ctl.c revision 273312
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 273312 2014-10-20 07:34:37Z mav $"); 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/types.h> 51#include <sys/kthread.h> 52#include <sys/bio.h> 53#include <sys/fcntl.h> 54#include <sys/lock.h> 55#include <sys/module.h> 56#include <sys/mutex.h> 57#include <sys/condvar.h> 58#include <sys/malloc.h> 59#include <sys/conf.h> 60#include <sys/ioccom.h> 61#include <sys/queue.h> 62#include <sys/sbuf.h> 63#include <sys/smp.h> 64#include <sys/endian.h> 65#include <sys/sysctl.h> 66 67#include <cam/cam.h> 68#include <cam/scsi/scsi_all.h> 69#include <cam/scsi/scsi_da.h> 70#include <cam/ctl/ctl_io.h> 71#include <cam/ctl/ctl.h> 72#include <cam/ctl/ctl_frontend.h> 73#include <cam/ctl/ctl_frontend_internal.h> 74#include <cam/ctl/ctl_util.h> 75#include <cam/ctl/ctl_backend.h> 76#include <cam/ctl/ctl_ioctl.h> 77#include <cam/ctl/ctl_ha.h> 78#include <cam/ctl/ctl_private.h> 79#include <cam/ctl/ctl_debug.h> 80#include <cam/ctl/ctl_scsi_all.h> 81#include <cam/ctl/ctl_error.h> 82 83struct ctl_softc *control_softc = NULL; 84 85/* 86 * Size and alignment macros needed for Copan-specific HA hardware. These 87 * can go away when the HA code is re-written, and uses busdma for any 88 * hardware. 89 */ 90#define CTL_ALIGN_8B(target, source, type) \ 91 if (((uint32_t)source & 0x7) != 0) \ 92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93 else \ 94 target = (type)source; 95 96#define CTL_SIZE_8B(target, size) \ 97 if ((size & 0x7) != 0) \ 98 target = size + (0x8 - (size & 0x7)); \ 99 else \ 100 target = size; 101 102#define CTL_ALIGN_8B_MARGIN 16 103 104/* 105 * Template mode pages. 106 */ 107 108/* 109 * Note that these are default values only. The actual values will be 110 * filled in when the user does a mode sense. 111 */ 112static struct copan_power_subpage power_page_default = { 113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 114 /*subpage*/ PWR_SUBPAGE_CODE, 115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 117 /*page_version*/ PWR_VERSION, 118 /* total_luns */ 26, 119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122 0, 0, 0, 0, 0, 0} 123}; 124 125static struct copan_power_subpage power_page_changeable = { 126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 127 /*subpage*/ PWR_SUBPAGE_CODE, 128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 130 /*page_version*/ 0, 131 /* total_luns */ 0, 132 /* max_active_luns*/ 0, 133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135 0, 0, 0, 0, 0, 0} 136}; 137 138static struct copan_aps_subpage aps_page_default = { 139 APS_PAGE_CODE | SMPH_SPF, //page_code 140 APS_SUBPAGE_CODE, //subpage 141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 143 APS_VERSION, //page_version 144 0, //lock_active 145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147 0, 0, 0, 0, 0} //reserved 148}; 149 150static struct copan_aps_subpage aps_page_changeable = { 151 APS_PAGE_CODE | SMPH_SPF, //page_code 152 APS_SUBPAGE_CODE, //subpage 153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 155 0, //page_version 156 0, //lock_active 157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159 0, 0, 0, 0, 0} //reserved 160}; 161 162static struct copan_debugconf_subpage debugconf_page_default = { 163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 164 DBGCNF_SUBPAGE_CODE, /* subpage */ 165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 167 DBGCNF_VERSION, /* page_version */ 168 {CTL_TIME_IO_DEFAULT_SECS>>8, 169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 170}; 171 172static struct copan_debugconf_subpage debugconf_page_changeable = { 173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 174 DBGCNF_SUBPAGE_CODE, /* subpage */ 175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 177 0, /* page_version */ 178 {0xff,0xff}, /* ctl_time_io_secs */ 179}; 180 181static struct scsi_format_page format_page_default = { 182 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 183 /*page_length*/sizeof(struct scsi_format_page) - 2, 184 /*tracks_per_zone*/ {0, 0}, 185 /*alt_sectors_per_zone*/ {0, 0}, 186 /*alt_tracks_per_zone*/ {0, 0}, 187 /*alt_tracks_per_lun*/ {0, 0}, 188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 190 /*bytes_per_sector*/ {0, 0}, 191 /*interleave*/ {0, 0}, 192 /*track_skew*/ {0, 0}, 193 /*cylinder_skew*/ {0, 0}, 194 /*flags*/ SFP_HSEC, 195 /*reserved*/ {0, 0, 0} 196}; 197 198static struct scsi_format_page format_page_changeable = { 199 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 200 /*page_length*/sizeof(struct scsi_format_page) - 2, 201 /*tracks_per_zone*/ {0, 0}, 202 /*alt_sectors_per_zone*/ {0, 0}, 203 /*alt_tracks_per_zone*/ {0, 0}, 204 /*alt_tracks_per_lun*/ {0, 0}, 205 /*sectors_per_track*/ {0, 0}, 206 /*bytes_per_sector*/ {0, 0}, 207 /*interleave*/ {0, 0}, 208 /*track_skew*/ {0, 0}, 209 /*cylinder_skew*/ {0, 0}, 210 /*flags*/ 0, 211 /*reserved*/ {0, 0, 0} 212}; 213 214static struct scsi_rigid_disk_page rigid_disk_page_default = { 215 /*page_code*/SMS_RIGID_DISK_PAGE, 216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 217 /*cylinders*/ {0, 0, 0}, 218 /*heads*/ CTL_DEFAULT_HEADS, 219 /*start_write_precomp*/ {0, 0, 0}, 220 /*start_reduced_current*/ {0, 0, 0}, 221 /*step_rate*/ {0, 0}, 222 /*landing_zone_cylinder*/ {0, 0, 0}, 223 /*rpl*/ SRDP_RPL_DISABLED, 224 /*rotational_offset*/ 0, 225 /*reserved1*/ 0, 226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 227 CTL_DEFAULT_ROTATION_RATE & 0xff}, 228 /*reserved2*/ {0, 0} 229}; 230 231static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 232 /*page_code*/SMS_RIGID_DISK_PAGE, 233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 234 /*cylinders*/ {0, 0, 0}, 235 /*heads*/ 0, 236 /*start_write_precomp*/ {0, 0, 0}, 237 /*start_reduced_current*/ {0, 0, 0}, 238 /*step_rate*/ {0, 0}, 239 /*landing_zone_cylinder*/ {0, 0, 0}, 240 /*rpl*/ 0, 241 /*rotational_offset*/ 0, 242 /*reserved1*/ 0, 243 /*rotation_rate*/ {0, 0}, 244 /*reserved2*/ {0, 0} 245}; 246 247static struct scsi_caching_page caching_page_default = { 248 /*page_code*/SMS_CACHING_PAGE, 249 /*page_length*/sizeof(struct scsi_caching_page) - 2, 250 /*flags1*/ SCP_DISC | SCP_WCE, 251 /*ret_priority*/ 0, 252 /*disable_pf_transfer_len*/ {0xff, 0xff}, 253 /*min_prefetch*/ {0, 0}, 254 /*max_prefetch*/ {0xff, 0xff}, 255 /*max_pf_ceiling*/ {0xff, 0xff}, 256 /*flags2*/ 0, 257 /*cache_segments*/ 0, 258 /*cache_seg_size*/ {0, 0}, 259 /*reserved*/ 0, 260 /*non_cache_seg_size*/ {0, 0, 0} 261}; 262 263static struct scsi_caching_page caching_page_changeable = { 264 /*page_code*/SMS_CACHING_PAGE, 265 /*page_length*/sizeof(struct scsi_caching_page) - 2, 266 /*flags1*/ SCP_WCE | SCP_RCD, 267 /*ret_priority*/ 0, 268 /*disable_pf_transfer_len*/ {0, 0}, 269 /*min_prefetch*/ {0, 0}, 270 /*max_prefetch*/ {0, 0}, 271 /*max_pf_ceiling*/ {0, 0}, 272 /*flags2*/ 0, 273 /*cache_segments*/ 0, 274 /*cache_seg_size*/ {0, 0}, 275 /*reserved*/ 0, 276 /*non_cache_seg_size*/ {0, 0, 0} 277}; 278 279static struct scsi_control_page control_page_default = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE, 281 /*page_length*/sizeof(struct scsi_control_page) - 2, 282 /*rlec*/0, 283 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 284 /*eca_and_aen*/0, 285 /*flags4*/SCP_TAS, 286 /*aen_holdoff_period*/{0, 0}, 287 /*busy_timeout_period*/{0, 0}, 288 /*extended_selftest_completion_time*/{0, 0} 289}; 290 291static struct scsi_control_page control_page_changeable = { 292 /*page_code*/SMS_CONTROL_MODE_PAGE, 293 /*page_length*/sizeof(struct scsi_control_page) - 2, 294 /*rlec*/SCP_DSENSE, 295 /*queue_flags*/SCP_QUEUE_ALG_MASK, 296 /*eca_and_aen*/SCP_SWP, 297 /*flags4*/0, 298 /*aen_holdoff_period*/{0, 0}, 299 /*busy_timeout_period*/{0, 0}, 300 /*extended_selftest_completion_time*/{0, 0} 301}; 302 303 304/* 305 * XXX KDM move these into the softc. 306 */ 307static int rcv_sync_msg; 308static int persis_offset; 309static uint8_t ctl_pause_rtr; 310static int ctl_is_single = 1; 311static int index_to_aps_page; 312 313SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 314static int worker_threads = -1; 315TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 316SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 317 &worker_threads, 1, "Number of worker threads"); 318static int verbose = 0; 319TUNABLE_INT("kern.cam.ctl.verbose", &verbose); 320SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 321 &verbose, 0, "Show SCSI errors returned to initiator"); 322 323/* 324 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 325 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 326 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 327 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 328 */ 329#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 330 331static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 332 int param); 333static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 334static int ctl_init(void); 335void ctl_shutdown(void); 336static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 337static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 338static void ctl_ioctl_online(void *arg); 339static void ctl_ioctl_offline(void *arg); 340static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 341static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 342static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 343static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 344static int ctl_ioctl_submit_wait(union ctl_io *io); 345static void ctl_ioctl_datamove(union ctl_io *io); 346static void ctl_ioctl_done(union ctl_io *io); 347static void ctl_ioctl_hard_startstop_callback(void *arg, 348 struct cfi_metatask *metatask); 349static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 350static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 351 struct ctl_ooa *ooa_hdr, 352 struct ctl_ooa_entry *kern_entries); 353static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 354 struct thread *td); 355static uint32_t ctl_map_lun(int port_num, uint32_t lun); 356static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 357#ifdef unused 358static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 359 uint32_t targ_target, uint32_t targ_lun, 360 int can_wait); 361static void ctl_kfree_io(union ctl_io *io); 362#endif /* unused */ 363static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 364 struct ctl_be_lun *be_lun, struct ctl_id target_id); 365static int ctl_free_lun(struct ctl_lun *lun); 366static void ctl_create_lun(struct ctl_be_lun *be_lun); 367/** 368static void ctl_failover_change_pages(struct ctl_softc *softc, 369 struct ctl_scsiio *ctsio, int master); 370**/ 371 372static int ctl_do_mode_select(union ctl_io *io); 373static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 374 uint64_t res_key, uint64_t sa_res_key, 375 uint8_t type, uint32_t residx, 376 struct ctl_scsiio *ctsio, 377 struct scsi_per_res_out *cdb, 378 struct scsi_per_res_out_parms* param); 379static void ctl_pro_preempt_other(struct ctl_lun *lun, 380 union ctl_ha_msg *msg); 381static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 382static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 383static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 384static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 385static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 386static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 387static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 388 int alloc_len); 389static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 390 int alloc_len); 391static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 392static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 393static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 394static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 395static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 396static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 397static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 398 union ctl_io *pending_io, union ctl_io *ooa_io); 399static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 400 union ctl_io *starting_io); 401static int ctl_check_blocked(struct ctl_lun *lun); 402static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 403 struct ctl_lun *lun, 404 const struct ctl_cmd_entry *entry, 405 struct ctl_scsiio *ctsio); 406//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 407static void ctl_failover(void); 408static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 409 struct ctl_scsiio *ctsio); 410static int ctl_scsiio(struct ctl_scsiio *ctsio); 411 412static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 413static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 414 ctl_ua_type ua_type); 415static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 416 ctl_ua_type ua_type); 417static int ctl_abort_task(union ctl_io *io); 418static int ctl_abort_task_set(union ctl_io *io); 419static int ctl_i_t_nexus_reset(union ctl_io *io); 420static void ctl_run_task(union ctl_io *io); 421#ifdef CTL_IO_DELAY 422static void ctl_datamove_timer_wakeup(void *arg); 423static void ctl_done_timer_wakeup(void *arg); 424#endif /* CTL_IO_DELAY */ 425 426static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 427static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 428static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 429static void ctl_datamove_remote_write(union ctl_io *io); 430static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 431static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 432static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 433static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 434 ctl_ha_dt_cb callback); 435static void ctl_datamove_remote_read(union ctl_io *io); 436static void ctl_datamove_remote(union ctl_io *io); 437static int ctl_process_done(union ctl_io *io); 438static void ctl_lun_thread(void *arg); 439static void ctl_work_thread(void *arg); 440static void ctl_enqueue_incoming(union ctl_io *io); 441static void ctl_enqueue_rtr(union ctl_io *io); 442static void ctl_enqueue_done(union ctl_io *io); 443static void ctl_enqueue_isc(union ctl_io *io); 444static const struct ctl_cmd_entry * 445 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 446static const struct ctl_cmd_entry * 447 ctl_validate_command(struct ctl_scsiio *ctsio); 448static int ctl_cmd_applicable(uint8_t lun_type, 449 const struct ctl_cmd_entry *entry); 450 451/* 452 * Load the serialization table. This isn't very pretty, but is probably 453 * the easiest way to do it. 454 */ 455#include "ctl_ser_table.c" 456 457/* 458 * We only need to define open, close and ioctl routines for this driver. 459 */ 460static struct cdevsw ctl_cdevsw = { 461 .d_version = D_VERSION, 462 .d_flags = 0, 463 .d_open = ctl_open, 464 .d_close = ctl_close, 465 .d_ioctl = ctl_ioctl, 466 .d_name = "ctl", 467}; 468 469 470MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 471MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 472 473static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 474 475static moduledata_t ctl_moduledata = { 476 "ctl", 477 ctl_module_event_handler, 478 NULL 479}; 480 481DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 482MODULE_VERSION(ctl, 1); 483 484static struct ctl_frontend ioctl_frontend = 485{ 486 .name = "ioctl", 487}; 488 489static void 490ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 491 union ctl_ha_msg *msg_info) 492{ 493 struct ctl_scsiio *ctsio; 494 495 if (msg_info->hdr.original_sc == NULL) { 496 printf("%s: original_sc == NULL!\n", __func__); 497 /* XXX KDM now what? */ 498 return; 499 } 500 501 ctsio = &msg_info->hdr.original_sc->scsiio; 502 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 503 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 504 ctsio->io_hdr.status = msg_info->hdr.status; 505 ctsio->scsi_status = msg_info->scsi.scsi_status; 506 ctsio->sense_len = msg_info->scsi.sense_len; 507 ctsio->sense_residual = msg_info->scsi.sense_residual; 508 ctsio->residual = msg_info->scsi.residual; 509 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 510 sizeof(ctsio->sense_data)); 511 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 512 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 513 ctl_enqueue_isc((union ctl_io *)ctsio); 514} 515 516static void 517ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 518 union ctl_ha_msg *msg_info) 519{ 520 struct ctl_scsiio *ctsio; 521 522 if (msg_info->hdr.serializing_sc == NULL) { 523 printf("%s: serializing_sc == NULL!\n", __func__); 524 /* XXX KDM now what? */ 525 return; 526 } 527 528 ctsio = &msg_info->hdr.serializing_sc->scsiio; 529#if 0 530 /* 531 * Attempt to catch the situation where an I/O has 532 * been freed, and we're using it again. 533 */ 534 if (ctsio->io_hdr.io_type == 0xff) { 535 union ctl_io *tmp_io; 536 tmp_io = (union ctl_io *)ctsio; 537 printf("%s: %p use after free!\n", __func__, 538 ctsio); 539 printf("%s: type %d msg %d cdb %x iptl: " 540 "%d:%d:%d:%d tag 0x%04x " 541 "flag %#x status %x\n", 542 __func__, 543 tmp_io->io_hdr.io_type, 544 tmp_io->io_hdr.msg_type, 545 tmp_io->scsiio.cdb[0], 546 tmp_io->io_hdr.nexus.initid.id, 547 tmp_io->io_hdr.nexus.targ_port, 548 tmp_io->io_hdr.nexus.targ_target.id, 549 tmp_io->io_hdr.nexus.targ_lun, 550 (tmp_io->io_hdr.io_type == 551 CTL_IO_TASK) ? 552 tmp_io->taskio.tag_num : 553 tmp_io->scsiio.tag_num, 554 tmp_io->io_hdr.flags, 555 tmp_io->io_hdr.status); 556 } 557#endif 558 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 559 ctl_enqueue_isc((union ctl_io *)ctsio); 560} 561 562/* 563 * ISC (Inter Shelf Communication) event handler. Events from the HA 564 * subsystem come in here. 565 */ 566static void 567ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 568{ 569 struct ctl_softc *ctl_softc; 570 union ctl_io *io; 571 struct ctl_prio *presio; 572 ctl_ha_status isc_status; 573 574 ctl_softc = control_softc; 575 io = NULL; 576 577 578#if 0 579 printf("CTL: Isc Msg event %d\n", event); 580#endif 581 if (event == CTL_HA_EVT_MSG_RECV) { 582 union ctl_ha_msg msg_info; 583 584 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 585 sizeof(msg_info), /*wait*/ 0); 586#if 0 587 printf("CTL: msg_type %d\n", msg_info.msg_type); 588#endif 589 if (isc_status != 0) { 590 printf("Error receiving message, status = %d\n", 591 isc_status); 592 return; 593 } 594 595 switch (msg_info.hdr.msg_type) { 596 case CTL_MSG_SERIALIZE: 597#if 0 598 printf("Serialize\n"); 599#endif 600 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 601 if (io == NULL) { 602 printf("ctl_isc_event_handler: can't allocate " 603 "ctl_io!\n"); 604 /* Bad Juju */ 605 /* Need to set busy and send msg back */ 606 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 607 msg_info.hdr.status = CTL_SCSI_ERROR; 608 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 609 msg_info.scsi.sense_len = 0; 610 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 611 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 612 } 613 goto bailout; 614 } 615 ctl_zero_io(io); 616 // populate ctsio from msg_info 617 io->io_hdr.io_type = CTL_IO_SCSI; 618 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 619 io->io_hdr.original_sc = msg_info.hdr.original_sc; 620#if 0 621 printf("pOrig %x\n", (int)msg_info.original_sc); 622#endif 623 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 624 CTL_FLAG_IO_ACTIVE; 625 /* 626 * If we're in serialization-only mode, we don't 627 * want to go through full done processing. Thus 628 * the COPY flag. 629 * 630 * XXX KDM add another flag that is more specific. 631 */ 632 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 633 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 634 io->io_hdr.nexus = msg_info.hdr.nexus; 635#if 0 636 printf("targ %d, port %d, iid %d, lun %d\n", 637 io->io_hdr.nexus.targ_target.id, 638 io->io_hdr.nexus.targ_port, 639 io->io_hdr.nexus.initid.id, 640 io->io_hdr.nexus.targ_lun); 641#endif 642 io->scsiio.tag_num = msg_info.scsi.tag_num; 643 io->scsiio.tag_type = msg_info.scsi.tag_type; 644 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 645 CTL_MAX_CDBLEN); 646 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 647 const struct ctl_cmd_entry *entry; 648 649 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 650 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 651 io->io_hdr.flags |= 652 entry->flags & CTL_FLAG_DATA_MASK; 653 } 654 ctl_enqueue_isc(io); 655 break; 656 657 /* Performed on the Originating SC, XFER mode only */ 658 case CTL_MSG_DATAMOVE: { 659 struct ctl_sg_entry *sgl; 660 int i, j; 661 662 io = msg_info.hdr.original_sc; 663 if (io == NULL) { 664 printf("%s: original_sc == NULL!\n", __func__); 665 /* XXX KDM do something here */ 666 break; 667 } 668 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 669 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 670 /* 671 * Keep track of this, we need to send it back over 672 * when the datamove is complete. 673 */ 674 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 675 676 if (msg_info.dt.sg_sequence == 0) { 677 /* 678 * XXX KDM we use the preallocated S/G list 679 * here, but we'll need to change this to 680 * dynamic allocation if we need larger S/G 681 * lists. 682 */ 683 if (msg_info.dt.kern_sg_entries > 684 sizeof(io->io_hdr.remote_sglist) / 685 sizeof(io->io_hdr.remote_sglist[0])) { 686 printf("%s: number of S/G entries " 687 "needed %u > allocated num %zd\n", 688 __func__, 689 msg_info.dt.kern_sg_entries, 690 sizeof(io->io_hdr.remote_sglist)/ 691 sizeof(io->io_hdr.remote_sglist[0])); 692 693 /* 694 * XXX KDM send a message back to 695 * the other side to shut down the 696 * DMA. The error will come back 697 * through via the normal channel. 698 */ 699 break; 700 } 701 sgl = io->io_hdr.remote_sglist; 702 memset(sgl, 0, 703 sizeof(io->io_hdr.remote_sglist)); 704 705 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 706 707 io->scsiio.kern_sg_entries = 708 msg_info.dt.kern_sg_entries; 709 io->scsiio.rem_sg_entries = 710 msg_info.dt.kern_sg_entries; 711 io->scsiio.kern_data_len = 712 msg_info.dt.kern_data_len; 713 io->scsiio.kern_total_len = 714 msg_info.dt.kern_total_len; 715 io->scsiio.kern_data_resid = 716 msg_info.dt.kern_data_resid; 717 io->scsiio.kern_rel_offset = 718 msg_info.dt.kern_rel_offset; 719 /* 720 * Clear out per-DMA flags. 721 */ 722 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 723 /* 724 * Add per-DMA flags that are set for this 725 * particular DMA request. 726 */ 727 io->io_hdr.flags |= msg_info.dt.flags & 728 CTL_FLAG_RDMA_MASK; 729 } else 730 sgl = (struct ctl_sg_entry *) 731 io->scsiio.kern_data_ptr; 732 733 for (i = msg_info.dt.sent_sg_entries, j = 0; 734 i < (msg_info.dt.sent_sg_entries + 735 msg_info.dt.cur_sg_entries); i++, j++) { 736 sgl[i].addr = msg_info.dt.sg_list[j].addr; 737 sgl[i].len = msg_info.dt.sg_list[j].len; 738 739#if 0 740 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 741 __func__, 742 msg_info.dt.sg_list[j].addr, 743 msg_info.dt.sg_list[j].len, 744 sgl[i].addr, sgl[i].len, j, i); 745#endif 746 } 747#if 0 748 memcpy(&sgl[msg_info.dt.sent_sg_entries], 749 msg_info.dt.sg_list, 750 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 751#endif 752 753 /* 754 * If this is the last piece of the I/O, we've got 755 * the full S/G list. Queue processing in the thread. 756 * Otherwise wait for the next piece. 757 */ 758 if (msg_info.dt.sg_last != 0) 759 ctl_enqueue_isc(io); 760 break; 761 } 762 /* Performed on the Serializing (primary) SC, XFER mode only */ 763 case CTL_MSG_DATAMOVE_DONE: { 764 if (msg_info.hdr.serializing_sc == NULL) { 765 printf("%s: serializing_sc == NULL!\n", 766 __func__); 767 /* XXX KDM now what? */ 768 break; 769 } 770 /* 771 * We grab the sense information here in case 772 * there was a failure, so we can return status 773 * back to the initiator. 774 */ 775 io = msg_info.hdr.serializing_sc; 776 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 777 io->io_hdr.status = msg_info.hdr.status; 778 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 779 io->scsiio.sense_len = msg_info.scsi.sense_len; 780 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 781 io->io_hdr.port_status = msg_info.scsi.fetd_status; 782 io->scsiio.residual = msg_info.scsi.residual; 783 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 784 sizeof(io->scsiio.sense_data)); 785 ctl_enqueue_isc(io); 786 break; 787 } 788 789 /* Preformed on Originating SC, SER_ONLY mode */ 790 case CTL_MSG_R2R: 791 io = msg_info.hdr.original_sc; 792 if (io == NULL) { 793 printf("%s: Major Bummer\n", __func__); 794 return; 795 } else { 796#if 0 797 printf("pOrig %x\n",(int) ctsio); 798#endif 799 } 800 io->io_hdr.msg_type = CTL_MSG_R2R; 801 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 802 ctl_enqueue_isc(io); 803 break; 804 805 /* 806 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 807 * mode. 808 * Performed on the Originating (i.e. secondary) SC in XFER 809 * mode 810 */ 811 case CTL_MSG_FINISH_IO: 812 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 813 ctl_isc_handler_finish_xfer(ctl_softc, 814 &msg_info); 815 else 816 ctl_isc_handler_finish_ser_only(ctl_softc, 817 &msg_info); 818 break; 819 820 /* Preformed on Originating SC */ 821 case CTL_MSG_BAD_JUJU: 822 io = msg_info.hdr.original_sc; 823 if (io == NULL) { 824 printf("%s: Bad JUJU!, original_sc is NULL!\n", 825 __func__); 826 break; 827 } 828 ctl_copy_sense_data(&msg_info, io); 829 /* 830 * IO should have already been cleaned up on other 831 * SC so clear this flag so we won't send a message 832 * back to finish the IO there. 833 */ 834 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 835 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 836 837 /* io = msg_info.hdr.serializing_sc; */ 838 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 839 ctl_enqueue_isc(io); 840 break; 841 842 /* Handle resets sent from the other side */ 843 case CTL_MSG_MANAGE_TASKS: { 844 struct ctl_taskio *taskio; 845 taskio = (struct ctl_taskio *)ctl_alloc_io( 846 (void *)ctl_softc->othersc_pool); 847 if (taskio == NULL) { 848 printf("ctl_isc_event_handler: can't allocate " 849 "ctl_io!\n"); 850 /* Bad Juju */ 851 /* should I just call the proper reset func 852 here??? */ 853 goto bailout; 854 } 855 ctl_zero_io((union ctl_io *)taskio); 856 taskio->io_hdr.io_type = CTL_IO_TASK; 857 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 858 taskio->io_hdr.nexus = msg_info.hdr.nexus; 859 taskio->task_action = msg_info.task.task_action; 860 taskio->tag_num = msg_info.task.tag_num; 861 taskio->tag_type = msg_info.task.tag_type; 862#ifdef CTL_TIME_IO 863 taskio->io_hdr.start_time = time_uptime; 864 getbintime(&taskio->io_hdr.start_bt); 865#if 0 866 cs_prof_gettime(&taskio->io_hdr.start_ticks); 867#endif 868#endif /* CTL_TIME_IO */ 869 ctl_run_task((union ctl_io *)taskio); 870 break; 871 } 872 /* Persistent Reserve action which needs attention */ 873 case CTL_MSG_PERS_ACTION: 874 presio = (struct ctl_prio *)ctl_alloc_io( 875 (void *)ctl_softc->othersc_pool); 876 if (presio == NULL) { 877 printf("ctl_isc_event_handler: can't allocate " 878 "ctl_io!\n"); 879 /* Bad Juju */ 880 /* Need to set busy and send msg back */ 881 goto bailout; 882 } 883 ctl_zero_io((union ctl_io *)presio); 884 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 885 presio->pr_msg = msg_info.pr; 886 ctl_enqueue_isc((union ctl_io *)presio); 887 break; 888 case CTL_MSG_SYNC_FE: 889 rcv_sync_msg = 1; 890 break; 891 case CTL_MSG_APS_LOCK: { 892 // It's quicker to execute this then to 893 // queue it. 894 struct ctl_lun *lun; 895 struct ctl_page_index *page_index; 896 struct copan_aps_subpage *current_sp; 897 uint32_t targ_lun; 898 899 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 900 lun = ctl_softc->ctl_luns[targ_lun]; 901 mtx_lock(&lun->lun_lock); 902 page_index = &lun->mode_pages.index[index_to_aps_page]; 903 current_sp = (struct copan_aps_subpage *) 904 (page_index->page_data + 905 (page_index->page_len * CTL_PAGE_CURRENT)); 906 907 current_sp->lock_active = msg_info.aps.lock_flag; 908 mtx_unlock(&lun->lun_lock); 909 break; 910 } 911 default: 912 printf("How did I get here?\n"); 913 } 914 } else if (event == CTL_HA_EVT_MSG_SENT) { 915 if (param != CTL_HA_STATUS_SUCCESS) { 916 printf("Bad status from ctl_ha_msg_send status %d\n", 917 param); 918 } 919 return; 920 } else if (event == CTL_HA_EVT_DISCONNECT) { 921 printf("CTL: Got a disconnect from Isc\n"); 922 return; 923 } else { 924 printf("ctl_isc_event_handler: Unknown event %d\n", event); 925 return; 926 } 927 928bailout: 929 return; 930} 931 932static void 933ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 934{ 935 struct scsi_sense_data *sense; 936 937 sense = &dest->scsiio.sense_data; 938 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 939 dest->scsiio.scsi_status = src->scsi.scsi_status; 940 dest->scsiio.sense_len = src->scsi.sense_len; 941 dest->io_hdr.status = src->hdr.status; 942} 943 944static int 945ctl_init(void) 946{ 947 struct ctl_softc *softc; 948 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 949 struct ctl_port *port; 950 uint8_t sc_id =0; 951 int i, error, retval; 952 //int isc_retval; 953 954 retval = 0; 955 ctl_pause_rtr = 0; 956 rcv_sync_msg = 0; 957 958 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 959 M_WAITOK | M_ZERO); 960 softc = control_softc; 961 962 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 963 "cam/ctl"); 964 965 softc->dev->si_drv1 = softc; 966 967 /* 968 * By default, return a "bad LUN" peripheral qualifier for unknown 969 * LUNs. The user can override this default using the tunable or 970 * sysctl. See the comment in ctl_inquiry_std() for more details. 971 */ 972 softc->inquiry_pq_no_lun = 1; 973 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 974 &softc->inquiry_pq_no_lun); 975 sysctl_ctx_init(&softc->sysctl_ctx); 976 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 977 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 978 CTLFLAG_RD, 0, "CAM Target Layer"); 979 980 if (softc->sysctl_tree == NULL) { 981 printf("%s: unable to allocate sysctl tree\n", __func__); 982 destroy_dev(softc->dev); 983 free(control_softc, M_DEVBUF); 984 control_softc = NULL; 985 return (ENOMEM); 986 } 987 988 SYSCTL_ADD_INT(&softc->sysctl_ctx, 989 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 990 "inquiry_pq_no_lun", CTLFLAG_RW, 991 &softc->inquiry_pq_no_lun, 0, 992 "Report no lun possible for invalid LUNs"); 993 994 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 995 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 996 softc->open_count = 0; 997 998 /* 999 * Default to actually sending a SYNCHRONIZE CACHE command down to 1000 * the drive. 1001 */ 1002 softc->flags = CTL_FLAG_REAL_SYNC; 1003 1004 /* 1005 * In Copan's HA scheme, the "master" and "slave" roles are 1006 * figured out through the slot the controller is in. Although it 1007 * is an active/active system, someone has to be in charge. 1008 */ 1009#ifdef NEEDTOPORT 1010 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1011#endif 1012 1013 if (sc_id == 0) { 1014 softc->flags |= CTL_FLAG_MASTER_SHELF; 1015 persis_offset = 0; 1016 } else 1017 persis_offset = CTL_MAX_INITIATORS; 1018 1019 /* 1020 * XXX KDM need to figure out where we want to get our target ID 1021 * and WWID. Is it different on each port? 1022 */ 1023 softc->target.id = 0; 1024 softc->target.wwid[0] = 0x12345678; 1025 softc->target.wwid[1] = 0x87654321; 1026 STAILQ_INIT(&softc->lun_list); 1027 STAILQ_INIT(&softc->pending_lun_queue); 1028 STAILQ_INIT(&softc->fe_list); 1029 STAILQ_INIT(&softc->port_list); 1030 STAILQ_INIT(&softc->be_list); 1031 STAILQ_INIT(&softc->io_pools); 1032 ctl_tpc_init(softc); 1033 1034 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1035 &internal_pool)!= 0){ 1036 printf("ctl: can't allocate %d entry internal pool, " 1037 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1038 return (ENOMEM); 1039 } 1040 1041 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1042 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1043 printf("ctl: can't allocate %d entry emergency pool, " 1044 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1045 ctl_pool_free(internal_pool); 1046 return (ENOMEM); 1047 } 1048 1049 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1050 &other_pool) != 0) 1051 { 1052 printf("ctl: can't allocate %d entry other SC pool, " 1053 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1054 ctl_pool_free(internal_pool); 1055 ctl_pool_free(emergency_pool); 1056 return (ENOMEM); 1057 } 1058 1059 softc->internal_pool = internal_pool; 1060 softc->emergency_pool = emergency_pool; 1061 softc->othersc_pool = other_pool; 1062 1063 if (worker_threads <= 0) 1064 worker_threads = max(1, mp_ncpus / 4); 1065 if (worker_threads > CTL_MAX_THREADS) 1066 worker_threads = CTL_MAX_THREADS; 1067 1068 for (i = 0; i < worker_threads; i++) { 1069 struct ctl_thread *thr = &softc->threads[i]; 1070 1071 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1072 thr->ctl_softc = softc; 1073 STAILQ_INIT(&thr->incoming_queue); 1074 STAILQ_INIT(&thr->rtr_queue); 1075 STAILQ_INIT(&thr->done_queue); 1076 STAILQ_INIT(&thr->isc_queue); 1077 1078 error = kproc_kthread_add(ctl_work_thread, thr, 1079 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1080 if (error != 0) { 1081 printf("error creating CTL work thread!\n"); 1082 ctl_pool_free(internal_pool); 1083 ctl_pool_free(emergency_pool); 1084 ctl_pool_free(other_pool); 1085 return (error); 1086 } 1087 } 1088 error = kproc_kthread_add(ctl_lun_thread, softc, 1089 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1090 if (error != 0) { 1091 printf("error creating CTL lun thread!\n"); 1092 ctl_pool_free(internal_pool); 1093 ctl_pool_free(emergency_pool); 1094 ctl_pool_free(other_pool); 1095 return (error); 1096 } 1097 if (bootverbose) 1098 printf("ctl: CAM Target Layer loaded\n"); 1099 1100 /* 1101 * Initialize the ioctl front end. 1102 */ 1103 ctl_frontend_register(&ioctl_frontend); 1104 port = &softc->ioctl_info.port; 1105 port->frontend = &ioctl_frontend; 1106 sprintf(softc->ioctl_info.port_name, "ioctl"); 1107 port->port_type = CTL_PORT_IOCTL; 1108 port->num_requested_ctl_io = 100; 1109 port->port_name = softc->ioctl_info.port_name; 1110 port->port_online = ctl_ioctl_online; 1111 port->port_offline = ctl_ioctl_offline; 1112 port->onoff_arg = &softc->ioctl_info; 1113 port->lun_enable = ctl_ioctl_lun_enable; 1114 port->lun_disable = ctl_ioctl_lun_disable; 1115 port->targ_lun_arg = &softc->ioctl_info; 1116 port->fe_datamove = ctl_ioctl_datamove; 1117 port->fe_done = ctl_ioctl_done; 1118 port->max_targets = 15; 1119 port->max_target_id = 15; 1120 1121 if (ctl_port_register(&softc->ioctl_info.port, 1122 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1123 printf("ctl: ioctl front end registration failed, will " 1124 "continue anyway\n"); 1125 } 1126 1127#ifdef CTL_IO_DELAY 1128 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1129 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1130 sizeof(struct callout), CTL_TIMER_BYTES); 1131 return (EINVAL); 1132 } 1133#endif /* CTL_IO_DELAY */ 1134 1135 return (0); 1136} 1137 1138void 1139ctl_shutdown(void) 1140{ 1141 struct ctl_softc *softc; 1142 struct ctl_lun *lun, *next_lun; 1143 struct ctl_io_pool *pool; 1144 1145 softc = (struct ctl_softc *)control_softc; 1146 1147 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1148 printf("ctl: ioctl front end deregistration failed\n"); 1149 1150 mtx_lock(&softc->ctl_lock); 1151 1152 /* 1153 * Free up each LUN. 1154 */ 1155 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1156 next_lun = STAILQ_NEXT(lun, links); 1157 ctl_free_lun(lun); 1158 } 1159 1160 mtx_unlock(&softc->ctl_lock); 1161 1162 ctl_frontend_deregister(&ioctl_frontend); 1163 1164 /* 1165 * This will rip the rug out from under any FETDs or anyone else 1166 * that has a pool allocated. Since we increment our module 1167 * refcount any time someone outside the main CTL module allocates 1168 * a pool, we shouldn't have any problems here. The user won't be 1169 * able to unload the CTL module until client modules have 1170 * successfully unloaded. 1171 */ 1172 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1173 ctl_pool_free(pool); 1174 1175#if 0 1176 ctl_shutdown_thread(softc->work_thread); 1177 mtx_destroy(&softc->queue_lock); 1178#endif 1179 1180 ctl_tpc_shutdown(softc); 1181 mtx_destroy(&softc->pool_lock); 1182 mtx_destroy(&softc->ctl_lock); 1183 1184 destroy_dev(softc->dev); 1185 1186 sysctl_ctx_free(&softc->sysctl_ctx); 1187 1188 free(control_softc, M_DEVBUF); 1189 control_softc = NULL; 1190 1191 if (bootverbose) 1192 printf("ctl: CAM Target Layer unloaded\n"); 1193} 1194 1195static int 1196ctl_module_event_handler(module_t mod, int what, void *arg) 1197{ 1198 1199 switch (what) { 1200 case MOD_LOAD: 1201 return (ctl_init()); 1202 case MOD_UNLOAD: 1203 return (EBUSY); 1204 default: 1205 return (EOPNOTSUPP); 1206 } 1207} 1208 1209/* 1210 * XXX KDM should we do some access checks here? Bump a reference count to 1211 * prevent a CTL module from being unloaded while someone has it open? 1212 */ 1213static int 1214ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1215{ 1216 return (0); 1217} 1218 1219static int 1220ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1221{ 1222 return (0); 1223} 1224 1225int 1226ctl_port_enable(ctl_port_type port_type) 1227{ 1228 struct ctl_softc *softc; 1229 struct ctl_port *port; 1230 1231 if (ctl_is_single == 0) { 1232 union ctl_ha_msg msg_info; 1233 int isc_retval; 1234 1235#if 0 1236 printf("%s: HA mode, synchronizing frontend enable\n", 1237 __func__); 1238#endif 1239 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1240 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1241 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1242 printf("Sync msg send error retval %d\n", isc_retval); 1243 } 1244 if (!rcv_sync_msg) { 1245 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1246 sizeof(msg_info), 1); 1247 } 1248#if 0 1249 printf("CTL:Frontend Enable\n"); 1250 } else { 1251 printf("%s: single mode, skipping frontend synchronization\n", 1252 __func__); 1253#endif 1254 } 1255 1256 softc = control_softc; 1257 1258 STAILQ_FOREACH(port, &softc->port_list, links) { 1259 if (port_type & port->port_type) 1260 { 1261#if 0 1262 printf("port %d\n", port->targ_port); 1263#endif 1264 ctl_port_online(port); 1265 } 1266 } 1267 1268 return (0); 1269} 1270 1271int 1272ctl_port_disable(ctl_port_type port_type) 1273{ 1274 struct ctl_softc *softc; 1275 struct ctl_port *port; 1276 1277 softc = control_softc; 1278 1279 STAILQ_FOREACH(port, &softc->port_list, links) { 1280 if (port_type & port->port_type) 1281 ctl_port_offline(port); 1282 } 1283 1284 return (0); 1285} 1286 1287/* 1288 * Returns 0 for success, 1 for failure. 1289 * Currently the only failure mode is if there aren't enough entries 1290 * allocated. So, in case of a failure, look at num_entries_dropped, 1291 * reallocate and try again. 1292 */ 1293int 1294ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1295 int *num_entries_filled, int *num_entries_dropped, 1296 ctl_port_type port_type, int no_virtual) 1297{ 1298 struct ctl_softc *softc; 1299 struct ctl_port *port; 1300 int entries_dropped, entries_filled; 1301 int retval; 1302 int i; 1303 1304 softc = control_softc; 1305 1306 retval = 0; 1307 entries_filled = 0; 1308 entries_dropped = 0; 1309 1310 i = 0; 1311 mtx_lock(&softc->ctl_lock); 1312 STAILQ_FOREACH(port, &softc->port_list, links) { 1313 struct ctl_port_entry *entry; 1314 1315 if ((port->port_type & port_type) == 0) 1316 continue; 1317 1318 if ((no_virtual != 0) 1319 && (port->virtual_port != 0)) 1320 continue; 1321 1322 if (entries_filled >= num_entries_alloced) { 1323 entries_dropped++; 1324 continue; 1325 } 1326 entry = &entries[i]; 1327 1328 entry->port_type = port->port_type; 1329 strlcpy(entry->port_name, port->port_name, 1330 sizeof(entry->port_name)); 1331 entry->physical_port = port->physical_port; 1332 entry->virtual_port = port->virtual_port; 1333 entry->wwnn = port->wwnn; 1334 entry->wwpn = port->wwpn; 1335 1336 i++; 1337 entries_filled++; 1338 } 1339 1340 mtx_unlock(&softc->ctl_lock); 1341 1342 if (entries_dropped > 0) 1343 retval = 1; 1344 1345 *num_entries_dropped = entries_dropped; 1346 *num_entries_filled = entries_filled; 1347 1348 return (retval); 1349} 1350 1351static void 1352ctl_ioctl_online(void *arg) 1353{ 1354 struct ctl_ioctl_info *ioctl_info; 1355 1356 ioctl_info = (struct ctl_ioctl_info *)arg; 1357 1358 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1359} 1360 1361static void 1362ctl_ioctl_offline(void *arg) 1363{ 1364 struct ctl_ioctl_info *ioctl_info; 1365 1366 ioctl_info = (struct ctl_ioctl_info *)arg; 1367 1368 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1369} 1370 1371/* 1372 * Remove an initiator by port number and initiator ID. 1373 * Returns 0 for success, -1 for failure. 1374 */ 1375int 1376ctl_remove_initiator(struct ctl_port *port, int iid) 1377{ 1378 struct ctl_softc *softc = control_softc; 1379 1380 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1381 1382 if (iid > CTL_MAX_INIT_PER_PORT) { 1383 printf("%s: initiator ID %u > maximun %u!\n", 1384 __func__, iid, CTL_MAX_INIT_PER_PORT); 1385 return (-1); 1386 } 1387 1388 mtx_lock(&softc->ctl_lock); 1389 port->wwpn_iid[iid].in_use--; 1390 port->wwpn_iid[iid].last_use = time_uptime; 1391 mtx_unlock(&softc->ctl_lock); 1392 1393 return (0); 1394} 1395 1396/* 1397 * Add an initiator to the initiator map. 1398 * Returns iid for success, < 0 for failure. 1399 */ 1400int 1401ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1402{ 1403 struct ctl_softc *softc = control_softc; 1404 time_t best_time; 1405 int i, best; 1406 1407 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1408 1409 if (iid >= CTL_MAX_INIT_PER_PORT) { 1410 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1411 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1412 free(name, M_CTL); 1413 return (-1); 1414 } 1415 1416 mtx_lock(&softc->ctl_lock); 1417 1418 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1419 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1420 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1421 iid = i; 1422 break; 1423 } 1424 if (name != NULL && port->wwpn_iid[i].name != NULL && 1425 strcmp(name, port->wwpn_iid[i].name) == 0) { 1426 iid = i; 1427 break; 1428 } 1429 } 1430 } 1431 1432 if (iid < 0) { 1433 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1434 if (port->wwpn_iid[i].in_use == 0 && 1435 port->wwpn_iid[i].wwpn == 0 && 1436 port->wwpn_iid[i].name == NULL) { 1437 iid = i; 1438 break; 1439 } 1440 } 1441 } 1442 1443 if (iid < 0) { 1444 best = -1; 1445 best_time = INT32_MAX; 1446 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1447 if (port->wwpn_iid[i].in_use == 0) { 1448 if (port->wwpn_iid[i].last_use < best_time) { 1449 best = i; 1450 best_time = port->wwpn_iid[i].last_use; 1451 } 1452 } 1453 } 1454 iid = best; 1455 } 1456 1457 if (iid < 0) { 1458 mtx_unlock(&softc->ctl_lock); 1459 free(name, M_CTL); 1460 return (-2); 1461 } 1462 1463 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1464 /* 1465 * This is not an error yet. 1466 */ 1467 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1468#if 0 1469 printf("%s: port %d iid %u WWPN %#jx arrived" 1470 " again\n", __func__, port->targ_port, 1471 iid, (uintmax_t)wwpn); 1472#endif 1473 goto take; 1474 } 1475 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1476 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1477#if 0 1478 printf("%s: port %d iid %u name '%s' arrived" 1479 " again\n", __func__, port->targ_port, 1480 iid, name); 1481#endif 1482 goto take; 1483 } 1484 1485 /* 1486 * This is an error, but what do we do about it? The 1487 * driver is telling us we have a new WWPN for this 1488 * initiator ID, so we pretty much need to use it. 1489 */ 1490 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1491 " but WWPN %#jx '%s' is still at that address\n", 1492 __func__, port->targ_port, iid, wwpn, name, 1493 (uintmax_t)port->wwpn_iid[iid].wwpn, 1494 port->wwpn_iid[iid].name); 1495 1496 /* 1497 * XXX KDM clear have_ca and ua_pending on each LUN for 1498 * this initiator. 1499 */ 1500 } 1501take: 1502 free(port->wwpn_iid[iid].name, M_CTL); 1503 port->wwpn_iid[iid].name = name; 1504 port->wwpn_iid[iid].wwpn = wwpn; 1505 port->wwpn_iid[iid].in_use++; 1506 mtx_unlock(&softc->ctl_lock); 1507 1508 return (iid); 1509} 1510 1511static int 1512ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1513{ 1514 int len; 1515 1516 switch (port->port_type) { 1517 case CTL_PORT_FC: 1518 { 1519 struct scsi_transportid_fcp *id = 1520 (struct scsi_transportid_fcp *)buf; 1521 if (port->wwpn_iid[iid].wwpn == 0) 1522 return (0); 1523 memset(id, 0, sizeof(*id)); 1524 id->format_protocol = SCSI_PROTO_FC; 1525 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1526 return (sizeof(*id)); 1527 } 1528 case CTL_PORT_ISCSI: 1529 { 1530 struct scsi_transportid_iscsi_port *id = 1531 (struct scsi_transportid_iscsi_port *)buf; 1532 if (port->wwpn_iid[iid].name == NULL) 1533 return (0); 1534 memset(id, 0, 256); 1535 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1536 SCSI_PROTO_ISCSI; 1537 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1538 len = roundup2(min(len, 252), 4); 1539 scsi_ulto2b(len, id->additional_length); 1540 return (sizeof(*id) + len); 1541 } 1542 case CTL_PORT_SAS: 1543 { 1544 struct scsi_transportid_sas *id = 1545 (struct scsi_transportid_sas *)buf; 1546 if (port->wwpn_iid[iid].wwpn == 0) 1547 return (0); 1548 memset(id, 0, sizeof(*id)); 1549 id->format_protocol = SCSI_PROTO_SAS; 1550 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1551 return (sizeof(*id)); 1552 } 1553 default: 1554 { 1555 struct scsi_transportid_spi *id = 1556 (struct scsi_transportid_spi *)buf; 1557 memset(id, 0, sizeof(*id)); 1558 id->format_protocol = SCSI_PROTO_SPI; 1559 scsi_ulto2b(iid, id->scsi_addr); 1560 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1561 return (sizeof(*id)); 1562 } 1563 } 1564} 1565 1566static int 1567ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1568{ 1569 return (0); 1570} 1571 1572static int 1573ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1574{ 1575 return (0); 1576} 1577 1578/* 1579 * Data movement routine for the CTL ioctl frontend port. 1580 */ 1581static int 1582ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1583{ 1584 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1585 struct ctl_sg_entry ext_entry, kern_entry; 1586 int ext_sglen, ext_sg_entries, kern_sg_entries; 1587 int ext_sg_start, ext_offset; 1588 int len_to_copy, len_copied; 1589 int kern_watermark, ext_watermark; 1590 int ext_sglist_malloced; 1591 int i, j; 1592 1593 ext_sglist_malloced = 0; 1594 ext_sg_start = 0; 1595 ext_offset = 0; 1596 1597 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1598 1599 /* 1600 * If this flag is set, fake the data transfer. 1601 */ 1602 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1603 ctsio->ext_data_filled = ctsio->ext_data_len; 1604 goto bailout; 1605 } 1606 1607 /* 1608 * To simplify things here, if we have a single buffer, stick it in 1609 * a S/G entry and just make it a single entry S/G list. 1610 */ 1611 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1612 int len_seen; 1613 1614 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1615 1616 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1617 M_WAITOK); 1618 ext_sglist_malloced = 1; 1619 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1620 ext_sglen) != 0) { 1621 ctl_set_internal_failure(ctsio, 1622 /*sks_valid*/ 0, 1623 /*retry_count*/ 0); 1624 goto bailout; 1625 } 1626 ext_sg_entries = ctsio->ext_sg_entries; 1627 len_seen = 0; 1628 for (i = 0; i < ext_sg_entries; i++) { 1629 if ((len_seen + ext_sglist[i].len) >= 1630 ctsio->ext_data_filled) { 1631 ext_sg_start = i; 1632 ext_offset = ctsio->ext_data_filled - len_seen; 1633 break; 1634 } 1635 len_seen += ext_sglist[i].len; 1636 } 1637 } else { 1638 ext_sglist = &ext_entry; 1639 ext_sglist->addr = ctsio->ext_data_ptr; 1640 ext_sglist->len = ctsio->ext_data_len; 1641 ext_sg_entries = 1; 1642 ext_sg_start = 0; 1643 ext_offset = ctsio->ext_data_filled; 1644 } 1645 1646 if (ctsio->kern_sg_entries > 0) { 1647 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1648 kern_sg_entries = ctsio->kern_sg_entries; 1649 } else { 1650 kern_sglist = &kern_entry; 1651 kern_sglist->addr = ctsio->kern_data_ptr; 1652 kern_sglist->len = ctsio->kern_data_len; 1653 kern_sg_entries = 1; 1654 } 1655 1656 1657 kern_watermark = 0; 1658 ext_watermark = ext_offset; 1659 len_copied = 0; 1660 for (i = ext_sg_start, j = 0; 1661 i < ext_sg_entries && j < kern_sg_entries;) { 1662 uint8_t *ext_ptr, *kern_ptr; 1663 1664 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1665 kern_sglist[j].len - kern_watermark); 1666 1667 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1668 ext_ptr = ext_ptr + ext_watermark; 1669 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1670 /* 1671 * XXX KDM fix this! 1672 */ 1673 panic("need to implement bus address support"); 1674#if 0 1675 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1676#endif 1677 } else 1678 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1679 kern_ptr = kern_ptr + kern_watermark; 1680 1681 kern_watermark += len_to_copy; 1682 ext_watermark += len_to_copy; 1683 1684 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1685 CTL_FLAG_DATA_IN) { 1686 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1687 "bytes to user\n", len_to_copy)); 1688 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1689 "to %p\n", kern_ptr, ext_ptr)); 1690 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1691 ctl_set_internal_failure(ctsio, 1692 /*sks_valid*/ 0, 1693 /*retry_count*/ 0); 1694 goto bailout; 1695 } 1696 } else { 1697 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1698 "bytes from user\n", len_to_copy)); 1699 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1700 "to %p\n", ext_ptr, kern_ptr)); 1701 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1702 ctl_set_internal_failure(ctsio, 1703 /*sks_valid*/ 0, 1704 /*retry_count*/0); 1705 goto bailout; 1706 } 1707 } 1708 1709 len_copied += len_to_copy; 1710 1711 if (ext_sglist[i].len == ext_watermark) { 1712 i++; 1713 ext_watermark = 0; 1714 } 1715 1716 if (kern_sglist[j].len == kern_watermark) { 1717 j++; 1718 kern_watermark = 0; 1719 } 1720 } 1721 1722 ctsio->ext_data_filled += len_copied; 1723 1724 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1725 "kern_sg_entries: %d\n", ext_sg_entries, 1726 kern_sg_entries)); 1727 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1728 "kern_data_len = %d\n", ctsio->ext_data_len, 1729 ctsio->kern_data_len)); 1730 1731 1732 /* XXX KDM set residual?? */ 1733bailout: 1734 1735 if (ext_sglist_malloced != 0) 1736 free(ext_sglist, M_CTL); 1737 1738 return (CTL_RETVAL_COMPLETE); 1739} 1740 1741/* 1742 * Serialize a command that went down the "wrong" side, and so was sent to 1743 * this controller for execution. The logic is a little different than the 1744 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1745 * sent back to the other side, but in the success case, we execute the 1746 * command on this side (XFER mode) or tell the other side to execute it 1747 * (SER_ONLY mode). 1748 */ 1749static int 1750ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1751{ 1752 struct ctl_softc *ctl_softc; 1753 union ctl_ha_msg msg_info; 1754 struct ctl_lun *lun; 1755 int retval = 0; 1756 uint32_t targ_lun; 1757 1758 ctl_softc = control_softc; 1759 1760 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1761 lun = ctl_softc->ctl_luns[targ_lun]; 1762 if (lun==NULL) 1763 { 1764 /* 1765 * Why isn't LUN defined? The other side wouldn't 1766 * send a cmd if the LUN is undefined. 1767 */ 1768 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1769 1770 /* "Logical unit not supported" */ 1771 ctl_set_sense_data(&msg_info.scsi.sense_data, 1772 lun, 1773 /*sense_format*/SSD_TYPE_NONE, 1774 /*current_error*/ 1, 1775 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1776 /*asc*/ 0x25, 1777 /*ascq*/ 0x00, 1778 SSD_ELEM_NONE); 1779 1780 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1781 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1782 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1783 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1784 msg_info.hdr.serializing_sc = NULL; 1785 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1786 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1787 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1788 } 1789 return(1); 1790 1791 } 1792 1793 mtx_lock(&lun->lun_lock); 1794 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1795 1796 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1797 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1798 ooa_links))) { 1799 case CTL_ACTION_BLOCK: 1800 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1801 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1802 blocked_links); 1803 break; 1804 case CTL_ACTION_PASS: 1805 case CTL_ACTION_SKIP: 1806 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1807 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1808 ctl_enqueue_rtr((union ctl_io *)ctsio); 1809 } else { 1810 1811 /* send msg back to other side */ 1812 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1813 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1814 msg_info.hdr.msg_type = CTL_MSG_R2R; 1815#if 0 1816 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1817#endif 1818 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1819 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1820 } 1821 } 1822 break; 1823 case CTL_ACTION_OVERLAP: 1824 /* OVERLAPPED COMMANDS ATTEMPTED */ 1825 ctl_set_sense_data(&msg_info.scsi.sense_data, 1826 lun, 1827 /*sense_format*/SSD_TYPE_NONE, 1828 /*current_error*/ 1, 1829 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1830 /*asc*/ 0x4E, 1831 /*ascq*/ 0x00, 1832 SSD_ELEM_NONE); 1833 1834 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1835 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1836 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1837 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1838 msg_info.hdr.serializing_sc = NULL; 1839 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1840#if 0 1841 printf("BAD JUJU:Major Bummer Overlap\n"); 1842#endif 1843 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1844 retval = 1; 1845 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1846 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1847 } 1848 break; 1849 case CTL_ACTION_OVERLAP_TAG: 1850 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1851 ctl_set_sense_data(&msg_info.scsi.sense_data, 1852 lun, 1853 /*sense_format*/SSD_TYPE_NONE, 1854 /*current_error*/ 1, 1855 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1856 /*asc*/ 0x4D, 1857 /*ascq*/ ctsio->tag_num & 0xff, 1858 SSD_ELEM_NONE); 1859 1860 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1861 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1862 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1863 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1864 msg_info.hdr.serializing_sc = NULL; 1865 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1866#if 0 1867 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1868#endif 1869 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1870 retval = 1; 1871 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1872 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1873 } 1874 break; 1875 case CTL_ACTION_ERROR: 1876 default: 1877 /* "Internal target failure" */ 1878 ctl_set_sense_data(&msg_info.scsi.sense_data, 1879 lun, 1880 /*sense_format*/SSD_TYPE_NONE, 1881 /*current_error*/ 1, 1882 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1883 /*asc*/ 0x44, 1884 /*ascq*/ 0x00, 1885 SSD_ELEM_NONE); 1886 1887 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1888 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1889 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1890 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1891 msg_info.hdr.serializing_sc = NULL; 1892 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1893#if 0 1894 printf("BAD JUJU:Major Bummer HW Error\n"); 1895#endif 1896 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1897 retval = 1; 1898 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1899 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1900 } 1901 break; 1902 } 1903 mtx_unlock(&lun->lun_lock); 1904 return (retval); 1905} 1906 1907static int 1908ctl_ioctl_submit_wait(union ctl_io *io) 1909{ 1910 struct ctl_fe_ioctl_params params; 1911 ctl_fe_ioctl_state last_state; 1912 int done, retval; 1913 1914 retval = 0; 1915 1916 bzero(¶ms, sizeof(params)); 1917 1918 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1919 cv_init(¶ms.sem, "ctlioccv"); 1920 params.state = CTL_IOCTL_INPROG; 1921 last_state = params.state; 1922 1923 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1924 1925 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1926 1927 /* This shouldn't happen */ 1928 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1929 return (retval); 1930 1931 done = 0; 1932 1933 do { 1934 mtx_lock(¶ms.ioctl_mtx); 1935 /* 1936 * Check the state here, and don't sleep if the state has 1937 * already changed (i.e. wakeup has already occured, but we 1938 * weren't waiting yet). 1939 */ 1940 if (params.state == last_state) { 1941 /* XXX KDM cv_wait_sig instead? */ 1942 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1943 } 1944 last_state = params.state; 1945 1946 switch (params.state) { 1947 case CTL_IOCTL_INPROG: 1948 /* Why did we wake up? */ 1949 /* XXX KDM error here? */ 1950 mtx_unlock(¶ms.ioctl_mtx); 1951 break; 1952 case CTL_IOCTL_DATAMOVE: 1953 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1954 1955 /* 1956 * change last_state back to INPROG to avoid 1957 * deadlock on subsequent data moves. 1958 */ 1959 params.state = last_state = CTL_IOCTL_INPROG; 1960 1961 mtx_unlock(¶ms.ioctl_mtx); 1962 ctl_ioctl_do_datamove(&io->scsiio); 1963 /* 1964 * Note that in some cases, most notably writes, 1965 * this will queue the I/O and call us back later. 1966 * In other cases, generally reads, this routine 1967 * will immediately call back and wake us up, 1968 * probably using our own context. 1969 */ 1970 io->scsiio.be_move_done(io); 1971 break; 1972 case CTL_IOCTL_DONE: 1973 mtx_unlock(¶ms.ioctl_mtx); 1974 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1975 done = 1; 1976 break; 1977 default: 1978 mtx_unlock(¶ms.ioctl_mtx); 1979 /* XXX KDM error here? */ 1980 break; 1981 } 1982 } while (done == 0); 1983 1984 mtx_destroy(¶ms.ioctl_mtx); 1985 cv_destroy(¶ms.sem); 1986 1987 return (CTL_RETVAL_COMPLETE); 1988} 1989 1990static void 1991ctl_ioctl_datamove(union ctl_io *io) 1992{ 1993 struct ctl_fe_ioctl_params *params; 1994 1995 params = (struct ctl_fe_ioctl_params *) 1996 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1997 1998 mtx_lock(¶ms->ioctl_mtx); 1999 params->state = CTL_IOCTL_DATAMOVE; 2000 cv_broadcast(¶ms->sem); 2001 mtx_unlock(¶ms->ioctl_mtx); 2002} 2003 2004static void 2005ctl_ioctl_done(union ctl_io *io) 2006{ 2007 struct ctl_fe_ioctl_params *params; 2008 2009 params = (struct ctl_fe_ioctl_params *) 2010 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2011 2012 mtx_lock(¶ms->ioctl_mtx); 2013 params->state = CTL_IOCTL_DONE; 2014 cv_broadcast(¶ms->sem); 2015 mtx_unlock(¶ms->ioctl_mtx); 2016} 2017 2018static void 2019ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2020{ 2021 struct ctl_fe_ioctl_startstop_info *sd_info; 2022 2023 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2024 2025 sd_info->hs_info.status = metatask->status; 2026 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2027 sd_info->hs_info.luns_complete = 2028 metatask->taskinfo.startstop.luns_complete; 2029 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2030 2031 cv_broadcast(&sd_info->sem); 2032} 2033 2034static void 2035ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2036{ 2037 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2038 2039 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2040 2041 mtx_lock(fe_bbr_info->lock); 2042 fe_bbr_info->bbr_info->status = metatask->status; 2043 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2044 fe_bbr_info->wakeup_done = 1; 2045 mtx_unlock(fe_bbr_info->lock); 2046 2047 cv_broadcast(&fe_bbr_info->sem); 2048} 2049 2050/* 2051 * Returns 0 for success, errno for failure. 2052 */ 2053static int 2054ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2055 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2056{ 2057 union ctl_io *io; 2058 int retval; 2059 2060 retval = 0; 2061 2062 mtx_lock(&lun->lun_lock); 2063 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2064 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2065 ooa_links)) { 2066 struct ctl_ooa_entry *entry; 2067 2068 /* 2069 * If we've got more than we can fit, just count the 2070 * remaining entries. 2071 */ 2072 if (*cur_fill_num >= ooa_hdr->alloc_num) 2073 continue; 2074 2075 entry = &kern_entries[*cur_fill_num]; 2076 2077 entry->tag_num = io->scsiio.tag_num; 2078 entry->lun_num = lun->lun; 2079#ifdef CTL_TIME_IO 2080 entry->start_bt = io->io_hdr.start_bt; 2081#endif 2082 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2083 entry->cdb_len = io->scsiio.cdb_len; 2084 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2085 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2086 2087 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2088 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2089 2090 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2091 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2092 2093 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2094 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2095 2096 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2097 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2098 } 2099 mtx_unlock(&lun->lun_lock); 2100 2101 return (retval); 2102} 2103 2104static void * 2105ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2106 size_t error_str_len) 2107{ 2108 void *kptr; 2109 2110 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2111 2112 if (copyin(user_addr, kptr, len) != 0) { 2113 snprintf(error_str, error_str_len, "Error copying %d bytes " 2114 "from user address %p to kernel address %p", len, 2115 user_addr, kptr); 2116 free(kptr, M_CTL); 2117 return (NULL); 2118 } 2119 2120 return (kptr); 2121} 2122 2123static void 2124ctl_free_args(int num_args, struct ctl_be_arg *args) 2125{ 2126 int i; 2127 2128 if (args == NULL) 2129 return; 2130 2131 for (i = 0; i < num_args; i++) { 2132 free(args[i].kname, M_CTL); 2133 free(args[i].kvalue, M_CTL); 2134 } 2135 2136 free(args, M_CTL); 2137} 2138 2139static struct ctl_be_arg * 2140ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2141 char *error_str, size_t error_str_len) 2142{ 2143 struct ctl_be_arg *args; 2144 int i; 2145 2146 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2147 error_str, error_str_len); 2148 2149 if (args == NULL) 2150 goto bailout; 2151 2152 for (i = 0; i < num_args; i++) { 2153 args[i].kname = NULL; 2154 args[i].kvalue = NULL; 2155 } 2156 2157 for (i = 0; i < num_args; i++) { 2158 uint8_t *tmpptr; 2159 2160 args[i].kname = ctl_copyin_alloc(args[i].name, 2161 args[i].namelen, error_str, error_str_len); 2162 if (args[i].kname == NULL) 2163 goto bailout; 2164 2165 if (args[i].kname[args[i].namelen - 1] != '\0') { 2166 snprintf(error_str, error_str_len, "Argument %d " 2167 "name is not NUL-terminated", i); 2168 goto bailout; 2169 } 2170 2171 if (args[i].flags & CTL_BEARG_RD) { 2172 tmpptr = ctl_copyin_alloc(args[i].value, 2173 args[i].vallen, error_str, error_str_len); 2174 if (tmpptr == NULL) 2175 goto bailout; 2176 if ((args[i].flags & CTL_BEARG_ASCII) 2177 && (tmpptr[args[i].vallen - 1] != '\0')) { 2178 snprintf(error_str, error_str_len, "Argument " 2179 "%d value is not NUL-terminated", i); 2180 goto bailout; 2181 } 2182 args[i].kvalue = tmpptr; 2183 } else { 2184 args[i].kvalue = malloc(args[i].vallen, 2185 M_CTL, M_WAITOK | M_ZERO); 2186 } 2187 } 2188 2189 return (args); 2190bailout: 2191 2192 ctl_free_args(num_args, args); 2193 2194 return (NULL); 2195} 2196 2197static void 2198ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2199{ 2200 int i; 2201 2202 for (i = 0; i < num_args; i++) { 2203 if (args[i].flags & CTL_BEARG_WR) 2204 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2205 } 2206} 2207 2208/* 2209 * Escape characters that are illegal or not recommended in XML. 2210 */ 2211int 2212ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2213{ 2214 int retval; 2215 2216 retval = 0; 2217 2218 for (; *str; str++) { 2219 switch (*str) { 2220 case '&': 2221 retval = sbuf_printf(sb, "&"); 2222 break; 2223 case '>': 2224 retval = sbuf_printf(sb, ">"); 2225 break; 2226 case '<': 2227 retval = sbuf_printf(sb, "<"); 2228 break; 2229 default: 2230 retval = sbuf_putc(sb, *str); 2231 break; 2232 } 2233 2234 if (retval != 0) 2235 break; 2236 2237 } 2238 2239 return (retval); 2240} 2241 2242static int 2243ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2244 struct thread *td) 2245{ 2246 struct ctl_softc *softc; 2247 int retval; 2248 2249 softc = control_softc; 2250 2251 retval = 0; 2252 2253 switch (cmd) { 2254 case CTL_IO: { 2255 union ctl_io *io; 2256 void *pool_tmp; 2257 2258 /* 2259 * If we haven't been "enabled", don't allow any SCSI I/O 2260 * to this FETD. 2261 */ 2262 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2263 retval = EPERM; 2264 break; 2265 } 2266 2267 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2268 if (io == NULL) { 2269 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2270 retval = ENOSPC; 2271 break; 2272 } 2273 2274 /* 2275 * Need to save the pool reference so it doesn't get 2276 * spammed by the user's ctl_io. 2277 */ 2278 pool_tmp = io->io_hdr.pool; 2279 2280 memcpy(io, (void *)addr, sizeof(*io)); 2281 2282 io->io_hdr.pool = pool_tmp; 2283 /* 2284 * No status yet, so make sure the status is set properly. 2285 */ 2286 io->io_hdr.status = CTL_STATUS_NONE; 2287 2288 /* 2289 * The user sets the initiator ID, target and LUN IDs. 2290 */ 2291 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2292 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2293 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2294 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2295 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2296 2297 retval = ctl_ioctl_submit_wait(io); 2298 2299 if (retval != 0) { 2300 ctl_free_io(io); 2301 break; 2302 } 2303 2304 memcpy((void *)addr, io, sizeof(*io)); 2305 2306 /* return this to our pool */ 2307 ctl_free_io(io); 2308 2309 break; 2310 } 2311 case CTL_ENABLE_PORT: 2312 case CTL_DISABLE_PORT: 2313 case CTL_SET_PORT_WWNS: { 2314 struct ctl_port *port; 2315 struct ctl_port_entry *entry; 2316 2317 entry = (struct ctl_port_entry *)addr; 2318 2319 mtx_lock(&softc->ctl_lock); 2320 STAILQ_FOREACH(port, &softc->port_list, links) { 2321 int action, done; 2322 2323 action = 0; 2324 done = 0; 2325 2326 if ((entry->port_type == CTL_PORT_NONE) 2327 && (entry->targ_port == port->targ_port)) { 2328 /* 2329 * If the user only wants to enable or 2330 * disable or set WWNs on a specific port, 2331 * do the operation and we're done. 2332 */ 2333 action = 1; 2334 done = 1; 2335 } else if (entry->port_type & port->port_type) { 2336 /* 2337 * Compare the user's type mask with the 2338 * particular frontend type to see if we 2339 * have a match. 2340 */ 2341 action = 1; 2342 done = 0; 2343 2344 /* 2345 * Make sure the user isn't trying to set 2346 * WWNs on multiple ports at the same time. 2347 */ 2348 if (cmd == CTL_SET_PORT_WWNS) { 2349 printf("%s: Can't set WWNs on " 2350 "multiple ports\n", __func__); 2351 retval = EINVAL; 2352 break; 2353 } 2354 } 2355 if (action != 0) { 2356 /* 2357 * XXX KDM we have to drop the lock here, 2358 * because the online/offline operations 2359 * can potentially block. We need to 2360 * reference count the frontends so they 2361 * can't go away, 2362 */ 2363 mtx_unlock(&softc->ctl_lock); 2364 2365 if (cmd == CTL_ENABLE_PORT) { 2366 struct ctl_lun *lun; 2367 2368 STAILQ_FOREACH(lun, &softc->lun_list, 2369 links) { 2370 port->lun_enable(port->targ_lun_arg, 2371 lun->target, 2372 lun->lun); 2373 } 2374 2375 ctl_port_online(port); 2376 } else if (cmd == CTL_DISABLE_PORT) { 2377 struct ctl_lun *lun; 2378 2379 ctl_port_offline(port); 2380 2381 STAILQ_FOREACH(lun, &softc->lun_list, 2382 links) { 2383 port->lun_disable( 2384 port->targ_lun_arg, 2385 lun->target, 2386 lun->lun); 2387 } 2388 } 2389 2390 mtx_lock(&softc->ctl_lock); 2391 2392 if (cmd == CTL_SET_PORT_WWNS) 2393 ctl_port_set_wwns(port, 2394 (entry->flags & CTL_PORT_WWNN_VALID) ? 2395 1 : 0, entry->wwnn, 2396 (entry->flags & CTL_PORT_WWPN_VALID) ? 2397 1 : 0, entry->wwpn); 2398 } 2399 if (done != 0) 2400 break; 2401 } 2402 mtx_unlock(&softc->ctl_lock); 2403 break; 2404 } 2405 case CTL_GET_PORT_LIST: { 2406 struct ctl_port *port; 2407 struct ctl_port_list *list; 2408 int i; 2409 2410 list = (struct ctl_port_list *)addr; 2411 2412 if (list->alloc_len != (list->alloc_num * 2413 sizeof(struct ctl_port_entry))) { 2414 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2415 "alloc_num %u * sizeof(struct ctl_port_entry) " 2416 "%zu\n", __func__, list->alloc_len, 2417 list->alloc_num, sizeof(struct ctl_port_entry)); 2418 retval = EINVAL; 2419 break; 2420 } 2421 list->fill_len = 0; 2422 list->fill_num = 0; 2423 list->dropped_num = 0; 2424 i = 0; 2425 mtx_lock(&softc->ctl_lock); 2426 STAILQ_FOREACH(port, &softc->port_list, links) { 2427 struct ctl_port_entry entry, *list_entry; 2428 2429 if (list->fill_num >= list->alloc_num) { 2430 list->dropped_num++; 2431 continue; 2432 } 2433 2434 entry.port_type = port->port_type; 2435 strlcpy(entry.port_name, port->port_name, 2436 sizeof(entry.port_name)); 2437 entry.targ_port = port->targ_port; 2438 entry.physical_port = port->physical_port; 2439 entry.virtual_port = port->virtual_port; 2440 entry.wwnn = port->wwnn; 2441 entry.wwpn = port->wwpn; 2442 if (port->status & CTL_PORT_STATUS_ONLINE) 2443 entry.online = 1; 2444 else 2445 entry.online = 0; 2446 2447 list_entry = &list->entries[i]; 2448 2449 retval = copyout(&entry, list_entry, sizeof(entry)); 2450 if (retval != 0) { 2451 printf("%s: CTL_GET_PORT_LIST: copyout " 2452 "returned %d\n", __func__, retval); 2453 break; 2454 } 2455 i++; 2456 list->fill_num++; 2457 list->fill_len += sizeof(entry); 2458 } 2459 mtx_unlock(&softc->ctl_lock); 2460 2461 /* 2462 * If this is non-zero, we had a copyout fault, so there's 2463 * probably no point in attempting to set the status inside 2464 * the structure. 2465 */ 2466 if (retval != 0) 2467 break; 2468 2469 if (list->dropped_num > 0) 2470 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2471 else 2472 list->status = CTL_PORT_LIST_OK; 2473 break; 2474 } 2475 case CTL_DUMP_OOA: { 2476 struct ctl_lun *lun; 2477 union ctl_io *io; 2478 char printbuf[128]; 2479 struct sbuf sb; 2480 2481 mtx_lock(&softc->ctl_lock); 2482 printf("Dumping OOA queues:\n"); 2483 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2484 mtx_lock(&lun->lun_lock); 2485 for (io = (union ctl_io *)TAILQ_FIRST( 2486 &lun->ooa_queue); io != NULL; 2487 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2488 ooa_links)) { 2489 sbuf_new(&sb, printbuf, sizeof(printbuf), 2490 SBUF_FIXEDLEN); 2491 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2492 (intmax_t)lun->lun, 2493 io->scsiio.tag_num, 2494 (io->io_hdr.flags & 2495 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2496 (io->io_hdr.flags & 2497 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2498 (io->io_hdr.flags & 2499 CTL_FLAG_ABORT) ? " ABORT" : "", 2500 (io->io_hdr.flags & 2501 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2502 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2503 sbuf_finish(&sb); 2504 printf("%s\n", sbuf_data(&sb)); 2505 } 2506 mtx_unlock(&lun->lun_lock); 2507 } 2508 printf("OOA queues dump done\n"); 2509 mtx_unlock(&softc->ctl_lock); 2510 break; 2511 } 2512 case CTL_GET_OOA: { 2513 struct ctl_lun *lun; 2514 struct ctl_ooa *ooa_hdr; 2515 struct ctl_ooa_entry *entries; 2516 uint32_t cur_fill_num; 2517 2518 ooa_hdr = (struct ctl_ooa *)addr; 2519 2520 if ((ooa_hdr->alloc_len == 0) 2521 || (ooa_hdr->alloc_num == 0)) { 2522 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2523 "must be non-zero\n", __func__, 2524 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2525 retval = EINVAL; 2526 break; 2527 } 2528 2529 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2530 sizeof(struct ctl_ooa_entry))) { 2531 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2532 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2533 __func__, ooa_hdr->alloc_len, 2534 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2535 retval = EINVAL; 2536 break; 2537 } 2538 2539 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2540 if (entries == NULL) { 2541 printf("%s: could not allocate %d bytes for OOA " 2542 "dump\n", __func__, ooa_hdr->alloc_len); 2543 retval = ENOMEM; 2544 break; 2545 } 2546 2547 mtx_lock(&softc->ctl_lock); 2548 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2549 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2550 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2551 mtx_unlock(&softc->ctl_lock); 2552 free(entries, M_CTL); 2553 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2554 __func__, (uintmax_t)ooa_hdr->lun_num); 2555 retval = EINVAL; 2556 break; 2557 } 2558 2559 cur_fill_num = 0; 2560 2561 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2562 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2563 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2564 ooa_hdr, entries); 2565 if (retval != 0) 2566 break; 2567 } 2568 if (retval != 0) { 2569 mtx_unlock(&softc->ctl_lock); 2570 free(entries, M_CTL); 2571 break; 2572 } 2573 } else { 2574 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2575 2576 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2577 entries); 2578 } 2579 mtx_unlock(&softc->ctl_lock); 2580 2581 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2582 ooa_hdr->fill_len = ooa_hdr->fill_num * 2583 sizeof(struct ctl_ooa_entry); 2584 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2585 if (retval != 0) { 2586 printf("%s: error copying out %d bytes for OOA dump\n", 2587 __func__, ooa_hdr->fill_len); 2588 } 2589 2590 getbintime(&ooa_hdr->cur_bt); 2591 2592 if (cur_fill_num > ooa_hdr->alloc_num) { 2593 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2594 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2595 } else { 2596 ooa_hdr->dropped_num = 0; 2597 ooa_hdr->status = CTL_OOA_OK; 2598 } 2599 2600 free(entries, M_CTL); 2601 break; 2602 } 2603 case CTL_CHECK_OOA: { 2604 union ctl_io *io; 2605 struct ctl_lun *lun; 2606 struct ctl_ooa_info *ooa_info; 2607 2608 2609 ooa_info = (struct ctl_ooa_info *)addr; 2610 2611 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2612 ooa_info->status = CTL_OOA_INVALID_LUN; 2613 break; 2614 } 2615 mtx_lock(&softc->ctl_lock); 2616 lun = softc->ctl_luns[ooa_info->lun_id]; 2617 if (lun == NULL) { 2618 mtx_unlock(&softc->ctl_lock); 2619 ooa_info->status = CTL_OOA_INVALID_LUN; 2620 break; 2621 } 2622 mtx_lock(&lun->lun_lock); 2623 mtx_unlock(&softc->ctl_lock); 2624 ooa_info->num_entries = 0; 2625 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2626 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2627 &io->io_hdr, ooa_links)) { 2628 ooa_info->num_entries++; 2629 } 2630 mtx_unlock(&lun->lun_lock); 2631 2632 ooa_info->status = CTL_OOA_SUCCESS; 2633 2634 break; 2635 } 2636 case CTL_HARD_START: 2637 case CTL_HARD_STOP: { 2638 struct ctl_fe_ioctl_startstop_info ss_info; 2639 struct cfi_metatask *metatask; 2640 struct mtx hs_mtx; 2641 2642 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2643 2644 cv_init(&ss_info.sem, "hard start/stop cv" ); 2645 2646 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2647 if (metatask == NULL) { 2648 retval = ENOMEM; 2649 mtx_destroy(&hs_mtx); 2650 break; 2651 } 2652 2653 if (cmd == CTL_HARD_START) 2654 metatask->tasktype = CFI_TASK_STARTUP; 2655 else 2656 metatask->tasktype = CFI_TASK_SHUTDOWN; 2657 2658 metatask->callback = ctl_ioctl_hard_startstop_callback; 2659 metatask->callback_arg = &ss_info; 2660 2661 cfi_action(metatask); 2662 2663 /* Wait for the callback */ 2664 mtx_lock(&hs_mtx); 2665 cv_wait_sig(&ss_info.sem, &hs_mtx); 2666 mtx_unlock(&hs_mtx); 2667 2668 /* 2669 * All information has been copied from the metatask by the 2670 * time cv_broadcast() is called, so we free the metatask here. 2671 */ 2672 cfi_free_metatask(metatask); 2673 2674 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2675 2676 mtx_destroy(&hs_mtx); 2677 break; 2678 } 2679 case CTL_BBRREAD: { 2680 struct ctl_bbrread_info *bbr_info; 2681 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2682 struct mtx bbr_mtx; 2683 struct cfi_metatask *metatask; 2684 2685 bbr_info = (struct ctl_bbrread_info *)addr; 2686 2687 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2688 2689 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2690 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2691 2692 fe_bbr_info.bbr_info = bbr_info; 2693 fe_bbr_info.lock = &bbr_mtx; 2694 2695 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2696 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2697 2698 if (metatask == NULL) { 2699 mtx_destroy(&bbr_mtx); 2700 cv_destroy(&fe_bbr_info.sem); 2701 retval = ENOMEM; 2702 break; 2703 } 2704 metatask->tasktype = CFI_TASK_BBRREAD; 2705 metatask->callback = ctl_ioctl_bbrread_callback; 2706 metatask->callback_arg = &fe_bbr_info; 2707 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2708 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2709 metatask->taskinfo.bbrread.len = bbr_info->len; 2710 2711 cfi_action(metatask); 2712 2713 mtx_lock(&bbr_mtx); 2714 while (fe_bbr_info.wakeup_done == 0) 2715 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2716 mtx_unlock(&bbr_mtx); 2717 2718 bbr_info->status = metatask->status; 2719 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2720 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2721 memcpy(&bbr_info->sense_data, 2722 &metatask->taskinfo.bbrread.sense_data, 2723 ctl_min(sizeof(bbr_info->sense_data), 2724 sizeof(metatask->taskinfo.bbrread.sense_data))); 2725 2726 cfi_free_metatask(metatask); 2727 2728 mtx_destroy(&bbr_mtx); 2729 cv_destroy(&fe_bbr_info.sem); 2730 2731 break; 2732 } 2733 case CTL_DELAY_IO: { 2734 struct ctl_io_delay_info *delay_info; 2735#ifdef CTL_IO_DELAY 2736 struct ctl_lun *lun; 2737#endif /* CTL_IO_DELAY */ 2738 2739 delay_info = (struct ctl_io_delay_info *)addr; 2740 2741#ifdef CTL_IO_DELAY 2742 mtx_lock(&softc->ctl_lock); 2743 2744 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2745 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2746 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2747 } else { 2748 lun = softc->ctl_luns[delay_info->lun_id]; 2749 mtx_lock(&lun->lun_lock); 2750 2751 delay_info->status = CTL_DELAY_STATUS_OK; 2752 2753 switch (delay_info->delay_type) { 2754 case CTL_DELAY_TYPE_CONT: 2755 break; 2756 case CTL_DELAY_TYPE_ONESHOT: 2757 break; 2758 default: 2759 delay_info->status = 2760 CTL_DELAY_STATUS_INVALID_TYPE; 2761 break; 2762 } 2763 2764 switch (delay_info->delay_loc) { 2765 case CTL_DELAY_LOC_DATAMOVE: 2766 lun->delay_info.datamove_type = 2767 delay_info->delay_type; 2768 lun->delay_info.datamove_delay = 2769 delay_info->delay_secs; 2770 break; 2771 case CTL_DELAY_LOC_DONE: 2772 lun->delay_info.done_type = 2773 delay_info->delay_type; 2774 lun->delay_info.done_delay = 2775 delay_info->delay_secs; 2776 break; 2777 default: 2778 delay_info->status = 2779 CTL_DELAY_STATUS_INVALID_LOC; 2780 break; 2781 } 2782 mtx_unlock(&lun->lun_lock); 2783 } 2784 2785 mtx_unlock(&softc->ctl_lock); 2786#else 2787 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2788#endif /* CTL_IO_DELAY */ 2789 break; 2790 } 2791 case CTL_REALSYNC_SET: { 2792 int *syncstate; 2793 2794 syncstate = (int *)addr; 2795 2796 mtx_lock(&softc->ctl_lock); 2797 switch (*syncstate) { 2798 case 0: 2799 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2800 break; 2801 case 1: 2802 softc->flags |= CTL_FLAG_REAL_SYNC; 2803 break; 2804 default: 2805 retval = EINVAL; 2806 break; 2807 } 2808 mtx_unlock(&softc->ctl_lock); 2809 break; 2810 } 2811 case CTL_REALSYNC_GET: { 2812 int *syncstate; 2813 2814 syncstate = (int*)addr; 2815 2816 mtx_lock(&softc->ctl_lock); 2817 if (softc->flags & CTL_FLAG_REAL_SYNC) 2818 *syncstate = 1; 2819 else 2820 *syncstate = 0; 2821 mtx_unlock(&softc->ctl_lock); 2822 2823 break; 2824 } 2825 case CTL_SETSYNC: 2826 case CTL_GETSYNC: { 2827 struct ctl_sync_info *sync_info; 2828 struct ctl_lun *lun; 2829 2830 sync_info = (struct ctl_sync_info *)addr; 2831 2832 mtx_lock(&softc->ctl_lock); 2833 lun = softc->ctl_luns[sync_info->lun_id]; 2834 if (lun == NULL) { 2835 mtx_unlock(&softc->ctl_lock); 2836 sync_info->status = CTL_GS_SYNC_NO_LUN; 2837 } 2838 /* 2839 * Get or set the sync interval. We're not bounds checking 2840 * in the set case, hopefully the user won't do something 2841 * silly. 2842 */ 2843 mtx_lock(&lun->lun_lock); 2844 mtx_unlock(&softc->ctl_lock); 2845 if (cmd == CTL_GETSYNC) 2846 sync_info->sync_interval = lun->sync_interval; 2847 else 2848 lun->sync_interval = sync_info->sync_interval; 2849 mtx_unlock(&lun->lun_lock); 2850 2851 sync_info->status = CTL_GS_SYNC_OK; 2852 2853 break; 2854 } 2855 case CTL_GETSTATS: { 2856 struct ctl_stats *stats; 2857 struct ctl_lun *lun; 2858 int i; 2859 2860 stats = (struct ctl_stats *)addr; 2861 2862 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2863 stats->alloc_len) { 2864 stats->status = CTL_SS_NEED_MORE_SPACE; 2865 stats->num_luns = softc->num_luns; 2866 break; 2867 } 2868 /* 2869 * XXX KDM no locking here. If the LUN list changes, 2870 * things can blow up. 2871 */ 2872 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2873 i++, lun = STAILQ_NEXT(lun, links)) { 2874 retval = copyout(&lun->stats, &stats->lun_stats[i], 2875 sizeof(lun->stats)); 2876 if (retval != 0) 2877 break; 2878 } 2879 stats->num_luns = softc->num_luns; 2880 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2881 softc->num_luns; 2882 stats->status = CTL_SS_OK; 2883#ifdef CTL_TIME_IO 2884 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2885#else 2886 stats->flags = CTL_STATS_FLAG_NONE; 2887#endif 2888 getnanouptime(&stats->timestamp); 2889 break; 2890 } 2891 case CTL_ERROR_INJECT: { 2892 struct ctl_error_desc *err_desc, *new_err_desc; 2893 struct ctl_lun *lun; 2894 2895 err_desc = (struct ctl_error_desc *)addr; 2896 2897 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2898 M_WAITOK | M_ZERO); 2899 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2900 2901 mtx_lock(&softc->ctl_lock); 2902 lun = softc->ctl_luns[err_desc->lun_id]; 2903 if (lun == NULL) { 2904 mtx_unlock(&softc->ctl_lock); 2905 free(new_err_desc, M_CTL); 2906 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2907 __func__, (uintmax_t)err_desc->lun_id); 2908 retval = EINVAL; 2909 break; 2910 } 2911 mtx_lock(&lun->lun_lock); 2912 mtx_unlock(&softc->ctl_lock); 2913 2914 /* 2915 * We could do some checking here to verify the validity 2916 * of the request, but given the complexity of error 2917 * injection requests, the checking logic would be fairly 2918 * complex. 2919 * 2920 * For now, if the request is invalid, it just won't get 2921 * executed and might get deleted. 2922 */ 2923 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2924 2925 /* 2926 * XXX KDM check to make sure the serial number is unique, 2927 * in case we somehow manage to wrap. That shouldn't 2928 * happen for a very long time, but it's the right thing to 2929 * do. 2930 */ 2931 new_err_desc->serial = lun->error_serial; 2932 err_desc->serial = lun->error_serial; 2933 lun->error_serial++; 2934 2935 mtx_unlock(&lun->lun_lock); 2936 break; 2937 } 2938 case CTL_ERROR_INJECT_DELETE: { 2939 struct ctl_error_desc *delete_desc, *desc, *desc2; 2940 struct ctl_lun *lun; 2941 int delete_done; 2942 2943 delete_desc = (struct ctl_error_desc *)addr; 2944 delete_done = 0; 2945 2946 mtx_lock(&softc->ctl_lock); 2947 lun = softc->ctl_luns[delete_desc->lun_id]; 2948 if (lun == NULL) { 2949 mtx_unlock(&softc->ctl_lock); 2950 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2951 __func__, (uintmax_t)delete_desc->lun_id); 2952 retval = EINVAL; 2953 break; 2954 } 2955 mtx_lock(&lun->lun_lock); 2956 mtx_unlock(&softc->ctl_lock); 2957 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2958 if (desc->serial != delete_desc->serial) 2959 continue; 2960 2961 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2962 links); 2963 free(desc, M_CTL); 2964 delete_done = 1; 2965 } 2966 mtx_unlock(&lun->lun_lock); 2967 if (delete_done == 0) { 2968 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2969 "error serial %ju on LUN %u\n", __func__, 2970 delete_desc->serial, delete_desc->lun_id); 2971 retval = EINVAL; 2972 break; 2973 } 2974 break; 2975 } 2976 case CTL_DUMP_STRUCTS: { 2977 int i, j, k, idx; 2978 struct ctl_port *port; 2979 struct ctl_frontend *fe; 2980 2981 mtx_lock(&softc->ctl_lock); 2982 printf("CTL Persistent Reservation information start:\n"); 2983 for (i = 0; i < CTL_MAX_LUNS; i++) { 2984 struct ctl_lun *lun; 2985 2986 lun = softc->ctl_luns[i]; 2987 2988 if ((lun == NULL) 2989 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2990 continue; 2991 2992 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2993 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2994 idx = j * CTL_MAX_INIT_PER_PORT + k; 2995 if (lun->per_res[idx].registered == 0) 2996 continue; 2997 printf(" LUN %d port %d iid %d key " 2998 "%#jx\n", i, j, k, 2999 (uintmax_t)scsi_8btou64( 3000 lun->per_res[idx].res_key.key)); 3001 } 3002 } 3003 } 3004 printf("CTL Persistent Reservation information end\n"); 3005 printf("CTL Ports:\n"); 3006 STAILQ_FOREACH(port, &softc->port_list, links) { 3007 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3008 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3009 port->frontend->name, port->port_type, 3010 port->physical_port, port->virtual_port, 3011 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3012 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3013 if (port->wwpn_iid[j].in_use == 0 && 3014 port->wwpn_iid[j].wwpn == 0 && 3015 port->wwpn_iid[j].name == NULL) 3016 continue; 3017 3018 printf(" iid %u use %d WWPN %#jx '%s'\n", 3019 j, port->wwpn_iid[j].in_use, 3020 (uintmax_t)port->wwpn_iid[j].wwpn, 3021 port->wwpn_iid[j].name); 3022 } 3023 } 3024 printf("CTL Port information end\n"); 3025 mtx_unlock(&softc->ctl_lock); 3026 /* 3027 * XXX KDM calling this without a lock. We'd likely want 3028 * to drop the lock before calling the frontend's dump 3029 * routine anyway. 3030 */ 3031 printf("CTL Frontends:\n"); 3032 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3033 printf(" Frontend '%s'\n", fe->name); 3034 if (fe->fe_dump != NULL) 3035 fe->fe_dump(); 3036 } 3037 printf("CTL Frontend information end\n"); 3038 break; 3039 } 3040 case CTL_LUN_REQ: { 3041 struct ctl_lun_req *lun_req; 3042 struct ctl_backend_driver *backend; 3043 3044 lun_req = (struct ctl_lun_req *)addr; 3045 3046 backend = ctl_backend_find(lun_req->backend); 3047 if (backend == NULL) { 3048 lun_req->status = CTL_LUN_ERROR; 3049 snprintf(lun_req->error_str, 3050 sizeof(lun_req->error_str), 3051 "Backend \"%s\" not found.", 3052 lun_req->backend); 3053 break; 3054 } 3055 if (lun_req->num_be_args > 0) { 3056 lun_req->kern_be_args = ctl_copyin_args( 3057 lun_req->num_be_args, 3058 lun_req->be_args, 3059 lun_req->error_str, 3060 sizeof(lun_req->error_str)); 3061 if (lun_req->kern_be_args == NULL) { 3062 lun_req->status = CTL_LUN_ERROR; 3063 break; 3064 } 3065 } 3066 3067 retval = backend->ioctl(dev, cmd, addr, flag, td); 3068 3069 if (lun_req->num_be_args > 0) { 3070 ctl_copyout_args(lun_req->num_be_args, 3071 lun_req->kern_be_args); 3072 ctl_free_args(lun_req->num_be_args, 3073 lun_req->kern_be_args); 3074 } 3075 break; 3076 } 3077 case CTL_LUN_LIST: { 3078 struct sbuf *sb; 3079 struct ctl_lun *lun; 3080 struct ctl_lun_list *list; 3081 struct ctl_option *opt; 3082 3083 list = (struct ctl_lun_list *)addr; 3084 3085 /* 3086 * Allocate a fixed length sbuf here, based on the length 3087 * of the user's buffer. We could allocate an auto-extending 3088 * buffer, and then tell the user how much larger our 3089 * amount of data is than his buffer, but that presents 3090 * some problems: 3091 * 3092 * 1. The sbuf(9) routines use a blocking malloc, and so 3093 * we can't hold a lock while calling them with an 3094 * auto-extending buffer. 3095 * 3096 * 2. There is not currently a LUN reference counting 3097 * mechanism, outside of outstanding transactions on 3098 * the LUN's OOA queue. So a LUN could go away on us 3099 * while we're getting the LUN number, backend-specific 3100 * information, etc. Thus, given the way things 3101 * currently work, we need to hold the CTL lock while 3102 * grabbing LUN information. 3103 * 3104 * So, from the user's standpoint, the best thing to do is 3105 * allocate what he thinks is a reasonable buffer length, 3106 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3107 * double the buffer length and try again. (And repeat 3108 * that until he succeeds.) 3109 */ 3110 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3111 if (sb == NULL) { 3112 list->status = CTL_LUN_LIST_ERROR; 3113 snprintf(list->error_str, sizeof(list->error_str), 3114 "Unable to allocate %d bytes for LUN list", 3115 list->alloc_len); 3116 break; 3117 } 3118 3119 sbuf_printf(sb, "<ctllunlist>\n"); 3120 3121 mtx_lock(&softc->ctl_lock); 3122 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3123 mtx_lock(&lun->lun_lock); 3124 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3125 (uintmax_t)lun->lun); 3126 3127 /* 3128 * Bail out as soon as we see that we've overfilled 3129 * the buffer. 3130 */ 3131 if (retval != 0) 3132 break; 3133 3134 retval = sbuf_printf(sb, "\t<backend_type>%s" 3135 "</backend_type>\n", 3136 (lun->backend == NULL) ? "none" : 3137 lun->backend->name); 3138 3139 if (retval != 0) 3140 break; 3141 3142 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3143 lun->be_lun->lun_type); 3144 3145 if (retval != 0) 3146 break; 3147 3148 if (lun->backend == NULL) { 3149 retval = sbuf_printf(sb, "</lun>\n"); 3150 if (retval != 0) 3151 break; 3152 continue; 3153 } 3154 3155 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3156 (lun->be_lun->maxlba > 0) ? 3157 lun->be_lun->maxlba + 1 : 0); 3158 3159 if (retval != 0) 3160 break; 3161 3162 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3163 lun->be_lun->blocksize); 3164 3165 if (retval != 0) 3166 break; 3167 3168 retval = sbuf_printf(sb, "\t<serial_number>"); 3169 3170 if (retval != 0) 3171 break; 3172 3173 retval = ctl_sbuf_printf_esc(sb, 3174 lun->be_lun->serial_num); 3175 3176 if (retval != 0) 3177 break; 3178 3179 retval = sbuf_printf(sb, "</serial_number>\n"); 3180 3181 if (retval != 0) 3182 break; 3183 3184 retval = sbuf_printf(sb, "\t<device_id>"); 3185 3186 if (retval != 0) 3187 break; 3188 3189 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3190 3191 if (retval != 0) 3192 break; 3193 3194 retval = sbuf_printf(sb, "</device_id>\n"); 3195 3196 if (retval != 0) 3197 break; 3198 3199 if (lun->backend->lun_info != NULL) { 3200 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3201 if (retval != 0) 3202 break; 3203 } 3204 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3205 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3206 opt->name, opt->value, opt->name); 3207 if (retval != 0) 3208 break; 3209 } 3210 3211 retval = sbuf_printf(sb, "</lun>\n"); 3212 3213 if (retval != 0) 3214 break; 3215 mtx_unlock(&lun->lun_lock); 3216 } 3217 if (lun != NULL) 3218 mtx_unlock(&lun->lun_lock); 3219 mtx_unlock(&softc->ctl_lock); 3220 3221 if ((retval != 0) 3222 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3223 retval = 0; 3224 sbuf_delete(sb); 3225 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3226 snprintf(list->error_str, sizeof(list->error_str), 3227 "Out of space, %d bytes is too small", 3228 list->alloc_len); 3229 break; 3230 } 3231 3232 sbuf_finish(sb); 3233 3234 retval = copyout(sbuf_data(sb), list->lun_xml, 3235 sbuf_len(sb) + 1); 3236 3237 list->fill_len = sbuf_len(sb) + 1; 3238 list->status = CTL_LUN_LIST_OK; 3239 sbuf_delete(sb); 3240 break; 3241 } 3242 case CTL_ISCSI: { 3243 struct ctl_iscsi *ci; 3244 struct ctl_frontend *fe; 3245 3246 ci = (struct ctl_iscsi *)addr; 3247 3248 fe = ctl_frontend_find("iscsi"); 3249 if (fe == NULL) { 3250 ci->status = CTL_ISCSI_ERROR; 3251 snprintf(ci->error_str, sizeof(ci->error_str), 3252 "Frontend \"iscsi\" not found."); 3253 break; 3254 } 3255 3256 retval = fe->ioctl(dev, cmd, addr, flag, td); 3257 break; 3258 } 3259 case CTL_PORT_REQ: { 3260 struct ctl_req *req; 3261 struct ctl_frontend *fe; 3262 3263 req = (struct ctl_req *)addr; 3264 3265 fe = ctl_frontend_find(req->driver); 3266 if (fe == NULL) { 3267 req->status = CTL_LUN_ERROR; 3268 snprintf(req->error_str, sizeof(req->error_str), 3269 "Frontend \"%s\" not found.", req->driver); 3270 break; 3271 } 3272 if (req->num_args > 0) { 3273 req->kern_args = ctl_copyin_args(req->num_args, 3274 req->args, req->error_str, sizeof(req->error_str)); 3275 if (req->kern_args == NULL) { 3276 req->status = CTL_LUN_ERROR; 3277 break; 3278 } 3279 } 3280 3281 retval = fe->ioctl(dev, cmd, addr, flag, td); 3282 3283 if (req->num_args > 0) { 3284 ctl_copyout_args(req->num_args, req->kern_args); 3285 ctl_free_args(req->num_args, req->kern_args); 3286 } 3287 break; 3288 } 3289 case CTL_PORT_LIST: { 3290 struct sbuf *sb; 3291 struct ctl_port *port; 3292 struct ctl_lun_list *list; 3293 struct ctl_option *opt; 3294 3295 list = (struct ctl_lun_list *)addr; 3296 3297 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3298 if (sb == NULL) { 3299 list->status = CTL_LUN_LIST_ERROR; 3300 snprintf(list->error_str, sizeof(list->error_str), 3301 "Unable to allocate %d bytes for LUN list", 3302 list->alloc_len); 3303 break; 3304 } 3305 3306 sbuf_printf(sb, "<ctlportlist>\n"); 3307 3308 mtx_lock(&softc->ctl_lock); 3309 STAILQ_FOREACH(port, &softc->port_list, links) { 3310 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3311 (uintmax_t)port->targ_port); 3312 3313 /* 3314 * Bail out as soon as we see that we've overfilled 3315 * the buffer. 3316 */ 3317 if (retval != 0) 3318 break; 3319 3320 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3321 "</frontend_type>\n", port->frontend->name); 3322 if (retval != 0) 3323 break; 3324 3325 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3326 port->port_type); 3327 if (retval != 0) 3328 break; 3329 3330 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3331 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3332 if (retval != 0) 3333 break; 3334 3335 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3336 port->port_name); 3337 if (retval != 0) 3338 break; 3339 3340 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3341 port->physical_port); 3342 if (retval != 0) 3343 break; 3344 3345 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3346 port->virtual_port); 3347 if (retval != 0) 3348 break; 3349 3350 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3351 (uintmax_t)port->wwnn); 3352 if (retval != 0) 3353 break; 3354 3355 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3356 (uintmax_t)port->wwpn); 3357 if (retval != 0) 3358 break; 3359 3360 if (port->port_info != NULL) { 3361 retval = port->port_info(port->onoff_arg, sb); 3362 if (retval != 0) 3363 break; 3364 } 3365 STAILQ_FOREACH(opt, &port->options, links) { 3366 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3367 opt->name, opt->value, opt->name); 3368 if (retval != 0) 3369 break; 3370 } 3371 3372 retval = sbuf_printf(sb, "</targ_port>\n"); 3373 if (retval != 0) 3374 break; 3375 } 3376 mtx_unlock(&softc->ctl_lock); 3377 3378 if ((retval != 0) 3379 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3380 retval = 0; 3381 sbuf_delete(sb); 3382 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3383 snprintf(list->error_str, sizeof(list->error_str), 3384 "Out of space, %d bytes is too small", 3385 list->alloc_len); 3386 break; 3387 } 3388 3389 sbuf_finish(sb); 3390 3391 retval = copyout(sbuf_data(sb), list->lun_xml, 3392 sbuf_len(sb) + 1); 3393 3394 list->fill_len = sbuf_len(sb) + 1; 3395 list->status = CTL_LUN_LIST_OK; 3396 sbuf_delete(sb); 3397 break; 3398 } 3399 default: { 3400 /* XXX KDM should we fix this? */ 3401#if 0 3402 struct ctl_backend_driver *backend; 3403 unsigned int type; 3404 int found; 3405 3406 found = 0; 3407 3408 /* 3409 * We encode the backend type as the ioctl type for backend 3410 * ioctls. So parse it out here, and then search for a 3411 * backend of this type. 3412 */ 3413 type = _IOC_TYPE(cmd); 3414 3415 STAILQ_FOREACH(backend, &softc->be_list, links) { 3416 if (backend->type == type) { 3417 found = 1; 3418 break; 3419 } 3420 } 3421 if (found == 0) { 3422 printf("ctl: unknown ioctl command %#lx or backend " 3423 "%d\n", cmd, type); 3424 retval = EINVAL; 3425 break; 3426 } 3427 retval = backend->ioctl(dev, cmd, addr, flag, td); 3428#endif 3429 retval = ENOTTY; 3430 break; 3431 } 3432 } 3433 return (retval); 3434} 3435 3436uint32_t 3437ctl_get_initindex(struct ctl_nexus *nexus) 3438{ 3439 if (nexus->targ_port < CTL_MAX_PORTS) 3440 return (nexus->initid.id + 3441 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3442 else 3443 return (nexus->initid.id + 3444 ((nexus->targ_port - CTL_MAX_PORTS) * 3445 CTL_MAX_INIT_PER_PORT)); 3446} 3447 3448uint32_t 3449ctl_get_resindex(struct ctl_nexus *nexus) 3450{ 3451 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3452} 3453 3454uint32_t 3455ctl_port_idx(int port_num) 3456{ 3457 if (port_num < CTL_MAX_PORTS) 3458 return(port_num); 3459 else 3460 return(port_num - CTL_MAX_PORTS); 3461} 3462 3463static uint32_t 3464ctl_map_lun(int port_num, uint32_t lun_id) 3465{ 3466 struct ctl_port *port; 3467 3468 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3469 if (port == NULL) 3470 return (UINT32_MAX); 3471 if (port->lun_map == NULL) 3472 return (lun_id); 3473 return (port->lun_map(port->targ_lun_arg, lun_id)); 3474} 3475 3476static uint32_t 3477ctl_map_lun_back(int port_num, uint32_t lun_id) 3478{ 3479 struct ctl_port *port; 3480 uint32_t i; 3481 3482 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3483 if (port->lun_map == NULL) 3484 return (lun_id); 3485 for (i = 0; i < CTL_MAX_LUNS; i++) { 3486 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3487 return (i); 3488 } 3489 return (UINT32_MAX); 3490} 3491 3492/* 3493 * Note: This only works for bitmask sizes that are at least 32 bits, and 3494 * that are a power of 2. 3495 */ 3496int 3497ctl_ffz(uint32_t *mask, uint32_t size) 3498{ 3499 uint32_t num_chunks, num_pieces; 3500 int i, j; 3501 3502 num_chunks = (size >> 5); 3503 if (num_chunks == 0) 3504 num_chunks++; 3505 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3506 3507 for (i = 0; i < num_chunks; i++) { 3508 for (j = 0; j < num_pieces; j++) { 3509 if ((mask[i] & (1 << j)) == 0) 3510 return ((i << 5) + j); 3511 } 3512 } 3513 3514 return (-1); 3515} 3516 3517int 3518ctl_set_mask(uint32_t *mask, uint32_t bit) 3519{ 3520 uint32_t chunk, piece; 3521 3522 chunk = bit >> 5; 3523 piece = bit % (sizeof(uint32_t) * 8); 3524 3525 if ((mask[chunk] & (1 << piece)) != 0) 3526 return (-1); 3527 else 3528 mask[chunk] |= (1 << piece); 3529 3530 return (0); 3531} 3532 3533int 3534ctl_clear_mask(uint32_t *mask, uint32_t bit) 3535{ 3536 uint32_t chunk, piece; 3537 3538 chunk = bit >> 5; 3539 piece = bit % (sizeof(uint32_t) * 8); 3540 3541 if ((mask[chunk] & (1 << piece)) == 0) 3542 return (-1); 3543 else 3544 mask[chunk] &= ~(1 << piece); 3545 3546 return (0); 3547} 3548 3549int 3550ctl_is_set(uint32_t *mask, uint32_t bit) 3551{ 3552 uint32_t chunk, piece; 3553 3554 chunk = bit >> 5; 3555 piece = bit % (sizeof(uint32_t) * 8); 3556 3557 if ((mask[chunk] & (1 << piece)) == 0) 3558 return (0); 3559 else 3560 return (1); 3561} 3562 3563#ifdef unused 3564/* 3565 * The bus, target and lun are optional, they can be filled in later. 3566 * can_wait is used to determine whether we can wait on the malloc or not. 3567 */ 3568union ctl_io* 3569ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3570 uint32_t targ_lun, int can_wait) 3571{ 3572 union ctl_io *io; 3573 3574 if (can_wait) 3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3576 else 3577 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3578 3579 if (io != NULL) { 3580 io->io_hdr.io_type = io_type; 3581 io->io_hdr.targ_port = targ_port; 3582 /* 3583 * XXX KDM this needs to change/go away. We need to move 3584 * to a preallocated pool of ctl_scsiio structures. 3585 */ 3586 io->io_hdr.nexus.targ_target.id = targ_target; 3587 io->io_hdr.nexus.targ_lun = targ_lun; 3588 } 3589 3590 return (io); 3591} 3592 3593void 3594ctl_kfree_io(union ctl_io *io) 3595{ 3596 free(io, M_CTL); 3597} 3598#endif /* unused */ 3599 3600/* 3601 * ctl_softc, pool_type, total_ctl_io are passed in. 3602 * npool is passed out. 3603 */ 3604int 3605ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3606 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3607{ 3608 uint32_t i; 3609 union ctl_io *cur_io, *next_io; 3610 struct ctl_io_pool *pool; 3611 int retval; 3612 3613 retval = 0; 3614 3615 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3616 M_NOWAIT | M_ZERO); 3617 if (pool == NULL) { 3618 retval = ENOMEM; 3619 goto bailout; 3620 } 3621 3622 pool->type = pool_type; 3623 pool->ctl_softc = ctl_softc; 3624 3625 mtx_lock(&ctl_softc->pool_lock); 3626 pool->id = ctl_softc->cur_pool_id++; 3627 mtx_unlock(&ctl_softc->pool_lock); 3628 3629 pool->flags = CTL_POOL_FLAG_NONE; 3630 pool->refcount = 1; /* Reference for validity. */ 3631 STAILQ_INIT(&pool->free_queue); 3632 3633 /* 3634 * XXX KDM other options here: 3635 * - allocate a page at a time 3636 * - allocate one big chunk of memory. 3637 * Page allocation might work well, but would take a little more 3638 * tracking. 3639 */ 3640 for (i = 0; i < total_ctl_io; i++) { 3641 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3642 M_NOWAIT); 3643 if (cur_io == NULL) { 3644 retval = ENOMEM; 3645 break; 3646 } 3647 cur_io->io_hdr.pool = pool; 3648 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3649 pool->total_ctl_io++; 3650 pool->free_ctl_io++; 3651 } 3652 3653 if (retval != 0) { 3654 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3655 cur_io != NULL; cur_io = next_io) { 3656 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3657 links); 3658 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3659 ctl_io_hdr, links); 3660 free(cur_io, M_CTLIO); 3661 } 3662 3663 free(pool, M_CTL); 3664 goto bailout; 3665 } 3666 mtx_lock(&ctl_softc->pool_lock); 3667 ctl_softc->num_pools++; 3668 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3669 /* 3670 * Increment our usage count if this is an external consumer, so we 3671 * can't get unloaded until the external consumer (most likely a 3672 * FETD) unloads and frees his pool. 3673 * 3674 * XXX KDM will this increment the caller's module use count, or 3675 * mine? 3676 */ 3677#if 0 3678 if ((pool_type != CTL_POOL_EMERGENCY) 3679 && (pool_type != CTL_POOL_INTERNAL) 3680 && (pool_type != CTL_POOL_4OTHERSC)) 3681 MOD_INC_USE_COUNT; 3682#endif 3683 3684 mtx_unlock(&ctl_softc->pool_lock); 3685 3686 *npool = pool; 3687 3688bailout: 3689 3690 return (retval); 3691} 3692 3693static int 3694ctl_pool_acquire(struct ctl_io_pool *pool) 3695{ 3696 3697 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3698 3699 if (pool->flags & CTL_POOL_FLAG_INVALID) 3700 return (EINVAL); 3701 3702 pool->refcount++; 3703 3704 return (0); 3705} 3706 3707static void 3708ctl_pool_release(struct ctl_io_pool *pool) 3709{ 3710 struct ctl_softc *ctl_softc = pool->ctl_softc; 3711 union ctl_io *io; 3712 3713 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3714 3715 if (--pool->refcount != 0) 3716 return; 3717 3718 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3719 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3720 links); 3721 free(io, M_CTLIO); 3722 } 3723 3724 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3725 ctl_softc->num_pools--; 3726 3727 /* 3728 * XXX KDM will this decrement the caller's usage count or mine? 3729 */ 3730#if 0 3731 if ((pool->type != CTL_POOL_EMERGENCY) 3732 && (pool->type != CTL_POOL_INTERNAL) 3733 && (pool->type != CTL_POOL_4OTHERSC)) 3734 MOD_DEC_USE_COUNT; 3735#endif 3736 3737 free(pool, M_CTL); 3738} 3739 3740void 3741ctl_pool_free(struct ctl_io_pool *pool) 3742{ 3743 struct ctl_softc *ctl_softc; 3744 3745 if (pool == NULL) 3746 return; 3747 3748 ctl_softc = pool->ctl_softc; 3749 mtx_lock(&ctl_softc->pool_lock); 3750 pool->flags |= CTL_POOL_FLAG_INVALID; 3751 ctl_pool_release(pool); 3752 mtx_unlock(&ctl_softc->pool_lock); 3753} 3754 3755/* 3756 * This routine does not block (except for spinlocks of course). 3757 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3758 * possible. 3759 */ 3760union ctl_io * 3761ctl_alloc_io(void *pool_ref) 3762{ 3763 union ctl_io *io; 3764 struct ctl_softc *ctl_softc; 3765 struct ctl_io_pool *pool, *npool; 3766 struct ctl_io_pool *emergency_pool; 3767 3768 pool = (struct ctl_io_pool *)pool_ref; 3769 3770 if (pool == NULL) { 3771 printf("%s: pool is NULL\n", __func__); 3772 return (NULL); 3773 } 3774 3775 emergency_pool = NULL; 3776 3777 ctl_softc = pool->ctl_softc; 3778 3779 mtx_lock(&ctl_softc->pool_lock); 3780 /* 3781 * First, try to get the io structure from the user's pool. 3782 */ 3783 if (ctl_pool_acquire(pool) == 0) { 3784 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3785 if (io != NULL) { 3786 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3787 pool->total_allocated++; 3788 pool->free_ctl_io--; 3789 mtx_unlock(&ctl_softc->pool_lock); 3790 return (io); 3791 } else 3792 ctl_pool_release(pool); 3793 } 3794 /* 3795 * If he doesn't have any io structures left, search for an 3796 * emergency pool and grab one from there. 3797 */ 3798 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3799 if (npool->type != CTL_POOL_EMERGENCY) 3800 continue; 3801 3802 if (ctl_pool_acquire(npool) != 0) 3803 continue; 3804 3805 emergency_pool = npool; 3806 3807 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3808 if (io != NULL) { 3809 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3810 npool->total_allocated++; 3811 npool->free_ctl_io--; 3812 mtx_unlock(&ctl_softc->pool_lock); 3813 return (io); 3814 } else 3815 ctl_pool_release(npool); 3816 } 3817 3818 /* Drop the spinlock before we malloc */ 3819 mtx_unlock(&ctl_softc->pool_lock); 3820 3821 /* 3822 * The emergency pool (if it exists) didn't have one, so try an 3823 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3824 */ 3825 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3826 if (io != NULL) { 3827 /* 3828 * If the emergency pool exists but is empty, add this 3829 * ctl_io to its list when it gets freed. 3830 */ 3831 if (emergency_pool != NULL) { 3832 mtx_lock(&ctl_softc->pool_lock); 3833 if (ctl_pool_acquire(emergency_pool) == 0) { 3834 io->io_hdr.pool = emergency_pool; 3835 emergency_pool->total_ctl_io++; 3836 /* 3837 * Need to bump this, otherwise 3838 * total_allocated and total_freed won't 3839 * match when we no longer have anything 3840 * outstanding. 3841 */ 3842 emergency_pool->total_allocated++; 3843 } 3844 mtx_unlock(&ctl_softc->pool_lock); 3845 } else 3846 io->io_hdr.pool = NULL; 3847 } 3848 3849 return (io); 3850} 3851 3852void 3853ctl_free_io(union ctl_io *io) 3854{ 3855 if (io == NULL) 3856 return; 3857 3858 /* 3859 * If this ctl_io has a pool, return it to that pool. 3860 */ 3861 if (io->io_hdr.pool != NULL) { 3862 struct ctl_io_pool *pool; 3863 3864 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3865 mtx_lock(&pool->ctl_softc->pool_lock); 3866 io->io_hdr.io_type = 0xff; 3867 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3868 pool->total_freed++; 3869 pool->free_ctl_io++; 3870 ctl_pool_release(pool); 3871 mtx_unlock(&pool->ctl_softc->pool_lock); 3872 } else { 3873 /* 3874 * Otherwise, just free it. We probably malloced it and 3875 * the emergency pool wasn't available. 3876 */ 3877 free(io, M_CTLIO); 3878 } 3879 3880} 3881 3882void 3883ctl_zero_io(union ctl_io *io) 3884{ 3885 void *pool_ref; 3886 3887 if (io == NULL) 3888 return; 3889 3890 /* 3891 * May need to preserve linked list pointers at some point too. 3892 */ 3893 pool_ref = io->io_hdr.pool; 3894 3895 memset(io, 0, sizeof(*io)); 3896 3897 io->io_hdr.pool = pool_ref; 3898} 3899 3900/* 3901 * This routine is currently used for internal copies of ctl_ios that need 3902 * to persist for some reason after we've already returned status to the 3903 * FETD. (Thus the flag set.) 3904 * 3905 * XXX XXX 3906 * Note that this makes a blind copy of all fields in the ctl_io, except 3907 * for the pool reference. This includes any memory that has been 3908 * allocated! That memory will no longer be valid after done has been 3909 * called, so this would be VERY DANGEROUS for command that actually does 3910 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3911 * start and stop commands, which don't transfer any data, so this is not a 3912 * problem. If it is used for anything else, the caller would also need to 3913 * allocate data buffer space and this routine would need to be modified to 3914 * copy the data buffer(s) as well. 3915 */ 3916void 3917ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3918{ 3919 void *pool_ref; 3920 3921 if ((src == NULL) 3922 || (dest == NULL)) 3923 return; 3924 3925 /* 3926 * May need to preserve linked list pointers at some point too. 3927 */ 3928 pool_ref = dest->io_hdr.pool; 3929 3930 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3931 3932 dest->io_hdr.pool = pool_ref; 3933 /* 3934 * We need to know that this is an internal copy, and doesn't need 3935 * to get passed back to the FETD that allocated it. 3936 */ 3937 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3938} 3939 3940#ifdef NEEDTOPORT 3941static void 3942ctl_update_power_subpage(struct copan_power_subpage *page) 3943{ 3944 int num_luns, num_partitions, config_type; 3945 struct ctl_softc *softc; 3946 cs_BOOL_t aor_present, shelf_50pct_power; 3947 cs_raidset_personality_t rs_type; 3948 int max_active_luns; 3949 3950 softc = control_softc; 3951 3952 /* subtract out the processor LUN */ 3953 num_luns = softc->num_luns - 1; 3954 /* 3955 * Default to 7 LUNs active, which was the only number we allowed 3956 * in the past. 3957 */ 3958 max_active_luns = 7; 3959 3960 num_partitions = config_GetRsPartitionInfo(); 3961 config_type = config_GetConfigType(); 3962 shelf_50pct_power = config_GetShelfPowerMode(); 3963 aor_present = config_IsAorRsPresent(); 3964 3965 rs_type = ddb_GetRsRaidType(1); 3966 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3967 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3968 EPRINT(0, "Unsupported RS type %d!", rs_type); 3969 } 3970 3971 3972 page->total_luns = num_luns; 3973 3974 switch (config_type) { 3975 case 40: 3976 /* 3977 * In a 40 drive configuration, it doesn't matter what DC 3978 * cards we have, whether we have AOR enabled or not, 3979 * partitioning or not, or what type of RAIDset we have. 3980 * In that scenario, we can power up every LUN we present 3981 * to the user. 3982 */ 3983 max_active_luns = num_luns; 3984 3985 break; 3986 case 64: 3987 if (shelf_50pct_power == CS_FALSE) { 3988 /* 25% power */ 3989 if (aor_present == CS_TRUE) { 3990 if (rs_type == 3991 CS_RAIDSET_PERSONALITY_RAID5) { 3992 max_active_luns = 7; 3993 } else if (rs_type == 3994 CS_RAIDSET_PERSONALITY_RAID1){ 3995 max_active_luns = 14; 3996 } else { 3997 /* XXX KDM now what?? */ 3998 } 3999 } else { 4000 if (rs_type == 4001 CS_RAIDSET_PERSONALITY_RAID5) { 4002 max_active_luns = 8; 4003 } else if (rs_type == 4004 CS_RAIDSET_PERSONALITY_RAID1){ 4005 max_active_luns = 16; 4006 } else { 4007 /* XXX KDM now what?? */ 4008 } 4009 } 4010 } else { 4011 /* 50% power */ 4012 /* 4013 * With 50% power in a 64 drive configuration, we 4014 * can power all LUNs we present. 4015 */ 4016 max_active_luns = num_luns; 4017 } 4018 break; 4019 case 112: 4020 if (shelf_50pct_power == CS_FALSE) { 4021 /* 25% power */ 4022 if (aor_present == CS_TRUE) { 4023 if (rs_type == 4024 CS_RAIDSET_PERSONALITY_RAID5) { 4025 max_active_luns = 7; 4026 } else if (rs_type == 4027 CS_RAIDSET_PERSONALITY_RAID1){ 4028 max_active_luns = 14; 4029 } else { 4030 /* XXX KDM now what?? */ 4031 } 4032 } else { 4033 if (rs_type == 4034 CS_RAIDSET_PERSONALITY_RAID5) { 4035 max_active_luns = 8; 4036 } else if (rs_type == 4037 CS_RAIDSET_PERSONALITY_RAID1){ 4038 max_active_luns = 16; 4039 } else { 4040 /* XXX KDM now what?? */ 4041 } 4042 } 4043 } else { 4044 /* 50% power */ 4045 if (aor_present == CS_TRUE) { 4046 if (rs_type == 4047 CS_RAIDSET_PERSONALITY_RAID5) { 4048 max_active_luns = 14; 4049 } else if (rs_type == 4050 CS_RAIDSET_PERSONALITY_RAID1){ 4051 /* 4052 * We're assuming here that disk 4053 * caching is enabled, and so we're 4054 * able to power up half of each 4055 * LUN, and cache all writes. 4056 */ 4057 max_active_luns = num_luns; 4058 } else { 4059 /* XXX KDM now what?? */ 4060 } 4061 } else { 4062 if (rs_type == 4063 CS_RAIDSET_PERSONALITY_RAID5) { 4064 max_active_luns = 15; 4065 } else if (rs_type == 4066 CS_RAIDSET_PERSONALITY_RAID1){ 4067 max_active_luns = 30; 4068 } else { 4069 /* XXX KDM now what?? */ 4070 } 4071 } 4072 } 4073 break; 4074 default: 4075 /* 4076 * In this case, we have an unknown configuration, so we 4077 * just use the default from above. 4078 */ 4079 break; 4080 } 4081 4082 page->max_active_luns = max_active_luns; 4083#if 0 4084 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4085 page->total_luns, page->max_active_luns); 4086#endif 4087} 4088#endif /* NEEDTOPORT */ 4089 4090/* 4091 * This routine could be used in the future to load default and/or saved 4092 * mode page parameters for a particuar lun. 4093 */ 4094static int 4095ctl_init_page_index(struct ctl_lun *lun) 4096{ 4097 int i; 4098 struct ctl_page_index *page_index; 4099 struct ctl_softc *softc; 4100 const char *value; 4101 4102 memcpy(&lun->mode_pages.index, page_index_template, 4103 sizeof(page_index_template)); 4104 4105 softc = lun->ctl_softc; 4106 4107 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4108 4109 page_index = &lun->mode_pages.index[i]; 4110 /* 4111 * If this is a disk-only mode page, there's no point in 4112 * setting it up. For some pages, we have to have some 4113 * basic information about the disk in order to calculate the 4114 * mode page data. 4115 */ 4116 if ((lun->be_lun->lun_type != T_DIRECT) 4117 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4118 continue; 4119 4120 switch (page_index->page_code & SMPH_PC_MASK) { 4121 case SMS_FORMAT_DEVICE_PAGE: { 4122 struct scsi_format_page *format_page; 4123 4124 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4125 panic("subpage is incorrect!"); 4126 4127 /* 4128 * Sectors per track are set above. Bytes per 4129 * sector need to be set here on a per-LUN basis. 4130 */ 4131 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4132 &format_page_default, 4133 sizeof(format_page_default)); 4134 memcpy(&lun->mode_pages.format_page[ 4135 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4136 sizeof(format_page_changeable)); 4137 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4138 &format_page_default, 4139 sizeof(format_page_default)); 4140 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4141 &format_page_default, 4142 sizeof(format_page_default)); 4143 4144 format_page = &lun->mode_pages.format_page[ 4145 CTL_PAGE_CURRENT]; 4146 scsi_ulto2b(lun->be_lun->blocksize, 4147 format_page->bytes_per_sector); 4148 4149 format_page = &lun->mode_pages.format_page[ 4150 CTL_PAGE_DEFAULT]; 4151 scsi_ulto2b(lun->be_lun->blocksize, 4152 format_page->bytes_per_sector); 4153 4154 format_page = &lun->mode_pages.format_page[ 4155 CTL_PAGE_SAVED]; 4156 scsi_ulto2b(lun->be_lun->blocksize, 4157 format_page->bytes_per_sector); 4158 4159 page_index->page_data = 4160 (uint8_t *)lun->mode_pages.format_page; 4161 break; 4162 } 4163 case SMS_RIGID_DISK_PAGE: { 4164 struct scsi_rigid_disk_page *rigid_disk_page; 4165 uint32_t sectors_per_cylinder; 4166 uint64_t cylinders; 4167#ifndef __XSCALE__ 4168 int shift; 4169#endif /* !__XSCALE__ */ 4170 4171 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4172 panic("invalid subpage value %d", 4173 page_index->subpage); 4174 4175 /* 4176 * Rotation rate and sectors per track are set 4177 * above. We calculate the cylinders here based on 4178 * capacity. Due to the number of heads and 4179 * sectors per track we're using, smaller arrays 4180 * may turn out to have 0 cylinders. Linux and 4181 * FreeBSD don't pay attention to these mode pages 4182 * to figure out capacity, but Solaris does. It 4183 * seems to deal with 0 cylinders just fine, and 4184 * works out a fake geometry based on the capacity. 4185 */ 4186 memcpy(&lun->mode_pages.rigid_disk_page[ 4187 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4188 sizeof(rigid_disk_page_default)); 4189 memcpy(&lun->mode_pages.rigid_disk_page[ 4190 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4191 sizeof(rigid_disk_page_changeable)); 4192 memcpy(&lun->mode_pages.rigid_disk_page[ 4193 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4194 sizeof(rigid_disk_page_default)); 4195 memcpy(&lun->mode_pages.rigid_disk_page[ 4196 CTL_PAGE_SAVED], &rigid_disk_page_default, 4197 sizeof(rigid_disk_page_default)); 4198 4199 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4200 CTL_DEFAULT_HEADS; 4201 4202 /* 4203 * The divide method here will be more accurate, 4204 * probably, but results in floating point being 4205 * used in the kernel on i386 (__udivdi3()). On the 4206 * XScale, though, __udivdi3() is implemented in 4207 * software. 4208 * 4209 * The shift method for cylinder calculation is 4210 * accurate if sectors_per_cylinder is a power of 4211 * 2. Otherwise it might be slightly off -- you 4212 * might have a bit of a truncation problem. 4213 */ 4214#ifdef __XSCALE__ 4215 cylinders = (lun->be_lun->maxlba + 1) / 4216 sectors_per_cylinder; 4217#else 4218 for (shift = 31; shift > 0; shift--) { 4219 if (sectors_per_cylinder & (1 << shift)) 4220 break; 4221 } 4222 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4223#endif 4224 4225 /* 4226 * We've basically got 3 bytes, or 24 bits for the 4227 * cylinder size in the mode page. If we're over, 4228 * just round down to 2^24. 4229 */ 4230 if (cylinders > 0xffffff) 4231 cylinders = 0xffffff; 4232 4233 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4234 CTL_PAGE_CURRENT]; 4235 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4236 4237 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4238 CTL_PAGE_DEFAULT]; 4239 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4240 4241 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4242 CTL_PAGE_SAVED]; 4243 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4244 4245 page_index->page_data = 4246 (uint8_t *)lun->mode_pages.rigid_disk_page; 4247 break; 4248 } 4249 case SMS_CACHING_PAGE: { 4250 struct scsi_caching_page *caching_page; 4251 4252 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4253 panic("invalid subpage value %d", 4254 page_index->subpage); 4255 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4256 &caching_page_default, 4257 sizeof(caching_page_default)); 4258 memcpy(&lun->mode_pages.caching_page[ 4259 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4260 sizeof(caching_page_changeable)); 4261 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4262 &caching_page_default, 4263 sizeof(caching_page_default)); 4264 caching_page = &lun->mode_pages.caching_page[ 4265 CTL_PAGE_SAVED]; 4266 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4267 if (value != NULL && strcmp(value, "off") == 0) 4268 caching_page->flags1 &= ~SCP_WCE; 4269 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4270 if (value != NULL && strcmp(value, "off") == 0) 4271 caching_page->flags1 |= SCP_RCD; 4272 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4273 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4274 sizeof(caching_page_default)); 4275 page_index->page_data = 4276 (uint8_t *)lun->mode_pages.caching_page; 4277 break; 4278 } 4279 case SMS_CONTROL_MODE_PAGE: { 4280 struct scsi_control_page *control_page; 4281 4282 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4283 panic("invalid subpage value %d", 4284 page_index->subpage); 4285 4286 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4287 &control_page_default, 4288 sizeof(control_page_default)); 4289 memcpy(&lun->mode_pages.control_page[ 4290 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4291 sizeof(control_page_changeable)); 4292 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4293 &control_page_default, 4294 sizeof(control_page_default)); 4295 control_page = &lun->mode_pages.control_page[ 4296 CTL_PAGE_SAVED]; 4297 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4298 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4299 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4300 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4301 } 4302 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4303 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4304 sizeof(control_page_default)); 4305 page_index->page_data = 4306 (uint8_t *)lun->mode_pages.control_page; 4307 break; 4308 4309 } 4310 case SMS_VENDOR_SPECIFIC_PAGE:{ 4311 switch (page_index->subpage) { 4312 case PWR_SUBPAGE_CODE: { 4313 struct copan_power_subpage *current_page, 4314 *saved_page; 4315 4316 memcpy(&lun->mode_pages.power_subpage[ 4317 CTL_PAGE_CURRENT], 4318 &power_page_default, 4319 sizeof(power_page_default)); 4320 memcpy(&lun->mode_pages.power_subpage[ 4321 CTL_PAGE_CHANGEABLE], 4322 &power_page_changeable, 4323 sizeof(power_page_changeable)); 4324 memcpy(&lun->mode_pages.power_subpage[ 4325 CTL_PAGE_DEFAULT], 4326 &power_page_default, 4327 sizeof(power_page_default)); 4328 memcpy(&lun->mode_pages.power_subpage[ 4329 CTL_PAGE_SAVED], 4330 &power_page_default, 4331 sizeof(power_page_default)); 4332 page_index->page_data = 4333 (uint8_t *)lun->mode_pages.power_subpage; 4334 4335 current_page = (struct copan_power_subpage *) 4336 (page_index->page_data + 4337 (page_index->page_len * 4338 CTL_PAGE_CURRENT)); 4339 saved_page = (struct copan_power_subpage *) 4340 (page_index->page_data + 4341 (page_index->page_len * 4342 CTL_PAGE_SAVED)); 4343 break; 4344 } 4345 case APS_SUBPAGE_CODE: { 4346 struct copan_aps_subpage *current_page, 4347 *saved_page; 4348 4349 // This gets set multiple times but 4350 // it should always be the same. It's 4351 // only done during init so who cares. 4352 index_to_aps_page = i; 4353 4354 memcpy(&lun->mode_pages.aps_subpage[ 4355 CTL_PAGE_CURRENT], 4356 &aps_page_default, 4357 sizeof(aps_page_default)); 4358 memcpy(&lun->mode_pages.aps_subpage[ 4359 CTL_PAGE_CHANGEABLE], 4360 &aps_page_changeable, 4361 sizeof(aps_page_changeable)); 4362 memcpy(&lun->mode_pages.aps_subpage[ 4363 CTL_PAGE_DEFAULT], 4364 &aps_page_default, 4365 sizeof(aps_page_default)); 4366 memcpy(&lun->mode_pages.aps_subpage[ 4367 CTL_PAGE_SAVED], 4368 &aps_page_default, 4369 sizeof(aps_page_default)); 4370 page_index->page_data = 4371 (uint8_t *)lun->mode_pages.aps_subpage; 4372 4373 current_page = (struct copan_aps_subpage *) 4374 (page_index->page_data + 4375 (page_index->page_len * 4376 CTL_PAGE_CURRENT)); 4377 saved_page = (struct copan_aps_subpage *) 4378 (page_index->page_data + 4379 (page_index->page_len * 4380 CTL_PAGE_SAVED)); 4381 break; 4382 } 4383 case DBGCNF_SUBPAGE_CODE: { 4384 struct copan_debugconf_subpage *current_page, 4385 *saved_page; 4386 4387 memcpy(&lun->mode_pages.debugconf_subpage[ 4388 CTL_PAGE_CURRENT], 4389 &debugconf_page_default, 4390 sizeof(debugconf_page_default)); 4391 memcpy(&lun->mode_pages.debugconf_subpage[ 4392 CTL_PAGE_CHANGEABLE], 4393 &debugconf_page_changeable, 4394 sizeof(debugconf_page_changeable)); 4395 memcpy(&lun->mode_pages.debugconf_subpage[ 4396 CTL_PAGE_DEFAULT], 4397 &debugconf_page_default, 4398 sizeof(debugconf_page_default)); 4399 memcpy(&lun->mode_pages.debugconf_subpage[ 4400 CTL_PAGE_SAVED], 4401 &debugconf_page_default, 4402 sizeof(debugconf_page_default)); 4403 page_index->page_data = 4404 (uint8_t *)lun->mode_pages.debugconf_subpage; 4405 4406 current_page = (struct copan_debugconf_subpage *) 4407 (page_index->page_data + 4408 (page_index->page_len * 4409 CTL_PAGE_CURRENT)); 4410 saved_page = (struct copan_debugconf_subpage *) 4411 (page_index->page_data + 4412 (page_index->page_len * 4413 CTL_PAGE_SAVED)); 4414 break; 4415 } 4416 default: 4417 panic("invalid subpage value %d", 4418 page_index->subpage); 4419 break; 4420 } 4421 break; 4422 } 4423 default: 4424 panic("invalid page value %d", 4425 page_index->page_code & SMPH_PC_MASK); 4426 break; 4427 } 4428 } 4429 4430 return (CTL_RETVAL_COMPLETE); 4431} 4432 4433/* 4434 * LUN allocation. 4435 * 4436 * Requirements: 4437 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4438 * wants us to allocate the LUN and he can block. 4439 * - ctl_softc is always set 4440 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4441 * 4442 * Returns 0 for success, non-zero (errno) for failure. 4443 */ 4444static int 4445ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4446 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4447{ 4448 struct ctl_lun *nlun, *lun; 4449 struct ctl_port *port; 4450 struct scsi_vpd_id_descriptor *desc; 4451 struct scsi_vpd_id_t10 *t10id; 4452 const char *eui, *naa, *scsiname, *vendor, *value; 4453 int lun_number, i, lun_malloced; 4454 int devidlen, idlen1, idlen2 = 0, len; 4455 4456 if (be_lun == NULL) 4457 return (EINVAL); 4458 4459 /* 4460 * We currently only support Direct Access or Processor LUN types. 4461 */ 4462 switch (be_lun->lun_type) { 4463 case T_DIRECT: 4464 break; 4465 case T_PROCESSOR: 4466 break; 4467 case T_SEQUENTIAL: 4468 case T_CHANGER: 4469 default: 4470 be_lun->lun_config_status(be_lun->be_lun, 4471 CTL_LUN_CONFIG_FAILURE); 4472 break; 4473 } 4474 if (ctl_lun == NULL) { 4475 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4476 lun_malloced = 1; 4477 } else { 4478 lun_malloced = 0; 4479 lun = ctl_lun; 4480 } 4481 4482 memset(lun, 0, sizeof(*lun)); 4483 if (lun_malloced) 4484 lun->flags = CTL_LUN_MALLOCED; 4485 4486 /* Generate LUN ID. */ 4487 devidlen = max(CTL_DEVID_MIN_LEN, 4488 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4489 idlen1 = sizeof(*t10id) + devidlen; 4490 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4491 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4492 if (scsiname != NULL) { 4493 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4494 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4495 } 4496 eui = ctl_get_opt(&be_lun->options, "eui"); 4497 if (eui != NULL) { 4498 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4499 } 4500 naa = ctl_get_opt(&be_lun->options, "naa"); 4501 if (naa != NULL) { 4502 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4503 } 4504 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4505 M_CTL, M_WAITOK | M_ZERO); 4506 lun->lun_devid->len = len; 4507 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4508 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4509 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4510 desc->length = idlen1; 4511 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4512 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4513 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4514 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4515 } else { 4516 strncpy(t10id->vendor, vendor, 4517 min(sizeof(t10id->vendor), strlen(vendor))); 4518 } 4519 strncpy((char *)t10id->vendor_spec_id, 4520 (char *)be_lun->device_id, devidlen); 4521 if (scsiname != NULL) { 4522 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4523 desc->length); 4524 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4525 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4526 SVPD_ID_TYPE_SCSI_NAME; 4527 desc->length = idlen2; 4528 strlcpy(desc->identifier, scsiname, idlen2); 4529 } 4530 if (eui != NULL) { 4531 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4532 desc->length); 4533 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4534 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4535 SVPD_ID_TYPE_EUI64; 4536 desc->length = 8; 4537 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4538 } 4539 if (naa != NULL) { 4540 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4541 desc->length); 4542 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4543 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4544 SVPD_ID_TYPE_NAA; 4545 desc->length = 8; 4546 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4547 } 4548 4549 mtx_lock(&ctl_softc->ctl_lock); 4550 /* 4551 * See if the caller requested a particular LUN number. If so, see 4552 * if it is available. Otherwise, allocate the first available LUN. 4553 */ 4554 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4555 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4556 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4557 mtx_unlock(&ctl_softc->ctl_lock); 4558 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4559 printf("ctl: requested LUN ID %d is higher " 4560 "than CTL_MAX_LUNS - 1 (%d)\n", 4561 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4562 } else { 4563 /* 4564 * XXX KDM return an error, or just assign 4565 * another LUN ID in this case?? 4566 */ 4567 printf("ctl: requested LUN ID %d is already " 4568 "in use\n", be_lun->req_lun_id); 4569 } 4570 if (lun->flags & CTL_LUN_MALLOCED) 4571 free(lun, M_CTL); 4572 be_lun->lun_config_status(be_lun->be_lun, 4573 CTL_LUN_CONFIG_FAILURE); 4574 return (ENOSPC); 4575 } 4576 lun_number = be_lun->req_lun_id; 4577 } else { 4578 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4579 if (lun_number == -1) { 4580 mtx_unlock(&ctl_softc->ctl_lock); 4581 printf("ctl: can't allocate LUN on target %ju, out of " 4582 "LUNs\n", (uintmax_t)target_id.id); 4583 if (lun->flags & CTL_LUN_MALLOCED) 4584 free(lun, M_CTL); 4585 be_lun->lun_config_status(be_lun->be_lun, 4586 CTL_LUN_CONFIG_FAILURE); 4587 return (ENOSPC); 4588 } 4589 } 4590 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4591 4592 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4593 lun->target = target_id; 4594 lun->lun = lun_number; 4595 lun->be_lun = be_lun; 4596 /* 4597 * The processor LUN is always enabled. Disk LUNs come on line 4598 * disabled, and must be enabled by the backend. 4599 */ 4600 lun->flags |= CTL_LUN_DISABLED; 4601 lun->backend = be_lun->be; 4602 be_lun->ctl_lun = lun; 4603 be_lun->lun_id = lun_number; 4604 atomic_add_int(&be_lun->be->num_luns, 1); 4605 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4606 lun->flags |= CTL_LUN_STOPPED; 4607 4608 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4609 lun->flags |= CTL_LUN_INOPERABLE; 4610 4611 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4612 lun->flags |= CTL_LUN_PRIMARY_SC; 4613 4614 value = ctl_get_opt(&be_lun->options, "readonly"); 4615 if (value != NULL && strcmp(value, "on") == 0) 4616 lun->flags |= CTL_LUN_READONLY; 4617 4618 lun->ctl_softc = ctl_softc; 4619 TAILQ_INIT(&lun->ooa_queue); 4620 TAILQ_INIT(&lun->blocked_queue); 4621 STAILQ_INIT(&lun->error_list); 4622 ctl_tpc_lun_init(lun); 4623 4624 /* 4625 * Initialize the mode page index. 4626 */ 4627 ctl_init_page_index(lun); 4628 4629 /* 4630 * Set the poweron UA for all initiators on this LUN only. 4631 */ 4632 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4633 lun->pending_ua[i] = CTL_UA_POWERON; 4634 4635 /* 4636 * Now, before we insert this lun on the lun list, set the lun 4637 * inventory changed UA for all other luns. 4638 */ 4639 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4640 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4641 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4642 } 4643 } 4644 4645 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4646 4647 ctl_softc->ctl_luns[lun_number] = lun; 4648 4649 ctl_softc->num_luns++; 4650 4651 /* Setup statistics gathering */ 4652 lun->stats.device_type = be_lun->lun_type; 4653 lun->stats.lun_number = lun_number; 4654 if (lun->stats.device_type == T_DIRECT) 4655 lun->stats.blocksize = be_lun->blocksize; 4656 else 4657 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4658 for (i = 0;i < CTL_MAX_PORTS;i++) 4659 lun->stats.ports[i].targ_port = i; 4660 4661 mtx_unlock(&ctl_softc->ctl_lock); 4662 4663 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4664 4665 /* 4666 * Run through each registered FETD and bring it online if it isn't 4667 * already. Enable the target ID if it hasn't been enabled, and 4668 * enable this particular LUN. 4669 */ 4670 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4671 int retval; 4672 4673 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4674 if (retval != 0) { 4675 printf("ctl_alloc_lun: FETD %s port %d returned error " 4676 "%d for lun_enable on target %ju lun %d\n", 4677 port->port_name, port->targ_port, retval, 4678 (uintmax_t)target_id.id, lun_number); 4679 } else 4680 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4681 } 4682 return (0); 4683} 4684 4685/* 4686 * Delete a LUN. 4687 * Assumptions: 4688 * - LUN has already been marked invalid and any pending I/O has been taken 4689 * care of. 4690 */ 4691static int 4692ctl_free_lun(struct ctl_lun *lun) 4693{ 4694 struct ctl_softc *softc; 4695#if 0 4696 struct ctl_port *port; 4697#endif 4698 struct ctl_lun *nlun; 4699 int i; 4700 4701 softc = lun->ctl_softc; 4702 4703 mtx_assert(&softc->ctl_lock, MA_OWNED); 4704 4705 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4706 4707 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4708 4709 softc->ctl_luns[lun->lun] = NULL; 4710 4711 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4712 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4713 4714 softc->num_luns--; 4715 4716 /* 4717 * XXX KDM this scheme only works for a single target/multiple LUN 4718 * setup. It needs to be revamped for a multiple target scheme. 4719 * 4720 * XXX KDM this results in port->lun_disable() getting called twice, 4721 * once when ctl_disable_lun() is called, and a second time here. 4722 * We really need to re-think the LUN disable semantics. There 4723 * should probably be several steps/levels to LUN removal: 4724 * - disable 4725 * - invalidate 4726 * - free 4727 * 4728 * Right now we only have a disable method when communicating to 4729 * the front end ports, at least for individual LUNs. 4730 */ 4731#if 0 4732 STAILQ_FOREACH(port, &softc->port_list, links) { 4733 int retval; 4734 4735 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4736 lun->lun); 4737 if (retval != 0) { 4738 printf("ctl_free_lun: FETD %s port %d returned error " 4739 "%d for lun_disable on target %ju lun %jd\n", 4740 port->port_name, port->targ_port, retval, 4741 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4742 } 4743 4744 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4745 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4746 4747 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4748 if (retval != 0) { 4749 printf("ctl_free_lun: FETD %s port %d " 4750 "returned error %d for targ_disable on " 4751 "target %ju\n", port->port_name, 4752 port->targ_port, retval, 4753 (uintmax_t)lun->target.id); 4754 } else 4755 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4756 4757 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4758 continue; 4759 4760#if 0 4761 port->port_offline(port->onoff_arg); 4762 port->status &= ~CTL_PORT_STATUS_ONLINE; 4763#endif 4764 } 4765 } 4766#endif 4767 4768 /* 4769 * Tell the backend to free resources, if this LUN has a backend. 4770 */ 4771 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4772 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4773 4774 ctl_tpc_lun_shutdown(lun); 4775 mtx_destroy(&lun->lun_lock); 4776 free(lun->lun_devid, M_CTL); 4777 if (lun->flags & CTL_LUN_MALLOCED) 4778 free(lun, M_CTL); 4779 4780 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4781 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4782 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4783 } 4784 } 4785 4786 return (0); 4787} 4788 4789static void 4790ctl_create_lun(struct ctl_be_lun *be_lun) 4791{ 4792 struct ctl_softc *ctl_softc; 4793 4794 ctl_softc = control_softc; 4795 4796 /* 4797 * ctl_alloc_lun() should handle all potential failure cases. 4798 */ 4799 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4800} 4801 4802int 4803ctl_add_lun(struct ctl_be_lun *be_lun) 4804{ 4805 struct ctl_softc *ctl_softc = control_softc; 4806 4807 mtx_lock(&ctl_softc->ctl_lock); 4808 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4809 mtx_unlock(&ctl_softc->ctl_lock); 4810 wakeup(&ctl_softc->pending_lun_queue); 4811 4812 return (0); 4813} 4814 4815int 4816ctl_enable_lun(struct ctl_be_lun *be_lun) 4817{ 4818 struct ctl_softc *ctl_softc; 4819 struct ctl_port *port, *nport; 4820 struct ctl_lun *lun; 4821 int retval; 4822 4823 ctl_softc = control_softc; 4824 4825 lun = (struct ctl_lun *)be_lun->ctl_lun; 4826 4827 mtx_lock(&ctl_softc->ctl_lock); 4828 mtx_lock(&lun->lun_lock); 4829 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4830 /* 4831 * eh? Why did we get called if the LUN is already 4832 * enabled? 4833 */ 4834 mtx_unlock(&lun->lun_lock); 4835 mtx_unlock(&ctl_softc->ctl_lock); 4836 return (0); 4837 } 4838 lun->flags &= ~CTL_LUN_DISABLED; 4839 mtx_unlock(&lun->lun_lock); 4840 4841 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4842 nport = STAILQ_NEXT(port, links); 4843 4844 /* 4845 * Drop the lock while we call the FETD's enable routine. 4846 * This can lead to a callback into CTL (at least in the 4847 * case of the internal initiator frontend. 4848 */ 4849 mtx_unlock(&ctl_softc->ctl_lock); 4850 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4851 mtx_lock(&ctl_softc->ctl_lock); 4852 if (retval != 0) { 4853 printf("%s: FETD %s port %d returned error " 4854 "%d for lun_enable on target %ju lun %jd\n", 4855 __func__, port->port_name, port->targ_port, retval, 4856 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4857 } 4858#if 0 4859 else { 4860 /* NOTE: TODO: why does lun enable affect port status? */ 4861 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4862 } 4863#endif 4864 } 4865 4866 mtx_unlock(&ctl_softc->ctl_lock); 4867 4868 return (0); 4869} 4870 4871int 4872ctl_disable_lun(struct ctl_be_lun *be_lun) 4873{ 4874 struct ctl_softc *ctl_softc; 4875 struct ctl_port *port; 4876 struct ctl_lun *lun; 4877 int retval; 4878 4879 ctl_softc = control_softc; 4880 4881 lun = (struct ctl_lun *)be_lun->ctl_lun; 4882 4883 mtx_lock(&ctl_softc->ctl_lock); 4884 mtx_lock(&lun->lun_lock); 4885 if (lun->flags & CTL_LUN_DISABLED) { 4886 mtx_unlock(&lun->lun_lock); 4887 mtx_unlock(&ctl_softc->ctl_lock); 4888 return (0); 4889 } 4890 lun->flags |= CTL_LUN_DISABLED; 4891 mtx_unlock(&lun->lun_lock); 4892 4893 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4894 mtx_unlock(&ctl_softc->ctl_lock); 4895 /* 4896 * Drop the lock before we call the frontend's disable 4897 * routine, to avoid lock order reversals. 4898 * 4899 * XXX KDM what happens if the frontend list changes while 4900 * we're traversing it? It's unlikely, but should be handled. 4901 */ 4902 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4903 lun->lun); 4904 mtx_lock(&ctl_softc->ctl_lock); 4905 if (retval != 0) { 4906 printf("ctl_alloc_lun: FETD %s port %d returned error " 4907 "%d for lun_disable on target %ju lun %jd\n", 4908 port->port_name, port->targ_port, retval, 4909 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4910 } 4911 } 4912 4913 mtx_unlock(&ctl_softc->ctl_lock); 4914 4915 return (0); 4916} 4917 4918int 4919ctl_start_lun(struct ctl_be_lun *be_lun) 4920{ 4921 struct ctl_softc *ctl_softc; 4922 struct ctl_lun *lun; 4923 4924 ctl_softc = control_softc; 4925 4926 lun = (struct ctl_lun *)be_lun->ctl_lun; 4927 4928 mtx_lock(&lun->lun_lock); 4929 lun->flags &= ~CTL_LUN_STOPPED; 4930 mtx_unlock(&lun->lun_lock); 4931 4932 return (0); 4933} 4934 4935int 4936ctl_stop_lun(struct ctl_be_lun *be_lun) 4937{ 4938 struct ctl_softc *ctl_softc; 4939 struct ctl_lun *lun; 4940 4941 ctl_softc = control_softc; 4942 4943 lun = (struct ctl_lun *)be_lun->ctl_lun; 4944 4945 mtx_lock(&lun->lun_lock); 4946 lun->flags |= CTL_LUN_STOPPED; 4947 mtx_unlock(&lun->lun_lock); 4948 4949 return (0); 4950} 4951 4952int 4953ctl_lun_offline(struct ctl_be_lun *be_lun) 4954{ 4955 struct ctl_softc *ctl_softc; 4956 struct ctl_lun *lun; 4957 4958 ctl_softc = control_softc; 4959 4960 lun = (struct ctl_lun *)be_lun->ctl_lun; 4961 4962 mtx_lock(&lun->lun_lock); 4963 lun->flags |= CTL_LUN_OFFLINE; 4964 mtx_unlock(&lun->lun_lock); 4965 4966 return (0); 4967} 4968 4969int 4970ctl_lun_online(struct ctl_be_lun *be_lun) 4971{ 4972 struct ctl_softc *ctl_softc; 4973 struct ctl_lun *lun; 4974 4975 ctl_softc = control_softc; 4976 4977 lun = (struct ctl_lun *)be_lun->ctl_lun; 4978 4979 mtx_lock(&lun->lun_lock); 4980 lun->flags &= ~CTL_LUN_OFFLINE; 4981 mtx_unlock(&lun->lun_lock); 4982 4983 return (0); 4984} 4985 4986int 4987ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4988{ 4989 struct ctl_softc *ctl_softc; 4990 struct ctl_lun *lun; 4991 4992 ctl_softc = control_softc; 4993 4994 lun = (struct ctl_lun *)be_lun->ctl_lun; 4995 4996 mtx_lock(&lun->lun_lock); 4997 4998 /* 4999 * The LUN needs to be disabled before it can be marked invalid. 5000 */ 5001 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 5002 mtx_unlock(&lun->lun_lock); 5003 return (-1); 5004 } 5005 /* 5006 * Mark the LUN invalid. 5007 */ 5008 lun->flags |= CTL_LUN_INVALID; 5009 5010 /* 5011 * If there is nothing in the OOA queue, go ahead and free the LUN. 5012 * If we have something in the OOA queue, we'll free it when the 5013 * last I/O completes. 5014 */ 5015 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5016 mtx_unlock(&lun->lun_lock); 5017 mtx_lock(&ctl_softc->ctl_lock); 5018 ctl_free_lun(lun); 5019 mtx_unlock(&ctl_softc->ctl_lock); 5020 } else 5021 mtx_unlock(&lun->lun_lock); 5022 5023 return (0); 5024} 5025 5026int 5027ctl_lun_inoperable(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_operable(struct ctl_be_lun *be_lun) 5044{ 5045 struct ctl_softc *ctl_softc; 5046 struct ctl_lun *lun; 5047 5048 ctl_softc = control_softc; 5049 lun = (struct ctl_lun *)be_lun->ctl_lun; 5050 5051 mtx_lock(&lun->lun_lock); 5052 lun->flags &= ~CTL_LUN_INOPERABLE; 5053 mtx_unlock(&lun->lun_lock); 5054 5055 return (0); 5056} 5057 5058int 5059ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5060 int lock) 5061{ 5062 struct ctl_softc *softc; 5063 struct ctl_lun *lun; 5064 struct copan_aps_subpage *current_sp; 5065 struct ctl_page_index *page_index; 5066 int i; 5067 5068 softc = control_softc; 5069 5070 mtx_lock(&softc->ctl_lock); 5071 5072 lun = (struct ctl_lun *)be_lun->ctl_lun; 5073 mtx_lock(&lun->lun_lock); 5074 5075 page_index = NULL; 5076 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5077 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5078 APS_PAGE_CODE) 5079 continue; 5080 5081 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5082 continue; 5083 page_index = &lun->mode_pages.index[i]; 5084 } 5085 5086 if (page_index == NULL) { 5087 mtx_unlock(&lun->lun_lock); 5088 mtx_unlock(&softc->ctl_lock); 5089 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5090 (uintmax_t)lun->lun); 5091 return (1); 5092 } 5093#if 0 5094 if ((softc->aps_locked_lun != 0) 5095 && (softc->aps_locked_lun != lun->lun)) { 5096 printf("%s: attempt to lock LUN %llu when %llu is already " 5097 "locked\n"); 5098 mtx_unlock(&lun->lun_lock); 5099 mtx_unlock(&softc->ctl_lock); 5100 return (1); 5101 } 5102#endif 5103 5104 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5105 (page_index->page_len * CTL_PAGE_CURRENT)); 5106 5107 if (lock != 0) { 5108 current_sp->lock_active = APS_LOCK_ACTIVE; 5109 softc->aps_locked_lun = lun->lun; 5110 } else { 5111 current_sp->lock_active = 0; 5112 softc->aps_locked_lun = 0; 5113 } 5114 5115 5116 /* 5117 * If we're in HA mode, try to send the lock message to the other 5118 * side. 5119 */ 5120 if (ctl_is_single == 0) { 5121 int isc_retval; 5122 union ctl_ha_msg lock_msg; 5123 5124 lock_msg.hdr.nexus = *nexus; 5125 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5126 if (lock != 0) 5127 lock_msg.aps.lock_flag = 1; 5128 else 5129 lock_msg.aps.lock_flag = 0; 5130 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5131 sizeof(lock_msg), 0); 5132 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5133 printf("%s: APS (lock=%d) error returned from " 5134 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5135 mtx_unlock(&lun->lun_lock); 5136 mtx_unlock(&softc->ctl_lock); 5137 return (1); 5138 } 5139 } 5140 5141 mtx_unlock(&lun->lun_lock); 5142 mtx_unlock(&softc->ctl_lock); 5143 5144 return (0); 5145} 5146 5147void 5148ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5149{ 5150 struct ctl_lun *lun; 5151 struct ctl_softc *softc; 5152 int i; 5153 5154 softc = control_softc; 5155 5156 lun = (struct ctl_lun *)be_lun->ctl_lun; 5157 5158 mtx_lock(&lun->lun_lock); 5159 5160 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5161 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5162 5163 mtx_unlock(&lun->lun_lock); 5164} 5165 5166/* 5167 * Backend "memory move is complete" callback for requests that never 5168 * make it down to say RAIDCore's configuration code. 5169 */ 5170int 5171ctl_config_move_done(union ctl_io *io) 5172{ 5173 int retval; 5174 5175 retval = CTL_RETVAL_COMPLETE; 5176 5177 5178 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5179 /* 5180 * XXX KDM this shouldn't happen, but what if it does? 5181 */ 5182 if (io->io_hdr.io_type != CTL_IO_SCSI) 5183 panic("I/O type isn't CTL_IO_SCSI!"); 5184 5185 if ((io->io_hdr.port_status == 0) 5186 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5187 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5188 io->io_hdr.status = CTL_SUCCESS; 5189 else if ((io->io_hdr.port_status != 0) 5190 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5191 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5192 /* 5193 * For hardware error sense keys, the sense key 5194 * specific value is defined to be a retry count, 5195 * but we use it to pass back an internal FETD 5196 * error code. XXX KDM Hopefully the FETD is only 5197 * using 16 bits for an error code, since that's 5198 * all the space we have in the sks field. 5199 */ 5200 ctl_set_internal_failure(&io->scsiio, 5201 /*sks_valid*/ 1, 5202 /*retry_count*/ 5203 io->io_hdr.port_status); 5204 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5205 free(io->scsiio.kern_data_ptr, M_CTL); 5206 ctl_done(io); 5207 goto bailout; 5208 } 5209 5210 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5211 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5212 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5213 /* 5214 * XXX KDM just assuming a single pointer here, and not a 5215 * S/G list. If we start using S/G lists for config data, 5216 * we'll need to know how to clean them up here as well. 5217 */ 5218 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5219 free(io->scsiio.kern_data_ptr, M_CTL); 5220 /* Hopefully the user has already set the status... */ 5221 ctl_done(io); 5222 } else { 5223 /* 5224 * XXX KDM now we need to continue data movement. Some 5225 * options: 5226 * - call ctl_scsiio() again? We don't do this for data 5227 * writes, because for those at least we know ahead of 5228 * time where the write will go and how long it is. For 5229 * config writes, though, that information is largely 5230 * contained within the write itself, thus we need to 5231 * parse out the data again. 5232 * 5233 * - Call some other function once the data is in? 5234 */ 5235 5236 /* 5237 * XXX KDM call ctl_scsiio() again for now, and check flag 5238 * bits to see whether we're allocated or not. 5239 */ 5240 retval = ctl_scsiio(&io->scsiio); 5241 } 5242bailout: 5243 return (retval); 5244} 5245 5246/* 5247 * This gets called by a backend driver when it is done with a 5248 * data_submit method. 5249 */ 5250void 5251ctl_data_submit_done(union ctl_io *io) 5252{ 5253 /* 5254 * If the IO_CONT flag is set, we need to call the supplied 5255 * function to continue processing the I/O, instead of completing 5256 * the I/O just yet. 5257 * 5258 * If there is an error, though, we don't want to keep processing. 5259 * Instead, just send status back to the initiator. 5260 */ 5261 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5262 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5263 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5264 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5265 io->scsiio.io_cont(io); 5266 return; 5267 } 5268 ctl_done(io); 5269} 5270 5271/* 5272 * This gets called by a backend driver when it is done with a 5273 * configuration write. 5274 */ 5275void 5276ctl_config_write_done(union ctl_io *io) 5277{ 5278 uint8_t *buf; 5279 5280 /* 5281 * If the IO_CONT flag is set, we need to call the supplied 5282 * function to continue processing the I/O, instead of completing 5283 * the I/O just yet. 5284 * 5285 * If there is an error, though, we don't want to keep processing. 5286 * Instead, just send status back to the initiator. 5287 */ 5288 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5289 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5290 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5291 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5292 io->scsiio.io_cont(io); 5293 return; 5294 } 5295 /* 5296 * Since a configuration write can be done for commands that actually 5297 * have data allocated, like write buffer, and commands that have 5298 * no data, like start/stop unit, we need to check here. 5299 */ 5300 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5301 buf = io->scsiio.kern_data_ptr; 5302 else 5303 buf = NULL; 5304 ctl_done(io); 5305 if (buf) 5306 free(buf, M_CTL); 5307} 5308 5309/* 5310 * SCSI release command. 5311 */ 5312int 5313ctl_scsi_release(struct ctl_scsiio *ctsio) 5314{ 5315 int length, longid, thirdparty_id, resv_id; 5316 struct ctl_softc *ctl_softc; 5317 struct ctl_lun *lun; 5318 uint32_t residx; 5319 5320 length = 0; 5321 resv_id = 0; 5322 5323 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5324 5325 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5326 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5327 ctl_softc = control_softc; 5328 5329 switch (ctsio->cdb[0]) { 5330 case RELEASE_10: { 5331 struct scsi_release_10 *cdb; 5332 5333 cdb = (struct scsi_release_10 *)ctsio->cdb; 5334 5335 if (cdb->byte2 & SR10_LONGID) 5336 longid = 1; 5337 else 5338 thirdparty_id = cdb->thirdparty_id; 5339 5340 resv_id = cdb->resv_id; 5341 length = scsi_2btoul(cdb->length); 5342 break; 5343 } 5344 } 5345 5346 5347 /* 5348 * XXX KDM right now, we only support LUN reservation. We don't 5349 * support 3rd party reservations, or extent reservations, which 5350 * might actually need the parameter list. If we've gotten this 5351 * far, we've got a LUN reservation. Anything else got kicked out 5352 * above. So, according to SPC, ignore the length. 5353 */ 5354 length = 0; 5355 5356 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5357 && (length > 0)) { 5358 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5359 ctsio->kern_data_len = length; 5360 ctsio->kern_total_len = length; 5361 ctsio->kern_data_resid = 0; 5362 ctsio->kern_rel_offset = 0; 5363 ctsio->kern_sg_entries = 0; 5364 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5365 ctsio->be_move_done = ctl_config_move_done; 5366 ctl_datamove((union ctl_io *)ctsio); 5367 5368 return (CTL_RETVAL_COMPLETE); 5369 } 5370 5371 if (length > 0) 5372 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5373 5374 mtx_lock(&lun->lun_lock); 5375 5376 /* 5377 * According to SPC, it is not an error for an intiator to attempt 5378 * to release a reservation on a LUN that isn't reserved, or that 5379 * is reserved by another initiator. The reservation can only be 5380 * released, though, by the initiator who made it or by one of 5381 * several reset type events. 5382 */ 5383 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5384 lun->flags &= ~CTL_LUN_RESERVED; 5385 5386 mtx_unlock(&lun->lun_lock); 5387 5388 ctsio->scsi_status = SCSI_STATUS_OK; 5389 ctsio->io_hdr.status = CTL_SUCCESS; 5390 5391 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5392 free(ctsio->kern_data_ptr, M_CTL); 5393 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5394 } 5395 5396 ctl_done((union ctl_io *)ctsio); 5397 return (CTL_RETVAL_COMPLETE); 5398} 5399 5400int 5401ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5402{ 5403 int extent, thirdparty, longid; 5404 int resv_id, length; 5405 uint64_t thirdparty_id; 5406 struct ctl_softc *ctl_softc; 5407 struct ctl_lun *lun; 5408 uint32_t residx; 5409 5410 extent = 0; 5411 thirdparty = 0; 5412 longid = 0; 5413 resv_id = 0; 5414 length = 0; 5415 thirdparty_id = 0; 5416 5417 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5418 5419 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5420 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5421 ctl_softc = control_softc; 5422 5423 switch (ctsio->cdb[0]) { 5424 case RESERVE_10: { 5425 struct scsi_reserve_10 *cdb; 5426 5427 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5428 5429 if (cdb->byte2 & SR10_LONGID) 5430 longid = 1; 5431 else 5432 thirdparty_id = cdb->thirdparty_id; 5433 5434 resv_id = cdb->resv_id; 5435 length = scsi_2btoul(cdb->length); 5436 break; 5437 } 5438 } 5439 5440 /* 5441 * XXX KDM right now, we only support LUN reservation. We don't 5442 * support 3rd party reservations, or extent reservations, which 5443 * might actually need the parameter list. If we've gotten this 5444 * far, we've got a LUN reservation. Anything else got kicked out 5445 * above. So, according to SPC, ignore the length. 5446 */ 5447 length = 0; 5448 5449 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5450 && (length > 0)) { 5451 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5452 ctsio->kern_data_len = length; 5453 ctsio->kern_total_len = length; 5454 ctsio->kern_data_resid = 0; 5455 ctsio->kern_rel_offset = 0; 5456 ctsio->kern_sg_entries = 0; 5457 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5458 ctsio->be_move_done = ctl_config_move_done; 5459 ctl_datamove((union ctl_io *)ctsio); 5460 5461 return (CTL_RETVAL_COMPLETE); 5462 } 5463 5464 if (length > 0) 5465 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5466 5467 mtx_lock(&lun->lun_lock); 5468 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5469 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5470 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5471 goto bailout; 5472 } 5473 5474 lun->flags |= CTL_LUN_RESERVED; 5475 lun->res_idx = residx; 5476 5477 ctsio->scsi_status = SCSI_STATUS_OK; 5478 ctsio->io_hdr.status = CTL_SUCCESS; 5479 5480bailout: 5481 mtx_unlock(&lun->lun_lock); 5482 5483 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5484 free(ctsio->kern_data_ptr, M_CTL); 5485 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5486 } 5487 5488 ctl_done((union ctl_io *)ctsio); 5489 return (CTL_RETVAL_COMPLETE); 5490} 5491 5492int 5493ctl_start_stop(struct ctl_scsiio *ctsio) 5494{ 5495 struct scsi_start_stop_unit *cdb; 5496 struct ctl_lun *lun; 5497 struct ctl_softc *ctl_softc; 5498 int retval; 5499 5500 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5501 5502 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5503 ctl_softc = control_softc; 5504 retval = 0; 5505 5506 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5507 5508 /* 5509 * XXX KDM 5510 * We don't support the immediate bit on a stop unit. In order to 5511 * do that, we would need to code up a way to know that a stop is 5512 * pending, and hold off any new commands until it completes, one 5513 * way or another. Then we could accept or reject those commands 5514 * depending on its status. We would almost need to do the reverse 5515 * of what we do below for an immediate start -- return the copy of 5516 * the ctl_io to the FETD with status to send to the host (and to 5517 * free the copy!) and then free the original I/O once the stop 5518 * actually completes. That way, the OOA queue mechanism can work 5519 * to block commands that shouldn't proceed. Another alternative 5520 * would be to put the copy in the queue in place of the original, 5521 * and return the original back to the caller. That could be 5522 * slightly safer.. 5523 */ 5524 if ((cdb->byte2 & SSS_IMMED) 5525 && ((cdb->how & SSS_START) == 0)) { 5526 ctl_set_invalid_field(ctsio, 5527 /*sks_valid*/ 1, 5528 /*command*/ 1, 5529 /*field*/ 1, 5530 /*bit_valid*/ 1, 5531 /*bit*/ 0); 5532 ctl_done((union ctl_io *)ctsio); 5533 return (CTL_RETVAL_COMPLETE); 5534 } 5535 5536 if ((lun->flags & CTL_LUN_PR_RESERVED) 5537 && ((cdb->how & SSS_START)==0)) { 5538 uint32_t residx; 5539 5540 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5541 if (!lun->per_res[residx].registered 5542 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5543 5544 ctl_set_reservation_conflict(ctsio); 5545 ctl_done((union ctl_io *)ctsio); 5546 return (CTL_RETVAL_COMPLETE); 5547 } 5548 } 5549 5550 /* 5551 * If there is no backend on this device, we can't start or stop 5552 * it. In theory we shouldn't get any start/stop commands in the 5553 * first place at this level if the LUN doesn't have a backend. 5554 * That should get stopped by the command decode code. 5555 */ 5556 if (lun->backend == NULL) { 5557 ctl_set_invalid_opcode(ctsio); 5558 ctl_done((union ctl_io *)ctsio); 5559 return (CTL_RETVAL_COMPLETE); 5560 } 5561 5562 /* 5563 * XXX KDM Copan-specific offline behavior. 5564 * Figure out a reasonable way to port this? 5565 */ 5566#ifdef NEEDTOPORT 5567 mtx_lock(&lun->lun_lock); 5568 5569 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5570 && (lun->flags & CTL_LUN_OFFLINE)) { 5571 /* 5572 * If the LUN is offline, and the on/offline bit isn't set, 5573 * reject the start or stop. Otherwise, let it through. 5574 */ 5575 mtx_unlock(&lun->lun_lock); 5576 ctl_set_lun_not_ready(ctsio); 5577 ctl_done((union ctl_io *)ctsio); 5578 } else { 5579 mtx_unlock(&lun->lun_lock); 5580#endif /* NEEDTOPORT */ 5581 /* 5582 * This could be a start or a stop when we're online, 5583 * or a stop/offline or start/online. A start or stop when 5584 * we're offline is covered in the case above. 5585 */ 5586 /* 5587 * In the non-immediate case, we send the request to 5588 * the backend and return status to the user when 5589 * it is done. 5590 * 5591 * In the immediate case, we allocate a new ctl_io 5592 * to hold a copy of the request, and send that to 5593 * the backend. We then set good status on the 5594 * user's request and return it immediately. 5595 */ 5596 if (cdb->byte2 & SSS_IMMED) { 5597 union ctl_io *new_io; 5598 5599 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5600 if (new_io == NULL) { 5601 ctl_set_busy(ctsio); 5602 ctl_done((union ctl_io *)ctsio); 5603 } else { 5604 ctl_copy_io((union ctl_io *)ctsio, 5605 new_io); 5606 retval = lun->backend->config_write(new_io); 5607 ctl_set_success(ctsio); 5608 ctl_done((union ctl_io *)ctsio); 5609 } 5610 } else { 5611 retval = lun->backend->config_write( 5612 (union ctl_io *)ctsio); 5613 } 5614#ifdef NEEDTOPORT 5615 } 5616#endif 5617 return (retval); 5618} 5619 5620/* 5621 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5622 * we don't really do anything with the LBA and length fields if the user 5623 * passes them in. Instead we'll just flush out the cache for the entire 5624 * LUN. 5625 */ 5626int 5627ctl_sync_cache(struct ctl_scsiio *ctsio) 5628{ 5629 struct ctl_lun *lun; 5630 struct ctl_softc *ctl_softc; 5631 uint64_t starting_lba; 5632 uint32_t block_count; 5633 int retval; 5634 5635 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5636 5637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5638 ctl_softc = control_softc; 5639 retval = 0; 5640 5641 switch (ctsio->cdb[0]) { 5642 case SYNCHRONIZE_CACHE: { 5643 struct scsi_sync_cache *cdb; 5644 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5645 5646 starting_lba = scsi_4btoul(cdb->begin_lba); 5647 block_count = scsi_2btoul(cdb->lb_count); 5648 break; 5649 } 5650 case SYNCHRONIZE_CACHE_16: { 5651 struct scsi_sync_cache_16 *cdb; 5652 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5653 5654 starting_lba = scsi_8btou64(cdb->begin_lba); 5655 block_count = scsi_4btoul(cdb->lb_count); 5656 break; 5657 } 5658 default: 5659 ctl_set_invalid_opcode(ctsio); 5660 ctl_done((union ctl_io *)ctsio); 5661 goto bailout; 5662 break; /* NOTREACHED */ 5663 } 5664 5665 /* 5666 * We check the LBA and length, but don't do anything with them. 5667 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5668 * get flushed. This check will just help satisfy anyone who wants 5669 * to see an error for an out of range LBA. 5670 */ 5671 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5672 ctl_set_lba_out_of_range(ctsio); 5673 ctl_done((union ctl_io *)ctsio); 5674 goto bailout; 5675 } 5676 5677 /* 5678 * If this LUN has no backend, we can't flush the cache anyway. 5679 */ 5680 if (lun->backend == NULL) { 5681 ctl_set_invalid_opcode(ctsio); 5682 ctl_done((union ctl_io *)ctsio); 5683 goto bailout; 5684 } 5685 5686 /* 5687 * Check to see whether we're configured to send the SYNCHRONIZE 5688 * CACHE command directly to the back end. 5689 */ 5690 mtx_lock(&lun->lun_lock); 5691 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5692 && (++(lun->sync_count) >= lun->sync_interval)) { 5693 lun->sync_count = 0; 5694 mtx_unlock(&lun->lun_lock); 5695 retval = lun->backend->config_write((union ctl_io *)ctsio); 5696 } else { 5697 mtx_unlock(&lun->lun_lock); 5698 ctl_set_success(ctsio); 5699 ctl_done((union ctl_io *)ctsio); 5700 } 5701 5702bailout: 5703 5704 return (retval); 5705} 5706 5707int 5708ctl_format(struct ctl_scsiio *ctsio) 5709{ 5710 struct scsi_format *cdb; 5711 struct ctl_lun *lun; 5712 struct ctl_softc *ctl_softc; 5713 int length, defect_list_len; 5714 5715 CTL_DEBUG_PRINT(("ctl_format\n")); 5716 5717 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5718 ctl_softc = control_softc; 5719 5720 cdb = (struct scsi_format *)ctsio->cdb; 5721 5722 length = 0; 5723 if (cdb->byte2 & SF_FMTDATA) { 5724 if (cdb->byte2 & SF_LONGLIST) 5725 length = sizeof(struct scsi_format_header_long); 5726 else 5727 length = sizeof(struct scsi_format_header_short); 5728 } 5729 5730 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5731 && (length > 0)) { 5732 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5733 ctsio->kern_data_len = length; 5734 ctsio->kern_total_len = length; 5735 ctsio->kern_data_resid = 0; 5736 ctsio->kern_rel_offset = 0; 5737 ctsio->kern_sg_entries = 0; 5738 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5739 ctsio->be_move_done = ctl_config_move_done; 5740 ctl_datamove((union ctl_io *)ctsio); 5741 5742 return (CTL_RETVAL_COMPLETE); 5743 } 5744 5745 defect_list_len = 0; 5746 5747 if (cdb->byte2 & SF_FMTDATA) { 5748 if (cdb->byte2 & SF_LONGLIST) { 5749 struct scsi_format_header_long *header; 5750 5751 header = (struct scsi_format_header_long *) 5752 ctsio->kern_data_ptr; 5753 5754 defect_list_len = scsi_4btoul(header->defect_list_len); 5755 if (defect_list_len != 0) { 5756 ctl_set_invalid_field(ctsio, 5757 /*sks_valid*/ 1, 5758 /*command*/ 0, 5759 /*field*/ 2, 5760 /*bit_valid*/ 0, 5761 /*bit*/ 0); 5762 goto bailout; 5763 } 5764 } else { 5765 struct scsi_format_header_short *header; 5766 5767 header = (struct scsi_format_header_short *) 5768 ctsio->kern_data_ptr; 5769 5770 defect_list_len = scsi_2btoul(header->defect_list_len); 5771 if (defect_list_len != 0) { 5772 ctl_set_invalid_field(ctsio, 5773 /*sks_valid*/ 1, 5774 /*command*/ 0, 5775 /*field*/ 2, 5776 /*bit_valid*/ 0, 5777 /*bit*/ 0); 5778 goto bailout; 5779 } 5780 } 5781 } 5782 5783 /* 5784 * The format command will clear out the "Medium format corrupted" 5785 * status if set by the configuration code. That status is really 5786 * just a way to notify the host that we have lost the media, and 5787 * get them to issue a command that will basically make them think 5788 * they're blowing away the media. 5789 */ 5790 mtx_lock(&lun->lun_lock); 5791 lun->flags &= ~CTL_LUN_INOPERABLE; 5792 mtx_unlock(&lun->lun_lock); 5793 5794 ctsio->scsi_status = SCSI_STATUS_OK; 5795 ctsio->io_hdr.status = CTL_SUCCESS; 5796bailout: 5797 5798 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5799 free(ctsio->kern_data_ptr, M_CTL); 5800 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5801 } 5802 5803 ctl_done((union ctl_io *)ctsio); 5804 return (CTL_RETVAL_COMPLETE); 5805} 5806 5807int 5808ctl_read_buffer(struct ctl_scsiio *ctsio) 5809{ 5810 struct scsi_read_buffer *cdb; 5811 struct ctl_lun *lun; 5812 int buffer_offset, len; 5813 static uint8_t descr[4]; 5814 static uint8_t echo_descr[4] = { 0 }; 5815 5816 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5817 5818 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5819 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5820 5821 if (lun->flags & CTL_LUN_PR_RESERVED) { 5822 uint32_t residx; 5823 5824 /* 5825 * XXX KDM need a lock here. 5826 */ 5827 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5828 if ((lun->res_type == SPR_TYPE_EX_AC 5829 && residx != lun->pr_res_idx) 5830 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5831 || lun->res_type == SPR_TYPE_EX_AC_AR) 5832 && !lun->per_res[residx].registered)) { 5833 ctl_set_reservation_conflict(ctsio); 5834 ctl_done((union ctl_io *)ctsio); 5835 return (CTL_RETVAL_COMPLETE); 5836 } 5837 } 5838 5839 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5840 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5841 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5842 ctl_set_invalid_field(ctsio, 5843 /*sks_valid*/ 1, 5844 /*command*/ 1, 5845 /*field*/ 1, 5846 /*bit_valid*/ 1, 5847 /*bit*/ 4); 5848 ctl_done((union ctl_io *)ctsio); 5849 return (CTL_RETVAL_COMPLETE); 5850 } 5851 5852 len = scsi_3btoul(cdb->length); 5853 buffer_offset = scsi_3btoul(cdb->offset); 5854 5855 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5856 ctl_set_invalid_field(ctsio, 5857 /*sks_valid*/ 1, 5858 /*command*/ 1, 5859 /*field*/ 6, 5860 /*bit_valid*/ 0, 5861 /*bit*/ 0); 5862 ctl_done((union ctl_io *)ctsio); 5863 return (CTL_RETVAL_COMPLETE); 5864 } 5865 5866 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5867 descr[0] = 0; 5868 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5869 ctsio->kern_data_ptr = descr; 5870 len = min(len, sizeof(descr)); 5871 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5872 ctsio->kern_data_ptr = echo_descr; 5873 len = min(len, sizeof(echo_descr)); 5874 } else 5875 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5876 ctsio->kern_data_len = len; 5877 ctsio->kern_total_len = len; 5878 ctsio->kern_data_resid = 0; 5879 ctsio->kern_rel_offset = 0; 5880 ctsio->kern_sg_entries = 0; 5881 ctsio->be_move_done = ctl_config_move_done; 5882 ctl_datamove((union ctl_io *)ctsio); 5883 5884 return (CTL_RETVAL_COMPLETE); 5885} 5886 5887int 5888ctl_write_buffer(struct ctl_scsiio *ctsio) 5889{ 5890 struct scsi_write_buffer *cdb; 5891 struct ctl_lun *lun; 5892 int buffer_offset, len; 5893 5894 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5895 5896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5897 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5898 5899 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5900 ctl_set_invalid_field(ctsio, 5901 /*sks_valid*/ 1, 5902 /*command*/ 1, 5903 /*field*/ 1, 5904 /*bit_valid*/ 1, 5905 /*bit*/ 4); 5906 ctl_done((union ctl_io *)ctsio); 5907 return (CTL_RETVAL_COMPLETE); 5908 } 5909 5910 len = scsi_3btoul(cdb->length); 5911 buffer_offset = scsi_3btoul(cdb->offset); 5912 5913 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5914 ctl_set_invalid_field(ctsio, 5915 /*sks_valid*/ 1, 5916 /*command*/ 1, 5917 /*field*/ 6, 5918 /*bit_valid*/ 0, 5919 /*bit*/ 0); 5920 ctl_done((union ctl_io *)ctsio); 5921 return (CTL_RETVAL_COMPLETE); 5922 } 5923 5924 /* 5925 * If we've got a kernel request that hasn't been malloced yet, 5926 * malloc it and tell the caller the data buffer is here. 5927 */ 5928 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5929 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5930 ctsio->kern_data_len = len; 5931 ctsio->kern_total_len = len; 5932 ctsio->kern_data_resid = 0; 5933 ctsio->kern_rel_offset = 0; 5934 ctsio->kern_sg_entries = 0; 5935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5936 ctsio->be_move_done = ctl_config_move_done; 5937 ctl_datamove((union ctl_io *)ctsio); 5938 5939 return (CTL_RETVAL_COMPLETE); 5940 } 5941 5942 ctl_done((union ctl_io *)ctsio); 5943 5944 return (CTL_RETVAL_COMPLETE); 5945} 5946 5947int 5948ctl_write_same(struct ctl_scsiio *ctsio) 5949{ 5950 struct ctl_lun *lun; 5951 struct ctl_lba_len_flags *lbalen; 5952 uint64_t lba; 5953 uint32_t num_blocks; 5954 int len, retval; 5955 uint8_t byte2; 5956 5957 retval = CTL_RETVAL_COMPLETE; 5958 5959 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5960 5961 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5962 5963 switch (ctsio->cdb[0]) { 5964 case WRITE_SAME_10: { 5965 struct scsi_write_same_10 *cdb; 5966 5967 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5968 5969 lba = scsi_4btoul(cdb->addr); 5970 num_blocks = scsi_2btoul(cdb->length); 5971 byte2 = cdb->byte2; 5972 break; 5973 } 5974 case WRITE_SAME_16: { 5975 struct scsi_write_same_16 *cdb; 5976 5977 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5978 5979 lba = scsi_8btou64(cdb->addr); 5980 num_blocks = scsi_4btoul(cdb->length); 5981 byte2 = cdb->byte2; 5982 break; 5983 } 5984 default: 5985 /* 5986 * We got a command we don't support. This shouldn't 5987 * happen, commands should be filtered out above us. 5988 */ 5989 ctl_set_invalid_opcode(ctsio); 5990 ctl_done((union ctl_io *)ctsio); 5991 5992 return (CTL_RETVAL_COMPLETE); 5993 break; /* NOTREACHED */ 5994 } 5995 5996 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5997 if ((byte2 & SWS_UNMAP) == 0 && 5998 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5999 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 6000 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 6001 ctl_done((union ctl_io *)ctsio); 6002 return (CTL_RETVAL_COMPLETE); 6003 } 6004 6005 /* 6006 * The first check is to make sure we're in bounds, the second 6007 * check is to catch wrap-around problems. If the lba + num blocks 6008 * is less than the lba, then we've wrapped around and the block 6009 * range is invalid anyway. 6010 */ 6011 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6012 || ((lba + num_blocks) < lba)) { 6013 ctl_set_lba_out_of_range(ctsio); 6014 ctl_done((union ctl_io *)ctsio); 6015 return (CTL_RETVAL_COMPLETE); 6016 } 6017 6018 /* Zero number of blocks means "to the last logical block" */ 6019 if (num_blocks == 0) { 6020 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 6021 ctl_set_invalid_field(ctsio, 6022 /*sks_valid*/ 0, 6023 /*command*/ 1, 6024 /*field*/ 0, 6025 /*bit_valid*/ 0, 6026 /*bit*/ 0); 6027 ctl_done((union ctl_io *)ctsio); 6028 return (CTL_RETVAL_COMPLETE); 6029 } 6030 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6031 } 6032 6033 len = lun->be_lun->blocksize; 6034 6035 /* 6036 * If we've got a kernel request that hasn't been malloced yet, 6037 * malloc it and tell the caller the data buffer is here. 6038 */ 6039 if ((byte2 & SWS_NDOB) == 0 && 6040 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6041 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6042 ctsio->kern_data_len = len; 6043 ctsio->kern_total_len = len; 6044 ctsio->kern_data_resid = 0; 6045 ctsio->kern_rel_offset = 0; 6046 ctsio->kern_sg_entries = 0; 6047 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6048 ctsio->be_move_done = ctl_config_move_done; 6049 ctl_datamove((union ctl_io *)ctsio); 6050 6051 return (CTL_RETVAL_COMPLETE); 6052 } 6053 6054 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6055 lbalen->lba = lba; 6056 lbalen->len = num_blocks; 6057 lbalen->flags = byte2; 6058 retval = lun->backend->config_write((union ctl_io *)ctsio); 6059 6060 return (retval); 6061} 6062 6063int 6064ctl_unmap(struct ctl_scsiio *ctsio) 6065{ 6066 struct ctl_lun *lun; 6067 struct scsi_unmap *cdb; 6068 struct ctl_ptr_len_flags *ptrlen; 6069 struct scsi_unmap_header *hdr; 6070 struct scsi_unmap_desc *buf, *end, *endnz, *range; 6071 uint64_t lba; 6072 uint32_t num_blocks; 6073 int len, retval; 6074 uint8_t byte2; 6075 6076 retval = CTL_RETVAL_COMPLETE; 6077 6078 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6079 6080 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6081 cdb = (struct scsi_unmap *)ctsio->cdb; 6082 6083 len = scsi_2btoul(cdb->length); 6084 byte2 = cdb->byte2; 6085 6086 /* 6087 * If we've got a kernel request that hasn't been malloced yet, 6088 * malloc it and tell the caller the data buffer is here. 6089 */ 6090 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6091 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6092 ctsio->kern_data_len = len; 6093 ctsio->kern_total_len = len; 6094 ctsio->kern_data_resid = 0; 6095 ctsio->kern_rel_offset = 0; 6096 ctsio->kern_sg_entries = 0; 6097 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6098 ctsio->be_move_done = ctl_config_move_done; 6099 ctl_datamove((union ctl_io *)ctsio); 6100 6101 return (CTL_RETVAL_COMPLETE); 6102 } 6103 6104 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6105 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6106 if (len < sizeof (*hdr) || 6107 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6108 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6109 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6110 ctl_set_invalid_field(ctsio, 6111 /*sks_valid*/ 0, 6112 /*command*/ 0, 6113 /*field*/ 0, 6114 /*bit_valid*/ 0, 6115 /*bit*/ 0); 6116 ctl_done((union ctl_io *)ctsio); 6117 return (CTL_RETVAL_COMPLETE); 6118 } 6119 len = scsi_2btoul(hdr->desc_length); 6120 buf = (struct scsi_unmap_desc *)(hdr + 1); 6121 end = buf + len / sizeof(*buf); 6122 6123 endnz = buf; 6124 for (range = buf; range < end; range++) { 6125 lba = scsi_8btou64(range->lba); 6126 num_blocks = scsi_4btoul(range->length); 6127 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6128 || ((lba + num_blocks) < lba)) { 6129 ctl_set_lba_out_of_range(ctsio); 6130 ctl_done((union ctl_io *)ctsio); 6131 return (CTL_RETVAL_COMPLETE); 6132 } 6133 if (num_blocks != 0) 6134 endnz = range + 1; 6135 } 6136 6137 /* 6138 * Block backend can not handle zero last range. 6139 * Filter it out and return if there is nothing left. 6140 */ 6141 len = (uint8_t *)endnz - (uint8_t *)buf; 6142 if (len == 0) { 6143 ctl_set_success(ctsio); 6144 ctl_done((union ctl_io *)ctsio); 6145 return (CTL_RETVAL_COMPLETE); 6146 } 6147 6148 mtx_lock(&lun->lun_lock); 6149 ptrlen = (struct ctl_ptr_len_flags *) 6150 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6151 ptrlen->ptr = (void *)buf; 6152 ptrlen->len = len; 6153 ptrlen->flags = byte2; 6154 ctl_check_blocked(lun); 6155 mtx_unlock(&lun->lun_lock); 6156 6157 retval = lun->backend->config_write((union ctl_io *)ctsio); 6158 return (retval); 6159} 6160 6161/* 6162 * Note that this function currently doesn't actually do anything inside 6163 * CTL to enforce things if the DQue bit is turned on. 6164 * 6165 * Also note that this function can't be used in the default case, because 6166 * the DQue bit isn't set in the changeable mask for the control mode page 6167 * anyway. This is just here as an example for how to implement a page 6168 * handler, and a placeholder in case we want to allow the user to turn 6169 * tagged queueing on and off. 6170 * 6171 * The D_SENSE bit handling is functional, however, and will turn 6172 * descriptor sense on and off for a given LUN. 6173 */ 6174int 6175ctl_control_page_handler(struct ctl_scsiio *ctsio, 6176 struct ctl_page_index *page_index, uint8_t *page_ptr) 6177{ 6178 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6179 struct ctl_lun *lun; 6180 struct ctl_softc *softc; 6181 int set_ua; 6182 uint32_t initidx; 6183 6184 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6185 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6186 set_ua = 0; 6187 6188 user_cp = (struct scsi_control_page *)page_ptr; 6189 current_cp = (struct scsi_control_page *) 6190 (page_index->page_data + (page_index->page_len * 6191 CTL_PAGE_CURRENT)); 6192 saved_cp = (struct scsi_control_page *) 6193 (page_index->page_data + (page_index->page_len * 6194 CTL_PAGE_SAVED)); 6195 6196 softc = control_softc; 6197 6198 mtx_lock(&lun->lun_lock); 6199 if (((current_cp->rlec & SCP_DSENSE) == 0) 6200 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6201 /* 6202 * Descriptor sense is currently turned off and the user 6203 * wants to turn it on. 6204 */ 6205 current_cp->rlec |= SCP_DSENSE; 6206 saved_cp->rlec |= SCP_DSENSE; 6207 lun->flags |= CTL_LUN_SENSE_DESC; 6208 set_ua = 1; 6209 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6210 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6211 /* 6212 * Descriptor sense is currently turned on, and the user 6213 * wants to turn it off. 6214 */ 6215 current_cp->rlec &= ~SCP_DSENSE; 6216 saved_cp->rlec &= ~SCP_DSENSE; 6217 lun->flags &= ~CTL_LUN_SENSE_DESC; 6218 set_ua = 1; 6219 } 6220 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6221 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6222 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6223 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6224 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6225 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6226 set_ua = 1; 6227 } 6228 if ((current_cp->eca_and_aen & SCP_SWP) != 6229 (user_cp->eca_and_aen & SCP_SWP)) { 6230 current_cp->eca_and_aen &= ~SCP_SWP; 6231 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6232 saved_cp->eca_and_aen &= ~SCP_SWP; 6233 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6234 set_ua = 1; 6235 } 6236 if (set_ua != 0) { 6237 int i; 6238 /* 6239 * Let other initiators know that the mode 6240 * parameters for this LUN have changed. 6241 */ 6242 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6243 if (i == initidx) 6244 continue; 6245 6246 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6247 } 6248 } 6249 mtx_unlock(&lun->lun_lock); 6250 6251 return (0); 6252} 6253 6254int 6255ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6256 struct ctl_page_index *page_index, uint8_t *page_ptr) 6257{ 6258 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6259 struct ctl_lun *lun; 6260 int set_ua; 6261 uint32_t initidx; 6262 6263 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6264 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6265 set_ua = 0; 6266 6267 user_cp = (struct scsi_caching_page *)page_ptr; 6268 current_cp = (struct scsi_caching_page *) 6269 (page_index->page_data + (page_index->page_len * 6270 CTL_PAGE_CURRENT)); 6271 saved_cp = (struct scsi_caching_page *) 6272 (page_index->page_data + (page_index->page_len * 6273 CTL_PAGE_SAVED)); 6274 6275 mtx_lock(&lun->lun_lock); 6276 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6277 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6278 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6279 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6280 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6281 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6282 set_ua = 1; 6283 } 6284 if (set_ua != 0) { 6285 int i; 6286 /* 6287 * Let other initiators know that the mode 6288 * parameters for this LUN have changed. 6289 */ 6290 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6291 if (i == initidx) 6292 continue; 6293 6294 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6295 } 6296 } 6297 mtx_unlock(&lun->lun_lock); 6298 6299 return (0); 6300} 6301 6302int 6303ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6304 struct ctl_page_index *page_index, uint8_t *page_ptr) 6305{ 6306 return (0); 6307} 6308 6309int 6310ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6311 struct ctl_page_index *page_index, int pc) 6312{ 6313 struct copan_power_subpage *page; 6314 6315 page = (struct copan_power_subpage *)page_index->page_data + 6316 (page_index->page_len * pc); 6317 6318 switch (pc) { 6319 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6320 /* 6321 * We don't update the changable bits for this page. 6322 */ 6323 break; 6324 case SMS_PAGE_CTRL_CURRENT >> 6: 6325 case SMS_PAGE_CTRL_DEFAULT >> 6: 6326 case SMS_PAGE_CTRL_SAVED >> 6: 6327#ifdef NEEDTOPORT 6328 ctl_update_power_subpage(page); 6329#endif 6330 break; 6331 default: 6332#ifdef NEEDTOPORT 6333 EPRINT(0, "Invalid PC %d!!", pc); 6334#endif 6335 break; 6336 } 6337 return (0); 6338} 6339 6340 6341int 6342ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6343 struct ctl_page_index *page_index, uint8_t *page_ptr) 6344{ 6345 struct copan_aps_subpage *user_sp; 6346 struct copan_aps_subpage *current_sp; 6347 union ctl_modepage_info *modepage_info; 6348 struct ctl_softc *softc; 6349 struct ctl_lun *lun; 6350 int retval; 6351 6352 retval = CTL_RETVAL_COMPLETE; 6353 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6354 (page_index->page_len * CTL_PAGE_CURRENT)); 6355 softc = control_softc; 6356 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6357 6358 user_sp = (struct copan_aps_subpage *)page_ptr; 6359 6360 modepage_info = (union ctl_modepage_info *) 6361 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6362 6363 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6364 modepage_info->header.subpage = page_index->subpage; 6365 modepage_info->aps.lock_active = user_sp->lock_active; 6366 6367 mtx_lock(&softc->ctl_lock); 6368 6369 /* 6370 * If there is a request to lock the LUN and another LUN is locked 6371 * this is an error. If the requested LUN is already locked ignore 6372 * the request. If no LUN is locked attempt to lock it. 6373 * if there is a request to unlock the LUN and the LUN is currently 6374 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6375 * if another LUN is locked or no LUN is locked. 6376 */ 6377 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6378 if (softc->aps_locked_lun == lun->lun) { 6379 /* 6380 * This LUN is already locked, so we're done. 6381 */ 6382 retval = CTL_RETVAL_COMPLETE; 6383 } else if (softc->aps_locked_lun == 0) { 6384 /* 6385 * No one has the lock, pass the request to the 6386 * backend. 6387 */ 6388 retval = lun->backend->config_write( 6389 (union ctl_io *)ctsio); 6390 } else { 6391 /* 6392 * Someone else has the lock, throw out the request. 6393 */ 6394 ctl_set_already_locked(ctsio); 6395 free(ctsio->kern_data_ptr, M_CTL); 6396 ctl_done((union ctl_io *)ctsio); 6397 6398 /* 6399 * Set the return value so that ctl_do_mode_select() 6400 * won't try to complete the command. We already 6401 * completed it here. 6402 */ 6403 retval = CTL_RETVAL_ERROR; 6404 } 6405 } else if (softc->aps_locked_lun == lun->lun) { 6406 /* 6407 * This LUN is locked, so pass the unlock request to the 6408 * backend. 6409 */ 6410 retval = lun->backend->config_write((union ctl_io *)ctsio); 6411 } 6412 mtx_unlock(&softc->ctl_lock); 6413 6414 return (retval); 6415} 6416 6417int 6418ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6419 struct ctl_page_index *page_index, 6420 uint8_t *page_ptr) 6421{ 6422 uint8_t *c; 6423 int i; 6424 6425 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6426 ctl_time_io_secs = 6427 (c[0] << 8) | 6428 (c[1] << 0) | 6429 0; 6430 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6431 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6432 printf("page data:"); 6433 for (i=0; i<8; i++) 6434 printf(" %.2x",page_ptr[i]); 6435 printf("\n"); 6436 return (0); 6437} 6438 6439int 6440ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6441 struct ctl_page_index *page_index, 6442 int pc) 6443{ 6444 struct copan_debugconf_subpage *page; 6445 6446 page = (struct copan_debugconf_subpage *)page_index->page_data + 6447 (page_index->page_len * pc); 6448 6449 switch (pc) { 6450 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6451 case SMS_PAGE_CTRL_DEFAULT >> 6: 6452 case SMS_PAGE_CTRL_SAVED >> 6: 6453 /* 6454 * We don't update the changable or default bits for this page. 6455 */ 6456 break; 6457 case SMS_PAGE_CTRL_CURRENT >> 6: 6458 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6459 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6460 break; 6461 default: 6462#ifdef NEEDTOPORT 6463 EPRINT(0, "Invalid PC %d!!", pc); 6464#endif /* NEEDTOPORT */ 6465 break; 6466 } 6467 return (0); 6468} 6469 6470 6471static int 6472ctl_do_mode_select(union ctl_io *io) 6473{ 6474 struct scsi_mode_page_header *page_header; 6475 struct ctl_page_index *page_index; 6476 struct ctl_scsiio *ctsio; 6477 int control_dev, page_len; 6478 int page_len_offset, page_len_size; 6479 union ctl_modepage_info *modepage_info; 6480 struct ctl_lun *lun; 6481 int *len_left, *len_used; 6482 int retval, i; 6483 6484 ctsio = &io->scsiio; 6485 page_index = NULL; 6486 page_len = 0; 6487 retval = CTL_RETVAL_COMPLETE; 6488 6489 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6490 6491 if (lun->be_lun->lun_type != T_DIRECT) 6492 control_dev = 1; 6493 else 6494 control_dev = 0; 6495 6496 modepage_info = (union ctl_modepage_info *) 6497 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6498 len_left = &modepage_info->header.len_left; 6499 len_used = &modepage_info->header.len_used; 6500 6501do_next_page: 6502 6503 page_header = (struct scsi_mode_page_header *) 6504 (ctsio->kern_data_ptr + *len_used); 6505 6506 if (*len_left == 0) { 6507 free(ctsio->kern_data_ptr, M_CTL); 6508 ctl_set_success(ctsio); 6509 ctl_done((union ctl_io *)ctsio); 6510 return (CTL_RETVAL_COMPLETE); 6511 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6512 6513 free(ctsio->kern_data_ptr, M_CTL); 6514 ctl_set_param_len_error(ctsio); 6515 ctl_done((union ctl_io *)ctsio); 6516 return (CTL_RETVAL_COMPLETE); 6517 6518 } else if ((page_header->page_code & SMPH_SPF) 6519 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6520 6521 free(ctsio->kern_data_ptr, M_CTL); 6522 ctl_set_param_len_error(ctsio); 6523 ctl_done((union ctl_io *)ctsio); 6524 return (CTL_RETVAL_COMPLETE); 6525 } 6526 6527 6528 /* 6529 * XXX KDM should we do something with the block descriptor? 6530 */ 6531 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6532 6533 if ((control_dev != 0) 6534 && (lun->mode_pages.index[i].page_flags & 6535 CTL_PAGE_FLAG_DISK_ONLY)) 6536 continue; 6537 6538 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6539 (page_header->page_code & SMPH_PC_MASK)) 6540 continue; 6541 6542 /* 6543 * If neither page has a subpage code, then we've got a 6544 * match. 6545 */ 6546 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6547 && ((page_header->page_code & SMPH_SPF) == 0)) { 6548 page_index = &lun->mode_pages.index[i]; 6549 page_len = page_header->page_length; 6550 break; 6551 } 6552 6553 /* 6554 * If both pages have subpages, then the subpage numbers 6555 * have to match. 6556 */ 6557 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6558 && (page_header->page_code & SMPH_SPF)) { 6559 struct scsi_mode_page_header_sp *sph; 6560 6561 sph = (struct scsi_mode_page_header_sp *)page_header; 6562 6563 if (lun->mode_pages.index[i].subpage == 6564 sph->subpage) { 6565 page_index = &lun->mode_pages.index[i]; 6566 page_len = scsi_2btoul(sph->page_length); 6567 break; 6568 } 6569 } 6570 } 6571 6572 /* 6573 * If we couldn't find the page, or if we don't have a mode select 6574 * handler for it, send back an error to the user. 6575 */ 6576 if ((page_index == NULL) 6577 || (page_index->select_handler == NULL)) { 6578 ctl_set_invalid_field(ctsio, 6579 /*sks_valid*/ 1, 6580 /*command*/ 0, 6581 /*field*/ *len_used, 6582 /*bit_valid*/ 0, 6583 /*bit*/ 0); 6584 free(ctsio->kern_data_ptr, M_CTL); 6585 ctl_done((union ctl_io *)ctsio); 6586 return (CTL_RETVAL_COMPLETE); 6587 } 6588 6589 if (page_index->page_code & SMPH_SPF) { 6590 page_len_offset = 2; 6591 page_len_size = 2; 6592 } else { 6593 page_len_size = 1; 6594 page_len_offset = 1; 6595 } 6596 6597 /* 6598 * If the length the initiator gives us isn't the one we specify in 6599 * the mode page header, or if they didn't specify enough data in 6600 * the CDB to avoid truncating this page, kick out the request. 6601 */ 6602 if ((page_len != (page_index->page_len - page_len_offset - 6603 page_len_size)) 6604 || (*len_left < page_index->page_len)) { 6605 6606 6607 ctl_set_invalid_field(ctsio, 6608 /*sks_valid*/ 1, 6609 /*command*/ 0, 6610 /*field*/ *len_used + page_len_offset, 6611 /*bit_valid*/ 0, 6612 /*bit*/ 0); 6613 free(ctsio->kern_data_ptr, M_CTL); 6614 ctl_done((union ctl_io *)ctsio); 6615 return (CTL_RETVAL_COMPLETE); 6616 } 6617 6618 /* 6619 * Run through the mode page, checking to make sure that the bits 6620 * the user changed are actually legal for him to change. 6621 */ 6622 for (i = 0; i < page_index->page_len; i++) { 6623 uint8_t *user_byte, *change_mask, *current_byte; 6624 int bad_bit; 6625 int j; 6626 6627 user_byte = (uint8_t *)page_header + i; 6628 change_mask = page_index->page_data + 6629 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6630 current_byte = page_index->page_data + 6631 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6632 6633 /* 6634 * Check to see whether the user set any bits in this byte 6635 * that he is not allowed to set. 6636 */ 6637 if ((*user_byte & ~(*change_mask)) == 6638 (*current_byte & ~(*change_mask))) 6639 continue; 6640 6641 /* 6642 * Go through bit by bit to determine which one is illegal. 6643 */ 6644 bad_bit = 0; 6645 for (j = 7; j >= 0; j--) { 6646 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6647 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6648 bad_bit = i; 6649 break; 6650 } 6651 } 6652 ctl_set_invalid_field(ctsio, 6653 /*sks_valid*/ 1, 6654 /*command*/ 0, 6655 /*field*/ *len_used + i, 6656 /*bit_valid*/ 1, 6657 /*bit*/ bad_bit); 6658 free(ctsio->kern_data_ptr, M_CTL); 6659 ctl_done((union ctl_io *)ctsio); 6660 return (CTL_RETVAL_COMPLETE); 6661 } 6662 6663 /* 6664 * Decrement these before we call the page handler, since we may 6665 * end up getting called back one way or another before the handler 6666 * returns to this context. 6667 */ 6668 *len_left -= page_index->page_len; 6669 *len_used += page_index->page_len; 6670 6671 retval = page_index->select_handler(ctsio, page_index, 6672 (uint8_t *)page_header); 6673 6674 /* 6675 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6676 * wait until this queued command completes to finish processing 6677 * the mode page. If it returns anything other than 6678 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6679 * already set the sense information, freed the data pointer, and 6680 * completed the io for us. 6681 */ 6682 if (retval != CTL_RETVAL_COMPLETE) 6683 goto bailout_no_done; 6684 6685 /* 6686 * If the initiator sent us more than one page, parse the next one. 6687 */ 6688 if (*len_left > 0) 6689 goto do_next_page; 6690 6691 ctl_set_success(ctsio); 6692 free(ctsio->kern_data_ptr, M_CTL); 6693 ctl_done((union ctl_io *)ctsio); 6694 6695bailout_no_done: 6696 6697 return (CTL_RETVAL_COMPLETE); 6698 6699} 6700 6701int 6702ctl_mode_select(struct ctl_scsiio *ctsio) 6703{ 6704 int param_len, pf, sp; 6705 int header_size, bd_len; 6706 int len_left, len_used; 6707 struct ctl_page_index *page_index; 6708 struct ctl_lun *lun; 6709 int control_dev, page_len; 6710 union ctl_modepage_info *modepage_info; 6711 int retval; 6712 6713 pf = 0; 6714 sp = 0; 6715 page_len = 0; 6716 len_used = 0; 6717 len_left = 0; 6718 retval = 0; 6719 bd_len = 0; 6720 page_index = NULL; 6721 6722 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6723 6724 if (lun->be_lun->lun_type != T_DIRECT) 6725 control_dev = 1; 6726 else 6727 control_dev = 0; 6728 6729 switch (ctsio->cdb[0]) { 6730 case MODE_SELECT_6: { 6731 struct scsi_mode_select_6 *cdb; 6732 6733 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6734 6735 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6736 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6737 6738 param_len = cdb->length; 6739 header_size = sizeof(struct scsi_mode_header_6); 6740 break; 6741 } 6742 case MODE_SELECT_10: { 6743 struct scsi_mode_select_10 *cdb; 6744 6745 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6746 6747 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6748 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6749 6750 param_len = scsi_2btoul(cdb->length); 6751 header_size = sizeof(struct scsi_mode_header_10); 6752 break; 6753 } 6754 default: 6755 ctl_set_invalid_opcode(ctsio); 6756 ctl_done((union ctl_io *)ctsio); 6757 return (CTL_RETVAL_COMPLETE); 6758 break; /* NOTREACHED */ 6759 } 6760 6761 /* 6762 * From SPC-3: 6763 * "A parameter list length of zero indicates that the Data-Out Buffer 6764 * shall be empty. This condition shall not be considered as an error." 6765 */ 6766 if (param_len == 0) { 6767 ctl_set_success(ctsio); 6768 ctl_done((union ctl_io *)ctsio); 6769 return (CTL_RETVAL_COMPLETE); 6770 } 6771 6772 /* 6773 * Since we'll hit this the first time through, prior to 6774 * allocation, we don't need to free a data buffer here. 6775 */ 6776 if (param_len < header_size) { 6777 ctl_set_param_len_error(ctsio); 6778 ctl_done((union ctl_io *)ctsio); 6779 return (CTL_RETVAL_COMPLETE); 6780 } 6781 6782 /* 6783 * Allocate the data buffer and grab the user's data. In theory, 6784 * we shouldn't have to sanity check the parameter list length here 6785 * because the maximum size is 64K. We should be able to malloc 6786 * that much without too many problems. 6787 */ 6788 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6789 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6790 ctsio->kern_data_len = param_len; 6791 ctsio->kern_total_len = param_len; 6792 ctsio->kern_data_resid = 0; 6793 ctsio->kern_rel_offset = 0; 6794 ctsio->kern_sg_entries = 0; 6795 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6796 ctsio->be_move_done = ctl_config_move_done; 6797 ctl_datamove((union ctl_io *)ctsio); 6798 6799 return (CTL_RETVAL_COMPLETE); 6800 } 6801 6802 switch (ctsio->cdb[0]) { 6803 case MODE_SELECT_6: { 6804 struct scsi_mode_header_6 *mh6; 6805 6806 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6807 bd_len = mh6->blk_desc_len; 6808 break; 6809 } 6810 case MODE_SELECT_10: { 6811 struct scsi_mode_header_10 *mh10; 6812 6813 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6814 bd_len = scsi_2btoul(mh10->blk_desc_len); 6815 break; 6816 } 6817 default: 6818 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6819 break; 6820 } 6821 6822 if (param_len < (header_size + bd_len)) { 6823 free(ctsio->kern_data_ptr, M_CTL); 6824 ctl_set_param_len_error(ctsio); 6825 ctl_done((union ctl_io *)ctsio); 6826 return (CTL_RETVAL_COMPLETE); 6827 } 6828 6829 /* 6830 * Set the IO_CONT flag, so that if this I/O gets passed to 6831 * ctl_config_write_done(), it'll get passed back to 6832 * ctl_do_mode_select() for further processing, or completion if 6833 * we're all done. 6834 */ 6835 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6836 ctsio->io_cont = ctl_do_mode_select; 6837 6838 modepage_info = (union ctl_modepage_info *) 6839 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6840 6841 memset(modepage_info, 0, sizeof(*modepage_info)); 6842 6843 len_left = param_len - header_size - bd_len; 6844 len_used = header_size + bd_len; 6845 6846 modepage_info->header.len_left = len_left; 6847 modepage_info->header.len_used = len_used; 6848 6849 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6850} 6851 6852int 6853ctl_mode_sense(struct ctl_scsiio *ctsio) 6854{ 6855 struct ctl_lun *lun; 6856 int pc, page_code, dbd, llba, subpage; 6857 int alloc_len, page_len, header_len, total_len; 6858 struct scsi_mode_block_descr *block_desc; 6859 struct ctl_page_index *page_index; 6860 int control_dev; 6861 6862 dbd = 0; 6863 llba = 0; 6864 block_desc = NULL; 6865 page_index = NULL; 6866 6867 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6868 6869 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6870 6871 if (lun->be_lun->lun_type != T_DIRECT) 6872 control_dev = 1; 6873 else 6874 control_dev = 0; 6875 6876 if (lun->flags & CTL_LUN_PR_RESERVED) { 6877 uint32_t residx; 6878 6879 /* 6880 * XXX KDM need a lock here. 6881 */ 6882 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6883 if ((lun->res_type == SPR_TYPE_EX_AC 6884 && residx != lun->pr_res_idx) 6885 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6886 || lun->res_type == SPR_TYPE_EX_AC_AR) 6887 && !lun->per_res[residx].registered)) { 6888 ctl_set_reservation_conflict(ctsio); 6889 ctl_done((union ctl_io *)ctsio); 6890 return (CTL_RETVAL_COMPLETE); 6891 } 6892 } 6893 6894 switch (ctsio->cdb[0]) { 6895 case MODE_SENSE_6: { 6896 struct scsi_mode_sense_6 *cdb; 6897 6898 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6899 6900 header_len = sizeof(struct scsi_mode_hdr_6); 6901 if (cdb->byte2 & SMS_DBD) 6902 dbd = 1; 6903 else 6904 header_len += sizeof(struct scsi_mode_block_descr); 6905 6906 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6907 page_code = cdb->page & SMS_PAGE_CODE; 6908 subpage = cdb->subpage; 6909 alloc_len = cdb->length; 6910 break; 6911 } 6912 case MODE_SENSE_10: { 6913 struct scsi_mode_sense_10 *cdb; 6914 6915 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6916 6917 header_len = sizeof(struct scsi_mode_hdr_10); 6918 6919 if (cdb->byte2 & SMS_DBD) 6920 dbd = 1; 6921 else 6922 header_len += sizeof(struct scsi_mode_block_descr); 6923 if (cdb->byte2 & SMS10_LLBAA) 6924 llba = 1; 6925 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6926 page_code = cdb->page & SMS_PAGE_CODE; 6927 subpage = cdb->subpage; 6928 alloc_len = scsi_2btoul(cdb->length); 6929 break; 6930 } 6931 default: 6932 ctl_set_invalid_opcode(ctsio); 6933 ctl_done((union ctl_io *)ctsio); 6934 return (CTL_RETVAL_COMPLETE); 6935 break; /* NOTREACHED */ 6936 } 6937 6938 /* 6939 * We have to make a first pass through to calculate the size of 6940 * the pages that match the user's query. Then we allocate enough 6941 * memory to hold it, and actually copy the data into the buffer. 6942 */ 6943 switch (page_code) { 6944 case SMS_ALL_PAGES_PAGE: { 6945 int i; 6946 6947 page_len = 0; 6948 6949 /* 6950 * At the moment, values other than 0 and 0xff here are 6951 * reserved according to SPC-3. 6952 */ 6953 if ((subpage != SMS_SUBPAGE_PAGE_0) 6954 && (subpage != SMS_SUBPAGE_ALL)) { 6955 ctl_set_invalid_field(ctsio, 6956 /*sks_valid*/ 1, 6957 /*command*/ 1, 6958 /*field*/ 3, 6959 /*bit_valid*/ 0, 6960 /*bit*/ 0); 6961 ctl_done((union ctl_io *)ctsio); 6962 return (CTL_RETVAL_COMPLETE); 6963 } 6964 6965 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6966 if ((control_dev != 0) 6967 && (lun->mode_pages.index[i].page_flags & 6968 CTL_PAGE_FLAG_DISK_ONLY)) 6969 continue; 6970 6971 /* 6972 * We don't use this subpage if the user didn't 6973 * request all subpages. 6974 */ 6975 if ((lun->mode_pages.index[i].subpage != 0) 6976 && (subpage == SMS_SUBPAGE_PAGE_0)) 6977 continue; 6978 6979#if 0 6980 printf("found page %#x len %d\n", 6981 lun->mode_pages.index[i].page_code & 6982 SMPH_PC_MASK, 6983 lun->mode_pages.index[i].page_len); 6984#endif 6985 page_len += lun->mode_pages.index[i].page_len; 6986 } 6987 break; 6988 } 6989 default: { 6990 int i; 6991 6992 page_len = 0; 6993 6994 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6995 /* Look for the right page code */ 6996 if ((lun->mode_pages.index[i].page_code & 6997 SMPH_PC_MASK) != page_code) 6998 continue; 6999 7000 /* Look for the right subpage or the subpage wildcard*/ 7001 if ((lun->mode_pages.index[i].subpage != subpage) 7002 && (subpage != SMS_SUBPAGE_ALL)) 7003 continue; 7004 7005 /* Make sure the page is supported for this dev type */ 7006 if ((control_dev != 0) 7007 && (lun->mode_pages.index[i].page_flags & 7008 CTL_PAGE_FLAG_DISK_ONLY)) 7009 continue; 7010 7011#if 0 7012 printf("found page %#x len %d\n", 7013 lun->mode_pages.index[i].page_code & 7014 SMPH_PC_MASK, 7015 lun->mode_pages.index[i].page_len); 7016#endif 7017 7018 page_len += lun->mode_pages.index[i].page_len; 7019 } 7020 7021 if (page_len == 0) { 7022 ctl_set_invalid_field(ctsio, 7023 /*sks_valid*/ 1, 7024 /*command*/ 1, 7025 /*field*/ 2, 7026 /*bit_valid*/ 1, 7027 /*bit*/ 5); 7028 ctl_done((union ctl_io *)ctsio); 7029 return (CTL_RETVAL_COMPLETE); 7030 } 7031 break; 7032 } 7033 } 7034 7035 total_len = header_len + page_len; 7036#if 0 7037 printf("header_len = %d, page_len = %d, total_len = %d\n", 7038 header_len, page_len, total_len); 7039#endif 7040 7041 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7042 ctsio->kern_sg_entries = 0; 7043 ctsio->kern_data_resid = 0; 7044 ctsio->kern_rel_offset = 0; 7045 if (total_len < alloc_len) { 7046 ctsio->residual = alloc_len - total_len; 7047 ctsio->kern_data_len = total_len; 7048 ctsio->kern_total_len = total_len; 7049 } else { 7050 ctsio->residual = 0; 7051 ctsio->kern_data_len = alloc_len; 7052 ctsio->kern_total_len = alloc_len; 7053 } 7054 7055 switch (ctsio->cdb[0]) { 7056 case MODE_SENSE_6: { 7057 struct scsi_mode_hdr_6 *header; 7058 7059 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7060 7061 header->datalen = ctl_min(total_len - 1, 254); 7062 if (control_dev == 0) { 7063 header->dev_specific = 0x10; /* DPOFUA */ 7064 if ((lun->flags & CTL_LUN_READONLY) || 7065 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 7066 .eca_and_aen & SCP_SWP) != 0) 7067 header->dev_specific |= 0x80; /* WP */ 7068 } 7069 if (dbd) 7070 header->block_descr_len = 0; 7071 else 7072 header->block_descr_len = 7073 sizeof(struct scsi_mode_block_descr); 7074 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7075 break; 7076 } 7077 case MODE_SENSE_10: { 7078 struct scsi_mode_hdr_10 *header; 7079 int datalen; 7080 7081 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7082 7083 datalen = ctl_min(total_len - 2, 65533); 7084 scsi_ulto2b(datalen, header->datalen); 7085 if (control_dev == 0) { 7086 header->dev_specific = 0x10; /* DPOFUA */ 7087 if ((lun->flags & CTL_LUN_READONLY) || 7088 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 7089 .eca_and_aen & SCP_SWP) != 0) 7090 header->dev_specific |= 0x80; /* WP */ 7091 } 7092 if (dbd) 7093 scsi_ulto2b(0, header->block_descr_len); 7094 else 7095 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7096 header->block_descr_len); 7097 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7098 break; 7099 } 7100 default: 7101 panic("invalid CDB type %#x", ctsio->cdb[0]); 7102 break; /* NOTREACHED */ 7103 } 7104 7105 /* 7106 * If we've got a disk, use its blocksize in the block 7107 * descriptor. Otherwise, just set it to 0. 7108 */ 7109 if (dbd == 0) { 7110 if (control_dev == 0) 7111 scsi_ulto3b(lun->be_lun->blocksize, 7112 block_desc->block_len); 7113 else 7114 scsi_ulto3b(0, block_desc->block_len); 7115 } 7116 7117 switch (page_code) { 7118 case SMS_ALL_PAGES_PAGE: { 7119 int i, data_used; 7120 7121 data_used = header_len; 7122 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7123 struct ctl_page_index *page_index; 7124 7125 page_index = &lun->mode_pages.index[i]; 7126 7127 if ((control_dev != 0) 7128 && (page_index->page_flags & 7129 CTL_PAGE_FLAG_DISK_ONLY)) 7130 continue; 7131 7132 /* 7133 * We don't use this subpage if the user didn't 7134 * request all subpages. We already checked (above) 7135 * to make sure the user only specified a subpage 7136 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7137 */ 7138 if ((page_index->subpage != 0) 7139 && (subpage == SMS_SUBPAGE_PAGE_0)) 7140 continue; 7141 7142 /* 7143 * Call the handler, if it exists, to update the 7144 * page to the latest values. 7145 */ 7146 if (page_index->sense_handler != NULL) 7147 page_index->sense_handler(ctsio, page_index,pc); 7148 7149 memcpy(ctsio->kern_data_ptr + data_used, 7150 page_index->page_data + 7151 (page_index->page_len * pc), 7152 page_index->page_len); 7153 data_used += page_index->page_len; 7154 } 7155 break; 7156 } 7157 default: { 7158 int i, data_used; 7159 7160 data_used = header_len; 7161 7162 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7163 struct ctl_page_index *page_index; 7164 7165 page_index = &lun->mode_pages.index[i]; 7166 7167 /* Look for the right page code */ 7168 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7169 continue; 7170 7171 /* Look for the right subpage or the subpage wildcard*/ 7172 if ((page_index->subpage != subpage) 7173 && (subpage != SMS_SUBPAGE_ALL)) 7174 continue; 7175 7176 /* Make sure the page is supported for this dev type */ 7177 if ((control_dev != 0) 7178 && (page_index->page_flags & 7179 CTL_PAGE_FLAG_DISK_ONLY)) 7180 continue; 7181 7182 /* 7183 * Call the handler, if it exists, to update the 7184 * page to the latest values. 7185 */ 7186 if (page_index->sense_handler != NULL) 7187 page_index->sense_handler(ctsio, page_index,pc); 7188 7189 memcpy(ctsio->kern_data_ptr + data_used, 7190 page_index->page_data + 7191 (page_index->page_len * pc), 7192 page_index->page_len); 7193 data_used += page_index->page_len; 7194 } 7195 break; 7196 } 7197 } 7198 7199 ctsio->scsi_status = SCSI_STATUS_OK; 7200 7201 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7202 ctsio->be_move_done = ctl_config_move_done; 7203 ctl_datamove((union ctl_io *)ctsio); 7204 7205 return (CTL_RETVAL_COMPLETE); 7206} 7207 7208int 7209ctl_read_capacity(struct ctl_scsiio *ctsio) 7210{ 7211 struct scsi_read_capacity *cdb; 7212 struct scsi_read_capacity_data *data; 7213 struct ctl_lun *lun; 7214 uint32_t lba; 7215 7216 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7217 7218 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7219 7220 lba = scsi_4btoul(cdb->addr); 7221 if (((cdb->pmi & SRC_PMI) == 0) 7222 && (lba != 0)) { 7223 ctl_set_invalid_field(/*ctsio*/ ctsio, 7224 /*sks_valid*/ 1, 7225 /*command*/ 1, 7226 /*field*/ 2, 7227 /*bit_valid*/ 0, 7228 /*bit*/ 0); 7229 ctl_done((union ctl_io *)ctsio); 7230 return (CTL_RETVAL_COMPLETE); 7231 } 7232 7233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7234 7235 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7236 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7237 ctsio->residual = 0; 7238 ctsio->kern_data_len = sizeof(*data); 7239 ctsio->kern_total_len = sizeof(*data); 7240 ctsio->kern_data_resid = 0; 7241 ctsio->kern_rel_offset = 0; 7242 ctsio->kern_sg_entries = 0; 7243 7244 /* 7245 * If the maximum LBA is greater than 0xfffffffe, the user must 7246 * issue a SERVICE ACTION IN (16) command, with the read capacity 7247 * serivce action set. 7248 */ 7249 if (lun->be_lun->maxlba > 0xfffffffe) 7250 scsi_ulto4b(0xffffffff, data->addr); 7251 else 7252 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7253 7254 /* 7255 * XXX KDM this may not be 512 bytes... 7256 */ 7257 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7258 7259 ctsio->scsi_status = SCSI_STATUS_OK; 7260 7261 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7262 ctsio->be_move_done = ctl_config_move_done; 7263 ctl_datamove((union ctl_io *)ctsio); 7264 7265 return (CTL_RETVAL_COMPLETE); 7266} 7267 7268int 7269ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7270{ 7271 struct scsi_read_capacity_16 *cdb; 7272 struct scsi_read_capacity_data_long *data; 7273 struct ctl_lun *lun; 7274 uint64_t lba; 7275 uint32_t alloc_len; 7276 7277 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7278 7279 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7280 7281 alloc_len = scsi_4btoul(cdb->alloc_len); 7282 lba = scsi_8btou64(cdb->addr); 7283 7284 if ((cdb->reladr & SRC16_PMI) 7285 && (lba != 0)) { 7286 ctl_set_invalid_field(/*ctsio*/ ctsio, 7287 /*sks_valid*/ 1, 7288 /*command*/ 1, 7289 /*field*/ 2, 7290 /*bit_valid*/ 0, 7291 /*bit*/ 0); 7292 ctl_done((union ctl_io *)ctsio); 7293 return (CTL_RETVAL_COMPLETE); 7294 } 7295 7296 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7297 7298 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7299 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7300 7301 if (sizeof(*data) < alloc_len) { 7302 ctsio->residual = alloc_len - sizeof(*data); 7303 ctsio->kern_data_len = sizeof(*data); 7304 ctsio->kern_total_len = sizeof(*data); 7305 } else { 7306 ctsio->residual = 0; 7307 ctsio->kern_data_len = alloc_len; 7308 ctsio->kern_total_len = alloc_len; 7309 } 7310 ctsio->kern_data_resid = 0; 7311 ctsio->kern_rel_offset = 0; 7312 ctsio->kern_sg_entries = 0; 7313 7314 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7315 /* XXX KDM this may not be 512 bytes... */ 7316 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7317 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7318 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7319 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7320 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7321 7322 ctsio->scsi_status = SCSI_STATUS_OK; 7323 7324 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7325 ctsio->be_move_done = ctl_config_move_done; 7326 ctl_datamove((union ctl_io *)ctsio); 7327 7328 return (CTL_RETVAL_COMPLETE); 7329} 7330 7331int 7332ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7333{ 7334 struct scsi_maintenance_in *cdb; 7335 int retval; 7336 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7337 int num_target_port_groups, num_target_ports, single; 7338 struct ctl_lun *lun; 7339 struct ctl_softc *softc; 7340 struct ctl_port *port; 7341 struct scsi_target_group_data *rtg_ptr; 7342 struct scsi_target_group_data_extended *rtg_ext_ptr; 7343 struct scsi_target_port_group_descriptor *tpg_desc; 7344 7345 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7346 7347 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7348 softc = control_softc; 7349 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7350 7351 retval = CTL_RETVAL_COMPLETE; 7352 7353 switch (cdb->byte2 & STG_PDF_MASK) { 7354 case STG_PDF_LENGTH: 7355 ext = 0; 7356 break; 7357 case STG_PDF_EXTENDED: 7358 ext = 1; 7359 break; 7360 default: 7361 ctl_set_invalid_field(/*ctsio*/ ctsio, 7362 /*sks_valid*/ 1, 7363 /*command*/ 1, 7364 /*field*/ 2, 7365 /*bit_valid*/ 1, 7366 /*bit*/ 5); 7367 ctl_done((union ctl_io *)ctsio); 7368 return(retval); 7369 } 7370 7371 single = ctl_is_single; 7372 if (single) 7373 num_target_port_groups = 1; 7374 else 7375 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7376 num_target_ports = 0; 7377 mtx_lock(&softc->ctl_lock); 7378 STAILQ_FOREACH(port, &softc->port_list, links) { 7379 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7380 continue; 7381 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7382 continue; 7383 num_target_ports++; 7384 } 7385 mtx_unlock(&softc->ctl_lock); 7386 7387 if (ext) 7388 total_len = sizeof(struct scsi_target_group_data_extended); 7389 else 7390 total_len = sizeof(struct scsi_target_group_data); 7391 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7392 num_target_port_groups + 7393 sizeof(struct scsi_target_port_descriptor) * 7394 num_target_ports * num_target_port_groups; 7395 7396 alloc_len = scsi_4btoul(cdb->length); 7397 7398 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7399 7400 ctsio->kern_sg_entries = 0; 7401 7402 if (total_len < alloc_len) { 7403 ctsio->residual = alloc_len - total_len; 7404 ctsio->kern_data_len = total_len; 7405 ctsio->kern_total_len = total_len; 7406 } else { 7407 ctsio->residual = 0; 7408 ctsio->kern_data_len = alloc_len; 7409 ctsio->kern_total_len = alloc_len; 7410 } 7411 ctsio->kern_data_resid = 0; 7412 ctsio->kern_rel_offset = 0; 7413 7414 if (ext) { 7415 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7416 ctsio->kern_data_ptr; 7417 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7418 rtg_ext_ptr->format_type = 0x10; 7419 rtg_ext_ptr->implicit_transition_time = 0; 7420 tpg_desc = &rtg_ext_ptr->groups[0]; 7421 } else { 7422 rtg_ptr = (struct scsi_target_group_data *) 7423 ctsio->kern_data_ptr; 7424 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7425 tpg_desc = &rtg_ptr->groups[0]; 7426 } 7427 7428 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7429 mtx_lock(&softc->ctl_lock); 7430 for (g = 0; g < num_target_port_groups; g++) { 7431 if (g == pg) 7432 tpg_desc->pref_state = TPG_PRIMARY | 7433 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7434 else 7435 tpg_desc->pref_state = 7436 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7437 tpg_desc->support = TPG_AO_SUP; 7438 if (!single) 7439 tpg_desc->support |= TPG_AN_SUP; 7440 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7441 tpg_desc->status = TPG_IMPLICIT; 7442 pc = 0; 7443 STAILQ_FOREACH(port, &softc->port_list, links) { 7444 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7445 continue; 7446 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7447 CTL_MAX_LUNS) 7448 continue; 7449 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7450 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7451 relative_target_port_identifier); 7452 pc++; 7453 } 7454 tpg_desc->target_port_count = pc; 7455 tpg_desc = (struct scsi_target_port_group_descriptor *) 7456 &tpg_desc->descriptors[pc]; 7457 } 7458 mtx_unlock(&softc->ctl_lock); 7459 7460 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7461 ctsio->be_move_done = ctl_config_move_done; 7462 7463 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7464 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7465 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7466 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7467 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7468 7469 ctl_datamove((union ctl_io *)ctsio); 7470 return(retval); 7471} 7472 7473int 7474ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7475{ 7476 struct ctl_lun *lun; 7477 struct scsi_report_supported_opcodes *cdb; 7478 const struct ctl_cmd_entry *entry, *sentry; 7479 struct scsi_report_supported_opcodes_all *all; 7480 struct scsi_report_supported_opcodes_descr *descr; 7481 struct scsi_report_supported_opcodes_one *one; 7482 int retval; 7483 int alloc_len, total_len; 7484 int opcode, service_action, i, j, num; 7485 7486 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7487 7488 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7489 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7490 7491 retval = CTL_RETVAL_COMPLETE; 7492 7493 opcode = cdb->requested_opcode; 7494 service_action = scsi_2btoul(cdb->requested_service_action); 7495 switch (cdb->options & RSO_OPTIONS_MASK) { 7496 case RSO_OPTIONS_ALL: 7497 num = 0; 7498 for (i = 0; i < 256; i++) { 7499 entry = &ctl_cmd_table[i]; 7500 if (entry->flags & CTL_CMD_FLAG_SA5) { 7501 for (j = 0; j < 32; j++) { 7502 sentry = &((const struct ctl_cmd_entry *) 7503 entry->execute)[j]; 7504 if (ctl_cmd_applicable( 7505 lun->be_lun->lun_type, sentry)) 7506 num++; 7507 } 7508 } else { 7509 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7510 entry)) 7511 num++; 7512 } 7513 } 7514 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7515 num * sizeof(struct scsi_report_supported_opcodes_descr); 7516 break; 7517 case RSO_OPTIONS_OC: 7518 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7519 ctl_set_invalid_field(/*ctsio*/ ctsio, 7520 /*sks_valid*/ 1, 7521 /*command*/ 1, 7522 /*field*/ 2, 7523 /*bit_valid*/ 1, 7524 /*bit*/ 2); 7525 ctl_done((union ctl_io *)ctsio); 7526 return (CTL_RETVAL_COMPLETE); 7527 } 7528 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7529 break; 7530 case RSO_OPTIONS_OC_SA: 7531 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7532 service_action >= 32) { 7533 ctl_set_invalid_field(/*ctsio*/ ctsio, 7534 /*sks_valid*/ 1, 7535 /*command*/ 1, 7536 /*field*/ 2, 7537 /*bit_valid*/ 1, 7538 /*bit*/ 2); 7539 ctl_done((union ctl_io *)ctsio); 7540 return (CTL_RETVAL_COMPLETE); 7541 } 7542 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7543 break; 7544 default: 7545 ctl_set_invalid_field(/*ctsio*/ ctsio, 7546 /*sks_valid*/ 1, 7547 /*command*/ 1, 7548 /*field*/ 2, 7549 /*bit_valid*/ 1, 7550 /*bit*/ 2); 7551 ctl_done((union ctl_io *)ctsio); 7552 return (CTL_RETVAL_COMPLETE); 7553 } 7554 7555 alloc_len = scsi_4btoul(cdb->length); 7556 7557 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7558 7559 ctsio->kern_sg_entries = 0; 7560 7561 if (total_len < alloc_len) { 7562 ctsio->residual = alloc_len - total_len; 7563 ctsio->kern_data_len = total_len; 7564 ctsio->kern_total_len = total_len; 7565 } else { 7566 ctsio->residual = 0; 7567 ctsio->kern_data_len = alloc_len; 7568 ctsio->kern_total_len = alloc_len; 7569 } 7570 ctsio->kern_data_resid = 0; 7571 ctsio->kern_rel_offset = 0; 7572 7573 switch (cdb->options & RSO_OPTIONS_MASK) { 7574 case RSO_OPTIONS_ALL: 7575 all = (struct scsi_report_supported_opcodes_all *) 7576 ctsio->kern_data_ptr; 7577 num = 0; 7578 for (i = 0; i < 256; i++) { 7579 entry = &ctl_cmd_table[i]; 7580 if (entry->flags & CTL_CMD_FLAG_SA5) { 7581 for (j = 0; j < 32; j++) { 7582 sentry = &((const struct ctl_cmd_entry *) 7583 entry->execute)[j]; 7584 if (!ctl_cmd_applicable( 7585 lun->be_lun->lun_type, sentry)) 7586 continue; 7587 descr = &all->descr[num++]; 7588 descr->opcode = i; 7589 scsi_ulto2b(j, descr->service_action); 7590 descr->flags = RSO_SERVACTV; 7591 scsi_ulto2b(sentry->length, 7592 descr->cdb_length); 7593 } 7594 } else { 7595 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7596 entry)) 7597 continue; 7598 descr = &all->descr[num++]; 7599 descr->opcode = i; 7600 scsi_ulto2b(0, descr->service_action); 7601 descr->flags = 0; 7602 scsi_ulto2b(entry->length, descr->cdb_length); 7603 } 7604 } 7605 scsi_ulto4b( 7606 num * sizeof(struct scsi_report_supported_opcodes_descr), 7607 all->length); 7608 break; 7609 case RSO_OPTIONS_OC: 7610 one = (struct scsi_report_supported_opcodes_one *) 7611 ctsio->kern_data_ptr; 7612 entry = &ctl_cmd_table[opcode]; 7613 goto fill_one; 7614 case RSO_OPTIONS_OC_SA: 7615 one = (struct scsi_report_supported_opcodes_one *) 7616 ctsio->kern_data_ptr; 7617 entry = &ctl_cmd_table[opcode]; 7618 entry = &((const struct ctl_cmd_entry *) 7619 entry->execute)[service_action]; 7620fill_one: 7621 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7622 one->support = 3; 7623 scsi_ulto2b(entry->length, one->cdb_length); 7624 one->cdb_usage[0] = opcode; 7625 memcpy(&one->cdb_usage[1], entry->usage, 7626 entry->length - 1); 7627 } else 7628 one->support = 1; 7629 break; 7630 } 7631 7632 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7633 ctsio->be_move_done = ctl_config_move_done; 7634 7635 ctl_datamove((union ctl_io *)ctsio); 7636 return(retval); 7637} 7638 7639int 7640ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7641{ 7642 struct ctl_lun *lun; 7643 struct scsi_report_supported_tmf *cdb; 7644 struct scsi_report_supported_tmf_data *data; 7645 int retval; 7646 int alloc_len, total_len; 7647 7648 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7649 7650 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7651 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7652 7653 retval = CTL_RETVAL_COMPLETE; 7654 7655 total_len = sizeof(struct scsi_report_supported_tmf_data); 7656 alloc_len = scsi_4btoul(cdb->length); 7657 7658 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7659 7660 ctsio->kern_sg_entries = 0; 7661 7662 if (total_len < alloc_len) { 7663 ctsio->residual = alloc_len - total_len; 7664 ctsio->kern_data_len = total_len; 7665 ctsio->kern_total_len = total_len; 7666 } else { 7667 ctsio->residual = 0; 7668 ctsio->kern_data_len = alloc_len; 7669 ctsio->kern_total_len = alloc_len; 7670 } 7671 ctsio->kern_data_resid = 0; 7672 ctsio->kern_rel_offset = 0; 7673 7674 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7675 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7676 data->byte2 |= RST_ITNRS; 7677 7678 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7679 ctsio->be_move_done = ctl_config_move_done; 7680 7681 ctl_datamove((union ctl_io *)ctsio); 7682 return (retval); 7683} 7684 7685int 7686ctl_report_timestamp(struct ctl_scsiio *ctsio) 7687{ 7688 struct ctl_lun *lun; 7689 struct scsi_report_timestamp *cdb; 7690 struct scsi_report_timestamp_data *data; 7691 struct timeval tv; 7692 int64_t timestamp; 7693 int retval; 7694 int alloc_len, total_len; 7695 7696 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7697 7698 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7700 7701 retval = CTL_RETVAL_COMPLETE; 7702 7703 total_len = sizeof(struct scsi_report_timestamp_data); 7704 alloc_len = scsi_4btoul(cdb->length); 7705 7706 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7707 7708 ctsio->kern_sg_entries = 0; 7709 7710 if (total_len < alloc_len) { 7711 ctsio->residual = alloc_len - total_len; 7712 ctsio->kern_data_len = total_len; 7713 ctsio->kern_total_len = total_len; 7714 } else { 7715 ctsio->residual = 0; 7716 ctsio->kern_data_len = alloc_len; 7717 ctsio->kern_total_len = alloc_len; 7718 } 7719 ctsio->kern_data_resid = 0; 7720 ctsio->kern_rel_offset = 0; 7721 7722 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7723 scsi_ulto2b(sizeof(*data) - 2, data->length); 7724 data->origin = RTS_ORIG_OUTSIDE; 7725 getmicrotime(&tv); 7726 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7727 scsi_ulto4b(timestamp >> 16, data->timestamp); 7728 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7729 7730 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7731 ctsio->be_move_done = ctl_config_move_done; 7732 7733 ctl_datamove((union ctl_io *)ctsio); 7734 return (retval); 7735} 7736 7737int 7738ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7739{ 7740 struct scsi_per_res_in *cdb; 7741 int alloc_len, total_len = 0; 7742 /* struct scsi_per_res_in_rsrv in_data; */ 7743 struct ctl_lun *lun; 7744 struct ctl_softc *softc; 7745 7746 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7747 7748 softc = control_softc; 7749 7750 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7751 7752 alloc_len = scsi_2btoul(cdb->length); 7753 7754 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7755 7756retry: 7757 mtx_lock(&lun->lun_lock); 7758 switch (cdb->action) { 7759 case SPRI_RK: /* read keys */ 7760 total_len = sizeof(struct scsi_per_res_in_keys) + 7761 lun->pr_key_count * 7762 sizeof(struct scsi_per_res_key); 7763 break; 7764 case SPRI_RR: /* read reservation */ 7765 if (lun->flags & CTL_LUN_PR_RESERVED) 7766 total_len = sizeof(struct scsi_per_res_in_rsrv); 7767 else 7768 total_len = sizeof(struct scsi_per_res_in_header); 7769 break; 7770 case SPRI_RC: /* report capabilities */ 7771 total_len = sizeof(struct scsi_per_res_cap); 7772 break; 7773 case SPRI_RS: /* read full status */ 7774 total_len = sizeof(struct scsi_per_res_in_header) + 7775 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7776 lun->pr_key_count; 7777 break; 7778 default: 7779 panic("Invalid PR type %x", cdb->action); 7780 } 7781 mtx_unlock(&lun->lun_lock); 7782 7783 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7784 7785 if (total_len < alloc_len) { 7786 ctsio->residual = alloc_len - total_len; 7787 ctsio->kern_data_len = total_len; 7788 ctsio->kern_total_len = total_len; 7789 } else { 7790 ctsio->residual = 0; 7791 ctsio->kern_data_len = alloc_len; 7792 ctsio->kern_total_len = alloc_len; 7793 } 7794 7795 ctsio->kern_data_resid = 0; 7796 ctsio->kern_rel_offset = 0; 7797 ctsio->kern_sg_entries = 0; 7798 7799 mtx_lock(&lun->lun_lock); 7800 switch (cdb->action) { 7801 case SPRI_RK: { // read keys 7802 struct scsi_per_res_in_keys *res_keys; 7803 int i, key_count; 7804 7805 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7806 7807 /* 7808 * We had to drop the lock to allocate our buffer, which 7809 * leaves time for someone to come in with another 7810 * persistent reservation. (That is unlikely, though, 7811 * since this should be the only persistent reservation 7812 * command active right now.) 7813 */ 7814 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7815 (lun->pr_key_count * 7816 sizeof(struct scsi_per_res_key)))){ 7817 mtx_unlock(&lun->lun_lock); 7818 free(ctsio->kern_data_ptr, M_CTL); 7819 printf("%s: reservation length changed, retrying\n", 7820 __func__); 7821 goto retry; 7822 } 7823 7824 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7825 7826 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7827 lun->pr_key_count, res_keys->header.length); 7828 7829 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7830 if (!lun->per_res[i].registered) 7831 continue; 7832 7833 /* 7834 * We used lun->pr_key_count to calculate the 7835 * size to allocate. If it turns out the number of 7836 * initiators with the registered flag set is 7837 * larger than that (i.e. they haven't been kept in 7838 * sync), we've got a problem. 7839 */ 7840 if (key_count >= lun->pr_key_count) { 7841#ifdef NEEDTOPORT 7842 csevent_log(CSC_CTL | CSC_SHELF_SW | 7843 CTL_PR_ERROR, 7844 csevent_LogType_Fault, 7845 csevent_AlertLevel_Yellow, 7846 csevent_FRU_ShelfController, 7847 csevent_FRU_Firmware, 7848 csevent_FRU_Unknown, 7849 "registered keys %d >= key " 7850 "count %d", key_count, 7851 lun->pr_key_count); 7852#endif 7853 key_count++; 7854 continue; 7855 } 7856 memcpy(res_keys->keys[key_count].key, 7857 lun->per_res[i].res_key.key, 7858 ctl_min(sizeof(res_keys->keys[key_count].key), 7859 sizeof(lun->per_res[i].res_key))); 7860 key_count++; 7861 } 7862 break; 7863 } 7864 case SPRI_RR: { // read reservation 7865 struct scsi_per_res_in_rsrv *res; 7866 int tmp_len, header_only; 7867 7868 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7869 7870 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7871 7872 if (lun->flags & CTL_LUN_PR_RESERVED) 7873 { 7874 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7875 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7876 res->header.length); 7877 header_only = 0; 7878 } else { 7879 tmp_len = sizeof(struct scsi_per_res_in_header); 7880 scsi_ulto4b(0, res->header.length); 7881 header_only = 1; 7882 } 7883 7884 /* 7885 * We had to drop the lock to allocate our buffer, which 7886 * leaves time for someone to come in with another 7887 * persistent reservation. (That is unlikely, though, 7888 * since this should be the only persistent reservation 7889 * command active right now.) 7890 */ 7891 if (tmp_len != total_len) { 7892 mtx_unlock(&lun->lun_lock); 7893 free(ctsio->kern_data_ptr, M_CTL); 7894 printf("%s: reservation status changed, retrying\n", 7895 __func__); 7896 goto retry; 7897 } 7898 7899 /* 7900 * No reservation held, so we're done. 7901 */ 7902 if (header_only != 0) 7903 break; 7904 7905 /* 7906 * If the registration is an All Registrants type, the key 7907 * is 0, since it doesn't really matter. 7908 */ 7909 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7910 memcpy(res->data.reservation, 7911 &lun->per_res[lun->pr_res_idx].res_key, 7912 sizeof(struct scsi_per_res_key)); 7913 } 7914 res->data.scopetype = lun->res_type; 7915 break; 7916 } 7917 case SPRI_RC: //report capabilities 7918 { 7919 struct scsi_per_res_cap *res_cap; 7920 uint16_t type_mask; 7921 7922 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7923 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7924 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7925 type_mask = SPRI_TM_WR_EX_AR | 7926 SPRI_TM_EX_AC_RO | 7927 SPRI_TM_WR_EX_RO | 7928 SPRI_TM_EX_AC | 7929 SPRI_TM_WR_EX | 7930 SPRI_TM_EX_AC_AR; 7931 scsi_ulto2b(type_mask, res_cap->type_mask); 7932 break; 7933 } 7934 case SPRI_RS: { // read full status 7935 struct scsi_per_res_in_full *res_status; 7936 struct scsi_per_res_in_full_desc *res_desc; 7937 struct ctl_port *port; 7938 int i, len; 7939 7940 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7941 7942 /* 7943 * We had to drop the lock to allocate our buffer, which 7944 * leaves time for someone to come in with another 7945 * persistent reservation. (That is unlikely, though, 7946 * since this should be the only persistent reservation 7947 * command active right now.) 7948 */ 7949 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7950 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7951 lun->pr_key_count)){ 7952 mtx_unlock(&lun->lun_lock); 7953 free(ctsio->kern_data_ptr, M_CTL); 7954 printf("%s: reservation length changed, retrying\n", 7955 __func__); 7956 goto retry; 7957 } 7958 7959 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7960 7961 res_desc = &res_status->desc[0]; 7962 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7963 if (!lun->per_res[i].registered) 7964 continue; 7965 7966 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7967 sizeof(res_desc->res_key)); 7968 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7969 (lun->pr_res_idx == i || 7970 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7971 res_desc->flags = SPRI_FULL_R_HOLDER; 7972 res_desc->scopetype = lun->res_type; 7973 } 7974 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7975 res_desc->rel_trgt_port_id); 7976 len = 0; 7977 port = softc->ctl_ports[ 7978 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7979 if (port != NULL) 7980 len = ctl_create_iid(port, 7981 i % CTL_MAX_INIT_PER_PORT, 7982 res_desc->transport_id); 7983 scsi_ulto4b(len, res_desc->additional_length); 7984 res_desc = (struct scsi_per_res_in_full_desc *) 7985 &res_desc->transport_id[len]; 7986 } 7987 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7988 res_status->header.length); 7989 break; 7990 } 7991 default: 7992 /* 7993 * This is a bug, because we just checked for this above, 7994 * and should have returned an error. 7995 */ 7996 panic("Invalid PR type %x", cdb->action); 7997 break; /* NOTREACHED */ 7998 } 7999 mtx_unlock(&lun->lun_lock); 8000 8001 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8002 ctsio->be_move_done = ctl_config_move_done; 8003 8004 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 8005 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 8006 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 8007 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 8008 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 8009 8010 ctl_datamove((union ctl_io *)ctsio); 8011 8012 return (CTL_RETVAL_COMPLETE); 8013} 8014 8015/* 8016 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8017 * it should return. 8018 */ 8019static int 8020ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8021 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8022 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8023 struct scsi_per_res_out_parms* param) 8024{ 8025 union ctl_ha_msg persis_io; 8026 int retval, i; 8027 int isc_retval; 8028 8029 retval = 0; 8030 8031 mtx_lock(&lun->lun_lock); 8032 if (sa_res_key == 0) { 8033 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8034 /* validate scope and type */ 8035 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8036 SPR_LU_SCOPE) { 8037 mtx_unlock(&lun->lun_lock); 8038 ctl_set_invalid_field(/*ctsio*/ ctsio, 8039 /*sks_valid*/ 1, 8040 /*command*/ 1, 8041 /*field*/ 2, 8042 /*bit_valid*/ 1, 8043 /*bit*/ 4); 8044 ctl_done((union ctl_io *)ctsio); 8045 return (1); 8046 } 8047 8048 if (type>8 || type==2 || type==4 || type==0) { 8049 mtx_unlock(&lun->lun_lock); 8050 ctl_set_invalid_field(/*ctsio*/ ctsio, 8051 /*sks_valid*/ 1, 8052 /*command*/ 1, 8053 /*field*/ 2, 8054 /*bit_valid*/ 1, 8055 /*bit*/ 0); 8056 ctl_done((union ctl_io *)ctsio); 8057 return (1); 8058 } 8059 8060 /* temporarily unregister this nexus */ 8061 lun->per_res[residx].registered = 0; 8062 8063 /* 8064 * Unregister everybody else and build UA for 8065 * them 8066 */ 8067 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8068 if (lun->per_res[i].registered == 0) 8069 continue; 8070 8071 if (!persis_offset 8072 && i <CTL_MAX_INITIATORS) 8073 lun->pending_ua[i] |= 8074 CTL_UA_REG_PREEMPT; 8075 else if (persis_offset 8076 && i >= persis_offset) 8077 lun->pending_ua[i-persis_offset] |= 8078 CTL_UA_REG_PREEMPT; 8079 lun->per_res[i].registered = 0; 8080 memset(&lun->per_res[i].res_key, 0, 8081 sizeof(struct scsi_per_res_key)); 8082 } 8083 lun->per_res[residx].registered = 1; 8084 lun->pr_key_count = 1; 8085 lun->res_type = type; 8086 if (lun->res_type != SPR_TYPE_WR_EX_AR 8087 && lun->res_type != SPR_TYPE_EX_AC_AR) 8088 lun->pr_res_idx = residx; 8089 8090 /* send msg to other side */ 8091 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8092 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8093 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8094 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8095 persis_io.pr.pr_info.res_type = type; 8096 memcpy(persis_io.pr.pr_info.sa_res_key, 8097 param->serv_act_res_key, 8098 sizeof(param->serv_act_res_key)); 8099 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8100 &persis_io, sizeof(persis_io), 0)) > 8101 CTL_HA_STATUS_SUCCESS) { 8102 printf("CTL:Persis Out error returned " 8103 "from ctl_ha_msg_send %d\n", 8104 isc_retval); 8105 } 8106 } else { 8107 /* not all registrants */ 8108 mtx_unlock(&lun->lun_lock); 8109 free(ctsio->kern_data_ptr, M_CTL); 8110 ctl_set_invalid_field(ctsio, 8111 /*sks_valid*/ 1, 8112 /*command*/ 0, 8113 /*field*/ 8, 8114 /*bit_valid*/ 0, 8115 /*bit*/ 0); 8116 ctl_done((union ctl_io *)ctsio); 8117 return (1); 8118 } 8119 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8120 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8121 int found = 0; 8122 8123 if (res_key == sa_res_key) { 8124 /* special case */ 8125 /* 8126 * The spec implies this is not good but doesn't 8127 * say what to do. There are two choices either 8128 * generate a res conflict or check condition 8129 * with illegal field in parameter data. Since 8130 * that is what is done when the sa_res_key is 8131 * zero I'll take that approach since this has 8132 * to do with the sa_res_key. 8133 */ 8134 mtx_unlock(&lun->lun_lock); 8135 free(ctsio->kern_data_ptr, M_CTL); 8136 ctl_set_invalid_field(ctsio, 8137 /*sks_valid*/ 1, 8138 /*command*/ 0, 8139 /*field*/ 8, 8140 /*bit_valid*/ 0, 8141 /*bit*/ 0); 8142 ctl_done((union ctl_io *)ctsio); 8143 return (1); 8144 } 8145 8146 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8147 if (lun->per_res[i].registered 8148 && memcmp(param->serv_act_res_key, 8149 lun->per_res[i].res_key.key, 8150 sizeof(struct scsi_per_res_key)) != 0) 8151 continue; 8152 8153 found = 1; 8154 lun->per_res[i].registered = 0; 8155 memset(&lun->per_res[i].res_key, 0, 8156 sizeof(struct scsi_per_res_key)); 8157 lun->pr_key_count--; 8158 8159 if (!persis_offset && i < CTL_MAX_INITIATORS) 8160 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8161 else if (persis_offset && i >= persis_offset) 8162 lun->pending_ua[i-persis_offset] |= 8163 CTL_UA_REG_PREEMPT; 8164 } 8165 if (!found) { 8166 mtx_unlock(&lun->lun_lock); 8167 free(ctsio->kern_data_ptr, M_CTL); 8168 ctl_set_reservation_conflict(ctsio); 8169 ctl_done((union ctl_io *)ctsio); 8170 return (CTL_RETVAL_COMPLETE); 8171 } 8172 /* send msg to other side */ 8173 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8174 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8175 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8176 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8177 persis_io.pr.pr_info.res_type = type; 8178 memcpy(persis_io.pr.pr_info.sa_res_key, 8179 param->serv_act_res_key, 8180 sizeof(param->serv_act_res_key)); 8181 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8182 &persis_io, sizeof(persis_io), 0)) > 8183 CTL_HA_STATUS_SUCCESS) { 8184 printf("CTL:Persis Out error returned from " 8185 "ctl_ha_msg_send %d\n", isc_retval); 8186 } 8187 } else { 8188 /* Reserved but not all registrants */ 8189 /* sa_res_key is res holder */ 8190 if (memcmp(param->serv_act_res_key, 8191 lun->per_res[lun->pr_res_idx].res_key.key, 8192 sizeof(struct scsi_per_res_key)) == 0) { 8193 /* validate scope and type */ 8194 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8195 SPR_LU_SCOPE) { 8196 mtx_unlock(&lun->lun_lock); 8197 ctl_set_invalid_field(/*ctsio*/ ctsio, 8198 /*sks_valid*/ 1, 8199 /*command*/ 1, 8200 /*field*/ 2, 8201 /*bit_valid*/ 1, 8202 /*bit*/ 4); 8203 ctl_done((union ctl_io *)ctsio); 8204 return (1); 8205 } 8206 8207 if (type>8 || type==2 || type==4 || type==0) { 8208 mtx_unlock(&lun->lun_lock); 8209 ctl_set_invalid_field(/*ctsio*/ ctsio, 8210 /*sks_valid*/ 1, 8211 /*command*/ 1, 8212 /*field*/ 2, 8213 /*bit_valid*/ 1, 8214 /*bit*/ 0); 8215 ctl_done((union ctl_io *)ctsio); 8216 return (1); 8217 } 8218 8219 /* 8220 * Do the following: 8221 * if sa_res_key != res_key remove all 8222 * registrants w/sa_res_key and generate UA 8223 * for these registrants(Registrations 8224 * Preempted) if it wasn't an exclusive 8225 * reservation generate UA(Reservations 8226 * Preempted) for all other registered nexuses 8227 * if the type has changed. Establish the new 8228 * reservation and holder. If res_key and 8229 * sa_res_key are the same do the above 8230 * except don't unregister the res holder. 8231 */ 8232 8233 /* 8234 * Temporarily unregister so it won't get 8235 * removed or UA generated 8236 */ 8237 lun->per_res[residx].registered = 0; 8238 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8239 if (lun->per_res[i].registered == 0) 8240 continue; 8241 8242 if (memcmp(param->serv_act_res_key, 8243 lun->per_res[i].res_key.key, 8244 sizeof(struct scsi_per_res_key)) == 0) { 8245 lun->per_res[i].registered = 0; 8246 memset(&lun->per_res[i].res_key, 8247 0, 8248 sizeof(struct scsi_per_res_key)); 8249 lun->pr_key_count--; 8250 8251 if (!persis_offset 8252 && i < CTL_MAX_INITIATORS) 8253 lun->pending_ua[i] |= 8254 CTL_UA_REG_PREEMPT; 8255 else if (persis_offset 8256 && i >= persis_offset) 8257 lun->pending_ua[i-persis_offset] |= 8258 CTL_UA_REG_PREEMPT; 8259 } else if (type != lun->res_type 8260 && (lun->res_type == SPR_TYPE_WR_EX_RO 8261 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8262 if (!persis_offset 8263 && i < CTL_MAX_INITIATORS) 8264 lun->pending_ua[i] |= 8265 CTL_UA_RES_RELEASE; 8266 else if (persis_offset 8267 && i >= persis_offset) 8268 lun->pending_ua[ 8269 i-persis_offset] |= 8270 CTL_UA_RES_RELEASE; 8271 } 8272 } 8273 lun->per_res[residx].registered = 1; 8274 lun->res_type = type; 8275 if (lun->res_type != SPR_TYPE_WR_EX_AR 8276 && lun->res_type != SPR_TYPE_EX_AC_AR) 8277 lun->pr_res_idx = residx; 8278 else 8279 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8280 8281 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8282 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8283 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8284 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8285 persis_io.pr.pr_info.res_type = type; 8286 memcpy(persis_io.pr.pr_info.sa_res_key, 8287 param->serv_act_res_key, 8288 sizeof(param->serv_act_res_key)); 8289 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8290 &persis_io, sizeof(persis_io), 0)) > 8291 CTL_HA_STATUS_SUCCESS) { 8292 printf("CTL:Persis Out error returned " 8293 "from ctl_ha_msg_send %d\n", 8294 isc_retval); 8295 } 8296 } else { 8297 /* 8298 * sa_res_key is not the res holder just 8299 * remove registrants 8300 */ 8301 int found=0; 8302 8303 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8304 if (memcmp(param->serv_act_res_key, 8305 lun->per_res[i].res_key.key, 8306 sizeof(struct scsi_per_res_key)) != 0) 8307 continue; 8308 8309 found = 1; 8310 lun->per_res[i].registered = 0; 8311 memset(&lun->per_res[i].res_key, 0, 8312 sizeof(struct scsi_per_res_key)); 8313 lun->pr_key_count--; 8314 8315 if (!persis_offset 8316 && i < CTL_MAX_INITIATORS) 8317 lun->pending_ua[i] |= 8318 CTL_UA_REG_PREEMPT; 8319 else if (persis_offset 8320 && i >= persis_offset) 8321 lun->pending_ua[i-persis_offset] |= 8322 CTL_UA_REG_PREEMPT; 8323 } 8324 8325 if (!found) { 8326 mtx_unlock(&lun->lun_lock); 8327 free(ctsio->kern_data_ptr, M_CTL); 8328 ctl_set_reservation_conflict(ctsio); 8329 ctl_done((union ctl_io *)ctsio); 8330 return (1); 8331 } 8332 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8333 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8334 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8335 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8336 persis_io.pr.pr_info.res_type = type; 8337 memcpy(persis_io.pr.pr_info.sa_res_key, 8338 param->serv_act_res_key, 8339 sizeof(param->serv_act_res_key)); 8340 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8341 &persis_io, sizeof(persis_io), 0)) > 8342 CTL_HA_STATUS_SUCCESS) { 8343 printf("CTL:Persis Out error returned " 8344 "from ctl_ha_msg_send %d\n", 8345 isc_retval); 8346 } 8347 } 8348 } 8349 8350 lun->PRGeneration++; 8351 mtx_unlock(&lun->lun_lock); 8352 8353 return (retval); 8354} 8355 8356static void 8357ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8358{ 8359 int i; 8360 8361 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8362 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8363 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8364 msg->pr.pr_info.sa_res_key, 8365 sizeof(struct scsi_per_res_key)) != 0) { 8366 uint64_t sa_res_key; 8367 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8368 8369 if (sa_res_key == 0) { 8370 /* temporarily unregister this nexus */ 8371 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8372 8373 /* 8374 * Unregister everybody else and build UA for 8375 * them 8376 */ 8377 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8378 if (lun->per_res[i].registered == 0) 8379 continue; 8380 8381 if (!persis_offset 8382 && i < CTL_MAX_INITIATORS) 8383 lun->pending_ua[i] |= 8384 CTL_UA_REG_PREEMPT; 8385 else if (persis_offset && i >= persis_offset) 8386 lun->pending_ua[i - persis_offset] |= 8387 CTL_UA_REG_PREEMPT; 8388 lun->per_res[i].registered = 0; 8389 memset(&lun->per_res[i].res_key, 0, 8390 sizeof(struct scsi_per_res_key)); 8391 } 8392 8393 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8394 lun->pr_key_count = 1; 8395 lun->res_type = msg->pr.pr_info.res_type; 8396 if (lun->res_type != SPR_TYPE_WR_EX_AR 8397 && lun->res_type != SPR_TYPE_EX_AC_AR) 8398 lun->pr_res_idx = msg->pr.pr_info.residx; 8399 } else { 8400 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8401 if (memcmp(msg->pr.pr_info.sa_res_key, 8402 lun->per_res[i].res_key.key, 8403 sizeof(struct scsi_per_res_key)) != 0) 8404 continue; 8405 8406 lun->per_res[i].registered = 0; 8407 memset(&lun->per_res[i].res_key, 0, 8408 sizeof(struct scsi_per_res_key)); 8409 lun->pr_key_count--; 8410 8411 if (!persis_offset 8412 && i < persis_offset) 8413 lun->pending_ua[i] |= 8414 CTL_UA_REG_PREEMPT; 8415 else if (persis_offset 8416 && i >= persis_offset) 8417 lun->pending_ua[i - persis_offset] |= 8418 CTL_UA_REG_PREEMPT; 8419 } 8420 } 8421 } else { 8422 /* 8423 * Temporarily unregister so it won't get removed 8424 * or UA generated 8425 */ 8426 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8427 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8428 if (lun->per_res[i].registered == 0) 8429 continue; 8430 8431 if (memcmp(msg->pr.pr_info.sa_res_key, 8432 lun->per_res[i].res_key.key, 8433 sizeof(struct scsi_per_res_key)) == 0) { 8434 lun->per_res[i].registered = 0; 8435 memset(&lun->per_res[i].res_key, 0, 8436 sizeof(struct scsi_per_res_key)); 8437 lun->pr_key_count--; 8438 if (!persis_offset 8439 && i < CTL_MAX_INITIATORS) 8440 lun->pending_ua[i] |= 8441 CTL_UA_REG_PREEMPT; 8442 else if (persis_offset 8443 && i >= persis_offset) 8444 lun->pending_ua[i - persis_offset] |= 8445 CTL_UA_REG_PREEMPT; 8446 } else if (msg->pr.pr_info.res_type != lun->res_type 8447 && (lun->res_type == SPR_TYPE_WR_EX_RO 8448 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8449 if (!persis_offset 8450 && i < persis_offset) 8451 lun->pending_ua[i] |= 8452 CTL_UA_RES_RELEASE; 8453 else if (persis_offset 8454 && i >= persis_offset) 8455 lun->pending_ua[i - persis_offset] |= 8456 CTL_UA_RES_RELEASE; 8457 } 8458 } 8459 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8460 lun->res_type = msg->pr.pr_info.res_type; 8461 if (lun->res_type != SPR_TYPE_WR_EX_AR 8462 && lun->res_type != SPR_TYPE_EX_AC_AR) 8463 lun->pr_res_idx = msg->pr.pr_info.residx; 8464 else 8465 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8466 } 8467 lun->PRGeneration++; 8468 8469} 8470 8471 8472int 8473ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8474{ 8475 int retval; 8476 int isc_retval; 8477 u_int32_t param_len; 8478 struct scsi_per_res_out *cdb; 8479 struct ctl_lun *lun; 8480 struct scsi_per_res_out_parms* param; 8481 struct ctl_softc *softc; 8482 uint32_t residx; 8483 uint64_t res_key, sa_res_key; 8484 uint8_t type; 8485 union ctl_ha_msg persis_io; 8486 int i; 8487 8488 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8489 8490 retval = CTL_RETVAL_COMPLETE; 8491 8492 softc = control_softc; 8493 8494 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8495 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8496 8497 /* 8498 * We only support whole-LUN scope. The scope & type are ignored for 8499 * register, register and ignore existing key and clear. 8500 * We sometimes ignore scope and type on preempts too!! 8501 * Verify reservation type here as well. 8502 */ 8503 type = cdb->scope_type & SPR_TYPE_MASK; 8504 if ((cdb->action == SPRO_RESERVE) 8505 || (cdb->action == SPRO_RELEASE)) { 8506 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8507 ctl_set_invalid_field(/*ctsio*/ ctsio, 8508 /*sks_valid*/ 1, 8509 /*command*/ 1, 8510 /*field*/ 2, 8511 /*bit_valid*/ 1, 8512 /*bit*/ 4); 8513 ctl_done((union ctl_io *)ctsio); 8514 return (CTL_RETVAL_COMPLETE); 8515 } 8516 8517 if (type>8 || type==2 || type==4 || type==0) { 8518 ctl_set_invalid_field(/*ctsio*/ ctsio, 8519 /*sks_valid*/ 1, 8520 /*command*/ 1, 8521 /*field*/ 2, 8522 /*bit_valid*/ 1, 8523 /*bit*/ 0); 8524 ctl_done((union ctl_io *)ctsio); 8525 return (CTL_RETVAL_COMPLETE); 8526 } 8527 } 8528 8529 param_len = scsi_4btoul(cdb->length); 8530 8531 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8532 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8533 ctsio->kern_data_len = param_len; 8534 ctsio->kern_total_len = param_len; 8535 ctsio->kern_data_resid = 0; 8536 ctsio->kern_rel_offset = 0; 8537 ctsio->kern_sg_entries = 0; 8538 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8539 ctsio->be_move_done = ctl_config_move_done; 8540 ctl_datamove((union ctl_io *)ctsio); 8541 8542 return (CTL_RETVAL_COMPLETE); 8543 } 8544 8545 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8546 8547 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8548 res_key = scsi_8btou64(param->res_key.key); 8549 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8550 8551 /* 8552 * Validate the reservation key here except for SPRO_REG_IGNO 8553 * This must be done for all other service actions 8554 */ 8555 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8556 mtx_lock(&lun->lun_lock); 8557 if (lun->per_res[residx].registered) { 8558 if (memcmp(param->res_key.key, 8559 lun->per_res[residx].res_key.key, 8560 ctl_min(sizeof(param->res_key), 8561 sizeof(lun->per_res[residx].res_key))) != 0) { 8562 /* 8563 * The current key passed in doesn't match 8564 * the one the initiator previously 8565 * registered. 8566 */ 8567 mtx_unlock(&lun->lun_lock); 8568 free(ctsio->kern_data_ptr, M_CTL); 8569 ctl_set_reservation_conflict(ctsio); 8570 ctl_done((union ctl_io *)ctsio); 8571 return (CTL_RETVAL_COMPLETE); 8572 } 8573 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8574 /* 8575 * We are not registered 8576 */ 8577 mtx_unlock(&lun->lun_lock); 8578 free(ctsio->kern_data_ptr, M_CTL); 8579 ctl_set_reservation_conflict(ctsio); 8580 ctl_done((union ctl_io *)ctsio); 8581 return (CTL_RETVAL_COMPLETE); 8582 } else if (res_key != 0) { 8583 /* 8584 * We are not registered and trying to register but 8585 * the register key isn't zero. 8586 */ 8587 mtx_unlock(&lun->lun_lock); 8588 free(ctsio->kern_data_ptr, M_CTL); 8589 ctl_set_reservation_conflict(ctsio); 8590 ctl_done((union ctl_io *)ctsio); 8591 return (CTL_RETVAL_COMPLETE); 8592 } 8593 mtx_unlock(&lun->lun_lock); 8594 } 8595 8596 switch (cdb->action & SPRO_ACTION_MASK) { 8597 case SPRO_REGISTER: 8598 case SPRO_REG_IGNO: { 8599 8600#if 0 8601 printf("Registration received\n"); 8602#endif 8603 8604 /* 8605 * We don't support any of these options, as we report in 8606 * the read capabilities request (see 8607 * ctl_persistent_reserve_in(), above). 8608 */ 8609 if ((param->flags & SPR_SPEC_I_PT) 8610 || (param->flags & SPR_ALL_TG_PT) 8611 || (param->flags & SPR_APTPL)) { 8612 int bit_ptr; 8613 8614 if (param->flags & SPR_APTPL) 8615 bit_ptr = 0; 8616 else if (param->flags & SPR_ALL_TG_PT) 8617 bit_ptr = 2; 8618 else /* SPR_SPEC_I_PT */ 8619 bit_ptr = 3; 8620 8621 free(ctsio->kern_data_ptr, M_CTL); 8622 ctl_set_invalid_field(ctsio, 8623 /*sks_valid*/ 1, 8624 /*command*/ 0, 8625 /*field*/ 20, 8626 /*bit_valid*/ 1, 8627 /*bit*/ bit_ptr); 8628 ctl_done((union ctl_io *)ctsio); 8629 return (CTL_RETVAL_COMPLETE); 8630 } 8631 8632 mtx_lock(&lun->lun_lock); 8633 8634 /* 8635 * The initiator wants to clear the 8636 * key/unregister. 8637 */ 8638 if (sa_res_key == 0) { 8639 if ((res_key == 0 8640 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8641 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8642 && !lun->per_res[residx].registered)) { 8643 mtx_unlock(&lun->lun_lock); 8644 goto done; 8645 } 8646 8647 lun->per_res[residx].registered = 0; 8648 memset(&lun->per_res[residx].res_key, 8649 0, sizeof(lun->per_res[residx].res_key)); 8650 lun->pr_key_count--; 8651 8652 if (residx == lun->pr_res_idx) { 8653 lun->flags &= ~CTL_LUN_PR_RESERVED; 8654 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8655 8656 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8657 || lun->res_type == SPR_TYPE_EX_AC_RO) 8658 && lun->pr_key_count) { 8659 /* 8660 * If the reservation is a registrants 8661 * only type we need to generate a UA 8662 * for other registered inits. The 8663 * sense code should be RESERVATIONS 8664 * RELEASED 8665 */ 8666 8667 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8668 if (lun->per_res[ 8669 i+persis_offset].registered 8670 == 0) 8671 continue; 8672 lun->pending_ua[i] |= 8673 CTL_UA_RES_RELEASE; 8674 } 8675 } 8676 lun->res_type = 0; 8677 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8678 if (lun->pr_key_count==0) { 8679 lun->flags &= ~CTL_LUN_PR_RESERVED; 8680 lun->res_type = 0; 8681 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8682 } 8683 } 8684 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8685 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8686 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8687 persis_io.pr.pr_info.residx = residx; 8688 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8689 &persis_io, sizeof(persis_io), 0 )) > 8690 CTL_HA_STATUS_SUCCESS) { 8691 printf("CTL:Persis Out error returned from " 8692 "ctl_ha_msg_send %d\n", isc_retval); 8693 } 8694 } else /* sa_res_key != 0 */ { 8695 8696 /* 8697 * If we aren't registered currently then increment 8698 * the key count and set the registered flag. 8699 */ 8700 if (!lun->per_res[residx].registered) { 8701 lun->pr_key_count++; 8702 lun->per_res[residx].registered = 1; 8703 } 8704 8705 memcpy(&lun->per_res[residx].res_key, 8706 param->serv_act_res_key, 8707 ctl_min(sizeof(param->serv_act_res_key), 8708 sizeof(lun->per_res[residx].res_key))); 8709 8710 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8711 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8712 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8713 persis_io.pr.pr_info.residx = residx; 8714 memcpy(persis_io.pr.pr_info.sa_res_key, 8715 param->serv_act_res_key, 8716 sizeof(param->serv_act_res_key)); 8717 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8718 &persis_io, sizeof(persis_io), 0)) > 8719 CTL_HA_STATUS_SUCCESS) { 8720 printf("CTL:Persis Out error returned from " 8721 "ctl_ha_msg_send %d\n", isc_retval); 8722 } 8723 } 8724 lun->PRGeneration++; 8725 mtx_unlock(&lun->lun_lock); 8726 8727 break; 8728 } 8729 case SPRO_RESERVE: 8730#if 0 8731 printf("Reserve executed type %d\n", type); 8732#endif 8733 mtx_lock(&lun->lun_lock); 8734 if (lun->flags & CTL_LUN_PR_RESERVED) { 8735 /* 8736 * if this isn't the reservation holder and it's 8737 * not a "all registrants" type or if the type is 8738 * different then we have a conflict 8739 */ 8740 if ((lun->pr_res_idx != residx 8741 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8742 || lun->res_type != type) { 8743 mtx_unlock(&lun->lun_lock); 8744 free(ctsio->kern_data_ptr, M_CTL); 8745 ctl_set_reservation_conflict(ctsio); 8746 ctl_done((union ctl_io *)ctsio); 8747 return (CTL_RETVAL_COMPLETE); 8748 } 8749 mtx_unlock(&lun->lun_lock); 8750 } else /* create a reservation */ { 8751 /* 8752 * If it's not an "all registrants" type record 8753 * reservation holder 8754 */ 8755 if (type != SPR_TYPE_WR_EX_AR 8756 && type != SPR_TYPE_EX_AC_AR) 8757 lun->pr_res_idx = residx; /* Res holder */ 8758 else 8759 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8760 8761 lun->flags |= CTL_LUN_PR_RESERVED; 8762 lun->res_type = type; 8763 8764 mtx_unlock(&lun->lun_lock); 8765 8766 /* send msg to other side */ 8767 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8768 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8769 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8770 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8771 persis_io.pr.pr_info.res_type = type; 8772 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8773 &persis_io, sizeof(persis_io), 0)) > 8774 CTL_HA_STATUS_SUCCESS) { 8775 printf("CTL:Persis Out error returned from " 8776 "ctl_ha_msg_send %d\n", isc_retval); 8777 } 8778 } 8779 break; 8780 8781 case SPRO_RELEASE: 8782 mtx_lock(&lun->lun_lock); 8783 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8784 /* No reservation exists return good status */ 8785 mtx_unlock(&lun->lun_lock); 8786 goto done; 8787 } 8788 /* 8789 * Is this nexus a reservation holder? 8790 */ 8791 if (lun->pr_res_idx != residx 8792 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8793 /* 8794 * not a res holder return good status but 8795 * do nothing 8796 */ 8797 mtx_unlock(&lun->lun_lock); 8798 goto done; 8799 } 8800 8801 if (lun->res_type != type) { 8802 mtx_unlock(&lun->lun_lock); 8803 free(ctsio->kern_data_ptr, M_CTL); 8804 ctl_set_illegal_pr_release(ctsio); 8805 ctl_done((union ctl_io *)ctsio); 8806 return (CTL_RETVAL_COMPLETE); 8807 } 8808 8809 /* okay to release */ 8810 lun->flags &= ~CTL_LUN_PR_RESERVED; 8811 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8812 lun->res_type = 0; 8813 8814 /* 8815 * if this isn't an exclusive access 8816 * res generate UA for all other 8817 * registrants. 8818 */ 8819 if (type != SPR_TYPE_EX_AC 8820 && type != SPR_TYPE_WR_EX) { 8821 /* 8822 * temporarily unregister so we don't generate UA 8823 */ 8824 lun->per_res[residx].registered = 0; 8825 8826 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8827 if (lun->per_res[i+persis_offset].registered 8828 == 0) 8829 continue; 8830 lun->pending_ua[i] |= 8831 CTL_UA_RES_RELEASE; 8832 } 8833 8834 lun->per_res[residx].registered = 1; 8835 } 8836 mtx_unlock(&lun->lun_lock); 8837 /* Send msg to other side */ 8838 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8839 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8840 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8841 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8842 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8843 printf("CTL:Persis Out error returned from " 8844 "ctl_ha_msg_send %d\n", isc_retval); 8845 } 8846 break; 8847 8848 case SPRO_CLEAR: 8849 /* send msg to other side */ 8850 8851 mtx_lock(&lun->lun_lock); 8852 lun->flags &= ~CTL_LUN_PR_RESERVED; 8853 lun->res_type = 0; 8854 lun->pr_key_count = 0; 8855 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8856 8857 8858 memset(&lun->per_res[residx].res_key, 8859 0, sizeof(lun->per_res[residx].res_key)); 8860 lun->per_res[residx].registered = 0; 8861 8862 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8863 if (lun->per_res[i].registered) { 8864 if (!persis_offset && i < CTL_MAX_INITIATORS) 8865 lun->pending_ua[i] |= 8866 CTL_UA_RES_PREEMPT; 8867 else if (persis_offset && i >= persis_offset) 8868 lun->pending_ua[i-persis_offset] |= 8869 CTL_UA_RES_PREEMPT; 8870 8871 memset(&lun->per_res[i].res_key, 8872 0, sizeof(struct scsi_per_res_key)); 8873 lun->per_res[i].registered = 0; 8874 } 8875 lun->PRGeneration++; 8876 mtx_unlock(&lun->lun_lock); 8877 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8878 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8879 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8880 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8881 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8882 printf("CTL:Persis Out error returned from " 8883 "ctl_ha_msg_send %d\n", isc_retval); 8884 } 8885 break; 8886 8887 case SPRO_PREEMPT: { 8888 int nretval; 8889 8890 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8891 residx, ctsio, cdb, param); 8892 if (nretval != 0) 8893 return (CTL_RETVAL_COMPLETE); 8894 break; 8895 } 8896 default: 8897 panic("Invalid PR type %x", cdb->action); 8898 } 8899 8900done: 8901 free(ctsio->kern_data_ptr, M_CTL); 8902 ctl_set_success(ctsio); 8903 ctl_done((union ctl_io *)ctsio); 8904 8905 return (retval); 8906} 8907 8908/* 8909 * This routine is for handling a message from the other SC pertaining to 8910 * persistent reserve out. All the error checking will have been done 8911 * so only perorming the action need be done here to keep the two 8912 * in sync. 8913 */ 8914static void 8915ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8916{ 8917 struct ctl_lun *lun; 8918 struct ctl_softc *softc; 8919 int i; 8920 uint32_t targ_lun; 8921 8922 softc = control_softc; 8923 8924 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8925 lun = softc->ctl_luns[targ_lun]; 8926 mtx_lock(&lun->lun_lock); 8927 switch(msg->pr.pr_info.action) { 8928 case CTL_PR_REG_KEY: 8929 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8930 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8931 lun->pr_key_count++; 8932 } 8933 lun->PRGeneration++; 8934 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8935 msg->pr.pr_info.sa_res_key, 8936 sizeof(struct scsi_per_res_key)); 8937 break; 8938 8939 case CTL_PR_UNREG_KEY: 8940 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8941 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8942 0, sizeof(struct scsi_per_res_key)); 8943 lun->pr_key_count--; 8944 8945 /* XXX Need to see if the reservation has been released */ 8946 /* if so do we need to generate UA? */ 8947 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8948 lun->flags &= ~CTL_LUN_PR_RESERVED; 8949 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8950 8951 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8952 || lun->res_type == SPR_TYPE_EX_AC_RO) 8953 && lun->pr_key_count) { 8954 /* 8955 * If the reservation is a registrants 8956 * only type we need to generate a UA 8957 * for other registered inits. The 8958 * sense code should be RESERVATIONS 8959 * RELEASED 8960 */ 8961 8962 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8963 if (lun->per_res[i+ 8964 persis_offset].registered == 0) 8965 continue; 8966 8967 lun->pending_ua[i] |= 8968 CTL_UA_RES_RELEASE; 8969 } 8970 } 8971 lun->res_type = 0; 8972 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8973 if (lun->pr_key_count==0) { 8974 lun->flags &= ~CTL_LUN_PR_RESERVED; 8975 lun->res_type = 0; 8976 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8977 } 8978 } 8979 lun->PRGeneration++; 8980 break; 8981 8982 case CTL_PR_RESERVE: 8983 lun->flags |= CTL_LUN_PR_RESERVED; 8984 lun->res_type = msg->pr.pr_info.res_type; 8985 lun->pr_res_idx = msg->pr.pr_info.residx; 8986 8987 break; 8988 8989 case CTL_PR_RELEASE: 8990 /* 8991 * if this isn't an exclusive access res generate UA for all 8992 * other registrants. 8993 */ 8994 if (lun->res_type != SPR_TYPE_EX_AC 8995 && lun->res_type != SPR_TYPE_WR_EX) { 8996 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8997 if (lun->per_res[i+persis_offset].registered) 8998 lun->pending_ua[i] |= 8999 CTL_UA_RES_RELEASE; 9000 } 9001 9002 lun->flags &= ~CTL_LUN_PR_RESERVED; 9003 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9004 lun->res_type = 0; 9005 break; 9006 9007 case CTL_PR_PREEMPT: 9008 ctl_pro_preempt_other(lun, msg); 9009 break; 9010 case CTL_PR_CLEAR: 9011 lun->flags &= ~CTL_LUN_PR_RESERVED; 9012 lun->res_type = 0; 9013 lun->pr_key_count = 0; 9014 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9015 9016 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 9017 if (lun->per_res[i].registered == 0) 9018 continue; 9019 if (!persis_offset 9020 && i < CTL_MAX_INITIATORS) 9021 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 9022 else if (persis_offset 9023 && i >= persis_offset) 9024 lun->pending_ua[i-persis_offset] |= 9025 CTL_UA_RES_PREEMPT; 9026 memset(&lun->per_res[i].res_key, 0, 9027 sizeof(struct scsi_per_res_key)); 9028 lun->per_res[i].registered = 0; 9029 } 9030 lun->PRGeneration++; 9031 break; 9032 } 9033 9034 mtx_unlock(&lun->lun_lock); 9035} 9036 9037int 9038ctl_read_write(struct ctl_scsiio *ctsio) 9039{ 9040 struct ctl_lun *lun; 9041 struct ctl_lba_len_flags *lbalen; 9042 uint64_t lba; 9043 uint32_t num_blocks; 9044 int flags, retval; 9045 int isread; 9046 9047 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9048 9049 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9050 9051 flags = 0; 9052 retval = CTL_RETVAL_COMPLETE; 9053 9054 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9055 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9056 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9057 uint32_t residx; 9058 9059 /* 9060 * XXX KDM need a lock here. 9061 */ 9062 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9063 if ((lun->res_type == SPR_TYPE_EX_AC 9064 && residx != lun->pr_res_idx) 9065 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9066 || lun->res_type == SPR_TYPE_EX_AC_AR) 9067 && !lun->per_res[residx].registered)) { 9068 ctl_set_reservation_conflict(ctsio); 9069 ctl_done((union ctl_io *)ctsio); 9070 return (CTL_RETVAL_COMPLETE); 9071 } 9072 } 9073 9074 switch (ctsio->cdb[0]) { 9075 case READ_6: 9076 case WRITE_6: { 9077 struct scsi_rw_6 *cdb; 9078 9079 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9080 9081 lba = scsi_3btoul(cdb->addr); 9082 /* only 5 bits are valid in the most significant address byte */ 9083 lba &= 0x1fffff; 9084 num_blocks = cdb->length; 9085 /* 9086 * This is correct according to SBC-2. 9087 */ 9088 if (num_blocks == 0) 9089 num_blocks = 256; 9090 break; 9091 } 9092 case READ_10: 9093 case WRITE_10: { 9094 struct scsi_rw_10 *cdb; 9095 9096 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9097 if (cdb->byte2 & SRW10_FUA) 9098 flags |= CTL_LLF_FUA; 9099 if (cdb->byte2 & SRW10_DPO) 9100 flags |= CTL_LLF_DPO; 9101 lba = scsi_4btoul(cdb->addr); 9102 num_blocks = scsi_2btoul(cdb->length); 9103 break; 9104 } 9105 case WRITE_VERIFY_10: { 9106 struct scsi_write_verify_10 *cdb; 9107 9108 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9109 flags |= CTL_LLF_FUA; 9110 if (cdb->byte2 & SWV_DPO) 9111 flags |= CTL_LLF_DPO; 9112 lba = scsi_4btoul(cdb->addr); 9113 num_blocks = scsi_2btoul(cdb->length); 9114 break; 9115 } 9116 case READ_12: 9117 case WRITE_12: { 9118 struct scsi_rw_12 *cdb; 9119 9120 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9121 if (cdb->byte2 & SRW12_FUA) 9122 flags |= CTL_LLF_FUA; 9123 if (cdb->byte2 & SRW12_DPO) 9124 flags |= CTL_LLF_DPO; 9125 lba = scsi_4btoul(cdb->addr); 9126 num_blocks = scsi_4btoul(cdb->length); 9127 break; 9128 } 9129 case WRITE_VERIFY_12: { 9130 struct scsi_write_verify_12 *cdb; 9131 9132 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9133 flags |= CTL_LLF_FUA; 9134 if (cdb->byte2 & SWV_DPO) 9135 flags |= CTL_LLF_DPO; 9136 lba = scsi_4btoul(cdb->addr); 9137 num_blocks = scsi_4btoul(cdb->length); 9138 break; 9139 } 9140 case READ_16: 9141 case WRITE_16: { 9142 struct scsi_rw_16 *cdb; 9143 9144 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9145 if (cdb->byte2 & SRW12_FUA) 9146 flags |= CTL_LLF_FUA; 9147 if (cdb->byte2 & SRW12_DPO) 9148 flags |= CTL_LLF_DPO; 9149 lba = scsi_8btou64(cdb->addr); 9150 num_blocks = scsi_4btoul(cdb->length); 9151 break; 9152 } 9153 case WRITE_ATOMIC_16: { 9154 struct scsi_rw_16 *cdb; 9155 9156 if (lun->be_lun->atomicblock == 0) { 9157 ctl_set_invalid_opcode(ctsio); 9158 ctl_done((union ctl_io *)ctsio); 9159 return (CTL_RETVAL_COMPLETE); 9160 } 9161 9162 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9163 if (cdb->byte2 & SRW12_FUA) 9164 flags |= CTL_LLF_FUA; 9165 if (cdb->byte2 & SRW12_DPO) 9166 flags |= CTL_LLF_DPO; 9167 lba = scsi_8btou64(cdb->addr); 9168 num_blocks = scsi_4btoul(cdb->length); 9169 if (num_blocks > lun->be_lun->atomicblock) { 9170 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9171 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9172 /*bit*/ 0); 9173 ctl_done((union ctl_io *)ctsio); 9174 return (CTL_RETVAL_COMPLETE); 9175 } 9176 break; 9177 } 9178 case WRITE_VERIFY_16: { 9179 struct scsi_write_verify_16 *cdb; 9180 9181 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9182 flags |= CTL_LLF_FUA; 9183 if (cdb->byte2 & SWV_DPO) 9184 flags |= CTL_LLF_DPO; 9185 lba = scsi_8btou64(cdb->addr); 9186 num_blocks = scsi_4btoul(cdb->length); 9187 break; 9188 } 9189 default: 9190 /* 9191 * We got a command we don't support. This shouldn't 9192 * happen, commands should be filtered out above us. 9193 */ 9194 ctl_set_invalid_opcode(ctsio); 9195 ctl_done((union ctl_io *)ctsio); 9196 9197 return (CTL_RETVAL_COMPLETE); 9198 break; /* NOTREACHED */ 9199 } 9200 9201 /* 9202 * The first check is to make sure we're in bounds, the second 9203 * check is to catch wrap-around problems. If the lba + num blocks 9204 * is less than the lba, then we've wrapped around and the block 9205 * range is invalid anyway. 9206 */ 9207 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9208 || ((lba + num_blocks) < lba)) { 9209 ctl_set_lba_out_of_range(ctsio); 9210 ctl_done((union ctl_io *)ctsio); 9211 return (CTL_RETVAL_COMPLETE); 9212 } 9213 9214 /* 9215 * According to SBC-3, a transfer length of 0 is not an error. 9216 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9217 * translates to 256 blocks for those commands. 9218 */ 9219 if (num_blocks == 0) { 9220 ctl_set_success(ctsio); 9221 ctl_done((union ctl_io *)ctsio); 9222 return (CTL_RETVAL_COMPLETE); 9223 } 9224 9225 /* Set FUA and/or DPO if caches are disabled. */ 9226 if (isread) { 9227 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9228 SCP_RCD) != 0) 9229 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9230 } else { 9231 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9232 SCP_WCE) == 0) 9233 flags |= CTL_LLF_FUA; 9234 } 9235 9236 lbalen = (struct ctl_lba_len_flags *) 9237 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9238 lbalen->lba = lba; 9239 lbalen->len = num_blocks; 9240 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9241 9242 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9243 ctsio->kern_rel_offset = 0; 9244 9245 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9246 9247 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9248 9249 return (retval); 9250} 9251 9252static int 9253ctl_cnw_cont(union ctl_io *io) 9254{ 9255 struct ctl_scsiio *ctsio; 9256 struct ctl_lun *lun; 9257 struct ctl_lba_len_flags *lbalen; 9258 int retval; 9259 9260 ctsio = &io->scsiio; 9261 ctsio->io_hdr.status = CTL_STATUS_NONE; 9262 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9263 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9264 lbalen = (struct ctl_lba_len_flags *) 9265 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9266 lbalen->flags &= ~CTL_LLF_COMPARE; 9267 lbalen->flags |= CTL_LLF_WRITE; 9268 9269 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9270 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9271 return (retval); 9272} 9273 9274int 9275ctl_cnw(struct ctl_scsiio *ctsio) 9276{ 9277 struct ctl_lun *lun; 9278 struct ctl_lba_len_flags *lbalen; 9279 uint64_t lba; 9280 uint32_t num_blocks; 9281 int flags, retval; 9282 9283 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9284 9285 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9286 9287 flags = 0; 9288 retval = CTL_RETVAL_COMPLETE; 9289 9290 switch (ctsio->cdb[0]) { 9291 case COMPARE_AND_WRITE: { 9292 struct scsi_compare_and_write *cdb; 9293 9294 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9295 if (cdb->byte2 & SRW10_FUA) 9296 flags |= CTL_LLF_FUA; 9297 if (cdb->byte2 & SRW10_DPO) 9298 flags |= CTL_LLF_DPO; 9299 lba = scsi_8btou64(cdb->addr); 9300 num_blocks = cdb->length; 9301 break; 9302 } 9303 default: 9304 /* 9305 * We got a command we don't support. This shouldn't 9306 * happen, commands should be filtered out above us. 9307 */ 9308 ctl_set_invalid_opcode(ctsio); 9309 ctl_done((union ctl_io *)ctsio); 9310 9311 return (CTL_RETVAL_COMPLETE); 9312 break; /* NOTREACHED */ 9313 } 9314 9315 /* 9316 * The first check is to make sure we're in bounds, the second 9317 * check is to catch wrap-around problems. If the lba + num blocks 9318 * is less than the lba, then we've wrapped around and the block 9319 * range is invalid anyway. 9320 */ 9321 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9322 || ((lba + num_blocks) < lba)) { 9323 ctl_set_lba_out_of_range(ctsio); 9324 ctl_done((union ctl_io *)ctsio); 9325 return (CTL_RETVAL_COMPLETE); 9326 } 9327 9328 /* 9329 * According to SBC-3, a transfer length of 0 is not an error. 9330 */ 9331 if (num_blocks == 0) { 9332 ctl_set_success(ctsio); 9333 ctl_done((union ctl_io *)ctsio); 9334 return (CTL_RETVAL_COMPLETE); 9335 } 9336 9337 /* Set FUA if write cache is disabled. */ 9338 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9339 SCP_WCE) == 0) 9340 flags |= CTL_LLF_FUA; 9341 9342 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9343 ctsio->kern_rel_offset = 0; 9344 9345 /* 9346 * Set the IO_CONT flag, so that if this I/O gets passed to 9347 * ctl_data_submit_done(), it'll get passed back to 9348 * ctl_ctl_cnw_cont() for further processing. 9349 */ 9350 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9351 ctsio->io_cont = ctl_cnw_cont; 9352 9353 lbalen = (struct ctl_lba_len_flags *) 9354 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9355 lbalen->lba = lba; 9356 lbalen->len = num_blocks; 9357 lbalen->flags = CTL_LLF_COMPARE | flags; 9358 9359 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9360 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9361 return (retval); 9362} 9363 9364int 9365ctl_verify(struct ctl_scsiio *ctsio) 9366{ 9367 struct ctl_lun *lun; 9368 struct ctl_lba_len_flags *lbalen; 9369 uint64_t lba; 9370 uint32_t num_blocks; 9371 int bytchk, flags; 9372 int retval; 9373 9374 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9375 9376 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9377 9378 bytchk = 0; 9379 flags = CTL_LLF_FUA; 9380 retval = CTL_RETVAL_COMPLETE; 9381 9382 switch (ctsio->cdb[0]) { 9383 case VERIFY_10: { 9384 struct scsi_verify_10 *cdb; 9385 9386 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9387 if (cdb->byte2 & SVFY_BYTCHK) 9388 bytchk = 1; 9389 if (cdb->byte2 & SVFY_DPO) 9390 flags |= CTL_LLF_DPO; 9391 lba = scsi_4btoul(cdb->addr); 9392 num_blocks = scsi_2btoul(cdb->length); 9393 break; 9394 } 9395 case VERIFY_12: { 9396 struct scsi_verify_12 *cdb; 9397 9398 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9399 if (cdb->byte2 & SVFY_BYTCHK) 9400 bytchk = 1; 9401 if (cdb->byte2 & SVFY_DPO) 9402 flags |= CTL_LLF_DPO; 9403 lba = scsi_4btoul(cdb->addr); 9404 num_blocks = scsi_4btoul(cdb->length); 9405 break; 9406 } 9407 case VERIFY_16: { 9408 struct scsi_rw_16 *cdb; 9409 9410 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9411 if (cdb->byte2 & SVFY_BYTCHK) 9412 bytchk = 1; 9413 if (cdb->byte2 & SVFY_DPO) 9414 flags |= CTL_LLF_DPO; 9415 lba = scsi_8btou64(cdb->addr); 9416 num_blocks = scsi_4btoul(cdb->length); 9417 break; 9418 } 9419 default: 9420 /* 9421 * We got a command we don't support. This shouldn't 9422 * happen, commands should be filtered out above us. 9423 */ 9424 ctl_set_invalid_opcode(ctsio); 9425 ctl_done((union ctl_io *)ctsio); 9426 return (CTL_RETVAL_COMPLETE); 9427 } 9428 9429 /* 9430 * The first check is to make sure we're in bounds, the second 9431 * check is to catch wrap-around problems. If the lba + num blocks 9432 * is less than the lba, then we've wrapped around and the block 9433 * range is invalid anyway. 9434 */ 9435 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9436 || ((lba + num_blocks) < lba)) { 9437 ctl_set_lba_out_of_range(ctsio); 9438 ctl_done((union ctl_io *)ctsio); 9439 return (CTL_RETVAL_COMPLETE); 9440 } 9441 9442 /* 9443 * According to SBC-3, a transfer length of 0 is not an error. 9444 */ 9445 if (num_blocks == 0) { 9446 ctl_set_success(ctsio); 9447 ctl_done((union ctl_io *)ctsio); 9448 return (CTL_RETVAL_COMPLETE); 9449 } 9450 9451 lbalen = (struct ctl_lba_len_flags *) 9452 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9453 lbalen->lba = lba; 9454 lbalen->len = num_blocks; 9455 if (bytchk) { 9456 lbalen->flags = CTL_LLF_COMPARE | flags; 9457 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9458 } else { 9459 lbalen->flags = CTL_LLF_VERIFY | flags; 9460 ctsio->kern_total_len = 0; 9461 } 9462 ctsio->kern_rel_offset = 0; 9463 9464 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9465 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9466 return (retval); 9467} 9468 9469int 9470ctl_report_luns(struct ctl_scsiio *ctsio) 9471{ 9472 struct scsi_report_luns *cdb; 9473 struct scsi_report_luns_data *lun_data; 9474 struct ctl_lun *lun, *request_lun; 9475 int num_luns, retval; 9476 uint32_t alloc_len, lun_datalen; 9477 int num_filled, well_known; 9478 uint32_t initidx, targ_lun_id, lun_id; 9479 9480 retval = CTL_RETVAL_COMPLETE; 9481 well_known = 0; 9482 9483 cdb = (struct scsi_report_luns *)ctsio->cdb; 9484 9485 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9486 9487 mtx_lock(&control_softc->ctl_lock); 9488 num_luns = control_softc->num_luns; 9489 mtx_unlock(&control_softc->ctl_lock); 9490 9491 switch (cdb->select_report) { 9492 case RPL_REPORT_DEFAULT: 9493 case RPL_REPORT_ALL: 9494 break; 9495 case RPL_REPORT_WELLKNOWN: 9496 well_known = 1; 9497 num_luns = 0; 9498 break; 9499 default: 9500 ctl_set_invalid_field(ctsio, 9501 /*sks_valid*/ 1, 9502 /*command*/ 1, 9503 /*field*/ 2, 9504 /*bit_valid*/ 0, 9505 /*bit*/ 0); 9506 ctl_done((union ctl_io *)ctsio); 9507 return (retval); 9508 break; /* NOTREACHED */ 9509 } 9510 9511 alloc_len = scsi_4btoul(cdb->length); 9512 /* 9513 * The initiator has to allocate at least 16 bytes for this request, 9514 * so he can at least get the header and the first LUN. Otherwise 9515 * we reject the request (per SPC-3 rev 14, section 6.21). 9516 */ 9517 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9518 sizeof(struct scsi_report_luns_lundata))) { 9519 ctl_set_invalid_field(ctsio, 9520 /*sks_valid*/ 1, 9521 /*command*/ 1, 9522 /*field*/ 6, 9523 /*bit_valid*/ 0, 9524 /*bit*/ 0); 9525 ctl_done((union ctl_io *)ctsio); 9526 return (retval); 9527 } 9528 9529 request_lun = (struct ctl_lun *) 9530 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9531 9532 lun_datalen = sizeof(*lun_data) + 9533 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9534 9535 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9536 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9537 ctsio->kern_sg_entries = 0; 9538 9539 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9540 9541 mtx_lock(&control_softc->ctl_lock); 9542 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9543 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9544 if (lun_id >= CTL_MAX_LUNS) 9545 continue; 9546 lun = control_softc->ctl_luns[lun_id]; 9547 if (lun == NULL) 9548 continue; 9549 9550 if (targ_lun_id <= 0xff) { 9551 /* 9552 * Peripheral addressing method, bus number 0. 9553 */ 9554 lun_data->luns[num_filled].lundata[0] = 9555 RPL_LUNDATA_ATYP_PERIPH; 9556 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9557 num_filled++; 9558 } else if (targ_lun_id <= 0x3fff) { 9559 /* 9560 * Flat addressing method. 9561 */ 9562 lun_data->luns[num_filled].lundata[0] = 9563 RPL_LUNDATA_ATYP_FLAT | 9564 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9565#ifdef OLDCTLHEADERS 9566 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9567 (targ_lun_id & SRLD_BUS_LUN_MASK); 9568#endif 9569 lun_data->luns[num_filled].lundata[1] = 9570#ifdef OLDCTLHEADERS 9571 targ_lun_id >> SRLD_BUS_LUN_BITS; 9572#endif 9573 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9574 num_filled++; 9575 } else { 9576 printf("ctl_report_luns: bogus LUN number %jd, " 9577 "skipping\n", (intmax_t)targ_lun_id); 9578 } 9579 /* 9580 * According to SPC-3, rev 14 section 6.21: 9581 * 9582 * "The execution of a REPORT LUNS command to any valid and 9583 * installed logical unit shall clear the REPORTED LUNS DATA 9584 * HAS CHANGED unit attention condition for all logical 9585 * units of that target with respect to the requesting 9586 * initiator. A valid and installed logical unit is one 9587 * having a PERIPHERAL QUALIFIER of 000b in the standard 9588 * INQUIRY data (see 6.4.2)." 9589 * 9590 * If request_lun is NULL, the LUN this report luns command 9591 * was issued to is either disabled or doesn't exist. In that 9592 * case, we shouldn't clear any pending lun change unit 9593 * attention. 9594 */ 9595 if (request_lun != NULL) { 9596 mtx_lock(&lun->lun_lock); 9597 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9598 mtx_unlock(&lun->lun_lock); 9599 } 9600 } 9601 mtx_unlock(&control_softc->ctl_lock); 9602 9603 /* 9604 * It's quite possible that we've returned fewer LUNs than we allocated 9605 * space for. Trim it. 9606 */ 9607 lun_datalen = sizeof(*lun_data) + 9608 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9609 9610 if (lun_datalen < alloc_len) { 9611 ctsio->residual = alloc_len - lun_datalen; 9612 ctsio->kern_data_len = lun_datalen; 9613 ctsio->kern_total_len = lun_datalen; 9614 } else { 9615 ctsio->residual = 0; 9616 ctsio->kern_data_len = alloc_len; 9617 ctsio->kern_total_len = alloc_len; 9618 } 9619 ctsio->kern_data_resid = 0; 9620 ctsio->kern_rel_offset = 0; 9621 ctsio->kern_sg_entries = 0; 9622 9623 /* 9624 * We set this to the actual data length, regardless of how much 9625 * space we actually have to return results. If the user looks at 9626 * this value, he'll know whether or not he allocated enough space 9627 * and reissue the command if necessary. We don't support well 9628 * known logical units, so if the user asks for that, return none. 9629 */ 9630 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9631 9632 /* 9633 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9634 * this request. 9635 */ 9636 ctsio->scsi_status = SCSI_STATUS_OK; 9637 9638 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9639 ctsio->be_move_done = ctl_config_move_done; 9640 ctl_datamove((union ctl_io *)ctsio); 9641 9642 return (retval); 9643} 9644 9645int 9646ctl_request_sense(struct ctl_scsiio *ctsio) 9647{ 9648 struct scsi_request_sense *cdb; 9649 struct scsi_sense_data *sense_ptr; 9650 struct ctl_lun *lun; 9651 uint32_t initidx; 9652 int have_error; 9653 scsi_sense_data_type sense_format; 9654 9655 cdb = (struct scsi_request_sense *)ctsio->cdb; 9656 9657 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9658 9659 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9660 9661 /* 9662 * Determine which sense format the user wants. 9663 */ 9664 if (cdb->byte2 & SRS_DESC) 9665 sense_format = SSD_TYPE_DESC; 9666 else 9667 sense_format = SSD_TYPE_FIXED; 9668 9669 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9670 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9671 ctsio->kern_sg_entries = 0; 9672 9673 /* 9674 * struct scsi_sense_data, which is currently set to 256 bytes, is 9675 * larger than the largest allowed value for the length field in the 9676 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9677 */ 9678 ctsio->residual = 0; 9679 ctsio->kern_data_len = cdb->length; 9680 ctsio->kern_total_len = cdb->length; 9681 9682 ctsio->kern_data_resid = 0; 9683 ctsio->kern_rel_offset = 0; 9684 ctsio->kern_sg_entries = 0; 9685 9686 /* 9687 * If we don't have a LUN, we don't have any pending sense. 9688 */ 9689 if (lun == NULL) 9690 goto no_sense; 9691 9692 have_error = 0; 9693 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9694 /* 9695 * Check for pending sense, and then for pending unit attentions. 9696 * Pending sense gets returned first, then pending unit attentions. 9697 */ 9698 mtx_lock(&lun->lun_lock); 9699#ifdef CTL_WITH_CA 9700 if (ctl_is_set(lun->have_ca, initidx)) { 9701 scsi_sense_data_type stored_format; 9702 9703 /* 9704 * Check to see which sense format was used for the stored 9705 * sense data. 9706 */ 9707 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9708 9709 /* 9710 * If the user requested a different sense format than the 9711 * one we stored, then we need to convert it to the other 9712 * format. If we're going from descriptor to fixed format 9713 * sense data, we may lose things in translation, depending 9714 * on what options were used. 9715 * 9716 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9717 * for some reason we'll just copy it out as-is. 9718 */ 9719 if ((stored_format == SSD_TYPE_FIXED) 9720 && (sense_format == SSD_TYPE_DESC)) 9721 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9722 &lun->pending_sense[initidx], 9723 (struct scsi_sense_data_desc *)sense_ptr); 9724 else if ((stored_format == SSD_TYPE_DESC) 9725 && (sense_format == SSD_TYPE_FIXED)) 9726 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9727 &lun->pending_sense[initidx], 9728 (struct scsi_sense_data_fixed *)sense_ptr); 9729 else 9730 memcpy(sense_ptr, &lun->pending_sense[initidx], 9731 ctl_min(sizeof(*sense_ptr), 9732 sizeof(lun->pending_sense[initidx]))); 9733 9734 ctl_clear_mask(lun->have_ca, initidx); 9735 have_error = 1; 9736 } else 9737#endif 9738 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9739 ctl_ua_type ua_type; 9740 9741 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9742 sense_ptr, sense_format); 9743 if (ua_type != CTL_UA_NONE) 9744 have_error = 1; 9745 } 9746 mtx_unlock(&lun->lun_lock); 9747 9748 /* 9749 * We already have a pending error, return it. 9750 */ 9751 if (have_error != 0) { 9752 /* 9753 * We report the SCSI status as OK, since the status of the 9754 * request sense command itself is OK. 9755 */ 9756 ctsio->scsi_status = SCSI_STATUS_OK; 9757 9758 /* 9759 * We report 0 for the sense length, because we aren't doing 9760 * autosense in this case. We're reporting sense as 9761 * parameter data. 9762 */ 9763 ctsio->sense_len = 0; 9764 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9765 ctsio->be_move_done = ctl_config_move_done; 9766 ctl_datamove((union ctl_io *)ctsio); 9767 9768 return (CTL_RETVAL_COMPLETE); 9769 } 9770 9771no_sense: 9772 9773 /* 9774 * No sense information to report, so we report that everything is 9775 * okay. 9776 */ 9777 ctl_set_sense_data(sense_ptr, 9778 lun, 9779 sense_format, 9780 /*current_error*/ 1, 9781 /*sense_key*/ SSD_KEY_NO_SENSE, 9782 /*asc*/ 0x00, 9783 /*ascq*/ 0x00, 9784 SSD_ELEM_NONE); 9785 9786 ctsio->scsi_status = SCSI_STATUS_OK; 9787 9788 /* 9789 * We report 0 for the sense length, because we aren't doing 9790 * autosense in this case. We're reporting sense as parameter data. 9791 */ 9792 ctsio->sense_len = 0; 9793 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9794 ctsio->be_move_done = ctl_config_move_done; 9795 ctl_datamove((union ctl_io *)ctsio); 9796 9797 return (CTL_RETVAL_COMPLETE); 9798} 9799 9800int 9801ctl_tur(struct ctl_scsiio *ctsio) 9802{ 9803 struct ctl_lun *lun; 9804 9805 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9806 9807 CTL_DEBUG_PRINT(("ctl_tur\n")); 9808 9809 if (lun == NULL) 9810 return (EINVAL); 9811 9812 ctsio->scsi_status = SCSI_STATUS_OK; 9813 ctsio->io_hdr.status = CTL_SUCCESS; 9814 9815 ctl_done((union ctl_io *)ctsio); 9816 9817 return (CTL_RETVAL_COMPLETE); 9818} 9819 9820#ifdef notyet 9821static int 9822ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9823{ 9824 9825} 9826#endif 9827 9828static int 9829ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9830{ 9831 struct scsi_vpd_supported_pages *pages; 9832 int sup_page_size; 9833 struct ctl_lun *lun; 9834 9835 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9836 9837 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9838 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9839 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9840 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9841 ctsio->kern_sg_entries = 0; 9842 9843 if (sup_page_size < alloc_len) { 9844 ctsio->residual = alloc_len - sup_page_size; 9845 ctsio->kern_data_len = sup_page_size; 9846 ctsio->kern_total_len = sup_page_size; 9847 } else { 9848 ctsio->residual = 0; 9849 ctsio->kern_data_len = alloc_len; 9850 ctsio->kern_total_len = alloc_len; 9851 } 9852 ctsio->kern_data_resid = 0; 9853 ctsio->kern_rel_offset = 0; 9854 ctsio->kern_sg_entries = 0; 9855 9856 /* 9857 * The control device is always connected. The disk device, on the 9858 * other hand, may not be online all the time. Need to change this 9859 * to figure out whether the disk device is actually online or not. 9860 */ 9861 if (lun != NULL) 9862 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9863 lun->be_lun->lun_type; 9864 else 9865 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9866 9867 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9868 /* Supported VPD pages */ 9869 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9870 /* Serial Number */ 9871 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9872 /* Device Identification */ 9873 pages->page_list[2] = SVPD_DEVICE_ID; 9874 /* Extended INQUIRY Data */ 9875 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9876 /* Mode Page Policy */ 9877 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9878 /* SCSI Ports */ 9879 pages->page_list[5] = SVPD_SCSI_PORTS; 9880 /* Third-party Copy */ 9881 pages->page_list[6] = SVPD_SCSI_TPC; 9882 /* Block limits */ 9883 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9884 /* Block Device Characteristics */ 9885 pages->page_list[8] = SVPD_BDC; 9886 /* Logical Block Provisioning */ 9887 pages->page_list[9] = SVPD_LBP; 9888 9889 ctsio->scsi_status = SCSI_STATUS_OK; 9890 9891 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9892 ctsio->be_move_done = ctl_config_move_done; 9893 ctl_datamove((union ctl_io *)ctsio); 9894 9895 return (CTL_RETVAL_COMPLETE); 9896} 9897 9898static int 9899ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9900{ 9901 struct scsi_vpd_unit_serial_number *sn_ptr; 9902 struct ctl_lun *lun; 9903 9904 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9905 9906 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9907 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9908 ctsio->kern_sg_entries = 0; 9909 9910 if (sizeof(*sn_ptr) < alloc_len) { 9911 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9912 ctsio->kern_data_len = sizeof(*sn_ptr); 9913 ctsio->kern_total_len = sizeof(*sn_ptr); 9914 } else { 9915 ctsio->residual = 0; 9916 ctsio->kern_data_len = alloc_len; 9917 ctsio->kern_total_len = alloc_len; 9918 } 9919 ctsio->kern_data_resid = 0; 9920 ctsio->kern_rel_offset = 0; 9921 ctsio->kern_sg_entries = 0; 9922 9923 /* 9924 * The control device is always connected. The disk device, on the 9925 * other hand, may not be online all the time. Need to change this 9926 * to figure out whether the disk device is actually online or not. 9927 */ 9928 if (lun != NULL) 9929 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9930 lun->be_lun->lun_type; 9931 else 9932 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9933 9934 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9935 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9936 /* 9937 * If we don't have a LUN, we just leave the serial number as 9938 * all spaces. 9939 */ 9940 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9941 if (lun != NULL) { 9942 strncpy((char *)sn_ptr->serial_num, 9943 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9944 } 9945 ctsio->scsi_status = SCSI_STATUS_OK; 9946 9947 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9948 ctsio->be_move_done = ctl_config_move_done; 9949 ctl_datamove((union ctl_io *)ctsio); 9950 9951 return (CTL_RETVAL_COMPLETE); 9952} 9953 9954 9955static int 9956ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9957{ 9958 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9959 struct ctl_lun *lun; 9960 int data_len; 9961 9962 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9963 9964 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9965 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9966 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9967 ctsio->kern_sg_entries = 0; 9968 9969 if (data_len < alloc_len) { 9970 ctsio->residual = alloc_len - data_len; 9971 ctsio->kern_data_len = data_len; 9972 ctsio->kern_total_len = data_len; 9973 } else { 9974 ctsio->residual = 0; 9975 ctsio->kern_data_len = alloc_len; 9976 ctsio->kern_total_len = alloc_len; 9977 } 9978 ctsio->kern_data_resid = 0; 9979 ctsio->kern_rel_offset = 0; 9980 ctsio->kern_sg_entries = 0; 9981 9982 /* 9983 * The control device is always connected. The disk device, on the 9984 * other hand, may not be online all the time. 9985 */ 9986 if (lun != NULL) 9987 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9988 lun->be_lun->lun_type; 9989 else 9990 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9991 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9992 eid_ptr->page_length = data_len - 4; 9993 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9994 eid_ptr->flags3 = SVPD_EID_V_SUP; 9995 9996 ctsio->scsi_status = SCSI_STATUS_OK; 9997 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9998 ctsio->be_move_done = ctl_config_move_done; 9999 ctl_datamove((union ctl_io *)ctsio); 10000 10001 return (CTL_RETVAL_COMPLETE); 10002} 10003 10004static int 10005ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 10006{ 10007 struct scsi_vpd_mode_page_policy *mpp_ptr; 10008 struct ctl_lun *lun; 10009 int data_len; 10010 10011 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10012 10013 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 10014 sizeof(struct scsi_vpd_mode_page_policy_descr); 10015 10016 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10017 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 10018 ctsio->kern_sg_entries = 0; 10019 10020 if (data_len < alloc_len) { 10021 ctsio->residual = alloc_len - data_len; 10022 ctsio->kern_data_len = data_len; 10023 ctsio->kern_total_len = data_len; 10024 } else { 10025 ctsio->residual = 0; 10026 ctsio->kern_data_len = alloc_len; 10027 ctsio->kern_total_len = alloc_len; 10028 } 10029 ctsio->kern_data_resid = 0; 10030 ctsio->kern_rel_offset = 0; 10031 ctsio->kern_sg_entries = 0; 10032 10033 /* 10034 * The control device is always connected. The disk device, on the 10035 * other hand, may not be online all the time. 10036 */ 10037 if (lun != NULL) 10038 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10039 lun->be_lun->lun_type; 10040 else 10041 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10042 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 10043 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 10044 mpp_ptr->descr[0].page_code = 0x3f; 10045 mpp_ptr->descr[0].subpage_code = 0xff; 10046 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 10047 10048 ctsio->scsi_status = SCSI_STATUS_OK; 10049 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10050 ctsio->be_move_done = ctl_config_move_done; 10051 ctl_datamove((union ctl_io *)ctsio); 10052 10053 return (CTL_RETVAL_COMPLETE); 10054} 10055 10056static int 10057ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 10058{ 10059 struct scsi_vpd_device_id *devid_ptr; 10060 struct scsi_vpd_id_descriptor *desc; 10061 struct ctl_softc *ctl_softc; 10062 struct ctl_lun *lun; 10063 struct ctl_port *port; 10064 int data_len; 10065 uint8_t proto; 10066 10067 ctl_softc = control_softc; 10068 10069 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 10070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10071 10072 data_len = sizeof(struct scsi_vpd_device_id) + 10073 sizeof(struct scsi_vpd_id_descriptor) + 10074 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 10075 sizeof(struct scsi_vpd_id_descriptor) + 10076 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 10077 if (lun && lun->lun_devid) 10078 data_len += lun->lun_devid->len; 10079 if (port->port_devid) 10080 data_len += port->port_devid->len; 10081 if (port->target_devid) 10082 data_len += port->target_devid->len; 10083 10084 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10085 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10086 ctsio->kern_sg_entries = 0; 10087 10088 if (data_len < alloc_len) { 10089 ctsio->residual = alloc_len - data_len; 10090 ctsio->kern_data_len = data_len; 10091 ctsio->kern_total_len = data_len; 10092 } else { 10093 ctsio->residual = 0; 10094 ctsio->kern_data_len = alloc_len; 10095 ctsio->kern_total_len = alloc_len; 10096 } 10097 ctsio->kern_data_resid = 0; 10098 ctsio->kern_rel_offset = 0; 10099 ctsio->kern_sg_entries = 0; 10100 10101 /* 10102 * The control device is always connected. The disk device, on the 10103 * other hand, may not be online all the time. 10104 */ 10105 if (lun != NULL) 10106 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10107 lun->be_lun->lun_type; 10108 else 10109 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10110 devid_ptr->page_code = SVPD_DEVICE_ID; 10111 scsi_ulto2b(data_len - 4, devid_ptr->length); 10112 10113 if (port->port_type == CTL_PORT_FC) 10114 proto = SCSI_PROTO_FC << 4; 10115 else if (port->port_type == CTL_PORT_ISCSI) 10116 proto = SCSI_PROTO_ISCSI << 4; 10117 else 10118 proto = SCSI_PROTO_SPI << 4; 10119 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10120 10121 /* 10122 * We're using a LUN association here. i.e., this device ID is a 10123 * per-LUN identifier. 10124 */ 10125 if (lun && lun->lun_devid) { 10126 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10127 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10128 lun->lun_devid->len); 10129 } 10130 10131 /* 10132 * This is for the WWPN which is a port association. 10133 */ 10134 if (port->port_devid) { 10135 memcpy(desc, port->port_devid->data, port->port_devid->len); 10136 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10137 port->port_devid->len); 10138 } 10139 10140 /* 10141 * This is for the Relative Target Port(type 4h) identifier 10142 */ 10143 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10144 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10145 SVPD_ID_TYPE_RELTARG; 10146 desc->length = 4; 10147 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10148 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10149 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10150 10151 /* 10152 * This is for the Target Port Group(type 5h) identifier 10153 */ 10154 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10155 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10156 SVPD_ID_TYPE_TPORTGRP; 10157 desc->length = 4; 10158 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10159 &desc->identifier[2]); 10160 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10161 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10162 10163 /* 10164 * This is for the Target identifier 10165 */ 10166 if (port->target_devid) { 10167 memcpy(desc, port->target_devid->data, port->target_devid->len); 10168 } 10169 10170 ctsio->scsi_status = SCSI_STATUS_OK; 10171 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10172 ctsio->be_move_done = ctl_config_move_done; 10173 ctl_datamove((union ctl_io *)ctsio); 10174 10175 return (CTL_RETVAL_COMPLETE); 10176} 10177 10178static int 10179ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10180{ 10181 struct ctl_softc *softc = control_softc; 10182 struct scsi_vpd_scsi_ports *sp; 10183 struct scsi_vpd_port_designation *pd; 10184 struct scsi_vpd_port_designation_cont *pdc; 10185 struct ctl_lun *lun; 10186 struct ctl_port *port; 10187 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10188 int num_target_port_groups, single; 10189 10190 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10191 10192 single = ctl_is_single; 10193 if (single) 10194 num_target_port_groups = 1; 10195 else 10196 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10197 num_target_ports = 0; 10198 iid_len = 0; 10199 id_len = 0; 10200 mtx_lock(&softc->ctl_lock); 10201 STAILQ_FOREACH(port, &softc->port_list, links) { 10202 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10203 continue; 10204 if (lun != NULL && 10205 ctl_map_lun_back(port->targ_port, lun->lun) >= 10206 CTL_MAX_LUNS) 10207 continue; 10208 num_target_ports++; 10209 if (port->init_devid) 10210 iid_len += port->init_devid->len; 10211 if (port->port_devid) 10212 id_len += port->port_devid->len; 10213 } 10214 mtx_unlock(&softc->ctl_lock); 10215 10216 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10217 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10218 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10219 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10220 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10221 ctsio->kern_sg_entries = 0; 10222 10223 if (data_len < alloc_len) { 10224 ctsio->residual = alloc_len - data_len; 10225 ctsio->kern_data_len = data_len; 10226 ctsio->kern_total_len = data_len; 10227 } else { 10228 ctsio->residual = 0; 10229 ctsio->kern_data_len = alloc_len; 10230 ctsio->kern_total_len = alloc_len; 10231 } 10232 ctsio->kern_data_resid = 0; 10233 ctsio->kern_rel_offset = 0; 10234 ctsio->kern_sg_entries = 0; 10235 10236 /* 10237 * The control device is always connected. The disk device, on the 10238 * other hand, may not be online all the time. Need to change this 10239 * to figure out whether the disk device is actually online or not. 10240 */ 10241 if (lun != NULL) 10242 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10243 lun->be_lun->lun_type; 10244 else 10245 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10246 10247 sp->page_code = SVPD_SCSI_PORTS; 10248 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10249 sp->page_length); 10250 pd = &sp->design[0]; 10251 10252 mtx_lock(&softc->ctl_lock); 10253 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10254 pg = 0; 10255 else 10256 pg = 1; 10257 for (g = 0; g < num_target_port_groups; g++) { 10258 STAILQ_FOREACH(port, &softc->port_list, links) { 10259 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10260 continue; 10261 if (lun != NULL && 10262 ctl_map_lun_back(port->targ_port, lun->lun) >= 10263 CTL_MAX_LUNS) 10264 continue; 10265 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10266 scsi_ulto2b(p, pd->relative_port_id); 10267 if (port->init_devid && g == pg) { 10268 iid_len = port->init_devid->len; 10269 memcpy(pd->initiator_transportid, 10270 port->init_devid->data, port->init_devid->len); 10271 } else 10272 iid_len = 0; 10273 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10274 pdc = (struct scsi_vpd_port_designation_cont *) 10275 (&pd->initiator_transportid[iid_len]); 10276 if (port->port_devid && g == pg) { 10277 id_len = port->port_devid->len; 10278 memcpy(pdc->target_port_descriptors, 10279 port->port_devid->data, port->port_devid->len); 10280 } else 10281 id_len = 0; 10282 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10283 pd = (struct scsi_vpd_port_designation *) 10284 ((uint8_t *)pdc->target_port_descriptors + id_len); 10285 } 10286 } 10287 mtx_unlock(&softc->ctl_lock); 10288 10289 ctsio->scsi_status = SCSI_STATUS_OK; 10290 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10291 ctsio->be_move_done = ctl_config_move_done; 10292 ctl_datamove((union ctl_io *)ctsio); 10293 10294 return (CTL_RETVAL_COMPLETE); 10295} 10296 10297static int 10298ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10299{ 10300 struct scsi_vpd_block_limits *bl_ptr; 10301 struct ctl_lun *lun; 10302 int bs; 10303 10304 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10305 10306 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10307 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10308 ctsio->kern_sg_entries = 0; 10309 10310 if (sizeof(*bl_ptr) < alloc_len) { 10311 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10312 ctsio->kern_data_len = sizeof(*bl_ptr); 10313 ctsio->kern_total_len = sizeof(*bl_ptr); 10314 } else { 10315 ctsio->residual = 0; 10316 ctsio->kern_data_len = alloc_len; 10317 ctsio->kern_total_len = alloc_len; 10318 } 10319 ctsio->kern_data_resid = 0; 10320 ctsio->kern_rel_offset = 0; 10321 ctsio->kern_sg_entries = 0; 10322 10323 /* 10324 * The control device is always connected. The disk device, on the 10325 * other hand, may not be online all the time. Need to change this 10326 * to figure out whether the disk device is actually online or not. 10327 */ 10328 if (lun != NULL) 10329 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10330 lun->be_lun->lun_type; 10331 else 10332 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10333 10334 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10335 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10336 bl_ptr->max_cmp_write_len = 0xff; 10337 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10338 if (lun != NULL) { 10339 bs = lun->be_lun->blocksize; 10340 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10341 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10342 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10343 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10344 if (lun->be_lun->pblockexp != 0) { 10345 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10346 bl_ptr->opt_unmap_grain); 10347 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10348 bl_ptr->unmap_grain_align); 10349 } 10350 } 10351 scsi_ulto4b(lun->be_lun->atomicblock, 10352 bl_ptr->max_atomic_transfer_length); 10353 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10354 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10355 } 10356 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10357 10358 ctsio->scsi_status = SCSI_STATUS_OK; 10359 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10360 ctsio->be_move_done = ctl_config_move_done; 10361 ctl_datamove((union ctl_io *)ctsio); 10362 10363 return (CTL_RETVAL_COMPLETE); 10364} 10365 10366static int 10367ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10368{ 10369 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10370 struct ctl_lun *lun; 10371 10372 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10373 10374 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10375 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10376 ctsio->kern_sg_entries = 0; 10377 10378 if (sizeof(*bdc_ptr) < alloc_len) { 10379 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10380 ctsio->kern_data_len = sizeof(*bdc_ptr); 10381 ctsio->kern_total_len = sizeof(*bdc_ptr); 10382 } else { 10383 ctsio->residual = 0; 10384 ctsio->kern_data_len = alloc_len; 10385 ctsio->kern_total_len = alloc_len; 10386 } 10387 ctsio->kern_data_resid = 0; 10388 ctsio->kern_rel_offset = 0; 10389 ctsio->kern_sg_entries = 0; 10390 10391 /* 10392 * The control device is always connected. The disk device, on the 10393 * other hand, may not be online all the time. Need to change this 10394 * to figure out whether the disk device is actually online or not. 10395 */ 10396 if (lun != NULL) 10397 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10398 lun->be_lun->lun_type; 10399 else 10400 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10401 bdc_ptr->page_code = SVPD_BDC; 10402 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10403 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10404 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10405 10406 ctsio->scsi_status = SCSI_STATUS_OK; 10407 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10408 ctsio->be_move_done = ctl_config_move_done; 10409 ctl_datamove((union ctl_io *)ctsio); 10410 10411 return (CTL_RETVAL_COMPLETE); 10412} 10413 10414static int 10415ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10416{ 10417 struct scsi_vpd_logical_block_prov *lbp_ptr; 10418 struct ctl_lun *lun; 10419 10420 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10421 10422 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10423 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10424 ctsio->kern_sg_entries = 0; 10425 10426 if (sizeof(*lbp_ptr) < alloc_len) { 10427 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10428 ctsio->kern_data_len = sizeof(*lbp_ptr); 10429 ctsio->kern_total_len = sizeof(*lbp_ptr); 10430 } else { 10431 ctsio->residual = 0; 10432 ctsio->kern_data_len = alloc_len; 10433 ctsio->kern_total_len = alloc_len; 10434 } 10435 ctsio->kern_data_resid = 0; 10436 ctsio->kern_rel_offset = 0; 10437 ctsio->kern_sg_entries = 0; 10438 10439 /* 10440 * The control device is always connected. The disk device, on the 10441 * other hand, may not be online all the time. Need to change this 10442 * to figure out whether the disk device is actually online or not. 10443 */ 10444 if (lun != NULL) 10445 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10446 lun->be_lun->lun_type; 10447 else 10448 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10449 10450 lbp_ptr->page_code = SVPD_LBP; 10451 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10452 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10453 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10454 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10455 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10456 } 10457 10458 ctsio->scsi_status = SCSI_STATUS_OK; 10459 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10460 ctsio->be_move_done = ctl_config_move_done; 10461 ctl_datamove((union ctl_io *)ctsio); 10462 10463 return (CTL_RETVAL_COMPLETE); 10464} 10465 10466static int 10467ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10468{ 10469 struct scsi_inquiry *cdb; 10470 struct ctl_lun *lun; 10471 int alloc_len, retval; 10472 10473 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10474 cdb = (struct scsi_inquiry *)ctsio->cdb; 10475 10476 retval = CTL_RETVAL_COMPLETE; 10477 10478 alloc_len = scsi_2btoul(cdb->length); 10479 10480 switch (cdb->page_code) { 10481 case SVPD_SUPPORTED_PAGES: 10482 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10483 break; 10484 case SVPD_UNIT_SERIAL_NUMBER: 10485 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10486 break; 10487 case SVPD_DEVICE_ID: 10488 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10489 break; 10490 case SVPD_EXTENDED_INQUIRY_DATA: 10491 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10492 break; 10493 case SVPD_MODE_PAGE_POLICY: 10494 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10495 break; 10496 case SVPD_SCSI_PORTS: 10497 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10498 break; 10499 case SVPD_SCSI_TPC: 10500 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10501 break; 10502 case SVPD_BLOCK_LIMITS: 10503 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10504 break; 10505 case SVPD_BDC: 10506 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10507 break; 10508 case SVPD_LBP: 10509 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10510 break; 10511 default: 10512 ctl_set_invalid_field(ctsio, 10513 /*sks_valid*/ 1, 10514 /*command*/ 1, 10515 /*field*/ 2, 10516 /*bit_valid*/ 0, 10517 /*bit*/ 0); 10518 ctl_done((union ctl_io *)ctsio); 10519 retval = CTL_RETVAL_COMPLETE; 10520 break; 10521 } 10522 10523 return (retval); 10524} 10525 10526static int 10527ctl_inquiry_std(struct ctl_scsiio *ctsio) 10528{ 10529 struct scsi_inquiry_data *inq_ptr; 10530 struct scsi_inquiry *cdb; 10531 struct ctl_softc *ctl_softc; 10532 struct ctl_lun *lun; 10533 char *val; 10534 uint32_t alloc_len, data_len; 10535 ctl_port_type port_type; 10536 10537 ctl_softc = control_softc; 10538 10539 /* 10540 * Figure out whether we're talking to a Fibre Channel port or not. 10541 * We treat the ioctl front end, and any SCSI adapters, as packetized 10542 * SCSI front ends. 10543 */ 10544 port_type = ctl_softc->ctl_ports[ 10545 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10546 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10547 port_type = CTL_PORT_SCSI; 10548 10549 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10550 cdb = (struct scsi_inquiry *)ctsio->cdb; 10551 alloc_len = scsi_2btoul(cdb->length); 10552 10553 /* 10554 * We malloc the full inquiry data size here and fill it 10555 * in. If the user only asks for less, we'll give him 10556 * that much. 10557 */ 10558 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10559 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10560 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10561 ctsio->kern_sg_entries = 0; 10562 ctsio->kern_data_resid = 0; 10563 ctsio->kern_rel_offset = 0; 10564 10565 if (data_len < alloc_len) { 10566 ctsio->residual = alloc_len - data_len; 10567 ctsio->kern_data_len = data_len; 10568 ctsio->kern_total_len = data_len; 10569 } else { 10570 ctsio->residual = 0; 10571 ctsio->kern_data_len = alloc_len; 10572 ctsio->kern_total_len = alloc_len; 10573 } 10574 10575 /* 10576 * If we have a LUN configured, report it as connected. Otherwise, 10577 * report that it is offline or no device is supported, depending 10578 * on the value of inquiry_pq_no_lun. 10579 * 10580 * According to the spec (SPC-4 r34), the peripheral qualifier 10581 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10582 * 10583 * "A peripheral device having the specified peripheral device type 10584 * is not connected to this logical unit. However, the device 10585 * server is capable of supporting the specified peripheral device 10586 * type on this logical unit." 10587 * 10588 * According to the same spec, the peripheral qualifier 10589 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10590 * 10591 * "The device server is not capable of supporting a peripheral 10592 * device on this logical unit. For this peripheral qualifier the 10593 * peripheral device type shall be set to 1Fh. All other peripheral 10594 * device type values are reserved for this peripheral qualifier." 10595 * 10596 * Given the text, it would seem that we probably want to report that 10597 * the LUN is offline here. There is no LUN connected, but we can 10598 * support a LUN at the given LUN number. 10599 * 10600 * In the real world, though, it sounds like things are a little 10601 * different: 10602 * 10603 * - Linux, when presented with a LUN with the offline peripheral 10604 * qualifier, will create an sg driver instance for it. So when 10605 * you attach it to CTL, you wind up with a ton of sg driver 10606 * instances. (One for every LUN that Linux bothered to probe.) 10607 * Linux does this despite the fact that it issues a REPORT LUNs 10608 * to LUN 0 to get the inventory of supported LUNs. 10609 * 10610 * - There is other anecdotal evidence (from Emulex folks) about 10611 * arrays that use the offline peripheral qualifier for LUNs that 10612 * are on the "passive" path in an active/passive array. 10613 * 10614 * So the solution is provide a hopefully reasonable default 10615 * (return bad/no LUN) and allow the user to change the behavior 10616 * with a tunable/sysctl variable. 10617 */ 10618 if (lun != NULL) 10619 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10620 lun->be_lun->lun_type; 10621 else if (ctl_softc->inquiry_pq_no_lun == 0) 10622 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10623 else 10624 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10625 10626 /* RMB in byte 2 is 0 */ 10627 inq_ptr->version = SCSI_REV_SPC4; 10628 10629 /* 10630 * According to SAM-3, even if a device only supports a single 10631 * level of LUN addressing, it should still set the HISUP bit: 10632 * 10633 * 4.9.1 Logical unit numbers overview 10634 * 10635 * All logical unit number formats described in this standard are 10636 * hierarchical in structure even when only a single level in that 10637 * hierarchy is used. The HISUP bit shall be set to one in the 10638 * standard INQUIRY data (see SPC-2) when any logical unit number 10639 * format described in this standard is used. Non-hierarchical 10640 * formats are outside the scope of this standard. 10641 * 10642 * Therefore we set the HiSup bit here. 10643 * 10644 * The reponse format is 2, per SPC-3. 10645 */ 10646 inq_ptr->response_format = SID_HiSup | 2; 10647 10648 inq_ptr->additional_length = data_len - 10649 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10650 CTL_DEBUG_PRINT(("additional_length = %d\n", 10651 inq_ptr->additional_length)); 10652 10653 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10654 /* 16 bit addressing */ 10655 if (port_type == CTL_PORT_SCSI) 10656 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10657 /* XXX set the SID_MultiP bit here if we're actually going to 10658 respond on multiple ports */ 10659 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10660 10661 /* 16 bit data bus, synchronous transfers */ 10662 if (port_type == CTL_PORT_SCSI) 10663 inq_ptr->flags = SID_WBus16 | SID_Sync; 10664 /* 10665 * XXX KDM do we want to support tagged queueing on the control 10666 * device at all? 10667 */ 10668 if ((lun == NULL) 10669 || (lun->be_lun->lun_type != T_PROCESSOR)) 10670 inq_ptr->flags |= SID_CmdQue; 10671 /* 10672 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10673 * We have 8 bytes for the vendor name, and 16 bytes for the device 10674 * name and 4 bytes for the revision. 10675 */ 10676 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10677 "vendor")) == NULL) { 10678 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10679 } else { 10680 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10681 strncpy(inq_ptr->vendor, val, 10682 min(sizeof(inq_ptr->vendor), strlen(val))); 10683 } 10684 if (lun == NULL) { 10685 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10686 sizeof(inq_ptr->product)); 10687 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10688 switch (lun->be_lun->lun_type) { 10689 case T_DIRECT: 10690 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10691 sizeof(inq_ptr->product)); 10692 break; 10693 case T_PROCESSOR: 10694 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10695 sizeof(inq_ptr->product)); 10696 break; 10697 default: 10698 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10699 sizeof(inq_ptr->product)); 10700 break; 10701 } 10702 } else { 10703 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10704 strncpy(inq_ptr->product, val, 10705 min(sizeof(inq_ptr->product), strlen(val))); 10706 } 10707 10708 /* 10709 * XXX make this a macro somewhere so it automatically gets 10710 * incremented when we make changes. 10711 */ 10712 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10713 "revision")) == NULL) { 10714 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10715 } else { 10716 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10717 strncpy(inq_ptr->revision, val, 10718 min(sizeof(inq_ptr->revision), strlen(val))); 10719 } 10720 10721 /* 10722 * For parallel SCSI, we support double transition and single 10723 * transition clocking. We also support QAS (Quick Arbitration 10724 * and Selection) and Information Unit transfers on both the 10725 * control and array devices. 10726 */ 10727 if (port_type == CTL_PORT_SCSI) 10728 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10729 SID_SPI_IUS; 10730 10731 /* SAM-5 (no version claimed) */ 10732 scsi_ulto2b(0x00A0, inq_ptr->version1); 10733 /* SPC-4 (no version claimed) */ 10734 scsi_ulto2b(0x0460, inq_ptr->version2); 10735 if (port_type == CTL_PORT_FC) { 10736 /* FCP-2 ANSI INCITS.350:2003 */ 10737 scsi_ulto2b(0x0917, inq_ptr->version3); 10738 } else if (port_type == CTL_PORT_SCSI) { 10739 /* SPI-4 ANSI INCITS.362:200x */ 10740 scsi_ulto2b(0x0B56, inq_ptr->version3); 10741 } else if (port_type == CTL_PORT_ISCSI) { 10742 /* iSCSI (no version claimed) */ 10743 scsi_ulto2b(0x0960, inq_ptr->version3); 10744 } else if (port_type == CTL_PORT_SAS) { 10745 /* SAS (no version claimed) */ 10746 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10747 } 10748 10749 if (lun == NULL) { 10750 /* SBC-4 (no version claimed) */ 10751 scsi_ulto2b(0x0600, inq_ptr->version4); 10752 } else { 10753 switch (lun->be_lun->lun_type) { 10754 case T_DIRECT: 10755 /* SBC-4 (no version claimed) */ 10756 scsi_ulto2b(0x0600, inq_ptr->version4); 10757 break; 10758 case T_PROCESSOR: 10759 default: 10760 break; 10761 } 10762 } 10763 10764 ctsio->scsi_status = SCSI_STATUS_OK; 10765 if (ctsio->kern_data_len > 0) { 10766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10767 ctsio->be_move_done = ctl_config_move_done; 10768 ctl_datamove((union ctl_io *)ctsio); 10769 } else { 10770 ctsio->io_hdr.status = CTL_SUCCESS; 10771 ctl_done((union ctl_io *)ctsio); 10772 } 10773 10774 return (CTL_RETVAL_COMPLETE); 10775} 10776 10777int 10778ctl_inquiry(struct ctl_scsiio *ctsio) 10779{ 10780 struct scsi_inquiry *cdb; 10781 int retval; 10782 10783 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10784 10785 cdb = (struct scsi_inquiry *)ctsio->cdb; 10786 if (cdb->byte2 & SI_EVPD) 10787 retval = ctl_inquiry_evpd(ctsio); 10788 else if (cdb->page_code == 0) 10789 retval = ctl_inquiry_std(ctsio); 10790 else { 10791 ctl_set_invalid_field(ctsio, 10792 /*sks_valid*/ 1, 10793 /*command*/ 1, 10794 /*field*/ 2, 10795 /*bit_valid*/ 0, 10796 /*bit*/ 0); 10797 ctl_done((union ctl_io *)ctsio); 10798 return (CTL_RETVAL_COMPLETE); 10799 } 10800 10801 return (retval); 10802} 10803 10804/* 10805 * For known CDB types, parse the LBA and length. 10806 */ 10807static int 10808ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10809{ 10810 if (io->io_hdr.io_type != CTL_IO_SCSI) 10811 return (1); 10812 10813 switch (io->scsiio.cdb[0]) { 10814 case COMPARE_AND_WRITE: { 10815 struct scsi_compare_and_write *cdb; 10816 10817 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10818 10819 *lba = scsi_8btou64(cdb->addr); 10820 *len = cdb->length; 10821 break; 10822 } 10823 case READ_6: 10824 case WRITE_6: { 10825 struct scsi_rw_6 *cdb; 10826 10827 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10828 10829 *lba = scsi_3btoul(cdb->addr); 10830 /* only 5 bits are valid in the most significant address byte */ 10831 *lba &= 0x1fffff; 10832 *len = cdb->length; 10833 break; 10834 } 10835 case READ_10: 10836 case WRITE_10: { 10837 struct scsi_rw_10 *cdb; 10838 10839 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10840 10841 *lba = scsi_4btoul(cdb->addr); 10842 *len = scsi_2btoul(cdb->length); 10843 break; 10844 } 10845 case WRITE_VERIFY_10: { 10846 struct scsi_write_verify_10 *cdb; 10847 10848 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10849 10850 *lba = scsi_4btoul(cdb->addr); 10851 *len = scsi_2btoul(cdb->length); 10852 break; 10853 } 10854 case READ_12: 10855 case WRITE_12: { 10856 struct scsi_rw_12 *cdb; 10857 10858 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10859 10860 *lba = scsi_4btoul(cdb->addr); 10861 *len = scsi_4btoul(cdb->length); 10862 break; 10863 } 10864 case WRITE_VERIFY_12: { 10865 struct scsi_write_verify_12 *cdb; 10866 10867 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10868 10869 *lba = scsi_4btoul(cdb->addr); 10870 *len = scsi_4btoul(cdb->length); 10871 break; 10872 } 10873 case READ_16: 10874 case WRITE_16: 10875 case WRITE_ATOMIC_16: { 10876 struct scsi_rw_16 *cdb; 10877 10878 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10879 10880 *lba = scsi_8btou64(cdb->addr); 10881 *len = scsi_4btoul(cdb->length); 10882 break; 10883 } 10884 case WRITE_VERIFY_16: { 10885 struct scsi_write_verify_16 *cdb; 10886 10887 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10888 10889 *lba = scsi_8btou64(cdb->addr); 10890 *len = scsi_4btoul(cdb->length); 10891 break; 10892 } 10893 case WRITE_SAME_10: { 10894 struct scsi_write_same_10 *cdb; 10895 10896 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10897 10898 *lba = scsi_4btoul(cdb->addr); 10899 *len = scsi_2btoul(cdb->length); 10900 break; 10901 } 10902 case WRITE_SAME_16: { 10903 struct scsi_write_same_16 *cdb; 10904 10905 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10906 10907 *lba = scsi_8btou64(cdb->addr); 10908 *len = scsi_4btoul(cdb->length); 10909 break; 10910 } 10911 case VERIFY_10: { 10912 struct scsi_verify_10 *cdb; 10913 10914 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10915 10916 *lba = scsi_4btoul(cdb->addr); 10917 *len = scsi_2btoul(cdb->length); 10918 break; 10919 } 10920 case VERIFY_12: { 10921 struct scsi_verify_12 *cdb; 10922 10923 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10924 10925 *lba = scsi_4btoul(cdb->addr); 10926 *len = scsi_4btoul(cdb->length); 10927 break; 10928 } 10929 case VERIFY_16: { 10930 struct scsi_verify_16 *cdb; 10931 10932 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10933 10934 *lba = scsi_8btou64(cdb->addr); 10935 *len = scsi_4btoul(cdb->length); 10936 break; 10937 } 10938 case UNMAP: { 10939 *lba = 0; 10940 *len = UINT64_MAX; 10941 break; 10942 } 10943 default: 10944 return (1); 10945 break; /* NOTREACHED */ 10946 } 10947 10948 return (0); 10949} 10950 10951static ctl_action 10952ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10953{ 10954 uint64_t endlba1, endlba2; 10955 10956 endlba1 = lba1 + len1 - 1; 10957 endlba2 = lba2 + len2 - 1; 10958 10959 if ((endlba1 < lba2) 10960 || (endlba2 < lba1)) 10961 return (CTL_ACTION_PASS); 10962 else 10963 return (CTL_ACTION_BLOCK); 10964} 10965 10966static int 10967ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10968{ 10969 struct ctl_ptr_len_flags *ptrlen; 10970 struct scsi_unmap_desc *buf, *end, *range; 10971 uint64_t lba; 10972 uint32_t len; 10973 10974 /* If not UNMAP -- go other way. */ 10975 if (io->io_hdr.io_type != CTL_IO_SCSI || 10976 io->scsiio.cdb[0] != UNMAP) 10977 return (CTL_ACTION_ERROR); 10978 10979 /* If UNMAP without data -- block and wait for data. */ 10980 ptrlen = (struct ctl_ptr_len_flags *) 10981 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10982 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10983 ptrlen->ptr == NULL) 10984 return (CTL_ACTION_BLOCK); 10985 10986 /* UNMAP with data -- check for collision. */ 10987 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10988 end = buf + ptrlen->len / sizeof(*buf); 10989 for (range = buf; range < end; range++) { 10990 lba = scsi_8btou64(range->lba); 10991 len = scsi_4btoul(range->length); 10992 if ((lba < lba2 + len2) && (lba + len > lba2)) 10993 return (CTL_ACTION_BLOCK); 10994 } 10995 return (CTL_ACTION_PASS); 10996} 10997 10998static ctl_action 10999ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 11000{ 11001 uint64_t lba1, lba2; 11002 uint64_t len1, len2; 11003 int retval; 11004 11005 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 11006 return (CTL_ACTION_ERROR); 11007 11008 retval = ctl_extent_check_unmap(io2, lba1, len1); 11009 if (retval != CTL_ACTION_ERROR) 11010 return (retval); 11011 11012 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 11013 return (CTL_ACTION_ERROR); 11014 11015 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 11016} 11017 11018static ctl_action 11019ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 11020 union ctl_io *ooa_io) 11021{ 11022 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 11023 ctl_serialize_action *serialize_row; 11024 11025 /* 11026 * The initiator attempted multiple untagged commands at the same 11027 * time. Can't do that. 11028 */ 11029 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11030 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11031 && ((pending_io->io_hdr.nexus.targ_port == 11032 ooa_io->io_hdr.nexus.targ_port) 11033 && (pending_io->io_hdr.nexus.initid.id == 11034 ooa_io->io_hdr.nexus.initid.id)) 11035 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11036 return (CTL_ACTION_OVERLAP); 11037 11038 /* 11039 * The initiator attempted to send multiple tagged commands with 11040 * the same ID. (It's fine if different initiators have the same 11041 * tag ID.) 11042 * 11043 * Even if all of those conditions are true, we don't kill the I/O 11044 * if the command ahead of us has been aborted. We won't end up 11045 * sending it to the FETD, and it's perfectly legal to resend a 11046 * command with the same tag number as long as the previous 11047 * instance of this tag number has been aborted somehow. 11048 */ 11049 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11050 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11051 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11052 && ((pending_io->io_hdr.nexus.targ_port == 11053 ooa_io->io_hdr.nexus.targ_port) 11054 && (pending_io->io_hdr.nexus.initid.id == 11055 ooa_io->io_hdr.nexus.initid.id)) 11056 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11057 return (CTL_ACTION_OVERLAP_TAG); 11058 11059 /* 11060 * If we get a head of queue tag, SAM-3 says that we should 11061 * immediately execute it. 11062 * 11063 * What happens if this command would normally block for some other 11064 * reason? e.g. a request sense with a head of queue tag 11065 * immediately after a write. Normally that would block, but this 11066 * will result in its getting executed immediately... 11067 * 11068 * We currently return "pass" instead of "skip", so we'll end up 11069 * going through the rest of the queue to check for overlapped tags. 11070 * 11071 * XXX KDM check for other types of blockage first?? 11072 */ 11073 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11074 return (CTL_ACTION_PASS); 11075 11076 /* 11077 * Ordered tags have to block until all items ahead of them 11078 * have completed. If we get called with an ordered tag, we always 11079 * block, if something else is ahead of us in the queue. 11080 */ 11081 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11082 return (CTL_ACTION_BLOCK); 11083 11084 /* 11085 * Simple tags get blocked until all head of queue and ordered tags 11086 * ahead of them have completed. I'm lumping untagged commands in 11087 * with simple tags here. XXX KDM is that the right thing to do? 11088 */ 11089 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11090 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11091 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11092 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11093 return (CTL_ACTION_BLOCK); 11094 11095 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11096 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11097 11098 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11099 11100 switch (serialize_row[pending_entry->seridx]) { 11101 case CTL_SER_BLOCK: 11102 return (CTL_ACTION_BLOCK); 11103 case CTL_SER_EXTENT: 11104 return (ctl_extent_check(pending_io, ooa_io)); 11105 case CTL_SER_EXTENTOPT: 11106 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11107 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11108 return (ctl_extent_check(pending_io, ooa_io)); 11109 /* FALLTHROUGH */ 11110 case CTL_SER_PASS: 11111 return (CTL_ACTION_PASS); 11112 case CTL_SER_BLOCKOPT: 11113 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11114 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11115 return (CTL_ACTION_BLOCK); 11116 return (CTL_ACTION_PASS); 11117 case CTL_SER_SKIP: 11118 return (CTL_ACTION_SKIP); 11119 default: 11120 panic("invalid serialization value %d", 11121 serialize_row[pending_entry->seridx]); 11122 } 11123 11124 return (CTL_ACTION_ERROR); 11125} 11126 11127/* 11128 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11129 * Assumptions: 11130 * - pending_io is generally either incoming, or on the blocked queue 11131 * - starting I/O is the I/O we want to start the check with. 11132 */ 11133static ctl_action 11134ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11135 union ctl_io *starting_io) 11136{ 11137 union ctl_io *ooa_io; 11138 ctl_action action; 11139 11140 mtx_assert(&lun->lun_lock, MA_OWNED); 11141 11142 /* 11143 * Run back along the OOA queue, starting with the current 11144 * blocked I/O and going through every I/O before it on the 11145 * queue. If starting_io is NULL, we'll just end up returning 11146 * CTL_ACTION_PASS. 11147 */ 11148 for (ooa_io = starting_io; ooa_io != NULL; 11149 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11150 ooa_links)){ 11151 11152 /* 11153 * This routine just checks to see whether 11154 * cur_blocked is blocked by ooa_io, which is ahead 11155 * of it in the queue. It doesn't queue/dequeue 11156 * cur_blocked. 11157 */ 11158 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11159 switch (action) { 11160 case CTL_ACTION_BLOCK: 11161 case CTL_ACTION_OVERLAP: 11162 case CTL_ACTION_OVERLAP_TAG: 11163 case CTL_ACTION_SKIP: 11164 case CTL_ACTION_ERROR: 11165 return (action); 11166 break; /* NOTREACHED */ 11167 case CTL_ACTION_PASS: 11168 break; 11169 default: 11170 panic("invalid action %d", action); 11171 break; /* NOTREACHED */ 11172 } 11173 } 11174 11175 return (CTL_ACTION_PASS); 11176} 11177 11178/* 11179 * Assumptions: 11180 * - An I/O has just completed, and has been removed from the per-LUN OOA 11181 * queue, so some items on the blocked queue may now be unblocked. 11182 */ 11183static int 11184ctl_check_blocked(struct ctl_lun *lun) 11185{ 11186 union ctl_io *cur_blocked, *next_blocked; 11187 11188 mtx_assert(&lun->lun_lock, MA_OWNED); 11189 11190 /* 11191 * Run forward from the head of the blocked queue, checking each 11192 * entry against the I/Os prior to it on the OOA queue to see if 11193 * there is still any blockage. 11194 * 11195 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11196 * with our removing a variable on it while it is traversing the 11197 * list. 11198 */ 11199 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11200 cur_blocked != NULL; cur_blocked = next_blocked) { 11201 union ctl_io *prev_ooa; 11202 ctl_action action; 11203 11204 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11205 blocked_links); 11206 11207 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11208 ctl_ooaq, ooa_links); 11209 11210 /* 11211 * If cur_blocked happens to be the first item in the OOA 11212 * queue now, prev_ooa will be NULL, and the action 11213 * returned will just be CTL_ACTION_PASS. 11214 */ 11215 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11216 11217 switch (action) { 11218 case CTL_ACTION_BLOCK: 11219 /* Nothing to do here, still blocked */ 11220 break; 11221 case CTL_ACTION_OVERLAP: 11222 case CTL_ACTION_OVERLAP_TAG: 11223 /* 11224 * This shouldn't happen! In theory we've already 11225 * checked this command for overlap... 11226 */ 11227 break; 11228 case CTL_ACTION_PASS: 11229 case CTL_ACTION_SKIP: { 11230 struct ctl_softc *softc; 11231 const struct ctl_cmd_entry *entry; 11232 uint32_t initidx; 11233 int isc_retval; 11234 11235 /* 11236 * The skip case shouldn't happen, this transaction 11237 * should have never made it onto the blocked queue. 11238 */ 11239 /* 11240 * This I/O is no longer blocked, we can remove it 11241 * from the blocked queue. Since this is a TAILQ 11242 * (doubly linked list), we can do O(1) removals 11243 * from any place on the list. 11244 */ 11245 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11246 blocked_links); 11247 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11248 11249 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11250 /* 11251 * Need to send IO back to original side to 11252 * run 11253 */ 11254 union ctl_ha_msg msg_info; 11255 11256 msg_info.hdr.original_sc = 11257 cur_blocked->io_hdr.original_sc; 11258 msg_info.hdr.serializing_sc = cur_blocked; 11259 msg_info.hdr.msg_type = CTL_MSG_R2R; 11260 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11261 &msg_info, sizeof(msg_info), 0)) > 11262 CTL_HA_STATUS_SUCCESS) { 11263 printf("CTL:Check Blocked error from " 11264 "ctl_ha_msg_send %d\n", 11265 isc_retval); 11266 } 11267 break; 11268 } 11269 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11270 softc = control_softc; 11271 11272 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11273 11274 /* 11275 * Check this I/O for LUN state changes that may 11276 * have happened while this command was blocked. 11277 * The LUN state may have been changed by a command 11278 * ahead of us in the queue, so we need to re-check 11279 * for any states that can be caused by SCSI 11280 * commands. 11281 */ 11282 if (ctl_scsiio_lun_check(softc, lun, entry, 11283 &cur_blocked->scsiio) == 0) { 11284 cur_blocked->io_hdr.flags |= 11285 CTL_FLAG_IS_WAS_ON_RTR; 11286 ctl_enqueue_rtr(cur_blocked); 11287 } else 11288 ctl_done(cur_blocked); 11289 break; 11290 } 11291 default: 11292 /* 11293 * This probably shouldn't happen -- we shouldn't 11294 * get CTL_ACTION_ERROR, or anything else. 11295 */ 11296 break; 11297 } 11298 } 11299 11300 return (CTL_RETVAL_COMPLETE); 11301} 11302 11303/* 11304 * This routine (with one exception) checks LUN flags that can be set by 11305 * commands ahead of us in the OOA queue. These flags have to be checked 11306 * when a command initially comes in, and when we pull a command off the 11307 * blocked queue and are preparing to execute it. The reason we have to 11308 * check these flags for commands on the blocked queue is that the LUN 11309 * state may have been changed by a command ahead of us while we're on the 11310 * blocked queue. 11311 * 11312 * Ordering is somewhat important with these checks, so please pay 11313 * careful attention to the placement of any new checks. 11314 */ 11315static int 11316ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11317 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11318{ 11319 int retval; 11320 uint32_t residx; 11321 11322 retval = 0; 11323 11324 mtx_assert(&lun->lun_lock, MA_OWNED); 11325 11326 /* 11327 * If this shelf is a secondary shelf controller, we have to reject 11328 * any media access commands. 11329 */ 11330#if 0 11331 /* No longer needed for HA */ 11332 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11333 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11334 ctl_set_lun_standby(ctsio); 11335 retval = 1; 11336 goto bailout; 11337 } 11338#endif 11339 11340 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11341 if (lun->flags & CTL_LUN_READONLY) { 11342 ctl_set_sense(ctsio, /*current_error*/ 1, 11343 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11344 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11345 retval = 1; 11346 goto bailout; 11347 } 11348 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11349 .eca_and_aen & SCP_SWP) != 0) { 11350 ctl_set_sense(ctsio, /*current_error*/ 1, 11351 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11352 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11353 retval = 1; 11354 goto bailout; 11355 } 11356 } 11357 11358 /* 11359 * Check for a reservation conflict. If this command isn't allowed 11360 * even on reserved LUNs, and if this initiator isn't the one who 11361 * reserved us, reject the command with a reservation conflict. 11362 */ 11363 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11364 if ((lun->flags & CTL_LUN_RESERVED) 11365 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11366 if (lun->res_idx != residx) { 11367 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11368 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11369 retval = 1; 11370 goto bailout; 11371 } 11372 } 11373 11374 if ((lun->flags & CTL_LUN_PR_RESERVED) 11375 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11376 /* 11377 * if we aren't registered or it's a res holder type 11378 * reservation and this isn't the res holder then set a 11379 * conflict. 11380 * NOTE: Commands which might be allowed on write exclusive 11381 * type reservations are checked in the particular command 11382 * for a conflict. Read and SSU are the only ones. 11383 */ 11384 if (!lun->per_res[residx].registered 11385 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11386 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11387 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11388 retval = 1; 11389 goto bailout; 11390 } 11391 11392 } 11393 11394 if ((lun->flags & CTL_LUN_OFFLINE) 11395 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11396 ctl_set_lun_not_ready(ctsio); 11397 retval = 1; 11398 goto bailout; 11399 } 11400 11401 /* 11402 * If the LUN is stopped, see if this particular command is allowed 11403 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11404 */ 11405 if ((lun->flags & CTL_LUN_STOPPED) 11406 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11407 /* "Logical unit not ready, initializing cmd. required" */ 11408 ctl_set_lun_stopped(ctsio); 11409 retval = 1; 11410 goto bailout; 11411 } 11412 11413 if ((lun->flags & CTL_LUN_INOPERABLE) 11414 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11415 /* "Medium format corrupted" */ 11416 ctl_set_medium_format_corrupted(ctsio); 11417 retval = 1; 11418 goto bailout; 11419 } 11420 11421bailout: 11422 return (retval); 11423 11424} 11425 11426static void 11427ctl_failover_io(union ctl_io *io, int have_lock) 11428{ 11429 ctl_set_busy(&io->scsiio); 11430 ctl_done(io); 11431} 11432 11433static void 11434ctl_failover(void) 11435{ 11436 struct ctl_lun *lun; 11437 struct ctl_softc *ctl_softc; 11438 union ctl_io *next_io, *pending_io; 11439 union ctl_io *io; 11440 int lun_idx; 11441 int i; 11442 11443 ctl_softc = control_softc; 11444 11445 mtx_lock(&ctl_softc->ctl_lock); 11446 /* 11447 * Remove any cmds from the other SC from the rtr queue. These 11448 * will obviously only be for LUNs for which we're the primary. 11449 * We can't send status or get/send data for these commands. 11450 * Since they haven't been executed yet, we can just remove them. 11451 * We'll either abort them or delete them below, depending on 11452 * which HA mode we're in. 11453 */ 11454#ifdef notyet 11455 mtx_lock(&ctl_softc->queue_lock); 11456 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11457 io != NULL; io = next_io) { 11458 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11459 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11460 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11461 ctl_io_hdr, links); 11462 } 11463 mtx_unlock(&ctl_softc->queue_lock); 11464#endif 11465 11466 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11467 lun = ctl_softc->ctl_luns[lun_idx]; 11468 if (lun==NULL) 11469 continue; 11470 11471 /* 11472 * Processor LUNs are primary on both sides. 11473 * XXX will this always be true? 11474 */ 11475 if (lun->be_lun->lun_type == T_PROCESSOR) 11476 continue; 11477 11478 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11479 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11480 printf("FAILOVER: primary lun %d\n", lun_idx); 11481 /* 11482 * Remove all commands from the other SC. First from the 11483 * blocked queue then from the ooa queue. Once we have 11484 * removed them. Call ctl_check_blocked to see if there 11485 * is anything that can run. 11486 */ 11487 for (io = (union ctl_io *)TAILQ_FIRST( 11488 &lun->blocked_queue); io != NULL; io = next_io) { 11489 11490 next_io = (union ctl_io *)TAILQ_NEXT( 11491 &io->io_hdr, blocked_links); 11492 11493 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11494 TAILQ_REMOVE(&lun->blocked_queue, 11495 &io->io_hdr,blocked_links); 11496 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11497 TAILQ_REMOVE(&lun->ooa_queue, 11498 &io->io_hdr, ooa_links); 11499 11500 ctl_free_io(io); 11501 } 11502 } 11503 11504 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11505 io != NULL; io = next_io) { 11506 11507 next_io = (union ctl_io *)TAILQ_NEXT( 11508 &io->io_hdr, ooa_links); 11509 11510 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11511 11512 TAILQ_REMOVE(&lun->ooa_queue, 11513 &io->io_hdr, 11514 ooa_links); 11515 11516 ctl_free_io(io); 11517 } 11518 } 11519 ctl_check_blocked(lun); 11520 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11521 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11522 11523 printf("FAILOVER: primary lun %d\n", lun_idx); 11524 /* 11525 * Abort all commands from the other SC. We can't 11526 * send status back for them now. These should get 11527 * cleaned up when they are completed or come out 11528 * for a datamove operation. 11529 */ 11530 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11531 io != NULL; io = next_io) { 11532 next_io = (union ctl_io *)TAILQ_NEXT( 11533 &io->io_hdr, ooa_links); 11534 11535 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11536 io->io_hdr.flags |= CTL_FLAG_ABORT; 11537 } 11538 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11539 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11540 11541 printf("FAILOVER: secondary lun %d\n", lun_idx); 11542 11543 lun->flags |= CTL_LUN_PRIMARY_SC; 11544 11545 /* 11546 * We send all I/O that was sent to this controller 11547 * and redirected to the other side back with 11548 * busy status, and have the initiator retry it. 11549 * Figuring out how much data has been transferred, 11550 * etc. and picking up where we left off would be 11551 * very tricky. 11552 * 11553 * XXX KDM need to remove I/O from the blocked 11554 * queue as well! 11555 */ 11556 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11557 &lun->ooa_queue); pending_io != NULL; 11558 pending_io = next_io) { 11559 11560 next_io = (union ctl_io *)TAILQ_NEXT( 11561 &pending_io->io_hdr, ooa_links); 11562 11563 pending_io->io_hdr.flags &= 11564 ~CTL_FLAG_SENT_2OTHER_SC; 11565 11566 if (pending_io->io_hdr.flags & 11567 CTL_FLAG_IO_ACTIVE) { 11568 pending_io->io_hdr.flags |= 11569 CTL_FLAG_FAILOVER; 11570 } else { 11571 ctl_set_busy(&pending_io->scsiio); 11572 ctl_done(pending_io); 11573 } 11574 } 11575 11576 /* 11577 * Build Unit Attention 11578 */ 11579 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11580 lun->pending_ua[i] |= 11581 CTL_UA_ASYM_ACC_CHANGE; 11582 } 11583 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11584 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11585 printf("FAILOVER: secondary lun %d\n", lun_idx); 11586 /* 11587 * if the first io on the OOA is not on the RtR queue 11588 * add it. 11589 */ 11590 lun->flags |= CTL_LUN_PRIMARY_SC; 11591 11592 pending_io = (union ctl_io *)TAILQ_FIRST( 11593 &lun->ooa_queue); 11594 if (pending_io==NULL) { 11595 printf("Nothing on OOA queue\n"); 11596 continue; 11597 } 11598 11599 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11600 if ((pending_io->io_hdr.flags & 11601 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11602 pending_io->io_hdr.flags |= 11603 CTL_FLAG_IS_WAS_ON_RTR; 11604 ctl_enqueue_rtr(pending_io); 11605 } 11606#if 0 11607 else 11608 { 11609 printf("Tag 0x%04x is running\n", 11610 pending_io->scsiio.tag_num); 11611 } 11612#endif 11613 11614 next_io = (union ctl_io *)TAILQ_NEXT( 11615 &pending_io->io_hdr, ooa_links); 11616 for (pending_io=next_io; pending_io != NULL; 11617 pending_io = next_io) { 11618 pending_io->io_hdr.flags &= 11619 ~CTL_FLAG_SENT_2OTHER_SC; 11620 next_io = (union ctl_io *)TAILQ_NEXT( 11621 &pending_io->io_hdr, ooa_links); 11622 if (pending_io->io_hdr.flags & 11623 CTL_FLAG_IS_WAS_ON_RTR) { 11624#if 0 11625 printf("Tag 0x%04x is running\n", 11626 pending_io->scsiio.tag_num); 11627#endif 11628 continue; 11629 } 11630 11631 switch (ctl_check_ooa(lun, pending_io, 11632 (union ctl_io *)TAILQ_PREV( 11633 &pending_io->io_hdr, ctl_ooaq, 11634 ooa_links))) { 11635 11636 case CTL_ACTION_BLOCK: 11637 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11638 &pending_io->io_hdr, 11639 blocked_links); 11640 pending_io->io_hdr.flags |= 11641 CTL_FLAG_BLOCKED; 11642 break; 11643 case CTL_ACTION_PASS: 11644 case CTL_ACTION_SKIP: 11645 pending_io->io_hdr.flags |= 11646 CTL_FLAG_IS_WAS_ON_RTR; 11647 ctl_enqueue_rtr(pending_io); 11648 break; 11649 case CTL_ACTION_OVERLAP: 11650 ctl_set_overlapped_cmd( 11651 (struct ctl_scsiio *)pending_io); 11652 ctl_done(pending_io); 11653 break; 11654 case CTL_ACTION_OVERLAP_TAG: 11655 ctl_set_overlapped_tag( 11656 (struct ctl_scsiio *)pending_io, 11657 pending_io->scsiio.tag_num & 0xff); 11658 ctl_done(pending_io); 11659 break; 11660 case CTL_ACTION_ERROR: 11661 default: 11662 ctl_set_internal_failure( 11663 (struct ctl_scsiio *)pending_io, 11664 0, // sks_valid 11665 0); //retry count 11666 ctl_done(pending_io); 11667 break; 11668 } 11669 } 11670 11671 /* 11672 * Build Unit Attention 11673 */ 11674 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11675 lun->pending_ua[i] |= 11676 CTL_UA_ASYM_ACC_CHANGE; 11677 } 11678 } else { 11679 panic("Unhandled HA mode failover, LUN flags = %#x, " 11680 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11681 } 11682 } 11683 ctl_pause_rtr = 0; 11684 mtx_unlock(&ctl_softc->ctl_lock); 11685} 11686 11687static int 11688ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11689{ 11690 struct ctl_lun *lun; 11691 const struct ctl_cmd_entry *entry; 11692 uint32_t initidx, targ_lun; 11693 int retval; 11694 11695 retval = 0; 11696 11697 lun = NULL; 11698 11699 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11700 if ((targ_lun < CTL_MAX_LUNS) 11701 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11702 lun = ctl_softc->ctl_luns[targ_lun]; 11703 /* 11704 * If the LUN is invalid, pretend that it doesn't exist. 11705 * It will go away as soon as all pending I/O has been 11706 * completed. 11707 */ 11708 if (lun->flags & CTL_LUN_DISABLED) { 11709 lun = NULL; 11710 } else { 11711 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11712 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11713 lun->be_lun; 11714 if (lun->be_lun->lun_type == T_PROCESSOR) { 11715 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11716 } 11717 11718 /* 11719 * Every I/O goes into the OOA queue for a 11720 * particular LUN, and stays there until completion. 11721 */ 11722 mtx_lock(&lun->lun_lock); 11723 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11724 ooa_links); 11725 } 11726 } else { 11727 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11728 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11729 } 11730 11731 /* Get command entry and return error if it is unsuppotyed. */ 11732 entry = ctl_validate_command(ctsio); 11733 if (entry == NULL) { 11734 if (lun) 11735 mtx_unlock(&lun->lun_lock); 11736 return (retval); 11737 } 11738 11739 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11740 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11741 11742 /* 11743 * Check to see whether we can send this command to LUNs that don't 11744 * exist. This should pretty much only be the case for inquiry 11745 * and request sense. Further checks, below, really require having 11746 * a LUN, so we can't really check the command anymore. Just put 11747 * it on the rtr queue. 11748 */ 11749 if (lun == NULL) { 11750 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11751 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11752 ctl_enqueue_rtr((union ctl_io *)ctsio); 11753 return (retval); 11754 } 11755 11756 ctl_set_unsupported_lun(ctsio); 11757 ctl_done((union ctl_io *)ctsio); 11758 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11759 return (retval); 11760 } else { 11761 /* 11762 * Make sure we support this particular command on this LUN. 11763 * e.g., we don't support writes to the control LUN. 11764 */ 11765 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11766 mtx_unlock(&lun->lun_lock); 11767 ctl_set_invalid_opcode(ctsio); 11768 ctl_done((union ctl_io *)ctsio); 11769 return (retval); 11770 } 11771 } 11772 11773 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11774 11775#ifdef CTL_WITH_CA 11776 /* 11777 * If we've got a request sense, it'll clear the contingent 11778 * allegiance condition. Otherwise, if we have a CA condition for 11779 * this initiator, clear it, because it sent down a command other 11780 * than request sense. 11781 */ 11782 if ((ctsio->cdb[0] != REQUEST_SENSE) 11783 && (ctl_is_set(lun->have_ca, initidx))) 11784 ctl_clear_mask(lun->have_ca, initidx); 11785#endif 11786 11787 /* 11788 * If the command has this flag set, it handles its own unit 11789 * attention reporting, we shouldn't do anything. Otherwise we 11790 * check for any pending unit attentions, and send them back to the 11791 * initiator. We only do this when a command initially comes in, 11792 * not when we pull it off the blocked queue. 11793 * 11794 * According to SAM-3, section 5.3.2, the order that things get 11795 * presented back to the host is basically unit attentions caused 11796 * by some sort of reset event, busy status, reservation conflicts 11797 * or task set full, and finally any other status. 11798 * 11799 * One issue here is that some of the unit attentions we report 11800 * don't fall into the "reset" category (e.g. "reported luns data 11801 * has changed"). So reporting it here, before the reservation 11802 * check, may be technically wrong. I guess the only thing to do 11803 * would be to check for and report the reset events here, and then 11804 * check for the other unit attention types after we check for a 11805 * reservation conflict. 11806 * 11807 * XXX KDM need to fix this 11808 */ 11809 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11810 ctl_ua_type ua_type; 11811 11812 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11813 scsi_sense_data_type sense_format; 11814 11815 if (lun != NULL) 11816 sense_format = (lun->flags & 11817 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11818 SSD_TYPE_FIXED; 11819 else 11820 sense_format = SSD_TYPE_FIXED; 11821 11822 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11823 &ctsio->sense_data, sense_format); 11824 if (ua_type != CTL_UA_NONE) { 11825 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11826 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11827 CTL_AUTOSENSE; 11828 ctsio->sense_len = SSD_FULL_SIZE; 11829 mtx_unlock(&lun->lun_lock); 11830 ctl_done((union ctl_io *)ctsio); 11831 return (retval); 11832 } 11833 } 11834 } 11835 11836 11837 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11838 mtx_unlock(&lun->lun_lock); 11839 ctl_done((union ctl_io *)ctsio); 11840 return (retval); 11841 } 11842 11843 /* 11844 * XXX CHD this is where we want to send IO to other side if 11845 * this LUN is secondary on this SC. We will need to make a copy 11846 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11847 * the copy we send as FROM_OTHER. 11848 * We also need to stuff the address of the original IO so we can 11849 * find it easily. Something similar will need be done on the other 11850 * side so when we are done we can find the copy. 11851 */ 11852 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11853 union ctl_ha_msg msg_info; 11854 int isc_retval; 11855 11856 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11857 11858 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11859 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11860#if 0 11861 printf("1. ctsio %p\n", ctsio); 11862#endif 11863 msg_info.hdr.serializing_sc = NULL; 11864 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11865 msg_info.scsi.tag_num = ctsio->tag_num; 11866 msg_info.scsi.tag_type = ctsio->tag_type; 11867 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11868 11869 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11870 11871 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11872 (void *)&msg_info, sizeof(msg_info), 0)) > 11873 CTL_HA_STATUS_SUCCESS) { 11874 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11875 isc_retval); 11876 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11877 } else { 11878#if 0 11879 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11880#endif 11881 } 11882 11883 /* 11884 * XXX KDM this I/O is off the incoming queue, but hasn't 11885 * been inserted on any other queue. We may need to come 11886 * up with a holding queue while we wait for serialization 11887 * so that we have an idea of what we're waiting for from 11888 * the other side. 11889 */ 11890 mtx_unlock(&lun->lun_lock); 11891 return (retval); 11892 } 11893 11894 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11895 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11896 ctl_ooaq, ooa_links))) { 11897 case CTL_ACTION_BLOCK: 11898 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11899 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11900 blocked_links); 11901 mtx_unlock(&lun->lun_lock); 11902 return (retval); 11903 case CTL_ACTION_PASS: 11904 case CTL_ACTION_SKIP: 11905 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11906 mtx_unlock(&lun->lun_lock); 11907 ctl_enqueue_rtr((union ctl_io *)ctsio); 11908 break; 11909 case CTL_ACTION_OVERLAP: 11910 mtx_unlock(&lun->lun_lock); 11911 ctl_set_overlapped_cmd(ctsio); 11912 ctl_done((union ctl_io *)ctsio); 11913 break; 11914 case CTL_ACTION_OVERLAP_TAG: 11915 mtx_unlock(&lun->lun_lock); 11916 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11917 ctl_done((union ctl_io *)ctsio); 11918 break; 11919 case CTL_ACTION_ERROR: 11920 default: 11921 mtx_unlock(&lun->lun_lock); 11922 ctl_set_internal_failure(ctsio, 11923 /*sks_valid*/ 0, 11924 /*retry_count*/ 0); 11925 ctl_done((union ctl_io *)ctsio); 11926 break; 11927 } 11928 return (retval); 11929} 11930 11931const struct ctl_cmd_entry * 11932ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11933{ 11934 const struct ctl_cmd_entry *entry; 11935 int service_action; 11936 11937 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11938 if (sa) 11939 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11940 if (entry->flags & CTL_CMD_FLAG_SA5) { 11941 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11942 entry = &((const struct ctl_cmd_entry *) 11943 entry->execute)[service_action]; 11944 } 11945 return (entry); 11946} 11947 11948const struct ctl_cmd_entry * 11949ctl_validate_command(struct ctl_scsiio *ctsio) 11950{ 11951 const struct ctl_cmd_entry *entry; 11952 int i, sa; 11953 uint8_t diff; 11954 11955 entry = ctl_get_cmd_entry(ctsio, &sa); 11956 if (entry->execute == NULL) { 11957 if (sa) 11958 ctl_set_invalid_field(ctsio, 11959 /*sks_valid*/ 1, 11960 /*command*/ 1, 11961 /*field*/ 1, 11962 /*bit_valid*/ 1, 11963 /*bit*/ 4); 11964 else 11965 ctl_set_invalid_opcode(ctsio); 11966 ctl_done((union ctl_io *)ctsio); 11967 return (NULL); 11968 } 11969 KASSERT(entry->length > 0, 11970 ("Not defined length for command 0x%02x/0x%02x", 11971 ctsio->cdb[0], ctsio->cdb[1])); 11972 for (i = 1; i < entry->length; i++) { 11973 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11974 if (diff == 0) 11975 continue; 11976 ctl_set_invalid_field(ctsio, 11977 /*sks_valid*/ 1, 11978 /*command*/ 1, 11979 /*field*/ i, 11980 /*bit_valid*/ 1, 11981 /*bit*/ fls(diff) - 1); 11982 ctl_done((union ctl_io *)ctsio); 11983 return (NULL); 11984 } 11985 return (entry); 11986} 11987 11988static int 11989ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11990{ 11991 11992 switch (lun_type) { 11993 case T_PROCESSOR: 11994 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11995 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11996 return (0); 11997 break; 11998 case T_DIRECT: 11999 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 12000 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 12001 return (0); 12002 break; 12003 default: 12004 return (0); 12005 } 12006 return (1); 12007} 12008 12009static int 12010ctl_scsiio(struct ctl_scsiio *ctsio) 12011{ 12012 int retval; 12013 const struct ctl_cmd_entry *entry; 12014 12015 retval = CTL_RETVAL_COMPLETE; 12016 12017 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 12018 12019 entry = ctl_get_cmd_entry(ctsio, NULL); 12020 12021 /* 12022 * If this I/O has been aborted, just send it straight to 12023 * ctl_done() without executing it. 12024 */ 12025 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 12026 ctl_done((union ctl_io *)ctsio); 12027 goto bailout; 12028 } 12029 12030 /* 12031 * All the checks should have been handled by ctl_scsiio_precheck(). 12032 * We should be clear now to just execute the I/O. 12033 */ 12034 retval = entry->execute(ctsio); 12035 12036bailout: 12037 return (retval); 12038} 12039 12040/* 12041 * Since we only implement one target right now, a bus reset simply resets 12042 * our single target. 12043 */ 12044static int 12045ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 12046{ 12047 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 12048} 12049 12050static int 12051ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 12052 ctl_ua_type ua_type) 12053{ 12054 struct ctl_lun *lun; 12055 int retval; 12056 12057 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12058 union ctl_ha_msg msg_info; 12059 12060 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12061 msg_info.hdr.nexus = io->io_hdr.nexus; 12062 if (ua_type==CTL_UA_TARG_RESET) 12063 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 12064 else 12065 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12066 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12067 msg_info.hdr.original_sc = NULL; 12068 msg_info.hdr.serializing_sc = NULL; 12069 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12070 (void *)&msg_info, sizeof(msg_info), 0)) { 12071 } 12072 } 12073 retval = 0; 12074 12075 mtx_lock(&ctl_softc->ctl_lock); 12076 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 12077 retval += ctl_lun_reset(lun, io, ua_type); 12078 mtx_unlock(&ctl_softc->ctl_lock); 12079 12080 return (retval); 12081} 12082 12083/* 12084 * The LUN should always be set. The I/O is optional, and is used to 12085 * distinguish between I/Os sent by this initiator, and by other 12086 * initiators. We set unit attention for initiators other than this one. 12087 * SAM-3 is vague on this point. It does say that a unit attention should 12088 * be established for other initiators when a LUN is reset (see section 12089 * 5.7.3), but it doesn't specifically say that the unit attention should 12090 * be established for this particular initiator when a LUN is reset. Here 12091 * is the relevant text, from SAM-3 rev 8: 12092 * 12093 * 5.7.2 When a SCSI initiator port aborts its own tasks 12094 * 12095 * When a SCSI initiator port causes its own task(s) to be aborted, no 12096 * notification that the task(s) have been aborted shall be returned to 12097 * the SCSI initiator port other than the completion response for the 12098 * command or task management function action that caused the task(s) to 12099 * be aborted and notification(s) associated with related effects of the 12100 * action (e.g., a reset unit attention condition). 12101 * 12102 * XXX KDM for now, we're setting unit attention for all initiators. 12103 */ 12104static int 12105ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12106{ 12107 union ctl_io *xio; 12108#if 0 12109 uint32_t initindex; 12110#endif 12111 int i; 12112 12113 mtx_lock(&lun->lun_lock); 12114 /* 12115 * Run through the OOA queue and abort each I/O. 12116 */ 12117#if 0 12118 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12119#endif 12120 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12121 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12122 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12123 } 12124 12125 /* 12126 * This version sets unit attention for every 12127 */ 12128#if 0 12129 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12130 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12131 if (initindex == i) 12132 continue; 12133 lun->pending_ua[i] |= ua_type; 12134 } 12135#endif 12136 12137 /* 12138 * A reset (any kind, really) clears reservations established with 12139 * RESERVE/RELEASE. It does not clear reservations established 12140 * with PERSISTENT RESERVE OUT, but we don't support that at the 12141 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12142 * reservations made with the RESERVE/RELEASE commands, because 12143 * those commands are obsolete in SPC-3. 12144 */ 12145 lun->flags &= ~CTL_LUN_RESERVED; 12146 12147 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12148#ifdef CTL_WITH_CA 12149 ctl_clear_mask(lun->have_ca, i); 12150#endif 12151 lun->pending_ua[i] |= ua_type; 12152 } 12153 mtx_unlock(&lun->lun_lock); 12154 12155 return (0); 12156} 12157 12158static void 12159ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12160 int other_sc) 12161{ 12162 union ctl_io *xio; 12163 12164 mtx_assert(&lun->lun_lock, MA_OWNED); 12165 12166 /* 12167 * Run through the OOA queue and attempt to find the given I/O. 12168 * The target port, initiator ID, tag type and tag number have to 12169 * match the values that we got from the initiator. If we have an 12170 * untagged command to abort, simply abort the first untagged command 12171 * we come to. We only allow one untagged command at a time of course. 12172 */ 12173 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12174 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12175 12176 if ((targ_port == UINT32_MAX || 12177 targ_port == xio->io_hdr.nexus.targ_port) && 12178 (init_id == UINT32_MAX || 12179 init_id == xio->io_hdr.nexus.initid.id)) { 12180 if (targ_port != xio->io_hdr.nexus.targ_port || 12181 init_id != xio->io_hdr.nexus.initid.id) 12182 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12183 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12184 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12185 union ctl_ha_msg msg_info; 12186 12187 msg_info.hdr.nexus = xio->io_hdr.nexus; 12188 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12189 msg_info.task.tag_num = xio->scsiio.tag_num; 12190 msg_info.task.tag_type = xio->scsiio.tag_type; 12191 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12192 msg_info.hdr.original_sc = NULL; 12193 msg_info.hdr.serializing_sc = NULL; 12194 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12195 (void *)&msg_info, sizeof(msg_info), 0); 12196 } 12197 } 12198 } 12199} 12200 12201static int 12202ctl_abort_task_set(union ctl_io *io) 12203{ 12204 struct ctl_softc *softc = control_softc; 12205 struct ctl_lun *lun; 12206 uint32_t targ_lun; 12207 12208 /* 12209 * Look up the LUN. 12210 */ 12211 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12212 mtx_lock(&softc->ctl_lock); 12213 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12214 lun = softc->ctl_luns[targ_lun]; 12215 else { 12216 mtx_unlock(&softc->ctl_lock); 12217 return (1); 12218 } 12219 12220 mtx_lock(&lun->lun_lock); 12221 mtx_unlock(&softc->ctl_lock); 12222 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12223 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12224 io->io_hdr.nexus.initid.id, 12225 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12226 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12227 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12228 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12229 } 12230 mtx_unlock(&lun->lun_lock); 12231 return (0); 12232} 12233 12234static int 12235ctl_i_t_nexus_reset(union ctl_io *io) 12236{ 12237 struct ctl_softc *softc = control_softc; 12238 struct ctl_lun *lun; 12239 uint32_t initindex, residx; 12240 12241 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12242 residx = ctl_get_resindex(&io->io_hdr.nexus); 12243 mtx_lock(&softc->ctl_lock); 12244 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12245 mtx_lock(&lun->lun_lock); 12246 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12247 io->io_hdr.nexus.initid.id, 12248 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12249#ifdef CTL_WITH_CA 12250 ctl_clear_mask(lun->have_ca, initindex); 12251#endif 12252 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12253 lun->flags &= ~CTL_LUN_RESERVED; 12254 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12255 mtx_unlock(&lun->lun_lock); 12256 } 12257 mtx_unlock(&softc->ctl_lock); 12258 return (0); 12259} 12260 12261static int 12262ctl_abort_task(union ctl_io *io) 12263{ 12264 union ctl_io *xio; 12265 struct ctl_lun *lun; 12266 struct ctl_softc *ctl_softc; 12267#if 0 12268 struct sbuf sb; 12269 char printbuf[128]; 12270#endif 12271 int found; 12272 uint32_t targ_lun; 12273 12274 ctl_softc = control_softc; 12275 found = 0; 12276 12277 /* 12278 * Look up the LUN. 12279 */ 12280 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12281 mtx_lock(&ctl_softc->ctl_lock); 12282 if ((targ_lun < CTL_MAX_LUNS) 12283 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12284 lun = ctl_softc->ctl_luns[targ_lun]; 12285 else { 12286 mtx_unlock(&ctl_softc->ctl_lock); 12287 return (1); 12288 } 12289 12290#if 0 12291 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12292 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12293#endif 12294 12295 mtx_lock(&lun->lun_lock); 12296 mtx_unlock(&ctl_softc->ctl_lock); 12297 /* 12298 * Run through the OOA queue and attempt to find the given I/O. 12299 * The target port, initiator ID, tag type and tag number have to 12300 * match the values that we got from the initiator. If we have an 12301 * untagged command to abort, simply abort the first untagged command 12302 * we come to. We only allow one untagged command at a time of course. 12303 */ 12304#if 0 12305 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12306#endif 12307 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12308 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12309#if 0 12310 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12311 12312 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12313 lun->lun, xio->scsiio.tag_num, 12314 xio->scsiio.tag_type, 12315 (xio->io_hdr.blocked_links.tqe_prev 12316 == NULL) ? "" : " BLOCKED", 12317 (xio->io_hdr.flags & 12318 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12319 (xio->io_hdr.flags & 12320 CTL_FLAG_ABORT) ? " ABORT" : "", 12321 (xio->io_hdr.flags & 12322 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12323 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12324 sbuf_finish(&sb); 12325 printf("%s\n", sbuf_data(&sb)); 12326#endif 12327 12328 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12329 && (xio->io_hdr.nexus.initid.id == 12330 io->io_hdr.nexus.initid.id)) { 12331 /* 12332 * If the abort says that the task is untagged, the 12333 * task in the queue must be untagged. Otherwise, 12334 * we just check to see whether the tag numbers 12335 * match. This is because the QLogic firmware 12336 * doesn't pass back the tag type in an abort 12337 * request. 12338 */ 12339#if 0 12340 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12341 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12342 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12343#endif 12344 /* 12345 * XXX KDM we've got problems with FC, because it 12346 * doesn't send down a tag type with aborts. So we 12347 * can only really go by the tag number... 12348 * This may cause problems with parallel SCSI. 12349 * Need to figure that out!! 12350 */ 12351 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12352 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12353 found = 1; 12354 if ((io->io_hdr.flags & 12355 CTL_FLAG_FROM_OTHER_SC) == 0 && 12356 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12357 union ctl_ha_msg msg_info; 12358 12359 io->io_hdr.flags |= 12360 CTL_FLAG_SENT_2OTHER_SC; 12361 msg_info.hdr.nexus = io->io_hdr.nexus; 12362 msg_info.task.task_action = 12363 CTL_TASK_ABORT_TASK; 12364 msg_info.task.tag_num = 12365 io->taskio.tag_num; 12366 msg_info.task.tag_type = 12367 io->taskio.tag_type; 12368 msg_info.hdr.msg_type = 12369 CTL_MSG_MANAGE_TASKS; 12370 msg_info.hdr.original_sc = NULL; 12371 msg_info.hdr.serializing_sc = NULL; 12372#if 0 12373 printf("Sent Abort to other side\n"); 12374#endif 12375 if (CTL_HA_STATUS_SUCCESS != 12376 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12377 (void *)&msg_info, 12378 sizeof(msg_info), 0)) { 12379 } 12380 } 12381#if 0 12382 printf("ctl_abort_task: found I/O to abort\n"); 12383#endif 12384 break; 12385 } 12386 } 12387 } 12388 mtx_unlock(&lun->lun_lock); 12389 12390 if (found == 0) { 12391 /* 12392 * This isn't really an error. It's entirely possible for 12393 * the abort and command completion to cross on the wire. 12394 * This is more of an informative/diagnostic error. 12395 */ 12396#if 0 12397 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12398 "%d:%d:%d:%d tag %d type %d\n", 12399 io->io_hdr.nexus.initid.id, 12400 io->io_hdr.nexus.targ_port, 12401 io->io_hdr.nexus.targ_target.id, 12402 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12403 io->taskio.tag_type); 12404#endif 12405 } 12406 return (0); 12407} 12408 12409static void 12410ctl_run_task(union ctl_io *io) 12411{ 12412 struct ctl_softc *ctl_softc = control_softc; 12413 int retval = 1; 12414 const char *task_desc; 12415 12416 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12417 12418 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12419 ("ctl_run_task: Unextected io_type %d\n", 12420 io->io_hdr.io_type)); 12421 12422 task_desc = ctl_scsi_task_string(&io->taskio); 12423 if (task_desc != NULL) { 12424#ifdef NEEDTOPORT 12425 csevent_log(CSC_CTL | CSC_SHELF_SW | 12426 CTL_TASK_REPORT, 12427 csevent_LogType_Trace, 12428 csevent_Severity_Information, 12429 csevent_AlertLevel_Green, 12430 csevent_FRU_Firmware, 12431 csevent_FRU_Unknown, 12432 "CTL: received task: %s",task_desc); 12433#endif 12434 } else { 12435#ifdef NEEDTOPORT 12436 csevent_log(CSC_CTL | CSC_SHELF_SW | 12437 CTL_TASK_REPORT, 12438 csevent_LogType_Trace, 12439 csevent_Severity_Information, 12440 csevent_AlertLevel_Green, 12441 csevent_FRU_Firmware, 12442 csevent_FRU_Unknown, 12443 "CTL: received unknown task " 12444 "type: %d (%#x)", 12445 io->taskio.task_action, 12446 io->taskio.task_action); 12447#endif 12448 } 12449 switch (io->taskio.task_action) { 12450 case CTL_TASK_ABORT_TASK: 12451 retval = ctl_abort_task(io); 12452 break; 12453 case CTL_TASK_ABORT_TASK_SET: 12454 case CTL_TASK_CLEAR_TASK_SET: 12455 retval = ctl_abort_task_set(io); 12456 break; 12457 case CTL_TASK_CLEAR_ACA: 12458 break; 12459 case CTL_TASK_I_T_NEXUS_RESET: 12460 retval = ctl_i_t_nexus_reset(io); 12461 break; 12462 case CTL_TASK_LUN_RESET: { 12463 struct ctl_lun *lun; 12464 uint32_t targ_lun; 12465 12466 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12467 mtx_lock(&ctl_softc->ctl_lock); 12468 if ((targ_lun < CTL_MAX_LUNS) 12469 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12470 lun = ctl_softc->ctl_luns[targ_lun]; 12471 else { 12472 mtx_unlock(&ctl_softc->ctl_lock); 12473 retval = 1; 12474 break; 12475 } 12476 12477 if (!(io->io_hdr.flags & 12478 CTL_FLAG_FROM_OTHER_SC)) { 12479 union ctl_ha_msg msg_info; 12480 12481 io->io_hdr.flags |= 12482 CTL_FLAG_SENT_2OTHER_SC; 12483 msg_info.hdr.msg_type = 12484 CTL_MSG_MANAGE_TASKS; 12485 msg_info.hdr.nexus = io->io_hdr.nexus; 12486 msg_info.task.task_action = 12487 CTL_TASK_LUN_RESET; 12488 msg_info.hdr.original_sc = NULL; 12489 msg_info.hdr.serializing_sc = NULL; 12490 if (CTL_HA_STATUS_SUCCESS != 12491 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12492 (void *)&msg_info, 12493 sizeof(msg_info), 0)) { 12494 } 12495 } 12496 12497 retval = ctl_lun_reset(lun, io, 12498 CTL_UA_LUN_RESET); 12499 mtx_unlock(&ctl_softc->ctl_lock); 12500 break; 12501 } 12502 case CTL_TASK_TARGET_RESET: 12503 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12504 break; 12505 case CTL_TASK_BUS_RESET: 12506 retval = ctl_bus_reset(ctl_softc, io); 12507 break; 12508 case CTL_TASK_PORT_LOGIN: 12509 break; 12510 case CTL_TASK_PORT_LOGOUT: 12511 break; 12512 default: 12513 printf("ctl_run_task: got unknown task management event %d\n", 12514 io->taskio.task_action); 12515 break; 12516 } 12517 if (retval == 0) 12518 io->io_hdr.status = CTL_SUCCESS; 12519 else 12520 io->io_hdr.status = CTL_ERROR; 12521 ctl_done(io); 12522} 12523 12524/* 12525 * For HA operation. Handle commands that come in from the other 12526 * controller. 12527 */ 12528static void 12529ctl_handle_isc(union ctl_io *io) 12530{ 12531 int free_io; 12532 struct ctl_lun *lun; 12533 struct ctl_softc *ctl_softc; 12534 uint32_t targ_lun; 12535 12536 ctl_softc = control_softc; 12537 12538 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12539 lun = ctl_softc->ctl_luns[targ_lun]; 12540 12541 switch (io->io_hdr.msg_type) { 12542 case CTL_MSG_SERIALIZE: 12543 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12544 break; 12545 case CTL_MSG_R2R: { 12546 const struct ctl_cmd_entry *entry; 12547 12548 /* 12549 * This is only used in SER_ONLY mode. 12550 */ 12551 free_io = 0; 12552 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12553 mtx_lock(&lun->lun_lock); 12554 if (ctl_scsiio_lun_check(ctl_softc, lun, 12555 entry, (struct ctl_scsiio *)io) != 0) { 12556 mtx_unlock(&lun->lun_lock); 12557 ctl_done(io); 12558 break; 12559 } 12560 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12561 mtx_unlock(&lun->lun_lock); 12562 ctl_enqueue_rtr(io); 12563 break; 12564 } 12565 case CTL_MSG_FINISH_IO: 12566 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12567 free_io = 0; 12568 ctl_done(io); 12569 } else { 12570 free_io = 1; 12571 mtx_lock(&lun->lun_lock); 12572 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12573 ooa_links); 12574 ctl_check_blocked(lun); 12575 mtx_unlock(&lun->lun_lock); 12576 } 12577 break; 12578 case CTL_MSG_PERS_ACTION: 12579 ctl_hndl_per_res_out_on_other_sc( 12580 (union ctl_ha_msg *)&io->presio.pr_msg); 12581 free_io = 1; 12582 break; 12583 case CTL_MSG_BAD_JUJU: 12584 free_io = 0; 12585 ctl_done(io); 12586 break; 12587 case CTL_MSG_DATAMOVE: 12588 /* Only used in XFER mode */ 12589 free_io = 0; 12590 ctl_datamove_remote(io); 12591 break; 12592 case CTL_MSG_DATAMOVE_DONE: 12593 /* Only used in XFER mode */ 12594 free_io = 0; 12595 io->scsiio.be_move_done(io); 12596 break; 12597 default: 12598 free_io = 1; 12599 printf("%s: Invalid message type %d\n", 12600 __func__, io->io_hdr.msg_type); 12601 break; 12602 } 12603 if (free_io) 12604 ctl_free_io(io); 12605 12606} 12607 12608 12609/* 12610 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12611 * there is no match. 12612 */ 12613static ctl_lun_error_pattern 12614ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12615{ 12616 const struct ctl_cmd_entry *entry; 12617 ctl_lun_error_pattern filtered_pattern, pattern; 12618 12619 pattern = desc->error_pattern; 12620 12621 /* 12622 * XXX KDM we need more data passed into this function to match a 12623 * custom pattern, and we actually need to implement custom pattern 12624 * matching. 12625 */ 12626 if (pattern & CTL_LUN_PAT_CMD) 12627 return (CTL_LUN_PAT_CMD); 12628 12629 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12630 return (CTL_LUN_PAT_ANY); 12631 12632 entry = ctl_get_cmd_entry(ctsio, NULL); 12633 12634 filtered_pattern = entry->pattern & pattern; 12635 12636 /* 12637 * If the user requested specific flags in the pattern (e.g. 12638 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12639 * flags. 12640 * 12641 * If the user did not specify any flags, it doesn't matter whether 12642 * or not the command supports the flags. 12643 */ 12644 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12645 (pattern & ~CTL_LUN_PAT_MASK)) 12646 return (CTL_LUN_PAT_NONE); 12647 12648 /* 12649 * If the user asked for a range check, see if the requested LBA 12650 * range overlaps with this command's LBA range. 12651 */ 12652 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12653 uint64_t lba1; 12654 uint64_t len1; 12655 ctl_action action; 12656 int retval; 12657 12658 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12659 if (retval != 0) 12660 return (CTL_LUN_PAT_NONE); 12661 12662 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12663 desc->lba_range.len); 12664 /* 12665 * A "pass" means that the LBA ranges don't overlap, so 12666 * this doesn't match the user's range criteria. 12667 */ 12668 if (action == CTL_ACTION_PASS) 12669 return (CTL_LUN_PAT_NONE); 12670 } 12671 12672 return (filtered_pattern); 12673} 12674 12675static void 12676ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12677{ 12678 struct ctl_error_desc *desc, *desc2; 12679 12680 mtx_assert(&lun->lun_lock, MA_OWNED); 12681 12682 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12683 ctl_lun_error_pattern pattern; 12684 /* 12685 * Check to see whether this particular command matches 12686 * the pattern in the descriptor. 12687 */ 12688 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12689 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12690 continue; 12691 12692 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12693 case CTL_LUN_INJ_ABORTED: 12694 ctl_set_aborted(&io->scsiio); 12695 break; 12696 case CTL_LUN_INJ_MEDIUM_ERR: 12697 ctl_set_medium_error(&io->scsiio); 12698 break; 12699 case CTL_LUN_INJ_UA: 12700 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12701 * OCCURRED */ 12702 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12703 break; 12704 case CTL_LUN_INJ_CUSTOM: 12705 /* 12706 * We're assuming the user knows what he is doing. 12707 * Just copy the sense information without doing 12708 * checks. 12709 */ 12710 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12711 ctl_min(sizeof(desc->custom_sense), 12712 sizeof(io->scsiio.sense_data))); 12713 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12714 io->scsiio.sense_len = SSD_FULL_SIZE; 12715 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12716 break; 12717 case CTL_LUN_INJ_NONE: 12718 default: 12719 /* 12720 * If this is an error injection type we don't know 12721 * about, clear the continuous flag (if it is set) 12722 * so it will get deleted below. 12723 */ 12724 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12725 break; 12726 } 12727 /* 12728 * By default, each error injection action is a one-shot 12729 */ 12730 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12731 continue; 12732 12733 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12734 12735 free(desc, M_CTL); 12736 } 12737} 12738 12739#ifdef CTL_IO_DELAY 12740static void 12741ctl_datamove_timer_wakeup(void *arg) 12742{ 12743 union ctl_io *io; 12744 12745 io = (union ctl_io *)arg; 12746 12747 ctl_datamove(io); 12748} 12749#endif /* CTL_IO_DELAY */ 12750 12751void 12752ctl_datamove(union ctl_io *io) 12753{ 12754 void (*fe_datamove)(union ctl_io *io); 12755 12756 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12757 12758 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12759 12760#ifdef CTL_TIME_IO 12761 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12762 char str[256]; 12763 char path_str[64]; 12764 struct sbuf sb; 12765 12766 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12767 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12768 12769 sbuf_cat(&sb, path_str); 12770 switch (io->io_hdr.io_type) { 12771 case CTL_IO_SCSI: 12772 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12773 sbuf_printf(&sb, "\n"); 12774 sbuf_cat(&sb, path_str); 12775 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12776 io->scsiio.tag_num, io->scsiio.tag_type); 12777 break; 12778 case CTL_IO_TASK: 12779 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12780 "Tag Type: %d\n", io->taskio.task_action, 12781 io->taskio.tag_num, io->taskio.tag_type); 12782 break; 12783 default: 12784 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12785 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12786 break; 12787 } 12788 sbuf_cat(&sb, path_str); 12789 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12790 (intmax_t)time_uptime - io->io_hdr.start_time); 12791 sbuf_finish(&sb); 12792 printf("%s", sbuf_data(&sb)); 12793 } 12794#endif /* CTL_TIME_IO */ 12795 12796#ifdef CTL_IO_DELAY 12797 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12798 struct ctl_lun *lun; 12799 12800 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12801 12802 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12803 } else { 12804 struct ctl_lun *lun; 12805 12806 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12807 if ((lun != NULL) 12808 && (lun->delay_info.datamove_delay > 0)) { 12809 struct callout *callout; 12810 12811 callout = (struct callout *)&io->io_hdr.timer_bytes; 12812 callout_init(callout, /*mpsafe*/ 1); 12813 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12814 callout_reset(callout, 12815 lun->delay_info.datamove_delay * hz, 12816 ctl_datamove_timer_wakeup, io); 12817 if (lun->delay_info.datamove_type == 12818 CTL_DELAY_TYPE_ONESHOT) 12819 lun->delay_info.datamove_delay = 0; 12820 return; 12821 } 12822 } 12823#endif 12824 12825 /* 12826 * This command has been aborted. Set the port status, so we fail 12827 * the data move. 12828 */ 12829 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12830 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12831 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12832 io->io_hdr.nexus.targ_port, 12833 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12834 io->io_hdr.nexus.targ_lun); 12835 io->io_hdr.port_status = 31337; 12836 /* 12837 * Note that the backend, in this case, will get the 12838 * callback in its context. In other cases it may get 12839 * called in the frontend's interrupt thread context. 12840 */ 12841 io->scsiio.be_move_done(io); 12842 return; 12843 } 12844 12845 /* 12846 * If we're in XFER mode and this I/O is from the other shelf 12847 * controller, we need to send the DMA to the other side to 12848 * actually transfer the data to/from the host. In serialize only 12849 * mode the transfer happens below CTL and ctl_datamove() is only 12850 * called on the machine that originally received the I/O. 12851 */ 12852 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12853 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12854 union ctl_ha_msg msg; 12855 uint32_t sg_entries_sent; 12856 int do_sg_copy; 12857 int i; 12858 12859 memset(&msg, 0, sizeof(msg)); 12860 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12861 msg.hdr.original_sc = io->io_hdr.original_sc; 12862 msg.hdr.serializing_sc = io; 12863 msg.hdr.nexus = io->io_hdr.nexus; 12864 msg.dt.flags = io->io_hdr.flags; 12865 /* 12866 * We convert everything into a S/G list here. We can't 12867 * pass by reference, only by value between controllers. 12868 * So we can't pass a pointer to the S/G list, only as many 12869 * S/G entries as we can fit in here. If it's possible for 12870 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12871 * then we need to break this up into multiple transfers. 12872 */ 12873 if (io->scsiio.kern_sg_entries == 0) { 12874 msg.dt.kern_sg_entries = 1; 12875 /* 12876 * If this is in cached memory, flush the cache 12877 * before we send the DMA request to the other 12878 * controller. We want to do this in either the 12879 * read or the write case. The read case is 12880 * straightforward. In the write case, we want to 12881 * make sure nothing is in the local cache that 12882 * could overwrite the DMAed data. 12883 */ 12884 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12885 /* 12886 * XXX KDM use bus_dmamap_sync() here. 12887 */ 12888 } 12889 12890 /* 12891 * Convert to a physical address if this is a 12892 * virtual address. 12893 */ 12894 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12895 msg.dt.sg_list[0].addr = 12896 io->scsiio.kern_data_ptr; 12897 } else { 12898 /* 12899 * XXX KDM use busdma here! 12900 */ 12901#if 0 12902 msg.dt.sg_list[0].addr = (void *) 12903 vtophys(io->scsiio.kern_data_ptr); 12904#endif 12905 } 12906 12907 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12908 do_sg_copy = 0; 12909 } else { 12910 struct ctl_sg_entry *sgl; 12911 12912 do_sg_copy = 1; 12913 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12914 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12915 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12916 /* 12917 * XXX KDM use bus_dmamap_sync() here. 12918 */ 12919 } 12920 } 12921 12922 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12923 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12924 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12925 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12926 msg.dt.sg_sequence = 0; 12927 12928 /* 12929 * Loop until we've sent all of the S/G entries. On the 12930 * other end, we'll recompose these S/G entries into one 12931 * contiguous list before passing it to the 12932 */ 12933 for (sg_entries_sent = 0; sg_entries_sent < 12934 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12935 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12936 sizeof(msg.dt.sg_list[0])), 12937 msg.dt.kern_sg_entries - sg_entries_sent); 12938 12939 if (do_sg_copy != 0) { 12940 struct ctl_sg_entry *sgl; 12941 int j; 12942 12943 sgl = (struct ctl_sg_entry *) 12944 io->scsiio.kern_data_ptr; 12945 /* 12946 * If this is in cached memory, flush the cache 12947 * before we send the DMA request to the other 12948 * controller. We want to do this in either 12949 * the * read or the write case. The read 12950 * case is straightforward. In the write 12951 * case, we want to make sure nothing is 12952 * in the local cache that could overwrite 12953 * the DMAed data. 12954 */ 12955 12956 for (i = sg_entries_sent, j = 0; 12957 i < msg.dt.cur_sg_entries; i++, j++) { 12958 if ((io->io_hdr.flags & 12959 CTL_FLAG_NO_DATASYNC) == 0) { 12960 /* 12961 * XXX KDM use bus_dmamap_sync() 12962 */ 12963 } 12964 if ((io->io_hdr.flags & 12965 CTL_FLAG_BUS_ADDR) == 0) { 12966 /* 12967 * XXX KDM use busdma. 12968 */ 12969#if 0 12970 msg.dt.sg_list[j].addr =(void *) 12971 vtophys(sgl[i].addr); 12972#endif 12973 } else { 12974 msg.dt.sg_list[j].addr = 12975 sgl[i].addr; 12976 } 12977 msg.dt.sg_list[j].len = sgl[i].len; 12978 } 12979 } 12980 12981 sg_entries_sent += msg.dt.cur_sg_entries; 12982 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12983 msg.dt.sg_last = 1; 12984 else 12985 msg.dt.sg_last = 0; 12986 12987 /* 12988 * XXX KDM drop and reacquire the lock here? 12989 */ 12990 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12991 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12992 /* 12993 * XXX do something here. 12994 */ 12995 } 12996 12997 msg.dt.sent_sg_entries = sg_entries_sent; 12998 } 12999 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13000 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 13001 ctl_failover_io(io, /*have_lock*/ 0); 13002 13003 } else { 13004 13005 /* 13006 * Lookup the fe_datamove() function for this particular 13007 * front end. 13008 */ 13009 fe_datamove = 13010 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13011 13012 fe_datamove(io); 13013 } 13014} 13015 13016static void 13017ctl_send_datamove_done(union ctl_io *io, int have_lock) 13018{ 13019 union ctl_ha_msg msg; 13020 int isc_status; 13021 13022 memset(&msg, 0, sizeof(msg)); 13023 13024 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 13025 msg.hdr.original_sc = io; 13026 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13027 msg.hdr.nexus = io->io_hdr.nexus; 13028 msg.hdr.status = io->io_hdr.status; 13029 msg.scsi.tag_num = io->scsiio.tag_num; 13030 msg.scsi.tag_type = io->scsiio.tag_type; 13031 msg.scsi.scsi_status = io->scsiio.scsi_status; 13032 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13033 sizeof(io->scsiio.sense_data)); 13034 msg.scsi.sense_len = io->scsiio.sense_len; 13035 msg.scsi.sense_residual = io->scsiio.sense_residual; 13036 msg.scsi.fetd_status = io->io_hdr.port_status; 13037 msg.scsi.residual = io->scsiio.residual; 13038 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13039 13040 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13041 ctl_failover_io(io, /*have_lock*/ have_lock); 13042 return; 13043 } 13044 13045 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 13046 if (isc_status > CTL_HA_STATUS_SUCCESS) { 13047 /* XXX do something if this fails */ 13048 } 13049 13050} 13051 13052/* 13053 * The DMA to the remote side is done, now we need to tell the other side 13054 * we're done so it can continue with its data movement. 13055 */ 13056static void 13057ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 13058{ 13059 union ctl_io *io; 13060 13061 io = rq->context; 13062 13063 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13064 printf("%s: ISC DMA write failed with error %d", __func__, 13065 rq->ret); 13066 ctl_set_internal_failure(&io->scsiio, 13067 /*sks_valid*/ 1, 13068 /*retry_count*/ rq->ret); 13069 } 13070 13071 ctl_dt_req_free(rq); 13072 13073 /* 13074 * In this case, we had to malloc the memory locally. Free it. 13075 */ 13076 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13077 int i; 13078 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13079 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13080 } 13081 /* 13082 * The data is in local and remote memory, so now we need to send 13083 * status (good or back) back to the other side. 13084 */ 13085 ctl_send_datamove_done(io, /*have_lock*/ 0); 13086} 13087 13088/* 13089 * We've moved the data from the host/controller into local memory. Now we 13090 * need to push it over to the remote controller's memory. 13091 */ 13092static int 13093ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13094{ 13095 int retval; 13096 13097 retval = 0; 13098 13099 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13100 ctl_datamove_remote_write_cb); 13101 13102 return (retval); 13103} 13104 13105static void 13106ctl_datamove_remote_write(union ctl_io *io) 13107{ 13108 int retval; 13109 void (*fe_datamove)(union ctl_io *io); 13110 13111 /* 13112 * - Get the data from the host/HBA into local memory. 13113 * - DMA memory from the local controller to the remote controller. 13114 * - Send status back to the remote controller. 13115 */ 13116 13117 retval = ctl_datamove_remote_sgl_setup(io); 13118 if (retval != 0) 13119 return; 13120 13121 /* Switch the pointer over so the FETD knows what to do */ 13122 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13123 13124 /* 13125 * Use a custom move done callback, since we need to send completion 13126 * back to the other controller, not to the backend on this side. 13127 */ 13128 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13129 13130 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13131 13132 fe_datamove(io); 13133 13134 return; 13135 13136} 13137 13138static int 13139ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13140{ 13141#if 0 13142 char str[256]; 13143 char path_str[64]; 13144 struct sbuf sb; 13145#endif 13146 13147 /* 13148 * In this case, we had to malloc the memory locally. Free it. 13149 */ 13150 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13151 int i; 13152 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13153 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13154 } 13155 13156#if 0 13157 scsi_path_string(io, path_str, sizeof(path_str)); 13158 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13159 sbuf_cat(&sb, path_str); 13160 scsi_command_string(&io->scsiio, NULL, &sb); 13161 sbuf_printf(&sb, "\n"); 13162 sbuf_cat(&sb, path_str); 13163 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13164 io->scsiio.tag_num, io->scsiio.tag_type); 13165 sbuf_cat(&sb, path_str); 13166 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13167 io->io_hdr.flags, io->io_hdr.status); 13168 sbuf_finish(&sb); 13169 printk("%s", sbuf_data(&sb)); 13170#endif 13171 13172 13173 /* 13174 * The read is done, now we need to send status (good or bad) back 13175 * to the other side. 13176 */ 13177 ctl_send_datamove_done(io, /*have_lock*/ 0); 13178 13179 return (0); 13180} 13181 13182static void 13183ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13184{ 13185 union ctl_io *io; 13186 void (*fe_datamove)(union ctl_io *io); 13187 13188 io = rq->context; 13189 13190 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13191 printf("%s: ISC DMA read failed with error %d", __func__, 13192 rq->ret); 13193 ctl_set_internal_failure(&io->scsiio, 13194 /*sks_valid*/ 1, 13195 /*retry_count*/ rq->ret); 13196 } 13197 13198 ctl_dt_req_free(rq); 13199 13200 /* Switch the pointer over so the FETD knows what to do */ 13201 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13202 13203 /* 13204 * Use a custom move done callback, since we need to send completion 13205 * back to the other controller, not to the backend on this side. 13206 */ 13207 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13208 13209 /* XXX KDM add checks like the ones in ctl_datamove? */ 13210 13211 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13212 13213 fe_datamove(io); 13214} 13215 13216static int 13217ctl_datamove_remote_sgl_setup(union ctl_io *io) 13218{ 13219 struct ctl_sg_entry *local_sglist, *remote_sglist; 13220 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13221 struct ctl_softc *softc; 13222 int retval; 13223 int i; 13224 13225 retval = 0; 13226 softc = control_softc; 13227 13228 local_sglist = io->io_hdr.local_sglist; 13229 local_dma_sglist = io->io_hdr.local_dma_sglist; 13230 remote_sglist = io->io_hdr.remote_sglist; 13231 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13232 13233 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13234 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13235 local_sglist[i].len = remote_sglist[i].len; 13236 13237 /* 13238 * XXX Detect the situation where the RS-level I/O 13239 * redirector on the other side has already read the 13240 * data off of the AOR RS on this side, and 13241 * transferred it to remote (mirror) memory on the 13242 * other side. Since we already have the data in 13243 * memory here, we just need to use it. 13244 * 13245 * XXX KDM this can probably be removed once we 13246 * get the cache device code in and take the 13247 * current AOR implementation out. 13248 */ 13249#ifdef NEEDTOPORT 13250 if ((remote_sglist[i].addr >= 13251 (void *)vtophys(softc->mirr->addr)) 13252 && (remote_sglist[i].addr < 13253 ((void *)vtophys(softc->mirr->addr) + 13254 CacheMirrorOffset))) { 13255 local_sglist[i].addr = remote_sglist[i].addr - 13256 CacheMirrorOffset; 13257 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13258 CTL_FLAG_DATA_IN) 13259 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13260 } else { 13261 local_sglist[i].addr = remote_sglist[i].addr + 13262 CacheMirrorOffset; 13263 } 13264#endif 13265#if 0 13266 printf("%s: local %p, remote %p, len %d\n", 13267 __func__, local_sglist[i].addr, 13268 remote_sglist[i].addr, local_sglist[i].len); 13269#endif 13270 } 13271 } else { 13272 uint32_t len_to_go; 13273 13274 /* 13275 * In this case, we don't have automatically allocated 13276 * memory for this I/O on this controller. This typically 13277 * happens with internal CTL I/O -- e.g. inquiry, mode 13278 * sense, etc. Anything coming from RAIDCore will have 13279 * a mirror area available. 13280 */ 13281 len_to_go = io->scsiio.kern_data_len; 13282 13283 /* 13284 * Clear the no datasync flag, we have to use malloced 13285 * buffers. 13286 */ 13287 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13288 13289 /* 13290 * The difficult thing here is that the size of the various 13291 * S/G segments may be different than the size from the 13292 * remote controller. That'll make it harder when DMAing 13293 * the data back to the other side. 13294 */ 13295 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13296 sizeof(io->io_hdr.remote_sglist[0])) && 13297 (len_to_go > 0); i++) { 13298 local_sglist[i].len = ctl_min(len_to_go, 131072); 13299 CTL_SIZE_8B(local_dma_sglist[i].len, 13300 local_sglist[i].len); 13301 local_sglist[i].addr = 13302 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13303 13304 local_dma_sglist[i].addr = local_sglist[i].addr; 13305 13306 if (local_sglist[i].addr == NULL) { 13307 int j; 13308 13309 printf("malloc failed for %zd bytes!", 13310 local_dma_sglist[i].len); 13311 for (j = 0; j < i; j++) { 13312 free(local_sglist[j].addr, M_CTL); 13313 } 13314 ctl_set_internal_failure(&io->scsiio, 13315 /*sks_valid*/ 1, 13316 /*retry_count*/ 4857); 13317 retval = 1; 13318 goto bailout_error; 13319 13320 } 13321 /* XXX KDM do we need a sync here? */ 13322 13323 len_to_go -= local_sglist[i].len; 13324 } 13325 /* 13326 * Reset the number of S/G entries accordingly. The 13327 * original number of S/G entries is available in 13328 * rem_sg_entries. 13329 */ 13330 io->scsiio.kern_sg_entries = i; 13331 13332#if 0 13333 printf("%s: kern_sg_entries = %d\n", __func__, 13334 io->scsiio.kern_sg_entries); 13335 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13336 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13337 local_sglist[i].addr, local_sglist[i].len, 13338 local_dma_sglist[i].len); 13339#endif 13340 } 13341 13342 13343 return (retval); 13344 13345bailout_error: 13346 13347 ctl_send_datamove_done(io, /*have_lock*/ 0); 13348 13349 return (retval); 13350} 13351 13352static int 13353ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13354 ctl_ha_dt_cb callback) 13355{ 13356 struct ctl_ha_dt_req *rq; 13357 struct ctl_sg_entry *remote_sglist, *local_sglist; 13358 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13359 uint32_t local_used, remote_used, total_used; 13360 int retval; 13361 int i, j; 13362 13363 retval = 0; 13364 13365 rq = ctl_dt_req_alloc(); 13366 13367 /* 13368 * If we failed to allocate the request, and if the DMA didn't fail 13369 * anyway, set busy status. This is just a resource allocation 13370 * failure. 13371 */ 13372 if ((rq == NULL) 13373 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13374 ctl_set_busy(&io->scsiio); 13375 13376 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13377 13378 if (rq != NULL) 13379 ctl_dt_req_free(rq); 13380 13381 /* 13382 * The data move failed. We need to return status back 13383 * to the other controller. No point in trying to DMA 13384 * data to the remote controller. 13385 */ 13386 13387 ctl_send_datamove_done(io, /*have_lock*/ 0); 13388 13389 retval = 1; 13390 13391 goto bailout; 13392 } 13393 13394 local_sglist = io->io_hdr.local_sglist; 13395 local_dma_sglist = io->io_hdr.local_dma_sglist; 13396 remote_sglist = io->io_hdr.remote_sglist; 13397 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13398 local_used = 0; 13399 remote_used = 0; 13400 total_used = 0; 13401 13402 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13403 rq->ret = CTL_HA_STATUS_SUCCESS; 13404 rq->context = io; 13405 callback(rq); 13406 goto bailout; 13407 } 13408 13409 /* 13410 * Pull/push the data over the wire from/to the other controller. 13411 * This takes into account the possibility that the local and 13412 * remote sglists may not be identical in terms of the size of 13413 * the elements and the number of elements. 13414 * 13415 * One fundamental assumption here is that the length allocated for 13416 * both the local and remote sglists is identical. Otherwise, we've 13417 * essentially got a coding error of some sort. 13418 */ 13419 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13420 int isc_ret; 13421 uint32_t cur_len, dma_length; 13422 uint8_t *tmp_ptr; 13423 13424 rq->id = CTL_HA_DATA_CTL; 13425 rq->command = command; 13426 rq->context = io; 13427 13428 /* 13429 * Both pointers should be aligned. But it is possible 13430 * that the allocation length is not. They should both 13431 * also have enough slack left over at the end, though, 13432 * to round up to the next 8 byte boundary. 13433 */ 13434 cur_len = ctl_min(local_sglist[i].len - local_used, 13435 remote_sglist[j].len - remote_used); 13436 13437 /* 13438 * In this case, we have a size issue and need to decrease 13439 * the size, except in the case where we actually have less 13440 * than 8 bytes left. In that case, we need to increase 13441 * the DMA length to get the last bit. 13442 */ 13443 if ((cur_len & 0x7) != 0) { 13444 if (cur_len > 0x7) { 13445 cur_len = cur_len - (cur_len & 0x7); 13446 dma_length = cur_len; 13447 } else { 13448 CTL_SIZE_8B(dma_length, cur_len); 13449 } 13450 13451 } else 13452 dma_length = cur_len; 13453 13454 /* 13455 * If we had to allocate memory for this I/O, instead of using 13456 * the non-cached mirror memory, we'll need to flush the cache 13457 * before trying to DMA to the other controller. 13458 * 13459 * We could end up doing this multiple times for the same 13460 * segment if we have a larger local segment than remote 13461 * segment. That shouldn't be an issue. 13462 */ 13463 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13464 /* 13465 * XXX KDM use bus_dmamap_sync() here. 13466 */ 13467 } 13468 13469 rq->size = dma_length; 13470 13471 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13472 tmp_ptr += local_used; 13473 13474 /* Use physical addresses when talking to ISC hardware */ 13475 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13476 /* XXX KDM use busdma */ 13477#if 0 13478 rq->local = vtophys(tmp_ptr); 13479#endif 13480 } else 13481 rq->local = tmp_ptr; 13482 13483 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13484 tmp_ptr += remote_used; 13485 rq->remote = tmp_ptr; 13486 13487 rq->callback = NULL; 13488 13489 local_used += cur_len; 13490 if (local_used >= local_sglist[i].len) { 13491 i++; 13492 local_used = 0; 13493 } 13494 13495 remote_used += cur_len; 13496 if (remote_used >= remote_sglist[j].len) { 13497 j++; 13498 remote_used = 0; 13499 } 13500 total_used += cur_len; 13501 13502 if (total_used >= io->scsiio.kern_data_len) 13503 rq->callback = callback; 13504 13505 if ((rq->size & 0x7) != 0) { 13506 printf("%s: warning: size %d is not on 8b boundary\n", 13507 __func__, rq->size); 13508 } 13509 if (((uintptr_t)rq->local & 0x7) != 0) { 13510 printf("%s: warning: local %p not on 8b boundary\n", 13511 __func__, rq->local); 13512 } 13513 if (((uintptr_t)rq->remote & 0x7) != 0) { 13514 printf("%s: warning: remote %p not on 8b boundary\n", 13515 __func__, rq->local); 13516 } 13517#if 0 13518 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13519 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13520 rq->local, rq->remote, rq->size); 13521#endif 13522 13523 isc_ret = ctl_dt_single(rq); 13524 if (isc_ret == CTL_HA_STATUS_WAIT) 13525 continue; 13526 13527 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13528 rq->ret = CTL_HA_STATUS_SUCCESS; 13529 } else { 13530 rq->ret = isc_ret; 13531 } 13532 callback(rq); 13533 goto bailout; 13534 } 13535 13536bailout: 13537 return (retval); 13538 13539} 13540 13541static void 13542ctl_datamove_remote_read(union ctl_io *io) 13543{ 13544 int retval; 13545 int i; 13546 13547 /* 13548 * This will send an error to the other controller in the case of a 13549 * failure. 13550 */ 13551 retval = ctl_datamove_remote_sgl_setup(io); 13552 if (retval != 0) 13553 return; 13554 13555 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13556 ctl_datamove_remote_read_cb); 13557 if ((retval != 0) 13558 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13559 /* 13560 * Make sure we free memory if there was an error.. The 13561 * ctl_datamove_remote_xfer() function will send the 13562 * datamove done message, or call the callback with an 13563 * error if there is a problem. 13564 */ 13565 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13566 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13567 } 13568 13569 return; 13570} 13571 13572/* 13573 * Process a datamove request from the other controller. This is used for 13574 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13575 * first. Once that is complete, the data gets DMAed into the remote 13576 * controller's memory. For reads, we DMA from the remote controller's 13577 * memory into our memory first, and then move it out to the FETD. 13578 */ 13579static void 13580ctl_datamove_remote(union ctl_io *io) 13581{ 13582 struct ctl_softc *softc; 13583 13584 softc = control_softc; 13585 13586 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13587 13588 /* 13589 * Note that we look for an aborted I/O here, but don't do some of 13590 * the other checks that ctl_datamove() normally does. 13591 * We don't need to run the datamove delay code, since that should 13592 * have been done if need be on the other controller. 13593 */ 13594 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13595 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13596 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13597 io->io_hdr.nexus.targ_port, 13598 io->io_hdr.nexus.targ_target.id, 13599 io->io_hdr.nexus.targ_lun); 13600 io->io_hdr.port_status = 31338; 13601 ctl_send_datamove_done(io, /*have_lock*/ 0); 13602 return; 13603 } 13604 13605 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13606 ctl_datamove_remote_write(io); 13607 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13608 ctl_datamove_remote_read(io); 13609 } else { 13610 union ctl_ha_msg msg; 13611 struct scsi_sense_data *sense; 13612 uint8_t sks[3]; 13613 int retry_count; 13614 13615 memset(&msg, 0, sizeof(msg)); 13616 13617 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13618 msg.hdr.status = CTL_SCSI_ERROR; 13619 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13620 13621 retry_count = 4243; 13622 13623 sense = &msg.scsi.sense_data; 13624 sks[0] = SSD_SCS_VALID; 13625 sks[1] = (retry_count >> 8) & 0xff; 13626 sks[2] = retry_count & 0xff; 13627 13628 /* "Internal target failure" */ 13629 scsi_set_sense_data(sense, 13630 /*sense_format*/ SSD_TYPE_NONE, 13631 /*current_error*/ 1, 13632 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13633 /*asc*/ 0x44, 13634 /*ascq*/ 0x00, 13635 /*type*/ SSD_ELEM_SKS, 13636 /*size*/ sizeof(sks), 13637 /*data*/ sks, 13638 SSD_ELEM_NONE); 13639 13640 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13641 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13642 ctl_failover_io(io, /*have_lock*/ 1); 13643 return; 13644 } 13645 13646 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13647 CTL_HA_STATUS_SUCCESS) { 13648 /* XXX KDM what to do if this fails? */ 13649 } 13650 return; 13651 } 13652 13653} 13654 13655static int 13656ctl_process_done(union ctl_io *io) 13657{ 13658 struct ctl_lun *lun; 13659 struct ctl_softc *ctl_softc; 13660 void (*fe_done)(union ctl_io *io); 13661 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13662 13663 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13664 13665 fe_done = 13666 control_softc->ctl_ports[targ_port]->fe_done; 13667 13668#ifdef CTL_TIME_IO 13669 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13670 char str[256]; 13671 char path_str[64]; 13672 struct sbuf sb; 13673 13674 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13675 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13676 13677 sbuf_cat(&sb, path_str); 13678 switch (io->io_hdr.io_type) { 13679 case CTL_IO_SCSI: 13680 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13681 sbuf_printf(&sb, "\n"); 13682 sbuf_cat(&sb, path_str); 13683 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13684 io->scsiio.tag_num, io->scsiio.tag_type); 13685 break; 13686 case CTL_IO_TASK: 13687 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13688 "Tag Type: %d\n", io->taskio.task_action, 13689 io->taskio.tag_num, io->taskio.tag_type); 13690 break; 13691 default: 13692 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13693 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13694 break; 13695 } 13696 sbuf_cat(&sb, path_str); 13697 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13698 (intmax_t)time_uptime - io->io_hdr.start_time); 13699 sbuf_finish(&sb); 13700 printf("%s", sbuf_data(&sb)); 13701 } 13702#endif /* CTL_TIME_IO */ 13703 13704 switch (io->io_hdr.io_type) { 13705 case CTL_IO_SCSI: 13706 break; 13707 case CTL_IO_TASK: 13708 if (bootverbose || verbose > 0) 13709 ctl_io_error_print(io, NULL); 13710 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13711 ctl_free_io(io); 13712 else 13713 fe_done(io); 13714 return (CTL_RETVAL_COMPLETE); 13715 break; 13716 default: 13717 printf("ctl_process_done: invalid io type %d\n", 13718 io->io_hdr.io_type); 13719 panic("ctl_process_done: invalid io type %d\n", 13720 io->io_hdr.io_type); 13721 break; /* NOTREACHED */ 13722 } 13723 13724 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13725 if (lun == NULL) { 13726 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13727 io->io_hdr.nexus.targ_mapped_lun)); 13728 fe_done(io); 13729 goto bailout; 13730 } 13731 ctl_softc = lun->ctl_softc; 13732 13733 mtx_lock(&lun->lun_lock); 13734 13735 /* 13736 * Check to see if we have any errors to inject here. We only 13737 * inject errors for commands that don't already have errors set. 13738 */ 13739 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13740 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13741 ctl_inject_error(lun, io); 13742 13743 /* 13744 * XXX KDM how do we treat commands that aren't completed 13745 * successfully? 13746 * 13747 * XXX KDM should we also track I/O latency? 13748 */ 13749 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13750 io->io_hdr.io_type == CTL_IO_SCSI) { 13751#ifdef CTL_TIME_IO 13752 struct bintime cur_bt; 13753#endif 13754 int type; 13755 13756 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13757 CTL_FLAG_DATA_IN) 13758 type = CTL_STATS_READ; 13759 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13760 CTL_FLAG_DATA_OUT) 13761 type = CTL_STATS_WRITE; 13762 else 13763 type = CTL_STATS_NO_IO; 13764 13765 lun->stats.ports[targ_port].bytes[type] += 13766 io->scsiio.kern_total_len; 13767 lun->stats.ports[targ_port].operations[type]++; 13768#ifdef CTL_TIME_IO 13769 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13770 &io->io_hdr.dma_bt); 13771 lun->stats.ports[targ_port].num_dmas[type] += 13772 io->io_hdr.num_dmas; 13773 getbintime(&cur_bt); 13774 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13775 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13776#endif 13777 } 13778 13779 /* 13780 * Remove this from the OOA queue. 13781 */ 13782 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13783 13784 /* 13785 * Run through the blocked queue on this LUN and see if anything 13786 * has become unblocked, now that this transaction is done. 13787 */ 13788 ctl_check_blocked(lun); 13789 13790 /* 13791 * If the LUN has been invalidated, free it if there is nothing 13792 * left on its OOA queue. 13793 */ 13794 if ((lun->flags & CTL_LUN_INVALID) 13795 && TAILQ_EMPTY(&lun->ooa_queue)) { 13796 mtx_unlock(&lun->lun_lock); 13797 mtx_lock(&ctl_softc->ctl_lock); 13798 ctl_free_lun(lun); 13799 mtx_unlock(&ctl_softc->ctl_lock); 13800 } else 13801 mtx_unlock(&lun->lun_lock); 13802 13803 /* 13804 * If this command has been aborted, make sure we set the status 13805 * properly. The FETD is responsible for freeing the I/O and doing 13806 * whatever it needs to do to clean up its state. 13807 */ 13808 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13809 ctl_set_task_aborted(&io->scsiio); 13810 13811 /* 13812 * We print out status for every task management command. For SCSI 13813 * commands, we filter out any unit attention errors; they happen 13814 * on every boot, and would clutter up the log. Note: task 13815 * management commands aren't printed here, they are printed above, 13816 * since they should never even make it down here. 13817 */ 13818 switch (io->io_hdr.io_type) { 13819 case CTL_IO_SCSI: { 13820 int error_code, sense_key, asc, ascq; 13821 13822 sense_key = 0; 13823 13824 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13825 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13826 /* 13827 * Since this is just for printing, no need to 13828 * show errors here. 13829 */ 13830 scsi_extract_sense_len(&io->scsiio.sense_data, 13831 io->scsiio.sense_len, 13832 &error_code, 13833 &sense_key, 13834 &asc, 13835 &ascq, 13836 /*show_errors*/ 0); 13837 } 13838 13839 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13840 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13841 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13842 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13843 13844 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13845 ctl_softc->skipped_prints++; 13846 } else { 13847 uint32_t skipped_prints; 13848 13849 skipped_prints = ctl_softc->skipped_prints; 13850 13851 ctl_softc->skipped_prints = 0; 13852 ctl_softc->last_print_jiffies = time_uptime; 13853 13854 if (skipped_prints > 0) { 13855#ifdef NEEDTOPORT 13856 csevent_log(CSC_CTL | CSC_SHELF_SW | 13857 CTL_ERROR_REPORT, 13858 csevent_LogType_Trace, 13859 csevent_Severity_Information, 13860 csevent_AlertLevel_Green, 13861 csevent_FRU_Firmware, 13862 csevent_FRU_Unknown, 13863 "High CTL error volume, %d prints " 13864 "skipped", skipped_prints); 13865#endif 13866 } 13867 if (bootverbose || verbose > 0) 13868 ctl_io_error_print(io, NULL); 13869 } 13870 } 13871 break; 13872 } 13873 case CTL_IO_TASK: 13874 if (bootverbose || verbose > 0) 13875 ctl_io_error_print(io, NULL); 13876 break; 13877 default: 13878 break; 13879 } 13880 13881 /* 13882 * Tell the FETD or the other shelf controller we're done with this 13883 * command. Note that only SCSI commands get to this point. Task 13884 * management commands are completed above. 13885 * 13886 * We only send status to the other controller if we're in XFER 13887 * mode. In SER_ONLY mode, the I/O is done on the controller that 13888 * received the I/O (from CTL's perspective), and so the status is 13889 * generated there. 13890 * 13891 * XXX KDM if we hold the lock here, we could cause a deadlock 13892 * if the frontend comes back in in this context to queue 13893 * something. 13894 */ 13895 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13896 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13897 union ctl_ha_msg msg; 13898 13899 memset(&msg, 0, sizeof(msg)); 13900 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13901 msg.hdr.original_sc = io->io_hdr.original_sc; 13902 msg.hdr.nexus = io->io_hdr.nexus; 13903 msg.hdr.status = io->io_hdr.status; 13904 msg.scsi.scsi_status = io->scsiio.scsi_status; 13905 msg.scsi.tag_num = io->scsiio.tag_num; 13906 msg.scsi.tag_type = io->scsiio.tag_type; 13907 msg.scsi.sense_len = io->scsiio.sense_len; 13908 msg.scsi.sense_residual = io->scsiio.sense_residual; 13909 msg.scsi.residual = io->scsiio.residual; 13910 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13911 sizeof(io->scsiio.sense_data)); 13912 /* 13913 * We copy this whether or not this is an I/O-related 13914 * command. Otherwise, we'd have to go and check to see 13915 * whether it's a read/write command, and it really isn't 13916 * worth it. 13917 */ 13918 memcpy(&msg.scsi.lbalen, 13919 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13920 sizeof(msg.scsi.lbalen)); 13921 13922 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13923 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13924 /* XXX do something here */ 13925 } 13926 13927 ctl_free_io(io); 13928 } else 13929 fe_done(io); 13930 13931bailout: 13932 13933 return (CTL_RETVAL_COMPLETE); 13934} 13935 13936#ifdef CTL_WITH_CA 13937/* 13938 * Front end should call this if it doesn't do autosense. When the request 13939 * sense comes back in from the initiator, we'll dequeue this and send it. 13940 */ 13941int 13942ctl_queue_sense(union ctl_io *io) 13943{ 13944 struct ctl_lun *lun; 13945 struct ctl_softc *ctl_softc; 13946 uint32_t initidx, targ_lun; 13947 13948 ctl_softc = control_softc; 13949 13950 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13951 13952 /* 13953 * LUN lookup will likely move to the ctl_work_thread() once we 13954 * have our new queueing infrastructure (that doesn't put things on 13955 * a per-LUN queue initially). That is so that we can handle 13956 * things like an INQUIRY to a LUN that we don't have enabled. We 13957 * can't deal with that right now. 13958 */ 13959 mtx_lock(&ctl_softc->ctl_lock); 13960 13961 /* 13962 * If we don't have a LUN for this, just toss the sense 13963 * information. 13964 */ 13965 targ_lun = io->io_hdr.nexus.targ_lun; 13966 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13967 if ((targ_lun < CTL_MAX_LUNS) 13968 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13969 lun = ctl_softc->ctl_luns[targ_lun]; 13970 else 13971 goto bailout; 13972 13973 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13974 13975 mtx_lock(&lun->lun_lock); 13976 /* 13977 * Already have CA set for this LUN...toss the sense information. 13978 */ 13979 if (ctl_is_set(lun->have_ca, initidx)) { 13980 mtx_unlock(&lun->lun_lock); 13981 goto bailout; 13982 } 13983 13984 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13985 ctl_min(sizeof(lun->pending_sense[initidx]), 13986 sizeof(io->scsiio.sense_data))); 13987 ctl_set_mask(lun->have_ca, initidx); 13988 mtx_unlock(&lun->lun_lock); 13989 13990bailout: 13991 mtx_unlock(&ctl_softc->ctl_lock); 13992 13993 ctl_free_io(io); 13994 13995 return (CTL_RETVAL_COMPLETE); 13996} 13997#endif 13998 13999/* 14000 * Primary command inlet from frontend ports. All SCSI and task I/O 14001 * requests must go through this function. 14002 */ 14003int 14004ctl_queue(union ctl_io *io) 14005{ 14006 struct ctl_softc *ctl_softc; 14007 14008 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 14009 14010 ctl_softc = control_softc; 14011 14012#ifdef CTL_TIME_IO 14013 io->io_hdr.start_time = time_uptime; 14014 getbintime(&io->io_hdr.start_bt); 14015#endif /* CTL_TIME_IO */ 14016 14017 /* Map FE-specific LUN ID into global one. */ 14018 io->io_hdr.nexus.targ_mapped_lun = 14019 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 14020 14021 switch (io->io_hdr.io_type) { 14022 case CTL_IO_SCSI: 14023 case CTL_IO_TASK: 14024 ctl_enqueue_incoming(io); 14025 break; 14026 default: 14027 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 14028 return (EINVAL); 14029 } 14030 14031 return (CTL_RETVAL_COMPLETE); 14032} 14033 14034#ifdef CTL_IO_DELAY 14035static void 14036ctl_done_timer_wakeup(void *arg) 14037{ 14038 union ctl_io *io; 14039 14040 io = (union ctl_io *)arg; 14041 ctl_done(io); 14042} 14043#endif /* CTL_IO_DELAY */ 14044 14045void 14046ctl_done(union ctl_io *io) 14047{ 14048 struct ctl_softc *ctl_softc; 14049 14050 ctl_softc = control_softc; 14051 14052 /* 14053 * Enable this to catch duplicate completion issues. 14054 */ 14055#if 0 14056 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 14057 printf("%s: type %d msg %d cdb %x iptl: " 14058 "%d:%d:%d:%d tag 0x%04x " 14059 "flag %#x status %x\n", 14060 __func__, 14061 io->io_hdr.io_type, 14062 io->io_hdr.msg_type, 14063 io->scsiio.cdb[0], 14064 io->io_hdr.nexus.initid.id, 14065 io->io_hdr.nexus.targ_port, 14066 io->io_hdr.nexus.targ_target.id, 14067 io->io_hdr.nexus.targ_lun, 14068 (io->io_hdr.io_type == 14069 CTL_IO_TASK) ? 14070 io->taskio.tag_num : 14071 io->scsiio.tag_num, 14072 io->io_hdr.flags, 14073 io->io_hdr.status); 14074 } else 14075 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 14076#endif 14077 14078 /* 14079 * This is an internal copy of an I/O, and should not go through 14080 * the normal done processing logic. 14081 */ 14082 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 14083 return; 14084 14085 /* 14086 * We need to send a msg to the serializing shelf to finish the IO 14087 * as well. We don't send a finish message to the other shelf if 14088 * this is a task management command. Task management commands 14089 * aren't serialized in the OOA queue, but rather just executed on 14090 * both shelf controllers for commands that originated on that 14091 * controller. 14092 */ 14093 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 14094 && (io->io_hdr.io_type != CTL_IO_TASK)) { 14095 union ctl_ha_msg msg_io; 14096 14097 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 14098 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 14099 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 14100 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 14101 } 14102 /* continue on to finish IO */ 14103 } 14104#ifdef CTL_IO_DELAY 14105 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 14106 struct ctl_lun *lun; 14107 14108 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14109 14110 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14111 } else { 14112 struct ctl_lun *lun; 14113 14114 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14115 14116 if ((lun != NULL) 14117 && (lun->delay_info.done_delay > 0)) { 14118 struct callout *callout; 14119 14120 callout = (struct callout *)&io->io_hdr.timer_bytes; 14121 callout_init(callout, /*mpsafe*/ 1); 14122 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14123 callout_reset(callout, 14124 lun->delay_info.done_delay * hz, 14125 ctl_done_timer_wakeup, io); 14126 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14127 lun->delay_info.done_delay = 0; 14128 return; 14129 } 14130 } 14131#endif /* CTL_IO_DELAY */ 14132 14133 ctl_enqueue_done(io); 14134} 14135 14136int 14137ctl_isc(struct ctl_scsiio *ctsio) 14138{ 14139 struct ctl_lun *lun; 14140 int retval; 14141 14142 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14143 14144 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14145 14146 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14147 14148 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14149 14150 return (retval); 14151} 14152 14153 14154static void 14155ctl_work_thread(void *arg) 14156{ 14157 struct ctl_thread *thr = (struct ctl_thread *)arg; 14158 struct ctl_softc *softc = thr->ctl_softc; 14159 union ctl_io *io; 14160 int retval; 14161 14162 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14163 14164 for (;;) { 14165 retval = 0; 14166 14167 /* 14168 * We handle the queues in this order: 14169 * - ISC 14170 * - done queue (to free up resources, unblock other commands) 14171 * - RtR queue 14172 * - incoming queue 14173 * 14174 * If those queues are empty, we break out of the loop and 14175 * go to sleep. 14176 */ 14177 mtx_lock(&thr->queue_lock); 14178 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14179 if (io != NULL) { 14180 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14181 mtx_unlock(&thr->queue_lock); 14182 ctl_handle_isc(io); 14183 continue; 14184 } 14185 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14186 if (io != NULL) { 14187 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14188 /* clear any blocked commands, call fe_done */ 14189 mtx_unlock(&thr->queue_lock); 14190 retval = ctl_process_done(io); 14191 continue; 14192 } 14193 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14194 if (io != NULL) { 14195 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14196 mtx_unlock(&thr->queue_lock); 14197 if (io->io_hdr.io_type == CTL_IO_TASK) 14198 ctl_run_task(io); 14199 else 14200 ctl_scsiio_precheck(softc, &io->scsiio); 14201 continue; 14202 } 14203 if (!ctl_pause_rtr) { 14204 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14205 if (io != NULL) { 14206 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14207 mtx_unlock(&thr->queue_lock); 14208 retval = ctl_scsiio(&io->scsiio); 14209 if (retval != CTL_RETVAL_COMPLETE) 14210 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14211 continue; 14212 } 14213 } 14214 14215 /* Sleep until we have something to do. */ 14216 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14217 } 14218} 14219 14220static void 14221ctl_lun_thread(void *arg) 14222{ 14223 struct ctl_softc *softc = (struct ctl_softc *)arg; 14224 struct ctl_be_lun *be_lun; 14225 int retval; 14226 14227 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14228 14229 for (;;) { 14230 retval = 0; 14231 mtx_lock(&softc->ctl_lock); 14232 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14233 if (be_lun != NULL) { 14234 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14235 mtx_unlock(&softc->ctl_lock); 14236 ctl_create_lun(be_lun); 14237 continue; 14238 } 14239 14240 /* Sleep until we have something to do. */ 14241 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14242 PDROP | PRIBIO, "-", 0); 14243 } 14244} 14245 14246static void 14247ctl_enqueue_incoming(union ctl_io *io) 14248{ 14249 struct ctl_softc *softc = control_softc; 14250 struct ctl_thread *thr; 14251 u_int idx; 14252 14253 idx = (io->io_hdr.nexus.targ_port * 127 + 14254 io->io_hdr.nexus.initid.id) % worker_threads; 14255 thr = &softc->threads[idx]; 14256 mtx_lock(&thr->queue_lock); 14257 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14258 mtx_unlock(&thr->queue_lock); 14259 wakeup(thr); 14260} 14261 14262static void 14263ctl_enqueue_rtr(union ctl_io *io) 14264{ 14265 struct ctl_softc *softc = control_softc; 14266 struct ctl_thread *thr; 14267 14268 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14269 mtx_lock(&thr->queue_lock); 14270 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14271 mtx_unlock(&thr->queue_lock); 14272 wakeup(thr); 14273} 14274 14275static void 14276ctl_enqueue_done(union ctl_io *io) 14277{ 14278 struct ctl_softc *softc = control_softc; 14279 struct ctl_thread *thr; 14280 14281 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14282 mtx_lock(&thr->queue_lock); 14283 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14284 mtx_unlock(&thr->queue_lock); 14285 wakeup(thr); 14286} 14287 14288static void 14289ctl_enqueue_isc(union ctl_io *io) 14290{ 14291 struct ctl_softc *softc = control_softc; 14292 struct ctl_thread *thr; 14293 14294 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14295 mtx_lock(&thr->queue_lock); 14296 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14297 mtx_unlock(&thr->queue_lock); 14298 wakeup(thr); 14299} 14300 14301/* Initialization and failover */ 14302 14303void 14304ctl_init_isc_msg(void) 14305{ 14306 printf("CTL: Still calling this thing\n"); 14307} 14308 14309/* 14310 * Init component 14311 * Initializes component into configuration defined by bootMode 14312 * (see hasc-sv.c) 14313 * returns hasc_Status: 14314 * OK 14315 * ERROR - fatal error 14316 */ 14317static ctl_ha_comp_status 14318ctl_isc_init(struct ctl_ha_component *c) 14319{ 14320 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14321 14322 c->status = ret; 14323 return ret; 14324} 14325 14326/* Start component 14327 * Starts component in state requested. If component starts successfully, 14328 * it must set its own state to the requestrd state 14329 * When requested state is HASC_STATE_HA, the component may refine it 14330 * by adding _SLAVE or _MASTER flags. 14331 * Currently allowed state transitions are: 14332 * UNKNOWN->HA - initial startup 14333 * UNKNOWN->SINGLE - initial startup when no parter detected 14334 * HA->SINGLE - failover 14335 * returns ctl_ha_comp_status: 14336 * OK - component successfully started in requested state 14337 * FAILED - could not start the requested state, failover may 14338 * be possible 14339 * ERROR - fatal error detected, no future startup possible 14340 */ 14341static ctl_ha_comp_status 14342ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14343{ 14344 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14345 14346 printf("%s: go\n", __func__); 14347 14348 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14349 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14350 ctl_is_single = 0; 14351 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14352 != CTL_HA_STATUS_SUCCESS) { 14353 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14354 ret = CTL_HA_COMP_STATUS_ERROR; 14355 } 14356 } else if (CTL_HA_STATE_IS_HA(c->state) 14357 && CTL_HA_STATE_IS_SINGLE(state)){ 14358 // HA->SINGLE transition 14359 ctl_failover(); 14360 ctl_is_single = 1; 14361 } else { 14362 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14363 c->state, state); 14364 ret = CTL_HA_COMP_STATUS_ERROR; 14365 } 14366 if (CTL_HA_STATE_IS_SINGLE(state)) 14367 ctl_is_single = 1; 14368 14369 c->state = state; 14370 c->status = ret; 14371 return ret; 14372} 14373 14374/* 14375 * Quiesce component 14376 * The component must clear any error conditions (set status to OK) and 14377 * prepare itself to another Start call 14378 * returns ctl_ha_comp_status: 14379 * OK 14380 * ERROR 14381 */ 14382static ctl_ha_comp_status 14383ctl_isc_quiesce(struct ctl_ha_component *c) 14384{ 14385 int ret = CTL_HA_COMP_STATUS_OK; 14386 14387 ctl_pause_rtr = 1; 14388 c->status = ret; 14389 return ret; 14390} 14391 14392struct ctl_ha_component ctl_ha_component_ctlisc = 14393{ 14394 .name = "CTL ISC", 14395 .state = CTL_HA_STATE_UNKNOWN, 14396 .init = ctl_isc_init, 14397 .start = ctl_isc_start, 14398 .quiesce = ctl_isc_quiesce 14399}; 14400 14401/* 14402 * vim: ts=8 14403 */ 14404