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*/SCP_SWP, 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, int *sa); 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, NULL); 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 void 2241 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2242 { 2243 struct scsi_vpd_id_descriptor *desc; 2244 int i; 2245 2246 if (id == NULL || id->len < 4) 2247 return; 2248 desc = (struct scsi_vpd_id_descriptor *)id->data; 2249 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2250 case SVPD_ID_TYPE_T10: 2251 sbuf_printf(sb, "t10."); 2252 break; 2253 case SVPD_ID_TYPE_EUI64: 2254 sbuf_printf(sb, "eui."); 2255 break; 2256 case SVPD_ID_TYPE_NAA: 2257 sbuf_printf(sb, "naa."); 2258 break; 2259 case SVPD_ID_TYPE_SCSI_NAME: 2260 break; 2261 } 2262 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2263 case SVPD_ID_CODESET_BINARY: 2264 for (i = 0; i < desc->length; i++) 2265 sbuf_printf(sb, "%02x", desc->identifier[i]); 2266 break; 2267 case SVPD_ID_CODESET_ASCII: 2268 sbuf_printf(sb, "%.*s", (int)desc->length, 2269 (char *)desc->identifier); 2270 break; 2271 case SVPD_ID_CODESET_UTF8: 2272 sbuf_printf(sb, "%s", (char *)desc->identifier); 2273 break; 2274 } 2275 } 2276 2277 static int 2278 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2279 struct thread *td) 2280 { 2281 struct ctl_softc *softc; 2282 int retval; 2283 2284 softc = control_softc; 2285 2286 retval = 0; 2287 2288 switch (cmd) { 2289 case CTL_IO: { 2290 union ctl_io *io; 2291 void *pool_tmp; 2292 2293 /* 2294 * If we haven't been "enabled", don't allow any SCSI I/O 2295 * to this FETD. 2296 */ 2297 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2298 retval = EPERM; 2299 break; 2300 } 2301 2302 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2303 if (io == NULL) { 2304 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2305 retval = ENOSPC; 2306 break; 2307 } 2308 2309 /* 2310 * Need to save the pool reference so it doesn't get 2311 * spammed by the user's ctl_io. 2312 */ 2313 pool_tmp = io->io_hdr.pool; 2314 2315 memcpy(io, (void *)addr, sizeof(*io)); 2316 2317 io->io_hdr.pool = pool_tmp; 2318 /* 2319 * No status yet, so make sure the status is set properly. 2320 */ 2321 io->io_hdr.status = CTL_STATUS_NONE; 2322 2323 /* 2324 * The user sets the initiator ID, target and LUN IDs. 2325 */ 2326 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2327 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2328 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2329 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2330 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2331 2332 retval = ctl_ioctl_submit_wait(io); 2333 2334 if (retval != 0) { 2335 ctl_free_io(io); 2336 break; 2337 } 2338 2339 memcpy((void *)addr, io, sizeof(*io)); 2340 2341 /* return this to our pool */ 2342 ctl_free_io(io); 2343 2344 break; 2345 } 2346 case CTL_ENABLE_PORT: 2347 case CTL_DISABLE_PORT: 2348 case CTL_SET_PORT_WWNS: { 2349 struct ctl_port *port; 2350 struct ctl_port_entry *entry; 2351 2352 entry = (struct ctl_port_entry *)addr; 2353 2354 mtx_lock(&softc->ctl_lock); 2355 STAILQ_FOREACH(port, &softc->port_list, links) { 2356 int action, done; 2357 2358 action = 0; 2359 done = 0; 2360 2361 if ((entry->port_type == CTL_PORT_NONE) 2362 && (entry->targ_port == port->targ_port)) { 2363 /* 2364 * If the user only wants to enable or 2365 * disable or set WWNs on a specific port, 2366 * do the operation and we're done. 2367 */ 2368 action = 1; 2369 done = 1; 2370 } else if (entry->port_type & port->port_type) { 2371 /* 2372 * Compare the user's type mask with the 2373 * particular frontend type to see if we 2374 * have a match. 2375 */ 2376 action = 1; 2377 done = 0; 2378 2379 /* 2380 * Make sure the user isn't trying to set 2381 * WWNs on multiple ports at the same time. 2382 */ 2383 if (cmd == CTL_SET_PORT_WWNS) { 2384 printf("%s: Can't set WWNs on " 2385 "multiple ports\n", __func__); 2386 retval = EINVAL; 2387 break; 2388 } 2389 } 2390 if (action != 0) { 2391 /* 2392 * XXX KDM we have to drop the lock here, 2393 * because the online/offline operations 2394 * can potentially block. We need to 2395 * reference count the frontends so they 2396 * can't go away, 2397 */ 2398 mtx_unlock(&softc->ctl_lock); 2399 2400 if (cmd == CTL_ENABLE_PORT) { 2401 struct ctl_lun *lun; 2402 2403 STAILQ_FOREACH(lun, &softc->lun_list, 2404 links) { 2405 port->lun_enable(port->targ_lun_arg, 2406 lun->target, 2407 lun->lun); 2408 } 2409 2410 ctl_port_online(port); 2411 } else if (cmd == CTL_DISABLE_PORT) { 2412 struct ctl_lun *lun; 2413 2414 ctl_port_offline(port); 2415 2416 STAILQ_FOREACH(lun, &softc->lun_list, 2417 links) { 2418 port->lun_disable( 2419 port->targ_lun_arg, 2420 lun->target, 2421 lun->lun); 2422 } 2423 } 2424 2425 mtx_lock(&softc->ctl_lock); 2426 2427 if (cmd == CTL_SET_PORT_WWNS) 2428 ctl_port_set_wwns(port, 2429 (entry->flags & CTL_PORT_WWNN_VALID) ? 2430 1 : 0, entry->wwnn, 2431 (entry->flags & CTL_PORT_WWPN_VALID) ? 2432 1 : 0, entry->wwpn); 2433 } 2434 if (done != 0) 2435 break; 2436 } 2437 mtx_unlock(&softc->ctl_lock); 2438 break; 2439 } 2440 case CTL_GET_PORT_LIST: { 2441 struct ctl_port *port; 2442 struct ctl_port_list *list; 2443 int i; 2444 2445 list = (struct ctl_port_list *)addr; 2446 2447 if (list->alloc_len != (list->alloc_num * 2448 sizeof(struct ctl_port_entry))) { 2449 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2450 "alloc_num %u * sizeof(struct ctl_port_entry) " 2451 "%zu\n", __func__, list->alloc_len, 2452 list->alloc_num, sizeof(struct ctl_port_entry)); 2453 retval = EINVAL; 2454 break; 2455 } 2456 list->fill_len = 0; 2457 list->fill_num = 0; 2458 list->dropped_num = 0; 2459 i = 0; 2460 mtx_lock(&softc->ctl_lock); 2461 STAILQ_FOREACH(port, &softc->port_list, links) { 2462 struct ctl_port_entry entry, *list_entry; 2463 2464 if (list->fill_num >= list->alloc_num) { 2465 list->dropped_num++; 2466 continue; 2467 } 2468 2469 entry.port_type = port->port_type; 2470 strlcpy(entry.port_name, port->port_name, 2471 sizeof(entry.port_name)); 2472 entry.targ_port = port->targ_port; 2473 entry.physical_port = port->physical_port; 2474 entry.virtual_port = port->virtual_port; 2475 entry.wwnn = port->wwnn; 2476 entry.wwpn = port->wwpn; 2477 if (port->status & CTL_PORT_STATUS_ONLINE) 2478 entry.online = 1; 2479 else 2480 entry.online = 0; 2481 2482 list_entry = &list->entries[i]; 2483 2484 retval = copyout(&entry, list_entry, sizeof(entry)); 2485 if (retval != 0) { 2486 printf("%s: CTL_GET_PORT_LIST: copyout " 2487 "returned %d\n", __func__, retval); 2488 break; 2489 } 2490 i++; 2491 list->fill_num++; 2492 list->fill_len += sizeof(entry); 2493 } 2494 mtx_unlock(&softc->ctl_lock); 2495 2496 /* 2497 * If this is non-zero, we had a copyout fault, so there's 2498 * probably no point in attempting to set the status inside 2499 * the structure. 2500 */ 2501 if (retval != 0) 2502 break; 2503 2504 if (list->dropped_num > 0) 2505 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2506 else 2507 list->status = CTL_PORT_LIST_OK; 2508 break; 2509 } 2510 case CTL_DUMP_OOA: { 2511 struct ctl_lun *lun; 2512 union ctl_io *io; 2513 char printbuf[128]; 2514 struct sbuf sb; 2515 2516 mtx_lock(&softc->ctl_lock); 2517 printf("Dumping OOA queues:\n"); 2518 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2519 mtx_lock(&lun->lun_lock); 2520 for (io = (union ctl_io *)TAILQ_FIRST( 2521 &lun->ooa_queue); io != NULL; 2522 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2523 ooa_links)) { 2524 sbuf_new(&sb, printbuf, sizeof(printbuf), 2525 SBUF_FIXEDLEN); 2526 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2527 (intmax_t)lun->lun, 2528 io->scsiio.tag_num, 2529 (io->io_hdr.flags & 2530 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2531 (io->io_hdr.flags & 2532 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2533 (io->io_hdr.flags & 2534 CTL_FLAG_ABORT) ? " ABORT" : "", 2535 (io->io_hdr.flags & 2536 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2537 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2538 sbuf_finish(&sb); 2539 printf("%s\n", sbuf_data(&sb)); 2540 } 2541 mtx_unlock(&lun->lun_lock); 2542 } 2543 printf("OOA queues dump done\n"); 2544 mtx_unlock(&softc->ctl_lock); 2545 break; 2546 } 2547 case CTL_GET_OOA: { 2548 struct ctl_lun *lun; 2549 struct ctl_ooa *ooa_hdr; 2550 struct ctl_ooa_entry *entries; 2551 uint32_t cur_fill_num; 2552 2553 ooa_hdr = (struct ctl_ooa *)addr; 2554 2555 if ((ooa_hdr->alloc_len == 0) 2556 || (ooa_hdr->alloc_num == 0)) { 2557 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2558 "must be non-zero\n", __func__, 2559 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2560 retval = EINVAL; 2561 break; 2562 } 2563 2564 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2565 sizeof(struct ctl_ooa_entry))) { 2566 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2567 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2568 __func__, ooa_hdr->alloc_len, 2569 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2570 retval = EINVAL; 2571 break; 2572 } 2573 2574 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2575 if (entries == NULL) { 2576 printf("%s: could not allocate %d bytes for OOA " 2577 "dump\n", __func__, ooa_hdr->alloc_len); 2578 retval = ENOMEM; 2579 break; 2580 } 2581 2582 mtx_lock(&softc->ctl_lock); 2583 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2584 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2585 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2586 mtx_unlock(&softc->ctl_lock); 2587 free(entries, M_CTL); 2588 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2589 __func__, (uintmax_t)ooa_hdr->lun_num); 2590 retval = EINVAL; 2591 break; 2592 } 2593 2594 cur_fill_num = 0; 2595 2596 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2597 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2598 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2599 ooa_hdr, entries); 2600 if (retval != 0) 2601 break; 2602 } 2603 if (retval != 0) { 2604 mtx_unlock(&softc->ctl_lock); 2605 free(entries, M_CTL); 2606 break; 2607 } 2608 } else { 2609 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2610 2611 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2612 entries); 2613 } 2614 mtx_unlock(&softc->ctl_lock); 2615 2616 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2617 ooa_hdr->fill_len = ooa_hdr->fill_num * 2618 sizeof(struct ctl_ooa_entry); 2619 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2620 if (retval != 0) { 2621 printf("%s: error copying out %d bytes for OOA dump\n", 2622 __func__, ooa_hdr->fill_len); 2623 } 2624 2625 getbintime(&ooa_hdr->cur_bt); 2626 2627 if (cur_fill_num > ooa_hdr->alloc_num) { 2628 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2629 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2630 } else { 2631 ooa_hdr->dropped_num = 0; 2632 ooa_hdr->status = CTL_OOA_OK; 2633 } 2634 2635 free(entries, M_CTL); 2636 break; 2637 } 2638 case CTL_CHECK_OOA: { 2639 union ctl_io *io; 2640 struct ctl_lun *lun; 2641 struct ctl_ooa_info *ooa_info; 2642 2643 2644 ooa_info = (struct ctl_ooa_info *)addr; 2645 2646 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2647 ooa_info->status = CTL_OOA_INVALID_LUN; 2648 break; 2649 } 2650 mtx_lock(&softc->ctl_lock); 2651 lun = softc->ctl_luns[ooa_info->lun_id]; 2652 if (lun == NULL) { 2653 mtx_unlock(&softc->ctl_lock); 2654 ooa_info->status = CTL_OOA_INVALID_LUN; 2655 break; 2656 } 2657 mtx_lock(&lun->lun_lock); 2658 mtx_unlock(&softc->ctl_lock); 2659 ooa_info->num_entries = 0; 2660 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2661 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2662 &io->io_hdr, ooa_links)) { 2663 ooa_info->num_entries++; 2664 } 2665 mtx_unlock(&lun->lun_lock); 2666 2667 ooa_info->status = CTL_OOA_SUCCESS; 2668 2669 break; 2670 } 2671 case CTL_HARD_START: 2672 case CTL_HARD_STOP: { 2673 struct ctl_fe_ioctl_startstop_info ss_info; 2674 struct cfi_metatask *metatask; 2675 struct mtx hs_mtx; 2676 2677 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2678 2679 cv_init(&ss_info.sem, "hard start/stop cv" ); 2680 2681 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2682 if (metatask == NULL) { 2683 retval = ENOMEM; 2684 mtx_destroy(&hs_mtx); 2685 break; 2686 } 2687 2688 if (cmd == CTL_HARD_START) 2689 metatask->tasktype = CFI_TASK_STARTUP; 2690 else 2691 metatask->tasktype = CFI_TASK_SHUTDOWN; 2692 2693 metatask->callback = ctl_ioctl_hard_startstop_callback; 2694 metatask->callback_arg = &ss_info; 2695 2696 cfi_action(metatask); 2697 2698 /* Wait for the callback */ 2699 mtx_lock(&hs_mtx); 2700 cv_wait_sig(&ss_info.sem, &hs_mtx); 2701 mtx_unlock(&hs_mtx); 2702 2703 /* 2704 * All information has been copied from the metatask by the 2705 * time cv_broadcast() is called, so we free the metatask here. 2706 */ 2707 cfi_free_metatask(metatask); 2708 2709 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2710 2711 mtx_destroy(&hs_mtx); 2712 break; 2713 } 2714 case CTL_BBRREAD: { 2715 struct ctl_bbrread_info *bbr_info; 2716 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2717 struct mtx bbr_mtx; 2718 struct cfi_metatask *metatask; 2719 2720 bbr_info = (struct ctl_bbrread_info *)addr; 2721 2722 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2723 2724 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2725 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2726 2727 fe_bbr_info.bbr_info = bbr_info; 2728 fe_bbr_info.lock = &bbr_mtx; 2729 2730 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2731 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2732 2733 if (metatask == NULL) { 2734 mtx_destroy(&bbr_mtx); 2735 cv_destroy(&fe_bbr_info.sem); 2736 retval = ENOMEM; 2737 break; 2738 } 2739 metatask->tasktype = CFI_TASK_BBRREAD; 2740 metatask->callback = ctl_ioctl_bbrread_callback; 2741 metatask->callback_arg = &fe_bbr_info; 2742 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2743 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2744 metatask->taskinfo.bbrread.len = bbr_info->len; 2745 2746 cfi_action(metatask); 2747 2748 mtx_lock(&bbr_mtx); 2749 while (fe_bbr_info.wakeup_done == 0) 2750 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2751 mtx_unlock(&bbr_mtx); 2752 2753 bbr_info->status = metatask->status; 2754 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2755 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2756 memcpy(&bbr_info->sense_data, 2757 &metatask->taskinfo.bbrread.sense_data, 2758 ctl_min(sizeof(bbr_info->sense_data), 2759 sizeof(metatask->taskinfo.bbrread.sense_data))); 2760 2761 cfi_free_metatask(metatask); 2762 2763 mtx_destroy(&bbr_mtx); 2764 cv_destroy(&fe_bbr_info.sem); 2765 2766 break; 2767 } 2768 case CTL_DELAY_IO: { 2769 struct ctl_io_delay_info *delay_info; 2770 #ifdef CTL_IO_DELAY 2771 struct ctl_lun *lun; 2772 #endif /* CTL_IO_DELAY */ 2773 2774 delay_info = (struct ctl_io_delay_info *)addr; 2775 2776 #ifdef CTL_IO_DELAY 2777 mtx_lock(&softc->ctl_lock); 2778 2779 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2780 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2781 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2782 } else { 2783 lun = softc->ctl_luns[delay_info->lun_id]; 2784 mtx_lock(&lun->lun_lock); 2785 2786 delay_info->status = CTL_DELAY_STATUS_OK; 2787 2788 switch (delay_info->delay_type) { 2789 case CTL_DELAY_TYPE_CONT: 2790 break; 2791 case CTL_DELAY_TYPE_ONESHOT: 2792 break; 2793 default: 2794 delay_info->status = 2795 CTL_DELAY_STATUS_INVALID_TYPE; 2796 break; 2797 } 2798 2799 switch (delay_info->delay_loc) { 2800 case CTL_DELAY_LOC_DATAMOVE: 2801 lun->delay_info.datamove_type = 2802 delay_info->delay_type; 2803 lun->delay_info.datamove_delay = 2804 delay_info->delay_secs; 2805 break; 2806 case CTL_DELAY_LOC_DONE: 2807 lun->delay_info.done_type = 2808 delay_info->delay_type; 2809 lun->delay_info.done_delay = 2810 delay_info->delay_secs; 2811 break; 2812 default: 2813 delay_info->status = 2814 CTL_DELAY_STATUS_INVALID_LOC; 2815 break; 2816 } 2817 mtx_unlock(&lun->lun_lock); 2818 } 2819 2820 mtx_unlock(&softc->ctl_lock); 2821 #else 2822 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2823 #endif /* CTL_IO_DELAY */ 2824 break; 2825 } 2826 case CTL_REALSYNC_SET: { 2827 int *syncstate; 2828 2829 syncstate = (int *)addr; 2830 2831 mtx_lock(&softc->ctl_lock); 2832 switch (*syncstate) { 2833 case 0: 2834 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2835 break; 2836 case 1: 2837 softc->flags |= CTL_FLAG_REAL_SYNC; 2838 break; 2839 default: 2840 retval = EINVAL; 2841 break; 2842 } 2843 mtx_unlock(&softc->ctl_lock); 2844 break; 2845 } 2846 case CTL_REALSYNC_GET: { 2847 int *syncstate; 2848 2849 syncstate = (int*)addr; 2850 2851 mtx_lock(&softc->ctl_lock); 2852 if (softc->flags & CTL_FLAG_REAL_SYNC) 2853 *syncstate = 1; 2854 else 2855 *syncstate = 0; 2856 mtx_unlock(&softc->ctl_lock); 2857 2858 break; 2859 } 2860 case CTL_SETSYNC: 2861 case CTL_GETSYNC: { 2862 struct ctl_sync_info *sync_info; 2863 struct ctl_lun *lun; 2864 2865 sync_info = (struct ctl_sync_info *)addr; 2866 2867 mtx_lock(&softc->ctl_lock); 2868 lun = softc->ctl_luns[sync_info->lun_id]; 2869 if (lun == NULL) { 2870 mtx_unlock(&softc->ctl_lock); 2871 sync_info->status = CTL_GS_SYNC_NO_LUN; 2872 } 2873 /* 2874 * Get or set the sync interval. We're not bounds checking 2875 * in the set case, hopefully the user won't do something 2876 * silly. 2877 */ 2878 mtx_lock(&lun->lun_lock); 2879 mtx_unlock(&softc->ctl_lock); 2880 if (cmd == CTL_GETSYNC) 2881 sync_info->sync_interval = lun->sync_interval; 2882 else 2883 lun->sync_interval = sync_info->sync_interval; 2884 mtx_unlock(&lun->lun_lock); 2885 2886 sync_info->status = CTL_GS_SYNC_OK; 2887 2888 break; 2889 } 2890 case CTL_GETSTATS: { 2891 struct ctl_stats *stats; 2892 struct ctl_lun *lun; 2893 int i; 2894 2895 stats = (struct ctl_stats *)addr; 2896 2897 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2898 stats->alloc_len) { 2899 stats->status = CTL_SS_NEED_MORE_SPACE; 2900 stats->num_luns = softc->num_luns; 2901 break; 2902 } 2903 /* 2904 * XXX KDM no locking here. If the LUN list changes, 2905 * things can blow up. 2906 */ 2907 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2908 i++, lun = STAILQ_NEXT(lun, links)) { 2909 retval = copyout(&lun->stats, &stats->lun_stats[i], 2910 sizeof(lun->stats)); 2911 if (retval != 0) 2912 break; 2913 } 2914 stats->num_luns = softc->num_luns; 2915 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2916 softc->num_luns; 2917 stats->status = CTL_SS_OK; 2918 #ifdef CTL_TIME_IO 2919 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2920 #else 2921 stats->flags = CTL_STATS_FLAG_NONE; 2922 #endif 2923 getnanouptime(&stats->timestamp); 2924 break; 2925 } 2926 case CTL_ERROR_INJECT: { 2927 struct ctl_error_desc *err_desc, *new_err_desc; 2928 struct ctl_lun *lun; 2929 2930 err_desc = (struct ctl_error_desc *)addr; 2931 2932 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2933 M_WAITOK | M_ZERO); 2934 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2935 2936 mtx_lock(&softc->ctl_lock); 2937 lun = softc->ctl_luns[err_desc->lun_id]; 2938 if (lun == NULL) { 2939 mtx_unlock(&softc->ctl_lock); 2940 free(new_err_desc, M_CTL); 2941 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2942 __func__, (uintmax_t)err_desc->lun_id); 2943 retval = EINVAL; 2944 break; 2945 } 2946 mtx_lock(&lun->lun_lock); 2947 mtx_unlock(&softc->ctl_lock); 2948 2949 /* 2950 * We could do some checking here to verify the validity 2951 * of the request, but given the complexity of error 2952 * injection requests, the checking logic would be fairly 2953 * complex. 2954 * 2955 * For now, if the request is invalid, it just won't get 2956 * executed and might get deleted. 2957 */ 2958 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2959 2960 /* 2961 * XXX KDM check to make sure the serial number is unique, 2962 * in case we somehow manage to wrap. That shouldn't 2963 * happen for a very long time, but it's the right thing to 2964 * do. 2965 */ 2966 new_err_desc->serial = lun->error_serial; 2967 err_desc->serial = lun->error_serial; 2968 lun->error_serial++; 2969 2970 mtx_unlock(&lun->lun_lock); 2971 break; 2972 } 2973 case CTL_ERROR_INJECT_DELETE: { 2974 struct ctl_error_desc *delete_desc, *desc, *desc2; 2975 struct ctl_lun *lun; 2976 int delete_done; 2977 2978 delete_desc = (struct ctl_error_desc *)addr; 2979 delete_done = 0; 2980 2981 mtx_lock(&softc->ctl_lock); 2982 lun = softc->ctl_luns[delete_desc->lun_id]; 2983 if (lun == NULL) { 2984 mtx_unlock(&softc->ctl_lock); 2985 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2986 __func__, (uintmax_t)delete_desc->lun_id); 2987 retval = EINVAL; 2988 break; 2989 } 2990 mtx_lock(&lun->lun_lock); 2991 mtx_unlock(&softc->ctl_lock); 2992 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2993 if (desc->serial != delete_desc->serial) 2994 continue; 2995 2996 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2997 links); 2998 free(desc, M_CTL); 2999 delete_done = 1; 3000 } 3001 mtx_unlock(&lun->lun_lock); 3002 if (delete_done == 0) { 3003 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3004 "error serial %ju on LUN %u\n", __func__, 3005 delete_desc->serial, delete_desc->lun_id); 3006 retval = EINVAL; 3007 break; 3008 } 3009 break; 3010 } 3011 case CTL_DUMP_STRUCTS: { 3012 int i, j, k, idx; 3013 struct ctl_port *port; 3014 struct ctl_frontend *fe; 3015 3016 mtx_lock(&softc->ctl_lock); 3017 printf("CTL Persistent Reservation information start:\n"); 3018 for (i = 0; i < CTL_MAX_LUNS; i++) { 3019 struct ctl_lun *lun; 3020 3021 lun = softc->ctl_luns[i]; 3022 3023 if ((lun == NULL) 3024 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3025 continue; 3026 3027 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3028 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3029 idx = j * CTL_MAX_INIT_PER_PORT + k; 3030 if (lun->pr_keys[idx] == 0) 3031 continue; 3032 printf(" LUN %d port %d iid %d key " 3033 "%#jx\n", i, j, k, 3034 (uintmax_t)lun->pr_keys[idx]); 3035 } 3036 } 3037 } 3038 printf("CTL Persistent Reservation information end\n"); 3039 printf("CTL Ports:\n"); 3040 STAILQ_FOREACH(port, &softc->port_list, links) { 3041 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3042 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3043 port->frontend->name, port->port_type, 3044 port->physical_port, port->virtual_port, 3045 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3046 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3047 if (port->wwpn_iid[j].in_use == 0 && 3048 port->wwpn_iid[j].wwpn == 0 && 3049 port->wwpn_iid[j].name == NULL) 3050 continue; 3051 3052 printf(" iid %u use %d WWPN %#jx '%s'\n", 3053 j, port->wwpn_iid[j].in_use, 3054 (uintmax_t)port->wwpn_iid[j].wwpn, 3055 port->wwpn_iid[j].name); 3056 } 3057 } 3058 printf("CTL Port information end\n"); 3059 mtx_unlock(&softc->ctl_lock); 3060 /* 3061 * XXX KDM calling this without a lock. We'd likely want 3062 * to drop the lock before calling the frontend's dump 3063 * routine anyway. 3064 */ 3065 printf("CTL Frontends:\n"); 3066 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3067 printf(" Frontend '%s'\n", fe->name); 3068 if (fe->fe_dump != NULL) 3069 fe->fe_dump(); 3070 } 3071 printf("CTL Frontend information end\n"); 3072 break; 3073 } 3074 case CTL_LUN_REQ: { 3075 struct ctl_lun_req *lun_req; 3076 struct ctl_backend_driver *backend; 3077 3078 lun_req = (struct ctl_lun_req *)addr; 3079 3080 backend = ctl_backend_find(lun_req->backend); 3081 if (backend == NULL) { 3082 lun_req->status = CTL_LUN_ERROR; 3083 snprintf(lun_req->error_str, 3084 sizeof(lun_req->error_str), 3085 "Backend \"%s\" not found.", 3086 lun_req->backend); 3087 break; 3088 } 3089 if (lun_req->num_be_args > 0) { 3090 lun_req->kern_be_args = ctl_copyin_args( 3091 lun_req->num_be_args, 3092 lun_req->be_args, 3093 lun_req->error_str, 3094 sizeof(lun_req->error_str)); 3095 if (lun_req->kern_be_args == NULL) { 3096 lun_req->status = CTL_LUN_ERROR; 3097 break; 3098 } 3099 } 3100 3101 retval = backend->ioctl(dev, cmd, addr, flag, td); 3102 3103 if (lun_req->num_be_args > 0) { 3104 ctl_copyout_args(lun_req->num_be_args, 3105 lun_req->kern_be_args); 3106 ctl_free_args(lun_req->num_be_args, 3107 lun_req->kern_be_args); 3108 } 3109 break; 3110 } 3111 case CTL_LUN_LIST: { 3112 struct sbuf *sb; 3113 struct ctl_lun *lun; 3114 struct ctl_lun_list *list; 3115 struct ctl_option *opt; 3116 3117 list = (struct ctl_lun_list *)addr; 3118 3119 /* 3120 * Allocate a fixed length sbuf here, based on the length 3121 * of the user's buffer. We could allocate an auto-extending 3122 * buffer, and then tell the user how much larger our 3123 * amount of data is than his buffer, but that presents 3124 * some problems: 3125 * 3126 * 1. The sbuf(9) routines use a blocking malloc, and so 3127 * we can't hold a lock while calling them with an 3128 * auto-extending buffer. 3129 * 3130 * 2. There is not currently a LUN reference counting 3131 * mechanism, outside of outstanding transactions on 3132 * the LUN's OOA queue. So a LUN could go away on us 3133 * while we're getting the LUN number, backend-specific 3134 * information, etc. Thus, given the way things 3135 * currently work, we need to hold the CTL lock while 3136 * grabbing LUN information. 3137 * 3138 * So, from the user's standpoint, the best thing to do is 3139 * allocate what he thinks is a reasonable buffer length, 3140 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3141 * double the buffer length and try again. (And repeat 3142 * that until he succeeds.) 3143 */ 3144 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3145 if (sb == NULL) { 3146 list->status = CTL_LUN_LIST_ERROR; 3147 snprintf(list->error_str, sizeof(list->error_str), 3148 "Unable to allocate %d bytes for LUN list", 3149 list->alloc_len); 3150 break; 3151 } 3152 3153 sbuf_printf(sb, "<ctllunlist>\n"); 3154 3155 mtx_lock(&softc->ctl_lock); 3156 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3157 mtx_lock(&lun->lun_lock); 3158 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3159 (uintmax_t)lun->lun); 3160 3161 /* 3162 * Bail out as soon as we see that we've overfilled 3163 * the buffer. 3164 */ 3165 if (retval != 0) 3166 break; 3167 3168 retval = sbuf_printf(sb, "\t<backend_type>%s" 3169 "</backend_type>\n", 3170 (lun->backend == NULL) ? "none" : 3171 lun->backend->name); 3172 3173 if (retval != 0) 3174 break; 3175 3176 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3177 lun->be_lun->lun_type); 3178 3179 if (retval != 0) 3180 break; 3181 3182 if (lun->backend == NULL) { 3183 retval = sbuf_printf(sb, "</lun>\n"); 3184 if (retval != 0) 3185 break; 3186 continue; 3187 } 3188 3189 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3190 (lun->be_lun->maxlba > 0) ? 3191 lun->be_lun->maxlba + 1 : 0); 3192 3193 if (retval != 0) 3194 break; 3195 3196 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3197 lun->be_lun->blocksize); 3198 3199 if (retval != 0) 3200 break; 3201 3202 retval = sbuf_printf(sb, "\t<serial_number>"); 3203 3204 if (retval != 0) 3205 break; 3206 3207 retval = ctl_sbuf_printf_esc(sb, 3208 lun->be_lun->serial_num); 3209 3210 if (retval != 0) 3211 break; 3212 3213 retval = sbuf_printf(sb, "</serial_number>\n"); 3214 3215 if (retval != 0) 3216 break; 3217 3218 retval = sbuf_printf(sb, "\t<device_id>"); 3219 3220 if (retval != 0) 3221 break; 3222 3223 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3224 3225 if (retval != 0) 3226 break; 3227 3228 retval = sbuf_printf(sb, "</device_id>\n"); 3229 3230 if (retval != 0) 3231 break; 3232 3233 if (lun->backend->lun_info != NULL) { 3234 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3235 if (retval != 0) 3236 break; 3237 } 3238 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3239 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3240 opt->name, opt->value, opt->name); 3241 if (retval != 0) 3242 break; 3243 } 3244 3245 retval = sbuf_printf(sb, "</lun>\n"); 3246 3247 if (retval != 0) 3248 break; 3249 mtx_unlock(&lun->lun_lock); 3250 } 3251 if (lun != NULL) 3252 mtx_unlock(&lun->lun_lock); 3253 mtx_unlock(&softc->ctl_lock); 3254 3255 if ((retval != 0) 3256 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3257 retval = 0; 3258 sbuf_delete(sb); 3259 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3260 snprintf(list->error_str, sizeof(list->error_str), 3261 "Out of space, %d bytes is too small", 3262 list->alloc_len); 3263 break; 3264 } 3265 3266 sbuf_finish(sb); 3267 3268 retval = copyout(sbuf_data(sb), list->lun_xml, 3269 sbuf_len(sb) + 1); 3270 3271 list->fill_len = sbuf_len(sb) + 1; 3272 list->status = CTL_LUN_LIST_OK; 3273 sbuf_delete(sb); 3274 break; 3275 } 3276 case CTL_ISCSI: { 3277 struct ctl_iscsi *ci; 3278 struct ctl_frontend *fe; 3279 3280 ci = (struct ctl_iscsi *)addr; 3281 3282 fe = ctl_frontend_find("iscsi"); 3283 if (fe == NULL) { 3284 ci->status = CTL_ISCSI_ERROR; 3285 snprintf(ci->error_str, sizeof(ci->error_str), 3286 "Frontend \"iscsi\" not found."); 3287 break; 3288 } 3289 3290 retval = fe->ioctl(dev, cmd, addr, flag, td); 3291 break; 3292 } 3293 case CTL_PORT_REQ: { 3294 struct ctl_req *req; 3295 struct ctl_frontend *fe; 3296 3297 req = (struct ctl_req *)addr; 3298 3299 fe = ctl_frontend_find(req->driver); 3300 if (fe == NULL) { 3301 req->status = CTL_LUN_ERROR; 3302 snprintf(req->error_str, sizeof(req->error_str), 3303 "Frontend \"%s\" not found.", req->driver); 3304 break; 3305 } 3306 if (req->num_args > 0) { 3307 req->kern_args = ctl_copyin_args(req->num_args, 3308 req->args, req->error_str, sizeof(req->error_str)); 3309 if (req->kern_args == NULL) { 3310 req->status = CTL_LUN_ERROR; 3311 break; 3312 } 3313 } 3314 3315 retval = fe->ioctl(dev, cmd, addr, flag, td); 3316 3317 if (req->num_args > 0) { 3318 ctl_copyout_args(req->num_args, req->kern_args); 3319 ctl_free_args(req->num_args, req->kern_args); 3320 } 3321 break; 3322 } 3323 case CTL_PORT_LIST: { 3324 struct sbuf *sb; 3325 struct ctl_port *port; 3326 struct ctl_lun_list *list; 3327 struct ctl_option *opt; 3328 int j; 3329 3330 list = (struct ctl_lun_list *)addr; 3331 3332 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3333 if (sb == NULL) { 3334 list->status = CTL_LUN_LIST_ERROR; 3335 snprintf(list->error_str, sizeof(list->error_str), 3336 "Unable to allocate %d bytes for LUN list", 3337 list->alloc_len); 3338 break; 3339 } 3340 3341 sbuf_printf(sb, "<ctlportlist>\n"); 3342 3343 mtx_lock(&softc->ctl_lock); 3344 STAILQ_FOREACH(port, &softc->port_list, links) { 3345 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3346 (uintmax_t)port->targ_port); 3347 3348 /* 3349 * Bail out as soon as we see that we've overfilled 3350 * the buffer. 3351 */ 3352 if (retval != 0) 3353 break; 3354 3355 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3356 "</frontend_type>\n", port->frontend->name); 3357 if (retval != 0) 3358 break; 3359 3360 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3361 port->port_type); 3362 if (retval != 0) 3363 break; 3364 3365 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3366 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3367 if (retval != 0) 3368 break; 3369 3370 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3371 port->port_name); 3372 if (retval != 0) 3373 break; 3374 3375 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3376 port->physical_port); 3377 if (retval != 0) 3378 break; 3379 3380 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3381 port->virtual_port); 3382 if (retval != 0) 3383 break; 3384 3385 if (port->target_devid != NULL) { 3386 sbuf_printf(sb, "\t<target>"); 3387 ctl_id_sbuf(port->target_devid, sb); 3388 sbuf_printf(sb, "</target>\n"); 3389 } 3390 3391 if (port->port_devid != NULL) { 3392 sbuf_printf(sb, "\t<port>"); 3393 ctl_id_sbuf(port->port_devid, sb); 3394 sbuf_printf(sb, "</port>\n"); 3395 } 3396 3397 if (port->port_info != NULL) { 3398 retval = port->port_info(port->onoff_arg, sb); 3399 if (retval != 0) 3400 break; 3401 } 3402 STAILQ_FOREACH(opt, &port->options, links) { 3403 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3404 opt->name, opt->value, opt->name); 3405 if (retval != 0) 3406 break; 3407 } 3408 3409 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3410 if (port->wwpn_iid[j].in_use == 0 || 3411 (port->wwpn_iid[j].wwpn == 0 && 3412 port->wwpn_iid[j].name == NULL)) 3413 continue; 3414 3415 if (port->wwpn_iid[j].name != NULL) 3416 retval = sbuf_printf(sb, 3417 "\t<initiator>%u %s</initiator>\n", 3418 j, port->wwpn_iid[j].name); 3419 else 3420 retval = sbuf_printf(sb, 3421 "\t<initiator>%u naa.%08jx</initiator>\n", 3422 j, port->wwpn_iid[j].wwpn); 3423 if (retval != 0) 3424 break; 3425 } 3426 if (retval != 0) 3427 break; 3428 3429 retval = sbuf_printf(sb, "</targ_port>\n"); 3430 if (retval != 0) 3431 break; 3432 } 3433 mtx_unlock(&softc->ctl_lock); 3434 3435 if ((retval != 0) 3436 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3437 retval = 0; 3438 sbuf_delete(sb); 3439 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3440 snprintf(list->error_str, sizeof(list->error_str), 3441 "Out of space, %d bytes is too small", 3442 list->alloc_len); 3443 break; 3444 } 3445 3446 sbuf_finish(sb); 3447 3448 retval = copyout(sbuf_data(sb), list->lun_xml, 3449 sbuf_len(sb) + 1); 3450 3451 list->fill_len = sbuf_len(sb) + 1; 3452 list->status = CTL_LUN_LIST_OK; 3453 sbuf_delete(sb); 3454 break; 3455 } 3456 default: { 3457 /* XXX KDM should we fix this? */ 3458 #if 0 3459 struct ctl_backend_driver *backend; 3460 unsigned int type; 3461 int found; 3462 3463 found = 0; 3464 3465 /* 3466 * We encode the backend type as the ioctl type for backend 3467 * ioctls. So parse it out here, and then search for a 3468 * backend of this type. 3469 */ 3470 type = _IOC_TYPE(cmd); 3471 3472 STAILQ_FOREACH(backend, &softc->be_list, links) { 3473 if (backend->type == type) { 3474 found = 1; 3475 break; 3476 } 3477 } 3478 if (found == 0) { 3479 printf("ctl: unknown ioctl command %#lx or backend " 3480 "%d\n", cmd, type); 3481 retval = EINVAL; 3482 break; 3483 } 3484 retval = backend->ioctl(dev, cmd, addr, flag, td); 3485 #endif 3486 retval = ENOTTY; 3487 break; 3488 } 3489 } 3490 return (retval); 3491 } 3492 3493 uint32_t 3494 ctl_get_initindex(struct ctl_nexus *nexus) 3495 { 3496 if (nexus->targ_port < CTL_MAX_PORTS) 3497 return (nexus->initid.id + 3498 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3499 else 3500 return (nexus->initid.id + 3501 ((nexus->targ_port - CTL_MAX_PORTS) * 3502 CTL_MAX_INIT_PER_PORT)); 3503 } 3504 3505 uint32_t 3506 ctl_get_resindex(struct ctl_nexus *nexus) 3507 { 3508 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3509 } 3510 3511 uint32_t 3512 ctl_port_idx(int port_num) 3513 { 3514 if (port_num < CTL_MAX_PORTS) 3515 return(port_num); 3516 else 3517 return(port_num - CTL_MAX_PORTS); 3518 } 3519 3520 static uint32_t 3521 ctl_map_lun(int port_num, uint32_t lun_id) 3522 { 3523 struct ctl_port *port; 3524 3525 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3526 if (port == NULL) 3527 return (UINT32_MAX); 3528 if (port->lun_map == NULL) 3529 return (lun_id); 3530 return (port->lun_map(port->targ_lun_arg, lun_id)); 3531 } 3532 3533 static uint32_t 3534 ctl_map_lun_back(int port_num, uint32_t lun_id) 3535 { 3536 struct ctl_port *port; 3537 uint32_t i; 3538 3539 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3540 if (port->lun_map == NULL) 3541 return (lun_id); 3542 for (i = 0; i < CTL_MAX_LUNS; i++) { 3543 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3544 return (i); 3545 } 3546 return (UINT32_MAX); 3547 } 3548 3549 /* 3550 * Note: This only works for bitmask sizes that are at least 32 bits, and 3551 * that are a power of 2. 3552 */ 3553 int 3554 ctl_ffz(uint32_t *mask, uint32_t size) 3555 { 3556 uint32_t num_chunks, num_pieces; 3557 int i, j; 3558 3559 num_chunks = (size >> 5); 3560 if (num_chunks == 0) 3561 num_chunks++; 3562 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3563 3564 for (i = 0; i < num_chunks; i++) { 3565 for (j = 0; j < num_pieces; j++) { 3566 if ((mask[i] & (1 << j)) == 0) 3567 return ((i << 5) + j); 3568 } 3569 } 3570 3571 return (-1); 3572 } 3573 3574 int 3575 ctl_set_mask(uint32_t *mask, uint32_t bit) 3576 { 3577 uint32_t chunk, piece; 3578 3579 chunk = bit >> 5; 3580 piece = bit % (sizeof(uint32_t) * 8); 3581 3582 if ((mask[chunk] & (1 << piece)) != 0) 3583 return (-1); 3584 else 3585 mask[chunk] |= (1 << piece); 3586 3587 return (0); 3588 } 3589 3590 int 3591 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3592 { 3593 uint32_t chunk, piece; 3594 3595 chunk = bit >> 5; 3596 piece = bit % (sizeof(uint32_t) * 8); 3597 3598 if ((mask[chunk] & (1 << piece)) == 0) 3599 return (-1); 3600 else 3601 mask[chunk] &= ~(1 << piece); 3602 3603 return (0); 3604 } 3605 3606 int 3607 ctl_is_set(uint32_t *mask, uint32_t bit) 3608 { 3609 uint32_t chunk, piece; 3610 3611 chunk = bit >> 5; 3612 piece = bit % (sizeof(uint32_t) * 8); 3613 3614 if ((mask[chunk] & (1 << piece)) == 0) 3615 return (0); 3616 else 3617 return (1); 3618 } 3619 3620 #ifdef unused 3621 /* 3622 * The bus, target and lun are optional, they can be filled in later. 3623 * can_wait is used to determine whether we can wait on the malloc or not. 3624 */ 3625 union ctl_io* 3626 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3627 uint32_t targ_lun, int can_wait) 3628 { 3629 union ctl_io *io; 3630 3631 if (can_wait) 3632 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3633 else 3634 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3635 3636 if (io != NULL) { 3637 io->io_hdr.io_type = io_type; 3638 io->io_hdr.targ_port = targ_port; 3639 /* 3640 * XXX KDM this needs to change/go away. We need to move 3641 * to a preallocated pool of ctl_scsiio structures. 3642 */ 3643 io->io_hdr.nexus.targ_target.id = targ_target; 3644 io->io_hdr.nexus.targ_lun = targ_lun; 3645 } 3646 3647 return (io); 3648 } 3649 3650 void 3651 ctl_kfree_io(union ctl_io *io) 3652 { 3653 free(io, M_CTL); 3654 } 3655 #endif /* unused */ 3656 3657 /* 3658 * ctl_softc, pool_type, total_ctl_io are passed in. 3659 * npool is passed out. 3660 */ 3661 int 3662 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3663 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3664 { 3665 uint32_t i; 3666 union ctl_io *cur_io, *next_io; 3667 struct ctl_io_pool *pool; 3668 int retval; 3669 3670 retval = 0; 3671 3672 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3673 M_NOWAIT | M_ZERO); 3674 if (pool == NULL) { 3675 retval = ENOMEM; 3676 goto bailout; 3677 } 3678 3679 pool->type = pool_type; 3680 pool->ctl_softc = ctl_softc; 3681 3682 mtx_lock(&ctl_softc->pool_lock); 3683 pool->id = ctl_softc->cur_pool_id++; 3684 mtx_unlock(&ctl_softc->pool_lock); 3685 3686 pool->flags = CTL_POOL_FLAG_NONE; 3687 pool->refcount = 1; /* Reference for validity. */ 3688 STAILQ_INIT(&pool->free_queue); 3689 3690 /* 3691 * XXX KDM other options here: 3692 * - allocate a page at a time 3693 * - allocate one big chunk of memory. 3694 * Page allocation might work well, but would take a little more 3695 * tracking. 3696 */ 3697 for (i = 0; i < total_ctl_io; i++) { 3698 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3699 M_NOWAIT); 3700 if (cur_io == NULL) { 3701 retval = ENOMEM; 3702 break; 3703 } 3704 cur_io->io_hdr.pool = pool; 3705 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3706 pool->total_ctl_io++; 3707 pool->free_ctl_io++; 3708 } 3709 3710 if (retval != 0) { 3711 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3712 cur_io != NULL; cur_io = next_io) { 3713 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3714 links); 3715 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3716 ctl_io_hdr, links); 3717 free(cur_io, M_CTLIO); 3718 } 3719 3720 free(pool, M_CTL); 3721 goto bailout; 3722 } 3723 mtx_lock(&ctl_softc->pool_lock); 3724 ctl_softc->num_pools++; 3725 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3726 /* 3727 * Increment our usage count if this is an external consumer, so we 3728 * can't get unloaded until the external consumer (most likely a 3729 * FETD) unloads and frees his pool. 3730 * 3731 * XXX KDM will this increment the caller's module use count, or 3732 * mine? 3733 */ 3734 #if 0 3735 if ((pool_type != CTL_POOL_EMERGENCY) 3736 && (pool_type != CTL_POOL_INTERNAL) 3737 && (pool_type != CTL_POOL_4OTHERSC)) 3738 MOD_INC_USE_COUNT; 3739 #endif 3740 3741 mtx_unlock(&ctl_softc->pool_lock); 3742 3743 *npool = pool; 3744 3745 bailout: 3746 3747 return (retval); 3748 } 3749 3750 static int 3751 ctl_pool_acquire(struct ctl_io_pool *pool) 3752 { 3753 3754 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3755 3756 if (pool->flags & CTL_POOL_FLAG_INVALID) 3757 return (EINVAL); 3758 3759 pool->refcount++; 3760 3761 return (0); 3762 } 3763 3764 static void 3765 ctl_pool_release(struct ctl_io_pool *pool) 3766 { 3767 struct ctl_softc *ctl_softc = pool->ctl_softc; 3768 union ctl_io *io; 3769 3770 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3771 3772 if (--pool->refcount != 0) 3773 return; 3774 3775 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3776 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3777 links); 3778 free(io, M_CTLIO); 3779 } 3780 3781 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3782 ctl_softc->num_pools--; 3783 3784 /* 3785 * XXX KDM will this decrement the caller's usage count or mine? 3786 */ 3787 #if 0 3788 if ((pool->type != CTL_POOL_EMERGENCY) 3789 && (pool->type != CTL_POOL_INTERNAL) 3790 && (pool->type != CTL_POOL_4OTHERSC)) 3791 MOD_DEC_USE_COUNT; 3792 #endif 3793 3794 free(pool, M_CTL); 3795 } 3796 3797 void 3798 ctl_pool_free(struct ctl_io_pool *pool) 3799 { 3800 struct ctl_softc *ctl_softc; 3801 3802 if (pool == NULL) 3803 return; 3804 3805 ctl_softc = pool->ctl_softc; 3806 mtx_lock(&ctl_softc->pool_lock); 3807 pool->flags |= CTL_POOL_FLAG_INVALID; 3808 ctl_pool_release(pool); 3809 mtx_unlock(&ctl_softc->pool_lock); 3810 } 3811 3812 /* 3813 * This routine does not block (except for spinlocks of course). 3814 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3815 * possible. 3816 */ 3817 union ctl_io * 3818 ctl_alloc_io(void *pool_ref) 3819 { 3820 union ctl_io *io; 3821 struct ctl_softc *ctl_softc; 3822 struct ctl_io_pool *pool, *npool; 3823 struct ctl_io_pool *emergency_pool; 3824 3825 pool = (struct ctl_io_pool *)pool_ref; 3826 3827 if (pool == NULL) { 3828 printf("%s: pool is NULL\n", __func__); 3829 return (NULL); 3830 } 3831 3832 emergency_pool = NULL; 3833 3834 ctl_softc = pool->ctl_softc; 3835 3836 mtx_lock(&ctl_softc->pool_lock); 3837 /* 3838 * First, try to get the io structure from the user's pool. 3839 */ 3840 if (ctl_pool_acquire(pool) == 0) { 3841 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3842 if (io != NULL) { 3843 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3844 pool->total_allocated++; 3845 pool->free_ctl_io--; 3846 mtx_unlock(&ctl_softc->pool_lock); 3847 return (io); 3848 } else 3849 ctl_pool_release(pool); 3850 } 3851 /* 3852 * If he doesn't have any io structures left, search for an 3853 * emergency pool and grab one from there. 3854 */ 3855 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3856 if (npool->type != CTL_POOL_EMERGENCY) 3857 continue; 3858 3859 if (ctl_pool_acquire(npool) != 0) 3860 continue; 3861 3862 emergency_pool = npool; 3863 3864 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3865 if (io != NULL) { 3866 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3867 npool->total_allocated++; 3868 npool->free_ctl_io--; 3869 mtx_unlock(&ctl_softc->pool_lock); 3870 return (io); 3871 } else 3872 ctl_pool_release(npool); 3873 } 3874 3875 /* Drop the spinlock before we malloc */ 3876 mtx_unlock(&ctl_softc->pool_lock); 3877 3878 /* 3879 * The emergency pool (if it exists) didn't have one, so try an 3880 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3881 */ 3882 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3883 if (io != NULL) { 3884 /* 3885 * If the emergency pool exists but is empty, add this 3886 * ctl_io to its list when it gets freed. 3887 */ 3888 if (emergency_pool != NULL) { 3889 mtx_lock(&ctl_softc->pool_lock); 3890 if (ctl_pool_acquire(emergency_pool) == 0) { 3891 io->io_hdr.pool = emergency_pool; 3892 emergency_pool->total_ctl_io++; 3893 /* 3894 * Need to bump this, otherwise 3895 * total_allocated and total_freed won't 3896 * match when we no longer have anything 3897 * outstanding. 3898 */ 3899 emergency_pool->total_allocated++; 3900 } 3901 mtx_unlock(&ctl_softc->pool_lock); 3902 } else 3903 io->io_hdr.pool = NULL; 3904 } 3905 3906 return (io); 3907 } 3908 3909 void 3910 ctl_free_io(union ctl_io *io) 3911 { 3912 if (io == NULL) 3913 return; 3914 3915 /* 3916 * If this ctl_io has a pool, return it to that pool. 3917 */ 3918 if (io->io_hdr.pool != NULL) { 3919 struct ctl_io_pool *pool; 3920 3921 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3922 mtx_lock(&pool->ctl_softc->pool_lock); 3923 io->io_hdr.io_type = 0xff; 3924 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3925 pool->total_freed++; 3926 pool->free_ctl_io++; 3927 ctl_pool_release(pool); 3928 mtx_unlock(&pool->ctl_softc->pool_lock); 3929 } else { 3930 /* 3931 * Otherwise, just free it. We probably malloced it and 3932 * the emergency pool wasn't available. 3933 */ 3934 free(io, M_CTLIO); 3935 } 3936 3937 } 3938 3939 void 3940 ctl_zero_io(union ctl_io *io) 3941 { 3942 void *pool_ref; 3943 3944 if (io == NULL) 3945 return; 3946 3947 /* 3948 * May need to preserve linked list pointers at some point too. 3949 */ 3950 pool_ref = io->io_hdr.pool; 3951 3952 memset(io, 0, sizeof(*io)); 3953 3954 io->io_hdr.pool = pool_ref; 3955 } 3956 3957 /* 3958 * This routine is currently used for internal copies of ctl_ios that need 3959 * to persist for some reason after we've already returned status to the 3960 * FETD. (Thus the flag set.) 3961 * 3962 * XXX XXX 3963 * Note that this makes a blind copy of all fields in the ctl_io, except 3964 * for the pool reference. This includes any memory that has been 3965 * allocated! That memory will no longer be valid after done has been 3966 * called, so this would be VERY DANGEROUS for command that actually does 3967 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3968 * start and stop commands, which don't transfer any data, so this is not a 3969 * problem. If it is used for anything else, the caller would also need to 3970 * allocate data buffer space and this routine would need to be modified to 3971 * copy the data buffer(s) as well. 3972 */ 3973 void 3974 ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3975 { 3976 void *pool_ref; 3977 3978 if ((src == NULL) 3979 || (dest == NULL)) 3980 return; 3981 3982 /* 3983 * May need to preserve linked list pointers at some point too. 3984 */ 3985 pool_ref = dest->io_hdr.pool; 3986 3987 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3988 3989 dest->io_hdr.pool = pool_ref; 3990 /* 3991 * We need to know that this is an internal copy, and doesn't need 3992 * to get passed back to the FETD that allocated it. 3993 */ 3994 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3995 } 3996 3997 #ifdef NEEDTOPORT 3998 static void 3999 ctl_update_power_subpage(struct copan_power_subpage *page) 4000 { 4001 int num_luns, num_partitions, config_type; 4002 struct ctl_softc *softc; 4003 cs_BOOL_t aor_present, shelf_50pct_power; 4004 cs_raidset_personality_t rs_type; 4005 int max_active_luns; 4006 4007 softc = control_softc; 4008 4009 /* subtract out the processor LUN */ 4010 num_luns = softc->num_luns - 1; 4011 /* 4012 * Default to 7 LUNs active, which was the only number we allowed 4013 * in the past. 4014 */ 4015 max_active_luns = 7; 4016 4017 num_partitions = config_GetRsPartitionInfo(); 4018 config_type = config_GetConfigType(); 4019 shelf_50pct_power = config_GetShelfPowerMode(); 4020 aor_present = config_IsAorRsPresent(); 4021 4022 rs_type = ddb_GetRsRaidType(1); 4023 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 4024 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 4025 EPRINT(0, "Unsupported RS type %d!", rs_type); 4026 } 4027 4028 4029 page->total_luns = num_luns; 4030 4031 switch (config_type) { 4032 case 40: 4033 /* 4034 * In a 40 drive configuration, it doesn't matter what DC 4035 * cards we have, whether we have AOR enabled or not, 4036 * partitioning or not, or what type of RAIDset we have. 4037 * In that scenario, we can power up every LUN we present 4038 * to the user. 4039 */ 4040 max_active_luns = num_luns; 4041 4042 break; 4043 case 64: 4044 if (shelf_50pct_power == CS_FALSE) { 4045 /* 25% power */ 4046 if (aor_present == CS_TRUE) { 4047 if (rs_type == 4048 CS_RAIDSET_PERSONALITY_RAID5) { 4049 max_active_luns = 7; 4050 } else if (rs_type == 4051 CS_RAIDSET_PERSONALITY_RAID1){ 4052 max_active_luns = 14; 4053 } else { 4054 /* XXX KDM now what?? */ 4055 } 4056 } else { 4057 if (rs_type == 4058 CS_RAIDSET_PERSONALITY_RAID5) { 4059 max_active_luns = 8; 4060 } else if (rs_type == 4061 CS_RAIDSET_PERSONALITY_RAID1){ 4062 max_active_luns = 16; 4063 } else { 4064 /* XXX KDM now what?? */ 4065 } 4066 } 4067 } else { 4068 /* 50% power */ 4069 /* 4070 * With 50% power in a 64 drive configuration, we 4071 * can power all LUNs we present. 4072 */ 4073 max_active_luns = num_luns; 4074 } 4075 break; 4076 case 112: 4077 if (shelf_50pct_power == CS_FALSE) { 4078 /* 25% power */ 4079 if (aor_present == CS_TRUE) { 4080 if (rs_type == 4081 CS_RAIDSET_PERSONALITY_RAID5) { 4082 max_active_luns = 7; 4083 } else if (rs_type == 4084 CS_RAIDSET_PERSONALITY_RAID1){ 4085 max_active_luns = 14; 4086 } else { 4087 /* XXX KDM now what?? */ 4088 } 4089 } else { 4090 if (rs_type == 4091 CS_RAIDSET_PERSONALITY_RAID5) { 4092 max_active_luns = 8; 4093 } else if (rs_type == 4094 CS_RAIDSET_PERSONALITY_RAID1){ 4095 max_active_luns = 16; 4096 } else { 4097 /* XXX KDM now what?? */ 4098 } 4099 } 4100 } else { 4101 /* 50% power */ 4102 if (aor_present == CS_TRUE) { 4103 if (rs_type == 4104 CS_RAIDSET_PERSONALITY_RAID5) { 4105 max_active_luns = 14; 4106 } else if (rs_type == 4107 CS_RAIDSET_PERSONALITY_RAID1){ 4108 /* 4109 * We're assuming here that disk 4110 * caching is enabled, and so we're 4111 * able to power up half of each 4112 * LUN, and cache all writes. 4113 */ 4114 max_active_luns = num_luns; 4115 } else { 4116 /* XXX KDM now what?? */ 4117 } 4118 } else { 4119 if (rs_type == 4120 CS_RAIDSET_PERSONALITY_RAID5) { 4121 max_active_luns = 15; 4122 } else if (rs_type == 4123 CS_RAIDSET_PERSONALITY_RAID1){ 4124 max_active_luns = 30; 4125 } else { 4126 /* XXX KDM now what?? */ 4127 } 4128 } 4129 } 4130 break; 4131 default: 4132 /* 4133 * In this case, we have an unknown configuration, so we 4134 * just use the default from above. 4135 */ 4136 break; 4137 } 4138 4139 page->max_active_luns = max_active_luns; 4140 #if 0 4141 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4142 page->total_luns, page->max_active_luns); 4143 #endif 4144 } 4145 #endif /* NEEDTOPORT */ 4146 4147 /* 4148 * This routine could be used in the future to load default and/or saved 4149 * mode page parameters for a particuar lun. 4150 */ 4151 static int 4152 ctl_init_page_index(struct ctl_lun *lun) 4153 { 4154 int i; 4155 struct ctl_page_index *page_index; 4156 struct ctl_softc *softc; 4157 const char *value; 4158 4159 memcpy(&lun->mode_pages.index, page_index_template, 4160 sizeof(page_index_template)); 4161 4162 softc = lun->ctl_softc; 4163 4164 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4165 4166 page_index = &lun->mode_pages.index[i]; 4167 /* 4168 * If this is a disk-only mode page, there's no point in 4169 * setting it up. For some pages, we have to have some 4170 * basic information about the disk in order to calculate the 4171 * mode page data. 4172 */ 4173 if ((lun->be_lun->lun_type != T_DIRECT) 4174 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4175 continue; 4176 4177 switch (page_index->page_code & SMPH_PC_MASK) { 4178 case SMS_FORMAT_DEVICE_PAGE: { 4179 struct scsi_format_page *format_page; 4180 4181 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4182 panic("subpage is incorrect!"); 4183 4184 /* 4185 * Sectors per track are set above. Bytes per 4186 * sector need to be set here on a per-LUN basis. 4187 */ 4188 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4189 &format_page_default, 4190 sizeof(format_page_default)); 4191 memcpy(&lun->mode_pages.format_page[ 4192 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4193 sizeof(format_page_changeable)); 4194 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4195 &format_page_default, 4196 sizeof(format_page_default)); 4197 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4198 &format_page_default, 4199 sizeof(format_page_default)); 4200 4201 format_page = &lun->mode_pages.format_page[ 4202 CTL_PAGE_CURRENT]; 4203 scsi_ulto2b(lun->be_lun->blocksize, 4204 format_page->bytes_per_sector); 4205 4206 format_page = &lun->mode_pages.format_page[ 4207 CTL_PAGE_DEFAULT]; 4208 scsi_ulto2b(lun->be_lun->blocksize, 4209 format_page->bytes_per_sector); 4210 4211 format_page = &lun->mode_pages.format_page[ 4212 CTL_PAGE_SAVED]; 4213 scsi_ulto2b(lun->be_lun->blocksize, 4214 format_page->bytes_per_sector); 4215 4216 page_index->page_data = 4217 (uint8_t *)lun->mode_pages.format_page; 4218 break; 4219 } 4220 case SMS_RIGID_DISK_PAGE: { 4221 struct scsi_rigid_disk_page *rigid_disk_page; 4222 uint32_t sectors_per_cylinder; 4223 uint64_t cylinders; 4224 #ifndef __XSCALE__ 4225 int shift; 4226 #endif /* !__XSCALE__ */ 4227 4228 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4229 panic("invalid subpage value %d", 4230 page_index->subpage); 4231 4232 /* 4233 * Rotation rate and sectors per track are set 4234 * above. We calculate the cylinders here based on 4235 * capacity. Due to the number of heads and 4236 * sectors per track we're using, smaller arrays 4237 * may turn out to have 0 cylinders. Linux and 4238 * FreeBSD don't pay attention to these mode pages 4239 * to figure out capacity, but Solaris does. It 4240 * seems to deal with 0 cylinders just fine, and 4241 * works out a fake geometry based on the capacity. 4242 */ 4243 memcpy(&lun->mode_pages.rigid_disk_page[ 4244 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4245 sizeof(rigid_disk_page_default)); 4246 memcpy(&lun->mode_pages.rigid_disk_page[ 4247 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4248 sizeof(rigid_disk_page_changeable)); 4249 memcpy(&lun->mode_pages.rigid_disk_page[ 4250 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4251 sizeof(rigid_disk_page_default)); 4252 memcpy(&lun->mode_pages.rigid_disk_page[ 4253 CTL_PAGE_SAVED], &rigid_disk_page_default, 4254 sizeof(rigid_disk_page_default)); 4255 4256 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4257 CTL_DEFAULT_HEADS; 4258 4259 /* 4260 * The divide method here will be more accurate, 4261 * probably, but results in floating point being 4262 * used in the kernel on i386 (__udivdi3()). On the 4263 * XScale, though, __udivdi3() is implemented in 4264 * software. 4265 * 4266 * The shift method for cylinder calculation is 4267 * accurate if sectors_per_cylinder is a power of 4268 * 2. Otherwise it might be slightly off -- you 4269 * might have a bit of a truncation problem. 4270 */ 4271 #ifdef __XSCALE__ 4272 cylinders = (lun->be_lun->maxlba + 1) / 4273 sectors_per_cylinder; 4274 #else 4275 for (shift = 31; shift > 0; shift--) { 4276 if (sectors_per_cylinder & (1 << shift)) 4277 break; 4278 } 4279 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4280 #endif 4281 4282 /* 4283 * We've basically got 3 bytes, or 24 bits for the 4284 * cylinder size in the mode page. If we're over, 4285 * just round down to 2^24. 4286 */ 4287 if (cylinders > 0xffffff) 4288 cylinders = 0xffffff; 4289 4290 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4291 CTL_PAGE_CURRENT]; 4292 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4293 4294 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4295 CTL_PAGE_DEFAULT]; 4296 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4297 4298 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4299 CTL_PAGE_SAVED]; 4300 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4301 4302 page_index->page_data = 4303 (uint8_t *)lun->mode_pages.rigid_disk_page; 4304 break; 4305 } 4306 case SMS_CACHING_PAGE: { 4307 struct scsi_caching_page *caching_page; 4308 4309 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4310 panic("invalid subpage value %d", 4311 page_index->subpage); 4312 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4313 &caching_page_default, 4314 sizeof(caching_page_default)); 4315 memcpy(&lun->mode_pages.caching_page[ 4316 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4317 sizeof(caching_page_changeable)); 4318 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4319 &caching_page_default, 4320 sizeof(caching_page_default)); 4321 caching_page = &lun->mode_pages.caching_page[ 4322 CTL_PAGE_SAVED]; 4323 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4324 if (value != NULL && strcmp(value, "off") == 0) 4325 caching_page->flags1 &= ~SCP_WCE; 4326 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4327 if (value != NULL && strcmp(value, "off") == 0) 4328 caching_page->flags1 |= SCP_RCD; 4329 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4330 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4331 sizeof(caching_page_default)); 4332 page_index->page_data = 4333 (uint8_t *)lun->mode_pages.caching_page; 4334 break; 4335 } 4336 case SMS_CONTROL_MODE_PAGE: { 4337 struct scsi_control_page *control_page; 4338 4339 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4340 panic("invalid subpage value %d", 4341 page_index->subpage); 4342 4343 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4344 &control_page_default, 4345 sizeof(control_page_default)); 4346 memcpy(&lun->mode_pages.control_page[ 4347 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4348 sizeof(control_page_changeable)); 4349 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4350 &control_page_default, 4351 sizeof(control_page_default)); 4352 control_page = &lun->mode_pages.control_page[ 4353 CTL_PAGE_SAVED]; 4354 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4355 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4356 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4357 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4358 } 4359 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4360 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4361 sizeof(control_page_default)); 4362 page_index->page_data = 4363 (uint8_t *)lun->mode_pages.control_page; 4364 break; 4365 4366 } 4367 case SMS_VENDOR_SPECIFIC_PAGE:{ 4368 switch (page_index->subpage) { 4369 case PWR_SUBPAGE_CODE: { 4370 struct copan_power_subpage *current_page, 4371 *saved_page; 4372 4373 memcpy(&lun->mode_pages.power_subpage[ 4374 CTL_PAGE_CURRENT], 4375 &power_page_default, 4376 sizeof(power_page_default)); 4377 memcpy(&lun->mode_pages.power_subpage[ 4378 CTL_PAGE_CHANGEABLE], 4379 &power_page_changeable, 4380 sizeof(power_page_changeable)); 4381 memcpy(&lun->mode_pages.power_subpage[ 4382 CTL_PAGE_DEFAULT], 4383 &power_page_default, 4384 sizeof(power_page_default)); 4385 memcpy(&lun->mode_pages.power_subpage[ 4386 CTL_PAGE_SAVED], 4387 &power_page_default, 4388 sizeof(power_page_default)); 4389 page_index->page_data = 4390 (uint8_t *)lun->mode_pages.power_subpage; 4391 4392 current_page = (struct copan_power_subpage *) 4393 (page_index->page_data + 4394 (page_index->page_len * 4395 CTL_PAGE_CURRENT)); 4396 saved_page = (struct copan_power_subpage *) 4397 (page_index->page_data + 4398 (page_index->page_len * 4399 CTL_PAGE_SAVED)); 4400 break; 4401 } 4402 case APS_SUBPAGE_CODE: { 4403 struct copan_aps_subpage *current_page, 4404 *saved_page; 4405 4406 // This gets set multiple times but 4407 // it should always be the same. It's 4408 // only done during init so who cares. 4409 index_to_aps_page = i; 4410 4411 memcpy(&lun->mode_pages.aps_subpage[ 4412 CTL_PAGE_CURRENT], 4413 &aps_page_default, 4414 sizeof(aps_page_default)); 4415 memcpy(&lun->mode_pages.aps_subpage[ 4416 CTL_PAGE_CHANGEABLE], 4417 &aps_page_changeable, 4418 sizeof(aps_page_changeable)); 4419 memcpy(&lun->mode_pages.aps_subpage[ 4420 CTL_PAGE_DEFAULT], 4421 &aps_page_default, 4422 sizeof(aps_page_default)); 4423 memcpy(&lun->mode_pages.aps_subpage[ 4424 CTL_PAGE_SAVED], 4425 &aps_page_default, 4426 sizeof(aps_page_default)); 4427 page_index->page_data = 4428 (uint8_t *)lun->mode_pages.aps_subpage; 4429 4430 current_page = (struct copan_aps_subpage *) 4431 (page_index->page_data + 4432 (page_index->page_len * 4433 CTL_PAGE_CURRENT)); 4434 saved_page = (struct copan_aps_subpage *) 4435 (page_index->page_data + 4436 (page_index->page_len * 4437 CTL_PAGE_SAVED)); 4438 break; 4439 } 4440 case DBGCNF_SUBPAGE_CODE: { 4441 struct copan_debugconf_subpage *current_page, 4442 *saved_page; 4443 4444 memcpy(&lun->mode_pages.debugconf_subpage[ 4445 CTL_PAGE_CURRENT], 4446 &debugconf_page_default, 4447 sizeof(debugconf_page_default)); 4448 memcpy(&lun->mode_pages.debugconf_subpage[ 4449 CTL_PAGE_CHANGEABLE], 4450 &debugconf_page_changeable, 4451 sizeof(debugconf_page_changeable)); 4452 memcpy(&lun->mode_pages.debugconf_subpage[ 4453 CTL_PAGE_DEFAULT], 4454 &debugconf_page_default, 4455 sizeof(debugconf_page_default)); 4456 memcpy(&lun->mode_pages.debugconf_subpage[ 4457 CTL_PAGE_SAVED], 4458 &debugconf_page_default, 4459 sizeof(debugconf_page_default)); 4460 page_index->page_data = 4461 (uint8_t *)lun->mode_pages.debugconf_subpage; 4462 4463 current_page = (struct copan_debugconf_subpage *) 4464 (page_index->page_data + 4465 (page_index->page_len * 4466 CTL_PAGE_CURRENT)); 4467 saved_page = (struct copan_debugconf_subpage *) 4468 (page_index->page_data + 4469 (page_index->page_len * 4470 CTL_PAGE_SAVED)); 4471 break; 4472 } 4473 default: 4474 panic("invalid subpage value %d", 4475 page_index->subpage); 4476 break; 4477 } 4478 break; 4479 } 4480 default: 4481 panic("invalid page value %d", 4482 page_index->page_code & SMPH_PC_MASK); 4483 break; 4484 } 4485 } 4486 4487 return (CTL_RETVAL_COMPLETE); 4488 } 4489 4490 /* 4491 * LUN allocation. 4492 * 4493 * Requirements: 4494 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4495 * wants us to allocate the LUN and he can block. 4496 * - ctl_softc is always set 4497 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4498 * 4499 * Returns 0 for success, non-zero (errno) for failure. 4500 */ 4501 static int 4502 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4503 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4504 { 4505 struct ctl_lun *nlun, *lun; 4506 struct ctl_port *port; 4507 struct scsi_vpd_id_descriptor *desc; 4508 struct scsi_vpd_id_t10 *t10id; 4509 const char *eui, *naa, *scsiname, *vendor, *value; 4510 int lun_number, i, lun_malloced; 4511 int devidlen, idlen1, idlen2 = 0, len; 4512 4513 if (be_lun == NULL) 4514 return (EINVAL); 4515 4516 /* 4517 * We currently only support Direct Access or Processor LUN types. 4518 */ 4519 switch (be_lun->lun_type) { 4520 case T_DIRECT: 4521 break; 4522 case T_PROCESSOR: 4523 break; 4524 case T_SEQUENTIAL: 4525 case T_CHANGER: 4526 default: 4527 be_lun->lun_config_status(be_lun->be_lun, 4528 CTL_LUN_CONFIG_FAILURE); 4529 break; 4530 } 4531 if (ctl_lun == NULL) { 4532 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4533 lun_malloced = 1; 4534 } else { 4535 lun_malloced = 0; 4536 lun = ctl_lun; 4537 } 4538 4539 memset(lun, 0, sizeof(*lun)); 4540 if (lun_malloced) 4541 lun->flags = CTL_LUN_MALLOCED; 4542 4543 /* Generate LUN ID. */ 4544 devidlen = max(CTL_DEVID_MIN_LEN, 4545 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4546 idlen1 = sizeof(*t10id) + devidlen; 4547 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4548 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4549 if (scsiname != NULL) { 4550 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4551 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4552 } 4553 eui = ctl_get_opt(&be_lun->options, "eui"); 4554 if (eui != NULL) { 4555 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4556 } 4557 naa = ctl_get_opt(&be_lun->options, "naa"); 4558 if (naa != NULL) { 4559 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4560 } 4561 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4562 M_CTL, M_WAITOK | M_ZERO); 4563 lun->lun_devid->len = len; 4564 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4565 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4566 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4567 desc->length = idlen1; 4568 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4569 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4570 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4571 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4572 } else { 4573 strncpy(t10id->vendor, vendor, 4574 min(sizeof(t10id->vendor), strlen(vendor))); 4575 } 4576 strncpy((char *)t10id->vendor_spec_id, 4577 (char *)be_lun->device_id, devidlen); 4578 if (scsiname != NULL) { 4579 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4580 desc->length); 4581 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4582 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4583 SVPD_ID_TYPE_SCSI_NAME; 4584 desc->length = idlen2; 4585 strlcpy(desc->identifier, scsiname, idlen2); 4586 } 4587 if (eui != NULL) { 4588 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4589 desc->length); 4590 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4591 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4592 SVPD_ID_TYPE_EUI64; 4593 desc->length = 8; 4594 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4595 } 4596 if (naa != NULL) { 4597 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4598 desc->length); 4599 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4600 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4601 SVPD_ID_TYPE_NAA; 4602 desc->length = 8; 4603 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4604 } 4605 4606 mtx_lock(&ctl_softc->ctl_lock); 4607 /* 4608 * See if the caller requested a particular LUN number. If so, see 4609 * if it is available. Otherwise, allocate the first available LUN. 4610 */ 4611 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4612 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4613 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4614 mtx_unlock(&ctl_softc->ctl_lock); 4615 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4616 printf("ctl: requested LUN ID %d is higher " 4617 "than CTL_MAX_LUNS - 1 (%d)\n", 4618 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4619 } else { 4620 /* 4621 * XXX KDM return an error, or just assign 4622 * another LUN ID in this case?? 4623 */ 4624 printf("ctl: requested LUN ID %d is already " 4625 "in use\n", be_lun->req_lun_id); 4626 } 4627 if (lun->flags & CTL_LUN_MALLOCED) 4628 free(lun, M_CTL); 4629 be_lun->lun_config_status(be_lun->be_lun, 4630 CTL_LUN_CONFIG_FAILURE); 4631 return (ENOSPC); 4632 } 4633 lun_number = be_lun->req_lun_id; 4634 } else { 4635 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4636 if (lun_number == -1) { 4637 mtx_unlock(&ctl_softc->ctl_lock); 4638 printf("ctl: can't allocate LUN on target %ju, out of " 4639 "LUNs\n", (uintmax_t)target_id.id); 4640 if (lun->flags & CTL_LUN_MALLOCED) 4641 free(lun, M_CTL); 4642 be_lun->lun_config_status(be_lun->be_lun, 4643 CTL_LUN_CONFIG_FAILURE); 4644 return (ENOSPC); 4645 } 4646 } 4647 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4648 4649 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4650 lun->target = target_id; 4651 lun->lun = lun_number; 4652 lun->be_lun = be_lun; 4653 /* 4654 * The processor LUN is always enabled. Disk LUNs come on line 4655 * disabled, and must be enabled by the backend. 4656 */ 4657 lun->flags |= CTL_LUN_DISABLED; 4658 lun->backend = be_lun->be; 4659 be_lun->ctl_lun = lun; 4660 be_lun->lun_id = lun_number; 4661 atomic_add_int(&be_lun->be->num_luns, 1); 4662 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4663 lun->flags |= CTL_LUN_OFFLINE; 4664 4665 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4666 lun->flags |= CTL_LUN_STOPPED; 4667 4668 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4669 lun->flags |= CTL_LUN_INOPERABLE; 4670 4671 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4672 lun->flags |= CTL_LUN_PRIMARY_SC; 4673 4674 value = ctl_get_opt(&be_lun->options, "readonly"); 4675 if (value != NULL && strcmp(value, "on") == 0) 4676 lun->flags |= CTL_LUN_READONLY; 4677 4678 lun->ctl_softc = ctl_softc; 4679 TAILQ_INIT(&lun->ooa_queue); 4680 TAILQ_INIT(&lun->blocked_queue); 4681 STAILQ_INIT(&lun->error_list); 4682 ctl_tpc_lun_init(lun); 4683 4684 /* 4685 * Initialize the mode page index. 4686 */ 4687 ctl_init_page_index(lun); 4688 4689 /* 4690 * Set the poweron UA for all initiators on this LUN only. 4691 */ 4692 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4693 lun->pending_ua[i] = CTL_UA_POWERON; 4694 4695 /* 4696 * Now, before we insert this lun on the lun list, set the lun 4697 * inventory changed UA for all other luns. 4698 */ 4699 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4700 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4701 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4702 } 4703 } 4704 4705 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4706 4707 ctl_softc->ctl_luns[lun_number] = lun; 4708 4709 ctl_softc->num_luns++; 4710 4711 /* Setup statistics gathering */ 4712 lun->stats.device_type = be_lun->lun_type; 4713 lun->stats.lun_number = lun_number; 4714 if (lun->stats.device_type == T_DIRECT) 4715 lun->stats.blocksize = be_lun->blocksize; 4716 else 4717 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4718 for (i = 0;i < CTL_MAX_PORTS;i++) 4719 lun->stats.ports[i].targ_port = i; 4720 4721 mtx_unlock(&ctl_softc->ctl_lock); 4722 4723 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4724 4725 /* 4726 * Run through each registered FETD and bring it online if it isn't 4727 * already. Enable the target ID if it hasn't been enabled, and 4728 * enable this particular LUN. 4729 */ 4730 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4731 int retval; 4732 4733 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4734 if (retval != 0) { 4735 printf("ctl_alloc_lun: FETD %s port %d returned error " 4736 "%d for lun_enable on target %ju lun %d\n", 4737 port->port_name, port->targ_port, retval, 4738 (uintmax_t)target_id.id, lun_number); 4739 } else 4740 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4741 } 4742 return (0); 4743 } 4744 4745 /* 4746 * Delete a LUN. 4747 * Assumptions: 4748 * - LUN has already been marked invalid and any pending I/O has been taken 4749 * care of. 4750 */ 4751 static int 4752 ctl_free_lun(struct ctl_lun *lun) 4753 { 4754 struct ctl_softc *softc; 4755 #if 0 4756 struct ctl_port *port; 4757 #endif 4758 struct ctl_lun *nlun; 4759 int i; 4760 4761 softc = lun->ctl_softc; 4762 4763 mtx_assert(&softc->ctl_lock, MA_OWNED); 4764 4765 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4766 4767 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4768 4769 softc->ctl_luns[lun->lun] = NULL; 4770 4771 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4772 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4773 4774 softc->num_luns--; 4775 4776 /* 4777 * XXX KDM this scheme only works for a single target/multiple LUN 4778 * setup. It needs to be revamped for a multiple target scheme. 4779 * 4780 * XXX KDM this results in port->lun_disable() getting called twice, 4781 * once when ctl_disable_lun() is called, and a second time here. 4782 * We really need to re-think the LUN disable semantics. There 4783 * should probably be several steps/levels to LUN removal: 4784 * - disable 4785 * - invalidate 4786 * - free 4787 * 4788 * Right now we only have a disable method when communicating to 4789 * the front end ports, at least for individual LUNs. 4790 */ 4791 #if 0 4792 STAILQ_FOREACH(port, &softc->port_list, links) { 4793 int retval; 4794 4795 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4796 lun->lun); 4797 if (retval != 0) { 4798 printf("ctl_free_lun: FETD %s port %d returned error " 4799 "%d for lun_disable on target %ju lun %jd\n", 4800 port->port_name, port->targ_port, retval, 4801 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4802 } 4803 4804 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4805 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4806 4807 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4808 if (retval != 0) { 4809 printf("ctl_free_lun: FETD %s port %d " 4810 "returned error %d for targ_disable on " 4811 "target %ju\n", port->port_name, 4812 port->targ_port, retval, 4813 (uintmax_t)lun->target.id); 4814 } else 4815 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4816 4817 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4818 continue; 4819 4820 #if 0 4821 port->port_offline(port->onoff_arg); 4822 port->status &= ~CTL_PORT_STATUS_ONLINE; 4823 #endif 4824 } 4825 } 4826 #endif 4827 4828 /* 4829 * Tell the backend to free resources, if this LUN has a backend. 4830 */ 4831 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4832 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4833 4834 ctl_tpc_lun_shutdown(lun); 4835 mtx_destroy(&lun->lun_lock); 4836 free(lun->lun_devid, M_CTL); 4837 if (lun->flags & CTL_LUN_MALLOCED) 4838 free(lun, M_CTL); 4839 4840 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4841 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4842 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4843 } 4844 } 4845 4846 return (0); 4847 } 4848 4849 static void 4850 ctl_create_lun(struct ctl_be_lun *be_lun) 4851 { 4852 struct ctl_softc *ctl_softc; 4853 4854 ctl_softc = control_softc; 4855 4856 /* 4857 * ctl_alloc_lun() should handle all potential failure cases. 4858 */ 4859 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4860 } 4861 4862 int 4863 ctl_add_lun(struct ctl_be_lun *be_lun) 4864 { 4865 struct ctl_softc *ctl_softc = control_softc; 4866 4867 mtx_lock(&ctl_softc->ctl_lock); 4868 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4869 mtx_unlock(&ctl_softc->ctl_lock); 4870 wakeup(&ctl_softc->pending_lun_queue); 4871 4872 return (0); 4873 } 4874 4875 int 4876 ctl_enable_lun(struct ctl_be_lun *be_lun) 4877 { 4878 struct ctl_softc *ctl_softc; 4879 struct ctl_port *port, *nport; 4880 struct ctl_lun *lun; 4881 int retval; 4882 4883 ctl_softc = control_softc; 4884 4885 lun = (struct ctl_lun *)be_lun->ctl_lun; 4886 4887 mtx_lock(&ctl_softc->ctl_lock); 4888 mtx_lock(&lun->lun_lock); 4889 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4890 /* 4891 * eh? Why did we get called if the LUN is already 4892 * enabled? 4893 */ 4894 mtx_unlock(&lun->lun_lock); 4895 mtx_unlock(&ctl_softc->ctl_lock); 4896 return (0); 4897 } 4898 lun->flags &= ~CTL_LUN_DISABLED; 4899 mtx_unlock(&lun->lun_lock); 4900 4901 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4902 nport = STAILQ_NEXT(port, links); 4903 4904 /* 4905 * Drop the lock while we call the FETD's enable routine. 4906 * This can lead to a callback into CTL (at least in the 4907 * case of the internal initiator frontend. 4908 */ 4909 mtx_unlock(&ctl_softc->ctl_lock); 4910 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4911 mtx_lock(&ctl_softc->ctl_lock); 4912 if (retval != 0) { 4913 printf("%s: FETD %s port %d returned error " 4914 "%d for lun_enable on target %ju lun %jd\n", 4915 __func__, port->port_name, port->targ_port, retval, 4916 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4917 } 4918 #if 0 4919 else { 4920 /* NOTE: TODO: why does lun enable affect port status? */ 4921 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4922 } 4923 #endif 4924 } 4925 4926 mtx_unlock(&ctl_softc->ctl_lock); 4927 4928 return (0); 4929 } 4930 4931 int 4932 ctl_disable_lun(struct ctl_be_lun *be_lun) 4933 { 4934 struct ctl_softc *ctl_softc; 4935 struct ctl_port *port; 4936 struct ctl_lun *lun; 4937 int retval; 4938 4939 ctl_softc = control_softc; 4940 4941 lun = (struct ctl_lun *)be_lun->ctl_lun; 4942 4943 mtx_lock(&ctl_softc->ctl_lock); 4944 mtx_lock(&lun->lun_lock); 4945 if (lun->flags & CTL_LUN_DISABLED) { 4946 mtx_unlock(&lun->lun_lock); 4947 mtx_unlock(&ctl_softc->ctl_lock); 4948 return (0); 4949 } 4950 lun->flags |= CTL_LUN_DISABLED; 4951 mtx_unlock(&lun->lun_lock); 4952 4953 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4954 mtx_unlock(&ctl_softc->ctl_lock); 4955 /* 4956 * Drop the lock before we call the frontend's disable 4957 * routine, to avoid lock order reversals. 4958 * 4959 * XXX KDM what happens if the frontend list changes while 4960 * we're traversing it? It's unlikely, but should be handled. 4961 */ 4962 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4963 lun->lun); 4964 mtx_lock(&ctl_softc->ctl_lock); 4965 if (retval != 0) { 4966 printf("ctl_alloc_lun: FETD %s port %d returned error " 4967 "%d for lun_disable on target %ju lun %jd\n", 4968 port->port_name, port->targ_port, retval, 4969 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4970 } 4971 } 4972 4973 mtx_unlock(&ctl_softc->ctl_lock); 4974 4975 return (0); 4976 } 4977 4978 int 4979 ctl_start_lun(struct ctl_be_lun *be_lun) 4980 { 4981 struct ctl_softc *ctl_softc; 4982 struct ctl_lun *lun; 4983 4984 ctl_softc = control_softc; 4985 4986 lun = (struct ctl_lun *)be_lun->ctl_lun; 4987 4988 mtx_lock(&lun->lun_lock); 4989 lun->flags &= ~CTL_LUN_STOPPED; 4990 mtx_unlock(&lun->lun_lock); 4991 4992 return (0); 4993 } 4994 4995 int 4996 ctl_stop_lun(struct ctl_be_lun *be_lun) 4997 { 4998 struct ctl_softc *ctl_softc; 4999 struct ctl_lun *lun; 5000 5001 ctl_softc = control_softc; 5002 5003 lun = (struct ctl_lun *)be_lun->ctl_lun; 5004 5005 mtx_lock(&lun->lun_lock); 5006 lun->flags |= CTL_LUN_STOPPED; 5007 mtx_unlock(&lun->lun_lock); 5008 5009 return (0); 5010 } 5011 5012 int 5013 ctl_lun_offline(struct ctl_be_lun *be_lun) 5014 { 5015 struct ctl_softc *ctl_softc; 5016 struct ctl_lun *lun; 5017 5018 ctl_softc = control_softc; 5019 5020 lun = (struct ctl_lun *)be_lun->ctl_lun; 5021 5022 mtx_lock(&lun->lun_lock); 5023 lun->flags |= CTL_LUN_OFFLINE; 5024 mtx_unlock(&lun->lun_lock); 5025 5026 return (0); 5027 } 5028 5029 int 5030 ctl_lun_online(struct ctl_be_lun *be_lun) 5031 { 5032 struct ctl_softc *ctl_softc; 5033 struct ctl_lun *lun; 5034 5035 ctl_softc = control_softc; 5036 5037 lun = (struct ctl_lun *)be_lun->ctl_lun; 5038 5039 mtx_lock(&lun->lun_lock); 5040 lun->flags &= ~CTL_LUN_OFFLINE; 5041 mtx_unlock(&lun->lun_lock); 5042 5043 return (0); 5044 } 5045 5046 int 5047 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 5048 { 5049 struct ctl_softc *ctl_softc; 5050 struct ctl_lun *lun; 5051 5052 ctl_softc = control_softc; 5053 5054 lun = (struct ctl_lun *)be_lun->ctl_lun; 5055 5056 mtx_lock(&lun->lun_lock); 5057 5058 /* 5059 * The LUN needs to be disabled before it can be marked invalid. 5060 */ 5061 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 5062 mtx_unlock(&lun->lun_lock); 5063 return (-1); 5064 } 5065 /* 5066 * Mark the LUN invalid. 5067 */ 5068 lun->flags |= CTL_LUN_INVALID; 5069 5070 /* 5071 * If there is nothing in the OOA queue, go ahead and free the LUN. 5072 * If we have something in the OOA queue, we'll free it when the 5073 * last I/O completes. 5074 */ 5075 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5076 mtx_unlock(&lun->lun_lock); 5077 mtx_lock(&ctl_softc->ctl_lock); 5078 ctl_free_lun(lun); 5079 mtx_unlock(&ctl_softc->ctl_lock); 5080 } else 5081 mtx_unlock(&lun->lun_lock); 5082 5083 return (0); 5084 } 5085 5086 int 5087 ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5088 { 5089 struct ctl_softc *ctl_softc; 5090 struct ctl_lun *lun; 5091 5092 ctl_softc = control_softc; 5093 lun = (struct ctl_lun *)be_lun->ctl_lun; 5094 5095 mtx_lock(&lun->lun_lock); 5096 lun->flags |= CTL_LUN_INOPERABLE; 5097 mtx_unlock(&lun->lun_lock); 5098 5099 return (0); 5100 } 5101 5102 int 5103 ctl_lun_operable(struct ctl_be_lun *be_lun) 5104 { 5105 struct ctl_softc *ctl_softc; 5106 struct ctl_lun *lun; 5107 5108 ctl_softc = control_softc; 5109 lun = (struct ctl_lun *)be_lun->ctl_lun; 5110 5111 mtx_lock(&lun->lun_lock); 5112 lun->flags &= ~CTL_LUN_INOPERABLE; 5113 mtx_unlock(&lun->lun_lock); 5114 5115 return (0); 5116 } 5117 5118 int 5119 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5120 int lock) 5121 { 5122 struct ctl_softc *softc; 5123 struct ctl_lun *lun; 5124 struct copan_aps_subpage *current_sp; 5125 struct ctl_page_index *page_index; 5126 int i; 5127 5128 softc = control_softc; 5129 5130 mtx_lock(&softc->ctl_lock); 5131 5132 lun = (struct ctl_lun *)be_lun->ctl_lun; 5133 mtx_lock(&lun->lun_lock); 5134 5135 page_index = NULL; 5136 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5137 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5138 APS_PAGE_CODE) 5139 continue; 5140 5141 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5142 continue; 5143 page_index = &lun->mode_pages.index[i]; 5144 } 5145 5146 if (page_index == NULL) { 5147 mtx_unlock(&lun->lun_lock); 5148 mtx_unlock(&softc->ctl_lock); 5149 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5150 (uintmax_t)lun->lun); 5151 return (1); 5152 } 5153 #if 0 5154 if ((softc->aps_locked_lun != 0) 5155 && (softc->aps_locked_lun != lun->lun)) { 5156 printf("%s: attempt to lock LUN %llu when %llu is already " 5157 "locked\n"); 5158 mtx_unlock(&lun->lun_lock); 5159 mtx_unlock(&softc->ctl_lock); 5160 return (1); 5161 } 5162 #endif 5163 5164 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5165 (page_index->page_len * CTL_PAGE_CURRENT)); 5166 5167 if (lock != 0) { 5168 current_sp->lock_active = APS_LOCK_ACTIVE; 5169 softc->aps_locked_lun = lun->lun; 5170 } else { 5171 current_sp->lock_active = 0; 5172 softc->aps_locked_lun = 0; 5173 } 5174 5175 5176 /* 5177 * If we're in HA mode, try to send the lock message to the other 5178 * side. 5179 */ 5180 if (ctl_is_single == 0) { 5181 int isc_retval; 5182 union ctl_ha_msg lock_msg; 5183 5184 lock_msg.hdr.nexus = *nexus; 5185 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5186 if (lock != 0) 5187 lock_msg.aps.lock_flag = 1; 5188 else 5189 lock_msg.aps.lock_flag = 0; 5190 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5191 sizeof(lock_msg), 0); 5192 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5193 printf("%s: APS (lock=%d) error returned from " 5194 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5195 mtx_unlock(&lun->lun_lock); 5196 mtx_unlock(&softc->ctl_lock); 5197 return (1); 5198 } 5199 } 5200 5201 mtx_unlock(&lun->lun_lock); 5202 mtx_unlock(&softc->ctl_lock); 5203 5204 return (0); 5205 } 5206 5207 void 5208 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5209 { 5210 struct ctl_lun *lun; 5211 struct ctl_softc *softc; 5212 int i; 5213 5214 softc = control_softc; 5215 5216 lun = (struct ctl_lun *)be_lun->ctl_lun; 5217 5218 mtx_lock(&lun->lun_lock); 5219 5220 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5221 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5222 5223 mtx_unlock(&lun->lun_lock); 5224 } 5225 5226 /* 5227 * Backend "memory move is complete" callback for requests that never 5228 * make it down to say RAIDCore's configuration code. 5229 */ 5230 int 5231 ctl_config_move_done(union ctl_io *io) 5232 { 5233 int retval; 5234 5235 retval = CTL_RETVAL_COMPLETE; 5236 5237 5238 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5239 /* 5240 * XXX KDM this shouldn't happen, but what if it does? 5241 */ 5242 if (io->io_hdr.io_type != CTL_IO_SCSI) 5243 panic("I/O type isn't CTL_IO_SCSI!"); 5244 5245 if ((io->io_hdr.port_status == 0) 5246 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5247 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5248 io->io_hdr.status = CTL_SUCCESS; 5249 else if ((io->io_hdr.port_status != 0) 5250 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5251 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5252 /* 5253 * For hardware error sense keys, the sense key 5254 * specific value is defined to be a retry count, 5255 * but we use it to pass back an internal FETD 5256 * error code. XXX KDM Hopefully the FETD is only 5257 * using 16 bits for an error code, since that's 5258 * all the space we have in the sks field. 5259 */ 5260 ctl_set_internal_failure(&io->scsiio, 5261 /*sks_valid*/ 1, 5262 /*retry_count*/ 5263 io->io_hdr.port_status); 5264 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5265 free(io->scsiio.kern_data_ptr, M_CTL); 5266 ctl_done(io); 5267 goto bailout; 5268 } 5269 5270 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5271 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5272 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5273 /* 5274 * XXX KDM just assuming a single pointer here, and not a 5275 * S/G list. If we start using S/G lists for config data, 5276 * we'll need to know how to clean them up here as well. 5277 */ 5278 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5279 free(io->scsiio.kern_data_ptr, M_CTL); 5280 /* Hopefully the user has already set the status... */ 5281 ctl_done(io); 5282 } else { 5283 /* 5284 * XXX KDM now we need to continue data movement. Some 5285 * options: 5286 * - call ctl_scsiio() again? We don't do this for data 5287 * writes, because for those at least we know ahead of 5288 * time where the write will go and how long it is. For 5289 * config writes, though, that information is largely 5290 * contained within the write itself, thus we need to 5291 * parse out the data again. 5292 * 5293 * - Call some other function once the data is in? 5294 */ 5295 5296 /* 5297 * XXX KDM call ctl_scsiio() again for now, and check flag 5298 * bits to see whether we're allocated or not. 5299 */ 5300 retval = ctl_scsiio(&io->scsiio); 5301 } 5302 bailout: 5303 return (retval); 5304 } 5305 5306 /* 5307 * This gets called by a backend driver when it is done with a 5308 * data_submit method. 5309 */ 5310 void 5311 ctl_data_submit_done(union ctl_io *io) 5312 { 5313 /* 5314 * If the IO_CONT flag is set, we need to call the supplied 5315 * function to continue processing the I/O, instead of completing 5316 * the I/O just yet. 5317 * 5318 * If there is an error, though, we don't want to keep processing. 5319 * Instead, just send status back to the initiator. 5320 */ 5321 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5322 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5323 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5324 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5325 io->scsiio.io_cont(io); 5326 return; 5327 } 5328 ctl_done(io); 5329 } 5330 5331 /* 5332 * This gets called by a backend driver when it is done with a 5333 * configuration write. 5334 */ 5335 void 5336 ctl_config_write_done(union ctl_io *io) 5337 { 5338 uint8_t *buf; 5339 5340 /* 5341 * If the IO_CONT flag is set, we need to call the supplied 5342 * function to continue processing the I/O, instead of completing 5343 * the I/O just yet. 5344 * 5345 * If there is an error, though, we don't want to keep processing. 5346 * Instead, just send status back to the initiator. 5347 */ 5348 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5349 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5350 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5351 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5352 io->scsiio.io_cont(io); 5353 return; 5354 } 5355 /* 5356 * Since a configuration write can be done for commands that actually 5357 * have data allocated, like write buffer, and commands that have 5358 * no data, like start/stop unit, we need to check here. 5359 */ 5360 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5361 buf = io->scsiio.kern_data_ptr; 5362 else 5363 buf = NULL; 5364 ctl_done(io); 5365 if (buf) 5366 free(buf, M_CTL); 5367 } 5368 5369 /* 5370 * SCSI release command. 5371 */ 5372 int 5373 ctl_scsi_release(struct ctl_scsiio *ctsio) 5374 { 5375 int length, longid, thirdparty_id, resv_id; 5376 struct ctl_softc *ctl_softc; 5377 struct ctl_lun *lun; 5378 uint32_t residx; 5379 5380 length = 0; 5381 resv_id = 0; 5382 5383 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5384 5385 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5386 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5387 ctl_softc = control_softc; 5388 5389 switch (ctsio->cdb[0]) { 5390 case RELEASE_10: { 5391 struct scsi_release_10 *cdb; 5392 5393 cdb = (struct scsi_release_10 *)ctsio->cdb; 5394 5395 if (cdb->byte2 & SR10_LONGID) 5396 longid = 1; 5397 else 5398 thirdparty_id = cdb->thirdparty_id; 5399 5400 resv_id = cdb->resv_id; 5401 length = scsi_2btoul(cdb->length); 5402 break; 5403 } 5404 } 5405 5406 5407 /* 5408 * XXX KDM right now, we only support LUN reservation. We don't 5409 * support 3rd party reservations, or extent reservations, which 5410 * might actually need the parameter list. If we've gotten this 5411 * far, we've got a LUN reservation. Anything else got kicked out 5412 * above. So, according to SPC, ignore the length. 5413 */ 5414 length = 0; 5415 5416 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5417 && (length > 0)) { 5418 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5419 ctsio->kern_data_len = length; 5420 ctsio->kern_total_len = length; 5421 ctsio->kern_data_resid = 0; 5422 ctsio->kern_rel_offset = 0; 5423 ctsio->kern_sg_entries = 0; 5424 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5425 ctsio->be_move_done = ctl_config_move_done; 5426 ctl_datamove((union ctl_io *)ctsio); 5427 5428 return (CTL_RETVAL_COMPLETE); 5429 } 5430 5431 if (length > 0) 5432 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5433 5434 mtx_lock(&lun->lun_lock); 5435 5436 /* 5437 * According to SPC, it is not an error for an intiator to attempt 5438 * to release a reservation on a LUN that isn't reserved, or that 5439 * is reserved by another initiator. The reservation can only be 5440 * released, though, by the initiator who made it or by one of 5441 * several reset type events. 5442 */ 5443 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5444 lun->flags &= ~CTL_LUN_RESERVED; 5445 5446 mtx_unlock(&lun->lun_lock); 5447 5448 ctsio->scsi_status = SCSI_STATUS_OK; 5449 ctsio->io_hdr.status = CTL_SUCCESS; 5450 5451 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5452 free(ctsio->kern_data_ptr, M_CTL); 5453 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5454 } 5455 5456 ctl_done((union ctl_io *)ctsio); 5457 return (CTL_RETVAL_COMPLETE); 5458 } 5459 5460 int 5461 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5462 { 5463 int extent, thirdparty, longid; 5464 int resv_id, length; 5465 uint64_t thirdparty_id; 5466 struct ctl_softc *ctl_softc; 5467 struct ctl_lun *lun; 5468 uint32_t residx; 5469 5470 extent = 0; 5471 thirdparty = 0; 5472 longid = 0; 5473 resv_id = 0; 5474 length = 0; 5475 thirdparty_id = 0; 5476 5477 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5478 5479 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5480 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5481 ctl_softc = control_softc; 5482 5483 switch (ctsio->cdb[0]) { 5484 case RESERVE_10: { 5485 struct scsi_reserve_10 *cdb; 5486 5487 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5488 5489 if (cdb->byte2 & SR10_LONGID) 5490 longid = 1; 5491 else 5492 thirdparty_id = cdb->thirdparty_id; 5493 5494 resv_id = cdb->resv_id; 5495 length = scsi_2btoul(cdb->length); 5496 break; 5497 } 5498 } 5499 5500 /* 5501 * XXX KDM right now, we only support LUN reservation. We don't 5502 * support 3rd party reservations, or extent reservations, which 5503 * might actually need the parameter list. If we've gotten this 5504 * far, we've got a LUN reservation. Anything else got kicked out 5505 * above. So, according to SPC, ignore the length. 5506 */ 5507 length = 0; 5508 5509 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5510 && (length > 0)) { 5511 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5512 ctsio->kern_data_len = length; 5513 ctsio->kern_total_len = length; 5514 ctsio->kern_data_resid = 0; 5515 ctsio->kern_rel_offset = 0; 5516 ctsio->kern_sg_entries = 0; 5517 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5518 ctsio->be_move_done = ctl_config_move_done; 5519 ctl_datamove((union ctl_io *)ctsio); 5520 5521 return (CTL_RETVAL_COMPLETE); 5522 } 5523 5524 if (length > 0) 5525 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5526 5527 mtx_lock(&lun->lun_lock); 5528 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5529 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5530 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5531 goto bailout; 5532 } 5533 5534 lun->flags |= CTL_LUN_RESERVED; 5535 lun->res_idx = residx; 5536 5537 ctsio->scsi_status = SCSI_STATUS_OK; 5538 ctsio->io_hdr.status = CTL_SUCCESS; 5539 5540 bailout: 5541 mtx_unlock(&lun->lun_lock); 5542 5543 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5544 free(ctsio->kern_data_ptr, M_CTL); 5545 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5546 } 5547 5548 ctl_done((union ctl_io *)ctsio); 5549 return (CTL_RETVAL_COMPLETE); 5550 } 5551 5552 int 5553 ctl_start_stop(struct ctl_scsiio *ctsio) 5554 { 5555 struct scsi_start_stop_unit *cdb; 5556 struct ctl_lun *lun; 5557 struct ctl_softc *ctl_softc; 5558 int retval; 5559 5560 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5561 5562 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5563 ctl_softc = control_softc; 5564 retval = 0; 5565 5566 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5567 5568 /* 5569 * XXX KDM 5570 * We don't support the immediate bit on a stop unit. In order to 5571 * do that, we would need to code up a way to know that a stop is 5572 * pending, and hold off any new commands until it completes, one 5573 * way or another. Then we could accept or reject those commands 5574 * depending on its status. We would almost need to do the reverse 5575 * of what we do below for an immediate start -- return the copy of 5576 * the ctl_io to the FETD with status to send to the host (and to 5577 * free the copy!) and then free the original I/O once the stop 5578 * actually completes. That way, the OOA queue mechanism can work 5579 * to block commands that shouldn't proceed. Another alternative 5580 * would be to put the copy in the queue in place of the original, 5581 * and return the original back to the caller. That could be 5582 * slightly safer.. 5583 */ 5584 if ((cdb->byte2 & SSS_IMMED) 5585 && ((cdb->how & SSS_START) == 0)) { 5586 ctl_set_invalid_field(ctsio, 5587 /*sks_valid*/ 1, 5588 /*command*/ 1, 5589 /*field*/ 1, 5590 /*bit_valid*/ 1, 5591 /*bit*/ 0); 5592 ctl_done((union ctl_io *)ctsio); 5593 return (CTL_RETVAL_COMPLETE); 5594 } 5595 5596 if ((lun->flags & CTL_LUN_PR_RESERVED) 5597 && ((cdb->how & SSS_START)==0)) { 5598 uint32_t residx; 5599 5600 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5601 if (lun->pr_keys[residx] == 0 5602 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5603 5604 ctl_set_reservation_conflict(ctsio); 5605 ctl_done((union ctl_io *)ctsio); 5606 return (CTL_RETVAL_COMPLETE); 5607 } 5608 } 5609 5610 /* 5611 * If there is no backend on this device, we can't start or stop 5612 * it. In theory we shouldn't get any start/stop commands in the 5613 * first place at this level if the LUN doesn't have a backend. 5614 * That should get stopped by the command decode code. 5615 */ 5616 if (lun->backend == NULL) { 5617 ctl_set_invalid_opcode(ctsio); 5618 ctl_done((union ctl_io *)ctsio); 5619 return (CTL_RETVAL_COMPLETE); 5620 } 5621 5622 /* 5623 * XXX KDM Copan-specific offline behavior. 5624 * Figure out a reasonable way to port this? 5625 */ 5626 #ifdef NEEDTOPORT 5627 mtx_lock(&lun->lun_lock); 5628 5629 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5630 && (lun->flags & CTL_LUN_OFFLINE)) { 5631 /* 5632 * If the LUN is offline, and the on/offline bit isn't set, 5633 * reject the start or stop. Otherwise, let it through. 5634 */ 5635 mtx_unlock(&lun->lun_lock); 5636 ctl_set_lun_not_ready(ctsio); 5637 ctl_done((union ctl_io *)ctsio); 5638 } else { 5639 mtx_unlock(&lun->lun_lock); 5640 #endif /* NEEDTOPORT */ 5641 /* 5642 * This could be a start or a stop when we're online, 5643 * or a stop/offline or start/online. A start or stop when 5644 * we're offline is covered in the case above. 5645 */ 5646 /* 5647 * In the non-immediate case, we send the request to 5648 * the backend and return status to the user when 5649 * it is done. 5650 * 5651 * In the immediate case, we allocate a new ctl_io 5652 * to hold a copy of the request, and send that to 5653 * the backend. We then set good status on the 5654 * user's request and return it immediately. 5655 */ 5656 if (cdb->byte2 & SSS_IMMED) { 5657 union ctl_io *new_io; 5658 5659 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5660 if (new_io == NULL) { 5661 ctl_set_busy(ctsio); 5662 ctl_done((union ctl_io *)ctsio); 5663 } else { 5664 ctl_copy_io((union ctl_io *)ctsio, 5665 new_io); 5666 retval = lun->backend->config_write(new_io); 5667 ctl_set_success(ctsio); 5668 ctl_done((union ctl_io *)ctsio); 5669 } 5670 } else { 5671 retval = lun->backend->config_write( 5672 (union ctl_io *)ctsio); 5673 } 5674 #ifdef NEEDTOPORT 5675 } 5676 #endif 5677 return (retval); 5678 } 5679 5680 /* 5681 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5682 * we don't really do anything with the LBA and length fields if the user 5683 * passes them in. Instead we'll just flush out the cache for the entire 5684 * LUN. 5685 */ 5686 int 5687 ctl_sync_cache(struct ctl_scsiio *ctsio) 5688 { 5689 struct ctl_lun *lun; 5690 struct ctl_softc *ctl_softc; 5691 uint64_t starting_lba; 5692 uint32_t block_count; 5693 int retval; 5694 5695 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5696 5697 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5698 ctl_softc = control_softc; 5699 retval = 0; 5700 5701 switch (ctsio->cdb[0]) { 5702 case SYNCHRONIZE_CACHE: { 5703 struct scsi_sync_cache *cdb; 5704 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5705 5706 starting_lba = scsi_4btoul(cdb->begin_lba); 5707 block_count = scsi_2btoul(cdb->lb_count); 5708 break; 5709 } 5710 case SYNCHRONIZE_CACHE_16: { 5711 struct scsi_sync_cache_16 *cdb; 5712 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5713 5714 starting_lba = scsi_8btou64(cdb->begin_lba); 5715 block_count = scsi_4btoul(cdb->lb_count); 5716 break; 5717 } 5718 default: 5719 ctl_set_invalid_opcode(ctsio); 5720 ctl_done((union ctl_io *)ctsio); 5721 goto bailout; 5722 break; /* NOTREACHED */ 5723 } 5724 5725 /* 5726 * We check the LBA and length, but don't do anything with them. 5727 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5728 * get flushed. This check will just help satisfy anyone who wants 5729 * to see an error for an out of range LBA. 5730 */ 5731 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5732 ctl_set_lba_out_of_range(ctsio); 5733 ctl_done((union ctl_io *)ctsio); 5734 goto bailout; 5735 } 5736 5737 /* 5738 * If this LUN has no backend, we can't flush the cache anyway. 5739 */ 5740 if (lun->backend == NULL) { 5741 ctl_set_invalid_opcode(ctsio); 5742 ctl_done((union ctl_io *)ctsio); 5743 goto bailout; 5744 } 5745 5746 /* 5747 * Check to see whether we're configured to send the SYNCHRONIZE 5748 * CACHE command directly to the back end. 5749 */ 5750 mtx_lock(&lun->lun_lock); 5751 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5752 && (++(lun->sync_count) >= lun->sync_interval)) { 5753 lun->sync_count = 0; 5754 mtx_unlock(&lun->lun_lock); 5755 retval = lun->backend->config_write((union ctl_io *)ctsio); 5756 } else { 5757 mtx_unlock(&lun->lun_lock); 5758 ctl_set_success(ctsio); 5759 ctl_done((union ctl_io *)ctsio); 5760 } 5761 5762 bailout: 5763 5764 return (retval); 5765 } 5766 5767 int 5768 ctl_format(struct ctl_scsiio *ctsio) 5769 { 5770 struct scsi_format *cdb; 5771 struct ctl_lun *lun; 5772 struct ctl_softc *ctl_softc; 5773 int length, defect_list_len; 5774 5775 CTL_DEBUG_PRINT(("ctl_format\n")); 5776 5777 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5778 ctl_softc = control_softc; 5779 5780 cdb = (struct scsi_format *)ctsio->cdb; 5781 5782 length = 0; 5783 if (cdb->byte2 & SF_FMTDATA) { 5784 if (cdb->byte2 & SF_LONGLIST) 5785 length = sizeof(struct scsi_format_header_long); 5786 else 5787 length = sizeof(struct scsi_format_header_short); 5788 } 5789 5790 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5791 && (length > 0)) { 5792 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5793 ctsio->kern_data_len = length; 5794 ctsio->kern_total_len = length; 5795 ctsio->kern_data_resid = 0; 5796 ctsio->kern_rel_offset = 0; 5797 ctsio->kern_sg_entries = 0; 5798 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5799 ctsio->be_move_done = ctl_config_move_done; 5800 ctl_datamove((union ctl_io *)ctsio); 5801 5802 return (CTL_RETVAL_COMPLETE); 5803 } 5804 5805 defect_list_len = 0; 5806 5807 if (cdb->byte2 & SF_FMTDATA) { 5808 if (cdb->byte2 & SF_LONGLIST) { 5809 struct scsi_format_header_long *header; 5810 5811 header = (struct scsi_format_header_long *) 5812 ctsio->kern_data_ptr; 5813 5814 defect_list_len = scsi_4btoul(header->defect_list_len); 5815 if (defect_list_len != 0) { 5816 ctl_set_invalid_field(ctsio, 5817 /*sks_valid*/ 1, 5818 /*command*/ 0, 5819 /*field*/ 2, 5820 /*bit_valid*/ 0, 5821 /*bit*/ 0); 5822 goto bailout; 5823 } 5824 } else { 5825 struct scsi_format_header_short *header; 5826 5827 header = (struct scsi_format_header_short *) 5828 ctsio->kern_data_ptr; 5829 5830 defect_list_len = scsi_2btoul(header->defect_list_len); 5831 if (defect_list_len != 0) { 5832 ctl_set_invalid_field(ctsio, 5833 /*sks_valid*/ 1, 5834 /*command*/ 0, 5835 /*field*/ 2, 5836 /*bit_valid*/ 0, 5837 /*bit*/ 0); 5838 goto bailout; 5839 } 5840 } 5841 } 5842 5843 /* 5844 * The format command will clear out the "Medium format corrupted" 5845 * status if set by the configuration code. That status is really 5846 * just a way to notify the host that we have lost the media, and 5847 * get them to issue a command that will basically make them think 5848 * they're blowing away the media. 5849 */ 5850 mtx_lock(&lun->lun_lock); 5851 lun->flags &= ~CTL_LUN_INOPERABLE; 5852 mtx_unlock(&lun->lun_lock); 5853 5854 ctsio->scsi_status = SCSI_STATUS_OK; 5855 ctsio->io_hdr.status = CTL_SUCCESS; 5856 bailout: 5857 5858 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5859 free(ctsio->kern_data_ptr, M_CTL); 5860 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5861 } 5862 5863 ctl_done((union ctl_io *)ctsio); 5864 return (CTL_RETVAL_COMPLETE); 5865 } 5866 5867 int 5868 ctl_read_buffer(struct ctl_scsiio *ctsio) 5869 { 5870 struct scsi_read_buffer *cdb; 5871 struct ctl_lun *lun; 5872 int buffer_offset, len; 5873 static uint8_t descr[4]; 5874 static uint8_t echo_descr[4] = { 0 }; 5875 5876 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5877 5878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5879 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5880 5881 if (lun->flags & CTL_LUN_PR_RESERVED) { 5882 uint32_t residx; 5883 5884 /* 5885 * XXX KDM need a lock here. 5886 */ 5887 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5888 if ((lun->res_type == SPR_TYPE_EX_AC 5889 && residx != lun->pr_res_idx) 5890 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5891 || lun->res_type == SPR_TYPE_EX_AC_AR) 5892 && lun->pr_keys[residx] == 0)) { 5893 ctl_set_reservation_conflict(ctsio); 5894 ctl_done((union ctl_io *)ctsio); 5895 return (CTL_RETVAL_COMPLETE); 5896 } 5897 } 5898 5899 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5900 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5901 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5902 ctl_set_invalid_field(ctsio, 5903 /*sks_valid*/ 1, 5904 /*command*/ 1, 5905 /*field*/ 1, 5906 /*bit_valid*/ 1, 5907 /*bit*/ 4); 5908 ctl_done((union ctl_io *)ctsio); 5909 return (CTL_RETVAL_COMPLETE); 5910 } 5911 5912 len = scsi_3btoul(cdb->length); 5913 buffer_offset = scsi_3btoul(cdb->offset); 5914 5915 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5916 ctl_set_invalid_field(ctsio, 5917 /*sks_valid*/ 1, 5918 /*command*/ 1, 5919 /*field*/ 6, 5920 /*bit_valid*/ 0, 5921 /*bit*/ 0); 5922 ctl_done((union ctl_io *)ctsio); 5923 return (CTL_RETVAL_COMPLETE); 5924 } 5925 5926 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5927 descr[0] = 0; 5928 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5929 ctsio->kern_data_ptr = descr; 5930 len = min(len, sizeof(descr)); 5931 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5932 ctsio->kern_data_ptr = echo_descr; 5933 len = min(len, sizeof(echo_descr)); 5934 } else 5935 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5936 ctsio->kern_data_len = len; 5937 ctsio->kern_total_len = len; 5938 ctsio->kern_data_resid = 0; 5939 ctsio->kern_rel_offset = 0; 5940 ctsio->kern_sg_entries = 0; 5941 ctsio->be_move_done = ctl_config_move_done; 5942 ctl_datamove((union ctl_io *)ctsio); 5943 5944 return (CTL_RETVAL_COMPLETE); 5945 } 5946 5947 int 5948 ctl_write_buffer(struct ctl_scsiio *ctsio) 5949 { 5950 struct scsi_write_buffer *cdb; 5951 struct ctl_lun *lun; 5952 int buffer_offset, len; 5953 5954 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5955 5956 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5957 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5958 5959 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5960 ctl_set_invalid_field(ctsio, 5961 /*sks_valid*/ 1, 5962 /*command*/ 1, 5963 /*field*/ 1, 5964 /*bit_valid*/ 1, 5965 /*bit*/ 4); 5966 ctl_done((union ctl_io *)ctsio); 5967 return (CTL_RETVAL_COMPLETE); 5968 } 5969 5970 len = scsi_3btoul(cdb->length); 5971 buffer_offset = scsi_3btoul(cdb->offset); 5972 5973 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5974 ctl_set_invalid_field(ctsio, 5975 /*sks_valid*/ 1, 5976 /*command*/ 1, 5977 /*field*/ 6, 5978 /*bit_valid*/ 0, 5979 /*bit*/ 0); 5980 ctl_done((union ctl_io *)ctsio); 5981 return (CTL_RETVAL_COMPLETE); 5982 } 5983 5984 /* 5985 * If we've got a kernel request that hasn't been malloced yet, 5986 * malloc it and tell the caller the data buffer is here. 5987 */ 5988 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5989 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5990 ctsio->kern_data_len = len; 5991 ctsio->kern_total_len = len; 5992 ctsio->kern_data_resid = 0; 5993 ctsio->kern_rel_offset = 0; 5994 ctsio->kern_sg_entries = 0; 5995 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5996 ctsio->be_move_done = ctl_config_move_done; 5997 ctl_datamove((union ctl_io *)ctsio); 5998 5999 return (CTL_RETVAL_COMPLETE); 6000 } 6001 6002 ctl_done((union ctl_io *)ctsio); 6003 6004 return (CTL_RETVAL_COMPLETE); 6005 } 6006 6007 int 6008 ctl_write_same(struct ctl_scsiio *ctsio) 6009 { 6010 struct ctl_lun *lun; 6011 struct ctl_lba_len_flags *lbalen; 6012 uint64_t lba; 6013 uint32_t num_blocks; 6014 int len, retval; 6015 uint8_t byte2; 6016 6017 retval = CTL_RETVAL_COMPLETE; 6018 6019 CTL_DEBUG_PRINT(("ctl_write_same\n")); 6020 6021 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6022 6023 switch (ctsio->cdb[0]) { 6024 case WRITE_SAME_10: { 6025 struct scsi_write_same_10 *cdb; 6026 6027 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 6028 6029 lba = scsi_4btoul(cdb->addr); 6030 num_blocks = scsi_2btoul(cdb->length); 6031 byte2 = cdb->byte2; 6032 break; 6033 } 6034 case WRITE_SAME_16: { 6035 struct scsi_write_same_16 *cdb; 6036 6037 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 6038 6039 lba = scsi_8btou64(cdb->addr); 6040 num_blocks = scsi_4btoul(cdb->length); 6041 byte2 = cdb->byte2; 6042 break; 6043 } 6044 default: 6045 /* 6046 * We got a command we don't support. This shouldn't 6047 * happen, commands should be filtered out above us. 6048 */ 6049 ctl_set_invalid_opcode(ctsio); 6050 ctl_done((union ctl_io *)ctsio); 6051 6052 return (CTL_RETVAL_COMPLETE); 6053 break; /* NOTREACHED */ 6054 } 6055 6056 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 6057 if ((byte2 & SWS_UNMAP) == 0 && 6058 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 6059 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 6060 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 6061 ctl_done((union ctl_io *)ctsio); 6062 return (CTL_RETVAL_COMPLETE); 6063 } 6064 6065 /* 6066 * The first check is to make sure we're in bounds, the second 6067 * check is to catch wrap-around problems. If the lba + num blocks 6068 * is less than the lba, then we've wrapped around and the block 6069 * range is invalid anyway. 6070 */ 6071 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6072 || ((lba + num_blocks) < lba)) { 6073 ctl_set_lba_out_of_range(ctsio); 6074 ctl_done((union ctl_io *)ctsio); 6075 return (CTL_RETVAL_COMPLETE); 6076 } 6077 6078 /* Zero number of blocks means "to the last logical block" */ 6079 if (num_blocks == 0) { 6080 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 6081 ctl_set_invalid_field(ctsio, 6082 /*sks_valid*/ 0, 6083 /*command*/ 1, 6084 /*field*/ 0, 6085 /*bit_valid*/ 0, 6086 /*bit*/ 0); 6087 ctl_done((union ctl_io *)ctsio); 6088 return (CTL_RETVAL_COMPLETE); 6089 } 6090 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6091 } 6092 6093 len = lun->be_lun->blocksize; 6094 6095 /* 6096 * If we've got a kernel request that hasn't been malloced yet, 6097 * malloc it and tell the caller the data buffer is here. 6098 */ 6099 if ((byte2 & SWS_NDOB) == 0 && 6100 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6101 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6102 ctsio->kern_data_len = len; 6103 ctsio->kern_total_len = len; 6104 ctsio->kern_data_resid = 0; 6105 ctsio->kern_rel_offset = 0; 6106 ctsio->kern_sg_entries = 0; 6107 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6108 ctsio->be_move_done = ctl_config_move_done; 6109 ctl_datamove((union ctl_io *)ctsio); 6110 6111 return (CTL_RETVAL_COMPLETE); 6112 } 6113 6114 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6115 lbalen->lba = lba; 6116 lbalen->len = num_blocks; 6117 lbalen->flags = byte2; 6118 retval = lun->backend->config_write((union ctl_io *)ctsio); 6119 6120 return (retval); 6121 } 6122 6123 int 6124 ctl_unmap(struct ctl_scsiio *ctsio) 6125 { 6126 struct ctl_lun *lun; 6127 struct scsi_unmap *cdb; 6128 struct ctl_ptr_len_flags *ptrlen; 6129 struct scsi_unmap_header *hdr; 6130 struct scsi_unmap_desc *buf, *end, *endnz, *range; 6131 uint64_t lba; 6132 uint32_t num_blocks; 6133 int len, retval; 6134 uint8_t byte2; 6135 6136 retval = CTL_RETVAL_COMPLETE; 6137 6138 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6139 6140 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6141 cdb = (struct scsi_unmap *)ctsio->cdb; 6142 6143 len = scsi_2btoul(cdb->length); 6144 byte2 = cdb->byte2; 6145 6146 /* 6147 * If we've got a kernel request that hasn't been malloced yet, 6148 * malloc it and tell the caller the data buffer is here. 6149 */ 6150 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6151 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6152 ctsio->kern_data_len = len; 6153 ctsio->kern_total_len = len; 6154 ctsio->kern_data_resid = 0; 6155 ctsio->kern_rel_offset = 0; 6156 ctsio->kern_sg_entries = 0; 6157 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6158 ctsio->be_move_done = ctl_config_move_done; 6159 ctl_datamove((union ctl_io *)ctsio); 6160 6161 return (CTL_RETVAL_COMPLETE); 6162 } 6163 6164 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6165 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6166 if (len < sizeof (*hdr) || 6167 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6168 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6169 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6170 ctl_set_invalid_field(ctsio, 6171 /*sks_valid*/ 0, 6172 /*command*/ 0, 6173 /*field*/ 0, 6174 /*bit_valid*/ 0, 6175 /*bit*/ 0); 6176 ctl_done((union ctl_io *)ctsio); 6177 return (CTL_RETVAL_COMPLETE); 6178 } 6179 len = scsi_2btoul(hdr->desc_length); 6180 buf = (struct scsi_unmap_desc *)(hdr + 1); 6181 end = buf + len / sizeof(*buf); 6182 6183 endnz = buf; 6184 for (range = buf; range < end; range++) { 6185 lba = scsi_8btou64(range->lba); 6186 num_blocks = scsi_4btoul(range->length); 6187 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6188 || ((lba + num_blocks) < lba)) { 6189 ctl_set_lba_out_of_range(ctsio); 6190 ctl_done((union ctl_io *)ctsio); 6191 return (CTL_RETVAL_COMPLETE); 6192 } 6193 if (num_blocks != 0) 6194 endnz = range + 1; 6195 } 6196 6197 /* 6198 * Block backend can not handle zero last range. 6199 * Filter it out and return if there is nothing left. 6200 */ 6201 len = (uint8_t *)endnz - (uint8_t *)buf; 6202 if (len == 0) { 6203 ctl_set_success(ctsio); 6204 ctl_done((union ctl_io *)ctsio); 6205 return (CTL_RETVAL_COMPLETE); 6206 } 6207 6208 mtx_lock(&lun->lun_lock); 6209 ptrlen = (struct ctl_ptr_len_flags *) 6210 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6211 ptrlen->ptr = (void *)buf; 6212 ptrlen->len = len; 6213 ptrlen->flags = byte2; 6214 ctl_check_blocked(lun); 6215 mtx_unlock(&lun->lun_lock); 6216 6217 retval = lun->backend->config_write((union ctl_io *)ctsio); 6218 return (retval); 6219 } 6220 6221 /* 6222 * Note that this function currently doesn't actually do anything inside 6223 * CTL to enforce things if the DQue bit is turned on. 6224 * 6225 * Also note that this function can't be used in the default case, because 6226 * the DQue bit isn't set in the changeable mask for the control mode page 6227 * anyway. This is just here as an example for how to implement a page 6228 * handler, and a placeholder in case we want to allow the user to turn 6229 * tagged queueing on and off. 6230 * 6231 * The D_SENSE bit handling is functional, however, and will turn 6232 * descriptor sense on and off for a given LUN. 6233 */ 6234 int 6235 ctl_control_page_handler(struct ctl_scsiio *ctsio, 6236 struct ctl_page_index *page_index, uint8_t *page_ptr) 6237 { 6238 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6239 struct ctl_lun *lun; 6240 struct ctl_softc *softc; 6241 int set_ua; 6242 uint32_t initidx; 6243 6244 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6245 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6246 set_ua = 0; 6247 6248 user_cp = (struct scsi_control_page *)page_ptr; 6249 current_cp = (struct scsi_control_page *) 6250 (page_index->page_data + (page_index->page_len * 6251 CTL_PAGE_CURRENT)); 6252 saved_cp = (struct scsi_control_page *) 6253 (page_index->page_data + (page_index->page_len * 6254 CTL_PAGE_SAVED)); 6255 6256 softc = control_softc; 6257 6258 mtx_lock(&lun->lun_lock); 6259 if (((current_cp->rlec & SCP_DSENSE) == 0) 6260 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6261 /* 6262 * Descriptor sense is currently turned off and the user 6263 * wants to turn it on. 6264 */ 6265 current_cp->rlec |= SCP_DSENSE; 6266 saved_cp->rlec |= SCP_DSENSE; 6267 lun->flags |= CTL_LUN_SENSE_DESC; 6268 set_ua = 1; 6269 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6270 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6271 /* 6272 * Descriptor sense is currently turned on, and the user 6273 * wants to turn it off. 6274 */ 6275 current_cp->rlec &= ~SCP_DSENSE; 6276 saved_cp->rlec &= ~SCP_DSENSE; 6277 lun->flags &= ~CTL_LUN_SENSE_DESC; 6278 set_ua = 1; 6279 } 6280 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6281 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6282 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6283 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6284 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6285 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6286 set_ua = 1; 6287 } 6288 if ((current_cp->eca_and_aen & SCP_SWP) != 6289 (user_cp->eca_and_aen & SCP_SWP)) { 6290 current_cp->eca_and_aen &= ~SCP_SWP; 6291 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6292 saved_cp->eca_and_aen &= ~SCP_SWP; 6293 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6294 set_ua = 1; 6295 } 6296 if (set_ua != 0) { 6297 int i; 6298 /* 6299 * Let other initiators know that the mode 6300 * parameters for this LUN have changed. 6301 */ 6302 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6303 if (i == initidx) 6304 continue; 6305 6306 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6307 } 6308 } 6309 mtx_unlock(&lun->lun_lock); 6310 6311 return (0); 6312 } 6313 6314 int 6315 ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6316 struct ctl_page_index *page_index, uint8_t *page_ptr) 6317 { 6318 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6319 struct ctl_lun *lun; 6320 int set_ua; 6321 uint32_t initidx; 6322 6323 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6324 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6325 set_ua = 0; 6326 6327 user_cp = (struct scsi_caching_page *)page_ptr; 6328 current_cp = (struct scsi_caching_page *) 6329 (page_index->page_data + (page_index->page_len * 6330 CTL_PAGE_CURRENT)); 6331 saved_cp = (struct scsi_caching_page *) 6332 (page_index->page_data + (page_index->page_len * 6333 CTL_PAGE_SAVED)); 6334 6335 mtx_lock(&lun->lun_lock); 6336 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6337 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6338 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6339 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6340 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6341 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6342 set_ua = 1; 6343 } 6344 if (set_ua != 0) { 6345 int i; 6346 /* 6347 * Let other initiators know that the mode 6348 * parameters for this LUN have changed. 6349 */ 6350 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6351 if (i == initidx) 6352 continue; 6353 6354 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6355 } 6356 } 6357 mtx_unlock(&lun->lun_lock); 6358 6359 return (0); 6360 } 6361 6362 int 6363 ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6364 struct ctl_page_index *page_index, uint8_t *page_ptr) 6365 { 6366 return (0); 6367 } 6368 6369 int 6370 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6371 struct ctl_page_index *page_index, int pc) 6372 { 6373 struct copan_power_subpage *page; 6374 6375 page = (struct copan_power_subpage *)page_index->page_data + 6376 (page_index->page_len * pc); 6377 6378 switch (pc) { 6379 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6380 /* 6381 * We don't update the changable bits for this page. 6382 */ 6383 break; 6384 case SMS_PAGE_CTRL_CURRENT >> 6: 6385 case SMS_PAGE_CTRL_DEFAULT >> 6: 6386 case SMS_PAGE_CTRL_SAVED >> 6: 6387 #ifdef NEEDTOPORT 6388 ctl_update_power_subpage(page); 6389 #endif 6390 break; 6391 default: 6392 #ifdef NEEDTOPORT 6393 EPRINT(0, "Invalid PC %d!!", pc); 6394 #endif 6395 break; 6396 } 6397 return (0); 6398 } 6399 6400 6401 int 6402 ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6403 struct ctl_page_index *page_index, uint8_t *page_ptr) 6404 { 6405 struct copan_aps_subpage *user_sp; 6406 struct copan_aps_subpage *current_sp; 6407 union ctl_modepage_info *modepage_info; 6408 struct ctl_softc *softc; 6409 struct ctl_lun *lun; 6410 int retval; 6411 6412 retval = CTL_RETVAL_COMPLETE; 6413 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6414 (page_index->page_len * CTL_PAGE_CURRENT)); 6415 softc = control_softc; 6416 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6417 6418 user_sp = (struct copan_aps_subpage *)page_ptr; 6419 6420 modepage_info = (union ctl_modepage_info *) 6421 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6422 6423 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6424 modepage_info->header.subpage = page_index->subpage; 6425 modepage_info->aps.lock_active = user_sp->lock_active; 6426 6427 mtx_lock(&softc->ctl_lock); 6428 6429 /* 6430 * If there is a request to lock the LUN and another LUN is locked 6431 * this is an error. If the requested LUN is already locked ignore 6432 * the request. If no LUN is locked attempt to lock it. 6433 * if there is a request to unlock the LUN and the LUN is currently 6434 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6435 * if another LUN is locked or no LUN is locked. 6436 */ 6437 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6438 if (softc->aps_locked_lun == lun->lun) { 6439 /* 6440 * This LUN is already locked, so we're done. 6441 */ 6442 retval = CTL_RETVAL_COMPLETE; 6443 } else if (softc->aps_locked_lun == 0) { 6444 /* 6445 * No one has the lock, pass the request to the 6446 * backend. 6447 */ 6448 retval = lun->backend->config_write( 6449 (union ctl_io *)ctsio); 6450 } else { 6451 /* 6452 * Someone else has the lock, throw out the request. 6453 */ 6454 ctl_set_already_locked(ctsio); 6455 free(ctsio->kern_data_ptr, M_CTL); 6456 ctl_done((union ctl_io *)ctsio); 6457 6458 /* 6459 * Set the return value so that ctl_do_mode_select() 6460 * won't try to complete the command. We already 6461 * completed it here. 6462 */ 6463 retval = CTL_RETVAL_ERROR; 6464 } 6465 } else if (softc->aps_locked_lun == lun->lun) { 6466 /* 6467 * This LUN is locked, so pass the unlock request to the 6468 * backend. 6469 */ 6470 retval = lun->backend->config_write((union ctl_io *)ctsio); 6471 } 6472 mtx_unlock(&softc->ctl_lock); 6473 6474 return (retval); 6475 } 6476 6477 int 6478 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6479 struct ctl_page_index *page_index, 6480 uint8_t *page_ptr) 6481 { 6482 uint8_t *c; 6483 int i; 6484 6485 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6486 ctl_time_io_secs = 6487 (c[0] << 8) | 6488 (c[1] << 0) | 6489 0; 6490 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6491 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6492 printf("page data:"); 6493 for (i=0; i<8; i++) 6494 printf(" %.2x",page_ptr[i]); 6495 printf("\n"); 6496 return (0); 6497 } 6498 6499 int 6500 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6501 struct ctl_page_index *page_index, 6502 int pc) 6503 { 6504 struct copan_debugconf_subpage *page; 6505 6506 page = (struct copan_debugconf_subpage *)page_index->page_data + 6507 (page_index->page_len * pc); 6508 6509 switch (pc) { 6510 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6511 case SMS_PAGE_CTRL_DEFAULT >> 6: 6512 case SMS_PAGE_CTRL_SAVED >> 6: 6513 /* 6514 * We don't update the changable or default bits for this page. 6515 */ 6516 break; 6517 case SMS_PAGE_CTRL_CURRENT >> 6: 6518 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6519 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6520 break; 6521 default: 6522 #ifdef NEEDTOPORT 6523 EPRINT(0, "Invalid PC %d!!", pc); 6524 #endif /* NEEDTOPORT */ 6525 break; 6526 } 6527 return (0); 6528 } 6529 6530 6531 static int 6532 ctl_do_mode_select(union ctl_io *io) 6533 { 6534 struct scsi_mode_page_header *page_header; 6535 struct ctl_page_index *page_index; 6536 struct ctl_scsiio *ctsio; 6537 int control_dev, page_len; 6538 int page_len_offset, page_len_size; 6539 union ctl_modepage_info *modepage_info; 6540 struct ctl_lun *lun; 6541 int *len_left, *len_used; 6542 int retval, i; 6543 6544 ctsio = &io->scsiio; 6545 page_index = NULL; 6546 page_len = 0; 6547 retval = CTL_RETVAL_COMPLETE; 6548 6549 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6550 6551 if (lun->be_lun->lun_type != T_DIRECT) 6552 control_dev = 1; 6553 else 6554 control_dev = 0; 6555 6556 modepage_info = (union ctl_modepage_info *) 6557 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6558 len_left = &modepage_info->header.len_left; 6559 len_used = &modepage_info->header.len_used; 6560 6561 do_next_page: 6562 6563 page_header = (struct scsi_mode_page_header *) 6564 (ctsio->kern_data_ptr + *len_used); 6565 6566 if (*len_left == 0) { 6567 free(ctsio->kern_data_ptr, M_CTL); 6568 ctl_set_success(ctsio); 6569 ctl_done((union ctl_io *)ctsio); 6570 return (CTL_RETVAL_COMPLETE); 6571 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6572 6573 free(ctsio->kern_data_ptr, M_CTL); 6574 ctl_set_param_len_error(ctsio); 6575 ctl_done((union ctl_io *)ctsio); 6576 return (CTL_RETVAL_COMPLETE); 6577 6578 } else if ((page_header->page_code & SMPH_SPF) 6579 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6580 6581 free(ctsio->kern_data_ptr, M_CTL); 6582 ctl_set_param_len_error(ctsio); 6583 ctl_done((union ctl_io *)ctsio); 6584 return (CTL_RETVAL_COMPLETE); 6585 } 6586 6587 6588 /* 6589 * XXX KDM should we do something with the block descriptor? 6590 */ 6591 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6592 6593 if ((control_dev != 0) 6594 && (lun->mode_pages.index[i].page_flags & 6595 CTL_PAGE_FLAG_DISK_ONLY)) 6596 continue; 6597 6598 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6599 (page_header->page_code & SMPH_PC_MASK)) 6600 continue; 6601 6602 /* 6603 * If neither page has a subpage code, then we've got a 6604 * match. 6605 */ 6606 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6607 && ((page_header->page_code & SMPH_SPF) == 0)) { 6608 page_index = &lun->mode_pages.index[i]; 6609 page_len = page_header->page_length; 6610 break; 6611 } 6612 6613 /* 6614 * If both pages have subpages, then the subpage numbers 6615 * have to match. 6616 */ 6617 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6618 && (page_header->page_code & SMPH_SPF)) { 6619 struct scsi_mode_page_header_sp *sph; 6620 6621 sph = (struct scsi_mode_page_header_sp *)page_header; 6622 6623 if (lun->mode_pages.index[i].subpage == 6624 sph->subpage) { 6625 page_index = &lun->mode_pages.index[i]; 6626 page_len = scsi_2btoul(sph->page_length); 6627 break; 6628 } 6629 } 6630 } 6631 6632 /* 6633 * If we couldn't find the page, or if we don't have a mode select 6634 * handler for it, send back an error to the user. 6635 */ 6636 if ((page_index == NULL) 6637 || (page_index->select_handler == NULL)) { 6638 ctl_set_invalid_field(ctsio, 6639 /*sks_valid*/ 1, 6640 /*command*/ 0, 6641 /*field*/ *len_used, 6642 /*bit_valid*/ 0, 6643 /*bit*/ 0); 6644 free(ctsio->kern_data_ptr, M_CTL); 6645 ctl_done((union ctl_io *)ctsio); 6646 return (CTL_RETVAL_COMPLETE); 6647 } 6648 6649 if (page_index->page_code & SMPH_SPF) { 6650 page_len_offset = 2; 6651 page_len_size = 2; 6652 } else { 6653 page_len_size = 1; 6654 page_len_offset = 1; 6655 } 6656 6657 /* 6658 * If the length the initiator gives us isn't the one we specify in 6659 * the mode page header, or if they didn't specify enough data in 6660 * the CDB to avoid truncating this page, kick out the request. 6661 */ 6662 if ((page_len != (page_index->page_len - page_len_offset - 6663 page_len_size)) 6664 || (*len_left < page_index->page_len)) { 6665 6666 6667 ctl_set_invalid_field(ctsio, 6668 /*sks_valid*/ 1, 6669 /*command*/ 0, 6670 /*field*/ *len_used + page_len_offset, 6671 /*bit_valid*/ 0, 6672 /*bit*/ 0); 6673 free(ctsio->kern_data_ptr, M_CTL); 6674 ctl_done((union ctl_io *)ctsio); 6675 return (CTL_RETVAL_COMPLETE); 6676 } 6677 6678 /* 6679 * Run through the mode page, checking to make sure that the bits 6680 * the user changed are actually legal for him to change. 6681 */ 6682 for (i = 0; i < page_index->page_len; i++) { 6683 uint8_t *user_byte, *change_mask, *current_byte; 6684 int bad_bit; 6685 int j; 6686 6687 user_byte = (uint8_t *)page_header + i; 6688 change_mask = page_index->page_data + 6689 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6690 current_byte = page_index->page_data + 6691 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6692 6693 /* 6694 * Check to see whether the user set any bits in this byte 6695 * that he is not allowed to set. 6696 */ 6697 if ((*user_byte & ~(*change_mask)) == 6698 (*current_byte & ~(*change_mask))) 6699 continue; 6700 6701 /* 6702 * Go through bit by bit to determine which one is illegal. 6703 */ 6704 bad_bit = 0; 6705 for (j = 7; j >= 0; j--) { 6706 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6707 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6708 bad_bit = i; 6709 break; 6710 } 6711 } 6712 ctl_set_invalid_field(ctsio, 6713 /*sks_valid*/ 1, 6714 /*command*/ 0, 6715 /*field*/ *len_used + i, 6716 /*bit_valid*/ 1, 6717 /*bit*/ bad_bit); 6718 free(ctsio->kern_data_ptr, M_CTL); 6719 ctl_done((union ctl_io *)ctsio); 6720 return (CTL_RETVAL_COMPLETE); 6721 } 6722 6723 /* 6724 * Decrement these before we call the page handler, since we may 6725 * end up getting called back one way or another before the handler 6726 * returns to this context. 6727 */ 6728 *len_left -= page_index->page_len; 6729 *len_used += page_index->page_len; 6730 6731 retval = page_index->select_handler(ctsio, page_index, 6732 (uint8_t *)page_header); 6733 6734 /* 6735 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6736 * wait until this queued command completes to finish processing 6737 * the mode page. If it returns anything other than 6738 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6739 * already set the sense information, freed the data pointer, and 6740 * completed the io for us. 6741 */ 6742 if (retval != CTL_RETVAL_COMPLETE) 6743 goto bailout_no_done; 6744 6745 /* 6746 * If the initiator sent us more than one page, parse the next one. 6747 */ 6748 if (*len_left > 0) 6749 goto do_next_page; 6750 6751 ctl_set_success(ctsio); 6752 free(ctsio->kern_data_ptr, M_CTL); 6753 ctl_done((union ctl_io *)ctsio); 6754 6755 bailout_no_done: 6756 6757 return (CTL_RETVAL_COMPLETE); 6758 6759 } 6760 6761 int 6762 ctl_mode_select(struct ctl_scsiio *ctsio) 6763 { 6764 int param_len, pf, sp; 6765 int header_size, bd_len; 6766 int len_left, len_used; 6767 struct ctl_page_index *page_index; 6768 struct ctl_lun *lun; 6769 int control_dev, page_len; 6770 union ctl_modepage_info *modepage_info; 6771 int retval; 6772 6773 pf = 0; 6774 sp = 0; 6775 page_len = 0; 6776 len_used = 0; 6777 len_left = 0; 6778 retval = 0; 6779 bd_len = 0; 6780 page_index = NULL; 6781 6782 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6783 6784 if (lun->be_lun->lun_type != T_DIRECT) 6785 control_dev = 1; 6786 else 6787 control_dev = 0; 6788 6789 switch (ctsio->cdb[0]) { 6790 case MODE_SELECT_6: { 6791 struct scsi_mode_select_6 *cdb; 6792 6793 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6794 6795 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6796 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6797 6798 param_len = cdb->length; 6799 header_size = sizeof(struct scsi_mode_header_6); 6800 break; 6801 } 6802 case MODE_SELECT_10: { 6803 struct scsi_mode_select_10 *cdb; 6804 6805 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6806 6807 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6808 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6809 6810 param_len = scsi_2btoul(cdb->length); 6811 header_size = sizeof(struct scsi_mode_header_10); 6812 break; 6813 } 6814 default: 6815 ctl_set_invalid_opcode(ctsio); 6816 ctl_done((union ctl_io *)ctsio); 6817 return (CTL_RETVAL_COMPLETE); 6818 break; /* NOTREACHED */ 6819 } 6820 6821 /* 6822 * From SPC-3: 6823 * "A parameter list length of zero indicates that the Data-Out Buffer 6824 * shall be empty. This condition shall not be considered as an error." 6825 */ 6826 if (param_len == 0) { 6827 ctl_set_success(ctsio); 6828 ctl_done((union ctl_io *)ctsio); 6829 return (CTL_RETVAL_COMPLETE); 6830 } 6831 6832 /* 6833 * Since we'll hit this the first time through, prior to 6834 * allocation, we don't need to free a data buffer here. 6835 */ 6836 if (param_len < header_size) { 6837 ctl_set_param_len_error(ctsio); 6838 ctl_done((union ctl_io *)ctsio); 6839 return (CTL_RETVAL_COMPLETE); 6840 } 6841 6842 /* 6843 * Allocate the data buffer and grab the user's data. In theory, 6844 * we shouldn't have to sanity check the parameter list length here 6845 * because the maximum size is 64K. We should be able to malloc 6846 * that much without too many problems. 6847 */ 6848 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6849 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6850 ctsio->kern_data_len = param_len; 6851 ctsio->kern_total_len = param_len; 6852 ctsio->kern_data_resid = 0; 6853 ctsio->kern_rel_offset = 0; 6854 ctsio->kern_sg_entries = 0; 6855 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6856 ctsio->be_move_done = ctl_config_move_done; 6857 ctl_datamove((union ctl_io *)ctsio); 6858 6859 return (CTL_RETVAL_COMPLETE); 6860 } 6861 6862 switch (ctsio->cdb[0]) { 6863 case MODE_SELECT_6: { 6864 struct scsi_mode_header_6 *mh6; 6865 6866 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6867 bd_len = mh6->blk_desc_len; 6868 break; 6869 } 6870 case MODE_SELECT_10: { 6871 struct scsi_mode_header_10 *mh10; 6872 6873 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6874 bd_len = scsi_2btoul(mh10->blk_desc_len); 6875 break; 6876 } 6877 default: 6878 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6879 break; 6880 } 6881 6882 if (param_len < (header_size + bd_len)) { 6883 free(ctsio->kern_data_ptr, M_CTL); 6884 ctl_set_param_len_error(ctsio); 6885 ctl_done((union ctl_io *)ctsio); 6886 return (CTL_RETVAL_COMPLETE); 6887 } 6888 6889 /* 6890 * Set the IO_CONT flag, so that if this I/O gets passed to 6891 * ctl_config_write_done(), it'll get passed back to 6892 * ctl_do_mode_select() for further processing, or completion if 6893 * we're all done. 6894 */ 6895 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6896 ctsio->io_cont = ctl_do_mode_select; 6897 6898 modepage_info = (union ctl_modepage_info *) 6899 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6900 6901 memset(modepage_info, 0, sizeof(*modepage_info)); 6902 6903 len_left = param_len - header_size - bd_len; 6904 len_used = header_size + bd_len; 6905 6906 modepage_info->header.len_left = len_left; 6907 modepage_info->header.len_used = len_used; 6908 6909 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6910 } 6911 6912 int 6913 ctl_mode_sense(struct ctl_scsiio *ctsio) 6914 { 6915 struct ctl_lun *lun; 6916 int pc, page_code, dbd, llba, subpage; 6917 int alloc_len, page_len, header_len, total_len; 6918 struct scsi_mode_block_descr *block_desc; 6919 struct ctl_page_index *page_index; 6920 int control_dev; 6921 6922 dbd = 0; 6923 llba = 0; 6924 block_desc = NULL; 6925 page_index = NULL; 6926 6927 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6928 6929 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6930 6931 if (lun->be_lun->lun_type != T_DIRECT) 6932 control_dev = 1; 6933 else 6934 control_dev = 0; 6935 6936 if (lun->flags & CTL_LUN_PR_RESERVED) { 6937 uint32_t residx; 6938 6939 /* 6940 * XXX KDM need a lock here. 6941 */ 6942 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6943 if ((lun->res_type == SPR_TYPE_EX_AC 6944 && residx != lun->pr_res_idx) 6945 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6946 || lun->res_type == SPR_TYPE_EX_AC_AR) 6947 && lun->pr_keys[residx] == 0)) { 6948 ctl_set_reservation_conflict(ctsio); 6949 ctl_done((union ctl_io *)ctsio); 6950 return (CTL_RETVAL_COMPLETE); 6951 } 6952 } 6953 6954 switch (ctsio->cdb[0]) { 6955 case MODE_SENSE_6: { 6956 struct scsi_mode_sense_6 *cdb; 6957 6958 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6959 6960 header_len = sizeof(struct scsi_mode_hdr_6); 6961 if (cdb->byte2 & SMS_DBD) 6962 dbd = 1; 6963 else 6964 header_len += sizeof(struct scsi_mode_block_descr); 6965 6966 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6967 page_code = cdb->page & SMS_PAGE_CODE; 6968 subpage = cdb->subpage; 6969 alloc_len = cdb->length; 6970 break; 6971 } 6972 case MODE_SENSE_10: { 6973 struct scsi_mode_sense_10 *cdb; 6974 6975 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6976 6977 header_len = sizeof(struct scsi_mode_hdr_10); 6978 6979 if (cdb->byte2 & SMS_DBD) 6980 dbd = 1; 6981 else 6982 header_len += sizeof(struct scsi_mode_block_descr); 6983 if (cdb->byte2 & SMS10_LLBAA) 6984 llba = 1; 6985 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6986 page_code = cdb->page & SMS_PAGE_CODE; 6987 subpage = cdb->subpage; 6988 alloc_len = scsi_2btoul(cdb->length); 6989 break; 6990 } 6991 default: 6992 ctl_set_invalid_opcode(ctsio); 6993 ctl_done((union ctl_io *)ctsio); 6994 return (CTL_RETVAL_COMPLETE); 6995 break; /* NOTREACHED */ 6996 } 6997 6998 /* 6999 * We have to make a first pass through to calculate the size of 7000 * the pages that match the user's query. Then we allocate enough 7001 * memory to hold it, and actually copy the data into the buffer. 7002 */ 7003 switch (page_code) { 7004 case SMS_ALL_PAGES_PAGE: { 7005 int i; 7006 7007 page_len = 0; 7008 7009 /* 7010 * At the moment, values other than 0 and 0xff here are 7011 * reserved according to SPC-3. 7012 */ 7013 if ((subpage != SMS_SUBPAGE_PAGE_0) 7014 && (subpage != SMS_SUBPAGE_ALL)) { 7015 ctl_set_invalid_field(ctsio, 7016 /*sks_valid*/ 1, 7017 /*command*/ 1, 7018 /*field*/ 3, 7019 /*bit_valid*/ 0, 7020 /*bit*/ 0); 7021 ctl_done((union ctl_io *)ctsio); 7022 return (CTL_RETVAL_COMPLETE); 7023 } 7024 7025 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7026 if ((control_dev != 0) 7027 && (lun->mode_pages.index[i].page_flags & 7028 CTL_PAGE_FLAG_DISK_ONLY)) 7029 continue; 7030 7031 /* 7032 * We don't use this subpage if the user didn't 7033 * request all subpages. 7034 */ 7035 if ((lun->mode_pages.index[i].subpage != 0) 7036 && (subpage == SMS_SUBPAGE_PAGE_0)) 7037 continue; 7038 7039 #if 0 7040 printf("found page %#x len %d\n", 7041 lun->mode_pages.index[i].page_code & 7042 SMPH_PC_MASK, 7043 lun->mode_pages.index[i].page_len); 7044 #endif 7045 page_len += lun->mode_pages.index[i].page_len; 7046 } 7047 break; 7048 } 7049 default: { 7050 int i; 7051 7052 page_len = 0; 7053 7054 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7055 /* Look for the right page code */ 7056 if ((lun->mode_pages.index[i].page_code & 7057 SMPH_PC_MASK) != page_code) 7058 continue; 7059 7060 /* Look for the right subpage or the subpage wildcard*/ 7061 if ((lun->mode_pages.index[i].subpage != subpage) 7062 && (subpage != SMS_SUBPAGE_ALL)) 7063 continue; 7064 7065 /* Make sure the page is supported for this dev type */ 7066 if ((control_dev != 0) 7067 && (lun->mode_pages.index[i].page_flags & 7068 CTL_PAGE_FLAG_DISK_ONLY)) 7069 continue; 7070 7071 #if 0 7072 printf("found page %#x len %d\n", 7073 lun->mode_pages.index[i].page_code & 7074 SMPH_PC_MASK, 7075 lun->mode_pages.index[i].page_len); 7076 #endif 7077 7078 page_len += lun->mode_pages.index[i].page_len; 7079 } 7080 7081 if (page_len == 0) { 7082 ctl_set_invalid_field(ctsio, 7083 /*sks_valid*/ 1, 7084 /*command*/ 1, 7085 /*field*/ 2, 7086 /*bit_valid*/ 1, 7087 /*bit*/ 5); 7088 ctl_done((union ctl_io *)ctsio); 7089 return (CTL_RETVAL_COMPLETE); 7090 } 7091 break; 7092 } 7093 } 7094 7095 total_len = header_len + page_len; 7096 #if 0 7097 printf("header_len = %d, page_len = %d, total_len = %d\n", 7098 header_len, page_len, total_len); 7099 #endif 7100 7101 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7102 ctsio->kern_sg_entries = 0; 7103 ctsio->kern_data_resid = 0; 7104 ctsio->kern_rel_offset = 0; 7105 if (total_len < alloc_len) { 7106 ctsio->residual = alloc_len - total_len; 7107 ctsio->kern_data_len = total_len; 7108 ctsio->kern_total_len = total_len; 7109 } else { 7110 ctsio->residual = 0; 7111 ctsio->kern_data_len = alloc_len; 7112 ctsio->kern_total_len = alloc_len; 7113 } 7114 7115 switch (ctsio->cdb[0]) { 7116 case MODE_SENSE_6: { 7117 struct scsi_mode_hdr_6 *header; 7118 7119 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7120 7121 header->datalen = ctl_min(total_len - 1, 254); 7122 if (control_dev == 0) { 7123 header->dev_specific = 0x10; /* DPOFUA */ 7124 if ((lun->flags & CTL_LUN_READONLY) || 7125 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 7126 .eca_and_aen & SCP_SWP) != 0) 7127 header->dev_specific |= 0x80; /* WP */ 7128 } 7129 if (dbd) 7130 header->block_descr_len = 0; 7131 else 7132 header->block_descr_len = 7133 sizeof(struct scsi_mode_block_descr); 7134 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7135 break; 7136 } 7137 case MODE_SENSE_10: { 7138 struct scsi_mode_hdr_10 *header; 7139 int datalen; 7140 7141 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7142 7143 datalen = ctl_min(total_len - 2, 65533); 7144 scsi_ulto2b(datalen, header->datalen); 7145 if (control_dev == 0) { 7146 header->dev_specific = 0x10; /* DPOFUA */ 7147 if ((lun->flags & CTL_LUN_READONLY) || 7148 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 7149 .eca_and_aen & SCP_SWP) != 0) 7150 header->dev_specific |= 0x80; /* WP */ 7151 } 7152 if (dbd) 7153 scsi_ulto2b(0, header->block_descr_len); 7154 else 7155 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7156 header->block_descr_len); 7157 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7158 break; 7159 } 7160 default: 7161 panic("invalid CDB type %#x", ctsio->cdb[0]); 7162 break; /* NOTREACHED */ 7163 } 7164 7165 /* 7166 * If we've got a disk, use its blocksize in the block 7167 * descriptor. Otherwise, just set it to 0. 7168 */ 7169 if (dbd == 0) { 7170 if (control_dev == 0) 7171 scsi_ulto3b(lun->be_lun->blocksize, 7172 block_desc->block_len); 7173 else 7174 scsi_ulto3b(0, block_desc->block_len); 7175 } 7176 7177 switch (page_code) { 7178 case SMS_ALL_PAGES_PAGE: { 7179 int i, data_used; 7180 7181 data_used = header_len; 7182 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7183 struct ctl_page_index *page_index; 7184 7185 page_index = &lun->mode_pages.index[i]; 7186 7187 if ((control_dev != 0) 7188 && (page_index->page_flags & 7189 CTL_PAGE_FLAG_DISK_ONLY)) 7190 continue; 7191 7192 /* 7193 * We don't use this subpage if the user didn't 7194 * request all subpages. We already checked (above) 7195 * to make sure the user only specified a subpage 7196 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7197 */ 7198 if ((page_index->subpage != 0) 7199 && (subpage == SMS_SUBPAGE_PAGE_0)) 7200 continue; 7201 7202 /* 7203 * Call the handler, if it exists, to update the 7204 * page to the latest values. 7205 */ 7206 if (page_index->sense_handler != NULL) 7207 page_index->sense_handler(ctsio, page_index,pc); 7208 7209 memcpy(ctsio->kern_data_ptr + data_used, 7210 page_index->page_data + 7211 (page_index->page_len * pc), 7212 page_index->page_len); 7213 data_used += page_index->page_len; 7214 } 7215 break; 7216 } 7217 default: { 7218 int i, data_used; 7219 7220 data_used = header_len; 7221 7222 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7223 struct ctl_page_index *page_index; 7224 7225 page_index = &lun->mode_pages.index[i]; 7226 7227 /* Look for the right page code */ 7228 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7229 continue; 7230 7231 /* Look for the right subpage or the subpage wildcard*/ 7232 if ((page_index->subpage != subpage) 7233 && (subpage != SMS_SUBPAGE_ALL)) 7234 continue; 7235 7236 /* Make sure the page is supported for this dev type */ 7237 if ((control_dev != 0) 7238 && (page_index->page_flags & 7239 CTL_PAGE_FLAG_DISK_ONLY)) 7240 continue; 7241 7242 /* 7243 * Call the handler, if it exists, to update the 7244 * page to the latest values. 7245 */ 7246 if (page_index->sense_handler != NULL) 7247 page_index->sense_handler(ctsio, page_index,pc); 7248 7249 memcpy(ctsio->kern_data_ptr + data_used, 7250 page_index->page_data + 7251 (page_index->page_len * pc), 7252 page_index->page_len); 7253 data_used += page_index->page_len; 7254 } 7255 break; 7256 } 7257 } 7258 7259 ctsio->scsi_status = SCSI_STATUS_OK; 7260 7261 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7262 ctsio->be_move_done = ctl_config_move_done; 7263 ctl_datamove((union ctl_io *)ctsio); 7264 7265 return (CTL_RETVAL_COMPLETE); 7266 } 7267 7268 int 7269 ctl_read_capacity(struct ctl_scsiio *ctsio) 7270 { 7271 struct scsi_read_capacity *cdb; 7272 struct scsi_read_capacity_data *data; 7273 struct ctl_lun *lun; 7274 uint32_t lba; 7275 7276 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7277 7278 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7279 7280 lba = scsi_4btoul(cdb->addr); 7281 if (((cdb->pmi & SRC_PMI) == 0) 7282 && (lba != 0)) { 7283 ctl_set_invalid_field(/*ctsio*/ ctsio, 7284 /*sks_valid*/ 1, 7285 /*command*/ 1, 7286 /*field*/ 2, 7287 /*bit_valid*/ 0, 7288 /*bit*/ 0); 7289 ctl_done((union ctl_io *)ctsio); 7290 return (CTL_RETVAL_COMPLETE); 7291 } 7292 7293 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7294 7295 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7296 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7297 ctsio->residual = 0; 7298 ctsio->kern_data_len = sizeof(*data); 7299 ctsio->kern_total_len = sizeof(*data); 7300 ctsio->kern_data_resid = 0; 7301 ctsio->kern_rel_offset = 0; 7302 ctsio->kern_sg_entries = 0; 7303 7304 /* 7305 * If the maximum LBA is greater than 0xfffffffe, the user must 7306 * issue a SERVICE ACTION IN (16) command, with the read capacity 7307 * serivce action set. 7308 */ 7309 if (lun->be_lun->maxlba > 0xfffffffe) 7310 scsi_ulto4b(0xffffffff, data->addr); 7311 else 7312 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7313 7314 /* 7315 * XXX KDM this may not be 512 bytes... 7316 */ 7317 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7318 7319 ctsio->scsi_status = SCSI_STATUS_OK; 7320 7321 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7322 ctsio->be_move_done = ctl_config_move_done; 7323 ctl_datamove((union ctl_io *)ctsio); 7324 7325 return (CTL_RETVAL_COMPLETE); 7326 } 7327 7328 int 7329 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7330 { 7331 struct scsi_read_capacity_16 *cdb; 7332 struct scsi_read_capacity_data_long *data; 7333 struct ctl_lun *lun; 7334 uint64_t lba; 7335 uint32_t alloc_len; 7336 7337 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7338 7339 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7340 7341 alloc_len = scsi_4btoul(cdb->alloc_len); 7342 lba = scsi_8btou64(cdb->addr); 7343 7344 if ((cdb->reladr & SRC16_PMI) 7345 && (lba != 0)) { 7346 ctl_set_invalid_field(/*ctsio*/ ctsio, 7347 /*sks_valid*/ 1, 7348 /*command*/ 1, 7349 /*field*/ 2, 7350 /*bit_valid*/ 0, 7351 /*bit*/ 0); 7352 ctl_done((union ctl_io *)ctsio); 7353 return (CTL_RETVAL_COMPLETE); 7354 } 7355 7356 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7357 7358 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7359 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7360 7361 if (sizeof(*data) < alloc_len) { 7362 ctsio->residual = alloc_len - sizeof(*data); 7363 ctsio->kern_data_len = sizeof(*data); 7364 ctsio->kern_total_len = sizeof(*data); 7365 } else { 7366 ctsio->residual = 0; 7367 ctsio->kern_data_len = alloc_len; 7368 ctsio->kern_total_len = alloc_len; 7369 } 7370 ctsio->kern_data_resid = 0; 7371 ctsio->kern_rel_offset = 0; 7372 ctsio->kern_sg_entries = 0; 7373 7374 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7375 /* XXX KDM this may not be 512 bytes... */ 7376 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7377 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7378 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7379 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7380 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7381 7382 ctsio->scsi_status = SCSI_STATUS_OK; 7383 7384 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7385 ctsio->be_move_done = ctl_config_move_done; 7386 ctl_datamove((union ctl_io *)ctsio); 7387 7388 return (CTL_RETVAL_COMPLETE); 7389 } 7390 7391 int 7392 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7393 { 7394 struct scsi_maintenance_in *cdb; 7395 int retval; 7396 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7397 int num_target_port_groups, num_target_ports, single; 7398 struct ctl_lun *lun; 7399 struct ctl_softc *softc; 7400 struct ctl_port *port; 7401 struct scsi_target_group_data *rtg_ptr; 7402 struct scsi_target_group_data_extended *rtg_ext_ptr; 7403 struct scsi_target_port_group_descriptor *tpg_desc; 7404 7405 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7406 7407 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7408 softc = control_softc; 7409 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7410 7411 retval = CTL_RETVAL_COMPLETE; 7412 7413 switch (cdb->byte2 & STG_PDF_MASK) { 7414 case STG_PDF_LENGTH: 7415 ext = 0; 7416 break; 7417 case STG_PDF_EXTENDED: 7418 ext = 1; 7419 break; 7420 default: 7421 ctl_set_invalid_field(/*ctsio*/ ctsio, 7422 /*sks_valid*/ 1, 7423 /*command*/ 1, 7424 /*field*/ 2, 7425 /*bit_valid*/ 1, 7426 /*bit*/ 5); 7427 ctl_done((union ctl_io *)ctsio); 7428 return(retval); 7429 } 7430 7431 single = ctl_is_single; 7432 if (single) 7433 num_target_port_groups = 1; 7434 else 7435 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7436 num_target_ports = 0; 7437 mtx_lock(&softc->ctl_lock); 7438 STAILQ_FOREACH(port, &softc->port_list, links) { 7439 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7440 continue; 7441 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7442 continue; 7443 num_target_ports++; 7444 } 7445 mtx_unlock(&softc->ctl_lock); 7446 7447 if (ext) 7448 total_len = sizeof(struct scsi_target_group_data_extended); 7449 else 7450 total_len = sizeof(struct scsi_target_group_data); 7451 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7452 num_target_port_groups + 7453 sizeof(struct scsi_target_port_descriptor) * 7454 num_target_ports * num_target_port_groups; 7455 7456 alloc_len = scsi_4btoul(cdb->length); 7457 7458 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7459 7460 ctsio->kern_sg_entries = 0; 7461 7462 if (total_len < alloc_len) { 7463 ctsio->residual = alloc_len - total_len; 7464 ctsio->kern_data_len = total_len; 7465 ctsio->kern_total_len = total_len; 7466 } else { 7467 ctsio->residual = 0; 7468 ctsio->kern_data_len = alloc_len; 7469 ctsio->kern_total_len = alloc_len; 7470 } 7471 ctsio->kern_data_resid = 0; 7472 ctsio->kern_rel_offset = 0; 7473 7474 if (ext) { 7475 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7476 ctsio->kern_data_ptr; 7477 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7478 rtg_ext_ptr->format_type = 0x10; 7479 rtg_ext_ptr->implicit_transition_time = 0; 7480 tpg_desc = &rtg_ext_ptr->groups[0]; 7481 } else { 7482 rtg_ptr = (struct scsi_target_group_data *) 7483 ctsio->kern_data_ptr; 7484 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7485 tpg_desc = &rtg_ptr->groups[0]; 7486 } 7487 7488 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7489 mtx_lock(&softc->ctl_lock); 7490 for (g = 0; g < num_target_port_groups; g++) { 7491 if (g == pg) 7492 tpg_desc->pref_state = TPG_PRIMARY | 7493 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7494 else 7495 tpg_desc->pref_state = 7496 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7497 tpg_desc->support = TPG_AO_SUP; 7498 if (!single) 7499 tpg_desc->support |= TPG_AN_SUP; 7500 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7501 tpg_desc->status = TPG_IMPLICIT; 7502 pc = 0; 7503 STAILQ_FOREACH(port, &softc->port_list, links) { 7504 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7505 continue; 7506 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7507 CTL_MAX_LUNS) 7508 continue; 7509 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7510 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7511 relative_target_port_identifier); 7512 pc++; 7513 } 7514 tpg_desc->target_port_count = pc; 7515 tpg_desc = (struct scsi_target_port_group_descriptor *) 7516 &tpg_desc->descriptors[pc]; 7517 } 7518 mtx_unlock(&softc->ctl_lock); 7519 7520 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7521 ctsio->be_move_done = ctl_config_move_done; 7522 7523 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7524 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7525 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7526 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7527 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7528 7529 ctl_datamove((union ctl_io *)ctsio); 7530 return(retval); 7531 } 7532 7533 int 7534 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7535 { 7536 struct ctl_lun *lun; 7537 struct scsi_report_supported_opcodes *cdb; 7538 const struct ctl_cmd_entry *entry, *sentry; 7539 struct scsi_report_supported_opcodes_all *all; 7540 struct scsi_report_supported_opcodes_descr *descr; 7541 struct scsi_report_supported_opcodes_one *one; 7542 int retval; 7543 int alloc_len, total_len; 7544 int opcode, service_action, i, j, num; 7545 7546 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7547 7548 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7549 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7550 7551 retval = CTL_RETVAL_COMPLETE; 7552 7553 opcode = cdb->requested_opcode; 7554 service_action = scsi_2btoul(cdb->requested_service_action); 7555 switch (cdb->options & RSO_OPTIONS_MASK) { 7556 case RSO_OPTIONS_ALL: 7557 num = 0; 7558 for (i = 0; i < 256; i++) { 7559 entry = &ctl_cmd_table[i]; 7560 if (entry->flags & CTL_CMD_FLAG_SA5) { 7561 for (j = 0; j < 32; j++) { 7562 sentry = &((const struct ctl_cmd_entry *) 7563 entry->execute)[j]; 7564 if (ctl_cmd_applicable( 7565 lun->be_lun->lun_type, sentry)) 7566 num++; 7567 } 7568 } else { 7569 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7570 entry)) 7571 num++; 7572 } 7573 } 7574 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7575 num * sizeof(struct scsi_report_supported_opcodes_descr); 7576 break; 7577 case RSO_OPTIONS_OC: 7578 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7579 ctl_set_invalid_field(/*ctsio*/ ctsio, 7580 /*sks_valid*/ 1, 7581 /*command*/ 1, 7582 /*field*/ 2, 7583 /*bit_valid*/ 1, 7584 /*bit*/ 2); 7585 ctl_done((union ctl_io *)ctsio); 7586 return (CTL_RETVAL_COMPLETE); 7587 } 7588 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7589 break; 7590 case RSO_OPTIONS_OC_SA: 7591 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7592 service_action >= 32) { 7593 ctl_set_invalid_field(/*ctsio*/ ctsio, 7594 /*sks_valid*/ 1, 7595 /*command*/ 1, 7596 /*field*/ 2, 7597 /*bit_valid*/ 1, 7598 /*bit*/ 2); 7599 ctl_done((union ctl_io *)ctsio); 7600 return (CTL_RETVAL_COMPLETE); 7601 } 7602 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7603 break; 7604 default: 7605 ctl_set_invalid_field(/*ctsio*/ ctsio, 7606 /*sks_valid*/ 1, 7607 /*command*/ 1, 7608 /*field*/ 2, 7609 /*bit_valid*/ 1, 7610 /*bit*/ 2); 7611 ctl_done((union ctl_io *)ctsio); 7612 return (CTL_RETVAL_COMPLETE); 7613 } 7614 7615 alloc_len = scsi_4btoul(cdb->length); 7616 7617 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7618 7619 ctsio->kern_sg_entries = 0; 7620 7621 if (total_len < alloc_len) { 7622 ctsio->residual = alloc_len - total_len; 7623 ctsio->kern_data_len = total_len; 7624 ctsio->kern_total_len = total_len; 7625 } else { 7626 ctsio->residual = 0; 7627 ctsio->kern_data_len = alloc_len; 7628 ctsio->kern_total_len = alloc_len; 7629 } 7630 ctsio->kern_data_resid = 0; 7631 ctsio->kern_rel_offset = 0; 7632 7633 switch (cdb->options & RSO_OPTIONS_MASK) { 7634 case RSO_OPTIONS_ALL: 7635 all = (struct scsi_report_supported_opcodes_all *) 7636 ctsio->kern_data_ptr; 7637 num = 0; 7638 for (i = 0; i < 256; i++) { 7639 entry = &ctl_cmd_table[i]; 7640 if (entry->flags & CTL_CMD_FLAG_SA5) { 7641 for (j = 0; j < 32; j++) { 7642 sentry = &((const struct ctl_cmd_entry *) 7643 entry->execute)[j]; 7644 if (!ctl_cmd_applicable( 7645 lun->be_lun->lun_type, sentry)) 7646 continue; 7647 descr = &all->descr[num++]; 7648 descr->opcode = i; 7649 scsi_ulto2b(j, descr->service_action); 7650 descr->flags = RSO_SERVACTV; 7651 scsi_ulto2b(sentry->length, 7652 descr->cdb_length); 7653 } 7654 } else { 7655 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7656 entry)) 7657 continue; 7658 descr = &all->descr[num++]; 7659 descr->opcode = i; 7660 scsi_ulto2b(0, descr->service_action); 7661 descr->flags = 0; 7662 scsi_ulto2b(entry->length, descr->cdb_length); 7663 } 7664 } 7665 scsi_ulto4b( 7666 num * sizeof(struct scsi_report_supported_opcodes_descr), 7667 all->length); 7668 break; 7669 case RSO_OPTIONS_OC: 7670 one = (struct scsi_report_supported_opcodes_one *) 7671 ctsio->kern_data_ptr; 7672 entry = &ctl_cmd_table[opcode]; 7673 goto fill_one; 7674 case RSO_OPTIONS_OC_SA: 7675 one = (struct scsi_report_supported_opcodes_one *) 7676 ctsio->kern_data_ptr; 7677 entry = &ctl_cmd_table[opcode]; 7678 entry = &((const struct ctl_cmd_entry *) 7679 entry->execute)[service_action]; 7680 fill_one: 7681 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7682 one->support = 3; 7683 scsi_ulto2b(entry->length, one->cdb_length); 7684 one->cdb_usage[0] = opcode; 7685 memcpy(&one->cdb_usage[1], entry->usage, 7686 entry->length - 1); 7687 } else 7688 one->support = 1; 7689 break; 7690 } 7691 7692 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7693 ctsio->be_move_done = ctl_config_move_done; 7694 7695 ctl_datamove((union ctl_io *)ctsio); 7696 return(retval); 7697 } 7698 7699 int 7700 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7701 { 7702 struct ctl_lun *lun; 7703 struct scsi_report_supported_tmf *cdb; 7704 struct scsi_report_supported_tmf_data *data; 7705 int retval; 7706 int alloc_len, total_len; 7707 7708 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7709 7710 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7711 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7712 7713 retval = CTL_RETVAL_COMPLETE; 7714 7715 total_len = sizeof(struct scsi_report_supported_tmf_data); 7716 alloc_len = scsi_4btoul(cdb->length); 7717 7718 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7719 7720 ctsio->kern_sg_entries = 0; 7721 7722 if (total_len < alloc_len) { 7723 ctsio->residual = alloc_len - total_len; 7724 ctsio->kern_data_len = total_len; 7725 ctsio->kern_total_len = total_len; 7726 } else { 7727 ctsio->residual = 0; 7728 ctsio->kern_data_len = alloc_len; 7729 ctsio->kern_total_len = alloc_len; 7730 } 7731 ctsio->kern_data_resid = 0; 7732 ctsio->kern_rel_offset = 0; 7733 7734 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7735 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7736 data->byte2 |= RST_ITNRS; 7737 7738 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7739 ctsio->be_move_done = ctl_config_move_done; 7740 7741 ctl_datamove((union ctl_io *)ctsio); 7742 return (retval); 7743 } 7744 7745 int 7746 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7747 { 7748 struct ctl_lun *lun; 7749 struct scsi_report_timestamp *cdb; 7750 struct scsi_report_timestamp_data *data; 7751 struct timeval tv; 7752 int64_t timestamp; 7753 int retval; 7754 int alloc_len, total_len; 7755 7756 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7757 7758 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7759 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7760 7761 retval = CTL_RETVAL_COMPLETE; 7762 7763 total_len = sizeof(struct scsi_report_timestamp_data); 7764 alloc_len = scsi_4btoul(cdb->length); 7765 7766 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7767 7768 ctsio->kern_sg_entries = 0; 7769 7770 if (total_len < alloc_len) { 7771 ctsio->residual = alloc_len - total_len; 7772 ctsio->kern_data_len = total_len; 7773 ctsio->kern_total_len = total_len; 7774 } else { 7775 ctsio->residual = 0; 7776 ctsio->kern_data_len = alloc_len; 7777 ctsio->kern_total_len = alloc_len; 7778 } 7779 ctsio->kern_data_resid = 0; 7780 ctsio->kern_rel_offset = 0; 7781 7782 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7783 scsi_ulto2b(sizeof(*data) - 2, data->length); 7784 data->origin = RTS_ORIG_OUTSIDE; 7785 getmicrotime(&tv); 7786 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7787 scsi_ulto4b(timestamp >> 16, data->timestamp); 7788 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7789 7790 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7791 ctsio->be_move_done = ctl_config_move_done; 7792 7793 ctl_datamove((union ctl_io *)ctsio); 7794 return (retval); 7795 } 7796 7797 int 7798 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7799 { 7800 struct scsi_per_res_in *cdb; 7801 int alloc_len, total_len = 0; 7802 /* struct scsi_per_res_in_rsrv in_data; */ 7803 struct ctl_lun *lun; 7804 struct ctl_softc *softc; 7805 7806 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7807 7808 softc = control_softc; 7809 7810 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7811 7812 alloc_len = scsi_2btoul(cdb->length); 7813 7814 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7815 7816 retry: 7817 mtx_lock(&lun->lun_lock); 7818 switch (cdb->action) { 7819 case SPRI_RK: /* read keys */ 7820 total_len = sizeof(struct scsi_per_res_in_keys) + 7821 lun->pr_key_count * 7822 sizeof(struct scsi_per_res_key); 7823 break; 7824 case SPRI_RR: /* read reservation */ 7825 if (lun->flags & CTL_LUN_PR_RESERVED) 7826 total_len = sizeof(struct scsi_per_res_in_rsrv); 7827 else 7828 total_len = sizeof(struct scsi_per_res_in_header); 7829 break; 7830 case SPRI_RC: /* report capabilities */ 7831 total_len = sizeof(struct scsi_per_res_cap); 7832 break; 7833 case SPRI_RS: /* read full status */ 7834 total_len = sizeof(struct scsi_per_res_in_header) + 7835 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7836 lun->pr_key_count; 7837 break; 7838 default: 7839 panic("Invalid PR type %x", cdb->action); 7840 } 7841 mtx_unlock(&lun->lun_lock); 7842 7843 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7844 7845 if (total_len < alloc_len) { 7846 ctsio->residual = alloc_len - total_len; 7847 ctsio->kern_data_len = total_len; 7848 ctsio->kern_total_len = total_len; 7849 } else { 7850 ctsio->residual = 0; 7851 ctsio->kern_data_len = alloc_len; 7852 ctsio->kern_total_len = alloc_len; 7853 } 7854 7855 ctsio->kern_data_resid = 0; 7856 ctsio->kern_rel_offset = 0; 7857 ctsio->kern_sg_entries = 0; 7858 7859 mtx_lock(&lun->lun_lock); 7860 switch (cdb->action) { 7861 case SPRI_RK: { // read keys 7862 struct scsi_per_res_in_keys *res_keys; 7863 int i, key_count; 7864 7865 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7866 7867 /* 7868 * We had to drop the lock to allocate our buffer, which 7869 * leaves time for someone to come in with another 7870 * persistent reservation. (That is unlikely, though, 7871 * since this should be the only persistent reservation 7872 * command active right now.) 7873 */ 7874 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7875 (lun->pr_key_count * 7876 sizeof(struct scsi_per_res_key)))){ 7877 mtx_unlock(&lun->lun_lock); 7878 free(ctsio->kern_data_ptr, M_CTL); 7879 printf("%s: reservation length changed, retrying\n", 7880 __func__); 7881 goto retry; 7882 } 7883 7884 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7885 7886 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7887 lun->pr_key_count, res_keys->header.length); 7888 7889 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7890 if (lun->pr_keys[i] == 0) 7891 continue; 7892 7893 /* 7894 * We used lun->pr_key_count to calculate the 7895 * size to allocate. If it turns out the number of 7896 * initiators with the registered flag set is 7897 * larger than that (i.e. they haven't been kept in 7898 * sync), we've got a problem. 7899 */ 7900 if (key_count >= lun->pr_key_count) { 7901 #ifdef NEEDTOPORT 7902 csevent_log(CSC_CTL | CSC_SHELF_SW | 7903 CTL_PR_ERROR, 7904 csevent_LogType_Fault, 7905 csevent_AlertLevel_Yellow, 7906 csevent_FRU_ShelfController, 7907 csevent_FRU_Firmware, 7908 csevent_FRU_Unknown, 7909 "registered keys %d >= key " 7910 "count %d", key_count, 7911 lun->pr_key_count); 7912 #endif 7913 key_count++; 7914 continue; 7915 } 7916 scsi_u64to8b(lun->pr_keys[i], 7917 res_keys->keys[key_count].key); 7918 key_count++; 7919 } 7920 break; 7921 } 7922 case SPRI_RR: { // read reservation 7923 struct scsi_per_res_in_rsrv *res; 7924 int tmp_len, header_only; 7925 7926 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7927 7928 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7929 7930 if (lun->flags & CTL_LUN_PR_RESERVED) 7931 { 7932 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7933 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7934 res->header.length); 7935 header_only = 0; 7936 } else { 7937 tmp_len = sizeof(struct scsi_per_res_in_header); 7938 scsi_ulto4b(0, res->header.length); 7939 header_only = 1; 7940 } 7941 7942 /* 7943 * We had to drop the lock to allocate our buffer, which 7944 * leaves time for someone to come in with another 7945 * persistent reservation. (That is unlikely, though, 7946 * since this should be the only persistent reservation 7947 * command active right now.) 7948 */ 7949 if (tmp_len != total_len) { 7950 mtx_unlock(&lun->lun_lock); 7951 free(ctsio->kern_data_ptr, M_CTL); 7952 printf("%s: reservation status changed, retrying\n", 7953 __func__); 7954 goto retry; 7955 } 7956 7957 /* 7958 * No reservation held, so we're done. 7959 */ 7960 if (header_only != 0) 7961 break; 7962 7963 /* 7964 * If the registration is an All Registrants type, the key 7965 * is 0, since it doesn't really matter. 7966 */ 7967 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7968 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 7969 res->data.reservation); 7970 } 7971 res->data.scopetype = lun->res_type; 7972 break; 7973 } 7974 case SPRI_RC: //report capabilities 7975 { 7976 struct scsi_per_res_cap *res_cap; 7977 uint16_t type_mask; 7978 7979 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7980 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7981 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7982 type_mask = SPRI_TM_WR_EX_AR | 7983 SPRI_TM_EX_AC_RO | 7984 SPRI_TM_WR_EX_RO | 7985 SPRI_TM_EX_AC | 7986 SPRI_TM_WR_EX | 7987 SPRI_TM_EX_AC_AR; 7988 scsi_ulto2b(type_mask, res_cap->type_mask); 7989 break; 7990 } 7991 case SPRI_RS: { // read full status 7992 struct scsi_per_res_in_full *res_status; 7993 struct scsi_per_res_in_full_desc *res_desc; 7994 struct ctl_port *port; 7995 int i, len; 7996 7997 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7998 7999 /* 8000 * We had to drop the lock to allocate our buffer, which 8001 * leaves time for someone to come in with another 8002 * persistent reservation. (That is unlikely, though, 8003 * since this should be the only persistent reservation 8004 * command active right now.) 8005 */ 8006 if (total_len < (sizeof(struct scsi_per_res_in_header) + 8007 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 8008 lun->pr_key_count)){ 8009 mtx_unlock(&lun->lun_lock); 8010 free(ctsio->kern_data_ptr, M_CTL); 8011 printf("%s: reservation length changed, retrying\n", 8012 __func__); 8013 goto retry; 8014 } 8015 8016 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 8017 8018 res_desc = &res_status->desc[0]; 8019 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 8020 if (lun->pr_keys[i] == 0) 8021 continue; 8022 8023 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 8024 if ((lun->flags & CTL_LUN_PR_RESERVED) && 8025 (lun->pr_res_idx == i || 8026 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 8027 res_desc->flags = SPRI_FULL_R_HOLDER; 8028 res_desc->scopetype = lun->res_type; 8029 } 8030 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 8031 res_desc->rel_trgt_port_id); 8032 len = 0; 8033 port = softc->ctl_ports[ 8034 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 8035 if (port != NULL) 8036 len = ctl_create_iid(port, 8037 i % CTL_MAX_INIT_PER_PORT, 8038 res_desc->transport_id); 8039 scsi_ulto4b(len, res_desc->additional_length); 8040 res_desc = (struct scsi_per_res_in_full_desc *) 8041 &res_desc->transport_id[len]; 8042 } 8043 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 8044 res_status->header.length); 8045 break; 8046 } 8047 default: 8048 /* 8049 * This is a bug, because we just checked for this above, 8050 * and should have returned an error. 8051 */ 8052 panic("Invalid PR type %x", cdb->action); 8053 break; /* NOTREACHED */ 8054 } 8055 mtx_unlock(&lun->lun_lock); 8056 8057 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8058 ctsio->be_move_done = ctl_config_move_done; 8059 8060 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 8061 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 8062 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 8063 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 8064 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 8065 8066 ctl_datamove((union ctl_io *)ctsio); 8067 8068 return (CTL_RETVAL_COMPLETE); 8069 } 8070 8071 /* 8072 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8073 * it should return. 8074 */ 8075 static int 8076 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8077 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8078 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8079 struct scsi_per_res_out_parms* param) 8080 { 8081 union ctl_ha_msg persis_io; 8082 int retval, i; 8083 int isc_retval; 8084 8085 retval = 0; 8086 8087 mtx_lock(&lun->lun_lock); 8088 if (sa_res_key == 0) { 8089 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8090 /* validate scope and type */ 8091 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8092 SPR_LU_SCOPE) { 8093 mtx_unlock(&lun->lun_lock); 8094 ctl_set_invalid_field(/*ctsio*/ ctsio, 8095 /*sks_valid*/ 1, 8096 /*command*/ 1, 8097 /*field*/ 2, 8098 /*bit_valid*/ 1, 8099 /*bit*/ 4); 8100 ctl_done((union ctl_io *)ctsio); 8101 return (1); 8102 } 8103 8104 if (type>8 || type==2 || type==4 || type==0) { 8105 mtx_unlock(&lun->lun_lock); 8106 ctl_set_invalid_field(/*ctsio*/ ctsio, 8107 /*sks_valid*/ 1, 8108 /*command*/ 1, 8109 /*field*/ 2, 8110 /*bit_valid*/ 1, 8111 /*bit*/ 0); 8112 ctl_done((union ctl_io *)ctsio); 8113 return (1); 8114 } 8115 8116 /* 8117 * Unregister everybody else and build UA for 8118 * them 8119 */ 8120 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8121 if (i == residx || lun->pr_keys[i] == 0) 8122 continue; 8123 8124 if (!persis_offset 8125 && i <CTL_MAX_INITIATORS) 8126 lun->pending_ua[i] |= 8127 CTL_UA_REG_PREEMPT; 8128 else if (persis_offset 8129 && i >= persis_offset) 8130 lun->pending_ua[i-persis_offset] |= 8131 CTL_UA_REG_PREEMPT; 8132 lun->pr_keys[i] = 0; 8133 } 8134 lun->pr_key_count = 1; 8135 lun->res_type = type; 8136 if (lun->res_type != SPR_TYPE_WR_EX_AR 8137 && lun->res_type != SPR_TYPE_EX_AC_AR) 8138 lun->pr_res_idx = residx; 8139 8140 /* send msg to other side */ 8141 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8142 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8143 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8144 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8145 persis_io.pr.pr_info.res_type = type; 8146 memcpy(persis_io.pr.pr_info.sa_res_key, 8147 param->serv_act_res_key, 8148 sizeof(param->serv_act_res_key)); 8149 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8150 &persis_io, sizeof(persis_io), 0)) > 8151 CTL_HA_STATUS_SUCCESS) { 8152 printf("CTL:Persis Out error returned " 8153 "from ctl_ha_msg_send %d\n", 8154 isc_retval); 8155 } 8156 } else { 8157 /* not all registrants */ 8158 mtx_unlock(&lun->lun_lock); 8159 free(ctsio->kern_data_ptr, M_CTL); 8160 ctl_set_invalid_field(ctsio, 8161 /*sks_valid*/ 1, 8162 /*command*/ 0, 8163 /*field*/ 8, 8164 /*bit_valid*/ 0, 8165 /*bit*/ 0); 8166 ctl_done((union ctl_io *)ctsio); 8167 return (1); 8168 } 8169 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8170 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8171 int found = 0; 8172 8173 if (res_key == sa_res_key) { 8174 /* special case */ 8175 /* 8176 * The spec implies this is not good but doesn't 8177 * say what to do. There are two choices either 8178 * generate a res conflict or check condition 8179 * with illegal field in parameter data. Since 8180 * that is what is done when the sa_res_key is 8181 * zero I'll take that approach since this has 8182 * to do with the sa_res_key. 8183 */ 8184 mtx_unlock(&lun->lun_lock); 8185 free(ctsio->kern_data_ptr, M_CTL); 8186 ctl_set_invalid_field(ctsio, 8187 /*sks_valid*/ 1, 8188 /*command*/ 0, 8189 /*field*/ 8, 8190 /*bit_valid*/ 0, 8191 /*bit*/ 0); 8192 ctl_done((union ctl_io *)ctsio); 8193 return (1); 8194 } 8195 8196 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8197 if (lun->pr_keys[i] != sa_res_key) 8198 continue; 8199 8200 found = 1; 8201 lun->pr_keys[i] = 0; 8202 lun->pr_key_count--; 8203 8204 if (!persis_offset && i < CTL_MAX_INITIATORS) 8205 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8206 else if (persis_offset && i >= persis_offset) 8207 lun->pending_ua[i-persis_offset] |= 8208 CTL_UA_REG_PREEMPT; 8209 } 8210 if (!found) { 8211 mtx_unlock(&lun->lun_lock); 8212 free(ctsio->kern_data_ptr, M_CTL); 8213 ctl_set_reservation_conflict(ctsio); 8214 ctl_done((union ctl_io *)ctsio); 8215 return (CTL_RETVAL_COMPLETE); 8216 } 8217 /* send msg to other side */ 8218 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8219 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8220 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8221 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8222 persis_io.pr.pr_info.res_type = type; 8223 memcpy(persis_io.pr.pr_info.sa_res_key, 8224 param->serv_act_res_key, 8225 sizeof(param->serv_act_res_key)); 8226 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8227 &persis_io, sizeof(persis_io), 0)) > 8228 CTL_HA_STATUS_SUCCESS) { 8229 printf("CTL:Persis Out error returned from " 8230 "ctl_ha_msg_send %d\n", isc_retval); 8231 } 8232 } else { 8233 /* Reserved but not all registrants */ 8234 /* sa_res_key is res holder */ 8235 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8236 /* validate scope and type */ 8237 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8238 SPR_LU_SCOPE) { 8239 mtx_unlock(&lun->lun_lock); 8240 ctl_set_invalid_field(/*ctsio*/ ctsio, 8241 /*sks_valid*/ 1, 8242 /*command*/ 1, 8243 /*field*/ 2, 8244 /*bit_valid*/ 1, 8245 /*bit*/ 4); 8246 ctl_done((union ctl_io *)ctsio); 8247 return (1); 8248 } 8249 8250 if (type>8 || type==2 || type==4 || type==0) { 8251 mtx_unlock(&lun->lun_lock); 8252 ctl_set_invalid_field(/*ctsio*/ ctsio, 8253 /*sks_valid*/ 1, 8254 /*command*/ 1, 8255 /*field*/ 2, 8256 /*bit_valid*/ 1, 8257 /*bit*/ 0); 8258 ctl_done((union ctl_io *)ctsio); 8259 return (1); 8260 } 8261 8262 /* 8263 * Do the following: 8264 * if sa_res_key != res_key remove all 8265 * registrants w/sa_res_key and generate UA 8266 * for these registrants(Registrations 8267 * Preempted) if it wasn't an exclusive 8268 * reservation generate UA(Reservations 8269 * Preempted) for all other registered nexuses 8270 * if the type has changed. Establish the new 8271 * reservation and holder. If res_key and 8272 * sa_res_key are the same do the above 8273 * except don't unregister the res holder. 8274 */ 8275 8276 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8277 if (i == residx || lun->pr_keys[i] == 0) 8278 continue; 8279 8280 if (sa_res_key == lun->pr_keys[i]) { 8281 lun->pr_keys[i] = 0; 8282 lun->pr_key_count--; 8283 8284 if (!persis_offset 8285 && i < CTL_MAX_INITIATORS) 8286 lun->pending_ua[i] |= 8287 CTL_UA_REG_PREEMPT; 8288 else if (persis_offset 8289 && i >= persis_offset) 8290 lun->pending_ua[i-persis_offset] |= 8291 CTL_UA_REG_PREEMPT; 8292 } else if (type != lun->res_type 8293 && (lun->res_type == SPR_TYPE_WR_EX_RO 8294 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8295 if (!persis_offset 8296 && i < CTL_MAX_INITIATORS) 8297 lun->pending_ua[i] |= 8298 CTL_UA_RES_RELEASE; 8299 else if (persis_offset 8300 && i >= persis_offset) 8301 lun->pending_ua[ 8302 i-persis_offset] |= 8303 CTL_UA_RES_RELEASE; 8304 } 8305 } 8306 lun->res_type = type; 8307 if (lun->res_type != SPR_TYPE_WR_EX_AR 8308 && lun->res_type != SPR_TYPE_EX_AC_AR) 8309 lun->pr_res_idx = residx; 8310 else 8311 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8312 8313 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8314 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8315 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8316 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8317 persis_io.pr.pr_info.res_type = type; 8318 memcpy(persis_io.pr.pr_info.sa_res_key, 8319 param->serv_act_res_key, 8320 sizeof(param->serv_act_res_key)); 8321 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8322 &persis_io, sizeof(persis_io), 0)) > 8323 CTL_HA_STATUS_SUCCESS) { 8324 printf("CTL:Persis Out error returned " 8325 "from ctl_ha_msg_send %d\n", 8326 isc_retval); 8327 } 8328 } else { 8329 /* 8330 * sa_res_key is not the res holder just 8331 * remove registrants 8332 */ 8333 int found=0; 8334 8335 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8336 if (sa_res_key != lun->pr_keys[i]) 8337 continue; 8338 8339 found = 1; 8340 lun->pr_keys[i] = 0; 8341 lun->pr_key_count--; 8342 8343 if (!persis_offset 8344 && i < CTL_MAX_INITIATORS) 8345 lun->pending_ua[i] |= 8346 CTL_UA_REG_PREEMPT; 8347 else if (persis_offset 8348 && i >= persis_offset) 8349 lun->pending_ua[i-persis_offset] |= 8350 CTL_UA_REG_PREEMPT; 8351 } 8352 8353 if (!found) { 8354 mtx_unlock(&lun->lun_lock); 8355 free(ctsio->kern_data_ptr, M_CTL); 8356 ctl_set_reservation_conflict(ctsio); 8357 ctl_done((union ctl_io *)ctsio); 8358 return (1); 8359 } 8360 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8361 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8362 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8363 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8364 persis_io.pr.pr_info.res_type = type; 8365 memcpy(persis_io.pr.pr_info.sa_res_key, 8366 param->serv_act_res_key, 8367 sizeof(param->serv_act_res_key)); 8368 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8369 &persis_io, sizeof(persis_io), 0)) > 8370 CTL_HA_STATUS_SUCCESS) { 8371 printf("CTL:Persis Out error returned " 8372 "from ctl_ha_msg_send %d\n", 8373 isc_retval); 8374 } 8375 } 8376 } 8377 8378 lun->PRGeneration++; 8379 mtx_unlock(&lun->lun_lock); 8380 8381 return (retval); 8382 } 8383 8384 static void 8385 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8386 { 8387 uint64_t sa_res_key; 8388 int i; 8389 8390 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8391 8392 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8393 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8394 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8395 if (sa_res_key == 0) { 8396 /* 8397 * Unregister everybody else and build UA for 8398 * them 8399 */ 8400 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8401 if (i == msg->pr.pr_info.residx || 8402 lun->pr_keys[i] == 0) 8403 continue; 8404 8405 if (!persis_offset 8406 && i < CTL_MAX_INITIATORS) 8407 lun->pending_ua[i] |= 8408 CTL_UA_REG_PREEMPT; 8409 else if (persis_offset && i >= persis_offset) 8410 lun->pending_ua[i - persis_offset] |= 8411 CTL_UA_REG_PREEMPT; 8412 lun->pr_keys[i] = 0; 8413 } 8414 8415 lun->pr_key_count = 1; 8416 lun->res_type = msg->pr.pr_info.res_type; 8417 if (lun->res_type != SPR_TYPE_WR_EX_AR 8418 && lun->res_type != SPR_TYPE_EX_AC_AR) 8419 lun->pr_res_idx = msg->pr.pr_info.residx; 8420 } else { 8421 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8422 if (sa_res_key == lun->pr_keys[i]) 8423 continue; 8424 8425 lun->pr_keys[i] = 0; 8426 lun->pr_key_count--; 8427 8428 if (!persis_offset 8429 && i < persis_offset) 8430 lun->pending_ua[i] |= 8431 CTL_UA_REG_PREEMPT; 8432 else if (persis_offset 8433 && i >= persis_offset) 8434 lun->pending_ua[i - persis_offset] |= 8435 CTL_UA_REG_PREEMPT; 8436 } 8437 } 8438 } else { 8439 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8440 if (i == msg->pr.pr_info.residx || 8441 lun->pr_keys[i] == 0) 8442 continue; 8443 8444 if (sa_res_key == lun->pr_keys[i]) { 8445 lun->pr_keys[i] = 0; 8446 lun->pr_key_count--; 8447 if (!persis_offset 8448 && i < CTL_MAX_INITIATORS) 8449 lun->pending_ua[i] |= 8450 CTL_UA_REG_PREEMPT; 8451 else if (persis_offset 8452 && i >= persis_offset) 8453 lun->pending_ua[i - persis_offset] |= 8454 CTL_UA_REG_PREEMPT; 8455 } else if (msg->pr.pr_info.res_type != lun->res_type 8456 && (lun->res_type == SPR_TYPE_WR_EX_RO 8457 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8458 if (!persis_offset 8459 && i < persis_offset) 8460 lun->pending_ua[i] |= 8461 CTL_UA_RES_RELEASE; 8462 else if (persis_offset 8463 && i >= persis_offset) 8464 lun->pending_ua[i - persis_offset] |= 8465 CTL_UA_RES_RELEASE; 8466 } 8467 } 8468 lun->res_type = msg->pr.pr_info.res_type; 8469 if (lun->res_type != SPR_TYPE_WR_EX_AR 8470 && lun->res_type != SPR_TYPE_EX_AC_AR) 8471 lun->pr_res_idx = msg->pr.pr_info.residx; 8472 else 8473 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8474 } 8475 lun->PRGeneration++; 8476 8477 } 8478 8479 8480 int 8481 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8482 { 8483 int retval; 8484 int isc_retval; 8485 u_int32_t param_len; 8486 struct scsi_per_res_out *cdb; 8487 struct ctl_lun *lun; 8488 struct scsi_per_res_out_parms* param; 8489 struct ctl_softc *softc; 8490 uint32_t residx; 8491 uint64_t res_key, sa_res_key; 8492 uint8_t type; 8493 union ctl_ha_msg persis_io; 8494 int i; 8495 8496 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8497 8498 retval = CTL_RETVAL_COMPLETE; 8499 8500 softc = control_softc; 8501 8502 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8503 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8504 8505 /* 8506 * We only support whole-LUN scope. The scope & type are ignored for 8507 * register, register and ignore existing key and clear. 8508 * We sometimes ignore scope and type on preempts too!! 8509 * Verify reservation type here as well. 8510 */ 8511 type = cdb->scope_type & SPR_TYPE_MASK; 8512 if ((cdb->action == SPRO_RESERVE) 8513 || (cdb->action == SPRO_RELEASE)) { 8514 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8515 ctl_set_invalid_field(/*ctsio*/ ctsio, 8516 /*sks_valid*/ 1, 8517 /*command*/ 1, 8518 /*field*/ 2, 8519 /*bit_valid*/ 1, 8520 /*bit*/ 4); 8521 ctl_done((union ctl_io *)ctsio); 8522 return (CTL_RETVAL_COMPLETE); 8523 } 8524 8525 if (type>8 || type==2 || type==4 || type==0) { 8526 ctl_set_invalid_field(/*ctsio*/ ctsio, 8527 /*sks_valid*/ 1, 8528 /*command*/ 1, 8529 /*field*/ 2, 8530 /*bit_valid*/ 1, 8531 /*bit*/ 0); 8532 ctl_done((union ctl_io *)ctsio); 8533 return (CTL_RETVAL_COMPLETE); 8534 } 8535 } 8536 8537 param_len = scsi_4btoul(cdb->length); 8538 8539 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8540 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8541 ctsio->kern_data_len = param_len; 8542 ctsio->kern_total_len = param_len; 8543 ctsio->kern_data_resid = 0; 8544 ctsio->kern_rel_offset = 0; 8545 ctsio->kern_sg_entries = 0; 8546 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8547 ctsio->be_move_done = ctl_config_move_done; 8548 ctl_datamove((union ctl_io *)ctsio); 8549 8550 return (CTL_RETVAL_COMPLETE); 8551 } 8552 8553 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8554 8555 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8556 res_key = scsi_8btou64(param->res_key.key); 8557 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8558 8559 /* 8560 * Validate the reservation key here except for SPRO_REG_IGNO 8561 * This must be done for all other service actions 8562 */ 8563 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8564 mtx_lock(&lun->lun_lock); 8565 if (lun->pr_keys[residx] != 0) { 8566 if (res_key != lun->pr_keys[residx]) { 8567 /* 8568 * The current key passed in doesn't match 8569 * the one the initiator previously 8570 * registered. 8571 */ 8572 mtx_unlock(&lun->lun_lock); 8573 free(ctsio->kern_data_ptr, M_CTL); 8574 ctl_set_reservation_conflict(ctsio); 8575 ctl_done((union ctl_io *)ctsio); 8576 return (CTL_RETVAL_COMPLETE); 8577 } 8578 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8579 /* 8580 * We are not registered 8581 */ 8582 mtx_unlock(&lun->lun_lock); 8583 free(ctsio->kern_data_ptr, M_CTL); 8584 ctl_set_reservation_conflict(ctsio); 8585 ctl_done((union ctl_io *)ctsio); 8586 return (CTL_RETVAL_COMPLETE); 8587 } else if (res_key != 0) { 8588 /* 8589 * We are not registered and trying to register but 8590 * the register key isn't zero. 8591 */ 8592 mtx_unlock(&lun->lun_lock); 8593 free(ctsio->kern_data_ptr, M_CTL); 8594 ctl_set_reservation_conflict(ctsio); 8595 ctl_done((union ctl_io *)ctsio); 8596 return (CTL_RETVAL_COMPLETE); 8597 } 8598 mtx_unlock(&lun->lun_lock); 8599 } 8600 8601 switch (cdb->action & SPRO_ACTION_MASK) { 8602 case SPRO_REGISTER: 8603 case SPRO_REG_IGNO: { 8604 8605 #if 0 8606 printf("Registration received\n"); 8607 #endif 8608 8609 /* 8610 * We don't support any of these options, as we report in 8611 * the read capabilities request (see 8612 * ctl_persistent_reserve_in(), above). 8613 */ 8614 if ((param->flags & SPR_SPEC_I_PT) 8615 || (param->flags & SPR_ALL_TG_PT) 8616 || (param->flags & SPR_APTPL)) { 8617 int bit_ptr; 8618 8619 if (param->flags & SPR_APTPL) 8620 bit_ptr = 0; 8621 else if (param->flags & SPR_ALL_TG_PT) 8622 bit_ptr = 2; 8623 else /* SPR_SPEC_I_PT */ 8624 bit_ptr = 3; 8625 8626 free(ctsio->kern_data_ptr, M_CTL); 8627 ctl_set_invalid_field(ctsio, 8628 /*sks_valid*/ 1, 8629 /*command*/ 0, 8630 /*field*/ 20, 8631 /*bit_valid*/ 1, 8632 /*bit*/ bit_ptr); 8633 ctl_done((union ctl_io *)ctsio); 8634 return (CTL_RETVAL_COMPLETE); 8635 } 8636 8637 mtx_lock(&lun->lun_lock); 8638 8639 /* 8640 * The initiator wants to clear the 8641 * key/unregister. 8642 */ 8643 if (sa_res_key == 0) { 8644 if ((res_key == 0 8645 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8646 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8647 && lun->pr_keys[residx] == 0)) { 8648 mtx_unlock(&lun->lun_lock); 8649 goto done; 8650 } 8651 8652 lun->pr_keys[residx] = 0; 8653 lun->pr_key_count--; 8654 8655 if (residx == lun->pr_res_idx) { 8656 lun->flags &= ~CTL_LUN_PR_RESERVED; 8657 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8658 8659 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8660 || lun->res_type == SPR_TYPE_EX_AC_RO) 8661 && lun->pr_key_count) { 8662 /* 8663 * If the reservation is a registrants 8664 * only type we need to generate a UA 8665 * for other registered inits. The 8666 * sense code should be RESERVATIONS 8667 * RELEASED 8668 */ 8669 8670 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8671 if (lun->pr_keys[ 8672 i + persis_offset] == 0) 8673 continue; 8674 lun->pending_ua[i] |= 8675 CTL_UA_RES_RELEASE; 8676 } 8677 } 8678 lun->res_type = 0; 8679 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8680 if (lun->pr_key_count==0) { 8681 lun->flags &= ~CTL_LUN_PR_RESERVED; 8682 lun->res_type = 0; 8683 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8684 } 8685 } 8686 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8687 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8688 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8689 persis_io.pr.pr_info.residx = residx; 8690 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8691 &persis_io, sizeof(persis_io), 0 )) > 8692 CTL_HA_STATUS_SUCCESS) { 8693 printf("CTL:Persis Out error returned from " 8694 "ctl_ha_msg_send %d\n", isc_retval); 8695 } 8696 } else /* sa_res_key != 0 */ { 8697 8698 /* 8699 * If we aren't registered currently then increment 8700 * the key count and set the registered flag. 8701 */ 8702 if (lun->pr_keys[residx] == 0) 8703 lun->pr_key_count++; 8704 lun->pr_keys[residx] = sa_res_key; 8705 8706 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8707 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8708 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8709 persis_io.pr.pr_info.residx = residx; 8710 memcpy(persis_io.pr.pr_info.sa_res_key, 8711 param->serv_act_res_key, 8712 sizeof(param->serv_act_res_key)); 8713 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8714 &persis_io, sizeof(persis_io), 0)) > 8715 CTL_HA_STATUS_SUCCESS) { 8716 printf("CTL:Persis Out error returned from " 8717 "ctl_ha_msg_send %d\n", isc_retval); 8718 } 8719 } 8720 lun->PRGeneration++; 8721 mtx_unlock(&lun->lun_lock); 8722 8723 break; 8724 } 8725 case SPRO_RESERVE: 8726 #if 0 8727 printf("Reserve executed type %d\n", type); 8728 #endif 8729 mtx_lock(&lun->lun_lock); 8730 if (lun->flags & CTL_LUN_PR_RESERVED) { 8731 /* 8732 * if this isn't the reservation holder and it's 8733 * not a "all registrants" type or if the type is 8734 * different then we have a conflict 8735 */ 8736 if ((lun->pr_res_idx != residx 8737 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8738 || lun->res_type != type) { 8739 mtx_unlock(&lun->lun_lock); 8740 free(ctsio->kern_data_ptr, M_CTL); 8741 ctl_set_reservation_conflict(ctsio); 8742 ctl_done((union ctl_io *)ctsio); 8743 return (CTL_RETVAL_COMPLETE); 8744 } 8745 mtx_unlock(&lun->lun_lock); 8746 } else /* create a reservation */ { 8747 /* 8748 * If it's not an "all registrants" type record 8749 * reservation holder 8750 */ 8751 if (type != SPR_TYPE_WR_EX_AR 8752 && type != SPR_TYPE_EX_AC_AR) 8753 lun->pr_res_idx = residx; /* Res holder */ 8754 else 8755 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8756 8757 lun->flags |= CTL_LUN_PR_RESERVED; 8758 lun->res_type = type; 8759 8760 mtx_unlock(&lun->lun_lock); 8761 8762 /* send msg to other side */ 8763 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8764 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8765 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8766 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8767 persis_io.pr.pr_info.res_type = type; 8768 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8769 &persis_io, sizeof(persis_io), 0)) > 8770 CTL_HA_STATUS_SUCCESS) { 8771 printf("CTL:Persis Out error returned from " 8772 "ctl_ha_msg_send %d\n", isc_retval); 8773 } 8774 } 8775 break; 8776 8777 case SPRO_RELEASE: 8778 mtx_lock(&lun->lun_lock); 8779 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8780 /* No reservation exists return good status */ 8781 mtx_unlock(&lun->lun_lock); 8782 goto done; 8783 } 8784 /* 8785 * Is this nexus a reservation holder? 8786 */ 8787 if (lun->pr_res_idx != residx 8788 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8789 /* 8790 * not a res holder return good status but 8791 * do nothing 8792 */ 8793 mtx_unlock(&lun->lun_lock); 8794 goto done; 8795 } 8796 8797 if (lun->res_type != type) { 8798 mtx_unlock(&lun->lun_lock); 8799 free(ctsio->kern_data_ptr, M_CTL); 8800 ctl_set_illegal_pr_release(ctsio); 8801 ctl_done((union ctl_io *)ctsio); 8802 return (CTL_RETVAL_COMPLETE); 8803 } 8804 8805 /* okay to release */ 8806 lun->flags &= ~CTL_LUN_PR_RESERVED; 8807 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8808 lun->res_type = 0; 8809 8810 /* 8811 * if this isn't an exclusive access 8812 * res generate UA for all other 8813 * registrants. 8814 */ 8815 if (type != SPR_TYPE_EX_AC 8816 && type != SPR_TYPE_WR_EX) { 8817 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8818 if (i == residx || 8819 lun->pr_keys[i + persis_offset] == 0) 8820 continue; 8821 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8822 } 8823 } 8824 mtx_unlock(&lun->lun_lock); 8825 /* Send msg to other side */ 8826 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8827 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8828 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8829 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8830 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8831 printf("CTL:Persis Out error returned from " 8832 "ctl_ha_msg_send %d\n", isc_retval); 8833 } 8834 break; 8835 8836 case SPRO_CLEAR: 8837 /* send msg to other side */ 8838 8839 mtx_lock(&lun->lun_lock); 8840 lun->flags &= ~CTL_LUN_PR_RESERVED; 8841 lun->res_type = 0; 8842 lun->pr_key_count = 0; 8843 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8844 8845 lun->pr_keys[residx] = 0; 8846 8847 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8848 if (lun->pr_keys[i] != 0) { 8849 if (!persis_offset && i < CTL_MAX_INITIATORS) 8850 lun->pending_ua[i] |= 8851 CTL_UA_RES_PREEMPT; 8852 else if (persis_offset && i >= persis_offset) 8853 lun->pending_ua[i-persis_offset] |= 8854 CTL_UA_RES_PREEMPT; 8855 8856 lun->pr_keys[i] = 0; 8857 } 8858 lun->PRGeneration++; 8859 mtx_unlock(&lun->lun_lock); 8860 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8861 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8862 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8863 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8864 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8865 printf("CTL:Persis Out error returned from " 8866 "ctl_ha_msg_send %d\n", isc_retval); 8867 } 8868 break; 8869 8870 case SPRO_PREEMPT: { 8871 int nretval; 8872 8873 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8874 residx, ctsio, cdb, param); 8875 if (nretval != 0) 8876 return (CTL_RETVAL_COMPLETE); 8877 break; 8878 } 8879 default: 8880 panic("Invalid PR type %x", cdb->action); 8881 } 8882 8883 done: 8884 free(ctsio->kern_data_ptr, M_CTL); 8885 ctl_set_success(ctsio); 8886 ctl_done((union ctl_io *)ctsio); 8887 8888 return (retval); 8889 } 8890 8891 /* 8892 * This routine is for handling a message from the other SC pertaining to 8893 * persistent reserve out. All the error checking will have been done 8894 * so only perorming the action need be done here to keep the two 8895 * in sync. 8896 */ 8897 static void 8898 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8899 { 8900 struct ctl_lun *lun; 8901 struct ctl_softc *softc; 8902 int i; 8903 uint32_t targ_lun; 8904 8905 softc = control_softc; 8906 8907 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8908 lun = softc->ctl_luns[targ_lun]; 8909 mtx_lock(&lun->lun_lock); 8910 switch(msg->pr.pr_info.action) { 8911 case CTL_PR_REG_KEY: 8912 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8913 lun->pr_key_count++; 8914 lun->pr_keys[msg->pr.pr_info.residx] = 8915 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8916 lun->PRGeneration++; 8917 break; 8918 8919 case CTL_PR_UNREG_KEY: 8920 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8921 lun->pr_key_count--; 8922 8923 /* XXX Need to see if the reservation has been released */ 8924 /* if so do we need to generate UA? */ 8925 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8926 lun->flags &= ~CTL_LUN_PR_RESERVED; 8927 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8928 8929 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8930 || lun->res_type == SPR_TYPE_EX_AC_RO) 8931 && lun->pr_key_count) { 8932 /* 8933 * If the reservation is a registrants 8934 * only type we need to generate a UA 8935 * for other registered inits. The 8936 * sense code should be RESERVATIONS 8937 * RELEASED 8938 */ 8939 8940 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8941 if (lun->pr_keys[i+ 8942 persis_offset] == 0) 8943 continue; 8944 8945 lun->pending_ua[i] |= 8946 CTL_UA_RES_RELEASE; 8947 } 8948 } 8949 lun->res_type = 0; 8950 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8951 if (lun->pr_key_count==0) { 8952 lun->flags &= ~CTL_LUN_PR_RESERVED; 8953 lun->res_type = 0; 8954 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8955 } 8956 } 8957 lun->PRGeneration++; 8958 break; 8959 8960 case CTL_PR_RESERVE: 8961 lun->flags |= CTL_LUN_PR_RESERVED; 8962 lun->res_type = msg->pr.pr_info.res_type; 8963 lun->pr_res_idx = msg->pr.pr_info.residx; 8964 8965 break; 8966 8967 case CTL_PR_RELEASE: 8968 /* 8969 * if this isn't an exclusive access res generate UA for all 8970 * other registrants. 8971 */ 8972 if (lun->res_type != SPR_TYPE_EX_AC 8973 && lun->res_type != SPR_TYPE_WR_EX) { 8974 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8975 if (lun->pr_keys[i+persis_offset] != 0) 8976 lun->pending_ua[i] |= 8977 CTL_UA_RES_RELEASE; 8978 } 8979 8980 lun->flags &= ~CTL_LUN_PR_RESERVED; 8981 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8982 lun->res_type = 0; 8983 break; 8984 8985 case CTL_PR_PREEMPT: 8986 ctl_pro_preempt_other(lun, msg); 8987 break; 8988 case CTL_PR_CLEAR: 8989 lun->flags &= ~CTL_LUN_PR_RESERVED; 8990 lun->res_type = 0; 8991 lun->pr_key_count = 0; 8992 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8993 8994 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8995 if (lun->pr_keys[i] == 0) 8996 continue; 8997 if (!persis_offset 8998 && i < CTL_MAX_INITIATORS) 8999 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 9000 else if (persis_offset 9001 && i >= persis_offset) 9002 lun->pending_ua[i-persis_offset] |= 9003 CTL_UA_RES_PREEMPT; 9004 lun->pr_keys[i] = 0; 9005 } 9006 lun->PRGeneration++; 9007 break; 9008 } 9009 9010 mtx_unlock(&lun->lun_lock); 9011 } 9012 9013 int 9014 ctl_read_write(struct ctl_scsiio *ctsio) 9015 { 9016 struct ctl_lun *lun; 9017 struct ctl_lba_len_flags *lbalen; 9018 uint64_t lba; 9019 uint32_t num_blocks; 9020 int flags, retval; 9021 int isread; 9022 9023 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9024 9025 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9026 9027 flags = 0; 9028 retval = CTL_RETVAL_COMPLETE; 9029 9030 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9031 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9032 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9033 uint32_t residx; 9034 9035 /* 9036 * XXX KDM need a lock here. 9037 */ 9038 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9039 if ((lun->res_type == SPR_TYPE_EX_AC 9040 && residx != lun->pr_res_idx) 9041 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9042 || lun->res_type == SPR_TYPE_EX_AC_AR) 9043 && lun->pr_keys[residx] == 0)) { 9044 ctl_set_reservation_conflict(ctsio); 9045 ctl_done((union ctl_io *)ctsio); 9046 return (CTL_RETVAL_COMPLETE); 9047 } 9048 } 9049 9050 switch (ctsio->cdb[0]) { 9051 case READ_6: 9052 case WRITE_6: { 9053 struct scsi_rw_6 *cdb; 9054 9055 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9056 9057 lba = scsi_3btoul(cdb->addr); 9058 /* only 5 bits are valid in the most significant address byte */ 9059 lba &= 0x1fffff; 9060 num_blocks = cdb->length; 9061 /* 9062 * This is correct according to SBC-2. 9063 */ 9064 if (num_blocks == 0) 9065 num_blocks = 256; 9066 break; 9067 } 9068 case READ_10: 9069 case WRITE_10: { 9070 struct scsi_rw_10 *cdb; 9071 9072 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9073 if (cdb->byte2 & SRW10_FUA) 9074 flags |= CTL_LLF_FUA; 9075 if (cdb->byte2 & SRW10_DPO) 9076 flags |= CTL_LLF_DPO; 9077 lba = scsi_4btoul(cdb->addr); 9078 num_blocks = scsi_2btoul(cdb->length); 9079 break; 9080 } 9081 case WRITE_VERIFY_10: { 9082 struct scsi_write_verify_10 *cdb; 9083 9084 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9085 flags |= CTL_LLF_FUA; 9086 if (cdb->byte2 & SWV_DPO) 9087 flags |= CTL_LLF_DPO; 9088 lba = scsi_4btoul(cdb->addr); 9089 num_blocks = scsi_2btoul(cdb->length); 9090 break; 9091 } 9092 case READ_12: 9093 case WRITE_12: { 9094 struct scsi_rw_12 *cdb; 9095 9096 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9097 if (cdb->byte2 & SRW12_FUA) 9098 flags |= CTL_LLF_FUA; 9099 if (cdb->byte2 & SRW12_DPO) 9100 flags |= CTL_LLF_DPO; 9101 lba = scsi_4btoul(cdb->addr); 9102 num_blocks = scsi_4btoul(cdb->length); 9103 break; 9104 } 9105 case WRITE_VERIFY_12: { 9106 struct scsi_write_verify_12 *cdb; 9107 9108 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9109 flags |= CTL_LLF_FUA; 9110 if (cdb->byte2 & SWV_DPO) 9111 flags |= CTL_LLF_DPO; 9112 lba = scsi_4btoul(cdb->addr); 9113 num_blocks = scsi_4btoul(cdb->length); 9114 break; 9115 } 9116 case READ_16: 9117 case WRITE_16: { 9118 struct scsi_rw_16 *cdb; 9119 9120 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9121 if (cdb->byte2 & SRW12_FUA) 9122 flags |= CTL_LLF_FUA; 9123 if (cdb->byte2 & SRW12_DPO) 9124 flags |= CTL_LLF_DPO; 9125 lba = scsi_8btou64(cdb->addr); 9126 num_blocks = scsi_4btoul(cdb->length); 9127 break; 9128 } 9129 case WRITE_ATOMIC_16: { 9130 struct scsi_rw_16 *cdb; 9131 9132 if (lun->be_lun->atomicblock == 0) { 9133 ctl_set_invalid_opcode(ctsio); 9134 ctl_done((union ctl_io *)ctsio); 9135 return (CTL_RETVAL_COMPLETE); 9136 } 9137 9138 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9139 if (cdb->byte2 & SRW12_FUA) 9140 flags |= CTL_LLF_FUA; 9141 if (cdb->byte2 & SRW12_DPO) 9142 flags |= CTL_LLF_DPO; 9143 lba = scsi_8btou64(cdb->addr); 9144 num_blocks = scsi_4btoul(cdb->length); 9145 if (num_blocks > lun->be_lun->atomicblock) { 9146 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9147 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9148 /*bit*/ 0); 9149 ctl_done((union ctl_io *)ctsio); 9150 return (CTL_RETVAL_COMPLETE); 9151 } 9152 break; 9153 } 9154 case WRITE_VERIFY_16: { 9155 struct scsi_write_verify_16 *cdb; 9156 9157 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9158 flags |= CTL_LLF_FUA; 9159 if (cdb->byte2 & SWV_DPO) 9160 flags |= CTL_LLF_DPO; 9161 lba = scsi_8btou64(cdb->addr); 9162 num_blocks = scsi_4btoul(cdb->length); 9163 break; 9164 } 9165 default: 9166 /* 9167 * We got a command we don't support. This shouldn't 9168 * happen, commands should be filtered out above us. 9169 */ 9170 ctl_set_invalid_opcode(ctsio); 9171 ctl_done((union ctl_io *)ctsio); 9172 9173 return (CTL_RETVAL_COMPLETE); 9174 break; /* NOTREACHED */ 9175 } 9176 9177 /* 9178 * The first check is to make sure we're in bounds, the second 9179 * check is to catch wrap-around problems. If the lba + num blocks 9180 * is less than the lba, then we've wrapped around and the block 9181 * range is invalid anyway. 9182 */ 9183 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9184 || ((lba + num_blocks) < lba)) { 9185 ctl_set_lba_out_of_range(ctsio); 9186 ctl_done((union ctl_io *)ctsio); 9187 return (CTL_RETVAL_COMPLETE); 9188 } 9189 9190 /* 9191 * According to SBC-3, a transfer length of 0 is not an error. 9192 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9193 * translates to 256 blocks for those commands. 9194 */ 9195 if (num_blocks == 0) { 9196 ctl_set_success(ctsio); 9197 ctl_done((union ctl_io *)ctsio); 9198 return (CTL_RETVAL_COMPLETE); 9199 } 9200 9201 /* Set FUA and/or DPO if caches are disabled. */ 9202 if (isread) { 9203 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9204 SCP_RCD) != 0) 9205 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9206 } else { 9207 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9208 SCP_WCE) == 0) 9209 flags |= CTL_LLF_FUA; 9210 } 9211 9212 lbalen = (struct ctl_lba_len_flags *) 9213 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9214 lbalen->lba = lba; 9215 lbalen->len = num_blocks; 9216 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9217 9218 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9219 ctsio->kern_rel_offset = 0; 9220 9221 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9222 9223 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9224 9225 return (retval); 9226 } 9227 9228 static int 9229 ctl_cnw_cont(union ctl_io *io) 9230 { 9231 struct ctl_scsiio *ctsio; 9232 struct ctl_lun *lun; 9233 struct ctl_lba_len_flags *lbalen; 9234 int retval; 9235 9236 ctsio = &io->scsiio; 9237 ctsio->io_hdr.status = CTL_STATUS_NONE; 9238 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9239 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9240 lbalen = (struct ctl_lba_len_flags *) 9241 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9242 lbalen->flags &= ~CTL_LLF_COMPARE; 9243 lbalen->flags |= CTL_LLF_WRITE; 9244 9245 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9246 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9247 return (retval); 9248 } 9249 9250 int 9251 ctl_cnw(struct ctl_scsiio *ctsio) 9252 { 9253 struct ctl_lun *lun; 9254 struct ctl_lba_len_flags *lbalen; 9255 uint64_t lba; 9256 uint32_t num_blocks; 9257 int flags, retval; 9258 9259 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9260 9261 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9262 9263 flags = 0; 9264 retval = CTL_RETVAL_COMPLETE; 9265 9266 switch (ctsio->cdb[0]) { 9267 case COMPARE_AND_WRITE: { 9268 struct scsi_compare_and_write *cdb; 9269 9270 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9271 if (cdb->byte2 & SRW10_FUA) 9272 flags |= CTL_LLF_FUA; 9273 if (cdb->byte2 & SRW10_DPO) 9274 flags |= CTL_LLF_DPO; 9275 lba = scsi_8btou64(cdb->addr); 9276 num_blocks = cdb->length; 9277 break; 9278 } 9279 default: 9280 /* 9281 * We got a command we don't support. This shouldn't 9282 * happen, commands should be filtered out above us. 9283 */ 9284 ctl_set_invalid_opcode(ctsio); 9285 ctl_done((union ctl_io *)ctsio); 9286 9287 return (CTL_RETVAL_COMPLETE); 9288 break; /* NOTREACHED */ 9289 } 9290 9291 /* 9292 * The first check is to make sure we're in bounds, the second 9293 * check is to catch wrap-around problems. If the lba + num blocks 9294 * is less than the lba, then we've wrapped around and the block 9295 * range is invalid anyway. 9296 */ 9297 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9298 || ((lba + num_blocks) < lba)) { 9299 ctl_set_lba_out_of_range(ctsio); 9300 ctl_done((union ctl_io *)ctsio); 9301 return (CTL_RETVAL_COMPLETE); 9302 } 9303 9304 /* 9305 * According to SBC-3, a transfer length of 0 is not an error. 9306 */ 9307 if (num_blocks == 0) { 9308 ctl_set_success(ctsio); 9309 ctl_done((union ctl_io *)ctsio); 9310 return (CTL_RETVAL_COMPLETE); 9311 } 9312 9313 /* Set FUA if write cache is disabled. */ 9314 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9315 SCP_WCE) == 0) 9316 flags |= CTL_LLF_FUA; 9317 9318 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9319 ctsio->kern_rel_offset = 0; 9320 9321 /* 9322 * Set the IO_CONT flag, so that if this I/O gets passed to 9323 * ctl_data_submit_done(), it'll get passed back to 9324 * ctl_ctl_cnw_cont() for further processing. 9325 */ 9326 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9327 ctsio->io_cont = ctl_cnw_cont; 9328 9329 lbalen = (struct ctl_lba_len_flags *) 9330 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9331 lbalen->lba = lba; 9332 lbalen->len = num_blocks; 9333 lbalen->flags = CTL_LLF_COMPARE | flags; 9334 9335 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9336 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9337 return (retval); 9338 } 9339 9340 int 9341 ctl_verify(struct ctl_scsiio *ctsio) 9342 { 9343 struct ctl_lun *lun; 9344 struct ctl_lba_len_flags *lbalen; 9345 uint64_t lba; 9346 uint32_t num_blocks; 9347 int bytchk, flags; 9348 int retval; 9349 9350 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9351 9352 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9353 9354 bytchk = 0; 9355 flags = CTL_LLF_FUA; 9356 retval = CTL_RETVAL_COMPLETE; 9357 9358 switch (ctsio->cdb[0]) { 9359 case VERIFY_10: { 9360 struct scsi_verify_10 *cdb; 9361 9362 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9363 if (cdb->byte2 & SVFY_BYTCHK) 9364 bytchk = 1; 9365 if (cdb->byte2 & SVFY_DPO) 9366 flags |= CTL_LLF_DPO; 9367 lba = scsi_4btoul(cdb->addr); 9368 num_blocks = scsi_2btoul(cdb->length); 9369 break; 9370 } 9371 case VERIFY_12: { 9372 struct scsi_verify_12 *cdb; 9373 9374 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9375 if (cdb->byte2 & SVFY_BYTCHK) 9376 bytchk = 1; 9377 if (cdb->byte2 & SVFY_DPO) 9378 flags |= CTL_LLF_DPO; 9379 lba = scsi_4btoul(cdb->addr); 9380 num_blocks = scsi_4btoul(cdb->length); 9381 break; 9382 } 9383 case VERIFY_16: { 9384 struct scsi_rw_16 *cdb; 9385 9386 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9387 if (cdb->byte2 & SVFY_BYTCHK) 9388 bytchk = 1; 9389 if (cdb->byte2 & SVFY_DPO) 9390 flags |= CTL_LLF_DPO; 9391 lba = scsi_8btou64(cdb->addr); 9392 num_blocks = scsi_4btoul(cdb->length); 9393 break; 9394 } 9395 default: 9396 /* 9397 * We got a command we don't support. This shouldn't 9398 * happen, commands should be filtered out above us. 9399 */ 9400 ctl_set_invalid_opcode(ctsio); 9401 ctl_done((union ctl_io *)ctsio); 9402 return (CTL_RETVAL_COMPLETE); 9403 } 9404 9405 /* 9406 * The first check is to make sure we're in bounds, the second 9407 * check is to catch wrap-around problems. If the lba + num blocks 9408 * is less than the lba, then we've wrapped around and the block 9409 * range is invalid anyway. 9410 */ 9411 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9412 || ((lba + num_blocks) < lba)) { 9413 ctl_set_lba_out_of_range(ctsio); 9414 ctl_done((union ctl_io *)ctsio); 9415 return (CTL_RETVAL_COMPLETE); 9416 } 9417 9418 /* 9419 * According to SBC-3, a transfer length of 0 is not an error. 9420 */ 9421 if (num_blocks == 0) { 9422 ctl_set_success(ctsio); 9423 ctl_done((union ctl_io *)ctsio); 9424 return (CTL_RETVAL_COMPLETE); 9425 } 9426 9427 lbalen = (struct ctl_lba_len_flags *) 9428 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9429 lbalen->lba = lba; 9430 lbalen->len = num_blocks; 9431 if (bytchk) { 9432 lbalen->flags = CTL_LLF_COMPARE | flags; 9433 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9434 } else { 9435 lbalen->flags = CTL_LLF_VERIFY | flags; 9436 ctsio->kern_total_len = 0; 9437 } 9438 ctsio->kern_rel_offset = 0; 9439 9440 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9441 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9442 return (retval); 9443 } 9444 9445 int 9446 ctl_report_luns(struct ctl_scsiio *ctsio) 9447 { 9448 struct scsi_report_luns *cdb; 9449 struct scsi_report_luns_data *lun_data; 9450 struct ctl_lun *lun, *request_lun; 9451 int num_luns, retval; 9452 uint32_t alloc_len, lun_datalen; 9453 int num_filled, well_known; 9454 uint32_t initidx, targ_lun_id, lun_id; 9455 9456 retval = CTL_RETVAL_COMPLETE; 9457 well_known = 0; 9458 9459 cdb = (struct scsi_report_luns *)ctsio->cdb; 9460 9461 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9462 9463 mtx_lock(&control_softc->ctl_lock); 9464 num_luns = control_softc->num_luns; 9465 mtx_unlock(&control_softc->ctl_lock); 9466 9467 switch (cdb->select_report) { 9468 case RPL_REPORT_DEFAULT: 9469 case RPL_REPORT_ALL: 9470 break; 9471 case RPL_REPORT_WELLKNOWN: 9472 well_known = 1; 9473 num_luns = 0; 9474 break; 9475 default: 9476 ctl_set_invalid_field(ctsio, 9477 /*sks_valid*/ 1, 9478 /*command*/ 1, 9479 /*field*/ 2, 9480 /*bit_valid*/ 0, 9481 /*bit*/ 0); 9482 ctl_done((union ctl_io *)ctsio); 9483 return (retval); 9484 break; /* NOTREACHED */ 9485 } 9486 9487 alloc_len = scsi_4btoul(cdb->length); 9488 /* 9489 * The initiator has to allocate at least 16 bytes for this request, 9490 * so he can at least get the header and the first LUN. Otherwise 9491 * we reject the request (per SPC-3 rev 14, section 6.21). 9492 */ 9493 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9494 sizeof(struct scsi_report_luns_lundata))) { 9495 ctl_set_invalid_field(ctsio, 9496 /*sks_valid*/ 1, 9497 /*command*/ 1, 9498 /*field*/ 6, 9499 /*bit_valid*/ 0, 9500 /*bit*/ 0); 9501 ctl_done((union ctl_io *)ctsio); 9502 return (retval); 9503 } 9504 9505 request_lun = (struct ctl_lun *) 9506 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9507 9508 lun_datalen = sizeof(*lun_data) + 9509 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9510 9511 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9512 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9513 ctsio->kern_sg_entries = 0; 9514 9515 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9516 9517 mtx_lock(&control_softc->ctl_lock); 9518 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9519 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9520 if (lun_id >= CTL_MAX_LUNS) 9521 continue; 9522 lun = control_softc->ctl_luns[lun_id]; 9523 if (lun == NULL) 9524 continue; 9525 9526 if (targ_lun_id <= 0xff) { 9527 /* 9528 * Peripheral addressing method, bus number 0. 9529 */ 9530 lun_data->luns[num_filled].lundata[0] = 9531 RPL_LUNDATA_ATYP_PERIPH; 9532 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9533 num_filled++; 9534 } else if (targ_lun_id <= 0x3fff) { 9535 /* 9536 * Flat addressing method. 9537 */ 9538 lun_data->luns[num_filled].lundata[0] = 9539 RPL_LUNDATA_ATYP_FLAT | 9540 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9541 #ifdef OLDCTLHEADERS 9542 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9543 (targ_lun_id & SRLD_BUS_LUN_MASK); 9544 #endif 9545 lun_data->luns[num_filled].lundata[1] = 9546 #ifdef OLDCTLHEADERS 9547 targ_lun_id >> SRLD_BUS_LUN_BITS; 9548 #endif 9549 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9550 num_filled++; 9551 } else { 9552 printf("ctl_report_luns: bogus LUN number %jd, " 9553 "skipping\n", (intmax_t)targ_lun_id); 9554 } 9555 /* 9556 * According to SPC-3, rev 14 section 6.21: 9557 * 9558 * "The execution of a REPORT LUNS command to any valid and 9559 * installed logical unit shall clear the REPORTED LUNS DATA 9560 * HAS CHANGED unit attention condition for all logical 9561 * units of that target with respect to the requesting 9562 * initiator. A valid and installed logical unit is one 9563 * having a PERIPHERAL QUALIFIER of 000b in the standard 9564 * INQUIRY data (see 6.4.2)." 9565 * 9566 * If request_lun is NULL, the LUN this report luns command 9567 * was issued to is either disabled or doesn't exist. In that 9568 * case, we shouldn't clear any pending lun change unit 9569 * attention. 9570 */ 9571 if (request_lun != NULL) { 9572 mtx_lock(&lun->lun_lock); 9573 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9574 mtx_unlock(&lun->lun_lock); 9575 } 9576 } 9577 mtx_unlock(&control_softc->ctl_lock); 9578 9579 /* 9580 * It's quite possible that we've returned fewer LUNs than we allocated 9581 * space for. Trim it. 9582 */ 9583 lun_datalen = sizeof(*lun_data) + 9584 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9585 9586 if (lun_datalen < alloc_len) { 9587 ctsio->residual = alloc_len - lun_datalen; 9588 ctsio->kern_data_len = lun_datalen; 9589 ctsio->kern_total_len = lun_datalen; 9590 } else { 9591 ctsio->residual = 0; 9592 ctsio->kern_data_len = alloc_len; 9593 ctsio->kern_total_len = alloc_len; 9594 } 9595 ctsio->kern_data_resid = 0; 9596 ctsio->kern_rel_offset = 0; 9597 ctsio->kern_sg_entries = 0; 9598 9599 /* 9600 * We set this to the actual data length, regardless of how much 9601 * space we actually have to return results. If the user looks at 9602 * this value, he'll know whether or not he allocated enough space 9603 * and reissue the command if necessary. We don't support well 9604 * known logical units, so if the user asks for that, return none. 9605 */ 9606 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9607 9608 /* 9609 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9610 * this request. 9611 */ 9612 ctsio->scsi_status = SCSI_STATUS_OK; 9613 9614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9615 ctsio->be_move_done = ctl_config_move_done; 9616 ctl_datamove((union ctl_io *)ctsio); 9617 9618 return (retval); 9619 } 9620 9621 int 9622 ctl_request_sense(struct ctl_scsiio *ctsio) 9623 { 9624 struct scsi_request_sense *cdb; 9625 struct scsi_sense_data *sense_ptr; 9626 struct ctl_lun *lun; 9627 uint32_t initidx; 9628 int have_error; 9629 scsi_sense_data_type sense_format; 9630 9631 cdb = (struct scsi_request_sense *)ctsio->cdb; 9632 9633 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9634 9635 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9636 9637 /* 9638 * Determine which sense format the user wants. 9639 */ 9640 if (cdb->byte2 & SRS_DESC) 9641 sense_format = SSD_TYPE_DESC; 9642 else 9643 sense_format = SSD_TYPE_FIXED; 9644 9645 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9646 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9647 ctsio->kern_sg_entries = 0; 9648 9649 /* 9650 * struct scsi_sense_data, which is currently set to 256 bytes, is 9651 * larger than the largest allowed value for the length field in the 9652 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9653 */ 9654 ctsio->residual = 0; 9655 ctsio->kern_data_len = cdb->length; 9656 ctsio->kern_total_len = cdb->length; 9657 9658 ctsio->kern_data_resid = 0; 9659 ctsio->kern_rel_offset = 0; 9660 ctsio->kern_sg_entries = 0; 9661 9662 /* 9663 * If we don't have a LUN, we don't have any pending sense. 9664 */ 9665 if (lun == NULL) 9666 goto no_sense; 9667 9668 have_error = 0; 9669 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9670 /* 9671 * Check for pending sense, and then for pending unit attentions. 9672 * Pending sense gets returned first, then pending unit attentions. 9673 */ 9674 mtx_lock(&lun->lun_lock); 9675 #ifdef CTL_WITH_CA 9676 if (ctl_is_set(lun->have_ca, initidx)) { 9677 scsi_sense_data_type stored_format; 9678 9679 /* 9680 * Check to see which sense format was used for the stored 9681 * sense data. 9682 */ 9683 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9684 9685 /* 9686 * If the user requested a different sense format than the 9687 * one we stored, then we need to convert it to the other 9688 * format. If we're going from descriptor to fixed format 9689 * sense data, we may lose things in translation, depending 9690 * on what options were used. 9691 * 9692 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9693 * for some reason we'll just copy it out as-is. 9694 */ 9695 if ((stored_format == SSD_TYPE_FIXED) 9696 && (sense_format == SSD_TYPE_DESC)) 9697 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9698 &lun->pending_sense[initidx], 9699 (struct scsi_sense_data_desc *)sense_ptr); 9700 else if ((stored_format == SSD_TYPE_DESC) 9701 && (sense_format == SSD_TYPE_FIXED)) 9702 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9703 &lun->pending_sense[initidx], 9704 (struct scsi_sense_data_fixed *)sense_ptr); 9705 else 9706 memcpy(sense_ptr, &lun->pending_sense[initidx], 9707 ctl_min(sizeof(*sense_ptr), 9708 sizeof(lun->pending_sense[initidx]))); 9709 9710 ctl_clear_mask(lun->have_ca, initidx); 9711 have_error = 1; 9712 } else 9713 #endif 9714 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9715 ctl_ua_type ua_type; 9716 9717 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9718 sense_ptr, sense_format); 9719 if (ua_type != CTL_UA_NONE) 9720 have_error = 1; 9721 } 9722 mtx_unlock(&lun->lun_lock); 9723 9724 /* 9725 * We already have a pending error, return it. 9726 */ 9727 if (have_error != 0) { 9728 /* 9729 * We report the SCSI status as OK, since the status of the 9730 * request sense command itself is OK. 9731 */ 9732 ctsio->scsi_status = SCSI_STATUS_OK; 9733 9734 /* 9735 * We report 0 for the sense length, because we aren't doing 9736 * autosense in this case. We're reporting sense as 9737 * parameter data. 9738 */ 9739 ctsio->sense_len = 0; 9740 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9741 ctsio->be_move_done = ctl_config_move_done; 9742 ctl_datamove((union ctl_io *)ctsio); 9743 9744 return (CTL_RETVAL_COMPLETE); 9745 } 9746 9747 no_sense: 9748 9749 /* 9750 * No sense information to report, so we report that everything is 9751 * okay. 9752 */ 9753 ctl_set_sense_data(sense_ptr, 9754 lun, 9755 sense_format, 9756 /*current_error*/ 1, 9757 /*sense_key*/ SSD_KEY_NO_SENSE, 9758 /*asc*/ 0x00, 9759 /*ascq*/ 0x00, 9760 SSD_ELEM_NONE); 9761 9762 ctsio->scsi_status = SCSI_STATUS_OK; 9763 9764 /* 9765 * We report 0 for the sense length, because we aren't doing 9766 * autosense in this case. We're reporting sense as parameter data. 9767 */ 9768 ctsio->sense_len = 0; 9769 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9770 ctsio->be_move_done = ctl_config_move_done; 9771 ctl_datamove((union ctl_io *)ctsio); 9772 9773 return (CTL_RETVAL_COMPLETE); 9774 } 9775 9776 int 9777 ctl_tur(struct ctl_scsiio *ctsio) 9778 { 9779 struct ctl_lun *lun; 9780 9781 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9782 9783 CTL_DEBUG_PRINT(("ctl_tur\n")); 9784 9785 if (lun == NULL) 9786 return (EINVAL); 9787 9788 ctsio->scsi_status = SCSI_STATUS_OK; 9789 ctsio->io_hdr.status = CTL_SUCCESS; 9790 9791 ctl_done((union ctl_io *)ctsio); 9792 9793 return (CTL_RETVAL_COMPLETE); 9794 } 9795 9796 #ifdef notyet 9797 static int 9798 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9799 { 9800 9801 } 9802 #endif 9803 9804 static int 9805 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9806 { 9807 struct scsi_vpd_supported_pages *pages; 9808 int sup_page_size; 9809 struct ctl_lun *lun; 9810 9811 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9812 9813 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9814 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9815 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9816 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9817 ctsio->kern_sg_entries = 0; 9818 9819 if (sup_page_size < alloc_len) { 9820 ctsio->residual = alloc_len - sup_page_size; 9821 ctsio->kern_data_len = sup_page_size; 9822 ctsio->kern_total_len = sup_page_size; 9823 } else { 9824 ctsio->residual = 0; 9825 ctsio->kern_data_len = alloc_len; 9826 ctsio->kern_total_len = alloc_len; 9827 } 9828 ctsio->kern_data_resid = 0; 9829 ctsio->kern_rel_offset = 0; 9830 ctsio->kern_sg_entries = 0; 9831 9832 /* 9833 * The control device is always connected. The disk device, on the 9834 * other hand, may not be online all the time. Need to change this 9835 * to figure out whether the disk device is actually online or not. 9836 */ 9837 if (lun != NULL) 9838 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9839 lun->be_lun->lun_type; 9840 else 9841 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9842 9843 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9844 /* Supported VPD pages */ 9845 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9846 /* Serial Number */ 9847 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9848 /* Device Identification */ 9849 pages->page_list[2] = SVPD_DEVICE_ID; 9850 /* Extended INQUIRY Data */ 9851 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9852 /* Mode Page Policy */ 9853 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9854 /* SCSI Ports */ 9855 pages->page_list[5] = SVPD_SCSI_PORTS; 9856 /* Third-party Copy */ 9857 pages->page_list[6] = SVPD_SCSI_TPC; 9858 /* Block limits */ 9859 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9860 /* Block Device Characteristics */ 9861 pages->page_list[8] = SVPD_BDC; 9862 /* Logical Block Provisioning */ 9863 pages->page_list[9] = SVPD_LBP; 9864 9865 ctsio->scsi_status = SCSI_STATUS_OK; 9866 9867 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9868 ctsio->be_move_done = ctl_config_move_done; 9869 ctl_datamove((union ctl_io *)ctsio); 9870 9871 return (CTL_RETVAL_COMPLETE); 9872 } 9873 9874 static int 9875 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9876 { 9877 struct scsi_vpd_unit_serial_number *sn_ptr; 9878 struct ctl_lun *lun; 9879 9880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9881 9882 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9883 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9884 ctsio->kern_sg_entries = 0; 9885 9886 if (sizeof(*sn_ptr) < alloc_len) { 9887 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9888 ctsio->kern_data_len = sizeof(*sn_ptr); 9889 ctsio->kern_total_len = sizeof(*sn_ptr); 9890 } else { 9891 ctsio->residual = 0; 9892 ctsio->kern_data_len = alloc_len; 9893 ctsio->kern_total_len = alloc_len; 9894 } 9895 ctsio->kern_data_resid = 0; 9896 ctsio->kern_rel_offset = 0; 9897 ctsio->kern_sg_entries = 0; 9898 9899 /* 9900 * The control device is always connected. The disk device, on the 9901 * other hand, may not be online all the time. Need to change this 9902 * to figure out whether the disk device is actually online or not. 9903 */ 9904 if (lun != NULL) 9905 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9906 lun->be_lun->lun_type; 9907 else 9908 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9909 9910 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9911 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9912 /* 9913 * If we don't have a LUN, we just leave the serial number as 9914 * all spaces. 9915 */ 9916 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9917 if (lun != NULL) { 9918 strncpy((char *)sn_ptr->serial_num, 9919 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9920 } 9921 ctsio->scsi_status = SCSI_STATUS_OK; 9922 9923 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9924 ctsio->be_move_done = ctl_config_move_done; 9925 ctl_datamove((union ctl_io *)ctsio); 9926 9927 return (CTL_RETVAL_COMPLETE); 9928 } 9929 9930 9931 static int 9932 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9933 { 9934 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9935 struct ctl_lun *lun; 9936 int data_len; 9937 9938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9939 9940 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9941 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9942 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9943 ctsio->kern_sg_entries = 0; 9944 9945 if (data_len < alloc_len) { 9946 ctsio->residual = alloc_len - data_len; 9947 ctsio->kern_data_len = data_len; 9948 ctsio->kern_total_len = data_len; 9949 } else { 9950 ctsio->residual = 0; 9951 ctsio->kern_data_len = alloc_len; 9952 ctsio->kern_total_len = alloc_len; 9953 } 9954 ctsio->kern_data_resid = 0; 9955 ctsio->kern_rel_offset = 0; 9956 ctsio->kern_sg_entries = 0; 9957 9958 /* 9959 * The control device is always connected. The disk device, on the 9960 * other hand, may not be online all the time. 9961 */ 9962 if (lun != NULL) 9963 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9964 lun->be_lun->lun_type; 9965 else 9966 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9967 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9968 eid_ptr->page_length = data_len - 4; 9969 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9970 eid_ptr->flags3 = SVPD_EID_V_SUP; 9971 9972 ctsio->scsi_status = SCSI_STATUS_OK; 9973 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9974 ctsio->be_move_done = ctl_config_move_done; 9975 ctl_datamove((union ctl_io *)ctsio); 9976 9977 return (CTL_RETVAL_COMPLETE); 9978 } 9979 9980 static int 9981 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9982 { 9983 struct scsi_vpd_mode_page_policy *mpp_ptr; 9984 struct ctl_lun *lun; 9985 int data_len; 9986 9987 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9988 9989 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9990 sizeof(struct scsi_vpd_mode_page_policy_descr); 9991 9992 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9993 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9994 ctsio->kern_sg_entries = 0; 9995 9996 if (data_len < alloc_len) { 9997 ctsio->residual = alloc_len - data_len; 9998 ctsio->kern_data_len = data_len; 9999 ctsio->kern_total_len = data_len; 10000 } else { 10001 ctsio->residual = 0; 10002 ctsio->kern_data_len = alloc_len; 10003 ctsio->kern_total_len = alloc_len; 10004 } 10005 ctsio->kern_data_resid = 0; 10006 ctsio->kern_rel_offset = 0; 10007 ctsio->kern_sg_entries = 0; 10008 10009 /* 10010 * The control device is always connected. The disk device, on the 10011 * other hand, may not be online all the time. 10012 */ 10013 if (lun != NULL) 10014 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10015 lun->be_lun->lun_type; 10016 else 10017 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10018 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 10019 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 10020 mpp_ptr->descr[0].page_code = 0x3f; 10021 mpp_ptr->descr[0].subpage_code = 0xff; 10022 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 10023 10024 ctsio->scsi_status = SCSI_STATUS_OK; 10025 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10026 ctsio->be_move_done = ctl_config_move_done; 10027 ctl_datamove((union ctl_io *)ctsio); 10028 10029 return (CTL_RETVAL_COMPLETE); 10030 } 10031 10032 static int 10033 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 10034 { 10035 struct scsi_vpd_device_id *devid_ptr; 10036 struct scsi_vpd_id_descriptor *desc; 10037 struct ctl_softc *ctl_softc; 10038 struct ctl_lun *lun; 10039 struct ctl_port *port; 10040 int data_len; 10041 uint8_t proto; 10042 10043 ctl_softc = control_softc; 10044 10045 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 10046 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10047 10048 data_len = sizeof(struct scsi_vpd_device_id) + 10049 sizeof(struct scsi_vpd_id_descriptor) + 10050 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 10051 sizeof(struct scsi_vpd_id_descriptor) + 10052 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 10053 if (lun && lun->lun_devid) 10054 data_len += lun->lun_devid->len; 10055 if (port->port_devid) 10056 data_len += port->port_devid->len; 10057 if (port->target_devid) 10058 data_len += port->target_devid->len; 10059 10060 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10061 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10062 ctsio->kern_sg_entries = 0; 10063 10064 if (data_len < alloc_len) { 10065 ctsio->residual = alloc_len - data_len; 10066 ctsio->kern_data_len = data_len; 10067 ctsio->kern_total_len = data_len; 10068 } else { 10069 ctsio->residual = 0; 10070 ctsio->kern_data_len = alloc_len; 10071 ctsio->kern_total_len = alloc_len; 10072 } 10073 ctsio->kern_data_resid = 0; 10074 ctsio->kern_rel_offset = 0; 10075 ctsio->kern_sg_entries = 0; 10076 10077 /* 10078 * The control device is always connected. The disk device, on the 10079 * other hand, may not be online all the time. 10080 */ 10081 if (lun != NULL) 10082 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10083 lun->be_lun->lun_type; 10084 else 10085 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10086 devid_ptr->page_code = SVPD_DEVICE_ID; 10087 scsi_ulto2b(data_len - 4, devid_ptr->length); 10088 10089 if (port->port_type == CTL_PORT_FC) 10090 proto = SCSI_PROTO_FC << 4; 10091 else if (port->port_type == CTL_PORT_ISCSI) 10092 proto = SCSI_PROTO_ISCSI << 4; 10093 else 10094 proto = SCSI_PROTO_SPI << 4; 10095 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10096 10097 /* 10098 * We're using a LUN association here. i.e., this device ID is a 10099 * per-LUN identifier. 10100 */ 10101 if (lun && lun->lun_devid) { 10102 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10103 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10104 lun->lun_devid->len); 10105 } 10106 10107 /* 10108 * This is for the WWPN which is a port association. 10109 */ 10110 if (port->port_devid) { 10111 memcpy(desc, port->port_devid->data, port->port_devid->len); 10112 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10113 port->port_devid->len); 10114 } 10115 10116 /* 10117 * This is for the Relative Target Port(type 4h) identifier 10118 */ 10119 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10120 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10121 SVPD_ID_TYPE_RELTARG; 10122 desc->length = 4; 10123 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10124 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10125 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10126 10127 /* 10128 * This is for the Target Port Group(type 5h) identifier 10129 */ 10130 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10131 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10132 SVPD_ID_TYPE_TPORTGRP; 10133 desc->length = 4; 10134 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10135 &desc->identifier[2]); 10136 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10137 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10138 10139 /* 10140 * This is for the Target identifier 10141 */ 10142 if (port->target_devid) { 10143 memcpy(desc, port->target_devid->data, port->target_devid->len); 10144 } 10145 10146 ctsio->scsi_status = SCSI_STATUS_OK; 10147 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10148 ctsio->be_move_done = ctl_config_move_done; 10149 ctl_datamove((union ctl_io *)ctsio); 10150 10151 return (CTL_RETVAL_COMPLETE); 10152 } 10153 10154 static int 10155 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10156 { 10157 struct ctl_softc *softc = control_softc; 10158 struct scsi_vpd_scsi_ports *sp; 10159 struct scsi_vpd_port_designation *pd; 10160 struct scsi_vpd_port_designation_cont *pdc; 10161 struct ctl_lun *lun; 10162 struct ctl_port *port; 10163 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10164 int num_target_port_groups, single; 10165 10166 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10167 10168 single = ctl_is_single; 10169 if (single) 10170 num_target_port_groups = 1; 10171 else 10172 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10173 num_target_ports = 0; 10174 iid_len = 0; 10175 id_len = 0; 10176 mtx_lock(&softc->ctl_lock); 10177 STAILQ_FOREACH(port, &softc->port_list, links) { 10178 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10179 continue; 10180 if (lun != NULL && 10181 ctl_map_lun_back(port->targ_port, lun->lun) >= 10182 CTL_MAX_LUNS) 10183 continue; 10184 num_target_ports++; 10185 if (port->init_devid) 10186 iid_len += port->init_devid->len; 10187 if (port->port_devid) 10188 id_len += port->port_devid->len; 10189 } 10190 mtx_unlock(&softc->ctl_lock); 10191 10192 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10193 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10194 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10195 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10196 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10197 ctsio->kern_sg_entries = 0; 10198 10199 if (data_len < alloc_len) { 10200 ctsio->residual = alloc_len - data_len; 10201 ctsio->kern_data_len = data_len; 10202 ctsio->kern_total_len = data_len; 10203 } else { 10204 ctsio->residual = 0; 10205 ctsio->kern_data_len = alloc_len; 10206 ctsio->kern_total_len = alloc_len; 10207 } 10208 ctsio->kern_data_resid = 0; 10209 ctsio->kern_rel_offset = 0; 10210 ctsio->kern_sg_entries = 0; 10211 10212 /* 10213 * The control device is always connected. The disk device, on the 10214 * other hand, may not be online all the time. Need to change this 10215 * to figure out whether the disk device is actually online or not. 10216 */ 10217 if (lun != NULL) 10218 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10219 lun->be_lun->lun_type; 10220 else 10221 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10222 10223 sp->page_code = SVPD_SCSI_PORTS; 10224 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10225 sp->page_length); 10226 pd = &sp->design[0]; 10227 10228 mtx_lock(&softc->ctl_lock); 10229 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10230 pg = 0; 10231 else 10232 pg = 1; 10233 for (g = 0; g < num_target_port_groups; g++) { 10234 STAILQ_FOREACH(port, &softc->port_list, links) { 10235 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10236 continue; 10237 if (lun != NULL && 10238 ctl_map_lun_back(port->targ_port, lun->lun) >= 10239 CTL_MAX_LUNS) 10240 continue; 10241 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10242 scsi_ulto2b(p, pd->relative_port_id); 10243 if (port->init_devid && g == pg) { 10244 iid_len = port->init_devid->len; 10245 memcpy(pd->initiator_transportid, 10246 port->init_devid->data, port->init_devid->len); 10247 } else 10248 iid_len = 0; 10249 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10250 pdc = (struct scsi_vpd_port_designation_cont *) 10251 (&pd->initiator_transportid[iid_len]); 10252 if (port->port_devid && g == pg) { 10253 id_len = port->port_devid->len; 10254 memcpy(pdc->target_port_descriptors, 10255 port->port_devid->data, port->port_devid->len); 10256 } else 10257 id_len = 0; 10258 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10259 pd = (struct scsi_vpd_port_designation *) 10260 ((uint8_t *)pdc->target_port_descriptors + id_len); 10261 } 10262 } 10263 mtx_unlock(&softc->ctl_lock); 10264 10265 ctsio->scsi_status = SCSI_STATUS_OK; 10266 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10267 ctsio->be_move_done = ctl_config_move_done; 10268 ctl_datamove((union ctl_io *)ctsio); 10269 10270 return (CTL_RETVAL_COMPLETE); 10271 } 10272 10273 static int 10274 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10275 { 10276 struct scsi_vpd_block_limits *bl_ptr; 10277 struct ctl_lun *lun; 10278 int bs; 10279 10280 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10281 10282 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10283 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10284 ctsio->kern_sg_entries = 0; 10285 10286 if (sizeof(*bl_ptr) < alloc_len) { 10287 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10288 ctsio->kern_data_len = sizeof(*bl_ptr); 10289 ctsio->kern_total_len = sizeof(*bl_ptr); 10290 } else { 10291 ctsio->residual = 0; 10292 ctsio->kern_data_len = alloc_len; 10293 ctsio->kern_total_len = alloc_len; 10294 } 10295 ctsio->kern_data_resid = 0; 10296 ctsio->kern_rel_offset = 0; 10297 ctsio->kern_sg_entries = 0; 10298 10299 /* 10300 * The control device is always connected. The disk device, on the 10301 * other hand, may not be online all the time. Need to change this 10302 * to figure out whether the disk device is actually online or not. 10303 */ 10304 if (lun != NULL) 10305 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10306 lun->be_lun->lun_type; 10307 else 10308 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10309 10310 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10311 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10312 bl_ptr->max_cmp_write_len = 0xff; 10313 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10314 if (lun != NULL) { 10315 bs = lun->be_lun->blocksize; 10316 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10317 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10318 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10319 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10320 if (lun->be_lun->pblockexp != 0) { 10321 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10322 bl_ptr->opt_unmap_grain); 10323 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10324 bl_ptr->unmap_grain_align); 10325 } 10326 } 10327 scsi_ulto4b(lun->be_lun->atomicblock, 10328 bl_ptr->max_atomic_transfer_length); 10329 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10330 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10331 } 10332 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10333 10334 ctsio->scsi_status = SCSI_STATUS_OK; 10335 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10336 ctsio->be_move_done = ctl_config_move_done; 10337 ctl_datamove((union ctl_io *)ctsio); 10338 10339 return (CTL_RETVAL_COMPLETE); 10340 } 10341 10342 static int 10343 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10344 { 10345 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10346 struct ctl_lun *lun; 10347 10348 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10349 10350 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10351 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10352 ctsio->kern_sg_entries = 0; 10353 10354 if (sizeof(*bdc_ptr) < alloc_len) { 10355 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10356 ctsio->kern_data_len = sizeof(*bdc_ptr); 10357 ctsio->kern_total_len = sizeof(*bdc_ptr); 10358 } else { 10359 ctsio->residual = 0; 10360 ctsio->kern_data_len = alloc_len; 10361 ctsio->kern_total_len = alloc_len; 10362 } 10363 ctsio->kern_data_resid = 0; 10364 ctsio->kern_rel_offset = 0; 10365 ctsio->kern_sg_entries = 0; 10366 10367 /* 10368 * The control device is always connected. The disk device, on the 10369 * other hand, may not be online all the time. Need to change this 10370 * to figure out whether the disk device is actually online or not. 10371 */ 10372 if (lun != NULL) 10373 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10374 lun->be_lun->lun_type; 10375 else 10376 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10377 bdc_ptr->page_code = SVPD_BDC; 10378 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10379 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10380 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10381 10382 ctsio->scsi_status = SCSI_STATUS_OK; 10383 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10384 ctsio->be_move_done = ctl_config_move_done; 10385 ctl_datamove((union ctl_io *)ctsio); 10386 10387 return (CTL_RETVAL_COMPLETE); 10388 } 10389 10390 static int 10391 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10392 { 10393 struct scsi_vpd_logical_block_prov *lbp_ptr; 10394 struct ctl_lun *lun; 10395 10396 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10397 10398 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10399 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10400 ctsio->kern_sg_entries = 0; 10401 10402 if (sizeof(*lbp_ptr) < alloc_len) { 10403 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10404 ctsio->kern_data_len = sizeof(*lbp_ptr); 10405 ctsio->kern_total_len = sizeof(*lbp_ptr); 10406 } else { 10407 ctsio->residual = 0; 10408 ctsio->kern_data_len = alloc_len; 10409 ctsio->kern_total_len = alloc_len; 10410 } 10411 ctsio->kern_data_resid = 0; 10412 ctsio->kern_rel_offset = 0; 10413 ctsio->kern_sg_entries = 0; 10414 10415 /* 10416 * The control device is always connected. The disk device, on the 10417 * other hand, may not be online all the time. Need to change this 10418 * to figure out whether the disk device is actually online or not. 10419 */ 10420 if (lun != NULL) 10421 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10422 lun->be_lun->lun_type; 10423 else 10424 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10425 10426 lbp_ptr->page_code = SVPD_LBP; 10427 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10428 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10429 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10430 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10431 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10432 } 10433 10434 ctsio->scsi_status = SCSI_STATUS_OK; 10435 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10436 ctsio->be_move_done = ctl_config_move_done; 10437 ctl_datamove((union ctl_io *)ctsio); 10438 10439 return (CTL_RETVAL_COMPLETE); 10440 } 10441 10442 static int 10443 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10444 { 10445 struct scsi_inquiry *cdb; 10446 struct ctl_lun *lun; 10447 int alloc_len, retval; 10448 10449 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10450 cdb = (struct scsi_inquiry *)ctsio->cdb; 10451 10452 retval = CTL_RETVAL_COMPLETE; 10453 10454 alloc_len = scsi_2btoul(cdb->length); 10455 10456 switch (cdb->page_code) { 10457 case SVPD_SUPPORTED_PAGES: 10458 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10459 break; 10460 case SVPD_UNIT_SERIAL_NUMBER: 10461 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10462 break; 10463 case SVPD_DEVICE_ID: 10464 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10465 break; 10466 case SVPD_EXTENDED_INQUIRY_DATA: 10467 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10468 break; 10469 case SVPD_MODE_PAGE_POLICY: 10470 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10471 break; 10472 case SVPD_SCSI_PORTS: 10473 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10474 break; 10475 case SVPD_SCSI_TPC: 10476 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10477 break; 10478 case SVPD_BLOCK_LIMITS: 10479 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10480 break; 10481 case SVPD_BDC: 10482 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10483 break; 10484 case SVPD_LBP: 10485 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10486 break; 10487 default: 10488 ctl_set_invalid_field(ctsio, 10489 /*sks_valid*/ 1, 10490 /*command*/ 1, 10491 /*field*/ 2, 10492 /*bit_valid*/ 0, 10493 /*bit*/ 0); 10494 ctl_done((union ctl_io *)ctsio); 10495 retval = CTL_RETVAL_COMPLETE; 10496 break; 10497 } 10498 10499 return (retval); 10500 } 10501 10502 static int 10503 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10504 { 10505 struct scsi_inquiry_data *inq_ptr; 10506 struct scsi_inquiry *cdb; 10507 struct ctl_softc *ctl_softc; 10508 struct ctl_lun *lun; 10509 char *val; 10510 uint32_t alloc_len, data_len; 10511 ctl_port_type port_type; 10512 10513 ctl_softc = control_softc; 10514 10515 /* 10516 * Figure out whether we're talking to a Fibre Channel port or not. 10517 * We treat the ioctl front end, and any SCSI adapters, as packetized 10518 * SCSI front ends. 10519 */ 10520 port_type = ctl_softc->ctl_ports[ 10521 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10522 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10523 port_type = CTL_PORT_SCSI; 10524 10525 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10526 cdb = (struct scsi_inquiry *)ctsio->cdb; 10527 alloc_len = scsi_2btoul(cdb->length); 10528 10529 /* 10530 * We malloc the full inquiry data size here and fill it 10531 * in. If the user only asks for less, we'll give him 10532 * that much. 10533 */ 10534 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10535 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10536 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10537 ctsio->kern_sg_entries = 0; 10538 ctsio->kern_data_resid = 0; 10539 ctsio->kern_rel_offset = 0; 10540 10541 if (data_len < alloc_len) { 10542 ctsio->residual = alloc_len - data_len; 10543 ctsio->kern_data_len = data_len; 10544 ctsio->kern_total_len = data_len; 10545 } else { 10546 ctsio->residual = 0; 10547 ctsio->kern_data_len = alloc_len; 10548 ctsio->kern_total_len = alloc_len; 10549 } 10550 10551 /* 10552 * If we have a LUN configured, report it as connected. Otherwise, 10553 * report that it is offline or no device is supported, depending 10554 * on the value of inquiry_pq_no_lun. 10555 * 10556 * According to the spec (SPC-4 r34), the peripheral qualifier 10557 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10558 * 10559 * "A peripheral device having the specified peripheral device type 10560 * is not connected to this logical unit. However, the device 10561 * server is capable of supporting the specified peripheral device 10562 * type on this logical unit." 10563 * 10564 * According to the same spec, the peripheral qualifier 10565 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10566 * 10567 * "The device server is not capable of supporting a peripheral 10568 * device on this logical unit. For this peripheral qualifier the 10569 * peripheral device type shall be set to 1Fh. All other peripheral 10570 * device type values are reserved for this peripheral qualifier." 10571 * 10572 * Given the text, it would seem that we probably want to report that 10573 * the LUN is offline here. There is no LUN connected, but we can 10574 * support a LUN at the given LUN number. 10575 * 10576 * In the real world, though, it sounds like things are a little 10577 * different: 10578 * 10579 * - Linux, when presented with a LUN with the offline peripheral 10580 * qualifier, will create an sg driver instance for it. So when 10581 * you attach it to CTL, you wind up with a ton of sg driver 10582 * instances. (One for every LUN that Linux bothered to probe.) 10583 * Linux does this despite the fact that it issues a REPORT LUNs 10584 * to LUN 0 to get the inventory of supported LUNs. 10585 * 10586 * - There is other anecdotal evidence (from Emulex folks) about 10587 * arrays that use the offline peripheral qualifier for LUNs that 10588 * are on the "passive" path in an active/passive array. 10589 * 10590 * So the solution is provide a hopefully reasonable default 10591 * (return bad/no LUN) and allow the user to change the behavior 10592 * with a tunable/sysctl variable. 10593 */ 10594 if (lun != NULL) 10595 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10596 lun->be_lun->lun_type; 10597 else if (ctl_softc->inquiry_pq_no_lun == 0) 10598 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10599 else 10600 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10601 10602 /* RMB in byte 2 is 0 */ 10603 inq_ptr->version = SCSI_REV_SPC4; 10604 10605 /* 10606 * According to SAM-3, even if a device only supports a single 10607 * level of LUN addressing, it should still set the HISUP bit: 10608 * 10609 * 4.9.1 Logical unit numbers overview 10610 * 10611 * All logical unit number formats described in this standard are 10612 * hierarchical in structure even when only a single level in that 10613 * hierarchy is used. The HISUP bit shall be set to one in the 10614 * standard INQUIRY data (see SPC-2) when any logical unit number 10615 * format described in this standard is used. Non-hierarchical 10616 * formats are outside the scope of this standard. 10617 * 10618 * Therefore we set the HiSup bit here. 10619 * 10620 * The reponse format is 2, per SPC-3. 10621 */ 10622 inq_ptr->response_format = SID_HiSup | 2; 10623 10624 inq_ptr->additional_length = data_len - 10625 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10626 CTL_DEBUG_PRINT(("additional_length = %d\n", 10627 inq_ptr->additional_length)); 10628 10629 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10630 /* 16 bit addressing */ 10631 if (port_type == CTL_PORT_SCSI) 10632 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10633 /* XXX set the SID_MultiP bit here if we're actually going to 10634 respond on multiple ports */ 10635 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10636 10637 /* 16 bit data bus, synchronous transfers */ 10638 if (port_type == CTL_PORT_SCSI) 10639 inq_ptr->flags = SID_WBus16 | SID_Sync; 10640 /* 10641 * XXX KDM do we want to support tagged queueing on the control 10642 * device at all? 10643 */ 10644 if ((lun == NULL) 10645 || (lun->be_lun->lun_type != T_PROCESSOR)) 10646 inq_ptr->flags |= SID_CmdQue; 10647 /* 10648 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10649 * We have 8 bytes for the vendor name, and 16 bytes for the device 10650 * name and 4 bytes for the revision. 10651 */ 10652 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10653 "vendor")) == NULL) { 10654 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10655 } else { 10656 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10657 strncpy(inq_ptr->vendor, val, 10658 min(sizeof(inq_ptr->vendor), strlen(val))); 10659 } 10660 if (lun == NULL) { 10661 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10662 sizeof(inq_ptr->product)); 10663 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10664 switch (lun->be_lun->lun_type) { 10665 case T_DIRECT: 10666 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10667 sizeof(inq_ptr->product)); 10668 break; 10669 case T_PROCESSOR: 10670 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10671 sizeof(inq_ptr->product)); 10672 break; 10673 default: 10674 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10675 sizeof(inq_ptr->product)); 10676 break; 10677 } 10678 } else { 10679 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10680 strncpy(inq_ptr->product, val, 10681 min(sizeof(inq_ptr->product), strlen(val))); 10682 } 10683 10684 /* 10685 * XXX make this a macro somewhere so it automatically gets 10686 * incremented when we make changes. 10687 */ 10688 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10689 "revision")) == NULL) { 10690 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10691 } else { 10692 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10693 strncpy(inq_ptr->revision, val, 10694 min(sizeof(inq_ptr->revision), strlen(val))); 10695 } 10696 10697 /* 10698 * For parallel SCSI, we support double transition and single 10699 * transition clocking. We also support QAS (Quick Arbitration 10700 * and Selection) and Information Unit transfers on both the 10701 * control and array devices. 10702 */ 10703 if (port_type == CTL_PORT_SCSI) 10704 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10705 SID_SPI_IUS; 10706 10707 /* SAM-5 (no version claimed) */ 10708 scsi_ulto2b(0x00A0, inq_ptr->version1); 10709 /* SPC-4 (no version claimed) */ 10710 scsi_ulto2b(0x0460, inq_ptr->version2); 10711 if (port_type == CTL_PORT_FC) { 10712 /* FCP-2 ANSI INCITS.350:2003 */ 10713 scsi_ulto2b(0x0917, inq_ptr->version3); 10714 } else if (port_type == CTL_PORT_SCSI) { 10715 /* SPI-4 ANSI INCITS.362:200x */ 10716 scsi_ulto2b(0x0B56, inq_ptr->version3); 10717 } else if (port_type == CTL_PORT_ISCSI) { 10718 /* iSCSI (no version claimed) */ 10719 scsi_ulto2b(0x0960, inq_ptr->version3); 10720 } else if (port_type == CTL_PORT_SAS) { 10721 /* SAS (no version claimed) */ 10722 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10723 } 10724 10725 if (lun == NULL) { 10726 /* SBC-4 (no version claimed) */ 10727 scsi_ulto2b(0x0600, inq_ptr->version4); 10728 } else { 10729 switch (lun->be_lun->lun_type) { 10730 case T_DIRECT: 10731 /* SBC-4 (no version claimed) */ 10732 scsi_ulto2b(0x0600, inq_ptr->version4); 10733 break; 10734 case T_PROCESSOR: 10735 default: 10736 break; 10737 } 10738 } 10739 10740 ctsio->scsi_status = SCSI_STATUS_OK; 10741 if (ctsio->kern_data_len > 0) { 10742 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10743 ctsio->be_move_done = ctl_config_move_done; 10744 ctl_datamove((union ctl_io *)ctsio); 10745 } else { 10746 ctsio->io_hdr.status = CTL_SUCCESS; 10747 ctl_done((union ctl_io *)ctsio); 10748 } 10749 10750 return (CTL_RETVAL_COMPLETE); 10751 } 10752 10753 int 10754 ctl_inquiry(struct ctl_scsiio *ctsio) 10755 { 10756 struct scsi_inquiry *cdb; 10757 int retval; 10758 10759 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10760 10761 cdb = (struct scsi_inquiry *)ctsio->cdb; 10762 if (cdb->byte2 & SI_EVPD) 10763 retval = ctl_inquiry_evpd(ctsio); 10764 else if (cdb->page_code == 0) 10765 retval = ctl_inquiry_std(ctsio); 10766 else { 10767 ctl_set_invalid_field(ctsio, 10768 /*sks_valid*/ 1, 10769 /*command*/ 1, 10770 /*field*/ 2, 10771 /*bit_valid*/ 0, 10772 /*bit*/ 0); 10773 ctl_done((union ctl_io *)ctsio); 10774 return (CTL_RETVAL_COMPLETE); 10775 } 10776 10777 return (retval); 10778 } 10779 10780 /* 10781 * For known CDB types, parse the LBA and length. 10782 */ 10783 static int 10784 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10785 { 10786 if (io->io_hdr.io_type != CTL_IO_SCSI) 10787 return (1); 10788 10789 switch (io->scsiio.cdb[0]) { 10790 case COMPARE_AND_WRITE: { 10791 struct scsi_compare_and_write *cdb; 10792 10793 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10794 10795 *lba = scsi_8btou64(cdb->addr); 10796 *len = cdb->length; 10797 break; 10798 } 10799 case READ_6: 10800 case WRITE_6: { 10801 struct scsi_rw_6 *cdb; 10802 10803 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10804 10805 *lba = scsi_3btoul(cdb->addr); 10806 /* only 5 bits are valid in the most significant address byte */ 10807 *lba &= 0x1fffff; 10808 *len = cdb->length; 10809 break; 10810 } 10811 case READ_10: 10812 case WRITE_10: { 10813 struct scsi_rw_10 *cdb; 10814 10815 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10816 10817 *lba = scsi_4btoul(cdb->addr); 10818 *len = scsi_2btoul(cdb->length); 10819 break; 10820 } 10821 case WRITE_VERIFY_10: { 10822 struct scsi_write_verify_10 *cdb; 10823 10824 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10825 10826 *lba = scsi_4btoul(cdb->addr); 10827 *len = scsi_2btoul(cdb->length); 10828 break; 10829 } 10830 case READ_12: 10831 case WRITE_12: { 10832 struct scsi_rw_12 *cdb; 10833 10834 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10835 10836 *lba = scsi_4btoul(cdb->addr); 10837 *len = scsi_4btoul(cdb->length); 10838 break; 10839 } 10840 case WRITE_VERIFY_12: { 10841 struct scsi_write_verify_12 *cdb; 10842 10843 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10844 10845 *lba = scsi_4btoul(cdb->addr); 10846 *len = scsi_4btoul(cdb->length); 10847 break; 10848 } 10849 case READ_16: 10850 case WRITE_16: 10851 case WRITE_ATOMIC_16: { 10852 struct scsi_rw_16 *cdb; 10853 10854 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10855 10856 *lba = scsi_8btou64(cdb->addr); 10857 *len = scsi_4btoul(cdb->length); 10858 break; 10859 } 10860 case WRITE_VERIFY_16: { 10861 struct scsi_write_verify_16 *cdb; 10862 10863 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10864 10865 *lba = scsi_8btou64(cdb->addr); 10866 *len = scsi_4btoul(cdb->length); 10867 break; 10868 } 10869 case WRITE_SAME_10: { 10870 struct scsi_write_same_10 *cdb; 10871 10872 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10873 10874 *lba = scsi_4btoul(cdb->addr); 10875 *len = scsi_2btoul(cdb->length); 10876 break; 10877 } 10878 case WRITE_SAME_16: { 10879 struct scsi_write_same_16 *cdb; 10880 10881 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10882 10883 *lba = scsi_8btou64(cdb->addr); 10884 *len = scsi_4btoul(cdb->length); 10885 break; 10886 } 10887 case VERIFY_10: { 10888 struct scsi_verify_10 *cdb; 10889 10890 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10891 10892 *lba = scsi_4btoul(cdb->addr); 10893 *len = scsi_2btoul(cdb->length); 10894 break; 10895 } 10896 case VERIFY_12: { 10897 struct scsi_verify_12 *cdb; 10898 10899 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10900 10901 *lba = scsi_4btoul(cdb->addr); 10902 *len = scsi_4btoul(cdb->length); 10903 break; 10904 } 10905 case VERIFY_16: { 10906 struct scsi_verify_16 *cdb; 10907 10908 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10909 10910 *lba = scsi_8btou64(cdb->addr); 10911 *len = scsi_4btoul(cdb->length); 10912 break; 10913 } 10914 case UNMAP: { 10915 *lba = 0; 10916 *len = UINT64_MAX; 10917 break; 10918 } 10919 default: 10920 return (1); 10921 break; /* NOTREACHED */ 10922 } 10923 10924 return (0); 10925 } 10926 10927 static ctl_action 10928 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10929 { 10930 uint64_t endlba1, endlba2; 10931 10932 endlba1 = lba1 + len1 - 1; 10933 endlba2 = lba2 + len2 - 1; 10934 10935 if ((endlba1 < lba2) 10936 || (endlba2 < lba1)) 10937 return (CTL_ACTION_PASS); 10938 else 10939 return (CTL_ACTION_BLOCK); 10940 } 10941 10942 static int 10943 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10944 { 10945 struct ctl_ptr_len_flags *ptrlen; 10946 struct scsi_unmap_desc *buf, *end, *range; 10947 uint64_t lba; 10948 uint32_t len; 10949 10950 /* If not UNMAP -- go other way. */ 10951 if (io->io_hdr.io_type != CTL_IO_SCSI || 10952 io->scsiio.cdb[0] != UNMAP) 10953 return (CTL_ACTION_ERROR); 10954 10955 /* If UNMAP without data -- block and wait for data. */ 10956 ptrlen = (struct ctl_ptr_len_flags *) 10957 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10958 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10959 ptrlen->ptr == NULL) 10960 return (CTL_ACTION_BLOCK); 10961 10962 /* UNMAP with data -- check for collision. */ 10963 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10964 end = buf + ptrlen->len / sizeof(*buf); 10965 for (range = buf; range < end; range++) { 10966 lba = scsi_8btou64(range->lba); 10967 len = scsi_4btoul(range->length); 10968 if ((lba < lba2 + len2) && (lba + len > lba2)) 10969 return (CTL_ACTION_BLOCK); 10970 } 10971 return (CTL_ACTION_PASS); 10972 } 10973 10974 static ctl_action 10975 ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10976 { 10977 uint64_t lba1, lba2; 10978 uint64_t len1, len2; 10979 int retval; 10980 10981 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10982 return (CTL_ACTION_ERROR); 10983 10984 retval = ctl_extent_check_unmap(io2, lba1, len1); 10985 if (retval != CTL_ACTION_ERROR) 10986 return (retval); 10987 10988 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10989 return (CTL_ACTION_ERROR); 10990 10991 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10992 } 10993 10994 static ctl_action 10995 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10996 union ctl_io *ooa_io) 10997 { 10998 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10999 ctl_serialize_action *serialize_row; 11000 11001 /* 11002 * The initiator attempted multiple untagged commands at the same 11003 * time. Can't do that. 11004 */ 11005 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11006 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11007 && ((pending_io->io_hdr.nexus.targ_port == 11008 ooa_io->io_hdr.nexus.targ_port) 11009 && (pending_io->io_hdr.nexus.initid.id == 11010 ooa_io->io_hdr.nexus.initid.id)) 11011 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11012 return (CTL_ACTION_OVERLAP); 11013 11014 /* 11015 * The initiator attempted to send multiple tagged commands with 11016 * the same ID. (It's fine if different initiators have the same 11017 * tag ID.) 11018 * 11019 * Even if all of those conditions are true, we don't kill the I/O 11020 * if the command ahead of us has been aborted. We won't end up 11021 * sending it to the FETD, and it's perfectly legal to resend a 11022 * command with the same tag number as long as the previous 11023 * instance of this tag number has been aborted somehow. 11024 */ 11025 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11026 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11027 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11028 && ((pending_io->io_hdr.nexus.targ_port == 11029 ooa_io->io_hdr.nexus.targ_port) 11030 && (pending_io->io_hdr.nexus.initid.id == 11031 ooa_io->io_hdr.nexus.initid.id)) 11032 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11033 return (CTL_ACTION_OVERLAP_TAG); 11034 11035 /* 11036 * If we get a head of queue tag, SAM-3 says that we should 11037 * immediately execute it. 11038 * 11039 * What happens if this command would normally block for some other 11040 * reason? e.g. a request sense with a head of queue tag 11041 * immediately after a write. Normally that would block, but this 11042 * will result in its getting executed immediately... 11043 * 11044 * We currently return "pass" instead of "skip", so we'll end up 11045 * going through the rest of the queue to check for overlapped tags. 11046 * 11047 * XXX KDM check for other types of blockage first?? 11048 */ 11049 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11050 return (CTL_ACTION_PASS); 11051 11052 /* 11053 * Ordered tags have to block until all items ahead of them 11054 * have completed. If we get called with an ordered tag, we always 11055 * block, if something else is ahead of us in the queue. 11056 */ 11057 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11058 return (CTL_ACTION_BLOCK); 11059 11060 /* 11061 * Simple tags get blocked until all head of queue and ordered tags 11062 * ahead of them have completed. I'm lumping untagged commands in 11063 * with simple tags here. XXX KDM is that the right thing to do? 11064 */ 11065 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11066 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11067 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11068 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11069 return (CTL_ACTION_BLOCK); 11070 11071 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11072 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11073 11074 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11075 11076 switch (serialize_row[pending_entry->seridx]) { 11077 case CTL_SER_BLOCK: 11078 return (CTL_ACTION_BLOCK); 11079 case CTL_SER_EXTENT: 11080 return (ctl_extent_check(pending_io, ooa_io)); 11081 case CTL_SER_EXTENTOPT: 11082 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11083 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11084 return (ctl_extent_check(pending_io, ooa_io)); 11085 /* FALLTHROUGH */ 11086 case CTL_SER_PASS: 11087 return (CTL_ACTION_PASS); 11088 case CTL_SER_BLOCKOPT: 11089 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11090 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11091 return (CTL_ACTION_BLOCK); 11092 return (CTL_ACTION_PASS); 11093 case CTL_SER_SKIP: 11094 return (CTL_ACTION_SKIP); 11095 default: 11096 panic("invalid serialization value %d", 11097 serialize_row[pending_entry->seridx]); 11098 } 11099 11100 return (CTL_ACTION_ERROR); 11101 } 11102 11103 /* 11104 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11105 * Assumptions: 11106 * - pending_io is generally either incoming, or on the blocked queue 11107 * - starting I/O is the I/O we want to start the check with. 11108 */ 11109 static ctl_action 11110 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11111 union ctl_io *starting_io) 11112 { 11113 union ctl_io *ooa_io; 11114 ctl_action action; 11115 11116 mtx_assert(&lun->lun_lock, MA_OWNED); 11117 11118 /* 11119 * Run back along the OOA queue, starting with the current 11120 * blocked I/O and going through every I/O before it on the 11121 * queue. If starting_io is NULL, we'll just end up returning 11122 * CTL_ACTION_PASS. 11123 */ 11124 for (ooa_io = starting_io; ooa_io != NULL; 11125 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11126 ooa_links)){ 11127 11128 /* 11129 * This routine just checks to see whether 11130 * cur_blocked is blocked by ooa_io, which is ahead 11131 * of it in the queue. It doesn't queue/dequeue 11132 * cur_blocked. 11133 */ 11134 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11135 switch (action) { 11136 case CTL_ACTION_BLOCK: 11137 case CTL_ACTION_OVERLAP: 11138 case CTL_ACTION_OVERLAP_TAG: 11139 case CTL_ACTION_SKIP: 11140 case CTL_ACTION_ERROR: 11141 return (action); 11142 break; /* NOTREACHED */ 11143 case CTL_ACTION_PASS: 11144 break; 11145 default: 11146 panic("invalid action %d", action); 11147 break; /* NOTREACHED */ 11148 } 11149 } 11150 11151 return (CTL_ACTION_PASS); 11152 } 11153 11154 /* 11155 * Assumptions: 11156 * - An I/O has just completed, and has been removed from the per-LUN OOA 11157 * queue, so some items on the blocked queue may now be unblocked. 11158 */ 11159 static int 11160 ctl_check_blocked(struct ctl_lun *lun) 11161 { 11162 union ctl_io *cur_blocked, *next_blocked; 11163 11164 mtx_assert(&lun->lun_lock, MA_OWNED); 11165 11166 /* 11167 * Run forward from the head of the blocked queue, checking each 11168 * entry against the I/Os prior to it on the OOA queue to see if 11169 * there is still any blockage. 11170 * 11171 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11172 * with our removing a variable on it while it is traversing the 11173 * list. 11174 */ 11175 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11176 cur_blocked != NULL; cur_blocked = next_blocked) { 11177 union ctl_io *prev_ooa; 11178 ctl_action action; 11179 11180 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11181 blocked_links); 11182 11183 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11184 ctl_ooaq, ooa_links); 11185 11186 /* 11187 * If cur_blocked happens to be the first item in the OOA 11188 * queue now, prev_ooa will be NULL, and the action 11189 * returned will just be CTL_ACTION_PASS. 11190 */ 11191 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11192 11193 switch (action) { 11194 case CTL_ACTION_BLOCK: 11195 /* Nothing to do here, still blocked */ 11196 break; 11197 case CTL_ACTION_OVERLAP: 11198 case CTL_ACTION_OVERLAP_TAG: 11199 /* 11200 * This shouldn't happen! In theory we've already 11201 * checked this command for overlap... 11202 */ 11203 break; 11204 case CTL_ACTION_PASS: 11205 case CTL_ACTION_SKIP: { 11206 struct ctl_softc *softc; 11207 const struct ctl_cmd_entry *entry; 11208 uint32_t initidx; 11209 int isc_retval; 11210 11211 /* 11212 * The skip case shouldn't happen, this transaction 11213 * should have never made it onto the blocked queue. 11214 */ 11215 /* 11216 * This I/O is no longer blocked, we can remove it 11217 * from the blocked queue. Since this is a TAILQ 11218 * (doubly linked list), we can do O(1) removals 11219 * from any place on the list. 11220 */ 11221 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11222 blocked_links); 11223 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11224 11225 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11226 /* 11227 * Need to send IO back to original side to 11228 * run 11229 */ 11230 union ctl_ha_msg msg_info; 11231 11232 msg_info.hdr.original_sc = 11233 cur_blocked->io_hdr.original_sc; 11234 msg_info.hdr.serializing_sc = cur_blocked; 11235 msg_info.hdr.msg_type = CTL_MSG_R2R; 11236 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11237 &msg_info, sizeof(msg_info), 0)) > 11238 CTL_HA_STATUS_SUCCESS) { 11239 printf("CTL:Check Blocked error from " 11240 "ctl_ha_msg_send %d\n", 11241 isc_retval); 11242 } 11243 break; 11244 } 11245 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11246 softc = control_softc; 11247 11248 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11249 11250 /* 11251 * Check this I/O for LUN state changes that may 11252 * have happened while this command was blocked. 11253 * The LUN state may have been changed by a command 11254 * ahead of us in the queue, so we need to re-check 11255 * for any states that can be caused by SCSI 11256 * commands. 11257 */ 11258 if (ctl_scsiio_lun_check(softc, lun, entry, 11259 &cur_blocked->scsiio) == 0) { 11260 cur_blocked->io_hdr.flags |= 11261 CTL_FLAG_IS_WAS_ON_RTR; 11262 ctl_enqueue_rtr(cur_blocked); 11263 } else 11264 ctl_done(cur_blocked); 11265 break; 11266 } 11267 default: 11268 /* 11269 * This probably shouldn't happen -- we shouldn't 11270 * get CTL_ACTION_ERROR, or anything else. 11271 */ 11272 break; 11273 } 11274 } 11275 11276 return (CTL_RETVAL_COMPLETE); 11277 } 11278 11279 /* 11280 * This routine (with one exception) checks LUN flags that can be set by 11281 * commands ahead of us in the OOA queue. These flags have to be checked 11282 * when a command initially comes in, and when we pull a command off the 11283 * blocked queue and are preparing to execute it. The reason we have to 11284 * check these flags for commands on the blocked queue is that the LUN 11285 * state may have been changed by a command ahead of us while we're on the 11286 * blocked queue. 11287 * 11288 * Ordering is somewhat important with these checks, so please pay 11289 * careful attention to the placement of any new checks. 11290 */ 11291 static int 11292 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11293 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11294 { 11295 int retval; 11296 uint32_t residx; 11297 11298 retval = 0; 11299 11300 mtx_assert(&lun->lun_lock, MA_OWNED); 11301 11302 /* 11303 * If this shelf is a secondary shelf controller, we have to reject 11304 * any media access commands. 11305 */ 11306 #if 0 11307 /* No longer needed for HA */ 11308 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11309 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11310 ctl_set_lun_standby(ctsio); 11311 retval = 1; 11312 goto bailout; 11313 } 11314 #endif 11315 11316 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11317 if (lun->flags & CTL_LUN_READONLY) { 11318 ctl_set_sense(ctsio, /*current_error*/ 1, 11319 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11320 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11321 retval = 1; 11322 goto bailout; 11323 } 11324 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11325 .eca_and_aen & SCP_SWP) != 0) { 11326 ctl_set_sense(ctsio, /*current_error*/ 1, 11327 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11328 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11329 retval = 1; 11330 goto bailout; 11331 } 11332 } 11333 11334 /* 11335 * Check for a reservation conflict. If this command isn't allowed 11336 * even on reserved LUNs, and if this initiator isn't the one who 11337 * reserved us, reject the command with a reservation conflict. 11338 */ 11339 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11340 if ((lun->flags & CTL_LUN_RESERVED) 11341 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11342 if (lun->res_idx != residx) { 11343 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11344 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11345 retval = 1; 11346 goto bailout; 11347 } 11348 } 11349 11350 if ((lun->flags & CTL_LUN_PR_RESERVED) 11351 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11352 /* 11353 * if we aren't registered or it's a res holder type 11354 * reservation and this isn't the res holder then set a 11355 * conflict. 11356 * NOTE: Commands which might be allowed on write exclusive 11357 * type reservations are checked in the particular command 11358 * for a conflict. Read and SSU are the only ones. 11359 */ 11360 if (lun->pr_keys[residx] == 0 11361 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11362 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11363 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11364 retval = 1; 11365 goto bailout; 11366 } 11367 11368 } 11369 11370 if ((lun->flags & CTL_LUN_OFFLINE) 11371 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11372 ctl_set_lun_not_ready(ctsio); 11373 retval = 1; 11374 goto bailout; 11375 } 11376 11377 /* 11378 * If the LUN is stopped, see if this particular command is allowed 11379 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11380 */ 11381 if ((lun->flags & CTL_LUN_STOPPED) 11382 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11383 /* "Logical unit not ready, initializing cmd. required" */ 11384 ctl_set_lun_stopped(ctsio); 11385 retval = 1; 11386 goto bailout; 11387 } 11388 11389 if ((lun->flags & CTL_LUN_INOPERABLE) 11390 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11391 /* "Medium format corrupted" */ 11392 ctl_set_medium_format_corrupted(ctsio); 11393 retval = 1; 11394 goto bailout; 11395 } 11396 11397 bailout: 11398 return (retval); 11399 11400 } 11401 11402 static void 11403 ctl_failover_io(union ctl_io *io, int have_lock) 11404 { 11405 ctl_set_busy(&io->scsiio); 11406 ctl_done(io); 11407 } 11408 11409 static void 11410 ctl_failover(void) 11411 { 11412 struct ctl_lun *lun; 11413 struct ctl_softc *ctl_softc; 11414 union ctl_io *next_io, *pending_io; 11415 union ctl_io *io; 11416 int lun_idx; 11417 int i; 11418 11419 ctl_softc = control_softc; 11420 11421 mtx_lock(&ctl_softc->ctl_lock); 11422 /* 11423 * Remove any cmds from the other SC from the rtr queue. These 11424 * will obviously only be for LUNs for which we're the primary. 11425 * We can't send status or get/send data for these commands. 11426 * Since they haven't been executed yet, we can just remove them. 11427 * We'll either abort them or delete them below, depending on 11428 * which HA mode we're in. 11429 */ 11430 #ifdef notyet 11431 mtx_lock(&ctl_softc->queue_lock); 11432 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11433 io != NULL; io = next_io) { 11434 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11435 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11436 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11437 ctl_io_hdr, links); 11438 } 11439 mtx_unlock(&ctl_softc->queue_lock); 11440 #endif 11441 11442 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11443 lun = ctl_softc->ctl_luns[lun_idx]; 11444 if (lun==NULL) 11445 continue; 11446 11447 /* 11448 * Processor LUNs are primary on both sides. 11449 * XXX will this always be true? 11450 */ 11451 if (lun->be_lun->lun_type == T_PROCESSOR) 11452 continue; 11453 11454 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11455 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11456 printf("FAILOVER: primary lun %d\n", lun_idx); 11457 /* 11458 * Remove all commands from the other SC. First from the 11459 * blocked queue then from the ooa queue. Once we have 11460 * removed them. Call ctl_check_blocked to see if there 11461 * is anything that can run. 11462 */ 11463 for (io = (union ctl_io *)TAILQ_FIRST( 11464 &lun->blocked_queue); io != NULL; io = next_io) { 11465 11466 next_io = (union ctl_io *)TAILQ_NEXT( 11467 &io->io_hdr, blocked_links); 11468 11469 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11470 TAILQ_REMOVE(&lun->blocked_queue, 11471 &io->io_hdr,blocked_links); 11472 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11473 TAILQ_REMOVE(&lun->ooa_queue, 11474 &io->io_hdr, ooa_links); 11475 11476 ctl_free_io(io); 11477 } 11478 } 11479 11480 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11481 io != NULL; io = next_io) { 11482 11483 next_io = (union ctl_io *)TAILQ_NEXT( 11484 &io->io_hdr, ooa_links); 11485 11486 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11487 11488 TAILQ_REMOVE(&lun->ooa_queue, 11489 &io->io_hdr, 11490 ooa_links); 11491 11492 ctl_free_io(io); 11493 } 11494 } 11495 ctl_check_blocked(lun); 11496 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11497 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11498 11499 printf("FAILOVER: primary lun %d\n", lun_idx); 11500 /* 11501 * Abort all commands from the other SC. We can't 11502 * send status back for them now. These should get 11503 * cleaned up when they are completed or come out 11504 * for a datamove operation. 11505 */ 11506 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11507 io != NULL; io = next_io) { 11508 next_io = (union ctl_io *)TAILQ_NEXT( 11509 &io->io_hdr, ooa_links); 11510 11511 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11512 io->io_hdr.flags |= CTL_FLAG_ABORT; 11513 } 11514 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11515 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11516 11517 printf("FAILOVER: secondary lun %d\n", lun_idx); 11518 11519 lun->flags |= CTL_LUN_PRIMARY_SC; 11520 11521 /* 11522 * We send all I/O that was sent to this controller 11523 * and redirected to the other side back with 11524 * busy status, and have the initiator retry it. 11525 * Figuring out how much data has been transferred, 11526 * etc. and picking up where we left off would be 11527 * very tricky. 11528 * 11529 * XXX KDM need to remove I/O from the blocked 11530 * queue as well! 11531 */ 11532 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11533 &lun->ooa_queue); pending_io != NULL; 11534 pending_io = next_io) { 11535 11536 next_io = (union ctl_io *)TAILQ_NEXT( 11537 &pending_io->io_hdr, ooa_links); 11538 11539 pending_io->io_hdr.flags &= 11540 ~CTL_FLAG_SENT_2OTHER_SC; 11541 11542 if (pending_io->io_hdr.flags & 11543 CTL_FLAG_IO_ACTIVE) { 11544 pending_io->io_hdr.flags |= 11545 CTL_FLAG_FAILOVER; 11546 } else { 11547 ctl_set_busy(&pending_io->scsiio); 11548 ctl_done(pending_io); 11549 } 11550 } 11551 11552 /* 11553 * Build Unit Attention 11554 */ 11555 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11556 lun->pending_ua[i] |= 11557 CTL_UA_ASYM_ACC_CHANGE; 11558 } 11559 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11560 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11561 printf("FAILOVER: secondary lun %d\n", lun_idx); 11562 /* 11563 * if the first io on the OOA is not on the RtR queue 11564 * add it. 11565 */ 11566 lun->flags |= CTL_LUN_PRIMARY_SC; 11567 11568 pending_io = (union ctl_io *)TAILQ_FIRST( 11569 &lun->ooa_queue); 11570 if (pending_io==NULL) { 11571 printf("Nothing on OOA queue\n"); 11572 continue; 11573 } 11574 11575 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11576 if ((pending_io->io_hdr.flags & 11577 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11578 pending_io->io_hdr.flags |= 11579 CTL_FLAG_IS_WAS_ON_RTR; 11580 ctl_enqueue_rtr(pending_io); 11581 } 11582 #if 0 11583 else 11584 { 11585 printf("Tag 0x%04x is running\n", 11586 pending_io->scsiio.tag_num); 11587 } 11588 #endif 11589 11590 next_io = (union ctl_io *)TAILQ_NEXT( 11591 &pending_io->io_hdr, ooa_links); 11592 for (pending_io=next_io; pending_io != NULL; 11593 pending_io = next_io) { 11594 pending_io->io_hdr.flags &= 11595 ~CTL_FLAG_SENT_2OTHER_SC; 11596 next_io = (union ctl_io *)TAILQ_NEXT( 11597 &pending_io->io_hdr, ooa_links); 11598 if (pending_io->io_hdr.flags & 11599 CTL_FLAG_IS_WAS_ON_RTR) { 11600 #if 0 11601 printf("Tag 0x%04x is running\n", 11602 pending_io->scsiio.tag_num); 11603 #endif 11604 continue; 11605 } 11606 11607 switch (ctl_check_ooa(lun, pending_io, 11608 (union ctl_io *)TAILQ_PREV( 11609 &pending_io->io_hdr, ctl_ooaq, 11610 ooa_links))) { 11611 11612 case CTL_ACTION_BLOCK: 11613 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11614 &pending_io->io_hdr, 11615 blocked_links); 11616 pending_io->io_hdr.flags |= 11617 CTL_FLAG_BLOCKED; 11618 break; 11619 case CTL_ACTION_PASS: 11620 case CTL_ACTION_SKIP: 11621 pending_io->io_hdr.flags |= 11622 CTL_FLAG_IS_WAS_ON_RTR; 11623 ctl_enqueue_rtr(pending_io); 11624 break; 11625 case CTL_ACTION_OVERLAP: 11626 ctl_set_overlapped_cmd( 11627 (struct ctl_scsiio *)pending_io); 11628 ctl_done(pending_io); 11629 break; 11630 case CTL_ACTION_OVERLAP_TAG: 11631 ctl_set_overlapped_tag( 11632 (struct ctl_scsiio *)pending_io, 11633 pending_io->scsiio.tag_num & 0xff); 11634 ctl_done(pending_io); 11635 break; 11636 case CTL_ACTION_ERROR: 11637 default: 11638 ctl_set_internal_failure( 11639 (struct ctl_scsiio *)pending_io, 11640 0, // sks_valid 11641 0); //retry count 11642 ctl_done(pending_io); 11643 break; 11644 } 11645 } 11646 11647 /* 11648 * Build Unit Attention 11649 */ 11650 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11651 lun->pending_ua[i] |= 11652 CTL_UA_ASYM_ACC_CHANGE; 11653 } 11654 } else { 11655 panic("Unhandled HA mode failover, LUN flags = %#x, " 11656 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11657 } 11658 } 11659 ctl_pause_rtr = 0; 11660 mtx_unlock(&ctl_softc->ctl_lock); 11661 } 11662 11663 static int 11664 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11665 { 11666 struct ctl_lun *lun; 11667 const struct ctl_cmd_entry *entry; 11668 uint32_t initidx, targ_lun; 11669 int retval; 11670 11671 retval = 0; 11672 11673 lun = NULL; 11674 11675 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11676 if ((targ_lun < CTL_MAX_LUNS) 11677 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11678 lun = ctl_softc->ctl_luns[targ_lun]; 11679 /* 11680 * If the LUN is invalid, pretend that it doesn't exist. 11681 * It will go away as soon as all pending I/O has been 11682 * completed. 11683 */ 11684 if (lun->flags & CTL_LUN_DISABLED) { 11685 lun = NULL; 11686 } else { 11687 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11688 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11689 lun->be_lun; 11690 if (lun->be_lun->lun_type == T_PROCESSOR) { 11691 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11692 } 11693 11694 /* 11695 * Every I/O goes into the OOA queue for a 11696 * particular LUN, and stays there until completion. 11697 */ 11698 mtx_lock(&lun->lun_lock); 11699 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11700 ooa_links); 11701 } 11702 } else { 11703 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11704 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11705 } 11706 11707 /* Get command entry and return error if it is unsuppotyed. */ 11708 entry = ctl_validate_command(ctsio); 11709 if (entry == NULL) { 11710 if (lun) 11711 mtx_unlock(&lun->lun_lock); 11712 return (retval); 11713 } 11714 11715 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11716 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11717 11718 /* 11719 * Check to see whether we can send this command to LUNs that don't 11720 * exist. This should pretty much only be the case for inquiry 11721 * and request sense. Further checks, below, really require having 11722 * a LUN, so we can't really check the command anymore. Just put 11723 * it on the rtr queue. 11724 */ 11725 if (lun == NULL) { 11726 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11727 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11728 ctl_enqueue_rtr((union ctl_io *)ctsio); 11729 return (retval); 11730 } 11731 11732 ctl_set_unsupported_lun(ctsio); 11733 ctl_done((union ctl_io *)ctsio); 11734 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11735 return (retval); 11736 } else { 11737 /* 11738 * Make sure we support this particular command on this LUN. 11739 * e.g., we don't support writes to the control LUN. 11740 */ 11741 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11742 mtx_unlock(&lun->lun_lock); 11743 ctl_set_invalid_opcode(ctsio); 11744 ctl_done((union ctl_io *)ctsio); 11745 return (retval); 11746 } 11747 } 11748 11749 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11750 11751 #ifdef CTL_WITH_CA 11752 /* 11753 * If we've got a request sense, it'll clear the contingent 11754 * allegiance condition. Otherwise, if we have a CA condition for 11755 * this initiator, clear it, because it sent down a command other 11756 * than request sense. 11757 */ 11758 if ((ctsio->cdb[0] != REQUEST_SENSE) 11759 && (ctl_is_set(lun->have_ca, initidx))) 11760 ctl_clear_mask(lun->have_ca, initidx); 11761 #endif 11762 11763 /* 11764 * If the command has this flag set, it handles its own unit 11765 * attention reporting, we shouldn't do anything. Otherwise we 11766 * check for any pending unit attentions, and send them back to the 11767 * initiator. We only do this when a command initially comes in, 11768 * not when we pull it off the blocked queue. 11769 * 11770 * According to SAM-3, section 5.3.2, the order that things get 11771 * presented back to the host is basically unit attentions caused 11772 * by some sort of reset event, busy status, reservation conflicts 11773 * or task set full, and finally any other status. 11774 * 11775 * One issue here is that some of the unit attentions we report 11776 * don't fall into the "reset" category (e.g. "reported luns data 11777 * has changed"). So reporting it here, before the reservation 11778 * check, may be technically wrong. I guess the only thing to do 11779 * would be to check for and report the reset events here, and then 11780 * check for the other unit attention types after we check for a 11781 * reservation conflict. 11782 * 11783 * XXX KDM need to fix this 11784 */ 11785 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11786 ctl_ua_type ua_type; 11787 11788 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11789 scsi_sense_data_type sense_format; 11790 11791 if (lun != NULL) 11792 sense_format = (lun->flags & 11793 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11794 SSD_TYPE_FIXED; 11795 else 11796 sense_format = SSD_TYPE_FIXED; 11797 11798 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11799 &ctsio->sense_data, sense_format); 11800 if (ua_type != CTL_UA_NONE) { 11801 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11802 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11803 CTL_AUTOSENSE; 11804 ctsio->sense_len = SSD_FULL_SIZE; 11805 mtx_unlock(&lun->lun_lock); 11806 ctl_done((union ctl_io *)ctsio); 11807 return (retval); 11808 } 11809 } 11810 } 11811 11812 11813 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11814 mtx_unlock(&lun->lun_lock); 11815 ctl_done((union ctl_io *)ctsio); 11816 return (retval); 11817 } 11818 11819 /* 11820 * XXX CHD this is where we want to send IO to other side if 11821 * this LUN is secondary on this SC. We will need to make a copy 11822 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11823 * the copy we send as FROM_OTHER. 11824 * We also need to stuff the address of the original IO so we can 11825 * find it easily. Something similar will need be done on the other 11826 * side so when we are done we can find the copy. 11827 */ 11828 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11829 union ctl_ha_msg msg_info; 11830 int isc_retval; 11831 11832 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11833 11834 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11835 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11836 #if 0 11837 printf("1. ctsio %p\n", ctsio); 11838 #endif 11839 msg_info.hdr.serializing_sc = NULL; 11840 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11841 msg_info.scsi.tag_num = ctsio->tag_num; 11842 msg_info.scsi.tag_type = ctsio->tag_type; 11843 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11844 11845 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11846 11847 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11848 (void *)&msg_info, sizeof(msg_info), 0)) > 11849 CTL_HA_STATUS_SUCCESS) { 11850 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11851 isc_retval); 11852 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11853 } else { 11854 #if 0 11855 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11856 #endif 11857 } 11858 11859 /* 11860 * XXX KDM this I/O is off the incoming queue, but hasn't 11861 * been inserted on any other queue. We may need to come 11862 * up with a holding queue while we wait for serialization 11863 * so that we have an idea of what we're waiting for from 11864 * the other side. 11865 */ 11866 mtx_unlock(&lun->lun_lock); 11867 return (retval); 11868 } 11869 11870 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11871 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11872 ctl_ooaq, ooa_links))) { 11873 case CTL_ACTION_BLOCK: 11874 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11875 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11876 blocked_links); 11877 mtx_unlock(&lun->lun_lock); 11878 return (retval); 11879 case CTL_ACTION_PASS: 11880 case CTL_ACTION_SKIP: 11881 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11882 mtx_unlock(&lun->lun_lock); 11883 ctl_enqueue_rtr((union ctl_io *)ctsio); 11884 break; 11885 case CTL_ACTION_OVERLAP: 11886 mtx_unlock(&lun->lun_lock); 11887 ctl_set_overlapped_cmd(ctsio); 11888 ctl_done((union ctl_io *)ctsio); 11889 break; 11890 case CTL_ACTION_OVERLAP_TAG: 11891 mtx_unlock(&lun->lun_lock); 11892 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11893 ctl_done((union ctl_io *)ctsio); 11894 break; 11895 case CTL_ACTION_ERROR: 11896 default: 11897 mtx_unlock(&lun->lun_lock); 11898 ctl_set_internal_failure(ctsio, 11899 /*sks_valid*/ 0, 11900 /*retry_count*/ 0); 11901 ctl_done((union ctl_io *)ctsio); 11902 break; 11903 } 11904 return (retval); 11905 } 11906 11907 const struct ctl_cmd_entry * 11908 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11909 { 11910 const struct ctl_cmd_entry *entry; 11911 int service_action; 11912 11913 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11914 if (sa) 11915 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11916 if (entry->flags & CTL_CMD_FLAG_SA5) { 11917 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11918 entry = &((const struct ctl_cmd_entry *) 11919 entry->execute)[service_action]; 11920 } 11921 return (entry); 11922 } 11923 11924 const struct ctl_cmd_entry * 11925 ctl_validate_command(struct ctl_scsiio *ctsio) 11926 { 11927 const struct ctl_cmd_entry *entry; 11928 int i, sa; 11929 uint8_t diff; 11930 11931 entry = ctl_get_cmd_entry(ctsio, &sa); 11932 if (entry->execute == NULL) { 11933 if (sa) 11934 ctl_set_invalid_field(ctsio, 11935 /*sks_valid*/ 1, 11936 /*command*/ 1, 11937 /*field*/ 1, 11938 /*bit_valid*/ 1, 11939 /*bit*/ 4); 11940 else 11941 ctl_set_invalid_opcode(ctsio); 11942 ctl_done((union ctl_io *)ctsio); 11943 return (NULL); 11944 } 11945 KASSERT(entry->length > 0, 11946 ("Not defined length for command 0x%02x/0x%02x", 11947 ctsio->cdb[0], ctsio->cdb[1])); 11948 for (i = 1; i < entry->length; i++) { 11949 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11950 if (diff == 0) 11951 continue; 11952 ctl_set_invalid_field(ctsio, 11953 /*sks_valid*/ 1, 11954 /*command*/ 1, 11955 /*field*/ i, 11956 /*bit_valid*/ 1, 11957 /*bit*/ fls(diff) - 1); 11958 ctl_done((union ctl_io *)ctsio); 11959 return (NULL); 11960 } 11961 return (entry); 11962 } 11963 11964 static int 11965 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11966 { 11967 11968 switch (lun_type) { 11969 case T_PROCESSOR: 11970 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11971 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11972 return (0); 11973 break; 11974 case T_DIRECT: 11975 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11976 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11977 return (0); 11978 break; 11979 default: 11980 return (0); 11981 } 11982 return (1); 11983 } 11984 11985 static int 11986 ctl_scsiio(struct ctl_scsiio *ctsio) 11987 { 11988 int retval; 11989 const struct ctl_cmd_entry *entry; 11990 11991 retval = CTL_RETVAL_COMPLETE; 11992 11993 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11994 11995 entry = ctl_get_cmd_entry(ctsio, NULL); 11996 11997 /* 11998 * If this I/O has been aborted, just send it straight to 11999 * ctl_done() without executing it. 12000 */ 12001 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 12002 ctl_done((union ctl_io *)ctsio); 12003 goto bailout; 12004 } 12005 12006 /* 12007 * All the checks should have been handled by ctl_scsiio_precheck(). 12008 * We should be clear now to just execute the I/O. 12009 */ 12010 retval = entry->execute(ctsio); 12011 12012 bailout: 12013 return (retval); 12014 } 12015 12016 /* 12017 * Since we only implement one target right now, a bus reset simply resets 12018 * our single target. 12019 */ 12020 static int 12021 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 12022 { 12023 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 12024 } 12025 12026 static int 12027 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 12028 ctl_ua_type ua_type) 12029 { 12030 struct ctl_lun *lun; 12031 int retval; 12032 12033 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12034 union ctl_ha_msg msg_info; 12035 12036 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12037 msg_info.hdr.nexus = io->io_hdr.nexus; 12038 if (ua_type==CTL_UA_TARG_RESET) 12039 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 12040 else 12041 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12042 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12043 msg_info.hdr.original_sc = NULL; 12044 msg_info.hdr.serializing_sc = NULL; 12045 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12046 (void *)&msg_info, sizeof(msg_info), 0)) { 12047 } 12048 } 12049 retval = 0; 12050 12051 mtx_lock(&ctl_softc->ctl_lock); 12052 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 12053 retval += ctl_lun_reset(lun, io, ua_type); 12054 mtx_unlock(&ctl_softc->ctl_lock); 12055 12056 return (retval); 12057 } 12058 12059 /* 12060 * The LUN should always be set. The I/O is optional, and is used to 12061 * distinguish between I/Os sent by this initiator, and by other 12062 * initiators. We set unit attention for initiators other than this one. 12063 * SAM-3 is vague on this point. It does say that a unit attention should 12064 * be established for other initiators when a LUN is reset (see section 12065 * 5.7.3), but it doesn't specifically say that the unit attention should 12066 * be established for this particular initiator when a LUN is reset. Here 12067 * is the relevant text, from SAM-3 rev 8: 12068 * 12069 * 5.7.2 When a SCSI initiator port aborts its own tasks 12070 * 12071 * When a SCSI initiator port causes its own task(s) to be aborted, no 12072 * notification that the task(s) have been aborted shall be returned to 12073 * the SCSI initiator port other than the completion response for the 12074 * command or task management function action that caused the task(s) to 12075 * be aborted and notification(s) associated with related effects of the 12076 * action (e.g., a reset unit attention condition). 12077 * 12078 * XXX KDM for now, we're setting unit attention for all initiators. 12079 */ 12080 static int 12081 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12082 { 12083 union ctl_io *xio; 12084 #if 0 12085 uint32_t initindex; 12086 #endif 12087 int i; 12088 12089 mtx_lock(&lun->lun_lock); 12090 /* 12091 * Run through the OOA queue and abort each I/O. 12092 */ 12093 #if 0 12094 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12095 #endif 12096 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12097 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12098 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12099 } 12100 12101 /* 12102 * This version sets unit attention for every 12103 */ 12104 #if 0 12105 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12106 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12107 if (initindex == i) 12108 continue; 12109 lun->pending_ua[i] |= ua_type; 12110 } 12111 #endif 12112 12113 /* 12114 * A reset (any kind, really) clears reservations established with 12115 * RESERVE/RELEASE. It does not clear reservations established 12116 * with PERSISTENT RESERVE OUT, but we don't support that at the 12117 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12118 * reservations made with the RESERVE/RELEASE commands, because 12119 * those commands are obsolete in SPC-3. 12120 */ 12121 lun->flags &= ~CTL_LUN_RESERVED; 12122 12123 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12124 #ifdef CTL_WITH_CA 12125 ctl_clear_mask(lun->have_ca, i); 12126 #endif 12127 lun->pending_ua[i] |= ua_type; 12128 } 12129 mtx_unlock(&lun->lun_lock); 12130 12131 return (0); 12132 } 12133 12134 static void 12135 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12136 int other_sc) 12137 { 12138 union ctl_io *xio; 12139 12140 mtx_assert(&lun->lun_lock, MA_OWNED); 12141 12142 /* 12143 * Run through the OOA queue and attempt to find the given I/O. 12144 * The target port, initiator ID, tag type and tag number have to 12145 * match the values that we got from the initiator. If we have an 12146 * untagged command to abort, simply abort the first untagged command 12147 * we come to. We only allow one untagged command at a time of course. 12148 */ 12149 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12150 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12151 12152 if ((targ_port == UINT32_MAX || 12153 targ_port == xio->io_hdr.nexus.targ_port) && 12154 (init_id == UINT32_MAX || 12155 init_id == xio->io_hdr.nexus.initid.id)) { 12156 if (targ_port != xio->io_hdr.nexus.targ_port || 12157 init_id != xio->io_hdr.nexus.initid.id) 12158 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12159 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12160 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12161 union ctl_ha_msg msg_info; 12162 12163 msg_info.hdr.nexus = xio->io_hdr.nexus; 12164 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12165 msg_info.task.tag_num = xio->scsiio.tag_num; 12166 msg_info.task.tag_type = xio->scsiio.tag_type; 12167 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12168 msg_info.hdr.original_sc = NULL; 12169 msg_info.hdr.serializing_sc = NULL; 12170 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12171 (void *)&msg_info, sizeof(msg_info), 0); 12172 } 12173 } 12174 } 12175 } 12176 12177 static int 12178 ctl_abort_task_set(union ctl_io *io) 12179 { 12180 struct ctl_softc *softc = control_softc; 12181 struct ctl_lun *lun; 12182 uint32_t targ_lun; 12183 12184 /* 12185 * Look up the LUN. 12186 */ 12187 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12188 mtx_lock(&softc->ctl_lock); 12189 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12190 lun = softc->ctl_luns[targ_lun]; 12191 else { 12192 mtx_unlock(&softc->ctl_lock); 12193 return (1); 12194 } 12195 12196 mtx_lock(&lun->lun_lock); 12197 mtx_unlock(&softc->ctl_lock); 12198 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12199 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12200 io->io_hdr.nexus.initid.id, 12201 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12202 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12203 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12204 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12205 } 12206 mtx_unlock(&lun->lun_lock); 12207 return (0); 12208 } 12209 12210 static int 12211 ctl_i_t_nexus_reset(union ctl_io *io) 12212 { 12213 struct ctl_softc *softc = control_softc; 12214 struct ctl_lun *lun; 12215 uint32_t initindex, residx; 12216 12217 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12218 residx = ctl_get_resindex(&io->io_hdr.nexus); 12219 mtx_lock(&softc->ctl_lock); 12220 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12221 mtx_lock(&lun->lun_lock); 12222 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12223 io->io_hdr.nexus.initid.id, 12224 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12225 #ifdef CTL_WITH_CA 12226 ctl_clear_mask(lun->have_ca, initindex); 12227 #endif 12228 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12229 lun->flags &= ~CTL_LUN_RESERVED; 12230 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12231 mtx_unlock(&lun->lun_lock); 12232 } 12233 mtx_unlock(&softc->ctl_lock); 12234 return (0); 12235 } 12236 12237 static int 12238 ctl_abort_task(union ctl_io *io) 12239 { 12240 union ctl_io *xio; 12241 struct ctl_lun *lun; 12242 struct ctl_softc *ctl_softc; 12243 #if 0 12244 struct sbuf sb; 12245 char printbuf[128]; 12246 #endif 12247 int found; 12248 uint32_t targ_lun; 12249 12250 ctl_softc = control_softc; 12251 found = 0; 12252 12253 /* 12254 * Look up the LUN. 12255 */ 12256 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12257 mtx_lock(&ctl_softc->ctl_lock); 12258 if ((targ_lun < CTL_MAX_LUNS) 12259 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12260 lun = ctl_softc->ctl_luns[targ_lun]; 12261 else { 12262 mtx_unlock(&ctl_softc->ctl_lock); 12263 return (1); 12264 } 12265 12266 #if 0 12267 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12268 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12269 #endif 12270 12271 mtx_lock(&lun->lun_lock); 12272 mtx_unlock(&ctl_softc->ctl_lock); 12273 /* 12274 * Run through the OOA queue and attempt to find the given I/O. 12275 * The target port, initiator ID, tag type and tag number have to 12276 * match the values that we got from the initiator. If we have an 12277 * untagged command to abort, simply abort the first untagged command 12278 * we come to. We only allow one untagged command at a time of course. 12279 */ 12280 #if 0 12281 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12282 #endif 12283 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12284 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12285 #if 0 12286 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12287 12288 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12289 lun->lun, xio->scsiio.tag_num, 12290 xio->scsiio.tag_type, 12291 (xio->io_hdr.blocked_links.tqe_prev 12292 == NULL) ? "" : " BLOCKED", 12293 (xio->io_hdr.flags & 12294 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12295 (xio->io_hdr.flags & 12296 CTL_FLAG_ABORT) ? " ABORT" : "", 12297 (xio->io_hdr.flags & 12298 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12299 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12300 sbuf_finish(&sb); 12301 printf("%s\n", sbuf_data(&sb)); 12302 #endif 12303 12304 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12305 && (xio->io_hdr.nexus.initid.id == 12306 io->io_hdr.nexus.initid.id)) { 12307 /* 12308 * If the abort says that the task is untagged, the 12309 * task in the queue must be untagged. Otherwise, 12310 * we just check to see whether the tag numbers 12311 * match. This is because the QLogic firmware 12312 * doesn't pass back the tag type in an abort 12313 * request. 12314 */ 12315 #if 0 12316 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12317 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12318 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12319 #endif 12320 /* 12321 * XXX KDM we've got problems with FC, because it 12322 * doesn't send down a tag type with aborts. So we 12323 * can only really go by the tag number... 12324 * This may cause problems with parallel SCSI. 12325 * Need to figure that out!! 12326 */ 12327 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12328 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12329 found = 1; 12330 if ((io->io_hdr.flags & 12331 CTL_FLAG_FROM_OTHER_SC) == 0 && 12332 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12333 union ctl_ha_msg msg_info; 12334 12335 io->io_hdr.flags |= 12336 CTL_FLAG_SENT_2OTHER_SC; 12337 msg_info.hdr.nexus = io->io_hdr.nexus; 12338 msg_info.task.task_action = 12339 CTL_TASK_ABORT_TASK; 12340 msg_info.task.tag_num = 12341 io->taskio.tag_num; 12342 msg_info.task.tag_type = 12343 io->taskio.tag_type; 12344 msg_info.hdr.msg_type = 12345 CTL_MSG_MANAGE_TASKS; 12346 msg_info.hdr.original_sc = NULL; 12347 msg_info.hdr.serializing_sc = NULL; 12348 #if 0 12349 printf("Sent Abort to other side\n"); 12350 #endif 12351 if (CTL_HA_STATUS_SUCCESS != 12352 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12353 (void *)&msg_info, 12354 sizeof(msg_info), 0)) { 12355 } 12356 } 12357 #if 0 12358 printf("ctl_abort_task: found I/O to abort\n"); 12359 #endif 12360 break; 12361 } 12362 } 12363 } 12364 mtx_unlock(&lun->lun_lock); 12365 12366 if (found == 0) { 12367 /* 12368 * This isn't really an error. It's entirely possible for 12369 * the abort and command completion to cross on the wire. 12370 * This is more of an informative/diagnostic error. 12371 */ 12372 #if 0 12373 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12374 "%d:%d:%d:%d tag %d type %d\n", 12375 io->io_hdr.nexus.initid.id, 12376 io->io_hdr.nexus.targ_port, 12377 io->io_hdr.nexus.targ_target.id, 12378 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12379 io->taskio.tag_type); 12380 #endif 12381 } 12382 return (0); 12383 } 12384 12385 static void 12386 ctl_run_task(union ctl_io *io) 12387 { 12388 struct ctl_softc *ctl_softc = control_softc; 12389 int retval = 1; 12390 const char *task_desc; 12391 12392 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12393 12394 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12395 ("ctl_run_task: Unextected io_type %d\n", 12396 io->io_hdr.io_type)); 12397 12398 task_desc = ctl_scsi_task_string(&io->taskio); 12399 if (task_desc != NULL) { 12400 #ifdef NEEDTOPORT 12401 csevent_log(CSC_CTL | CSC_SHELF_SW | 12402 CTL_TASK_REPORT, 12403 csevent_LogType_Trace, 12404 csevent_Severity_Information, 12405 csevent_AlertLevel_Green, 12406 csevent_FRU_Firmware, 12407 csevent_FRU_Unknown, 12408 "CTL: received task: %s",task_desc); 12409 #endif 12410 } else { 12411 #ifdef NEEDTOPORT 12412 csevent_log(CSC_CTL | CSC_SHELF_SW | 12413 CTL_TASK_REPORT, 12414 csevent_LogType_Trace, 12415 csevent_Severity_Information, 12416 csevent_AlertLevel_Green, 12417 csevent_FRU_Firmware, 12418 csevent_FRU_Unknown, 12419 "CTL: received unknown task " 12420 "type: %d (%#x)", 12421 io->taskio.task_action, 12422 io->taskio.task_action); 12423 #endif 12424 } 12425 switch (io->taskio.task_action) { 12426 case CTL_TASK_ABORT_TASK: 12427 retval = ctl_abort_task(io); 12428 break; 12429 case CTL_TASK_ABORT_TASK_SET: 12430 case CTL_TASK_CLEAR_TASK_SET: 12431 retval = ctl_abort_task_set(io); 12432 break; 12433 case CTL_TASK_CLEAR_ACA: 12434 break; 12435 case CTL_TASK_I_T_NEXUS_RESET: 12436 retval = ctl_i_t_nexus_reset(io); 12437 break; 12438 case CTL_TASK_LUN_RESET: { 12439 struct ctl_lun *lun; 12440 uint32_t targ_lun; 12441 12442 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12443 mtx_lock(&ctl_softc->ctl_lock); 12444 if ((targ_lun < CTL_MAX_LUNS) 12445 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12446 lun = ctl_softc->ctl_luns[targ_lun]; 12447 else { 12448 mtx_unlock(&ctl_softc->ctl_lock); 12449 retval = 1; 12450 break; 12451 } 12452 12453 if (!(io->io_hdr.flags & 12454 CTL_FLAG_FROM_OTHER_SC)) { 12455 union ctl_ha_msg msg_info; 12456 12457 io->io_hdr.flags |= 12458 CTL_FLAG_SENT_2OTHER_SC; 12459 msg_info.hdr.msg_type = 12460 CTL_MSG_MANAGE_TASKS; 12461 msg_info.hdr.nexus = io->io_hdr.nexus; 12462 msg_info.task.task_action = 12463 CTL_TASK_LUN_RESET; 12464 msg_info.hdr.original_sc = NULL; 12465 msg_info.hdr.serializing_sc = NULL; 12466 if (CTL_HA_STATUS_SUCCESS != 12467 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12468 (void *)&msg_info, 12469 sizeof(msg_info), 0)) { 12470 } 12471 } 12472 12473 retval = ctl_lun_reset(lun, io, 12474 CTL_UA_LUN_RESET); 12475 mtx_unlock(&ctl_softc->ctl_lock); 12476 break; 12477 } 12478 case CTL_TASK_TARGET_RESET: 12479 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12480 break; 12481 case CTL_TASK_BUS_RESET: 12482 retval = ctl_bus_reset(ctl_softc, io); 12483 break; 12484 case CTL_TASK_PORT_LOGIN: 12485 break; 12486 case CTL_TASK_PORT_LOGOUT: 12487 break; 12488 default: 12489 printf("ctl_run_task: got unknown task management event %d\n", 12490 io->taskio.task_action); 12491 break; 12492 } 12493 if (retval == 0) 12494 io->io_hdr.status = CTL_SUCCESS; 12495 else 12496 io->io_hdr.status = CTL_ERROR; 12497 ctl_done(io); 12498 } 12499 12500 /* 12501 * For HA operation. Handle commands that come in from the other 12502 * controller. 12503 */ 12504 static void 12505 ctl_handle_isc(union ctl_io *io) 12506 { 12507 int free_io; 12508 struct ctl_lun *lun; 12509 struct ctl_softc *ctl_softc; 12510 uint32_t targ_lun; 12511 12512 ctl_softc = control_softc; 12513 12514 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12515 lun = ctl_softc->ctl_luns[targ_lun]; 12516 12517 switch (io->io_hdr.msg_type) { 12518 case CTL_MSG_SERIALIZE: 12519 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12520 break; 12521 case CTL_MSG_R2R: { 12522 const struct ctl_cmd_entry *entry; 12523 12524 /* 12525 * This is only used in SER_ONLY mode. 12526 */ 12527 free_io = 0; 12528 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12529 mtx_lock(&lun->lun_lock); 12530 if (ctl_scsiio_lun_check(ctl_softc, lun, 12531 entry, (struct ctl_scsiio *)io) != 0) { 12532 mtx_unlock(&lun->lun_lock); 12533 ctl_done(io); 12534 break; 12535 } 12536 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12537 mtx_unlock(&lun->lun_lock); 12538 ctl_enqueue_rtr(io); 12539 break; 12540 } 12541 case CTL_MSG_FINISH_IO: 12542 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12543 free_io = 0; 12544 ctl_done(io); 12545 } else { 12546 free_io = 1; 12547 mtx_lock(&lun->lun_lock); 12548 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12549 ooa_links); 12550 ctl_check_blocked(lun); 12551 mtx_unlock(&lun->lun_lock); 12552 } 12553 break; 12554 case CTL_MSG_PERS_ACTION: 12555 ctl_hndl_per_res_out_on_other_sc( 12556 (union ctl_ha_msg *)&io->presio.pr_msg); 12557 free_io = 1; 12558 break; 12559 case CTL_MSG_BAD_JUJU: 12560 free_io = 0; 12561 ctl_done(io); 12562 break; 12563 case CTL_MSG_DATAMOVE: 12564 /* Only used in XFER mode */ 12565 free_io = 0; 12566 ctl_datamove_remote(io); 12567 break; 12568 case CTL_MSG_DATAMOVE_DONE: 12569 /* Only used in XFER mode */ 12570 free_io = 0; 12571 io->scsiio.be_move_done(io); 12572 break; 12573 default: 12574 free_io = 1; 12575 printf("%s: Invalid message type %d\n", 12576 __func__, io->io_hdr.msg_type); 12577 break; 12578 } 12579 if (free_io) 12580 ctl_free_io(io); 12581 12582 } 12583 12584 12585 /* 12586 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12587 * there is no match. 12588 */ 12589 static ctl_lun_error_pattern 12590 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12591 { 12592 const struct ctl_cmd_entry *entry; 12593 ctl_lun_error_pattern filtered_pattern, pattern; 12594 12595 pattern = desc->error_pattern; 12596 12597 /* 12598 * XXX KDM we need more data passed into this function to match a 12599 * custom pattern, and we actually need to implement custom pattern 12600 * matching. 12601 */ 12602 if (pattern & CTL_LUN_PAT_CMD) 12603 return (CTL_LUN_PAT_CMD); 12604 12605 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12606 return (CTL_LUN_PAT_ANY); 12607 12608 entry = ctl_get_cmd_entry(ctsio, NULL); 12609 12610 filtered_pattern = entry->pattern & pattern; 12611 12612 /* 12613 * If the user requested specific flags in the pattern (e.g. 12614 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12615 * flags. 12616 * 12617 * If the user did not specify any flags, it doesn't matter whether 12618 * or not the command supports the flags. 12619 */ 12620 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12621 (pattern & ~CTL_LUN_PAT_MASK)) 12622 return (CTL_LUN_PAT_NONE); 12623 12624 /* 12625 * If the user asked for a range check, see if the requested LBA 12626 * range overlaps with this command's LBA range. 12627 */ 12628 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12629 uint64_t lba1; 12630 uint64_t len1; 12631 ctl_action action; 12632 int retval; 12633 12634 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12635 if (retval != 0) 12636 return (CTL_LUN_PAT_NONE); 12637 12638 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12639 desc->lba_range.len); 12640 /* 12641 * A "pass" means that the LBA ranges don't overlap, so 12642 * this doesn't match the user's range criteria. 12643 */ 12644 if (action == CTL_ACTION_PASS) 12645 return (CTL_LUN_PAT_NONE); 12646 } 12647 12648 return (filtered_pattern); 12649 } 12650 12651 static void 12652 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12653 { 12654 struct ctl_error_desc *desc, *desc2; 12655 12656 mtx_assert(&lun->lun_lock, MA_OWNED); 12657 12658 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12659 ctl_lun_error_pattern pattern; 12660 /* 12661 * Check to see whether this particular command matches 12662 * the pattern in the descriptor. 12663 */ 12664 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12665 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12666 continue; 12667 12668 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12669 case CTL_LUN_INJ_ABORTED: 12670 ctl_set_aborted(&io->scsiio); 12671 break; 12672 case CTL_LUN_INJ_MEDIUM_ERR: 12673 ctl_set_medium_error(&io->scsiio); 12674 break; 12675 case CTL_LUN_INJ_UA: 12676 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12677 * OCCURRED */ 12678 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12679 break; 12680 case CTL_LUN_INJ_CUSTOM: 12681 /* 12682 * We're assuming the user knows what he is doing. 12683 * Just copy the sense information without doing 12684 * checks. 12685 */ 12686 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12687 ctl_min(sizeof(desc->custom_sense), 12688 sizeof(io->scsiio.sense_data))); 12689 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12690 io->scsiio.sense_len = SSD_FULL_SIZE; 12691 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12692 break; 12693 case CTL_LUN_INJ_NONE: 12694 default: 12695 /* 12696 * If this is an error injection type we don't know 12697 * about, clear the continuous flag (if it is set) 12698 * so it will get deleted below. 12699 */ 12700 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12701 break; 12702 } 12703 /* 12704 * By default, each error injection action is a one-shot 12705 */ 12706 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12707 continue; 12708 12709 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12710 12711 free(desc, M_CTL); 12712 } 12713 } 12714 12715 #ifdef CTL_IO_DELAY 12716 static void 12717 ctl_datamove_timer_wakeup(void *arg) 12718 { 12719 union ctl_io *io; 12720 12721 io = (union ctl_io *)arg; 12722 12723 ctl_datamove(io); 12724 } 12725 #endif /* CTL_IO_DELAY */ 12726 12727 void 12728 ctl_datamove(union ctl_io *io) 12729 { 12730 void (*fe_datamove)(union ctl_io *io); 12731 12732 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12733 12734 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12735 12736 #ifdef CTL_TIME_IO 12737 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12738 char str[256]; 12739 char path_str[64]; 12740 struct sbuf sb; 12741 12742 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12743 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12744 12745 sbuf_cat(&sb, path_str); 12746 switch (io->io_hdr.io_type) { 12747 case CTL_IO_SCSI: 12748 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12749 sbuf_printf(&sb, "\n"); 12750 sbuf_cat(&sb, path_str); 12751 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12752 io->scsiio.tag_num, io->scsiio.tag_type); 12753 break; 12754 case CTL_IO_TASK: 12755 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12756 "Tag Type: %d\n", io->taskio.task_action, 12757 io->taskio.tag_num, io->taskio.tag_type); 12758 break; 12759 default: 12760 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12761 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12762 break; 12763 } 12764 sbuf_cat(&sb, path_str); 12765 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12766 (intmax_t)time_uptime - io->io_hdr.start_time); 12767 sbuf_finish(&sb); 12768 printf("%s", sbuf_data(&sb)); 12769 } 12770 #endif /* CTL_TIME_IO */ 12771 12772 #ifdef CTL_IO_DELAY 12773 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12774 struct ctl_lun *lun; 12775 12776 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12777 12778 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12779 } else { 12780 struct ctl_lun *lun; 12781 12782 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12783 if ((lun != NULL) 12784 && (lun->delay_info.datamove_delay > 0)) { 12785 struct callout *callout; 12786 12787 callout = (struct callout *)&io->io_hdr.timer_bytes; 12788 callout_init(callout, /*mpsafe*/ 1); 12789 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12790 callout_reset(callout, 12791 lun->delay_info.datamove_delay * hz, 12792 ctl_datamove_timer_wakeup, io); 12793 if (lun->delay_info.datamove_type == 12794 CTL_DELAY_TYPE_ONESHOT) 12795 lun->delay_info.datamove_delay = 0; 12796 return; 12797 } 12798 } 12799 #endif 12800 12801 /* 12802 * This command has been aborted. Set the port status, so we fail 12803 * the data move. 12804 */ 12805 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12806 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12807 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12808 io->io_hdr.nexus.targ_port, 12809 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12810 io->io_hdr.nexus.targ_lun); 12811 io->io_hdr.port_status = 31337; 12812 /* 12813 * Note that the backend, in this case, will get the 12814 * callback in its context. In other cases it may get 12815 * called in the frontend's interrupt thread context. 12816 */ 12817 io->scsiio.be_move_done(io); 12818 return; 12819 } 12820 12821 /* 12822 * If we're in XFER mode and this I/O is from the other shelf 12823 * controller, we need to send the DMA to the other side to 12824 * actually transfer the data to/from the host. In serialize only 12825 * mode the transfer happens below CTL and ctl_datamove() is only 12826 * called on the machine that originally received the I/O. 12827 */ 12828 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12829 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12830 union ctl_ha_msg msg; 12831 uint32_t sg_entries_sent; 12832 int do_sg_copy; 12833 int i; 12834 12835 memset(&msg, 0, sizeof(msg)); 12836 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12837 msg.hdr.original_sc = io->io_hdr.original_sc; 12838 msg.hdr.serializing_sc = io; 12839 msg.hdr.nexus = io->io_hdr.nexus; 12840 msg.dt.flags = io->io_hdr.flags; 12841 /* 12842 * We convert everything into a S/G list here. We can't 12843 * pass by reference, only by value between controllers. 12844 * So we can't pass a pointer to the S/G list, only as many 12845 * S/G entries as we can fit in here. If it's possible for 12846 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12847 * then we need to break this up into multiple transfers. 12848 */ 12849 if (io->scsiio.kern_sg_entries == 0) { 12850 msg.dt.kern_sg_entries = 1; 12851 /* 12852 * If this is in cached memory, flush the cache 12853 * before we send the DMA request to the other 12854 * controller. We want to do this in either the 12855 * read or the write case. The read case is 12856 * straightforward. In the write case, we want to 12857 * make sure nothing is in the local cache that 12858 * could overwrite the DMAed data. 12859 */ 12860 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12861 /* 12862 * XXX KDM use bus_dmamap_sync() here. 12863 */ 12864 } 12865 12866 /* 12867 * Convert to a physical address if this is a 12868 * virtual address. 12869 */ 12870 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12871 msg.dt.sg_list[0].addr = 12872 io->scsiio.kern_data_ptr; 12873 } else { 12874 /* 12875 * XXX KDM use busdma here! 12876 */ 12877 #if 0 12878 msg.dt.sg_list[0].addr = (void *) 12879 vtophys(io->scsiio.kern_data_ptr); 12880 #endif 12881 } 12882 12883 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12884 do_sg_copy = 0; 12885 } else { 12886 struct ctl_sg_entry *sgl; 12887 12888 do_sg_copy = 1; 12889 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12890 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12891 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12892 /* 12893 * XXX KDM use bus_dmamap_sync() here. 12894 */ 12895 } 12896 } 12897 12898 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12899 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12900 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12901 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12902 msg.dt.sg_sequence = 0; 12903 12904 /* 12905 * Loop until we've sent all of the S/G entries. On the 12906 * other end, we'll recompose these S/G entries into one 12907 * contiguous list before passing it to the 12908 */ 12909 for (sg_entries_sent = 0; sg_entries_sent < 12910 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12911 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12912 sizeof(msg.dt.sg_list[0])), 12913 msg.dt.kern_sg_entries - sg_entries_sent); 12914 12915 if (do_sg_copy != 0) { 12916 struct ctl_sg_entry *sgl; 12917 int j; 12918 12919 sgl = (struct ctl_sg_entry *) 12920 io->scsiio.kern_data_ptr; 12921 /* 12922 * If this is in cached memory, flush the cache 12923 * before we send the DMA request to the other 12924 * controller. We want to do this in either 12925 * the * read or the write case. The read 12926 * case is straightforward. In the write 12927 * case, we want to make sure nothing is 12928 * in the local cache that could overwrite 12929 * the DMAed data. 12930 */ 12931 12932 for (i = sg_entries_sent, j = 0; 12933 i < msg.dt.cur_sg_entries; i++, j++) { 12934 if ((io->io_hdr.flags & 12935 CTL_FLAG_NO_DATASYNC) == 0) { 12936 /* 12937 * XXX KDM use bus_dmamap_sync() 12938 */ 12939 } 12940 if ((io->io_hdr.flags & 12941 CTL_FLAG_BUS_ADDR) == 0) { 12942 /* 12943 * XXX KDM use busdma. 12944 */ 12945 #if 0 12946 msg.dt.sg_list[j].addr =(void *) 12947 vtophys(sgl[i].addr); 12948 #endif 12949 } else { 12950 msg.dt.sg_list[j].addr = 12951 sgl[i].addr; 12952 } 12953 msg.dt.sg_list[j].len = sgl[i].len; 12954 } 12955 } 12956 12957 sg_entries_sent += msg.dt.cur_sg_entries; 12958 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12959 msg.dt.sg_last = 1; 12960 else 12961 msg.dt.sg_last = 0; 12962 12963 /* 12964 * XXX KDM drop and reacquire the lock here? 12965 */ 12966 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12967 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12968 /* 12969 * XXX do something here. 12970 */ 12971 } 12972 12973 msg.dt.sent_sg_entries = sg_entries_sent; 12974 } 12975 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12976 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12977 ctl_failover_io(io, /*have_lock*/ 0); 12978 12979 } else { 12980 12981 /* 12982 * Lookup the fe_datamove() function for this particular 12983 * front end. 12984 */ 12985 fe_datamove = 12986 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12987 12988 fe_datamove(io); 12989 } 12990 } 12991 12992 static void 12993 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12994 { 12995 union ctl_ha_msg msg; 12996 int isc_status; 12997 12998 memset(&msg, 0, sizeof(msg)); 12999 13000 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 13001 msg.hdr.original_sc = io; 13002 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13003 msg.hdr.nexus = io->io_hdr.nexus; 13004 msg.hdr.status = io->io_hdr.status; 13005 msg.scsi.tag_num = io->scsiio.tag_num; 13006 msg.scsi.tag_type = io->scsiio.tag_type; 13007 msg.scsi.scsi_status = io->scsiio.scsi_status; 13008 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13009 sizeof(io->scsiio.sense_data)); 13010 msg.scsi.sense_len = io->scsiio.sense_len; 13011 msg.scsi.sense_residual = io->scsiio.sense_residual; 13012 msg.scsi.fetd_status = io->io_hdr.port_status; 13013 msg.scsi.residual = io->scsiio.residual; 13014 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13015 13016 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13017 ctl_failover_io(io, /*have_lock*/ have_lock); 13018 return; 13019 } 13020 13021 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 13022 if (isc_status > CTL_HA_STATUS_SUCCESS) { 13023 /* XXX do something if this fails */ 13024 } 13025 13026 } 13027 13028 /* 13029 * The DMA to the remote side is done, now we need to tell the other side 13030 * we're done so it can continue with its data movement. 13031 */ 13032 static void 13033 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 13034 { 13035 union ctl_io *io; 13036 13037 io = rq->context; 13038 13039 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13040 printf("%s: ISC DMA write failed with error %d", __func__, 13041 rq->ret); 13042 ctl_set_internal_failure(&io->scsiio, 13043 /*sks_valid*/ 1, 13044 /*retry_count*/ rq->ret); 13045 } 13046 13047 ctl_dt_req_free(rq); 13048 13049 /* 13050 * In this case, we had to malloc the memory locally. Free it. 13051 */ 13052 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13053 int i; 13054 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13055 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13056 } 13057 /* 13058 * The data is in local and remote memory, so now we need to send 13059 * status (good or back) back to the other side. 13060 */ 13061 ctl_send_datamove_done(io, /*have_lock*/ 0); 13062 } 13063 13064 /* 13065 * We've moved the data from the host/controller into local memory. Now we 13066 * need to push it over to the remote controller's memory. 13067 */ 13068 static int 13069 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13070 { 13071 int retval; 13072 13073 retval = 0; 13074 13075 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13076 ctl_datamove_remote_write_cb); 13077 13078 return (retval); 13079 } 13080 13081 static void 13082 ctl_datamove_remote_write(union ctl_io *io) 13083 { 13084 int retval; 13085 void (*fe_datamove)(union ctl_io *io); 13086 13087 /* 13088 * - Get the data from the host/HBA into local memory. 13089 * - DMA memory from the local controller to the remote controller. 13090 * - Send status back to the remote controller. 13091 */ 13092 13093 retval = ctl_datamove_remote_sgl_setup(io); 13094 if (retval != 0) 13095 return; 13096 13097 /* Switch the pointer over so the FETD knows what to do */ 13098 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13099 13100 /* 13101 * Use a custom move done callback, since we need to send completion 13102 * back to the other controller, not to the backend on this side. 13103 */ 13104 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13105 13106 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13107 13108 fe_datamove(io); 13109 13110 return; 13111 13112 } 13113 13114 static int 13115 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13116 { 13117 #if 0 13118 char str[256]; 13119 char path_str[64]; 13120 struct sbuf sb; 13121 #endif 13122 13123 /* 13124 * In this case, we had to malloc the memory locally. Free it. 13125 */ 13126 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13127 int i; 13128 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13129 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13130 } 13131 13132 #if 0 13133 scsi_path_string(io, path_str, sizeof(path_str)); 13134 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13135 sbuf_cat(&sb, path_str); 13136 scsi_command_string(&io->scsiio, NULL, &sb); 13137 sbuf_printf(&sb, "\n"); 13138 sbuf_cat(&sb, path_str); 13139 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13140 io->scsiio.tag_num, io->scsiio.tag_type); 13141 sbuf_cat(&sb, path_str); 13142 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13143 io->io_hdr.flags, io->io_hdr.status); 13144 sbuf_finish(&sb); 13145 printk("%s", sbuf_data(&sb)); 13146 #endif 13147 13148 13149 /* 13150 * The read is done, now we need to send status (good or bad) back 13151 * to the other side. 13152 */ 13153 ctl_send_datamove_done(io, /*have_lock*/ 0); 13154 13155 return (0); 13156 } 13157 13158 static void 13159 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13160 { 13161 union ctl_io *io; 13162 void (*fe_datamove)(union ctl_io *io); 13163 13164 io = rq->context; 13165 13166 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13167 printf("%s: ISC DMA read failed with error %d", __func__, 13168 rq->ret); 13169 ctl_set_internal_failure(&io->scsiio, 13170 /*sks_valid*/ 1, 13171 /*retry_count*/ rq->ret); 13172 } 13173 13174 ctl_dt_req_free(rq); 13175 13176 /* Switch the pointer over so the FETD knows what to do */ 13177 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13178 13179 /* 13180 * Use a custom move done callback, since we need to send completion 13181 * back to the other controller, not to the backend on this side. 13182 */ 13183 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13184 13185 /* XXX KDM add checks like the ones in ctl_datamove? */ 13186 13187 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13188 13189 fe_datamove(io); 13190 } 13191 13192 static int 13193 ctl_datamove_remote_sgl_setup(union ctl_io *io) 13194 { 13195 struct ctl_sg_entry *local_sglist, *remote_sglist; 13196 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13197 struct ctl_softc *softc; 13198 int retval; 13199 int i; 13200 13201 retval = 0; 13202 softc = control_softc; 13203 13204 local_sglist = io->io_hdr.local_sglist; 13205 local_dma_sglist = io->io_hdr.local_dma_sglist; 13206 remote_sglist = io->io_hdr.remote_sglist; 13207 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13208 13209 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13210 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13211 local_sglist[i].len = remote_sglist[i].len; 13212 13213 /* 13214 * XXX Detect the situation where the RS-level I/O 13215 * redirector on the other side has already read the 13216 * data off of the AOR RS on this side, and 13217 * transferred it to remote (mirror) memory on the 13218 * other side. Since we already have the data in 13219 * memory here, we just need to use it. 13220 * 13221 * XXX KDM this can probably be removed once we 13222 * get the cache device code in and take the 13223 * current AOR implementation out. 13224 */ 13225 #ifdef NEEDTOPORT 13226 if ((remote_sglist[i].addr >= 13227 (void *)vtophys(softc->mirr->addr)) 13228 && (remote_sglist[i].addr < 13229 ((void *)vtophys(softc->mirr->addr) + 13230 CacheMirrorOffset))) { 13231 local_sglist[i].addr = remote_sglist[i].addr - 13232 CacheMirrorOffset; 13233 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13234 CTL_FLAG_DATA_IN) 13235 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13236 } else { 13237 local_sglist[i].addr = remote_sglist[i].addr + 13238 CacheMirrorOffset; 13239 } 13240 #endif 13241 #if 0 13242 printf("%s: local %p, remote %p, len %d\n", 13243 __func__, local_sglist[i].addr, 13244 remote_sglist[i].addr, local_sglist[i].len); 13245 #endif 13246 } 13247 } else { 13248 uint32_t len_to_go; 13249 13250 /* 13251 * In this case, we don't have automatically allocated 13252 * memory for this I/O on this controller. This typically 13253 * happens with internal CTL I/O -- e.g. inquiry, mode 13254 * sense, etc. Anything coming from RAIDCore will have 13255 * a mirror area available. 13256 */ 13257 len_to_go = io->scsiio.kern_data_len; 13258 13259 /* 13260 * Clear the no datasync flag, we have to use malloced 13261 * buffers. 13262 */ 13263 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13264 13265 /* 13266 * The difficult thing here is that the size of the various 13267 * S/G segments may be different than the size from the 13268 * remote controller. That'll make it harder when DMAing 13269 * the data back to the other side. 13270 */ 13271 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13272 sizeof(io->io_hdr.remote_sglist[0])) && 13273 (len_to_go > 0); i++) { 13274 local_sglist[i].len = ctl_min(len_to_go, 131072); 13275 CTL_SIZE_8B(local_dma_sglist[i].len, 13276 local_sglist[i].len); 13277 local_sglist[i].addr = 13278 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13279 13280 local_dma_sglist[i].addr = local_sglist[i].addr; 13281 13282 if (local_sglist[i].addr == NULL) { 13283 int j; 13284 13285 printf("malloc failed for %zd bytes!", 13286 local_dma_sglist[i].len); 13287 for (j = 0; j < i; j++) { 13288 free(local_sglist[j].addr, M_CTL); 13289 } 13290 ctl_set_internal_failure(&io->scsiio, 13291 /*sks_valid*/ 1, 13292 /*retry_count*/ 4857); 13293 retval = 1; 13294 goto bailout_error; 13295 13296 } 13297 /* XXX KDM do we need a sync here? */ 13298 13299 len_to_go -= local_sglist[i].len; 13300 } 13301 /* 13302 * Reset the number of S/G entries accordingly. The 13303 * original number of S/G entries is available in 13304 * rem_sg_entries. 13305 */ 13306 io->scsiio.kern_sg_entries = i; 13307 13308 #if 0 13309 printf("%s: kern_sg_entries = %d\n", __func__, 13310 io->scsiio.kern_sg_entries); 13311 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13312 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13313 local_sglist[i].addr, local_sglist[i].len, 13314 local_dma_sglist[i].len); 13315 #endif 13316 } 13317 13318 13319 return (retval); 13320 13321 bailout_error: 13322 13323 ctl_send_datamove_done(io, /*have_lock*/ 0); 13324 13325 return (retval); 13326 } 13327 13328 static int 13329 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13330 ctl_ha_dt_cb callback) 13331 { 13332 struct ctl_ha_dt_req *rq; 13333 struct ctl_sg_entry *remote_sglist, *local_sglist; 13334 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13335 uint32_t local_used, remote_used, total_used; 13336 int retval; 13337 int i, j; 13338 13339 retval = 0; 13340 13341 rq = ctl_dt_req_alloc(); 13342 13343 /* 13344 * If we failed to allocate the request, and if the DMA didn't fail 13345 * anyway, set busy status. This is just a resource allocation 13346 * failure. 13347 */ 13348 if ((rq == NULL) 13349 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13350 ctl_set_busy(&io->scsiio); 13351 13352 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13353 13354 if (rq != NULL) 13355 ctl_dt_req_free(rq); 13356 13357 /* 13358 * The data move failed. We need to return status back 13359 * to the other controller. No point in trying to DMA 13360 * data to the remote controller. 13361 */ 13362 13363 ctl_send_datamove_done(io, /*have_lock*/ 0); 13364 13365 retval = 1; 13366 13367 goto bailout; 13368 } 13369 13370 local_sglist = io->io_hdr.local_sglist; 13371 local_dma_sglist = io->io_hdr.local_dma_sglist; 13372 remote_sglist = io->io_hdr.remote_sglist; 13373 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13374 local_used = 0; 13375 remote_used = 0; 13376 total_used = 0; 13377 13378 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13379 rq->ret = CTL_HA_STATUS_SUCCESS; 13380 rq->context = io; 13381 callback(rq); 13382 goto bailout; 13383 } 13384 13385 /* 13386 * Pull/push the data over the wire from/to the other controller. 13387 * This takes into account the possibility that the local and 13388 * remote sglists may not be identical in terms of the size of 13389 * the elements and the number of elements. 13390 * 13391 * One fundamental assumption here is that the length allocated for 13392 * both the local and remote sglists is identical. Otherwise, we've 13393 * essentially got a coding error of some sort. 13394 */ 13395 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13396 int isc_ret; 13397 uint32_t cur_len, dma_length; 13398 uint8_t *tmp_ptr; 13399 13400 rq->id = CTL_HA_DATA_CTL; 13401 rq->command = command; 13402 rq->context = io; 13403 13404 /* 13405 * Both pointers should be aligned. But it is possible 13406 * that the allocation length is not. They should both 13407 * also have enough slack left over at the end, though, 13408 * to round up to the next 8 byte boundary. 13409 */ 13410 cur_len = ctl_min(local_sglist[i].len - local_used, 13411 remote_sglist[j].len - remote_used); 13412 13413 /* 13414 * In this case, we have a size issue and need to decrease 13415 * the size, except in the case where we actually have less 13416 * than 8 bytes left. In that case, we need to increase 13417 * the DMA length to get the last bit. 13418 */ 13419 if ((cur_len & 0x7) != 0) { 13420 if (cur_len > 0x7) { 13421 cur_len = cur_len - (cur_len & 0x7); 13422 dma_length = cur_len; 13423 } else { 13424 CTL_SIZE_8B(dma_length, cur_len); 13425 } 13426 13427 } else 13428 dma_length = cur_len; 13429 13430 /* 13431 * If we had to allocate memory for this I/O, instead of using 13432 * the non-cached mirror memory, we'll need to flush the cache 13433 * before trying to DMA to the other controller. 13434 * 13435 * We could end up doing this multiple times for the same 13436 * segment if we have a larger local segment than remote 13437 * segment. That shouldn't be an issue. 13438 */ 13439 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13440 /* 13441 * XXX KDM use bus_dmamap_sync() here. 13442 */ 13443 } 13444 13445 rq->size = dma_length; 13446 13447 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13448 tmp_ptr += local_used; 13449 13450 /* Use physical addresses when talking to ISC hardware */ 13451 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13452 /* XXX KDM use busdma */ 13453 #if 0 13454 rq->local = vtophys(tmp_ptr); 13455 #endif 13456 } else 13457 rq->local = tmp_ptr; 13458 13459 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13460 tmp_ptr += remote_used; 13461 rq->remote = tmp_ptr; 13462 13463 rq->callback = NULL; 13464 13465 local_used += cur_len; 13466 if (local_used >= local_sglist[i].len) { 13467 i++; 13468 local_used = 0; 13469 } 13470 13471 remote_used += cur_len; 13472 if (remote_used >= remote_sglist[j].len) { 13473 j++; 13474 remote_used = 0; 13475 } 13476 total_used += cur_len; 13477 13478 if (total_used >= io->scsiio.kern_data_len) 13479 rq->callback = callback; 13480 13481 if ((rq->size & 0x7) != 0) { 13482 printf("%s: warning: size %d is not on 8b boundary\n", 13483 __func__, rq->size); 13484 } 13485 if (((uintptr_t)rq->local & 0x7) != 0) { 13486 printf("%s: warning: local %p not on 8b boundary\n", 13487 __func__, rq->local); 13488 } 13489 if (((uintptr_t)rq->remote & 0x7) != 0) { 13490 printf("%s: warning: remote %p not on 8b boundary\n", 13491 __func__, rq->local); 13492 } 13493 #if 0 13494 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13495 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13496 rq->local, rq->remote, rq->size); 13497 #endif 13498 13499 isc_ret = ctl_dt_single(rq); 13500 if (isc_ret == CTL_HA_STATUS_WAIT) 13501 continue; 13502 13503 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13504 rq->ret = CTL_HA_STATUS_SUCCESS; 13505 } else { 13506 rq->ret = isc_ret; 13507 } 13508 callback(rq); 13509 goto bailout; 13510 } 13511 13512 bailout: 13513 return (retval); 13514 13515 } 13516 13517 static void 13518 ctl_datamove_remote_read(union ctl_io *io) 13519 { 13520 int retval; 13521 int i; 13522 13523 /* 13524 * This will send an error to the other controller in the case of a 13525 * failure. 13526 */ 13527 retval = ctl_datamove_remote_sgl_setup(io); 13528 if (retval != 0) 13529 return; 13530 13531 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13532 ctl_datamove_remote_read_cb); 13533 if ((retval != 0) 13534 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13535 /* 13536 * Make sure we free memory if there was an error.. The 13537 * ctl_datamove_remote_xfer() function will send the 13538 * datamove done message, or call the callback with an 13539 * error if there is a problem. 13540 */ 13541 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13542 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13543 } 13544 13545 return; 13546 } 13547 13548 /* 13549 * Process a datamove request from the other controller. This is used for 13550 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13551 * first. Once that is complete, the data gets DMAed into the remote 13552 * controller's memory. For reads, we DMA from the remote controller's 13553 * memory into our memory first, and then move it out to the FETD. 13554 */ 13555 static void 13556 ctl_datamove_remote(union ctl_io *io) 13557 { 13558 struct ctl_softc *softc; 13559 13560 softc = control_softc; 13561 13562 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13563 13564 /* 13565 * Note that we look for an aborted I/O here, but don't do some of 13566 * the other checks that ctl_datamove() normally does. 13567 * We don't need to run the datamove delay code, since that should 13568 * have been done if need be on the other controller. 13569 */ 13570 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13571 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13572 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13573 io->io_hdr.nexus.targ_port, 13574 io->io_hdr.nexus.targ_target.id, 13575 io->io_hdr.nexus.targ_lun); 13576 io->io_hdr.port_status = 31338; 13577 ctl_send_datamove_done(io, /*have_lock*/ 0); 13578 return; 13579 } 13580 13581 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13582 ctl_datamove_remote_write(io); 13583 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13584 ctl_datamove_remote_read(io); 13585 } else { 13586 union ctl_ha_msg msg; 13587 struct scsi_sense_data *sense; 13588 uint8_t sks[3]; 13589 int retry_count; 13590 13591 memset(&msg, 0, sizeof(msg)); 13592 13593 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13594 msg.hdr.status = CTL_SCSI_ERROR; 13595 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13596 13597 retry_count = 4243; 13598 13599 sense = &msg.scsi.sense_data; 13600 sks[0] = SSD_SCS_VALID; 13601 sks[1] = (retry_count >> 8) & 0xff; 13602 sks[2] = retry_count & 0xff; 13603 13604 /* "Internal target failure" */ 13605 scsi_set_sense_data(sense, 13606 /*sense_format*/ SSD_TYPE_NONE, 13607 /*current_error*/ 1, 13608 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13609 /*asc*/ 0x44, 13610 /*ascq*/ 0x00, 13611 /*type*/ SSD_ELEM_SKS, 13612 /*size*/ sizeof(sks), 13613 /*data*/ sks, 13614 SSD_ELEM_NONE); 13615 13616 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13617 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13618 ctl_failover_io(io, /*have_lock*/ 1); 13619 return; 13620 } 13621 13622 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13623 CTL_HA_STATUS_SUCCESS) { 13624 /* XXX KDM what to do if this fails? */ 13625 } 13626 return; 13627 } 13628 13629 } 13630 13631 static int 13632 ctl_process_done(union ctl_io *io) 13633 { 13634 struct ctl_lun *lun; 13635 struct ctl_softc *ctl_softc; 13636 void (*fe_done)(union ctl_io *io); 13637 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13638 13639 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13640 13641 fe_done = 13642 control_softc->ctl_ports[targ_port]->fe_done; 13643 13644 #ifdef CTL_TIME_IO 13645 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13646 char str[256]; 13647 char path_str[64]; 13648 struct sbuf sb; 13649 13650 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13651 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13652 13653 sbuf_cat(&sb, path_str); 13654 switch (io->io_hdr.io_type) { 13655 case CTL_IO_SCSI: 13656 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13657 sbuf_printf(&sb, "\n"); 13658 sbuf_cat(&sb, path_str); 13659 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13660 io->scsiio.tag_num, io->scsiio.tag_type); 13661 break; 13662 case CTL_IO_TASK: 13663 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13664 "Tag Type: %d\n", io->taskio.task_action, 13665 io->taskio.tag_num, io->taskio.tag_type); 13666 break; 13667 default: 13668 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13669 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13670 break; 13671 } 13672 sbuf_cat(&sb, path_str); 13673 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13674 (intmax_t)time_uptime - io->io_hdr.start_time); 13675 sbuf_finish(&sb); 13676 printf("%s", sbuf_data(&sb)); 13677 } 13678 #endif /* CTL_TIME_IO */ 13679 13680 switch (io->io_hdr.io_type) { 13681 case CTL_IO_SCSI: 13682 break; 13683 case CTL_IO_TASK: 13684 if (bootverbose || verbose > 0) 13685 ctl_io_error_print(io, NULL); 13686 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13687 ctl_free_io(io); 13688 else 13689 fe_done(io); 13690 return (CTL_RETVAL_COMPLETE); 13691 break; 13692 default: 13693 printf("ctl_process_done: invalid io type %d\n", 13694 io->io_hdr.io_type); 13695 panic("ctl_process_done: invalid io type %d\n", 13696 io->io_hdr.io_type); 13697 break; /* NOTREACHED */ 13698 } 13699 13700 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13701 if (lun == NULL) { 13702 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13703 io->io_hdr.nexus.targ_mapped_lun)); 13704 fe_done(io); 13705 goto bailout; 13706 } 13707 ctl_softc = lun->ctl_softc; 13708 13709 mtx_lock(&lun->lun_lock); 13710 13711 /* 13712 * Check to see if we have any errors to inject here. We only 13713 * inject errors for commands that don't already have errors set. 13714 */ 13715 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13716 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13717 ctl_inject_error(lun, io); 13718 13719 /* 13720 * XXX KDM how do we treat commands that aren't completed 13721 * successfully? 13722 * 13723 * XXX KDM should we also track I/O latency? 13724 */ 13725 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13726 io->io_hdr.io_type == CTL_IO_SCSI) { 13727 #ifdef CTL_TIME_IO 13728 struct bintime cur_bt; 13729 #endif 13730 int type; 13731 13732 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13733 CTL_FLAG_DATA_IN) 13734 type = CTL_STATS_READ; 13735 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13736 CTL_FLAG_DATA_OUT) 13737 type = CTL_STATS_WRITE; 13738 else 13739 type = CTL_STATS_NO_IO; 13740 13741 lun->stats.ports[targ_port].bytes[type] += 13742 io->scsiio.kern_total_len; 13743 lun->stats.ports[targ_port].operations[type]++; 13744 #ifdef CTL_TIME_IO 13745 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13746 &io->io_hdr.dma_bt); 13747 lun->stats.ports[targ_port].num_dmas[type] += 13748 io->io_hdr.num_dmas; 13749 getbintime(&cur_bt); 13750 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13751 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13752 #endif 13753 } 13754 13755 /* 13756 * Remove this from the OOA queue. 13757 */ 13758 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13759 13760 /* 13761 * Run through the blocked queue on this LUN and see if anything 13762 * has become unblocked, now that this transaction is done. 13763 */ 13764 ctl_check_blocked(lun); 13765 13766 /* 13767 * If the LUN has been invalidated, free it if there is nothing 13768 * left on its OOA queue. 13769 */ 13770 if ((lun->flags & CTL_LUN_INVALID) 13771 && TAILQ_EMPTY(&lun->ooa_queue)) { 13772 mtx_unlock(&lun->lun_lock); 13773 mtx_lock(&ctl_softc->ctl_lock); 13774 ctl_free_lun(lun); 13775 mtx_unlock(&ctl_softc->ctl_lock); 13776 } else 13777 mtx_unlock(&lun->lun_lock); 13778 13779 /* 13780 * If this command has been aborted, make sure we set the status 13781 * properly. The FETD is responsible for freeing the I/O and doing 13782 * whatever it needs to do to clean up its state. 13783 */ 13784 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13785 ctl_set_task_aborted(&io->scsiio); 13786 13787 /* 13788 * We print out status for every task management command. For SCSI 13789 * commands, we filter out any unit attention errors; they happen 13790 * on every boot, and would clutter up the log. Note: task 13791 * management commands aren't printed here, they are printed above, 13792 * since they should never even make it down here. 13793 */ 13794 switch (io->io_hdr.io_type) { 13795 case CTL_IO_SCSI: { 13796 int error_code, sense_key, asc, ascq; 13797 13798 sense_key = 0; 13799 13800 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13801 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13802 /* 13803 * Since this is just for printing, no need to 13804 * show errors here. 13805 */ 13806 scsi_extract_sense_len(&io->scsiio.sense_data, 13807 io->scsiio.sense_len, 13808 &error_code, 13809 &sense_key, 13810 &asc, 13811 &ascq, 13812 /*show_errors*/ 0); 13813 } 13814 13815 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13816 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13817 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13818 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13819 13820 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13821 ctl_softc->skipped_prints++; 13822 } else { 13823 uint32_t skipped_prints; 13824 13825 skipped_prints = ctl_softc->skipped_prints; 13826 13827 ctl_softc->skipped_prints = 0; 13828 ctl_softc->last_print_jiffies = time_uptime; 13829 13830 if (skipped_prints > 0) { 13831 #ifdef NEEDTOPORT 13832 csevent_log(CSC_CTL | CSC_SHELF_SW | 13833 CTL_ERROR_REPORT, 13834 csevent_LogType_Trace, 13835 csevent_Severity_Information, 13836 csevent_AlertLevel_Green, 13837 csevent_FRU_Firmware, 13838 csevent_FRU_Unknown, 13839 "High CTL error volume, %d prints " 13840 "skipped", skipped_prints); 13841 #endif 13842 } 13843 if (bootverbose || verbose > 0) 13844 ctl_io_error_print(io, NULL); 13845 } 13846 } 13847 break; 13848 } 13849 case CTL_IO_TASK: 13850 if (bootverbose || verbose > 0) 13851 ctl_io_error_print(io, NULL); 13852 break; 13853 default: 13854 break; 13855 } 13856 13857 /* 13858 * Tell the FETD or the other shelf controller we're done with this 13859 * command. Note that only SCSI commands get to this point. Task 13860 * management commands are completed above. 13861 * 13862 * We only send status to the other controller if we're in XFER 13863 * mode. In SER_ONLY mode, the I/O is done on the controller that 13864 * received the I/O (from CTL's perspective), and so the status is 13865 * generated there. 13866 * 13867 * XXX KDM if we hold the lock here, we could cause a deadlock 13868 * if the frontend comes back in in this context to queue 13869 * something. 13870 */ 13871 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13872 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13873 union ctl_ha_msg msg; 13874 13875 memset(&msg, 0, sizeof(msg)); 13876 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13877 msg.hdr.original_sc = io->io_hdr.original_sc; 13878 msg.hdr.nexus = io->io_hdr.nexus; 13879 msg.hdr.status = io->io_hdr.status; 13880 msg.scsi.scsi_status = io->scsiio.scsi_status; 13881 msg.scsi.tag_num = io->scsiio.tag_num; 13882 msg.scsi.tag_type = io->scsiio.tag_type; 13883 msg.scsi.sense_len = io->scsiio.sense_len; 13884 msg.scsi.sense_residual = io->scsiio.sense_residual; 13885 msg.scsi.residual = io->scsiio.residual; 13886 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13887 sizeof(io->scsiio.sense_data)); 13888 /* 13889 * We copy this whether or not this is an I/O-related 13890 * command. Otherwise, we'd have to go and check to see 13891 * whether it's a read/write command, and it really isn't 13892 * worth it. 13893 */ 13894 memcpy(&msg.scsi.lbalen, 13895 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13896 sizeof(msg.scsi.lbalen)); 13897 13898 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13899 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13900 /* XXX do something here */ 13901 } 13902 13903 ctl_free_io(io); 13904 } else 13905 fe_done(io); 13906 13907 bailout: 13908 13909 return (CTL_RETVAL_COMPLETE); 13910 } 13911 13912 #ifdef CTL_WITH_CA 13913 /* 13914 * Front end should call this if it doesn't do autosense. When the request 13915 * sense comes back in from the initiator, we'll dequeue this and send it. 13916 */ 13917 int 13918 ctl_queue_sense(union ctl_io *io) 13919 { 13920 struct ctl_lun *lun; 13921 struct ctl_softc *ctl_softc; 13922 uint32_t initidx, targ_lun; 13923 13924 ctl_softc = control_softc; 13925 13926 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13927 13928 /* 13929 * LUN lookup will likely move to the ctl_work_thread() once we 13930 * have our new queueing infrastructure (that doesn't put things on 13931 * a per-LUN queue initially). That is so that we can handle 13932 * things like an INQUIRY to a LUN that we don't have enabled. We 13933 * can't deal with that right now. 13934 */ 13935 mtx_lock(&ctl_softc->ctl_lock); 13936 13937 /* 13938 * If we don't have a LUN for this, just toss the sense 13939 * information. 13940 */ 13941 targ_lun = io->io_hdr.nexus.targ_lun; 13942 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13943 if ((targ_lun < CTL_MAX_LUNS) 13944 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13945 lun = ctl_softc->ctl_luns[targ_lun]; 13946 else 13947 goto bailout; 13948 13949 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13950 13951 mtx_lock(&lun->lun_lock); 13952 /* 13953 * Already have CA set for this LUN...toss the sense information. 13954 */ 13955 if (ctl_is_set(lun->have_ca, initidx)) { 13956 mtx_unlock(&lun->lun_lock); 13957 goto bailout; 13958 } 13959 13960 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13961 ctl_min(sizeof(lun->pending_sense[initidx]), 13962 sizeof(io->scsiio.sense_data))); 13963 ctl_set_mask(lun->have_ca, initidx); 13964 mtx_unlock(&lun->lun_lock); 13965 13966 bailout: 13967 mtx_unlock(&ctl_softc->ctl_lock); 13968 13969 ctl_free_io(io); 13970 13971 return (CTL_RETVAL_COMPLETE); 13972 } 13973 #endif 13974 13975 /* 13976 * Primary command inlet from frontend ports. All SCSI and task I/O 13977 * requests must go through this function. 13978 */ 13979 int 13980 ctl_queue(union ctl_io *io) 13981 { 13982 struct ctl_softc *ctl_softc; 13983 13984 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13985 13986 ctl_softc = control_softc; 13987 13988 #ifdef CTL_TIME_IO 13989 io->io_hdr.start_time = time_uptime; 13990 getbintime(&io->io_hdr.start_bt); 13991 #endif /* CTL_TIME_IO */ 13992 13993 /* Map FE-specific LUN ID into global one. */ 13994 io->io_hdr.nexus.targ_mapped_lun = 13995 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13996 13997 switch (io->io_hdr.io_type) { 13998 case CTL_IO_SCSI: 13999 case CTL_IO_TASK: 14000 ctl_enqueue_incoming(io); 14001 break; 14002 default: 14003 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 14004 return (EINVAL); 14005 } 14006 14007 return (CTL_RETVAL_COMPLETE); 14008 } 14009 14010 #ifdef CTL_IO_DELAY 14011 static void 14012 ctl_done_timer_wakeup(void *arg) 14013 { 14014 union ctl_io *io; 14015 14016 io = (union ctl_io *)arg; 14017 ctl_done(io); 14018 } 14019 #endif /* CTL_IO_DELAY */ 14020 14021 void 14022 ctl_done(union ctl_io *io) 14023 { 14024 struct ctl_softc *ctl_softc; 14025 14026 ctl_softc = control_softc; 14027 14028 /* 14029 * Enable this to catch duplicate completion issues. 14030 */ 14031 #if 0 14032 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 14033 printf("%s: type %d msg %d cdb %x iptl: " 14034 "%d:%d:%d:%d tag 0x%04x " 14035 "flag %#x status %x\n", 14036 __func__, 14037 io->io_hdr.io_type, 14038 io->io_hdr.msg_type, 14039 io->scsiio.cdb[0], 14040 io->io_hdr.nexus.initid.id, 14041 io->io_hdr.nexus.targ_port, 14042 io->io_hdr.nexus.targ_target.id, 14043 io->io_hdr.nexus.targ_lun, 14044 (io->io_hdr.io_type == 14045 CTL_IO_TASK) ? 14046 io->taskio.tag_num : 14047 io->scsiio.tag_num, 14048 io->io_hdr.flags, 14049 io->io_hdr.status); 14050 } else 14051 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 14052 #endif 14053 14054 /* 14055 * This is an internal copy of an I/O, and should not go through 14056 * the normal done processing logic. 14057 */ 14058 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 14059 return; 14060 14061 /* 14062 * We need to send a msg to the serializing shelf to finish the IO 14063 * as well. We don't send a finish message to the other shelf if 14064 * this is a task management command. Task management commands 14065 * aren't serialized in the OOA queue, but rather just executed on 14066 * both shelf controllers for commands that originated on that 14067 * controller. 14068 */ 14069 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 14070 && (io->io_hdr.io_type != CTL_IO_TASK)) { 14071 union ctl_ha_msg msg_io; 14072 14073 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 14074 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 14075 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 14076 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 14077 } 14078 /* continue on to finish IO */ 14079 } 14080 #ifdef CTL_IO_DELAY 14081 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 14082 struct ctl_lun *lun; 14083 14084 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14085 14086 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14087 } else { 14088 struct ctl_lun *lun; 14089 14090 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14091 14092 if ((lun != NULL) 14093 && (lun->delay_info.done_delay > 0)) { 14094 struct callout *callout; 14095 14096 callout = (struct callout *)&io->io_hdr.timer_bytes; 14097 callout_init(callout, /*mpsafe*/ 1); 14098 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14099 callout_reset(callout, 14100 lun->delay_info.done_delay * hz, 14101 ctl_done_timer_wakeup, io); 14102 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14103 lun->delay_info.done_delay = 0; 14104 return; 14105 } 14106 } 14107 #endif /* CTL_IO_DELAY */ 14108 14109 ctl_enqueue_done(io); 14110 } 14111 14112 int 14113 ctl_isc(struct ctl_scsiio *ctsio) 14114 { 14115 struct ctl_lun *lun; 14116 int retval; 14117 14118 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14119 14120 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14121 14122 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14123 14124 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14125 14126 return (retval); 14127 } 14128 14129 14130 static void 14131 ctl_work_thread(void *arg) 14132 { 14133 struct ctl_thread *thr = (struct ctl_thread *)arg; 14134 struct ctl_softc *softc = thr->ctl_softc; 14135 union ctl_io *io; 14136 int retval; 14137 14138 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14139 14140 for (;;) { 14141 retval = 0; 14142 14143 /* 14144 * We handle the queues in this order: 14145 * - ISC 14146 * - done queue (to free up resources, unblock other commands) 14147 * - RtR queue 14148 * - incoming queue 14149 * 14150 * If those queues are empty, we break out of the loop and 14151 * go to sleep. 14152 */ 14153 mtx_lock(&thr->queue_lock); 14154 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14155 if (io != NULL) { 14156 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14157 mtx_unlock(&thr->queue_lock); 14158 ctl_handle_isc(io); 14159 continue; 14160 } 14161 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14162 if (io != NULL) { 14163 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14164 /* clear any blocked commands, call fe_done */ 14165 mtx_unlock(&thr->queue_lock); 14166 retval = ctl_process_done(io); 14167 continue; 14168 } 14169 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14170 if (io != NULL) { 14171 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14172 mtx_unlock(&thr->queue_lock); 14173 if (io->io_hdr.io_type == CTL_IO_TASK) 14174 ctl_run_task(io); 14175 else 14176 ctl_scsiio_precheck(softc, &io->scsiio); 14177 continue; 14178 } 14179 if (!ctl_pause_rtr) { 14180 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14181 if (io != NULL) { 14182 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14183 mtx_unlock(&thr->queue_lock); 14184 retval = ctl_scsiio(&io->scsiio); 14185 if (retval != CTL_RETVAL_COMPLETE) 14186 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14187 continue; 14188 } 14189 } 14190 14191 /* Sleep until we have something to do. */ 14192 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14193 } 14194 } 14195 14196 static void 14197 ctl_lun_thread(void *arg) 14198 { 14199 struct ctl_softc *softc = (struct ctl_softc *)arg; 14200 struct ctl_be_lun *be_lun; 14201 int retval; 14202 14203 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14204 14205 for (;;) { 14206 retval = 0; 14207 mtx_lock(&softc->ctl_lock); 14208 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14209 if (be_lun != NULL) { 14210 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14211 mtx_unlock(&softc->ctl_lock); 14212 ctl_create_lun(be_lun); 14213 continue; 14214 } 14215 14216 /* Sleep until we have something to do. */ 14217 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14218 PDROP | PRIBIO, "-", 0); 14219 } 14220 } 14221 14222 static void 14223 ctl_enqueue_incoming(union ctl_io *io) 14224 { 14225 struct ctl_softc *softc = control_softc; 14226 struct ctl_thread *thr; 14227 u_int idx; 14228 14229 idx = (io->io_hdr.nexus.targ_port * 127 + 14230 io->io_hdr.nexus.initid.id) % worker_threads; 14231 thr = &softc->threads[idx]; 14232 mtx_lock(&thr->queue_lock); 14233 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14234 mtx_unlock(&thr->queue_lock); 14235 wakeup(thr); 14236 } 14237 14238 static void 14239 ctl_enqueue_rtr(union ctl_io *io) 14240 { 14241 struct ctl_softc *softc = control_softc; 14242 struct ctl_thread *thr; 14243 14244 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14245 mtx_lock(&thr->queue_lock); 14246 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14247 mtx_unlock(&thr->queue_lock); 14248 wakeup(thr); 14249 } 14250 14251 static void 14252 ctl_enqueue_done(union ctl_io *io) 14253 { 14254 struct ctl_softc *softc = control_softc; 14255 struct ctl_thread *thr; 14256 14257 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14258 mtx_lock(&thr->queue_lock); 14259 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14260 mtx_unlock(&thr->queue_lock); 14261 wakeup(thr); 14262 } 14263 14264 static void 14265 ctl_enqueue_isc(union ctl_io *io) 14266 { 14267 struct ctl_softc *softc = control_softc; 14268 struct ctl_thread *thr; 14269 14270 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14271 mtx_lock(&thr->queue_lock); 14272 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14273 mtx_unlock(&thr->queue_lock); 14274 wakeup(thr); 14275 } 14276 14277 /* Initialization and failover */ 14278 14279 void 14280 ctl_init_isc_msg(void) 14281 { 14282 printf("CTL: Still calling this thing\n"); 14283 } 14284 14285 /* 14286 * Init component 14287 * Initializes component into configuration defined by bootMode 14288 * (see hasc-sv.c) 14289 * returns hasc_Status: 14290 * OK 14291 * ERROR - fatal error 14292 */ 14293 static ctl_ha_comp_status 14294 ctl_isc_init(struct ctl_ha_component *c) 14295 { 14296 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14297 14298 c->status = ret; 14299 return ret; 14300 } 14301 14302 /* Start component 14303 * Starts component in state requested. If component starts successfully, 14304 * it must set its own state to the requestrd state 14305 * When requested state is HASC_STATE_HA, the component may refine it 14306 * by adding _SLAVE or _MASTER flags. 14307 * Currently allowed state transitions are: 14308 * UNKNOWN->HA - initial startup 14309 * UNKNOWN->SINGLE - initial startup when no parter detected 14310 * HA->SINGLE - failover 14311 * returns ctl_ha_comp_status: 14312 * OK - component successfully started in requested state 14313 * FAILED - could not start the requested state, failover may 14314 * be possible 14315 * ERROR - fatal error detected, no future startup possible 14316 */ 14317 static ctl_ha_comp_status 14318 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14319 { 14320 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14321 14322 printf("%s: go\n", __func__); 14323 14324 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14325 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14326 ctl_is_single = 0; 14327 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14328 != CTL_HA_STATUS_SUCCESS) { 14329 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14330 ret = CTL_HA_COMP_STATUS_ERROR; 14331 } 14332 } else if (CTL_HA_STATE_IS_HA(c->state) 14333 && CTL_HA_STATE_IS_SINGLE(state)){ 14334 // HA->SINGLE transition 14335 ctl_failover(); 14336 ctl_is_single = 1; 14337 } else { 14338 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14339 c->state, state); 14340 ret = CTL_HA_COMP_STATUS_ERROR; 14341 } 14342 if (CTL_HA_STATE_IS_SINGLE(state)) 14343 ctl_is_single = 1; 14344 14345 c->state = state; 14346 c->status = ret; 14347 return ret; 14348 } 14349 14350 /* 14351 * Quiesce component 14352 * The component must clear any error conditions (set status to OK) and 14353 * prepare itself to another Start call 14354 * returns ctl_ha_comp_status: 14355 * OK 14356 * ERROR 14357 */ 14358 static ctl_ha_comp_status 14359 ctl_isc_quiesce(struct ctl_ha_component *c) 14360 { 14361 int ret = CTL_HA_COMP_STATUS_OK; 14362 14363 ctl_pause_rtr = 1; 14364 c->status = ret; 14365 return ret; 14366 } 14367 14368 struct ctl_ha_component ctl_ha_component_ctlisc = 14369 { 14370 .name = "CTL ISC", 14371 .state = CTL_HA_STATE_UNKNOWN, 14372 .init = ctl_isc_init, 14373 .start = ctl_isc_start, 14374 .quiesce = ctl_isc_quiesce 14375 }; 14376 14377 /* 14378 * vim: ts=8 14379 */ 14380