ctl.c revision 272619
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 272619 2014-10-06 12:38:35Z 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; 315TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 316SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 317 &worker_threads, 1, "Number of worker threads"); 318static int verbose = 0; 319TUNABLE_INT("kern.cam.ctl.verbose", &verbose); 320SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 321 &verbose, 0, "Show SCSI errors returned to initiator"); 322 323/* 324 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 325 * Mode Page Policy (0x87), 326 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 327 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 328 */ 329#define SCSI_EVPD_NUM_SUPPORTED_PAGES 9 330 331static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 332 int param); 333static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 334static int ctl_init(void); 335void ctl_shutdown(void); 336static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 337static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 338static void ctl_ioctl_online(void *arg); 339static void ctl_ioctl_offline(void *arg); 340static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 341static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 342static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 343static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 344static int ctl_ioctl_submit_wait(union ctl_io *io); 345static void ctl_ioctl_datamove(union ctl_io *io); 346static void ctl_ioctl_done(union ctl_io *io); 347static void ctl_ioctl_hard_startstop_callback(void *arg, 348 struct cfi_metatask *metatask); 349static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 350static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 351 struct ctl_ooa *ooa_hdr, 352 struct ctl_ooa_entry *kern_entries); 353static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 354 struct thread *td); 355static uint32_t ctl_map_lun(int port_num, uint32_t lun); 356static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 357#ifdef unused 358static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 359 uint32_t targ_target, uint32_t targ_lun, 360 int can_wait); 361static void ctl_kfree_io(union ctl_io *io); 362#endif /* unused */ 363static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 364 struct ctl_be_lun *be_lun, struct ctl_id target_id); 365static int ctl_free_lun(struct ctl_lun *lun); 366static void ctl_create_lun(struct ctl_be_lun *be_lun); 367/** 368static void ctl_failover_change_pages(struct ctl_softc *softc, 369 struct ctl_scsiio *ctsio, int master); 370**/ 371 372static int ctl_do_mode_select(union ctl_io *io); 373static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 374 uint64_t res_key, uint64_t sa_res_key, 375 uint8_t type, uint32_t residx, 376 struct ctl_scsiio *ctsio, 377 struct scsi_per_res_out *cdb, 378 struct scsi_per_res_out_parms* param); 379static void ctl_pro_preempt_other(struct ctl_lun *lun, 380 union ctl_ha_msg *msg); 381static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 382static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 383static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 384static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 385static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 386static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 387 int alloc_len); 388static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 389 int alloc_len); 390static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 391static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 392static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 393static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 394static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 395static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 396static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 397 union ctl_io *ooa_io); 398static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 399 union ctl_io *starting_io); 400static int ctl_check_blocked(struct ctl_lun *lun); 401static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 402 struct ctl_lun *lun, 403 const struct ctl_cmd_entry *entry, 404 struct ctl_scsiio *ctsio); 405//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 406static void ctl_failover(void); 407static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 408 struct ctl_scsiio *ctsio); 409static int ctl_scsiio(struct ctl_scsiio *ctsio); 410 411static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 412static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 413 ctl_ua_type ua_type); 414static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 415 ctl_ua_type ua_type); 416static int ctl_abort_task(union ctl_io *io); 417static int ctl_abort_task_set(union ctl_io *io); 418static int ctl_i_t_nexus_reset(union ctl_io *io); 419static void ctl_run_task(union ctl_io *io); 420#ifdef CTL_IO_DELAY 421static void ctl_datamove_timer_wakeup(void *arg); 422static void ctl_done_timer_wakeup(void *arg); 423#endif /* CTL_IO_DELAY */ 424 425static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 426static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 427static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 428static void ctl_datamove_remote_write(union ctl_io *io); 429static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 430static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 431static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 432static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 433 ctl_ha_dt_cb callback); 434static void ctl_datamove_remote_read(union ctl_io *io); 435static void ctl_datamove_remote(union ctl_io *io); 436static int ctl_process_done(union ctl_io *io); 437static void ctl_lun_thread(void *arg); 438static void ctl_work_thread(void *arg); 439static void ctl_enqueue_incoming(union ctl_io *io); 440static void ctl_enqueue_rtr(union ctl_io *io); 441static void ctl_enqueue_done(union ctl_io *io); 442static void ctl_enqueue_isc(union ctl_io *io); 443static const struct ctl_cmd_entry * 444 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 445static const struct ctl_cmd_entry * 446 ctl_validate_command(struct ctl_scsiio *ctsio); 447static int ctl_cmd_applicable(uint8_t lun_type, 448 const struct ctl_cmd_entry *entry); 449 450/* 451 * Load the serialization table. This isn't very pretty, but is probably 452 * the easiest way to do it. 453 */ 454#include "ctl_ser_table.c" 455 456/* 457 * We only need to define open, close and ioctl routines for this driver. 458 */ 459static struct cdevsw ctl_cdevsw = { 460 .d_version = D_VERSION, 461 .d_flags = 0, 462 .d_open = ctl_open, 463 .d_close = ctl_close, 464 .d_ioctl = ctl_ioctl, 465 .d_name = "ctl", 466}; 467 468 469MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 470MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 471 472static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 473 474static moduledata_t ctl_moduledata = { 475 "ctl", 476 ctl_module_event_handler, 477 NULL 478}; 479 480DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 481MODULE_VERSION(ctl, 1); 482 483static struct ctl_frontend ioctl_frontend = 484{ 485 .name = "ioctl", 486}; 487 488static void 489ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 490 union ctl_ha_msg *msg_info) 491{ 492 struct ctl_scsiio *ctsio; 493 494 if (msg_info->hdr.original_sc == NULL) { 495 printf("%s: original_sc == NULL!\n", __func__); 496 /* XXX KDM now what? */ 497 return; 498 } 499 500 ctsio = &msg_info->hdr.original_sc->scsiio; 501 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 502 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 503 ctsio->io_hdr.status = msg_info->hdr.status; 504 ctsio->scsi_status = msg_info->scsi.scsi_status; 505 ctsio->sense_len = msg_info->scsi.sense_len; 506 ctsio->sense_residual = msg_info->scsi.sense_residual; 507 ctsio->residual = msg_info->scsi.residual; 508 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 509 sizeof(ctsio->sense_data)); 510 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 511 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 512 ctl_enqueue_isc((union ctl_io *)ctsio); 513} 514 515static void 516ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 517 union ctl_ha_msg *msg_info) 518{ 519 struct ctl_scsiio *ctsio; 520 521 if (msg_info->hdr.serializing_sc == NULL) { 522 printf("%s: serializing_sc == NULL!\n", __func__); 523 /* XXX KDM now what? */ 524 return; 525 } 526 527 ctsio = &msg_info->hdr.serializing_sc->scsiio; 528#if 0 529 /* 530 * Attempt to catch the situation where an I/O has 531 * been freed, and we're using it again. 532 */ 533 if (ctsio->io_hdr.io_type == 0xff) { 534 union ctl_io *tmp_io; 535 tmp_io = (union ctl_io *)ctsio; 536 printf("%s: %p use after free!\n", __func__, 537 ctsio); 538 printf("%s: type %d msg %d cdb %x iptl: " 539 "%d:%d:%d:%d tag 0x%04x " 540 "flag %#x status %x\n", 541 __func__, 542 tmp_io->io_hdr.io_type, 543 tmp_io->io_hdr.msg_type, 544 tmp_io->scsiio.cdb[0], 545 tmp_io->io_hdr.nexus.initid.id, 546 tmp_io->io_hdr.nexus.targ_port, 547 tmp_io->io_hdr.nexus.targ_target.id, 548 tmp_io->io_hdr.nexus.targ_lun, 549 (tmp_io->io_hdr.io_type == 550 CTL_IO_TASK) ? 551 tmp_io->taskio.tag_num : 552 tmp_io->scsiio.tag_num, 553 tmp_io->io_hdr.flags, 554 tmp_io->io_hdr.status); 555 } 556#endif 557 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 558 ctl_enqueue_isc((union ctl_io *)ctsio); 559} 560 561/* 562 * ISC (Inter Shelf Communication) event handler. Events from the HA 563 * subsystem come in here. 564 */ 565static void 566ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 567{ 568 struct ctl_softc *ctl_softc; 569 union ctl_io *io; 570 struct ctl_prio *presio; 571 ctl_ha_status isc_status; 572 573 ctl_softc = control_softc; 574 io = NULL; 575 576 577#if 0 578 printf("CTL: Isc Msg event %d\n", event); 579#endif 580 if (event == CTL_HA_EVT_MSG_RECV) { 581 union ctl_ha_msg msg_info; 582 583 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 584 sizeof(msg_info), /*wait*/ 0); 585#if 0 586 printf("CTL: msg_type %d\n", msg_info.msg_type); 587#endif 588 if (isc_status != 0) { 589 printf("Error receiving message, status = %d\n", 590 isc_status); 591 return; 592 } 593 594 switch (msg_info.hdr.msg_type) { 595 case CTL_MSG_SERIALIZE: 596#if 0 597 printf("Serialize\n"); 598#endif 599 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 600 if (io == NULL) { 601 printf("ctl_isc_event_handler: can't allocate " 602 "ctl_io!\n"); 603 /* Bad Juju */ 604 /* Need to set busy and send msg back */ 605 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 606 msg_info.hdr.status = CTL_SCSI_ERROR; 607 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 608 msg_info.scsi.sense_len = 0; 609 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 610 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 611 } 612 goto bailout; 613 } 614 ctl_zero_io(io); 615 // populate ctsio from msg_info 616 io->io_hdr.io_type = CTL_IO_SCSI; 617 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 618 io->io_hdr.original_sc = msg_info.hdr.original_sc; 619#if 0 620 printf("pOrig %x\n", (int)msg_info.original_sc); 621#endif 622 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 623 CTL_FLAG_IO_ACTIVE; 624 /* 625 * If we're in serialization-only mode, we don't 626 * want to go through full done processing. Thus 627 * the COPY flag. 628 * 629 * XXX KDM add another flag that is more specific. 630 */ 631 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 632 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 633 io->io_hdr.nexus = msg_info.hdr.nexus; 634#if 0 635 printf("targ %d, port %d, iid %d, lun %d\n", 636 io->io_hdr.nexus.targ_target.id, 637 io->io_hdr.nexus.targ_port, 638 io->io_hdr.nexus.initid.id, 639 io->io_hdr.nexus.targ_lun); 640#endif 641 io->scsiio.tag_num = msg_info.scsi.tag_num; 642 io->scsiio.tag_type = msg_info.scsi.tag_type; 643 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 644 CTL_MAX_CDBLEN); 645 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 646 const struct ctl_cmd_entry *entry; 647 648 entry = ctl_get_cmd_entry(&io->scsiio); 649 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 650 io->io_hdr.flags |= 651 entry->flags & CTL_FLAG_DATA_MASK; 652 } 653 ctl_enqueue_isc(io); 654 break; 655 656 /* Performed on the Originating SC, XFER mode only */ 657 case CTL_MSG_DATAMOVE: { 658 struct ctl_sg_entry *sgl; 659 int i, j; 660 661 io = msg_info.hdr.original_sc; 662 if (io == NULL) { 663 printf("%s: original_sc == NULL!\n", __func__); 664 /* XXX KDM do something here */ 665 break; 666 } 667 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 668 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 669 /* 670 * Keep track of this, we need to send it back over 671 * when the datamove is complete. 672 */ 673 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 674 675 if (msg_info.dt.sg_sequence == 0) { 676 /* 677 * XXX KDM we use the preallocated S/G list 678 * here, but we'll need to change this to 679 * dynamic allocation if we need larger S/G 680 * lists. 681 */ 682 if (msg_info.dt.kern_sg_entries > 683 sizeof(io->io_hdr.remote_sglist) / 684 sizeof(io->io_hdr.remote_sglist[0])) { 685 printf("%s: number of S/G entries " 686 "needed %u > allocated num %zd\n", 687 __func__, 688 msg_info.dt.kern_sg_entries, 689 sizeof(io->io_hdr.remote_sglist)/ 690 sizeof(io->io_hdr.remote_sglist[0])); 691 692 /* 693 * XXX KDM send a message back to 694 * the other side to shut down the 695 * DMA. The error will come back 696 * through via the normal channel. 697 */ 698 break; 699 } 700 sgl = io->io_hdr.remote_sglist; 701 memset(sgl, 0, 702 sizeof(io->io_hdr.remote_sglist)); 703 704 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 705 706 io->scsiio.kern_sg_entries = 707 msg_info.dt.kern_sg_entries; 708 io->scsiio.rem_sg_entries = 709 msg_info.dt.kern_sg_entries; 710 io->scsiio.kern_data_len = 711 msg_info.dt.kern_data_len; 712 io->scsiio.kern_total_len = 713 msg_info.dt.kern_total_len; 714 io->scsiio.kern_data_resid = 715 msg_info.dt.kern_data_resid; 716 io->scsiio.kern_rel_offset = 717 msg_info.dt.kern_rel_offset; 718 /* 719 * Clear out per-DMA flags. 720 */ 721 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 722 /* 723 * Add per-DMA flags that are set for this 724 * particular DMA request. 725 */ 726 io->io_hdr.flags |= msg_info.dt.flags & 727 CTL_FLAG_RDMA_MASK; 728 } else 729 sgl = (struct ctl_sg_entry *) 730 io->scsiio.kern_data_ptr; 731 732 for (i = msg_info.dt.sent_sg_entries, j = 0; 733 i < (msg_info.dt.sent_sg_entries + 734 msg_info.dt.cur_sg_entries); i++, j++) { 735 sgl[i].addr = msg_info.dt.sg_list[j].addr; 736 sgl[i].len = msg_info.dt.sg_list[j].len; 737 738#if 0 739 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 740 __func__, 741 msg_info.dt.sg_list[j].addr, 742 msg_info.dt.sg_list[j].len, 743 sgl[i].addr, sgl[i].len, j, i); 744#endif 745 } 746#if 0 747 memcpy(&sgl[msg_info.dt.sent_sg_entries], 748 msg_info.dt.sg_list, 749 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 750#endif 751 752 /* 753 * If this is the last piece of the I/O, we've got 754 * the full S/G list. Queue processing in the thread. 755 * Otherwise wait for the next piece. 756 */ 757 if (msg_info.dt.sg_last != 0) 758 ctl_enqueue_isc(io); 759 break; 760 } 761 /* Performed on the Serializing (primary) SC, XFER mode only */ 762 case CTL_MSG_DATAMOVE_DONE: { 763 if (msg_info.hdr.serializing_sc == NULL) { 764 printf("%s: serializing_sc == NULL!\n", 765 __func__); 766 /* XXX KDM now what? */ 767 break; 768 } 769 /* 770 * We grab the sense information here in case 771 * there was a failure, so we can return status 772 * back to the initiator. 773 */ 774 io = msg_info.hdr.serializing_sc; 775 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 776 io->io_hdr.status = msg_info.hdr.status; 777 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 778 io->scsiio.sense_len = msg_info.scsi.sense_len; 779 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 780 io->io_hdr.port_status = msg_info.scsi.fetd_status; 781 io->scsiio.residual = msg_info.scsi.residual; 782 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 783 sizeof(io->scsiio.sense_data)); 784 ctl_enqueue_isc(io); 785 break; 786 } 787 788 /* Preformed on Originating SC, SER_ONLY mode */ 789 case CTL_MSG_R2R: 790 io = msg_info.hdr.original_sc; 791 if (io == NULL) { 792 printf("%s: Major Bummer\n", __func__); 793 return; 794 } else { 795#if 0 796 printf("pOrig %x\n",(int) ctsio); 797#endif 798 } 799 io->io_hdr.msg_type = CTL_MSG_R2R; 800 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 801 ctl_enqueue_isc(io); 802 break; 803 804 /* 805 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 806 * mode. 807 * Performed on the Originating (i.e. secondary) SC in XFER 808 * mode 809 */ 810 case CTL_MSG_FINISH_IO: 811 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 812 ctl_isc_handler_finish_xfer(ctl_softc, 813 &msg_info); 814 else 815 ctl_isc_handler_finish_ser_only(ctl_softc, 816 &msg_info); 817 break; 818 819 /* Preformed on Originating SC */ 820 case CTL_MSG_BAD_JUJU: 821 io = msg_info.hdr.original_sc; 822 if (io == NULL) { 823 printf("%s: Bad JUJU!, original_sc is NULL!\n", 824 __func__); 825 break; 826 } 827 ctl_copy_sense_data(&msg_info, io); 828 /* 829 * IO should have already been cleaned up on other 830 * SC so clear this flag so we won't send a message 831 * back to finish the IO there. 832 */ 833 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 834 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 835 836 /* io = msg_info.hdr.serializing_sc; */ 837 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 838 ctl_enqueue_isc(io); 839 break; 840 841 /* Handle resets sent from the other side */ 842 case CTL_MSG_MANAGE_TASKS: { 843 struct ctl_taskio *taskio; 844 taskio = (struct ctl_taskio *)ctl_alloc_io( 845 (void *)ctl_softc->othersc_pool); 846 if (taskio == NULL) { 847 printf("ctl_isc_event_handler: can't allocate " 848 "ctl_io!\n"); 849 /* Bad Juju */ 850 /* should I just call the proper reset func 851 here??? */ 852 goto bailout; 853 } 854 ctl_zero_io((union ctl_io *)taskio); 855 taskio->io_hdr.io_type = CTL_IO_TASK; 856 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 857 taskio->io_hdr.nexus = msg_info.hdr.nexus; 858 taskio->task_action = msg_info.task.task_action; 859 taskio->tag_num = msg_info.task.tag_num; 860 taskio->tag_type = msg_info.task.tag_type; 861#ifdef CTL_TIME_IO 862 taskio->io_hdr.start_time = time_uptime; 863 getbintime(&taskio->io_hdr.start_bt); 864#if 0 865 cs_prof_gettime(&taskio->io_hdr.start_ticks); 866#endif 867#endif /* CTL_TIME_IO */ 868 ctl_run_task((union ctl_io *)taskio); 869 break; 870 } 871 /* Persistent Reserve action which needs attention */ 872 case CTL_MSG_PERS_ACTION: 873 presio = (struct ctl_prio *)ctl_alloc_io( 874 (void *)ctl_softc->othersc_pool); 875 if (presio == NULL) { 876 printf("ctl_isc_event_handler: can't allocate " 877 "ctl_io!\n"); 878 /* Bad Juju */ 879 /* Need to set busy and send msg back */ 880 goto bailout; 881 } 882 ctl_zero_io((union ctl_io *)presio); 883 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 884 presio->pr_msg = msg_info.pr; 885 ctl_enqueue_isc((union ctl_io *)presio); 886 break; 887 case CTL_MSG_SYNC_FE: 888 rcv_sync_msg = 1; 889 break; 890 case CTL_MSG_APS_LOCK: { 891 // It's quicker to execute this then to 892 // queue it. 893 struct ctl_lun *lun; 894 struct ctl_page_index *page_index; 895 struct copan_aps_subpage *current_sp; 896 uint32_t targ_lun; 897 898 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 899 lun = ctl_softc->ctl_luns[targ_lun]; 900 mtx_lock(&lun->lun_lock); 901 page_index = &lun->mode_pages.index[index_to_aps_page]; 902 current_sp = (struct copan_aps_subpage *) 903 (page_index->page_data + 904 (page_index->page_len * CTL_PAGE_CURRENT)); 905 906 current_sp->lock_active = msg_info.aps.lock_flag; 907 mtx_unlock(&lun->lun_lock); 908 break; 909 } 910 default: 911 printf("How did I get here?\n"); 912 } 913 } else if (event == CTL_HA_EVT_MSG_SENT) { 914 if (param != CTL_HA_STATUS_SUCCESS) { 915 printf("Bad status from ctl_ha_msg_send status %d\n", 916 param); 917 } 918 return; 919 } else if (event == CTL_HA_EVT_DISCONNECT) { 920 printf("CTL: Got a disconnect from Isc\n"); 921 return; 922 } else { 923 printf("ctl_isc_event_handler: Unknown event %d\n", event); 924 return; 925 } 926 927bailout: 928 return; 929} 930 931static void 932ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 933{ 934 struct scsi_sense_data *sense; 935 936 sense = &dest->scsiio.sense_data; 937 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 938 dest->scsiio.scsi_status = src->scsi.scsi_status; 939 dest->scsiio.sense_len = src->scsi.sense_len; 940 dest->io_hdr.status = src->hdr.status; 941} 942 943static int 944ctl_init(void) 945{ 946 struct ctl_softc *softc; 947 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 948 struct ctl_port *port; 949 uint8_t sc_id =0; 950 int i, error, retval; 951 //int isc_retval; 952 953 retval = 0; 954 ctl_pause_rtr = 0; 955 rcv_sync_msg = 0; 956 957 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 958 M_WAITOK | M_ZERO); 959 softc = control_softc; 960 961 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 962 "cam/ctl"); 963 964 softc->dev->si_drv1 = softc; 965 966 /* 967 * By default, return a "bad LUN" peripheral qualifier for unknown 968 * LUNs. The user can override this default using the tunable or 969 * sysctl. See the comment in ctl_inquiry_std() for more details. 970 */ 971 softc->inquiry_pq_no_lun = 1; 972 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 973 &softc->inquiry_pq_no_lun); 974 sysctl_ctx_init(&softc->sysctl_ctx); 975 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 976 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 977 CTLFLAG_RD, 0, "CAM Target Layer"); 978 979 if (softc->sysctl_tree == NULL) { 980 printf("%s: unable to allocate sysctl tree\n", __func__); 981 destroy_dev(softc->dev); 982 free(control_softc, M_DEVBUF); 983 control_softc = NULL; 984 return (ENOMEM); 985 } 986 987 SYSCTL_ADD_INT(&softc->sysctl_ctx, 988 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 989 "inquiry_pq_no_lun", CTLFLAG_RW, 990 &softc->inquiry_pq_no_lun, 0, 991 "Report no lun possible for invalid LUNs"); 992 993 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 994 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 995 softc->open_count = 0; 996 997 /* 998 * Default to actually sending a SYNCHRONIZE CACHE command down to 999 * the drive. 1000 */ 1001 softc->flags = CTL_FLAG_REAL_SYNC; 1002 1003 /* 1004 * In Copan's HA scheme, the "master" and "slave" roles are 1005 * figured out through the slot the controller is in. Although it 1006 * is an active/active system, someone has to be in charge. 1007 */ 1008#ifdef NEEDTOPORT 1009 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1010#endif 1011 1012 if (sc_id == 0) { 1013 softc->flags |= CTL_FLAG_MASTER_SHELF; 1014 persis_offset = 0; 1015 } else 1016 persis_offset = CTL_MAX_INITIATORS; 1017 1018 /* 1019 * XXX KDM need to figure out where we want to get our target ID 1020 * and WWID. Is it different on each port? 1021 */ 1022 softc->target.id = 0; 1023 softc->target.wwid[0] = 0x12345678; 1024 softc->target.wwid[1] = 0x87654321; 1025 STAILQ_INIT(&softc->lun_list); 1026 STAILQ_INIT(&softc->pending_lun_queue); 1027 STAILQ_INIT(&softc->fe_list); 1028 STAILQ_INIT(&softc->port_list); 1029 STAILQ_INIT(&softc->be_list); 1030 STAILQ_INIT(&softc->io_pools); 1031 ctl_tpc_init(softc); 1032 1033 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1034 &internal_pool)!= 0){ 1035 printf("ctl: can't allocate %d entry internal pool, " 1036 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1037 return (ENOMEM); 1038 } 1039 1040 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1041 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1042 printf("ctl: can't allocate %d entry emergency pool, " 1043 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1044 ctl_pool_free(internal_pool); 1045 return (ENOMEM); 1046 } 1047 1048 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1049 &other_pool) != 0) 1050 { 1051 printf("ctl: can't allocate %d entry other SC pool, " 1052 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1053 ctl_pool_free(internal_pool); 1054 ctl_pool_free(emergency_pool); 1055 return (ENOMEM); 1056 } 1057 1058 softc->internal_pool = internal_pool; 1059 softc->emergency_pool = emergency_pool; 1060 softc->othersc_pool = other_pool; 1061 1062 if (worker_threads <= 0) 1063 worker_threads = max(1, mp_ncpus / 4); 1064 if (worker_threads > CTL_MAX_THREADS) 1065 worker_threads = CTL_MAX_THREADS; 1066 1067 for (i = 0; i < worker_threads; i++) { 1068 struct ctl_thread *thr = &softc->threads[i]; 1069 1070 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1071 thr->ctl_softc = softc; 1072 STAILQ_INIT(&thr->incoming_queue); 1073 STAILQ_INIT(&thr->rtr_queue); 1074 STAILQ_INIT(&thr->done_queue); 1075 STAILQ_INIT(&thr->isc_queue); 1076 1077 error = kproc_kthread_add(ctl_work_thread, thr, 1078 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1079 if (error != 0) { 1080 printf("error creating CTL work thread!\n"); 1081 ctl_pool_free(internal_pool); 1082 ctl_pool_free(emergency_pool); 1083 ctl_pool_free(other_pool); 1084 return (error); 1085 } 1086 } 1087 error = kproc_kthread_add(ctl_lun_thread, softc, 1088 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1089 if (error != 0) { 1090 printf("error creating CTL lun thread!\n"); 1091 ctl_pool_free(internal_pool); 1092 ctl_pool_free(emergency_pool); 1093 ctl_pool_free(other_pool); 1094 return (error); 1095 } 1096 if (bootverbose) 1097 printf("ctl: CAM Target Layer loaded\n"); 1098 1099 /* 1100 * Initialize the ioctl front end. 1101 */ 1102 ctl_frontend_register(&ioctl_frontend); 1103 port = &softc->ioctl_info.port; 1104 port->frontend = &ioctl_frontend; 1105 sprintf(softc->ioctl_info.port_name, "ioctl"); 1106 port->port_type = CTL_PORT_IOCTL; 1107 port->num_requested_ctl_io = 100; 1108 port->port_name = softc->ioctl_info.port_name; 1109 port->port_online = ctl_ioctl_online; 1110 port->port_offline = ctl_ioctl_offline; 1111 port->onoff_arg = &softc->ioctl_info; 1112 port->lun_enable = ctl_ioctl_lun_enable; 1113 port->lun_disable = ctl_ioctl_lun_disable; 1114 port->targ_lun_arg = &softc->ioctl_info; 1115 port->fe_datamove = ctl_ioctl_datamove; 1116 port->fe_done = ctl_ioctl_done; 1117 port->max_targets = 15; 1118 port->max_target_id = 15; 1119 1120 if (ctl_port_register(&softc->ioctl_info.port, 1121 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1122 printf("ctl: ioctl front end registration failed, will " 1123 "continue anyway\n"); 1124 } 1125 1126#ifdef CTL_IO_DELAY 1127 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1128 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1129 sizeof(struct callout), CTL_TIMER_BYTES); 1130 return (EINVAL); 1131 } 1132#endif /* CTL_IO_DELAY */ 1133 1134 return (0); 1135} 1136 1137void 1138ctl_shutdown(void) 1139{ 1140 struct ctl_softc *softc; 1141 struct ctl_lun *lun, *next_lun; 1142 struct ctl_io_pool *pool; 1143 1144 softc = (struct ctl_softc *)control_softc; 1145 1146 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1147 printf("ctl: ioctl front end deregistration failed\n"); 1148 1149 mtx_lock(&softc->ctl_lock); 1150 1151 /* 1152 * Free up each LUN. 1153 */ 1154 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1155 next_lun = STAILQ_NEXT(lun, links); 1156 ctl_free_lun(lun); 1157 } 1158 1159 mtx_unlock(&softc->ctl_lock); 1160 1161 ctl_frontend_deregister(&ioctl_frontend); 1162 1163 /* 1164 * This will rip the rug out from under any FETDs or anyone else 1165 * that has a pool allocated. Since we increment our module 1166 * refcount any time someone outside the main CTL module allocates 1167 * a pool, we shouldn't have any problems here. The user won't be 1168 * able to unload the CTL module until client modules have 1169 * successfully unloaded. 1170 */ 1171 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1172 ctl_pool_free(pool); 1173 1174#if 0 1175 ctl_shutdown_thread(softc->work_thread); 1176 mtx_destroy(&softc->queue_lock); 1177#endif 1178 1179 ctl_tpc_shutdown(softc); 1180 mtx_destroy(&softc->pool_lock); 1181 mtx_destroy(&softc->ctl_lock); 1182 1183 destroy_dev(softc->dev); 1184 1185 sysctl_ctx_free(&softc->sysctl_ctx); 1186 1187 free(control_softc, M_DEVBUF); 1188 control_softc = NULL; 1189 1190 if (bootverbose) 1191 printf("ctl: CAM Target Layer unloaded\n"); 1192} 1193 1194static int 1195ctl_module_event_handler(module_t mod, int what, void *arg) 1196{ 1197 1198 switch (what) { 1199 case MOD_LOAD: 1200 return (ctl_init()); 1201 case MOD_UNLOAD: 1202 return (EBUSY); 1203 default: 1204 return (EOPNOTSUPP); 1205 } 1206} 1207 1208/* 1209 * XXX KDM should we do some access checks here? Bump a reference count to 1210 * prevent a CTL module from being unloaded while someone has it open? 1211 */ 1212static int 1213ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1214{ 1215 return (0); 1216} 1217 1218static int 1219ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1220{ 1221 return (0); 1222} 1223 1224int 1225ctl_port_enable(ctl_port_type port_type) 1226{ 1227 struct ctl_softc *softc; 1228 struct ctl_port *port; 1229 1230 if (ctl_is_single == 0) { 1231 union ctl_ha_msg msg_info; 1232 int isc_retval; 1233 1234#if 0 1235 printf("%s: HA mode, synchronizing frontend enable\n", 1236 __func__); 1237#endif 1238 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1239 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1240 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1241 printf("Sync msg send error retval %d\n", isc_retval); 1242 } 1243 if (!rcv_sync_msg) { 1244 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1245 sizeof(msg_info), 1); 1246 } 1247#if 0 1248 printf("CTL:Frontend Enable\n"); 1249 } else { 1250 printf("%s: single mode, skipping frontend synchronization\n", 1251 __func__); 1252#endif 1253 } 1254 1255 softc = control_softc; 1256 1257 STAILQ_FOREACH(port, &softc->port_list, links) { 1258 if (port_type & port->port_type) 1259 { 1260#if 0 1261 printf("port %d\n", port->targ_port); 1262#endif 1263 ctl_port_online(port); 1264 } 1265 } 1266 1267 return (0); 1268} 1269 1270int 1271ctl_port_disable(ctl_port_type port_type) 1272{ 1273 struct ctl_softc *softc; 1274 struct ctl_port *port; 1275 1276 softc = control_softc; 1277 1278 STAILQ_FOREACH(port, &softc->port_list, links) { 1279 if (port_type & port->port_type) 1280 ctl_port_offline(port); 1281 } 1282 1283 return (0); 1284} 1285 1286/* 1287 * Returns 0 for success, 1 for failure. 1288 * Currently the only failure mode is if there aren't enough entries 1289 * allocated. So, in case of a failure, look at num_entries_dropped, 1290 * reallocate and try again. 1291 */ 1292int 1293ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1294 int *num_entries_filled, int *num_entries_dropped, 1295 ctl_port_type port_type, int no_virtual) 1296{ 1297 struct ctl_softc *softc; 1298 struct ctl_port *port; 1299 int entries_dropped, entries_filled; 1300 int retval; 1301 int i; 1302 1303 softc = control_softc; 1304 1305 retval = 0; 1306 entries_filled = 0; 1307 entries_dropped = 0; 1308 1309 i = 0; 1310 mtx_lock(&softc->ctl_lock); 1311 STAILQ_FOREACH(port, &softc->port_list, links) { 1312 struct ctl_port_entry *entry; 1313 1314 if ((port->port_type & port_type) == 0) 1315 continue; 1316 1317 if ((no_virtual != 0) 1318 && (port->virtual_port != 0)) 1319 continue; 1320 1321 if (entries_filled >= num_entries_alloced) { 1322 entries_dropped++; 1323 continue; 1324 } 1325 entry = &entries[i]; 1326 1327 entry->port_type = port->port_type; 1328 strlcpy(entry->port_name, port->port_name, 1329 sizeof(entry->port_name)); 1330 entry->physical_port = port->physical_port; 1331 entry->virtual_port = port->virtual_port; 1332 entry->wwnn = port->wwnn; 1333 entry->wwpn = port->wwpn; 1334 1335 i++; 1336 entries_filled++; 1337 } 1338 1339 mtx_unlock(&softc->ctl_lock); 1340 1341 if (entries_dropped > 0) 1342 retval = 1; 1343 1344 *num_entries_dropped = entries_dropped; 1345 *num_entries_filled = entries_filled; 1346 1347 return (retval); 1348} 1349 1350static void 1351ctl_ioctl_online(void *arg) 1352{ 1353 struct ctl_ioctl_info *ioctl_info; 1354 1355 ioctl_info = (struct ctl_ioctl_info *)arg; 1356 1357 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1358} 1359 1360static void 1361ctl_ioctl_offline(void *arg) 1362{ 1363 struct ctl_ioctl_info *ioctl_info; 1364 1365 ioctl_info = (struct ctl_ioctl_info *)arg; 1366 1367 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1368} 1369 1370/* 1371 * Remove an initiator by port number and initiator ID. 1372 * Returns 0 for success, -1 for failure. 1373 */ 1374int 1375ctl_remove_initiator(struct ctl_port *port, int iid) 1376{ 1377 struct ctl_softc *softc = control_softc; 1378 1379 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1380 1381 if (iid > CTL_MAX_INIT_PER_PORT) { 1382 printf("%s: initiator ID %u > maximun %u!\n", 1383 __func__, iid, CTL_MAX_INIT_PER_PORT); 1384 return (-1); 1385 } 1386 1387 mtx_lock(&softc->ctl_lock); 1388 port->wwpn_iid[iid].in_use--; 1389 port->wwpn_iid[iid].last_use = time_uptime; 1390 mtx_unlock(&softc->ctl_lock); 1391 1392 return (0); 1393} 1394 1395/* 1396 * Add an initiator to the initiator map. 1397 * Returns iid for success, < 0 for failure. 1398 */ 1399int 1400ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1401{ 1402 struct ctl_softc *softc = control_softc; 1403 time_t best_time; 1404 int i, best; 1405 1406 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1407 1408 if (iid >= CTL_MAX_INIT_PER_PORT) { 1409 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1410 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1411 free(name, M_CTL); 1412 return (-1); 1413 } 1414 1415 mtx_lock(&softc->ctl_lock); 1416 1417 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1418 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1419 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1420 iid = i; 1421 break; 1422 } 1423 if (name != NULL && port->wwpn_iid[i].name != NULL && 1424 strcmp(name, port->wwpn_iid[i].name) == 0) { 1425 iid = i; 1426 break; 1427 } 1428 } 1429 } 1430 1431 if (iid < 0) { 1432 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1433 if (port->wwpn_iid[i].in_use == 0 && 1434 port->wwpn_iid[i].wwpn == 0 && 1435 port->wwpn_iid[i].name == NULL) { 1436 iid = i; 1437 break; 1438 } 1439 } 1440 } 1441 1442 if (iid < 0) { 1443 best = -1; 1444 best_time = INT32_MAX; 1445 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1446 if (port->wwpn_iid[i].in_use == 0) { 1447 if (port->wwpn_iid[i].last_use < best_time) { 1448 best = i; 1449 best_time = port->wwpn_iid[i].last_use; 1450 } 1451 } 1452 } 1453 iid = best; 1454 } 1455 1456 if (iid < 0) { 1457 mtx_unlock(&softc->ctl_lock); 1458 free(name, M_CTL); 1459 return (-2); 1460 } 1461 1462 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1463 /* 1464 * This is not an error yet. 1465 */ 1466 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1467#if 0 1468 printf("%s: port %d iid %u WWPN %#jx arrived" 1469 " again\n", __func__, port->targ_port, 1470 iid, (uintmax_t)wwpn); 1471#endif 1472 goto take; 1473 } 1474 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1475 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1476#if 0 1477 printf("%s: port %d iid %u name '%s' arrived" 1478 " again\n", __func__, port->targ_port, 1479 iid, name); 1480#endif 1481 goto take; 1482 } 1483 1484 /* 1485 * This is an error, but what do we do about it? The 1486 * driver is telling us we have a new WWPN for this 1487 * initiator ID, so we pretty much need to use it. 1488 */ 1489 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1490 " but WWPN %#jx '%s' is still at that address\n", 1491 __func__, port->targ_port, iid, wwpn, name, 1492 (uintmax_t)port->wwpn_iid[iid].wwpn, 1493 port->wwpn_iid[iid].name); 1494 1495 /* 1496 * XXX KDM clear have_ca and ua_pending on each LUN for 1497 * this initiator. 1498 */ 1499 } 1500take: 1501 free(port->wwpn_iid[iid].name, M_CTL); 1502 port->wwpn_iid[iid].name = name; 1503 port->wwpn_iid[iid].wwpn = wwpn; 1504 port->wwpn_iid[iid].in_use++; 1505 mtx_unlock(&softc->ctl_lock); 1506 1507 return (iid); 1508} 1509 1510static int 1511ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1512{ 1513 int len; 1514 1515 switch (port->port_type) { 1516 case CTL_PORT_FC: 1517 { 1518 struct scsi_transportid_fcp *id = 1519 (struct scsi_transportid_fcp *)buf; 1520 if (port->wwpn_iid[iid].wwpn == 0) 1521 return (0); 1522 memset(id, 0, sizeof(*id)); 1523 id->format_protocol = SCSI_PROTO_FC; 1524 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1525 return (sizeof(*id)); 1526 } 1527 case CTL_PORT_ISCSI: 1528 { 1529 struct scsi_transportid_iscsi_port *id = 1530 (struct scsi_transportid_iscsi_port *)buf; 1531 if (port->wwpn_iid[iid].name == NULL) 1532 return (0); 1533 memset(id, 0, 256); 1534 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1535 SCSI_PROTO_ISCSI; 1536 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1537 len = roundup2(min(len, 252), 4); 1538 scsi_ulto2b(len, id->additional_length); 1539 return (sizeof(*id) + len); 1540 } 1541 case CTL_PORT_SAS: 1542 { 1543 struct scsi_transportid_sas *id = 1544 (struct scsi_transportid_sas *)buf; 1545 if (port->wwpn_iid[iid].wwpn == 0) 1546 return (0); 1547 memset(id, 0, sizeof(*id)); 1548 id->format_protocol = SCSI_PROTO_SAS; 1549 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1550 return (sizeof(*id)); 1551 } 1552 default: 1553 { 1554 struct scsi_transportid_spi *id = 1555 (struct scsi_transportid_spi *)buf; 1556 memset(id, 0, sizeof(*id)); 1557 id->format_protocol = SCSI_PROTO_SPI; 1558 scsi_ulto2b(iid, id->scsi_addr); 1559 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1560 return (sizeof(*id)); 1561 } 1562 } 1563} 1564 1565static int 1566ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1567{ 1568 return (0); 1569} 1570 1571static int 1572ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1573{ 1574 return (0); 1575} 1576 1577/* 1578 * Data movement routine for the CTL ioctl frontend port. 1579 */ 1580static int 1581ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1582{ 1583 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1584 struct ctl_sg_entry ext_entry, kern_entry; 1585 int ext_sglen, ext_sg_entries, kern_sg_entries; 1586 int ext_sg_start, ext_offset; 1587 int len_to_copy, len_copied; 1588 int kern_watermark, ext_watermark; 1589 int ext_sglist_malloced; 1590 int i, j; 1591 1592 ext_sglist_malloced = 0; 1593 ext_sg_start = 0; 1594 ext_offset = 0; 1595 1596 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1597 1598 /* 1599 * If this flag is set, fake the data transfer. 1600 */ 1601 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1602 ctsio->ext_data_filled = ctsio->ext_data_len; 1603 goto bailout; 1604 } 1605 1606 /* 1607 * To simplify things here, if we have a single buffer, stick it in 1608 * a S/G entry and just make it a single entry S/G list. 1609 */ 1610 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1611 int len_seen; 1612 1613 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1614 1615 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1616 M_WAITOK); 1617 ext_sglist_malloced = 1; 1618 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1619 ext_sglen) != 0) { 1620 ctl_set_internal_failure(ctsio, 1621 /*sks_valid*/ 0, 1622 /*retry_count*/ 0); 1623 goto bailout; 1624 } 1625 ext_sg_entries = ctsio->ext_sg_entries; 1626 len_seen = 0; 1627 for (i = 0; i < ext_sg_entries; i++) { 1628 if ((len_seen + ext_sglist[i].len) >= 1629 ctsio->ext_data_filled) { 1630 ext_sg_start = i; 1631 ext_offset = ctsio->ext_data_filled - len_seen; 1632 break; 1633 } 1634 len_seen += ext_sglist[i].len; 1635 } 1636 } else { 1637 ext_sglist = &ext_entry; 1638 ext_sglist->addr = ctsio->ext_data_ptr; 1639 ext_sglist->len = ctsio->ext_data_len; 1640 ext_sg_entries = 1; 1641 ext_sg_start = 0; 1642 ext_offset = ctsio->ext_data_filled; 1643 } 1644 1645 if (ctsio->kern_sg_entries > 0) { 1646 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1647 kern_sg_entries = ctsio->kern_sg_entries; 1648 } else { 1649 kern_sglist = &kern_entry; 1650 kern_sglist->addr = ctsio->kern_data_ptr; 1651 kern_sglist->len = ctsio->kern_data_len; 1652 kern_sg_entries = 1; 1653 } 1654 1655 1656 kern_watermark = 0; 1657 ext_watermark = ext_offset; 1658 len_copied = 0; 1659 for (i = ext_sg_start, j = 0; 1660 i < ext_sg_entries && j < kern_sg_entries;) { 1661 uint8_t *ext_ptr, *kern_ptr; 1662 1663 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1664 kern_sglist[j].len - kern_watermark); 1665 1666 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1667 ext_ptr = ext_ptr + ext_watermark; 1668 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1669 /* 1670 * XXX KDM fix this! 1671 */ 1672 panic("need to implement bus address support"); 1673#if 0 1674 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1675#endif 1676 } else 1677 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1678 kern_ptr = kern_ptr + kern_watermark; 1679 1680 kern_watermark += len_to_copy; 1681 ext_watermark += len_to_copy; 1682 1683 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1684 CTL_FLAG_DATA_IN) { 1685 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1686 "bytes to user\n", len_to_copy)); 1687 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1688 "to %p\n", kern_ptr, ext_ptr)); 1689 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1690 ctl_set_internal_failure(ctsio, 1691 /*sks_valid*/ 0, 1692 /*retry_count*/ 0); 1693 goto bailout; 1694 } 1695 } else { 1696 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1697 "bytes from user\n", len_to_copy)); 1698 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1699 "to %p\n", ext_ptr, kern_ptr)); 1700 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1701 ctl_set_internal_failure(ctsio, 1702 /*sks_valid*/ 0, 1703 /*retry_count*/0); 1704 goto bailout; 1705 } 1706 } 1707 1708 len_copied += len_to_copy; 1709 1710 if (ext_sglist[i].len == ext_watermark) { 1711 i++; 1712 ext_watermark = 0; 1713 } 1714 1715 if (kern_sglist[j].len == kern_watermark) { 1716 j++; 1717 kern_watermark = 0; 1718 } 1719 } 1720 1721 ctsio->ext_data_filled += len_copied; 1722 1723 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1724 "kern_sg_entries: %d\n", ext_sg_entries, 1725 kern_sg_entries)); 1726 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1727 "kern_data_len = %d\n", ctsio->ext_data_len, 1728 ctsio->kern_data_len)); 1729 1730 1731 /* XXX KDM set residual?? */ 1732bailout: 1733 1734 if (ext_sglist_malloced != 0) 1735 free(ext_sglist, M_CTL); 1736 1737 return (CTL_RETVAL_COMPLETE); 1738} 1739 1740/* 1741 * Serialize a command that went down the "wrong" side, and so was sent to 1742 * this controller for execution. The logic is a little different than the 1743 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1744 * sent back to the other side, but in the success case, we execute the 1745 * command on this side (XFER mode) or tell the other side to execute it 1746 * (SER_ONLY mode). 1747 */ 1748static int 1749ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1750{ 1751 struct ctl_softc *ctl_softc; 1752 union ctl_ha_msg msg_info; 1753 struct ctl_lun *lun; 1754 int retval = 0; 1755 uint32_t targ_lun; 1756 1757 ctl_softc = control_softc; 1758 1759 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1760 lun = ctl_softc->ctl_luns[targ_lun]; 1761 if (lun==NULL) 1762 { 1763 /* 1764 * Why isn't LUN defined? The other side wouldn't 1765 * send a cmd if the LUN is undefined. 1766 */ 1767 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1768 1769 /* "Logical unit not supported" */ 1770 ctl_set_sense_data(&msg_info.scsi.sense_data, 1771 lun, 1772 /*sense_format*/SSD_TYPE_NONE, 1773 /*current_error*/ 1, 1774 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1775 /*asc*/ 0x25, 1776 /*ascq*/ 0x00, 1777 SSD_ELEM_NONE); 1778 1779 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1780 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1781 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1782 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1783 msg_info.hdr.serializing_sc = NULL; 1784 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1785 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1786 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1787 } 1788 return(1); 1789 1790 } 1791 1792 mtx_lock(&lun->lun_lock); 1793 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1794 1795 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1796 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1797 ooa_links))) { 1798 case CTL_ACTION_BLOCK: 1799 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1800 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1801 blocked_links); 1802 break; 1803 case CTL_ACTION_PASS: 1804 case CTL_ACTION_SKIP: 1805 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1806 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1807 ctl_enqueue_rtr((union ctl_io *)ctsio); 1808 } else { 1809 1810 /* send msg back to other side */ 1811 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1812 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1813 msg_info.hdr.msg_type = CTL_MSG_R2R; 1814#if 0 1815 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1816#endif 1817 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1818 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1819 } 1820 } 1821 break; 1822 case CTL_ACTION_OVERLAP: 1823 /* OVERLAPPED COMMANDS ATTEMPTED */ 1824 ctl_set_sense_data(&msg_info.scsi.sense_data, 1825 lun, 1826 /*sense_format*/SSD_TYPE_NONE, 1827 /*current_error*/ 1, 1828 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1829 /*asc*/ 0x4E, 1830 /*ascq*/ 0x00, 1831 SSD_ELEM_NONE); 1832 1833 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1834 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1835 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1836 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1837 msg_info.hdr.serializing_sc = NULL; 1838 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1839#if 0 1840 printf("BAD JUJU:Major Bummer Overlap\n"); 1841#endif 1842 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1843 retval = 1; 1844 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1845 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1846 } 1847 break; 1848 case CTL_ACTION_OVERLAP_TAG: 1849 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1850 ctl_set_sense_data(&msg_info.scsi.sense_data, 1851 lun, 1852 /*sense_format*/SSD_TYPE_NONE, 1853 /*current_error*/ 1, 1854 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1855 /*asc*/ 0x4D, 1856 /*ascq*/ ctsio->tag_num & 0xff, 1857 SSD_ELEM_NONE); 1858 1859 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1860 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1861 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1862 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1863 msg_info.hdr.serializing_sc = NULL; 1864 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1865#if 0 1866 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1867#endif 1868 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1869 retval = 1; 1870 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1871 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1872 } 1873 break; 1874 case CTL_ACTION_ERROR: 1875 default: 1876 /* "Internal target failure" */ 1877 ctl_set_sense_data(&msg_info.scsi.sense_data, 1878 lun, 1879 /*sense_format*/SSD_TYPE_NONE, 1880 /*current_error*/ 1, 1881 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1882 /*asc*/ 0x44, 1883 /*ascq*/ 0x00, 1884 SSD_ELEM_NONE); 1885 1886 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1887 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1888 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1889 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1890 msg_info.hdr.serializing_sc = NULL; 1891 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1892#if 0 1893 printf("BAD JUJU:Major Bummer HW Error\n"); 1894#endif 1895 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1896 retval = 1; 1897 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1898 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1899 } 1900 break; 1901 } 1902 mtx_unlock(&lun->lun_lock); 1903 return (retval); 1904} 1905 1906static int 1907ctl_ioctl_submit_wait(union ctl_io *io) 1908{ 1909 struct ctl_fe_ioctl_params params; 1910 ctl_fe_ioctl_state last_state; 1911 int done, retval; 1912 1913 retval = 0; 1914 1915 bzero(¶ms, sizeof(params)); 1916 1917 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1918 cv_init(¶ms.sem, "ctlioccv"); 1919 params.state = CTL_IOCTL_INPROG; 1920 last_state = params.state; 1921 1922 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1923 1924 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1925 1926 /* This shouldn't happen */ 1927 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1928 return (retval); 1929 1930 done = 0; 1931 1932 do { 1933 mtx_lock(¶ms.ioctl_mtx); 1934 /* 1935 * Check the state here, and don't sleep if the state has 1936 * already changed (i.e. wakeup has already occured, but we 1937 * weren't waiting yet). 1938 */ 1939 if (params.state == last_state) { 1940 /* XXX KDM cv_wait_sig instead? */ 1941 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1942 } 1943 last_state = params.state; 1944 1945 switch (params.state) { 1946 case CTL_IOCTL_INPROG: 1947 /* Why did we wake up? */ 1948 /* XXX KDM error here? */ 1949 mtx_unlock(¶ms.ioctl_mtx); 1950 break; 1951 case CTL_IOCTL_DATAMOVE: 1952 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1953 1954 /* 1955 * change last_state back to INPROG to avoid 1956 * deadlock on subsequent data moves. 1957 */ 1958 params.state = last_state = CTL_IOCTL_INPROG; 1959 1960 mtx_unlock(¶ms.ioctl_mtx); 1961 ctl_ioctl_do_datamove(&io->scsiio); 1962 /* 1963 * Note that in some cases, most notably writes, 1964 * this will queue the I/O and call us back later. 1965 * In other cases, generally reads, this routine 1966 * will immediately call back and wake us up, 1967 * probably using our own context. 1968 */ 1969 io->scsiio.be_move_done(io); 1970 break; 1971 case CTL_IOCTL_DONE: 1972 mtx_unlock(¶ms.ioctl_mtx); 1973 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1974 done = 1; 1975 break; 1976 default: 1977 mtx_unlock(¶ms.ioctl_mtx); 1978 /* XXX KDM error here? */ 1979 break; 1980 } 1981 } while (done == 0); 1982 1983 mtx_destroy(¶ms.ioctl_mtx); 1984 cv_destroy(¶ms.sem); 1985 1986 return (CTL_RETVAL_COMPLETE); 1987} 1988 1989static void 1990ctl_ioctl_datamove(union ctl_io *io) 1991{ 1992 struct ctl_fe_ioctl_params *params; 1993 1994 params = (struct ctl_fe_ioctl_params *) 1995 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1996 1997 mtx_lock(¶ms->ioctl_mtx); 1998 params->state = CTL_IOCTL_DATAMOVE; 1999 cv_broadcast(¶ms->sem); 2000 mtx_unlock(¶ms->ioctl_mtx); 2001} 2002 2003static void 2004ctl_ioctl_done(union ctl_io *io) 2005{ 2006 struct ctl_fe_ioctl_params *params; 2007 2008 params = (struct ctl_fe_ioctl_params *) 2009 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2010 2011 mtx_lock(¶ms->ioctl_mtx); 2012 params->state = CTL_IOCTL_DONE; 2013 cv_broadcast(¶ms->sem); 2014 mtx_unlock(¶ms->ioctl_mtx); 2015} 2016 2017static void 2018ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2019{ 2020 struct ctl_fe_ioctl_startstop_info *sd_info; 2021 2022 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2023 2024 sd_info->hs_info.status = metatask->status; 2025 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2026 sd_info->hs_info.luns_complete = 2027 metatask->taskinfo.startstop.luns_complete; 2028 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2029 2030 cv_broadcast(&sd_info->sem); 2031} 2032 2033static void 2034ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2035{ 2036 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2037 2038 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2039 2040 mtx_lock(fe_bbr_info->lock); 2041 fe_bbr_info->bbr_info->status = metatask->status; 2042 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2043 fe_bbr_info->wakeup_done = 1; 2044 mtx_unlock(fe_bbr_info->lock); 2045 2046 cv_broadcast(&fe_bbr_info->sem); 2047} 2048 2049/* 2050 * Returns 0 for success, errno for failure. 2051 */ 2052static int 2053ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2054 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2055{ 2056 union ctl_io *io; 2057 int retval; 2058 2059 retval = 0; 2060 2061 mtx_lock(&lun->lun_lock); 2062 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2063 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2064 ooa_links)) { 2065 struct ctl_ooa_entry *entry; 2066 2067 /* 2068 * If we've got more than we can fit, just count the 2069 * remaining entries. 2070 */ 2071 if (*cur_fill_num >= ooa_hdr->alloc_num) 2072 continue; 2073 2074 entry = &kern_entries[*cur_fill_num]; 2075 2076 entry->tag_num = io->scsiio.tag_num; 2077 entry->lun_num = lun->lun; 2078#ifdef CTL_TIME_IO 2079 entry->start_bt = io->io_hdr.start_bt; 2080#endif 2081 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2082 entry->cdb_len = io->scsiio.cdb_len; 2083 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2084 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2085 2086 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2087 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2088 2089 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2090 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2091 2092 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2093 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2094 2095 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2096 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2097 } 2098 mtx_unlock(&lun->lun_lock); 2099 2100 return (retval); 2101} 2102 2103static void * 2104ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2105 size_t error_str_len) 2106{ 2107 void *kptr; 2108 2109 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2110 2111 if (copyin(user_addr, kptr, len) != 0) { 2112 snprintf(error_str, error_str_len, "Error copying %d bytes " 2113 "from user address %p to kernel address %p", len, 2114 user_addr, kptr); 2115 free(kptr, M_CTL); 2116 return (NULL); 2117 } 2118 2119 return (kptr); 2120} 2121 2122static void 2123ctl_free_args(int num_args, struct ctl_be_arg *args) 2124{ 2125 int i; 2126 2127 if (args == NULL) 2128 return; 2129 2130 for (i = 0; i < num_args; i++) { 2131 free(args[i].kname, M_CTL); 2132 free(args[i].kvalue, M_CTL); 2133 } 2134 2135 free(args, M_CTL); 2136} 2137 2138static struct ctl_be_arg * 2139ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2140 char *error_str, size_t error_str_len) 2141{ 2142 struct ctl_be_arg *args; 2143 int i; 2144 2145 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2146 error_str, error_str_len); 2147 2148 if (args == NULL) 2149 goto bailout; 2150 2151 for (i = 0; i < num_args; i++) { 2152 args[i].kname = NULL; 2153 args[i].kvalue = NULL; 2154 } 2155 2156 for (i = 0; i < num_args; i++) { 2157 uint8_t *tmpptr; 2158 2159 args[i].kname = ctl_copyin_alloc(args[i].name, 2160 args[i].namelen, error_str, error_str_len); 2161 if (args[i].kname == NULL) 2162 goto bailout; 2163 2164 if (args[i].kname[args[i].namelen - 1] != '\0') { 2165 snprintf(error_str, error_str_len, "Argument %d " 2166 "name is not NUL-terminated", i); 2167 goto bailout; 2168 } 2169 2170 if (args[i].flags & CTL_BEARG_RD) { 2171 tmpptr = ctl_copyin_alloc(args[i].value, 2172 args[i].vallen, error_str, error_str_len); 2173 if (tmpptr == NULL) 2174 goto bailout; 2175 if ((args[i].flags & CTL_BEARG_ASCII) 2176 && (tmpptr[args[i].vallen - 1] != '\0')) { 2177 snprintf(error_str, error_str_len, "Argument " 2178 "%d value is not NUL-terminated", i); 2179 goto bailout; 2180 } 2181 args[i].kvalue = tmpptr; 2182 } else { 2183 args[i].kvalue = malloc(args[i].vallen, 2184 M_CTL, M_WAITOK | M_ZERO); 2185 } 2186 } 2187 2188 return (args); 2189bailout: 2190 2191 ctl_free_args(num_args, args); 2192 2193 return (NULL); 2194} 2195 2196static void 2197ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2198{ 2199 int i; 2200 2201 for (i = 0; i < num_args; i++) { 2202 if (args[i].flags & CTL_BEARG_WR) 2203 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2204 } 2205} 2206 2207/* 2208 * Escape characters that are illegal or not recommended in XML. 2209 */ 2210int 2211ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2212{ 2213 int retval; 2214 2215 retval = 0; 2216 2217 for (; *str; str++) { 2218 switch (*str) { 2219 case '&': 2220 retval = sbuf_printf(sb, "&"); 2221 break; 2222 case '>': 2223 retval = sbuf_printf(sb, ">"); 2224 break; 2225 case '<': 2226 retval = sbuf_printf(sb, "<"); 2227 break; 2228 default: 2229 retval = sbuf_putc(sb, *str); 2230 break; 2231 } 2232 2233 if (retval != 0) 2234 break; 2235 2236 } 2237 2238 return (retval); 2239} 2240 2241static int 2242ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2243 struct thread *td) 2244{ 2245 struct ctl_softc *softc; 2246 int retval; 2247 2248 softc = control_softc; 2249 2250 retval = 0; 2251 2252 switch (cmd) { 2253 case CTL_IO: { 2254 union ctl_io *io; 2255 void *pool_tmp; 2256 2257 /* 2258 * If we haven't been "enabled", don't allow any SCSI I/O 2259 * to this FETD. 2260 */ 2261 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2262 retval = EPERM; 2263 break; 2264 } 2265 2266 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2267 if (io == NULL) { 2268 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2269 retval = ENOSPC; 2270 break; 2271 } 2272 2273 /* 2274 * Need to save the pool reference so it doesn't get 2275 * spammed by the user's ctl_io. 2276 */ 2277 pool_tmp = io->io_hdr.pool; 2278 2279 memcpy(io, (void *)addr, sizeof(*io)); 2280 2281 io->io_hdr.pool = pool_tmp; 2282 /* 2283 * No status yet, so make sure the status is set properly. 2284 */ 2285 io->io_hdr.status = CTL_STATUS_NONE; 2286 2287 /* 2288 * The user sets the initiator ID, target and LUN IDs. 2289 */ 2290 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2291 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2292 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2293 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2294 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2295 2296 retval = ctl_ioctl_submit_wait(io); 2297 2298 if (retval != 0) { 2299 ctl_free_io(io); 2300 break; 2301 } 2302 2303 memcpy((void *)addr, io, sizeof(*io)); 2304 2305 /* return this to our pool */ 2306 ctl_free_io(io); 2307 2308 break; 2309 } 2310 case CTL_ENABLE_PORT: 2311 case CTL_DISABLE_PORT: 2312 case CTL_SET_PORT_WWNS: { 2313 struct ctl_port *port; 2314 struct ctl_port_entry *entry; 2315 2316 entry = (struct ctl_port_entry *)addr; 2317 2318 mtx_lock(&softc->ctl_lock); 2319 STAILQ_FOREACH(port, &softc->port_list, links) { 2320 int action, done; 2321 2322 action = 0; 2323 done = 0; 2324 2325 if ((entry->port_type == CTL_PORT_NONE) 2326 && (entry->targ_port == port->targ_port)) { 2327 /* 2328 * If the user only wants to enable or 2329 * disable or set WWNs on a specific port, 2330 * do the operation and we're done. 2331 */ 2332 action = 1; 2333 done = 1; 2334 } else if (entry->port_type & port->port_type) { 2335 /* 2336 * Compare the user's type mask with the 2337 * particular frontend type to see if we 2338 * have a match. 2339 */ 2340 action = 1; 2341 done = 0; 2342 2343 /* 2344 * Make sure the user isn't trying to set 2345 * WWNs on multiple ports at the same time. 2346 */ 2347 if (cmd == CTL_SET_PORT_WWNS) { 2348 printf("%s: Can't set WWNs on " 2349 "multiple ports\n", __func__); 2350 retval = EINVAL; 2351 break; 2352 } 2353 } 2354 if (action != 0) { 2355 /* 2356 * XXX KDM we have to drop the lock here, 2357 * because the online/offline operations 2358 * can potentially block. We need to 2359 * reference count the frontends so they 2360 * can't go away, 2361 */ 2362 mtx_unlock(&softc->ctl_lock); 2363 2364 if (cmd == CTL_ENABLE_PORT) { 2365 struct ctl_lun *lun; 2366 2367 STAILQ_FOREACH(lun, &softc->lun_list, 2368 links) { 2369 port->lun_enable(port->targ_lun_arg, 2370 lun->target, 2371 lun->lun); 2372 } 2373 2374 ctl_port_online(port); 2375 } else if (cmd == CTL_DISABLE_PORT) { 2376 struct ctl_lun *lun; 2377 2378 ctl_port_offline(port); 2379 2380 STAILQ_FOREACH(lun, &softc->lun_list, 2381 links) { 2382 port->lun_disable( 2383 port->targ_lun_arg, 2384 lun->target, 2385 lun->lun); 2386 } 2387 } 2388 2389 mtx_lock(&softc->ctl_lock); 2390 2391 if (cmd == CTL_SET_PORT_WWNS) 2392 ctl_port_set_wwns(port, 2393 (entry->flags & CTL_PORT_WWNN_VALID) ? 2394 1 : 0, entry->wwnn, 2395 (entry->flags & CTL_PORT_WWPN_VALID) ? 2396 1 : 0, entry->wwpn); 2397 } 2398 if (done != 0) 2399 break; 2400 } 2401 mtx_unlock(&softc->ctl_lock); 2402 break; 2403 } 2404 case CTL_GET_PORT_LIST: { 2405 struct ctl_port *port; 2406 struct ctl_port_list *list; 2407 int i; 2408 2409 list = (struct ctl_port_list *)addr; 2410 2411 if (list->alloc_len != (list->alloc_num * 2412 sizeof(struct ctl_port_entry))) { 2413 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2414 "alloc_num %u * sizeof(struct ctl_port_entry) " 2415 "%zu\n", __func__, list->alloc_len, 2416 list->alloc_num, sizeof(struct ctl_port_entry)); 2417 retval = EINVAL; 2418 break; 2419 } 2420 list->fill_len = 0; 2421 list->fill_num = 0; 2422 list->dropped_num = 0; 2423 i = 0; 2424 mtx_lock(&softc->ctl_lock); 2425 STAILQ_FOREACH(port, &softc->port_list, links) { 2426 struct ctl_port_entry entry, *list_entry; 2427 2428 if (list->fill_num >= list->alloc_num) { 2429 list->dropped_num++; 2430 continue; 2431 } 2432 2433 entry.port_type = port->port_type; 2434 strlcpy(entry.port_name, port->port_name, 2435 sizeof(entry.port_name)); 2436 entry.targ_port = port->targ_port; 2437 entry.physical_port = port->physical_port; 2438 entry.virtual_port = port->virtual_port; 2439 entry.wwnn = port->wwnn; 2440 entry.wwpn = port->wwpn; 2441 if (port->status & CTL_PORT_STATUS_ONLINE) 2442 entry.online = 1; 2443 else 2444 entry.online = 0; 2445 2446 list_entry = &list->entries[i]; 2447 2448 retval = copyout(&entry, list_entry, sizeof(entry)); 2449 if (retval != 0) { 2450 printf("%s: CTL_GET_PORT_LIST: copyout " 2451 "returned %d\n", __func__, retval); 2452 break; 2453 } 2454 i++; 2455 list->fill_num++; 2456 list->fill_len += sizeof(entry); 2457 } 2458 mtx_unlock(&softc->ctl_lock); 2459 2460 /* 2461 * If this is non-zero, we had a copyout fault, so there's 2462 * probably no point in attempting to set the status inside 2463 * the structure. 2464 */ 2465 if (retval != 0) 2466 break; 2467 2468 if (list->dropped_num > 0) 2469 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2470 else 2471 list->status = CTL_PORT_LIST_OK; 2472 break; 2473 } 2474 case CTL_DUMP_OOA: { 2475 struct ctl_lun *lun; 2476 union ctl_io *io; 2477 char printbuf[128]; 2478 struct sbuf sb; 2479 2480 mtx_lock(&softc->ctl_lock); 2481 printf("Dumping OOA queues:\n"); 2482 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2483 mtx_lock(&lun->lun_lock); 2484 for (io = (union ctl_io *)TAILQ_FIRST( 2485 &lun->ooa_queue); io != NULL; 2486 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2487 ooa_links)) { 2488 sbuf_new(&sb, printbuf, sizeof(printbuf), 2489 SBUF_FIXEDLEN); 2490 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2491 (intmax_t)lun->lun, 2492 io->scsiio.tag_num, 2493 (io->io_hdr.flags & 2494 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2495 (io->io_hdr.flags & 2496 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2497 (io->io_hdr.flags & 2498 CTL_FLAG_ABORT) ? " ABORT" : "", 2499 (io->io_hdr.flags & 2500 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2501 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2502 sbuf_finish(&sb); 2503 printf("%s\n", sbuf_data(&sb)); 2504 } 2505 mtx_unlock(&lun->lun_lock); 2506 } 2507 printf("OOA queues dump done\n"); 2508 mtx_unlock(&softc->ctl_lock); 2509 break; 2510 } 2511 case CTL_GET_OOA: { 2512 struct ctl_lun *lun; 2513 struct ctl_ooa *ooa_hdr; 2514 struct ctl_ooa_entry *entries; 2515 uint32_t cur_fill_num; 2516 2517 ooa_hdr = (struct ctl_ooa *)addr; 2518 2519 if ((ooa_hdr->alloc_len == 0) 2520 || (ooa_hdr->alloc_num == 0)) { 2521 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2522 "must be non-zero\n", __func__, 2523 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2524 retval = EINVAL; 2525 break; 2526 } 2527 2528 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2529 sizeof(struct ctl_ooa_entry))) { 2530 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2531 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2532 __func__, ooa_hdr->alloc_len, 2533 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2534 retval = EINVAL; 2535 break; 2536 } 2537 2538 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2539 if (entries == NULL) { 2540 printf("%s: could not allocate %d bytes for OOA " 2541 "dump\n", __func__, ooa_hdr->alloc_len); 2542 retval = ENOMEM; 2543 break; 2544 } 2545 2546 mtx_lock(&softc->ctl_lock); 2547 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2548 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2549 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2550 mtx_unlock(&softc->ctl_lock); 2551 free(entries, M_CTL); 2552 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2553 __func__, (uintmax_t)ooa_hdr->lun_num); 2554 retval = EINVAL; 2555 break; 2556 } 2557 2558 cur_fill_num = 0; 2559 2560 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2561 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2562 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2563 ooa_hdr, entries); 2564 if (retval != 0) 2565 break; 2566 } 2567 if (retval != 0) { 2568 mtx_unlock(&softc->ctl_lock); 2569 free(entries, M_CTL); 2570 break; 2571 } 2572 } else { 2573 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2574 2575 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2576 entries); 2577 } 2578 mtx_unlock(&softc->ctl_lock); 2579 2580 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2581 ooa_hdr->fill_len = ooa_hdr->fill_num * 2582 sizeof(struct ctl_ooa_entry); 2583 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2584 if (retval != 0) { 2585 printf("%s: error copying out %d bytes for OOA dump\n", 2586 __func__, ooa_hdr->fill_len); 2587 } 2588 2589 getbintime(&ooa_hdr->cur_bt); 2590 2591 if (cur_fill_num > ooa_hdr->alloc_num) { 2592 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2593 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2594 } else { 2595 ooa_hdr->dropped_num = 0; 2596 ooa_hdr->status = CTL_OOA_OK; 2597 } 2598 2599 free(entries, M_CTL); 2600 break; 2601 } 2602 case CTL_CHECK_OOA: { 2603 union ctl_io *io; 2604 struct ctl_lun *lun; 2605 struct ctl_ooa_info *ooa_info; 2606 2607 2608 ooa_info = (struct ctl_ooa_info *)addr; 2609 2610 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2611 ooa_info->status = CTL_OOA_INVALID_LUN; 2612 break; 2613 } 2614 mtx_lock(&softc->ctl_lock); 2615 lun = softc->ctl_luns[ooa_info->lun_id]; 2616 if (lun == NULL) { 2617 mtx_unlock(&softc->ctl_lock); 2618 ooa_info->status = CTL_OOA_INVALID_LUN; 2619 break; 2620 } 2621 mtx_lock(&lun->lun_lock); 2622 mtx_unlock(&softc->ctl_lock); 2623 ooa_info->num_entries = 0; 2624 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2625 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2626 &io->io_hdr, ooa_links)) { 2627 ooa_info->num_entries++; 2628 } 2629 mtx_unlock(&lun->lun_lock); 2630 2631 ooa_info->status = CTL_OOA_SUCCESS; 2632 2633 break; 2634 } 2635 case CTL_HARD_START: 2636 case CTL_HARD_STOP: { 2637 struct ctl_fe_ioctl_startstop_info ss_info; 2638 struct cfi_metatask *metatask; 2639 struct mtx hs_mtx; 2640 2641 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2642 2643 cv_init(&ss_info.sem, "hard start/stop cv" ); 2644 2645 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2646 if (metatask == NULL) { 2647 retval = ENOMEM; 2648 mtx_destroy(&hs_mtx); 2649 break; 2650 } 2651 2652 if (cmd == CTL_HARD_START) 2653 metatask->tasktype = CFI_TASK_STARTUP; 2654 else 2655 metatask->tasktype = CFI_TASK_SHUTDOWN; 2656 2657 metatask->callback = ctl_ioctl_hard_startstop_callback; 2658 metatask->callback_arg = &ss_info; 2659 2660 cfi_action(metatask); 2661 2662 /* Wait for the callback */ 2663 mtx_lock(&hs_mtx); 2664 cv_wait_sig(&ss_info.sem, &hs_mtx); 2665 mtx_unlock(&hs_mtx); 2666 2667 /* 2668 * All information has been copied from the metatask by the 2669 * time cv_broadcast() is called, so we free the metatask here. 2670 */ 2671 cfi_free_metatask(metatask); 2672 2673 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2674 2675 mtx_destroy(&hs_mtx); 2676 break; 2677 } 2678 case CTL_BBRREAD: { 2679 struct ctl_bbrread_info *bbr_info; 2680 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2681 struct mtx bbr_mtx; 2682 struct cfi_metatask *metatask; 2683 2684 bbr_info = (struct ctl_bbrread_info *)addr; 2685 2686 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2687 2688 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2689 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2690 2691 fe_bbr_info.bbr_info = bbr_info; 2692 fe_bbr_info.lock = &bbr_mtx; 2693 2694 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2695 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2696 2697 if (metatask == NULL) { 2698 mtx_destroy(&bbr_mtx); 2699 cv_destroy(&fe_bbr_info.sem); 2700 retval = ENOMEM; 2701 break; 2702 } 2703 metatask->tasktype = CFI_TASK_BBRREAD; 2704 metatask->callback = ctl_ioctl_bbrread_callback; 2705 metatask->callback_arg = &fe_bbr_info; 2706 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2707 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2708 metatask->taskinfo.bbrread.len = bbr_info->len; 2709 2710 cfi_action(metatask); 2711 2712 mtx_lock(&bbr_mtx); 2713 while (fe_bbr_info.wakeup_done == 0) 2714 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2715 mtx_unlock(&bbr_mtx); 2716 2717 bbr_info->status = metatask->status; 2718 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2719 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2720 memcpy(&bbr_info->sense_data, 2721 &metatask->taskinfo.bbrread.sense_data, 2722 ctl_min(sizeof(bbr_info->sense_data), 2723 sizeof(metatask->taskinfo.bbrread.sense_data))); 2724 2725 cfi_free_metatask(metatask); 2726 2727 mtx_destroy(&bbr_mtx); 2728 cv_destroy(&fe_bbr_info.sem); 2729 2730 break; 2731 } 2732 case CTL_DELAY_IO: { 2733 struct ctl_io_delay_info *delay_info; 2734#ifdef CTL_IO_DELAY 2735 struct ctl_lun *lun; 2736#endif /* CTL_IO_DELAY */ 2737 2738 delay_info = (struct ctl_io_delay_info *)addr; 2739 2740#ifdef CTL_IO_DELAY 2741 mtx_lock(&softc->ctl_lock); 2742 2743 if ((delay_info->lun_id > CTL_MAX_LUNS) 2744 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2745 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2746 } else { 2747 lun = softc->ctl_luns[delay_info->lun_id]; 2748 mtx_lock(&lun->lun_lock); 2749 2750 delay_info->status = CTL_DELAY_STATUS_OK; 2751 2752 switch (delay_info->delay_type) { 2753 case CTL_DELAY_TYPE_CONT: 2754 break; 2755 case CTL_DELAY_TYPE_ONESHOT: 2756 break; 2757 default: 2758 delay_info->status = 2759 CTL_DELAY_STATUS_INVALID_TYPE; 2760 break; 2761 } 2762 2763 switch (delay_info->delay_loc) { 2764 case CTL_DELAY_LOC_DATAMOVE: 2765 lun->delay_info.datamove_type = 2766 delay_info->delay_type; 2767 lun->delay_info.datamove_delay = 2768 delay_info->delay_secs; 2769 break; 2770 case CTL_DELAY_LOC_DONE: 2771 lun->delay_info.done_type = 2772 delay_info->delay_type; 2773 lun->delay_info.done_delay = 2774 delay_info->delay_secs; 2775 break; 2776 default: 2777 delay_info->status = 2778 CTL_DELAY_STATUS_INVALID_LOC; 2779 break; 2780 } 2781 mtx_unlock(&lun->lun_lock); 2782 } 2783 2784 mtx_unlock(&softc->ctl_lock); 2785#else 2786 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2787#endif /* CTL_IO_DELAY */ 2788 break; 2789 } 2790 case CTL_REALSYNC_SET: { 2791 int *syncstate; 2792 2793 syncstate = (int *)addr; 2794 2795 mtx_lock(&softc->ctl_lock); 2796 switch (*syncstate) { 2797 case 0: 2798 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2799 break; 2800 case 1: 2801 softc->flags |= CTL_FLAG_REAL_SYNC; 2802 break; 2803 default: 2804 retval = EINVAL; 2805 break; 2806 } 2807 mtx_unlock(&softc->ctl_lock); 2808 break; 2809 } 2810 case CTL_REALSYNC_GET: { 2811 int *syncstate; 2812 2813 syncstate = (int*)addr; 2814 2815 mtx_lock(&softc->ctl_lock); 2816 if (softc->flags & CTL_FLAG_REAL_SYNC) 2817 *syncstate = 1; 2818 else 2819 *syncstate = 0; 2820 mtx_unlock(&softc->ctl_lock); 2821 2822 break; 2823 } 2824 case CTL_SETSYNC: 2825 case CTL_GETSYNC: { 2826 struct ctl_sync_info *sync_info; 2827 struct ctl_lun *lun; 2828 2829 sync_info = (struct ctl_sync_info *)addr; 2830 2831 mtx_lock(&softc->ctl_lock); 2832 lun = softc->ctl_luns[sync_info->lun_id]; 2833 if (lun == NULL) { 2834 mtx_unlock(&softc->ctl_lock); 2835 sync_info->status = CTL_GS_SYNC_NO_LUN; 2836 } 2837 /* 2838 * Get or set the sync interval. We're not bounds checking 2839 * in the set case, hopefully the user won't do something 2840 * silly. 2841 */ 2842 mtx_lock(&lun->lun_lock); 2843 mtx_unlock(&softc->ctl_lock); 2844 if (cmd == CTL_GETSYNC) 2845 sync_info->sync_interval = lun->sync_interval; 2846 else 2847 lun->sync_interval = sync_info->sync_interval; 2848 mtx_unlock(&lun->lun_lock); 2849 2850 sync_info->status = CTL_GS_SYNC_OK; 2851 2852 break; 2853 } 2854 case CTL_GETSTATS: { 2855 struct ctl_stats *stats; 2856 struct ctl_lun *lun; 2857 int i; 2858 2859 stats = (struct ctl_stats *)addr; 2860 2861 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2862 stats->alloc_len) { 2863 stats->status = CTL_SS_NEED_MORE_SPACE; 2864 stats->num_luns = softc->num_luns; 2865 break; 2866 } 2867 /* 2868 * XXX KDM no locking here. If the LUN list changes, 2869 * things can blow up. 2870 */ 2871 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2872 i++, lun = STAILQ_NEXT(lun, links)) { 2873 retval = copyout(&lun->stats, &stats->lun_stats[i], 2874 sizeof(lun->stats)); 2875 if (retval != 0) 2876 break; 2877 } 2878 stats->num_luns = softc->num_luns; 2879 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2880 softc->num_luns; 2881 stats->status = CTL_SS_OK; 2882#ifdef CTL_TIME_IO 2883 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2884#else 2885 stats->flags = CTL_STATS_FLAG_NONE; 2886#endif 2887 getnanouptime(&stats->timestamp); 2888 break; 2889 } 2890 case CTL_ERROR_INJECT: { 2891 struct ctl_error_desc *err_desc, *new_err_desc; 2892 struct ctl_lun *lun; 2893 2894 err_desc = (struct ctl_error_desc *)addr; 2895 2896 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2897 M_WAITOK | M_ZERO); 2898 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2899 2900 mtx_lock(&softc->ctl_lock); 2901 lun = softc->ctl_luns[err_desc->lun_id]; 2902 if (lun == NULL) { 2903 mtx_unlock(&softc->ctl_lock); 2904 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2905 __func__, (uintmax_t)err_desc->lun_id); 2906 retval = EINVAL; 2907 break; 2908 } 2909 mtx_lock(&lun->lun_lock); 2910 mtx_unlock(&softc->ctl_lock); 2911 2912 /* 2913 * We could do some checking here to verify the validity 2914 * of the request, but given the complexity of error 2915 * injection requests, the checking logic would be fairly 2916 * complex. 2917 * 2918 * For now, if the request is invalid, it just won't get 2919 * executed and might get deleted. 2920 */ 2921 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2922 2923 /* 2924 * XXX KDM check to make sure the serial number is unique, 2925 * in case we somehow manage to wrap. That shouldn't 2926 * happen for a very long time, but it's the right thing to 2927 * do. 2928 */ 2929 new_err_desc->serial = lun->error_serial; 2930 err_desc->serial = lun->error_serial; 2931 lun->error_serial++; 2932 2933 mtx_unlock(&lun->lun_lock); 2934 break; 2935 } 2936 case CTL_ERROR_INJECT_DELETE: { 2937 struct ctl_error_desc *delete_desc, *desc, *desc2; 2938 struct ctl_lun *lun; 2939 int delete_done; 2940 2941 delete_desc = (struct ctl_error_desc *)addr; 2942 delete_done = 0; 2943 2944 mtx_lock(&softc->ctl_lock); 2945 lun = softc->ctl_luns[delete_desc->lun_id]; 2946 if (lun == NULL) { 2947 mtx_unlock(&softc->ctl_lock); 2948 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2949 __func__, (uintmax_t)delete_desc->lun_id); 2950 retval = EINVAL; 2951 break; 2952 } 2953 mtx_lock(&lun->lun_lock); 2954 mtx_unlock(&softc->ctl_lock); 2955 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2956 if (desc->serial != delete_desc->serial) 2957 continue; 2958 2959 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2960 links); 2961 free(desc, M_CTL); 2962 delete_done = 1; 2963 } 2964 mtx_unlock(&lun->lun_lock); 2965 if (delete_done == 0) { 2966 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2967 "error serial %ju on LUN %u\n", __func__, 2968 delete_desc->serial, delete_desc->lun_id); 2969 retval = EINVAL; 2970 break; 2971 } 2972 break; 2973 } 2974 case CTL_DUMP_STRUCTS: { 2975 int i, j, k, idx; 2976 struct ctl_port *port; 2977 struct ctl_frontend *fe; 2978 2979 mtx_lock(&softc->ctl_lock); 2980 printf("CTL Persistent Reservation information start:\n"); 2981 for (i = 0; i < CTL_MAX_LUNS; i++) { 2982 struct ctl_lun *lun; 2983 2984 lun = softc->ctl_luns[i]; 2985 2986 if ((lun == NULL) 2987 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2988 continue; 2989 2990 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2991 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2992 idx = j * CTL_MAX_INIT_PER_PORT + k; 2993 if (lun->per_res[idx].registered == 0) 2994 continue; 2995 printf(" LUN %d port %d iid %d key " 2996 "%#jx\n", i, j, k, 2997 (uintmax_t)scsi_8btou64( 2998 lun->per_res[idx].res_key.key)); 2999 } 3000 } 3001 } 3002 printf("CTL Persistent Reservation information end\n"); 3003 printf("CTL Ports:\n"); 3004 STAILQ_FOREACH(port, &softc->port_list, links) { 3005 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3006 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3007 port->frontend->name, port->port_type, 3008 port->physical_port, port->virtual_port, 3009 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3010 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3011 if (port->wwpn_iid[j].in_use == 0 && 3012 port->wwpn_iid[j].wwpn == 0 && 3013 port->wwpn_iid[j].name == NULL) 3014 continue; 3015 3016 printf(" iid %u use %d WWPN %#jx '%s'\n", 3017 j, port->wwpn_iid[j].in_use, 3018 (uintmax_t)port->wwpn_iid[j].wwpn, 3019 port->wwpn_iid[j].name); 3020 } 3021 } 3022 printf("CTL Port information end\n"); 3023 mtx_unlock(&softc->ctl_lock); 3024 /* 3025 * XXX KDM calling this without a lock. We'd likely want 3026 * to drop the lock before calling the frontend's dump 3027 * routine anyway. 3028 */ 3029 printf("CTL Frontends:\n"); 3030 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3031 printf(" Frontend '%s'\n", fe->name); 3032 if (fe->fe_dump != NULL) 3033 fe->fe_dump(); 3034 } 3035 printf("CTL Frontend information end\n"); 3036 break; 3037 } 3038 case CTL_LUN_REQ: { 3039 struct ctl_lun_req *lun_req; 3040 struct ctl_backend_driver *backend; 3041 3042 lun_req = (struct ctl_lun_req *)addr; 3043 3044 backend = ctl_backend_find(lun_req->backend); 3045 if (backend == NULL) { 3046 lun_req->status = CTL_LUN_ERROR; 3047 snprintf(lun_req->error_str, 3048 sizeof(lun_req->error_str), 3049 "Backend \"%s\" not found.", 3050 lun_req->backend); 3051 break; 3052 } 3053 if (lun_req->num_be_args > 0) { 3054 lun_req->kern_be_args = ctl_copyin_args( 3055 lun_req->num_be_args, 3056 lun_req->be_args, 3057 lun_req->error_str, 3058 sizeof(lun_req->error_str)); 3059 if (lun_req->kern_be_args == NULL) { 3060 lun_req->status = CTL_LUN_ERROR; 3061 break; 3062 } 3063 } 3064 3065 retval = backend->ioctl(dev, cmd, addr, flag, td); 3066 3067 if (lun_req->num_be_args > 0) { 3068 ctl_copyout_args(lun_req->num_be_args, 3069 lun_req->kern_be_args); 3070 ctl_free_args(lun_req->num_be_args, 3071 lun_req->kern_be_args); 3072 } 3073 break; 3074 } 3075 case CTL_LUN_LIST: { 3076 struct sbuf *sb; 3077 struct ctl_lun *lun; 3078 struct ctl_lun_list *list; 3079 struct ctl_option *opt; 3080 3081 list = (struct ctl_lun_list *)addr; 3082 3083 /* 3084 * Allocate a fixed length sbuf here, based on the length 3085 * of the user's buffer. We could allocate an auto-extending 3086 * buffer, and then tell the user how much larger our 3087 * amount of data is than his buffer, but that presents 3088 * some problems: 3089 * 3090 * 1. The sbuf(9) routines use a blocking malloc, and so 3091 * we can't hold a lock while calling them with an 3092 * auto-extending buffer. 3093 * 3094 * 2. There is not currently a LUN reference counting 3095 * mechanism, outside of outstanding transactions on 3096 * the LUN's OOA queue. So a LUN could go away on us 3097 * while we're getting the LUN number, backend-specific 3098 * information, etc. Thus, given the way things 3099 * currently work, we need to hold the CTL lock while 3100 * grabbing LUN information. 3101 * 3102 * So, from the user's standpoint, the best thing to do is 3103 * allocate what he thinks is a reasonable buffer length, 3104 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3105 * double the buffer length and try again. (And repeat 3106 * that until he succeeds.) 3107 */ 3108 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3109 if (sb == NULL) { 3110 list->status = CTL_LUN_LIST_ERROR; 3111 snprintf(list->error_str, sizeof(list->error_str), 3112 "Unable to allocate %d bytes for LUN list", 3113 list->alloc_len); 3114 break; 3115 } 3116 3117 sbuf_printf(sb, "<ctllunlist>\n"); 3118 3119 mtx_lock(&softc->ctl_lock); 3120 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3121 mtx_lock(&lun->lun_lock); 3122 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3123 (uintmax_t)lun->lun); 3124 3125 /* 3126 * Bail out as soon as we see that we've overfilled 3127 * the buffer. 3128 */ 3129 if (retval != 0) 3130 break; 3131 3132 retval = sbuf_printf(sb, "\t<backend_type>%s" 3133 "</backend_type>\n", 3134 (lun->backend == NULL) ? "none" : 3135 lun->backend->name); 3136 3137 if (retval != 0) 3138 break; 3139 3140 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3141 lun->be_lun->lun_type); 3142 3143 if (retval != 0) 3144 break; 3145 3146 if (lun->backend == NULL) { 3147 retval = sbuf_printf(sb, "</lun>\n"); 3148 if (retval != 0) 3149 break; 3150 continue; 3151 } 3152 3153 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3154 (lun->be_lun->maxlba > 0) ? 3155 lun->be_lun->maxlba + 1 : 0); 3156 3157 if (retval != 0) 3158 break; 3159 3160 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3161 lun->be_lun->blocksize); 3162 3163 if (retval != 0) 3164 break; 3165 3166 retval = sbuf_printf(sb, "\t<serial_number>"); 3167 3168 if (retval != 0) 3169 break; 3170 3171 retval = ctl_sbuf_printf_esc(sb, 3172 lun->be_lun->serial_num); 3173 3174 if (retval != 0) 3175 break; 3176 3177 retval = sbuf_printf(sb, "</serial_number>\n"); 3178 3179 if (retval != 0) 3180 break; 3181 3182 retval = sbuf_printf(sb, "\t<device_id>"); 3183 3184 if (retval != 0) 3185 break; 3186 3187 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3188 3189 if (retval != 0) 3190 break; 3191 3192 retval = sbuf_printf(sb, "</device_id>\n"); 3193 3194 if (retval != 0) 3195 break; 3196 3197 if (lun->backend->lun_info != NULL) { 3198 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3199 if (retval != 0) 3200 break; 3201 } 3202 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3203 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3204 opt->name, opt->value, opt->name); 3205 if (retval != 0) 3206 break; 3207 } 3208 3209 retval = sbuf_printf(sb, "</lun>\n"); 3210 3211 if (retval != 0) 3212 break; 3213 mtx_unlock(&lun->lun_lock); 3214 } 3215 if (lun != NULL) 3216 mtx_unlock(&lun->lun_lock); 3217 mtx_unlock(&softc->ctl_lock); 3218 3219 if ((retval != 0) 3220 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3221 retval = 0; 3222 sbuf_delete(sb); 3223 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3224 snprintf(list->error_str, sizeof(list->error_str), 3225 "Out of space, %d bytes is too small", 3226 list->alloc_len); 3227 break; 3228 } 3229 3230 sbuf_finish(sb); 3231 3232 retval = copyout(sbuf_data(sb), list->lun_xml, 3233 sbuf_len(sb) + 1); 3234 3235 list->fill_len = sbuf_len(sb) + 1; 3236 list->status = CTL_LUN_LIST_OK; 3237 sbuf_delete(sb); 3238 break; 3239 } 3240 case CTL_ISCSI: { 3241 struct ctl_iscsi *ci; 3242 struct ctl_frontend *fe; 3243 3244 ci = (struct ctl_iscsi *)addr; 3245 3246 fe = ctl_frontend_find("iscsi"); 3247 if (fe == NULL) { 3248 ci->status = CTL_ISCSI_ERROR; 3249 snprintf(ci->error_str, sizeof(ci->error_str), 3250 "Frontend \"iscsi\" not found."); 3251 break; 3252 } 3253 3254 retval = fe->ioctl(dev, cmd, addr, flag, td); 3255 break; 3256 } 3257 case CTL_PORT_REQ: { 3258 struct ctl_req *req; 3259 struct ctl_frontend *fe; 3260 3261 req = (struct ctl_req *)addr; 3262 3263 fe = ctl_frontend_find(req->driver); 3264 if (fe == NULL) { 3265 req->status = CTL_LUN_ERROR; 3266 snprintf(req->error_str, sizeof(req->error_str), 3267 "Frontend \"%s\" not found.", req->driver); 3268 break; 3269 } 3270 if (req->num_args > 0) { 3271 req->kern_args = ctl_copyin_args(req->num_args, 3272 req->args, req->error_str, sizeof(req->error_str)); 3273 if (req->kern_args == NULL) { 3274 req->status = CTL_LUN_ERROR; 3275 break; 3276 } 3277 } 3278 3279 retval = fe->ioctl(dev, cmd, addr, flag, td); 3280 3281 if (req->num_args > 0) { 3282 ctl_copyout_args(req->num_args, req->kern_args); 3283 ctl_free_args(req->num_args, req->kern_args); 3284 } 3285 break; 3286 } 3287 case CTL_PORT_LIST: { 3288 struct sbuf *sb; 3289 struct ctl_port *port; 3290 struct ctl_lun_list *list; 3291 struct ctl_option *opt; 3292 3293 list = (struct ctl_lun_list *)addr; 3294 3295 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3296 if (sb == NULL) { 3297 list->status = CTL_LUN_LIST_ERROR; 3298 snprintf(list->error_str, sizeof(list->error_str), 3299 "Unable to allocate %d bytes for LUN list", 3300 list->alloc_len); 3301 break; 3302 } 3303 3304 sbuf_printf(sb, "<ctlportlist>\n"); 3305 3306 mtx_lock(&softc->ctl_lock); 3307 STAILQ_FOREACH(port, &softc->port_list, links) { 3308 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3309 (uintmax_t)port->targ_port); 3310 3311 /* 3312 * Bail out as soon as we see that we've overfilled 3313 * the buffer. 3314 */ 3315 if (retval != 0) 3316 break; 3317 3318 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3319 "</frontend_type>\n", port->frontend->name); 3320 if (retval != 0) 3321 break; 3322 3323 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3324 port->port_type); 3325 if (retval != 0) 3326 break; 3327 3328 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3329 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3330 if (retval != 0) 3331 break; 3332 3333 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3334 port->port_name); 3335 if (retval != 0) 3336 break; 3337 3338 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3339 port->physical_port); 3340 if (retval != 0) 3341 break; 3342 3343 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3344 port->virtual_port); 3345 if (retval != 0) 3346 break; 3347 3348 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3349 (uintmax_t)port->wwnn); 3350 if (retval != 0) 3351 break; 3352 3353 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3354 (uintmax_t)port->wwpn); 3355 if (retval != 0) 3356 break; 3357 3358 if (port->port_info != NULL) { 3359 retval = port->port_info(port->onoff_arg, sb); 3360 if (retval != 0) 3361 break; 3362 } 3363 STAILQ_FOREACH(opt, &port->options, links) { 3364 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3365 opt->name, opt->value, opt->name); 3366 if (retval != 0) 3367 break; 3368 } 3369 3370 retval = sbuf_printf(sb, "</targ_port>\n"); 3371 if (retval != 0) 3372 break; 3373 } 3374 mtx_unlock(&softc->ctl_lock); 3375 3376 if ((retval != 0) 3377 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3378 retval = 0; 3379 sbuf_delete(sb); 3380 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3381 snprintf(list->error_str, sizeof(list->error_str), 3382 "Out of space, %d bytes is too small", 3383 list->alloc_len); 3384 break; 3385 } 3386 3387 sbuf_finish(sb); 3388 3389 retval = copyout(sbuf_data(sb), list->lun_xml, 3390 sbuf_len(sb) + 1); 3391 3392 list->fill_len = sbuf_len(sb) + 1; 3393 list->status = CTL_LUN_LIST_OK; 3394 sbuf_delete(sb); 3395 break; 3396 } 3397 default: { 3398 /* XXX KDM should we fix this? */ 3399#if 0 3400 struct ctl_backend_driver *backend; 3401 unsigned int type; 3402 int found; 3403 3404 found = 0; 3405 3406 /* 3407 * We encode the backend type as the ioctl type for backend 3408 * ioctls. So parse it out here, and then search for a 3409 * backend of this type. 3410 */ 3411 type = _IOC_TYPE(cmd); 3412 3413 STAILQ_FOREACH(backend, &softc->be_list, links) { 3414 if (backend->type == type) { 3415 found = 1; 3416 break; 3417 } 3418 } 3419 if (found == 0) { 3420 printf("ctl: unknown ioctl command %#lx or backend " 3421 "%d\n", cmd, type); 3422 retval = EINVAL; 3423 break; 3424 } 3425 retval = backend->ioctl(dev, cmd, addr, flag, td); 3426#endif 3427 retval = ENOTTY; 3428 break; 3429 } 3430 } 3431 return (retval); 3432} 3433 3434uint32_t 3435ctl_get_initindex(struct ctl_nexus *nexus) 3436{ 3437 if (nexus->targ_port < CTL_MAX_PORTS) 3438 return (nexus->initid.id + 3439 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3440 else 3441 return (nexus->initid.id + 3442 ((nexus->targ_port - CTL_MAX_PORTS) * 3443 CTL_MAX_INIT_PER_PORT)); 3444} 3445 3446uint32_t 3447ctl_get_resindex(struct ctl_nexus *nexus) 3448{ 3449 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3450} 3451 3452uint32_t 3453ctl_port_idx(int port_num) 3454{ 3455 if (port_num < CTL_MAX_PORTS) 3456 return(port_num); 3457 else 3458 return(port_num - CTL_MAX_PORTS); 3459} 3460 3461static uint32_t 3462ctl_map_lun(int port_num, uint32_t lun_id) 3463{ 3464 struct ctl_port *port; 3465 3466 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3467 if (port == NULL) 3468 return (UINT32_MAX); 3469 if (port->lun_map == NULL) 3470 return (lun_id); 3471 return (port->lun_map(port->targ_lun_arg, lun_id)); 3472} 3473 3474static uint32_t 3475ctl_map_lun_back(int port_num, uint32_t lun_id) 3476{ 3477 struct ctl_port *port; 3478 uint32_t i; 3479 3480 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3481 if (port->lun_map == NULL) 3482 return (lun_id); 3483 for (i = 0; i < CTL_MAX_LUNS; i++) { 3484 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3485 return (i); 3486 } 3487 return (UINT32_MAX); 3488} 3489 3490/* 3491 * Note: This only works for bitmask sizes that are at least 32 bits, and 3492 * that are a power of 2. 3493 */ 3494int 3495ctl_ffz(uint32_t *mask, uint32_t size) 3496{ 3497 uint32_t num_chunks, num_pieces; 3498 int i, j; 3499 3500 num_chunks = (size >> 5); 3501 if (num_chunks == 0) 3502 num_chunks++; 3503 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3504 3505 for (i = 0; i < num_chunks; i++) { 3506 for (j = 0; j < num_pieces; j++) { 3507 if ((mask[i] & (1 << j)) == 0) 3508 return ((i << 5) + j); 3509 } 3510 } 3511 3512 return (-1); 3513} 3514 3515int 3516ctl_set_mask(uint32_t *mask, uint32_t bit) 3517{ 3518 uint32_t chunk, piece; 3519 3520 chunk = bit >> 5; 3521 piece = bit % (sizeof(uint32_t) * 8); 3522 3523 if ((mask[chunk] & (1 << piece)) != 0) 3524 return (-1); 3525 else 3526 mask[chunk] |= (1 << piece); 3527 3528 return (0); 3529} 3530 3531int 3532ctl_clear_mask(uint32_t *mask, uint32_t bit) 3533{ 3534 uint32_t chunk, piece; 3535 3536 chunk = bit >> 5; 3537 piece = bit % (sizeof(uint32_t) * 8); 3538 3539 if ((mask[chunk] & (1 << piece)) == 0) 3540 return (-1); 3541 else 3542 mask[chunk] &= ~(1 << piece); 3543 3544 return (0); 3545} 3546 3547int 3548ctl_is_set(uint32_t *mask, uint32_t bit) 3549{ 3550 uint32_t chunk, piece; 3551 3552 chunk = bit >> 5; 3553 piece = bit % (sizeof(uint32_t) * 8); 3554 3555 if ((mask[chunk] & (1 << piece)) == 0) 3556 return (0); 3557 else 3558 return (1); 3559} 3560 3561#ifdef unused 3562/* 3563 * The bus, target and lun are optional, they can be filled in later. 3564 * can_wait is used to determine whether we can wait on the malloc or not. 3565 */ 3566union ctl_io* 3567ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3568 uint32_t targ_lun, int can_wait) 3569{ 3570 union ctl_io *io; 3571 3572 if (can_wait) 3573 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3574 else 3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3576 3577 if (io != NULL) { 3578 io->io_hdr.io_type = io_type; 3579 io->io_hdr.targ_port = targ_port; 3580 /* 3581 * XXX KDM this needs to change/go away. We need to move 3582 * to a preallocated pool of ctl_scsiio structures. 3583 */ 3584 io->io_hdr.nexus.targ_target.id = targ_target; 3585 io->io_hdr.nexus.targ_lun = targ_lun; 3586 } 3587 3588 return (io); 3589} 3590 3591void 3592ctl_kfree_io(union ctl_io *io) 3593{ 3594 free(io, M_CTL); 3595} 3596#endif /* unused */ 3597 3598/* 3599 * ctl_softc, pool_type, total_ctl_io are passed in. 3600 * npool is passed out. 3601 */ 3602int 3603ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3604 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3605{ 3606 uint32_t i; 3607 union ctl_io *cur_io, *next_io; 3608 struct ctl_io_pool *pool; 3609 int retval; 3610 3611 retval = 0; 3612 3613 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3614 M_NOWAIT | M_ZERO); 3615 if (pool == NULL) { 3616 retval = ENOMEM; 3617 goto bailout; 3618 } 3619 3620 pool->type = pool_type; 3621 pool->ctl_softc = ctl_softc; 3622 3623 mtx_lock(&ctl_softc->pool_lock); 3624 pool->id = ctl_softc->cur_pool_id++; 3625 mtx_unlock(&ctl_softc->pool_lock); 3626 3627 pool->flags = CTL_POOL_FLAG_NONE; 3628 pool->refcount = 1; /* Reference for validity. */ 3629 STAILQ_INIT(&pool->free_queue); 3630 3631 /* 3632 * XXX KDM other options here: 3633 * - allocate a page at a time 3634 * - allocate one big chunk of memory. 3635 * Page allocation might work well, but would take a little more 3636 * tracking. 3637 */ 3638 for (i = 0; i < total_ctl_io; i++) { 3639 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3640 M_NOWAIT); 3641 if (cur_io == NULL) { 3642 retval = ENOMEM; 3643 break; 3644 } 3645 cur_io->io_hdr.pool = pool; 3646 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3647 pool->total_ctl_io++; 3648 pool->free_ctl_io++; 3649 } 3650 3651 if (retval != 0) { 3652 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3653 cur_io != NULL; cur_io = next_io) { 3654 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3655 links); 3656 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3657 ctl_io_hdr, links); 3658 free(cur_io, M_CTLIO); 3659 } 3660 3661 free(pool, M_CTL); 3662 goto bailout; 3663 } 3664 mtx_lock(&ctl_softc->pool_lock); 3665 ctl_softc->num_pools++; 3666 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3667 /* 3668 * Increment our usage count if this is an external consumer, so we 3669 * can't get unloaded until the external consumer (most likely a 3670 * FETD) unloads and frees his pool. 3671 * 3672 * XXX KDM will this increment the caller's module use count, or 3673 * mine? 3674 */ 3675#if 0 3676 if ((pool_type != CTL_POOL_EMERGENCY) 3677 && (pool_type != CTL_POOL_INTERNAL) 3678 && (pool_type != CTL_POOL_4OTHERSC)) 3679 MOD_INC_USE_COUNT; 3680#endif 3681 3682 mtx_unlock(&ctl_softc->pool_lock); 3683 3684 *npool = pool; 3685 3686bailout: 3687 3688 return (retval); 3689} 3690 3691static int 3692ctl_pool_acquire(struct ctl_io_pool *pool) 3693{ 3694 3695 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3696 3697 if (pool->flags & CTL_POOL_FLAG_INVALID) 3698 return (EINVAL); 3699 3700 pool->refcount++; 3701 3702 return (0); 3703} 3704 3705static void 3706ctl_pool_release(struct ctl_io_pool *pool) 3707{ 3708 struct ctl_softc *ctl_softc = pool->ctl_softc; 3709 union ctl_io *io; 3710 3711 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3712 3713 if (--pool->refcount != 0) 3714 return; 3715 3716 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3717 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3718 links); 3719 free(io, M_CTLIO); 3720 } 3721 3722 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3723 ctl_softc->num_pools--; 3724 3725 /* 3726 * XXX KDM will this decrement the caller's usage count or mine? 3727 */ 3728#if 0 3729 if ((pool->type != CTL_POOL_EMERGENCY) 3730 && (pool->type != CTL_POOL_INTERNAL) 3731 && (pool->type != CTL_POOL_4OTHERSC)) 3732 MOD_DEC_USE_COUNT; 3733#endif 3734 3735 free(pool, M_CTL); 3736} 3737 3738void 3739ctl_pool_free(struct ctl_io_pool *pool) 3740{ 3741 struct ctl_softc *ctl_softc; 3742 3743 if (pool == NULL) 3744 return; 3745 3746 ctl_softc = pool->ctl_softc; 3747 mtx_lock(&ctl_softc->pool_lock); 3748 pool->flags |= CTL_POOL_FLAG_INVALID; 3749 ctl_pool_release(pool); 3750 mtx_unlock(&ctl_softc->pool_lock); 3751} 3752 3753/* 3754 * This routine does not block (except for spinlocks of course). 3755 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3756 * possible. 3757 */ 3758union ctl_io * 3759ctl_alloc_io(void *pool_ref) 3760{ 3761 union ctl_io *io; 3762 struct ctl_softc *ctl_softc; 3763 struct ctl_io_pool *pool, *npool; 3764 struct ctl_io_pool *emergency_pool; 3765 3766 pool = (struct ctl_io_pool *)pool_ref; 3767 3768 if (pool == NULL) { 3769 printf("%s: pool is NULL\n", __func__); 3770 return (NULL); 3771 } 3772 3773 emergency_pool = NULL; 3774 3775 ctl_softc = pool->ctl_softc; 3776 3777 mtx_lock(&ctl_softc->pool_lock); 3778 /* 3779 * First, try to get the io structure from the user's pool. 3780 */ 3781 if (ctl_pool_acquire(pool) == 0) { 3782 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3783 if (io != NULL) { 3784 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3785 pool->total_allocated++; 3786 pool->free_ctl_io--; 3787 mtx_unlock(&ctl_softc->pool_lock); 3788 return (io); 3789 } else 3790 ctl_pool_release(pool); 3791 } 3792 /* 3793 * If he doesn't have any io structures left, search for an 3794 * emergency pool and grab one from there. 3795 */ 3796 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3797 if (npool->type != CTL_POOL_EMERGENCY) 3798 continue; 3799 3800 if (ctl_pool_acquire(npool) != 0) 3801 continue; 3802 3803 emergency_pool = npool; 3804 3805 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3806 if (io != NULL) { 3807 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3808 npool->total_allocated++; 3809 npool->free_ctl_io--; 3810 mtx_unlock(&ctl_softc->pool_lock); 3811 return (io); 3812 } else 3813 ctl_pool_release(npool); 3814 } 3815 3816 /* Drop the spinlock before we malloc */ 3817 mtx_unlock(&ctl_softc->pool_lock); 3818 3819 /* 3820 * The emergency pool (if it exists) didn't have one, so try an 3821 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3822 */ 3823 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3824 if (io != NULL) { 3825 /* 3826 * If the emergency pool exists but is empty, add this 3827 * ctl_io to its list when it gets freed. 3828 */ 3829 if (emergency_pool != NULL) { 3830 mtx_lock(&ctl_softc->pool_lock); 3831 if (ctl_pool_acquire(emergency_pool) == 0) { 3832 io->io_hdr.pool = emergency_pool; 3833 emergency_pool->total_ctl_io++; 3834 /* 3835 * Need to bump this, otherwise 3836 * total_allocated and total_freed won't 3837 * match when we no longer have anything 3838 * outstanding. 3839 */ 3840 emergency_pool->total_allocated++; 3841 } 3842 mtx_unlock(&ctl_softc->pool_lock); 3843 } else 3844 io->io_hdr.pool = NULL; 3845 } 3846 3847 return (io); 3848} 3849 3850void 3851ctl_free_io(union ctl_io *io) 3852{ 3853 if (io == NULL) 3854 return; 3855 3856 /* 3857 * If this ctl_io has a pool, return it to that pool. 3858 */ 3859 if (io->io_hdr.pool != NULL) { 3860 struct ctl_io_pool *pool; 3861 3862 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3863 mtx_lock(&pool->ctl_softc->pool_lock); 3864 io->io_hdr.io_type = 0xff; 3865 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3866 pool->total_freed++; 3867 pool->free_ctl_io++; 3868 ctl_pool_release(pool); 3869 mtx_unlock(&pool->ctl_softc->pool_lock); 3870 } else { 3871 /* 3872 * Otherwise, just free it. We probably malloced it and 3873 * the emergency pool wasn't available. 3874 */ 3875 free(io, M_CTLIO); 3876 } 3877 3878} 3879 3880void 3881ctl_zero_io(union ctl_io *io) 3882{ 3883 void *pool_ref; 3884 3885 if (io == NULL) 3886 return; 3887 3888 /* 3889 * May need to preserve linked list pointers at some point too. 3890 */ 3891 pool_ref = io->io_hdr.pool; 3892 3893 memset(io, 0, sizeof(*io)); 3894 3895 io->io_hdr.pool = pool_ref; 3896} 3897 3898/* 3899 * This routine is currently used for internal copies of ctl_ios that need 3900 * to persist for some reason after we've already returned status to the 3901 * FETD. (Thus the flag set.) 3902 * 3903 * XXX XXX 3904 * Note that this makes a blind copy of all fields in the ctl_io, except 3905 * for the pool reference. This includes any memory that has been 3906 * allocated! That memory will no longer be valid after done has been 3907 * called, so this would be VERY DANGEROUS for command that actually does 3908 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3909 * start and stop commands, which don't transfer any data, so this is not a 3910 * problem. If it is used for anything else, the caller would also need to 3911 * allocate data buffer space and this routine would need to be modified to 3912 * copy the data buffer(s) as well. 3913 */ 3914void 3915ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3916{ 3917 void *pool_ref; 3918 3919 if ((src == NULL) 3920 || (dest == NULL)) 3921 return; 3922 3923 /* 3924 * May need to preserve linked list pointers at some point too. 3925 */ 3926 pool_ref = dest->io_hdr.pool; 3927 3928 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3929 3930 dest->io_hdr.pool = pool_ref; 3931 /* 3932 * We need to know that this is an internal copy, and doesn't need 3933 * to get passed back to the FETD that allocated it. 3934 */ 3935 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3936} 3937 3938#ifdef NEEDTOPORT 3939static void 3940ctl_update_power_subpage(struct copan_power_subpage *page) 3941{ 3942 int num_luns, num_partitions, config_type; 3943 struct ctl_softc *softc; 3944 cs_BOOL_t aor_present, shelf_50pct_power; 3945 cs_raidset_personality_t rs_type; 3946 int max_active_luns; 3947 3948 softc = control_softc; 3949 3950 /* subtract out the processor LUN */ 3951 num_luns = softc->num_luns - 1; 3952 /* 3953 * Default to 7 LUNs active, which was the only number we allowed 3954 * in the past. 3955 */ 3956 max_active_luns = 7; 3957 3958 num_partitions = config_GetRsPartitionInfo(); 3959 config_type = config_GetConfigType(); 3960 shelf_50pct_power = config_GetShelfPowerMode(); 3961 aor_present = config_IsAorRsPresent(); 3962 3963 rs_type = ddb_GetRsRaidType(1); 3964 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3965 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3966 EPRINT(0, "Unsupported RS type %d!", rs_type); 3967 } 3968 3969 3970 page->total_luns = num_luns; 3971 3972 switch (config_type) { 3973 case 40: 3974 /* 3975 * In a 40 drive configuration, it doesn't matter what DC 3976 * cards we have, whether we have AOR enabled or not, 3977 * partitioning or not, or what type of RAIDset we have. 3978 * In that scenario, we can power up every LUN we present 3979 * to the user. 3980 */ 3981 max_active_luns = num_luns; 3982 3983 break; 3984 case 64: 3985 if (shelf_50pct_power == CS_FALSE) { 3986 /* 25% power */ 3987 if (aor_present == CS_TRUE) { 3988 if (rs_type == 3989 CS_RAIDSET_PERSONALITY_RAID5) { 3990 max_active_luns = 7; 3991 } else if (rs_type == 3992 CS_RAIDSET_PERSONALITY_RAID1){ 3993 max_active_luns = 14; 3994 } else { 3995 /* XXX KDM now what?? */ 3996 } 3997 } else { 3998 if (rs_type == 3999 CS_RAIDSET_PERSONALITY_RAID5) { 4000 max_active_luns = 8; 4001 } else if (rs_type == 4002 CS_RAIDSET_PERSONALITY_RAID1){ 4003 max_active_luns = 16; 4004 } else { 4005 /* XXX KDM now what?? */ 4006 } 4007 } 4008 } else { 4009 /* 50% power */ 4010 /* 4011 * With 50% power in a 64 drive configuration, we 4012 * can power all LUNs we present. 4013 */ 4014 max_active_luns = num_luns; 4015 } 4016 break; 4017 case 112: 4018 if (shelf_50pct_power == CS_FALSE) { 4019 /* 25% power */ 4020 if (aor_present == CS_TRUE) { 4021 if (rs_type == 4022 CS_RAIDSET_PERSONALITY_RAID5) { 4023 max_active_luns = 7; 4024 } else if (rs_type == 4025 CS_RAIDSET_PERSONALITY_RAID1){ 4026 max_active_luns = 14; 4027 } else { 4028 /* XXX KDM now what?? */ 4029 } 4030 } else { 4031 if (rs_type == 4032 CS_RAIDSET_PERSONALITY_RAID5) { 4033 max_active_luns = 8; 4034 } else if (rs_type == 4035 CS_RAIDSET_PERSONALITY_RAID1){ 4036 max_active_luns = 16; 4037 } else { 4038 /* XXX KDM now what?? */ 4039 } 4040 } 4041 } else { 4042 /* 50% power */ 4043 if (aor_present == CS_TRUE) { 4044 if (rs_type == 4045 CS_RAIDSET_PERSONALITY_RAID5) { 4046 max_active_luns = 14; 4047 } else if (rs_type == 4048 CS_RAIDSET_PERSONALITY_RAID1){ 4049 /* 4050 * We're assuming here that disk 4051 * caching is enabled, and so we're 4052 * able to power up half of each 4053 * LUN, and cache all writes. 4054 */ 4055 max_active_luns = num_luns; 4056 } else { 4057 /* XXX KDM now what?? */ 4058 } 4059 } else { 4060 if (rs_type == 4061 CS_RAIDSET_PERSONALITY_RAID5) { 4062 max_active_luns = 15; 4063 } else if (rs_type == 4064 CS_RAIDSET_PERSONALITY_RAID1){ 4065 max_active_luns = 30; 4066 } else { 4067 /* XXX KDM now what?? */ 4068 } 4069 } 4070 } 4071 break; 4072 default: 4073 /* 4074 * In this case, we have an unknown configuration, so we 4075 * just use the default from above. 4076 */ 4077 break; 4078 } 4079 4080 page->max_active_luns = max_active_luns; 4081#if 0 4082 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4083 page->total_luns, page->max_active_luns); 4084#endif 4085} 4086#endif /* NEEDTOPORT */ 4087 4088/* 4089 * This routine could be used in the future to load default and/or saved 4090 * mode page parameters for a particuar lun. 4091 */ 4092static int 4093ctl_init_page_index(struct ctl_lun *lun) 4094{ 4095 int i; 4096 struct ctl_page_index *page_index; 4097 struct ctl_softc *softc; 4098 4099 memcpy(&lun->mode_pages.index, page_index_template, 4100 sizeof(page_index_template)); 4101 4102 softc = lun->ctl_softc; 4103 4104 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4105 4106 page_index = &lun->mode_pages.index[i]; 4107 /* 4108 * If this is a disk-only mode page, there's no point in 4109 * setting it up. For some pages, we have to have some 4110 * basic information about the disk in order to calculate the 4111 * mode page data. 4112 */ 4113 if ((lun->be_lun->lun_type != T_DIRECT) 4114 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4115 continue; 4116 4117 switch (page_index->page_code & SMPH_PC_MASK) { 4118 case SMS_FORMAT_DEVICE_PAGE: { 4119 struct scsi_format_page *format_page; 4120 4121 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4122 panic("subpage is incorrect!"); 4123 4124 /* 4125 * Sectors per track are set above. Bytes per 4126 * sector need to be set here on a per-LUN basis. 4127 */ 4128 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4129 &format_page_default, 4130 sizeof(format_page_default)); 4131 memcpy(&lun->mode_pages.format_page[ 4132 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4133 sizeof(format_page_changeable)); 4134 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4135 &format_page_default, 4136 sizeof(format_page_default)); 4137 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4138 &format_page_default, 4139 sizeof(format_page_default)); 4140 4141 format_page = &lun->mode_pages.format_page[ 4142 CTL_PAGE_CURRENT]; 4143 scsi_ulto2b(lun->be_lun->blocksize, 4144 format_page->bytes_per_sector); 4145 4146 format_page = &lun->mode_pages.format_page[ 4147 CTL_PAGE_DEFAULT]; 4148 scsi_ulto2b(lun->be_lun->blocksize, 4149 format_page->bytes_per_sector); 4150 4151 format_page = &lun->mode_pages.format_page[ 4152 CTL_PAGE_SAVED]; 4153 scsi_ulto2b(lun->be_lun->blocksize, 4154 format_page->bytes_per_sector); 4155 4156 page_index->page_data = 4157 (uint8_t *)lun->mode_pages.format_page; 4158 break; 4159 } 4160 case SMS_RIGID_DISK_PAGE: { 4161 struct scsi_rigid_disk_page *rigid_disk_page; 4162 uint32_t sectors_per_cylinder; 4163 uint64_t cylinders; 4164#ifndef __XSCALE__ 4165 int shift; 4166#endif /* !__XSCALE__ */ 4167 4168 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4169 panic("invalid subpage value %d", 4170 page_index->subpage); 4171 4172 /* 4173 * Rotation rate and sectors per track are set 4174 * above. We calculate the cylinders here based on 4175 * capacity. Due to the number of heads and 4176 * sectors per track we're using, smaller arrays 4177 * may turn out to have 0 cylinders. Linux and 4178 * FreeBSD don't pay attention to these mode pages 4179 * to figure out capacity, but Solaris does. It 4180 * seems to deal with 0 cylinders just fine, and 4181 * works out a fake geometry based on the capacity. 4182 */ 4183 memcpy(&lun->mode_pages.rigid_disk_page[ 4184 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4185 sizeof(rigid_disk_page_default)); 4186 memcpy(&lun->mode_pages.rigid_disk_page[ 4187 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4188 sizeof(rigid_disk_page_changeable)); 4189 memcpy(&lun->mode_pages.rigid_disk_page[ 4190 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4191 sizeof(rigid_disk_page_default)); 4192 memcpy(&lun->mode_pages.rigid_disk_page[ 4193 CTL_PAGE_SAVED], &rigid_disk_page_default, 4194 sizeof(rigid_disk_page_default)); 4195 4196 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4197 CTL_DEFAULT_HEADS; 4198 4199 /* 4200 * The divide method here will be more accurate, 4201 * probably, but results in floating point being 4202 * used in the kernel on i386 (__udivdi3()). On the 4203 * XScale, though, __udivdi3() is implemented in 4204 * software. 4205 * 4206 * The shift method for cylinder calculation is 4207 * accurate if sectors_per_cylinder is a power of 4208 * 2. Otherwise it might be slightly off -- you 4209 * might have a bit of a truncation problem. 4210 */ 4211#ifdef __XSCALE__ 4212 cylinders = (lun->be_lun->maxlba + 1) / 4213 sectors_per_cylinder; 4214#else 4215 for (shift = 31; shift > 0; shift--) { 4216 if (sectors_per_cylinder & (1 << shift)) 4217 break; 4218 } 4219 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4220#endif 4221 4222 /* 4223 * We've basically got 3 bytes, or 24 bits for the 4224 * cylinder size in the mode page. If we're over, 4225 * just round down to 2^24. 4226 */ 4227 if (cylinders > 0xffffff) 4228 cylinders = 0xffffff; 4229 4230 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4231 CTL_PAGE_CURRENT]; 4232 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4233 4234 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4235 CTL_PAGE_DEFAULT]; 4236 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4237 4238 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4239 CTL_PAGE_SAVED]; 4240 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4241 4242 page_index->page_data = 4243 (uint8_t *)lun->mode_pages.rigid_disk_page; 4244 break; 4245 } 4246 case SMS_CACHING_PAGE: { 4247 4248 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4249 panic("invalid subpage value %d", 4250 page_index->subpage); 4251 /* 4252 * Defaults should be okay here, no calculations 4253 * needed. 4254 */ 4255 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4256 &caching_page_default, 4257 sizeof(caching_page_default)); 4258 memcpy(&lun->mode_pages.caching_page[ 4259 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4260 sizeof(caching_page_changeable)); 4261 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4262 &caching_page_default, 4263 sizeof(caching_page_default)); 4264 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4265 &caching_page_default, 4266 sizeof(caching_page_default)); 4267 page_index->page_data = 4268 (uint8_t *)lun->mode_pages.caching_page; 4269 break; 4270 } 4271 case SMS_CONTROL_MODE_PAGE: { 4272 4273 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4274 panic("invalid subpage value %d", 4275 page_index->subpage); 4276 4277 /* 4278 * Defaults should be okay here, no calculations 4279 * needed. 4280 */ 4281 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4282 &control_page_default, 4283 sizeof(control_page_default)); 4284 memcpy(&lun->mode_pages.control_page[ 4285 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4286 sizeof(control_page_changeable)); 4287 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4288 &control_page_default, 4289 sizeof(control_page_default)); 4290 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4291 &control_page_default, 4292 sizeof(control_page_default)); 4293 page_index->page_data = 4294 (uint8_t *)lun->mode_pages.control_page; 4295 break; 4296 4297 } 4298 case SMS_VENDOR_SPECIFIC_PAGE:{ 4299 switch (page_index->subpage) { 4300 case PWR_SUBPAGE_CODE: { 4301 struct copan_power_subpage *current_page, 4302 *saved_page; 4303 4304 memcpy(&lun->mode_pages.power_subpage[ 4305 CTL_PAGE_CURRENT], 4306 &power_page_default, 4307 sizeof(power_page_default)); 4308 memcpy(&lun->mode_pages.power_subpage[ 4309 CTL_PAGE_CHANGEABLE], 4310 &power_page_changeable, 4311 sizeof(power_page_changeable)); 4312 memcpy(&lun->mode_pages.power_subpage[ 4313 CTL_PAGE_DEFAULT], 4314 &power_page_default, 4315 sizeof(power_page_default)); 4316 memcpy(&lun->mode_pages.power_subpage[ 4317 CTL_PAGE_SAVED], 4318 &power_page_default, 4319 sizeof(power_page_default)); 4320 page_index->page_data = 4321 (uint8_t *)lun->mode_pages.power_subpage; 4322 4323 current_page = (struct copan_power_subpage *) 4324 (page_index->page_data + 4325 (page_index->page_len * 4326 CTL_PAGE_CURRENT)); 4327 saved_page = (struct copan_power_subpage *) 4328 (page_index->page_data + 4329 (page_index->page_len * 4330 CTL_PAGE_SAVED)); 4331 break; 4332 } 4333 case APS_SUBPAGE_CODE: { 4334 struct copan_aps_subpage *current_page, 4335 *saved_page; 4336 4337 // This gets set multiple times but 4338 // it should always be the same. It's 4339 // only done during init so who cares. 4340 index_to_aps_page = i; 4341 4342 memcpy(&lun->mode_pages.aps_subpage[ 4343 CTL_PAGE_CURRENT], 4344 &aps_page_default, 4345 sizeof(aps_page_default)); 4346 memcpy(&lun->mode_pages.aps_subpage[ 4347 CTL_PAGE_CHANGEABLE], 4348 &aps_page_changeable, 4349 sizeof(aps_page_changeable)); 4350 memcpy(&lun->mode_pages.aps_subpage[ 4351 CTL_PAGE_DEFAULT], 4352 &aps_page_default, 4353 sizeof(aps_page_default)); 4354 memcpy(&lun->mode_pages.aps_subpage[ 4355 CTL_PAGE_SAVED], 4356 &aps_page_default, 4357 sizeof(aps_page_default)); 4358 page_index->page_data = 4359 (uint8_t *)lun->mode_pages.aps_subpage; 4360 4361 current_page = (struct copan_aps_subpage *) 4362 (page_index->page_data + 4363 (page_index->page_len * 4364 CTL_PAGE_CURRENT)); 4365 saved_page = (struct copan_aps_subpage *) 4366 (page_index->page_data + 4367 (page_index->page_len * 4368 CTL_PAGE_SAVED)); 4369 break; 4370 } 4371 case DBGCNF_SUBPAGE_CODE: { 4372 struct copan_debugconf_subpage *current_page, 4373 *saved_page; 4374 4375 memcpy(&lun->mode_pages.debugconf_subpage[ 4376 CTL_PAGE_CURRENT], 4377 &debugconf_page_default, 4378 sizeof(debugconf_page_default)); 4379 memcpy(&lun->mode_pages.debugconf_subpage[ 4380 CTL_PAGE_CHANGEABLE], 4381 &debugconf_page_changeable, 4382 sizeof(debugconf_page_changeable)); 4383 memcpy(&lun->mode_pages.debugconf_subpage[ 4384 CTL_PAGE_DEFAULT], 4385 &debugconf_page_default, 4386 sizeof(debugconf_page_default)); 4387 memcpy(&lun->mode_pages.debugconf_subpage[ 4388 CTL_PAGE_SAVED], 4389 &debugconf_page_default, 4390 sizeof(debugconf_page_default)); 4391 page_index->page_data = 4392 (uint8_t *)lun->mode_pages.debugconf_subpage; 4393 4394 current_page = (struct copan_debugconf_subpage *) 4395 (page_index->page_data + 4396 (page_index->page_len * 4397 CTL_PAGE_CURRENT)); 4398 saved_page = (struct copan_debugconf_subpage *) 4399 (page_index->page_data + 4400 (page_index->page_len * 4401 CTL_PAGE_SAVED)); 4402 break; 4403 } 4404 default: 4405 panic("invalid subpage value %d", 4406 page_index->subpage); 4407 break; 4408 } 4409 break; 4410 } 4411 default: 4412 panic("invalid page value %d", 4413 page_index->page_code & SMPH_PC_MASK); 4414 break; 4415 } 4416 } 4417 4418 return (CTL_RETVAL_COMPLETE); 4419} 4420 4421/* 4422 * LUN allocation. 4423 * 4424 * Requirements: 4425 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4426 * wants us to allocate the LUN and he can block. 4427 * - ctl_softc is always set 4428 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4429 * 4430 * Returns 0 for success, non-zero (errno) for failure. 4431 */ 4432static int 4433ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4434 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4435{ 4436 struct ctl_lun *nlun, *lun; 4437 struct ctl_port *port; 4438 struct scsi_vpd_id_descriptor *desc; 4439 struct scsi_vpd_id_t10 *t10id; 4440 const char *eui, *naa, *scsiname, *vendor; 4441 int lun_number, i, lun_malloced; 4442 int devidlen, idlen1, idlen2 = 0, len; 4443 4444 if (be_lun == NULL) 4445 return (EINVAL); 4446 4447 /* 4448 * We currently only support Direct Access or Processor LUN types. 4449 */ 4450 switch (be_lun->lun_type) { 4451 case T_DIRECT: 4452 break; 4453 case T_PROCESSOR: 4454 break; 4455 case T_SEQUENTIAL: 4456 case T_CHANGER: 4457 default: 4458 be_lun->lun_config_status(be_lun->be_lun, 4459 CTL_LUN_CONFIG_FAILURE); 4460 break; 4461 } 4462 if (ctl_lun == NULL) { 4463 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4464 lun_malloced = 1; 4465 } else { 4466 lun_malloced = 0; 4467 lun = ctl_lun; 4468 } 4469 4470 memset(lun, 0, sizeof(*lun)); 4471 if (lun_malloced) 4472 lun->flags = CTL_LUN_MALLOCED; 4473 4474 /* Generate LUN ID. */ 4475 devidlen = max(CTL_DEVID_MIN_LEN, 4476 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4477 idlen1 = sizeof(*t10id) + devidlen; 4478 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4479 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4480 if (scsiname != NULL) { 4481 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4482 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4483 } 4484 eui = ctl_get_opt(&be_lun->options, "eui"); 4485 if (eui != NULL) { 4486 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4487 } 4488 naa = ctl_get_opt(&be_lun->options, "naa"); 4489 if (naa != NULL) { 4490 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4491 } 4492 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4493 M_CTL, M_WAITOK | M_ZERO); 4494 lun->lun_devid->len = len; 4495 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4496 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4497 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4498 desc->length = idlen1; 4499 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4500 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4501 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4502 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4503 } else { 4504 strncpy(t10id->vendor, vendor, 4505 min(sizeof(t10id->vendor), strlen(vendor))); 4506 } 4507 strncpy((char *)t10id->vendor_spec_id, 4508 (char *)be_lun->device_id, devidlen); 4509 if (scsiname != NULL) { 4510 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4511 desc->length); 4512 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4513 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4514 SVPD_ID_TYPE_SCSI_NAME; 4515 desc->length = idlen2; 4516 strlcpy(desc->identifier, scsiname, idlen2); 4517 } 4518 if (eui != NULL) { 4519 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4520 desc->length); 4521 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4522 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4523 SVPD_ID_TYPE_EUI64; 4524 desc->length = 8; 4525 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4526 } 4527 if (naa != NULL) { 4528 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4529 desc->length); 4530 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4531 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4532 SVPD_ID_TYPE_NAA; 4533 desc->length = 8; 4534 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4535 } 4536 4537 mtx_lock(&ctl_softc->ctl_lock); 4538 /* 4539 * See if the caller requested a particular LUN number. If so, see 4540 * if it is available. Otherwise, allocate the first available LUN. 4541 */ 4542 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4543 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4544 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4545 mtx_unlock(&ctl_softc->ctl_lock); 4546 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4547 printf("ctl: requested LUN ID %d is higher " 4548 "than CTL_MAX_LUNS - 1 (%d)\n", 4549 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4550 } else { 4551 /* 4552 * XXX KDM return an error, or just assign 4553 * another LUN ID in this case?? 4554 */ 4555 printf("ctl: requested LUN ID %d is already " 4556 "in use\n", be_lun->req_lun_id); 4557 } 4558 if (lun->flags & CTL_LUN_MALLOCED) 4559 free(lun, M_CTL); 4560 be_lun->lun_config_status(be_lun->be_lun, 4561 CTL_LUN_CONFIG_FAILURE); 4562 return (ENOSPC); 4563 } 4564 lun_number = be_lun->req_lun_id; 4565 } else { 4566 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4567 if (lun_number == -1) { 4568 mtx_unlock(&ctl_softc->ctl_lock); 4569 printf("ctl: can't allocate LUN on target %ju, out of " 4570 "LUNs\n", (uintmax_t)target_id.id); 4571 if (lun->flags & CTL_LUN_MALLOCED) 4572 free(lun, M_CTL); 4573 be_lun->lun_config_status(be_lun->be_lun, 4574 CTL_LUN_CONFIG_FAILURE); 4575 return (ENOSPC); 4576 } 4577 } 4578 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4579 4580 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4581 lun->target = target_id; 4582 lun->lun = lun_number; 4583 lun->be_lun = be_lun; 4584 /* 4585 * The processor LUN is always enabled. Disk LUNs come on line 4586 * disabled, and must be enabled by the backend. 4587 */ 4588 lun->flags |= CTL_LUN_DISABLED; 4589 lun->backend = be_lun->be; 4590 be_lun->ctl_lun = lun; 4591 be_lun->lun_id = lun_number; 4592 atomic_add_int(&be_lun->be->num_luns, 1); 4593 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4594 lun->flags |= CTL_LUN_STOPPED; 4595 4596 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4597 lun->flags |= CTL_LUN_INOPERABLE; 4598 4599 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4600 lun->flags |= CTL_LUN_PRIMARY_SC; 4601 4602 lun->ctl_softc = ctl_softc; 4603 TAILQ_INIT(&lun->ooa_queue); 4604 TAILQ_INIT(&lun->blocked_queue); 4605 STAILQ_INIT(&lun->error_list); 4606 ctl_tpc_lun_init(lun); 4607 4608 /* 4609 * Initialize the mode page index. 4610 */ 4611 ctl_init_page_index(lun); 4612 4613 /* 4614 * Set the poweron UA for all initiators on this LUN only. 4615 */ 4616 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4617 lun->pending_ua[i] = CTL_UA_POWERON; 4618 4619 /* 4620 * Now, before we insert this lun on the lun list, set the lun 4621 * inventory changed UA for all other luns. 4622 */ 4623 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4624 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4625 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4626 } 4627 } 4628 4629 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4630 4631 ctl_softc->ctl_luns[lun_number] = lun; 4632 4633 ctl_softc->num_luns++; 4634 4635 /* Setup statistics gathering */ 4636 lun->stats.device_type = be_lun->lun_type; 4637 lun->stats.lun_number = lun_number; 4638 if (lun->stats.device_type == T_DIRECT) 4639 lun->stats.blocksize = be_lun->blocksize; 4640 else 4641 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4642 for (i = 0;i < CTL_MAX_PORTS;i++) 4643 lun->stats.ports[i].targ_port = i; 4644 4645 mtx_unlock(&ctl_softc->ctl_lock); 4646 4647 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4648 4649 /* 4650 * Run through each registered FETD and bring it online if it isn't 4651 * already. Enable the target ID if it hasn't been enabled, and 4652 * enable this particular LUN. 4653 */ 4654 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4655 int retval; 4656 4657 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4658 if (retval != 0) { 4659 printf("ctl_alloc_lun: FETD %s port %d returned error " 4660 "%d for lun_enable on target %ju lun %d\n", 4661 port->port_name, port->targ_port, retval, 4662 (uintmax_t)target_id.id, lun_number); 4663 } else 4664 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4665 } 4666 return (0); 4667} 4668 4669/* 4670 * Delete a LUN. 4671 * Assumptions: 4672 * - LUN has already been marked invalid and any pending I/O has been taken 4673 * care of. 4674 */ 4675static int 4676ctl_free_lun(struct ctl_lun *lun) 4677{ 4678 struct ctl_softc *softc; 4679#if 0 4680 struct ctl_port *port; 4681#endif 4682 struct ctl_lun *nlun; 4683 int i; 4684 4685 softc = lun->ctl_softc; 4686 4687 mtx_assert(&softc->ctl_lock, MA_OWNED); 4688 4689 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4690 4691 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4692 4693 softc->ctl_luns[lun->lun] = NULL; 4694 4695 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4696 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4697 4698 softc->num_luns--; 4699 4700 /* 4701 * XXX KDM this scheme only works for a single target/multiple LUN 4702 * setup. It needs to be revamped for a multiple target scheme. 4703 * 4704 * XXX KDM this results in port->lun_disable() getting called twice, 4705 * once when ctl_disable_lun() is called, and a second time here. 4706 * We really need to re-think the LUN disable semantics. There 4707 * should probably be several steps/levels to LUN removal: 4708 * - disable 4709 * - invalidate 4710 * - free 4711 * 4712 * Right now we only have a disable method when communicating to 4713 * the front end ports, at least for individual LUNs. 4714 */ 4715#if 0 4716 STAILQ_FOREACH(port, &softc->port_list, links) { 4717 int retval; 4718 4719 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4720 lun->lun); 4721 if (retval != 0) { 4722 printf("ctl_free_lun: FETD %s port %d returned error " 4723 "%d for lun_disable on target %ju lun %jd\n", 4724 port->port_name, port->targ_port, retval, 4725 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4726 } 4727 4728 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4729 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4730 4731 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4732 if (retval != 0) { 4733 printf("ctl_free_lun: FETD %s port %d " 4734 "returned error %d for targ_disable on " 4735 "target %ju\n", port->port_name, 4736 port->targ_port, retval, 4737 (uintmax_t)lun->target.id); 4738 } else 4739 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4740 4741 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4742 continue; 4743 4744#if 0 4745 port->port_offline(port->onoff_arg); 4746 port->status &= ~CTL_PORT_STATUS_ONLINE; 4747#endif 4748 } 4749 } 4750#endif 4751 4752 /* 4753 * Tell the backend to free resources, if this LUN has a backend. 4754 */ 4755 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4756 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4757 4758 ctl_tpc_lun_shutdown(lun); 4759 mtx_destroy(&lun->lun_lock); 4760 free(lun->lun_devid, M_CTL); 4761 if (lun->flags & CTL_LUN_MALLOCED) 4762 free(lun, M_CTL); 4763 4764 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4765 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4766 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4767 } 4768 } 4769 4770 return (0); 4771} 4772 4773static void 4774ctl_create_lun(struct ctl_be_lun *be_lun) 4775{ 4776 struct ctl_softc *ctl_softc; 4777 4778 ctl_softc = control_softc; 4779 4780 /* 4781 * ctl_alloc_lun() should handle all potential failure cases. 4782 */ 4783 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4784} 4785 4786int 4787ctl_add_lun(struct ctl_be_lun *be_lun) 4788{ 4789 struct ctl_softc *ctl_softc = control_softc; 4790 4791 mtx_lock(&ctl_softc->ctl_lock); 4792 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4793 mtx_unlock(&ctl_softc->ctl_lock); 4794 wakeup(&ctl_softc->pending_lun_queue); 4795 4796 return (0); 4797} 4798 4799int 4800ctl_enable_lun(struct ctl_be_lun *be_lun) 4801{ 4802 struct ctl_softc *ctl_softc; 4803 struct ctl_port *port, *nport; 4804 struct ctl_lun *lun; 4805 int retval; 4806 4807 ctl_softc = control_softc; 4808 4809 lun = (struct ctl_lun *)be_lun->ctl_lun; 4810 4811 mtx_lock(&ctl_softc->ctl_lock); 4812 mtx_lock(&lun->lun_lock); 4813 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4814 /* 4815 * eh? Why did we get called if the LUN is already 4816 * enabled? 4817 */ 4818 mtx_unlock(&lun->lun_lock); 4819 mtx_unlock(&ctl_softc->ctl_lock); 4820 return (0); 4821 } 4822 lun->flags &= ~CTL_LUN_DISABLED; 4823 mtx_unlock(&lun->lun_lock); 4824 4825 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4826 nport = STAILQ_NEXT(port, links); 4827 4828 /* 4829 * Drop the lock while we call the FETD's enable routine. 4830 * This can lead to a callback into CTL (at least in the 4831 * case of the internal initiator frontend. 4832 */ 4833 mtx_unlock(&ctl_softc->ctl_lock); 4834 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4835 mtx_lock(&ctl_softc->ctl_lock); 4836 if (retval != 0) { 4837 printf("%s: FETD %s port %d returned error " 4838 "%d for lun_enable on target %ju lun %jd\n", 4839 __func__, port->port_name, port->targ_port, retval, 4840 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4841 } 4842#if 0 4843 else { 4844 /* NOTE: TODO: why does lun enable affect port status? */ 4845 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4846 } 4847#endif 4848 } 4849 4850 mtx_unlock(&ctl_softc->ctl_lock); 4851 4852 return (0); 4853} 4854 4855int 4856ctl_disable_lun(struct ctl_be_lun *be_lun) 4857{ 4858 struct ctl_softc *ctl_softc; 4859 struct ctl_port *port; 4860 struct ctl_lun *lun; 4861 int retval; 4862 4863 ctl_softc = control_softc; 4864 4865 lun = (struct ctl_lun *)be_lun->ctl_lun; 4866 4867 mtx_lock(&ctl_softc->ctl_lock); 4868 mtx_lock(&lun->lun_lock); 4869 if (lun->flags & CTL_LUN_DISABLED) { 4870 mtx_unlock(&lun->lun_lock); 4871 mtx_unlock(&ctl_softc->ctl_lock); 4872 return (0); 4873 } 4874 lun->flags |= CTL_LUN_DISABLED; 4875 mtx_unlock(&lun->lun_lock); 4876 4877 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4878 mtx_unlock(&ctl_softc->ctl_lock); 4879 /* 4880 * Drop the lock before we call the frontend's disable 4881 * routine, to avoid lock order reversals. 4882 * 4883 * XXX KDM what happens if the frontend list changes while 4884 * we're traversing it? It's unlikely, but should be handled. 4885 */ 4886 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4887 lun->lun); 4888 mtx_lock(&ctl_softc->ctl_lock); 4889 if (retval != 0) { 4890 printf("ctl_alloc_lun: FETD %s port %d returned error " 4891 "%d for lun_disable on target %ju lun %jd\n", 4892 port->port_name, port->targ_port, retval, 4893 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4894 } 4895 } 4896 4897 mtx_unlock(&ctl_softc->ctl_lock); 4898 4899 return (0); 4900} 4901 4902int 4903ctl_start_lun(struct ctl_be_lun *be_lun) 4904{ 4905 struct ctl_softc *ctl_softc; 4906 struct ctl_lun *lun; 4907 4908 ctl_softc = control_softc; 4909 4910 lun = (struct ctl_lun *)be_lun->ctl_lun; 4911 4912 mtx_lock(&lun->lun_lock); 4913 lun->flags &= ~CTL_LUN_STOPPED; 4914 mtx_unlock(&lun->lun_lock); 4915 4916 return (0); 4917} 4918 4919int 4920ctl_stop_lun(struct ctl_be_lun *be_lun) 4921{ 4922 struct ctl_softc *ctl_softc; 4923 struct ctl_lun *lun; 4924 4925 ctl_softc = control_softc; 4926 4927 lun = (struct ctl_lun *)be_lun->ctl_lun; 4928 4929 mtx_lock(&lun->lun_lock); 4930 lun->flags |= CTL_LUN_STOPPED; 4931 mtx_unlock(&lun->lun_lock); 4932 4933 return (0); 4934} 4935 4936int 4937ctl_lun_offline(struct ctl_be_lun *be_lun) 4938{ 4939 struct ctl_softc *ctl_softc; 4940 struct ctl_lun *lun; 4941 4942 ctl_softc = control_softc; 4943 4944 lun = (struct ctl_lun *)be_lun->ctl_lun; 4945 4946 mtx_lock(&lun->lun_lock); 4947 lun->flags |= CTL_LUN_OFFLINE; 4948 mtx_unlock(&lun->lun_lock); 4949 4950 return (0); 4951} 4952 4953int 4954ctl_lun_online(struct ctl_be_lun *be_lun) 4955{ 4956 struct ctl_softc *ctl_softc; 4957 struct ctl_lun *lun; 4958 4959 ctl_softc = control_softc; 4960 4961 lun = (struct ctl_lun *)be_lun->ctl_lun; 4962 4963 mtx_lock(&lun->lun_lock); 4964 lun->flags &= ~CTL_LUN_OFFLINE; 4965 mtx_unlock(&lun->lun_lock); 4966 4967 return (0); 4968} 4969 4970int 4971ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4972{ 4973 struct ctl_softc *ctl_softc; 4974 struct ctl_lun *lun; 4975 4976 ctl_softc = control_softc; 4977 4978 lun = (struct ctl_lun *)be_lun->ctl_lun; 4979 4980 mtx_lock(&lun->lun_lock); 4981 4982 /* 4983 * The LUN needs to be disabled before it can be marked invalid. 4984 */ 4985 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4986 mtx_unlock(&lun->lun_lock); 4987 return (-1); 4988 } 4989 /* 4990 * Mark the LUN invalid. 4991 */ 4992 lun->flags |= CTL_LUN_INVALID; 4993 4994 /* 4995 * If there is nothing in the OOA queue, go ahead and free the LUN. 4996 * If we have something in the OOA queue, we'll free it when the 4997 * last I/O completes. 4998 */ 4999 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5000 mtx_unlock(&lun->lun_lock); 5001 mtx_lock(&ctl_softc->ctl_lock); 5002 ctl_free_lun(lun); 5003 mtx_unlock(&ctl_softc->ctl_lock); 5004 } else 5005 mtx_unlock(&lun->lun_lock); 5006 5007 return (0); 5008} 5009 5010int 5011ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5012{ 5013 struct ctl_softc *ctl_softc; 5014 struct ctl_lun *lun; 5015 5016 ctl_softc = control_softc; 5017 lun = (struct ctl_lun *)be_lun->ctl_lun; 5018 5019 mtx_lock(&lun->lun_lock); 5020 lun->flags |= CTL_LUN_INOPERABLE; 5021 mtx_unlock(&lun->lun_lock); 5022 5023 return (0); 5024} 5025 5026int 5027ctl_lun_operable(struct ctl_be_lun *be_lun) 5028{ 5029 struct ctl_softc *ctl_softc; 5030 struct ctl_lun *lun; 5031 5032 ctl_softc = control_softc; 5033 lun = (struct ctl_lun *)be_lun->ctl_lun; 5034 5035 mtx_lock(&lun->lun_lock); 5036 lun->flags &= ~CTL_LUN_INOPERABLE; 5037 mtx_unlock(&lun->lun_lock); 5038 5039 return (0); 5040} 5041 5042int 5043ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5044 int lock) 5045{ 5046 struct ctl_softc *softc; 5047 struct ctl_lun *lun; 5048 struct copan_aps_subpage *current_sp; 5049 struct ctl_page_index *page_index; 5050 int i; 5051 5052 softc = control_softc; 5053 5054 mtx_lock(&softc->ctl_lock); 5055 5056 lun = (struct ctl_lun *)be_lun->ctl_lun; 5057 mtx_lock(&lun->lun_lock); 5058 5059 page_index = NULL; 5060 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5061 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5062 APS_PAGE_CODE) 5063 continue; 5064 5065 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5066 continue; 5067 page_index = &lun->mode_pages.index[i]; 5068 } 5069 5070 if (page_index == NULL) { 5071 mtx_unlock(&lun->lun_lock); 5072 mtx_unlock(&softc->ctl_lock); 5073 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5074 (uintmax_t)lun->lun); 5075 return (1); 5076 } 5077#if 0 5078 if ((softc->aps_locked_lun != 0) 5079 && (softc->aps_locked_lun != lun->lun)) { 5080 printf("%s: attempt to lock LUN %llu when %llu is already " 5081 "locked\n"); 5082 mtx_unlock(&lun->lun_lock); 5083 mtx_unlock(&softc->ctl_lock); 5084 return (1); 5085 } 5086#endif 5087 5088 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5089 (page_index->page_len * CTL_PAGE_CURRENT)); 5090 5091 if (lock != 0) { 5092 current_sp->lock_active = APS_LOCK_ACTIVE; 5093 softc->aps_locked_lun = lun->lun; 5094 } else { 5095 current_sp->lock_active = 0; 5096 softc->aps_locked_lun = 0; 5097 } 5098 5099 5100 /* 5101 * If we're in HA mode, try to send the lock message to the other 5102 * side. 5103 */ 5104 if (ctl_is_single == 0) { 5105 int isc_retval; 5106 union ctl_ha_msg lock_msg; 5107 5108 lock_msg.hdr.nexus = *nexus; 5109 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5110 if (lock != 0) 5111 lock_msg.aps.lock_flag = 1; 5112 else 5113 lock_msg.aps.lock_flag = 0; 5114 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5115 sizeof(lock_msg), 0); 5116 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5117 printf("%s: APS (lock=%d) error returned from " 5118 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5119 mtx_unlock(&lun->lun_lock); 5120 mtx_unlock(&softc->ctl_lock); 5121 return (1); 5122 } 5123 } 5124 5125 mtx_unlock(&lun->lun_lock); 5126 mtx_unlock(&softc->ctl_lock); 5127 5128 return (0); 5129} 5130 5131void 5132ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5133{ 5134 struct ctl_lun *lun; 5135 struct ctl_softc *softc; 5136 int i; 5137 5138 softc = control_softc; 5139 5140 lun = (struct ctl_lun *)be_lun->ctl_lun; 5141 5142 mtx_lock(&lun->lun_lock); 5143 5144 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5145 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5146 5147 mtx_unlock(&lun->lun_lock); 5148} 5149 5150/* 5151 * Backend "memory move is complete" callback for requests that never 5152 * make it down to say RAIDCore's configuration code. 5153 */ 5154int 5155ctl_config_move_done(union ctl_io *io) 5156{ 5157 int retval; 5158 5159 retval = CTL_RETVAL_COMPLETE; 5160 5161 5162 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5163 /* 5164 * XXX KDM this shouldn't happen, but what if it does? 5165 */ 5166 if (io->io_hdr.io_type != CTL_IO_SCSI) 5167 panic("I/O type isn't CTL_IO_SCSI!"); 5168 5169 if ((io->io_hdr.port_status == 0) 5170 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5171 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5172 io->io_hdr.status = CTL_SUCCESS; 5173 else if ((io->io_hdr.port_status != 0) 5174 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5175 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5176 /* 5177 * For hardware error sense keys, the sense key 5178 * specific value is defined to be a retry count, 5179 * but we use it to pass back an internal FETD 5180 * error code. XXX KDM Hopefully the FETD is only 5181 * using 16 bits for an error code, since that's 5182 * all the space we have in the sks field. 5183 */ 5184 ctl_set_internal_failure(&io->scsiio, 5185 /*sks_valid*/ 1, 5186 /*retry_count*/ 5187 io->io_hdr.port_status); 5188 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5189 free(io->scsiio.kern_data_ptr, M_CTL); 5190 ctl_done(io); 5191 goto bailout; 5192 } 5193 5194 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5195 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5196 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5197 /* 5198 * XXX KDM just assuming a single pointer here, and not a 5199 * S/G list. If we start using S/G lists for config data, 5200 * we'll need to know how to clean them up here as well. 5201 */ 5202 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5203 free(io->scsiio.kern_data_ptr, M_CTL); 5204 /* Hopefully the user has already set the status... */ 5205 ctl_done(io); 5206 } else { 5207 /* 5208 * XXX KDM now we need to continue data movement. Some 5209 * options: 5210 * - call ctl_scsiio() again? We don't do this for data 5211 * writes, because for those at least we know ahead of 5212 * time where the write will go and how long it is. For 5213 * config writes, though, that information is largely 5214 * contained within the write itself, thus we need to 5215 * parse out the data again. 5216 * 5217 * - Call some other function once the data is in? 5218 */ 5219 5220 /* 5221 * XXX KDM call ctl_scsiio() again for now, and check flag 5222 * bits to see whether we're allocated or not. 5223 */ 5224 retval = ctl_scsiio(&io->scsiio); 5225 } 5226bailout: 5227 return (retval); 5228} 5229 5230/* 5231 * This gets called by a backend driver when it is done with a 5232 * data_submit method. 5233 */ 5234void 5235ctl_data_submit_done(union ctl_io *io) 5236{ 5237 /* 5238 * If the IO_CONT flag is set, we need to call the supplied 5239 * function to continue processing the I/O, instead of completing 5240 * the I/O just yet. 5241 * 5242 * If there is an error, though, we don't want to keep processing. 5243 * Instead, just send status back to the initiator. 5244 */ 5245 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5246 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5247 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5248 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5249 io->scsiio.io_cont(io); 5250 return; 5251 } 5252 ctl_done(io); 5253} 5254 5255/* 5256 * This gets called by a backend driver when it is done with a 5257 * configuration write. 5258 */ 5259void 5260ctl_config_write_done(union ctl_io *io) 5261{ 5262 /* 5263 * If the IO_CONT flag is set, we need to call the supplied 5264 * function to continue processing the I/O, instead of completing 5265 * the I/O just yet. 5266 * 5267 * If there is an error, though, we don't want to keep processing. 5268 * Instead, just send status back to the initiator. 5269 */ 5270 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5271 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5272 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5273 io->scsiio.io_cont(io); 5274 return; 5275 } 5276 /* 5277 * Since a configuration write can be done for commands that actually 5278 * have data allocated, like write buffer, and commands that have 5279 * no data, like start/stop unit, we need to check here. 5280 */ 5281 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5282 free(io->scsiio.kern_data_ptr, M_CTL); 5283 ctl_done(io); 5284} 5285 5286/* 5287 * SCSI release command. 5288 */ 5289int 5290ctl_scsi_release(struct ctl_scsiio *ctsio) 5291{ 5292 int length, longid, thirdparty_id, resv_id; 5293 struct ctl_softc *ctl_softc; 5294 struct ctl_lun *lun; 5295 5296 length = 0; 5297 resv_id = 0; 5298 5299 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5300 5301 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5302 ctl_softc = control_softc; 5303 5304 switch (ctsio->cdb[0]) { 5305 case RELEASE_10: { 5306 struct scsi_release_10 *cdb; 5307 5308 cdb = (struct scsi_release_10 *)ctsio->cdb; 5309 5310 if (cdb->byte2 & SR10_LONGID) 5311 longid = 1; 5312 else 5313 thirdparty_id = cdb->thirdparty_id; 5314 5315 resv_id = cdb->resv_id; 5316 length = scsi_2btoul(cdb->length); 5317 break; 5318 } 5319 } 5320 5321 5322 /* 5323 * XXX KDM right now, we only support LUN reservation. We don't 5324 * support 3rd party reservations, or extent reservations, which 5325 * might actually need the parameter list. If we've gotten this 5326 * far, we've got a LUN reservation. Anything else got kicked out 5327 * above. So, according to SPC, ignore the length. 5328 */ 5329 length = 0; 5330 5331 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5332 && (length > 0)) { 5333 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5334 ctsio->kern_data_len = length; 5335 ctsio->kern_total_len = length; 5336 ctsio->kern_data_resid = 0; 5337 ctsio->kern_rel_offset = 0; 5338 ctsio->kern_sg_entries = 0; 5339 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5340 ctsio->be_move_done = ctl_config_move_done; 5341 ctl_datamove((union ctl_io *)ctsio); 5342 5343 return (CTL_RETVAL_COMPLETE); 5344 } 5345 5346 if (length > 0) 5347 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5348 5349 mtx_lock(&lun->lun_lock); 5350 5351 /* 5352 * According to SPC, it is not an error for an intiator to attempt 5353 * to release a reservation on a LUN that isn't reserved, or that 5354 * is reserved by another initiator. The reservation can only be 5355 * released, though, by the initiator who made it or by one of 5356 * several reset type events. 5357 */ 5358 if (lun->flags & CTL_LUN_RESERVED) { 5359 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5360 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5361 && (ctsio->io_hdr.nexus.targ_target.id == 5362 lun->rsv_nexus.targ_target.id)) { 5363 lun->flags &= ~CTL_LUN_RESERVED; 5364 } 5365 } 5366 5367 mtx_unlock(&lun->lun_lock); 5368 5369 ctsio->scsi_status = SCSI_STATUS_OK; 5370 ctsio->io_hdr.status = CTL_SUCCESS; 5371 5372 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5373 free(ctsio->kern_data_ptr, M_CTL); 5374 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5375 } 5376 5377 ctl_done((union ctl_io *)ctsio); 5378 return (CTL_RETVAL_COMPLETE); 5379} 5380 5381int 5382ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5383{ 5384 int extent, thirdparty, longid; 5385 int resv_id, length; 5386 uint64_t thirdparty_id; 5387 struct ctl_softc *ctl_softc; 5388 struct ctl_lun *lun; 5389 5390 extent = 0; 5391 thirdparty = 0; 5392 longid = 0; 5393 resv_id = 0; 5394 length = 0; 5395 thirdparty_id = 0; 5396 5397 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5398 5399 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5400 ctl_softc = control_softc; 5401 5402 switch (ctsio->cdb[0]) { 5403 case RESERVE_10: { 5404 struct scsi_reserve_10 *cdb; 5405 5406 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5407 5408 if (cdb->byte2 & SR10_LONGID) 5409 longid = 1; 5410 else 5411 thirdparty_id = cdb->thirdparty_id; 5412 5413 resv_id = cdb->resv_id; 5414 length = scsi_2btoul(cdb->length); 5415 break; 5416 } 5417 } 5418 5419 /* 5420 * XXX KDM right now, we only support LUN reservation. We don't 5421 * support 3rd party reservations, or extent reservations, which 5422 * might actually need the parameter list. If we've gotten this 5423 * far, we've got a LUN reservation. Anything else got kicked out 5424 * above. So, according to SPC, ignore the length. 5425 */ 5426 length = 0; 5427 5428 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5429 && (length > 0)) { 5430 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5431 ctsio->kern_data_len = length; 5432 ctsio->kern_total_len = length; 5433 ctsio->kern_data_resid = 0; 5434 ctsio->kern_rel_offset = 0; 5435 ctsio->kern_sg_entries = 0; 5436 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5437 ctsio->be_move_done = ctl_config_move_done; 5438 ctl_datamove((union ctl_io *)ctsio); 5439 5440 return (CTL_RETVAL_COMPLETE); 5441 } 5442 5443 if (length > 0) 5444 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5445 5446 mtx_lock(&lun->lun_lock); 5447 if (lun->flags & CTL_LUN_RESERVED) { 5448 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5449 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5450 || (ctsio->io_hdr.nexus.targ_target.id != 5451 lun->rsv_nexus.targ_target.id)) { 5452 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5453 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5454 goto bailout; 5455 } 5456 } 5457 5458 lun->flags |= CTL_LUN_RESERVED; 5459 lun->rsv_nexus = ctsio->io_hdr.nexus; 5460 5461 ctsio->scsi_status = SCSI_STATUS_OK; 5462 ctsio->io_hdr.status = CTL_SUCCESS; 5463 5464bailout: 5465 mtx_unlock(&lun->lun_lock); 5466 5467 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5468 free(ctsio->kern_data_ptr, M_CTL); 5469 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5470 } 5471 5472 ctl_done((union ctl_io *)ctsio); 5473 return (CTL_RETVAL_COMPLETE); 5474} 5475 5476int 5477ctl_start_stop(struct ctl_scsiio *ctsio) 5478{ 5479 struct scsi_start_stop_unit *cdb; 5480 struct ctl_lun *lun; 5481 struct ctl_softc *ctl_softc; 5482 int retval; 5483 5484 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5485 5486 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5487 ctl_softc = control_softc; 5488 retval = 0; 5489 5490 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5491 5492 /* 5493 * XXX KDM 5494 * We don't support the immediate bit on a stop unit. In order to 5495 * do that, we would need to code up a way to know that a stop is 5496 * pending, and hold off any new commands until it completes, one 5497 * way or another. Then we could accept or reject those commands 5498 * depending on its status. We would almost need to do the reverse 5499 * of what we do below for an immediate start -- return the copy of 5500 * the ctl_io to the FETD with status to send to the host (and to 5501 * free the copy!) and then free the original I/O once the stop 5502 * actually completes. That way, the OOA queue mechanism can work 5503 * to block commands that shouldn't proceed. Another alternative 5504 * would be to put the copy in the queue in place of the original, 5505 * and return the original back to the caller. That could be 5506 * slightly safer.. 5507 */ 5508 if ((cdb->byte2 & SSS_IMMED) 5509 && ((cdb->how & SSS_START) == 0)) { 5510 ctl_set_invalid_field(ctsio, 5511 /*sks_valid*/ 1, 5512 /*command*/ 1, 5513 /*field*/ 1, 5514 /*bit_valid*/ 1, 5515 /*bit*/ 0); 5516 ctl_done((union ctl_io *)ctsio); 5517 return (CTL_RETVAL_COMPLETE); 5518 } 5519 5520 if ((lun->flags & CTL_LUN_PR_RESERVED) 5521 && ((cdb->how & SSS_START)==0)) { 5522 uint32_t residx; 5523 5524 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5525 if (!lun->per_res[residx].registered 5526 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5527 5528 ctl_set_reservation_conflict(ctsio); 5529 ctl_done((union ctl_io *)ctsio); 5530 return (CTL_RETVAL_COMPLETE); 5531 } 5532 } 5533 5534 /* 5535 * If there is no backend on this device, we can't start or stop 5536 * it. In theory we shouldn't get any start/stop commands in the 5537 * first place at this level if the LUN doesn't have a backend. 5538 * That should get stopped by the command decode code. 5539 */ 5540 if (lun->backend == NULL) { 5541 ctl_set_invalid_opcode(ctsio); 5542 ctl_done((union ctl_io *)ctsio); 5543 return (CTL_RETVAL_COMPLETE); 5544 } 5545 5546 /* 5547 * XXX KDM Copan-specific offline behavior. 5548 * Figure out a reasonable way to port this? 5549 */ 5550#ifdef NEEDTOPORT 5551 mtx_lock(&lun->lun_lock); 5552 5553 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5554 && (lun->flags & CTL_LUN_OFFLINE)) { 5555 /* 5556 * If the LUN is offline, and the on/offline bit isn't set, 5557 * reject the start or stop. Otherwise, let it through. 5558 */ 5559 mtx_unlock(&lun->lun_lock); 5560 ctl_set_lun_not_ready(ctsio); 5561 ctl_done((union ctl_io *)ctsio); 5562 } else { 5563 mtx_unlock(&lun->lun_lock); 5564#endif /* NEEDTOPORT */ 5565 /* 5566 * This could be a start or a stop when we're online, 5567 * or a stop/offline or start/online. A start or stop when 5568 * we're offline is covered in the case above. 5569 */ 5570 /* 5571 * In the non-immediate case, we send the request to 5572 * the backend and return status to the user when 5573 * it is done. 5574 * 5575 * In the immediate case, we allocate a new ctl_io 5576 * to hold a copy of the request, and send that to 5577 * the backend. We then set good status on the 5578 * user's request and return it immediately. 5579 */ 5580 if (cdb->byte2 & SSS_IMMED) { 5581 union ctl_io *new_io; 5582 5583 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5584 if (new_io == NULL) { 5585 ctl_set_busy(ctsio); 5586 ctl_done((union ctl_io *)ctsio); 5587 } else { 5588 ctl_copy_io((union ctl_io *)ctsio, 5589 new_io); 5590 retval = lun->backend->config_write(new_io); 5591 ctl_set_success(ctsio); 5592 ctl_done((union ctl_io *)ctsio); 5593 } 5594 } else { 5595 retval = lun->backend->config_write( 5596 (union ctl_io *)ctsio); 5597 } 5598#ifdef NEEDTOPORT 5599 } 5600#endif 5601 return (retval); 5602} 5603 5604/* 5605 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5606 * we don't really do anything with the LBA and length fields if the user 5607 * passes them in. Instead we'll just flush out the cache for the entire 5608 * LUN. 5609 */ 5610int 5611ctl_sync_cache(struct ctl_scsiio *ctsio) 5612{ 5613 struct ctl_lun *lun; 5614 struct ctl_softc *ctl_softc; 5615 uint64_t starting_lba; 5616 uint32_t block_count; 5617 int retval; 5618 5619 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5620 5621 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5622 ctl_softc = control_softc; 5623 retval = 0; 5624 5625 switch (ctsio->cdb[0]) { 5626 case SYNCHRONIZE_CACHE: { 5627 struct scsi_sync_cache *cdb; 5628 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5629 5630 starting_lba = scsi_4btoul(cdb->begin_lba); 5631 block_count = scsi_2btoul(cdb->lb_count); 5632 break; 5633 } 5634 case SYNCHRONIZE_CACHE_16: { 5635 struct scsi_sync_cache_16 *cdb; 5636 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5637 5638 starting_lba = scsi_8btou64(cdb->begin_lba); 5639 block_count = scsi_4btoul(cdb->lb_count); 5640 break; 5641 } 5642 default: 5643 ctl_set_invalid_opcode(ctsio); 5644 ctl_done((union ctl_io *)ctsio); 5645 goto bailout; 5646 break; /* NOTREACHED */ 5647 } 5648 5649 /* 5650 * We check the LBA and length, but don't do anything with them. 5651 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5652 * get flushed. This check will just help satisfy anyone who wants 5653 * to see an error for an out of range LBA. 5654 */ 5655 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5656 ctl_set_lba_out_of_range(ctsio); 5657 ctl_done((union ctl_io *)ctsio); 5658 goto bailout; 5659 } 5660 5661 /* 5662 * If this LUN has no backend, we can't flush the cache anyway. 5663 */ 5664 if (lun->backend == NULL) { 5665 ctl_set_invalid_opcode(ctsio); 5666 ctl_done((union ctl_io *)ctsio); 5667 goto bailout; 5668 } 5669 5670 /* 5671 * Check to see whether we're configured to send the SYNCHRONIZE 5672 * CACHE command directly to the back end. 5673 */ 5674 mtx_lock(&lun->lun_lock); 5675 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5676 && (++(lun->sync_count) >= lun->sync_interval)) { 5677 lun->sync_count = 0; 5678 mtx_unlock(&lun->lun_lock); 5679 retval = lun->backend->config_write((union ctl_io *)ctsio); 5680 } else { 5681 mtx_unlock(&lun->lun_lock); 5682 ctl_set_success(ctsio); 5683 ctl_done((union ctl_io *)ctsio); 5684 } 5685 5686bailout: 5687 5688 return (retval); 5689} 5690 5691int 5692ctl_format(struct ctl_scsiio *ctsio) 5693{ 5694 struct scsi_format *cdb; 5695 struct ctl_lun *lun; 5696 struct ctl_softc *ctl_softc; 5697 int length, defect_list_len; 5698 5699 CTL_DEBUG_PRINT(("ctl_format\n")); 5700 5701 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5702 ctl_softc = control_softc; 5703 5704 cdb = (struct scsi_format *)ctsio->cdb; 5705 5706 length = 0; 5707 if (cdb->byte2 & SF_FMTDATA) { 5708 if (cdb->byte2 & SF_LONGLIST) 5709 length = sizeof(struct scsi_format_header_long); 5710 else 5711 length = sizeof(struct scsi_format_header_short); 5712 } 5713 5714 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5715 && (length > 0)) { 5716 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5717 ctsio->kern_data_len = length; 5718 ctsio->kern_total_len = length; 5719 ctsio->kern_data_resid = 0; 5720 ctsio->kern_rel_offset = 0; 5721 ctsio->kern_sg_entries = 0; 5722 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5723 ctsio->be_move_done = ctl_config_move_done; 5724 ctl_datamove((union ctl_io *)ctsio); 5725 5726 return (CTL_RETVAL_COMPLETE); 5727 } 5728 5729 defect_list_len = 0; 5730 5731 if (cdb->byte2 & SF_FMTDATA) { 5732 if (cdb->byte2 & SF_LONGLIST) { 5733 struct scsi_format_header_long *header; 5734 5735 header = (struct scsi_format_header_long *) 5736 ctsio->kern_data_ptr; 5737 5738 defect_list_len = scsi_4btoul(header->defect_list_len); 5739 if (defect_list_len != 0) { 5740 ctl_set_invalid_field(ctsio, 5741 /*sks_valid*/ 1, 5742 /*command*/ 0, 5743 /*field*/ 2, 5744 /*bit_valid*/ 0, 5745 /*bit*/ 0); 5746 goto bailout; 5747 } 5748 } else { 5749 struct scsi_format_header_short *header; 5750 5751 header = (struct scsi_format_header_short *) 5752 ctsio->kern_data_ptr; 5753 5754 defect_list_len = scsi_2btoul(header->defect_list_len); 5755 if (defect_list_len != 0) { 5756 ctl_set_invalid_field(ctsio, 5757 /*sks_valid*/ 1, 5758 /*command*/ 0, 5759 /*field*/ 2, 5760 /*bit_valid*/ 0, 5761 /*bit*/ 0); 5762 goto bailout; 5763 } 5764 } 5765 } 5766 5767 /* 5768 * The format command will clear out the "Medium format corrupted" 5769 * status if set by the configuration code. That status is really 5770 * just a way to notify the host that we have lost the media, and 5771 * get them to issue a command that will basically make them think 5772 * they're blowing away the media. 5773 */ 5774 mtx_lock(&lun->lun_lock); 5775 lun->flags &= ~CTL_LUN_INOPERABLE; 5776 mtx_unlock(&lun->lun_lock); 5777 5778 ctsio->scsi_status = SCSI_STATUS_OK; 5779 ctsio->io_hdr.status = CTL_SUCCESS; 5780bailout: 5781 5782 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5783 free(ctsio->kern_data_ptr, M_CTL); 5784 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5785 } 5786 5787 ctl_done((union ctl_io *)ctsio); 5788 return (CTL_RETVAL_COMPLETE); 5789} 5790 5791int 5792ctl_read_buffer(struct ctl_scsiio *ctsio) 5793{ 5794 struct scsi_read_buffer *cdb; 5795 struct ctl_lun *lun; 5796 int buffer_offset, len; 5797 static uint8_t descr[4]; 5798 static uint8_t echo_descr[4] = { 0 }; 5799 5800 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5801 5802 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5803 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5804 5805 if (lun->flags & CTL_LUN_PR_RESERVED) { 5806 uint32_t residx; 5807 5808 /* 5809 * XXX KDM need a lock here. 5810 */ 5811 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5812 if ((lun->res_type == SPR_TYPE_EX_AC 5813 && residx != lun->pr_res_idx) 5814 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5815 || lun->res_type == SPR_TYPE_EX_AC_AR) 5816 && !lun->per_res[residx].registered)) { 5817 ctl_set_reservation_conflict(ctsio); 5818 ctl_done((union ctl_io *)ctsio); 5819 return (CTL_RETVAL_COMPLETE); 5820 } 5821 } 5822 5823 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5824 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5825 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5826 ctl_set_invalid_field(ctsio, 5827 /*sks_valid*/ 1, 5828 /*command*/ 1, 5829 /*field*/ 1, 5830 /*bit_valid*/ 1, 5831 /*bit*/ 4); 5832 ctl_done((union ctl_io *)ctsio); 5833 return (CTL_RETVAL_COMPLETE); 5834 } 5835 5836 len = scsi_3btoul(cdb->length); 5837 buffer_offset = scsi_3btoul(cdb->offset); 5838 5839 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5840 ctl_set_invalid_field(ctsio, 5841 /*sks_valid*/ 1, 5842 /*command*/ 1, 5843 /*field*/ 6, 5844 /*bit_valid*/ 0, 5845 /*bit*/ 0); 5846 ctl_done((union ctl_io *)ctsio); 5847 return (CTL_RETVAL_COMPLETE); 5848 } 5849 5850 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5851 descr[0] = 0; 5852 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5853 ctsio->kern_data_ptr = descr; 5854 len = min(len, sizeof(descr)); 5855 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5856 ctsio->kern_data_ptr = echo_descr; 5857 len = min(len, sizeof(echo_descr)); 5858 } else 5859 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5860 ctsio->kern_data_len = len; 5861 ctsio->kern_total_len = len; 5862 ctsio->kern_data_resid = 0; 5863 ctsio->kern_rel_offset = 0; 5864 ctsio->kern_sg_entries = 0; 5865 ctsio->be_move_done = ctl_config_move_done; 5866 ctl_datamove((union ctl_io *)ctsio); 5867 5868 return (CTL_RETVAL_COMPLETE); 5869} 5870 5871int 5872ctl_write_buffer(struct ctl_scsiio *ctsio) 5873{ 5874 struct scsi_write_buffer *cdb; 5875 struct ctl_lun *lun; 5876 int buffer_offset, len; 5877 5878 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5879 5880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5881 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5882 5883 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5884 ctl_set_invalid_field(ctsio, 5885 /*sks_valid*/ 1, 5886 /*command*/ 1, 5887 /*field*/ 1, 5888 /*bit_valid*/ 1, 5889 /*bit*/ 4); 5890 ctl_done((union ctl_io *)ctsio); 5891 return (CTL_RETVAL_COMPLETE); 5892 } 5893 5894 len = scsi_3btoul(cdb->length); 5895 buffer_offset = scsi_3btoul(cdb->offset); 5896 5897 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5898 ctl_set_invalid_field(ctsio, 5899 /*sks_valid*/ 1, 5900 /*command*/ 1, 5901 /*field*/ 6, 5902 /*bit_valid*/ 0, 5903 /*bit*/ 0); 5904 ctl_done((union ctl_io *)ctsio); 5905 return (CTL_RETVAL_COMPLETE); 5906 } 5907 5908 /* 5909 * If we've got a kernel request that hasn't been malloced yet, 5910 * malloc it and tell the caller the data buffer is here. 5911 */ 5912 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5913 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5914 ctsio->kern_data_len = len; 5915 ctsio->kern_total_len = len; 5916 ctsio->kern_data_resid = 0; 5917 ctsio->kern_rel_offset = 0; 5918 ctsio->kern_sg_entries = 0; 5919 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5920 ctsio->be_move_done = ctl_config_move_done; 5921 ctl_datamove((union ctl_io *)ctsio); 5922 5923 return (CTL_RETVAL_COMPLETE); 5924 } 5925 5926 ctl_done((union ctl_io *)ctsio); 5927 5928 return (CTL_RETVAL_COMPLETE); 5929} 5930 5931int 5932ctl_write_same(struct ctl_scsiio *ctsio) 5933{ 5934 struct ctl_lun *lun; 5935 struct ctl_lba_len_flags *lbalen; 5936 uint64_t lba; 5937 uint32_t num_blocks; 5938 int len, retval; 5939 uint8_t byte2; 5940 5941 retval = CTL_RETVAL_COMPLETE; 5942 5943 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5944 5945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5946 5947 switch (ctsio->cdb[0]) { 5948 case WRITE_SAME_10: { 5949 struct scsi_write_same_10 *cdb; 5950 5951 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5952 5953 lba = scsi_4btoul(cdb->addr); 5954 num_blocks = scsi_2btoul(cdb->length); 5955 byte2 = cdb->byte2; 5956 break; 5957 } 5958 case WRITE_SAME_16: { 5959 struct scsi_write_same_16 *cdb; 5960 5961 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5962 5963 lba = scsi_8btou64(cdb->addr); 5964 num_blocks = scsi_4btoul(cdb->length); 5965 byte2 = cdb->byte2; 5966 break; 5967 } 5968 default: 5969 /* 5970 * We got a command we don't support. This shouldn't 5971 * happen, commands should be filtered out above us. 5972 */ 5973 ctl_set_invalid_opcode(ctsio); 5974 ctl_done((union ctl_io *)ctsio); 5975 5976 return (CTL_RETVAL_COMPLETE); 5977 break; /* NOTREACHED */ 5978 } 5979 5980 /* 5981 * The first check is to make sure we're in bounds, the second 5982 * check is to catch wrap-around problems. If the lba + num blocks 5983 * is less than the lba, then we've wrapped around and the block 5984 * range is invalid anyway. 5985 */ 5986 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5987 || ((lba + num_blocks) < lba)) { 5988 ctl_set_lba_out_of_range(ctsio); 5989 ctl_done((union ctl_io *)ctsio); 5990 return (CTL_RETVAL_COMPLETE); 5991 } 5992 5993 /* Zero number of blocks means "to the last logical block" */ 5994 if (num_blocks == 0) { 5995 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5996 ctl_set_invalid_field(ctsio, 5997 /*sks_valid*/ 0, 5998 /*command*/ 1, 5999 /*field*/ 0, 6000 /*bit_valid*/ 0, 6001 /*bit*/ 0); 6002 ctl_done((union ctl_io *)ctsio); 6003 return (CTL_RETVAL_COMPLETE); 6004 } 6005 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6006 } 6007 6008 len = lun->be_lun->blocksize; 6009 6010 /* 6011 * If we've got a kernel request that hasn't been malloced yet, 6012 * malloc it and tell the caller the data buffer is here. 6013 */ 6014 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6015 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6016 ctsio->kern_data_len = len; 6017 ctsio->kern_total_len = len; 6018 ctsio->kern_data_resid = 0; 6019 ctsio->kern_rel_offset = 0; 6020 ctsio->kern_sg_entries = 0; 6021 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6022 ctsio->be_move_done = ctl_config_move_done; 6023 ctl_datamove((union ctl_io *)ctsio); 6024 6025 return (CTL_RETVAL_COMPLETE); 6026 } 6027 6028 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6029 lbalen->lba = lba; 6030 lbalen->len = num_blocks; 6031 lbalen->flags = byte2; 6032 retval = lun->backend->config_write((union ctl_io *)ctsio); 6033 6034 return (retval); 6035} 6036 6037int 6038ctl_unmap(struct ctl_scsiio *ctsio) 6039{ 6040 struct ctl_lun *lun; 6041 struct scsi_unmap *cdb; 6042 struct ctl_ptr_len_flags *ptrlen; 6043 struct scsi_unmap_header *hdr; 6044 struct scsi_unmap_desc *buf, *end, *endnz, *range; 6045 uint64_t lba; 6046 uint32_t num_blocks; 6047 int len, retval; 6048 uint8_t byte2; 6049 6050 retval = CTL_RETVAL_COMPLETE; 6051 6052 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6053 6054 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6055 cdb = (struct scsi_unmap *)ctsio->cdb; 6056 6057 len = scsi_2btoul(cdb->length); 6058 byte2 = cdb->byte2; 6059 6060 /* 6061 * If we've got a kernel request that hasn't been malloced yet, 6062 * malloc it and tell the caller the data buffer is here. 6063 */ 6064 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6065 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6066 ctsio->kern_data_len = len; 6067 ctsio->kern_total_len = len; 6068 ctsio->kern_data_resid = 0; 6069 ctsio->kern_rel_offset = 0; 6070 ctsio->kern_sg_entries = 0; 6071 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6072 ctsio->be_move_done = ctl_config_move_done; 6073 ctl_datamove((union ctl_io *)ctsio); 6074 6075 return (CTL_RETVAL_COMPLETE); 6076 } 6077 6078 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6079 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6080 if (len < sizeof (*hdr) || 6081 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6082 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6083 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6084 ctl_set_invalid_field(ctsio, 6085 /*sks_valid*/ 0, 6086 /*command*/ 0, 6087 /*field*/ 0, 6088 /*bit_valid*/ 0, 6089 /*bit*/ 0); 6090 ctl_done((union ctl_io *)ctsio); 6091 return (CTL_RETVAL_COMPLETE); 6092 } 6093 len = scsi_2btoul(hdr->desc_length); 6094 buf = (struct scsi_unmap_desc *)(hdr + 1); 6095 end = buf + len / sizeof(*buf); 6096 6097 endnz = buf; 6098 for (range = buf; range < end; range++) { 6099 lba = scsi_8btou64(range->lba); 6100 num_blocks = scsi_4btoul(range->length); 6101 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6102 || ((lba + num_blocks) < lba)) { 6103 ctl_set_lba_out_of_range(ctsio); 6104 ctl_done((union ctl_io *)ctsio); 6105 return (CTL_RETVAL_COMPLETE); 6106 } 6107 if (num_blocks != 0) 6108 endnz = range + 1; 6109 } 6110 6111 /* 6112 * Block backend can not handle zero last range. 6113 * Filter it out and return if there is nothing left. 6114 */ 6115 len = (uint8_t *)endnz - (uint8_t *)buf; 6116 if (len == 0) { 6117 ctl_set_success(ctsio); 6118 ctl_done((union ctl_io *)ctsio); 6119 return (CTL_RETVAL_COMPLETE); 6120 } 6121 6122 ptrlen = (struct ctl_ptr_len_flags *) 6123 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6124 ptrlen->ptr = (void *)buf; 6125 ptrlen->len = len; 6126 ptrlen->flags = byte2; 6127 6128 retval = lun->backend->config_write((union ctl_io *)ctsio); 6129 return (retval); 6130} 6131 6132/* 6133 * Note that this function currently doesn't actually do anything inside 6134 * CTL to enforce things if the DQue bit is turned on. 6135 * 6136 * Also note that this function can't be used in the default case, because 6137 * the DQue bit isn't set in the changeable mask for the control mode page 6138 * anyway. This is just here as an example for how to implement a page 6139 * handler, and a placeholder in case we want to allow the user to turn 6140 * tagged queueing on and off. 6141 * 6142 * The D_SENSE bit handling is functional, however, and will turn 6143 * descriptor sense on and off for a given LUN. 6144 */ 6145int 6146ctl_control_page_handler(struct ctl_scsiio *ctsio, 6147 struct ctl_page_index *page_index, uint8_t *page_ptr) 6148{ 6149 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6150 struct ctl_lun *lun; 6151 struct ctl_softc *softc; 6152 int set_ua; 6153 uint32_t initidx; 6154 6155 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6156 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6157 set_ua = 0; 6158 6159 user_cp = (struct scsi_control_page *)page_ptr; 6160 current_cp = (struct scsi_control_page *) 6161 (page_index->page_data + (page_index->page_len * 6162 CTL_PAGE_CURRENT)); 6163 saved_cp = (struct scsi_control_page *) 6164 (page_index->page_data + (page_index->page_len * 6165 CTL_PAGE_SAVED)); 6166 6167 softc = control_softc; 6168 6169 mtx_lock(&lun->lun_lock); 6170 if (((current_cp->rlec & SCP_DSENSE) == 0) 6171 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6172 /* 6173 * Descriptor sense is currently turned off and the user 6174 * wants to turn it on. 6175 */ 6176 current_cp->rlec |= SCP_DSENSE; 6177 saved_cp->rlec |= SCP_DSENSE; 6178 lun->flags |= CTL_LUN_SENSE_DESC; 6179 set_ua = 1; 6180 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6181 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6182 /* 6183 * Descriptor sense is currently turned on, and the user 6184 * wants to turn it off. 6185 */ 6186 current_cp->rlec &= ~SCP_DSENSE; 6187 saved_cp->rlec &= ~SCP_DSENSE; 6188 lun->flags &= ~CTL_LUN_SENSE_DESC; 6189 set_ua = 1; 6190 } 6191 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6192 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6193#ifdef NEEDTOPORT 6194 csevent_log(CSC_CTL | CSC_SHELF_SW | 6195 CTL_UNTAG_TO_UNTAG, 6196 csevent_LogType_Trace, 6197 csevent_Severity_Information, 6198 csevent_AlertLevel_Green, 6199 csevent_FRU_Firmware, 6200 csevent_FRU_Unknown, 6201 "Received untagged to untagged transition"); 6202#endif /* NEEDTOPORT */ 6203 } else { 6204#ifdef NEEDTOPORT 6205 csevent_log(CSC_CTL | CSC_SHELF_SW | 6206 CTL_UNTAG_TO_TAG, 6207 csevent_LogType_ConfigChange, 6208 csevent_Severity_Information, 6209 csevent_AlertLevel_Green, 6210 csevent_FRU_Firmware, 6211 csevent_FRU_Unknown, 6212 "Received untagged to tagged " 6213 "queueing transition"); 6214#endif /* NEEDTOPORT */ 6215 6216 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6217 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6218 set_ua = 1; 6219 } 6220 } else { 6221 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6222#ifdef NEEDTOPORT 6223 csevent_log(CSC_CTL | CSC_SHELF_SW | 6224 CTL_TAG_TO_UNTAG, 6225 csevent_LogType_ConfigChange, 6226 csevent_Severity_Warning, 6227 csevent_AlertLevel_Yellow, 6228 csevent_FRU_Firmware, 6229 csevent_FRU_Unknown, 6230 "Received tagged queueing to untagged " 6231 "transition"); 6232#endif /* NEEDTOPORT */ 6233 6234 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6235 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6236 set_ua = 1; 6237 } else { 6238#ifdef NEEDTOPORT 6239 csevent_log(CSC_CTL | CSC_SHELF_SW | 6240 CTL_TAG_TO_TAG, 6241 csevent_LogType_Trace, 6242 csevent_Severity_Information, 6243 csevent_AlertLevel_Green, 6244 csevent_FRU_Firmware, 6245 csevent_FRU_Unknown, 6246 "Received tagged queueing to tagged " 6247 "queueing transition"); 6248#endif /* NEEDTOPORT */ 6249 } 6250 } 6251 if (set_ua != 0) { 6252 int i; 6253 /* 6254 * Let other initiators know that the mode 6255 * parameters for this LUN have changed. 6256 */ 6257 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6258 if (i == initidx) 6259 continue; 6260 6261 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6262 } 6263 } 6264 mtx_unlock(&lun->lun_lock); 6265 6266 return (0); 6267} 6268 6269int 6270ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6271 struct ctl_page_index *page_index, uint8_t *page_ptr) 6272{ 6273 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6274 struct ctl_lun *lun; 6275 int set_ua; 6276 uint32_t initidx; 6277 6278 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6279 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6280 set_ua = 0; 6281 6282 user_cp = (struct scsi_caching_page *)page_ptr; 6283 current_cp = (struct scsi_caching_page *) 6284 (page_index->page_data + (page_index->page_len * 6285 CTL_PAGE_CURRENT)); 6286 saved_cp = (struct scsi_caching_page *) 6287 (page_index->page_data + (page_index->page_len * 6288 CTL_PAGE_SAVED)); 6289 6290 mtx_lock(&lun->lun_lock); 6291 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6292 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) 6293 set_ua = 1; 6294 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6295 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6296 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6297 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6298 if (set_ua != 0) { 6299 int i; 6300 /* 6301 * Let other initiators know that the mode 6302 * parameters for this LUN have changed. 6303 */ 6304 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6305 if (i == initidx) 6306 continue; 6307 6308 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6309 } 6310 } 6311 mtx_unlock(&lun->lun_lock); 6312 6313 return (0); 6314} 6315 6316int 6317ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6318 struct ctl_page_index *page_index, uint8_t *page_ptr) 6319{ 6320 return (0); 6321} 6322 6323int 6324ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6325 struct ctl_page_index *page_index, int pc) 6326{ 6327 struct copan_power_subpage *page; 6328 6329 page = (struct copan_power_subpage *)page_index->page_data + 6330 (page_index->page_len * pc); 6331 6332 switch (pc) { 6333 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6334 /* 6335 * We don't update the changable bits for this page. 6336 */ 6337 break; 6338 case SMS_PAGE_CTRL_CURRENT >> 6: 6339 case SMS_PAGE_CTRL_DEFAULT >> 6: 6340 case SMS_PAGE_CTRL_SAVED >> 6: 6341#ifdef NEEDTOPORT 6342 ctl_update_power_subpage(page); 6343#endif 6344 break; 6345 default: 6346#ifdef NEEDTOPORT 6347 EPRINT(0, "Invalid PC %d!!", pc); 6348#endif 6349 break; 6350 } 6351 return (0); 6352} 6353 6354 6355int 6356ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6357 struct ctl_page_index *page_index, uint8_t *page_ptr) 6358{ 6359 struct copan_aps_subpage *user_sp; 6360 struct copan_aps_subpage *current_sp; 6361 union ctl_modepage_info *modepage_info; 6362 struct ctl_softc *softc; 6363 struct ctl_lun *lun; 6364 int retval; 6365 6366 retval = CTL_RETVAL_COMPLETE; 6367 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6368 (page_index->page_len * CTL_PAGE_CURRENT)); 6369 softc = control_softc; 6370 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6371 6372 user_sp = (struct copan_aps_subpage *)page_ptr; 6373 6374 modepage_info = (union ctl_modepage_info *) 6375 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6376 6377 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6378 modepage_info->header.subpage = page_index->subpage; 6379 modepage_info->aps.lock_active = user_sp->lock_active; 6380 6381 mtx_lock(&softc->ctl_lock); 6382 6383 /* 6384 * If there is a request to lock the LUN and another LUN is locked 6385 * this is an error. If the requested LUN is already locked ignore 6386 * the request. If no LUN is locked attempt to lock it. 6387 * if there is a request to unlock the LUN and the LUN is currently 6388 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6389 * if another LUN is locked or no LUN is locked. 6390 */ 6391 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6392 if (softc->aps_locked_lun == lun->lun) { 6393 /* 6394 * This LUN is already locked, so we're done. 6395 */ 6396 retval = CTL_RETVAL_COMPLETE; 6397 } else if (softc->aps_locked_lun == 0) { 6398 /* 6399 * No one has the lock, pass the request to the 6400 * backend. 6401 */ 6402 retval = lun->backend->config_write( 6403 (union ctl_io *)ctsio); 6404 } else { 6405 /* 6406 * Someone else has the lock, throw out the request. 6407 */ 6408 ctl_set_already_locked(ctsio); 6409 free(ctsio->kern_data_ptr, M_CTL); 6410 ctl_done((union ctl_io *)ctsio); 6411 6412 /* 6413 * Set the return value so that ctl_do_mode_select() 6414 * won't try to complete the command. We already 6415 * completed it here. 6416 */ 6417 retval = CTL_RETVAL_ERROR; 6418 } 6419 } else if (softc->aps_locked_lun == lun->lun) { 6420 /* 6421 * This LUN is locked, so pass the unlock request to the 6422 * backend. 6423 */ 6424 retval = lun->backend->config_write((union ctl_io *)ctsio); 6425 } 6426 mtx_unlock(&softc->ctl_lock); 6427 6428 return (retval); 6429} 6430 6431int 6432ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6433 struct ctl_page_index *page_index, 6434 uint8_t *page_ptr) 6435{ 6436 uint8_t *c; 6437 int i; 6438 6439 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6440 ctl_time_io_secs = 6441 (c[0] << 8) | 6442 (c[1] << 0) | 6443 0; 6444 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6445 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6446 printf("page data:"); 6447 for (i=0; i<8; i++) 6448 printf(" %.2x",page_ptr[i]); 6449 printf("\n"); 6450 return (0); 6451} 6452 6453int 6454ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6455 struct ctl_page_index *page_index, 6456 int pc) 6457{ 6458 struct copan_debugconf_subpage *page; 6459 6460 page = (struct copan_debugconf_subpage *)page_index->page_data + 6461 (page_index->page_len * pc); 6462 6463 switch (pc) { 6464 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6465 case SMS_PAGE_CTRL_DEFAULT >> 6: 6466 case SMS_PAGE_CTRL_SAVED >> 6: 6467 /* 6468 * We don't update the changable or default bits for this page. 6469 */ 6470 break; 6471 case SMS_PAGE_CTRL_CURRENT >> 6: 6472 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6473 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6474 break; 6475 default: 6476#ifdef NEEDTOPORT 6477 EPRINT(0, "Invalid PC %d!!", pc); 6478#endif /* NEEDTOPORT */ 6479 break; 6480 } 6481 return (0); 6482} 6483 6484 6485static int 6486ctl_do_mode_select(union ctl_io *io) 6487{ 6488 struct scsi_mode_page_header *page_header; 6489 struct ctl_page_index *page_index; 6490 struct ctl_scsiio *ctsio; 6491 int control_dev, page_len; 6492 int page_len_offset, page_len_size; 6493 union ctl_modepage_info *modepage_info; 6494 struct ctl_lun *lun; 6495 int *len_left, *len_used; 6496 int retval, i; 6497 6498 ctsio = &io->scsiio; 6499 page_index = NULL; 6500 page_len = 0; 6501 retval = CTL_RETVAL_COMPLETE; 6502 6503 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6504 6505 if (lun->be_lun->lun_type != T_DIRECT) 6506 control_dev = 1; 6507 else 6508 control_dev = 0; 6509 6510 modepage_info = (union ctl_modepage_info *) 6511 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6512 len_left = &modepage_info->header.len_left; 6513 len_used = &modepage_info->header.len_used; 6514 6515do_next_page: 6516 6517 page_header = (struct scsi_mode_page_header *) 6518 (ctsio->kern_data_ptr + *len_used); 6519 6520 if (*len_left == 0) { 6521 free(ctsio->kern_data_ptr, M_CTL); 6522 ctl_set_success(ctsio); 6523 ctl_done((union ctl_io *)ctsio); 6524 return (CTL_RETVAL_COMPLETE); 6525 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6526 6527 free(ctsio->kern_data_ptr, M_CTL); 6528 ctl_set_param_len_error(ctsio); 6529 ctl_done((union ctl_io *)ctsio); 6530 return (CTL_RETVAL_COMPLETE); 6531 6532 } else if ((page_header->page_code & SMPH_SPF) 6533 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6534 6535 free(ctsio->kern_data_ptr, M_CTL); 6536 ctl_set_param_len_error(ctsio); 6537 ctl_done((union ctl_io *)ctsio); 6538 return (CTL_RETVAL_COMPLETE); 6539 } 6540 6541 6542 /* 6543 * XXX KDM should we do something with the block descriptor? 6544 */ 6545 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6546 6547 if ((control_dev != 0) 6548 && (lun->mode_pages.index[i].page_flags & 6549 CTL_PAGE_FLAG_DISK_ONLY)) 6550 continue; 6551 6552 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6553 (page_header->page_code & SMPH_PC_MASK)) 6554 continue; 6555 6556 /* 6557 * If neither page has a subpage code, then we've got a 6558 * match. 6559 */ 6560 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6561 && ((page_header->page_code & SMPH_SPF) == 0)) { 6562 page_index = &lun->mode_pages.index[i]; 6563 page_len = page_header->page_length; 6564 break; 6565 } 6566 6567 /* 6568 * If both pages have subpages, then the subpage numbers 6569 * have to match. 6570 */ 6571 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6572 && (page_header->page_code & SMPH_SPF)) { 6573 struct scsi_mode_page_header_sp *sph; 6574 6575 sph = (struct scsi_mode_page_header_sp *)page_header; 6576 6577 if (lun->mode_pages.index[i].subpage == 6578 sph->subpage) { 6579 page_index = &lun->mode_pages.index[i]; 6580 page_len = scsi_2btoul(sph->page_length); 6581 break; 6582 } 6583 } 6584 } 6585 6586 /* 6587 * If we couldn't find the page, or if we don't have a mode select 6588 * handler for it, send back an error to the user. 6589 */ 6590 if ((page_index == NULL) 6591 || (page_index->select_handler == NULL)) { 6592 ctl_set_invalid_field(ctsio, 6593 /*sks_valid*/ 1, 6594 /*command*/ 0, 6595 /*field*/ *len_used, 6596 /*bit_valid*/ 0, 6597 /*bit*/ 0); 6598 free(ctsio->kern_data_ptr, M_CTL); 6599 ctl_done((union ctl_io *)ctsio); 6600 return (CTL_RETVAL_COMPLETE); 6601 } 6602 6603 if (page_index->page_code & SMPH_SPF) { 6604 page_len_offset = 2; 6605 page_len_size = 2; 6606 } else { 6607 page_len_size = 1; 6608 page_len_offset = 1; 6609 } 6610 6611 /* 6612 * If the length the initiator gives us isn't the one we specify in 6613 * the mode page header, or if they didn't specify enough data in 6614 * the CDB to avoid truncating this page, kick out the request. 6615 */ 6616 if ((page_len != (page_index->page_len - page_len_offset - 6617 page_len_size)) 6618 || (*len_left < page_index->page_len)) { 6619 6620 6621 ctl_set_invalid_field(ctsio, 6622 /*sks_valid*/ 1, 6623 /*command*/ 0, 6624 /*field*/ *len_used + page_len_offset, 6625 /*bit_valid*/ 0, 6626 /*bit*/ 0); 6627 free(ctsio->kern_data_ptr, M_CTL); 6628 ctl_done((union ctl_io *)ctsio); 6629 return (CTL_RETVAL_COMPLETE); 6630 } 6631 6632 /* 6633 * Run through the mode page, checking to make sure that the bits 6634 * the user changed are actually legal for him to change. 6635 */ 6636 for (i = 0; i < page_index->page_len; i++) { 6637 uint8_t *user_byte, *change_mask, *current_byte; 6638 int bad_bit; 6639 int j; 6640 6641 user_byte = (uint8_t *)page_header + i; 6642 change_mask = page_index->page_data + 6643 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6644 current_byte = page_index->page_data + 6645 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6646 6647 /* 6648 * Check to see whether the user set any bits in this byte 6649 * that he is not allowed to set. 6650 */ 6651 if ((*user_byte & ~(*change_mask)) == 6652 (*current_byte & ~(*change_mask))) 6653 continue; 6654 6655 /* 6656 * Go through bit by bit to determine which one is illegal. 6657 */ 6658 bad_bit = 0; 6659 for (j = 7; j >= 0; j--) { 6660 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6661 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6662 bad_bit = i; 6663 break; 6664 } 6665 } 6666 ctl_set_invalid_field(ctsio, 6667 /*sks_valid*/ 1, 6668 /*command*/ 0, 6669 /*field*/ *len_used + i, 6670 /*bit_valid*/ 1, 6671 /*bit*/ bad_bit); 6672 free(ctsio->kern_data_ptr, M_CTL); 6673 ctl_done((union ctl_io *)ctsio); 6674 return (CTL_RETVAL_COMPLETE); 6675 } 6676 6677 /* 6678 * Decrement these before we call the page handler, since we may 6679 * end up getting called back one way or another before the handler 6680 * returns to this context. 6681 */ 6682 *len_left -= page_index->page_len; 6683 *len_used += page_index->page_len; 6684 6685 retval = page_index->select_handler(ctsio, page_index, 6686 (uint8_t *)page_header); 6687 6688 /* 6689 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6690 * wait until this queued command completes to finish processing 6691 * the mode page. If it returns anything other than 6692 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6693 * already set the sense information, freed the data pointer, and 6694 * completed the io for us. 6695 */ 6696 if (retval != CTL_RETVAL_COMPLETE) 6697 goto bailout_no_done; 6698 6699 /* 6700 * If the initiator sent us more than one page, parse the next one. 6701 */ 6702 if (*len_left > 0) 6703 goto do_next_page; 6704 6705 ctl_set_success(ctsio); 6706 free(ctsio->kern_data_ptr, M_CTL); 6707 ctl_done((union ctl_io *)ctsio); 6708 6709bailout_no_done: 6710 6711 return (CTL_RETVAL_COMPLETE); 6712 6713} 6714 6715int 6716ctl_mode_select(struct ctl_scsiio *ctsio) 6717{ 6718 int param_len, pf, sp; 6719 int header_size, bd_len; 6720 int len_left, len_used; 6721 struct ctl_page_index *page_index; 6722 struct ctl_lun *lun; 6723 int control_dev, page_len; 6724 union ctl_modepage_info *modepage_info; 6725 int retval; 6726 6727 pf = 0; 6728 sp = 0; 6729 page_len = 0; 6730 len_used = 0; 6731 len_left = 0; 6732 retval = 0; 6733 bd_len = 0; 6734 page_index = NULL; 6735 6736 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6737 6738 if (lun->be_lun->lun_type != T_DIRECT) 6739 control_dev = 1; 6740 else 6741 control_dev = 0; 6742 6743 switch (ctsio->cdb[0]) { 6744 case MODE_SELECT_6: { 6745 struct scsi_mode_select_6 *cdb; 6746 6747 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6748 6749 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6750 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6751 6752 param_len = cdb->length; 6753 header_size = sizeof(struct scsi_mode_header_6); 6754 break; 6755 } 6756 case MODE_SELECT_10: { 6757 struct scsi_mode_select_10 *cdb; 6758 6759 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6760 6761 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6762 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6763 6764 param_len = scsi_2btoul(cdb->length); 6765 header_size = sizeof(struct scsi_mode_header_10); 6766 break; 6767 } 6768 default: 6769 ctl_set_invalid_opcode(ctsio); 6770 ctl_done((union ctl_io *)ctsio); 6771 return (CTL_RETVAL_COMPLETE); 6772 break; /* NOTREACHED */ 6773 } 6774 6775 /* 6776 * From SPC-3: 6777 * "A parameter list length of zero indicates that the Data-Out Buffer 6778 * shall be empty. This condition shall not be considered as an error." 6779 */ 6780 if (param_len == 0) { 6781 ctl_set_success(ctsio); 6782 ctl_done((union ctl_io *)ctsio); 6783 return (CTL_RETVAL_COMPLETE); 6784 } 6785 6786 /* 6787 * Since we'll hit this the first time through, prior to 6788 * allocation, we don't need to free a data buffer here. 6789 */ 6790 if (param_len < header_size) { 6791 ctl_set_param_len_error(ctsio); 6792 ctl_done((union ctl_io *)ctsio); 6793 return (CTL_RETVAL_COMPLETE); 6794 } 6795 6796 /* 6797 * Allocate the data buffer and grab the user's data. In theory, 6798 * we shouldn't have to sanity check the parameter list length here 6799 * because the maximum size is 64K. We should be able to malloc 6800 * that much without too many problems. 6801 */ 6802 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6803 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6804 ctsio->kern_data_len = param_len; 6805 ctsio->kern_total_len = param_len; 6806 ctsio->kern_data_resid = 0; 6807 ctsio->kern_rel_offset = 0; 6808 ctsio->kern_sg_entries = 0; 6809 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6810 ctsio->be_move_done = ctl_config_move_done; 6811 ctl_datamove((union ctl_io *)ctsio); 6812 6813 return (CTL_RETVAL_COMPLETE); 6814 } 6815 6816 switch (ctsio->cdb[0]) { 6817 case MODE_SELECT_6: { 6818 struct scsi_mode_header_6 *mh6; 6819 6820 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6821 bd_len = mh6->blk_desc_len; 6822 break; 6823 } 6824 case MODE_SELECT_10: { 6825 struct scsi_mode_header_10 *mh10; 6826 6827 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6828 bd_len = scsi_2btoul(mh10->blk_desc_len); 6829 break; 6830 } 6831 default: 6832 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6833 break; 6834 } 6835 6836 if (param_len < (header_size + bd_len)) { 6837 free(ctsio->kern_data_ptr, M_CTL); 6838 ctl_set_param_len_error(ctsio); 6839 ctl_done((union ctl_io *)ctsio); 6840 return (CTL_RETVAL_COMPLETE); 6841 } 6842 6843 /* 6844 * Set the IO_CONT flag, so that if this I/O gets passed to 6845 * ctl_config_write_done(), it'll get passed back to 6846 * ctl_do_mode_select() for further processing, or completion if 6847 * we're all done. 6848 */ 6849 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6850 ctsio->io_cont = ctl_do_mode_select; 6851 6852 modepage_info = (union ctl_modepage_info *) 6853 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6854 6855 memset(modepage_info, 0, sizeof(*modepage_info)); 6856 6857 len_left = param_len - header_size - bd_len; 6858 len_used = header_size + bd_len; 6859 6860 modepage_info->header.len_left = len_left; 6861 modepage_info->header.len_used = len_used; 6862 6863 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6864} 6865 6866int 6867ctl_mode_sense(struct ctl_scsiio *ctsio) 6868{ 6869 struct ctl_lun *lun; 6870 int pc, page_code, dbd, llba, subpage; 6871 int alloc_len, page_len, header_len, total_len; 6872 struct scsi_mode_block_descr *block_desc; 6873 struct ctl_page_index *page_index; 6874 int control_dev; 6875 6876 dbd = 0; 6877 llba = 0; 6878 block_desc = NULL; 6879 page_index = NULL; 6880 6881 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6882 6883 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6884 6885 if (lun->be_lun->lun_type != T_DIRECT) 6886 control_dev = 1; 6887 else 6888 control_dev = 0; 6889 6890 if (lun->flags & CTL_LUN_PR_RESERVED) { 6891 uint32_t residx; 6892 6893 /* 6894 * XXX KDM need a lock here. 6895 */ 6896 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6897 if ((lun->res_type == SPR_TYPE_EX_AC 6898 && residx != lun->pr_res_idx) 6899 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6900 || lun->res_type == SPR_TYPE_EX_AC_AR) 6901 && !lun->per_res[residx].registered)) { 6902 ctl_set_reservation_conflict(ctsio); 6903 ctl_done((union ctl_io *)ctsio); 6904 return (CTL_RETVAL_COMPLETE); 6905 } 6906 } 6907 6908 switch (ctsio->cdb[0]) { 6909 case MODE_SENSE_6: { 6910 struct scsi_mode_sense_6 *cdb; 6911 6912 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6913 6914 header_len = sizeof(struct scsi_mode_hdr_6); 6915 if (cdb->byte2 & SMS_DBD) 6916 dbd = 1; 6917 else 6918 header_len += sizeof(struct scsi_mode_block_descr); 6919 6920 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6921 page_code = cdb->page & SMS_PAGE_CODE; 6922 subpage = cdb->subpage; 6923 alloc_len = cdb->length; 6924 break; 6925 } 6926 case MODE_SENSE_10: { 6927 struct scsi_mode_sense_10 *cdb; 6928 6929 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6930 6931 header_len = sizeof(struct scsi_mode_hdr_10); 6932 6933 if (cdb->byte2 & SMS_DBD) 6934 dbd = 1; 6935 else 6936 header_len += sizeof(struct scsi_mode_block_descr); 6937 if (cdb->byte2 & SMS10_LLBAA) 6938 llba = 1; 6939 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6940 page_code = cdb->page & SMS_PAGE_CODE; 6941 subpage = cdb->subpage; 6942 alloc_len = scsi_2btoul(cdb->length); 6943 break; 6944 } 6945 default: 6946 ctl_set_invalid_opcode(ctsio); 6947 ctl_done((union ctl_io *)ctsio); 6948 return (CTL_RETVAL_COMPLETE); 6949 break; /* NOTREACHED */ 6950 } 6951 6952 /* 6953 * We have to make a first pass through to calculate the size of 6954 * the pages that match the user's query. Then we allocate enough 6955 * memory to hold it, and actually copy the data into the buffer. 6956 */ 6957 switch (page_code) { 6958 case SMS_ALL_PAGES_PAGE: { 6959 int i; 6960 6961 page_len = 0; 6962 6963 /* 6964 * At the moment, values other than 0 and 0xff here are 6965 * reserved according to SPC-3. 6966 */ 6967 if ((subpage != SMS_SUBPAGE_PAGE_0) 6968 && (subpage != SMS_SUBPAGE_ALL)) { 6969 ctl_set_invalid_field(ctsio, 6970 /*sks_valid*/ 1, 6971 /*command*/ 1, 6972 /*field*/ 3, 6973 /*bit_valid*/ 0, 6974 /*bit*/ 0); 6975 ctl_done((union ctl_io *)ctsio); 6976 return (CTL_RETVAL_COMPLETE); 6977 } 6978 6979 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6980 if ((control_dev != 0) 6981 && (lun->mode_pages.index[i].page_flags & 6982 CTL_PAGE_FLAG_DISK_ONLY)) 6983 continue; 6984 6985 /* 6986 * We don't use this subpage if the user didn't 6987 * request all subpages. 6988 */ 6989 if ((lun->mode_pages.index[i].subpage != 0) 6990 && (subpage == SMS_SUBPAGE_PAGE_0)) 6991 continue; 6992 6993#if 0 6994 printf("found page %#x len %d\n", 6995 lun->mode_pages.index[i].page_code & 6996 SMPH_PC_MASK, 6997 lun->mode_pages.index[i].page_len); 6998#endif 6999 page_len += lun->mode_pages.index[i].page_len; 7000 } 7001 break; 7002 } 7003 default: { 7004 int i; 7005 7006 page_len = 0; 7007 7008 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7009 /* Look for the right page code */ 7010 if ((lun->mode_pages.index[i].page_code & 7011 SMPH_PC_MASK) != page_code) 7012 continue; 7013 7014 /* Look for the right subpage or the subpage wildcard*/ 7015 if ((lun->mode_pages.index[i].subpage != subpage) 7016 && (subpage != SMS_SUBPAGE_ALL)) 7017 continue; 7018 7019 /* Make sure the page is supported for this dev type */ 7020 if ((control_dev != 0) 7021 && (lun->mode_pages.index[i].page_flags & 7022 CTL_PAGE_FLAG_DISK_ONLY)) 7023 continue; 7024 7025#if 0 7026 printf("found page %#x len %d\n", 7027 lun->mode_pages.index[i].page_code & 7028 SMPH_PC_MASK, 7029 lun->mode_pages.index[i].page_len); 7030#endif 7031 7032 page_len += lun->mode_pages.index[i].page_len; 7033 } 7034 7035 if (page_len == 0) { 7036 ctl_set_invalid_field(ctsio, 7037 /*sks_valid*/ 1, 7038 /*command*/ 1, 7039 /*field*/ 2, 7040 /*bit_valid*/ 1, 7041 /*bit*/ 5); 7042 ctl_done((union ctl_io *)ctsio); 7043 return (CTL_RETVAL_COMPLETE); 7044 } 7045 break; 7046 } 7047 } 7048 7049 total_len = header_len + page_len; 7050#if 0 7051 printf("header_len = %d, page_len = %d, total_len = %d\n", 7052 header_len, page_len, total_len); 7053#endif 7054 7055 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7056 ctsio->kern_sg_entries = 0; 7057 ctsio->kern_data_resid = 0; 7058 ctsio->kern_rel_offset = 0; 7059 if (total_len < alloc_len) { 7060 ctsio->residual = alloc_len - total_len; 7061 ctsio->kern_data_len = total_len; 7062 ctsio->kern_total_len = total_len; 7063 } else { 7064 ctsio->residual = 0; 7065 ctsio->kern_data_len = alloc_len; 7066 ctsio->kern_total_len = alloc_len; 7067 } 7068 7069 switch (ctsio->cdb[0]) { 7070 case MODE_SENSE_6: { 7071 struct scsi_mode_hdr_6 *header; 7072 7073 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7074 7075 header->datalen = ctl_min(total_len - 1, 254); 7076 if (control_dev == 0) 7077 header->dev_specific = 0x10; /* DPOFUA */ 7078 if (dbd) 7079 header->block_descr_len = 0; 7080 else 7081 header->block_descr_len = 7082 sizeof(struct scsi_mode_block_descr); 7083 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7084 break; 7085 } 7086 case MODE_SENSE_10: { 7087 struct scsi_mode_hdr_10 *header; 7088 int datalen; 7089 7090 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7091 7092 datalen = ctl_min(total_len - 2, 65533); 7093 scsi_ulto2b(datalen, header->datalen); 7094 if (control_dev == 0) 7095 header->dev_specific = 0x10; /* DPOFUA */ 7096 if (dbd) 7097 scsi_ulto2b(0, header->block_descr_len); 7098 else 7099 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7100 header->block_descr_len); 7101 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7102 break; 7103 } 7104 default: 7105 panic("invalid CDB type %#x", ctsio->cdb[0]); 7106 break; /* NOTREACHED */ 7107 } 7108 7109 /* 7110 * If we've got a disk, use its blocksize in the block 7111 * descriptor. Otherwise, just set it to 0. 7112 */ 7113 if (dbd == 0) { 7114 if (control_dev != 0) 7115 scsi_ulto3b(lun->be_lun->blocksize, 7116 block_desc->block_len); 7117 else 7118 scsi_ulto3b(0, block_desc->block_len); 7119 } 7120 7121 switch (page_code) { 7122 case SMS_ALL_PAGES_PAGE: { 7123 int i, data_used; 7124 7125 data_used = header_len; 7126 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7127 struct ctl_page_index *page_index; 7128 7129 page_index = &lun->mode_pages.index[i]; 7130 7131 if ((control_dev != 0) 7132 && (page_index->page_flags & 7133 CTL_PAGE_FLAG_DISK_ONLY)) 7134 continue; 7135 7136 /* 7137 * We don't use this subpage if the user didn't 7138 * request all subpages. We already checked (above) 7139 * to make sure the user only specified a subpage 7140 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7141 */ 7142 if ((page_index->subpage != 0) 7143 && (subpage == SMS_SUBPAGE_PAGE_0)) 7144 continue; 7145 7146 /* 7147 * Call the handler, if it exists, to update the 7148 * page to the latest values. 7149 */ 7150 if (page_index->sense_handler != NULL) 7151 page_index->sense_handler(ctsio, page_index,pc); 7152 7153 memcpy(ctsio->kern_data_ptr + data_used, 7154 page_index->page_data + 7155 (page_index->page_len * pc), 7156 page_index->page_len); 7157 data_used += page_index->page_len; 7158 } 7159 break; 7160 } 7161 default: { 7162 int i, data_used; 7163 7164 data_used = header_len; 7165 7166 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7167 struct ctl_page_index *page_index; 7168 7169 page_index = &lun->mode_pages.index[i]; 7170 7171 /* Look for the right page code */ 7172 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7173 continue; 7174 7175 /* Look for the right subpage or the subpage wildcard*/ 7176 if ((page_index->subpage != subpage) 7177 && (subpage != SMS_SUBPAGE_ALL)) 7178 continue; 7179 7180 /* Make sure the page is supported for this dev type */ 7181 if ((control_dev != 0) 7182 && (page_index->page_flags & 7183 CTL_PAGE_FLAG_DISK_ONLY)) 7184 continue; 7185 7186 /* 7187 * Call the handler, if it exists, to update the 7188 * page to the latest values. 7189 */ 7190 if (page_index->sense_handler != NULL) 7191 page_index->sense_handler(ctsio, page_index,pc); 7192 7193 memcpy(ctsio->kern_data_ptr + data_used, 7194 page_index->page_data + 7195 (page_index->page_len * pc), 7196 page_index->page_len); 7197 data_used += page_index->page_len; 7198 } 7199 break; 7200 } 7201 } 7202 7203 ctsio->scsi_status = SCSI_STATUS_OK; 7204 7205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7206 ctsio->be_move_done = ctl_config_move_done; 7207 ctl_datamove((union ctl_io *)ctsio); 7208 7209 return (CTL_RETVAL_COMPLETE); 7210} 7211 7212int 7213ctl_read_capacity(struct ctl_scsiio *ctsio) 7214{ 7215 struct scsi_read_capacity *cdb; 7216 struct scsi_read_capacity_data *data; 7217 struct ctl_lun *lun; 7218 uint32_t lba; 7219 7220 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7221 7222 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7223 7224 lba = scsi_4btoul(cdb->addr); 7225 if (((cdb->pmi & SRC_PMI) == 0) 7226 && (lba != 0)) { 7227 ctl_set_invalid_field(/*ctsio*/ ctsio, 7228 /*sks_valid*/ 1, 7229 /*command*/ 1, 7230 /*field*/ 2, 7231 /*bit_valid*/ 0, 7232 /*bit*/ 0); 7233 ctl_done((union ctl_io *)ctsio); 7234 return (CTL_RETVAL_COMPLETE); 7235 } 7236 7237 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7238 7239 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7240 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7241 ctsio->residual = 0; 7242 ctsio->kern_data_len = sizeof(*data); 7243 ctsio->kern_total_len = sizeof(*data); 7244 ctsio->kern_data_resid = 0; 7245 ctsio->kern_rel_offset = 0; 7246 ctsio->kern_sg_entries = 0; 7247 7248 /* 7249 * If the maximum LBA is greater than 0xfffffffe, the user must 7250 * issue a SERVICE ACTION IN (16) command, with the read capacity 7251 * serivce action set. 7252 */ 7253 if (lun->be_lun->maxlba > 0xfffffffe) 7254 scsi_ulto4b(0xffffffff, data->addr); 7255 else 7256 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7257 7258 /* 7259 * XXX KDM this may not be 512 bytes... 7260 */ 7261 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7262 7263 ctsio->scsi_status = SCSI_STATUS_OK; 7264 7265 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7266 ctsio->be_move_done = ctl_config_move_done; 7267 ctl_datamove((union ctl_io *)ctsio); 7268 7269 return (CTL_RETVAL_COMPLETE); 7270} 7271 7272int 7273ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7274{ 7275 struct scsi_read_capacity_16 *cdb; 7276 struct scsi_read_capacity_data_long *data; 7277 struct ctl_lun *lun; 7278 uint64_t lba; 7279 uint32_t alloc_len; 7280 7281 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7282 7283 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7284 7285 alloc_len = scsi_4btoul(cdb->alloc_len); 7286 lba = scsi_8btou64(cdb->addr); 7287 7288 if ((cdb->reladr & SRC16_PMI) 7289 && (lba != 0)) { 7290 ctl_set_invalid_field(/*ctsio*/ ctsio, 7291 /*sks_valid*/ 1, 7292 /*command*/ 1, 7293 /*field*/ 2, 7294 /*bit_valid*/ 0, 7295 /*bit*/ 0); 7296 ctl_done((union ctl_io *)ctsio); 7297 return (CTL_RETVAL_COMPLETE); 7298 } 7299 7300 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7301 7302 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7303 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7304 7305 if (sizeof(*data) < alloc_len) { 7306 ctsio->residual = alloc_len - sizeof(*data); 7307 ctsio->kern_data_len = sizeof(*data); 7308 ctsio->kern_total_len = sizeof(*data); 7309 } else { 7310 ctsio->residual = 0; 7311 ctsio->kern_data_len = alloc_len; 7312 ctsio->kern_total_len = alloc_len; 7313 } 7314 ctsio->kern_data_resid = 0; 7315 ctsio->kern_rel_offset = 0; 7316 ctsio->kern_sg_entries = 0; 7317 7318 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7319 /* XXX KDM this may not be 512 bytes... */ 7320 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7321 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7322 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7323 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7324 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7325 7326 ctsio->scsi_status = SCSI_STATUS_OK; 7327 7328 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7329 ctsio->be_move_done = ctl_config_move_done; 7330 ctl_datamove((union ctl_io *)ctsio); 7331 7332 return (CTL_RETVAL_COMPLETE); 7333} 7334 7335int 7336ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7337{ 7338 struct scsi_maintenance_in *cdb; 7339 int retval; 7340 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7341 int num_target_port_groups, num_target_ports, single; 7342 struct ctl_lun *lun; 7343 struct ctl_softc *softc; 7344 struct ctl_port *port; 7345 struct scsi_target_group_data *rtg_ptr; 7346 struct scsi_target_group_data_extended *rtg_ext_ptr; 7347 struct scsi_target_port_group_descriptor *tpg_desc; 7348 7349 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7350 7351 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7352 softc = control_softc; 7353 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7354 7355 retval = CTL_RETVAL_COMPLETE; 7356 7357 switch (cdb->byte2 & STG_PDF_MASK) { 7358 case STG_PDF_LENGTH: 7359 ext = 0; 7360 break; 7361 case STG_PDF_EXTENDED: 7362 ext = 1; 7363 break; 7364 default: 7365 ctl_set_invalid_field(/*ctsio*/ ctsio, 7366 /*sks_valid*/ 1, 7367 /*command*/ 1, 7368 /*field*/ 2, 7369 /*bit_valid*/ 1, 7370 /*bit*/ 5); 7371 ctl_done((union ctl_io *)ctsio); 7372 return(retval); 7373 } 7374 7375 single = ctl_is_single; 7376 if (single) 7377 num_target_port_groups = 1; 7378 else 7379 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7380 num_target_ports = 0; 7381 mtx_lock(&softc->ctl_lock); 7382 STAILQ_FOREACH(port, &softc->port_list, links) { 7383 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7384 continue; 7385 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7386 continue; 7387 num_target_ports++; 7388 } 7389 mtx_unlock(&softc->ctl_lock); 7390 7391 if (ext) 7392 total_len = sizeof(struct scsi_target_group_data_extended); 7393 else 7394 total_len = sizeof(struct scsi_target_group_data); 7395 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7396 num_target_port_groups + 7397 sizeof(struct scsi_target_port_descriptor) * 7398 num_target_ports * num_target_port_groups; 7399 7400 alloc_len = scsi_4btoul(cdb->length); 7401 7402 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7403 7404 ctsio->kern_sg_entries = 0; 7405 7406 if (total_len < alloc_len) { 7407 ctsio->residual = alloc_len - total_len; 7408 ctsio->kern_data_len = total_len; 7409 ctsio->kern_total_len = total_len; 7410 } else { 7411 ctsio->residual = 0; 7412 ctsio->kern_data_len = alloc_len; 7413 ctsio->kern_total_len = alloc_len; 7414 } 7415 ctsio->kern_data_resid = 0; 7416 ctsio->kern_rel_offset = 0; 7417 7418 if (ext) { 7419 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7420 ctsio->kern_data_ptr; 7421 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7422 rtg_ext_ptr->format_type = 0x10; 7423 rtg_ext_ptr->implicit_transition_time = 0; 7424 tpg_desc = &rtg_ext_ptr->groups[0]; 7425 } else { 7426 rtg_ptr = (struct scsi_target_group_data *) 7427 ctsio->kern_data_ptr; 7428 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7429 tpg_desc = &rtg_ptr->groups[0]; 7430 } 7431 7432 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7433 mtx_lock(&softc->ctl_lock); 7434 for (g = 0; g < num_target_port_groups; g++) { 7435 if (g == pg) 7436 tpg_desc->pref_state = TPG_PRIMARY | 7437 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7438 else 7439 tpg_desc->pref_state = 7440 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7441 tpg_desc->support = TPG_AO_SUP; 7442 if (!single) 7443 tpg_desc->support |= TPG_AN_SUP; 7444 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7445 tpg_desc->status = TPG_IMPLICIT; 7446 pc = 0; 7447 STAILQ_FOREACH(port, &softc->port_list, links) { 7448 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7449 continue; 7450 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7451 CTL_MAX_LUNS) 7452 continue; 7453 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7454 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7455 relative_target_port_identifier); 7456 pc++; 7457 } 7458 tpg_desc->target_port_count = pc; 7459 tpg_desc = (struct scsi_target_port_group_descriptor *) 7460 &tpg_desc->descriptors[pc]; 7461 } 7462 mtx_unlock(&softc->ctl_lock); 7463 7464 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7465 ctsio->be_move_done = ctl_config_move_done; 7466 7467 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7468 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7469 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7470 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7471 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7472 7473 ctl_datamove((union ctl_io *)ctsio); 7474 return(retval); 7475} 7476 7477int 7478ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7479{ 7480 struct ctl_lun *lun; 7481 struct scsi_report_supported_opcodes *cdb; 7482 const struct ctl_cmd_entry *entry, *sentry; 7483 struct scsi_report_supported_opcodes_all *all; 7484 struct scsi_report_supported_opcodes_descr *descr; 7485 struct scsi_report_supported_opcodes_one *one; 7486 int retval; 7487 int alloc_len, total_len; 7488 int opcode, service_action, i, j, num; 7489 7490 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7491 7492 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7493 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7494 7495 retval = CTL_RETVAL_COMPLETE; 7496 7497 opcode = cdb->requested_opcode; 7498 service_action = scsi_2btoul(cdb->requested_service_action); 7499 switch (cdb->options & RSO_OPTIONS_MASK) { 7500 case RSO_OPTIONS_ALL: 7501 num = 0; 7502 for (i = 0; i < 256; i++) { 7503 entry = &ctl_cmd_table[i]; 7504 if (entry->flags & CTL_CMD_FLAG_SA5) { 7505 for (j = 0; j < 32; j++) { 7506 sentry = &((const struct ctl_cmd_entry *) 7507 entry->execute)[j]; 7508 if (ctl_cmd_applicable( 7509 lun->be_lun->lun_type, sentry)) 7510 num++; 7511 } 7512 } else { 7513 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7514 entry)) 7515 num++; 7516 } 7517 } 7518 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7519 num * sizeof(struct scsi_report_supported_opcodes_descr); 7520 break; 7521 case RSO_OPTIONS_OC: 7522 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7523 ctl_set_invalid_field(/*ctsio*/ ctsio, 7524 /*sks_valid*/ 1, 7525 /*command*/ 1, 7526 /*field*/ 2, 7527 /*bit_valid*/ 1, 7528 /*bit*/ 2); 7529 ctl_done((union ctl_io *)ctsio); 7530 return (CTL_RETVAL_COMPLETE); 7531 } 7532 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7533 break; 7534 case RSO_OPTIONS_OC_SA: 7535 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7536 service_action >= 32) { 7537 ctl_set_invalid_field(/*ctsio*/ ctsio, 7538 /*sks_valid*/ 1, 7539 /*command*/ 1, 7540 /*field*/ 2, 7541 /*bit_valid*/ 1, 7542 /*bit*/ 2); 7543 ctl_done((union ctl_io *)ctsio); 7544 return (CTL_RETVAL_COMPLETE); 7545 } 7546 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7547 break; 7548 default: 7549 ctl_set_invalid_field(/*ctsio*/ ctsio, 7550 /*sks_valid*/ 1, 7551 /*command*/ 1, 7552 /*field*/ 2, 7553 /*bit_valid*/ 1, 7554 /*bit*/ 2); 7555 ctl_done((union ctl_io *)ctsio); 7556 return (CTL_RETVAL_COMPLETE); 7557 } 7558 7559 alloc_len = scsi_4btoul(cdb->length); 7560 7561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7562 7563 ctsio->kern_sg_entries = 0; 7564 7565 if (total_len < alloc_len) { 7566 ctsio->residual = alloc_len - total_len; 7567 ctsio->kern_data_len = total_len; 7568 ctsio->kern_total_len = total_len; 7569 } else { 7570 ctsio->residual = 0; 7571 ctsio->kern_data_len = alloc_len; 7572 ctsio->kern_total_len = alloc_len; 7573 } 7574 ctsio->kern_data_resid = 0; 7575 ctsio->kern_rel_offset = 0; 7576 7577 switch (cdb->options & RSO_OPTIONS_MASK) { 7578 case RSO_OPTIONS_ALL: 7579 all = (struct scsi_report_supported_opcodes_all *) 7580 ctsio->kern_data_ptr; 7581 num = 0; 7582 for (i = 0; i < 256; i++) { 7583 entry = &ctl_cmd_table[i]; 7584 if (entry->flags & CTL_CMD_FLAG_SA5) { 7585 for (j = 0; j < 32; j++) { 7586 sentry = &((const struct ctl_cmd_entry *) 7587 entry->execute)[j]; 7588 if (!ctl_cmd_applicable( 7589 lun->be_lun->lun_type, sentry)) 7590 continue; 7591 descr = &all->descr[num++]; 7592 descr->opcode = i; 7593 scsi_ulto2b(j, descr->service_action); 7594 descr->flags = RSO_SERVACTV; 7595 scsi_ulto2b(sentry->length, 7596 descr->cdb_length); 7597 } 7598 } else { 7599 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7600 entry)) 7601 continue; 7602 descr = &all->descr[num++]; 7603 descr->opcode = i; 7604 scsi_ulto2b(0, descr->service_action); 7605 descr->flags = 0; 7606 scsi_ulto2b(entry->length, descr->cdb_length); 7607 } 7608 } 7609 scsi_ulto4b( 7610 num * sizeof(struct scsi_report_supported_opcodes_descr), 7611 all->length); 7612 break; 7613 case RSO_OPTIONS_OC: 7614 one = (struct scsi_report_supported_opcodes_one *) 7615 ctsio->kern_data_ptr; 7616 entry = &ctl_cmd_table[opcode]; 7617 goto fill_one; 7618 case RSO_OPTIONS_OC_SA: 7619 one = (struct scsi_report_supported_opcodes_one *) 7620 ctsio->kern_data_ptr; 7621 entry = &ctl_cmd_table[opcode]; 7622 entry = &((const struct ctl_cmd_entry *) 7623 entry->execute)[service_action]; 7624fill_one: 7625 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7626 one->support = 3; 7627 scsi_ulto2b(entry->length, one->cdb_length); 7628 one->cdb_usage[0] = opcode; 7629 memcpy(&one->cdb_usage[1], entry->usage, 7630 entry->length - 1); 7631 } else 7632 one->support = 1; 7633 break; 7634 } 7635 7636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7637 ctsio->be_move_done = ctl_config_move_done; 7638 7639 ctl_datamove((union ctl_io *)ctsio); 7640 return(retval); 7641} 7642 7643int 7644ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7645{ 7646 struct ctl_lun *lun; 7647 struct scsi_report_supported_tmf *cdb; 7648 struct scsi_report_supported_tmf_data *data; 7649 int retval; 7650 int alloc_len, total_len; 7651 7652 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7653 7654 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7656 7657 retval = CTL_RETVAL_COMPLETE; 7658 7659 total_len = sizeof(struct scsi_report_supported_tmf_data); 7660 alloc_len = scsi_4btoul(cdb->length); 7661 7662 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7663 7664 ctsio->kern_sg_entries = 0; 7665 7666 if (total_len < alloc_len) { 7667 ctsio->residual = alloc_len - total_len; 7668 ctsio->kern_data_len = total_len; 7669 ctsio->kern_total_len = total_len; 7670 } else { 7671 ctsio->residual = 0; 7672 ctsio->kern_data_len = alloc_len; 7673 ctsio->kern_total_len = alloc_len; 7674 } 7675 ctsio->kern_data_resid = 0; 7676 ctsio->kern_rel_offset = 0; 7677 7678 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7679 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7680 data->byte2 |= RST_ITNRS; 7681 7682 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7683 ctsio->be_move_done = ctl_config_move_done; 7684 7685 ctl_datamove((union ctl_io *)ctsio); 7686 return (retval); 7687} 7688 7689int 7690ctl_report_timestamp(struct ctl_scsiio *ctsio) 7691{ 7692 struct ctl_lun *lun; 7693 struct scsi_report_timestamp *cdb; 7694 struct scsi_report_timestamp_data *data; 7695 struct timeval tv; 7696 int64_t timestamp; 7697 int retval; 7698 int alloc_len, total_len; 7699 7700 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7701 7702 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7703 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7704 7705 retval = CTL_RETVAL_COMPLETE; 7706 7707 total_len = sizeof(struct scsi_report_timestamp_data); 7708 alloc_len = scsi_4btoul(cdb->length); 7709 7710 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7711 7712 ctsio->kern_sg_entries = 0; 7713 7714 if (total_len < alloc_len) { 7715 ctsio->residual = alloc_len - total_len; 7716 ctsio->kern_data_len = total_len; 7717 ctsio->kern_total_len = total_len; 7718 } else { 7719 ctsio->residual = 0; 7720 ctsio->kern_data_len = alloc_len; 7721 ctsio->kern_total_len = alloc_len; 7722 } 7723 ctsio->kern_data_resid = 0; 7724 ctsio->kern_rel_offset = 0; 7725 7726 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7727 scsi_ulto2b(sizeof(*data) - 2, data->length); 7728 data->origin = RTS_ORIG_OUTSIDE; 7729 getmicrotime(&tv); 7730 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7731 scsi_ulto4b(timestamp >> 16, data->timestamp); 7732 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7733 7734 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7735 ctsio->be_move_done = ctl_config_move_done; 7736 7737 ctl_datamove((union ctl_io *)ctsio); 7738 return (retval); 7739} 7740 7741int 7742ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7743{ 7744 struct scsi_per_res_in *cdb; 7745 int alloc_len, total_len = 0; 7746 /* struct scsi_per_res_in_rsrv in_data; */ 7747 struct ctl_lun *lun; 7748 struct ctl_softc *softc; 7749 7750 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7751 7752 softc = control_softc; 7753 7754 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7755 7756 alloc_len = scsi_2btoul(cdb->length); 7757 7758 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7759 7760retry: 7761 mtx_lock(&lun->lun_lock); 7762 switch (cdb->action) { 7763 case SPRI_RK: /* read keys */ 7764 total_len = sizeof(struct scsi_per_res_in_keys) + 7765 lun->pr_key_count * 7766 sizeof(struct scsi_per_res_key); 7767 break; 7768 case SPRI_RR: /* read reservation */ 7769 if (lun->flags & CTL_LUN_PR_RESERVED) 7770 total_len = sizeof(struct scsi_per_res_in_rsrv); 7771 else 7772 total_len = sizeof(struct scsi_per_res_in_header); 7773 break; 7774 case SPRI_RC: /* report capabilities */ 7775 total_len = sizeof(struct scsi_per_res_cap); 7776 break; 7777 case SPRI_RS: /* read full status */ 7778 total_len = sizeof(struct scsi_per_res_in_header) + 7779 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7780 lun->pr_key_count; 7781 break; 7782 default: 7783 panic("Invalid PR type %x", cdb->action); 7784 } 7785 mtx_unlock(&lun->lun_lock); 7786 7787 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7788 7789 if (total_len < alloc_len) { 7790 ctsio->residual = alloc_len - total_len; 7791 ctsio->kern_data_len = total_len; 7792 ctsio->kern_total_len = total_len; 7793 } else { 7794 ctsio->residual = 0; 7795 ctsio->kern_data_len = alloc_len; 7796 ctsio->kern_total_len = alloc_len; 7797 } 7798 7799 ctsio->kern_data_resid = 0; 7800 ctsio->kern_rel_offset = 0; 7801 ctsio->kern_sg_entries = 0; 7802 7803 mtx_lock(&lun->lun_lock); 7804 switch (cdb->action) { 7805 case SPRI_RK: { // read keys 7806 struct scsi_per_res_in_keys *res_keys; 7807 int i, key_count; 7808 7809 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7810 7811 /* 7812 * We had to drop the lock to allocate our buffer, which 7813 * leaves time for someone to come in with another 7814 * persistent reservation. (That is unlikely, though, 7815 * since this should be the only persistent reservation 7816 * command active right now.) 7817 */ 7818 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7819 (lun->pr_key_count * 7820 sizeof(struct scsi_per_res_key)))){ 7821 mtx_unlock(&lun->lun_lock); 7822 free(ctsio->kern_data_ptr, M_CTL); 7823 printf("%s: reservation length changed, retrying\n", 7824 __func__); 7825 goto retry; 7826 } 7827 7828 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7829 7830 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7831 lun->pr_key_count, res_keys->header.length); 7832 7833 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7834 if (!lun->per_res[i].registered) 7835 continue; 7836 7837 /* 7838 * We used lun->pr_key_count to calculate the 7839 * size to allocate. If it turns out the number of 7840 * initiators with the registered flag set is 7841 * larger than that (i.e. they haven't been kept in 7842 * sync), we've got a problem. 7843 */ 7844 if (key_count >= lun->pr_key_count) { 7845#ifdef NEEDTOPORT 7846 csevent_log(CSC_CTL | CSC_SHELF_SW | 7847 CTL_PR_ERROR, 7848 csevent_LogType_Fault, 7849 csevent_AlertLevel_Yellow, 7850 csevent_FRU_ShelfController, 7851 csevent_FRU_Firmware, 7852 csevent_FRU_Unknown, 7853 "registered keys %d >= key " 7854 "count %d", key_count, 7855 lun->pr_key_count); 7856#endif 7857 key_count++; 7858 continue; 7859 } 7860 memcpy(res_keys->keys[key_count].key, 7861 lun->per_res[i].res_key.key, 7862 ctl_min(sizeof(res_keys->keys[key_count].key), 7863 sizeof(lun->per_res[i].res_key))); 7864 key_count++; 7865 } 7866 break; 7867 } 7868 case SPRI_RR: { // read reservation 7869 struct scsi_per_res_in_rsrv *res; 7870 int tmp_len, header_only; 7871 7872 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7873 7874 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7875 7876 if (lun->flags & CTL_LUN_PR_RESERVED) 7877 { 7878 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7879 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7880 res->header.length); 7881 header_only = 0; 7882 } else { 7883 tmp_len = sizeof(struct scsi_per_res_in_header); 7884 scsi_ulto4b(0, res->header.length); 7885 header_only = 1; 7886 } 7887 7888 /* 7889 * We had to drop the lock to allocate our buffer, which 7890 * leaves time for someone to come in with another 7891 * persistent reservation. (That is unlikely, though, 7892 * since this should be the only persistent reservation 7893 * command active right now.) 7894 */ 7895 if (tmp_len != total_len) { 7896 mtx_unlock(&lun->lun_lock); 7897 free(ctsio->kern_data_ptr, M_CTL); 7898 printf("%s: reservation status changed, retrying\n", 7899 __func__); 7900 goto retry; 7901 } 7902 7903 /* 7904 * No reservation held, so we're done. 7905 */ 7906 if (header_only != 0) 7907 break; 7908 7909 /* 7910 * If the registration is an All Registrants type, the key 7911 * is 0, since it doesn't really matter. 7912 */ 7913 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7914 memcpy(res->data.reservation, 7915 &lun->per_res[lun->pr_res_idx].res_key, 7916 sizeof(struct scsi_per_res_key)); 7917 } 7918 res->data.scopetype = lun->res_type; 7919 break; 7920 } 7921 case SPRI_RC: //report capabilities 7922 { 7923 struct scsi_per_res_cap *res_cap; 7924 uint16_t type_mask; 7925 7926 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7927 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7928 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7929 type_mask = SPRI_TM_WR_EX_AR | 7930 SPRI_TM_EX_AC_RO | 7931 SPRI_TM_WR_EX_RO | 7932 SPRI_TM_EX_AC | 7933 SPRI_TM_WR_EX | 7934 SPRI_TM_EX_AC_AR; 7935 scsi_ulto2b(type_mask, res_cap->type_mask); 7936 break; 7937 } 7938 case SPRI_RS: { // read full status 7939 struct scsi_per_res_in_full *res_status; 7940 struct scsi_per_res_in_full_desc *res_desc; 7941 struct ctl_port *port; 7942 int i, len; 7943 7944 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7945 7946 /* 7947 * We had to drop the lock to allocate our buffer, which 7948 * leaves time for someone to come in with another 7949 * persistent reservation. (That is unlikely, though, 7950 * since this should be the only persistent reservation 7951 * command active right now.) 7952 */ 7953 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7954 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7955 lun->pr_key_count)){ 7956 mtx_unlock(&lun->lun_lock); 7957 free(ctsio->kern_data_ptr, M_CTL); 7958 printf("%s: reservation length changed, retrying\n", 7959 __func__); 7960 goto retry; 7961 } 7962 7963 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7964 7965 res_desc = &res_status->desc[0]; 7966 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7967 if (!lun->per_res[i].registered) 7968 continue; 7969 7970 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7971 sizeof(res_desc->res_key)); 7972 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7973 (lun->pr_res_idx == i || 7974 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7975 res_desc->flags = SPRI_FULL_R_HOLDER; 7976 res_desc->scopetype = lun->res_type; 7977 } 7978 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7979 res_desc->rel_trgt_port_id); 7980 len = 0; 7981 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7982 if (port != NULL) 7983 len = ctl_create_iid(port, 7984 i % CTL_MAX_INIT_PER_PORT, 7985 res_desc->transport_id); 7986 scsi_ulto4b(len, res_desc->additional_length); 7987 res_desc = (struct scsi_per_res_in_full_desc *) 7988 &res_desc->transport_id[len]; 7989 } 7990 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7991 res_status->header.length); 7992 break; 7993 } 7994 default: 7995 /* 7996 * This is a bug, because we just checked for this above, 7997 * and should have returned an error. 7998 */ 7999 panic("Invalid PR type %x", cdb->action); 8000 break; /* NOTREACHED */ 8001 } 8002 mtx_unlock(&lun->lun_lock); 8003 8004 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8005 ctsio->be_move_done = ctl_config_move_done; 8006 8007 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 8008 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 8009 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 8010 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 8011 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 8012 8013 ctl_datamove((union ctl_io *)ctsio); 8014 8015 return (CTL_RETVAL_COMPLETE); 8016} 8017 8018/* 8019 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8020 * it should return. 8021 */ 8022static int 8023ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8024 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8025 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8026 struct scsi_per_res_out_parms* param) 8027{ 8028 union ctl_ha_msg persis_io; 8029 int retval, i; 8030 int isc_retval; 8031 8032 retval = 0; 8033 8034 mtx_lock(&lun->lun_lock); 8035 if (sa_res_key == 0) { 8036 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8037 /* validate scope and type */ 8038 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8039 SPR_LU_SCOPE) { 8040 mtx_unlock(&lun->lun_lock); 8041 ctl_set_invalid_field(/*ctsio*/ ctsio, 8042 /*sks_valid*/ 1, 8043 /*command*/ 1, 8044 /*field*/ 2, 8045 /*bit_valid*/ 1, 8046 /*bit*/ 4); 8047 ctl_done((union ctl_io *)ctsio); 8048 return (1); 8049 } 8050 8051 if (type>8 || type==2 || type==4 || type==0) { 8052 mtx_unlock(&lun->lun_lock); 8053 ctl_set_invalid_field(/*ctsio*/ ctsio, 8054 /*sks_valid*/ 1, 8055 /*command*/ 1, 8056 /*field*/ 2, 8057 /*bit_valid*/ 1, 8058 /*bit*/ 0); 8059 ctl_done((union ctl_io *)ctsio); 8060 return (1); 8061 } 8062 8063 /* temporarily unregister this nexus */ 8064 lun->per_res[residx].registered = 0; 8065 8066 /* 8067 * Unregister everybody else and build UA for 8068 * them 8069 */ 8070 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8071 if (lun->per_res[i].registered == 0) 8072 continue; 8073 8074 if (!persis_offset 8075 && i <CTL_MAX_INITIATORS) 8076 lun->pending_ua[i] |= 8077 CTL_UA_REG_PREEMPT; 8078 else if (persis_offset 8079 && i >= persis_offset) 8080 lun->pending_ua[i-persis_offset] |= 8081 CTL_UA_REG_PREEMPT; 8082 lun->per_res[i].registered = 0; 8083 memset(&lun->per_res[i].res_key, 0, 8084 sizeof(struct scsi_per_res_key)); 8085 } 8086 lun->per_res[residx].registered = 1; 8087 lun->pr_key_count = 1; 8088 lun->res_type = type; 8089 if (lun->res_type != SPR_TYPE_WR_EX_AR 8090 && lun->res_type != SPR_TYPE_EX_AC_AR) 8091 lun->pr_res_idx = residx; 8092 8093 /* send msg to other side */ 8094 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8095 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8096 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8097 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8098 persis_io.pr.pr_info.res_type = type; 8099 memcpy(persis_io.pr.pr_info.sa_res_key, 8100 param->serv_act_res_key, 8101 sizeof(param->serv_act_res_key)); 8102 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8103 &persis_io, sizeof(persis_io), 0)) > 8104 CTL_HA_STATUS_SUCCESS) { 8105 printf("CTL:Persis Out error returned " 8106 "from ctl_ha_msg_send %d\n", 8107 isc_retval); 8108 } 8109 } else { 8110 /* not all registrants */ 8111 mtx_unlock(&lun->lun_lock); 8112 free(ctsio->kern_data_ptr, M_CTL); 8113 ctl_set_invalid_field(ctsio, 8114 /*sks_valid*/ 1, 8115 /*command*/ 0, 8116 /*field*/ 8, 8117 /*bit_valid*/ 0, 8118 /*bit*/ 0); 8119 ctl_done((union ctl_io *)ctsio); 8120 return (1); 8121 } 8122 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8123 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8124 int found = 0; 8125 8126 if (res_key == sa_res_key) { 8127 /* special case */ 8128 /* 8129 * The spec implies this is not good but doesn't 8130 * say what to do. There are two choices either 8131 * generate a res conflict or check condition 8132 * with illegal field in parameter data. Since 8133 * that is what is done when the sa_res_key is 8134 * zero I'll take that approach since this has 8135 * to do with the sa_res_key. 8136 */ 8137 mtx_unlock(&lun->lun_lock); 8138 free(ctsio->kern_data_ptr, M_CTL); 8139 ctl_set_invalid_field(ctsio, 8140 /*sks_valid*/ 1, 8141 /*command*/ 0, 8142 /*field*/ 8, 8143 /*bit_valid*/ 0, 8144 /*bit*/ 0); 8145 ctl_done((union ctl_io *)ctsio); 8146 return (1); 8147 } 8148 8149 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8150 if (lun->per_res[i].registered 8151 && memcmp(param->serv_act_res_key, 8152 lun->per_res[i].res_key.key, 8153 sizeof(struct scsi_per_res_key)) != 0) 8154 continue; 8155 8156 found = 1; 8157 lun->per_res[i].registered = 0; 8158 memset(&lun->per_res[i].res_key, 0, 8159 sizeof(struct scsi_per_res_key)); 8160 lun->pr_key_count--; 8161 8162 if (!persis_offset && i < CTL_MAX_INITIATORS) 8163 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8164 else if (persis_offset && i >= persis_offset) 8165 lun->pending_ua[i-persis_offset] |= 8166 CTL_UA_REG_PREEMPT; 8167 } 8168 if (!found) { 8169 mtx_unlock(&lun->lun_lock); 8170 free(ctsio->kern_data_ptr, M_CTL); 8171 ctl_set_reservation_conflict(ctsio); 8172 ctl_done((union ctl_io *)ctsio); 8173 return (CTL_RETVAL_COMPLETE); 8174 } 8175 /* send msg to other side */ 8176 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8177 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8178 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8179 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8180 persis_io.pr.pr_info.res_type = type; 8181 memcpy(persis_io.pr.pr_info.sa_res_key, 8182 param->serv_act_res_key, 8183 sizeof(param->serv_act_res_key)); 8184 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8185 &persis_io, sizeof(persis_io), 0)) > 8186 CTL_HA_STATUS_SUCCESS) { 8187 printf("CTL:Persis Out error returned from " 8188 "ctl_ha_msg_send %d\n", isc_retval); 8189 } 8190 } else { 8191 /* Reserved but not all registrants */ 8192 /* sa_res_key is res holder */ 8193 if (memcmp(param->serv_act_res_key, 8194 lun->per_res[lun->pr_res_idx].res_key.key, 8195 sizeof(struct scsi_per_res_key)) == 0) { 8196 /* validate scope and type */ 8197 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8198 SPR_LU_SCOPE) { 8199 mtx_unlock(&lun->lun_lock); 8200 ctl_set_invalid_field(/*ctsio*/ ctsio, 8201 /*sks_valid*/ 1, 8202 /*command*/ 1, 8203 /*field*/ 2, 8204 /*bit_valid*/ 1, 8205 /*bit*/ 4); 8206 ctl_done((union ctl_io *)ctsio); 8207 return (1); 8208 } 8209 8210 if (type>8 || type==2 || type==4 || type==0) { 8211 mtx_unlock(&lun->lun_lock); 8212 ctl_set_invalid_field(/*ctsio*/ ctsio, 8213 /*sks_valid*/ 1, 8214 /*command*/ 1, 8215 /*field*/ 2, 8216 /*bit_valid*/ 1, 8217 /*bit*/ 0); 8218 ctl_done((union ctl_io *)ctsio); 8219 return (1); 8220 } 8221 8222 /* 8223 * Do the following: 8224 * if sa_res_key != res_key remove all 8225 * registrants w/sa_res_key and generate UA 8226 * for these registrants(Registrations 8227 * Preempted) if it wasn't an exclusive 8228 * reservation generate UA(Reservations 8229 * Preempted) for all other registered nexuses 8230 * if the type has changed. Establish the new 8231 * reservation and holder. If res_key and 8232 * sa_res_key are the same do the above 8233 * except don't unregister the res holder. 8234 */ 8235 8236 /* 8237 * Temporarily unregister so it won't get 8238 * removed or UA generated 8239 */ 8240 lun->per_res[residx].registered = 0; 8241 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8242 if (lun->per_res[i].registered == 0) 8243 continue; 8244 8245 if (memcmp(param->serv_act_res_key, 8246 lun->per_res[i].res_key.key, 8247 sizeof(struct scsi_per_res_key)) == 0) { 8248 lun->per_res[i].registered = 0; 8249 memset(&lun->per_res[i].res_key, 8250 0, 8251 sizeof(struct scsi_per_res_key)); 8252 lun->pr_key_count--; 8253 8254 if (!persis_offset 8255 && i < CTL_MAX_INITIATORS) 8256 lun->pending_ua[i] |= 8257 CTL_UA_REG_PREEMPT; 8258 else if (persis_offset 8259 && i >= persis_offset) 8260 lun->pending_ua[i-persis_offset] |= 8261 CTL_UA_REG_PREEMPT; 8262 } else if (type != lun->res_type 8263 && (lun->res_type == SPR_TYPE_WR_EX_RO 8264 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8265 if (!persis_offset 8266 && i < CTL_MAX_INITIATORS) 8267 lun->pending_ua[i] |= 8268 CTL_UA_RES_RELEASE; 8269 else if (persis_offset 8270 && i >= persis_offset) 8271 lun->pending_ua[ 8272 i-persis_offset] |= 8273 CTL_UA_RES_RELEASE; 8274 } 8275 } 8276 lun->per_res[residx].registered = 1; 8277 lun->res_type = type; 8278 if (lun->res_type != SPR_TYPE_WR_EX_AR 8279 && lun->res_type != SPR_TYPE_EX_AC_AR) 8280 lun->pr_res_idx = residx; 8281 else 8282 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8283 8284 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8285 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8286 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8287 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8288 persis_io.pr.pr_info.res_type = type; 8289 memcpy(persis_io.pr.pr_info.sa_res_key, 8290 param->serv_act_res_key, 8291 sizeof(param->serv_act_res_key)); 8292 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8293 &persis_io, sizeof(persis_io), 0)) > 8294 CTL_HA_STATUS_SUCCESS) { 8295 printf("CTL:Persis Out error returned " 8296 "from ctl_ha_msg_send %d\n", 8297 isc_retval); 8298 } 8299 } else { 8300 /* 8301 * sa_res_key is not the res holder just 8302 * remove registrants 8303 */ 8304 int found=0; 8305 8306 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8307 if (memcmp(param->serv_act_res_key, 8308 lun->per_res[i].res_key.key, 8309 sizeof(struct scsi_per_res_key)) != 0) 8310 continue; 8311 8312 found = 1; 8313 lun->per_res[i].registered = 0; 8314 memset(&lun->per_res[i].res_key, 0, 8315 sizeof(struct scsi_per_res_key)); 8316 lun->pr_key_count--; 8317 8318 if (!persis_offset 8319 && i < CTL_MAX_INITIATORS) 8320 lun->pending_ua[i] |= 8321 CTL_UA_REG_PREEMPT; 8322 else if (persis_offset 8323 && i >= persis_offset) 8324 lun->pending_ua[i-persis_offset] |= 8325 CTL_UA_REG_PREEMPT; 8326 } 8327 8328 if (!found) { 8329 mtx_unlock(&lun->lun_lock); 8330 free(ctsio->kern_data_ptr, M_CTL); 8331 ctl_set_reservation_conflict(ctsio); 8332 ctl_done((union ctl_io *)ctsio); 8333 return (1); 8334 } 8335 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8336 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8337 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8338 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8339 persis_io.pr.pr_info.res_type = type; 8340 memcpy(persis_io.pr.pr_info.sa_res_key, 8341 param->serv_act_res_key, 8342 sizeof(param->serv_act_res_key)); 8343 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8344 &persis_io, sizeof(persis_io), 0)) > 8345 CTL_HA_STATUS_SUCCESS) { 8346 printf("CTL:Persis Out error returned " 8347 "from ctl_ha_msg_send %d\n", 8348 isc_retval); 8349 } 8350 } 8351 } 8352 8353 lun->PRGeneration++; 8354 mtx_unlock(&lun->lun_lock); 8355 8356 return (retval); 8357} 8358 8359static void 8360ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8361{ 8362 int i; 8363 8364 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8365 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8366 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8367 msg->pr.pr_info.sa_res_key, 8368 sizeof(struct scsi_per_res_key)) != 0) { 8369 uint64_t sa_res_key; 8370 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8371 8372 if (sa_res_key == 0) { 8373 /* temporarily unregister this nexus */ 8374 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8375 8376 /* 8377 * Unregister everybody else and build UA for 8378 * them 8379 */ 8380 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8381 if (lun->per_res[i].registered == 0) 8382 continue; 8383 8384 if (!persis_offset 8385 && i < CTL_MAX_INITIATORS) 8386 lun->pending_ua[i] |= 8387 CTL_UA_REG_PREEMPT; 8388 else if (persis_offset && i >= persis_offset) 8389 lun->pending_ua[i - persis_offset] |= 8390 CTL_UA_REG_PREEMPT; 8391 lun->per_res[i].registered = 0; 8392 memset(&lun->per_res[i].res_key, 0, 8393 sizeof(struct scsi_per_res_key)); 8394 } 8395 8396 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8397 lun->pr_key_count = 1; 8398 lun->res_type = msg->pr.pr_info.res_type; 8399 if (lun->res_type != SPR_TYPE_WR_EX_AR 8400 && lun->res_type != SPR_TYPE_EX_AC_AR) 8401 lun->pr_res_idx = msg->pr.pr_info.residx; 8402 } else { 8403 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8404 if (memcmp(msg->pr.pr_info.sa_res_key, 8405 lun->per_res[i].res_key.key, 8406 sizeof(struct scsi_per_res_key)) != 0) 8407 continue; 8408 8409 lun->per_res[i].registered = 0; 8410 memset(&lun->per_res[i].res_key, 0, 8411 sizeof(struct scsi_per_res_key)); 8412 lun->pr_key_count--; 8413 8414 if (!persis_offset 8415 && i < persis_offset) 8416 lun->pending_ua[i] |= 8417 CTL_UA_REG_PREEMPT; 8418 else if (persis_offset 8419 && i >= persis_offset) 8420 lun->pending_ua[i - persis_offset] |= 8421 CTL_UA_REG_PREEMPT; 8422 } 8423 } 8424 } else { 8425 /* 8426 * Temporarily unregister so it won't get removed 8427 * or UA generated 8428 */ 8429 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8430 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8431 if (lun->per_res[i].registered == 0) 8432 continue; 8433 8434 if (memcmp(msg->pr.pr_info.sa_res_key, 8435 lun->per_res[i].res_key.key, 8436 sizeof(struct scsi_per_res_key)) == 0) { 8437 lun->per_res[i].registered = 0; 8438 memset(&lun->per_res[i].res_key, 0, 8439 sizeof(struct scsi_per_res_key)); 8440 lun->pr_key_count--; 8441 if (!persis_offset 8442 && i < CTL_MAX_INITIATORS) 8443 lun->pending_ua[i] |= 8444 CTL_UA_REG_PREEMPT; 8445 else if (persis_offset 8446 && i >= persis_offset) 8447 lun->pending_ua[i - persis_offset] |= 8448 CTL_UA_REG_PREEMPT; 8449 } else if (msg->pr.pr_info.res_type != lun->res_type 8450 && (lun->res_type == SPR_TYPE_WR_EX_RO 8451 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8452 if (!persis_offset 8453 && i < persis_offset) 8454 lun->pending_ua[i] |= 8455 CTL_UA_RES_RELEASE; 8456 else if (persis_offset 8457 && i >= persis_offset) 8458 lun->pending_ua[i - persis_offset] |= 8459 CTL_UA_RES_RELEASE; 8460 } 8461 } 8462 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8463 lun->res_type = msg->pr.pr_info.res_type; 8464 if (lun->res_type != SPR_TYPE_WR_EX_AR 8465 && lun->res_type != SPR_TYPE_EX_AC_AR) 8466 lun->pr_res_idx = msg->pr.pr_info.residx; 8467 else 8468 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8469 } 8470 lun->PRGeneration++; 8471 8472} 8473 8474 8475int 8476ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8477{ 8478 int retval; 8479 int isc_retval; 8480 u_int32_t param_len; 8481 struct scsi_per_res_out *cdb; 8482 struct ctl_lun *lun; 8483 struct scsi_per_res_out_parms* param; 8484 struct ctl_softc *softc; 8485 uint32_t residx; 8486 uint64_t res_key, sa_res_key; 8487 uint8_t type; 8488 union ctl_ha_msg persis_io; 8489 int i; 8490 8491 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8492 8493 retval = CTL_RETVAL_COMPLETE; 8494 8495 softc = control_softc; 8496 8497 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8498 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8499 8500 /* 8501 * We only support whole-LUN scope. The scope & type are ignored for 8502 * register, register and ignore existing key and clear. 8503 * We sometimes ignore scope and type on preempts too!! 8504 * Verify reservation type here as well. 8505 */ 8506 type = cdb->scope_type & SPR_TYPE_MASK; 8507 if ((cdb->action == SPRO_RESERVE) 8508 || (cdb->action == SPRO_RELEASE)) { 8509 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8510 ctl_set_invalid_field(/*ctsio*/ ctsio, 8511 /*sks_valid*/ 1, 8512 /*command*/ 1, 8513 /*field*/ 2, 8514 /*bit_valid*/ 1, 8515 /*bit*/ 4); 8516 ctl_done((union ctl_io *)ctsio); 8517 return (CTL_RETVAL_COMPLETE); 8518 } 8519 8520 if (type>8 || type==2 || type==4 || type==0) { 8521 ctl_set_invalid_field(/*ctsio*/ ctsio, 8522 /*sks_valid*/ 1, 8523 /*command*/ 1, 8524 /*field*/ 2, 8525 /*bit_valid*/ 1, 8526 /*bit*/ 0); 8527 ctl_done((union ctl_io *)ctsio); 8528 return (CTL_RETVAL_COMPLETE); 8529 } 8530 } 8531 8532 param_len = scsi_4btoul(cdb->length); 8533 8534 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8535 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8536 ctsio->kern_data_len = param_len; 8537 ctsio->kern_total_len = param_len; 8538 ctsio->kern_data_resid = 0; 8539 ctsio->kern_rel_offset = 0; 8540 ctsio->kern_sg_entries = 0; 8541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8542 ctsio->be_move_done = ctl_config_move_done; 8543 ctl_datamove((union ctl_io *)ctsio); 8544 8545 return (CTL_RETVAL_COMPLETE); 8546 } 8547 8548 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8549 8550 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8551 res_key = scsi_8btou64(param->res_key.key); 8552 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8553 8554 /* 8555 * Validate the reservation key here except for SPRO_REG_IGNO 8556 * This must be done for all other service actions 8557 */ 8558 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8559 mtx_lock(&lun->lun_lock); 8560 if (lun->per_res[residx].registered) { 8561 if (memcmp(param->res_key.key, 8562 lun->per_res[residx].res_key.key, 8563 ctl_min(sizeof(param->res_key), 8564 sizeof(lun->per_res[residx].res_key))) != 0) { 8565 /* 8566 * The current key passed in doesn't match 8567 * the one the initiator previously 8568 * registered. 8569 */ 8570 mtx_unlock(&lun->lun_lock); 8571 free(ctsio->kern_data_ptr, M_CTL); 8572 ctl_set_reservation_conflict(ctsio); 8573 ctl_done((union ctl_io *)ctsio); 8574 return (CTL_RETVAL_COMPLETE); 8575 } 8576 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8577 /* 8578 * We are not registered 8579 */ 8580 mtx_unlock(&lun->lun_lock); 8581 free(ctsio->kern_data_ptr, M_CTL); 8582 ctl_set_reservation_conflict(ctsio); 8583 ctl_done((union ctl_io *)ctsio); 8584 return (CTL_RETVAL_COMPLETE); 8585 } else if (res_key != 0) { 8586 /* 8587 * We are not registered and trying to register but 8588 * the register key isn't zero. 8589 */ 8590 mtx_unlock(&lun->lun_lock); 8591 free(ctsio->kern_data_ptr, M_CTL); 8592 ctl_set_reservation_conflict(ctsio); 8593 ctl_done((union ctl_io *)ctsio); 8594 return (CTL_RETVAL_COMPLETE); 8595 } 8596 mtx_unlock(&lun->lun_lock); 8597 } 8598 8599 switch (cdb->action & SPRO_ACTION_MASK) { 8600 case SPRO_REGISTER: 8601 case SPRO_REG_IGNO: { 8602 8603#if 0 8604 printf("Registration received\n"); 8605#endif 8606 8607 /* 8608 * We don't support any of these options, as we report in 8609 * the read capabilities request (see 8610 * ctl_persistent_reserve_in(), above). 8611 */ 8612 if ((param->flags & SPR_SPEC_I_PT) 8613 || (param->flags & SPR_ALL_TG_PT) 8614 || (param->flags & SPR_APTPL)) { 8615 int bit_ptr; 8616 8617 if (param->flags & SPR_APTPL) 8618 bit_ptr = 0; 8619 else if (param->flags & SPR_ALL_TG_PT) 8620 bit_ptr = 2; 8621 else /* SPR_SPEC_I_PT */ 8622 bit_ptr = 3; 8623 8624 free(ctsio->kern_data_ptr, M_CTL); 8625 ctl_set_invalid_field(ctsio, 8626 /*sks_valid*/ 1, 8627 /*command*/ 0, 8628 /*field*/ 20, 8629 /*bit_valid*/ 1, 8630 /*bit*/ bit_ptr); 8631 ctl_done((union ctl_io *)ctsio); 8632 return (CTL_RETVAL_COMPLETE); 8633 } 8634 8635 mtx_lock(&lun->lun_lock); 8636 8637 /* 8638 * The initiator wants to clear the 8639 * key/unregister. 8640 */ 8641 if (sa_res_key == 0) { 8642 if ((res_key == 0 8643 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8644 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8645 && !lun->per_res[residx].registered)) { 8646 mtx_unlock(&lun->lun_lock); 8647 goto done; 8648 } 8649 8650 lun->per_res[residx].registered = 0; 8651 memset(&lun->per_res[residx].res_key, 8652 0, sizeof(lun->per_res[residx].res_key)); 8653 lun->pr_key_count--; 8654 8655 if (residx == lun->pr_res_idx) { 8656 lun->flags &= ~CTL_LUN_PR_RESERVED; 8657 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8658 8659 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8660 || lun->res_type == SPR_TYPE_EX_AC_RO) 8661 && lun->pr_key_count) { 8662 /* 8663 * If the reservation is a registrants 8664 * only type we need to generate a UA 8665 * for other registered inits. The 8666 * sense code should be RESERVATIONS 8667 * RELEASED 8668 */ 8669 8670 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8671 if (lun->per_res[ 8672 i+persis_offset].registered 8673 == 0) 8674 continue; 8675 lun->pending_ua[i] |= 8676 CTL_UA_RES_RELEASE; 8677 } 8678 } 8679 lun->res_type = 0; 8680 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8681 if (lun->pr_key_count==0) { 8682 lun->flags &= ~CTL_LUN_PR_RESERVED; 8683 lun->res_type = 0; 8684 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8685 } 8686 } 8687 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8688 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8689 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8690 persis_io.pr.pr_info.residx = residx; 8691 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8692 &persis_io, sizeof(persis_io), 0 )) > 8693 CTL_HA_STATUS_SUCCESS) { 8694 printf("CTL:Persis Out error returned from " 8695 "ctl_ha_msg_send %d\n", isc_retval); 8696 } 8697 } else /* sa_res_key != 0 */ { 8698 8699 /* 8700 * If we aren't registered currently then increment 8701 * the key count and set the registered flag. 8702 */ 8703 if (!lun->per_res[residx].registered) { 8704 lun->pr_key_count++; 8705 lun->per_res[residx].registered = 1; 8706 } 8707 8708 memcpy(&lun->per_res[residx].res_key, 8709 param->serv_act_res_key, 8710 ctl_min(sizeof(param->serv_act_res_key), 8711 sizeof(lun->per_res[residx].res_key))); 8712 8713 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8714 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8715 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8716 persis_io.pr.pr_info.residx = residx; 8717 memcpy(persis_io.pr.pr_info.sa_res_key, 8718 param->serv_act_res_key, 8719 sizeof(param->serv_act_res_key)); 8720 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8721 &persis_io, sizeof(persis_io), 0)) > 8722 CTL_HA_STATUS_SUCCESS) { 8723 printf("CTL:Persis Out error returned from " 8724 "ctl_ha_msg_send %d\n", isc_retval); 8725 } 8726 } 8727 lun->PRGeneration++; 8728 mtx_unlock(&lun->lun_lock); 8729 8730 break; 8731 } 8732 case SPRO_RESERVE: 8733#if 0 8734 printf("Reserve executed type %d\n", type); 8735#endif 8736 mtx_lock(&lun->lun_lock); 8737 if (lun->flags & CTL_LUN_PR_RESERVED) { 8738 /* 8739 * if this isn't the reservation holder and it's 8740 * not a "all registrants" type or if the type is 8741 * different then we have a conflict 8742 */ 8743 if ((lun->pr_res_idx != residx 8744 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8745 || lun->res_type != type) { 8746 mtx_unlock(&lun->lun_lock); 8747 free(ctsio->kern_data_ptr, M_CTL); 8748 ctl_set_reservation_conflict(ctsio); 8749 ctl_done((union ctl_io *)ctsio); 8750 return (CTL_RETVAL_COMPLETE); 8751 } 8752 mtx_unlock(&lun->lun_lock); 8753 } else /* create a reservation */ { 8754 /* 8755 * If it's not an "all registrants" type record 8756 * reservation holder 8757 */ 8758 if (type != SPR_TYPE_WR_EX_AR 8759 && type != SPR_TYPE_EX_AC_AR) 8760 lun->pr_res_idx = residx; /* Res holder */ 8761 else 8762 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8763 8764 lun->flags |= CTL_LUN_PR_RESERVED; 8765 lun->res_type = type; 8766 8767 mtx_unlock(&lun->lun_lock); 8768 8769 /* send msg to other side */ 8770 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8771 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8772 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8773 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8774 persis_io.pr.pr_info.res_type = type; 8775 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8776 &persis_io, sizeof(persis_io), 0)) > 8777 CTL_HA_STATUS_SUCCESS) { 8778 printf("CTL:Persis Out error returned from " 8779 "ctl_ha_msg_send %d\n", isc_retval); 8780 } 8781 } 8782 break; 8783 8784 case SPRO_RELEASE: 8785 mtx_lock(&lun->lun_lock); 8786 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8787 /* No reservation exists return good status */ 8788 mtx_unlock(&lun->lun_lock); 8789 goto done; 8790 } 8791 /* 8792 * Is this nexus a reservation holder? 8793 */ 8794 if (lun->pr_res_idx != residx 8795 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8796 /* 8797 * not a res holder return good status but 8798 * do nothing 8799 */ 8800 mtx_unlock(&lun->lun_lock); 8801 goto done; 8802 } 8803 8804 if (lun->res_type != type) { 8805 mtx_unlock(&lun->lun_lock); 8806 free(ctsio->kern_data_ptr, M_CTL); 8807 ctl_set_illegal_pr_release(ctsio); 8808 ctl_done((union ctl_io *)ctsio); 8809 return (CTL_RETVAL_COMPLETE); 8810 } 8811 8812 /* okay to release */ 8813 lun->flags &= ~CTL_LUN_PR_RESERVED; 8814 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8815 lun->res_type = 0; 8816 8817 /* 8818 * if this isn't an exclusive access 8819 * res generate UA for all other 8820 * registrants. 8821 */ 8822 if (type != SPR_TYPE_EX_AC 8823 && type != SPR_TYPE_WR_EX) { 8824 /* 8825 * temporarily unregister so we don't generate UA 8826 */ 8827 lun->per_res[residx].registered = 0; 8828 8829 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8830 if (lun->per_res[i+persis_offset].registered 8831 == 0) 8832 continue; 8833 lun->pending_ua[i] |= 8834 CTL_UA_RES_RELEASE; 8835 } 8836 8837 lun->per_res[residx].registered = 1; 8838 } 8839 mtx_unlock(&lun->lun_lock); 8840 /* Send msg to other side */ 8841 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8842 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8843 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8844 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8845 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8846 printf("CTL:Persis Out error returned from " 8847 "ctl_ha_msg_send %d\n", isc_retval); 8848 } 8849 break; 8850 8851 case SPRO_CLEAR: 8852 /* send msg to other side */ 8853 8854 mtx_lock(&lun->lun_lock); 8855 lun->flags &= ~CTL_LUN_PR_RESERVED; 8856 lun->res_type = 0; 8857 lun->pr_key_count = 0; 8858 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8859 8860 8861 memset(&lun->per_res[residx].res_key, 8862 0, sizeof(lun->per_res[residx].res_key)); 8863 lun->per_res[residx].registered = 0; 8864 8865 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8866 if (lun->per_res[i].registered) { 8867 if (!persis_offset && i < CTL_MAX_INITIATORS) 8868 lun->pending_ua[i] |= 8869 CTL_UA_RES_PREEMPT; 8870 else if (persis_offset && i >= persis_offset) 8871 lun->pending_ua[i-persis_offset] |= 8872 CTL_UA_RES_PREEMPT; 8873 8874 memset(&lun->per_res[i].res_key, 8875 0, sizeof(struct scsi_per_res_key)); 8876 lun->per_res[i].registered = 0; 8877 } 8878 lun->PRGeneration++; 8879 mtx_unlock(&lun->lun_lock); 8880 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8881 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8882 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8883 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8884 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8885 printf("CTL:Persis Out error returned from " 8886 "ctl_ha_msg_send %d\n", isc_retval); 8887 } 8888 break; 8889 8890 case SPRO_PREEMPT: { 8891 int nretval; 8892 8893 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8894 residx, ctsio, cdb, param); 8895 if (nretval != 0) 8896 return (CTL_RETVAL_COMPLETE); 8897 break; 8898 } 8899 default: 8900 panic("Invalid PR type %x", cdb->action); 8901 } 8902 8903done: 8904 free(ctsio->kern_data_ptr, M_CTL); 8905 ctl_set_success(ctsio); 8906 ctl_done((union ctl_io *)ctsio); 8907 8908 return (retval); 8909} 8910 8911/* 8912 * This routine is for handling a message from the other SC pertaining to 8913 * persistent reserve out. All the error checking will have been done 8914 * so only perorming the action need be done here to keep the two 8915 * in sync. 8916 */ 8917static void 8918ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8919{ 8920 struct ctl_lun *lun; 8921 struct ctl_softc *softc; 8922 int i; 8923 uint32_t targ_lun; 8924 8925 softc = control_softc; 8926 8927 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8928 lun = softc->ctl_luns[targ_lun]; 8929 mtx_lock(&lun->lun_lock); 8930 switch(msg->pr.pr_info.action) { 8931 case CTL_PR_REG_KEY: 8932 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8933 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8934 lun->pr_key_count++; 8935 } 8936 lun->PRGeneration++; 8937 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8938 msg->pr.pr_info.sa_res_key, 8939 sizeof(struct scsi_per_res_key)); 8940 break; 8941 8942 case CTL_PR_UNREG_KEY: 8943 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8944 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8945 0, sizeof(struct scsi_per_res_key)); 8946 lun->pr_key_count--; 8947 8948 /* XXX Need to see if the reservation has been released */ 8949 /* if so do we need to generate UA? */ 8950 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8951 lun->flags &= ~CTL_LUN_PR_RESERVED; 8952 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8953 8954 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8955 || lun->res_type == SPR_TYPE_EX_AC_RO) 8956 && lun->pr_key_count) { 8957 /* 8958 * If the reservation is a registrants 8959 * only type we need to generate a UA 8960 * for other registered inits. The 8961 * sense code should be RESERVATIONS 8962 * RELEASED 8963 */ 8964 8965 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8966 if (lun->per_res[i+ 8967 persis_offset].registered == 0) 8968 continue; 8969 8970 lun->pending_ua[i] |= 8971 CTL_UA_RES_RELEASE; 8972 } 8973 } 8974 lun->res_type = 0; 8975 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8976 if (lun->pr_key_count==0) { 8977 lun->flags &= ~CTL_LUN_PR_RESERVED; 8978 lun->res_type = 0; 8979 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8980 } 8981 } 8982 lun->PRGeneration++; 8983 break; 8984 8985 case CTL_PR_RESERVE: 8986 lun->flags |= CTL_LUN_PR_RESERVED; 8987 lun->res_type = msg->pr.pr_info.res_type; 8988 lun->pr_res_idx = msg->pr.pr_info.residx; 8989 8990 break; 8991 8992 case CTL_PR_RELEASE: 8993 /* 8994 * if this isn't an exclusive access res generate UA for all 8995 * other registrants. 8996 */ 8997 if (lun->res_type != SPR_TYPE_EX_AC 8998 && lun->res_type != SPR_TYPE_WR_EX) { 8999 for (i = 0; i < CTL_MAX_INITIATORS; i++) 9000 if (lun->per_res[i+persis_offset].registered) 9001 lun->pending_ua[i] |= 9002 CTL_UA_RES_RELEASE; 9003 } 9004 9005 lun->flags &= ~CTL_LUN_PR_RESERVED; 9006 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9007 lun->res_type = 0; 9008 break; 9009 9010 case CTL_PR_PREEMPT: 9011 ctl_pro_preempt_other(lun, msg); 9012 break; 9013 case CTL_PR_CLEAR: 9014 lun->flags &= ~CTL_LUN_PR_RESERVED; 9015 lun->res_type = 0; 9016 lun->pr_key_count = 0; 9017 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9018 9019 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 9020 if (lun->per_res[i].registered == 0) 9021 continue; 9022 if (!persis_offset 9023 && i < CTL_MAX_INITIATORS) 9024 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 9025 else if (persis_offset 9026 && i >= persis_offset) 9027 lun->pending_ua[i-persis_offset] |= 9028 CTL_UA_RES_PREEMPT; 9029 memset(&lun->per_res[i].res_key, 0, 9030 sizeof(struct scsi_per_res_key)); 9031 lun->per_res[i].registered = 0; 9032 } 9033 lun->PRGeneration++; 9034 break; 9035 } 9036 9037 mtx_unlock(&lun->lun_lock); 9038} 9039 9040int 9041ctl_read_write(struct ctl_scsiio *ctsio) 9042{ 9043 struct ctl_lun *lun; 9044 struct ctl_lba_len_flags *lbalen; 9045 uint64_t lba; 9046 uint32_t num_blocks; 9047 int flags, retval; 9048 int isread; 9049 9050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9051 9052 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9053 9054 flags = 0; 9055 retval = CTL_RETVAL_COMPLETE; 9056 9057 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9058 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9059 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9060 uint32_t residx; 9061 9062 /* 9063 * XXX KDM need a lock here. 9064 */ 9065 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9066 if ((lun->res_type == SPR_TYPE_EX_AC 9067 && residx != lun->pr_res_idx) 9068 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9069 || lun->res_type == SPR_TYPE_EX_AC_AR) 9070 && !lun->per_res[residx].registered)) { 9071 ctl_set_reservation_conflict(ctsio); 9072 ctl_done((union ctl_io *)ctsio); 9073 return (CTL_RETVAL_COMPLETE); 9074 } 9075 } 9076 9077 switch (ctsio->cdb[0]) { 9078 case READ_6: 9079 case WRITE_6: { 9080 struct scsi_rw_6 *cdb; 9081 9082 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9083 9084 lba = scsi_3btoul(cdb->addr); 9085 /* only 5 bits are valid in the most significant address byte */ 9086 lba &= 0x1fffff; 9087 num_blocks = cdb->length; 9088 /* 9089 * This is correct according to SBC-2. 9090 */ 9091 if (num_blocks == 0) 9092 num_blocks = 256; 9093 break; 9094 } 9095 case READ_10: 9096 case WRITE_10: { 9097 struct scsi_rw_10 *cdb; 9098 9099 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9100 if (cdb->byte2 & SRW10_FUA) 9101 flags |= CTL_LLF_FUA; 9102 if (cdb->byte2 & SRW10_DPO) 9103 flags |= CTL_LLF_DPO; 9104 lba = scsi_4btoul(cdb->addr); 9105 num_blocks = scsi_2btoul(cdb->length); 9106 break; 9107 } 9108 case WRITE_VERIFY_10: { 9109 struct scsi_write_verify_10 *cdb; 9110 9111 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9112 flags |= CTL_LLF_FUA; 9113 if (cdb->byte2 & SWV_DPO) 9114 flags |= CTL_LLF_DPO; 9115 lba = scsi_4btoul(cdb->addr); 9116 num_blocks = scsi_2btoul(cdb->length); 9117 break; 9118 } 9119 case READ_12: 9120 case WRITE_12: { 9121 struct scsi_rw_12 *cdb; 9122 9123 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9124 if (cdb->byte2 & SRW12_FUA) 9125 flags |= CTL_LLF_FUA; 9126 if (cdb->byte2 & SRW12_DPO) 9127 flags |= CTL_LLF_DPO; 9128 lba = scsi_4btoul(cdb->addr); 9129 num_blocks = scsi_4btoul(cdb->length); 9130 break; 9131 } 9132 case WRITE_VERIFY_12: { 9133 struct scsi_write_verify_12 *cdb; 9134 9135 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9136 flags |= CTL_LLF_FUA; 9137 if (cdb->byte2 & SWV_DPO) 9138 flags |= CTL_LLF_DPO; 9139 lba = scsi_4btoul(cdb->addr); 9140 num_blocks = scsi_4btoul(cdb->length); 9141 break; 9142 } 9143 case READ_16: 9144 case WRITE_16: { 9145 struct scsi_rw_16 *cdb; 9146 9147 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9148 if (cdb->byte2 & SRW12_FUA) 9149 flags |= CTL_LLF_FUA; 9150 if (cdb->byte2 & SRW12_DPO) 9151 flags |= CTL_LLF_DPO; 9152 lba = scsi_8btou64(cdb->addr); 9153 num_blocks = scsi_4btoul(cdb->length); 9154 break; 9155 } 9156 case WRITE_VERIFY_16: { 9157 struct scsi_write_verify_16 *cdb; 9158 9159 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9160 flags |= CTL_LLF_FUA; 9161 if (cdb->byte2 & SWV_DPO) 9162 flags |= CTL_LLF_DPO; 9163 lba = scsi_8btou64(cdb->addr); 9164 num_blocks = scsi_4btoul(cdb->length); 9165 break; 9166 } 9167 default: 9168 /* 9169 * We got a command we don't support. This shouldn't 9170 * happen, commands should be filtered out above us. 9171 */ 9172 ctl_set_invalid_opcode(ctsio); 9173 ctl_done((union ctl_io *)ctsio); 9174 9175 return (CTL_RETVAL_COMPLETE); 9176 break; /* NOTREACHED */ 9177 } 9178 9179 /* 9180 * The first check is to make sure we're in bounds, the second 9181 * check is to catch wrap-around problems. If the lba + num blocks 9182 * is less than the lba, then we've wrapped around and the block 9183 * range is invalid anyway. 9184 */ 9185 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9186 || ((lba + num_blocks) < lba)) { 9187 ctl_set_lba_out_of_range(ctsio); 9188 ctl_done((union ctl_io *)ctsio); 9189 return (CTL_RETVAL_COMPLETE); 9190 } 9191 9192 /* 9193 * According to SBC-3, a transfer length of 0 is not an error. 9194 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9195 * translates to 256 blocks for those commands. 9196 */ 9197 if (num_blocks == 0) { 9198 ctl_set_success(ctsio); 9199 ctl_done((union ctl_io *)ctsio); 9200 return (CTL_RETVAL_COMPLETE); 9201 } 9202 9203 /* Set FUA and/or DPO if caches are disabled. */ 9204 if (isread) { 9205 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9206 SCP_RCD) != 0) 9207 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9208 } else { 9209 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9210 SCP_WCE) == 0) 9211 flags |= CTL_LLF_FUA; 9212 } 9213 9214 lbalen = (struct ctl_lba_len_flags *) 9215 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9216 lbalen->lba = lba; 9217 lbalen->len = num_blocks; 9218 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9219 9220 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9221 ctsio->kern_rel_offset = 0; 9222 9223 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9224 9225 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9226 9227 return (retval); 9228} 9229 9230static int 9231ctl_cnw_cont(union ctl_io *io) 9232{ 9233 struct ctl_scsiio *ctsio; 9234 struct ctl_lun *lun; 9235 struct ctl_lba_len_flags *lbalen; 9236 int retval; 9237 9238 ctsio = &io->scsiio; 9239 ctsio->io_hdr.status = CTL_STATUS_NONE; 9240 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9241 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9242 lbalen = (struct ctl_lba_len_flags *) 9243 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9244 lbalen->flags &= ~CTL_LLF_COMPARE; 9245 lbalen->flags |= CTL_LLF_WRITE; 9246 9247 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9248 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9249 return (retval); 9250} 9251 9252int 9253ctl_cnw(struct ctl_scsiio *ctsio) 9254{ 9255 struct ctl_lun *lun; 9256 struct ctl_lba_len_flags *lbalen; 9257 uint64_t lba; 9258 uint32_t num_blocks; 9259 int flags, retval; 9260 9261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9262 9263 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9264 9265 flags = 0; 9266 retval = CTL_RETVAL_COMPLETE; 9267 9268 switch (ctsio->cdb[0]) { 9269 case COMPARE_AND_WRITE: { 9270 struct scsi_compare_and_write *cdb; 9271 9272 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9273 if (cdb->byte2 & SRW10_FUA) 9274 flags |= CTL_LLF_FUA; 9275 if (cdb->byte2 & SRW10_DPO) 9276 flags |= CTL_LLF_DPO; 9277 lba = scsi_8btou64(cdb->addr); 9278 num_blocks = cdb->length; 9279 break; 9280 } 9281 default: 9282 /* 9283 * We got a command we don't support. This shouldn't 9284 * happen, commands should be filtered out above us. 9285 */ 9286 ctl_set_invalid_opcode(ctsio); 9287 ctl_done((union ctl_io *)ctsio); 9288 9289 return (CTL_RETVAL_COMPLETE); 9290 break; /* NOTREACHED */ 9291 } 9292 9293 /* 9294 * The first check is to make sure we're in bounds, the second 9295 * check is to catch wrap-around problems. If the lba + num blocks 9296 * is less than the lba, then we've wrapped around and the block 9297 * range is invalid anyway. 9298 */ 9299 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9300 || ((lba + num_blocks) < lba)) { 9301 ctl_set_lba_out_of_range(ctsio); 9302 ctl_done((union ctl_io *)ctsio); 9303 return (CTL_RETVAL_COMPLETE); 9304 } 9305 9306 /* 9307 * According to SBC-3, a transfer length of 0 is not an error. 9308 */ 9309 if (num_blocks == 0) { 9310 ctl_set_success(ctsio); 9311 ctl_done((union ctl_io *)ctsio); 9312 return (CTL_RETVAL_COMPLETE); 9313 } 9314 9315 /* Set FUA if write cache is disabled. */ 9316 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9317 SCP_WCE) == 0) 9318 flags |= CTL_LLF_FUA; 9319 9320 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9321 ctsio->kern_rel_offset = 0; 9322 9323 /* 9324 * Set the IO_CONT flag, so that if this I/O gets passed to 9325 * ctl_data_submit_done(), it'll get passed back to 9326 * ctl_ctl_cnw_cont() for further processing. 9327 */ 9328 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9329 ctsio->io_cont = ctl_cnw_cont; 9330 9331 lbalen = (struct ctl_lba_len_flags *) 9332 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9333 lbalen->lba = lba; 9334 lbalen->len = num_blocks; 9335 lbalen->flags = CTL_LLF_COMPARE | flags; 9336 9337 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9338 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9339 return (retval); 9340} 9341 9342int 9343ctl_verify(struct ctl_scsiio *ctsio) 9344{ 9345 struct ctl_lun *lun; 9346 struct ctl_lba_len_flags *lbalen; 9347 uint64_t lba; 9348 uint32_t num_blocks; 9349 int bytchk, flags; 9350 int retval; 9351 9352 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9353 9354 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9355 9356 bytchk = 0; 9357 flags = CTL_LLF_FUA; 9358 retval = CTL_RETVAL_COMPLETE; 9359 9360 switch (ctsio->cdb[0]) { 9361 case VERIFY_10: { 9362 struct scsi_verify_10 *cdb; 9363 9364 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9365 if (cdb->byte2 & SVFY_BYTCHK) 9366 bytchk = 1; 9367 if (cdb->byte2 & SVFY_DPO) 9368 flags |= CTL_LLF_DPO; 9369 lba = scsi_4btoul(cdb->addr); 9370 num_blocks = scsi_2btoul(cdb->length); 9371 break; 9372 } 9373 case VERIFY_12: { 9374 struct scsi_verify_12 *cdb; 9375 9376 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9377 if (cdb->byte2 & SVFY_BYTCHK) 9378 bytchk = 1; 9379 if (cdb->byte2 & SVFY_DPO) 9380 flags |= CTL_LLF_DPO; 9381 lba = scsi_4btoul(cdb->addr); 9382 num_blocks = scsi_4btoul(cdb->length); 9383 break; 9384 } 9385 case VERIFY_16: { 9386 struct scsi_rw_16 *cdb; 9387 9388 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9389 if (cdb->byte2 & SVFY_BYTCHK) 9390 bytchk = 1; 9391 if (cdb->byte2 & SVFY_DPO) 9392 flags |= CTL_LLF_DPO; 9393 lba = scsi_8btou64(cdb->addr); 9394 num_blocks = scsi_4btoul(cdb->length); 9395 break; 9396 } 9397 default: 9398 /* 9399 * We got a command we don't support. This shouldn't 9400 * happen, commands should be filtered out above us. 9401 */ 9402 ctl_set_invalid_opcode(ctsio); 9403 ctl_done((union ctl_io *)ctsio); 9404 return (CTL_RETVAL_COMPLETE); 9405 } 9406 9407 /* 9408 * The first check is to make sure we're in bounds, the second 9409 * check is to catch wrap-around problems. If the lba + num blocks 9410 * is less than the lba, then we've wrapped around and the block 9411 * range is invalid anyway. 9412 */ 9413 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9414 || ((lba + num_blocks) < lba)) { 9415 ctl_set_lba_out_of_range(ctsio); 9416 ctl_done((union ctl_io *)ctsio); 9417 return (CTL_RETVAL_COMPLETE); 9418 } 9419 9420 /* 9421 * According to SBC-3, a transfer length of 0 is not an error. 9422 */ 9423 if (num_blocks == 0) { 9424 ctl_set_success(ctsio); 9425 ctl_done((union ctl_io *)ctsio); 9426 return (CTL_RETVAL_COMPLETE); 9427 } 9428 9429 lbalen = (struct ctl_lba_len_flags *) 9430 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9431 lbalen->lba = lba; 9432 lbalen->len = num_blocks; 9433 if (bytchk) { 9434 lbalen->flags = CTL_LLF_COMPARE | flags; 9435 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9436 } else { 9437 lbalen->flags = CTL_LLF_VERIFY | flags; 9438 ctsio->kern_total_len = 0; 9439 } 9440 ctsio->kern_rel_offset = 0; 9441 9442 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9443 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9444 return (retval); 9445} 9446 9447int 9448ctl_report_luns(struct ctl_scsiio *ctsio) 9449{ 9450 struct scsi_report_luns *cdb; 9451 struct scsi_report_luns_data *lun_data; 9452 struct ctl_lun *lun, *request_lun; 9453 int num_luns, retval; 9454 uint32_t alloc_len, lun_datalen; 9455 int num_filled, well_known; 9456 uint32_t initidx, targ_lun_id, lun_id; 9457 9458 retval = CTL_RETVAL_COMPLETE; 9459 well_known = 0; 9460 9461 cdb = (struct scsi_report_luns *)ctsio->cdb; 9462 9463 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9464 9465 mtx_lock(&control_softc->ctl_lock); 9466 num_luns = control_softc->num_luns; 9467 mtx_unlock(&control_softc->ctl_lock); 9468 9469 switch (cdb->select_report) { 9470 case RPL_REPORT_DEFAULT: 9471 case RPL_REPORT_ALL: 9472 break; 9473 case RPL_REPORT_WELLKNOWN: 9474 well_known = 1; 9475 num_luns = 0; 9476 break; 9477 default: 9478 ctl_set_invalid_field(ctsio, 9479 /*sks_valid*/ 1, 9480 /*command*/ 1, 9481 /*field*/ 2, 9482 /*bit_valid*/ 0, 9483 /*bit*/ 0); 9484 ctl_done((union ctl_io *)ctsio); 9485 return (retval); 9486 break; /* NOTREACHED */ 9487 } 9488 9489 alloc_len = scsi_4btoul(cdb->length); 9490 /* 9491 * The initiator has to allocate at least 16 bytes for this request, 9492 * so he can at least get the header and the first LUN. Otherwise 9493 * we reject the request (per SPC-3 rev 14, section 6.21). 9494 */ 9495 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9496 sizeof(struct scsi_report_luns_lundata))) { 9497 ctl_set_invalid_field(ctsio, 9498 /*sks_valid*/ 1, 9499 /*command*/ 1, 9500 /*field*/ 6, 9501 /*bit_valid*/ 0, 9502 /*bit*/ 0); 9503 ctl_done((union ctl_io *)ctsio); 9504 return (retval); 9505 } 9506 9507 request_lun = (struct ctl_lun *) 9508 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9509 9510 lun_datalen = sizeof(*lun_data) + 9511 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9512 9513 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9514 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9515 ctsio->kern_sg_entries = 0; 9516 9517 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9518 9519 mtx_lock(&control_softc->ctl_lock); 9520 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9521 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9522 if (lun_id >= CTL_MAX_LUNS) 9523 continue; 9524 lun = control_softc->ctl_luns[lun_id]; 9525 if (lun == NULL) 9526 continue; 9527 9528 if (targ_lun_id <= 0xff) { 9529 /* 9530 * Peripheral addressing method, bus number 0. 9531 */ 9532 lun_data->luns[num_filled].lundata[0] = 9533 RPL_LUNDATA_ATYP_PERIPH; 9534 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9535 num_filled++; 9536 } else if (targ_lun_id <= 0x3fff) { 9537 /* 9538 * Flat addressing method. 9539 */ 9540 lun_data->luns[num_filled].lundata[0] = 9541 RPL_LUNDATA_ATYP_FLAT | 9542 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9543#ifdef OLDCTLHEADERS 9544 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9545 (targ_lun_id & SRLD_BUS_LUN_MASK); 9546#endif 9547 lun_data->luns[num_filled].lundata[1] = 9548#ifdef OLDCTLHEADERS 9549 targ_lun_id >> SRLD_BUS_LUN_BITS; 9550#endif 9551 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9552 num_filled++; 9553 } else { 9554 printf("ctl_report_luns: bogus LUN number %jd, " 9555 "skipping\n", (intmax_t)targ_lun_id); 9556 } 9557 /* 9558 * According to SPC-3, rev 14 section 6.21: 9559 * 9560 * "The execution of a REPORT LUNS command to any valid and 9561 * installed logical unit shall clear the REPORTED LUNS DATA 9562 * HAS CHANGED unit attention condition for all logical 9563 * units of that target with respect to the requesting 9564 * initiator. A valid and installed logical unit is one 9565 * having a PERIPHERAL QUALIFIER of 000b in the standard 9566 * INQUIRY data (see 6.4.2)." 9567 * 9568 * If request_lun is NULL, the LUN this report luns command 9569 * was issued to is either disabled or doesn't exist. In that 9570 * case, we shouldn't clear any pending lun change unit 9571 * attention. 9572 */ 9573 if (request_lun != NULL) { 9574 mtx_lock(&lun->lun_lock); 9575 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9576 mtx_unlock(&lun->lun_lock); 9577 } 9578 } 9579 mtx_unlock(&control_softc->ctl_lock); 9580 9581 /* 9582 * It's quite possible that we've returned fewer LUNs than we allocated 9583 * space for. Trim it. 9584 */ 9585 lun_datalen = sizeof(*lun_data) + 9586 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9587 9588 if (lun_datalen < alloc_len) { 9589 ctsio->residual = alloc_len - lun_datalen; 9590 ctsio->kern_data_len = lun_datalen; 9591 ctsio->kern_total_len = lun_datalen; 9592 } else { 9593 ctsio->residual = 0; 9594 ctsio->kern_data_len = alloc_len; 9595 ctsio->kern_total_len = alloc_len; 9596 } 9597 ctsio->kern_data_resid = 0; 9598 ctsio->kern_rel_offset = 0; 9599 ctsio->kern_sg_entries = 0; 9600 9601 /* 9602 * We set this to the actual data length, regardless of how much 9603 * space we actually have to return results. If the user looks at 9604 * this value, he'll know whether or not he allocated enough space 9605 * and reissue the command if necessary. We don't support well 9606 * known logical units, so if the user asks for that, return none. 9607 */ 9608 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9609 9610 /* 9611 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9612 * this request. 9613 */ 9614 ctsio->scsi_status = SCSI_STATUS_OK; 9615 9616 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9617 ctsio->be_move_done = ctl_config_move_done; 9618 ctl_datamove((union ctl_io *)ctsio); 9619 9620 return (retval); 9621} 9622 9623int 9624ctl_request_sense(struct ctl_scsiio *ctsio) 9625{ 9626 struct scsi_request_sense *cdb; 9627 struct scsi_sense_data *sense_ptr; 9628 struct ctl_lun *lun; 9629 uint32_t initidx; 9630 int have_error; 9631 scsi_sense_data_type sense_format; 9632 9633 cdb = (struct scsi_request_sense *)ctsio->cdb; 9634 9635 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9636 9637 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9638 9639 /* 9640 * Determine which sense format the user wants. 9641 */ 9642 if (cdb->byte2 & SRS_DESC) 9643 sense_format = SSD_TYPE_DESC; 9644 else 9645 sense_format = SSD_TYPE_FIXED; 9646 9647 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9648 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9649 ctsio->kern_sg_entries = 0; 9650 9651 /* 9652 * struct scsi_sense_data, which is currently set to 256 bytes, is 9653 * larger than the largest allowed value for the length field in the 9654 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9655 */ 9656 ctsio->residual = 0; 9657 ctsio->kern_data_len = cdb->length; 9658 ctsio->kern_total_len = cdb->length; 9659 9660 ctsio->kern_data_resid = 0; 9661 ctsio->kern_rel_offset = 0; 9662 ctsio->kern_sg_entries = 0; 9663 9664 /* 9665 * If we don't have a LUN, we don't have any pending sense. 9666 */ 9667 if (lun == NULL) 9668 goto no_sense; 9669 9670 have_error = 0; 9671 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9672 /* 9673 * Check for pending sense, and then for pending unit attentions. 9674 * Pending sense gets returned first, then pending unit attentions. 9675 */ 9676 mtx_lock(&lun->lun_lock); 9677#ifdef CTL_WITH_CA 9678 if (ctl_is_set(lun->have_ca, initidx)) { 9679 scsi_sense_data_type stored_format; 9680 9681 /* 9682 * Check to see which sense format was used for the stored 9683 * sense data. 9684 */ 9685 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9686 9687 /* 9688 * If the user requested a different sense format than the 9689 * one we stored, then we need to convert it to the other 9690 * format. If we're going from descriptor to fixed format 9691 * sense data, we may lose things in translation, depending 9692 * on what options were used. 9693 * 9694 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9695 * for some reason we'll just copy it out as-is. 9696 */ 9697 if ((stored_format == SSD_TYPE_FIXED) 9698 && (sense_format == SSD_TYPE_DESC)) 9699 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9700 &lun->pending_sense[initidx], 9701 (struct scsi_sense_data_desc *)sense_ptr); 9702 else if ((stored_format == SSD_TYPE_DESC) 9703 && (sense_format == SSD_TYPE_FIXED)) 9704 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9705 &lun->pending_sense[initidx], 9706 (struct scsi_sense_data_fixed *)sense_ptr); 9707 else 9708 memcpy(sense_ptr, &lun->pending_sense[initidx], 9709 ctl_min(sizeof(*sense_ptr), 9710 sizeof(lun->pending_sense[initidx]))); 9711 9712 ctl_clear_mask(lun->have_ca, initidx); 9713 have_error = 1; 9714 } else 9715#endif 9716 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9717 ctl_ua_type ua_type; 9718 9719 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9720 sense_ptr, sense_format); 9721 if (ua_type != CTL_UA_NONE) 9722 have_error = 1; 9723 } 9724 mtx_unlock(&lun->lun_lock); 9725 9726 /* 9727 * We already have a pending error, return it. 9728 */ 9729 if (have_error != 0) { 9730 /* 9731 * We report the SCSI status as OK, since the status of the 9732 * request sense command itself is OK. 9733 */ 9734 ctsio->scsi_status = SCSI_STATUS_OK; 9735 9736 /* 9737 * We report 0 for the sense length, because we aren't doing 9738 * autosense in this case. We're reporting sense as 9739 * parameter data. 9740 */ 9741 ctsio->sense_len = 0; 9742 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9743 ctsio->be_move_done = ctl_config_move_done; 9744 ctl_datamove((union ctl_io *)ctsio); 9745 9746 return (CTL_RETVAL_COMPLETE); 9747 } 9748 9749no_sense: 9750 9751 /* 9752 * No sense information to report, so we report that everything is 9753 * okay. 9754 */ 9755 ctl_set_sense_data(sense_ptr, 9756 lun, 9757 sense_format, 9758 /*current_error*/ 1, 9759 /*sense_key*/ SSD_KEY_NO_SENSE, 9760 /*asc*/ 0x00, 9761 /*ascq*/ 0x00, 9762 SSD_ELEM_NONE); 9763 9764 ctsio->scsi_status = SCSI_STATUS_OK; 9765 9766 /* 9767 * We report 0 for the sense length, because we aren't doing 9768 * autosense in this case. We're reporting sense as parameter data. 9769 */ 9770 ctsio->sense_len = 0; 9771 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9772 ctsio->be_move_done = ctl_config_move_done; 9773 ctl_datamove((union ctl_io *)ctsio); 9774 9775 return (CTL_RETVAL_COMPLETE); 9776} 9777 9778int 9779ctl_tur(struct ctl_scsiio *ctsio) 9780{ 9781 struct ctl_lun *lun; 9782 9783 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9784 9785 CTL_DEBUG_PRINT(("ctl_tur\n")); 9786 9787 if (lun == NULL) 9788 return (EINVAL); 9789 9790 ctsio->scsi_status = SCSI_STATUS_OK; 9791 ctsio->io_hdr.status = CTL_SUCCESS; 9792 9793 ctl_done((union ctl_io *)ctsio); 9794 9795 return (CTL_RETVAL_COMPLETE); 9796} 9797 9798#ifdef notyet 9799static int 9800ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9801{ 9802 9803} 9804#endif 9805 9806static int 9807ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9808{ 9809 struct scsi_vpd_supported_pages *pages; 9810 int sup_page_size; 9811 struct ctl_lun *lun; 9812 9813 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9814 9815 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9816 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9817 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9818 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9819 ctsio->kern_sg_entries = 0; 9820 9821 if (sup_page_size < alloc_len) { 9822 ctsio->residual = alloc_len - sup_page_size; 9823 ctsio->kern_data_len = sup_page_size; 9824 ctsio->kern_total_len = sup_page_size; 9825 } else { 9826 ctsio->residual = 0; 9827 ctsio->kern_data_len = alloc_len; 9828 ctsio->kern_total_len = alloc_len; 9829 } 9830 ctsio->kern_data_resid = 0; 9831 ctsio->kern_rel_offset = 0; 9832 ctsio->kern_sg_entries = 0; 9833 9834 /* 9835 * The control device is always connected. The disk device, on the 9836 * other hand, may not be online all the time. Need to change this 9837 * to figure out whether the disk device is actually online or not. 9838 */ 9839 if (lun != NULL) 9840 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9841 lun->be_lun->lun_type; 9842 else 9843 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9844 9845 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9846 /* Supported VPD pages */ 9847 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9848 /* Serial Number */ 9849 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9850 /* Device Identification */ 9851 pages->page_list[2] = SVPD_DEVICE_ID; 9852 /* Mode Page Policy */ 9853 pages->page_list[3] = SVPD_MODE_PAGE_POLICY; 9854 /* SCSI Ports */ 9855 pages->page_list[4] = SVPD_SCSI_PORTS; 9856 /* Third-party Copy */ 9857 pages->page_list[5] = SVPD_SCSI_TPC; 9858 /* Block limits */ 9859 pages->page_list[6] = SVPD_BLOCK_LIMITS; 9860 /* Block Device Characteristics */ 9861 pages->page_list[7] = SVPD_BDC; 9862 /* Logical Block Provisioning */ 9863 pages->page_list[8] = SVPD_LBP; 9864 9865 ctsio->scsi_status = SCSI_STATUS_OK; 9866 9867 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9868 ctsio->be_move_done = ctl_config_move_done; 9869 ctl_datamove((union ctl_io *)ctsio); 9870 9871 return (CTL_RETVAL_COMPLETE); 9872} 9873 9874static int 9875ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9876{ 9877 struct scsi_vpd_unit_serial_number *sn_ptr; 9878 struct ctl_lun *lun; 9879 9880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9881 9882 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9883 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9884 ctsio->kern_sg_entries = 0; 9885 9886 if (sizeof(*sn_ptr) < alloc_len) { 9887 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9888 ctsio->kern_data_len = sizeof(*sn_ptr); 9889 ctsio->kern_total_len = sizeof(*sn_ptr); 9890 } else { 9891 ctsio->residual = 0; 9892 ctsio->kern_data_len = alloc_len; 9893 ctsio->kern_total_len = alloc_len; 9894 } 9895 ctsio->kern_data_resid = 0; 9896 ctsio->kern_rel_offset = 0; 9897 ctsio->kern_sg_entries = 0; 9898 9899 /* 9900 * The control device is always connected. The disk device, on the 9901 * other hand, may not be online all the time. Need to change this 9902 * to figure out whether the disk device is actually online or not. 9903 */ 9904 if (lun != NULL) 9905 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9906 lun->be_lun->lun_type; 9907 else 9908 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9909 9910 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9911 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9912 /* 9913 * If we don't have a LUN, we just leave the serial number as 9914 * all spaces. 9915 */ 9916 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9917 if (lun != NULL) { 9918 strncpy((char *)sn_ptr->serial_num, 9919 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9920 } 9921 ctsio->scsi_status = SCSI_STATUS_OK; 9922 9923 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9924 ctsio->be_move_done = ctl_config_move_done; 9925 ctl_datamove((union ctl_io *)ctsio); 9926 9927 return (CTL_RETVAL_COMPLETE); 9928} 9929 9930 9931static int 9932ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9933{ 9934 struct scsi_vpd_mode_page_policy *mpp_ptr; 9935 struct ctl_lun *lun; 9936 int data_len; 9937 9938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9939 9940 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9941 sizeof(struct scsi_vpd_mode_page_policy_descr); 9942 9943 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9944 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9945 ctsio->kern_sg_entries = 0; 9946 9947 if (data_len < alloc_len) { 9948 ctsio->residual = alloc_len - data_len; 9949 ctsio->kern_data_len = data_len; 9950 ctsio->kern_total_len = data_len; 9951 } else { 9952 ctsio->residual = 0; 9953 ctsio->kern_data_len = alloc_len; 9954 ctsio->kern_total_len = alloc_len; 9955 } 9956 ctsio->kern_data_resid = 0; 9957 ctsio->kern_rel_offset = 0; 9958 ctsio->kern_sg_entries = 0; 9959 9960 /* 9961 * The control device is always connected. The disk device, on the 9962 * other hand, may not be online all the time. 9963 */ 9964 if (lun != NULL) 9965 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9966 lun->be_lun->lun_type; 9967 else 9968 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9969 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9970 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9971 mpp_ptr->descr[0].page_code = 0x3f; 9972 mpp_ptr->descr[0].subpage_code = 0xff; 9973 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9974 9975 ctsio->scsi_status = SCSI_STATUS_OK; 9976 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9977 ctsio->be_move_done = ctl_config_move_done; 9978 ctl_datamove((union ctl_io *)ctsio); 9979 9980 return (CTL_RETVAL_COMPLETE); 9981} 9982 9983static int 9984ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9985{ 9986 struct scsi_vpd_device_id *devid_ptr; 9987 struct scsi_vpd_id_descriptor *desc; 9988 struct ctl_softc *ctl_softc; 9989 struct ctl_lun *lun; 9990 struct ctl_port *port; 9991 int data_len; 9992 uint8_t proto; 9993 9994 ctl_softc = control_softc; 9995 9996 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9997 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9998 9999 data_len = sizeof(struct scsi_vpd_device_id) + 10000 sizeof(struct scsi_vpd_id_descriptor) + 10001 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 10002 sizeof(struct scsi_vpd_id_descriptor) + 10003 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 10004 if (lun && lun->lun_devid) 10005 data_len += lun->lun_devid->len; 10006 if (port->port_devid) 10007 data_len += port->port_devid->len; 10008 if (port->target_devid) 10009 data_len += port->target_devid->len; 10010 10011 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10012 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10013 ctsio->kern_sg_entries = 0; 10014 10015 if (data_len < alloc_len) { 10016 ctsio->residual = alloc_len - data_len; 10017 ctsio->kern_data_len = data_len; 10018 ctsio->kern_total_len = data_len; 10019 } else { 10020 ctsio->residual = 0; 10021 ctsio->kern_data_len = alloc_len; 10022 ctsio->kern_total_len = alloc_len; 10023 } 10024 ctsio->kern_data_resid = 0; 10025 ctsio->kern_rel_offset = 0; 10026 ctsio->kern_sg_entries = 0; 10027 10028 /* 10029 * The control device is always connected. The disk device, on the 10030 * other hand, may not be online all the time. 10031 */ 10032 if (lun != NULL) 10033 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10034 lun->be_lun->lun_type; 10035 else 10036 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10037 devid_ptr->page_code = SVPD_DEVICE_ID; 10038 scsi_ulto2b(data_len - 4, devid_ptr->length); 10039 10040 if (port->port_type == CTL_PORT_FC) 10041 proto = SCSI_PROTO_FC << 4; 10042 else if (port->port_type == CTL_PORT_ISCSI) 10043 proto = SCSI_PROTO_ISCSI << 4; 10044 else 10045 proto = SCSI_PROTO_SPI << 4; 10046 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10047 10048 /* 10049 * We're using a LUN association here. i.e., this device ID is a 10050 * per-LUN identifier. 10051 */ 10052 if (lun && lun->lun_devid) { 10053 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10054 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10055 lun->lun_devid->len); 10056 } 10057 10058 /* 10059 * This is for the WWPN which is a port association. 10060 */ 10061 if (port->port_devid) { 10062 memcpy(desc, port->port_devid->data, port->port_devid->len); 10063 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10064 port->port_devid->len); 10065 } 10066 10067 /* 10068 * This is for the Relative Target Port(type 4h) identifier 10069 */ 10070 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10071 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10072 SVPD_ID_TYPE_RELTARG; 10073 desc->length = 4; 10074 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10075 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10076 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10077 10078 /* 10079 * This is for the Target Port Group(type 5h) identifier 10080 */ 10081 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10082 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10083 SVPD_ID_TYPE_TPORTGRP; 10084 desc->length = 4; 10085 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10086 &desc->identifier[2]); 10087 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10088 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10089 10090 /* 10091 * This is for the Target identifier 10092 */ 10093 if (port->target_devid) { 10094 memcpy(desc, port->target_devid->data, port->target_devid->len); 10095 } 10096 10097 ctsio->scsi_status = SCSI_STATUS_OK; 10098 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10099 ctsio->be_move_done = ctl_config_move_done; 10100 ctl_datamove((union ctl_io *)ctsio); 10101 10102 return (CTL_RETVAL_COMPLETE); 10103} 10104 10105static int 10106ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10107{ 10108 struct ctl_softc *softc = control_softc; 10109 struct scsi_vpd_scsi_ports *sp; 10110 struct scsi_vpd_port_designation *pd; 10111 struct scsi_vpd_port_designation_cont *pdc; 10112 struct ctl_lun *lun; 10113 struct ctl_port *port; 10114 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10115 int num_target_port_groups, single; 10116 10117 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10118 10119 single = ctl_is_single; 10120 if (single) 10121 num_target_port_groups = 1; 10122 else 10123 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10124 num_target_ports = 0; 10125 iid_len = 0; 10126 id_len = 0; 10127 mtx_lock(&softc->ctl_lock); 10128 STAILQ_FOREACH(port, &softc->port_list, links) { 10129 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10130 continue; 10131 if (lun != NULL && 10132 ctl_map_lun_back(port->targ_port, lun->lun) >= 10133 CTL_MAX_LUNS) 10134 continue; 10135 num_target_ports++; 10136 if (port->init_devid) 10137 iid_len += port->init_devid->len; 10138 if (port->port_devid) 10139 id_len += port->port_devid->len; 10140 } 10141 mtx_unlock(&softc->ctl_lock); 10142 10143 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10144 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10145 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10146 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10147 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10148 ctsio->kern_sg_entries = 0; 10149 10150 if (data_len < alloc_len) { 10151 ctsio->residual = alloc_len - data_len; 10152 ctsio->kern_data_len = data_len; 10153 ctsio->kern_total_len = data_len; 10154 } else { 10155 ctsio->residual = 0; 10156 ctsio->kern_data_len = alloc_len; 10157 ctsio->kern_total_len = alloc_len; 10158 } 10159 ctsio->kern_data_resid = 0; 10160 ctsio->kern_rel_offset = 0; 10161 ctsio->kern_sg_entries = 0; 10162 10163 /* 10164 * The control device is always connected. The disk device, on the 10165 * other hand, may not be online all the time. Need to change this 10166 * to figure out whether the disk device is actually online or not. 10167 */ 10168 if (lun != NULL) 10169 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10170 lun->be_lun->lun_type; 10171 else 10172 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10173 10174 sp->page_code = SVPD_SCSI_PORTS; 10175 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10176 sp->page_length); 10177 pd = &sp->design[0]; 10178 10179 mtx_lock(&softc->ctl_lock); 10180 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10181 pg = 0; 10182 else 10183 pg = 1; 10184 for (g = 0; g < num_target_port_groups; g++) { 10185 STAILQ_FOREACH(port, &softc->port_list, links) { 10186 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10187 continue; 10188 if (lun != NULL && 10189 ctl_map_lun_back(port->targ_port, lun->lun) >= 10190 CTL_MAX_LUNS) 10191 continue; 10192 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10193 scsi_ulto2b(p, pd->relative_port_id); 10194 if (port->init_devid && g == pg) { 10195 iid_len = port->init_devid->len; 10196 memcpy(pd->initiator_transportid, 10197 port->init_devid->data, port->init_devid->len); 10198 } else 10199 iid_len = 0; 10200 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10201 pdc = (struct scsi_vpd_port_designation_cont *) 10202 (&pd->initiator_transportid[iid_len]); 10203 if (port->port_devid && g == pg) { 10204 id_len = port->port_devid->len; 10205 memcpy(pdc->target_port_descriptors, 10206 port->port_devid->data, port->port_devid->len); 10207 } else 10208 id_len = 0; 10209 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10210 pd = (struct scsi_vpd_port_designation *) 10211 ((uint8_t *)pdc->target_port_descriptors + id_len); 10212 } 10213 } 10214 mtx_unlock(&softc->ctl_lock); 10215 10216 ctsio->scsi_status = SCSI_STATUS_OK; 10217 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10218 ctsio->be_move_done = ctl_config_move_done; 10219 ctl_datamove((union ctl_io *)ctsio); 10220 10221 return (CTL_RETVAL_COMPLETE); 10222} 10223 10224static int 10225ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10226{ 10227 struct scsi_vpd_block_limits *bl_ptr; 10228 struct ctl_lun *lun; 10229 int bs; 10230 10231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10232 10233 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10234 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10235 ctsio->kern_sg_entries = 0; 10236 10237 if (sizeof(*bl_ptr) < alloc_len) { 10238 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10239 ctsio->kern_data_len = sizeof(*bl_ptr); 10240 ctsio->kern_total_len = sizeof(*bl_ptr); 10241 } else { 10242 ctsio->residual = 0; 10243 ctsio->kern_data_len = alloc_len; 10244 ctsio->kern_total_len = alloc_len; 10245 } 10246 ctsio->kern_data_resid = 0; 10247 ctsio->kern_rel_offset = 0; 10248 ctsio->kern_sg_entries = 0; 10249 10250 /* 10251 * The control device is always connected. The disk device, on the 10252 * other hand, may not be online all the time. Need to change this 10253 * to figure out whether the disk device is actually online or not. 10254 */ 10255 if (lun != NULL) 10256 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10257 lun->be_lun->lun_type; 10258 else 10259 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10260 10261 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10262 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10263 bl_ptr->max_cmp_write_len = 0xff; 10264 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10265 if (lun != NULL) { 10266 bs = lun->be_lun->blocksize; 10267 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10268 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10269 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10270 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10271 if (lun->be_lun->pblockexp != 0) { 10272 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10273 bl_ptr->opt_unmap_grain); 10274 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10275 bl_ptr->unmap_grain_align); 10276 } 10277 } 10278 } 10279 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10280 10281 ctsio->scsi_status = SCSI_STATUS_OK; 10282 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10283 ctsio->be_move_done = ctl_config_move_done; 10284 ctl_datamove((union ctl_io *)ctsio); 10285 10286 return (CTL_RETVAL_COMPLETE); 10287} 10288 10289static int 10290ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10291{ 10292 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10293 struct ctl_lun *lun; 10294 10295 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10296 10297 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10298 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10299 ctsio->kern_sg_entries = 0; 10300 10301 if (sizeof(*bdc_ptr) < alloc_len) { 10302 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10303 ctsio->kern_data_len = sizeof(*bdc_ptr); 10304 ctsio->kern_total_len = sizeof(*bdc_ptr); 10305 } else { 10306 ctsio->residual = 0; 10307 ctsio->kern_data_len = alloc_len; 10308 ctsio->kern_total_len = alloc_len; 10309 } 10310 ctsio->kern_data_resid = 0; 10311 ctsio->kern_rel_offset = 0; 10312 ctsio->kern_sg_entries = 0; 10313 10314 /* 10315 * The control device is always connected. The disk device, on the 10316 * other hand, may not be online all the time. Need to change this 10317 * to figure out whether the disk device is actually online or not. 10318 */ 10319 if (lun != NULL) 10320 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10321 lun->be_lun->lun_type; 10322 else 10323 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10324 bdc_ptr->page_code = SVPD_BDC; 10325 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10326 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10327 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10328 10329 ctsio->scsi_status = SCSI_STATUS_OK; 10330 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10331 ctsio->be_move_done = ctl_config_move_done; 10332 ctl_datamove((union ctl_io *)ctsio); 10333 10334 return (CTL_RETVAL_COMPLETE); 10335} 10336 10337static int 10338ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10339{ 10340 struct scsi_vpd_logical_block_prov *lbp_ptr; 10341 struct ctl_lun *lun; 10342 10343 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10344 10345 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10346 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10347 ctsio->kern_sg_entries = 0; 10348 10349 if (sizeof(*lbp_ptr) < alloc_len) { 10350 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10351 ctsio->kern_data_len = sizeof(*lbp_ptr); 10352 ctsio->kern_total_len = sizeof(*lbp_ptr); 10353 } else { 10354 ctsio->residual = 0; 10355 ctsio->kern_data_len = alloc_len; 10356 ctsio->kern_total_len = alloc_len; 10357 } 10358 ctsio->kern_data_resid = 0; 10359 ctsio->kern_rel_offset = 0; 10360 ctsio->kern_sg_entries = 0; 10361 10362 /* 10363 * The control device is always connected. The disk device, on the 10364 * other hand, may not be online all the time. Need to change this 10365 * to figure out whether the disk device is actually online or not. 10366 */ 10367 if (lun != NULL) 10368 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10369 lun->be_lun->lun_type; 10370 else 10371 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10372 10373 lbp_ptr->page_code = SVPD_LBP; 10374 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10375 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10376 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10377 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10378 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10379 } 10380 10381 ctsio->scsi_status = SCSI_STATUS_OK; 10382 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10383 ctsio->be_move_done = ctl_config_move_done; 10384 ctl_datamove((union ctl_io *)ctsio); 10385 10386 return (CTL_RETVAL_COMPLETE); 10387} 10388 10389static int 10390ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10391{ 10392 struct scsi_inquiry *cdb; 10393 struct ctl_lun *lun; 10394 int alloc_len, retval; 10395 10396 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10397 cdb = (struct scsi_inquiry *)ctsio->cdb; 10398 10399 retval = CTL_RETVAL_COMPLETE; 10400 10401 alloc_len = scsi_2btoul(cdb->length); 10402 10403 switch (cdb->page_code) { 10404 case SVPD_SUPPORTED_PAGES: 10405 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10406 break; 10407 case SVPD_UNIT_SERIAL_NUMBER: 10408 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10409 break; 10410 case SVPD_DEVICE_ID: 10411 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10412 break; 10413 case SVPD_MODE_PAGE_POLICY: 10414 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10415 break; 10416 case SVPD_SCSI_PORTS: 10417 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10418 break; 10419 case SVPD_SCSI_TPC: 10420 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10421 break; 10422 case SVPD_BLOCK_LIMITS: 10423 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10424 break; 10425 case SVPD_BDC: 10426 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10427 break; 10428 case SVPD_LBP: 10429 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10430 break; 10431 default: 10432 ctl_set_invalid_field(ctsio, 10433 /*sks_valid*/ 1, 10434 /*command*/ 1, 10435 /*field*/ 2, 10436 /*bit_valid*/ 0, 10437 /*bit*/ 0); 10438 ctl_done((union ctl_io *)ctsio); 10439 retval = CTL_RETVAL_COMPLETE; 10440 break; 10441 } 10442 10443 return (retval); 10444} 10445 10446static int 10447ctl_inquiry_std(struct ctl_scsiio *ctsio) 10448{ 10449 struct scsi_inquiry_data *inq_ptr; 10450 struct scsi_inquiry *cdb; 10451 struct ctl_softc *ctl_softc; 10452 struct ctl_lun *lun; 10453 char *val; 10454 uint32_t alloc_len; 10455 ctl_port_type port_type; 10456 10457 ctl_softc = control_softc; 10458 10459 /* 10460 * Figure out whether we're talking to a Fibre Channel port or not. 10461 * We treat the ioctl front end, and any SCSI adapters, as packetized 10462 * SCSI front ends. 10463 */ 10464 port_type = ctl_softc->ctl_ports[ 10465 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10466 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10467 port_type = CTL_PORT_SCSI; 10468 10469 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10470 cdb = (struct scsi_inquiry *)ctsio->cdb; 10471 alloc_len = scsi_2btoul(cdb->length); 10472 10473 /* 10474 * We malloc the full inquiry data size here and fill it 10475 * in. If the user only asks for less, we'll give him 10476 * that much. 10477 */ 10478 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10479 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10480 ctsio->kern_sg_entries = 0; 10481 ctsio->kern_data_resid = 0; 10482 ctsio->kern_rel_offset = 0; 10483 10484 if (sizeof(*inq_ptr) < alloc_len) { 10485 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10486 ctsio->kern_data_len = sizeof(*inq_ptr); 10487 ctsio->kern_total_len = sizeof(*inq_ptr); 10488 } else { 10489 ctsio->residual = 0; 10490 ctsio->kern_data_len = alloc_len; 10491 ctsio->kern_total_len = alloc_len; 10492 } 10493 10494 /* 10495 * If we have a LUN configured, report it as connected. Otherwise, 10496 * report that it is offline or no device is supported, depending 10497 * on the value of inquiry_pq_no_lun. 10498 * 10499 * According to the spec (SPC-4 r34), the peripheral qualifier 10500 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10501 * 10502 * "A peripheral device having the specified peripheral device type 10503 * is not connected to this logical unit. However, the device 10504 * server is capable of supporting the specified peripheral device 10505 * type on this logical unit." 10506 * 10507 * According to the same spec, the peripheral qualifier 10508 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10509 * 10510 * "The device server is not capable of supporting a peripheral 10511 * device on this logical unit. For this peripheral qualifier the 10512 * peripheral device type shall be set to 1Fh. All other peripheral 10513 * device type values are reserved for this peripheral qualifier." 10514 * 10515 * Given the text, it would seem that we probably want to report that 10516 * the LUN is offline here. There is no LUN connected, but we can 10517 * support a LUN at the given LUN number. 10518 * 10519 * In the real world, though, it sounds like things are a little 10520 * different: 10521 * 10522 * - Linux, when presented with a LUN with the offline peripheral 10523 * qualifier, will create an sg driver instance for it. So when 10524 * you attach it to CTL, you wind up with a ton of sg driver 10525 * instances. (One for every LUN that Linux bothered to probe.) 10526 * Linux does this despite the fact that it issues a REPORT LUNs 10527 * to LUN 0 to get the inventory of supported LUNs. 10528 * 10529 * - There is other anecdotal evidence (from Emulex folks) about 10530 * arrays that use the offline peripheral qualifier for LUNs that 10531 * are on the "passive" path in an active/passive array. 10532 * 10533 * So the solution is provide a hopefully reasonable default 10534 * (return bad/no LUN) and allow the user to change the behavior 10535 * with a tunable/sysctl variable. 10536 */ 10537 if (lun != NULL) 10538 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10539 lun->be_lun->lun_type; 10540 else if (ctl_softc->inquiry_pq_no_lun == 0) 10541 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10542 else 10543 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10544 10545 /* RMB in byte 2 is 0 */ 10546 inq_ptr->version = SCSI_REV_SPC4; 10547 10548 /* 10549 * According to SAM-3, even if a device only supports a single 10550 * level of LUN addressing, it should still set the HISUP bit: 10551 * 10552 * 4.9.1 Logical unit numbers overview 10553 * 10554 * All logical unit number formats described in this standard are 10555 * hierarchical in structure even when only a single level in that 10556 * hierarchy is used. The HISUP bit shall be set to one in the 10557 * standard INQUIRY data (see SPC-2) when any logical unit number 10558 * format described in this standard is used. Non-hierarchical 10559 * formats are outside the scope of this standard. 10560 * 10561 * Therefore we set the HiSup bit here. 10562 * 10563 * The reponse format is 2, per SPC-3. 10564 */ 10565 inq_ptr->response_format = SID_HiSup | 2; 10566 10567 inq_ptr->additional_length = 10568 offsetof(struct scsi_inquiry_data, vendor_specific1) - 10569 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10570 CTL_DEBUG_PRINT(("additional_length = %d\n", 10571 inq_ptr->additional_length)); 10572 10573 inq_ptr->spc3_flags = SPC3_SID_3PC; 10574 if (!ctl_is_single) 10575 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT; 10576 /* 16 bit addressing */ 10577 if (port_type == CTL_PORT_SCSI) 10578 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10579 /* XXX set the SID_MultiP bit here if we're actually going to 10580 respond on multiple ports */ 10581 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10582 10583 /* 16 bit data bus, synchronous transfers */ 10584 if (port_type == CTL_PORT_SCSI) 10585 inq_ptr->flags = SID_WBus16 | SID_Sync; 10586 /* 10587 * XXX KDM do we want to support tagged queueing on the control 10588 * device at all? 10589 */ 10590 if ((lun == NULL) 10591 || (lun->be_lun->lun_type != T_PROCESSOR)) 10592 inq_ptr->flags |= SID_CmdQue; 10593 /* 10594 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10595 * We have 8 bytes for the vendor name, and 16 bytes for the device 10596 * name and 4 bytes for the revision. 10597 */ 10598 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10599 "vendor")) == NULL) { 10600 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10601 } else { 10602 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10603 strncpy(inq_ptr->vendor, val, 10604 min(sizeof(inq_ptr->vendor), strlen(val))); 10605 } 10606 if (lun == NULL) { 10607 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10608 sizeof(inq_ptr->product)); 10609 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10610 switch (lun->be_lun->lun_type) { 10611 case T_DIRECT: 10612 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10613 sizeof(inq_ptr->product)); 10614 break; 10615 case T_PROCESSOR: 10616 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10617 sizeof(inq_ptr->product)); 10618 break; 10619 default: 10620 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10621 sizeof(inq_ptr->product)); 10622 break; 10623 } 10624 } else { 10625 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10626 strncpy(inq_ptr->product, val, 10627 min(sizeof(inq_ptr->product), strlen(val))); 10628 } 10629 10630 /* 10631 * XXX make this a macro somewhere so it automatically gets 10632 * incremented when we make changes. 10633 */ 10634 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10635 "revision")) == NULL) { 10636 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10637 } else { 10638 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10639 strncpy(inq_ptr->revision, val, 10640 min(sizeof(inq_ptr->revision), strlen(val))); 10641 } 10642 10643 /* 10644 * For parallel SCSI, we support double transition and single 10645 * transition clocking. We also support QAS (Quick Arbitration 10646 * and Selection) and Information Unit transfers on both the 10647 * control and array devices. 10648 */ 10649 if (port_type == CTL_PORT_SCSI) 10650 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10651 SID_SPI_IUS; 10652 10653 /* SAM-5 (no version claimed) */ 10654 scsi_ulto2b(0x00A0, inq_ptr->version1); 10655 /* SPC-4 (no version claimed) */ 10656 scsi_ulto2b(0x0460, inq_ptr->version2); 10657 if (port_type == CTL_PORT_FC) { 10658 /* FCP-2 ANSI INCITS.350:2003 */ 10659 scsi_ulto2b(0x0917, inq_ptr->version3); 10660 } else if (port_type == CTL_PORT_SCSI) { 10661 /* SPI-4 ANSI INCITS.362:200x */ 10662 scsi_ulto2b(0x0B56, inq_ptr->version3); 10663 } else if (port_type == CTL_PORT_ISCSI) { 10664 /* iSCSI (no version claimed) */ 10665 scsi_ulto2b(0x0960, inq_ptr->version3); 10666 } else if (port_type == CTL_PORT_SAS) { 10667 /* SAS (no version claimed) */ 10668 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10669 } 10670 10671 if (lun == NULL) { 10672 /* SBC-3 (no version claimed) */ 10673 scsi_ulto2b(0x04C0, inq_ptr->version4); 10674 } else { 10675 switch (lun->be_lun->lun_type) { 10676 case T_DIRECT: 10677 /* SBC-3 (no version claimed) */ 10678 scsi_ulto2b(0x04C0, inq_ptr->version4); 10679 break; 10680 case T_PROCESSOR: 10681 default: 10682 break; 10683 } 10684 } 10685 10686 ctsio->scsi_status = SCSI_STATUS_OK; 10687 if (ctsio->kern_data_len > 0) { 10688 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10689 ctsio->be_move_done = ctl_config_move_done; 10690 ctl_datamove((union ctl_io *)ctsio); 10691 } else { 10692 ctsio->io_hdr.status = CTL_SUCCESS; 10693 ctl_done((union ctl_io *)ctsio); 10694 } 10695 10696 return (CTL_RETVAL_COMPLETE); 10697} 10698 10699int 10700ctl_inquiry(struct ctl_scsiio *ctsio) 10701{ 10702 struct scsi_inquiry *cdb; 10703 int retval; 10704 10705 cdb = (struct scsi_inquiry *)ctsio->cdb; 10706 10707 retval = 0; 10708 10709 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10710 10711 /* 10712 * Right now, we don't support the CmdDt inquiry information. 10713 * This would be nice to support in the future. When we do 10714 * support it, we should change this test so that it checks to make 10715 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10716 */ 10717#ifdef notyet 10718 if (((cdb->byte2 & SI_EVPD) 10719 && (cdb->byte2 & SI_CMDDT))) 10720#endif 10721 if (cdb->byte2 & SI_CMDDT) { 10722 /* 10723 * Point to the SI_CMDDT bit. We might change this 10724 * when we support SI_CMDDT, but since both bits would be 10725 * "wrong", this should probably just stay as-is then. 10726 */ 10727 ctl_set_invalid_field(ctsio, 10728 /*sks_valid*/ 1, 10729 /*command*/ 1, 10730 /*field*/ 1, 10731 /*bit_valid*/ 1, 10732 /*bit*/ 1); 10733 ctl_done((union ctl_io *)ctsio); 10734 return (CTL_RETVAL_COMPLETE); 10735 } 10736 if (cdb->byte2 & SI_EVPD) 10737 retval = ctl_inquiry_evpd(ctsio); 10738#ifdef notyet 10739 else if (cdb->byte2 & SI_CMDDT) 10740 retval = ctl_inquiry_cmddt(ctsio); 10741#endif 10742 else 10743 retval = ctl_inquiry_std(ctsio); 10744 10745 return (retval); 10746} 10747 10748/* 10749 * For known CDB types, parse the LBA and length. 10750 */ 10751static int 10752ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10753{ 10754 if (io->io_hdr.io_type != CTL_IO_SCSI) 10755 return (1); 10756 10757 switch (io->scsiio.cdb[0]) { 10758 case COMPARE_AND_WRITE: { 10759 struct scsi_compare_and_write *cdb; 10760 10761 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10762 10763 *lba = scsi_8btou64(cdb->addr); 10764 *len = cdb->length; 10765 break; 10766 } 10767 case READ_6: 10768 case WRITE_6: { 10769 struct scsi_rw_6 *cdb; 10770 10771 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10772 10773 *lba = scsi_3btoul(cdb->addr); 10774 /* only 5 bits are valid in the most significant address byte */ 10775 *lba &= 0x1fffff; 10776 *len = cdb->length; 10777 break; 10778 } 10779 case READ_10: 10780 case WRITE_10: { 10781 struct scsi_rw_10 *cdb; 10782 10783 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10784 10785 *lba = scsi_4btoul(cdb->addr); 10786 *len = scsi_2btoul(cdb->length); 10787 break; 10788 } 10789 case WRITE_VERIFY_10: { 10790 struct scsi_write_verify_10 *cdb; 10791 10792 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10793 10794 *lba = scsi_4btoul(cdb->addr); 10795 *len = scsi_2btoul(cdb->length); 10796 break; 10797 } 10798 case READ_12: 10799 case WRITE_12: { 10800 struct scsi_rw_12 *cdb; 10801 10802 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10803 10804 *lba = scsi_4btoul(cdb->addr); 10805 *len = scsi_4btoul(cdb->length); 10806 break; 10807 } 10808 case WRITE_VERIFY_12: { 10809 struct scsi_write_verify_12 *cdb; 10810 10811 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10812 10813 *lba = scsi_4btoul(cdb->addr); 10814 *len = scsi_4btoul(cdb->length); 10815 break; 10816 } 10817 case READ_16: 10818 case WRITE_16: { 10819 struct scsi_rw_16 *cdb; 10820 10821 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10822 10823 *lba = scsi_8btou64(cdb->addr); 10824 *len = scsi_4btoul(cdb->length); 10825 break; 10826 } 10827 case WRITE_VERIFY_16: { 10828 struct scsi_write_verify_16 *cdb; 10829 10830 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10831 10832 10833 *lba = scsi_8btou64(cdb->addr); 10834 *len = scsi_4btoul(cdb->length); 10835 break; 10836 } 10837 case WRITE_SAME_10: { 10838 struct scsi_write_same_10 *cdb; 10839 10840 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10841 10842 *lba = scsi_4btoul(cdb->addr); 10843 *len = scsi_2btoul(cdb->length); 10844 break; 10845 } 10846 case WRITE_SAME_16: { 10847 struct scsi_write_same_16 *cdb; 10848 10849 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10850 10851 *lba = scsi_8btou64(cdb->addr); 10852 *len = scsi_4btoul(cdb->length); 10853 break; 10854 } 10855 case VERIFY_10: { 10856 struct scsi_verify_10 *cdb; 10857 10858 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10859 10860 *lba = scsi_4btoul(cdb->addr); 10861 *len = scsi_2btoul(cdb->length); 10862 break; 10863 } 10864 case VERIFY_12: { 10865 struct scsi_verify_12 *cdb; 10866 10867 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10868 10869 *lba = scsi_4btoul(cdb->addr); 10870 *len = scsi_4btoul(cdb->length); 10871 break; 10872 } 10873 case VERIFY_16: { 10874 struct scsi_verify_16 *cdb; 10875 10876 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10877 10878 *lba = scsi_8btou64(cdb->addr); 10879 *len = scsi_4btoul(cdb->length); 10880 break; 10881 } 10882 default: 10883 return (1); 10884 break; /* NOTREACHED */ 10885 } 10886 10887 return (0); 10888} 10889 10890static ctl_action 10891ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10892{ 10893 uint64_t endlba1, endlba2; 10894 10895 endlba1 = lba1 + len1 - 1; 10896 endlba2 = lba2 + len2 - 1; 10897 10898 if ((endlba1 < lba2) 10899 || (endlba2 < lba1)) 10900 return (CTL_ACTION_PASS); 10901 else 10902 return (CTL_ACTION_BLOCK); 10903} 10904 10905static ctl_action 10906ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10907{ 10908 uint64_t lba1, lba2; 10909 uint32_t len1, len2; 10910 int retval; 10911 10912 retval = ctl_get_lba_len(io1, &lba1, &len1); 10913 if (retval != 0) 10914 return (CTL_ACTION_ERROR); 10915 10916 retval = ctl_get_lba_len(io2, &lba2, &len2); 10917 if (retval != 0) 10918 return (CTL_ACTION_ERROR); 10919 10920 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10921} 10922 10923static ctl_action 10924ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10925{ 10926 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10927 ctl_serialize_action *serialize_row; 10928 10929 /* 10930 * The initiator attempted multiple untagged commands at the same 10931 * time. Can't do that. 10932 */ 10933 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10934 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10935 && ((pending_io->io_hdr.nexus.targ_port == 10936 ooa_io->io_hdr.nexus.targ_port) 10937 && (pending_io->io_hdr.nexus.initid.id == 10938 ooa_io->io_hdr.nexus.initid.id)) 10939 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10940 return (CTL_ACTION_OVERLAP); 10941 10942 /* 10943 * The initiator attempted to send multiple tagged commands with 10944 * the same ID. (It's fine if different initiators have the same 10945 * tag ID.) 10946 * 10947 * Even if all of those conditions are true, we don't kill the I/O 10948 * if the command ahead of us has been aborted. We won't end up 10949 * sending it to the FETD, and it's perfectly legal to resend a 10950 * command with the same tag number as long as the previous 10951 * instance of this tag number has been aborted somehow. 10952 */ 10953 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10954 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10955 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10956 && ((pending_io->io_hdr.nexus.targ_port == 10957 ooa_io->io_hdr.nexus.targ_port) 10958 && (pending_io->io_hdr.nexus.initid.id == 10959 ooa_io->io_hdr.nexus.initid.id)) 10960 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10961 return (CTL_ACTION_OVERLAP_TAG); 10962 10963 /* 10964 * If we get a head of queue tag, SAM-3 says that we should 10965 * immediately execute it. 10966 * 10967 * What happens if this command would normally block for some other 10968 * reason? e.g. a request sense with a head of queue tag 10969 * immediately after a write. Normally that would block, but this 10970 * will result in its getting executed immediately... 10971 * 10972 * We currently return "pass" instead of "skip", so we'll end up 10973 * going through the rest of the queue to check for overlapped tags. 10974 * 10975 * XXX KDM check for other types of blockage first?? 10976 */ 10977 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10978 return (CTL_ACTION_PASS); 10979 10980 /* 10981 * Ordered tags have to block until all items ahead of them 10982 * have completed. If we get called with an ordered tag, we always 10983 * block, if something else is ahead of us in the queue. 10984 */ 10985 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10986 return (CTL_ACTION_BLOCK); 10987 10988 /* 10989 * Simple tags get blocked until all head of queue and ordered tags 10990 * ahead of them have completed. I'm lumping untagged commands in 10991 * with simple tags here. XXX KDM is that the right thing to do? 10992 */ 10993 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10994 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10995 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10996 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10997 return (CTL_ACTION_BLOCK); 10998 10999 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 11000 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 11001 11002 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11003 11004 switch (serialize_row[pending_entry->seridx]) { 11005 case CTL_SER_BLOCK: 11006 return (CTL_ACTION_BLOCK); 11007 break; /* NOTREACHED */ 11008 case CTL_SER_EXTENT: 11009 return (ctl_extent_check(pending_io, ooa_io)); 11010 break; /* NOTREACHED */ 11011 case CTL_SER_PASS: 11012 return (CTL_ACTION_PASS); 11013 break; /* NOTREACHED */ 11014 case CTL_SER_SKIP: 11015 return (CTL_ACTION_SKIP); 11016 break; 11017 default: 11018 panic("invalid serialization value %d", 11019 serialize_row[pending_entry->seridx]); 11020 break; /* NOTREACHED */ 11021 } 11022 11023 return (CTL_ACTION_ERROR); 11024} 11025 11026/* 11027 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11028 * Assumptions: 11029 * - pending_io is generally either incoming, or on the blocked queue 11030 * - starting I/O is the I/O we want to start the check with. 11031 */ 11032static ctl_action 11033ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11034 union ctl_io *starting_io) 11035{ 11036 union ctl_io *ooa_io; 11037 ctl_action action; 11038 11039 mtx_assert(&lun->lun_lock, MA_OWNED); 11040 11041 /* 11042 * Run back along the OOA queue, starting with the current 11043 * blocked I/O and going through every I/O before it on the 11044 * queue. If starting_io is NULL, we'll just end up returning 11045 * CTL_ACTION_PASS. 11046 */ 11047 for (ooa_io = starting_io; ooa_io != NULL; 11048 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11049 ooa_links)){ 11050 11051 /* 11052 * This routine just checks to see whether 11053 * cur_blocked is blocked by ooa_io, which is ahead 11054 * of it in the queue. It doesn't queue/dequeue 11055 * cur_blocked. 11056 */ 11057 action = ctl_check_for_blockage(pending_io, ooa_io); 11058 switch (action) { 11059 case CTL_ACTION_BLOCK: 11060 case CTL_ACTION_OVERLAP: 11061 case CTL_ACTION_OVERLAP_TAG: 11062 case CTL_ACTION_SKIP: 11063 case CTL_ACTION_ERROR: 11064 return (action); 11065 break; /* NOTREACHED */ 11066 case CTL_ACTION_PASS: 11067 break; 11068 default: 11069 panic("invalid action %d", action); 11070 break; /* NOTREACHED */ 11071 } 11072 } 11073 11074 return (CTL_ACTION_PASS); 11075} 11076 11077/* 11078 * Assumptions: 11079 * - An I/O has just completed, and has been removed from the per-LUN OOA 11080 * queue, so some items on the blocked queue may now be unblocked. 11081 */ 11082static int 11083ctl_check_blocked(struct ctl_lun *lun) 11084{ 11085 union ctl_io *cur_blocked, *next_blocked; 11086 11087 mtx_assert(&lun->lun_lock, MA_OWNED); 11088 11089 /* 11090 * Run forward from the head of the blocked queue, checking each 11091 * entry against the I/Os prior to it on the OOA queue to see if 11092 * there is still any blockage. 11093 * 11094 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11095 * with our removing a variable on it while it is traversing the 11096 * list. 11097 */ 11098 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11099 cur_blocked != NULL; cur_blocked = next_blocked) { 11100 union ctl_io *prev_ooa; 11101 ctl_action action; 11102 11103 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11104 blocked_links); 11105 11106 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11107 ctl_ooaq, ooa_links); 11108 11109 /* 11110 * If cur_blocked happens to be the first item in the OOA 11111 * queue now, prev_ooa will be NULL, and the action 11112 * returned will just be CTL_ACTION_PASS. 11113 */ 11114 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11115 11116 switch (action) { 11117 case CTL_ACTION_BLOCK: 11118 /* Nothing to do here, still blocked */ 11119 break; 11120 case CTL_ACTION_OVERLAP: 11121 case CTL_ACTION_OVERLAP_TAG: 11122 /* 11123 * This shouldn't happen! In theory we've already 11124 * checked this command for overlap... 11125 */ 11126 break; 11127 case CTL_ACTION_PASS: 11128 case CTL_ACTION_SKIP: { 11129 struct ctl_softc *softc; 11130 const struct ctl_cmd_entry *entry; 11131 uint32_t initidx; 11132 int isc_retval; 11133 11134 /* 11135 * The skip case shouldn't happen, this transaction 11136 * should have never made it onto the blocked queue. 11137 */ 11138 /* 11139 * This I/O is no longer blocked, we can remove it 11140 * from the blocked queue. Since this is a TAILQ 11141 * (doubly linked list), we can do O(1) removals 11142 * from any place on the list. 11143 */ 11144 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11145 blocked_links); 11146 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11147 11148 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11149 /* 11150 * Need to send IO back to original side to 11151 * run 11152 */ 11153 union ctl_ha_msg msg_info; 11154 11155 msg_info.hdr.original_sc = 11156 cur_blocked->io_hdr.original_sc; 11157 msg_info.hdr.serializing_sc = cur_blocked; 11158 msg_info.hdr.msg_type = CTL_MSG_R2R; 11159 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11160 &msg_info, sizeof(msg_info), 0)) > 11161 CTL_HA_STATUS_SUCCESS) { 11162 printf("CTL:Check Blocked error from " 11163 "ctl_ha_msg_send %d\n", 11164 isc_retval); 11165 } 11166 break; 11167 } 11168 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 11169 softc = control_softc; 11170 11171 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11172 11173 /* 11174 * Check this I/O for LUN state changes that may 11175 * have happened while this command was blocked. 11176 * The LUN state may have been changed by a command 11177 * ahead of us in the queue, so we need to re-check 11178 * for any states that can be caused by SCSI 11179 * commands. 11180 */ 11181 if (ctl_scsiio_lun_check(softc, lun, entry, 11182 &cur_blocked->scsiio) == 0) { 11183 cur_blocked->io_hdr.flags |= 11184 CTL_FLAG_IS_WAS_ON_RTR; 11185 ctl_enqueue_rtr(cur_blocked); 11186 } else 11187 ctl_done(cur_blocked); 11188 break; 11189 } 11190 default: 11191 /* 11192 * This probably shouldn't happen -- we shouldn't 11193 * get CTL_ACTION_ERROR, or anything else. 11194 */ 11195 break; 11196 } 11197 } 11198 11199 return (CTL_RETVAL_COMPLETE); 11200} 11201 11202/* 11203 * This routine (with one exception) checks LUN flags that can be set by 11204 * commands ahead of us in the OOA queue. These flags have to be checked 11205 * when a command initially comes in, and when we pull a command off the 11206 * blocked queue and are preparing to execute it. The reason we have to 11207 * check these flags for commands on the blocked queue is that the LUN 11208 * state may have been changed by a command ahead of us while we're on the 11209 * blocked queue. 11210 * 11211 * Ordering is somewhat important with these checks, so please pay 11212 * careful attention to the placement of any new checks. 11213 */ 11214static int 11215ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11216 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11217{ 11218 int retval; 11219 11220 retval = 0; 11221 11222 mtx_assert(&lun->lun_lock, MA_OWNED); 11223 11224 /* 11225 * If this shelf is a secondary shelf controller, we have to reject 11226 * any media access commands. 11227 */ 11228#if 0 11229 /* No longer needed for HA */ 11230 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11231 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11232 ctl_set_lun_standby(ctsio); 11233 retval = 1; 11234 goto bailout; 11235 } 11236#endif 11237 11238 /* 11239 * Check for a reservation conflict. If this command isn't allowed 11240 * even on reserved LUNs, and if this initiator isn't the one who 11241 * reserved us, reject the command with a reservation conflict. 11242 */ 11243 if ((lun->flags & CTL_LUN_RESERVED) 11244 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11245 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11246 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11247 || (ctsio->io_hdr.nexus.targ_target.id != 11248 lun->rsv_nexus.targ_target.id)) { 11249 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11250 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11251 retval = 1; 11252 goto bailout; 11253 } 11254 } 11255 11256 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11257 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11258 uint32_t residx; 11259 11260 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11261 /* 11262 * if we aren't registered or it's a res holder type 11263 * reservation and this isn't the res holder then set a 11264 * conflict. 11265 * NOTE: Commands which might be allowed on write exclusive 11266 * type reservations are checked in the particular command 11267 * for a conflict. Read and SSU are the only ones. 11268 */ 11269 if (!lun->per_res[residx].registered 11270 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11271 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11272 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11273 retval = 1; 11274 goto bailout; 11275 } 11276 11277 } 11278 11279 if ((lun->flags & CTL_LUN_OFFLINE) 11280 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11281 ctl_set_lun_not_ready(ctsio); 11282 retval = 1; 11283 goto bailout; 11284 } 11285 11286 /* 11287 * If the LUN is stopped, see if this particular command is allowed 11288 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11289 */ 11290 if ((lun->flags & CTL_LUN_STOPPED) 11291 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11292 /* "Logical unit not ready, initializing cmd. required" */ 11293 ctl_set_lun_stopped(ctsio); 11294 retval = 1; 11295 goto bailout; 11296 } 11297 11298 if ((lun->flags & CTL_LUN_INOPERABLE) 11299 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11300 /* "Medium format corrupted" */ 11301 ctl_set_medium_format_corrupted(ctsio); 11302 retval = 1; 11303 goto bailout; 11304 } 11305 11306bailout: 11307 return (retval); 11308 11309} 11310 11311static void 11312ctl_failover_io(union ctl_io *io, int have_lock) 11313{ 11314 ctl_set_busy(&io->scsiio); 11315 ctl_done(io); 11316} 11317 11318static void 11319ctl_failover(void) 11320{ 11321 struct ctl_lun *lun; 11322 struct ctl_softc *ctl_softc; 11323 union ctl_io *next_io, *pending_io; 11324 union ctl_io *io; 11325 int lun_idx; 11326 int i; 11327 11328 ctl_softc = control_softc; 11329 11330 mtx_lock(&ctl_softc->ctl_lock); 11331 /* 11332 * Remove any cmds from the other SC from the rtr queue. These 11333 * will obviously only be for LUNs for which we're the primary. 11334 * We can't send status or get/send data for these commands. 11335 * Since they haven't been executed yet, we can just remove them. 11336 * We'll either abort them or delete them below, depending on 11337 * which HA mode we're in. 11338 */ 11339#ifdef notyet 11340 mtx_lock(&ctl_softc->queue_lock); 11341 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11342 io != NULL; io = next_io) { 11343 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11344 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11345 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11346 ctl_io_hdr, links); 11347 } 11348 mtx_unlock(&ctl_softc->queue_lock); 11349#endif 11350 11351 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11352 lun = ctl_softc->ctl_luns[lun_idx]; 11353 if (lun==NULL) 11354 continue; 11355 11356 /* 11357 * Processor LUNs are primary on both sides. 11358 * XXX will this always be true? 11359 */ 11360 if (lun->be_lun->lun_type == T_PROCESSOR) 11361 continue; 11362 11363 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11364 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11365 printf("FAILOVER: primary lun %d\n", lun_idx); 11366 /* 11367 * Remove all commands from the other SC. First from the 11368 * blocked queue then from the ooa queue. Once we have 11369 * removed them. Call ctl_check_blocked to see if there 11370 * is anything that can run. 11371 */ 11372 for (io = (union ctl_io *)TAILQ_FIRST( 11373 &lun->blocked_queue); io != NULL; io = next_io) { 11374 11375 next_io = (union ctl_io *)TAILQ_NEXT( 11376 &io->io_hdr, blocked_links); 11377 11378 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11379 TAILQ_REMOVE(&lun->blocked_queue, 11380 &io->io_hdr,blocked_links); 11381 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11382 TAILQ_REMOVE(&lun->ooa_queue, 11383 &io->io_hdr, ooa_links); 11384 11385 ctl_free_io(io); 11386 } 11387 } 11388 11389 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11390 io != NULL; io = next_io) { 11391 11392 next_io = (union ctl_io *)TAILQ_NEXT( 11393 &io->io_hdr, ooa_links); 11394 11395 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11396 11397 TAILQ_REMOVE(&lun->ooa_queue, 11398 &io->io_hdr, 11399 ooa_links); 11400 11401 ctl_free_io(io); 11402 } 11403 } 11404 ctl_check_blocked(lun); 11405 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11406 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11407 11408 printf("FAILOVER: primary lun %d\n", lun_idx); 11409 /* 11410 * Abort all commands from the other SC. We can't 11411 * send status back for them now. These should get 11412 * cleaned up when they are completed or come out 11413 * for a datamove operation. 11414 */ 11415 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11416 io != NULL; io = next_io) { 11417 next_io = (union ctl_io *)TAILQ_NEXT( 11418 &io->io_hdr, ooa_links); 11419 11420 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11421 io->io_hdr.flags |= CTL_FLAG_ABORT; 11422 } 11423 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11424 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11425 11426 printf("FAILOVER: secondary lun %d\n", lun_idx); 11427 11428 lun->flags |= CTL_LUN_PRIMARY_SC; 11429 11430 /* 11431 * We send all I/O that was sent to this controller 11432 * and redirected to the other side back with 11433 * busy status, and have the initiator retry it. 11434 * Figuring out how much data has been transferred, 11435 * etc. and picking up where we left off would be 11436 * very tricky. 11437 * 11438 * XXX KDM need to remove I/O from the blocked 11439 * queue as well! 11440 */ 11441 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11442 &lun->ooa_queue); pending_io != NULL; 11443 pending_io = next_io) { 11444 11445 next_io = (union ctl_io *)TAILQ_NEXT( 11446 &pending_io->io_hdr, ooa_links); 11447 11448 pending_io->io_hdr.flags &= 11449 ~CTL_FLAG_SENT_2OTHER_SC; 11450 11451 if (pending_io->io_hdr.flags & 11452 CTL_FLAG_IO_ACTIVE) { 11453 pending_io->io_hdr.flags |= 11454 CTL_FLAG_FAILOVER; 11455 } else { 11456 ctl_set_busy(&pending_io->scsiio); 11457 ctl_done(pending_io); 11458 } 11459 } 11460 11461 /* 11462 * Build Unit Attention 11463 */ 11464 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11465 lun->pending_ua[i] |= 11466 CTL_UA_ASYM_ACC_CHANGE; 11467 } 11468 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11469 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11470 printf("FAILOVER: secondary lun %d\n", lun_idx); 11471 /* 11472 * if the first io on the OOA is not on the RtR queue 11473 * add it. 11474 */ 11475 lun->flags |= CTL_LUN_PRIMARY_SC; 11476 11477 pending_io = (union ctl_io *)TAILQ_FIRST( 11478 &lun->ooa_queue); 11479 if (pending_io==NULL) { 11480 printf("Nothing on OOA queue\n"); 11481 continue; 11482 } 11483 11484 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11485 if ((pending_io->io_hdr.flags & 11486 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11487 pending_io->io_hdr.flags |= 11488 CTL_FLAG_IS_WAS_ON_RTR; 11489 ctl_enqueue_rtr(pending_io); 11490 } 11491#if 0 11492 else 11493 { 11494 printf("Tag 0x%04x is running\n", 11495 pending_io->scsiio.tag_num); 11496 } 11497#endif 11498 11499 next_io = (union ctl_io *)TAILQ_NEXT( 11500 &pending_io->io_hdr, ooa_links); 11501 for (pending_io=next_io; pending_io != NULL; 11502 pending_io = next_io) { 11503 pending_io->io_hdr.flags &= 11504 ~CTL_FLAG_SENT_2OTHER_SC; 11505 next_io = (union ctl_io *)TAILQ_NEXT( 11506 &pending_io->io_hdr, ooa_links); 11507 if (pending_io->io_hdr.flags & 11508 CTL_FLAG_IS_WAS_ON_RTR) { 11509#if 0 11510 printf("Tag 0x%04x is running\n", 11511 pending_io->scsiio.tag_num); 11512#endif 11513 continue; 11514 } 11515 11516 switch (ctl_check_ooa(lun, pending_io, 11517 (union ctl_io *)TAILQ_PREV( 11518 &pending_io->io_hdr, ctl_ooaq, 11519 ooa_links))) { 11520 11521 case CTL_ACTION_BLOCK: 11522 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11523 &pending_io->io_hdr, 11524 blocked_links); 11525 pending_io->io_hdr.flags |= 11526 CTL_FLAG_BLOCKED; 11527 break; 11528 case CTL_ACTION_PASS: 11529 case CTL_ACTION_SKIP: 11530 pending_io->io_hdr.flags |= 11531 CTL_FLAG_IS_WAS_ON_RTR; 11532 ctl_enqueue_rtr(pending_io); 11533 break; 11534 case CTL_ACTION_OVERLAP: 11535 ctl_set_overlapped_cmd( 11536 (struct ctl_scsiio *)pending_io); 11537 ctl_done(pending_io); 11538 break; 11539 case CTL_ACTION_OVERLAP_TAG: 11540 ctl_set_overlapped_tag( 11541 (struct ctl_scsiio *)pending_io, 11542 pending_io->scsiio.tag_num & 0xff); 11543 ctl_done(pending_io); 11544 break; 11545 case CTL_ACTION_ERROR: 11546 default: 11547 ctl_set_internal_failure( 11548 (struct ctl_scsiio *)pending_io, 11549 0, // sks_valid 11550 0); //retry count 11551 ctl_done(pending_io); 11552 break; 11553 } 11554 } 11555 11556 /* 11557 * Build Unit Attention 11558 */ 11559 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11560 lun->pending_ua[i] |= 11561 CTL_UA_ASYM_ACC_CHANGE; 11562 } 11563 } else { 11564 panic("Unhandled HA mode failover, LUN flags = %#x, " 11565 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11566 } 11567 } 11568 ctl_pause_rtr = 0; 11569 mtx_unlock(&ctl_softc->ctl_lock); 11570} 11571 11572static int 11573ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11574{ 11575 struct ctl_lun *lun; 11576 const struct ctl_cmd_entry *entry; 11577 uint32_t initidx, targ_lun; 11578 int retval; 11579 11580 retval = 0; 11581 11582 lun = NULL; 11583 11584 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11585 if ((targ_lun < CTL_MAX_LUNS) 11586 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11587 lun = ctl_softc->ctl_luns[targ_lun]; 11588 /* 11589 * If the LUN is invalid, pretend that it doesn't exist. 11590 * It will go away as soon as all pending I/O has been 11591 * completed. 11592 */ 11593 if (lun->flags & CTL_LUN_DISABLED) { 11594 lun = NULL; 11595 } else { 11596 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11597 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11598 lun->be_lun; 11599 if (lun->be_lun->lun_type == T_PROCESSOR) { 11600 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11601 } 11602 11603 /* 11604 * Every I/O goes into the OOA queue for a 11605 * particular LUN, and stays there until completion. 11606 */ 11607 mtx_lock(&lun->lun_lock); 11608 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11609 ooa_links); 11610 } 11611 } else { 11612 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11613 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11614 } 11615 11616 /* Get command entry and return error if it is unsuppotyed. */ 11617 entry = ctl_validate_command(ctsio); 11618 if (entry == NULL) { 11619 if (lun) 11620 mtx_unlock(&lun->lun_lock); 11621 return (retval); 11622 } 11623 11624 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11625 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11626 11627 /* 11628 * Check to see whether we can send this command to LUNs that don't 11629 * exist. This should pretty much only be the case for inquiry 11630 * and request sense. Further checks, below, really require having 11631 * a LUN, so we can't really check the command anymore. Just put 11632 * it on the rtr queue. 11633 */ 11634 if (lun == NULL) { 11635 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11636 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11637 ctl_enqueue_rtr((union ctl_io *)ctsio); 11638 return (retval); 11639 } 11640 11641 ctl_set_unsupported_lun(ctsio); 11642 ctl_done((union ctl_io *)ctsio); 11643 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11644 return (retval); 11645 } else { 11646 /* 11647 * Make sure we support this particular command on this LUN. 11648 * e.g., we don't support writes to the control LUN. 11649 */ 11650 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11651 mtx_unlock(&lun->lun_lock); 11652 ctl_set_invalid_opcode(ctsio); 11653 ctl_done((union ctl_io *)ctsio); 11654 return (retval); 11655 } 11656 } 11657 11658 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11659 11660#ifdef CTL_WITH_CA 11661 /* 11662 * If we've got a request sense, it'll clear the contingent 11663 * allegiance condition. Otherwise, if we have a CA condition for 11664 * this initiator, clear it, because it sent down a command other 11665 * than request sense. 11666 */ 11667 if ((ctsio->cdb[0] != REQUEST_SENSE) 11668 && (ctl_is_set(lun->have_ca, initidx))) 11669 ctl_clear_mask(lun->have_ca, initidx); 11670#endif 11671 11672 /* 11673 * If the command has this flag set, it handles its own unit 11674 * attention reporting, we shouldn't do anything. Otherwise we 11675 * check for any pending unit attentions, and send them back to the 11676 * initiator. We only do this when a command initially comes in, 11677 * not when we pull it off the blocked queue. 11678 * 11679 * According to SAM-3, section 5.3.2, the order that things get 11680 * presented back to the host is basically unit attentions caused 11681 * by some sort of reset event, busy status, reservation conflicts 11682 * or task set full, and finally any other status. 11683 * 11684 * One issue here is that some of the unit attentions we report 11685 * don't fall into the "reset" category (e.g. "reported luns data 11686 * has changed"). So reporting it here, before the reservation 11687 * check, may be technically wrong. I guess the only thing to do 11688 * would be to check for and report the reset events here, and then 11689 * check for the other unit attention types after we check for a 11690 * reservation conflict. 11691 * 11692 * XXX KDM need to fix this 11693 */ 11694 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11695 ctl_ua_type ua_type; 11696 11697 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11698 scsi_sense_data_type sense_format; 11699 11700 if (lun != NULL) 11701 sense_format = (lun->flags & 11702 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11703 SSD_TYPE_FIXED; 11704 else 11705 sense_format = SSD_TYPE_FIXED; 11706 11707 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11708 &ctsio->sense_data, sense_format); 11709 if (ua_type != CTL_UA_NONE) { 11710 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11711 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11712 CTL_AUTOSENSE; 11713 ctsio->sense_len = SSD_FULL_SIZE; 11714 mtx_unlock(&lun->lun_lock); 11715 ctl_done((union ctl_io *)ctsio); 11716 return (retval); 11717 } 11718 } 11719 } 11720 11721 11722 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11723 mtx_unlock(&lun->lun_lock); 11724 ctl_done((union ctl_io *)ctsio); 11725 return (retval); 11726 } 11727 11728 /* 11729 * XXX CHD this is where we want to send IO to other side if 11730 * this LUN is secondary on this SC. We will need to make a copy 11731 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11732 * the copy we send as FROM_OTHER. 11733 * We also need to stuff the address of the original IO so we can 11734 * find it easily. Something similar will need be done on the other 11735 * side so when we are done we can find the copy. 11736 */ 11737 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11738 union ctl_ha_msg msg_info; 11739 int isc_retval; 11740 11741 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11742 11743 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11744 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11745#if 0 11746 printf("1. ctsio %p\n", ctsio); 11747#endif 11748 msg_info.hdr.serializing_sc = NULL; 11749 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11750 msg_info.scsi.tag_num = ctsio->tag_num; 11751 msg_info.scsi.tag_type = ctsio->tag_type; 11752 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11753 11754 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11755 11756 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11757 (void *)&msg_info, sizeof(msg_info), 0)) > 11758 CTL_HA_STATUS_SUCCESS) { 11759 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11760 isc_retval); 11761 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11762 } else { 11763#if 0 11764 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11765#endif 11766 } 11767 11768 /* 11769 * XXX KDM this I/O is off the incoming queue, but hasn't 11770 * been inserted on any other queue. We may need to come 11771 * up with a holding queue while we wait for serialization 11772 * so that we have an idea of what we're waiting for from 11773 * the other side. 11774 */ 11775 mtx_unlock(&lun->lun_lock); 11776 return (retval); 11777 } 11778 11779 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11780 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11781 ctl_ooaq, ooa_links))) { 11782 case CTL_ACTION_BLOCK: 11783 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11784 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11785 blocked_links); 11786 mtx_unlock(&lun->lun_lock); 11787 return (retval); 11788 case CTL_ACTION_PASS: 11789 case CTL_ACTION_SKIP: 11790 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11791 mtx_unlock(&lun->lun_lock); 11792 ctl_enqueue_rtr((union ctl_io *)ctsio); 11793 break; 11794 case CTL_ACTION_OVERLAP: 11795 mtx_unlock(&lun->lun_lock); 11796 ctl_set_overlapped_cmd(ctsio); 11797 ctl_done((union ctl_io *)ctsio); 11798 break; 11799 case CTL_ACTION_OVERLAP_TAG: 11800 mtx_unlock(&lun->lun_lock); 11801 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11802 ctl_done((union ctl_io *)ctsio); 11803 break; 11804 case CTL_ACTION_ERROR: 11805 default: 11806 mtx_unlock(&lun->lun_lock); 11807 ctl_set_internal_failure(ctsio, 11808 /*sks_valid*/ 0, 11809 /*retry_count*/ 0); 11810 ctl_done((union ctl_io *)ctsio); 11811 break; 11812 } 11813 return (retval); 11814} 11815 11816const struct ctl_cmd_entry * 11817ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11818{ 11819 const struct ctl_cmd_entry *entry; 11820 int service_action; 11821 11822 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11823 if (entry->flags & CTL_CMD_FLAG_SA5) { 11824 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11825 entry = &((const struct ctl_cmd_entry *) 11826 entry->execute)[service_action]; 11827 } 11828 return (entry); 11829} 11830 11831const struct ctl_cmd_entry * 11832ctl_validate_command(struct ctl_scsiio *ctsio) 11833{ 11834 const struct ctl_cmd_entry *entry; 11835 int i; 11836 uint8_t diff; 11837 11838 entry = ctl_get_cmd_entry(ctsio); 11839 if (entry->execute == NULL) { 11840 ctl_set_invalid_opcode(ctsio); 11841 ctl_done((union ctl_io *)ctsio); 11842 return (NULL); 11843 } 11844 KASSERT(entry->length > 0, 11845 ("Not defined length for command 0x%02x/0x%02x", 11846 ctsio->cdb[0], ctsio->cdb[1])); 11847 for (i = 1; i < entry->length; i++) { 11848 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11849 if (diff == 0) 11850 continue; 11851 ctl_set_invalid_field(ctsio, 11852 /*sks_valid*/ 1, 11853 /*command*/ 1, 11854 /*field*/ i, 11855 /*bit_valid*/ 1, 11856 /*bit*/ fls(diff) - 1); 11857 ctl_done((union ctl_io *)ctsio); 11858 return (NULL); 11859 } 11860 return (entry); 11861} 11862 11863static int 11864ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11865{ 11866 11867 switch (lun_type) { 11868 case T_PROCESSOR: 11869 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11870 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11871 return (0); 11872 break; 11873 case T_DIRECT: 11874 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11875 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11876 return (0); 11877 break; 11878 default: 11879 return (0); 11880 } 11881 return (1); 11882} 11883 11884static int 11885ctl_scsiio(struct ctl_scsiio *ctsio) 11886{ 11887 int retval; 11888 const struct ctl_cmd_entry *entry; 11889 11890 retval = CTL_RETVAL_COMPLETE; 11891 11892 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11893 11894 entry = ctl_get_cmd_entry(ctsio); 11895 11896 /* 11897 * If this I/O has been aborted, just send it straight to 11898 * ctl_done() without executing it. 11899 */ 11900 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11901 ctl_done((union ctl_io *)ctsio); 11902 goto bailout; 11903 } 11904 11905 /* 11906 * All the checks should have been handled by ctl_scsiio_precheck(). 11907 * We should be clear now to just execute the I/O. 11908 */ 11909 retval = entry->execute(ctsio); 11910 11911bailout: 11912 return (retval); 11913} 11914 11915/* 11916 * Since we only implement one target right now, a bus reset simply resets 11917 * our single target. 11918 */ 11919static int 11920ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11921{ 11922 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11923} 11924 11925static int 11926ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11927 ctl_ua_type ua_type) 11928{ 11929 struct ctl_lun *lun; 11930 int retval; 11931 11932 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11933 union ctl_ha_msg msg_info; 11934 11935 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11936 msg_info.hdr.nexus = io->io_hdr.nexus; 11937 if (ua_type==CTL_UA_TARG_RESET) 11938 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11939 else 11940 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11941 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11942 msg_info.hdr.original_sc = NULL; 11943 msg_info.hdr.serializing_sc = NULL; 11944 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11945 (void *)&msg_info, sizeof(msg_info), 0)) { 11946 } 11947 } 11948 retval = 0; 11949 11950 mtx_lock(&ctl_softc->ctl_lock); 11951 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11952 retval += ctl_lun_reset(lun, io, ua_type); 11953 mtx_unlock(&ctl_softc->ctl_lock); 11954 11955 return (retval); 11956} 11957 11958/* 11959 * The LUN should always be set. The I/O is optional, and is used to 11960 * distinguish between I/Os sent by this initiator, and by other 11961 * initiators. We set unit attention for initiators other than this one. 11962 * SAM-3 is vague on this point. It does say that a unit attention should 11963 * be established for other initiators when a LUN is reset (see section 11964 * 5.7.3), but it doesn't specifically say that the unit attention should 11965 * be established for this particular initiator when a LUN is reset. Here 11966 * is the relevant text, from SAM-3 rev 8: 11967 * 11968 * 5.7.2 When a SCSI initiator port aborts its own tasks 11969 * 11970 * When a SCSI initiator port causes its own task(s) to be aborted, no 11971 * notification that the task(s) have been aborted shall be returned to 11972 * the SCSI initiator port other than the completion response for the 11973 * command or task management function action that caused the task(s) to 11974 * be aborted and notification(s) associated with related effects of the 11975 * action (e.g., a reset unit attention condition). 11976 * 11977 * XXX KDM for now, we're setting unit attention for all initiators. 11978 */ 11979static int 11980ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11981{ 11982 union ctl_io *xio; 11983#if 0 11984 uint32_t initindex; 11985#endif 11986 int i; 11987 11988 mtx_lock(&lun->lun_lock); 11989 /* 11990 * Run through the OOA queue and abort each I/O. 11991 */ 11992#if 0 11993 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11994#endif 11995 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11996 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11997 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11998 } 11999 12000 /* 12001 * This version sets unit attention for every 12002 */ 12003#if 0 12004 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12005 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12006 if (initindex == i) 12007 continue; 12008 lun->pending_ua[i] |= ua_type; 12009 } 12010#endif 12011 12012 /* 12013 * A reset (any kind, really) clears reservations established with 12014 * RESERVE/RELEASE. It does not clear reservations established 12015 * with PERSISTENT RESERVE OUT, but we don't support that at the 12016 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12017 * reservations made with the RESERVE/RELEASE commands, because 12018 * those commands are obsolete in SPC-3. 12019 */ 12020 lun->flags &= ~CTL_LUN_RESERVED; 12021 12022 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12023#ifdef CTL_WITH_CA 12024 ctl_clear_mask(lun->have_ca, i); 12025#endif 12026 lun->pending_ua[i] |= ua_type; 12027 } 12028 mtx_unlock(&lun->lun_lock); 12029 12030 return (0); 12031} 12032 12033static int 12034ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12035 int other_sc) 12036{ 12037 union ctl_io *xio; 12038 int found; 12039 12040 mtx_assert(&lun->lun_lock, MA_OWNED); 12041 12042 /* 12043 * Run through the OOA queue and attempt to find the given I/O. 12044 * The target port, initiator ID, tag type and tag number have to 12045 * match the values that we got from the initiator. If we have an 12046 * untagged command to abort, simply abort the first untagged command 12047 * we come to. We only allow one untagged command at a time of course. 12048 */ 12049 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12050 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12051 12052 if ((targ_port == UINT32_MAX || 12053 targ_port == xio->io_hdr.nexus.targ_port) && 12054 (init_id == UINT32_MAX || 12055 init_id == xio->io_hdr.nexus.initid.id)) { 12056 if (targ_port != xio->io_hdr.nexus.targ_port || 12057 init_id != xio->io_hdr.nexus.initid.id) 12058 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12059 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12060 found = 1; 12061 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12062 union ctl_ha_msg msg_info; 12063 12064 msg_info.hdr.nexus = xio->io_hdr.nexus; 12065 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12066 msg_info.task.tag_num = xio->scsiio.tag_num; 12067 msg_info.task.tag_type = xio->scsiio.tag_type; 12068 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12069 msg_info.hdr.original_sc = NULL; 12070 msg_info.hdr.serializing_sc = NULL; 12071 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12072 (void *)&msg_info, sizeof(msg_info), 0); 12073 } 12074 } 12075 } 12076 return (found); 12077} 12078 12079static int 12080ctl_abort_task_set(union ctl_io *io) 12081{ 12082 struct ctl_softc *softc = control_softc; 12083 struct ctl_lun *lun; 12084 uint32_t targ_lun; 12085 12086 /* 12087 * Look up the LUN. 12088 */ 12089 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12090 mtx_lock(&softc->ctl_lock); 12091 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12092 lun = softc->ctl_luns[targ_lun]; 12093 else { 12094 mtx_unlock(&softc->ctl_lock); 12095 return (1); 12096 } 12097 12098 mtx_lock(&lun->lun_lock); 12099 mtx_unlock(&softc->ctl_lock); 12100 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12101 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12102 io->io_hdr.nexus.initid.id, 12103 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12104 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12105 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12106 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12107 } 12108 mtx_unlock(&lun->lun_lock); 12109 return (0); 12110} 12111 12112static int 12113ctl_i_t_nexus_reset(union ctl_io *io) 12114{ 12115 struct ctl_softc *softc = control_softc; 12116 struct ctl_lun *lun; 12117 uint32_t initindex; 12118 12119 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12120 mtx_lock(&softc->ctl_lock); 12121 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12122 mtx_lock(&lun->lun_lock); 12123 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12124 io->io_hdr.nexus.initid.id, 12125 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12126#ifdef CTL_WITH_CA 12127 ctl_clear_mask(lun->have_ca, initindex); 12128#endif 12129 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12130 mtx_unlock(&lun->lun_lock); 12131 } 12132 mtx_unlock(&softc->ctl_lock); 12133 return (0); 12134} 12135 12136static int 12137ctl_abort_task(union ctl_io *io) 12138{ 12139 union ctl_io *xio; 12140 struct ctl_lun *lun; 12141 struct ctl_softc *ctl_softc; 12142#if 0 12143 struct sbuf sb; 12144 char printbuf[128]; 12145#endif 12146 int found; 12147 uint32_t targ_lun; 12148 12149 ctl_softc = control_softc; 12150 found = 0; 12151 12152 /* 12153 * Look up the LUN. 12154 */ 12155 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12156 mtx_lock(&ctl_softc->ctl_lock); 12157 if ((targ_lun < CTL_MAX_LUNS) 12158 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12159 lun = ctl_softc->ctl_luns[targ_lun]; 12160 else { 12161 mtx_unlock(&ctl_softc->ctl_lock); 12162 return (1); 12163 } 12164 12165#if 0 12166 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12167 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12168#endif 12169 12170 mtx_lock(&lun->lun_lock); 12171 mtx_unlock(&ctl_softc->ctl_lock); 12172 /* 12173 * Run through the OOA queue and attempt to find the given I/O. 12174 * The target port, initiator ID, tag type and tag number have to 12175 * match the values that we got from the initiator. If we have an 12176 * untagged command to abort, simply abort the first untagged command 12177 * we come to. We only allow one untagged command at a time of course. 12178 */ 12179#if 0 12180 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12181#endif 12182 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12183 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12184#if 0 12185 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12186 12187 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12188 lun->lun, xio->scsiio.tag_num, 12189 xio->scsiio.tag_type, 12190 (xio->io_hdr.blocked_links.tqe_prev 12191 == NULL) ? "" : " BLOCKED", 12192 (xio->io_hdr.flags & 12193 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12194 (xio->io_hdr.flags & 12195 CTL_FLAG_ABORT) ? " ABORT" : "", 12196 (xio->io_hdr.flags & 12197 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12198 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12199 sbuf_finish(&sb); 12200 printf("%s\n", sbuf_data(&sb)); 12201#endif 12202 12203 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12204 && (xio->io_hdr.nexus.initid.id == 12205 io->io_hdr.nexus.initid.id)) { 12206 /* 12207 * If the abort says that the task is untagged, the 12208 * task in the queue must be untagged. Otherwise, 12209 * we just check to see whether the tag numbers 12210 * match. This is because the QLogic firmware 12211 * doesn't pass back the tag type in an abort 12212 * request. 12213 */ 12214#if 0 12215 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12216 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12217 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12218#endif 12219 /* 12220 * XXX KDM we've got problems with FC, because it 12221 * doesn't send down a tag type with aborts. So we 12222 * can only really go by the tag number... 12223 * This may cause problems with parallel SCSI. 12224 * Need to figure that out!! 12225 */ 12226 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12227 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12228 found = 1; 12229 if ((io->io_hdr.flags & 12230 CTL_FLAG_FROM_OTHER_SC) == 0 && 12231 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12232 union ctl_ha_msg msg_info; 12233 12234 io->io_hdr.flags |= 12235 CTL_FLAG_SENT_2OTHER_SC; 12236 msg_info.hdr.nexus = io->io_hdr.nexus; 12237 msg_info.task.task_action = 12238 CTL_TASK_ABORT_TASK; 12239 msg_info.task.tag_num = 12240 io->taskio.tag_num; 12241 msg_info.task.tag_type = 12242 io->taskio.tag_type; 12243 msg_info.hdr.msg_type = 12244 CTL_MSG_MANAGE_TASKS; 12245 msg_info.hdr.original_sc = NULL; 12246 msg_info.hdr.serializing_sc = NULL; 12247#if 0 12248 printf("Sent Abort to other side\n"); 12249#endif 12250 if (CTL_HA_STATUS_SUCCESS != 12251 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12252 (void *)&msg_info, 12253 sizeof(msg_info), 0)) { 12254 } 12255 } 12256#if 0 12257 printf("ctl_abort_task: found I/O to abort\n"); 12258#endif 12259 break; 12260 } 12261 } 12262 } 12263 mtx_unlock(&lun->lun_lock); 12264 12265 if (found == 0) { 12266 /* 12267 * This isn't really an error. It's entirely possible for 12268 * the abort and command completion to cross on the wire. 12269 * This is more of an informative/diagnostic error. 12270 */ 12271#if 0 12272 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12273 "%d:%d:%d:%d tag %d type %d\n", 12274 io->io_hdr.nexus.initid.id, 12275 io->io_hdr.nexus.targ_port, 12276 io->io_hdr.nexus.targ_target.id, 12277 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12278 io->taskio.tag_type); 12279#endif 12280 } 12281 return (0); 12282} 12283 12284static void 12285ctl_run_task(union ctl_io *io) 12286{ 12287 struct ctl_softc *ctl_softc = control_softc; 12288 int retval = 1; 12289 const char *task_desc; 12290 12291 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12292 12293 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12294 ("ctl_run_task: Unextected io_type %d\n", 12295 io->io_hdr.io_type)); 12296 12297 task_desc = ctl_scsi_task_string(&io->taskio); 12298 if (task_desc != NULL) { 12299#ifdef NEEDTOPORT 12300 csevent_log(CSC_CTL | CSC_SHELF_SW | 12301 CTL_TASK_REPORT, 12302 csevent_LogType_Trace, 12303 csevent_Severity_Information, 12304 csevent_AlertLevel_Green, 12305 csevent_FRU_Firmware, 12306 csevent_FRU_Unknown, 12307 "CTL: received task: %s",task_desc); 12308#endif 12309 } else { 12310#ifdef NEEDTOPORT 12311 csevent_log(CSC_CTL | CSC_SHELF_SW | 12312 CTL_TASK_REPORT, 12313 csevent_LogType_Trace, 12314 csevent_Severity_Information, 12315 csevent_AlertLevel_Green, 12316 csevent_FRU_Firmware, 12317 csevent_FRU_Unknown, 12318 "CTL: received unknown task " 12319 "type: %d (%#x)", 12320 io->taskio.task_action, 12321 io->taskio.task_action); 12322#endif 12323 } 12324 switch (io->taskio.task_action) { 12325 case CTL_TASK_ABORT_TASK: 12326 retval = ctl_abort_task(io); 12327 break; 12328 case CTL_TASK_ABORT_TASK_SET: 12329 case CTL_TASK_CLEAR_TASK_SET: 12330 retval = ctl_abort_task_set(io); 12331 break; 12332 case CTL_TASK_CLEAR_ACA: 12333 break; 12334 case CTL_TASK_I_T_NEXUS_RESET: 12335 retval = ctl_i_t_nexus_reset(io); 12336 break; 12337 case CTL_TASK_LUN_RESET: { 12338 struct ctl_lun *lun; 12339 uint32_t targ_lun; 12340 12341 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12342 mtx_lock(&ctl_softc->ctl_lock); 12343 if ((targ_lun < CTL_MAX_LUNS) 12344 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12345 lun = ctl_softc->ctl_luns[targ_lun]; 12346 else { 12347 mtx_unlock(&ctl_softc->ctl_lock); 12348 retval = 1; 12349 break; 12350 } 12351 12352 if (!(io->io_hdr.flags & 12353 CTL_FLAG_FROM_OTHER_SC)) { 12354 union ctl_ha_msg msg_info; 12355 12356 io->io_hdr.flags |= 12357 CTL_FLAG_SENT_2OTHER_SC; 12358 msg_info.hdr.msg_type = 12359 CTL_MSG_MANAGE_TASKS; 12360 msg_info.hdr.nexus = io->io_hdr.nexus; 12361 msg_info.task.task_action = 12362 CTL_TASK_LUN_RESET; 12363 msg_info.hdr.original_sc = NULL; 12364 msg_info.hdr.serializing_sc = NULL; 12365 if (CTL_HA_STATUS_SUCCESS != 12366 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12367 (void *)&msg_info, 12368 sizeof(msg_info), 0)) { 12369 } 12370 } 12371 12372 retval = ctl_lun_reset(lun, io, 12373 CTL_UA_LUN_RESET); 12374 mtx_unlock(&ctl_softc->ctl_lock); 12375 break; 12376 } 12377 case CTL_TASK_TARGET_RESET: 12378 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12379 break; 12380 case CTL_TASK_BUS_RESET: 12381 retval = ctl_bus_reset(ctl_softc, io); 12382 break; 12383 case CTL_TASK_PORT_LOGIN: 12384 break; 12385 case CTL_TASK_PORT_LOGOUT: 12386 break; 12387 default: 12388 printf("ctl_run_task: got unknown task management event %d\n", 12389 io->taskio.task_action); 12390 break; 12391 } 12392 if (retval == 0) 12393 io->io_hdr.status = CTL_SUCCESS; 12394 else 12395 io->io_hdr.status = CTL_ERROR; 12396 ctl_done(io); 12397} 12398 12399/* 12400 * For HA operation. Handle commands that come in from the other 12401 * controller. 12402 */ 12403static void 12404ctl_handle_isc(union ctl_io *io) 12405{ 12406 int free_io; 12407 struct ctl_lun *lun; 12408 struct ctl_softc *ctl_softc; 12409 uint32_t targ_lun; 12410 12411 ctl_softc = control_softc; 12412 12413 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12414 lun = ctl_softc->ctl_luns[targ_lun]; 12415 12416 switch (io->io_hdr.msg_type) { 12417 case CTL_MSG_SERIALIZE: 12418 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12419 break; 12420 case CTL_MSG_R2R: { 12421 const struct ctl_cmd_entry *entry; 12422 12423 /* 12424 * This is only used in SER_ONLY mode. 12425 */ 12426 free_io = 0; 12427 entry = ctl_get_cmd_entry(&io->scsiio); 12428 mtx_lock(&lun->lun_lock); 12429 if (ctl_scsiio_lun_check(ctl_softc, lun, 12430 entry, (struct ctl_scsiio *)io) != 0) { 12431 mtx_unlock(&lun->lun_lock); 12432 ctl_done(io); 12433 break; 12434 } 12435 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12436 mtx_unlock(&lun->lun_lock); 12437 ctl_enqueue_rtr(io); 12438 break; 12439 } 12440 case CTL_MSG_FINISH_IO: 12441 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12442 free_io = 0; 12443 ctl_done(io); 12444 } else { 12445 free_io = 1; 12446 mtx_lock(&lun->lun_lock); 12447 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12448 ooa_links); 12449 ctl_check_blocked(lun); 12450 mtx_unlock(&lun->lun_lock); 12451 } 12452 break; 12453 case CTL_MSG_PERS_ACTION: 12454 ctl_hndl_per_res_out_on_other_sc( 12455 (union ctl_ha_msg *)&io->presio.pr_msg); 12456 free_io = 1; 12457 break; 12458 case CTL_MSG_BAD_JUJU: 12459 free_io = 0; 12460 ctl_done(io); 12461 break; 12462 case CTL_MSG_DATAMOVE: 12463 /* Only used in XFER mode */ 12464 free_io = 0; 12465 ctl_datamove_remote(io); 12466 break; 12467 case CTL_MSG_DATAMOVE_DONE: 12468 /* Only used in XFER mode */ 12469 free_io = 0; 12470 io->scsiio.be_move_done(io); 12471 break; 12472 default: 12473 free_io = 1; 12474 printf("%s: Invalid message type %d\n", 12475 __func__, io->io_hdr.msg_type); 12476 break; 12477 } 12478 if (free_io) 12479 ctl_free_io(io); 12480 12481} 12482 12483 12484/* 12485 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12486 * there is no match. 12487 */ 12488static ctl_lun_error_pattern 12489ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12490{ 12491 const struct ctl_cmd_entry *entry; 12492 ctl_lun_error_pattern filtered_pattern, pattern; 12493 12494 pattern = desc->error_pattern; 12495 12496 /* 12497 * XXX KDM we need more data passed into this function to match a 12498 * custom pattern, and we actually need to implement custom pattern 12499 * matching. 12500 */ 12501 if (pattern & CTL_LUN_PAT_CMD) 12502 return (CTL_LUN_PAT_CMD); 12503 12504 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12505 return (CTL_LUN_PAT_ANY); 12506 12507 entry = ctl_get_cmd_entry(ctsio); 12508 12509 filtered_pattern = entry->pattern & pattern; 12510 12511 /* 12512 * If the user requested specific flags in the pattern (e.g. 12513 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12514 * flags. 12515 * 12516 * If the user did not specify any flags, it doesn't matter whether 12517 * or not the command supports the flags. 12518 */ 12519 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12520 (pattern & ~CTL_LUN_PAT_MASK)) 12521 return (CTL_LUN_PAT_NONE); 12522 12523 /* 12524 * If the user asked for a range check, see if the requested LBA 12525 * range overlaps with this command's LBA range. 12526 */ 12527 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12528 uint64_t lba1; 12529 uint32_t len1; 12530 ctl_action action; 12531 int retval; 12532 12533 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12534 if (retval != 0) 12535 return (CTL_LUN_PAT_NONE); 12536 12537 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12538 desc->lba_range.len); 12539 /* 12540 * A "pass" means that the LBA ranges don't overlap, so 12541 * this doesn't match the user's range criteria. 12542 */ 12543 if (action == CTL_ACTION_PASS) 12544 return (CTL_LUN_PAT_NONE); 12545 } 12546 12547 return (filtered_pattern); 12548} 12549 12550static void 12551ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12552{ 12553 struct ctl_error_desc *desc, *desc2; 12554 12555 mtx_assert(&lun->lun_lock, MA_OWNED); 12556 12557 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12558 ctl_lun_error_pattern pattern; 12559 /* 12560 * Check to see whether this particular command matches 12561 * the pattern in the descriptor. 12562 */ 12563 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12564 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12565 continue; 12566 12567 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12568 case CTL_LUN_INJ_ABORTED: 12569 ctl_set_aborted(&io->scsiio); 12570 break; 12571 case CTL_LUN_INJ_MEDIUM_ERR: 12572 ctl_set_medium_error(&io->scsiio); 12573 break; 12574 case CTL_LUN_INJ_UA: 12575 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12576 * OCCURRED */ 12577 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12578 break; 12579 case CTL_LUN_INJ_CUSTOM: 12580 /* 12581 * We're assuming the user knows what he is doing. 12582 * Just copy the sense information without doing 12583 * checks. 12584 */ 12585 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12586 ctl_min(sizeof(desc->custom_sense), 12587 sizeof(io->scsiio.sense_data))); 12588 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12589 io->scsiio.sense_len = SSD_FULL_SIZE; 12590 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12591 break; 12592 case CTL_LUN_INJ_NONE: 12593 default: 12594 /* 12595 * If this is an error injection type we don't know 12596 * about, clear the continuous flag (if it is set) 12597 * so it will get deleted below. 12598 */ 12599 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12600 break; 12601 } 12602 /* 12603 * By default, each error injection action is a one-shot 12604 */ 12605 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12606 continue; 12607 12608 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12609 12610 free(desc, M_CTL); 12611 } 12612} 12613 12614#ifdef CTL_IO_DELAY 12615static void 12616ctl_datamove_timer_wakeup(void *arg) 12617{ 12618 union ctl_io *io; 12619 12620 io = (union ctl_io *)arg; 12621 12622 ctl_datamove(io); 12623} 12624#endif /* CTL_IO_DELAY */ 12625 12626void 12627ctl_datamove(union ctl_io *io) 12628{ 12629 void (*fe_datamove)(union ctl_io *io); 12630 12631 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12632 12633 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12634 12635#ifdef CTL_TIME_IO 12636 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12637 char str[256]; 12638 char path_str[64]; 12639 struct sbuf sb; 12640 12641 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12642 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12643 12644 sbuf_cat(&sb, path_str); 12645 switch (io->io_hdr.io_type) { 12646 case CTL_IO_SCSI: 12647 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12648 sbuf_printf(&sb, "\n"); 12649 sbuf_cat(&sb, path_str); 12650 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12651 io->scsiio.tag_num, io->scsiio.tag_type); 12652 break; 12653 case CTL_IO_TASK: 12654 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12655 "Tag Type: %d\n", io->taskio.task_action, 12656 io->taskio.tag_num, io->taskio.tag_type); 12657 break; 12658 default: 12659 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12660 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12661 break; 12662 } 12663 sbuf_cat(&sb, path_str); 12664 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12665 (intmax_t)time_uptime - io->io_hdr.start_time); 12666 sbuf_finish(&sb); 12667 printf("%s", sbuf_data(&sb)); 12668 } 12669#endif /* CTL_TIME_IO */ 12670 12671#ifdef CTL_IO_DELAY 12672 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12673 struct ctl_lun *lun; 12674 12675 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12676 12677 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12678 } else { 12679 struct ctl_lun *lun; 12680 12681 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12682 if ((lun != NULL) 12683 && (lun->delay_info.datamove_delay > 0)) { 12684 struct callout *callout; 12685 12686 callout = (struct callout *)&io->io_hdr.timer_bytes; 12687 callout_init(callout, /*mpsafe*/ 1); 12688 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12689 callout_reset(callout, 12690 lun->delay_info.datamove_delay * hz, 12691 ctl_datamove_timer_wakeup, io); 12692 if (lun->delay_info.datamove_type == 12693 CTL_DELAY_TYPE_ONESHOT) 12694 lun->delay_info.datamove_delay = 0; 12695 return; 12696 } 12697 } 12698#endif 12699 12700 /* 12701 * This command has been aborted. Set the port status, so we fail 12702 * the data move. 12703 */ 12704 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12705 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12706 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12707 io->io_hdr.nexus.targ_port, 12708 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12709 io->io_hdr.nexus.targ_lun); 12710 io->io_hdr.port_status = 31337; 12711 /* 12712 * Note that the backend, in this case, will get the 12713 * callback in its context. In other cases it may get 12714 * called in the frontend's interrupt thread context. 12715 */ 12716 io->scsiio.be_move_done(io); 12717 return; 12718 } 12719 12720 /* 12721 * If we're in XFER mode and this I/O is from the other shelf 12722 * controller, we need to send the DMA to the other side to 12723 * actually transfer the data to/from the host. In serialize only 12724 * mode the transfer happens below CTL and ctl_datamove() is only 12725 * called on the machine that originally received the I/O. 12726 */ 12727 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12728 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12729 union ctl_ha_msg msg; 12730 uint32_t sg_entries_sent; 12731 int do_sg_copy; 12732 int i; 12733 12734 memset(&msg, 0, sizeof(msg)); 12735 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12736 msg.hdr.original_sc = io->io_hdr.original_sc; 12737 msg.hdr.serializing_sc = io; 12738 msg.hdr.nexus = io->io_hdr.nexus; 12739 msg.dt.flags = io->io_hdr.flags; 12740 /* 12741 * We convert everything into a S/G list here. We can't 12742 * pass by reference, only by value between controllers. 12743 * So we can't pass a pointer to the S/G list, only as many 12744 * S/G entries as we can fit in here. If it's possible for 12745 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12746 * then we need to break this up into multiple transfers. 12747 */ 12748 if (io->scsiio.kern_sg_entries == 0) { 12749 msg.dt.kern_sg_entries = 1; 12750 /* 12751 * If this is in cached memory, flush the cache 12752 * before we send the DMA request to the other 12753 * controller. We want to do this in either the 12754 * read or the write case. The read case is 12755 * straightforward. In the write case, we want to 12756 * make sure nothing is in the local cache that 12757 * could overwrite the DMAed data. 12758 */ 12759 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12760 /* 12761 * XXX KDM use bus_dmamap_sync() here. 12762 */ 12763 } 12764 12765 /* 12766 * Convert to a physical address if this is a 12767 * virtual address. 12768 */ 12769 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12770 msg.dt.sg_list[0].addr = 12771 io->scsiio.kern_data_ptr; 12772 } else { 12773 /* 12774 * XXX KDM use busdma here! 12775 */ 12776#if 0 12777 msg.dt.sg_list[0].addr = (void *) 12778 vtophys(io->scsiio.kern_data_ptr); 12779#endif 12780 } 12781 12782 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12783 do_sg_copy = 0; 12784 } else { 12785 struct ctl_sg_entry *sgl; 12786 12787 do_sg_copy = 1; 12788 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12789 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12790 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12791 /* 12792 * XXX KDM use bus_dmamap_sync() here. 12793 */ 12794 } 12795 } 12796 12797 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12798 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12799 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12800 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12801 msg.dt.sg_sequence = 0; 12802 12803 /* 12804 * Loop until we've sent all of the S/G entries. On the 12805 * other end, we'll recompose these S/G entries into one 12806 * contiguous list before passing it to the 12807 */ 12808 for (sg_entries_sent = 0; sg_entries_sent < 12809 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12810 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12811 sizeof(msg.dt.sg_list[0])), 12812 msg.dt.kern_sg_entries - sg_entries_sent); 12813 12814 if (do_sg_copy != 0) { 12815 struct ctl_sg_entry *sgl; 12816 int j; 12817 12818 sgl = (struct ctl_sg_entry *) 12819 io->scsiio.kern_data_ptr; 12820 /* 12821 * If this is in cached memory, flush the cache 12822 * before we send the DMA request to the other 12823 * controller. We want to do this in either 12824 * the * read or the write case. The read 12825 * case is straightforward. In the write 12826 * case, we want to make sure nothing is 12827 * in the local cache that could overwrite 12828 * the DMAed data. 12829 */ 12830 12831 for (i = sg_entries_sent, j = 0; 12832 i < msg.dt.cur_sg_entries; i++, j++) { 12833 if ((io->io_hdr.flags & 12834 CTL_FLAG_NO_DATASYNC) == 0) { 12835 /* 12836 * XXX KDM use bus_dmamap_sync() 12837 */ 12838 } 12839 if ((io->io_hdr.flags & 12840 CTL_FLAG_BUS_ADDR) == 0) { 12841 /* 12842 * XXX KDM use busdma. 12843 */ 12844#if 0 12845 msg.dt.sg_list[j].addr =(void *) 12846 vtophys(sgl[i].addr); 12847#endif 12848 } else { 12849 msg.dt.sg_list[j].addr = 12850 sgl[i].addr; 12851 } 12852 msg.dt.sg_list[j].len = sgl[i].len; 12853 } 12854 } 12855 12856 sg_entries_sent += msg.dt.cur_sg_entries; 12857 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12858 msg.dt.sg_last = 1; 12859 else 12860 msg.dt.sg_last = 0; 12861 12862 /* 12863 * XXX KDM drop and reacquire the lock here? 12864 */ 12865 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12866 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12867 /* 12868 * XXX do something here. 12869 */ 12870 } 12871 12872 msg.dt.sent_sg_entries = sg_entries_sent; 12873 } 12874 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12875 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12876 ctl_failover_io(io, /*have_lock*/ 0); 12877 12878 } else { 12879 12880 /* 12881 * Lookup the fe_datamove() function for this particular 12882 * front end. 12883 */ 12884 fe_datamove = 12885 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12886 12887 fe_datamove(io); 12888 } 12889} 12890 12891static void 12892ctl_send_datamove_done(union ctl_io *io, int have_lock) 12893{ 12894 union ctl_ha_msg msg; 12895 int isc_status; 12896 12897 memset(&msg, 0, sizeof(msg)); 12898 12899 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12900 msg.hdr.original_sc = io; 12901 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12902 msg.hdr.nexus = io->io_hdr.nexus; 12903 msg.hdr.status = io->io_hdr.status; 12904 msg.scsi.tag_num = io->scsiio.tag_num; 12905 msg.scsi.tag_type = io->scsiio.tag_type; 12906 msg.scsi.scsi_status = io->scsiio.scsi_status; 12907 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12908 sizeof(io->scsiio.sense_data)); 12909 msg.scsi.sense_len = io->scsiio.sense_len; 12910 msg.scsi.sense_residual = io->scsiio.sense_residual; 12911 msg.scsi.fetd_status = io->io_hdr.port_status; 12912 msg.scsi.residual = io->scsiio.residual; 12913 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12914 12915 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12916 ctl_failover_io(io, /*have_lock*/ have_lock); 12917 return; 12918 } 12919 12920 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12921 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12922 /* XXX do something if this fails */ 12923 } 12924 12925} 12926 12927/* 12928 * The DMA to the remote side is done, now we need to tell the other side 12929 * we're done so it can continue with its data movement. 12930 */ 12931static void 12932ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12933{ 12934 union ctl_io *io; 12935 12936 io = rq->context; 12937 12938 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12939 printf("%s: ISC DMA write failed with error %d", __func__, 12940 rq->ret); 12941 ctl_set_internal_failure(&io->scsiio, 12942 /*sks_valid*/ 1, 12943 /*retry_count*/ rq->ret); 12944 } 12945 12946 ctl_dt_req_free(rq); 12947 12948 /* 12949 * In this case, we had to malloc the memory locally. Free it. 12950 */ 12951 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12952 int i; 12953 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12954 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12955 } 12956 /* 12957 * The data is in local and remote memory, so now we need to send 12958 * status (good or back) back to the other side. 12959 */ 12960 ctl_send_datamove_done(io, /*have_lock*/ 0); 12961} 12962 12963/* 12964 * We've moved the data from the host/controller into local memory. Now we 12965 * need to push it over to the remote controller's memory. 12966 */ 12967static int 12968ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12969{ 12970 int retval; 12971 12972 retval = 0; 12973 12974 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12975 ctl_datamove_remote_write_cb); 12976 12977 return (retval); 12978} 12979 12980static void 12981ctl_datamove_remote_write(union ctl_io *io) 12982{ 12983 int retval; 12984 void (*fe_datamove)(union ctl_io *io); 12985 12986 /* 12987 * - Get the data from the host/HBA into local memory. 12988 * - DMA memory from the local controller to the remote controller. 12989 * - Send status back to the remote controller. 12990 */ 12991 12992 retval = ctl_datamove_remote_sgl_setup(io); 12993 if (retval != 0) 12994 return; 12995 12996 /* Switch the pointer over so the FETD knows what to do */ 12997 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12998 12999 /* 13000 * Use a custom move done callback, since we need to send completion 13001 * back to the other controller, not to the backend on this side. 13002 */ 13003 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13004 13005 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13006 13007 fe_datamove(io); 13008 13009 return; 13010 13011} 13012 13013static int 13014ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13015{ 13016#if 0 13017 char str[256]; 13018 char path_str[64]; 13019 struct sbuf sb; 13020#endif 13021 13022 /* 13023 * In this case, we had to malloc the memory locally. Free it. 13024 */ 13025 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13026 int i; 13027 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13028 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13029 } 13030 13031#if 0 13032 scsi_path_string(io, path_str, sizeof(path_str)); 13033 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13034 sbuf_cat(&sb, path_str); 13035 scsi_command_string(&io->scsiio, NULL, &sb); 13036 sbuf_printf(&sb, "\n"); 13037 sbuf_cat(&sb, path_str); 13038 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13039 io->scsiio.tag_num, io->scsiio.tag_type); 13040 sbuf_cat(&sb, path_str); 13041 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13042 io->io_hdr.flags, io->io_hdr.status); 13043 sbuf_finish(&sb); 13044 printk("%s", sbuf_data(&sb)); 13045#endif 13046 13047 13048 /* 13049 * The read is done, now we need to send status (good or bad) back 13050 * to the other side. 13051 */ 13052 ctl_send_datamove_done(io, /*have_lock*/ 0); 13053 13054 return (0); 13055} 13056 13057static void 13058ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13059{ 13060 union ctl_io *io; 13061 void (*fe_datamove)(union ctl_io *io); 13062 13063 io = rq->context; 13064 13065 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13066 printf("%s: ISC DMA read failed with error %d", __func__, 13067 rq->ret); 13068 ctl_set_internal_failure(&io->scsiio, 13069 /*sks_valid*/ 1, 13070 /*retry_count*/ rq->ret); 13071 } 13072 13073 ctl_dt_req_free(rq); 13074 13075 /* Switch the pointer over so the FETD knows what to do */ 13076 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13077 13078 /* 13079 * Use a custom move done callback, since we need to send completion 13080 * back to the other controller, not to the backend on this side. 13081 */ 13082 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13083 13084 /* XXX KDM add checks like the ones in ctl_datamove? */ 13085 13086 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13087 13088 fe_datamove(io); 13089} 13090 13091static int 13092ctl_datamove_remote_sgl_setup(union ctl_io *io) 13093{ 13094 struct ctl_sg_entry *local_sglist, *remote_sglist; 13095 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13096 struct ctl_softc *softc; 13097 int retval; 13098 int i; 13099 13100 retval = 0; 13101 softc = control_softc; 13102 13103 local_sglist = io->io_hdr.local_sglist; 13104 local_dma_sglist = io->io_hdr.local_dma_sglist; 13105 remote_sglist = io->io_hdr.remote_sglist; 13106 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13107 13108 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13109 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13110 local_sglist[i].len = remote_sglist[i].len; 13111 13112 /* 13113 * XXX Detect the situation where the RS-level I/O 13114 * redirector on the other side has already read the 13115 * data off of the AOR RS on this side, and 13116 * transferred it to remote (mirror) memory on the 13117 * other side. Since we already have the data in 13118 * memory here, we just need to use it. 13119 * 13120 * XXX KDM this can probably be removed once we 13121 * get the cache device code in and take the 13122 * current AOR implementation out. 13123 */ 13124#ifdef NEEDTOPORT 13125 if ((remote_sglist[i].addr >= 13126 (void *)vtophys(softc->mirr->addr)) 13127 && (remote_sglist[i].addr < 13128 ((void *)vtophys(softc->mirr->addr) + 13129 CacheMirrorOffset))) { 13130 local_sglist[i].addr = remote_sglist[i].addr - 13131 CacheMirrorOffset; 13132 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13133 CTL_FLAG_DATA_IN) 13134 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13135 } else { 13136 local_sglist[i].addr = remote_sglist[i].addr + 13137 CacheMirrorOffset; 13138 } 13139#endif 13140#if 0 13141 printf("%s: local %p, remote %p, len %d\n", 13142 __func__, local_sglist[i].addr, 13143 remote_sglist[i].addr, local_sglist[i].len); 13144#endif 13145 } 13146 } else { 13147 uint32_t len_to_go; 13148 13149 /* 13150 * In this case, we don't have automatically allocated 13151 * memory for this I/O on this controller. This typically 13152 * happens with internal CTL I/O -- e.g. inquiry, mode 13153 * sense, etc. Anything coming from RAIDCore will have 13154 * a mirror area available. 13155 */ 13156 len_to_go = io->scsiio.kern_data_len; 13157 13158 /* 13159 * Clear the no datasync flag, we have to use malloced 13160 * buffers. 13161 */ 13162 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13163 13164 /* 13165 * The difficult thing here is that the size of the various 13166 * S/G segments may be different than the size from the 13167 * remote controller. That'll make it harder when DMAing 13168 * the data back to the other side. 13169 */ 13170 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13171 sizeof(io->io_hdr.remote_sglist[0])) && 13172 (len_to_go > 0); i++) { 13173 local_sglist[i].len = ctl_min(len_to_go, 131072); 13174 CTL_SIZE_8B(local_dma_sglist[i].len, 13175 local_sglist[i].len); 13176 local_sglist[i].addr = 13177 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13178 13179 local_dma_sglist[i].addr = local_sglist[i].addr; 13180 13181 if (local_sglist[i].addr == NULL) { 13182 int j; 13183 13184 printf("malloc failed for %zd bytes!", 13185 local_dma_sglist[i].len); 13186 for (j = 0; j < i; j++) { 13187 free(local_sglist[j].addr, M_CTL); 13188 } 13189 ctl_set_internal_failure(&io->scsiio, 13190 /*sks_valid*/ 1, 13191 /*retry_count*/ 4857); 13192 retval = 1; 13193 goto bailout_error; 13194 13195 } 13196 /* XXX KDM do we need a sync here? */ 13197 13198 len_to_go -= local_sglist[i].len; 13199 } 13200 /* 13201 * Reset the number of S/G entries accordingly. The 13202 * original number of S/G entries is available in 13203 * rem_sg_entries. 13204 */ 13205 io->scsiio.kern_sg_entries = i; 13206 13207#if 0 13208 printf("%s: kern_sg_entries = %d\n", __func__, 13209 io->scsiio.kern_sg_entries); 13210 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13211 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13212 local_sglist[i].addr, local_sglist[i].len, 13213 local_dma_sglist[i].len); 13214#endif 13215 } 13216 13217 13218 return (retval); 13219 13220bailout_error: 13221 13222 ctl_send_datamove_done(io, /*have_lock*/ 0); 13223 13224 return (retval); 13225} 13226 13227static int 13228ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13229 ctl_ha_dt_cb callback) 13230{ 13231 struct ctl_ha_dt_req *rq; 13232 struct ctl_sg_entry *remote_sglist, *local_sglist; 13233 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13234 uint32_t local_used, remote_used, total_used; 13235 int retval; 13236 int i, j; 13237 13238 retval = 0; 13239 13240 rq = ctl_dt_req_alloc(); 13241 13242 /* 13243 * If we failed to allocate the request, and if the DMA didn't fail 13244 * anyway, set busy status. This is just a resource allocation 13245 * failure. 13246 */ 13247 if ((rq == NULL) 13248 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13249 ctl_set_busy(&io->scsiio); 13250 13251 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13252 13253 if (rq != NULL) 13254 ctl_dt_req_free(rq); 13255 13256 /* 13257 * The data move failed. We need to return status back 13258 * to the other controller. No point in trying to DMA 13259 * data to the remote controller. 13260 */ 13261 13262 ctl_send_datamove_done(io, /*have_lock*/ 0); 13263 13264 retval = 1; 13265 13266 goto bailout; 13267 } 13268 13269 local_sglist = io->io_hdr.local_sglist; 13270 local_dma_sglist = io->io_hdr.local_dma_sglist; 13271 remote_sglist = io->io_hdr.remote_sglist; 13272 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13273 local_used = 0; 13274 remote_used = 0; 13275 total_used = 0; 13276 13277 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13278 rq->ret = CTL_HA_STATUS_SUCCESS; 13279 rq->context = io; 13280 callback(rq); 13281 goto bailout; 13282 } 13283 13284 /* 13285 * Pull/push the data over the wire from/to the other controller. 13286 * This takes into account the possibility that the local and 13287 * remote sglists may not be identical in terms of the size of 13288 * the elements and the number of elements. 13289 * 13290 * One fundamental assumption here is that the length allocated for 13291 * both the local and remote sglists is identical. Otherwise, we've 13292 * essentially got a coding error of some sort. 13293 */ 13294 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13295 int isc_ret; 13296 uint32_t cur_len, dma_length; 13297 uint8_t *tmp_ptr; 13298 13299 rq->id = CTL_HA_DATA_CTL; 13300 rq->command = command; 13301 rq->context = io; 13302 13303 /* 13304 * Both pointers should be aligned. But it is possible 13305 * that the allocation length is not. They should both 13306 * also have enough slack left over at the end, though, 13307 * to round up to the next 8 byte boundary. 13308 */ 13309 cur_len = ctl_min(local_sglist[i].len - local_used, 13310 remote_sglist[j].len - remote_used); 13311 13312 /* 13313 * In this case, we have a size issue and need to decrease 13314 * the size, except in the case where we actually have less 13315 * than 8 bytes left. In that case, we need to increase 13316 * the DMA length to get the last bit. 13317 */ 13318 if ((cur_len & 0x7) != 0) { 13319 if (cur_len > 0x7) { 13320 cur_len = cur_len - (cur_len & 0x7); 13321 dma_length = cur_len; 13322 } else { 13323 CTL_SIZE_8B(dma_length, cur_len); 13324 } 13325 13326 } else 13327 dma_length = cur_len; 13328 13329 /* 13330 * If we had to allocate memory for this I/O, instead of using 13331 * the non-cached mirror memory, we'll need to flush the cache 13332 * before trying to DMA to the other controller. 13333 * 13334 * We could end up doing this multiple times for the same 13335 * segment if we have a larger local segment than remote 13336 * segment. That shouldn't be an issue. 13337 */ 13338 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13339 /* 13340 * XXX KDM use bus_dmamap_sync() here. 13341 */ 13342 } 13343 13344 rq->size = dma_length; 13345 13346 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13347 tmp_ptr += local_used; 13348 13349 /* Use physical addresses when talking to ISC hardware */ 13350 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13351 /* XXX KDM use busdma */ 13352#if 0 13353 rq->local = vtophys(tmp_ptr); 13354#endif 13355 } else 13356 rq->local = tmp_ptr; 13357 13358 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13359 tmp_ptr += remote_used; 13360 rq->remote = tmp_ptr; 13361 13362 rq->callback = NULL; 13363 13364 local_used += cur_len; 13365 if (local_used >= local_sglist[i].len) { 13366 i++; 13367 local_used = 0; 13368 } 13369 13370 remote_used += cur_len; 13371 if (remote_used >= remote_sglist[j].len) { 13372 j++; 13373 remote_used = 0; 13374 } 13375 total_used += cur_len; 13376 13377 if (total_used >= io->scsiio.kern_data_len) 13378 rq->callback = callback; 13379 13380 if ((rq->size & 0x7) != 0) { 13381 printf("%s: warning: size %d is not on 8b boundary\n", 13382 __func__, rq->size); 13383 } 13384 if (((uintptr_t)rq->local & 0x7) != 0) { 13385 printf("%s: warning: local %p not on 8b boundary\n", 13386 __func__, rq->local); 13387 } 13388 if (((uintptr_t)rq->remote & 0x7) != 0) { 13389 printf("%s: warning: remote %p not on 8b boundary\n", 13390 __func__, rq->local); 13391 } 13392#if 0 13393 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13394 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13395 rq->local, rq->remote, rq->size); 13396#endif 13397 13398 isc_ret = ctl_dt_single(rq); 13399 if (isc_ret == CTL_HA_STATUS_WAIT) 13400 continue; 13401 13402 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13403 rq->ret = CTL_HA_STATUS_SUCCESS; 13404 } else { 13405 rq->ret = isc_ret; 13406 } 13407 callback(rq); 13408 goto bailout; 13409 } 13410 13411bailout: 13412 return (retval); 13413 13414} 13415 13416static void 13417ctl_datamove_remote_read(union ctl_io *io) 13418{ 13419 int retval; 13420 int i; 13421 13422 /* 13423 * This will send an error to the other controller in the case of a 13424 * failure. 13425 */ 13426 retval = ctl_datamove_remote_sgl_setup(io); 13427 if (retval != 0) 13428 return; 13429 13430 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13431 ctl_datamove_remote_read_cb); 13432 if ((retval != 0) 13433 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13434 /* 13435 * Make sure we free memory if there was an error.. The 13436 * ctl_datamove_remote_xfer() function will send the 13437 * datamove done message, or call the callback with an 13438 * error if there is a problem. 13439 */ 13440 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13441 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13442 } 13443 13444 return; 13445} 13446 13447/* 13448 * Process a datamove request from the other controller. This is used for 13449 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13450 * first. Once that is complete, the data gets DMAed into the remote 13451 * controller's memory. For reads, we DMA from the remote controller's 13452 * memory into our memory first, and then move it out to the FETD. 13453 */ 13454static void 13455ctl_datamove_remote(union ctl_io *io) 13456{ 13457 struct ctl_softc *softc; 13458 13459 softc = control_softc; 13460 13461 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13462 13463 /* 13464 * Note that we look for an aborted I/O here, but don't do some of 13465 * the other checks that ctl_datamove() normally does. 13466 * We don't need to run the datamove delay code, since that should 13467 * have been done if need be on the other controller. 13468 */ 13469 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13470 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13471 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13472 io->io_hdr.nexus.targ_port, 13473 io->io_hdr.nexus.targ_target.id, 13474 io->io_hdr.nexus.targ_lun); 13475 io->io_hdr.port_status = 31338; 13476 ctl_send_datamove_done(io, /*have_lock*/ 0); 13477 return; 13478 } 13479 13480 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13481 ctl_datamove_remote_write(io); 13482 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13483 ctl_datamove_remote_read(io); 13484 } else { 13485 union ctl_ha_msg msg; 13486 struct scsi_sense_data *sense; 13487 uint8_t sks[3]; 13488 int retry_count; 13489 13490 memset(&msg, 0, sizeof(msg)); 13491 13492 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13493 msg.hdr.status = CTL_SCSI_ERROR; 13494 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13495 13496 retry_count = 4243; 13497 13498 sense = &msg.scsi.sense_data; 13499 sks[0] = SSD_SCS_VALID; 13500 sks[1] = (retry_count >> 8) & 0xff; 13501 sks[2] = retry_count & 0xff; 13502 13503 /* "Internal target failure" */ 13504 scsi_set_sense_data(sense, 13505 /*sense_format*/ SSD_TYPE_NONE, 13506 /*current_error*/ 1, 13507 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13508 /*asc*/ 0x44, 13509 /*ascq*/ 0x00, 13510 /*type*/ SSD_ELEM_SKS, 13511 /*size*/ sizeof(sks), 13512 /*data*/ sks, 13513 SSD_ELEM_NONE); 13514 13515 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13516 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13517 ctl_failover_io(io, /*have_lock*/ 1); 13518 return; 13519 } 13520 13521 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13522 CTL_HA_STATUS_SUCCESS) { 13523 /* XXX KDM what to do if this fails? */ 13524 } 13525 return; 13526 } 13527 13528} 13529 13530static int 13531ctl_process_done(union ctl_io *io) 13532{ 13533 struct ctl_lun *lun; 13534 struct ctl_softc *ctl_softc; 13535 void (*fe_done)(union ctl_io *io); 13536 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13537 13538 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13539 13540 fe_done = 13541 control_softc->ctl_ports[targ_port]->fe_done; 13542 13543#ifdef CTL_TIME_IO 13544 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13545 char str[256]; 13546 char path_str[64]; 13547 struct sbuf sb; 13548 13549 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13550 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13551 13552 sbuf_cat(&sb, path_str); 13553 switch (io->io_hdr.io_type) { 13554 case CTL_IO_SCSI: 13555 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13556 sbuf_printf(&sb, "\n"); 13557 sbuf_cat(&sb, path_str); 13558 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13559 io->scsiio.tag_num, io->scsiio.tag_type); 13560 break; 13561 case CTL_IO_TASK: 13562 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13563 "Tag Type: %d\n", io->taskio.task_action, 13564 io->taskio.tag_num, io->taskio.tag_type); 13565 break; 13566 default: 13567 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13568 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13569 break; 13570 } 13571 sbuf_cat(&sb, path_str); 13572 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13573 (intmax_t)time_uptime - io->io_hdr.start_time); 13574 sbuf_finish(&sb); 13575 printf("%s", sbuf_data(&sb)); 13576 } 13577#endif /* CTL_TIME_IO */ 13578 13579 switch (io->io_hdr.io_type) { 13580 case CTL_IO_SCSI: 13581 break; 13582 case CTL_IO_TASK: 13583 if (bootverbose || verbose > 0) 13584 ctl_io_error_print(io, NULL); 13585 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13586 ctl_free_io(io); 13587 else 13588 fe_done(io); 13589 return (CTL_RETVAL_COMPLETE); 13590 break; 13591 default: 13592 printf("ctl_process_done: invalid io type %d\n", 13593 io->io_hdr.io_type); 13594 panic("ctl_process_done: invalid io type %d\n", 13595 io->io_hdr.io_type); 13596 break; /* NOTREACHED */ 13597 } 13598 13599 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13600 if (lun == NULL) { 13601 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13602 io->io_hdr.nexus.targ_mapped_lun)); 13603 fe_done(io); 13604 goto bailout; 13605 } 13606 ctl_softc = lun->ctl_softc; 13607 13608 mtx_lock(&lun->lun_lock); 13609 13610 /* 13611 * Check to see if we have any errors to inject here. We only 13612 * inject errors for commands that don't already have errors set. 13613 */ 13614 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13615 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13616 ctl_inject_error(lun, io); 13617 13618 /* 13619 * XXX KDM how do we treat commands that aren't completed 13620 * successfully? 13621 * 13622 * XXX KDM should we also track I/O latency? 13623 */ 13624 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13625 io->io_hdr.io_type == CTL_IO_SCSI) { 13626#ifdef CTL_TIME_IO 13627 struct bintime cur_bt; 13628#endif 13629 int type; 13630 13631 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13632 CTL_FLAG_DATA_IN) 13633 type = CTL_STATS_READ; 13634 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13635 CTL_FLAG_DATA_OUT) 13636 type = CTL_STATS_WRITE; 13637 else 13638 type = CTL_STATS_NO_IO; 13639 13640 lun->stats.ports[targ_port].bytes[type] += 13641 io->scsiio.kern_total_len; 13642 lun->stats.ports[targ_port].operations[type]++; 13643#ifdef CTL_TIME_IO 13644 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13645 &io->io_hdr.dma_bt); 13646 lun->stats.ports[targ_port].num_dmas[type] += 13647 io->io_hdr.num_dmas; 13648 getbintime(&cur_bt); 13649 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13650 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13651#endif 13652 } 13653 13654 /* 13655 * Remove this from the OOA queue. 13656 */ 13657 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13658 13659 /* 13660 * Run through the blocked queue on this LUN and see if anything 13661 * has become unblocked, now that this transaction is done. 13662 */ 13663 ctl_check_blocked(lun); 13664 13665 /* 13666 * If the LUN has been invalidated, free it if there is nothing 13667 * left on its OOA queue. 13668 */ 13669 if ((lun->flags & CTL_LUN_INVALID) 13670 && TAILQ_EMPTY(&lun->ooa_queue)) { 13671 mtx_unlock(&lun->lun_lock); 13672 mtx_lock(&ctl_softc->ctl_lock); 13673 ctl_free_lun(lun); 13674 mtx_unlock(&ctl_softc->ctl_lock); 13675 } else 13676 mtx_unlock(&lun->lun_lock); 13677 13678 /* 13679 * If this command has been aborted, make sure we set the status 13680 * properly. The FETD is responsible for freeing the I/O and doing 13681 * whatever it needs to do to clean up its state. 13682 */ 13683 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13684 ctl_set_task_aborted(&io->scsiio); 13685 13686 /* 13687 * We print out status for every task management command. For SCSI 13688 * commands, we filter out any unit attention errors; they happen 13689 * on every boot, and would clutter up the log. Note: task 13690 * management commands aren't printed here, they are printed above, 13691 * since they should never even make it down here. 13692 */ 13693 switch (io->io_hdr.io_type) { 13694 case CTL_IO_SCSI: { 13695 int error_code, sense_key, asc, ascq; 13696 13697 sense_key = 0; 13698 13699 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13700 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13701 /* 13702 * Since this is just for printing, no need to 13703 * show errors here. 13704 */ 13705 scsi_extract_sense_len(&io->scsiio.sense_data, 13706 io->scsiio.sense_len, 13707 &error_code, 13708 &sense_key, 13709 &asc, 13710 &ascq, 13711 /*show_errors*/ 0); 13712 } 13713 13714 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13715 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13716 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13717 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13718 13719 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13720 ctl_softc->skipped_prints++; 13721 } else { 13722 uint32_t skipped_prints; 13723 13724 skipped_prints = ctl_softc->skipped_prints; 13725 13726 ctl_softc->skipped_prints = 0; 13727 ctl_softc->last_print_jiffies = time_uptime; 13728 13729 if (skipped_prints > 0) { 13730#ifdef NEEDTOPORT 13731 csevent_log(CSC_CTL | CSC_SHELF_SW | 13732 CTL_ERROR_REPORT, 13733 csevent_LogType_Trace, 13734 csevent_Severity_Information, 13735 csevent_AlertLevel_Green, 13736 csevent_FRU_Firmware, 13737 csevent_FRU_Unknown, 13738 "High CTL error volume, %d prints " 13739 "skipped", skipped_prints); 13740#endif 13741 } 13742 if (bootverbose || verbose > 0) 13743 ctl_io_error_print(io, NULL); 13744 } 13745 } 13746 break; 13747 } 13748 case CTL_IO_TASK: 13749 if (bootverbose || verbose > 0) 13750 ctl_io_error_print(io, NULL); 13751 break; 13752 default: 13753 break; 13754 } 13755 13756 /* 13757 * Tell the FETD or the other shelf controller we're done with this 13758 * command. Note that only SCSI commands get to this point. Task 13759 * management commands are completed above. 13760 * 13761 * We only send status to the other controller if we're in XFER 13762 * mode. In SER_ONLY mode, the I/O is done on the controller that 13763 * received the I/O (from CTL's perspective), and so the status is 13764 * generated there. 13765 * 13766 * XXX KDM if we hold the lock here, we could cause a deadlock 13767 * if the frontend comes back in in this context to queue 13768 * something. 13769 */ 13770 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13771 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13772 union ctl_ha_msg msg; 13773 13774 memset(&msg, 0, sizeof(msg)); 13775 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13776 msg.hdr.original_sc = io->io_hdr.original_sc; 13777 msg.hdr.nexus = io->io_hdr.nexus; 13778 msg.hdr.status = io->io_hdr.status; 13779 msg.scsi.scsi_status = io->scsiio.scsi_status; 13780 msg.scsi.tag_num = io->scsiio.tag_num; 13781 msg.scsi.tag_type = io->scsiio.tag_type; 13782 msg.scsi.sense_len = io->scsiio.sense_len; 13783 msg.scsi.sense_residual = io->scsiio.sense_residual; 13784 msg.scsi.residual = io->scsiio.residual; 13785 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13786 sizeof(io->scsiio.sense_data)); 13787 /* 13788 * We copy this whether or not this is an I/O-related 13789 * command. Otherwise, we'd have to go and check to see 13790 * whether it's a read/write command, and it really isn't 13791 * worth it. 13792 */ 13793 memcpy(&msg.scsi.lbalen, 13794 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13795 sizeof(msg.scsi.lbalen)); 13796 13797 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13798 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13799 /* XXX do something here */ 13800 } 13801 13802 ctl_free_io(io); 13803 } else 13804 fe_done(io); 13805 13806bailout: 13807 13808 return (CTL_RETVAL_COMPLETE); 13809} 13810 13811#ifdef CTL_WITH_CA 13812/* 13813 * Front end should call this if it doesn't do autosense. When the request 13814 * sense comes back in from the initiator, we'll dequeue this and send it. 13815 */ 13816int 13817ctl_queue_sense(union ctl_io *io) 13818{ 13819 struct ctl_lun *lun; 13820 struct ctl_softc *ctl_softc; 13821 uint32_t initidx, targ_lun; 13822 13823 ctl_softc = control_softc; 13824 13825 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13826 13827 /* 13828 * LUN lookup will likely move to the ctl_work_thread() once we 13829 * have our new queueing infrastructure (that doesn't put things on 13830 * a per-LUN queue initially). That is so that we can handle 13831 * things like an INQUIRY to a LUN that we don't have enabled. We 13832 * can't deal with that right now. 13833 */ 13834 mtx_lock(&ctl_softc->ctl_lock); 13835 13836 /* 13837 * If we don't have a LUN for this, just toss the sense 13838 * information. 13839 */ 13840 targ_lun = io->io_hdr.nexus.targ_lun; 13841 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13842 if ((targ_lun < CTL_MAX_LUNS) 13843 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13844 lun = ctl_softc->ctl_luns[targ_lun]; 13845 else 13846 goto bailout; 13847 13848 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13849 13850 mtx_lock(&lun->lun_lock); 13851 /* 13852 * Already have CA set for this LUN...toss the sense information. 13853 */ 13854 if (ctl_is_set(lun->have_ca, initidx)) { 13855 mtx_unlock(&lun->lun_lock); 13856 goto bailout; 13857 } 13858 13859 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13860 ctl_min(sizeof(lun->pending_sense[initidx]), 13861 sizeof(io->scsiio.sense_data))); 13862 ctl_set_mask(lun->have_ca, initidx); 13863 mtx_unlock(&lun->lun_lock); 13864 13865bailout: 13866 mtx_unlock(&ctl_softc->ctl_lock); 13867 13868 ctl_free_io(io); 13869 13870 return (CTL_RETVAL_COMPLETE); 13871} 13872#endif 13873 13874/* 13875 * Primary command inlet from frontend ports. All SCSI and task I/O 13876 * requests must go through this function. 13877 */ 13878int 13879ctl_queue(union ctl_io *io) 13880{ 13881 struct ctl_softc *ctl_softc; 13882 13883 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13884 13885 ctl_softc = control_softc; 13886 13887#ifdef CTL_TIME_IO 13888 io->io_hdr.start_time = time_uptime; 13889 getbintime(&io->io_hdr.start_bt); 13890#endif /* CTL_TIME_IO */ 13891 13892 /* Map FE-specific LUN ID into global one. */ 13893 io->io_hdr.nexus.targ_mapped_lun = 13894 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13895 13896 switch (io->io_hdr.io_type) { 13897 case CTL_IO_SCSI: 13898 case CTL_IO_TASK: 13899 ctl_enqueue_incoming(io); 13900 break; 13901 default: 13902 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13903 return (EINVAL); 13904 } 13905 13906 return (CTL_RETVAL_COMPLETE); 13907} 13908 13909#ifdef CTL_IO_DELAY 13910static void 13911ctl_done_timer_wakeup(void *arg) 13912{ 13913 union ctl_io *io; 13914 13915 io = (union ctl_io *)arg; 13916 ctl_done(io); 13917} 13918#endif /* CTL_IO_DELAY */ 13919 13920void 13921ctl_done(union ctl_io *io) 13922{ 13923 struct ctl_softc *ctl_softc; 13924 13925 ctl_softc = control_softc; 13926 13927 /* 13928 * Enable this to catch duplicate completion issues. 13929 */ 13930#if 0 13931 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13932 printf("%s: type %d msg %d cdb %x iptl: " 13933 "%d:%d:%d:%d tag 0x%04x " 13934 "flag %#x status %x\n", 13935 __func__, 13936 io->io_hdr.io_type, 13937 io->io_hdr.msg_type, 13938 io->scsiio.cdb[0], 13939 io->io_hdr.nexus.initid.id, 13940 io->io_hdr.nexus.targ_port, 13941 io->io_hdr.nexus.targ_target.id, 13942 io->io_hdr.nexus.targ_lun, 13943 (io->io_hdr.io_type == 13944 CTL_IO_TASK) ? 13945 io->taskio.tag_num : 13946 io->scsiio.tag_num, 13947 io->io_hdr.flags, 13948 io->io_hdr.status); 13949 } else 13950 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13951#endif 13952 13953 /* 13954 * This is an internal copy of an I/O, and should not go through 13955 * the normal done processing logic. 13956 */ 13957 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13958 return; 13959 13960 /* 13961 * We need to send a msg to the serializing shelf to finish the IO 13962 * as well. We don't send a finish message to the other shelf if 13963 * this is a task management command. Task management commands 13964 * aren't serialized in the OOA queue, but rather just executed on 13965 * both shelf controllers for commands that originated on that 13966 * controller. 13967 */ 13968 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13969 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13970 union ctl_ha_msg msg_io; 13971 13972 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13973 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13974 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13975 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13976 } 13977 /* continue on to finish IO */ 13978 } 13979#ifdef CTL_IO_DELAY 13980 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13981 struct ctl_lun *lun; 13982 13983 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13984 13985 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13986 } else { 13987 struct ctl_lun *lun; 13988 13989 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13990 13991 if ((lun != NULL) 13992 && (lun->delay_info.done_delay > 0)) { 13993 struct callout *callout; 13994 13995 callout = (struct callout *)&io->io_hdr.timer_bytes; 13996 callout_init(callout, /*mpsafe*/ 1); 13997 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13998 callout_reset(callout, 13999 lun->delay_info.done_delay * hz, 14000 ctl_done_timer_wakeup, io); 14001 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14002 lun->delay_info.done_delay = 0; 14003 return; 14004 } 14005 } 14006#endif /* CTL_IO_DELAY */ 14007 14008 ctl_enqueue_done(io); 14009} 14010 14011int 14012ctl_isc(struct ctl_scsiio *ctsio) 14013{ 14014 struct ctl_lun *lun; 14015 int retval; 14016 14017 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14018 14019 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14020 14021 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14022 14023 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14024 14025 return (retval); 14026} 14027 14028 14029static void 14030ctl_work_thread(void *arg) 14031{ 14032 struct ctl_thread *thr = (struct ctl_thread *)arg; 14033 struct ctl_softc *softc = thr->ctl_softc; 14034 union ctl_io *io; 14035 int retval; 14036 14037 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14038 14039 for (;;) { 14040 retval = 0; 14041 14042 /* 14043 * We handle the queues in this order: 14044 * - ISC 14045 * - done queue (to free up resources, unblock other commands) 14046 * - RtR queue 14047 * - incoming queue 14048 * 14049 * If those queues are empty, we break out of the loop and 14050 * go to sleep. 14051 */ 14052 mtx_lock(&thr->queue_lock); 14053 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14054 if (io != NULL) { 14055 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14056 mtx_unlock(&thr->queue_lock); 14057 ctl_handle_isc(io); 14058 continue; 14059 } 14060 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14061 if (io != NULL) { 14062 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14063 /* clear any blocked commands, call fe_done */ 14064 mtx_unlock(&thr->queue_lock); 14065 retval = ctl_process_done(io); 14066 continue; 14067 } 14068 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14069 if (io != NULL) { 14070 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14071 mtx_unlock(&thr->queue_lock); 14072 if (io->io_hdr.io_type == CTL_IO_TASK) 14073 ctl_run_task(io); 14074 else 14075 ctl_scsiio_precheck(softc, &io->scsiio); 14076 continue; 14077 } 14078 if (!ctl_pause_rtr) { 14079 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14080 if (io != NULL) { 14081 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14082 mtx_unlock(&thr->queue_lock); 14083 retval = ctl_scsiio(&io->scsiio); 14084 if (retval != CTL_RETVAL_COMPLETE) 14085 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14086 continue; 14087 } 14088 } 14089 14090 /* Sleep until we have something to do. */ 14091 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14092 } 14093} 14094 14095static void 14096ctl_lun_thread(void *arg) 14097{ 14098 struct ctl_softc *softc = (struct ctl_softc *)arg; 14099 struct ctl_be_lun *be_lun; 14100 int retval; 14101 14102 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14103 14104 for (;;) { 14105 retval = 0; 14106 mtx_lock(&softc->ctl_lock); 14107 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14108 if (be_lun != NULL) { 14109 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14110 mtx_unlock(&softc->ctl_lock); 14111 ctl_create_lun(be_lun); 14112 continue; 14113 } 14114 14115 /* Sleep until we have something to do. */ 14116 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14117 PDROP | PRIBIO, "-", 0); 14118 } 14119} 14120 14121static void 14122ctl_enqueue_incoming(union ctl_io *io) 14123{ 14124 struct ctl_softc *softc = control_softc; 14125 struct ctl_thread *thr; 14126 u_int idx; 14127 14128 idx = (io->io_hdr.nexus.targ_port * 127 + 14129 io->io_hdr.nexus.initid.id) % worker_threads; 14130 thr = &softc->threads[idx]; 14131 mtx_lock(&thr->queue_lock); 14132 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14133 mtx_unlock(&thr->queue_lock); 14134 wakeup(thr); 14135} 14136 14137static void 14138ctl_enqueue_rtr(union ctl_io *io) 14139{ 14140 struct ctl_softc *softc = control_softc; 14141 struct ctl_thread *thr; 14142 14143 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14144 mtx_lock(&thr->queue_lock); 14145 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14146 mtx_unlock(&thr->queue_lock); 14147 wakeup(thr); 14148} 14149 14150static void 14151ctl_enqueue_done(union ctl_io *io) 14152{ 14153 struct ctl_softc *softc = control_softc; 14154 struct ctl_thread *thr; 14155 14156 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14157 mtx_lock(&thr->queue_lock); 14158 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14159 mtx_unlock(&thr->queue_lock); 14160 wakeup(thr); 14161} 14162 14163static void 14164ctl_enqueue_isc(union ctl_io *io) 14165{ 14166 struct ctl_softc *softc = control_softc; 14167 struct ctl_thread *thr; 14168 14169 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14170 mtx_lock(&thr->queue_lock); 14171 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14172 mtx_unlock(&thr->queue_lock); 14173 wakeup(thr); 14174} 14175 14176/* Initialization and failover */ 14177 14178void 14179ctl_init_isc_msg(void) 14180{ 14181 printf("CTL: Still calling this thing\n"); 14182} 14183 14184/* 14185 * Init component 14186 * Initializes component into configuration defined by bootMode 14187 * (see hasc-sv.c) 14188 * returns hasc_Status: 14189 * OK 14190 * ERROR - fatal error 14191 */ 14192static ctl_ha_comp_status 14193ctl_isc_init(struct ctl_ha_component *c) 14194{ 14195 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14196 14197 c->status = ret; 14198 return ret; 14199} 14200 14201/* Start component 14202 * Starts component in state requested. If component starts successfully, 14203 * it must set its own state to the requestrd state 14204 * When requested state is HASC_STATE_HA, the component may refine it 14205 * by adding _SLAVE or _MASTER flags. 14206 * Currently allowed state transitions are: 14207 * UNKNOWN->HA - initial startup 14208 * UNKNOWN->SINGLE - initial startup when no parter detected 14209 * HA->SINGLE - failover 14210 * returns ctl_ha_comp_status: 14211 * OK - component successfully started in requested state 14212 * FAILED - could not start the requested state, failover may 14213 * be possible 14214 * ERROR - fatal error detected, no future startup possible 14215 */ 14216static ctl_ha_comp_status 14217ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14218{ 14219 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14220 14221 printf("%s: go\n", __func__); 14222 14223 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14224 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14225 ctl_is_single = 0; 14226 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14227 != CTL_HA_STATUS_SUCCESS) { 14228 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14229 ret = CTL_HA_COMP_STATUS_ERROR; 14230 } 14231 } else if (CTL_HA_STATE_IS_HA(c->state) 14232 && CTL_HA_STATE_IS_SINGLE(state)){ 14233 // HA->SINGLE transition 14234 ctl_failover(); 14235 ctl_is_single = 1; 14236 } else { 14237 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14238 c->state, state); 14239 ret = CTL_HA_COMP_STATUS_ERROR; 14240 } 14241 if (CTL_HA_STATE_IS_SINGLE(state)) 14242 ctl_is_single = 1; 14243 14244 c->state = state; 14245 c->status = ret; 14246 return ret; 14247} 14248 14249/* 14250 * Quiesce component 14251 * The component must clear any error conditions (set status to OK) and 14252 * prepare itself to another Start call 14253 * returns ctl_ha_comp_status: 14254 * OK 14255 * ERROR 14256 */ 14257static ctl_ha_comp_status 14258ctl_isc_quiesce(struct ctl_ha_component *c) 14259{ 14260 int ret = CTL_HA_COMP_STATUS_OK; 14261 14262 ctl_pause_rtr = 1; 14263 c->status = ret; 14264 return ret; 14265} 14266 14267struct ctl_ha_component ctl_ha_component_ctlisc = 14268{ 14269 .name = "CTL ISC", 14270 .state = CTL_HA_STATE_UNKNOWN, 14271 .init = ctl_isc_init, 14272 .start = ctl_isc_start, 14273 .quiesce = ctl_isc_quiesce 14274}; 14275 14276/* 14277 * vim: ts=8 14278 */ 14279