ctl.c revision 272646
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: stable/10/sys/cam/ctl/ctl.c 272646 2014-10-06 13:33:22Z mav $"); 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 83struct 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 */ 112static 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 125static 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 138static 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 150static 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 162static 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 172static 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 181static 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 198static 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 214static 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 231static 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 247static 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 263static 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 279static 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 291static struct scsi_control_page control_page_changeable = { 292 /*page_code*/SMS_CONTROL_MODE_PAGE, 293 /*page_length*/sizeof(struct scsi_control_page) - 2, 294 /*rlec*/SCP_DSENSE, 295 /*queue_flags*/SCP_QUEUE_ALG_MASK, 296 /*eca_and_aen*/0, 297 /*flags4*/0, 298 /*aen_holdoff_period*/{0, 0}, 299 /*busy_timeout_period*/{0, 0}, 300 /*extended_selftest_completion_time*/{0, 0} 301}; 302 303 304/* 305 * XXX KDM move these into the softc. 306 */ 307static int rcv_sync_msg; 308static int persis_offset; 309static uint8_t ctl_pause_rtr; 310static int ctl_is_single = 1; 311static int index_to_aps_page; 312 313SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 314static int worker_threads = -1; 315TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 316SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 317 &worker_threads, 1, "Number of worker threads"); 318static int verbose = 0; 319TUNABLE_INT("kern.cam.ctl.verbose", &verbose); 320SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 321 &verbose, 0, "Show SCSI errors returned to initiator"); 322 323/* 324 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 325 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 326 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 327 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 328 */ 329#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 330 331static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 332 int param); 333static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 334static int ctl_init(void); 335void ctl_shutdown(void); 336static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 337static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 338static void ctl_ioctl_online(void *arg); 339static void ctl_ioctl_offline(void *arg); 340static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 341static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 342static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 343static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 344static int ctl_ioctl_submit_wait(union ctl_io *io); 345static void ctl_ioctl_datamove(union ctl_io *io); 346static void ctl_ioctl_done(union ctl_io *io); 347static void ctl_ioctl_hard_startstop_callback(void *arg, 348 struct cfi_metatask *metatask); 349static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 350static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 351 struct ctl_ooa *ooa_hdr, 352 struct ctl_ooa_entry *kern_entries); 353static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 354 struct thread *td); 355static uint32_t ctl_map_lun(int port_num, uint32_t lun); 356static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 357#ifdef unused 358static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 359 uint32_t targ_target, uint32_t targ_lun, 360 int can_wait); 361static void ctl_kfree_io(union ctl_io *io); 362#endif /* unused */ 363static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 364 struct ctl_be_lun *be_lun, struct ctl_id target_id); 365static int ctl_free_lun(struct ctl_lun *lun); 366static void ctl_create_lun(struct ctl_be_lun *be_lun); 367/** 368static void ctl_failover_change_pages(struct ctl_softc *softc, 369 struct ctl_scsiio *ctsio, int master); 370**/ 371 372static int ctl_do_mode_select(union ctl_io *io); 373static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 374 uint64_t res_key, uint64_t sa_res_key, 375 uint8_t type, uint32_t residx, 376 struct ctl_scsiio *ctsio, 377 struct scsi_per_res_out *cdb, 378 struct scsi_per_res_out_parms* param); 379static void ctl_pro_preempt_other(struct ctl_lun *lun, 380 union ctl_ha_msg *msg); 381static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 382static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 383static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 384static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 385static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 386static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 387static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 388 int alloc_len); 389static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 390 int alloc_len); 391static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 392static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 393static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 394static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 395static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 396static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 397static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 398 union ctl_io *pending_io, union ctl_io *ooa_io); 399static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 400 union ctl_io *starting_io); 401static int ctl_check_blocked(struct ctl_lun *lun); 402static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 403 struct ctl_lun *lun, 404 const struct ctl_cmd_entry *entry, 405 struct ctl_scsiio *ctsio); 406//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 407static void ctl_failover(void); 408static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 409 struct ctl_scsiio *ctsio); 410static int ctl_scsiio(struct ctl_scsiio *ctsio); 411 412static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 413static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 414 ctl_ua_type ua_type); 415static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 416 ctl_ua_type ua_type); 417static int ctl_abort_task(union ctl_io *io); 418static int ctl_abort_task_set(union ctl_io *io); 419static int ctl_i_t_nexus_reset(union ctl_io *io); 420static void ctl_run_task(union ctl_io *io); 421#ifdef CTL_IO_DELAY 422static void ctl_datamove_timer_wakeup(void *arg); 423static void ctl_done_timer_wakeup(void *arg); 424#endif /* CTL_IO_DELAY */ 425 426static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 427static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 428static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 429static void ctl_datamove_remote_write(union ctl_io *io); 430static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 431static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 432static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 433static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 434 ctl_ha_dt_cb callback); 435static void ctl_datamove_remote_read(union ctl_io *io); 436static void ctl_datamove_remote(union ctl_io *io); 437static int ctl_process_done(union ctl_io *io); 438static void ctl_lun_thread(void *arg); 439static void ctl_work_thread(void *arg); 440static void ctl_enqueue_incoming(union ctl_io *io); 441static void ctl_enqueue_rtr(union ctl_io *io); 442static void ctl_enqueue_done(union ctl_io *io); 443static void ctl_enqueue_isc(union ctl_io *io); 444static const struct ctl_cmd_entry * 445 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 446static const struct ctl_cmd_entry * 447 ctl_validate_command(struct ctl_scsiio *ctsio); 448static int ctl_cmd_applicable(uint8_t lun_type, 449 const struct ctl_cmd_entry *entry); 450 451/* 452 * Load the serialization table. This isn't very pretty, but is probably 453 * the easiest way to do it. 454 */ 455#include "ctl_ser_table.c" 456 457/* 458 * We only need to define open, close and ioctl routines for this driver. 459 */ 460static struct cdevsw ctl_cdevsw = { 461 .d_version = D_VERSION, 462 .d_flags = 0, 463 .d_open = ctl_open, 464 .d_close = ctl_close, 465 .d_ioctl = ctl_ioctl, 466 .d_name = "ctl", 467}; 468 469 470MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 471MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 472 473static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 474 475static moduledata_t ctl_moduledata = { 476 "ctl", 477 ctl_module_event_handler, 478 NULL 479}; 480 481DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 482MODULE_VERSION(ctl, 1); 483 484static struct ctl_frontend ioctl_frontend = 485{ 486 .name = "ioctl", 487}; 488 489static void 490ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 491 union ctl_ha_msg *msg_info) 492{ 493 struct ctl_scsiio *ctsio; 494 495 if (msg_info->hdr.original_sc == NULL) { 496 printf("%s: original_sc == NULL!\n", __func__); 497 /* XXX KDM now what? */ 498 return; 499 } 500 501 ctsio = &msg_info->hdr.original_sc->scsiio; 502 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 503 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 504 ctsio->io_hdr.status = msg_info->hdr.status; 505 ctsio->scsi_status = msg_info->scsi.scsi_status; 506 ctsio->sense_len = msg_info->scsi.sense_len; 507 ctsio->sense_residual = msg_info->scsi.sense_residual; 508 ctsio->residual = msg_info->scsi.residual; 509 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 510 sizeof(ctsio->sense_data)); 511 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 512 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 513 ctl_enqueue_isc((union ctl_io *)ctsio); 514} 515 516static void 517ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 518 union ctl_ha_msg *msg_info) 519{ 520 struct ctl_scsiio *ctsio; 521 522 if (msg_info->hdr.serializing_sc == NULL) { 523 printf("%s: serializing_sc == NULL!\n", __func__); 524 /* XXX KDM now what? */ 525 return; 526 } 527 528 ctsio = &msg_info->hdr.serializing_sc->scsiio; 529#if 0 530 /* 531 * Attempt to catch the situation where an I/O has 532 * been freed, and we're using it again. 533 */ 534 if (ctsio->io_hdr.io_type == 0xff) { 535 union ctl_io *tmp_io; 536 tmp_io = (union ctl_io *)ctsio; 537 printf("%s: %p use after free!\n", __func__, 538 ctsio); 539 printf("%s: type %d msg %d cdb %x iptl: " 540 "%d:%d:%d:%d tag 0x%04x " 541 "flag %#x status %x\n", 542 __func__, 543 tmp_io->io_hdr.io_type, 544 tmp_io->io_hdr.msg_type, 545 tmp_io->scsiio.cdb[0], 546 tmp_io->io_hdr.nexus.initid.id, 547 tmp_io->io_hdr.nexus.targ_port, 548 tmp_io->io_hdr.nexus.targ_target.id, 549 tmp_io->io_hdr.nexus.targ_lun, 550 (tmp_io->io_hdr.io_type == 551 CTL_IO_TASK) ? 552 tmp_io->taskio.tag_num : 553 tmp_io->scsiio.tag_num, 554 tmp_io->io_hdr.flags, 555 tmp_io->io_hdr.status); 556 } 557#endif 558 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 559 ctl_enqueue_isc((union ctl_io *)ctsio); 560} 561 562/* 563 * ISC (Inter Shelf Communication) event handler. Events from the HA 564 * subsystem come in here. 565 */ 566static void 567ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 568{ 569 struct ctl_softc *ctl_softc; 570 union ctl_io *io; 571 struct ctl_prio *presio; 572 ctl_ha_status isc_status; 573 574 ctl_softc = control_softc; 575 io = NULL; 576 577 578#if 0 579 printf("CTL: Isc Msg event %d\n", event); 580#endif 581 if (event == CTL_HA_EVT_MSG_RECV) { 582 union ctl_ha_msg msg_info; 583 584 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 585 sizeof(msg_info), /*wait*/ 0); 586#if 0 587 printf("CTL: msg_type %d\n", msg_info.msg_type); 588#endif 589 if (isc_status != 0) { 590 printf("Error receiving message, status = %d\n", 591 isc_status); 592 return; 593 } 594 595 switch (msg_info.hdr.msg_type) { 596 case CTL_MSG_SERIALIZE: 597#if 0 598 printf("Serialize\n"); 599#endif 600 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 601 if (io == NULL) { 602 printf("ctl_isc_event_handler: can't allocate " 603 "ctl_io!\n"); 604 /* Bad Juju */ 605 /* Need to set busy and send msg back */ 606 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 607 msg_info.hdr.status = CTL_SCSI_ERROR; 608 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 609 msg_info.scsi.sense_len = 0; 610 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 611 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 612 } 613 goto bailout; 614 } 615 ctl_zero_io(io); 616 // populate ctsio from msg_info 617 io->io_hdr.io_type = CTL_IO_SCSI; 618 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 619 io->io_hdr.original_sc = msg_info.hdr.original_sc; 620#if 0 621 printf("pOrig %x\n", (int)msg_info.original_sc); 622#endif 623 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 624 CTL_FLAG_IO_ACTIVE; 625 /* 626 * If we're in serialization-only mode, we don't 627 * want to go through full done processing. Thus 628 * the COPY flag. 629 * 630 * XXX KDM add another flag that is more specific. 631 */ 632 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 633 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 634 io->io_hdr.nexus = msg_info.hdr.nexus; 635#if 0 636 printf("targ %d, port %d, iid %d, lun %d\n", 637 io->io_hdr.nexus.targ_target.id, 638 io->io_hdr.nexus.targ_port, 639 io->io_hdr.nexus.initid.id, 640 io->io_hdr.nexus.targ_lun); 641#endif 642 io->scsiio.tag_num = msg_info.scsi.tag_num; 643 io->scsiio.tag_type = msg_info.scsi.tag_type; 644 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 645 CTL_MAX_CDBLEN); 646 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 647 const struct ctl_cmd_entry *entry; 648 649 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 650 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 651 io->io_hdr.flags |= 652 entry->flags & CTL_FLAG_DATA_MASK; 653 } 654 ctl_enqueue_isc(io); 655 break; 656 657 /* Performed on the Originating SC, XFER mode only */ 658 case CTL_MSG_DATAMOVE: { 659 struct ctl_sg_entry *sgl; 660 int i, j; 661 662 io = msg_info.hdr.original_sc; 663 if (io == NULL) { 664 printf("%s: original_sc == NULL!\n", __func__); 665 /* XXX KDM do something here */ 666 break; 667 } 668 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 669 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 670 /* 671 * Keep track of this, we need to send it back over 672 * when the datamove is complete. 673 */ 674 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 675 676 if (msg_info.dt.sg_sequence == 0) { 677 /* 678 * XXX KDM we use the preallocated S/G list 679 * here, but we'll need to change this to 680 * dynamic allocation if we need larger S/G 681 * lists. 682 */ 683 if (msg_info.dt.kern_sg_entries > 684 sizeof(io->io_hdr.remote_sglist) / 685 sizeof(io->io_hdr.remote_sglist[0])) { 686 printf("%s: number of S/G entries " 687 "needed %u > allocated num %zd\n", 688 __func__, 689 msg_info.dt.kern_sg_entries, 690 sizeof(io->io_hdr.remote_sglist)/ 691 sizeof(io->io_hdr.remote_sglist[0])); 692 693 /* 694 * XXX KDM send a message back to 695 * the other side to shut down the 696 * DMA. The error will come back 697 * through via the normal channel. 698 */ 699 break; 700 } 701 sgl = io->io_hdr.remote_sglist; 702 memset(sgl, 0, 703 sizeof(io->io_hdr.remote_sglist)); 704 705 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 706 707 io->scsiio.kern_sg_entries = 708 msg_info.dt.kern_sg_entries; 709 io->scsiio.rem_sg_entries = 710 msg_info.dt.kern_sg_entries; 711 io->scsiio.kern_data_len = 712 msg_info.dt.kern_data_len; 713 io->scsiio.kern_total_len = 714 msg_info.dt.kern_total_len; 715 io->scsiio.kern_data_resid = 716 msg_info.dt.kern_data_resid; 717 io->scsiio.kern_rel_offset = 718 msg_info.dt.kern_rel_offset; 719 /* 720 * Clear out per-DMA flags. 721 */ 722 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 723 /* 724 * Add per-DMA flags that are set for this 725 * particular DMA request. 726 */ 727 io->io_hdr.flags |= msg_info.dt.flags & 728 CTL_FLAG_RDMA_MASK; 729 } else 730 sgl = (struct ctl_sg_entry *) 731 io->scsiio.kern_data_ptr; 732 733 for (i = msg_info.dt.sent_sg_entries, j = 0; 734 i < (msg_info.dt.sent_sg_entries + 735 msg_info.dt.cur_sg_entries); i++, j++) { 736 sgl[i].addr = msg_info.dt.sg_list[j].addr; 737 sgl[i].len = msg_info.dt.sg_list[j].len; 738 739#if 0 740 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 741 __func__, 742 msg_info.dt.sg_list[j].addr, 743 msg_info.dt.sg_list[j].len, 744 sgl[i].addr, sgl[i].len, j, i); 745#endif 746 } 747#if 0 748 memcpy(&sgl[msg_info.dt.sent_sg_entries], 749 msg_info.dt.sg_list, 750 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 751#endif 752 753 /* 754 * If this is the last piece of the I/O, we've got 755 * the full S/G list. Queue processing in the thread. 756 * Otherwise wait for the next piece. 757 */ 758 if (msg_info.dt.sg_last != 0) 759 ctl_enqueue_isc(io); 760 break; 761 } 762 /* Performed on the Serializing (primary) SC, XFER mode only */ 763 case CTL_MSG_DATAMOVE_DONE: { 764 if (msg_info.hdr.serializing_sc == NULL) { 765 printf("%s: serializing_sc == NULL!\n", 766 __func__); 767 /* XXX KDM now what? */ 768 break; 769 } 770 /* 771 * We grab the sense information here in case 772 * there was a failure, so we can return status 773 * back to the initiator. 774 */ 775 io = msg_info.hdr.serializing_sc; 776 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 777 io->io_hdr.status = msg_info.hdr.status; 778 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 779 io->scsiio.sense_len = msg_info.scsi.sense_len; 780 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 781 io->io_hdr.port_status = msg_info.scsi.fetd_status; 782 io->scsiio.residual = msg_info.scsi.residual; 783 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 784 sizeof(io->scsiio.sense_data)); 785 ctl_enqueue_isc(io); 786 break; 787 } 788 789 /* Preformed on Originating SC, SER_ONLY mode */ 790 case CTL_MSG_R2R: 791 io = msg_info.hdr.original_sc; 792 if (io == NULL) { 793 printf("%s: Major Bummer\n", __func__); 794 return; 795 } else { 796#if 0 797 printf("pOrig %x\n",(int) ctsio); 798#endif 799 } 800 io->io_hdr.msg_type = CTL_MSG_R2R; 801 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 802 ctl_enqueue_isc(io); 803 break; 804 805 /* 806 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 807 * mode. 808 * Performed on the Originating (i.e. secondary) SC in XFER 809 * mode 810 */ 811 case CTL_MSG_FINISH_IO: 812 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 813 ctl_isc_handler_finish_xfer(ctl_softc, 814 &msg_info); 815 else 816 ctl_isc_handler_finish_ser_only(ctl_softc, 817 &msg_info); 818 break; 819 820 /* Preformed on Originating SC */ 821 case CTL_MSG_BAD_JUJU: 822 io = msg_info.hdr.original_sc; 823 if (io == NULL) { 824 printf("%s: Bad JUJU!, original_sc is NULL!\n", 825 __func__); 826 break; 827 } 828 ctl_copy_sense_data(&msg_info, io); 829 /* 830 * IO should have already been cleaned up on other 831 * SC so clear this flag so we won't send a message 832 * back to finish the IO there. 833 */ 834 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 835 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 836 837 /* io = msg_info.hdr.serializing_sc; */ 838 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 839 ctl_enqueue_isc(io); 840 break; 841 842 /* Handle resets sent from the other side */ 843 case CTL_MSG_MANAGE_TASKS: { 844 struct ctl_taskio *taskio; 845 taskio = (struct ctl_taskio *)ctl_alloc_io( 846 (void *)ctl_softc->othersc_pool); 847 if (taskio == NULL) { 848 printf("ctl_isc_event_handler: can't allocate " 849 "ctl_io!\n"); 850 /* Bad Juju */ 851 /* should I just call the proper reset func 852 here??? */ 853 goto bailout; 854 } 855 ctl_zero_io((union ctl_io *)taskio); 856 taskio->io_hdr.io_type = CTL_IO_TASK; 857 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 858 taskio->io_hdr.nexus = msg_info.hdr.nexus; 859 taskio->task_action = msg_info.task.task_action; 860 taskio->tag_num = msg_info.task.tag_num; 861 taskio->tag_type = msg_info.task.tag_type; 862#ifdef CTL_TIME_IO 863 taskio->io_hdr.start_time = time_uptime; 864 getbintime(&taskio->io_hdr.start_bt); 865#if 0 866 cs_prof_gettime(&taskio->io_hdr.start_ticks); 867#endif 868#endif /* CTL_TIME_IO */ 869 ctl_run_task((union ctl_io *)taskio); 870 break; 871 } 872 /* Persistent Reserve action which needs attention */ 873 case CTL_MSG_PERS_ACTION: 874 presio = (struct ctl_prio *)ctl_alloc_io( 875 (void *)ctl_softc->othersc_pool); 876 if (presio == NULL) { 877 printf("ctl_isc_event_handler: can't allocate " 878 "ctl_io!\n"); 879 /* Bad Juju */ 880 /* Need to set busy and send msg back */ 881 goto bailout; 882 } 883 ctl_zero_io((union ctl_io *)presio); 884 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 885 presio->pr_msg = msg_info.pr; 886 ctl_enqueue_isc((union ctl_io *)presio); 887 break; 888 case CTL_MSG_SYNC_FE: 889 rcv_sync_msg = 1; 890 break; 891 case CTL_MSG_APS_LOCK: { 892 // It's quicker to execute this then to 893 // queue it. 894 struct ctl_lun *lun; 895 struct ctl_page_index *page_index; 896 struct copan_aps_subpage *current_sp; 897 uint32_t targ_lun; 898 899 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 900 lun = ctl_softc->ctl_luns[targ_lun]; 901 mtx_lock(&lun->lun_lock); 902 page_index = &lun->mode_pages.index[index_to_aps_page]; 903 current_sp = (struct copan_aps_subpage *) 904 (page_index->page_data + 905 (page_index->page_len * CTL_PAGE_CURRENT)); 906 907 current_sp->lock_active = msg_info.aps.lock_flag; 908 mtx_unlock(&lun->lun_lock); 909 break; 910 } 911 default: 912 printf("How did I get here?\n"); 913 } 914 } else if (event == CTL_HA_EVT_MSG_SENT) { 915 if (param != CTL_HA_STATUS_SUCCESS) { 916 printf("Bad status from ctl_ha_msg_send status %d\n", 917 param); 918 } 919 return; 920 } else if (event == CTL_HA_EVT_DISCONNECT) { 921 printf("CTL: Got a disconnect from Isc\n"); 922 return; 923 } else { 924 printf("ctl_isc_event_handler: Unknown event %d\n", event); 925 return; 926 } 927 928bailout: 929 return; 930} 931 932static void 933ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 934{ 935 struct scsi_sense_data *sense; 936 937 sense = &dest->scsiio.sense_data; 938 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 939 dest->scsiio.scsi_status = src->scsi.scsi_status; 940 dest->scsiio.sense_len = src->scsi.sense_len; 941 dest->io_hdr.status = src->hdr.status; 942} 943 944static int 945ctl_init(void) 946{ 947 struct ctl_softc *softc; 948 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 949 struct ctl_port *port; 950 uint8_t sc_id =0; 951 int i, error, retval; 952 //int isc_retval; 953 954 retval = 0; 955 ctl_pause_rtr = 0; 956 rcv_sync_msg = 0; 957 958 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 959 M_WAITOK | M_ZERO); 960 softc = control_softc; 961 962 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 963 "cam/ctl"); 964 965 softc->dev->si_drv1 = softc; 966 967 /* 968 * By default, return a "bad LUN" peripheral qualifier for unknown 969 * LUNs. The user can override this default using the tunable or 970 * sysctl. See the comment in ctl_inquiry_std() for more details. 971 */ 972 softc->inquiry_pq_no_lun = 1; 973 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 974 &softc->inquiry_pq_no_lun); 975 sysctl_ctx_init(&softc->sysctl_ctx); 976 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 977 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 978 CTLFLAG_RD, 0, "CAM Target Layer"); 979 980 if (softc->sysctl_tree == NULL) { 981 printf("%s: unable to allocate sysctl tree\n", __func__); 982 destroy_dev(softc->dev); 983 free(control_softc, M_DEVBUF); 984 control_softc = NULL; 985 return (ENOMEM); 986 } 987 988 SYSCTL_ADD_INT(&softc->sysctl_ctx, 989 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 990 "inquiry_pq_no_lun", CTLFLAG_RW, 991 &softc->inquiry_pq_no_lun, 0, 992 "Report no lun possible for invalid LUNs"); 993 994 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 995 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 996 softc->open_count = 0; 997 998 /* 999 * Default to actually sending a SYNCHRONIZE CACHE command down to 1000 * the drive. 1001 */ 1002 softc->flags = CTL_FLAG_REAL_SYNC; 1003 1004 /* 1005 * In Copan's HA scheme, the "master" and "slave" roles are 1006 * figured out through the slot the controller is in. Although it 1007 * is an active/active system, someone has to be in charge. 1008 */ 1009#ifdef NEEDTOPORT 1010 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1011#endif 1012 1013 if (sc_id == 0) { 1014 softc->flags |= CTL_FLAG_MASTER_SHELF; 1015 persis_offset = 0; 1016 } else 1017 persis_offset = CTL_MAX_INITIATORS; 1018 1019 /* 1020 * XXX KDM need to figure out where we want to get our target ID 1021 * and WWID. Is it different on each port? 1022 */ 1023 softc->target.id = 0; 1024 softc->target.wwid[0] = 0x12345678; 1025 softc->target.wwid[1] = 0x87654321; 1026 STAILQ_INIT(&softc->lun_list); 1027 STAILQ_INIT(&softc->pending_lun_queue); 1028 STAILQ_INIT(&softc->fe_list); 1029 STAILQ_INIT(&softc->port_list); 1030 STAILQ_INIT(&softc->be_list); 1031 STAILQ_INIT(&softc->io_pools); 1032 ctl_tpc_init(softc); 1033 1034 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1035 &internal_pool)!= 0){ 1036 printf("ctl: can't allocate %d entry internal pool, " 1037 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1038 return (ENOMEM); 1039 } 1040 1041 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1042 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1043 printf("ctl: can't allocate %d entry emergency pool, " 1044 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1045 ctl_pool_free(internal_pool); 1046 return (ENOMEM); 1047 } 1048 1049 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1050 &other_pool) != 0) 1051 { 1052 printf("ctl: can't allocate %d entry other SC pool, " 1053 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1054 ctl_pool_free(internal_pool); 1055 ctl_pool_free(emergency_pool); 1056 return (ENOMEM); 1057 } 1058 1059 softc->internal_pool = internal_pool; 1060 softc->emergency_pool = emergency_pool; 1061 softc->othersc_pool = other_pool; 1062 1063 if (worker_threads <= 0) 1064 worker_threads = max(1, mp_ncpus / 4); 1065 if (worker_threads > CTL_MAX_THREADS) 1066 worker_threads = CTL_MAX_THREADS; 1067 1068 for (i = 0; i < worker_threads; i++) { 1069 struct ctl_thread *thr = &softc->threads[i]; 1070 1071 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1072 thr->ctl_softc = softc; 1073 STAILQ_INIT(&thr->incoming_queue); 1074 STAILQ_INIT(&thr->rtr_queue); 1075 STAILQ_INIT(&thr->done_queue); 1076 STAILQ_INIT(&thr->isc_queue); 1077 1078 error = kproc_kthread_add(ctl_work_thread, thr, 1079 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1080 if (error != 0) { 1081 printf("error creating CTL work thread!\n"); 1082 ctl_pool_free(internal_pool); 1083 ctl_pool_free(emergency_pool); 1084 ctl_pool_free(other_pool); 1085 return (error); 1086 } 1087 } 1088 error = kproc_kthread_add(ctl_lun_thread, softc, 1089 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1090 if (error != 0) { 1091 printf("error creating CTL lun thread!\n"); 1092 ctl_pool_free(internal_pool); 1093 ctl_pool_free(emergency_pool); 1094 ctl_pool_free(other_pool); 1095 return (error); 1096 } 1097 if (bootverbose) 1098 printf("ctl: CAM Target Layer loaded\n"); 1099 1100 /* 1101 * Initialize the ioctl front end. 1102 */ 1103 ctl_frontend_register(&ioctl_frontend); 1104 port = &softc->ioctl_info.port; 1105 port->frontend = &ioctl_frontend; 1106 sprintf(softc->ioctl_info.port_name, "ioctl"); 1107 port->port_type = CTL_PORT_IOCTL; 1108 port->num_requested_ctl_io = 100; 1109 port->port_name = softc->ioctl_info.port_name; 1110 port->port_online = ctl_ioctl_online; 1111 port->port_offline = ctl_ioctl_offline; 1112 port->onoff_arg = &softc->ioctl_info; 1113 port->lun_enable = ctl_ioctl_lun_enable; 1114 port->lun_disable = ctl_ioctl_lun_disable; 1115 port->targ_lun_arg = &softc->ioctl_info; 1116 port->fe_datamove = ctl_ioctl_datamove; 1117 port->fe_done = ctl_ioctl_done; 1118 port->max_targets = 15; 1119 port->max_target_id = 15; 1120 1121 if (ctl_port_register(&softc->ioctl_info.port, 1122 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1123 printf("ctl: ioctl front end registration failed, will " 1124 "continue anyway\n"); 1125 } 1126 1127#ifdef CTL_IO_DELAY 1128 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1129 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1130 sizeof(struct callout), CTL_TIMER_BYTES); 1131 return (EINVAL); 1132 } 1133#endif /* CTL_IO_DELAY */ 1134 1135 return (0); 1136} 1137 1138void 1139ctl_shutdown(void) 1140{ 1141 struct ctl_softc *softc; 1142 struct ctl_lun *lun, *next_lun; 1143 struct ctl_io_pool *pool; 1144 1145 softc = (struct ctl_softc *)control_softc; 1146 1147 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1148 printf("ctl: ioctl front end deregistration failed\n"); 1149 1150 mtx_lock(&softc->ctl_lock); 1151 1152 /* 1153 * Free up each LUN. 1154 */ 1155 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1156 next_lun = STAILQ_NEXT(lun, links); 1157 ctl_free_lun(lun); 1158 } 1159 1160 mtx_unlock(&softc->ctl_lock); 1161 1162 ctl_frontend_deregister(&ioctl_frontend); 1163 1164 /* 1165 * This will rip the rug out from under any FETDs or anyone else 1166 * that has a pool allocated. Since we increment our module 1167 * refcount any time someone outside the main CTL module allocates 1168 * a pool, we shouldn't have any problems here. The user won't be 1169 * able to unload the CTL module until client modules have 1170 * successfully unloaded. 1171 */ 1172 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1173 ctl_pool_free(pool); 1174 1175#if 0 1176 ctl_shutdown_thread(softc->work_thread); 1177 mtx_destroy(&softc->queue_lock); 1178#endif 1179 1180 ctl_tpc_shutdown(softc); 1181 mtx_destroy(&softc->pool_lock); 1182 mtx_destroy(&softc->ctl_lock); 1183 1184 destroy_dev(softc->dev); 1185 1186 sysctl_ctx_free(&softc->sysctl_ctx); 1187 1188 free(control_softc, M_DEVBUF); 1189 control_softc = NULL; 1190 1191 if (bootverbose) 1192 printf("ctl: CAM Target Layer unloaded\n"); 1193} 1194 1195static int 1196ctl_module_event_handler(module_t mod, int what, void *arg) 1197{ 1198 1199 switch (what) { 1200 case MOD_LOAD: 1201 return (ctl_init()); 1202 case MOD_UNLOAD: 1203 return (EBUSY); 1204 default: 1205 return (EOPNOTSUPP); 1206 } 1207} 1208 1209/* 1210 * XXX KDM should we do some access checks here? Bump a reference count to 1211 * prevent a CTL module from being unloaded while someone has it open? 1212 */ 1213static int 1214ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1215{ 1216 return (0); 1217} 1218 1219static int 1220ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1221{ 1222 return (0); 1223} 1224 1225int 1226ctl_port_enable(ctl_port_type port_type) 1227{ 1228 struct ctl_softc *softc; 1229 struct ctl_port *port; 1230 1231 if (ctl_is_single == 0) { 1232 union ctl_ha_msg msg_info; 1233 int isc_retval; 1234 1235#if 0 1236 printf("%s: HA mode, synchronizing frontend enable\n", 1237 __func__); 1238#endif 1239 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1240 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1241 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1242 printf("Sync msg send error retval %d\n", isc_retval); 1243 } 1244 if (!rcv_sync_msg) { 1245 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1246 sizeof(msg_info), 1); 1247 } 1248#if 0 1249 printf("CTL:Frontend Enable\n"); 1250 } else { 1251 printf("%s: single mode, skipping frontend synchronization\n", 1252 __func__); 1253#endif 1254 } 1255 1256 softc = control_softc; 1257 1258 STAILQ_FOREACH(port, &softc->port_list, links) { 1259 if (port_type & port->port_type) 1260 { 1261#if 0 1262 printf("port %d\n", port->targ_port); 1263#endif 1264 ctl_port_online(port); 1265 } 1266 } 1267 1268 return (0); 1269} 1270 1271int 1272ctl_port_disable(ctl_port_type port_type) 1273{ 1274 struct ctl_softc *softc; 1275 struct ctl_port *port; 1276 1277 softc = control_softc; 1278 1279 STAILQ_FOREACH(port, &softc->port_list, links) { 1280 if (port_type & port->port_type) 1281 ctl_port_offline(port); 1282 } 1283 1284 return (0); 1285} 1286 1287/* 1288 * Returns 0 for success, 1 for failure. 1289 * Currently the only failure mode is if there aren't enough entries 1290 * allocated. So, in case of a failure, look at num_entries_dropped, 1291 * reallocate and try again. 1292 */ 1293int 1294ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1295 int *num_entries_filled, int *num_entries_dropped, 1296 ctl_port_type port_type, int no_virtual) 1297{ 1298 struct ctl_softc *softc; 1299 struct ctl_port *port; 1300 int entries_dropped, entries_filled; 1301 int retval; 1302 int i; 1303 1304 softc = control_softc; 1305 1306 retval = 0; 1307 entries_filled = 0; 1308 entries_dropped = 0; 1309 1310 i = 0; 1311 mtx_lock(&softc->ctl_lock); 1312 STAILQ_FOREACH(port, &softc->port_list, links) { 1313 struct ctl_port_entry *entry; 1314 1315 if ((port->port_type & port_type) == 0) 1316 continue; 1317 1318 if ((no_virtual != 0) 1319 && (port->virtual_port != 0)) 1320 continue; 1321 1322 if (entries_filled >= num_entries_alloced) { 1323 entries_dropped++; 1324 continue; 1325 } 1326 entry = &entries[i]; 1327 1328 entry->port_type = port->port_type; 1329 strlcpy(entry->port_name, port->port_name, 1330 sizeof(entry->port_name)); 1331 entry->physical_port = port->physical_port; 1332 entry->virtual_port = port->virtual_port; 1333 entry->wwnn = port->wwnn; 1334 entry->wwpn = port->wwpn; 1335 1336 i++; 1337 entries_filled++; 1338 } 1339 1340 mtx_unlock(&softc->ctl_lock); 1341 1342 if (entries_dropped > 0) 1343 retval = 1; 1344 1345 *num_entries_dropped = entries_dropped; 1346 *num_entries_filled = entries_filled; 1347 1348 return (retval); 1349} 1350 1351static void 1352ctl_ioctl_online(void *arg) 1353{ 1354 struct ctl_ioctl_info *ioctl_info; 1355 1356 ioctl_info = (struct ctl_ioctl_info *)arg; 1357 1358 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1359} 1360 1361static void 1362ctl_ioctl_offline(void *arg) 1363{ 1364 struct ctl_ioctl_info *ioctl_info; 1365 1366 ioctl_info = (struct ctl_ioctl_info *)arg; 1367 1368 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1369} 1370 1371/* 1372 * Remove an initiator by port number and initiator ID. 1373 * Returns 0 for success, -1 for failure. 1374 */ 1375int 1376ctl_remove_initiator(struct ctl_port *port, int iid) 1377{ 1378 struct ctl_softc *softc = control_softc; 1379 1380 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1381 1382 if (iid > CTL_MAX_INIT_PER_PORT) { 1383 printf("%s: initiator ID %u > maximun %u!\n", 1384 __func__, iid, CTL_MAX_INIT_PER_PORT); 1385 return (-1); 1386 } 1387 1388 mtx_lock(&softc->ctl_lock); 1389 port->wwpn_iid[iid].in_use--; 1390 port->wwpn_iid[iid].last_use = time_uptime; 1391 mtx_unlock(&softc->ctl_lock); 1392 1393 return (0); 1394} 1395 1396/* 1397 * Add an initiator to the initiator map. 1398 * Returns iid for success, < 0 for failure. 1399 */ 1400int 1401ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1402{ 1403 struct ctl_softc *softc = control_softc; 1404 time_t best_time; 1405 int i, best; 1406 1407 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1408 1409 if (iid >= CTL_MAX_INIT_PER_PORT) { 1410 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1411 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1412 free(name, M_CTL); 1413 return (-1); 1414 } 1415 1416 mtx_lock(&softc->ctl_lock); 1417 1418 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1419 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1420 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1421 iid = i; 1422 break; 1423 } 1424 if (name != NULL && port->wwpn_iid[i].name != NULL && 1425 strcmp(name, port->wwpn_iid[i].name) == 0) { 1426 iid = i; 1427 break; 1428 } 1429 } 1430 } 1431 1432 if (iid < 0) { 1433 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1434 if (port->wwpn_iid[i].in_use == 0 && 1435 port->wwpn_iid[i].wwpn == 0 && 1436 port->wwpn_iid[i].name == NULL) { 1437 iid = i; 1438 break; 1439 } 1440 } 1441 } 1442 1443 if (iid < 0) { 1444 best = -1; 1445 best_time = INT32_MAX; 1446 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1447 if (port->wwpn_iid[i].in_use == 0) { 1448 if (port->wwpn_iid[i].last_use < best_time) { 1449 best = i; 1450 best_time = port->wwpn_iid[i].last_use; 1451 } 1452 } 1453 } 1454 iid = best; 1455 } 1456 1457 if (iid < 0) { 1458 mtx_unlock(&softc->ctl_lock); 1459 free(name, M_CTL); 1460 return (-2); 1461 } 1462 1463 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1464 /* 1465 * This is not an error yet. 1466 */ 1467 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1468#if 0 1469 printf("%s: port %d iid %u WWPN %#jx arrived" 1470 " again\n", __func__, port->targ_port, 1471 iid, (uintmax_t)wwpn); 1472#endif 1473 goto take; 1474 } 1475 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1476 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1477#if 0 1478 printf("%s: port %d iid %u name '%s' arrived" 1479 " again\n", __func__, port->targ_port, 1480 iid, name); 1481#endif 1482 goto take; 1483 } 1484 1485 /* 1486 * This is an error, but what do we do about it? The 1487 * driver is telling us we have a new WWPN for this 1488 * initiator ID, so we pretty much need to use it. 1489 */ 1490 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1491 " but WWPN %#jx '%s' is still at that address\n", 1492 __func__, port->targ_port, iid, wwpn, name, 1493 (uintmax_t)port->wwpn_iid[iid].wwpn, 1494 port->wwpn_iid[iid].name); 1495 1496 /* 1497 * XXX KDM clear have_ca and ua_pending on each LUN for 1498 * this initiator. 1499 */ 1500 } 1501take: 1502 free(port->wwpn_iid[iid].name, M_CTL); 1503 port->wwpn_iid[iid].name = name; 1504 port->wwpn_iid[iid].wwpn = wwpn; 1505 port->wwpn_iid[iid].in_use++; 1506 mtx_unlock(&softc->ctl_lock); 1507 1508 return (iid); 1509} 1510 1511static int 1512ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1513{ 1514 int len; 1515 1516 switch (port->port_type) { 1517 case CTL_PORT_FC: 1518 { 1519 struct scsi_transportid_fcp *id = 1520 (struct scsi_transportid_fcp *)buf; 1521 if (port->wwpn_iid[iid].wwpn == 0) 1522 return (0); 1523 memset(id, 0, sizeof(*id)); 1524 id->format_protocol = SCSI_PROTO_FC; 1525 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1526 return (sizeof(*id)); 1527 } 1528 case CTL_PORT_ISCSI: 1529 { 1530 struct scsi_transportid_iscsi_port *id = 1531 (struct scsi_transportid_iscsi_port *)buf; 1532 if (port->wwpn_iid[iid].name == NULL) 1533 return (0); 1534 memset(id, 0, 256); 1535 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1536 SCSI_PROTO_ISCSI; 1537 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1538 len = roundup2(min(len, 252), 4); 1539 scsi_ulto2b(len, id->additional_length); 1540 return (sizeof(*id) + len); 1541 } 1542 case CTL_PORT_SAS: 1543 { 1544 struct scsi_transportid_sas *id = 1545 (struct scsi_transportid_sas *)buf; 1546 if (port->wwpn_iid[iid].wwpn == 0) 1547 return (0); 1548 memset(id, 0, sizeof(*id)); 1549 id->format_protocol = SCSI_PROTO_SAS; 1550 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1551 return (sizeof(*id)); 1552 } 1553 default: 1554 { 1555 struct scsi_transportid_spi *id = 1556 (struct scsi_transportid_spi *)buf; 1557 memset(id, 0, sizeof(*id)); 1558 id->format_protocol = SCSI_PROTO_SPI; 1559 scsi_ulto2b(iid, id->scsi_addr); 1560 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1561 return (sizeof(*id)); 1562 } 1563 } 1564} 1565 1566static int 1567ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1568{ 1569 return (0); 1570} 1571 1572static int 1573ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1574{ 1575 return (0); 1576} 1577 1578/* 1579 * Data movement routine for the CTL ioctl frontend port. 1580 */ 1581static int 1582ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1583{ 1584 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1585 struct ctl_sg_entry ext_entry, kern_entry; 1586 int ext_sglen, ext_sg_entries, kern_sg_entries; 1587 int ext_sg_start, ext_offset; 1588 int len_to_copy, len_copied; 1589 int kern_watermark, ext_watermark; 1590 int ext_sglist_malloced; 1591 int i, j; 1592 1593 ext_sglist_malloced = 0; 1594 ext_sg_start = 0; 1595 ext_offset = 0; 1596 1597 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1598 1599 /* 1600 * If this flag is set, fake the data transfer. 1601 */ 1602 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1603 ctsio->ext_data_filled = ctsio->ext_data_len; 1604 goto bailout; 1605 } 1606 1607 /* 1608 * To simplify things here, if we have a single buffer, stick it in 1609 * a S/G entry and just make it a single entry S/G list. 1610 */ 1611 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1612 int len_seen; 1613 1614 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1615 1616 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1617 M_WAITOK); 1618 ext_sglist_malloced = 1; 1619 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1620 ext_sglen) != 0) { 1621 ctl_set_internal_failure(ctsio, 1622 /*sks_valid*/ 0, 1623 /*retry_count*/ 0); 1624 goto bailout; 1625 } 1626 ext_sg_entries = ctsio->ext_sg_entries; 1627 len_seen = 0; 1628 for (i = 0; i < ext_sg_entries; i++) { 1629 if ((len_seen + ext_sglist[i].len) >= 1630 ctsio->ext_data_filled) { 1631 ext_sg_start = i; 1632 ext_offset = ctsio->ext_data_filled - len_seen; 1633 break; 1634 } 1635 len_seen += ext_sglist[i].len; 1636 } 1637 } else { 1638 ext_sglist = &ext_entry; 1639 ext_sglist->addr = ctsio->ext_data_ptr; 1640 ext_sglist->len = ctsio->ext_data_len; 1641 ext_sg_entries = 1; 1642 ext_sg_start = 0; 1643 ext_offset = ctsio->ext_data_filled; 1644 } 1645 1646 if (ctsio->kern_sg_entries > 0) { 1647 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1648 kern_sg_entries = ctsio->kern_sg_entries; 1649 } else { 1650 kern_sglist = &kern_entry; 1651 kern_sglist->addr = ctsio->kern_data_ptr; 1652 kern_sglist->len = ctsio->kern_data_len; 1653 kern_sg_entries = 1; 1654 } 1655 1656 1657 kern_watermark = 0; 1658 ext_watermark = ext_offset; 1659 len_copied = 0; 1660 for (i = ext_sg_start, j = 0; 1661 i < ext_sg_entries && j < kern_sg_entries;) { 1662 uint8_t *ext_ptr, *kern_ptr; 1663 1664 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1665 kern_sglist[j].len - kern_watermark); 1666 1667 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1668 ext_ptr = ext_ptr + ext_watermark; 1669 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1670 /* 1671 * XXX KDM fix this! 1672 */ 1673 panic("need to implement bus address support"); 1674#if 0 1675 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1676#endif 1677 } else 1678 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1679 kern_ptr = kern_ptr + kern_watermark; 1680 1681 kern_watermark += len_to_copy; 1682 ext_watermark += len_to_copy; 1683 1684 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1685 CTL_FLAG_DATA_IN) { 1686 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1687 "bytes to user\n", len_to_copy)); 1688 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1689 "to %p\n", kern_ptr, ext_ptr)); 1690 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1691 ctl_set_internal_failure(ctsio, 1692 /*sks_valid*/ 0, 1693 /*retry_count*/ 0); 1694 goto bailout; 1695 } 1696 } else { 1697 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1698 "bytes from user\n", len_to_copy)); 1699 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1700 "to %p\n", ext_ptr, kern_ptr)); 1701 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1702 ctl_set_internal_failure(ctsio, 1703 /*sks_valid*/ 0, 1704 /*retry_count*/0); 1705 goto bailout; 1706 } 1707 } 1708 1709 len_copied += len_to_copy; 1710 1711 if (ext_sglist[i].len == ext_watermark) { 1712 i++; 1713 ext_watermark = 0; 1714 } 1715 1716 if (kern_sglist[j].len == kern_watermark) { 1717 j++; 1718 kern_watermark = 0; 1719 } 1720 } 1721 1722 ctsio->ext_data_filled += len_copied; 1723 1724 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1725 "kern_sg_entries: %d\n", ext_sg_entries, 1726 kern_sg_entries)); 1727 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1728 "kern_data_len = %d\n", ctsio->ext_data_len, 1729 ctsio->kern_data_len)); 1730 1731 1732 /* XXX KDM set residual?? */ 1733bailout: 1734 1735 if (ext_sglist_malloced != 0) 1736 free(ext_sglist, M_CTL); 1737 1738 return (CTL_RETVAL_COMPLETE); 1739} 1740 1741/* 1742 * Serialize a command that went down the "wrong" side, and so was sent to 1743 * this controller for execution. The logic is a little different than the 1744 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1745 * sent back to the other side, but in the success case, we execute the 1746 * command on this side (XFER mode) or tell the other side to execute it 1747 * (SER_ONLY mode). 1748 */ 1749static int 1750ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1751{ 1752 struct ctl_softc *ctl_softc; 1753 union ctl_ha_msg msg_info; 1754 struct ctl_lun *lun; 1755 int retval = 0; 1756 uint32_t targ_lun; 1757 1758 ctl_softc = control_softc; 1759 1760 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1761 lun = ctl_softc->ctl_luns[targ_lun]; 1762 if (lun==NULL) 1763 { 1764 /* 1765 * Why isn't LUN defined? The other side wouldn't 1766 * send a cmd if the LUN is undefined. 1767 */ 1768 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1769 1770 /* "Logical unit not supported" */ 1771 ctl_set_sense_data(&msg_info.scsi.sense_data, 1772 lun, 1773 /*sense_format*/SSD_TYPE_NONE, 1774 /*current_error*/ 1, 1775 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1776 /*asc*/ 0x25, 1777 /*ascq*/ 0x00, 1778 SSD_ELEM_NONE); 1779 1780 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1781 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1782 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1783 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1784 msg_info.hdr.serializing_sc = NULL; 1785 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1786 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1787 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1788 } 1789 return(1); 1790 1791 } 1792 1793 mtx_lock(&lun->lun_lock); 1794 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1795 1796 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1797 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1798 ooa_links))) { 1799 case CTL_ACTION_BLOCK: 1800 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1801 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1802 blocked_links); 1803 break; 1804 case CTL_ACTION_PASS: 1805 case CTL_ACTION_SKIP: 1806 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1807 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1808 ctl_enqueue_rtr((union ctl_io *)ctsio); 1809 } else { 1810 1811 /* send msg back to other side */ 1812 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1813 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1814 msg_info.hdr.msg_type = CTL_MSG_R2R; 1815#if 0 1816 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1817#endif 1818 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1819 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1820 } 1821 } 1822 break; 1823 case CTL_ACTION_OVERLAP: 1824 /* OVERLAPPED COMMANDS ATTEMPTED */ 1825 ctl_set_sense_data(&msg_info.scsi.sense_data, 1826 lun, 1827 /*sense_format*/SSD_TYPE_NONE, 1828 /*current_error*/ 1, 1829 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1830 /*asc*/ 0x4E, 1831 /*ascq*/ 0x00, 1832 SSD_ELEM_NONE); 1833 1834 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1835 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1836 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1837 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1838 msg_info.hdr.serializing_sc = NULL; 1839 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1840#if 0 1841 printf("BAD JUJU:Major Bummer Overlap\n"); 1842#endif 1843 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1844 retval = 1; 1845 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1846 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1847 } 1848 break; 1849 case CTL_ACTION_OVERLAP_TAG: 1850 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1851 ctl_set_sense_data(&msg_info.scsi.sense_data, 1852 lun, 1853 /*sense_format*/SSD_TYPE_NONE, 1854 /*current_error*/ 1, 1855 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1856 /*asc*/ 0x4D, 1857 /*ascq*/ ctsio->tag_num & 0xff, 1858 SSD_ELEM_NONE); 1859 1860 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1861 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1862 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1863 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1864 msg_info.hdr.serializing_sc = NULL; 1865 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1866#if 0 1867 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1868#endif 1869 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1870 retval = 1; 1871 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1872 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1873 } 1874 break; 1875 case CTL_ACTION_ERROR: 1876 default: 1877 /* "Internal target failure" */ 1878 ctl_set_sense_data(&msg_info.scsi.sense_data, 1879 lun, 1880 /*sense_format*/SSD_TYPE_NONE, 1881 /*current_error*/ 1, 1882 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1883 /*asc*/ 0x44, 1884 /*ascq*/ 0x00, 1885 SSD_ELEM_NONE); 1886 1887 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1888 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1889 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1890 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1891 msg_info.hdr.serializing_sc = NULL; 1892 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1893#if 0 1894 printf("BAD JUJU:Major Bummer HW Error\n"); 1895#endif 1896 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1897 retval = 1; 1898 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1899 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1900 } 1901 break; 1902 } 1903 mtx_unlock(&lun->lun_lock); 1904 return (retval); 1905} 1906 1907static int 1908ctl_ioctl_submit_wait(union ctl_io *io) 1909{ 1910 struct ctl_fe_ioctl_params params; 1911 ctl_fe_ioctl_state last_state; 1912 int done, retval; 1913 1914 retval = 0; 1915 1916 bzero(¶ms, sizeof(params)); 1917 1918 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1919 cv_init(¶ms.sem, "ctlioccv"); 1920 params.state = CTL_IOCTL_INPROG; 1921 last_state = params.state; 1922 1923 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1924 1925 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1926 1927 /* This shouldn't happen */ 1928 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1929 return (retval); 1930 1931 done = 0; 1932 1933 do { 1934 mtx_lock(¶ms.ioctl_mtx); 1935 /* 1936 * Check the state here, and don't sleep if the state has 1937 * already changed (i.e. wakeup has already occured, but we 1938 * weren't waiting yet). 1939 */ 1940 if (params.state == last_state) { 1941 /* XXX KDM cv_wait_sig instead? */ 1942 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1943 } 1944 last_state = params.state; 1945 1946 switch (params.state) { 1947 case CTL_IOCTL_INPROG: 1948 /* Why did we wake up? */ 1949 /* XXX KDM error here? */ 1950 mtx_unlock(¶ms.ioctl_mtx); 1951 break; 1952 case CTL_IOCTL_DATAMOVE: 1953 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1954 1955 /* 1956 * change last_state back to INPROG to avoid 1957 * deadlock on subsequent data moves. 1958 */ 1959 params.state = last_state = CTL_IOCTL_INPROG; 1960 1961 mtx_unlock(¶ms.ioctl_mtx); 1962 ctl_ioctl_do_datamove(&io->scsiio); 1963 /* 1964 * Note that in some cases, most notably writes, 1965 * this will queue the I/O and call us back later. 1966 * In other cases, generally reads, this routine 1967 * will immediately call back and wake us up, 1968 * probably using our own context. 1969 */ 1970 io->scsiio.be_move_done(io); 1971 break; 1972 case CTL_IOCTL_DONE: 1973 mtx_unlock(¶ms.ioctl_mtx); 1974 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1975 done = 1; 1976 break; 1977 default: 1978 mtx_unlock(¶ms.ioctl_mtx); 1979 /* XXX KDM error here? */ 1980 break; 1981 } 1982 } while (done == 0); 1983 1984 mtx_destroy(¶ms.ioctl_mtx); 1985 cv_destroy(¶ms.sem); 1986 1987 return (CTL_RETVAL_COMPLETE); 1988} 1989 1990static void 1991ctl_ioctl_datamove(union ctl_io *io) 1992{ 1993 struct ctl_fe_ioctl_params *params; 1994 1995 params = (struct ctl_fe_ioctl_params *) 1996 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1997 1998 mtx_lock(¶ms->ioctl_mtx); 1999 params->state = CTL_IOCTL_DATAMOVE; 2000 cv_broadcast(¶ms->sem); 2001 mtx_unlock(¶ms->ioctl_mtx); 2002} 2003 2004static void 2005ctl_ioctl_done(union ctl_io *io) 2006{ 2007 struct ctl_fe_ioctl_params *params; 2008 2009 params = (struct ctl_fe_ioctl_params *) 2010 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2011 2012 mtx_lock(¶ms->ioctl_mtx); 2013 params->state = CTL_IOCTL_DONE; 2014 cv_broadcast(¶ms->sem); 2015 mtx_unlock(¶ms->ioctl_mtx); 2016} 2017 2018static void 2019ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2020{ 2021 struct ctl_fe_ioctl_startstop_info *sd_info; 2022 2023 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2024 2025 sd_info->hs_info.status = metatask->status; 2026 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2027 sd_info->hs_info.luns_complete = 2028 metatask->taskinfo.startstop.luns_complete; 2029 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2030 2031 cv_broadcast(&sd_info->sem); 2032} 2033 2034static void 2035ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2036{ 2037 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2038 2039 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2040 2041 mtx_lock(fe_bbr_info->lock); 2042 fe_bbr_info->bbr_info->status = metatask->status; 2043 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2044 fe_bbr_info->wakeup_done = 1; 2045 mtx_unlock(fe_bbr_info->lock); 2046 2047 cv_broadcast(&fe_bbr_info->sem); 2048} 2049 2050/* 2051 * Returns 0 for success, errno for failure. 2052 */ 2053static int 2054ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2055 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2056{ 2057 union ctl_io *io; 2058 int retval; 2059 2060 retval = 0; 2061 2062 mtx_lock(&lun->lun_lock); 2063 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2064 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2065 ooa_links)) { 2066 struct ctl_ooa_entry *entry; 2067 2068 /* 2069 * If we've got more than we can fit, just count the 2070 * remaining entries. 2071 */ 2072 if (*cur_fill_num >= ooa_hdr->alloc_num) 2073 continue; 2074 2075 entry = &kern_entries[*cur_fill_num]; 2076 2077 entry->tag_num = io->scsiio.tag_num; 2078 entry->lun_num = lun->lun; 2079#ifdef CTL_TIME_IO 2080 entry->start_bt = io->io_hdr.start_bt; 2081#endif 2082 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2083 entry->cdb_len = io->scsiio.cdb_len; 2084 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2085 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2086 2087 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2088 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2089 2090 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2091 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2092 2093 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2094 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2095 2096 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2097 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2098 } 2099 mtx_unlock(&lun->lun_lock); 2100 2101 return (retval); 2102} 2103 2104static void * 2105ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2106 size_t error_str_len) 2107{ 2108 void *kptr; 2109 2110 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2111 2112 if (copyin(user_addr, kptr, len) != 0) { 2113 snprintf(error_str, error_str_len, "Error copying %d bytes " 2114 "from user address %p to kernel address %p", len, 2115 user_addr, kptr); 2116 free(kptr, M_CTL); 2117 return (NULL); 2118 } 2119 2120 return (kptr); 2121} 2122 2123static void 2124ctl_free_args(int num_args, struct ctl_be_arg *args) 2125{ 2126 int i; 2127 2128 if (args == NULL) 2129 return; 2130 2131 for (i = 0; i < num_args; i++) { 2132 free(args[i].kname, M_CTL); 2133 free(args[i].kvalue, M_CTL); 2134 } 2135 2136 free(args, M_CTL); 2137} 2138 2139static struct ctl_be_arg * 2140ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2141 char *error_str, size_t error_str_len) 2142{ 2143 struct ctl_be_arg *args; 2144 int i; 2145 2146 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2147 error_str, error_str_len); 2148 2149 if (args == NULL) 2150 goto bailout; 2151 2152 for (i = 0; i < num_args; i++) { 2153 args[i].kname = NULL; 2154 args[i].kvalue = NULL; 2155 } 2156 2157 for (i = 0; i < num_args; i++) { 2158 uint8_t *tmpptr; 2159 2160 args[i].kname = ctl_copyin_alloc(args[i].name, 2161 args[i].namelen, error_str, error_str_len); 2162 if (args[i].kname == NULL) 2163 goto bailout; 2164 2165 if (args[i].kname[args[i].namelen - 1] != '\0') { 2166 snprintf(error_str, error_str_len, "Argument %d " 2167 "name is not NUL-terminated", i); 2168 goto bailout; 2169 } 2170 2171 if (args[i].flags & CTL_BEARG_RD) { 2172 tmpptr = ctl_copyin_alloc(args[i].value, 2173 args[i].vallen, error_str, error_str_len); 2174 if (tmpptr == NULL) 2175 goto bailout; 2176 if ((args[i].flags & CTL_BEARG_ASCII) 2177 && (tmpptr[args[i].vallen - 1] != '\0')) { 2178 snprintf(error_str, error_str_len, "Argument " 2179 "%d value is not NUL-terminated", i); 2180 goto bailout; 2181 } 2182 args[i].kvalue = tmpptr; 2183 } else { 2184 args[i].kvalue = malloc(args[i].vallen, 2185 M_CTL, M_WAITOK | M_ZERO); 2186 } 2187 } 2188 2189 return (args); 2190bailout: 2191 2192 ctl_free_args(num_args, args); 2193 2194 return (NULL); 2195} 2196 2197static void 2198ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2199{ 2200 int i; 2201 2202 for (i = 0; i < num_args; i++) { 2203 if (args[i].flags & CTL_BEARG_WR) 2204 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2205 } 2206} 2207 2208/* 2209 * Escape characters that are illegal or not recommended in XML. 2210 */ 2211int 2212ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2213{ 2214 int retval; 2215 2216 retval = 0; 2217 2218 for (; *str; str++) { 2219 switch (*str) { 2220 case '&': 2221 retval = sbuf_printf(sb, "&"); 2222 break; 2223 case '>': 2224 retval = sbuf_printf(sb, ">"); 2225 break; 2226 case '<': 2227 retval = sbuf_printf(sb, "<"); 2228 break; 2229 default: 2230 retval = sbuf_putc(sb, *str); 2231 break; 2232 } 2233 2234 if (retval != 0) 2235 break; 2236 2237 } 2238 2239 return (retval); 2240} 2241 2242static int 2243ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2244 struct thread *td) 2245{ 2246 struct ctl_softc *softc; 2247 int retval; 2248 2249 softc = control_softc; 2250 2251 retval = 0; 2252 2253 switch (cmd) { 2254 case CTL_IO: { 2255 union ctl_io *io; 2256 void *pool_tmp; 2257 2258 /* 2259 * If we haven't been "enabled", don't allow any SCSI I/O 2260 * to this FETD. 2261 */ 2262 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2263 retval = EPERM; 2264 break; 2265 } 2266 2267 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2268 if (io == NULL) { 2269 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2270 retval = ENOSPC; 2271 break; 2272 } 2273 2274 /* 2275 * Need to save the pool reference so it doesn't get 2276 * spammed by the user's ctl_io. 2277 */ 2278 pool_tmp = io->io_hdr.pool; 2279 2280 memcpy(io, (void *)addr, sizeof(*io)); 2281 2282 io->io_hdr.pool = pool_tmp; 2283 /* 2284 * No status yet, so make sure the status is set properly. 2285 */ 2286 io->io_hdr.status = CTL_STATUS_NONE; 2287 2288 /* 2289 * The user sets the initiator ID, target and LUN IDs. 2290 */ 2291 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2292 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2293 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2294 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2295 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2296 2297 retval = ctl_ioctl_submit_wait(io); 2298 2299 if (retval != 0) { 2300 ctl_free_io(io); 2301 break; 2302 } 2303 2304 memcpy((void *)addr, io, sizeof(*io)); 2305 2306 /* return this to our pool */ 2307 ctl_free_io(io); 2308 2309 break; 2310 } 2311 case CTL_ENABLE_PORT: 2312 case CTL_DISABLE_PORT: 2313 case CTL_SET_PORT_WWNS: { 2314 struct ctl_port *port; 2315 struct ctl_port_entry *entry; 2316 2317 entry = (struct ctl_port_entry *)addr; 2318 2319 mtx_lock(&softc->ctl_lock); 2320 STAILQ_FOREACH(port, &softc->port_list, links) { 2321 int action, done; 2322 2323 action = 0; 2324 done = 0; 2325 2326 if ((entry->port_type == CTL_PORT_NONE) 2327 && (entry->targ_port == port->targ_port)) { 2328 /* 2329 * If the user only wants to enable or 2330 * disable or set WWNs on a specific port, 2331 * do the operation and we're done. 2332 */ 2333 action = 1; 2334 done = 1; 2335 } else if (entry->port_type & port->port_type) { 2336 /* 2337 * Compare the user's type mask with the 2338 * particular frontend type to see if we 2339 * have a match. 2340 */ 2341 action = 1; 2342 done = 0; 2343 2344 /* 2345 * Make sure the user isn't trying to set 2346 * WWNs on multiple ports at the same time. 2347 */ 2348 if (cmd == CTL_SET_PORT_WWNS) { 2349 printf("%s: Can't set WWNs on " 2350 "multiple ports\n", __func__); 2351 retval = EINVAL; 2352 break; 2353 } 2354 } 2355 if (action != 0) { 2356 /* 2357 * XXX KDM we have to drop the lock here, 2358 * because the online/offline operations 2359 * can potentially block. We need to 2360 * reference count the frontends so they 2361 * can't go away, 2362 */ 2363 mtx_unlock(&softc->ctl_lock); 2364 2365 if (cmd == CTL_ENABLE_PORT) { 2366 struct ctl_lun *lun; 2367 2368 STAILQ_FOREACH(lun, &softc->lun_list, 2369 links) { 2370 port->lun_enable(port->targ_lun_arg, 2371 lun->target, 2372 lun->lun); 2373 } 2374 2375 ctl_port_online(port); 2376 } else if (cmd == CTL_DISABLE_PORT) { 2377 struct ctl_lun *lun; 2378 2379 ctl_port_offline(port); 2380 2381 STAILQ_FOREACH(lun, &softc->lun_list, 2382 links) { 2383 port->lun_disable( 2384 port->targ_lun_arg, 2385 lun->target, 2386 lun->lun); 2387 } 2388 } 2389 2390 mtx_lock(&softc->ctl_lock); 2391 2392 if (cmd == CTL_SET_PORT_WWNS) 2393 ctl_port_set_wwns(port, 2394 (entry->flags & CTL_PORT_WWNN_VALID) ? 2395 1 : 0, entry->wwnn, 2396 (entry->flags & CTL_PORT_WWPN_VALID) ? 2397 1 : 0, entry->wwpn); 2398 } 2399 if (done != 0) 2400 break; 2401 } 2402 mtx_unlock(&softc->ctl_lock); 2403 break; 2404 } 2405 case CTL_GET_PORT_LIST: { 2406 struct ctl_port *port; 2407 struct ctl_port_list *list; 2408 int i; 2409 2410 list = (struct ctl_port_list *)addr; 2411 2412 if (list->alloc_len != (list->alloc_num * 2413 sizeof(struct ctl_port_entry))) { 2414 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2415 "alloc_num %u * sizeof(struct ctl_port_entry) " 2416 "%zu\n", __func__, list->alloc_len, 2417 list->alloc_num, sizeof(struct ctl_port_entry)); 2418 retval = EINVAL; 2419 break; 2420 } 2421 list->fill_len = 0; 2422 list->fill_num = 0; 2423 list->dropped_num = 0; 2424 i = 0; 2425 mtx_lock(&softc->ctl_lock); 2426 STAILQ_FOREACH(port, &softc->port_list, links) { 2427 struct ctl_port_entry entry, *list_entry; 2428 2429 if (list->fill_num >= list->alloc_num) { 2430 list->dropped_num++; 2431 continue; 2432 } 2433 2434 entry.port_type = port->port_type; 2435 strlcpy(entry.port_name, port->port_name, 2436 sizeof(entry.port_name)); 2437 entry.targ_port = port->targ_port; 2438 entry.physical_port = port->physical_port; 2439 entry.virtual_port = port->virtual_port; 2440 entry.wwnn = port->wwnn; 2441 entry.wwpn = port->wwpn; 2442 if (port->status & CTL_PORT_STATUS_ONLINE) 2443 entry.online = 1; 2444 else 2445 entry.online = 0; 2446 2447 list_entry = &list->entries[i]; 2448 2449 retval = copyout(&entry, list_entry, sizeof(entry)); 2450 if (retval != 0) { 2451 printf("%s: CTL_GET_PORT_LIST: copyout " 2452 "returned %d\n", __func__, retval); 2453 break; 2454 } 2455 i++; 2456 list->fill_num++; 2457 list->fill_len += sizeof(entry); 2458 } 2459 mtx_unlock(&softc->ctl_lock); 2460 2461 /* 2462 * If this is non-zero, we had a copyout fault, so there's 2463 * probably no point in attempting to set the status inside 2464 * the structure. 2465 */ 2466 if (retval != 0) 2467 break; 2468 2469 if (list->dropped_num > 0) 2470 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2471 else 2472 list->status = CTL_PORT_LIST_OK; 2473 break; 2474 } 2475 case CTL_DUMP_OOA: { 2476 struct ctl_lun *lun; 2477 union ctl_io *io; 2478 char printbuf[128]; 2479 struct sbuf sb; 2480 2481 mtx_lock(&softc->ctl_lock); 2482 printf("Dumping OOA queues:\n"); 2483 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2484 mtx_lock(&lun->lun_lock); 2485 for (io = (union ctl_io *)TAILQ_FIRST( 2486 &lun->ooa_queue); io != NULL; 2487 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2488 ooa_links)) { 2489 sbuf_new(&sb, printbuf, sizeof(printbuf), 2490 SBUF_FIXEDLEN); 2491 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2492 (intmax_t)lun->lun, 2493 io->scsiio.tag_num, 2494 (io->io_hdr.flags & 2495 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2496 (io->io_hdr.flags & 2497 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2498 (io->io_hdr.flags & 2499 CTL_FLAG_ABORT) ? " ABORT" : "", 2500 (io->io_hdr.flags & 2501 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2502 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2503 sbuf_finish(&sb); 2504 printf("%s\n", sbuf_data(&sb)); 2505 } 2506 mtx_unlock(&lun->lun_lock); 2507 } 2508 printf("OOA queues dump done\n"); 2509 mtx_unlock(&softc->ctl_lock); 2510 break; 2511 } 2512 case CTL_GET_OOA: { 2513 struct ctl_lun *lun; 2514 struct ctl_ooa *ooa_hdr; 2515 struct ctl_ooa_entry *entries; 2516 uint32_t cur_fill_num; 2517 2518 ooa_hdr = (struct ctl_ooa *)addr; 2519 2520 if ((ooa_hdr->alloc_len == 0) 2521 || (ooa_hdr->alloc_num == 0)) { 2522 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2523 "must be non-zero\n", __func__, 2524 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2525 retval = EINVAL; 2526 break; 2527 } 2528 2529 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2530 sizeof(struct ctl_ooa_entry))) { 2531 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2532 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2533 __func__, ooa_hdr->alloc_len, 2534 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2535 retval = EINVAL; 2536 break; 2537 } 2538 2539 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2540 if (entries == NULL) { 2541 printf("%s: could not allocate %d bytes for OOA " 2542 "dump\n", __func__, ooa_hdr->alloc_len); 2543 retval = ENOMEM; 2544 break; 2545 } 2546 2547 mtx_lock(&softc->ctl_lock); 2548 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2549 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2550 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2551 mtx_unlock(&softc->ctl_lock); 2552 free(entries, M_CTL); 2553 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2554 __func__, (uintmax_t)ooa_hdr->lun_num); 2555 retval = EINVAL; 2556 break; 2557 } 2558 2559 cur_fill_num = 0; 2560 2561 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2562 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2563 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2564 ooa_hdr, entries); 2565 if (retval != 0) 2566 break; 2567 } 2568 if (retval != 0) { 2569 mtx_unlock(&softc->ctl_lock); 2570 free(entries, M_CTL); 2571 break; 2572 } 2573 } else { 2574 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2575 2576 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2577 entries); 2578 } 2579 mtx_unlock(&softc->ctl_lock); 2580 2581 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2582 ooa_hdr->fill_len = ooa_hdr->fill_num * 2583 sizeof(struct ctl_ooa_entry); 2584 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2585 if (retval != 0) { 2586 printf("%s: error copying out %d bytes for OOA dump\n", 2587 __func__, ooa_hdr->fill_len); 2588 } 2589 2590 getbintime(&ooa_hdr->cur_bt); 2591 2592 if (cur_fill_num > ooa_hdr->alloc_num) { 2593 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2594 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2595 } else { 2596 ooa_hdr->dropped_num = 0; 2597 ooa_hdr->status = CTL_OOA_OK; 2598 } 2599 2600 free(entries, M_CTL); 2601 break; 2602 } 2603 case CTL_CHECK_OOA: { 2604 union ctl_io *io; 2605 struct ctl_lun *lun; 2606 struct ctl_ooa_info *ooa_info; 2607 2608 2609 ooa_info = (struct ctl_ooa_info *)addr; 2610 2611 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2612 ooa_info->status = CTL_OOA_INVALID_LUN; 2613 break; 2614 } 2615 mtx_lock(&softc->ctl_lock); 2616 lun = softc->ctl_luns[ooa_info->lun_id]; 2617 if (lun == NULL) { 2618 mtx_unlock(&softc->ctl_lock); 2619 ooa_info->status = CTL_OOA_INVALID_LUN; 2620 break; 2621 } 2622 mtx_lock(&lun->lun_lock); 2623 mtx_unlock(&softc->ctl_lock); 2624 ooa_info->num_entries = 0; 2625 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2626 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2627 &io->io_hdr, ooa_links)) { 2628 ooa_info->num_entries++; 2629 } 2630 mtx_unlock(&lun->lun_lock); 2631 2632 ooa_info->status = CTL_OOA_SUCCESS; 2633 2634 break; 2635 } 2636 case CTL_HARD_START: 2637 case CTL_HARD_STOP: { 2638 struct ctl_fe_ioctl_startstop_info ss_info; 2639 struct cfi_metatask *metatask; 2640 struct mtx hs_mtx; 2641 2642 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2643 2644 cv_init(&ss_info.sem, "hard start/stop cv" ); 2645 2646 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2647 if (metatask == NULL) { 2648 retval = ENOMEM; 2649 mtx_destroy(&hs_mtx); 2650 break; 2651 } 2652 2653 if (cmd == CTL_HARD_START) 2654 metatask->tasktype = CFI_TASK_STARTUP; 2655 else 2656 metatask->tasktype = CFI_TASK_SHUTDOWN; 2657 2658 metatask->callback = ctl_ioctl_hard_startstop_callback; 2659 metatask->callback_arg = &ss_info; 2660 2661 cfi_action(metatask); 2662 2663 /* Wait for the callback */ 2664 mtx_lock(&hs_mtx); 2665 cv_wait_sig(&ss_info.sem, &hs_mtx); 2666 mtx_unlock(&hs_mtx); 2667 2668 /* 2669 * All information has been copied from the metatask by the 2670 * time cv_broadcast() is called, so we free the metatask here. 2671 */ 2672 cfi_free_metatask(metatask); 2673 2674 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2675 2676 mtx_destroy(&hs_mtx); 2677 break; 2678 } 2679 case CTL_BBRREAD: { 2680 struct ctl_bbrread_info *bbr_info; 2681 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2682 struct mtx bbr_mtx; 2683 struct cfi_metatask *metatask; 2684 2685 bbr_info = (struct ctl_bbrread_info *)addr; 2686 2687 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2688 2689 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2690 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2691 2692 fe_bbr_info.bbr_info = bbr_info; 2693 fe_bbr_info.lock = &bbr_mtx; 2694 2695 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2696 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2697 2698 if (metatask == NULL) { 2699 mtx_destroy(&bbr_mtx); 2700 cv_destroy(&fe_bbr_info.sem); 2701 retval = ENOMEM; 2702 break; 2703 } 2704 metatask->tasktype = CFI_TASK_BBRREAD; 2705 metatask->callback = ctl_ioctl_bbrread_callback; 2706 metatask->callback_arg = &fe_bbr_info; 2707 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2708 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2709 metatask->taskinfo.bbrread.len = bbr_info->len; 2710 2711 cfi_action(metatask); 2712 2713 mtx_lock(&bbr_mtx); 2714 while (fe_bbr_info.wakeup_done == 0) 2715 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2716 mtx_unlock(&bbr_mtx); 2717 2718 bbr_info->status = metatask->status; 2719 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2720 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2721 memcpy(&bbr_info->sense_data, 2722 &metatask->taskinfo.bbrread.sense_data, 2723 ctl_min(sizeof(bbr_info->sense_data), 2724 sizeof(metatask->taskinfo.bbrread.sense_data))); 2725 2726 cfi_free_metatask(metatask); 2727 2728 mtx_destroy(&bbr_mtx); 2729 cv_destroy(&fe_bbr_info.sem); 2730 2731 break; 2732 } 2733 case CTL_DELAY_IO: { 2734 struct ctl_io_delay_info *delay_info; 2735#ifdef CTL_IO_DELAY 2736 struct ctl_lun *lun; 2737#endif /* CTL_IO_DELAY */ 2738 2739 delay_info = (struct ctl_io_delay_info *)addr; 2740 2741#ifdef CTL_IO_DELAY 2742 mtx_lock(&softc->ctl_lock); 2743 2744 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2745 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2746 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2747 } else { 2748 lun = softc->ctl_luns[delay_info->lun_id]; 2749 mtx_lock(&lun->lun_lock); 2750 2751 delay_info->status = CTL_DELAY_STATUS_OK; 2752 2753 switch (delay_info->delay_type) { 2754 case CTL_DELAY_TYPE_CONT: 2755 break; 2756 case CTL_DELAY_TYPE_ONESHOT: 2757 break; 2758 default: 2759 delay_info->status = 2760 CTL_DELAY_STATUS_INVALID_TYPE; 2761 break; 2762 } 2763 2764 switch (delay_info->delay_loc) { 2765 case CTL_DELAY_LOC_DATAMOVE: 2766 lun->delay_info.datamove_type = 2767 delay_info->delay_type; 2768 lun->delay_info.datamove_delay = 2769 delay_info->delay_secs; 2770 break; 2771 case CTL_DELAY_LOC_DONE: 2772 lun->delay_info.done_type = 2773 delay_info->delay_type; 2774 lun->delay_info.done_delay = 2775 delay_info->delay_secs; 2776 break; 2777 default: 2778 delay_info->status = 2779 CTL_DELAY_STATUS_INVALID_LOC; 2780 break; 2781 } 2782 mtx_unlock(&lun->lun_lock); 2783 } 2784 2785 mtx_unlock(&softc->ctl_lock); 2786#else 2787 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2788#endif /* CTL_IO_DELAY */ 2789 break; 2790 } 2791 case CTL_REALSYNC_SET: { 2792 int *syncstate; 2793 2794 syncstate = (int *)addr; 2795 2796 mtx_lock(&softc->ctl_lock); 2797 switch (*syncstate) { 2798 case 0: 2799 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2800 break; 2801 case 1: 2802 softc->flags |= CTL_FLAG_REAL_SYNC; 2803 break; 2804 default: 2805 retval = EINVAL; 2806 break; 2807 } 2808 mtx_unlock(&softc->ctl_lock); 2809 break; 2810 } 2811 case CTL_REALSYNC_GET: { 2812 int *syncstate; 2813 2814 syncstate = (int*)addr; 2815 2816 mtx_lock(&softc->ctl_lock); 2817 if (softc->flags & CTL_FLAG_REAL_SYNC) 2818 *syncstate = 1; 2819 else 2820 *syncstate = 0; 2821 mtx_unlock(&softc->ctl_lock); 2822 2823 break; 2824 } 2825 case CTL_SETSYNC: 2826 case CTL_GETSYNC: { 2827 struct ctl_sync_info *sync_info; 2828 struct ctl_lun *lun; 2829 2830 sync_info = (struct ctl_sync_info *)addr; 2831 2832 mtx_lock(&softc->ctl_lock); 2833 lun = softc->ctl_luns[sync_info->lun_id]; 2834 if (lun == NULL) { 2835 mtx_unlock(&softc->ctl_lock); 2836 sync_info->status = CTL_GS_SYNC_NO_LUN; 2837 } 2838 /* 2839 * Get or set the sync interval. We're not bounds checking 2840 * in the set case, hopefully the user won't do something 2841 * silly. 2842 */ 2843 mtx_lock(&lun->lun_lock); 2844 mtx_unlock(&softc->ctl_lock); 2845 if (cmd == CTL_GETSYNC) 2846 sync_info->sync_interval = lun->sync_interval; 2847 else 2848 lun->sync_interval = sync_info->sync_interval; 2849 mtx_unlock(&lun->lun_lock); 2850 2851 sync_info->status = CTL_GS_SYNC_OK; 2852 2853 break; 2854 } 2855 case CTL_GETSTATS: { 2856 struct ctl_stats *stats; 2857 struct ctl_lun *lun; 2858 int i; 2859 2860 stats = (struct ctl_stats *)addr; 2861 2862 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2863 stats->alloc_len) { 2864 stats->status = CTL_SS_NEED_MORE_SPACE; 2865 stats->num_luns = softc->num_luns; 2866 break; 2867 } 2868 /* 2869 * XXX KDM no locking here. If the LUN list changes, 2870 * things can blow up. 2871 */ 2872 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2873 i++, lun = STAILQ_NEXT(lun, links)) { 2874 retval = copyout(&lun->stats, &stats->lun_stats[i], 2875 sizeof(lun->stats)); 2876 if (retval != 0) 2877 break; 2878 } 2879 stats->num_luns = softc->num_luns; 2880 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2881 softc->num_luns; 2882 stats->status = CTL_SS_OK; 2883#ifdef CTL_TIME_IO 2884 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2885#else 2886 stats->flags = CTL_STATS_FLAG_NONE; 2887#endif 2888 getnanouptime(&stats->timestamp); 2889 break; 2890 } 2891 case CTL_ERROR_INJECT: { 2892 struct ctl_error_desc *err_desc, *new_err_desc; 2893 struct ctl_lun *lun; 2894 2895 err_desc = (struct ctl_error_desc *)addr; 2896 2897 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2898 M_WAITOK | M_ZERO); 2899 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2900 2901 mtx_lock(&softc->ctl_lock); 2902 lun = softc->ctl_luns[err_desc->lun_id]; 2903 if (lun == NULL) { 2904 mtx_unlock(&softc->ctl_lock); 2905 free(new_err_desc, M_CTL); 2906 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2907 __func__, (uintmax_t)err_desc->lun_id); 2908 retval = EINVAL; 2909 break; 2910 } 2911 mtx_lock(&lun->lun_lock); 2912 mtx_unlock(&softc->ctl_lock); 2913 2914 /* 2915 * We could do some checking here to verify the validity 2916 * of the request, but given the complexity of error 2917 * injection requests, the checking logic would be fairly 2918 * complex. 2919 * 2920 * For now, if the request is invalid, it just won't get 2921 * executed and might get deleted. 2922 */ 2923 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2924 2925 /* 2926 * XXX KDM check to make sure the serial number is unique, 2927 * in case we somehow manage to wrap. That shouldn't 2928 * happen for a very long time, but it's the right thing to 2929 * do. 2930 */ 2931 new_err_desc->serial = lun->error_serial; 2932 err_desc->serial = lun->error_serial; 2933 lun->error_serial++; 2934 2935 mtx_unlock(&lun->lun_lock); 2936 break; 2937 } 2938 case CTL_ERROR_INJECT_DELETE: { 2939 struct ctl_error_desc *delete_desc, *desc, *desc2; 2940 struct ctl_lun *lun; 2941 int delete_done; 2942 2943 delete_desc = (struct ctl_error_desc *)addr; 2944 delete_done = 0; 2945 2946 mtx_lock(&softc->ctl_lock); 2947 lun = softc->ctl_luns[delete_desc->lun_id]; 2948 if (lun == NULL) { 2949 mtx_unlock(&softc->ctl_lock); 2950 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2951 __func__, (uintmax_t)delete_desc->lun_id); 2952 retval = EINVAL; 2953 break; 2954 } 2955 mtx_lock(&lun->lun_lock); 2956 mtx_unlock(&softc->ctl_lock); 2957 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2958 if (desc->serial != delete_desc->serial) 2959 continue; 2960 2961 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2962 links); 2963 free(desc, M_CTL); 2964 delete_done = 1; 2965 } 2966 mtx_unlock(&lun->lun_lock); 2967 if (delete_done == 0) { 2968 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2969 "error serial %ju on LUN %u\n", __func__, 2970 delete_desc->serial, delete_desc->lun_id); 2971 retval = EINVAL; 2972 break; 2973 } 2974 break; 2975 } 2976 case CTL_DUMP_STRUCTS: { 2977 int i, j, k, idx; 2978 struct ctl_port *port; 2979 struct ctl_frontend *fe; 2980 2981 mtx_lock(&softc->ctl_lock); 2982 printf("CTL Persistent Reservation information start:\n"); 2983 for (i = 0; i < CTL_MAX_LUNS; i++) { 2984 struct ctl_lun *lun; 2985 2986 lun = softc->ctl_luns[i]; 2987 2988 if ((lun == NULL) 2989 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2990 continue; 2991 2992 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2993 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2994 idx = j * CTL_MAX_INIT_PER_PORT + k; 2995 if (lun->per_res[idx].registered == 0) 2996 continue; 2997 printf(" LUN %d port %d iid %d key " 2998 "%#jx\n", i, j, k, 2999 (uintmax_t)scsi_8btou64( 3000 lun->per_res[idx].res_key.key)); 3001 } 3002 } 3003 } 3004 printf("CTL Persistent Reservation information end\n"); 3005 printf("CTL Ports:\n"); 3006 STAILQ_FOREACH(port, &softc->port_list, links) { 3007 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3008 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3009 port->frontend->name, port->port_type, 3010 port->physical_port, port->virtual_port, 3011 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3012 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3013 if (port->wwpn_iid[j].in_use == 0 && 3014 port->wwpn_iid[j].wwpn == 0 && 3015 port->wwpn_iid[j].name == NULL) 3016 continue; 3017 3018 printf(" iid %u use %d WWPN %#jx '%s'\n", 3019 j, port->wwpn_iid[j].in_use, 3020 (uintmax_t)port->wwpn_iid[j].wwpn, 3021 port->wwpn_iid[j].name); 3022 } 3023 } 3024 printf("CTL Port information end\n"); 3025 mtx_unlock(&softc->ctl_lock); 3026 /* 3027 * XXX KDM calling this without a lock. We'd likely want 3028 * to drop the lock before calling the frontend's dump 3029 * routine anyway. 3030 */ 3031 printf("CTL Frontends:\n"); 3032 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3033 printf(" Frontend '%s'\n", fe->name); 3034 if (fe->fe_dump != NULL) 3035 fe->fe_dump(); 3036 } 3037 printf("CTL Frontend information end\n"); 3038 break; 3039 } 3040 case CTL_LUN_REQ: { 3041 struct ctl_lun_req *lun_req; 3042 struct ctl_backend_driver *backend; 3043 3044 lun_req = (struct ctl_lun_req *)addr; 3045 3046 backend = ctl_backend_find(lun_req->backend); 3047 if (backend == NULL) { 3048 lun_req->status = CTL_LUN_ERROR; 3049 snprintf(lun_req->error_str, 3050 sizeof(lun_req->error_str), 3051 "Backend \"%s\" not found.", 3052 lun_req->backend); 3053 break; 3054 } 3055 if (lun_req->num_be_args > 0) { 3056 lun_req->kern_be_args = ctl_copyin_args( 3057 lun_req->num_be_args, 3058 lun_req->be_args, 3059 lun_req->error_str, 3060 sizeof(lun_req->error_str)); 3061 if (lun_req->kern_be_args == NULL) { 3062 lun_req->status = CTL_LUN_ERROR; 3063 break; 3064 } 3065 } 3066 3067 retval = backend->ioctl(dev, cmd, addr, flag, td); 3068 3069 if (lun_req->num_be_args > 0) { 3070 ctl_copyout_args(lun_req->num_be_args, 3071 lun_req->kern_be_args); 3072 ctl_free_args(lun_req->num_be_args, 3073 lun_req->kern_be_args); 3074 } 3075 break; 3076 } 3077 case CTL_LUN_LIST: { 3078 struct sbuf *sb; 3079 struct ctl_lun *lun; 3080 struct ctl_lun_list *list; 3081 struct ctl_option *opt; 3082 3083 list = (struct ctl_lun_list *)addr; 3084 3085 /* 3086 * Allocate a fixed length sbuf here, based on the length 3087 * of the user's buffer. We could allocate an auto-extending 3088 * buffer, and then tell the user how much larger our 3089 * amount of data is than his buffer, but that presents 3090 * some problems: 3091 * 3092 * 1. The sbuf(9) routines use a blocking malloc, and so 3093 * we can't hold a lock while calling them with an 3094 * auto-extending buffer. 3095 * 3096 * 2. There is not currently a LUN reference counting 3097 * mechanism, outside of outstanding transactions on 3098 * the LUN's OOA queue. So a LUN could go away on us 3099 * while we're getting the LUN number, backend-specific 3100 * information, etc. Thus, given the way things 3101 * currently work, we need to hold the CTL lock while 3102 * grabbing LUN information. 3103 * 3104 * So, from the user's standpoint, the best thing to do is 3105 * allocate what he thinks is a reasonable buffer length, 3106 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3107 * double the buffer length and try again. (And repeat 3108 * that until he succeeds.) 3109 */ 3110 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3111 if (sb == NULL) { 3112 list->status = CTL_LUN_LIST_ERROR; 3113 snprintf(list->error_str, sizeof(list->error_str), 3114 "Unable to allocate %d bytes for LUN list", 3115 list->alloc_len); 3116 break; 3117 } 3118 3119 sbuf_printf(sb, "<ctllunlist>\n"); 3120 3121 mtx_lock(&softc->ctl_lock); 3122 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3123 mtx_lock(&lun->lun_lock); 3124 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3125 (uintmax_t)lun->lun); 3126 3127 /* 3128 * Bail out as soon as we see that we've overfilled 3129 * the buffer. 3130 */ 3131 if (retval != 0) 3132 break; 3133 3134 retval = sbuf_printf(sb, "\t<backend_type>%s" 3135 "</backend_type>\n", 3136 (lun->backend == NULL) ? "none" : 3137 lun->backend->name); 3138 3139 if (retval != 0) 3140 break; 3141 3142 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3143 lun->be_lun->lun_type); 3144 3145 if (retval != 0) 3146 break; 3147 3148 if (lun->backend == NULL) { 3149 retval = sbuf_printf(sb, "</lun>\n"); 3150 if (retval != 0) 3151 break; 3152 continue; 3153 } 3154 3155 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3156 (lun->be_lun->maxlba > 0) ? 3157 lun->be_lun->maxlba + 1 : 0); 3158 3159 if (retval != 0) 3160 break; 3161 3162 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3163 lun->be_lun->blocksize); 3164 3165 if (retval != 0) 3166 break; 3167 3168 retval = sbuf_printf(sb, "\t<serial_number>"); 3169 3170 if (retval != 0) 3171 break; 3172 3173 retval = ctl_sbuf_printf_esc(sb, 3174 lun->be_lun->serial_num); 3175 3176 if (retval != 0) 3177 break; 3178 3179 retval = sbuf_printf(sb, "</serial_number>\n"); 3180 3181 if (retval != 0) 3182 break; 3183 3184 retval = sbuf_printf(sb, "\t<device_id>"); 3185 3186 if (retval != 0) 3187 break; 3188 3189 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3190 3191 if (retval != 0) 3192 break; 3193 3194 retval = sbuf_printf(sb, "</device_id>\n"); 3195 3196 if (retval != 0) 3197 break; 3198 3199 if (lun->backend->lun_info != NULL) { 3200 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3201 if (retval != 0) 3202 break; 3203 } 3204 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3205 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3206 opt->name, opt->value, opt->name); 3207 if (retval != 0) 3208 break; 3209 } 3210 3211 retval = sbuf_printf(sb, "</lun>\n"); 3212 3213 if (retval != 0) 3214 break; 3215 mtx_unlock(&lun->lun_lock); 3216 } 3217 if (lun != NULL) 3218 mtx_unlock(&lun->lun_lock); 3219 mtx_unlock(&softc->ctl_lock); 3220 3221 if ((retval != 0) 3222 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3223 retval = 0; 3224 sbuf_delete(sb); 3225 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3226 snprintf(list->error_str, sizeof(list->error_str), 3227 "Out of space, %d bytes is too small", 3228 list->alloc_len); 3229 break; 3230 } 3231 3232 sbuf_finish(sb); 3233 3234 retval = copyout(sbuf_data(sb), list->lun_xml, 3235 sbuf_len(sb) + 1); 3236 3237 list->fill_len = sbuf_len(sb) + 1; 3238 list->status = CTL_LUN_LIST_OK; 3239 sbuf_delete(sb); 3240 break; 3241 } 3242 case CTL_ISCSI: { 3243 struct ctl_iscsi *ci; 3244 struct ctl_frontend *fe; 3245 3246 ci = (struct ctl_iscsi *)addr; 3247 3248 fe = ctl_frontend_find("iscsi"); 3249 if (fe == NULL) { 3250 ci->status = CTL_ISCSI_ERROR; 3251 snprintf(ci->error_str, sizeof(ci->error_str), 3252 "Frontend \"iscsi\" not found."); 3253 break; 3254 } 3255 3256 retval = fe->ioctl(dev, cmd, addr, flag, td); 3257 break; 3258 } 3259 case CTL_PORT_REQ: { 3260 struct ctl_req *req; 3261 struct ctl_frontend *fe; 3262 3263 req = (struct ctl_req *)addr; 3264 3265 fe = ctl_frontend_find(req->driver); 3266 if (fe == NULL) { 3267 req->status = CTL_LUN_ERROR; 3268 snprintf(req->error_str, sizeof(req->error_str), 3269 "Frontend \"%s\" not found.", req->driver); 3270 break; 3271 } 3272 if (req->num_args > 0) { 3273 req->kern_args = ctl_copyin_args(req->num_args, 3274 req->args, req->error_str, sizeof(req->error_str)); 3275 if (req->kern_args == NULL) { 3276 req->status = CTL_LUN_ERROR; 3277 break; 3278 } 3279 } 3280 3281 retval = fe->ioctl(dev, cmd, addr, flag, td); 3282 3283 if (req->num_args > 0) { 3284 ctl_copyout_args(req->num_args, req->kern_args); 3285 ctl_free_args(req->num_args, req->kern_args); 3286 } 3287 break; 3288 } 3289 case CTL_PORT_LIST: { 3290 struct sbuf *sb; 3291 struct ctl_port *port; 3292 struct ctl_lun_list *list; 3293 struct ctl_option *opt; 3294 3295 list = (struct ctl_lun_list *)addr; 3296 3297 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3298 if (sb == NULL) { 3299 list->status = CTL_LUN_LIST_ERROR; 3300 snprintf(list->error_str, sizeof(list->error_str), 3301 "Unable to allocate %d bytes for LUN list", 3302 list->alloc_len); 3303 break; 3304 } 3305 3306 sbuf_printf(sb, "<ctlportlist>\n"); 3307 3308 mtx_lock(&softc->ctl_lock); 3309 STAILQ_FOREACH(port, &softc->port_list, links) { 3310 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3311 (uintmax_t)port->targ_port); 3312 3313 /* 3314 * Bail out as soon as we see that we've overfilled 3315 * the buffer. 3316 */ 3317 if (retval != 0) 3318 break; 3319 3320 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3321 "</frontend_type>\n", port->frontend->name); 3322 if (retval != 0) 3323 break; 3324 3325 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3326 port->port_type); 3327 if (retval != 0) 3328 break; 3329 3330 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3331 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3332 if (retval != 0) 3333 break; 3334 3335 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3336 port->port_name); 3337 if (retval != 0) 3338 break; 3339 3340 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3341 port->physical_port); 3342 if (retval != 0) 3343 break; 3344 3345 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3346 port->virtual_port); 3347 if (retval != 0) 3348 break; 3349 3350 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3351 (uintmax_t)port->wwnn); 3352 if (retval != 0) 3353 break; 3354 3355 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3356 (uintmax_t)port->wwpn); 3357 if (retval != 0) 3358 break; 3359 3360 if (port->port_info != NULL) { 3361 retval = port->port_info(port->onoff_arg, sb); 3362 if (retval != 0) 3363 break; 3364 } 3365 STAILQ_FOREACH(opt, &port->options, links) { 3366 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3367 opt->name, opt->value, opt->name); 3368 if (retval != 0) 3369 break; 3370 } 3371 3372 retval = sbuf_printf(sb, "</targ_port>\n"); 3373 if (retval != 0) 3374 break; 3375 } 3376 mtx_unlock(&softc->ctl_lock); 3377 3378 if ((retval != 0) 3379 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3380 retval = 0; 3381 sbuf_delete(sb); 3382 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3383 snprintf(list->error_str, sizeof(list->error_str), 3384 "Out of space, %d bytes is too small", 3385 list->alloc_len); 3386 break; 3387 } 3388 3389 sbuf_finish(sb); 3390 3391 retval = copyout(sbuf_data(sb), list->lun_xml, 3392 sbuf_len(sb) + 1); 3393 3394 list->fill_len = sbuf_len(sb) + 1; 3395 list->status = CTL_LUN_LIST_OK; 3396 sbuf_delete(sb); 3397 break; 3398 } 3399 default: { 3400 /* XXX KDM should we fix this? */ 3401#if 0 3402 struct ctl_backend_driver *backend; 3403 unsigned int type; 3404 int found; 3405 3406 found = 0; 3407 3408 /* 3409 * We encode the backend type as the ioctl type for backend 3410 * ioctls. So parse it out here, and then search for a 3411 * backend of this type. 3412 */ 3413 type = _IOC_TYPE(cmd); 3414 3415 STAILQ_FOREACH(backend, &softc->be_list, links) { 3416 if (backend->type == type) { 3417 found = 1; 3418 break; 3419 } 3420 } 3421 if (found == 0) { 3422 printf("ctl: unknown ioctl command %#lx or backend " 3423 "%d\n", cmd, type); 3424 retval = EINVAL; 3425 break; 3426 } 3427 retval = backend->ioctl(dev, cmd, addr, flag, td); 3428#endif 3429 retval = ENOTTY; 3430 break; 3431 } 3432 } 3433 return (retval); 3434} 3435 3436uint32_t 3437ctl_get_initindex(struct ctl_nexus *nexus) 3438{ 3439 if (nexus->targ_port < CTL_MAX_PORTS) 3440 return (nexus->initid.id + 3441 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3442 else 3443 return (nexus->initid.id + 3444 ((nexus->targ_port - CTL_MAX_PORTS) * 3445 CTL_MAX_INIT_PER_PORT)); 3446} 3447 3448uint32_t 3449ctl_get_resindex(struct ctl_nexus *nexus) 3450{ 3451 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3452} 3453 3454uint32_t 3455ctl_port_idx(int port_num) 3456{ 3457 if (port_num < CTL_MAX_PORTS) 3458 return(port_num); 3459 else 3460 return(port_num - CTL_MAX_PORTS); 3461} 3462 3463static uint32_t 3464ctl_map_lun(int port_num, uint32_t lun_id) 3465{ 3466 struct ctl_port *port; 3467 3468 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3469 if (port == NULL) 3470 return (UINT32_MAX); 3471 if (port->lun_map == NULL) 3472 return (lun_id); 3473 return (port->lun_map(port->targ_lun_arg, lun_id)); 3474} 3475 3476static uint32_t 3477ctl_map_lun_back(int port_num, uint32_t lun_id) 3478{ 3479 struct ctl_port *port; 3480 uint32_t i; 3481 3482 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3483 if (port->lun_map == NULL) 3484 return (lun_id); 3485 for (i = 0; i < CTL_MAX_LUNS; i++) { 3486 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3487 return (i); 3488 } 3489 return (UINT32_MAX); 3490} 3491 3492/* 3493 * Note: This only works for bitmask sizes that are at least 32 bits, and 3494 * that are a power of 2. 3495 */ 3496int 3497ctl_ffz(uint32_t *mask, uint32_t size) 3498{ 3499 uint32_t num_chunks, num_pieces; 3500 int i, j; 3501 3502 num_chunks = (size >> 5); 3503 if (num_chunks == 0) 3504 num_chunks++; 3505 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3506 3507 for (i = 0; i < num_chunks; i++) { 3508 for (j = 0; j < num_pieces; j++) { 3509 if ((mask[i] & (1 << j)) == 0) 3510 return ((i << 5) + j); 3511 } 3512 } 3513 3514 return (-1); 3515} 3516 3517int 3518ctl_set_mask(uint32_t *mask, uint32_t bit) 3519{ 3520 uint32_t chunk, piece; 3521 3522 chunk = bit >> 5; 3523 piece = bit % (sizeof(uint32_t) * 8); 3524 3525 if ((mask[chunk] & (1 << piece)) != 0) 3526 return (-1); 3527 else 3528 mask[chunk] |= (1 << piece); 3529 3530 return (0); 3531} 3532 3533int 3534ctl_clear_mask(uint32_t *mask, uint32_t bit) 3535{ 3536 uint32_t chunk, piece; 3537 3538 chunk = bit >> 5; 3539 piece = bit % (sizeof(uint32_t) * 8); 3540 3541 if ((mask[chunk] & (1 << piece)) == 0) 3542 return (-1); 3543 else 3544 mask[chunk] &= ~(1 << piece); 3545 3546 return (0); 3547} 3548 3549int 3550ctl_is_set(uint32_t *mask, uint32_t bit) 3551{ 3552 uint32_t chunk, piece; 3553 3554 chunk = bit >> 5; 3555 piece = bit % (sizeof(uint32_t) * 8); 3556 3557 if ((mask[chunk] & (1 << piece)) == 0) 3558 return (0); 3559 else 3560 return (1); 3561} 3562 3563#ifdef unused 3564/* 3565 * The bus, target and lun are optional, they can be filled in later. 3566 * can_wait is used to determine whether we can wait on the malloc or not. 3567 */ 3568union ctl_io* 3569ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3570 uint32_t targ_lun, int can_wait) 3571{ 3572 union ctl_io *io; 3573 3574 if (can_wait) 3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3576 else 3577 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3578 3579 if (io != NULL) { 3580 io->io_hdr.io_type = io_type; 3581 io->io_hdr.targ_port = targ_port; 3582 /* 3583 * XXX KDM this needs to change/go away. We need to move 3584 * to a preallocated pool of ctl_scsiio structures. 3585 */ 3586 io->io_hdr.nexus.targ_target.id = targ_target; 3587 io->io_hdr.nexus.targ_lun = targ_lun; 3588 } 3589 3590 return (io); 3591} 3592 3593void 3594ctl_kfree_io(union ctl_io *io) 3595{ 3596 free(io, M_CTL); 3597} 3598#endif /* unused */ 3599 3600/* 3601 * ctl_softc, pool_type, total_ctl_io are passed in. 3602 * npool is passed out. 3603 */ 3604int 3605ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3606 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3607{ 3608 uint32_t i; 3609 union ctl_io *cur_io, *next_io; 3610 struct ctl_io_pool *pool; 3611 int retval; 3612 3613 retval = 0; 3614 3615 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3616 M_NOWAIT | M_ZERO); 3617 if (pool == NULL) { 3618 retval = ENOMEM; 3619 goto bailout; 3620 } 3621 3622 pool->type = pool_type; 3623 pool->ctl_softc = ctl_softc; 3624 3625 mtx_lock(&ctl_softc->pool_lock); 3626 pool->id = ctl_softc->cur_pool_id++; 3627 mtx_unlock(&ctl_softc->pool_lock); 3628 3629 pool->flags = CTL_POOL_FLAG_NONE; 3630 pool->refcount = 1; /* Reference for validity. */ 3631 STAILQ_INIT(&pool->free_queue); 3632 3633 /* 3634 * XXX KDM other options here: 3635 * - allocate a page at a time 3636 * - allocate one big chunk of memory. 3637 * Page allocation might work well, but would take a little more 3638 * tracking. 3639 */ 3640 for (i = 0; i < total_ctl_io; i++) { 3641 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3642 M_NOWAIT); 3643 if (cur_io == NULL) { 3644 retval = ENOMEM; 3645 break; 3646 } 3647 cur_io->io_hdr.pool = pool; 3648 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3649 pool->total_ctl_io++; 3650 pool->free_ctl_io++; 3651 } 3652 3653 if (retval != 0) { 3654 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3655 cur_io != NULL; cur_io = next_io) { 3656 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3657 links); 3658 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3659 ctl_io_hdr, links); 3660 free(cur_io, M_CTLIO); 3661 } 3662 3663 free(pool, M_CTL); 3664 goto bailout; 3665 } 3666 mtx_lock(&ctl_softc->pool_lock); 3667 ctl_softc->num_pools++; 3668 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3669 /* 3670 * Increment our usage count if this is an external consumer, so we 3671 * can't get unloaded until the external consumer (most likely a 3672 * FETD) unloads and frees his pool. 3673 * 3674 * XXX KDM will this increment the caller's module use count, or 3675 * mine? 3676 */ 3677#if 0 3678 if ((pool_type != CTL_POOL_EMERGENCY) 3679 && (pool_type != CTL_POOL_INTERNAL) 3680 && (pool_type != CTL_POOL_4OTHERSC)) 3681 MOD_INC_USE_COUNT; 3682#endif 3683 3684 mtx_unlock(&ctl_softc->pool_lock); 3685 3686 *npool = pool; 3687 3688bailout: 3689 3690 return (retval); 3691} 3692 3693static int 3694ctl_pool_acquire(struct ctl_io_pool *pool) 3695{ 3696 3697 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3698 3699 if (pool->flags & CTL_POOL_FLAG_INVALID) 3700 return (EINVAL); 3701 3702 pool->refcount++; 3703 3704 return (0); 3705} 3706 3707static void 3708ctl_pool_release(struct ctl_io_pool *pool) 3709{ 3710 struct ctl_softc *ctl_softc = pool->ctl_softc; 3711 union ctl_io *io; 3712 3713 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3714 3715 if (--pool->refcount != 0) 3716 return; 3717 3718 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3719 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3720 links); 3721 free(io, M_CTLIO); 3722 } 3723 3724 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3725 ctl_softc->num_pools--; 3726 3727 /* 3728 * XXX KDM will this decrement the caller's usage count or mine? 3729 */ 3730#if 0 3731 if ((pool->type != CTL_POOL_EMERGENCY) 3732 && (pool->type != CTL_POOL_INTERNAL) 3733 && (pool->type != CTL_POOL_4OTHERSC)) 3734 MOD_DEC_USE_COUNT; 3735#endif 3736 3737 free(pool, M_CTL); 3738} 3739 3740void 3741ctl_pool_free(struct ctl_io_pool *pool) 3742{ 3743 struct ctl_softc *ctl_softc; 3744 3745 if (pool == NULL) 3746 return; 3747 3748 ctl_softc = pool->ctl_softc; 3749 mtx_lock(&ctl_softc->pool_lock); 3750 pool->flags |= CTL_POOL_FLAG_INVALID; 3751 ctl_pool_release(pool); 3752 mtx_unlock(&ctl_softc->pool_lock); 3753} 3754 3755/* 3756 * This routine does not block (except for spinlocks of course). 3757 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3758 * possible. 3759 */ 3760union ctl_io * 3761ctl_alloc_io(void *pool_ref) 3762{ 3763 union ctl_io *io; 3764 struct ctl_softc *ctl_softc; 3765 struct ctl_io_pool *pool, *npool; 3766 struct ctl_io_pool *emergency_pool; 3767 3768 pool = (struct ctl_io_pool *)pool_ref; 3769 3770 if (pool == NULL) { 3771 printf("%s: pool is NULL\n", __func__); 3772 return (NULL); 3773 } 3774 3775 emergency_pool = NULL; 3776 3777 ctl_softc = pool->ctl_softc; 3778 3779 mtx_lock(&ctl_softc->pool_lock); 3780 /* 3781 * First, try to get the io structure from the user's pool. 3782 */ 3783 if (ctl_pool_acquire(pool) == 0) { 3784 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3785 if (io != NULL) { 3786 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3787 pool->total_allocated++; 3788 pool->free_ctl_io--; 3789 mtx_unlock(&ctl_softc->pool_lock); 3790 return (io); 3791 } else 3792 ctl_pool_release(pool); 3793 } 3794 /* 3795 * If he doesn't have any io structures left, search for an 3796 * emergency pool and grab one from there. 3797 */ 3798 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3799 if (npool->type != CTL_POOL_EMERGENCY) 3800 continue; 3801 3802 if (ctl_pool_acquire(npool) != 0) 3803 continue; 3804 3805 emergency_pool = npool; 3806 3807 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3808 if (io != NULL) { 3809 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3810 npool->total_allocated++; 3811 npool->free_ctl_io--; 3812 mtx_unlock(&ctl_softc->pool_lock); 3813 return (io); 3814 } else 3815 ctl_pool_release(npool); 3816 } 3817 3818 /* Drop the spinlock before we malloc */ 3819 mtx_unlock(&ctl_softc->pool_lock); 3820 3821 /* 3822 * The emergency pool (if it exists) didn't have one, so try an 3823 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3824 */ 3825 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3826 if (io != NULL) { 3827 /* 3828 * If the emergency pool exists but is empty, add this 3829 * ctl_io to its list when it gets freed. 3830 */ 3831 if (emergency_pool != NULL) { 3832 mtx_lock(&ctl_softc->pool_lock); 3833 if (ctl_pool_acquire(emergency_pool) == 0) { 3834 io->io_hdr.pool = emergency_pool; 3835 emergency_pool->total_ctl_io++; 3836 /* 3837 * Need to bump this, otherwise 3838 * total_allocated and total_freed won't 3839 * match when we no longer have anything 3840 * outstanding. 3841 */ 3842 emergency_pool->total_allocated++; 3843 } 3844 mtx_unlock(&ctl_softc->pool_lock); 3845 } else 3846 io->io_hdr.pool = NULL; 3847 } 3848 3849 return (io); 3850} 3851 3852void 3853ctl_free_io(union ctl_io *io) 3854{ 3855 if (io == NULL) 3856 return; 3857 3858 /* 3859 * If this ctl_io has a pool, return it to that pool. 3860 */ 3861 if (io->io_hdr.pool != NULL) { 3862 struct ctl_io_pool *pool; 3863 3864 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3865 mtx_lock(&pool->ctl_softc->pool_lock); 3866 io->io_hdr.io_type = 0xff; 3867 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3868 pool->total_freed++; 3869 pool->free_ctl_io++; 3870 ctl_pool_release(pool); 3871 mtx_unlock(&pool->ctl_softc->pool_lock); 3872 } else { 3873 /* 3874 * Otherwise, just free it. We probably malloced it and 3875 * the emergency pool wasn't available. 3876 */ 3877 free(io, M_CTLIO); 3878 } 3879 3880} 3881 3882void 3883ctl_zero_io(union ctl_io *io) 3884{ 3885 void *pool_ref; 3886 3887 if (io == NULL) 3888 return; 3889 3890 /* 3891 * May need to preserve linked list pointers at some point too. 3892 */ 3893 pool_ref = io->io_hdr.pool; 3894 3895 memset(io, 0, sizeof(*io)); 3896 3897 io->io_hdr.pool = pool_ref; 3898} 3899 3900/* 3901 * This routine is currently used for internal copies of ctl_ios that need 3902 * to persist for some reason after we've already returned status to the 3903 * FETD. (Thus the flag set.) 3904 * 3905 * XXX XXX 3906 * Note that this makes a blind copy of all fields in the ctl_io, except 3907 * for the pool reference. This includes any memory that has been 3908 * allocated! That memory will no longer be valid after done has been 3909 * called, so this would be VERY DANGEROUS for command that actually does 3910 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3911 * start and stop commands, which don't transfer any data, so this is not a 3912 * problem. If it is used for anything else, the caller would also need to 3913 * allocate data buffer space and this routine would need to be modified to 3914 * copy the data buffer(s) as well. 3915 */ 3916void 3917ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3918{ 3919 void *pool_ref; 3920 3921 if ((src == NULL) 3922 || (dest == NULL)) 3923 return; 3924 3925 /* 3926 * May need to preserve linked list pointers at some point too. 3927 */ 3928 pool_ref = dest->io_hdr.pool; 3929 3930 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3931 3932 dest->io_hdr.pool = pool_ref; 3933 /* 3934 * We need to know that this is an internal copy, and doesn't need 3935 * to get passed back to the FETD that allocated it. 3936 */ 3937 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3938} 3939 3940#ifdef NEEDTOPORT 3941static void 3942ctl_update_power_subpage(struct copan_power_subpage *page) 3943{ 3944 int num_luns, num_partitions, config_type; 3945 struct ctl_softc *softc; 3946 cs_BOOL_t aor_present, shelf_50pct_power; 3947 cs_raidset_personality_t rs_type; 3948 int max_active_luns; 3949 3950 softc = control_softc; 3951 3952 /* subtract out the processor LUN */ 3953 num_luns = softc->num_luns - 1; 3954 /* 3955 * Default to 7 LUNs active, which was the only number we allowed 3956 * in the past. 3957 */ 3958 max_active_luns = 7; 3959 3960 num_partitions = config_GetRsPartitionInfo(); 3961 config_type = config_GetConfigType(); 3962 shelf_50pct_power = config_GetShelfPowerMode(); 3963 aor_present = config_IsAorRsPresent(); 3964 3965 rs_type = ddb_GetRsRaidType(1); 3966 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3967 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3968 EPRINT(0, "Unsupported RS type %d!", rs_type); 3969 } 3970 3971 3972 page->total_luns = num_luns; 3973 3974 switch (config_type) { 3975 case 40: 3976 /* 3977 * In a 40 drive configuration, it doesn't matter what DC 3978 * cards we have, whether we have AOR enabled or not, 3979 * partitioning or not, or what type of RAIDset we have. 3980 * In that scenario, we can power up every LUN we present 3981 * to the user. 3982 */ 3983 max_active_luns = num_luns; 3984 3985 break; 3986 case 64: 3987 if (shelf_50pct_power == CS_FALSE) { 3988 /* 25% power */ 3989 if (aor_present == CS_TRUE) { 3990 if (rs_type == 3991 CS_RAIDSET_PERSONALITY_RAID5) { 3992 max_active_luns = 7; 3993 } else if (rs_type == 3994 CS_RAIDSET_PERSONALITY_RAID1){ 3995 max_active_luns = 14; 3996 } else { 3997 /* XXX KDM now what?? */ 3998 } 3999 } else { 4000 if (rs_type == 4001 CS_RAIDSET_PERSONALITY_RAID5) { 4002 max_active_luns = 8; 4003 } else if (rs_type == 4004 CS_RAIDSET_PERSONALITY_RAID1){ 4005 max_active_luns = 16; 4006 } else { 4007 /* XXX KDM now what?? */ 4008 } 4009 } 4010 } else { 4011 /* 50% power */ 4012 /* 4013 * With 50% power in a 64 drive configuration, we 4014 * can power all LUNs we present. 4015 */ 4016 max_active_luns = num_luns; 4017 } 4018 break; 4019 case 112: 4020 if (shelf_50pct_power == CS_FALSE) { 4021 /* 25% power */ 4022 if (aor_present == CS_TRUE) { 4023 if (rs_type == 4024 CS_RAIDSET_PERSONALITY_RAID5) { 4025 max_active_luns = 7; 4026 } else if (rs_type == 4027 CS_RAIDSET_PERSONALITY_RAID1){ 4028 max_active_luns = 14; 4029 } else { 4030 /* XXX KDM now what?? */ 4031 } 4032 } else { 4033 if (rs_type == 4034 CS_RAIDSET_PERSONALITY_RAID5) { 4035 max_active_luns = 8; 4036 } else if (rs_type == 4037 CS_RAIDSET_PERSONALITY_RAID1){ 4038 max_active_luns = 16; 4039 } else { 4040 /* XXX KDM now what?? */ 4041 } 4042 } 4043 } else { 4044 /* 50% power */ 4045 if (aor_present == CS_TRUE) { 4046 if (rs_type == 4047 CS_RAIDSET_PERSONALITY_RAID5) { 4048 max_active_luns = 14; 4049 } else if (rs_type == 4050 CS_RAIDSET_PERSONALITY_RAID1){ 4051 /* 4052 * We're assuming here that disk 4053 * caching is enabled, and so we're 4054 * able to power up half of each 4055 * LUN, and cache all writes. 4056 */ 4057 max_active_luns = num_luns; 4058 } else { 4059 /* XXX KDM now what?? */ 4060 } 4061 } else { 4062 if (rs_type == 4063 CS_RAIDSET_PERSONALITY_RAID5) { 4064 max_active_luns = 15; 4065 } else if (rs_type == 4066 CS_RAIDSET_PERSONALITY_RAID1){ 4067 max_active_luns = 30; 4068 } else { 4069 /* XXX KDM now what?? */ 4070 } 4071 } 4072 } 4073 break; 4074 default: 4075 /* 4076 * In this case, we have an unknown configuration, so we 4077 * just use the default from above. 4078 */ 4079 break; 4080 } 4081 4082 page->max_active_luns = max_active_luns; 4083#if 0 4084 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4085 page->total_luns, page->max_active_luns); 4086#endif 4087} 4088#endif /* NEEDTOPORT */ 4089 4090/* 4091 * This routine could be used in the future to load default and/or saved 4092 * mode page parameters for a particuar lun. 4093 */ 4094static int 4095ctl_init_page_index(struct ctl_lun *lun) 4096{ 4097 int i; 4098 struct ctl_page_index *page_index; 4099 struct ctl_softc *softc; 4100 const char *value; 4101 4102 memcpy(&lun->mode_pages.index, page_index_template, 4103 sizeof(page_index_template)); 4104 4105 softc = lun->ctl_softc; 4106 4107 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4108 4109 page_index = &lun->mode_pages.index[i]; 4110 /* 4111 * If this is a disk-only mode page, there's no point in 4112 * setting it up. For some pages, we have to have some 4113 * basic information about the disk in order to calculate the 4114 * mode page data. 4115 */ 4116 if ((lun->be_lun->lun_type != T_DIRECT) 4117 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4118 continue; 4119 4120 switch (page_index->page_code & SMPH_PC_MASK) { 4121 case SMS_FORMAT_DEVICE_PAGE: { 4122 struct scsi_format_page *format_page; 4123 4124 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4125 panic("subpage is incorrect!"); 4126 4127 /* 4128 * Sectors per track are set above. Bytes per 4129 * sector need to be set here on a per-LUN basis. 4130 */ 4131 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4132 &format_page_default, 4133 sizeof(format_page_default)); 4134 memcpy(&lun->mode_pages.format_page[ 4135 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4136 sizeof(format_page_changeable)); 4137 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4138 &format_page_default, 4139 sizeof(format_page_default)); 4140 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4141 &format_page_default, 4142 sizeof(format_page_default)); 4143 4144 format_page = &lun->mode_pages.format_page[ 4145 CTL_PAGE_CURRENT]; 4146 scsi_ulto2b(lun->be_lun->blocksize, 4147 format_page->bytes_per_sector); 4148 4149 format_page = &lun->mode_pages.format_page[ 4150 CTL_PAGE_DEFAULT]; 4151 scsi_ulto2b(lun->be_lun->blocksize, 4152 format_page->bytes_per_sector); 4153 4154 format_page = &lun->mode_pages.format_page[ 4155 CTL_PAGE_SAVED]; 4156 scsi_ulto2b(lun->be_lun->blocksize, 4157 format_page->bytes_per_sector); 4158 4159 page_index->page_data = 4160 (uint8_t *)lun->mode_pages.format_page; 4161 break; 4162 } 4163 case SMS_RIGID_DISK_PAGE: { 4164 struct scsi_rigid_disk_page *rigid_disk_page; 4165 uint32_t sectors_per_cylinder; 4166 uint64_t cylinders; 4167#ifndef __XSCALE__ 4168 int shift; 4169#endif /* !__XSCALE__ */ 4170 4171 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4172 panic("invalid subpage value %d", 4173 page_index->subpage); 4174 4175 /* 4176 * Rotation rate and sectors per track are set 4177 * above. We calculate the cylinders here based on 4178 * capacity. Due to the number of heads and 4179 * sectors per track we're using, smaller arrays 4180 * may turn out to have 0 cylinders. Linux and 4181 * FreeBSD don't pay attention to these mode pages 4182 * to figure out capacity, but Solaris does. It 4183 * seems to deal with 0 cylinders just fine, and 4184 * works out a fake geometry based on the capacity. 4185 */ 4186 memcpy(&lun->mode_pages.rigid_disk_page[ 4187 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4188 sizeof(rigid_disk_page_default)); 4189 memcpy(&lun->mode_pages.rigid_disk_page[ 4190 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4191 sizeof(rigid_disk_page_changeable)); 4192 memcpy(&lun->mode_pages.rigid_disk_page[ 4193 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4194 sizeof(rigid_disk_page_default)); 4195 memcpy(&lun->mode_pages.rigid_disk_page[ 4196 CTL_PAGE_SAVED], &rigid_disk_page_default, 4197 sizeof(rigid_disk_page_default)); 4198 4199 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4200 CTL_DEFAULT_HEADS; 4201 4202 /* 4203 * The divide method here will be more accurate, 4204 * probably, but results in floating point being 4205 * used in the kernel on i386 (__udivdi3()). On the 4206 * XScale, though, __udivdi3() is implemented in 4207 * software. 4208 * 4209 * The shift method for cylinder calculation is 4210 * accurate if sectors_per_cylinder is a power of 4211 * 2. Otherwise it might be slightly off -- you 4212 * might have a bit of a truncation problem. 4213 */ 4214#ifdef __XSCALE__ 4215 cylinders = (lun->be_lun->maxlba + 1) / 4216 sectors_per_cylinder; 4217#else 4218 for (shift = 31; shift > 0; shift--) { 4219 if (sectors_per_cylinder & (1 << shift)) 4220 break; 4221 } 4222 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4223#endif 4224 4225 /* 4226 * We've basically got 3 bytes, or 24 bits for the 4227 * cylinder size in the mode page. If we're over, 4228 * just round down to 2^24. 4229 */ 4230 if (cylinders > 0xffffff) 4231 cylinders = 0xffffff; 4232 4233 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4234 CTL_PAGE_CURRENT]; 4235 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4236 4237 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4238 CTL_PAGE_DEFAULT]; 4239 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4240 4241 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4242 CTL_PAGE_SAVED]; 4243 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4244 4245 page_index->page_data = 4246 (uint8_t *)lun->mode_pages.rigid_disk_page; 4247 break; 4248 } 4249 case SMS_CACHING_PAGE: { 4250 struct scsi_caching_page *caching_page; 4251 4252 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4253 panic("invalid subpage value %d", 4254 page_index->subpage); 4255 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4256 &caching_page_default, 4257 sizeof(caching_page_default)); 4258 memcpy(&lun->mode_pages.caching_page[ 4259 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4260 sizeof(caching_page_changeable)); 4261 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4262 &caching_page_default, 4263 sizeof(caching_page_default)); 4264 caching_page = &lun->mode_pages.caching_page[ 4265 CTL_PAGE_SAVED]; 4266 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4267 if (value != NULL && strcmp(value, "off") == 0) 4268 caching_page->flags1 &= ~SCP_WCE; 4269 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4270 if (value != NULL && strcmp(value, "off") == 0) 4271 caching_page->flags1 |= SCP_RCD; 4272 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4273 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4274 sizeof(caching_page_default)); 4275 page_index->page_data = 4276 (uint8_t *)lun->mode_pages.caching_page; 4277 break; 4278 } 4279 case SMS_CONTROL_MODE_PAGE: { 4280 struct scsi_control_page *control_page; 4281 4282 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4283 panic("invalid subpage value %d", 4284 page_index->subpage); 4285 4286 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4287 &control_page_default, 4288 sizeof(control_page_default)); 4289 memcpy(&lun->mode_pages.control_page[ 4290 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4291 sizeof(control_page_changeable)); 4292 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4293 &control_page_default, 4294 sizeof(control_page_default)); 4295 control_page = &lun->mode_pages.control_page[ 4296 CTL_PAGE_SAVED]; 4297 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4298 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4299 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4300 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4301 } 4302 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4303 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4304 sizeof(control_page_default)); 4305 page_index->page_data = 4306 (uint8_t *)lun->mode_pages.control_page; 4307 break; 4308 4309 } 4310 case SMS_VENDOR_SPECIFIC_PAGE:{ 4311 switch (page_index->subpage) { 4312 case PWR_SUBPAGE_CODE: { 4313 struct copan_power_subpage *current_page, 4314 *saved_page; 4315 4316 memcpy(&lun->mode_pages.power_subpage[ 4317 CTL_PAGE_CURRENT], 4318 &power_page_default, 4319 sizeof(power_page_default)); 4320 memcpy(&lun->mode_pages.power_subpage[ 4321 CTL_PAGE_CHANGEABLE], 4322 &power_page_changeable, 4323 sizeof(power_page_changeable)); 4324 memcpy(&lun->mode_pages.power_subpage[ 4325 CTL_PAGE_DEFAULT], 4326 &power_page_default, 4327 sizeof(power_page_default)); 4328 memcpy(&lun->mode_pages.power_subpage[ 4329 CTL_PAGE_SAVED], 4330 &power_page_default, 4331 sizeof(power_page_default)); 4332 page_index->page_data = 4333 (uint8_t *)lun->mode_pages.power_subpage; 4334 4335 current_page = (struct copan_power_subpage *) 4336 (page_index->page_data + 4337 (page_index->page_len * 4338 CTL_PAGE_CURRENT)); 4339 saved_page = (struct copan_power_subpage *) 4340 (page_index->page_data + 4341 (page_index->page_len * 4342 CTL_PAGE_SAVED)); 4343 break; 4344 } 4345 case APS_SUBPAGE_CODE: { 4346 struct copan_aps_subpage *current_page, 4347 *saved_page; 4348 4349 // This gets set multiple times but 4350 // it should always be the same. It's 4351 // only done during init so who cares. 4352 index_to_aps_page = i; 4353 4354 memcpy(&lun->mode_pages.aps_subpage[ 4355 CTL_PAGE_CURRENT], 4356 &aps_page_default, 4357 sizeof(aps_page_default)); 4358 memcpy(&lun->mode_pages.aps_subpage[ 4359 CTL_PAGE_CHANGEABLE], 4360 &aps_page_changeable, 4361 sizeof(aps_page_changeable)); 4362 memcpy(&lun->mode_pages.aps_subpage[ 4363 CTL_PAGE_DEFAULT], 4364 &aps_page_default, 4365 sizeof(aps_page_default)); 4366 memcpy(&lun->mode_pages.aps_subpage[ 4367 CTL_PAGE_SAVED], 4368 &aps_page_default, 4369 sizeof(aps_page_default)); 4370 page_index->page_data = 4371 (uint8_t *)lun->mode_pages.aps_subpage; 4372 4373 current_page = (struct copan_aps_subpage *) 4374 (page_index->page_data + 4375 (page_index->page_len * 4376 CTL_PAGE_CURRENT)); 4377 saved_page = (struct copan_aps_subpage *) 4378 (page_index->page_data + 4379 (page_index->page_len * 4380 CTL_PAGE_SAVED)); 4381 break; 4382 } 4383 case DBGCNF_SUBPAGE_CODE: { 4384 struct copan_debugconf_subpage *current_page, 4385 *saved_page; 4386 4387 memcpy(&lun->mode_pages.debugconf_subpage[ 4388 CTL_PAGE_CURRENT], 4389 &debugconf_page_default, 4390 sizeof(debugconf_page_default)); 4391 memcpy(&lun->mode_pages.debugconf_subpage[ 4392 CTL_PAGE_CHANGEABLE], 4393 &debugconf_page_changeable, 4394 sizeof(debugconf_page_changeable)); 4395 memcpy(&lun->mode_pages.debugconf_subpage[ 4396 CTL_PAGE_DEFAULT], 4397 &debugconf_page_default, 4398 sizeof(debugconf_page_default)); 4399 memcpy(&lun->mode_pages.debugconf_subpage[ 4400 CTL_PAGE_SAVED], 4401 &debugconf_page_default, 4402 sizeof(debugconf_page_default)); 4403 page_index->page_data = 4404 (uint8_t *)lun->mode_pages.debugconf_subpage; 4405 4406 current_page = (struct copan_debugconf_subpage *) 4407 (page_index->page_data + 4408 (page_index->page_len * 4409 CTL_PAGE_CURRENT)); 4410 saved_page = (struct copan_debugconf_subpage *) 4411 (page_index->page_data + 4412 (page_index->page_len * 4413 CTL_PAGE_SAVED)); 4414 break; 4415 } 4416 default: 4417 panic("invalid subpage value %d", 4418 page_index->subpage); 4419 break; 4420 } 4421 break; 4422 } 4423 default: 4424 panic("invalid page value %d", 4425 page_index->page_code & SMPH_PC_MASK); 4426 break; 4427 } 4428 } 4429 4430 return (CTL_RETVAL_COMPLETE); 4431} 4432 4433/* 4434 * LUN allocation. 4435 * 4436 * Requirements: 4437 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4438 * wants us to allocate the LUN and he can block. 4439 * - ctl_softc is always set 4440 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4441 * 4442 * Returns 0 for success, non-zero (errno) for failure. 4443 */ 4444static int 4445ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4446 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4447{ 4448 struct ctl_lun *nlun, *lun; 4449 struct ctl_port *port; 4450 struct scsi_vpd_id_descriptor *desc; 4451 struct scsi_vpd_id_t10 *t10id; 4452 const char *eui, *naa, *scsiname, *vendor; 4453 int lun_number, i, lun_malloced; 4454 int devidlen, idlen1, idlen2 = 0, len; 4455 4456 if (be_lun == NULL) 4457 return (EINVAL); 4458 4459 /* 4460 * We currently only support Direct Access or Processor LUN types. 4461 */ 4462 switch (be_lun->lun_type) { 4463 case T_DIRECT: 4464 break; 4465 case T_PROCESSOR: 4466 break; 4467 case T_SEQUENTIAL: 4468 case T_CHANGER: 4469 default: 4470 be_lun->lun_config_status(be_lun->be_lun, 4471 CTL_LUN_CONFIG_FAILURE); 4472 break; 4473 } 4474 if (ctl_lun == NULL) { 4475 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4476 lun_malloced = 1; 4477 } else { 4478 lun_malloced = 0; 4479 lun = ctl_lun; 4480 } 4481 4482 memset(lun, 0, sizeof(*lun)); 4483 if (lun_malloced) 4484 lun->flags = CTL_LUN_MALLOCED; 4485 4486 /* Generate LUN ID. */ 4487 devidlen = max(CTL_DEVID_MIN_LEN, 4488 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4489 idlen1 = sizeof(*t10id) + devidlen; 4490 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4491 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4492 if (scsiname != NULL) { 4493 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4494 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4495 } 4496 eui = ctl_get_opt(&be_lun->options, "eui"); 4497 if (eui != NULL) { 4498 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4499 } 4500 naa = ctl_get_opt(&be_lun->options, "naa"); 4501 if (naa != NULL) { 4502 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4503 } 4504 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4505 M_CTL, M_WAITOK | M_ZERO); 4506 lun->lun_devid->len = len; 4507 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4508 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4509 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4510 desc->length = idlen1; 4511 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4512 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4513 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4514 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4515 } else { 4516 strncpy(t10id->vendor, vendor, 4517 min(sizeof(t10id->vendor), strlen(vendor))); 4518 } 4519 strncpy((char *)t10id->vendor_spec_id, 4520 (char *)be_lun->device_id, devidlen); 4521 if (scsiname != NULL) { 4522 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4523 desc->length); 4524 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4525 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4526 SVPD_ID_TYPE_SCSI_NAME; 4527 desc->length = idlen2; 4528 strlcpy(desc->identifier, scsiname, idlen2); 4529 } 4530 if (eui != NULL) { 4531 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4532 desc->length); 4533 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4534 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4535 SVPD_ID_TYPE_EUI64; 4536 desc->length = 8; 4537 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4538 } 4539 if (naa != NULL) { 4540 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4541 desc->length); 4542 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4543 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4544 SVPD_ID_TYPE_NAA; 4545 desc->length = 8; 4546 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4547 } 4548 4549 mtx_lock(&ctl_softc->ctl_lock); 4550 /* 4551 * See if the caller requested a particular LUN number. If so, see 4552 * if it is available. Otherwise, allocate the first available LUN. 4553 */ 4554 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4555 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4556 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4557 mtx_unlock(&ctl_softc->ctl_lock); 4558 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4559 printf("ctl: requested LUN ID %d is higher " 4560 "than CTL_MAX_LUNS - 1 (%d)\n", 4561 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4562 } else { 4563 /* 4564 * XXX KDM return an error, or just assign 4565 * another LUN ID in this case?? 4566 */ 4567 printf("ctl: requested LUN ID %d is already " 4568 "in use\n", be_lun->req_lun_id); 4569 } 4570 if (lun->flags & CTL_LUN_MALLOCED) 4571 free(lun, M_CTL); 4572 be_lun->lun_config_status(be_lun->be_lun, 4573 CTL_LUN_CONFIG_FAILURE); 4574 return (ENOSPC); 4575 } 4576 lun_number = be_lun->req_lun_id; 4577 } else { 4578 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4579 if (lun_number == -1) { 4580 mtx_unlock(&ctl_softc->ctl_lock); 4581 printf("ctl: can't allocate LUN on target %ju, out of " 4582 "LUNs\n", (uintmax_t)target_id.id); 4583 if (lun->flags & CTL_LUN_MALLOCED) 4584 free(lun, M_CTL); 4585 be_lun->lun_config_status(be_lun->be_lun, 4586 CTL_LUN_CONFIG_FAILURE); 4587 return (ENOSPC); 4588 } 4589 } 4590 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4591 4592 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4593 lun->target = target_id; 4594 lun->lun = lun_number; 4595 lun->be_lun = be_lun; 4596 /* 4597 * The processor LUN is always enabled. Disk LUNs come on line 4598 * disabled, and must be enabled by the backend. 4599 */ 4600 lun->flags |= CTL_LUN_DISABLED; 4601 lun->backend = be_lun->be; 4602 be_lun->ctl_lun = lun; 4603 be_lun->lun_id = lun_number; 4604 atomic_add_int(&be_lun->be->num_luns, 1); 4605 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4606 lun->flags |= CTL_LUN_STOPPED; 4607 4608 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4609 lun->flags |= CTL_LUN_INOPERABLE; 4610 4611 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4612 lun->flags |= CTL_LUN_PRIMARY_SC; 4613 4614 lun->ctl_softc = ctl_softc; 4615 TAILQ_INIT(&lun->ooa_queue); 4616 TAILQ_INIT(&lun->blocked_queue); 4617 STAILQ_INIT(&lun->error_list); 4618 ctl_tpc_lun_init(lun); 4619 4620 /* 4621 * Initialize the mode page index. 4622 */ 4623 ctl_init_page_index(lun); 4624 4625 /* 4626 * Set the poweron UA for all initiators on this LUN only. 4627 */ 4628 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4629 lun->pending_ua[i] = CTL_UA_POWERON; 4630 4631 /* 4632 * Now, before we insert this lun on the lun list, set the lun 4633 * inventory changed UA for all other luns. 4634 */ 4635 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4636 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4637 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4638 } 4639 } 4640 4641 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4642 4643 ctl_softc->ctl_luns[lun_number] = lun; 4644 4645 ctl_softc->num_luns++; 4646 4647 /* Setup statistics gathering */ 4648 lun->stats.device_type = be_lun->lun_type; 4649 lun->stats.lun_number = lun_number; 4650 if (lun->stats.device_type == T_DIRECT) 4651 lun->stats.blocksize = be_lun->blocksize; 4652 else 4653 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4654 for (i = 0;i < CTL_MAX_PORTS;i++) 4655 lun->stats.ports[i].targ_port = i; 4656 4657 mtx_unlock(&ctl_softc->ctl_lock); 4658 4659 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4660 4661 /* 4662 * Run through each registered FETD and bring it online if it isn't 4663 * already. Enable the target ID if it hasn't been enabled, and 4664 * enable this particular LUN. 4665 */ 4666 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4667 int retval; 4668 4669 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4670 if (retval != 0) { 4671 printf("ctl_alloc_lun: FETD %s port %d returned error " 4672 "%d for lun_enable on target %ju lun %d\n", 4673 port->port_name, port->targ_port, retval, 4674 (uintmax_t)target_id.id, lun_number); 4675 } else 4676 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4677 } 4678 return (0); 4679} 4680 4681/* 4682 * Delete a LUN. 4683 * Assumptions: 4684 * - LUN has already been marked invalid and any pending I/O has been taken 4685 * care of. 4686 */ 4687static int 4688ctl_free_lun(struct ctl_lun *lun) 4689{ 4690 struct ctl_softc *softc; 4691#if 0 4692 struct ctl_port *port; 4693#endif 4694 struct ctl_lun *nlun; 4695 int i; 4696 4697 softc = lun->ctl_softc; 4698 4699 mtx_assert(&softc->ctl_lock, MA_OWNED); 4700 4701 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4702 4703 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4704 4705 softc->ctl_luns[lun->lun] = NULL; 4706 4707 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4708 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4709 4710 softc->num_luns--; 4711 4712 /* 4713 * XXX KDM this scheme only works for a single target/multiple LUN 4714 * setup. It needs to be revamped for a multiple target scheme. 4715 * 4716 * XXX KDM this results in port->lun_disable() getting called twice, 4717 * once when ctl_disable_lun() is called, and a second time here. 4718 * We really need to re-think the LUN disable semantics. There 4719 * should probably be several steps/levels to LUN removal: 4720 * - disable 4721 * - invalidate 4722 * - free 4723 * 4724 * Right now we only have a disable method when communicating to 4725 * the front end ports, at least for individual LUNs. 4726 */ 4727#if 0 4728 STAILQ_FOREACH(port, &softc->port_list, links) { 4729 int retval; 4730 4731 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4732 lun->lun); 4733 if (retval != 0) { 4734 printf("ctl_free_lun: FETD %s port %d returned error " 4735 "%d for lun_disable on target %ju lun %jd\n", 4736 port->port_name, port->targ_port, retval, 4737 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4738 } 4739 4740 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4741 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4742 4743 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4744 if (retval != 0) { 4745 printf("ctl_free_lun: FETD %s port %d " 4746 "returned error %d for targ_disable on " 4747 "target %ju\n", port->port_name, 4748 port->targ_port, retval, 4749 (uintmax_t)lun->target.id); 4750 } else 4751 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4752 4753 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4754 continue; 4755 4756#if 0 4757 port->port_offline(port->onoff_arg); 4758 port->status &= ~CTL_PORT_STATUS_ONLINE; 4759#endif 4760 } 4761 } 4762#endif 4763 4764 /* 4765 * Tell the backend to free resources, if this LUN has a backend. 4766 */ 4767 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4768 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4769 4770 ctl_tpc_lun_shutdown(lun); 4771 mtx_destroy(&lun->lun_lock); 4772 free(lun->lun_devid, M_CTL); 4773 if (lun->flags & CTL_LUN_MALLOCED) 4774 free(lun, M_CTL); 4775 4776 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4777 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4778 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4779 } 4780 } 4781 4782 return (0); 4783} 4784 4785static void 4786ctl_create_lun(struct ctl_be_lun *be_lun) 4787{ 4788 struct ctl_softc *ctl_softc; 4789 4790 ctl_softc = control_softc; 4791 4792 /* 4793 * ctl_alloc_lun() should handle all potential failure cases. 4794 */ 4795 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4796} 4797 4798int 4799ctl_add_lun(struct ctl_be_lun *be_lun) 4800{ 4801 struct ctl_softc *ctl_softc = control_softc; 4802 4803 mtx_lock(&ctl_softc->ctl_lock); 4804 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4805 mtx_unlock(&ctl_softc->ctl_lock); 4806 wakeup(&ctl_softc->pending_lun_queue); 4807 4808 return (0); 4809} 4810 4811int 4812ctl_enable_lun(struct ctl_be_lun *be_lun) 4813{ 4814 struct ctl_softc *ctl_softc; 4815 struct ctl_port *port, *nport; 4816 struct ctl_lun *lun; 4817 int retval; 4818 4819 ctl_softc = control_softc; 4820 4821 lun = (struct ctl_lun *)be_lun->ctl_lun; 4822 4823 mtx_lock(&ctl_softc->ctl_lock); 4824 mtx_lock(&lun->lun_lock); 4825 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4826 /* 4827 * eh? Why did we get called if the LUN is already 4828 * enabled? 4829 */ 4830 mtx_unlock(&lun->lun_lock); 4831 mtx_unlock(&ctl_softc->ctl_lock); 4832 return (0); 4833 } 4834 lun->flags &= ~CTL_LUN_DISABLED; 4835 mtx_unlock(&lun->lun_lock); 4836 4837 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4838 nport = STAILQ_NEXT(port, links); 4839 4840 /* 4841 * Drop the lock while we call the FETD's enable routine. 4842 * This can lead to a callback into CTL (at least in the 4843 * case of the internal initiator frontend. 4844 */ 4845 mtx_unlock(&ctl_softc->ctl_lock); 4846 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4847 mtx_lock(&ctl_softc->ctl_lock); 4848 if (retval != 0) { 4849 printf("%s: FETD %s port %d returned error " 4850 "%d for lun_enable on target %ju lun %jd\n", 4851 __func__, port->port_name, port->targ_port, retval, 4852 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4853 } 4854#if 0 4855 else { 4856 /* NOTE: TODO: why does lun enable affect port status? */ 4857 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4858 } 4859#endif 4860 } 4861 4862 mtx_unlock(&ctl_softc->ctl_lock); 4863 4864 return (0); 4865} 4866 4867int 4868ctl_disable_lun(struct ctl_be_lun *be_lun) 4869{ 4870 struct ctl_softc *ctl_softc; 4871 struct ctl_port *port; 4872 struct ctl_lun *lun; 4873 int retval; 4874 4875 ctl_softc = control_softc; 4876 4877 lun = (struct ctl_lun *)be_lun->ctl_lun; 4878 4879 mtx_lock(&ctl_softc->ctl_lock); 4880 mtx_lock(&lun->lun_lock); 4881 if (lun->flags & CTL_LUN_DISABLED) { 4882 mtx_unlock(&lun->lun_lock); 4883 mtx_unlock(&ctl_softc->ctl_lock); 4884 return (0); 4885 } 4886 lun->flags |= CTL_LUN_DISABLED; 4887 mtx_unlock(&lun->lun_lock); 4888 4889 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4890 mtx_unlock(&ctl_softc->ctl_lock); 4891 /* 4892 * Drop the lock before we call the frontend's disable 4893 * routine, to avoid lock order reversals. 4894 * 4895 * XXX KDM what happens if the frontend list changes while 4896 * we're traversing it? It's unlikely, but should be handled. 4897 */ 4898 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4899 lun->lun); 4900 mtx_lock(&ctl_softc->ctl_lock); 4901 if (retval != 0) { 4902 printf("ctl_alloc_lun: FETD %s port %d returned error " 4903 "%d for lun_disable on target %ju lun %jd\n", 4904 port->port_name, port->targ_port, retval, 4905 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4906 } 4907 } 4908 4909 mtx_unlock(&ctl_softc->ctl_lock); 4910 4911 return (0); 4912} 4913 4914int 4915ctl_start_lun(struct ctl_be_lun *be_lun) 4916{ 4917 struct ctl_softc *ctl_softc; 4918 struct ctl_lun *lun; 4919 4920 ctl_softc = control_softc; 4921 4922 lun = (struct ctl_lun *)be_lun->ctl_lun; 4923 4924 mtx_lock(&lun->lun_lock); 4925 lun->flags &= ~CTL_LUN_STOPPED; 4926 mtx_unlock(&lun->lun_lock); 4927 4928 return (0); 4929} 4930 4931int 4932ctl_stop_lun(struct ctl_be_lun *be_lun) 4933{ 4934 struct ctl_softc *ctl_softc; 4935 struct ctl_lun *lun; 4936 4937 ctl_softc = control_softc; 4938 4939 lun = (struct ctl_lun *)be_lun->ctl_lun; 4940 4941 mtx_lock(&lun->lun_lock); 4942 lun->flags |= CTL_LUN_STOPPED; 4943 mtx_unlock(&lun->lun_lock); 4944 4945 return (0); 4946} 4947 4948int 4949ctl_lun_offline(struct ctl_be_lun *be_lun) 4950{ 4951 struct ctl_softc *ctl_softc; 4952 struct ctl_lun *lun; 4953 4954 ctl_softc = control_softc; 4955 4956 lun = (struct ctl_lun *)be_lun->ctl_lun; 4957 4958 mtx_lock(&lun->lun_lock); 4959 lun->flags |= CTL_LUN_OFFLINE; 4960 mtx_unlock(&lun->lun_lock); 4961 4962 return (0); 4963} 4964 4965int 4966ctl_lun_online(struct ctl_be_lun *be_lun) 4967{ 4968 struct ctl_softc *ctl_softc; 4969 struct ctl_lun *lun; 4970 4971 ctl_softc = control_softc; 4972 4973 lun = (struct ctl_lun *)be_lun->ctl_lun; 4974 4975 mtx_lock(&lun->lun_lock); 4976 lun->flags &= ~CTL_LUN_OFFLINE; 4977 mtx_unlock(&lun->lun_lock); 4978 4979 return (0); 4980} 4981 4982int 4983ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4984{ 4985 struct ctl_softc *ctl_softc; 4986 struct ctl_lun *lun; 4987 4988 ctl_softc = control_softc; 4989 4990 lun = (struct ctl_lun *)be_lun->ctl_lun; 4991 4992 mtx_lock(&lun->lun_lock); 4993 4994 /* 4995 * The LUN needs to be disabled before it can be marked invalid. 4996 */ 4997 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4998 mtx_unlock(&lun->lun_lock); 4999 return (-1); 5000 } 5001 /* 5002 * Mark the LUN invalid. 5003 */ 5004 lun->flags |= CTL_LUN_INVALID; 5005 5006 /* 5007 * If there is nothing in the OOA queue, go ahead and free the LUN. 5008 * If we have something in the OOA queue, we'll free it when the 5009 * last I/O completes. 5010 */ 5011 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5012 mtx_unlock(&lun->lun_lock); 5013 mtx_lock(&ctl_softc->ctl_lock); 5014 ctl_free_lun(lun); 5015 mtx_unlock(&ctl_softc->ctl_lock); 5016 } else 5017 mtx_unlock(&lun->lun_lock); 5018 5019 return (0); 5020} 5021 5022int 5023ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5024{ 5025 struct ctl_softc *ctl_softc; 5026 struct ctl_lun *lun; 5027 5028 ctl_softc = control_softc; 5029 lun = (struct ctl_lun *)be_lun->ctl_lun; 5030 5031 mtx_lock(&lun->lun_lock); 5032 lun->flags |= CTL_LUN_INOPERABLE; 5033 mtx_unlock(&lun->lun_lock); 5034 5035 return (0); 5036} 5037 5038int 5039ctl_lun_operable(struct ctl_be_lun *be_lun) 5040{ 5041 struct ctl_softc *ctl_softc; 5042 struct ctl_lun *lun; 5043 5044 ctl_softc = control_softc; 5045 lun = (struct ctl_lun *)be_lun->ctl_lun; 5046 5047 mtx_lock(&lun->lun_lock); 5048 lun->flags &= ~CTL_LUN_INOPERABLE; 5049 mtx_unlock(&lun->lun_lock); 5050 5051 return (0); 5052} 5053 5054int 5055ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5056 int lock) 5057{ 5058 struct ctl_softc *softc; 5059 struct ctl_lun *lun; 5060 struct copan_aps_subpage *current_sp; 5061 struct ctl_page_index *page_index; 5062 int i; 5063 5064 softc = control_softc; 5065 5066 mtx_lock(&softc->ctl_lock); 5067 5068 lun = (struct ctl_lun *)be_lun->ctl_lun; 5069 mtx_lock(&lun->lun_lock); 5070 5071 page_index = NULL; 5072 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5073 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5074 APS_PAGE_CODE) 5075 continue; 5076 5077 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5078 continue; 5079 page_index = &lun->mode_pages.index[i]; 5080 } 5081 5082 if (page_index == NULL) { 5083 mtx_unlock(&lun->lun_lock); 5084 mtx_unlock(&softc->ctl_lock); 5085 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5086 (uintmax_t)lun->lun); 5087 return (1); 5088 } 5089#if 0 5090 if ((softc->aps_locked_lun != 0) 5091 && (softc->aps_locked_lun != lun->lun)) { 5092 printf("%s: attempt to lock LUN %llu when %llu is already " 5093 "locked\n"); 5094 mtx_unlock(&lun->lun_lock); 5095 mtx_unlock(&softc->ctl_lock); 5096 return (1); 5097 } 5098#endif 5099 5100 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5101 (page_index->page_len * CTL_PAGE_CURRENT)); 5102 5103 if (lock != 0) { 5104 current_sp->lock_active = APS_LOCK_ACTIVE; 5105 softc->aps_locked_lun = lun->lun; 5106 } else { 5107 current_sp->lock_active = 0; 5108 softc->aps_locked_lun = 0; 5109 } 5110 5111 5112 /* 5113 * If we're in HA mode, try to send the lock message to the other 5114 * side. 5115 */ 5116 if (ctl_is_single == 0) { 5117 int isc_retval; 5118 union ctl_ha_msg lock_msg; 5119 5120 lock_msg.hdr.nexus = *nexus; 5121 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5122 if (lock != 0) 5123 lock_msg.aps.lock_flag = 1; 5124 else 5125 lock_msg.aps.lock_flag = 0; 5126 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5127 sizeof(lock_msg), 0); 5128 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5129 printf("%s: APS (lock=%d) error returned from " 5130 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5131 mtx_unlock(&lun->lun_lock); 5132 mtx_unlock(&softc->ctl_lock); 5133 return (1); 5134 } 5135 } 5136 5137 mtx_unlock(&lun->lun_lock); 5138 mtx_unlock(&softc->ctl_lock); 5139 5140 return (0); 5141} 5142 5143void 5144ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5145{ 5146 struct ctl_lun *lun; 5147 struct ctl_softc *softc; 5148 int i; 5149 5150 softc = control_softc; 5151 5152 lun = (struct ctl_lun *)be_lun->ctl_lun; 5153 5154 mtx_lock(&lun->lun_lock); 5155 5156 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5157 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5158 5159 mtx_unlock(&lun->lun_lock); 5160} 5161 5162/* 5163 * Backend "memory move is complete" callback for requests that never 5164 * make it down to say RAIDCore's configuration code. 5165 */ 5166int 5167ctl_config_move_done(union ctl_io *io) 5168{ 5169 int retval; 5170 5171 retval = CTL_RETVAL_COMPLETE; 5172 5173 5174 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5175 /* 5176 * XXX KDM this shouldn't happen, but what if it does? 5177 */ 5178 if (io->io_hdr.io_type != CTL_IO_SCSI) 5179 panic("I/O type isn't CTL_IO_SCSI!"); 5180 5181 if ((io->io_hdr.port_status == 0) 5182 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5183 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5184 io->io_hdr.status = CTL_SUCCESS; 5185 else if ((io->io_hdr.port_status != 0) 5186 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5187 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5188 /* 5189 * For hardware error sense keys, the sense key 5190 * specific value is defined to be a retry count, 5191 * but we use it to pass back an internal FETD 5192 * error code. XXX KDM Hopefully the FETD is only 5193 * using 16 bits for an error code, since that's 5194 * all the space we have in the sks field. 5195 */ 5196 ctl_set_internal_failure(&io->scsiio, 5197 /*sks_valid*/ 1, 5198 /*retry_count*/ 5199 io->io_hdr.port_status); 5200 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5201 free(io->scsiio.kern_data_ptr, M_CTL); 5202 ctl_done(io); 5203 goto bailout; 5204 } 5205 5206 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5207 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5208 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5209 /* 5210 * XXX KDM just assuming a single pointer here, and not a 5211 * S/G list. If we start using S/G lists for config data, 5212 * we'll need to know how to clean them up here as well. 5213 */ 5214 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5215 free(io->scsiio.kern_data_ptr, M_CTL); 5216 /* Hopefully the user has already set the status... */ 5217 ctl_done(io); 5218 } else { 5219 /* 5220 * XXX KDM now we need to continue data movement. Some 5221 * options: 5222 * - call ctl_scsiio() again? We don't do this for data 5223 * writes, because for those at least we know ahead of 5224 * time where the write will go and how long it is. For 5225 * config writes, though, that information is largely 5226 * contained within the write itself, thus we need to 5227 * parse out the data again. 5228 * 5229 * - Call some other function once the data is in? 5230 */ 5231 5232 /* 5233 * XXX KDM call ctl_scsiio() again for now, and check flag 5234 * bits to see whether we're allocated or not. 5235 */ 5236 retval = ctl_scsiio(&io->scsiio); 5237 } 5238bailout: 5239 return (retval); 5240} 5241 5242/* 5243 * This gets called by a backend driver when it is done with a 5244 * data_submit method. 5245 */ 5246void 5247ctl_data_submit_done(union ctl_io *io) 5248{ 5249 /* 5250 * If the IO_CONT flag is set, we need to call the supplied 5251 * function to continue processing the I/O, instead of completing 5252 * the I/O just yet. 5253 * 5254 * If there is an error, though, we don't want to keep processing. 5255 * Instead, just send status back to the initiator. 5256 */ 5257 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5258 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5259 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5260 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5261 io->scsiio.io_cont(io); 5262 return; 5263 } 5264 ctl_done(io); 5265} 5266 5267/* 5268 * This gets called by a backend driver when it is done with a 5269 * configuration write. 5270 */ 5271void 5272ctl_config_write_done(union ctl_io *io) 5273{ 5274 uint8_t *buf; 5275 5276 /* 5277 * If the IO_CONT flag is set, we need to call the supplied 5278 * function to continue processing the I/O, instead of completing 5279 * the I/O just yet. 5280 * 5281 * If there is an error, though, we don't want to keep processing. 5282 * Instead, just send status back to the initiator. 5283 */ 5284 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5285 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5286 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5287 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5288 io->scsiio.io_cont(io); 5289 return; 5290 } 5291 /* 5292 * Since a configuration write can be done for commands that actually 5293 * have data allocated, like write buffer, and commands that have 5294 * no data, like start/stop unit, we need to check here. 5295 */ 5296 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5297 buf = io->scsiio.kern_data_ptr; 5298 else 5299 buf = NULL; 5300 ctl_done(io); 5301 if (buf) 5302 free(buf, M_CTL); 5303} 5304 5305/* 5306 * SCSI release command. 5307 */ 5308int 5309ctl_scsi_release(struct ctl_scsiio *ctsio) 5310{ 5311 int length, longid, thirdparty_id, resv_id; 5312 struct ctl_softc *ctl_softc; 5313 struct ctl_lun *lun; 5314 uint32_t residx; 5315 5316 length = 0; 5317 resv_id = 0; 5318 5319 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5320 5321 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5322 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5323 ctl_softc = control_softc; 5324 5325 switch (ctsio->cdb[0]) { 5326 case RELEASE_10: { 5327 struct scsi_release_10 *cdb; 5328 5329 cdb = (struct scsi_release_10 *)ctsio->cdb; 5330 5331 if (cdb->byte2 & SR10_LONGID) 5332 longid = 1; 5333 else 5334 thirdparty_id = cdb->thirdparty_id; 5335 5336 resv_id = cdb->resv_id; 5337 length = scsi_2btoul(cdb->length); 5338 break; 5339 } 5340 } 5341 5342 5343 /* 5344 * XXX KDM right now, we only support LUN reservation. We don't 5345 * support 3rd party reservations, or extent reservations, which 5346 * might actually need the parameter list. If we've gotten this 5347 * far, we've got a LUN reservation. Anything else got kicked out 5348 * above. So, according to SPC, ignore the length. 5349 */ 5350 length = 0; 5351 5352 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5353 && (length > 0)) { 5354 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5355 ctsio->kern_data_len = length; 5356 ctsio->kern_total_len = length; 5357 ctsio->kern_data_resid = 0; 5358 ctsio->kern_rel_offset = 0; 5359 ctsio->kern_sg_entries = 0; 5360 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5361 ctsio->be_move_done = ctl_config_move_done; 5362 ctl_datamove((union ctl_io *)ctsio); 5363 5364 return (CTL_RETVAL_COMPLETE); 5365 } 5366 5367 if (length > 0) 5368 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5369 5370 mtx_lock(&lun->lun_lock); 5371 5372 /* 5373 * According to SPC, it is not an error for an intiator to attempt 5374 * to release a reservation on a LUN that isn't reserved, or that 5375 * is reserved by another initiator. The reservation can only be 5376 * released, though, by the initiator who made it or by one of 5377 * several reset type events. 5378 */ 5379 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5380 lun->flags &= ~CTL_LUN_RESERVED; 5381 5382 mtx_unlock(&lun->lun_lock); 5383 5384 ctsio->scsi_status = SCSI_STATUS_OK; 5385 ctsio->io_hdr.status = CTL_SUCCESS; 5386 5387 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5388 free(ctsio->kern_data_ptr, M_CTL); 5389 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5390 } 5391 5392 ctl_done((union ctl_io *)ctsio); 5393 return (CTL_RETVAL_COMPLETE); 5394} 5395 5396int 5397ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5398{ 5399 int extent, thirdparty, longid; 5400 int resv_id, length; 5401 uint64_t thirdparty_id; 5402 struct ctl_softc *ctl_softc; 5403 struct ctl_lun *lun; 5404 uint32_t residx; 5405 5406 extent = 0; 5407 thirdparty = 0; 5408 longid = 0; 5409 resv_id = 0; 5410 length = 0; 5411 thirdparty_id = 0; 5412 5413 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5414 5415 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5416 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5417 ctl_softc = control_softc; 5418 5419 switch (ctsio->cdb[0]) { 5420 case RESERVE_10: { 5421 struct scsi_reserve_10 *cdb; 5422 5423 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5424 5425 if (cdb->byte2 & SR10_LONGID) 5426 longid = 1; 5427 else 5428 thirdparty_id = cdb->thirdparty_id; 5429 5430 resv_id = cdb->resv_id; 5431 length = scsi_2btoul(cdb->length); 5432 break; 5433 } 5434 } 5435 5436 /* 5437 * XXX KDM right now, we only support LUN reservation. We don't 5438 * support 3rd party reservations, or extent reservations, which 5439 * might actually need the parameter list. If we've gotten this 5440 * far, we've got a LUN reservation. Anything else got kicked out 5441 * above. So, according to SPC, ignore the length. 5442 */ 5443 length = 0; 5444 5445 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5446 && (length > 0)) { 5447 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5448 ctsio->kern_data_len = length; 5449 ctsio->kern_total_len = length; 5450 ctsio->kern_data_resid = 0; 5451 ctsio->kern_rel_offset = 0; 5452 ctsio->kern_sg_entries = 0; 5453 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5454 ctsio->be_move_done = ctl_config_move_done; 5455 ctl_datamove((union ctl_io *)ctsio); 5456 5457 return (CTL_RETVAL_COMPLETE); 5458 } 5459 5460 if (length > 0) 5461 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5462 5463 mtx_lock(&lun->lun_lock); 5464 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5465 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5466 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5467 goto bailout; 5468 } 5469 5470 lun->flags |= CTL_LUN_RESERVED; 5471 lun->res_idx = residx; 5472 5473 ctsio->scsi_status = SCSI_STATUS_OK; 5474 ctsio->io_hdr.status = CTL_SUCCESS; 5475 5476bailout: 5477 mtx_unlock(&lun->lun_lock); 5478 5479 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5480 free(ctsio->kern_data_ptr, M_CTL); 5481 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5482 } 5483 5484 ctl_done((union ctl_io *)ctsio); 5485 return (CTL_RETVAL_COMPLETE); 5486} 5487 5488int 5489ctl_start_stop(struct ctl_scsiio *ctsio) 5490{ 5491 struct scsi_start_stop_unit *cdb; 5492 struct ctl_lun *lun; 5493 struct ctl_softc *ctl_softc; 5494 int retval; 5495 5496 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5497 5498 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5499 ctl_softc = control_softc; 5500 retval = 0; 5501 5502 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5503 5504 /* 5505 * XXX KDM 5506 * We don't support the immediate bit on a stop unit. In order to 5507 * do that, we would need to code up a way to know that a stop is 5508 * pending, and hold off any new commands until it completes, one 5509 * way or another. Then we could accept or reject those commands 5510 * depending on its status. We would almost need to do the reverse 5511 * of what we do below for an immediate start -- return the copy of 5512 * the ctl_io to the FETD with status to send to the host (and to 5513 * free the copy!) and then free the original I/O once the stop 5514 * actually completes. That way, the OOA queue mechanism can work 5515 * to block commands that shouldn't proceed. Another alternative 5516 * would be to put the copy in the queue in place of the original, 5517 * and return the original back to the caller. That could be 5518 * slightly safer.. 5519 */ 5520 if ((cdb->byte2 & SSS_IMMED) 5521 && ((cdb->how & SSS_START) == 0)) { 5522 ctl_set_invalid_field(ctsio, 5523 /*sks_valid*/ 1, 5524 /*command*/ 1, 5525 /*field*/ 1, 5526 /*bit_valid*/ 1, 5527 /*bit*/ 0); 5528 ctl_done((union ctl_io *)ctsio); 5529 return (CTL_RETVAL_COMPLETE); 5530 } 5531 5532 if ((lun->flags & CTL_LUN_PR_RESERVED) 5533 && ((cdb->how & SSS_START)==0)) { 5534 uint32_t residx; 5535 5536 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5537 if (!lun->per_res[residx].registered 5538 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5539 5540 ctl_set_reservation_conflict(ctsio); 5541 ctl_done((union ctl_io *)ctsio); 5542 return (CTL_RETVAL_COMPLETE); 5543 } 5544 } 5545 5546 /* 5547 * If there is no backend on this device, we can't start or stop 5548 * it. In theory we shouldn't get any start/stop commands in the 5549 * first place at this level if the LUN doesn't have a backend. 5550 * That should get stopped by the command decode code. 5551 */ 5552 if (lun->backend == NULL) { 5553 ctl_set_invalid_opcode(ctsio); 5554 ctl_done((union ctl_io *)ctsio); 5555 return (CTL_RETVAL_COMPLETE); 5556 } 5557 5558 /* 5559 * XXX KDM Copan-specific offline behavior. 5560 * Figure out a reasonable way to port this? 5561 */ 5562#ifdef NEEDTOPORT 5563 mtx_lock(&lun->lun_lock); 5564 5565 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5566 && (lun->flags & CTL_LUN_OFFLINE)) { 5567 /* 5568 * If the LUN is offline, and the on/offline bit isn't set, 5569 * reject the start or stop. Otherwise, let it through. 5570 */ 5571 mtx_unlock(&lun->lun_lock); 5572 ctl_set_lun_not_ready(ctsio); 5573 ctl_done((union ctl_io *)ctsio); 5574 } else { 5575 mtx_unlock(&lun->lun_lock); 5576#endif /* NEEDTOPORT */ 5577 /* 5578 * This could be a start or a stop when we're online, 5579 * or a stop/offline or start/online. A start or stop when 5580 * we're offline is covered in the case above. 5581 */ 5582 /* 5583 * In the non-immediate case, we send the request to 5584 * the backend and return status to the user when 5585 * it is done. 5586 * 5587 * In the immediate case, we allocate a new ctl_io 5588 * to hold a copy of the request, and send that to 5589 * the backend. We then set good status on the 5590 * user's request and return it immediately. 5591 */ 5592 if (cdb->byte2 & SSS_IMMED) { 5593 union ctl_io *new_io; 5594 5595 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5596 if (new_io == NULL) { 5597 ctl_set_busy(ctsio); 5598 ctl_done((union ctl_io *)ctsio); 5599 } else { 5600 ctl_copy_io((union ctl_io *)ctsio, 5601 new_io); 5602 retval = lun->backend->config_write(new_io); 5603 ctl_set_success(ctsio); 5604 ctl_done((union ctl_io *)ctsio); 5605 } 5606 } else { 5607 retval = lun->backend->config_write( 5608 (union ctl_io *)ctsio); 5609 } 5610#ifdef NEEDTOPORT 5611 } 5612#endif 5613 return (retval); 5614} 5615 5616/* 5617 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5618 * we don't really do anything with the LBA and length fields if the user 5619 * passes them in. Instead we'll just flush out the cache for the entire 5620 * LUN. 5621 */ 5622int 5623ctl_sync_cache(struct ctl_scsiio *ctsio) 5624{ 5625 struct ctl_lun *lun; 5626 struct ctl_softc *ctl_softc; 5627 uint64_t starting_lba; 5628 uint32_t block_count; 5629 int retval; 5630 5631 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5632 5633 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5634 ctl_softc = control_softc; 5635 retval = 0; 5636 5637 switch (ctsio->cdb[0]) { 5638 case SYNCHRONIZE_CACHE: { 5639 struct scsi_sync_cache *cdb; 5640 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5641 5642 starting_lba = scsi_4btoul(cdb->begin_lba); 5643 block_count = scsi_2btoul(cdb->lb_count); 5644 break; 5645 } 5646 case SYNCHRONIZE_CACHE_16: { 5647 struct scsi_sync_cache_16 *cdb; 5648 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5649 5650 starting_lba = scsi_8btou64(cdb->begin_lba); 5651 block_count = scsi_4btoul(cdb->lb_count); 5652 break; 5653 } 5654 default: 5655 ctl_set_invalid_opcode(ctsio); 5656 ctl_done((union ctl_io *)ctsio); 5657 goto bailout; 5658 break; /* NOTREACHED */ 5659 } 5660 5661 /* 5662 * We check the LBA and length, but don't do anything with them. 5663 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5664 * get flushed. This check will just help satisfy anyone who wants 5665 * to see an error for an out of range LBA. 5666 */ 5667 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5668 ctl_set_lba_out_of_range(ctsio); 5669 ctl_done((union ctl_io *)ctsio); 5670 goto bailout; 5671 } 5672 5673 /* 5674 * If this LUN has no backend, we can't flush the cache anyway. 5675 */ 5676 if (lun->backend == NULL) { 5677 ctl_set_invalid_opcode(ctsio); 5678 ctl_done((union ctl_io *)ctsio); 5679 goto bailout; 5680 } 5681 5682 /* 5683 * Check to see whether we're configured to send the SYNCHRONIZE 5684 * CACHE command directly to the back end. 5685 */ 5686 mtx_lock(&lun->lun_lock); 5687 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5688 && (++(lun->sync_count) >= lun->sync_interval)) { 5689 lun->sync_count = 0; 5690 mtx_unlock(&lun->lun_lock); 5691 retval = lun->backend->config_write((union ctl_io *)ctsio); 5692 } else { 5693 mtx_unlock(&lun->lun_lock); 5694 ctl_set_success(ctsio); 5695 ctl_done((union ctl_io *)ctsio); 5696 } 5697 5698bailout: 5699 5700 return (retval); 5701} 5702 5703int 5704ctl_format(struct ctl_scsiio *ctsio) 5705{ 5706 struct scsi_format *cdb; 5707 struct ctl_lun *lun; 5708 struct ctl_softc *ctl_softc; 5709 int length, defect_list_len; 5710 5711 CTL_DEBUG_PRINT(("ctl_format\n")); 5712 5713 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5714 ctl_softc = control_softc; 5715 5716 cdb = (struct scsi_format *)ctsio->cdb; 5717 5718 length = 0; 5719 if (cdb->byte2 & SF_FMTDATA) { 5720 if (cdb->byte2 & SF_LONGLIST) 5721 length = sizeof(struct scsi_format_header_long); 5722 else 5723 length = sizeof(struct scsi_format_header_short); 5724 } 5725 5726 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5727 && (length > 0)) { 5728 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5729 ctsio->kern_data_len = length; 5730 ctsio->kern_total_len = length; 5731 ctsio->kern_data_resid = 0; 5732 ctsio->kern_rel_offset = 0; 5733 ctsio->kern_sg_entries = 0; 5734 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5735 ctsio->be_move_done = ctl_config_move_done; 5736 ctl_datamove((union ctl_io *)ctsio); 5737 5738 return (CTL_RETVAL_COMPLETE); 5739 } 5740 5741 defect_list_len = 0; 5742 5743 if (cdb->byte2 & SF_FMTDATA) { 5744 if (cdb->byte2 & SF_LONGLIST) { 5745 struct scsi_format_header_long *header; 5746 5747 header = (struct scsi_format_header_long *) 5748 ctsio->kern_data_ptr; 5749 5750 defect_list_len = scsi_4btoul(header->defect_list_len); 5751 if (defect_list_len != 0) { 5752 ctl_set_invalid_field(ctsio, 5753 /*sks_valid*/ 1, 5754 /*command*/ 0, 5755 /*field*/ 2, 5756 /*bit_valid*/ 0, 5757 /*bit*/ 0); 5758 goto bailout; 5759 } 5760 } else { 5761 struct scsi_format_header_short *header; 5762 5763 header = (struct scsi_format_header_short *) 5764 ctsio->kern_data_ptr; 5765 5766 defect_list_len = scsi_2btoul(header->defect_list_len); 5767 if (defect_list_len != 0) { 5768 ctl_set_invalid_field(ctsio, 5769 /*sks_valid*/ 1, 5770 /*command*/ 0, 5771 /*field*/ 2, 5772 /*bit_valid*/ 0, 5773 /*bit*/ 0); 5774 goto bailout; 5775 } 5776 } 5777 } 5778 5779 /* 5780 * The format command will clear out the "Medium format corrupted" 5781 * status if set by the configuration code. That status is really 5782 * just a way to notify the host that we have lost the media, and 5783 * get them to issue a command that will basically make them think 5784 * they're blowing away the media. 5785 */ 5786 mtx_lock(&lun->lun_lock); 5787 lun->flags &= ~CTL_LUN_INOPERABLE; 5788 mtx_unlock(&lun->lun_lock); 5789 5790 ctsio->scsi_status = SCSI_STATUS_OK; 5791 ctsio->io_hdr.status = CTL_SUCCESS; 5792bailout: 5793 5794 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5795 free(ctsio->kern_data_ptr, M_CTL); 5796 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5797 } 5798 5799 ctl_done((union ctl_io *)ctsio); 5800 return (CTL_RETVAL_COMPLETE); 5801} 5802 5803int 5804ctl_read_buffer(struct ctl_scsiio *ctsio) 5805{ 5806 struct scsi_read_buffer *cdb; 5807 struct ctl_lun *lun; 5808 int buffer_offset, len; 5809 static uint8_t descr[4]; 5810 static uint8_t echo_descr[4] = { 0 }; 5811 5812 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5813 5814 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5815 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5816 5817 if (lun->flags & CTL_LUN_PR_RESERVED) { 5818 uint32_t residx; 5819 5820 /* 5821 * XXX KDM need a lock here. 5822 */ 5823 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5824 if ((lun->res_type == SPR_TYPE_EX_AC 5825 && residx != lun->pr_res_idx) 5826 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5827 || lun->res_type == SPR_TYPE_EX_AC_AR) 5828 && !lun->per_res[residx].registered)) { 5829 ctl_set_reservation_conflict(ctsio); 5830 ctl_done((union ctl_io *)ctsio); 5831 return (CTL_RETVAL_COMPLETE); 5832 } 5833 } 5834 5835 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5836 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5837 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5838 ctl_set_invalid_field(ctsio, 5839 /*sks_valid*/ 1, 5840 /*command*/ 1, 5841 /*field*/ 1, 5842 /*bit_valid*/ 1, 5843 /*bit*/ 4); 5844 ctl_done((union ctl_io *)ctsio); 5845 return (CTL_RETVAL_COMPLETE); 5846 } 5847 5848 len = scsi_3btoul(cdb->length); 5849 buffer_offset = scsi_3btoul(cdb->offset); 5850 5851 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5852 ctl_set_invalid_field(ctsio, 5853 /*sks_valid*/ 1, 5854 /*command*/ 1, 5855 /*field*/ 6, 5856 /*bit_valid*/ 0, 5857 /*bit*/ 0); 5858 ctl_done((union ctl_io *)ctsio); 5859 return (CTL_RETVAL_COMPLETE); 5860 } 5861 5862 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5863 descr[0] = 0; 5864 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5865 ctsio->kern_data_ptr = descr; 5866 len = min(len, sizeof(descr)); 5867 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5868 ctsio->kern_data_ptr = echo_descr; 5869 len = min(len, sizeof(echo_descr)); 5870 } else 5871 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5872 ctsio->kern_data_len = len; 5873 ctsio->kern_total_len = len; 5874 ctsio->kern_data_resid = 0; 5875 ctsio->kern_rel_offset = 0; 5876 ctsio->kern_sg_entries = 0; 5877 ctsio->be_move_done = ctl_config_move_done; 5878 ctl_datamove((union ctl_io *)ctsio); 5879 5880 return (CTL_RETVAL_COMPLETE); 5881} 5882 5883int 5884ctl_write_buffer(struct ctl_scsiio *ctsio) 5885{ 5886 struct scsi_write_buffer *cdb; 5887 struct ctl_lun *lun; 5888 int buffer_offset, len; 5889 5890 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5891 5892 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5893 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5894 5895 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5896 ctl_set_invalid_field(ctsio, 5897 /*sks_valid*/ 1, 5898 /*command*/ 1, 5899 /*field*/ 1, 5900 /*bit_valid*/ 1, 5901 /*bit*/ 4); 5902 ctl_done((union ctl_io *)ctsio); 5903 return (CTL_RETVAL_COMPLETE); 5904 } 5905 5906 len = scsi_3btoul(cdb->length); 5907 buffer_offset = scsi_3btoul(cdb->offset); 5908 5909 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5910 ctl_set_invalid_field(ctsio, 5911 /*sks_valid*/ 1, 5912 /*command*/ 1, 5913 /*field*/ 6, 5914 /*bit_valid*/ 0, 5915 /*bit*/ 0); 5916 ctl_done((union ctl_io *)ctsio); 5917 return (CTL_RETVAL_COMPLETE); 5918 } 5919 5920 /* 5921 * If we've got a kernel request that hasn't been malloced yet, 5922 * malloc it and tell the caller the data buffer is here. 5923 */ 5924 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5925 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5926 ctsio->kern_data_len = len; 5927 ctsio->kern_total_len = len; 5928 ctsio->kern_data_resid = 0; 5929 ctsio->kern_rel_offset = 0; 5930 ctsio->kern_sg_entries = 0; 5931 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5932 ctsio->be_move_done = ctl_config_move_done; 5933 ctl_datamove((union ctl_io *)ctsio); 5934 5935 return (CTL_RETVAL_COMPLETE); 5936 } 5937 5938 ctl_done((union ctl_io *)ctsio); 5939 5940 return (CTL_RETVAL_COMPLETE); 5941} 5942 5943int 5944ctl_write_same(struct ctl_scsiio *ctsio) 5945{ 5946 struct ctl_lun *lun; 5947 struct ctl_lba_len_flags *lbalen; 5948 uint64_t lba; 5949 uint32_t num_blocks; 5950 int len, retval; 5951 uint8_t byte2; 5952 5953 retval = CTL_RETVAL_COMPLETE; 5954 5955 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5956 5957 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5958 5959 switch (ctsio->cdb[0]) { 5960 case WRITE_SAME_10: { 5961 struct scsi_write_same_10 *cdb; 5962 5963 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5964 5965 lba = scsi_4btoul(cdb->addr); 5966 num_blocks = scsi_2btoul(cdb->length); 5967 byte2 = cdb->byte2; 5968 break; 5969 } 5970 case WRITE_SAME_16: { 5971 struct scsi_write_same_16 *cdb; 5972 5973 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5974 5975 lba = scsi_8btou64(cdb->addr); 5976 num_blocks = scsi_4btoul(cdb->length); 5977 byte2 = cdb->byte2; 5978 break; 5979 } 5980 default: 5981 /* 5982 * We got a command we don't support. This shouldn't 5983 * happen, commands should be filtered out above us. 5984 */ 5985 ctl_set_invalid_opcode(ctsio); 5986 ctl_done((union ctl_io *)ctsio); 5987 5988 return (CTL_RETVAL_COMPLETE); 5989 break; /* NOTREACHED */ 5990 } 5991 5992 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5993 if ((byte2 & SWS_UNMAP) == 0 && 5994 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5995 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5996 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5997 ctl_done((union ctl_io *)ctsio); 5998 return (CTL_RETVAL_COMPLETE); 5999 } 6000 6001 /* 6002 * The first check is to make sure we're in bounds, the second 6003 * check is to catch wrap-around problems. If the lba + num blocks 6004 * is less than the lba, then we've wrapped around and the block 6005 * range is invalid anyway. 6006 */ 6007 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6008 || ((lba + num_blocks) < lba)) { 6009 ctl_set_lba_out_of_range(ctsio); 6010 ctl_done((union ctl_io *)ctsio); 6011 return (CTL_RETVAL_COMPLETE); 6012 } 6013 6014 /* Zero number of blocks means "to the last logical block" */ 6015 if (num_blocks == 0) { 6016 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 6017 ctl_set_invalid_field(ctsio, 6018 /*sks_valid*/ 0, 6019 /*command*/ 1, 6020 /*field*/ 0, 6021 /*bit_valid*/ 0, 6022 /*bit*/ 0); 6023 ctl_done((union ctl_io *)ctsio); 6024 return (CTL_RETVAL_COMPLETE); 6025 } 6026 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6027 } 6028 6029 len = lun->be_lun->blocksize; 6030 6031 /* 6032 * If we've got a kernel request that hasn't been malloced yet, 6033 * malloc it and tell the caller the data buffer is here. 6034 */ 6035 if ((byte2 & SWS_NDOB) == 0 && 6036 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6037 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6038 ctsio->kern_data_len = len; 6039 ctsio->kern_total_len = len; 6040 ctsio->kern_data_resid = 0; 6041 ctsio->kern_rel_offset = 0; 6042 ctsio->kern_sg_entries = 0; 6043 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6044 ctsio->be_move_done = ctl_config_move_done; 6045 ctl_datamove((union ctl_io *)ctsio); 6046 6047 return (CTL_RETVAL_COMPLETE); 6048 } 6049 6050 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6051 lbalen->lba = lba; 6052 lbalen->len = num_blocks; 6053 lbalen->flags = byte2; 6054 retval = lun->backend->config_write((union ctl_io *)ctsio); 6055 6056 return (retval); 6057} 6058 6059int 6060ctl_unmap(struct ctl_scsiio *ctsio) 6061{ 6062 struct ctl_lun *lun; 6063 struct scsi_unmap *cdb; 6064 struct ctl_ptr_len_flags *ptrlen; 6065 struct scsi_unmap_header *hdr; 6066 struct scsi_unmap_desc *buf, *end, *endnz, *range; 6067 uint64_t lba; 6068 uint32_t num_blocks; 6069 int len, retval; 6070 uint8_t byte2; 6071 6072 retval = CTL_RETVAL_COMPLETE; 6073 6074 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6075 6076 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6077 cdb = (struct scsi_unmap *)ctsio->cdb; 6078 6079 len = scsi_2btoul(cdb->length); 6080 byte2 = cdb->byte2; 6081 6082 /* 6083 * If we've got a kernel request that hasn't been malloced yet, 6084 * malloc it and tell the caller the data buffer is here. 6085 */ 6086 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6087 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6088 ctsio->kern_data_len = len; 6089 ctsio->kern_total_len = len; 6090 ctsio->kern_data_resid = 0; 6091 ctsio->kern_rel_offset = 0; 6092 ctsio->kern_sg_entries = 0; 6093 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6094 ctsio->be_move_done = ctl_config_move_done; 6095 ctl_datamove((union ctl_io *)ctsio); 6096 6097 return (CTL_RETVAL_COMPLETE); 6098 } 6099 6100 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6101 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6102 if (len < sizeof (*hdr) || 6103 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6104 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6105 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6106 ctl_set_invalid_field(ctsio, 6107 /*sks_valid*/ 0, 6108 /*command*/ 0, 6109 /*field*/ 0, 6110 /*bit_valid*/ 0, 6111 /*bit*/ 0); 6112 ctl_done((union ctl_io *)ctsio); 6113 return (CTL_RETVAL_COMPLETE); 6114 } 6115 len = scsi_2btoul(hdr->desc_length); 6116 buf = (struct scsi_unmap_desc *)(hdr + 1); 6117 end = buf + len / sizeof(*buf); 6118 6119 endnz = buf; 6120 for (range = buf; range < end; range++) { 6121 lba = scsi_8btou64(range->lba); 6122 num_blocks = scsi_4btoul(range->length); 6123 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6124 || ((lba + num_blocks) < lba)) { 6125 ctl_set_lba_out_of_range(ctsio); 6126 ctl_done((union ctl_io *)ctsio); 6127 return (CTL_RETVAL_COMPLETE); 6128 } 6129 if (num_blocks != 0) 6130 endnz = range + 1; 6131 } 6132 6133 /* 6134 * Block backend can not handle zero last range. 6135 * Filter it out and return if there is nothing left. 6136 */ 6137 len = (uint8_t *)endnz - (uint8_t *)buf; 6138 if (len == 0) { 6139 ctl_set_success(ctsio); 6140 ctl_done((union ctl_io *)ctsio); 6141 return (CTL_RETVAL_COMPLETE); 6142 } 6143 6144 mtx_lock(&lun->lun_lock); 6145 ptrlen = (struct ctl_ptr_len_flags *) 6146 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6147 ptrlen->ptr = (void *)buf; 6148 ptrlen->len = len; 6149 ptrlen->flags = byte2; 6150 ctl_check_blocked(lun); 6151 mtx_unlock(&lun->lun_lock); 6152 6153 retval = lun->backend->config_write((union ctl_io *)ctsio); 6154 return (retval); 6155} 6156 6157/* 6158 * Note that this function currently doesn't actually do anything inside 6159 * CTL to enforce things if the DQue bit is turned on. 6160 * 6161 * Also note that this function can't be used in the default case, because 6162 * the DQue bit isn't set in the changeable mask for the control mode page 6163 * anyway. This is just here as an example for how to implement a page 6164 * handler, and a placeholder in case we want to allow the user to turn 6165 * tagged queueing on and off. 6166 * 6167 * The D_SENSE bit handling is functional, however, and will turn 6168 * descriptor sense on and off for a given LUN. 6169 */ 6170int 6171ctl_control_page_handler(struct ctl_scsiio *ctsio, 6172 struct ctl_page_index *page_index, uint8_t *page_ptr) 6173{ 6174 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6175 struct ctl_lun *lun; 6176 struct ctl_softc *softc; 6177 int set_ua; 6178 uint32_t initidx; 6179 6180 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6181 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6182 set_ua = 0; 6183 6184 user_cp = (struct scsi_control_page *)page_ptr; 6185 current_cp = (struct scsi_control_page *) 6186 (page_index->page_data + (page_index->page_len * 6187 CTL_PAGE_CURRENT)); 6188 saved_cp = (struct scsi_control_page *) 6189 (page_index->page_data + (page_index->page_len * 6190 CTL_PAGE_SAVED)); 6191 6192 softc = control_softc; 6193 6194 mtx_lock(&lun->lun_lock); 6195 if (((current_cp->rlec & SCP_DSENSE) == 0) 6196 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6197 /* 6198 * Descriptor sense is currently turned off and the user 6199 * wants to turn it on. 6200 */ 6201 current_cp->rlec |= SCP_DSENSE; 6202 saved_cp->rlec |= SCP_DSENSE; 6203 lun->flags |= CTL_LUN_SENSE_DESC; 6204 set_ua = 1; 6205 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6206 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6207 /* 6208 * Descriptor sense is currently turned on, and the user 6209 * wants to turn it off. 6210 */ 6211 current_cp->rlec &= ~SCP_DSENSE; 6212 saved_cp->rlec &= ~SCP_DSENSE; 6213 lun->flags &= ~CTL_LUN_SENSE_DESC; 6214 set_ua = 1; 6215 } 6216 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6217 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6218 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6219 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6220 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6221 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6222 set_ua = 1; 6223 } 6224 if (set_ua != 0) { 6225 int i; 6226 /* 6227 * Let other initiators know that the mode 6228 * parameters for this LUN have changed. 6229 */ 6230 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6231 if (i == initidx) 6232 continue; 6233 6234 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6235 } 6236 } 6237 mtx_unlock(&lun->lun_lock); 6238 6239 return (0); 6240} 6241 6242int 6243ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6244 struct ctl_page_index *page_index, uint8_t *page_ptr) 6245{ 6246 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6247 struct ctl_lun *lun; 6248 int set_ua; 6249 uint32_t initidx; 6250 6251 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6252 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6253 set_ua = 0; 6254 6255 user_cp = (struct scsi_caching_page *)page_ptr; 6256 current_cp = (struct scsi_caching_page *) 6257 (page_index->page_data + (page_index->page_len * 6258 CTL_PAGE_CURRENT)); 6259 saved_cp = (struct scsi_caching_page *) 6260 (page_index->page_data + (page_index->page_len * 6261 CTL_PAGE_SAVED)); 6262 6263 mtx_lock(&lun->lun_lock); 6264 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6265 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6266 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6267 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6268 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6269 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6270 set_ua = 1; 6271 } 6272 if (set_ua != 0) { 6273 int i; 6274 /* 6275 * Let other initiators know that the mode 6276 * parameters for this LUN have changed. 6277 */ 6278 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6279 if (i == initidx) 6280 continue; 6281 6282 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6283 } 6284 } 6285 mtx_unlock(&lun->lun_lock); 6286 6287 return (0); 6288} 6289 6290int 6291ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6292 struct ctl_page_index *page_index, uint8_t *page_ptr) 6293{ 6294 return (0); 6295} 6296 6297int 6298ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6299 struct ctl_page_index *page_index, int pc) 6300{ 6301 struct copan_power_subpage *page; 6302 6303 page = (struct copan_power_subpage *)page_index->page_data + 6304 (page_index->page_len * pc); 6305 6306 switch (pc) { 6307 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6308 /* 6309 * We don't update the changable bits for this page. 6310 */ 6311 break; 6312 case SMS_PAGE_CTRL_CURRENT >> 6: 6313 case SMS_PAGE_CTRL_DEFAULT >> 6: 6314 case SMS_PAGE_CTRL_SAVED >> 6: 6315#ifdef NEEDTOPORT 6316 ctl_update_power_subpage(page); 6317#endif 6318 break; 6319 default: 6320#ifdef NEEDTOPORT 6321 EPRINT(0, "Invalid PC %d!!", pc); 6322#endif 6323 break; 6324 } 6325 return (0); 6326} 6327 6328 6329int 6330ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6331 struct ctl_page_index *page_index, uint8_t *page_ptr) 6332{ 6333 struct copan_aps_subpage *user_sp; 6334 struct copan_aps_subpage *current_sp; 6335 union ctl_modepage_info *modepage_info; 6336 struct ctl_softc *softc; 6337 struct ctl_lun *lun; 6338 int retval; 6339 6340 retval = CTL_RETVAL_COMPLETE; 6341 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6342 (page_index->page_len * CTL_PAGE_CURRENT)); 6343 softc = control_softc; 6344 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6345 6346 user_sp = (struct copan_aps_subpage *)page_ptr; 6347 6348 modepage_info = (union ctl_modepage_info *) 6349 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6350 6351 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6352 modepage_info->header.subpage = page_index->subpage; 6353 modepage_info->aps.lock_active = user_sp->lock_active; 6354 6355 mtx_lock(&softc->ctl_lock); 6356 6357 /* 6358 * If there is a request to lock the LUN and another LUN is locked 6359 * this is an error. If the requested LUN is already locked ignore 6360 * the request. If no LUN is locked attempt to lock it. 6361 * if there is a request to unlock the LUN and the LUN is currently 6362 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6363 * if another LUN is locked or no LUN is locked. 6364 */ 6365 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6366 if (softc->aps_locked_lun == lun->lun) { 6367 /* 6368 * This LUN is already locked, so we're done. 6369 */ 6370 retval = CTL_RETVAL_COMPLETE; 6371 } else if (softc->aps_locked_lun == 0) { 6372 /* 6373 * No one has the lock, pass the request to the 6374 * backend. 6375 */ 6376 retval = lun->backend->config_write( 6377 (union ctl_io *)ctsio); 6378 } else { 6379 /* 6380 * Someone else has the lock, throw out the request. 6381 */ 6382 ctl_set_already_locked(ctsio); 6383 free(ctsio->kern_data_ptr, M_CTL); 6384 ctl_done((union ctl_io *)ctsio); 6385 6386 /* 6387 * Set the return value so that ctl_do_mode_select() 6388 * won't try to complete the command. We already 6389 * completed it here. 6390 */ 6391 retval = CTL_RETVAL_ERROR; 6392 } 6393 } else if (softc->aps_locked_lun == lun->lun) { 6394 /* 6395 * This LUN is locked, so pass the unlock request to the 6396 * backend. 6397 */ 6398 retval = lun->backend->config_write((union ctl_io *)ctsio); 6399 } 6400 mtx_unlock(&softc->ctl_lock); 6401 6402 return (retval); 6403} 6404 6405int 6406ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6407 struct ctl_page_index *page_index, 6408 uint8_t *page_ptr) 6409{ 6410 uint8_t *c; 6411 int i; 6412 6413 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6414 ctl_time_io_secs = 6415 (c[0] << 8) | 6416 (c[1] << 0) | 6417 0; 6418 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6419 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6420 printf("page data:"); 6421 for (i=0; i<8; i++) 6422 printf(" %.2x",page_ptr[i]); 6423 printf("\n"); 6424 return (0); 6425} 6426 6427int 6428ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6429 struct ctl_page_index *page_index, 6430 int pc) 6431{ 6432 struct copan_debugconf_subpage *page; 6433 6434 page = (struct copan_debugconf_subpage *)page_index->page_data + 6435 (page_index->page_len * pc); 6436 6437 switch (pc) { 6438 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6439 case SMS_PAGE_CTRL_DEFAULT >> 6: 6440 case SMS_PAGE_CTRL_SAVED >> 6: 6441 /* 6442 * We don't update the changable or default bits for this page. 6443 */ 6444 break; 6445 case SMS_PAGE_CTRL_CURRENT >> 6: 6446 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6447 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6448 break; 6449 default: 6450#ifdef NEEDTOPORT 6451 EPRINT(0, "Invalid PC %d!!", pc); 6452#endif /* NEEDTOPORT */ 6453 break; 6454 } 6455 return (0); 6456} 6457 6458 6459static int 6460ctl_do_mode_select(union ctl_io *io) 6461{ 6462 struct scsi_mode_page_header *page_header; 6463 struct ctl_page_index *page_index; 6464 struct ctl_scsiio *ctsio; 6465 int control_dev, page_len; 6466 int page_len_offset, page_len_size; 6467 union ctl_modepage_info *modepage_info; 6468 struct ctl_lun *lun; 6469 int *len_left, *len_used; 6470 int retval, i; 6471 6472 ctsio = &io->scsiio; 6473 page_index = NULL; 6474 page_len = 0; 6475 retval = CTL_RETVAL_COMPLETE; 6476 6477 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6478 6479 if (lun->be_lun->lun_type != T_DIRECT) 6480 control_dev = 1; 6481 else 6482 control_dev = 0; 6483 6484 modepage_info = (union ctl_modepage_info *) 6485 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6486 len_left = &modepage_info->header.len_left; 6487 len_used = &modepage_info->header.len_used; 6488 6489do_next_page: 6490 6491 page_header = (struct scsi_mode_page_header *) 6492 (ctsio->kern_data_ptr + *len_used); 6493 6494 if (*len_left == 0) { 6495 free(ctsio->kern_data_ptr, M_CTL); 6496 ctl_set_success(ctsio); 6497 ctl_done((union ctl_io *)ctsio); 6498 return (CTL_RETVAL_COMPLETE); 6499 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6500 6501 free(ctsio->kern_data_ptr, M_CTL); 6502 ctl_set_param_len_error(ctsio); 6503 ctl_done((union ctl_io *)ctsio); 6504 return (CTL_RETVAL_COMPLETE); 6505 6506 } else if ((page_header->page_code & SMPH_SPF) 6507 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6508 6509 free(ctsio->kern_data_ptr, M_CTL); 6510 ctl_set_param_len_error(ctsio); 6511 ctl_done((union ctl_io *)ctsio); 6512 return (CTL_RETVAL_COMPLETE); 6513 } 6514 6515 6516 /* 6517 * XXX KDM should we do something with the block descriptor? 6518 */ 6519 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6520 6521 if ((control_dev != 0) 6522 && (lun->mode_pages.index[i].page_flags & 6523 CTL_PAGE_FLAG_DISK_ONLY)) 6524 continue; 6525 6526 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6527 (page_header->page_code & SMPH_PC_MASK)) 6528 continue; 6529 6530 /* 6531 * If neither page has a subpage code, then we've got a 6532 * match. 6533 */ 6534 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6535 && ((page_header->page_code & SMPH_SPF) == 0)) { 6536 page_index = &lun->mode_pages.index[i]; 6537 page_len = page_header->page_length; 6538 break; 6539 } 6540 6541 /* 6542 * If both pages have subpages, then the subpage numbers 6543 * have to match. 6544 */ 6545 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6546 && (page_header->page_code & SMPH_SPF)) { 6547 struct scsi_mode_page_header_sp *sph; 6548 6549 sph = (struct scsi_mode_page_header_sp *)page_header; 6550 6551 if (lun->mode_pages.index[i].subpage == 6552 sph->subpage) { 6553 page_index = &lun->mode_pages.index[i]; 6554 page_len = scsi_2btoul(sph->page_length); 6555 break; 6556 } 6557 } 6558 } 6559 6560 /* 6561 * If we couldn't find the page, or if we don't have a mode select 6562 * handler for it, send back an error to the user. 6563 */ 6564 if ((page_index == NULL) 6565 || (page_index->select_handler == NULL)) { 6566 ctl_set_invalid_field(ctsio, 6567 /*sks_valid*/ 1, 6568 /*command*/ 0, 6569 /*field*/ *len_used, 6570 /*bit_valid*/ 0, 6571 /*bit*/ 0); 6572 free(ctsio->kern_data_ptr, M_CTL); 6573 ctl_done((union ctl_io *)ctsio); 6574 return (CTL_RETVAL_COMPLETE); 6575 } 6576 6577 if (page_index->page_code & SMPH_SPF) { 6578 page_len_offset = 2; 6579 page_len_size = 2; 6580 } else { 6581 page_len_size = 1; 6582 page_len_offset = 1; 6583 } 6584 6585 /* 6586 * If the length the initiator gives us isn't the one we specify in 6587 * the mode page header, or if they didn't specify enough data in 6588 * the CDB to avoid truncating this page, kick out the request. 6589 */ 6590 if ((page_len != (page_index->page_len - page_len_offset - 6591 page_len_size)) 6592 || (*len_left < page_index->page_len)) { 6593 6594 6595 ctl_set_invalid_field(ctsio, 6596 /*sks_valid*/ 1, 6597 /*command*/ 0, 6598 /*field*/ *len_used + page_len_offset, 6599 /*bit_valid*/ 0, 6600 /*bit*/ 0); 6601 free(ctsio->kern_data_ptr, M_CTL); 6602 ctl_done((union ctl_io *)ctsio); 6603 return (CTL_RETVAL_COMPLETE); 6604 } 6605 6606 /* 6607 * Run through the mode page, checking to make sure that the bits 6608 * the user changed are actually legal for him to change. 6609 */ 6610 for (i = 0; i < page_index->page_len; i++) { 6611 uint8_t *user_byte, *change_mask, *current_byte; 6612 int bad_bit; 6613 int j; 6614 6615 user_byte = (uint8_t *)page_header + i; 6616 change_mask = page_index->page_data + 6617 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6618 current_byte = page_index->page_data + 6619 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6620 6621 /* 6622 * Check to see whether the user set any bits in this byte 6623 * that he is not allowed to set. 6624 */ 6625 if ((*user_byte & ~(*change_mask)) == 6626 (*current_byte & ~(*change_mask))) 6627 continue; 6628 6629 /* 6630 * Go through bit by bit to determine which one is illegal. 6631 */ 6632 bad_bit = 0; 6633 for (j = 7; j >= 0; j--) { 6634 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6635 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6636 bad_bit = i; 6637 break; 6638 } 6639 } 6640 ctl_set_invalid_field(ctsio, 6641 /*sks_valid*/ 1, 6642 /*command*/ 0, 6643 /*field*/ *len_used + i, 6644 /*bit_valid*/ 1, 6645 /*bit*/ bad_bit); 6646 free(ctsio->kern_data_ptr, M_CTL); 6647 ctl_done((union ctl_io *)ctsio); 6648 return (CTL_RETVAL_COMPLETE); 6649 } 6650 6651 /* 6652 * Decrement these before we call the page handler, since we may 6653 * end up getting called back one way or another before the handler 6654 * returns to this context. 6655 */ 6656 *len_left -= page_index->page_len; 6657 *len_used += page_index->page_len; 6658 6659 retval = page_index->select_handler(ctsio, page_index, 6660 (uint8_t *)page_header); 6661 6662 /* 6663 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6664 * wait until this queued command completes to finish processing 6665 * the mode page. If it returns anything other than 6666 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6667 * already set the sense information, freed the data pointer, and 6668 * completed the io for us. 6669 */ 6670 if (retval != CTL_RETVAL_COMPLETE) 6671 goto bailout_no_done; 6672 6673 /* 6674 * If the initiator sent us more than one page, parse the next one. 6675 */ 6676 if (*len_left > 0) 6677 goto do_next_page; 6678 6679 ctl_set_success(ctsio); 6680 free(ctsio->kern_data_ptr, M_CTL); 6681 ctl_done((union ctl_io *)ctsio); 6682 6683bailout_no_done: 6684 6685 return (CTL_RETVAL_COMPLETE); 6686 6687} 6688 6689int 6690ctl_mode_select(struct ctl_scsiio *ctsio) 6691{ 6692 int param_len, pf, sp; 6693 int header_size, bd_len; 6694 int len_left, len_used; 6695 struct ctl_page_index *page_index; 6696 struct ctl_lun *lun; 6697 int control_dev, page_len; 6698 union ctl_modepage_info *modepage_info; 6699 int retval; 6700 6701 pf = 0; 6702 sp = 0; 6703 page_len = 0; 6704 len_used = 0; 6705 len_left = 0; 6706 retval = 0; 6707 bd_len = 0; 6708 page_index = NULL; 6709 6710 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6711 6712 if (lun->be_lun->lun_type != T_DIRECT) 6713 control_dev = 1; 6714 else 6715 control_dev = 0; 6716 6717 switch (ctsio->cdb[0]) { 6718 case MODE_SELECT_6: { 6719 struct scsi_mode_select_6 *cdb; 6720 6721 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6722 6723 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6724 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6725 6726 param_len = cdb->length; 6727 header_size = sizeof(struct scsi_mode_header_6); 6728 break; 6729 } 6730 case MODE_SELECT_10: { 6731 struct scsi_mode_select_10 *cdb; 6732 6733 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6734 6735 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6736 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6737 6738 param_len = scsi_2btoul(cdb->length); 6739 header_size = sizeof(struct scsi_mode_header_10); 6740 break; 6741 } 6742 default: 6743 ctl_set_invalid_opcode(ctsio); 6744 ctl_done((union ctl_io *)ctsio); 6745 return (CTL_RETVAL_COMPLETE); 6746 break; /* NOTREACHED */ 6747 } 6748 6749 /* 6750 * From SPC-3: 6751 * "A parameter list length of zero indicates that the Data-Out Buffer 6752 * shall be empty. This condition shall not be considered as an error." 6753 */ 6754 if (param_len == 0) { 6755 ctl_set_success(ctsio); 6756 ctl_done((union ctl_io *)ctsio); 6757 return (CTL_RETVAL_COMPLETE); 6758 } 6759 6760 /* 6761 * Since we'll hit this the first time through, prior to 6762 * allocation, we don't need to free a data buffer here. 6763 */ 6764 if (param_len < header_size) { 6765 ctl_set_param_len_error(ctsio); 6766 ctl_done((union ctl_io *)ctsio); 6767 return (CTL_RETVAL_COMPLETE); 6768 } 6769 6770 /* 6771 * Allocate the data buffer and grab the user's data. In theory, 6772 * we shouldn't have to sanity check the parameter list length here 6773 * because the maximum size is 64K. We should be able to malloc 6774 * that much without too many problems. 6775 */ 6776 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6777 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6778 ctsio->kern_data_len = param_len; 6779 ctsio->kern_total_len = param_len; 6780 ctsio->kern_data_resid = 0; 6781 ctsio->kern_rel_offset = 0; 6782 ctsio->kern_sg_entries = 0; 6783 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6784 ctsio->be_move_done = ctl_config_move_done; 6785 ctl_datamove((union ctl_io *)ctsio); 6786 6787 return (CTL_RETVAL_COMPLETE); 6788 } 6789 6790 switch (ctsio->cdb[0]) { 6791 case MODE_SELECT_6: { 6792 struct scsi_mode_header_6 *mh6; 6793 6794 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6795 bd_len = mh6->blk_desc_len; 6796 break; 6797 } 6798 case MODE_SELECT_10: { 6799 struct scsi_mode_header_10 *mh10; 6800 6801 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6802 bd_len = scsi_2btoul(mh10->blk_desc_len); 6803 break; 6804 } 6805 default: 6806 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6807 break; 6808 } 6809 6810 if (param_len < (header_size + bd_len)) { 6811 free(ctsio->kern_data_ptr, M_CTL); 6812 ctl_set_param_len_error(ctsio); 6813 ctl_done((union ctl_io *)ctsio); 6814 return (CTL_RETVAL_COMPLETE); 6815 } 6816 6817 /* 6818 * Set the IO_CONT flag, so that if this I/O gets passed to 6819 * ctl_config_write_done(), it'll get passed back to 6820 * ctl_do_mode_select() for further processing, or completion if 6821 * we're all done. 6822 */ 6823 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6824 ctsio->io_cont = ctl_do_mode_select; 6825 6826 modepage_info = (union ctl_modepage_info *) 6827 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6828 6829 memset(modepage_info, 0, sizeof(*modepage_info)); 6830 6831 len_left = param_len - header_size - bd_len; 6832 len_used = header_size + bd_len; 6833 6834 modepage_info->header.len_left = len_left; 6835 modepage_info->header.len_used = len_used; 6836 6837 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6838} 6839 6840int 6841ctl_mode_sense(struct ctl_scsiio *ctsio) 6842{ 6843 struct ctl_lun *lun; 6844 int pc, page_code, dbd, llba, subpage; 6845 int alloc_len, page_len, header_len, total_len; 6846 struct scsi_mode_block_descr *block_desc; 6847 struct ctl_page_index *page_index; 6848 int control_dev; 6849 6850 dbd = 0; 6851 llba = 0; 6852 block_desc = NULL; 6853 page_index = NULL; 6854 6855 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6856 6857 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6858 6859 if (lun->be_lun->lun_type != T_DIRECT) 6860 control_dev = 1; 6861 else 6862 control_dev = 0; 6863 6864 if (lun->flags & CTL_LUN_PR_RESERVED) { 6865 uint32_t residx; 6866 6867 /* 6868 * XXX KDM need a lock here. 6869 */ 6870 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6871 if ((lun->res_type == SPR_TYPE_EX_AC 6872 && residx != lun->pr_res_idx) 6873 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6874 || lun->res_type == SPR_TYPE_EX_AC_AR) 6875 && !lun->per_res[residx].registered)) { 6876 ctl_set_reservation_conflict(ctsio); 6877 ctl_done((union ctl_io *)ctsio); 6878 return (CTL_RETVAL_COMPLETE); 6879 } 6880 } 6881 6882 switch (ctsio->cdb[0]) { 6883 case MODE_SENSE_6: { 6884 struct scsi_mode_sense_6 *cdb; 6885 6886 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6887 6888 header_len = sizeof(struct scsi_mode_hdr_6); 6889 if (cdb->byte2 & SMS_DBD) 6890 dbd = 1; 6891 else 6892 header_len += sizeof(struct scsi_mode_block_descr); 6893 6894 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6895 page_code = cdb->page & SMS_PAGE_CODE; 6896 subpage = cdb->subpage; 6897 alloc_len = cdb->length; 6898 break; 6899 } 6900 case MODE_SENSE_10: { 6901 struct scsi_mode_sense_10 *cdb; 6902 6903 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6904 6905 header_len = sizeof(struct scsi_mode_hdr_10); 6906 6907 if (cdb->byte2 & SMS_DBD) 6908 dbd = 1; 6909 else 6910 header_len += sizeof(struct scsi_mode_block_descr); 6911 if (cdb->byte2 & SMS10_LLBAA) 6912 llba = 1; 6913 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6914 page_code = cdb->page & SMS_PAGE_CODE; 6915 subpage = cdb->subpage; 6916 alloc_len = scsi_2btoul(cdb->length); 6917 break; 6918 } 6919 default: 6920 ctl_set_invalid_opcode(ctsio); 6921 ctl_done((union ctl_io *)ctsio); 6922 return (CTL_RETVAL_COMPLETE); 6923 break; /* NOTREACHED */ 6924 } 6925 6926 /* 6927 * We have to make a first pass through to calculate the size of 6928 * the pages that match the user's query. Then we allocate enough 6929 * memory to hold it, and actually copy the data into the buffer. 6930 */ 6931 switch (page_code) { 6932 case SMS_ALL_PAGES_PAGE: { 6933 int i; 6934 6935 page_len = 0; 6936 6937 /* 6938 * At the moment, values other than 0 and 0xff here are 6939 * reserved according to SPC-3. 6940 */ 6941 if ((subpage != SMS_SUBPAGE_PAGE_0) 6942 && (subpage != SMS_SUBPAGE_ALL)) { 6943 ctl_set_invalid_field(ctsio, 6944 /*sks_valid*/ 1, 6945 /*command*/ 1, 6946 /*field*/ 3, 6947 /*bit_valid*/ 0, 6948 /*bit*/ 0); 6949 ctl_done((union ctl_io *)ctsio); 6950 return (CTL_RETVAL_COMPLETE); 6951 } 6952 6953 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6954 if ((control_dev != 0) 6955 && (lun->mode_pages.index[i].page_flags & 6956 CTL_PAGE_FLAG_DISK_ONLY)) 6957 continue; 6958 6959 /* 6960 * We don't use this subpage if the user didn't 6961 * request all subpages. 6962 */ 6963 if ((lun->mode_pages.index[i].subpage != 0) 6964 && (subpage == SMS_SUBPAGE_PAGE_0)) 6965 continue; 6966 6967#if 0 6968 printf("found page %#x len %d\n", 6969 lun->mode_pages.index[i].page_code & 6970 SMPH_PC_MASK, 6971 lun->mode_pages.index[i].page_len); 6972#endif 6973 page_len += lun->mode_pages.index[i].page_len; 6974 } 6975 break; 6976 } 6977 default: { 6978 int i; 6979 6980 page_len = 0; 6981 6982 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6983 /* Look for the right page code */ 6984 if ((lun->mode_pages.index[i].page_code & 6985 SMPH_PC_MASK) != page_code) 6986 continue; 6987 6988 /* Look for the right subpage or the subpage wildcard*/ 6989 if ((lun->mode_pages.index[i].subpage != subpage) 6990 && (subpage != SMS_SUBPAGE_ALL)) 6991 continue; 6992 6993 /* Make sure the page is supported for this dev type */ 6994 if ((control_dev != 0) 6995 && (lun->mode_pages.index[i].page_flags & 6996 CTL_PAGE_FLAG_DISK_ONLY)) 6997 continue; 6998 6999#if 0 7000 printf("found page %#x len %d\n", 7001 lun->mode_pages.index[i].page_code & 7002 SMPH_PC_MASK, 7003 lun->mode_pages.index[i].page_len); 7004#endif 7005 7006 page_len += lun->mode_pages.index[i].page_len; 7007 } 7008 7009 if (page_len == 0) { 7010 ctl_set_invalid_field(ctsio, 7011 /*sks_valid*/ 1, 7012 /*command*/ 1, 7013 /*field*/ 2, 7014 /*bit_valid*/ 1, 7015 /*bit*/ 5); 7016 ctl_done((union ctl_io *)ctsio); 7017 return (CTL_RETVAL_COMPLETE); 7018 } 7019 break; 7020 } 7021 } 7022 7023 total_len = header_len + page_len; 7024#if 0 7025 printf("header_len = %d, page_len = %d, total_len = %d\n", 7026 header_len, page_len, total_len); 7027#endif 7028 7029 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7030 ctsio->kern_sg_entries = 0; 7031 ctsio->kern_data_resid = 0; 7032 ctsio->kern_rel_offset = 0; 7033 if (total_len < alloc_len) { 7034 ctsio->residual = alloc_len - total_len; 7035 ctsio->kern_data_len = total_len; 7036 ctsio->kern_total_len = total_len; 7037 } else { 7038 ctsio->residual = 0; 7039 ctsio->kern_data_len = alloc_len; 7040 ctsio->kern_total_len = alloc_len; 7041 } 7042 7043 switch (ctsio->cdb[0]) { 7044 case MODE_SENSE_6: { 7045 struct scsi_mode_hdr_6 *header; 7046 7047 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7048 7049 header->datalen = ctl_min(total_len - 1, 254); 7050 if (control_dev == 0) 7051 header->dev_specific = 0x10; /* DPOFUA */ 7052 if (dbd) 7053 header->block_descr_len = 0; 7054 else 7055 header->block_descr_len = 7056 sizeof(struct scsi_mode_block_descr); 7057 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7058 break; 7059 } 7060 case MODE_SENSE_10: { 7061 struct scsi_mode_hdr_10 *header; 7062 int datalen; 7063 7064 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7065 7066 datalen = ctl_min(total_len - 2, 65533); 7067 scsi_ulto2b(datalen, header->datalen); 7068 if (control_dev == 0) 7069 header->dev_specific = 0x10; /* DPOFUA */ 7070 if (dbd) 7071 scsi_ulto2b(0, header->block_descr_len); 7072 else 7073 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7074 header->block_descr_len); 7075 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7076 break; 7077 } 7078 default: 7079 panic("invalid CDB type %#x", ctsio->cdb[0]); 7080 break; /* NOTREACHED */ 7081 } 7082 7083 /* 7084 * If we've got a disk, use its blocksize in the block 7085 * descriptor. Otherwise, just set it to 0. 7086 */ 7087 if (dbd == 0) { 7088 if (control_dev == 0) 7089 scsi_ulto3b(lun->be_lun->blocksize, 7090 block_desc->block_len); 7091 else 7092 scsi_ulto3b(0, block_desc->block_len); 7093 } 7094 7095 switch (page_code) { 7096 case SMS_ALL_PAGES_PAGE: { 7097 int i, data_used; 7098 7099 data_used = header_len; 7100 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7101 struct ctl_page_index *page_index; 7102 7103 page_index = &lun->mode_pages.index[i]; 7104 7105 if ((control_dev != 0) 7106 && (page_index->page_flags & 7107 CTL_PAGE_FLAG_DISK_ONLY)) 7108 continue; 7109 7110 /* 7111 * We don't use this subpage if the user didn't 7112 * request all subpages. We already checked (above) 7113 * to make sure the user only specified a subpage 7114 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7115 */ 7116 if ((page_index->subpage != 0) 7117 && (subpage == SMS_SUBPAGE_PAGE_0)) 7118 continue; 7119 7120 /* 7121 * Call the handler, if it exists, to update the 7122 * page to the latest values. 7123 */ 7124 if (page_index->sense_handler != NULL) 7125 page_index->sense_handler(ctsio, page_index,pc); 7126 7127 memcpy(ctsio->kern_data_ptr + data_used, 7128 page_index->page_data + 7129 (page_index->page_len * pc), 7130 page_index->page_len); 7131 data_used += page_index->page_len; 7132 } 7133 break; 7134 } 7135 default: { 7136 int i, data_used; 7137 7138 data_used = header_len; 7139 7140 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7141 struct ctl_page_index *page_index; 7142 7143 page_index = &lun->mode_pages.index[i]; 7144 7145 /* Look for the right page code */ 7146 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7147 continue; 7148 7149 /* Look for the right subpage or the subpage wildcard*/ 7150 if ((page_index->subpage != subpage) 7151 && (subpage != SMS_SUBPAGE_ALL)) 7152 continue; 7153 7154 /* Make sure the page is supported for this dev type */ 7155 if ((control_dev != 0) 7156 && (page_index->page_flags & 7157 CTL_PAGE_FLAG_DISK_ONLY)) 7158 continue; 7159 7160 /* 7161 * Call the handler, if it exists, to update the 7162 * page to the latest values. 7163 */ 7164 if (page_index->sense_handler != NULL) 7165 page_index->sense_handler(ctsio, page_index,pc); 7166 7167 memcpy(ctsio->kern_data_ptr + data_used, 7168 page_index->page_data + 7169 (page_index->page_len * pc), 7170 page_index->page_len); 7171 data_used += page_index->page_len; 7172 } 7173 break; 7174 } 7175 } 7176 7177 ctsio->scsi_status = SCSI_STATUS_OK; 7178 7179 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7180 ctsio->be_move_done = ctl_config_move_done; 7181 ctl_datamove((union ctl_io *)ctsio); 7182 7183 return (CTL_RETVAL_COMPLETE); 7184} 7185 7186int 7187ctl_read_capacity(struct ctl_scsiio *ctsio) 7188{ 7189 struct scsi_read_capacity *cdb; 7190 struct scsi_read_capacity_data *data; 7191 struct ctl_lun *lun; 7192 uint32_t lba; 7193 7194 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7195 7196 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7197 7198 lba = scsi_4btoul(cdb->addr); 7199 if (((cdb->pmi & SRC_PMI) == 0) 7200 && (lba != 0)) { 7201 ctl_set_invalid_field(/*ctsio*/ ctsio, 7202 /*sks_valid*/ 1, 7203 /*command*/ 1, 7204 /*field*/ 2, 7205 /*bit_valid*/ 0, 7206 /*bit*/ 0); 7207 ctl_done((union ctl_io *)ctsio); 7208 return (CTL_RETVAL_COMPLETE); 7209 } 7210 7211 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7212 7213 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7214 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7215 ctsio->residual = 0; 7216 ctsio->kern_data_len = sizeof(*data); 7217 ctsio->kern_total_len = sizeof(*data); 7218 ctsio->kern_data_resid = 0; 7219 ctsio->kern_rel_offset = 0; 7220 ctsio->kern_sg_entries = 0; 7221 7222 /* 7223 * If the maximum LBA is greater than 0xfffffffe, the user must 7224 * issue a SERVICE ACTION IN (16) command, with the read capacity 7225 * serivce action set. 7226 */ 7227 if (lun->be_lun->maxlba > 0xfffffffe) 7228 scsi_ulto4b(0xffffffff, data->addr); 7229 else 7230 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7231 7232 /* 7233 * XXX KDM this may not be 512 bytes... 7234 */ 7235 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7236 7237 ctsio->scsi_status = SCSI_STATUS_OK; 7238 7239 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7240 ctsio->be_move_done = ctl_config_move_done; 7241 ctl_datamove((union ctl_io *)ctsio); 7242 7243 return (CTL_RETVAL_COMPLETE); 7244} 7245 7246int 7247ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7248{ 7249 struct scsi_read_capacity_16 *cdb; 7250 struct scsi_read_capacity_data_long *data; 7251 struct ctl_lun *lun; 7252 uint64_t lba; 7253 uint32_t alloc_len; 7254 7255 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7256 7257 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7258 7259 alloc_len = scsi_4btoul(cdb->alloc_len); 7260 lba = scsi_8btou64(cdb->addr); 7261 7262 if ((cdb->reladr & SRC16_PMI) 7263 && (lba != 0)) { 7264 ctl_set_invalid_field(/*ctsio*/ ctsio, 7265 /*sks_valid*/ 1, 7266 /*command*/ 1, 7267 /*field*/ 2, 7268 /*bit_valid*/ 0, 7269 /*bit*/ 0); 7270 ctl_done((union ctl_io *)ctsio); 7271 return (CTL_RETVAL_COMPLETE); 7272 } 7273 7274 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7275 7276 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7277 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7278 7279 if (sizeof(*data) < alloc_len) { 7280 ctsio->residual = alloc_len - sizeof(*data); 7281 ctsio->kern_data_len = sizeof(*data); 7282 ctsio->kern_total_len = sizeof(*data); 7283 } else { 7284 ctsio->residual = 0; 7285 ctsio->kern_data_len = alloc_len; 7286 ctsio->kern_total_len = alloc_len; 7287 } 7288 ctsio->kern_data_resid = 0; 7289 ctsio->kern_rel_offset = 0; 7290 ctsio->kern_sg_entries = 0; 7291 7292 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7293 /* XXX KDM this may not be 512 bytes... */ 7294 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7295 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7296 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7297 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7298 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7299 7300 ctsio->scsi_status = SCSI_STATUS_OK; 7301 7302 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7303 ctsio->be_move_done = ctl_config_move_done; 7304 ctl_datamove((union ctl_io *)ctsio); 7305 7306 return (CTL_RETVAL_COMPLETE); 7307} 7308 7309int 7310ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7311{ 7312 struct scsi_maintenance_in *cdb; 7313 int retval; 7314 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7315 int num_target_port_groups, num_target_ports, single; 7316 struct ctl_lun *lun; 7317 struct ctl_softc *softc; 7318 struct ctl_port *port; 7319 struct scsi_target_group_data *rtg_ptr; 7320 struct scsi_target_group_data_extended *rtg_ext_ptr; 7321 struct scsi_target_port_group_descriptor *tpg_desc; 7322 7323 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7324 7325 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7326 softc = control_softc; 7327 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7328 7329 retval = CTL_RETVAL_COMPLETE; 7330 7331 switch (cdb->byte2 & STG_PDF_MASK) { 7332 case STG_PDF_LENGTH: 7333 ext = 0; 7334 break; 7335 case STG_PDF_EXTENDED: 7336 ext = 1; 7337 break; 7338 default: 7339 ctl_set_invalid_field(/*ctsio*/ ctsio, 7340 /*sks_valid*/ 1, 7341 /*command*/ 1, 7342 /*field*/ 2, 7343 /*bit_valid*/ 1, 7344 /*bit*/ 5); 7345 ctl_done((union ctl_io *)ctsio); 7346 return(retval); 7347 } 7348 7349 single = ctl_is_single; 7350 if (single) 7351 num_target_port_groups = 1; 7352 else 7353 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7354 num_target_ports = 0; 7355 mtx_lock(&softc->ctl_lock); 7356 STAILQ_FOREACH(port, &softc->port_list, links) { 7357 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7358 continue; 7359 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7360 continue; 7361 num_target_ports++; 7362 } 7363 mtx_unlock(&softc->ctl_lock); 7364 7365 if (ext) 7366 total_len = sizeof(struct scsi_target_group_data_extended); 7367 else 7368 total_len = sizeof(struct scsi_target_group_data); 7369 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7370 num_target_port_groups + 7371 sizeof(struct scsi_target_port_descriptor) * 7372 num_target_ports * num_target_port_groups; 7373 7374 alloc_len = scsi_4btoul(cdb->length); 7375 7376 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7377 7378 ctsio->kern_sg_entries = 0; 7379 7380 if (total_len < alloc_len) { 7381 ctsio->residual = alloc_len - total_len; 7382 ctsio->kern_data_len = total_len; 7383 ctsio->kern_total_len = total_len; 7384 } else { 7385 ctsio->residual = 0; 7386 ctsio->kern_data_len = alloc_len; 7387 ctsio->kern_total_len = alloc_len; 7388 } 7389 ctsio->kern_data_resid = 0; 7390 ctsio->kern_rel_offset = 0; 7391 7392 if (ext) { 7393 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7394 ctsio->kern_data_ptr; 7395 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7396 rtg_ext_ptr->format_type = 0x10; 7397 rtg_ext_ptr->implicit_transition_time = 0; 7398 tpg_desc = &rtg_ext_ptr->groups[0]; 7399 } else { 7400 rtg_ptr = (struct scsi_target_group_data *) 7401 ctsio->kern_data_ptr; 7402 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7403 tpg_desc = &rtg_ptr->groups[0]; 7404 } 7405 7406 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7407 mtx_lock(&softc->ctl_lock); 7408 for (g = 0; g < num_target_port_groups; g++) { 7409 if (g == pg) 7410 tpg_desc->pref_state = TPG_PRIMARY | 7411 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7412 else 7413 tpg_desc->pref_state = 7414 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7415 tpg_desc->support = TPG_AO_SUP; 7416 if (!single) 7417 tpg_desc->support |= TPG_AN_SUP; 7418 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7419 tpg_desc->status = TPG_IMPLICIT; 7420 pc = 0; 7421 STAILQ_FOREACH(port, &softc->port_list, links) { 7422 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7423 continue; 7424 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7425 CTL_MAX_LUNS) 7426 continue; 7427 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7428 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7429 relative_target_port_identifier); 7430 pc++; 7431 } 7432 tpg_desc->target_port_count = pc; 7433 tpg_desc = (struct scsi_target_port_group_descriptor *) 7434 &tpg_desc->descriptors[pc]; 7435 } 7436 mtx_unlock(&softc->ctl_lock); 7437 7438 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7439 ctsio->be_move_done = ctl_config_move_done; 7440 7441 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7442 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7443 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7444 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7445 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7446 7447 ctl_datamove((union ctl_io *)ctsio); 7448 return(retval); 7449} 7450 7451int 7452ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7453{ 7454 struct ctl_lun *lun; 7455 struct scsi_report_supported_opcodes *cdb; 7456 const struct ctl_cmd_entry *entry, *sentry; 7457 struct scsi_report_supported_opcodes_all *all; 7458 struct scsi_report_supported_opcodes_descr *descr; 7459 struct scsi_report_supported_opcodes_one *one; 7460 int retval; 7461 int alloc_len, total_len; 7462 int opcode, service_action, i, j, num; 7463 7464 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7465 7466 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7467 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7468 7469 retval = CTL_RETVAL_COMPLETE; 7470 7471 opcode = cdb->requested_opcode; 7472 service_action = scsi_2btoul(cdb->requested_service_action); 7473 switch (cdb->options & RSO_OPTIONS_MASK) { 7474 case RSO_OPTIONS_ALL: 7475 num = 0; 7476 for (i = 0; i < 256; i++) { 7477 entry = &ctl_cmd_table[i]; 7478 if (entry->flags & CTL_CMD_FLAG_SA5) { 7479 for (j = 0; j < 32; j++) { 7480 sentry = &((const struct ctl_cmd_entry *) 7481 entry->execute)[j]; 7482 if (ctl_cmd_applicable( 7483 lun->be_lun->lun_type, sentry)) 7484 num++; 7485 } 7486 } else { 7487 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7488 entry)) 7489 num++; 7490 } 7491 } 7492 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7493 num * sizeof(struct scsi_report_supported_opcodes_descr); 7494 break; 7495 case RSO_OPTIONS_OC: 7496 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7497 ctl_set_invalid_field(/*ctsio*/ ctsio, 7498 /*sks_valid*/ 1, 7499 /*command*/ 1, 7500 /*field*/ 2, 7501 /*bit_valid*/ 1, 7502 /*bit*/ 2); 7503 ctl_done((union ctl_io *)ctsio); 7504 return (CTL_RETVAL_COMPLETE); 7505 } 7506 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7507 break; 7508 case RSO_OPTIONS_OC_SA: 7509 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7510 service_action >= 32) { 7511 ctl_set_invalid_field(/*ctsio*/ ctsio, 7512 /*sks_valid*/ 1, 7513 /*command*/ 1, 7514 /*field*/ 2, 7515 /*bit_valid*/ 1, 7516 /*bit*/ 2); 7517 ctl_done((union ctl_io *)ctsio); 7518 return (CTL_RETVAL_COMPLETE); 7519 } 7520 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7521 break; 7522 default: 7523 ctl_set_invalid_field(/*ctsio*/ ctsio, 7524 /*sks_valid*/ 1, 7525 /*command*/ 1, 7526 /*field*/ 2, 7527 /*bit_valid*/ 1, 7528 /*bit*/ 2); 7529 ctl_done((union ctl_io *)ctsio); 7530 return (CTL_RETVAL_COMPLETE); 7531 } 7532 7533 alloc_len = scsi_4btoul(cdb->length); 7534 7535 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7536 7537 ctsio->kern_sg_entries = 0; 7538 7539 if (total_len < alloc_len) { 7540 ctsio->residual = alloc_len - total_len; 7541 ctsio->kern_data_len = total_len; 7542 ctsio->kern_total_len = total_len; 7543 } else { 7544 ctsio->residual = 0; 7545 ctsio->kern_data_len = alloc_len; 7546 ctsio->kern_total_len = alloc_len; 7547 } 7548 ctsio->kern_data_resid = 0; 7549 ctsio->kern_rel_offset = 0; 7550 7551 switch (cdb->options & RSO_OPTIONS_MASK) { 7552 case RSO_OPTIONS_ALL: 7553 all = (struct scsi_report_supported_opcodes_all *) 7554 ctsio->kern_data_ptr; 7555 num = 0; 7556 for (i = 0; i < 256; i++) { 7557 entry = &ctl_cmd_table[i]; 7558 if (entry->flags & CTL_CMD_FLAG_SA5) { 7559 for (j = 0; j < 32; j++) { 7560 sentry = &((const struct ctl_cmd_entry *) 7561 entry->execute)[j]; 7562 if (!ctl_cmd_applicable( 7563 lun->be_lun->lun_type, sentry)) 7564 continue; 7565 descr = &all->descr[num++]; 7566 descr->opcode = i; 7567 scsi_ulto2b(j, descr->service_action); 7568 descr->flags = RSO_SERVACTV; 7569 scsi_ulto2b(sentry->length, 7570 descr->cdb_length); 7571 } 7572 } else { 7573 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7574 entry)) 7575 continue; 7576 descr = &all->descr[num++]; 7577 descr->opcode = i; 7578 scsi_ulto2b(0, descr->service_action); 7579 descr->flags = 0; 7580 scsi_ulto2b(entry->length, descr->cdb_length); 7581 } 7582 } 7583 scsi_ulto4b( 7584 num * sizeof(struct scsi_report_supported_opcodes_descr), 7585 all->length); 7586 break; 7587 case RSO_OPTIONS_OC: 7588 one = (struct scsi_report_supported_opcodes_one *) 7589 ctsio->kern_data_ptr; 7590 entry = &ctl_cmd_table[opcode]; 7591 goto fill_one; 7592 case RSO_OPTIONS_OC_SA: 7593 one = (struct scsi_report_supported_opcodes_one *) 7594 ctsio->kern_data_ptr; 7595 entry = &ctl_cmd_table[opcode]; 7596 entry = &((const struct ctl_cmd_entry *) 7597 entry->execute)[service_action]; 7598fill_one: 7599 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7600 one->support = 3; 7601 scsi_ulto2b(entry->length, one->cdb_length); 7602 one->cdb_usage[0] = opcode; 7603 memcpy(&one->cdb_usage[1], entry->usage, 7604 entry->length - 1); 7605 } else 7606 one->support = 1; 7607 break; 7608 } 7609 7610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7611 ctsio->be_move_done = ctl_config_move_done; 7612 7613 ctl_datamove((union ctl_io *)ctsio); 7614 return(retval); 7615} 7616 7617int 7618ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7619{ 7620 struct ctl_lun *lun; 7621 struct scsi_report_supported_tmf *cdb; 7622 struct scsi_report_supported_tmf_data *data; 7623 int retval; 7624 int alloc_len, total_len; 7625 7626 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7627 7628 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7629 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7630 7631 retval = CTL_RETVAL_COMPLETE; 7632 7633 total_len = sizeof(struct scsi_report_supported_tmf_data); 7634 alloc_len = scsi_4btoul(cdb->length); 7635 7636 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7637 7638 ctsio->kern_sg_entries = 0; 7639 7640 if (total_len < alloc_len) { 7641 ctsio->residual = alloc_len - total_len; 7642 ctsio->kern_data_len = total_len; 7643 ctsio->kern_total_len = total_len; 7644 } else { 7645 ctsio->residual = 0; 7646 ctsio->kern_data_len = alloc_len; 7647 ctsio->kern_total_len = alloc_len; 7648 } 7649 ctsio->kern_data_resid = 0; 7650 ctsio->kern_rel_offset = 0; 7651 7652 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7653 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7654 data->byte2 |= RST_ITNRS; 7655 7656 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7657 ctsio->be_move_done = ctl_config_move_done; 7658 7659 ctl_datamove((union ctl_io *)ctsio); 7660 return (retval); 7661} 7662 7663int 7664ctl_report_timestamp(struct ctl_scsiio *ctsio) 7665{ 7666 struct ctl_lun *lun; 7667 struct scsi_report_timestamp *cdb; 7668 struct scsi_report_timestamp_data *data; 7669 struct timeval tv; 7670 int64_t timestamp; 7671 int retval; 7672 int alloc_len, total_len; 7673 7674 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7675 7676 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7677 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7678 7679 retval = CTL_RETVAL_COMPLETE; 7680 7681 total_len = sizeof(struct scsi_report_timestamp_data); 7682 alloc_len = scsi_4btoul(cdb->length); 7683 7684 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7685 7686 ctsio->kern_sg_entries = 0; 7687 7688 if (total_len < alloc_len) { 7689 ctsio->residual = alloc_len - total_len; 7690 ctsio->kern_data_len = total_len; 7691 ctsio->kern_total_len = total_len; 7692 } else { 7693 ctsio->residual = 0; 7694 ctsio->kern_data_len = alloc_len; 7695 ctsio->kern_total_len = alloc_len; 7696 } 7697 ctsio->kern_data_resid = 0; 7698 ctsio->kern_rel_offset = 0; 7699 7700 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7701 scsi_ulto2b(sizeof(*data) - 2, data->length); 7702 data->origin = RTS_ORIG_OUTSIDE; 7703 getmicrotime(&tv); 7704 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7705 scsi_ulto4b(timestamp >> 16, data->timestamp); 7706 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7707 7708 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7709 ctsio->be_move_done = ctl_config_move_done; 7710 7711 ctl_datamove((union ctl_io *)ctsio); 7712 return (retval); 7713} 7714 7715int 7716ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7717{ 7718 struct scsi_per_res_in *cdb; 7719 int alloc_len, total_len = 0; 7720 /* struct scsi_per_res_in_rsrv in_data; */ 7721 struct ctl_lun *lun; 7722 struct ctl_softc *softc; 7723 7724 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7725 7726 softc = control_softc; 7727 7728 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7729 7730 alloc_len = scsi_2btoul(cdb->length); 7731 7732 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7733 7734retry: 7735 mtx_lock(&lun->lun_lock); 7736 switch (cdb->action) { 7737 case SPRI_RK: /* read keys */ 7738 total_len = sizeof(struct scsi_per_res_in_keys) + 7739 lun->pr_key_count * 7740 sizeof(struct scsi_per_res_key); 7741 break; 7742 case SPRI_RR: /* read reservation */ 7743 if (lun->flags & CTL_LUN_PR_RESERVED) 7744 total_len = sizeof(struct scsi_per_res_in_rsrv); 7745 else 7746 total_len = sizeof(struct scsi_per_res_in_header); 7747 break; 7748 case SPRI_RC: /* report capabilities */ 7749 total_len = sizeof(struct scsi_per_res_cap); 7750 break; 7751 case SPRI_RS: /* read full status */ 7752 total_len = sizeof(struct scsi_per_res_in_header) + 7753 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7754 lun->pr_key_count; 7755 break; 7756 default: 7757 panic("Invalid PR type %x", cdb->action); 7758 } 7759 mtx_unlock(&lun->lun_lock); 7760 7761 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7762 7763 if (total_len < alloc_len) { 7764 ctsio->residual = alloc_len - total_len; 7765 ctsio->kern_data_len = total_len; 7766 ctsio->kern_total_len = total_len; 7767 } else { 7768 ctsio->residual = 0; 7769 ctsio->kern_data_len = alloc_len; 7770 ctsio->kern_total_len = alloc_len; 7771 } 7772 7773 ctsio->kern_data_resid = 0; 7774 ctsio->kern_rel_offset = 0; 7775 ctsio->kern_sg_entries = 0; 7776 7777 mtx_lock(&lun->lun_lock); 7778 switch (cdb->action) { 7779 case SPRI_RK: { // read keys 7780 struct scsi_per_res_in_keys *res_keys; 7781 int i, key_count; 7782 7783 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7784 7785 /* 7786 * We had to drop the lock to allocate our buffer, which 7787 * leaves time for someone to come in with another 7788 * persistent reservation. (That is unlikely, though, 7789 * since this should be the only persistent reservation 7790 * command active right now.) 7791 */ 7792 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7793 (lun->pr_key_count * 7794 sizeof(struct scsi_per_res_key)))){ 7795 mtx_unlock(&lun->lun_lock); 7796 free(ctsio->kern_data_ptr, M_CTL); 7797 printf("%s: reservation length changed, retrying\n", 7798 __func__); 7799 goto retry; 7800 } 7801 7802 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7803 7804 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7805 lun->pr_key_count, res_keys->header.length); 7806 7807 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7808 if (!lun->per_res[i].registered) 7809 continue; 7810 7811 /* 7812 * We used lun->pr_key_count to calculate the 7813 * size to allocate. If it turns out the number of 7814 * initiators with the registered flag set is 7815 * larger than that (i.e. they haven't been kept in 7816 * sync), we've got a problem. 7817 */ 7818 if (key_count >= lun->pr_key_count) { 7819#ifdef NEEDTOPORT 7820 csevent_log(CSC_CTL | CSC_SHELF_SW | 7821 CTL_PR_ERROR, 7822 csevent_LogType_Fault, 7823 csevent_AlertLevel_Yellow, 7824 csevent_FRU_ShelfController, 7825 csevent_FRU_Firmware, 7826 csevent_FRU_Unknown, 7827 "registered keys %d >= key " 7828 "count %d", key_count, 7829 lun->pr_key_count); 7830#endif 7831 key_count++; 7832 continue; 7833 } 7834 memcpy(res_keys->keys[key_count].key, 7835 lun->per_res[i].res_key.key, 7836 ctl_min(sizeof(res_keys->keys[key_count].key), 7837 sizeof(lun->per_res[i].res_key))); 7838 key_count++; 7839 } 7840 break; 7841 } 7842 case SPRI_RR: { // read reservation 7843 struct scsi_per_res_in_rsrv *res; 7844 int tmp_len, header_only; 7845 7846 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7847 7848 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7849 7850 if (lun->flags & CTL_LUN_PR_RESERVED) 7851 { 7852 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7853 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7854 res->header.length); 7855 header_only = 0; 7856 } else { 7857 tmp_len = sizeof(struct scsi_per_res_in_header); 7858 scsi_ulto4b(0, res->header.length); 7859 header_only = 1; 7860 } 7861 7862 /* 7863 * We had to drop the lock to allocate our buffer, which 7864 * leaves time for someone to come in with another 7865 * persistent reservation. (That is unlikely, though, 7866 * since this should be the only persistent reservation 7867 * command active right now.) 7868 */ 7869 if (tmp_len != total_len) { 7870 mtx_unlock(&lun->lun_lock); 7871 free(ctsio->kern_data_ptr, M_CTL); 7872 printf("%s: reservation status changed, retrying\n", 7873 __func__); 7874 goto retry; 7875 } 7876 7877 /* 7878 * No reservation held, so we're done. 7879 */ 7880 if (header_only != 0) 7881 break; 7882 7883 /* 7884 * If the registration is an All Registrants type, the key 7885 * is 0, since it doesn't really matter. 7886 */ 7887 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7888 memcpy(res->data.reservation, 7889 &lun->per_res[lun->pr_res_idx].res_key, 7890 sizeof(struct scsi_per_res_key)); 7891 } 7892 res->data.scopetype = lun->res_type; 7893 break; 7894 } 7895 case SPRI_RC: //report capabilities 7896 { 7897 struct scsi_per_res_cap *res_cap; 7898 uint16_t type_mask; 7899 7900 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7901 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7902 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7903 type_mask = SPRI_TM_WR_EX_AR | 7904 SPRI_TM_EX_AC_RO | 7905 SPRI_TM_WR_EX_RO | 7906 SPRI_TM_EX_AC | 7907 SPRI_TM_WR_EX | 7908 SPRI_TM_EX_AC_AR; 7909 scsi_ulto2b(type_mask, res_cap->type_mask); 7910 break; 7911 } 7912 case SPRI_RS: { // read full status 7913 struct scsi_per_res_in_full *res_status; 7914 struct scsi_per_res_in_full_desc *res_desc; 7915 struct ctl_port *port; 7916 int i, len; 7917 7918 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7919 7920 /* 7921 * We had to drop the lock to allocate our buffer, which 7922 * leaves time for someone to come in with another 7923 * persistent reservation. (That is unlikely, though, 7924 * since this should be the only persistent reservation 7925 * command active right now.) 7926 */ 7927 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7928 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7929 lun->pr_key_count)){ 7930 mtx_unlock(&lun->lun_lock); 7931 free(ctsio->kern_data_ptr, M_CTL); 7932 printf("%s: reservation length changed, retrying\n", 7933 __func__); 7934 goto retry; 7935 } 7936 7937 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7938 7939 res_desc = &res_status->desc[0]; 7940 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7941 if (!lun->per_res[i].registered) 7942 continue; 7943 7944 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7945 sizeof(res_desc->res_key)); 7946 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7947 (lun->pr_res_idx == i || 7948 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7949 res_desc->flags = SPRI_FULL_R_HOLDER; 7950 res_desc->scopetype = lun->res_type; 7951 } 7952 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7953 res_desc->rel_trgt_port_id); 7954 len = 0; 7955 port = softc->ctl_ports[ 7956 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7957 if (port != NULL) 7958 len = ctl_create_iid(port, 7959 i % CTL_MAX_INIT_PER_PORT, 7960 res_desc->transport_id); 7961 scsi_ulto4b(len, res_desc->additional_length); 7962 res_desc = (struct scsi_per_res_in_full_desc *) 7963 &res_desc->transport_id[len]; 7964 } 7965 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7966 res_status->header.length); 7967 break; 7968 } 7969 default: 7970 /* 7971 * This is a bug, because we just checked for this above, 7972 * and should have returned an error. 7973 */ 7974 panic("Invalid PR type %x", cdb->action); 7975 break; /* NOTREACHED */ 7976 } 7977 mtx_unlock(&lun->lun_lock); 7978 7979 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7980 ctsio->be_move_done = ctl_config_move_done; 7981 7982 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7983 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7984 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7985 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7986 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7987 7988 ctl_datamove((union ctl_io *)ctsio); 7989 7990 return (CTL_RETVAL_COMPLETE); 7991} 7992 7993/* 7994 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7995 * it should return. 7996 */ 7997static int 7998ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7999 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8000 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8001 struct scsi_per_res_out_parms* param) 8002{ 8003 union ctl_ha_msg persis_io; 8004 int retval, i; 8005 int isc_retval; 8006 8007 retval = 0; 8008 8009 mtx_lock(&lun->lun_lock); 8010 if (sa_res_key == 0) { 8011 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8012 /* validate scope and type */ 8013 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8014 SPR_LU_SCOPE) { 8015 mtx_unlock(&lun->lun_lock); 8016 ctl_set_invalid_field(/*ctsio*/ ctsio, 8017 /*sks_valid*/ 1, 8018 /*command*/ 1, 8019 /*field*/ 2, 8020 /*bit_valid*/ 1, 8021 /*bit*/ 4); 8022 ctl_done((union ctl_io *)ctsio); 8023 return (1); 8024 } 8025 8026 if (type>8 || type==2 || type==4 || type==0) { 8027 mtx_unlock(&lun->lun_lock); 8028 ctl_set_invalid_field(/*ctsio*/ ctsio, 8029 /*sks_valid*/ 1, 8030 /*command*/ 1, 8031 /*field*/ 2, 8032 /*bit_valid*/ 1, 8033 /*bit*/ 0); 8034 ctl_done((union ctl_io *)ctsio); 8035 return (1); 8036 } 8037 8038 /* temporarily unregister this nexus */ 8039 lun->per_res[residx].registered = 0; 8040 8041 /* 8042 * Unregister everybody else and build UA for 8043 * them 8044 */ 8045 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8046 if (lun->per_res[i].registered == 0) 8047 continue; 8048 8049 if (!persis_offset 8050 && i <CTL_MAX_INITIATORS) 8051 lun->pending_ua[i] |= 8052 CTL_UA_REG_PREEMPT; 8053 else if (persis_offset 8054 && i >= persis_offset) 8055 lun->pending_ua[i-persis_offset] |= 8056 CTL_UA_REG_PREEMPT; 8057 lun->per_res[i].registered = 0; 8058 memset(&lun->per_res[i].res_key, 0, 8059 sizeof(struct scsi_per_res_key)); 8060 } 8061 lun->per_res[residx].registered = 1; 8062 lun->pr_key_count = 1; 8063 lun->res_type = type; 8064 if (lun->res_type != SPR_TYPE_WR_EX_AR 8065 && lun->res_type != SPR_TYPE_EX_AC_AR) 8066 lun->pr_res_idx = residx; 8067 8068 /* send msg to other side */ 8069 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8070 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8071 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8072 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8073 persis_io.pr.pr_info.res_type = type; 8074 memcpy(persis_io.pr.pr_info.sa_res_key, 8075 param->serv_act_res_key, 8076 sizeof(param->serv_act_res_key)); 8077 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8078 &persis_io, sizeof(persis_io), 0)) > 8079 CTL_HA_STATUS_SUCCESS) { 8080 printf("CTL:Persis Out error returned " 8081 "from ctl_ha_msg_send %d\n", 8082 isc_retval); 8083 } 8084 } else { 8085 /* not all registrants */ 8086 mtx_unlock(&lun->lun_lock); 8087 free(ctsio->kern_data_ptr, M_CTL); 8088 ctl_set_invalid_field(ctsio, 8089 /*sks_valid*/ 1, 8090 /*command*/ 0, 8091 /*field*/ 8, 8092 /*bit_valid*/ 0, 8093 /*bit*/ 0); 8094 ctl_done((union ctl_io *)ctsio); 8095 return (1); 8096 } 8097 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8098 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8099 int found = 0; 8100 8101 if (res_key == sa_res_key) { 8102 /* special case */ 8103 /* 8104 * The spec implies this is not good but doesn't 8105 * say what to do. There are two choices either 8106 * generate a res conflict or check condition 8107 * with illegal field in parameter data. Since 8108 * that is what is done when the sa_res_key is 8109 * zero I'll take that approach since this has 8110 * to do with the sa_res_key. 8111 */ 8112 mtx_unlock(&lun->lun_lock); 8113 free(ctsio->kern_data_ptr, M_CTL); 8114 ctl_set_invalid_field(ctsio, 8115 /*sks_valid*/ 1, 8116 /*command*/ 0, 8117 /*field*/ 8, 8118 /*bit_valid*/ 0, 8119 /*bit*/ 0); 8120 ctl_done((union ctl_io *)ctsio); 8121 return (1); 8122 } 8123 8124 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8125 if (lun->per_res[i].registered 8126 && memcmp(param->serv_act_res_key, 8127 lun->per_res[i].res_key.key, 8128 sizeof(struct scsi_per_res_key)) != 0) 8129 continue; 8130 8131 found = 1; 8132 lun->per_res[i].registered = 0; 8133 memset(&lun->per_res[i].res_key, 0, 8134 sizeof(struct scsi_per_res_key)); 8135 lun->pr_key_count--; 8136 8137 if (!persis_offset && i < CTL_MAX_INITIATORS) 8138 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8139 else if (persis_offset && i >= persis_offset) 8140 lun->pending_ua[i-persis_offset] |= 8141 CTL_UA_REG_PREEMPT; 8142 } 8143 if (!found) { 8144 mtx_unlock(&lun->lun_lock); 8145 free(ctsio->kern_data_ptr, M_CTL); 8146 ctl_set_reservation_conflict(ctsio); 8147 ctl_done((union ctl_io *)ctsio); 8148 return (CTL_RETVAL_COMPLETE); 8149 } 8150 /* send msg to other side */ 8151 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8152 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8153 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8154 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8155 persis_io.pr.pr_info.res_type = type; 8156 memcpy(persis_io.pr.pr_info.sa_res_key, 8157 param->serv_act_res_key, 8158 sizeof(param->serv_act_res_key)); 8159 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8160 &persis_io, sizeof(persis_io), 0)) > 8161 CTL_HA_STATUS_SUCCESS) { 8162 printf("CTL:Persis Out error returned from " 8163 "ctl_ha_msg_send %d\n", isc_retval); 8164 } 8165 } else { 8166 /* Reserved but not all registrants */ 8167 /* sa_res_key is res holder */ 8168 if (memcmp(param->serv_act_res_key, 8169 lun->per_res[lun->pr_res_idx].res_key.key, 8170 sizeof(struct scsi_per_res_key)) == 0) { 8171 /* validate scope and type */ 8172 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8173 SPR_LU_SCOPE) { 8174 mtx_unlock(&lun->lun_lock); 8175 ctl_set_invalid_field(/*ctsio*/ ctsio, 8176 /*sks_valid*/ 1, 8177 /*command*/ 1, 8178 /*field*/ 2, 8179 /*bit_valid*/ 1, 8180 /*bit*/ 4); 8181 ctl_done((union ctl_io *)ctsio); 8182 return (1); 8183 } 8184 8185 if (type>8 || type==2 || type==4 || type==0) { 8186 mtx_unlock(&lun->lun_lock); 8187 ctl_set_invalid_field(/*ctsio*/ ctsio, 8188 /*sks_valid*/ 1, 8189 /*command*/ 1, 8190 /*field*/ 2, 8191 /*bit_valid*/ 1, 8192 /*bit*/ 0); 8193 ctl_done((union ctl_io *)ctsio); 8194 return (1); 8195 } 8196 8197 /* 8198 * Do the following: 8199 * if sa_res_key != res_key remove all 8200 * registrants w/sa_res_key and generate UA 8201 * for these registrants(Registrations 8202 * Preempted) if it wasn't an exclusive 8203 * reservation generate UA(Reservations 8204 * Preempted) for all other registered nexuses 8205 * if the type has changed. Establish the new 8206 * reservation and holder. If res_key and 8207 * sa_res_key are the same do the above 8208 * except don't unregister the res holder. 8209 */ 8210 8211 /* 8212 * Temporarily unregister so it won't get 8213 * removed or UA generated 8214 */ 8215 lun->per_res[residx].registered = 0; 8216 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8217 if (lun->per_res[i].registered == 0) 8218 continue; 8219 8220 if (memcmp(param->serv_act_res_key, 8221 lun->per_res[i].res_key.key, 8222 sizeof(struct scsi_per_res_key)) == 0) { 8223 lun->per_res[i].registered = 0; 8224 memset(&lun->per_res[i].res_key, 8225 0, 8226 sizeof(struct scsi_per_res_key)); 8227 lun->pr_key_count--; 8228 8229 if (!persis_offset 8230 && i < CTL_MAX_INITIATORS) 8231 lun->pending_ua[i] |= 8232 CTL_UA_REG_PREEMPT; 8233 else if (persis_offset 8234 && i >= persis_offset) 8235 lun->pending_ua[i-persis_offset] |= 8236 CTL_UA_REG_PREEMPT; 8237 } else if (type != lun->res_type 8238 && (lun->res_type == SPR_TYPE_WR_EX_RO 8239 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8240 if (!persis_offset 8241 && i < CTL_MAX_INITIATORS) 8242 lun->pending_ua[i] |= 8243 CTL_UA_RES_RELEASE; 8244 else if (persis_offset 8245 && i >= persis_offset) 8246 lun->pending_ua[ 8247 i-persis_offset] |= 8248 CTL_UA_RES_RELEASE; 8249 } 8250 } 8251 lun->per_res[residx].registered = 1; 8252 lun->res_type = type; 8253 if (lun->res_type != SPR_TYPE_WR_EX_AR 8254 && lun->res_type != SPR_TYPE_EX_AC_AR) 8255 lun->pr_res_idx = residx; 8256 else 8257 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8258 8259 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8260 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8261 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8262 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8263 persis_io.pr.pr_info.res_type = type; 8264 memcpy(persis_io.pr.pr_info.sa_res_key, 8265 param->serv_act_res_key, 8266 sizeof(param->serv_act_res_key)); 8267 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8268 &persis_io, sizeof(persis_io), 0)) > 8269 CTL_HA_STATUS_SUCCESS) { 8270 printf("CTL:Persis Out error returned " 8271 "from ctl_ha_msg_send %d\n", 8272 isc_retval); 8273 } 8274 } else { 8275 /* 8276 * sa_res_key is not the res holder just 8277 * remove registrants 8278 */ 8279 int found=0; 8280 8281 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8282 if (memcmp(param->serv_act_res_key, 8283 lun->per_res[i].res_key.key, 8284 sizeof(struct scsi_per_res_key)) != 0) 8285 continue; 8286 8287 found = 1; 8288 lun->per_res[i].registered = 0; 8289 memset(&lun->per_res[i].res_key, 0, 8290 sizeof(struct scsi_per_res_key)); 8291 lun->pr_key_count--; 8292 8293 if (!persis_offset 8294 && i < CTL_MAX_INITIATORS) 8295 lun->pending_ua[i] |= 8296 CTL_UA_REG_PREEMPT; 8297 else if (persis_offset 8298 && i >= persis_offset) 8299 lun->pending_ua[i-persis_offset] |= 8300 CTL_UA_REG_PREEMPT; 8301 } 8302 8303 if (!found) { 8304 mtx_unlock(&lun->lun_lock); 8305 free(ctsio->kern_data_ptr, M_CTL); 8306 ctl_set_reservation_conflict(ctsio); 8307 ctl_done((union ctl_io *)ctsio); 8308 return (1); 8309 } 8310 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8311 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8312 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8313 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8314 persis_io.pr.pr_info.res_type = type; 8315 memcpy(persis_io.pr.pr_info.sa_res_key, 8316 param->serv_act_res_key, 8317 sizeof(param->serv_act_res_key)); 8318 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8319 &persis_io, sizeof(persis_io), 0)) > 8320 CTL_HA_STATUS_SUCCESS) { 8321 printf("CTL:Persis Out error returned " 8322 "from ctl_ha_msg_send %d\n", 8323 isc_retval); 8324 } 8325 } 8326 } 8327 8328 lun->PRGeneration++; 8329 mtx_unlock(&lun->lun_lock); 8330 8331 return (retval); 8332} 8333 8334static void 8335ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8336{ 8337 int i; 8338 8339 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8340 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8341 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8342 msg->pr.pr_info.sa_res_key, 8343 sizeof(struct scsi_per_res_key)) != 0) { 8344 uint64_t sa_res_key; 8345 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8346 8347 if (sa_res_key == 0) { 8348 /* temporarily unregister this nexus */ 8349 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8350 8351 /* 8352 * Unregister everybody else and build UA for 8353 * them 8354 */ 8355 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8356 if (lun->per_res[i].registered == 0) 8357 continue; 8358 8359 if (!persis_offset 8360 && i < CTL_MAX_INITIATORS) 8361 lun->pending_ua[i] |= 8362 CTL_UA_REG_PREEMPT; 8363 else if (persis_offset && i >= persis_offset) 8364 lun->pending_ua[i - persis_offset] |= 8365 CTL_UA_REG_PREEMPT; 8366 lun->per_res[i].registered = 0; 8367 memset(&lun->per_res[i].res_key, 0, 8368 sizeof(struct scsi_per_res_key)); 8369 } 8370 8371 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8372 lun->pr_key_count = 1; 8373 lun->res_type = msg->pr.pr_info.res_type; 8374 if (lun->res_type != SPR_TYPE_WR_EX_AR 8375 && lun->res_type != SPR_TYPE_EX_AC_AR) 8376 lun->pr_res_idx = msg->pr.pr_info.residx; 8377 } else { 8378 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8379 if (memcmp(msg->pr.pr_info.sa_res_key, 8380 lun->per_res[i].res_key.key, 8381 sizeof(struct scsi_per_res_key)) != 0) 8382 continue; 8383 8384 lun->per_res[i].registered = 0; 8385 memset(&lun->per_res[i].res_key, 0, 8386 sizeof(struct scsi_per_res_key)); 8387 lun->pr_key_count--; 8388 8389 if (!persis_offset 8390 && i < persis_offset) 8391 lun->pending_ua[i] |= 8392 CTL_UA_REG_PREEMPT; 8393 else if (persis_offset 8394 && i >= persis_offset) 8395 lun->pending_ua[i - persis_offset] |= 8396 CTL_UA_REG_PREEMPT; 8397 } 8398 } 8399 } else { 8400 /* 8401 * Temporarily unregister so it won't get removed 8402 * or UA generated 8403 */ 8404 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8405 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8406 if (lun->per_res[i].registered == 0) 8407 continue; 8408 8409 if (memcmp(msg->pr.pr_info.sa_res_key, 8410 lun->per_res[i].res_key.key, 8411 sizeof(struct scsi_per_res_key)) == 0) { 8412 lun->per_res[i].registered = 0; 8413 memset(&lun->per_res[i].res_key, 0, 8414 sizeof(struct scsi_per_res_key)); 8415 lun->pr_key_count--; 8416 if (!persis_offset 8417 && i < CTL_MAX_INITIATORS) 8418 lun->pending_ua[i] |= 8419 CTL_UA_REG_PREEMPT; 8420 else if (persis_offset 8421 && i >= persis_offset) 8422 lun->pending_ua[i - persis_offset] |= 8423 CTL_UA_REG_PREEMPT; 8424 } else if (msg->pr.pr_info.res_type != lun->res_type 8425 && (lun->res_type == SPR_TYPE_WR_EX_RO 8426 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8427 if (!persis_offset 8428 && i < persis_offset) 8429 lun->pending_ua[i] |= 8430 CTL_UA_RES_RELEASE; 8431 else if (persis_offset 8432 && i >= persis_offset) 8433 lun->pending_ua[i - persis_offset] |= 8434 CTL_UA_RES_RELEASE; 8435 } 8436 } 8437 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8438 lun->res_type = msg->pr.pr_info.res_type; 8439 if (lun->res_type != SPR_TYPE_WR_EX_AR 8440 && lun->res_type != SPR_TYPE_EX_AC_AR) 8441 lun->pr_res_idx = msg->pr.pr_info.residx; 8442 else 8443 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8444 } 8445 lun->PRGeneration++; 8446 8447} 8448 8449 8450int 8451ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8452{ 8453 int retval; 8454 int isc_retval; 8455 u_int32_t param_len; 8456 struct scsi_per_res_out *cdb; 8457 struct ctl_lun *lun; 8458 struct scsi_per_res_out_parms* param; 8459 struct ctl_softc *softc; 8460 uint32_t residx; 8461 uint64_t res_key, sa_res_key; 8462 uint8_t type; 8463 union ctl_ha_msg persis_io; 8464 int i; 8465 8466 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8467 8468 retval = CTL_RETVAL_COMPLETE; 8469 8470 softc = control_softc; 8471 8472 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8473 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8474 8475 /* 8476 * We only support whole-LUN scope. The scope & type are ignored for 8477 * register, register and ignore existing key and clear. 8478 * We sometimes ignore scope and type on preempts too!! 8479 * Verify reservation type here as well. 8480 */ 8481 type = cdb->scope_type & SPR_TYPE_MASK; 8482 if ((cdb->action == SPRO_RESERVE) 8483 || (cdb->action == SPRO_RELEASE)) { 8484 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8485 ctl_set_invalid_field(/*ctsio*/ ctsio, 8486 /*sks_valid*/ 1, 8487 /*command*/ 1, 8488 /*field*/ 2, 8489 /*bit_valid*/ 1, 8490 /*bit*/ 4); 8491 ctl_done((union ctl_io *)ctsio); 8492 return (CTL_RETVAL_COMPLETE); 8493 } 8494 8495 if (type>8 || type==2 || type==4 || type==0) { 8496 ctl_set_invalid_field(/*ctsio*/ ctsio, 8497 /*sks_valid*/ 1, 8498 /*command*/ 1, 8499 /*field*/ 2, 8500 /*bit_valid*/ 1, 8501 /*bit*/ 0); 8502 ctl_done((union ctl_io *)ctsio); 8503 return (CTL_RETVAL_COMPLETE); 8504 } 8505 } 8506 8507 param_len = scsi_4btoul(cdb->length); 8508 8509 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8510 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8511 ctsio->kern_data_len = param_len; 8512 ctsio->kern_total_len = param_len; 8513 ctsio->kern_data_resid = 0; 8514 ctsio->kern_rel_offset = 0; 8515 ctsio->kern_sg_entries = 0; 8516 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8517 ctsio->be_move_done = ctl_config_move_done; 8518 ctl_datamove((union ctl_io *)ctsio); 8519 8520 return (CTL_RETVAL_COMPLETE); 8521 } 8522 8523 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8524 8525 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8526 res_key = scsi_8btou64(param->res_key.key); 8527 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8528 8529 /* 8530 * Validate the reservation key here except for SPRO_REG_IGNO 8531 * This must be done for all other service actions 8532 */ 8533 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8534 mtx_lock(&lun->lun_lock); 8535 if (lun->per_res[residx].registered) { 8536 if (memcmp(param->res_key.key, 8537 lun->per_res[residx].res_key.key, 8538 ctl_min(sizeof(param->res_key), 8539 sizeof(lun->per_res[residx].res_key))) != 0) { 8540 /* 8541 * The current key passed in doesn't match 8542 * the one the initiator previously 8543 * registered. 8544 */ 8545 mtx_unlock(&lun->lun_lock); 8546 free(ctsio->kern_data_ptr, M_CTL); 8547 ctl_set_reservation_conflict(ctsio); 8548 ctl_done((union ctl_io *)ctsio); 8549 return (CTL_RETVAL_COMPLETE); 8550 } 8551 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8552 /* 8553 * We are not registered 8554 */ 8555 mtx_unlock(&lun->lun_lock); 8556 free(ctsio->kern_data_ptr, M_CTL); 8557 ctl_set_reservation_conflict(ctsio); 8558 ctl_done((union ctl_io *)ctsio); 8559 return (CTL_RETVAL_COMPLETE); 8560 } else if (res_key != 0) { 8561 /* 8562 * We are not registered and trying to register but 8563 * the register key isn't zero. 8564 */ 8565 mtx_unlock(&lun->lun_lock); 8566 free(ctsio->kern_data_ptr, M_CTL); 8567 ctl_set_reservation_conflict(ctsio); 8568 ctl_done((union ctl_io *)ctsio); 8569 return (CTL_RETVAL_COMPLETE); 8570 } 8571 mtx_unlock(&lun->lun_lock); 8572 } 8573 8574 switch (cdb->action & SPRO_ACTION_MASK) { 8575 case SPRO_REGISTER: 8576 case SPRO_REG_IGNO: { 8577 8578#if 0 8579 printf("Registration received\n"); 8580#endif 8581 8582 /* 8583 * We don't support any of these options, as we report in 8584 * the read capabilities request (see 8585 * ctl_persistent_reserve_in(), above). 8586 */ 8587 if ((param->flags & SPR_SPEC_I_PT) 8588 || (param->flags & SPR_ALL_TG_PT) 8589 || (param->flags & SPR_APTPL)) { 8590 int bit_ptr; 8591 8592 if (param->flags & SPR_APTPL) 8593 bit_ptr = 0; 8594 else if (param->flags & SPR_ALL_TG_PT) 8595 bit_ptr = 2; 8596 else /* SPR_SPEC_I_PT */ 8597 bit_ptr = 3; 8598 8599 free(ctsio->kern_data_ptr, M_CTL); 8600 ctl_set_invalid_field(ctsio, 8601 /*sks_valid*/ 1, 8602 /*command*/ 0, 8603 /*field*/ 20, 8604 /*bit_valid*/ 1, 8605 /*bit*/ bit_ptr); 8606 ctl_done((union ctl_io *)ctsio); 8607 return (CTL_RETVAL_COMPLETE); 8608 } 8609 8610 mtx_lock(&lun->lun_lock); 8611 8612 /* 8613 * The initiator wants to clear the 8614 * key/unregister. 8615 */ 8616 if (sa_res_key == 0) { 8617 if ((res_key == 0 8618 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8619 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8620 && !lun->per_res[residx].registered)) { 8621 mtx_unlock(&lun->lun_lock); 8622 goto done; 8623 } 8624 8625 lun->per_res[residx].registered = 0; 8626 memset(&lun->per_res[residx].res_key, 8627 0, sizeof(lun->per_res[residx].res_key)); 8628 lun->pr_key_count--; 8629 8630 if (residx == lun->pr_res_idx) { 8631 lun->flags &= ~CTL_LUN_PR_RESERVED; 8632 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8633 8634 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8635 || lun->res_type == SPR_TYPE_EX_AC_RO) 8636 && lun->pr_key_count) { 8637 /* 8638 * If the reservation is a registrants 8639 * only type we need to generate a UA 8640 * for other registered inits. The 8641 * sense code should be RESERVATIONS 8642 * RELEASED 8643 */ 8644 8645 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8646 if (lun->per_res[ 8647 i+persis_offset].registered 8648 == 0) 8649 continue; 8650 lun->pending_ua[i] |= 8651 CTL_UA_RES_RELEASE; 8652 } 8653 } 8654 lun->res_type = 0; 8655 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8656 if (lun->pr_key_count==0) { 8657 lun->flags &= ~CTL_LUN_PR_RESERVED; 8658 lun->res_type = 0; 8659 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8660 } 8661 } 8662 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8663 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8664 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8665 persis_io.pr.pr_info.residx = residx; 8666 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8667 &persis_io, sizeof(persis_io), 0 )) > 8668 CTL_HA_STATUS_SUCCESS) { 8669 printf("CTL:Persis Out error returned from " 8670 "ctl_ha_msg_send %d\n", isc_retval); 8671 } 8672 } else /* sa_res_key != 0 */ { 8673 8674 /* 8675 * If we aren't registered currently then increment 8676 * the key count and set the registered flag. 8677 */ 8678 if (!lun->per_res[residx].registered) { 8679 lun->pr_key_count++; 8680 lun->per_res[residx].registered = 1; 8681 } 8682 8683 memcpy(&lun->per_res[residx].res_key, 8684 param->serv_act_res_key, 8685 ctl_min(sizeof(param->serv_act_res_key), 8686 sizeof(lun->per_res[residx].res_key))); 8687 8688 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8689 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8690 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8691 persis_io.pr.pr_info.residx = residx; 8692 memcpy(persis_io.pr.pr_info.sa_res_key, 8693 param->serv_act_res_key, 8694 sizeof(param->serv_act_res_key)); 8695 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8696 &persis_io, sizeof(persis_io), 0)) > 8697 CTL_HA_STATUS_SUCCESS) { 8698 printf("CTL:Persis Out error returned from " 8699 "ctl_ha_msg_send %d\n", isc_retval); 8700 } 8701 } 8702 lun->PRGeneration++; 8703 mtx_unlock(&lun->lun_lock); 8704 8705 break; 8706 } 8707 case SPRO_RESERVE: 8708#if 0 8709 printf("Reserve executed type %d\n", type); 8710#endif 8711 mtx_lock(&lun->lun_lock); 8712 if (lun->flags & CTL_LUN_PR_RESERVED) { 8713 /* 8714 * if this isn't the reservation holder and it's 8715 * not a "all registrants" type or if the type is 8716 * different then we have a conflict 8717 */ 8718 if ((lun->pr_res_idx != residx 8719 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8720 || lun->res_type != type) { 8721 mtx_unlock(&lun->lun_lock); 8722 free(ctsio->kern_data_ptr, M_CTL); 8723 ctl_set_reservation_conflict(ctsio); 8724 ctl_done((union ctl_io *)ctsio); 8725 return (CTL_RETVAL_COMPLETE); 8726 } 8727 mtx_unlock(&lun->lun_lock); 8728 } else /* create a reservation */ { 8729 /* 8730 * If it's not an "all registrants" type record 8731 * reservation holder 8732 */ 8733 if (type != SPR_TYPE_WR_EX_AR 8734 && type != SPR_TYPE_EX_AC_AR) 8735 lun->pr_res_idx = residx; /* Res holder */ 8736 else 8737 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8738 8739 lun->flags |= CTL_LUN_PR_RESERVED; 8740 lun->res_type = type; 8741 8742 mtx_unlock(&lun->lun_lock); 8743 8744 /* send msg to other side */ 8745 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8746 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8747 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8748 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8749 persis_io.pr.pr_info.res_type = type; 8750 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8751 &persis_io, sizeof(persis_io), 0)) > 8752 CTL_HA_STATUS_SUCCESS) { 8753 printf("CTL:Persis Out error returned from " 8754 "ctl_ha_msg_send %d\n", isc_retval); 8755 } 8756 } 8757 break; 8758 8759 case SPRO_RELEASE: 8760 mtx_lock(&lun->lun_lock); 8761 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8762 /* No reservation exists return good status */ 8763 mtx_unlock(&lun->lun_lock); 8764 goto done; 8765 } 8766 /* 8767 * Is this nexus a reservation holder? 8768 */ 8769 if (lun->pr_res_idx != residx 8770 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8771 /* 8772 * not a res holder return good status but 8773 * do nothing 8774 */ 8775 mtx_unlock(&lun->lun_lock); 8776 goto done; 8777 } 8778 8779 if (lun->res_type != type) { 8780 mtx_unlock(&lun->lun_lock); 8781 free(ctsio->kern_data_ptr, M_CTL); 8782 ctl_set_illegal_pr_release(ctsio); 8783 ctl_done((union ctl_io *)ctsio); 8784 return (CTL_RETVAL_COMPLETE); 8785 } 8786 8787 /* okay to release */ 8788 lun->flags &= ~CTL_LUN_PR_RESERVED; 8789 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8790 lun->res_type = 0; 8791 8792 /* 8793 * if this isn't an exclusive access 8794 * res generate UA for all other 8795 * registrants. 8796 */ 8797 if (type != SPR_TYPE_EX_AC 8798 && type != SPR_TYPE_WR_EX) { 8799 /* 8800 * temporarily unregister so we don't generate UA 8801 */ 8802 lun->per_res[residx].registered = 0; 8803 8804 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8805 if (lun->per_res[i+persis_offset].registered 8806 == 0) 8807 continue; 8808 lun->pending_ua[i] |= 8809 CTL_UA_RES_RELEASE; 8810 } 8811 8812 lun->per_res[residx].registered = 1; 8813 } 8814 mtx_unlock(&lun->lun_lock); 8815 /* Send msg to other side */ 8816 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8817 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8818 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8819 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8820 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8821 printf("CTL:Persis Out error returned from " 8822 "ctl_ha_msg_send %d\n", isc_retval); 8823 } 8824 break; 8825 8826 case SPRO_CLEAR: 8827 /* send msg to other side */ 8828 8829 mtx_lock(&lun->lun_lock); 8830 lun->flags &= ~CTL_LUN_PR_RESERVED; 8831 lun->res_type = 0; 8832 lun->pr_key_count = 0; 8833 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8834 8835 8836 memset(&lun->per_res[residx].res_key, 8837 0, sizeof(lun->per_res[residx].res_key)); 8838 lun->per_res[residx].registered = 0; 8839 8840 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8841 if (lun->per_res[i].registered) { 8842 if (!persis_offset && i < CTL_MAX_INITIATORS) 8843 lun->pending_ua[i] |= 8844 CTL_UA_RES_PREEMPT; 8845 else if (persis_offset && i >= persis_offset) 8846 lun->pending_ua[i-persis_offset] |= 8847 CTL_UA_RES_PREEMPT; 8848 8849 memset(&lun->per_res[i].res_key, 8850 0, sizeof(struct scsi_per_res_key)); 8851 lun->per_res[i].registered = 0; 8852 } 8853 lun->PRGeneration++; 8854 mtx_unlock(&lun->lun_lock); 8855 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8856 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8857 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8858 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8859 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8860 printf("CTL:Persis Out error returned from " 8861 "ctl_ha_msg_send %d\n", isc_retval); 8862 } 8863 break; 8864 8865 case SPRO_PREEMPT: { 8866 int nretval; 8867 8868 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8869 residx, ctsio, cdb, param); 8870 if (nretval != 0) 8871 return (CTL_RETVAL_COMPLETE); 8872 break; 8873 } 8874 default: 8875 panic("Invalid PR type %x", cdb->action); 8876 } 8877 8878done: 8879 free(ctsio->kern_data_ptr, M_CTL); 8880 ctl_set_success(ctsio); 8881 ctl_done((union ctl_io *)ctsio); 8882 8883 return (retval); 8884} 8885 8886/* 8887 * This routine is for handling a message from the other SC pertaining to 8888 * persistent reserve out. All the error checking will have been done 8889 * so only perorming the action need be done here to keep the two 8890 * in sync. 8891 */ 8892static void 8893ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8894{ 8895 struct ctl_lun *lun; 8896 struct ctl_softc *softc; 8897 int i; 8898 uint32_t targ_lun; 8899 8900 softc = control_softc; 8901 8902 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8903 lun = softc->ctl_luns[targ_lun]; 8904 mtx_lock(&lun->lun_lock); 8905 switch(msg->pr.pr_info.action) { 8906 case CTL_PR_REG_KEY: 8907 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8908 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8909 lun->pr_key_count++; 8910 } 8911 lun->PRGeneration++; 8912 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8913 msg->pr.pr_info.sa_res_key, 8914 sizeof(struct scsi_per_res_key)); 8915 break; 8916 8917 case CTL_PR_UNREG_KEY: 8918 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8919 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8920 0, sizeof(struct scsi_per_res_key)); 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->per_res[i+ 8942 persis_offset].registered == 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->per_res[i+persis_offset].registered) 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->per_res[i].registered == 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 memset(&lun->per_res[i].res_key, 0, 9005 sizeof(struct scsi_per_res_key)); 9006 lun->per_res[i].registered = 0; 9007 } 9008 lun->PRGeneration++; 9009 break; 9010 } 9011 9012 mtx_unlock(&lun->lun_lock); 9013} 9014 9015int 9016ctl_read_write(struct ctl_scsiio *ctsio) 9017{ 9018 struct ctl_lun *lun; 9019 struct ctl_lba_len_flags *lbalen; 9020 uint64_t lba; 9021 uint32_t num_blocks; 9022 int flags, retval; 9023 int isread; 9024 9025 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9026 9027 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9028 9029 flags = 0; 9030 retval = CTL_RETVAL_COMPLETE; 9031 9032 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9033 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9034 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9035 uint32_t residx; 9036 9037 /* 9038 * XXX KDM need a lock here. 9039 */ 9040 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9041 if ((lun->res_type == SPR_TYPE_EX_AC 9042 && residx != lun->pr_res_idx) 9043 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9044 || lun->res_type == SPR_TYPE_EX_AC_AR) 9045 && !lun->per_res[residx].registered)) { 9046 ctl_set_reservation_conflict(ctsio); 9047 ctl_done((union ctl_io *)ctsio); 9048 return (CTL_RETVAL_COMPLETE); 9049 } 9050 } 9051 9052 switch (ctsio->cdb[0]) { 9053 case READ_6: 9054 case WRITE_6: { 9055 struct scsi_rw_6 *cdb; 9056 9057 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9058 9059 lba = scsi_3btoul(cdb->addr); 9060 /* only 5 bits are valid in the most significant address byte */ 9061 lba &= 0x1fffff; 9062 num_blocks = cdb->length; 9063 /* 9064 * This is correct according to SBC-2. 9065 */ 9066 if (num_blocks == 0) 9067 num_blocks = 256; 9068 break; 9069 } 9070 case READ_10: 9071 case WRITE_10: { 9072 struct scsi_rw_10 *cdb; 9073 9074 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9075 if (cdb->byte2 & SRW10_FUA) 9076 flags |= CTL_LLF_FUA; 9077 if (cdb->byte2 & SRW10_DPO) 9078 flags |= CTL_LLF_DPO; 9079 lba = scsi_4btoul(cdb->addr); 9080 num_blocks = scsi_2btoul(cdb->length); 9081 break; 9082 } 9083 case WRITE_VERIFY_10: { 9084 struct scsi_write_verify_10 *cdb; 9085 9086 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9087 flags |= CTL_LLF_FUA; 9088 if (cdb->byte2 & SWV_DPO) 9089 flags |= CTL_LLF_DPO; 9090 lba = scsi_4btoul(cdb->addr); 9091 num_blocks = scsi_2btoul(cdb->length); 9092 break; 9093 } 9094 case READ_12: 9095 case WRITE_12: { 9096 struct scsi_rw_12 *cdb; 9097 9098 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9099 if (cdb->byte2 & SRW12_FUA) 9100 flags |= CTL_LLF_FUA; 9101 if (cdb->byte2 & SRW12_DPO) 9102 flags |= CTL_LLF_DPO; 9103 lba = scsi_4btoul(cdb->addr); 9104 num_blocks = scsi_4btoul(cdb->length); 9105 break; 9106 } 9107 case WRITE_VERIFY_12: { 9108 struct scsi_write_verify_12 *cdb; 9109 9110 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9111 flags |= CTL_LLF_FUA; 9112 if (cdb->byte2 & SWV_DPO) 9113 flags |= CTL_LLF_DPO; 9114 lba = scsi_4btoul(cdb->addr); 9115 num_blocks = scsi_4btoul(cdb->length); 9116 break; 9117 } 9118 case READ_16: 9119 case WRITE_16: { 9120 struct scsi_rw_16 *cdb; 9121 9122 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9123 if (cdb->byte2 & SRW12_FUA) 9124 flags |= CTL_LLF_FUA; 9125 if (cdb->byte2 & SRW12_DPO) 9126 flags |= CTL_LLF_DPO; 9127 lba = scsi_8btou64(cdb->addr); 9128 num_blocks = scsi_4btoul(cdb->length); 9129 break; 9130 } 9131 case WRITE_VERIFY_16: { 9132 struct scsi_write_verify_16 *cdb; 9133 9134 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9135 flags |= CTL_LLF_FUA; 9136 if (cdb->byte2 & SWV_DPO) 9137 flags |= CTL_LLF_DPO; 9138 lba = scsi_8btou64(cdb->addr); 9139 num_blocks = scsi_4btoul(cdb->length); 9140 break; 9141 } 9142 default: 9143 /* 9144 * We got a command we don't support. This shouldn't 9145 * happen, commands should be filtered out above us. 9146 */ 9147 ctl_set_invalid_opcode(ctsio); 9148 ctl_done((union ctl_io *)ctsio); 9149 9150 return (CTL_RETVAL_COMPLETE); 9151 break; /* NOTREACHED */ 9152 } 9153 9154 /* 9155 * The first check is to make sure we're in bounds, the second 9156 * check is to catch wrap-around problems. If the lba + num blocks 9157 * is less than the lba, then we've wrapped around and the block 9158 * range is invalid anyway. 9159 */ 9160 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9161 || ((lba + num_blocks) < lba)) { 9162 ctl_set_lba_out_of_range(ctsio); 9163 ctl_done((union ctl_io *)ctsio); 9164 return (CTL_RETVAL_COMPLETE); 9165 } 9166 9167 /* 9168 * According to SBC-3, a transfer length of 0 is not an error. 9169 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9170 * translates to 256 blocks for those commands. 9171 */ 9172 if (num_blocks == 0) { 9173 ctl_set_success(ctsio); 9174 ctl_done((union ctl_io *)ctsio); 9175 return (CTL_RETVAL_COMPLETE); 9176 } 9177 9178 /* Set FUA and/or DPO if caches are disabled. */ 9179 if (isread) { 9180 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9181 SCP_RCD) != 0) 9182 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9183 } else { 9184 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9185 SCP_WCE) == 0) 9186 flags |= CTL_LLF_FUA; 9187 } 9188 9189 lbalen = (struct ctl_lba_len_flags *) 9190 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9191 lbalen->lba = lba; 9192 lbalen->len = num_blocks; 9193 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9194 9195 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9196 ctsio->kern_rel_offset = 0; 9197 9198 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9199 9200 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9201 9202 return (retval); 9203} 9204 9205static int 9206ctl_cnw_cont(union ctl_io *io) 9207{ 9208 struct ctl_scsiio *ctsio; 9209 struct ctl_lun *lun; 9210 struct ctl_lba_len_flags *lbalen; 9211 int retval; 9212 9213 ctsio = &io->scsiio; 9214 ctsio->io_hdr.status = CTL_STATUS_NONE; 9215 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9216 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9217 lbalen = (struct ctl_lba_len_flags *) 9218 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9219 lbalen->flags &= ~CTL_LLF_COMPARE; 9220 lbalen->flags |= CTL_LLF_WRITE; 9221 9222 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9223 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9224 return (retval); 9225} 9226 9227int 9228ctl_cnw(struct ctl_scsiio *ctsio) 9229{ 9230 struct ctl_lun *lun; 9231 struct ctl_lba_len_flags *lbalen; 9232 uint64_t lba; 9233 uint32_t num_blocks; 9234 int flags, retval; 9235 9236 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9237 9238 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9239 9240 flags = 0; 9241 retval = CTL_RETVAL_COMPLETE; 9242 9243 switch (ctsio->cdb[0]) { 9244 case COMPARE_AND_WRITE: { 9245 struct scsi_compare_and_write *cdb; 9246 9247 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9248 if (cdb->byte2 & SRW10_FUA) 9249 flags |= CTL_LLF_FUA; 9250 if (cdb->byte2 & SRW10_DPO) 9251 flags |= CTL_LLF_DPO; 9252 lba = scsi_8btou64(cdb->addr); 9253 num_blocks = cdb->length; 9254 break; 9255 } 9256 default: 9257 /* 9258 * We got a command we don't support. This shouldn't 9259 * happen, commands should be filtered out above us. 9260 */ 9261 ctl_set_invalid_opcode(ctsio); 9262 ctl_done((union ctl_io *)ctsio); 9263 9264 return (CTL_RETVAL_COMPLETE); 9265 break; /* NOTREACHED */ 9266 } 9267 9268 /* 9269 * The first check is to make sure we're in bounds, the second 9270 * check is to catch wrap-around problems. If the lba + num blocks 9271 * is less than the lba, then we've wrapped around and the block 9272 * range is invalid anyway. 9273 */ 9274 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9275 || ((lba + num_blocks) < lba)) { 9276 ctl_set_lba_out_of_range(ctsio); 9277 ctl_done((union ctl_io *)ctsio); 9278 return (CTL_RETVAL_COMPLETE); 9279 } 9280 9281 /* 9282 * According to SBC-3, a transfer length of 0 is not an error. 9283 */ 9284 if (num_blocks == 0) { 9285 ctl_set_success(ctsio); 9286 ctl_done((union ctl_io *)ctsio); 9287 return (CTL_RETVAL_COMPLETE); 9288 } 9289 9290 /* Set FUA if write cache is disabled. */ 9291 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9292 SCP_WCE) == 0) 9293 flags |= CTL_LLF_FUA; 9294 9295 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9296 ctsio->kern_rel_offset = 0; 9297 9298 /* 9299 * Set the IO_CONT flag, so that if this I/O gets passed to 9300 * ctl_data_submit_done(), it'll get passed back to 9301 * ctl_ctl_cnw_cont() for further processing. 9302 */ 9303 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9304 ctsio->io_cont = ctl_cnw_cont; 9305 9306 lbalen = (struct ctl_lba_len_flags *) 9307 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9308 lbalen->lba = lba; 9309 lbalen->len = num_blocks; 9310 lbalen->flags = CTL_LLF_COMPARE | flags; 9311 9312 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9313 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9314 return (retval); 9315} 9316 9317int 9318ctl_verify(struct ctl_scsiio *ctsio) 9319{ 9320 struct ctl_lun *lun; 9321 struct ctl_lba_len_flags *lbalen; 9322 uint64_t lba; 9323 uint32_t num_blocks; 9324 int bytchk, flags; 9325 int retval; 9326 9327 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9328 9329 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9330 9331 bytchk = 0; 9332 flags = CTL_LLF_FUA; 9333 retval = CTL_RETVAL_COMPLETE; 9334 9335 switch (ctsio->cdb[0]) { 9336 case VERIFY_10: { 9337 struct scsi_verify_10 *cdb; 9338 9339 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9340 if (cdb->byte2 & SVFY_BYTCHK) 9341 bytchk = 1; 9342 if (cdb->byte2 & SVFY_DPO) 9343 flags |= CTL_LLF_DPO; 9344 lba = scsi_4btoul(cdb->addr); 9345 num_blocks = scsi_2btoul(cdb->length); 9346 break; 9347 } 9348 case VERIFY_12: { 9349 struct scsi_verify_12 *cdb; 9350 9351 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9352 if (cdb->byte2 & SVFY_BYTCHK) 9353 bytchk = 1; 9354 if (cdb->byte2 & SVFY_DPO) 9355 flags |= CTL_LLF_DPO; 9356 lba = scsi_4btoul(cdb->addr); 9357 num_blocks = scsi_4btoul(cdb->length); 9358 break; 9359 } 9360 case VERIFY_16: { 9361 struct scsi_rw_16 *cdb; 9362 9363 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9364 if (cdb->byte2 & SVFY_BYTCHK) 9365 bytchk = 1; 9366 if (cdb->byte2 & SVFY_DPO) 9367 flags |= CTL_LLF_DPO; 9368 lba = scsi_8btou64(cdb->addr); 9369 num_blocks = scsi_4btoul(cdb->length); 9370 break; 9371 } 9372 default: 9373 /* 9374 * We got a command we don't support. This shouldn't 9375 * happen, commands should be filtered out above us. 9376 */ 9377 ctl_set_invalid_opcode(ctsio); 9378 ctl_done((union ctl_io *)ctsio); 9379 return (CTL_RETVAL_COMPLETE); 9380 } 9381 9382 /* 9383 * The first check is to make sure we're in bounds, the second 9384 * check is to catch wrap-around problems. If the lba + num blocks 9385 * is less than the lba, then we've wrapped around and the block 9386 * range is invalid anyway. 9387 */ 9388 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9389 || ((lba + num_blocks) < lba)) { 9390 ctl_set_lba_out_of_range(ctsio); 9391 ctl_done((union ctl_io *)ctsio); 9392 return (CTL_RETVAL_COMPLETE); 9393 } 9394 9395 /* 9396 * According to SBC-3, a transfer length of 0 is not an error. 9397 */ 9398 if (num_blocks == 0) { 9399 ctl_set_success(ctsio); 9400 ctl_done((union ctl_io *)ctsio); 9401 return (CTL_RETVAL_COMPLETE); 9402 } 9403 9404 lbalen = (struct ctl_lba_len_flags *) 9405 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9406 lbalen->lba = lba; 9407 lbalen->len = num_blocks; 9408 if (bytchk) { 9409 lbalen->flags = CTL_LLF_COMPARE | flags; 9410 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9411 } else { 9412 lbalen->flags = CTL_LLF_VERIFY | flags; 9413 ctsio->kern_total_len = 0; 9414 } 9415 ctsio->kern_rel_offset = 0; 9416 9417 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9418 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9419 return (retval); 9420} 9421 9422int 9423ctl_report_luns(struct ctl_scsiio *ctsio) 9424{ 9425 struct scsi_report_luns *cdb; 9426 struct scsi_report_luns_data *lun_data; 9427 struct ctl_lun *lun, *request_lun; 9428 int num_luns, retval; 9429 uint32_t alloc_len, lun_datalen; 9430 int num_filled, well_known; 9431 uint32_t initidx, targ_lun_id, lun_id; 9432 9433 retval = CTL_RETVAL_COMPLETE; 9434 well_known = 0; 9435 9436 cdb = (struct scsi_report_luns *)ctsio->cdb; 9437 9438 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9439 9440 mtx_lock(&control_softc->ctl_lock); 9441 num_luns = control_softc->num_luns; 9442 mtx_unlock(&control_softc->ctl_lock); 9443 9444 switch (cdb->select_report) { 9445 case RPL_REPORT_DEFAULT: 9446 case RPL_REPORT_ALL: 9447 break; 9448 case RPL_REPORT_WELLKNOWN: 9449 well_known = 1; 9450 num_luns = 0; 9451 break; 9452 default: 9453 ctl_set_invalid_field(ctsio, 9454 /*sks_valid*/ 1, 9455 /*command*/ 1, 9456 /*field*/ 2, 9457 /*bit_valid*/ 0, 9458 /*bit*/ 0); 9459 ctl_done((union ctl_io *)ctsio); 9460 return (retval); 9461 break; /* NOTREACHED */ 9462 } 9463 9464 alloc_len = scsi_4btoul(cdb->length); 9465 /* 9466 * The initiator has to allocate at least 16 bytes for this request, 9467 * so he can at least get the header and the first LUN. Otherwise 9468 * we reject the request (per SPC-3 rev 14, section 6.21). 9469 */ 9470 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9471 sizeof(struct scsi_report_luns_lundata))) { 9472 ctl_set_invalid_field(ctsio, 9473 /*sks_valid*/ 1, 9474 /*command*/ 1, 9475 /*field*/ 6, 9476 /*bit_valid*/ 0, 9477 /*bit*/ 0); 9478 ctl_done((union ctl_io *)ctsio); 9479 return (retval); 9480 } 9481 9482 request_lun = (struct ctl_lun *) 9483 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9484 9485 lun_datalen = sizeof(*lun_data) + 9486 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9487 9488 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9489 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9490 ctsio->kern_sg_entries = 0; 9491 9492 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9493 9494 mtx_lock(&control_softc->ctl_lock); 9495 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9496 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9497 if (lun_id >= CTL_MAX_LUNS) 9498 continue; 9499 lun = control_softc->ctl_luns[lun_id]; 9500 if (lun == NULL) 9501 continue; 9502 9503 if (targ_lun_id <= 0xff) { 9504 /* 9505 * Peripheral addressing method, bus number 0. 9506 */ 9507 lun_data->luns[num_filled].lundata[0] = 9508 RPL_LUNDATA_ATYP_PERIPH; 9509 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9510 num_filled++; 9511 } else if (targ_lun_id <= 0x3fff) { 9512 /* 9513 * Flat addressing method. 9514 */ 9515 lun_data->luns[num_filled].lundata[0] = 9516 RPL_LUNDATA_ATYP_FLAT | 9517 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9518#ifdef OLDCTLHEADERS 9519 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9520 (targ_lun_id & SRLD_BUS_LUN_MASK); 9521#endif 9522 lun_data->luns[num_filled].lundata[1] = 9523#ifdef OLDCTLHEADERS 9524 targ_lun_id >> SRLD_BUS_LUN_BITS; 9525#endif 9526 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9527 num_filled++; 9528 } else { 9529 printf("ctl_report_luns: bogus LUN number %jd, " 9530 "skipping\n", (intmax_t)targ_lun_id); 9531 } 9532 /* 9533 * According to SPC-3, rev 14 section 6.21: 9534 * 9535 * "The execution of a REPORT LUNS command to any valid and 9536 * installed logical unit shall clear the REPORTED LUNS DATA 9537 * HAS CHANGED unit attention condition for all logical 9538 * units of that target with respect to the requesting 9539 * initiator. A valid and installed logical unit is one 9540 * having a PERIPHERAL QUALIFIER of 000b in the standard 9541 * INQUIRY data (see 6.4.2)." 9542 * 9543 * If request_lun is NULL, the LUN this report luns command 9544 * was issued to is either disabled or doesn't exist. In that 9545 * case, we shouldn't clear any pending lun change unit 9546 * attention. 9547 */ 9548 if (request_lun != NULL) { 9549 mtx_lock(&lun->lun_lock); 9550 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9551 mtx_unlock(&lun->lun_lock); 9552 } 9553 } 9554 mtx_unlock(&control_softc->ctl_lock); 9555 9556 /* 9557 * It's quite possible that we've returned fewer LUNs than we allocated 9558 * space for. Trim it. 9559 */ 9560 lun_datalen = sizeof(*lun_data) + 9561 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9562 9563 if (lun_datalen < alloc_len) { 9564 ctsio->residual = alloc_len - lun_datalen; 9565 ctsio->kern_data_len = lun_datalen; 9566 ctsio->kern_total_len = lun_datalen; 9567 } else { 9568 ctsio->residual = 0; 9569 ctsio->kern_data_len = alloc_len; 9570 ctsio->kern_total_len = alloc_len; 9571 } 9572 ctsio->kern_data_resid = 0; 9573 ctsio->kern_rel_offset = 0; 9574 ctsio->kern_sg_entries = 0; 9575 9576 /* 9577 * We set this to the actual data length, regardless of how much 9578 * space we actually have to return results. If the user looks at 9579 * this value, he'll know whether or not he allocated enough space 9580 * and reissue the command if necessary. We don't support well 9581 * known logical units, so if the user asks for that, return none. 9582 */ 9583 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9584 9585 /* 9586 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9587 * this request. 9588 */ 9589 ctsio->scsi_status = SCSI_STATUS_OK; 9590 9591 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9592 ctsio->be_move_done = ctl_config_move_done; 9593 ctl_datamove((union ctl_io *)ctsio); 9594 9595 return (retval); 9596} 9597 9598int 9599ctl_request_sense(struct ctl_scsiio *ctsio) 9600{ 9601 struct scsi_request_sense *cdb; 9602 struct scsi_sense_data *sense_ptr; 9603 struct ctl_lun *lun; 9604 uint32_t initidx; 9605 int have_error; 9606 scsi_sense_data_type sense_format; 9607 9608 cdb = (struct scsi_request_sense *)ctsio->cdb; 9609 9610 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9611 9612 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9613 9614 /* 9615 * Determine which sense format the user wants. 9616 */ 9617 if (cdb->byte2 & SRS_DESC) 9618 sense_format = SSD_TYPE_DESC; 9619 else 9620 sense_format = SSD_TYPE_FIXED; 9621 9622 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9623 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9624 ctsio->kern_sg_entries = 0; 9625 9626 /* 9627 * struct scsi_sense_data, which is currently set to 256 bytes, is 9628 * larger than the largest allowed value for the length field in the 9629 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9630 */ 9631 ctsio->residual = 0; 9632 ctsio->kern_data_len = cdb->length; 9633 ctsio->kern_total_len = cdb->length; 9634 9635 ctsio->kern_data_resid = 0; 9636 ctsio->kern_rel_offset = 0; 9637 ctsio->kern_sg_entries = 0; 9638 9639 /* 9640 * If we don't have a LUN, we don't have any pending sense. 9641 */ 9642 if (lun == NULL) 9643 goto no_sense; 9644 9645 have_error = 0; 9646 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9647 /* 9648 * Check for pending sense, and then for pending unit attentions. 9649 * Pending sense gets returned first, then pending unit attentions. 9650 */ 9651 mtx_lock(&lun->lun_lock); 9652#ifdef CTL_WITH_CA 9653 if (ctl_is_set(lun->have_ca, initidx)) { 9654 scsi_sense_data_type stored_format; 9655 9656 /* 9657 * Check to see which sense format was used for the stored 9658 * sense data. 9659 */ 9660 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9661 9662 /* 9663 * If the user requested a different sense format than the 9664 * one we stored, then we need to convert it to the other 9665 * format. If we're going from descriptor to fixed format 9666 * sense data, we may lose things in translation, depending 9667 * on what options were used. 9668 * 9669 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9670 * for some reason we'll just copy it out as-is. 9671 */ 9672 if ((stored_format == SSD_TYPE_FIXED) 9673 && (sense_format == SSD_TYPE_DESC)) 9674 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9675 &lun->pending_sense[initidx], 9676 (struct scsi_sense_data_desc *)sense_ptr); 9677 else if ((stored_format == SSD_TYPE_DESC) 9678 && (sense_format == SSD_TYPE_FIXED)) 9679 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9680 &lun->pending_sense[initidx], 9681 (struct scsi_sense_data_fixed *)sense_ptr); 9682 else 9683 memcpy(sense_ptr, &lun->pending_sense[initidx], 9684 ctl_min(sizeof(*sense_ptr), 9685 sizeof(lun->pending_sense[initidx]))); 9686 9687 ctl_clear_mask(lun->have_ca, initidx); 9688 have_error = 1; 9689 } else 9690#endif 9691 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9692 ctl_ua_type ua_type; 9693 9694 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9695 sense_ptr, sense_format); 9696 if (ua_type != CTL_UA_NONE) 9697 have_error = 1; 9698 } 9699 mtx_unlock(&lun->lun_lock); 9700 9701 /* 9702 * We already have a pending error, return it. 9703 */ 9704 if (have_error != 0) { 9705 /* 9706 * We report the SCSI status as OK, since the status of the 9707 * request sense command itself is OK. 9708 */ 9709 ctsio->scsi_status = SCSI_STATUS_OK; 9710 9711 /* 9712 * We report 0 for the sense length, because we aren't doing 9713 * autosense in this case. We're reporting sense as 9714 * parameter data. 9715 */ 9716 ctsio->sense_len = 0; 9717 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9718 ctsio->be_move_done = ctl_config_move_done; 9719 ctl_datamove((union ctl_io *)ctsio); 9720 9721 return (CTL_RETVAL_COMPLETE); 9722 } 9723 9724no_sense: 9725 9726 /* 9727 * No sense information to report, so we report that everything is 9728 * okay. 9729 */ 9730 ctl_set_sense_data(sense_ptr, 9731 lun, 9732 sense_format, 9733 /*current_error*/ 1, 9734 /*sense_key*/ SSD_KEY_NO_SENSE, 9735 /*asc*/ 0x00, 9736 /*ascq*/ 0x00, 9737 SSD_ELEM_NONE); 9738 9739 ctsio->scsi_status = SCSI_STATUS_OK; 9740 9741 /* 9742 * We report 0 for the sense length, because we aren't doing 9743 * autosense in this case. We're reporting sense as parameter data. 9744 */ 9745 ctsio->sense_len = 0; 9746 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9747 ctsio->be_move_done = ctl_config_move_done; 9748 ctl_datamove((union ctl_io *)ctsio); 9749 9750 return (CTL_RETVAL_COMPLETE); 9751} 9752 9753int 9754ctl_tur(struct ctl_scsiio *ctsio) 9755{ 9756 struct ctl_lun *lun; 9757 9758 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9759 9760 CTL_DEBUG_PRINT(("ctl_tur\n")); 9761 9762 if (lun == NULL) 9763 return (EINVAL); 9764 9765 ctsio->scsi_status = SCSI_STATUS_OK; 9766 ctsio->io_hdr.status = CTL_SUCCESS; 9767 9768 ctl_done((union ctl_io *)ctsio); 9769 9770 return (CTL_RETVAL_COMPLETE); 9771} 9772 9773#ifdef notyet 9774static int 9775ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9776{ 9777 9778} 9779#endif 9780 9781static int 9782ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9783{ 9784 struct scsi_vpd_supported_pages *pages; 9785 int sup_page_size; 9786 struct ctl_lun *lun; 9787 9788 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9789 9790 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9791 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9792 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9793 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9794 ctsio->kern_sg_entries = 0; 9795 9796 if (sup_page_size < alloc_len) { 9797 ctsio->residual = alloc_len - sup_page_size; 9798 ctsio->kern_data_len = sup_page_size; 9799 ctsio->kern_total_len = sup_page_size; 9800 } else { 9801 ctsio->residual = 0; 9802 ctsio->kern_data_len = alloc_len; 9803 ctsio->kern_total_len = alloc_len; 9804 } 9805 ctsio->kern_data_resid = 0; 9806 ctsio->kern_rel_offset = 0; 9807 ctsio->kern_sg_entries = 0; 9808 9809 /* 9810 * The control device is always connected. The disk device, on the 9811 * other hand, may not be online all the time. Need to change this 9812 * to figure out whether the disk device is actually online or not. 9813 */ 9814 if (lun != NULL) 9815 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9816 lun->be_lun->lun_type; 9817 else 9818 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9819 9820 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9821 /* Supported VPD pages */ 9822 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9823 /* Serial Number */ 9824 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9825 /* Device Identification */ 9826 pages->page_list[2] = SVPD_DEVICE_ID; 9827 /* Extended INQUIRY Data */ 9828 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9829 /* Mode Page Policy */ 9830 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9831 /* SCSI Ports */ 9832 pages->page_list[5] = SVPD_SCSI_PORTS; 9833 /* Third-party Copy */ 9834 pages->page_list[6] = SVPD_SCSI_TPC; 9835 /* Block limits */ 9836 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9837 /* Block Device Characteristics */ 9838 pages->page_list[8] = SVPD_BDC; 9839 /* Logical Block Provisioning */ 9840 pages->page_list[9] = SVPD_LBP; 9841 9842 ctsio->scsi_status = SCSI_STATUS_OK; 9843 9844 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9845 ctsio->be_move_done = ctl_config_move_done; 9846 ctl_datamove((union ctl_io *)ctsio); 9847 9848 return (CTL_RETVAL_COMPLETE); 9849} 9850 9851static int 9852ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9853{ 9854 struct scsi_vpd_unit_serial_number *sn_ptr; 9855 struct ctl_lun *lun; 9856 9857 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9858 9859 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9860 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9861 ctsio->kern_sg_entries = 0; 9862 9863 if (sizeof(*sn_ptr) < alloc_len) { 9864 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9865 ctsio->kern_data_len = sizeof(*sn_ptr); 9866 ctsio->kern_total_len = sizeof(*sn_ptr); 9867 } else { 9868 ctsio->residual = 0; 9869 ctsio->kern_data_len = alloc_len; 9870 ctsio->kern_total_len = alloc_len; 9871 } 9872 ctsio->kern_data_resid = 0; 9873 ctsio->kern_rel_offset = 0; 9874 ctsio->kern_sg_entries = 0; 9875 9876 /* 9877 * The control device is always connected. The disk device, on the 9878 * other hand, may not be online all the time. Need to change this 9879 * to figure out whether the disk device is actually online or not. 9880 */ 9881 if (lun != NULL) 9882 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9883 lun->be_lun->lun_type; 9884 else 9885 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9886 9887 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9888 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9889 /* 9890 * If we don't have a LUN, we just leave the serial number as 9891 * all spaces. 9892 */ 9893 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9894 if (lun != NULL) { 9895 strncpy((char *)sn_ptr->serial_num, 9896 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9897 } 9898 ctsio->scsi_status = SCSI_STATUS_OK; 9899 9900 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9901 ctsio->be_move_done = ctl_config_move_done; 9902 ctl_datamove((union ctl_io *)ctsio); 9903 9904 return (CTL_RETVAL_COMPLETE); 9905} 9906 9907 9908static int 9909ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9910{ 9911 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9912 struct ctl_lun *lun; 9913 int data_len; 9914 9915 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9916 9917 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9918 sizeof(struct scsi_vpd_mode_page_policy_descr); 9919 9920 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9921 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9922 ctsio->kern_sg_entries = 0; 9923 9924 if (data_len < alloc_len) { 9925 ctsio->residual = alloc_len - data_len; 9926 ctsio->kern_data_len = data_len; 9927 ctsio->kern_total_len = data_len; 9928 } else { 9929 ctsio->residual = 0; 9930 ctsio->kern_data_len = alloc_len; 9931 ctsio->kern_total_len = alloc_len; 9932 } 9933 ctsio->kern_data_resid = 0; 9934 ctsio->kern_rel_offset = 0; 9935 ctsio->kern_sg_entries = 0; 9936 9937 /* 9938 * The control device is always connected. The disk device, on the 9939 * other hand, may not be online all the time. 9940 */ 9941 if (lun != NULL) 9942 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9943 lun->be_lun->lun_type; 9944 else 9945 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9946 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9947 eid_ptr->page_length = data_len - 4; 9948 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9949 eid_ptr->flags3 = SVPD_EID_V_SUP; 9950 9951 ctsio->scsi_status = SCSI_STATUS_OK; 9952 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9953 ctsio->be_move_done = ctl_config_move_done; 9954 ctl_datamove((union ctl_io *)ctsio); 9955 9956 return (CTL_RETVAL_COMPLETE); 9957} 9958 9959static int 9960ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9961{ 9962 struct scsi_vpd_mode_page_policy *mpp_ptr; 9963 struct ctl_lun *lun; 9964 int data_len; 9965 9966 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9967 9968 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9969 sizeof(struct scsi_vpd_mode_page_policy_descr); 9970 9971 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9972 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9973 ctsio->kern_sg_entries = 0; 9974 9975 if (data_len < alloc_len) { 9976 ctsio->residual = alloc_len - data_len; 9977 ctsio->kern_data_len = data_len; 9978 ctsio->kern_total_len = data_len; 9979 } else { 9980 ctsio->residual = 0; 9981 ctsio->kern_data_len = alloc_len; 9982 ctsio->kern_total_len = alloc_len; 9983 } 9984 ctsio->kern_data_resid = 0; 9985 ctsio->kern_rel_offset = 0; 9986 ctsio->kern_sg_entries = 0; 9987 9988 /* 9989 * The control device is always connected. The disk device, on the 9990 * other hand, may not be online all the time. 9991 */ 9992 if (lun != NULL) 9993 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9994 lun->be_lun->lun_type; 9995 else 9996 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9997 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9998 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9999 mpp_ptr->descr[0].page_code = 0x3f; 10000 mpp_ptr->descr[0].subpage_code = 0xff; 10001 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 10002 10003 ctsio->scsi_status = SCSI_STATUS_OK; 10004 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10005 ctsio->be_move_done = ctl_config_move_done; 10006 ctl_datamove((union ctl_io *)ctsio); 10007 10008 return (CTL_RETVAL_COMPLETE); 10009} 10010 10011static int 10012ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 10013{ 10014 struct scsi_vpd_device_id *devid_ptr; 10015 struct scsi_vpd_id_descriptor *desc; 10016 struct ctl_softc *ctl_softc; 10017 struct ctl_lun *lun; 10018 struct ctl_port *port; 10019 int data_len; 10020 uint8_t proto; 10021 10022 ctl_softc = control_softc; 10023 10024 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 10025 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10026 10027 data_len = sizeof(struct scsi_vpd_device_id) + 10028 sizeof(struct scsi_vpd_id_descriptor) + 10029 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 10030 sizeof(struct scsi_vpd_id_descriptor) + 10031 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 10032 if (lun && lun->lun_devid) 10033 data_len += lun->lun_devid->len; 10034 if (port->port_devid) 10035 data_len += port->port_devid->len; 10036 if (port->target_devid) 10037 data_len += port->target_devid->len; 10038 10039 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10040 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10041 ctsio->kern_sg_entries = 0; 10042 10043 if (data_len < alloc_len) { 10044 ctsio->residual = alloc_len - data_len; 10045 ctsio->kern_data_len = data_len; 10046 ctsio->kern_total_len = data_len; 10047 } else { 10048 ctsio->residual = 0; 10049 ctsio->kern_data_len = alloc_len; 10050 ctsio->kern_total_len = alloc_len; 10051 } 10052 ctsio->kern_data_resid = 0; 10053 ctsio->kern_rel_offset = 0; 10054 ctsio->kern_sg_entries = 0; 10055 10056 /* 10057 * The control device is always connected. The disk device, on the 10058 * other hand, may not be online all the time. 10059 */ 10060 if (lun != NULL) 10061 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10062 lun->be_lun->lun_type; 10063 else 10064 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10065 devid_ptr->page_code = SVPD_DEVICE_ID; 10066 scsi_ulto2b(data_len - 4, devid_ptr->length); 10067 10068 if (port->port_type == CTL_PORT_FC) 10069 proto = SCSI_PROTO_FC << 4; 10070 else if (port->port_type == CTL_PORT_ISCSI) 10071 proto = SCSI_PROTO_ISCSI << 4; 10072 else 10073 proto = SCSI_PROTO_SPI << 4; 10074 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10075 10076 /* 10077 * We're using a LUN association here. i.e., this device ID is a 10078 * per-LUN identifier. 10079 */ 10080 if (lun && lun->lun_devid) { 10081 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10082 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10083 lun->lun_devid->len); 10084 } 10085 10086 /* 10087 * This is for the WWPN which is a port association. 10088 */ 10089 if (port->port_devid) { 10090 memcpy(desc, port->port_devid->data, port->port_devid->len); 10091 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10092 port->port_devid->len); 10093 } 10094 10095 /* 10096 * This is for the Relative Target Port(type 4h) identifier 10097 */ 10098 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10099 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10100 SVPD_ID_TYPE_RELTARG; 10101 desc->length = 4; 10102 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10103 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10104 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10105 10106 /* 10107 * This is for the Target Port Group(type 5h) identifier 10108 */ 10109 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10110 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10111 SVPD_ID_TYPE_TPORTGRP; 10112 desc->length = 4; 10113 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10114 &desc->identifier[2]); 10115 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10116 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10117 10118 /* 10119 * This is for the Target identifier 10120 */ 10121 if (port->target_devid) { 10122 memcpy(desc, port->target_devid->data, port->target_devid->len); 10123 } 10124 10125 ctsio->scsi_status = SCSI_STATUS_OK; 10126 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10127 ctsio->be_move_done = ctl_config_move_done; 10128 ctl_datamove((union ctl_io *)ctsio); 10129 10130 return (CTL_RETVAL_COMPLETE); 10131} 10132 10133static int 10134ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10135{ 10136 struct ctl_softc *softc = control_softc; 10137 struct scsi_vpd_scsi_ports *sp; 10138 struct scsi_vpd_port_designation *pd; 10139 struct scsi_vpd_port_designation_cont *pdc; 10140 struct ctl_lun *lun; 10141 struct ctl_port *port; 10142 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10143 int num_target_port_groups, single; 10144 10145 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10146 10147 single = ctl_is_single; 10148 if (single) 10149 num_target_port_groups = 1; 10150 else 10151 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10152 num_target_ports = 0; 10153 iid_len = 0; 10154 id_len = 0; 10155 mtx_lock(&softc->ctl_lock); 10156 STAILQ_FOREACH(port, &softc->port_list, links) { 10157 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10158 continue; 10159 if (lun != NULL && 10160 ctl_map_lun_back(port->targ_port, lun->lun) >= 10161 CTL_MAX_LUNS) 10162 continue; 10163 num_target_ports++; 10164 if (port->init_devid) 10165 iid_len += port->init_devid->len; 10166 if (port->port_devid) 10167 id_len += port->port_devid->len; 10168 } 10169 mtx_unlock(&softc->ctl_lock); 10170 10171 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10172 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10173 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10174 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10175 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10176 ctsio->kern_sg_entries = 0; 10177 10178 if (data_len < alloc_len) { 10179 ctsio->residual = alloc_len - data_len; 10180 ctsio->kern_data_len = data_len; 10181 ctsio->kern_total_len = data_len; 10182 } else { 10183 ctsio->residual = 0; 10184 ctsio->kern_data_len = alloc_len; 10185 ctsio->kern_total_len = alloc_len; 10186 } 10187 ctsio->kern_data_resid = 0; 10188 ctsio->kern_rel_offset = 0; 10189 ctsio->kern_sg_entries = 0; 10190 10191 /* 10192 * The control device is always connected. The disk device, on the 10193 * other hand, may not be online all the time. Need to change this 10194 * to figure out whether the disk device is actually online or not. 10195 */ 10196 if (lun != NULL) 10197 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10198 lun->be_lun->lun_type; 10199 else 10200 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10201 10202 sp->page_code = SVPD_SCSI_PORTS; 10203 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10204 sp->page_length); 10205 pd = &sp->design[0]; 10206 10207 mtx_lock(&softc->ctl_lock); 10208 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10209 pg = 0; 10210 else 10211 pg = 1; 10212 for (g = 0; g < num_target_port_groups; g++) { 10213 STAILQ_FOREACH(port, &softc->port_list, links) { 10214 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10215 continue; 10216 if (lun != NULL && 10217 ctl_map_lun_back(port->targ_port, lun->lun) >= 10218 CTL_MAX_LUNS) 10219 continue; 10220 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10221 scsi_ulto2b(p, pd->relative_port_id); 10222 if (port->init_devid && g == pg) { 10223 iid_len = port->init_devid->len; 10224 memcpy(pd->initiator_transportid, 10225 port->init_devid->data, port->init_devid->len); 10226 } else 10227 iid_len = 0; 10228 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10229 pdc = (struct scsi_vpd_port_designation_cont *) 10230 (&pd->initiator_transportid[iid_len]); 10231 if (port->port_devid && g == pg) { 10232 id_len = port->port_devid->len; 10233 memcpy(pdc->target_port_descriptors, 10234 port->port_devid->data, port->port_devid->len); 10235 } else 10236 id_len = 0; 10237 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10238 pd = (struct scsi_vpd_port_designation *) 10239 ((uint8_t *)pdc->target_port_descriptors + id_len); 10240 } 10241 } 10242 mtx_unlock(&softc->ctl_lock); 10243 10244 ctsio->scsi_status = SCSI_STATUS_OK; 10245 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10246 ctsio->be_move_done = ctl_config_move_done; 10247 ctl_datamove((union ctl_io *)ctsio); 10248 10249 return (CTL_RETVAL_COMPLETE); 10250} 10251 10252static int 10253ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10254{ 10255 struct scsi_vpd_block_limits *bl_ptr; 10256 struct ctl_lun *lun; 10257 int bs; 10258 10259 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10260 10261 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10262 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10263 ctsio->kern_sg_entries = 0; 10264 10265 if (sizeof(*bl_ptr) < alloc_len) { 10266 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10267 ctsio->kern_data_len = sizeof(*bl_ptr); 10268 ctsio->kern_total_len = sizeof(*bl_ptr); 10269 } else { 10270 ctsio->residual = 0; 10271 ctsio->kern_data_len = alloc_len; 10272 ctsio->kern_total_len = alloc_len; 10273 } 10274 ctsio->kern_data_resid = 0; 10275 ctsio->kern_rel_offset = 0; 10276 ctsio->kern_sg_entries = 0; 10277 10278 /* 10279 * The control device is always connected. The disk device, on the 10280 * other hand, may not be online all the time. Need to change this 10281 * to figure out whether the disk device is actually online or not. 10282 */ 10283 if (lun != NULL) 10284 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10285 lun->be_lun->lun_type; 10286 else 10287 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10288 10289 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10290 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10291 bl_ptr->max_cmp_write_len = 0xff; 10292 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10293 if (lun != NULL) { 10294 bs = lun->be_lun->blocksize; 10295 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10296 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10297 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10298 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10299 if (lun->be_lun->pblockexp != 0) { 10300 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10301 bl_ptr->opt_unmap_grain); 10302 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10303 bl_ptr->unmap_grain_align); 10304 } 10305 } 10306 } 10307 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10308 10309 ctsio->scsi_status = SCSI_STATUS_OK; 10310 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10311 ctsio->be_move_done = ctl_config_move_done; 10312 ctl_datamove((union ctl_io *)ctsio); 10313 10314 return (CTL_RETVAL_COMPLETE); 10315} 10316 10317static int 10318ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10319{ 10320 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10321 struct ctl_lun *lun; 10322 10323 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10324 10325 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10326 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10327 ctsio->kern_sg_entries = 0; 10328 10329 if (sizeof(*bdc_ptr) < alloc_len) { 10330 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10331 ctsio->kern_data_len = sizeof(*bdc_ptr); 10332 ctsio->kern_total_len = sizeof(*bdc_ptr); 10333 } else { 10334 ctsio->residual = 0; 10335 ctsio->kern_data_len = alloc_len; 10336 ctsio->kern_total_len = alloc_len; 10337 } 10338 ctsio->kern_data_resid = 0; 10339 ctsio->kern_rel_offset = 0; 10340 ctsio->kern_sg_entries = 0; 10341 10342 /* 10343 * The control device is always connected. The disk device, on the 10344 * other hand, may not be online all the time. Need to change this 10345 * to figure out whether the disk device is actually online or not. 10346 */ 10347 if (lun != NULL) 10348 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10349 lun->be_lun->lun_type; 10350 else 10351 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10352 bdc_ptr->page_code = SVPD_BDC; 10353 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10354 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10355 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10356 10357 ctsio->scsi_status = SCSI_STATUS_OK; 10358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10359 ctsio->be_move_done = ctl_config_move_done; 10360 ctl_datamove((union ctl_io *)ctsio); 10361 10362 return (CTL_RETVAL_COMPLETE); 10363} 10364 10365static int 10366ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10367{ 10368 struct scsi_vpd_logical_block_prov *lbp_ptr; 10369 struct ctl_lun *lun; 10370 10371 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10372 10373 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10374 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10375 ctsio->kern_sg_entries = 0; 10376 10377 if (sizeof(*lbp_ptr) < alloc_len) { 10378 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10379 ctsio->kern_data_len = sizeof(*lbp_ptr); 10380 ctsio->kern_total_len = sizeof(*lbp_ptr); 10381 } else { 10382 ctsio->residual = 0; 10383 ctsio->kern_data_len = alloc_len; 10384 ctsio->kern_total_len = alloc_len; 10385 } 10386 ctsio->kern_data_resid = 0; 10387 ctsio->kern_rel_offset = 0; 10388 ctsio->kern_sg_entries = 0; 10389 10390 /* 10391 * The control device is always connected. The disk device, on the 10392 * other hand, may not be online all the time. Need to change this 10393 * to figure out whether the disk device is actually online or not. 10394 */ 10395 if (lun != NULL) 10396 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10397 lun->be_lun->lun_type; 10398 else 10399 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10400 10401 lbp_ptr->page_code = SVPD_LBP; 10402 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10403 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10404 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10405 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10406 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10407 } 10408 10409 ctsio->scsi_status = SCSI_STATUS_OK; 10410 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10411 ctsio->be_move_done = ctl_config_move_done; 10412 ctl_datamove((union ctl_io *)ctsio); 10413 10414 return (CTL_RETVAL_COMPLETE); 10415} 10416 10417static int 10418ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10419{ 10420 struct scsi_inquiry *cdb; 10421 struct ctl_lun *lun; 10422 int alloc_len, retval; 10423 10424 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10425 cdb = (struct scsi_inquiry *)ctsio->cdb; 10426 10427 retval = CTL_RETVAL_COMPLETE; 10428 10429 alloc_len = scsi_2btoul(cdb->length); 10430 10431 switch (cdb->page_code) { 10432 case SVPD_SUPPORTED_PAGES: 10433 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10434 break; 10435 case SVPD_UNIT_SERIAL_NUMBER: 10436 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10437 break; 10438 case SVPD_DEVICE_ID: 10439 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10440 break; 10441 case SVPD_EXTENDED_INQUIRY_DATA: 10442 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10443 break; 10444 case SVPD_MODE_PAGE_POLICY: 10445 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10446 break; 10447 case SVPD_SCSI_PORTS: 10448 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10449 break; 10450 case SVPD_SCSI_TPC: 10451 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10452 break; 10453 case SVPD_BLOCK_LIMITS: 10454 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10455 break; 10456 case SVPD_BDC: 10457 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10458 break; 10459 case SVPD_LBP: 10460 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10461 break; 10462 default: 10463 ctl_set_invalid_field(ctsio, 10464 /*sks_valid*/ 1, 10465 /*command*/ 1, 10466 /*field*/ 2, 10467 /*bit_valid*/ 0, 10468 /*bit*/ 0); 10469 ctl_done((union ctl_io *)ctsio); 10470 retval = CTL_RETVAL_COMPLETE; 10471 break; 10472 } 10473 10474 return (retval); 10475} 10476 10477static int 10478ctl_inquiry_std(struct ctl_scsiio *ctsio) 10479{ 10480 struct scsi_inquiry_data *inq_ptr; 10481 struct scsi_inquiry *cdb; 10482 struct ctl_softc *ctl_softc; 10483 struct ctl_lun *lun; 10484 char *val; 10485 uint32_t alloc_len, data_len; 10486 ctl_port_type port_type; 10487 10488 ctl_softc = control_softc; 10489 10490 /* 10491 * Figure out whether we're talking to a Fibre Channel port or not. 10492 * We treat the ioctl front end, and any SCSI adapters, as packetized 10493 * SCSI front ends. 10494 */ 10495 port_type = ctl_softc->ctl_ports[ 10496 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10497 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10498 port_type = CTL_PORT_SCSI; 10499 10500 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10501 cdb = (struct scsi_inquiry *)ctsio->cdb; 10502 alloc_len = scsi_2btoul(cdb->length); 10503 10504 /* 10505 * We malloc the full inquiry data size here and fill it 10506 * in. If the user only asks for less, we'll give him 10507 * that much. 10508 */ 10509 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10510 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10511 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10512 ctsio->kern_sg_entries = 0; 10513 ctsio->kern_data_resid = 0; 10514 ctsio->kern_rel_offset = 0; 10515 10516 if (data_len < alloc_len) { 10517 ctsio->residual = alloc_len - data_len; 10518 ctsio->kern_data_len = data_len; 10519 ctsio->kern_total_len = data_len; 10520 } else { 10521 ctsio->residual = 0; 10522 ctsio->kern_data_len = alloc_len; 10523 ctsio->kern_total_len = alloc_len; 10524 } 10525 10526 /* 10527 * If we have a LUN configured, report it as connected. Otherwise, 10528 * report that it is offline or no device is supported, depending 10529 * on the value of inquiry_pq_no_lun. 10530 * 10531 * According to the spec (SPC-4 r34), the peripheral qualifier 10532 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10533 * 10534 * "A peripheral device having the specified peripheral device type 10535 * is not connected to this logical unit. However, the device 10536 * server is capable of supporting the specified peripheral device 10537 * type on this logical unit." 10538 * 10539 * According to the same spec, the peripheral qualifier 10540 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10541 * 10542 * "The device server is not capable of supporting a peripheral 10543 * device on this logical unit. For this peripheral qualifier the 10544 * peripheral device type shall be set to 1Fh. All other peripheral 10545 * device type values are reserved for this peripheral qualifier." 10546 * 10547 * Given the text, it would seem that we probably want to report that 10548 * the LUN is offline here. There is no LUN connected, but we can 10549 * support a LUN at the given LUN number. 10550 * 10551 * In the real world, though, it sounds like things are a little 10552 * different: 10553 * 10554 * - Linux, when presented with a LUN with the offline peripheral 10555 * qualifier, will create an sg driver instance for it. So when 10556 * you attach it to CTL, you wind up with a ton of sg driver 10557 * instances. (One for every LUN that Linux bothered to probe.) 10558 * Linux does this despite the fact that it issues a REPORT LUNs 10559 * to LUN 0 to get the inventory of supported LUNs. 10560 * 10561 * - There is other anecdotal evidence (from Emulex folks) about 10562 * arrays that use the offline peripheral qualifier for LUNs that 10563 * are on the "passive" path in an active/passive array. 10564 * 10565 * So the solution is provide a hopefully reasonable default 10566 * (return bad/no LUN) and allow the user to change the behavior 10567 * with a tunable/sysctl variable. 10568 */ 10569 if (lun != NULL) 10570 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10571 lun->be_lun->lun_type; 10572 else if (ctl_softc->inquiry_pq_no_lun == 0) 10573 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10574 else 10575 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10576 10577 /* RMB in byte 2 is 0 */ 10578 inq_ptr->version = SCSI_REV_SPC4; 10579 10580 /* 10581 * According to SAM-3, even if a device only supports a single 10582 * level of LUN addressing, it should still set the HISUP bit: 10583 * 10584 * 4.9.1 Logical unit numbers overview 10585 * 10586 * All logical unit number formats described in this standard are 10587 * hierarchical in structure even when only a single level in that 10588 * hierarchy is used. The HISUP bit shall be set to one in the 10589 * standard INQUIRY data (see SPC-2) when any logical unit number 10590 * format described in this standard is used. Non-hierarchical 10591 * formats are outside the scope of this standard. 10592 * 10593 * Therefore we set the HiSup bit here. 10594 * 10595 * The reponse format is 2, per SPC-3. 10596 */ 10597 inq_ptr->response_format = SID_HiSup | 2; 10598 10599 inq_ptr->additional_length = data_len - 10600 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10601 CTL_DEBUG_PRINT(("additional_length = %d\n", 10602 inq_ptr->additional_length)); 10603 10604 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10605 /* 16 bit addressing */ 10606 if (port_type == CTL_PORT_SCSI) 10607 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10608 /* XXX set the SID_MultiP bit here if we're actually going to 10609 respond on multiple ports */ 10610 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10611 10612 /* 16 bit data bus, synchronous transfers */ 10613 if (port_type == CTL_PORT_SCSI) 10614 inq_ptr->flags = SID_WBus16 | SID_Sync; 10615 /* 10616 * XXX KDM do we want to support tagged queueing on the control 10617 * device at all? 10618 */ 10619 if ((lun == NULL) 10620 || (lun->be_lun->lun_type != T_PROCESSOR)) 10621 inq_ptr->flags |= SID_CmdQue; 10622 /* 10623 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10624 * We have 8 bytes for the vendor name, and 16 bytes for the device 10625 * name and 4 bytes for the revision. 10626 */ 10627 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10628 "vendor")) == NULL) { 10629 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10630 } else { 10631 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10632 strncpy(inq_ptr->vendor, val, 10633 min(sizeof(inq_ptr->vendor), strlen(val))); 10634 } 10635 if (lun == NULL) { 10636 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10637 sizeof(inq_ptr->product)); 10638 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10639 switch (lun->be_lun->lun_type) { 10640 case T_DIRECT: 10641 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10642 sizeof(inq_ptr->product)); 10643 break; 10644 case T_PROCESSOR: 10645 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10646 sizeof(inq_ptr->product)); 10647 break; 10648 default: 10649 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10650 sizeof(inq_ptr->product)); 10651 break; 10652 } 10653 } else { 10654 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10655 strncpy(inq_ptr->product, val, 10656 min(sizeof(inq_ptr->product), strlen(val))); 10657 } 10658 10659 /* 10660 * XXX make this a macro somewhere so it automatically gets 10661 * incremented when we make changes. 10662 */ 10663 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10664 "revision")) == NULL) { 10665 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10666 } else { 10667 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10668 strncpy(inq_ptr->revision, val, 10669 min(sizeof(inq_ptr->revision), strlen(val))); 10670 } 10671 10672 /* 10673 * For parallel SCSI, we support double transition and single 10674 * transition clocking. We also support QAS (Quick Arbitration 10675 * and Selection) and Information Unit transfers on both the 10676 * control and array devices. 10677 */ 10678 if (port_type == CTL_PORT_SCSI) 10679 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10680 SID_SPI_IUS; 10681 10682 /* SAM-5 (no version claimed) */ 10683 scsi_ulto2b(0x00A0, inq_ptr->version1); 10684 /* SPC-4 (no version claimed) */ 10685 scsi_ulto2b(0x0460, inq_ptr->version2); 10686 if (port_type == CTL_PORT_FC) { 10687 /* FCP-2 ANSI INCITS.350:2003 */ 10688 scsi_ulto2b(0x0917, inq_ptr->version3); 10689 } else if (port_type == CTL_PORT_SCSI) { 10690 /* SPI-4 ANSI INCITS.362:200x */ 10691 scsi_ulto2b(0x0B56, inq_ptr->version3); 10692 } else if (port_type == CTL_PORT_ISCSI) { 10693 /* iSCSI (no version claimed) */ 10694 scsi_ulto2b(0x0960, inq_ptr->version3); 10695 } else if (port_type == CTL_PORT_SAS) { 10696 /* SAS (no version claimed) */ 10697 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10698 } 10699 10700 if (lun == NULL) { 10701 /* SBC-3 (no version claimed) */ 10702 scsi_ulto2b(0x04C0, inq_ptr->version4); 10703 } else { 10704 switch (lun->be_lun->lun_type) { 10705 case T_DIRECT: 10706 /* SBC-3 (no version claimed) */ 10707 scsi_ulto2b(0x04C0, inq_ptr->version4); 10708 break; 10709 case T_PROCESSOR: 10710 default: 10711 break; 10712 } 10713 } 10714 10715 ctsio->scsi_status = SCSI_STATUS_OK; 10716 if (ctsio->kern_data_len > 0) { 10717 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10718 ctsio->be_move_done = ctl_config_move_done; 10719 ctl_datamove((union ctl_io *)ctsio); 10720 } else { 10721 ctsio->io_hdr.status = CTL_SUCCESS; 10722 ctl_done((union ctl_io *)ctsio); 10723 } 10724 10725 return (CTL_RETVAL_COMPLETE); 10726} 10727 10728int 10729ctl_inquiry(struct ctl_scsiio *ctsio) 10730{ 10731 struct scsi_inquiry *cdb; 10732 int retval; 10733 10734 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10735 10736 cdb = (struct scsi_inquiry *)ctsio->cdb; 10737 if (cdb->byte2 & SI_EVPD) 10738 retval = ctl_inquiry_evpd(ctsio); 10739 else if (cdb->page_code == 0) 10740 retval = ctl_inquiry_std(ctsio); 10741 else { 10742 ctl_set_invalid_field(ctsio, 10743 /*sks_valid*/ 1, 10744 /*command*/ 1, 10745 /*field*/ 2, 10746 /*bit_valid*/ 0, 10747 /*bit*/ 0); 10748 ctl_done((union ctl_io *)ctsio); 10749 return (CTL_RETVAL_COMPLETE); 10750 } 10751 10752 return (retval); 10753} 10754 10755/* 10756 * For known CDB types, parse the LBA and length. 10757 */ 10758static int 10759ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10760{ 10761 if (io->io_hdr.io_type != CTL_IO_SCSI) 10762 return (1); 10763 10764 switch (io->scsiio.cdb[0]) { 10765 case COMPARE_AND_WRITE: { 10766 struct scsi_compare_and_write *cdb; 10767 10768 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10769 10770 *lba = scsi_8btou64(cdb->addr); 10771 *len = cdb->length; 10772 break; 10773 } 10774 case READ_6: 10775 case WRITE_6: { 10776 struct scsi_rw_6 *cdb; 10777 10778 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10779 10780 *lba = scsi_3btoul(cdb->addr); 10781 /* only 5 bits are valid in the most significant address byte */ 10782 *lba &= 0x1fffff; 10783 *len = cdb->length; 10784 break; 10785 } 10786 case READ_10: 10787 case WRITE_10: { 10788 struct scsi_rw_10 *cdb; 10789 10790 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10791 10792 *lba = scsi_4btoul(cdb->addr); 10793 *len = scsi_2btoul(cdb->length); 10794 break; 10795 } 10796 case WRITE_VERIFY_10: { 10797 struct scsi_write_verify_10 *cdb; 10798 10799 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10800 10801 *lba = scsi_4btoul(cdb->addr); 10802 *len = scsi_2btoul(cdb->length); 10803 break; 10804 } 10805 case READ_12: 10806 case WRITE_12: { 10807 struct scsi_rw_12 *cdb; 10808 10809 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10810 10811 *lba = scsi_4btoul(cdb->addr); 10812 *len = scsi_4btoul(cdb->length); 10813 break; 10814 } 10815 case WRITE_VERIFY_12: { 10816 struct scsi_write_verify_12 *cdb; 10817 10818 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10819 10820 *lba = scsi_4btoul(cdb->addr); 10821 *len = scsi_4btoul(cdb->length); 10822 break; 10823 } 10824 case READ_16: 10825 case WRITE_16: { 10826 struct scsi_rw_16 *cdb; 10827 10828 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10829 10830 *lba = scsi_8btou64(cdb->addr); 10831 *len = scsi_4btoul(cdb->length); 10832 break; 10833 } 10834 case WRITE_VERIFY_16: { 10835 struct scsi_write_verify_16 *cdb; 10836 10837 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10838 10839 10840 *lba = scsi_8btou64(cdb->addr); 10841 *len = scsi_4btoul(cdb->length); 10842 break; 10843 } 10844 case WRITE_SAME_10: { 10845 struct scsi_write_same_10 *cdb; 10846 10847 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10848 10849 *lba = scsi_4btoul(cdb->addr); 10850 *len = scsi_2btoul(cdb->length); 10851 break; 10852 } 10853 case WRITE_SAME_16: { 10854 struct scsi_write_same_16 *cdb; 10855 10856 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10857 10858 *lba = scsi_8btou64(cdb->addr); 10859 *len = scsi_4btoul(cdb->length); 10860 break; 10861 } 10862 case VERIFY_10: { 10863 struct scsi_verify_10 *cdb; 10864 10865 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10866 10867 *lba = scsi_4btoul(cdb->addr); 10868 *len = scsi_2btoul(cdb->length); 10869 break; 10870 } 10871 case VERIFY_12: { 10872 struct scsi_verify_12 *cdb; 10873 10874 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10875 10876 *lba = scsi_4btoul(cdb->addr); 10877 *len = scsi_4btoul(cdb->length); 10878 break; 10879 } 10880 case VERIFY_16: { 10881 struct scsi_verify_16 *cdb; 10882 10883 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10884 10885 *lba = scsi_8btou64(cdb->addr); 10886 *len = scsi_4btoul(cdb->length); 10887 break; 10888 } 10889 case UNMAP: { 10890 *lba = 0; 10891 *len = UINT64_MAX; 10892 break; 10893 } 10894 default: 10895 return (1); 10896 break; /* NOTREACHED */ 10897 } 10898 10899 return (0); 10900} 10901 10902static ctl_action 10903ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10904{ 10905 uint64_t endlba1, endlba2; 10906 10907 endlba1 = lba1 + len1 - 1; 10908 endlba2 = lba2 + len2 - 1; 10909 10910 if ((endlba1 < lba2) 10911 || (endlba2 < lba1)) 10912 return (CTL_ACTION_PASS); 10913 else 10914 return (CTL_ACTION_BLOCK); 10915} 10916 10917static int 10918ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10919{ 10920 struct ctl_ptr_len_flags *ptrlen; 10921 struct scsi_unmap_desc *buf, *end, *range; 10922 uint64_t lba; 10923 uint32_t len; 10924 10925 /* If not UNMAP -- go other way. */ 10926 if (io->io_hdr.io_type != CTL_IO_SCSI || 10927 io->scsiio.cdb[0] != UNMAP) 10928 return (CTL_ACTION_ERROR); 10929 10930 /* If UNMAP without data -- block and wait for data. */ 10931 ptrlen = (struct ctl_ptr_len_flags *) 10932 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10933 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10934 ptrlen->ptr == NULL) 10935 return (CTL_ACTION_BLOCK); 10936 10937 /* UNMAP with data -- check for collision. */ 10938 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10939 end = buf + ptrlen->len / sizeof(*buf); 10940 for (range = buf; range < end; range++) { 10941 lba = scsi_8btou64(range->lba); 10942 len = scsi_4btoul(range->length); 10943 if ((lba < lba2 + len2) && (lba + len > lba2)) 10944 return (CTL_ACTION_BLOCK); 10945 } 10946 return (CTL_ACTION_PASS); 10947} 10948 10949static ctl_action 10950ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10951{ 10952 uint64_t lba1, lba2; 10953 uint64_t len1, len2; 10954 int retval; 10955 10956 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10957 return (CTL_ACTION_ERROR); 10958 10959 retval = ctl_extent_check_unmap(io2, lba1, len1); 10960 if (retval != CTL_ACTION_ERROR) 10961 return (retval); 10962 10963 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10964 return (CTL_ACTION_ERROR); 10965 10966 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10967} 10968 10969static ctl_action 10970ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10971 union ctl_io *ooa_io) 10972{ 10973 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10974 ctl_serialize_action *serialize_row; 10975 10976 /* 10977 * The initiator attempted multiple untagged commands at the same 10978 * time. Can't do that. 10979 */ 10980 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10981 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10982 && ((pending_io->io_hdr.nexus.targ_port == 10983 ooa_io->io_hdr.nexus.targ_port) 10984 && (pending_io->io_hdr.nexus.initid.id == 10985 ooa_io->io_hdr.nexus.initid.id)) 10986 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10987 return (CTL_ACTION_OVERLAP); 10988 10989 /* 10990 * The initiator attempted to send multiple tagged commands with 10991 * the same ID. (It's fine if different initiators have the same 10992 * tag ID.) 10993 * 10994 * Even if all of those conditions are true, we don't kill the I/O 10995 * if the command ahead of us has been aborted. We won't end up 10996 * sending it to the FETD, and it's perfectly legal to resend a 10997 * command with the same tag number as long as the previous 10998 * instance of this tag number has been aborted somehow. 10999 */ 11000 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11001 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11002 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11003 && ((pending_io->io_hdr.nexus.targ_port == 11004 ooa_io->io_hdr.nexus.targ_port) 11005 && (pending_io->io_hdr.nexus.initid.id == 11006 ooa_io->io_hdr.nexus.initid.id)) 11007 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11008 return (CTL_ACTION_OVERLAP_TAG); 11009 11010 /* 11011 * If we get a head of queue tag, SAM-3 says that we should 11012 * immediately execute it. 11013 * 11014 * What happens if this command would normally block for some other 11015 * reason? e.g. a request sense with a head of queue tag 11016 * immediately after a write. Normally that would block, but this 11017 * will result in its getting executed immediately... 11018 * 11019 * We currently return "pass" instead of "skip", so we'll end up 11020 * going through the rest of the queue to check for overlapped tags. 11021 * 11022 * XXX KDM check for other types of blockage first?? 11023 */ 11024 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11025 return (CTL_ACTION_PASS); 11026 11027 /* 11028 * Ordered tags have to block until all items ahead of them 11029 * have completed. If we get called with an ordered tag, we always 11030 * block, if something else is ahead of us in the queue. 11031 */ 11032 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11033 return (CTL_ACTION_BLOCK); 11034 11035 /* 11036 * Simple tags get blocked until all head of queue and ordered tags 11037 * ahead of them have completed. I'm lumping untagged commands in 11038 * with simple tags here. XXX KDM is that the right thing to do? 11039 */ 11040 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11041 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11042 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11043 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11044 return (CTL_ACTION_BLOCK); 11045 11046 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11047 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11048 11049 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11050 11051 switch (serialize_row[pending_entry->seridx]) { 11052 case CTL_SER_BLOCK: 11053 return (CTL_ACTION_BLOCK); 11054 case CTL_SER_EXTENT: 11055 return (ctl_extent_check(pending_io, ooa_io)); 11056 case CTL_SER_EXTENTOPT: 11057 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11058 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11059 return (ctl_extent_check(pending_io, ooa_io)); 11060 /* FALLTHROUGH */ 11061 case CTL_SER_PASS: 11062 return (CTL_ACTION_PASS); 11063 case CTL_SER_BLOCKOPT: 11064 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11065 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11066 return (CTL_ACTION_BLOCK); 11067 return (CTL_ACTION_PASS); 11068 case CTL_SER_SKIP: 11069 return (CTL_ACTION_SKIP); 11070 default: 11071 panic("invalid serialization value %d", 11072 serialize_row[pending_entry->seridx]); 11073 } 11074 11075 return (CTL_ACTION_ERROR); 11076} 11077 11078/* 11079 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11080 * Assumptions: 11081 * - pending_io is generally either incoming, or on the blocked queue 11082 * - starting I/O is the I/O we want to start the check with. 11083 */ 11084static ctl_action 11085ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11086 union ctl_io *starting_io) 11087{ 11088 union ctl_io *ooa_io; 11089 ctl_action action; 11090 11091 mtx_assert(&lun->lun_lock, MA_OWNED); 11092 11093 /* 11094 * Run back along the OOA queue, starting with the current 11095 * blocked I/O and going through every I/O before it on the 11096 * queue. If starting_io is NULL, we'll just end up returning 11097 * CTL_ACTION_PASS. 11098 */ 11099 for (ooa_io = starting_io; ooa_io != NULL; 11100 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11101 ooa_links)){ 11102 11103 /* 11104 * This routine just checks to see whether 11105 * cur_blocked is blocked by ooa_io, which is ahead 11106 * of it in the queue. It doesn't queue/dequeue 11107 * cur_blocked. 11108 */ 11109 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11110 switch (action) { 11111 case CTL_ACTION_BLOCK: 11112 case CTL_ACTION_OVERLAP: 11113 case CTL_ACTION_OVERLAP_TAG: 11114 case CTL_ACTION_SKIP: 11115 case CTL_ACTION_ERROR: 11116 return (action); 11117 break; /* NOTREACHED */ 11118 case CTL_ACTION_PASS: 11119 break; 11120 default: 11121 panic("invalid action %d", action); 11122 break; /* NOTREACHED */ 11123 } 11124 } 11125 11126 return (CTL_ACTION_PASS); 11127} 11128 11129/* 11130 * Assumptions: 11131 * - An I/O has just completed, and has been removed from the per-LUN OOA 11132 * queue, so some items on the blocked queue may now be unblocked. 11133 */ 11134static int 11135ctl_check_blocked(struct ctl_lun *lun) 11136{ 11137 union ctl_io *cur_blocked, *next_blocked; 11138 11139 mtx_assert(&lun->lun_lock, MA_OWNED); 11140 11141 /* 11142 * Run forward from the head of the blocked queue, checking each 11143 * entry against the I/Os prior to it on the OOA queue to see if 11144 * there is still any blockage. 11145 * 11146 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11147 * with our removing a variable on it while it is traversing the 11148 * list. 11149 */ 11150 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11151 cur_blocked != NULL; cur_blocked = next_blocked) { 11152 union ctl_io *prev_ooa; 11153 ctl_action action; 11154 11155 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11156 blocked_links); 11157 11158 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11159 ctl_ooaq, ooa_links); 11160 11161 /* 11162 * If cur_blocked happens to be the first item in the OOA 11163 * queue now, prev_ooa will be NULL, and the action 11164 * returned will just be CTL_ACTION_PASS. 11165 */ 11166 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11167 11168 switch (action) { 11169 case CTL_ACTION_BLOCK: 11170 /* Nothing to do here, still blocked */ 11171 break; 11172 case CTL_ACTION_OVERLAP: 11173 case CTL_ACTION_OVERLAP_TAG: 11174 /* 11175 * This shouldn't happen! In theory we've already 11176 * checked this command for overlap... 11177 */ 11178 break; 11179 case CTL_ACTION_PASS: 11180 case CTL_ACTION_SKIP: { 11181 struct ctl_softc *softc; 11182 const struct ctl_cmd_entry *entry; 11183 uint32_t initidx; 11184 int isc_retval; 11185 11186 /* 11187 * The skip case shouldn't happen, this transaction 11188 * should have never made it onto the blocked queue. 11189 */ 11190 /* 11191 * This I/O is no longer blocked, we can remove it 11192 * from the blocked queue. Since this is a TAILQ 11193 * (doubly linked list), we can do O(1) removals 11194 * from any place on the list. 11195 */ 11196 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11197 blocked_links); 11198 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11199 11200 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11201 /* 11202 * Need to send IO back to original side to 11203 * run 11204 */ 11205 union ctl_ha_msg msg_info; 11206 11207 msg_info.hdr.original_sc = 11208 cur_blocked->io_hdr.original_sc; 11209 msg_info.hdr.serializing_sc = cur_blocked; 11210 msg_info.hdr.msg_type = CTL_MSG_R2R; 11211 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11212 &msg_info, sizeof(msg_info), 0)) > 11213 CTL_HA_STATUS_SUCCESS) { 11214 printf("CTL:Check Blocked error from " 11215 "ctl_ha_msg_send %d\n", 11216 isc_retval); 11217 } 11218 break; 11219 } 11220 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11221 softc = control_softc; 11222 11223 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11224 11225 /* 11226 * Check this I/O for LUN state changes that may 11227 * have happened while this command was blocked. 11228 * The LUN state may have been changed by a command 11229 * ahead of us in the queue, so we need to re-check 11230 * for any states that can be caused by SCSI 11231 * commands. 11232 */ 11233 if (ctl_scsiio_lun_check(softc, lun, entry, 11234 &cur_blocked->scsiio) == 0) { 11235 cur_blocked->io_hdr.flags |= 11236 CTL_FLAG_IS_WAS_ON_RTR; 11237 ctl_enqueue_rtr(cur_blocked); 11238 } else 11239 ctl_done(cur_blocked); 11240 break; 11241 } 11242 default: 11243 /* 11244 * This probably shouldn't happen -- we shouldn't 11245 * get CTL_ACTION_ERROR, or anything else. 11246 */ 11247 break; 11248 } 11249 } 11250 11251 return (CTL_RETVAL_COMPLETE); 11252} 11253 11254/* 11255 * This routine (with one exception) checks LUN flags that can be set by 11256 * commands ahead of us in the OOA queue. These flags have to be checked 11257 * when a command initially comes in, and when we pull a command off the 11258 * blocked queue and are preparing to execute it. The reason we have to 11259 * check these flags for commands on the blocked queue is that the LUN 11260 * state may have been changed by a command ahead of us while we're on the 11261 * blocked queue. 11262 * 11263 * Ordering is somewhat important with these checks, so please pay 11264 * careful attention to the placement of any new checks. 11265 */ 11266static int 11267ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11268 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11269{ 11270 int retval; 11271 uint32_t residx; 11272 11273 retval = 0; 11274 11275 mtx_assert(&lun->lun_lock, MA_OWNED); 11276 11277 /* 11278 * If this shelf is a secondary shelf controller, we have to reject 11279 * any media access commands. 11280 */ 11281#if 0 11282 /* No longer needed for HA */ 11283 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11284 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11285 ctl_set_lun_standby(ctsio); 11286 retval = 1; 11287 goto bailout; 11288 } 11289#endif 11290 11291 /* 11292 * Check for a reservation conflict. If this command isn't allowed 11293 * even on reserved LUNs, and if this initiator isn't the one who 11294 * reserved us, reject the command with a reservation conflict. 11295 */ 11296 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11297 if ((lun->flags & CTL_LUN_RESERVED) 11298 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11299 if (lun->res_idx != residx) { 11300 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11301 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11302 retval = 1; 11303 goto bailout; 11304 } 11305 } 11306 11307 if ((lun->flags & CTL_LUN_PR_RESERVED) 11308 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11309 /* 11310 * if we aren't registered or it's a res holder type 11311 * reservation and this isn't the res holder then set a 11312 * conflict. 11313 * NOTE: Commands which might be allowed on write exclusive 11314 * type reservations are checked in the particular command 11315 * for a conflict. Read and SSU are the only ones. 11316 */ 11317 if (!lun->per_res[residx].registered 11318 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11319 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11320 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11321 retval = 1; 11322 goto bailout; 11323 } 11324 11325 } 11326 11327 if ((lun->flags & CTL_LUN_OFFLINE) 11328 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11329 ctl_set_lun_not_ready(ctsio); 11330 retval = 1; 11331 goto bailout; 11332 } 11333 11334 /* 11335 * If the LUN is stopped, see if this particular command is allowed 11336 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11337 */ 11338 if ((lun->flags & CTL_LUN_STOPPED) 11339 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11340 /* "Logical unit not ready, initializing cmd. required" */ 11341 ctl_set_lun_stopped(ctsio); 11342 retval = 1; 11343 goto bailout; 11344 } 11345 11346 if ((lun->flags & CTL_LUN_INOPERABLE) 11347 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11348 /* "Medium format corrupted" */ 11349 ctl_set_medium_format_corrupted(ctsio); 11350 retval = 1; 11351 goto bailout; 11352 } 11353 11354bailout: 11355 return (retval); 11356 11357} 11358 11359static void 11360ctl_failover_io(union ctl_io *io, int have_lock) 11361{ 11362 ctl_set_busy(&io->scsiio); 11363 ctl_done(io); 11364} 11365 11366static void 11367ctl_failover(void) 11368{ 11369 struct ctl_lun *lun; 11370 struct ctl_softc *ctl_softc; 11371 union ctl_io *next_io, *pending_io; 11372 union ctl_io *io; 11373 int lun_idx; 11374 int i; 11375 11376 ctl_softc = control_softc; 11377 11378 mtx_lock(&ctl_softc->ctl_lock); 11379 /* 11380 * Remove any cmds from the other SC from the rtr queue. These 11381 * will obviously only be for LUNs for which we're the primary. 11382 * We can't send status or get/send data for these commands. 11383 * Since they haven't been executed yet, we can just remove them. 11384 * We'll either abort them or delete them below, depending on 11385 * which HA mode we're in. 11386 */ 11387#ifdef notyet 11388 mtx_lock(&ctl_softc->queue_lock); 11389 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11390 io != NULL; io = next_io) { 11391 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11392 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11393 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11394 ctl_io_hdr, links); 11395 } 11396 mtx_unlock(&ctl_softc->queue_lock); 11397#endif 11398 11399 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11400 lun = ctl_softc->ctl_luns[lun_idx]; 11401 if (lun==NULL) 11402 continue; 11403 11404 /* 11405 * Processor LUNs are primary on both sides. 11406 * XXX will this always be true? 11407 */ 11408 if (lun->be_lun->lun_type == T_PROCESSOR) 11409 continue; 11410 11411 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11412 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11413 printf("FAILOVER: primary lun %d\n", lun_idx); 11414 /* 11415 * Remove all commands from the other SC. First from the 11416 * blocked queue then from the ooa queue. Once we have 11417 * removed them. Call ctl_check_blocked to see if there 11418 * is anything that can run. 11419 */ 11420 for (io = (union ctl_io *)TAILQ_FIRST( 11421 &lun->blocked_queue); io != NULL; io = next_io) { 11422 11423 next_io = (union ctl_io *)TAILQ_NEXT( 11424 &io->io_hdr, blocked_links); 11425 11426 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11427 TAILQ_REMOVE(&lun->blocked_queue, 11428 &io->io_hdr,blocked_links); 11429 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11430 TAILQ_REMOVE(&lun->ooa_queue, 11431 &io->io_hdr, ooa_links); 11432 11433 ctl_free_io(io); 11434 } 11435 } 11436 11437 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11438 io != NULL; io = next_io) { 11439 11440 next_io = (union ctl_io *)TAILQ_NEXT( 11441 &io->io_hdr, ooa_links); 11442 11443 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11444 11445 TAILQ_REMOVE(&lun->ooa_queue, 11446 &io->io_hdr, 11447 ooa_links); 11448 11449 ctl_free_io(io); 11450 } 11451 } 11452 ctl_check_blocked(lun); 11453 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11454 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11455 11456 printf("FAILOVER: primary lun %d\n", lun_idx); 11457 /* 11458 * Abort all commands from the other SC. We can't 11459 * send status back for them now. These should get 11460 * cleaned up when they are completed or come out 11461 * for a datamove operation. 11462 */ 11463 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11464 io != NULL; io = next_io) { 11465 next_io = (union ctl_io *)TAILQ_NEXT( 11466 &io->io_hdr, ooa_links); 11467 11468 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11469 io->io_hdr.flags |= CTL_FLAG_ABORT; 11470 } 11471 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11472 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11473 11474 printf("FAILOVER: secondary lun %d\n", lun_idx); 11475 11476 lun->flags |= CTL_LUN_PRIMARY_SC; 11477 11478 /* 11479 * We send all I/O that was sent to this controller 11480 * and redirected to the other side back with 11481 * busy status, and have the initiator retry it. 11482 * Figuring out how much data has been transferred, 11483 * etc. and picking up where we left off would be 11484 * very tricky. 11485 * 11486 * XXX KDM need to remove I/O from the blocked 11487 * queue as well! 11488 */ 11489 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11490 &lun->ooa_queue); pending_io != NULL; 11491 pending_io = next_io) { 11492 11493 next_io = (union ctl_io *)TAILQ_NEXT( 11494 &pending_io->io_hdr, ooa_links); 11495 11496 pending_io->io_hdr.flags &= 11497 ~CTL_FLAG_SENT_2OTHER_SC; 11498 11499 if (pending_io->io_hdr.flags & 11500 CTL_FLAG_IO_ACTIVE) { 11501 pending_io->io_hdr.flags |= 11502 CTL_FLAG_FAILOVER; 11503 } else { 11504 ctl_set_busy(&pending_io->scsiio); 11505 ctl_done(pending_io); 11506 } 11507 } 11508 11509 /* 11510 * Build Unit Attention 11511 */ 11512 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11513 lun->pending_ua[i] |= 11514 CTL_UA_ASYM_ACC_CHANGE; 11515 } 11516 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11517 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11518 printf("FAILOVER: secondary lun %d\n", lun_idx); 11519 /* 11520 * if the first io on the OOA is not on the RtR queue 11521 * add it. 11522 */ 11523 lun->flags |= CTL_LUN_PRIMARY_SC; 11524 11525 pending_io = (union ctl_io *)TAILQ_FIRST( 11526 &lun->ooa_queue); 11527 if (pending_io==NULL) { 11528 printf("Nothing on OOA queue\n"); 11529 continue; 11530 } 11531 11532 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11533 if ((pending_io->io_hdr.flags & 11534 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11535 pending_io->io_hdr.flags |= 11536 CTL_FLAG_IS_WAS_ON_RTR; 11537 ctl_enqueue_rtr(pending_io); 11538 } 11539#if 0 11540 else 11541 { 11542 printf("Tag 0x%04x is running\n", 11543 pending_io->scsiio.tag_num); 11544 } 11545#endif 11546 11547 next_io = (union ctl_io *)TAILQ_NEXT( 11548 &pending_io->io_hdr, ooa_links); 11549 for (pending_io=next_io; pending_io != NULL; 11550 pending_io = next_io) { 11551 pending_io->io_hdr.flags &= 11552 ~CTL_FLAG_SENT_2OTHER_SC; 11553 next_io = (union ctl_io *)TAILQ_NEXT( 11554 &pending_io->io_hdr, ooa_links); 11555 if (pending_io->io_hdr.flags & 11556 CTL_FLAG_IS_WAS_ON_RTR) { 11557#if 0 11558 printf("Tag 0x%04x is running\n", 11559 pending_io->scsiio.tag_num); 11560#endif 11561 continue; 11562 } 11563 11564 switch (ctl_check_ooa(lun, pending_io, 11565 (union ctl_io *)TAILQ_PREV( 11566 &pending_io->io_hdr, ctl_ooaq, 11567 ooa_links))) { 11568 11569 case CTL_ACTION_BLOCK: 11570 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11571 &pending_io->io_hdr, 11572 blocked_links); 11573 pending_io->io_hdr.flags |= 11574 CTL_FLAG_BLOCKED; 11575 break; 11576 case CTL_ACTION_PASS: 11577 case CTL_ACTION_SKIP: 11578 pending_io->io_hdr.flags |= 11579 CTL_FLAG_IS_WAS_ON_RTR; 11580 ctl_enqueue_rtr(pending_io); 11581 break; 11582 case CTL_ACTION_OVERLAP: 11583 ctl_set_overlapped_cmd( 11584 (struct ctl_scsiio *)pending_io); 11585 ctl_done(pending_io); 11586 break; 11587 case CTL_ACTION_OVERLAP_TAG: 11588 ctl_set_overlapped_tag( 11589 (struct ctl_scsiio *)pending_io, 11590 pending_io->scsiio.tag_num & 0xff); 11591 ctl_done(pending_io); 11592 break; 11593 case CTL_ACTION_ERROR: 11594 default: 11595 ctl_set_internal_failure( 11596 (struct ctl_scsiio *)pending_io, 11597 0, // sks_valid 11598 0); //retry count 11599 ctl_done(pending_io); 11600 break; 11601 } 11602 } 11603 11604 /* 11605 * Build Unit Attention 11606 */ 11607 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11608 lun->pending_ua[i] |= 11609 CTL_UA_ASYM_ACC_CHANGE; 11610 } 11611 } else { 11612 panic("Unhandled HA mode failover, LUN flags = %#x, " 11613 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11614 } 11615 } 11616 ctl_pause_rtr = 0; 11617 mtx_unlock(&ctl_softc->ctl_lock); 11618} 11619 11620static int 11621ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11622{ 11623 struct ctl_lun *lun; 11624 const struct ctl_cmd_entry *entry; 11625 uint32_t initidx, targ_lun; 11626 int retval; 11627 11628 retval = 0; 11629 11630 lun = NULL; 11631 11632 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11633 if ((targ_lun < CTL_MAX_LUNS) 11634 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11635 lun = ctl_softc->ctl_luns[targ_lun]; 11636 /* 11637 * If the LUN is invalid, pretend that it doesn't exist. 11638 * It will go away as soon as all pending I/O has been 11639 * completed. 11640 */ 11641 if (lun->flags & CTL_LUN_DISABLED) { 11642 lun = NULL; 11643 } else { 11644 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11645 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11646 lun->be_lun; 11647 if (lun->be_lun->lun_type == T_PROCESSOR) { 11648 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11649 } 11650 11651 /* 11652 * Every I/O goes into the OOA queue for a 11653 * particular LUN, and stays there until completion. 11654 */ 11655 mtx_lock(&lun->lun_lock); 11656 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11657 ooa_links); 11658 } 11659 } else { 11660 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11661 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11662 } 11663 11664 /* Get command entry and return error if it is unsuppotyed. */ 11665 entry = ctl_validate_command(ctsio); 11666 if (entry == NULL) { 11667 if (lun) 11668 mtx_unlock(&lun->lun_lock); 11669 return (retval); 11670 } 11671 11672 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11673 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11674 11675 /* 11676 * Check to see whether we can send this command to LUNs that don't 11677 * exist. This should pretty much only be the case for inquiry 11678 * and request sense. Further checks, below, really require having 11679 * a LUN, so we can't really check the command anymore. Just put 11680 * it on the rtr queue. 11681 */ 11682 if (lun == NULL) { 11683 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11684 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11685 ctl_enqueue_rtr((union ctl_io *)ctsio); 11686 return (retval); 11687 } 11688 11689 ctl_set_unsupported_lun(ctsio); 11690 ctl_done((union ctl_io *)ctsio); 11691 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11692 return (retval); 11693 } else { 11694 /* 11695 * Make sure we support this particular command on this LUN. 11696 * e.g., we don't support writes to the control LUN. 11697 */ 11698 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11699 mtx_unlock(&lun->lun_lock); 11700 ctl_set_invalid_opcode(ctsio); 11701 ctl_done((union ctl_io *)ctsio); 11702 return (retval); 11703 } 11704 } 11705 11706 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11707 11708#ifdef CTL_WITH_CA 11709 /* 11710 * If we've got a request sense, it'll clear the contingent 11711 * allegiance condition. Otherwise, if we have a CA condition for 11712 * this initiator, clear it, because it sent down a command other 11713 * than request sense. 11714 */ 11715 if ((ctsio->cdb[0] != REQUEST_SENSE) 11716 && (ctl_is_set(lun->have_ca, initidx))) 11717 ctl_clear_mask(lun->have_ca, initidx); 11718#endif 11719 11720 /* 11721 * If the command has this flag set, it handles its own unit 11722 * attention reporting, we shouldn't do anything. Otherwise we 11723 * check for any pending unit attentions, and send them back to the 11724 * initiator. We only do this when a command initially comes in, 11725 * not when we pull it off the blocked queue. 11726 * 11727 * According to SAM-3, section 5.3.2, the order that things get 11728 * presented back to the host is basically unit attentions caused 11729 * by some sort of reset event, busy status, reservation conflicts 11730 * or task set full, and finally any other status. 11731 * 11732 * One issue here is that some of the unit attentions we report 11733 * don't fall into the "reset" category (e.g. "reported luns data 11734 * has changed"). So reporting it here, before the reservation 11735 * check, may be technically wrong. I guess the only thing to do 11736 * would be to check for and report the reset events here, and then 11737 * check for the other unit attention types after we check for a 11738 * reservation conflict. 11739 * 11740 * XXX KDM need to fix this 11741 */ 11742 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11743 ctl_ua_type ua_type; 11744 11745 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11746 scsi_sense_data_type sense_format; 11747 11748 if (lun != NULL) 11749 sense_format = (lun->flags & 11750 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11751 SSD_TYPE_FIXED; 11752 else 11753 sense_format = SSD_TYPE_FIXED; 11754 11755 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11756 &ctsio->sense_data, sense_format); 11757 if (ua_type != CTL_UA_NONE) { 11758 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11759 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11760 CTL_AUTOSENSE; 11761 ctsio->sense_len = SSD_FULL_SIZE; 11762 mtx_unlock(&lun->lun_lock); 11763 ctl_done((union ctl_io *)ctsio); 11764 return (retval); 11765 } 11766 } 11767 } 11768 11769 11770 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11771 mtx_unlock(&lun->lun_lock); 11772 ctl_done((union ctl_io *)ctsio); 11773 return (retval); 11774 } 11775 11776 /* 11777 * XXX CHD this is where we want to send IO to other side if 11778 * this LUN is secondary on this SC. We will need to make a copy 11779 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11780 * the copy we send as FROM_OTHER. 11781 * We also need to stuff the address of the original IO so we can 11782 * find it easily. Something similar will need be done on the other 11783 * side so when we are done we can find the copy. 11784 */ 11785 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11786 union ctl_ha_msg msg_info; 11787 int isc_retval; 11788 11789 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11790 11791 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11792 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11793#if 0 11794 printf("1. ctsio %p\n", ctsio); 11795#endif 11796 msg_info.hdr.serializing_sc = NULL; 11797 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11798 msg_info.scsi.tag_num = ctsio->tag_num; 11799 msg_info.scsi.tag_type = ctsio->tag_type; 11800 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11801 11802 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11803 11804 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11805 (void *)&msg_info, sizeof(msg_info), 0)) > 11806 CTL_HA_STATUS_SUCCESS) { 11807 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11808 isc_retval); 11809 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11810 } else { 11811#if 0 11812 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11813#endif 11814 } 11815 11816 /* 11817 * XXX KDM this I/O is off the incoming queue, but hasn't 11818 * been inserted on any other queue. We may need to come 11819 * up with a holding queue while we wait for serialization 11820 * so that we have an idea of what we're waiting for from 11821 * the other side. 11822 */ 11823 mtx_unlock(&lun->lun_lock); 11824 return (retval); 11825 } 11826 11827 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11828 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11829 ctl_ooaq, ooa_links))) { 11830 case CTL_ACTION_BLOCK: 11831 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11832 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11833 blocked_links); 11834 mtx_unlock(&lun->lun_lock); 11835 return (retval); 11836 case CTL_ACTION_PASS: 11837 case CTL_ACTION_SKIP: 11838 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11839 mtx_unlock(&lun->lun_lock); 11840 ctl_enqueue_rtr((union ctl_io *)ctsio); 11841 break; 11842 case CTL_ACTION_OVERLAP: 11843 mtx_unlock(&lun->lun_lock); 11844 ctl_set_overlapped_cmd(ctsio); 11845 ctl_done((union ctl_io *)ctsio); 11846 break; 11847 case CTL_ACTION_OVERLAP_TAG: 11848 mtx_unlock(&lun->lun_lock); 11849 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11850 ctl_done((union ctl_io *)ctsio); 11851 break; 11852 case CTL_ACTION_ERROR: 11853 default: 11854 mtx_unlock(&lun->lun_lock); 11855 ctl_set_internal_failure(ctsio, 11856 /*sks_valid*/ 0, 11857 /*retry_count*/ 0); 11858 ctl_done((union ctl_io *)ctsio); 11859 break; 11860 } 11861 return (retval); 11862} 11863 11864const struct ctl_cmd_entry * 11865ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11866{ 11867 const struct ctl_cmd_entry *entry; 11868 int service_action; 11869 11870 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11871 if (sa) 11872 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11873 if (entry->flags & CTL_CMD_FLAG_SA5) { 11874 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11875 entry = &((const struct ctl_cmd_entry *) 11876 entry->execute)[service_action]; 11877 } 11878 return (entry); 11879} 11880 11881const struct ctl_cmd_entry * 11882ctl_validate_command(struct ctl_scsiio *ctsio) 11883{ 11884 const struct ctl_cmd_entry *entry; 11885 int i, sa; 11886 uint8_t diff; 11887 11888 entry = ctl_get_cmd_entry(ctsio, &sa); 11889 if (entry->execute == NULL) { 11890 if (sa) 11891 ctl_set_invalid_field(ctsio, 11892 /*sks_valid*/ 1, 11893 /*command*/ 1, 11894 /*field*/ 1, 11895 /*bit_valid*/ 1, 11896 /*bit*/ 4); 11897 else 11898 ctl_set_invalid_opcode(ctsio); 11899 ctl_done((union ctl_io *)ctsio); 11900 return (NULL); 11901 } 11902 KASSERT(entry->length > 0, 11903 ("Not defined length for command 0x%02x/0x%02x", 11904 ctsio->cdb[0], ctsio->cdb[1])); 11905 for (i = 1; i < entry->length; i++) { 11906 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11907 if (diff == 0) 11908 continue; 11909 ctl_set_invalid_field(ctsio, 11910 /*sks_valid*/ 1, 11911 /*command*/ 1, 11912 /*field*/ i, 11913 /*bit_valid*/ 1, 11914 /*bit*/ fls(diff) - 1); 11915 ctl_done((union ctl_io *)ctsio); 11916 return (NULL); 11917 } 11918 return (entry); 11919} 11920 11921static int 11922ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11923{ 11924 11925 switch (lun_type) { 11926 case T_PROCESSOR: 11927 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11928 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11929 return (0); 11930 break; 11931 case T_DIRECT: 11932 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11933 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11934 return (0); 11935 break; 11936 default: 11937 return (0); 11938 } 11939 return (1); 11940} 11941 11942static int 11943ctl_scsiio(struct ctl_scsiio *ctsio) 11944{ 11945 int retval; 11946 const struct ctl_cmd_entry *entry; 11947 11948 retval = CTL_RETVAL_COMPLETE; 11949 11950 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11951 11952 entry = ctl_get_cmd_entry(ctsio, NULL); 11953 11954 /* 11955 * If this I/O has been aborted, just send it straight to 11956 * ctl_done() without executing it. 11957 */ 11958 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11959 ctl_done((union ctl_io *)ctsio); 11960 goto bailout; 11961 } 11962 11963 /* 11964 * All the checks should have been handled by ctl_scsiio_precheck(). 11965 * We should be clear now to just execute the I/O. 11966 */ 11967 retval = entry->execute(ctsio); 11968 11969bailout: 11970 return (retval); 11971} 11972 11973/* 11974 * Since we only implement one target right now, a bus reset simply resets 11975 * our single target. 11976 */ 11977static int 11978ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11979{ 11980 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11981} 11982 11983static int 11984ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11985 ctl_ua_type ua_type) 11986{ 11987 struct ctl_lun *lun; 11988 int retval; 11989 11990 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11991 union ctl_ha_msg msg_info; 11992 11993 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11994 msg_info.hdr.nexus = io->io_hdr.nexus; 11995 if (ua_type==CTL_UA_TARG_RESET) 11996 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11997 else 11998 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11999 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12000 msg_info.hdr.original_sc = NULL; 12001 msg_info.hdr.serializing_sc = NULL; 12002 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12003 (void *)&msg_info, sizeof(msg_info), 0)) { 12004 } 12005 } 12006 retval = 0; 12007 12008 mtx_lock(&ctl_softc->ctl_lock); 12009 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 12010 retval += ctl_lun_reset(lun, io, ua_type); 12011 mtx_unlock(&ctl_softc->ctl_lock); 12012 12013 return (retval); 12014} 12015 12016/* 12017 * The LUN should always be set. The I/O is optional, and is used to 12018 * distinguish between I/Os sent by this initiator, and by other 12019 * initiators. We set unit attention for initiators other than this one. 12020 * SAM-3 is vague on this point. It does say that a unit attention should 12021 * be established for other initiators when a LUN is reset (see section 12022 * 5.7.3), but it doesn't specifically say that the unit attention should 12023 * be established for this particular initiator when a LUN is reset. Here 12024 * is the relevant text, from SAM-3 rev 8: 12025 * 12026 * 5.7.2 When a SCSI initiator port aborts its own tasks 12027 * 12028 * When a SCSI initiator port causes its own task(s) to be aborted, no 12029 * notification that the task(s) have been aborted shall be returned to 12030 * the SCSI initiator port other than the completion response for the 12031 * command or task management function action that caused the task(s) to 12032 * be aborted and notification(s) associated with related effects of the 12033 * action (e.g., a reset unit attention condition). 12034 * 12035 * XXX KDM for now, we're setting unit attention for all initiators. 12036 */ 12037static int 12038ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12039{ 12040 union ctl_io *xio; 12041#if 0 12042 uint32_t initindex; 12043#endif 12044 int i; 12045 12046 mtx_lock(&lun->lun_lock); 12047 /* 12048 * Run through the OOA queue and abort each I/O. 12049 */ 12050#if 0 12051 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12052#endif 12053 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12054 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12055 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12056 } 12057 12058 /* 12059 * This version sets unit attention for every 12060 */ 12061#if 0 12062 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12063 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12064 if (initindex == i) 12065 continue; 12066 lun->pending_ua[i] |= ua_type; 12067 } 12068#endif 12069 12070 /* 12071 * A reset (any kind, really) clears reservations established with 12072 * RESERVE/RELEASE. It does not clear reservations established 12073 * with PERSISTENT RESERVE OUT, but we don't support that at the 12074 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12075 * reservations made with the RESERVE/RELEASE commands, because 12076 * those commands are obsolete in SPC-3. 12077 */ 12078 lun->flags &= ~CTL_LUN_RESERVED; 12079 12080 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12081#ifdef CTL_WITH_CA 12082 ctl_clear_mask(lun->have_ca, i); 12083#endif 12084 lun->pending_ua[i] |= ua_type; 12085 } 12086 mtx_unlock(&lun->lun_lock); 12087 12088 return (0); 12089} 12090 12091static void 12092ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12093 int other_sc) 12094{ 12095 union ctl_io *xio; 12096 12097 mtx_assert(&lun->lun_lock, MA_OWNED); 12098 12099 /* 12100 * Run through the OOA queue and attempt to find the given I/O. 12101 * The target port, initiator ID, tag type and tag number have to 12102 * match the values that we got from the initiator. If we have an 12103 * untagged command to abort, simply abort the first untagged command 12104 * we come to. We only allow one untagged command at a time of course. 12105 */ 12106 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12107 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12108 12109 if ((targ_port == UINT32_MAX || 12110 targ_port == xio->io_hdr.nexus.targ_port) && 12111 (init_id == UINT32_MAX || 12112 init_id == xio->io_hdr.nexus.initid.id)) { 12113 if (targ_port != xio->io_hdr.nexus.targ_port || 12114 init_id != xio->io_hdr.nexus.initid.id) 12115 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12116 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12117 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12118 union ctl_ha_msg msg_info; 12119 12120 msg_info.hdr.nexus = xio->io_hdr.nexus; 12121 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12122 msg_info.task.tag_num = xio->scsiio.tag_num; 12123 msg_info.task.tag_type = xio->scsiio.tag_type; 12124 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12125 msg_info.hdr.original_sc = NULL; 12126 msg_info.hdr.serializing_sc = NULL; 12127 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12128 (void *)&msg_info, sizeof(msg_info), 0); 12129 } 12130 } 12131 } 12132} 12133 12134static int 12135ctl_abort_task_set(union ctl_io *io) 12136{ 12137 struct ctl_softc *softc = control_softc; 12138 struct ctl_lun *lun; 12139 uint32_t targ_lun; 12140 12141 /* 12142 * Look up the LUN. 12143 */ 12144 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12145 mtx_lock(&softc->ctl_lock); 12146 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12147 lun = softc->ctl_luns[targ_lun]; 12148 else { 12149 mtx_unlock(&softc->ctl_lock); 12150 return (1); 12151 } 12152 12153 mtx_lock(&lun->lun_lock); 12154 mtx_unlock(&softc->ctl_lock); 12155 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12156 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12157 io->io_hdr.nexus.initid.id, 12158 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12159 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12160 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12161 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12162 } 12163 mtx_unlock(&lun->lun_lock); 12164 return (0); 12165} 12166 12167static int 12168ctl_i_t_nexus_reset(union ctl_io *io) 12169{ 12170 struct ctl_softc *softc = control_softc; 12171 struct ctl_lun *lun; 12172 uint32_t initindex, residx; 12173 12174 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12175 residx = ctl_get_resindex(&io->io_hdr.nexus); 12176 mtx_lock(&softc->ctl_lock); 12177 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12178 mtx_lock(&lun->lun_lock); 12179 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12180 io->io_hdr.nexus.initid.id, 12181 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12182#ifdef CTL_WITH_CA 12183 ctl_clear_mask(lun->have_ca, initindex); 12184#endif 12185 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12186 lun->flags &= ~CTL_LUN_RESERVED; 12187 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12188 mtx_unlock(&lun->lun_lock); 12189 } 12190 mtx_unlock(&softc->ctl_lock); 12191 return (0); 12192} 12193 12194static int 12195ctl_abort_task(union ctl_io *io) 12196{ 12197 union ctl_io *xio; 12198 struct ctl_lun *lun; 12199 struct ctl_softc *ctl_softc; 12200#if 0 12201 struct sbuf sb; 12202 char printbuf[128]; 12203#endif 12204 int found; 12205 uint32_t targ_lun; 12206 12207 ctl_softc = control_softc; 12208 found = 0; 12209 12210 /* 12211 * Look up the LUN. 12212 */ 12213 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12214 mtx_lock(&ctl_softc->ctl_lock); 12215 if ((targ_lun < CTL_MAX_LUNS) 12216 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12217 lun = ctl_softc->ctl_luns[targ_lun]; 12218 else { 12219 mtx_unlock(&ctl_softc->ctl_lock); 12220 return (1); 12221 } 12222 12223#if 0 12224 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12225 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12226#endif 12227 12228 mtx_lock(&lun->lun_lock); 12229 mtx_unlock(&ctl_softc->ctl_lock); 12230 /* 12231 * Run through the OOA queue and attempt to find the given I/O. 12232 * The target port, initiator ID, tag type and tag number have to 12233 * match the values that we got from the initiator. If we have an 12234 * untagged command to abort, simply abort the first untagged command 12235 * we come to. We only allow one untagged command at a time of course. 12236 */ 12237#if 0 12238 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12239#endif 12240 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12241 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12242#if 0 12243 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12244 12245 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12246 lun->lun, xio->scsiio.tag_num, 12247 xio->scsiio.tag_type, 12248 (xio->io_hdr.blocked_links.tqe_prev 12249 == NULL) ? "" : " BLOCKED", 12250 (xio->io_hdr.flags & 12251 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12252 (xio->io_hdr.flags & 12253 CTL_FLAG_ABORT) ? " ABORT" : "", 12254 (xio->io_hdr.flags & 12255 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12256 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12257 sbuf_finish(&sb); 12258 printf("%s\n", sbuf_data(&sb)); 12259#endif 12260 12261 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12262 && (xio->io_hdr.nexus.initid.id == 12263 io->io_hdr.nexus.initid.id)) { 12264 /* 12265 * If the abort says that the task is untagged, the 12266 * task in the queue must be untagged. Otherwise, 12267 * we just check to see whether the tag numbers 12268 * match. This is because the QLogic firmware 12269 * doesn't pass back the tag type in an abort 12270 * request. 12271 */ 12272#if 0 12273 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12274 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12275 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12276#endif 12277 /* 12278 * XXX KDM we've got problems with FC, because it 12279 * doesn't send down a tag type with aborts. So we 12280 * can only really go by the tag number... 12281 * This may cause problems with parallel SCSI. 12282 * Need to figure that out!! 12283 */ 12284 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12285 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12286 found = 1; 12287 if ((io->io_hdr.flags & 12288 CTL_FLAG_FROM_OTHER_SC) == 0 && 12289 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12290 union ctl_ha_msg msg_info; 12291 12292 io->io_hdr.flags |= 12293 CTL_FLAG_SENT_2OTHER_SC; 12294 msg_info.hdr.nexus = io->io_hdr.nexus; 12295 msg_info.task.task_action = 12296 CTL_TASK_ABORT_TASK; 12297 msg_info.task.tag_num = 12298 io->taskio.tag_num; 12299 msg_info.task.tag_type = 12300 io->taskio.tag_type; 12301 msg_info.hdr.msg_type = 12302 CTL_MSG_MANAGE_TASKS; 12303 msg_info.hdr.original_sc = NULL; 12304 msg_info.hdr.serializing_sc = NULL; 12305#if 0 12306 printf("Sent Abort to other side\n"); 12307#endif 12308 if (CTL_HA_STATUS_SUCCESS != 12309 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12310 (void *)&msg_info, 12311 sizeof(msg_info), 0)) { 12312 } 12313 } 12314#if 0 12315 printf("ctl_abort_task: found I/O to abort\n"); 12316#endif 12317 break; 12318 } 12319 } 12320 } 12321 mtx_unlock(&lun->lun_lock); 12322 12323 if (found == 0) { 12324 /* 12325 * This isn't really an error. It's entirely possible for 12326 * the abort and command completion to cross on the wire. 12327 * This is more of an informative/diagnostic error. 12328 */ 12329#if 0 12330 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12331 "%d:%d:%d:%d tag %d type %d\n", 12332 io->io_hdr.nexus.initid.id, 12333 io->io_hdr.nexus.targ_port, 12334 io->io_hdr.nexus.targ_target.id, 12335 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12336 io->taskio.tag_type); 12337#endif 12338 } 12339 return (0); 12340} 12341 12342static void 12343ctl_run_task(union ctl_io *io) 12344{ 12345 struct ctl_softc *ctl_softc = control_softc; 12346 int retval = 1; 12347 const char *task_desc; 12348 12349 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12350 12351 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12352 ("ctl_run_task: Unextected io_type %d\n", 12353 io->io_hdr.io_type)); 12354 12355 task_desc = ctl_scsi_task_string(&io->taskio); 12356 if (task_desc != NULL) { 12357#ifdef NEEDTOPORT 12358 csevent_log(CSC_CTL | CSC_SHELF_SW | 12359 CTL_TASK_REPORT, 12360 csevent_LogType_Trace, 12361 csevent_Severity_Information, 12362 csevent_AlertLevel_Green, 12363 csevent_FRU_Firmware, 12364 csevent_FRU_Unknown, 12365 "CTL: received task: %s",task_desc); 12366#endif 12367 } else { 12368#ifdef NEEDTOPORT 12369 csevent_log(CSC_CTL | CSC_SHELF_SW | 12370 CTL_TASK_REPORT, 12371 csevent_LogType_Trace, 12372 csevent_Severity_Information, 12373 csevent_AlertLevel_Green, 12374 csevent_FRU_Firmware, 12375 csevent_FRU_Unknown, 12376 "CTL: received unknown task " 12377 "type: %d (%#x)", 12378 io->taskio.task_action, 12379 io->taskio.task_action); 12380#endif 12381 } 12382 switch (io->taskio.task_action) { 12383 case CTL_TASK_ABORT_TASK: 12384 retval = ctl_abort_task(io); 12385 break; 12386 case CTL_TASK_ABORT_TASK_SET: 12387 case CTL_TASK_CLEAR_TASK_SET: 12388 retval = ctl_abort_task_set(io); 12389 break; 12390 case CTL_TASK_CLEAR_ACA: 12391 break; 12392 case CTL_TASK_I_T_NEXUS_RESET: 12393 retval = ctl_i_t_nexus_reset(io); 12394 break; 12395 case CTL_TASK_LUN_RESET: { 12396 struct ctl_lun *lun; 12397 uint32_t targ_lun; 12398 12399 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12400 mtx_lock(&ctl_softc->ctl_lock); 12401 if ((targ_lun < CTL_MAX_LUNS) 12402 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12403 lun = ctl_softc->ctl_luns[targ_lun]; 12404 else { 12405 mtx_unlock(&ctl_softc->ctl_lock); 12406 retval = 1; 12407 break; 12408 } 12409 12410 if (!(io->io_hdr.flags & 12411 CTL_FLAG_FROM_OTHER_SC)) { 12412 union ctl_ha_msg msg_info; 12413 12414 io->io_hdr.flags |= 12415 CTL_FLAG_SENT_2OTHER_SC; 12416 msg_info.hdr.msg_type = 12417 CTL_MSG_MANAGE_TASKS; 12418 msg_info.hdr.nexus = io->io_hdr.nexus; 12419 msg_info.task.task_action = 12420 CTL_TASK_LUN_RESET; 12421 msg_info.hdr.original_sc = NULL; 12422 msg_info.hdr.serializing_sc = NULL; 12423 if (CTL_HA_STATUS_SUCCESS != 12424 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12425 (void *)&msg_info, 12426 sizeof(msg_info), 0)) { 12427 } 12428 } 12429 12430 retval = ctl_lun_reset(lun, io, 12431 CTL_UA_LUN_RESET); 12432 mtx_unlock(&ctl_softc->ctl_lock); 12433 break; 12434 } 12435 case CTL_TASK_TARGET_RESET: 12436 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12437 break; 12438 case CTL_TASK_BUS_RESET: 12439 retval = ctl_bus_reset(ctl_softc, io); 12440 break; 12441 case CTL_TASK_PORT_LOGIN: 12442 break; 12443 case CTL_TASK_PORT_LOGOUT: 12444 break; 12445 default: 12446 printf("ctl_run_task: got unknown task management event %d\n", 12447 io->taskio.task_action); 12448 break; 12449 } 12450 if (retval == 0) 12451 io->io_hdr.status = CTL_SUCCESS; 12452 else 12453 io->io_hdr.status = CTL_ERROR; 12454 ctl_done(io); 12455} 12456 12457/* 12458 * For HA operation. Handle commands that come in from the other 12459 * controller. 12460 */ 12461static void 12462ctl_handle_isc(union ctl_io *io) 12463{ 12464 int free_io; 12465 struct ctl_lun *lun; 12466 struct ctl_softc *ctl_softc; 12467 uint32_t targ_lun; 12468 12469 ctl_softc = control_softc; 12470 12471 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12472 lun = ctl_softc->ctl_luns[targ_lun]; 12473 12474 switch (io->io_hdr.msg_type) { 12475 case CTL_MSG_SERIALIZE: 12476 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12477 break; 12478 case CTL_MSG_R2R: { 12479 const struct ctl_cmd_entry *entry; 12480 12481 /* 12482 * This is only used in SER_ONLY mode. 12483 */ 12484 free_io = 0; 12485 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12486 mtx_lock(&lun->lun_lock); 12487 if (ctl_scsiio_lun_check(ctl_softc, lun, 12488 entry, (struct ctl_scsiio *)io) != 0) { 12489 mtx_unlock(&lun->lun_lock); 12490 ctl_done(io); 12491 break; 12492 } 12493 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12494 mtx_unlock(&lun->lun_lock); 12495 ctl_enqueue_rtr(io); 12496 break; 12497 } 12498 case CTL_MSG_FINISH_IO: 12499 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12500 free_io = 0; 12501 ctl_done(io); 12502 } else { 12503 free_io = 1; 12504 mtx_lock(&lun->lun_lock); 12505 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12506 ooa_links); 12507 ctl_check_blocked(lun); 12508 mtx_unlock(&lun->lun_lock); 12509 } 12510 break; 12511 case CTL_MSG_PERS_ACTION: 12512 ctl_hndl_per_res_out_on_other_sc( 12513 (union ctl_ha_msg *)&io->presio.pr_msg); 12514 free_io = 1; 12515 break; 12516 case CTL_MSG_BAD_JUJU: 12517 free_io = 0; 12518 ctl_done(io); 12519 break; 12520 case CTL_MSG_DATAMOVE: 12521 /* Only used in XFER mode */ 12522 free_io = 0; 12523 ctl_datamove_remote(io); 12524 break; 12525 case CTL_MSG_DATAMOVE_DONE: 12526 /* Only used in XFER mode */ 12527 free_io = 0; 12528 io->scsiio.be_move_done(io); 12529 break; 12530 default: 12531 free_io = 1; 12532 printf("%s: Invalid message type %d\n", 12533 __func__, io->io_hdr.msg_type); 12534 break; 12535 } 12536 if (free_io) 12537 ctl_free_io(io); 12538 12539} 12540 12541 12542/* 12543 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12544 * there is no match. 12545 */ 12546static ctl_lun_error_pattern 12547ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12548{ 12549 const struct ctl_cmd_entry *entry; 12550 ctl_lun_error_pattern filtered_pattern, pattern; 12551 12552 pattern = desc->error_pattern; 12553 12554 /* 12555 * XXX KDM we need more data passed into this function to match a 12556 * custom pattern, and we actually need to implement custom pattern 12557 * matching. 12558 */ 12559 if (pattern & CTL_LUN_PAT_CMD) 12560 return (CTL_LUN_PAT_CMD); 12561 12562 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12563 return (CTL_LUN_PAT_ANY); 12564 12565 entry = ctl_get_cmd_entry(ctsio, NULL); 12566 12567 filtered_pattern = entry->pattern & pattern; 12568 12569 /* 12570 * If the user requested specific flags in the pattern (e.g. 12571 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12572 * flags. 12573 * 12574 * If the user did not specify any flags, it doesn't matter whether 12575 * or not the command supports the flags. 12576 */ 12577 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12578 (pattern & ~CTL_LUN_PAT_MASK)) 12579 return (CTL_LUN_PAT_NONE); 12580 12581 /* 12582 * If the user asked for a range check, see if the requested LBA 12583 * range overlaps with this command's LBA range. 12584 */ 12585 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12586 uint64_t lba1; 12587 uint64_t len1; 12588 ctl_action action; 12589 int retval; 12590 12591 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12592 if (retval != 0) 12593 return (CTL_LUN_PAT_NONE); 12594 12595 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12596 desc->lba_range.len); 12597 /* 12598 * A "pass" means that the LBA ranges don't overlap, so 12599 * this doesn't match the user's range criteria. 12600 */ 12601 if (action == CTL_ACTION_PASS) 12602 return (CTL_LUN_PAT_NONE); 12603 } 12604 12605 return (filtered_pattern); 12606} 12607 12608static void 12609ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12610{ 12611 struct ctl_error_desc *desc, *desc2; 12612 12613 mtx_assert(&lun->lun_lock, MA_OWNED); 12614 12615 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12616 ctl_lun_error_pattern pattern; 12617 /* 12618 * Check to see whether this particular command matches 12619 * the pattern in the descriptor. 12620 */ 12621 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12622 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12623 continue; 12624 12625 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12626 case CTL_LUN_INJ_ABORTED: 12627 ctl_set_aborted(&io->scsiio); 12628 break; 12629 case CTL_LUN_INJ_MEDIUM_ERR: 12630 ctl_set_medium_error(&io->scsiio); 12631 break; 12632 case CTL_LUN_INJ_UA: 12633 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12634 * OCCURRED */ 12635 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12636 break; 12637 case CTL_LUN_INJ_CUSTOM: 12638 /* 12639 * We're assuming the user knows what he is doing. 12640 * Just copy the sense information without doing 12641 * checks. 12642 */ 12643 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12644 ctl_min(sizeof(desc->custom_sense), 12645 sizeof(io->scsiio.sense_data))); 12646 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12647 io->scsiio.sense_len = SSD_FULL_SIZE; 12648 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12649 break; 12650 case CTL_LUN_INJ_NONE: 12651 default: 12652 /* 12653 * If this is an error injection type we don't know 12654 * about, clear the continuous flag (if it is set) 12655 * so it will get deleted below. 12656 */ 12657 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12658 break; 12659 } 12660 /* 12661 * By default, each error injection action is a one-shot 12662 */ 12663 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12664 continue; 12665 12666 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12667 12668 free(desc, M_CTL); 12669 } 12670} 12671 12672#ifdef CTL_IO_DELAY 12673static void 12674ctl_datamove_timer_wakeup(void *arg) 12675{ 12676 union ctl_io *io; 12677 12678 io = (union ctl_io *)arg; 12679 12680 ctl_datamove(io); 12681} 12682#endif /* CTL_IO_DELAY */ 12683 12684void 12685ctl_datamove(union ctl_io *io) 12686{ 12687 void (*fe_datamove)(union ctl_io *io); 12688 12689 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12690 12691 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12692 12693#ifdef CTL_TIME_IO 12694 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12695 char str[256]; 12696 char path_str[64]; 12697 struct sbuf sb; 12698 12699 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12700 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12701 12702 sbuf_cat(&sb, path_str); 12703 switch (io->io_hdr.io_type) { 12704 case CTL_IO_SCSI: 12705 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12706 sbuf_printf(&sb, "\n"); 12707 sbuf_cat(&sb, path_str); 12708 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12709 io->scsiio.tag_num, io->scsiio.tag_type); 12710 break; 12711 case CTL_IO_TASK: 12712 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12713 "Tag Type: %d\n", io->taskio.task_action, 12714 io->taskio.tag_num, io->taskio.tag_type); 12715 break; 12716 default: 12717 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12718 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12719 break; 12720 } 12721 sbuf_cat(&sb, path_str); 12722 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12723 (intmax_t)time_uptime - io->io_hdr.start_time); 12724 sbuf_finish(&sb); 12725 printf("%s", sbuf_data(&sb)); 12726 } 12727#endif /* CTL_TIME_IO */ 12728 12729#ifdef CTL_IO_DELAY 12730 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12731 struct ctl_lun *lun; 12732 12733 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12734 12735 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12736 } else { 12737 struct ctl_lun *lun; 12738 12739 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12740 if ((lun != NULL) 12741 && (lun->delay_info.datamove_delay > 0)) { 12742 struct callout *callout; 12743 12744 callout = (struct callout *)&io->io_hdr.timer_bytes; 12745 callout_init(callout, /*mpsafe*/ 1); 12746 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12747 callout_reset(callout, 12748 lun->delay_info.datamove_delay * hz, 12749 ctl_datamove_timer_wakeup, io); 12750 if (lun->delay_info.datamove_type == 12751 CTL_DELAY_TYPE_ONESHOT) 12752 lun->delay_info.datamove_delay = 0; 12753 return; 12754 } 12755 } 12756#endif 12757 12758 /* 12759 * This command has been aborted. Set the port status, so we fail 12760 * the data move. 12761 */ 12762 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12763 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12764 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12765 io->io_hdr.nexus.targ_port, 12766 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12767 io->io_hdr.nexus.targ_lun); 12768 io->io_hdr.port_status = 31337; 12769 /* 12770 * Note that the backend, in this case, will get the 12771 * callback in its context. In other cases it may get 12772 * called in the frontend's interrupt thread context. 12773 */ 12774 io->scsiio.be_move_done(io); 12775 return; 12776 } 12777 12778 /* 12779 * If we're in XFER mode and this I/O is from the other shelf 12780 * controller, we need to send the DMA to the other side to 12781 * actually transfer the data to/from the host. In serialize only 12782 * mode the transfer happens below CTL and ctl_datamove() is only 12783 * called on the machine that originally received the I/O. 12784 */ 12785 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12786 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12787 union ctl_ha_msg msg; 12788 uint32_t sg_entries_sent; 12789 int do_sg_copy; 12790 int i; 12791 12792 memset(&msg, 0, sizeof(msg)); 12793 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12794 msg.hdr.original_sc = io->io_hdr.original_sc; 12795 msg.hdr.serializing_sc = io; 12796 msg.hdr.nexus = io->io_hdr.nexus; 12797 msg.dt.flags = io->io_hdr.flags; 12798 /* 12799 * We convert everything into a S/G list here. We can't 12800 * pass by reference, only by value between controllers. 12801 * So we can't pass a pointer to the S/G list, only as many 12802 * S/G entries as we can fit in here. If it's possible for 12803 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12804 * then we need to break this up into multiple transfers. 12805 */ 12806 if (io->scsiio.kern_sg_entries == 0) { 12807 msg.dt.kern_sg_entries = 1; 12808 /* 12809 * If this is in cached memory, flush the cache 12810 * before we send the DMA request to the other 12811 * controller. We want to do this in either the 12812 * read or the write case. The read case is 12813 * straightforward. In the write case, we want to 12814 * make sure nothing is in the local cache that 12815 * could overwrite the DMAed data. 12816 */ 12817 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12818 /* 12819 * XXX KDM use bus_dmamap_sync() here. 12820 */ 12821 } 12822 12823 /* 12824 * Convert to a physical address if this is a 12825 * virtual address. 12826 */ 12827 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12828 msg.dt.sg_list[0].addr = 12829 io->scsiio.kern_data_ptr; 12830 } else { 12831 /* 12832 * XXX KDM use busdma here! 12833 */ 12834#if 0 12835 msg.dt.sg_list[0].addr = (void *) 12836 vtophys(io->scsiio.kern_data_ptr); 12837#endif 12838 } 12839 12840 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12841 do_sg_copy = 0; 12842 } else { 12843 struct ctl_sg_entry *sgl; 12844 12845 do_sg_copy = 1; 12846 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12847 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12848 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12849 /* 12850 * XXX KDM use bus_dmamap_sync() here. 12851 */ 12852 } 12853 } 12854 12855 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12856 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12857 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12858 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12859 msg.dt.sg_sequence = 0; 12860 12861 /* 12862 * Loop until we've sent all of the S/G entries. On the 12863 * other end, we'll recompose these S/G entries into one 12864 * contiguous list before passing it to the 12865 */ 12866 for (sg_entries_sent = 0; sg_entries_sent < 12867 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12868 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12869 sizeof(msg.dt.sg_list[0])), 12870 msg.dt.kern_sg_entries - sg_entries_sent); 12871 12872 if (do_sg_copy != 0) { 12873 struct ctl_sg_entry *sgl; 12874 int j; 12875 12876 sgl = (struct ctl_sg_entry *) 12877 io->scsiio.kern_data_ptr; 12878 /* 12879 * If this is in cached memory, flush the cache 12880 * before we send the DMA request to the other 12881 * controller. We want to do this in either 12882 * the * read or the write case. The read 12883 * case is straightforward. In the write 12884 * case, we want to make sure nothing is 12885 * in the local cache that could overwrite 12886 * the DMAed data. 12887 */ 12888 12889 for (i = sg_entries_sent, j = 0; 12890 i < msg.dt.cur_sg_entries; i++, j++) { 12891 if ((io->io_hdr.flags & 12892 CTL_FLAG_NO_DATASYNC) == 0) { 12893 /* 12894 * XXX KDM use bus_dmamap_sync() 12895 */ 12896 } 12897 if ((io->io_hdr.flags & 12898 CTL_FLAG_BUS_ADDR) == 0) { 12899 /* 12900 * XXX KDM use busdma. 12901 */ 12902#if 0 12903 msg.dt.sg_list[j].addr =(void *) 12904 vtophys(sgl[i].addr); 12905#endif 12906 } else { 12907 msg.dt.sg_list[j].addr = 12908 sgl[i].addr; 12909 } 12910 msg.dt.sg_list[j].len = sgl[i].len; 12911 } 12912 } 12913 12914 sg_entries_sent += msg.dt.cur_sg_entries; 12915 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12916 msg.dt.sg_last = 1; 12917 else 12918 msg.dt.sg_last = 0; 12919 12920 /* 12921 * XXX KDM drop and reacquire the lock here? 12922 */ 12923 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12924 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12925 /* 12926 * XXX do something here. 12927 */ 12928 } 12929 12930 msg.dt.sent_sg_entries = sg_entries_sent; 12931 } 12932 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12933 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12934 ctl_failover_io(io, /*have_lock*/ 0); 12935 12936 } else { 12937 12938 /* 12939 * Lookup the fe_datamove() function for this particular 12940 * front end. 12941 */ 12942 fe_datamove = 12943 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12944 12945 fe_datamove(io); 12946 } 12947} 12948 12949static void 12950ctl_send_datamove_done(union ctl_io *io, int have_lock) 12951{ 12952 union ctl_ha_msg msg; 12953 int isc_status; 12954 12955 memset(&msg, 0, sizeof(msg)); 12956 12957 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12958 msg.hdr.original_sc = io; 12959 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12960 msg.hdr.nexus = io->io_hdr.nexus; 12961 msg.hdr.status = io->io_hdr.status; 12962 msg.scsi.tag_num = io->scsiio.tag_num; 12963 msg.scsi.tag_type = io->scsiio.tag_type; 12964 msg.scsi.scsi_status = io->scsiio.scsi_status; 12965 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12966 sizeof(io->scsiio.sense_data)); 12967 msg.scsi.sense_len = io->scsiio.sense_len; 12968 msg.scsi.sense_residual = io->scsiio.sense_residual; 12969 msg.scsi.fetd_status = io->io_hdr.port_status; 12970 msg.scsi.residual = io->scsiio.residual; 12971 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12972 12973 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12974 ctl_failover_io(io, /*have_lock*/ have_lock); 12975 return; 12976 } 12977 12978 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12979 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12980 /* XXX do something if this fails */ 12981 } 12982 12983} 12984 12985/* 12986 * The DMA to the remote side is done, now we need to tell the other side 12987 * we're done so it can continue with its data movement. 12988 */ 12989static void 12990ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12991{ 12992 union ctl_io *io; 12993 12994 io = rq->context; 12995 12996 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12997 printf("%s: ISC DMA write failed with error %d", __func__, 12998 rq->ret); 12999 ctl_set_internal_failure(&io->scsiio, 13000 /*sks_valid*/ 1, 13001 /*retry_count*/ rq->ret); 13002 } 13003 13004 ctl_dt_req_free(rq); 13005 13006 /* 13007 * In this case, we had to malloc the memory locally. Free it. 13008 */ 13009 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13010 int i; 13011 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13012 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13013 } 13014 /* 13015 * The data is in local and remote memory, so now we need to send 13016 * status (good or back) back to the other side. 13017 */ 13018 ctl_send_datamove_done(io, /*have_lock*/ 0); 13019} 13020 13021/* 13022 * We've moved the data from the host/controller into local memory. Now we 13023 * need to push it over to the remote controller's memory. 13024 */ 13025static int 13026ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13027{ 13028 int retval; 13029 13030 retval = 0; 13031 13032 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13033 ctl_datamove_remote_write_cb); 13034 13035 return (retval); 13036} 13037 13038static void 13039ctl_datamove_remote_write(union ctl_io *io) 13040{ 13041 int retval; 13042 void (*fe_datamove)(union ctl_io *io); 13043 13044 /* 13045 * - Get the data from the host/HBA into local memory. 13046 * - DMA memory from the local controller to the remote controller. 13047 * - Send status back to the remote controller. 13048 */ 13049 13050 retval = ctl_datamove_remote_sgl_setup(io); 13051 if (retval != 0) 13052 return; 13053 13054 /* Switch the pointer over so the FETD knows what to do */ 13055 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13056 13057 /* 13058 * Use a custom move done callback, since we need to send completion 13059 * back to the other controller, not to the backend on this side. 13060 */ 13061 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13062 13063 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13064 13065 fe_datamove(io); 13066 13067 return; 13068 13069} 13070 13071static int 13072ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13073{ 13074#if 0 13075 char str[256]; 13076 char path_str[64]; 13077 struct sbuf sb; 13078#endif 13079 13080 /* 13081 * In this case, we had to malloc the memory locally. Free it. 13082 */ 13083 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13084 int i; 13085 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13086 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13087 } 13088 13089#if 0 13090 scsi_path_string(io, path_str, sizeof(path_str)); 13091 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13092 sbuf_cat(&sb, path_str); 13093 scsi_command_string(&io->scsiio, NULL, &sb); 13094 sbuf_printf(&sb, "\n"); 13095 sbuf_cat(&sb, path_str); 13096 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13097 io->scsiio.tag_num, io->scsiio.tag_type); 13098 sbuf_cat(&sb, path_str); 13099 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13100 io->io_hdr.flags, io->io_hdr.status); 13101 sbuf_finish(&sb); 13102 printk("%s", sbuf_data(&sb)); 13103#endif 13104 13105 13106 /* 13107 * The read is done, now we need to send status (good or bad) back 13108 * to the other side. 13109 */ 13110 ctl_send_datamove_done(io, /*have_lock*/ 0); 13111 13112 return (0); 13113} 13114 13115static void 13116ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13117{ 13118 union ctl_io *io; 13119 void (*fe_datamove)(union ctl_io *io); 13120 13121 io = rq->context; 13122 13123 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13124 printf("%s: ISC DMA read failed with error %d", __func__, 13125 rq->ret); 13126 ctl_set_internal_failure(&io->scsiio, 13127 /*sks_valid*/ 1, 13128 /*retry_count*/ rq->ret); 13129 } 13130 13131 ctl_dt_req_free(rq); 13132 13133 /* Switch the pointer over so the FETD knows what to do */ 13134 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13135 13136 /* 13137 * Use a custom move done callback, since we need to send completion 13138 * back to the other controller, not to the backend on this side. 13139 */ 13140 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13141 13142 /* XXX KDM add checks like the ones in ctl_datamove? */ 13143 13144 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13145 13146 fe_datamove(io); 13147} 13148 13149static int 13150ctl_datamove_remote_sgl_setup(union ctl_io *io) 13151{ 13152 struct ctl_sg_entry *local_sglist, *remote_sglist; 13153 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13154 struct ctl_softc *softc; 13155 int retval; 13156 int i; 13157 13158 retval = 0; 13159 softc = control_softc; 13160 13161 local_sglist = io->io_hdr.local_sglist; 13162 local_dma_sglist = io->io_hdr.local_dma_sglist; 13163 remote_sglist = io->io_hdr.remote_sglist; 13164 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13165 13166 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13167 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13168 local_sglist[i].len = remote_sglist[i].len; 13169 13170 /* 13171 * XXX Detect the situation where the RS-level I/O 13172 * redirector on the other side has already read the 13173 * data off of the AOR RS on this side, and 13174 * transferred it to remote (mirror) memory on the 13175 * other side. Since we already have the data in 13176 * memory here, we just need to use it. 13177 * 13178 * XXX KDM this can probably be removed once we 13179 * get the cache device code in and take the 13180 * current AOR implementation out. 13181 */ 13182#ifdef NEEDTOPORT 13183 if ((remote_sglist[i].addr >= 13184 (void *)vtophys(softc->mirr->addr)) 13185 && (remote_sglist[i].addr < 13186 ((void *)vtophys(softc->mirr->addr) + 13187 CacheMirrorOffset))) { 13188 local_sglist[i].addr = remote_sglist[i].addr - 13189 CacheMirrorOffset; 13190 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13191 CTL_FLAG_DATA_IN) 13192 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13193 } else { 13194 local_sglist[i].addr = remote_sglist[i].addr + 13195 CacheMirrorOffset; 13196 } 13197#endif 13198#if 0 13199 printf("%s: local %p, remote %p, len %d\n", 13200 __func__, local_sglist[i].addr, 13201 remote_sglist[i].addr, local_sglist[i].len); 13202#endif 13203 } 13204 } else { 13205 uint32_t len_to_go; 13206 13207 /* 13208 * In this case, we don't have automatically allocated 13209 * memory for this I/O on this controller. This typically 13210 * happens with internal CTL I/O -- e.g. inquiry, mode 13211 * sense, etc. Anything coming from RAIDCore will have 13212 * a mirror area available. 13213 */ 13214 len_to_go = io->scsiio.kern_data_len; 13215 13216 /* 13217 * Clear the no datasync flag, we have to use malloced 13218 * buffers. 13219 */ 13220 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13221 13222 /* 13223 * The difficult thing here is that the size of the various 13224 * S/G segments may be different than the size from the 13225 * remote controller. That'll make it harder when DMAing 13226 * the data back to the other side. 13227 */ 13228 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13229 sizeof(io->io_hdr.remote_sglist[0])) && 13230 (len_to_go > 0); i++) { 13231 local_sglist[i].len = ctl_min(len_to_go, 131072); 13232 CTL_SIZE_8B(local_dma_sglist[i].len, 13233 local_sglist[i].len); 13234 local_sglist[i].addr = 13235 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13236 13237 local_dma_sglist[i].addr = local_sglist[i].addr; 13238 13239 if (local_sglist[i].addr == NULL) { 13240 int j; 13241 13242 printf("malloc failed for %zd bytes!", 13243 local_dma_sglist[i].len); 13244 for (j = 0; j < i; j++) { 13245 free(local_sglist[j].addr, M_CTL); 13246 } 13247 ctl_set_internal_failure(&io->scsiio, 13248 /*sks_valid*/ 1, 13249 /*retry_count*/ 4857); 13250 retval = 1; 13251 goto bailout_error; 13252 13253 } 13254 /* XXX KDM do we need a sync here? */ 13255 13256 len_to_go -= local_sglist[i].len; 13257 } 13258 /* 13259 * Reset the number of S/G entries accordingly. The 13260 * original number of S/G entries is available in 13261 * rem_sg_entries. 13262 */ 13263 io->scsiio.kern_sg_entries = i; 13264 13265#if 0 13266 printf("%s: kern_sg_entries = %d\n", __func__, 13267 io->scsiio.kern_sg_entries); 13268 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13269 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13270 local_sglist[i].addr, local_sglist[i].len, 13271 local_dma_sglist[i].len); 13272#endif 13273 } 13274 13275 13276 return (retval); 13277 13278bailout_error: 13279 13280 ctl_send_datamove_done(io, /*have_lock*/ 0); 13281 13282 return (retval); 13283} 13284 13285static int 13286ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13287 ctl_ha_dt_cb callback) 13288{ 13289 struct ctl_ha_dt_req *rq; 13290 struct ctl_sg_entry *remote_sglist, *local_sglist; 13291 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13292 uint32_t local_used, remote_used, total_used; 13293 int retval; 13294 int i, j; 13295 13296 retval = 0; 13297 13298 rq = ctl_dt_req_alloc(); 13299 13300 /* 13301 * If we failed to allocate the request, and if the DMA didn't fail 13302 * anyway, set busy status. This is just a resource allocation 13303 * failure. 13304 */ 13305 if ((rq == NULL) 13306 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13307 ctl_set_busy(&io->scsiio); 13308 13309 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13310 13311 if (rq != NULL) 13312 ctl_dt_req_free(rq); 13313 13314 /* 13315 * The data move failed. We need to return status back 13316 * to the other controller. No point in trying to DMA 13317 * data to the remote controller. 13318 */ 13319 13320 ctl_send_datamove_done(io, /*have_lock*/ 0); 13321 13322 retval = 1; 13323 13324 goto bailout; 13325 } 13326 13327 local_sglist = io->io_hdr.local_sglist; 13328 local_dma_sglist = io->io_hdr.local_dma_sglist; 13329 remote_sglist = io->io_hdr.remote_sglist; 13330 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13331 local_used = 0; 13332 remote_used = 0; 13333 total_used = 0; 13334 13335 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13336 rq->ret = CTL_HA_STATUS_SUCCESS; 13337 rq->context = io; 13338 callback(rq); 13339 goto bailout; 13340 } 13341 13342 /* 13343 * Pull/push the data over the wire from/to the other controller. 13344 * This takes into account the possibility that the local and 13345 * remote sglists may not be identical in terms of the size of 13346 * the elements and the number of elements. 13347 * 13348 * One fundamental assumption here is that the length allocated for 13349 * both the local and remote sglists is identical. Otherwise, we've 13350 * essentially got a coding error of some sort. 13351 */ 13352 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13353 int isc_ret; 13354 uint32_t cur_len, dma_length; 13355 uint8_t *tmp_ptr; 13356 13357 rq->id = CTL_HA_DATA_CTL; 13358 rq->command = command; 13359 rq->context = io; 13360 13361 /* 13362 * Both pointers should be aligned. But it is possible 13363 * that the allocation length is not. They should both 13364 * also have enough slack left over at the end, though, 13365 * to round up to the next 8 byte boundary. 13366 */ 13367 cur_len = ctl_min(local_sglist[i].len - local_used, 13368 remote_sglist[j].len - remote_used); 13369 13370 /* 13371 * In this case, we have a size issue and need to decrease 13372 * the size, except in the case where we actually have less 13373 * than 8 bytes left. In that case, we need to increase 13374 * the DMA length to get the last bit. 13375 */ 13376 if ((cur_len & 0x7) != 0) { 13377 if (cur_len > 0x7) { 13378 cur_len = cur_len - (cur_len & 0x7); 13379 dma_length = cur_len; 13380 } else { 13381 CTL_SIZE_8B(dma_length, cur_len); 13382 } 13383 13384 } else 13385 dma_length = cur_len; 13386 13387 /* 13388 * If we had to allocate memory for this I/O, instead of using 13389 * the non-cached mirror memory, we'll need to flush the cache 13390 * before trying to DMA to the other controller. 13391 * 13392 * We could end up doing this multiple times for the same 13393 * segment if we have a larger local segment than remote 13394 * segment. That shouldn't be an issue. 13395 */ 13396 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13397 /* 13398 * XXX KDM use bus_dmamap_sync() here. 13399 */ 13400 } 13401 13402 rq->size = dma_length; 13403 13404 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13405 tmp_ptr += local_used; 13406 13407 /* Use physical addresses when talking to ISC hardware */ 13408 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13409 /* XXX KDM use busdma */ 13410#if 0 13411 rq->local = vtophys(tmp_ptr); 13412#endif 13413 } else 13414 rq->local = tmp_ptr; 13415 13416 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13417 tmp_ptr += remote_used; 13418 rq->remote = tmp_ptr; 13419 13420 rq->callback = NULL; 13421 13422 local_used += cur_len; 13423 if (local_used >= local_sglist[i].len) { 13424 i++; 13425 local_used = 0; 13426 } 13427 13428 remote_used += cur_len; 13429 if (remote_used >= remote_sglist[j].len) { 13430 j++; 13431 remote_used = 0; 13432 } 13433 total_used += cur_len; 13434 13435 if (total_used >= io->scsiio.kern_data_len) 13436 rq->callback = callback; 13437 13438 if ((rq->size & 0x7) != 0) { 13439 printf("%s: warning: size %d is not on 8b boundary\n", 13440 __func__, rq->size); 13441 } 13442 if (((uintptr_t)rq->local & 0x7) != 0) { 13443 printf("%s: warning: local %p not on 8b boundary\n", 13444 __func__, rq->local); 13445 } 13446 if (((uintptr_t)rq->remote & 0x7) != 0) { 13447 printf("%s: warning: remote %p not on 8b boundary\n", 13448 __func__, rq->local); 13449 } 13450#if 0 13451 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13452 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13453 rq->local, rq->remote, rq->size); 13454#endif 13455 13456 isc_ret = ctl_dt_single(rq); 13457 if (isc_ret == CTL_HA_STATUS_WAIT) 13458 continue; 13459 13460 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13461 rq->ret = CTL_HA_STATUS_SUCCESS; 13462 } else { 13463 rq->ret = isc_ret; 13464 } 13465 callback(rq); 13466 goto bailout; 13467 } 13468 13469bailout: 13470 return (retval); 13471 13472} 13473 13474static void 13475ctl_datamove_remote_read(union ctl_io *io) 13476{ 13477 int retval; 13478 int i; 13479 13480 /* 13481 * This will send an error to the other controller in the case of a 13482 * failure. 13483 */ 13484 retval = ctl_datamove_remote_sgl_setup(io); 13485 if (retval != 0) 13486 return; 13487 13488 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13489 ctl_datamove_remote_read_cb); 13490 if ((retval != 0) 13491 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13492 /* 13493 * Make sure we free memory if there was an error.. The 13494 * ctl_datamove_remote_xfer() function will send the 13495 * datamove done message, or call the callback with an 13496 * error if there is a problem. 13497 */ 13498 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13499 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13500 } 13501 13502 return; 13503} 13504 13505/* 13506 * Process a datamove request from the other controller. This is used for 13507 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13508 * first. Once that is complete, the data gets DMAed into the remote 13509 * controller's memory. For reads, we DMA from the remote controller's 13510 * memory into our memory first, and then move it out to the FETD. 13511 */ 13512static void 13513ctl_datamove_remote(union ctl_io *io) 13514{ 13515 struct ctl_softc *softc; 13516 13517 softc = control_softc; 13518 13519 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13520 13521 /* 13522 * Note that we look for an aborted I/O here, but don't do some of 13523 * the other checks that ctl_datamove() normally does. 13524 * We don't need to run the datamove delay code, since that should 13525 * have been done if need be on the other controller. 13526 */ 13527 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13528 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13529 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13530 io->io_hdr.nexus.targ_port, 13531 io->io_hdr.nexus.targ_target.id, 13532 io->io_hdr.nexus.targ_lun); 13533 io->io_hdr.port_status = 31338; 13534 ctl_send_datamove_done(io, /*have_lock*/ 0); 13535 return; 13536 } 13537 13538 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13539 ctl_datamove_remote_write(io); 13540 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13541 ctl_datamove_remote_read(io); 13542 } else { 13543 union ctl_ha_msg msg; 13544 struct scsi_sense_data *sense; 13545 uint8_t sks[3]; 13546 int retry_count; 13547 13548 memset(&msg, 0, sizeof(msg)); 13549 13550 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13551 msg.hdr.status = CTL_SCSI_ERROR; 13552 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13553 13554 retry_count = 4243; 13555 13556 sense = &msg.scsi.sense_data; 13557 sks[0] = SSD_SCS_VALID; 13558 sks[1] = (retry_count >> 8) & 0xff; 13559 sks[2] = retry_count & 0xff; 13560 13561 /* "Internal target failure" */ 13562 scsi_set_sense_data(sense, 13563 /*sense_format*/ SSD_TYPE_NONE, 13564 /*current_error*/ 1, 13565 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13566 /*asc*/ 0x44, 13567 /*ascq*/ 0x00, 13568 /*type*/ SSD_ELEM_SKS, 13569 /*size*/ sizeof(sks), 13570 /*data*/ sks, 13571 SSD_ELEM_NONE); 13572 13573 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13574 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13575 ctl_failover_io(io, /*have_lock*/ 1); 13576 return; 13577 } 13578 13579 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13580 CTL_HA_STATUS_SUCCESS) { 13581 /* XXX KDM what to do if this fails? */ 13582 } 13583 return; 13584 } 13585 13586} 13587 13588static int 13589ctl_process_done(union ctl_io *io) 13590{ 13591 struct ctl_lun *lun; 13592 struct ctl_softc *ctl_softc; 13593 void (*fe_done)(union ctl_io *io); 13594 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13595 13596 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13597 13598 fe_done = 13599 control_softc->ctl_ports[targ_port]->fe_done; 13600 13601#ifdef CTL_TIME_IO 13602 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13603 char str[256]; 13604 char path_str[64]; 13605 struct sbuf sb; 13606 13607 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13608 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13609 13610 sbuf_cat(&sb, path_str); 13611 switch (io->io_hdr.io_type) { 13612 case CTL_IO_SCSI: 13613 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13614 sbuf_printf(&sb, "\n"); 13615 sbuf_cat(&sb, path_str); 13616 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13617 io->scsiio.tag_num, io->scsiio.tag_type); 13618 break; 13619 case CTL_IO_TASK: 13620 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13621 "Tag Type: %d\n", io->taskio.task_action, 13622 io->taskio.tag_num, io->taskio.tag_type); 13623 break; 13624 default: 13625 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13626 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13627 break; 13628 } 13629 sbuf_cat(&sb, path_str); 13630 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13631 (intmax_t)time_uptime - io->io_hdr.start_time); 13632 sbuf_finish(&sb); 13633 printf("%s", sbuf_data(&sb)); 13634 } 13635#endif /* CTL_TIME_IO */ 13636 13637 switch (io->io_hdr.io_type) { 13638 case CTL_IO_SCSI: 13639 break; 13640 case CTL_IO_TASK: 13641 if (bootverbose || verbose > 0) 13642 ctl_io_error_print(io, NULL); 13643 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13644 ctl_free_io(io); 13645 else 13646 fe_done(io); 13647 return (CTL_RETVAL_COMPLETE); 13648 break; 13649 default: 13650 printf("ctl_process_done: invalid io type %d\n", 13651 io->io_hdr.io_type); 13652 panic("ctl_process_done: invalid io type %d\n", 13653 io->io_hdr.io_type); 13654 break; /* NOTREACHED */ 13655 } 13656 13657 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13658 if (lun == NULL) { 13659 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13660 io->io_hdr.nexus.targ_mapped_lun)); 13661 fe_done(io); 13662 goto bailout; 13663 } 13664 ctl_softc = lun->ctl_softc; 13665 13666 mtx_lock(&lun->lun_lock); 13667 13668 /* 13669 * Check to see if we have any errors to inject here. We only 13670 * inject errors for commands that don't already have errors set. 13671 */ 13672 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13673 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13674 ctl_inject_error(lun, io); 13675 13676 /* 13677 * XXX KDM how do we treat commands that aren't completed 13678 * successfully? 13679 * 13680 * XXX KDM should we also track I/O latency? 13681 */ 13682 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13683 io->io_hdr.io_type == CTL_IO_SCSI) { 13684#ifdef CTL_TIME_IO 13685 struct bintime cur_bt; 13686#endif 13687 int type; 13688 13689 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13690 CTL_FLAG_DATA_IN) 13691 type = CTL_STATS_READ; 13692 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13693 CTL_FLAG_DATA_OUT) 13694 type = CTL_STATS_WRITE; 13695 else 13696 type = CTL_STATS_NO_IO; 13697 13698 lun->stats.ports[targ_port].bytes[type] += 13699 io->scsiio.kern_total_len; 13700 lun->stats.ports[targ_port].operations[type]++; 13701#ifdef CTL_TIME_IO 13702 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13703 &io->io_hdr.dma_bt); 13704 lun->stats.ports[targ_port].num_dmas[type] += 13705 io->io_hdr.num_dmas; 13706 getbintime(&cur_bt); 13707 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13708 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13709#endif 13710 } 13711 13712 /* 13713 * Remove this from the OOA queue. 13714 */ 13715 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13716 13717 /* 13718 * Run through the blocked queue on this LUN and see if anything 13719 * has become unblocked, now that this transaction is done. 13720 */ 13721 ctl_check_blocked(lun); 13722 13723 /* 13724 * If the LUN has been invalidated, free it if there is nothing 13725 * left on its OOA queue. 13726 */ 13727 if ((lun->flags & CTL_LUN_INVALID) 13728 && TAILQ_EMPTY(&lun->ooa_queue)) { 13729 mtx_unlock(&lun->lun_lock); 13730 mtx_lock(&ctl_softc->ctl_lock); 13731 ctl_free_lun(lun); 13732 mtx_unlock(&ctl_softc->ctl_lock); 13733 } else 13734 mtx_unlock(&lun->lun_lock); 13735 13736 /* 13737 * If this command has been aborted, make sure we set the status 13738 * properly. The FETD is responsible for freeing the I/O and doing 13739 * whatever it needs to do to clean up its state. 13740 */ 13741 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13742 ctl_set_task_aborted(&io->scsiio); 13743 13744 /* 13745 * We print out status for every task management command. For SCSI 13746 * commands, we filter out any unit attention errors; they happen 13747 * on every boot, and would clutter up the log. Note: task 13748 * management commands aren't printed here, they are printed above, 13749 * since they should never even make it down here. 13750 */ 13751 switch (io->io_hdr.io_type) { 13752 case CTL_IO_SCSI: { 13753 int error_code, sense_key, asc, ascq; 13754 13755 sense_key = 0; 13756 13757 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13758 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13759 /* 13760 * Since this is just for printing, no need to 13761 * show errors here. 13762 */ 13763 scsi_extract_sense_len(&io->scsiio.sense_data, 13764 io->scsiio.sense_len, 13765 &error_code, 13766 &sense_key, 13767 &asc, 13768 &ascq, 13769 /*show_errors*/ 0); 13770 } 13771 13772 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13773 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13774 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13775 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13776 13777 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13778 ctl_softc->skipped_prints++; 13779 } else { 13780 uint32_t skipped_prints; 13781 13782 skipped_prints = ctl_softc->skipped_prints; 13783 13784 ctl_softc->skipped_prints = 0; 13785 ctl_softc->last_print_jiffies = time_uptime; 13786 13787 if (skipped_prints > 0) { 13788#ifdef NEEDTOPORT 13789 csevent_log(CSC_CTL | CSC_SHELF_SW | 13790 CTL_ERROR_REPORT, 13791 csevent_LogType_Trace, 13792 csevent_Severity_Information, 13793 csevent_AlertLevel_Green, 13794 csevent_FRU_Firmware, 13795 csevent_FRU_Unknown, 13796 "High CTL error volume, %d prints " 13797 "skipped", skipped_prints); 13798#endif 13799 } 13800 if (bootverbose || verbose > 0) 13801 ctl_io_error_print(io, NULL); 13802 } 13803 } 13804 break; 13805 } 13806 case CTL_IO_TASK: 13807 if (bootverbose || verbose > 0) 13808 ctl_io_error_print(io, NULL); 13809 break; 13810 default: 13811 break; 13812 } 13813 13814 /* 13815 * Tell the FETD or the other shelf controller we're done with this 13816 * command. Note that only SCSI commands get to this point. Task 13817 * management commands are completed above. 13818 * 13819 * We only send status to the other controller if we're in XFER 13820 * mode. In SER_ONLY mode, the I/O is done on the controller that 13821 * received the I/O (from CTL's perspective), and so the status is 13822 * generated there. 13823 * 13824 * XXX KDM if we hold the lock here, we could cause a deadlock 13825 * if the frontend comes back in in this context to queue 13826 * something. 13827 */ 13828 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13829 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13830 union ctl_ha_msg msg; 13831 13832 memset(&msg, 0, sizeof(msg)); 13833 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13834 msg.hdr.original_sc = io->io_hdr.original_sc; 13835 msg.hdr.nexus = io->io_hdr.nexus; 13836 msg.hdr.status = io->io_hdr.status; 13837 msg.scsi.scsi_status = io->scsiio.scsi_status; 13838 msg.scsi.tag_num = io->scsiio.tag_num; 13839 msg.scsi.tag_type = io->scsiio.tag_type; 13840 msg.scsi.sense_len = io->scsiio.sense_len; 13841 msg.scsi.sense_residual = io->scsiio.sense_residual; 13842 msg.scsi.residual = io->scsiio.residual; 13843 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13844 sizeof(io->scsiio.sense_data)); 13845 /* 13846 * We copy this whether or not this is an I/O-related 13847 * command. Otherwise, we'd have to go and check to see 13848 * whether it's a read/write command, and it really isn't 13849 * worth it. 13850 */ 13851 memcpy(&msg.scsi.lbalen, 13852 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13853 sizeof(msg.scsi.lbalen)); 13854 13855 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13856 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13857 /* XXX do something here */ 13858 } 13859 13860 ctl_free_io(io); 13861 } else 13862 fe_done(io); 13863 13864bailout: 13865 13866 return (CTL_RETVAL_COMPLETE); 13867} 13868 13869#ifdef CTL_WITH_CA 13870/* 13871 * Front end should call this if it doesn't do autosense. When the request 13872 * sense comes back in from the initiator, we'll dequeue this and send it. 13873 */ 13874int 13875ctl_queue_sense(union ctl_io *io) 13876{ 13877 struct ctl_lun *lun; 13878 struct ctl_softc *ctl_softc; 13879 uint32_t initidx, targ_lun; 13880 13881 ctl_softc = control_softc; 13882 13883 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13884 13885 /* 13886 * LUN lookup will likely move to the ctl_work_thread() once we 13887 * have our new queueing infrastructure (that doesn't put things on 13888 * a per-LUN queue initially). That is so that we can handle 13889 * things like an INQUIRY to a LUN that we don't have enabled. We 13890 * can't deal with that right now. 13891 */ 13892 mtx_lock(&ctl_softc->ctl_lock); 13893 13894 /* 13895 * If we don't have a LUN for this, just toss the sense 13896 * information. 13897 */ 13898 targ_lun = io->io_hdr.nexus.targ_lun; 13899 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13900 if ((targ_lun < CTL_MAX_LUNS) 13901 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13902 lun = ctl_softc->ctl_luns[targ_lun]; 13903 else 13904 goto bailout; 13905 13906 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13907 13908 mtx_lock(&lun->lun_lock); 13909 /* 13910 * Already have CA set for this LUN...toss the sense information. 13911 */ 13912 if (ctl_is_set(lun->have_ca, initidx)) { 13913 mtx_unlock(&lun->lun_lock); 13914 goto bailout; 13915 } 13916 13917 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13918 ctl_min(sizeof(lun->pending_sense[initidx]), 13919 sizeof(io->scsiio.sense_data))); 13920 ctl_set_mask(lun->have_ca, initidx); 13921 mtx_unlock(&lun->lun_lock); 13922 13923bailout: 13924 mtx_unlock(&ctl_softc->ctl_lock); 13925 13926 ctl_free_io(io); 13927 13928 return (CTL_RETVAL_COMPLETE); 13929} 13930#endif 13931 13932/* 13933 * Primary command inlet from frontend ports. All SCSI and task I/O 13934 * requests must go through this function. 13935 */ 13936int 13937ctl_queue(union ctl_io *io) 13938{ 13939 struct ctl_softc *ctl_softc; 13940 13941 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13942 13943 ctl_softc = control_softc; 13944 13945#ifdef CTL_TIME_IO 13946 io->io_hdr.start_time = time_uptime; 13947 getbintime(&io->io_hdr.start_bt); 13948#endif /* CTL_TIME_IO */ 13949 13950 /* Map FE-specific LUN ID into global one. */ 13951 io->io_hdr.nexus.targ_mapped_lun = 13952 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13953 13954 switch (io->io_hdr.io_type) { 13955 case CTL_IO_SCSI: 13956 case CTL_IO_TASK: 13957 ctl_enqueue_incoming(io); 13958 break; 13959 default: 13960 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13961 return (EINVAL); 13962 } 13963 13964 return (CTL_RETVAL_COMPLETE); 13965} 13966 13967#ifdef CTL_IO_DELAY 13968static void 13969ctl_done_timer_wakeup(void *arg) 13970{ 13971 union ctl_io *io; 13972 13973 io = (union ctl_io *)arg; 13974 ctl_done(io); 13975} 13976#endif /* CTL_IO_DELAY */ 13977 13978void 13979ctl_done(union ctl_io *io) 13980{ 13981 struct ctl_softc *ctl_softc; 13982 13983 ctl_softc = control_softc; 13984 13985 /* 13986 * Enable this to catch duplicate completion issues. 13987 */ 13988#if 0 13989 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13990 printf("%s: type %d msg %d cdb %x iptl: " 13991 "%d:%d:%d:%d tag 0x%04x " 13992 "flag %#x status %x\n", 13993 __func__, 13994 io->io_hdr.io_type, 13995 io->io_hdr.msg_type, 13996 io->scsiio.cdb[0], 13997 io->io_hdr.nexus.initid.id, 13998 io->io_hdr.nexus.targ_port, 13999 io->io_hdr.nexus.targ_target.id, 14000 io->io_hdr.nexus.targ_lun, 14001 (io->io_hdr.io_type == 14002 CTL_IO_TASK) ? 14003 io->taskio.tag_num : 14004 io->scsiio.tag_num, 14005 io->io_hdr.flags, 14006 io->io_hdr.status); 14007 } else 14008 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 14009#endif 14010 14011 /* 14012 * This is an internal copy of an I/O, and should not go through 14013 * the normal done processing logic. 14014 */ 14015 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 14016 return; 14017 14018 /* 14019 * We need to send a msg to the serializing shelf to finish the IO 14020 * as well. We don't send a finish message to the other shelf if 14021 * this is a task management command. Task management commands 14022 * aren't serialized in the OOA queue, but rather just executed on 14023 * both shelf controllers for commands that originated on that 14024 * controller. 14025 */ 14026 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 14027 && (io->io_hdr.io_type != CTL_IO_TASK)) { 14028 union ctl_ha_msg msg_io; 14029 14030 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 14031 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 14032 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 14033 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 14034 } 14035 /* continue on to finish IO */ 14036 } 14037#ifdef CTL_IO_DELAY 14038 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 14039 struct ctl_lun *lun; 14040 14041 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14042 14043 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14044 } else { 14045 struct ctl_lun *lun; 14046 14047 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14048 14049 if ((lun != NULL) 14050 && (lun->delay_info.done_delay > 0)) { 14051 struct callout *callout; 14052 14053 callout = (struct callout *)&io->io_hdr.timer_bytes; 14054 callout_init(callout, /*mpsafe*/ 1); 14055 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14056 callout_reset(callout, 14057 lun->delay_info.done_delay * hz, 14058 ctl_done_timer_wakeup, io); 14059 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14060 lun->delay_info.done_delay = 0; 14061 return; 14062 } 14063 } 14064#endif /* CTL_IO_DELAY */ 14065 14066 ctl_enqueue_done(io); 14067} 14068 14069int 14070ctl_isc(struct ctl_scsiio *ctsio) 14071{ 14072 struct ctl_lun *lun; 14073 int retval; 14074 14075 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14076 14077 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14078 14079 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14080 14081 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14082 14083 return (retval); 14084} 14085 14086 14087static void 14088ctl_work_thread(void *arg) 14089{ 14090 struct ctl_thread *thr = (struct ctl_thread *)arg; 14091 struct ctl_softc *softc = thr->ctl_softc; 14092 union ctl_io *io; 14093 int retval; 14094 14095 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14096 14097 for (;;) { 14098 retval = 0; 14099 14100 /* 14101 * We handle the queues in this order: 14102 * - ISC 14103 * - done queue (to free up resources, unblock other commands) 14104 * - RtR queue 14105 * - incoming queue 14106 * 14107 * If those queues are empty, we break out of the loop and 14108 * go to sleep. 14109 */ 14110 mtx_lock(&thr->queue_lock); 14111 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14112 if (io != NULL) { 14113 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14114 mtx_unlock(&thr->queue_lock); 14115 ctl_handle_isc(io); 14116 continue; 14117 } 14118 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14119 if (io != NULL) { 14120 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14121 /* clear any blocked commands, call fe_done */ 14122 mtx_unlock(&thr->queue_lock); 14123 retval = ctl_process_done(io); 14124 continue; 14125 } 14126 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14127 if (io != NULL) { 14128 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14129 mtx_unlock(&thr->queue_lock); 14130 if (io->io_hdr.io_type == CTL_IO_TASK) 14131 ctl_run_task(io); 14132 else 14133 ctl_scsiio_precheck(softc, &io->scsiio); 14134 continue; 14135 } 14136 if (!ctl_pause_rtr) { 14137 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14138 if (io != NULL) { 14139 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14140 mtx_unlock(&thr->queue_lock); 14141 retval = ctl_scsiio(&io->scsiio); 14142 if (retval != CTL_RETVAL_COMPLETE) 14143 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14144 continue; 14145 } 14146 } 14147 14148 /* Sleep until we have something to do. */ 14149 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14150 } 14151} 14152 14153static void 14154ctl_lun_thread(void *arg) 14155{ 14156 struct ctl_softc *softc = (struct ctl_softc *)arg; 14157 struct ctl_be_lun *be_lun; 14158 int retval; 14159 14160 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14161 14162 for (;;) { 14163 retval = 0; 14164 mtx_lock(&softc->ctl_lock); 14165 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14166 if (be_lun != NULL) { 14167 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14168 mtx_unlock(&softc->ctl_lock); 14169 ctl_create_lun(be_lun); 14170 continue; 14171 } 14172 14173 /* Sleep until we have something to do. */ 14174 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14175 PDROP | PRIBIO, "-", 0); 14176 } 14177} 14178 14179static void 14180ctl_enqueue_incoming(union ctl_io *io) 14181{ 14182 struct ctl_softc *softc = control_softc; 14183 struct ctl_thread *thr; 14184 u_int idx; 14185 14186 idx = (io->io_hdr.nexus.targ_port * 127 + 14187 io->io_hdr.nexus.initid.id) % worker_threads; 14188 thr = &softc->threads[idx]; 14189 mtx_lock(&thr->queue_lock); 14190 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14191 mtx_unlock(&thr->queue_lock); 14192 wakeup(thr); 14193} 14194 14195static void 14196ctl_enqueue_rtr(union ctl_io *io) 14197{ 14198 struct ctl_softc *softc = control_softc; 14199 struct ctl_thread *thr; 14200 14201 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14202 mtx_lock(&thr->queue_lock); 14203 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14204 mtx_unlock(&thr->queue_lock); 14205 wakeup(thr); 14206} 14207 14208static void 14209ctl_enqueue_done(union ctl_io *io) 14210{ 14211 struct ctl_softc *softc = control_softc; 14212 struct ctl_thread *thr; 14213 14214 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14215 mtx_lock(&thr->queue_lock); 14216 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14217 mtx_unlock(&thr->queue_lock); 14218 wakeup(thr); 14219} 14220 14221static void 14222ctl_enqueue_isc(union ctl_io *io) 14223{ 14224 struct ctl_softc *softc = control_softc; 14225 struct ctl_thread *thr; 14226 14227 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14228 mtx_lock(&thr->queue_lock); 14229 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14230 mtx_unlock(&thr->queue_lock); 14231 wakeup(thr); 14232} 14233 14234/* Initialization and failover */ 14235 14236void 14237ctl_init_isc_msg(void) 14238{ 14239 printf("CTL: Still calling this thing\n"); 14240} 14241 14242/* 14243 * Init component 14244 * Initializes component into configuration defined by bootMode 14245 * (see hasc-sv.c) 14246 * returns hasc_Status: 14247 * OK 14248 * ERROR - fatal error 14249 */ 14250static ctl_ha_comp_status 14251ctl_isc_init(struct ctl_ha_component *c) 14252{ 14253 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14254 14255 c->status = ret; 14256 return ret; 14257} 14258 14259/* Start component 14260 * Starts component in state requested. If component starts successfully, 14261 * it must set its own state to the requestrd state 14262 * When requested state is HASC_STATE_HA, the component may refine it 14263 * by adding _SLAVE or _MASTER flags. 14264 * Currently allowed state transitions are: 14265 * UNKNOWN->HA - initial startup 14266 * UNKNOWN->SINGLE - initial startup when no parter detected 14267 * HA->SINGLE - failover 14268 * returns ctl_ha_comp_status: 14269 * OK - component successfully started in requested state 14270 * FAILED - could not start the requested state, failover may 14271 * be possible 14272 * ERROR - fatal error detected, no future startup possible 14273 */ 14274static ctl_ha_comp_status 14275ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14276{ 14277 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14278 14279 printf("%s: go\n", __func__); 14280 14281 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14282 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14283 ctl_is_single = 0; 14284 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14285 != CTL_HA_STATUS_SUCCESS) { 14286 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14287 ret = CTL_HA_COMP_STATUS_ERROR; 14288 } 14289 } else if (CTL_HA_STATE_IS_HA(c->state) 14290 && CTL_HA_STATE_IS_SINGLE(state)){ 14291 // HA->SINGLE transition 14292 ctl_failover(); 14293 ctl_is_single = 1; 14294 } else { 14295 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14296 c->state, state); 14297 ret = CTL_HA_COMP_STATUS_ERROR; 14298 } 14299 if (CTL_HA_STATE_IS_SINGLE(state)) 14300 ctl_is_single = 1; 14301 14302 c->state = state; 14303 c->status = ret; 14304 return ret; 14305} 14306 14307/* 14308 * Quiesce component 14309 * The component must clear any error conditions (set status to OK) and 14310 * prepare itself to another Start call 14311 * returns ctl_ha_comp_status: 14312 * OK 14313 * ERROR 14314 */ 14315static ctl_ha_comp_status 14316ctl_isc_quiesce(struct ctl_ha_component *c) 14317{ 14318 int ret = CTL_HA_COMP_STATUS_OK; 14319 14320 ctl_pause_rtr = 1; 14321 c->status = ret; 14322 return ret; 14323} 14324 14325struct ctl_ha_component ctl_ha_component_ctlisc = 14326{ 14327 .name = "CTL ISC", 14328 .state = CTL_HA_STATE_UNKNOWN, 14329 .init = ctl_isc_init, 14330 .start = ctl_isc_start, 14331 .quiesce = ctl_isc_quiesce 14332}; 14333 14334/* 14335 * vim: ts=8 14336 */ 14337