ctl.c revision 271354
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: head/sys/cam/ctl/ctl.c 271354 2014-09-10 06:35:00Z 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*/0, 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*/0, 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; 315SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 316 &worker_threads, 1, "Number of worker threads"); 317static int verbose = 0; 318SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 319 &verbose, 0, "Show SCSI errors returned to initiator"); 320 321/* 322 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 323 * Mode Page Policy (0x87), 324 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 325 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 326 */ 327#define SCSI_EVPD_NUM_SUPPORTED_PAGES 9 328 329static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 330 int param); 331static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 332static int ctl_init(void); 333void ctl_shutdown(void); 334static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 335static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 336static void ctl_ioctl_online(void *arg); 337static void ctl_ioctl_offline(void *arg); 338static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 339static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 340static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 341static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 342static int ctl_ioctl_submit_wait(union ctl_io *io); 343static void ctl_ioctl_datamove(union ctl_io *io); 344static void ctl_ioctl_done(union ctl_io *io); 345static void ctl_ioctl_hard_startstop_callback(void *arg, 346 struct cfi_metatask *metatask); 347static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 348static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 349 struct ctl_ooa *ooa_hdr, 350 struct ctl_ooa_entry *kern_entries); 351static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 352 struct thread *td); 353static uint32_t ctl_map_lun(int port_num, uint32_t lun); 354static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 355#ifdef unused 356static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 357 uint32_t targ_target, uint32_t targ_lun, 358 int can_wait); 359static void ctl_kfree_io(union ctl_io *io); 360#endif /* unused */ 361static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 362 struct ctl_be_lun *be_lun, struct ctl_id target_id); 363static int ctl_free_lun(struct ctl_lun *lun); 364static void ctl_create_lun(struct ctl_be_lun *be_lun); 365/** 366static void ctl_failover_change_pages(struct ctl_softc *softc, 367 struct ctl_scsiio *ctsio, int master); 368**/ 369 370static int ctl_do_mode_select(union ctl_io *io); 371static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 372 uint64_t res_key, uint64_t sa_res_key, 373 uint8_t type, uint32_t residx, 374 struct ctl_scsiio *ctsio, 375 struct scsi_per_res_out *cdb, 376 struct scsi_per_res_out_parms* param); 377static void ctl_pro_preempt_other(struct ctl_lun *lun, 378 union ctl_ha_msg *msg); 379static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 380static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 381static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 382static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 383static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 384static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 385 int alloc_len); 386static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 387 int alloc_len); 388static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 389static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 390static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 391static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 392static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 393static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 394static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 395 union ctl_io *ooa_io); 396static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 397 union ctl_io *starting_io); 398static int ctl_check_blocked(struct ctl_lun *lun); 399static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 400 struct ctl_lun *lun, 401 const struct ctl_cmd_entry *entry, 402 struct ctl_scsiio *ctsio); 403//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 404static void ctl_failover(void); 405static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 406 struct ctl_scsiio *ctsio); 407static int ctl_scsiio(struct ctl_scsiio *ctsio); 408 409static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 410static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 411 ctl_ua_type ua_type); 412static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 413 ctl_ua_type ua_type); 414static int ctl_abort_task(union ctl_io *io); 415static int ctl_abort_task_set(union ctl_io *io); 416static int ctl_i_t_nexus_reset(union ctl_io *io); 417static void ctl_run_task(union ctl_io *io); 418#ifdef CTL_IO_DELAY 419static void ctl_datamove_timer_wakeup(void *arg); 420static void ctl_done_timer_wakeup(void *arg); 421#endif /* CTL_IO_DELAY */ 422 423static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 424static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 425static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 426static void ctl_datamove_remote_write(union ctl_io *io); 427static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 428static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 429static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 430static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 431 ctl_ha_dt_cb callback); 432static void ctl_datamove_remote_read(union ctl_io *io); 433static void ctl_datamove_remote(union ctl_io *io); 434static int ctl_process_done(union ctl_io *io); 435static void ctl_lun_thread(void *arg); 436static void ctl_work_thread(void *arg); 437static void ctl_enqueue_incoming(union ctl_io *io); 438static void ctl_enqueue_rtr(union ctl_io *io); 439static void ctl_enqueue_done(union ctl_io *io); 440static void ctl_enqueue_isc(union ctl_io *io); 441static const struct ctl_cmd_entry * 442 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 443static const struct ctl_cmd_entry * 444 ctl_validate_command(struct ctl_scsiio *ctsio); 445static int ctl_cmd_applicable(uint8_t lun_type, 446 const struct ctl_cmd_entry *entry); 447 448/* 449 * Load the serialization table. This isn't very pretty, but is probably 450 * the easiest way to do it. 451 */ 452#include "ctl_ser_table.c" 453 454/* 455 * We only need to define open, close and ioctl routines for this driver. 456 */ 457static struct cdevsw ctl_cdevsw = { 458 .d_version = D_VERSION, 459 .d_flags = 0, 460 .d_open = ctl_open, 461 .d_close = ctl_close, 462 .d_ioctl = ctl_ioctl, 463 .d_name = "ctl", 464}; 465 466 467MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 468MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 469 470static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 471 472static moduledata_t ctl_moduledata = { 473 "ctl", 474 ctl_module_event_handler, 475 NULL 476}; 477 478DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 479MODULE_VERSION(ctl, 1); 480 481static struct ctl_frontend ioctl_frontend = 482{ 483 .name = "ioctl", 484}; 485 486static void 487ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 488 union ctl_ha_msg *msg_info) 489{ 490 struct ctl_scsiio *ctsio; 491 492 if (msg_info->hdr.original_sc == NULL) { 493 printf("%s: original_sc == NULL!\n", __func__); 494 /* XXX KDM now what? */ 495 return; 496 } 497 498 ctsio = &msg_info->hdr.original_sc->scsiio; 499 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 500 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 501 ctsio->io_hdr.status = msg_info->hdr.status; 502 ctsio->scsi_status = msg_info->scsi.scsi_status; 503 ctsio->sense_len = msg_info->scsi.sense_len; 504 ctsio->sense_residual = msg_info->scsi.sense_residual; 505 ctsio->residual = msg_info->scsi.residual; 506 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 507 sizeof(ctsio->sense_data)); 508 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 509 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 510 ctl_enqueue_isc((union ctl_io *)ctsio); 511} 512 513static void 514ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 515 union ctl_ha_msg *msg_info) 516{ 517 struct ctl_scsiio *ctsio; 518 519 if (msg_info->hdr.serializing_sc == NULL) { 520 printf("%s: serializing_sc == NULL!\n", __func__); 521 /* XXX KDM now what? */ 522 return; 523 } 524 525 ctsio = &msg_info->hdr.serializing_sc->scsiio; 526#if 0 527 /* 528 * Attempt to catch the situation where an I/O has 529 * been freed, and we're using it again. 530 */ 531 if (ctsio->io_hdr.io_type == 0xff) { 532 union ctl_io *tmp_io; 533 tmp_io = (union ctl_io *)ctsio; 534 printf("%s: %p use after free!\n", __func__, 535 ctsio); 536 printf("%s: type %d msg %d cdb %x iptl: " 537 "%d:%d:%d:%d tag 0x%04x " 538 "flag %#x status %x\n", 539 __func__, 540 tmp_io->io_hdr.io_type, 541 tmp_io->io_hdr.msg_type, 542 tmp_io->scsiio.cdb[0], 543 tmp_io->io_hdr.nexus.initid.id, 544 tmp_io->io_hdr.nexus.targ_port, 545 tmp_io->io_hdr.nexus.targ_target.id, 546 tmp_io->io_hdr.nexus.targ_lun, 547 (tmp_io->io_hdr.io_type == 548 CTL_IO_TASK) ? 549 tmp_io->taskio.tag_num : 550 tmp_io->scsiio.tag_num, 551 tmp_io->io_hdr.flags, 552 tmp_io->io_hdr.status); 553 } 554#endif 555 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 556 ctl_enqueue_isc((union ctl_io *)ctsio); 557} 558 559/* 560 * ISC (Inter Shelf Communication) event handler. Events from the HA 561 * subsystem come in here. 562 */ 563static void 564ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 565{ 566 struct ctl_softc *ctl_softc; 567 union ctl_io *io; 568 struct ctl_prio *presio; 569 ctl_ha_status isc_status; 570 571 ctl_softc = control_softc; 572 io = NULL; 573 574 575#if 0 576 printf("CTL: Isc Msg event %d\n", event); 577#endif 578 if (event == CTL_HA_EVT_MSG_RECV) { 579 union ctl_ha_msg msg_info; 580 581 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 582 sizeof(msg_info), /*wait*/ 0); 583#if 0 584 printf("CTL: msg_type %d\n", msg_info.msg_type); 585#endif 586 if (isc_status != 0) { 587 printf("Error receiving message, status = %d\n", 588 isc_status); 589 return; 590 } 591 592 switch (msg_info.hdr.msg_type) { 593 case CTL_MSG_SERIALIZE: 594#if 0 595 printf("Serialize\n"); 596#endif 597 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 598 if (io == NULL) { 599 printf("ctl_isc_event_handler: can't allocate " 600 "ctl_io!\n"); 601 /* Bad Juju */ 602 /* Need to set busy and send msg back */ 603 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 604 msg_info.hdr.status = CTL_SCSI_ERROR; 605 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 606 msg_info.scsi.sense_len = 0; 607 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 608 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 609 } 610 goto bailout; 611 } 612 ctl_zero_io(io); 613 // populate ctsio from msg_info 614 io->io_hdr.io_type = CTL_IO_SCSI; 615 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 616 io->io_hdr.original_sc = msg_info.hdr.original_sc; 617#if 0 618 printf("pOrig %x\n", (int)msg_info.original_sc); 619#endif 620 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 621 CTL_FLAG_IO_ACTIVE; 622 /* 623 * If we're in serialization-only mode, we don't 624 * want to go through full done processing. Thus 625 * the COPY flag. 626 * 627 * XXX KDM add another flag that is more specific. 628 */ 629 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 630 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 631 io->io_hdr.nexus = msg_info.hdr.nexus; 632#if 0 633 printf("targ %d, port %d, iid %d, lun %d\n", 634 io->io_hdr.nexus.targ_target.id, 635 io->io_hdr.nexus.targ_port, 636 io->io_hdr.nexus.initid.id, 637 io->io_hdr.nexus.targ_lun); 638#endif 639 io->scsiio.tag_num = msg_info.scsi.tag_num; 640 io->scsiio.tag_type = msg_info.scsi.tag_type; 641 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 642 CTL_MAX_CDBLEN); 643 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 644 const struct ctl_cmd_entry *entry; 645 646 entry = ctl_get_cmd_entry(&io->scsiio); 647 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 648 io->io_hdr.flags |= 649 entry->flags & CTL_FLAG_DATA_MASK; 650 } 651 ctl_enqueue_isc(io); 652 break; 653 654 /* Performed on the Originating SC, XFER mode only */ 655 case CTL_MSG_DATAMOVE: { 656 struct ctl_sg_entry *sgl; 657 int i, j; 658 659 io = msg_info.hdr.original_sc; 660 if (io == NULL) { 661 printf("%s: original_sc == NULL!\n", __func__); 662 /* XXX KDM do something here */ 663 break; 664 } 665 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 666 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 667 /* 668 * Keep track of this, we need to send it back over 669 * when the datamove is complete. 670 */ 671 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 672 673 if (msg_info.dt.sg_sequence == 0) { 674 /* 675 * XXX KDM we use the preallocated S/G list 676 * here, but we'll need to change this to 677 * dynamic allocation if we need larger S/G 678 * lists. 679 */ 680 if (msg_info.dt.kern_sg_entries > 681 sizeof(io->io_hdr.remote_sglist) / 682 sizeof(io->io_hdr.remote_sglist[0])) { 683 printf("%s: number of S/G entries " 684 "needed %u > allocated num %zd\n", 685 __func__, 686 msg_info.dt.kern_sg_entries, 687 sizeof(io->io_hdr.remote_sglist)/ 688 sizeof(io->io_hdr.remote_sglist[0])); 689 690 /* 691 * XXX KDM send a message back to 692 * the other side to shut down the 693 * DMA. The error will come back 694 * through via the normal channel. 695 */ 696 break; 697 } 698 sgl = io->io_hdr.remote_sglist; 699 memset(sgl, 0, 700 sizeof(io->io_hdr.remote_sglist)); 701 702 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 703 704 io->scsiio.kern_sg_entries = 705 msg_info.dt.kern_sg_entries; 706 io->scsiio.rem_sg_entries = 707 msg_info.dt.kern_sg_entries; 708 io->scsiio.kern_data_len = 709 msg_info.dt.kern_data_len; 710 io->scsiio.kern_total_len = 711 msg_info.dt.kern_total_len; 712 io->scsiio.kern_data_resid = 713 msg_info.dt.kern_data_resid; 714 io->scsiio.kern_rel_offset = 715 msg_info.dt.kern_rel_offset; 716 /* 717 * Clear out per-DMA flags. 718 */ 719 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 720 /* 721 * Add per-DMA flags that are set for this 722 * particular DMA request. 723 */ 724 io->io_hdr.flags |= msg_info.dt.flags & 725 CTL_FLAG_RDMA_MASK; 726 } else 727 sgl = (struct ctl_sg_entry *) 728 io->scsiio.kern_data_ptr; 729 730 for (i = msg_info.dt.sent_sg_entries, j = 0; 731 i < (msg_info.dt.sent_sg_entries + 732 msg_info.dt.cur_sg_entries); i++, j++) { 733 sgl[i].addr = msg_info.dt.sg_list[j].addr; 734 sgl[i].len = msg_info.dt.sg_list[j].len; 735 736#if 0 737 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 738 __func__, 739 msg_info.dt.sg_list[j].addr, 740 msg_info.dt.sg_list[j].len, 741 sgl[i].addr, sgl[i].len, j, i); 742#endif 743 } 744#if 0 745 memcpy(&sgl[msg_info.dt.sent_sg_entries], 746 msg_info.dt.sg_list, 747 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 748#endif 749 750 /* 751 * If this is the last piece of the I/O, we've got 752 * the full S/G list. Queue processing in the thread. 753 * Otherwise wait for the next piece. 754 */ 755 if (msg_info.dt.sg_last != 0) 756 ctl_enqueue_isc(io); 757 break; 758 } 759 /* Performed on the Serializing (primary) SC, XFER mode only */ 760 case CTL_MSG_DATAMOVE_DONE: { 761 if (msg_info.hdr.serializing_sc == NULL) { 762 printf("%s: serializing_sc == NULL!\n", 763 __func__); 764 /* XXX KDM now what? */ 765 break; 766 } 767 /* 768 * We grab the sense information here in case 769 * there was a failure, so we can return status 770 * back to the initiator. 771 */ 772 io = msg_info.hdr.serializing_sc; 773 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 774 io->io_hdr.status = msg_info.hdr.status; 775 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 776 io->scsiio.sense_len = msg_info.scsi.sense_len; 777 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 778 io->io_hdr.port_status = msg_info.scsi.fetd_status; 779 io->scsiio.residual = msg_info.scsi.residual; 780 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 781 sizeof(io->scsiio.sense_data)); 782 ctl_enqueue_isc(io); 783 break; 784 } 785 786 /* Preformed on Originating SC, SER_ONLY mode */ 787 case CTL_MSG_R2R: 788 io = msg_info.hdr.original_sc; 789 if (io == NULL) { 790 printf("%s: Major Bummer\n", __func__); 791 return; 792 } else { 793#if 0 794 printf("pOrig %x\n",(int) ctsio); 795#endif 796 } 797 io->io_hdr.msg_type = CTL_MSG_R2R; 798 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 799 ctl_enqueue_isc(io); 800 break; 801 802 /* 803 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 804 * mode. 805 * Performed on the Originating (i.e. secondary) SC in XFER 806 * mode 807 */ 808 case CTL_MSG_FINISH_IO: 809 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 810 ctl_isc_handler_finish_xfer(ctl_softc, 811 &msg_info); 812 else 813 ctl_isc_handler_finish_ser_only(ctl_softc, 814 &msg_info); 815 break; 816 817 /* Preformed on Originating SC */ 818 case CTL_MSG_BAD_JUJU: 819 io = msg_info.hdr.original_sc; 820 if (io == NULL) { 821 printf("%s: Bad JUJU!, original_sc is NULL!\n", 822 __func__); 823 break; 824 } 825 ctl_copy_sense_data(&msg_info, io); 826 /* 827 * IO should have already been cleaned up on other 828 * SC so clear this flag so we won't send a message 829 * back to finish the IO there. 830 */ 831 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 832 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 833 834 /* io = msg_info.hdr.serializing_sc; */ 835 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 836 ctl_enqueue_isc(io); 837 break; 838 839 /* Handle resets sent from the other side */ 840 case CTL_MSG_MANAGE_TASKS: { 841 struct ctl_taskio *taskio; 842 taskio = (struct ctl_taskio *)ctl_alloc_io( 843 (void *)ctl_softc->othersc_pool); 844 if (taskio == NULL) { 845 printf("ctl_isc_event_handler: can't allocate " 846 "ctl_io!\n"); 847 /* Bad Juju */ 848 /* should I just call the proper reset func 849 here??? */ 850 goto bailout; 851 } 852 ctl_zero_io((union ctl_io *)taskio); 853 taskio->io_hdr.io_type = CTL_IO_TASK; 854 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 855 taskio->io_hdr.nexus = msg_info.hdr.nexus; 856 taskio->task_action = msg_info.task.task_action; 857 taskio->tag_num = msg_info.task.tag_num; 858 taskio->tag_type = msg_info.task.tag_type; 859#ifdef CTL_TIME_IO 860 taskio->io_hdr.start_time = time_uptime; 861 getbintime(&taskio->io_hdr.start_bt); 862#if 0 863 cs_prof_gettime(&taskio->io_hdr.start_ticks); 864#endif 865#endif /* CTL_TIME_IO */ 866 ctl_run_task((union ctl_io *)taskio); 867 break; 868 } 869 /* Persistent Reserve action which needs attention */ 870 case CTL_MSG_PERS_ACTION: 871 presio = (struct ctl_prio *)ctl_alloc_io( 872 (void *)ctl_softc->othersc_pool); 873 if (presio == NULL) { 874 printf("ctl_isc_event_handler: can't allocate " 875 "ctl_io!\n"); 876 /* Bad Juju */ 877 /* Need to set busy and send msg back */ 878 goto bailout; 879 } 880 ctl_zero_io((union ctl_io *)presio); 881 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 882 presio->pr_msg = msg_info.pr; 883 ctl_enqueue_isc((union ctl_io *)presio); 884 break; 885 case CTL_MSG_SYNC_FE: 886 rcv_sync_msg = 1; 887 break; 888 case CTL_MSG_APS_LOCK: { 889 // It's quicker to execute this then to 890 // queue it. 891 struct ctl_lun *lun; 892 struct ctl_page_index *page_index; 893 struct copan_aps_subpage *current_sp; 894 uint32_t targ_lun; 895 896 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 897 lun = ctl_softc->ctl_luns[targ_lun]; 898 mtx_lock(&lun->lun_lock); 899 page_index = &lun->mode_pages.index[index_to_aps_page]; 900 current_sp = (struct copan_aps_subpage *) 901 (page_index->page_data + 902 (page_index->page_len * CTL_PAGE_CURRENT)); 903 904 current_sp->lock_active = msg_info.aps.lock_flag; 905 mtx_unlock(&lun->lun_lock); 906 break; 907 } 908 default: 909 printf("How did I get here?\n"); 910 } 911 } else if (event == CTL_HA_EVT_MSG_SENT) { 912 if (param != CTL_HA_STATUS_SUCCESS) { 913 printf("Bad status from ctl_ha_msg_send status %d\n", 914 param); 915 } 916 return; 917 } else if (event == CTL_HA_EVT_DISCONNECT) { 918 printf("CTL: Got a disconnect from Isc\n"); 919 return; 920 } else { 921 printf("ctl_isc_event_handler: Unknown event %d\n", event); 922 return; 923 } 924 925bailout: 926 return; 927} 928 929static void 930ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 931{ 932 struct scsi_sense_data *sense; 933 934 sense = &dest->scsiio.sense_data; 935 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 936 dest->scsiio.scsi_status = src->scsi.scsi_status; 937 dest->scsiio.sense_len = src->scsi.sense_len; 938 dest->io_hdr.status = src->hdr.status; 939} 940 941static int 942ctl_init(void) 943{ 944 struct ctl_softc *softc; 945 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 946 struct ctl_port *port; 947 uint8_t sc_id =0; 948 int i, error, retval; 949 //int isc_retval; 950 951 retval = 0; 952 ctl_pause_rtr = 0; 953 rcv_sync_msg = 0; 954 955 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 956 M_WAITOK | M_ZERO); 957 softc = control_softc; 958 959 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 960 "cam/ctl"); 961 962 softc->dev->si_drv1 = softc; 963 964 /* 965 * By default, return a "bad LUN" peripheral qualifier for unknown 966 * LUNs. The user can override this default using the tunable or 967 * sysctl. See the comment in ctl_inquiry_std() for more details. 968 */ 969 softc->inquiry_pq_no_lun = 1; 970 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 971 &softc->inquiry_pq_no_lun); 972 sysctl_ctx_init(&softc->sysctl_ctx); 973 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 974 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 975 CTLFLAG_RD, 0, "CAM Target Layer"); 976 977 if (softc->sysctl_tree == NULL) { 978 printf("%s: unable to allocate sysctl tree\n", __func__); 979 destroy_dev(softc->dev); 980 free(control_softc, M_DEVBUF); 981 control_softc = NULL; 982 return (ENOMEM); 983 } 984 985 SYSCTL_ADD_INT(&softc->sysctl_ctx, 986 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 987 "inquiry_pq_no_lun", CTLFLAG_RW, 988 &softc->inquiry_pq_no_lun, 0, 989 "Report no lun possible for invalid LUNs"); 990 991 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 992 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 993 softc->open_count = 0; 994 995 /* 996 * Default to actually sending a SYNCHRONIZE CACHE command down to 997 * the drive. 998 */ 999 softc->flags = CTL_FLAG_REAL_SYNC; 1000 1001 /* 1002 * In Copan's HA scheme, the "master" and "slave" roles are 1003 * figured out through the slot the controller is in. Although it 1004 * is an active/active system, someone has to be in charge. 1005 */ 1006#ifdef NEEDTOPORT 1007 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1008#endif 1009 1010 if (sc_id == 0) { 1011 softc->flags |= CTL_FLAG_MASTER_SHELF; 1012 persis_offset = 0; 1013 } else 1014 persis_offset = CTL_MAX_INITIATORS; 1015 1016 /* 1017 * XXX KDM need to figure out where we want to get our target ID 1018 * and WWID. Is it different on each port? 1019 */ 1020 softc->target.id = 0; 1021 softc->target.wwid[0] = 0x12345678; 1022 softc->target.wwid[1] = 0x87654321; 1023 STAILQ_INIT(&softc->lun_list); 1024 STAILQ_INIT(&softc->pending_lun_queue); 1025 STAILQ_INIT(&softc->fe_list); 1026 STAILQ_INIT(&softc->port_list); 1027 STAILQ_INIT(&softc->be_list); 1028 STAILQ_INIT(&softc->io_pools); 1029 ctl_tpc_init(softc); 1030 1031 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1032 &internal_pool)!= 0){ 1033 printf("ctl: can't allocate %d entry internal pool, " 1034 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1035 return (ENOMEM); 1036 } 1037 1038 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1039 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1040 printf("ctl: can't allocate %d entry emergency pool, " 1041 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1042 ctl_pool_free(internal_pool); 1043 return (ENOMEM); 1044 } 1045 1046 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1047 &other_pool) != 0) 1048 { 1049 printf("ctl: can't allocate %d entry other SC pool, " 1050 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1051 ctl_pool_free(internal_pool); 1052 ctl_pool_free(emergency_pool); 1053 return (ENOMEM); 1054 } 1055 1056 softc->internal_pool = internal_pool; 1057 softc->emergency_pool = emergency_pool; 1058 softc->othersc_pool = other_pool; 1059 1060 if (worker_threads <= 0) 1061 worker_threads = max(1, mp_ncpus / 4); 1062 if (worker_threads > CTL_MAX_THREADS) 1063 worker_threads = CTL_MAX_THREADS; 1064 1065 for (i = 0; i < worker_threads; i++) { 1066 struct ctl_thread *thr = &softc->threads[i]; 1067 1068 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1069 thr->ctl_softc = softc; 1070 STAILQ_INIT(&thr->incoming_queue); 1071 STAILQ_INIT(&thr->rtr_queue); 1072 STAILQ_INIT(&thr->done_queue); 1073 STAILQ_INIT(&thr->isc_queue); 1074 1075 error = kproc_kthread_add(ctl_work_thread, thr, 1076 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1077 if (error != 0) { 1078 printf("error creating CTL work thread!\n"); 1079 ctl_pool_free(internal_pool); 1080 ctl_pool_free(emergency_pool); 1081 ctl_pool_free(other_pool); 1082 return (error); 1083 } 1084 } 1085 error = kproc_kthread_add(ctl_lun_thread, softc, 1086 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1087 if (error != 0) { 1088 printf("error creating CTL lun thread!\n"); 1089 ctl_pool_free(internal_pool); 1090 ctl_pool_free(emergency_pool); 1091 ctl_pool_free(other_pool); 1092 return (error); 1093 } 1094 if (bootverbose) 1095 printf("ctl: CAM Target Layer loaded\n"); 1096 1097 /* 1098 * Initialize the ioctl front end. 1099 */ 1100 ctl_frontend_register(&ioctl_frontend); 1101 port = &softc->ioctl_info.port; 1102 port->frontend = &ioctl_frontend; 1103 sprintf(softc->ioctl_info.port_name, "ioctl"); 1104 port->port_type = CTL_PORT_IOCTL; 1105 port->num_requested_ctl_io = 100; 1106 port->port_name = softc->ioctl_info.port_name; 1107 port->port_online = ctl_ioctl_online; 1108 port->port_offline = ctl_ioctl_offline; 1109 port->onoff_arg = &softc->ioctl_info; 1110 port->lun_enable = ctl_ioctl_lun_enable; 1111 port->lun_disable = ctl_ioctl_lun_disable; 1112 port->targ_lun_arg = &softc->ioctl_info; 1113 port->fe_datamove = ctl_ioctl_datamove; 1114 port->fe_done = ctl_ioctl_done; 1115 port->max_targets = 15; 1116 port->max_target_id = 15; 1117 1118 if (ctl_port_register(&softc->ioctl_info.port, 1119 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1120 printf("ctl: ioctl front end registration failed, will " 1121 "continue anyway\n"); 1122 } 1123 1124#ifdef CTL_IO_DELAY 1125 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1126 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1127 sizeof(struct callout), CTL_TIMER_BYTES); 1128 return (EINVAL); 1129 } 1130#endif /* CTL_IO_DELAY */ 1131 1132 return (0); 1133} 1134 1135void 1136ctl_shutdown(void) 1137{ 1138 struct ctl_softc *softc; 1139 struct ctl_lun *lun, *next_lun; 1140 struct ctl_io_pool *pool; 1141 1142 softc = (struct ctl_softc *)control_softc; 1143 1144 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1145 printf("ctl: ioctl front end deregistration failed\n"); 1146 1147 mtx_lock(&softc->ctl_lock); 1148 1149 /* 1150 * Free up each LUN. 1151 */ 1152 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1153 next_lun = STAILQ_NEXT(lun, links); 1154 ctl_free_lun(lun); 1155 } 1156 1157 mtx_unlock(&softc->ctl_lock); 1158 1159 ctl_frontend_deregister(&ioctl_frontend); 1160 1161 /* 1162 * This will rip the rug out from under any FETDs or anyone else 1163 * that has a pool allocated. Since we increment our module 1164 * refcount any time someone outside the main CTL module allocates 1165 * a pool, we shouldn't have any problems here. The user won't be 1166 * able to unload the CTL module until client modules have 1167 * successfully unloaded. 1168 */ 1169 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1170 ctl_pool_free(pool); 1171 1172#if 0 1173 ctl_shutdown_thread(softc->work_thread); 1174 mtx_destroy(&softc->queue_lock); 1175#endif 1176 1177 ctl_tpc_shutdown(softc); 1178 mtx_destroy(&softc->pool_lock); 1179 mtx_destroy(&softc->ctl_lock); 1180 1181 destroy_dev(softc->dev); 1182 1183 sysctl_ctx_free(&softc->sysctl_ctx); 1184 1185 free(control_softc, M_DEVBUF); 1186 control_softc = NULL; 1187 1188 if (bootverbose) 1189 printf("ctl: CAM Target Layer unloaded\n"); 1190} 1191 1192static int 1193ctl_module_event_handler(module_t mod, int what, void *arg) 1194{ 1195 1196 switch (what) { 1197 case MOD_LOAD: 1198 return (ctl_init()); 1199 case MOD_UNLOAD: 1200 return (EBUSY); 1201 default: 1202 return (EOPNOTSUPP); 1203 } 1204} 1205 1206/* 1207 * XXX KDM should we do some access checks here? Bump a reference count to 1208 * prevent a CTL module from being unloaded while someone has it open? 1209 */ 1210static int 1211ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1212{ 1213 return (0); 1214} 1215 1216static int 1217ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1218{ 1219 return (0); 1220} 1221 1222int 1223ctl_port_enable(ctl_port_type port_type) 1224{ 1225 struct ctl_softc *softc; 1226 struct ctl_port *port; 1227 1228 if (ctl_is_single == 0) { 1229 union ctl_ha_msg msg_info; 1230 int isc_retval; 1231 1232#if 0 1233 printf("%s: HA mode, synchronizing frontend enable\n", 1234 __func__); 1235#endif 1236 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1237 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1238 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1239 printf("Sync msg send error retval %d\n", isc_retval); 1240 } 1241 if (!rcv_sync_msg) { 1242 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1243 sizeof(msg_info), 1); 1244 } 1245#if 0 1246 printf("CTL:Frontend Enable\n"); 1247 } else { 1248 printf("%s: single mode, skipping frontend synchronization\n", 1249 __func__); 1250#endif 1251 } 1252 1253 softc = control_softc; 1254 1255 STAILQ_FOREACH(port, &softc->port_list, links) { 1256 if (port_type & port->port_type) 1257 { 1258#if 0 1259 printf("port %d\n", port->targ_port); 1260#endif 1261 ctl_port_online(port); 1262 } 1263 } 1264 1265 return (0); 1266} 1267 1268int 1269ctl_port_disable(ctl_port_type port_type) 1270{ 1271 struct ctl_softc *softc; 1272 struct ctl_port *port; 1273 1274 softc = control_softc; 1275 1276 STAILQ_FOREACH(port, &softc->port_list, links) { 1277 if (port_type & port->port_type) 1278 ctl_port_offline(port); 1279 } 1280 1281 return (0); 1282} 1283 1284/* 1285 * Returns 0 for success, 1 for failure. 1286 * Currently the only failure mode is if there aren't enough entries 1287 * allocated. So, in case of a failure, look at num_entries_dropped, 1288 * reallocate and try again. 1289 */ 1290int 1291ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1292 int *num_entries_filled, int *num_entries_dropped, 1293 ctl_port_type port_type, int no_virtual) 1294{ 1295 struct ctl_softc *softc; 1296 struct ctl_port *port; 1297 int entries_dropped, entries_filled; 1298 int retval; 1299 int i; 1300 1301 softc = control_softc; 1302 1303 retval = 0; 1304 entries_filled = 0; 1305 entries_dropped = 0; 1306 1307 i = 0; 1308 mtx_lock(&softc->ctl_lock); 1309 STAILQ_FOREACH(port, &softc->port_list, links) { 1310 struct ctl_port_entry *entry; 1311 1312 if ((port->port_type & port_type) == 0) 1313 continue; 1314 1315 if ((no_virtual != 0) 1316 && (port->virtual_port != 0)) 1317 continue; 1318 1319 if (entries_filled >= num_entries_alloced) { 1320 entries_dropped++; 1321 continue; 1322 } 1323 entry = &entries[i]; 1324 1325 entry->port_type = port->port_type; 1326 strlcpy(entry->port_name, port->port_name, 1327 sizeof(entry->port_name)); 1328 entry->physical_port = port->physical_port; 1329 entry->virtual_port = port->virtual_port; 1330 entry->wwnn = port->wwnn; 1331 entry->wwpn = port->wwpn; 1332 1333 i++; 1334 entries_filled++; 1335 } 1336 1337 mtx_unlock(&softc->ctl_lock); 1338 1339 if (entries_dropped > 0) 1340 retval = 1; 1341 1342 *num_entries_dropped = entries_dropped; 1343 *num_entries_filled = entries_filled; 1344 1345 return (retval); 1346} 1347 1348static void 1349ctl_ioctl_online(void *arg) 1350{ 1351 struct ctl_ioctl_info *ioctl_info; 1352 1353 ioctl_info = (struct ctl_ioctl_info *)arg; 1354 1355 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1356} 1357 1358static void 1359ctl_ioctl_offline(void *arg) 1360{ 1361 struct ctl_ioctl_info *ioctl_info; 1362 1363 ioctl_info = (struct ctl_ioctl_info *)arg; 1364 1365 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1366} 1367 1368/* 1369 * Remove an initiator by port number and initiator ID. 1370 * Returns 0 for success, -1 for failure. 1371 */ 1372int 1373ctl_remove_initiator(struct ctl_port *port, int iid) 1374{ 1375 struct ctl_softc *softc = control_softc; 1376 1377 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1378 1379 if (iid > CTL_MAX_INIT_PER_PORT) { 1380 printf("%s: initiator ID %u > maximun %u!\n", 1381 __func__, iid, CTL_MAX_INIT_PER_PORT); 1382 return (-1); 1383 } 1384 1385 mtx_lock(&softc->ctl_lock); 1386 port->wwpn_iid[iid].in_use--; 1387 port->wwpn_iid[iid].last_use = time_uptime; 1388 mtx_unlock(&softc->ctl_lock); 1389 1390 return (0); 1391} 1392 1393/* 1394 * Add an initiator to the initiator map. 1395 * Returns iid for success, < 0 for failure. 1396 */ 1397int 1398ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1399{ 1400 struct ctl_softc *softc = control_softc; 1401 time_t best_time; 1402 int i, best; 1403 1404 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1405 1406 if (iid >= CTL_MAX_INIT_PER_PORT) { 1407 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1408 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1409 free(name, M_CTL); 1410 return (-1); 1411 } 1412 1413 mtx_lock(&softc->ctl_lock); 1414 1415 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1416 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1417 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1418 iid = i; 1419 break; 1420 } 1421 if (name != NULL && port->wwpn_iid[i].name != NULL && 1422 strcmp(name, port->wwpn_iid[i].name) == 0) { 1423 iid = i; 1424 break; 1425 } 1426 } 1427 } 1428 1429 if (iid < 0) { 1430 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1431 if (port->wwpn_iid[i].in_use == 0 && 1432 port->wwpn_iid[i].wwpn == 0 && 1433 port->wwpn_iid[i].name == NULL) { 1434 iid = i; 1435 break; 1436 } 1437 } 1438 } 1439 1440 if (iid < 0) { 1441 best = -1; 1442 best_time = INT32_MAX; 1443 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1444 if (port->wwpn_iid[i].in_use == 0) { 1445 if (port->wwpn_iid[i].last_use < best_time) { 1446 best = i; 1447 best_time = port->wwpn_iid[i].last_use; 1448 } 1449 } 1450 } 1451 iid = best; 1452 } 1453 1454 if (iid < 0) { 1455 mtx_unlock(&softc->ctl_lock); 1456 free(name, M_CTL); 1457 return (-2); 1458 } 1459 1460 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1461 /* 1462 * This is not an error yet. 1463 */ 1464 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1465#if 0 1466 printf("%s: port %d iid %u WWPN %#jx arrived" 1467 " again\n", __func__, port->targ_port, 1468 iid, (uintmax_t)wwpn); 1469#endif 1470 goto take; 1471 } 1472 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1473 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1474#if 0 1475 printf("%s: port %d iid %u name '%s' arrived" 1476 " again\n", __func__, port->targ_port, 1477 iid, name); 1478#endif 1479 goto take; 1480 } 1481 1482 /* 1483 * This is an error, but what do we do about it? The 1484 * driver is telling us we have a new WWPN for this 1485 * initiator ID, so we pretty much need to use it. 1486 */ 1487 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1488 " but WWPN %#jx '%s' is still at that address\n", 1489 __func__, port->targ_port, iid, wwpn, name, 1490 (uintmax_t)port->wwpn_iid[iid].wwpn, 1491 port->wwpn_iid[iid].name); 1492 1493 /* 1494 * XXX KDM clear have_ca and ua_pending on each LUN for 1495 * this initiator. 1496 */ 1497 } 1498take: 1499 free(port->wwpn_iid[iid].name, M_CTL); 1500 port->wwpn_iid[iid].name = name; 1501 port->wwpn_iid[iid].wwpn = wwpn; 1502 port->wwpn_iid[iid].in_use++; 1503 mtx_unlock(&softc->ctl_lock); 1504 1505 return (iid); 1506} 1507 1508static int 1509ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1510{ 1511 int len; 1512 1513 switch (port->port_type) { 1514 case CTL_PORT_FC: 1515 { 1516 struct scsi_transportid_fcp *id = 1517 (struct scsi_transportid_fcp *)buf; 1518 if (port->wwpn_iid[iid].wwpn == 0) 1519 return (0); 1520 memset(id, 0, sizeof(*id)); 1521 id->format_protocol = SCSI_PROTO_FC; 1522 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1523 return (sizeof(*id)); 1524 } 1525 case CTL_PORT_ISCSI: 1526 { 1527 struct scsi_transportid_iscsi_port *id = 1528 (struct scsi_transportid_iscsi_port *)buf; 1529 if (port->wwpn_iid[iid].name == NULL) 1530 return (0); 1531 memset(id, 0, 256); 1532 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1533 SCSI_PROTO_ISCSI; 1534 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1535 len = roundup2(min(len, 252), 4); 1536 scsi_ulto2b(len, id->additional_length); 1537 return (sizeof(*id) + len); 1538 } 1539 case CTL_PORT_SAS: 1540 { 1541 struct scsi_transportid_sas *id = 1542 (struct scsi_transportid_sas *)buf; 1543 if (port->wwpn_iid[iid].wwpn == 0) 1544 return (0); 1545 memset(id, 0, sizeof(*id)); 1546 id->format_protocol = SCSI_PROTO_SAS; 1547 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1548 return (sizeof(*id)); 1549 } 1550 default: 1551 { 1552 struct scsi_transportid_spi *id = 1553 (struct scsi_transportid_spi *)buf; 1554 memset(id, 0, sizeof(*id)); 1555 id->format_protocol = SCSI_PROTO_SPI; 1556 scsi_ulto2b(iid, id->scsi_addr); 1557 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1558 return (sizeof(*id)); 1559 } 1560 } 1561} 1562 1563static int 1564ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1565{ 1566 return (0); 1567} 1568 1569static int 1570ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1571{ 1572 return (0); 1573} 1574 1575/* 1576 * Data movement routine for the CTL ioctl frontend port. 1577 */ 1578static int 1579ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1580{ 1581 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1582 struct ctl_sg_entry ext_entry, kern_entry; 1583 int ext_sglen, ext_sg_entries, kern_sg_entries; 1584 int ext_sg_start, ext_offset; 1585 int len_to_copy, len_copied; 1586 int kern_watermark, ext_watermark; 1587 int ext_sglist_malloced; 1588 int i, j; 1589 1590 ext_sglist_malloced = 0; 1591 ext_sg_start = 0; 1592 ext_offset = 0; 1593 1594 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1595 1596 /* 1597 * If this flag is set, fake the data transfer. 1598 */ 1599 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1600 ctsio->ext_data_filled = ctsio->ext_data_len; 1601 goto bailout; 1602 } 1603 1604 /* 1605 * To simplify things here, if we have a single buffer, stick it in 1606 * a S/G entry and just make it a single entry S/G list. 1607 */ 1608 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1609 int len_seen; 1610 1611 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1612 1613 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1614 M_WAITOK); 1615 ext_sglist_malloced = 1; 1616 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1617 ext_sglen) != 0) { 1618 ctl_set_internal_failure(ctsio, 1619 /*sks_valid*/ 0, 1620 /*retry_count*/ 0); 1621 goto bailout; 1622 } 1623 ext_sg_entries = ctsio->ext_sg_entries; 1624 len_seen = 0; 1625 for (i = 0; i < ext_sg_entries; i++) { 1626 if ((len_seen + ext_sglist[i].len) >= 1627 ctsio->ext_data_filled) { 1628 ext_sg_start = i; 1629 ext_offset = ctsio->ext_data_filled - len_seen; 1630 break; 1631 } 1632 len_seen += ext_sglist[i].len; 1633 } 1634 } else { 1635 ext_sglist = &ext_entry; 1636 ext_sglist->addr = ctsio->ext_data_ptr; 1637 ext_sglist->len = ctsio->ext_data_len; 1638 ext_sg_entries = 1; 1639 ext_sg_start = 0; 1640 ext_offset = ctsio->ext_data_filled; 1641 } 1642 1643 if (ctsio->kern_sg_entries > 0) { 1644 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1645 kern_sg_entries = ctsio->kern_sg_entries; 1646 } else { 1647 kern_sglist = &kern_entry; 1648 kern_sglist->addr = ctsio->kern_data_ptr; 1649 kern_sglist->len = ctsio->kern_data_len; 1650 kern_sg_entries = 1; 1651 } 1652 1653 1654 kern_watermark = 0; 1655 ext_watermark = ext_offset; 1656 len_copied = 0; 1657 for (i = ext_sg_start, j = 0; 1658 i < ext_sg_entries && j < kern_sg_entries;) { 1659 uint8_t *ext_ptr, *kern_ptr; 1660 1661 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1662 kern_sglist[j].len - kern_watermark); 1663 1664 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1665 ext_ptr = ext_ptr + ext_watermark; 1666 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1667 /* 1668 * XXX KDM fix this! 1669 */ 1670 panic("need to implement bus address support"); 1671#if 0 1672 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1673#endif 1674 } else 1675 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1676 kern_ptr = kern_ptr + kern_watermark; 1677 1678 kern_watermark += len_to_copy; 1679 ext_watermark += len_to_copy; 1680 1681 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1682 CTL_FLAG_DATA_IN) { 1683 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1684 "bytes to user\n", len_to_copy)); 1685 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1686 "to %p\n", kern_ptr, ext_ptr)); 1687 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1688 ctl_set_internal_failure(ctsio, 1689 /*sks_valid*/ 0, 1690 /*retry_count*/ 0); 1691 goto bailout; 1692 } 1693 } else { 1694 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1695 "bytes from user\n", len_to_copy)); 1696 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1697 "to %p\n", ext_ptr, kern_ptr)); 1698 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1699 ctl_set_internal_failure(ctsio, 1700 /*sks_valid*/ 0, 1701 /*retry_count*/0); 1702 goto bailout; 1703 } 1704 } 1705 1706 len_copied += len_to_copy; 1707 1708 if (ext_sglist[i].len == ext_watermark) { 1709 i++; 1710 ext_watermark = 0; 1711 } 1712 1713 if (kern_sglist[j].len == kern_watermark) { 1714 j++; 1715 kern_watermark = 0; 1716 } 1717 } 1718 1719 ctsio->ext_data_filled += len_copied; 1720 1721 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1722 "kern_sg_entries: %d\n", ext_sg_entries, 1723 kern_sg_entries)); 1724 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1725 "kern_data_len = %d\n", ctsio->ext_data_len, 1726 ctsio->kern_data_len)); 1727 1728 1729 /* XXX KDM set residual?? */ 1730bailout: 1731 1732 if (ext_sglist_malloced != 0) 1733 free(ext_sglist, M_CTL); 1734 1735 return (CTL_RETVAL_COMPLETE); 1736} 1737 1738/* 1739 * Serialize a command that went down the "wrong" side, and so was sent to 1740 * this controller for execution. The logic is a little different than the 1741 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1742 * sent back to the other side, but in the success case, we execute the 1743 * command on this side (XFER mode) or tell the other side to execute it 1744 * (SER_ONLY mode). 1745 */ 1746static int 1747ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1748{ 1749 struct ctl_softc *ctl_softc; 1750 union ctl_ha_msg msg_info; 1751 struct ctl_lun *lun; 1752 int retval = 0; 1753 uint32_t targ_lun; 1754 1755 ctl_softc = control_softc; 1756 1757 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1758 lun = ctl_softc->ctl_luns[targ_lun]; 1759 if (lun==NULL) 1760 { 1761 /* 1762 * Why isn't LUN defined? The other side wouldn't 1763 * send a cmd if the LUN is undefined. 1764 */ 1765 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1766 1767 /* "Logical unit not supported" */ 1768 ctl_set_sense_data(&msg_info.scsi.sense_data, 1769 lun, 1770 /*sense_format*/SSD_TYPE_NONE, 1771 /*current_error*/ 1, 1772 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1773 /*asc*/ 0x25, 1774 /*ascq*/ 0x00, 1775 SSD_ELEM_NONE); 1776 1777 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1778 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1779 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1780 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1781 msg_info.hdr.serializing_sc = NULL; 1782 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1783 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1784 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1785 } 1786 return(1); 1787 1788 } 1789 1790 mtx_lock(&lun->lun_lock); 1791 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1792 1793 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1794 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1795 ooa_links))) { 1796 case CTL_ACTION_BLOCK: 1797 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1798 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1799 blocked_links); 1800 break; 1801 case CTL_ACTION_PASS: 1802 case CTL_ACTION_SKIP: 1803 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1804 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1805 ctl_enqueue_rtr((union ctl_io *)ctsio); 1806 } else { 1807 1808 /* send msg back to other side */ 1809 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1810 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1811 msg_info.hdr.msg_type = CTL_MSG_R2R; 1812#if 0 1813 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1814#endif 1815 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1816 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1817 } 1818 } 1819 break; 1820 case CTL_ACTION_OVERLAP: 1821 /* OVERLAPPED COMMANDS ATTEMPTED */ 1822 ctl_set_sense_data(&msg_info.scsi.sense_data, 1823 lun, 1824 /*sense_format*/SSD_TYPE_NONE, 1825 /*current_error*/ 1, 1826 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1827 /*asc*/ 0x4E, 1828 /*ascq*/ 0x00, 1829 SSD_ELEM_NONE); 1830 1831 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1832 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1833 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1834 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1835 msg_info.hdr.serializing_sc = NULL; 1836 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1837#if 0 1838 printf("BAD JUJU:Major Bummer Overlap\n"); 1839#endif 1840 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1841 retval = 1; 1842 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1843 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1844 } 1845 break; 1846 case CTL_ACTION_OVERLAP_TAG: 1847 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1848 ctl_set_sense_data(&msg_info.scsi.sense_data, 1849 lun, 1850 /*sense_format*/SSD_TYPE_NONE, 1851 /*current_error*/ 1, 1852 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1853 /*asc*/ 0x4D, 1854 /*ascq*/ ctsio->tag_num & 0xff, 1855 SSD_ELEM_NONE); 1856 1857 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1858 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1859 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1860 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1861 msg_info.hdr.serializing_sc = NULL; 1862 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1863#if 0 1864 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1865#endif 1866 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1867 retval = 1; 1868 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1869 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1870 } 1871 break; 1872 case CTL_ACTION_ERROR: 1873 default: 1874 /* "Internal target failure" */ 1875 ctl_set_sense_data(&msg_info.scsi.sense_data, 1876 lun, 1877 /*sense_format*/SSD_TYPE_NONE, 1878 /*current_error*/ 1, 1879 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1880 /*asc*/ 0x44, 1881 /*ascq*/ 0x00, 1882 SSD_ELEM_NONE); 1883 1884 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1885 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1886 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1887 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1888 msg_info.hdr.serializing_sc = NULL; 1889 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1890#if 0 1891 printf("BAD JUJU:Major Bummer HW Error\n"); 1892#endif 1893 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1894 retval = 1; 1895 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1896 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1897 } 1898 break; 1899 } 1900 mtx_unlock(&lun->lun_lock); 1901 return (retval); 1902} 1903 1904static int 1905ctl_ioctl_submit_wait(union ctl_io *io) 1906{ 1907 struct ctl_fe_ioctl_params params; 1908 ctl_fe_ioctl_state last_state; 1909 int done, retval; 1910 1911 retval = 0; 1912 1913 bzero(¶ms, sizeof(params)); 1914 1915 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1916 cv_init(¶ms.sem, "ctlioccv"); 1917 params.state = CTL_IOCTL_INPROG; 1918 last_state = params.state; 1919 1920 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1921 1922 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1923 1924 /* This shouldn't happen */ 1925 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1926 return (retval); 1927 1928 done = 0; 1929 1930 do { 1931 mtx_lock(¶ms.ioctl_mtx); 1932 /* 1933 * Check the state here, and don't sleep if the state has 1934 * already changed (i.e. wakeup has already occured, but we 1935 * weren't waiting yet). 1936 */ 1937 if (params.state == last_state) { 1938 /* XXX KDM cv_wait_sig instead? */ 1939 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1940 } 1941 last_state = params.state; 1942 1943 switch (params.state) { 1944 case CTL_IOCTL_INPROG: 1945 /* Why did we wake up? */ 1946 /* XXX KDM error here? */ 1947 mtx_unlock(¶ms.ioctl_mtx); 1948 break; 1949 case CTL_IOCTL_DATAMOVE: 1950 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1951 1952 /* 1953 * change last_state back to INPROG to avoid 1954 * deadlock on subsequent data moves. 1955 */ 1956 params.state = last_state = CTL_IOCTL_INPROG; 1957 1958 mtx_unlock(¶ms.ioctl_mtx); 1959 ctl_ioctl_do_datamove(&io->scsiio); 1960 /* 1961 * Note that in some cases, most notably writes, 1962 * this will queue the I/O and call us back later. 1963 * In other cases, generally reads, this routine 1964 * will immediately call back and wake us up, 1965 * probably using our own context. 1966 */ 1967 io->scsiio.be_move_done(io); 1968 break; 1969 case CTL_IOCTL_DONE: 1970 mtx_unlock(¶ms.ioctl_mtx); 1971 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1972 done = 1; 1973 break; 1974 default: 1975 mtx_unlock(¶ms.ioctl_mtx); 1976 /* XXX KDM error here? */ 1977 break; 1978 } 1979 } while (done == 0); 1980 1981 mtx_destroy(¶ms.ioctl_mtx); 1982 cv_destroy(¶ms.sem); 1983 1984 return (CTL_RETVAL_COMPLETE); 1985} 1986 1987static void 1988ctl_ioctl_datamove(union ctl_io *io) 1989{ 1990 struct ctl_fe_ioctl_params *params; 1991 1992 params = (struct ctl_fe_ioctl_params *) 1993 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1994 1995 mtx_lock(¶ms->ioctl_mtx); 1996 params->state = CTL_IOCTL_DATAMOVE; 1997 cv_broadcast(¶ms->sem); 1998 mtx_unlock(¶ms->ioctl_mtx); 1999} 2000 2001static void 2002ctl_ioctl_done(union ctl_io *io) 2003{ 2004 struct ctl_fe_ioctl_params *params; 2005 2006 params = (struct ctl_fe_ioctl_params *) 2007 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2008 2009 mtx_lock(¶ms->ioctl_mtx); 2010 params->state = CTL_IOCTL_DONE; 2011 cv_broadcast(¶ms->sem); 2012 mtx_unlock(¶ms->ioctl_mtx); 2013} 2014 2015static void 2016ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2017{ 2018 struct ctl_fe_ioctl_startstop_info *sd_info; 2019 2020 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2021 2022 sd_info->hs_info.status = metatask->status; 2023 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2024 sd_info->hs_info.luns_complete = 2025 metatask->taskinfo.startstop.luns_complete; 2026 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2027 2028 cv_broadcast(&sd_info->sem); 2029} 2030 2031static void 2032ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2033{ 2034 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2035 2036 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2037 2038 mtx_lock(fe_bbr_info->lock); 2039 fe_bbr_info->bbr_info->status = metatask->status; 2040 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2041 fe_bbr_info->wakeup_done = 1; 2042 mtx_unlock(fe_bbr_info->lock); 2043 2044 cv_broadcast(&fe_bbr_info->sem); 2045} 2046 2047/* 2048 * Returns 0 for success, errno for failure. 2049 */ 2050static int 2051ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2052 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2053{ 2054 union ctl_io *io; 2055 int retval; 2056 2057 retval = 0; 2058 2059 mtx_lock(&lun->lun_lock); 2060 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2061 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2062 ooa_links)) { 2063 struct ctl_ooa_entry *entry; 2064 2065 /* 2066 * If we've got more than we can fit, just count the 2067 * remaining entries. 2068 */ 2069 if (*cur_fill_num >= ooa_hdr->alloc_num) 2070 continue; 2071 2072 entry = &kern_entries[*cur_fill_num]; 2073 2074 entry->tag_num = io->scsiio.tag_num; 2075 entry->lun_num = lun->lun; 2076#ifdef CTL_TIME_IO 2077 entry->start_bt = io->io_hdr.start_bt; 2078#endif 2079 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2080 entry->cdb_len = io->scsiio.cdb_len; 2081 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2082 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2083 2084 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2085 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2086 2087 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2088 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2089 2090 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2091 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2092 2093 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2094 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2095 } 2096 mtx_unlock(&lun->lun_lock); 2097 2098 return (retval); 2099} 2100 2101static void * 2102ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2103 size_t error_str_len) 2104{ 2105 void *kptr; 2106 2107 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2108 2109 if (copyin(user_addr, kptr, len) != 0) { 2110 snprintf(error_str, error_str_len, "Error copying %d bytes " 2111 "from user address %p to kernel address %p", len, 2112 user_addr, kptr); 2113 free(kptr, M_CTL); 2114 return (NULL); 2115 } 2116 2117 return (kptr); 2118} 2119 2120static void 2121ctl_free_args(int num_args, struct ctl_be_arg *args) 2122{ 2123 int i; 2124 2125 if (args == NULL) 2126 return; 2127 2128 for (i = 0; i < num_args; i++) { 2129 free(args[i].kname, M_CTL); 2130 free(args[i].kvalue, M_CTL); 2131 } 2132 2133 free(args, M_CTL); 2134} 2135 2136static struct ctl_be_arg * 2137ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2138 char *error_str, size_t error_str_len) 2139{ 2140 struct ctl_be_arg *args; 2141 int i; 2142 2143 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2144 error_str, error_str_len); 2145 2146 if (args == NULL) 2147 goto bailout; 2148 2149 for (i = 0; i < num_args; i++) { 2150 args[i].kname = NULL; 2151 args[i].kvalue = NULL; 2152 } 2153 2154 for (i = 0; i < num_args; i++) { 2155 uint8_t *tmpptr; 2156 2157 args[i].kname = ctl_copyin_alloc(args[i].name, 2158 args[i].namelen, error_str, error_str_len); 2159 if (args[i].kname == NULL) 2160 goto bailout; 2161 2162 if (args[i].kname[args[i].namelen - 1] != '\0') { 2163 snprintf(error_str, error_str_len, "Argument %d " 2164 "name is not NUL-terminated", i); 2165 goto bailout; 2166 } 2167 2168 if (args[i].flags & CTL_BEARG_RD) { 2169 tmpptr = ctl_copyin_alloc(args[i].value, 2170 args[i].vallen, error_str, error_str_len); 2171 if (tmpptr == NULL) 2172 goto bailout; 2173 if ((args[i].flags & CTL_BEARG_ASCII) 2174 && (tmpptr[args[i].vallen - 1] != '\0')) { 2175 snprintf(error_str, error_str_len, "Argument " 2176 "%d value is not NUL-terminated", i); 2177 goto bailout; 2178 } 2179 args[i].kvalue = tmpptr; 2180 } else { 2181 args[i].kvalue = malloc(args[i].vallen, 2182 M_CTL, M_WAITOK | M_ZERO); 2183 } 2184 } 2185 2186 return (args); 2187bailout: 2188 2189 ctl_free_args(num_args, args); 2190 2191 return (NULL); 2192} 2193 2194static void 2195ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2196{ 2197 int i; 2198 2199 for (i = 0; i < num_args; i++) { 2200 if (args[i].flags & CTL_BEARG_WR) 2201 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2202 } 2203} 2204 2205/* 2206 * Escape characters that are illegal or not recommended in XML. 2207 */ 2208int 2209ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2210{ 2211 int retval; 2212 2213 retval = 0; 2214 2215 for (; *str; str++) { 2216 switch (*str) { 2217 case '&': 2218 retval = sbuf_printf(sb, "&"); 2219 break; 2220 case '>': 2221 retval = sbuf_printf(sb, ">"); 2222 break; 2223 case '<': 2224 retval = sbuf_printf(sb, "<"); 2225 break; 2226 default: 2227 retval = sbuf_putc(sb, *str); 2228 break; 2229 } 2230 2231 if (retval != 0) 2232 break; 2233 2234 } 2235 2236 return (retval); 2237} 2238 2239static int 2240ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2241 struct thread *td) 2242{ 2243 struct ctl_softc *softc; 2244 int retval; 2245 2246 softc = control_softc; 2247 2248 retval = 0; 2249 2250 switch (cmd) { 2251 case CTL_IO: { 2252 union ctl_io *io; 2253 void *pool_tmp; 2254 2255 /* 2256 * If we haven't been "enabled", don't allow any SCSI I/O 2257 * to this FETD. 2258 */ 2259 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2260 retval = EPERM; 2261 break; 2262 } 2263 2264 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2265 if (io == NULL) { 2266 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2267 retval = ENOSPC; 2268 break; 2269 } 2270 2271 /* 2272 * Need to save the pool reference so it doesn't get 2273 * spammed by the user's ctl_io. 2274 */ 2275 pool_tmp = io->io_hdr.pool; 2276 2277 memcpy(io, (void *)addr, sizeof(*io)); 2278 2279 io->io_hdr.pool = pool_tmp; 2280 /* 2281 * No status yet, so make sure the status is set properly. 2282 */ 2283 io->io_hdr.status = CTL_STATUS_NONE; 2284 2285 /* 2286 * The user sets the initiator ID, target and LUN IDs. 2287 */ 2288 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2289 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2290 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2291 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2292 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2293 2294 retval = ctl_ioctl_submit_wait(io); 2295 2296 if (retval != 0) { 2297 ctl_free_io(io); 2298 break; 2299 } 2300 2301 memcpy((void *)addr, io, sizeof(*io)); 2302 2303 /* return this to our pool */ 2304 ctl_free_io(io); 2305 2306 break; 2307 } 2308 case CTL_ENABLE_PORT: 2309 case CTL_DISABLE_PORT: 2310 case CTL_SET_PORT_WWNS: { 2311 struct ctl_port *port; 2312 struct ctl_port_entry *entry; 2313 2314 entry = (struct ctl_port_entry *)addr; 2315 2316 mtx_lock(&softc->ctl_lock); 2317 STAILQ_FOREACH(port, &softc->port_list, links) { 2318 int action, done; 2319 2320 action = 0; 2321 done = 0; 2322 2323 if ((entry->port_type == CTL_PORT_NONE) 2324 && (entry->targ_port == port->targ_port)) { 2325 /* 2326 * If the user only wants to enable or 2327 * disable or set WWNs on a specific port, 2328 * do the operation and we're done. 2329 */ 2330 action = 1; 2331 done = 1; 2332 } else if (entry->port_type & port->port_type) { 2333 /* 2334 * Compare the user's type mask with the 2335 * particular frontend type to see if we 2336 * have a match. 2337 */ 2338 action = 1; 2339 done = 0; 2340 2341 /* 2342 * Make sure the user isn't trying to set 2343 * WWNs on multiple ports at the same time. 2344 */ 2345 if (cmd == CTL_SET_PORT_WWNS) { 2346 printf("%s: Can't set WWNs on " 2347 "multiple ports\n", __func__); 2348 retval = EINVAL; 2349 break; 2350 } 2351 } 2352 if (action != 0) { 2353 /* 2354 * XXX KDM we have to drop the lock here, 2355 * because the online/offline operations 2356 * can potentially block. We need to 2357 * reference count the frontends so they 2358 * can't go away, 2359 */ 2360 mtx_unlock(&softc->ctl_lock); 2361 2362 if (cmd == CTL_ENABLE_PORT) { 2363 struct ctl_lun *lun; 2364 2365 STAILQ_FOREACH(lun, &softc->lun_list, 2366 links) { 2367 port->lun_enable(port->targ_lun_arg, 2368 lun->target, 2369 lun->lun); 2370 } 2371 2372 ctl_port_online(port); 2373 } else if (cmd == CTL_DISABLE_PORT) { 2374 struct ctl_lun *lun; 2375 2376 ctl_port_offline(port); 2377 2378 STAILQ_FOREACH(lun, &softc->lun_list, 2379 links) { 2380 port->lun_disable( 2381 port->targ_lun_arg, 2382 lun->target, 2383 lun->lun); 2384 } 2385 } 2386 2387 mtx_lock(&softc->ctl_lock); 2388 2389 if (cmd == CTL_SET_PORT_WWNS) 2390 ctl_port_set_wwns(port, 2391 (entry->flags & CTL_PORT_WWNN_VALID) ? 2392 1 : 0, entry->wwnn, 2393 (entry->flags & CTL_PORT_WWPN_VALID) ? 2394 1 : 0, entry->wwpn); 2395 } 2396 if (done != 0) 2397 break; 2398 } 2399 mtx_unlock(&softc->ctl_lock); 2400 break; 2401 } 2402 case CTL_GET_PORT_LIST: { 2403 struct ctl_port *port; 2404 struct ctl_port_list *list; 2405 int i; 2406 2407 list = (struct ctl_port_list *)addr; 2408 2409 if (list->alloc_len != (list->alloc_num * 2410 sizeof(struct ctl_port_entry))) { 2411 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2412 "alloc_num %u * sizeof(struct ctl_port_entry) " 2413 "%zu\n", __func__, list->alloc_len, 2414 list->alloc_num, sizeof(struct ctl_port_entry)); 2415 retval = EINVAL; 2416 break; 2417 } 2418 list->fill_len = 0; 2419 list->fill_num = 0; 2420 list->dropped_num = 0; 2421 i = 0; 2422 mtx_lock(&softc->ctl_lock); 2423 STAILQ_FOREACH(port, &softc->port_list, links) { 2424 struct ctl_port_entry entry, *list_entry; 2425 2426 if (list->fill_num >= list->alloc_num) { 2427 list->dropped_num++; 2428 continue; 2429 } 2430 2431 entry.port_type = port->port_type; 2432 strlcpy(entry.port_name, port->port_name, 2433 sizeof(entry.port_name)); 2434 entry.targ_port = port->targ_port; 2435 entry.physical_port = port->physical_port; 2436 entry.virtual_port = port->virtual_port; 2437 entry.wwnn = port->wwnn; 2438 entry.wwpn = port->wwpn; 2439 if (port->status & CTL_PORT_STATUS_ONLINE) 2440 entry.online = 1; 2441 else 2442 entry.online = 0; 2443 2444 list_entry = &list->entries[i]; 2445 2446 retval = copyout(&entry, list_entry, sizeof(entry)); 2447 if (retval != 0) { 2448 printf("%s: CTL_GET_PORT_LIST: copyout " 2449 "returned %d\n", __func__, retval); 2450 break; 2451 } 2452 i++; 2453 list->fill_num++; 2454 list->fill_len += sizeof(entry); 2455 } 2456 mtx_unlock(&softc->ctl_lock); 2457 2458 /* 2459 * If this is non-zero, we had a copyout fault, so there's 2460 * probably no point in attempting to set the status inside 2461 * the structure. 2462 */ 2463 if (retval != 0) 2464 break; 2465 2466 if (list->dropped_num > 0) 2467 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2468 else 2469 list->status = CTL_PORT_LIST_OK; 2470 break; 2471 } 2472 case CTL_DUMP_OOA: { 2473 struct ctl_lun *lun; 2474 union ctl_io *io; 2475 char printbuf[128]; 2476 struct sbuf sb; 2477 2478 mtx_lock(&softc->ctl_lock); 2479 printf("Dumping OOA queues:\n"); 2480 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2481 mtx_lock(&lun->lun_lock); 2482 for (io = (union ctl_io *)TAILQ_FIRST( 2483 &lun->ooa_queue); io != NULL; 2484 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2485 ooa_links)) { 2486 sbuf_new(&sb, printbuf, sizeof(printbuf), 2487 SBUF_FIXEDLEN); 2488 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2489 (intmax_t)lun->lun, 2490 io->scsiio.tag_num, 2491 (io->io_hdr.flags & 2492 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2493 (io->io_hdr.flags & 2494 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2495 (io->io_hdr.flags & 2496 CTL_FLAG_ABORT) ? " ABORT" : "", 2497 (io->io_hdr.flags & 2498 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2499 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2500 sbuf_finish(&sb); 2501 printf("%s\n", sbuf_data(&sb)); 2502 } 2503 mtx_unlock(&lun->lun_lock); 2504 } 2505 printf("OOA queues dump done\n"); 2506 mtx_unlock(&softc->ctl_lock); 2507 break; 2508 } 2509 case CTL_GET_OOA: { 2510 struct ctl_lun *lun; 2511 struct ctl_ooa *ooa_hdr; 2512 struct ctl_ooa_entry *entries; 2513 uint32_t cur_fill_num; 2514 2515 ooa_hdr = (struct ctl_ooa *)addr; 2516 2517 if ((ooa_hdr->alloc_len == 0) 2518 || (ooa_hdr->alloc_num == 0)) { 2519 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2520 "must be non-zero\n", __func__, 2521 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2522 retval = EINVAL; 2523 break; 2524 } 2525 2526 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2527 sizeof(struct ctl_ooa_entry))) { 2528 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2529 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2530 __func__, ooa_hdr->alloc_len, 2531 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2532 retval = EINVAL; 2533 break; 2534 } 2535 2536 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2537 if (entries == NULL) { 2538 printf("%s: could not allocate %d bytes for OOA " 2539 "dump\n", __func__, ooa_hdr->alloc_len); 2540 retval = ENOMEM; 2541 break; 2542 } 2543 2544 mtx_lock(&softc->ctl_lock); 2545 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2546 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2547 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2548 mtx_unlock(&softc->ctl_lock); 2549 free(entries, M_CTL); 2550 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2551 __func__, (uintmax_t)ooa_hdr->lun_num); 2552 retval = EINVAL; 2553 break; 2554 } 2555 2556 cur_fill_num = 0; 2557 2558 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2559 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2560 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2561 ooa_hdr, entries); 2562 if (retval != 0) 2563 break; 2564 } 2565 if (retval != 0) { 2566 mtx_unlock(&softc->ctl_lock); 2567 free(entries, M_CTL); 2568 break; 2569 } 2570 } else { 2571 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2572 2573 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2574 entries); 2575 } 2576 mtx_unlock(&softc->ctl_lock); 2577 2578 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2579 ooa_hdr->fill_len = ooa_hdr->fill_num * 2580 sizeof(struct ctl_ooa_entry); 2581 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2582 if (retval != 0) { 2583 printf("%s: error copying out %d bytes for OOA dump\n", 2584 __func__, ooa_hdr->fill_len); 2585 } 2586 2587 getbintime(&ooa_hdr->cur_bt); 2588 2589 if (cur_fill_num > ooa_hdr->alloc_num) { 2590 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2591 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2592 } else { 2593 ooa_hdr->dropped_num = 0; 2594 ooa_hdr->status = CTL_OOA_OK; 2595 } 2596 2597 free(entries, M_CTL); 2598 break; 2599 } 2600 case CTL_CHECK_OOA: { 2601 union ctl_io *io; 2602 struct ctl_lun *lun; 2603 struct ctl_ooa_info *ooa_info; 2604 2605 2606 ooa_info = (struct ctl_ooa_info *)addr; 2607 2608 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2609 ooa_info->status = CTL_OOA_INVALID_LUN; 2610 break; 2611 } 2612 mtx_lock(&softc->ctl_lock); 2613 lun = softc->ctl_luns[ooa_info->lun_id]; 2614 if (lun == NULL) { 2615 mtx_unlock(&softc->ctl_lock); 2616 ooa_info->status = CTL_OOA_INVALID_LUN; 2617 break; 2618 } 2619 mtx_lock(&lun->lun_lock); 2620 mtx_unlock(&softc->ctl_lock); 2621 ooa_info->num_entries = 0; 2622 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2623 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2624 &io->io_hdr, ooa_links)) { 2625 ooa_info->num_entries++; 2626 } 2627 mtx_unlock(&lun->lun_lock); 2628 2629 ooa_info->status = CTL_OOA_SUCCESS; 2630 2631 break; 2632 } 2633 case CTL_HARD_START: 2634 case CTL_HARD_STOP: { 2635 struct ctl_fe_ioctl_startstop_info ss_info; 2636 struct cfi_metatask *metatask; 2637 struct mtx hs_mtx; 2638 2639 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2640 2641 cv_init(&ss_info.sem, "hard start/stop cv" ); 2642 2643 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2644 if (metatask == NULL) { 2645 retval = ENOMEM; 2646 mtx_destroy(&hs_mtx); 2647 break; 2648 } 2649 2650 if (cmd == CTL_HARD_START) 2651 metatask->tasktype = CFI_TASK_STARTUP; 2652 else 2653 metatask->tasktype = CFI_TASK_SHUTDOWN; 2654 2655 metatask->callback = ctl_ioctl_hard_startstop_callback; 2656 metatask->callback_arg = &ss_info; 2657 2658 cfi_action(metatask); 2659 2660 /* Wait for the callback */ 2661 mtx_lock(&hs_mtx); 2662 cv_wait_sig(&ss_info.sem, &hs_mtx); 2663 mtx_unlock(&hs_mtx); 2664 2665 /* 2666 * All information has been copied from the metatask by the 2667 * time cv_broadcast() is called, so we free the metatask here. 2668 */ 2669 cfi_free_metatask(metatask); 2670 2671 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2672 2673 mtx_destroy(&hs_mtx); 2674 break; 2675 } 2676 case CTL_BBRREAD: { 2677 struct ctl_bbrread_info *bbr_info; 2678 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2679 struct mtx bbr_mtx; 2680 struct cfi_metatask *metatask; 2681 2682 bbr_info = (struct ctl_bbrread_info *)addr; 2683 2684 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2685 2686 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2687 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2688 2689 fe_bbr_info.bbr_info = bbr_info; 2690 fe_bbr_info.lock = &bbr_mtx; 2691 2692 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2693 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2694 2695 if (metatask == NULL) { 2696 mtx_destroy(&bbr_mtx); 2697 cv_destroy(&fe_bbr_info.sem); 2698 retval = ENOMEM; 2699 break; 2700 } 2701 metatask->tasktype = CFI_TASK_BBRREAD; 2702 metatask->callback = ctl_ioctl_bbrread_callback; 2703 metatask->callback_arg = &fe_bbr_info; 2704 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2705 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2706 metatask->taskinfo.bbrread.len = bbr_info->len; 2707 2708 cfi_action(metatask); 2709 2710 mtx_lock(&bbr_mtx); 2711 while (fe_bbr_info.wakeup_done == 0) 2712 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2713 mtx_unlock(&bbr_mtx); 2714 2715 bbr_info->status = metatask->status; 2716 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2717 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2718 memcpy(&bbr_info->sense_data, 2719 &metatask->taskinfo.bbrread.sense_data, 2720 ctl_min(sizeof(bbr_info->sense_data), 2721 sizeof(metatask->taskinfo.bbrread.sense_data))); 2722 2723 cfi_free_metatask(metatask); 2724 2725 mtx_destroy(&bbr_mtx); 2726 cv_destroy(&fe_bbr_info.sem); 2727 2728 break; 2729 } 2730 case CTL_DELAY_IO: { 2731 struct ctl_io_delay_info *delay_info; 2732#ifdef CTL_IO_DELAY 2733 struct ctl_lun *lun; 2734#endif /* CTL_IO_DELAY */ 2735 2736 delay_info = (struct ctl_io_delay_info *)addr; 2737 2738#ifdef CTL_IO_DELAY 2739 mtx_lock(&softc->ctl_lock); 2740 2741 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2742 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2743 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2744 } else { 2745 lun = softc->ctl_luns[delay_info->lun_id]; 2746 mtx_lock(&lun->lun_lock); 2747 2748 delay_info->status = CTL_DELAY_STATUS_OK; 2749 2750 switch (delay_info->delay_type) { 2751 case CTL_DELAY_TYPE_CONT: 2752 break; 2753 case CTL_DELAY_TYPE_ONESHOT: 2754 break; 2755 default: 2756 delay_info->status = 2757 CTL_DELAY_STATUS_INVALID_TYPE; 2758 break; 2759 } 2760 2761 switch (delay_info->delay_loc) { 2762 case CTL_DELAY_LOC_DATAMOVE: 2763 lun->delay_info.datamove_type = 2764 delay_info->delay_type; 2765 lun->delay_info.datamove_delay = 2766 delay_info->delay_secs; 2767 break; 2768 case CTL_DELAY_LOC_DONE: 2769 lun->delay_info.done_type = 2770 delay_info->delay_type; 2771 lun->delay_info.done_delay = 2772 delay_info->delay_secs; 2773 break; 2774 default: 2775 delay_info->status = 2776 CTL_DELAY_STATUS_INVALID_LOC; 2777 break; 2778 } 2779 mtx_unlock(&lun->lun_lock); 2780 } 2781 2782 mtx_unlock(&softc->ctl_lock); 2783#else 2784 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2785#endif /* CTL_IO_DELAY */ 2786 break; 2787 } 2788 case CTL_REALSYNC_SET: { 2789 int *syncstate; 2790 2791 syncstate = (int *)addr; 2792 2793 mtx_lock(&softc->ctl_lock); 2794 switch (*syncstate) { 2795 case 0: 2796 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2797 break; 2798 case 1: 2799 softc->flags |= CTL_FLAG_REAL_SYNC; 2800 break; 2801 default: 2802 retval = EINVAL; 2803 break; 2804 } 2805 mtx_unlock(&softc->ctl_lock); 2806 break; 2807 } 2808 case CTL_REALSYNC_GET: { 2809 int *syncstate; 2810 2811 syncstate = (int*)addr; 2812 2813 mtx_lock(&softc->ctl_lock); 2814 if (softc->flags & CTL_FLAG_REAL_SYNC) 2815 *syncstate = 1; 2816 else 2817 *syncstate = 0; 2818 mtx_unlock(&softc->ctl_lock); 2819 2820 break; 2821 } 2822 case CTL_SETSYNC: 2823 case CTL_GETSYNC: { 2824 struct ctl_sync_info *sync_info; 2825 struct ctl_lun *lun; 2826 2827 sync_info = (struct ctl_sync_info *)addr; 2828 2829 mtx_lock(&softc->ctl_lock); 2830 lun = softc->ctl_luns[sync_info->lun_id]; 2831 if (lun == NULL) { 2832 mtx_unlock(&softc->ctl_lock); 2833 sync_info->status = CTL_GS_SYNC_NO_LUN; 2834 } 2835 /* 2836 * Get or set the sync interval. We're not bounds checking 2837 * in the set case, hopefully the user won't do something 2838 * silly. 2839 */ 2840 mtx_lock(&lun->lun_lock); 2841 mtx_unlock(&softc->ctl_lock); 2842 if (cmd == CTL_GETSYNC) 2843 sync_info->sync_interval = lun->sync_interval; 2844 else 2845 lun->sync_interval = sync_info->sync_interval; 2846 mtx_unlock(&lun->lun_lock); 2847 2848 sync_info->status = CTL_GS_SYNC_OK; 2849 2850 break; 2851 } 2852 case CTL_GETSTATS: { 2853 struct ctl_stats *stats; 2854 struct ctl_lun *lun; 2855 int i; 2856 2857 stats = (struct ctl_stats *)addr; 2858 2859 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2860 stats->alloc_len) { 2861 stats->status = CTL_SS_NEED_MORE_SPACE; 2862 stats->num_luns = softc->num_luns; 2863 break; 2864 } 2865 /* 2866 * XXX KDM no locking here. If the LUN list changes, 2867 * things can blow up. 2868 */ 2869 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2870 i++, lun = STAILQ_NEXT(lun, links)) { 2871 retval = copyout(&lun->stats, &stats->lun_stats[i], 2872 sizeof(lun->stats)); 2873 if (retval != 0) 2874 break; 2875 } 2876 stats->num_luns = softc->num_luns; 2877 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2878 softc->num_luns; 2879 stats->status = CTL_SS_OK; 2880#ifdef CTL_TIME_IO 2881 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2882#else 2883 stats->flags = CTL_STATS_FLAG_NONE; 2884#endif 2885 getnanouptime(&stats->timestamp); 2886 break; 2887 } 2888 case CTL_ERROR_INJECT: { 2889 struct ctl_error_desc *err_desc, *new_err_desc; 2890 struct ctl_lun *lun; 2891 2892 err_desc = (struct ctl_error_desc *)addr; 2893 2894 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2895 M_WAITOK | M_ZERO); 2896 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2897 2898 mtx_lock(&softc->ctl_lock); 2899 lun = softc->ctl_luns[err_desc->lun_id]; 2900 if (lun == NULL) { 2901 mtx_unlock(&softc->ctl_lock); 2902 free(new_err_desc, M_CTL); 2903 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2904 __func__, (uintmax_t)err_desc->lun_id); 2905 retval = EINVAL; 2906 break; 2907 } 2908 mtx_lock(&lun->lun_lock); 2909 mtx_unlock(&softc->ctl_lock); 2910 2911 /* 2912 * We could do some checking here to verify the validity 2913 * of the request, but given the complexity of error 2914 * injection requests, the checking logic would be fairly 2915 * complex. 2916 * 2917 * For now, if the request is invalid, it just won't get 2918 * executed and might get deleted. 2919 */ 2920 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2921 2922 /* 2923 * XXX KDM check to make sure the serial number is unique, 2924 * in case we somehow manage to wrap. That shouldn't 2925 * happen for a very long time, but it's the right thing to 2926 * do. 2927 */ 2928 new_err_desc->serial = lun->error_serial; 2929 err_desc->serial = lun->error_serial; 2930 lun->error_serial++; 2931 2932 mtx_unlock(&lun->lun_lock); 2933 break; 2934 } 2935 case CTL_ERROR_INJECT_DELETE: { 2936 struct ctl_error_desc *delete_desc, *desc, *desc2; 2937 struct ctl_lun *lun; 2938 int delete_done; 2939 2940 delete_desc = (struct ctl_error_desc *)addr; 2941 delete_done = 0; 2942 2943 mtx_lock(&softc->ctl_lock); 2944 lun = softc->ctl_luns[delete_desc->lun_id]; 2945 if (lun == NULL) { 2946 mtx_unlock(&softc->ctl_lock); 2947 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2948 __func__, (uintmax_t)delete_desc->lun_id); 2949 retval = EINVAL; 2950 break; 2951 } 2952 mtx_lock(&lun->lun_lock); 2953 mtx_unlock(&softc->ctl_lock); 2954 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2955 if (desc->serial != delete_desc->serial) 2956 continue; 2957 2958 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2959 links); 2960 free(desc, M_CTL); 2961 delete_done = 1; 2962 } 2963 mtx_unlock(&lun->lun_lock); 2964 if (delete_done == 0) { 2965 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2966 "error serial %ju on LUN %u\n", __func__, 2967 delete_desc->serial, delete_desc->lun_id); 2968 retval = EINVAL; 2969 break; 2970 } 2971 break; 2972 } 2973 case CTL_DUMP_STRUCTS: { 2974 int i, j, k, idx; 2975 struct ctl_port *port; 2976 struct ctl_frontend *fe; 2977 2978 mtx_lock(&softc->ctl_lock); 2979 printf("CTL Persistent Reservation information start:\n"); 2980 for (i = 0; i < CTL_MAX_LUNS; i++) { 2981 struct ctl_lun *lun; 2982 2983 lun = softc->ctl_luns[i]; 2984 2985 if ((lun == NULL) 2986 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2987 continue; 2988 2989 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2990 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2991 idx = j * CTL_MAX_INIT_PER_PORT + k; 2992 if (lun->per_res[idx].registered == 0) 2993 continue; 2994 printf(" LUN %d port %d iid %d key " 2995 "%#jx\n", i, j, k, 2996 (uintmax_t)scsi_8btou64( 2997 lun->per_res[idx].res_key.key)); 2998 } 2999 } 3000 } 3001 printf("CTL Persistent Reservation information end\n"); 3002 printf("CTL Ports:\n"); 3003 STAILQ_FOREACH(port, &softc->port_list, links) { 3004 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3005 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3006 port->frontend->name, port->port_type, 3007 port->physical_port, port->virtual_port, 3008 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3009 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3010 if (port->wwpn_iid[j].in_use == 0 && 3011 port->wwpn_iid[j].wwpn == 0 && 3012 port->wwpn_iid[j].name == NULL) 3013 continue; 3014 3015 printf(" iid %u use %d WWPN %#jx '%s'\n", 3016 j, port->wwpn_iid[j].in_use, 3017 (uintmax_t)port->wwpn_iid[j].wwpn, 3018 port->wwpn_iid[j].name); 3019 } 3020 } 3021 printf("CTL Port information end\n"); 3022 mtx_unlock(&softc->ctl_lock); 3023 /* 3024 * XXX KDM calling this without a lock. We'd likely want 3025 * to drop the lock before calling the frontend's dump 3026 * routine anyway. 3027 */ 3028 printf("CTL Frontends:\n"); 3029 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3030 printf(" Frontend '%s'\n", fe->name); 3031 if (fe->fe_dump != NULL) 3032 fe->fe_dump(); 3033 } 3034 printf("CTL Frontend information end\n"); 3035 break; 3036 } 3037 case CTL_LUN_REQ: { 3038 struct ctl_lun_req *lun_req; 3039 struct ctl_backend_driver *backend; 3040 3041 lun_req = (struct ctl_lun_req *)addr; 3042 3043 backend = ctl_backend_find(lun_req->backend); 3044 if (backend == NULL) { 3045 lun_req->status = CTL_LUN_ERROR; 3046 snprintf(lun_req->error_str, 3047 sizeof(lun_req->error_str), 3048 "Backend \"%s\" not found.", 3049 lun_req->backend); 3050 break; 3051 } 3052 if (lun_req->num_be_args > 0) { 3053 lun_req->kern_be_args = ctl_copyin_args( 3054 lun_req->num_be_args, 3055 lun_req->be_args, 3056 lun_req->error_str, 3057 sizeof(lun_req->error_str)); 3058 if (lun_req->kern_be_args == NULL) { 3059 lun_req->status = CTL_LUN_ERROR; 3060 break; 3061 } 3062 } 3063 3064 retval = backend->ioctl(dev, cmd, addr, flag, td); 3065 3066 if (lun_req->num_be_args > 0) { 3067 ctl_copyout_args(lun_req->num_be_args, 3068 lun_req->kern_be_args); 3069 ctl_free_args(lun_req->num_be_args, 3070 lun_req->kern_be_args); 3071 } 3072 break; 3073 } 3074 case CTL_LUN_LIST: { 3075 struct sbuf *sb; 3076 struct ctl_lun *lun; 3077 struct ctl_lun_list *list; 3078 struct ctl_option *opt; 3079 3080 list = (struct ctl_lun_list *)addr; 3081 3082 /* 3083 * Allocate a fixed length sbuf here, based on the length 3084 * of the user's buffer. We could allocate an auto-extending 3085 * buffer, and then tell the user how much larger our 3086 * amount of data is than his buffer, but that presents 3087 * some problems: 3088 * 3089 * 1. The sbuf(9) routines use a blocking malloc, and so 3090 * we can't hold a lock while calling them with an 3091 * auto-extending buffer. 3092 * 3093 * 2. There is not currently a LUN reference counting 3094 * mechanism, outside of outstanding transactions on 3095 * the LUN's OOA queue. So a LUN could go away on us 3096 * while we're getting the LUN number, backend-specific 3097 * information, etc. Thus, given the way things 3098 * currently work, we need to hold the CTL lock while 3099 * grabbing LUN information. 3100 * 3101 * So, from the user's standpoint, the best thing to do is 3102 * allocate what he thinks is a reasonable buffer length, 3103 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3104 * double the buffer length and try again. (And repeat 3105 * that until he succeeds.) 3106 */ 3107 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3108 if (sb == NULL) { 3109 list->status = CTL_LUN_LIST_ERROR; 3110 snprintf(list->error_str, sizeof(list->error_str), 3111 "Unable to allocate %d bytes for LUN list", 3112 list->alloc_len); 3113 break; 3114 } 3115 3116 sbuf_printf(sb, "<ctllunlist>\n"); 3117 3118 mtx_lock(&softc->ctl_lock); 3119 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3120 mtx_lock(&lun->lun_lock); 3121 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3122 (uintmax_t)lun->lun); 3123 3124 /* 3125 * Bail out as soon as we see that we've overfilled 3126 * the buffer. 3127 */ 3128 if (retval != 0) 3129 break; 3130 3131 retval = sbuf_printf(sb, "\t<backend_type>%s" 3132 "</backend_type>\n", 3133 (lun->backend == NULL) ? "none" : 3134 lun->backend->name); 3135 3136 if (retval != 0) 3137 break; 3138 3139 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3140 lun->be_lun->lun_type); 3141 3142 if (retval != 0) 3143 break; 3144 3145 if (lun->backend == NULL) { 3146 retval = sbuf_printf(sb, "</lun>\n"); 3147 if (retval != 0) 3148 break; 3149 continue; 3150 } 3151 3152 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3153 (lun->be_lun->maxlba > 0) ? 3154 lun->be_lun->maxlba + 1 : 0); 3155 3156 if (retval != 0) 3157 break; 3158 3159 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3160 lun->be_lun->blocksize); 3161 3162 if (retval != 0) 3163 break; 3164 3165 retval = sbuf_printf(sb, "\t<serial_number>"); 3166 3167 if (retval != 0) 3168 break; 3169 3170 retval = ctl_sbuf_printf_esc(sb, 3171 lun->be_lun->serial_num); 3172 3173 if (retval != 0) 3174 break; 3175 3176 retval = sbuf_printf(sb, "</serial_number>\n"); 3177 3178 if (retval != 0) 3179 break; 3180 3181 retval = sbuf_printf(sb, "\t<device_id>"); 3182 3183 if (retval != 0) 3184 break; 3185 3186 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3187 3188 if (retval != 0) 3189 break; 3190 3191 retval = sbuf_printf(sb, "</device_id>\n"); 3192 3193 if (retval != 0) 3194 break; 3195 3196 if (lun->backend->lun_info != NULL) { 3197 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3198 if (retval != 0) 3199 break; 3200 } 3201 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3202 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3203 opt->name, opt->value, opt->name); 3204 if (retval != 0) 3205 break; 3206 } 3207 3208 retval = sbuf_printf(sb, "</lun>\n"); 3209 3210 if (retval != 0) 3211 break; 3212 mtx_unlock(&lun->lun_lock); 3213 } 3214 if (lun != NULL) 3215 mtx_unlock(&lun->lun_lock); 3216 mtx_unlock(&softc->ctl_lock); 3217 3218 if ((retval != 0) 3219 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3220 retval = 0; 3221 sbuf_delete(sb); 3222 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3223 snprintf(list->error_str, sizeof(list->error_str), 3224 "Out of space, %d bytes is too small", 3225 list->alloc_len); 3226 break; 3227 } 3228 3229 sbuf_finish(sb); 3230 3231 retval = copyout(sbuf_data(sb), list->lun_xml, 3232 sbuf_len(sb) + 1); 3233 3234 list->fill_len = sbuf_len(sb) + 1; 3235 list->status = CTL_LUN_LIST_OK; 3236 sbuf_delete(sb); 3237 break; 3238 } 3239 case CTL_ISCSI: { 3240 struct ctl_iscsi *ci; 3241 struct ctl_frontend *fe; 3242 3243 ci = (struct ctl_iscsi *)addr; 3244 3245 fe = ctl_frontend_find("iscsi"); 3246 if (fe == NULL) { 3247 ci->status = CTL_ISCSI_ERROR; 3248 snprintf(ci->error_str, sizeof(ci->error_str), 3249 "Frontend \"iscsi\" not found."); 3250 break; 3251 } 3252 3253 retval = fe->ioctl(dev, cmd, addr, flag, td); 3254 break; 3255 } 3256 case CTL_PORT_REQ: { 3257 struct ctl_req *req; 3258 struct ctl_frontend *fe; 3259 3260 req = (struct ctl_req *)addr; 3261 3262 fe = ctl_frontend_find(req->driver); 3263 if (fe == NULL) { 3264 req->status = CTL_LUN_ERROR; 3265 snprintf(req->error_str, sizeof(req->error_str), 3266 "Frontend \"%s\" not found.", req->driver); 3267 break; 3268 } 3269 if (req->num_args > 0) { 3270 req->kern_args = ctl_copyin_args(req->num_args, 3271 req->args, req->error_str, sizeof(req->error_str)); 3272 if (req->kern_args == NULL) { 3273 req->status = CTL_LUN_ERROR; 3274 break; 3275 } 3276 } 3277 3278 retval = fe->ioctl(dev, cmd, addr, flag, td); 3279 3280 if (req->num_args > 0) { 3281 ctl_copyout_args(req->num_args, req->kern_args); 3282 ctl_free_args(req->num_args, req->kern_args); 3283 } 3284 break; 3285 } 3286 case CTL_PORT_LIST: { 3287 struct sbuf *sb; 3288 struct ctl_port *port; 3289 struct ctl_lun_list *list; 3290 struct ctl_option *opt; 3291 3292 list = (struct ctl_lun_list *)addr; 3293 3294 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3295 if (sb == NULL) { 3296 list->status = CTL_LUN_LIST_ERROR; 3297 snprintf(list->error_str, sizeof(list->error_str), 3298 "Unable to allocate %d bytes for LUN list", 3299 list->alloc_len); 3300 break; 3301 } 3302 3303 sbuf_printf(sb, "<ctlportlist>\n"); 3304 3305 mtx_lock(&softc->ctl_lock); 3306 STAILQ_FOREACH(port, &softc->port_list, links) { 3307 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3308 (uintmax_t)port->targ_port); 3309 3310 /* 3311 * Bail out as soon as we see that we've overfilled 3312 * the buffer. 3313 */ 3314 if (retval != 0) 3315 break; 3316 3317 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3318 "</frontend_type>\n", port->frontend->name); 3319 if (retval != 0) 3320 break; 3321 3322 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3323 port->port_type); 3324 if (retval != 0) 3325 break; 3326 3327 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3328 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3329 if (retval != 0) 3330 break; 3331 3332 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3333 port->port_name); 3334 if (retval != 0) 3335 break; 3336 3337 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3338 port->physical_port); 3339 if (retval != 0) 3340 break; 3341 3342 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3343 port->virtual_port); 3344 if (retval != 0) 3345 break; 3346 3347 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3348 (uintmax_t)port->wwnn); 3349 if (retval != 0) 3350 break; 3351 3352 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3353 (uintmax_t)port->wwpn); 3354 if (retval != 0) 3355 break; 3356 3357 if (port->port_info != NULL) { 3358 retval = port->port_info(port->onoff_arg, sb); 3359 if (retval != 0) 3360 break; 3361 } 3362 STAILQ_FOREACH(opt, &port->options, links) { 3363 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3364 opt->name, opt->value, opt->name); 3365 if (retval != 0) 3366 break; 3367 } 3368 3369 retval = sbuf_printf(sb, "</targ_port>\n"); 3370 if (retval != 0) 3371 break; 3372 } 3373 mtx_unlock(&softc->ctl_lock); 3374 3375 if ((retval != 0) 3376 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3377 retval = 0; 3378 sbuf_delete(sb); 3379 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3380 snprintf(list->error_str, sizeof(list->error_str), 3381 "Out of space, %d bytes is too small", 3382 list->alloc_len); 3383 break; 3384 } 3385 3386 sbuf_finish(sb); 3387 3388 retval = copyout(sbuf_data(sb), list->lun_xml, 3389 sbuf_len(sb) + 1); 3390 3391 list->fill_len = sbuf_len(sb) + 1; 3392 list->status = CTL_LUN_LIST_OK; 3393 sbuf_delete(sb); 3394 break; 3395 } 3396 default: { 3397 /* XXX KDM should we fix this? */ 3398#if 0 3399 struct ctl_backend_driver *backend; 3400 unsigned int type; 3401 int found; 3402 3403 found = 0; 3404 3405 /* 3406 * We encode the backend type as the ioctl type for backend 3407 * ioctls. So parse it out here, and then search for a 3408 * backend of this type. 3409 */ 3410 type = _IOC_TYPE(cmd); 3411 3412 STAILQ_FOREACH(backend, &softc->be_list, links) { 3413 if (backend->type == type) { 3414 found = 1; 3415 break; 3416 } 3417 } 3418 if (found == 0) { 3419 printf("ctl: unknown ioctl command %#lx or backend " 3420 "%d\n", cmd, type); 3421 retval = EINVAL; 3422 break; 3423 } 3424 retval = backend->ioctl(dev, cmd, addr, flag, td); 3425#endif 3426 retval = ENOTTY; 3427 break; 3428 } 3429 } 3430 return (retval); 3431} 3432 3433uint32_t 3434ctl_get_initindex(struct ctl_nexus *nexus) 3435{ 3436 if (nexus->targ_port < CTL_MAX_PORTS) 3437 return (nexus->initid.id + 3438 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3439 else 3440 return (nexus->initid.id + 3441 ((nexus->targ_port - CTL_MAX_PORTS) * 3442 CTL_MAX_INIT_PER_PORT)); 3443} 3444 3445uint32_t 3446ctl_get_resindex(struct ctl_nexus *nexus) 3447{ 3448 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3449} 3450 3451uint32_t 3452ctl_port_idx(int port_num) 3453{ 3454 if (port_num < CTL_MAX_PORTS) 3455 return(port_num); 3456 else 3457 return(port_num - CTL_MAX_PORTS); 3458} 3459 3460static uint32_t 3461ctl_map_lun(int port_num, uint32_t lun_id) 3462{ 3463 struct ctl_port *port; 3464 3465 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3466 if (port == NULL) 3467 return (UINT32_MAX); 3468 if (port->lun_map == NULL) 3469 return (lun_id); 3470 return (port->lun_map(port->targ_lun_arg, lun_id)); 3471} 3472 3473static uint32_t 3474ctl_map_lun_back(int port_num, uint32_t lun_id) 3475{ 3476 struct ctl_port *port; 3477 uint32_t i; 3478 3479 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3480 if (port->lun_map == NULL) 3481 return (lun_id); 3482 for (i = 0; i < CTL_MAX_LUNS; i++) { 3483 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3484 return (i); 3485 } 3486 return (UINT32_MAX); 3487} 3488 3489/* 3490 * Note: This only works for bitmask sizes that are at least 32 bits, and 3491 * that are a power of 2. 3492 */ 3493int 3494ctl_ffz(uint32_t *mask, uint32_t size) 3495{ 3496 uint32_t num_chunks, num_pieces; 3497 int i, j; 3498 3499 num_chunks = (size >> 5); 3500 if (num_chunks == 0) 3501 num_chunks++; 3502 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3503 3504 for (i = 0; i < num_chunks; i++) { 3505 for (j = 0; j < num_pieces; j++) { 3506 if ((mask[i] & (1 << j)) == 0) 3507 return ((i << 5) + j); 3508 } 3509 } 3510 3511 return (-1); 3512} 3513 3514int 3515ctl_set_mask(uint32_t *mask, uint32_t bit) 3516{ 3517 uint32_t chunk, piece; 3518 3519 chunk = bit >> 5; 3520 piece = bit % (sizeof(uint32_t) * 8); 3521 3522 if ((mask[chunk] & (1 << piece)) != 0) 3523 return (-1); 3524 else 3525 mask[chunk] |= (1 << piece); 3526 3527 return (0); 3528} 3529 3530int 3531ctl_clear_mask(uint32_t *mask, uint32_t bit) 3532{ 3533 uint32_t chunk, piece; 3534 3535 chunk = bit >> 5; 3536 piece = bit % (sizeof(uint32_t) * 8); 3537 3538 if ((mask[chunk] & (1 << piece)) == 0) 3539 return (-1); 3540 else 3541 mask[chunk] &= ~(1 << piece); 3542 3543 return (0); 3544} 3545 3546int 3547ctl_is_set(uint32_t *mask, uint32_t bit) 3548{ 3549 uint32_t chunk, piece; 3550 3551 chunk = bit >> 5; 3552 piece = bit % (sizeof(uint32_t) * 8); 3553 3554 if ((mask[chunk] & (1 << piece)) == 0) 3555 return (0); 3556 else 3557 return (1); 3558} 3559 3560#ifdef unused 3561/* 3562 * The bus, target and lun are optional, they can be filled in later. 3563 * can_wait is used to determine whether we can wait on the malloc or not. 3564 */ 3565union ctl_io* 3566ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3567 uint32_t targ_lun, int can_wait) 3568{ 3569 union ctl_io *io; 3570 3571 if (can_wait) 3572 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3573 else 3574 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3575 3576 if (io != NULL) { 3577 io->io_hdr.io_type = io_type; 3578 io->io_hdr.targ_port = targ_port; 3579 /* 3580 * XXX KDM this needs to change/go away. We need to move 3581 * to a preallocated pool of ctl_scsiio structures. 3582 */ 3583 io->io_hdr.nexus.targ_target.id = targ_target; 3584 io->io_hdr.nexus.targ_lun = targ_lun; 3585 } 3586 3587 return (io); 3588} 3589 3590void 3591ctl_kfree_io(union ctl_io *io) 3592{ 3593 free(io, M_CTL); 3594} 3595#endif /* unused */ 3596 3597/* 3598 * ctl_softc, pool_type, total_ctl_io are passed in. 3599 * npool is passed out. 3600 */ 3601int 3602ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3603 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3604{ 3605 uint32_t i; 3606 union ctl_io *cur_io, *next_io; 3607 struct ctl_io_pool *pool; 3608 int retval; 3609 3610 retval = 0; 3611 3612 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3613 M_NOWAIT | M_ZERO); 3614 if (pool == NULL) { 3615 retval = ENOMEM; 3616 goto bailout; 3617 } 3618 3619 pool->type = pool_type; 3620 pool->ctl_softc = ctl_softc; 3621 3622 mtx_lock(&ctl_softc->pool_lock); 3623 pool->id = ctl_softc->cur_pool_id++; 3624 mtx_unlock(&ctl_softc->pool_lock); 3625 3626 pool->flags = CTL_POOL_FLAG_NONE; 3627 pool->refcount = 1; /* Reference for validity. */ 3628 STAILQ_INIT(&pool->free_queue); 3629 3630 /* 3631 * XXX KDM other options here: 3632 * - allocate a page at a time 3633 * - allocate one big chunk of memory. 3634 * Page allocation might work well, but would take a little more 3635 * tracking. 3636 */ 3637 for (i = 0; i < total_ctl_io; i++) { 3638 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3639 M_NOWAIT); 3640 if (cur_io == NULL) { 3641 retval = ENOMEM; 3642 break; 3643 } 3644 cur_io->io_hdr.pool = pool; 3645 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3646 pool->total_ctl_io++; 3647 pool->free_ctl_io++; 3648 } 3649 3650 if (retval != 0) { 3651 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3652 cur_io != NULL; cur_io = next_io) { 3653 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3654 links); 3655 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3656 ctl_io_hdr, links); 3657 free(cur_io, M_CTLIO); 3658 } 3659 3660 free(pool, M_CTL); 3661 goto bailout; 3662 } 3663 mtx_lock(&ctl_softc->pool_lock); 3664 ctl_softc->num_pools++; 3665 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3666 /* 3667 * Increment our usage count if this is an external consumer, so we 3668 * can't get unloaded until the external consumer (most likely a 3669 * FETD) unloads and frees his pool. 3670 * 3671 * XXX KDM will this increment the caller's module use count, or 3672 * mine? 3673 */ 3674#if 0 3675 if ((pool_type != CTL_POOL_EMERGENCY) 3676 && (pool_type != CTL_POOL_INTERNAL) 3677 && (pool_type != CTL_POOL_4OTHERSC)) 3678 MOD_INC_USE_COUNT; 3679#endif 3680 3681 mtx_unlock(&ctl_softc->pool_lock); 3682 3683 *npool = pool; 3684 3685bailout: 3686 3687 return (retval); 3688} 3689 3690static int 3691ctl_pool_acquire(struct ctl_io_pool *pool) 3692{ 3693 3694 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3695 3696 if (pool->flags & CTL_POOL_FLAG_INVALID) 3697 return (EINVAL); 3698 3699 pool->refcount++; 3700 3701 return (0); 3702} 3703 3704static void 3705ctl_pool_release(struct ctl_io_pool *pool) 3706{ 3707 struct ctl_softc *ctl_softc = pool->ctl_softc; 3708 union ctl_io *io; 3709 3710 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3711 3712 if (--pool->refcount != 0) 3713 return; 3714 3715 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3716 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3717 links); 3718 free(io, M_CTLIO); 3719 } 3720 3721 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3722 ctl_softc->num_pools--; 3723 3724 /* 3725 * XXX KDM will this decrement the caller's usage count or mine? 3726 */ 3727#if 0 3728 if ((pool->type != CTL_POOL_EMERGENCY) 3729 && (pool->type != CTL_POOL_INTERNAL) 3730 && (pool->type != CTL_POOL_4OTHERSC)) 3731 MOD_DEC_USE_COUNT; 3732#endif 3733 3734 free(pool, M_CTL); 3735} 3736 3737void 3738ctl_pool_free(struct ctl_io_pool *pool) 3739{ 3740 struct ctl_softc *ctl_softc; 3741 3742 if (pool == NULL) 3743 return; 3744 3745 ctl_softc = pool->ctl_softc; 3746 mtx_lock(&ctl_softc->pool_lock); 3747 pool->flags |= CTL_POOL_FLAG_INVALID; 3748 ctl_pool_release(pool); 3749 mtx_unlock(&ctl_softc->pool_lock); 3750} 3751 3752/* 3753 * This routine does not block (except for spinlocks of course). 3754 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3755 * possible. 3756 */ 3757union ctl_io * 3758ctl_alloc_io(void *pool_ref) 3759{ 3760 union ctl_io *io; 3761 struct ctl_softc *ctl_softc; 3762 struct ctl_io_pool *pool, *npool; 3763 struct ctl_io_pool *emergency_pool; 3764 3765 pool = (struct ctl_io_pool *)pool_ref; 3766 3767 if (pool == NULL) { 3768 printf("%s: pool is NULL\n", __func__); 3769 return (NULL); 3770 } 3771 3772 emergency_pool = NULL; 3773 3774 ctl_softc = pool->ctl_softc; 3775 3776 mtx_lock(&ctl_softc->pool_lock); 3777 /* 3778 * First, try to get the io structure from the user's pool. 3779 */ 3780 if (ctl_pool_acquire(pool) == 0) { 3781 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3782 if (io != NULL) { 3783 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3784 pool->total_allocated++; 3785 pool->free_ctl_io--; 3786 mtx_unlock(&ctl_softc->pool_lock); 3787 return (io); 3788 } else 3789 ctl_pool_release(pool); 3790 } 3791 /* 3792 * If he doesn't have any io structures left, search for an 3793 * emergency pool and grab one from there. 3794 */ 3795 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3796 if (npool->type != CTL_POOL_EMERGENCY) 3797 continue; 3798 3799 if (ctl_pool_acquire(npool) != 0) 3800 continue; 3801 3802 emergency_pool = npool; 3803 3804 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3805 if (io != NULL) { 3806 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3807 npool->total_allocated++; 3808 npool->free_ctl_io--; 3809 mtx_unlock(&ctl_softc->pool_lock); 3810 return (io); 3811 } else 3812 ctl_pool_release(npool); 3813 } 3814 3815 /* Drop the spinlock before we malloc */ 3816 mtx_unlock(&ctl_softc->pool_lock); 3817 3818 /* 3819 * The emergency pool (if it exists) didn't have one, so try an 3820 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3821 */ 3822 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3823 if (io != NULL) { 3824 /* 3825 * If the emergency pool exists but is empty, add this 3826 * ctl_io to its list when it gets freed. 3827 */ 3828 if (emergency_pool != NULL) { 3829 mtx_lock(&ctl_softc->pool_lock); 3830 if (ctl_pool_acquire(emergency_pool) == 0) { 3831 io->io_hdr.pool = emergency_pool; 3832 emergency_pool->total_ctl_io++; 3833 /* 3834 * Need to bump this, otherwise 3835 * total_allocated and total_freed won't 3836 * match when we no longer have anything 3837 * outstanding. 3838 */ 3839 emergency_pool->total_allocated++; 3840 } 3841 mtx_unlock(&ctl_softc->pool_lock); 3842 } else 3843 io->io_hdr.pool = NULL; 3844 } 3845 3846 return (io); 3847} 3848 3849void 3850ctl_free_io(union ctl_io *io) 3851{ 3852 if (io == NULL) 3853 return; 3854 3855 /* 3856 * If this ctl_io has a pool, return it to that pool. 3857 */ 3858 if (io->io_hdr.pool != NULL) { 3859 struct ctl_io_pool *pool; 3860 3861 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3862 mtx_lock(&pool->ctl_softc->pool_lock); 3863 io->io_hdr.io_type = 0xff; 3864 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3865 pool->total_freed++; 3866 pool->free_ctl_io++; 3867 ctl_pool_release(pool); 3868 mtx_unlock(&pool->ctl_softc->pool_lock); 3869 } else { 3870 /* 3871 * Otherwise, just free it. We probably malloced it and 3872 * the emergency pool wasn't available. 3873 */ 3874 free(io, M_CTLIO); 3875 } 3876 3877} 3878 3879void 3880ctl_zero_io(union ctl_io *io) 3881{ 3882 void *pool_ref; 3883 3884 if (io == NULL) 3885 return; 3886 3887 /* 3888 * May need to preserve linked list pointers at some point too. 3889 */ 3890 pool_ref = io->io_hdr.pool; 3891 3892 memset(io, 0, sizeof(*io)); 3893 3894 io->io_hdr.pool = pool_ref; 3895} 3896 3897/* 3898 * This routine is currently used for internal copies of ctl_ios that need 3899 * to persist for some reason after we've already returned status to the 3900 * FETD. (Thus the flag set.) 3901 * 3902 * XXX XXX 3903 * Note that this makes a blind copy of all fields in the ctl_io, except 3904 * for the pool reference. This includes any memory that has been 3905 * allocated! That memory will no longer be valid after done has been 3906 * called, so this would be VERY DANGEROUS for command that actually does 3907 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3908 * start and stop commands, which don't transfer any data, so this is not a 3909 * problem. If it is used for anything else, the caller would also need to 3910 * allocate data buffer space and this routine would need to be modified to 3911 * copy the data buffer(s) as well. 3912 */ 3913void 3914ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3915{ 3916 void *pool_ref; 3917 3918 if ((src == NULL) 3919 || (dest == NULL)) 3920 return; 3921 3922 /* 3923 * May need to preserve linked list pointers at some point too. 3924 */ 3925 pool_ref = dest->io_hdr.pool; 3926 3927 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3928 3929 dest->io_hdr.pool = pool_ref; 3930 /* 3931 * We need to know that this is an internal copy, and doesn't need 3932 * to get passed back to the FETD that allocated it. 3933 */ 3934 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3935} 3936 3937#ifdef NEEDTOPORT 3938static void 3939ctl_update_power_subpage(struct copan_power_subpage *page) 3940{ 3941 int num_luns, num_partitions, config_type; 3942 struct ctl_softc *softc; 3943 cs_BOOL_t aor_present, shelf_50pct_power; 3944 cs_raidset_personality_t rs_type; 3945 int max_active_luns; 3946 3947 softc = control_softc; 3948 3949 /* subtract out the processor LUN */ 3950 num_luns = softc->num_luns - 1; 3951 /* 3952 * Default to 7 LUNs active, which was the only number we allowed 3953 * in the past. 3954 */ 3955 max_active_luns = 7; 3956 3957 num_partitions = config_GetRsPartitionInfo(); 3958 config_type = config_GetConfigType(); 3959 shelf_50pct_power = config_GetShelfPowerMode(); 3960 aor_present = config_IsAorRsPresent(); 3961 3962 rs_type = ddb_GetRsRaidType(1); 3963 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3964 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3965 EPRINT(0, "Unsupported RS type %d!", rs_type); 3966 } 3967 3968 3969 page->total_luns = num_luns; 3970 3971 switch (config_type) { 3972 case 40: 3973 /* 3974 * In a 40 drive configuration, it doesn't matter what DC 3975 * cards we have, whether we have AOR enabled or not, 3976 * partitioning or not, or what type of RAIDset we have. 3977 * In that scenario, we can power up every LUN we present 3978 * to the user. 3979 */ 3980 max_active_luns = num_luns; 3981 3982 break; 3983 case 64: 3984 if (shelf_50pct_power == CS_FALSE) { 3985 /* 25% power */ 3986 if (aor_present == CS_TRUE) { 3987 if (rs_type == 3988 CS_RAIDSET_PERSONALITY_RAID5) { 3989 max_active_luns = 7; 3990 } else if (rs_type == 3991 CS_RAIDSET_PERSONALITY_RAID1){ 3992 max_active_luns = 14; 3993 } else { 3994 /* XXX KDM now what?? */ 3995 } 3996 } else { 3997 if (rs_type == 3998 CS_RAIDSET_PERSONALITY_RAID5) { 3999 max_active_luns = 8; 4000 } else if (rs_type == 4001 CS_RAIDSET_PERSONALITY_RAID1){ 4002 max_active_luns = 16; 4003 } else { 4004 /* XXX KDM now what?? */ 4005 } 4006 } 4007 } else { 4008 /* 50% power */ 4009 /* 4010 * With 50% power in a 64 drive configuration, we 4011 * can power all LUNs we present. 4012 */ 4013 max_active_luns = num_luns; 4014 } 4015 break; 4016 case 112: 4017 if (shelf_50pct_power == CS_FALSE) { 4018 /* 25% power */ 4019 if (aor_present == CS_TRUE) { 4020 if (rs_type == 4021 CS_RAIDSET_PERSONALITY_RAID5) { 4022 max_active_luns = 7; 4023 } else if (rs_type == 4024 CS_RAIDSET_PERSONALITY_RAID1){ 4025 max_active_luns = 14; 4026 } else { 4027 /* XXX KDM now what?? */ 4028 } 4029 } else { 4030 if (rs_type == 4031 CS_RAIDSET_PERSONALITY_RAID5) { 4032 max_active_luns = 8; 4033 } else if (rs_type == 4034 CS_RAIDSET_PERSONALITY_RAID1){ 4035 max_active_luns = 16; 4036 } else { 4037 /* XXX KDM now what?? */ 4038 } 4039 } 4040 } else { 4041 /* 50% power */ 4042 if (aor_present == CS_TRUE) { 4043 if (rs_type == 4044 CS_RAIDSET_PERSONALITY_RAID5) { 4045 max_active_luns = 14; 4046 } else if (rs_type == 4047 CS_RAIDSET_PERSONALITY_RAID1){ 4048 /* 4049 * We're assuming here that disk 4050 * caching is enabled, and so we're 4051 * able to power up half of each 4052 * LUN, and cache all writes. 4053 */ 4054 max_active_luns = num_luns; 4055 } else { 4056 /* XXX KDM now what?? */ 4057 } 4058 } else { 4059 if (rs_type == 4060 CS_RAIDSET_PERSONALITY_RAID5) { 4061 max_active_luns = 15; 4062 } else if (rs_type == 4063 CS_RAIDSET_PERSONALITY_RAID1){ 4064 max_active_luns = 30; 4065 } else { 4066 /* XXX KDM now what?? */ 4067 } 4068 } 4069 } 4070 break; 4071 default: 4072 /* 4073 * In this case, we have an unknown configuration, so we 4074 * just use the default from above. 4075 */ 4076 break; 4077 } 4078 4079 page->max_active_luns = max_active_luns; 4080#if 0 4081 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4082 page->total_luns, page->max_active_luns); 4083#endif 4084} 4085#endif /* NEEDTOPORT */ 4086 4087/* 4088 * This routine could be used in the future to load default and/or saved 4089 * mode page parameters for a particuar lun. 4090 */ 4091static int 4092ctl_init_page_index(struct ctl_lun *lun) 4093{ 4094 int i; 4095 struct ctl_page_index *page_index; 4096 struct ctl_softc *softc; 4097 4098 memcpy(&lun->mode_pages.index, page_index_template, 4099 sizeof(page_index_template)); 4100 4101 softc = lun->ctl_softc; 4102 4103 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4104 4105 page_index = &lun->mode_pages.index[i]; 4106 /* 4107 * If this is a disk-only mode page, there's no point in 4108 * setting it up. For some pages, we have to have some 4109 * basic information about the disk in order to calculate the 4110 * mode page data. 4111 */ 4112 if ((lun->be_lun->lun_type != T_DIRECT) 4113 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4114 continue; 4115 4116 switch (page_index->page_code & SMPH_PC_MASK) { 4117 case SMS_FORMAT_DEVICE_PAGE: { 4118 struct scsi_format_page *format_page; 4119 4120 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4121 panic("subpage is incorrect!"); 4122 4123 /* 4124 * Sectors per track are set above. Bytes per 4125 * sector need to be set here on a per-LUN basis. 4126 */ 4127 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4128 &format_page_default, 4129 sizeof(format_page_default)); 4130 memcpy(&lun->mode_pages.format_page[ 4131 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4132 sizeof(format_page_changeable)); 4133 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4134 &format_page_default, 4135 sizeof(format_page_default)); 4136 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4137 &format_page_default, 4138 sizeof(format_page_default)); 4139 4140 format_page = &lun->mode_pages.format_page[ 4141 CTL_PAGE_CURRENT]; 4142 scsi_ulto2b(lun->be_lun->blocksize, 4143 format_page->bytes_per_sector); 4144 4145 format_page = &lun->mode_pages.format_page[ 4146 CTL_PAGE_DEFAULT]; 4147 scsi_ulto2b(lun->be_lun->blocksize, 4148 format_page->bytes_per_sector); 4149 4150 format_page = &lun->mode_pages.format_page[ 4151 CTL_PAGE_SAVED]; 4152 scsi_ulto2b(lun->be_lun->blocksize, 4153 format_page->bytes_per_sector); 4154 4155 page_index->page_data = 4156 (uint8_t *)lun->mode_pages.format_page; 4157 break; 4158 } 4159 case SMS_RIGID_DISK_PAGE: { 4160 struct scsi_rigid_disk_page *rigid_disk_page; 4161 uint32_t sectors_per_cylinder; 4162 uint64_t cylinders; 4163#ifndef __XSCALE__ 4164 int shift; 4165#endif /* !__XSCALE__ */ 4166 4167 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4168 panic("invalid subpage value %d", 4169 page_index->subpage); 4170 4171 /* 4172 * Rotation rate and sectors per track are set 4173 * above. We calculate the cylinders here based on 4174 * capacity. Due to the number of heads and 4175 * sectors per track we're using, smaller arrays 4176 * may turn out to have 0 cylinders. Linux and 4177 * FreeBSD don't pay attention to these mode pages 4178 * to figure out capacity, but Solaris does. It 4179 * seems to deal with 0 cylinders just fine, and 4180 * works out a fake geometry based on the capacity. 4181 */ 4182 memcpy(&lun->mode_pages.rigid_disk_page[ 4183 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4184 sizeof(rigid_disk_page_default)); 4185 memcpy(&lun->mode_pages.rigid_disk_page[ 4186 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4187 sizeof(rigid_disk_page_changeable)); 4188 memcpy(&lun->mode_pages.rigid_disk_page[ 4189 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4190 sizeof(rigid_disk_page_default)); 4191 memcpy(&lun->mode_pages.rigid_disk_page[ 4192 CTL_PAGE_SAVED], &rigid_disk_page_default, 4193 sizeof(rigid_disk_page_default)); 4194 4195 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4196 CTL_DEFAULT_HEADS; 4197 4198 /* 4199 * The divide method here will be more accurate, 4200 * probably, but results in floating point being 4201 * used in the kernel on i386 (__udivdi3()). On the 4202 * XScale, though, __udivdi3() is implemented in 4203 * software. 4204 * 4205 * The shift method for cylinder calculation is 4206 * accurate if sectors_per_cylinder is a power of 4207 * 2. Otherwise it might be slightly off -- you 4208 * might have a bit of a truncation problem. 4209 */ 4210#ifdef __XSCALE__ 4211 cylinders = (lun->be_lun->maxlba + 1) / 4212 sectors_per_cylinder; 4213#else 4214 for (shift = 31; shift > 0; shift--) { 4215 if (sectors_per_cylinder & (1 << shift)) 4216 break; 4217 } 4218 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4219#endif 4220 4221 /* 4222 * We've basically got 3 bytes, or 24 bits for the 4223 * cylinder size in the mode page. If we're over, 4224 * just round down to 2^24. 4225 */ 4226 if (cylinders > 0xffffff) 4227 cylinders = 0xffffff; 4228 4229 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4230 CTL_PAGE_CURRENT]; 4231 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4232 4233 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4234 CTL_PAGE_DEFAULT]; 4235 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4236 4237 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4238 CTL_PAGE_SAVED]; 4239 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4240 4241 page_index->page_data = 4242 (uint8_t *)lun->mode_pages.rigid_disk_page; 4243 break; 4244 } 4245 case SMS_CACHING_PAGE: { 4246 4247 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4248 panic("invalid subpage value %d", 4249 page_index->subpage); 4250 /* 4251 * Defaults should be okay here, no calculations 4252 * needed. 4253 */ 4254 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4255 &caching_page_default, 4256 sizeof(caching_page_default)); 4257 memcpy(&lun->mode_pages.caching_page[ 4258 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4259 sizeof(caching_page_changeable)); 4260 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4261 &caching_page_default, 4262 sizeof(caching_page_default)); 4263 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4264 &caching_page_default, 4265 sizeof(caching_page_default)); 4266 page_index->page_data = 4267 (uint8_t *)lun->mode_pages.caching_page; 4268 break; 4269 } 4270 case SMS_CONTROL_MODE_PAGE: { 4271 4272 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4273 panic("invalid subpage value %d", 4274 page_index->subpage); 4275 4276 /* 4277 * Defaults should be okay here, no calculations 4278 * needed. 4279 */ 4280 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4281 &control_page_default, 4282 sizeof(control_page_default)); 4283 memcpy(&lun->mode_pages.control_page[ 4284 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4285 sizeof(control_page_changeable)); 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[CTL_PAGE_SAVED], 4290 &control_page_default, 4291 sizeof(control_page_default)); 4292 page_index->page_data = 4293 (uint8_t *)lun->mode_pages.control_page; 4294 break; 4295 4296 } 4297 case SMS_VENDOR_SPECIFIC_PAGE:{ 4298 switch (page_index->subpage) { 4299 case PWR_SUBPAGE_CODE: { 4300 struct copan_power_subpage *current_page, 4301 *saved_page; 4302 4303 memcpy(&lun->mode_pages.power_subpage[ 4304 CTL_PAGE_CURRENT], 4305 &power_page_default, 4306 sizeof(power_page_default)); 4307 memcpy(&lun->mode_pages.power_subpage[ 4308 CTL_PAGE_CHANGEABLE], 4309 &power_page_changeable, 4310 sizeof(power_page_changeable)); 4311 memcpy(&lun->mode_pages.power_subpage[ 4312 CTL_PAGE_DEFAULT], 4313 &power_page_default, 4314 sizeof(power_page_default)); 4315 memcpy(&lun->mode_pages.power_subpage[ 4316 CTL_PAGE_SAVED], 4317 &power_page_default, 4318 sizeof(power_page_default)); 4319 page_index->page_data = 4320 (uint8_t *)lun->mode_pages.power_subpage; 4321 4322 current_page = (struct copan_power_subpage *) 4323 (page_index->page_data + 4324 (page_index->page_len * 4325 CTL_PAGE_CURRENT)); 4326 saved_page = (struct copan_power_subpage *) 4327 (page_index->page_data + 4328 (page_index->page_len * 4329 CTL_PAGE_SAVED)); 4330 break; 4331 } 4332 case APS_SUBPAGE_CODE: { 4333 struct copan_aps_subpage *current_page, 4334 *saved_page; 4335 4336 // This gets set multiple times but 4337 // it should always be the same. It's 4338 // only done during init so who cares. 4339 index_to_aps_page = i; 4340 4341 memcpy(&lun->mode_pages.aps_subpage[ 4342 CTL_PAGE_CURRENT], 4343 &aps_page_default, 4344 sizeof(aps_page_default)); 4345 memcpy(&lun->mode_pages.aps_subpage[ 4346 CTL_PAGE_CHANGEABLE], 4347 &aps_page_changeable, 4348 sizeof(aps_page_changeable)); 4349 memcpy(&lun->mode_pages.aps_subpage[ 4350 CTL_PAGE_DEFAULT], 4351 &aps_page_default, 4352 sizeof(aps_page_default)); 4353 memcpy(&lun->mode_pages.aps_subpage[ 4354 CTL_PAGE_SAVED], 4355 &aps_page_default, 4356 sizeof(aps_page_default)); 4357 page_index->page_data = 4358 (uint8_t *)lun->mode_pages.aps_subpage; 4359 4360 current_page = (struct copan_aps_subpage *) 4361 (page_index->page_data + 4362 (page_index->page_len * 4363 CTL_PAGE_CURRENT)); 4364 saved_page = (struct copan_aps_subpage *) 4365 (page_index->page_data + 4366 (page_index->page_len * 4367 CTL_PAGE_SAVED)); 4368 break; 4369 } 4370 case DBGCNF_SUBPAGE_CODE: { 4371 struct copan_debugconf_subpage *current_page, 4372 *saved_page; 4373 4374 memcpy(&lun->mode_pages.debugconf_subpage[ 4375 CTL_PAGE_CURRENT], 4376 &debugconf_page_default, 4377 sizeof(debugconf_page_default)); 4378 memcpy(&lun->mode_pages.debugconf_subpage[ 4379 CTL_PAGE_CHANGEABLE], 4380 &debugconf_page_changeable, 4381 sizeof(debugconf_page_changeable)); 4382 memcpy(&lun->mode_pages.debugconf_subpage[ 4383 CTL_PAGE_DEFAULT], 4384 &debugconf_page_default, 4385 sizeof(debugconf_page_default)); 4386 memcpy(&lun->mode_pages.debugconf_subpage[ 4387 CTL_PAGE_SAVED], 4388 &debugconf_page_default, 4389 sizeof(debugconf_page_default)); 4390 page_index->page_data = 4391 (uint8_t *)lun->mode_pages.debugconf_subpage; 4392 4393 current_page = (struct copan_debugconf_subpage *) 4394 (page_index->page_data + 4395 (page_index->page_len * 4396 CTL_PAGE_CURRENT)); 4397 saved_page = (struct copan_debugconf_subpage *) 4398 (page_index->page_data + 4399 (page_index->page_len * 4400 CTL_PAGE_SAVED)); 4401 break; 4402 } 4403 default: 4404 panic("invalid subpage value %d", 4405 page_index->subpage); 4406 break; 4407 } 4408 break; 4409 } 4410 default: 4411 panic("invalid page value %d", 4412 page_index->page_code & SMPH_PC_MASK); 4413 break; 4414 } 4415 } 4416 4417 return (CTL_RETVAL_COMPLETE); 4418} 4419 4420/* 4421 * LUN allocation. 4422 * 4423 * Requirements: 4424 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4425 * wants us to allocate the LUN and he can block. 4426 * - ctl_softc is always set 4427 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4428 * 4429 * Returns 0 for success, non-zero (errno) for failure. 4430 */ 4431static int 4432ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4433 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4434{ 4435 struct ctl_lun *nlun, *lun; 4436 struct ctl_port *port; 4437 struct scsi_vpd_id_descriptor *desc; 4438 struct scsi_vpd_id_t10 *t10id; 4439 const char *eui, *naa, *scsiname, *vendor; 4440 int lun_number, i, lun_malloced; 4441 int devidlen, idlen1, idlen2 = 0, len; 4442 4443 if (be_lun == NULL) 4444 return (EINVAL); 4445 4446 /* 4447 * We currently only support Direct Access or Processor LUN types. 4448 */ 4449 switch (be_lun->lun_type) { 4450 case T_DIRECT: 4451 break; 4452 case T_PROCESSOR: 4453 break; 4454 case T_SEQUENTIAL: 4455 case T_CHANGER: 4456 default: 4457 be_lun->lun_config_status(be_lun->be_lun, 4458 CTL_LUN_CONFIG_FAILURE); 4459 break; 4460 } 4461 if (ctl_lun == NULL) { 4462 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4463 lun_malloced = 1; 4464 } else { 4465 lun_malloced = 0; 4466 lun = ctl_lun; 4467 } 4468 4469 memset(lun, 0, sizeof(*lun)); 4470 if (lun_malloced) 4471 lun->flags = CTL_LUN_MALLOCED; 4472 4473 /* Generate LUN ID. */ 4474 devidlen = max(CTL_DEVID_MIN_LEN, 4475 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4476 idlen1 = sizeof(*t10id) + devidlen; 4477 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4478 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4479 if (scsiname != NULL) { 4480 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4481 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4482 } 4483 eui = ctl_get_opt(&be_lun->options, "eui"); 4484 if (eui != NULL) { 4485 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4486 } 4487 naa = ctl_get_opt(&be_lun->options, "naa"); 4488 if (naa != NULL) { 4489 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4490 } 4491 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4492 M_CTL, M_WAITOK | M_ZERO); 4493 lun->lun_devid->len = len; 4494 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4495 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4496 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4497 desc->length = idlen1; 4498 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4499 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4500 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4501 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4502 } else { 4503 strncpy(t10id->vendor, vendor, 4504 min(sizeof(t10id->vendor), strlen(vendor))); 4505 } 4506 strncpy((char *)t10id->vendor_spec_id, 4507 (char *)be_lun->device_id, devidlen); 4508 if (scsiname != NULL) { 4509 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4510 desc->length); 4511 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4512 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4513 SVPD_ID_TYPE_SCSI_NAME; 4514 desc->length = idlen2; 4515 strlcpy(desc->identifier, scsiname, idlen2); 4516 } 4517 if (eui != NULL) { 4518 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4519 desc->length); 4520 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4521 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4522 SVPD_ID_TYPE_EUI64; 4523 desc->length = 8; 4524 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4525 } 4526 if (naa != NULL) { 4527 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4528 desc->length); 4529 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4530 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4531 SVPD_ID_TYPE_NAA; 4532 desc->length = 8; 4533 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4534 } 4535 4536 mtx_lock(&ctl_softc->ctl_lock); 4537 /* 4538 * See if the caller requested a particular LUN number. If so, see 4539 * if it is available. Otherwise, allocate the first available LUN. 4540 */ 4541 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4542 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4543 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4544 mtx_unlock(&ctl_softc->ctl_lock); 4545 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4546 printf("ctl: requested LUN ID %d is higher " 4547 "than CTL_MAX_LUNS - 1 (%d)\n", 4548 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4549 } else { 4550 /* 4551 * XXX KDM return an error, or just assign 4552 * another LUN ID in this case?? 4553 */ 4554 printf("ctl: requested LUN ID %d is already " 4555 "in use\n", be_lun->req_lun_id); 4556 } 4557 if (lun->flags & CTL_LUN_MALLOCED) 4558 free(lun, M_CTL); 4559 be_lun->lun_config_status(be_lun->be_lun, 4560 CTL_LUN_CONFIG_FAILURE); 4561 return (ENOSPC); 4562 } 4563 lun_number = be_lun->req_lun_id; 4564 } else { 4565 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4566 if (lun_number == -1) { 4567 mtx_unlock(&ctl_softc->ctl_lock); 4568 printf("ctl: can't allocate LUN on target %ju, out of " 4569 "LUNs\n", (uintmax_t)target_id.id); 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 } 4577 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4578 4579 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4580 lun->target = target_id; 4581 lun->lun = lun_number; 4582 lun->be_lun = be_lun; 4583 /* 4584 * The processor LUN is always enabled. Disk LUNs come on line 4585 * disabled, and must be enabled by the backend. 4586 */ 4587 lun->flags |= CTL_LUN_DISABLED; 4588 lun->backend = be_lun->be; 4589 be_lun->ctl_lun = lun; 4590 be_lun->lun_id = lun_number; 4591 atomic_add_int(&be_lun->be->num_luns, 1); 4592 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4593 lun->flags |= CTL_LUN_STOPPED; 4594 4595 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4596 lun->flags |= CTL_LUN_INOPERABLE; 4597 4598 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4599 lun->flags |= CTL_LUN_PRIMARY_SC; 4600 4601 lun->ctl_softc = ctl_softc; 4602 TAILQ_INIT(&lun->ooa_queue); 4603 TAILQ_INIT(&lun->blocked_queue); 4604 STAILQ_INIT(&lun->error_list); 4605 ctl_tpc_lun_init(lun); 4606 4607 /* 4608 * Initialize the mode page index. 4609 */ 4610 ctl_init_page_index(lun); 4611 4612 /* 4613 * Set the poweron UA for all initiators on this LUN only. 4614 */ 4615 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4616 lun->pending_ua[i] = CTL_UA_POWERON; 4617 4618 /* 4619 * Now, before we insert this lun on the lun list, set the lun 4620 * inventory changed UA for all other luns. 4621 */ 4622 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4623 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4624 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4625 } 4626 } 4627 4628 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4629 4630 ctl_softc->ctl_luns[lun_number] = lun; 4631 4632 ctl_softc->num_luns++; 4633 4634 /* Setup statistics gathering */ 4635 lun->stats.device_type = be_lun->lun_type; 4636 lun->stats.lun_number = lun_number; 4637 if (lun->stats.device_type == T_DIRECT) 4638 lun->stats.blocksize = be_lun->blocksize; 4639 else 4640 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4641 for (i = 0;i < CTL_MAX_PORTS;i++) 4642 lun->stats.ports[i].targ_port = i; 4643 4644 mtx_unlock(&ctl_softc->ctl_lock); 4645 4646 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4647 4648 /* 4649 * Run through each registered FETD and bring it online if it isn't 4650 * already. Enable the target ID if it hasn't been enabled, and 4651 * enable this particular LUN. 4652 */ 4653 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4654 int retval; 4655 4656 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4657 if (retval != 0) { 4658 printf("ctl_alloc_lun: FETD %s port %d returned error " 4659 "%d for lun_enable on target %ju lun %d\n", 4660 port->port_name, port->targ_port, retval, 4661 (uintmax_t)target_id.id, lun_number); 4662 } else 4663 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4664 } 4665 return (0); 4666} 4667 4668/* 4669 * Delete a LUN. 4670 * Assumptions: 4671 * - LUN has already been marked invalid and any pending I/O has been taken 4672 * care of. 4673 */ 4674static int 4675ctl_free_lun(struct ctl_lun *lun) 4676{ 4677 struct ctl_softc *softc; 4678#if 0 4679 struct ctl_port *port; 4680#endif 4681 struct ctl_lun *nlun; 4682 int i; 4683 4684 softc = lun->ctl_softc; 4685 4686 mtx_assert(&softc->ctl_lock, MA_OWNED); 4687 4688 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4689 4690 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4691 4692 softc->ctl_luns[lun->lun] = NULL; 4693 4694 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4695 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4696 4697 softc->num_luns--; 4698 4699 /* 4700 * XXX KDM this scheme only works for a single target/multiple LUN 4701 * setup. It needs to be revamped for a multiple target scheme. 4702 * 4703 * XXX KDM this results in port->lun_disable() getting called twice, 4704 * once when ctl_disable_lun() is called, and a second time here. 4705 * We really need to re-think the LUN disable semantics. There 4706 * should probably be several steps/levels to LUN removal: 4707 * - disable 4708 * - invalidate 4709 * - free 4710 * 4711 * Right now we only have a disable method when communicating to 4712 * the front end ports, at least for individual LUNs. 4713 */ 4714#if 0 4715 STAILQ_FOREACH(port, &softc->port_list, links) { 4716 int retval; 4717 4718 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4719 lun->lun); 4720 if (retval != 0) { 4721 printf("ctl_free_lun: FETD %s port %d returned error " 4722 "%d for lun_disable on target %ju lun %jd\n", 4723 port->port_name, port->targ_port, retval, 4724 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4725 } 4726 4727 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4728 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4729 4730 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4731 if (retval != 0) { 4732 printf("ctl_free_lun: FETD %s port %d " 4733 "returned error %d for targ_disable on " 4734 "target %ju\n", port->port_name, 4735 port->targ_port, retval, 4736 (uintmax_t)lun->target.id); 4737 } else 4738 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4739 4740 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4741 continue; 4742 4743#if 0 4744 port->port_offline(port->onoff_arg); 4745 port->status &= ~CTL_PORT_STATUS_ONLINE; 4746#endif 4747 } 4748 } 4749#endif 4750 4751 /* 4752 * Tell the backend to free resources, if this LUN has a backend. 4753 */ 4754 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4755 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4756 4757 ctl_tpc_lun_shutdown(lun); 4758 mtx_destroy(&lun->lun_lock); 4759 free(lun->lun_devid, M_CTL); 4760 if (lun->flags & CTL_LUN_MALLOCED) 4761 free(lun, M_CTL); 4762 4763 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4764 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4765 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4766 } 4767 } 4768 4769 return (0); 4770} 4771 4772static void 4773ctl_create_lun(struct ctl_be_lun *be_lun) 4774{ 4775 struct ctl_softc *ctl_softc; 4776 4777 ctl_softc = control_softc; 4778 4779 /* 4780 * ctl_alloc_lun() should handle all potential failure cases. 4781 */ 4782 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4783} 4784 4785int 4786ctl_add_lun(struct ctl_be_lun *be_lun) 4787{ 4788 struct ctl_softc *ctl_softc = control_softc; 4789 4790 mtx_lock(&ctl_softc->ctl_lock); 4791 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4792 mtx_unlock(&ctl_softc->ctl_lock); 4793 wakeup(&ctl_softc->pending_lun_queue); 4794 4795 return (0); 4796} 4797 4798int 4799ctl_enable_lun(struct ctl_be_lun *be_lun) 4800{ 4801 struct ctl_softc *ctl_softc; 4802 struct ctl_port *port, *nport; 4803 struct ctl_lun *lun; 4804 int retval; 4805 4806 ctl_softc = control_softc; 4807 4808 lun = (struct ctl_lun *)be_lun->ctl_lun; 4809 4810 mtx_lock(&ctl_softc->ctl_lock); 4811 mtx_lock(&lun->lun_lock); 4812 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4813 /* 4814 * eh? Why did we get called if the LUN is already 4815 * enabled? 4816 */ 4817 mtx_unlock(&lun->lun_lock); 4818 mtx_unlock(&ctl_softc->ctl_lock); 4819 return (0); 4820 } 4821 lun->flags &= ~CTL_LUN_DISABLED; 4822 mtx_unlock(&lun->lun_lock); 4823 4824 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4825 nport = STAILQ_NEXT(port, links); 4826 4827 /* 4828 * Drop the lock while we call the FETD's enable routine. 4829 * This can lead to a callback into CTL (at least in the 4830 * case of the internal initiator frontend. 4831 */ 4832 mtx_unlock(&ctl_softc->ctl_lock); 4833 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4834 mtx_lock(&ctl_softc->ctl_lock); 4835 if (retval != 0) { 4836 printf("%s: FETD %s port %d returned error " 4837 "%d for lun_enable on target %ju lun %jd\n", 4838 __func__, port->port_name, port->targ_port, retval, 4839 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4840 } 4841#if 0 4842 else { 4843 /* NOTE: TODO: why does lun enable affect port status? */ 4844 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4845 } 4846#endif 4847 } 4848 4849 mtx_unlock(&ctl_softc->ctl_lock); 4850 4851 return (0); 4852} 4853 4854int 4855ctl_disable_lun(struct ctl_be_lun *be_lun) 4856{ 4857 struct ctl_softc *ctl_softc; 4858 struct ctl_port *port; 4859 struct ctl_lun *lun; 4860 int retval; 4861 4862 ctl_softc = control_softc; 4863 4864 lun = (struct ctl_lun *)be_lun->ctl_lun; 4865 4866 mtx_lock(&ctl_softc->ctl_lock); 4867 mtx_lock(&lun->lun_lock); 4868 if (lun->flags & CTL_LUN_DISABLED) { 4869 mtx_unlock(&lun->lun_lock); 4870 mtx_unlock(&ctl_softc->ctl_lock); 4871 return (0); 4872 } 4873 lun->flags |= CTL_LUN_DISABLED; 4874 mtx_unlock(&lun->lun_lock); 4875 4876 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4877 mtx_unlock(&ctl_softc->ctl_lock); 4878 /* 4879 * Drop the lock before we call the frontend's disable 4880 * routine, to avoid lock order reversals. 4881 * 4882 * XXX KDM what happens if the frontend list changes while 4883 * we're traversing it? It's unlikely, but should be handled. 4884 */ 4885 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4886 lun->lun); 4887 mtx_lock(&ctl_softc->ctl_lock); 4888 if (retval != 0) { 4889 printf("ctl_alloc_lun: FETD %s port %d returned error " 4890 "%d for lun_disable on target %ju lun %jd\n", 4891 port->port_name, port->targ_port, retval, 4892 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4893 } 4894 } 4895 4896 mtx_unlock(&ctl_softc->ctl_lock); 4897 4898 return (0); 4899} 4900 4901int 4902ctl_start_lun(struct ctl_be_lun *be_lun) 4903{ 4904 struct ctl_softc *ctl_softc; 4905 struct ctl_lun *lun; 4906 4907 ctl_softc = control_softc; 4908 4909 lun = (struct ctl_lun *)be_lun->ctl_lun; 4910 4911 mtx_lock(&lun->lun_lock); 4912 lun->flags &= ~CTL_LUN_STOPPED; 4913 mtx_unlock(&lun->lun_lock); 4914 4915 return (0); 4916} 4917 4918int 4919ctl_stop_lun(struct ctl_be_lun *be_lun) 4920{ 4921 struct ctl_softc *ctl_softc; 4922 struct ctl_lun *lun; 4923 4924 ctl_softc = control_softc; 4925 4926 lun = (struct ctl_lun *)be_lun->ctl_lun; 4927 4928 mtx_lock(&lun->lun_lock); 4929 lun->flags |= CTL_LUN_STOPPED; 4930 mtx_unlock(&lun->lun_lock); 4931 4932 return (0); 4933} 4934 4935int 4936ctl_lun_offline(struct ctl_be_lun *be_lun) 4937{ 4938 struct ctl_softc *ctl_softc; 4939 struct ctl_lun *lun; 4940 4941 ctl_softc = control_softc; 4942 4943 lun = (struct ctl_lun *)be_lun->ctl_lun; 4944 4945 mtx_lock(&lun->lun_lock); 4946 lun->flags |= CTL_LUN_OFFLINE; 4947 mtx_unlock(&lun->lun_lock); 4948 4949 return (0); 4950} 4951 4952int 4953ctl_lun_online(struct ctl_be_lun *be_lun) 4954{ 4955 struct ctl_softc *ctl_softc; 4956 struct ctl_lun *lun; 4957 4958 ctl_softc = control_softc; 4959 4960 lun = (struct ctl_lun *)be_lun->ctl_lun; 4961 4962 mtx_lock(&lun->lun_lock); 4963 lun->flags &= ~CTL_LUN_OFFLINE; 4964 mtx_unlock(&lun->lun_lock); 4965 4966 return (0); 4967} 4968 4969int 4970ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4971{ 4972 struct ctl_softc *ctl_softc; 4973 struct ctl_lun *lun; 4974 4975 ctl_softc = control_softc; 4976 4977 lun = (struct ctl_lun *)be_lun->ctl_lun; 4978 4979 mtx_lock(&lun->lun_lock); 4980 4981 /* 4982 * The LUN needs to be disabled before it can be marked invalid. 4983 */ 4984 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4985 mtx_unlock(&lun->lun_lock); 4986 return (-1); 4987 } 4988 /* 4989 * Mark the LUN invalid. 4990 */ 4991 lun->flags |= CTL_LUN_INVALID; 4992 4993 /* 4994 * If there is nothing in the OOA queue, go ahead and free the LUN. 4995 * If we have something in the OOA queue, we'll free it when the 4996 * last I/O completes. 4997 */ 4998 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4999 mtx_unlock(&lun->lun_lock); 5000 mtx_lock(&ctl_softc->ctl_lock); 5001 ctl_free_lun(lun); 5002 mtx_unlock(&ctl_softc->ctl_lock); 5003 } else 5004 mtx_unlock(&lun->lun_lock); 5005 5006 return (0); 5007} 5008 5009int 5010ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5011{ 5012 struct ctl_softc *ctl_softc; 5013 struct ctl_lun *lun; 5014 5015 ctl_softc = control_softc; 5016 lun = (struct ctl_lun *)be_lun->ctl_lun; 5017 5018 mtx_lock(&lun->lun_lock); 5019 lun->flags |= CTL_LUN_INOPERABLE; 5020 mtx_unlock(&lun->lun_lock); 5021 5022 return (0); 5023} 5024 5025int 5026ctl_lun_operable(struct ctl_be_lun *be_lun) 5027{ 5028 struct ctl_softc *ctl_softc; 5029 struct ctl_lun *lun; 5030 5031 ctl_softc = control_softc; 5032 lun = (struct ctl_lun *)be_lun->ctl_lun; 5033 5034 mtx_lock(&lun->lun_lock); 5035 lun->flags &= ~CTL_LUN_INOPERABLE; 5036 mtx_unlock(&lun->lun_lock); 5037 5038 return (0); 5039} 5040 5041int 5042ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5043 int lock) 5044{ 5045 struct ctl_softc *softc; 5046 struct ctl_lun *lun; 5047 struct copan_aps_subpage *current_sp; 5048 struct ctl_page_index *page_index; 5049 int i; 5050 5051 softc = control_softc; 5052 5053 mtx_lock(&softc->ctl_lock); 5054 5055 lun = (struct ctl_lun *)be_lun->ctl_lun; 5056 mtx_lock(&lun->lun_lock); 5057 5058 page_index = NULL; 5059 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5060 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5061 APS_PAGE_CODE) 5062 continue; 5063 5064 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5065 continue; 5066 page_index = &lun->mode_pages.index[i]; 5067 } 5068 5069 if (page_index == NULL) { 5070 mtx_unlock(&lun->lun_lock); 5071 mtx_unlock(&softc->ctl_lock); 5072 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5073 (uintmax_t)lun->lun); 5074 return (1); 5075 } 5076#if 0 5077 if ((softc->aps_locked_lun != 0) 5078 && (softc->aps_locked_lun != lun->lun)) { 5079 printf("%s: attempt to lock LUN %llu when %llu is already " 5080 "locked\n"); 5081 mtx_unlock(&lun->lun_lock); 5082 mtx_unlock(&softc->ctl_lock); 5083 return (1); 5084 } 5085#endif 5086 5087 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5088 (page_index->page_len * CTL_PAGE_CURRENT)); 5089 5090 if (lock != 0) { 5091 current_sp->lock_active = APS_LOCK_ACTIVE; 5092 softc->aps_locked_lun = lun->lun; 5093 } else { 5094 current_sp->lock_active = 0; 5095 softc->aps_locked_lun = 0; 5096 } 5097 5098 5099 /* 5100 * If we're in HA mode, try to send the lock message to the other 5101 * side. 5102 */ 5103 if (ctl_is_single == 0) { 5104 int isc_retval; 5105 union ctl_ha_msg lock_msg; 5106 5107 lock_msg.hdr.nexus = *nexus; 5108 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5109 if (lock != 0) 5110 lock_msg.aps.lock_flag = 1; 5111 else 5112 lock_msg.aps.lock_flag = 0; 5113 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5114 sizeof(lock_msg), 0); 5115 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5116 printf("%s: APS (lock=%d) error returned from " 5117 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5118 mtx_unlock(&lun->lun_lock); 5119 mtx_unlock(&softc->ctl_lock); 5120 return (1); 5121 } 5122 } 5123 5124 mtx_unlock(&lun->lun_lock); 5125 mtx_unlock(&softc->ctl_lock); 5126 5127 return (0); 5128} 5129 5130void 5131ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5132{ 5133 struct ctl_lun *lun; 5134 struct ctl_softc *softc; 5135 int i; 5136 5137 softc = control_softc; 5138 5139 lun = (struct ctl_lun *)be_lun->ctl_lun; 5140 5141 mtx_lock(&lun->lun_lock); 5142 5143 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5144 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5145 5146 mtx_unlock(&lun->lun_lock); 5147} 5148 5149/* 5150 * Backend "memory move is complete" callback for requests that never 5151 * make it down to say RAIDCore's configuration code. 5152 */ 5153int 5154ctl_config_move_done(union ctl_io *io) 5155{ 5156 int retval; 5157 5158 retval = CTL_RETVAL_COMPLETE; 5159 5160 5161 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5162 /* 5163 * XXX KDM this shouldn't happen, but what if it does? 5164 */ 5165 if (io->io_hdr.io_type != CTL_IO_SCSI) 5166 panic("I/O type isn't CTL_IO_SCSI!"); 5167 5168 if ((io->io_hdr.port_status == 0) 5169 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5170 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5171 io->io_hdr.status = CTL_SUCCESS; 5172 else if ((io->io_hdr.port_status != 0) 5173 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5174 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5175 /* 5176 * For hardware error sense keys, the sense key 5177 * specific value is defined to be a retry count, 5178 * but we use it to pass back an internal FETD 5179 * error code. XXX KDM Hopefully the FETD is only 5180 * using 16 bits for an error code, since that's 5181 * all the space we have in the sks field. 5182 */ 5183 ctl_set_internal_failure(&io->scsiio, 5184 /*sks_valid*/ 1, 5185 /*retry_count*/ 5186 io->io_hdr.port_status); 5187 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5188 free(io->scsiio.kern_data_ptr, M_CTL); 5189 ctl_done(io); 5190 goto bailout; 5191 } 5192 5193 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5194 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5195 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5196 /* 5197 * XXX KDM just assuming a single pointer here, and not a 5198 * S/G list. If we start using S/G lists for config data, 5199 * we'll need to know how to clean them up here as well. 5200 */ 5201 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5202 free(io->scsiio.kern_data_ptr, M_CTL); 5203 /* Hopefully the user has already set the status... */ 5204 ctl_done(io); 5205 } else { 5206 /* 5207 * XXX KDM now we need to continue data movement. Some 5208 * options: 5209 * - call ctl_scsiio() again? We don't do this for data 5210 * writes, because for those at least we know ahead of 5211 * time where the write will go and how long it is. For 5212 * config writes, though, that information is largely 5213 * contained within the write itself, thus we need to 5214 * parse out the data again. 5215 * 5216 * - Call some other function once the data is in? 5217 */ 5218 5219 /* 5220 * XXX KDM call ctl_scsiio() again for now, and check flag 5221 * bits to see whether we're allocated or not. 5222 */ 5223 retval = ctl_scsiio(&io->scsiio); 5224 } 5225bailout: 5226 return (retval); 5227} 5228 5229/* 5230 * This gets called by a backend driver when it is done with a 5231 * data_submit method. 5232 */ 5233void 5234ctl_data_submit_done(union ctl_io *io) 5235{ 5236 /* 5237 * If the IO_CONT flag is set, we need to call the supplied 5238 * function to continue processing the I/O, instead of completing 5239 * the I/O just yet. 5240 * 5241 * If there is an error, though, we don't want to keep processing. 5242 * Instead, just send status back to the initiator. 5243 */ 5244 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5245 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5246 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5247 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5248 io->scsiio.io_cont(io); 5249 return; 5250 } 5251 ctl_done(io); 5252} 5253 5254/* 5255 * This gets called by a backend driver when it is done with a 5256 * configuration write. 5257 */ 5258void 5259ctl_config_write_done(union ctl_io *io) 5260{ 5261 /* 5262 * If the IO_CONT flag is set, we need to call the supplied 5263 * function to continue processing the I/O, instead of completing 5264 * the I/O just yet. 5265 * 5266 * If there is an error, though, we don't want to keep processing. 5267 * Instead, just send status back to the initiator. 5268 */ 5269 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5270 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5271 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5272 io->scsiio.io_cont(io); 5273 return; 5274 } 5275 /* 5276 * Since a configuration write can be done for commands that actually 5277 * have data allocated, like write buffer, and commands that have 5278 * no data, like start/stop unit, we need to check here. 5279 */ 5280 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5281 free(io->scsiio.kern_data_ptr, M_CTL); 5282 ctl_done(io); 5283} 5284 5285/* 5286 * SCSI release command. 5287 */ 5288int 5289ctl_scsi_release(struct ctl_scsiio *ctsio) 5290{ 5291 int length, longid, thirdparty_id, resv_id; 5292 struct ctl_softc *ctl_softc; 5293 struct ctl_lun *lun; 5294 5295 length = 0; 5296 resv_id = 0; 5297 5298 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5299 5300 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5301 ctl_softc = control_softc; 5302 5303 switch (ctsio->cdb[0]) { 5304 case RELEASE_10: { 5305 struct scsi_release_10 *cdb; 5306 5307 cdb = (struct scsi_release_10 *)ctsio->cdb; 5308 5309 if (cdb->byte2 & SR10_LONGID) 5310 longid = 1; 5311 else 5312 thirdparty_id = cdb->thirdparty_id; 5313 5314 resv_id = cdb->resv_id; 5315 length = scsi_2btoul(cdb->length); 5316 break; 5317 } 5318 } 5319 5320 5321 /* 5322 * XXX KDM right now, we only support LUN reservation. We don't 5323 * support 3rd party reservations, or extent reservations, which 5324 * might actually need the parameter list. If we've gotten this 5325 * far, we've got a LUN reservation. Anything else got kicked out 5326 * above. So, according to SPC, ignore the length. 5327 */ 5328 length = 0; 5329 5330 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5331 && (length > 0)) { 5332 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5333 ctsio->kern_data_len = length; 5334 ctsio->kern_total_len = length; 5335 ctsio->kern_data_resid = 0; 5336 ctsio->kern_rel_offset = 0; 5337 ctsio->kern_sg_entries = 0; 5338 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5339 ctsio->be_move_done = ctl_config_move_done; 5340 ctl_datamove((union ctl_io *)ctsio); 5341 5342 return (CTL_RETVAL_COMPLETE); 5343 } 5344 5345 if (length > 0) 5346 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5347 5348 mtx_lock(&lun->lun_lock); 5349 5350 /* 5351 * According to SPC, it is not an error for an intiator to attempt 5352 * to release a reservation on a LUN that isn't reserved, or that 5353 * is reserved by another initiator. The reservation can only be 5354 * released, though, by the initiator who made it or by one of 5355 * several reset type events. 5356 */ 5357 if (lun->flags & CTL_LUN_RESERVED) { 5358 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5359 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5360 && (ctsio->io_hdr.nexus.targ_target.id == 5361 lun->rsv_nexus.targ_target.id)) { 5362 lun->flags &= ~CTL_LUN_RESERVED; 5363 } 5364 } 5365 5366 mtx_unlock(&lun->lun_lock); 5367 5368 ctsio->scsi_status = SCSI_STATUS_OK; 5369 ctsio->io_hdr.status = CTL_SUCCESS; 5370 5371 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5372 free(ctsio->kern_data_ptr, M_CTL); 5373 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5374 } 5375 5376 ctl_done((union ctl_io *)ctsio); 5377 return (CTL_RETVAL_COMPLETE); 5378} 5379 5380int 5381ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5382{ 5383 int extent, thirdparty, longid; 5384 int resv_id, length; 5385 uint64_t thirdparty_id; 5386 struct ctl_softc *ctl_softc; 5387 struct ctl_lun *lun; 5388 5389 extent = 0; 5390 thirdparty = 0; 5391 longid = 0; 5392 resv_id = 0; 5393 length = 0; 5394 thirdparty_id = 0; 5395 5396 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5397 5398 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5399 ctl_softc = control_softc; 5400 5401 switch (ctsio->cdb[0]) { 5402 case RESERVE_10: { 5403 struct scsi_reserve_10 *cdb; 5404 5405 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5406 5407 if (cdb->byte2 & SR10_LONGID) 5408 longid = 1; 5409 else 5410 thirdparty_id = cdb->thirdparty_id; 5411 5412 resv_id = cdb->resv_id; 5413 length = scsi_2btoul(cdb->length); 5414 break; 5415 } 5416 } 5417 5418 /* 5419 * XXX KDM right now, we only support LUN reservation. We don't 5420 * support 3rd party reservations, or extent reservations, which 5421 * might actually need the parameter list. If we've gotten this 5422 * far, we've got a LUN reservation. Anything else got kicked out 5423 * above. So, according to SPC, ignore the length. 5424 */ 5425 length = 0; 5426 5427 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5428 && (length > 0)) { 5429 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5430 ctsio->kern_data_len = length; 5431 ctsio->kern_total_len = length; 5432 ctsio->kern_data_resid = 0; 5433 ctsio->kern_rel_offset = 0; 5434 ctsio->kern_sg_entries = 0; 5435 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5436 ctsio->be_move_done = ctl_config_move_done; 5437 ctl_datamove((union ctl_io *)ctsio); 5438 5439 return (CTL_RETVAL_COMPLETE); 5440 } 5441 5442 if (length > 0) 5443 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5444 5445 mtx_lock(&lun->lun_lock); 5446 if (lun->flags & CTL_LUN_RESERVED) { 5447 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5448 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5449 || (ctsio->io_hdr.nexus.targ_target.id != 5450 lun->rsv_nexus.targ_target.id)) { 5451 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5452 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5453 goto bailout; 5454 } 5455 } 5456 5457 lun->flags |= CTL_LUN_RESERVED; 5458 lun->rsv_nexus = ctsio->io_hdr.nexus; 5459 5460 ctsio->scsi_status = SCSI_STATUS_OK; 5461 ctsio->io_hdr.status = CTL_SUCCESS; 5462 5463bailout: 5464 mtx_unlock(&lun->lun_lock); 5465 5466 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5467 free(ctsio->kern_data_ptr, M_CTL); 5468 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5469 } 5470 5471 ctl_done((union ctl_io *)ctsio); 5472 return (CTL_RETVAL_COMPLETE); 5473} 5474 5475int 5476ctl_start_stop(struct ctl_scsiio *ctsio) 5477{ 5478 struct scsi_start_stop_unit *cdb; 5479 struct ctl_lun *lun; 5480 struct ctl_softc *ctl_softc; 5481 int retval; 5482 5483 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5484 5485 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5486 ctl_softc = control_softc; 5487 retval = 0; 5488 5489 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5490 5491 /* 5492 * XXX KDM 5493 * We don't support the immediate bit on a stop unit. In order to 5494 * do that, we would need to code up a way to know that a stop is 5495 * pending, and hold off any new commands until it completes, one 5496 * way or another. Then we could accept or reject those commands 5497 * depending on its status. We would almost need to do the reverse 5498 * of what we do below for an immediate start -- return the copy of 5499 * the ctl_io to the FETD with status to send to the host (and to 5500 * free the copy!) and then free the original I/O once the stop 5501 * actually completes. That way, the OOA queue mechanism can work 5502 * to block commands that shouldn't proceed. Another alternative 5503 * would be to put the copy in the queue in place of the original, 5504 * and return the original back to the caller. That could be 5505 * slightly safer.. 5506 */ 5507 if ((cdb->byte2 & SSS_IMMED) 5508 && ((cdb->how & SSS_START) == 0)) { 5509 ctl_set_invalid_field(ctsio, 5510 /*sks_valid*/ 1, 5511 /*command*/ 1, 5512 /*field*/ 1, 5513 /*bit_valid*/ 1, 5514 /*bit*/ 0); 5515 ctl_done((union ctl_io *)ctsio); 5516 return (CTL_RETVAL_COMPLETE); 5517 } 5518 5519 if ((lun->flags & CTL_LUN_PR_RESERVED) 5520 && ((cdb->how & SSS_START)==0)) { 5521 uint32_t residx; 5522 5523 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5524 if (!lun->per_res[residx].registered 5525 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5526 5527 ctl_set_reservation_conflict(ctsio); 5528 ctl_done((union ctl_io *)ctsio); 5529 return (CTL_RETVAL_COMPLETE); 5530 } 5531 } 5532 5533 /* 5534 * If there is no backend on this device, we can't start or stop 5535 * it. In theory we shouldn't get any start/stop commands in the 5536 * first place at this level if the LUN doesn't have a backend. 5537 * That should get stopped by the command decode code. 5538 */ 5539 if (lun->backend == NULL) { 5540 ctl_set_invalid_opcode(ctsio); 5541 ctl_done((union ctl_io *)ctsio); 5542 return (CTL_RETVAL_COMPLETE); 5543 } 5544 5545 /* 5546 * XXX KDM Copan-specific offline behavior. 5547 * Figure out a reasonable way to port this? 5548 */ 5549#ifdef NEEDTOPORT 5550 mtx_lock(&lun->lun_lock); 5551 5552 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5553 && (lun->flags & CTL_LUN_OFFLINE)) { 5554 /* 5555 * If the LUN is offline, and the on/offline bit isn't set, 5556 * reject the start or stop. Otherwise, let it through. 5557 */ 5558 mtx_unlock(&lun->lun_lock); 5559 ctl_set_lun_not_ready(ctsio); 5560 ctl_done((union ctl_io *)ctsio); 5561 } else { 5562 mtx_unlock(&lun->lun_lock); 5563#endif /* NEEDTOPORT */ 5564 /* 5565 * This could be a start or a stop when we're online, 5566 * or a stop/offline or start/online. A start or stop when 5567 * we're offline is covered in the case above. 5568 */ 5569 /* 5570 * In the non-immediate case, we send the request to 5571 * the backend and return status to the user when 5572 * it is done. 5573 * 5574 * In the immediate case, we allocate a new ctl_io 5575 * to hold a copy of the request, and send that to 5576 * the backend. We then set good status on the 5577 * user's request and return it immediately. 5578 */ 5579 if (cdb->byte2 & SSS_IMMED) { 5580 union ctl_io *new_io; 5581 5582 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5583 if (new_io == NULL) { 5584 ctl_set_busy(ctsio); 5585 ctl_done((union ctl_io *)ctsio); 5586 } else { 5587 ctl_copy_io((union ctl_io *)ctsio, 5588 new_io); 5589 retval = lun->backend->config_write(new_io); 5590 ctl_set_success(ctsio); 5591 ctl_done((union ctl_io *)ctsio); 5592 } 5593 } else { 5594 retval = lun->backend->config_write( 5595 (union ctl_io *)ctsio); 5596 } 5597#ifdef NEEDTOPORT 5598 } 5599#endif 5600 return (retval); 5601} 5602 5603/* 5604 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5605 * we don't really do anything with the LBA and length fields if the user 5606 * passes them in. Instead we'll just flush out the cache for the entire 5607 * LUN. 5608 */ 5609int 5610ctl_sync_cache(struct ctl_scsiio *ctsio) 5611{ 5612 struct ctl_lun *lun; 5613 struct ctl_softc *ctl_softc; 5614 uint64_t starting_lba; 5615 uint32_t block_count; 5616 int retval; 5617 5618 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5619 5620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5621 ctl_softc = control_softc; 5622 retval = 0; 5623 5624 switch (ctsio->cdb[0]) { 5625 case SYNCHRONIZE_CACHE: { 5626 struct scsi_sync_cache *cdb; 5627 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5628 5629 starting_lba = scsi_4btoul(cdb->begin_lba); 5630 block_count = scsi_2btoul(cdb->lb_count); 5631 break; 5632 } 5633 case SYNCHRONIZE_CACHE_16: { 5634 struct scsi_sync_cache_16 *cdb; 5635 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5636 5637 starting_lba = scsi_8btou64(cdb->begin_lba); 5638 block_count = scsi_4btoul(cdb->lb_count); 5639 break; 5640 } 5641 default: 5642 ctl_set_invalid_opcode(ctsio); 5643 ctl_done((union ctl_io *)ctsio); 5644 goto bailout; 5645 break; /* NOTREACHED */ 5646 } 5647 5648 /* 5649 * We check the LBA and length, but don't do anything with them. 5650 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5651 * get flushed. This check will just help satisfy anyone who wants 5652 * to see an error for an out of range LBA. 5653 */ 5654 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5655 ctl_set_lba_out_of_range(ctsio); 5656 ctl_done((union ctl_io *)ctsio); 5657 goto bailout; 5658 } 5659 5660 /* 5661 * If this LUN has no backend, we can't flush the cache anyway. 5662 */ 5663 if (lun->backend == NULL) { 5664 ctl_set_invalid_opcode(ctsio); 5665 ctl_done((union ctl_io *)ctsio); 5666 goto bailout; 5667 } 5668 5669 /* 5670 * Check to see whether we're configured to send the SYNCHRONIZE 5671 * CACHE command directly to the back end. 5672 */ 5673 mtx_lock(&lun->lun_lock); 5674 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5675 && (++(lun->sync_count) >= lun->sync_interval)) { 5676 lun->sync_count = 0; 5677 mtx_unlock(&lun->lun_lock); 5678 retval = lun->backend->config_write((union ctl_io *)ctsio); 5679 } else { 5680 mtx_unlock(&lun->lun_lock); 5681 ctl_set_success(ctsio); 5682 ctl_done((union ctl_io *)ctsio); 5683 } 5684 5685bailout: 5686 5687 return (retval); 5688} 5689 5690int 5691ctl_format(struct ctl_scsiio *ctsio) 5692{ 5693 struct scsi_format *cdb; 5694 struct ctl_lun *lun; 5695 struct ctl_softc *ctl_softc; 5696 int length, defect_list_len; 5697 5698 CTL_DEBUG_PRINT(("ctl_format\n")); 5699 5700 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5701 ctl_softc = control_softc; 5702 5703 cdb = (struct scsi_format *)ctsio->cdb; 5704 5705 length = 0; 5706 if (cdb->byte2 & SF_FMTDATA) { 5707 if (cdb->byte2 & SF_LONGLIST) 5708 length = sizeof(struct scsi_format_header_long); 5709 else 5710 length = sizeof(struct scsi_format_header_short); 5711 } 5712 5713 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5714 && (length > 0)) { 5715 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5716 ctsio->kern_data_len = length; 5717 ctsio->kern_total_len = length; 5718 ctsio->kern_data_resid = 0; 5719 ctsio->kern_rel_offset = 0; 5720 ctsio->kern_sg_entries = 0; 5721 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5722 ctsio->be_move_done = ctl_config_move_done; 5723 ctl_datamove((union ctl_io *)ctsio); 5724 5725 return (CTL_RETVAL_COMPLETE); 5726 } 5727 5728 defect_list_len = 0; 5729 5730 if (cdb->byte2 & SF_FMTDATA) { 5731 if (cdb->byte2 & SF_LONGLIST) { 5732 struct scsi_format_header_long *header; 5733 5734 header = (struct scsi_format_header_long *) 5735 ctsio->kern_data_ptr; 5736 5737 defect_list_len = scsi_4btoul(header->defect_list_len); 5738 if (defect_list_len != 0) { 5739 ctl_set_invalid_field(ctsio, 5740 /*sks_valid*/ 1, 5741 /*command*/ 0, 5742 /*field*/ 2, 5743 /*bit_valid*/ 0, 5744 /*bit*/ 0); 5745 goto bailout; 5746 } 5747 } else { 5748 struct scsi_format_header_short *header; 5749 5750 header = (struct scsi_format_header_short *) 5751 ctsio->kern_data_ptr; 5752 5753 defect_list_len = scsi_2btoul(header->defect_list_len); 5754 if (defect_list_len != 0) { 5755 ctl_set_invalid_field(ctsio, 5756 /*sks_valid*/ 1, 5757 /*command*/ 0, 5758 /*field*/ 2, 5759 /*bit_valid*/ 0, 5760 /*bit*/ 0); 5761 goto bailout; 5762 } 5763 } 5764 } 5765 5766 /* 5767 * The format command will clear out the "Medium format corrupted" 5768 * status if set by the configuration code. That status is really 5769 * just a way to notify the host that we have lost the media, and 5770 * get them to issue a command that will basically make them think 5771 * they're blowing away the media. 5772 */ 5773 mtx_lock(&lun->lun_lock); 5774 lun->flags &= ~CTL_LUN_INOPERABLE; 5775 mtx_unlock(&lun->lun_lock); 5776 5777 ctsio->scsi_status = SCSI_STATUS_OK; 5778 ctsio->io_hdr.status = CTL_SUCCESS; 5779bailout: 5780 5781 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5782 free(ctsio->kern_data_ptr, M_CTL); 5783 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5784 } 5785 5786 ctl_done((union ctl_io *)ctsio); 5787 return (CTL_RETVAL_COMPLETE); 5788} 5789 5790int 5791ctl_read_buffer(struct ctl_scsiio *ctsio) 5792{ 5793 struct scsi_read_buffer *cdb; 5794 struct ctl_lun *lun; 5795 int buffer_offset, len; 5796 static uint8_t descr[4]; 5797 static uint8_t echo_descr[4] = { 0 }; 5798 5799 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5800 5801 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5802 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5803 5804 if (lun->flags & CTL_LUN_PR_RESERVED) { 5805 uint32_t residx; 5806 5807 /* 5808 * XXX KDM need a lock here. 5809 */ 5810 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5811 if ((lun->res_type == SPR_TYPE_EX_AC 5812 && residx != lun->pr_res_idx) 5813 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5814 || lun->res_type == SPR_TYPE_EX_AC_AR) 5815 && !lun->per_res[residx].registered)) { 5816 ctl_set_reservation_conflict(ctsio); 5817 ctl_done((union ctl_io *)ctsio); 5818 return (CTL_RETVAL_COMPLETE); 5819 } 5820 } 5821 5822 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5823 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5824 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5825 ctl_set_invalid_field(ctsio, 5826 /*sks_valid*/ 1, 5827 /*command*/ 1, 5828 /*field*/ 1, 5829 /*bit_valid*/ 1, 5830 /*bit*/ 4); 5831 ctl_done((union ctl_io *)ctsio); 5832 return (CTL_RETVAL_COMPLETE); 5833 } 5834 5835 len = scsi_3btoul(cdb->length); 5836 buffer_offset = scsi_3btoul(cdb->offset); 5837 5838 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5839 ctl_set_invalid_field(ctsio, 5840 /*sks_valid*/ 1, 5841 /*command*/ 1, 5842 /*field*/ 6, 5843 /*bit_valid*/ 0, 5844 /*bit*/ 0); 5845 ctl_done((union ctl_io *)ctsio); 5846 return (CTL_RETVAL_COMPLETE); 5847 } 5848 5849 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5850 descr[0] = 0; 5851 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5852 ctsio->kern_data_ptr = descr; 5853 len = min(len, sizeof(descr)); 5854 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5855 ctsio->kern_data_ptr = echo_descr; 5856 len = min(len, sizeof(echo_descr)); 5857 } else 5858 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5859 ctsio->kern_data_len = len; 5860 ctsio->kern_total_len = len; 5861 ctsio->kern_data_resid = 0; 5862 ctsio->kern_rel_offset = 0; 5863 ctsio->kern_sg_entries = 0; 5864 ctsio->be_move_done = ctl_config_move_done; 5865 ctl_datamove((union ctl_io *)ctsio); 5866 5867 return (CTL_RETVAL_COMPLETE); 5868} 5869 5870int 5871ctl_write_buffer(struct ctl_scsiio *ctsio) 5872{ 5873 struct scsi_write_buffer *cdb; 5874 struct ctl_lun *lun; 5875 int buffer_offset, len; 5876 5877 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5878 5879 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5880 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5881 5882 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5883 ctl_set_invalid_field(ctsio, 5884 /*sks_valid*/ 1, 5885 /*command*/ 1, 5886 /*field*/ 1, 5887 /*bit_valid*/ 1, 5888 /*bit*/ 4); 5889 ctl_done((union ctl_io *)ctsio); 5890 return (CTL_RETVAL_COMPLETE); 5891 } 5892 5893 len = scsi_3btoul(cdb->length); 5894 buffer_offset = scsi_3btoul(cdb->offset); 5895 5896 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5897 ctl_set_invalid_field(ctsio, 5898 /*sks_valid*/ 1, 5899 /*command*/ 1, 5900 /*field*/ 6, 5901 /*bit_valid*/ 0, 5902 /*bit*/ 0); 5903 ctl_done((union ctl_io *)ctsio); 5904 return (CTL_RETVAL_COMPLETE); 5905 } 5906 5907 /* 5908 * If we've got a kernel request that hasn't been malloced yet, 5909 * malloc it and tell the caller the data buffer is here. 5910 */ 5911 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5912 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5913 ctsio->kern_data_len = len; 5914 ctsio->kern_total_len = len; 5915 ctsio->kern_data_resid = 0; 5916 ctsio->kern_rel_offset = 0; 5917 ctsio->kern_sg_entries = 0; 5918 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5919 ctsio->be_move_done = ctl_config_move_done; 5920 ctl_datamove((union ctl_io *)ctsio); 5921 5922 return (CTL_RETVAL_COMPLETE); 5923 } 5924 5925 ctl_done((union ctl_io *)ctsio); 5926 5927 return (CTL_RETVAL_COMPLETE); 5928} 5929 5930int 5931ctl_write_same(struct ctl_scsiio *ctsio) 5932{ 5933 struct ctl_lun *lun; 5934 struct ctl_lba_len_flags *lbalen; 5935 uint64_t lba; 5936 uint32_t num_blocks; 5937 int len, retval; 5938 uint8_t byte2; 5939 5940 retval = CTL_RETVAL_COMPLETE; 5941 5942 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5943 5944 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5945 5946 switch (ctsio->cdb[0]) { 5947 case WRITE_SAME_10: { 5948 struct scsi_write_same_10 *cdb; 5949 5950 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5951 5952 lba = scsi_4btoul(cdb->addr); 5953 num_blocks = scsi_2btoul(cdb->length); 5954 byte2 = cdb->byte2; 5955 break; 5956 } 5957 case WRITE_SAME_16: { 5958 struct scsi_write_same_16 *cdb; 5959 5960 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5961 5962 lba = scsi_8btou64(cdb->addr); 5963 num_blocks = scsi_4btoul(cdb->length); 5964 byte2 = cdb->byte2; 5965 break; 5966 } 5967 default: 5968 /* 5969 * We got a command we don't support. This shouldn't 5970 * happen, commands should be filtered out above us. 5971 */ 5972 ctl_set_invalid_opcode(ctsio); 5973 ctl_done((union ctl_io *)ctsio); 5974 5975 return (CTL_RETVAL_COMPLETE); 5976 break; /* NOTREACHED */ 5977 } 5978 5979 /* 5980 * The first check is to make sure we're in bounds, the second 5981 * check is to catch wrap-around problems. If the lba + num blocks 5982 * is less than the lba, then we've wrapped around and the block 5983 * range is invalid anyway. 5984 */ 5985 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5986 || ((lba + num_blocks) < lba)) { 5987 ctl_set_lba_out_of_range(ctsio); 5988 ctl_done((union ctl_io *)ctsio); 5989 return (CTL_RETVAL_COMPLETE); 5990 } 5991 5992 /* Zero number of blocks means "to the last logical block" */ 5993 if (num_blocks == 0) { 5994 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5995 ctl_set_invalid_field(ctsio, 5996 /*sks_valid*/ 0, 5997 /*command*/ 1, 5998 /*field*/ 0, 5999 /*bit_valid*/ 0, 6000 /*bit*/ 0); 6001 ctl_done((union ctl_io *)ctsio); 6002 return (CTL_RETVAL_COMPLETE); 6003 } 6004 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6005 } 6006 6007 len = lun->be_lun->blocksize; 6008 6009 /* 6010 * If we've got a kernel request that hasn't been malloced yet, 6011 * malloc it and tell the caller the data buffer is here. 6012 */ 6013 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6014 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6015 ctsio->kern_data_len = len; 6016 ctsio->kern_total_len = len; 6017 ctsio->kern_data_resid = 0; 6018 ctsio->kern_rel_offset = 0; 6019 ctsio->kern_sg_entries = 0; 6020 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6021 ctsio->be_move_done = ctl_config_move_done; 6022 ctl_datamove((union ctl_io *)ctsio); 6023 6024 return (CTL_RETVAL_COMPLETE); 6025 } 6026 6027 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6028 lbalen->lba = lba; 6029 lbalen->len = num_blocks; 6030 lbalen->flags = byte2; 6031 retval = lun->backend->config_write((union ctl_io *)ctsio); 6032 6033 return (retval); 6034} 6035 6036int 6037ctl_unmap(struct ctl_scsiio *ctsio) 6038{ 6039 struct ctl_lun *lun; 6040 struct scsi_unmap *cdb; 6041 struct ctl_ptr_len_flags *ptrlen; 6042 struct scsi_unmap_header *hdr; 6043 struct scsi_unmap_desc *buf, *end; 6044 uint64_t lba; 6045 uint32_t num_blocks; 6046 int len, retval; 6047 uint8_t byte2; 6048 6049 retval = CTL_RETVAL_COMPLETE; 6050 6051 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6052 6053 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6054 cdb = (struct scsi_unmap *)ctsio->cdb; 6055 6056 len = scsi_2btoul(cdb->length); 6057 byte2 = cdb->byte2; 6058 6059 /* 6060 * If we've got a kernel request that hasn't been malloced yet, 6061 * malloc it and tell the caller the data buffer is here. 6062 */ 6063 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6064 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6065 ctsio->kern_data_len = len; 6066 ctsio->kern_total_len = len; 6067 ctsio->kern_data_resid = 0; 6068 ctsio->kern_rel_offset = 0; 6069 ctsio->kern_sg_entries = 0; 6070 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6071 ctsio->be_move_done = ctl_config_move_done; 6072 ctl_datamove((union ctl_io *)ctsio); 6073 6074 return (CTL_RETVAL_COMPLETE); 6075 } 6076 6077 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6078 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6079 if (len < sizeof (*hdr) || 6080 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6081 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6082 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6083 ctl_set_invalid_field(ctsio, 6084 /*sks_valid*/ 0, 6085 /*command*/ 0, 6086 /*field*/ 0, 6087 /*bit_valid*/ 0, 6088 /*bit*/ 0); 6089 ctl_done((union ctl_io *)ctsio); 6090 return (CTL_RETVAL_COMPLETE); 6091 } 6092 len = scsi_2btoul(hdr->desc_length); 6093 buf = (struct scsi_unmap_desc *)(hdr + 1); 6094 end = buf + len / sizeof(*buf); 6095 6096 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6097 ptrlen->ptr = (void *)buf; 6098 ptrlen->len = len; 6099 ptrlen->flags = byte2; 6100 6101 for (; buf < end; buf++) { 6102 lba = scsi_8btou64(buf->lba); 6103 num_blocks = scsi_4btoul(buf->length); 6104 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6105 || ((lba + num_blocks) < lba)) { 6106 ctl_set_lba_out_of_range(ctsio); 6107 ctl_done((union ctl_io *)ctsio); 6108 return (CTL_RETVAL_COMPLETE); 6109 } 6110 } 6111 6112 retval = lun->backend->config_write((union ctl_io *)ctsio); 6113 6114 return (retval); 6115} 6116 6117/* 6118 * Note that this function currently doesn't actually do anything inside 6119 * CTL to enforce things if the DQue bit is turned on. 6120 * 6121 * Also note that this function can't be used in the default case, because 6122 * the DQue bit isn't set in the changeable mask for the control mode page 6123 * anyway. This is just here as an example for how to implement a page 6124 * handler, and a placeholder in case we want to allow the user to turn 6125 * tagged queueing on and off. 6126 * 6127 * The D_SENSE bit handling is functional, however, and will turn 6128 * descriptor sense on and off for a given LUN. 6129 */ 6130int 6131ctl_control_page_handler(struct ctl_scsiio *ctsio, 6132 struct ctl_page_index *page_index, uint8_t *page_ptr) 6133{ 6134 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6135 struct ctl_lun *lun; 6136 struct ctl_softc *softc; 6137 int set_ua; 6138 uint32_t initidx; 6139 6140 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6141 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6142 set_ua = 0; 6143 6144 user_cp = (struct scsi_control_page *)page_ptr; 6145 current_cp = (struct scsi_control_page *) 6146 (page_index->page_data + (page_index->page_len * 6147 CTL_PAGE_CURRENT)); 6148 saved_cp = (struct scsi_control_page *) 6149 (page_index->page_data + (page_index->page_len * 6150 CTL_PAGE_SAVED)); 6151 6152 softc = control_softc; 6153 6154 mtx_lock(&lun->lun_lock); 6155 if (((current_cp->rlec & SCP_DSENSE) == 0) 6156 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6157 /* 6158 * Descriptor sense is currently turned off and the user 6159 * wants to turn it on. 6160 */ 6161 current_cp->rlec |= SCP_DSENSE; 6162 saved_cp->rlec |= SCP_DSENSE; 6163 lun->flags |= CTL_LUN_SENSE_DESC; 6164 set_ua = 1; 6165 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6166 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6167 /* 6168 * Descriptor sense is currently turned on, and the user 6169 * wants to turn it off. 6170 */ 6171 current_cp->rlec &= ~SCP_DSENSE; 6172 saved_cp->rlec &= ~SCP_DSENSE; 6173 lun->flags &= ~CTL_LUN_SENSE_DESC; 6174 set_ua = 1; 6175 } 6176 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6177 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6178#ifdef NEEDTOPORT 6179 csevent_log(CSC_CTL | CSC_SHELF_SW | 6180 CTL_UNTAG_TO_UNTAG, 6181 csevent_LogType_Trace, 6182 csevent_Severity_Information, 6183 csevent_AlertLevel_Green, 6184 csevent_FRU_Firmware, 6185 csevent_FRU_Unknown, 6186 "Received untagged to untagged transition"); 6187#endif /* NEEDTOPORT */ 6188 } else { 6189#ifdef NEEDTOPORT 6190 csevent_log(CSC_CTL | CSC_SHELF_SW | 6191 CTL_UNTAG_TO_TAG, 6192 csevent_LogType_ConfigChange, 6193 csevent_Severity_Information, 6194 csevent_AlertLevel_Green, 6195 csevent_FRU_Firmware, 6196 csevent_FRU_Unknown, 6197 "Received untagged to tagged " 6198 "queueing transition"); 6199#endif /* NEEDTOPORT */ 6200 6201 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6202 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6203 set_ua = 1; 6204 } 6205 } else { 6206 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6207#ifdef NEEDTOPORT 6208 csevent_log(CSC_CTL | CSC_SHELF_SW | 6209 CTL_TAG_TO_UNTAG, 6210 csevent_LogType_ConfigChange, 6211 csevent_Severity_Warning, 6212 csevent_AlertLevel_Yellow, 6213 csevent_FRU_Firmware, 6214 csevent_FRU_Unknown, 6215 "Received tagged queueing to untagged " 6216 "transition"); 6217#endif /* NEEDTOPORT */ 6218 6219 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6220 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6221 set_ua = 1; 6222 } else { 6223#ifdef NEEDTOPORT 6224 csevent_log(CSC_CTL | CSC_SHELF_SW | 6225 CTL_TAG_TO_TAG, 6226 csevent_LogType_Trace, 6227 csevent_Severity_Information, 6228 csevent_AlertLevel_Green, 6229 csevent_FRU_Firmware, 6230 csevent_FRU_Unknown, 6231 "Received tagged queueing to tagged " 6232 "queueing transition"); 6233#endif /* NEEDTOPORT */ 6234 } 6235 } 6236 if (set_ua != 0) { 6237 int i; 6238 /* 6239 * Let other initiators know that the mode 6240 * parameters for this LUN have changed. 6241 */ 6242 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6243 if (i == initidx) 6244 continue; 6245 6246 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6247 } 6248 } 6249 mtx_unlock(&lun->lun_lock); 6250 6251 return (0); 6252} 6253 6254int 6255ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6256 struct ctl_page_index *page_index, uint8_t *page_ptr) 6257{ 6258 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6259 struct ctl_lun *lun; 6260 int set_ua; 6261 uint32_t initidx; 6262 6263 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6264 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6265 set_ua = 0; 6266 6267 user_cp = (struct scsi_caching_page *)page_ptr; 6268 current_cp = (struct scsi_caching_page *) 6269 (page_index->page_data + (page_index->page_len * 6270 CTL_PAGE_CURRENT)); 6271 saved_cp = (struct scsi_caching_page *) 6272 (page_index->page_data + (page_index->page_len * 6273 CTL_PAGE_SAVED)); 6274 6275 mtx_lock(&lun->lun_lock); 6276 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6277 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) 6278 set_ua = 1; 6279 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6280 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6281 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6282 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6283 if (set_ua != 0) { 6284 int i; 6285 /* 6286 * Let other initiators know that the mode 6287 * parameters for this LUN have changed. 6288 */ 6289 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6290 if (i == initidx) 6291 continue; 6292 6293 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6294 } 6295 } 6296 mtx_unlock(&lun->lun_lock); 6297 6298 return (0); 6299} 6300 6301int 6302ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6303 struct ctl_page_index *page_index, uint8_t *page_ptr) 6304{ 6305 return (0); 6306} 6307 6308int 6309ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6310 struct ctl_page_index *page_index, int pc) 6311{ 6312 struct copan_power_subpage *page; 6313 6314 page = (struct copan_power_subpage *)page_index->page_data + 6315 (page_index->page_len * pc); 6316 6317 switch (pc) { 6318 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6319 /* 6320 * We don't update the changable bits for this page. 6321 */ 6322 break; 6323 case SMS_PAGE_CTRL_CURRENT >> 6: 6324 case SMS_PAGE_CTRL_DEFAULT >> 6: 6325 case SMS_PAGE_CTRL_SAVED >> 6: 6326#ifdef NEEDTOPORT 6327 ctl_update_power_subpage(page); 6328#endif 6329 break; 6330 default: 6331#ifdef NEEDTOPORT 6332 EPRINT(0, "Invalid PC %d!!", pc); 6333#endif 6334 break; 6335 } 6336 return (0); 6337} 6338 6339 6340int 6341ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6342 struct ctl_page_index *page_index, uint8_t *page_ptr) 6343{ 6344 struct copan_aps_subpage *user_sp; 6345 struct copan_aps_subpage *current_sp; 6346 union ctl_modepage_info *modepage_info; 6347 struct ctl_softc *softc; 6348 struct ctl_lun *lun; 6349 int retval; 6350 6351 retval = CTL_RETVAL_COMPLETE; 6352 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6353 (page_index->page_len * CTL_PAGE_CURRENT)); 6354 softc = control_softc; 6355 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6356 6357 user_sp = (struct copan_aps_subpage *)page_ptr; 6358 6359 modepage_info = (union ctl_modepage_info *) 6360 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6361 6362 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6363 modepage_info->header.subpage = page_index->subpage; 6364 modepage_info->aps.lock_active = user_sp->lock_active; 6365 6366 mtx_lock(&softc->ctl_lock); 6367 6368 /* 6369 * If there is a request to lock the LUN and another LUN is locked 6370 * this is an error. If the requested LUN is already locked ignore 6371 * the request. If no LUN is locked attempt to lock it. 6372 * if there is a request to unlock the LUN and the LUN is currently 6373 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6374 * if another LUN is locked or no LUN is locked. 6375 */ 6376 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6377 if (softc->aps_locked_lun == lun->lun) { 6378 /* 6379 * This LUN is already locked, so we're done. 6380 */ 6381 retval = CTL_RETVAL_COMPLETE; 6382 } else if (softc->aps_locked_lun == 0) { 6383 /* 6384 * No one has the lock, pass the request to the 6385 * backend. 6386 */ 6387 retval = lun->backend->config_write( 6388 (union ctl_io *)ctsio); 6389 } else { 6390 /* 6391 * Someone else has the lock, throw out the request. 6392 */ 6393 ctl_set_already_locked(ctsio); 6394 free(ctsio->kern_data_ptr, M_CTL); 6395 ctl_done((union ctl_io *)ctsio); 6396 6397 /* 6398 * Set the return value so that ctl_do_mode_select() 6399 * won't try to complete the command. We already 6400 * completed it here. 6401 */ 6402 retval = CTL_RETVAL_ERROR; 6403 } 6404 } else if (softc->aps_locked_lun == lun->lun) { 6405 /* 6406 * This LUN is locked, so pass the unlock request to the 6407 * backend. 6408 */ 6409 retval = lun->backend->config_write((union ctl_io *)ctsio); 6410 } 6411 mtx_unlock(&softc->ctl_lock); 6412 6413 return (retval); 6414} 6415 6416int 6417ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6418 struct ctl_page_index *page_index, 6419 uint8_t *page_ptr) 6420{ 6421 uint8_t *c; 6422 int i; 6423 6424 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6425 ctl_time_io_secs = 6426 (c[0] << 8) | 6427 (c[1] << 0) | 6428 0; 6429 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6430 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6431 printf("page data:"); 6432 for (i=0; i<8; i++) 6433 printf(" %.2x",page_ptr[i]); 6434 printf("\n"); 6435 return (0); 6436} 6437 6438int 6439ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6440 struct ctl_page_index *page_index, 6441 int pc) 6442{ 6443 struct copan_debugconf_subpage *page; 6444 6445 page = (struct copan_debugconf_subpage *)page_index->page_data + 6446 (page_index->page_len * pc); 6447 6448 switch (pc) { 6449 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6450 case SMS_PAGE_CTRL_DEFAULT >> 6: 6451 case SMS_PAGE_CTRL_SAVED >> 6: 6452 /* 6453 * We don't update the changable or default bits for this page. 6454 */ 6455 break; 6456 case SMS_PAGE_CTRL_CURRENT >> 6: 6457 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6458 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6459 break; 6460 default: 6461#ifdef NEEDTOPORT 6462 EPRINT(0, "Invalid PC %d!!", pc); 6463#endif /* NEEDTOPORT */ 6464 break; 6465 } 6466 return (0); 6467} 6468 6469 6470static int 6471ctl_do_mode_select(union ctl_io *io) 6472{ 6473 struct scsi_mode_page_header *page_header; 6474 struct ctl_page_index *page_index; 6475 struct ctl_scsiio *ctsio; 6476 int control_dev, page_len; 6477 int page_len_offset, page_len_size; 6478 union ctl_modepage_info *modepage_info; 6479 struct ctl_lun *lun; 6480 int *len_left, *len_used; 6481 int retval, i; 6482 6483 ctsio = &io->scsiio; 6484 page_index = NULL; 6485 page_len = 0; 6486 retval = CTL_RETVAL_COMPLETE; 6487 6488 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6489 6490 if (lun->be_lun->lun_type != T_DIRECT) 6491 control_dev = 1; 6492 else 6493 control_dev = 0; 6494 6495 modepage_info = (union ctl_modepage_info *) 6496 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6497 len_left = &modepage_info->header.len_left; 6498 len_used = &modepage_info->header.len_used; 6499 6500do_next_page: 6501 6502 page_header = (struct scsi_mode_page_header *) 6503 (ctsio->kern_data_ptr + *len_used); 6504 6505 if (*len_left == 0) { 6506 free(ctsio->kern_data_ptr, M_CTL); 6507 ctl_set_success(ctsio); 6508 ctl_done((union ctl_io *)ctsio); 6509 return (CTL_RETVAL_COMPLETE); 6510 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6511 6512 free(ctsio->kern_data_ptr, M_CTL); 6513 ctl_set_param_len_error(ctsio); 6514 ctl_done((union ctl_io *)ctsio); 6515 return (CTL_RETVAL_COMPLETE); 6516 6517 } else if ((page_header->page_code & SMPH_SPF) 6518 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6519 6520 free(ctsio->kern_data_ptr, M_CTL); 6521 ctl_set_param_len_error(ctsio); 6522 ctl_done((union ctl_io *)ctsio); 6523 return (CTL_RETVAL_COMPLETE); 6524 } 6525 6526 6527 /* 6528 * XXX KDM should we do something with the block descriptor? 6529 */ 6530 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6531 6532 if ((control_dev != 0) 6533 && (lun->mode_pages.index[i].page_flags & 6534 CTL_PAGE_FLAG_DISK_ONLY)) 6535 continue; 6536 6537 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6538 (page_header->page_code & SMPH_PC_MASK)) 6539 continue; 6540 6541 /* 6542 * If neither page has a subpage code, then we've got a 6543 * match. 6544 */ 6545 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6546 && ((page_header->page_code & SMPH_SPF) == 0)) { 6547 page_index = &lun->mode_pages.index[i]; 6548 page_len = page_header->page_length; 6549 break; 6550 } 6551 6552 /* 6553 * If both pages have subpages, then the subpage numbers 6554 * have to match. 6555 */ 6556 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6557 && (page_header->page_code & SMPH_SPF)) { 6558 struct scsi_mode_page_header_sp *sph; 6559 6560 sph = (struct scsi_mode_page_header_sp *)page_header; 6561 6562 if (lun->mode_pages.index[i].subpage == 6563 sph->subpage) { 6564 page_index = &lun->mode_pages.index[i]; 6565 page_len = scsi_2btoul(sph->page_length); 6566 break; 6567 } 6568 } 6569 } 6570 6571 /* 6572 * If we couldn't find the page, or if we don't have a mode select 6573 * handler for it, send back an error to the user. 6574 */ 6575 if ((page_index == NULL) 6576 || (page_index->select_handler == NULL)) { 6577 ctl_set_invalid_field(ctsio, 6578 /*sks_valid*/ 1, 6579 /*command*/ 0, 6580 /*field*/ *len_used, 6581 /*bit_valid*/ 0, 6582 /*bit*/ 0); 6583 free(ctsio->kern_data_ptr, M_CTL); 6584 ctl_done((union ctl_io *)ctsio); 6585 return (CTL_RETVAL_COMPLETE); 6586 } 6587 6588 if (page_index->page_code & SMPH_SPF) { 6589 page_len_offset = 2; 6590 page_len_size = 2; 6591 } else { 6592 page_len_size = 1; 6593 page_len_offset = 1; 6594 } 6595 6596 /* 6597 * If the length the initiator gives us isn't the one we specify in 6598 * the mode page header, or if they didn't specify enough data in 6599 * the CDB to avoid truncating this page, kick out the request. 6600 */ 6601 if ((page_len != (page_index->page_len - page_len_offset - 6602 page_len_size)) 6603 || (*len_left < page_index->page_len)) { 6604 6605 6606 ctl_set_invalid_field(ctsio, 6607 /*sks_valid*/ 1, 6608 /*command*/ 0, 6609 /*field*/ *len_used + page_len_offset, 6610 /*bit_valid*/ 0, 6611 /*bit*/ 0); 6612 free(ctsio->kern_data_ptr, M_CTL); 6613 ctl_done((union ctl_io *)ctsio); 6614 return (CTL_RETVAL_COMPLETE); 6615 } 6616 6617 /* 6618 * Run through the mode page, checking to make sure that the bits 6619 * the user changed are actually legal for him to change. 6620 */ 6621 for (i = 0; i < page_index->page_len; i++) { 6622 uint8_t *user_byte, *change_mask, *current_byte; 6623 int bad_bit; 6624 int j; 6625 6626 user_byte = (uint8_t *)page_header + i; 6627 change_mask = page_index->page_data + 6628 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6629 current_byte = page_index->page_data + 6630 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6631 6632 /* 6633 * Check to see whether the user set any bits in this byte 6634 * that he is not allowed to set. 6635 */ 6636 if ((*user_byte & ~(*change_mask)) == 6637 (*current_byte & ~(*change_mask))) 6638 continue; 6639 6640 /* 6641 * Go through bit by bit to determine which one is illegal. 6642 */ 6643 bad_bit = 0; 6644 for (j = 7; j >= 0; j--) { 6645 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6646 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6647 bad_bit = i; 6648 break; 6649 } 6650 } 6651 ctl_set_invalid_field(ctsio, 6652 /*sks_valid*/ 1, 6653 /*command*/ 0, 6654 /*field*/ *len_used + i, 6655 /*bit_valid*/ 1, 6656 /*bit*/ bad_bit); 6657 free(ctsio->kern_data_ptr, M_CTL); 6658 ctl_done((union ctl_io *)ctsio); 6659 return (CTL_RETVAL_COMPLETE); 6660 } 6661 6662 /* 6663 * Decrement these before we call the page handler, since we may 6664 * end up getting called back one way or another before the handler 6665 * returns to this context. 6666 */ 6667 *len_left -= page_index->page_len; 6668 *len_used += page_index->page_len; 6669 6670 retval = page_index->select_handler(ctsio, page_index, 6671 (uint8_t *)page_header); 6672 6673 /* 6674 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6675 * wait until this queued command completes to finish processing 6676 * the mode page. If it returns anything other than 6677 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6678 * already set the sense information, freed the data pointer, and 6679 * completed the io for us. 6680 */ 6681 if (retval != CTL_RETVAL_COMPLETE) 6682 goto bailout_no_done; 6683 6684 /* 6685 * If the initiator sent us more than one page, parse the next one. 6686 */ 6687 if (*len_left > 0) 6688 goto do_next_page; 6689 6690 ctl_set_success(ctsio); 6691 free(ctsio->kern_data_ptr, M_CTL); 6692 ctl_done((union ctl_io *)ctsio); 6693 6694bailout_no_done: 6695 6696 return (CTL_RETVAL_COMPLETE); 6697 6698} 6699 6700int 6701ctl_mode_select(struct ctl_scsiio *ctsio) 6702{ 6703 int param_len, pf, sp; 6704 int header_size, bd_len; 6705 int len_left, len_used; 6706 struct ctl_page_index *page_index; 6707 struct ctl_lun *lun; 6708 int control_dev, page_len; 6709 union ctl_modepage_info *modepage_info; 6710 int retval; 6711 6712 pf = 0; 6713 sp = 0; 6714 page_len = 0; 6715 len_used = 0; 6716 len_left = 0; 6717 retval = 0; 6718 bd_len = 0; 6719 page_index = NULL; 6720 6721 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6722 6723 if (lun->be_lun->lun_type != T_DIRECT) 6724 control_dev = 1; 6725 else 6726 control_dev = 0; 6727 6728 switch (ctsio->cdb[0]) { 6729 case MODE_SELECT_6: { 6730 struct scsi_mode_select_6 *cdb; 6731 6732 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6733 6734 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6735 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6736 6737 param_len = cdb->length; 6738 header_size = sizeof(struct scsi_mode_header_6); 6739 break; 6740 } 6741 case MODE_SELECT_10: { 6742 struct scsi_mode_select_10 *cdb; 6743 6744 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6745 6746 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6747 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6748 6749 param_len = scsi_2btoul(cdb->length); 6750 header_size = sizeof(struct scsi_mode_header_10); 6751 break; 6752 } 6753 default: 6754 ctl_set_invalid_opcode(ctsio); 6755 ctl_done((union ctl_io *)ctsio); 6756 return (CTL_RETVAL_COMPLETE); 6757 break; /* NOTREACHED */ 6758 } 6759 6760 /* 6761 * From SPC-3: 6762 * "A parameter list length of zero indicates that the Data-Out Buffer 6763 * shall be empty. This condition shall not be considered as an error." 6764 */ 6765 if (param_len == 0) { 6766 ctl_set_success(ctsio); 6767 ctl_done((union ctl_io *)ctsio); 6768 return (CTL_RETVAL_COMPLETE); 6769 } 6770 6771 /* 6772 * Since we'll hit this the first time through, prior to 6773 * allocation, we don't need to free a data buffer here. 6774 */ 6775 if (param_len < header_size) { 6776 ctl_set_param_len_error(ctsio); 6777 ctl_done((union ctl_io *)ctsio); 6778 return (CTL_RETVAL_COMPLETE); 6779 } 6780 6781 /* 6782 * Allocate the data buffer and grab the user's data. In theory, 6783 * we shouldn't have to sanity check the parameter list length here 6784 * because the maximum size is 64K. We should be able to malloc 6785 * that much without too many problems. 6786 */ 6787 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6788 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6789 ctsio->kern_data_len = param_len; 6790 ctsio->kern_total_len = param_len; 6791 ctsio->kern_data_resid = 0; 6792 ctsio->kern_rel_offset = 0; 6793 ctsio->kern_sg_entries = 0; 6794 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6795 ctsio->be_move_done = ctl_config_move_done; 6796 ctl_datamove((union ctl_io *)ctsio); 6797 6798 return (CTL_RETVAL_COMPLETE); 6799 } 6800 6801 switch (ctsio->cdb[0]) { 6802 case MODE_SELECT_6: { 6803 struct scsi_mode_header_6 *mh6; 6804 6805 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6806 bd_len = mh6->blk_desc_len; 6807 break; 6808 } 6809 case MODE_SELECT_10: { 6810 struct scsi_mode_header_10 *mh10; 6811 6812 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6813 bd_len = scsi_2btoul(mh10->blk_desc_len); 6814 break; 6815 } 6816 default: 6817 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6818 break; 6819 } 6820 6821 if (param_len < (header_size + bd_len)) { 6822 free(ctsio->kern_data_ptr, M_CTL); 6823 ctl_set_param_len_error(ctsio); 6824 ctl_done((union ctl_io *)ctsio); 6825 return (CTL_RETVAL_COMPLETE); 6826 } 6827 6828 /* 6829 * Set the IO_CONT flag, so that if this I/O gets passed to 6830 * ctl_config_write_done(), it'll get passed back to 6831 * ctl_do_mode_select() for further processing, or completion if 6832 * we're all done. 6833 */ 6834 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6835 ctsio->io_cont = ctl_do_mode_select; 6836 6837 modepage_info = (union ctl_modepage_info *) 6838 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6839 6840 memset(modepage_info, 0, sizeof(*modepage_info)); 6841 6842 len_left = param_len - header_size - bd_len; 6843 len_used = header_size + bd_len; 6844 6845 modepage_info->header.len_left = len_left; 6846 modepage_info->header.len_used = len_used; 6847 6848 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6849} 6850 6851int 6852ctl_mode_sense(struct ctl_scsiio *ctsio) 6853{ 6854 struct ctl_lun *lun; 6855 int pc, page_code, dbd, llba, subpage; 6856 int alloc_len, page_len, header_len, total_len; 6857 struct scsi_mode_block_descr *block_desc; 6858 struct ctl_page_index *page_index; 6859 int control_dev; 6860 6861 dbd = 0; 6862 llba = 0; 6863 block_desc = NULL; 6864 page_index = NULL; 6865 6866 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6867 6868 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6869 6870 if (lun->be_lun->lun_type != T_DIRECT) 6871 control_dev = 1; 6872 else 6873 control_dev = 0; 6874 6875 if (lun->flags & CTL_LUN_PR_RESERVED) { 6876 uint32_t residx; 6877 6878 /* 6879 * XXX KDM need a lock here. 6880 */ 6881 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6882 if ((lun->res_type == SPR_TYPE_EX_AC 6883 && residx != lun->pr_res_idx) 6884 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6885 || lun->res_type == SPR_TYPE_EX_AC_AR) 6886 && !lun->per_res[residx].registered)) { 6887 ctl_set_reservation_conflict(ctsio); 6888 ctl_done((union ctl_io *)ctsio); 6889 return (CTL_RETVAL_COMPLETE); 6890 } 6891 } 6892 6893 switch (ctsio->cdb[0]) { 6894 case MODE_SENSE_6: { 6895 struct scsi_mode_sense_6 *cdb; 6896 6897 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6898 6899 header_len = sizeof(struct scsi_mode_hdr_6); 6900 if (cdb->byte2 & SMS_DBD) 6901 dbd = 1; 6902 else 6903 header_len += sizeof(struct scsi_mode_block_descr); 6904 6905 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6906 page_code = cdb->page & SMS_PAGE_CODE; 6907 subpage = cdb->subpage; 6908 alloc_len = cdb->length; 6909 break; 6910 } 6911 case MODE_SENSE_10: { 6912 struct scsi_mode_sense_10 *cdb; 6913 6914 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6915 6916 header_len = sizeof(struct scsi_mode_hdr_10); 6917 6918 if (cdb->byte2 & SMS_DBD) 6919 dbd = 1; 6920 else 6921 header_len += sizeof(struct scsi_mode_block_descr); 6922 if (cdb->byte2 & SMS10_LLBAA) 6923 llba = 1; 6924 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6925 page_code = cdb->page & SMS_PAGE_CODE; 6926 subpage = cdb->subpage; 6927 alloc_len = scsi_2btoul(cdb->length); 6928 break; 6929 } 6930 default: 6931 ctl_set_invalid_opcode(ctsio); 6932 ctl_done((union ctl_io *)ctsio); 6933 return (CTL_RETVAL_COMPLETE); 6934 break; /* NOTREACHED */ 6935 } 6936 6937 /* 6938 * We have to make a first pass through to calculate the size of 6939 * the pages that match the user's query. Then we allocate enough 6940 * memory to hold it, and actually copy the data into the buffer. 6941 */ 6942 switch (page_code) { 6943 case SMS_ALL_PAGES_PAGE: { 6944 int i; 6945 6946 page_len = 0; 6947 6948 /* 6949 * At the moment, values other than 0 and 0xff here are 6950 * reserved according to SPC-3. 6951 */ 6952 if ((subpage != SMS_SUBPAGE_PAGE_0) 6953 && (subpage != SMS_SUBPAGE_ALL)) { 6954 ctl_set_invalid_field(ctsio, 6955 /*sks_valid*/ 1, 6956 /*command*/ 1, 6957 /*field*/ 3, 6958 /*bit_valid*/ 0, 6959 /*bit*/ 0); 6960 ctl_done((union ctl_io *)ctsio); 6961 return (CTL_RETVAL_COMPLETE); 6962 } 6963 6964 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6965 if ((control_dev != 0) 6966 && (lun->mode_pages.index[i].page_flags & 6967 CTL_PAGE_FLAG_DISK_ONLY)) 6968 continue; 6969 6970 /* 6971 * We don't use this subpage if the user didn't 6972 * request all subpages. 6973 */ 6974 if ((lun->mode_pages.index[i].subpage != 0) 6975 && (subpage == SMS_SUBPAGE_PAGE_0)) 6976 continue; 6977 6978#if 0 6979 printf("found page %#x len %d\n", 6980 lun->mode_pages.index[i].page_code & 6981 SMPH_PC_MASK, 6982 lun->mode_pages.index[i].page_len); 6983#endif 6984 page_len += lun->mode_pages.index[i].page_len; 6985 } 6986 break; 6987 } 6988 default: { 6989 int i; 6990 6991 page_len = 0; 6992 6993 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6994 /* Look for the right page code */ 6995 if ((lun->mode_pages.index[i].page_code & 6996 SMPH_PC_MASK) != page_code) 6997 continue; 6998 6999 /* Look for the right subpage or the subpage wildcard*/ 7000 if ((lun->mode_pages.index[i].subpage != subpage) 7001 && (subpage != SMS_SUBPAGE_ALL)) 7002 continue; 7003 7004 /* Make sure the page is supported for this dev type */ 7005 if ((control_dev != 0) 7006 && (lun->mode_pages.index[i].page_flags & 7007 CTL_PAGE_FLAG_DISK_ONLY)) 7008 continue; 7009 7010#if 0 7011 printf("found page %#x len %d\n", 7012 lun->mode_pages.index[i].page_code & 7013 SMPH_PC_MASK, 7014 lun->mode_pages.index[i].page_len); 7015#endif 7016 7017 page_len += lun->mode_pages.index[i].page_len; 7018 } 7019 7020 if (page_len == 0) { 7021 ctl_set_invalid_field(ctsio, 7022 /*sks_valid*/ 1, 7023 /*command*/ 1, 7024 /*field*/ 2, 7025 /*bit_valid*/ 1, 7026 /*bit*/ 5); 7027 ctl_done((union ctl_io *)ctsio); 7028 return (CTL_RETVAL_COMPLETE); 7029 } 7030 break; 7031 } 7032 } 7033 7034 total_len = header_len + page_len; 7035#if 0 7036 printf("header_len = %d, page_len = %d, total_len = %d\n", 7037 header_len, page_len, total_len); 7038#endif 7039 7040 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7041 ctsio->kern_sg_entries = 0; 7042 ctsio->kern_data_resid = 0; 7043 ctsio->kern_rel_offset = 0; 7044 if (total_len < alloc_len) { 7045 ctsio->residual = alloc_len - total_len; 7046 ctsio->kern_data_len = total_len; 7047 ctsio->kern_total_len = total_len; 7048 } else { 7049 ctsio->residual = 0; 7050 ctsio->kern_data_len = alloc_len; 7051 ctsio->kern_total_len = alloc_len; 7052 } 7053 7054 switch (ctsio->cdb[0]) { 7055 case MODE_SENSE_6: { 7056 struct scsi_mode_hdr_6 *header; 7057 7058 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7059 7060 header->datalen = ctl_min(total_len - 1, 254); 7061 if (control_dev == 0) 7062 header->dev_specific = 0x10; /* DPOFUA */ 7063 if (dbd) 7064 header->block_descr_len = 0; 7065 else 7066 header->block_descr_len = 7067 sizeof(struct scsi_mode_block_descr); 7068 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7069 break; 7070 } 7071 case MODE_SENSE_10: { 7072 struct scsi_mode_hdr_10 *header; 7073 int datalen; 7074 7075 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7076 7077 datalen = ctl_min(total_len - 2, 65533); 7078 scsi_ulto2b(datalen, header->datalen); 7079 if (control_dev == 0) 7080 header->dev_specific = 0x10; /* DPOFUA */ 7081 if (dbd) 7082 scsi_ulto2b(0, header->block_descr_len); 7083 else 7084 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7085 header->block_descr_len); 7086 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7087 break; 7088 } 7089 default: 7090 panic("invalid CDB type %#x", ctsio->cdb[0]); 7091 break; /* NOTREACHED */ 7092 } 7093 7094 /* 7095 * If we've got a disk, use its blocksize in the block 7096 * descriptor. Otherwise, just set it to 0. 7097 */ 7098 if (dbd == 0) { 7099 if (control_dev != 0) 7100 scsi_ulto3b(lun->be_lun->blocksize, 7101 block_desc->block_len); 7102 else 7103 scsi_ulto3b(0, block_desc->block_len); 7104 } 7105 7106 switch (page_code) { 7107 case SMS_ALL_PAGES_PAGE: { 7108 int i, data_used; 7109 7110 data_used = header_len; 7111 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7112 struct ctl_page_index *page_index; 7113 7114 page_index = &lun->mode_pages.index[i]; 7115 7116 if ((control_dev != 0) 7117 && (page_index->page_flags & 7118 CTL_PAGE_FLAG_DISK_ONLY)) 7119 continue; 7120 7121 /* 7122 * We don't use this subpage if the user didn't 7123 * request all subpages. We already checked (above) 7124 * to make sure the user only specified a subpage 7125 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7126 */ 7127 if ((page_index->subpage != 0) 7128 && (subpage == SMS_SUBPAGE_PAGE_0)) 7129 continue; 7130 7131 /* 7132 * Call the handler, if it exists, to update the 7133 * page to the latest values. 7134 */ 7135 if (page_index->sense_handler != NULL) 7136 page_index->sense_handler(ctsio, page_index,pc); 7137 7138 memcpy(ctsio->kern_data_ptr + data_used, 7139 page_index->page_data + 7140 (page_index->page_len * pc), 7141 page_index->page_len); 7142 data_used += page_index->page_len; 7143 } 7144 break; 7145 } 7146 default: { 7147 int i, data_used; 7148 7149 data_used = header_len; 7150 7151 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7152 struct ctl_page_index *page_index; 7153 7154 page_index = &lun->mode_pages.index[i]; 7155 7156 /* Look for the right page code */ 7157 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7158 continue; 7159 7160 /* Look for the right subpage or the subpage wildcard*/ 7161 if ((page_index->subpage != subpage) 7162 && (subpage != SMS_SUBPAGE_ALL)) 7163 continue; 7164 7165 /* Make sure the page is supported for this dev type */ 7166 if ((control_dev != 0) 7167 && (page_index->page_flags & 7168 CTL_PAGE_FLAG_DISK_ONLY)) 7169 continue; 7170 7171 /* 7172 * Call the handler, if it exists, to update the 7173 * page to the latest values. 7174 */ 7175 if (page_index->sense_handler != NULL) 7176 page_index->sense_handler(ctsio, page_index,pc); 7177 7178 memcpy(ctsio->kern_data_ptr + data_used, 7179 page_index->page_data + 7180 (page_index->page_len * pc), 7181 page_index->page_len); 7182 data_used += page_index->page_len; 7183 } 7184 break; 7185 } 7186 } 7187 7188 ctsio->scsi_status = SCSI_STATUS_OK; 7189 7190 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7191 ctsio->be_move_done = ctl_config_move_done; 7192 ctl_datamove((union ctl_io *)ctsio); 7193 7194 return (CTL_RETVAL_COMPLETE); 7195} 7196 7197int 7198ctl_read_capacity(struct ctl_scsiio *ctsio) 7199{ 7200 struct scsi_read_capacity *cdb; 7201 struct scsi_read_capacity_data *data; 7202 struct ctl_lun *lun; 7203 uint32_t lba; 7204 7205 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7206 7207 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7208 7209 lba = scsi_4btoul(cdb->addr); 7210 if (((cdb->pmi & SRC_PMI) == 0) 7211 && (lba != 0)) { 7212 ctl_set_invalid_field(/*ctsio*/ ctsio, 7213 /*sks_valid*/ 1, 7214 /*command*/ 1, 7215 /*field*/ 2, 7216 /*bit_valid*/ 0, 7217 /*bit*/ 0); 7218 ctl_done((union ctl_io *)ctsio); 7219 return (CTL_RETVAL_COMPLETE); 7220 } 7221 7222 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7223 7224 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7225 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7226 ctsio->residual = 0; 7227 ctsio->kern_data_len = sizeof(*data); 7228 ctsio->kern_total_len = sizeof(*data); 7229 ctsio->kern_data_resid = 0; 7230 ctsio->kern_rel_offset = 0; 7231 ctsio->kern_sg_entries = 0; 7232 7233 /* 7234 * If the maximum LBA is greater than 0xfffffffe, the user must 7235 * issue a SERVICE ACTION IN (16) command, with the read capacity 7236 * serivce action set. 7237 */ 7238 if (lun->be_lun->maxlba > 0xfffffffe) 7239 scsi_ulto4b(0xffffffff, data->addr); 7240 else 7241 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7242 7243 /* 7244 * XXX KDM this may not be 512 bytes... 7245 */ 7246 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7247 7248 ctsio->scsi_status = SCSI_STATUS_OK; 7249 7250 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7251 ctsio->be_move_done = ctl_config_move_done; 7252 ctl_datamove((union ctl_io *)ctsio); 7253 7254 return (CTL_RETVAL_COMPLETE); 7255} 7256 7257int 7258ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7259{ 7260 struct scsi_read_capacity_16 *cdb; 7261 struct scsi_read_capacity_data_long *data; 7262 struct ctl_lun *lun; 7263 uint64_t lba; 7264 uint32_t alloc_len; 7265 7266 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7267 7268 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7269 7270 alloc_len = scsi_4btoul(cdb->alloc_len); 7271 lba = scsi_8btou64(cdb->addr); 7272 7273 if ((cdb->reladr & SRC16_PMI) 7274 && (lba != 0)) { 7275 ctl_set_invalid_field(/*ctsio*/ ctsio, 7276 /*sks_valid*/ 1, 7277 /*command*/ 1, 7278 /*field*/ 2, 7279 /*bit_valid*/ 0, 7280 /*bit*/ 0); 7281 ctl_done((union ctl_io *)ctsio); 7282 return (CTL_RETVAL_COMPLETE); 7283 } 7284 7285 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7286 7287 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7288 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7289 7290 if (sizeof(*data) < alloc_len) { 7291 ctsio->residual = alloc_len - sizeof(*data); 7292 ctsio->kern_data_len = sizeof(*data); 7293 ctsio->kern_total_len = sizeof(*data); 7294 } else { 7295 ctsio->residual = 0; 7296 ctsio->kern_data_len = alloc_len; 7297 ctsio->kern_total_len = alloc_len; 7298 } 7299 ctsio->kern_data_resid = 0; 7300 ctsio->kern_rel_offset = 0; 7301 ctsio->kern_sg_entries = 0; 7302 7303 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7304 /* XXX KDM this may not be 512 bytes... */ 7305 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7306 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7307 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7308 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7309 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7310 7311 ctsio->scsi_status = SCSI_STATUS_OK; 7312 7313 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7314 ctsio->be_move_done = ctl_config_move_done; 7315 ctl_datamove((union ctl_io *)ctsio); 7316 7317 return (CTL_RETVAL_COMPLETE); 7318} 7319 7320int 7321ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7322{ 7323 struct scsi_maintenance_in *cdb; 7324 int retval; 7325 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7326 int num_target_port_groups, num_target_ports, single; 7327 struct ctl_lun *lun; 7328 struct ctl_softc *softc; 7329 struct ctl_port *port; 7330 struct scsi_target_group_data *rtg_ptr; 7331 struct scsi_target_group_data_extended *rtg_ext_ptr; 7332 struct scsi_target_port_group_descriptor *tpg_desc; 7333 7334 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7335 7336 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7337 softc = control_softc; 7338 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7339 7340 retval = CTL_RETVAL_COMPLETE; 7341 7342 switch (cdb->byte2 & STG_PDF_MASK) { 7343 case STG_PDF_LENGTH: 7344 ext = 0; 7345 break; 7346 case STG_PDF_EXTENDED: 7347 ext = 1; 7348 break; 7349 default: 7350 ctl_set_invalid_field(/*ctsio*/ ctsio, 7351 /*sks_valid*/ 1, 7352 /*command*/ 1, 7353 /*field*/ 2, 7354 /*bit_valid*/ 1, 7355 /*bit*/ 5); 7356 ctl_done((union ctl_io *)ctsio); 7357 return(retval); 7358 } 7359 7360 single = ctl_is_single; 7361 if (single) 7362 num_target_port_groups = 1; 7363 else 7364 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7365 num_target_ports = 0; 7366 mtx_lock(&softc->ctl_lock); 7367 STAILQ_FOREACH(port, &softc->port_list, links) { 7368 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7369 continue; 7370 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7371 continue; 7372 num_target_ports++; 7373 } 7374 mtx_unlock(&softc->ctl_lock); 7375 7376 if (ext) 7377 total_len = sizeof(struct scsi_target_group_data_extended); 7378 else 7379 total_len = sizeof(struct scsi_target_group_data); 7380 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7381 num_target_port_groups + 7382 sizeof(struct scsi_target_port_descriptor) * 7383 num_target_ports * num_target_port_groups; 7384 7385 alloc_len = scsi_4btoul(cdb->length); 7386 7387 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7388 7389 ctsio->kern_sg_entries = 0; 7390 7391 if (total_len < alloc_len) { 7392 ctsio->residual = alloc_len - total_len; 7393 ctsio->kern_data_len = total_len; 7394 ctsio->kern_total_len = total_len; 7395 } else { 7396 ctsio->residual = 0; 7397 ctsio->kern_data_len = alloc_len; 7398 ctsio->kern_total_len = alloc_len; 7399 } 7400 ctsio->kern_data_resid = 0; 7401 ctsio->kern_rel_offset = 0; 7402 7403 if (ext) { 7404 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7405 ctsio->kern_data_ptr; 7406 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7407 rtg_ext_ptr->format_type = 0x10; 7408 rtg_ext_ptr->implicit_transition_time = 0; 7409 tpg_desc = &rtg_ext_ptr->groups[0]; 7410 } else { 7411 rtg_ptr = (struct scsi_target_group_data *) 7412 ctsio->kern_data_ptr; 7413 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7414 tpg_desc = &rtg_ptr->groups[0]; 7415 } 7416 7417 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7418 mtx_lock(&softc->ctl_lock); 7419 for (g = 0; g < num_target_port_groups; g++) { 7420 if (g == pg) 7421 tpg_desc->pref_state = TPG_PRIMARY | 7422 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7423 else 7424 tpg_desc->pref_state = 7425 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7426 tpg_desc->support = TPG_AO_SUP; 7427 if (!single) 7428 tpg_desc->support |= TPG_AN_SUP; 7429 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7430 tpg_desc->status = TPG_IMPLICIT; 7431 pc = 0; 7432 STAILQ_FOREACH(port, &softc->port_list, links) { 7433 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7434 continue; 7435 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7436 CTL_MAX_LUNS) 7437 continue; 7438 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7439 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7440 relative_target_port_identifier); 7441 pc++; 7442 } 7443 tpg_desc->target_port_count = pc; 7444 tpg_desc = (struct scsi_target_port_group_descriptor *) 7445 &tpg_desc->descriptors[pc]; 7446 } 7447 mtx_unlock(&softc->ctl_lock); 7448 7449 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7450 ctsio->be_move_done = ctl_config_move_done; 7451 7452 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7453 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7454 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7455 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7456 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7457 7458 ctl_datamove((union ctl_io *)ctsio); 7459 return(retval); 7460} 7461 7462int 7463ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7464{ 7465 struct ctl_lun *lun; 7466 struct scsi_report_supported_opcodes *cdb; 7467 const struct ctl_cmd_entry *entry, *sentry; 7468 struct scsi_report_supported_opcodes_all *all; 7469 struct scsi_report_supported_opcodes_descr *descr; 7470 struct scsi_report_supported_opcodes_one *one; 7471 int retval; 7472 int alloc_len, total_len; 7473 int opcode, service_action, i, j, num; 7474 7475 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7476 7477 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7478 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7479 7480 retval = CTL_RETVAL_COMPLETE; 7481 7482 opcode = cdb->requested_opcode; 7483 service_action = scsi_2btoul(cdb->requested_service_action); 7484 switch (cdb->options & RSO_OPTIONS_MASK) { 7485 case RSO_OPTIONS_ALL: 7486 num = 0; 7487 for (i = 0; i < 256; i++) { 7488 entry = &ctl_cmd_table[i]; 7489 if (entry->flags & CTL_CMD_FLAG_SA5) { 7490 for (j = 0; j < 32; j++) { 7491 sentry = &((const struct ctl_cmd_entry *) 7492 entry->execute)[j]; 7493 if (ctl_cmd_applicable( 7494 lun->be_lun->lun_type, sentry)) 7495 num++; 7496 } 7497 } else { 7498 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7499 entry)) 7500 num++; 7501 } 7502 } 7503 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7504 num * sizeof(struct scsi_report_supported_opcodes_descr); 7505 break; 7506 case RSO_OPTIONS_OC: 7507 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7508 ctl_set_invalid_field(/*ctsio*/ ctsio, 7509 /*sks_valid*/ 1, 7510 /*command*/ 1, 7511 /*field*/ 2, 7512 /*bit_valid*/ 1, 7513 /*bit*/ 2); 7514 ctl_done((union ctl_io *)ctsio); 7515 return (CTL_RETVAL_COMPLETE); 7516 } 7517 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7518 break; 7519 case RSO_OPTIONS_OC_SA: 7520 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7521 service_action >= 32) { 7522 ctl_set_invalid_field(/*ctsio*/ ctsio, 7523 /*sks_valid*/ 1, 7524 /*command*/ 1, 7525 /*field*/ 2, 7526 /*bit_valid*/ 1, 7527 /*bit*/ 2); 7528 ctl_done((union ctl_io *)ctsio); 7529 return (CTL_RETVAL_COMPLETE); 7530 } 7531 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7532 break; 7533 default: 7534 ctl_set_invalid_field(/*ctsio*/ ctsio, 7535 /*sks_valid*/ 1, 7536 /*command*/ 1, 7537 /*field*/ 2, 7538 /*bit_valid*/ 1, 7539 /*bit*/ 2); 7540 ctl_done((union ctl_io *)ctsio); 7541 return (CTL_RETVAL_COMPLETE); 7542 } 7543 7544 alloc_len = scsi_4btoul(cdb->length); 7545 7546 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7547 7548 ctsio->kern_sg_entries = 0; 7549 7550 if (total_len < alloc_len) { 7551 ctsio->residual = alloc_len - total_len; 7552 ctsio->kern_data_len = total_len; 7553 ctsio->kern_total_len = total_len; 7554 } else { 7555 ctsio->residual = 0; 7556 ctsio->kern_data_len = alloc_len; 7557 ctsio->kern_total_len = alloc_len; 7558 } 7559 ctsio->kern_data_resid = 0; 7560 ctsio->kern_rel_offset = 0; 7561 7562 switch (cdb->options & RSO_OPTIONS_MASK) { 7563 case RSO_OPTIONS_ALL: 7564 all = (struct scsi_report_supported_opcodes_all *) 7565 ctsio->kern_data_ptr; 7566 num = 0; 7567 for (i = 0; i < 256; i++) { 7568 entry = &ctl_cmd_table[i]; 7569 if (entry->flags & CTL_CMD_FLAG_SA5) { 7570 for (j = 0; j < 32; j++) { 7571 sentry = &((const struct ctl_cmd_entry *) 7572 entry->execute)[j]; 7573 if (!ctl_cmd_applicable( 7574 lun->be_lun->lun_type, sentry)) 7575 continue; 7576 descr = &all->descr[num++]; 7577 descr->opcode = i; 7578 scsi_ulto2b(j, descr->service_action); 7579 descr->flags = RSO_SERVACTV; 7580 scsi_ulto2b(sentry->length, 7581 descr->cdb_length); 7582 } 7583 } else { 7584 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7585 entry)) 7586 continue; 7587 descr = &all->descr[num++]; 7588 descr->opcode = i; 7589 scsi_ulto2b(0, descr->service_action); 7590 descr->flags = 0; 7591 scsi_ulto2b(entry->length, descr->cdb_length); 7592 } 7593 } 7594 scsi_ulto4b( 7595 num * sizeof(struct scsi_report_supported_opcodes_descr), 7596 all->length); 7597 break; 7598 case RSO_OPTIONS_OC: 7599 one = (struct scsi_report_supported_opcodes_one *) 7600 ctsio->kern_data_ptr; 7601 entry = &ctl_cmd_table[opcode]; 7602 goto fill_one; 7603 case RSO_OPTIONS_OC_SA: 7604 one = (struct scsi_report_supported_opcodes_one *) 7605 ctsio->kern_data_ptr; 7606 entry = &ctl_cmd_table[opcode]; 7607 entry = &((const struct ctl_cmd_entry *) 7608 entry->execute)[service_action]; 7609fill_one: 7610 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7611 one->support = 3; 7612 scsi_ulto2b(entry->length, one->cdb_length); 7613 one->cdb_usage[0] = opcode; 7614 memcpy(&one->cdb_usage[1], entry->usage, 7615 entry->length - 1); 7616 } else 7617 one->support = 1; 7618 break; 7619 } 7620 7621 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7622 ctsio->be_move_done = ctl_config_move_done; 7623 7624 ctl_datamove((union ctl_io *)ctsio); 7625 return(retval); 7626} 7627 7628int 7629ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7630{ 7631 struct ctl_lun *lun; 7632 struct scsi_report_supported_tmf *cdb; 7633 struct scsi_report_supported_tmf_data *data; 7634 int retval; 7635 int alloc_len, total_len; 7636 7637 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7638 7639 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7640 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7641 7642 retval = CTL_RETVAL_COMPLETE; 7643 7644 total_len = sizeof(struct scsi_report_supported_tmf_data); 7645 alloc_len = scsi_4btoul(cdb->length); 7646 7647 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7648 7649 ctsio->kern_sg_entries = 0; 7650 7651 if (total_len < alloc_len) { 7652 ctsio->residual = alloc_len - total_len; 7653 ctsio->kern_data_len = total_len; 7654 ctsio->kern_total_len = total_len; 7655 } else { 7656 ctsio->residual = 0; 7657 ctsio->kern_data_len = alloc_len; 7658 ctsio->kern_total_len = alloc_len; 7659 } 7660 ctsio->kern_data_resid = 0; 7661 ctsio->kern_rel_offset = 0; 7662 7663 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7664 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7665 data->byte2 |= RST_ITNRS; 7666 7667 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7668 ctsio->be_move_done = ctl_config_move_done; 7669 7670 ctl_datamove((union ctl_io *)ctsio); 7671 return (retval); 7672} 7673 7674int 7675ctl_report_timestamp(struct ctl_scsiio *ctsio) 7676{ 7677 struct ctl_lun *lun; 7678 struct scsi_report_timestamp *cdb; 7679 struct scsi_report_timestamp_data *data; 7680 struct timeval tv; 7681 int64_t timestamp; 7682 int retval; 7683 int alloc_len, total_len; 7684 7685 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7686 7687 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7688 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7689 7690 retval = CTL_RETVAL_COMPLETE; 7691 7692 total_len = sizeof(struct scsi_report_timestamp_data); 7693 alloc_len = scsi_4btoul(cdb->length); 7694 7695 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7696 7697 ctsio->kern_sg_entries = 0; 7698 7699 if (total_len < alloc_len) { 7700 ctsio->residual = alloc_len - total_len; 7701 ctsio->kern_data_len = total_len; 7702 ctsio->kern_total_len = total_len; 7703 } else { 7704 ctsio->residual = 0; 7705 ctsio->kern_data_len = alloc_len; 7706 ctsio->kern_total_len = alloc_len; 7707 } 7708 ctsio->kern_data_resid = 0; 7709 ctsio->kern_rel_offset = 0; 7710 7711 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7712 scsi_ulto2b(sizeof(*data) - 2, data->length); 7713 data->origin = RTS_ORIG_OUTSIDE; 7714 getmicrotime(&tv); 7715 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7716 scsi_ulto4b(timestamp >> 16, data->timestamp); 7717 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7718 7719 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7720 ctsio->be_move_done = ctl_config_move_done; 7721 7722 ctl_datamove((union ctl_io *)ctsio); 7723 return (retval); 7724} 7725 7726int 7727ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7728{ 7729 struct scsi_per_res_in *cdb; 7730 int alloc_len, total_len = 0; 7731 /* struct scsi_per_res_in_rsrv in_data; */ 7732 struct ctl_lun *lun; 7733 struct ctl_softc *softc; 7734 7735 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7736 7737 softc = control_softc; 7738 7739 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7740 7741 alloc_len = scsi_2btoul(cdb->length); 7742 7743 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7744 7745retry: 7746 mtx_lock(&lun->lun_lock); 7747 switch (cdb->action) { 7748 case SPRI_RK: /* read keys */ 7749 total_len = sizeof(struct scsi_per_res_in_keys) + 7750 lun->pr_key_count * 7751 sizeof(struct scsi_per_res_key); 7752 break; 7753 case SPRI_RR: /* read reservation */ 7754 if (lun->flags & CTL_LUN_PR_RESERVED) 7755 total_len = sizeof(struct scsi_per_res_in_rsrv); 7756 else 7757 total_len = sizeof(struct scsi_per_res_in_header); 7758 break; 7759 case SPRI_RC: /* report capabilities */ 7760 total_len = sizeof(struct scsi_per_res_cap); 7761 break; 7762 case SPRI_RS: /* read full status */ 7763 total_len = sizeof(struct scsi_per_res_in_header) + 7764 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7765 lun->pr_key_count; 7766 break; 7767 default: 7768 panic("Invalid PR type %x", cdb->action); 7769 } 7770 mtx_unlock(&lun->lun_lock); 7771 7772 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7773 7774 if (total_len < alloc_len) { 7775 ctsio->residual = alloc_len - total_len; 7776 ctsio->kern_data_len = total_len; 7777 ctsio->kern_total_len = total_len; 7778 } else { 7779 ctsio->residual = 0; 7780 ctsio->kern_data_len = alloc_len; 7781 ctsio->kern_total_len = alloc_len; 7782 } 7783 7784 ctsio->kern_data_resid = 0; 7785 ctsio->kern_rel_offset = 0; 7786 ctsio->kern_sg_entries = 0; 7787 7788 mtx_lock(&lun->lun_lock); 7789 switch (cdb->action) { 7790 case SPRI_RK: { // read keys 7791 struct scsi_per_res_in_keys *res_keys; 7792 int i, key_count; 7793 7794 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7795 7796 /* 7797 * We had to drop the lock to allocate our buffer, which 7798 * leaves time for someone to come in with another 7799 * persistent reservation. (That is unlikely, though, 7800 * since this should be the only persistent reservation 7801 * command active right now.) 7802 */ 7803 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7804 (lun->pr_key_count * 7805 sizeof(struct scsi_per_res_key)))){ 7806 mtx_unlock(&lun->lun_lock); 7807 free(ctsio->kern_data_ptr, M_CTL); 7808 printf("%s: reservation length changed, retrying\n", 7809 __func__); 7810 goto retry; 7811 } 7812 7813 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7814 7815 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7816 lun->pr_key_count, res_keys->header.length); 7817 7818 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7819 if (!lun->per_res[i].registered) 7820 continue; 7821 7822 /* 7823 * We used lun->pr_key_count to calculate the 7824 * size to allocate. If it turns out the number of 7825 * initiators with the registered flag set is 7826 * larger than that (i.e. they haven't been kept in 7827 * sync), we've got a problem. 7828 */ 7829 if (key_count >= lun->pr_key_count) { 7830#ifdef NEEDTOPORT 7831 csevent_log(CSC_CTL | CSC_SHELF_SW | 7832 CTL_PR_ERROR, 7833 csevent_LogType_Fault, 7834 csevent_AlertLevel_Yellow, 7835 csevent_FRU_ShelfController, 7836 csevent_FRU_Firmware, 7837 csevent_FRU_Unknown, 7838 "registered keys %d >= key " 7839 "count %d", key_count, 7840 lun->pr_key_count); 7841#endif 7842 key_count++; 7843 continue; 7844 } 7845 memcpy(res_keys->keys[key_count].key, 7846 lun->per_res[i].res_key.key, 7847 ctl_min(sizeof(res_keys->keys[key_count].key), 7848 sizeof(lun->per_res[i].res_key))); 7849 key_count++; 7850 } 7851 break; 7852 } 7853 case SPRI_RR: { // read reservation 7854 struct scsi_per_res_in_rsrv *res; 7855 int tmp_len, header_only; 7856 7857 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7858 7859 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7860 7861 if (lun->flags & CTL_LUN_PR_RESERVED) 7862 { 7863 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7864 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7865 res->header.length); 7866 header_only = 0; 7867 } else { 7868 tmp_len = sizeof(struct scsi_per_res_in_header); 7869 scsi_ulto4b(0, res->header.length); 7870 header_only = 1; 7871 } 7872 7873 /* 7874 * We had to drop the lock to allocate our buffer, which 7875 * leaves time for someone to come in with another 7876 * persistent reservation. (That is unlikely, though, 7877 * since this should be the only persistent reservation 7878 * command active right now.) 7879 */ 7880 if (tmp_len != total_len) { 7881 mtx_unlock(&lun->lun_lock); 7882 free(ctsio->kern_data_ptr, M_CTL); 7883 printf("%s: reservation status changed, retrying\n", 7884 __func__); 7885 goto retry; 7886 } 7887 7888 /* 7889 * No reservation held, so we're done. 7890 */ 7891 if (header_only != 0) 7892 break; 7893 7894 /* 7895 * If the registration is an All Registrants type, the key 7896 * is 0, since it doesn't really matter. 7897 */ 7898 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7899 memcpy(res->data.reservation, 7900 &lun->per_res[lun->pr_res_idx].res_key, 7901 sizeof(struct scsi_per_res_key)); 7902 } 7903 res->data.scopetype = lun->res_type; 7904 break; 7905 } 7906 case SPRI_RC: //report capabilities 7907 { 7908 struct scsi_per_res_cap *res_cap; 7909 uint16_t type_mask; 7910 7911 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7912 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7913 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7914 type_mask = SPRI_TM_WR_EX_AR | 7915 SPRI_TM_EX_AC_RO | 7916 SPRI_TM_WR_EX_RO | 7917 SPRI_TM_EX_AC | 7918 SPRI_TM_WR_EX | 7919 SPRI_TM_EX_AC_AR; 7920 scsi_ulto2b(type_mask, res_cap->type_mask); 7921 break; 7922 } 7923 case SPRI_RS: { // read full status 7924 struct scsi_per_res_in_full *res_status; 7925 struct scsi_per_res_in_full_desc *res_desc; 7926 struct ctl_port *port; 7927 int i, len; 7928 7929 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7930 7931 /* 7932 * We had to drop the lock to allocate our buffer, which 7933 * leaves time for someone to come in with another 7934 * persistent reservation. (That is unlikely, though, 7935 * since this should be the only persistent reservation 7936 * command active right now.) 7937 */ 7938 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7939 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7940 lun->pr_key_count)){ 7941 mtx_unlock(&lun->lun_lock); 7942 free(ctsio->kern_data_ptr, M_CTL); 7943 printf("%s: reservation length changed, retrying\n", 7944 __func__); 7945 goto retry; 7946 } 7947 7948 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7949 7950 res_desc = &res_status->desc[0]; 7951 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7952 if (!lun->per_res[i].registered) 7953 continue; 7954 7955 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7956 sizeof(res_desc->res_key)); 7957 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7958 (lun->pr_res_idx == i || 7959 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7960 res_desc->flags = SPRI_FULL_R_HOLDER; 7961 res_desc->scopetype = lun->res_type; 7962 } 7963 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7964 res_desc->rel_trgt_port_id); 7965 len = 0; 7966 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7967 if (port != NULL) 7968 len = ctl_create_iid(port, 7969 i % CTL_MAX_INIT_PER_PORT, 7970 res_desc->transport_id); 7971 scsi_ulto4b(len, res_desc->additional_length); 7972 res_desc = (struct scsi_per_res_in_full_desc *) 7973 &res_desc->transport_id[len]; 7974 } 7975 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7976 res_status->header.length); 7977 break; 7978 } 7979 default: 7980 /* 7981 * This is a bug, because we just checked for this above, 7982 * and should have returned an error. 7983 */ 7984 panic("Invalid PR type %x", cdb->action); 7985 break; /* NOTREACHED */ 7986 } 7987 mtx_unlock(&lun->lun_lock); 7988 7989 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7990 ctsio->be_move_done = ctl_config_move_done; 7991 7992 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7993 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7994 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7995 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7996 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7997 7998 ctl_datamove((union ctl_io *)ctsio); 7999 8000 return (CTL_RETVAL_COMPLETE); 8001} 8002 8003/* 8004 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8005 * it should return. 8006 */ 8007static int 8008ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8009 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8010 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8011 struct scsi_per_res_out_parms* param) 8012{ 8013 union ctl_ha_msg persis_io; 8014 int retval, i; 8015 int isc_retval; 8016 8017 retval = 0; 8018 8019 mtx_lock(&lun->lun_lock); 8020 if (sa_res_key == 0) { 8021 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8022 /* validate scope and type */ 8023 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8024 SPR_LU_SCOPE) { 8025 mtx_unlock(&lun->lun_lock); 8026 ctl_set_invalid_field(/*ctsio*/ ctsio, 8027 /*sks_valid*/ 1, 8028 /*command*/ 1, 8029 /*field*/ 2, 8030 /*bit_valid*/ 1, 8031 /*bit*/ 4); 8032 ctl_done((union ctl_io *)ctsio); 8033 return (1); 8034 } 8035 8036 if (type>8 || type==2 || type==4 || type==0) { 8037 mtx_unlock(&lun->lun_lock); 8038 ctl_set_invalid_field(/*ctsio*/ ctsio, 8039 /*sks_valid*/ 1, 8040 /*command*/ 1, 8041 /*field*/ 2, 8042 /*bit_valid*/ 1, 8043 /*bit*/ 0); 8044 ctl_done((union ctl_io *)ctsio); 8045 return (1); 8046 } 8047 8048 /* temporarily unregister this nexus */ 8049 lun->per_res[residx].registered = 0; 8050 8051 /* 8052 * Unregister everybody else and build UA for 8053 * them 8054 */ 8055 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8056 if (lun->per_res[i].registered == 0) 8057 continue; 8058 8059 if (!persis_offset 8060 && i <CTL_MAX_INITIATORS) 8061 lun->pending_ua[i] |= 8062 CTL_UA_REG_PREEMPT; 8063 else if (persis_offset 8064 && i >= persis_offset) 8065 lun->pending_ua[i-persis_offset] |= 8066 CTL_UA_REG_PREEMPT; 8067 lun->per_res[i].registered = 0; 8068 memset(&lun->per_res[i].res_key, 0, 8069 sizeof(struct scsi_per_res_key)); 8070 } 8071 lun->per_res[residx].registered = 1; 8072 lun->pr_key_count = 1; 8073 lun->res_type = type; 8074 if (lun->res_type != SPR_TYPE_WR_EX_AR 8075 && lun->res_type != SPR_TYPE_EX_AC_AR) 8076 lun->pr_res_idx = residx; 8077 8078 /* send msg to other side */ 8079 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8080 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8081 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8082 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8083 persis_io.pr.pr_info.res_type = type; 8084 memcpy(persis_io.pr.pr_info.sa_res_key, 8085 param->serv_act_res_key, 8086 sizeof(param->serv_act_res_key)); 8087 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8088 &persis_io, sizeof(persis_io), 0)) > 8089 CTL_HA_STATUS_SUCCESS) { 8090 printf("CTL:Persis Out error returned " 8091 "from ctl_ha_msg_send %d\n", 8092 isc_retval); 8093 } 8094 } else { 8095 /* not all registrants */ 8096 mtx_unlock(&lun->lun_lock); 8097 free(ctsio->kern_data_ptr, M_CTL); 8098 ctl_set_invalid_field(ctsio, 8099 /*sks_valid*/ 1, 8100 /*command*/ 0, 8101 /*field*/ 8, 8102 /*bit_valid*/ 0, 8103 /*bit*/ 0); 8104 ctl_done((union ctl_io *)ctsio); 8105 return (1); 8106 } 8107 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8108 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8109 int found = 0; 8110 8111 if (res_key == sa_res_key) { 8112 /* special case */ 8113 /* 8114 * The spec implies this is not good but doesn't 8115 * say what to do. There are two choices either 8116 * generate a res conflict or check condition 8117 * with illegal field in parameter data. Since 8118 * that is what is done when the sa_res_key is 8119 * zero I'll take that approach since this has 8120 * to do with the sa_res_key. 8121 */ 8122 mtx_unlock(&lun->lun_lock); 8123 free(ctsio->kern_data_ptr, M_CTL); 8124 ctl_set_invalid_field(ctsio, 8125 /*sks_valid*/ 1, 8126 /*command*/ 0, 8127 /*field*/ 8, 8128 /*bit_valid*/ 0, 8129 /*bit*/ 0); 8130 ctl_done((union ctl_io *)ctsio); 8131 return (1); 8132 } 8133 8134 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8135 if (lun->per_res[i].registered 8136 && memcmp(param->serv_act_res_key, 8137 lun->per_res[i].res_key.key, 8138 sizeof(struct scsi_per_res_key)) != 0) 8139 continue; 8140 8141 found = 1; 8142 lun->per_res[i].registered = 0; 8143 memset(&lun->per_res[i].res_key, 0, 8144 sizeof(struct scsi_per_res_key)); 8145 lun->pr_key_count--; 8146 8147 if (!persis_offset && i < CTL_MAX_INITIATORS) 8148 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8149 else if (persis_offset && i >= persis_offset) 8150 lun->pending_ua[i-persis_offset] |= 8151 CTL_UA_REG_PREEMPT; 8152 } 8153 if (!found) { 8154 mtx_unlock(&lun->lun_lock); 8155 free(ctsio->kern_data_ptr, M_CTL); 8156 ctl_set_reservation_conflict(ctsio); 8157 ctl_done((union ctl_io *)ctsio); 8158 return (CTL_RETVAL_COMPLETE); 8159 } 8160 /* send msg to other side */ 8161 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8162 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8163 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8164 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8165 persis_io.pr.pr_info.res_type = type; 8166 memcpy(persis_io.pr.pr_info.sa_res_key, 8167 param->serv_act_res_key, 8168 sizeof(param->serv_act_res_key)); 8169 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8170 &persis_io, sizeof(persis_io), 0)) > 8171 CTL_HA_STATUS_SUCCESS) { 8172 printf("CTL:Persis Out error returned from " 8173 "ctl_ha_msg_send %d\n", isc_retval); 8174 } 8175 } else { 8176 /* Reserved but not all registrants */ 8177 /* sa_res_key is res holder */ 8178 if (memcmp(param->serv_act_res_key, 8179 lun->per_res[lun->pr_res_idx].res_key.key, 8180 sizeof(struct scsi_per_res_key)) == 0) { 8181 /* validate scope and type */ 8182 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8183 SPR_LU_SCOPE) { 8184 mtx_unlock(&lun->lun_lock); 8185 ctl_set_invalid_field(/*ctsio*/ ctsio, 8186 /*sks_valid*/ 1, 8187 /*command*/ 1, 8188 /*field*/ 2, 8189 /*bit_valid*/ 1, 8190 /*bit*/ 4); 8191 ctl_done((union ctl_io *)ctsio); 8192 return (1); 8193 } 8194 8195 if (type>8 || type==2 || type==4 || type==0) { 8196 mtx_unlock(&lun->lun_lock); 8197 ctl_set_invalid_field(/*ctsio*/ ctsio, 8198 /*sks_valid*/ 1, 8199 /*command*/ 1, 8200 /*field*/ 2, 8201 /*bit_valid*/ 1, 8202 /*bit*/ 0); 8203 ctl_done((union ctl_io *)ctsio); 8204 return (1); 8205 } 8206 8207 /* 8208 * Do the following: 8209 * if sa_res_key != res_key remove all 8210 * registrants w/sa_res_key and generate UA 8211 * for these registrants(Registrations 8212 * Preempted) if it wasn't an exclusive 8213 * reservation generate UA(Reservations 8214 * Preempted) for all other registered nexuses 8215 * if the type has changed. Establish the new 8216 * reservation and holder. If res_key and 8217 * sa_res_key are the same do the above 8218 * except don't unregister the res holder. 8219 */ 8220 8221 /* 8222 * Temporarily unregister so it won't get 8223 * removed or UA generated 8224 */ 8225 lun->per_res[residx].registered = 0; 8226 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8227 if (lun->per_res[i].registered == 0) 8228 continue; 8229 8230 if (memcmp(param->serv_act_res_key, 8231 lun->per_res[i].res_key.key, 8232 sizeof(struct scsi_per_res_key)) == 0) { 8233 lun->per_res[i].registered = 0; 8234 memset(&lun->per_res[i].res_key, 8235 0, 8236 sizeof(struct scsi_per_res_key)); 8237 lun->pr_key_count--; 8238 8239 if (!persis_offset 8240 && i < CTL_MAX_INITIATORS) 8241 lun->pending_ua[i] |= 8242 CTL_UA_REG_PREEMPT; 8243 else if (persis_offset 8244 && i >= persis_offset) 8245 lun->pending_ua[i-persis_offset] |= 8246 CTL_UA_REG_PREEMPT; 8247 } else if (type != lun->res_type 8248 && (lun->res_type == SPR_TYPE_WR_EX_RO 8249 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8250 if (!persis_offset 8251 && i < CTL_MAX_INITIATORS) 8252 lun->pending_ua[i] |= 8253 CTL_UA_RES_RELEASE; 8254 else if (persis_offset 8255 && i >= persis_offset) 8256 lun->pending_ua[ 8257 i-persis_offset] |= 8258 CTL_UA_RES_RELEASE; 8259 } 8260 } 8261 lun->per_res[residx].registered = 1; 8262 lun->res_type = type; 8263 if (lun->res_type != SPR_TYPE_WR_EX_AR 8264 && lun->res_type != SPR_TYPE_EX_AC_AR) 8265 lun->pr_res_idx = residx; 8266 else 8267 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8268 8269 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8270 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8271 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8272 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8273 persis_io.pr.pr_info.res_type = type; 8274 memcpy(persis_io.pr.pr_info.sa_res_key, 8275 param->serv_act_res_key, 8276 sizeof(param->serv_act_res_key)); 8277 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8278 &persis_io, sizeof(persis_io), 0)) > 8279 CTL_HA_STATUS_SUCCESS) { 8280 printf("CTL:Persis Out error returned " 8281 "from ctl_ha_msg_send %d\n", 8282 isc_retval); 8283 } 8284 } else { 8285 /* 8286 * sa_res_key is not the res holder just 8287 * remove registrants 8288 */ 8289 int found=0; 8290 8291 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8292 if (memcmp(param->serv_act_res_key, 8293 lun->per_res[i].res_key.key, 8294 sizeof(struct scsi_per_res_key)) != 0) 8295 continue; 8296 8297 found = 1; 8298 lun->per_res[i].registered = 0; 8299 memset(&lun->per_res[i].res_key, 0, 8300 sizeof(struct scsi_per_res_key)); 8301 lun->pr_key_count--; 8302 8303 if (!persis_offset 8304 && i < CTL_MAX_INITIATORS) 8305 lun->pending_ua[i] |= 8306 CTL_UA_REG_PREEMPT; 8307 else if (persis_offset 8308 && i >= persis_offset) 8309 lun->pending_ua[i-persis_offset] |= 8310 CTL_UA_REG_PREEMPT; 8311 } 8312 8313 if (!found) { 8314 mtx_unlock(&lun->lun_lock); 8315 free(ctsio->kern_data_ptr, M_CTL); 8316 ctl_set_reservation_conflict(ctsio); 8317 ctl_done((union ctl_io *)ctsio); 8318 return (1); 8319 } 8320 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8321 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8322 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8323 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8324 persis_io.pr.pr_info.res_type = type; 8325 memcpy(persis_io.pr.pr_info.sa_res_key, 8326 param->serv_act_res_key, 8327 sizeof(param->serv_act_res_key)); 8328 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8329 &persis_io, sizeof(persis_io), 0)) > 8330 CTL_HA_STATUS_SUCCESS) { 8331 printf("CTL:Persis Out error returned " 8332 "from ctl_ha_msg_send %d\n", 8333 isc_retval); 8334 } 8335 } 8336 } 8337 8338 lun->PRGeneration++; 8339 mtx_unlock(&lun->lun_lock); 8340 8341 return (retval); 8342} 8343 8344static void 8345ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8346{ 8347 int i; 8348 8349 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8350 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8351 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8352 msg->pr.pr_info.sa_res_key, 8353 sizeof(struct scsi_per_res_key)) != 0) { 8354 uint64_t sa_res_key; 8355 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8356 8357 if (sa_res_key == 0) { 8358 /* temporarily unregister this nexus */ 8359 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8360 8361 /* 8362 * Unregister everybody else and build UA for 8363 * them 8364 */ 8365 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8366 if (lun->per_res[i].registered == 0) 8367 continue; 8368 8369 if (!persis_offset 8370 && i < CTL_MAX_INITIATORS) 8371 lun->pending_ua[i] |= 8372 CTL_UA_REG_PREEMPT; 8373 else if (persis_offset && i >= persis_offset) 8374 lun->pending_ua[i - persis_offset] |= 8375 CTL_UA_REG_PREEMPT; 8376 lun->per_res[i].registered = 0; 8377 memset(&lun->per_res[i].res_key, 0, 8378 sizeof(struct scsi_per_res_key)); 8379 } 8380 8381 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8382 lun->pr_key_count = 1; 8383 lun->res_type = msg->pr.pr_info.res_type; 8384 if (lun->res_type != SPR_TYPE_WR_EX_AR 8385 && lun->res_type != SPR_TYPE_EX_AC_AR) 8386 lun->pr_res_idx = msg->pr.pr_info.residx; 8387 } else { 8388 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8389 if (memcmp(msg->pr.pr_info.sa_res_key, 8390 lun->per_res[i].res_key.key, 8391 sizeof(struct scsi_per_res_key)) != 0) 8392 continue; 8393 8394 lun->per_res[i].registered = 0; 8395 memset(&lun->per_res[i].res_key, 0, 8396 sizeof(struct scsi_per_res_key)); 8397 lun->pr_key_count--; 8398 8399 if (!persis_offset 8400 && i < persis_offset) 8401 lun->pending_ua[i] |= 8402 CTL_UA_REG_PREEMPT; 8403 else if (persis_offset 8404 && i >= persis_offset) 8405 lun->pending_ua[i - persis_offset] |= 8406 CTL_UA_REG_PREEMPT; 8407 } 8408 } 8409 } else { 8410 /* 8411 * Temporarily unregister so it won't get removed 8412 * or UA generated 8413 */ 8414 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8415 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8416 if (lun->per_res[i].registered == 0) 8417 continue; 8418 8419 if (memcmp(msg->pr.pr_info.sa_res_key, 8420 lun->per_res[i].res_key.key, 8421 sizeof(struct scsi_per_res_key)) == 0) { 8422 lun->per_res[i].registered = 0; 8423 memset(&lun->per_res[i].res_key, 0, 8424 sizeof(struct scsi_per_res_key)); 8425 lun->pr_key_count--; 8426 if (!persis_offset 8427 && i < CTL_MAX_INITIATORS) 8428 lun->pending_ua[i] |= 8429 CTL_UA_REG_PREEMPT; 8430 else if (persis_offset 8431 && i >= persis_offset) 8432 lun->pending_ua[i - persis_offset] |= 8433 CTL_UA_REG_PREEMPT; 8434 } else if (msg->pr.pr_info.res_type != lun->res_type 8435 && (lun->res_type == SPR_TYPE_WR_EX_RO 8436 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8437 if (!persis_offset 8438 && i < persis_offset) 8439 lun->pending_ua[i] |= 8440 CTL_UA_RES_RELEASE; 8441 else if (persis_offset 8442 && i >= persis_offset) 8443 lun->pending_ua[i - persis_offset] |= 8444 CTL_UA_RES_RELEASE; 8445 } 8446 } 8447 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8448 lun->res_type = msg->pr.pr_info.res_type; 8449 if (lun->res_type != SPR_TYPE_WR_EX_AR 8450 && lun->res_type != SPR_TYPE_EX_AC_AR) 8451 lun->pr_res_idx = msg->pr.pr_info.residx; 8452 else 8453 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8454 } 8455 lun->PRGeneration++; 8456 8457} 8458 8459 8460int 8461ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8462{ 8463 int retval; 8464 int isc_retval; 8465 u_int32_t param_len; 8466 struct scsi_per_res_out *cdb; 8467 struct ctl_lun *lun; 8468 struct scsi_per_res_out_parms* param; 8469 struct ctl_softc *softc; 8470 uint32_t residx; 8471 uint64_t res_key, sa_res_key; 8472 uint8_t type; 8473 union ctl_ha_msg persis_io; 8474 int i; 8475 8476 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8477 8478 retval = CTL_RETVAL_COMPLETE; 8479 8480 softc = control_softc; 8481 8482 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8483 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8484 8485 /* 8486 * We only support whole-LUN scope. The scope & type are ignored for 8487 * register, register and ignore existing key and clear. 8488 * We sometimes ignore scope and type on preempts too!! 8489 * Verify reservation type here as well. 8490 */ 8491 type = cdb->scope_type & SPR_TYPE_MASK; 8492 if ((cdb->action == SPRO_RESERVE) 8493 || (cdb->action == SPRO_RELEASE)) { 8494 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8495 ctl_set_invalid_field(/*ctsio*/ ctsio, 8496 /*sks_valid*/ 1, 8497 /*command*/ 1, 8498 /*field*/ 2, 8499 /*bit_valid*/ 1, 8500 /*bit*/ 4); 8501 ctl_done((union ctl_io *)ctsio); 8502 return (CTL_RETVAL_COMPLETE); 8503 } 8504 8505 if (type>8 || type==2 || type==4 || type==0) { 8506 ctl_set_invalid_field(/*ctsio*/ ctsio, 8507 /*sks_valid*/ 1, 8508 /*command*/ 1, 8509 /*field*/ 2, 8510 /*bit_valid*/ 1, 8511 /*bit*/ 0); 8512 ctl_done((union ctl_io *)ctsio); 8513 return (CTL_RETVAL_COMPLETE); 8514 } 8515 } 8516 8517 param_len = scsi_4btoul(cdb->length); 8518 8519 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8520 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8521 ctsio->kern_data_len = param_len; 8522 ctsio->kern_total_len = param_len; 8523 ctsio->kern_data_resid = 0; 8524 ctsio->kern_rel_offset = 0; 8525 ctsio->kern_sg_entries = 0; 8526 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8527 ctsio->be_move_done = ctl_config_move_done; 8528 ctl_datamove((union ctl_io *)ctsio); 8529 8530 return (CTL_RETVAL_COMPLETE); 8531 } 8532 8533 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8534 8535 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8536 res_key = scsi_8btou64(param->res_key.key); 8537 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8538 8539 /* 8540 * Validate the reservation key here except for SPRO_REG_IGNO 8541 * This must be done for all other service actions 8542 */ 8543 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8544 mtx_lock(&lun->lun_lock); 8545 if (lun->per_res[residx].registered) { 8546 if (memcmp(param->res_key.key, 8547 lun->per_res[residx].res_key.key, 8548 ctl_min(sizeof(param->res_key), 8549 sizeof(lun->per_res[residx].res_key))) != 0) { 8550 /* 8551 * The current key passed in doesn't match 8552 * the one the initiator previously 8553 * 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 } 8561 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8562 /* 8563 * We are not registered 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 } else if (res_key != 0) { 8571 /* 8572 * We are not registered and trying to register but 8573 * the register key isn't zero. 8574 */ 8575 mtx_unlock(&lun->lun_lock); 8576 free(ctsio->kern_data_ptr, M_CTL); 8577 ctl_set_reservation_conflict(ctsio); 8578 ctl_done((union ctl_io *)ctsio); 8579 return (CTL_RETVAL_COMPLETE); 8580 } 8581 mtx_unlock(&lun->lun_lock); 8582 } 8583 8584 switch (cdb->action & SPRO_ACTION_MASK) { 8585 case SPRO_REGISTER: 8586 case SPRO_REG_IGNO: { 8587 8588#if 0 8589 printf("Registration received\n"); 8590#endif 8591 8592 /* 8593 * We don't support any of these options, as we report in 8594 * the read capabilities request (see 8595 * ctl_persistent_reserve_in(), above). 8596 */ 8597 if ((param->flags & SPR_SPEC_I_PT) 8598 || (param->flags & SPR_ALL_TG_PT) 8599 || (param->flags & SPR_APTPL)) { 8600 int bit_ptr; 8601 8602 if (param->flags & SPR_APTPL) 8603 bit_ptr = 0; 8604 else if (param->flags & SPR_ALL_TG_PT) 8605 bit_ptr = 2; 8606 else /* SPR_SPEC_I_PT */ 8607 bit_ptr = 3; 8608 8609 free(ctsio->kern_data_ptr, M_CTL); 8610 ctl_set_invalid_field(ctsio, 8611 /*sks_valid*/ 1, 8612 /*command*/ 0, 8613 /*field*/ 20, 8614 /*bit_valid*/ 1, 8615 /*bit*/ bit_ptr); 8616 ctl_done((union ctl_io *)ctsio); 8617 return (CTL_RETVAL_COMPLETE); 8618 } 8619 8620 mtx_lock(&lun->lun_lock); 8621 8622 /* 8623 * The initiator wants to clear the 8624 * key/unregister. 8625 */ 8626 if (sa_res_key == 0) { 8627 if ((res_key == 0 8628 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8629 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8630 && !lun->per_res[residx].registered)) { 8631 mtx_unlock(&lun->lun_lock); 8632 goto done; 8633 } 8634 8635 lun->per_res[residx].registered = 0; 8636 memset(&lun->per_res[residx].res_key, 8637 0, sizeof(lun->per_res[residx].res_key)); 8638 lun->pr_key_count--; 8639 8640 if (residx == lun->pr_res_idx) { 8641 lun->flags &= ~CTL_LUN_PR_RESERVED; 8642 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8643 8644 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8645 || lun->res_type == SPR_TYPE_EX_AC_RO) 8646 && lun->pr_key_count) { 8647 /* 8648 * If the reservation is a registrants 8649 * only type we need to generate a UA 8650 * for other registered inits. The 8651 * sense code should be RESERVATIONS 8652 * RELEASED 8653 */ 8654 8655 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8656 if (lun->per_res[ 8657 i+persis_offset].registered 8658 == 0) 8659 continue; 8660 lun->pending_ua[i] |= 8661 CTL_UA_RES_RELEASE; 8662 } 8663 } 8664 lun->res_type = 0; 8665 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8666 if (lun->pr_key_count==0) { 8667 lun->flags &= ~CTL_LUN_PR_RESERVED; 8668 lun->res_type = 0; 8669 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8670 } 8671 } 8672 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8673 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8674 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8675 persis_io.pr.pr_info.residx = residx; 8676 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8677 &persis_io, sizeof(persis_io), 0 )) > 8678 CTL_HA_STATUS_SUCCESS) { 8679 printf("CTL:Persis Out error returned from " 8680 "ctl_ha_msg_send %d\n", isc_retval); 8681 } 8682 } else /* sa_res_key != 0 */ { 8683 8684 /* 8685 * If we aren't registered currently then increment 8686 * the key count and set the registered flag. 8687 */ 8688 if (!lun->per_res[residx].registered) { 8689 lun->pr_key_count++; 8690 lun->per_res[residx].registered = 1; 8691 } 8692 8693 memcpy(&lun->per_res[residx].res_key, 8694 param->serv_act_res_key, 8695 ctl_min(sizeof(param->serv_act_res_key), 8696 sizeof(lun->per_res[residx].res_key))); 8697 8698 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8699 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8700 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8701 persis_io.pr.pr_info.residx = residx; 8702 memcpy(persis_io.pr.pr_info.sa_res_key, 8703 param->serv_act_res_key, 8704 sizeof(param->serv_act_res_key)); 8705 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8706 &persis_io, sizeof(persis_io), 0)) > 8707 CTL_HA_STATUS_SUCCESS) { 8708 printf("CTL:Persis Out error returned from " 8709 "ctl_ha_msg_send %d\n", isc_retval); 8710 } 8711 } 8712 lun->PRGeneration++; 8713 mtx_unlock(&lun->lun_lock); 8714 8715 break; 8716 } 8717 case SPRO_RESERVE: 8718#if 0 8719 printf("Reserve executed type %d\n", type); 8720#endif 8721 mtx_lock(&lun->lun_lock); 8722 if (lun->flags & CTL_LUN_PR_RESERVED) { 8723 /* 8724 * if this isn't the reservation holder and it's 8725 * not a "all registrants" type or if the type is 8726 * different then we have a conflict 8727 */ 8728 if ((lun->pr_res_idx != residx 8729 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8730 || lun->res_type != type) { 8731 mtx_unlock(&lun->lun_lock); 8732 free(ctsio->kern_data_ptr, M_CTL); 8733 ctl_set_reservation_conflict(ctsio); 8734 ctl_done((union ctl_io *)ctsio); 8735 return (CTL_RETVAL_COMPLETE); 8736 } 8737 mtx_unlock(&lun->lun_lock); 8738 } else /* create a reservation */ { 8739 /* 8740 * If it's not an "all registrants" type record 8741 * reservation holder 8742 */ 8743 if (type != SPR_TYPE_WR_EX_AR 8744 && type != SPR_TYPE_EX_AC_AR) 8745 lun->pr_res_idx = residx; /* Res holder */ 8746 else 8747 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8748 8749 lun->flags |= CTL_LUN_PR_RESERVED; 8750 lun->res_type = type; 8751 8752 mtx_unlock(&lun->lun_lock); 8753 8754 /* send msg to other side */ 8755 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8756 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8757 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8758 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8759 persis_io.pr.pr_info.res_type = type; 8760 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8761 &persis_io, sizeof(persis_io), 0)) > 8762 CTL_HA_STATUS_SUCCESS) { 8763 printf("CTL:Persis Out error returned from " 8764 "ctl_ha_msg_send %d\n", isc_retval); 8765 } 8766 } 8767 break; 8768 8769 case SPRO_RELEASE: 8770 mtx_lock(&lun->lun_lock); 8771 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8772 /* No reservation exists return good status */ 8773 mtx_unlock(&lun->lun_lock); 8774 goto done; 8775 } 8776 /* 8777 * Is this nexus a reservation holder? 8778 */ 8779 if (lun->pr_res_idx != residx 8780 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8781 /* 8782 * not a res holder return good status but 8783 * do nothing 8784 */ 8785 mtx_unlock(&lun->lun_lock); 8786 goto done; 8787 } 8788 8789 if (lun->res_type != type) { 8790 mtx_unlock(&lun->lun_lock); 8791 free(ctsio->kern_data_ptr, M_CTL); 8792 ctl_set_illegal_pr_release(ctsio); 8793 ctl_done((union ctl_io *)ctsio); 8794 return (CTL_RETVAL_COMPLETE); 8795 } 8796 8797 /* okay to release */ 8798 lun->flags &= ~CTL_LUN_PR_RESERVED; 8799 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8800 lun->res_type = 0; 8801 8802 /* 8803 * if this isn't an exclusive access 8804 * res generate UA for all other 8805 * registrants. 8806 */ 8807 if (type != SPR_TYPE_EX_AC 8808 && type != SPR_TYPE_WR_EX) { 8809 /* 8810 * temporarily unregister so we don't generate UA 8811 */ 8812 lun->per_res[residx].registered = 0; 8813 8814 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8815 if (lun->per_res[i+persis_offset].registered 8816 == 0) 8817 continue; 8818 lun->pending_ua[i] |= 8819 CTL_UA_RES_RELEASE; 8820 } 8821 8822 lun->per_res[residx].registered = 1; 8823 } 8824 mtx_unlock(&lun->lun_lock); 8825 /* Send msg to other side */ 8826 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8827 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8828 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8829 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8830 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8831 printf("CTL:Persis Out error returned from " 8832 "ctl_ha_msg_send %d\n", isc_retval); 8833 } 8834 break; 8835 8836 case SPRO_CLEAR: 8837 /* send msg to other side */ 8838 8839 mtx_lock(&lun->lun_lock); 8840 lun->flags &= ~CTL_LUN_PR_RESERVED; 8841 lun->res_type = 0; 8842 lun->pr_key_count = 0; 8843 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8844 8845 8846 memset(&lun->per_res[residx].res_key, 8847 0, sizeof(lun->per_res[residx].res_key)); 8848 lun->per_res[residx].registered = 0; 8849 8850 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8851 if (lun->per_res[i].registered) { 8852 if (!persis_offset && i < CTL_MAX_INITIATORS) 8853 lun->pending_ua[i] |= 8854 CTL_UA_RES_PREEMPT; 8855 else if (persis_offset && i >= persis_offset) 8856 lun->pending_ua[i-persis_offset] |= 8857 CTL_UA_RES_PREEMPT; 8858 8859 memset(&lun->per_res[i].res_key, 8860 0, sizeof(struct scsi_per_res_key)); 8861 lun->per_res[i].registered = 0; 8862 } 8863 lun->PRGeneration++; 8864 mtx_unlock(&lun->lun_lock); 8865 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8866 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8867 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8868 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8869 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8870 printf("CTL:Persis Out error returned from " 8871 "ctl_ha_msg_send %d\n", isc_retval); 8872 } 8873 break; 8874 8875 case SPRO_PREEMPT: { 8876 int nretval; 8877 8878 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8879 residx, ctsio, cdb, param); 8880 if (nretval != 0) 8881 return (CTL_RETVAL_COMPLETE); 8882 break; 8883 } 8884 default: 8885 panic("Invalid PR type %x", cdb->action); 8886 } 8887 8888done: 8889 free(ctsio->kern_data_ptr, M_CTL); 8890 ctl_set_success(ctsio); 8891 ctl_done((union ctl_io *)ctsio); 8892 8893 return (retval); 8894} 8895 8896/* 8897 * This routine is for handling a message from the other SC pertaining to 8898 * persistent reserve out. All the error checking will have been done 8899 * so only perorming the action need be done here to keep the two 8900 * in sync. 8901 */ 8902static void 8903ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8904{ 8905 struct ctl_lun *lun; 8906 struct ctl_softc *softc; 8907 int i; 8908 uint32_t targ_lun; 8909 8910 softc = control_softc; 8911 8912 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8913 lun = softc->ctl_luns[targ_lun]; 8914 mtx_lock(&lun->lun_lock); 8915 switch(msg->pr.pr_info.action) { 8916 case CTL_PR_REG_KEY: 8917 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8918 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8919 lun->pr_key_count++; 8920 } 8921 lun->PRGeneration++; 8922 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8923 msg->pr.pr_info.sa_res_key, 8924 sizeof(struct scsi_per_res_key)); 8925 break; 8926 8927 case CTL_PR_UNREG_KEY: 8928 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8929 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8930 0, sizeof(struct scsi_per_res_key)); 8931 lun->pr_key_count--; 8932 8933 /* XXX Need to see if the reservation has been released */ 8934 /* if so do we need to generate UA? */ 8935 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8936 lun->flags &= ~CTL_LUN_PR_RESERVED; 8937 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8938 8939 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8940 || lun->res_type == SPR_TYPE_EX_AC_RO) 8941 && lun->pr_key_count) { 8942 /* 8943 * If the reservation is a registrants 8944 * only type we need to generate a UA 8945 * for other registered inits. The 8946 * sense code should be RESERVATIONS 8947 * RELEASED 8948 */ 8949 8950 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8951 if (lun->per_res[i+ 8952 persis_offset].registered == 0) 8953 continue; 8954 8955 lun->pending_ua[i] |= 8956 CTL_UA_RES_RELEASE; 8957 } 8958 } 8959 lun->res_type = 0; 8960 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8961 if (lun->pr_key_count==0) { 8962 lun->flags &= ~CTL_LUN_PR_RESERVED; 8963 lun->res_type = 0; 8964 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8965 } 8966 } 8967 lun->PRGeneration++; 8968 break; 8969 8970 case CTL_PR_RESERVE: 8971 lun->flags |= CTL_LUN_PR_RESERVED; 8972 lun->res_type = msg->pr.pr_info.res_type; 8973 lun->pr_res_idx = msg->pr.pr_info.residx; 8974 8975 break; 8976 8977 case CTL_PR_RELEASE: 8978 /* 8979 * if this isn't an exclusive access res generate UA for all 8980 * other registrants. 8981 */ 8982 if (lun->res_type != SPR_TYPE_EX_AC 8983 && lun->res_type != SPR_TYPE_WR_EX) { 8984 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8985 if (lun->per_res[i+persis_offset].registered) 8986 lun->pending_ua[i] |= 8987 CTL_UA_RES_RELEASE; 8988 } 8989 8990 lun->flags &= ~CTL_LUN_PR_RESERVED; 8991 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8992 lun->res_type = 0; 8993 break; 8994 8995 case CTL_PR_PREEMPT: 8996 ctl_pro_preempt_other(lun, msg); 8997 break; 8998 case CTL_PR_CLEAR: 8999 lun->flags &= ~CTL_LUN_PR_RESERVED; 9000 lun->res_type = 0; 9001 lun->pr_key_count = 0; 9002 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9003 9004 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 9005 if (lun->per_res[i].registered == 0) 9006 continue; 9007 if (!persis_offset 9008 && i < CTL_MAX_INITIATORS) 9009 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 9010 else if (persis_offset 9011 && i >= persis_offset) 9012 lun->pending_ua[i-persis_offset] |= 9013 CTL_UA_RES_PREEMPT; 9014 memset(&lun->per_res[i].res_key, 0, 9015 sizeof(struct scsi_per_res_key)); 9016 lun->per_res[i].registered = 0; 9017 } 9018 lun->PRGeneration++; 9019 break; 9020 } 9021 9022 mtx_unlock(&lun->lun_lock); 9023} 9024 9025int 9026ctl_read_write(struct ctl_scsiio *ctsio) 9027{ 9028 struct ctl_lun *lun; 9029 struct ctl_lba_len_flags *lbalen; 9030 uint64_t lba; 9031 uint32_t num_blocks; 9032 int flags, retval; 9033 int isread; 9034 9035 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9036 9037 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9038 9039 flags = 0; 9040 retval = CTL_RETVAL_COMPLETE; 9041 9042 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9043 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9044 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9045 uint32_t residx; 9046 9047 /* 9048 * XXX KDM need a lock here. 9049 */ 9050 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9051 if ((lun->res_type == SPR_TYPE_EX_AC 9052 && residx != lun->pr_res_idx) 9053 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9054 || lun->res_type == SPR_TYPE_EX_AC_AR) 9055 && !lun->per_res[residx].registered)) { 9056 ctl_set_reservation_conflict(ctsio); 9057 ctl_done((union ctl_io *)ctsio); 9058 return (CTL_RETVAL_COMPLETE); 9059 } 9060 } 9061 9062 switch (ctsio->cdb[0]) { 9063 case READ_6: 9064 case WRITE_6: { 9065 struct scsi_rw_6 *cdb; 9066 9067 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9068 9069 lba = scsi_3btoul(cdb->addr); 9070 /* only 5 bits are valid in the most significant address byte */ 9071 lba &= 0x1fffff; 9072 num_blocks = cdb->length; 9073 /* 9074 * This is correct according to SBC-2. 9075 */ 9076 if (num_blocks == 0) 9077 num_blocks = 256; 9078 break; 9079 } 9080 case READ_10: 9081 case WRITE_10: { 9082 struct scsi_rw_10 *cdb; 9083 9084 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9085 if (cdb->byte2 & SRW10_FUA) 9086 flags |= CTL_LLF_FUA; 9087 if (cdb->byte2 & SRW10_DPO) 9088 flags |= CTL_LLF_DPO; 9089 lba = scsi_4btoul(cdb->addr); 9090 num_blocks = scsi_2btoul(cdb->length); 9091 break; 9092 } 9093 case WRITE_VERIFY_10: { 9094 struct scsi_write_verify_10 *cdb; 9095 9096 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9097 flags |= CTL_LLF_FUA; 9098 if (cdb->byte2 & SWV_DPO) 9099 flags |= CTL_LLF_DPO; 9100 lba = scsi_4btoul(cdb->addr); 9101 num_blocks = scsi_2btoul(cdb->length); 9102 break; 9103 } 9104 case READ_12: 9105 case WRITE_12: { 9106 struct scsi_rw_12 *cdb; 9107 9108 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9109 if (cdb->byte2 & SRW12_FUA) 9110 flags |= CTL_LLF_FUA; 9111 if (cdb->byte2 & SRW12_DPO) 9112 flags |= CTL_LLF_DPO; 9113 lba = scsi_4btoul(cdb->addr); 9114 num_blocks = scsi_4btoul(cdb->length); 9115 break; 9116 } 9117 case WRITE_VERIFY_12: { 9118 struct scsi_write_verify_12 *cdb; 9119 9120 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9121 flags |= CTL_LLF_FUA; 9122 if (cdb->byte2 & SWV_DPO) 9123 flags |= CTL_LLF_DPO; 9124 lba = scsi_4btoul(cdb->addr); 9125 num_blocks = scsi_4btoul(cdb->length); 9126 break; 9127 } 9128 case READ_16: 9129 case WRITE_16: { 9130 struct scsi_rw_16 *cdb; 9131 9132 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9133 if (cdb->byte2 & SRW12_FUA) 9134 flags |= CTL_LLF_FUA; 9135 if (cdb->byte2 & SRW12_DPO) 9136 flags |= CTL_LLF_DPO; 9137 lba = scsi_8btou64(cdb->addr); 9138 num_blocks = scsi_4btoul(cdb->length); 9139 break; 9140 } 9141 case WRITE_VERIFY_16: { 9142 struct scsi_write_verify_16 *cdb; 9143 9144 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9145 flags |= CTL_LLF_FUA; 9146 if (cdb->byte2 & SWV_DPO) 9147 flags |= CTL_LLF_DPO; 9148 lba = scsi_8btou64(cdb->addr); 9149 num_blocks = scsi_4btoul(cdb->length); 9150 break; 9151 } 9152 default: 9153 /* 9154 * We got a command we don't support. This shouldn't 9155 * happen, commands should be filtered out above us. 9156 */ 9157 ctl_set_invalid_opcode(ctsio); 9158 ctl_done((union ctl_io *)ctsio); 9159 9160 return (CTL_RETVAL_COMPLETE); 9161 break; /* NOTREACHED */ 9162 } 9163 9164 /* 9165 * The first check is to make sure we're in bounds, the second 9166 * check is to catch wrap-around problems. If the lba + num blocks 9167 * is less than the lba, then we've wrapped around and the block 9168 * range is invalid anyway. 9169 */ 9170 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9171 || ((lba + num_blocks) < lba)) { 9172 ctl_set_lba_out_of_range(ctsio); 9173 ctl_done((union ctl_io *)ctsio); 9174 return (CTL_RETVAL_COMPLETE); 9175 } 9176 9177 /* 9178 * According to SBC-3, a transfer length of 0 is not an error. 9179 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9180 * translates to 256 blocks for those commands. 9181 */ 9182 if (num_blocks == 0) { 9183 ctl_set_success(ctsio); 9184 ctl_done((union ctl_io *)ctsio); 9185 return (CTL_RETVAL_COMPLETE); 9186 } 9187 9188 /* Set FUA and/or DPO if caches are disabled. */ 9189 if (isread) { 9190 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9191 SCP_RCD) != 0) 9192 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9193 } else { 9194 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9195 SCP_WCE) == 0) 9196 flags |= CTL_LLF_FUA; 9197 } 9198 9199 lbalen = (struct ctl_lba_len_flags *) 9200 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9201 lbalen->lba = lba; 9202 lbalen->len = num_blocks; 9203 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9204 9205 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9206 ctsio->kern_rel_offset = 0; 9207 9208 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9209 9210 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9211 9212 return (retval); 9213} 9214 9215static int 9216ctl_cnw_cont(union ctl_io *io) 9217{ 9218 struct ctl_scsiio *ctsio; 9219 struct ctl_lun *lun; 9220 struct ctl_lba_len_flags *lbalen; 9221 int retval; 9222 9223 ctsio = &io->scsiio; 9224 ctsio->io_hdr.status = CTL_STATUS_NONE; 9225 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9226 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9227 lbalen = (struct ctl_lba_len_flags *) 9228 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9229 lbalen->flags &= ~CTL_LLF_COMPARE; 9230 lbalen->flags |= CTL_LLF_WRITE; 9231 9232 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9233 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9234 return (retval); 9235} 9236 9237int 9238ctl_cnw(struct ctl_scsiio *ctsio) 9239{ 9240 struct ctl_lun *lun; 9241 struct ctl_lba_len_flags *lbalen; 9242 uint64_t lba; 9243 uint32_t num_blocks; 9244 int flags, retval; 9245 9246 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9247 9248 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9249 9250 flags = 0; 9251 retval = CTL_RETVAL_COMPLETE; 9252 9253 switch (ctsio->cdb[0]) { 9254 case COMPARE_AND_WRITE: { 9255 struct scsi_compare_and_write *cdb; 9256 9257 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9258 if (cdb->byte2 & SRW10_FUA) 9259 flags |= CTL_LLF_FUA; 9260 if (cdb->byte2 & SRW10_DPO) 9261 flags |= CTL_LLF_DPO; 9262 lba = scsi_8btou64(cdb->addr); 9263 num_blocks = cdb->length; 9264 break; 9265 } 9266 default: 9267 /* 9268 * We got a command we don't support. This shouldn't 9269 * happen, commands should be filtered out above us. 9270 */ 9271 ctl_set_invalid_opcode(ctsio); 9272 ctl_done((union ctl_io *)ctsio); 9273 9274 return (CTL_RETVAL_COMPLETE); 9275 break; /* NOTREACHED */ 9276 } 9277 9278 /* 9279 * The first check is to make sure we're in bounds, the second 9280 * check is to catch wrap-around problems. If the lba + num blocks 9281 * is less than the lba, then we've wrapped around and the block 9282 * range is invalid anyway. 9283 */ 9284 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9285 || ((lba + num_blocks) < lba)) { 9286 ctl_set_lba_out_of_range(ctsio); 9287 ctl_done((union ctl_io *)ctsio); 9288 return (CTL_RETVAL_COMPLETE); 9289 } 9290 9291 /* 9292 * According to SBC-3, a transfer length of 0 is not an error. 9293 */ 9294 if (num_blocks == 0) { 9295 ctl_set_success(ctsio); 9296 ctl_done((union ctl_io *)ctsio); 9297 return (CTL_RETVAL_COMPLETE); 9298 } 9299 9300 /* Set FUA if write cache is disabled. */ 9301 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9302 SCP_WCE) == 0) 9303 flags |= CTL_LLF_FUA; 9304 9305 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9306 ctsio->kern_rel_offset = 0; 9307 9308 /* 9309 * Set the IO_CONT flag, so that if this I/O gets passed to 9310 * ctl_data_submit_done(), it'll get passed back to 9311 * ctl_ctl_cnw_cont() for further processing. 9312 */ 9313 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9314 ctsio->io_cont = ctl_cnw_cont; 9315 9316 lbalen = (struct ctl_lba_len_flags *) 9317 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9318 lbalen->lba = lba; 9319 lbalen->len = num_blocks; 9320 lbalen->flags = CTL_LLF_COMPARE | flags; 9321 9322 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9323 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9324 return (retval); 9325} 9326 9327int 9328ctl_verify(struct ctl_scsiio *ctsio) 9329{ 9330 struct ctl_lun *lun; 9331 struct ctl_lba_len_flags *lbalen; 9332 uint64_t lba; 9333 uint32_t num_blocks; 9334 int bytchk, flags; 9335 int retval; 9336 9337 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9338 9339 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9340 9341 bytchk = 0; 9342 flags = CTL_LLF_FUA; 9343 retval = CTL_RETVAL_COMPLETE; 9344 9345 switch (ctsio->cdb[0]) { 9346 case VERIFY_10: { 9347 struct scsi_verify_10 *cdb; 9348 9349 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9350 if (cdb->byte2 & SVFY_BYTCHK) 9351 bytchk = 1; 9352 if (cdb->byte2 & SVFY_DPO) 9353 flags |= CTL_LLF_DPO; 9354 lba = scsi_4btoul(cdb->addr); 9355 num_blocks = scsi_2btoul(cdb->length); 9356 break; 9357 } 9358 case VERIFY_12: { 9359 struct scsi_verify_12 *cdb; 9360 9361 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9362 if (cdb->byte2 & SVFY_BYTCHK) 9363 bytchk = 1; 9364 if (cdb->byte2 & SVFY_DPO) 9365 flags |= CTL_LLF_DPO; 9366 lba = scsi_4btoul(cdb->addr); 9367 num_blocks = scsi_4btoul(cdb->length); 9368 break; 9369 } 9370 case VERIFY_16: { 9371 struct scsi_rw_16 *cdb; 9372 9373 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9374 if (cdb->byte2 & SVFY_BYTCHK) 9375 bytchk = 1; 9376 if (cdb->byte2 & SVFY_DPO) 9377 flags |= CTL_LLF_DPO; 9378 lba = scsi_8btou64(cdb->addr); 9379 num_blocks = scsi_4btoul(cdb->length); 9380 break; 9381 } 9382 default: 9383 /* 9384 * We got a command we don't support. This shouldn't 9385 * happen, commands should be filtered out above us. 9386 */ 9387 ctl_set_invalid_opcode(ctsio); 9388 ctl_done((union ctl_io *)ctsio); 9389 return (CTL_RETVAL_COMPLETE); 9390 } 9391 9392 /* 9393 * The first check is to make sure we're in bounds, the second 9394 * check is to catch wrap-around problems. If the lba + num blocks 9395 * is less than the lba, then we've wrapped around and the block 9396 * range is invalid anyway. 9397 */ 9398 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9399 || ((lba + num_blocks) < lba)) { 9400 ctl_set_lba_out_of_range(ctsio); 9401 ctl_done((union ctl_io *)ctsio); 9402 return (CTL_RETVAL_COMPLETE); 9403 } 9404 9405 /* 9406 * According to SBC-3, a transfer length of 0 is not an error. 9407 */ 9408 if (num_blocks == 0) { 9409 ctl_set_success(ctsio); 9410 ctl_done((union ctl_io *)ctsio); 9411 return (CTL_RETVAL_COMPLETE); 9412 } 9413 9414 lbalen = (struct ctl_lba_len_flags *) 9415 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9416 lbalen->lba = lba; 9417 lbalen->len = num_blocks; 9418 if (bytchk) { 9419 lbalen->flags = CTL_LLF_COMPARE | flags; 9420 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9421 } else { 9422 lbalen->flags = CTL_LLF_VERIFY | flags; 9423 ctsio->kern_total_len = 0; 9424 } 9425 ctsio->kern_rel_offset = 0; 9426 9427 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9428 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9429 return (retval); 9430} 9431 9432int 9433ctl_report_luns(struct ctl_scsiio *ctsio) 9434{ 9435 struct scsi_report_luns *cdb; 9436 struct scsi_report_luns_data *lun_data; 9437 struct ctl_lun *lun, *request_lun; 9438 int num_luns, retval; 9439 uint32_t alloc_len, lun_datalen; 9440 int num_filled, well_known; 9441 uint32_t initidx, targ_lun_id, lun_id; 9442 9443 retval = CTL_RETVAL_COMPLETE; 9444 well_known = 0; 9445 9446 cdb = (struct scsi_report_luns *)ctsio->cdb; 9447 9448 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9449 9450 mtx_lock(&control_softc->ctl_lock); 9451 num_luns = control_softc->num_luns; 9452 mtx_unlock(&control_softc->ctl_lock); 9453 9454 switch (cdb->select_report) { 9455 case RPL_REPORT_DEFAULT: 9456 case RPL_REPORT_ALL: 9457 break; 9458 case RPL_REPORT_WELLKNOWN: 9459 well_known = 1; 9460 num_luns = 0; 9461 break; 9462 default: 9463 ctl_set_invalid_field(ctsio, 9464 /*sks_valid*/ 1, 9465 /*command*/ 1, 9466 /*field*/ 2, 9467 /*bit_valid*/ 0, 9468 /*bit*/ 0); 9469 ctl_done((union ctl_io *)ctsio); 9470 return (retval); 9471 break; /* NOTREACHED */ 9472 } 9473 9474 alloc_len = scsi_4btoul(cdb->length); 9475 /* 9476 * The initiator has to allocate at least 16 bytes for this request, 9477 * so he can at least get the header and the first LUN. Otherwise 9478 * we reject the request (per SPC-3 rev 14, section 6.21). 9479 */ 9480 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9481 sizeof(struct scsi_report_luns_lundata))) { 9482 ctl_set_invalid_field(ctsio, 9483 /*sks_valid*/ 1, 9484 /*command*/ 1, 9485 /*field*/ 6, 9486 /*bit_valid*/ 0, 9487 /*bit*/ 0); 9488 ctl_done((union ctl_io *)ctsio); 9489 return (retval); 9490 } 9491 9492 request_lun = (struct ctl_lun *) 9493 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9494 9495 lun_datalen = sizeof(*lun_data) + 9496 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9497 9498 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9499 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9500 ctsio->kern_sg_entries = 0; 9501 9502 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9503 9504 mtx_lock(&control_softc->ctl_lock); 9505 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9506 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9507 if (lun_id >= CTL_MAX_LUNS) 9508 continue; 9509 lun = control_softc->ctl_luns[lun_id]; 9510 if (lun == NULL) 9511 continue; 9512 9513 if (targ_lun_id <= 0xff) { 9514 /* 9515 * Peripheral addressing method, bus number 0. 9516 */ 9517 lun_data->luns[num_filled].lundata[0] = 9518 RPL_LUNDATA_ATYP_PERIPH; 9519 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9520 num_filled++; 9521 } else if (targ_lun_id <= 0x3fff) { 9522 /* 9523 * Flat addressing method. 9524 */ 9525 lun_data->luns[num_filled].lundata[0] = 9526 RPL_LUNDATA_ATYP_FLAT | 9527 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9528#ifdef OLDCTLHEADERS 9529 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9530 (targ_lun_id & SRLD_BUS_LUN_MASK); 9531#endif 9532 lun_data->luns[num_filled].lundata[1] = 9533#ifdef OLDCTLHEADERS 9534 targ_lun_id >> SRLD_BUS_LUN_BITS; 9535#endif 9536 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9537 num_filled++; 9538 } else { 9539 printf("ctl_report_luns: bogus LUN number %jd, " 9540 "skipping\n", (intmax_t)targ_lun_id); 9541 } 9542 /* 9543 * According to SPC-3, rev 14 section 6.21: 9544 * 9545 * "The execution of a REPORT LUNS command to any valid and 9546 * installed logical unit shall clear the REPORTED LUNS DATA 9547 * HAS CHANGED unit attention condition for all logical 9548 * units of that target with respect to the requesting 9549 * initiator. A valid and installed logical unit is one 9550 * having a PERIPHERAL QUALIFIER of 000b in the standard 9551 * INQUIRY data (see 6.4.2)." 9552 * 9553 * If request_lun is NULL, the LUN this report luns command 9554 * was issued to is either disabled or doesn't exist. In that 9555 * case, we shouldn't clear any pending lun change unit 9556 * attention. 9557 */ 9558 if (request_lun != NULL) { 9559 mtx_lock(&lun->lun_lock); 9560 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9561 mtx_unlock(&lun->lun_lock); 9562 } 9563 } 9564 mtx_unlock(&control_softc->ctl_lock); 9565 9566 /* 9567 * It's quite possible that we've returned fewer LUNs than we allocated 9568 * space for. Trim it. 9569 */ 9570 lun_datalen = sizeof(*lun_data) + 9571 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9572 9573 if (lun_datalen < alloc_len) { 9574 ctsio->residual = alloc_len - lun_datalen; 9575 ctsio->kern_data_len = lun_datalen; 9576 ctsio->kern_total_len = lun_datalen; 9577 } else { 9578 ctsio->residual = 0; 9579 ctsio->kern_data_len = alloc_len; 9580 ctsio->kern_total_len = alloc_len; 9581 } 9582 ctsio->kern_data_resid = 0; 9583 ctsio->kern_rel_offset = 0; 9584 ctsio->kern_sg_entries = 0; 9585 9586 /* 9587 * We set this to the actual data length, regardless of how much 9588 * space we actually have to return results. If the user looks at 9589 * this value, he'll know whether or not he allocated enough space 9590 * and reissue the command if necessary. We don't support well 9591 * known logical units, so if the user asks for that, return none. 9592 */ 9593 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9594 9595 /* 9596 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9597 * this request. 9598 */ 9599 ctsio->scsi_status = SCSI_STATUS_OK; 9600 9601 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9602 ctsio->be_move_done = ctl_config_move_done; 9603 ctl_datamove((union ctl_io *)ctsio); 9604 9605 return (retval); 9606} 9607 9608int 9609ctl_request_sense(struct ctl_scsiio *ctsio) 9610{ 9611 struct scsi_request_sense *cdb; 9612 struct scsi_sense_data *sense_ptr; 9613 struct ctl_lun *lun; 9614 uint32_t initidx; 9615 int have_error; 9616 scsi_sense_data_type sense_format; 9617 9618 cdb = (struct scsi_request_sense *)ctsio->cdb; 9619 9620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9621 9622 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9623 9624 /* 9625 * Determine which sense format the user wants. 9626 */ 9627 if (cdb->byte2 & SRS_DESC) 9628 sense_format = SSD_TYPE_DESC; 9629 else 9630 sense_format = SSD_TYPE_FIXED; 9631 9632 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9633 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9634 ctsio->kern_sg_entries = 0; 9635 9636 /* 9637 * struct scsi_sense_data, which is currently set to 256 bytes, is 9638 * larger than the largest allowed value for the length field in the 9639 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9640 */ 9641 ctsio->residual = 0; 9642 ctsio->kern_data_len = cdb->length; 9643 ctsio->kern_total_len = cdb->length; 9644 9645 ctsio->kern_data_resid = 0; 9646 ctsio->kern_rel_offset = 0; 9647 ctsio->kern_sg_entries = 0; 9648 9649 /* 9650 * If we don't have a LUN, we don't have any pending sense. 9651 */ 9652 if (lun == NULL) 9653 goto no_sense; 9654 9655 have_error = 0; 9656 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9657 /* 9658 * Check for pending sense, and then for pending unit attentions. 9659 * Pending sense gets returned first, then pending unit attentions. 9660 */ 9661 mtx_lock(&lun->lun_lock); 9662#ifdef CTL_WITH_CA 9663 if (ctl_is_set(lun->have_ca, initidx)) { 9664 scsi_sense_data_type stored_format; 9665 9666 /* 9667 * Check to see which sense format was used for the stored 9668 * sense data. 9669 */ 9670 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9671 9672 /* 9673 * If the user requested a different sense format than the 9674 * one we stored, then we need to convert it to the other 9675 * format. If we're going from descriptor to fixed format 9676 * sense data, we may lose things in translation, depending 9677 * on what options were used. 9678 * 9679 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9680 * for some reason we'll just copy it out as-is. 9681 */ 9682 if ((stored_format == SSD_TYPE_FIXED) 9683 && (sense_format == SSD_TYPE_DESC)) 9684 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9685 &lun->pending_sense[initidx], 9686 (struct scsi_sense_data_desc *)sense_ptr); 9687 else if ((stored_format == SSD_TYPE_DESC) 9688 && (sense_format == SSD_TYPE_FIXED)) 9689 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9690 &lun->pending_sense[initidx], 9691 (struct scsi_sense_data_fixed *)sense_ptr); 9692 else 9693 memcpy(sense_ptr, &lun->pending_sense[initidx], 9694 ctl_min(sizeof(*sense_ptr), 9695 sizeof(lun->pending_sense[initidx]))); 9696 9697 ctl_clear_mask(lun->have_ca, initidx); 9698 have_error = 1; 9699 } else 9700#endif 9701 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9702 ctl_ua_type ua_type; 9703 9704 ua_type = ctl_build_ua(lun->pending_ua[initidx], 9705 sense_ptr, sense_format); 9706 if (ua_type != CTL_UA_NONE) { 9707 have_error = 1; 9708 /* We're reporting this UA, so clear it */ 9709 lun->pending_ua[initidx] &= ~ua_type; 9710 } 9711 } 9712 mtx_unlock(&lun->lun_lock); 9713 9714 /* 9715 * We already have a pending error, return it. 9716 */ 9717 if (have_error != 0) { 9718 /* 9719 * We report the SCSI status as OK, since the status of the 9720 * request sense command itself is OK. 9721 */ 9722 ctsio->scsi_status = SCSI_STATUS_OK; 9723 9724 /* 9725 * We report 0 for the sense length, because we aren't doing 9726 * autosense in this case. We're reporting sense as 9727 * parameter data. 9728 */ 9729 ctsio->sense_len = 0; 9730 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9731 ctsio->be_move_done = ctl_config_move_done; 9732 ctl_datamove((union ctl_io *)ctsio); 9733 9734 return (CTL_RETVAL_COMPLETE); 9735 } 9736 9737no_sense: 9738 9739 /* 9740 * No sense information to report, so we report that everything is 9741 * okay. 9742 */ 9743 ctl_set_sense_data(sense_ptr, 9744 lun, 9745 sense_format, 9746 /*current_error*/ 1, 9747 /*sense_key*/ SSD_KEY_NO_SENSE, 9748 /*asc*/ 0x00, 9749 /*ascq*/ 0x00, 9750 SSD_ELEM_NONE); 9751 9752 ctsio->scsi_status = SCSI_STATUS_OK; 9753 9754 /* 9755 * We report 0 for the sense length, because we aren't doing 9756 * autosense in this case. We're reporting sense as parameter data. 9757 */ 9758 ctsio->sense_len = 0; 9759 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9760 ctsio->be_move_done = ctl_config_move_done; 9761 ctl_datamove((union ctl_io *)ctsio); 9762 9763 return (CTL_RETVAL_COMPLETE); 9764} 9765 9766int 9767ctl_tur(struct ctl_scsiio *ctsio) 9768{ 9769 struct ctl_lun *lun; 9770 9771 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9772 9773 CTL_DEBUG_PRINT(("ctl_tur\n")); 9774 9775 if (lun == NULL) 9776 return (EINVAL); 9777 9778 ctsio->scsi_status = SCSI_STATUS_OK; 9779 ctsio->io_hdr.status = CTL_SUCCESS; 9780 9781 ctl_done((union ctl_io *)ctsio); 9782 9783 return (CTL_RETVAL_COMPLETE); 9784} 9785 9786#ifdef notyet 9787static int 9788ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9789{ 9790 9791} 9792#endif 9793 9794static int 9795ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9796{ 9797 struct scsi_vpd_supported_pages *pages; 9798 int sup_page_size; 9799 struct ctl_lun *lun; 9800 9801 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9802 9803 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9804 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9805 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9806 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9807 ctsio->kern_sg_entries = 0; 9808 9809 if (sup_page_size < alloc_len) { 9810 ctsio->residual = alloc_len - sup_page_size; 9811 ctsio->kern_data_len = sup_page_size; 9812 ctsio->kern_total_len = sup_page_size; 9813 } else { 9814 ctsio->residual = 0; 9815 ctsio->kern_data_len = alloc_len; 9816 ctsio->kern_total_len = alloc_len; 9817 } 9818 ctsio->kern_data_resid = 0; 9819 ctsio->kern_rel_offset = 0; 9820 ctsio->kern_sg_entries = 0; 9821 9822 /* 9823 * The control device is always connected. The disk device, on the 9824 * other hand, may not be online all the time. Need to change this 9825 * to figure out whether the disk device is actually online or not. 9826 */ 9827 if (lun != NULL) 9828 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9829 lun->be_lun->lun_type; 9830 else 9831 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9832 9833 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9834 /* Supported VPD pages */ 9835 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9836 /* Serial Number */ 9837 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9838 /* Device Identification */ 9839 pages->page_list[2] = SVPD_DEVICE_ID; 9840 /* Mode Page Policy */ 9841 pages->page_list[3] = SVPD_MODE_PAGE_POLICY; 9842 /* SCSI Ports */ 9843 pages->page_list[4] = SVPD_SCSI_PORTS; 9844 /* Third-party Copy */ 9845 pages->page_list[5] = SVPD_SCSI_TPC; 9846 /* Block limits */ 9847 pages->page_list[6] = SVPD_BLOCK_LIMITS; 9848 /* Block Device Characteristics */ 9849 pages->page_list[7] = SVPD_BDC; 9850 /* Logical Block Provisioning */ 9851 pages->page_list[8] = SVPD_LBP; 9852 9853 ctsio->scsi_status = SCSI_STATUS_OK; 9854 9855 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9856 ctsio->be_move_done = ctl_config_move_done; 9857 ctl_datamove((union ctl_io *)ctsio); 9858 9859 return (CTL_RETVAL_COMPLETE); 9860} 9861 9862static int 9863ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9864{ 9865 struct scsi_vpd_unit_serial_number *sn_ptr; 9866 struct ctl_lun *lun; 9867 9868 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9869 9870 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9871 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9872 ctsio->kern_sg_entries = 0; 9873 9874 if (sizeof(*sn_ptr) < alloc_len) { 9875 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9876 ctsio->kern_data_len = sizeof(*sn_ptr); 9877 ctsio->kern_total_len = sizeof(*sn_ptr); 9878 } else { 9879 ctsio->residual = 0; 9880 ctsio->kern_data_len = alloc_len; 9881 ctsio->kern_total_len = alloc_len; 9882 } 9883 ctsio->kern_data_resid = 0; 9884 ctsio->kern_rel_offset = 0; 9885 ctsio->kern_sg_entries = 0; 9886 9887 /* 9888 * The control device is always connected. The disk device, on the 9889 * other hand, may not be online all the time. Need to change this 9890 * to figure out whether the disk device is actually online or not. 9891 */ 9892 if (lun != NULL) 9893 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9894 lun->be_lun->lun_type; 9895 else 9896 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9897 9898 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9899 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9900 /* 9901 * If we don't have a LUN, we just leave the serial number as 9902 * all spaces. 9903 */ 9904 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9905 if (lun != NULL) { 9906 strncpy((char *)sn_ptr->serial_num, 9907 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9908 } 9909 ctsio->scsi_status = SCSI_STATUS_OK; 9910 9911 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9912 ctsio->be_move_done = ctl_config_move_done; 9913 ctl_datamove((union ctl_io *)ctsio); 9914 9915 return (CTL_RETVAL_COMPLETE); 9916} 9917 9918 9919static int 9920ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9921{ 9922 struct scsi_vpd_mode_page_policy *mpp_ptr; 9923 struct ctl_lun *lun; 9924 int data_len; 9925 9926 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9927 9928 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9929 sizeof(struct scsi_vpd_mode_page_policy_descr); 9930 9931 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9932 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9933 ctsio->kern_sg_entries = 0; 9934 9935 if (data_len < alloc_len) { 9936 ctsio->residual = alloc_len - data_len; 9937 ctsio->kern_data_len = data_len; 9938 ctsio->kern_total_len = data_len; 9939 } else { 9940 ctsio->residual = 0; 9941 ctsio->kern_data_len = alloc_len; 9942 ctsio->kern_total_len = alloc_len; 9943 } 9944 ctsio->kern_data_resid = 0; 9945 ctsio->kern_rel_offset = 0; 9946 ctsio->kern_sg_entries = 0; 9947 9948 /* 9949 * The control device is always connected. The disk device, on the 9950 * other hand, may not be online all the time. 9951 */ 9952 if (lun != NULL) 9953 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9954 lun->be_lun->lun_type; 9955 else 9956 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9957 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9958 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9959 mpp_ptr->descr[0].page_code = 0x3f; 9960 mpp_ptr->descr[0].subpage_code = 0xff; 9961 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9962 9963 ctsio->scsi_status = SCSI_STATUS_OK; 9964 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9965 ctsio->be_move_done = ctl_config_move_done; 9966 ctl_datamove((union ctl_io *)ctsio); 9967 9968 return (CTL_RETVAL_COMPLETE); 9969} 9970 9971static int 9972ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9973{ 9974 struct scsi_vpd_device_id *devid_ptr; 9975 struct scsi_vpd_id_descriptor *desc; 9976 struct ctl_softc *ctl_softc; 9977 struct ctl_lun *lun; 9978 struct ctl_port *port; 9979 int data_len; 9980 uint8_t proto; 9981 9982 ctl_softc = control_softc; 9983 9984 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9985 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9986 9987 data_len = sizeof(struct scsi_vpd_device_id) + 9988 sizeof(struct scsi_vpd_id_descriptor) + 9989 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9990 sizeof(struct scsi_vpd_id_descriptor) + 9991 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9992 if (lun && lun->lun_devid) 9993 data_len += lun->lun_devid->len; 9994 if (port->port_devid) 9995 data_len += port->port_devid->len; 9996 if (port->target_devid) 9997 data_len += port->target_devid->len; 9998 9999 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10000 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10001 ctsio->kern_sg_entries = 0; 10002 10003 if (data_len < alloc_len) { 10004 ctsio->residual = alloc_len - data_len; 10005 ctsio->kern_data_len = data_len; 10006 ctsio->kern_total_len = data_len; 10007 } else { 10008 ctsio->residual = 0; 10009 ctsio->kern_data_len = alloc_len; 10010 ctsio->kern_total_len = alloc_len; 10011 } 10012 ctsio->kern_data_resid = 0; 10013 ctsio->kern_rel_offset = 0; 10014 ctsio->kern_sg_entries = 0; 10015 10016 /* 10017 * The control device is always connected. The disk device, on the 10018 * other hand, may not be online all the time. 10019 */ 10020 if (lun != NULL) 10021 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10022 lun->be_lun->lun_type; 10023 else 10024 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10025 devid_ptr->page_code = SVPD_DEVICE_ID; 10026 scsi_ulto2b(data_len - 4, devid_ptr->length); 10027 10028 if (port->port_type == CTL_PORT_FC) 10029 proto = SCSI_PROTO_FC << 4; 10030 else if (port->port_type == CTL_PORT_ISCSI) 10031 proto = SCSI_PROTO_ISCSI << 4; 10032 else 10033 proto = SCSI_PROTO_SPI << 4; 10034 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10035 10036 /* 10037 * We're using a LUN association here. i.e., this device ID is a 10038 * per-LUN identifier. 10039 */ 10040 if (lun && lun->lun_devid) { 10041 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10042 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10043 lun->lun_devid->len); 10044 } 10045 10046 /* 10047 * This is for the WWPN which is a port association. 10048 */ 10049 if (port->port_devid) { 10050 memcpy(desc, port->port_devid->data, port->port_devid->len); 10051 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10052 port->port_devid->len); 10053 } 10054 10055 /* 10056 * This is for the Relative Target Port(type 4h) identifier 10057 */ 10058 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10059 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10060 SVPD_ID_TYPE_RELTARG; 10061 desc->length = 4; 10062 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10063 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10064 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10065 10066 /* 10067 * This is for the Target Port Group(type 5h) identifier 10068 */ 10069 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10070 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10071 SVPD_ID_TYPE_TPORTGRP; 10072 desc->length = 4; 10073 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10074 &desc->identifier[2]); 10075 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10076 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10077 10078 /* 10079 * This is for the Target identifier 10080 */ 10081 if (port->target_devid) { 10082 memcpy(desc, port->target_devid->data, port->target_devid->len); 10083 } 10084 10085 ctsio->scsi_status = SCSI_STATUS_OK; 10086 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10087 ctsio->be_move_done = ctl_config_move_done; 10088 ctl_datamove((union ctl_io *)ctsio); 10089 10090 return (CTL_RETVAL_COMPLETE); 10091} 10092 10093static int 10094ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10095{ 10096 struct ctl_softc *softc = control_softc; 10097 struct scsi_vpd_scsi_ports *sp; 10098 struct scsi_vpd_port_designation *pd; 10099 struct scsi_vpd_port_designation_cont *pdc; 10100 struct ctl_lun *lun; 10101 struct ctl_port *port; 10102 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10103 int num_target_port_groups, single; 10104 10105 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10106 10107 single = ctl_is_single; 10108 if (single) 10109 num_target_port_groups = 1; 10110 else 10111 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10112 num_target_ports = 0; 10113 iid_len = 0; 10114 id_len = 0; 10115 mtx_lock(&softc->ctl_lock); 10116 STAILQ_FOREACH(port, &softc->port_list, links) { 10117 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10118 continue; 10119 if (lun != NULL && 10120 ctl_map_lun_back(port->targ_port, lun->lun) >= 10121 CTL_MAX_LUNS) 10122 continue; 10123 num_target_ports++; 10124 if (port->init_devid) 10125 iid_len += port->init_devid->len; 10126 if (port->port_devid) 10127 id_len += port->port_devid->len; 10128 } 10129 mtx_unlock(&softc->ctl_lock); 10130 10131 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10132 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10133 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10134 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10135 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10136 ctsio->kern_sg_entries = 0; 10137 10138 if (data_len < alloc_len) { 10139 ctsio->residual = alloc_len - data_len; 10140 ctsio->kern_data_len = data_len; 10141 ctsio->kern_total_len = data_len; 10142 } else { 10143 ctsio->residual = 0; 10144 ctsio->kern_data_len = alloc_len; 10145 ctsio->kern_total_len = alloc_len; 10146 } 10147 ctsio->kern_data_resid = 0; 10148 ctsio->kern_rel_offset = 0; 10149 ctsio->kern_sg_entries = 0; 10150 10151 /* 10152 * The control device is always connected. The disk device, on the 10153 * other hand, may not be online all the time. Need to change this 10154 * to figure out whether the disk device is actually online or not. 10155 */ 10156 if (lun != NULL) 10157 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10158 lun->be_lun->lun_type; 10159 else 10160 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10161 10162 sp->page_code = SVPD_SCSI_PORTS; 10163 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10164 sp->page_length); 10165 pd = &sp->design[0]; 10166 10167 mtx_lock(&softc->ctl_lock); 10168 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10169 pg = 0; 10170 else 10171 pg = 1; 10172 for (g = 0; g < num_target_port_groups; g++) { 10173 STAILQ_FOREACH(port, &softc->port_list, links) { 10174 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10175 continue; 10176 if (lun != NULL && 10177 ctl_map_lun_back(port->targ_port, lun->lun) >= 10178 CTL_MAX_LUNS) 10179 continue; 10180 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10181 scsi_ulto2b(p, pd->relative_port_id); 10182 if (port->init_devid && g == pg) { 10183 iid_len = port->init_devid->len; 10184 memcpy(pd->initiator_transportid, 10185 port->init_devid->data, port->init_devid->len); 10186 } else 10187 iid_len = 0; 10188 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10189 pdc = (struct scsi_vpd_port_designation_cont *) 10190 (&pd->initiator_transportid[iid_len]); 10191 if (port->port_devid && g == pg) { 10192 id_len = port->port_devid->len; 10193 memcpy(pdc->target_port_descriptors, 10194 port->port_devid->data, port->port_devid->len); 10195 } else 10196 id_len = 0; 10197 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10198 pd = (struct scsi_vpd_port_designation *) 10199 ((uint8_t *)pdc->target_port_descriptors + id_len); 10200 } 10201 } 10202 mtx_unlock(&softc->ctl_lock); 10203 10204 ctsio->scsi_status = SCSI_STATUS_OK; 10205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10206 ctsio->be_move_done = ctl_config_move_done; 10207 ctl_datamove((union ctl_io *)ctsio); 10208 10209 return (CTL_RETVAL_COMPLETE); 10210} 10211 10212static int 10213ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10214{ 10215 struct scsi_vpd_block_limits *bl_ptr; 10216 struct ctl_lun *lun; 10217 int bs; 10218 10219 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10220 10221 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10222 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10223 ctsio->kern_sg_entries = 0; 10224 10225 if (sizeof(*bl_ptr) < alloc_len) { 10226 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10227 ctsio->kern_data_len = sizeof(*bl_ptr); 10228 ctsio->kern_total_len = sizeof(*bl_ptr); 10229 } else { 10230 ctsio->residual = 0; 10231 ctsio->kern_data_len = alloc_len; 10232 ctsio->kern_total_len = alloc_len; 10233 } 10234 ctsio->kern_data_resid = 0; 10235 ctsio->kern_rel_offset = 0; 10236 ctsio->kern_sg_entries = 0; 10237 10238 /* 10239 * The control device is always connected. The disk device, on the 10240 * other hand, may not be online all the time. Need to change this 10241 * to figure out whether the disk device is actually online or not. 10242 */ 10243 if (lun != NULL) 10244 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10245 lun->be_lun->lun_type; 10246 else 10247 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10248 10249 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10250 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10251 bl_ptr->max_cmp_write_len = 0xff; 10252 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10253 if (lun != NULL) { 10254 bs = lun->be_lun->blocksize; 10255 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10256 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10257 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10258 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10259 if (lun->be_lun->pblockexp != 0) { 10260 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10261 bl_ptr->opt_unmap_grain); 10262 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10263 bl_ptr->unmap_grain_align); 10264 } 10265 } 10266 } 10267 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10268 10269 ctsio->scsi_status = SCSI_STATUS_OK; 10270 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10271 ctsio->be_move_done = ctl_config_move_done; 10272 ctl_datamove((union ctl_io *)ctsio); 10273 10274 return (CTL_RETVAL_COMPLETE); 10275} 10276 10277static int 10278ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10279{ 10280 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10281 struct ctl_lun *lun; 10282 10283 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10284 10285 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10286 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10287 ctsio->kern_sg_entries = 0; 10288 10289 if (sizeof(*bdc_ptr) < alloc_len) { 10290 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10291 ctsio->kern_data_len = sizeof(*bdc_ptr); 10292 ctsio->kern_total_len = sizeof(*bdc_ptr); 10293 } else { 10294 ctsio->residual = 0; 10295 ctsio->kern_data_len = alloc_len; 10296 ctsio->kern_total_len = alloc_len; 10297 } 10298 ctsio->kern_data_resid = 0; 10299 ctsio->kern_rel_offset = 0; 10300 ctsio->kern_sg_entries = 0; 10301 10302 /* 10303 * The control device is always connected. The disk device, on the 10304 * other hand, may not be online all the time. Need to change this 10305 * to figure out whether the disk device is actually online or not. 10306 */ 10307 if (lun != NULL) 10308 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10309 lun->be_lun->lun_type; 10310 else 10311 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10312 bdc_ptr->page_code = SVPD_BDC; 10313 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10314 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10315 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10316 10317 ctsio->scsi_status = SCSI_STATUS_OK; 10318 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10319 ctsio->be_move_done = ctl_config_move_done; 10320 ctl_datamove((union ctl_io *)ctsio); 10321 10322 return (CTL_RETVAL_COMPLETE); 10323} 10324 10325static int 10326ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10327{ 10328 struct scsi_vpd_logical_block_prov *lbp_ptr; 10329 struct ctl_lun *lun; 10330 10331 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10332 10333 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10334 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10335 ctsio->kern_sg_entries = 0; 10336 10337 if (sizeof(*lbp_ptr) < alloc_len) { 10338 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10339 ctsio->kern_data_len = sizeof(*lbp_ptr); 10340 ctsio->kern_total_len = sizeof(*lbp_ptr); 10341 } else { 10342 ctsio->residual = 0; 10343 ctsio->kern_data_len = alloc_len; 10344 ctsio->kern_total_len = alloc_len; 10345 } 10346 ctsio->kern_data_resid = 0; 10347 ctsio->kern_rel_offset = 0; 10348 ctsio->kern_sg_entries = 0; 10349 10350 /* 10351 * The control device is always connected. The disk device, on the 10352 * other hand, may not be online all the time. Need to change this 10353 * to figure out whether the disk device is actually online or not. 10354 */ 10355 if (lun != NULL) 10356 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10357 lun->be_lun->lun_type; 10358 else 10359 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10360 10361 lbp_ptr->page_code = SVPD_LBP; 10362 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10363 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10364 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10365 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10366 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10367 } 10368 10369 ctsio->scsi_status = SCSI_STATUS_OK; 10370 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10371 ctsio->be_move_done = ctl_config_move_done; 10372 ctl_datamove((union ctl_io *)ctsio); 10373 10374 return (CTL_RETVAL_COMPLETE); 10375} 10376 10377static int 10378ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10379{ 10380 struct scsi_inquiry *cdb; 10381 struct ctl_lun *lun; 10382 int alloc_len, retval; 10383 10384 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10385 cdb = (struct scsi_inquiry *)ctsio->cdb; 10386 10387 retval = CTL_RETVAL_COMPLETE; 10388 10389 alloc_len = scsi_2btoul(cdb->length); 10390 10391 switch (cdb->page_code) { 10392 case SVPD_SUPPORTED_PAGES: 10393 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10394 break; 10395 case SVPD_UNIT_SERIAL_NUMBER: 10396 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10397 break; 10398 case SVPD_DEVICE_ID: 10399 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10400 break; 10401 case SVPD_MODE_PAGE_POLICY: 10402 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10403 break; 10404 case SVPD_SCSI_PORTS: 10405 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10406 break; 10407 case SVPD_SCSI_TPC: 10408 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10409 break; 10410 case SVPD_BLOCK_LIMITS: 10411 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10412 break; 10413 case SVPD_BDC: 10414 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10415 break; 10416 case SVPD_LBP: 10417 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10418 break; 10419 default: 10420 ctl_set_invalid_field(ctsio, 10421 /*sks_valid*/ 1, 10422 /*command*/ 1, 10423 /*field*/ 2, 10424 /*bit_valid*/ 0, 10425 /*bit*/ 0); 10426 ctl_done((union ctl_io *)ctsio); 10427 retval = CTL_RETVAL_COMPLETE; 10428 break; 10429 } 10430 10431 return (retval); 10432} 10433 10434static int 10435ctl_inquiry_std(struct ctl_scsiio *ctsio) 10436{ 10437 struct scsi_inquiry_data *inq_ptr; 10438 struct scsi_inquiry *cdb; 10439 struct ctl_softc *ctl_softc; 10440 struct ctl_lun *lun; 10441 char *val; 10442 uint32_t alloc_len; 10443 ctl_port_type port_type; 10444 10445 ctl_softc = control_softc; 10446 10447 /* 10448 * Figure out whether we're talking to a Fibre Channel port or not. 10449 * We treat the ioctl front end, and any SCSI adapters, as packetized 10450 * SCSI front ends. 10451 */ 10452 port_type = ctl_softc->ctl_ports[ 10453 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10454 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10455 port_type = CTL_PORT_SCSI; 10456 10457 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10458 cdb = (struct scsi_inquiry *)ctsio->cdb; 10459 alloc_len = scsi_2btoul(cdb->length); 10460 10461 /* 10462 * We malloc the full inquiry data size here and fill it 10463 * in. If the user only asks for less, we'll give him 10464 * that much. 10465 */ 10466 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10467 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10468 ctsio->kern_sg_entries = 0; 10469 ctsio->kern_data_resid = 0; 10470 ctsio->kern_rel_offset = 0; 10471 10472 if (sizeof(*inq_ptr) < alloc_len) { 10473 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10474 ctsio->kern_data_len = sizeof(*inq_ptr); 10475 ctsio->kern_total_len = sizeof(*inq_ptr); 10476 } else { 10477 ctsio->residual = 0; 10478 ctsio->kern_data_len = alloc_len; 10479 ctsio->kern_total_len = alloc_len; 10480 } 10481 10482 /* 10483 * If we have a LUN configured, report it as connected. Otherwise, 10484 * report that it is offline or no device is supported, depending 10485 * on the value of inquiry_pq_no_lun. 10486 * 10487 * According to the spec (SPC-4 r34), the peripheral qualifier 10488 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10489 * 10490 * "A peripheral device having the specified peripheral device type 10491 * is not connected to this logical unit. However, the device 10492 * server is capable of supporting the specified peripheral device 10493 * type on this logical unit." 10494 * 10495 * According to the same spec, the peripheral qualifier 10496 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10497 * 10498 * "The device server is not capable of supporting a peripheral 10499 * device on this logical unit. For this peripheral qualifier the 10500 * peripheral device type shall be set to 1Fh. All other peripheral 10501 * device type values are reserved for this peripheral qualifier." 10502 * 10503 * Given the text, it would seem that we probably want to report that 10504 * the LUN is offline here. There is no LUN connected, but we can 10505 * support a LUN at the given LUN number. 10506 * 10507 * In the real world, though, it sounds like things are a little 10508 * different: 10509 * 10510 * - Linux, when presented with a LUN with the offline peripheral 10511 * qualifier, will create an sg driver instance for it. So when 10512 * you attach it to CTL, you wind up with a ton of sg driver 10513 * instances. (One for every LUN that Linux bothered to probe.) 10514 * Linux does this despite the fact that it issues a REPORT LUNs 10515 * to LUN 0 to get the inventory of supported LUNs. 10516 * 10517 * - There is other anecdotal evidence (from Emulex folks) about 10518 * arrays that use the offline peripheral qualifier for LUNs that 10519 * are on the "passive" path in an active/passive array. 10520 * 10521 * So the solution is provide a hopefully reasonable default 10522 * (return bad/no LUN) and allow the user to change the behavior 10523 * with a tunable/sysctl variable. 10524 */ 10525 if (lun != NULL) 10526 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10527 lun->be_lun->lun_type; 10528 else if (ctl_softc->inquiry_pq_no_lun == 0) 10529 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10530 else 10531 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10532 10533 /* RMB in byte 2 is 0 */ 10534 inq_ptr->version = SCSI_REV_SPC4; 10535 10536 /* 10537 * According to SAM-3, even if a device only supports a single 10538 * level of LUN addressing, it should still set the HISUP bit: 10539 * 10540 * 4.9.1 Logical unit numbers overview 10541 * 10542 * All logical unit number formats described in this standard are 10543 * hierarchical in structure even when only a single level in that 10544 * hierarchy is used. The HISUP bit shall be set to one in the 10545 * standard INQUIRY data (see SPC-2) when any logical unit number 10546 * format described in this standard is used. Non-hierarchical 10547 * formats are outside the scope of this standard. 10548 * 10549 * Therefore we set the HiSup bit here. 10550 * 10551 * The reponse format is 2, per SPC-3. 10552 */ 10553 inq_ptr->response_format = SID_HiSup | 2; 10554 10555 inq_ptr->additional_length = 10556 offsetof(struct scsi_inquiry_data, vendor_specific1) - 10557 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10558 CTL_DEBUG_PRINT(("additional_length = %d\n", 10559 inq_ptr->additional_length)); 10560 10561 inq_ptr->spc3_flags = SPC3_SID_3PC; 10562 if (!ctl_is_single) 10563 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT; 10564 /* 16 bit addressing */ 10565 if (port_type == CTL_PORT_SCSI) 10566 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10567 /* XXX set the SID_MultiP bit here if we're actually going to 10568 respond on multiple ports */ 10569 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10570 10571 /* 16 bit data bus, synchronous transfers */ 10572 if (port_type == CTL_PORT_SCSI) 10573 inq_ptr->flags = SID_WBus16 | SID_Sync; 10574 /* 10575 * XXX KDM do we want to support tagged queueing on the control 10576 * device at all? 10577 */ 10578 if ((lun == NULL) 10579 || (lun->be_lun->lun_type != T_PROCESSOR)) 10580 inq_ptr->flags |= SID_CmdQue; 10581 /* 10582 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10583 * We have 8 bytes for the vendor name, and 16 bytes for the device 10584 * name and 4 bytes for the revision. 10585 */ 10586 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10587 "vendor")) == NULL) { 10588 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10589 } else { 10590 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10591 strncpy(inq_ptr->vendor, val, 10592 min(sizeof(inq_ptr->vendor), strlen(val))); 10593 } 10594 if (lun == NULL) { 10595 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10596 sizeof(inq_ptr->product)); 10597 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10598 switch (lun->be_lun->lun_type) { 10599 case T_DIRECT: 10600 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10601 sizeof(inq_ptr->product)); 10602 break; 10603 case T_PROCESSOR: 10604 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10605 sizeof(inq_ptr->product)); 10606 break; 10607 default: 10608 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10609 sizeof(inq_ptr->product)); 10610 break; 10611 } 10612 } else { 10613 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10614 strncpy(inq_ptr->product, val, 10615 min(sizeof(inq_ptr->product), strlen(val))); 10616 } 10617 10618 /* 10619 * XXX make this a macro somewhere so it automatically gets 10620 * incremented when we make changes. 10621 */ 10622 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10623 "revision")) == NULL) { 10624 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10625 } else { 10626 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10627 strncpy(inq_ptr->revision, val, 10628 min(sizeof(inq_ptr->revision), strlen(val))); 10629 } 10630 10631 /* 10632 * For parallel SCSI, we support double transition and single 10633 * transition clocking. We also support QAS (Quick Arbitration 10634 * and Selection) and Information Unit transfers on both the 10635 * control and array devices. 10636 */ 10637 if (port_type == CTL_PORT_SCSI) 10638 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10639 SID_SPI_IUS; 10640 10641 /* SAM-5 (no version claimed) */ 10642 scsi_ulto2b(0x00A0, inq_ptr->version1); 10643 /* SPC-4 (no version claimed) */ 10644 scsi_ulto2b(0x0460, inq_ptr->version2); 10645 if (port_type == CTL_PORT_FC) { 10646 /* FCP-2 ANSI INCITS.350:2003 */ 10647 scsi_ulto2b(0x0917, inq_ptr->version3); 10648 } else if (port_type == CTL_PORT_SCSI) { 10649 /* SPI-4 ANSI INCITS.362:200x */ 10650 scsi_ulto2b(0x0B56, inq_ptr->version3); 10651 } else if (port_type == CTL_PORT_ISCSI) { 10652 /* iSCSI (no version claimed) */ 10653 scsi_ulto2b(0x0960, inq_ptr->version3); 10654 } else if (port_type == CTL_PORT_SAS) { 10655 /* SAS (no version claimed) */ 10656 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10657 } 10658 10659 if (lun == NULL) { 10660 /* SBC-3 (no version claimed) */ 10661 scsi_ulto2b(0x04C0, inq_ptr->version4); 10662 } else { 10663 switch (lun->be_lun->lun_type) { 10664 case T_DIRECT: 10665 /* SBC-3 (no version claimed) */ 10666 scsi_ulto2b(0x04C0, inq_ptr->version4); 10667 break; 10668 case T_PROCESSOR: 10669 default: 10670 break; 10671 } 10672 } 10673 10674 ctsio->scsi_status = SCSI_STATUS_OK; 10675 if (ctsio->kern_data_len > 0) { 10676 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10677 ctsio->be_move_done = ctl_config_move_done; 10678 ctl_datamove((union ctl_io *)ctsio); 10679 } else { 10680 ctsio->io_hdr.status = CTL_SUCCESS; 10681 ctl_done((union ctl_io *)ctsio); 10682 } 10683 10684 return (CTL_RETVAL_COMPLETE); 10685} 10686 10687int 10688ctl_inquiry(struct ctl_scsiio *ctsio) 10689{ 10690 struct scsi_inquiry *cdb; 10691 int retval; 10692 10693 cdb = (struct scsi_inquiry *)ctsio->cdb; 10694 10695 retval = 0; 10696 10697 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10698 10699 /* 10700 * Right now, we don't support the CmdDt inquiry information. 10701 * This would be nice to support in the future. When we do 10702 * support it, we should change this test so that it checks to make 10703 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10704 */ 10705#ifdef notyet 10706 if (((cdb->byte2 & SI_EVPD) 10707 && (cdb->byte2 & SI_CMDDT))) 10708#endif 10709 if (cdb->byte2 & SI_CMDDT) { 10710 /* 10711 * Point to the SI_CMDDT bit. We might change this 10712 * when we support SI_CMDDT, but since both bits would be 10713 * "wrong", this should probably just stay as-is then. 10714 */ 10715 ctl_set_invalid_field(ctsio, 10716 /*sks_valid*/ 1, 10717 /*command*/ 1, 10718 /*field*/ 1, 10719 /*bit_valid*/ 1, 10720 /*bit*/ 1); 10721 ctl_done((union ctl_io *)ctsio); 10722 return (CTL_RETVAL_COMPLETE); 10723 } 10724 if (cdb->byte2 & SI_EVPD) 10725 retval = ctl_inquiry_evpd(ctsio); 10726#ifdef notyet 10727 else if (cdb->byte2 & SI_CMDDT) 10728 retval = ctl_inquiry_cmddt(ctsio); 10729#endif 10730 else 10731 retval = ctl_inquiry_std(ctsio); 10732 10733 return (retval); 10734} 10735 10736/* 10737 * For known CDB types, parse the LBA and length. 10738 */ 10739static int 10740ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10741{ 10742 if (io->io_hdr.io_type != CTL_IO_SCSI) 10743 return (1); 10744 10745 switch (io->scsiio.cdb[0]) { 10746 case COMPARE_AND_WRITE: { 10747 struct scsi_compare_and_write *cdb; 10748 10749 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10750 10751 *lba = scsi_8btou64(cdb->addr); 10752 *len = cdb->length; 10753 break; 10754 } 10755 case READ_6: 10756 case WRITE_6: { 10757 struct scsi_rw_6 *cdb; 10758 10759 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10760 10761 *lba = scsi_3btoul(cdb->addr); 10762 /* only 5 bits are valid in the most significant address byte */ 10763 *lba &= 0x1fffff; 10764 *len = cdb->length; 10765 break; 10766 } 10767 case READ_10: 10768 case WRITE_10: { 10769 struct scsi_rw_10 *cdb; 10770 10771 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10772 10773 *lba = scsi_4btoul(cdb->addr); 10774 *len = scsi_2btoul(cdb->length); 10775 break; 10776 } 10777 case WRITE_VERIFY_10: { 10778 struct scsi_write_verify_10 *cdb; 10779 10780 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10781 10782 *lba = scsi_4btoul(cdb->addr); 10783 *len = scsi_2btoul(cdb->length); 10784 break; 10785 } 10786 case READ_12: 10787 case WRITE_12: { 10788 struct scsi_rw_12 *cdb; 10789 10790 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10791 10792 *lba = scsi_4btoul(cdb->addr); 10793 *len = scsi_4btoul(cdb->length); 10794 break; 10795 } 10796 case WRITE_VERIFY_12: { 10797 struct scsi_write_verify_12 *cdb; 10798 10799 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10800 10801 *lba = scsi_4btoul(cdb->addr); 10802 *len = scsi_4btoul(cdb->length); 10803 break; 10804 } 10805 case READ_16: 10806 case WRITE_16: { 10807 struct scsi_rw_16 *cdb; 10808 10809 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10810 10811 *lba = scsi_8btou64(cdb->addr); 10812 *len = scsi_4btoul(cdb->length); 10813 break; 10814 } 10815 case WRITE_VERIFY_16: { 10816 struct scsi_write_verify_16 *cdb; 10817 10818 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10819 10820 10821 *lba = scsi_8btou64(cdb->addr); 10822 *len = scsi_4btoul(cdb->length); 10823 break; 10824 } 10825 case WRITE_SAME_10: { 10826 struct scsi_write_same_10 *cdb; 10827 10828 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10829 10830 *lba = scsi_4btoul(cdb->addr); 10831 *len = scsi_2btoul(cdb->length); 10832 break; 10833 } 10834 case WRITE_SAME_16: { 10835 struct scsi_write_same_16 *cdb; 10836 10837 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10838 10839 *lba = scsi_8btou64(cdb->addr); 10840 *len = scsi_4btoul(cdb->length); 10841 break; 10842 } 10843 case VERIFY_10: { 10844 struct scsi_verify_10 *cdb; 10845 10846 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10847 10848 *lba = scsi_4btoul(cdb->addr); 10849 *len = scsi_2btoul(cdb->length); 10850 break; 10851 } 10852 case VERIFY_12: { 10853 struct scsi_verify_12 *cdb; 10854 10855 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10856 10857 *lba = scsi_4btoul(cdb->addr); 10858 *len = scsi_4btoul(cdb->length); 10859 break; 10860 } 10861 case VERIFY_16: { 10862 struct scsi_verify_16 *cdb; 10863 10864 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10865 10866 *lba = scsi_8btou64(cdb->addr); 10867 *len = scsi_4btoul(cdb->length); 10868 break; 10869 } 10870 default: 10871 return (1); 10872 break; /* NOTREACHED */ 10873 } 10874 10875 return (0); 10876} 10877 10878static ctl_action 10879ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10880{ 10881 uint64_t endlba1, endlba2; 10882 10883 endlba1 = lba1 + len1 - 1; 10884 endlba2 = lba2 + len2 - 1; 10885 10886 if ((endlba1 < lba2) 10887 || (endlba2 < lba1)) 10888 return (CTL_ACTION_PASS); 10889 else 10890 return (CTL_ACTION_BLOCK); 10891} 10892 10893static ctl_action 10894ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10895{ 10896 uint64_t lba1, lba2; 10897 uint32_t len1, len2; 10898 int retval; 10899 10900 retval = ctl_get_lba_len(io1, &lba1, &len1); 10901 if (retval != 0) 10902 return (CTL_ACTION_ERROR); 10903 10904 retval = ctl_get_lba_len(io2, &lba2, &len2); 10905 if (retval != 0) 10906 return (CTL_ACTION_ERROR); 10907 10908 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10909} 10910 10911static ctl_action 10912ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10913{ 10914 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10915 ctl_serialize_action *serialize_row; 10916 10917 /* 10918 * The initiator attempted multiple untagged commands at the same 10919 * time. Can't do that. 10920 */ 10921 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10922 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10923 && ((pending_io->io_hdr.nexus.targ_port == 10924 ooa_io->io_hdr.nexus.targ_port) 10925 && (pending_io->io_hdr.nexus.initid.id == 10926 ooa_io->io_hdr.nexus.initid.id)) 10927 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10928 return (CTL_ACTION_OVERLAP); 10929 10930 /* 10931 * The initiator attempted to send multiple tagged commands with 10932 * the same ID. (It's fine if different initiators have the same 10933 * tag ID.) 10934 * 10935 * Even if all of those conditions are true, we don't kill the I/O 10936 * if the command ahead of us has been aborted. We won't end up 10937 * sending it to the FETD, and it's perfectly legal to resend a 10938 * command with the same tag number as long as the previous 10939 * instance of this tag number has been aborted somehow. 10940 */ 10941 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10942 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10943 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10944 && ((pending_io->io_hdr.nexus.targ_port == 10945 ooa_io->io_hdr.nexus.targ_port) 10946 && (pending_io->io_hdr.nexus.initid.id == 10947 ooa_io->io_hdr.nexus.initid.id)) 10948 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10949 return (CTL_ACTION_OVERLAP_TAG); 10950 10951 /* 10952 * If we get a head of queue tag, SAM-3 says that we should 10953 * immediately execute it. 10954 * 10955 * What happens if this command would normally block for some other 10956 * reason? e.g. a request sense with a head of queue tag 10957 * immediately after a write. Normally that would block, but this 10958 * will result in its getting executed immediately... 10959 * 10960 * We currently return "pass" instead of "skip", so we'll end up 10961 * going through the rest of the queue to check for overlapped tags. 10962 * 10963 * XXX KDM check for other types of blockage first?? 10964 */ 10965 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10966 return (CTL_ACTION_PASS); 10967 10968 /* 10969 * Ordered tags have to block until all items ahead of them 10970 * have completed. If we get called with an ordered tag, we always 10971 * block, if something else is ahead of us in the queue. 10972 */ 10973 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10974 return (CTL_ACTION_BLOCK); 10975 10976 /* 10977 * Simple tags get blocked until all head of queue and ordered tags 10978 * ahead of them have completed. I'm lumping untagged commands in 10979 * with simple tags here. XXX KDM is that the right thing to do? 10980 */ 10981 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10982 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10983 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10984 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10985 return (CTL_ACTION_BLOCK); 10986 10987 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10988 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10989 10990 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10991 10992 switch (serialize_row[pending_entry->seridx]) { 10993 case CTL_SER_BLOCK: 10994 return (CTL_ACTION_BLOCK); 10995 break; /* NOTREACHED */ 10996 case CTL_SER_EXTENT: 10997 return (ctl_extent_check(pending_io, ooa_io)); 10998 break; /* NOTREACHED */ 10999 case CTL_SER_PASS: 11000 return (CTL_ACTION_PASS); 11001 break; /* NOTREACHED */ 11002 case CTL_SER_SKIP: 11003 return (CTL_ACTION_SKIP); 11004 break; 11005 default: 11006 panic("invalid serialization value %d", 11007 serialize_row[pending_entry->seridx]); 11008 break; /* NOTREACHED */ 11009 } 11010 11011 return (CTL_ACTION_ERROR); 11012} 11013 11014/* 11015 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11016 * Assumptions: 11017 * - pending_io is generally either incoming, or on the blocked queue 11018 * - starting I/O is the I/O we want to start the check with. 11019 */ 11020static ctl_action 11021ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11022 union ctl_io *starting_io) 11023{ 11024 union ctl_io *ooa_io; 11025 ctl_action action; 11026 11027 mtx_assert(&lun->lun_lock, MA_OWNED); 11028 11029 /* 11030 * Run back along the OOA queue, starting with the current 11031 * blocked I/O and going through every I/O before it on the 11032 * queue. If starting_io is NULL, we'll just end up returning 11033 * CTL_ACTION_PASS. 11034 */ 11035 for (ooa_io = starting_io; ooa_io != NULL; 11036 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11037 ooa_links)){ 11038 11039 /* 11040 * This routine just checks to see whether 11041 * cur_blocked is blocked by ooa_io, which is ahead 11042 * of it in the queue. It doesn't queue/dequeue 11043 * cur_blocked. 11044 */ 11045 action = ctl_check_for_blockage(pending_io, ooa_io); 11046 switch (action) { 11047 case CTL_ACTION_BLOCK: 11048 case CTL_ACTION_OVERLAP: 11049 case CTL_ACTION_OVERLAP_TAG: 11050 case CTL_ACTION_SKIP: 11051 case CTL_ACTION_ERROR: 11052 return (action); 11053 break; /* NOTREACHED */ 11054 case CTL_ACTION_PASS: 11055 break; 11056 default: 11057 panic("invalid action %d", action); 11058 break; /* NOTREACHED */ 11059 } 11060 } 11061 11062 return (CTL_ACTION_PASS); 11063} 11064 11065/* 11066 * Assumptions: 11067 * - An I/O has just completed, and has been removed from the per-LUN OOA 11068 * queue, so some items on the blocked queue may now be unblocked. 11069 */ 11070static int 11071ctl_check_blocked(struct ctl_lun *lun) 11072{ 11073 union ctl_io *cur_blocked, *next_blocked; 11074 11075 mtx_assert(&lun->lun_lock, MA_OWNED); 11076 11077 /* 11078 * Run forward from the head of the blocked queue, checking each 11079 * entry against the I/Os prior to it on the OOA queue to see if 11080 * there is still any blockage. 11081 * 11082 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11083 * with our removing a variable on it while it is traversing the 11084 * list. 11085 */ 11086 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11087 cur_blocked != NULL; cur_blocked = next_blocked) { 11088 union ctl_io *prev_ooa; 11089 ctl_action action; 11090 11091 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11092 blocked_links); 11093 11094 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11095 ctl_ooaq, ooa_links); 11096 11097 /* 11098 * If cur_blocked happens to be the first item in the OOA 11099 * queue now, prev_ooa will be NULL, and the action 11100 * returned will just be CTL_ACTION_PASS. 11101 */ 11102 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11103 11104 switch (action) { 11105 case CTL_ACTION_BLOCK: 11106 /* Nothing to do here, still blocked */ 11107 break; 11108 case CTL_ACTION_OVERLAP: 11109 case CTL_ACTION_OVERLAP_TAG: 11110 /* 11111 * This shouldn't happen! In theory we've already 11112 * checked this command for overlap... 11113 */ 11114 break; 11115 case CTL_ACTION_PASS: 11116 case CTL_ACTION_SKIP: { 11117 struct ctl_softc *softc; 11118 const struct ctl_cmd_entry *entry; 11119 uint32_t initidx; 11120 int isc_retval; 11121 11122 /* 11123 * The skip case shouldn't happen, this transaction 11124 * should have never made it onto the blocked queue. 11125 */ 11126 /* 11127 * This I/O is no longer blocked, we can remove it 11128 * from the blocked queue. Since this is a TAILQ 11129 * (doubly linked list), we can do O(1) removals 11130 * from any place on the list. 11131 */ 11132 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11133 blocked_links); 11134 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11135 11136 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11137 /* 11138 * Need to send IO back to original side to 11139 * run 11140 */ 11141 union ctl_ha_msg msg_info; 11142 11143 msg_info.hdr.original_sc = 11144 cur_blocked->io_hdr.original_sc; 11145 msg_info.hdr.serializing_sc = cur_blocked; 11146 msg_info.hdr.msg_type = CTL_MSG_R2R; 11147 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11148 &msg_info, sizeof(msg_info), 0)) > 11149 CTL_HA_STATUS_SUCCESS) { 11150 printf("CTL:Check Blocked error from " 11151 "ctl_ha_msg_send %d\n", 11152 isc_retval); 11153 } 11154 break; 11155 } 11156 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 11157 softc = control_softc; 11158 11159 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11160 11161 /* 11162 * Check this I/O for LUN state changes that may 11163 * have happened while this command was blocked. 11164 * The LUN state may have been changed by a command 11165 * ahead of us in the queue, so we need to re-check 11166 * for any states that can be caused by SCSI 11167 * commands. 11168 */ 11169 if (ctl_scsiio_lun_check(softc, lun, entry, 11170 &cur_blocked->scsiio) == 0) { 11171 cur_blocked->io_hdr.flags |= 11172 CTL_FLAG_IS_WAS_ON_RTR; 11173 ctl_enqueue_rtr(cur_blocked); 11174 } else 11175 ctl_done(cur_blocked); 11176 break; 11177 } 11178 default: 11179 /* 11180 * This probably shouldn't happen -- we shouldn't 11181 * get CTL_ACTION_ERROR, or anything else. 11182 */ 11183 break; 11184 } 11185 } 11186 11187 return (CTL_RETVAL_COMPLETE); 11188} 11189 11190/* 11191 * This routine (with one exception) checks LUN flags that can be set by 11192 * commands ahead of us in the OOA queue. These flags have to be checked 11193 * when a command initially comes in, and when we pull a command off the 11194 * blocked queue and are preparing to execute it. The reason we have to 11195 * check these flags for commands on the blocked queue is that the LUN 11196 * state may have been changed by a command ahead of us while we're on the 11197 * blocked queue. 11198 * 11199 * Ordering is somewhat important with these checks, so please pay 11200 * careful attention to the placement of any new checks. 11201 */ 11202static int 11203ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11204 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11205{ 11206 int retval; 11207 11208 retval = 0; 11209 11210 mtx_assert(&lun->lun_lock, MA_OWNED); 11211 11212 /* 11213 * If this shelf is a secondary shelf controller, we have to reject 11214 * any media access commands. 11215 */ 11216#if 0 11217 /* No longer needed for HA */ 11218 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11219 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11220 ctl_set_lun_standby(ctsio); 11221 retval = 1; 11222 goto bailout; 11223 } 11224#endif 11225 11226 /* 11227 * Check for a reservation conflict. If this command isn't allowed 11228 * even on reserved LUNs, and if this initiator isn't the one who 11229 * reserved us, reject the command with a reservation conflict. 11230 */ 11231 if ((lun->flags & CTL_LUN_RESERVED) 11232 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11233 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11234 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11235 || (ctsio->io_hdr.nexus.targ_target.id != 11236 lun->rsv_nexus.targ_target.id)) { 11237 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11238 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11239 retval = 1; 11240 goto bailout; 11241 } 11242 } 11243 11244 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11245 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11246 uint32_t residx; 11247 11248 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11249 /* 11250 * if we aren't registered or it's a res holder type 11251 * reservation and this isn't the res holder then set a 11252 * conflict. 11253 * NOTE: Commands which might be allowed on write exclusive 11254 * type reservations are checked in the particular command 11255 * for a conflict. Read and SSU are the only ones. 11256 */ 11257 if (!lun->per_res[residx].registered 11258 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11259 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11260 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11261 retval = 1; 11262 goto bailout; 11263 } 11264 11265 } 11266 11267 if ((lun->flags & CTL_LUN_OFFLINE) 11268 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11269 ctl_set_lun_not_ready(ctsio); 11270 retval = 1; 11271 goto bailout; 11272 } 11273 11274 /* 11275 * If the LUN is stopped, see if this particular command is allowed 11276 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11277 */ 11278 if ((lun->flags & CTL_LUN_STOPPED) 11279 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11280 /* "Logical unit not ready, initializing cmd. required" */ 11281 ctl_set_lun_stopped(ctsio); 11282 retval = 1; 11283 goto bailout; 11284 } 11285 11286 if ((lun->flags & CTL_LUN_INOPERABLE) 11287 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11288 /* "Medium format corrupted" */ 11289 ctl_set_medium_format_corrupted(ctsio); 11290 retval = 1; 11291 goto bailout; 11292 } 11293 11294bailout: 11295 return (retval); 11296 11297} 11298 11299static void 11300ctl_failover_io(union ctl_io *io, int have_lock) 11301{ 11302 ctl_set_busy(&io->scsiio); 11303 ctl_done(io); 11304} 11305 11306static void 11307ctl_failover(void) 11308{ 11309 struct ctl_lun *lun; 11310 struct ctl_softc *ctl_softc; 11311 union ctl_io *next_io, *pending_io; 11312 union ctl_io *io; 11313 int lun_idx; 11314 int i; 11315 11316 ctl_softc = control_softc; 11317 11318 mtx_lock(&ctl_softc->ctl_lock); 11319 /* 11320 * Remove any cmds from the other SC from the rtr queue. These 11321 * will obviously only be for LUNs for which we're the primary. 11322 * We can't send status or get/send data for these commands. 11323 * Since they haven't been executed yet, we can just remove them. 11324 * We'll either abort them or delete them below, depending on 11325 * which HA mode we're in. 11326 */ 11327#ifdef notyet 11328 mtx_lock(&ctl_softc->queue_lock); 11329 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11330 io != NULL; io = next_io) { 11331 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11332 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11333 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11334 ctl_io_hdr, links); 11335 } 11336 mtx_unlock(&ctl_softc->queue_lock); 11337#endif 11338 11339 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11340 lun = ctl_softc->ctl_luns[lun_idx]; 11341 if (lun==NULL) 11342 continue; 11343 11344 /* 11345 * Processor LUNs are primary on both sides. 11346 * XXX will this always be true? 11347 */ 11348 if (lun->be_lun->lun_type == T_PROCESSOR) 11349 continue; 11350 11351 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11352 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11353 printf("FAILOVER: primary lun %d\n", lun_idx); 11354 /* 11355 * Remove all commands from the other SC. First from the 11356 * blocked queue then from the ooa queue. Once we have 11357 * removed them. Call ctl_check_blocked to see if there 11358 * is anything that can run. 11359 */ 11360 for (io = (union ctl_io *)TAILQ_FIRST( 11361 &lun->blocked_queue); io != NULL; io = next_io) { 11362 11363 next_io = (union ctl_io *)TAILQ_NEXT( 11364 &io->io_hdr, blocked_links); 11365 11366 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11367 TAILQ_REMOVE(&lun->blocked_queue, 11368 &io->io_hdr,blocked_links); 11369 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11370 TAILQ_REMOVE(&lun->ooa_queue, 11371 &io->io_hdr, ooa_links); 11372 11373 ctl_free_io(io); 11374 } 11375 } 11376 11377 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11378 io != NULL; io = next_io) { 11379 11380 next_io = (union ctl_io *)TAILQ_NEXT( 11381 &io->io_hdr, ooa_links); 11382 11383 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11384 11385 TAILQ_REMOVE(&lun->ooa_queue, 11386 &io->io_hdr, 11387 ooa_links); 11388 11389 ctl_free_io(io); 11390 } 11391 } 11392 ctl_check_blocked(lun); 11393 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11394 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11395 11396 printf("FAILOVER: primary lun %d\n", lun_idx); 11397 /* 11398 * Abort all commands from the other SC. We can't 11399 * send status back for them now. These should get 11400 * cleaned up when they are completed or come out 11401 * for a datamove operation. 11402 */ 11403 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11404 io != NULL; io = next_io) { 11405 next_io = (union ctl_io *)TAILQ_NEXT( 11406 &io->io_hdr, ooa_links); 11407 11408 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11409 io->io_hdr.flags |= CTL_FLAG_ABORT; 11410 } 11411 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11412 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11413 11414 printf("FAILOVER: secondary lun %d\n", lun_idx); 11415 11416 lun->flags |= CTL_LUN_PRIMARY_SC; 11417 11418 /* 11419 * We send all I/O that was sent to this controller 11420 * and redirected to the other side back with 11421 * busy status, and have the initiator retry it. 11422 * Figuring out how much data has been transferred, 11423 * etc. and picking up where we left off would be 11424 * very tricky. 11425 * 11426 * XXX KDM need to remove I/O from the blocked 11427 * queue as well! 11428 */ 11429 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11430 &lun->ooa_queue); pending_io != NULL; 11431 pending_io = next_io) { 11432 11433 next_io = (union ctl_io *)TAILQ_NEXT( 11434 &pending_io->io_hdr, ooa_links); 11435 11436 pending_io->io_hdr.flags &= 11437 ~CTL_FLAG_SENT_2OTHER_SC; 11438 11439 if (pending_io->io_hdr.flags & 11440 CTL_FLAG_IO_ACTIVE) { 11441 pending_io->io_hdr.flags |= 11442 CTL_FLAG_FAILOVER; 11443 } else { 11444 ctl_set_busy(&pending_io->scsiio); 11445 ctl_done(pending_io); 11446 } 11447 } 11448 11449 /* 11450 * Build Unit Attention 11451 */ 11452 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11453 lun->pending_ua[i] |= 11454 CTL_UA_ASYM_ACC_CHANGE; 11455 } 11456 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11457 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11458 printf("FAILOVER: secondary lun %d\n", lun_idx); 11459 /* 11460 * if the first io on the OOA is not on the RtR queue 11461 * add it. 11462 */ 11463 lun->flags |= CTL_LUN_PRIMARY_SC; 11464 11465 pending_io = (union ctl_io *)TAILQ_FIRST( 11466 &lun->ooa_queue); 11467 if (pending_io==NULL) { 11468 printf("Nothing on OOA queue\n"); 11469 continue; 11470 } 11471 11472 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11473 if ((pending_io->io_hdr.flags & 11474 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11475 pending_io->io_hdr.flags |= 11476 CTL_FLAG_IS_WAS_ON_RTR; 11477 ctl_enqueue_rtr(pending_io); 11478 } 11479#if 0 11480 else 11481 { 11482 printf("Tag 0x%04x is running\n", 11483 pending_io->scsiio.tag_num); 11484 } 11485#endif 11486 11487 next_io = (union ctl_io *)TAILQ_NEXT( 11488 &pending_io->io_hdr, ooa_links); 11489 for (pending_io=next_io; pending_io != NULL; 11490 pending_io = next_io) { 11491 pending_io->io_hdr.flags &= 11492 ~CTL_FLAG_SENT_2OTHER_SC; 11493 next_io = (union ctl_io *)TAILQ_NEXT( 11494 &pending_io->io_hdr, ooa_links); 11495 if (pending_io->io_hdr.flags & 11496 CTL_FLAG_IS_WAS_ON_RTR) { 11497#if 0 11498 printf("Tag 0x%04x is running\n", 11499 pending_io->scsiio.tag_num); 11500#endif 11501 continue; 11502 } 11503 11504 switch (ctl_check_ooa(lun, pending_io, 11505 (union ctl_io *)TAILQ_PREV( 11506 &pending_io->io_hdr, ctl_ooaq, 11507 ooa_links))) { 11508 11509 case CTL_ACTION_BLOCK: 11510 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11511 &pending_io->io_hdr, 11512 blocked_links); 11513 pending_io->io_hdr.flags |= 11514 CTL_FLAG_BLOCKED; 11515 break; 11516 case CTL_ACTION_PASS: 11517 case CTL_ACTION_SKIP: 11518 pending_io->io_hdr.flags |= 11519 CTL_FLAG_IS_WAS_ON_RTR; 11520 ctl_enqueue_rtr(pending_io); 11521 break; 11522 case CTL_ACTION_OVERLAP: 11523 ctl_set_overlapped_cmd( 11524 (struct ctl_scsiio *)pending_io); 11525 ctl_done(pending_io); 11526 break; 11527 case CTL_ACTION_OVERLAP_TAG: 11528 ctl_set_overlapped_tag( 11529 (struct ctl_scsiio *)pending_io, 11530 pending_io->scsiio.tag_num & 0xff); 11531 ctl_done(pending_io); 11532 break; 11533 case CTL_ACTION_ERROR: 11534 default: 11535 ctl_set_internal_failure( 11536 (struct ctl_scsiio *)pending_io, 11537 0, // sks_valid 11538 0); //retry count 11539 ctl_done(pending_io); 11540 break; 11541 } 11542 } 11543 11544 /* 11545 * Build Unit Attention 11546 */ 11547 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11548 lun->pending_ua[i] |= 11549 CTL_UA_ASYM_ACC_CHANGE; 11550 } 11551 } else { 11552 panic("Unhandled HA mode failover, LUN flags = %#x, " 11553 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11554 } 11555 } 11556 ctl_pause_rtr = 0; 11557 mtx_unlock(&ctl_softc->ctl_lock); 11558} 11559 11560static int 11561ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11562{ 11563 struct ctl_lun *lun; 11564 const struct ctl_cmd_entry *entry; 11565 uint32_t initidx, targ_lun; 11566 int retval; 11567 11568 retval = 0; 11569 11570 lun = NULL; 11571 11572 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11573 if ((targ_lun < CTL_MAX_LUNS) 11574 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11575 lun = ctl_softc->ctl_luns[targ_lun]; 11576 /* 11577 * If the LUN is invalid, pretend that it doesn't exist. 11578 * It will go away as soon as all pending I/O has been 11579 * completed. 11580 */ 11581 if (lun->flags & CTL_LUN_DISABLED) { 11582 lun = NULL; 11583 } else { 11584 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11585 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11586 lun->be_lun; 11587 if (lun->be_lun->lun_type == T_PROCESSOR) { 11588 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11589 } 11590 11591 /* 11592 * Every I/O goes into the OOA queue for a 11593 * particular LUN, and stays there until completion. 11594 */ 11595 mtx_lock(&lun->lun_lock); 11596 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11597 ooa_links); 11598 } 11599 } else { 11600 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11601 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11602 } 11603 11604 /* Get command entry and return error if it is unsuppotyed. */ 11605 entry = ctl_validate_command(ctsio); 11606 if (entry == NULL) { 11607 if (lun) 11608 mtx_unlock(&lun->lun_lock); 11609 return (retval); 11610 } 11611 11612 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11613 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11614 11615 /* 11616 * Check to see whether we can send this command to LUNs that don't 11617 * exist. This should pretty much only be the case for inquiry 11618 * and request sense. Further checks, below, really require having 11619 * a LUN, so we can't really check the command anymore. Just put 11620 * it on the rtr queue. 11621 */ 11622 if (lun == NULL) { 11623 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11624 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11625 ctl_enqueue_rtr((union ctl_io *)ctsio); 11626 return (retval); 11627 } 11628 11629 ctl_set_unsupported_lun(ctsio); 11630 ctl_done((union ctl_io *)ctsio); 11631 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11632 return (retval); 11633 } else { 11634 /* 11635 * Make sure we support this particular command on this LUN. 11636 * e.g., we don't support writes to the control LUN. 11637 */ 11638 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11639 mtx_unlock(&lun->lun_lock); 11640 ctl_set_invalid_opcode(ctsio); 11641 ctl_done((union ctl_io *)ctsio); 11642 return (retval); 11643 } 11644 } 11645 11646 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11647 11648#ifdef CTL_WITH_CA 11649 /* 11650 * If we've got a request sense, it'll clear the contingent 11651 * allegiance condition. Otherwise, if we have a CA condition for 11652 * this initiator, clear it, because it sent down a command other 11653 * than request sense. 11654 */ 11655 if ((ctsio->cdb[0] != REQUEST_SENSE) 11656 && (ctl_is_set(lun->have_ca, initidx))) 11657 ctl_clear_mask(lun->have_ca, initidx); 11658#endif 11659 11660 /* 11661 * If the command has this flag set, it handles its own unit 11662 * attention reporting, we shouldn't do anything. Otherwise we 11663 * check for any pending unit attentions, and send them back to the 11664 * initiator. We only do this when a command initially comes in, 11665 * not when we pull it off the blocked queue. 11666 * 11667 * According to SAM-3, section 5.3.2, the order that things get 11668 * presented back to the host is basically unit attentions caused 11669 * by some sort of reset event, busy status, reservation conflicts 11670 * or task set full, and finally any other status. 11671 * 11672 * One issue here is that some of the unit attentions we report 11673 * don't fall into the "reset" category (e.g. "reported luns data 11674 * has changed"). So reporting it here, before the reservation 11675 * check, may be technically wrong. I guess the only thing to do 11676 * would be to check for and report the reset events here, and then 11677 * check for the other unit attention types after we check for a 11678 * reservation conflict. 11679 * 11680 * XXX KDM need to fix this 11681 */ 11682 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11683 ctl_ua_type ua_type; 11684 11685 ua_type = lun->pending_ua[initidx]; 11686 if (ua_type != CTL_UA_NONE) { 11687 scsi_sense_data_type sense_format; 11688 11689 if (lun != NULL) 11690 sense_format = (lun->flags & 11691 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11692 SSD_TYPE_FIXED; 11693 else 11694 sense_format = SSD_TYPE_FIXED; 11695 11696 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11697 sense_format); 11698 if (ua_type != CTL_UA_NONE) { 11699 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11700 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11701 CTL_AUTOSENSE; 11702 ctsio->sense_len = SSD_FULL_SIZE; 11703 lun->pending_ua[initidx] &= ~ua_type; 11704 mtx_unlock(&lun->lun_lock); 11705 ctl_done((union ctl_io *)ctsio); 11706 return (retval); 11707 } 11708 } 11709 } 11710 11711 11712 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11713 mtx_unlock(&lun->lun_lock); 11714 ctl_done((union ctl_io *)ctsio); 11715 return (retval); 11716 } 11717 11718 /* 11719 * XXX CHD this is where we want to send IO to other side if 11720 * this LUN is secondary on this SC. We will need to make a copy 11721 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11722 * the copy we send as FROM_OTHER. 11723 * We also need to stuff the address of the original IO so we can 11724 * find it easily. Something similar will need be done on the other 11725 * side so when we are done we can find the copy. 11726 */ 11727 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11728 union ctl_ha_msg msg_info; 11729 int isc_retval; 11730 11731 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11732 11733 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11734 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11735#if 0 11736 printf("1. ctsio %p\n", ctsio); 11737#endif 11738 msg_info.hdr.serializing_sc = NULL; 11739 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11740 msg_info.scsi.tag_num = ctsio->tag_num; 11741 msg_info.scsi.tag_type = ctsio->tag_type; 11742 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11743 11744 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11745 11746 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11747 (void *)&msg_info, sizeof(msg_info), 0)) > 11748 CTL_HA_STATUS_SUCCESS) { 11749 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11750 isc_retval); 11751 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11752 } else { 11753#if 0 11754 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11755#endif 11756 } 11757 11758 /* 11759 * XXX KDM this I/O is off the incoming queue, but hasn't 11760 * been inserted on any other queue. We may need to come 11761 * up with a holding queue while we wait for serialization 11762 * so that we have an idea of what we're waiting for from 11763 * the other side. 11764 */ 11765 mtx_unlock(&lun->lun_lock); 11766 return (retval); 11767 } 11768 11769 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11770 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11771 ctl_ooaq, ooa_links))) { 11772 case CTL_ACTION_BLOCK: 11773 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11774 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11775 blocked_links); 11776 mtx_unlock(&lun->lun_lock); 11777 return (retval); 11778 case CTL_ACTION_PASS: 11779 case CTL_ACTION_SKIP: 11780 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11781 mtx_unlock(&lun->lun_lock); 11782 ctl_enqueue_rtr((union ctl_io *)ctsio); 11783 break; 11784 case CTL_ACTION_OVERLAP: 11785 mtx_unlock(&lun->lun_lock); 11786 ctl_set_overlapped_cmd(ctsio); 11787 ctl_done((union ctl_io *)ctsio); 11788 break; 11789 case CTL_ACTION_OVERLAP_TAG: 11790 mtx_unlock(&lun->lun_lock); 11791 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11792 ctl_done((union ctl_io *)ctsio); 11793 break; 11794 case CTL_ACTION_ERROR: 11795 default: 11796 mtx_unlock(&lun->lun_lock); 11797 ctl_set_internal_failure(ctsio, 11798 /*sks_valid*/ 0, 11799 /*retry_count*/ 0); 11800 ctl_done((union ctl_io *)ctsio); 11801 break; 11802 } 11803 return (retval); 11804} 11805 11806const struct ctl_cmd_entry * 11807ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11808{ 11809 const struct ctl_cmd_entry *entry; 11810 int service_action; 11811 11812 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11813 if (entry->flags & CTL_CMD_FLAG_SA5) { 11814 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11815 entry = &((const struct ctl_cmd_entry *) 11816 entry->execute)[service_action]; 11817 } 11818 return (entry); 11819} 11820 11821const struct ctl_cmd_entry * 11822ctl_validate_command(struct ctl_scsiio *ctsio) 11823{ 11824 const struct ctl_cmd_entry *entry; 11825 int i; 11826 uint8_t diff; 11827 11828 entry = ctl_get_cmd_entry(ctsio); 11829 if (entry->execute == NULL) { 11830 ctl_set_invalid_opcode(ctsio); 11831 ctl_done((union ctl_io *)ctsio); 11832 return (NULL); 11833 } 11834 KASSERT(entry->length > 0, 11835 ("Not defined length for command 0x%02x/0x%02x", 11836 ctsio->cdb[0], ctsio->cdb[1])); 11837 for (i = 1; i < entry->length; i++) { 11838 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11839 if (diff == 0) 11840 continue; 11841 ctl_set_invalid_field(ctsio, 11842 /*sks_valid*/ 1, 11843 /*command*/ 1, 11844 /*field*/ i, 11845 /*bit_valid*/ 1, 11846 /*bit*/ fls(diff) - 1); 11847 ctl_done((union ctl_io *)ctsio); 11848 return (NULL); 11849 } 11850 return (entry); 11851} 11852 11853static int 11854ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11855{ 11856 11857 switch (lun_type) { 11858 case T_PROCESSOR: 11859 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11860 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11861 return (0); 11862 break; 11863 case T_DIRECT: 11864 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11865 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11866 return (0); 11867 break; 11868 default: 11869 return (0); 11870 } 11871 return (1); 11872} 11873 11874static int 11875ctl_scsiio(struct ctl_scsiio *ctsio) 11876{ 11877 int retval; 11878 const struct ctl_cmd_entry *entry; 11879 11880 retval = CTL_RETVAL_COMPLETE; 11881 11882 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11883 11884 entry = ctl_get_cmd_entry(ctsio); 11885 11886 /* 11887 * If this I/O has been aborted, just send it straight to 11888 * ctl_done() without executing it. 11889 */ 11890 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11891 ctl_done((union ctl_io *)ctsio); 11892 goto bailout; 11893 } 11894 11895 /* 11896 * All the checks should have been handled by ctl_scsiio_precheck(). 11897 * We should be clear now to just execute the I/O. 11898 */ 11899 retval = entry->execute(ctsio); 11900 11901bailout: 11902 return (retval); 11903} 11904 11905/* 11906 * Since we only implement one target right now, a bus reset simply resets 11907 * our single target. 11908 */ 11909static int 11910ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11911{ 11912 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11913} 11914 11915static int 11916ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11917 ctl_ua_type ua_type) 11918{ 11919 struct ctl_lun *lun; 11920 int retval; 11921 11922 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11923 union ctl_ha_msg msg_info; 11924 11925 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11926 msg_info.hdr.nexus = io->io_hdr.nexus; 11927 if (ua_type==CTL_UA_TARG_RESET) 11928 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11929 else 11930 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11931 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11932 msg_info.hdr.original_sc = NULL; 11933 msg_info.hdr.serializing_sc = NULL; 11934 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11935 (void *)&msg_info, sizeof(msg_info), 0)) { 11936 } 11937 } 11938 retval = 0; 11939 11940 mtx_lock(&ctl_softc->ctl_lock); 11941 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11942 retval += ctl_lun_reset(lun, io, ua_type); 11943 mtx_unlock(&ctl_softc->ctl_lock); 11944 11945 return (retval); 11946} 11947 11948/* 11949 * The LUN should always be set. The I/O is optional, and is used to 11950 * distinguish between I/Os sent by this initiator, and by other 11951 * initiators. We set unit attention for initiators other than this one. 11952 * SAM-3 is vague on this point. It does say that a unit attention should 11953 * be established for other initiators when a LUN is reset (see section 11954 * 5.7.3), but it doesn't specifically say that the unit attention should 11955 * be established for this particular initiator when a LUN is reset. Here 11956 * is the relevant text, from SAM-3 rev 8: 11957 * 11958 * 5.7.2 When a SCSI initiator port aborts its own tasks 11959 * 11960 * When a SCSI initiator port causes its own task(s) to be aborted, no 11961 * notification that the task(s) have been aborted shall be returned to 11962 * the SCSI initiator port other than the completion response for the 11963 * command or task management function action that caused the task(s) to 11964 * be aborted and notification(s) associated with related effects of the 11965 * action (e.g., a reset unit attention condition). 11966 * 11967 * XXX KDM for now, we're setting unit attention for all initiators. 11968 */ 11969static int 11970ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11971{ 11972 union ctl_io *xio; 11973#if 0 11974 uint32_t initindex; 11975#endif 11976 int i; 11977 11978 mtx_lock(&lun->lun_lock); 11979 /* 11980 * Run through the OOA queue and abort each I/O. 11981 */ 11982#if 0 11983 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11984#endif 11985 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11986 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11987 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11988 } 11989 11990 /* 11991 * This version sets unit attention for every 11992 */ 11993#if 0 11994 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11995 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11996 if (initindex == i) 11997 continue; 11998 lun->pending_ua[i] |= ua_type; 11999 } 12000#endif 12001 12002 /* 12003 * A reset (any kind, really) clears reservations established with 12004 * RESERVE/RELEASE. It does not clear reservations established 12005 * with PERSISTENT RESERVE OUT, but we don't support that at the 12006 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12007 * reservations made with the RESERVE/RELEASE commands, because 12008 * those commands are obsolete in SPC-3. 12009 */ 12010 lun->flags &= ~CTL_LUN_RESERVED; 12011 12012 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12013#ifdef CTL_WITH_CA 12014 ctl_clear_mask(lun->have_ca, i); 12015#endif 12016 lun->pending_ua[i] |= ua_type; 12017 } 12018 mtx_unlock(&lun->lun_lock); 12019 12020 return (0); 12021} 12022 12023static int 12024ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12025 int other_sc) 12026{ 12027 union ctl_io *xio; 12028 int found; 12029 12030 mtx_assert(&lun->lun_lock, MA_OWNED); 12031 12032 /* 12033 * Run through the OOA queue and attempt to find the given I/O. 12034 * The target port, initiator ID, tag type and tag number have to 12035 * match the values that we got from the initiator. If we have an 12036 * untagged command to abort, simply abort the first untagged command 12037 * we come to. We only allow one untagged command at a time of course. 12038 */ 12039 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12040 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12041 12042 if ((targ_port == UINT32_MAX || 12043 targ_port == xio->io_hdr.nexus.targ_port) && 12044 (init_id == UINT32_MAX || 12045 init_id == xio->io_hdr.nexus.initid.id)) { 12046 if (targ_port != xio->io_hdr.nexus.targ_port || 12047 init_id != xio->io_hdr.nexus.initid.id) 12048 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12049 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12050 found = 1; 12051 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12052 union ctl_ha_msg msg_info; 12053 12054 msg_info.hdr.nexus = xio->io_hdr.nexus; 12055 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12056 msg_info.task.tag_num = xio->scsiio.tag_num; 12057 msg_info.task.tag_type = xio->scsiio.tag_type; 12058 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12059 msg_info.hdr.original_sc = NULL; 12060 msg_info.hdr.serializing_sc = NULL; 12061 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12062 (void *)&msg_info, sizeof(msg_info), 0); 12063 } 12064 } 12065 } 12066 return (found); 12067} 12068 12069static int 12070ctl_abort_task_set(union ctl_io *io) 12071{ 12072 struct ctl_softc *softc = control_softc; 12073 struct ctl_lun *lun; 12074 uint32_t targ_lun; 12075 12076 /* 12077 * Look up the LUN. 12078 */ 12079 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12080 mtx_lock(&softc->ctl_lock); 12081 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12082 lun = softc->ctl_luns[targ_lun]; 12083 else { 12084 mtx_unlock(&softc->ctl_lock); 12085 return (1); 12086 } 12087 12088 mtx_lock(&lun->lun_lock); 12089 mtx_unlock(&softc->ctl_lock); 12090 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12091 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12092 io->io_hdr.nexus.initid.id, 12093 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12094 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12095 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12096 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12097 } 12098 mtx_unlock(&lun->lun_lock); 12099 return (0); 12100} 12101 12102static int 12103ctl_i_t_nexus_reset(union ctl_io *io) 12104{ 12105 struct ctl_softc *softc = control_softc; 12106 struct ctl_lun *lun; 12107 uint32_t initindex; 12108 12109 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12110 mtx_lock(&softc->ctl_lock); 12111 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12112 mtx_lock(&lun->lun_lock); 12113 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12114 io->io_hdr.nexus.initid.id, 12115 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12116#ifdef CTL_WITH_CA 12117 ctl_clear_mask(lun->have_ca, initindex); 12118#endif 12119 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12120 mtx_unlock(&lun->lun_lock); 12121 } 12122 mtx_unlock(&softc->ctl_lock); 12123 return (0); 12124} 12125 12126static int 12127ctl_abort_task(union ctl_io *io) 12128{ 12129 union ctl_io *xio; 12130 struct ctl_lun *lun; 12131 struct ctl_softc *ctl_softc; 12132#if 0 12133 struct sbuf sb; 12134 char printbuf[128]; 12135#endif 12136 int found; 12137 uint32_t targ_lun; 12138 12139 ctl_softc = control_softc; 12140 found = 0; 12141 12142 /* 12143 * Look up the LUN. 12144 */ 12145 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12146 mtx_lock(&ctl_softc->ctl_lock); 12147 if ((targ_lun < CTL_MAX_LUNS) 12148 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12149 lun = ctl_softc->ctl_luns[targ_lun]; 12150 else { 12151 mtx_unlock(&ctl_softc->ctl_lock); 12152 return (1); 12153 } 12154 12155#if 0 12156 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12157 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12158#endif 12159 12160 mtx_lock(&lun->lun_lock); 12161 mtx_unlock(&ctl_softc->ctl_lock); 12162 /* 12163 * Run through the OOA queue and attempt to find the given I/O. 12164 * The target port, initiator ID, tag type and tag number have to 12165 * match the values that we got from the initiator. If we have an 12166 * untagged command to abort, simply abort the first untagged command 12167 * we come to. We only allow one untagged command at a time of course. 12168 */ 12169#if 0 12170 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12171#endif 12172 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12173 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12174#if 0 12175 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12176 12177 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12178 lun->lun, xio->scsiio.tag_num, 12179 xio->scsiio.tag_type, 12180 (xio->io_hdr.blocked_links.tqe_prev 12181 == NULL) ? "" : " BLOCKED", 12182 (xio->io_hdr.flags & 12183 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12184 (xio->io_hdr.flags & 12185 CTL_FLAG_ABORT) ? " ABORT" : "", 12186 (xio->io_hdr.flags & 12187 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12188 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12189 sbuf_finish(&sb); 12190 printf("%s\n", sbuf_data(&sb)); 12191#endif 12192 12193 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12194 && (xio->io_hdr.nexus.initid.id == 12195 io->io_hdr.nexus.initid.id)) { 12196 /* 12197 * If the abort says that the task is untagged, the 12198 * task in the queue must be untagged. Otherwise, 12199 * we just check to see whether the tag numbers 12200 * match. This is because the QLogic firmware 12201 * doesn't pass back the tag type in an abort 12202 * request. 12203 */ 12204#if 0 12205 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12206 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12207 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12208#endif 12209 /* 12210 * XXX KDM we've got problems with FC, because it 12211 * doesn't send down a tag type with aborts. So we 12212 * can only really go by the tag number... 12213 * This may cause problems with parallel SCSI. 12214 * Need to figure that out!! 12215 */ 12216 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12217 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12218 found = 1; 12219 if ((io->io_hdr.flags & 12220 CTL_FLAG_FROM_OTHER_SC) == 0 && 12221 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12222 union ctl_ha_msg msg_info; 12223 12224 io->io_hdr.flags |= 12225 CTL_FLAG_SENT_2OTHER_SC; 12226 msg_info.hdr.nexus = io->io_hdr.nexus; 12227 msg_info.task.task_action = 12228 CTL_TASK_ABORT_TASK; 12229 msg_info.task.tag_num = 12230 io->taskio.tag_num; 12231 msg_info.task.tag_type = 12232 io->taskio.tag_type; 12233 msg_info.hdr.msg_type = 12234 CTL_MSG_MANAGE_TASKS; 12235 msg_info.hdr.original_sc = NULL; 12236 msg_info.hdr.serializing_sc = NULL; 12237#if 0 12238 printf("Sent Abort to other side\n"); 12239#endif 12240 if (CTL_HA_STATUS_SUCCESS != 12241 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12242 (void *)&msg_info, 12243 sizeof(msg_info), 0)) { 12244 } 12245 } 12246#if 0 12247 printf("ctl_abort_task: found I/O to abort\n"); 12248#endif 12249 break; 12250 } 12251 } 12252 } 12253 mtx_unlock(&lun->lun_lock); 12254 12255 if (found == 0) { 12256 /* 12257 * This isn't really an error. It's entirely possible for 12258 * the abort and command completion to cross on the wire. 12259 * This is more of an informative/diagnostic error. 12260 */ 12261#if 0 12262 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12263 "%d:%d:%d:%d tag %d type %d\n", 12264 io->io_hdr.nexus.initid.id, 12265 io->io_hdr.nexus.targ_port, 12266 io->io_hdr.nexus.targ_target.id, 12267 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12268 io->taskio.tag_type); 12269#endif 12270 } 12271 return (0); 12272} 12273 12274static void 12275ctl_run_task(union ctl_io *io) 12276{ 12277 struct ctl_softc *ctl_softc = control_softc; 12278 int retval = 1; 12279 const char *task_desc; 12280 12281 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12282 12283 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12284 ("ctl_run_task: Unextected io_type %d\n", 12285 io->io_hdr.io_type)); 12286 12287 task_desc = ctl_scsi_task_string(&io->taskio); 12288 if (task_desc != NULL) { 12289#ifdef NEEDTOPORT 12290 csevent_log(CSC_CTL | CSC_SHELF_SW | 12291 CTL_TASK_REPORT, 12292 csevent_LogType_Trace, 12293 csevent_Severity_Information, 12294 csevent_AlertLevel_Green, 12295 csevent_FRU_Firmware, 12296 csevent_FRU_Unknown, 12297 "CTL: received task: %s",task_desc); 12298#endif 12299 } else { 12300#ifdef NEEDTOPORT 12301 csevent_log(CSC_CTL | CSC_SHELF_SW | 12302 CTL_TASK_REPORT, 12303 csevent_LogType_Trace, 12304 csevent_Severity_Information, 12305 csevent_AlertLevel_Green, 12306 csevent_FRU_Firmware, 12307 csevent_FRU_Unknown, 12308 "CTL: received unknown task " 12309 "type: %d (%#x)", 12310 io->taskio.task_action, 12311 io->taskio.task_action); 12312#endif 12313 } 12314 switch (io->taskio.task_action) { 12315 case CTL_TASK_ABORT_TASK: 12316 retval = ctl_abort_task(io); 12317 break; 12318 case CTL_TASK_ABORT_TASK_SET: 12319 case CTL_TASK_CLEAR_TASK_SET: 12320 retval = ctl_abort_task_set(io); 12321 break; 12322 case CTL_TASK_CLEAR_ACA: 12323 break; 12324 case CTL_TASK_I_T_NEXUS_RESET: 12325 retval = ctl_i_t_nexus_reset(io); 12326 break; 12327 case CTL_TASK_LUN_RESET: { 12328 struct ctl_lun *lun; 12329 uint32_t targ_lun; 12330 12331 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12332 mtx_lock(&ctl_softc->ctl_lock); 12333 if ((targ_lun < CTL_MAX_LUNS) 12334 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12335 lun = ctl_softc->ctl_luns[targ_lun]; 12336 else { 12337 mtx_unlock(&ctl_softc->ctl_lock); 12338 retval = 1; 12339 break; 12340 } 12341 12342 if (!(io->io_hdr.flags & 12343 CTL_FLAG_FROM_OTHER_SC)) { 12344 union ctl_ha_msg msg_info; 12345 12346 io->io_hdr.flags |= 12347 CTL_FLAG_SENT_2OTHER_SC; 12348 msg_info.hdr.msg_type = 12349 CTL_MSG_MANAGE_TASKS; 12350 msg_info.hdr.nexus = io->io_hdr.nexus; 12351 msg_info.task.task_action = 12352 CTL_TASK_LUN_RESET; 12353 msg_info.hdr.original_sc = NULL; 12354 msg_info.hdr.serializing_sc = NULL; 12355 if (CTL_HA_STATUS_SUCCESS != 12356 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12357 (void *)&msg_info, 12358 sizeof(msg_info), 0)) { 12359 } 12360 } 12361 12362 retval = ctl_lun_reset(lun, io, 12363 CTL_UA_LUN_RESET); 12364 mtx_unlock(&ctl_softc->ctl_lock); 12365 break; 12366 } 12367 case CTL_TASK_TARGET_RESET: 12368 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12369 break; 12370 case CTL_TASK_BUS_RESET: 12371 retval = ctl_bus_reset(ctl_softc, io); 12372 break; 12373 case CTL_TASK_PORT_LOGIN: 12374 break; 12375 case CTL_TASK_PORT_LOGOUT: 12376 break; 12377 default: 12378 printf("ctl_run_task: got unknown task management event %d\n", 12379 io->taskio.task_action); 12380 break; 12381 } 12382 if (retval == 0) 12383 io->io_hdr.status = CTL_SUCCESS; 12384 else 12385 io->io_hdr.status = CTL_ERROR; 12386 ctl_done(io); 12387} 12388 12389/* 12390 * For HA operation. Handle commands that come in from the other 12391 * controller. 12392 */ 12393static void 12394ctl_handle_isc(union ctl_io *io) 12395{ 12396 int free_io; 12397 struct ctl_lun *lun; 12398 struct ctl_softc *ctl_softc; 12399 uint32_t targ_lun; 12400 12401 ctl_softc = control_softc; 12402 12403 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12404 lun = ctl_softc->ctl_luns[targ_lun]; 12405 12406 switch (io->io_hdr.msg_type) { 12407 case CTL_MSG_SERIALIZE: 12408 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12409 break; 12410 case CTL_MSG_R2R: { 12411 const struct ctl_cmd_entry *entry; 12412 12413 /* 12414 * This is only used in SER_ONLY mode. 12415 */ 12416 free_io = 0; 12417 entry = ctl_get_cmd_entry(&io->scsiio); 12418 mtx_lock(&lun->lun_lock); 12419 if (ctl_scsiio_lun_check(ctl_softc, lun, 12420 entry, (struct ctl_scsiio *)io) != 0) { 12421 mtx_unlock(&lun->lun_lock); 12422 ctl_done(io); 12423 break; 12424 } 12425 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12426 mtx_unlock(&lun->lun_lock); 12427 ctl_enqueue_rtr(io); 12428 break; 12429 } 12430 case CTL_MSG_FINISH_IO: 12431 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12432 free_io = 0; 12433 ctl_done(io); 12434 } else { 12435 free_io = 1; 12436 mtx_lock(&lun->lun_lock); 12437 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12438 ooa_links); 12439 ctl_check_blocked(lun); 12440 mtx_unlock(&lun->lun_lock); 12441 } 12442 break; 12443 case CTL_MSG_PERS_ACTION: 12444 ctl_hndl_per_res_out_on_other_sc( 12445 (union ctl_ha_msg *)&io->presio.pr_msg); 12446 free_io = 1; 12447 break; 12448 case CTL_MSG_BAD_JUJU: 12449 free_io = 0; 12450 ctl_done(io); 12451 break; 12452 case CTL_MSG_DATAMOVE: 12453 /* Only used in XFER mode */ 12454 free_io = 0; 12455 ctl_datamove_remote(io); 12456 break; 12457 case CTL_MSG_DATAMOVE_DONE: 12458 /* Only used in XFER mode */ 12459 free_io = 0; 12460 io->scsiio.be_move_done(io); 12461 break; 12462 default: 12463 free_io = 1; 12464 printf("%s: Invalid message type %d\n", 12465 __func__, io->io_hdr.msg_type); 12466 break; 12467 } 12468 if (free_io) 12469 ctl_free_io(io); 12470 12471} 12472 12473 12474/* 12475 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12476 * there is no match. 12477 */ 12478static ctl_lun_error_pattern 12479ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12480{ 12481 const struct ctl_cmd_entry *entry; 12482 ctl_lun_error_pattern filtered_pattern, pattern; 12483 12484 pattern = desc->error_pattern; 12485 12486 /* 12487 * XXX KDM we need more data passed into this function to match a 12488 * custom pattern, and we actually need to implement custom pattern 12489 * matching. 12490 */ 12491 if (pattern & CTL_LUN_PAT_CMD) 12492 return (CTL_LUN_PAT_CMD); 12493 12494 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12495 return (CTL_LUN_PAT_ANY); 12496 12497 entry = ctl_get_cmd_entry(ctsio); 12498 12499 filtered_pattern = entry->pattern & pattern; 12500 12501 /* 12502 * If the user requested specific flags in the pattern (e.g. 12503 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12504 * flags. 12505 * 12506 * If the user did not specify any flags, it doesn't matter whether 12507 * or not the command supports the flags. 12508 */ 12509 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12510 (pattern & ~CTL_LUN_PAT_MASK)) 12511 return (CTL_LUN_PAT_NONE); 12512 12513 /* 12514 * If the user asked for a range check, see if the requested LBA 12515 * range overlaps with this command's LBA range. 12516 */ 12517 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12518 uint64_t lba1; 12519 uint32_t len1; 12520 ctl_action action; 12521 int retval; 12522 12523 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12524 if (retval != 0) 12525 return (CTL_LUN_PAT_NONE); 12526 12527 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12528 desc->lba_range.len); 12529 /* 12530 * A "pass" means that the LBA ranges don't overlap, so 12531 * this doesn't match the user's range criteria. 12532 */ 12533 if (action == CTL_ACTION_PASS) 12534 return (CTL_LUN_PAT_NONE); 12535 } 12536 12537 return (filtered_pattern); 12538} 12539 12540static void 12541ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12542{ 12543 struct ctl_error_desc *desc, *desc2; 12544 12545 mtx_assert(&lun->lun_lock, MA_OWNED); 12546 12547 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12548 ctl_lun_error_pattern pattern; 12549 /* 12550 * Check to see whether this particular command matches 12551 * the pattern in the descriptor. 12552 */ 12553 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12554 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12555 continue; 12556 12557 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12558 case CTL_LUN_INJ_ABORTED: 12559 ctl_set_aborted(&io->scsiio); 12560 break; 12561 case CTL_LUN_INJ_MEDIUM_ERR: 12562 ctl_set_medium_error(&io->scsiio); 12563 break; 12564 case CTL_LUN_INJ_UA: 12565 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12566 * OCCURRED */ 12567 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12568 break; 12569 case CTL_LUN_INJ_CUSTOM: 12570 /* 12571 * We're assuming the user knows what he is doing. 12572 * Just copy the sense information without doing 12573 * checks. 12574 */ 12575 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12576 ctl_min(sizeof(desc->custom_sense), 12577 sizeof(io->scsiio.sense_data))); 12578 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12579 io->scsiio.sense_len = SSD_FULL_SIZE; 12580 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12581 break; 12582 case CTL_LUN_INJ_NONE: 12583 default: 12584 /* 12585 * If this is an error injection type we don't know 12586 * about, clear the continuous flag (if it is set) 12587 * so it will get deleted below. 12588 */ 12589 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12590 break; 12591 } 12592 /* 12593 * By default, each error injection action is a one-shot 12594 */ 12595 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12596 continue; 12597 12598 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12599 12600 free(desc, M_CTL); 12601 } 12602} 12603 12604#ifdef CTL_IO_DELAY 12605static void 12606ctl_datamove_timer_wakeup(void *arg) 12607{ 12608 union ctl_io *io; 12609 12610 io = (union ctl_io *)arg; 12611 12612 ctl_datamove(io); 12613} 12614#endif /* CTL_IO_DELAY */ 12615 12616void 12617ctl_datamove(union ctl_io *io) 12618{ 12619 void (*fe_datamove)(union ctl_io *io); 12620 12621 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12622 12623 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12624 12625#ifdef CTL_TIME_IO 12626 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12627 char str[256]; 12628 char path_str[64]; 12629 struct sbuf sb; 12630 12631 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12632 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12633 12634 sbuf_cat(&sb, path_str); 12635 switch (io->io_hdr.io_type) { 12636 case CTL_IO_SCSI: 12637 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12638 sbuf_printf(&sb, "\n"); 12639 sbuf_cat(&sb, path_str); 12640 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12641 io->scsiio.tag_num, io->scsiio.tag_type); 12642 break; 12643 case CTL_IO_TASK: 12644 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12645 "Tag Type: %d\n", io->taskio.task_action, 12646 io->taskio.tag_num, io->taskio.tag_type); 12647 break; 12648 default: 12649 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12650 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12651 break; 12652 } 12653 sbuf_cat(&sb, path_str); 12654 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12655 (intmax_t)time_uptime - io->io_hdr.start_time); 12656 sbuf_finish(&sb); 12657 printf("%s", sbuf_data(&sb)); 12658 } 12659#endif /* CTL_TIME_IO */ 12660 12661#ifdef CTL_IO_DELAY 12662 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12663 struct ctl_lun *lun; 12664 12665 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12666 12667 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12668 } else { 12669 struct ctl_lun *lun; 12670 12671 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12672 if ((lun != NULL) 12673 && (lun->delay_info.datamove_delay > 0)) { 12674 struct callout *callout; 12675 12676 callout = (struct callout *)&io->io_hdr.timer_bytes; 12677 callout_init(callout, /*mpsafe*/ 1); 12678 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12679 callout_reset(callout, 12680 lun->delay_info.datamove_delay * hz, 12681 ctl_datamove_timer_wakeup, io); 12682 if (lun->delay_info.datamove_type == 12683 CTL_DELAY_TYPE_ONESHOT) 12684 lun->delay_info.datamove_delay = 0; 12685 return; 12686 } 12687 } 12688#endif 12689 12690 /* 12691 * This command has been aborted. Set the port status, so we fail 12692 * the data move. 12693 */ 12694 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12695 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12696 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12697 io->io_hdr.nexus.targ_port, 12698 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12699 io->io_hdr.nexus.targ_lun); 12700 io->io_hdr.port_status = 31337; 12701 /* 12702 * Note that the backend, in this case, will get the 12703 * callback in its context. In other cases it may get 12704 * called in the frontend's interrupt thread context. 12705 */ 12706 io->scsiio.be_move_done(io); 12707 return; 12708 } 12709 12710 /* 12711 * If we're in XFER mode and this I/O is from the other shelf 12712 * controller, we need to send the DMA to the other side to 12713 * actually transfer the data to/from the host. In serialize only 12714 * mode the transfer happens below CTL and ctl_datamove() is only 12715 * called on the machine that originally received the I/O. 12716 */ 12717 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12718 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12719 union ctl_ha_msg msg; 12720 uint32_t sg_entries_sent; 12721 int do_sg_copy; 12722 int i; 12723 12724 memset(&msg, 0, sizeof(msg)); 12725 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12726 msg.hdr.original_sc = io->io_hdr.original_sc; 12727 msg.hdr.serializing_sc = io; 12728 msg.hdr.nexus = io->io_hdr.nexus; 12729 msg.dt.flags = io->io_hdr.flags; 12730 /* 12731 * We convert everything into a S/G list here. We can't 12732 * pass by reference, only by value between controllers. 12733 * So we can't pass a pointer to the S/G list, only as many 12734 * S/G entries as we can fit in here. If it's possible for 12735 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12736 * then we need to break this up into multiple transfers. 12737 */ 12738 if (io->scsiio.kern_sg_entries == 0) { 12739 msg.dt.kern_sg_entries = 1; 12740 /* 12741 * If this is in cached memory, flush the cache 12742 * before we send the DMA request to the other 12743 * controller. We want to do this in either the 12744 * read or the write case. The read case is 12745 * straightforward. In the write case, we want to 12746 * make sure nothing is in the local cache that 12747 * could overwrite the DMAed data. 12748 */ 12749 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12750 /* 12751 * XXX KDM use bus_dmamap_sync() here. 12752 */ 12753 } 12754 12755 /* 12756 * Convert to a physical address if this is a 12757 * virtual address. 12758 */ 12759 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12760 msg.dt.sg_list[0].addr = 12761 io->scsiio.kern_data_ptr; 12762 } else { 12763 /* 12764 * XXX KDM use busdma here! 12765 */ 12766#if 0 12767 msg.dt.sg_list[0].addr = (void *) 12768 vtophys(io->scsiio.kern_data_ptr); 12769#endif 12770 } 12771 12772 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12773 do_sg_copy = 0; 12774 } else { 12775 struct ctl_sg_entry *sgl; 12776 12777 do_sg_copy = 1; 12778 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12779 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12780 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12781 /* 12782 * XXX KDM use bus_dmamap_sync() here. 12783 */ 12784 } 12785 } 12786 12787 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12788 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12789 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12790 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12791 msg.dt.sg_sequence = 0; 12792 12793 /* 12794 * Loop until we've sent all of the S/G entries. On the 12795 * other end, we'll recompose these S/G entries into one 12796 * contiguous list before passing it to the 12797 */ 12798 for (sg_entries_sent = 0; sg_entries_sent < 12799 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12800 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12801 sizeof(msg.dt.sg_list[0])), 12802 msg.dt.kern_sg_entries - sg_entries_sent); 12803 12804 if (do_sg_copy != 0) { 12805 struct ctl_sg_entry *sgl; 12806 int j; 12807 12808 sgl = (struct ctl_sg_entry *) 12809 io->scsiio.kern_data_ptr; 12810 /* 12811 * If this is in cached memory, flush the cache 12812 * before we send the DMA request to the other 12813 * controller. We want to do this in either 12814 * the * read or the write case. The read 12815 * case is straightforward. In the write 12816 * case, we want to make sure nothing is 12817 * in the local cache that could overwrite 12818 * the DMAed data. 12819 */ 12820 12821 for (i = sg_entries_sent, j = 0; 12822 i < msg.dt.cur_sg_entries; i++, j++) { 12823 if ((io->io_hdr.flags & 12824 CTL_FLAG_NO_DATASYNC) == 0) { 12825 /* 12826 * XXX KDM use bus_dmamap_sync() 12827 */ 12828 } 12829 if ((io->io_hdr.flags & 12830 CTL_FLAG_BUS_ADDR) == 0) { 12831 /* 12832 * XXX KDM use busdma. 12833 */ 12834#if 0 12835 msg.dt.sg_list[j].addr =(void *) 12836 vtophys(sgl[i].addr); 12837#endif 12838 } else { 12839 msg.dt.sg_list[j].addr = 12840 sgl[i].addr; 12841 } 12842 msg.dt.sg_list[j].len = sgl[i].len; 12843 } 12844 } 12845 12846 sg_entries_sent += msg.dt.cur_sg_entries; 12847 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12848 msg.dt.sg_last = 1; 12849 else 12850 msg.dt.sg_last = 0; 12851 12852 /* 12853 * XXX KDM drop and reacquire the lock here? 12854 */ 12855 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12856 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12857 /* 12858 * XXX do something here. 12859 */ 12860 } 12861 12862 msg.dt.sent_sg_entries = sg_entries_sent; 12863 } 12864 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12865 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12866 ctl_failover_io(io, /*have_lock*/ 0); 12867 12868 } else { 12869 12870 /* 12871 * Lookup the fe_datamove() function for this particular 12872 * front end. 12873 */ 12874 fe_datamove = 12875 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12876 12877 fe_datamove(io); 12878 } 12879} 12880 12881static void 12882ctl_send_datamove_done(union ctl_io *io, int have_lock) 12883{ 12884 union ctl_ha_msg msg; 12885 int isc_status; 12886 12887 memset(&msg, 0, sizeof(msg)); 12888 12889 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12890 msg.hdr.original_sc = io; 12891 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12892 msg.hdr.nexus = io->io_hdr.nexus; 12893 msg.hdr.status = io->io_hdr.status; 12894 msg.scsi.tag_num = io->scsiio.tag_num; 12895 msg.scsi.tag_type = io->scsiio.tag_type; 12896 msg.scsi.scsi_status = io->scsiio.scsi_status; 12897 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12898 sizeof(io->scsiio.sense_data)); 12899 msg.scsi.sense_len = io->scsiio.sense_len; 12900 msg.scsi.sense_residual = io->scsiio.sense_residual; 12901 msg.scsi.fetd_status = io->io_hdr.port_status; 12902 msg.scsi.residual = io->scsiio.residual; 12903 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12904 12905 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12906 ctl_failover_io(io, /*have_lock*/ have_lock); 12907 return; 12908 } 12909 12910 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12911 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12912 /* XXX do something if this fails */ 12913 } 12914 12915} 12916 12917/* 12918 * The DMA to the remote side is done, now we need to tell the other side 12919 * we're done so it can continue with its data movement. 12920 */ 12921static void 12922ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12923{ 12924 union ctl_io *io; 12925 12926 io = rq->context; 12927 12928 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12929 printf("%s: ISC DMA write failed with error %d", __func__, 12930 rq->ret); 12931 ctl_set_internal_failure(&io->scsiio, 12932 /*sks_valid*/ 1, 12933 /*retry_count*/ rq->ret); 12934 } 12935 12936 ctl_dt_req_free(rq); 12937 12938 /* 12939 * In this case, we had to malloc the memory locally. Free it. 12940 */ 12941 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12942 int i; 12943 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12944 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12945 } 12946 /* 12947 * The data is in local and remote memory, so now we need to send 12948 * status (good or back) back to the other side. 12949 */ 12950 ctl_send_datamove_done(io, /*have_lock*/ 0); 12951} 12952 12953/* 12954 * We've moved the data from the host/controller into local memory. Now we 12955 * need to push it over to the remote controller's memory. 12956 */ 12957static int 12958ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12959{ 12960 int retval; 12961 12962 retval = 0; 12963 12964 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12965 ctl_datamove_remote_write_cb); 12966 12967 return (retval); 12968} 12969 12970static void 12971ctl_datamove_remote_write(union ctl_io *io) 12972{ 12973 int retval; 12974 void (*fe_datamove)(union ctl_io *io); 12975 12976 /* 12977 * - Get the data from the host/HBA into local memory. 12978 * - DMA memory from the local controller to the remote controller. 12979 * - Send status back to the remote controller. 12980 */ 12981 12982 retval = ctl_datamove_remote_sgl_setup(io); 12983 if (retval != 0) 12984 return; 12985 12986 /* Switch the pointer over so the FETD knows what to do */ 12987 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12988 12989 /* 12990 * Use a custom move done callback, since we need to send completion 12991 * back to the other controller, not to the backend on this side. 12992 */ 12993 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12994 12995 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12996 12997 fe_datamove(io); 12998 12999 return; 13000 13001} 13002 13003static int 13004ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13005{ 13006#if 0 13007 char str[256]; 13008 char path_str[64]; 13009 struct sbuf sb; 13010#endif 13011 13012 /* 13013 * In this case, we had to malloc the memory locally. Free it. 13014 */ 13015 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13016 int i; 13017 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13018 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13019 } 13020 13021#if 0 13022 scsi_path_string(io, path_str, sizeof(path_str)); 13023 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13024 sbuf_cat(&sb, path_str); 13025 scsi_command_string(&io->scsiio, NULL, &sb); 13026 sbuf_printf(&sb, "\n"); 13027 sbuf_cat(&sb, path_str); 13028 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13029 io->scsiio.tag_num, io->scsiio.tag_type); 13030 sbuf_cat(&sb, path_str); 13031 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13032 io->io_hdr.flags, io->io_hdr.status); 13033 sbuf_finish(&sb); 13034 printk("%s", sbuf_data(&sb)); 13035#endif 13036 13037 13038 /* 13039 * The read is done, now we need to send status (good or bad) back 13040 * to the other side. 13041 */ 13042 ctl_send_datamove_done(io, /*have_lock*/ 0); 13043 13044 return (0); 13045} 13046 13047static void 13048ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13049{ 13050 union ctl_io *io; 13051 void (*fe_datamove)(union ctl_io *io); 13052 13053 io = rq->context; 13054 13055 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13056 printf("%s: ISC DMA read failed with error %d", __func__, 13057 rq->ret); 13058 ctl_set_internal_failure(&io->scsiio, 13059 /*sks_valid*/ 1, 13060 /*retry_count*/ rq->ret); 13061 } 13062 13063 ctl_dt_req_free(rq); 13064 13065 /* Switch the pointer over so the FETD knows what to do */ 13066 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13067 13068 /* 13069 * Use a custom move done callback, since we need to send completion 13070 * back to the other controller, not to the backend on this side. 13071 */ 13072 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13073 13074 /* XXX KDM add checks like the ones in ctl_datamove? */ 13075 13076 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13077 13078 fe_datamove(io); 13079} 13080 13081static int 13082ctl_datamove_remote_sgl_setup(union ctl_io *io) 13083{ 13084 struct ctl_sg_entry *local_sglist, *remote_sglist; 13085 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13086 struct ctl_softc *softc; 13087 int retval; 13088 int i; 13089 13090 retval = 0; 13091 softc = control_softc; 13092 13093 local_sglist = io->io_hdr.local_sglist; 13094 local_dma_sglist = io->io_hdr.local_dma_sglist; 13095 remote_sglist = io->io_hdr.remote_sglist; 13096 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13097 13098 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13099 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13100 local_sglist[i].len = remote_sglist[i].len; 13101 13102 /* 13103 * XXX Detect the situation where the RS-level I/O 13104 * redirector on the other side has already read the 13105 * data off of the AOR RS on this side, and 13106 * transferred it to remote (mirror) memory on the 13107 * other side. Since we already have the data in 13108 * memory here, we just need to use it. 13109 * 13110 * XXX KDM this can probably be removed once we 13111 * get the cache device code in and take the 13112 * current AOR implementation out. 13113 */ 13114#ifdef NEEDTOPORT 13115 if ((remote_sglist[i].addr >= 13116 (void *)vtophys(softc->mirr->addr)) 13117 && (remote_sglist[i].addr < 13118 ((void *)vtophys(softc->mirr->addr) + 13119 CacheMirrorOffset))) { 13120 local_sglist[i].addr = remote_sglist[i].addr - 13121 CacheMirrorOffset; 13122 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13123 CTL_FLAG_DATA_IN) 13124 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13125 } else { 13126 local_sglist[i].addr = remote_sglist[i].addr + 13127 CacheMirrorOffset; 13128 } 13129#endif 13130#if 0 13131 printf("%s: local %p, remote %p, len %d\n", 13132 __func__, local_sglist[i].addr, 13133 remote_sglist[i].addr, local_sglist[i].len); 13134#endif 13135 } 13136 } else { 13137 uint32_t len_to_go; 13138 13139 /* 13140 * In this case, we don't have automatically allocated 13141 * memory for this I/O on this controller. This typically 13142 * happens with internal CTL I/O -- e.g. inquiry, mode 13143 * sense, etc. Anything coming from RAIDCore will have 13144 * a mirror area available. 13145 */ 13146 len_to_go = io->scsiio.kern_data_len; 13147 13148 /* 13149 * Clear the no datasync flag, we have to use malloced 13150 * buffers. 13151 */ 13152 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13153 13154 /* 13155 * The difficult thing here is that the size of the various 13156 * S/G segments may be different than the size from the 13157 * remote controller. That'll make it harder when DMAing 13158 * the data back to the other side. 13159 */ 13160 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13161 sizeof(io->io_hdr.remote_sglist[0])) && 13162 (len_to_go > 0); i++) { 13163 local_sglist[i].len = ctl_min(len_to_go, 131072); 13164 CTL_SIZE_8B(local_dma_sglist[i].len, 13165 local_sglist[i].len); 13166 local_sglist[i].addr = 13167 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13168 13169 local_dma_sglist[i].addr = local_sglist[i].addr; 13170 13171 if (local_sglist[i].addr == NULL) { 13172 int j; 13173 13174 printf("malloc failed for %zd bytes!", 13175 local_dma_sglist[i].len); 13176 for (j = 0; j < i; j++) { 13177 free(local_sglist[j].addr, M_CTL); 13178 } 13179 ctl_set_internal_failure(&io->scsiio, 13180 /*sks_valid*/ 1, 13181 /*retry_count*/ 4857); 13182 retval = 1; 13183 goto bailout_error; 13184 13185 } 13186 /* XXX KDM do we need a sync here? */ 13187 13188 len_to_go -= local_sglist[i].len; 13189 } 13190 /* 13191 * Reset the number of S/G entries accordingly. The 13192 * original number of S/G entries is available in 13193 * rem_sg_entries. 13194 */ 13195 io->scsiio.kern_sg_entries = i; 13196 13197#if 0 13198 printf("%s: kern_sg_entries = %d\n", __func__, 13199 io->scsiio.kern_sg_entries); 13200 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13201 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13202 local_sglist[i].addr, local_sglist[i].len, 13203 local_dma_sglist[i].len); 13204#endif 13205 } 13206 13207 13208 return (retval); 13209 13210bailout_error: 13211 13212 ctl_send_datamove_done(io, /*have_lock*/ 0); 13213 13214 return (retval); 13215} 13216 13217static int 13218ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13219 ctl_ha_dt_cb callback) 13220{ 13221 struct ctl_ha_dt_req *rq; 13222 struct ctl_sg_entry *remote_sglist, *local_sglist; 13223 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13224 uint32_t local_used, remote_used, total_used; 13225 int retval; 13226 int i, j; 13227 13228 retval = 0; 13229 13230 rq = ctl_dt_req_alloc(); 13231 13232 /* 13233 * If we failed to allocate the request, and if the DMA didn't fail 13234 * anyway, set busy status. This is just a resource allocation 13235 * failure. 13236 */ 13237 if ((rq == NULL) 13238 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13239 ctl_set_busy(&io->scsiio); 13240 13241 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13242 13243 if (rq != NULL) 13244 ctl_dt_req_free(rq); 13245 13246 /* 13247 * The data move failed. We need to return status back 13248 * to the other controller. No point in trying to DMA 13249 * data to the remote controller. 13250 */ 13251 13252 ctl_send_datamove_done(io, /*have_lock*/ 0); 13253 13254 retval = 1; 13255 13256 goto bailout; 13257 } 13258 13259 local_sglist = io->io_hdr.local_sglist; 13260 local_dma_sglist = io->io_hdr.local_dma_sglist; 13261 remote_sglist = io->io_hdr.remote_sglist; 13262 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13263 local_used = 0; 13264 remote_used = 0; 13265 total_used = 0; 13266 13267 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13268 rq->ret = CTL_HA_STATUS_SUCCESS; 13269 rq->context = io; 13270 callback(rq); 13271 goto bailout; 13272 } 13273 13274 /* 13275 * Pull/push the data over the wire from/to the other controller. 13276 * This takes into account the possibility that the local and 13277 * remote sglists may not be identical in terms of the size of 13278 * the elements and the number of elements. 13279 * 13280 * One fundamental assumption here is that the length allocated for 13281 * both the local and remote sglists is identical. Otherwise, we've 13282 * essentially got a coding error of some sort. 13283 */ 13284 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13285 int isc_ret; 13286 uint32_t cur_len, dma_length; 13287 uint8_t *tmp_ptr; 13288 13289 rq->id = CTL_HA_DATA_CTL; 13290 rq->command = command; 13291 rq->context = io; 13292 13293 /* 13294 * Both pointers should be aligned. But it is possible 13295 * that the allocation length is not. They should both 13296 * also have enough slack left over at the end, though, 13297 * to round up to the next 8 byte boundary. 13298 */ 13299 cur_len = ctl_min(local_sglist[i].len - local_used, 13300 remote_sglist[j].len - remote_used); 13301 13302 /* 13303 * In this case, we have a size issue and need to decrease 13304 * the size, except in the case where we actually have less 13305 * than 8 bytes left. In that case, we need to increase 13306 * the DMA length to get the last bit. 13307 */ 13308 if ((cur_len & 0x7) != 0) { 13309 if (cur_len > 0x7) { 13310 cur_len = cur_len - (cur_len & 0x7); 13311 dma_length = cur_len; 13312 } else { 13313 CTL_SIZE_8B(dma_length, cur_len); 13314 } 13315 13316 } else 13317 dma_length = cur_len; 13318 13319 /* 13320 * If we had to allocate memory for this I/O, instead of using 13321 * the non-cached mirror memory, we'll need to flush the cache 13322 * before trying to DMA to the other controller. 13323 * 13324 * We could end up doing this multiple times for the same 13325 * segment if we have a larger local segment than remote 13326 * segment. That shouldn't be an issue. 13327 */ 13328 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13329 /* 13330 * XXX KDM use bus_dmamap_sync() here. 13331 */ 13332 } 13333 13334 rq->size = dma_length; 13335 13336 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13337 tmp_ptr += local_used; 13338 13339 /* Use physical addresses when talking to ISC hardware */ 13340 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13341 /* XXX KDM use busdma */ 13342#if 0 13343 rq->local = vtophys(tmp_ptr); 13344#endif 13345 } else 13346 rq->local = tmp_ptr; 13347 13348 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13349 tmp_ptr += remote_used; 13350 rq->remote = tmp_ptr; 13351 13352 rq->callback = NULL; 13353 13354 local_used += cur_len; 13355 if (local_used >= local_sglist[i].len) { 13356 i++; 13357 local_used = 0; 13358 } 13359 13360 remote_used += cur_len; 13361 if (remote_used >= remote_sglist[j].len) { 13362 j++; 13363 remote_used = 0; 13364 } 13365 total_used += cur_len; 13366 13367 if (total_used >= io->scsiio.kern_data_len) 13368 rq->callback = callback; 13369 13370 if ((rq->size & 0x7) != 0) { 13371 printf("%s: warning: size %d is not on 8b boundary\n", 13372 __func__, rq->size); 13373 } 13374 if (((uintptr_t)rq->local & 0x7) != 0) { 13375 printf("%s: warning: local %p not on 8b boundary\n", 13376 __func__, rq->local); 13377 } 13378 if (((uintptr_t)rq->remote & 0x7) != 0) { 13379 printf("%s: warning: remote %p not on 8b boundary\n", 13380 __func__, rq->local); 13381 } 13382#if 0 13383 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13384 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13385 rq->local, rq->remote, rq->size); 13386#endif 13387 13388 isc_ret = ctl_dt_single(rq); 13389 if (isc_ret == CTL_HA_STATUS_WAIT) 13390 continue; 13391 13392 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13393 rq->ret = CTL_HA_STATUS_SUCCESS; 13394 } else { 13395 rq->ret = isc_ret; 13396 } 13397 callback(rq); 13398 goto bailout; 13399 } 13400 13401bailout: 13402 return (retval); 13403 13404} 13405 13406static void 13407ctl_datamove_remote_read(union ctl_io *io) 13408{ 13409 int retval; 13410 int i; 13411 13412 /* 13413 * This will send an error to the other controller in the case of a 13414 * failure. 13415 */ 13416 retval = ctl_datamove_remote_sgl_setup(io); 13417 if (retval != 0) 13418 return; 13419 13420 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13421 ctl_datamove_remote_read_cb); 13422 if ((retval != 0) 13423 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13424 /* 13425 * Make sure we free memory if there was an error.. The 13426 * ctl_datamove_remote_xfer() function will send the 13427 * datamove done message, or call the callback with an 13428 * error if there is a problem. 13429 */ 13430 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13431 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13432 } 13433 13434 return; 13435} 13436 13437/* 13438 * Process a datamove request from the other controller. This is used for 13439 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13440 * first. Once that is complete, the data gets DMAed into the remote 13441 * controller's memory. For reads, we DMA from the remote controller's 13442 * memory into our memory first, and then move it out to the FETD. 13443 */ 13444static void 13445ctl_datamove_remote(union ctl_io *io) 13446{ 13447 struct ctl_softc *softc; 13448 13449 softc = control_softc; 13450 13451 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13452 13453 /* 13454 * Note that we look for an aborted I/O here, but don't do some of 13455 * the other checks that ctl_datamove() normally does. 13456 * We don't need to run the datamove delay code, since that should 13457 * have been done if need be on the other controller. 13458 */ 13459 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13460 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13461 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13462 io->io_hdr.nexus.targ_port, 13463 io->io_hdr.nexus.targ_target.id, 13464 io->io_hdr.nexus.targ_lun); 13465 io->io_hdr.port_status = 31338; 13466 ctl_send_datamove_done(io, /*have_lock*/ 0); 13467 return; 13468 } 13469 13470 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13471 ctl_datamove_remote_write(io); 13472 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13473 ctl_datamove_remote_read(io); 13474 } else { 13475 union ctl_ha_msg msg; 13476 struct scsi_sense_data *sense; 13477 uint8_t sks[3]; 13478 int retry_count; 13479 13480 memset(&msg, 0, sizeof(msg)); 13481 13482 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13483 msg.hdr.status = CTL_SCSI_ERROR; 13484 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13485 13486 retry_count = 4243; 13487 13488 sense = &msg.scsi.sense_data; 13489 sks[0] = SSD_SCS_VALID; 13490 sks[1] = (retry_count >> 8) & 0xff; 13491 sks[2] = retry_count & 0xff; 13492 13493 /* "Internal target failure" */ 13494 scsi_set_sense_data(sense, 13495 /*sense_format*/ SSD_TYPE_NONE, 13496 /*current_error*/ 1, 13497 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13498 /*asc*/ 0x44, 13499 /*ascq*/ 0x00, 13500 /*type*/ SSD_ELEM_SKS, 13501 /*size*/ sizeof(sks), 13502 /*data*/ sks, 13503 SSD_ELEM_NONE); 13504 13505 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13506 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13507 ctl_failover_io(io, /*have_lock*/ 1); 13508 return; 13509 } 13510 13511 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13512 CTL_HA_STATUS_SUCCESS) { 13513 /* XXX KDM what to do if this fails? */ 13514 } 13515 return; 13516 } 13517 13518} 13519 13520static int 13521ctl_process_done(union ctl_io *io) 13522{ 13523 struct ctl_lun *lun; 13524 struct ctl_softc *ctl_softc; 13525 void (*fe_done)(union ctl_io *io); 13526 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13527 13528 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13529 13530 fe_done = 13531 control_softc->ctl_ports[targ_port]->fe_done; 13532 13533#ifdef CTL_TIME_IO 13534 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13535 char str[256]; 13536 char path_str[64]; 13537 struct sbuf sb; 13538 13539 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13540 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13541 13542 sbuf_cat(&sb, path_str); 13543 switch (io->io_hdr.io_type) { 13544 case CTL_IO_SCSI: 13545 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13546 sbuf_printf(&sb, "\n"); 13547 sbuf_cat(&sb, path_str); 13548 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13549 io->scsiio.tag_num, io->scsiio.tag_type); 13550 break; 13551 case CTL_IO_TASK: 13552 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13553 "Tag Type: %d\n", io->taskio.task_action, 13554 io->taskio.tag_num, io->taskio.tag_type); 13555 break; 13556 default: 13557 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13558 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13559 break; 13560 } 13561 sbuf_cat(&sb, path_str); 13562 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13563 (intmax_t)time_uptime - io->io_hdr.start_time); 13564 sbuf_finish(&sb); 13565 printf("%s", sbuf_data(&sb)); 13566 } 13567#endif /* CTL_TIME_IO */ 13568 13569 switch (io->io_hdr.io_type) { 13570 case CTL_IO_SCSI: 13571 break; 13572 case CTL_IO_TASK: 13573 if (bootverbose || verbose > 0) 13574 ctl_io_error_print(io, NULL); 13575 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13576 ctl_free_io(io); 13577 else 13578 fe_done(io); 13579 return (CTL_RETVAL_COMPLETE); 13580 break; 13581 default: 13582 printf("ctl_process_done: invalid io type %d\n", 13583 io->io_hdr.io_type); 13584 panic("ctl_process_done: invalid io type %d\n", 13585 io->io_hdr.io_type); 13586 break; /* NOTREACHED */ 13587 } 13588 13589 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13590 if (lun == NULL) { 13591 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13592 io->io_hdr.nexus.targ_mapped_lun)); 13593 fe_done(io); 13594 goto bailout; 13595 } 13596 ctl_softc = lun->ctl_softc; 13597 13598 mtx_lock(&lun->lun_lock); 13599 13600 /* 13601 * Check to see if we have any errors to inject here. We only 13602 * inject errors for commands that don't already have errors set. 13603 */ 13604 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13605 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13606 ctl_inject_error(lun, io); 13607 13608 /* 13609 * XXX KDM how do we treat commands that aren't completed 13610 * successfully? 13611 * 13612 * XXX KDM should we also track I/O latency? 13613 */ 13614 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13615 io->io_hdr.io_type == CTL_IO_SCSI) { 13616#ifdef CTL_TIME_IO 13617 struct bintime cur_bt; 13618#endif 13619 int type; 13620 13621 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13622 CTL_FLAG_DATA_IN) 13623 type = CTL_STATS_READ; 13624 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13625 CTL_FLAG_DATA_OUT) 13626 type = CTL_STATS_WRITE; 13627 else 13628 type = CTL_STATS_NO_IO; 13629 13630 lun->stats.ports[targ_port].bytes[type] += 13631 io->scsiio.kern_total_len; 13632 lun->stats.ports[targ_port].operations[type]++; 13633#ifdef CTL_TIME_IO 13634 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13635 &io->io_hdr.dma_bt); 13636 lun->stats.ports[targ_port].num_dmas[type] += 13637 io->io_hdr.num_dmas; 13638 getbintime(&cur_bt); 13639 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13640 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13641#endif 13642 } 13643 13644 /* 13645 * Remove this from the OOA queue. 13646 */ 13647 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13648 13649 /* 13650 * Run through the blocked queue on this LUN and see if anything 13651 * has become unblocked, now that this transaction is done. 13652 */ 13653 ctl_check_blocked(lun); 13654 13655 /* 13656 * If the LUN has been invalidated, free it if there is nothing 13657 * left on its OOA queue. 13658 */ 13659 if ((lun->flags & CTL_LUN_INVALID) 13660 && TAILQ_EMPTY(&lun->ooa_queue)) { 13661 mtx_unlock(&lun->lun_lock); 13662 mtx_lock(&ctl_softc->ctl_lock); 13663 ctl_free_lun(lun); 13664 mtx_unlock(&ctl_softc->ctl_lock); 13665 } else 13666 mtx_unlock(&lun->lun_lock); 13667 13668 /* 13669 * If this command has been aborted, make sure we set the status 13670 * properly. The FETD is responsible for freeing the I/O and doing 13671 * whatever it needs to do to clean up its state. 13672 */ 13673 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13674 ctl_set_task_aborted(&io->scsiio); 13675 13676 /* 13677 * We print out status for every task management command. For SCSI 13678 * commands, we filter out any unit attention errors; they happen 13679 * on every boot, and would clutter up the log. Note: task 13680 * management commands aren't printed here, they are printed above, 13681 * since they should never even make it down here. 13682 */ 13683 switch (io->io_hdr.io_type) { 13684 case CTL_IO_SCSI: { 13685 int error_code, sense_key, asc, ascq; 13686 13687 sense_key = 0; 13688 13689 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13690 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13691 /* 13692 * Since this is just for printing, no need to 13693 * show errors here. 13694 */ 13695 scsi_extract_sense_len(&io->scsiio.sense_data, 13696 io->scsiio.sense_len, 13697 &error_code, 13698 &sense_key, 13699 &asc, 13700 &ascq, 13701 /*show_errors*/ 0); 13702 } 13703 13704 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13705 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13706 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13707 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13708 13709 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13710 ctl_softc->skipped_prints++; 13711 } else { 13712 uint32_t skipped_prints; 13713 13714 skipped_prints = ctl_softc->skipped_prints; 13715 13716 ctl_softc->skipped_prints = 0; 13717 ctl_softc->last_print_jiffies = time_uptime; 13718 13719 if (skipped_prints > 0) { 13720#ifdef NEEDTOPORT 13721 csevent_log(CSC_CTL | CSC_SHELF_SW | 13722 CTL_ERROR_REPORT, 13723 csevent_LogType_Trace, 13724 csevent_Severity_Information, 13725 csevent_AlertLevel_Green, 13726 csevent_FRU_Firmware, 13727 csevent_FRU_Unknown, 13728 "High CTL error volume, %d prints " 13729 "skipped", skipped_prints); 13730#endif 13731 } 13732 if (bootverbose || verbose > 0) 13733 ctl_io_error_print(io, NULL); 13734 } 13735 } 13736 break; 13737 } 13738 case CTL_IO_TASK: 13739 if (bootverbose || verbose > 0) 13740 ctl_io_error_print(io, NULL); 13741 break; 13742 default: 13743 break; 13744 } 13745 13746 /* 13747 * Tell the FETD or the other shelf controller we're done with this 13748 * command. Note that only SCSI commands get to this point. Task 13749 * management commands are completed above. 13750 * 13751 * We only send status to the other controller if we're in XFER 13752 * mode. In SER_ONLY mode, the I/O is done on the controller that 13753 * received the I/O (from CTL's perspective), and so the status is 13754 * generated there. 13755 * 13756 * XXX KDM if we hold the lock here, we could cause a deadlock 13757 * if the frontend comes back in in this context to queue 13758 * something. 13759 */ 13760 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13761 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13762 union ctl_ha_msg msg; 13763 13764 memset(&msg, 0, sizeof(msg)); 13765 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13766 msg.hdr.original_sc = io->io_hdr.original_sc; 13767 msg.hdr.nexus = io->io_hdr.nexus; 13768 msg.hdr.status = io->io_hdr.status; 13769 msg.scsi.scsi_status = io->scsiio.scsi_status; 13770 msg.scsi.tag_num = io->scsiio.tag_num; 13771 msg.scsi.tag_type = io->scsiio.tag_type; 13772 msg.scsi.sense_len = io->scsiio.sense_len; 13773 msg.scsi.sense_residual = io->scsiio.sense_residual; 13774 msg.scsi.residual = io->scsiio.residual; 13775 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13776 sizeof(io->scsiio.sense_data)); 13777 /* 13778 * We copy this whether or not this is an I/O-related 13779 * command. Otherwise, we'd have to go and check to see 13780 * whether it's a read/write command, and it really isn't 13781 * worth it. 13782 */ 13783 memcpy(&msg.scsi.lbalen, 13784 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13785 sizeof(msg.scsi.lbalen)); 13786 13787 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13788 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13789 /* XXX do something here */ 13790 } 13791 13792 ctl_free_io(io); 13793 } else 13794 fe_done(io); 13795 13796bailout: 13797 13798 return (CTL_RETVAL_COMPLETE); 13799} 13800 13801#ifdef CTL_WITH_CA 13802/* 13803 * Front end should call this if it doesn't do autosense. When the request 13804 * sense comes back in from the initiator, we'll dequeue this and send it. 13805 */ 13806int 13807ctl_queue_sense(union ctl_io *io) 13808{ 13809 struct ctl_lun *lun; 13810 struct ctl_softc *ctl_softc; 13811 uint32_t initidx, targ_lun; 13812 13813 ctl_softc = control_softc; 13814 13815 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13816 13817 /* 13818 * LUN lookup will likely move to the ctl_work_thread() once we 13819 * have our new queueing infrastructure (that doesn't put things on 13820 * a per-LUN queue initially). That is so that we can handle 13821 * things like an INQUIRY to a LUN that we don't have enabled. We 13822 * can't deal with that right now. 13823 */ 13824 mtx_lock(&ctl_softc->ctl_lock); 13825 13826 /* 13827 * If we don't have a LUN for this, just toss the sense 13828 * information. 13829 */ 13830 targ_lun = io->io_hdr.nexus.targ_lun; 13831 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13832 if ((targ_lun < CTL_MAX_LUNS) 13833 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13834 lun = ctl_softc->ctl_luns[targ_lun]; 13835 else 13836 goto bailout; 13837 13838 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13839 13840 mtx_lock(&lun->lun_lock); 13841 /* 13842 * Already have CA set for this LUN...toss the sense information. 13843 */ 13844 if (ctl_is_set(lun->have_ca, initidx)) { 13845 mtx_unlock(&lun->lun_lock); 13846 goto bailout; 13847 } 13848 13849 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13850 ctl_min(sizeof(lun->pending_sense[initidx]), 13851 sizeof(io->scsiio.sense_data))); 13852 ctl_set_mask(lun->have_ca, initidx); 13853 mtx_unlock(&lun->lun_lock); 13854 13855bailout: 13856 mtx_unlock(&ctl_softc->ctl_lock); 13857 13858 ctl_free_io(io); 13859 13860 return (CTL_RETVAL_COMPLETE); 13861} 13862#endif 13863 13864/* 13865 * Primary command inlet from frontend ports. All SCSI and task I/O 13866 * requests must go through this function. 13867 */ 13868int 13869ctl_queue(union ctl_io *io) 13870{ 13871 struct ctl_softc *ctl_softc; 13872 13873 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13874 13875 ctl_softc = control_softc; 13876 13877#ifdef CTL_TIME_IO 13878 io->io_hdr.start_time = time_uptime; 13879 getbintime(&io->io_hdr.start_bt); 13880#endif /* CTL_TIME_IO */ 13881 13882 /* Map FE-specific LUN ID into global one. */ 13883 io->io_hdr.nexus.targ_mapped_lun = 13884 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13885 13886 switch (io->io_hdr.io_type) { 13887 case CTL_IO_SCSI: 13888 case CTL_IO_TASK: 13889 ctl_enqueue_incoming(io); 13890 break; 13891 default: 13892 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13893 return (EINVAL); 13894 } 13895 13896 return (CTL_RETVAL_COMPLETE); 13897} 13898 13899#ifdef CTL_IO_DELAY 13900static void 13901ctl_done_timer_wakeup(void *arg) 13902{ 13903 union ctl_io *io; 13904 13905 io = (union ctl_io *)arg; 13906 ctl_done(io); 13907} 13908#endif /* CTL_IO_DELAY */ 13909 13910void 13911ctl_done(union ctl_io *io) 13912{ 13913 struct ctl_softc *ctl_softc; 13914 13915 ctl_softc = control_softc; 13916 13917 /* 13918 * Enable this to catch duplicate completion issues. 13919 */ 13920#if 0 13921 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13922 printf("%s: type %d msg %d cdb %x iptl: " 13923 "%d:%d:%d:%d tag 0x%04x " 13924 "flag %#x status %x\n", 13925 __func__, 13926 io->io_hdr.io_type, 13927 io->io_hdr.msg_type, 13928 io->scsiio.cdb[0], 13929 io->io_hdr.nexus.initid.id, 13930 io->io_hdr.nexus.targ_port, 13931 io->io_hdr.nexus.targ_target.id, 13932 io->io_hdr.nexus.targ_lun, 13933 (io->io_hdr.io_type == 13934 CTL_IO_TASK) ? 13935 io->taskio.tag_num : 13936 io->scsiio.tag_num, 13937 io->io_hdr.flags, 13938 io->io_hdr.status); 13939 } else 13940 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13941#endif 13942 13943 /* 13944 * This is an internal copy of an I/O, and should not go through 13945 * the normal done processing logic. 13946 */ 13947 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13948 return; 13949 13950 /* 13951 * We need to send a msg to the serializing shelf to finish the IO 13952 * as well. We don't send a finish message to the other shelf if 13953 * this is a task management command. Task management commands 13954 * aren't serialized in the OOA queue, but rather just executed on 13955 * both shelf controllers for commands that originated on that 13956 * controller. 13957 */ 13958 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13959 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13960 union ctl_ha_msg msg_io; 13961 13962 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13963 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13964 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13965 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13966 } 13967 /* continue on to finish IO */ 13968 } 13969#ifdef CTL_IO_DELAY 13970 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13971 struct ctl_lun *lun; 13972 13973 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13974 13975 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13976 } else { 13977 struct ctl_lun *lun; 13978 13979 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13980 13981 if ((lun != NULL) 13982 && (lun->delay_info.done_delay > 0)) { 13983 struct callout *callout; 13984 13985 callout = (struct callout *)&io->io_hdr.timer_bytes; 13986 callout_init(callout, /*mpsafe*/ 1); 13987 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13988 callout_reset(callout, 13989 lun->delay_info.done_delay * hz, 13990 ctl_done_timer_wakeup, io); 13991 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13992 lun->delay_info.done_delay = 0; 13993 return; 13994 } 13995 } 13996#endif /* CTL_IO_DELAY */ 13997 13998 ctl_enqueue_done(io); 13999} 14000 14001int 14002ctl_isc(struct ctl_scsiio *ctsio) 14003{ 14004 struct ctl_lun *lun; 14005 int retval; 14006 14007 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14008 14009 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14010 14011 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14012 14013 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14014 14015 return (retval); 14016} 14017 14018 14019static void 14020ctl_work_thread(void *arg) 14021{ 14022 struct ctl_thread *thr = (struct ctl_thread *)arg; 14023 struct ctl_softc *softc = thr->ctl_softc; 14024 union ctl_io *io; 14025 int retval; 14026 14027 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14028 14029 for (;;) { 14030 retval = 0; 14031 14032 /* 14033 * We handle the queues in this order: 14034 * - ISC 14035 * - done queue (to free up resources, unblock other commands) 14036 * - RtR queue 14037 * - incoming queue 14038 * 14039 * If those queues are empty, we break out of the loop and 14040 * go to sleep. 14041 */ 14042 mtx_lock(&thr->queue_lock); 14043 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14044 if (io != NULL) { 14045 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14046 mtx_unlock(&thr->queue_lock); 14047 ctl_handle_isc(io); 14048 continue; 14049 } 14050 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14051 if (io != NULL) { 14052 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14053 /* clear any blocked commands, call fe_done */ 14054 mtx_unlock(&thr->queue_lock); 14055 retval = ctl_process_done(io); 14056 continue; 14057 } 14058 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14059 if (io != NULL) { 14060 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14061 mtx_unlock(&thr->queue_lock); 14062 if (io->io_hdr.io_type == CTL_IO_TASK) 14063 ctl_run_task(io); 14064 else 14065 ctl_scsiio_precheck(softc, &io->scsiio); 14066 continue; 14067 } 14068 if (!ctl_pause_rtr) { 14069 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14070 if (io != NULL) { 14071 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14072 mtx_unlock(&thr->queue_lock); 14073 retval = ctl_scsiio(&io->scsiio); 14074 if (retval != CTL_RETVAL_COMPLETE) 14075 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14076 continue; 14077 } 14078 } 14079 14080 /* Sleep until we have something to do. */ 14081 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14082 } 14083} 14084 14085static void 14086ctl_lun_thread(void *arg) 14087{ 14088 struct ctl_softc *softc = (struct ctl_softc *)arg; 14089 struct ctl_be_lun *be_lun; 14090 int retval; 14091 14092 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14093 14094 for (;;) { 14095 retval = 0; 14096 mtx_lock(&softc->ctl_lock); 14097 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14098 if (be_lun != NULL) { 14099 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14100 mtx_unlock(&softc->ctl_lock); 14101 ctl_create_lun(be_lun); 14102 continue; 14103 } 14104 14105 /* Sleep until we have something to do. */ 14106 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14107 PDROP | PRIBIO, "-", 0); 14108 } 14109} 14110 14111static void 14112ctl_enqueue_incoming(union ctl_io *io) 14113{ 14114 struct ctl_softc *softc = control_softc; 14115 struct ctl_thread *thr; 14116 u_int idx; 14117 14118 idx = (io->io_hdr.nexus.targ_port * 127 + 14119 io->io_hdr.nexus.initid.id) % worker_threads; 14120 thr = &softc->threads[idx]; 14121 mtx_lock(&thr->queue_lock); 14122 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14123 mtx_unlock(&thr->queue_lock); 14124 wakeup(thr); 14125} 14126 14127static void 14128ctl_enqueue_rtr(union ctl_io *io) 14129{ 14130 struct ctl_softc *softc = control_softc; 14131 struct ctl_thread *thr; 14132 14133 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14134 mtx_lock(&thr->queue_lock); 14135 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14136 mtx_unlock(&thr->queue_lock); 14137 wakeup(thr); 14138} 14139 14140static void 14141ctl_enqueue_done(union ctl_io *io) 14142{ 14143 struct ctl_softc *softc = control_softc; 14144 struct ctl_thread *thr; 14145 14146 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14147 mtx_lock(&thr->queue_lock); 14148 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14149 mtx_unlock(&thr->queue_lock); 14150 wakeup(thr); 14151} 14152 14153static void 14154ctl_enqueue_isc(union ctl_io *io) 14155{ 14156 struct ctl_softc *softc = control_softc; 14157 struct ctl_thread *thr; 14158 14159 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14160 mtx_lock(&thr->queue_lock); 14161 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14162 mtx_unlock(&thr->queue_lock); 14163 wakeup(thr); 14164} 14165 14166/* Initialization and failover */ 14167 14168void 14169ctl_init_isc_msg(void) 14170{ 14171 printf("CTL: Still calling this thing\n"); 14172} 14173 14174/* 14175 * Init component 14176 * Initializes component into configuration defined by bootMode 14177 * (see hasc-sv.c) 14178 * returns hasc_Status: 14179 * OK 14180 * ERROR - fatal error 14181 */ 14182static ctl_ha_comp_status 14183ctl_isc_init(struct ctl_ha_component *c) 14184{ 14185 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14186 14187 c->status = ret; 14188 return ret; 14189} 14190 14191/* Start component 14192 * Starts component in state requested. If component starts successfully, 14193 * it must set its own state to the requestrd state 14194 * When requested state is HASC_STATE_HA, the component may refine it 14195 * by adding _SLAVE or _MASTER flags. 14196 * Currently allowed state transitions are: 14197 * UNKNOWN->HA - initial startup 14198 * UNKNOWN->SINGLE - initial startup when no parter detected 14199 * HA->SINGLE - failover 14200 * returns ctl_ha_comp_status: 14201 * OK - component successfully started in requested state 14202 * FAILED - could not start the requested state, failover may 14203 * be possible 14204 * ERROR - fatal error detected, no future startup possible 14205 */ 14206static ctl_ha_comp_status 14207ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14208{ 14209 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14210 14211 printf("%s: go\n", __func__); 14212 14213 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14214 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14215 ctl_is_single = 0; 14216 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14217 != CTL_HA_STATUS_SUCCESS) { 14218 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14219 ret = CTL_HA_COMP_STATUS_ERROR; 14220 } 14221 } else if (CTL_HA_STATE_IS_HA(c->state) 14222 && CTL_HA_STATE_IS_SINGLE(state)){ 14223 // HA->SINGLE transition 14224 ctl_failover(); 14225 ctl_is_single = 1; 14226 } else { 14227 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14228 c->state, state); 14229 ret = CTL_HA_COMP_STATUS_ERROR; 14230 } 14231 if (CTL_HA_STATE_IS_SINGLE(state)) 14232 ctl_is_single = 1; 14233 14234 c->state = state; 14235 c->status = ret; 14236 return ret; 14237} 14238 14239/* 14240 * Quiesce component 14241 * The component must clear any error conditions (set status to OK) and 14242 * prepare itself to another Start call 14243 * returns ctl_ha_comp_status: 14244 * OK 14245 * ERROR 14246 */ 14247static ctl_ha_comp_status 14248ctl_isc_quiesce(struct ctl_ha_component *c) 14249{ 14250 int ret = CTL_HA_COMP_STATUS_OK; 14251 14252 ctl_pause_rtr = 1; 14253 c->status = ret; 14254 return ret; 14255} 14256 14257struct ctl_ha_component ctl_ha_component_ctlisc = 14258{ 14259 .name = "CTL ISC", 14260 .state = CTL_HA_STATE_UNKNOWN, 14261 .init = ctl_isc_init, 14262 .start = ctl_isc_start, 14263 .quiesce = ctl_isc_quiesce 14264}; 14265 14266/* 14267 * vim: ts=8 14268 */ 14269