ctl.c revision 272203
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 272203 2014-09-27 08:14:15Z 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*/ 0, 267 /*ret_priority*/ 0, 268 /*disable_pf_transfer_len*/ {0, 0}, 269 /*min_prefetch*/ {0, 0}, 270 /*max_prefetch*/ {0, 0}, 271 /*max_pf_ceiling*/ {0, 0}, 272 /*flags2*/ 0, 273 /*cache_segments*/ 0, 274 /*cache_seg_size*/ {0, 0}, 275 /*reserved*/ 0, 276 /*non_cache_seg_size*/ {0, 0, 0} 277}; 278 279static struct scsi_control_page control_page_default = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE, 281 /*page_length*/sizeof(struct scsi_control_page) - 2, 282 /*rlec*/0, 283 /*queue_flags*/0, 284 /*eca_and_aen*/0, 285 /*flags4*/SCP_TAS, 286 /*aen_holdoff_period*/{0, 0}, 287 /*busy_timeout_period*/{0, 0}, 288 /*extended_selftest_completion_time*/{0, 0} 289}; 290 291static struct scsi_control_page control_page_changeable = { 292 /*page_code*/SMS_CONTROL_MODE_PAGE, 293 /*page_length*/sizeof(struct scsi_control_page) - 2, 294 /*rlec*/SCP_DSENSE, 295 /*queue_flags*/0, 296 /*eca_and_aen*/0, 297 /*flags4*/0, 298 /*aen_holdoff_period*/{0, 0}, 299 /*busy_timeout_period*/{0, 0}, 300 /*extended_selftest_completion_time*/{0, 0} 301}; 302 303 304/* 305 * XXX KDM move these into the softc. 306 */ 307static int rcv_sync_msg; 308static int persis_offset; 309static uint8_t ctl_pause_rtr; 310static int ctl_is_single = 1; 311static int index_to_aps_page; 312 313SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 314static int worker_threads = -1; 315TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 316SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 317 &worker_threads, 1, "Number of worker threads"); 318static int verbose = 0; 319TUNABLE_INT("kern.cam.ctl.verbose", &verbose); 320SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 321 &verbose, 0, "Show SCSI errors returned to initiator"); 322 323/* 324 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 325 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 326 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 327 */ 328#define SCSI_EVPD_NUM_SUPPORTED_PAGES 8 329 330static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 331 int param); 332static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 333static int ctl_init(void); 334void ctl_shutdown(void); 335static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 336static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 337static void ctl_ioctl_online(void *arg); 338static void ctl_ioctl_offline(void *arg); 339static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 340static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 341static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 342static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 343static int ctl_ioctl_submit_wait(union ctl_io *io); 344static void ctl_ioctl_datamove(union ctl_io *io); 345static void ctl_ioctl_done(union ctl_io *io); 346static void ctl_ioctl_hard_startstop_callback(void *arg, 347 struct cfi_metatask *metatask); 348static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 349static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 350 struct ctl_ooa *ooa_hdr, 351 struct ctl_ooa_entry *kern_entries); 352static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 353 struct thread *td); 354static uint32_t ctl_map_lun(int port_num, uint32_t lun); 355static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 356#ifdef unused 357static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 358 uint32_t targ_target, uint32_t targ_lun, 359 int can_wait); 360static void ctl_kfree_io(union ctl_io *io); 361#endif /* unused */ 362static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 363 struct ctl_be_lun *be_lun, struct ctl_id target_id); 364static int ctl_free_lun(struct ctl_lun *lun); 365static void ctl_create_lun(struct ctl_be_lun *be_lun); 366/** 367static void ctl_failover_change_pages(struct ctl_softc *softc, 368 struct ctl_scsiio *ctsio, int master); 369**/ 370 371static int ctl_do_mode_select(union ctl_io *io); 372static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 373 uint64_t res_key, uint64_t sa_res_key, 374 uint8_t type, uint32_t residx, 375 struct ctl_scsiio *ctsio, 376 struct scsi_per_res_out *cdb, 377 struct scsi_per_res_out_parms* param); 378static void ctl_pro_preempt_other(struct ctl_lun *lun, 379 union ctl_ha_msg *msg); 380static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 381static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 382static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 383static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 384static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 385 int alloc_len); 386static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 387 int alloc_len); 388static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 389static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 390static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 391static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 392static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 393static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 394static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 395 union ctl_io *ooa_io); 396static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 397 union ctl_io *starting_io); 398static int ctl_check_blocked(struct ctl_lun *lun); 399static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 400 struct ctl_lun *lun, 401 const struct ctl_cmd_entry *entry, 402 struct ctl_scsiio *ctsio); 403//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 404static void ctl_failover(void); 405static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 406 struct ctl_scsiio *ctsio); 407static int ctl_scsiio(struct ctl_scsiio *ctsio); 408 409static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 410static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 411 ctl_ua_type ua_type); 412static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 413 ctl_ua_type ua_type); 414static int ctl_abort_task(union ctl_io *io); 415static int ctl_abort_task_set(union ctl_io *io); 416static int ctl_i_t_nexus_reset(union ctl_io *io); 417static void ctl_run_task(union ctl_io *io); 418#ifdef CTL_IO_DELAY 419static void ctl_datamove_timer_wakeup(void *arg); 420static void ctl_done_timer_wakeup(void *arg); 421#endif /* CTL_IO_DELAY */ 422 423static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 424static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 425static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 426static void ctl_datamove_remote_write(union ctl_io *io); 427static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 428static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 429static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 430static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 431 ctl_ha_dt_cb callback); 432static void ctl_datamove_remote_read(union ctl_io *io); 433static void ctl_datamove_remote(union ctl_io *io); 434static int ctl_process_done(union ctl_io *io); 435static void ctl_lun_thread(void *arg); 436static void ctl_work_thread(void *arg); 437static void ctl_enqueue_incoming(union ctl_io *io); 438static void ctl_enqueue_rtr(union ctl_io *io); 439static void ctl_enqueue_done(union ctl_io *io); 440static void ctl_enqueue_isc(union ctl_io *io); 441static const struct ctl_cmd_entry * 442 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 443static const struct ctl_cmd_entry * 444 ctl_validate_command(struct ctl_scsiio *ctsio); 445static int ctl_cmd_applicable(uint8_t lun_type, 446 const struct ctl_cmd_entry *entry); 447 448/* 449 * Load the serialization table. This isn't very pretty, but is probably 450 * the easiest way to do it. 451 */ 452#include "ctl_ser_table.c" 453 454/* 455 * We only need to define open, close and ioctl routines for this driver. 456 */ 457static struct cdevsw ctl_cdevsw = { 458 .d_version = D_VERSION, 459 .d_flags = 0, 460 .d_open = ctl_open, 461 .d_close = ctl_close, 462 .d_ioctl = ctl_ioctl, 463 .d_name = "ctl", 464}; 465 466 467MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 468MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 469 470static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 471 472static moduledata_t ctl_moduledata = { 473 "ctl", 474 ctl_module_event_handler, 475 NULL 476}; 477 478DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 479MODULE_VERSION(ctl, 1); 480 481static struct ctl_frontend ioctl_frontend = 482{ 483 .name = "ioctl", 484}; 485 486static void 487ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 488 union ctl_ha_msg *msg_info) 489{ 490 struct ctl_scsiio *ctsio; 491 492 if (msg_info->hdr.original_sc == NULL) { 493 printf("%s: original_sc == NULL!\n", __func__); 494 /* XXX KDM now what? */ 495 return; 496 } 497 498 ctsio = &msg_info->hdr.original_sc->scsiio; 499 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 500 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 501 ctsio->io_hdr.status = msg_info->hdr.status; 502 ctsio->scsi_status = msg_info->scsi.scsi_status; 503 ctsio->sense_len = msg_info->scsi.sense_len; 504 ctsio->sense_residual = msg_info->scsi.sense_residual; 505 ctsio->residual = msg_info->scsi.residual; 506 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 507 sizeof(ctsio->sense_data)); 508 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 509 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 510 ctl_enqueue_isc((union ctl_io *)ctsio); 511} 512 513static void 514ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 515 union ctl_ha_msg *msg_info) 516{ 517 struct ctl_scsiio *ctsio; 518 519 if (msg_info->hdr.serializing_sc == NULL) { 520 printf("%s: serializing_sc == NULL!\n", __func__); 521 /* XXX KDM now what? */ 522 return; 523 } 524 525 ctsio = &msg_info->hdr.serializing_sc->scsiio; 526#if 0 527 /* 528 * Attempt to catch the situation where an I/O has 529 * been freed, and we're using it again. 530 */ 531 if (ctsio->io_hdr.io_type == 0xff) { 532 union ctl_io *tmp_io; 533 tmp_io = (union ctl_io *)ctsio; 534 printf("%s: %p use after free!\n", __func__, 535 ctsio); 536 printf("%s: type %d msg %d cdb %x iptl: " 537 "%d:%d:%d:%d tag 0x%04x " 538 "flag %#x status %x\n", 539 __func__, 540 tmp_io->io_hdr.io_type, 541 tmp_io->io_hdr.msg_type, 542 tmp_io->scsiio.cdb[0], 543 tmp_io->io_hdr.nexus.initid.id, 544 tmp_io->io_hdr.nexus.targ_port, 545 tmp_io->io_hdr.nexus.targ_target.id, 546 tmp_io->io_hdr.nexus.targ_lun, 547 (tmp_io->io_hdr.io_type == 548 CTL_IO_TASK) ? 549 tmp_io->taskio.tag_num : 550 tmp_io->scsiio.tag_num, 551 tmp_io->io_hdr.flags, 552 tmp_io->io_hdr.status); 553 } 554#endif 555 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 556 ctl_enqueue_isc((union ctl_io *)ctsio); 557} 558 559/* 560 * ISC (Inter Shelf Communication) event handler. Events from the HA 561 * subsystem come in here. 562 */ 563static void 564ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 565{ 566 struct ctl_softc *ctl_softc; 567 union ctl_io *io; 568 struct ctl_prio *presio; 569 ctl_ha_status isc_status; 570 571 ctl_softc = control_softc; 572 io = NULL; 573 574 575#if 0 576 printf("CTL: Isc Msg event %d\n", event); 577#endif 578 if (event == CTL_HA_EVT_MSG_RECV) { 579 union ctl_ha_msg msg_info; 580 581 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 582 sizeof(msg_info), /*wait*/ 0); 583#if 0 584 printf("CTL: msg_type %d\n", msg_info.msg_type); 585#endif 586 if (isc_status != 0) { 587 printf("Error receiving message, status = %d\n", 588 isc_status); 589 return; 590 } 591 592 switch (msg_info.hdr.msg_type) { 593 case CTL_MSG_SERIALIZE: 594#if 0 595 printf("Serialize\n"); 596#endif 597 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 598 if (io == NULL) { 599 printf("ctl_isc_event_handler: can't allocate " 600 "ctl_io!\n"); 601 /* Bad Juju */ 602 /* Need to set busy and send msg back */ 603 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 604 msg_info.hdr.status = CTL_SCSI_ERROR; 605 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 606 msg_info.scsi.sense_len = 0; 607 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 608 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 609 } 610 goto bailout; 611 } 612 ctl_zero_io(io); 613 // populate ctsio from msg_info 614 io->io_hdr.io_type = CTL_IO_SCSI; 615 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 616 io->io_hdr.original_sc = msg_info.hdr.original_sc; 617#if 0 618 printf("pOrig %x\n", (int)msg_info.original_sc); 619#endif 620 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 621 CTL_FLAG_IO_ACTIVE; 622 /* 623 * If we're in serialization-only mode, we don't 624 * want to go through full done processing. Thus 625 * the COPY flag. 626 * 627 * XXX KDM add another flag that is more specific. 628 */ 629 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 630 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 631 io->io_hdr.nexus = msg_info.hdr.nexus; 632#if 0 633 printf("targ %d, port %d, iid %d, lun %d\n", 634 io->io_hdr.nexus.targ_target.id, 635 io->io_hdr.nexus.targ_port, 636 io->io_hdr.nexus.initid.id, 637 io->io_hdr.nexus.targ_lun); 638#endif 639 io->scsiio.tag_num = msg_info.scsi.tag_num; 640 io->scsiio.tag_type = msg_info.scsi.tag_type; 641 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 642 CTL_MAX_CDBLEN); 643 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 644 const struct ctl_cmd_entry *entry; 645 646 entry = ctl_get_cmd_entry(&io->scsiio); 647 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 648 io->io_hdr.flags |= 649 entry->flags & CTL_FLAG_DATA_MASK; 650 } 651 ctl_enqueue_isc(io); 652 break; 653 654 /* Performed on the Originating SC, XFER mode only */ 655 case CTL_MSG_DATAMOVE: { 656 struct ctl_sg_entry *sgl; 657 int i, j; 658 659 io = msg_info.hdr.original_sc; 660 if (io == NULL) { 661 printf("%s: original_sc == NULL!\n", __func__); 662 /* XXX KDM do something here */ 663 break; 664 } 665 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 666 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 667 /* 668 * Keep track of this, we need to send it back over 669 * when the datamove is complete. 670 */ 671 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 672 673 if (msg_info.dt.sg_sequence == 0) { 674 /* 675 * XXX KDM we use the preallocated S/G list 676 * here, but we'll need to change this to 677 * dynamic allocation if we need larger S/G 678 * lists. 679 */ 680 if (msg_info.dt.kern_sg_entries > 681 sizeof(io->io_hdr.remote_sglist) / 682 sizeof(io->io_hdr.remote_sglist[0])) { 683 printf("%s: number of S/G entries " 684 "needed %u > allocated num %zd\n", 685 __func__, 686 msg_info.dt.kern_sg_entries, 687 sizeof(io->io_hdr.remote_sglist)/ 688 sizeof(io->io_hdr.remote_sglist[0])); 689 690 /* 691 * XXX KDM send a message back to 692 * the other side to shut down the 693 * DMA. The error will come back 694 * through via the normal channel. 695 */ 696 break; 697 } 698 sgl = io->io_hdr.remote_sglist; 699 memset(sgl, 0, 700 sizeof(io->io_hdr.remote_sglist)); 701 702 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 703 704 io->scsiio.kern_sg_entries = 705 msg_info.dt.kern_sg_entries; 706 io->scsiio.rem_sg_entries = 707 msg_info.dt.kern_sg_entries; 708 io->scsiio.kern_data_len = 709 msg_info.dt.kern_data_len; 710 io->scsiio.kern_total_len = 711 msg_info.dt.kern_total_len; 712 io->scsiio.kern_data_resid = 713 msg_info.dt.kern_data_resid; 714 io->scsiio.kern_rel_offset = 715 msg_info.dt.kern_rel_offset; 716 /* 717 * Clear out per-DMA flags. 718 */ 719 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 720 /* 721 * Add per-DMA flags that are set for this 722 * particular DMA request. 723 */ 724 io->io_hdr.flags |= msg_info.dt.flags & 725 CTL_FLAG_RDMA_MASK; 726 } else 727 sgl = (struct ctl_sg_entry *) 728 io->scsiio.kern_data_ptr; 729 730 for (i = msg_info.dt.sent_sg_entries, j = 0; 731 i < (msg_info.dt.sent_sg_entries + 732 msg_info.dt.cur_sg_entries); i++, j++) { 733 sgl[i].addr = msg_info.dt.sg_list[j].addr; 734 sgl[i].len = msg_info.dt.sg_list[j].len; 735 736#if 0 737 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 738 __func__, 739 msg_info.dt.sg_list[j].addr, 740 msg_info.dt.sg_list[j].len, 741 sgl[i].addr, sgl[i].len, j, i); 742#endif 743 } 744#if 0 745 memcpy(&sgl[msg_info.dt.sent_sg_entries], 746 msg_info.dt.sg_list, 747 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 748#endif 749 750 /* 751 * If this is the last piece of the I/O, we've got 752 * the full S/G list. Queue processing in the thread. 753 * Otherwise wait for the next piece. 754 */ 755 if (msg_info.dt.sg_last != 0) 756 ctl_enqueue_isc(io); 757 break; 758 } 759 /* Performed on the Serializing (primary) SC, XFER mode only */ 760 case CTL_MSG_DATAMOVE_DONE: { 761 if (msg_info.hdr.serializing_sc == NULL) { 762 printf("%s: serializing_sc == NULL!\n", 763 __func__); 764 /* XXX KDM now what? */ 765 break; 766 } 767 /* 768 * We grab the sense information here in case 769 * there was a failure, so we can return status 770 * back to the initiator. 771 */ 772 io = msg_info.hdr.serializing_sc; 773 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 774 io->io_hdr.status = msg_info.hdr.status; 775 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 776 io->scsiio.sense_len = msg_info.scsi.sense_len; 777 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 778 io->io_hdr.port_status = msg_info.scsi.fetd_status; 779 io->scsiio.residual = msg_info.scsi.residual; 780 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 781 sizeof(io->scsiio.sense_data)); 782 ctl_enqueue_isc(io); 783 break; 784 } 785 786 /* Preformed on Originating SC, SER_ONLY mode */ 787 case CTL_MSG_R2R: 788 io = msg_info.hdr.original_sc; 789 if (io == NULL) { 790 printf("%s: Major Bummer\n", __func__); 791 return; 792 } else { 793#if 0 794 printf("pOrig %x\n",(int) ctsio); 795#endif 796 } 797 io->io_hdr.msg_type = CTL_MSG_R2R; 798 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 799 ctl_enqueue_isc(io); 800 break; 801 802 /* 803 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 804 * mode. 805 * Performed on the Originating (i.e. secondary) SC in XFER 806 * mode 807 */ 808 case CTL_MSG_FINISH_IO: 809 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 810 ctl_isc_handler_finish_xfer(ctl_softc, 811 &msg_info); 812 else 813 ctl_isc_handler_finish_ser_only(ctl_softc, 814 &msg_info); 815 break; 816 817 /* Preformed on Originating SC */ 818 case CTL_MSG_BAD_JUJU: 819 io = msg_info.hdr.original_sc; 820 if (io == NULL) { 821 printf("%s: Bad JUJU!, original_sc is NULL!\n", 822 __func__); 823 break; 824 } 825 ctl_copy_sense_data(&msg_info, io); 826 /* 827 * IO should have already been cleaned up on other 828 * SC so clear this flag so we won't send a message 829 * back to finish the IO there. 830 */ 831 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 832 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 833 834 /* io = msg_info.hdr.serializing_sc; */ 835 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 836 ctl_enqueue_isc(io); 837 break; 838 839 /* Handle resets sent from the other side */ 840 case CTL_MSG_MANAGE_TASKS: { 841 struct ctl_taskio *taskio; 842 taskio = (struct ctl_taskio *)ctl_alloc_io( 843 (void *)ctl_softc->othersc_pool); 844 if (taskio == NULL) { 845 printf("ctl_isc_event_handler: can't allocate " 846 "ctl_io!\n"); 847 /* Bad Juju */ 848 /* should I just call the proper reset func 849 here??? */ 850 goto bailout; 851 } 852 ctl_zero_io((union ctl_io *)taskio); 853 taskio->io_hdr.io_type = CTL_IO_TASK; 854 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 855 taskio->io_hdr.nexus = msg_info.hdr.nexus; 856 taskio->task_action = msg_info.task.task_action; 857 taskio->tag_num = msg_info.task.tag_num; 858 taskio->tag_type = msg_info.task.tag_type; 859#ifdef CTL_TIME_IO 860 taskio->io_hdr.start_time = time_uptime; 861 getbintime(&taskio->io_hdr.start_bt); 862#if 0 863 cs_prof_gettime(&taskio->io_hdr.start_ticks); 864#endif 865#endif /* CTL_TIME_IO */ 866 ctl_run_task((union ctl_io *)taskio); 867 break; 868 } 869 /* Persistent Reserve action which needs attention */ 870 case CTL_MSG_PERS_ACTION: 871 presio = (struct ctl_prio *)ctl_alloc_io( 872 (void *)ctl_softc->othersc_pool); 873 if (presio == NULL) { 874 printf("ctl_isc_event_handler: can't allocate " 875 "ctl_io!\n"); 876 /* Bad Juju */ 877 /* Need to set busy and send msg back */ 878 goto bailout; 879 } 880 ctl_zero_io((union ctl_io *)presio); 881 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 882 presio->pr_msg = msg_info.pr; 883 ctl_enqueue_isc((union ctl_io *)presio); 884 break; 885 case CTL_MSG_SYNC_FE: 886 rcv_sync_msg = 1; 887 break; 888 case CTL_MSG_APS_LOCK: { 889 // It's quicker to execute this then to 890 // queue it. 891 struct ctl_lun *lun; 892 struct ctl_page_index *page_index; 893 struct copan_aps_subpage *current_sp; 894 uint32_t targ_lun; 895 896 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 897 lun = ctl_softc->ctl_luns[targ_lun]; 898 mtx_lock(&lun->lun_lock); 899 page_index = &lun->mode_pages.index[index_to_aps_page]; 900 current_sp = (struct copan_aps_subpage *) 901 (page_index->page_data + 902 (page_index->page_len * CTL_PAGE_CURRENT)); 903 904 current_sp->lock_active = msg_info.aps.lock_flag; 905 mtx_unlock(&lun->lun_lock); 906 break; 907 } 908 default: 909 printf("How did I get here?\n"); 910 } 911 } else if (event == CTL_HA_EVT_MSG_SENT) { 912 if (param != CTL_HA_STATUS_SUCCESS) { 913 printf("Bad status from ctl_ha_msg_send status %d\n", 914 param); 915 } 916 return; 917 } else if (event == CTL_HA_EVT_DISCONNECT) { 918 printf("CTL: Got a disconnect from Isc\n"); 919 return; 920 } else { 921 printf("ctl_isc_event_handler: Unknown event %d\n", event); 922 return; 923 } 924 925bailout: 926 return; 927} 928 929static void 930ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 931{ 932 struct scsi_sense_data *sense; 933 934 sense = &dest->scsiio.sense_data; 935 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 936 dest->scsiio.scsi_status = src->scsi.scsi_status; 937 dest->scsiio.sense_len = src->scsi.sense_len; 938 dest->io_hdr.status = src->hdr.status; 939} 940 941static int 942ctl_init(void) 943{ 944 struct ctl_softc *softc; 945 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 946 struct ctl_port *port; 947 uint8_t sc_id =0; 948 int i, error, retval; 949 //int isc_retval; 950 951 retval = 0; 952 ctl_pause_rtr = 0; 953 rcv_sync_msg = 0; 954 955 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 956 M_WAITOK | M_ZERO); 957 softc = control_softc; 958 959 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 960 "cam/ctl"); 961 962 softc->dev->si_drv1 = softc; 963 964 /* 965 * By default, return a "bad LUN" peripheral qualifier for unknown 966 * LUNs. The user can override this default using the tunable or 967 * sysctl. See the comment in ctl_inquiry_std() for more details. 968 */ 969 softc->inquiry_pq_no_lun = 1; 970 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 971 &softc->inquiry_pq_no_lun); 972 sysctl_ctx_init(&softc->sysctl_ctx); 973 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 974 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 975 CTLFLAG_RD, 0, "CAM Target Layer"); 976 977 if (softc->sysctl_tree == NULL) { 978 printf("%s: unable to allocate sysctl tree\n", __func__); 979 destroy_dev(softc->dev); 980 free(control_softc, M_DEVBUF); 981 control_softc = NULL; 982 return (ENOMEM); 983 } 984 985 SYSCTL_ADD_INT(&softc->sysctl_ctx, 986 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 987 "inquiry_pq_no_lun", CTLFLAG_RW, 988 &softc->inquiry_pq_no_lun, 0, 989 "Report no lun possible for invalid LUNs"); 990 991 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 992 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 993 softc->open_count = 0; 994 995 /* 996 * Default to actually sending a SYNCHRONIZE CACHE command down to 997 * the drive. 998 */ 999 softc->flags = CTL_FLAG_REAL_SYNC; 1000 1001 /* 1002 * In Copan's HA scheme, the "master" and "slave" roles are 1003 * figured out through the slot the controller is in. Although it 1004 * is an active/active system, someone has to be in charge. 1005 */ 1006#ifdef NEEDTOPORT 1007 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1008#endif 1009 1010 if (sc_id == 0) { 1011 softc->flags |= CTL_FLAG_MASTER_SHELF; 1012 persis_offset = 0; 1013 } else 1014 persis_offset = CTL_MAX_INITIATORS; 1015 1016 /* 1017 * XXX KDM need to figure out where we want to get our target ID 1018 * and WWID. Is it different on each port? 1019 */ 1020 softc->target.id = 0; 1021 softc->target.wwid[0] = 0x12345678; 1022 softc->target.wwid[1] = 0x87654321; 1023 STAILQ_INIT(&softc->lun_list); 1024 STAILQ_INIT(&softc->pending_lun_queue); 1025 STAILQ_INIT(&softc->fe_list); 1026 STAILQ_INIT(&softc->port_list); 1027 STAILQ_INIT(&softc->be_list); 1028 STAILQ_INIT(&softc->io_pools); 1029 ctl_tpc_init(softc); 1030 1031 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1032 &internal_pool)!= 0){ 1033 printf("ctl: can't allocate %d entry internal pool, " 1034 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1035 return (ENOMEM); 1036 } 1037 1038 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1039 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1040 printf("ctl: can't allocate %d entry emergency pool, " 1041 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1042 ctl_pool_free(internal_pool); 1043 return (ENOMEM); 1044 } 1045 1046 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1047 &other_pool) != 0) 1048 { 1049 printf("ctl: can't allocate %d entry other SC pool, " 1050 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1051 ctl_pool_free(internal_pool); 1052 ctl_pool_free(emergency_pool); 1053 return (ENOMEM); 1054 } 1055 1056 softc->internal_pool = internal_pool; 1057 softc->emergency_pool = emergency_pool; 1058 softc->othersc_pool = other_pool; 1059 1060 if (worker_threads <= 0) 1061 worker_threads = max(1, mp_ncpus / 4); 1062 if (worker_threads > CTL_MAX_THREADS) 1063 worker_threads = CTL_MAX_THREADS; 1064 1065 for (i = 0; i < worker_threads; i++) { 1066 struct ctl_thread *thr = &softc->threads[i]; 1067 1068 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1069 thr->ctl_softc = softc; 1070 STAILQ_INIT(&thr->incoming_queue); 1071 STAILQ_INIT(&thr->rtr_queue); 1072 STAILQ_INIT(&thr->done_queue); 1073 STAILQ_INIT(&thr->isc_queue); 1074 1075 error = kproc_kthread_add(ctl_work_thread, thr, 1076 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1077 if (error != 0) { 1078 printf("error creating CTL work thread!\n"); 1079 ctl_pool_free(internal_pool); 1080 ctl_pool_free(emergency_pool); 1081 ctl_pool_free(other_pool); 1082 return (error); 1083 } 1084 } 1085 error = kproc_kthread_add(ctl_lun_thread, softc, 1086 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1087 if (error != 0) { 1088 printf("error creating CTL lun thread!\n"); 1089 ctl_pool_free(internal_pool); 1090 ctl_pool_free(emergency_pool); 1091 ctl_pool_free(other_pool); 1092 return (error); 1093 } 1094 if (bootverbose) 1095 printf("ctl: CAM Target Layer loaded\n"); 1096 1097 /* 1098 * Initialize the ioctl front end. 1099 */ 1100 ctl_frontend_register(&ioctl_frontend); 1101 port = &softc->ioctl_info.port; 1102 port->frontend = &ioctl_frontend; 1103 sprintf(softc->ioctl_info.port_name, "ioctl"); 1104 port->port_type = CTL_PORT_IOCTL; 1105 port->num_requested_ctl_io = 100; 1106 port->port_name = softc->ioctl_info.port_name; 1107 port->port_online = ctl_ioctl_online; 1108 port->port_offline = ctl_ioctl_offline; 1109 port->onoff_arg = &softc->ioctl_info; 1110 port->lun_enable = ctl_ioctl_lun_enable; 1111 port->lun_disable = ctl_ioctl_lun_disable; 1112 port->targ_lun_arg = &softc->ioctl_info; 1113 port->fe_datamove = ctl_ioctl_datamove; 1114 port->fe_done = ctl_ioctl_done; 1115 port->max_targets = 15; 1116 port->max_target_id = 15; 1117 1118 if (ctl_port_register(&softc->ioctl_info.port, 1119 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1120 printf("ctl: ioctl front end registration failed, will " 1121 "continue anyway\n"); 1122 } 1123 1124#ifdef CTL_IO_DELAY 1125 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1126 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1127 sizeof(struct callout), CTL_TIMER_BYTES); 1128 return (EINVAL); 1129 } 1130#endif /* CTL_IO_DELAY */ 1131 1132 return (0); 1133} 1134 1135void 1136ctl_shutdown(void) 1137{ 1138 struct ctl_softc *softc; 1139 struct ctl_lun *lun, *next_lun; 1140 struct ctl_io_pool *pool; 1141 1142 softc = (struct ctl_softc *)control_softc; 1143 1144 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1145 printf("ctl: ioctl front end deregistration failed\n"); 1146 1147 mtx_lock(&softc->ctl_lock); 1148 1149 /* 1150 * Free up each LUN. 1151 */ 1152 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1153 next_lun = STAILQ_NEXT(lun, links); 1154 ctl_free_lun(lun); 1155 } 1156 1157 mtx_unlock(&softc->ctl_lock); 1158 1159 ctl_frontend_deregister(&ioctl_frontend); 1160 1161 /* 1162 * This will rip the rug out from under any FETDs or anyone else 1163 * that has a pool allocated. Since we increment our module 1164 * refcount any time someone outside the main CTL module allocates 1165 * a pool, we shouldn't have any problems here. The user won't be 1166 * able to unload the CTL module until client modules have 1167 * successfully unloaded. 1168 */ 1169 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1170 ctl_pool_free(pool); 1171 1172#if 0 1173 ctl_shutdown_thread(softc->work_thread); 1174 mtx_destroy(&softc->queue_lock); 1175#endif 1176 1177 ctl_tpc_shutdown(softc); 1178 mtx_destroy(&softc->pool_lock); 1179 mtx_destroy(&softc->ctl_lock); 1180 1181 destroy_dev(softc->dev); 1182 1183 sysctl_ctx_free(&softc->sysctl_ctx); 1184 1185 free(control_softc, M_DEVBUF); 1186 control_softc = NULL; 1187 1188 if (bootverbose) 1189 printf("ctl: CAM Target Layer unloaded\n"); 1190} 1191 1192static int 1193ctl_module_event_handler(module_t mod, int what, void *arg) 1194{ 1195 1196 switch (what) { 1197 case MOD_LOAD: 1198 return (ctl_init()); 1199 case MOD_UNLOAD: 1200 return (EBUSY); 1201 default: 1202 return (EOPNOTSUPP); 1203 } 1204} 1205 1206/* 1207 * XXX KDM should we do some access checks here? Bump a reference count to 1208 * prevent a CTL module from being unloaded while someone has it open? 1209 */ 1210static int 1211ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1212{ 1213 return (0); 1214} 1215 1216static int 1217ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1218{ 1219 return (0); 1220} 1221 1222int 1223ctl_port_enable(ctl_port_type port_type) 1224{ 1225 struct ctl_softc *softc; 1226 struct ctl_port *port; 1227 1228 if (ctl_is_single == 0) { 1229 union ctl_ha_msg msg_info; 1230 int isc_retval; 1231 1232#if 0 1233 printf("%s: HA mode, synchronizing frontend enable\n", 1234 __func__); 1235#endif 1236 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1237 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1238 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1239 printf("Sync msg send error retval %d\n", isc_retval); 1240 } 1241 if (!rcv_sync_msg) { 1242 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1243 sizeof(msg_info), 1); 1244 } 1245#if 0 1246 printf("CTL:Frontend Enable\n"); 1247 } else { 1248 printf("%s: single mode, skipping frontend synchronization\n", 1249 __func__); 1250#endif 1251 } 1252 1253 softc = control_softc; 1254 1255 STAILQ_FOREACH(port, &softc->port_list, links) { 1256 if (port_type & port->port_type) 1257 { 1258#if 0 1259 printf("port %d\n", port->targ_port); 1260#endif 1261 ctl_port_online(port); 1262 } 1263 } 1264 1265 return (0); 1266} 1267 1268int 1269ctl_port_disable(ctl_port_type port_type) 1270{ 1271 struct ctl_softc *softc; 1272 struct ctl_port *port; 1273 1274 softc = control_softc; 1275 1276 STAILQ_FOREACH(port, &softc->port_list, links) { 1277 if (port_type & port->port_type) 1278 ctl_port_offline(port); 1279 } 1280 1281 return (0); 1282} 1283 1284/* 1285 * Returns 0 for success, 1 for failure. 1286 * Currently the only failure mode is if there aren't enough entries 1287 * allocated. So, in case of a failure, look at num_entries_dropped, 1288 * reallocate and try again. 1289 */ 1290int 1291ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1292 int *num_entries_filled, int *num_entries_dropped, 1293 ctl_port_type port_type, int no_virtual) 1294{ 1295 struct ctl_softc *softc; 1296 struct ctl_port *port; 1297 int entries_dropped, entries_filled; 1298 int retval; 1299 int i; 1300 1301 softc = control_softc; 1302 1303 retval = 0; 1304 entries_filled = 0; 1305 entries_dropped = 0; 1306 1307 i = 0; 1308 mtx_lock(&softc->ctl_lock); 1309 STAILQ_FOREACH(port, &softc->port_list, links) { 1310 struct ctl_port_entry *entry; 1311 1312 if ((port->port_type & port_type) == 0) 1313 continue; 1314 1315 if ((no_virtual != 0) 1316 && (port->virtual_port != 0)) 1317 continue; 1318 1319 if (entries_filled >= num_entries_alloced) { 1320 entries_dropped++; 1321 continue; 1322 } 1323 entry = &entries[i]; 1324 1325 entry->port_type = port->port_type; 1326 strlcpy(entry->port_name, port->port_name, 1327 sizeof(entry->port_name)); 1328 entry->physical_port = port->physical_port; 1329 entry->virtual_port = port->virtual_port; 1330 entry->wwnn = port->wwnn; 1331 entry->wwpn = port->wwpn; 1332 1333 i++; 1334 entries_filled++; 1335 } 1336 1337 mtx_unlock(&softc->ctl_lock); 1338 1339 if (entries_dropped > 0) 1340 retval = 1; 1341 1342 *num_entries_dropped = entries_dropped; 1343 *num_entries_filled = entries_filled; 1344 1345 return (retval); 1346} 1347 1348static void 1349ctl_ioctl_online(void *arg) 1350{ 1351 struct ctl_ioctl_info *ioctl_info; 1352 1353 ioctl_info = (struct ctl_ioctl_info *)arg; 1354 1355 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1356} 1357 1358static void 1359ctl_ioctl_offline(void *arg) 1360{ 1361 struct ctl_ioctl_info *ioctl_info; 1362 1363 ioctl_info = (struct ctl_ioctl_info *)arg; 1364 1365 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1366} 1367 1368/* 1369 * Remove an initiator by port number and initiator ID. 1370 * Returns 0 for success, -1 for failure. 1371 */ 1372int 1373ctl_remove_initiator(struct ctl_port *port, int iid) 1374{ 1375 struct ctl_softc *softc = control_softc; 1376 1377 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1378 1379 if (iid > CTL_MAX_INIT_PER_PORT) { 1380 printf("%s: initiator ID %u > maximun %u!\n", 1381 __func__, iid, CTL_MAX_INIT_PER_PORT); 1382 return (-1); 1383 } 1384 1385 mtx_lock(&softc->ctl_lock); 1386 port->wwpn_iid[iid].in_use--; 1387 port->wwpn_iid[iid].last_use = time_uptime; 1388 mtx_unlock(&softc->ctl_lock); 1389 1390 return (0); 1391} 1392 1393/* 1394 * Add an initiator to the initiator map. 1395 * Returns iid for success, < 0 for failure. 1396 */ 1397int 1398ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1399{ 1400 struct ctl_softc *softc = control_softc; 1401 time_t best_time; 1402 int i, best; 1403 1404 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1405 1406 if (iid >= CTL_MAX_INIT_PER_PORT) { 1407 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1408 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1409 free(name, M_CTL); 1410 return (-1); 1411 } 1412 1413 mtx_lock(&softc->ctl_lock); 1414 1415 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1416 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1417 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1418 iid = i; 1419 break; 1420 } 1421 if (name != NULL && port->wwpn_iid[i].name != NULL && 1422 strcmp(name, port->wwpn_iid[i].name) == 0) { 1423 iid = i; 1424 break; 1425 } 1426 } 1427 } 1428 1429 if (iid < 0) { 1430 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1431 if (port->wwpn_iid[i].in_use == 0 && 1432 port->wwpn_iid[i].wwpn == 0 && 1433 port->wwpn_iid[i].name == NULL) { 1434 iid = i; 1435 break; 1436 } 1437 } 1438 } 1439 1440 if (iid < 0) { 1441 best = -1; 1442 best_time = INT32_MAX; 1443 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1444 if (port->wwpn_iid[i].in_use == 0) { 1445 if (port->wwpn_iid[i].last_use < best_time) { 1446 best = i; 1447 best_time = port->wwpn_iid[i].last_use; 1448 } 1449 } 1450 } 1451 iid = best; 1452 } 1453 1454 if (iid < 0) { 1455 mtx_unlock(&softc->ctl_lock); 1456 free(name, M_CTL); 1457 return (-2); 1458 } 1459 1460 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1461 /* 1462 * This is not an error yet. 1463 */ 1464 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1465#if 0 1466 printf("%s: port %d iid %u WWPN %#jx arrived" 1467 " again\n", __func__, port->targ_port, 1468 iid, (uintmax_t)wwpn); 1469#endif 1470 goto take; 1471 } 1472 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1473 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1474#if 0 1475 printf("%s: port %d iid %u name '%s' arrived" 1476 " again\n", __func__, port->targ_port, 1477 iid, name); 1478#endif 1479 goto take; 1480 } 1481 1482 /* 1483 * This is an error, but what do we do about it? The 1484 * driver is telling us we have a new WWPN for this 1485 * initiator ID, so we pretty much need to use it. 1486 */ 1487 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1488 " but WWPN %#jx '%s' is still at that address\n", 1489 __func__, port->targ_port, iid, wwpn, name, 1490 (uintmax_t)port->wwpn_iid[iid].wwpn, 1491 port->wwpn_iid[iid].name); 1492 1493 /* 1494 * XXX KDM clear have_ca and ua_pending on each LUN for 1495 * this initiator. 1496 */ 1497 } 1498take: 1499 free(port->wwpn_iid[iid].name, M_CTL); 1500 port->wwpn_iid[iid].name = name; 1501 port->wwpn_iid[iid].wwpn = wwpn; 1502 port->wwpn_iid[iid].in_use++; 1503 mtx_unlock(&softc->ctl_lock); 1504 1505 return (iid); 1506} 1507 1508static int 1509ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1510{ 1511 int len; 1512 1513 switch (port->port_type) { 1514 case CTL_PORT_FC: 1515 { 1516 struct scsi_transportid_fcp *id = 1517 (struct scsi_transportid_fcp *)buf; 1518 if (port->wwpn_iid[iid].wwpn == 0) 1519 return (0); 1520 memset(id, 0, sizeof(*id)); 1521 id->format_protocol = SCSI_PROTO_FC; 1522 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1523 return (sizeof(*id)); 1524 } 1525 case CTL_PORT_ISCSI: 1526 { 1527 struct scsi_transportid_iscsi_port *id = 1528 (struct scsi_transportid_iscsi_port *)buf; 1529 if (port->wwpn_iid[iid].name == NULL) 1530 return (0); 1531 memset(id, 0, 256); 1532 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1533 SCSI_PROTO_ISCSI; 1534 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1535 len = roundup2(min(len, 252), 4); 1536 scsi_ulto2b(len, id->additional_length); 1537 return (sizeof(*id) + len); 1538 } 1539 case CTL_PORT_SAS: 1540 { 1541 struct scsi_transportid_sas *id = 1542 (struct scsi_transportid_sas *)buf; 1543 if (port->wwpn_iid[iid].wwpn == 0) 1544 return (0); 1545 memset(id, 0, sizeof(*id)); 1546 id->format_protocol = SCSI_PROTO_SAS; 1547 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1548 return (sizeof(*id)); 1549 } 1550 default: 1551 { 1552 struct scsi_transportid_spi *id = 1553 (struct scsi_transportid_spi *)buf; 1554 memset(id, 0, sizeof(*id)); 1555 id->format_protocol = SCSI_PROTO_SPI; 1556 scsi_ulto2b(iid, id->scsi_addr); 1557 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1558 return (sizeof(*id)); 1559 } 1560 } 1561} 1562 1563static int 1564ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1565{ 1566 return (0); 1567} 1568 1569static int 1570ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1571{ 1572 return (0); 1573} 1574 1575/* 1576 * Data movement routine for the CTL ioctl frontend port. 1577 */ 1578static int 1579ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1580{ 1581 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1582 struct ctl_sg_entry ext_entry, kern_entry; 1583 int ext_sglen, ext_sg_entries, kern_sg_entries; 1584 int ext_sg_start, ext_offset; 1585 int len_to_copy, len_copied; 1586 int kern_watermark, ext_watermark; 1587 int ext_sglist_malloced; 1588 int i, j; 1589 1590 ext_sglist_malloced = 0; 1591 ext_sg_start = 0; 1592 ext_offset = 0; 1593 1594 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1595 1596 /* 1597 * If this flag is set, fake the data transfer. 1598 */ 1599 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1600 ctsio->ext_data_filled = ctsio->ext_data_len; 1601 goto bailout; 1602 } 1603 1604 /* 1605 * To simplify things here, if we have a single buffer, stick it in 1606 * a S/G entry and just make it a single entry S/G list. 1607 */ 1608 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1609 int len_seen; 1610 1611 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1612 1613 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1614 M_WAITOK); 1615 ext_sglist_malloced = 1; 1616 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1617 ext_sglen) != 0) { 1618 ctl_set_internal_failure(ctsio, 1619 /*sks_valid*/ 0, 1620 /*retry_count*/ 0); 1621 goto bailout; 1622 } 1623 ext_sg_entries = ctsio->ext_sg_entries; 1624 len_seen = 0; 1625 for (i = 0; i < ext_sg_entries; i++) { 1626 if ((len_seen + ext_sglist[i].len) >= 1627 ctsio->ext_data_filled) { 1628 ext_sg_start = i; 1629 ext_offset = ctsio->ext_data_filled - len_seen; 1630 break; 1631 } 1632 len_seen += ext_sglist[i].len; 1633 } 1634 } else { 1635 ext_sglist = &ext_entry; 1636 ext_sglist->addr = ctsio->ext_data_ptr; 1637 ext_sglist->len = ctsio->ext_data_len; 1638 ext_sg_entries = 1; 1639 ext_sg_start = 0; 1640 ext_offset = ctsio->ext_data_filled; 1641 } 1642 1643 if (ctsio->kern_sg_entries > 0) { 1644 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1645 kern_sg_entries = ctsio->kern_sg_entries; 1646 } else { 1647 kern_sglist = &kern_entry; 1648 kern_sglist->addr = ctsio->kern_data_ptr; 1649 kern_sglist->len = ctsio->kern_data_len; 1650 kern_sg_entries = 1; 1651 } 1652 1653 1654 kern_watermark = 0; 1655 ext_watermark = ext_offset; 1656 len_copied = 0; 1657 for (i = ext_sg_start, j = 0; 1658 i < ext_sg_entries && j < kern_sg_entries;) { 1659 uint8_t *ext_ptr, *kern_ptr; 1660 1661 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1662 kern_sglist[j].len - kern_watermark); 1663 1664 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1665 ext_ptr = ext_ptr + ext_watermark; 1666 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1667 /* 1668 * XXX KDM fix this! 1669 */ 1670 panic("need to implement bus address support"); 1671#if 0 1672 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1673#endif 1674 } else 1675 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1676 kern_ptr = kern_ptr + kern_watermark; 1677 1678 kern_watermark += len_to_copy; 1679 ext_watermark += len_to_copy; 1680 1681 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1682 CTL_FLAG_DATA_IN) { 1683 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1684 "bytes to user\n", len_to_copy)); 1685 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1686 "to %p\n", kern_ptr, ext_ptr)); 1687 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1688 ctl_set_internal_failure(ctsio, 1689 /*sks_valid*/ 0, 1690 /*retry_count*/ 0); 1691 goto bailout; 1692 } 1693 } else { 1694 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1695 "bytes from user\n", len_to_copy)); 1696 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1697 "to %p\n", ext_ptr, kern_ptr)); 1698 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1699 ctl_set_internal_failure(ctsio, 1700 /*sks_valid*/ 0, 1701 /*retry_count*/0); 1702 goto bailout; 1703 } 1704 } 1705 1706 len_copied += len_to_copy; 1707 1708 if (ext_sglist[i].len == ext_watermark) { 1709 i++; 1710 ext_watermark = 0; 1711 } 1712 1713 if (kern_sglist[j].len == kern_watermark) { 1714 j++; 1715 kern_watermark = 0; 1716 } 1717 } 1718 1719 ctsio->ext_data_filled += len_copied; 1720 1721 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1722 "kern_sg_entries: %d\n", ext_sg_entries, 1723 kern_sg_entries)); 1724 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1725 "kern_data_len = %d\n", ctsio->ext_data_len, 1726 ctsio->kern_data_len)); 1727 1728 1729 /* XXX KDM set residual?? */ 1730bailout: 1731 1732 if (ext_sglist_malloced != 0) 1733 free(ext_sglist, M_CTL); 1734 1735 return (CTL_RETVAL_COMPLETE); 1736} 1737 1738/* 1739 * Serialize a command that went down the "wrong" side, and so was sent to 1740 * this controller for execution. The logic is a little different than the 1741 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1742 * sent back to the other side, but in the success case, we execute the 1743 * command on this side (XFER mode) or tell the other side to execute it 1744 * (SER_ONLY mode). 1745 */ 1746static int 1747ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1748{ 1749 struct ctl_softc *ctl_softc; 1750 union ctl_ha_msg msg_info; 1751 struct ctl_lun *lun; 1752 int retval = 0; 1753 uint32_t targ_lun; 1754 1755 ctl_softc = control_softc; 1756 1757 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1758 lun = ctl_softc->ctl_luns[targ_lun]; 1759 if (lun==NULL) 1760 { 1761 /* 1762 * Why isn't LUN defined? The other side wouldn't 1763 * send a cmd if the LUN is undefined. 1764 */ 1765 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1766 1767 /* "Logical unit not supported" */ 1768 ctl_set_sense_data(&msg_info.scsi.sense_data, 1769 lun, 1770 /*sense_format*/SSD_TYPE_NONE, 1771 /*current_error*/ 1, 1772 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1773 /*asc*/ 0x25, 1774 /*ascq*/ 0x00, 1775 SSD_ELEM_NONE); 1776 1777 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1778 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1779 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1780 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1781 msg_info.hdr.serializing_sc = NULL; 1782 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1783 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1784 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1785 } 1786 return(1); 1787 1788 } 1789 1790 mtx_lock(&lun->lun_lock); 1791 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1792 1793 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1794 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1795 ooa_links))) { 1796 case CTL_ACTION_BLOCK: 1797 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1798 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1799 blocked_links); 1800 break; 1801 case CTL_ACTION_PASS: 1802 case CTL_ACTION_SKIP: 1803 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1804 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1805 ctl_enqueue_rtr((union ctl_io *)ctsio); 1806 } else { 1807 1808 /* send msg back to other side */ 1809 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1810 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1811 msg_info.hdr.msg_type = CTL_MSG_R2R; 1812#if 0 1813 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1814#endif 1815 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1816 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1817 } 1818 } 1819 break; 1820 case CTL_ACTION_OVERLAP: 1821 /* OVERLAPPED COMMANDS ATTEMPTED */ 1822 ctl_set_sense_data(&msg_info.scsi.sense_data, 1823 lun, 1824 /*sense_format*/SSD_TYPE_NONE, 1825 /*current_error*/ 1, 1826 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1827 /*asc*/ 0x4E, 1828 /*ascq*/ 0x00, 1829 SSD_ELEM_NONE); 1830 1831 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1832 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1833 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1834 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1835 msg_info.hdr.serializing_sc = NULL; 1836 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1837#if 0 1838 printf("BAD JUJU:Major Bummer Overlap\n"); 1839#endif 1840 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1841 retval = 1; 1842 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1843 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1844 } 1845 break; 1846 case CTL_ACTION_OVERLAP_TAG: 1847 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1848 ctl_set_sense_data(&msg_info.scsi.sense_data, 1849 lun, 1850 /*sense_format*/SSD_TYPE_NONE, 1851 /*current_error*/ 1, 1852 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1853 /*asc*/ 0x4D, 1854 /*ascq*/ ctsio->tag_num & 0xff, 1855 SSD_ELEM_NONE); 1856 1857 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1858 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1859 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1860 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1861 msg_info.hdr.serializing_sc = NULL; 1862 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1863#if 0 1864 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1865#endif 1866 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1867 retval = 1; 1868 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1869 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1870 } 1871 break; 1872 case CTL_ACTION_ERROR: 1873 default: 1874 /* "Internal target failure" */ 1875 ctl_set_sense_data(&msg_info.scsi.sense_data, 1876 lun, 1877 /*sense_format*/SSD_TYPE_NONE, 1878 /*current_error*/ 1, 1879 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1880 /*asc*/ 0x44, 1881 /*ascq*/ 0x00, 1882 SSD_ELEM_NONE); 1883 1884 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1885 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1886 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1887 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1888 msg_info.hdr.serializing_sc = NULL; 1889 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1890#if 0 1891 printf("BAD JUJU:Major Bummer HW Error\n"); 1892#endif 1893 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1894 retval = 1; 1895 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1896 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1897 } 1898 break; 1899 } 1900 mtx_unlock(&lun->lun_lock); 1901 return (retval); 1902} 1903 1904static int 1905ctl_ioctl_submit_wait(union ctl_io *io) 1906{ 1907 struct ctl_fe_ioctl_params params; 1908 ctl_fe_ioctl_state last_state; 1909 int done, retval; 1910 1911 retval = 0; 1912 1913 bzero(¶ms, sizeof(params)); 1914 1915 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1916 cv_init(¶ms.sem, "ctlioccv"); 1917 params.state = CTL_IOCTL_INPROG; 1918 last_state = params.state; 1919 1920 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1921 1922 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1923 1924 /* This shouldn't happen */ 1925 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1926 return (retval); 1927 1928 done = 0; 1929 1930 do { 1931 mtx_lock(¶ms.ioctl_mtx); 1932 /* 1933 * Check the state here, and don't sleep if the state has 1934 * already changed (i.e. wakeup has already occured, but we 1935 * weren't waiting yet). 1936 */ 1937 if (params.state == last_state) { 1938 /* XXX KDM cv_wait_sig instead? */ 1939 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1940 } 1941 last_state = params.state; 1942 1943 switch (params.state) { 1944 case CTL_IOCTL_INPROG: 1945 /* Why did we wake up? */ 1946 /* XXX KDM error here? */ 1947 mtx_unlock(¶ms.ioctl_mtx); 1948 break; 1949 case CTL_IOCTL_DATAMOVE: 1950 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1951 1952 /* 1953 * change last_state back to INPROG to avoid 1954 * deadlock on subsequent data moves. 1955 */ 1956 params.state = last_state = CTL_IOCTL_INPROG; 1957 1958 mtx_unlock(¶ms.ioctl_mtx); 1959 ctl_ioctl_do_datamove(&io->scsiio); 1960 /* 1961 * Note that in some cases, most notably writes, 1962 * this will queue the I/O and call us back later. 1963 * In other cases, generally reads, this routine 1964 * will immediately call back and wake us up, 1965 * probably using our own context. 1966 */ 1967 io->scsiio.be_move_done(io); 1968 break; 1969 case CTL_IOCTL_DONE: 1970 mtx_unlock(¶ms.ioctl_mtx); 1971 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1972 done = 1; 1973 break; 1974 default: 1975 mtx_unlock(¶ms.ioctl_mtx); 1976 /* XXX KDM error here? */ 1977 break; 1978 } 1979 } while (done == 0); 1980 1981 mtx_destroy(¶ms.ioctl_mtx); 1982 cv_destroy(¶ms.sem); 1983 1984 return (CTL_RETVAL_COMPLETE); 1985} 1986 1987static void 1988ctl_ioctl_datamove(union ctl_io *io) 1989{ 1990 struct ctl_fe_ioctl_params *params; 1991 1992 params = (struct ctl_fe_ioctl_params *) 1993 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1994 1995 mtx_lock(¶ms->ioctl_mtx); 1996 params->state = CTL_IOCTL_DATAMOVE; 1997 cv_broadcast(¶ms->sem); 1998 mtx_unlock(¶ms->ioctl_mtx); 1999} 2000 2001static void 2002ctl_ioctl_done(union ctl_io *io) 2003{ 2004 struct ctl_fe_ioctl_params *params; 2005 2006 params = (struct ctl_fe_ioctl_params *) 2007 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2008 2009 mtx_lock(¶ms->ioctl_mtx); 2010 params->state = CTL_IOCTL_DONE; 2011 cv_broadcast(¶ms->sem); 2012 mtx_unlock(¶ms->ioctl_mtx); 2013} 2014 2015static void 2016ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2017{ 2018 struct ctl_fe_ioctl_startstop_info *sd_info; 2019 2020 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2021 2022 sd_info->hs_info.status = metatask->status; 2023 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2024 sd_info->hs_info.luns_complete = 2025 metatask->taskinfo.startstop.luns_complete; 2026 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2027 2028 cv_broadcast(&sd_info->sem); 2029} 2030 2031static void 2032ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2033{ 2034 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2035 2036 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2037 2038 mtx_lock(fe_bbr_info->lock); 2039 fe_bbr_info->bbr_info->status = metatask->status; 2040 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2041 fe_bbr_info->wakeup_done = 1; 2042 mtx_unlock(fe_bbr_info->lock); 2043 2044 cv_broadcast(&fe_bbr_info->sem); 2045} 2046 2047/* 2048 * Returns 0 for success, errno for failure. 2049 */ 2050static int 2051ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2052 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2053{ 2054 union ctl_io *io; 2055 int retval; 2056 2057 retval = 0; 2058 2059 mtx_lock(&lun->lun_lock); 2060 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2061 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2062 ooa_links)) { 2063 struct ctl_ooa_entry *entry; 2064 2065 /* 2066 * If we've got more than we can fit, just count the 2067 * remaining entries. 2068 */ 2069 if (*cur_fill_num >= ooa_hdr->alloc_num) 2070 continue; 2071 2072 entry = &kern_entries[*cur_fill_num]; 2073 2074 entry->tag_num = io->scsiio.tag_num; 2075 entry->lun_num = lun->lun; 2076#ifdef CTL_TIME_IO 2077 entry->start_bt = io->io_hdr.start_bt; 2078#endif 2079 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2080 entry->cdb_len = io->scsiio.cdb_len; 2081 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2082 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2083 2084 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2085 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2086 2087 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2088 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2089 2090 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2091 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2092 2093 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2094 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2095 } 2096 mtx_unlock(&lun->lun_lock); 2097 2098 return (retval); 2099} 2100 2101static void * 2102ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2103 size_t error_str_len) 2104{ 2105 void *kptr; 2106 2107 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2108 2109 if (copyin(user_addr, kptr, len) != 0) { 2110 snprintf(error_str, error_str_len, "Error copying %d bytes " 2111 "from user address %p to kernel address %p", len, 2112 user_addr, kptr); 2113 free(kptr, M_CTL); 2114 return (NULL); 2115 } 2116 2117 return (kptr); 2118} 2119 2120static void 2121ctl_free_args(int num_args, struct ctl_be_arg *args) 2122{ 2123 int i; 2124 2125 if (args == NULL) 2126 return; 2127 2128 for (i = 0; i < num_args; i++) { 2129 free(args[i].kname, M_CTL); 2130 free(args[i].kvalue, M_CTL); 2131 } 2132 2133 free(args, M_CTL); 2134} 2135 2136static struct ctl_be_arg * 2137ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2138 char *error_str, size_t error_str_len) 2139{ 2140 struct ctl_be_arg *args; 2141 int i; 2142 2143 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2144 error_str, error_str_len); 2145 2146 if (args == NULL) 2147 goto bailout; 2148 2149 for (i = 0; i < num_args; i++) { 2150 args[i].kname = NULL; 2151 args[i].kvalue = NULL; 2152 } 2153 2154 for (i = 0; i < num_args; i++) { 2155 uint8_t *tmpptr; 2156 2157 args[i].kname = ctl_copyin_alloc(args[i].name, 2158 args[i].namelen, error_str, error_str_len); 2159 if (args[i].kname == NULL) 2160 goto bailout; 2161 2162 if (args[i].kname[args[i].namelen - 1] != '\0') { 2163 snprintf(error_str, error_str_len, "Argument %d " 2164 "name is not NUL-terminated", i); 2165 goto bailout; 2166 } 2167 2168 if (args[i].flags & CTL_BEARG_RD) { 2169 tmpptr = ctl_copyin_alloc(args[i].value, 2170 args[i].vallen, error_str, error_str_len); 2171 if (tmpptr == NULL) 2172 goto bailout; 2173 if ((args[i].flags & CTL_BEARG_ASCII) 2174 && (tmpptr[args[i].vallen - 1] != '\0')) { 2175 snprintf(error_str, error_str_len, "Argument " 2176 "%d value is not NUL-terminated", i); 2177 goto bailout; 2178 } 2179 args[i].kvalue = tmpptr; 2180 } else { 2181 args[i].kvalue = malloc(args[i].vallen, 2182 M_CTL, M_WAITOK | M_ZERO); 2183 } 2184 } 2185 2186 return (args); 2187bailout: 2188 2189 ctl_free_args(num_args, args); 2190 2191 return (NULL); 2192} 2193 2194static void 2195ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2196{ 2197 int i; 2198 2199 for (i = 0; i < num_args; i++) { 2200 if (args[i].flags & CTL_BEARG_WR) 2201 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2202 } 2203} 2204 2205/* 2206 * Escape characters that are illegal or not recommended in XML. 2207 */ 2208int 2209ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2210{ 2211 int retval; 2212 2213 retval = 0; 2214 2215 for (; *str; str++) { 2216 switch (*str) { 2217 case '&': 2218 retval = sbuf_printf(sb, "&"); 2219 break; 2220 case '>': 2221 retval = sbuf_printf(sb, ">"); 2222 break; 2223 case '<': 2224 retval = sbuf_printf(sb, "<"); 2225 break; 2226 default: 2227 retval = sbuf_putc(sb, *str); 2228 break; 2229 } 2230 2231 if (retval != 0) 2232 break; 2233 2234 } 2235 2236 return (retval); 2237} 2238 2239static int 2240ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2241 struct thread *td) 2242{ 2243 struct ctl_softc *softc; 2244 int retval; 2245 2246 softc = control_softc; 2247 2248 retval = 0; 2249 2250 switch (cmd) { 2251 case CTL_IO: { 2252 union ctl_io *io; 2253 void *pool_tmp; 2254 2255 /* 2256 * If we haven't been "enabled", don't allow any SCSI I/O 2257 * to this FETD. 2258 */ 2259 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2260 retval = EPERM; 2261 break; 2262 } 2263 2264 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2265 if (io == NULL) { 2266 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2267 retval = ENOSPC; 2268 break; 2269 } 2270 2271 /* 2272 * Need to save the pool reference so it doesn't get 2273 * spammed by the user's ctl_io. 2274 */ 2275 pool_tmp = io->io_hdr.pool; 2276 2277 memcpy(io, (void *)addr, sizeof(*io)); 2278 2279 io->io_hdr.pool = pool_tmp; 2280 /* 2281 * No status yet, so make sure the status is set properly. 2282 */ 2283 io->io_hdr.status = CTL_STATUS_NONE; 2284 2285 /* 2286 * The user sets the initiator ID, target and LUN IDs. 2287 */ 2288 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2289 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2290 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2291 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2292 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2293 2294 retval = ctl_ioctl_submit_wait(io); 2295 2296 if (retval != 0) { 2297 ctl_free_io(io); 2298 break; 2299 } 2300 2301 memcpy((void *)addr, io, sizeof(*io)); 2302 2303 /* return this to our pool */ 2304 ctl_free_io(io); 2305 2306 break; 2307 } 2308 case CTL_ENABLE_PORT: 2309 case CTL_DISABLE_PORT: 2310 case CTL_SET_PORT_WWNS: { 2311 struct ctl_port *port; 2312 struct ctl_port_entry *entry; 2313 2314 entry = (struct ctl_port_entry *)addr; 2315 2316 mtx_lock(&softc->ctl_lock); 2317 STAILQ_FOREACH(port, &softc->port_list, links) { 2318 int action, done; 2319 2320 action = 0; 2321 done = 0; 2322 2323 if ((entry->port_type == CTL_PORT_NONE) 2324 && (entry->targ_port == port->targ_port)) { 2325 /* 2326 * If the user only wants to enable or 2327 * disable or set WWNs on a specific port, 2328 * do the operation and we're done. 2329 */ 2330 action = 1; 2331 done = 1; 2332 } else if (entry->port_type & port->port_type) { 2333 /* 2334 * Compare the user's type mask with the 2335 * particular frontend type to see if we 2336 * have a match. 2337 */ 2338 action = 1; 2339 done = 0; 2340 2341 /* 2342 * Make sure the user isn't trying to set 2343 * WWNs on multiple ports at the same time. 2344 */ 2345 if (cmd == CTL_SET_PORT_WWNS) { 2346 printf("%s: Can't set WWNs on " 2347 "multiple ports\n", __func__); 2348 retval = EINVAL; 2349 break; 2350 } 2351 } 2352 if (action != 0) { 2353 /* 2354 * XXX KDM we have to drop the lock here, 2355 * because the online/offline operations 2356 * can potentially block. We need to 2357 * reference count the frontends so they 2358 * can't go away, 2359 */ 2360 mtx_unlock(&softc->ctl_lock); 2361 2362 if (cmd == CTL_ENABLE_PORT) { 2363 struct ctl_lun *lun; 2364 2365 STAILQ_FOREACH(lun, &softc->lun_list, 2366 links) { 2367 port->lun_enable(port->targ_lun_arg, 2368 lun->target, 2369 lun->lun); 2370 } 2371 2372 ctl_port_online(port); 2373 } else if (cmd == CTL_DISABLE_PORT) { 2374 struct ctl_lun *lun; 2375 2376 ctl_port_offline(port); 2377 2378 STAILQ_FOREACH(lun, &softc->lun_list, 2379 links) { 2380 port->lun_disable( 2381 port->targ_lun_arg, 2382 lun->target, 2383 lun->lun); 2384 } 2385 } 2386 2387 mtx_lock(&softc->ctl_lock); 2388 2389 if (cmd == CTL_SET_PORT_WWNS) 2390 ctl_port_set_wwns(port, 2391 (entry->flags & CTL_PORT_WWNN_VALID) ? 2392 1 : 0, entry->wwnn, 2393 (entry->flags & CTL_PORT_WWPN_VALID) ? 2394 1 : 0, entry->wwpn); 2395 } 2396 if (done != 0) 2397 break; 2398 } 2399 mtx_unlock(&softc->ctl_lock); 2400 break; 2401 } 2402 case CTL_GET_PORT_LIST: { 2403 struct ctl_port *port; 2404 struct ctl_port_list *list; 2405 int i; 2406 2407 list = (struct ctl_port_list *)addr; 2408 2409 if (list->alloc_len != (list->alloc_num * 2410 sizeof(struct ctl_port_entry))) { 2411 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2412 "alloc_num %u * sizeof(struct ctl_port_entry) " 2413 "%zu\n", __func__, list->alloc_len, 2414 list->alloc_num, sizeof(struct ctl_port_entry)); 2415 retval = EINVAL; 2416 break; 2417 } 2418 list->fill_len = 0; 2419 list->fill_num = 0; 2420 list->dropped_num = 0; 2421 i = 0; 2422 mtx_lock(&softc->ctl_lock); 2423 STAILQ_FOREACH(port, &softc->port_list, links) { 2424 struct ctl_port_entry entry, *list_entry; 2425 2426 if (list->fill_num >= list->alloc_num) { 2427 list->dropped_num++; 2428 continue; 2429 } 2430 2431 entry.port_type = port->port_type; 2432 strlcpy(entry.port_name, port->port_name, 2433 sizeof(entry.port_name)); 2434 entry.targ_port = port->targ_port; 2435 entry.physical_port = port->physical_port; 2436 entry.virtual_port = port->virtual_port; 2437 entry.wwnn = port->wwnn; 2438 entry.wwpn = port->wwpn; 2439 if (port->status & CTL_PORT_STATUS_ONLINE) 2440 entry.online = 1; 2441 else 2442 entry.online = 0; 2443 2444 list_entry = &list->entries[i]; 2445 2446 retval = copyout(&entry, list_entry, sizeof(entry)); 2447 if (retval != 0) { 2448 printf("%s: CTL_GET_PORT_LIST: copyout " 2449 "returned %d\n", __func__, retval); 2450 break; 2451 } 2452 i++; 2453 list->fill_num++; 2454 list->fill_len += sizeof(entry); 2455 } 2456 mtx_unlock(&softc->ctl_lock); 2457 2458 /* 2459 * If this is non-zero, we had a copyout fault, so there's 2460 * probably no point in attempting to set the status inside 2461 * the structure. 2462 */ 2463 if (retval != 0) 2464 break; 2465 2466 if (list->dropped_num > 0) 2467 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2468 else 2469 list->status = CTL_PORT_LIST_OK; 2470 break; 2471 } 2472 case CTL_DUMP_OOA: { 2473 struct ctl_lun *lun; 2474 union ctl_io *io; 2475 char printbuf[128]; 2476 struct sbuf sb; 2477 2478 mtx_lock(&softc->ctl_lock); 2479 printf("Dumping OOA queues:\n"); 2480 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2481 mtx_lock(&lun->lun_lock); 2482 for (io = (union ctl_io *)TAILQ_FIRST( 2483 &lun->ooa_queue); io != NULL; 2484 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2485 ooa_links)) { 2486 sbuf_new(&sb, printbuf, sizeof(printbuf), 2487 SBUF_FIXEDLEN); 2488 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2489 (intmax_t)lun->lun, 2490 io->scsiio.tag_num, 2491 (io->io_hdr.flags & 2492 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2493 (io->io_hdr.flags & 2494 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2495 (io->io_hdr.flags & 2496 CTL_FLAG_ABORT) ? " ABORT" : "", 2497 (io->io_hdr.flags & 2498 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2499 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2500 sbuf_finish(&sb); 2501 printf("%s\n", sbuf_data(&sb)); 2502 } 2503 mtx_unlock(&lun->lun_lock); 2504 } 2505 printf("OOA queues dump done\n"); 2506 mtx_unlock(&softc->ctl_lock); 2507 break; 2508 } 2509 case CTL_GET_OOA: { 2510 struct ctl_lun *lun; 2511 struct ctl_ooa *ooa_hdr; 2512 struct ctl_ooa_entry *entries; 2513 uint32_t cur_fill_num; 2514 2515 ooa_hdr = (struct ctl_ooa *)addr; 2516 2517 if ((ooa_hdr->alloc_len == 0) 2518 || (ooa_hdr->alloc_num == 0)) { 2519 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2520 "must be non-zero\n", __func__, 2521 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2522 retval = EINVAL; 2523 break; 2524 } 2525 2526 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2527 sizeof(struct ctl_ooa_entry))) { 2528 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2529 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2530 __func__, ooa_hdr->alloc_len, 2531 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2532 retval = EINVAL; 2533 break; 2534 } 2535 2536 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2537 if (entries == NULL) { 2538 printf("%s: could not allocate %d bytes for OOA " 2539 "dump\n", __func__, ooa_hdr->alloc_len); 2540 retval = ENOMEM; 2541 break; 2542 } 2543 2544 mtx_lock(&softc->ctl_lock); 2545 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2546 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2547 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2548 mtx_unlock(&softc->ctl_lock); 2549 free(entries, M_CTL); 2550 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2551 __func__, (uintmax_t)ooa_hdr->lun_num); 2552 retval = EINVAL; 2553 break; 2554 } 2555 2556 cur_fill_num = 0; 2557 2558 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2559 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2560 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2561 ooa_hdr, entries); 2562 if (retval != 0) 2563 break; 2564 } 2565 if (retval != 0) { 2566 mtx_unlock(&softc->ctl_lock); 2567 free(entries, M_CTL); 2568 break; 2569 } 2570 } else { 2571 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2572 2573 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2574 entries); 2575 } 2576 mtx_unlock(&softc->ctl_lock); 2577 2578 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2579 ooa_hdr->fill_len = ooa_hdr->fill_num * 2580 sizeof(struct ctl_ooa_entry); 2581 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2582 if (retval != 0) { 2583 printf("%s: error copying out %d bytes for OOA dump\n", 2584 __func__, ooa_hdr->fill_len); 2585 } 2586 2587 getbintime(&ooa_hdr->cur_bt); 2588 2589 if (cur_fill_num > ooa_hdr->alloc_num) { 2590 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2591 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2592 } else { 2593 ooa_hdr->dropped_num = 0; 2594 ooa_hdr->status = CTL_OOA_OK; 2595 } 2596 2597 free(entries, M_CTL); 2598 break; 2599 } 2600 case CTL_CHECK_OOA: { 2601 union ctl_io *io; 2602 struct ctl_lun *lun; 2603 struct ctl_ooa_info *ooa_info; 2604 2605 2606 ooa_info = (struct ctl_ooa_info *)addr; 2607 2608 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2609 ooa_info->status = CTL_OOA_INVALID_LUN; 2610 break; 2611 } 2612 mtx_lock(&softc->ctl_lock); 2613 lun = softc->ctl_luns[ooa_info->lun_id]; 2614 if (lun == NULL) { 2615 mtx_unlock(&softc->ctl_lock); 2616 ooa_info->status = CTL_OOA_INVALID_LUN; 2617 break; 2618 } 2619 mtx_lock(&lun->lun_lock); 2620 mtx_unlock(&softc->ctl_lock); 2621 ooa_info->num_entries = 0; 2622 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2623 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2624 &io->io_hdr, ooa_links)) { 2625 ooa_info->num_entries++; 2626 } 2627 mtx_unlock(&lun->lun_lock); 2628 2629 ooa_info->status = CTL_OOA_SUCCESS; 2630 2631 break; 2632 } 2633 case CTL_HARD_START: 2634 case CTL_HARD_STOP: { 2635 struct ctl_fe_ioctl_startstop_info ss_info; 2636 struct cfi_metatask *metatask; 2637 struct mtx hs_mtx; 2638 2639 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2640 2641 cv_init(&ss_info.sem, "hard start/stop cv" ); 2642 2643 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2644 if (metatask == NULL) { 2645 retval = ENOMEM; 2646 mtx_destroy(&hs_mtx); 2647 break; 2648 } 2649 2650 if (cmd == CTL_HARD_START) 2651 metatask->tasktype = CFI_TASK_STARTUP; 2652 else 2653 metatask->tasktype = CFI_TASK_SHUTDOWN; 2654 2655 metatask->callback = ctl_ioctl_hard_startstop_callback; 2656 metatask->callback_arg = &ss_info; 2657 2658 cfi_action(metatask); 2659 2660 /* Wait for the callback */ 2661 mtx_lock(&hs_mtx); 2662 cv_wait_sig(&ss_info.sem, &hs_mtx); 2663 mtx_unlock(&hs_mtx); 2664 2665 /* 2666 * All information has been copied from the metatask by the 2667 * time cv_broadcast() is called, so we free the metatask here. 2668 */ 2669 cfi_free_metatask(metatask); 2670 2671 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2672 2673 mtx_destroy(&hs_mtx); 2674 break; 2675 } 2676 case CTL_BBRREAD: { 2677 struct ctl_bbrread_info *bbr_info; 2678 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2679 struct mtx bbr_mtx; 2680 struct cfi_metatask *metatask; 2681 2682 bbr_info = (struct ctl_bbrread_info *)addr; 2683 2684 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2685 2686 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2687 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2688 2689 fe_bbr_info.bbr_info = bbr_info; 2690 fe_bbr_info.lock = &bbr_mtx; 2691 2692 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2693 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2694 2695 if (metatask == NULL) { 2696 mtx_destroy(&bbr_mtx); 2697 cv_destroy(&fe_bbr_info.sem); 2698 retval = ENOMEM; 2699 break; 2700 } 2701 metatask->tasktype = CFI_TASK_BBRREAD; 2702 metatask->callback = ctl_ioctl_bbrread_callback; 2703 metatask->callback_arg = &fe_bbr_info; 2704 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2705 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2706 metatask->taskinfo.bbrread.len = bbr_info->len; 2707 2708 cfi_action(metatask); 2709 2710 mtx_lock(&bbr_mtx); 2711 while (fe_bbr_info.wakeup_done == 0) 2712 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2713 mtx_unlock(&bbr_mtx); 2714 2715 bbr_info->status = metatask->status; 2716 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2717 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2718 memcpy(&bbr_info->sense_data, 2719 &metatask->taskinfo.bbrread.sense_data, 2720 ctl_min(sizeof(bbr_info->sense_data), 2721 sizeof(metatask->taskinfo.bbrread.sense_data))); 2722 2723 cfi_free_metatask(metatask); 2724 2725 mtx_destroy(&bbr_mtx); 2726 cv_destroy(&fe_bbr_info.sem); 2727 2728 break; 2729 } 2730 case CTL_DELAY_IO: { 2731 struct ctl_io_delay_info *delay_info; 2732#ifdef CTL_IO_DELAY 2733 struct ctl_lun *lun; 2734#endif /* CTL_IO_DELAY */ 2735 2736 delay_info = (struct ctl_io_delay_info *)addr; 2737 2738#ifdef CTL_IO_DELAY 2739 mtx_lock(&softc->ctl_lock); 2740 2741 if ((delay_info->lun_id > CTL_MAX_LUNS) 2742 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2743 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2744 } else { 2745 lun = softc->ctl_luns[delay_info->lun_id]; 2746 mtx_lock(&lun->lun_lock); 2747 2748 delay_info->status = CTL_DELAY_STATUS_OK; 2749 2750 switch (delay_info->delay_type) { 2751 case CTL_DELAY_TYPE_CONT: 2752 break; 2753 case CTL_DELAY_TYPE_ONESHOT: 2754 break; 2755 default: 2756 delay_info->status = 2757 CTL_DELAY_STATUS_INVALID_TYPE; 2758 break; 2759 } 2760 2761 switch (delay_info->delay_loc) { 2762 case CTL_DELAY_LOC_DATAMOVE: 2763 lun->delay_info.datamove_type = 2764 delay_info->delay_type; 2765 lun->delay_info.datamove_delay = 2766 delay_info->delay_secs; 2767 break; 2768 case CTL_DELAY_LOC_DONE: 2769 lun->delay_info.done_type = 2770 delay_info->delay_type; 2771 lun->delay_info.done_delay = 2772 delay_info->delay_secs; 2773 break; 2774 default: 2775 delay_info->status = 2776 CTL_DELAY_STATUS_INVALID_LOC; 2777 break; 2778 } 2779 mtx_unlock(&lun->lun_lock); 2780 } 2781 2782 mtx_unlock(&softc->ctl_lock); 2783#else 2784 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2785#endif /* CTL_IO_DELAY */ 2786 break; 2787 } 2788 case CTL_REALSYNC_SET: { 2789 int *syncstate; 2790 2791 syncstate = (int *)addr; 2792 2793 mtx_lock(&softc->ctl_lock); 2794 switch (*syncstate) { 2795 case 0: 2796 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2797 break; 2798 case 1: 2799 softc->flags |= CTL_FLAG_REAL_SYNC; 2800 break; 2801 default: 2802 retval = EINVAL; 2803 break; 2804 } 2805 mtx_unlock(&softc->ctl_lock); 2806 break; 2807 } 2808 case CTL_REALSYNC_GET: { 2809 int *syncstate; 2810 2811 syncstate = (int*)addr; 2812 2813 mtx_lock(&softc->ctl_lock); 2814 if (softc->flags & CTL_FLAG_REAL_SYNC) 2815 *syncstate = 1; 2816 else 2817 *syncstate = 0; 2818 mtx_unlock(&softc->ctl_lock); 2819 2820 break; 2821 } 2822 case CTL_SETSYNC: 2823 case CTL_GETSYNC: { 2824 struct ctl_sync_info *sync_info; 2825 struct ctl_lun *lun; 2826 2827 sync_info = (struct ctl_sync_info *)addr; 2828 2829 mtx_lock(&softc->ctl_lock); 2830 lun = softc->ctl_luns[sync_info->lun_id]; 2831 if (lun == NULL) { 2832 mtx_unlock(&softc->ctl_lock); 2833 sync_info->status = CTL_GS_SYNC_NO_LUN; 2834 } 2835 /* 2836 * Get or set the sync interval. We're not bounds checking 2837 * in the set case, hopefully the user won't do something 2838 * silly. 2839 */ 2840 mtx_lock(&lun->lun_lock); 2841 mtx_unlock(&softc->ctl_lock); 2842 if (cmd == CTL_GETSYNC) 2843 sync_info->sync_interval = lun->sync_interval; 2844 else 2845 lun->sync_interval = sync_info->sync_interval; 2846 mtx_unlock(&lun->lun_lock); 2847 2848 sync_info->status = CTL_GS_SYNC_OK; 2849 2850 break; 2851 } 2852 case CTL_GETSTATS: { 2853 struct ctl_stats *stats; 2854 struct ctl_lun *lun; 2855 int i; 2856 2857 stats = (struct ctl_stats *)addr; 2858 2859 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2860 stats->alloc_len) { 2861 stats->status = CTL_SS_NEED_MORE_SPACE; 2862 stats->num_luns = softc->num_luns; 2863 break; 2864 } 2865 /* 2866 * XXX KDM no locking here. If the LUN list changes, 2867 * things can blow up. 2868 */ 2869 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2870 i++, lun = STAILQ_NEXT(lun, links)) { 2871 retval = copyout(&lun->stats, &stats->lun_stats[i], 2872 sizeof(lun->stats)); 2873 if (retval != 0) 2874 break; 2875 } 2876 stats->num_luns = softc->num_luns; 2877 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2878 softc->num_luns; 2879 stats->status = CTL_SS_OK; 2880#ifdef CTL_TIME_IO 2881 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2882#else 2883 stats->flags = CTL_STATS_FLAG_NONE; 2884#endif 2885 getnanouptime(&stats->timestamp); 2886 break; 2887 } 2888 case CTL_ERROR_INJECT: { 2889 struct ctl_error_desc *err_desc, *new_err_desc; 2890 struct ctl_lun *lun; 2891 2892 err_desc = (struct ctl_error_desc *)addr; 2893 2894 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2895 M_WAITOK | M_ZERO); 2896 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2897 2898 mtx_lock(&softc->ctl_lock); 2899 lun = softc->ctl_luns[err_desc->lun_id]; 2900 if (lun == NULL) { 2901 mtx_unlock(&softc->ctl_lock); 2902 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2903 __func__, (uintmax_t)err_desc->lun_id); 2904 retval = EINVAL; 2905 break; 2906 } 2907 mtx_lock(&lun->lun_lock); 2908 mtx_unlock(&softc->ctl_lock); 2909 2910 /* 2911 * We could do some checking here to verify the validity 2912 * of the request, but given the complexity of error 2913 * injection requests, the checking logic would be fairly 2914 * complex. 2915 * 2916 * For now, if the request is invalid, it just won't get 2917 * executed and might get deleted. 2918 */ 2919 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2920 2921 /* 2922 * XXX KDM check to make sure the serial number is unique, 2923 * in case we somehow manage to wrap. That shouldn't 2924 * happen for a very long time, but it's the right thing to 2925 * do. 2926 */ 2927 new_err_desc->serial = lun->error_serial; 2928 err_desc->serial = lun->error_serial; 2929 lun->error_serial++; 2930 2931 mtx_unlock(&lun->lun_lock); 2932 break; 2933 } 2934 case CTL_ERROR_INJECT_DELETE: { 2935 struct ctl_error_desc *delete_desc, *desc, *desc2; 2936 struct ctl_lun *lun; 2937 int delete_done; 2938 2939 delete_desc = (struct ctl_error_desc *)addr; 2940 delete_done = 0; 2941 2942 mtx_lock(&softc->ctl_lock); 2943 lun = softc->ctl_luns[delete_desc->lun_id]; 2944 if (lun == NULL) { 2945 mtx_unlock(&softc->ctl_lock); 2946 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2947 __func__, (uintmax_t)delete_desc->lun_id); 2948 retval = EINVAL; 2949 break; 2950 } 2951 mtx_lock(&lun->lun_lock); 2952 mtx_unlock(&softc->ctl_lock); 2953 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2954 if (desc->serial != delete_desc->serial) 2955 continue; 2956 2957 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2958 links); 2959 free(desc, M_CTL); 2960 delete_done = 1; 2961 } 2962 mtx_unlock(&lun->lun_lock); 2963 if (delete_done == 0) { 2964 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2965 "error serial %ju on LUN %u\n", __func__, 2966 delete_desc->serial, delete_desc->lun_id); 2967 retval = EINVAL; 2968 break; 2969 } 2970 break; 2971 } 2972 case CTL_DUMP_STRUCTS: { 2973 int i, j, k, idx; 2974 struct ctl_port *port; 2975 struct ctl_frontend *fe; 2976 2977 mtx_lock(&softc->ctl_lock); 2978 printf("CTL Persistent Reservation information start:\n"); 2979 for (i = 0; i < CTL_MAX_LUNS; i++) { 2980 struct ctl_lun *lun; 2981 2982 lun = softc->ctl_luns[i]; 2983 2984 if ((lun == NULL) 2985 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2986 continue; 2987 2988 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2989 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2990 idx = j * CTL_MAX_INIT_PER_PORT + k; 2991 if (lun->per_res[idx].registered == 0) 2992 continue; 2993 printf(" LUN %d port %d iid %d key " 2994 "%#jx\n", i, j, k, 2995 (uintmax_t)scsi_8btou64( 2996 lun->per_res[idx].res_key.key)); 2997 } 2998 } 2999 } 3000 printf("CTL Persistent Reservation information end\n"); 3001 printf("CTL Ports:\n"); 3002 STAILQ_FOREACH(port, &softc->port_list, links) { 3003 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3004 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3005 port->frontend->name, port->port_type, 3006 port->physical_port, port->virtual_port, 3007 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3008 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3009 if (port->wwpn_iid[j].in_use == 0 && 3010 port->wwpn_iid[j].wwpn == 0 && 3011 port->wwpn_iid[j].name == NULL) 3012 continue; 3013 3014 printf(" iid %u use %d WWPN %#jx '%s'\n", 3015 j, port->wwpn_iid[j].in_use, 3016 (uintmax_t)port->wwpn_iid[j].wwpn, 3017 port->wwpn_iid[j].name); 3018 } 3019 } 3020 printf("CTL Port information end\n"); 3021 mtx_unlock(&softc->ctl_lock); 3022 /* 3023 * XXX KDM calling this without a lock. We'd likely want 3024 * to drop the lock before calling the frontend's dump 3025 * routine anyway. 3026 */ 3027 printf("CTL Frontends:\n"); 3028 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3029 printf(" Frontend '%s'\n", fe->name); 3030 if (fe->fe_dump != NULL) 3031 fe->fe_dump(); 3032 } 3033 printf("CTL Frontend information end\n"); 3034 break; 3035 } 3036 case CTL_LUN_REQ: { 3037 struct ctl_lun_req *lun_req; 3038 struct ctl_backend_driver *backend; 3039 3040 lun_req = (struct ctl_lun_req *)addr; 3041 3042 backend = ctl_backend_find(lun_req->backend); 3043 if (backend == NULL) { 3044 lun_req->status = CTL_LUN_ERROR; 3045 snprintf(lun_req->error_str, 3046 sizeof(lun_req->error_str), 3047 "Backend \"%s\" not found.", 3048 lun_req->backend); 3049 break; 3050 } 3051 if (lun_req->num_be_args > 0) { 3052 lun_req->kern_be_args = ctl_copyin_args( 3053 lun_req->num_be_args, 3054 lun_req->be_args, 3055 lun_req->error_str, 3056 sizeof(lun_req->error_str)); 3057 if (lun_req->kern_be_args == NULL) { 3058 lun_req->status = CTL_LUN_ERROR; 3059 break; 3060 } 3061 } 3062 3063 retval = backend->ioctl(dev, cmd, addr, flag, td); 3064 3065 if (lun_req->num_be_args > 0) { 3066 ctl_copyout_args(lun_req->num_be_args, 3067 lun_req->kern_be_args); 3068 ctl_free_args(lun_req->num_be_args, 3069 lun_req->kern_be_args); 3070 } 3071 break; 3072 } 3073 case CTL_LUN_LIST: { 3074 struct sbuf *sb; 3075 struct ctl_lun *lun; 3076 struct ctl_lun_list *list; 3077 struct ctl_option *opt; 3078 3079 list = (struct ctl_lun_list *)addr; 3080 3081 /* 3082 * Allocate a fixed length sbuf here, based on the length 3083 * of the user's buffer. We could allocate an auto-extending 3084 * buffer, and then tell the user how much larger our 3085 * amount of data is than his buffer, but that presents 3086 * some problems: 3087 * 3088 * 1. The sbuf(9) routines use a blocking malloc, and so 3089 * we can't hold a lock while calling them with an 3090 * auto-extending buffer. 3091 * 3092 * 2. There is not currently a LUN reference counting 3093 * mechanism, outside of outstanding transactions on 3094 * the LUN's OOA queue. So a LUN could go away on us 3095 * while we're getting the LUN number, backend-specific 3096 * information, etc. Thus, given the way things 3097 * currently work, we need to hold the CTL lock while 3098 * grabbing LUN information. 3099 * 3100 * So, from the user's standpoint, the best thing to do is 3101 * allocate what he thinks is a reasonable buffer length, 3102 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3103 * double the buffer length and try again. (And repeat 3104 * that until he succeeds.) 3105 */ 3106 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3107 if (sb == NULL) { 3108 list->status = CTL_LUN_LIST_ERROR; 3109 snprintf(list->error_str, sizeof(list->error_str), 3110 "Unable to allocate %d bytes for LUN list", 3111 list->alloc_len); 3112 break; 3113 } 3114 3115 sbuf_printf(sb, "<ctllunlist>\n"); 3116 3117 mtx_lock(&softc->ctl_lock); 3118 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3119 mtx_lock(&lun->lun_lock); 3120 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3121 (uintmax_t)lun->lun); 3122 3123 /* 3124 * Bail out as soon as we see that we've overfilled 3125 * the buffer. 3126 */ 3127 if (retval != 0) 3128 break; 3129 3130 retval = sbuf_printf(sb, "\t<backend_type>%s" 3131 "</backend_type>\n", 3132 (lun->backend == NULL) ? "none" : 3133 lun->backend->name); 3134 3135 if (retval != 0) 3136 break; 3137 3138 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3139 lun->be_lun->lun_type); 3140 3141 if (retval != 0) 3142 break; 3143 3144 if (lun->backend == NULL) { 3145 retval = sbuf_printf(sb, "</lun>\n"); 3146 if (retval != 0) 3147 break; 3148 continue; 3149 } 3150 3151 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3152 (lun->be_lun->maxlba > 0) ? 3153 lun->be_lun->maxlba + 1 : 0); 3154 3155 if (retval != 0) 3156 break; 3157 3158 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3159 lun->be_lun->blocksize); 3160 3161 if (retval != 0) 3162 break; 3163 3164 retval = sbuf_printf(sb, "\t<serial_number>"); 3165 3166 if (retval != 0) 3167 break; 3168 3169 retval = ctl_sbuf_printf_esc(sb, 3170 lun->be_lun->serial_num); 3171 3172 if (retval != 0) 3173 break; 3174 3175 retval = sbuf_printf(sb, "</serial_number>\n"); 3176 3177 if (retval != 0) 3178 break; 3179 3180 retval = sbuf_printf(sb, "\t<device_id>"); 3181 3182 if (retval != 0) 3183 break; 3184 3185 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3186 3187 if (retval != 0) 3188 break; 3189 3190 retval = sbuf_printf(sb, "</device_id>\n"); 3191 3192 if (retval != 0) 3193 break; 3194 3195 if (lun->backend->lun_info != NULL) { 3196 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3197 if (retval != 0) 3198 break; 3199 } 3200 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3201 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3202 opt->name, opt->value, opt->name); 3203 if (retval != 0) 3204 break; 3205 } 3206 3207 retval = sbuf_printf(sb, "</lun>\n"); 3208 3209 if (retval != 0) 3210 break; 3211 mtx_unlock(&lun->lun_lock); 3212 } 3213 if (lun != NULL) 3214 mtx_unlock(&lun->lun_lock); 3215 mtx_unlock(&softc->ctl_lock); 3216 3217 if ((retval != 0) 3218 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3219 retval = 0; 3220 sbuf_delete(sb); 3221 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3222 snprintf(list->error_str, sizeof(list->error_str), 3223 "Out of space, %d bytes is too small", 3224 list->alloc_len); 3225 break; 3226 } 3227 3228 sbuf_finish(sb); 3229 3230 retval = copyout(sbuf_data(sb), list->lun_xml, 3231 sbuf_len(sb) + 1); 3232 3233 list->fill_len = sbuf_len(sb) + 1; 3234 list->status = CTL_LUN_LIST_OK; 3235 sbuf_delete(sb); 3236 break; 3237 } 3238 case CTL_ISCSI: { 3239 struct ctl_iscsi *ci; 3240 struct ctl_frontend *fe; 3241 3242 ci = (struct ctl_iscsi *)addr; 3243 3244 fe = ctl_frontend_find("iscsi"); 3245 if (fe == NULL) { 3246 ci->status = CTL_ISCSI_ERROR; 3247 snprintf(ci->error_str, sizeof(ci->error_str), 3248 "Frontend \"iscsi\" not found."); 3249 break; 3250 } 3251 3252 retval = fe->ioctl(dev, cmd, addr, flag, td); 3253 break; 3254 } 3255 case CTL_PORT_REQ: { 3256 struct ctl_req *req; 3257 struct ctl_frontend *fe; 3258 3259 req = (struct ctl_req *)addr; 3260 3261 fe = ctl_frontend_find(req->driver); 3262 if (fe == NULL) { 3263 req->status = CTL_LUN_ERROR; 3264 snprintf(req->error_str, sizeof(req->error_str), 3265 "Frontend \"%s\" not found.", req->driver); 3266 break; 3267 } 3268 if (req->num_args > 0) { 3269 req->kern_args = ctl_copyin_args(req->num_args, 3270 req->args, req->error_str, sizeof(req->error_str)); 3271 if (req->kern_args == NULL) { 3272 req->status = CTL_LUN_ERROR; 3273 break; 3274 } 3275 } 3276 3277 retval = fe->ioctl(dev, cmd, addr, flag, td); 3278 3279 if (req->num_args > 0) { 3280 ctl_copyout_args(req->num_args, req->kern_args); 3281 ctl_free_args(req->num_args, req->kern_args); 3282 } 3283 break; 3284 } 3285 case CTL_PORT_LIST: { 3286 struct sbuf *sb; 3287 struct ctl_port *port; 3288 struct ctl_lun_list *list; 3289 struct ctl_option *opt; 3290 3291 list = (struct ctl_lun_list *)addr; 3292 3293 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3294 if (sb == NULL) { 3295 list->status = CTL_LUN_LIST_ERROR; 3296 snprintf(list->error_str, sizeof(list->error_str), 3297 "Unable to allocate %d bytes for LUN list", 3298 list->alloc_len); 3299 break; 3300 } 3301 3302 sbuf_printf(sb, "<ctlportlist>\n"); 3303 3304 mtx_lock(&softc->ctl_lock); 3305 STAILQ_FOREACH(port, &softc->port_list, links) { 3306 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3307 (uintmax_t)port->targ_port); 3308 3309 /* 3310 * Bail out as soon as we see that we've overfilled 3311 * the buffer. 3312 */ 3313 if (retval != 0) 3314 break; 3315 3316 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3317 "</frontend_type>\n", port->frontend->name); 3318 if (retval != 0) 3319 break; 3320 3321 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3322 port->port_type); 3323 if (retval != 0) 3324 break; 3325 3326 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3327 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3328 if (retval != 0) 3329 break; 3330 3331 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3332 port->port_name); 3333 if (retval != 0) 3334 break; 3335 3336 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3337 port->physical_port); 3338 if (retval != 0) 3339 break; 3340 3341 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3342 port->virtual_port); 3343 if (retval != 0) 3344 break; 3345 3346 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3347 (uintmax_t)port->wwnn); 3348 if (retval != 0) 3349 break; 3350 3351 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3352 (uintmax_t)port->wwpn); 3353 if (retval != 0) 3354 break; 3355 3356 if (port->port_info != NULL) { 3357 retval = port->port_info(port->onoff_arg, sb); 3358 if (retval != 0) 3359 break; 3360 } 3361 STAILQ_FOREACH(opt, &port->options, links) { 3362 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3363 opt->name, opt->value, opt->name); 3364 if (retval != 0) 3365 break; 3366 } 3367 3368 retval = sbuf_printf(sb, "</targ_port>\n"); 3369 if (retval != 0) 3370 break; 3371 } 3372 mtx_unlock(&softc->ctl_lock); 3373 3374 if ((retval != 0) 3375 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3376 retval = 0; 3377 sbuf_delete(sb); 3378 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3379 snprintf(list->error_str, sizeof(list->error_str), 3380 "Out of space, %d bytes is too small", 3381 list->alloc_len); 3382 break; 3383 } 3384 3385 sbuf_finish(sb); 3386 3387 retval = copyout(sbuf_data(sb), list->lun_xml, 3388 sbuf_len(sb) + 1); 3389 3390 list->fill_len = sbuf_len(sb) + 1; 3391 list->status = CTL_LUN_LIST_OK; 3392 sbuf_delete(sb); 3393 break; 3394 } 3395 default: { 3396 /* XXX KDM should we fix this? */ 3397#if 0 3398 struct ctl_backend_driver *backend; 3399 unsigned int type; 3400 int found; 3401 3402 found = 0; 3403 3404 /* 3405 * We encode the backend type as the ioctl type for backend 3406 * ioctls. So parse it out here, and then search for a 3407 * backend of this type. 3408 */ 3409 type = _IOC_TYPE(cmd); 3410 3411 STAILQ_FOREACH(backend, &softc->be_list, links) { 3412 if (backend->type == type) { 3413 found = 1; 3414 break; 3415 } 3416 } 3417 if (found == 0) { 3418 printf("ctl: unknown ioctl command %#lx or backend " 3419 "%d\n", cmd, type); 3420 retval = EINVAL; 3421 break; 3422 } 3423 retval = backend->ioctl(dev, cmd, addr, flag, td); 3424#endif 3425 retval = ENOTTY; 3426 break; 3427 } 3428 } 3429 return (retval); 3430} 3431 3432uint32_t 3433ctl_get_initindex(struct ctl_nexus *nexus) 3434{ 3435 if (nexus->targ_port < CTL_MAX_PORTS) 3436 return (nexus->initid.id + 3437 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3438 else 3439 return (nexus->initid.id + 3440 ((nexus->targ_port - CTL_MAX_PORTS) * 3441 CTL_MAX_INIT_PER_PORT)); 3442} 3443 3444uint32_t 3445ctl_get_resindex(struct ctl_nexus *nexus) 3446{ 3447 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3448} 3449 3450uint32_t 3451ctl_port_idx(int port_num) 3452{ 3453 if (port_num < CTL_MAX_PORTS) 3454 return(port_num); 3455 else 3456 return(port_num - CTL_MAX_PORTS); 3457} 3458 3459static uint32_t 3460ctl_map_lun(int port_num, uint32_t lun_id) 3461{ 3462 struct ctl_port *port; 3463 3464 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3465 if (port == NULL) 3466 return (UINT32_MAX); 3467 if (port->lun_map == NULL) 3468 return (lun_id); 3469 return (port->lun_map(port->targ_lun_arg, lun_id)); 3470} 3471 3472static uint32_t 3473ctl_map_lun_back(int port_num, uint32_t lun_id) 3474{ 3475 struct ctl_port *port; 3476 uint32_t i; 3477 3478 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3479 if (port->lun_map == NULL) 3480 return (lun_id); 3481 for (i = 0; i < CTL_MAX_LUNS; i++) { 3482 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3483 return (i); 3484 } 3485 return (UINT32_MAX); 3486} 3487 3488/* 3489 * Note: This only works for bitmask sizes that are at least 32 bits, and 3490 * that are a power of 2. 3491 */ 3492int 3493ctl_ffz(uint32_t *mask, uint32_t size) 3494{ 3495 uint32_t num_chunks, num_pieces; 3496 int i, j; 3497 3498 num_chunks = (size >> 5); 3499 if (num_chunks == 0) 3500 num_chunks++; 3501 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3502 3503 for (i = 0; i < num_chunks; i++) { 3504 for (j = 0; j < num_pieces; j++) { 3505 if ((mask[i] & (1 << j)) == 0) 3506 return ((i << 5) + j); 3507 } 3508 } 3509 3510 return (-1); 3511} 3512 3513int 3514ctl_set_mask(uint32_t *mask, uint32_t bit) 3515{ 3516 uint32_t chunk, piece; 3517 3518 chunk = bit >> 5; 3519 piece = bit % (sizeof(uint32_t) * 8); 3520 3521 if ((mask[chunk] & (1 << piece)) != 0) 3522 return (-1); 3523 else 3524 mask[chunk] |= (1 << piece); 3525 3526 return (0); 3527} 3528 3529int 3530ctl_clear_mask(uint32_t *mask, uint32_t bit) 3531{ 3532 uint32_t chunk, piece; 3533 3534 chunk = bit >> 5; 3535 piece = bit % (sizeof(uint32_t) * 8); 3536 3537 if ((mask[chunk] & (1 << piece)) == 0) 3538 return (-1); 3539 else 3540 mask[chunk] &= ~(1 << piece); 3541 3542 return (0); 3543} 3544 3545int 3546ctl_is_set(uint32_t *mask, uint32_t bit) 3547{ 3548 uint32_t chunk, piece; 3549 3550 chunk = bit >> 5; 3551 piece = bit % (sizeof(uint32_t) * 8); 3552 3553 if ((mask[chunk] & (1 << piece)) == 0) 3554 return (0); 3555 else 3556 return (1); 3557} 3558 3559#ifdef unused 3560/* 3561 * The bus, target and lun are optional, they can be filled in later. 3562 * can_wait is used to determine whether we can wait on the malloc or not. 3563 */ 3564union ctl_io* 3565ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3566 uint32_t targ_lun, int can_wait) 3567{ 3568 union ctl_io *io; 3569 3570 if (can_wait) 3571 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3572 else 3573 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3574 3575 if (io != NULL) { 3576 io->io_hdr.io_type = io_type; 3577 io->io_hdr.targ_port = targ_port; 3578 /* 3579 * XXX KDM this needs to change/go away. We need to move 3580 * to a preallocated pool of ctl_scsiio structures. 3581 */ 3582 io->io_hdr.nexus.targ_target.id = targ_target; 3583 io->io_hdr.nexus.targ_lun = targ_lun; 3584 } 3585 3586 return (io); 3587} 3588 3589void 3590ctl_kfree_io(union ctl_io *io) 3591{ 3592 free(io, M_CTL); 3593} 3594#endif /* unused */ 3595 3596/* 3597 * ctl_softc, pool_type, total_ctl_io are passed in. 3598 * npool is passed out. 3599 */ 3600int 3601ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3602 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3603{ 3604 uint32_t i; 3605 union ctl_io *cur_io, *next_io; 3606 struct ctl_io_pool *pool; 3607 int retval; 3608 3609 retval = 0; 3610 3611 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3612 M_NOWAIT | M_ZERO); 3613 if (pool == NULL) { 3614 retval = ENOMEM; 3615 goto bailout; 3616 } 3617 3618 pool->type = pool_type; 3619 pool->ctl_softc = ctl_softc; 3620 3621 mtx_lock(&ctl_softc->pool_lock); 3622 pool->id = ctl_softc->cur_pool_id++; 3623 mtx_unlock(&ctl_softc->pool_lock); 3624 3625 pool->flags = CTL_POOL_FLAG_NONE; 3626 pool->refcount = 1; /* Reference for validity. */ 3627 STAILQ_INIT(&pool->free_queue); 3628 3629 /* 3630 * XXX KDM other options here: 3631 * - allocate a page at a time 3632 * - allocate one big chunk of memory. 3633 * Page allocation might work well, but would take a little more 3634 * tracking. 3635 */ 3636 for (i = 0; i < total_ctl_io; i++) { 3637 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3638 M_NOWAIT); 3639 if (cur_io == NULL) { 3640 retval = ENOMEM; 3641 break; 3642 } 3643 cur_io->io_hdr.pool = pool; 3644 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3645 pool->total_ctl_io++; 3646 pool->free_ctl_io++; 3647 } 3648 3649 if (retval != 0) { 3650 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3651 cur_io != NULL; cur_io = next_io) { 3652 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3653 links); 3654 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3655 ctl_io_hdr, links); 3656 free(cur_io, M_CTLIO); 3657 } 3658 3659 free(pool, M_CTL); 3660 goto bailout; 3661 } 3662 mtx_lock(&ctl_softc->pool_lock); 3663 ctl_softc->num_pools++; 3664 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3665 /* 3666 * Increment our usage count if this is an external consumer, so we 3667 * can't get unloaded until the external consumer (most likely a 3668 * FETD) unloads and frees his pool. 3669 * 3670 * XXX KDM will this increment the caller's module use count, or 3671 * mine? 3672 */ 3673#if 0 3674 if ((pool_type != CTL_POOL_EMERGENCY) 3675 && (pool_type != CTL_POOL_INTERNAL) 3676 && (pool_type != CTL_POOL_4OTHERSC)) 3677 MOD_INC_USE_COUNT; 3678#endif 3679 3680 mtx_unlock(&ctl_softc->pool_lock); 3681 3682 *npool = pool; 3683 3684bailout: 3685 3686 return (retval); 3687} 3688 3689static int 3690ctl_pool_acquire(struct ctl_io_pool *pool) 3691{ 3692 3693 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3694 3695 if (pool->flags & CTL_POOL_FLAG_INVALID) 3696 return (EINVAL); 3697 3698 pool->refcount++; 3699 3700 return (0); 3701} 3702 3703static void 3704ctl_pool_release(struct ctl_io_pool *pool) 3705{ 3706 struct ctl_softc *ctl_softc = pool->ctl_softc; 3707 union ctl_io *io; 3708 3709 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3710 3711 if (--pool->refcount != 0) 3712 return; 3713 3714 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3715 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3716 links); 3717 free(io, M_CTLIO); 3718 } 3719 3720 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3721 ctl_softc->num_pools--; 3722 3723 /* 3724 * XXX KDM will this decrement the caller's usage count or mine? 3725 */ 3726#if 0 3727 if ((pool->type != CTL_POOL_EMERGENCY) 3728 && (pool->type != CTL_POOL_INTERNAL) 3729 && (pool->type != CTL_POOL_4OTHERSC)) 3730 MOD_DEC_USE_COUNT; 3731#endif 3732 3733 free(pool, M_CTL); 3734} 3735 3736void 3737ctl_pool_free(struct ctl_io_pool *pool) 3738{ 3739 struct ctl_softc *ctl_softc; 3740 3741 if (pool == NULL) 3742 return; 3743 3744 ctl_softc = pool->ctl_softc; 3745 mtx_lock(&ctl_softc->pool_lock); 3746 pool->flags |= CTL_POOL_FLAG_INVALID; 3747 ctl_pool_release(pool); 3748 mtx_unlock(&ctl_softc->pool_lock); 3749} 3750 3751/* 3752 * This routine does not block (except for spinlocks of course). 3753 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3754 * possible. 3755 */ 3756union ctl_io * 3757ctl_alloc_io(void *pool_ref) 3758{ 3759 union ctl_io *io; 3760 struct ctl_softc *ctl_softc; 3761 struct ctl_io_pool *pool, *npool; 3762 struct ctl_io_pool *emergency_pool; 3763 3764 pool = (struct ctl_io_pool *)pool_ref; 3765 3766 if (pool == NULL) { 3767 printf("%s: pool is NULL\n", __func__); 3768 return (NULL); 3769 } 3770 3771 emergency_pool = NULL; 3772 3773 ctl_softc = pool->ctl_softc; 3774 3775 mtx_lock(&ctl_softc->pool_lock); 3776 /* 3777 * First, try to get the io structure from the user's pool. 3778 */ 3779 if (ctl_pool_acquire(pool) == 0) { 3780 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3781 if (io != NULL) { 3782 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3783 pool->total_allocated++; 3784 pool->free_ctl_io--; 3785 mtx_unlock(&ctl_softc->pool_lock); 3786 return (io); 3787 } else 3788 ctl_pool_release(pool); 3789 } 3790 /* 3791 * If he doesn't have any io structures left, search for an 3792 * emergency pool and grab one from there. 3793 */ 3794 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3795 if (npool->type != CTL_POOL_EMERGENCY) 3796 continue; 3797 3798 if (ctl_pool_acquire(npool) != 0) 3799 continue; 3800 3801 emergency_pool = npool; 3802 3803 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3804 if (io != NULL) { 3805 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3806 npool->total_allocated++; 3807 npool->free_ctl_io--; 3808 mtx_unlock(&ctl_softc->pool_lock); 3809 return (io); 3810 } else 3811 ctl_pool_release(npool); 3812 } 3813 3814 /* Drop the spinlock before we malloc */ 3815 mtx_unlock(&ctl_softc->pool_lock); 3816 3817 /* 3818 * The emergency pool (if it exists) didn't have one, so try an 3819 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3820 */ 3821 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3822 if (io != NULL) { 3823 /* 3824 * If the emergency pool exists but is empty, add this 3825 * ctl_io to its list when it gets freed. 3826 */ 3827 if (emergency_pool != NULL) { 3828 mtx_lock(&ctl_softc->pool_lock); 3829 if (ctl_pool_acquire(emergency_pool) == 0) { 3830 io->io_hdr.pool = emergency_pool; 3831 emergency_pool->total_ctl_io++; 3832 /* 3833 * Need to bump this, otherwise 3834 * total_allocated and total_freed won't 3835 * match when we no longer have anything 3836 * outstanding. 3837 */ 3838 emergency_pool->total_allocated++; 3839 } 3840 mtx_unlock(&ctl_softc->pool_lock); 3841 } else 3842 io->io_hdr.pool = NULL; 3843 } 3844 3845 return (io); 3846} 3847 3848void 3849ctl_free_io(union ctl_io *io) 3850{ 3851 if (io == NULL) 3852 return; 3853 3854 /* 3855 * If this ctl_io has a pool, return it to that pool. 3856 */ 3857 if (io->io_hdr.pool != NULL) { 3858 struct ctl_io_pool *pool; 3859 3860 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3861 mtx_lock(&pool->ctl_softc->pool_lock); 3862 io->io_hdr.io_type = 0xff; 3863 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3864 pool->total_freed++; 3865 pool->free_ctl_io++; 3866 ctl_pool_release(pool); 3867 mtx_unlock(&pool->ctl_softc->pool_lock); 3868 } else { 3869 /* 3870 * Otherwise, just free it. We probably malloced it and 3871 * the emergency pool wasn't available. 3872 */ 3873 free(io, M_CTLIO); 3874 } 3875 3876} 3877 3878void 3879ctl_zero_io(union ctl_io *io) 3880{ 3881 void *pool_ref; 3882 3883 if (io == NULL) 3884 return; 3885 3886 /* 3887 * May need to preserve linked list pointers at some point too. 3888 */ 3889 pool_ref = io->io_hdr.pool; 3890 3891 memset(io, 0, sizeof(*io)); 3892 3893 io->io_hdr.pool = pool_ref; 3894} 3895 3896/* 3897 * This routine is currently used for internal copies of ctl_ios that need 3898 * to persist for some reason after we've already returned status to the 3899 * FETD. (Thus the flag set.) 3900 * 3901 * XXX XXX 3902 * Note that this makes a blind copy of all fields in the ctl_io, except 3903 * for the pool reference. This includes any memory that has been 3904 * allocated! That memory will no longer be valid after done has been 3905 * called, so this would be VERY DANGEROUS for command that actually does 3906 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3907 * start and stop commands, which don't transfer any data, so this is not a 3908 * problem. If it is used for anything else, the caller would also need to 3909 * allocate data buffer space and this routine would need to be modified to 3910 * copy the data buffer(s) as well. 3911 */ 3912void 3913ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3914{ 3915 void *pool_ref; 3916 3917 if ((src == NULL) 3918 || (dest == NULL)) 3919 return; 3920 3921 /* 3922 * May need to preserve linked list pointers at some point too. 3923 */ 3924 pool_ref = dest->io_hdr.pool; 3925 3926 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3927 3928 dest->io_hdr.pool = pool_ref; 3929 /* 3930 * We need to know that this is an internal copy, and doesn't need 3931 * to get passed back to the FETD that allocated it. 3932 */ 3933 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3934} 3935 3936#ifdef NEEDTOPORT 3937static void 3938ctl_update_power_subpage(struct copan_power_subpage *page) 3939{ 3940 int num_luns, num_partitions, config_type; 3941 struct ctl_softc *softc; 3942 cs_BOOL_t aor_present, shelf_50pct_power; 3943 cs_raidset_personality_t rs_type; 3944 int max_active_luns; 3945 3946 softc = control_softc; 3947 3948 /* subtract out the processor LUN */ 3949 num_luns = softc->num_luns - 1; 3950 /* 3951 * Default to 7 LUNs active, which was the only number we allowed 3952 * in the past. 3953 */ 3954 max_active_luns = 7; 3955 3956 num_partitions = config_GetRsPartitionInfo(); 3957 config_type = config_GetConfigType(); 3958 shelf_50pct_power = config_GetShelfPowerMode(); 3959 aor_present = config_IsAorRsPresent(); 3960 3961 rs_type = ddb_GetRsRaidType(1); 3962 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3963 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3964 EPRINT(0, "Unsupported RS type %d!", rs_type); 3965 } 3966 3967 3968 page->total_luns = num_luns; 3969 3970 switch (config_type) { 3971 case 40: 3972 /* 3973 * In a 40 drive configuration, it doesn't matter what DC 3974 * cards we have, whether we have AOR enabled or not, 3975 * partitioning or not, or what type of RAIDset we have. 3976 * In that scenario, we can power up every LUN we present 3977 * to the user. 3978 */ 3979 max_active_luns = num_luns; 3980 3981 break; 3982 case 64: 3983 if (shelf_50pct_power == CS_FALSE) { 3984 /* 25% power */ 3985 if (aor_present == CS_TRUE) { 3986 if (rs_type == 3987 CS_RAIDSET_PERSONALITY_RAID5) { 3988 max_active_luns = 7; 3989 } else if (rs_type == 3990 CS_RAIDSET_PERSONALITY_RAID1){ 3991 max_active_luns = 14; 3992 } else { 3993 /* XXX KDM now what?? */ 3994 } 3995 } else { 3996 if (rs_type == 3997 CS_RAIDSET_PERSONALITY_RAID5) { 3998 max_active_luns = 8; 3999 } else if (rs_type == 4000 CS_RAIDSET_PERSONALITY_RAID1){ 4001 max_active_luns = 16; 4002 } else { 4003 /* XXX KDM now what?? */ 4004 } 4005 } 4006 } else { 4007 /* 50% power */ 4008 /* 4009 * With 50% power in a 64 drive configuration, we 4010 * can power all LUNs we present. 4011 */ 4012 max_active_luns = num_luns; 4013 } 4014 break; 4015 case 112: 4016 if (shelf_50pct_power == CS_FALSE) { 4017 /* 25% power */ 4018 if (aor_present == CS_TRUE) { 4019 if (rs_type == 4020 CS_RAIDSET_PERSONALITY_RAID5) { 4021 max_active_luns = 7; 4022 } else if (rs_type == 4023 CS_RAIDSET_PERSONALITY_RAID1){ 4024 max_active_luns = 14; 4025 } else { 4026 /* XXX KDM now what?? */ 4027 } 4028 } else { 4029 if (rs_type == 4030 CS_RAIDSET_PERSONALITY_RAID5) { 4031 max_active_luns = 8; 4032 } else if (rs_type == 4033 CS_RAIDSET_PERSONALITY_RAID1){ 4034 max_active_luns = 16; 4035 } else { 4036 /* XXX KDM now what?? */ 4037 } 4038 } 4039 } else { 4040 /* 50% power */ 4041 if (aor_present == CS_TRUE) { 4042 if (rs_type == 4043 CS_RAIDSET_PERSONALITY_RAID5) { 4044 max_active_luns = 14; 4045 } else if (rs_type == 4046 CS_RAIDSET_PERSONALITY_RAID1){ 4047 /* 4048 * We're assuming here that disk 4049 * caching is enabled, and so we're 4050 * able to power up half of each 4051 * LUN, and cache all writes. 4052 */ 4053 max_active_luns = num_luns; 4054 } else { 4055 /* XXX KDM now what?? */ 4056 } 4057 } else { 4058 if (rs_type == 4059 CS_RAIDSET_PERSONALITY_RAID5) { 4060 max_active_luns = 15; 4061 } else if (rs_type == 4062 CS_RAIDSET_PERSONALITY_RAID1){ 4063 max_active_luns = 30; 4064 } else { 4065 /* XXX KDM now what?? */ 4066 } 4067 } 4068 } 4069 break; 4070 default: 4071 /* 4072 * In this case, we have an unknown configuration, so we 4073 * just use the default from above. 4074 */ 4075 break; 4076 } 4077 4078 page->max_active_luns = max_active_luns; 4079#if 0 4080 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4081 page->total_luns, page->max_active_luns); 4082#endif 4083} 4084#endif /* NEEDTOPORT */ 4085 4086/* 4087 * This routine could be used in the future to load default and/or saved 4088 * mode page parameters for a particuar lun. 4089 */ 4090static int 4091ctl_init_page_index(struct ctl_lun *lun) 4092{ 4093 int i; 4094 struct ctl_page_index *page_index; 4095 struct ctl_softc *softc; 4096 4097 memcpy(&lun->mode_pages.index, page_index_template, 4098 sizeof(page_index_template)); 4099 4100 softc = lun->ctl_softc; 4101 4102 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4103 4104 page_index = &lun->mode_pages.index[i]; 4105 /* 4106 * If this is a disk-only mode page, there's no point in 4107 * setting it up. For some pages, we have to have some 4108 * basic information about the disk in order to calculate the 4109 * mode page data. 4110 */ 4111 if ((lun->be_lun->lun_type != T_DIRECT) 4112 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4113 continue; 4114 4115 switch (page_index->page_code & SMPH_PC_MASK) { 4116 case SMS_FORMAT_DEVICE_PAGE: { 4117 struct scsi_format_page *format_page; 4118 4119 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4120 panic("subpage is incorrect!"); 4121 4122 /* 4123 * Sectors per track are set above. Bytes per 4124 * sector need to be set here on a per-LUN basis. 4125 */ 4126 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4127 &format_page_default, 4128 sizeof(format_page_default)); 4129 memcpy(&lun->mode_pages.format_page[ 4130 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4131 sizeof(format_page_changeable)); 4132 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4133 &format_page_default, 4134 sizeof(format_page_default)); 4135 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4136 &format_page_default, 4137 sizeof(format_page_default)); 4138 4139 format_page = &lun->mode_pages.format_page[ 4140 CTL_PAGE_CURRENT]; 4141 scsi_ulto2b(lun->be_lun->blocksize, 4142 format_page->bytes_per_sector); 4143 4144 format_page = &lun->mode_pages.format_page[ 4145 CTL_PAGE_DEFAULT]; 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_SAVED]; 4151 scsi_ulto2b(lun->be_lun->blocksize, 4152 format_page->bytes_per_sector); 4153 4154 page_index->page_data = 4155 (uint8_t *)lun->mode_pages.format_page; 4156 break; 4157 } 4158 case SMS_RIGID_DISK_PAGE: { 4159 struct scsi_rigid_disk_page *rigid_disk_page; 4160 uint32_t sectors_per_cylinder; 4161 uint64_t cylinders; 4162#ifndef __XSCALE__ 4163 int shift; 4164#endif /* !__XSCALE__ */ 4165 4166 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4167 panic("invalid subpage value %d", 4168 page_index->subpage); 4169 4170 /* 4171 * Rotation rate and sectors per track are set 4172 * above. We calculate the cylinders here based on 4173 * capacity. Due to the number of heads and 4174 * sectors per track we're using, smaller arrays 4175 * may turn out to have 0 cylinders. Linux and 4176 * FreeBSD don't pay attention to these mode pages 4177 * to figure out capacity, but Solaris does. It 4178 * seems to deal with 0 cylinders just fine, and 4179 * works out a fake geometry based on the capacity. 4180 */ 4181 memcpy(&lun->mode_pages.rigid_disk_page[ 4182 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4183 sizeof(rigid_disk_page_default)); 4184 memcpy(&lun->mode_pages.rigid_disk_page[ 4185 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4186 sizeof(rigid_disk_page_changeable)); 4187 memcpy(&lun->mode_pages.rigid_disk_page[ 4188 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4189 sizeof(rigid_disk_page_default)); 4190 memcpy(&lun->mode_pages.rigid_disk_page[ 4191 CTL_PAGE_SAVED], &rigid_disk_page_default, 4192 sizeof(rigid_disk_page_default)); 4193 4194 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4195 CTL_DEFAULT_HEADS; 4196 4197 /* 4198 * The divide method here will be more accurate, 4199 * probably, but results in floating point being 4200 * used in the kernel on i386 (__udivdi3()). On the 4201 * XScale, though, __udivdi3() is implemented in 4202 * software. 4203 * 4204 * The shift method for cylinder calculation is 4205 * accurate if sectors_per_cylinder is a power of 4206 * 2. Otherwise it might be slightly off -- you 4207 * might have a bit of a truncation problem. 4208 */ 4209#ifdef __XSCALE__ 4210 cylinders = (lun->be_lun->maxlba + 1) / 4211 sectors_per_cylinder; 4212#else 4213 for (shift = 31; shift > 0; shift--) { 4214 if (sectors_per_cylinder & (1 << shift)) 4215 break; 4216 } 4217 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4218#endif 4219 4220 /* 4221 * We've basically got 3 bytes, or 24 bits for the 4222 * cylinder size in the mode page. If we're over, 4223 * just round down to 2^24. 4224 */ 4225 if (cylinders > 0xffffff) 4226 cylinders = 0xffffff; 4227 4228 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4229 CTL_PAGE_CURRENT]; 4230 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4231 4232 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4233 CTL_PAGE_DEFAULT]; 4234 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4235 4236 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4237 CTL_PAGE_SAVED]; 4238 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4239 4240 page_index->page_data = 4241 (uint8_t *)lun->mode_pages.rigid_disk_page; 4242 break; 4243 } 4244 case SMS_CACHING_PAGE: { 4245 4246 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4247 panic("invalid subpage value %d", 4248 page_index->subpage); 4249 /* 4250 * Defaults should be okay here, no calculations 4251 * needed. 4252 */ 4253 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4254 &caching_page_default, 4255 sizeof(caching_page_default)); 4256 memcpy(&lun->mode_pages.caching_page[ 4257 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4258 sizeof(caching_page_changeable)); 4259 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4260 &caching_page_default, 4261 sizeof(caching_page_default)); 4262 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4263 &caching_page_default, 4264 sizeof(caching_page_default)); 4265 page_index->page_data = 4266 (uint8_t *)lun->mode_pages.caching_page; 4267 break; 4268 } 4269 case SMS_CONTROL_MODE_PAGE: { 4270 4271 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4272 panic("invalid subpage value %d", 4273 page_index->subpage); 4274 4275 /* 4276 * Defaults should be okay here, no calculations 4277 * needed. 4278 */ 4279 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4280 &control_page_default, 4281 sizeof(control_page_default)); 4282 memcpy(&lun->mode_pages.control_page[ 4283 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4284 sizeof(control_page_changeable)); 4285 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4286 &control_page_default, 4287 sizeof(control_page_default)); 4288 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4289 &control_page_default, 4290 sizeof(control_page_default)); 4291 page_index->page_data = 4292 (uint8_t *)lun->mode_pages.control_page; 4293 break; 4294 4295 } 4296 case SMS_VENDOR_SPECIFIC_PAGE:{ 4297 switch (page_index->subpage) { 4298 case PWR_SUBPAGE_CODE: { 4299 struct copan_power_subpage *current_page, 4300 *saved_page; 4301 4302 memcpy(&lun->mode_pages.power_subpage[ 4303 CTL_PAGE_CURRENT], 4304 &power_page_default, 4305 sizeof(power_page_default)); 4306 memcpy(&lun->mode_pages.power_subpage[ 4307 CTL_PAGE_CHANGEABLE], 4308 &power_page_changeable, 4309 sizeof(power_page_changeable)); 4310 memcpy(&lun->mode_pages.power_subpage[ 4311 CTL_PAGE_DEFAULT], 4312 &power_page_default, 4313 sizeof(power_page_default)); 4314 memcpy(&lun->mode_pages.power_subpage[ 4315 CTL_PAGE_SAVED], 4316 &power_page_default, 4317 sizeof(power_page_default)); 4318 page_index->page_data = 4319 (uint8_t *)lun->mode_pages.power_subpage; 4320 4321 current_page = (struct copan_power_subpage *) 4322 (page_index->page_data + 4323 (page_index->page_len * 4324 CTL_PAGE_CURRENT)); 4325 saved_page = (struct copan_power_subpage *) 4326 (page_index->page_data + 4327 (page_index->page_len * 4328 CTL_PAGE_SAVED)); 4329 break; 4330 } 4331 case APS_SUBPAGE_CODE: { 4332 struct copan_aps_subpage *current_page, 4333 *saved_page; 4334 4335 // This gets set multiple times but 4336 // it should always be the same. It's 4337 // only done during init so who cares. 4338 index_to_aps_page = i; 4339 4340 memcpy(&lun->mode_pages.aps_subpage[ 4341 CTL_PAGE_CURRENT], 4342 &aps_page_default, 4343 sizeof(aps_page_default)); 4344 memcpy(&lun->mode_pages.aps_subpage[ 4345 CTL_PAGE_CHANGEABLE], 4346 &aps_page_changeable, 4347 sizeof(aps_page_changeable)); 4348 memcpy(&lun->mode_pages.aps_subpage[ 4349 CTL_PAGE_DEFAULT], 4350 &aps_page_default, 4351 sizeof(aps_page_default)); 4352 memcpy(&lun->mode_pages.aps_subpage[ 4353 CTL_PAGE_SAVED], 4354 &aps_page_default, 4355 sizeof(aps_page_default)); 4356 page_index->page_data = 4357 (uint8_t *)lun->mode_pages.aps_subpage; 4358 4359 current_page = (struct copan_aps_subpage *) 4360 (page_index->page_data + 4361 (page_index->page_len * 4362 CTL_PAGE_CURRENT)); 4363 saved_page = (struct copan_aps_subpage *) 4364 (page_index->page_data + 4365 (page_index->page_len * 4366 CTL_PAGE_SAVED)); 4367 break; 4368 } 4369 case DBGCNF_SUBPAGE_CODE: { 4370 struct copan_debugconf_subpage *current_page, 4371 *saved_page; 4372 4373 memcpy(&lun->mode_pages.debugconf_subpage[ 4374 CTL_PAGE_CURRENT], 4375 &debugconf_page_default, 4376 sizeof(debugconf_page_default)); 4377 memcpy(&lun->mode_pages.debugconf_subpage[ 4378 CTL_PAGE_CHANGEABLE], 4379 &debugconf_page_changeable, 4380 sizeof(debugconf_page_changeable)); 4381 memcpy(&lun->mode_pages.debugconf_subpage[ 4382 CTL_PAGE_DEFAULT], 4383 &debugconf_page_default, 4384 sizeof(debugconf_page_default)); 4385 memcpy(&lun->mode_pages.debugconf_subpage[ 4386 CTL_PAGE_SAVED], 4387 &debugconf_page_default, 4388 sizeof(debugconf_page_default)); 4389 page_index->page_data = 4390 (uint8_t *)lun->mode_pages.debugconf_subpage; 4391 4392 current_page = (struct copan_debugconf_subpage *) 4393 (page_index->page_data + 4394 (page_index->page_len * 4395 CTL_PAGE_CURRENT)); 4396 saved_page = (struct copan_debugconf_subpage *) 4397 (page_index->page_data + 4398 (page_index->page_len * 4399 CTL_PAGE_SAVED)); 4400 break; 4401 } 4402 default: 4403 panic("invalid subpage value %d", 4404 page_index->subpage); 4405 break; 4406 } 4407 break; 4408 } 4409 default: 4410 panic("invalid page value %d", 4411 page_index->page_code & SMPH_PC_MASK); 4412 break; 4413 } 4414 } 4415 4416 return (CTL_RETVAL_COMPLETE); 4417} 4418 4419/* 4420 * LUN allocation. 4421 * 4422 * Requirements: 4423 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4424 * wants us to allocate the LUN and he can block. 4425 * - ctl_softc is always set 4426 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4427 * 4428 * Returns 0 for success, non-zero (errno) for failure. 4429 */ 4430static int 4431ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4432 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4433{ 4434 struct ctl_lun *nlun, *lun; 4435 struct ctl_port *port; 4436 struct scsi_vpd_id_descriptor *desc; 4437 struct scsi_vpd_id_t10 *t10id; 4438 const char *eui, *naa, *scsiname, *vendor; 4439 int lun_number, i, lun_malloced; 4440 int devidlen, idlen1, idlen2 = 0, len; 4441 4442 if (be_lun == NULL) 4443 return (EINVAL); 4444 4445 /* 4446 * We currently only support Direct Access or Processor LUN types. 4447 */ 4448 switch (be_lun->lun_type) { 4449 case T_DIRECT: 4450 break; 4451 case T_PROCESSOR: 4452 break; 4453 case T_SEQUENTIAL: 4454 case T_CHANGER: 4455 default: 4456 be_lun->lun_config_status(be_lun->be_lun, 4457 CTL_LUN_CONFIG_FAILURE); 4458 break; 4459 } 4460 if (ctl_lun == NULL) { 4461 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4462 lun_malloced = 1; 4463 } else { 4464 lun_malloced = 0; 4465 lun = ctl_lun; 4466 } 4467 4468 memset(lun, 0, sizeof(*lun)); 4469 if (lun_malloced) 4470 lun->flags = CTL_LUN_MALLOCED; 4471 4472 /* Generate LUN ID. */ 4473 devidlen = max(CTL_DEVID_MIN_LEN, 4474 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4475 idlen1 = sizeof(*t10id) + devidlen; 4476 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4477 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4478 if (scsiname != NULL) { 4479 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4480 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4481 } 4482 eui = ctl_get_opt(&be_lun->options, "eui"); 4483 if (eui != NULL) { 4484 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4485 } 4486 naa = ctl_get_opt(&be_lun->options, "naa"); 4487 if (naa != NULL) { 4488 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4489 } 4490 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4491 M_CTL, M_WAITOK | M_ZERO); 4492 lun->lun_devid->len = len; 4493 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4494 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4495 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4496 desc->length = idlen1; 4497 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4498 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4499 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4500 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4501 } else { 4502 strncpy(t10id->vendor, vendor, 4503 min(sizeof(t10id->vendor), strlen(vendor))); 4504 } 4505 strncpy((char *)t10id->vendor_spec_id, 4506 (char *)be_lun->device_id, devidlen); 4507 if (scsiname != NULL) { 4508 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4509 desc->length); 4510 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4511 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4512 SVPD_ID_TYPE_SCSI_NAME; 4513 desc->length = idlen2; 4514 strlcpy(desc->identifier, scsiname, idlen2); 4515 } 4516 if (eui != NULL) { 4517 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4518 desc->length); 4519 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4520 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4521 SVPD_ID_TYPE_EUI64; 4522 desc->length = 8; 4523 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4524 } 4525 if (naa != NULL) { 4526 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4527 desc->length); 4528 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4529 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4530 SVPD_ID_TYPE_NAA; 4531 desc->length = 8; 4532 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4533 } 4534 4535 mtx_lock(&ctl_softc->ctl_lock); 4536 /* 4537 * See if the caller requested a particular LUN number. If so, see 4538 * if it is available. Otherwise, allocate the first available LUN. 4539 */ 4540 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4541 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4542 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4543 mtx_unlock(&ctl_softc->ctl_lock); 4544 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4545 printf("ctl: requested LUN ID %d is higher " 4546 "than CTL_MAX_LUNS - 1 (%d)\n", 4547 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4548 } else { 4549 /* 4550 * XXX KDM return an error, or just assign 4551 * another LUN ID in this case?? 4552 */ 4553 printf("ctl: requested LUN ID %d is already " 4554 "in use\n", be_lun->req_lun_id); 4555 } 4556 if (lun->flags & CTL_LUN_MALLOCED) 4557 free(lun, M_CTL); 4558 be_lun->lun_config_status(be_lun->be_lun, 4559 CTL_LUN_CONFIG_FAILURE); 4560 return (ENOSPC); 4561 } 4562 lun_number = be_lun->req_lun_id; 4563 } else { 4564 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4565 if (lun_number == -1) { 4566 mtx_unlock(&ctl_softc->ctl_lock); 4567 printf("ctl: can't allocate LUN on target %ju, out of " 4568 "LUNs\n", (uintmax_t)target_id.id); 4569 if (lun->flags & CTL_LUN_MALLOCED) 4570 free(lun, M_CTL); 4571 be_lun->lun_config_status(be_lun->be_lun, 4572 CTL_LUN_CONFIG_FAILURE); 4573 return (ENOSPC); 4574 } 4575 } 4576 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4577 4578 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4579 lun->target = target_id; 4580 lun->lun = lun_number; 4581 lun->be_lun = be_lun; 4582 /* 4583 * The processor LUN is always enabled. Disk LUNs come on line 4584 * disabled, and must be enabled by the backend. 4585 */ 4586 lun->flags |= CTL_LUN_DISABLED; 4587 lun->backend = be_lun->be; 4588 be_lun->ctl_lun = lun; 4589 be_lun->lun_id = lun_number; 4590 atomic_add_int(&be_lun->be->num_luns, 1); 4591 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4592 lun->flags |= CTL_LUN_STOPPED; 4593 4594 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4595 lun->flags |= CTL_LUN_INOPERABLE; 4596 4597 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4598 lun->flags |= CTL_LUN_PRIMARY_SC; 4599 4600 lun->ctl_softc = ctl_softc; 4601 TAILQ_INIT(&lun->ooa_queue); 4602 TAILQ_INIT(&lun->blocked_queue); 4603 STAILQ_INIT(&lun->error_list); 4604 ctl_tpc_lun_init(lun); 4605 4606 /* 4607 * Initialize the mode page index. 4608 */ 4609 ctl_init_page_index(lun); 4610 4611 /* 4612 * Set the poweron UA for all initiators on this LUN only. 4613 */ 4614 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4615 lun->pending_ua[i] = CTL_UA_POWERON; 4616 4617 /* 4618 * Now, before we insert this lun on the lun list, set the lun 4619 * inventory changed UA for all other luns. 4620 */ 4621 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4622 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4623 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4624 } 4625 } 4626 4627 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4628 4629 ctl_softc->ctl_luns[lun_number] = lun; 4630 4631 ctl_softc->num_luns++; 4632 4633 /* Setup statistics gathering */ 4634 lun->stats.device_type = be_lun->lun_type; 4635 lun->stats.lun_number = lun_number; 4636 if (lun->stats.device_type == T_DIRECT) 4637 lun->stats.blocksize = be_lun->blocksize; 4638 else 4639 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4640 for (i = 0;i < CTL_MAX_PORTS;i++) 4641 lun->stats.ports[i].targ_port = i; 4642 4643 mtx_unlock(&ctl_softc->ctl_lock); 4644 4645 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4646 4647 /* 4648 * Run through each registered FETD and bring it online if it isn't 4649 * already. Enable the target ID if it hasn't been enabled, and 4650 * enable this particular LUN. 4651 */ 4652 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4653 int retval; 4654 4655 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4656 if (retval != 0) { 4657 printf("ctl_alloc_lun: FETD %s port %d returned error " 4658 "%d for lun_enable on target %ju lun %d\n", 4659 port->port_name, port->targ_port, retval, 4660 (uintmax_t)target_id.id, lun_number); 4661 } else 4662 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4663 } 4664 return (0); 4665} 4666 4667/* 4668 * Delete a LUN. 4669 * Assumptions: 4670 * - LUN has already been marked invalid and any pending I/O has been taken 4671 * care of. 4672 */ 4673static int 4674ctl_free_lun(struct ctl_lun *lun) 4675{ 4676 struct ctl_softc *softc; 4677#if 0 4678 struct ctl_port *port; 4679#endif 4680 struct ctl_lun *nlun; 4681 int i; 4682 4683 softc = lun->ctl_softc; 4684 4685 mtx_assert(&softc->ctl_lock, MA_OWNED); 4686 4687 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4688 4689 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4690 4691 softc->ctl_luns[lun->lun] = NULL; 4692 4693 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4694 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4695 4696 softc->num_luns--; 4697 4698 /* 4699 * XXX KDM this scheme only works for a single target/multiple LUN 4700 * setup. It needs to be revamped for a multiple target scheme. 4701 * 4702 * XXX KDM this results in port->lun_disable() getting called twice, 4703 * once when ctl_disable_lun() is called, and a second time here. 4704 * We really need to re-think the LUN disable semantics. There 4705 * should probably be several steps/levels to LUN removal: 4706 * - disable 4707 * - invalidate 4708 * - free 4709 * 4710 * Right now we only have a disable method when communicating to 4711 * the front end ports, at least for individual LUNs. 4712 */ 4713#if 0 4714 STAILQ_FOREACH(port, &softc->port_list, links) { 4715 int retval; 4716 4717 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4718 lun->lun); 4719 if (retval != 0) { 4720 printf("ctl_free_lun: FETD %s port %d returned error " 4721 "%d for lun_disable on target %ju lun %jd\n", 4722 port->port_name, port->targ_port, retval, 4723 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4724 } 4725 4726 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4727 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4728 4729 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4730 if (retval != 0) { 4731 printf("ctl_free_lun: FETD %s port %d " 4732 "returned error %d for targ_disable on " 4733 "target %ju\n", port->port_name, 4734 port->targ_port, retval, 4735 (uintmax_t)lun->target.id); 4736 } else 4737 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4738 4739 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4740 continue; 4741 4742#if 0 4743 port->port_offline(port->onoff_arg); 4744 port->status &= ~CTL_PORT_STATUS_ONLINE; 4745#endif 4746 } 4747 } 4748#endif 4749 4750 /* 4751 * Tell the backend to free resources, if this LUN has a backend. 4752 */ 4753 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4754 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4755 4756 ctl_tpc_lun_shutdown(lun); 4757 mtx_destroy(&lun->lun_lock); 4758 free(lun->lun_devid, M_CTL); 4759 if (lun->flags & CTL_LUN_MALLOCED) 4760 free(lun, M_CTL); 4761 4762 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4763 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4764 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4765 } 4766 } 4767 4768 return (0); 4769} 4770 4771static void 4772ctl_create_lun(struct ctl_be_lun *be_lun) 4773{ 4774 struct ctl_softc *ctl_softc; 4775 4776 ctl_softc = control_softc; 4777 4778 /* 4779 * ctl_alloc_lun() should handle all potential failure cases. 4780 */ 4781 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4782} 4783 4784int 4785ctl_add_lun(struct ctl_be_lun *be_lun) 4786{ 4787 struct ctl_softc *ctl_softc = control_softc; 4788 4789 mtx_lock(&ctl_softc->ctl_lock); 4790 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4791 mtx_unlock(&ctl_softc->ctl_lock); 4792 wakeup(&ctl_softc->pending_lun_queue); 4793 4794 return (0); 4795} 4796 4797int 4798ctl_enable_lun(struct ctl_be_lun *be_lun) 4799{ 4800 struct ctl_softc *ctl_softc; 4801 struct ctl_port *port, *nport; 4802 struct ctl_lun *lun; 4803 int retval; 4804 4805 ctl_softc = control_softc; 4806 4807 lun = (struct ctl_lun *)be_lun->ctl_lun; 4808 4809 mtx_lock(&ctl_softc->ctl_lock); 4810 mtx_lock(&lun->lun_lock); 4811 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4812 /* 4813 * eh? Why did we get called if the LUN is already 4814 * enabled? 4815 */ 4816 mtx_unlock(&lun->lun_lock); 4817 mtx_unlock(&ctl_softc->ctl_lock); 4818 return (0); 4819 } 4820 lun->flags &= ~CTL_LUN_DISABLED; 4821 mtx_unlock(&lun->lun_lock); 4822 4823 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4824 nport = STAILQ_NEXT(port, links); 4825 4826 /* 4827 * Drop the lock while we call the FETD's enable routine. 4828 * This can lead to a callback into CTL (at least in the 4829 * case of the internal initiator frontend. 4830 */ 4831 mtx_unlock(&ctl_softc->ctl_lock); 4832 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4833 mtx_lock(&ctl_softc->ctl_lock); 4834 if (retval != 0) { 4835 printf("%s: FETD %s port %d returned error " 4836 "%d for lun_enable on target %ju lun %jd\n", 4837 __func__, port->port_name, port->targ_port, retval, 4838 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4839 } 4840#if 0 4841 else { 4842 /* NOTE: TODO: why does lun enable affect port status? */ 4843 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4844 } 4845#endif 4846 } 4847 4848 mtx_unlock(&ctl_softc->ctl_lock); 4849 4850 return (0); 4851} 4852 4853int 4854ctl_disable_lun(struct ctl_be_lun *be_lun) 4855{ 4856 struct ctl_softc *ctl_softc; 4857 struct ctl_port *port; 4858 struct ctl_lun *lun; 4859 int retval; 4860 4861 ctl_softc = control_softc; 4862 4863 lun = (struct ctl_lun *)be_lun->ctl_lun; 4864 4865 mtx_lock(&ctl_softc->ctl_lock); 4866 mtx_lock(&lun->lun_lock); 4867 if (lun->flags & CTL_LUN_DISABLED) { 4868 mtx_unlock(&lun->lun_lock); 4869 mtx_unlock(&ctl_softc->ctl_lock); 4870 return (0); 4871 } 4872 lun->flags |= CTL_LUN_DISABLED; 4873 mtx_unlock(&lun->lun_lock); 4874 4875 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4876 mtx_unlock(&ctl_softc->ctl_lock); 4877 /* 4878 * Drop the lock before we call the frontend's disable 4879 * routine, to avoid lock order reversals. 4880 * 4881 * XXX KDM what happens if the frontend list changes while 4882 * we're traversing it? It's unlikely, but should be handled. 4883 */ 4884 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4885 lun->lun); 4886 mtx_lock(&ctl_softc->ctl_lock); 4887 if (retval != 0) { 4888 printf("ctl_alloc_lun: FETD %s port %d returned error " 4889 "%d for lun_disable on target %ju lun %jd\n", 4890 port->port_name, port->targ_port, retval, 4891 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4892 } 4893 } 4894 4895 mtx_unlock(&ctl_softc->ctl_lock); 4896 4897 return (0); 4898} 4899 4900int 4901ctl_start_lun(struct ctl_be_lun *be_lun) 4902{ 4903 struct ctl_softc *ctl_softc; 4904 struct ctl_lun *lun; 4905 4906 ctl_softc = control_softc; 4907 4908 lun = (struct ctl_lun *)be_lun->ctl_lun; 4909 4910 mtx_lock(&lun->lun_lock); 4911 lun->flags &= ~CTL_LUN_STOPPED; 4912 mtx_unlock(&lun->lun_lock); 4913 4914 return (0); 4915} 4916 4917int 4918ctl_stop_lun(struct ctl_be_lun *be_lun) 4919{ 4920 struct ctl_softc *ctl_softc; 4921 struct ctl_lun *lun; 4922 4923 ctl_softc = control_softc; 4924 4925 lun = (struct ctl_lun *)be_lun->ctl_lun; 4926 4927 mtx_lock(&lun->lun_lock); 4928 lun->flags |= CTL_LUN_STOPPED; 4929 mtx_unlock(&lun->lun_lock); 4930 4931 return (0); 4932} 4933 4934int 4935ctl_lun_offline(struct ctl_be_lun *be_lun) 4936{ 4937 struct ctl_softc *ctl_softc; 4938 struct ctl_lun *lun; 4939 4940 ctl_softc = control_softc; 4941 4942 lun = (struct ctl_lun *)be_lun->ctl_lun; 4943 4944 mtx_lock(&lun->lun_lock); 4945 lun->flags |= CTL_LUN_OFFLINE; 4946 mtx_unlock(&lun->lun_lock); 4947 4948 return (0); 4949} 4950 4951int 4952ctl_lun_online(struct ctl_be_lun *be_lun) 4953{ 4954 struct ctl_softc *ctl_softc; 4955 struct ctl_lun *lun; 4956 4957 ctl_softc = control_softc; 4958 4959 lun = (struct ctl_lun *)be_lun->ctl_lun; 4960 4961 mtx_lock(&lun->lun_lock); 4962 lun->flags &= ~CTL_LUN_OFFLINE; 4963 mtx_unlock(&lun->lun_lock); 4964 4965 return (0); 4966} 4967 4968int 4969ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4970{ 4971 struct ctl_softc *ctl_softc; 4972 struct ctl_lun *lun; 4973 4974 ctl_softc = control_softc; 4975 4976 lun = (struct ctl_lun *)be_lun->ctl_lun; 4977 4978 mtx_lock(&lun->lun_lock); 4979 4980 /* 4981 * The LUN needs to be disabled before it can be marked invalid. 4982 */ 4983 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4984 mtx_unlock(&lun->lun_lock); 4985 return (-1); 4986 } 4987 /* 4988 * Mark the LUN invalid. 4989 */ 4990 lun->flags |= CTL_LUN_INVALID; 4991 4992 /* 4993 * If there is nothing in the OOA queue, go ahead and free the LUN. 4994 * If we have something in the OOA queue, we'll free it when the 4995 * last I/O completes. 4996 */ 4997 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4998 mtx_unlock(&lun->lun_lock); 4999 mtx_lock(&ctl_softc->ctl_lock); 5000 ctl_free_lun(lun); 5001 mtx_unlock(&ctl_softc->ctl_lock); 5002 } else 5003 mtx_unlock(&lun->lun_lock); 5004 5005 return (0); 5006} 5007 5008int 5009ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5010{ 5011 struct ctl_softc *ctl_softc; 5012 struct ctl_lun *lun; 5013 5014 ctl_softc = control_softc; 5015 lun = (struct ctl_lun *)be_lun->ctl_lun; 5016 5017 mtx_lock(&lun->lun_lock); 5018 lun->flags |= CTL_LUN_INOPERABLE; 5019 mtx_unlock(&lun->lun_lock); 5020 5021 return (0); 5022} 5023 5024int 5025ctl_lun_operable(struct ctl_be_lun *be_lun) 5026{ 5027 struct ctl_softc *ctl_softc; 5028 struct ctl_lun *lun; 5029 5030 ctl_softc = control_softc; 5031 lun = (struct ctl_lun *)be_lun->ctl_lun; 5032 5033 mtx_lock(&lun->lun_lock); 5034 lun->flags &= ~CTL_LUN_INOPERABLE; 5035 mtx_unlock(&lun->lun_lock); 5036 5037 return (0); 5038} 5039 5040int 5041ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5042 int lock) 5043{ 5044 struct ctl_softc *softc; 5045 struct ctl_lun *lun; 5046 struct copan_aps_subpage *current_sp; 5047 struct ctl_page_index *page_index; 5048 int i; 5049 5050 softc = control_softc; 5051 5052 mtx_lock(&softc->ctl_lock); 5053 5054 lun = (struct ctl_lun *)be_lun->ctl_lun; 5055 mtx_lock(&lun->lun_lock); 5056 5057 page_index = NULL; 5058 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5059 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5060 APS_PAGE_CODE) 5061 continue; 5062 5063 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5064 continue; 5065 page_index = &lun->mode_pages.index[i]; 5066 } 5067 5068 if (page_index == NULL) { 5069 mtx_unlock(&lun->lun_lock); 5070 mtx_unlock(&softc->ctl_lock); 5071 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5072 (uintmax_t)lun->lun); 5073 return (1); 5074 } 5075#if 0 5076 if ((softc->aps_locked_lun != 0) 5077 && (softc->aps_locked_lun != lun->lun)) { 5078 printf("%s: attempt to lock LUN %llu when %llu is already " 5079 "locked\n"); 5080 mtx_unlock(&lun->lun_lock); 5081 mtx_unlock(&softc->ctl_lock); 5082 return (1); 5083 } 5084#endif 5085 5086 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5087 (page_index->page_len * CTL_PAGE_CURRENT)); 5088 5089 if (lock != 0) { 5090 current_sp->lock_active = APS_LOCK_ACTIVE; 5091 softc->aps_locked_lun = lun->lun; 5092 } else { 5093 current_sp->lock_active = 0; 5094 softc->aps_locked_lun = 0; 5095 } 5096 5097 5098 /* 5099 * If we're in HA mode, try to send the lock message to the other 5100 * side. 5101 */ 5102 if (ctl_is_single == 0) { 5103 int isc_retval; 5104 union ctl_ha_msg lock_msg; 5105 5106 lock_msg.hdr.nexus = *nexus; 5107 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5108 if (lock != 0) 5109 lock_msg.aps.lock_flag = 1; 5110 else 5111 lock_msg.aps.lock_flag = 0; 5112 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5113 sizeof(lock_msg), 0); 5114 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5115 printf("%s: APS (lock=%d) error returned from " 5116 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5117 mtx_unlock(&lun->lun_lock); 5118 mtx_unlock(&softc->ctl_lock); 5119 return (1); 5120 } 5121 } 5122 5123 mtx_unlock(&lun->lun_lock); 5124 mtx_unlock(&softc->ctl_lock); 5125 5126 return (0); 5127} 5128 5129void 5130ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5131{ 5132 struct ctl_lun *lun; 5133 struct ctl_softc *softc; 5134 int i; 5135 5136 softc = control_softc; 5137 5138 lun = (struct ctl_lun *)be_lun->ctl_lun; 5139 5140 mtx_lock(&lun->lun_lock); 5141 5142 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5143 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5144 5145 mtx_unlock(&lun->lun_lock); 5146} 5147 5148/* 5149 * Backend "memory move is complete" callback for requests that never 5150 * make it down to say RAIDCore's configuration code. 5151 */ 5152int 5153ctl_config_move_done(union ctl_io *io) 5154{ 5155 int retval; 5156 5157 retval = CTL_RETVAL_COMPLETE; 5158 5159 5160 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5161 /* 5162 * XXX KDM this shouldn't happen, but what if it does? 5163 */ 5164 if (io->io_hdr.io_type != CTL_IO_SCSI) 5165 panic("I/O type isn't CTL_IO_SCSI!"); 5166 5167 if ((io->io_hdr.port_status == 0) 5168 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5169 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5170 io->io_hdr.status = CTL_SUCCESS; 5171 else if ((io->io_hdr.port_status != 0) 5172 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5173 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5174 /* 5175 * For hardware error sense keys, the sense key 5176 * specific value is defined to be a retry count, 5177 * but we use it to pass back an internal FETD 5178 * error code. XXX KDM Hopefully the FETD is only 5179 * using 16 bits for an error code, since that's 5180 * all the space we have in the sks field. 5181 */ 5182 ctl_set_internal_failure(&io->scsiio, 5183 /*sks_valid*/ 1, 5184 /*retry_count*/ 5185 io->io_hdr.port_status); 5186 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5187 free(io->scsiio.kern_data_ptr, M_CTL); 5188 ctl_done(io); 5189 goto bailout; 5190 } 5191 5192 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5193 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5194 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5195 /* 5196 * XXX KDM just assuming a single pointer here, and not a 5197 * S/G list. If we start using S/G lists for config data, 5198 * we'll need to know how to clean them up here as well. 5199 */ 5200 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5201 free(io->scsiio.kern_data_ptr, M_CTL); 5202 /* Hopefully the user has already set the status... */ 5203 ctl_done(io); 5204 } else { 5205 /* 5206 * XXX KDM now we need to continue data movement. Some 5207 * options: 5208 * - call ctl_scsiio() again? We don't do this for data 5209 * writes, because for those at least we know ahead of 5210 * time where the write will go and how long it is. For 5211 * config writes, though, that information is largely 5212 * contained within the write itself, thus we need to 5213 * parse out the data again. 5214 * 5215 * - Call some other function once the data is in? 5216 */ 5217 5218 /* 5219 * XXX KDM call ctl_scsiio() again for now, and check flag 5220 * bits to see whether we're allocated or not. 5221 */ 5222 retval = ctl_scsiio(&io->scsiio); 5223 } 5224bailout: 5225 return (retval); 5226} 5227 5228/* 5229 * This gets called by a backend driver when it is done with a 5230 * data_submit method. 5231 */ 5232void 5233ctl_data_submit_done(union ctl_io *io) 5234{ 5235 /* 5236 * If the IO_CONT flag is set, we need to call the supplied 5237 * function to continue processing the I/O, instead of completing 5238 * the I/O just yet. 5239 * 5240 * If there is an error, though, we don't want to keep processing. 5241 * Instead, just send status back to the initiator. 5242 */ 5243 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5244 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5245 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5246 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5247 io->scsiio.io_cont(io); 5248 return; 5249 } 5250 ctl_done(io); 5251} 5252 5253/* 5254 * This gets called by a backend driver when it is done with a 5255 * configuration write. 5256 */ 5257void 5258ctl_config_write_done(union ctl_io *io) 5259{ 5260 /* 5261 * If the IO_CONT flag is set, we need to call the supplied 5262 * function to continue processing the I/O, instead of completing 5263 * the I/O just yet. 5264 * 5265 * If there is an error, though, we don't want to keep processing. 5266 * Instead, just send status back to the initiator. 5267 */ 5268 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5269 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5270 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5271 io->scsiio.io_cont(io); 5272 return; 5273 } 5274 /* 5275 * Since a configuration write can be done for commands that actually 5276 * have data allocated, like write buffer, and commands that have 5277 * no data, like start/stop unit, we need to check here. 5278 */ 5279 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5280 free(io->scsiio.kern_data_ptr, M_CTL); 5281 ctl_done(io); 5282} 5283 5284/* 5285 * SCSI release command. 5286 */ 5287int 5288ctl_scsi_release(struct ctl_scsiio *ctsio) 5289{ 5290 int length, longid, thirdparty_id, resv_id; 5291 struct ctl_softc *ctl_softc; 5292 struct ctl_lun *lun; 5293 5294 length = 0; 5295 resv_id = 0; 5296 5297 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5298 5299 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5300 ctl_softc = control_softc; 5301 5302 switch (ctsio->cdb[0]) { 5303 case RELEASE_10: { 5304 struct scsi_release_10 *cdb; 5305 5306 cdb = (struct scsi_release_10 *)ctsio->cdb; 5307 5308 if (cdb->byte2 & SR10_LONGID) 5309 longid = 1; 5310 else 5311 thirdparty_id = cdb->thirdparty_id; 5312 5313 resv_id = cdb->resv_id; 5314 length = scsi_2btoul(cdb->length); 5315 break; 5316 } 5317 } 5318 5319 5320 /* 5321 * XXX KDM right now, we only support LUN reservation. We don't 5322 * support 3rd party reservations, or extent reservations, which 5323 * might actually need the parameter list. If we've gotten this 5324 * far, we've got a LUN reservation. Anything else got kicked out 5325 * above. So, according to SPC, ignore the length. 5326 */ 5327 length = 0; 5328 5329 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5330 && (length > 0)) { 5331 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5332 ctsio->kern_data_len = length; 5333 ctsio->kern_total_len = length; 5334 ctsio->kern_data_resid = 0; 5335 ctsio->kern_rel_offset = 0; 5336 ctsio->kern_sg_entries = 0; 5337 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5338 ctsio->be_move_done = ctl_config_move_done; 5339 ctl_datamove((union ctl_io *)ctsio); 5340 5341 return (CTL_RETVAL_COMPLETE); 5342 } 5343 5344 if (length > 0) 5345 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5346 5347 mtx_lock(&lun->lun_lock); 5348 5349 /* 5350 * According to SPC, it is not an error for an intiator to attempt 5351 * to release a reservation on a LUN that isn't reserved, or that 5352 * is reserved by another initiator. The reservation can only be 5353 * released, though, by the initiator who made it or by one of 5354 * several reset type events. 5355 */ 5356 if (lun->flags & CTL_LUN_RESERVED) { 5357 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5358 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5359 && (ctsio->io_hdr.nexus.targ_target.id == 5360 lun->rsv_nexus.targ_target.id)) { 5361 lun->flags &= ~CTL_LUN_RESERVED; 5362 } 5363 } 5364 5365 mtx_unlock(&lun->lun_lock); 5366 5367 ctsio->scsi_status = SCSI_STATUS_OK; 5368 ctsio->io_hdr.status = CTL_SUCCESS; 5369 5370 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5371 free(ctsio->kern_data_ptr, M_CTL); 5372 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5373 } 5374 5375 ctl_done((union ctl_io *)ctsio); 5376 return (CTL_RETVAL_COMPLETE); 5377} 5378 5379int 5380ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5381{ 5382 int extent, thirdparty, longid; 5383 int resv_id, length; 5384 uint64_t thirdparty_id; 5385 struct ctl_softc *ctl_softc; 5386 struct ctl_lun *lun; 5387 5388 extent = 0; 5389 thirdparty = 0; 5390 longid = 0; 5391 resv_id = 0; 5392 length = 0; 5393 thirdparty_id = 0; 5394 5395 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5396 5397 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5398 ctl_softc = control_softc; 5399 5400 switch (ctsio->cdb[0]) { 5401 case RESERVE_10: { 5402 struct scsi_reserve_10 *cdb; 5403 5404 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5405 5406 if (cdb->byte2 & SR10_LONGID) 5407 longid = 1; 5408 else 5409 thirdparty_id = cdb->thirdparty_id; 5410 5411 resv_id = cdb->resv_id; 5412 length = scsi_2btoul(cdb->length); 5413 break; 5414 } 5415 } 5416 5417 /* 5418 * XXX KDM right now, we only support LUN reservation. We don't 5419 * support 3rd party reservations, or extent reservations, which 5420 * might actually need the parameter list. If we've gotten this 5421 * far, we've got a LUN reservation. Anything else got kicked out 5422 * above. So, according to SPC, ignore the length. 5423 */ 5424 length = 0; 5425 5426 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5427 && (length > 0)) { 5428 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5429 ctsio->kern_data_len = length; 5430 ctsio->kern_total_len = length; 5431 ctsio->kern_data_resid = 0; 5432 ctsio->kern_rel_offset = 0; 5433 ctsio->kern_sg_entries = 0; 5434 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5435 ctsio->be_move_done = ctl_config_move_done; 5436 ctl_datamove((union ctl_io *)ctsio); 5437 5438 return (CTL_RETVAL_COMPLETE); 5439 } 5440 5441 if (length > 0) 5442 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5443 5444 mtx_lock(&lun->lun_lock); 5445 if (lun->flags & CTL_LUN_RESERVED) { 5446 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5447 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5448 || (ctsio->io_hdr.nexus.targ_target.id != 5449 lun->rsv_nexus.targ_target.id)) { 5450 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5451 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5452 goto bailout; 5453 } 5454 } 5455 5456 lun->flags |= CTL_LUN_RESERVED; 5457 lun->rsv_nexus = ctsio->io_hdr.nexus; 5458 5459 ctsio->scsi_status = SCSI_STATUS_OK; 5460 ctsio->io_hdr.status = CTL_SUCCESS; 5461 5462bailout: 5463 mtx_unlock(&lun->lun_lock); 5464 5465 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5466 free(ctsio->kern_data_ptr, M_CTL); 5467 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5468 } 5469 5470 ctl_done((union ctl_io *)ctsio); 5471 return (CTL_RETVAL_COMPLETE); 5472} 5473 5474int 5475ctl_start_stop(struct ctl_scsiio *ctsio) 5476{ 5477 struct scsi_start_stop_unit *cdb; 5478 struct ctl_lun *lun; 5479 struct ctl_softc *ctl_softc; 5480 int retval; 5481 5482 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5483 5484 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5485 ctl_softc = control_softc; 5486 retval = 0; 5487 5488 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5489 5490 /* 5491 * XXX KDM 5492 * We don't support the immediate bit on a stop unit. In order to 5493 * do that, we would need to code up a way to know that a stop is 5494 * pending, and hold off any new commands until it completes, one 5495 * way or another. Then we could accept or reject those commands 5496 * depending on its status. We would almost need to do the reverse 5497 * of what we do below for an immediate start -- return the copy of 5498 * the ctl_io to the FETD with status to send to the host (and to 5499 * free the copy!) and then free the original I/O once the stop 5500 * actually completes. That way, the OOA queue mechanism can work 5501 * to block commands that shouldn't proceed. Another alternative 5502 * would be to put the copy in the queue in place of the original, 5503 * and return the original back to the caller. That could be 5504 * slightly safer.. 5505 */ 5506 if ((cdb->byte2 & SSS_IMMED) 5507 && ((cdb->how & SSS_START) == 0)) { 5508 ctl_set_invalid_field(ctsio, 5509 /*sks_valid*/ 1, 5510 /*command*/ 1, 5511 /*field*/ 1, 5512 /*bit_valid*/ 1, 5513 /*bit*/ 0); 5514 ctl_done((union ctl_io *)ctsio); 5515 return (CTL_RETVAL_COMPLETE); 5516 } 5517 5518 if ((lun->flags & CTL_LUN_PR_RESERVED) 5519 && ((cdb->how & SSS_START)==0)) { 5520 uint32_t residx; 5521 5522 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5523 if (!lun->per_res[residx].registered 5524 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5525 5526 ctl_set_reservation_conflict(ctsio); 5527 ctl_done((union ctl_io *)ctsio); 5528 return (CTL_RETVAL_COMPLETE); 5529 } 5530 } 5531 5532 /* 5533 * If there is no backend on this device, we can't start or stop 5534 * it. In theory we shouldn't get any start/stop commands in the 5535 * first place at this level if the LUN doesn't have a backend. 5536 * That should get stopped by the command decode code. 5537 */ 5538 if (lun->backend == NULL) { 5539 ctl_set_invalid_opcode(ctsio); 5540 ctl_done((union ctl_io *)ctsio); 5541 return (CTL_RETVAL_COMPLETE); 5542 } 5543 5544 /* 5545 * XXX KDM Copan-specific offline behavior. 5546 * Figure out a reasonable way to port this? 5547 */ 5548#ifdef NEEDTOPORT 5549 mtx_lock(&lun->lun_lock); 5550 5551 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5552 && (lun->flags & CTL_LUN_OFFLINE)) { 5553 /* 5554 * If the LUN is offline, and the on/offline bit isn't set, 5555 * reject the start or stop. Otherwise, let it through. 5556 */ 5557 mtx_unlock(&lun->lun_lock); 5558 ctl_set_lun_not_ready(ctsio); 5559 ctl_done((union ctl_io *)ctsio); 5560 } else { 5561 mtx_unlock(&lun->lun_lock); 5562#endif /* NEEDTOPORT */ 5563 /* 5564 * This could be a start or a stop when we're online, 5565 * or a stop/offline or start/online. A start or stop when 5566 * we're offline is covered in the case above. 5567 */ 5568 /* 5569 * In the non-immediate case, we send the request to 5570 * the backend and return status to the user when 5571 * it is done. 5572 * 5573 * In the immediate case, we allocate a new ctl_io 5574 * to hold a copy of the request, and send that to 5575 * the backend. We then set good status on the 5576 * user's request and return it immediately. 5577 */ 5578 if (cdb->byte2 & SSS_IMMED) { 5579 union ctl_io *new_io; 5580 5581 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5582 if (new_io == NULL) { 5583 ctl_set_busy(ctsio); 5584 ctl_done((union ctl_io *)ctsio); 5585 } else { 5586 ctl_copy_io((union ctl_io *)ctsio, 5587 new_io); 5588 retval = lun->backend->config_write(new_io); 5589 ctl_set_success(ctsio); 5590 ctl_done((union ctl_io *)ctsio); 5591 } 5592 } else { 5593 retval = lun->backend->config_write( 5594 (union ctl_io *)ctsio); 5595 } 5596#ifdef NEEDTOPORT 5597 } 5598#endif 5599 return (retval); 5600} 5601 5602/* 5603 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5604 * we don't really do anything with the LBA and length fields if the user 5605 * passes them in. Instead we'll just flush out the cache for the entire 5606 * LUN. 5607 */ 5608int 5609ctl_sync_cache(struct ctl_scsiio *ctsio) 5610{ 5611 struct ctl_lun *lun; 5612 struct ctl_softc *ctl_softc; 5613 uint64_t starting_lba; 5614 uint32_t block_count; 5615 int retval; 5616 5617 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5618 5619 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5620 ctl_softc = control_softc; 5621 retval = 0; 5622 5623 switch (ctsio->cdb[0]) { 5624 case SYNCHRONIZE_CACHE: { 5625 struct scsi_sync_cache *cdb; 5626 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5627 5628 starting_lba = scsi_4btoul(cdb->begin_lba); 5629 block_count = scsi_2btoul(cdb->lb_count); 5630 break; 5631 } 5632 case SYNCHRONIZE_CACHE_16: { 5633 struct scsi_sync_cache_16 *cdb; 5634 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5635 5636 starting_lba = scsi_8btou64(cdb->begin_lba); 5637 block_count = scsi_4btoul(cdb->lb_count); 5638 break; 5639 } 5640 default: 5641 ctl_set_invalid_opcode(ctsio); 5642 ctl_done((union ctl_io *)ctsio); 5643 goto bailout; 5644 break; /* NOTREACHED */ 5645 } 5646 5647 /* 5648 * We check the LBA and length, but don't do anything with them. 5649 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5650 * get flushed. This check will just help satisfy anyone who wants 5651 * to see an error for an out of range LBA. 5652 */ 5653 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5654 ctl_set_lba_out_of_range(ctsio); 5655 ctl_done((union ctl_io *)ctsio); 5656 goto bailout; 5657 } 5658 5659 /* 5660 * If this LUN has no backend, we can't flush the cache anyway. 5661 */ 5662 if (lun->backend == NULL) { 5663 ctl_set_invalid_opcode(ctsio); 5664 ctl_done((union ctl_io *)ctsio); 5665 goto bailout; 5666 } 5667 5668 /* 5669 * Check to see whether we're configured to send the SYNCHRONIZE 5670 * CACHE command directly to the back end. 5671 */ 5672 mtx_lock(&lun->lun_lock); 5673 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5674 && (++(lun->sync_count) >= lun->sync_interval)) { 5675 lun->sync_count = 0; 5676 mtx_unlock(&lun->lun_lock); 5677 retval = lun->backend->config_write((union ctl_io *)ctsio); 5678 } else { 5679 mtx_unlock(&lun->lun_lock); 5680 ctl_set_success(ctsio); 5681 ctl_done((union ctl_io *)ctsio); 5682 } 5683 5684bailout: 5685 5686 return (retval); 5687} 5688 5689int 5690ctl_format(struct ctl_scsiio *ctsio) 5691{ 5692 struct scsi_format *cdb; 5693 struct ctl_lun *lun; 5694 struct ctl_softc *ctl_softc; 5695 int length, defect_list_len; 5696 5697 CTL_DEBUG_PRINT(("ctl_format\n")); 5698 5699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5700 ctl_softc = control_softc; 5701 5702 cdb = (struct scsi_format *)ctsio->cdb; 5703 5704 length = 0; 5705 if (cdb->byte2 & SF_FMTDATA) { 5706 if (cdb->byte2 & SF_LONGLIST) 5707 length = sizeof(struct scsi_format_header_long); 5708 else 5709 length = sizeof(struct scsi_format_header_short); 5710 } 5711 5712 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5713 && (length > 0)) { 5714 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5715 ctsio->kern_data_len = length; 5716 ctsio->kern_total_len = length; 5717 ctsio->kern_data_resid = 0; 5718 ctsio->kern_rel_offset = 0; 5719 ctsio->kern_sg_entries = 0; 5720 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5721 ctsio->be_move_done = ctl_config_move_done; 5722 ctl_datamove((union ctl_io *)ctsio); 5723 5724 return (CTL_RETVAL_COMPLETE); 5725 } 5726 5727 defect_list_len = 0; 5728 5729 if (cdb->byte2 & SF_FMTDATA) { 5730 if (cdb->byte2 & SF_LONGLIST) { 5731 struct scsi_format_header_long *header; 5732 5733 header = (struct scsi_format_header_long *) 5734 ctsio->kern_data_ptr; 5735 5736 defect_list_len = scsi_4btoul(header->defect_list_len); 5737 if (defect_list_len != 0) { 5738 ctl_set_invalid_field(ctsio, 5739 /*sks_valid*/ 1, 5740 /*command*/ 0, 5741 /*field*/ 2, 5742 /*bit_valid*/ 0, 5743 /*bit*/ 0); 5744 goto bailout; 5745 } 5746 } else { 5747 struct scsi_format_header_short *header; 5748 5749 header = (struct scsi_format_header_short *) 5750 ctsio->kern_data_ptr; 5751 5752 defect_list_len = scsi_2btoul(header->defect_list_len); 5753 if (defect_list_len != 0) { 5754 ctl_set_invalid_field(ctsio, 5755 /*sks_valid*/ 1, 5756 /*command*/ 0, 5757 /*field*/ 2, 5758 /*bit_valid*/ 0, 5759 /*bit*/ 0); 5760 goto bailout; 5761 } 5762 } 5763 } 5764 5765 /* 5766 * The format command will clear out the "Medium format corrupted" 5767 * status if set by the configuration code. That status is really 5768 * just a way to notify the host that we have lost the media, and 5769 * get them to issue a command that will basically make them think 5770 * they're blowing away the media. 5771 */ 5772 mtx_lock(&lun->lun_lock); 5773 lun->flags &= ~CTL_LUN_INOPERABLE; 5774 mtx_unlock(&lun->lun_lock); 5775 5776 ctsio->scsi_status = SCSI_STATUS_OK; 5777 ctsio->io_hdr.status = CTL_SUCCESS; 5778bailout: 5779 5780 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5781 free(ctsio->kern_data_ptr, M_CTL); 5782 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5783 } 5784 5785 ctl_done((union ctl_io *)ctsio); 5786 return (CTL_RETVAL_COMPLETE); 5787} 5788 5789int 5790ctl_read_buffer(struct ctl_scsiio *ctsio) 5791{ 5792 struct scsi_read_buffer *cdb; 5793 struct ctl_lun *lun; 5794 int buffer_offset, len; 5795 static uint8_t descr[4]; 5796 static uint8_t echo_descr[4] = { 0 }; 5797 5798 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5799 5800 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5801 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5802 5803 if (lun->flags & CTL_LUN_PR_RESERVED) { 5804 uint32_t residx; 5805 5806 /* 5807 * XXX KDM need a lock here. 5808 */ 5809 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5810 if ((lun->res_type == SPR_TYPE_EX_AC 5811 && residx != lun->pr_res_idx) 5812 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5813 || lun->res_type == SPR_TYPE_EX_AC_AR) 5814 && !lun->per_res[residx].registered)) { 5815 ctl_set_reservation_conflict(ctsio); 5816 ctl_done((union ctl_io *)ctsio); 5817 return (CTL_RETVAL_COMPLETE); 5818 } 5819 } 5820 5821 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5822 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5823 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5824 ctl_set_invalid_field(ctsio, 5825 /*sks_valid*/ 1, 5826 /*command*/ 1, 5827 /*field*/ 1, 5828 /*bit_valid*/ 1, 5829 /*bit*/ 4); 5830 ctl_done((union ctl_io *)ctsio); 5831 return (CTL_RETVAL_COMPLETE); 5832 } 5833 5834 len = scsi_3btoul(cdb->length); 5835 buffer_offset = scsi_3btoul(cdb->offset); 5836 5837 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5838 ctl_set_invalid_field(ctsio, 5839 /*sks_valid*/ 1, 5840 /*command*/ 1, 5841 /*field*/ 6, 5842 /*bit_valid*/ 0, 5843 /*bit*/ 0); 5844 ctl_done((union ctl_io *)ctsio); 5845 return (CTL_RETVAL_COMPLETE); 5846 } 5847 5848 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5849 descr[0] = 0; 5850 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5851 ctsio->kern_data_ptr = descr; 5852 len = min(len, sizeof(descr)); 5853 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5854 ctsio->kern_data_ptr = echo_descr; 5855 len = min(len, sizeof(echo_descr)); 5856 } else 5857 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5858 ctsio->kern_data_len = len; 5859 ctsio->kern_total_len = len; 5860 ctsio->kern_data_resid = 0; 5861 ctsio->kern_rel_offset = 0; 5862 ctsio->kern_sg_entries = 0; 5863 ctsio->be_move_done = ctl_config_move_done; 5864 ctl_datamove((union ctl_io *)ctsio); 5865 5866 return (CTL_RETVAL_COMPLETE); 5867} 5868 5869int 5870ctl_write_buffer(struct ctl_scsiio *ctsio) 5871{ 5872 struct scsi_write_buffer *cdb; 5873 struct ctl_lun *lun; 5874 int buffer_offset, len; 5875 5876 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5877 5878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5879 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5880 5881 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5882 ctl_set_invalid_field(ctsio, 5883 /*sks_valid*/ 1, 5884 /*command*/ 1, 5885 /*field*/ 1, 5886 /*bit_valid*/ 1, 5887 /*bit*/ 4); 5888 ctl_done((union ctl_io *)ctsio); 5889 return (CTL_RETVAL_COMPLETE); 5890 } 5891 5892 len = scsi_3btoul(cdb->length); 5893 buffer_offset = scsi_3btoul(cdb->offset); 5894 5895 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5896 ctl_set_invalid_field(ctsio, 5897 /*sks_valid*/ 1, 5898 /*command*/ 1, 5899 /*field*/ 6, 5900 /*bit_valid*/ 0, 5901 /*bit*/ 0); 5902 ctl_done((union ctl_io *)ctsio); 5903 return (CTL_RETVAL_COMPLETE); 5904 } 5905 5906 /* 5907 * If we've got a kernel request that hasn't been malloced yet, 5908 * malloc it and tell the caller the data buffer is here. 5909 */ 5910 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5911 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5912 ctsio->kern_data_len = len; 5913 ctsio->kern_total_len = len; 5914 ctsio->kern_data_resid = 0; 5915 ctsio->kern_rel_offset = 0; 5916 ctsio->kern_sg_entries = 0; 5917 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5918 ctsio->be_move_done = ctl_config_move_done; 5919 ctl_datamove((union ctl_io *)ctsio); 5920 5921 return (CTL_RETVAL_COMPLETE); 5922 } 5923 5924 ctl_done((union ctl_io *)ctsio); 5925 5926 return (CTL_RETVAL_COMPLETE); 5927} 5928 5929int 5930ctl_write_same(struct ctl_scsiio *ctsio) 5931{ 5932 struct ctl_lun *lun; 5933 struct ctl_lba_len_flags *lbalen; 5934 uint64_t lba; 5935 uint32_t num_blocks; 5936 int len, retval; 5937 uint8_t byte2; 5938 5939 retval = CTL_RETVAL_COMPLETE; 5940 5941 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5942 5943 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5944 5945 switch (ctsio->cdb[0]) { 5946 case WRITE_SAME_10: { 5947 struct scsi_write_same_10 *cdb; 5948 5949 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5950 5951 lba = scsi_4btoul(cdb->addr); 5952 num_blocks = scsi_2btoul(cdb->length); 5953 byte2 = cdb->byte2; 5954 break; 5955 } 5956 case WRITE_SAME_16: { 5957 struct scsi_write_same_16 *cdb; 5958 5959 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5960 5961 lba = scsi_8btou64(cdb->addr); 5962 num_blocks = scsi_4btoul(cdb->length); 5963 byte2 = cdb->byte2; 5964 break; 5965 } 5966 default: 5967 /* 5968 * We got a command we don't support. This shouldn't 5969 * happen, commands should be filtered out above us. 5970 */ 5971 ctl_set_invalid_opcode(ctsio); 5972 ctl_done((union ctl_io *)ctsio); 5973 5974 return (CTL_RETVAL_COMPLETE); 5975 break; /* NOTREACHED */ 5976 } 5977 5978 /* 5979 * The first check is to make sure we're in bounds, the second 5980 * check is to catch wrap-around problems. If the lba + num blocks 5981 * is less than the lba, then we've wrapped around and the block 5982 * range is invalid anyway. 5983 */ 5984 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5985 || ((lba + num_blocks) < lba)) { 5986 ctl_set_lba_out_of_range(ctsio); 5987 ctl_done((union ctl_io *)ctsio); 5988 return (CTL_RETVAL_COMPLETE); 5989 } 5990 5991 /* Zero number of blocks means "to the last logical block" */ 5992 if (num_blocks == 0) { 5993 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5994 ctl_set_invalid_field(ctsio, 5995 /*sks_valid*/ 0, 5996 /*command*/ 1, 5997 /*field*/ 0, 5998 /*bit_valid*/ 0, 5999 /*bit*/ 0); 6000 ctl_done((union ctl_io *)ctsio); 6001 return (CTL_RETVAL_COMPLETE); 6002 } 6003 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6004 } 6005 6006 len = lun->be_lun->blocksize; 6007 6008 /* 6009 * If we've got a kernel request that hasn't been malloced yet, 6010 * malloc it and tell the caller the data buffer is here. 6011 */ 6012 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6013 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6014 ctsio->kern_data_len = len; 6015 ctsio->kern_total_len = len; 6016 ctsio->kern_data_resid = 0; 6017 ctsio->kern_rel_offset = 0; 6018 ctsio->kern_sg_entries = 0; 6019 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6020 ctsio->be_move_done = ctl_config_move_done; 6021 ctl_datamove((union ctl_io *)ctsio); 6022 6023 return (CTL_RETVAL_COMPLETE); 6024 } 6025 6026 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6027 lbalen->lba = lba; 6028 lbalen->len = num_blocks; 6029 lbalen->flags = byte2; 6030 retval = lun->backend->config_write((union ctl_io *)ctsio); 6031 6032 return (retval); 6033} 6034 6035int 6036ctl_unmap(struct ctl_scsiio *ctsio) 6037{ 6038 struct ctl_lun *lun; 6039 struct scsi_unmap *cdb; 6040 struct ctl_ptr_len_flags *ptrlen; 6041 struct scsi_unmap_header *hdr; 6042 struct scsi_unmap_desc *buf, *end, *endnz, *range; 6043 uint64_t lba; 6044 uint32_t num_blocks; 6045 int len, retval; 6046 uint8_t byte2; 6047 6048 retval = CTL_RETVAL_COMPLETE; 6049 6050 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6051 6052 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6053 cdb = (struct scsi_unmap *)ctsio->cdb; 6054 6055 len = scsi_2btoul(cdb->length); 6056 byte2 = cdb->byte2; 6057 6058 /* 6059 * If we've got a kernel request that hasn't been malloced yet, 6060 * malloc it and tell the caller the data buffer is here. 6061 */ 6062 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6063 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6064 ctsio->kern_data_len = len; 6065 ctsio->kern_total_len = len; 6066 ctsio->kern_data_resid = 0; 6067 ctsio->kern_rel_offset = 0; 6068 ctsio->kern_sg_entries = 0; 6069 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6070 ctsio->be_move_done = ctl_config_move_done; 6071 ctl_datamove((union ctl_io *)ctsio); 6072 6073 return (CTL_RETVAL_COMPLETE); 6074 } 6075 6076 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6077 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6078 if (len < sizeof (*hdr) || 6079 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6080 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6081 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6082 ctl_set_invalid_field(ctsio, 6083 /*sks_valid*/ 0, 6084 /*command*/ 0, 6085 /*field*/ 0, 6086 /*bit_valid*/ 0, 6087 /*bit*/ 0); 6088 ctl_done((union ctl_io *)ctsio); 6089 return (CTL_RETVAL_COMPLETE); 6090 } 6091 len = scsi_2btoul(hdr->desc_length); 6092 buf = (struct scsi_unmap_desc *)(hdr + 1); 6093 end = buf + len / sizeof(*buf); 6094 6095 endnz = buf; 6096 for (range = buf; range < end; range++) { 6097 lba = scsi_8btou64(range->lba); 6098 num_blocks = scsi_4btoul(range->length); 6099 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6100 || ((lba + num_blocks) < lba)) { 6101 ctl_set_lba_out_of_range(ctsio); 6102 ctl_done((union ctl_io *)ctsio); 6103 return (CTL_RETVAL_COMPLETE); 6104 } 6105 if (num_blocks != 0) 6106 endnz = range + 1; 6107 } 6108 6109 /* 6110 * Block backend can not handle zero last range. 6111 * Filter it out and return if there is nothing left. 6112 */ 6113 len = (uint8_t *)endnz - (uint8_t *)buf; 6114 if (len == 0) { 6115 ctl_set_success(ctsio); 6116 ctl_done((union ctl_io *)ctsio); 6117 return (CTL_RETVAL_COMPLETE); 6118 } 6119 6120 ptrlen = (struct ctl_ptr_len_flags *) 6121 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6122 ptrlen->ptr = (void *)buf; 6123 ptrlen->len = len; 6124 ptrlen->flags = byte2; 6125 6126 retval = lun->backend->config_write((union ctl_io *)ctsio); 6127 return (retval); 6128} 6129 6130/* 6131 * Note that this function currently doesn't actually do anything inside 6132 * CTL to enforce things if the DQue bit is turned on. 6133 * 6134 * Also note that this function can't be used in the default case, because 6135 * the DQue bit isn't set in the changeable mask for the control mode page 6136 * anyway. This is just here as an example for how to implement a page 6137 * handler, and a placeholder in case we want to allow the user to turn 6138 * tagged queueing on and off. 6139 * 6140 * The D_SENSE bit handling is functional, however, and will turn 6141 * descriptor sense on and off for a given LUN. 6142 */ 6143int 6144ctl_control_page_handler(struct ctl_scsiio *ctsio, 6145 struct ctl_page_index *page_index, uint8_t *page_ptr) 6146{ 6147 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6148 struct ctl_lun *lun; 6149 struct ctl_softc *softc; 6150 int set_ua; 6151 uint32_t initidx; 6152 6153 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6154 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6155 set_ua = 0; 6156 6157 user_cp = (struct scsi_control_page *)page_ptr; 6158 current_cp = (struct scsi_control_page *) 6159 (page_index->page_data + (page_index->page_len * 6160 CTL_PAGE_CURRENT)); 6161 saved_cp = (struct scsi_control_page *) 6162 (page_index->page_data + (page_index->page_len * 6163 CTL_PAGE_SAVED)); 6164 6165 softc = control_softc; 6166 6167 mtx_lock(&lun->lun_lock); 6168 if (((current_cp->rlec & SCP_DSENSE) == 0) 6169 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6170 /* 6171 * Descriptor sense is currently turned off and the user 6172 * wants to turn it on. 6173 */ 6174 current_cp->rlec |= SCP_DSENSE; 6175 saved_cp->rlec |= SCP_DSENSE; 6176 lun->flags |= CTL_LUN_SENSE_DESC; 6177 set_ua = 1; 6178 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6179 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6180 /* 6181 * Descriptor sense is currently turned on, and the user 6182 * wants to turn it off. 6183 */ 6184 current_cp->rlec &= ~SCP_DSENSE; 6185 saved_cp->rlec &= ~SCP_DSENSE; 6186 lun->flags &= ~CTL_LUN_SENSE_DESC; 6187 set_ua = 1; 6188 } 6189 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6190 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6191#ifdef NEEDTOPORT 6192 csevent_log(CSC_CTL | CSC_SHELF_SW | 6193 CTL_UNTAG_TO_UNTAG, 6194 csevent_LogType_Trace, 6195 csevent_Severity_Information, 6196 csevent_AlertLevel_Green, 6197 csevent_FRU_Firmware, 6198 csevent_FRU_Unknown, 6199 "Received untagged to untagged transition"); 6200#endif /* NEEDTOPORT */ 6201 } else { 6202#ifdef NEEDTOPORT 6203 csevent_log(CSC_CTL | CSC_SHELF_SW | 6204 CTL_UNTAG_TO_TAG, 6205 csevent_LogType_ConfigChange, 6206 csevent_Severity_Information, 6207 csevent_AlertLevel_Green, 6208 csevent_FRU_Firmware, 6209 csevent_FRU_Unknown, 6210 "Received untagged to tagged " 6211 "queueing transition"); 6212#endif /* NEEDTOPORT */ 6213 6214 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6215 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6216 set_ua = 1; 6217 } 6218 } else { 6219 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6220#ifdef NEEDTOPORT 6221 csevent_log(CSC_CTL | CSC_SHELF_SW | 6222 CTL_TAG_TO_UNTAG, 6223 csevent_LogType_ConfigChange, 6224 csevent_Severity_Warning, 6225 csevent_AlertLevel_Yellow, 6226 csevent_FRU_Firmware, 6227 csevent_FRU_Unknown, 6228 "Received tagged queueing to untagged " 6229 "transition"); 6230#endif /* NEEDTOPORT */ 6231 6232 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6233 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6234 set_ua = 1; 6235 } else { 6236#ifdef NEEDTOPORT 6237 csevent_log(CSC_CTL | CSC_SHELF_SW | 6238 CTL_TAG_TO_TAG, 6239 csevent_LogType_Trace, 6240 csevent_Severity_Information, 6241 csevent_AlertLevel_Green, 6242 csevent_FRU_Firmware, 6243 csevent_FRU_Unknown, 6244 "Received tagged queueing to tagged " 6245 "queueing transition"); 6246#endif /* NEEDTOPORT */ 6247 } 6248 } 6249 if (set_ua != 0) { 6250 int i; 6251 /* 6252 * Let other initiators know that the mode 6253 * parameters for this LUN have changed. 6254 */ 6255 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6256 if (i == initidx) 6257 continue; 6258 6259 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6260 } 6261 } 6262 mtx_unlock(&lun->lun_lock); 6263 6264 return (0); 6265} 6266 6267int 6268ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6269 struct ctl_page_index *page_index, uint8_t *page_ptr) 6270{ 6271 return (0); 6272} 6273 6274int 6275ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6276 struct ctl_page_index *page_index, int pc) 6277{ 6278 struct copan_power_subpage *page; 6279 6280 page = (struct copan_power_subpage *)page_index->page_data + 6281 (page_index->page_len * pc); 6282 6283 switch (pc) { 6284 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6285 /* 6286 * We don't update the changable bits for this page. 6287 */ 6288 break; 6289 case SMS_PAGE_CTRL_CURRENT >> 6: 6290 case SMS_PAGE_CTRL_DEFAULT >> 6: 6291 case SMS_PAGE_CTRL_SAVED >> 6: 6292#ifdef NEEDTOPORT 6293 ctl_update_power_subpage(page); 6294#endif 6295 break; 6296 default: 6297#ifdef NEEDTOPORT 6298 EPRINT(0, "Invalid PC %d!!", pc); 6299#endif 6300 break; 6301 } 6302 return (0); 6303} 6304 6305 6306int 6307ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6308 struct ctl_page_index *page_index, uint8_t *page_ptr) 6309{ 6310 struct copan_aps_subpage *user_sp; 6311 struct copan_aps_subpage *current_sp; 6312 union ctl_modepage_info *modepage_info; 6313 struct ctl_softc *softc; 6314 struct ctl_lun *lun; 6315 int retval; 6316 6317 retval = CTL_RETVAL_COMPLETE; 6318 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6319 (page_index->page_len * CTL_PAGE_CURRENT)); 6320 softc = control_softc; 6321 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6322 6323 user_sp = (struct copan_aps_subpage *)page_ptr; 6324 6325 modepage_info = (union ctl_modepage_info *) 6326 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6327 6328 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6329 modepage_info->header.subpage = page_index->subpage; 6330 modepage_info->aps.lock_active = user_sp->lock_active; 6331 6332 mtx_lock(&softc->ctl_lock); 6333 6334 /* 6335 * If there is a request to lock the LUN and another LUN is locked 6336 * this is an error. If the requested LUN is already locked ignore 6337 * the request. If no LUN is locked attempt to lock it. 6338 * if there is a request to unlock the LUN and the LUN is currently 6339 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6340 * if another LUN is locked or no LUN is locked. 6341 */ 6342 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6343 if (softc->aps_locked_lun == lun->lun) { 6344 /* 6345 * This LUN is already locked, so we're done. 6346 */ 6347 retval = CTL_RETVAL_COMPLETE; 6348 } else if (softc->aps_locked_lun == 0) { 6349 /* 6350 * No one has the lock, pass the request to the 6351 * backend. 6352 */ 6353 retval = lun->backend->config_write( 6354 (union ctl_io *)ctsio); 6355 } else { 6356 /* 6357 * Someone else has the lock, throw out the request. 6358 */ 6359 ctl_set_already_locked(ctsio); 6360 free(ctsio->kern_data_ptr, M_CTL); 6361 ctl_done((union ctl_io *)ctsio); 6362 6363 /* 6364 * Set the return value so that ctl_do_mode_select() 6365 * won't try to complete the command. We already 6366 * completed it here. 6367 */ 6368 retval = CTL_RETVAL_ERROR; 6369 } 6370 } else if (softc->aps_locked_lun == lun->lun) { 6371 /* 6372 * This LUN is locked, so pass the unlock request to the 6373 * backend. 6374 */ 6375 retval = lun->backend->config_write((union ctl_io *)ctsio); 6376 } 6377 mtx_unlock(&softc->ctl_lock); 6378 6379 return (retval); 6380} 6381 6382int 6383ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6384 struct ctl_page_index *page_index, 6385 uint8_t *page_ptr) 6386{ 6387 uint8_t *c; 6388 int i; 6389 6390 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6391 ctl_time_io_secs = 6392 (c[0] << 8) | 6393 (c[1] << 0) | 6394 0; 6395 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6396 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6397 printf("page data:"); 6398 for (i=0; i<8; i++) 6399 printf(" %.2x",page_ptr[i]); 6400 printf("\n"); 6401 return (0); 6402} 6403 6404int 6405ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6406 struct ctl_page_index *page_index, 6407 int pc) 6408{ 6409 struct copan_debugconf_subpage *page; 6410 6411 page = (struct copan_debugconf_subpage *)page_index->page_data + 6412 (page_index->page_len * pc); 6413 6414 switch (pc) { 6415 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6416 case SMS_PAGE_CTRL_DEFAULT >> 6: 6417 case SMS_PAGE_CTRL_SAVED >> 6: 6418 /* 6419 * We don't update the changable or default bits for this page. 6420 */ 6421 break; 6422 case SMS_PAGE_CTRL_CURRENT >> 6: 6423 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6424 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6425 break; 6426 default: 6427#ifdef NEEDTOPORT 6428 EPRINT(0, "Invalid PC %d!!", pc); 6429#endif /* NEEDTOPORT */ 6430 break; 6431 } 6432 return (0); 6433} 6434 6435 6436static int 6437ctl_do_mode_select(union ctl_io *io) 6438{ 6439 struct scsi_mode_page_header *page_header; 6440 struct ctl_page_index *page_index; 6441 struct ctl_scsiio *ctsio; 6442 int control_dev, page_len; 6443 int page_len_offset, page_len_size; 6444 union ctl_modepage_info *modepage_info; 6445 struct ctl_lun *lun; 6446 int *len_left, *len_used; 6447 int retval, i; 6448 6449 ctsio = &io->scsiio; 6450 page_index = NULL; 6451 page_len = 0; 6452 retval = CTL_RETVAL_COMPLETE; 6453 6454 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6455 6456 if (lun->be_lun->lun_type != T_DIRECT) 6457 control_dev = 1; 6458 else 6459 control_dev = 0; 6460 6461 modepage_info = (union ctl_modepage_info *) 6462 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6463 len_left = &modepage_info->header.len_left; 6464 len_used = &modepage_info->header.len_used; 6465 6466do_next_page: 6467 6468 page_header = (struct scsi_mode_page_header *) 6469 (ctsio->kern_data_ptr + *len_used); 6470 6471 if (*len_left == 0) { 6472 free(ctsio->kern_data_ptr, M_CTL); 6473 ctl_set_success(ctsio); 6474 ctl_done((union ctl_io *)ctsio); 6475 return (CTL_RETVAL_COMPLETE); 6476 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6477 6478 free(ctsio->kern_data_ptr, M_CTL); 6479 ctl_set_param_len_error(ctsio); 6480 ctl_done((union ctl_io *)ctsio); 6481 return (CTL_RETVAL_COMPLETE); 6482 6483 } else if ((page_header->page_code & SMPH_SPF) 6484 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6485 6486 free(ctsio->kern_data_ptr, M_CTL); 6487 ctl_set_param_len_error(ctsio); 6488 ctl_done((union ctl_io *)ctsio); 6489 return (CTL_RETVAL_COMPLETE); 6490 } 6491 6492 6493 /* 6494 * XXX KDM should we do something with the block descriptor? 6495 */ 6496 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6497 6498 if ((control_dev != 0) 6499 && (lun->mode_pages.index[i].page_flags & 6500 CTL_PAGE_FLAG_DISK_ONLY)) 6501 continue; 6502 6503 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6504 (page_header->page_code & SMPH_PC_MASK)) 6505 continue; 6506 6507 /* 6508 * If neither page has a subpage code, then we've got a 6509 * match. 6510 */ 6511 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6512 && ((page_header->page_code & SMPH_SPF) == 0)) { 6513 page_index = &lun->mode_pages.index[i]; 6514 page_len = page_header->page_length; 6515 break; 6516 } 6517 6518 /* 6519 * If both pages have subpages, then the subpage numbers 6520 * have to match. 6521 */ 6522 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6523 && (page_header->page_code & SMPH_SPF)) { 6524 struct scsi_mode_page_header_sp *sph; 6525 6526 sph = (struct scsi_mode_page_header_sp *)page_header; 6527 6528 if (lun->mode_pages.index[i].subpage == 6529 sph->subpage) { 6530 page_index = &lun->mode_pages.index[i]; 6531 page_len = scsi_2btoul(sph->page_length); 6532 break; 6533 } 6534 } 6535 } 6536 6537 /* 6538 * If we couldn't find the page, or if we don't have a mode select 6539 * handler for it, send back an error to the user. 6540 */ 6541 if ((page_index == NULL) 6542 || (page_index->select_handler == NULL)) { 6543 ctl_set_invalid_field(ctsio, 6544 /*sks_valid*/ 1, 6545 /*command*/ 0, 6546 /*field*/ *len_used, 6547 /*bit_valid*/ 0, 6548 /*bit*/ 0); 6549 free(ctsio->kern_data_ptr, M_CTL); 6550 ctl_done((union ctl_io *)ctsio); 6551 return (CTL_RETVAL_COMPLETE); 6552 } 6553 6554 if (page_index->page_code & SMPH_SPF) { 6555 page_len_offset = 2; 6556 page_len_size = 2; 6557 } else { 6558 page_len_size = 1; 6559 page_len_offset = 1; 6560 } 6561 6562 /* 6563 * If the length the initiator gives us isn't the one we specify in 6564 * the mode page header, or if they didn't specify enough data in 6565 * the CDB to avoid truncating this page, kick out the request. 6566 */ 6567 if ((page_len != (page_index->page_len - page_len_offset - 6568 page_len_size)) 6569 || (*len_left < page_index->page_len)) { 6570 6571 6572 ctl_set_invalid_field(ctsio, 6573 /*sks_valid*/ 1, 6574 /*command*/ 0, 6575 /*field*/ *len_used + page_len_offset, 6576 /*bit_valid*/ 0, 6577 /*bit*/ 0); 6578 free(ctsio->kern_data_ptr, M_CTL); 6579 ctl_done((union ctl_io *)ctsio); 6580 return (CTL_RETVAL_COMPLETE); 6581 } 6582 6583 /* 6584 * Run through the mode page, checking to make sure that the bits 6585 * the user changed are actually legal for him to change. 6586 */ 6587 for (i = 0; i < page_index->page_len; i++) { 6588 uint8_t *user_byte, *change_mask, *current_byte; 6589 int bad_bit; 6590 int j; 6591 6592 user_byte = (uint8_t *)page_header + i; 6593 change_mask = page_index->page_data + 6594 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6595 current_byte = page_index->page_data + 6596 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6597 6598 /* 6599 * Check to see whether the user set any bits in this byte 6600 * that he is not allowed to set. 6601 */ 6602 if ((*user_byte & ~(*change_mask)) == 6603 (*current_byte & ~(*change_mask))) 6604 continue; 6605 6606 /* 6607 * Go through bit by bit to determine which one is illegal. 6608 */ 6609 bad_bit = 0; 6610 for (j = 7; j >= 0; j--) { 6611 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6612 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6613 bad_bit = i; 6614 break; 6615 } 6616 } 6617 ctl_set_invalid_field(ctsio, 6618 /*sks_valid*/ 1, 6619 /*command*/ 0, 6620 /*field*/ *len_used + i, 6621 /*bit_valid*/ 1, 6622 /*bit*/ bad_bit); 6623 free(ctsio->kern_data_ptr, M_CTL); 6624 ctl_done((union ctl_io *)ctsio); 6625 return (CTL_RETVAL_COMPLETE); 6626 } 6627 6628 /* 6629 * Decrement these before we call the page handler, since we may 6630 * end up getting called back one way or another before the handler 6631 * returns to this context. 6632 */ 6633 *len_left -= page_index->page_len; 6634 *len_used += page_index->page_len; 6635 6636 retval = page_index->select_handler(ctsio, page_index, 6637 (uint8_t *)page_header); 6638 6639 /* 6640 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6641 * wait until this queued command completes to finish processing 6642 * the mode page. If it returns anything other than 6643 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6644 * already set the sense information, freed the data pointer, and 6645 * completed the io for us. 6646 */ 6647 if (retval != CTL_RETVAL_COMPLETE) 6648 goto bailout_no_done; 6649 6650 /* 6651 * If the initiator sent us more than one page, parse the next one. 6652 */ 6653 if (*len_left > 0) 6654 goto do_next_page; 6655 6656 ctl_set_success(ctsio); 6657 free(ctsio->kern_data_ptr, M_CTL); 6658 ctl_done((union ctl_io *)ctsio); 6659 6660bailout_no_done: 6661 6662 return (CTL_RETVAL_COMPLETE); 6663 6664} 6665 6666int 6667ctl_mode_select(struct ctl_scsiio *ctsio) 6668{ 6669 int param_len, pf, sp; 6670 int header_size, bd_len; 6671 int len_left, len_used; 6672 struct ctl_page_index *page_index; 6673 struct ctl_lun *lun; 6674 int control_dev, page_len; 6675 union ctl_modepage_info *modepage_info; 6676 int retval; 6677 6678 pf = 0; 6679 sp = 0; 6680 page_len = 0; 6681 len_used = 0; 6682 len_left = 0; 6683 retval = 0; 6684 bd_len = 0; 6685 page_index = NULL; 6686 6687 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6688 6689 if (lun->be_lun->lun_type != T_DIRECT) 6690 control_dev = 1; 6691 else 6692 control_dev = 0; 6693 6694 switch (ctsio->cdb[0]) { 6695 case MODE_SELECT_6: { 6696 struct scsi_mode_select_6 *cdb; 6697 6698 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6699 6700 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6701 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6702 6703 param_len = cdb->length; 6704 header_size = sizeof(struct scsi_mode_header_6); 6705 break; 6706 } 6707 case MODE_SELECT_10: { 6708 struct scsi_mode_select_10 *cdb; 6709 6710 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6711 6712 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6713 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6714 6715 param_len = scsi_2btoul(cdb->length); 6716 header_size = sizeof(struct scsi_mode_header_10); 6717 break; 6718 } 6719 default: 6720 ctl_set_invalid_opcode(ctsio); 6721 ctl_done((union ctl_io *)ctsio); 6722 return (CTL_RETVAL_COMPLETE); 6723 break; /* NOTREACHED */ 6724 } 6725 6726 /* 6727 * From SPC-3: 6728 * "A parameter list length of zero indicates that the Data-Out Buffer 6729 * shall be empty. This condition shall not be considered as an error." 6730 */ 6731 if (param_len == 0) { 6732 ctl_set_success(ctsio); 6733 ctl_done((union ctl_io *)ctsio); 6734 return (CTL_RETVAL_COMPLETE); 6735 } 6736 6737 /* 6738 * Since we'll hit this the first time through, prior to 6739 * allocation, we don't need to free a data buffer here. 6740 */ 6741 if (param_len < header_size) { 6742 ctl_set_param_len_error(ctsio); 6743 ctl_done((union ctl_io *)ctsio); 6744 return (CTL_RETVAL_COMPLETE); 6745 } 6746 6747 /* 6748 * Allocate the data buffer and grab the user's data. In theory, 6749 * we shouldn't have to sanity check the parameter list length here 6750 * because the maximum size is 64K. We should be able to malloc 6751 * that much without too many problems. 6752 */ 6753 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6754 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6755 ctsio->kern_data_len = param_len; 6756 ctsio->kern_total_len = param_len; 6757 ctsio->kern_data_resid = 0; 6758 ctsio->kern_rel_offset = 0; 6759 ctsio->kern_sg_entries = 0; 6760 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6761 ctsio->be_move_done = ctl_config_move_done; 6762 ctl_datamove((union ctl_io *)ctsio); 6763 6764 return (CTL_RETVAL_COMPLETE); 6765 } 6766 6767 switch (ctsio->cdb[0]) { 6768 case MODE_SELECT_6: { 6769 struct scsi_mode_header_6 *mh6; 6770 6771 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6772 bd_len = mh6->blk_desc_len; 6773 break; 6774 } 6775 case MODE_SELECT_10: { 6776 struct scsi_mode_header_10 *mh10; 6777 6778 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6779 bd_len = scsi_2btoul(mh10->blk_desc_len); 6780 break; 6781 } 6782 default: 6783 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6784 break; 6785 } 6786 6787 if (param_len < (header_size + bd_len)) { 6788 free(ctsio->kern_data_ptr, M_CTL); 6789 ctl_set_param_len_error(ctsio); 6790 ctl_done((union ctl_io *)ctsio); 6791 return (CTL_RETVAL_COMPLETE); 6792 } 6793 6794 /* 6795 * Set the IO_CONT flag, so that if this I/O gets passed to 6796 * ctl_config_write_done(), it'll get passed back to 6797 * ctl_do_mode_select() for further processing, or completion if 6798 * we're all done. 6799 */ 6800 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6801 ctsio->io_cont = ctl_do_mode_select; 6802 6803 modepage_info = (union ctl_modepage_info *) 6804 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6805 6806 memset(modepage_info, 0, sizeof(*modepage_info)); 6807 6808 len_left = param_len - header_size - bd_len; 6809 len_used = header_size + bd_len; 6810 6811 modepage_info->header.len_left = len_left; 6812 modepage_info->header.len_used = len_used; 6813 6814 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6815} 6816 6817int 6818ctl_mode_sense(struct ctl_scsiio *ctsio) 6819{ 6820 struct ctl_lun *lun; 6821 int pc, page_code, dbd, llba, subpage; 6822 int alloc_len, page_len, header_len, total_len; 6823 struct scsi_mode_block_descr *block_desc; 6824 struct ctl_page_index *page_index; 6825 int control_dev; 6826 6827 dbd = 0; 6828 llba = 0; 6829 block_desc = NULL; 6830 page_index = NULL; 6831 6832 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6833 6834 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6835 6836 if (lun->be_lun->lun_type != T_DIRECT) 6837 control_dev = 1; 6838 else 6839 control_dev = 0; 6840 6841 if (lun->flags & CTL_LUN_PR_RESERVED) { 6842 uint32_t residx; 6843 6844 /* 6845 * XXX KDM need a lock here. 6846 */ 6847 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6848 if ((lun->res_type == SPR_TYPE_EX_AC 6849 && residx != lun->pr_res_idx) 6850 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6851 || lun->res_type == SPR_TYPE_EX_AC_AR) 6852 && !lun->per_res[residx].registered)) { 6853 ctl_set_reservation_conflict(ctsio); 6854 ctl_done((union ctl_io *)ctsio); 6855 return (CTL_RETVAL_COMPLETE); 6856 } 6857 } 6858 6859 switch (ctsio->cdb[0]) { 6860 case MODE_SENSE_6: { 6861 struct scsi_mode_sense_6 *cdb; 6862 6863 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6864 6865 header_len = sizeof(struct scsi_mode_hdr_6); 6866 if (cdb->byte2 & SMS_DBD) 6867 dbd = 1; 6868 else 6869 header_len += sizeof(struct scsi_mode_block_descr); 6870 6871 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6872 page_code = cdb->page & SMS_PAGE_CODE; 6873 subpage = cdb->subpage; 6874 alloc_len = cdb->length; 6875 break; 6876 } 6877 case MODE_SENSE_10: { 6878 struct scsi_mode_sense_10 *cdb; 6879 6880 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6881 6882 header_len = sizeof(struct scsi_mode_hdr_10); 6883 6884 if (cdb->byte2 & SMS_DBD) 6885 dbd = 1; 6886 else 6887 header_len += sizeof(struct scsi_mode_block_descr); 6888 if (cdb->byte2 & SMS10_LLBAA) 6889 llba = 1; 6890 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6891 page_code = cdb->page & SMS_PAGE_CODE; 6892 subpage = cdb->subpage; 6893 alloc_len = scsi_2btoul(cdb->length); 6894 break; 6895 } 6896 default: 6897 ctl_set_invalid_opcode(ctsio); 6898 ctl_done((union ctl_io *)ctsio); 6899 return (CTL_RETVAL_COMPLETE); 6900 break; /* NOTREACHED */ 6901 } 6902 6903 /* 6904 * We have to make a first pass through to calculate the size of 6905 * the pages that match the user's query. Then we allocate enough 6906 * memory to hold it, and actually copy the data into the buffer. 6907 */ 6908 switch (page_code) { 6909 case SMS_ALL_PAGES_PAGE: { 6910 int i; 6911 6912 page_len = 0; 6913 6914 /* 6915 * At the moment, values other than 0 and 0xff here are 6916 * reserved according to SPC-3. 6917 */ 6918 if ((subpage != SMS_SUBPAGE_PAGE_0) 6919 && (subpage != SMS_SUBPAGE_ALL)) { 6920 ctl_set_invalid_field(ctsio, 6921 /*sks_valid*/ 1, 6922 /*command*/ 1, 6923 /*field*/ 3, 6924 /*bit_valid*/ 0, 6925 /*bit*/ 0); 6926 ctl_done((union ctl_io *)ctsio); 6927 return (CTL_RETVAL_COMPLETE); 6928 } 6929 6930 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6931 if ((control_dev != 0) 6932 && (lun->mode_pages.index[i].page_flags & 6933 CTL_PAGE_FLAG_DISK_ONLY)) 6934 continue; 6935 6936 /* 6937 * We don't use this subpage if the user didn't 6938 * request all subpages. 6939 */ 6940 if ((lun->mode_pages.index[i].subpage != 0) 6941 && (subpage == SMS_SUBPAGE_PAGE_0)) 6942 continue; 6943 6944#if 0 6945 printf("found page %#x len %d\n", 6946 lun->mode_pages.index[i].page_code & 6947 SMPH_PC_MASK, 6948 lun->mode_pages.index[i].page_len); 6949#endif 6950 page_len += lun->mode_pages.index[i].page_len; 6951 } 6952 break; 6953 } 6954 default: { 6955 int i; 6956 6957 page_len = 0; 6958 6959 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6960 /* Look for the right page code */ 6961 if ((lun->mode_pages.index[i].page_code & 6962 SMPH_PC_MASK) != page_code) 6963 continue; 6964 6965 /* Look for the right subpage or the subpage wildcard*/ 6966 if ((lun->mode_pages.index[i].subpage != subpage) 6967 && (subpage != SMS_SUBPAGE_ALL)) 6968 continue; 6969 6970 /* Make sure the page is supported for this dev type */ 6971 if ((control_dev != 0) 6972 && (lun->mode_pages.index[i].page_flags & 6973 CTL_PAGE_FLAG_DISK_ONLY)) 6974 continue; 6975 6976#if 0 6977 printf("found page %#x len %d\n", 6978 lun->mode_pages.index[i].page_code & 6979 SMPH_PC_MASK, 6980 lun->mode_pages.index[i].page_len); 6981#endif 6982 6983 page_len += lun->mode_pages.index[i].page_len; 6984 } 6985 6986 if (page_len == 0) { 6987 ctl_set_invalid_field(ctsio, 6988 /*sks_valid*/ 1, 6989 /*command*/ 1, 6990 /*field*/ 2, 6991 /*bit_valid*/ 1, 6992 /*bit*/ 5); 6993 ctl_done((union ctl_io *)ctsio); 6994 return (CTL_RETVAL_COMPLETE); 6995 } 6996 break; 6997 } 6998 } 6999 7000 total_len = header_len + page_len; 7001#if 0 7002 printf("header_len = %d, page_len = %d, total_len = %d\n", 7003 header_len, page_len, total_len); 7004#endif 7005 7006 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7007 ctsio->kern_sg_entries = 0; 7008 ctsio->kern_data_resid = 0; 7009 ctsio->kern_rel_offset = 0; 7010 if (total_len < alloc_len) { 7011 ctsio->residual = alloc_len - total_len; 7012 ctsio->kern_data_len = total_len; 7013 ctsio->kern_total_len = total_len; 7014 } else { 7015 ctsio->residual = 0; 7016 ctsio->kern_data_len = alloc_len; 7017 ctsio->kern_total_len = alloc_len; 7018 } 7019 7020 switch (ctsio->cdb[0]) { 7021 case MODE_SENSE_6: { 7022 struct scsi_mode_hdr_6 *header; 7023 7024 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7025 7026 header->datalen = ctl_min(total_len - 1, 254); 7027 7028 if (dbd) 7029 header->block_descr_len = 0; 7030 else 7031 header->block_descr_len = 7032 sizeof(struct scsi_mode_block_descr); 7033 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7034 break; 7035 } 7036 case MODE_SENSE_10: { 7037 struct scsi_mode_hdr_10 *header; 7038 int datalen; 7039 7040 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7041 7042 datalen = ctl_min(total_len - 2, 65533); 7043 scsi_ulto2b(datalen, header->datalen); 7044 if (dbd) 7045 scsi_ulto2b(0, header->block_descr_len); 7046 else 7047 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7048 header->block_descr_len); 7049 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7050 break; 7051 } 7052 default: 7053 panic("invalid CDB type %#x", ctsio->cdb[0]); 7054 break; /* NOTREACHED */ 7055 } 7056 7057 /* 7058 * If we've got a disk, use its blocksize in the block 7059 * descriptor. Otherwise, just set it to 0. 7060 */ 7061 if (dbd == 0) { 7062 if (control_dev != 0) 7063 scsi_ulto3b(lun->be_lun->blocksize, 7064 block_desc->block_len); 7065 else 7066 scsi_ulto3b(0, block_desc->block_len); 7067 } 7068 7069 switch (page_code) { 7070 case SMS_ALL_PAGES_PAGE: { 7071 int i, data_used; 7072 7073 data_used = header_len; 7074 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7075 struct ctl_page_index *page_index; 7076 7077 page_index = &lun->mode_pages.index[i]; 7078 7079 if ((control_dev != 0) 7080 && (page_index->page_flags & 7081 CTL_PAGE_FLAG_DISK_ONLY)) 7082 continue; 7083 7084 /* 7085 * We don't use this subpage if the user didn't 7086 * request all subpages. We already checked (above) 7087 * to make sure the user only specified a subpage 7088 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7089 */ 7090 if ((page_index->subpage != 0) 7091 && (subpage == SMS_SUBPAGE_PAGE_0)) 7092 continue; 7093 7094 /* 7095 * Call the handler, if it exists, to update the 7096 * page to the latest values. 7097 */ 7098 if (page_index->sense_handler != NULL) 7099 page_index->sense_handler(ctsio, page_index,pc); 7100 7101 memcpy(ctsio->kern_data_ptr + data_used, 7102 page_index->page_data + 7103 (page_index->page_len * pc), 7104 page_index->page_len); 7105 data_used += page_index->page_len; 7106 } 7107 break; 7108 } 7109 default: { 7110 int i, data_used; 7111 7112 data_used = header_len; 7113 7114 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7115 struct ctl_page_index *page_index; 7116 7117 page_index = &lun->mode_pages.index[i]; 7118 7119 /* Look for the right page code */ 7120 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7121 continue; 7122 7123 /* Look for the right subpage or the subpage wildcard*/ 7124 if ((page_index->subpage != subpage) 7125 && (subpage != SMS_SUBPAGE_ALL)) 7126 continue; 7127 7128 /* Make sure the page is supported for this dev type */ 7129 if ((control_dev != 0) 7130 && (page_index->page_flags & 7131 CTL_PAGE_FLAG_DISK_ONLY)) 7132 continue; 7133 7134 /* 7135 * Call the handler, if it exists, to update the 7136 * page to the latest values. 7137 */ 7138 if (page_index->sense_handler != NULL) 7139 page_index->sense_handler(ctsio, page_index,pc); 7140 7141 memcpy(ctsio->kern_data_ptr + data_used, 7142 page_index->page_data + 7143 (page_index->page_len * pc), 7144 page_index->page_len); 7145 data_used += page_index->page_len; 7146 } 7147 break; 7148 } 7149 } 7150 7151 ctsio->scsi_status = SCSI_STATUS_OK; 7152 7153 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7154 ctsio->be_move_done = ctl_config_move_done; 7155 ctl_datamove((union ctl_io *)ctsio); 7156 7157 return (CTL_RETVAL_COMPLETE); 7158} 7159 7160int 7161ctl_read_capacity(struct ctl_scsiio *ctsio) 7162{ 7163 struct scsi_read_capacity *cdb; 7164 struct scsi_read_capacity_data *data; 7165 struct ctl_lun *lun; 7166 uint32_t lba; 7167 7168 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7169 7170 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7171 7172 lba = scsi_4btoul(cdb->addr); 7173 if (((cdb->pmi & SRC_PMI) == 0) 7174 && (lba != 0)) { 7175 ctl_set_invalid_field(/*ctsio*/ ctsio, 7176 /*sks_valid*/ 1, 7177 /*command*/ 1, 7178 /*field*/ 2, 7179 /*bit_valid*/ 0, 7180 /*bit*/ 0); 7181 ctl_done((union ctl_io *)ctsio); 7182 return (CTL_RETVAL_COMPLETE); 7183 } 7184 7185 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7186 7187 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7188 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7189 ctsio->residual = 0; 7190 ctsio->kern_data_len = sizeof(*data); 7191 ctsio->kern_total_len = sizeof(*data); 7192 ctsio->kern_data_resid = 0; 7193 ctsio->kern_rel_offset = 0; 7194 ctsio->kern_sg_entries = 0; 7195 7196 /* 7197 * If the maximum LBA is greater than 0xfffffffe, the user must 7198 * issue a SERVICE ACTION IN (16) command, with the read capacity 7199 * serivce action set. 7200 */ 7201 if (lun->be_lun->maxlba > 0xfffffffe) 7202 scsi_ulto4b(0xffffffff, data->addr); 7203 else 7204 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7205 7206 /* 7207 * XXX KDM this may not be 512 bytes... 7208 */ 7209 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7210 7211 ctsio->scsi_status = SCSI_STATUS_OK; 7212 7213 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7214 ctsio->be_move_done = ctl_config_move_done; 7215 ctl_datamove((union ctl_io *)ctsio); 7216 7217 return (CTL_RETVAL_COMPLETE); 7218} 7219 7220int 7221ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7222{ 7223 struct scsi_read_capacity_16 *cdb; 7224 struct scsi_read_capacity_data_long *data; 7225 struct ctl_lun *lun; 7226 uint64_t lba; 7227 uint32_t alloc_len; 7228 7229 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7230 7231 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7232 7233 alloc_len = scsi_4btoul(cdb->alloc_len); 7234 lba = scsi_8btou64(cdb->addr); 7235 7236 if ((cdb->reladr & SRC16_PMI) 7237 && (lba != 0)) { 7238 ctl_set_invalid_field(/*ctsio*/ ctsio, 7239 /*sks_valid*/ 1, 7240 /*command*/ 1, 7241 /*field*/ 2, 7242 /*bit_valid*/ 0, 7243 /*bit*/ 0); 7244 ctl_done((union ctl_io *)ctsio); 7245 return (CTL_RETVAL_COMPLETE); 7246 } 7247 7248 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7249 7250 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7251 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7252 7253 if (sizeof(*data) < alloc_len) { 7254 ctsio->residual = alloc_len - sizeof(*data); 7255 ctsio->kern_data_len = sizeof(*data); 7256 ctsio->kern_total_len = sizeof(*data); 7257 } else { 7258 ctsio->residual = 0; 7259 ctsio->kern_data_len = alloc_len; 7260 ctsio->kern_total_len = alloc_len; 7261 } 7262 ctsio->kern_data_resid = 0; 7263 ctsio->kern_rel_offset = 0; 7264 ctsio->kern_sg_entries = 0; 7265 7266 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7267 /* XXX KDM this may not be 512 bytes... */ 7268 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7269 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7270 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7271 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7272 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7273 7274 ctsio->scsi_status = SCSI_STATUS_OK; 7275 7276 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7277 ctsio->be_move_done = ctl_config_move_done; 7278 ctl_datamove((union ctl_io *)ctsio); 7279 7280 return (CTL_RETVAL_COMPLETE); 7281} 7282 7283int 7284ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7285{ 7286 struct scsi_maintenance_in *cdb; 7287 int retval; 7288 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7289 int num_target_port_groups, num_target_ports, single; 7290 struct ctl_lun *lun; 7291 struct ctl_softc *softc; 7292 struct ctl_port *port; 7293 struct scsi_target_group_data *rtg_ptr; 7294 struct scsi_target_group_data_extended *rtg_ext_ptr; 7295 struct scsi_target_port_group_descriptor *tpg_desc; 7296 7297 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7298 7299 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7300 softc = control_softc; 7301 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7302 7303 retval = CTL_RETVAL_COMPLETE; 7304 7305 switch (cdb->byte2 & STG_PDF_MASK) { 7306 case STG_PDF_LENGTH: 7307 ext = 0; 7308 break; 7309 case STG_PDF_EXTENDED: 7310 ext = 1; 7311 break; 7312 default: 7313 ctl_set_invalid_field(/*ctsio*/ ctsio, 7314 /*sks_valid*/ 1, 7315 /*command*/ 1, 7316 /*field*/ 2, 7317 /*bit_valid*/ 1, 7318 /*bit*/ 5); 7319 ctl_done((union ctl_io *)ctsio); 7320 return(retval); 7321 } 7322 7323 single = ctl_is_single; 7324 if (single) 7325 num_target_port_groups = 1; 7326 else 7327 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7328 num_target_ports = 0; 7329 mtx_lock(&softc->ctl_lock); 7330 STAILQ_FOREACH(port, &softc->port_list, links) { 7331 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7332 continue; 7333 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7334 continue; 7335 num_target_ports++; 7336 } 7337 mtx_unlock(&softc->ctl_lock); 7338 7339 if (ext) 7340 total_len = sizeof(struct scsi_target_group_data_extended); 7341 else 7342 total_len = sizeof(struct scsi_target_group_data); 7343 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7344 num_target_port_groups + 7345 sizeof(struct scsi_target_port_descriptor) * 7346 num_target_ports * num_target_port_groups; 7347 7348 alloc_len = scsi_4btoul(cdb->length); 7349 7350 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7351 7352 ctsio->kern_sg_entries = 0; 7353 7354 if (total_len < alloc_len) { 7355 ctsio->residual = alloc_len - total_len; 7356 ctsio->kern_data_len = total_len; 7357 ctsio->kern_total_len = total_len; 7358 } else { 7359 ctsio->residual = 0; 7360 ctsio->kern_data_len = alloc_len; 7361 ctsio->kern_total_len = alloc_len; 7362 } 7363 ctsio->kern_data_resid = 0; 7364 ctsio->kern_rel_offset = 0; 7365 7366 if (ext) { 7367 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7368 ctsio->kern_data_ptr; 7369 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7370 rtg_ext_ptr->format_type = 0x10; 7371 rtg_ext_ptr->implicit_transition_time = 0; 7372 tpg_desc = &rtg_ext_ptr->groups[0]; 7373 } else { 7374 rtg_ptr = (struct scsi_target_group_data *) 7375 ctsio->kern_data_ptr; 7376 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7377 tpg_desc = &rtg_ptr->groups[0]; 7378 } 7379 7380 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7381 mtx_lock(&softc->ctl_lock); 7382 for (g = 0; g < num_target_port_groups; g++) { 7383 if (g == pg) 7384 tpg_desc->pref_state = TPG_PRIMARY | 7385 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7386 else 7387 tpg_desc->pref_state = 7388 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7389 tpg_desc->support = TPG_AO_SUP; 7390 if (!single) 7391 tpg_desc->support |= TPG_AN_SUP; 7392 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7393 tpg_desc->status = TPG_IMPLICIT; 7394 pc = 0; 7395 STAILQ_FOREACH(port, &softc->port_list, links) { 7396 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7397 continue; 7398 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7399 CTL_MAX_LUNS) 7400 continue; 7401 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7402 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7403 relative_target_port_identifier); 7404 pc++; 7405 } 7406 tpg_desc->target_port_count = pc; 7407 tpg_desc = (struct scsi_target_port_group_descriptor *) 7408 &tpg_desc->descriptors[pc]; 7409 } 7410 mtx_unlock(&softc->ctl_lock); 7411 7412 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7413 ctsio->be_move_done = ctl_config_move_done; 7414 7415 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7416 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7417 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7418 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7419 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7420 7421 ctl_datamove((union ctl_io *)ctsio); 7422 return(retval); 7423} 7424 7425int 7426ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7427{ 7428 struct ctl_lun *lun; 7429 struct scsi_report_supported_opcodes *cdb; 7430 const struct ctl_cmd_entry *entry, *sentry; 7431 struct scsi_report_supported_opcodes_all *all; 7432 struct scsi_report_supported_opcodes_descr *descr; 7433 struct scsi_report_supported_opcodes_one *one; 7434 int retval; 7435 int alloc_len, total_len; 7436 int opcode, service_action, i, j, num; 7437 7438 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7439 7440 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7441 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7442 7443 retval = CTL_RETVAL_COMPLETE; 7444 7445 opcode = cdb->requested_opcode; 7446 service_action = scsi_2btoul(cdb->requested_service_action); 7447 switch (cdb->options & RSO_OPTIONS_MASK) { 7448 case RSO_OPTIONS_ALL: 7449 num = 0; 7450 for (i = 0; i < 256; i++) { 7451 entry = &ctl_cmd_table[i]; 7452 if (entry->flags & CTL_CMD_FLAG_SA5) { 7453 for (j = 0; j < 32; j++) { 7454 sentry = &((const struct ctl_cmd_entry *) 7455 entry->execute)[j]; 7456 if (ctl_cmd_applicable( 7457 lun->be_lun->lun_type, sentry)) 7458 num++; 7459 } 7460 } else { 7461 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7462 entry)) 7463 num++; 7464 } 7465 } 7466 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7467 num * sizeof(struct scsi_report_supported_opcodes_descr); 7468 break; 7469 case RSO_OPTIONS_OC: 7470 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7471 ctl_set_invalid_field(/*ctsio*/ ctsio, 7472 /*sks_valid*/ 1, 7473 /*command*/ 1, 7474 /*field*/ 2, 7475 /*bit_valid*/ 1, 7476 /*bit*/ 2); 7477 ctl_done((union ctl_io *)ctsio); 7478 return (CTL_RETVAL_COMPLETE); 7479 } 7480 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7481 break; 7482 case RSO_OPTIONS_OC_SA: 7483 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7484 service_action >= 32) { 7485 ctl_set_invalid_field(/*ctsio*/ ctsio, 7486 /*sks_valid*/ 1, 7487 /*command*/ 1, 7488 /*field*/ 2, 7489 /*bit_valid*/ 1, 7490 /*bit*/ 2); 7491 ctl_done((union ctl_io *)ctsio); 7492 return (CTL_RETVAL_COMPLETE); 7493 } 7494 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7495 break; 7496 default: 7497 ctl_set_invalid_field(/*ctsio*/ ctsio, 7498 /*sks_valid*/ 1, 7499 /*command*/ 1, 7500 /*field*/ 2, 7501 /*bit_valid*/ 1, 7502 /*bit*/ 2); 7503 ctl_done((union ctl_io *)ctsio); 7504 return (CTL_RETVAL_COMPLETE); 7505 } 7506 7507 alloc_len = scsi_4btoul(cdb->length); 7508 7509 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7510 7511 ctsio->kern_sg_entries = 0; 7512 7513 if (total_len < alloc_len) { 7514 ctsio->residual = alloc_len - total_len; 7515 ctsio->kern_data_len = total_len; 7516 ctsio->kern_total_len = total_len; 7517 } else { 7518 ctsio->residual = 0; 7519 ctsio->kern_data_len = alloc_len; 7520 ctsio->kern_total_len = alloc_len; 7521 } 7522 ctsio->kern_data_resid = 0; 7523 ctsio->kern_rel_offset = 0; 7524 7525 switch (cdb->options & RSO_OPTIONS_MASK) { 7526 case RSO_OPTIONS_ALL: 7527 all = (struct scsi_report_supported_opcodes_all *) 7528 ctsio->kern_data_ptr; 7529 num = 0; 7530 for (i = 0; i < 256; i++) { 7531 entry = &ctl_cmd_table[i]; 7532 if (entry->flags & CTL_CMD_FLAG_SA5) { 7533 for (j = 0; j < 32; j++) { 7534 sentry = &((const struct ctl_cmd_entry *) 7535 entry->execute)[j]; 7536 if (!ctl_cmd_applicable( 7537 lun->be_lun->lun_type, sentry)) 7538 continue; 7539 descr = &all->descr[num++]; 7540 descr->opcode = i; 7541 scsi_ulto2b(j, descr->service_action); 7542 descr->flags = RSO_SERVACTV; 7543 scsi_ulto2b(sentry->length, 7544 descr->cdb_length); 7545 } 7546 } else { 7547 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7548 entry)) 7549 continue; 7550 descr = &all->descr[num++]; 7551 descr->opcode = i; 7552 scsi_ulto2b(0, descr->service_action); 7553 descr->flags = 0; 7554 scsi_ulto2b(entry->length, descr->cdb_length); 7555 } 7556 } 7557 scsi_ulto4b( 7558 num * sizeof(struct scsi_report_supported_opcodes_descr), 7559 all->length); 7560 break; 7561 case RSO_OPTIONS_OC: 7562 one = (struct scsi_report_supported_opcodes_one *) 7563 ctsio->kern_data_ptr; 7564 entry = &ctl_cmd_table[opcode]; 7565 goto fill_one; 7566 case RSO_OPTIONS_OC_SA: 7567 one = (struct scsi_report_supported_opcodes_one *) 7568 ctsio->kern_data_ptr; 7569 entry = &ctl_cmd_table[opcode]; 7570 entry = &((const struct ctl_cmd_entry *) 7571 entry->execute)[service_action]; 7572fill_one: 7573 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7574 one->support = 3; 7575 scsi_ulto2b(entry->length, one->cdb_length); 7576 one->cdb_usage[0] = opcode; 7577 memcpy(&one->cdb_usage[1], entry->usage, 7578 entry->length - 1); 7579 } else 7580 one->support = 1; 7581 break; 7582 } 7583 7584 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7585 ctsio->be_move_done = ctl_config_move_done; 7586 7587 ctl_datamove((union ctl_io *)ctsio); 7588 return(retval); 7589} 7590 7591int 7592ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7593{ 7594 struct ctl_lun *lun; 7595 struct scsi_report_supported_tmf *cdb; 7596 struct scsi_report_supported_tmf_data *data; 7597 int retval; 7598 int alloc_len, total_len; 7599 7600 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7601 7602 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7603 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7604 7605 retval = CTL_RETVAL_COMPLETE; 7606 7607 total_len = sizeof(struct scsi_report_supported_tmf_data); 7608 alloc_len = scsi_4btoul(cdb->length); 7609 7610 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7611 7612 ctsio->kern_sg_entries = 0; 7613 7614 if (total_len < alloc_len) { 7615 ctsio->residual = alloc_len - total_len; 7616 ctsio->kern_data_len = total_len; 7617 ctsio->kern_total_len = total_len; 7618 } else { 7619 ctsio->residual = 0; 7620 ctsio->kern_data_len = alloc_len; 7621 ctsio->kern_total_len = alloc_len; 7622 } 7623 ctsio->kern_data_resid = 0; 7624 ctsio->kern_rel_offset = 0; 7625 7626 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7627 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7628 data->byte2 |= RST_ITNRS; 7629 7630 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7631 ctsio->be_move_done = ctl_config_move_done; 7632 7633 ctl_datamove((union ctl_io *)ctsio); 7634 return (retval); 7635} 7636 7637int 7638ctl_report_timestamp(struct ctl_scsiio *ctsio) 7639{ 7640 struct ctl_lun *lun; 7641 struct scsi_report_timestamp *cdb; 7642 struct scsi_report_timestamp_data *data; 7643 struct timeval tv; 7644 int64_t timestamp; 7645 int retval; 7646 int alloc_len, total_len; 7647 7648 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7649 7650 cdb = (struct scsi_report_timestamp *)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_timestamp_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_timestamp_data *)ctsio->kern_data_ptr; 7675 scsi_ulto2b(sizeof(*data) - 2, data->length); 7676 data->origin = RTS_ORIG_OUTSIDE; 7677 getmicrotime(&tv); 7678 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7679 scsi_ulto4b(timestamp >> 16, data->timestamp); 7680 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7681 7682 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7683 ctsio->be_move_done = ctl_config_move_done; 7684 7685 ctl_datamove((union ctl_io *)ctsio); 7686 return (retval); 7687} 7688 7689int 7690ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7691{ 7692 struct scsi_per_res_in *cdb; 7693 int alloc_len, total_len = 0; 7694 /* struct scsi_per_res_in_rsrv in_data; */ 7695 struct ctl_lun *lun; 7696 struct ctl_softc *softc; 7697 7698 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7699 7700 softc = control_softc; 7701 7702 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7703 7704 alloc_len = scsi_2btoul(cdb->length); 7705 7706 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7707 7708retry: 7709 mtx_lock(&lun->lun_lock); 7710 switch (cdb->action) { 7711 case SPRI_RK: /* read keys */ 7712 total_len = sizeof(struct scsi_per_res_in_keys) + 7713 lun->pr_key_count * 7714 sizeof(struct scsi_per_res_key); 7715 break; 7716 case SPRI_RR: /* read reservation */ 7717 if (lun->flags & CTL_LUN_PR_RESERVED) 7718 total_len = sizeof(struct scsi_per_res_in_rsrv); 7719 else 7720 total_len = sizeof(struct scsi_per_res_in_header); 7721 break; 7722 case SPRI_RC: /* report capabilities */ 7723 total_len = sizeof(struct scsi_per_res_cap); 7724 break; 7725 case SPRI_RS: /* read full status */ 7726 total_len = sizeof(struct scsi_per_res_in_header) + 7727 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7728 lun->pr_key_count; 7729 break; 7730 default: 7731 panic("Invalid PR type %x", cdb->action); 7732 } 7733 mtx_unlock(&lun->lun_lock); 7734 7735 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7736 7737 if (total_len < alloc_len) { 7738 ctsio->residual = alloc_len - total_len; 7739 ctsio->kern_data_len = total_len; 7740 ctsio->kern_total_len = total_len; 7741 } else { 7742 ctsio->residual = 0; 7743 ctsio->kern_data_len = alloc_len; 7744 ctsio->kern_total_len = alloc_len; 7745 } 7746 7747 ctsio->kern_data_resid = 0; 7748 ctsio->kern_rel_offset = 0; 7749 ctsio->kern_sg_entries = 0; 7750 7751 mtx_lock(&lun->lun_lock); 7752 switch (cdb->action) { 7753 case SPRI_RK: { // read keys 7754 struct scsi_per_res_in_keys *res_keys; 7755 int i, key_count; 7756 7757 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7758 7759 /* 7760 * We had to drop the lock to allocate our buffer, which 7761 * leaves time for someone to come in with another 7762 * persistent reservation. (That is unlikely, though, 7763 * since this should be the only persistent reservation 7764 * command active right now.) 7765 */ 7766 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7767 (lun->pr_key_count * 7768 sizeof(struct scsi_per_res_key)))){ 7769 mtx_unlock(&lun->lun_lock); 7770 free(ctsio->kern_data_ptr, M_CTL); 7771 printf("%s: reservation length changed, retrying\n", 7772 __func__); 7773 goto retry; 7774 } 7775 7776 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7777 7778 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7779 lun->pr_key_count, res_keys->header.length); 7780 7781 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7782 if (!lun->per_res[i].registered) 7783 continue; 7784 7785 /* 7786 * We used lun->pr_key_count to calculate the 7787 * size to allocate. If it turns out the number of 7788 * initiators with the registered flag set is 7789 * larger than that (i.e. they haven't been kept in 7790 * sync), we've got a problem. 7791 */ 7792 if (key_count >= lun->pr_key_count) { 7793#ifdef NEEDTOPORT 7794 csevent_log(CSC_CTL | CSC_SHELF_SW | 7795 CTL_PR_ERROR, 7796 csevent_LogType_Fault, 7797 csevent_AlertLevel_Yellow, 7798 csevent_FRU_ShelfController, 7799 csevent_FRU_Firmware, 7800 csevent_FRU_Unknown, 7801 "registered keys %d >= key " 7802 "count %d", key_count, 7803 lun->pr_key_count); 7804#endif 7805 key_count++; 7806 continue; 7807 } 7808 memcpy(res_keys->keys[key_count].key, 7809 lun->per_res[i].res_key.key, 7810 ctl_min(sizeof(res_keys->keys[key_count].key), 7811 sizeof(lun->per_res[i].res_key))); 7812 key_count++; 7813 } 7814 break; 7815 } 7816 case SPRI_RR: { // read reservation 7817 struct scsi_per_res_in_rsrv *res; 7818 int tmp_len, header_only; 7819 7820 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7821 7822 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7823 7824 if (lun->flags & CTL_LUN_PR_RESERVED) 7825 { 7826 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7827 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7828 res->header.length); 7829 header_only = 0; 7830 } else { 7831 tmp_len = sizeof(struct scsi_per_res_in_header); 7832 scsi_ulto4b(0, res->header.length); 7833 header_only = 1; 7834 } 7835 7836 /* 7837 * We had to drop the lock to allocate our buffer, which 7838 * leaves time for someone to come in with another 7839 * persistent reservation. (That is unlikely, though, 7840 * since this should be the only persistent reservation 7841 * command active right now.) 7842 */ 7843 if (tmp_len != total_len) { 7844 mtx_unlock(&lun->lun_lock); 7845 free(ctsio->kern_data_ptr, M_CTL); 7846 printf("%s: reservation status changed, retrying\n", 7847 __func__); 7848 goto retry; 7849 } 7850 7851 /* 7852 * No reservation held, so we're done. 7853 */ 7854 if (header_only != 0) 7855 break; 7856 7857 /* 7858 * If the registration is an All Registrants type, the key 7859 * is 0, since it doesn't really matter. 7860 */ 7861 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7862 memcpy(res->data.reservation, 7863 &lun->per_res[lun->pr_res_idx].res_key, 7864 sizeof(struct scsi_per_res_key)); 7865 } 7866 res->data.scopetype = lun->res_type; 7867 break; 7868 } 7869 case SPRI_RC: //report capabilities 7870 { 7871 struct scsi_per_res_cap *res_cap; 7872 uint16_t type_mask; 7873 7874 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7875 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7876 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7877 type_mask = SPRI_TM_WR_EX_AR | 7878 SPRI_TM_EX_AC_RO | 7879 SPRI_TM_WR_EX_RO | 7880 SPRI_TM_EX_AC | 7881 SPRI_TM_WR_EX | 7882 SPRI_TM_EX_AC_AR; 7883 scsi_ulto2b(type_mask, res_cap->type_mask); 7884 break; 7885 } 7886 case SPRI_RS: { // read full status 7887 struct scsi_per_res_in_full *res_status; 7888 struct scsi_per_res_in_full_desc *res_desc; 7889 struct ctl_port *port; 7890 int i, len; 7891 7892 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7893 7894 /* 7895 * We had to drop the lock to allocate our buffer, which 7896 * leaves time for someone to come in with another 7897 * persistent reservation. (That is unlikely, though, 7898 * since this should be the only persistent reservation 7899 * command active right now.) 7900 */ 7901 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7902 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7903 lun->pr_key_count)){ 7904 mtx_unlock(&lun->lun_lock); 7905 free(ctsio->kern_data_ptr, M_CTL); 7906 printf("%s: reservation length changed, retrying\n", 7907 __func__); 7908 goto retry; 7909 } 7910 7911 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7912 7913 res_desc = &res_status->desc[0]; 7914 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7915 if (!lun->per_res[i].registered) 7916 continue; 7917 7918 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7919 sizeof(res_desc->res_key)); 7920 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7921 (lun->pr_res_idx == i || 7922 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7923 res_desc->flags = SPRI_FULL_R_HOLDER; 7924 res_desc->scopetype = lun->res_type; 7925 } 7926 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7927 res_desc->rel_trgt_port_id); 7928 len = 0; 7929 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7930 if (port != NULL) 7931 len = ctl_create_iid(port, 7932 i % CTL_MAX_INIT_PER_PORT, 7933 res_desc->transport_id); 7934 scsi_ulto4b(len, res_desc->additional_length); 7935 res_desc = (struct scsi_per_res_in_full_desc *) 7936 &res_desc->transport_id[len]; 7937 } 7938 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7939 res_status->header.length); 7940 break; 7941 } 7942 default: 7943 /* 7944 * This is a bug, because we just checked for this above, 7945 * and should have returned an error. 7946 */ 7947 panic("Invalid PR type %x", cdb->action); 7948 break; /* NOTREACHED */ 7949 } 7950 mtx_unlock(&lun->lun_lock); 7951 7952 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7953 ctsio->be_move_done = ctl_config_move_done; 7954 7955 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7956 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7957 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7958 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7959 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7960 7961 ctl_datamove((union ctl_io *)ctsio); 7962 7963 return (CTL_RETVAL_COMPLETE); 7964} 7965 7966/* 7967 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7968 * it should return. 7969 */ 7970static int 7971ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7972 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7973 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7974 struct scsi_per_res_out_parms* param) 7975{ 7976 union ctl_ha_msg persis_io; 7977 int retval, i; 7978 int isc_retval; 7979 7980 retval = 0; 7981 7982 mtx_lock(&lun->lun_lock); 7983 if (sa_res_key == 0) { 7984 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7985 /* validate scope and type */ 7986 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7987 SPR_LU_SCOPE) { 7988 mtx_unlock(&lun->lun_lock); 7989 ctl_set_invalid_field(/*ctsio*/ ctsio, 7990 /*sks_valid*/ 1, 7991 /*command*/ 1, 7992 /*field*/ 2, 7993 /*bit_valid*/ 1, 7994 /*bit*/ 4); 7995 ctl_done((union ctl_io *)ctsio); 7996 return (1); 7997 } 7998 7999 if (type>8 || type==2 || type==4 || type==0) { 8000 mtx_unlock(&lun->lun_lock); 8001 ctl_set_invalid_field(/*ctsio*/ ctsio, 8002 /*sks_valid*/ 1, 8003 /*command*/ 1, 8004 /*field*/ 2, 8005 /*bit_valid*/ 1, 8006 /*bit*/ 0); 8007 ctl_done((union ctl_io *)ctsio); 8008 return (1); 8009 } 8010 8011 /* temporarily unregister this nexus */ 8012 lun->per_res[residx].registered = 0; 8013 8014 /* 8015 * Unregister everybody else and build UA for 8016 * them 8017 */ 8018 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8019 if (lun->per_res[i].registered == 0) 8020 continue; 8021 8022 if (!persis_offset 8023 && i <CTL_MAX_INITIATORS) 8024 lun->pending_ua[i] |= 8025 CTL_UA_REG_PREEMPT; 8026 else if (persis_offset 8027 && i >= persis_offset) 8028 lun->pending_ua[i-persis_offset] |= 8029 CTL_UA_REG_PREEMPT; 8030 lun->per_res[i].registered = 0; 8031 memset(&lun->per_res[i].res_key, 0, 8032 sizeof(struct scsi_per_res_key)); 8033 } 8034 lun->per_res[residx].registered = 1; 8035 lun->pr_key_count = 1; 8036 lun->res_type = type; 8037 if (lun->res_type != SPR_TYPE_WR_EX_AR 8038 && lun->res_type != SPR_TYPE_EX_AC_AR) 8039 lun->pr_res_idx = residx; 8040 8041 /* send msg to other side */ 8042 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8043 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8044 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8045 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8046 persis_io.pr.pr_info.res_type = type; 8047 memcpy(persis_io.pr.pr_info.sa_res_key, 8048 param->serv_act_res_key, 8049 sizeof(param->serv_act_res_key)); 8050 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8051 &persis_io, sizeof(persis_io), 0)) > 8052 CTL_HA_STATUS_SUCCESS) { 8053 printf("CTL:Persis Out error returned " 8054 "from ctl_ha_msg_send %d\n", 8055 isc_retval); 8056 } 8057 } else { 8058 /* not all registrants */ 8059 mtx_unlock(&lun->lun_lock); 8060 free(ctsio->kern_data_ptr, M_CTL); 8061 ctl_set_invalid_field(ctsio, 8062 /*sks_valid*/ 1, 8063 /*command*/ 0, 8064 /*field*/ 8, 8065 /*bit_valid*/ 0, 8066 /*bit*/ 0); 8067 ctl_done((union ctl_io *)ctsio); 8068 return (1); 8069 } 8070 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8071 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8072 int found = 0; 8073 8074 if (res_key == sa_res_key) { 8075 /* special case */ 8076 /* 8077 * The spec implies this is not good but doesn't 8078 * say what to do. There are two choices either 8079 * generate a res conflict or check condition 8080 * with illegal field in parameter data. Since 8081 * that is what is done when the sa_res_key is 8082 * zero I'll take that approach since this has 8083 * to do with the sa_res_key. 8084 */ 8085 mtx_unlock(&lun->lun_lock); 8086 free(ctsio->kern_data_ptr, M_CTL); 8087 ctl_set_invalid_field(ctsio, 8088 /*sks_valid*/ 1, 8089 /*command*/ 0, 8090 /*field*/ 8, 8091 /*bit_valid*/ 0, 8092 /*bit*/ 0); 8093 ctl_done((union ctl_io *)ctsio); 8094 return (1); 8095 } 8096 8097 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8098 if (lun->per_res[i].registered 8099 && memcmp(param->serv_act_res_key, 8100 lun->per_res[i].res_key.key, 8101 sizeof(struct scsi_per_res_key)) != 0) 8102 continue; 8103 8104 found = 1; 8105 lun->per_res[i].registered = 0; 8106 memset(&lun->per_res[i].res_key, 0, 8107 sizeof(struct scsi_per_res_key)); 8108 lun->pr_key_count--; 8109 8110 if (!persis_offset && i < CTL_MAX_INITIATORS) 8111 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8112 else if (persis_offset && i >= persis_offset) 8113 lun->pending_ua[i-persis_offset] |= 8114 CTL_UA_REG_PREEMPT; 8115 } 8116 if (!found) { 8117 mtx_unlock(&lun->lun_lock); 8118 free(ctsio->kern_data_ptr, M_CTL); 8119 ctl_set_reservation_conflict(ctsio); 8120 ctl_done((union ctl_io *)ctsio); 8121 return (CTL_RETVAL_COMPLETE); 8122 } 8123 /* send msg to other side */ 8124 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8125 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8126 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8127 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8128 persis_io.pr.pr_info.res_type = type; 8129 memcpy(persis_io.pr.pr_info.sa_res_key, 8130 param->serv_act_res_key, 8131 sizeof(param->serv_act_res_key)); 8132 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8133 &persis_io, sizeof(persis_io), 0)) > 8134 CTL_HA_STATUS_SUCCESS) { 8135 printf("CTL:Persis Out error returned from " 8136 "ctl_ha_msg_send %d\n", isc_retval); 8137 } 8138 } else { 8139 /* Reserved but not all registrants */ 8140 /* sa_res_key is res holder */ 8141 if (memcmp(param->serv_act_res_key, 8142 lun->per_res[lun->pr_res_idx].res_key.key, 8143 sizeof(struct scsi_per_res_key)) == 0) { 8144 /* validate scope and type */ 8145 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8146 SPR_LU_SCOPE) { 8147 mtx_unlock(&lun->lun_lock); 8148 ctl_set_invalid_field(/*ctsio*/ ctsio, 8149 /*sks_valid*/ 1, 8150 /*command*/ 1, 8151 /*field*/ 2, 8152 /*bit_valid*/ 1, 8153 /*bit*/ 4); 8154 ctl_done((union ctl_io *)ctsio); 8155 return (1); 8156 } 8157 8158 if (type>8 || type==2 || type==4 || type==0) { 8159 mtx_unlock(&lun->lun_lock); 8160 ctl_set_invalid_field(/*ctsio*/ ctsio, 8161 /*sks_valid*/ 1, 8162 /*command*/ 1, 8163 /*field*/ 2, 8164 /*bit_valid*/ 1, 8165 /*bit*/ 0); 8166 ctl_done((union ctl_io *)ctsio); 8167 return (1); 8168 } 8169 8170 /* 8171 * Do the following: 8172 * if sa_res_key != res_key remove all 8173 * registrants w/sa_res_key and generate UA 8174 * for these registrants(Registrations 8175 * Preempted) if it wasn't an exclusive 8176 * reservation generate UA(Reservations 8177 * Preempted) for all other registered nexuses 8178 * if the type has changed. Establish the new 8179 * reservation and holder. If res_key and 8180 * sa_res_key are the same do the above 8181 * except don't unregister the res holder. 8182 */ 8183 8184 /* 8185 * Temporarily unregister so it won't get 8186 * removed or UA generated 8187 */ 8188 lun->per_res[residx].registered = 0; 8189 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8190 if (lun->per_res[i].registered == 0) 8191 continue; 8192 8193 if (memcmp(param->serv_act_res_key, 8194 lun->per_res[i].res_key.key, 8195 sizeof(struct scsi_per_res_key)) == 0) { 8196 lun->per_res[i].registered = 0; 8197 memset(&lun->per_res[i].res_key, 8198 0, 8199 sizeof(struct scsi_per_res_key)); 8200 lun->pr_key_count--; 8201 8202 if (!persis_offset 8203 && i < CTL_MAX_INITIATORS) 8204 lun->pending_ua[i] |= 8205 CTL_UA_REG_PREEMPT; 8206 else if (persis_offset 8207 && i >= persis_offset) 8208 lun->pending_ua[i-persis_offset] |= 8209 CTL_UA_REG_PREEMPT; 8210 } else if (type != lun->res_type 8211 && (lun->res_type == SPR_TYPE_WR_EX_RO 8212 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8213 if (!persis_offset 8214 && i < CTL_MAX_INITIATORS) 8215 lun->pending_ua[i] |= 8216 CTL_UA_RES_RELEASE; 8217 else if (persis_offset 8218 && i >= persis_offset) 8219 lun->pending_ua[ 8220 i-persis_offset] |= 8221 CTL_UA_RES_RELEASE; 8222 } 8223 } 8224 lun->per_res[residx].registered = 1; 8225 lun->res_type = type; 8226 if (lun->res_type != SPR_TYPE_WR_EX_AR 8227 && lun->res_type != SPR_TYPE_EX_AC_AR) 8228 lun->pr_res_idx = residx; 8229 else 8230 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8231 8232 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8233 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8234 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8235 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8236 persis_io.pr.pr_info.res_type = type; 8237 memcpy(persis_io.pr.pr_info.sa_res_key, 8238 param->serv_act_res_key, 8239 sizeof(param->serv_act_res_key)); 8240 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8241 &persis_io, sizeof(persis_io), 0)) > 8242 CTL_HA_STATUS_SUCCESS) { 8243 printf("CTL:Persis Out error returned " 8244 "from ctl_ha_msg_send %d\n", 8245 isc_retval); 8246 } 8247 } else { 8248 /* 8249 * sa_res_key is not the res holder just 8250 * remove registrants 8251 */ 8252 int found=0; 8253 8254 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8255 if (memcmp(param->serv_act_res_key, 8256 lun->per_res[i].res_key.key, 8257 sizeof(struct scsi_per_res_key)) != 0) 8258 continue; 8259 8260 found = 1; 8261 lun->per_res[i].registered = 0; 8262 memset(&lun->per_res[i].res_key, 0, 8263 sizeof(struct scsi_per_res_key)); 8264 lun->pr_key_count--; 8265 8266 if (!persis_offset 8267 && i < CTL_MAX_INITIATORS) 8268 lun->pending_ua[i] |= 8269 CTL_UA_REG_PREEMPT; 8270 else if (persis_offset 8271 && i >= persis_offset) 8272 lun->pending_ua[i-persis_offset] |= 8273 CTL_UA_REG_PREEMPT; 8274 } 8275 8276 if (!found) { 8277 mtx_unlock(&lun->lun_lock); 8278 free(ctsio->kern_data_ptr, M_CTL); 8279 ctl_set_reservation_conflict(ctsio); 8280 ctl_done((union ctl_io *)ctsio); 8281 return (1); 8282 } 8283 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8284 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8285 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8286 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8287 persis_io.pr.pr_info.res_type = type; 8288 memcpy(persis_io.pr.pr_info.sa_res_key, 8289 param->serv_act_res_key, 8290 sizeof(param->serv_act_res_key)); 8291 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8292 &persis_io, sizeof(persis_io), 0)) > 8293 CTL_HA_STATUS_SUCCESS) { 8294 printf("CTL:Persis Out error returned " 8295 "from ctl_ha_msg_send %d\n", 8296 isc_retval); 8297 } 8298 } 8299 } 8300 8301 lun->PRGeneration++; 8302 mtx_unlock(&lun->lun_lock); 8303 8304 return (retval); 8305} 8306 8307static void 8308ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8309{ 8310 int i; 8311 8312 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8313 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8314 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8315 msg->pr.pr_info.sa_res_key, 8316 sizeof(struct scsi_per_res_key)) != 0) { 8317 uint64_t sa_res_key; 8318 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8319 8320 if (sa_res_key == 0) { 8321 /* temporarily unregister this nexus */ 8322 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8323 8324 /* 8325 * Unregister everybody else and build UA for 8326 * them 8327 */ 8328 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8329 if (lun->per_res[i].registered == 0) 8330 continue; 8331 8332 if (!persis_offset 8333 && i < CTL_MAX_INITIATORS) 8334 lun->pending_ua[i] |= 8335 CTL_UA_REG_PREEMPT; 8336 else if (persis_offset && i >= persis_offset) 8337 lun->pending_ua[i - persis_offset] |= 8338 CTL_UA_REG_PREEMPT; 8339 lun->per_res[i].registered = 0; 8340 memset(&lun->per_res[i].res_key, 0, 8341 sizeof(struct scsi_per_res_key)); 8342 } 8343 8344 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8345 lun->pr_key_count = 1; 8346 lun->res_type = msg->pr.pr_info.res_type; 8347 if (lun->res_type != SPR_TYPE_WR_EX_AR 8348 && lun->res_type != SPR_TYPE_EX_AC_AR) 8349 lun->pr_res_idx = msg->pr.pr_info.residx; 8350 } else { 8351 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8352 if (memcmp(msg->pr.pr_info.sa_res_key, 8353 lun->per_res[i].res_key.key, 8354 sizeof(struct scsi_per_res_key)) != 0) 8355 continue; 8356 8357 lun->per_res[i].registered = 0; 8358 memset(&lun->per_res[i].res_key, 0, 8359 sizeof(struct scsi_per_res_key)); 8360 lun->pr_key_count--; 8361 8362 if (!persis_offset 8363 && i < persis_offset) 8364 lun->pending_ua[i] |= 8365 CTL_UA_REG_PREEMPT; 8366 else if (persis_offset 8367 && i >= persis_offset) 8368 lun->pending_ua[i - persis_offset] |= 8369 CTL_UA_REG_PREEMPT; 8370 } 8371 } 8372 } else { 8373 /* 8374 * Temporarily unregister so it won't get removed 8375 * or UA generated 8376 */ 8377 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8378 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8379 if (lun->per_res[i].registered == 0) 8380 continue; 8381 8382 if (memcmp(msg->pr.pr_info.sa_res_key, 8383 lun->per_res[i].res_key.key, 8384 sizeof(struct scsi_per_res_key)) == 0) { 8385 lun->per_res[i].registered = 0; 8386 memset(&lun->per_res[i].res_key, 0, 8387 sizeof(struct scsi_per_res_key)); 8388 lun->pr_key_count--; 8389 if (!persis_offset 8390 && i < CTL_MAX_INITIATORS) 8391 lun->pending_ua[i] |= 8392 CTL_UA_REG_PREEMPT; 8393 else if (persis_offset 8394 && i >= persis_offset) 8395 lun->pending_ua[i - persis_offset] |= 8396 CTL_UA_REG_PREEMPT; 8397 } else if (msg->pr.pr_info.res_type != lun->res_type 8398 && (lun->res_type == SPR_TYPE_WR_EX_RO 8399 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8400 if (!persis_offset 8401 && i < persis_offset) 8402 lun->pending_ua[i] |= 8403 CTL_UA_RES_RELEASE; 8404 else if (persis_offset 8405 && i >= persis_offset) 8406 lun->pending_ua[i - persis_offset] |= 8407 CTL_UA_RES_RELEASE; 8408 } 8409 } 8410 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8411 lun->res_type = msg->pr.pr_info.res_type; 8412 if (lun->res_type != SPR_TYPE_WR_EX_AR 8413 && lun->res_type != SPR_TYPE_EX_AC_AR) 8414 lun->pr_res_idx = msg->pr.pr_info.residx; 8415 else 8416 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8417 } 8418 lun->PRGeneration++; 8419 8420} 8421 8422 8423int 8424ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8425{ 8426 int retval; 8427 int isc_retval; 8428 u_int32_t param_len; 8429 struct scsi_per_res_out *cdb; 8430 struct ctl_lun *lun; 8431 struct scsi_per_res_out_parms* param; 8432 struct ctl_softc *softc; 8433 uint32_t residx; 8434 uint64_t res_key, sa_res_key; 8435 uint8_t type; 8436 union ctl_ha_msg persis_io; 8437 int i; 8438 8439 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8440 8441 retval = CTL_RETVAL_COMPLETE; 8442 8443 softc = control_softc; 8444 8445 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8446 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8447 8448 /* 8449 * We only support whole-LUN scope. The scope & type are ignored for 8450 * register, register and ignore existing key and clear. 8451 * We sometimes ignore scope and type on preempts too!! 8452 * Verify reservation type here as well. 8453 */ 8454 type = cdb->scope_type & SPR_TYPE_MASK; 8455 if ((cdb->action == SPRO_RESERVE) 8456 || (cdb->action == SPRO_RELEASE)) { 8457 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8458 ctl_set_invalid_field(/*ctsio*/ ctsio, 8459 /*sks_valid*/ 1, 8460 /*command*/ 1, 8461 /*field*/ 2, 8462 /*bit_valid*/ 1, 8463 /*bit*/ 4); 8464 ctl_done((union ctl_io *)ctsio); 8465 return (CTL_RETVAL_COMPLETE); 8466 } 8467 8468 if (type>8 || type==2 || type==4 || type==0) { 8469 ctl_set_invalid_field(/*ctsio*/ ctsio, 8470 /*sks_valid*/ 1, 8471 /*command*/ 1, 8472 /*field*/ 2, 8473 /*bit_valid*/ 1, 8474 /*bit*/ 0); 8475 ctl_done((union ctl_io *)ctsio); 8476 return (CTL_RETVAL_COMPLETE); 8477 } 8478 } 8479 8480 param_len = scsi_4btoul(cdb->length); 8481 8482 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8483 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8484 ctsio->kern_data_len = param_len; 8485 ctsio->kern_total_len = param_len; 8486 ctsio->kern_data_resid = 0; 8487 ctsio->kern_rel_offset = 0; 8488 ctsio->kern_sg_entries = 0; 8489 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8490 ctsio->be_move_done = ctl_config_move_done; 8491 ctl_datamove((union ctl_io *)ctsio); 8492 8493 return (CTL_RETVAL_COMPLETE); 8494 } 8495 8496 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8497 8498 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8499 res_key = scsi_8btou64(param->res_key.key); 8500 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8501 8502 /* 8503 * Validate the reservation key here except for SPRO_REG_IGNO 8504 * This must be done for all other service actions 8505 */ 8506 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8507 mtx_lock(&lun->lun_lock); 8508 if (lun->per_res[residx].registered) { 8509 if (memcmp(param->res_key.key, 8510 lun->per_res[residx].res_key.key, 8511 ctl_min(sizeof(param->res_key), 8512 sizeof(lun->per_res[residx].res_key))) != 0) { 8513 /* 8514 * The current key passed in doesn't match 8515 * the one the initiator previously 8516 * registered. 8517 */ 8518 mtx_unlock(&lun->lun_lock); 8519 free(ctsio->kern_data_ptr, M_CTL); 8520 ctl_set_reservation_conflict(ctsio); 8521 ctl_done((union ctl_io *)ctsio); 8522 return (CTL_RETVAL_COMPLETE); 8523 } 8524 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8525 /* 8526 * We are not registered 8527 */ 8528 mtx_unlock(&lun->lun_lock); 8529 free(ctsio->kern_data_ptr, M_CTL); 8530 ctl_set_reservation_conflict(ctsio); 8531 ctl_done((union ctl_io *)ctsio); 8532 return (CTL_RETVAL_COMPLETE); 8533 } else if (res_key != 0) { 8534 /* 8535 * We are not registered and trying to register but 8536 * the register key isn't zero. 8537 */ 8538 mtx_unlock(&lun->lun_lock); 8539 free(ctsio->kern_data_ptr, M_CTL); 8540 ctl_set_reservation_conflict(ctsio); 8541 ctl_done((union ctl_io *)ctsio); 8542 return (CTL_RETVAL_COMPLETE); 8543 } 8544 mtx_unlock(&lun->lun_lock); 8545 } 8546 8547 switch (cdb->action & SPRO_ACTION_MASK) { 8548 case SPRO_REGISTER: 8549 case SPRO_REG_IGNO: { 8550 8551#if 0 8552 printf("Registration received\n"); 8553#endif 8554 8555 /* 8556 * We don't support any of these options, as we report in 8557 * the read capabilities request (see 8558 * ctl_persistent_reserve_in(), above). 8559 */ 8560 if ((param->flags & SPR_SPEC_I_PT) 8561 || (param->flags & SPR_ALL_TG_PT) 8562 || (param->flags & SPR_APTPL)) { 8563 int bit_ptr; 8564 8565 if (param->flags & SPR_APTPL) 8566 bit_ptr = 0; 8567 else if (param->flags & SPR_ALL_TG_PT) 8568 bit_ptr = 2; 8569 else /* SPR_SPEC_I_PT */ 8570 bit_ptr = 3; 8571 8572 free(ctsio->kern_data_ptr, M_CTL); 8573 ctl_set_invalid_field(ctsio, 8574 /*sks_valid*/ 1, 8575 /*command*/ 0, 8576 /*field*/ 20, 8577 /*bit_valid*/ 1, 8578 /*bit*/ bit_ptr); 8579 ctl_done((union ctl_io *)ctsio); 8580 return (CTL_RETVAL_COMPLETE); 8581 } 8582 8583 mtx_lock(&lun->lun_lock); 8584 8585 /* 8586 * The initiator wants to clear the 8587 * key/unregister. 8588 */ 8589 if (sa_res_key == 0) { 8590 if ((res_key == 0 8591 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8592 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8593 && !lun->per_res[residx].registered)) { 8594 mtx_unlock(&lun->lun_lock); 8595 goto done; 8596 } 8597 8598 lun->per_res[residx].registered = 0; 8599 memset(&lun->per_res[residx].res_key, 8600 0, sizeof(lun->per_res[residx].res_key)); 8601 lun->pr_key_count--; 8602 8603 if (residx == lun->pr_res_idx) { 8604 lun->flags &= ~CTL_LUN_PR_RESERVED; 8605 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8606 8607 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8608 || lun->res_type == SPR_TYPE_EX_AC_RO) 8609 && lun->pr_key_count) { 8610 /* 8611 * If the reservation is a registrants 8612 * only type we need to generate a UA 8613 * for other registered inits. The 8614 * sense code should be RESERVATIONS 8615 * RELEASED 8616 */ 8617 8618 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8619 if (lun->per_res[ 8620 i+persis_offset].registered 8621 == 0) 8622 continue; 8623 lun->pending_ua[i] |= 8624 CTL_UA_RES_RELEASE; 8625 } 8626 } 8627 lun->res_type = 0; 8628 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8629 if (lun->pr_key_count==0) { 8630 lun->flags &= ~CTL_LUN_PR_RESERVED; 8631 lun->res_type = 0; 8632 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8633 } 8634 } 8635 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8636 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8637 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8638 persis_io.pr.pr_info.residx = residx; 8639 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8640 &persis_io, sizeof(persis_io), 0 )) > 8641 CTL_HA_STATUS_SUCCESS) { 8642 printf("CTL:Persis Out error returned from " 8643 "ctl_ha_msg_send %d\n", isc_retval); 8644 } 8645 } else /* sa_res_key != 0 */ { 8646 8647 /* 8648 * If we aren't registered currently then increment 8649 * the key count and set the registered flag. 8650 */ 8651 if (!lun->per_res[residx].registered) { 8652 lun->pr_key_count++; 8653 lun->per_res[residx].registered = 1; 8654 } 8655 8656 memcpy(&lun->per_res[residx].res_key, 8657 param->serv_act_res_key, 8658 ctl_min(sizeof(param->serv_act_res_key), 8659 sizeof(lun->per_res[residx].res_key))); 8660 8661 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8662 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8663 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8664 persis_io.pr.pr_info.residx = residx; 8665 memcpy(persis_io.pr.pr_info.sa_res_key, 8666 param->serv_act_res_key, 8667 sizeof(param->serv_act_res_key)); 8668 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8669 &persis_io, sizeof(persis_io), 0)) > 8670 CTL_HA_STATUS_SUCCESS) { 8671 printf("CTL:Persis Out error returned from " 8672 "ctl_ha_msg_send %d\n", isc_retval); 8673 } 8674 } 8675 lun->PRGeneration++; 8676 mtx_unlock(&lun->lun_lock); 8677 8678 break; 8679 } 8680 case SPRO_RESERVE: 8681#if 0 8682 printf("Reserve executed type %d\n", type); 8683#endif 8684 mtx_lock(&lun->lun_lock); 8685 if (lun->flags & CTL_LUN_PR_RESERVED) { 8686 /* 8687 * if this isn't the reservation holder and it's 8688 * not a "all registrants" type or if the type is 8689 * different then we have a conflict 8690 */ 8691 if ((lun->pr_res_idx != residx 8692 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8693 || lun->res_type != type) { 8694 mtx_unlock(&lun->lun_lock); 8695 free(ctsio->kern_data_ptr, M_CTL); 8696 ctl_set_reservation_conflict(ctsio); 8697 ctl_done((union ctl_io *)ctsio); 8698 return (CTL_RETVAL_COMPLETE); 8699 } 8700 mtx_unlock(&lun->lun_lock); 8701 } else /* create a reservation */ { 8702 /* 8703 * If it's not an "all registrants" type record 8704 * reservation holder 8705 */ 8706 if (type != SPR_TYPE_WR_EX_AR 8707 && type != SPR_TYPE_EX_AC_AR) 8708 lun->pr_res_idx = residx; /* Res holder */ 8709 else 8710 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8711 8712 lun->flags |= CTL_LUN_PR_RESERVED; 8713 lun->res_type = type; 8714 8715 mtx_unlock(&lun->lun_lock); 8716 8717 /* send msg to other side */ 8718 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8719 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8720 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8721 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8722 persis_io.pr.pr_info.res_type = type; 8723 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8724 &persis_io, sizeof(persis_io), 0)) > 8725 CTL_HA_STATUS_SUCCESS) { 8726 printf("CTL:Persis Out error returned from " 8727 "ctl_ha_msg_send %d\n", isc_retval); 8728 } 8729 } 8730 break; 8731 8732 case SPRO_RELEASE: 8733 mtx_lock(&lun->lun_lock); 8734 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8735 /* No reservation exists return good status */ 8736 mtx_unlock(&lun->lun_lock); 8737 goto done; 8738 } 8739 /* 8740 * Is this nexus a reservation holder? 8741 */ 8742 if (lun->pr_res_idx != residx 8743 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8744 /* 8745 * not a res holder return good status but 8746 * do nothing 8747 */ 8748 mtx_unlock(&lun->lun_lock); 8749 goto done; 8750 } 8751 8752 if (lun->res_type != type) { 8753 mtx_unlock(&lun->lun_lock); 8754 free(ctsio->kern_data_ptr, M_CTL); 8755 ctl_set_illegal_pr_release(ctsio); 8756 ctl_done((union ctl_io *)ctsio); 8757 return (CTL_RETVAL_COMPLETE); 8758 } 8759 8760 /* okay to release */ 8761 lun->flags &= ~CTL_LUN_PR_RESERVED; 8762 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8763 lun->res_type = 0; 8764 8765 /* 8766 * if this isn't an exclusive access 8767 * res generate UA for all other 8768 * registrants. 8769 */ 8770 if (type != SPR_TYPE_EX_AC 8771 && type != SPR_TYPE_WR_EX) { 8772 /* 8773 * temporarily unregister so we don't generate UA 8774 */ 8775 lun->per_res[residx].registered = 0; 8776 8777 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8778 if (lun->per_res[i+persis_offset].registered 8779 == 0) 8780 continue; 8781 lun->pending_ua[i] |= 8782 CTL_UA_RES_RELEASE; 8783 } 8784 8785 lun->per_res[residx].registered = 1; 8786 } 8787 mtx_unlock(&lun->lun_lock); 8788 /* Send msg to other side */ 8789 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8790 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8791 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8792 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8793 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8794 printf("CTL:Persis Out error returned from " 8795 "ctl_ha_msg_send %d\n", isc_retval); 8796 } 8797 break; 8798 8799 case SPRO_CLEAR: 8800 /* send msg to other side */ 8801 8802 mtx_lock(&lun->lun_lock); 8803 lun->flags &= ~CTL_LUN_PR_RESERVED; 8804 lun->res_type = 0; 8805 lun->pr_key_count = 0; 8806 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8807 8808 8809 memset(&lun->per_res[residx].res_key, 8810 0, sizeof(lun->per_res[residx].res_key)); 8811 lun->per_res[residx].registered = 0; 8812 8813 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8814 if (lun->per_res[i].registered) { 8815 if (!persis_offset && i < CTL_MAX_INITIATORS) 8816 lun->pending_ua[i] |= 8817 CTL_UA_RES_PREEMPT; 8818 else if (persis_offset && i >= persis_offset) 8819 lun->pending_ua[i-persis_offset] |= 8820 CTL_UA_RES_PREEMPT; 8821 8822 memset(&lun->per_res[i].res_key, 8823 0, sizeof(struct scsi_per_res_key)); 8824 lun->per_res[i].registered = 0; 8825 } 8826 lun->PRGeneration++; 8827 mtx_unlock(&lun->lun_lock); 8828 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8829 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8830 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8831 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8832 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8833 printf("CTL:Persis Out error returned from " 8834 "ctl_ha_msg_send %d\n", isc_retval); 8835 } 8836 break; 8837 8838 case SPRO_PREEMPT: { 8839 int nretval; 8840 8841 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8842 residx, ctsio, cdb, param); 8843 if (nretval != 0) 8844 return (CTL_RETVAL_COMPLETE); 8845 break; 8846 } 8847 default: 8848 panic("Invalid PR type %x", cdb->action); 8849 } 8850 8851done: 8852 free(ctsio->kern_data_ptr, M_CTL); 8853 ctl_set_success(ctsio); 8854 ctl_done((union ctl_io *)ctsio); 8855 8856 return (retval); 8857} 8858 8859/* 8860 * This routine is for handling a message from the other SC pertaining to 8861 * persistent reserve out. All the error checking will have been done 8862 * so only perorming the action need be done here to keep the two 8863 * in sync. 8864 */ 8865static void 8866ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8867{ 8868 struct ctl_lun *lun; 8869 struct ctl_softc *softc; 8870 int i; 8871 uint32_t targ_lun; 8872 8873 softc = control_softc; 8874 8875 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8876 lun = softc->ctl_luns[targ_lun]; 8877 mtx_lock(&lun->lun_lock); 8878 switch(msg->pr.pr_info.action) { 8879 case CTL_PR_REG_KEY: 8880 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8881 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8882 lun->pr_key_count++; 8883 } 8884 lun->PRGeneration++; 8885 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8886 msg->pr.pr_info.sa_res_key, 8887 sizeof(struct scsi_per_res_key)); 8888 break; 8889 8890 case CTL_PR_UNREG_KEY: 8891 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8892 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8893 0, sizeof(struct scsi_per_res_key)); 8894 lun->pr_key_count--; 8895 8896 /* XXX Need to see if the reservation has been released */ 8897 /* if so do we need to generate UA? */ 8898 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8899 lun->flags &= ~CTL_LUN_PR_RESERVED; 8900 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8901 8902 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8903 || lun->res_type == SPR_TYPE_EX_AC_RO) 8904 && lun->pr_key_count) { 8905 /* 8906 * If the reservation is a registrants 8907 * only type we need to generate a UA 8908 * for other registered inits. The 8909 * sense code should be RESERVATIONS 8910 * RELEASED 8911 */ 8912 8913 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8914 if (lun->per_res[i+ 8915 persis_offset].registered == 0) 8916 continue; 8917 8918 lun->pending_ua[i] |= 8919 CTL_UA_RES_RELEASE; 8920 } 8921 } 8922 lun->res_type = 0; 8923 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8924 if (lun->pr_key_count==0) { 8925 lun->flags &= ~CTL_LUN_PR_RESERVED; 8926 lun->res_type = 0; 8927 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8928 } 8929 } 8930 lun->PRGeneration++; 8931 break; 8932 8933 case CTL_PR_RESERVE: 8934 lun->flags |= CTL_LUN_PR_RESERVED; 8935 lun->res_type = msg->pr.pr_info.res_type; 8936 lun->pr_res_idx = msg->pr.pr_info.residx; 8937 8938 break; 8939 8940 case CTL_PR_RELEASE: 8941 /* 8942 * if this isn't an exclusive access res generate UA for all 8943 * other registrants. 8944 */ 8945 if (lun->res_type != SPR_TYPE_EX_AC 8946 && lun->res_type != SPR_TYPE_WR_EX) { 8947 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8948 if (lun->per_res[i+persis_offset].registered) 8949 lun->pending_ua[i] |= 8950 CTL_UA_RES_RELEASE; 8951 } 8952 8953 lun->flags &= ~CTL_LUN_PR_RESERVED; 8954 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8955 lun->res_type = 0; 8956 break; 8957 8958 case CTL_PR_PREEMPT: 8959 ctl_pro_preempt_other(lun, msg); 8960 break; 8961 case CTL_PR_CLEAR: 8962 lun->flags &= ~CTL_LUN_PR_RESERVED; 8963 lun->res_type = 0; 8964 lun->pr_key_count = 0; 8965 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8966 8967 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8968 if (lun->per_res[i].registered == 0) 8969 continue; 8970 if (!persis_offset 8971 && i < CTL_MAX_INITIATORS) 8972 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8973 else if (persis_offset 8974 && i >= persis_offset) 8975 lun->pending_ua[i-persis_offset] |= 8976 CTL_UA_RES_PREEMPT; 8977 memset(&lun->per_res[i].res_key, 0, 8978 sizeof(struct scsi_per_res_key)); 8979 lun->per_res[i].registered = 0; 8980 } 8981 lun->PRGeneration++; 8982 break; 8983 } 8984 8985 mtx_unlock(&lun->lun_lock); 8986} 8987 8988int 8989ctl_read_write(struct ctl_scsiio *ctsio) 8990{ 8991 struct ctl_lun *lun; 8992 struct ctl_lba_len_flags *lbalen; 8993 uint64_t lba; 8994 uint32_t num_blocks; 8995 int fua, dpo; 8996 int retval; 8997 int isread; 8998 8999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9000 9001 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9002 9003 fua = 0; 9004 dpo = 0; 9005 9006 retval = CTL_RETVAL_COMPLETE; 9007 9008 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9009 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9010 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9011 uint32_t residx; 9012 9013 /* 9014 * XXX KDM need a lock here. 9015 */ 9016 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9017 if ((lun->res_type == SPR_TYPE_EX_AC 9018 && residx != lun->pr_res_idx) 9019 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9020 || lun->res_type == SPR_TYPE_EX_AC_AR) 9021 && !lun->per_res[residx].registered)) { 9022 ctl_set_reservation_conflict(ctsio); 9023 ctl_done((union ctl_io *)ctsio); 9024 return (CTL_RETVAL_COMPLETE); 9025 } 9026 } 9027 9028 switch (ctsio->cdb[0]) { 9029 case READ_6: 9030 case WRITE_6: { 9031 struct scsi_rw_6 *cdb; 9032 9033 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9034 9035 lba = scsi_3btoul(cdb->addr); 9036 /* only 5 bits are valid in the most significant address byte */ 9037 lba &= 0x1fffff; 9038 num_blocks = cdb->length; 9039 /* 9040 * This is correct according to SBC-2. 9041 */ 9042 if (num_blocks == 0) 9043 num_blocks = 256; 9044 break; 9045 } 9046 case READ_10: 9047 case WRITE_10: { 9048 struct scsi_rw_10 *cdb; 9049 9050 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9051 9052 if (cdb->byte2 & SRW10_FUA) 9053 fua = 1; 9054 if (cdb->byte2 & SRW10_DPO) 9055 dpo = 1; 9056 9057 lba = scsi_4btoul(cdb->addr); 9058 num_blocks = scsi_2btoul(cdb->length); 9059 break; 9060 } 9061 case WRITE_VERIFY_10: { 9062 struct scsi_write_verify_10 *cdb; 9063 9064 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9065 9066 /* 9067 * XXX KDM we should do actual write verify support at some 9068 * point. This is obviously fake, we're just translating 9069 * things to a write. So we don't even bother checking the 9070 * BYTCHK field, since we don't do any verification. If 9071 * the user asks for it, we'll just pretend we did it. 9072 */ 9073 if (cdb->byte2 & SWV_DPO) 9074 dpo = 1; 9075 9076 lba = scsi_4btoul(cdb->addr); 9077 num_blocks = scsi_2btoul(cdb->length); 9078 break; 9079 } 9080 case READ_12: 9081 case WRITE_12: { 9082 struct scsi_rw_12 *cdb; 9083 9084 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9085 9086 if (cdb->byte2 & SRW12_FUA) 9087 fua = 1; 9088 if (cdb->byte2 & SRW12_DPO) 9089 dpo = 1; 9090 lba = scsi_4btoul(cdb->addr); 9091 num_blocks = scsi_4btoul(cdb->length); 9092 break; 9093 } 9094 case WRITE_VERIFY_12: { 9095 struct scsi_write_verify_12 *cdb; 9096 9097 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9098 9099 if (cdb->byte2 & SWV_DPO) 9100 dpo = 1; 9101 9102 lba = scsi_4btoul(cdb->addr); 9103 num_blocks = scsi_4btoul(cdb->length); 9104 9105 break; 9106 } 9107 case READ_16: 9108 case WRITE_16: { 9109 struct scsi_rw_16 *cdb; 9110 9111 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9112 9113 if (cdb->byte2 & SRW12_FUA) 9114 fua = 1; 9115 if (cdb->byte2 & SRW12_DPO) 9116 dpo = 1; 9117 9118 lba = scsi_8btou64(cdb->addr); 9119 num_blocks = scsi_4btoul(cdb->length); 9120 break; 9121 } 9122 case WRITE_VERIFY_16: { 9123 struct scsi_write_verify_16 *cdb; 9124 9125 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9126 9127 if (cdb->byte2 & SWV_DPO) 9128 dpo = 1; 9129 9130 lba = scsi_8btou64(cdb->addr); 9131 num_blocks = scsi_4btoul(cdb->length); 9132 break; 9133 } 9134 default: 9135 /* 9136 * We got a command we don't support. This shouldn't 9137 * happen, commands should be filtered out above us. 9138 */ 9139 ctl_set_invalid_opcode(ctsio); 9140 ctl_done((union ctl_io *)ctsio); 9141 9142 return (CTL_RETVAL_COMPLETE); 9143 break; /* NOTREACHED */ 9144 } 9145 9146 /* 9147 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9148 * interesting for us, but if RAIDCore is in write-back mode, 9149 * getting it to do write-through for a particular transaction may 9150 * not be possible. 9151 */ 9152 9153 /* 9154 * The first check is to make sure we're in bounds, the second 9155 * check is to catch wrap-around problems. If the lba + num blocks 9156 * is less than the lba, then we've wrapped around and the block 9157 * range is invalid anyway. 9158 */ 9159 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9160 || ((lba + num_blocks) < lba)) { 9161 ctl_set_lba_out_of_range(ctsio); 9162 ctl_done((union ctl_io *)ctsio); 9163 return (CTL_RETVAL_COMPLETE); 9164 } 9165 9166 /* 9167 * According to SBC-3, a transfer length of 0 is not an error. 9168 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9169 * translates to 256 blocks for those commands. 9170 */ 9171 if (num_blocks == 0) { 9172 ctl_set_success(ctsio); 9173 ctl_done((union ctl_io *)ctsio); 9174 return (CTL_RETVAL_COMPLETE); 9175 } 9176 9177 lbalen = (struct ctl_lba_len_flags *) 9178 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9179 lbalen->lba = lba; 9180 lbalen->len = num_blocks; 9181 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 9182 9183 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9184 ctsio->kern_rel_offset = 0; 9185 9186 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9187 9188 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9189 9190 return (retval); 9191} 9192 9193static int 9194ctl_cnw_cont(union ctl_io *io) 9195{ 9196 struct ctl_scsiio *ctsio; 9197 struct ctl_lun *lun; 9198 struct ctl_lba_len_flags *lbalen; 9199 int retval; 9200 9201 ctsio = &io->scsiio; 9202 ctsio->io_hdr.status = CTL_STATUS_NONE; 9203 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9204 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9205 lbalen = (struct ctl_lba_len_flags *) 9206 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9207 lbalen->flags = CTL_LLF_WRITE; 9208 9209 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9210 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9211 return (retval); 9212} 9213 9214int 9215ctl_cnw(struct ctl_scsiio *ctsio) 9216{ 9217 struct ctl_lun *lun; 9218 struct ctl_lba_len_flags *lbalen; 9219 uint64_t lba; 9220 uint32_t num_blocks; 9221 int fua, dpo; 9222 int retval; 9223 9224 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9225 9226 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9227 9228 fua = 0; 9229 dpo = 0; 9230 9231 retval = CTL_RETVAL_COMPLETE; 9232 9233 switch (ctsio->cdb[0]) { 9234 case COMPARE_AND_WRITE: { 9235 struct scsi_compare_and_write *cdb; 9236 9237 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9238 9239 if (cdb->byte2 & SRW10_FUA) 9240 fua = 1; 9241 if (cdb->byte2 & SRW10_DPO) 9242 dpo = 1; 9243 lba = scsi_8btou64(cdb->addr); 9244 num_blocks = cdb->length; 9245 break; 9246 } 9247 default: 9248 /* 9249 * We got a command we don't support. This shouldn't 9250 * happen, commands should be filtered out above us. 9251 */ 9252 ctl_set_invalid_opcode(ctsio); 9253 ctl_done((union ctl_io *)ctsio); 9254 9255 return (CTL_RETVAL_COMPLETE); 9256 break; /* NOTREACHED */ 9257 } 9258 9259 /* 9260 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9261 * interesting for us, but if RAIDCore is in write-back mode, 9262 * getting it to do write-through for a particular transaction may 9263 * not be possible. 9264 */ 9265 9266 /* 9267 * The first check is to make sure we're in bounds, the second 9268 * check is to catch wrap-around problems. If the lba + num blocks 9269 * is less than the lba, then we've wrapped around and the block 9270 * range is invalid anyway. 9271 */ 9272 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9273 || ((lba + num_blocks) < lba)) { 9274 ctl_set_lba_out_of_range(ctsio); 9275 ctl_done((union ctl_io *)ctsio); 9276 return (CTL_RETVAL_COMPLETE); 9277 } 9278 9279 /* 9280 * According to SBC-3, a transfer length of 0 is not an error. 9281 */ 9282 if (num_blocks == 0) { 9283 ctl_set_success(ctsio); 9284 ctl_done((union ctl_io *)ctsio); 9285 return (CTL_RETVAL_COMPLETE); 9286 } 9287 9288 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9289 ctsio->kern_rel_offset = 0; 9290 9291 /* 9292 * Set the IO_CONT flag, so that if this I/O gets passed to 9293 * ctl_data_submit_done(), it'll get passed back to 9294 * ctl_ctl_cnw_cont() for further processing. 9295 */ 9296 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9297 ctsio->io_cont = ctl_cnw_cont; 9298 9299 lbalen = (struct ctl_lba_len_flags *) 9300 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9301 lbalen->lba = lba; 9302 lbalen->len = num_blocks; 9303 lbalen->flags = CTL_LLF_COMPARE; 9304 9305 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9306 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9307 return (retval); 9308} 9309 9310int 9311ctl_verify(struct ctl_scsiio *ctsio) 9312{ 9313 struct ctl_lun *lun; 9314 struct ctl_lba_len_flags *lbalen; 9315 uint64_t lba; 9316 uint32_t num_blocks; 9317 int bytchk, dpo; 9318 int retval; 9319 9320 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9321 9322 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9323 9324 bytchk = 0; 9325 dpo = 0; 9326 retval = CTL_RETVAL_COMPLETE; 9327 9328 switch (ctsio->cdb[0]) { 9329 case VERIFY_10: { 9330 struct scsi_verify_10 *cdb; 9331 9332 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9333 if (cdb->byte2 & SVFY_BYTCHK) 9334 bytchk = 1; 9335 if (cdb->byte2 & SVFY_DPO) 9336 dpo = 1; 9337 lba = scsi_4btoul(cdb->addr); 9338 num_blocks = scsi_2btoul(cdb->length); 9339 break; 9340 } 9341 case VERIFY_12: { 9342 struct scsi_verify_12 *cdb; 9343 9344 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9345 if (cdb->byte2 & SVFY_BYTCHK) 9346 bytchk = 1; 9347 if (cdb->byte2 & SVFY_DPO) 9348 dpo = 1; 9349 lba = scsi_4btoul(cdb->addr); 9350 num_blocks = scsi_4btoul(cdb->length); 9351 break; 9352 } 9353 case VERIFY_16: { 9354 struct scsi_rw_16 *cdb; 9355 9356 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9357 if (cdb->byte2 & SVFY_BYTCHK) 9358 bytchk = 1; 9359 if (cdb->byte2 & SVFY_DPO) 9360 dpo = 1; 9361 lba = scsi_8btou64(cdb->addr); 9362 num_blocks = scsi_4btoul(cdb->length); 9363 break; 9364 } 9365 default: 9366 /* 9367 * We got a command we don't support. This shouldn't 9368 * happen, commands should be filtered out above us. 9369 */ 9370 ctl_set_invalid_opcode(ctsio); 9371 ctl_done((union ctl_io *)ctsio); 9372 return (CTL_RETVAL_COMPLETE); 9373 } 9374 9375 /* 9376 * The first check is to make sure we're in bounds, the second 9377 * check is to catch wrap-around problems. If the lba + num blocks 9378 * is less than the lba, then we've wrapped around and the block 9379 * range is invalid anyway. 9380 */ 9381 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9382 || ((lba + num_blocks) < lba)) { 9383 ctl_set_lba_out_of_range(ctsio); 9384 ctl_done((union ctl_io *)ctsio); 9385 return (CTL_RETVAL_COMPLETE); 9386 } 9387 9388 /* 9389 * According to SBC-3, a transfer length of 0 is not an error. 9390 */ 9391 if (num_blocks == 0) { 9392 ctl_set_success(ctsio); 9393 ctl_done((union ctl_io *)ctsio); 9394 return (CTL_RETVAL_COMPLETE); 9395 } 9396 9397 lbalen = (struct ctl_lba_len_flags *) 9398 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9399 lbalen->lba = lba; 9400 lbalen->len = num_blocks; 9401 if (bytchk) { 9402 lbalen->flags = CTL_LLF_COMPARE; 9403 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9404 } else { 9405 lbalen->flags = CTL_LLF_VERIFY; 9406 ctsio->kern_total_len = 0; 9407 } 9408 ctsio->kern_rel_offset = 0; 9409 9410 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9411 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9412 return (retval); 9413} 9414 9415int 9416ctl_report_luns(struct ctl_scsiio *ctsio) 9417{ 9418 struct scsi_report_luns *cdb; 9419 struct scsi_report_luns_data *lun_data; 9420 struct ctl_lun *lun, *request_lun; 9421 int num_luns, retval; 9422 uint32_t alloc_len, lun_datalen; 9423 int num_filled, well_known; 9424 uint32_t initidx, targ_lun_id, lun_id; 9425 9426 retval = CTL_RETVAL_COMPLETE; 9427 well_known = 0; 9428 9429 cdb = (struct scsi_report_luns *)ctsio->cdb; 9430 9431 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9432 9433 mtx_lock(&control_softc->ctl_lock); 9434 num_luns = control_softc->num_luns; 9435 mtx_unlock(&control_softc->ctl_lock); 9436 9437 switch (cdb->select_report) { 9438 case RPL_REPORT_DEFAULT: 9439 case RPL_REPORT_ALL: 9440 break; 9441 case RPL_REPORT_WELLKNOWN: 9442 well_known = 1; 9443 num_luns = 0; 9444 break; 9445 default: 9446 ctl_set_invalid_field(ctsio, 9447 /*sks_valid*/ 1, 9448 /*command*/ 1, 9449 /*field*/ 2, 9450 /*bit_valid*/ 0, 9451 /*bit*/ 0); 9452 ctl_done((union ctl_io *)ctsio); 9453 return (retval); 9454 break; /* NOTREACHED */ 9455 } 9456 9457 alloc_len = scsi_4btoul(cdb->length); 9458 /* 9459 * The initiator has to allocate at least 16 bytes for this request, 9460 * so he can at least get the header and the first LUN. Otherwise 9461 * we reject the request (per SPC-3 rev 14, section 6.21). 9462 */ 9463 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9464 sizeof(struct scsi_report_luns_lundata))) { 9465 ctl_set_invalid_field(ctsio, 9466 /*sks_valid*/ 1, 9467 /*command*/ 1, 9468 /*field*/ 6, 9469 /*bit_valid*/ 0, 9470 /*bit*/ 0); 9471 ctl_done((union ctl_io *)ctsio); 9472 return (retval); 9473 } 9474 9475 request_lun = (struct ctl_lun *) 9476 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9477 9478 lun_datalen = sizeof(*lun_data) + 9479 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9480 9481 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9482 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9483 ctsio->kern_sg_entries = 0; 9484 9485 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9486 9487 mtx_lock(&control_softc->ctl_lock); 9488 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9489 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9490 if (lun_id >= CTL_MAX_LUNS) 9491 continue; 9492 lun = control_softc->ctl_luns[lun_id]; 9493 if (lun == NULL) 9494 continue; 9495 9496 if (targ_lun_id <= 0xff) { 9497 /* 9498 * Peripheral addressing method, bus number 0. 9499 */ 9500 lun_data->luns[num_filled].lundata[0] = 9501 RPL_LUNDATA_ATYP_PERIPH; 9502 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9503 num_filled++; 9504 } else if (targ_lun_id <= 0x3fff) { 9505 /* 9506 * Flat addressing method. 9507 */ 9508 lun_data->luns[num_filled].lundata[0] = 9509 RPL_LUNDATA_ATYP_FLAT | 9510 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9511#ifdef OLDCTLHEADERS 9512 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9513 (targ_lun_id & SRLD_BUS_LUN_MASK); 9514#endif 9515 lun_data->luns[num_filled].lundata[1] = 9516#ifdef OLDCTLHEADERS 9517 targ_lun_id >> SRLD_BUS_LUN_BITS; 9518#endif 9519 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9520 num_filled++; 9521 } else { 9522 printf("ctl_report_luns: bogus LUN number %jd, " 9523 "skipping\n", (intmax_t)targ_lun_id); 9524 } 9525 /* 9526 * According to SPC-3, rev 14 section 6.21: 9527 * 9528 * "The execution of a REPORT LUNS command to any valid and 9529 * installed logical unit shall clear the REPORTED LUNS DATA 9530 * HAS CHANGED unit attention condition for all logical 9531 * units of that target with respect to the requesting 9532 * initiator. A valid and installed logical unit is one 9533 * having a PERIPHERAL QUALIFIER of 000b in the standard 9534 * INQUIRY data (see 6.4.2)." 9535 * 9536 * If request_lun is NULL, the LUN this report luns command 9537 * was issued to is either disabled or doesn't exist. In that 9538 * case, we shouldn't clear any pending lun change unit 9539 * attention. 9540 */ 9541 if (request_lun != NULL) { 9542 mtx_lock(&lun->lun_lock); 9543 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9544 mtx_unlock(&lun->lun_lock); 9545 } 9546 } 9547 mtx_unlock(&control_softc->ctl_lock); 9548 9549 /* 9550 * It's quite possible that we've returned fewer LUNs than we allocated 9551 * space for. Trim it. 9552 */ 9553 lun_datalen = sizeof(*lun_data) + 9554 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9555 9556 if (lun_datalen < alloc_len) { 9557 ctsio->residual = alloc_len - lun_datalen; 9558 ctsio->kern_data_len = lun_datalen; 9559 ctsio->kern_total_len = lun_datalen; 9560 } else { 9561 ctsio->residual = 0; 9562 ctsio->kern_data_len = alloc_len; 9563 ctsio->kern_total_len = alloc_len; 9564 } 9565 ctsio->kern_data_resid = 0; 9566 ctsio->kern_rel_offset = 0; 9567 ctsio->kern_sg_entries = 0; 9568 9569 /* 9570 * We set this to the actual data length, regardless of how much 9571 * space we actually have to return results. If the user looks at 9572 * this value, he'll know whether or not he allocated enough space 9573 * and reissue the command if necessary. We don't support well 9574 * known logical units, so if the user asks for that, return none. 9575 */ 9576 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9577 9578 /* 9579 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9580 * this request. 9581 */ 9582 ctsio->scsi_status = SCSI_STATUS_OK; 9583 9584 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9585 ctsio->be_move_done = ctl_config_move_done; 9586 ctl_datamove((union ctl_io *)ctsio); 9587 9588 return (retval); 9589} 9590 9591int 9592ctl_request_sense(struct ctl_scsiio *ctsio) 9593{ 9594 struct scsi_request_sense *cdb; 9595 struct scsi_sense_data *sense_ptr; 9596 struct ctl_lun *lun; 9597 uint32_t initidx; 9598 int have_error; 9599 scsi_sense_data_type sense_format; 9600 9601 cdb = (struct scsi_request_sense *)ctsio->cdb; 9602 9603 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9604 9605 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9606 9607 /* 9608 * Determine which sense format the user wants. 9609 */ 9610 if (cdb->byte2 & SRS_DESC) 9611 sense_format = SSD_TYPE_DESC; 9612 else 9613 sense_format = SSD_TYPE_FIXED; 9614 9615 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9616 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9617 ctsio->kern_sg_entries = 0; 9618 9619 /* 9620 * struct scsi_sense_data, which is currently set to 256 bytes, is 9621 * larger than the largest allowed value for the length field in the 9622 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9623 */ 9624 ctsio->residual = 0; 9625 ctsio->kern_data_len = cdb->length; 9626 ctsio->kern_total_len = cdb->length; 9627 9628 ctsio->kern_data_resid = 0; 9629 ctsio->kern_rel_offset = 0; 9630 ctsio->kern_sg_entries = 0; 9631 9632 /* 9633 * If we don't have a LUN, we don't have any pending sense. 9634 */ 9635 if (lun == NULL) 9636 goto no_sense; 9637 9638 have_error = 0; 9639 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9640 /* 9641 * Check for pending sense, and then for pending unit attentions. 9642 * Pending sense gets returned first, then pending unit attentions. 9643 */ 9644 mtx_lock(&lun->lun_lock); 9645#ifdef CTL_WITH_CA 9646 if (ctl_is_set(lun->have_ca, initidx)) { 9647 scsi_sense_data_type stored_format; 9648 9649 /* 9650 * Check to see which sense format was used for the stored 9651 * sense data. 9652 */ 9653 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9654 9655 /* 9656 * If the user requested a different sense format than the 9657 * one we stored, then we need to convert it to the other 9658 * format. If we're going from descriptor to fixed format 9659 * sense data, we may lose things in translation, depending 9660 * on what options were used. 9661 * 9662 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9663 * for some reason we'll just copy it out as-is. 9664 */ 9665 if ((stored_format == SSD_TYPE_FIXED) 9666 && (sense_format == SSD_TYPE_DESC)) 9667 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9668 &lun->pending_sense[initidx], 9669 (struct scsi_sense_data_desc *)sense_ptr); 9670 else if ((stored_format == SSD_TYPE_DESC) 9671 && (sense_format == SSD_TYPE_FIXED)) 9672 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9673 &lun->pending_sense[initidx], 9674 (struct scsi_sense_data_fixed *)sense_ptr); 9675 else 9676 memcpy(sense_ptr, &lun->pending_sense[initidx], 9677 ctl_min(sizeof(*sense_ptr), 9678 sizeof(lun->pending_sense[initidx]))); 9679 9680 ctl_clear_mask(lun->have_ca, initidx); 9681 have_error = 1; 9682 } else 9683#endif 9684 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9685 ctl_ua_type ua_type; 9686 9687 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9688 sense_ptr, sense_format); 9689 if (ua_type != CTL_UA_NONE) 9690 have_error = 1; 9691 } 9692 mtx_unlock(&lun->lun_lock); 9693 9694 /* 9695 * We already have a pending error, return it. 9696 */ 9697 if (have_error != 0) { 9698 /* 9699 * We report the SCSI status as OK, since the status of the 9700 * request sense command itself is OK. 9701 */ 9702 ctsio->scsi_status = SCSI_STATUS_OK; 9703 9704 /* 9705 * We report 0 for the sense length, because we aren't doing 9706 * autosense in this case. We're reporting sense as 9707 * parameter data. 9708 */ 9709 ctsio->sense_len = 0; 9710 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9711 ctsio->be_move_done = ctl_config_move_done; 9712 ctl_datamove((union ctl_io *)ctsio); 9713 9714 return (CTL_RETVAL_COMPLETE); 9715 } 9716 9717no_sense: 9718 9719 /* 9720 * No sense information to report, so we report that everything is 9721 * okay. 9722 */ 9723 ctl_set_sense_data(sense_ptr, 9724 lun, 9725 sense_format, 9726 /*current_error*/ 1, 9727 /*sense_key*/ SSD_KEY_NO_SENSE, 9728 /*asc*/ 0x00, 9729 /*ascq*/ 0x00, 9730 SSD_ELEM_NONE); 9731 9732 ctsio->scsi_status = SCSI_STATUS_OK; 9733 9734 /* 9735 * We report 0 for the sense length, because we aren't doing 9736 * autosense in this case. We're reporting sense as parameter data. 9737 */ 9738 ctsio->sense_len = 0; 9739 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9740 ctsio->be_move_done = ctl_config_move_done; 9741 ctl_datamove((union ctl_io *)ctsio); 9742 9743 return (CTL_RETVAL_COMPLETE); 9744} 9745 9746int 9747ctl_tur(struct ctl_scsiio *ctsio) 9748{ 9749 struct ctl_lun *lun; 9750 9751 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9752 9753 CTL_DEBUG_PRINT(("ctl_tur\n")); 9754 9755 if (lun == NULL) 9756 return (EINVAL); 9757 9758 ctsio->scsi_status = SCSI_STATUS_OK; 9759 ctsio->io_hdr.status = CTL_SUCCESS; 9760 9761 ctl_done((union ctl_io *)ctsio); 9762 9763 return (CTL_RETVAL_COMPLETE); 9764} 9765 9766#ifdef notyet 9767static int 9768ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9769{ 9770 9771} 9772#endif 9773 9774static int 9775ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9776{ 9777 struct scsi_vpd_supported_pages *pages; 9778 int sup_page_size; 9779 struct ctl_lun *lun; 9780 9781 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9782 9783 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9784 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9785 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9786 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9787 ctsio->kern_sg_entries = 0; 9788 9789 if (sup_page_size < alloc_len) { 9790 ctsio->residual = alloc_len - sup_page_size; 9791 ctsio->kern_data_len = sup_page_size; 9792 ctsio->kern_total_len = sup_page_size; 9793 } else { 9794 ctsio->residual = 0; 9795 ctsio->kern_data_len = alloc_len; 9796 ctsio->kern_total_len = alloc_len; 9797 } 9798 ctsio->kern_data_resid = 0; 9799 ctsio->kern_rel_offset = 0; 9800 ctsio->kern_sg_entries = 0; 9801 9802 /* 9803 * The control device is always connected. The disk device, on the 9804 * other hand, may not be online all the time. Need to change this 9805 * to figure out whether the disk device is actually online or not. 9806 */ 9807 if (lun != NULL) 9808 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9809 lun->be_lun->lun_type; 9810 else 9811 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9812 9813 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9814 /* Supported VPD pages */ 9815 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9816 /* Serial Number */ 9817 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9818 /* Device Identification */ 9819 pages->page_list[2] = SVPD_DEVICE_ID; 9820 /* SCSI Ports */ 9821 pages->page_list[3] = SVPD_SCSI_PORTS; 9822 /* Third-party Copy */ 9823 pages->page_list[4] = SVPD_SCSI_TPC; 9824 /* Block limits */ 9825 pages->page_list[5] = SVPD_BLOCK_LIMITS; 9826 /* Block Device Characteristics */ 9827 pages->page_list[6] = SVPD_BDC; 9828 /* Logical Block Provisioning */ 9829 pages->page_list[7] = SVPD_LBP; 9830 9831 ctsio->scsi_status = SCSI_STATUS_OK; 9832 9833 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9834 ctsio->be_move_done = ctl_config_move_done; 9835 ctl_datamove((union ctl_io *)ctsio); 9836 9837 return (CTL_RETVAL_COMPLETE); 9838} 9839 9840static int 9841ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9842{ 9843 struct scsi_vpd_unit_serial_number *sn_ptr; 9844 struct ctl_lun *lun; 9845 9846 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9847 9848 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9849 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9850 ctsio->kern_sg_entries = 0; 9851 9852 if (sizeof(*sn_ptr) < alloc_len) { 9853 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9854 ctsio->kern_data_len = sizeof(*sn_ptr); 9855 ctsio->kern_total_len = sizeof(*sn_ptr); 9856 } else { 9857 ctsio->residual = 0; 9858 ctsio->kern_data_len = alloc_len; 9859 ctsio->kern_total_len = alloc_len; 9860 } 9861 ctsio->kern_data_resid = 0; 9862 ctsio->kern_rel_offset = 0; 9863 ctsio->kern_sg_entries = 0; 9864 9865 /* 9866 * The control device is always connected. The disk device, on the 9867 * other hand, may not be online all the time. Need to change this 9868 * to figure out whether the disk device is actually online or not. 9869 */ 9870 if (lun != NULL) 9871 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9872 lun->be_lun->lun_type; 9873 else 9874 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9875 9876 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9877 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9878 /* 9879 * If we don't have a LUN, we just leave the serial number as 9880 * all spaces. 9881 */ 9882 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9883 if (lun != NULL) { 9884 strncpy((char *)sn_ptr->serial_num, 9885 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9886 } 9887 ctsio->scsi_status = SCSI_STATUS_OK; 9888 9889 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9890 ctsio->be_move_done = ctl_config_move_done; 9891 ctl_datamove((union ctl_io *)ctsio); 9892 9893 return (CTL_RETVAL_COMPLETE); 9894} 9895 9896 9897static int 9898ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9899{ 9900 struct scsi_vpd_device_id *devid_ptr; 9901 struct scsi_vpd_id_descriptor *desc; 9902 struct ctl_softc *ctl_softc; 9903 struct ctl_lun *lun; 9904 struct ctl_port *port; 9905 int data_len; 9906 uint8_t proto; 9907 9908 ctl_softc = control_softc; 9909 9910 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9911 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9912 9913 data_len = sizeof(struct scsi_vpd_device_id) + 9914 sizeof(struct scsi_vpd_id_descriptor) + 9915 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9916 sizeof(struct scsi_vpd_id_descriptor) + 9917 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9918 if (lun && lun->lun_devid) 9919 data_len += lun->lun_devid->len; 9920 if (port->port_devid) 9921 data_len += port->port_devid->len; 9922 if (port->target_devid) 9923 data_len += port->target_devid->len; 9924 9925 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9926 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9927 ctsio->kern_sg_entries = 0; 9928 9929 if (data_len < alloc_len) { 9930 ctsio->residual = alloc_len - data_len; 9931 ctsio->kern_data_len = data_len; 9932 ctsio->kern_total_len = data_len; 9933 } else { 9934 ctsio->residual = 0; 9935 ctsio->kern_data_len = alloc_len; 9936 ctsio->kern_total_len = alloc_len; 9937 } 9938 ctsio->kern_data_resid = 0; 9939 ctsio->kern_rel_offset = 0; 9940 ctsio->kern_sg_entries = 0; 9941 9942 /* 9943 * The control device is always connected. The disk device, on the 9944 * other hand, may not be online all the time. 9945 */ 9946 if (lun != NULL) 9947 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9948 lun->be_lun->lun_type; 9949 else 9950 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9951 devid_ptr->page_code = SVPD_DEVICE_ID; 9952 scsi_ulto2b(data_len - 4, devid_ptr->length); 9953 9954 if (port->port_type == CTL_PORT_FC) 9955 proto = SCSI_PROTO_FC << 4; 9956 else if (port->port_type == CTL_PORT_ISCSI) 9957 proto = SCSI_PROTO_ISCSI << 4; 9958 else 9959 proto = SCSI_PROTO_SPI << 4; 9960 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9961 9962 /* 9963 * We're using a LUN association here. i.e., this device ID is a 9964 * per-LUN identifier. 9965 */ 9966 if (lun && lun->lun_devid) { 9967 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9968 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9969 lun->lun_devid->len); 9970 } 9971 9972 /* 9973 * This is for the WWPN which is a port association. 9974 */ 9975 if (port->port_devid) { 9976 memcpy(desc, port->port_devid->data, port->port_devid->len); 9977 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9978 port->port_devid->len); 9979 } 9980 9981 /* 9982 * This is for the Relative Target Port(type 4h) identifier 9983 */ 9984 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9985 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9986 SVPD_ID_TYPE_RELTARG; 9987 desc->length = 4; 9988 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9989 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9990 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9991 9992 /* 9993 * This is for the Target Port Group(type 5h) identifier 9994 */ 9995 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9996 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9997 SVPD_ID_TYPE_TPORTGRP; 9998 desc->length = 4; 9999 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10000 &desc->identifier[2]); 10001 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10002 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10003 10004 /* 10005 * This is for the Target identifier 10006 */ 10007 if (port->target_devid) { 10008 memcpy(desc, port->target_devid->data, port->target_devid->len); 10009 } 10010 10011 ctsio->scsi_status = SCSI_STATUS_OK; 10012 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10013 ctsio->be_move_done = ctl_config_move_done; 10014 ctl_datamove((union ctl_io *)ctsio); 10015 10016 return (CTL_RETVAL_COMPLETE); 10017} 10018 10019static int 10020ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10021{ 10022 struct ctl_softc *softc = control_softc; 10023 struct scsi_vpd_scsi_ports *sp; 10024 struct scsi_vpd_port_designation *pd; 10025 struct scsi_vpd_port_designation_cont *pdc; 10026 struct ctl_lun *lun; 10027 struct ctl_port *port; 10028 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10029 int num_target_port_groups, single; 10030 10031 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10032 10033 single = ctl_is_single; 10034 if (single) 10035 num_target_port_groups = 1; 10036 else 10037 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10038 num_target_ports = 0; 10039 iid_len = 0; 10040 id_len = 0; 10041 mtx_lock(&softc->ctl_lock); 10042 STAILQ_FOREACH(port, &softc->port_list, links) { 10043 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10044 continue; 10045 if (lun != NULL && 10046 ctl_map_lun_back(port->targ_port, lun->lun) >= 10047 CTL_MAX_LUNS) 10048 continue; 10049 num_target_ports++; 10050 if (port->init_devid) 10051 iid_len += port->init_devid->len; 10052 if (port->port_devid) 10053 id_len += port->port_devid->len; 10054 } 10055 mtx_unlock(&softc->ctl_lock); 10056 10057 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10058 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10059 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10060 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10061 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10062 ctsio->kern_sg_entries = 0; 10063 10064 if (data_len < alloc_len) { 10065 ctsio->residual = alloc_len - data_len; 10066 ctsio->kern_data_len = data_len; 10067 ctsio->kern_total_len = data_len; 10068 } else { 10069 ctsio->residual = 0; 10070 ctsio->kern_data_len = alloc_len; 10071 ctsio->kern_total_len = alloc_len; 10072 } 10073 ctsio->kern_data_resid = 0; 10074 ctsio->kern_rel_offset = 0; 10075 ctsio->kern_sg_entries = 0; 10076 10077 /* 10078 * The control device is always connected. The disk device, on the 10079 * other hand, may not be online all the time. Need to change this 10080 * to figure out whether the disk device is actually online or not. 10081 */ 10082 if (lun != NULL) 10083 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10084 lun->be_lun->lun_type; 10085 else 10086 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10087 10088 sp->page_code = SVPD_SCSI_PORTS; 10089 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10090 sp->page_length); 10091 pd = &sp->design[0]; 10092 10093 mtx_lock(&softc->ctl_lock); 10094 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10095 pg = 0; 10096 else 10097 pg = 1; 10098 for (g = 0; g < num_target_port_groups; g++) { 10099 STAILQ_FOREACH(port, &softc->port_list, links) { 10100 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10101 continue; 10102 if (lun != NULL && 10103 ctl_map_lun_back(port->targ_port, lun->lun) >= 10104 CTL_MAX_LUNS) 10105 continue; 10106 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10107 scsi_ulto2b(p, pd->relative_port_id); 10108 if (port->init_devid && g == pg) { 10109 iid_len = port->init_devid->len; 10110 memcpy(pd->initiator_transportid, 10111 port->init_devid->data, port->init_devid->len); 10112 } else 10113 iid_len = 0; 10114 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10115 pdc = (struct scsi_vpd_port_designation_cont *) 10116 (&pd->initiator_transportid[iid_len]); 10117 if (port->port_devid && g == pg) { 10118 id_len = port->port_devid->len; 10119 memcpy(pdc->target_port_descriptors, 10120 port->port_devid->data, port->port_devid->len); 10121 } else 10122 id_len = 0; 10123 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10124 pd = (struct scsi_vpd_port_designation *) 10125 ((uint8_t *)pdc->target_port_descriptors + id_len); 10126 } 10127 } 10128 mtx_unlock(&softc->ctl_lock); 10129 10130 ctsio->scsi_status = SCSI_STATUS_OK; 10131 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10132 ctsio->be_move_done = ctl_config_move_done; 10133 ctl_datamove((union ctl_io *)ctsio); 10134 10135 return (CTL_RETVAL_COMPLETE); 10136} 10137 10138static int 10139ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10140{ 10141 struct scsi_vpd_block_limits *bl_ptr; 10142 struct ctl_lun *lun; 10143 int bs; 10144 10145 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10146 10147 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10148 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10149 ctsio->kern_sg_entries = 0; 10150 10151 if (sizeof(*bl_ptr) < alloc_len) { 10152 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10153 ctsio->kern_data_len = sizeof(*bl_ptr); 10154 ctsio->kern_total_len = sizeof(*bl_ptr); 10155 } else { 10156 ctsio->residual = 0; 10157 ctsio->kern_data_len = alloc_len; 10158 ctsio->kern_total_len = alloc_len; 10159 } 10160 ctsio->kern_data_resid = 0; 10161 ctsio->kern_rel_offset = 0; 10162 ctsio->kern_sg_entries = 0; 10163 10164 /* 10165 * The control device is always connected. The disk device, on the 10166 * other hand, may not be online all the time. Need to change this 10167 * to figure out whether the disk device is actually online or not. 10168 */ 10169 if (lun != NULL) 10170 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10171 lun->be_lun->lun_type; 10172 else 10173 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10174 10175 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10176 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10177 bl_ptr->max_cmp_write_len = 0xff; 10178 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10179 if (lun != NULL) { 10180 bs = lun->be_lun->blocksize; 10181 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10182 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10183 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10184 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10185 if (lun->be_lun->pblockexp != 0) { 10186 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10187 bl_ptr->opt_unmap_grain); 10188 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10189 bl_ptr->unmap_grain_align); 10190 } 10191 } 10192 } 10193 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10194 10195 ctsio->scsi_status = SCSI_STATUS_OK; 10196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10197 ctsio->be_move_done = ctl_config_move_done; 10198 ctl_datamove((union ctl_io *)ctsio); 10199 10200 return (CTL_RETVAL_COMPLETE); 10201} 10202 10203static int 10204ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10205{ 10206 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10207 struct ctl_lun *lun; 10208 10209 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10210 10211 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10212 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10213 ctsio->kern_sg_entries = 0; 10214 10215 if (sizeof(*bdc_ptr) < alloc_len) { 10216 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10217 ctsio->kern_data_len = sizeof(*bdc_ptr); 10218 ctsio->kern_total_len = sizeof(*bdc_ptr); 10219 } else { 10220 ctsio->residual = 0; 10221 ctsio->kern_data_len = alloc_len; 10222 ctsio->kern_total_len = alloc_len; 10223 } 10224 ctsio->kern_data_resid = 0; 10225 ctsio->kern_rel_offset = 0; 10226 ctsio->kern_sg_entries = 0; 10227 10228 /* 10229 * The control device is always connected. The disk device, on the 10230 * other hand, may not be online all the time. Need to change this 10231 * to figure out whether the disk device is actually online or not. 10232 */ 10233 if (lun != NULL) 10234 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10235 lun->be_lun->lun_type; 10236 else 10237 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10238 bdc_ptr->page_code = SVPD_BDC; 10239 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10240 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10241 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10242 10243 ctsio->scsi_status = SCSI_STATUS_OK; 10244 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10245 ctsio->be_move_done = ctl_config_move_done; 10246 ctl_datamove((union ctl_io *)ctsio); 10247 10248 return (CTL_RETVAL_COMPLETE); 10249} 10250 10251static int 10252ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10253{ 10254 struct scsi_vpd_logical_block_prov *lbp_ptr; 10255 struct ctl_lun *lun; 10256 10257 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10258 10259 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10260 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10261 ctsio->kern_sg_entries = 0; 10262 10263 if (sizeof(*lbp_ptr) < alloc_len) { 10264 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10265 ctsio->kern_data_len = sizeof(*lbp_ptr); 10266 ctsio->kern_total_len = sizeof(*lbp_ptr); 10267 } else { 10268 ctsio->residual = 0; 10269 ctsio->kern_data_len = alloc_len; 10270 ctsio->kern_total_len = alloc_len; 10271 } 10272 ctsio->kern_data_resid = 0; 10273 ctsio->kern_rel_offset = 0; 10274 ctsio->kern_sg_entries = 0; 10275 10276 /* 10277 * The control device is always connected. The disk device, on the 10278 * other hand, may not be online all the time. Need to change this 10279 * to figure out whether the disk device is actually online or not. 10280 */ 10281 if (lun != NULL) 10282 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10283 lun->be_lun->lun_type; 10284 else 10285 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10286 10287 lbp_ptr->page_code = SVPD_LBP; 10288 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10289 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10290 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10291 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10292 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10293 } 10294 10295 ctsio->scsi_status = SCSI_STATUS_OK; 10296 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10297 ctsio->be_move_done = ctl_config_move_done; 10298 ctl_datamove((union ctl_io *)ctsio); 10299 10300 return (CTL_RETVAL_COMPLETE); 10301} 10302 10303static int 10304ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10305{ 10306 struct scsi_inquiry *cdb; 10307 struct ctl_lun *lun; 10308 int alloc_len, retval; 10309 10310 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10311 cdb = (struct scsi_inquiry *)ctsio->cdb; 10312 10313 retval = CTL_RETVAL_COMPLETE; 10314 10315 alloc_len = scsi_2btoul(cdb->length); 10316 10317 switch (cdb->page_code) { 10318 case SVPD_SUPPORTED_PAGES: 10319 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10320 break; 10321 case SVPD_UNIT_SERIAL_NUMBER: 10322 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10323 break; 10324 case SVPD_DEVICE_ID: 10325 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10326 break; 10327 case SVPD_SCSI_PORTS: 10328 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10329 break; 10330 case SVPD_SCSI_TPC: 10331 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10332 break; 10333 case SVPD_BLOCK_LIMITS: 10334 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10335 break; 10336 case SVPD_BDC: 10337 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10338 break; 10339 case SVPD_LBP: 10340 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10341 break; 10342 default: 10343 ctl_set_invalid_field(ctsio, 10344 /*sks_valid*/ 1, 10345 /*command*/ 1, 10346 /*field*/ 2, 10347 /*bit_valid*/ 0, 10348 /*bit*/ 0); 10349 ctl_done((union ctl_io *)ctsio); 10350 retval = CTL_RETVAL_COMPLETE; 10351 break; 10352 } 10353 10354 return (retval); 10355} 10356 10357static int 10358ctl_inquiry_std(struct ctl_scsiio *ctsio) 10359{ 10360 struct scsi_inquiry_data *inq_ptr; 10361 struct scsi_inquiry *cdb; 10362 struct ctl_softc *ctl_softc; 10363 struct ctl_lun *lun; 10364 char *val; 10365 uint32_t alloc_len; 10366 ctl_port_type port_type; 10367 10368 ctl_softc = control_softc; 10369 10370 /* 10371 * Figure out whether we're talking to a Fibre Channel port or not. 10372 * We treat the ioctl front end, and any SCSI adapters, as packetized 10373 * SCSI front ends. 10374 */ 10375 port_type = ctl_softc->ctl_ports[ 10376 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10377 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10378 port_type = CTL_PORT_SCSI; 10379 10380 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10381 cdb = (struct scsi_inquiry *)ctsio->cdb; 10382 alloc_len = scsi_2btoul(cdb->length); 10383 10384 /* 10385 * We malloc the full inquiry data size here and fill it 10386 * in. If the user only asks for less, we'll give him 10387 * that much. 10388 */ 10389 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10390 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10391 ctsio->kern_sg_entries = 0; 10392 ctsio->kern_data_resid = 0; 10393 ctsio->kern_rel_offset = 0; 10394 10395 if (sizeof(*inq_ptr) < alloc_len) { 10396 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10397 ctsio->kern_data_len = sizeof(*inq_ptr); 10398 ctsio->kern_total_len = sizeof(*inq_ptr); 10399 } else { 10400 ctsio->residual = 0; 10401 ctsio->kern_data_len = alloc_len; 10402 ctsio->kern_total_len = alloc_len; 10403 } 10404 10405 /* 10406 * If we have a LUN configured, report it as connected. Otherwise, 10407 * report that it is offline or no device is supported, depending 10408 * on the value of inquiry_pq_no_lun. 10409 * 10410 * According to the spec (SPC-4 r34), the peripheral qualifier 10411 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10412 * 10413 * "A peripheral device having the specified peripheral device type 10414 * is not connected to this logical unit. However, the device 10415 * server is capable of supporting the specified peripheral device 10416 * type on this logical unit." 10417 * 10418 * According to the same spec, the peripheral qualifier 10419 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10420 * 10421 * "The device server is not capable of supporting a peripheral 10422 * device on this logical unit. For this peripheral qualifier the 10423 * peripheral device type shall be set to 1Fh. All other peripheral 10424 * device type values are reserved for this peripheral qualifier." 10425 * 10426 * Given the text, it would seem that we probably want to report that 10427 * the LUN is offline here. There is no LUN connected, but we can 10428 * support a LUN at the given LUN number. 10429 * 10430 * In the real world, though, it sounds like things are a little 10431 * different: 10432 * 10433 * - Linux, when presented with a LUN with the offline peripheral 10434 * qualifier, will create an sg driver instance for it. So when 10435 * you attach it to CTL, you wind up with a ton of sg driver 10436 * instances. (One for every LUN that Linux bothered to probe.) 10437 * Linux does this despite the fact that it issues a REPORT LUNs 10438 * to LUN 0 to get the inventory of supported LUNs. 10439 * 10440 * - There is other anecdotal evidence (from Emulex folks) about 10441 * arrays that use the offline peripheral qualifier for LUNs that 10442 * are on the "passive" path in an active/passive array. 10443 * 10444 * So the solution is provide a hopefully reasonable default 10445 * (return bad/no LUN) and allow the user to change the behavior 10446 * with a tunable/sysctl variable. 10447 */ 10448 if (lun != NULL) 10449 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10450 lun->be_lun->lun_type; 10451 else if (ctl_softc->inquiry_pq_no_lun == 0) 10452 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10453 else 10454 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10455 10456 /* RMB in byte 2 is 0 */ 10457 inq_ptr->version = SCSI_REV_SPC4; 10458 10459 /* 10460 * According to SAM-3, even if a device only supports a single 10461 * level of LUN addressing, it should still set the HISUP bit: 10462 * 10463 * 4.9.1 Logical unit numbers overview 10464 * 10465 * All logical unit number formats described in this standard are 10466 * hierarchical in structure even when only a single level in that 10467 * hierarchy is used. The HISUP bit shall be set to one in the 10468 * standard INQUIRY data (see SPC-2) when any logical unit number 10469 * format described in this standard is used. Non-hierarchical 10470 * formats are outside the scope of this standard. 10471 * 10472 * Therefore we set the HiSup bit here. 10473 * 10474 * The reponse format is 2, per SPC-3. 10475 */ 10476 inq_ptr->response_format = SID_HiSup | 2; 10477 10478 inq_ptr->additional_length = 10479 offsetof(struct scsi_inquiry_data, vendor_specific1) - 10480 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10481 CTL_DEBUG_PRINT(("additional_length = %d\n", 10482 inq_ptr->additional_length)); 10483 10484 inq_ptr->spc3_flags = SPC3_SID_3PC; 10485 if (!ctl_is_single) 10486 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT; 10487 /* 16 bit addressing */ 10488 if (port_type == CTL_PORT_SCSI) 10489 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10490 /* XXX set the SID_MultiP bit here if we're actually going to 10491 respond on multiple ports */ 10492 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10493 10494 /* 16 bit data bus, synchronous transfers */ 10495 if (port_type == CTL_PORT_SCSI) 10496 inq_ptr->flags = SID_WBus16 | SID_Sync; 10497 /* 10498 * XXX KDM do we want to support tagged queueing on the control 10499 * device at all? 10500 */ 10501 if ((lun == NULL) 10502 || (lun->be_lun->lun_type != T_PROCESSOR)) 10503 inq_ptr->flags |= SID_CmdQue; 10504 /* 10505 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10506 * We have 8 bytes for the vendor name, and 16 bytes for the device 10507 * name and 4 bytes for the revision. 10508 */ 10509 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10510 "vendor")) == NULL) { 10511 strcpy(inq_ptr->vendor, CTL_VENDOR); 10512 } else { 10513 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10514 strncpy(inq_ptr->vendor, val, 10515 min(sizeof(inq_ptr->vendor), strlen(val))); 10516 } 10517 if (lun == NULL) { 10518 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10519 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10520 switch (lun->be_lun->lun_type) { 10521 case T_DIRECT: 10522 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10523 break; 10524 case T_PROCESSOR: 10525 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10526 break; 10527 default: 10528 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10529 break; 10530 } 10531 } else { 10532 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10533 strncpy(inq_ptr->product, val, 10534 min(sizeof(inq_ptr->product), strlen(val))); 10535 } 10536 10537 /* 10538 * XXX make this a macro somewhere so it automatically gets 10539 * incremented when we make changes. 10540 */ 10541 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10542 "revision")) == NULL) { 10543 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10544 } else { 10545 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10546 strncpy(inq_ptr->revision, val, 10547 min(sizeof(inq_ptr->revision), strlen(val))); 10548 } 10549 10550 /* 10551 * For parallel SCSI, we support double transition and single 10552 * transition clocking. We also support QAS (Quick Arbitration 10553 * and Selection) and Information Unit transfers on both the 10554 * control and array devices. 10555 */ 10556 if (port_type == CTL_PORT_SCSI) 10557 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10558 SID_SPI_IUS; 10559 10560 /* SAM-5 (no version claimed) */ 10561 scsi_ulto2b(0x00A0, inq_ptr->version1); 10562 /* SPC-4 (no version claimed) */ 10563 scsi_ulto2b(0x0460, inq_ptr->version2); 10564 if (port_type == CTL_PORT_FC) { 10565 /* FCP-2 ANSI INCITS.350:2003 */ 10566 scsi_ulto2b(0x0917, inq_ptr->version3); 10567 } else if (port_type == CTL_PORT_SCSI) { 10568 /* SPI-4 ANSI INCITS.362:200x */ 10569 scsi_ulto2b(0x0B56, inq_ptr->version3); 10570 } else if (port_type == CTL_PORT_ISCSI) { 10571 /* iSCSI (no version claimed) */ 10572 scsi_ulto2b(0x0960, inq_ptr->version3); 10573 } else if (port_type == CTL_PORT_SAS) { 10574 /* SAS (no version claimed) */ 10575 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10576 } 10577 10578 if (lun == NULL) { 10579 /* SBC-3 (no version claimed) */ 10580 scsi_ulto2b(0x04C0, inq_ptr->version4); 10581 } else { 10582 switch (lun->be_lun->lun_type) { 10583 case T_DIRECT: 10584 /* SBC-3 (no version claimed) */ 10585 scsi_ulto2b(0x04C0, inq_ptr->version4); 10586 break; 10587 case T_PROCESSOR: 10588 default: 10589 break; 10590 } 10591 } 10592 10593 ctsio->scsi_status = SCSI_STATUS_OK; 10594 if (ctsio->kern_data_len > 0) { 10595 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10596 ctsio->be_move_done = ctl_config_move_done; 10597 ctl_datamove((union ctl_io *)ctsio); 10598 } else { 10599 ctsio->io_hdr.status = CTL_SUCCESS; 10600 ctl_done((union ctl_io *)ctsio); 10601 } 10602 10603 return (CTL_RETVAL_COMPLETE); 10604} 10605 10606int 10607ctl_inquiry(struct ctl_scsiio *ctsio) 10608{ 10609 struct scsi_inquiry *cdb; 10610 int retval; 10611 10612 cdb = (struct scsi_inquiry *)ctsio->cdb; 10613 10614 retval = 0; 10615 10616 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10617 10618 /* 10619 * Right now, we don't support the CmdDt inquiry information. 10620 * This would be nice to support in the future. When we do 10621 * support it, we should change this test so that it checks to make 10622 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10623 */ 10624#ifdef notyet 10625 if (((cdb->byte2 & SI_EVPD) 10626 && (cdb->byte2 & SI_CMDDT))) 10627#endif 10628 if (cdb->byte2 & SI_CMDDT) { 10629 /* 10630 * Point to the SI_CMDDT bit. We might change this 10631 * when we support SI_CMDDT, but since both bits would be 10632 * "wrong", this should probably just stay as-is then. 10633 */ 10634 ctl_set_invalid_field(ctsio, 10635 /*sks_valid*/ 1, 10636 /*command*/ 1, 10637 /*field*/ 1, 10638 /*bit_valid*/ 1, 10639 /*bit*/ 1); 10640 ctl_done((union ctl_io *)ctsio); 10641 return (CTL_RETVAL_COMPLETE); 10642 } 10643 if (cdb->byte2 & SI_EVPD) 10644 retval = ctl_inquiry_evpd(ctsio); 10645#ifdef notyet 10646 else if (cdb->byte2 & SI_CMDDT) 10647 retval = ctl_inquiry_cmddt(ctsio); 10648#endif 10649 else 10650 retval = ctl_inquiry_std(ctsio); 10651 10652 return (retval); 10653} 10654 10655/* 10656 * For known CDB types, parse the LBA and length. 10657 */ 10658static int 10659ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10660{ 10661 if (io->io_hdr.io_type != CTL_IO_SCSI) 10662 return (1); 10663 10664 switch (io->scsiio.cdb[0]) { 10665 case COMPARE_AND_WRITE: { 10666 struct scsi_compare_and_write *cdb; 10667 10668 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10669 10670 *lba = scsi_8btou64(cdb->addr); 10671 *len = cdb->length; 10672 break; 10673 } 10674 case READ_6: 10675 case WRITE_6: { 10676 struct scsi_rw_6 *cdb; 10677 10678 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10679 10680 *lba = scsi_3btoul(cdb->addr); 10681 /* only 5 bits are valid in the most significant address byte */ 10682 *lba &= 0x1fffff; 10683 *len = cdb->length; 10684 break; 10685 } 10686 case READ_10: 10687 case WRITE_10: { 10688 struct scsi_rw_10 *cdb; 10689 10690 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10691 10692 *lba = scsi_4btoul(cdb->addr); 10693 *len = scsi_2btoul(cdb->length); 10694 break; 10695 } 10696 case WRITE_VERIFY_10: { 10697 struct scsi_write_verify_10 *cdb; 10698 10699 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10700 10701 *lba = scsi_4btoul(cdb->addr); 10702 *len = scsi_2btoul(cdb->length); 10703 break; 10704 } 10705 case READ_12: 10706 case WRITE_12: { 10707 struct scsi_rw_12 *cdb; 10708 10709 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10710 10711 *lba = scsi_4btoul(cdb->addr); 10712 *len = scsi_4btoul(cdb->length); 10713 break; 10714 } 10715 case WRITE_VERIFY_12: { 10716 struct scsi_write_verify_12 *cdb; 10717 10718 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10719 10720 *lba = scsi_4btoul(cdb->addr); 10721 *len = scsi_4btoul(cdb->length); 10722 break; 10723 } 10724 case READ_16: 10725 case WRITE_16: { 10726 struct scsi_rw_16 *cdb; 10727 10728 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10729 10730 *lba = scsi_8btou64(cdb->addr); 10731 *len = scsi_4btoul(cdb->length); 10732 break; 10733 } 10734 case WRITE_VERIFY_16: { 10735 struct scsi_write_verify_16 *cdb; 10736 10737 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10738 10739 10740 *lba = scsi_8btou64(cdb->addr); 10741 *len = scsi_4btoul(cdb->length); 10742 break; 10743 } 10744 case WRITE_SAME_10: { 10745 struct scsi_write_same_10 *cdb; 10746 10747 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10748 10749 *lba = scsi_4btoul(cdb->addr); 10750 *len = scsi_2btoul(cdb->length); 10751 break; 10752 } 10753 case WRITE_SAME_16: { 10754 struct scsi_write_same_16 *cdb; 10755 10756 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10757 10758 *lba = scsi_8btou64(cdb->addr); 10759 *len = scsi_4btoul(cdb->length); 10760 break; 10761 } 10762 case VERIFY_10: { 10763 struct scsi_verify_10 *cdb; 10764 10765 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10766 10767 *lba = scsi_4btoul(cdb->addr); 10768 *len = scsi_2btoul(cdb->length); 10769 break; 10770 } 10771 case VERIFY_12: { 10772 struct scsi_verify_12 *cdb; 10773 10774 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10775 10776 *lba = scsi_4btoul(cdb->addr); 10777 *len = scsi_4btoul(cdb->length); 10778 break; 10779 } 10780 case VERIFY_16: { 10781 struct scsi_verify_16 *cdb; 10782 10783 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10784 10785 *lba = scsi_8btou64(cdb->addr); 10786 *len = scsi_4btoul(cdb->length); 10787 break; 10788 } 10789 default: 10790 return (1); 10791 break; /* NOTREACHED */ 10792 } 10793 10794 return (0); 10795} 10796 10797static ctl_action 10798ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10799{ 10800 uint64_t endlba1, endlba2; 10801 10802 endlba1 = lba1 + len1 - 1; 10803 endlba2 = lba2 + len2 - 1; 10804 10805 if ((endlba1 < lba2) 10806 || (endlba2 < lba1)) 10807 return (CTL_ACTION_PASS); 10808 else 10809 return (CTL_ACTION_BLOCK); 10810} 10811 10812static ctl_action 10813ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10814{ 10815 uint64_t lba1, lba2; 10816 uint32_t len1, len2; 10817 int retval; 10818 10819 retval = ctl_get_lba_len(io1, &lba1, &len1); 10820 if (retval != 0) 10821 return (CTL_ACTION_ERROR); 10822 10823 retval = ctl_get_lba_len(io2, &lba2, &len2); 10824 if (retval != 0) 10825 return (CTL_ACTION_ERROR); 10826 10827 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10828} 10829 10830static ctl_action 10831ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10832{ 10833 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10834 ctl_serialize_action *serialize_row; 10835 10836 /* 10837 * The initiator attempted multiple untagged commands at the same 10838 * time. Can't do that. 10839 */ 10840 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10841 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10842 && ((pending_io->io_hdr.nexus.targ_port == 10843 ooa_io->io_hdr.nexus.targ_port) 10844 && (pending_io->io_hdr.nexus.initid.id == 10845 ooa_io->io_hdr.nexus.initid.id)) 10846 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10847 return (CTL_ACTION_OVERLAP); 10848 10849 /* 10850 * The initiator attempted to send multiple tagged commands with 10851 * the same ID. (It's fine if different initiators have the same 10852 * tag ID.) 10853 * 10854 * Even if all of those conditions are true, we don't kill the I/O 10855 * if the command ahead of us has been aborted. We won't end up 10856 * sending it to the FETD, and it's perfectly legal to resend a 10857 * command with the same tag number as long as the previous 10858 * instance of this tag number has been aborted somehow. 10859 */ 10860 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10861 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10862 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10863 && ((pending_io->io_hdr.nexus.targ_port == 10864 ooa_io->io_hdr.nexus.targ_port) 10865 && (pending_io->io_hdr.nexus.initid.id == 10866 ooa_io->io_hdr.nexus.initid.id)) 10867 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10868 return (CTL_ACTION_OVERLAP_TAG); 10869 10870 /* 10871 * If we get a head of queue tag, SAM-3 says that we should 10872 * immediately execute it. 10873 * 10874 * What happens if this command would normally block for some other 10875 * reason? e.g. a request sense with a head of queue tag 10876 * immediately after a write. Normally that would block, but this 10877 * will result in its getting executed immediately... 10878 * 10879 * We currently return "pass" instead of "skip", so we'll end up 10880 * going through the rest of the queue to check for overlapped tags. 10881 * 10882 * XXX KDM check for other types of blockage first?? 10883 */ 10884 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10885 return (CTL_ACTION_PASS); 10886 10887 /* 10888 * Ordered tags have to block until all items ahead of them 10889 * have completed. If we get called with an ordered tag, we always 10890 * block, if something else is ahead of us in the queue. 10891 */ 10892 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10893 return (CTL_ACTION_BLOCK); 10894 10895 /* 10896 * Simple tags get blocked until all head of queue and ordered tags 10897 * ahead of them have completed. I'm lumping untagged commands in 10898 * with simple tags here. XXX KDM is that the right thing to do? 10899 */ 10900 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10901 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10902 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10903 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10904 return (CTL_ACTION_BLOCK); 10905 10906 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10907 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10908 10909 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10910 10911 switch (serialize_row[pending_entry->seridx]) { 10912 case CTL_SER_BLOCK: 10913 return (CTL_ACTION_BLOCK); 10914 break; /* NOTREACHED */ 10915 case CTL_SER_EXTENT: 10916 return (ctl_extent_check(pending_io, ooa_io)); 10917 break; /* NOTREACHED */ 10918 case CTL_SER_PASS: 10919 return (CTL_ACTION_PASS); 10920 break; /* NOTREACHED */ 10921 case CTL_SER_SKIP: 10922 return (CTL_ACTION_SKIP); 10923 break; 10924 default: 10925 panic("invalid serialization value %d", 10926 serialize_row[pending_entry->seridx]); 10927 break; /* NOTREACHED */ 10928 } 10929 10930 return (CTL_ACTION_ERROR); 10931} 10932 10933/* 10934 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10935 * Assumptions: 10936 * - pending_io is generally either incoming, or on the blocked queue 10937 * - starting I/O is the I/O we want to start the check with. 10938 */ 10939static ctl_action 10940ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10941 union ctl_io *starting_io) 10942{ 10943 union ctl_io *ooa_io; 10944 ctl_action action; 10945 10946 mtx_assert(&lun->lun_lock, MA_OWNED); 10947 10948 /* 10949 * Run back along the OOA queue, starting with the current 10950 * blocked I/O and going through every I/O before it on the 10951 * queue. If starting_io is NULL, we'll just end up returning 10952 * CTL_ACTION_PASS. 10953 */ 10954 for (ooa_io = starting_io; ooa_io != NULL; 10955 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10956 ooa_links)){ 10957 10958 /* 10959 * This routine just checks to see whether 10960 * cur_blocked is blocked by ooa_io, which is ahead 10961 * of it in the queue. It doesn't queue/dequeue 10962 * cur_blocked. 10963 */ 10964 action = ctl_check_for_blockage(pending_io, ooa_io); 10965 switch (action) { 10966 case CTL_ACTION_BLOCK: 10967 case CTL_ACTION_OVERLAP: 10968 case CTL_ACTION_OVERLAP_TAG: 10969 case CTL_ACTION_SKIP: 10970 case CTL_ACTION_ERROR: 10971 return (action); 10972 break; /* NOTREACHED */ 10973 case CTL_ACTION_PASS: 10974 break; 10975 default: 10976 panic("invalid action %d", action); 10977 break; /* NOTREACHED */ 10978 } 10979 } 10980 10981 return (CTL_ACTION_PASS); 10982} 10983 10984/* 10985 * Assumptions: 10986 * - An I/O has just completed, and has been removed from the per-LUN OOA 10987 * queue, so some items on the blocked queue may now be unblocked. 10988 */ 10989static int 10990ctl_check_blocked(struct ctl_lun *lun) 10991{ 10992 union ctl_io *cur_blocked, *next_blocked; 10993 10994 mtx_assert(&lun->lun_lock, MA_OWNED); 10995 10996 /* 10997 * Run forward from the head of the blocked queue, checking each 10998 * entry against the I/Os prior to it on the OOA queue to see if 10999 * there is still any blockage. 11000 * 11001 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11002 * with our removing a variable on it while it is traversing the 11003 * list. 11004 */ 11005 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11006 cur_blocked != NULL; cur_blocked = next_blocked) { 11007 union ctl_io *prev_ooa; 11008 ctl_action action; 11009 11010 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11011 blocked_links); 11012 11013 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11014 ctl_ooaq, ooa_links); 11015 11016 /* 11017 * If cur_blocked happens to be the first item in the OOA 11018 * queue now, prev_ooa will be NULL, and the action 11019 * returned will just be CTL_ACTION_PASS. 11020 */ 11021 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11022 11023 switch (action) { 11024 case CTL_ACTION_BLOCK: 11025 /* Nothing to do here, still blocked */ 11026 break; 11027 case CTL_ACTION_OVERLAP: 11028 case CTL_ACTION_OVERLAP_TAG: 11029 /* 11030 * This shouldn't happen! In theory we've already 11031 * checked this command for overlap... 11032 */ 11033 break; 11034 case CTL_ACTION_PASS: 11035 case CTL_ACTION_SKIP: { 11036 struct ctl_softc *softc; 11037 const struct ctl_cmd_entry *entry; 11038 uint32_t initidx; 11039 int isc_retval; 11040 11041 /* 11042 * The skip case shouldn't happen, this transaction 11043 * should have never made it onto the blocked queue. 11044 */ 11045 /* 11046 * This I/O is no longer blocked, we can remove it 11047 * from the blocked queue. Since this is a TAILQ 11048 * (doubly linked list), we can do O(1) removals 11049 * from any place on the list. 11050 */ 11051 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11052 blocked_links); 11053 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11054 11055 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11056 /* 11057 * Need to send IO back to original side to 11058 * run 11059 */ 11060 union ctl_ha_msg msg_info; 11061 11062 msg_info.hdr.original_sc = 11063 cur_blocked->io_hdr.original_sc; 11064 msg_info.hdr.serializing_sc = cur_blocked; 11065 msg_info.hdr.msg_type = CTL_MSG_R2R; 11066 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11067 &msg_info, sizeof(msg_info), 0)) > 11068 CTL_HA_STATUS_SUCCESS) { 11069 printf("CTL:Check Blocked error from " 11070 "ctl_ha_msg_send %d\n", 11071 isc_retval); 11072 } 11073 break; 11074 } 11075 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 11076 softc = control_softc; 11077 11078 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11079 11080 /* 11081 * Check this I/O for LUN state changes that may 11082 * have happened while this command was blocked. 11083 * The LUN state may have been changed by a command 11084 * ahead of us in the queue, so we need to re-check 11085 * for any states that can be caused by SCSI 11086 * commands. 11087 */ 11088 if (ctl_scsiio_lun_check(softc, lun, entry, 11089 &cur_blocked->scsiio) == 0) { 11090 cur_blocked->io_hdr.flags |= 11091 CTL_FLAG_IS_WAS_ON_RTR; 11092 ctl_enqueue_rtr(cur_blocked); 11093 } else 11094 ctl_done(cur_blocked); 11095 break; 11096 } 11097 default: 11098 /* 11099 * This probably shouldn't happen -- we shouldn't 11100 * get CTL_ACTION_ERROR, or anything else. 11101 */ 11102 break; 11103 } 11104 } 11105 11106 return (CTL_RETVAL_COMPLETE); 11107} 11108 11109/* 11110 * This routine (with one exception) checks LUN flags that can be set by 11111 * commands ahead of us in the OOA queue. These flags have to be checked 11112 * when a command initially comes in, and when we pull a command off the 11113 * blocked queue and are preparing to execute it. The reason we have to 11114 * check these flags for commands on the blocked queue is that the LUN 11115 * state may have been changed by a command ahead of us while we're on the 11116 * blocked queue. 11117 * 11118 * Ordering is somewhat important with these checks, so please pay 11119 * careful attention to the placement of any new checks. 11120 */ 11121static int 11122ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11123 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11124{ 11125 int retval; 11126 11127 retval = 0; 11128 11129 mtx_assert(&lun->lun_lock, MA_OWNED); 11130 11131 /* 11132 * If this shelf is a secondary shelf controller, we have to reject 11133 * any media access commands. 11134 */ 11135#if 0 11136 /* No longer needed for HA */ 11137 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11138 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11139 ctl_set_lun_standby(ctsio); 11140 retval = 1; 11141 goto bailout; 11142 } 11143#endif 11144 11145 /* 11146 * Check for a reservation conflict. If this command isn't allowed 11147 * even on reserved LUNs, and if this initiator isn't the one who 11148 * reserved us, reject the command with a reservation conflict. 11149 */ 11150 if ((lun->flags & CTL_LUN_RESERVED) 11151 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11152 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11153 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11154 || (ctsio->io_hdr.nexus.targ_target.id != 11155 lun->rsv_nexus.targ_target.id)) { 11156 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11157 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11158 retval = 1; 11159 goto bailout; 11160 } 11161 } 11162 11163 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11164 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11165 uint32_t residx; 11166 11167 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11168 /* 11169 * if we aren't registered or it's a res holder type 11170 * reservation and this isn't the res holder then set a 11171 * conflict. 11172 * NOTE: Commands which might be allowed on write exclusive 11173 * type reservations are checked in the particular command 11174 * for a conflict. Read and SSU are the only ones. 11175 */ 11176 if (!lun->per_res[residx].registered 11177 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11178 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11179 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11180 retval = 1; 11181 goto bailout; 11182 } 11183 11184 } 11185 11186 if ((lun->flags & CTL_LUN_OFFLINE) 11187 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11188 ctl_set_lun_not_ready(ctsio); 11189 retval = 1; 11190 goto bailout; 11191 } 11192 11193 /* 11194 * If the LUN is stopped, see if this particular command is allowed 11195 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11196 */ 11197 if ((lun->flags & CTL_LUN_STOPPED) 11198 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11199 /* "Logical unit not ready, initializing cmd. required" */ 11200 ctl_set_lun_stopped(ctsio); 11201 retval = 1; 11202 goto bailout; 11203 } 11204 11205 if ((lun->flags & CTL_LUN_INOPERABLE) 11206 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11207 /* "Medium format corrupted" */ 11208 ctl_set_medium_format_corrupted(ctsio); 11209 retval = 1; 11210 goto bailout; 11211 } 11212 11213bailout: 11214 return (retval); 11215 11216} 11217 11218static void 11219ctl_failover_io(union ctl_io *io, int have_lock) 11220{ 11221 ctl_set_busy(&io->scsiio); 11222 ctl_done(io); 11223} 11224 11225static void 11226ctl_failover(void) 11227{ 11228 struct ctl_lun *lun; 11229 struct ctl_softc *ctl_softc; 11230 union ctl_io *next_io, *pending_io; 11231 union ctl_io *io; 11232 int lun_idx; 11233 int i; 11234 11235 ctl_softc = control_softc; 11236 11237 mtx_lock(&ctl_softc->ctl_lock); 11238 /* 11239 * Remove any cmds from the other SC from the rtr queue. These 11240 * will obviously only be for LUNs for which we're the primary. 11241 * We can't send status or get/send data for these commands. 11242 * Since they haven't been executed yet, we can just remove them. 11243 * We'll either abort them or delete them below, depending on 11244 * which HA mode we're in. 11245 */ 11246#ifdef notyet 11247 mtx_lock(&ctl_softc->queue_lock); 11248 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11249 io != NULL; io = next_io) { 11250 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11251 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11252 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11253 ctl_io_hdr, links); 11254 } 11255 mtx_unlock(&ctl_softc->queue_lock); 11256#endif 11257 11258 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11259 lun = ctl_softc->ctl_luns[lun_idx]; 11260 if (lun==NULL) 11261 continue; 11262 11263 /* 11264 * Processor LUNs are primary on both sides. 11265 * XXX will this always be true? 11266 */ 11267 if (lun->be_lun->lun_type == T_PROCESSOR) 11268 continue; 11269 11270 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11271 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11272 printf("FAILOVER: primary lun %d\n", lun_idx); 11273 /* 11274 * Remove all commands from the other SC. First from the 11275 * blocked queue then from the ooa queue. Once we have 11276 * removed them. Call ctl_check_blocked to see if there 11277 * is anything that can run. 11278 */ 11279 for (io = (union ctl_io *)TAILQ_FIRST( 11280 &lun->blocked_queue); io != NULL; io = next_io) { 11281 11282 next_io = (union ctl_io *)TAILQ_NEXT( 11283 &io->io_hdr, blocked_links); 11284 11285 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11286 TAILQ_REMOVE(&lun->blocked_queue, 11287 &io->io_hdr,blocked_links); 11288 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11289 TAILQ_REMOVE(&lun->ooa_queue, 11290 &io->io_hdr, ooa_links); 11291 11292 ctl_free_io(io); 11293 } 11294 } 11295 11296 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11297 io != NULL; io = next_io) { 11298 11299 next_io = (union ctl_io *)TAILQ_NEXT( 11300 &io->io_hdr, ooa_links); 11301 11302 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11303 11304 TAILQ_REMOVE(&lun->ooa_queue, 11305 &io->io_hdr, 11306 ooa_links); 11307 11308 ctl_free_io(io); 11309 } 11310 } 11311 ctl_check_blocked(lun); 11312 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11313 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11314 11315 printf("FAILOVER: primary lun %d\n", lun_idx); 11316 /* 11317 * Abort all commands from the other SC. We can't 11318 * send status back for them now. These should get 11319 * cleaned up when they are completed or come out 11320 * for a datamove operation. 11321 */ 11322 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11323 io != NULL; io = next_io) { 11324 next_io = (union ctl_io *)TAILQ_NEXT( 11325 &io->io_hdr, ooa_links); 11326 11327 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11328 io->io_hdr.flags |= CTL_FLAG_ABORT; 11329 } 11330 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11331 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11332 11333 printf("FAILOVER: secondary lun %d\n", lun_idx); 11334 11335 lun->flags |= CTL_LUN_PRIMARY_SC; 11336 11337 /* 11338 * We send all I/O that was sent to this controller 11339 * and redirected to the other side back with 11340 * busy status, and have the initiator retry it. 11341 * Figuring out how much data has been transferred, 11342 * etc. and picking up where we left off would be 11343 * very tricky. 11344 * 11345 * XXX KDM need to remove I/O from the blocked 11346 * queue as well! 11347 */ 11348 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11349 &lun->ooa_queue); pending_io != NULL; 11350 pending_io = next_io) { 11351 11352 next_io = (union ctl_io *)TAILQ_NEXT( 11353 &pending_io->io_hdr, ooa_links); 11354 11355 pending_io->io_hdr.flags &= 11356 ~CTL_FLAG_SENT_2OTHER_SC; 11357 11358 if (pending_io->io_hdr.flags & 11359 CTL_FLAG_IO_ACTIVE) { 11360 pending_io->io_hdr.flags |= 11361 CTL_FLAG_FAILOVER; 11362 } else { 11363 ctl_set_busy(&pending_io->scsiio); 11364 ctl_done(pending_io); 11365 } 11366 } 11367 11368 /* 11369 * Build Unit Attention 11370 */ 11371 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11372 lun->pending_ua[i] |= 11373 CTL_UA_ASYM_ACC_CHANGE; 11374 } 11375 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11376 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11377 printf("FAILOVER: secondary lun %d\n", lun_idx); 11378 /* 11379 * if the first io on the OOA is not on the RtR queue 11380 * add it. 11381 */ 11382 lun->flags |= CTL_LUN_PRIMARY_SC; 11383 11384 pending_io = (union ctl_io *)TAILQ_FIRST( 11385 &lun->ooa_queue); 11386 if (pending_io==NULL) { 11387 printf("Nothing on OOA queue\n"); 11388 continue; 11389 } 11390 11391 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11392 if ((pending_io->io_hdr.flags & 11393 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11394 pending_io->io_hdr.flags |= 11395 CTL_FLAG_IS_WAS_ON_RTR; 11396 ctl_enqueue_rtr(pending_io); 11397 } 11398#if 0 11399 else 11400 { 11401 printf("Tag 0x%04x is running\n", 11402 pending_io->scsiio.tag_num); 11403 } 11404#endif 11405 11406 next_io = (union ctl_io *)TAILQ_NEXT( 11407 &pending_io->io_hdr, ooa_links); 11408 for (pending_io=next_io; pending_io != NULL; 11409 pending_io = next_io) { 11410 pending_io->io_hdr.flags &= 11411 ~CTL_FLAG_SENT_2OTHER_SC; 11412 next_io = (union ctl_io *)TAILQ_NEXT( 11413 &pending_io->io_hdr, ooa_links); 11414 if (pending_io->io_hdr.flags & 11415 CTL_FLAG_IS_WAS_ON_RTR) { 11416#if 0 11417 printf("Tag 0x%04x is running\n", 11418 pending_io->scsiio.tag_num); 11419#endif 11420 continue; 11421 } 11422 11423 switch (ctl_check_ooa(lun, pending_io, 11424 (union ctl_io *)TAILQ_PREV( 11425 &pending_io->io_hdr, ctl_ooaq, 11426 ooa_links))) { 11427 11428 case CTL_ACTION_BLOCK: 11429 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11430 &pending_io->io_hdr, 11431 blocked_links); 11432 pending_io->io_hdr.flags |= 11433 CTL_FLAG_BLOCKED; 11434 break; 11435 case CTL_ACTION_PASS: 11436 case CTL_ACTION_SKIP: 11437 pending_io->io_hdr.flags |= 11438 CTL_FLAG_IS_WAS_ON_RTR; 11439 ctl_enqueue_rtr(pending_io); 11440 break; 11441 case CTL_ACTION_OVERLAP: 11442 ctl_set_overlapped_cmd( 11443 (struct ctl_scsiio *)pending_io); 11444 ctl_done(pending_io); 11445 break; 11446 case CTL_ACTION_OVERLAP_TAG: 11447 ctl_set_overlapped_tag( 11448 (struct ctl_scsiio *)pending_io, 11449 pending_io->scsiio.tag_num & 0xff); 11450 ctl_done(pending_io); 11451 break; 11452 case CTL_ACTION_ERROR: 11453 default: 11454 ctl_set_internal_failure( 11455 (struct ctl_scsiio *)pending_io, 11456 0, // sks_valid 11457 0); //retry count 11458 ctl_done(pending_io); 11459 break; 11460 } 11461 } 11462 11463 /* 11464 * Build Unit Attention 11465 */ 11466 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11467 lun->pending_ua[i] |= 11468 CTL_UA_ASYM_ACC_CHANGE; 11469 } 11470 } else { 11471 panic("Unhandled HA mode failover, LUN flags = %#x, " 11472 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11473 } 11474 } 11475 ctl_pause_rtr = 0; 11476 mtx_unlock(&ctl_softc->ctl_lock); 11477} 11478 11479static int 11480ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11481{ 11482 struct ctl_lun *lun; 11483 const struct ctl_cmd_entry *entry; 11484 uint32_t initidx, targ_lun; 11485 int retval; 11486 11487 retval = 0; 11488 11489 lun = NULL; 11490 11491 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11492 if ((targ_lun < CTL_MAX_LUNS) 11493 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11494 lun = ctl_softc->ctl_luns[targ_lun]; 11495 /* 11496 * If the LUN is invalid, pretend that it doesn't exist. 11497 * It will go away as soon as all pending I/O has been 11498 * completed. 11499 */ 11500 if (lun->flags & CTL_LUN_DISABLED) { 11501 lun = NULL; 11502 } else { 11503 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11504 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11505 lun->be_lun; 11506 if (lun->be_lun->lun_type == T_PROCESSOR) { 11507 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11508 } 11509 11510 /* 11511 * Every I/O goes into the OOA queue for a 11512 * particular LUN, and stays there until completion. 11513 */ 11514 mtx_lock(&lun->lun_lock); 11515 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11516 ooa_links); 11517 } 11518 } else { 11519 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11520 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11521 } 11522 11523 /* Get command entry and return error if it is unsuppotyed. */ 11524 entry = ctl_validate_command(ctsio); 11525 if (entry == NULL) { 11526 if (lun) 11527 mtx_unlock(&lun->lun_lock); 11528 return (retval); 11529 } 11530 11531 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11532 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11533 11534 /* 11535 * Check to see whether we can send this command to LUNs that don't 11536 * exist. This should pretty much only be the case for inquiry 11537 * and request sense. Further checks, below, really require having 11538 * a LUN, so we can't really check the command anymore. Just put 11539 * it on the rtr queue. 11540 */ 11541 if (lun == NULL) { 11542 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11543 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11544 ctl_enqueue_rtr((union ctl_io *)ctsio); 11545 return (retval); 11546 } 11547 11548 ctl_set_unsupported_lun(ctsio); 11549 ctl_done((union ctl_io *)ctsio); 11550 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11551 return (retval); 11552 } else { 11553 /* 11554 * Make sure we support this particular command on this LUN. 11555 * e.g., we don't support writes to the control LUN. 11556 */ 11557 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11558 mtx_unlock(&lun->lun_lock); 11559 ctl_set_invalid_opcode(ctsio); 11560 ctl_done((union ctl_io *)ctsio); 11561 return (retval); 11562 } 11563 } 11564 11565 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11566 11567#ifdef CTL_WITH_CA 11568 /* 11569 * If we've got a request sense, it'll clear the contingent 11570 * allegiance condition. Otherwise, if we have a CA condition for 11571 * this initiator, clear it, because it sent down a command other 11572 * than request sense. 11573 */ 11574 if ((ctsio->cdb[0] != REQUEST_SENSE) 11575 && (ctl_is_set(lun->have_ca, initidx))) 11576 ctl_clear_mask(lun->have_ca, initidx); 11577#endif 11578 11579 /* 11580 * If the command has this flag set, it handles its own unit 11581 * attention reporting, we shouldn't do anything. Otherwise we 11582 * check for any pending unit attentions, and send them back to the 11583 * initiator. We only do this when a command initially comes in, 11584 * not when we pull it off the blocked queue. 11585 * 11586 * According to SAM-3, section 5.3.2, the order that things get 11587 * presented back to the host is basically unit attentions caused 11588 * by some sort of reset event, busy status, reservation conflicts 11589 * or task set full, and finally any other status. 11590 * 11591 * One issue here is that some of the unit attentions we report 11592 * don't fall into the "reset" category (e.g. "reported luns data 11593 * has changed"). So reporting it here, before the reservation 11594 * check, may be technically wrong. I guess the only thing to do 11595 * would be to check for and report the reset events here, and then 11596 * check for the other unit attention types after we check for a 11597 * reservation conflict. 11598 * 11599 * XXX KDM need to fix this 11600 */ 11601 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11602 ctl_ua_type ua_type; 11603 11604 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11605 scsi_sense_data_type sense_format; 11606 11607 if (lun != NULL) 11608 sense_format = (lun->flags & 11609 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11610 SSD_TYPE_FIXED; 11611 else 11612 sense_format = SSD_TYPE_FIXED; 11613 11614 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11615 &ctsio->sense_data, sense_format); 11616 if (ua_type != CTL_UA_NONE) { 11617 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11618 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11619 CTL_AUTOSENSE; 11620 ctsio->sense_len = SSD_FULL_SIZE; 11621 mtx_unlock(&lun->lun_lock); 11622 ctl_done((union ctl_io *)ctsio); 11623 return (retval); 11624 } 11625 } 11626 } 11627 11628 11629 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11630 mtx_unlock(&lun->lun_lock); 11631 ctl_done((union ctl_io *)ctsio); 11632 return (retval); 11633 } 11634 11635 /* 11636 * XXX CHD this is where we want to send IO to other side if 11637 * this LUN is secondary on this SC. We will need to make a copy 11638 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11639 * the copy we send as FROM_OTHER. 11640 * We also need to stuff the address of the original IO so we can 11641 * find it easily. Something similar will need be done on the other 11642 * side so when we are done we can find the copy. 11643 */ 11644 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11645 union ctl_ha_msg msg_info; 11646 int isc_retval; 11647 11648 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11649 11650 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11651 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11652#if 0 11653 printf("1. ctsio %p\n", ctsio); 11654#endif 11655 msg_info.hdr.serializing_sc = NULL; 11656 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11657 msg_info.scsi.tag_num = ctsio->tag_num; 11658 msg_info.scsi.tag_type = ctsio->tag_type; 11659 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11660 11661 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11662 11663 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11664 (void *)&msg_info, sizeof(msg_info), 0)) > 11665 CTL_HA_STATUS_SUCCESS) { 11666 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11667 isc_retval); 11668 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11669 } else { 11670#if 0 11671 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11672#endif 11673 } 11674 11675 /* 11676 * XXX KDM this I/O is off the incoming queue, but hasn't 11677 * been inserted on any other queue. We may need to come 11678 * up with a holding queue while we wait for serialization 11679 * so that we have an idea of what we're waiting for from 11680 * the other side. 11681 */ 11682 mtx_unlock(&lun->lun_lock); 11683 return (retval); 11684 } 11685 11686 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11687 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11688 ctl_ooaq, ooa_links))) { 11689 case CTL_ACTION_BLOCK: 11690 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11691 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11692 blocked_links); 11693 mtx_unlock(&lun->lun_lock); 11694 return (retval); 11695 case CTL_ACTION_PASS: 11696 case CTL_ACTION_SKIP: 11697 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11698 mtx_unlock(&lun->lun_lock); 11699 ctl_enqueue_rtr((union ctl_io *)ctsio); 11700 break; 11701 case CTL_ACTION_OVERLAP: 11702 mtx_unlock(&lun->lun_lock); 11703 ctl_set_overlapped_cmd(ctsio); 11704 ctl_done((union ctl_io *)ctsio); 11705 break; 11706 case CTL_ACTION_OVERLAP_TAG: 11707 mtx_unlock(&lun->lun_lock); 11708 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11709 ctl_done((union ctl_io *)ctsio); 11710 break; 11711 case CTL_ACTION_ERROR: 11712 default: 11713 mtx_unlock(&lun->lun_lock); 11714 ctl_set_internal_failure(ctsio, 11715 /*sks_valid*/ 0, 11716 /*retry_count*/ 0); 11717 ctl_done((union ctl_io *)ctsio); 11718 break; 11719 } 11720 return (retval); 11721} 11722 11723const struct ctl_cmd_entry * 11724ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11725{ 11726 const struct ctl_cmd_entry *entry; 11727 int service_action; 11728 11729 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11730 if (entry->flags & CTL_CMD_FLAG_SA5) { 11731 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11732 entry = &((const struct ctl_cmd_entry *) 11733 entry->execute)[service_action]; 11734 } 11735 return (entry); 11736} 11737 11738const struct ctl_cmd_entry * 11739ctl_validate_command(struct ctl_scsiio *ctsio) 11740{ 11741 const struct ctl_cmd_entry *entry; 11742 int i; 11743 uint8_t diff; 11744 11745 entry = ctl_get_cmd_entry(ctsio); 11746 if (entry->execute == NULL) { 11747 ctl_set_invalid_opcode(ctsio); 11748 ctl_done((union ctl_io *)ctsio); 11749 return (NULL); 11750 } 11751 KASSERT(entry->length > 0, 11752 ("Not defined length for command 0x%02x/0x%02x", 11753 ctsio->cdb[0], ctsio->cdb[1])); 11754 for (i = 1; i < entry->length; i++) { 11755 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11756 if (diff == 0) 11757 continue; 11758 ctl_set_invalid_field(ctsio, 11759 /*sks_valid*/ 1, 11760 /*command*/ 1, 11761 /*field*/ i, 11762 /*bit_valid*/ 1, 11763 /*bit*/ fls(diff) - 1); 11764 ctl_done((union ctl_io *)ctsio); 11765 return (NULL); 11766 } 11767 return (entry); 11768} 11769 11770static int 11771ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11772{ 11773 11774 switch (lun_type) { 11775 case T_PROCESSOR: 11776 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11777 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11778 return (0); 11779 break; 11780 case T_DIRECT: 11781 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11782 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11783 return (0); 11784 break; 11785 default: 11786 return (0); 11787 } 11788 return (1); 11789} 11790 11791static int 11792ctl_scsiio(struct ctl_scsiio *ctsio) 11793{ 11794 int retval; 11795 const struct ctl_cmd_entry *entry; 11796 11797 retval = CTL_RETVAL_COMPLETE; 11798 11799 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11800 11801 entry = ctl_get_cmd_entry(ctsio); 11802 11803 /* 11804 * If this I/O has been aborted, just send it straight to 11805 * ctl_done() without executing it. 11806 */ 11807 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11808 ctl_done((union ctl_io *)ctsio); 11809 goto bailout; 11810 } 11811 11812 /* 11813 * All the checks should have been handled by ctl_scsiio_precheck(). 11814 * We should be clear now to just execute the I/O. 11815 */ 11816 retval = entry->execute(ctsio); 11817 11818bailout: 11819 return (retval); 11820} 11821 11822/* 11823 * Since we only implement one target right now, a bus reset simply resets 11824 * our single target. 11825 */ 11826static int 11827ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11828{ 11829 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11830} 11831 11832static int 11833ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11834 ctl_ua_type ua_type) 11835{ 11836 struct ctl_lun *lun; 11837 int retval; 11838 11839 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11840 union ctl_ha_msg msg_info; 11841 11842 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11843 msg_info.hdr.nexus = io->io_hdr.nexus; 11844 if (ua_type==CTL_UA_TARG_RESET) 11845 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11846 else 11847 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11848 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11849 msg_info.hdr.original_sc = NULL; 11850 msg_info.hdr.serializing_sc = NULL; 11851 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11852 (void *)&msg_info, sizeof(msg_info), 0)) { 11853 } 11854 } 11855 retval = 0; 11856 11857 mtx_lock(&ctl_softc->ctl_lock); 11858 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11859 retval += ctl_lun_reset(lun, io, ua_type); 11860 mtx_unlock(&ctl_softc->ctl_lock); 11861 11862 return (retval); 11863} 11864 11865/* 11866 * The LUN should always be set. The I/O is optional, and is used to 11867 * distinguish between I/Os sent by this initiator, and by other 11868 * initiators. We set unit attention for initiators other than this one. 11869 * SAM-3 is vague on this point. It does say that a unit attention should 11870 * be established for other initiators when a LUN is reset (see section 11871 * 5.7.3), but it doesn't specifically say that the unit attention should 11872 * be established for this particular initiator when a LUN is reset. Here 11873 * is the relevant text, from SAM-3 rev 8: 11874 * 11875 * 5.7.2 When a SCSI initiator port aborts its own tasks 11876 * 11877 * When a SCSI initiator port causes its own task(s) to be aborted, no 11878 * notification that the task(s) have been aborted shall be returned to 11879 * the SCSI initiator port other than the completion response for the 11880 * command or task management function action that caused the task(s) to 11881 * be aborted and notification(s) associated with related effects of the 11882 * action (e.g., a reset unit attention condition). 11883 * 11884 * XXX KDM for now, we're setting unit attention for all initiators. 11885 */ 11886static int 11887ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11888{ 11889 union ctl_io *xio; 11890#if 0 11891 uint32_t initindex; 11892#endif 11893 int i; 11894 11895 mtx_lock(&lun->lun_lock); 11896 /* 11897 * Run through the OOA queue and abort each I/O. 11898 */ 11899#if 0 11900 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11901#endif 11902 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11903 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11904 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11905 } 11906 11907 /* 11908 * This version sets unit attention for every 11909 */ 11910#if 0 11911 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11912 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11913 if (initindex == i) 11914 continue; 11915 lun->pending_ua[i] |= ua_type; 11916 } 11917#endif 11918 11919 /* 11920 * A reset (any kind, really) clears reservations established with 11921 * RESERVE/RELEASE. It does not clear reservations established 11922 * with PERSISTENT RESERVE OUT, but we don't support that at the 11923 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11924 * reservations made with the RESERVE/RELEASE commands, because 11925 * those commands are obsolete in SPC-3. 11926 */ 11927 lun->flags &= ~CTL_LUN_RESERVED; 11928 11929 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11930#ifdef CTL_WITH_CA 11931 ctl_clear_mask(lun->have_ca, i); 11932#endif 11933 lun->pending_ua[i] |= ua_type; 11934 } 11935 mtx_unlock(&lun->lun_lock); 11936 11937 return (0); 11938} 11939 11940static int 11941ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11942 int other_sc) 11943{ 11944 union ctl_io *xio; 11945 int found; 11946 11947 mtx_assert(&lun->lun_lock, MA_OWNED); 11948 11949 /* 11950 * Run through the OOA queue and attempt to find the given I/O. 11951 * The target port, initiator ID, tag type and tag number have to 11952 * match the values that we got from the initiator. If we have an 11953 * untagged command to abort, simply abort the first untagged command 11954 * we come to. We only allow one untagged command at a time of course. 11955 */ 11956 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11957 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11958 11959 if ((targ_port == UINT32_MAX || 11960 targ_port == xio->io_hdr.nexus.targ_port) && 11961 (init_id == UINT32_MAX || 11962 init_id == xio->io_hdr.nexus.initid.id)) { 11963 if (targ_port != xio->io_hdr.nexus.targ_port || 11964 init_id != xio->io_hdr.nexus.initid.id) 11965 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11966 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11967 found = 1; 11968 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11969 union ctl_ha_msg msg_info; 11970 11971 msg_info.hdr.nexus = xio->io_hdr.nexus; 11972 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11973 msg_info.task.tag_num = xio->scsiio.tag_num; 11974 msg_info.task.tag_type = xio->scsiio.tag_type; 11975 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11976 msg_info.hdr.original_sc = NULL; 11977 msg_info.hdr.serializing_sc = NULL; 11978 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11979 (void *)&msg_info, sizeof(msg_info), 0); 11980 } 11981 } 11982 } 11983 return (found); 11984} 11985 11986static int 11987ctl_abort_task_set(union ctl_io *io) 11988{ 11989 struct ctl_softc *softc = control_softc; 11990 struct ctl_lun *lun; 11991 uint32_t targ_lun; 11992 11993 /* 11994 * Look up the LUN. 11995 */ 11996 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11997 mtx_lock(&softc->ctl_lock); 11998 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11999 lun = softc->ctl_luns[targ_lun]; 12000 else { 12001 mtx_unlock(&softc->ctl_lock); 12002 return (1); 12003 } 12004 12005 mtx_lock(&lun->lun_lock); 12006 mtx_unlock(&softc->ctl_lock); 12007 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12008 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12009 io->io_hdr.nexus.initid.id, 12010 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12011 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12012 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12013 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12014 } 12015 mtx_unlock(&lun->lun_lock); 12016 return (0); 12017} 12018 12019static int 12020ctl_i_t_nexus_reset(union ctl_io *io) 12021{ 12022 struct ctl_softc *softc = control_softc; 12023 struct ctl_lun *lun; 12024 uint32_t initindex; 12025 12026 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12027 mtx_lock(&softc->ctl_lock); 12028 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12029 mtx_lock(&lun->lun_lock); 12030 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12031 io->io_hdr.nexus.initid.id, 12032 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12033#ifdef CTL_WITH_CA 12034 ctl_clear_mask(lun->have_ca, initindex); 12035#endif 12036 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12037 mtx_unlock(&lun->lun_lock); 12038 } 12039 mtx_unlock(&softc->ctl_lock); 12040 return (0); 12041} 12042 12043static int 12044ctl_abort_task(union ctl_io *io) 12045{ 12046 union ctl_io *xio; 12047 struct ctl_lun *lun; 12048 struct ctl_softc *ctl_softc; 12049#if 0 12050 struct sbuf sb; 12051 char printbuf[128]; 12052#endif 12053 int found; 12054 uint32_t targ_lun; 12055 12056 ctl_softc = control_softc; 12057 found = 0; 12058 12059 /* 12060 * Look up the LUN. 12061 */ 12062 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12063 mtx_lock(&ctl_softc->ctl_lock); 12064 if ((targ_lun < CTL_MAX_LUNS) 12065 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12066 lun = ctl_softc->ctl_luns[targ_lun]; 12067 else { 12068 mtx_unlock(&ctl_softc->ctl_lock); 12069 return (1); 12070 } 12071 12072#if 0 12073 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12074 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12075#endif 12076 12077 mtx_lock(&lun->lun_lock); 12078 mtx_unlock(&ctl_softc->ctl_lock); 12079 /* 12080 * Run through the OOA queue and attempt to find the given I/O. 12081 * The target port, initiator ID, tag type and tag number have to 12082 * match the values that we got from the initiator. If we have an 12083 * untagged command to abort, simply abort the first untagged command 12084 * we come to. We only allow one untagged command at a time of course. 12085 */ 12086#if 0 12087 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12088#endif 12089 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12090 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12091#if 0 12092 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12093 12094 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12095 lun->lun, xio->scsiio.tag_num, 12096 xio->scsiio.tag_type, 12097 (xio->io_hdr.blocked_links.tqe_prev 12098 == NULL) ? "" : " BLOCKED", 12099 (xio->io_hdr.flags & 12100 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12101 (xio->io_hdr.flags & 12102 CTL_FLAG_ABORT) ? " ABORT" : "", 12103 (xio->io_hdr.flags & 12104 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12105 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12106 sbuf_finish(&sb); 12107 printf("%s\n", sbuf_data(&sb)); 12108#endif 12109 12110 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12111 && (xio->io_hdr.nexus.initid.id == 12112 io->io_hdr.nexus.initid.id)) { 12113 /* 12114 * If the abort says that the task is untagged, the 12115 * task in the queue must be untagged. Otherwise, 12116 * we just check to see whether the tag numbers 12117 * match. This is because the QLogic firmware 12118 * doesn't pass back the tag type in an abort 12119 * request. 12120 */ 12121#if 0 12122 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12123 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12124 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12125#endif 12126 /* 12127 * XXX KDM we've got problems with FC, because it 12128 * doesn't send down a tag type with aborts. So we 12129 * can only really go by the tag number... 12130 * This may cause problems with parallel SCSI. 12131 * Need to figure that out!! 12132 */ 12133 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12134 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12135 found = 1; 12136 if ((io->io_hdr.flags & 12137 CTL_FLAG_FROM_OTHER_SC) == 0 && 12138 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12139 union ctl_ha_msg msg_info; 12140 12141 io->io_hdr.flags |= 12142 CTL_FLAG_SENT_2OTHER_SC; 12143 msg_info.hdr.nexus = io->io_hdr.nexus; 12144 msg_info.task.task_action = 12145 CTL_TASK_ABORT_TASK; 12146 msg_info.task.tag_num = 12147 io->taskio.tag_num; 12148 msg_info.task.tag_type = 12149 io->taskio.tag_type; 12150 msg_info.hdr.msg_type = 12151 CTL_MSG_MANAGE_TASKS; 12152 msg_info.hdr.original_sc = NULL; 12153 msg_info.hdr.serializing_sc = NULL; 12154#if 0 12155 printf("Sent Abort to other side\n"); 12156#endif 12157 if (CTL_HA_STATUS_SUCCESS != 12158 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12159 (void *)&msg_info, 12160 sizeof(msg_info), 0)) { 12161 } 12162 } 12163#if 0 12164 printf("ctl_abort_task: found I/O to abort\n"); 12165#endif 12166 break; 12167 } 12168 } 12169 } 12170 mtx_unlock(&lun->lun_lock); 12171 12172 if (found == 0) { 12173 /* 12174 * This isn't really an error. It's entirely possible for 12175 * the abort and command completion to cross on the wire. 12176 * This is more of an informative/diagnostic error. 12177 */ 12178#if 0 12179 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12180 "%d:%d:%d:%d tag %d type %d\n", 12181 io->io_hdr.nexus.initid.id, 12182 io->io_hdr.nexus.targ_port, 12183 io->io_hdr.nexus.targ_target.id, 12184 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12185 io->taskio.tag_type); 12186#endif 12187 } 12188 return (0); 12189} 12190 12191static void 12192ctl_run_task(union ctl_io *io) 12193{ 12194 struct ctl_softc *ctl_softc = control_softc; 12195 int retval = 1; 12196 const char *task_desc; 12197 12198 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12199 12200 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12201 ("ctl_run_task: Unextected io_type %d\n", 12202 io->io_hdr.io_type)); 12203 12204 task_desc = ctl_scsi_task_string(&io->taskio); 12205 if (task_desc != NULL) { 12206#ifdef NEEDTOPORT 12207 csevent_log(CSC_CTL | CSC_SHELF_SW | 12208 CTL_TASK_REPORT, 12209 csevent_LogType_Trace, 12210 csevent_Severity_Information, 12211 csevent_AlertLevel_Green, 12212 csevent_FRU_Firmware, 12213 csevent_FRU_Unknown, 12214 "CTL: received task: %s",task_desc); 12215#endif 12216 } else { 12217#ifdef NEEDTOPORT 12218 csevent_log(CSC_CTL | CSC_SHELF_SW | 12219 CTL_TASK_REPORT, 12220 csevent_LogType_Trace, 12221 csevent_Severity_Information, 12222 csevent_AlertLevel_Green, 12223 csevent_FRU_Firmware, 12224 csevent_FRU_Unknown, 12225 "CTL: received unknown task " 12226 "type: %d (%#x)", 12227 io->taskio.task_action, 12228 io->taskio.task_action); 12229#endif 12230 } 12231 switch (io->taskio.task_action) { 12232 case CTL_TASK_ABORT_TASK: 12233 retval = ctl_abort_task(io); 12234 break; 12235 case CTL_TASK_ABORT_TASK_SET: 12236 case CTL_TASK_CLEAR_TASK_SET: 12237 retval = ctl_abort_task_set(io); 12238 break; 12239 case CTL_TASK_CLEAR_ACA: 12240 break; 12241 case CTL_TASK_I_T_NEXUS_RESET: 12242 retval = ctl_i_t_nexus_reset(io); 12243 break; 12244 case CTL_TASK_LUN_RESET: { 12245 struct ctl_lun *lun; 12246 uint32_t targ_lun; 12247 12248 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12249 mtx_lock(&ctl_softc->ctl_lock); 12250 if ((targ_lun < CTL_MAX_LUNS) 12251 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12252 lun = ctl_softc->ctl_luns[targ_lun]; 12253 else { 12254 mtx_unlock(&ctl_softc->ctl_lock); 12255 retval = 1; 12256 break; 12257 } 12258 12259 if (!(io->io_hdr.flags & 12260 CTL_FLAG_FROM_OTHER_SC)) { 12261 union ctl_ha_msg msg_info; 12262 12263 io->io_hdr.flags |= 12264 CTL_FLAG_SENT_2OTHER_SC; 12265 msg_info.hdr.msg_type = 12266 CTL_MSG_MANAGE_TASKS; 12267 msg_info.hdr.nexus = io->io_hdr.nexus; 12268 msg_info.task.task_action = 12269 CTL_TASK_LUN_RESET; 12270 msg_info.hdr.original_sc = NULL; 12271 msg_info.hdr.serializing_sc = NULL; 12272 if (CTL_HA_STATUS_SUCCESS != 12273 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12274 (void *)&msg_info, 12275 sizeof(msg_info), 0)) { 12276 } 12277 } 12278 12279 retval = ctl_lun_reset(lun, io, 12280 CTL_UA_LUN_RESET); 12281 mtx_unlock(&ctl_softc->ctl_lock); 12282 break; 12283 } 12284 case CTL_TASK_TARGET_RESET: 12285 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12286 break; 12287 case CTL_TASK_BUS_RESET: 12288 retval = ctl_bus_reset(ctl_softc, io); 12289 break; 12290 case CTL_TASK_PORT_LOGIN: 12291 break; 12292 case CTL_TASK_PORT_LOGOUT: 12293 break; 12294 default: 12295 printf("ctl_run_task: got unknown task management event %d\n", 12296 io->taskio.task_action); 12297 break; 12298 } 12299 if (retval == 0) 12300 io->io_hdr.status = CTL_SUCCESS; 12301 else 12302 io->io_hdr.status = CTL_ERROR; 12303 ctl_done(io); 12304} 12305 12306/* 12307 * For HA operation. Handle commands that come in from the other 12308 * controller. 12309 */ 12310static void 12311ctl_handle_isc(union ctl_io *io) 12312{ 12313 int free_io; 12314 struct ctl_lun *lun; 12315 struct ctl_softc *ctl_softc; 12316 uint32_t targ_lun; 12317 12318 ctl_softc = control_softc; 12319 12320 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12321 lun = ctl_softc->ctl_luns[targ_lun]; 12322 12323 switch (io->io_hdr.msg_type) { 12324 case CTL_MSG_SERIALIZE: 12325 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12326 break; 12327 case CTL_MSG_R2R: { 12328 const struct ctl_cmd_entry *entry; 12329 12330 /* 12331 * This is only used in SER_ONLY mode. 12332 */ 12333 free_io = 0; 12334 entry = ctl_get_cmd_entry(&io->scsiio); 12335 mtx_lock(&lun->lun_lock); 12336 if (ctl_scsiio_lun_check(ctl_softc, lun, 12337 entry, (struct ctl_scsiio *)io) != 0) { 12338 mtx_unlock(&lun->lun_lock); 12339 ctl_done(io); 12340 break; 12341 } 12342 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12343 mtx_unlock(&lun->lun_lock); 12344 ctl_enqueue_rtr(io); 12345 break; 12346 } 12347 case CTL_MSG_FINISH_IO: 12348 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12349 free_io = 0; 12350 ctl_done(io); 12351 } else { 12352 free_io = 1; 12353 mtx_lock(&lun->lun_lock); 12354 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12355 ooa_links); 12356 ctl_check_blocked(lun); 12357 mtx_unlock(&lun->lun_lock); 12358 } 12359 break; 12360 case CTL_MSG_PERS_ACTION: 12361 ctl_hndl_per_res_out_on_other_sc( 12362 (union ctl_ha_msg *)&io->presio.pr_msg); 12363 free_io = 1; 12364 break; 12365 case CTL_MSG_BAD_JUJU: 12366 free_io = 0; 12367 ctl_done(io); 12368 break; 12369 case CTL_MSG_DATAMOVE: 12370 /* Only used in XFER mode */ 12371 free_io = 0; 12372 ctl_datamove_remote(io); 12373 break; 12374 case CTL_MSG_DATAMOVE_DONE: 12375 /* Only used in XFER mode */ 12376 free_io = 0; 12377 io->scsiio.be_move_done(io); 12378 break; 12379 default: 12380 free_io = 1; 12381 printf("%s: Invalid message type %d\n", 12382 __func__, io->io_hdr.msg_type); 12383 break; 12384 } 12385 if (free_io) 12386 ctl_free_io(io); 12387 12388} 12389 12390 12391/* 12392 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12393 * there is no match. 12394 */ 12395static ctl_lun_error_pattern 12396ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12397{ 12398 const struct ctl_cmd_entry *entry; 12399 ctl_lun_error_pattern filtered_pattern, pattern; 12400 12401 pattern = desc->error_pattern; 12402 12403 /* 12404 * XXX KDM we need more data passed into this function to match a 12405 * custom pattern, and we actually need to implement custom pattern 12406 * matching. 12407 */ 12408 if (pattern & CTL_LUN_PAT_CMD) 12409 return (CTL_LUN_PAT_CMD); 12410 12411 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12412 return (CTL_LUN_PAT_ANY); 12413 12414 entry = ctl_get_cmd_entry(ctsio); 12415 12416 filtered_pattern = entry->pattern & pattern; 12417 12418 /* 12419 * If the user requested specific flags in the pattern (e.g. 12420 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12421 * flags. 12422 * 12423 * If the user did not specify any flags, it doesn't matter whether 12424 * or not the command supports the flags. 12425 */ 12426 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12427 (pattern & ~CTL_LUN_PAT_MASK)) 12428 return (CTL_LUN_PAT_NONE); 12429 12430 /* 12431 * If the user asked for a range check, see if the requested LBA 12432 * range overlaps with this command's LBA range. 12433 */ 12434 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12435 uint64_t lba1; 12436 uint32_t len1; 12437 ctl_action action; 12438 int retval; 12439 12440 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12441 if (retval != 0) 12442 return (CTL_LUN_PAT_NONE); 12443 12444 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12445 desc->lba_range.len); 12446 /* 12447 * A "pass" means that the LBA ranges don't overlap, so 12448 * this doesn't match the user's range criteria. 12449 */ 12450 if (action == CTL_ACTION_PASS) 12451 return (CTL_LUN_PAT_NONE); 12452 } 12453 12454 return (filtered_pattern); 12455} 12456 12457static void 12458ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12459{ 12460 struct ctl_error_desc *desc, *desc2; 12461 12462 mtx_assert(&lun->lun_lock, MA_OWNED); 12463 12464 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12465 ctl_lun_error_pattern pattern; 12466 /* 12467 * Check to see whether this particular command matches 12468 * the pattern in the descriptor. 12469 */ 12470 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12471 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12472 continue; 12473 12474 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12475 case CTL_LUN_INJ_ABORTED: 12476 ctl_set_aborted(&io->scsiio); 12477 break; 12478 case CTL_LUN_INJ_MEDIUM_ERR: 12479 ctl_set_medium_error(&io->scsiio); 12480 break; 12481 case CTL_LUN_INJ_UA: 12482 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12483 * OCCURRED */ 12484 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12485 break; 12486 case CTL_LUN_INJ_CUSTOM: 12487 /* 12488 * We're assuming the user knows what he is doing. 12489 * Just copy the sense information without doing 12490 * checks. 12491 */ 12492 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12493 ctl_min(sizeof(desc->custom_sense), 12494 sizeof(io->scsiio.sense_data))); 12495 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12496 io->scsiio.sense_len = SSD_FULL_SIZE; 12497 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12498 break; 12499 case CTL_LUN_INJ_NONE: 12500 default: 12501 /* 12502 * If this is an error injection type we don't know 12503 * about, clear the continuous flag (if it is set) 12504 * so it will get deleted below. 12505 */ 12506 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12507 break; 12508 } 12509 /* 12510 * By default, each error injection action is a one-shot 12511 */ 12512 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12513 continue; 12514 12515 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12516 12517 free(desc, M_CTL); 12518 } 12519} 12520 12521#ifdef CTL_IO_DELAY 12522static void 12523ctl_datamove_timer_wakeup(void *arg) 12524{ 12525 union ctl_io *io; 12526 12527 io = (union ctl_io *)arg; 12528 12529 ctl_datamove(io); 12530} 12531#endif /* CTL_IO_DELAY */ 12532 12533void 12534ctl_datamove(union ctl_io *io) 12535{ 12536 void (*fe_datamove)(union ctl_io *io); 12537 12538 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12539 12540 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12541 12542#ifdef CTL_TIME_IO 12543 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12544 char str[256]; 12545 char path_str[64]; 12546 struct sbuf sb; 12547 12548 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12549 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12550 12551 sbuf_cat(&sb, path_str); 12552 switch (io->io_hdr.io_type) { 12553 case CTL_IO_SCSI: 12554 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12555 sbuf_printf(&sb, "\n"); 12556 sbuf_cat(&sb, path_str); 12557 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12558 io->scsiio.tag_num, io->scsiio.tag_type); 12559 break; 12560 case CTL_IO_TASK: 12561 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12562 "Tag Type: %d\n", io->taskio.task_action, 12563 io->taskio.tag_num, io->taskio.tag_type); 12564 break; 12565 default: 12566 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12567 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12568 break; 12569 } 12570 sbuf_cat(&sb, path_str); 12571 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12572 (intmax_t)time_uptime - io->io_hdr.start_time); 12573 sbuf_finish(&sb); 12574 printf("%s", sbuf_data(&sb)); 12575 } 12576#endif /* CTL_TIME_IO */ 12577 12578#ifdef CTL_IO_DELAY 12579 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12580 struct ctl_lun *lun; 12581 12582 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12583 12584 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12585 } else { 12586 struct ctl_lun *lun; 12587 12588 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12589 if ((lun != NULL) 12590 && (lun->delay_info.datamove_delay > 0)) { 12591 struct callout *callout; 12592 12593 callout = (struct callout *)&io->io_hdr.timer_bytes; 12594 callout_init(callout, /*mpsafe*/ 1); 12595 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12596 callout_reset(callout, 12597 lun->delay_info.datamove_delay * hz, 12598 ctl_datamove_timer_wakeup, io); 12599 if (lun->delay_info.datamove_type == 12600 CTL_DELAY_TYPE_ONESHOT) 12601 lun->delay_info.datamove_delay = 0; 12602 return; 12603 } 12604 } 12605#endif 12606 12607 /* 12608 * This command has been aborted. Set the port status, so we fail 12609 * the data move. 12610 */ 12611 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12612 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12613 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12614 io->io_hdr.nexus.targ_port, 12615 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12616 io->io_hdr.nexus.targ_lun); 12617 io->io_hdr.port_status = 31337; 12618 /* 12619 * Note that the backend, in this case, will get the 12620 * callback in its context. In other cases it may get 12621 * called in the frontend's interrupt thread context. 12622 */ 12623 io->scsiio.be_move_done(io); 12624 return; 12625 } 12626 12627 /* 12628 * If we're in XFER mode and this I/O is from the other shelf 12629 * controller, we need to send the DMA to the other side to 12630 * actually transfer the data to/from the host. In serialize only 12631 * mode the transfer happens below CTL and ctl_datamove() is only 12632 * called on the machine that originally received the I/O. 12633 */ 12634 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12635 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12636 union ctl_ha_msg msg; 12637 uint32_t sg_entries_sent; 12638 int do_sg_copy; 12639 int i; 12640 12641 memset(&msg, 0, sizeof(msg)); 12642 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12643 msg.hdr.original_sc = io->io_hdr.original_sc; 12644 msg.hdr.serializing_sc = io; 12645 msg.hdr.nexus = io->io_hdr.nexus; 12646 msg.dt.flags = io->io_hdr.flags; 12647 /* 12648 * We convert everything into a S/G list here. We can't 12649 * pass by reference, only by value between controllers. 12650 * So we can't pass a pointer to the S/G list, only as many 12651 * S/G entries as we can fit in here. If it's possible for 12652 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12653 * then we need to break this up into multiple transfers. 12654 */ 12655 if (io->scsiio.kern_sg_entries == 0) { 12656 msg.dt.kern_sg_entries = 1; 12657 /* 12658 * If this is in cached memory, flush the cache 12659 * before we send the DMA request to the other 12660 * controller. We want to do this in either the 12661 * read or the write case. The read case is 12662 * straightforward. In the write case, we want to 12663 * make sure nothing is in the local cache that 12664 * could overwrite the DMAed data. 12665 */ 12666 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12667 /* 12668 * XXX KDM use bus_dmamap_sync() here. 12669 */ 12670 } 12671 12672 /* 12673 * Convert to a physical address if this is a 12674 * virtual address. 12675 */ 12676 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12677 msg.dt.sg_list[0].addr = 12678 io->scsiio.kern_data_ptr; 12679 } else { 12680 /* 12681 * XXX KDM use busdma here! 12682 */ 12683#if 0 12684 msg.dt.sg_list[0].addr = (void *) 12685 vtophys(io->scsiio.kern_data_ptr); 12686#endif 12687 } 12688 12689 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12690 do_sg_copy = 0; 12691 } else { 12692 struct ctl_sg_entry *sgl; 12693 12694 do_sg_copy = 1; 12695 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12696 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12697 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12698 /* 12699 * XXX KDM use bus_dmamap_sync() here. 12700 */ 12701 } 12702 } 12703 12704 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12705 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12706 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12707 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12708 msg.dt.sg_sequence = 0; 12709 12710 /* 12711 * Loop until we've sent all of the S/G entries. On the 12712 * other end, we'll recompose these S/G entries into one 12713 * contiguous list before passing it to the 12714 */ 12715 for (sg_entries_sent = 0; sg_entries_sent < 12716 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12717 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12718 sizeof(msg.dt.sg_list[0])), 12719 msg.dt.kern_sg_entries - sg_entries_sent); 12720 12721 if (do_sg_copy != 0) { 12722 struct ctl_sg_entry *sgl; 12723 int j; 12724 12725 sgl = (struct ctl_sg_entry *) 12726 io->scsiio.kern_data_ptr; 12727 /* 12728 * If this is in cached memory, flush the cache 12729 * before we send the DMA request to the other 12730 * controller. We want to do this in either 12731 * the * read or the write case. The read 12732 * case is straightforward. In the write 12733 * case, we want to make sure nothing is 12734 * in the local cache that could overwrite 12735 * the DMAed data. 12736 */ 12737 12738 for (i = sg_entries_sent, j = 0; 12739 i < msg.dt.cur_sg_entries; i++, j++) { 12740 if ((io->io_hdr.flags & 12741 CTL_FLAG_NO_DATASYNC) == 0) { 12742 /* 12743 * XXX KDM use bus_dmamap_sync() 12744 */ 12745 } 12746 if ((io->io_hdr.flags & 12747 CTL_FLAG_BUS_ADDR) == 0) { 12748 /* 12749 * XXX KDM use busdma. 12750 */ 12751#if 0 12752 msg.dt.sg_list[j].addr =(void *) 12753 vtophys(sgl[i].addr); 12754#endif 12755 } else { 12756 msg.dt.sg_list[j].addr = 12757 sgl[i].addr; 12758 } 12759 msg.dt.sg_list[j].len = sgl[i].len; 12760 } 12761 } 12762 12763 sg_entries_sent += msg.dt.cur_sg_entries; 12764 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12765 msg.dt.sg_last = 1; 12766 else 12767 msg.dt.sg_last = 0; 12768 12769 /* 12770 * XXX KDM drop and reacquire the lock here? 12771 */ 12772 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12773 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12774 /* 12775 * XXX do something here. 12776 */ 12777 } 12778 12779 msg.dt.sent_sg_entries = sg_entries_sent; 12780 } 12781 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12782 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12783 ctl_failover_io(io, /*have_lock*/ 0); 12784 12785 } else { 12786 12787 /* 12788 * Lookup the fe_datamove() function for this particular 12789 * front end. 12790 */ 12791 fe_datamove = 12792 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12793 12794 fe_datamove(io); 12795 } 12796} 12797 12798static void 12799ctl_send_datamove_done(union ctl_io *io, int have_lock) 12800{ 12801 union ctl_ha_msg msg; 12802 int isc_status; 12803 12804 memset(&msg, 0, sizeof(msg)); 12805 12806 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12807 msg.hdr.original_sc = io; 12808 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12809 msg.hdr.nexus = io->io_hdr.nexus; 12810 msg.hdr.status = io->io_hdr.status; 12811 msg.scsi.tag_num = io->scsiio.tag_num; 12812 msg.scsi.tag_type = io->scsiio.tag_type; 12813 msg.scsi.scsi_status = io->scsiio.scsi_status; 12814 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12815 sizeof(io->scsiio.sense_data)); 12816 msg.scsi.sense_len = io->scsiio.sense_len; 12817 msg.scsi.sense_residual = io->scsiio.sense_residual; 12818 msg.scsi.fetd_status = io->io_hdr.port_status; 12819 msg.scsi.residual = io->scsiio.residual; 12820 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12821 12822 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12823 ctl_failover_io(io, /*have_lock*/ have_lock); 12824 return; 12825 } 12826 12827 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12828 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12829 /* XXX do something if this fails */ 12830 } 12831 12832} 12833 12834/* 12835 * The DMA to the remote side is done, now we need to tell the other side 12836 * we're done so it can continue with its data movement. 12837 */ 12838static void 12839ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12840{ 12841 union ctl_io *io; 12842 12843 io = rq->context; 12844 12845 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12846 printf("%s: ISC DMA write failed with error %d", __func__, 12847 rq->ret); 12848 ctl_set_internal_failure(&io->scsiio, 12849 /*sks_valid*/ 1, 12850 /*retry_count*/ rq->ret); 12851 } 12852 12853 ctl_dt_req_free(rq); 12854 12855 /* 12856 * In this case, we had to malloc the memory locally. Free it. 12857 */ 12858 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12859 int i; 12860 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12861 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12862 } 12863 /* 12864 * The data is in local and remote memory, so now we need to send 12865 * status (good or back) back to the other side. 12866 */ 12867 ctl_send_datamove_done(io, /*have_lock*/ 0); 12868} 12869 12870/* 12871 * We've moved the data from the host/controller into local memory. Now we 12872 * need to push it over to the remote controller's memory. 12873 */ 12874static int 12875ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12876{ 12877 int retval; 12878 12879 retval = 0; 12880 12881 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12882 ctl_datamove_remote_write_cb); 12883 12884 return (retval); 12885} 12886 12887static void 12888ctl_datamove_remote_write(union ctl_io *io) 12889{ 12890 int retval; 12891 void (*fe_datamove)(union ctl_io *io); 12892 12893 /* 12894 * - Get the data from the host/HBA into local memory. 12895 * - DMA memory from the local controller to the remote controller. 12896 * - Send status back to the remote controller. 12897 */ 12898 12899 retval = ctl_datamove_remote_sgl_setup(io); 12900 if (retval != 0) 12901 return; 12902 12903 /* Switch the pointer over so the FETD knows what to do */ 12904 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12905 12906 /* 12907 * Use a custom move done callback, since we need to send completion 12908 * back to the other controller, not to the backend on this side. 12909 */ 12910 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12911 12912 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12913 12914 fe_datamove(io); 12915 12916 return; 12917 12918} 12919 12920static int 12921ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12922{ 12923#if 0 12924 char str[256]; 12925 char path_str[64]; 12926 struct sbuf sb; 12927#endif 12928 12929 /* 12930 * In this case, we had to malloc the memory locally. Free it. 12931 */ 12932 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12933 int i; 12934 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12935 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12936 } 12937 12938#if 0 12939 scsi_path_string(io, path_str, sizeof(path_str)); 12940 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12941 sbuf_cat(&sb, path_str); 12942 scsi_command_string(&io->scsiio, NULL, &sb); 12943 sbuf_printf(&sb, "\n"); 12944 sbuf_cat(&sb, path_str); 12945 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12946 io->scsiio.tag_num, io->scsiio.tag_type); 12947 sbuf_cat(&sb, path_str); 12948 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12949 io->io_hdr.flags, io->io_hdr.status); 12950 sbuf_finish(&sb); 12951 printk("%s", sbuf_data(&sb)); 12952#endif 12953 12954 12955 /* 12956 * The read is done, now we need to send status (good or bad) back 12957 * to the other side. 12958 */ 12959 ctl_send_datamove_done(io, /*have_lock*/ 0); 12960 12961 return (0); 12962} 12963 12964static void 12965ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12966{ 12967 union ctl_io *io; 12968 void (*fe_datamove)(union ctl_io *io); 12969 12970 io = rq->context; 12971 12972 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12973 printf("%s: ISC DMA read failed with error %d", __func__, 12974 rq->ret); 12975 ctl_set_internal_failure(&io->scsiio, 12976 /*sks_valid*/ 1, 12977 /*retry_count*/ rq->ret); 12978 } 12979 12980 ctl_dt_req_free(rq); 12981 12982 /* Switch the pointer over so the FETD knows what to do */ 12983 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12984 12985 /* 12986 * Use a custom move done callback, since we need to send completion 12987 * back to the other controller, not to the backend on this side. 12988 */ 12989 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12990 12991 /* XXX KDM add checks like the ones in ctl_datamove? */ 12992 12993 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12994 12995 fe_datamove(io); 12996} 12997 12998static int 12999ctl_datamove_remote_sgl_setup(union ctl_io *io) 13000{ 13001 struct ctl_sg_entry *local_sglist, *remote_sglist; 13002 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13003 struct ctl_softc *softc; 13004 int retval; 13005 int i; 13006 13007 retval = 0; 13008 softc = control_softc; 13009 13010 local_sglist = io->io_hdr.local_sglist; 13011 local_dma_sglist = io->io_hdr.local_dma_sglist; 13012 remote_sglist = io->io_hdr.remote_sglist; 13013 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13014 13015 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13016 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13017 local_sglist[i].len = remote_sglist[i].len; 13018 13019 /* 13020 * XXX Detect the situation where the RS-level I/O 13021 * redirector on the other side has already read the 13022 * data off of the AOR RS on this side, and 13023 * transferred it to remote (mirror) memory on the 13024 * other side. Since we already have the data in 13025 * memory here, we just need to use it. 13026 * 13027 * XXX KDM this can probably be removed once we 13028 * get the cache device code in and take the 13029 * current AOR implementation out. 13030 */ 13031#ifdef NEEDTOPORT 13032 if ((remote_sglist[i].addr >= 13033 (void *)vtophys(softc->mirr->addr)) 13034 && (remote_sglist[i].addr < 13035 ((void *)vtophys(softc->mirr->addr) + 13036 CacheMirrorOffset))) { 13037 local_sglist[i].addr = remote_sglist[i].addr - 13038 CacheMirrorOffset; 13039 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13040 CTL_FLAG_DATA_IN) 13041 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13042 } else { 13043 local_sglist[i].addr = remote_sglist[i].addr + 13044 CacheMirrorOffset; 13045 } 13046#endif 13047#if 0 13048 printf("%s: local %p, remote %p, len %d\n", 13049 __func__, local_sglist[i].addr, 13050 remote_sglist[i].addr, local_sglist[i].len); 13051#endif 13052 } 13053 } else { 13054 uint32_t len_to_go; 13055 13056 /* 13057 * In this case, we don't have automatically allocated 13058 * memory for this I/O on this controller. This typically 13059 * happens with internal CTL I/O -- e.g. inquiry, mode 13060 * sense, etc. Anything coming from RAIDCore will have 13061 * a mirror area available. 13062 */ 13063 len_to_go = io->scsiio.kern_data_len; 13064 13065 /* 13066 * Clear the no datasync flag, we have to use malloced 13067 * buffers. 13068 */ 13069 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13070 13071 /* 13072 * The difficult thing here is that the size of the various 13073 * S/G segments may be different than the size from the 13074 * remote controller. That'll make it harder when DMAing 13075 * the data back to the other side. 13076 */ 13077 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13078 sizeof(io->io_hdr.remote_sglist[0])) && 13079 (len_to_go > 0); i++) { 13080 local_sglist[i].len = ctl_min(len_to_go, 131072); 13081 CTL_SIZE_8B(local_dma_sglist[i].len, 13082 local_sglist[i].len); 13083 local_sglist[i].addr = 13084 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13085 13086 local_dma_sglist[i].addr = local_sglist[i].addr; 13087 13088 if (local_sglist[i].addr == NULL) { 13089 int j; 13090 13091 printf("malloc failed for %zd bytes!", 13092 local_dma_sglist[i].len); 13093 for (j = 0; j < i; j++) { 13094 free(local_sglist[j].addr, M_CTL); 13095 } 13096 ctl_set_internal_failure(&io->scsiio, 13097 /*sks_valid*/ 1, 13098 /*retry_count*/ 4857); 13099 retval = 1; 13100 goto bailout_error; 13101 13102 } 13103 /* XXX KDM do we need a sync here? */ 13104 13105 len_to_go -= local_sglist[i].len; 13106 } 13107 /* 13108 * Reset the number of S/G entries accordingly. The 13109 * original number of S/G entries is available in 13110 * rem_sg_entries. 13111 */ 13112 io->scsiio.kern_sg_entries = i; 13113 13114#if 0 13115 printf("%s: kern_sg_entries = %d\n", __func__, 13116 io->scsiio.kern_sg_entries); 13117 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13118 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13119 local_sglist[i].addr, local_sglist[i].len, 13120 local_dma_sglist[i].len); 13121#endif 13122 } 13123 13124 13125 return (retval); 13126 13127bailout_error: 13128 13129 ctl_send_datamove_done(io, /*have_lock*/ 0); 13130 13131 return (retval); 13132} 13133 13134static int 13135ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13136 ctl_ha_dt_cb callback) 13137{ 13138 struct ctl_ha_dt_req *rq; 13139 struct ctl_sg_entry *remote_sglist, *local_sglist; 13140 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13141 uint32_t local_used, remote_used, total_used; 13142 int retval; 13143 int i, j; 13144 13145 retval = 0; 13146 13147 rq = ctl_dt_req_alloc(); 13148 13149 /* 13150 * If we failed to allocate the request, and if the DMA didn't fail 13151 * anyway, set busy status. This is just a resource allocation 13152 * failure. 13153 */ 13154 if ((rq == NULL) 13155 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13156 ctl_set_busy(&io->scsiio); 13157 13158 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13159 13160 if (rq != NULL) 13161 ctl_dt_req_free(rq); 13162 13163 /* 13164 * The data move failed. We need to return status back 13165 * to the other controller. No point in trying to DMA 13166 * data to the remote controller. 13167 */ 13168 13169 ctl_send_datamove_done(io, /*have_lock*/ 0); 13170 13171 retval = 1; 13172 13173 goto bailout; 13174 } 13175 13176 local_sglist = io->io_hdr.local_sglist; 13177 local_dma_sglist = io->io_hdr.local_dma_sglist; 13178 remote_sglist = io->io_hdr.remote_sglist; 13179 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13180 local_used = 0; 13181 remote_used = 0; 13182 total_used = 0; 13183 13184 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13185 rq->ret = CTL_HA_STATUS_SUCCESS; 13186 rq->context = io; 13187 callback(rq); 13188 goto bailout; 13189 } 13190 13191 /* 13192 * Pull/push the data over the wire from/to the other controller. 13193 * This takes into account the possibility that the local and 13194 * remote sglists may not be identical in terms of the size of 13195 * the elements and the number of elements. 13196 * 13197 * One fundamental assumption here is that the length allocated for 13198 * both the local and remote sglists is identical. Otherwise, we've 13199 * essentially got a coding error of some sort. 13200 */ 13201 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13202 int isc_ret; 13203 uint32_t cur_len, dma_length; 13204 uint8_t *tmp_ptr; 13205 13206 rq->id = CTL_HA_DATA_CTL; 13207 rq->command = command; 13208 rq->context = io; 13209 13210 /* 13211 * Both pointers should be aligned. But it is possible 13212 * that the allocation length is not. They should both 13213 * also have enough slack left over at the end, though, 13214 * to round up to the next 8 byte boundary. 13215 */ 13216 cur_len = ctl_min(local_sglist[i].len - local_used, 13217 remote_sglist[j].len - remote_used); 13218 13219 /* 13220 * In this case, we have a size issue and need to decrease 13221 * the size, except in the case where we actually have less 13222 * than 8 bytes left. In that case, we need to increase 13223 * the DMA length to get the last bit. 13224 */ 13225 if ((cur_len & 0x7) != 0) { 13226 if (cur_len > 0x7) { 13227 cur_len = cur_len - (cur_len & 0x7); 13228 dma_length = cur_len; 13229 } else { 13230 CTL_SIZE_8B(dma_length, cur_len); 13231 } 13232 13233 } else 13234 dma_length = cur_len; 13235 13236 /* 13237 * If we had to allocate memory for this I/O, instead of using 13238 * the non-cached mirror memory, we'll need to flush the cache 13239 * before trying to DMA to the other controller. 13240 * 13241 * We could end up doing this multiple times for the same 13242 * segment if we have a larger local segment than remote 13243 * segment. That shouldn't be an issue. 13244 */ 13245 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13246 /* 13247 * XXX KDM use bus_dmamap_sync() here. 13248 */ 13249 } 13250 13251 rq->size = dma_length; 13252 13253 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13254 tmp_ptr += local_used; 13255 13256 /* Use physical addresses when talking to ISC hardware */ 13257 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13258 /* XXX KDM use busdma */ 13259#if 0 13260 rq->local = vtophys(tmp_ptr); 13261#endif 13262 } else 13263 rq->local = tmp_ptr; 13264 13265 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13266 tmp_ptr += remote_used; 13267 rq->remote = tmp_ptr; 13268 13269 rq->callback = NULL; 13270 13271 local_used += cur_len; 13272 if (local_used >= local_sglist[i].len) { 13273 i++; 13274 local_used = 0; 13275 } 13276 13277 remote_used += cur_len; 13278 if (remote_used >= remote_sglist[j].len) { 13279 j++; 13280 remote_used = 0; 13281 } 13282 total_used += cur_len; 13283 13284 if (total_used >= io->scsiio.kern_data_len) 13285 rq->callback = callback; 13286 13287 if ((rq->size & 0x7) != 0) { 13288 printf("%s: warning: size %d is not on 8b boundary\n", 13289 __func__, rq->size); 13290 } 13291 if (((uintptr_t)rq->local & 0x7) != 0) { 13292 printf("%s: warning: local %p not on 8b boundary\n", 13293 __func__, rq->local); 13294 } 13295 if (((uintptr_t)rq->remote & 0x7) != 0) { 13296 printf("%s: warning: remote %p not on 8b boundary\n", 13297 __func__, rq->local); 13298 } 13299#if 0 13300 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13301 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13302 rq->local, rq->remote, rq->size); 13303#endif 13304 13305 isc_ret = ctl_dt_single(rq); 13306 if (isc_ret == CTL_HA_STATUS_WAIT) 13307 continue; 13308 13309 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13310 rq->ret = CTL_HA_STATUS_SUCCESS; 13311 } else { 13312 rq->ret = isc_ret; 13313 } 13314 callback(rq); 13315 goto bailout; 13316 } 13317 13318bailout: 13319 return (retval); 13320 13321} 13322 13323static void 13324ctl_datamove_remote_read(union ctl_io *io) 13325{ 13326 int retval; 13327 int i; 13328 13329 /* 13330 * This will send an error to the other controller in the case of a 13331 * failure. 13332 */ 13333 retval = ctl_datamove_remote_sgl_setup(io); 13334 if (retval != 0) 13335 return; 13336 13337 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13338 ctl_datamove_remote_read_cb); 13339 if ((retval != 0) 13340 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13341 /* 13342 * Make sure we free memory if there was an error.. The 13343 * ctl_datamove_remote_xfer() function will send the 13344 * datamove done message, or call the callback with an 13345 * error if there is a problem. 13346 */ 13347 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13348 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13349 } 13350 13351 return; 13352} 13353 13354/* 13355 * Process a datamove request from the other controller. This is used for 13356 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13357 * first. Once that is complete, the data gets DMAed into the remote 13358 * controller's memory. For reads, we DMA from the remote controller's 13359 * memory into our memory first, and then move it out to the FETD. 13360 */ 13361static void 13362ctl_datamove_remote(union ctl_io *io) 13363{ 13364 struct ctl_softc *softc; 13365 13366 softc = control_softc; 13367 13368 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13369 13370 /* 13371 * Note that we look for an aborted I/O here, but don't do some of 13372 * the other checks that ctl_datamove() normally does. 13373 * We don't need to run the datamove delay code, since that should 13374 * have been done if need be on the other controller. 13375 */ 13376 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13377 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13378 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13379 io->io_hdr.nexus.targ_port, 13380 io->io_hdr.nexus.targ_target.id, 13381 io->io_hdr.nexus.targ_lun); 13382 io->io_hdr.port_status = 31338; 13383 ctl_send_datamove_done(io, /*have_lock*/ 0); 13384 return; 13385 } 13386 13387 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13388 ctl_datamove_remote_write(io); 13389 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13390 ctl_datamove_remote_read(io); 13391 } else { 13392 union ctl_ha_msg msg; 13393 struct scsi_sense_data *sense; 13394 uint8_t sks[3]; 13395 int retry_count; 13396 13397 memset(&msg, 0, sizeof(msg)); 13398 13399 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13400 msg.hdr.status = CTL_SCSI_ERROR; 13401 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13402 13403 retry_count = 4243; 13404 13405 sense = &msg.scsi.sense_data; 13406 sks[0] = SSD_SCS_VALID; 13407 sks[1] = (retry_count >> 8) & 0xff; 13408 sks[2] = retry_count & 0xff; 13409 13410 /* "Internal target failure" */ 13411 scsi_set_sense_data(sense, 13412 /*sense_format*/ SSD_TYPE_NONE, 13413 /*current_error*/ 1, 13414 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13415 /*asc*/ 0x44, 13416 /*ascq*/ 0x00, 13417 /*type*/ SSD_ELEM_SKS, 13418 /*size*/ sizeof(sks), 13419 /*data*/ sks, 13420 SSD_ELEM_NONE); 13421 13422 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13423 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13424 ctl_failover_io(io, /*have_lock*/ 1); 13425 return; 13426 } 13427 13428 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13429 CTL_HA_STATUS_SUCCESS) { 13430 /* XXX KDM what to do if this fails? */ 13431 } 13432 return; 13433 } 13434 13435} 13436 13437static int 13438ctl_process_done(union ctl_io *io) 13439{ 13440 struct ctl_lun *lun; 13441 struct ctl_softc *ctl_softc; 13442 void (*fe_done)(union ctl_io *io); 13443 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13444 13445 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13446 13447 fe_done = 13448 control_softc->ctl_ports[targ_port]->fe_done; 13449 13450#ifdef CTL_TIME_IO 13451 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13452 char str[256]; 13453 char path_str[64]; 13454 struct sbuf sb; 13455 13456 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13457 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13458 13459 sbuf_cat(&sb, path_str); 13460 switch (io->io_hdr.io_type) { 13461 case CTL_IO_SCSI: 13462 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13463 sbuf_printf(&sb, "\n"); 13464 sbuf_cat(&sb, path_str); 13465 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13466 io->scsiio.tag_num, io->scsiio.tag_type); 13467 break; 13468 case CTL_IO_TASK: 13469 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13470 "Tag Type: %d\n", io->taskio.task_action, 13471 io->taskio.tag_num, io->taskio.tag_type); 13472 break; 13473 default: 13474 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13475 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13476 break; 13477 } 13478 sbuf_cat(&sb, path_str); 13479 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13480 (intmax_t)time_uptime - io->io_hdr.start_time); 13481 sbuf_finish(&sb); 13482 printf("%s", sbuf_data(&sb)); 13483 } 13484#endif /* CTL_TIME_IO */ 13485 13486 switch (io->io_hdr.io_type) { 13487 case CTL_IO_SCSI: 13488 break; 13489 case CTL_IO_TASK: 13490 if (bootverbose || verbose > 0) 13491 ctl_io_error_print(io, NULL); 13492 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13493 ctl_free_io(io); 13494 else 13495 fe_done(io); 13496 return (CTL_RETVAL_COMPLETE); 13497 break; 13498 default: 13499 printf("ctl_process_done: invalid io type %d\n", 13500 io->io_hdr.io_type); 13501 panic("ctl_process_done: invalid io type %d\n", 13502 io->io_hdr.io_type); 13503 break; /* NOTREACHED */ 13504 } 13505 13506 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13507 if (lun == NULL) { 13508 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13509 io->io_hdr.nexus.targ_mapped_lun)); 13510 fe_done(io); 13511 goto bailout; 13512 } 13513 ctl_softc = lun->ctl_softc; 13514 13515 mtx_lock(&lun->lun_lock); 13516 13517 /* 13518 * Check to see if we have any errors to inject here. We only 13519 * inject errors for commands that don't already have errors set. 13520 */ 13521 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13522 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13523 ctl_inject_error(lun, io); 13524 13525 /* 13526 * XXX KDM how do we treat commands that aren't completed 13527 * successfully? 13528 * 13529 * XXX KDM should we also track I/O latency? 13530 */ 13531 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13532 io->io_hdr.io_type == CTL_IO_SCSI) { 13533#ifdef CTL_TIME_IO 13534 struct bintime cur_bt; 13535#endif 13536 int type; 13537 13538 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13539 CTL_FLAG_DATA_IN) 13540 type = CTL_STATS_READ; 13541 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13542 CTL_FLAG_DATA_OUT) 13543 type = CTL_STATS_WRITE; 13544 else 13545 type = CTL_STATS_NO_IO; 13546 13547 lun->stats.ports[targ_port].bytes[type] += 13548 io->scsiio.kern_total_len; 13549 lun->stats.ports[targ_port].operations[type]++; 13550#ifdef CTL_TIME_IO 13551 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13552 &io->io_hdr.dma_bt); 13553 lun->stats.ports[targ_port].num_dmas[type] += 13554 io->io_hdr.num_dmas; 13555 getbintime(&cur_bt); 13556 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13557 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13558#endif 13559 } 13560 13561 /* 13562 * Remove this from the OOA queue. 13563 */ 13564 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13565 13566 /* 13567 * Run through the blocked queue on this LUN and see if anything 13568 * has become unblocked, now that this transaction is done. 13569 */ 13570 ctl_check_blocked(lun); 13571 13572 /* 13573 * If the LUN has been invalidated, free it if there is nothing 13574 * left on its OOA queue. 13575 */ 13576 if ((lun->flags & CTL_LUN_INVALID) 13577 && TAILQ_EMPTY(&lun->ooa_queue)) { 13578 mtx_unlock(&lun->lun_lock); 13579 mtx_lock(&ctl_softc->ctl_lock); 13580 ctl_free_lun(lun); 13581 mtx_unlock(&ctl_softc->ctl_lock); 13582 } else 13583 mtx_unlock(&lun->lun_lock); 13584 13585 /* 13586 * If this command has been aborted, make sure we set the status 13587 * properly. The FETD is responsible for freeing the I/O and doing 13588 * whatever it needs to do to clean up its state. 13589 */ 13590 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13591 ctl_set_task_aborted(&io->scsiio); 13592 13593 /* 13594 * We print out status for every task management command. For SCSI 13595 * commands, we filter out any unit attention errors; they happen 13596 * on every boot, and would clutter up the log. Note: task 13597 * management commands aren't printed here, they are printed above, 13598 * since they should never even make it down here. 13599 */ 13600 switch (io->io_hdr.io_type) { 13601 case CTL_IO_SCSI: { 13602 int error_code, sense_key, asc, ascq; 13603 13604 sense_key = 0; 13605 13606 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13607 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13608 /* 13609 * Since this is just for printing, no need to 13610 * show errors here. 13611 */ 13612 scsi_extract_sense_len(&io->scsiio.sense_data, 13613 io->scsiio.sense_len, 13614 &error_code, 13615 &sense_key, 13616 &asc, 13617 &ascq, 13618 /*show_errors*/ 0); 13619 } 13620 13621 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13622 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13623 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13624 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13625 13626 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13627 ctl_softc->skipped_prints++; 13628 } else { 13629 uint32_t skipped_prints; 13630 13631 skipped_prints = ctl_softc->skipped_prints; 13632 13633 ctl_softc->skipped_prints = 0; 13634 ctl_softc->last_print_jiffies = time_uptime; 13635 13636 if (skipped_prints > 0) { 13637#ifdef NEEDTOPORT 13638 csevent_log(CSC_CTL | CSC_SHELF_SW | 13639 CTL_ERROR_REPORT, 13640 csevent_LogType_Trace, 13641 csevent_Severity_Information, 13642 csevent_AlertLevel_Green, 13643 csevent_FRU_Firmware, 13644 csevent_FRU_Unknown, 13645 "High CTL error volume, %d prints " 13646 "skipped", skipped_prints); 13647#endif 13648 } 13649 if (bootverbose || verbose > 0) 13650 ctl_io_error_print(io, NULL); 13651 } 13652 } 13653 break; 13654 } 13655 case CTL_IO_TASK: 13656 if (bootverbose || verbose > 0) 13657 ctl_io_error_print(io, NULL); 13658 break; 13659 default: 13660 break; 13661 } 13662 13663 /* 13664 * Tell the FETD or the other shelf controller we're done with this 13665 * command. Note that only SCSI commands get to this point. Task 13666 * management commands are completed above. 13667 * 13668 * We only send status to the other controller if we're in XFER 13669 * mode. In SER_ONLY mode, the I/O is done on the controller that 13670 * received the I/O (from CTL's perspective), and so the status is 13671 * generated there. 13672 * 13673 * XXX KDM if we hold the lock here, we could cause a deadlock 13674 * if the frontend comes back in in this context to queue 13675 * something. 13676 */ 13677 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13678 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13679 union ctl_ha_msg msg; 13680 13681 memset(&msg, 0, sizeof(msg)); 13682 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13683 msg.hdr.original_sc = io->io_hdr.original_sc; 13684 msg.hdr.nexus = io->io_hdr.nexus; 13685 msg.hdr.status = io->io_hdr.status; 13686 msg.scsi.scsi_status = io->scsiio.scsi_status; 13687 msg.scsi.tag_num = io->scsiio.tag_num; 13688 msg.scsi.tag_type = io->scsiio.tag_type; 13689 msg.scsi.sense_len = io->scsiio.sense_len; 13690 msg.scsi.sense_residual = io->scsiio.sense_residual; 13691 msg.scsi.residual = io->scsiio.residual; 13692 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13693 sizeof(io->scsiio.sense_data)); 13694 /* 13695 * We copy this whether or not this is an I/O-related 13696 * command. Otherwise, we'd have to go and check to see 13697 * whether it's a read/write command, and it really isn't 13698 * worth it. 13699 */ 13700 memcpy(&msg.scsi.lbalen, 13701 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13702 sizeof(msg.scsi.lbalen)); 13703 13704 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13705 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13706 /* XXX do something here */ 13707 } 13708 13709 ctl_free_io(io); 13710 } else 13711 fe_done(io); 13712 13713bailout: 13714 13715 return (CTL_RETVAL_COMPLETE); 13716} 13717 13718#ifdef CTL_WITH_CA 13719/* 13720 * Front end should call this if it doesn't do autosense. When the request 13721 * sense comes back in from the initiator, we'll dequeue this and send it. 13722 */ 13723int 13724ctl_queue_sense(union ctl_io *io) 13725{ 13726 struct ctl_lun *lun; 13727 struct ctl_softc *ctl_softc; 13728 uint32_t initidx, targ_lun; 13729 13730 ctl_softc = control_softc; 13731 13732 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13733 13734 /* 13735 * LUN lookup will likely move to the ctl_work_thread() once we 13736 * have our new queueing infrastructure (that doesn't put things on 13737 * a per-LUN queue initially). That is so that we can handle 13738 * things like an INQUIRY to a LUN that we don't have enabled. We 13739 * can't deal with that right now. 13740 */ 13741 mtx_lock(&ctl_softc->ctl_lock); 13742 13743 /* 13744 * If we don't have a LUN for this, just toss the sense 13745 * information. 13746 */ 13747 targ_lun = io->io_hdr.nexus.targ_lun; 13748 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13749 if ((targ_lun < CTL_MAX_LUNS) 13750 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13751 lun = ctl_softc->ctl_luns[targ_lun]; 13752 else 13753 goto bailout; 13754 13755 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13756 13757 mtx_lock(&lun->lun_lock); 13758 /* 13759 * Already have CA set for this LUN...toss the sense information. 13760 */ 13761 if (ctl_is_set(lun->have_ca, initidx)) { 13762 mtx_unlock(&lun->lun_lock); 13763 goto bailout; 13764 } 13765 13766 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13767 ctl_min(sizeof(lun->pending_sense[initidx]), 13768 sizeof(io->scsiio.sense_data))); 13769 ctl_set_mask(lun->have_ca, initidx); 13770 mtx_unlock(&lun->lun_lock); 13771 13772bailout: 13773 mtx_unlock(&ctl_softc->ctl_lock); 13774 13775 ctl_free_io(io); 13776 13777 return (CTL_RETVAL_COMPLETE); 13778} 13779#endif 13780 13781/* 13782 * Primary command inlet from frontend ports. All SCSI and task I/O 13783 * requests must go through this function. 13784 */ 13785int 13786ctl_queue(union ctl_io *io) 13787{ 13788 struct ctl_softc *ctl_softc; 13789 13790 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13791 13792 ctl_softc = control_softc; 13793 13794#ifdef CTL_TIME_IO 13795 io->io_hdr.start_time = time_uptime; 13796 getbintime(&io->io_hdr.start_bt); 13797#endif /* CTL_TIME_IO */ 13798 13799 /* Map FE-specific LUN ID into global one. */ 13800 io->io_hdr.nexus.targ_mapped_lun = 13801 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13802 13803 switch (io->io_hdr.io_type) { 13804 case CTL_IO_SCSI: 13805 case CTL_IO_TASK: 13806 ctl_enqueue_incoming(io); 13807 break; 13808 default: 13809 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13810 return (EINVAL); 13811 } 13812 13813 return (CTL_RETVAL_COMPLETE); 13814} 13815 13816#ifdef CTL_IO_DELAY 13817static void 13818ctl_done_timer_wakeup(void *arg) 13819{ 13820 union ctl_io *io; 13821 13822 io = (union ctl_io *)arg; 13823 ctl_done(io); 13824} 13825#endif /* CTL_IO_DELAY */ 13826 13827void 13828ctl_done(union ctl_io *io) 13829{ 13830 struct ctl_softc *ctl_softc; 13831 13832 ctl_softc = control_softc; 13833 13834 /* 13835 * Enable this to catch duplicate completion issues. 13836 */ 13837#if 0 13838 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13839 printf("%s: type %d msg %d cdb %x iptl: " 13840 "%d:%d:%d:%d tag 0x%04x " 13841 "flag %#x status %x\n", 13842 __func__, 13843 io->io_hdr.io_type, 13844 io->io_hdr.msg_type, 13845 io->scsiio.cdb[0], 13846 io->io_hdr.nexus.initid.id, 13847 io->io_hdr.nexus.targ_port, 13848 io->io_hdr.nexus.targ_target.id, 13849 io->io_hdr.nexus.targ_lun, 13850 (io->io_hdr.io_type == 13851 CTL_IO_TASK) ? 13852 io->taskio.tag_num : 13853 io->scsiio.tag_num, 13854 io->io_hdr.flags, 13855 io->io_hdr.status); 13856 } else 13857 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13858#endif 13859 13860 /* 13861 * This is an internal copy of an I/O, and should not go through 13862 * the normal done processing logic. 13863 */ 13864 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13865 return; 13866 13867 /* 13868 * We need to send a msg to the serializing shelf to finish the IO 13869 * as well. We don't send a finish message to the other shelf if 13870 * this is a task management command. Task management commands 13871 * aren't serialized in the OOA queue, but rather just executed on 13872 * both shelf controllers for commands that originated on that 13873 * controller. 13874 */ 13875 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13876 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13877 union ctl_ha_msg msg_io; 13878 13879 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13880 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13881 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13882 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13883 } 13884 /* continue on to finish IO */ 13885 } 13886#ifdef CTL_IO_DELAY 13887 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13888 struct ctl_lun *lun; 13889 13890 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13891 13892 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13893 } else { 13894 struct ctl_lun *lun; 13895 13896 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13897 13898 if ((lun != NULL) 13899 && (lun->delay_info.done_delay > 0)) { 13900 struct callout *callout; 13901 13902 callout = (struct callout *)&io->io_hdr.timer_bytes; 13903 callout_init(callout, /*mpsafe*/ 1); 13904 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13905 callout_reset(callout, 13906 lun->delay_info.done_delay * hz, 13907 ctl_done_timer_wakeup, io); 13908 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13909 lun->delay_info.done_delay = 0; 13910 return; 13911 } 13912 } 13913#endif /* CTL_IO_DELAY */ 13914 13915 ctl_enqueue_done(io); 13916} 13917 13918int 13919ctl_isc(struct ctl_scsiio *ctsio) 13920{ 13921 struct ctl_lun *lun; 13922 int retval; 13923 13924 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13925 13926 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13927 13928 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13929 13930 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13931 13932 return (retval); 13933} 13934 13935 13936static void 13937ctl_work_thread(void *arg) 13938{ 13939 struct ctl_thread *thr = (struct ctl_thread *)arg; 13940 struct ctl_softc *softc = thr->ctl_softc; 13941 union ctl_io *io; 13942 int retval; 13943 13944 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13945 13946 for (;;) { 13947 retval = 0; 13948 13949 /* 13950 * We handle the queues in this order: 13951 * - ISC 13952 * - done queue (to free up resources, unblock other commands) 13953 * - RtR queue 13954 * - incoming queue 13955 * 13956 * If those queues are empty, we break out of the loop and 13957 * go to sleep. 13958 */ 13959 mtx_lock(&thr->queue_lock); 13960 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13961 if (io != NULL) { 13962 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13963 mtx_unlock(&thr->queue_lock); 13964 ctl_handle_isc(io); 13965 continue; 13966 } 13967 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13968 if (io != NULL) { 13969 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13970 /* clear any blocked commands, call fe_done */ 13971 mtx_unlock(&thr->queue_lock); 13972 retval = ctl_process_done(io); 13973 continue; 13974 } 13975 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13976 if (io != NULL) { 13977 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13978 mtx_unlock(&thr->queue_lock); 13979 if (io->io_hdr.io_type == CTL_IO_TASK) 13980 ctl_run_task(io); 13981 else 13982 ctl_scsiio_precheck(softc, &io->scsiio); 13983 continue; 13984 } 13985 if (!ctl_pause_rtr) { 13986 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13987 if (io != NULL) { 13988 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13989 mtx_unlock(&thr->queue_lock); 13990 retval = ctl_scsiio(&io->scsiio); 13991 if (retval != CTL_RETVAL_COMPLETE) 13992 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13993 continue; 13994 } 13995 } 13996 13997 /* Sleep until we have something to do. */ 13998 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13999 } 14000} 14001 14002static void 14003ctl_lun_thread(void *arg) 14004{ 14005 struct ctl_softc *softc = (struct ctl_softc *)arg; 14006 struct ctl_be_lun *be_lun; 14007 int retval; 14008 14009 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14010 14011 for (;;) { 14012 retval = 0; 14013 mtx_lock(&softc->ctl_lock); 14014 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14015 if (be_lun != NULL) { 14016 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14017 mtx_unlock(&softc->ctl_lock); 14018 ctl_create_lun(be_lun); 14019 continue; 14020 } 14021 14022 /* Sleep until we have something to do. */ 14023 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14024 PDROP | PRIBIO, "-", 0); 14025 } 14026} 14027 14028static void 14029ctl_enqueue_incoming(union ctl_io *io) 14030{ 14031 struct ctl_softc *softc = control_softc; 14032 struct ctl_thread *thr; 14033 u_int idx; 14034 14035 idx = (io->io_hdr.nexus.targ_port * 127 + 14036 io->io_hdr.nexus.initid.id) % worker_threads; 14037 thr = &softc->threads[idx]; 14038 mtx_lock(&thr->queue_lock); 14039 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14040 mtx_unlock(&thr->queue_lock); 14041 wakeup(thr); 14042} 14043 14044static void 14045ctl_enqueue_rtr(union ctl_io *io) 14046{ 14047 struct ctl_softc *softc = control_softc; 14048 struct ctl_thread *thr; 14049 14050 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14051 mtx_lock(&thr->queue_lock); 14052 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14053 mtx_unlock(&thr->queue_lock); 14054 wakeup(thr); 14055} 14056 14057static void 14058ctl_enqueue_done(union ctl_io *io) 14059{ 14060 struct ctl_softc *softc = control_softc; 14061 struct ctl_thread *thr; 14062 14063 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14064 mtx_lock(&thr->queue_lock); 14065 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14066 mtx_unlock(&thr->queue_lock); 14067 wakeup(thr); 14068} 14069 14070static void 14071ctl_enqueue_isc(union ctl_io *io) 14072{ 14073 struct ctl_softc *softc = control_softc; 14074 struct ctl_thread *thr; 14075 14076 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14077 mtx_lock(&thr->queue_lock); 14078 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14079 mtx_unlock(&thr->queue_lock); 14080 wakeup(thr); 14081} 14082 14083/* Initialization and failover */ 14084 14085void 14086ctl_init_isc_msg(void) 14087{ 14088 printf("CTL: Still calling this thing\n"); 14089} 14090 14091/* 14092 * Init component 14093 * Initializes component into configuration defined by bootMode 14094 * (see hasc-sv.c) 14095 * returns hasc_Status: 14096 * OK 14097 * ERROR - fatal error 14098 */ 14099static ctl_ha_comp_status 14100ctl_isc_init(struct ctl_ha_component *c) 14101{ 14102 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14103 14104 c->status = ret; 14105 return ret; 14106} 14107 14108/* Start component 14109 * Starts component in state requested. If component starts successfully, 14110 * it must set its own state to the requestrd state 14111 * When requested state is HASC_STATE_HA, the component may refine it 14112 * by adding _SLAVE or _MASTER flags. 14113 * Currently allowed state transitions are: 14114 * UNKNOWN->HA - initial startup 14115 * UNKNOWN->SINGLE - initial startup when no parter detected 14116 * HA->SINGLE - failover 14117 * returns ctl_ha_comp_status: 14118 * OK - component successfully started in requested state 14119 * FAILED - could not start the requested state, failover may 14120 * be possible 14121 * ERROR - fatal error detected, no future startup possible 14122 */ 14123static ctl_ha_comp_status 14124ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14125{ 14126 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14127 14128 printf("%s: go\n", __func__); 14129 14130 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14131 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14132 ctl_is_single = 0; 14133 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14134 != CTL_HA_STATUS_SUCCESS) { 14135 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14136 ret = CTL_HA_COMP_STATUS_ERROR; 14137 } 14138 } else if (CTL_HA_STATE_IS_HA(c->state) 14139 && CTL_HA_STATE_IS_SINGLE(state)){ 14140 // HA->SINGLE transition 14141 ctl_failover(); 14142 ctl_is_single = 1; 14143 } else { 14144 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14145 c->state, state); 14146 ret = CTL_HA_COMP_STATUS_ERROR; 14147 } 14148 if (CTL_HA_STATE_IS_SINGLE(state)) 14149 ctl_is_single = 1; 14150 14151 c->state = state; 14152 c->status = ret; 14153 return ret; 14154} 14155 14156/* 14157 * Quiesce component 14158 * The component must clear any error conditions (set status to OK) and 14159 * prepare itself to another Start call 14160 * returns ctl_ha_comp_status: 14161 * OK 14162 * ERROR 14163 */ 14164static ctl_ha_comp_status 14165ctl_isc_quiesce(struct ctl_ha_component *c) 14166{ 14167 int ret = CTL_HA_COMP_STATUS_OK; 14168 14169 ctl_pause_rtr = 1; 14170 c->status = ret; 14171 return ret; 14172} 14173 14174struct ctl_ha_component ctl_ha_component_ctlisc = 14175{ 14176 .name = "CTL ISC", 14177 .state = CTL_HA_STATE_UNKNOWN, 14178 .init = ctl_isc_init, 14179 .start = ctl_isc_start, 14180 .quiesce = ctl_isc_quiesce 14181}; 14182 14183/* 14184 * vim: ts=8 14185 */ 14186