ctl.c revision 270106
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 270106 2014-08-17 18:22:42Z 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) and 326 * Logical Block Provisioning (0xB2) 327 */ 328#define SCSI_EVPD_NUM_SUPPORTED_PAGES 7 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_lbp(struct ctl_scsiio *ctsio, int alloc_len); 389static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 390static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 391static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 392static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 393static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 394 union ctl_io *ooa_io); 395static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 396 union ctl_io *starting_io); 397static int ctl_check_blocked(struct ctl_lun *lun); 398static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 399 struct ctl_lun *lun, 400 const struct ctl_cmd_entry *entry, 401 struct ctl_scsiio *ctsio); 402//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 403static void ctl_failover(void); 404static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 405 struct ctl_scsiio *ctsio); 406static int ctl_scsiio(struct ctl_scsiio *ctsio); 407 408static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 409static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 410 ctl_ua_type ua_type); 411static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 412 ctl_ua_type ua_type); 413static int ctl_abort_task(union ctl_io *io); 414static int ctl_abort_task_set(union ctl_io *io); 415static int ctl_i_t_nexus_reset(union ctl_io *io); 416static void ctl_run_task(union ctl_io *io); 417#ifdef CTL_IO_DELAY 418static void ctl_datamove_timer_wakeup(void *arg); 419static void ctl_done_timer_wakeup(void *arg); 420#endif /* CTL_IO_DELAY */ 421 422static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 423static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 424static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 425static void ctl_datamove_remote_write(union ctl_io *io); 426static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 427static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 428static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 429static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 430 ctl_ha_dt_cb callback); 431static void ctl_datamove_remote_read(union ctl_io *io); 432static void ctl_datamove_remote(union ctl_io *io); 433static int ctl_process_done(union ctl_io *io); 434static void ctl_lun_thread(void *arg); 435static void ctl_work_thread(void *arg); 436static void ctl_enqueue_incoming(union ctl_io *io); 437static void ctl_enqueue_rtr(union ctl_io *io); 438static void ctl_enqueue_done(union ctl_io *io); 439static void ctl_enqueue_isc(union ctl_io *io); 440static const struct ctl_cmd_entry * 441 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 442static const struct ctl_cmd_entry * 443 ctl_validate_command(struct ctl_scsiio *ctsio); 444static int ctl_cmd_applicable(uint8_t lun_type, 445 const struct ctl_cmd_entry *entry); 446 447/* 448 * Load the serialization table. This isn't very pretty, but is probably 449 * the easiest way to do it. 450 */ 451#include "ctl_ser_table.c" 452 453/* 454 * We only need to define open, close and ioctl routines for this driver. 455 */ 456static struct cdevsw ctl_cdevsw = { 457 .d_version = D_VERSION, 458 .d_flags = 0, 459 .d_open = ctl_open, 460 .d_close = ctl_close, 461 .d_ioctl = ctl_ioctl, 462 .d_name = "ctl", 463}; 464 465 466MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 467MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 468 469static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 470 471static moduledata_t ctl_moduledata = { 472 "ctl", 473 ctl_module_event_handler, 474 NULL 475}; 476 477DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 478MODULE_VERSION(ctl, 1); 479 480static struct ctl_frontend ioctl_frontend = 481{ 482 .name = "ioctl", 483}; 484 485static void 486ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 487 union ctl_ha_msg *msg_info) 488{ 489 struct ctl_scsiio *ctsio; 490 491 if (msg_info->hdr.original_sc == NULL) { 492 printf("%s: original_sc == NULL!\n", __func__); 493 /* XXX KDM now what? */ 494 return; 495 } 496 497 ctsio = &msg_info->hdr.original_sc->scsiio; 498 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 499 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 500 ctsio->io_hdr.status = msg_info->hdr.status; 501 ctsio->scsi_status = msg_info->scsi.scsi_status; 502 ctsio->sense_len = msg_info->scsi.sense_len; 503 ctsio->sense_residual = msg_info->scsi.sense_residual; 504 ctsio->residual = msg_info->scsi.residual; 505 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 506 sizeof(ctsio->sense_data)); 507 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 508 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 509 ctl_enqueue_isc((union ctl_io *)ctsio); 510} 511 512static void 513ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 514 union ctl_ha_msg *msg_info) 515{ 516 struct ctl_scsiio *ctsio; 517 518 if (msg_info->hdr.serializing_sc == NULL) { 519 printf("%s: serializing_sc == NULL!\n", __func__); 520 /* XXX KDM now what? */ 521 return; 522 } 523 524 ctsio = &msg_info->hdr.serializing_sc->scsiio; 525#if 0 526 /* 527 * Attempt to catch the situation where an I/O has 528 * been freed, and we're using it again. 529 */ 530 if (ctsio->io_hdr.io_type == 0xff) { 531 union ctl_io *tmp_io; 532 tmp_io = (union ctl_io *)ctsio; 533 printf("%s: %p use after free!\n", __func__, 534 ctsio); 535 printf("%s: type %d msg %d cdb %x iptl: " 536 "%d:%d:%d:%d tag 0x%04x " 537 "flag %#x status %x\n", 538 __func__, 539 tmp_io->io_hdr.io_type, 540 tmp_io->io_hdr.msg_type, 541 tmp_io->scsiio.cdb[0], 542 tmp_io->io_hdr.nexus.initid.id, 543 tmp_io->io_hdr.nexus.targ_port, 544 tmp_io->io_hdr.nexus.targ_target.id, 545 tmp_io->io_hdr.nexus.targ_lun, 546 (tmp_io->io_hdr.io_type == 547 CTL_IO_TASK) ? 548 tmp_io->taskio.tag_num : 549 tmp_io->scsiio.tag_num, 550 tmp_io->io_hdr.flags, 551 tmp_io->io_hdr.status); 552 } 553#endif 554 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 555 ctl_enqueue_isc((union ctl_io *)ctsio); 556} 557 558/* 559 * ISC (Inter Shelf Communication) event handler. Events from the HA 560 * subsystem come in here. 561 */ 562static void 563ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 564{ 565 struct ctl_softc *ctl_softc; 566 union ctl_io *io; 567 struct ctl_prio *presio; 568 ctl_ha_status isc_status; 569 570 ctl_softc = control_softc; 571 io = NULL; 572 573 574#if 0 575 printf("CTL: Isc Msg event %d\n", event); 576#endif 577 if (event == CTL_HA_EVT_MSG_RECV) { 578 union ctl_ha_msg msg_info; 579 580 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 581 sizeof(msg_info), /*wait*/ 0); 582#if 0 583 printf("CTL: msg_type %d\n", msg_info.msg_type); 584#endif 585 if (isc_status != 0) { 586 printf("Error receiving message, status = %d\n", 587 isc_status); 588 return; 589 } 590 591 switch (msg_info.hdr.msg_type) { 592 case CTL_MSG_SERIALIZE: 593#if 0 594 printf("Serialize\n"); 595#endif 596 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 597 if (io == NULL) { 598 printf("ctl_isc_event_handler: can't allocate " 599 "ctl_io!\n"); 600 /* Bad Juju */ 601 /* Need to set busy and send msg back */ 602 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 603 msg_info.hdr.status = CTL_SCSI_ERROR; 604 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 605 msg_info.scsi.sense_len = 0; 606 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 607 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 608 } 609 goto bailout; 610 } 611 ctl_zero_io(io); 612 // populate ctsio from msg_info 613 io->io_hdr.io_type = CTL_IO_SCSI; 614 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 615 io->io_hdr.original_sc = msg_info.hdr.original_sc; 616#if 0 617 printf("pOrig %x\n", (int)msg_info.original_sc); 618#endif 619 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 620 CTL_FLAG_IO_ACTIVE; 621 /* 622 * If we're in serialization-only mode, we don't 623 * want to go through full done processing. Thus 624 * the COPY flag. 625 * 626 * XXX KDM add another flag that is more specific. 627 */ 628 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 629 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 630 io->io_hdr.nexus = msg_info.hdr.nexus; 631#if 0 632 printf("targ %d, port %d, iid %d, lun %d\n", 633 io->io_hdr.nexus.targ_target.id, 634 io->io_hdr.nexus.targ_port, 635 io->io_hdr.nexus.initid.id, 636 io->io_hdr.nexus.targ_lun); 637#endif 638 io->scsiio.tag_num = msg_info.scsi.tag_num; 639 io->scsiio.tag_type = msg_info.scsi.tag_type; 640 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 641 CTL_MAX_CDBLEN); 642 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 643 const struct ctl_cmd_entry *entry; 644 645 entry = ctl_get_cmd_entry(&io->scsiio); 646 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 647 io->io_hdr.flags |= 648 entry->flags & CTL_FLAG_DATA_MASK; 649 } 650 ctl_enqueue_isc(io); 651 break; 652 653 /* Performed on the Originating SC, XFER mode only */ 654 case CTL_MSG_DATAMOVE: { 655 struct ctl_sg_entry *sgl; 656 int i, j; 657 658 io = msg_info.hdr.original_sc; 659 if (io == NULL) { 660 printf("%s: original_sc == NULL!\n", __func__); 661 /* XXX KDM do something here */ 662 break; 663 } 664 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 665 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 666 /* 667 * Keep track of this, we need to send it back over 668 * when the datamove is complete. 669 */ 670 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 671 672 if (msg_info.dt.sg_sequence == 0) { 673 /* 674 * XXX KDM we use the preallocated S/G list 675 * here, but we'll need to change this to 676 * dynamic allocation if we need larger S/G 677 * lists. 678 */ 679 if (msg_info.dt.kern_sg_entries > 680 sizeof(io->io_hdr.remote_sglist) / 681 sizeof(io->io_hdr.remote_sglist[0])) { 682 printf("%s: number of S/G entries " 683 "needed %u > allocated num %zd\n", 684 __func__, 685 msg_info.dt.kern_sg_entries, 686 sizeof(io->io_hdr.remote_sglist)/ 687 sizeof(io->io_hdr.remote_sglist[0])); 688 689 /* 690 * XXX KDM send a message back to 691 * the other side to shut down the 692 * DMA. The error will come back 693 * through via the normal channel. 694 */ 695 break; 696 } 697 sgl = io->io_hdr.remote_sglist; 698 memset(sgl, 0, 699 sizeof(io->io_hdr.remote_sglist)); 700 701 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 702 703 io->scsiio.kern_sg_entries = 704 msg_info.dt.kern_sg_entries; 705 io->scsiio.rem_sg_entries = 706 msg_info.dt.kern_sg_entries; 707 io->scsiio.kern_data_len = 708 msg_info.dt.kern_data_len; 709 io->scsiio.kern_total_len = 710 msg_info.dt.kern_total_len; 711 io->scsiio.kern_data_resid = 712 msg_info.dt.kern_data_resid; 713 io->scsiio.kern_rel_offset = 714 msg_info.dt.kern_rel_offset; 715 /* 716 * Clear out per-DMA flags. 717 */ 718 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 719 /* 720 * Add per-DMA flags that are set for this 721 * particular DMA request. 722 */ 723 io->io_hdr.flags |= msg_info.dt.flags & 724 CTL_FLAG_RDMA_MASK; 725 } else 726 sgl = (struct ctl_sg_entry *) 727 io->scsiio.kern_data_ptr; 728 729 for (i = msg_info.dt.sent_sg_entries, j = 0; 730 i < (msg_info.dt.sent_sg_entries + 731 msg_info.dt.cur_sg_entries); i++, j++) { 732 sgl[i].addr = msg_info.dt.sg_list[j].addr; 733 sgl[i].len = msg_info.dt.sg_list[j].len; 734 735#if 0 736 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 737 __func__, 738 msg_info.dt.sg_list[j].addr, 739 msg_info.dt.sg_list[j].len, 740 sgl[i].addr, sgl[i].len, j, i); 741#endif 742 } 743#if 0 744 memcpy(&sgl[msg_info.dt.sent_sg_entries], 745 msg_info.dt.sg_list, 746 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 747#endif 748 749 /* 750 * If this is the last piece of the I/O, we've got 751 * the full S/G list. Queue processing in the thread. 752 * Otherwise wait for the next piece. 753 */ 754 if (msg_info.dt.sg_last != 0) 755 ctl_enqueue_isc(io); 756 break; 757 } 758 /* Performed on the Serializing (primary) SC, XFER mode only */ 759 case CTL_MSG_DATAMOVE_DONE: { 760 if (msg_info.hdr.serializing_sc == NULL) { 761 printf("%s: serializing_sc == NULL!\n", 762 __func__); 763 /* XXX KDM now what? */ 764 break; 765 } 766 /* 767 * We grab the sense information here in case 768 * there was a failure, so we can return status 769 * back to the initiator. 770 */ 771 io = msg_info.hdr.serializing_sc; 772 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 773 io->io_hdr.status = msg_info.hdr.status; 774 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 775 io->scsiio.sense_len = msg_info.scsi.sense_len; 776 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 777 io->io_hdr.port_status = msg_info.scsi.fetd_status; 778 io->scsiio.residual = msg_info.scsi.residual; 779 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 780 sizeof(io->scsiio.sense_data)); 781 ctl_enqueue_isc(io); 782 break; 783 } 784 785 /* Preformed on Originating SC, SER_ONLY mode */ 786 case CTL_MSG_R2R: 787 io = msg_info.hdr.original_sc; 788 if (io == NULL) { 789 printf("%s: Major Bummer\n", __func__); 790 return; 791 } else { 792#if 0 793 printf("pOrig %x\n",(int) ctsio); 794#endif 795 } 796 io->io_hdr.msg_type = CTL_MSG_R2R; 797 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 798 ctl_enqueue_isc(io); 799 break; 800 801 /* 802 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 803 * mode. 804 * Performed on the Originating (i.e. secondary) SC in XFER 805 * mode 806 */ 807 case CTL_MSG_FINISH_IO: 808 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 809 ctl_isc_handler_finish_xfer(ctl_softc, 810 &msg_info); 811 else 812 ctl_isc_handler_finish_ser_only(ctl_softc, 813 &msg_info); 814 break; 815 816 /* Preformed on Originating SC */ 817 case CTL_MSG_BAD_JUJU: 818 io = msg_info.hdr.original_sc; 819 if (io == NULL) { 820 printf("%s: Bad JUJU!, original_sc is NULL!\n", 821 __func__); 822 break; 823 } 824 ctl_copy_sense_data(&msg_info, io); 825 /* 826 * IO should have already been cleaned up on other 827 * SC so clear this flag so we won't send a message 828 * back to finish the IO there. 829 */ 830 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 831 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 832 833 /* io = msg_info.hdr.serializing_sc; */ 834 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 835 ctl_enqueue_isc(io); 836 break; 837 838 /* Handle resets sent from the other side */ 839 case CTL_MSG_MANAGE_TASKS: { 840 struct ctl_taskio *taskio; 841 taskio = (struct ctl_taskio *)ctl_alloc_io( 842 (void *)ctl_softc->othersc_pool); 843 if (taskio == NULL) { 844 printf("ctl_isc_event_handler: can't allocate " 845 "ctl_io!\n"); 846 /* Bad Juju */ 847 /* should I just call the proper reset func 848 here??? */ 849 goto bailout; 850 } 851 ctl_zero_io((union ctl_io *)taskio); 852 taskio->io_hdr.io_type = CTL_IO_TASK; 853 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 854 taskio->io_hdr.nexus = msg_info.hdr.nexus; 855 taskio->task_action = msg_info.task.task_action; 856 taskio->tag_num = msg_info.task.tag_num; 857 taskio->tag_type = msg_info.task.tag_type; 858#ifdef CTL_TIME_IO 859 taskio->io_hdr.start_time = time_uptime; 860 getbintime(&taskio->io_hdr.start_bt); 861#if 0 862 cs_prof_gettime(&taskio->io_hdr.start_ticks); 863#endif 864#endif /* CTL_TIME_IO */ 865 ctl_run_task((union ctl_io *)taskio); 866 break; 867 } 868 /* Persistent Reserve action which needs attention */ 869 case CTL_MSG_PERS_ACTION: 870 presio = (struct ctl_prio *)ctl_alloc_io( 871 (void *)ctl_softc->othersc_pool); 872 if (presio == NULL) { 873 printf("ctl_isc_event_handler: can't allocate " 874 "ctl_io!\n"); 875 /* Bad Juju */ 876 /* Need to set busy and send msg back */ 877 goto bailout; 878 } 879 ctl_zero_io((union ctl_io *)presio); 880 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 881 presio->pr_msg = msg_info.pr; 882 ctl_enqueue_isc((union ctl_io *)presio); 883 break; 884 case CTL_MSG_SYNC_FE: 885 rcv_sync_msg = 1; 886 break; 887 case CTL_MSG_APS_LOCK: { 888 // It's quicker to execute this then to 889 // queue it. 890 struct ctl_lun *lun; 891 struct ctl_page_index *page_index; 892 struct copan_aps_subpage *current_sp; 893 uint32_t targ_lun; 894 895 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 896 lun = ctl_softc->ctl_luns[targ_lun]; 897 mtx_lock(&lun->lun_lock); 898 page_index = &lun->mode_pages.index[index_to_aps_page]; 899 current_sp = (struct copan_aps_subpage *) 900 (page_index->page_data + 901 (page_index->page_len * CTL_PAGE_CURRENT)); 902 903 current_sp->lock_active = msg_info.aps.lock_flag; 904 mtx_unlock(&lun->lun_lock); 905 break; 906 } 907 default: 908 printf("How did I get here?\n"); 909 } 910 } else if (event == CTL_HA_EVT_MSG_SENT) { 911 if (param != CTL_HA_STATUS_SUCCESS) { 912 printf("Bad status from ctl_ha_msg_send status %d\n", 913 param); 914 } 915 return; 916 } else if (event == CTL_HA_EVT_DISCONNECT) { 917 printf("CTL: Got a disconnect from Isc\n"); 918 return; 919 } else { 920 printf("ctl_isc_event_handler: Unknown event %d\n", event); 921 return; 922 } 923 924bailout: 925 return; 926} 927 928static void 929ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 930{ 931 struct scsi_sense_data *sense; 932 933 sense = &dest->scsiio.sense_data; 934 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 935 dest->scsiio.scsi_status = src->scsi.scsi_status; 936 dest->scsiio.sense_len = src->scsi.sense_len; 937 dest->io_hdr.status = src->hdr.status; 938} 939 940static int 941ctl_init(void) 942{ 943 struct ctl_softc *softc; 944 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 945 struct ctl_port *port; 946 uint8_t sc_id =0; 947 int i, error, retval; 948 //int isc_retval; 949 950 retval = 0; 951 ctl_pause_rtr = 0; 952 rcv_sync_msg = 0; 953 954 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 955 M_WAITOK | M_ZERO); 956 softc = control_softc; 957 958 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 959 "cam/ctl"); 960 961 softc->dev->si_drv1 = softc; 962 963 /* 964 * By default, return a "bad LUN" peripheral qualifier for unknown 965 * LUNs. The user can override this default using the tunable or 966 * sysctl. See the comment in ctl_inquiry_std() for more details. 967 */ 968 softc->inquiry_pq_no_lun = 1; 969 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 970 &softc->inquiry_pq_no_lun); 971 sysctl_ctx_init(&softc->sysctl_ctx); 972 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 973 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 974 CTLFLAG_RD, 0, "CAM Target Layer"); 975 976 if (softc->sysctl_tree == NULL) { 977 printf("%s: unable to allocate sysctl tree\n", __func__); 978 destroy_dev(softc->dev); 979 free(control_softc, M_DEVBUF); 980 control_softc = NULL; 981 return (ENOMEM); 982 } 983 984 SYSCTL_ADD_INT(&softc->sysctl_ctx, 985 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 986 "inquiry_pq_no_lun", CTLFLAG_RW, 987 &softc->inquiry_pq_no_lun, 0, 988 "Report no lun possible for invalid LUNs"); 989 990 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 991 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 992 softc->open_count = 0; 993 994 /* 995 * Default to actually sending a SYNCHRONIZE CACHE command down to 996 * the drive. 997 */ 998 softc->flags = CTL_FLAG_REAL_SYNC; 999 1000 /* 1001 * In Copan's HA scheme, the "master" and "slave" roles are 1002 * figured out through the slot the controller is in. Although it 1003 * is an active/active system, someone has to be in charge. 1004 */ 1005#ifdef NEEDTOPORT 1006 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1007#endif 1008 1009 if (sc_id == 0) { 1010 softc->flags |= CTL_FLAG_MASTER_SHELF; 1011 persis_offset = 0; 1012 } else 1013 persis_offset = CTL_MAX_INITIATORS; 1014 1015 /* 1016 * XXX KDM need to figure out where we want to get our target ID 1017 * and WWID. Is it different on each port? 1018 */ 1019 softc->target.id = 0; 1020 softc->target.wwid[0] = 0x12345678; 1021 softc->target.wwid[1] = 0x87654321; 1022 STAILQ_INIT(&softc->lun_list); 1023 STAILQ_INIT(&softc->pending_lun_queue); 1024 STAILQ_INIT(&softc->fe_list); 1025 STAILQ_INIT(&softc->port_list); 1026 STAILQ_INIT(&softc->be_list); 1027 STAILQ_INIT(&softc->io_pools); 1028 ctl_tpc_init(softc); 1029 1030 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1031 &internal_pool)!= 0){ 1032 printf("ctl: can't allocate %d entry internal pool, " 1033 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1034 return (ENOMEM); 1035 } 1036 1037 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1038 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1039 printf("ctl: can't allocate %d entry emergency pool, " 1040 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1041 ctl_pool_free(internal_pool); 1042 return (ENOMEM); 1043 } 1044 1045 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1046 &other_pool) != 0) 1047 { 1048 printf("ctl: can't allocate %d entry other SC pool, " 1049 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1050 ctl_pool_free(internal_pool); 1051 ctl_pool_free(emergency_pool); 1052 return (ENOMEM); 1053 } 1054 1055 softc->internal_pool = internal_pool; 1056 softc->emergency_pool = emergency_pool; 1057 softc->othersc_pool = other_pool; 1058 1059 if (worker_threads <= 0) 1060 worker_threads = max(1, mp_ncpus / 4); 1061 if (worker_threads > CTL_MAX_THREADS) 1062 worker_threads = CTL_MAX_THREADS; 1063 1064 for (i = 0; i < worker_threads; i++) { 1065 struct ctl_thread *thr = &softc->threads[i]; 1066 1067 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1068 thr->ctl_softc = softc; 1069 STAILQ_INIT(&thr->incoming_queue); 1070 STAILQ_INIT(&thr->rtr_queue); 1071 STAILQ_INIT(&thr->done_queue); 1072 STAILQ_INIT(&thr->isc_queue); 1073 1074 error = kproc_kthread_add(ctl_work_thread, thr, 1075 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1076 if (error != 0) { 1077 printf("error creating CTL work thread!\n"); 1078 ctl_pool_free(internal_pool); 1079 ctl_pool_free(emergency_pool); 1080 ctl_pool_free(other_pool); 1081 return (error); 1082 } 1083 } 1084 error = kproc_kthread_add(ctl_lun_thread, softc, 1085 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1086 if (error != 0) { 1087 printf("error creating CTL lun thread!\n"); 1088 ctl_pool_free(internal_pool); 1089 ctl_pool_free(emergency_pool); 1090 ctl_pool_free(other_pool); 1091 return (error); 1092 } 1093 if (bootverbose) 1094 printf("ctl: CAM Target Layer loaded\n"); 1095 1096 /* 1097 * Initialize the ioctl front end. 1098 */ 1099 ctl_frontend_register(&ioctl_frontend); 1100 port = &softc->ioctl_info.port; 1101 port->frontend = &ioctl_frontend; 1102 sprintf(softc->ioctl_info.port_name, "ioctl"); 1103 port->port_type = CTL_PORT_IOCTL; 1104 port->num_requested_ctl_io = 100; 1105 port->port_name = softc->ioctl_info.port_name; 1106 port->port_online = ctl_ioctl_online; 1107 port->port_offline = ctl_ioctl_offline; 1108 port->onoff_arg = &softc->ioctl_info; 1109 port->lun_enable = ctl_ioctl_lun_enable; 1110 port->lun_disable = ctl_ioctl_lun_disable; 1111 port->targ_lun_arg = &softc->ioctl_info; 1112 port->fe_datamove = ctl_ioctl_datamove; 1113 port->fe_done = ctl_ioctl_done; 1114 port->max_targets = 15; 1115 port->max_target_id = 15; 1116 1117 if (ctl_port_register(&softc->ioctl_info.port, 1118 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1119 printf("ctl: ioctl front end registration failed, will " 1120 "continue anyway\n"); 1121 } 1122 1123#ifdef CTL_IO_DELAY 1124 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1125 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1126 sizeof(struct callout), CTL_TIMER_BYTES); 1127 return (EINVAL); 1128 } 1129#endif /* CTL_IO_DELAY */ 1130 1131 return (0); 1132} 1133 1134void 1135ctl_shutdown(void) 1136{ 1137 struct ctl_softc *softc; 1138 struct ctl_lun *lun, *next_lun; 1139 struct ctl_io_pool *pool; 1140 1141 softc = (struct ctl_softc *)control_softc; 1142 1143 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1144 printf("ctl: ioctl front end deregistration failed\n"); 1145 1146 mtx_lock(&softc->ctl_lock); 1147 1148 /* 1149 * Free up each LUN. 1150 */ 1151 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1152 next_lun = STAILQ_NEXT(lun, links); 1153 ctl_free_lun(lun); 1154 } 1155 1156 mtx_unlock(&softc->ctl_lock); 1157 1158 ctl_frontend_deregister(&ioctl_frontend); 1159 1160 /* 1161 * This will rip the rug out from under any FETDs or anyone else 1162 * that has a pool allocated. Since we increment our module 1163 * refcount any time someone outside the main CTL module allocates 1164 * a pool, we shouldn't have any problems here. The user won't be 1165 * able to unload the CTL module until client modules have 1166 * successfully unloaded. 1167 */ 1168 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1169 ctl_pool_free(pool); 1170 1171#if 0 1172 ctl_shutdown_thread(softc->work_thread); 1173 mtx_destroy(&softc->queue_lock); 1174#endif 1175 1176 ctl_tpc_shutdown(softc); 1177 mtx_destroy(&softc->pool_lock); 1178 mtx_destroy(&softc->ctl_lock); 1179 1180 destroy_dev(softc->dev); 1181 1182 sysctl_ctx_free(&softc->sysctl_ctx); 1183 1184 free(control_softc, M_DEVBUF); 1185 control_softc = NULL; 1186 1187 if (bootverbose) 1188 printf("ctl: CAM Target Layer unloaded\n"); 1189} 1190 1191static int 1192ctl_module_event_handler(module_t mod, int what, void *arg) 1193{ 1194 1195 switch (what) { 1196 case MOD_LOAD: 1197 return (ctl_init()); 1198 case MOD_UNLOAD: 1199 return (EBUSY); 1200 default: 1201 return (EOPNOTSUPP); 1202 } 1203} 1204 1205/* 1206 * XXX KDM should we do some access checks here? Bump a reference count to 1207 * prevent a CTL module from being unloaded while someone has it open? 1208 */ 1209static int 1210ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1211{ 1212 return (0); 1213} 1214 1215static int 1216ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1217{ 1218 return (0); 1219} 1220 1221int 1222ctl_port_enable(ctl_port_type port_type) 1223{ 1224 struct ctl_softc *softc; 1225 struct ctl_port *port; 1226 1227 if (ctl_is_single == 0) { 1228 union ctl_ha_msg msg_info; 1229 int isc_retval; 1230 1231#if 0 1232 printf("%s: HA mode, synchronizing frontend enable\n", 1233 __func__); 1234#endif 1235 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1236 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1237 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1238 printf("Sync msg send error retval %d\n", isc_retval); 1239 } 1240 if (!rcv_sync_msg) { 1241 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1242 sizeof(msg_info), 1); 1243 } 1244#if 0 1245 printf("CTL:Frontend Enable\n"); 1246 } else { 1247 printf("%s: single mode, skipping frontend synchronization\n", 1248 __func__); 1249#endif 1250 } 1251 1252 softc = control_softc; 1253 1254 STAILQ_FOREACH(port, &softc->port_list, links) { 1255 if (port_type & port->port_type) 1256 { 1257#if 0 1258 printf("port %d\n", port->targ_port); 1259#endif 1260 ctl_port_online(port); 1261 } 1262 } 1263 1264 return (0); 1265} 1266 1267int 1268ctl_port_disable(ctl_port_type port_type) 1269{ 1270 struct ctl_softc *softc; 1271 struct ctl_port *port; 1272 1273 softc = control_softc; 1274 1275 STAILQ_FOREACH(port, &softc->port_list, links) { 1276 if (port_type & port->port_type) 1277 ctl_port_offline(port); 1278 } 1279 1280 return (0); 1281} 1282 1283/* 1284 * Returns 0 for success, 1 for failure. 1285 * Currently the only failure mode is if there aren't enough entries 1286 * allocated. So, in case of a failure, look at num_entries_dropped, 1287 * reallocate and try again. 1288 */ 1289int 1290ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1291 int *num_entries_filled, int *num_entries_dropped, 1292 ctl_port_type port_type, int no_virtual) 1293{ 1294 struct ctl_softc *softc; 1295 struct ctl_port *port; 1296 int entries_dropped, entries_filled; 1297 int retval; 1298 int i; 1299 1300 softc = control_softc; 1301 1302 retval = 0; 1303 entries_filled = 0; 1304 entries_dropped = 0; 1305 1306 i = 0; 1307 mtx_lock(&softc->ctl_lock); 1308 STAILQ_FOREACH(port, &softc->port_list, links) { 1309 struct ctl_port_entry *entry; 1310 1311 if ((port->port_type & port_type) == 0) 1312 continue; 1313 1314 if ((no_virtual != 0) 1315 && (port->virtual_port != 0)) 1316 continue; 1317 1318 if (entries_filled >= num_entries_alloced) { 1319 entries_dropped++; 1320 continue; 1321 } 1322 entry = &entries[i]; 1323 1324 entry->port_type = port->port_type; 1325 strlcpy(entry->port_name, port->port_name, 1326 sizeof(entry->port_name)); 1327 entry->physical_port = port->physical_port; 1328 entry->virtual_port = port->virtual_port; 1329 entry->wwnn = port->wwnn; 1330 entry->wwpn = port->wwpn; 1331 1332 i++; 1333 entries_filled++; 1334 } 1335 1336 mtx_unlock(&softc->ctl_lock); 1337 1338 if (entries_dropped > 0) 1339 retval = 1; 1340 1341 *num_entries_dropped = entries_dropped; 1342 *num_entries_filled = entries_filled; 1343 1344 return (retval); 1345} 1346 1347static void 1348ctl_ioctl_online(void *arg) 1349{ 1350 struct ctl_ioctl_info *ioctl_info; 1351 1352 ioctl_info = (struct ctl_ioctl_info *)arg; 1353 1354 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1355} 1356 1357static void 1358ctl_ioctl_offline(void *arg) 1359{ 1360 struct ctl_ioctl_info *ioctl_info; 1361 1362 ioctl_info = (struct ctl_ioctl_info *)arg; 1363 1364 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1365} 1366 1367/* 1368 * Remove an initiator by port number and initiator ID. 1369 * Returns 0 for success, -1 for failure. 1370 */ 1371int 1372ctl_remove_initiator(struct ctl_port *port, int iid) 1373{ 1374 struct ctl_softc *softc = control_softc; 1375 1376 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1377 1378 if (iid > CTL_MAX_INIT_PER_PORT) { 1379 printf("%s: initiator ID %u > maximun %u!\n", 1380 __func__, iid, CTL_MAX_INIT_PER_PORT); 1381 return (-1); 1382 } 1383 1384 mtx_lock(&softc->ctl_lock); 1385 port->wwpn_iid[iid].in_use--; 1386 port->wwpn_iid[iid].last_use = time_uptime; 1387 mtx_unlock(&softc->ctl_lock); 1388 1389 return (0); 1390} 1391 1392/* 1393 * Add an initiator to the initiator map. 1394 * Returns iid for success, < 0 for failure. 1395 */ 1396int 1397ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1398{ 1399 struct ctl_softc *softc = control_softc; 1400 time_t best_time; 1401 int i, best; 1402 1403 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1404 1405 if (iid >= CTL_MAX_INIT_PER_PORT) { 1406 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1407 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1408 free(name, M_CTL); 1409 return (-1); 1410 } 1411 1412 mtx_lock(&softc->ctl_lock); 1413 1414 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1415 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1416 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1417 iid = i; 1418 break; 1419 } 1420 if (name != NULL && port->wwpn_iid[i].name != NULL && 1421 strcmp(name, port->wwpn_iid[i].name) == 0) { 1422 iid = i; 1423 break; 1424 } 1425 } 1426 } 1427 1428 if (iid < 0) { 1429 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1430 if (port->wwpn_iid[i].in_use == 0 && 1431 port->wwpn_iid[i].wwpn == 0 && 1432 port->wwpn_iid[i].name == NULL) { 1433 iid = i; 1434 break; 1435 } 1436 } 1437 } 1438 1439 if (iid < 0) { 1440 best = -1; 1441 best_time = INT32_MAX; 1442 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1443 if (port->wwpn_iid[i].in_use == 0) { 1444 if (port->wwpn_iid[i].last_use < best_time) { 1445 best = i; 1446 best_time = port->wwpn_iid[i].last_use; 1447 } 1448 } 1449 } 1450 iid = best; 1451 } 1452 1453 if (iid < 0) { 1454 mtx_unlock(&softc->ctl_lock); 1455 free(name, M_CTL); 1456 return (-2); 1457 } 1458 1459 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1460 /* 1461 * This is not an error yet. 1462 */ 1463 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1464#if 0 1465 printf("%s: port %d iid %u WWPN %#jx arrived" 1466 " again\n", __func__, port->targ_port, 1467 iid, (uintmax_t)wwpn); 1468#endif 1469 goto take; 1470 } 1471 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1472 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1473#if 0 1474 printf("%s: port %d iid %u name '%s' arrived" 1475 " again\n", __func__, port->targ_port, 1476 iid, name); 1477#endif 1478 goto take; 1479 } 1480 1481 /* 1482 * This is an error, but what do we do about it? The 1483 * driver is telling us we have a new WWPN for this 1484 * initiator ID, so we pretty much need to use it. 1485 */ 1486 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1487 " but WWPN %#jx '%s' is still at that address\n", 1488 __func__, port->targ_port, iid, wwpn, name, 1489 (uintmax_t)port->wwpn_iid[iid].wwpn, 1490 port->wwpn_iid[iid].name); 1491 1492 /* 1493 * XXX KDM clear have_ca and ua_pending on each LUN for 1494 * this initiator. 1495 */ 1496 } 1497take: 1498 free(port->wwpn_iid[iid].name, M_CTL); 1499 port->wwpn_iid[iid].name = name; 1500 port->wwpn_iid[iid].wwpn = wwpn; 1501 port->wwpn_iid[iid].in_use++; 1502 mtx_unlock(&softc->ctl_lock); 1503 1504 return (iid); 1505} 1506 1507static int 1508ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1509{ 1510 int len; 1511 1512 switch (port->port_type) { 1513 case CTL_PORT_FC: 1514 { 1515 struct scsi_transportid_fcp *id = 1516 (struct scsi_transportid_fcp *)buf; 1517 if (port->wwpn_iid[iid].wwpn == 0) 1518 return (0); 1519 memset(id, 0, sizeof(*id)); 1520 id->format_protocol = SCSI_PROTO_FC; 1521 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1522 return (sizeof(*id)); 1523 } 1524 case CTL_PORT_ISCSI: 1525 { 1526 struct scsi_transportid_iscsi_port *id = 1527 (struct scsi_transportid_iscsi_port *)buf; 1528 if (port->wwpn_iid[iid].name == NULL) 1529 return (0); 1530 memset(id, 0, 256); 1531 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1532 SCSI_PROTO_ISCSI; 1533 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1534 len = roundup2(min(len, 252), 4); 1535 scsi_ulto2b(len, id->additional_length); 1536 return (sizeof(*id) + len); 1537 } 1538 case CTL_PORT_SAS: 1539 { 1540 struct scsi_transportid_sas *id = 1541 (struct scsi_transportid_sas *)buf; 1542 if (port->wwpn_iid[iid].wwpn == 0) 1543 return (0); 1544 memset(id, 0, sizeof(*id)); 1545 id->format_protocol = SCSI_PROTO_SAS; 1546 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1547 return (sizeof(*id)); 1548 } 1549 default: 1550 { 1551 struct scsi_transportid_spi *id = 1552 (struct scsi_transportid_spi *)buf; 1553 memset(id, 0, sizeof(*id)); 1554 id->format_protocol = SCSI_PROTO_SPI; 1555 scsi_ulto2b(iid, id->scsi_addr); 1556 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1557 return (sizeof(*id)); 1558 } 1559 } 1560} 1561 1562static int 1563ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1564{ 1565 return (0); 1566} 1567 1568static int 1569ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1570{ 1571 return (0); 1572} 1573 1574/* 1575 * Data movement routine for the CTL ioctl frontend port. 1576 */ 1577static int 1578ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1579{ 1580 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1581 struct ctl_sg_entry ext_entry, kern_entry; 1582 int ext_sglen, ext_sg_entries, kern_sg_entries; 1583 int ext_sg_start, ext_offset; 1584 int len_to_copy, len_copied; 1585 int kern_watermark, ext_watermark; 1586 int ext_sglist_malloced; 1587 int i, j; 1588 1589 ext_sglist_malloced = 0; 1590 ext_sg_start = 0; 1591 ext_offset = 0; 1592 1593 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1594 1595 /* 1596 * If this flag is set, fake the data transfer. 1597 */ 1598 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1599 ctsio->ext_data_filled = ctsio->ext_data_len; 1600 goto bailout; 1601 } 1602 1603 /* 1604 * To simplify things here, if we have a single buffer, stick it in 1605 * a S/G entry and just make it a single entry S/G list. 1606 */ 1607 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1608 int len_seen; 1609 1610 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1611 1612 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1613 M_WAITOK); 1614 ext_sglist_malloced = 1; 1615 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1616 ext_sglen) != 0) { 1617 ctl_set_internal_failure(ctsio, 1618 /*sks_valid*/ 0, 1619 /*retry_count*/ 0); 1620 goto bailout; 1621 } 1622 ext_sg_entries = ctsio->ext_sg_entries; 1623 len_seen = 0; 1624 for (i = 0; i < ext_sg_entries; i++) { 1625 if ((len_seen + ext_sglist[i].len) >= 1626 ctsio->ext_data_filled) { 1627 ext_sg_start = i; 1628 ext_offset = ctsio->ext_data_filled - len_seen; 1629 break; 1630 } 1631 len_seen += ext_sglist[i].len; 1632 } 1633 } else { 1634 ext_sglist = &ext_entry; 1635 ext_sglist->addr = ctsio->ext_data_ptr; 1636 ext_sglist->len = ctsio->ext_data_len; 1637 ext_sg_entries = 1; 1638 ext_sg_start = 0; 1639 ext_offset = ctsio->ext_data_filled; 1640 } 1641 1642 if (ctsio->kern_sg_entries > 0) { 1643 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1644 kern_sg_entries = ctsio->kern_sg_entries; 1645 } else { 1646 kern_sglist = &kern_entry; 1647 kern_sglist->addr = ctsio->kern_data_ptr; 1648 kern_sglist->len = ctsio->kern_data_len; 1649 kern_sg_entries = 1; 1650 } 1651 1652 1653 kern_watermark = 0; 1654 ext_watermark = ext_offset; 1655 len_copied = 0; 1656 for (i = ext_sg_start, j = 0; 1657 i < ext_sg_entries && j < kern_sg_entries;) { 1658 uint8_t *ext_ptr, *kern_ptr; 1659 1660 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1661 kern_sglist[j].len - kern_watermark); 1662 1663 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1664 ext_ptr = ext_ptr + ext_watermark; 1665 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1666 /* 1667 * XXX KDM fix this! 1668 */ 1669 panic("need to implement bus address support"); 1670#if 0 1671 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1672#endif 1673 } else 1674 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1675 kern_ptr = kern_ptr + kern_watermark; 1676 1677 kern_watermark += len_to_copy; 1678 ext_watermark += len_to_copy; 1679 1680 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1681 CTL_FLAG_DATA_IN) { 1682 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1683 "bytes to user\n", len_to_copy)); 1684 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1685 "to %p\n", kern_ptr, ext_ptr)); 1686 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1687 ctl_set_internal_failure(ctsio, 1688 /*sks_valid*/ 0, 1689 /*retry_count*/ 0); 1690 goto bailout; 1691 } 1692 } else { 1693 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1694 "bytes from user\n", len_to_copy)); 1695 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1696 "to %p\n", ext_ptr, kern_ptr)); 1697 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1698 ctl_set_internal_failure(ctsio, 1699 /*sks_valid*/ 0, 1700 /*retry_count*/0); 1701 goto bailout; 1702 } 1703 } 1704 1705 len_copied += len_to_copy; 1706 1707 if (ext_sglist[i].len == ext_watermark) { 1708 i++; 1709 ext_watermark = 0; 1710 } 1711 1712 if (kern_sglist[j].len == kern_watermark) { 1713 j++; 1714 kern_watermark = 0; 1715 } 1716 } 1717 1718 ctsio->ext_data_filled += len_copied; 1719 1720 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1721 "kern_sg_entries: %d\n", ext_sg_entries, 1722 kern_sg_entries)); 1723 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1724 "kern_data_len = %d\n", ctsio->ext_data_len, 1725 ctsio->kern_data_len)); 1726 1727 1728 /* XXX KDM set residual?? */ 1729bailout: 1730 1731 if (ext_sglist_malloced != 0) 1732 free(ext_sglist, M_CTL); 1733 1734 return (CTL_RETVAL_COMPLETE); 1735} 1736 1737/* 1738 * Serialize a command that went down the "wrong" side, and so was sent to 1739 * this controller for execution. The logic is a little different than the 1740 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1741 * sent back to the other side, but in the success case, we execute the 1742 * command on this side (XFER mode) or tell the other side to execute it 1743 * (SER_ONLY mode). 1744 */ 1745static int 1746ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1747{ 1748 struct ctl_softc *ctl_softc; 1749 union ctl_ha_msg msg_info; 1750 struct ctl_lun *lun; 1751 int retval = 0; 1752 uint32_t targ_lun; 1753 1754 ctl_softc = control_softc; 1755 1756 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1757 lun = ctl_softc->ctl_luns[targ_lun]; 1758 if (lun==NULL) 1759 { 1760 /* 1761 * Why isn't LUN defined? The other side wouldn't 1762 * send a cmd if the LUN is undefined. 1763 */ 1764 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1765 1766 /* "Logical unit not supported" */ 1767 ctl_set_sense_data(&msg_info.scsi.sense_data, 1768 lun, 1769 /*sense_format*/SSD_TYPE_NONE, 1770 /*current_error*/ 1, 1771 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1772 /*asc*/ 0x25, 1773 /*ascq*/ 0x00, 1774 SSD_ELEM_NONE); 1775 1776 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1777 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1778 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1779 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1780 msg_info.hdr.serializing_sc = NULL; 1781 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1782 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1783 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1784 } 1785 return(1); 1786 1787 } 1788 1789 mtx_lock(&lun->lun_lock); 1790 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1791 1792 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1793 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1794 ooa_links))) { 1795 case CTL_ACTION_BLOCK: 1796 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1797 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1798 blocked_links); 1799 break; 1800 case CTL_ACTION_PASS: 1801 case CTL_ACTION_SKIP: 1802 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1803 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1804 ctl_enqueue_rtr((union ctl_io *)ctsio); 1805 } else { 1806 1807 /* send msg back to other side */ 1808 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1809 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1810 msg_info.hdr.msg_type = CTL_MSG_R2R; 1811#if 0 1812 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1813#endif 1814 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1815 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1816 } 1817 } 1818 break; 1819 case CTL_ACTION_OVERLAP: 1820 /* OVERLAPPED COMMANDS ATTEMPTED */ 1821 ctl_set_sense_data(&msg_info.scsi.sense_data, 1822 lun, 1823 /*sense_format*/SSD_TYPE_NONE, 1824 /*current_error*/ 1, 1825 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1826 /*asc*/ 0x4E, 1827 /*ascq*/ 0x00, 1828 SSD_ELEM_NONE); 1829 1830 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1831 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1832 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1833 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1834 msg_info.hdr.serializing_sc = NULL; 1835 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1836#if 0 1837 printf("BAD JUJU:Major Bummer Overlap\n"); 1838#endif 1839 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1840 retval = 1; 1841 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1842 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1843 } 1844 break; 1845 case CTL_ACTION_OVERLAP_TAG: 1846 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1847 ctl_set_sense_data(&msg_info.scsi.sense_data, 1848 lun, 1849 /*sense_format*/SSD_TYPE_NONE, 1850 /*current_error*/ 1, 1851 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1852 /*asc*/ 0x4D, 1853 /*ascq*/ ctsio->tag_num & 0xff, 1854 SSD_ELEM_NONE); 1855 1856 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1857 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1858 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1859 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1860 msg_info.hdr.serializing_sc = NULL; 1861 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1862#if 0 1863 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1864#endif 1865 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1866 retval = 1; 1867 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1868 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1869 } 1870 break; 1871 case CTL_ACTION_ERROR: 1872 default: 1873 /* "Internal target failure" */ 1874 ctl_set_sense_data(&msg_info.scsi.sense_data, 1875 lun, 1876 /*sense_format*/SSD_TYPE_NONE, 1877 /*current_error*/ 1, 1878 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1879 /*asc*/ 0x44, 1880 /*ascq*/ 0x00, 1881 SSD_ELEM_NONE); 1882 1883 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1884 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1885 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1886 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1887 msg_info.hdr.serializing_sc = NULL; 1888 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1889#if 0 1890 printf("BAD JUJU:Major Bummer HW Error\n"); 1891#endif 1892 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1893 retval = 1; 1894 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1895 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1896 } 1897 break; 1898 } 1899 mtx_unlock(&lun->lun_lock); 1900 return (retval); 1901} 1902 1903static int 1904ctl_ioctl_submit_wait(union ctl_io *io) 1905{ 1906 struct ctl_fe_ioctl_params params; 1907 ctl_fe_ioctl_state last_state; 1908 int done, retval; 1909 1910 retval = 0; 1911 1912 bzero(¶ms, sizeof(params)); 1913 1914 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1915 cv_init(¶ms.sem, "ctlioccv"); 1916 params.state = CTL_IOCTL_INPROG; 1917 last_state = params.state; 1918 1919 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1920 1921 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1922 1923 /* This shouldn't happen */ 1924 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1925 return (retval); 1926 1927 done = 0; 1928 1929 do { 1930 mtx_lock(¶ms.ioctl_mtx); 1931 /* 1932 * Check the state here, and don't sleep if the state has 1933 * already changed (i.e. wakeup has already occured, but we 1934 * weren't waiting yet). 1935 */ 1936 if (params.state == last_state) { 1937 /* XXX KDM cv_wait_sig instead? */ 1938 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1939 } 1940 last_state = params.state; 1941 1942 switch (params.state) { 1943 case CTL_IOCTL_INPROG: 1944 /* Why did we wake up? */ 1945 /* XXX KDM error here? */ 1946 mtx_unlock(¶ms.ioctl_mtx); 1947 break; 1948 case CTL_IOCTL_DATAMOVE: 1949 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1950 1951 /* 1952 * change last_state back to INPROG to avoid 1953 * deadlock on subsequent data moves. 1954 */ 1955 params.state = last_state = CTL_IOCTL_INPROG; 1956 1957 mtx_unlock(¶ms.ioctl_mtx); 1958 ctl_ioctl_do_datamove(&io->scsiio); 1959 /* 1960 * Note that in some cases, most notably writes, 1961 * this will queue the I/O and call us back later. 1962 * In other cases, generally reads, this routine 1963 * will immediately call back and wake us up, 1964 * probably using our own context. 1965 */ 1966 io->scsiio.be_move_done(io); 1967 break; 1968 case CTL_IOCTL_DONE: 1969 mtx_unlock(¶ms.ioctl_mtx); 1970 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1971 done = 1; 1972 break; 1973 default: 1974 mtx_unlock(¶ms.ioctl_mtx); 1975 /* XXX KDM error here? */ 1976 break; 1977 } 1978 } while (done == 0); 1979 1980 mtx_destroy(¶ms.ioctl_mtx); 1981 cv_destroy(¶ms.sem); 1982 1983 return (CTL_RETVAL_COMPLETE); 1984} 1985 1986static void 1987ctl_ioctl_datamove(union ctl_io *io) 1988{ 1989 struct ctl_fe_ioctl_params *params; 1990 1991 params = (struct ctl_fe_ioctl_params *) 1992 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1993 1994 mtx_lock(¶ms->ioctl_mtx); 1995 params->state = CTL_IOCTL_DATAMOVE; 1996 cv_broadcast(¶ms->sem); 1997 mtx_unlock(¶ms->ioctl_mtx); 1998} 1999 2000static void 2001ctl_ioctl_done(union ctl_io *io) 2002{ 2003 struct ctl_fe_ioctl_params *params; 2004 2005 params = (struct ctl_fe_ioctl_params *) 2006 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2007 2008 mtx_lock(¶ms->ioctl_mtx); 2009 params->state = CTL_IOCTL_DONE; 2010 cv_broadcast(¶ms->sem); 2011 mtx_unlock(¶ms->ioctl_mtx); 2012} 2013 2014static void 2015ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2016{ 2017 struct ctl_fe_ioctl_startstop_info *sd_info; 2018 2019 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2020 2021 sd_info->hs_info.status = metatask->status; 2022 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2023 sd_info->hs_info.luns_complete = 2024 metatask->taskinfo.startstop.luns_complete; 2025 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2026 2027 cv_broadcast(&sd_info->sem); 2028} 2029 2030static void 2031ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2032{ 2033 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2034 2035 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2036 2037 mtx_lock(fe_bbr_info->lock); 2038 fe_bbr_info->bbr_info->status = metatask->status; 2039 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2040 fe_bbr_info->wakeup_done = 1; 2041 mtx_unlock(fe_bbr_info->lock); 2042 2043 cv_broadcast(&fe_bbr_info->sem); 2044} 2045 2046/* 2047 * Returns 0 for success, errno for failure. 2048 */ 2049static int 2050ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2051 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2052{ 2053 union ctl_io *io; 2054 int retval; 2055 2056 retval = 0; 2057 2058 mtx_lock(&lun->lun_lock); 2059 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2060 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2061 ooa_links)) { 2062 struct ctl_ooa_entry *entry; 2063 2064 /* 2065 * If we've got more than we can fit, just count the 2066 * remaining entries. 2067 */ 2068 if (*cur_fill_num >= ooa_hdr->alloc_num) 2069 continue; 2070 2071 entry = &kern_entries[*cur_fill_num]; 2072 2073 entry->tag_num = io->scsiio.tag_num; 2074 entry->lun_num = lun->lun; 2075#ifdef CTL_TIME_IO 2076 entry->start_bt = io->io_hdr.start_bt; 2077#endif 2078 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2079 entry->cdb_len = io->scsiio.cdb_len; 2080 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2081 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2082 2083 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2084 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2085 2086 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2087 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2088 2089 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2090 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2091 2092 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2093 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2094 } 2095 mtx_unlock(&lun->lun_lock); 2096 2097 return (retval); 2098} 2099 2100static void * 2101ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2102 size_t error_str_len) 2103{ 2104 void *kptr; 2105 2106 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2107 2108 if (copyin(user_addr, kptr, len) != 0) { 2109 snprintf(error_str, error_str_len, "Error copying %d bytes " 2110 "from user address %p to kernel address %p", len, 2111 user_addr, kptr); 2112 free(kptr, M_CTL); 2113 return (NULL); 2114 } 2115 2116 return (kptr); 2117} 2118 2119static void 2120ctl_free_args(int num_args, struct ctl_be_arg *args) 2121{ 2122 int i; 2123 2124 if (args == NULL) 2125 return; 2126 2127 for (i = 0; i < num_args; i++) { 2128 free(args[i].kname, M_CTL); 2129 free(args[i].kvalue, M_CTL); 2130 } 2131 2132 free(args, M_CTL); 2133} 2134 2135static struct ctl_be_arg * 2136ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2137 char *error_str, size_t error_str_len) 2138{ 2139 struct ctl_be_arg *args; 2140 int i; 2141 2142 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2143 error_str, error_str_len); 2144 2145 if (args == NULL) 2146 goto bailout; 2147 2148 for (i = 0; i < num_args; i++) { 2149 args[i].kname = NULL; 2150 args[i].kvalue = NULL; 2151 } 2152 2153 for (i = 0; i < num_args; i++) { 2154 uint8_t *tmpptr; 2155 2156 args[i].kname = ctl_copyin_alloc(args[i].name, 2157 args[i].namelen, error_str, error_str_len); 2158 if (args[i].kname == NULL) 2159 goto bailout; 2160 2161 if (args[i].kname[args[i].namelen - 1] != '\0') { 2162 snprintf(error_str, error_str_len, "Argument %d " 2163 "name is not NUL-terminated", i); 2164 goto bailout; 2165 } 2166 2167 if (args[i].flags & CTL_BEARG_RD) { 2168 tmpptr = ctl_copyin_alloc(args[i].value, 2169 args[i].vallen, error_str, error_str_len); 2170 if (tmpptr == NULL) 2171 goto bailout; 2172 if ((args[i].flags & CTL_BEARG_ASCII) 2173 && (tmpptr[args[i].vallen - 1] != '\0')) { 2174 snprintf(error_str, error_str_len, "Argument " 2175 "%d value is not NUL-terminated", i); 2176 goto bailout; 2177 } 2178 args[i].kvalue = tmpptr; 2179 } else { 2180 args[i].kvalue = malloc(args[i].vallen, 2181 M_CTL, M_WAITOK | M_ZERO); 2182 } 2183 } 2184 2185 return (args); 2186bailout: 2187 2188 ctl_free_args(num_args, args); 2189 2190 return (NULL); 2191} 2192 2193static void 2194ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2195{ 2196 int i; 2197 2198 for (i = 0; i < num_args; i++) { 2199 if (args[i].flags & CTL_BEARG_WR) 2200 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2201 } 2202} 2203 2204/* 2205 * Escape characters that are illegal or not recommended in XML. 2206 */ 2207int 2208ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2209{ 2210 int retval; 2211 2212 retval = 0; 2213 2214 for (; *str; str++) { 2215 switch (*str) { 2216 case '&': 2217 retval = sbuf_printf(sb, "&"); 2218 break; 2219 case '>': 2220 retval = sbuf_printf(sb, ">"); 2221 break; 2222 case '<': 2223 retval = sbuf_printf(sb, "<"); 2224 break; 2225 default: 2226 retval = sbuf_putc(sb, *str); 2227 break; 2228 } 2229 2230 if (retval != 0) 2231 break; 2232 2233 } 2234 2235 return (retval); 2236} 2237 2238static int 2239ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2240 struct thread *td) 2241{ 2242 struct ctl_softc *softc; 2243 int retval; 2244 2245 softc = control_softc; 2246 2247 retval = 0; 2248 2249 switch (cmd) { 2250 case CTL_IO: { 2251 union ctl_io *io; 2252 void *pool_tmp; 2253 2254 /* 2255 * If we haven't been "enabled", don't allow any SCSI I/O 2256 * to this FETD. 2257 */ 2258 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2259 retval = EPERM; 2260 break; 2261 } 2262 2263 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2264 if (io == NULL) { 2265 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2266 retval = ENOSPC; 2267 break; 2268 } 2269 2270 /* 2271 * Need to save the pool reference so it doesn't get 2272 * spammed by the user's ctl_io. 2273 */ 2274 pool_tmp = io->io_hdr.pool; 2275 2276 memcpy(io, (void *)addr, sizeof(*io)); 2277 2278 io->io_hdr.pool = pool_tmp; 2279 /* 2280 * No status yet, so make sure the status is set properly. 2281 */ 2282 io->io_hdr.status = CTL_STATUS_NONE; 2283 2284 /* 2285 * The user sets the initiator ID, target and LUN IDs. 2286 */ 2287 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2288 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2289 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2290 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2291 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2292 2293 retval = ctl_ioctl_submit_wait(io); 2294 2295 if (retval != 0) { 2296 ctl_free_io(io); 2297 break; 2298 } 2299 2300 memcpy((void *)addr, io, sizeof(*io)); 2301 2302 /* return this to our pool */ 2303 ctl_free_io(io); 2304 2305 break; 2306 } 2307 case CTL_ENABLE_PORT: 2308 case CTL_DISABLE_PORT: 2309 case CTL_SET_PORT_WWNS: { 2310 struct ctl_port *port; 2311 struct ctl_port_entry *entry; 2312 2313 entry = (struct ctl_port_entry *)addr; 2314 2315 mtx_lock(&softc->ctl_lock); 2316 STAILQ_FOREACH(port, &softc->port_list, links) { 2317 int action, done; 2318 2319 action = 0; 2320 done = 0; 2321 2322 if ((entry->port_type == CTL_PORT_NONE) 2323 && (entry->targ_port == port->targ_port)) { 2324 /* 2325 * If the user only wants to enable or 2326 * disable or set WWNs on a specific port, 2327 * do the operation and we're done. 2328 */ 2329 action = 1; 2330 done = 1; 2331 } else if (entry->port_type & port->port_type) { 2332 /* 2333 * Compare the user's type mask with the 2334 * particular frontend type to see if we 2335 * have a match. 2336 */ 2337 action = 1; 2338 done = 0; 2339 2340 /* 2341 * Make sure the user isn't trying to set 2342 * WWNs on multiple ports at the same time. 2343 */ 2344 if (cmd == CTL_SET_PORT_WWNS) { 2345 printf("%s: Can't set WWNs on " 2346 "multiple ports\n", __func__); 2347 retval = EINVAL; 2348 break; 2349 } 2350 } 2351 if (action != 0) { 2352 /* 2353 * XXX KDM we have to drop the lock here, 2354 * because the online/offline operations 2355 * can potentially block. We need to 2356 * reference count the frontends so they 2357 * can't go away, 2358 */ 2359 mtx_unlock(&softc->ctl_lock); 2360 2361 if (cmd == CTL_ENABLE_PORT) { 2362 struct ctl_lun *lun; 2363 2364 STAILQ_FOREACH(lun, &softc->lun_list, 2365 links) { 2366 port->lun_enable(port->targ_lun_arg, 2367 lun->target, 2368 lun->lun); 2369 } 2370 2371 ctl_port_online(port); 2372 } else if (cmd == CTL_DISABLE_PORT) { 2373 struct ctl_lun *lun; 2374 2375 ctl_port_offline(port); 2376 2377 STAILQ_FOREACH(lun, &softc->lun_list, 2378 links) { 2379 port->lun_disable( 2380 port->targ_lun_arg, 2381 lun->target, 2382 lun->lun); 2383 } 2384 } 2385 2386 mtx_lock(&softc->ctl_lock); 2387 2388 if (cmd == CTL_SET_PORT_WWNS) 2389 ctl_port_set_wwns(port, 2390 (entry->flags & CTL_PORT_WWNN_VALID) ? 2391 1 : 0, entry->wwnn, 2392 (entry->flags & CTL_PORT_WWPN_VALID) ? 2393 1 : 0, entry->wwpn); 2394 } 2395 if (done != 0) 2396 break; 2397 } 2398 mtx_unlock(&softc->ctl_lock); 2399 break; 2400 } 2401 case CTL_GET_PORT_LIST: { 2402 struct ctl_port *port; 2403 struct ctl_port_list *list; 2404 int i; 2405 2406 list = (struct ctl_port_list *)addr; 2407 2408 if (list->alloc_len != (list->alloc_num * 2409 sizeof(struct ctl_port_entry))) { 2410 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2411 "alloc_num %u * sizeof(struct ctl_port_entry) " 2412 "%zu\n", __func__, list->alloc_len, 2413 list->alloc_num, sizeof(struct ctl_port_entry)); 2414 retval = EINVAL; 2415 break; 2416 } 2417 list->fill_len = 0; 2418 list->fill_num = 0; 2419 list->dropped_num = 0; 2420 i = 0; 2421 mtx_lock(&softc->ctl_lock); 2422 STAILQ_FOREACH(port, &softc->port_list, links) { 2423 struct ctl_port_entry entry, *list_entry; 2424 2425 if (list->fill_num >= list->alloc_num) { 2426 list->dropped_num++; 2427 continue; 2428 } 2429 2430 entry.port_type = port->port_type; 2431 strlcpy(entry.port_name, port->port_name, 2432 sizeof(entry.port_name)); 2433 entry.targ_port = port->targ_port; 2434 entry.physical_port = port->physical_port; 2435 entry.virtual_port = port->virtual_port; 2436 entry.wwnn = port->wwnn; 2437 entry.wwpn = port->wwpn; 2438 if (port->status & CTL_PORT_STATUS_ONLINE) 2439 entry.online = 1; 2440 else 2441 entry.online = 0; 2442 2443 list_entry = &list->entries[i]; 2444 2445 retval = copyout(&entry, list_entry, sizeof(entry)); 2446 if (retval != 0) { 2447 printf("%s: CTL_GET_PORT_LIST: copyout " 2448 "returned %d\n", __func__, retval); 2449 break; 2450 } 2451 i++; 2452 list->fill_num++; 2453 list->fill_len += sizeof(entry); 2454 } 2455 mtx_unlock(&softc->ctl_lock); 2456 2457 /* 2458 * If this is non-zero, we had a copyout fault, so there's 2459 * probably no point in attempting to set the status inside 2460 * the structure. 2461 */ 2462 if (retval != 0) 2463 break; 2464 2465 if (list->dropped_num > 0) 2466 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2467 else 2468 list->status = CTL_PORT_LIST_OK; 2469 break; 2470 } 2471 case CTL_DUMP_OOA: { 2472 struct ctl_lun *lun; 2473 union ctl_io *io; 2474 char printbuf[128]; 2475 struct sbuf sb; 2476 2477 mtx_lock(&softc->ctl_lock); 2478 printf("Dumping OOA queues:\n"); 2479 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2480 mtx_lock(&lun->lun_lock); 2481 for (io = (union ctl_io *)TAILQ_FIRST( 2482 &lun->ooa_queue); io != NULL; 2483 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2484 ooa_links)) { 2485 sbuf_new(&sb, printbuf, sizeof(printbuf), 2486 SBUF_FIXEDLEN); 2487 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2488 (intmax_t)lun->lun, 2489 io->scsiio.tag_num, 2490 (io->io_hdr.flags & 2491 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2492 (io->io_hdr.flags & 2493 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2494 (io->io_hdr.flags & 2495 CTL_FLAG_ABORT) ? " ABORT" : "", 2496 (io->io_hdr.flags & 2497 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2498 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2499 sbuf_finish(&sb); 2500 printf("%s\n", sbuf_data(&sb)); 2501 } 2502 mtx_unlock(&lun->lun_lock); 2503 } 2504 printf("OOA queues dump done\n"); 2505 mtx_unlock(&softc->ctl_lock); 2506 break; 2507 } 2508 case CTL_GET_OOA: { 2509 struct ctl_lun *lun; 2510 struct ctl_ooa *ooa_hdr; 2511 struct ctl_ooa_entry *entries; 2512 uint32_t cur_fill_num; 2513 2514 ooa_hdr = (struct ctl_ooa *)addr; 2515 2516 if ((ooa_hdr->alloc_len == 0) 2517 || (ooa_hdr->alloc_num == 0)) { 2518 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2519 "must be non-zero\n", __func__, 2520 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2521 retval = EINVAL; 2522 break; 2523 } 2524 2525 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2526 sizeof(struct ctl_ooa_entry))) { 2527 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2528 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2529 __func__, ooa_hdr->alloc_len, 2530 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2531 retval = EINVAL; 2532 break; 2533 } 2534 2535 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2536 if (entries == NULL) { 2537 printf("%s: could not allocate %d bytes for OOA " 2538 "dump\n", __func__, ooa_hdr->alloc_len); 2539 retval = ENOMEM; 2540 break; 2541 } 2542 2543 mtx_lock(&softc->ctl_lock); 2544 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2545 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2546 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2547 mtx_unlock(&softc->ctl_lock); 2548 free(entries, M_CTL); 2549 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2550 __func__, (uintmax_t)ooa_hdr->lun_num); 2551 retval = EINVAL; 2552 break; 2553 } 2554 2555 cur_fill_num = 0; 2556 2557 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2558 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2559 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2560 ooa_hdr, entries); 2561 if (retval != 0) 2562 break; 2563 } 2564 if (retval != 0) { 2565 mtx_unlock(&softc->ctl_lock); 2566 free(entries, M_CTL); 2567 break; 2568 } 2569 } else { 2570 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2571 2572 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2573 entries); 2574 } 2575 mtx_unlock(&softc->ctl_lock); 2576 2577 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2578 ooa_hdr->fill_len = ooa_hdr->fill_num * 2579 sizeof(struct ctl_ooa_entry); 2580 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2581 if (retval != 0) { 2582 printf("%s: error copying out %d bytes for OOA dump\n", 2583 __func__, ooa_hdr->fill_len); 2584 } 2585 2586 getbintime(&ooa_hdr->cur_bt); 2587 2588 if (cur_fill_num > ooa_hdr->alloc_num) { 2589 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2590 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2591 } else { 2592 ooa_hdr->dropped_num = 0; 2593 ooa_hdr->status = CTL_OOA_OK; 2594 } 2595 2596 free(entries, M_CTL); 2597 break; 2598 } 2599 case CTL_CHECK_OOA: { 2600 union ctl_io *io; 2601 struct ctl_lun *lun; 2602 struct ctl_ooa_info *ooa_info; 2603 2604 2605 ooa_info = (struct ctl_ooa_info *)addr; 2606 2607 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2608 ooa_info->status = CTL_OOA_INVALID_LUN; 2609 break; 2610 } 2611 mtx_lock(&softc->ctl_lock); 2612 lun = softc->ctl_luns[ooa_info->lun_id]; 2613 if (lun == NULL) { 2614 mtx_unlock(&softc->ctl_lock); 2615 ooa_info->status = CTL_OOA_INVALID_LUN; 2616 break; 2617 } 2618 mtx_lock(&lun->lun_lock); 2619 mtx_unlock(&softc->ctl_lock); 2620 ooa_info->num_entries = 0; 2621 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2622 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2623 &io->io_hdr, ooa_links)) { 2624 ooa_info->num_entries++; 2625 } 2626 mtx_unlock(&lun->lun_lock); 2627 2628 ooa_info->status = CTL_OOA_SUCCESS; 2629 2630 break; 2631 } 2632 case CTL_HARD_START: 2633 case CTL_HARD_STOP: { 2634 struct ctl_fe_ioctl_startstop_info ss_info; 2635 struct cfi_metatask *metatask; 2636 struct mtx hs_mtx; 2637 2638 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2639 2640 cv_init(&ss_info.sem, "hard start/stop cv" ); 2641 2642 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2643 if (metatask == NULL) { 2644 retval = ENOMEM; 2645 mtx_destroy(&hs_mtx); 2646 break; 2647 } 2648 2649 if (cmd == CTL_HARD_START) 2650 metatask->tasktype = CFI_TASK_STARTUP; 2651 else 2652 metatask->tasktype = CFI_TASK_SHUTDOWN; 2653 2654 metatask->callback = ctl_ioctl_hard_startstop_callback; 2655 metatask->callback_arg = &ss_info; 2656 2657 cfi_action(metatask); 2658 2659 /* Wait for the callback */ 2660 mtx_lock(&hs_mtx); 2661 cv_wait_sig(&ss_info.sem, &hs_mtx); 2662 mtx_unlock(&hs_mtx); 2663 2664 /* 2665 * All information has been copied from the metatask by the 2666 * time cv_broadcast() is called, so we free the metatask here. 2667 */ 2668 cfi_free_metatask(metatask); 2669 2670 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2671 2672 mtx_destroy(&hs_mtx); 2673 break; 2674 } 2675 case CTL_BBRREAD: { 2676 struct ctl_bbrread_info *bbr_info; 2677 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2678 struct mtx bbr_mtx; 2679 struct cfi_metatask *metatask; 2680 2681 bbr_info = (struct ctl_bbrread_info *)addr; 2682 2683 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2684 2685 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2686 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2687 2688 fe_bbr_info.bbr_info = bbr_info; 2689 fe_bbr_info.lock = &bbr_mtx; 2690 2691 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2692 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2693 2694 if (metatask == NULL) { 2695 mtx_destroy(&bbr_mtx); 2696 cv_destroy(&fe_bbr_info.sem); 2697 retval = ENOMEM; 2698 break; 2699 } 2700 metatask->tasktype = CFI_TASK_BBRREAD; 2701 metatask->callback = ctl_ioctl_bbrread_callback; 2702 metatask->callback_arg = &fe_bbr_info; 2703 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2704 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2705 metatask->taskinfo.bbrread.len = bbr_info->len; 2706 2707 cfi_action(metatask); 2708 2709 mtx_lock(&bbr_mtx); 2710 while (fe_bbr_info.wakeup_done == 0) 2711 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2712 mtx_unlock(&bbr_mtx); 2713 2714 bbr_info->status = metatask->status; 2715 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2716 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2717 memcpy(&bbr_info->sense_data, 2718 &metatask->taskinfo.bbrread.sense_data, 2719 ctl_min(sizeof(bbr_info->sense_data), 2720 sizeof(metatask->taskinfo.bbrread.sense_data))); 2721 2722 cfi_free_metatask(metatask); 2723 2724 mtx_destroy(&bbr_mtx); 2725 cv_destroy(&fe_bbr_info.sem); 2726 2727 break; 2728 } 2729 case CTL_DELAY_IO: { 2730 struct ctl_io_delay_info *delay_info; 2731#ifdef CTL_IO_DELAY 2732 struct ctl_lun *lun; 2733#endif /* CTL_IO_DELAY */ 2734 2735 delay_info = (struct ctl_io_delay_info *)addr; 2736 2737#ifdef CTL_IO_DELAY 2738 mtx_lock(&softc->ctl_lock); 2739 2740 if ((delay_info->lun_id > CTL_MAX_LUNS) 2741 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2742 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2743 } else { 2744 lun = softc->ctl_luns[delay_info->lun_id]; 2745 mtx_lock(&lun->lun_lock); 2746 2747 delay_info->status = CTL_DELAY_STATUS_OK; 2748 2749 switch (delay_info->delay_type) { 2750 case CTL_DELAY_TYPE_CONT: 2751 break; 2752 case CTL_DELAY_TYPE_ONESHOT: 2753 break; 2754 default: 2755 delay_info->status = 2756 CTL_DELAY_STATUS_INVALID_TYPE; 2757 break; 2758 } 2759 2760 switch (delay_info->delay_loc) { 2761 case CTL_DELAY_LOC_DATAMOVE: 2762 lun->delay_info.datamove_type = 2763 delay_info->delay_type; 2764 lun->delay_info.datamove_delay = 2765 delay_info->delay_secs; 2766 break; 2767 case CTL_DELAY_LOC_DONE: 2768 lun->delay_info.done_type = 2769 delay_info->delay_type; 2770 lun->delay_info.done_delay = 2771 delay_info->delay_secs; 2772 break; 2773 default: 2774 delay_info->status = 2775 CTL_DELAY_STATUS_INVALID_LOC; 2776 break; 2777 } 2778 mtx_unlock(&lun->lun_lock); 2779 } 2780 2781 mtx_unlock(&softc->ctl_lock); 2782#else 2783 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2784#endif /* CTL_IO_DELAY */ 2785 break; 2786 } 2787 case CTL_REALSYNC_SET: { 2788 int *syncstate; 2789 2790 syncstate = (int *)addr; 2791 2792 mtx_lock(&softc->ctl_lock); 2793 switch (*syncstate) { 2794 case 0: 2795 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2796 break; 2797 case 1: 2798 softc->flags |= CTL_FLAG_REAL_SYNC; 2799 break; 2800 default: 2801 retval = EINVAL; 2802 break; 2803 } 2804 mtx_unlock(&softc->ctl_lock); 2805 break; 2806 } 2807 case CTL_REALSYNC_GET: { 2808 int *syncstate; 2809 2810 syncstate = (int*)addr; 2811 2812 mtx_lock(&softc->ctl_lock); 2813 if (softc->flags & CTL_FLAG_REAL_SYNC) 2814 *syncstate = 1; 2815 else 2816 *syncstate = 0; 2817 mtx_unlock(&softc->ctl_lock); 2818 2819 break; 2820 } 2821 case CTL_SETSYNC: 2822 case CTL_GETSYNC: { 2823 struct ctl_sync_info *sync_info; 2824 struct ctl_lun *lun; 2825 2826 sync_info = (struct ctl_sync_info *)addr; 2827 2828 mtx_lock(&softc->ctl_lock); 2829 lun = softc->ctl_luns[sync_info->lun_id]; 2830 if (lun == NULL) { 2831 mtx_unlock(&softc->ctl_lock); 2832 sync_info->status = CTL_GS_SYNC_NO_LUN; 2833 } 2834 /* 2835 * Get or set the sync interval. We're not bounds checking 2836 * in the set case, hopefully the user won't do something 2837 * silly. 2838 */ 2839 mtx_lock(&lun->lun_lock); 2840 mtx_unlock(&softc->ctl_lock); 2841 if (cmd == CTL_GETSYNC) 2842 sync_info->sync_interval = lun->sync_interval; 2843 else 2844 lun->sync_interval = sync_info->sync_interval; 2845 mtx_unlock(&lun->lun_lock); 2846 2847 sync_info->status = CTL_GS_SYNC_OK; 2848 2849 break; 2850 } 2851 case CTL_GETSTATS: { 2852 struct ctl_stats *stats; 2853 struct ctl_lun *lun; 2854 int i; 2855 2856 stats = (struct ctl_stats *)addr; 2857 2858 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2859 stats->alloc_len) { 2860 stats->status = CTL_SS_NEED_MORE_SPACE; 2861 stats->num_luns = softc->num_luns; 2862 break; 2863 } 2864 /* 2865 * XXX KDM no locking here. If the LUN list changes, 2866 * things can blow up. 2867 */ 2868 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2869 i++, lun = STAILQ_NEXT(lun, links)) { 2870 retval = copyout(&lun->stats, &stats->lun_stats[i], 2871 sizeof(lun->stats)); 2872 if (retval != 0) 2873 break; 2874 } 2875 stats->num_luns = softc->num_luns; 2876 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2877 softc->num_luns; 2878 stats->status = CTL_SS_OK; 2879#ifdef CTL_TIME_IO 2880 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2881#else 2882 stats->flags = CTL_STATS_FLAG_NONE; 2883#endif 2884 getnanouptime(&stats->timestamp); 2885 break; 2886 } 2887 case CTL_ERROR_INJECT: { 2888 struct ctl_error_desc *err_desc, *new_err_desc; 2889 struct ctl_lun *lun; 2890 2891 err_desc = (struct ctl_error_desc *)addr; 2892 2893 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2894 M_WAITOK | M_ZERO); 2895 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2896 2897 mtx_lock(&softc->ctl_lock); 2898 lun = softc->ctl_luns[err_desc->lun_id]; 2899 if (lun == NULL) { 2900 mtx_unlock(&softc->ctl_lock); 2901 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2902 __func__, (uintmax_t)err_desc->lun_id); 2903 retval = EINVAL; 2904 break; 2905 } 2906 mtx_lock(&lun->lun_lock); 2907 mtx_unlock(&softc->ctl_lock); 2908 2909 /* 2910 * We could do some checking here to verify the validity 2911 * of the request, but given the complexity of error 2912 * injection requests, the checking logic would be fairly 2913 * complex. 2914 * 2915 * For now, if the request is invalid, it just won't get 2916 * executed and might get deleted. 2917 */ 2918 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2919 2920 /* 2921 * XXX KDM check to make sure the serial number is unique, 2922 * in case we somehow manage to wrap. That shouldn't 2923 * happen for a very long time, but it's the right thing to 2924 * do. 2925 */ 2926 new_err_desc->serial = lun->error_serial; 2927 err_desc->serial = lun->error_serial; 2928 lun->error_serial++; 2929 2930 mtx_unlock(&lun->lun_lock); 2931 break; 2932 } 2933 case CTL_ERROR_INJECT_DELETE: { 2934 struct ctl_error_desc *delete_desc, *desc, *desc2; 2935 struct ctl_lun *lun; 2936 int delete_done; 2937 2938 delete_desc = (struct ctl_error_desc *)addr; 2939 delete_done = 0; 2940 2941 mtx_lock(&softc->ctl_lock); 2942 lun = softc->ctl_luns[delete_desc->lun_id]; 2943 if (lun == NULL) { 2944 mtx_unlock(&softc->ctl_lock); 2945 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2946 __func__, (uintmax_t)delete_desc->lun_id); 2947 retval = EINVAL; 2948 break; 2949 } 2950 mtx_lock(&lun->lun_lock); 2951 mtx_unlock(&softc->ctl_lock); 2952 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2953 if (desc->serial != delete_desc->serial) 2954 continue; 2955 2956 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2957 links); 2958 free(desc, M_CTL); 2959 delete_done = 1; 2960 } 2961 mtx_unlock(&lun->lun_lock); 2962 if (delete_done == 0) { 2963 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2964 "error serial %ju on LUN %u\n", __func__, 2965 delete_desc->serial, delete_desc->lun_id); 2966 retval = EINVAL; 2967 break; 2968 } 2969 break; 2970 } 2971 case CTL_DUMP_STRUCTS: { 2972 int i, j, k, idx; 2973 struct ctl_port *port; 2974 struct ctl_frontend *fe; 2975 2976 mtx_lock(&softc->ctl_lock); 2977 printf("CTL Persistent Reservation information start:\n"); 2978 for (i = 0; i < CTL_MAX_LUNS; i++) { 2979 struct ctl_lun *lun; 2980 2981 lun = softc->ctl_luns[i]; 2982 2983 if ((lun == NULL) 2984 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2985 continue; 2986 2987 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2988 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2989 idx = j * CTL_MAX_INIT_PER_PORT + k; 2990 if (lun->per_res[idx].registered == 0) 2991 continue; 2992 printf(" LUN %d port %d iid %d key " 2993 "%#jx\n", i, j, k, 2994 (uintmax_t)scsi_8btou64( 2995 lun->per_res[idx].res_key.key)); 2996 } 2997 } 2998 } 2999 printf("CTL Persistent Reservation information end\n"); 3000 printf("CTL Ports:\n"); 3001 STAILQ_FOREACH(port, &softc->port_list, links) { 3002 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3003 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3004 port->frontend->name, port->port_type, 3005 port->physical_port, port->virtual_port, 3006 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3007 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3008 if (port->wwpn_iid[j].in_use == 0 && 3009 port->wwpn_iid[j].wwpn == 0 && 3010 port->wwpn_iid[j].name == NULL) 3011 continue; 3012 3013 printf(" iid %u use %d WWPN %#jx '%s'\n", 3014 j, port->wwpn_iid[j].in_use, 3015 (uintmax_t)port->wwpn_iid[j].wwpn, 3016 port->wwpn_iid[j].name); 3017 } 3018 } 3019 printf("CTL Port information end\n"); 3020 mtx_unlock(&softc->ctl_lock); 3021 /* 3022 * XXX KDM calling this without a lock. We'd likely want 3023 * to drop the lock before calling the frontend's dump 3024 * routine anyway. 3025 */ 3026 printf("CTL Frontends:\n"); 3027 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3028 printf(" Frontend '%s'\n", fe->name); 3029 if (fe->fe_dump != NULL) 3030 fe->fe_dump(); 3031 } 3032 printf("CTL Frontend information end\n"); 3033 break; 3034 } 3035 case CTL_LUN_REQ: { 3036 struct ctl_lun_req *lun_req; 3037 struct ctl_backend_driver *backend; 3038 3039 lun_req = (struct ctl_lun_req *)addr; 3040 3041 backend = ctl_backend_find(lun_req->backend); 3042 if (backend == NULL) { 3043 lun_req->status = CTL_LUN_ERROR; 3044 snprintf(lun_req->error_str, 3045 sizeof(lun_req->error_str), 3046 "Backend \"%s\" not found.", 3047 lun_req->backend); 3048 break; 3049 } 3050 if (lun_req->num_be_args > 0) { 3051 lun_req->kern_be_args = ctl_copyin_args( 3052 lun_req->num_be_args, 3053 lun_req->be_args, 3054 lun_req->error_str, 3055 sizeof(lun_req->error_str)); 3056 if (lun_req->kern_be_args == NULL) { 3057 lun_req->status = CTL_LUN_ERROR; 3058 break; 3059 } 3060 } 3061 3062 retval = backend->ioctl(dev, cmd, addr, flag, td); 3063 3064 if (lun_req->num_be_args > 0) { 3065 ctl_copyout_args(lun_req->num_be_args, 3066 lun_req->kern_be_args); 3067 ctl_free_args(lun_req->num_be_args, 3068 lun_req->kern_be_args); 3069 } 3070 break; 3071 } 3072 case CTL_LUN_LIST: { 3073 struct sbuf *sb; 3074 struct ctl_lun *lun; 3075 struct ctl_lun_list *list; 3076 struct ctl_option *opt; 3077 3078 list = (struct ctl_lun_list *)addr; 3079 3080 /* 3081 * Allocate a fixed length sbuf here, based on the length 3082 * of the user's buffer. We could allocate an auto-extending 3083 * buffer, and then tell the user how much larger our 3084 * amount of data is than his buffer, but that presents 3085 * some problems: 3086 * 3087 * 1. The sbuf(9) routines use a blocking malloc, and so 3088 * we can't hold a lock while calling them with an 3089 * auto-extending buffer. 3090 * 3091 * 2. There is not currently a LUN reference counting 3092 * mechanism, outside of outstanding transactions on 3093 * the LUN's OOA queue. So a LUN could go away on us 3094 * while we're getting the LUN number, backend-specific 3095 * information, etc. Thus, given the way things 3096 * currently work, we need to hold the CTL lock while 3097 * grabbing LUN information. 3098 * 3099 * So, from the user's standpoint, the best thing to do is 3100 * allocate what he thinks is a reasonable buffer length, 3101 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3102 * double the buffer length and try again. (And repeat 3103 * that until he succeeds.) 3104 */ 3105 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3106 if (sb == NULL) { 3107 list->status = CTL_LUN_LIST_ERROR; 3108 snprintf(list->error_str, sizeof(list->error_str), 3109 "Unable to allocate %d bytes for LUN list", 3110 list->alloc_len); 3111 break; 3112 } 3113 3114 sbuf_printf(sb, "<ctllunlist>\n"); 3115 3116 mtx_lock(&softc->ctl_lock); 3117 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3118 mtx_lock(&lun->lun_lock); 3119 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3120 (uintmax_t)lun->lun); 3121 3122 /* 3123 * Bail out as soon as we see that we've overfilled 3124 * the buffer. 3125 */ 3126 if (retval != 0) 3127 break; 3128 3129 retval = sbuf_printf(sb, "\t<backend_type>%s" 3130 "</backend_type>\n", 3131 (lun->backend == NULL) ? "none" : 3132 lun->backend->name); 3133 3134 if (retval != 0) 3135 break; 3136 3137 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3138 lun->be_lun->lun_type); 3139 3140 if (retval != 0) 3141 break; 3142 3143 if (lun->backend == NULL) { 3144 retval = sbuf_printf(sb, "</lun>\n"); 3145 if (retval != 0) 3146 break; 3147 continue; 3148 } 3149 3150 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3151 (lun->be_lun->maxlba > 0) ? 3152 lun->be_lun->maxlba + 1 : 0); 3153 3154 if (retval != 0) 3155 break; 3156 3157 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3158 lun->be_lun->blocksize); 3159 3160 if (retval != 0) 3161 break; 3162 3163 retval = sbuf_printf(sb, "\t<serial_number>"); 3164 3165 if (retval != 0) 3166 break; 3167 3168 retval = ctl_sbuf_printf_esc(sb, 3169 lun->be_lun->serial_num); 3170 3171 if (retval != 0) 3172 break; 3173 3174 retval = sbuf_printf(sb, "</serial_number>\n"); 3175 3176 if (retval != 0) 3177 break; 3178 3179 retval = sbuf_printf(sb, "\t<device_id>"); 3180 3181 if (retval != 0) 3182 break; 3183 3184 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3185 3186 if (retval != 0) 3187 break; 3188 3189 retval = sbuf_printf(sb, "</device_id>\n"); 3190 3191 if (retval != 0) 3192 break; 3193 3194 if (lun->backend->lun_info != NULL) { 3195 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3196 if (retval != 0) 3197 break; 3198 } 3199 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3200 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3201 opt->name, opt->value, opt->name); 3202 if (retval != 0) 3203 break; 3204 } 3205 3206 retval = sbuf_printf(sb, "</lun>\n"); 3207 3208 if (retval != 0) 3209 break; 3210 mtx_unlock(&lun->lun_lock); 3211 } 3212 if (lun != NULL) 3213 mtx_unlock(&lun->lun_lock); 3214 mtx_unlock(&softc->ctl_lock); 3215 3216 if ((retval != 0) 3217 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3218 retval = 0; 3219 sbuf_delete(sb); 3220 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3221 snprintf(list->error_str, sizeof(list->error_str), 3222 "Out of space, %d bytes is too small", 3223 list->alloc_len); 3224 break; 3225 } 3226 3227 sbuf_finish(sb); 3228 3229 retval = copyout(sbuf_data(sb), list->lun_xml, 3230 sbuf_len(sb) + 1); 3231 3232 list->fill_len = sbuf_len(sb) + 1; 3233 list->status = CTL_LUN_LIST_OK; 3234 sbuf_delete(sb); 3235 break; 3236 } 3237 case CTL_ISCSI: { 3238 struct ctl_iscsi *ci; 3239 struct ctl_frontend *fe; 3240 3241 ci = (struct ctl_iscsi *)addr; 3242 3243 fe = ctl_frontend_find("iscsi"); 3244 if (fe == NULL) { 3245 ci->status = CTL_ISCSI_ERROR; 3246 snprintf(ci->error_str, sizeof(ci->error_str), 3247 "Frontend \"iscsi\" not found."); 3248 break; 3249 } 3250 3251 retval = fe->ioctl(dev, cmd, addr, flag, td); 3252 break; 3253 } 3254 case CTL_PORT_REQ: { 3255 struct ctl_req *req; 3256 struct ctl_frontend *fe; 3257 3258 req = (struct ctl_req *)addr; 3259 3260 fe = ctl_frontend_find(req->driver); 3261 if (fe == NULL) { 3262 req->status = CTL_LUN_ERROR; 3263 snprintf(req->error_str, sizeof(req->error_str), 3264 "Frontend \"%s\" not found.", req->driver); 3265 break; 3266 } 3267 if (req->num_args > 0) { 3268 req->kern_args = ctl_copyin_args(req->num_args, 3269 req->args, req->error_str, sizeof(req->error_str)); 3270 if (req->kern_args == NULL) { 3271 req->status = CTL_LUN_ERROR; 3272 break; 3273 } 3274 } 3275 3276 retval = fe->ioctl(dev, cmd, addr, flag, td); 3277 3278 if (req->num_args > 0) { 3279 ctl_copyout_args(req->num_args, req->kern_args); 3280 ctl_free_args(req->num_args, req->kern_args); 3281 } 3282 break; 3283 } 3284 case CTL_PORT_LIST: { 3285 struct sbuf *sb; 3286 struct ctl_port *port; 3287 struct ctl_lun_list *list; 3288 struct ctl_option *opt; 3289 3290 list = (struct ctl_lun_list *)addr; 3291 3292 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3293 if (sb == NULL) { 3294 list->status = CTL_LUN_LIST_ERROR; 3295 snprintf(list->error_str, sizeof(list->error_str), 3296 "Unable to allocate %d bytes for LUN list", 3297 list->alloc_len); 3298 break; 3299 } 3300 3301 sbuf_printf(sb, "<ctlportlist>\n"); 3302 3303 mtx_lock(&softc->ctl_lock); 3304 STAILQ_FOREACH(port, &softc->port_list, links) { 3305 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3306 (uintmax_t)port->targ_port); 3307 3308 /* 3309 * Bail out as soon as we see that we've overfilled 3310 * the buffer. 3311 */ 3312 if (retval != 0) 3313 break; 3314 3315 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3316 "</frontend_type>\n", port->frontend->name); 3317 if (retval != 0) 3318 break; 3319 3320 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3321 port->port_type); 3322 if (retval != 0) 3323 break; 3324 3325 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3326 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3327 if (retval != 0) 3328 break; 3329 3330 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3331 port->port_name); 3332 if (retval != 0) 3333 break; 3334 3335 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3336 port->physical_port); 3337 if (retval != 0) 3338 break; 3339 3340 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3341 port->virtual_port); 3342 if (retval != 0) 3343 break; 3344 3345 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3346 (uintmax_t)port->wwnn); 3347 if (retval != 0) 3348 break; 3349 3350 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3351 (uintmax_t)port->wwpn); 3352 if (retval != 0) 3353 break; 3354 3355 if (port->port_info != NULL) { 3356 retval = port->port_info(port->onoff_arg, sb); 3357 if (retval != 0) 3358 break; 3359 } 3360 STAILQ_FOREACH(opt, &port->options, links) { 3361 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3362 opt->name, opt->value, opt->name); 3363 if (retval != 0) 3364 break; 3365 } 3366 3367 retval = sbuf_printf(sb, "</targ_port>\n"); 3368 if (retval != 0) 3369 break; 3370 } 3371 mtx_unlock(&softc->ctl_lock); 3372 3373 if ((retval != 0) 3374 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3375 retval = 0; 3376 sbuf_delete(sb); 3377 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3378 snprintf(list->error_str, sizeof(list->error_str), 3379 "Out of space, %d bytes is too small", 3380 list->alloc_len); 3381 break; 3382 } 3383 3384 sbuf_finish(sb); 3385 3386 retval = copyout(sbuf_data(sb), list->lun_xml, 3387 sbuf_len(sb) + 1); 3388 3389 list->fill_len = sbuf_len(sb) + 1; 3390 list->status = CTL_LUN_LIST_OK; 3391 sbuf_delete(sb); 3392 break; 3393 } 3394 default: { 3395 /* XXX KDM should we fix this? */ 3396#if 0 3397 struct ctl_backend_driver *backend; 3398 unsigned int type; 3399 int found; 3400 3401 found = 0; 3402 3403 /* 3404 * We encode the backend type as the ioctl type for backend 3405 * ioctls. So parse it out here, and then search for a 3406 * backend of this type. 3407 */ 3408 type = _IOC_TYPE(cmd); 3409 3410 STAILQ_FOREACH(backend, &softc->be_list, links) { 3411 if (backend->type == type) { 3412 found = 1; 3413 break; 3414 } 3415 } 3416 if (found == 0) { 3417 printf("ctl: unknown ioctl command %#lx or backend " 3418 "%d\n", cmd, type); 3419 retval = EINVAL; 3420 break; 3421 } 3422 retval = backend->ioctl(dev, cmd, addr, flag, td); 3423#endif 3424 retval = ENOTTY; 3425 break; 3426 } 3427 } 3428 return (retval); 3429} 3430 3431uint32_t 3432ctl_get_initindex(struct ctl_nexus *nexus) 3433{ 3434 if (nexus->targ_port < CTL_MAX_PORTS) 3435 return (nexus->initid.id + 3436 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3437 else 3438 return (nexus->initid.id + 3439 ((nexus->targ_port - CTL_MAX_PORTS) * 3440 CTL_MAX_INIT_PER_PORT)); 3441} 3442 3443uint32_t 3444ctl_get_resindex(struct ctl_nexus *nexus) 3445{ 3446 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3447} 3448 3449uint32_t 3450ctl_port_idx(int port_num) 3451{ 3452 if (port_num < CTL_MAX_PORTS) 3453 return(port_num); 3454 else 3455 return(port_num - CTL_MAX_PORTS); 3456} 3457 3458static uint32_t 3459ctl_map_lun(int port_num, uint32_t lun_id) 3460{ 3461 struct ctl_port *port; 3462 3463 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3464 if (port == NULL) 3465 return (UINT32_MAX); 3466 if (port->lun_map == NULL) 3467 return (lun_id); 3468 return (port->lun_map(port->targ_lun_arg, lun_id)); 3469} 3470 3471static uint32_t 3472ctl_map_lun_back(int port_num, uint32_t lun_id) 3473{ 3474 struct ctl_port *port; 3475 uint32_t i; 3476 3477 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3478 if (port->lun_map == NULL) 3479 return (lun_id); 3480 for (i = 0; i < CTL_MAX_LUNS; i++) { 3481 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3482 return (i); 3483 } 3484 return (UINT32_MAX); 3485} 3486 3487/* 3488 * Note: This only works for bitmask sizes that are at least 32 bits, and 3489 * that are a power of 2. 3490 */ 3491int 3492ctl_ffz(uint32_t *mask, uint32_t size) 3493{ 3494 uint32_t num_chunks, num_pieces; 3495 int i, j; 3496 3497 num_chunks = (size >> 5); 3498 if (num_chunks == 0) 3499 num_chunks++; 3500 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3501 3502 for (i = 0; i < num_chunks; i++) { 3503 for (j = 0; j < num_pieces; j++) { 3504 if ((mask[i] & (1 << j)) == 0) 3505 return ((i << 5) + j); 3506 } 3507 } 3508 3509 return (-1); 3510} 3511 3512int 3513ctl_set_mask(uint32_t *mask, uint32_t bit) 3514{ 3515 uint32_t chunk, piece; 3516 3517 chunk = bit >> 5; 3518 piece = bit % (sizeof(uint32_t) * 8); 3519 3520 if ((mask[chunk] & (1 << piece)) != 0) 3521 return (-1); 3522 else 3523 mask[chunk] |= (1 << piece); 3524 3525 return (0); 3526} 3527 3528int 3529ctl_clear_mask(uint32_t *mask, uint32_t bit) 3530{ 3531 uint32_t chunk, piece; 3532 3533 chunk = bit >> 5; 3534 piece = bit % (sizeof(uint32_t) * 8); 3535 3536 if ((mask[chunk] & (1 << piece)) == 0) 3537 return (-1); 3538 else 3539 mask[chunk] &= ~(1 << piece); 3540 3541 return (0); 3542} 3543 3544int 3545ctl_is_set(uint32_t *mask, uint32_t bit) 3546{ 3547 uint32_t chunk, piece; 3548 3549 chunk = bit >> 5; 3550 piece = bit % (sizeof(uint32_t) * 8); 3551 3552 if ((mask[chunk] & (1 << piece)) == 0) 3553 return (0); 3554 else 3555 return (1); 3556} 3557 3558#ifdef unused 3559/* 3560 * The bus, target and lun are optional, they can be filled in later. 3561 * can_wait is used to determine whether we can wait on the malloc or not. 3562 */ 3563union ctl_io* 3564ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3565 uint32_t targ_lun, int can_wait) 3566{ 3567 union ctl_io *io; 3568 3569 if (can_wait) 3570 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3571 else 3572 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3573 3574 if (io != NULL) { 3575 io->io_hdr.io_type = io_type; 3576 io->io_hdr.targ_port = targ_port; 3577 /* 3578 * XXX KDM this needs to change/go away. We need to move 3579 * to a preallocated pool of ctl_scsiio structures. 3580 */ 3581 io->io_hdr.nexus.targ_target.id = targ_target; 3582 io->io_hdr.nexus.targ_lun = targ_lun; 3583 } 3584 3585 return (io); 3586} 3587 3588void 3589ctl_kfree_io(union ctl_io *io) 3590{ 3591 free(io, M_CTL); 3592} 3593#endif /* unused */ 3594 3595/* 3596 * ctl_softc, pool_type, total_ctl_io are passed in. 3597 * npool is passed out. 3598 */ 3599int 3600ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3601 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3602{ 3603 uint32_t i; 3604 union ctl_io *cur_io, *next_io; 3605 struct ctl_io_pool *pool; 3606 int retval; 3607 3608 retval = 0; 3609 3610 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3611 M_NOWAIT | M_ZERO); 3612 if (pool == NULL) { 3613 retval = ENOMEM; 3614 goto bailout; 3615 } 3616 3617 pool->type = pool_type; 3618 pool->ctl_softc = ctl_softc; 3619 3620 mtx_lock(&ctl_softc->pool_lock); 3621 pool->id = ctl_softc->cur_pool_id++; 3622 mtx_unlock(&ctl_softc->pool_lock); 3623 3624 pool->flags = CTL_POOL_FLAG_NONE; 3625 pool->refcount = 1; /* Reference for validity. */ 3626 STAILQ_INIT(&pool->free_queue); 3627 3628 /* 3629 * XXX KDM other options here: 3630 * - allocate a page at a time 3631 * - allocate one big chunk of memory. 3632 * Page allocation might work well, but would take a little more 3633 * tracking. 3634 */ 3635 for (i = 0; i < total_ctl_io; i++) { 3636 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3637 M_NOWAIT); 3638 if (cur_io == NULL) { 3639 retval = ENOMEM; 3640 break; 3641 } 3642 cur_io->io_hdr.pool = pool; 3643 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3644 pool->total_ctl_io++; 3645 pool->free_ctl_io++; 3646 } 3647 3648 if (retval != 0) { 3649 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3650 cur_io != NULL; cur_io = next_io) { 3651 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3652 links); 3653 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3654 ctl_io_hdr, links); 3655 free(cur_io, M_CTLIO); 3656 } 3657 3658 free(pool, M_CTL); 3659 goto bailout; 3660 } 3661 mtx_lock(&ctl_softc->pool_lock); 3662 ctl_softc->num_pools++; 3663 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3664 /* 3665 * Increment our usage count if this is an external consumer, so we 3666 * can't get unloaded until the external consumer (most likely a 3667 * FETD) unloads and frees his pool. 3668 * 3669 * XXX KDM will this increment the caller's module use count, or 3670 * mine? 3671 */ 3672#if 0 3673 if ((pool_type != CTL_POOL_EMERGENCY) 3674 && (pool_type != CTL_POOL_INTERNAL) 3675 && (pool_type != CTL_POOL_4OTHERSC)) 3676 MOD_INC_USE_COUNT; 3677#endif 3678 3679 mtx_unlock(&ctl_softc->pool_lock); 3680 3681 *npool = pool; 3682 3683bailout: 3684 3685 return (retval); 3686} 3687 3688static int 3689ctl_pool_acquire(struct ctl_io_pool *pool) 3690{ 3691 3692 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3693 3694 if (pool->flags & CTL_POOL_FLAG_INVALID) 3695 return (EINVAL); 3696 3697 pool->refcount++; 3698 3699 return (0); 3700} 3701 3702static void 3703ctl_pool_release(struct ctl_io_pool *pool) 3704{ 3705 struct ctl_softc *ctl_softc = pool->ctl_softc; 3706 union ctl_io *io; 3707 3708 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3709 3710 if (--pool->refcount != 0) 3711 return; 3712 3713 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3714 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3715 links); 3716 free(io, M_CTLIO); 3717 } 3718 3719 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3720 ctl_softc->num_pools--; 3721 3722 /* 3723 * XXX KDM will this decrement the caller's usage count or mine? 3724 */ 3725#if 0 3726 if ((pool->type != CTL_POOL_EMERGENCY) 3727 && (pool->type != CTL_POOL_INTERNAL) 3728 && (pool->type != CTL_POOL_4OTHERSC)) 3729 MOD_DEC_USE_COUNT; 3730#endif 3731 3732 free(pool, M_CTL); 3733} 3734 3735void 3736ctl_pool_free(struct ctl_io_pool *pool) 3737{ 3738 struct ctl_softc *ctl_softc; 3739 3740 if (pool == NULL) 3741 return; 3742 3743 ctl_softc = pool->ctl_softc; 3744 mtx_lock(&ctl_softc->pool_lock); 3745 pool->flags |= CTL_POOL_FLAG_INVALID; 3746 ctl_pool_release(pool); 3747 mtx_unlock(&ctl_softc->pool_lock); 3748} 3749 3750/* 3751 * This routine does not block (except for spinlocks of course). 3752 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3753 * possible. 3754 */ 3755union ctl_io * 3756ctl_alloc_io(void *pool_ref) 3757{ 3758 union ctl_io *io; 3759 struct ctl_softc *ctl_softc; 3760 struct ctl_io_pool *pool, *npool; 3761 struct ctl_io_pool *emergency_pool; 3762 3763 pool = (struct ctl_io_pool *)pool_ref; 3764 3765 if (pool == NULL) { 3766 printf("%s: pool is NULL\n", __func__); 3767 return (NULL); 3768 } 3769 3770 emergency_pool = NULL; 3771 3772 ctl_softc = pool->ctl_softc; 3773 3774 mtx_lock(&ctl_softc->pool_lock); 3775 /* 3776 * First, try to get the io structure from the user's pool. 3777 */ 3778 if (ctl_pool_acquire(pool) == 0) { 3779 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3780 if (io != NULL) { 3781 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3782 pool->total_allocated++; 3783 pool->free_ctl_io--; 3784 mtx_unlock(&ctl_softc->pool_lock); 3785 return (io); 3786 } else 3787 ctl_pool_release(pool); 3788 } 3789 /* 3790 * If he doesn't have any io structures left, search for an 3791 * emergency pool and grab one from there. 3792 */ 3793 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3794 if (npool->type != CTL_POOL_EMERGENCY) 3795 continue; 3796 3797 if (ctl_pool_acquire(npool) != 0) 3798 continue; 3799 3800 emergency_pool = npool; 3801 3802 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3803 if (io != NULL) { 3804 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3805 npool->total_allocated++; 3806 npool->free_ctl_io--; 3807 mtx_unlock(&ctl_softc->pool_lock); 3808 return (io); 3809 } else 3810 ctl_pool_release(npool); 3811 } 3812 3813 /* Drop the spinlock before we malloc */ 3814 mtx_unlock(&ctl_softc->pool_lock); 3815 3816 /* 3817 * The emergency pool (if it exists) didn't have one, so try an 3818 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3819 */ 3820 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3821 if (io != NULL) { 3822 /* 3823 * If the emergency pool exists but is empty, add this 3824 * ctl_io to its list when it gets freed. 3825 */ 3826 if (emergency_pool != NULL) { 3827 mtx_lock(&ctl_softc->pool_lock); 3828 if (ctl_pool_acquire(emergency_pool) == 0) { 3829 io->io_hdr.pool = emergency_pool; 3830 emergency_pool->total_ctl_io++; 3831 /* 3832 * Need to bump this, otherwise 3833 * total_allocated and total_freed won't 3834 * match when we no longer have anything 3835 * outstanding. 3836 */ 3837 emergency_pool->total_allocated++; 3838 } 3839 mtx_unlock(&ctl_softc->pool_lock); 3840 } else 3841 io->io_hdr.pool = NULL; 3842 } 3843 3844 return (io); 3845} 3846 3847void 3848ctl_free_io(union ctl_io *io) 3849{ 3850 if (io == NULL) 3851 return; 3852 3853 /* 3854 * If this ctl_io has a pool, return it to that pool. 3855 */ 3856 if (io->io_hdr.pool != NULL) { 3857 struct ctl_io_pool *pool; 3858 3859 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3860 mtx_lock(&pool->ctl_softc->pool_lock); 3861 io->io_hdr.io_type = 0xff; 3862 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3863 pool->total_freed++; 3864 pool->free_ctl_io++; 3865 ctl_pool_release(pool); 3866 mtx_unlock(&pool->ctl_softc->pool_lock); 3867 } else { 3868 /* 3869 * Otherwise, just free it. We probably malloced it and 3870 * the emergency pool wasn't available. 3871 */ 3872 free(io, M_CTLIO); 3873 } 3874 3875} 3876 3877void 3878ctl_zero_io(union ctl_io *io) 3879{ 3880 void *pool_ref; 3881 3882 if (io == NULL) 3883 return; 3884 3885 /* 3886 * May need to preserve linked list pointers at some point too. 3887 */ 3888 pool_ref = io->io_hdr.pool; 3889 3890 memset(io, 0, sizeof(*io)); 3891 3892 io->io_hdr.pool = pool_ref; 3893} 3894 3895/* 3896 * This routine is currently used for internal copies of ctl_ios that need 3897 * to persist for some reason after we've already returned status to the 3898 * FETD. (Thus the flag set.) 3899 * 3900 * XXX XXX 3901 * Note that this makes a blind copy of all fields in the ctl_io, except 3902 * for the pool reference. This includes any memory that has been 3903 * allocated! That memory will no longer be valid after done has been 3904 * called, so this would be VERY DANGEROUS for command that actually does 3905 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3906 * start and stop commands, which don't transfer any data, so this is not a 3907 * problem. If it is used for anything else, the caller would also need to 3908 * allocate data buffer space and this routine would need to be modified to 3909 * copy the data buffer(s) as well. 3910 */ 3911void 3912ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3913{ 3914 void *pool_ref; 3915 3916 if ((src == NULL) 3917 || (dest == NULL)) 3918 return; 3919 3920 /* 3921 * May need to preserve linked list pointers at some point too. 3922 */ 3923 pool_ref = dest->io_hdr.pool; 3924 3925 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3926 3927 dest->io_hdr.pool = pool_ref; 3928 /* 3929 * We need to know that this is an internal copy, and doesn't need 3930 * to get passed back to the FETD that allocated it. 3931 */ 3932 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3933} 3934 3935#ifdef NEEDTOPORT 3936static void 3937ctl_update_power_subpage(struct copan_power_subpage *page) 3938{ 3939 int num_luns, num_partitions, config_type; 3940 struct ctl_softc *softc; 3941 cs_BOOL_t aor_present, shelf_50pct_power; 3942 cs_raidset_personality_t rs_type; 3943 int max_active_luns; 3944 3945 softc = control_softc; 3946 3947 /* subtract out the processor LUN */ 3948 num_luns = softc->num_luns - 1; 3949 /* 3950 * Default to 7 LUNs active, which was the only number we allowed 3951 * in the past. 3952 */ 3953 max_active_luns = 7; 3954 3955 num_partitions = config_GetRsPartitionInfo(); 3956 config_type = config_GetConfigType(); 3957 shelf_50pct_power = config_GetShelfPowerMode(); 3958 aor_present = config_IsAorRsPresent(); 3959 3960 rs_type = ddb_GetRsRaidType(1); 3961 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3962 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3963 EPRINT(0, "Unsupported RS type %d!", rs_type); 3964 } 3965 3966 3967 page->total_luns = num_luns; 3968 3969 switch (config_type) { 3970 case 40: 3971 /* 3972 * In a 40 drive configuration, it doesn't matter what DC 3973 * cards we have, whether we have AOR enabled or not, 3974 * partitioning or not, or what type of RAIDset we have. 3975 * In that scenario, we can power up every LUN we present 3976 * to the user. 3977 */ 3978 max_active_luns = num_luns; 3979 3980 break; 3981 case 64: 3982 if (shelf_50pct_power == CS_FALSE) { 3983 /* 25% power */ 3984 if (aor_present == CS_TRUE) { 3985 if (rs_type == 3986 CS_RAIDSET_PERSONALITY_RAID5) { 3987 max_active_luns = 7; 3988 } else if (rs_type == 3989 CS_RAIDSET_PERSONALITY_RAID1){ 3990 max_active_luns = 14; 3991 } else { 3992 /* XXX KDM now what?? */ 3993 } 3994 } else { 3995 if (rs_type == 3996 CS_RAIDSET_PERSONALITY_RAID5) { 3997 max_active_luns = 8; 3998 } else if (rs_type == 3999 CS_RAIDSET_PERSONALITY_RAID1){ 4000 max_active_luns = 16; 4001 } else { 4002 /* XXX KDM now what?? */ 4003 } 4004 } 4005 } else { 4006 /* 50% power */ 4007 /* 4008 * With 50% power in a 64 drive configuration, we 4009 * can power all LUNs we present. 4010 */ 4011 max_active_luns = num_luns; 4012 } 4013 break; 4014 case 112: 4015 if (shelf_50pct_power == CS_FALSE) { 4016 /* 25% power */ 4017 if (aor_present == CS_TRUE) { 4018 if (rs_type == 4019 CS_RAIDSET_PERSONALITY_RAID5) { 4020 max_active_luns = 7; 4021 } else if (rs_type == 4022 CS_RAIDSET_PERSONALITY_RAID1){ 4023 max_active_luns = 14; 4024 } else { 4025 /* XXX KDM now what?? */ 4026 } 4027 } else { 4028 if (rs_type == 4029 CS_RAIDSET_PERSONALITY_RAID5) { 4030 max_active_luns = 8; 4031 } else if (rs_type == 4032 CS_RAIDSET_PERSONALITY_RAID1){ 4033 max_active_luns = 16; 4034 } else { 4035 /* XXX KDM now what?? */ 4036 } 4037 } 4038 } else { 4039 /* 50% power */ 4040 if (aor_present == CS_TRUE) { 4041 if (rs_type == 4042 CS_RAIDSET_PERSONALITY_RAID5) { 4043 max_active_luns = 14; 4044 } else if (rs_type == 4045 CS_RAIDSET_PERSONALITY_RAID1){ 4046 /* 4047 * We're assuming here that disk 4048 * caching is enabled, and so we're 4049 * able to power up half of each 4050 * LUN, and cache all writes. 4051 */ 4052 max_active_luns = num_luns; 4053 } else { 4054 /* XXX KDM now what?? */ 4055 } 4056 } else { 4057 if (rs_type == 4058 CS_RAIDSET_PERSONALITY_RAID5) { 4059 max_active_luns = 15; 4060 } else if (rs_type == 4061 CS_RAIDSET_PERSONALITY_RAID1){ 4062 max_active_luns = 30; 4063 } else { 4064 /* XXX KDM now what?? */ 4065 } 4066 } 4067 } 4068 break; 4069 default: 4070 /* 4071 * In this case, we have an unknown configuration, so we 4072 * just use the default from above. 4073 */ 4074 break; 4075 } 4076 4077 page->max_active_luns = max_active_luns; 4078#if 0 4079 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4080 page->total_luns, page->max_active_luns); 4081#endif 4082} 4083#endif /* NEEDTOPORT */ 4084 4085/* 4086 * This routine could be used in the future to load default and/or saved 4087 * mode page parameters for a particuar lun. 4088 */ 4089static int 4090ctl_init_page_index(struct ctl_lun *lun) 4091{ 4092 int i; 4093 struct ctl_page_index *page_index; 4094 struct ctl_softc *softc; 4095 4096 memcpy(&lun->mode_pages.index, page_index_template, 4097 sizeof(page_index_template)); 4098 4099 softc = lun->ctl_softc; 4100 4101 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4102 4103 page_index = &lun->mode_pages.index[i]; 4104 /* 4105 * If this is a disk-only mode page, there's no point in 4106 * setting it up. For some pages, we have to have some 4107 * basic information about the disk in order to calculate the 4108 * mode page data. 4109 */ 4110 if ((lun->be_lun->lun_type != T_DIRECT) 4111 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4112 continue; 4113 4114 switch (page_index->page_code & SMPH_PC_MASK) { 4115 case SMS_FORMAT_DEVICE_PAGE: { 4116 struct scsi_format_page *format_page; 4117 4118 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4119 panic("subpage is incorrect!"); 4120 4121 /* 4122 * Sectors per track are set above. Bytes per 4123 * sector need to be set here on a per-LUN basis. 4124 */ 4125 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4126 &format_page_default, 4127 sizeof(format_page_default)); 4128 memcpy(&lun->mode_pages.format_page[ 4129 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4130 sizeof(format_page_changeable)); 4131 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4132 &format_page_default, 4133 sizeof(format_page_default)); 4134 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4135 &format_page_default, 4136 sizeof(format_page_default)); 4137 4138 format_page = &lun->mode_pages.format_page[ 4139 CTL_PAGE_CURRENT]; 4140 scsi_ulto2b(lun->be_lun->blocksize, 4141 format_page->bytes_per_sector); 4142 4143 format_page = &lun->mode_pages.format_page[ 4144 CTL_PAGE_DEFAULT]; 4145 scsi_ulto2b(lun->be_lun->blocksize, 4146 format_page->bytes_per_sector); 4147 4148 format_page = &lun->mode_pages.format_page[ 4149 CTL_PAGE_SAVED]; 4150 scsi_ulto2b(lun->be_lun->blocksize, 4151 format_page->bytes_per_sector); 4152 4153 page_index->page_data = 4154 (uint8_t *)lun->mode_pages.format_page; 4155 break; 4156 } 4157 case SMS_RIGID_DISK_PAGE: { 4158 struct scsi_rigid_disk_page *rigid_disk_page; 4159 uint32_t sectors_per_cylinder; 4160 uint64_t cylinders; 4161#ifndef __XSCALE__ 4162 int shift; 4163#endif /* !__XSCALE__ */ 4164 4165 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4166 panic("invalid subpage value %d", 4167 page_index->subpage); 4168 4169 /* 4170 * Rotation rate and sectors per track are set 4171 * above. We calculate the cylinders here based on 4172 * capacity. Due to the number of heads and 4173 * sectors per track we're using, smaller arrays 4174 * may turn out to have 0 cylinders. Linux and 4175 * FreeBSD don't pay attention to these mode pages 4176 * to figure out capacity, but Solaris does. It 4177 * seems to deal with 0 cylinders just fine, and 4178 * works out a fake geometry based on the capacity. 4179 */ 4180 memcpy(&lun->mode_pages.rigid_disk_page[ 4181 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4182 sizeof(rigid_disk_page_default)); 4183 memcpy(&lun->mode_pages.rigid_disk_page[ 4184 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4185 sizeof(rigid_disk_page_changeable)); 4186 memcpy(&lun->mode_pages.rigid_disk_page[ 4187 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4188 sizeof(rigid_disk_page_default)); 4189 memcpy(&lun->mode_pages.rigid_disk_page[ 4190 CTL_PAGE_SAVED], &rigid_disk_page_default, 4191 sizeof(rigid_disk_page_default)); 4192 4193 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4194 CTL_DEFAULT_HEADS; 4195 4196 /* 4197 * The divide method here will be more accurate, 4198 * probably, but results in floating point being 4199 * used in the kernel on i386 (__udivdi3()). On the 4200 * XScale, though, __udivdi3() is implemented in 4201 * software. 4202 * 4203 * The shift method for cylinder calculation is 4204 * accurate if sectors_per_cylinder is a power of 4205 * 2. Otherwise it might be slightly off -- you 4206 * might have a bit of a truncation problem. 4207 */ 4208#ifdef __XSCALE__ 4209 cylinders = (lun->be_lun->maxlba + 1) / 4210 sectors_per_cylinder; 4211#else 4212 for (shift = 31; shift > 0; shift--) { 4213 if (sectors_per_cylinder & (1 << shift)) 4214 break; 4215 } 4216 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4217#endif 4218 4219 /* 4220 * We've basically got 3 bytes, or 24 bits for the 4221 * cylinder size in the mode page. If we're over, 4222 * just round down to 2^24. 4223 */ 4224 if (cylinders > 0xffffff) 4225 cylinders = 0xffffff; 4226 4227 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4228 CTL_PAGE_CURRENT]; 4229 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4230 4231 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4232 CTL_PAGE_DEFAULT]; 4233 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4234 4235 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4236 CTL_PAGE_SAVED]; 4237 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4238 4239 page_index->page_data = 4240 (uint8_t *)lun->mode_pages.rigid_disk_page; 4241 break; 4242 } 4243 case SMS_CACHING_PAGE: { 4244 4245 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4246 panic("invalid subpage value %d", 4247 page_index->subpage); 4248 /* 4249 * Defaults should be okay here, no calculations 4250 * needed. 4251 */ 4252 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4253 &caching_page_default, 4254 sizeof(caching_page_default)); 4255 memcpy(&lun->mode_pages.caching_page[ 4256 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4257 sizeof(caching_page_changeable)); 4258 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4259 &caching_page_default, 4260 sizeof(caching_page_default)); 4261 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4262 &caching_page_default, 4263 sizeof(caching_page_default)); 4264 page_index->page_data = 4265 (uint8_t *)lun->mode_pages.caching_page; 4266 break; 4267 } 4268 case SMS_CONTROL_MODE_PAGE: { 4269 4270 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4271 panic("invalid subpage value %d", 4272 page_index->subpage); 4273 4274 /* 4275 * Defaults should be okay here, no calculations 4276 * needed. 4277 */ 4278 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4279 &control_page_default, 4280 sizeof(control_page_default)); 4281 memcpy(&lun->mode_pages.control_page[ 4282 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4283 sizeof(control_page_changeable)); 4284 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4285 &control_page_default, 4286 sizeof(control_page_default)); 4287 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4288 &control_page_default, 4289 sizeof(control_page_default)); 4290 page_index->page_data = 4291 (uint8_t *)lun->mode_pages.control_page; 4292 break; 4293 4294 } 4295 case SMS_VENDOR_SPECIFIC_PAGE:{ 4296 switch (page_index->subpage) { 4297 case PWR_SUBPAGE_CODE: { 4298 struct copan_power_subpage *current_page, 4299 *saved_page; 4300 4301 memcpy(&lun->mode_pages.power_subpage[ 4302 CTL_PAGE_CURRENT], 4303 &power_page_default, 4304 sizeof(power_page_default)); 4305 memcpy(&lun->mode_pages.power_subpage[ 4306 CTL_PAGE_CHANGEABLE], 4307 &power_page_changeable, 4308 sizeof(power_page_changeable)); 4309 memcpy(&lun->mode_pages.power_subpage[ 4310 CTL_PAGE_DEFAULT], 4311 &power_page_default, 4312 sizeof(power_page_default)); 4313 memcpy(&lun->mode_pages.power_subpage[ 4314 CTL_PAGE_SAVED], 4315 &power_page_default, 4316 sizeof(power_page_default)); 4317 page_index->page_data = 4318 (uint8_t *)lun->mode_pages.power_subpage; 4319 4320 current_page = (struct copan_power_subpage *) 4321 (page_index->page_data + 4322 (page_index->page_len * 4323 CTL_PAGE_CURRENT)); 4324 saved_page = (struct copan_power_subpage *) 4325 (page_index->page_data + 4326 (page_index->page_len * 4327 CTL_PAGE_SAVED)); 4328 break; 4329 } 4330 case APS_SUBPAGE_CODE: { 4331 struct copan_aps_subpage *current_page, 4332 *saved_page; 4333 4334 // This gets set multiple times but 4335 // it should always be the same. It's 4336 // only done during init so who cares. 4337 index_to_aps_page = i; 4338 4339 memcpy(&lun->mode_pages.aps_subpage[ 4340 CTL_PAGE_CURRENT], 4341 &aps_page_default, 4342 sizeof(aps_page_default)); 4343 memcpy(&lun->mode_pages.aps_subpage[ 4344 CTL_PAGE_CHANGEABLE], 4345 &aps_page_changeable, 4346 sizeof(aps_page_changeable)); 4347 memcpy(&lun->mode_pages.aps_subpage[ 4348 CTL_PAGE_DEFAULT], 4349 &aps_page_default, 4350 sizeof(aps_page_default)); 4351 memcpy(&lun->mode_pages.aps_subpage[ 4352 CTL_PAGE_SAVED], 4353 &aps_page_default, 4354 sizeof(aps_page_default)); 4355 page_index->page_data = 4356 (uint8_t *)lun->mode_pages.aps_subpage; 4357 4358 current_page = (struct copan_aps_subpage *) 4359 (page_index->page_data + 4360 (page_index->page_len * 4361 CTL_PAGE_CURRENT)); 4362 saved_page = (struct copan_aps_subpage *) 4363 (page_index->page_data + 4364 (page_index->page_len * 4365 CTL_PAGE_SAVED)); 4366 break; 4367 } 4368 case DBGCNF_SUBPAGE_CODE: { 4369 struct copan_debugconf_subpage *current_page, 4370 *saved_page; 4371 4372 memcpy(&lun->mode_pages.debugconf_subpage[ 4373 CTL_PAGE_CURRENT], 4374 &debugconf_page_default, 4375 sizeof(debugconf_page_default)); 4376 memcpy(&lun->mode_pages.debugconf_subpage[ 4377 CTL_PAGE_CHANGEABLE], 4378 &debugconf_page_changeable, 4379 sizeof(debugconf_page_changeable)); 4380 memcpy(&lun->mode_pages.debugconf_subpage[ 4381 CTL_PAGE_DEFAULT], 4382 &debugconf_page_default, 4383 sizeof(debugconf_page_default)); 4384 memcpy(&lun->mode_pages.debugconf_subpage[ 4385 CTL_PAGE_SAVED], 4386 &debugconf_page_default, 4387 sizeof(debugconf_page_default)); 4388 page_index->page_data = 4389 (uint8_t *)lun->mode_pages.debugconf_subpage; 4390 4391 current_page = (struct copan_debugconf_subpage *) 4392 (page_index->page_data + 4393 (page_index->page_len * 4394 CTL_PAGE_CURRENT)); 4395 saved_page = (struct copan_debugconf_subpage *) 4396 (page_index->page_data + 4397 (page_index->page_len * 4398 CTL_PAGE_SAVED)); 4399 break; 4400 } 4401 default: 4402 panic("invalid subpage value %d", 4403 page_index->subpage); 4404 break; 4405 } 4406 break; 4407 } 4408 default: 4409 panic("invalid page value %d", 4410 page_index->page_code & SMPH_PC_MASK); 4411 break; 4412 } 4413 } 4414 4415 return (CTL_RETVAL_COMPLETE); 4416} 4417 4418/* 4419 * LUN allocation. 4420 * 4421 * Requirements: 4422 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4423 * wants us to allocate the LUN and he can block. 4424 * - ctl_softc is always set 4425 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4426 * 4427 * Returns 0 for success, non-zero (errno) for failure. 4428 */ 4429static int 4430ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4431 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4432{ 4433 struct ctl_lun *nlun, *lun; 4434 struct ctl_port *port; 4435 struct scsi_vpd_id_descriptor *desc; 4436 struct scsi_vpd_id_t10 *t10id; 4437 const char *eui, *naa, *scsiname, *vendor; 4438 int lun_number, i, lun_malloced; 4439 int devidlen, idlen1, idlen2 = 0, len; 4440 4441 if (be_lun == NULL) 4442 return (EINVAL); 4443 4444 /* 4445 * We currently only support Direct Access or Processor LUN types. 4446 */ 4447 switch (be_lun->lun_type) { 4448 case T_DIRECT: 4449 break; 4450 case T_PROCESSOR: 4451 break; 4452 case T_SEQUENTIAL: 4453 case T_CHANGER: 4454 default: 4455 be_lun->lun_config_status(be_lun->be_lun, 4456 CTL_LUN_CONFIG_FAILURE); 4457 break; 4458 } 4459 if (ctl_lun == NULL) { 4460 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4461 lun_malloced = 1; 4462 } else { 4463 lun_malloced = 0; 4464 lun = ctl_lun; 4465 } 4466 4467 memset(lun, 0, sizeof(*lun)); 4468 if (lun_malloced) 4469 lun->flags = CTL_LUN_MALLOCED; 4470 4471 /* Generate LUN ID. */ 4472 devidlen = max(CTL_DEVID_MIN_LEN, 4473 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4474 idlen1 = sizeof(*t10id) + devidlen; 4475 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4476 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4477 if (scsiname != NULL) { 4478 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4479 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4480 } 4481 eui = ctl_get_opt(&be_lun->options, "eui"); 4482 if (eui != NULL) { 4483 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4484 } 4485 naa = ctl_get_opt(&be_lun->options, "naa"); 4486 if (naa != NULL) { 4487 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4488 } 4489 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4490 M_CTL, M_WAITOK | M_ZERO); 4491 lun->lun_devid->len = len; 4492 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4493 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4494 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4495 desc->length = idlen1; 4496 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4497 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4498 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4499 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4500 } else { 4501 strncpy(t10id->vendor, vendor, 4502 min(sizeof(t10id->vendor), strlen(vendor))); 4503 } 4504 strncpy((char *)t10id->vendor_spec_id, 4505 (char *)be_lun->device_id, devidlen); 4506 if (scsiname != NULL) { 4507 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4508 desc->length); 4509 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4510 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4511 SVPD_ID_TYPE_SCSI_NAME; 4512 desc->length = idlen2; 4513 strlcpy(desc->identifier, scsiname, idlen2); 4514 } 4515 if (eui != NULL) { 4516 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4517 desc->length); 4518 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4519 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4520 SVPD_ID_TYPE_EUI64; 4521 desc->length = 8; 4522 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4523 } 4524 if (naa != NULL) { 4525 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4526 desc->length); 4527 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4528 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4529 SVPD_ID_TYPE_NAA; 4530 desc->length = 8; 4531 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4532 } 4533 4534 mtx_lock(&ctl_softc->ctl_lock); 4535 /* 4536 * See if the caller requested a particular LUN number. If so, see 4537 * if it is available. Otherwise, allocate the first available LUN. 4538 */ 4539 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4540 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4541 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4542 mtx_unlock(&ctl_softc->ctl_lock); 4543 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4544 printf("ctl: requested LUN ID %d is higher " 4545 "than CTL_MAX_LUNS - 1 (%d)\n", 4546 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4547 } else { 4548 /* 4549 * XXX KDM return an error, or just assign 4550 * another LUN ID in this case?? 4551 */ 4552 printf("ctl: requested LUN ID %d is already " 4553 "in use\n", be_lun->req_lun_id); 4554 } 4555 if (lun->flags & CTL_LUN_MALLOCED) 4556 free(lun, M_CTL); 4557 be_lun->lun_config_status(be_lun->be_lun, 4558 CTL_LUN_CONFIG_FAILURE); 4559 return (ENOSPC); 4560 } 4561 lun_number = be_lun->req_lun_id; 4562 } else { 4563 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4564 if (lun_number == -1) { 4565 mtx_unlock(&ctl_softc->ctl_lock); 4566 printf("ctl: can't allocate LUN on target %ju, out of " 4567 "LUNs\n", (uintmax_t)target_id.id); 4568 if (lun->flags & CTL_LUN_MALLOCED) 4569 free(lun, M_CTL); 4570 be_lun->lun_config_status(be_lun->be_lun, 4571 CTL_LUN_CONFIG_FAILURE); 4572 return (ENOSPC); 4573 } 4574 } 4575 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4576 4577 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4578 lun->target = target_id; 4579 lun->lun = lun_number; 4580 lun->be_lun = be_lun; 4581 /* 4582 * The processor LUN is always enabled. Disk LUNs come on line 4583 * disabled, and must be enabled by the backend. 4584 */ 4585 lun->flags |= CTL_LUN_DISABLED; 4586 lun->backend = be_lun->be; 4587 be_lun->ctl_lun = lun; 4588 be_lun->lun_id = lun_number; 4589 atomic_add_int(&be_lun->be->num_luns, 1); 4590 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4591 lun->flags |= CTL_LUN_STOPPED; 4592 4593 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4594 lun->flags |= CTL_LUN_INOPERABLE; 4595 4596 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4597 lun->flags |= CTL_LUN_PRIMARY_SC; 4598 4599 lun->ctl_softc = ctl_softc; 4600 TAILQ_INIT(&lun->ooa_queue); 4601 TAILQ_INIT(&lun->blocked_queue); 4602 STAILQ_INIT(&lun->error_list); 4603 ctl_tpc_lun_init(lun); 4604 4605 /* 4606 * Initialize the mode page index. 4607 */ 4608 ctl_init_page_index(lun); 4609 4610 /* 4611 * Set the poweron UA for all initiators on this LUN only. 4612 */ 4613 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4614 lun->pending_ua[i] = CTL_UA_POWERON; 4615 4616 /* 4617 * Now, before we insert this lun on the lun list, set the lun 4618 * inventory changed UA for all other luns. 4619 */ 4620 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4621 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4622 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4623 } 4624 } 4625 4626 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4627 4628 ctl_softc->ctl_luns[lun_number] = lun; 4629 4630 ctl_softc->num_luns++; 4631 4632 /* Setup statistics gathering */ 4633 lun->stats.device_type = be_lun->lun_type; 4634 lun->stats.lun_number = lun_number; 4635 if (lun->stats.device_type == T_DIRECT) 4636 lun->stats.blocksize = be_lun->blocksize; 4637 else 4638 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4639 for (i = 0;i < CTL_MAX_PORTS;i++) 4640 lun->stats.ports[i].targ_port = i; 4641 4642 mtx_unlock(&ctl_softc->ctl_lock); 4643 4644 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4645 4646 /* 4647 * Run through each registered FETD and bring it online if it isn't 4648 * already. Enable the target ID if it hasn't been enabled, and 4649 * enable this particular LUN. 4650 */ 4651 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4652 int retval; 4653 4654 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4655 if (retval != 0) { 4656 printf("ctl_alloc_lun: FETD %s port %d returned error " 4657 "%d for lun_enable on target %ju lun %d\n", 4658 port->port_name, port->targ_port, retval, 4659 (uintmax_t)target_id.id, lun_number); 4660 } else 4661 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4662 } 4663 return (0); 4664} 4665 4666/* 4667 * Delete a LUN. 4668 * Assumptions: 4669 * - LUN has already been marked invalid and any pending I/O has been taken 4670 * care of. 4671 */ 4672static int 4673ctl_free_lun(struct ctl_lun *lun) 4674{ 4675 struct ctl_softc *softc; 4676#if 0 4677 struct ctl_port *port; 4678#endif 4679 struct ctl_lun *nlun; 4680 int i; 4681 4682 softc = lun->ctl_softc; 4683 4684 mtx_assert(&softc->ctl_lock, MA_OWNED); 4685 4686 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4687 4688 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4689 4690 softc->ctl_luns[lun->lun] = NULL; 4691 4692 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4693 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4694 4695 softc->num_luns--; 4696 4697 /* 4698 * XXX KDM this scheme only works for a single target/multiple LUN 4699 * setup. It needs to be revamped for a multiple target scheme. 4700 * 4701 * XXX KDM this results in port->lun_disable() getting called twice, 4702 * once when ctl_disable_lun() is called, and a second time here. 4703 * We really need to re-think the LUN disable semantics. There 4704 * should probably be several steps/levels to LUN removal: 4705 * - disable 4706 * - invalidate 4707 * - free 4708 * 4709 * Right now we only have a disable method when communicating to 4710 * the front end ports, at least for individual LUNs. 4711 */ 4712#if 0 4713 STAILQ_FOREACH(port, &softc->port_list, links) { 4714 int retval; 4715 4716 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4717 lun->lun); 4718 if (retval != 0) { 4719 printf("ctl_free_lun: FETD %s port %d returned error " 4720 "%d for lun_disable on target %ju lun %jd\n", 4721 port->port_name, port->targ_port, retval, 4722 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4723 } 4724 4725 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4726 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4727 4728 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4729 if (retval != 0) { 4730 printf("ctl_free_lun: FETD %s port %d " 4731 "returned error %d for targ_disable on " 4732 "target %ju\n", port->port_name, 4733 port->targ_port, retval, 4734 (uintmax_t)lun->target.id); 4735 } else 4736 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4737 4738 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4739 continue; 4740 4741#if 0 4742 port->port_offline(port->onoff_arg); 4743 port->status &= ~CTL_PORT_STATUS_ONLINE; 4744#endif 4745 } 4746 } 4747#endif 4748 4749 /* 4750 * Tell the backend to free resources, if this LUN has a backend. 4751 */ 4752 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4753 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4754 4755 ctl_tpc_lun_shutdown(lun); 4756 mtx_destroy(&lun->lun_lock); 4757 free(lun->lun_devid, M_CTL); 4758 if (lun->flags & CTL_LUN_MALLOCED) 4759 free(lun, M_CTL); 4760 4761 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4762 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4763 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4764 } 4765 } 4766 4767 return (0); 4768} 4769 4770static void 4771ctl_create_lun(struct ctl_be_lun *be_lun) 4772{ 4773 struct ctl_softc *ctl_softc; 4774 4775 ctl_softc = control_softc; 4776 4777 /* 4778 * ctl_alloc_lun() should handle all potential failure cases. 4779 */ 4780 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4781} 4782 4783int 4784ctl_add_lun(struct ctl_be_lun *be_lun) 4785{ 4786 struct ctl_softc *ctl_softc = control_softc; 4787 4788 mtx_lock(&ctl_softc->ctl_lock); 4789 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4790 mtx_unlock(&ctl_softc->ctl_lock); 4791 wakeup(&ctl_softc->pending_lun_queue); 4792 4793 return (0); 4794} 4795 4796int 4797ctl_enable_lun(struct ctl_be_lun *be_lun) 4798{ 4799 struct ctl_softc *ctl_softc; 4800 struct ctl_port *port, *nport; 4801 struct ctl_lun *lun; 4802 int retval; 4803 4804 ctl_softc = control_softc; 4805 4806 lun = (struct ctl_lun *)be_lun->ctl_lun; 4807 4808 mtx_lock(&ctl_softc->ctl_lock); 4809 mtx_lock(&lun->lun_lock); 4810 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4811 /* 4812 * eh? Why did we get called if the LUN is already 4813 * enabled? 4814 */ 4815 mtx_unlock(&lun->lun_lock); 4816 mtx_unlock(&ctl_softc->ctl_lock); 4817 return (0); 4818 } 4819 lun->flags &= ~CTL_LUN_DISABLED; 4820 mtx_unlock(&lun->lun_lock); 4821 4822 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4823 nport = STAILQ_NEXT(port, links); 4824 4825 /* 4826 * Drop the lock while we call the FETD's enable routine. 4827 * This can lead to a callback into CTL (at least in the 4828 * case of the internal initiator frontend. 4829 */ 4830 mtx_unlock(&ctl_softc->ctl_lock); 4831 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4832 mtx_lock(&ctl_softc->ctl_lock); 4833 if (retval != 0) { 4834 printf("%s: FETD %s port %d returned error " 4835 "%d for lun_enable on target %ju lun %jd\n", 4836 __func__, port->port_name, port->targ_port, retval, 4837 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4838 } 4839#if 0 4840 else { 4841 /* NOTE: TODO: why does lun enable affect port status? */ 4842 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4843 } 4844#endif 4845 } 4846 4847 mtx_unlock(&ctl_softc->ctl_lock); 4848 4849 return (0); 4850} 4851 4852int 4853ctl_disable_lun(struct ctl_be_lun *be_lun) 4854{ 4855 struct ctl_softc *ctl_softc; 4856 struct ctl_port *port; 4857 struct ctl_lun *lun; 4858 int retval; 4859 4860 ctl_softc = control_softc; 4861 4862 lun = (struct ctl_lun *)be_lun->ctl_lun; 4863 4864 mtx_lock(&ctl_softc->ctl_lock); 4865 mtx_lock(&lun->lun_lock); 4866 if (lun->flags & CTL_LUN_DISABLED) { 4867 mtx_unlock(&lun->lun_lock); 4868 mtx_unlock(&ctl_softc->ctl_lock); 4869 return (0); 4870 } 4871 lun->flags |= CTL_LUN_DISABLED; 4872 mtx_unlock(&lun->lun_lock); 4873 4874 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4875 mtx_unlock(&ctl_softc->ctl_lock); 4876 /* 4877 * Drop the lock before we call the frontend's disable 4878 * routine, to avoid lock order reversals. 4879 * 4880 * XXX KDM what happens if the frontend list changes while 4881 * we're traversing it? It's unlikely, but should be handled. 4882 */ 4883 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4884 lun->lun); 4885 mtx_lock(&ctl_softc->ctl_lock); 4886 if (retval != 0) { 4887 printf("ctl_alloc_lun: FETD %s port %d returned error " 4888 "%d for lun_disable on target %ju lun %jd\n", 4889 port->port_name, port->targ_port, retval, 4890 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4891 } 4892 } 4893 4894 mtx_unlock(&ctl_softc->ctl_lock); 4895 4896 return (0); 4897} 4898 4899int 4900ctl_start_lun(struct ctl_be_lun *be_lun) 4901{ 4902 struct ctl_softc *ctl_softc; 4903 struct ctl_lun *lun; 4904 4905 ctl_softc = control_softc; 4906 4907 lun = (struct ctl_lun *)be_lun->ctl_lun; 4908 4909 mtx_lock(&lun->lun_lock); 4910 lun->flags &= ~CTL_LUN_STOPPED; 4911 mtx_unlock(&lun->lun_lock); 4912 4913 return (0); 4914} 4915 4916int 4917ctl_stop_lun(struct ctl_be_lun *be_lun) 4918{ 4919 struct ctl_softc *ctl_softc; 4920 struct ctl_lun *lun; 4921 4922 ctl_softc = control_softc; 4923 4924 lun = (struct ctl_lun *)be_lun->ctl_lun; 4925 4926 mtx_lock(&lun->lun_lock); 4927 lun->flags |= CTL_LUN_STOPPED; 4928 mtx_unlock(&lun->lun_lock); 4929 4930 return (0); 4931} 4932 4933int 4934ctl_lun_offline(struct ctl_be_lun *be_lun) 4935{ 4936 struct ctl_softc *ctl_softc; 4937 struct ctl_lun *lun; 4938 4939 ctl_softc = control_softc; 4940 4941 lun = (struct ctl_lun *)be_lun->ctl_lun; 4942 4943 mtx_lock(&lun->lun_lock); 4944 lun->flags |= CTL_LUN_OFFLINE; 4945 mtx_unlock(&lun->lun_lock); 4946 4947 return (0); 4948} 4949 4950int 4951ctl_lun_online(struct ctl_be_lun *be_lun) 4952{ 4953 struct ctl_softc *ctl_softc; 4954 struct ctl_lun *lun; 4955 4956 ctl_softc = control_softc; 4957 4958 lun = (struct ctl_lun *)be_lun->ctl_lun; 4959 4960 mtx_lock(&lun->lun_lock); 4961 lun->flags &= ~CTL_LUN_OFFLINE; 4962 mtx_unlock(&lun->lun_lock); 4963 4964 return (0); 4965} 4966 4967int 4968ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4969{ 4970 struct ctl_softc *ctl_softc; 4971 struct ctl_lun *lun; 4972 4973 ctl_softc = control_softc; 4974 4975 lun = (struct ctl_lun *)be_lun->ctl_lun; 4976 4977 mtx_lock(&lun->lun_lock); 4978 4979 /* 4980 * The LUN needs to be disabled before it can be marked invalid. 4981 */ 4982 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4983 mtx_unlock(&lun->lun_lock); 4984 return (-1); 4985 } 4986 /* 4987 * Mark the LUN invalid. 4988 */ 4989 lun->flags |= CTL_LUN_INVALID; 4990 4991 /* 4992 * If there is nothing in the OOA queue, go ahead and free the LUN. 4993 * If we have something in the OOA queue, we'll free it when the 4994 * last I/O completes. 4995 */ 4996 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4997 mtx_unlock(&lun->lun_lock); 4998 mtx_lock(&ctl_softc->ctl_lock); 4999 ctl_free_lun(lun); 5000 mtx_unlock(&ctl_softc->ctl_lock); 5001 } else 5002 mtx_unlock(&lun->lun_lock); 5003 5004 return (0); 5005} 5006 5007int 5008ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5009{ 5010 struct ctl_softc *ctl_softc; 5011 struct ctl_lun *lun; 5012 5013 ctl_softc = control_softc; 5014 lun = (struct ctl_lun *)be_lun->ctl_lun; 5015 5016 mtx_lock(&lun->lun_lock); 5017 lun->flags |= CTL_LUN_INOPERABLE; 5018 mtx_unlock(&lun->lun_lock); 5019 5020 return (0); 5021} 5022 5023int 5024ctl_lun_operable(struct ctl_be_lun *be_lun) 5025{ 5026 struct ctl_softc *ctl_softc; 5027 struct ctl_lun *lun; 5028 5029 ctl_softc = control_softc; 5030 lun = (struct ctl_lun *)be_lun->ctl_lun; 5031 5032 mtx_lock(&lun->lun_lock); 5033 lun->flags &= ~CTL_LUN_INOPERABLE; 5034 mtx_unlock(&lun->lun_lock); 5035 5036 return (0); 5037} 5038 5039int 5040ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5041 int lock) 5042{ 5043 struct ctl_softc *softc; 5044 struct ctl_lun *lun; 5045 struct copan_aps_subpage *current_sp; 5046 struct ctl_page_index *page_index; 5047 int i; 5048 5049 softc = control_softc; 5050 5051 mtx_lock(&softc->ctl_lock); 5052 5053 lun = (struct ctl_lun *)be_lun->ctl_lun; 5054 mtx_lock(&lun->lun_lock); 5055 5056 page_index = NULL; 5057 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5058 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5059 APS_PAGE_CODE) 5060 continue; 5061 5062 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5063 continue; 5064 page_index = &lun->mode_pages.index[i]; 5065 } 5066 5067 if (page_index == NULL) { 5068 mtx_unlock(&lun->lun_lock); 5069 mtx_unlock(&softc->ctl_lock); 5070 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5071 (uintmax_t)lun->lun); 5072 return (1); 5073 } 5074#if 0 5075 if ((softc->aps_locked_lun != 0) 5076 && (softc->aps_locked_lun != lun->lun)) { 5077 printf("%s: attempt to lock LUN %llu when %llu is already " 5078 "locked\n"); 5079 mtx_unlock(&lun->lun_lock); 5080 mtx_unlock(&softc->ctl_lock); 5081 return (1); 5082 } 5083#endif 5084 5085 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5086 (page_index->page_len * CTL_PAGE_CURRENT)); 5087 5088 if (lock != 0) { 5089 current_sp->lock_active = APS_LOCK_ACTIVE; 5090 softc->aps_locked_lun = lun->lun; 5091 } else { 5092 current_sp->lock_active = 0; 5093 softc->aps_locked_lun = 0; 5094 } 5095 5096 5097 /* 5098 * If we're in HA mode, try to send the lock message to the other 5099 * side. 5100 */ 5101 if (ctl_is_single == 0) { 5102 int isc_retval; 5103 union ctl_ha_msg lock_msg; 5104 5105 lock_msg.hdr.nexus = *nexus; 5106 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5107 if (lock != 0) 5108 lock_msg.aps.lock_flag = 1; 5109 else 5110 lock_msg.aps.lock_flag = 0; 5111 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5112 sizeof(lock_msg), 0); 5113 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5114 printf("%s: APS (lock=%d) error returned from " 5115 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5116 mtx_unlock(&lun->lun_lock); 5117 mtx_unlock(&softc->ctl_lock); 5118 return (1); 5119 } 5120 } 5121 5122 mtx_unlock(&lun->lun_lock); 5123 mtx_unlock(&softc->ctl_lock); 5124 5125 return (0); 5126} 5127 5128void 5129ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5130{ 5131 struct ctl_lun *lun; 5132 struct ctl_softc *softc; 5133 int i; 5134 5135 softc = control_softc; 5136 5137 lun = (struct ctl_lun *)be_lun->ctl_lun; 5138 5139 mtx_lock(&lun->lun_lock); 5140 5141 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5142 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5143 5144 mtx_unlock(&lun->lun_lock); 5145} 5146 5147/* 5148 * Backend "memory move is complete" callback for requests that never 5149 * make it down to say RAIDCore's configuration code. 5150 */ 5151int 5152ctl_config_move_done(union ctl_io *io) 5153{ 5154 int retval; 5155 5156 retval = CTL_RETVAL_COMPLETE; 5157 5158 5159 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5160 /* 5161 * XXX KDM this shouldn't happen, but what if it does? 5162 */ 5163 if (io->io_hdr.io_type != CTL_IO_SCSI) 5164 panic("I/O type isn't CTL_IO_SCSI!"); 5165 5166 if ((io->io_hdr.port_status == 0) 5167 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5168 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5169 io->io_hdr.status = CTL_SUCCESS; 5170 else if ((io->io_hdr.port_status != 0) 5171 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5172 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5173 /* 5174 * For hardware error sense keys, the sense key 5175 * specific value is defined to be a retry count, 5176 * but we use it to pass back an internal FETD 5177 * error code. XXX KDM Hopefully the FETD is only 5178 * using 16 bits for an error code, since that's 5179 * all the space we have in the sks field. 5180 */ 5181 ctl_set_internal_failure(&io->scsiio, 5182 /*sks_valid*/ 1, 5183 /*retry_count*/ 5184 io->io_hdr.port_status); 5185 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5186 free(io->scsiio.kern_data_ptr, M_CTL); 5187 ctl_done(io); 5188 goto bailout; 5189 } 5190 5191 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5192 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5193 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5194 /* 5195 * XXX KDM just assuming a single pointer here, and not a 5196 * S/G list. If we start using S/G lists for config data, 5197 * we'll need to know how to clean them up here as well. 5198 */ 5199 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5200 free(io->scsiio.kern_data_ptr, M_CTL); 5201 /* Hopefully the user has already set the status... */ 5202 ctl_done(io); 5203 } else { 5204 /* 5205 * XXX KDM now we need to continue data movement. Some 5206 * options: 5207 * - call ctl_scsiio() again? We don't do this for data 5208 * writes, because for those at least we know ahead of 5209 * time where the write will go and how long it is. For 5210 * config writes, though, that information is largely 5211 * contained within the write itself, thus we need to 5212 * parse out the data again. 5213 * 5214 * - Call some other function once the data is in? 5215 */ 5216 5217 /* 5218 * XXX KDM call ctl_scsiio() again for now, and check flag 5219 * bits to see whether we're allocated or not. 5220 */ 5221 retval = ctl_scsiio(&io->scsiio); 5222 } 5223bailout: 5224 return (retval); 5225} 5226 5227/* 5228 * This gets called by a backend driver when it is done with a 5229 * data_submit method. 5230 */ 5231void 5232ctl_data_submit_done(union ctl_io *io) 5233{ 5234 /* 5235 * If the IO_CONT flag is set, we need to call the supplied 5236 * function to continue processing the I/O, instead of completing 5237 * the I/O just yet. 5238 * 5239 * If there is an error, though, we don't want to keep processing. 5240 * Instead, just send status back to the initiator. 5241 */ 5242 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5243 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5244 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5245 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5246 io->scsiio.io_cont(io); 5247 return; 5248 } 5249 ctl_done(io); 5250} 5251 5252/* 5253 * This gets called by a backend driver when it is done with a 5254 * configuration write. 5255 */ 5256void 5257ctl_config_write_done(union ctl_io *io) 5258{ 5259 /* 5260 * If the IO_CONT flag is set, we need to call the supplied 5261 * function to continue processing the I/O, instead of completing 5262 * the I/O just yet. 5263 * 5264 * If there is an error, though, we don't want to keep processing. 5265 * Instead, just send status back to the initiator. 5266 */ 5267 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5268 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5269 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5270 io->scsiio.io_cont(io); 5271 return; 5272 } 5273 /* 5274 * Since a configuration write can be done for commands that actually 5275 * have data allocated, like write buffer, and commands that have 5276 * no data, like start/stop unit, we need to check here. 5277 */ 5278 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5279 free(io->scsiio.kern_data_ptr, M_CTL); 5280 ctl_done(io); 5281} 5282 5283/* 5284 * SCSI release command. 5285 */ 5286int 5287ctl_scsi_release(struct ctl_scsiio *ctsio) 5288{ 5289 int length, longid, thirdparty_id, resv_id; 5290 struct ctl_softc *ctl_softc; 5291 struct ctl_lun *lun; 5292 5293 length = 0; 5294 resv_id = 0; 5295 5296 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5297 5298 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5299 ctl_softc = control_softc; 5300 5301 switch (ctsio->cdb[0]) { 5302 case RELEASE_10: { 5303 struct scsi_release_10 *cdb; 5304 5305 cdb = (struct scsi_release_10 *)ctsio->cdb; 5306 5307 if (cdb->byte2 & SR10_LONGID) 5308 longid = 1; 5309 else 5310 thirdparty_id = cdb->thirdparty_id; 5311 5312 resv_id = cdb->resv_id; 5313 length = scsi_2btoul(cdb->length); 5314 break; 5315 } 5316 } 5317 5318 5319 /* 5320 * XXX KDM right now, we only support LUN reservation. We don't 5321 * support 3rd party reservations, or extent reservations, which 5322 * might actually need the parameter list. If we've gotten this 5323 * far, we've got a LUN reservation. Anything else got kicked out 5324 * above. So, according to SPC, ignore the length. 5325 */ 5326 length = 0; 5327 5328 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5329 && (length > 0)) { 5330 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5331 ctsio->kern_data_len = length; 5332 ctsio->kern_total_len = length; 5333 ctsio->kern_data_resid = 0; 5334 ctsio->kern_rel_offset = 0; 5335 ctsio->kern_sg_entries = 0; 5336 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5337 ctsio->be_move_done = ctl_config_move_done; 5338 ctl_datamove((union ctl_io *)ctsio); 5339 5340 return (CTL_RETVAL_COMPLETE); 5341 } 5342 5343 if (length > 0) 5344 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5345 5346 mtx_lock(&lun->lun_lock); 5347 5348 /* 5349 * According to SPC, it is not an error for an intiator to attempt 5350 * to release a reservation on a LUN that isn't reserved, or that 5351 * is reserved by another initiator. The reservation can only be 5352 * released, though, by the initiator who made it or by one of 5353 * several reset type events. 5354 */ 5355 if (lun->flags & CTL_LUN_RESERVED) { 5356 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5357 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5358 && (ctsio->io_hdr.nexus.targ_target.id == 5359 lun->rsv_nexus.targ_target.id)) { 5360 lun->flags &= ~CTL_LUN_RESERVED; 5361 } 5362 } 5363 5364 mtx_unlock(&lun->lun_lock); 5365 5366 ctsio->scsi_status = SCSI_STATUS_OK; 5367 ctsio->io_hdr.status = CTL_SUCCESS; 5368 5369 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5370 free(ctsio->kern_data_ptr, M_CTL); 5371 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5372 } 5373 5374 ctl_done((union ctl_io *)ctsio); 5375 return (CTL_RETVAL_COMPLETE); 5376} 5377 5378int 5379ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5380{ 5381 int extent, thirdparty, longid; 5382 int resv_id, length; 5383 uint64_t thirdparty_id; 5384 struct ctl_softc *ctl_softc; 5385 struct ctl_lun *lun; 5386 5387 extent = 0; 5388 thirdparty = 0; 5389 longid = 0; 5390 resv_id = 0; 5391 length = 0; 5392 thirdparty_id = 0; 5393 5394 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5395 5396 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5397 ctl_softc = control_softc; 5398 5399 switch (ctsio->cdb[0]) { 5400 case RESERVE_10: { 5401 struct scsi_reserve_10 *cdb; 5402 5403 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5404 5405 if (cdb->byte2 & SR10_LONGID) 5406 longid = 1; 5407 else 5408 thirdparty_id = cdb->thirdparty_id; 5409 5410 resv_id = cdb->resv_id; 5411 length = scsi_2btoul(cdb->length); 5412 break; 5413 } 5414 } 5415 5416 /* 5417 * XXX KDM right now, we only support LUN reservation. We don't 5418 * support 3rd party reservations, or extent reservations, which 5419 * might actually need the parameter list. If we've gotten this 5420 * far, we've got a LUN reservation. Anything else got kicked out 5421 * above. So, according to SPC, ignore the length. 5422 */ 5423 length = 0; 5424 5425 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5426 && (length > 0)) { 5427 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5428 ctsio->kern_data_len = length; 5429 ctsio->kern_total_len = length; 5430 ctsio->kern_data_resid = 0; 5431 ctsio->kern_rel_offset = 0; 5432 ctsio->kern_sg_entries = 0; 5433 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5434 ctsio->be_move_done = ctl_config_move_done; 5435 ctl_datamove((union ctl_io *)ctsio); 5436 5437 return (CTL_RETVAL_COMPLETE); 5438 } 5439 5440 if (length > 0) 5441 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5442 5443 mtx_lock(&lun->lun_lock); 5444 if (lun->flags & CTL_LUN_RESERVED) { 5445 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5446 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5447 || (ctsio->io_hdr.nexus.targ_target.id != 5448 lun->rsv_nexus.targ_target.id)) { 5449 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5450 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5451 goto bailout; 5452 } 5453 } 5454 5455 lun->flags |= CTL_LUN_RESERVED; 5456 lun->rsv_nexus = ctsio->io_hdr.nexus; 5457 5458 ctsio->scsi_status = SCSI_STATUS_OK; 5459 ctsio->io_hdr.status = CTL_SUCCESS; 5460 5461bailout: 5462 mtx_unlock(&lun->lun_lock); 5463 5464 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5465 free(ctsio->kern_data_ptr, M_CTL); 5466 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5467 } 5468 5469 ctl_done((union ctl_io *)ctsio); 5470 return (CTL_RETVAL_COMPLETE); 5471} 5472 5473int 5474ctl_start_stop(struct ctl_scsiio *ctsio) 5475{ 5476 struct scsi_start_stop_unit *cdb; 5477 struct ctl_lun *lun; 5478 struct ctl_softc *ctl_softc; 5479 int retval; 5480 5481 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5482 5483 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5484 ctl_softc = control_softc; 5485 retval = 0; 5486 5487 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5488 5489 /* 5490 * XXX KDM 5491 * We don't support the immediate bit on a stop unit. In order to 5492 * do that, we would need to code up a way to know that a stop is 5493 * pending, and hold off any new commands until it completes, one 5494 * way or another. Then we could accept or reject those commands 5495 * depending on its status. We would almost need to do the reverse 5496 * of what we do below for an immediate start -- return the copy of 5497 * the ctl_io to the FETD with status to send to the host (and to 5498 * free the copy!) and then free the original I/O once the stop 5499 * actually completes. That way, the OOA queue mechanism can work 5500 * to block commands that shouldn't proceed. Another alternative 5501 * would be to put the copy in the queue in place of the original, 5502 * and return the original back to the caller. That could be 5503 * slightly safer.. 5504 */ 5505 if ((cdb->byte2 & SSS_IMMED) 5506 && ((cdb->how & SSS_START) == 0)) { 5507 ctl_set_invalid_field(ctsio, 5508 /*sks_valid*/ 1, 5509 /*command*/ 1, 5510 /*field*/ 1, 5511 /*bit_valid*/ 1, 5512 /*bit*/ 0); 5513 ctl_done((union ctl_io *)ctsio); 5514 return (CTL_RETVAL_COMPLETE); 5515 } 5516 5517 if ((lun->flags & CTL_LUN_PR_RESERVED) 5518 && ((cdb->how & SSS_START)==0)) { 5519 uint32_t residx; 5520 5521 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5522 if (!lun->per_res[residx].registered 5523 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5524 5525 ctl_set_reservation_conflict(ctsio); 5526 ctl_done((union ctl_io *)ctsio); 5527 return (CTL_RETVAL_COMPLETE); 5528 } 5529 } 5530 5531 /* 5532 * If there is no backend on this device, we can't start or stop 5533 * it. In theory we shouldn't get any start/stop commands in the 5534 * first place at this level if the LUN doesn't have a backend. 5535 * That should get stopped by the command decode code. 5536 */ 5537 if (lun->backend == NULL) { 5538 ctl_set_invalid_opcode(ctsio); 5539 ctl_done((union ctl_io *)ctsio); 5540 return (CTL_RETVAL_COMPLETE); 5541 } 5542 5543 /* 5544 * XXX KDM Copan-specific offline behavior. 5545 * Figure out a reasonable way to port this? 5546 */ 5547#ifdef NEEDTOPORT 5548 mtx_lock(&lun->lun_lock); 5549 5550 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5551 && (lun->flags & CTL_LUN_OFFLINE)) { 5552 /* 5553 * If the LUN is offline, and the on/offline bit isn't set, 5554 * reject the start or stop. Otherwise, let it through. 5555 */ 5556 mtx_unlock(&lun->lun_lock); 5557 ctl_set_lun_not_ready(ctsio); 5558 ctl_done((union ctl_io *)ctsio); 5559 } else { 5560 mtx_unlock(&lun->lun_lock); 5561#endif /* NEEDTOPORT */ 5562 /* 5563 * This could be a start or a stop when we're online, 5564 * or a stop/offline or start/online. A start or stop when 5565 * we're offline is covered in the case above. 5566 */ 5567 /* 5568 * In the non-immediate case, we send the request to 5569 * the backend and return status to the user when 5570 * it is done. 5571 * 5572 * In the immediate case, we allocate a new ctl_io 5573 * to hold a copy of the request, and send that to 5574 * the backend. We then set good status on the 5575 * user's request and return it immediately. 5576 */ 5577 if (cdb->byte2 & SSS_IMMED) { 5578 union ctl_io *new_io; 5579 5580 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5581 if (new_io == NULL) { 5582 ctl_set_busy(ctsio); 5583 ctl_done((union ctl_io *)ctsio); 5584 } else { 5585 ctl_copy_io((union ctl_io *)ctsio, 5586 new_io); 5587 retval = lun->backend->config_write(new_io); 5588 ctl_set_success(ctsio); 5589 ctl_done((union ctl_io *)ctsio); 5590 } 5591 } else { 5592 retval = lun->backend->config_write( 5593 (union ctl_io *)ctsio); 5594 } 5595#ifdef NEEDTOPORT 5596 } 5597#endif 5598 return (retval); 5599} 5600 5601/* 5602 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5603 * we don't really do anything with the LBA and length fields if the user 5604 * passes them in. Instead we'll just flush out the cache for the entire 5605 * LUN. 5606 */ 5607int 5608ctl_sync_cache(struct ctl_scsiio *ctsio) 5609{ 5610 struct ctl_lun *lun; 5611 struct ctl_softc *ctl_softc; 5612 uint64_t starting_lba; 5613 uint32_t block_count; 5614 int retval; 5615 5616 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5617 5618 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5619 ctl_softc = control_softc; 5620 retval = 0; 5621 5622 switch (ctsio->cdb[0]) { 5623 case SYNCHRONIZE_CACHE: { 5624 struct scsi_sync_cache *cdb; 5625 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5626 5627 starting_lba = scsi_4btoul(cdb->begin_lba); 5628 block_count = scsi_2btoul(cdb->lb_count); 5629 break; 5630 } 5631 case SYNCHRONIZE_CACHE_16: { 5632 struct scsi_sync_cache_16 *cdb; 5633 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5634 5635 starting_lba = scsi_8btou64(cdb->begin_lba); 5636 block_count = scsi_4btoul(cdb->lb_count); 5637 break; 5638 } 5639 default: 5640 ctl_set_invalid_opcode(ctsio); 5641 ctl_done((union ctl_io *)ctsio); 5642 goto bailout; 5643 break; /* NOTREACHED */ 5644 } 5645 5646 /* 5647 * We check the LBA and length, but don't do anything with them. 5648 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5649 * get flushed. This check will just help satisfy anyone who wants 5650 * to see an error for an out of range LBA. 5651 */ 5652 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5653 ctl_set_lba_out_of_range(ctsio); 5654 ctl_done((union ctl_io *)ctsio); 5655 goto bailout; 5656 } 5657 5658 /* 5659 * If this LUN has no backend, we can't flush the cache anyway. 5660 */ 5661 if (lun->backend == NULL) { 5662 ctl_set_invalid_opcode(ctsio); 5663 ctl_done((union ctl_io *)ctsio); 5664 goto bailout; 5665 } 5666 5667 /* 5668 * Check to see whether we're configured to send the SYNCHRONIZE 5669 * CACHE command directly to the back end. 5670 */ 5671 mtx_lock(&lun->lun_lock); 5672 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5673 && (++(lun->sync_count) >= lun->sync_interval)) { 5674 lun->sync_count = 0; 5675 mtx_unlock(&lun->lun_lock); 5676 retval = lun->backend->config_write((union ctl_io *)ctsio); 5677 } else { 5678 mtx_unlock(&lun->lun_lock); 5679 ctl_set_success(ctsio); 5680 ctl_done((union ctl_io *)ctsio); 5681 } 5682 5683bailout: 5684 5685 return (retval); 5686} 5687 5688int 5689ctl_format(struct ctl_scsiio *ctsio) 5690{ 5691 struct scsi_format *cdb; 5692 struct ctl_lun *lun; 5693 struct ctl_softc *ctl_softc; 5694 int length, defect_list_len; 5695 5696 CTL_DEBUG_PRINT(("ctl_format\n")); 5697 5698 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5699 ctl_softc = control_softc; 5700 5701 cdb = (struct scsi_format *)ctsio->cdb; 5702 5703 length = 0; 5704 if (cdb->byte2 & SF_FMTDATA) { 5705 if (cdb->byte2 & SF_LONGLIST) 5706 length = sizeof(struct scsi_format_header_long); 5707 else 5708 length = sizeof(struct scsi_format_header_short); 5709 } 5710 5711 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5712 && (length > 0)) { 5713 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5714 ctsio->kern_data_len = length; 5715 ctsio->kern_total_len = length; 5716 ctsio->kern_data_resid = 0; 5717 ctsio->kern_rel_offset = 0; 5718 ctsio->kern_sg_entries = 0; 5719 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5720 ctsio->be_move_done = ctl_config_move_done; 5721 ctl_datamove((union ctl_io *)ctsio); 5722 5723 return (CTL_RETVAL_COMPLETE); 5724 } 5725 5726 defect_list_len = 0; 5727 5728 if (cdb->byte2 & SF_FMTDATA) { 5729 if (cdb->byte2 & SF_LONGLIST) { 5730 struct scsi_format_header_long *header; 5731 5732 header = (struct scsi_format_header_long *) 5733 ctsio->kern_data_ptr; 5734 5735 defect_list_len = scsi_4btoul(header->defect_list_len); 5736 if (defect_list_len != 0) { 5737 ctl_set_invalid_field(ctsio, 5738 /*sks_valid*/ 1, 5739 /*command*/ 0, 5740 /*field*/ 2, 5741 /*bit_valid*/ 0, 5742 /*bit*/ 0); 5743 goto bailout; 5744 } 5745 } else { 5746 struct scsi_format_header_short *header; 5747 5748 header = (struct scsi_format_header_short *) 5749 ctsio->kern_data_ptr; 5750 5751 defect_list_len = scsi_2btoul(header->defect_list_len); 5752 if (defect_list_len != 0) { 5753 ctl_set_invalid_field(ctsio, 5754 /*sks_valid*/ 1, 5755 /*command*/ 0, 5756 /*field*/ 2, 5757 /*bit_valid*/ 0, 5758 /*bit*/ 0); 5759 goto bailout; 5760 } 5761 } 5762 } 5763 5764 /* 5765 * The format command will clear out the "Medium format corrupted" 5766 * status if set by the configuration code. That status is really 5767 * just a way to notify the host that we have lost the media, and 5768 * get them to issue a command that will basically make them think 5769 * they're blowing away the media. 5770 */ 5771 mtx_lock(&lun->lun_lock); 5772 lun->flags &= ~CTL_LUN_INOPERABLE; 5773 mtx_unlock(&lun->lun_lock); 5774 5775 ctsio->scsi_status = SCSI_STATUS_OK; 5776 ctsio->io_hdr.status = CTL_SUCCESS; 5777bailout: 5778 5779 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5780 free(ctsio->kern_data_ptr, M_CTL); 5781 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5782 } 5783 5784 ctl_done((union ctl_io *)ctsio); 5785 return (CTL_RETVAL_COMPLETE); 5786} 5787 5788int 5789ctl_read_buffer(struct ctl_scsiio *ctsio) 5790{ 5791 struct scsi_read_buffer *cdb; 5792 struct ctl_lun *lun; 5793 int buffer_offset, len; 5794 static uint8_t descr[4]; 5795 static uint8_t echo_descr[4] = { 0 }; 5796 5797 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5798 5799 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5800 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5801 5802 if (lun->flags & CTL_LUN_PR_RESERVED) { 5803 uint32_t residx; 5804 5805 /* 5806 * XXX KDM need a lock here. 5807 */ 5808 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5809 if ((lun->res_type == SPR_TYPE_EX_AC 5810 && residx != lun->pr_res_idx) 5811 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5812 || lun->res_type == SPR_TYPE_EX_AC_AR) 5813 && !lun->per_res[residx].registered)) { 5814 ctl_set_reservation_conflict(ctsio); 5815 ctl_done((union ctl_io *)ctsio); 5816 return (CTL_RETVAL_COMPLETE); 5817 } 5818 } 5819 5820 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5821 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5822 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5823 ctl_set_invalid_field(ctsio, 5824 /*sks_valid*/ 1, 5825 /*command*/ 1, 5826 /*field*/ 1, 5827 /*bit_valid*/ 1, 5828 /*bit*/ 4); 5829 ctl_done((union ctl_io *)ctsio); 5830 return (CTL_RETVAL_COMPLETE); 5831 } 5832 5833 len = scsi_3btoul(cdb->length); 5834 buffer_offset = scsi_3btoul(cdb->offset); 5835 5836 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5837 ctl_set_invalid_field(ctsio, 5838 /*sks_valid*/ 1, 5839 /*command*/ 1, 5840 /*field*/ 6, 5841 /*bit_valid*/ 0, 5842 /*bit*/ 0); 5843 ctl_done((union ctl_io *)ctsio); 5844 return (CTL_RETVAL_COMPLETE); 5845 } 5846 5847 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5848 descr[0] = 0; 5849 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5850 ctsio->kern_data_ptr = descr; 5851 len = min(len, sizeof(descr)); 5852 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5853 ctsio->kern_data_ptr = echo_descr; 5854 len = min(len, sizeof(echo_descr)); 5855 } else 5856 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5857 ctsio->kern_data_len = len; 5858 ctsio->kern_total_len = len; 5859 ctsio->kern_data_resid = 0; 5860 ctsio->kern_rel_offset = 0; 5861 ctsio->kern_sg_entries = 0; 5862 ctsio->be_move_done = ctl_config_move_done; 5863 ctl_datamove((union ctl_io *)ctsio); 5864 5865 return (CTL_RETVAL_COMPLETE); 5866} 5867 5868int 5869ctl_write_buffer(struct ctl_scsiio *ctsio) 5870{ 5871 struct scsi_write_buffer *cdb; 5872 struct ctl_lun *lun; 5873 int buffer_offset, len; 5874 5875 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5876 5877 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5878 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5879 5880 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5881 ctl_set_invalid_field(ctsio, 5882 /*sks_valid*/ 1, 5883 /*command*/ 1, 5884 /*field*/ 1, 5885 /*bit_valid*/ 1, 5886 /*bit*/ 4); 5887 ctl_done((union ctl_io *)ctsio); 5888 return (CTL_RETVAL_COMPLETE); 5889 } 5890 5891 len = scsi_3btoul(cdb->length); 5892 buffer_offset = scsi_3btoul(cdb->offset); 5893 5894 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5895 ctl_set_invalid_field(ctsio, 5896 /*sks_valid*/ 1, 5897 /*command*/ 1, 5898 /*field*/ 6, 5899 /*bit_valid*/ 0, 5900 /*bit*/ 0); 5901 ctl_done((union ctl_io *)ctsio); 5902 return (CTL_RETVAL_COMPLETE); 5903 } 5904 5905 /* 5906 * If we've got a kernel request that hasn't been malloced yet, 5907 * malloc it and tell the caller the data buffer is here. 5908 */ 5909 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5910 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5911 ctsio->kern_data_len = len; 5912 ctsio->kern_total_len = len; 5913 ctsio->kern_data_resid = 0; 5914 ctsio->kern_rel_offset = 0; 5915 ctsio->kern_sg_entries = 0; 5916 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5917 ctsio->be_move_done = ctl_config_move_done; 5918 ctl_datamove((union ctl_io *)ctsio); 5919 5920 return (CTL_RETVAL_COMPLETE); 5921 } 5922 5923 ctl_done((union ctl_io *)ctsio); 5924 5925 return (CTL_RETVAL_COMPLETE); 5926} 5927 5928int 5929ctl_write_same(struct ctl_scsiio *ctsio) 5930{ 5931 struct ctl_lun *lun; 5932 struct ctl_lba_len_flags *lbalen; 5933 uint64_t lba; 5934 uint32_t num_blocks; 5935 int len, retval; 5936 uint8_t byte2; 5937 5938 retval = CTL_RETVAL_COMPLETE; 5939 5940 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5941 5942 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5943 5944 switch (ctsio->cdb[0]) { 5945 case WRITE_SAME_10: { 5946 struct scsi_write_same_10 *cdb; 5947 5948 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5949 5950 lba = scsi_4btoul(cdb->addr); 5951 num_blocks = scsi_2btoul(cdb->length); 5952 byte2 = cdb->byte2; 5953 break; 5954 } 5955 case WRITE_SAME_16: { 5956 struct scsi_write_same_16 *cdb; 5957 5958 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5959 5960 lba = scsi_8btou64(cdb->addr); 5961 num_blocks = scsi_4btoul(cdb->length); 5962 byte2 = cdb->byte2; 5963 break; 5964 } 5965 default: 5966 /* 5967 * We got a command we don't support. This shouldn't 5968 * happen, commands should be filtered out above us. 5969 */ 5970 ctl_set_invalid_opcode(ctsio); 5971 ctl_done((union ctl_io *)ctsio); 5972 5973 return (CTL_RETVAL_COMPLETE); 5974 break; /* NOTREACHED */ 5975 } 5976 5977 /* 5978 * The first check is to make sure we're in bounds, the second 5979 * check is to catch wrap-around problems. If the lba + num blocks 5980 * is less than the lba, then we've wrapped around and the block 5981 * range is invalid anyway. 5982 */ 5983 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5984 || ((lba + num_blocks) < lba)) { 5985 ctl_set_lba_out_of_range(ctsio); 5986 ctl_done((union ctl_io *)ctsio); 5987 return (CTL_RETVAL_COMPLETE); 5988 } 5989 5990 /* Zero number of blocks means "to the last logical block" */ 5991 if (num_blocks == 0) { 5992 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5993 ctl_set_invalid_field(ctsio, 5994 /*sks_valid*/ 0, 5995 /*command*/ 1, 5996 /*field*/ 0, 5997 /*bit_valid*/ 0, 5998 /*bit*/ 0); 5999 ctl_done((union ctl_io *)ctsio); 6000 return (CTL_RETVAL_COMPLETE); 6001 } 6002 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6003 } 6004 6005 len = lun->be_lun->blocksize; 6006 6007 /* 6008 * If we've got a kernel request that hasn't been malloced yet, 6009 * malloc it and tell the caller the data buffer is here. 6010 */ 6011 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6012 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6013 ctsio->kern_data_len = len; 6014 ctsio->kern_total_len = len; 6015 ctsio->kern_data_resid = 0; 6016 ctsio->kern_rel_offset = 0; 6017 ctsio->kern_sg_entries = 0; 6018 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6019 ctsio->be_move_done = ctl_config_move_done; 6020 ctl_datamove((union ctl_io *)ctsio); 6021 6022 return (CTL_RETVAL_COMPLETE); 6023 } 6024 6025 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6026 lbalen->lba = lba; 6027 lbalen->len = num_blocks; 6028 lbalen->flags = byte2; 6029 retval = lun->backend->config_write((union ctl_io *)ctsio); 6030 6031 return (retval); 6032} 6033 6034int 6035ctl_unmap(struct ctl_scsiio *ctsio) 6036{ 6037 struct ctl_lun *lun; 6038 struct scsi_unmap *cdb; 6039 struct ctl_ptr_len_flags *ptrlen; 6040 struct scsi_unmap_header *hdr; 6041 struct scsi_unmap_desc *buf, *end; 6042 uint64_t lba; 6043 uint32_t num_blocks; 6044 int len, retval; 6045 uint8_t byte2; 6046 6047 retval = CTL_RETVAL_COMPLETE; 6048 6049 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6050 6051 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6052 cdb = (struct scsi_unmap *)ctsio->cdb; 6053 6054 len = scsi_2btoul(cdb->length); 6055 byte2 = cdb->byte2; 6056 6057 /* 6058 * If we've got a kernel request that hasn't been malloced yet, 6059 * malloc it and tell the caller the data buffer is here. 6060 */ 6061 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6062 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6063 ctsio->kern_data_len = len; 6064 ctsio->kern_total_len = len; 6065 ctsio->kern_data_resid = 0; 6066 ctsio->kern_rel_offset = 0; 6067 ctsio->kern_sg_entries = 0; 6068 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6069 ctsio->be_move_done = ctl_config_move_done; 6070 ctl_datamove((union ctl_io *)ctsio); 6071 6072 return (CTL_RETVAL_COMPLETE); 6073 } 6074 6075 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6076 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6077 if (len < sizeof (*hdr) || 6078 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6079 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6080 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6081 ctl_set_invalid_field(ctsio, 6082 /*sks_valid*/ 0, 6083 /*command*/ 0, 6084 /*field*/ 0, 6085 /*bit_valid*/ 0, 6086 /*bit*/ 0); 6087 ctl_done((union ctl_io *)ctsio); 6088 return (CTL_RETVAL_COMPLETE); 6089 } 6090 len = scsi_2btoul(hdr->desc_length); 6091 buf = (struct scsi_unmap_desc *)(hdr + 1); 6092 end = buf + len / sizeof(*buf); 6093 6094 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6095 ptrlen->ptr = (void *)buf; 6096 ptrlen->len = len; 6097 ptrlen->flags = byte2; 6098 6099 for (; buf < end; buf++) { 6100 lba = scsi_8btou64(buf->lba); 6101 num_blocks = scsi_4btoul(buf->length); 6102 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6103 || ((lba + num_blocks) < lba)) { 6104 ctl_set_lba_out_of_range(ctsio); 6105 ctl_done((union ctl_io *)ctsio); 6106 return (CTL_RETVAL_COMPLETE); 6107 } 6108 } 6109 6110 retval = lun->backend->config_write((union ctl_io *)ctsio); 6111 6112 return (retval); 6113} 6114 6115/* 6116 * Note that this function currently doesn't actually do anything inside 6117 * CTL to enforce things if the DQue bit is turned on. 6118 * 6119 * Also note that this function can't be used in the default case, because 6120 * the DQue bit isn't set in the changeable mask for the control mode page 6121 * anyway. This is just here as an example for how to implement a page 6122 * handler, and a placeholder in case we want to allow the user to turn 6123 * tagged queueing on and off. 6124 * 6125 * The D_SENSE bit handling is functional, however, and will turn 6126 * descriptor sense on and off for a given LUN. 6127 */ 6128int 6129ctl_control_page_handler(struct ctl_scsiio *ctsio, 6130 struct ctl_page_index *page_index, uint8_t *page_ptr) 6131{ 6132 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6133 struct ctl_lun *lun; 6134 struct ctl_softc *softc; 6135 int set_ua; 6136 uint32_t initidx; 6137 6138 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6139 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6140 set_ua = 0; 6141 6142 user_cp = (struct scsi_control_page *)page_ptr; 6143 current_cp = (struct scsi_control_page *) 6144 (page_index->page_data + (page_index->page_len * 6145 CTL_PAGE_CURRENT)); 6146 saved_cp = (struct scsi_control_page *) 6147 (page_index->page_data + (page_index->page_len * 6148 CTL_PAGE_SAVED)); 6149 6150 softc = control_softc; 6151 6152 mtx_lock(&lun->lun_lock); 6153 if (((current_cp->rlec & SCP_DSENSE) == 0) 6154 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6155 /* 6156 * Descriptor sense is currently turned off and the user 6157 * wants to turn it on. 6158 */ 6159 current_cp->rlec |= SCP_DSENSE; 6160 saved_cp->rlec |= SCP_DSENSE; 6161 lun->flags |= CTL_LUN_SENSE_DESC; 6162 set_ua = 1; 6163 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6164 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6165 /* 6166 * Descriptor sense is currently turned on, and the user 6167 * wants to turn it off. 6168 */ 6169 current_cp->rlec &= ~SCP_DSENSE; 6170 saved_cp->rlec &= ~SCP_DSENSE; 6171 lun->flags &= ~CTL_LUN_SENSE_DESC; 6172 set_ua = 1; 6173 } 6174 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6175 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6176#ifdef NEEDTOPORT 6177 csevent_log(CSC_CTL | CSC_SHELF_SW | 6178 CTL_UNTAG_TO_UNTAG, 6179 csevent_LogType_Trace, 6180 csevent_Severity_Information, 6181 csevent_AlertLevel_Green, 6182 csevent_FRU_Firmware, 6183 csevent_FRU_Unknown, 6184 "Received untagged to untagged transition"); 6185#endif /* NEEDTOPORT */ 6186 } else { 6187#ifdef NEEDTOPORT 6188 csevent_log(CSC_CTL | CSC_SHELF_SW | 6189 CTL_UNTAG_TO_TAG, 6190 csevent_LogType_ConfigChange, 6191 csevent_Severity_Information, 6192 csevent_AlertLevel_Green, 6193 csevent_FRU_Firmware, 6194 csevent_FRU_Unknown, 6195 "Received untagged to tagged " 6196 "queueing transition"); 6197#endif /* NEEDTOPORT */ 6198 6199 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6200 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6201 set_ua = 1; 6202 } 6203 } else { 6204 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6205#ifdef NEEDTOPORT 6206 csevent_log(CSC_CTL | CSC_SHELF_SW | 6207 CTL_TAG_TO_UNTAG, 6208 csevent_LogType_ConfigChange, 6209 csevent_Severity_Warning, 6210 csevent_AlertLevel_Yellow, 6211 csevent_FRU_Firmware, 6212 csevent_FRU_Unknown, 6213 "Received tagged queueing to untagged " 6214 "transition"); 6215#endif /* NEEDTOPORT */ 6216 6217 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6218 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6219 set_ua = 1; 6220 } else { 6221#ifdef NEEDTOPORT 6222 csevent_log(CSC_CTL | CSC_SHELF_SW | 6223 CTL_TAG_TO_TAG, 6224 csevent_LogType_Trace, 6225 csevent_Severity_Information, 6226 csevent_AlertLevel_Green, 6227 csevent_FRU_Firmware, 6228 csevent_FRU_Unknown, 6229 "Received tagged queueing to tagged " 6230 "queueing transition"); 6231#endif /* NEEDTOPORT */ 6232 } 6233 } 6234 if (set_ua != 0) { 6235 int i; 6236 /* 6237 * Let other initiators know that the mode 6238 * parameters for this LUN have changed. 6239 */ 6240 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6241 if (i == initidx) 6242 continue; 6243 6244 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6245 } 6246 } 6247 mtx_unlock(&lun->lun_lock); 6248 6249 return (0); 6250} 6251 6252int 6253ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6254 struct ctl_page_index *page_index, uint8_t *page_ptr) 6255{ 6256 return (0); 6257} 6258 6259int 6260ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6261 struct ctl_page_index *page_index, int pc) 6262{ 6263 struct copan_power_subpage *page; 6264 6265 page = (struct copan_power_subpage *)page_index->page_data + 6266 (page_index->page_len * pc); 6267 6268 switch (pc) { 6269 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6270 /* 6271 * We don't update the changable bits for this page. 6272 */ 6273 break; 6274 case SMS_PAGE_CTRL_CURRENT >> 6: 6275 case SMS_PAGE_CTRL_DEFAULT >> 6: 6276 case SMS_PAGE_CTRL_SAVED >> 6: 6277#ifdef NEEDTOPORT 6278 ctl_update_power_subpage(page); 6279#endif 6280 break; 6281 default: 6282#ifdef NEEDTOPORT 6283 EPRINT(0, "Invalid PC %d!!", pc); 6284#endif 6285 break; 6286 } 6287 return (0); 6288} 6289 6290 6291int 6292ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6293 struct ctl_page_index *page_index, uint8_t *page_ptr) 6294{ 6295 struct copan_aps_subpage *user_sp; 6296 struct copan_aps_subpage *current_sp; 6297 union ctl_modepage_info *modepage_info; 6298 struct ctl_softc *softc; 6299 struct ctl_lun *lun; 6300 int retval; 6301 6302 retval = CTL_RETVAL_COMPLETE; 6303 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6304 (page_index->page_len * CTL_PAGE_CURRENT)); 6305 softc = control_softc; 6306 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6307 6308 user_sp = (struct copan_aps_subpage *)page_ptr; 6309 6310 modepage_info = (union ctl_modepage_info *) 6311 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6312 6313 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6314 modepage_info->header.subpage = page_index->subpage; 6315 modepage_info->aps.lock_active = user_sp->lock_active; 6316 6317 mtx_lock(&softc->ctl_lock); 6318 6319 /* 6320 * If there is a request to lock the LUN and another LUN is locked 6321 * this is an error. If the requested LUN is already locked ignore 6322 * the request. If no LUN is locked attempt to lock it. 6323 * if there is a request to unlock the LUN and the LUN is currently 6324 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6325 * if another LUN is locked or no LUN is locked. 6326 */ 6327 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6328 if (softc->aps_locked_lun == lun->lun) { 6329 /* 6330 * This LUN is already locked, so we're done. 6331 */ 6332 retval = CTL_RETVAL_COMPLETE; 6333 } else if (softc->aps_locked_lun == 0) { 6334 /* 6335 * No one has the lock, pass the request to the 6336 * backend. 6337 */ 6338 retval = lun->backend->config_write( 6339 (union ctl_io *)ctsio); 6340 } else { 6341 /* 6342 * Someone else has the lock, throw out the request. 6343 */ 6344 ctl_set_already_locked(ctsio); 6345 free(ctsio->kern_data_ptr, M_CTL); 6346 ctl_done((union ctl_io *)ctsio); 6347 6348 /* 6349 * Set the return value so that ctl_do_mode_select() 6350 * won't try to complete the command. We already 6351 * completed it here. 6352 */ 6353 retval = CTL_RETVAL_ERROR; 6354 } 6355 } else if (softc->aps_locked_lun == lun->lun) { 6356 /* 6357 * This LUN is locked, so pass the unlock request to the 6358 * backend. 6359 */ 6360 retval = lun->backend->config_write((union ctl_io *)ctsio); 6361 } 6362 mtx_unlock(&softc->ctl_lock); 6363 6364 return (retval); 6365} 6366 6367int 6368ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6369 struct ctl_page_index *page_index, 6370 uint8_t *page_ptr) 6371{ 6372 uint8_t *c; 6373 int i; 6374 6375 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6376 ctl_time_io_secs = 6377 (c[0] << 8) | 6378 (c[1] << 0) | 6379 0; 6380 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6381 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6382 printf("page data:"); 6383 for (i=0; i<8; i++) 6384 printf(" %.2x",page_ptr[i]); 6385 printf("\n"); 6386 return (0); 6387} 6388 6389int 6390ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6391 struct ctl_page_index *page_index, 6392 int pc) 6393{ 6394 struct copan_debugconf_subpage *page; 6395 6396 page = (struct copan_debugconf_subpage *)page_index->page_data + 6397 (page_index->page_len * pc); 6398 6399 switch (pc) { 6400 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6401 case SMS_PAGE_CTRL_DEFAULT >> 6: 6402 case SMS_PAGE_CTRL_SAVED >> 6: 6403 /* 6404 * We don't update the changable or default bits for this page. 6405 */ 6406 break; 6407 case SMS_PAGE_CTRL_CURRENT >> 6: 6408 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6409 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6410 break; 6411 default: 6412#ifdef NEEDTOPORT 6413 EPRINT(0, "Invalid PC %d!!", pc); 6414#endif /* NEEDTOPORT */ 6415 break; 6416 } 6417 return (0); 6418} 6419 6420 6421static int 6422ctl_do_mode_select(union ctl_io *io) 6423{ 6424 struct scsi_mode_page_header *page_header; 6425 struct ctl_page_index *page_index; 6426 struct ctl_scsiio *ctsio; 6427 int control_dev, page_len; 6428 int page_len_offset, page_len_size; 6429 union ctl_modepage_info *modepage_info; 6430 struct ctl_lun *lun; 6431 int *len_left, *len_used; 6432 int retval, i; 6433 6434 ctsio = &io->scsiio; 6435 page_index = NULL; 6436 page_len = 0; 6437 retval = CTL_RETVAL_COMPLETE; 6438 6439 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6440 6441 if (lun->be_lun->lun_type != T_DIRECT) 6442 control_dev = 1; 6443 else 6444 control_dev = 0; 6445 6446 modepage_info = (union ctl_modepage_info *) 6447 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6448 len_left = &modepage_info->header.len_left; 6449 len_used = &modepage_info->header.len_used; 6450 6451do_next_page: 6452 6453 page_header = (struct scsi_mode_page_header *) 6454 (ctsio->kern_data_ptr + *len_used); 6455 6456 if (*len_left == 0) { 6457 free(ctsio->kern_data_ptr, M_CTL); 6458 ctl_set_success(ctsio); 6459 ctl_done((union ctl_io *)ctsio); 6460 return (CTL_RETVAL_COMPLETE); 6461 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6462 6463 free(ctsio->kern_data_ptr, M_CTL); 6464 ctl_set_param_len_error(ctsio); 6465 ctl_done((union ctl_io *)ctsio); 6466 return (CTL_RETVAL_COMPLETE); 6467 6468 } else if ((page_header->page_code & SMPH_SPF) 6469 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6470 6471 free(ctsio->kern_data_ptr, M_CTL); 6472 ctl_set_param_len_error(ctsio); 6473 ctl_done((union ctl_io *)ctsio); 6474 return (CTL_RETVAL_COMPLETE); 6475 } 6476 6477 6478 /* 6479 * XXX KDM should we do something with the block descriptor? 6480 */ 6481 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6482 6483 if ((control_dev != 0) 6484 && (lun->mode_pages.index[i].page_flags & 6485 CTL_PAGE_FLAG_DISK_ONLY)) 6486 continue; 6487 6488 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6489 (page_header->page_code & SMPH_PC_MASK)) 6490 continue; 6491 6492 /* 6493 * If neither page has a subpage code, then we've got a 6494 * match. 6495 */ 6496 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6497 && ((page_header->page_code & SMPH_SPF) == 0)) { 6498 page_index = &lun->mode_pages.index[i]; 6499 page_len = page_header->page_length; 6500 break; 6501 } 6502 6503 /* 6504 * If both pages have subpages, then the subpage numbers 6505 * have to match. 6506 */ 6507 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6508 && (page_header->page_code & SMPH_SPF)) { 6509 struct scsi_mode_page_header_sp *sph; 6510 6511 sph = (struct scsi_mode_page_header_sp *)page_header; 6512 6513 if (lun->mode_pages.index[i].subpage == 6514 sph->subpage) { 6515 page_index = &lun->mode_pages.index[i]; 6516 page_len = scsi_2btoul(sph->page_length); 6517 break; 6518 } 6519 } 6520 } 6521 6522 /* 6523 * If we couldn't find the page, or if we don't have a mode select 6524 * handler for it, send back an error to the user. 6525 */ 6526 if ((page_index == NULL) 6527 || (page_index->select_handler == NULL)) { 6528 ctl_set_invalid_field(ctsio, 6529 /*sks_valid*/ 1, 6530 /*command*/ 0, 6531 /*field*/ *len_used, 6532 /*bit_valid*/ 0, 6533 /*bit*/ 0); 6534 free(ctsio->kern_data_ptr, M_CTL); 6535 ctl_done((union ctl_io *)ctsio); 6536 return (CTL_RETVAL_COMPLETE); 6537 } 6538 6539 if (page_index->page_code & SMPH_SPF) { 6540 page_len_offset = 2; 6541 page_len_size = 2; 6542 } else { 6543 page_len_size = 1; 6544 page_len_offset = 1; 6545 } 6546 6547 /* 6548 * If the length the initiator gives us isn't the one we specify in 6549 * the mode page header, or if they didn't specify enough data in 6550 * the CDB to avoid truncating this page, kick out the request. 6551 */ 6552 if ((page_len != (page_index->page_len - page_len_offset - 6553 page_len_size)) 6554 || (*len_left < page_index->page_len)) { 6555 6556 6557 ctl_set_invalid_field(ctsio, 6558 /*sks_valid*/ 1, 6559 /*command*/ 0, 6560 /*field*/ *len_used + page_len_offset, 6561 /*bit_valid*/ 0, 6562 /*bit*/ 0); 6563 free(ctsio->kern_data_ptr, M_CTL); 6564 ctl_done((union ctl_io *)ctsio); 6565 return (CTL_RETVAL_COMPLETE); 6566 } 6567 6568 /* 6569 * Run through the mode page, checking to make sure that the bits 6570 * the user changed are actually legal for him to change. 6571 */ 6572 for (i = 0; i < page_index->page_len; i++) { 6573 uint8_t *user_byte, *change_mask, *current_byte; 6574 int bad_bit; 6575 int j; 6576 6577 user_byte = (uint8_t *)page_header + i; 6578 change_mask = page_index->page_data + 6579 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6580 current_byte = page_index->page_data + 6581 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6582 6583 /* 6584 * Check to see whether the user set any bits in this byte 6585 * that he is not allowed to set. 6586 */ 6587 if ((*user_byte & ~(*change_mask)) == 6588 (*current_byte & ~(*change_mask))) 6589 continue; 6590 6591 /* 6592 * Go through bit by bit to determine which one is illegal. 6593 */ 6594 bad_bit = 0; 6595 for (j = 7; j >= 0; j--) { 6596 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6597 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6598 bad_bit = i; 6599 break; 6600 } 6601 } 6602 ctl_set_invalid_field(ctsio, 6603 /*sks_valid*/ 1, 6604 /*command*/ 0, 6605 /*field*/ *len_used + i, 6606 /*bit_valid*/ 1, 6607 /*bit*/ bad_bit); 6608 free(ctsio->kern_data_ptr, M_CTL); 6609 ctl_done((union ctl_io *)ctsio); 6610 return (CTL_RETVAL_COMPLETE); 6611 } 6612 6613 /* 6614 * Decrement these before we call the page handler, since we may 6615 * end up getting called back one way or another before the handler 6616 * returns to this context. 6617 */ 6618 *len_left -= page_index->page_len; 6619 *len_used += page_index->page_len; 6620 6621 retval = page_index->select_handler(ctsio, page_index, 6622 (uint8_t *)page_header); 6623 6624 /* 6625 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6626 * wait until this queued command completes to finish processing 6627 * the mode page. If it returns anything other than 6628 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6629 * already set the sense information, freed the data pointer, and 6630 * completed the io for us. 6631 */ 6632 if (retval != CTL_RETVAL_COMPLETE) 6633 goto bailout_no_done; 6634 6635 /* 6636 * If the initiator sent us more than one page, parse the next one. 6637 */ 6638 if (*len_left > 0) 6639 goto do_next_page; 6640 6641 ctl_set_success(ctsio); 6642 free(ctsio->kern_data_ptr, M_CTL); 6643 ctl_done((union ctl_io *)ctsio); 6644 6645bailout_no_done: 6646 6647 return (CTL_RETVAL_COMPLETE); 6648 6649} 6650 6651int 6652ctl_mode_select(struct ctl_scsiio *ctsio) 6653{ 6654 int param_len, pf, sp; 6655 int header_size, bd_len; 6656 int len_left, len_used; 6657 struct ctl_page_index *page_index; 6658 struct ctl_lun *lun; 6659 int control_dev, page_len; 6660 union ctl_modepage_info *modepage_info; 6661 int retval; 6662 6663 pf = 0; 6664 sp = 0; 6665 page_len = 0; 6666 len_used = 0; 6667 len_left = 0; 6668 retval = 0; 6669 bd_len = 0; 6670 page_index = NULL; 6671 6672 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6673 6674 if (lun->be_lun->lun_type != T_DIRECT) 6675 control_dev = 1; 6676 else 6677 control_dev = 0; 6678 6679 switch (ctsio->cdb[0]) { 6680 case MODE_SELECT_6: { 6681 struct scsi_mode_select_6 *cdb; 6682 6683 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6684 6685 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6686 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6687 6688 param_len = cdb->length; 6689 header_size = sizeof(struct scsi_mode_header_6); 6690 break; 6691 } 6692 case MODE_SELECT_10: { 6693 struct scsi_mode_select_10 *cdb; 6694 6695 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6696 6697 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6698 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6699 6700 param_len = scsi_2btoul(cdb->length); 6701 header_size = sizeof(struct scsi_mode_header_10); 6702 break; 6703 } 6704 default: 6705 ctl_set_invalid_opcode(ctsio); 6706 ctl_done((union ctl_io *)ctsio); 6707 return (CTL_RETVAL_COMPLETE); 6708 break; /* NOTREACHED */ 6709 } 6710 6711 /* 6712 * From SPC-3: 6713 * "A parameter list length of zero indicates that the Data-Out Buffer 6714 * shall be empty. This condition shall not be considered as an error." 6715 */ 6716 if (param_len == 0) { 6717 ctl_set_success(ctsio); 6718 ctl_done((union ctl_io *)ctsio); 6719 return (CTL_RETVAL_COMPLETE); 6720 } 6721 6722 /* 6723 * Since we'll hit this the first time through, prior to 6724 * allocation, we don't need to free a data buffer here. 6725 */ 6726 if (param_len < header_size) { 6727 ctl_set_param_len_error(ctsio); 6728 ctl_done((union ctl_io *)ctsio); 6729 return (CTL_RETVAL_COMPLETE); 6730 } 6731 6732 /* 6733 * Allocate the data buffer and grab the user's data. In theory, 6734 * we shouldn't have to sanity check the parameter list length here 6735 * because the maximum size is 64K. We should be able to malloc 6736 * that much without too many problems. 6737 */ 6738 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6739 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6740 ctsio->kern_data_len = param_len; 6741 ctsio->kern_total_len = param_len; 6742 ctsio->kern_data_resid = 0; 6743 ctsio->kern_rel_offset = 0; 6744 ctsio->kern_sg_entries = 0; 6745 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6746 ctsio->be_move_done = ctl_config_move_done; 6747 ctl_datamove((union ctl_io *)ctsio); 6748 6749 return (CTL_RETVAL_COMPLETE); 6750 } 6751 6752 switch (ctsio->cdb[0]) { 6753 case MODE_SELECT_6: { 6754 struct scsi_mode_header_6 *mh6; 6755 6756 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6757 bd_len = mh6->blk_desc_len; 6758 break; 6759 } 6760 case MODE_SELECT_10: { 6761 struct scsi_mode_header_10 *mh10; 6762 6763 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6764 bd_len = scsi_2btoul(mh10->blk_desc_len); 6765 break; 6766 } 6767 default: 6768 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6769 break; 6770 } 6771 6772 if (param_len < (header_size + bd_len)) { 6773 free(ctsio->kern_data_ptr, M_CTL); 6774 ctl_set_param_len_error(ctsio); 6775 ctl_done((union ctl_io *)ctsio); 6776 return (CTL_RETVAL_COMPLETE); 6777 } 6778 6779 /* 6780 * Set the IO_CONT flag, so that if this I/O gets passed to 6781 * ctl_config_write_done(), it'll get passed back to 6782 * ctl_do_mode_select() for further processing, or completion if 6783 * we're all done. 6784 */ 6785 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6786 ctsio->io_cont = ctl_do_mode_select; 6787 6788 modepage_info = (union ctl_modepage_info *) 6789 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6790 6791 memset(modepage_info, 0, sizeof(*modepage_info)); 6792 6793 len_left = param_len - header_size - bd_len; 6794 len_used = header_size + bd_len; 6795 6796 modepage_info->header.len_left = len_left; 6797 modepage_info->header.len_used = len_used; 6798 6799 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6800} 6801 6802int 6803ctl_mode_sense(struct ctl_scsiio *ctsio) 6804{ 6805 struct ctl_lun *lun; 6806 int pc, page_code, dbd, llba, subpage; 6807 int alloc_len, page_len, header_len, total_len; 6808 struct scsi_mode_block_descr *block_desc; 6809 struct ctl_page_index *page_index; 6810 int control_dev; 6811 6812 dbd = 0; 6813 llba = 0; 6814 block_desc = NULL; 6815 page_index = NULL; 6816 6817 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6818 6819 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6820 6821 if (lun->be_lun->lun_type != T_DIRECT) 6822 control_dev = 1; 6823 else 6824 control_dev = 0; 6825 6826 if (lun->flags & CTL_LUN_PR_RESERVED) { 6827 uint32_t residx; 6828 6829 /* 6830 * XXX KDM need a lock here. 6831 */ 6832 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6833 if ((lun->res_type == SPR_TYPE_EX_AC 6834 && residx != lun->pr_res_idx) 6835 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6836 || lun->res_type == SPR_TYPE_EX_AC_AR) 6837 && !lun->per_res[residx].registered)) { 6838 ctl_set_reservation_conflict(ctsio); 6839 ctl_done((union ctl_io *)ctsio); 6840 return (CTL_RETVAL_COMPLETE); 6841 } 6842 } 6843 6844 switch (ctsio->cdb[0]) { 6845 case MODE_SENSE_6: { 6846 struct scsi_mode_sense_6 *cdb; 6847 6848 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6849 6850 header_len = sizeof(struct scsi_mode_hdr_6); 6851 if (cdb->byte2 & SMS_DBD) 6852 dbd = 1; 6853 else 6854 header_len += sizeof(struct scsi_mode_block_descr); 6855 6856 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6857 page_code = cdb->page & SMS_PAGE_CODE; 6858 subpage = cdb->subpage; 6859 alloc_len = cdb->length; 6860 break; 6861 } 6862 case MODE_SENSE_10: { 6863 struct scsi_mode_sense_10 *cdb; 6864 6865 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6866 6867 header_len = sizeof(struct scsi_mode_hdr_10); 6868 6869 if (cdb->byte2 & SMS_DBD) 6870 dbd = 1; 6871 else 6872 header_len += sizeof(struct scsi_mode_block_descr); 6873 if (cdb->byte2 & SMS10_LLBAA) 6874 llba = 1; 6875 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6876 page_code = cdb->page & SMS_PAGE_CODE; 6877 subpage = cdb->subpage; 6878 alloc_len = scsi_2btoul(cdb->length); 6879 break; 6880 } 6881 default: 6882 ctl_set_invalid_opcode(ctsio); 6883 ctl_done((union ctl_io *)ctsio); 6884 return (CTL_RETVAL_COMPLETE); 6885 break; /* NOTREACHED */ 6886 } 6887 6888 /* 6889 * We have to make a first pass through to calculate the size of 6890 * the pages that match the user's query. Then we allocate enough 6891 * memory to hold it, and actually copy the data into the buffer. 6892 */ 6893 switch (page_code) { 6894 case SMS_ALL_PAGES_PAGE: { 6895 int i; 6896 6897 page_len = 0; 6898 6899 /* 6900 * At the moment, values other than 0 and 0xff here are 6901 * reserved according to SPC-3. 6902 */ 6903 if ((subpage != SMS_SUBPAGE_PAGE_0) 6904 && (subpage != SMS_SUBPAGE_ALL)) { 6905 ctl_set_invalid_field(ctsio, 6906 /*sks_valid*/ 1, 6907 /*command*/ 1, 6908 /*field*/ 3, 6909 /*bit_valid*/ 0, 6910 /*bit*/ 0); 6911 ctl_done((union ctl_io *)ctsio); 6912 return (CTL_RETVAL_COMPLETE); 6913 } 6914 6915 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6916 if ((control_dev != 0) 6917 && (lun->mode_pages.index[i].page_flags & 6918 CTL_PAGE_FLAG_DISK_ONLY)) 6919 continue; 6920 6921 /* 6922 * We don't use this subpage if the user didn't 6923 * request all subpages. 6924 */ 6925 if ((lun->mode_pages.index[i].subpage != 0) 6926 && (subpage == SMS_SUBPAGE_PAGE_0)) 6927 continue; 6928 6929#if 0 6930 printf("found page %#x len %d\n", 6931 lun->mode_pages.index[i].page_code & 6932 SMPH_PC_MASK, 6933 lun->mode_pages.index[i].page_len); 6934#endif 6935 page_len += lun->mode_pages.index[i].page_len; 6936 } 6937 break; 6938 } 6939 default: { 6940 int i; 6941 6942 page_len = 0; 6943 6944 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6945 /* Look for the right page code */ 6946 if ((lun->mode_pages.index[i].page_code & 6947 SMPH_PC_MASK) != page_code) 6948 continue; 6949 6950 /* Look for the right subpage or the subpage wildcard*/ 6951 if ((lun->mode_pages.index[i].subpage != subpage) 6952 && (subpage != SMS_SUBPAGE_ALL)) 6953 continue; 6954 6955 /* Make sure the page is supported for this dev type */ 6956 if ((control_dev != 0) 6957 && (lun->mode_pages.index[i].page_flags & 6958 CTL_PAGE_FLAG_DISK_ONLY)) 6959 continue; 6960 6961#if 0 6962 printf("found page %#x len %d\n", 6963 lun->mode_pages.index[i].page_code & 6964 SMPH_PC_MASK, 6965 lun->mode_pages.index[i].page_len); 6966#endif 6967 6968 page_len += lun->mode_pages.index[i].page_len; 6969 } 6970 6971 if (page_len == 0) { 6972 ctl_set_invalid_field(ctsio, 6973 /*sks_valid*/ 1, 6974 /*command*/ 1, 6975 /*field*/ 2, 6976 /*bit_valid*/ 1, 6977 /*bit*/ 5); 6978 ctl_done((union ctl_io *)ctsio); 6979 return (CTL_RETVAL_COMPLETE); 6980 } 6981 break; 6982 } 6983 } 6984 6985 total_len = header_len + page_len; 6986#if 0 6987 printf("header_len = %d, page_len = %d, total_len = %d\n", 6988 header_len, page_len, total_len); 6989#endif 6990 6991 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6992 ctsio->kern_sg_entries = 0; 6993 ctsio->kern_data_resid = 0; 6994 ctsio->kern_rel_offset = 0; 6995 if (total_len < alloc_len) { 6996 ctsio->residual = alloc_len - total_len; 6997 ctsio->kern_data_len = total_len; 6998 ctsio->kern_total_len = total_len; 6999 } else { 7000 ctsio->residual = 0; 7001 ctsio->kern_data_len = alloc_len; 7002 ctsio->kern_total_len = alloc_len; 7003 } 7004 7005 switch (ctsio->cdb[0]) { 7006 case MODE_SENSE_6: { 7007 struct scsi_mode_hdr_6 *header; 7008 7009 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7010 7011 header->datalen = ctl_min(total_len - 1, 254); 7012 7013 if (dbd) 7014 header->block_descr_len = 0; 7015 else 7016 header->block_descr_len = 7017 sizeof(struct scsi_mode_block_descr); 7018 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7019 break; 7020 } 7021 case MODE_SENSE_10: { 7022 struct scsi_mode_hdr_10 *header; 7023 int datalen; 7024 7025 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7026 7027 datalen = ctl_min(total_len - 2, 65533); 7028 scsi_ulto2b(datalen, header->datalen); 7029 if (dbd) 7030 scsi_ulto2b(0, header->block_descr_len); 7031 else 7032 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7033 header->block_descr_len); 7034 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7035 break; 7036 } 7037 default: 7038 panic("invalid CDB type %#x", ctsio->cdb[0]); 7039 break; /* NOTREACHED */ 7040 } 7041 7042 /* 7043 * If we've got a disk, use its blocksize in the block 7044 * descriptor. Otherwise, just set it to 0. 7045 */ 7046 if (dbd == 0) { 7047 if (control_dev != 0) 7048 scsi_ulto3b(lun->be_lun->blocksize, 7049 block_desc->block_len); 7050 else 7051 scsi_ulto3b(0, block_desc->block_len); 7052 } 7053 7054 switch (page_code) { 7055 case SMS_ALL_PAGES_PAGE: { 7056 int i, data_used; 7057 7058 data_used = header_len; 7059 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7060 struct ctl_page_index *page_index; 7061 7062 page_index = &lun->mode_pages.index[i]; 7063 7064 if ((control_dev != 0) 7065 && (page_index->page_flags & 7066 CTL_PAGE_FLAG_DISK_ONLY)) 7067 continue; 7068 7069 /* 7070 * We don't use this subpage if the user didn't 7071 * request all subpages. We already checked (above) 7072 * to make sure the user only specified a subpage 7073 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7074 */ 7075 if ((page_index->subpage != 0) 7076 && (subpage == SMS_SUBPAGE_PAGE_0)) 7077 continue; 7078 7079 /* 7080 * Call the handler, if it exists, to update the 7081 * page to the latest values. 7082 */ 7083 if (page_index->sense_handler != NULL) 7084 page_index->sense_handler(ctsio, page_index,pc); 7085 7086 memcpy(ctsio->kern_data_ptr + data_used, 7087 page_index->page_data + 7088 (page_index->page_len * pc), 7089 page_index->page_len); 7090 data_used += page_index->page_len; 7091 } 7092 break; 7093 } 7094 default: { 7095 int i, data_used; 7096 7097 data_used = header_len; 7098 7099 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7100 struct ctl_page_index *page_index; 7101 7102 page_index = &lun->mode_pages.index[i]; 7103 7104 /* Look for the right page code */ 7105 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7106 continue; 7107 7108 /* Look for the right subpage or the subpage wildcard*/ 7109 if ((page_index->subpage != subpage) 7110 && (subpage != SMS_SUBPAGE_ALL)) 7111 continue; 7112 7113 /* Make sure the page is supported for this dev type */ 7114 if ((control_dev != 0) 7115 && (page_index->page_flags & 7116 CTL_PAGE_FLAG_DISK_ONLY)) 7117 continue; 7118 7119 /* 7120 * Call the handler, if it exists, to update the 7121 * page to the latest values. 7122 */ 7123 if (page_index->sense_handler != NULL) 7124 page_index->sense_handler(ctsio, page_index,pc); 7125 7126 memcpy(ctsio->kern_data_ptr + data_used, 7127 page_index->page_data + 7128 (page_index->page_len * pc), 7129 page_index->page_len); 7130 data_used += page_index->page_len; 7131 } 7132 break; 7133 } 7134 } 7135 7136 ctsio->scsi_status = SCSI_STATUS_OK; 7137 7138 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7139 ctsio->be_move_done = ctl_config_move_done; 7140 ctl_datamove((union ctl_io *)ctsio); 7141 7142 return (CTL_RETVAL_COMPLETE); 7143} 7144 7145int 7146ctl_read_capacity(struct ctl_scsiio *ctsio) 7147{ 7148 struct scsi_read_capacity *cdb; 7149 struct scsi_read_capacity_data *data; 7150 struct ctl_lun *lun; 7151 uint32_t lba; 7152 7153 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7154 7155 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7156 7157 lba = scsi_4btoul(cdb->addr); 7158 if (((cdb->pmi & SRC_PMI) == 0) 7159 && (lba != 0)) { 7160 ctl_set_invalid_field(/*ctsio*/ ctsio, 7161 /*sks_valid*/ 1, 7162 /*command*/ 1, 7163 /*field*/ 2, 7164 /*bit_valid*/ 0, 7165 /*bit*/ 0); 7166 ctl_done((union ctl_io *)ctsio); 7167 return (CTL_RETVAL_COMPLETE); 7168 } 7169 7170 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7171 7172 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7173 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7174 ctsio->residual = 0; 7175 ctsio->kern_data_len = sizeof(*data); 7176 ctsio->kern_total_len = sizeof(*data); 7177 ctsio->kern_data_resid = 0; 7178 ctsio->kern_rel_offset = 0; 7179 ctsio->kern_sg_entries = 0; 7180 7181 /* 7182 * If the maximum LBA is greater than 0xfffffffe, the user must 7183 * issue a SERVICE ACTION IN (16) command, with the read capacity 7184 * serivce action set. 7185 */ 7186 if (lun->be_lun->maxlba > 0xfffffffe) 7187 scsi_ulto4b(0xffffffff, data->addr); 7188 else 7189 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7190 7191 /* 7192 * XXX KDM this may not be 512 bytes... 7193 */ 7194 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7195 7196 ctsio->scsi_status = SCSI_STATUS_OK; 7197 7198 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7199 ctsio->be_move_done = ctl_config_move_done; 7200 ctl_datamove((union ctl_io *)ctsio); 7201 7202 return (CTL_RETVAL_COMPLETE); 7203} 7204 7205int 7206ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7207{ 7208 struct scsi_read_capacity_16 *cdb; 7209 struct scsi_read_capacity_data_long *data; 7210 struct ctl_lun *lun; 7211 uint64_t lba; 7212 uint32_t alloc_len; 7213 7214 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7215 7216 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7217 7218 alloc_len = scsi_4btoul(cdb->alloc_len); 7219 lba = scsi_8btou64(cdb->addr); 7220 7221 if ((cdb->reladr & SRC16_PMI) 7222 && (lba != 0)) { 7223 ctl_set_invalid_field(/*ctsio*/ ctsio, 7224 /*sks_valid*/ 1, 7225 /*command*/ 1, 7226 /*field*/ 2, 7227 /*bit_valid*/ 0, 7228 /*bit*/ 0); 7229 ctl_done((union ctl_io *)ctsio); 7230 return (CTL_RETVAL_COMPLETE); 7231 } 7232 7233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7234 7235 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7236 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7237 7238 if (sizeof(*data) < alloc_len) { 7239 ctsio->residual = alloc_len - sizeof(*data); 7240 ctsio->kern_data_len = sizeof(*data); 7241 ctsio->kern_total_len = sizeof(*data); 7242 } else { 7243 ctsio->residual = 0; 7244 ctsio->kern_data_len = alloc_len; 7245 ctsio->kern_total_len = alloc_len; 7246 } 7247 ctsio->kern_data_resid = 0; 7248 ctsio->kern_rel_offset = 0; 7249 ctsio->kern_sg_entries = 0; 7250 7251 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7252 /* XXX KDM this may not be 512 bytes... */ 7253 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7254 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7255 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7256 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7257 data->lalba_lbp[0] |= SRC16_LBPME; 7258 7259 ctsio->scsi_status = SCSI_STATUS_OK; 7260 7261 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7262 ctsio->be_move_done = ctl_config_move_done; 7263 ctl_datamove((union ctl_io *)ctsio); 7264 7265 return (CTL_RETVAL_COMPLETE); 7266} 7267 7268int 7269ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7270{ 7271 struct scsi_maintenance_in *cdb; 7272 int retval; 7273 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7274 int num_target_port_groups, num_target_ports, single; 7275 struct ctl_lun *lun; 7276 struct ctl_softc *softc; 7277 struct ctl_port *port; 7278 struct scsi_target_group_data *rtg_ptr; 7279 struct scsi_target_group_data_extended *rtg_ext_ptr; 7280 struct scsi_target_port_group_descriptor *tpg_desc; 7281 7282 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7283 7284 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7285 softc = control_softc; 7286 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7287 7288 retval = CTL_RETVAL_COMPLETE; 7289 7290 switch (cdb->byte2 & STG_PDF_MASK) { 7291 case STG_PDF_LENGTH: 7292 ext = 0; 7293 break; 7294 case STG_PDF_EXTENDED: 7295 ext = 1; 7296 break; 7297 default: 7298 ctl_set_invalid_field(/*ctsio*/ ctsio, 7299 /*sks_valid*/ 1, 7300 /*command*/ 1, 7301 /*field*/ 2, 7302 /*bit_valid*/ 1, 7303 /*bit*/ 5); 7304 ctl_done((union ctl_io *)ctsio); 7305 return(retval); 7306 } 7307 7308 single = ctl_is_single; 7309 if (single) 7310 num_target_port_groups = 1; 7311 else 7312 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7313 num_target_ports = 0; 7314 mtx_lock(&softc->ctl_lock); 7315 STAILQ_FOREACH(port, &softc->port_list, links) { 7316 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7317 continue; 7318 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7319 continue; 7320 num_target_ports++; 7321 } 7322 mtx_unlock(&softc->ctl_lock); 7323 7324 if (ext) 7325 total_len = sizeof(struct scsi_target_group_data_extended); 7326 else 7327 total_len = sizeof(struct scsi_target_group_data); 7328 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7329 num_target_port_groups + 7330 sizeof(struct scsi_target_port_descriptor) * 7331 num_target_ports * num_target_port_groups; 7332 7333 alloc_len = scsi_4btoul(cdb->length); 7334 7335 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7336 7337 ctsio->kern_sg_entries = 0; 7338 7339 if (total_len < alloc_len) { 7340 ctsio->residual = alloc_len - total_len; 7341 ctsio->kern_data_len = total_len; 7342 ctsio->kern_total_len = total_len; 7343 } else { 7344 ctsio->residual = 0; 7345 ctsio->kern_data_len = alloc_len; 7346 ctsio->kern_total_len = alloc_len; 7347 } 7348 ctsio->kern_data_resid = 0; 7349 ctsio->kern_rel_offset = 0; 7350 7351 if (ext) { 7352 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7353 ctsio->kern_data_ptr; 7354 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7355 rtg_ext_ptr->format_type = 0x10; 7356 rtg_ext_ptr->implicit_transition_time = 0; 7357 tpg_desc = &rtg_ext_ptr->groups[0]; 7358 } else { 7359 rtg_ptr = (struct scsi_target_group_data *) 7360 ctsio->kern_data_ptr; 7361 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7362 tpg_desc = &rtg_ptr->groups[0]; 7363 } 7364 7365 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7366 mtx_lock(&softc->ctl_lock); 7367 for (g = 0; g < num_target_port_groups; g++) { 7368 if (g == pg) 7369 tpg_desc->pref_state = TPG_PRIMARY | 7370 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7371 else 7372 tpg_desc->pref_state = 7373 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7374 tpg_desc->support = TPG_AO_SUP; 7375 if (!single) 7376 tpg_desc->support |= TPG_AN_SUP; 7377 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7378 tpg_desc->status = TPG_IMPLICIT; 7379 pc = 0; 7380 STAILQ_FOREACH(port, &softc->port_list, links) { 7381 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7382 continue; 7383 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7384 CTL_MAX_LUNS) 7385 continue; 7386 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7387 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7388 relative_target_port_identifier); 7389 pc++; 7390 } 7391 tpg_desc->target_port_count = pc; 7392 tpg_desc = (struct scsi_target_port_group_descriptor *) 7393 &tpg_desc->descriptors[pc]; 7394 } 7395 mtx_unlock(&softc->ctl_lock); 7396 7397 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7398 ctsio->be_move_done = ctl_config_move_done; 7399 7400 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7401 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7402 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7403 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7404 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7405 7406 ctl_datamove((union ctl_io *)ctsio); 7407 return(retval); 7408} 7409 7410int 7411ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7412{ 7413 struct ctl_lun *lun; 7414 struct scsi_report_supported_opcodes *cdb; 7415 const struct ctl_cmd_entry *entry, *sentry; 7416 struct scsi_report_supported_opcodes_all *all; 7417 struct scsi_report_supported_opcodes_descr *descr; 7418 struct scsi_report_supported_opcodes_one *one; 7419 int retval; 7420 int alloc_len, total_len; 7421 int opcode, service_action, i, j, num; 7422 7423 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7424 7425 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7426 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7427 7428 retval = CTL_RETVAL_COMPLETE; 7429 7430 opcode = cdb->requested_opcode; 7431 service_action = scsi_2btoul(cdb->requested_service_action); 7432 switch (cdb->options & RSO_OPTIONS_MASK) { 7433 case RSO_OPTIONS_ALL: 7434 num = 0; 7435 for (i = 0; i < 256; i++) { 7436 entry = &ctl_cmd_table[i]; 7437 if (entry->flags & CTL_CMD_FLAG_SA5) { 7438 for (j = 0; j < 32; j++) { 7439 sentry = &((const struct ctl_cmd_entry *) 7440 entry->execute)[j]; 7441 if (ctl_cmd_applicable( 7442 lun->be_lun->lun_type, sentry)) 7443 num++; 7444 } 7445 } else { 7446 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7447 entry)) 7448 num++; 7449 } 7450 } 7451 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7452 num * sizeof(struct scsi_report_supported_opcodes_descr); 7453 break; 7454 case RSO_OPTIONS_OC: 7455 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7456 ctl_set_invalid_field(/*ctsio*/ ctsio, 7457 /*sks_valid*/ 1, 7458 /*command*/ 1, 7459 /*field*/ 2, 7460 /*bit_valid*/ 1, 7461 /*bit*/ 2); 7462 ctl_done((union ctl_io *)ctsio); 7463 return (CTL_RETVAL_COMPLETE); 7464 } 7465 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7466 break; 7467 case RSO_OPTIONS_OC_SA: 7468 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7469 service_action >= 32) { 7470 ctl_set_invalid_field(/*ctsio*/ ctsio, 7471 /*sks_valid*/ 1, 7472 /*command*/ 1, 7473 /*field*/ 2, 7474 /*bit_valid*/ 1, 7475 /*bit*/ 2); 7476 ctl_done((union ctl_io *)ctsio); 7477 return (CTL_RETVAL_COMPLETE); 7478 } 7479 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7480 break; 7481 default: 7482 ctl_set_invalid_field(/*ctsio*/ ctsio, 7483 /*sks_valid*/ 1, 7484 /*command*/ 1, 7485 /*field*/ 2, 7486 /*bit_valid*/ 1, 7487 /*bit*/ 2); 7488 ctl_done((union ctl_io *)ctsio); 7489 return (CTL_RETVAL_COMPLETE); 7490 } 7491 7492 alloc_len = scsi_4btoul(cdb->length); 7493 7494 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7495 7496 ctsio->kern_sg_entries = 0; 7497 7498 if (total_len < alloc_len) { 7499 ctsio->residual = alloc_len - total_len; 7500 ctsio->kern_data_len = total_len; 7501 ctsio->kern_total_len = total_len; 7502 } else { 7503 ctsio->residual = 0; 7504 ctsio->kern_data_len = alloc_len; 7505 ctsio->kern_total_len = alloc_len; 7506 } 7507 ctsio->kern_data_resid = 0; 7508 ctsio->kern_rel_offset = 0; 7509 7510 switch (cdb->options & RSO_OPTIONS_MASK) { 7511 case RSO_OPTIONS_ALL: 7512 all = (struct scsi_report_supported_opcodes_all *) 7513 ctsio->kern_data_ptr; 7514 num = 0; 7515 for (i = 0; i < 256; i++) { 7516 entry = &ctl_cmd_table[i]; 7517 if (entry->flags & CTL_CMD_FLAG_SA5) { 7518 for (j = 0; j < 32; j++) { 7519 sentry = &((const struct ctl_cmd_entry *) 7520 entry->execute)[j]; 7521 if (!ctl_cmd_applicable( 7522 lun->be_lun->lun_type, sentry)) 7523 continue; 7524 descr = &all->descr[num++]; 7525 descr->opcode = i; 7526 scsi_ulto2b(j, descr->service_action); 7527 descr->flags = RSO_SERVACTV; 7528 scsi_ulto2b(sentry->length, 7529 descr->cdb_length); 7530 } 7531 } else { 7532 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7533 entry)) 7534 continue; 7535 descr = &all->descr[num++]; 7536 descr->opcode = i; 7537 scsi_ulto2b(0, descr->service_action); 7538 descr->flags = 0; 7539 scsi_ulto2b(entry->length, descr->cdb_length); 7540 } 7541 } 7542 scsi_ulto4b( 7543 num * sizeof(struct scsi_report_supported_opcodes_descr), 7544 all->length); 7545 break; 7546 case RSO_OPTIONS_OC: 7547 one = (struct scsi_report_supported_opcodes_one *) 7548 ctsio->kern_data_ptr; 7549 entry = &ctl_cmd_table[opcode]; 7550 goto fill_one; 7551 case RSO_OPTIONS_OC_SA: 7552 one = (struct scsi_report_supported_opcodes_one *) 7553 ctsio->kern_data_ptr; 7554 entry = &ctl_cmd_table[opcode]; 7555 entry = &((const struct ctl_cmd_entry *) 7556 entry->execute)[service_action]; 7557fill_one: 7558 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7559 one->support = 3; 7560 scsi_ulto2b(entry->length, one->cdb_length); 7561 one->cdb_usage[0] = opcode; 7562 memcpy(&one->cdb_usage[1], entry->usage, 7563 entry->length - 1); 7564 } else 7565 one->support = 1; 7566 break; 7567 } 7568 7569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7570 ctsio->be_move_done = ctl_config_move_done; 7571 7572 ctl_datamove((union ctl_io *)ctsio); 7573 return(retval); 7574} 7575 7576int 7577ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7578{ 7579 struct ctl_lun *lun; 7580 struct scsi_report_supported_tmf *cdb; 7581 struct scsi_report_supported_tmf_data *data; 7582 int retval; 7583 int alloc_len, total_len; 7584 7585 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7586 7587 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7588 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7589 7590 retval = CTL_RETVAL_COMPLETE; 7591 7592 total_len = sizeof(struct scsi_report_supported_tmf_data); 7593 alloc_len = scsi_4btoul(cdb->length); 7594 7595 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7596 7597 ctsio->kern_sg_entries = 0; 7598 7599 if (total_len < alloc_len) { 7600 ctsio->residual = alloc_len - total_len; 7601 ctsio->kern_data_len = total_len; 7602 ctsio->kern_total_len = total_len; 7603 } else { 7604 ctsio->residual = 0; 7605 ctsio->kern_data_len = alloc_len; 7606 ctsio->kern_total_len = alloc_len; 7607 } 7608 ctsio->kern_data_resid = 0; 7609 ctsio->kern_rel_offset = 0; 7610 7611 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7612 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7613 data->byte2 |= RST_ITNRS; 7614 7615 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7616 ctsio->be_move_done = ctl_config_move_done; 7617 7618 ctl_datamove((union ctl_io *)ctsio); 7619 return (retval); 7620} 7621 7622int 7623ctl_report_timestamp(struct ctl_scsiio *ctsio) 7624{ 7625 struct ctl_lun *lun; 7626 struct scsi_report_timestamp *cdb; 7627 struct scsi_report_timestamp_data *data; 7628 struct timeval tv; 7629 int64_t timestamp; 7630 int retval; 7631 int alloc_len, total_len; 7632 7633 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7634 7635 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7636 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7637 7638 retval = CTL_RETVAL_COMPLETE; 7639 7640 total_len = sizeof(struct scsi_report_timestamp_data); 7641 alloc_len = scsi_4btoul(cdb->length); 7642 7643 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7644 7645 ctsio->kern_sg_entries = 0; 7646 7647 if (total_len < alloc_len) { 7648 ctsio->residual = alloc_len - total_len; 7649 ctsio->kern_data_len = total_len; 7650 ctsio->kern_total_len = total_len; 7651 } else { 7652 ctsio->residual = 0; 7653 ctsio->kern_data_len = alloc_len; 7654 ctsio->kern_total_len = alloc_len; 7655 } 7656 ctsio->kern_data_resid = 0; 7657 ctsio->kern_rel_offset = 0; 7658 7659 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7660 scsi_ulto2b(sizeof(*data) - 2, data->length); 7661 data->origin = RTS_ORIG_OUTSIDE; 7662 getmicrotime(&tv); 7663 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7664 scsi_ulto4b(timestamp >> 16, data->timestamp); 7665 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7666 7667 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7668 ctsio->be_move_done = ctl_config_move_done; 7669 7670 ctl_datamove((union ctl_io *)ctsio); 7671 return (retval); 7672} 7673 7674int 7675ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7676{ 7677 struct scsi_per_res_in *cdb; 7678 int alloc_len, total_len = 0; 7679 /* struct scsi_per_res_in_rsrv in_data; */ 7680 struct ctl_lun *lun; 7681 struct ctl_softc *softc; 7682 7683 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7684 7685 softc = control_softc; 7686 7687 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7688 7689 alloc_len = scsi_2btoul(cdb->length); 7690 7691 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7692 7693retry: 7694 mtx_lock(&lun->lun_lock); 7695 switch (cdb->action) { 7696 case SPRI_RK: /* read keys */ 7697 total_len = sizeof(struct scsi_per_res_in_keys) + 7698 lun->pr_key_count * 7699 sizeof(struct scsi_per_res_key); 7700 break; 7701 case SPRI_RR: /* read reservation */ 7702 if (lun->flags & CTL_LUN_PR_RESERVED) 7703 total_len = sizeof(struct scsi_per_res_in_rsrv); 7704 else 7705 total_len = sizeof(struct scsi_per_res_in_header); 7706 break; 7707 case SPRI_RC: /* report capabilities */ 7708 total_len = sizeof(struct scsi_per_res_cap); 7709 break; 7710 case SPRI_RS: /* read full status */ 7711 total_len = sizeof(struct scsi_per_res_in_header) + 7712 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7713 lun->pr_key_count; 7714 break; 7715 default: 7716 panic("Invalid PR type %x", cdb->action); 7717 } 7718 mtx_unlock(&lun->lun_lock); 7719 7720 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7721 7722 if (total_len < alloc_len) { 7723 ctsio->residual = alloc_len - total_len; 7724 ctsio->kern_data_len = total_len; 7725 ctsio->kern_total_len = total_len; 7726 } else { 7727 ctsio->residual = 0; 7728 ctsio->kern_data_len = alloc_len; 7729 ctsio->kern_total_len = alloc_len; 7730 } 7731 7732 ctsio->kern_data_resid = 0; 7733 ctsio->kern_rel_offset = 0; 7734 ctsio->kern_sg_entries = 0; 7735 7736 mtx_lock(&lun->lun_lock); 7737 switch (cdb->action) { 7738 case SPRI_RK: { // read keys 7739 struct scsi_per_res_in_keys *res_keys; 7740 int i, key_count; 7741 7742 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7743 7744 /* 7745 * We had to drop the lock to allocate our buffer, which 7746 * leaves time for someone to come in with another 7747 * persistent reservation. (That is unlikely, though, 7748 * since this should be the only persistent reservation 7749 * command active right now.) 7750 */ 7751 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7752 (lun->pr_key_count * 7753 sizeof(struct scsi_per_res_key)))){ 7754 mtx_unlock(&lun->lun_lock); 7755 free(ctsio->kern_data_ptr, M_CTL); 7756 printf("%s: reservation length changed, retrying\n", 7757 __func__); 7758 goto retry; 7759 } 7760 7761 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7762 7763 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7764 lun->pr_key_count, res_keys->header.length); 7765 7766 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7767 if (!lun->per_res[i].registered) 7768 continue; 7769 7770 /* 7771 * We used lun->pr_key_count to calculate the 7772 * size to allocate. If it turns out the number of 7773 * initiators with the registered flag set is 7774 * larger than that (i.e. they haven't been kept in 7775 * sync), we've got a problem. 7776 */ 7777 if (key_count >= lun->pr_key_count) { 7778#ifdef NEEDTOPORT 7779 csevent_log(CSC_CTL | CSC_SHELF_SW | 7780 CTL_PR_ERROR, 7781 csevent_LogType_Fault, 7782 csevent_AlertLevel_Yellow, 7783 csevent_FRU_ShelfController, 7784 csevent_FRU_Firmware, 7785 csevent_FRU_Unknown, 7786 "registered keys %d >= key " 7787 "count %d", key_count, 7788 lun->pr_key_count); 7789#endif 7790 key_count++; 7791 continue; 7792 } 7793 memcpy(res_keys->keys[key_count].key, 7794 lun->per_res[i].res_key.key, 7795 ctl_min(sizeof(res_keys->keys[key_count].key), 7796 sizeof(lun->per_res[i].res_key))); 7797 key_count++; 7798 } 7799 break; 7800 } 7801 case SPRI_RR: { // read reservation 7802 struct scsi_per_res_in_rsrv *res; 7803 int tmp_len, header_only; 7804 7805 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7806 7807 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7808 7809 if (lun->flags & CTL_LUN_PR_RESERVED) 7810 { 7811 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7812 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7813 res->header.length); 7814 header_only = 0; 7815 } else { 7816 tmp_len = sizeof(struct scsi_per_res_in_header); 7817 scsi_ulto4b(0, res->header.length); 7818 header_only = 1; 7819 } 7820 7821 /* 7822 * We had to drop the lock to allocate our buffer, which 7823 * leaves time for someone to come in with another 7824 * persistent reservation. (That is unlikely, though, 7825 * since this should be the only persistent reservation 7826 * command active right now.) 7827 */ 7828 if (tmp_len != total_len) { 7829 mtx_unlock(&lun->lun_lock); 7830 free(ctsio->kern_data_ptr, M_CTL); 7831 printf("%s: reservation status changed, retrying\n", 7832 __func__); 7833 goto retry; 7834 } 7835 7836 /* 7837 * No reservation held, so we're done. 7838 */ 7839 if (header_only != 0) 7840 break; 7841 7842 /* 7843 * If the registration is an All Registrants type, the key 7844 * is 0, since it doesn't really matter. 7845 */ 7846 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7847 memcpy(res->data.reservation, 7848 &lun->per_res[lun->pr_res_idx].res_key, 7849 sizeof(struct scsi_per_res_key)); 7850 } 7851 res->data.scopetype = lun->res_type; 7852 break; 7853 } 7854 case SPRI_RC: //report capabilities 7855 { 7856 struct scsi_per_res_cap *res_cap; 7857 uint16_t type_mask; 7858 7859 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7860 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7861 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7862 type_mask = SPRI_TM_WR_EX_AR | 7863 SPRI_TM_EX_AC_RO | 7864 SPRI_TM_WR_EX_RO | 7865 SPRI_TM_EX_AC | 7866 SPRI_TM_WR_EX | 7867 SPRI_TM_EX_AC_AR; 7868 scsi_ulto2b(type_mask, res_cap->type_mask); 7869 break; 7870 } 7871 case SPRI_RS: { // read full status 7872 struct scsi_per_res_in_full *res_status; 7873 struct scsi_per_res_in_full_desc *res_desc; 7874 struct ctl_port *port; 7875 int i, len; 7876 7877 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7878 7879 /* 7880 * We had to drop the lock to allocate our buffer, which 7881 * leaves time for someone to come in with another 7882 * persistent reservation. (That is unlikely, though, 7883 * since this should be the only persistent reservation 7884 * command active right now.) 7885 */ 7886 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7887 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7888 lun->pr_key_count)){ 7889 mtx_unlock(&lun->lun_lock); 7890 free(ctsio->kern_data_ptr, M_CTL); 7891 printf("%s: reservation length changed, retrying\n", 7892 __func__); 7893 goto retry; 7894 } 7895 7896 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7897 7898 res_desc = &res_status->desc[0]; 7899 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7900 if (!lun->per_res[i].registered) 7901 continue; 7902 7903 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7904 sizeof(res_desc->res_key)); 7905 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7906 (lun->pr_res_idx == i || 7907 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7908 res_desc->flags = SPRI_FULL_R_HOLDER; 7909 res_desc->scopetype = lun->res_type; 7910 } 7911 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7912 res_desc->rel_trgt_port_id); 7913 len = 0; 7914 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7915 if (port != NULL) 7916 len = ctl_create_iid(port, 7917 i % CTL_MAX_INIT_PER_PORT, 7918 res_desc->transport_id); 7919 scsi_ulto4b(len, res_desc->additional_length); 7920 res_desc = (struct scsi_per_res_in_full_desc *) 7921 &res_desc->transport_id[len]; 7922 } 7923 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7924 res_status->header.length); 7925 break; 7926 } 7927 default: 7928 /* 7929 * This is a bug, because we just checked for this above, 7930 * and should have returned an error. 7931 */ 7932 panic("Invalid PR type %x", cdb->action); 7933 break; /* NOTREACHED */ 7934 } 7935 mtx_unlock(&lun->lun_lock); 7936 7937 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7938 ctsio->be_move_done = ctl_config_move_done; 7939 7940 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7941 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7942 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7943 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7944 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7945 7946 ctl_datamove((union ctl_io *)ctsio); 7947 7948 return (CTL_RETVAL_COMPLETE); 7949} 7950 7951/* 7952 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7953 * it should return. 7954 */ 7955static int 7956ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7957 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7958 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7959 struct scsi_per_res_out_parms* param) 7960{ 7961 union ctl_ha_msg persis_io; 7962 int retval, i; 7963 int isc_retval; 7964 7965 retval = 0; 7966 7967 mtx_lock(&lun->lun_lock); 7968 if (sa_res_key == 0) { 7969 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7970 /* validate scope and type */ 7971 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7972 SPR_LU_SCOPE) { 7973 mtx_unlock(&lun->lun_lock); 7974 ctl_set_invalid_field(/*ctsio*/ ctsio, 7975 /*sks_valid*/ 1, 7976 /*command*/ 1, 7977 /*field*/ 2, 7978 /*bit_valid*/ 1, 7979 /*bit*/ 4); 7980 ctl_done((union ctl_io *)ctsio); 7981 return (1); 7982 } 7983 7984 if (type>8 || type==2 || type==4 || type==0) { 7985 mtx_unlock(&lun->lun_lock); 7986 ctl_set_invalid_field(/*ctsio*/ ctsio, 7987 /*sks_valid*/ 1, 7988 /*command*/ 1, 7989 /*field*/ 2, 7990 /*bit_valid*/ 1, 7991 /*bit*/ 0); 7992 ctl_done((union ctl_io *)ctsio); 7993 return (1); 7994 } 7995 7996 /* temporarily unregister this nexus */ 7997 lun->per_res[residx].registered = 0; 7998 7999 /* 8000 * Unregister everybody else and build UA for 8001 * them 8002 */ 8003 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8004 if (lun->per_res[i].registered == 0) 8005 continue; 8006 8007 if (!persis_offset 8008 && i <CTL_MAX_INITIATORS) 8009 lun->pending_ua[i] |= 8010 CTL_UA_REG_PREEMPT; 8011 else if (persis_offset 8012 && i >= persis_offset) 8013 lun->pending_ua[i-persis_offset] |= 8014 CTL_UA_REG_PREEMPT; 8015 lun->per_res[i].registered = 0; 8016 memset(&lun->per_res[i].res_key, 0, 8017 sizeof(struct scsi_per_res_key)); 8018 } 8019 lun->per_res[residx].registered = 1; 8020 lun->pr_key_count = 1; 8021 lun->res_type = type; 8022 if (lun->res_type != SPR_TYPE_WR_EX_AR 8023 && lun->res_type != SPR_TYPE_EX_AC_AR) 8024 lun->pr_res_idx = residx; 8025 8026 /* send msg to other side */ 8027 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8028 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8029 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8030 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8031 persis_io.pr.pr_info.res_type = type; 8032 memcpy(persis_io.pr.pr_info.sa_res_key, 8033 param->serv_act_res_key, 8034 sizeof(param->serv_act_res_key)); 8035 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8036 &persis_io, sizeof(persis_io), 0)) > 8037 CTL_HA_STATUS_SUCCESS) { 8038 printf("CTL:Persis Out error returned " 8039 "from ctl_ha_msg_send %d\n", 8040 isc_retval); 8041 } 8042 } else { 8043 /* not all registrants */ 8044 mtx_unlock(&lun->lun_lock); 8045 free(ctsio->kern_data_ptr, M_CTL); 8046 ctl_set_invalid_field(ctsio, 8047 /*sks_valid*/ 1, 8048 /*command*/ 0, 8049 /*field*/ 8, 8050 /*bit_valid*/ 0, 8051 /*bit*/ 0); 8052 ctl_done((union ctl_io *)ctsio); 8053 return (1); 8054 } 8055 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8056 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8057 int found = 0; 8058 8059 if (res_key == sa_res_key) { 8060 /* special case */ 8061 /* 8062 * The spec implies this is not good but doesn't 8063 * say what to do. There are two choices either 8064 * generate a res conflict or check condition 8065 * with illegal field in parameter data. Since 8066 * that is what is done when the sa_res_key is 8067 * zero I'll take that approach since this has 8068 * to do with the sa_res_key. 8069 */ 8070 mtx_unlock(&lun->lun_lock); 8071 free(ctsio->kern_data_ptr, M_CTL); 8072 ctl_set_invalid_field(ctsio, 8073 /*sks_valid*/ 1, 8074 /*command*/ 0, 8075 /*field*/ 8, 8076 /*bit_valid*/ 0, 8077 /*bit*/ 0); 8078 ctl_done((union ctl_io *)ctsio); 8079 return (1); 8080 } 8081 8082 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8083 if (lun->per_res[i].registered 8084 && memcmp(param->serv_act_res_key, 8085 lun->per_res[i].res_key.key, 8086 sizeof(struct scsi_per_res_key)) != 0) 8087 continue; 8088 8089 found = 1; 8090 lun->per_res[i].registered = 0; 8091 memset(&lun->per_res[i].res_key, 0, 8092 sizeof(struct scsi_per_res_key)); 8093 lun->pr_key_count--; 8094 8095 if (!persis_offset && i < CTL_MAX_INITIATORS) 8096 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8097 else if (persis_offset && i >= persis_offset) 8098 lun->pending_ua[i-persis_offset] |= 8099 CTL_UA_REG_PREEMPT; 8100 } 8101 if (!found) { 8102 mtx_unlock(&lun->lun_lock); 8103 free(ctsio->kern_data_ptr, M_CTL); 8104 ctl_set_reservation_conflict(ctsio); 8105 ctl_done((union ctl_io *)ctsio); 8106 return (CTL_RETVAL_COMPLETE); 8107 } 8108 /* send msg to other side */ 8109 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8110 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8111 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8112 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8113 persis_io.pr.pr_info.res_type = type; 8114 memcpy(persis_io.pr.pr_info.sa_res_key, 8115 param->serv_act_res_key, 8116 sizeof(param->serv_act_res_key)); 8117 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8118 &persis_io, sizeof(persis_io), 0)) > 8119 CTL_HA_STATUS_SUCCESS) { 8120 printf("CTL:Persis Out error returned from " 8121 "ctl_ha_msg_send %d\n", isc_retval); 8122 } 8123 } else { 8124 /* Reserved but not all registrants */ 8125 /* sa_res_key is res holder */ 8126 if (memcmp(param->serv_act_res_key, 8127 lun->per_res[lun->pr_res_idx].res_key.key, 8128 sizeof(struct scsi_per_res_key)) == 0) { 8129 /* validate scope and type */ 8130 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8131 SPR_LU_SCOPE) { 8132 mtx_unlock(&lun->lun_lock); 8133 ctl_set_invalid_field(/*ctsio*/ ctsio, 8134 /*sks_valid*/ 1, 8135 /*command*/ 1, 8136 /*field*/ 2, 8137 /*bit_valid*/ 1, 8138 /*bit*/ 4); 8139 ctl_done((union ctl_io *)ctsio); 8140 return (1); 8141 } 8142 8143 if (type>8 || type==2 || type==4 || type==0) { 8144 mtx_unlock(&lun->lun_lock); 8145 ctl_set_invalid_field(/*ctsio*/ ctsio, 8146 /*sks_valid*/ 1, 8147 /*command*/ 1, 8148 /*field*/ 2, 8149 /*bit_valid*/ 1, 8150 /*bit*/ 0); 8151 ctl_done((union ctl_io *)ctsio); 8152 return (1); 8153 } 8154 8155 /* 8156 * Do the following: 8157 * if sa_res_key != res_key remove all 8158 * registrants w/sa_res_key and generate UA 8159 * for these registrants(Registrations 8160 * Preempted) if it wasn't an exclusive 8161 * reservation generate UA(Reservations 8162 * Preempted) for all other registered nexuses 8163 * if the type has changed. Establish the new 8164 * reservation and holder. If res_key and 8165 * sa_res_key are the same do the above 8166 * except don't unregister the res holder. 8167 */ 8168 8169 /* 8170 * Temporarily unregister so it won't get 8171 * removed or UA generated 8172 */ 8173 lun->per_res[residx].registered = 0; 8174 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8175 if (lun->per_res[i].registered == 0) 8176 continue; 8177 8178 if (memcmp(param->serv_act_res_key, 8179 lun->per_res[i].res_key.key, 8180 sizeof(struct scsi_per_res_key)) == 0) { 8181 lun->per_res[i].registered = 0; 8182 memset(&lun->per_res[i].res_key, 8183 0, 8184 sizeof(struct scsi_per_res_key)); 8185 lun->pr_key_count--; 8186 8187 if (!persis_offset 8188 && i < CTL_MAX_INITIATORS) 8189 lun->pending_ua[i] |= 8190 CTL_UA_REG_PREEMPT; 8191 else if (persis_offset 8192 && i >= persis_offset) 8193 lun->pending_ua[i-persis_offset] |= 8194 CTL_UA_REG_PREEMPT; 8195 } else if (type != lun->res_type 8196 && (lun->res_type == SPR_TYPE_WR_EX_RO 8197 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8198 if (!persis_offset 8199 && i < CTL_MAX_INITIATORS) 8200 lun->pending_ua[i] |= 8201 CTL_UA_RES_RELEASE; 8202 else if (persis_offset 8203 && i >= persis_offset) 8204 lun->pending_ua[ 8205 i-persis_offset] |= 8206 CTL_UA_RES_RELEASE; 8207 } 8208 } 8209 lun->per_res[residx].registered = 1; 8210 lun->res_type = type; 8211 if (lun->res_type != SPR_TYPE_WR_EX_AR 8212 && lun->res_type != SPR_TYPE_EX_AC_AR) 8213 lun->pr_res_idx = residx; 8214 else 8215 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8216 8217 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8218 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8219 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8220 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8221 persis_io.pr.pr_info.res_type = type; 8222 memcpy(persis_io.pr.pr_info.sa_res_key, 8223 param->serv_act_res_key, 8224 sizeof(param->serv_act_res_key)); 8225 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8226 &persis_io, sizeof(persis_io), 0)) > 8227 CTL_HA_STATUS_SUCCESS) { 8228 printf("CTL:Persis Out error returned " 8229 "from ctl_ha_msg_send %d\n", 8230 isc_retval); 8231 } 8232 } else { 8233 /* 8234 * sa_res_key is not the res holder just 8235 * remove registrants 8236 */ 8237 int found=0; 8238 8239 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8240 if (memcmp(param->serv_act_res_key, 8241 lun->per_res[i].res_key.key, 8242 sizeof(struct scsi_per_res_key)) != 0) 8243 continue; 8244 8245 found = 1; 8246 lun->per_res[i].registered = 0; 8247 memset(&lun->per_res[i].res_key, 0, 8248 sizeof(struct scsi_per_res_key)); 8249 lun->pr_key_count--; 8250 8251 if (!persis_offset 8252 && i < CTL_MAX_INITIATORS) 8253 lun->pending_ua[i] |= 8254 CTL_UA_REG_PREEMPT; 8255 else if (persis_offset 8256 && i >= persis_offset) 8257 lun->pending_ua[i-persis_offset] |= 8258 CTL_UA_REG_PREEMPT; 8259 } 8260 8261 if (!found) { 8262 mtx_unlock(&lun->lun_lock); 8263 free(ctsio->kern_data_ptr, M_CTL); 8264 ctl_set_reservation_conflict(ctsio); 8265 ctl_done((union ctl_io *)ctsio); 8266 return (1); 8267 } 8268 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8269 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8270 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8271 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8272 persis_io.pr.pr_info.res_type = type; 8273 memcpy(persis_io.pr.pr_info.sa_res_key, 8274 param->serv_act_res_key, 8275 sizeof(param->serv_act_res_key)); 8276 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8277 &persis_io, sizeof(persis_io), 0)) > 8278 CTL_HA_STATUS_SUCCESS) { 8279 printf("CTL:Persis Out error returned " 8280 "from ctl_ha_msg_send %d\n", 8281 isc_retval); 8282 } 8283 } 8284 } 8285 8286 lun->PRGeneration++; 8287 mtx_unlock(&lun->lun_lock); 8288 8289 return (retval); 8290} 8291 8292static void 8293ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8294{ 8295 int i; 8296 8297 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8298 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8299 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8300 msg->pr.pr_info.sa_res_key, 8301 sizeof(struct scsi_per_res_key)) != 0) { 8302 uint64_t sa_res_key; 8303 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8304 8305 if (sa_res_key == 0) { 8306 /* temporarily unregister this nexus */ 8307 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8308 8309 /* 8310 * Unregister everybody else and build UA for 8311 * them 8312 */ 8313 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8314 if (lun->per_res[i].registered == 0) 8315 continue; 8316 8317 if (!persis_offset 8318 && i < CTL_MAX_INITIATORS) 8319 lun->pending_ua[i] |= 8320 CTL_UA_REG_PREEMPT; 8321 else if (persis_offset && i >= persis_offset) 8322 lun->pending_ua[i - persis_offset] |= 8323 CTL_UA_REG_PREEMPT; 8324 lun->per_res[i].registered = 0; 8325 memset(&lun->per_res[i].res_key, 0, 8326 sizeof(struct scsi_per_res_key)); 8327 } 8328 8329 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8330 lun->pr_key_count = 1; 8331 lun->res_type = msg->pr.pr_info.res_type; 8332 if (lun->res_type != SPR_TYPE_WR_EX_AR 8333 && lun->res_type != SPR_TYPE_EX_AC_AR) 8334 lun->pr_res_idx = msg->pr.pr_info.residx; 8335 } else { 8336 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8337 if (memcmp(msg->pr.pr_info.sa_res_key, 8338 lun->per_res[i].res_key.key, 8339 sizeof(struct scsi_per_res_key)) != 0) 8340 continue; 8341 8342 lun->per_res[i].registered = 0; 8343 memset(&lun->per_res[i].res_key, 0, 8344 sizeof(struct scsi_per_res_key)); 8345 lun->pr_key_count--; 8346 8347 if (!persis_offset 8348 && i < persis_offset) 8349 lun->pending_ua[i] |= 8350 CTL_UA_REG_PREEMPT; 8351 else if (persis_offset 8352 && i >= persis_offset) 8353 lun->pending_ua[i - persis_offset] |= 8354 CTL_UA_REG_PREEMPT; 8355 } 8356 } 8357 } else { 8358 /* 8359 * Temporarily unregister so it won't get removed 8360 * or UA generated 8361 */ 8362 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8363 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8364 if (lun->per_res[i].registered == 0) 8365 continue; 8366 8367 if (memcmp(msg->pr.pr_info.sa_res_key, 8368 lun->per_res[i].res_key.key, 8369 sizeof(struct scsi_per_res_key)) == 0) { 8370 lun->per_res[i].registered = 0; 8371 memset(&lun->per_res[i].res_key, 0, 8372 sizeof(struct scsi_per_res_key)); 8373 lun->pr_key_count--; 8374 if (!persis_offset 8375 && i < CTL_MAX_INITIATORS) 8376 lun->pending_ua[i] |= 8377 CTL_UA_REG_PREEMPT; 8378 else if (persis_offset 8379 && i >= persis_offset) 8380 lun->pending_ua[i - persis_offset] |= 8381 CTL_UA_REG_PREEMPT; 8382 } else if (msg->pr.pr_info.res_type != lun->res_type 8383 && (lun->res_type == SPR_TYPE_WR_EX_RO 8384 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8385 if (!persis_offset 8386 && i < persis_offset) 8387 lun->pending_ua[i] |= 8388 CTL_UA_RES_RELEASE; 8389 else if (persis_offset 8390 && i >= persis_offset) 8391 lun->pending_ua[i - persis_offset] |= 8392 CTL_UA_RES_RELEASE; 8393 } 8394 } 8395 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8396 lun->res_type = msg->pr.pr_info.res_type; 8397 if (lun->res_type != SPR_TYPE_WR_EX_AR 8398 && lun->res_type != SPR_TYPE_EX_AC_AR) 8399 lun->pr_res_idx = msg->pr.pr_info.residx; 8400 else 8401 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8402 } 8403 lun->PRGeneration++; 8404 8405} 8406 8407 8408int 8409ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8410{ 8411 int retval; 8412 int isc_retval; 8413 u_int32_t param_len; 8414 struct scsi_per_res_out *cdb; 8415 struct ctl_lun *lun; 8416 struct scsi_per_res_out_parms* param; 8417 struct ctl_softc *softc; 8418 uint32_t residx; 8419 uint64_t res_key, sa_res_key; 8420 uint8_t type; 8421 union ctl_ha_msg persis_io; 8422 int i; 8423 8424 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8425 8426 retval = CTL_RETVAL_COMPLETE; 8427 8428 softc = control_softc; 8429 8430 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8431 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8432 8433 /* 8434 * We only support whole-LUN scope. The scope & type are ignored for 8435 * register, register and ignore existing key and clear. 8436 * We sometimes ignore scope and type on preempts too!! 8437 * Verify reservation type here as well. 8438 */ 8439 type = cdb->scope_type & SPR_TYPE_MASK; 8440 if ((cdb->action == SPRO_RESERVE) 8441 || (cdb->action == SPRO_RELEASE)) { 8442 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8443 ctl_set_invalid_field(/*ctsio*/ ctsio, 8444 /*sks_valid*/ 1, 8445 /*command*/ 1, 8446 /*field*/ 2, 8447 /*bit_valid*/ 1, 8448 /*bit*/ 4); 8449 ctl_done((union ctl_io *)ctsio); 8450 return (CTL_RETVAL_COMPLETE); 8451 } 8452 8453 if (type>8 || type==2 || type==4 || type==0) { 8454 ctl_set_invalid_field(/*ctsio*/ ctsio, 8455 /*sks_valid*/ 1, 8456 /*command*/ 1, 8457 /*field*/ 2, 8458 /*bit_valid*/ 1, 8459 /*bit*/ 0); 8460 ctl_done((union ctl_io *)ctsio); 8461 return (CTL_RETVAL_COMPLETE); 8462 } 8463 } 8464 8465 param_len = scsi_4btoul(cdb->length); 8466 8467 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8468 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8469 ctsio->kern_data_len = param_len; 8470 ctsio->kern_total_len = param_len; 8471 ctsio->kern_data_resid = 0; 8472 ctsio->kern_rel_offset = 0; 8473 ctsio->kern_sg_entries = 0; 8474 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8475 ctsio->be_move_done = ctl_config_move_done; 8476 ctl_datamove((union ctl_io *)ctsio); 8477 8478 return (CTL_RETVAL_COMPLETE); 8479 } 8480 8481 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8482 8483 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8484 res_key = scsi_8btou64(param->res_key.key); 8485 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8486 8487 /* 8488 * Validate the reservation key here except for SPRO_REG_IGNO 8489 * This must be done for all other service actions 8490 */ 8491 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8492 mtx_lock(&lun->lun_lock); 8493 if (lun->per_res[residx].registered) { 8494 if (memcmp(param->res_key.key, 8495 lun->per_res[residx].res_key.key, 8496 ctl_min(sizeof(param->res_key), 8497 sizeof(lun->per_res[residx].res_key))) != 0) { 8498 /* 8499 * The current key passed in doesn't match 8500 * the one the initiator previously 8501 * registered. 8502 */ 8503 mtx_unlock(&lun->lun_lock); 8504 free(ctsio->kern_data_ptr, M_CTL); 8505 ctl_set_reservation_conflict(ctsio); 8506 ctl_done((union ctl_io *)ctsio); 8507 return (CTL_RETVAL_COMPLETE); 8508 } 8509 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8510 /* 8511 * We are not registered 8512 */ 8513 mtx_unlock(&lun->lun_lock); 8514 free(ctsio->kern_data_ptr, M_CTL); 8515 ctl_set_reservation_conflict(ctsio); 8516 ctl_done((union ctl_io *)ctsio); 8517 return (CTL_RETVAL_COMPLETE); 8518 } else if (res_key != 0) { 8519 /* 8520 * We are not registered and trying to register but 8521 * the register key isn't zero. 8522 */ 8523 mtx_unlock(&lun->lun_lock); 8524 free(ctsio->kern_data_ptr, M_CTL); 8525 ctl_set_reservation_conflict(ctsio); 8526 ctl_done((union ctl_io *)ctsio); 8527 return (CTL_RETVAL_COMPLETE); 8528 } 8529 mtx_unlock(&lun->lun_lock); 8530 } 8531 8532 switch (cdb->action & SPRO_ACTION_MASK) { 8533 case SPRO_REGISTER: 8534 case SPRO_REG_IGNO: { 8535 8536#if 0 8537 printf("Registration received\n"); 8538#endif 8539 8540 /* 8541 * We don't support any of these options, as we report in 8542 * the read capabilities request (see 8543 * ctl_persistent_reserve_in(), above). 8544 */ 8545 if ((param->flags & SPR_SPEC_I_PT) 8546 || (param->flags & SPR_ALL_TG_PT) 8547 || (param->flags & SPR_APTPL)) { 8548 int bit_ptr; 8549 8550 if (param->flags & SPR_APTPL) 8551 bit_ptr = 0; 8552 else if (param->flags & SPR_ALL_TG_PT) 8553 bit_ptr = 2; 8554 else /* SPR_SPEC_I_PT */ 8555 bit_ptr = 3; 8556 8557 free(ctsio->kern_data_ptr, M_CTL); 8558 ctl_set_invalid_field(ctsio, 8559 /*sks_valid*/ 1, 8560 /*command*/ 0, 8561 /*field*/ 20, 8562 /*bit_valid*/ 1, 8563 /*bit*/ bit_ptr); 8564 ctl_done((union ctl_io *)ctsio); 8565 return (CTL_RETVAL_COMPLETE); 8566 } 8567 8568 mtx_lock(&lun->lun_lock); 8569 8570 /* 8571 * The initiator wants to clear the 8572 * key/unregister. 8573 */ 8574 if (sa_res_key == 0) { 8575 if ((res_key == 0 8576 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8577 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8578 && !lun->per_res[residx].registered)) { 8579 mtx_unlock(&lun->lun_lock); 8580 goto done; 8581 } 8582 8583 lun->per_res[residx].registered = 0; 8584 memset(&lun->per_res[residx].res_key, 8585 0, sizeof(lun->per_res[residx].res_key)); 8586 lun->pr_key_count--; 8587 8588 if (residx == lun->pr_res_idx) { 8589 lun->flags &= ~CTL_LUN_PR_RESERVED; 8590 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8591 8592 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8593 || lun->res_type == SPR_TYPE_EX_AC_RO) 8594 && lun->pr_key_count) { 8595 /* 8596 * If the reservation is a registrants 8597 * only type we need to generate a UA 8598 * for other registered inits. The 8599 * sense code should be RESERVATIONS 8600 * RELEASED 8601 */ 8602 8603 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8604 if (lun->per_res[ 8605 i+persis_offset].registered 8606 == 0) 8607 continue; 8608 lun->pending_ua[i] |= 8609 CTL_UA_RES_RELEASE; 8610 } 8611 } 8612 lun->res_type = 0; 8613 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8614 if (lun->pr_key_count==0) { 8615 lun->flags &= ~CTL_LUN_PR_RESERVED; 8616 lun->res_type = 0; 8617 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8618 } 8619 } 8620 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8621 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8622 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8623 persis_io.pr.pr_info.residx = residx; 8624 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8625 &persis_io, sizeof(persis_io), 0 )) > 8626 CTL_HA_STATUS_SUCCESS) { 8627 printf("CTL:Persis Out error returned from " 8628 "ctl_ha_msg_send %d\n", isc_retval); 8629 } 8630 } else /* sa_res_key != 0 */ { 8631 8632 /* 8633 * If we aren't registered currently then increment 8634 * the key count and set the registered flag. 8635 */ 8636 if (!lun->per_res[residx].registered) { 8637 lun->pr_key_count++; 8638 lun->per_res[residx].registered = 1; 8639 } 8640 8641 memcpy(&lun->per_res[residx].res_key, 8642 param->serv_act_res_key, 8643 ctl_min(sizeof(param->serv_act_res_key), 8644 sizeof(lun->per_res[residx].res_key))); 8645 8646 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8647 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8648 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8649 persis_io.pr.pr_info.residx = residx; 8650 memcpy(persis_io.pr.pr_info.sa_res_key, 8651 param->serv_act_res_key, 8652 sizeof(param->serv_act_res_key)); 8653 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8654 &persis_io, sizeof(persis_io), 0)) > 8655 CTL_HA_STATUS_SUCCESS) { 8656 printf("CTL:Persis Out error returned from " 8657 "ctl_ha_msg_send %d\n", isc_retval); 8658 } 8659 } 8660 lun->PRGeneration++; 8661 mtx_unlock(&lun->lun_lock); 8662 8663 break; 8664 } 8665 case SPRO_RESERVE: 8666#if 0 8667 printf("Reserve executed type %d\n", type); 8668#endif 8669 mtx_lock(&lun->lun_lock); 8670 if (lun->flags & CTL_LUN_PR_RESERVED) { 8671 /* 8672 * if this isn't the reservation holder and it's 8673 * not a "all registrants" type or if the type is 8674 * different then we have a conflict 8675 */ 8676 if ((lun->pr_res_idx != residx 8677 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8678 || lun->res_type != type) { 8679 mtx_unlock(&lun->lun_lock); 8680 free(ctsio->kern_data_ptr, M_CTL); 8681 ctl_set_reservation_conflict(ctsio); 8682 ctl_done((union ctl_io *)ctsio); 8683 return (CTL_RETVAL_COMPLETE); 8684 } 8685 mtx_unlock(&lun->lun_lock); 8686 } else /* create a reservation */ { 8687 /* 8688 * If it's not an "all registrants" type record 8689 * reservation holder 8690 */ 8691 if (type != SPR_TYPE_WR_EX_AR 8692 && type != SPR_TYPE_EX_AC_AR) 8693 lun->pr_res_idx = residx; /* Res holder */ 8694 else 8695 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8696 8697 lun->flags |= CTL_LUN_PR_RESERVED; 8698 lun->res_type = type; 8699 8700 mtx_unlock(&lun->lun_lock); 8701 8702 /* send msg to other side */ 8703 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8704 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8705 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8706 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8707 persis_io.pr.pr_info.res_type = type; 8708 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8709 &persis_io, sizeof(persis_io), 0)) > 8710 CTL_HA_STATUS_SUCCESS) { 8711 printf("CTL:Persis Out error returned from " 8712 "ctl_ha_msg_send %d\n", isc_retval); 8713 } 8714 } 8715 break; 8716 8717 case SPRO_RELEASE: 8718 mtx_lock(&lun->lun_lock); 8719 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8720 /* No reservation exists return good status */ 8721 mtx_unlock(&lun->lun_lock); 8722 goto done; 8723 } 8724 /* 8725 * Is this nexus a reservation holder? 8726 */ 8727 if (lun->pr_res_idx != residx 8728 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8729 /* 8730 * not a res holder return good status but 8731 * do nothing 8732 */ 8733 mtx_unlock(&lun->lun_lock); 8734 goto done; 8735 } 8736 8737 if (lun->res_type != type) { 8738 mtx_unlock(&lun->lun_lock); 8739 free(ctsio->kern_data_ptr, M_CTL); 8740 ctl_set_illegal_pr_release(ctsio); 8741 ctl_done((union ctl_io *)ctsio); 8742 return (CTL_RETVAL_COMPLETE); 8743 } 8744 8745 /* okay to release */ 8746 lun->flags &= ~CTL_LUN_PR_RESERVED; 8747 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8748 lun->res_type = 0; 8749 8750 /* 8751 * if this isn't an exclusive access 8752 * res generate UA for all other 8753 * registrants. 8754 */ 8755 if (type != SPR_TYPE_EX_AC 8756 && type != SPR_TYPE_WR_EX) { 8757 /* 8758 * temporarily unregister so we don't generate UA 8759 */ 8760 lun->per_res[residx].registered = 0; 8761 8762 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8763 if (lun->per_res[i+persis_offset].registered 8764 == 0) 8765 continue; 8766 lun->pending_ua[i] |= 8767 CTL_UA_RES_RELEASE; 8768 } 8769 8770 lun->per_res[residx].registered = 1; 8771 } 8772 mtx_unlock(&lun->lun_lock); 8773 /* Send msg to other side */ 8774 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8775 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8776 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8777 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8778 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8779 printf("CTL:Persis Out error returned from " 8780 "ctl_ha_msg_send %d\n", isc_retval); 8781 } 8782 break; 8783 8784 case SPRO_CLEAR: 8785 /* send msg to other side */ 8786 8787 mtx_lock(&lun->lun_lock); 8788 lun->flags &= ~CTL_LUN_PR_RESERVED; 8789 lun->res_type = 0; 8790 lun->pr_key_count = 0; 8791 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8792 8793 8794 memset(&lun->per_res[residx].res_key, 8795 0, sizeof(lun->per_res[residx].res_key)); 8796 lun->per_res[residx].registered = 0; 8797 8798 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8799 if (lun->per_res[i].registered) { 8800 if (!persis_offset && i < CTL_MAX_INITIATORS) 8801 lun->pending_ua[i] |= 8802 CTL_UA_RES_PREEMPT; 8803 else if (persis_offset && i >= persis_offset) 8804 lun->pending_ua[i-persis_offset] |= 8805 CTL_UA_RES_PREEMPT; 8806 8807 memset(&lun->per_res[i].res_key, 8808 0, sizeof(struct scsi_per_res_key)); 8809 lun->per_res[i].registered = 0; 8810 } 8811 lun->PRGeneration++; 8812 mtx_unlock(&lun->lun_lock); 8813 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8814 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8815 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8816 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8817 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8818 printf("CTL:Persis Out error returned from " 8819 "ctl_ha_msg_send %d\n", isc_retval); 8820 } 8821 break; 8822 8823 case SPRO_PREEMPT: { 8824 int nretval; 8825 8826 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8827 residx, ctsio, cdb, param); 8828 if (nretval != 0) 8829 return (CTL_RETVAL_COMPLETE); 8830 break; 8831 } 8832 default: 8833 panic("Invalid PR type %x", cdb->action); 8834 } 8835 8836done: 8837 free(ctsio->kern_data_ptr, M_CTL); 8838 ctl_set_success(ctsio); 8839 ctl_done((union ctl_io *)ctsio); 8840 8841 return (retval); 8842} 8843 8844/* 8845 * This routine is for handling a message from the other SC pertaining to 8846 * persistent reserve out. All the error checking will have been done 8847 * so only perorming the action need be done here to keep the two 8848 * in sync. 8849 */ 8850static void 8851ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8852{ 8853 struct ctl_lun *lun; 8854 struct ctl_softc *softc; 8855 int i; 8856 uint32_t targ_lun; 8857 8858 softc = control_softc; 8859 8860 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8861 lun = softc->ctl_luns[targ_lun]; 8862 mtx_lock(&lun->lun_lock); 8863 switch(msg->pr.pr_info.action) { 8864 case CTL_PR_REG_KEY: 8865 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8866 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8867 lun->pr_key_count++; 8868 } 8869 lun->PRGeneration++; 8870 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8871 msg->pr.pr_info.sa_res_key, 8872 sizeof(struct scsi_per_res_key)); 8873 break; 8874 8875 case CTL_PR_UNREG_KEY: 8876 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8877 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8878 0, sizeof(struct scsi_per_res_key)); 8879 lun->pr_key_count--; 8880 8881 /* XXX Need to see if the reservation has been released */ 8882 /* if so do we need to generate UA? */ 8883 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8884 lun->flags &= ~CTL_LUN_PR_RESERVED; 8885 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8886 8887 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8888 || lun->res_type == SPR_TYPE_EX_AC_RO) 8889 && lun->pr_key_count) { 8890 /* 8891 * If the reservation is a registrants 8892 * only type we need to generate a UA 8893 * for other registered inits. The 8894 * sense code should be RESERVATIONS 8895 * RELEASED 8896 */ 8897 8898 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8899 if (lun->per_res[i+ 8900 persis_offset].registered == 0) 8901 continue; 8902 8903 lun->pending_ua[i] |= 8904 CTL_UA_RES_RELEASE; 8905 } 8906 } 8907 lun->res_type = 0; 8908 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8909 if (lun->pr_key_count==0) { 8910 lun->flags &= ~CTL_LUN_PR_RESERVED; 8911 lun->res_type = 0; 8912 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8913 } 8914 } 8915 lun->PRGeneration++; 8916 break; 8917 8918 case CTL_PR_RESERVE: 8919 lun->flags |= CTL_LUN_PR_RESERVED; 8920 lun->res_type = msg->pr.pr_info.res_type; 8921 lun->pr_res_idx = msg->pr.pr_info.residx; 8922 8923 break; 8924 8925 case CTL_PR_RELEASE: 8926 /* 8927 * if this isn't an exclusive access res generate UA for all 8928 * other registrants. 8929 */ 8930 if (lun->res_type != SPR_TYPE_EX_AC 8931 && lun->res_type != SPR_TYPE_WR_EX) { 8932 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8933 if (lun->per_res[i+persis_offset].registered) 8934 lun->pending_ua[i] |= 8935 CTL_UA_RES_RELEASE; 8936 } 8937 8938 lun->flags &= ~CTL_LUN_PR_RESERVED; 8939 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8940 lun->res_type = 0; 8941 break; 8942 8943 case CTL_PR_PREEMPT: 8944 ctl_pro_preempt_other(lun, msg); 8945 break; 8946 case CTL_PR_CLEAR: 8947 lun->flags &= ~CTL_LUN_PR_RESERVED; 8948 lun->res_type = 0; 8949 lun->pr_key_count = 0; 8950 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8951 8952 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8953 if (lun->per_res[i].registered == 0) 8954 continue; 8955 if (!persis_offset 8956 && i < CTL_MAX_INITIATORS) 8957 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8958 else if (persis_offset 8959 && i >= persis_offset) 8960 lun->pending_ua[i-persis_offset] |= 8961 CTL_UA_RES_PREEMPT; 8962 memset(&lun->per_res[i].res_key, 0, 8963 sizeof(struct scsi_per_res_key)); 8964 lun->per_res[i].registered = 0; 8965 } 8966 lun->PRGeneration++; 8967 break; 8968 } 8969 8970 mtx_unlock(&lun->lun_lock); 8971} 8972 8973int 8974ctl_read_write(struct ctl_scsiio *ctsio) 8975{ 8976 struct ctl_lun *lun; 8977 struct ctl_lba_len_flags *lbalen; 8978 uint64_t lba; 8979 uint32_t num_blocks; 8980 int fua, dpo; 8981 int retval; 8982 int isread; 8983 8984 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8985 8986 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8987 8988 fua = 0; 8989 dpo = 0; 8990 8991 retval = CTL_RETVAL_COMPLETE; 8992 8993 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8994 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8995 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8996 uint32_t residx; 8997 8998 /* 8999 * XXX KDM need a lock here. 9000 */ 9001 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9002 if ((lun->res_type == SPR_TYPE_EX_AC 9003 && residx != lun->pr_res_idx) 9004 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9005 || lun->res_type == SPR_TYPE_EX_AC_AR) 9006 && !lun->per_res[residx].registered)) { 9007 ctl_set_reservation_conflict(ctsio); 9008 ctl_done((union ctl_io *)ctsio); 9009 return (CTL_RETVAL_COMPLETE); 9010 } 9011 } 9012 9013 switch (ctsio->cdb[0]) { 9014 case READ_6: 9015 case WRITE_6: { 9016 struct scsi_rw_6 *cdb; 9017 9018 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9019 9020 lba = scsi_3btoul(cdb->addr); 9021 /* only 5 bits are valid in the most significant address byte */ 9022 lba &= 0x1fffff; 9023 num_blocks = cdb->length; 9024 /* 9025 * This is correct according to SBC-2. 9026 */ 9027 if (num_blocks == 0) 9028 num_blocks = 256; 9029 break; 9030 } 9031 case READ_10: 9032 case WRITE_10: { 9033 struct scsi_rw_10 *cdb; 9034 9035 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9036 9037 if (cdb->byte2 & SRW10_FUA) 9038 fua = 1; 9039 if (cdb->byte2 & SRW10_DPO) 9040 dpo = 1; 9041 9042 lba = scsi_4btoul(cdb->addr); 9043 num_blocks = scsi_2btoul(cdb->length); 9044 break; 9045 } 9046 case WRITE_VERIFY_10: { 9047 struct scsi_write_verify_10 *cdb; 9048 9049 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9050 9051 /* 9052 * XXX KDM we should do actual write verify support at some 9053 * point. This is obviously fake, we're just translating 9054 * things to a write. So we don't even bother checking the 9055 * BYTCHK field, since we don't do any verification. If 9056 * the user asks for it, we'll just pretend we did it. 9057 */ 9058 if (cdb->byte2 & SWV_DPO) 9059 dpo = 1; 9060 9061 lba = scsi_4btoul(cdb->addr); 9062 num_blocks = scsi_2btoul(cdb->length); 9063 break; 9064 } 9065 case READ_12: 9066 case WRITE_12: { 9067 struct scsi_rw_12 *cdb; 9068 9069 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9070 9071 if (cdb->byte2 & SRW12_FUA) 9072 fua = 1; 9073 if (cdb->byte2 & SRW12_DPO) 9074 dpo = 1; 9075 lba = scsi_4btoul(cdb->addr); 9076 num_blocks = scsi_4btoul(cdb->length); 9077 break; 9078 } 9079 case WRITE_VERIFY_12: { 9080 struct scsi_write_verify_12 *cdb; 9081 9082 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9083 9084 if (cdb->byte2 & SWV_DPO) 9085 dpo = 1; 9086 9087 lba = scsi_4btoul(cdb->addr); 9088 num_blocks = scsi_4btoul(cdb->length); 9089 9090 break; 9091 } 9092 case READ_16: 9093 case WRITE_16: { 9094 struct scsi_rw_16 *cdb; 9095 9096 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9097 9098 if (cdb->byte2 & SRW12_FUA) 9099 fua = 1; 9100 if (cdb->byte2 & SRW12_DPO) 9101 dpo = 1; 9102 9103 lba = scsi_8btou64(cdb->addr); 9104 num_blocks = scsi_4btoul(cdb->length); 9105 break; 9106 } 9107 case WRITE_VERIFY_16: { 9108 struct scsi_write_verify_16 *cdb; 9109 9110 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9111 9112 if (cdb->byte2 & SWV_DPO) 9113 dpo = 1; 9114 9115 lba = scsi_8btou64(cdb->addr); 9116 num_blocks = scsi_4btoul(cdb->length); 9117 break; 9118 } 9119 default: 9120 /* 9121 * We got a command we don't support. This shouldn't 9122 * happen, commands should be filtered out above us. 9123 */ 9124 ctl_set_invalid_opcode(ctsio); 9125 ctl_done((union ctl_io *)ctsio); 9126 9127 return (CTL_RETVAL_COMPLETE); 9128 break; /* NOTREACHED */ 9129 } 9130 9131 /* 9132 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9133 * interesting for us, but if RAIDCore is in write-back mode, 9134 * getting it to do write-through for a particular transaction may 9135 * not be possible. 9136 */ 9137 9138 /* 9139 * The first check is to make sure we're in bounds, the second 9140 * check is to catch wrap-around problems. If the lba + num blocks 9141 * is less than the lba, then we've wrapped around and the block 9142 * range is invalid anyway. 9143 */ 9144 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9145 || ((lba + num_blocks) < lba)) { 9146 ctl_set_lba_out_of_range(ctsio); 9147 ctl_done((union ctl_io *)ctsio); 9148 return (CTL_RETVAL_COMPLETE); 9149 } 9150 9151 /* 9152 * According to SBC-3, a transfer length of 0 is not an error. 9153 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9154 * translates to 256 blocks for those commands. 9155 */ 9156 if (num_blocks == 0) { 9157 ctl_set_success(ctsio); 9158 ctl_done((union ctl_io *)ctsio); 9159 return (CTL_RETVAL_COMPLETE); 9160 } 9161 9162 lbalen = (struct ctl_lba_len_flags *) 9163 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9164 lbalen->lba = lba; 9165 lbalen->len = num_blocks; 9166 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 9167 9168 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9169 ctsio->kern_rel_offset = 0; 9170 9171 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9172 9173 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9174 9175 return (retval); 9176} 9177 9178static int 9179ctl_cnw_cont(union ctl_io *io) 9180{ 9181 struct ctl_scsiio *ctsio; 9182 struct ctl_lun *lun; 9183 struct ctl_lba_len_flags *lbalen; 9184 int retval; 9185 9186 ctsio = &io->scsiio; 9187 ctsio->io_hdr.status = CTL_STATUS_NONE; 9188 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9189 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9190 lbalen = (struct ctl_lba_len_flags *) 9191 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9192 lbalen->flags = CTL_LLF_WRITE; 9193 9194 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9195 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9196 return (retval); 9197} 9198 9199int 9200ctl_cnw(struct ctl_scsiio *ctsio) 9201{ 9202 struct ctl_lun *lun; 9203 struct ctl_lba_len_flags *lbalen; 9204 uint64_t lba; 9205 uint32_t num_blocks; 9206 int fua, dpo; 9207 int retval; 9208 9209 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9210 9211 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9212 9213 fua = 0; 9214 dpo = 0; 9215 9216 retval = CTL_RETVAL_COMPLETE; 9217 9218 switch (ctsio->cdb[0]) { 9219 case COMPARE_AND_WRITE: { 9220 struct scsi_compare_and_write *cdb; 9221 9222 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9223 9224 if (cdb->byte2 & SRW10_FUA) 9225 fua = 1; 9226 if (cdb->byte2 & SRW10_DPO) 9227 dpo = 1; 9228 lba = scsi_8btou64(cdb->addr); 9229 num_blocks = cdb->length; 9230 break; 9231 } 9232 default: 9233 /* 9234 * We got a command we don't support. This shouldn't 9235 * happen, commands should be filtered out above us. 9236 */ 9237 ctl_set_invalid_opcode(ctsio); 9238 ctl_done((union ctl_io *)ctsio); 9239 9240 return (CTL_RETVAL_COMPLETE); 9241 break; /* NOTREACHED */ 9242 } 9243 9244 /* 9245 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9246 * interesting for us, but if RAIDCore is in write-back mode, 9247 * getting it to do write-through for a particular transaction may 9248 * not be possible. 9249 */ 9250 9251 /* 9252 * The first check is to make sure we're in bounds, the second 9253 * check is to catch wrap-around problems. If the lba + num blocks 9254 * is less than the lba, then we've wrapped around and the block 9255 * range is invalid anyway. 9256 */ 9257 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9258 || ((lba + num_blocks) < lba)) { 9259 ctl_set_lba_out_of_range(ctsio); 9260 ctl_done((union ctl_io *)ctsio); 9261 return (CTL_RETVAL_COMPLETE); 9262 } 9263 9264 /* 9265 * According to SBC-3, a transfer length of 0 is not an error. 9266 */ 9267 if (num_blocks == 0) { 9268 ctl_set_success(ctsio); 9269 ctl_done((union ctl_io *)ctsio); 9270 return (CTL_RETVAL_COMPLETE); 9271 } 9272 9273 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9274 ctsio->kern_rel_offset = 0; 9275 9276 /* 9277 * Set the IO_CONT flag, so that if this I/O gets passed to 9278 * ctl_data_submit_done(), it'll get passed back to 9279 * ctl_ctl_cnw_cont() for further processing. 9280 */ 9281 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9282 ctsio->io_cont = ctl_cnw_cont; 9283 9284 lbalen = (struct ctl_lba_len_flags *) 9285 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9286 lbalen->lba = lba; 9287 lbalen->len = num_blocks; 9288 lbalen->flags = CTL_LLF_COMPARE; 9289 9290 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9291 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9292 return (retval); 9293} 9294 9295int 9296ctl_verify(struct ctl_scsiio *ctsio) 9297{ 9298 struct ctl_lun *lun; 9299 struct ctl_lba_len_flags *lbalen; 9300 uint64_t lba; 9301 uint32_t num_blocks; 9302 int bytchk, dpo; 9303 int retval; 9304 9305 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9306 9307 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9308 9309 bytchk = 0; 9310 dpo = 0; 9311 retval = CTL_RETVAL_COMPLETE; 9312 9313 switch (ctsio->cdb[0]) { 9314 case VERIFY_10: { 9315 struct scsi_verify_10 *cdb; 9316 9317 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9318 if (cdb->byte2 & SVFY_BYTCHK) 9319 bytchk = 1; 9320 if (cdb->byte2 & SVFY_DPO) 9321 dpo = 1; 9322 lba = scsi_4btoul(cdb->addr); 9323 num_blocks = scsi_2btoul(cdb->length); 9324 break; 9325 } 9326 case VERIFY_12: { 9327 struct scsi_verify_12 *cdb; 9328 9329 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9330 if (cdb->byte2 & SVFY_BYTCHK) 9331 bytchk = 1; 9332 if (cdb->byte2 & SVFY_DPO) 9333 dpo = 1; 9334 lba = scsi_4btoul(cdb->addr); 9335 num_blocks = scsi_4btoul(cdb->length); 9336 break; 9337 } 9338 case VERIFY_16: { 9339 struct scsi_rw_16 *cdb; 9340 9341 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9342 if (cdb->byte2 & SVFY_BYTCHK) 9343 bytchk = 1; 9344 if (cdb->byte2 & SVFY_DPO) 9345 dpo = 1; 9346 lba = scsi_8btou64(cdb->addr); 9347 num_blocks = scsi_4btoul(cdb->length); 9348 break; 9349 } 9350 default: 9351 /* 9352 * We got a command we don't support. This shouldn't 9353 * happen, commands should be filtered out above us. 9354 */ 9355 ctl_set_invalid_opcode(ctsio); 9356 ctl_done((union ctl_io *)ctsio); 9357 return (CTL_RETVAL_COMPLETE); 9358 } 9359 9360 /* 9361 * The first check is to make sure we're in bounds, the second 9362 * check is to catch wrap-around problems. If the lba + num blocks 9363 * is less than the lba, then we've wrapped around and the block 9364 * range is invalid anyway. 9365 */ 9366 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9367 || ((lba + num_blocks) < lba)) { 9368 ctl_set_lba_out_of_range(ctsio); 9369 ctl_done((union ctl_io *)ctsio); 9370 return (CTL_RETVAL_COMPLETE); 9371 } 9372 9373 /* 9374 * According to SBC-3, a transfer length of 0 is not an error. 9375 */ 9376 if (num_blocks == 0) { 9377 ctl_set_success(ctsio); 9378 ctl_done((union ctl_io *)ctsio); 9379 return (CTL_RETVAL_COMPLETE); 9380 } 9381 9382 lbalen = (struct ctl_lba_len_flags *) 9383 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9384 lbalen->lba = lba; 9385 lbalen->len = num_blocks; 9386 if (bytchk) { 9387 lbalen->flags = CTL_LLF_COMPARE; 9388 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9389 } else { 9390 lbalen->flags = CTL_LLF_VERIFY; 9391 ctsio->kern_total_len = 0; 9392 } 9393 ctsio->kern_rel_offset = 0; 9394 9395 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9396 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9397 return (retval); 9398} 9399 9400int 9401ctl_report_luns(struct ctl_scsiio *ctsio) 9402{ 9403 struct scsi_report_luns *cdb; 9404 struct scsi_report_luns_data *lun_data; 9405 struct ctl_lun *lun, *request_lun; 9406 int num_luns, retval; 9407 uint32_t alloc_len, lun_datalen; 9408 int num_filled, well_known; 9409 uint32_t initidx, targ_lun_id, lun_id; 9410 9411 retval = CTL_RETVAL_COMPLETE; 9412 well_known = 0; 9413 9414 cdb = (struct scsi_report_luns *)ctsio->cdb; 9415 9416 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9417 9418 mtx_lock(&control_softc->ctl_lock); 9419 num_luns = control_softc->num_luns; 9420 mtx_unlock(&control_softc->ctl_lock); 9421 9422 switch (cdb->select_report) { 9423 case RPL_REPORT_DEFAULT: 9424 case RPL_REPORT_ALL: 9425 break; 9426 case RPL_REPORT_WELLKNOWN: 9427 well_known = 1; 9428 num_luns = 0; 9429 break; 9430 default: 9431 ctl_set_invalid_field(ctsio, 9432 /*sks_valid*/ 1, 9433 /*command*/ 1, 9434 /*field*/ 2, 9435 /*bit_valid*/ 0, 9436 /*bit*/ 0); 9437 ctl_done((union ctl_io *)ctsio); 9438 return (retval); 9439 break; /* NOTREACHED */ 9440 } 9441 9442 alloc_len = scsi_4btoul(cdb->length); 9443 /* 9444 * The initiator has to allocate at least 16 bytes for this request, 9445 * so he can at least get the header and the first LUN. Otherwise 9446 * we reject the request (per SPC-3 rev 14, section 6.21). 9447 */ 9448 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9449 sizeof(struct scsi_report_luns_lundata))) { 9450 ctl_set_invalid_field(ctsio, 9451 /*sks_valid*/ 1, 9452 /*command*/ 1, 9453 /*field*/ 6, 9454 /*bit_valid*/ 0, 9455 /*bit*/ 0); 9456 ctl_done((union ctl_io *)ctsio); 9457 return (retval); 9458 } 9459 9460 request_lun = (struct ctl_lun *) 9461 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9462 9463 lun_datalen = sizeof(*lun_data) + 9464 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9465 9466 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9467 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9468 ctsio->kern_sg_entries = 0; 9469 9470 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9471 9472 mtx_lock(&control_softc->ctl_lock); 9473 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9474 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9475 if (lun_id >= CTL_MAX_LUNS) 9476 continue; 9477 lun = control_softc->ctl_luns[lun_id]; 9478 if (lun == NULL) 9479 continue; 9480 9481 if (targ_lun_id <= 0xff) { 9482 /* 9483 * Peripheral addressing method, bus number 0. 9484 */ 9485 lun_data->luns[num_filled].lundata[0] = 9486 RPL_LUNDATA_ATYP_PERIPH; 9487 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9488 num_filled++; 9489 } else if (targ_lun_id <= 0x3fff) { 9490 /* 9491 * Flat addressing method. 9492 */ 9493 lun_data->luns[num_filled].lundata[0] = 9494 RPL_LUNDATA_ATYP_FLAT | 9495 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9496#ifdef OLDCTLHEADERS 9497 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9498 (targ_lun_id & SRLD_BUS_LUN_MASK); 9499#endif 9500 lun_data->luns[num_filled].lundata[1] = 9501#ifdef OLDCTLHEADERS 9502 targ_lun_id >> SRLD_BUS_LUN_BITS; 9503#endif 9504 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9505 num_filled++; 9506 } else { 9507 printf("ctl_report_luns: bogus LUN number %jd, " 9508 "skipping\n", (intmax_t)targ_lun_id); 9509 } 9510 /* 9511 * According to SPC-3, rev 14 section 6.21: 9512 * 9513 * "The execution of a REPORT LUNS command to any valid and 9514 * installed logical unit shall clear the REPORTED LUNS DATA 9515 * HAS CHANGED unit attention condition for all logical 9516 * units of that target with respect to the requesting 9517 * initiator. A valid and installed logical unit is one 9518 * having a PERIPHERAL QUALIFIER of 000b in the standard 9519 * INQUIRY data (see 6.4.2)." 9520 * 9521 * If request_lun is NULL, the LUN this report luns command 9522 * was issued to is either disabled or doesn't exist. In that 9523 * case, we shouldn't clear any pending lun change unit 9524 * attention. 9525 */ 9526 if (request_lun != NULL) { 9527 mtx_lock(&lun->lun_lock); 9528 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9529 mtx_unlock(&lun->lun_lock); 9530 } 9531 } 9532 mtx_unlock(&control_softc->ctl_lock); 9533 9534 /* 9535 * It's quite possible that we've returned fewer LUNs than we allocated 9536 * space for. Trim it. 9537 */ 9538 lun_datalen = sizeof(*lun_data) + 9539 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9540 9541 if (lun_datalen < alloc_len) { 9542 ctsio->residual = alloc_len - lun_datalen; 9543 ctsio->kern_data_len = lun_datalen; 9544 ctsio->kern_total_len = lun_datalen; 9545 } else { 9546 ctsio->residual = 0; 9547 ctsio->kern_data_len = alloc_len; 9548 ctsio->kern_total_len = alloc_len; 9549 } 9550 ctsio->kern_data_resid = 0; 9551 ctsio->kern_rel_offset = 0; 9552 ctsio->kern_sg_entries = 0; 9553 9554 /* 9555 * We set this to the actual data length, regardless of how much 9556 * space we actually have to return results. If the user looks at 9557 * this value, he'll know whether or not he allocated enough space 9558 * and reissue the command if necessary. We don't support well 9559 * known logical units, so if the user asks for that, return none. 9560 */ 9561 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9562 9563 /* 9564 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9565 * this request. 9566 */ 9567 ctsio->scsi_status = SCSI_STATUS_OK; 9568 9569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9570 ctsio->be_move_done = ctl_config_move_done; 9571 ctl_datamove((union ctl_io *)ctsio); 9572 9573 return (retval); 9574} 9575 9576int 9577ctl_request_sense(struct ctl_scsiio *ctsio) 9578{ 9579 struct scsi_request_sense *cdb; 9580 struct scsi_sense_data *sense_ptr; 9581 struct ctl_lun *lun; 9582 uint32_t initidx; 9583 int have_error; 9584 scsi_sense_data_type sense_format; 9585 9586 cdb = (struct scsi_request_sense *)ctsio->cdb; 9587 9588 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9589 9590 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9591 9592 /* 9593 * Determine which sense format the user wants. 9594 */ 9595 if (cdb->byte2 & SRS_DESC) 9596 sense_format = SSD_TYPE_DESC; 9597 else 9598 sense_format = SSD_TYPE_FIXED; 9599 9600 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9601 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9602 ctsio->kern_sg_entries = 0; 9603 9604 /* 9605 * struct scsi_sense_data, which is currently set to 256 bytes, is 9606 * larger than the largest allowed value for the length field in the 9607 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9608 */ 9609 ctsio->residual = 0; 9610 ctsio->kern_data_len = cdb->length; 9611 ctsio->kern_total_len = cdb->length; 9612 9613 ctsio->kern_data_resid = 0; 9614 ctsio->kern_rel_offset = 0; 9615 ctsio->kern_sg_entries = 0; 9616 9617 /* 9618 * If we don't have a LUN, we don't have any pending sense. 9619 */ 9620 if (lun == NULL) 9621 goto no_sense; 9622 9623 have_error = 0; 9624 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9625 /* 9626 * Check for pending sense, and then for pending unit attentions. 9627 * Pending sense gets returned first, then pending unit attentions. 9628 */ 9629 mtx_lock(&lun->lun_lock); 9630#ifdef CTL_WITH_CA 9631 if (ctl_is_set(lun->have_ca, initidx)) { 9632 scsi_sense_data_type stored_format; 9633 9634 /* 9635 * Check to see which sense format was used for the stored 9636 * sense data. 9637 */ 9638 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9639 9640 /* 9641 * If the user requested a different sense format than the 9642 * one we stored, then we need to convert it to the other 9643 * format. If we're going from descriptor to fixed format 9644 * sense data, we may lose things in translation, depending 9645 * on what options were used. 9646 * 9647 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9648 * for some reason we'll just copy it out as-is. 9649 */ 9650 if ((stored_format == SSD_TYPE_FIXED) 9651 && (sense_format == SSD_TYPE_DESC)) 9652 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9653 &lun->pending_sense[initidx], 9654 (struct scsi_sense_data_desc *)sense_ptr); 9655 else if ((stored_format == SSD_TYPE_DESC) 9656 && (sense_format == SSD_TYPE_FIXED)) 9657 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9658 &lun->pending_sense[initidx], 9659 (struct scsi_sense_data_fixed *)sense_ptr); 9660 else 9661 memcpy(sense_ptr, &lun->pending_sense[initidx], 9662 ctl_min(sizeof(*sense_ptr), 9663 sizeof(lun->pending_sense[initidx]))); 9664 9665 ctl_clear_mask(lun->have_ca, initidx); 9666 have_error = 1; 9667 } else 9668#endif 9669 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9670 ctl_ua_type ua_type; 9671 9672 ua_type = ctl_build_ua(lun->pending_ua[initidx], 9673 sense_ptr, sense_format); 9674 if (ua_type != CTL_UA_NONE) { 9675 have_error = 1; 9676 /* We're reporting this UA, so clear it */ 9677 lun->pending_ua[initidx] &= ~ua_type; 9678 } 9679 } 9680 mtx_unlock(&lun->lun_lock); 9681 9682 /* 9683 * We already have a pending error, return it. 9684 */ 9685 if (have_error != 0) { 9686 /* 9687 * We report the SCSI status as OK, since the status of the 9688 * request sense command itself is OK. 9689 */ 9690 ctsio->scsi_status = SCSI_STATUS_OK; 9691 9692 /* 9693 * We report 0 for the sense length, because we aren't doing 9694 * autosense in this case. We're reporting sense as 9695 * parameter data. 9696 */ 9697 ctsio->sense_len = 0; 9698 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9699 ctsio->be_move_done = ctl_config_move_done; 9700 ctl_datamove((union ctl_io *)ctsio); 9701 9702 return (CTL_RETVAL_COMPLETE); 9703 } 9704 9705no_sense: 9706 9707 /* 9708 * No sense information to report, so we report that everything is 9709 * okay. 9710 */ 9711 ctl_set_sense_data(sense_ptr, 9712 lun, 9713 sense_format, 9714 /*current_error*/ 1, 9715 /*sense_key*/ SSD_KEY_NO_SENSE, 9716 /*asc*/ 0x00, 9717 /*ascq*/ 0x00, 9718 SSD_ELEM_NONE); 9719 9720 ctsio->scsi_status = SCSI_STATUS_OK; 9721 9722 /* 9723 * We report 0 for the sense length, because we aren't doing 9724 * autosense in this case. We're reporting sense as parameter data. 9725 */ 9726 ctsio->sense_len = 0; 9727 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9728 ctsio->be_move_done = ctl_config_move_done; 9729 ctl_datamove((union ctl_io *)ctsio); 9730 9731 return (CTL_RETVAL_COMPLETE); 9732} 9733 9734int 9735ctl_tur(struct ctl_scsiio *ctsio) 9736{ 9737 struct ctl_lun *lun; 9738 9739 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9740 9741 CTL_DEBUG_PRINT(("ctl_tur\n")); 9742 9743 if (lun == NULL) 9744 return (EINVAL); 9745 9746 ctsio->scsi_status = SCSI_STATUS_OK; 9747 ctsio->io_hdr.status = CTL_SUCCESS; 9748 9749 ctl_done((union ctl_io *)ctsio); 9750 9751 return (CTL_RETVAL_COMPLETE); 9752} 9753 9754#ifdef notyet 9755static int 9756ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9757{ 9758 9759} 9760#endif 9761 9762static int 9763ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9764{ 9765 struct scsi_vpd_supported_pages *pages; 9766 int sup_page_size; 9767 struct ctl_lun *lun; 9768 9769 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9770 9771 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9772 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9773 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9774 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9775 ctsio->kern_sg_entries = 0; 9776 9777 if (sup_page_size < alloc_len) { 9778 ctsio->residual = alloc_len - sup_page_size; 9779 ctsio->kern_data_len = sup_page_size; 9780 ctsio->kern_total_len = sup_page_size; 9781 } else { 9782 ctsio->residual = 0; 9783 ctsio->kern_data_len = alloc_len; 9784 ctsio->kern_total_len = alloc_len; 9785 } 9786 ctsio->kern_data_resid = 0; 9787 ctsio->kern_rel_offset = 0; 9788 ctsio->kern_sg_entries = 0; 9789 9790 /* 9791 * The control device is always connected. The disk device, on the 9792 * other hand, may not be online all the time. Need to change this 9793 * to figure out whether the disk device is actually online or not. 9794 */ 9795 if (lun != NULL) 9796 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9797 lun->be_lun->lun_type; 9798 else 9799 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9800 9801 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9802 /* Supported VPD pages */ 9803 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9804 /* Serial Number */ 9805 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9806 /* Device Identification */ 9807 pages->page_list[2] = SVPD_DEVICE_ID; 9808 /* SCSI Ports */ 9809 pages->page_list[3] = SVPD_SCSI_PORTS; 9810 /* Third-party Copy */ 9811 pages->page_list[4] = SVPD_SCSI_TPC; 9812 /* Block limits */ 9813 pages->page_list[5] = SVPD_BLOCK_LIMITS; 9814 /* Logical Block Provisioning */ 9815 pages->page_list[6] = SVPD_LBP; 9816 9817 ctsio->scsi_status = SCSI_STATUS_OK; 9818 9819 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9820 ctsio->be_move_done = ctl_config_move_done; 9821 ctl_datamove((union ctl_io *)ctsio); 9822 9823 return (CTL_RETVAL_COMPLETE); 9824} 9825 9826static int 9827ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9828{ 9829 struct scsi_vpd_unit_serial_number *sn_ptr; 9830 struct ctl_lun *lun; 9831 9832 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9833 9834 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9835 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9836 ctsio->kern_sg_entries = 0; 9837 9838 if (sizeof(*sn_ptr) < alloc_len) { 9839 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9840 ctsio->kern_data_len = sizeof(*sn_ptr); 9841 ctsio->kern_total_len = sizeof(*sn_ptr); 9842 } else { 9843 ctsio->residual = 0; 9844 ctsio->kern_data_len = alloc_len; 9845 ctsio->kern_total_len = alloc_len; 9846 } 9847 ctsio->kern_data_resid = 0; 9848 ctsio->kern_rel_offset = 0; 9849 ctsio->kern_sg_entries = 0; 9850 9851 /* 9852 * The control device is always connected. The disk device, on the 9853 * other hand, may not be online all the time. Need to change this 9854 * to figure out whether the disk device is actually online or not. 9855 */ 9856 if (lun != NULL) 9857 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9858 lun->be_lun->lun_type; 9859 else 9860 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9861 9862 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9863 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9864 /* 9865 * If we don't have a LUN, we just leave the serial number as 9866 * all spaces. 9867 */ 9868 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9869 if (lun != NULL) { 9870 strncpy((char *)sn_ptr->serial_num, 9871 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9872 } 9873 ctsio->scsi_status = SCSI_STATUS_OK; 9874 9875 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9876 ctsio->be_move_done = ctl_config_move_done; 9877 ctl_datamove((union ctl_io *)ctsio); 9878 9879 return (CTL_RETVAL_COMPLETE); 9880} 9881 9882 9883static int 9884ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9885{ 9886 struct scsi_vpd_device_id *devid_ptr; 9887 struct scsi_vpd_id_descriptor *desc; 9888 struct ctl_softc *ctl_softc; 9889 struct ctl_lun *lun; 9890 struct ctl_port *port; 9891 int data_len; 9892 uint8_t proto; 9893 9894 ctl_softc = control_softc; 9895 9896 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9897 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9898 9899 data_len = sizeof(struct scsi_vpd_device_id) + 9900 sizeof(struct scsi_vpd_id_descriptor) + 9901 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9902 sizeof(struct scsi_vpd_id_descriptor) + 9903 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9904 if (lun && lun->lun_devid) 9905 data_len += lun->lun_devid->len; 9906 if (port->port_devid) 9907 data_len += port->port_devid->len; 9908 if (port->target_devid) 9909 data_len += port->target_devid->len; 9910 9911 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9912 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9913 ctsio->kern_sg_entries = 0; 9914 9915 if (data_len < alloc_len) { 9916 ctsio->residual = alloc_len - data_len; 9917 ctsio->kern_data_len = data_len; 9918 ctsio->kern_total_len = data_len; 9919 } else { 9920 ctsio->residual = 0; 9921 ctsio->kern_data_len = alloc_len; 9922 ctsio->kern_total_len = alloc_len; 9923 } 9924 ctsio->kern_data_resid = 0; 9925 ctsio->kern_rel_offset = 0; 9926 ctsio->kern_sg_entries = 0; 9927 9928 /* 9929 * The control device is always connected. The disk device, on the 9930 * other hand, may not be online all the time. 9931 */ 9932 if (lun != NULL) 9933 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9934 lun->be_lun->lun_type; 9935 else 9936 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9937 devid_ptr->page_code = SVPD_DEVICE_ID; 9938 scsi_ulto2b(data_len - 4, devid_ptr->length); 9939 9940 if (port->port_type == CTL_PORT_FC) 9941 proto = SCSI_PROTO_FC << 4; 9942 else if (port->port_type == CTL_PORT_ISCSI) 9943 proto = SCSI_PROTO_ISCSI << 4; 9944 else 9945 proto = SCSI_PROTO_SPI << 4; 9946 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9947 9948 /* 9949 * We're using a LUN association here. i.e., this device ID is a 9950 * per-LUN identifier. 9951 */ 9952 if (lun && lun->lun_devid) { 9953 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9954 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9955 lun->lun_devid->len); 9956 } 9957 9958 /* 9959 * This is for the WWPN which is a port association. 9960 */ 9961 if (port->port_devid) { 9962 memcpy(desc, port->port_devid->data, port->port_devid->len); 9963 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9964 port->port_devid->len); 9965 } 9966 9967 /* 9968 * This is for the Relative Target Port(type 4h) identifier 9969 */ 9970 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9971 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9972 SVPD_ID_TYPE_RELTARG; 9973 desc->length = 4; 9974 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9975 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9976 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9977 9978 /* 9979 * This is for the Target Port Group(type 5h) identifier 9980 */ 9981 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9982 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9983 SVPD_ID_TYPE_TPORTGRP; 9984 desc->length = 4; 9985 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9986 &desc->identifier[2]); 9987 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9988 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9989 9990 /* 9991 * This is for the Target identifier 9992 */ 9993 if (port->target_devid) { 9994 memcpy(desc, port->target_devid->data, port->target_devid->len); 9995 } 9996 9997 ctsio->scsi_status = SCSI_STATUS_OK; 9998 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9999 ctsio->be_move_done = ctl_config_move_done; 10000 ctl_datamove((union ctl_io *)ctsio); 10001 10002 return (CTL_RETVAL_COMPLETE); 10003} 10004 10005static int 10006ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10007{ 10008 struct ctl_softc *softc = control_softc; 10009 struct scsi_vpd_scsi_ports *sp; 10010 struct scsi_vpd_port_designation *pd; 10011 struct scsi_vpd_port_designation_cont *pdc; 10012 struct ctl_lun *lun; 10013 struct ctl_port *port; 10014 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10015 int num_target_port_groups, single; 10016 10017 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10018 10019 single = ctl_is_single; 10020 if (single) 10021 num_target_port_groups = 1; 10022 else 10023 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10024 num_target_ports = 0; 10025 iid_len = 0; 10026 id_len = 0; 10027 mtx_lock(&softc->ctl_lock); 10028 STAILQ_FOREACH(port, &softc->port_list, links) { 10029 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10030 continue; 10031 if (lun != NULL && 10032 ctl_map_lun_back(port->targ_port, lun->lun) >= 10033 CTL_MAX_LUNS) 10034 continue; 10035 num_target_ports++; 10036 if (port->init_devid) 10037 iid_len += port->init_devid->len; 10038 if (port->port_devid) 10039 id_len += port->port_devid->len; 10040 } 10041 mtx_unlock(&softc->ctl_lock); 10042 10043 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10044 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10045 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10046 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10047 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10048 ctsio->kern_sg_entries = 0; 10049 10050 if (data_len < alloc_len) { 10051 ctsio->residual = alloc_len - data_len; 10052 ctsio->kern_data_len = data_len; 10053 ctsio->kern_total_len = data_len; 10054 } else { 10055 ctsio->residual = 0; 10056 ctsio->kern_data_len = alloc_len; 10057 ctsio->kern_total_len = alloc_len; 10058 } 10059 ctsio->kern_data_resid = 0; 10060 ctsio->kern_rel_offset = 0; 10061 ctsio->kern_sg_entries = 0; 10062 10063 /* 10064 * The control device is always connected. The disk device, on the 10065 * other hand, may not be online all the time. Need to change this 10066 * to figure out whether the disk device is actually online or not. 10067 */ 10068 if (lun != NULL) 10069 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10070 lun->be_lun->lun_type; 10071 else 10072 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10073 10074 sp->page_code = SVPD_SCSI_PORTS; 10075 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10076 sp->page_length); 10077 pd = &sp->design[0]; 10078 10079 mtx_lock(&softc->ctl_lock); 10080 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10081 pg = 0; 10082 else 10083 pg = 1; 10084 for (g = 0; g < num_target_port_groups; g++) { 10085 STAILQ_FOREACH(port, &softc->port_list, links) { 10086 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10087 continue; 10088 if (lun != NULL && 10089 ctl_map_lun_back(port->targ_port, lun->lun) >= 10090 CTL_MAX_LUNS) 10091 continue; 10092 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10093 scsi_ulto2b(p, pd->relative_port_id); 10094 if (port->init_devid && g == pg) { 10095 iid_len = port->init_devid->len; 10096 memcpy(pd->initiator_transportid, 10097 port->init_devid->data, port->init_devid->len); 10098 } else 10099 iid_len = 0; 10100 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10101 pdc = (struct scsi_vpd_port_designation_cont *) 10102 (&pd->initiator_transportid[iid_len]); 10103 if (port->port_devid && g == pg) { 10104 id_len = port->port_devid->len; 10105 memcpy(pdc->target_port_descriptors, 10106 port->port_devid->data, port->port_devid->len); 10107 } else 10108 id_len = 0; 10109 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10110 pd = (struct scsi_vpd_port_designation *) 10111 ((uint8_t *)pdc->target_port_descriptors + id_len); 10112 } 10113 } 10114 mtx_unlock(&softc->ctl_lock); 10115 10116 ctsio->scsi_status = SCSI_STATUS_OK; 10117 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10118 ctsio->be_move_done = ctl_config_move_done; 10119 ctl_datamove((union ctl_io *)ctsio); 10120 10121 return (CTL_RETVAL_COMPLETE); 10122} 10123 10124static int 10125ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10126{ 10127 struct scsi_vpd_block_limits *bl_ptr; 10128 struct ctl_lun *lun; 10129 int bs; 10130 10131 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10132 10133 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10134 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10135 ctsio->kern_sg_entries = 0; 10136 10137 if (sizeof(*bl_ptr) < alloc_len) { 10138 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10139 ctsio->kern_data_len = sizeof(*bl_ptr); 10140 ctsio->kern_total_len = sizeof(*bl_ptr); 10141 } else { 10142 ctsio->residual = 0; 10143 ctsio->kern_data_len = alloc_len; 10144 ctsio->kern_total_len = alloc_len; 10145 } 10146 ctsio->kern_data_resid = 0; 10147 ctsio->kern_rel_offset = 0; 10148 ctsio->kern_sg_entries = 0; 10149 10150 /* 10151 * The control device is always connected. The disk device, on the 10152 * other hand, may not be online all the time. Need to change this 10153 * to figure out whether the disk device is actually online or not. 10154 */ 10155 if (lun != NULL) 10156 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10157 lun->be_lun->lun_type; 10158 else 10159 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10160 10161 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10162 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10163 bl_ptr->max_cmp_write_len = 0xff; 10164 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10165 if (lun != NULL) { 10166 bs = lun->be_lun->blocksize; 10167 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10168 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10169 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10170 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10171 } 10172 } 10173 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10174 10175 ctsio->scsi_status = SCSI_STATUS_OK; 10176 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10177 ctsio->be_move_done = ctl_config_move_done; 10178 ctl_datamove((union ctl_io *)ctsio); 10179 10180 return (CTL_RETVAL_COMPLETE); 10181} 10182 10183static int 10184ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10185{ 10186 struct scsi_vpd_logical_block_prov *lbp_ptr; 10187 struct ctl_lun *lun; 10188 10189 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10190 10191 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10192 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10193 ctsio->kern_sg_entries = 0; 10194 10195 if (sizeof(*lbp_ptr) < alloc_len) { 10196 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10197 ctsio->kern_data_len = sizeof(*lbp_ptr); 10198 ctsio->kern_total_len = sizeof(*lbp_ptr); 10199 } else { 10200 ctsio->residual = 0; 10201 ctsio->kern_data_len = alloc_len; 10202 ctsio->kern_total_len = alloc_len; 10203 } 10204 ctsio->kern_data_resid = 0; 10205 ctsio->kern_rel_offset = 0; 10206 ctsio->kern_sg_entries = 0; 10207 10208 /* 10209 * The control device is always connected. The disk device, on the 10210 * other hand, may not be online all the time. Need to change this 10211 * to figure out whether the disk device is actually online or not. 10212 */ 10213 if (lun != NULL) 10214 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10215 lun->be_lun->lun_type; 10216 else 10217 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10218 10219 lbp_ptr->page_code = SVPD_LBP; 10220 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 10221 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10; 10222 10223 ctsio->scsi_status = SCSI_STATUS_OK; 10224 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10225 ctsio->be_move_done = ctl_config_move_done; 10226 ctl_datamove((union ctl_io *)ctsio); 10227 10228 return (CTL_RETVAL_COMPLETE); 10229} 10230 10231static int 10232ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10233{ 10234 struct scsi_inquiry *cdb; 10235 struct ctl_lun *lun; 10236 int alloc_len, retval; 10237 10238 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10239 cdb = (struct scsi_inquiry *)ctsio->cdb; 10240 10241 retval = CTL_RETVAL_COMPLETE; 10242 10243 alloc_len = scsi_2btoul(cdb->length); 10244 10245 switch (cdb->page_code) { 10246 case SVPD_SUPPORTED_PAGES: 10247 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10248 break; 10249 case SVPD_UNIT_SERIAL_NUMBER: 10250 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10251 break; 10252 case SVPD_DEVICE_ID: 10253 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10254 break; 10255 case SVPD_SCSI_PORTS: 10256 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10257 break; 10258 case SVPD_SCSI_TPC: 10259 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10260 break; 10261 case SVPD_BLOCK_LIMITS: 10262 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10263 break; 10264 case SVPD_LBP: 10265 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10266 break; 10267 default: 10268 ctl_set_invalid_field(ctsio, 10269 /*sks_valid*/ 1, 10270 /*command*/ 1, 10271 /*field*/ 2, 10272 /*bit_valid*/ 0, 10273 /*bit*/ 0); 10274 ctl_done((union ctl_io *)ctsio); 10275 retval = CTL_RETVAL_COMPLETE; 10276 break; 10277 } 10278 10279 return (retval); 10280} 10281 10282static int 10283ctl_inquiry_std(struct ctl_scsiio *ctsio) 10284{ 10285 struct scsi_inquiry_data *inq_ptr; 10286 struct scsi_inquiry *cdb; 10287 struct ctl_softc *ctl_softc; 10288 struct ctl_lun *lun; 10289 char *val; 10290 uint32_t alloc_len; 10291 ctl_port_type port_type; 10292 10293 ctl_softc = control_softc; 10294 10295 /* 10296 * Figure out whether we're talking to a Fibre Channel port or not. 10297 * We treat the ioctl front end, and any SCSI adapters, as packetized 10298 * SCSI front ends. 10299 */ 10300 port_type = ctl_softc->ctl_ports[ 10301 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10302 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10303 port_type = CTL_PORT_SCSI; 10304 10305 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10306 cdb = (struct scsi_inquiry *)ctsio->cdb; 10307 alloc_len = scsi_2btoul(cdb->length); 10308 10309 /* 10310 * We malloc the full inquiry data size here and fill it 10311 * in. If the user only asks for less, we'll give him 10312 * that much. 10313 */ 10314 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10315 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10316 ctsio->kern_sg_entries = 0; 10317 ctsio->kern_data_resid = 0; 10318 ctsio->kern_rel_offset = 0; 10319 10320 if (sizeof(*inq_ptr) < alloc_len) { 10321 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10322 ctsio->kern_data_len = sizeof(*inq_ptr); 10323 ctsio->kern_total_len = sizeof(*inq_ptr); 10324 } else { 10325 ctsio->residual = 0; 10326 ctsio->kern_data_len = alloc_len; 10327 ctsio->kern_total_len = alloc_len; 10328 } 10329 10330 /* 10331 * If we have a LUN configured, report it as connected. Otherwise, 10332 * report that it is offline or no device is supported, depending 10333 * on the value of inquiry_pq_no_lun. 10334 * 10335 * According to the spec (SPC-4 r34), the peripheral qualifier 10336 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10337 * 10338 * "A peripheral device having the specified peripheral device type 10339 * is not connected to this logical unit. However, the device 10340 * server is capable of supporting the specified peripheral device 10341 * type on this logical unit." 10342 * 10343 * According to the same spec, the peripheral qualifier 10344 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10345 * 10346 * "The device server is not capable of supporting a peripheral 10347 * device on this logical unit. For this peripheral qualifier the 10348 * peripheral device type shall be set to 1Fh. All other peripheral 10349 * device type values are reserved for this peripheral qualifier." 10350 * 10351 * Given the text, it would seem that we probably want to report that 10352 * the LUN is offline here. There is no LUN connected, but we can 10353 * support a LUN at the given LUN number. 10354 * 10355 * In the real world, though, it sounds like things are a little 10356 * different: 10357 * 10358 * - Linux, when presented with a LUN with the offline peripheral 10359 * qualifier, will create an sg driver instance for it. So when 10360 * you attach it to CTL, you wind up with a ton of sg driver 10361 * instances. (One for every LUN that Linux bothered to probe.) 10362 * Linux does this despite the fact that it issues a REPORT LUNs 10363 * to LUN 0 to get the inventory of supported LUNs. 10364 * 10365 * - There is other anecdotal evidence (from Emulex folks) about 10366 * arrays that use the offline peripheral qualifier for LUNs that 10367 * are on the "passive" path in an active/passive array. 10368 * 10369 * So the solution is provide a hopefully reasonable default 10370 * (return bad/no LUN) and allow the user to change the behavior 10371 * with a tunable/sysctl variable. 10372 */ 10373 if (lun != NULL) 10374 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10375 lun->be_lun->lun_type; 10376 else if (ctl_softc->inquiry_pq_no_lun == 0) 10377 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10378 else 10379 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10380 10381 /* RMB in byte 2 is 0 */ 10382 inq_ptr->version = SCSI_REV_SPC4; 10383 10384 /* 10385 * According to SAM-3, even if a device only supports a single 10386 * level of LUN addressing, it should still set the HISUP bit: 10387 * 10388 * 4.9.1 Logical unit numbers overview 10389 * 10390 * All logical unit number formats described in this standard are 10391 * hierarchical in structure even when only a single level in that 10392 * hierarchy is used. The HISUP bit shall be set to one in the 10393 * standard INQUIRY data (see SPC-2) when any logical unit number 10394 * format described in this standard is used. Non-hierarchical 10395 * formats are outside the scope of this standard. 10396 * 10397 * Therefore we set the HiSup bit here. 10398 * 10399 * The reponse format is 2, per SPC-3. 10400 */ 10401 inq_ptr->response_format = SID_HiSup | 2; 10402 10403 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 10404 CTL_DEBUG_PRINT(("additional_length = %d\n", 10405 inq_ptr->additional_length)); 10406 10407 inq_ptr->spc3_flags = SPC3_SID_3PC; 10408 if (!ctl_is_single) 10409 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT; 10410 /* 16 bit addressing */ 10411 if (port_type == CTL_PORT_SCSI) 10412 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10413 /* XXX set the SID_MultiP bit here if we're actually going to 10414 respond on multiple ports */ 10415 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10416 10417 /* 16 bit data bus, synchronous transfers */ 10418 if (port_type == CTL_PORT_SCSI) 10419 inq_ptr->flags = SID_WBus16 | SID_Sync; 10420 /* 10421 * XXX KDM do we want to support tagged queueing on the control 10422 * device at all? 10423 */ 10424 if ((lun == NULL) 10425 || (lun->be_lun->lun_type != T_PROCESSOR)) 10426 inq_ptr->flags |= SID_CmdQue; 10427 /* 10428 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10429 * We have 8 bytes for the vendor name, and 16 bytes for the device 10430 * name and 4 bytes for the revision. 10431 */ 10432 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10433 "vendor")) == NULL) { 10434 strcpy(inq_ptr->vendor, CTL_VENDOR); 10435 } else { 10436 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10437 strncpy(inq_ptr->vendor, val, 10438 min(sizeof(inq_ptr->vendor), strlen(val))); 10439 } 10440 if (lun == NULL) { 10441 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10442 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10443 switch (lun->be_lun->lun_type) { 10444 case T_DIRECT: 10445 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10446 break; 10447 case T_PROCESSOR: 10448 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10449 break; 10450 default: 10451 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10452 break; 10453 } 10454 } else { 10455 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10456 strncpy(inq_ptr->product, val, 10457 min(sizeof(inq_ptr->product), strlen(val))); 10458 } 10459 10460 /* 10461 * XXX make this a macro somewhere so it automatically gets 10462 * incremented when we make changes. 10463 */ 10464 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10465 "revision")) == NULL) { 10466 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10467 } else { 10468 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10469 strncpy(inq_ptr->revision, val, 10470 min(sizeof(inq_ptr->revision), strlen(val))); 10471 } 10472 10473 /* 10474 * For parallel SCSI, we support double transition and single 10475 * transition clocking. We also support QAS (Quick Arbitration 10476 * and Selection) and Information Unit transfers on both the 10477 * control and array devices. 10478 */ 10479 if (port_type == CTL_PORT_SCSI) 10480 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10481 SID_SPI_IUS; 10482 10483 /* SAM-5 (no version claimed) */ 10484 scsi_ulto2b(0x00A0, inq_ptr->version1); 10485 /* SPC-4 (no version claimed) */ 10486 scsi_ulto2b(0x0460, inq_ptr->version2); 10487 if (port_type == CTL_PORT_FC) { 10488 /* FCP-2 ANSI INCITS.350:2003 */ 10489 scsi_ulto2b(0x0917, inq_ptr->version3); 10490 } else if (port_type == CTL_PORT_SCSI) { 10491 /* SPI-4 ANSI INCITS.362:200x */ 10492 scsi_ulto2b(0x0B56, inq_ptr->version3); 10493 } else if (port_type == CTL_PORT_ISCSI) { 10494 /* iSCSI (no version claimed) */ 10495 scsi_ulto2b(0x0960, inq_ptr->version3); 10496 } else if (port_type == CTL_PORT_SAS) { 10497 /* SAS (no version claimed) */ 10498 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10499 } 10500 10501 if (lun == NULL) { 10502 /* SBC-3 (no version claimed) */ 10503 scsi_ulto2b(0x04C0, inq_ptr->version4); 10504 } else { 10505 switch (lun->be_lun->lun_type) { 10506 case T_DIRECT: 10507 /* SBC-3 (no version claimed) */ 10508 scsi_ulto2b(0x04C0, inq_ptr->version4); 10509 break; 10510 case T_PROCESSOR: 10511 default: 10512 break; 10513 } 10514 } 10515 10516 ctsio->scsi_status = SCSI_STATUS_OK; 10517 if (ctsio->kern_data_len > 0) { 10518 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10519 ctsio->be_move_done = ctl_config_move_done; 10520 ctl_datamove((union ctl_io *)ctsio); 10521 } else { 10522 ctsio->io_hdr.status = CTL_SUCCESS; 10523 ctl_done((union ctl_io *)ctsio); 10524 } 10525 10526 return (CTL_RETVAL_COMPLETE); 10527} 10528 10529int 10530ctl_inquiry(struct ctl_scsiio *ctsio) 10531{ 10532 struct scsi_inquiry *cdb; 10533 int retval; 10534 10535 cdb = (struct scsi_inquiry *)ctsio->cdb; 10536 10537 retval = 0; 10538 10539 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10540 10541 /* 10542 * Right now, we don't support the CmdDt inquiry information. 10543 * This would be nice to support in the future. When we do 10544 * support it, we should change this test so that it checks to make 10545 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10546 */ 10547#ifdef notyet 10548 if (((cdb->byte2 & SI_EVPD) 10549 && (cdb->byte2 & SI_CMDDT))) 10550#endif 10551 if (cdb->byte2 & SI_CMDDT) { 10552 /* 10553 * Point to the SI_CMDDT bit. We might change this 10554 * when we support SI_CMDDT, but since both bits would be 10555 * "wrong", this should probably just stay as-is then. 10556 */ 10557 ctl_set_invalid_field(ctsio, 10558 /*sks_valid*/ 1, 10559 /*command*/ 1, 10560 /*field*/ 1, 10561 /*bit_valid*/ 1, 10562 /*bit*/ 1); 10563 ctl_done((union ctl_io *)ctsio); 10564 return (CTL_RETVAL_COMPLETE); 10565 } 10566 if (cdb->byte2 & SI_EVPD) 10567 retval = ctl_inquiry_evpd(ctsio); 10568#ifdef notyet 10569 else if (cdb->byte2 & SI_CMDDT) 10570 retval = ctl_inquiry_cmddt(ctsio); 10571#endif 10572 else 10573 retval = ctl_inquiry_std(ctsio); 10574 10575 return (retval); 10576} 10577 10578/* 10579 * For known CDB types, parse the LBA and length. 10580 */ 10581static int 10582ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10583{ 10584 if (io->io_hdr.io_type != CTL_IO_SCSI) 10585 return (1); 10586 10587 switch (io->scsiio.cdb[0]) { 10588 case COMPARE_AND_WRITE: { 10589 struct scsi_compare_and_write *cdb; 10590 10591 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10592 10593 *lba = scsi_8btou64(cdb->addr); 10594 *len = cdb->length; 10595 break; 10596 } 10597 case READ_6: 10598 case WRITE_6: { 10599 struct scsi_rw_6 *cdb; 10600 10601 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10602 10603 *lba = scsi_3btoul(cdb->addr); 10604 /* only 5 bits are valid in the most significant address byte */ 10605 *lba &= 0x1fffff; 10606 *len = cdb->length; 10607 break; 10608 } 10609 case READ_10: 10610 case WRITE_10: { 10611 struct scsi_rw_10 *cdb; 10612 10613 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10614 10615 *lba = scsi_4btoul(cdb->addr); 10616 *len = scsi_2btoul(cdb->length); 10617 break; 10618 } 10619 case WRITE_VERIFY_10: { 10620 struct scsi_write_verify_10 *cdb; 10621 10622 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10623 10624 *lba = scsi_4btoul(cdb->addr); 10625 *len = scsi_2btoul(cdb->length); 10626 break; 10627 } 10628 case READ_12: 10629 case WRITE_12: { 10630 struct scsi_rw_12 *cdb; 10631 10632 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10633 10634 *lba = scsi_4btoul(cdb->addr); 10635 *len = scsi_4btoul(cdb->length); 10636 break; 10637 } 10638 case WRITE_VERIFY_12: { 10639 struct scsi_write_verify_12 *cdb; 10640 10641 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10642 10643 *lba = scsi_4btoul(cdb->addr); 10644 *len = scsi_4btoul(cdb->length); 10645 break; 10646 } 10647 case READ_16: 10648 case WRITE_16: { 10649 struct scsi_rw_16 *cdb; 10650 10651 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10652 10653 *lba = scsi_8btou64(cdb->addr); 10654 *len = scsi_4btoul(cdb->length); 10655 break; 10656 } 10657 case WRITE_VERIFY_16: { 10658 struct scsi_write_verify_16 *cdb; 10659 10660 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10661 10662 10663 *lba = scsi_8btou64(cdb->addr); 10664 *len = scsi_4btoul(cdb->length); 10665 break; 10666 } 10667 case WRITE_SAME_10: { 10668 struct scsi_write_same_10 *cdb; 10669 10670 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10671 10672 *lba = scsi_4btoul(cdb->addr); 10673 *len = scsi_2btoul(cdb->length); 10674 break; 10675 } 10676 case WRITE_SAME_16: { 10677 struct scsi_write_same_16 *cdb; 10678 10679 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10680 10681 *lba = scsi_8btou64(cdb->addr); 10682 *len = scsi_4btoul(cdb->length); 10683 break; 10684 } 10685 case VERIFY_10: { 10686 struct scsi_verify_10 *cdb; 10687 10688 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10689 10690 *lba = scsi_4btoul(cdb->addr); 10691 *len = scsi_2btoul(cdb->length); 10692 break; 10693 } 10694 case VERIFY_12: { 10695 struct scsi_verify_12 *cdb; 10696 10697 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10698 10699 *lba = scsi_4btoul(cdb->addr); 10700 *len = scsi_4btoul(cdb->length); 10701 break; 10702 } 10703 case VERIFY_16: { 10704 struct scsi_verify_16 *cdb; 10705 10706 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10707 10708 *lba = scsi_8btou64(cdb->addr); 10709 *len = scsi_4btoul(cdb->length); 10710 break; 10711 } 10712 default: 10713 return (1); 10714 break; /* NOTREACHED */ 10715 } 10716 10717 return (0); 10718} 10719 10720static ctl_action 10721ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10722{ 10723 uint64_t endlba1, endlba2; 10724 10725 endlba1 = lba1 + len1 - 1; 10726 endlba2 = lba2 + len2 - 1; 10727 10728 if ((endlba1 < lba2) 10729 || (endlba2 < lba1)) 10730 return (CTL_ACTION_PASS); 10731 else 10732 return (CTL_ACTION_BLOCK); 10733} 10734 10735static ctl_action 10736ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10737{ 10738 uint64_t lba1, lba2; 10739 uint32_t len1, len2; 10740 int retval; 10741 10742 retval = ctl_get_lba_len(io1, &lba1, &len1); 10743 if (retval != 0) 10744 return (CTL_ACTION_ERROR); 10745 10746 retval = ctl_get_lba_len(io2, &lba2, &len2); 10747 if (retval != 0) 10748 return (CTL_ACTION_ERROR); 10749 10750 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10751} 10752 10753static ctl_action 10754ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10755{ 10756 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10757 ctl_serialize_action *serialize_row; 10758 10759 /* 10760 * The initiator attempted multiple untagged commands at the same 10761 * time. Can't do that. 10762 */ 10763 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10764 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10765 && ((pending_io->io_hdr.nexus.targ_port == 10766 ooa_io->io_hdr.nexus.targ_port) 10767 && (pending_io->io_hdr.nexus.initid.id == 10768 ooa_io->io_hdr.nexus.initid.id)) 10769 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10770 return (CTL_ACTION_OVERLAP); 10771 10772 /* 10773 * The initiator attempted to send multiple tagged commands with 10774 * the same ID. (It's fine if different initiators have the same 10775 * tag ID.) 10776 * 10777 * Even if all of those conditions are true, we don't kill the I/O 10778 * if the command ahead of us has been aborted. We won't end up 10779 * sending it to the FETD, and it's perfectly legal to resend a 10780 * command with the same tag number as long as the previous 10781 * instance of this tag number has been aborted somehow. 10782 */ 10783 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10784 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10785 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10786 && ((pending_io->io_hdr.nexus.targ_port == 10787 ooa_io->io_hdr.nexus.targ_port) 10788 && (pending_io->io_hdr.nexus.initid.id == 10789 ooa_io->io_hdr.nexus.initid.id)) 10790 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10791 return (CTL_ACTION_OVERLAP_TAG); 10792 10793 /* 10794 * If we get a head of queue tag, SAM-3 says that we should 10795 * immediately execute it. 10796 * 10797 * What happens if this command would normally block for some other 10798 * reason? e.g. a request sense with a head of queue tag 10799 * immediately after a write. Normally that would block, but this 10800 * will result in its getting executed immediately... 10801 * 10802 * We currently return "pass" instead of "skip", so we'll end up 10803 * going through the rest of the queue to check for overlapped tags. 10804 * 10805 * XXX KDM check for other types of blockage first?? 10806 */ 10807 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10808 return (CTL_ACTION_PASS); 10809 10810 /* 10811 * Ordered tags have to block until all items ahead of them 10812 * have completed. If we get called with an ordered tag, we always 10813 * block, if something else is ahead of us in the queue. 10814 */ 10815 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10816 return (CTL_ACTION_BLOCK); 10817 10818 /* 10819 * Simple tags get blocked until all head of queue and ordered tags 10820 * ahead of them have completed. I'm lumping untagged commands in 10821 * with simple tags here. XXX KDM is that the right thing to do? 10822 */ 10823 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10824 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10825 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10826 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10827 return (CTL_ACTION_BLOCK); 10828 10829 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10830 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10831 10832 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10833 10834 switch (serialize_row[pending_entry->seridx]) { 10835 case CTL_SER_BLOCK: 10836 return (CTL_ACTION_BLOCK); 10837 break; /* NOTREACHED */ 10838 case CTL_SER_EXTENT: 10839 return (ctl_extent_check(pending_io, ooa_io)); 10840 break; /* NOTREACHED */ 10841 case CTL_SER_PASS: 10842 return (CTL_ACTION_PASS); 10843 break; /* NOTREACHED */ 10844 case CTL_SER_SKIP: 10845 return (CTL_ACTION_SKIP); 10846 break; 10847 default: 10848 panic("invalid serialization value %d", 10849 serialize_row[pending_entry->seridx]); 10850 break; /* NOTREACHED */ 10851 } 10852 10853 return (CTL_ACTION_ERROR); 10854} 10855 10856/* 10857 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10858 * Assumptions: 10859 * - pending_io is generally either incoming, or on the blocked queue 10860 * - starting I/O is the I/O we want to start the check with. 10861 */ 10862static ctl_action 10863ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10864 union ctl_io *starting_io) 10865{ 10866 union ctl_io *ooa_io; 10867 ctl_action action; 10868 10869 mtx_assert(&lun->lun_lock, MA_OWNED); 10870 10871 /* 10872 * Run back along the OOA queue, starting with the current 10873 * blocked I/O and going through every I/O before it on the 10874 * queue. If starting_io is NULL, we'll just end up returning 10875 * CTL_ACTION_PASS. 10876 */ 10877 for (ooa_io = starting_io; ooa_io != NULL; 10878 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10879 ooa_links)){ 10880 10881 /* 10882 * This routine just checks to see whether 10883 * cur_blocked is blocked by ooa_io, which is ahead 10884 * of it in the queue. It doesn't queue/dequeue 10885 * cur_blocked. 10886 */ 10887 action = ctl_check_for_blockage(pending_io, ooa_io); 10888 switch (action) { 10889 case CTL_ACTION_BLOCK: 10890 case CTL_ACTION_OVERLAP: 10891 case CTL_ACTION_OVERLAP_TAG: 10892 case CTL_ACTION_SKIP: 10893 case CTL_ACTION_ERROR: 10894 return (action); 10895 break; /* NOTREACHED */ 10896 case CTL_ACTION_PASS: 10897 break; 10898 default: 10899 panic("invalid action %d", action); 10900 break; /* NOTREACHED */ 10901 } 10902 } 10903 10904 return (CTL_ACTION_PASS); 10905} 10906 10907/* 10908 * Assumptions: 10909 * - An I/O has just completed, and has been removed from the per-LUN OOA 10910 * queue, so some items on the blocked queue may now be unblocked. 10911 */ 10912static int 10913ctl_check_blocked(struct ctl_lun *lun) 10914{ 10915 union ctl_io *cur_blocked, *next_blocked; 10916 10917 mtx_assert(&lun->lun_lock, MA_OWNED); 10918 10919 /* 10920 * Run forward from the head of the blocked queue, checking each 10921 * entry against the I/Os prior to it on the OOA queue to see if 10922 * there is still any blockage. 10923 * 10924 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10925 * with our removing a variable on it while it is traversing the 10926 * list. 10927 */ 10928 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10929 cur_blocked != NULL; cur_blocked = next_blocked) { 10930 union ctl_io *prev_ooa; 10931 ctl_action action; 10932 10933 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10934 blocked_links); 10935 10936 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10937 ctl_ooaq, ooa_links); 10938 10939 /* 10940 * If cur_blocked happens to be the first item in the OOA 10941 * queue now, prev_ooa will be NULL, and the action 10942 * returned will just be CTL_ACTION_PASS. 10943 */ 10944 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10945 10946 switch (action) { 10947 case CTL_ACTION_BLOCK: 10948 /* Nothing to do here, still blocked */ 10949 break; 10950 case CTL_ACTION_OVERLAP: 10951 case CTL_ACTION_OVERLAP_TAG: 10952 /* 10953 * This shouldn't happen! In theory we've already 10954 * checked this command for overlap... 10955 */ 10956 break; 10957 case CTL_ACTION_PASS: 10958 case CTL_ACTION_SKIP: { 10959 struct ctl_softc *softc; 10960 const struct ctl_cmd_entry *entry; 10961 uint32_t initidx; 10962 int isc_retval; 10963 10964 /* 10965 * The skip case shouldn't happen, this transaction 10966 * should have never made it onto the blocked queue. 10967 */ 10968 /* 10969 * This I/O is no longer blocked, we can remove it 10970 * from the blocked queue. Since this is a TAILQ 10971 * (doubly linked list), we can do O(1) removals 10972 * from any place on the list. 10973 */ 10974 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10975 blocked_links); 10976 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10977 10978 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10979 /* 10980 * Need to send IO back to original side to 10981 * run 10982 */ 10983 union ctl_ha_msg msg_info; 10984 10985 msg_info.hdr.original_sc = 10986 cur_blocked->io_hdr.original_sc; 10987 msg_info.hdr.serializing_sc = cur_blocked; 10988 msg_info.hdr.msg_type = CTL_MSG_R2R; 10989 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10990 &msg_info, sizeof(msg_info), 0)) > 10991 CTL_HA_STATUS_SUCCESS) { 10992 printf("CTL:Check Blocked error from " 10993 "ctl_ha_msg_send %d\n", 10994 isc_retval); 10995 } 10996 break; 10997 } 10998 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 10999 softc = control_softc; 11000 11001 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11002 11003 /* 11004 * Check this I/O for LUN state changes that may 11005 * have happened while this command was blocked. 11006 * The LUN state may have been changed by a command 11007 * ahead of us in the queue, so we need to re-check 11008 * for any states that can be caused by SCSI 11009 * commands. 11010 */ 11011 if (ctl_scsiio_lun_check(softc, lun, entry, 11012 &cur_blocked->scsiio) == 0) { 11013 cur_blocked->io_hdr.flags |= 11014 CTL_FLAG_IS_WAS_ON_RTR; 11015 ctl_enqueue_rtr(cur_blocked); 11016 } else 11017 ctl_done(cur_blocked); 11018 break; 11019 } 11020 default: 11021 /* 11022 * This probably shouldn't happen -- we shouldn't 11023 * get CTL_ACTION_ERROR, or anything else. 11024 */ 11025 break; 11026 } 11027 } 11028 11029 return (CTL_RETVAL_COMPLETE); 11030} 11031 11032/* 11033 * This routine (with one exception) checks LUN flags that can be set by 11034 * commands ahead of us in the OOA queue. These flags have to be checked 11035 * when a command initially comes in, and when we pull a command off the 11036 * blocked queue and are preparing to execute it. The reason we have to 11037 * check these flags for commands on the blocked queue is that the LUN 11038 * state may have been changed by a command ahead of us while we're on the 11039 * blocked queue. 11040 * 11041 * Ordering is somewhat important with these checks, so please pay 11042 * careful attention to the placement of any new checks. 11043 */ 11044static int 11045ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11046 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11047{ 11048 int retval; 11049 11050 retval = 0; 11051 11052 mtx_assert(&lun->lun_lock, MA_OWNED); 11053 11054 /* 11055 * If this shelf is a secondary shelf controller, we have to reject 11056 * any media access commands. 11057 */ 11058#if 0 11059 /* No longer needed for HA */ 11060 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11061 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11062 ctl_set_lun_standby(ctsio); 11063 retval = 1; 11064 goto bailout; 11065 } 11066#endif 11067 11068 /* 11069 * Check for a reservation conflict. If this command isn't allowed 11070 * even on reserved LUNs, and if this initiator isn't the one who 11071 * reserved us, reject the command with a reservation conflict. 11072 */ 11073 if ((lun->flags & CTL_LUN_RESERVED) 11074 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11075 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11076 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11077 || (ctsio->io_hdr.nexus.targ_target.id != 11078 lun->rsv_nexus.targ_target.id)) { 11079 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11080 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11081 retval = 1; 11082 goto bailout; 11083 } 11084 } 11085 11086 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11087 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11088 uint32_t residx; 11089 11090 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11091 /* 11092 * if we aren't registered or it's a res holder type 11093 * reservation and this isn't the res holder then set a 11094 * conflict. 11095 * NOTE: Commands which might be allowed on write exclusive 11096 * type reservations are checked in the particular command 11097 * for a conflict. Read and SSU are the only ones. 11098 */ 11099 if (!lun->per_res[residx].registered 11100 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11101 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11102 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11103 retval = 1; 11104 goto bailout; 11105 } 11106 11107 } 11108 11109 if ((lun->flags & CTL_LUN_OFFLINE) 11110 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11111 ctl_set_lun_not_ready(ctsio); 11112 retval = 1; 11113 goto bailout; 11114 } 11115 11116 /* 11117 * If the LUN is stopped, see if this particular command is allowed 11118 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11119 */ 11120 if ((lun->flags & CTL_LUN_STOPPED) 11121 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11122 /* "Logical unit not ready, initializing cmd. required" */ 11123 ctl_set_lun_stopped(ctsio); 11124 retval = 1; 11125 goto bailout; 11126 } 11127 11128 if ((lun->flags & CTL_LUN_INOPERABLE) 11129 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11130 /* "Medium format corrupted" */ 11131 ctl_set_medium_format_corrupted(ctsio); 11132 retval = 1; 11133 goto bailout; 11134 } 11135 11136bailout: 11137 return (retval); 11138 11139} 11140 11141static void 11142ctl_failover_io(union ctl_io *io, int have_lock) 11143{ 11144 ctl_set_busy(&io->scsiio); 11145 ctl_done(io); 11146} 11147 11148static void 11149ctl_failover(void) 11150{ 11151 struct ctl_lun *lun; 11152 struct ctl_softc *ctl_softc; 11153 union ctl_io *next_io, *pending_io; 11154 union ctl_io *io; 11155 int lun_idx; 11156 int i; 11157 11158 ctl_softc = control_softc; 11159 11160 mtx_lock(&ctl_softc->ctl_lock); 11161 /* 11162 * Remove any cmds from the other SC from the rtr queue. These 11163 * will obviously only be for LUNs for which we're the primary. 11164 * We can't send status or get/send data for these commands. 11165 * Since they haven't been executed yet, we can just remove them. 11166 * We'll either abort them or delete them below, depending on 11167 * which HA mode we're in. 11168 */ 11169#ifdef notyet 11170 mtx_lock(&ctl_softc->queue_lock); 11171 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11172 io != NULL; io = next_io) { 11173 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11174 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11175 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11176 ctl_io_hdr, links); 11177 } 11178 mtx_unlock(&ctl_softc->queue_lock); 11179#endif 11180 11181 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11182 lun = ctl_softc->ctl_luns[lun_idx]; 11183 if (lun==NULL) 11184 continue; 11185 11186 /* 11187 * Processor LUNs are primary on both sides. 11188 * XXX will this always be true? 11189 */ 11190 if (lun->be_lun->lun_type == T_PROCESSOR) 11191 continue; 11192 11193 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11194 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11195 printf("FAILOVER: primary lun %d\n", lun_idx); 11196 /* 11197 * Remove all commands from the other SC. First from the 11198 * blocked queue then from the ooa queue. Once we have 11199 * removed them. Call ctl_check_blocked to see if there 11200 * is anything that can run. 11201 */ 11202 for (io = (union ctl_io *)TAILQ_FIRST( 11203 &lun->blocked_queue); io != NULL; io = next_io) { 11204 11205 next_io = (union ctl_io *)TAILQ_NEXT( 11206 &io->io_hdr, blocked_links); 11207 11208 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11209 TAILQ_REMOVE(&lun->blocked_queue, 11210 &io->io_hdr,blocked_links); 11211 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11212 TAILQ_REMOVE(&lun->ooa_queue, 11213 &io->io_hdr, ooa_links); 11214 11215 ctl_free_io(io); 11216 } 11217 } 11218 11219 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11220 io != NULL; io = next_io) { 11221 11222 next_io = (union ctl_io *)TAILQ_NEXT( 11223 &io->io_hdr, ooa_links); 11224 11225 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11226 11227 TAILQ_REMOVE(&lun->ooa_queue, 11228 &io->io_hdr, 11229 ooa_links); 11230 11231 ctl_free_io(io); 11232 } 11233 } 11234 ctl_check_blocked(lun); 11235 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11236 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11237 11238 printf("FAILOVER: primary lun %d\n", lun_idx); 11239 /* 11240 * Abort all commands from the other SC. We can't 11241 * send status back for them now. These should get 11242 * cleaned up when they are completed or come out 11243 * for a datamove operation. 11244 */ 11245 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11246 io != NULL; io = next_io) { 11247 next_io = (union ctl_io *)TAILQ_NEXT( 11248 &io->io_hdr, ooa_links); 11249 11250 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11251 io->io_hdr.flags |= CTL_FLAG_ABORT; 11252 } 11253 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11254 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11255 11256 printf("FAILOVER: secondary lun %d\n", lun_idx); 11257 11258 lun->flags |= CTL_LUN_PRIMARY_SC; 11259 11260 /* 11261 * We send all I/O that was sent to this controller 11262 * and redirected to the other side back with 11263 * busy status, and have the initiator retry it. 11264 * Figuring out how much data has been transferred, 11265 * etc. and picking up where we left off would be 11266 * very tricky. 11267 * 11268 * XXX KDM need to remove I/O from the blocked 11269 * queue as well! 11270 */ 11271 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11272 &lun->ooa_queue); pending_io != NULL; 11273 pending_io = next_io) { 11274 11275 next_io = (union ctl_io *)TAILQ_NEXT( 11276 &pending_io->io_hdr, ooa_links); 11277 11278 pending_io->io_hdr.flags &= 11279 ~CTL_FLAG_SENT_2OTHER_SC; 11280 11281 if (pending_io->io_hdr.flags & 11282 CTL_FLAG_IO_ACTIVE) { 11283 pending_io->io_hdr.flags |= 11284 CTL_FLAG_FAILOVER; 11285 } else { 11286 ctl_set_busy(&pending_io->scsiio); 11287 ctl_done(pending_io); 11288 } 11289 } 11290 11291 /* 11292 * Build Unit Attention 11293 */ 11294 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11295 lun->pending_ua[i] |= 11296 CTL_UA_ASYM_ACC_CHANGE; 11297 } 11298 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11299 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11300 printf("FAILOVER: secondary lun %d\n", lun_idx); 11301 /* 11302 * if the first io on the OOA is not on the RtR queue 11303 * add it. 11304 */ 11305 lun->flags |= CTL_LUN_PRIMARY_SC; 11306 11307 pending_io = (union ctl_io *)TAILQ_FIRST( 11308 &lun->ooa_queue); 11309 if (pending_io==NULL) { 11310 printf("Nothing on OOA queue\n"); 11311 continue; 11312 } 11313 11314 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11315 if ((pending_io->io_hdr.flags & 11316 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11317 pending_io->io_hdr.flags |= 11318 CTL_FLAG_IS_WAS_ON_RTR; 11319 ctl_enqueue_rtr(pending_io); 11320 } 11321#if 0 11322 else 11323 { 11324 printf("Tag 0x%04x is running\n", 11325 pending_io->scsiio.tag_num); 11326 } 11327#endif 11328 11329 next_io = (union ctl_io *)TAILQ_NEXT( 11330 &pending_io->io_hdr, ooa_links); 11331 for (pending_io=next_io; pending_io != NULL; 11332 pending_io = next_io) { 11333 pending_io->io_hdr.flags &= 11334 ~CTL_FLAG_SENT_2OTHER_SC; 11335 next_io = (union ctl_io *)TAILQ_NEXT( 11336 &pending_io->io_hdr, ooa_links); 11337 if (pending_io->io_hdr.flags & 11338 CTL_FLAG_IS_WAS_ON_RTR) { 11339#if 0 11340 printf("Tag 0x%04x is running\n", 11341 pending_io->scsiio.tag_num); 11342#endif 11343 continue; 11344 } 11345 11346 switch (ctl_check_ooa(lun, pending_io, 11347 (union ctl_io *)TAILQ_PREV( 11348 &pending_io->io_hdr, ctl_ooaq, 11349 ooa_links))) { 11350 11351 case CTL_ACTION_BLOCK: 11352 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11353 &pending_io->io_hdr, 11354 blocked_links); 11355 pending_io->io_hdr.flags |= 11356 CTL_FLAG_BLOCKED; 11357 break; 11358 case CTL_ACTION_PASS: 11359 case CTL_ACTION_SKIP: 11360 pending_io->io_hdr.flags |= 11361 CTL_FLAG_IS_WAS_ON_RTR; 11362 ctl_enqueue_rtr(pending_io); 11363 break; 11364 case CTL_ACTION_OVERLAP: 11365 ctl_set_overlapped_cmd( 11366 (struct ctl_scsiio *)pending_io); 11367 ctl_done(pending_io); 11368 break; 11369 case CTL_ACTION_OVERLAP_TAG: 11370 ctl_set_overlapped_tag( 11371 (struct ctl_scsiio *)pending_io, 11372 pending_io->scsiio.tag_num & 0xff); 11373 ctl_done(pending_io); 11374 break; 11375 case CTL_ACTION_ERROR: 11376 default: 11377 ctl_set_internal_failure( 11378 (struct ctl_scsiio *)pending_io, 11379 0, // sks_valid 11380 0); //retry count 11381 ctl_done(pending_io); 11382 break; 11383 } 11384 } 11385 11386 /* 11387 * Build Unit Attention 11388 */ 11389 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11390 lun->pending_ua[i] |= 11391 CTL_UA_ASYM_ACC_CHANGE; 11392 } 11393 } else { 11394 panic("Unhandled HA mode failover, LUN flags = %#x, " 11395 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11396 } 11397 } 11398 ctl_pause_rtr = 0; 11399 mtx_unlock(&ctl_softc->ctl_lock); 11400} 11401 11402static int 11403ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11404{ 11405 struct ctl_lun *lun; 11406 const struct ctl_cmd_entry *entry; 11407 uint32_t initidx, targ_lun; 11408 int retval; 11409 11410 retval = 0; 11411 11412 lun = NULL; 11413 11414 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11415 if ((targ_lun < CTL_MAX_LUNS) 11416 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11417 lun = ctl_softc->ctl_luns[targ_lun]; 11418 /* 11419 * If the LUN is invalid, pretend that it doesn't exist. 11420 * It will go away as soon as all pending I/O has been 11421 * completed. 11422 */ 11423 if (lun->flags & CTL_LUN_DISABLED) { 11424 lun = NULL; 11425 } else { 11426 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11427 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11428 lun->be_lun; 11429 if (lun->be_lun->lun_type == T_PROCESSOR) { 11430 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11431 } 11432 11433 /* 11434 * Every I/O goes into the OOA queue for a 11435 * particular LUN, and stays there until completion. 11436 */ 11437 mtx_lock(&lun->lun_lock); 11438 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11439 ooa_links); 11440 } 11441 } else { 11442 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11443 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11444 } 11445 11446 /* Get command entry and return error if it is unsuppotyed. */ 11447 entry = ctl_validate_command(ctsio); 11448 if (entry == NULL) { 11449 if (lun) 11450 mtx_unlock(&lun->lun_lock); 11451 return (retval); 11452 } 11453 11454 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11455 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11456 11457 /* 11458 * Check to see whether we can send this command to LUNs that don't 11459 * exist. This should pretty much only be the case for inquiry 11460 * and request sense. Further checks, below, really require having 11461 * a LUN, so we can't really check the command anymore. Just put 11462 * it on the rtr queue. 11463 */ 11464 if (lun == NULL) { 11465 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11466 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11467 ctl_enqueue_rtr((union ctl_io *)ctsio); 11468 return (retval); 11469 } 11470 11471 ctl_set_unsupported_lun(ctsio); 11472 ctl_done((union ctl_io *)ctsio); 11473 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11474 return (retval); 11475 } else { 11476 /* 11477 * Make sure we support this particular command on this LUN. 11478 * e.g., we don't support writes to the control LUN. 11479 */ 11480 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11481 mtx_unlock(&lun->lun_lock); 11482 ctl_set_invalid_opcode(ctsio); 11483 ctl_done((union ctl_io *)ctsio); 11484 return (retval); 11485 } 11486 } 11487 11488 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11489 11490#ifdef CTL_WITH_CA 11491 /* 11492 * If we've got a request sense, it'll clear the contingent 11493 * allegiance condition. Otherwise, if we have a CA condition for 11494 * this initiator, clear it, because it sent down a command other 11495 * than request sense. 11496 */ 11497 if ((ctsio->cdb[0] != REQUEST_SENSE) 11498 && (ctl_is_set(lun->have_ca, initidx))) 11499 ctl_clear_mask(lun->have_ca, initidx); 11500#endif 11501 11502 /* 11503 * If the command has this flag set, it handles its own unit 11504 * attention reporting, we shouldn't do anything. Otherwise we 11505 * check for any pending unit attentions, and send them back to the 11506 * initiator. We only do this when a command initially comes in, 11507 * not when we pull it off the blocked queue. 11508 * 11509 * According to SAM-3, section 5.3.2, the order that things get 11510 * presented back to the host is basically unit attentions caused 11511 * by some sort of reset event, busy status, reservation conflicts 11512 * or task set full, and finally any other status. 11513 * 11514 * One issue here is that some of the unit attentions we report 11515 * don't fall into the "reset" category (e.g. "reported luns data 11516 * has changed"). So reporting it here, before the reservation 11517 * check, may be technically wrong. I guess the only thing to do 11518 * would be to check for and report the reset events here, and then 11519 * check for the other unit attention types after we check for a 11520 * reservation conflict. 11521 * 11522 * XXX KDM need to fix this 11523 */ 11524 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11525 ctl_ua_type ua_type; 11526 11527 ua_type = lun->pending_ua[initidx]; 11528 if (ua_type != CTL_UA_NONE) { 11529 scsi_sense_data_type sense_format; 11530 11531 if (lun != NULL) 11532 sense_format = (lun->flags & 11533 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11534 SSD_TYPE_FIXED; 11535 else 11536 sense_format = SSD_TYPE_FIXED; 11537 11538 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11539 sense_format); 11540 if (ua_type != CTL_UA_NONE) { 11541 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11542 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11543 CTL_AUTOSENSE; 11544 ctsio->sense_len = SSD_FULL_SIZE; 11545 lun->pending_ua[initidx] &= ~ua_type; 11546 mtx_unlock(&lun->lun_lock); 11547 ctl_done((union ctl_io *)ctsio); 11548 return (retval); 11549 } 11550 } 11551 } 11552 11553 11554 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11555 mtx_unlock(&lun->lun_lock); 11556 ctl_done((union ctl_io *)ctsio); 11557 return (retval); 11558 } 11559 11560 /* 11561 * XXX CHD this is where we want to send IO to other side if 11562 * this LUN is secondary on this SC. We will need to make a copy 11563 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11564 * the copy we send as FROM_OTHER. 11565 * We also need to stuff the address of the original IO so we can 11566 * find it easily. Something similar will need be done on the other 11567 * side so when we are done we can find the copy. 11568 */ 11569 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11570 union ctl_ha_msg msg_info; 11571 int isc_retval; 11572 11573 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11574 11575 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11576 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11577#if 0 11578 printf("1. ctsio %p\n", ctsio); 11579#endif 11580 msg_info.hdr.serializing_sc = NULL; 11581 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11582 msg_info.scsi.tag_num = ctsio->tag_num; 11583 msg_info.scsi.tag_type = ctsio->tag_type; 11584 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11585 11586 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11587 11588 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11589 (void *)&msg_info, sizeof(msg_info), 0)) > 11590 CTL_HA_STATUS_SUCCESS) { 11591 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11592 isc_retval); 11593 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11594 } else { 11595#if 0 11596 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11597#endif 11598 } 11599 11600 /* 11601 * XXX KDM this I/O is off the incoming queue, but hasn't 11602 * been inserted on any other queue. We may need to come 11603 * up with a holding queue while we wait for serialization 11604 * so that we have an idea of what we're waiting for from 11605 * the other side. 11606 */ 11607 mtx_unlock(&lun->lun_lock); 11608 return (retval); 11609 } 11610 11611 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11612 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11613 ctl_ooaq, ooa_links))) { 11614 case CTL_ACTION_BLOCK: 11615 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11616 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11617 blocked_links); 11618 mtx_unlock(&lun->lun_lock); 11619 return (retval); 11620 case CTL_ACTION_PASS: 11621 case CTL_ACTION_SKIP: 11622 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11623 mtx_unlock(&lun->lun_lock); 11624 ctl_enqueue_rtr((union ctl_io *)ctsio); 11625 break; 11626 case CTL_ACTION_OVERLAP: 11627 mtx_unlock(&lun->lun_lock); 11628 ctl_set_overlapped_cmd(ctsio); 11629 ctl_done((union ctl_io *)ctsio); 11630 break; 11631 case CTL_ACTION_OVERLAP_TAG: 11632 mtx_unlock(&lun->lun_lock); 11633 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11634 ctl_done((union ctl_io *)ctsio); 11635 break; 11636 case CTL_ACTION_ERROR: 11637 default: 11638 mtx_unlock(&lun->lun_lock); 11639 ctl_set_internal_failure(ctsio, 11640 /*sks_valid*/ 0, 11641 /*retry_count*/ 0); 11642 ctl_done((union ctl_io *)ctsio); 11643 break; 11644 } 11645 return (retval); 11646} 11647 11648const struct ctl_cmd_entry * 11649ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11650{ 11651 const struct ctl_cmd_entry *entry; 11652 int service_action; 11653 11654 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11655 if (entry->flags & CTL_CMD_FLAG_SA5) { 11656 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11657 entry = &((const struct ctl_cmd_entry *) 11658 entry->execute)[service_action]; 11659 } 11660 return (entry); 11661} 11662 11663const struct ctl_cmd_entry * 11664ctl_validate_command(struct ctl_scsiio *ctsio) 11665{ 11666 const struct ctl_cmd_entry *entry; 11667 int i; 11668 uint8_t diff; 11669 11670 entry = ctl_get_cmd_entry(ctsio); 11671 if (entry->execute == NULL) { 11672 ctl_set_invalid_opcode(ctsio); 11673 ctl_done((union ctl_io *)ctsio); 11674 return (NULL); 11675 } 11676 KASSERT(entry->length > 0, 11677 ("Not defined length for command 0x%02x/0x%02x", 11678 ctsio->cdb[0], ctsio->cdb[1])); 11679 for (i = 1; i < entry->length; i++) { 11680 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11681 if (diff == 0) 11682 continue; 11683 ctl_set_invalid_field(ctsio, 11684 /*sks_valid*/ 1, 11685 /*command*/ 1, 11686 /*field*/ i, 11687 /*bit_valid*/ 1, 11688 /*bit*/ fls(diff) - 1); 11689 ctl_done((union ctl_io *)ctsio); 11690 return (NULL); 11691 } 11692 return (entry); 11693} 11694 11695static int 11696ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11697{ 11698 11699 switch (lun_type) { 11700 case T_PROCESSOR: 11701 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11702 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11703 return (0); 11704 break; 11705 case T_DIRECT: 11706 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11707 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11708 return (0); 11709 break; 11710 default: 11711 return (0); 11712 } 11713 return (1); 11714} 11715 11716static int 11717ctl_scsiio(struct ctl_scsiio *ctsio) 11718{ 11719 int retval; 11720 const struct ctl_cmd_entry *entry; 11721 11722 retval = CTL_RETVAL_COMPLETE; 11723 11724 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11725 11726 entry = ctl_get_cmd_entry(ctsio); 11727 11728 /* 11729 * If this I/O has been aborted, just send it straight to 11730 * ctl_done() without executing it. 11731 */ 11732 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11733 ctl_done((union ctl_io *)ctsio); 11734 goto bailout; 11735 } 11736 11737 /* 11738 * All the checks should have been handled by ctl_scsiio_precheck(). 11739 * We should be clear now to just execute the I/O. 11740 */ 11741 retval = entry->execute(ctsio); 11742 11743bailout: 11744 return (retval); 11745} 11746 11747/* 11748 * Since we only implement one target right now, a bus reset simply resets 11749 * our single target. 11750 */ 11751static int 11752ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11753{ 11754 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11755} 11756 11757static int 11758ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11759 ctl_ua_type ua_type) 11760{ 11761 struct ctl_lun *lun; 11762 int retval; 11763 11764 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11765 union ctl_ha_msg msg_info; 11766 11767 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11768 msg_info.hdr.nexus = io->io_hdr.nexus; 11769 if (ua_type==CTL_UA_TARG_RESET) 11770 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11771 else 11772 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11773 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11774 msg_info.hdr.original_sc = NULL; 11775 msg_info.hdr.serializing_sc = NULL; 11776 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11777 (void *)&msg_info, sizeof(msg_info), 0)) { 11778 } 11779 } 11780 retval = 0; 11781 11782 mtx_lock(&ctl_softc->ctl_lock); 11783 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11784 retval += ctl_lun_reset(lun, io, ua_type); 11785 mtx_unlock(&ctl_softc->ctl_lock); 11786 11787 return (retval); 11788} 11789 11790/* 11791 * The LUN should always be set. The I/O is optional, and is used to 11792 * distinguish between I/Os sent by this initiator, and by other 11793 * initiators. We set unit attention for initiators other than this one. 11794 * SAM-3 is vague on this point. It does say that a unit attention should 11795 * be established for other initiators when a LUN is reset (see section 11796 * 5.7.3), but it doesn't specifically say that the unit attention should 11797 * be established for this particular initiator when a LUN is reset. Here 11798 * is the relevant text, from SAM-3 rev 8: 11799 * 11800 * 5.7.2 When a SCSI initiator port aborts its own tasks 11801 * 11802 * When a SCSI initiator port causes its own task(s) to be aborted, no 11803 * notification that the task(s) have been aborted shall be returned to 11804 * the SCSI initiator port other than the completion response for the 11805 * command or task management function action that caused the task(s) to 11806 * be aborted and notification(s) associated with related effects of the 11807 * action (e.g., a reset unit attention condition). 11808 * 11809 * XXX KDM for now, we're setting unit attention for all initiators. 11810 */ 11811static int 11812ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11813{ 11814 union ctl_io *xio; 11815#if 0 11816 uint32_t initindex; 11817#endif 11818 int i; 11819 11820 mtx_lock(&lun->lun_lock); 11821 /* 11822 * Run through the OOA queue and abort each I/O. 11823 */ 11824#if 0 11825 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11826#endif 11827 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11828 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11829 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11830 } 11831 11832 /* 11833 * This version sets unit attention for every 11834 */ 11835#if 0 11836 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11837 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11838 if (initindex == i) 11839 continue; 11840 lun->pending_ua[i] |= ua_type; 11841 } 11842#endif 11843 11844 /* 11845 * A reset (any kind, really) clears reservations established with 11846 * RESERVE/RELEASE. It does not clear reservations established 11847 * with PERSISTENT RESERVE OUT, but we don't support that at the 11848 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11849 * reservations made with the RESERVE/RELEASE commands, because 11850 * those commands are obsolete in SPC-3. 11851 */ 11852 lun->flags &= ~CTL_LUN_RESERVED; 11853 11854 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11855#ifdef CTL_WITH_CA 11856 ctl_clear_mask(lun->have_ca, i); 11857#endif 11858 lun->pending_ua[i] |= ua_type; 11859 } 11860 mtx_unlock(&lun->lun_lock); 11861 11862 return (0); 11863} 11864 11865static int 11866ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11867 int other_sc) 11868{ 11869 union ctl_io *xio; 11870 int found; 11871 11872 mtx_assert(&lun->lun_lock, MA_OWNED); 11873 11874 /* 11875 * Run through the OOA queue and attempt to find the given I/O. 11876 * The target port, initiator ID, tag type and tag number have to 11877 * match the values that we got from the initiator. If we have an 11878 * untagged command to abort, simply abort the first untagged command 11879 * we come to. We only allow one untagged command at a time of course. 11880 */ 11881 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11882 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11883 11884 if ((targ_port == UINT32_MAX || 11885 targ_port == xio->io_hdr.nexus.targ_port) && 11886 (init_id == UINT32_MAX || 11887 init_id == xio->io_hdr.nexus.initid.id)) { 11888 if (targ_port != xio->io_hdr.nexus.targ_port || 11889 init_id != xio->io_hdr.nexus.initid.id) 11890 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11891 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11892 found = 1; 11893 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11894 union ctl_ha_msg msg_info; 11895 11896 msg_info.hdr.nexus = xio->io_hdr.nexus; 11897 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11898 msg_info.task.tag_num = xio->scsiio.tag_num; 11899 msg_info.task.tag_type = xio->scsiio.tag_type; 11900 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11901 msg_info.hdr.original_sc = NULL; 11902 msg_info.hdr.serializing_sc = NULL; 11903 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11904 (void *)&msg_info, sizeof(msg_info), 0); 11905 } 11906 } 11907 } 11908 return (found); 11909} 11910 11911static int 11912ctl_abort_task_set(union ctl_io *io) 11913{ 11914 struct ctl_softc *softc = control_softc; 11915 struct ctl_lun *lun; 11916 uint32_t targ_lun; 11917 11918 /* 11919 * Look up the LUN. 11920 */ 11921 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11922 mtx_lock(&softc->ctl_lock); 11923 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11924 lun = softc->ctl_luns[targ_lun]; 11925 else { 11926 mtx_unlock(&softc->ctl_lock); 11927 return (1); 11928 } 11929 11930 mtx_lock(&lun->lun_lock); 11931 mtx_unlock(&softc->ctl_lock); 11932 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11933 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11934 io->io_hdr.nexus.initid.id, 11935 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11936 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11937 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11938 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11939 } 11940 mtx_unlock(&lun->lun_lock); 11941 return (0); 11942} 11943 11944static int 11945ctl_i_t_nexus_reset(union ctl_io *io) 11946{ 11947 struct ctl_softc *softc = control_softc; 11948 struct ctl_lun *lun; 11949 uint32_t initindex; 11950 11951 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11952 mtx_lock(&softc->ctl_lock); 11953 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11954 mtx_lock(&lun->lun_lock); 11955 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11956 io->io_hdr.nexus.initid.id, 11957 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11958#ifdef CTL_WITH_CA 11959 ctl_clear_mask(lun->have_ca, initindex); 11960#endif 11961 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 11962 mtx_unlock(&lun->lun_lock); 11963 } 11964 mtx_unlock(&softc->ctl_lock); 11965 return (0); 11966} 11967 11968static int 11969ctl_abort_task(union ctl_io *io) 11970{ 11971 union ctl_io *xio; 11972 struct ctl_lun *lun; 11973 struct ctl_softc *ctl_softc; 11974#if 0 11975 struct sbuf sb; 11976 char printbuf[128]; 11977#endif 11978 int found; 11979 uint32_t targ_lun; 11980 11981 ctl_softc = control_softc; 11982 found = 0; 11983 11984 /* 11985 * Look up the LUN. 11986 */ 11987 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11988 mtx_lock(&ctl_softc->ctl_lock); 11989 if ((targ_lun < CTL_MAX_LUNS) 11990 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11991 lun = ctl_softc->ctl_luns[targ_lun]; 11992 else { 11993 mtx_unlock(&ctl_softc->ctl_lock); 11994 return (1); 11995 } 11996 11997#if 0 11998 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11999 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12000#endif 12001 12002 mtx_lock(&lun->lun_lock); 12003 mtx_unlock(&ctl_softc->ctl_lock); 12004 /* 12005 * Run through the OOA queue and attempt to find the given I/O. 12006 * The target port, initiator ID, tag type and tag number have to 12007 * match the values that we got from the initiator. If we have an 12008 * untagged command to abort, simply abort the first untagged command 12009 * we come to. We only allow one untagged command at a time of course. 12010 */ 12011#if 0 12012 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12013#endif 12014 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12015 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12016#if 0 12017 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12018 12019 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12020 lun->lun, xio->scsiio.tag_num, 12021 xio->scsiio.tag_type, 12022 (xio->io_hdr.blocked_links.tqe_prev 12023 == NULL) ? "" : " BLOCKED", 12024 (xio->io_hdr.flags & 12025 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12026 (xio->io_hdr.flags & 12027 CTL_FLAG_ABORT) ? " ABORT" : "", 12028 (xio->io_hdr.flags & 12029 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12030 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12031 sbuf_finish(&sb); 12032 printf("%s\n", sbuf_data(&sb)); 12033#endif 12034 12035 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12036 && (xio->io_hdr.nexus.initid.id == 12037 io->io_hdr.nexus.initid.id)) { 12038 /* 12039 * If the abort says that the task is untagged, the 12040 * task in the queue must be untagged. Otherwise, 12041 * we just check to see whether the tag numbers 12042 * match. This is because the QLogic firmware 12043 * doesn't pass back the tag type in an abort 12044 * request. 12045 */ 12046#if 0 12047 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12048 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12049 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12050#endif 12051 /* 12052 * XXX KDM we've got problems with FC, because it 12053 * doesn't send down a tag type with aborts. So we 12054 * can only really go by the tag number... 12055 * This may cause problems with parallel SCSI. 12056 * Need to figure that out!! 12057 */ 12058 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12059 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12060 found = 1; 12061 if ((io->io_hdr.flags & 12062 CTL_FLAG_FROM_OTHER_SC) == 0 && 12063 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12064 union ctl_ha_msg msg_info; 12065 12066 io->io_hdr.flags |= 12067 CTL_FLAG_SENT_2OTHER_SC; 12068 msg_info.hdr.nexus = io->io_hdr.nexus; 12069 msg_info.task.task_action = 12070 CTL_TASK_ABORT_TASK; 12071 msg_info.task.tag_num = 12072 io->taskio.tag_num; 12073 msg_info.task.tag_type = 12074 io->taskio.tag_type; 12075 msg_info.hdr.msg_type = 12076 CTL_MSG_MANAGE_TASKS; 12077 msg_info.hdr.original_sc = NULL; 12078 msg_info.hdr.serializing_sc = NULL; 12079#if 0 12080 printf("Sent Abort to other side\n"); 12081#endif 12082 if (CTL_HA_STATUS_SUCCESS != 12083 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12084 (void *)&msg_info, 12085 sizeof(msg_info), 0)) { 12086 } 12087 } 12088#if 0 12089 printf("ctl_abort_task: found I/O to abort\n"); 12090#endif 12091 break; 12092 } 12093 } 12094 } 12095 mtx_unlock(&lun->lun_lock); 12096 12097 if (found == 0) { 12098 /* 12099 * This isn't really an error. It's entirely possible for 12100 * the abort and command completion to cross on the wire. 12101 * This is more of an informative/diagnostic error. 12102 */ 12103#if 0 12104 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12105 "%d:%d:%d:%d tag %d type %d\n", 12106 io->io_hdr.nexus.initid.id, 12107 io->io_hdr.nexus.targ_port, 12108 io->io_hdr.nexus.targ_target.id, 12109 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12110 io->taskio.tag_type); 12111#endif 12112 } 12113 return (0); 12114} 12115 12116static void 12117ctl_run_task(union ctl_io *io) 12118{ 12119 struct ctl_softc *ctl_softc = control_softc; 12120 int retval = 1; 12121 const char *task_desc; 12122 12123 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12124 12125 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12126 ("ctl_run_task: Unextected io_type %d\n", 12127 io->io_hdr.io_type)); 12128 12129 task_desc = ctl_scsi_task_string(&io->taskio); 12130 if (task_desc != NULL) { 12131#ifdef NEEDTOPORT 12132 csevent_log(CSC_CTL | CSC_SHELF_SW | 12133 CTL_TASK_REPORT, 12134 csevent_LogType_Trace, 12135 csevent_Severity_Information, 12136 csevent_AlertLevel_Green, 12137 csevent_FRU_Firmware, 12138 csevent_FRU_Unknown, 12139 "CTL: received task: %s",task_desc); 12140#endif 12141 } else { 12142#ifdef NEEDTOPORT 12143 csevent_log(CSC_CTL | CSC_SHELF_SW | 12144 CTL_TASK_REPORT, 12145 csevent_LogType_Trace, 12146 csevent_Severity_Information, 12147 csevent_AlertLevel_Green, 12148 csevent_FRU_Firmware, 12149 csevent_FRU_Unknown, 12150 "CTL: received unknown task " 12151 "type: %d (%#x)", 12152 io->taskio.task_action, 12153 io->taskio.task_action); 12154#endif 12155 } 12156 switch (io->taskio.task_action) { 12157 case CTL_TASK_ABORT_TASK: 12158 retval = ctl_abort_task(io); 12159 break; 12160 case CTL_TASK_ABORT_TASK_SET: 12161 case CTL_TASK_CLEAR_TASK_SET: 12162 retval = ctl_abort_task_set(io); 12163 break; 12164 case CTL_TASK_CLEAR_ACA: 12165 break; 12166 case CTL_TASK_I_T_NEXUS_RESET: 12167 retval = ctl_i_t_nexus_reset(io); 12168 break; 12169 case CTL_TASK_LUN_RESET: { 12170 struct ctl_lun *lun; 12171 uint32_t targ_lun; 12172 12173 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12174 mtx_lock(&ctl_softc->ctl_lock); 12175 if ((targ_lun < CTL_MAX_LUNS) 12176 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12177 lun = ctl_softc->ctl_luns[targ_lun]; 12178 else { 12179 mtx_unlock(&ctl_softc->ctl_lock); 12180 retval = 1; 12181 break; 12182 } 12183 12184 if (!(io->io_hdr.flags & 12185 CTL_FLAG_FROM_OTHER_SC)) { 12186 union ctl_ha_msg msg_info; 12187 12188 io->io_hdr.flags |= 12189 CTL_FLAG_SENT_2OTHER_SC; 12190 msg_info.hdr.msg_type = 12191 CTL_MSG_MANAGE_TASKS; 12192 msg_info.hdr.nexus = io->io_hdr.nexus; 12193 msg_info.task.task_action = 12194 CTL_TASK_LUN_RESET; 12195 msg_info.hdr.original_sc = NULL; 12196 msg_info.hdr.serializing_sc = NULL; 12197 if (CTL_HA_STATUS_SUCCESS != 12198 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12199 (void *)&msg_info, 12200 sizeof(msg_info), 0)) { 12201 } 12202 } 12203 12204 retval = ctl_lun_reset(lun, io, 12205 CTL_UA_LUN_RESET); 12206 mtx_unlock(&ctl_softc->ctl_lock); 12207 break; 12208 } 12209 case CTL_TASK_TARGET_RESET: 12210 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12211 break; 12212 case CTL_TASK_BUS_RESET: 12213 retval = ctl_bus_reset(ctl_softc, io); 12214 break; 12215 case CTL_TASK_PORT_LOGIN: 12216 break; 12217 case CTL_TASK_PORT_LOGOUT: 12218 break; 12219 default: 12220 printf("ctl_run_task: got unknown task management event %d\n", 12221 io->taskio.task_action); 12222 break; 12223 } 12224 if (retval == 0) 12225 io->io_hdr.status = CTL_SUCCESS; 12226 else 12227 io->io_hdr.status = CTL_ERROR; 12228 ctl_done(io); 12229} 12230 12231/* 12232 * For HA operation. Handle commands that come in from the other 12233 * controller. 12234 */ 12235static void 12236ctl_handle_isc(union ctl_io *io) 12237{ 12238 int free_io; 12239 struct ctl_lun *lun; 12240 struct ctl_softc *ctl_softc; 12241 uint32_t targ_lun; 12242 12243 ctl_softc = control_softc; 12244 12245 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12246 lun = ctl_softc->ctl_luns[targ_lun]; 12247 12248 switch (io->io_hdr.msg_type) { 12249 case CTL_MSG_SERIALIZE: 12250 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12251 break; 12252 case CTL_MSG_R2R: { 12253 const struct ctl_cmd_entry *entry; 12254 12255 /* 12256 * This is only used in SER_ONLY mode. 12257 */ 12258 free_io = 0; 12259 entry = ctl_get_cmd_entry(&io->scsiio); 12260 mtx_lock(&lun->lun_lock); 12261 if (ctl_scsiio_lun_check(ctl_softc, lun, 12262 entry, (struct ctl_scsiio *)io) != 0) { 12263 mtx_unlock(&lun->lun_lock); 12264 ctl_done(io); 12265 break; 12266 } 12267 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12268 mtx_unlock(&lun->lun_lock); 12269 ctl_enqueue_rtr(io); 12270 break; 12271 } 12272 case CTL_MSG_FINISH_IO: 12273 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12274 free_io = 0; 12275 ctl_done(io); 12276 } else { 12277 free_io = 1; 12278 mtx_lock(&lun->lun_lock); 12279 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12280 ooa_links); 12281 ctl_check_blocked(lun); 12282 mtx_unlock(&lun->lun_lock); 12283 } 12284 break; 12285 case CTL_MSG_PERS_ACTION: 12286 ctl_hndl_per_res_out_on_other_sc( 12287 (union ctl_ha_msg *)&io->presio.pr_msg); 12288 free_io = 1; 12289 break; 12290 case CTL_MSG_BAD_JUJU: 12291 free_io = 0; 12292 ctl_done(io); 12293 break; 12294 case CTL_MSG_DATAMOVE: 12295 /* Only used in XFER mode */ 12296 free_io = 0; 12297 ctl_datamove_remote(io); 12298 break; 12299 case CTL_MSG_DATAMOVE_DONE: 12300 /* Only used in XFER mode */ 12301 free_io = 0; 12302 io->scsiio.be_move_done(io); 12303 break; 12304 default: 12305 free_io = 1; 12306 printf("%s: Invalid message type %d\n", 12307 __func__, io->io_hdr.msg_type); 12308 break; 12309 } 12310 if (free_io) 12311 ctl_free_io(io); 12312 12313} 12314 12315 12316/* 12317 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12318 * there is no match. 12319 */ 12320static ctl_lun_error_pattern 12321ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12322{ 12323 const struct ctl_cmd_entry *entry; 12324 ctl_lun_error_pattern filtered_pattern, pattern; 12325 12326 pattern = desc->error_pattern; 12327 12328 /* 12329 * XXX KDM we need more data passed into this function to match a 12330 * custom pattern, and we actually need to implement custom pattern 12331 * matching. 12332 */ 12333 if (pattern & CTL_LUN_PAT_CMD) 12334 return (CTL_LUN_PAT_CMD); 12335 12336 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12337 return (CTL_LUN_PAT_ANY); 12338 12339 entry = ctl_get_cmd_entry(ctsio); 12340 12341 filtered_pattern = entry->pattern & pattern; 12342 12343 /* 12344 * If the user requested specific flags in the pattern (e.g. 12345 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12346 * flags. 12347 * 12348 * If the user did not specify any flags, it doesn't matter whether 12349 * or not the command supports the flags. 12350 */ 12351 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12352 (pattern & ~CTL_LUN_PAT_MASK)) 12353 return (CTL_LUN_PAT_NONE); 12354 12355 /* 12356 * If the user asked for a range check, see if the requested LBA 12357 * range overlaps with this command's LBA range. 12358 */ 12359 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12360 uint64_t lba1; 12361 uint32_t len1; 12362 ctl_action action; 12363 int retval; 12364 12365 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12366 if (retval != 0) 12367 return (CTL_LUN_PAT_NONE); 12368 12369 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12370 desc->lba_range.len); 12371 /* 12372 * A "pass" means that the LBA ranges don't overlap, so 12373 * this doesn't match the user's range criteria. 12374 */ 12375 if (action == CTL_ACTION_PASS) 12376 return (CTL_LUN_PAT_NONE); 12377 } 12378 12379 return (filtered_pattern); 12380} 12381 12382static void 12383ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12384{ 12385 struct ctl_error_desc *desc, *desc2; 12386 12387 mtx_assert(&lun->lun_lock, MA_OWNED); 12388 12389 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12390 ctl_lun_error_pattern pattern; 12391 /* 12392 * Check to see whether this particular command matches 12393 * the pattern in the descriptor. 12394 */ 12395 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12396 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12397 continue; 12398 12399 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12400 case CTL_LUN_INJ_ABORTED: 12401 ctl_set_aborted(&io->scsiio); 12402 break; 12403 case CTL_LUN_INJ_MEDIUM_ERR: 12404 ctl_set_medium_error(&io->scsiio); 12405 break; 12406 case CTL_LUN_INJ_UA: 12407 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12408 * OCCURRED */ 12409 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12410 break; 12411 case CTL_LUN_INJ_CUSTOM: 12412 /* 12413 * We're assuming the user knows what he is doing. 12414 * Just copy the sense information without doing 12415 * checks. 12416 */ 12417 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12418 ctl_min(sizeof(desc->custom_sense), 12419 sizeof(io->scsiio.sense_data))); 12420 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12421 io->scsiio.sense_len = SSD_FULL_SIZE; 12422 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12423 break; 12424 case CTL_LUN_INJ_NONE: 12425 default: 12426 /* 12427 * If this is an error injection type we don't know 12428 * about, clear the continuous flag (if it is set) 12429 * so it will get deleted below. 12430 */ 12431 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12432 break; 12433 } 12434 /* 12435 * By default, each error injection action is a one-shot 12436 */ 12437 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12438 continue; 12439 12440 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12441 12442 free(desc, M_CTL); 12443 } 12444} 12445 12446#ifdef CTL_IO_DELAY 12447static void 12448ctl_datamove_timer_wakeup(void *arg) 12449{ 12450 union ctl_io *io; 12451 12452 io = (union ctl_io *)arg; 12453 12454 ctl_datamove(io); 12455} 12456#endif /* CTL_IO_DELAY */ 12457 12458void 12459ctl_datamove(union ctl_io *io) 12460{ 12461 void (*fe_datamove)(union ctl_io *io); 12462 12463 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12464 12465 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12466 12467#ifdef CTL_TIME_IO 12468 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12469 char str[256]; 12470 char path_str[64]; 12471 struct sbuf sb; 12472 12473 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12474 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12475 12476 sbuf_cat(&sb, path_str); 12477 switch (io->io_hdr.io_type) { 12478 case CTL_IO_SCSI: 12479 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12480 sbuf_printf(&sb, "\n"); 12481 sbuf_cat(&sb, path_str); 12482 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12483 io->scsiio.tag_num, io->scsiio.tag_type); 12484 break; 12485 case CTL_IO_TASK: 12486 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12487 "Tag Type: %d\n", io->taskio.task_action, 12488 io->taskio.tag_num, io->taskio.tag_type); 12489 break; 12490 default: 12491 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12492 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12493 break; 12494 } 12495 sbuf_cat(&sb, path_str); 12496 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12497 (intmax_t)time_uptime - io->io_hdr.start_time); 12498 sbuf_finish(&sb); 12499 printf("%s", sbuf_data(&sb)); 12500 } 12501#endif /* CTL_TIME_IO */ 12502 12503#ifdef CTL_IO_DELAY 12504 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12505 struct ctl_lun *lun; 12506 12507 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12508 12509 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12510 } else { 12511 struct ctl_lun *lun; 12512 12513 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12514 if ((lun != NULL) 12515 && (lun->delay_info.datamove_delay > 0)) { 12516 struct callout *callout; 12517 12518 callout = (struct callout *)&io->io_hdr.timer_bytes; 12519 callout_init(callout, /*mpsafe*/ 1); 12520 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12521 callout_reset(callout, 12522 lun->delay_info.datamove_delay * hz, 12523 ctl_datamove_timer_wakeup, io); 12524 if (lun->delay_info.datamove_type == 12525 CTL_DELAY_TYPE_ONESHOT) 12526 lun->delay_info.datamove_delay = 0; 12527 return; 12528 } 12529 } 12530#endif 12531 12532 /* 12533 * This command has been aborted. Set the port status, so we fail 12534 * the data move. 12535 */ 12536 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12537 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12538 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12539 io->io_hdr.nexus.targ_port, 12540 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12541 io->io_hdr.nexus.targ_lun); 12542 io->io_hdr.port_status = 31337; 12543 /* 12544 * Note that the backend, in this case, will get the 12545 * callback in its context. In other cases it may get 12546 * called in the frontend's interrupt thread context. 12547 */ 12548 io->scsiio.be_move_done(io); 12549 return; 12550 } 12551 12552 /* 12553 * If we're in XFER mode and this I/O is from the other shelf 12554 * controller, we need to send the DMA to the other side to 12555 * actually transfer the data to/from the host. In serialize only 12556 * mode the transfer happens below CTL and ctl_datamove() is only 12557 * called on the machine that originally received the I/O. 12558 */ 12559 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12560 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12561 union ctl_ha_msg msg; 12562 uint32_t sg_entries_sent; 12563 int do_sg_copy; 12564 int i; 12565 12566 memset(&msg, 0, sizeof(msg)); 12567 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12568 msg.hdr.original_sc = io->io_hdr.original_sc; 12569 msg.hdr.serializing_sc = io; 12570 msg.hdr.nexus = io->io_hdr.nexus; 12571 msg.dt.flags = io->io_hdr.flags; 12572 /* 12573 * We convert everything into a S/G list here. We can't 12574 * pass by reference, only by value between controllers. 12575 * So we can't pass a pointer to the S/G list, only as many 12576 * S/G entries as we can fit in here. If it's possible for 12577 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12578 * then we need to break this up into multiple transfers. 12579 */ 12580 if (io->scsiio.kern_sg_entries == 0) { 12581 msg.dt.kern_sg_entries = 1; 12582 /* 12583 * If this is in cached memory, flush the cache 12584 * before we send the DMA request to the other 12585 * controller. We want to do this in either the 12586 * read or the write case. The read case is 12587 * straightforward. In the write case, we want to 12588 * make sure nothing is in the local cache that 12589 * could overwrite the DMAed data. 12590 */ 12591 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12592 /* 12593 * XXX KDM use bus_dmamap_sync() here. 12594 */ 12595 } 12596 12597 /* 12598 * Convert to a physical address if this is a 12599 * virtual address. 12600 */ 12601 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12602 msg.dt.sg_list[0].addr = 12603 io->scsiio.kern_data_ptr; 12604 } else { 12605 /* 12606 * XXX KDM use busdma here! 12607 */ 12608#if 0 12609 msg.dt.sg_list[0].addr = (void *) 12610 vtophys(io->scsiio.kern_data_ptr); 12611#endif 12612 } 12613 12614 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12615 do_sg_copy = 0; 12616 } else { 12617 struct ctl_sg_entry *sgl; 12618 12619 do_sg_copy = 1; 12620 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12621 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12622 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12623 /* 12624 * XXX KDM use bus_dmamap_sync() here. 12625 */ 12626 } 12627 } 12628 12629 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12630 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12631 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12632 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12633 msg.dt.sg_sequence = 0; 12634 12635 /* 12636 * Loop until we've sent all of the S/G entries. On the 12637 * other end, we'll recompose these S/G entries into one 12638 * contiguous list before passing it to the 12639 */ 12640 for (sg_entries_sent = 0; sg_entries_sent < 12641 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12642 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12643 sizeof(msg.dt.sg_list[0])), 12644 msg.dt.kern_sg_entries - sg_entries_sent); 12645 12646 if (do_sg_copy != 0) { 12647 struct ctl_sg_entry *sgl; 12648 int j; 12649 12650 sgl = (struct ctl_sg_entry *) 12651 io->scsiio.kern_data_ptr; 12652 /* 12653 * If this is in cached memory, flush the cache 12654 * before we send the DMA request to the other 12655 * controller. We want to do this in either 12656 * the * read or the write case. The read 12657 * case is straightforward. In the write 12658 * case, we want to make sure nothing is 12659 * in the local cache that could overwrite 12660 * the DMAed data. 12661 */ 12662 12663 for (i = sg_entries_sent, j = 0; 12664 i < msg.dt.cur_sg_entries; i++, j++) { 12665 if ((io->io_hdr.flags & 12666 CTL_FLAG_NO_DATASYNC) == 0) { 12667 /* 12668 * XXX KDM use bus_dmamap_sync() 12669 */ 12670 } 12671 if ((io->io_hdr.flags & 12672 CTL_FLAG_BUS_ADDR) == 0) { 12673 /* 12674 * XXX KDM use busdma. 12675 */ 12676#if 0 12677 msg.dt.sg_list[j].addr =(void *) 12678 vtophys(sgl[i].addr); 12679#endif 12680 } else { 12681 msg.dt.sg_list[j].addr = 12682 sgl[i].addr; 12683 } 12684 msg.dt.sg_list[j].len = sgl[i].len; 12685 } 12686 } 12687 12688 sg_entries_sent += msg.dt.cur_sg_entries; 12689 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12690 msg.dt.sg_last = 1; 12691 else 12692 msg.dt.sg_last = 0; 12693 12694 /* 12695 * XXX KDM drop and reacquire the lock here? 12696 */ 12697 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12698 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12699 /* 12700 * XXX do something here. 12701 */ 12702 } 12703 12704 msg.dt.sent_sg_entries = sg_entries_sent; 12705 } 12706 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12707 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12708 ctl_failover_io(io, /*have_lock*/ 0); 12709 12710 } else { 12711 12712 /* 12713 * Lookup the fe_datamove() function for this particular 12714 * front end. 12715 */ 12716 fe_datamove = 12717 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12718 12719 fe_datamove(io); 12720 } 12721} 12722 12723static void 12724ctl_send_datamove_done(union ctl_io *io, int have_lock) 12725{ 12726 union ctl_ha_msg msg; 12727 int isc_status; 12728 12729 memset(&msg, 0, sizeof(msg)); 12730 12731 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12732 msg.hdr.original_sc = io; 12733 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12734 msg.hdr.nexus = io->io_hdr.nexus; 12735 msg.hdr.status = io->io_hdr.status; 12736 msg.scsi.tag_num = io->scsiio.tag_num; 12737 msg.scsi.tag_type = io->scsiio.tag_type; 12738 msg.scsi.scsi_status = io->scsiio.scsi_status; 12739 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12740 sizeof(io->scsiio.sense_data)); 12741 msg.scsi.sense_len = io->scsiio.sense_len; 12742 msg.scsi.sense_residual = io->scsiio.sense_residual; 12743 msg.scsi.fetd_status = io->io_hdr.port_status; 12744 msg.scsi.residual = io->scsiio.residual; 12745 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12746 12747 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12748 ctl_failover_io(io, /*have_lock*/ have_lock); 12749 return; 12750 } 12751 12752 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12753 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12754 /* XXX do something if this fails */ 12755 } 12756 12757} 12758 12759/* 12760 * The DMA to the remote side is done, now we need to tell the other side 12761 * we're done so it can continue with its data movement. 12762 */ 12763static void 12764ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12765{ 12766 union ctl_io *io; 12767 12768 io = rq->context; 12769 12770 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12771 printf("%s: ISC DMA write failed with error %d", __func__, 12772 rq->ret); 12773 ctl_set_internal_failure(&io->scsiio, 12774 /*sks_valid*/ 1, 12775 /*retry_count*/ rq->ret); 12776 } 12777 12778 ctl_dt_req_free(rq); 12779 12780 /* 12781 * In this case, we had to malloc the memory locally. Free it. 12782 */ 12783 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12784 int i; 12785 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12786 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12787 } 12788 /* 12789 * The data is in local and remote memory, so now we need to send 12790 * status (good or back) back to the other side. 12791 */ 12792 ctl_send_datamove_done(io, /*have_lock*/ 0); 12793} 12794 12795/* 12796 * We've moved the data from the host/controller into local memory. Now we 12797 * need to push it over to the remote controller's memory. 12798 */ 12799static int 12800ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12801{ 12802 int retval; 12803 12804 retval = 0; 12805 12806 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12807 ctl_datamove_remote_write_cb); 12808 12809 return (retval); 12810} 12811 12812static void 12813ctl_datamove_remote_write(union ctl_io *io) 12814{ 12815 int retval; 12816 void (*fe_datamove)(union ctl_io *io); 12817 12818 /* 12819 * - Get the data from the host/HBA into local memory. 12820 * - DMA memory from the local controller to the remote controller. 12821 * - Send status back to the remote controller. 12822 */ 12823 12824 retval = ctl_datamove_remote_sgl_setup(io); 12825 if (retval != 0) 12826 return; 12827 12828 /* Switch the pointer over so the FETD knows what to do */ 12829 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12830 12831 /* 12832 * Use a custom move done callback, since we need to send completion 12833 * back to the other controller, not to the backend on this side. 12834 */ 12835 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12836 12837 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12838 12839 fe_datamove(io); 12840 12841 return; 12842 12843} 12844 12845static int 12846ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12847{ 12848#if 0 12849 char str[256]; 12850 char path_str[64]; 12851 struct sbuf sb; 12852#endif 12853 12854 /* 12855 * In this case, we had to malloc the memory locally. Free it. 12856 */ 12857 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12858 int i; 12859 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12860 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12861 } 12862 12863#if 0 12864 scsi_path_string(io, path_str, sizeof(path_str)); 12865 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12866 sbuf_cat(&sb, path_str); 12867 scsi_command_string(&io->scsiio, NULL, &sb); 12868 sbuf_printf(&sb, "\n"); 12869 sbuf_cat(&sb, path_str); 12870 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12871 io->scsiio.tag_num, io->scsiio.tag_type); 12872 sbuf_cat(&sb, path_str); 12873 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12874 io->io_hdr.flags, io->io_hdr.status); 12875 sbuf_finish(&sb); 12876 printk("%s", sbuf_data(&sb)); 12877#endif 12878 12879 12880 /* 12881 * The read is done, now we need to send status (good or bad) back 12882 * to the other side. 12883 */ 12884 ctl_send_datamove_done(io, /*have_lock*/ 0); 12885 12886 return (0); 12887} 12888 12889static void 12890ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12891{ 12892 union ctl_io *io; 12893 void (*fe_datamove)(union ctl_io *io); 12894 12895 io = rq->context; 12896 12897 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12898 printf("%s: ISC DMA read failed with error %d", __func__, 12899 rq->ret); 12900 ctl_set_internal_failure(&io->scsiio, 12901 /*sks_valid*/ 1, 12902 /*retry_count*/ rq->ret); 12903 } 12904 12905 ctl_dt_req_free(rq); 12906 12907 /* Switch the pointer over so the FETD knows what to do */ 12908 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12909 12910 /* 12911 * Use a custom move done callback, since we need to send completion 12912 * back to the other controller, not to the backend on this side. 12913 */ 12914 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12915 12916 /* XXX KDM add checks like the ones in ctl_datamove? */ 12917 12918 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12919 12920 fe_datamove(io); 12921} 12922 12923static int 12924ctl_datamove_remote_sgl_setup(union ctl_io *io) 12925{ 12926 struct ctl_sg_entry *local_sglist, *remote_sglist; 12927 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12928 struct ctl_softc *softc; 12929 int retval; 12930 int i; 12931 12932 retval = 0; 12933 softc = control_softc; 12934 12935 local_sglist = io->io_hdr.local_sglist; 12936 local_dma_sglist = io->io_hdr.local_dma_sglist; 12937 remote_sglist = io->io_hdr.remote_sglist; 12938 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12939 12940 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12941 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12942 local_sglist[i].len = remote_sglist[i].len; 12943 12944 /* 12945 * XXX Detect the situation where the RS-level I/O 12946 * redirector on the other side has already read the 12947 * data off of the AOR RS on this side, and 12948 * transferred it to remote (mirror) memory on the 12949 * other side. Since we already have the data in 12950 * memory here, we just need to use it. 12951 * 12952 * XXX KDM this can probably be removed once we 12953 * get the cache device code in and take the 12954 * current AOR implementation out. 12955 */ 12956#ifdef NEEDTOPORT 12957 if ((remote_sglist[i].addr >= 12958 (void *)vtophys(softc->mirr->addr)) 12959 && (remote_sglist[i].addr < 12960 ((void *)vtophys(softc->mirr->addr) + 12961 CacheMirrorOffset))) { 12962 local_sglist[i].addr = remote_sglist[i].addr - 12963 CacheMirrorOffset; 12964 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12965 CTL_FLAG_DATA_IN) 12966 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12967 } else { 12968 local_sglist[i].addr = remote_sglist[i].addr + 12969 CacheMirrorOffset; 12970 } 12971#endif 12972#if 0 12973 printf("%s: local %p, remote %p, len %d\n", 12974 __func__, local_sglist[i].addr, 12975 remote_sglist[i].addr, local_sglist[i].len); 12976#endif 12977 } 12978 } else { 12979 uint32_t len_to_go; 12980 12981 /* 12982 * In this case, we don't have automatically allocated 12983 * memory for this I/O on this controller. This typically 12984 * happens with internal CTL I/O -- e.g. inquiry, mode 12985 * sense, etc. Anything coming from RAIDCore will have 12986 * a mirror area available. 12987 */ 12988 len_to_go = io->scsiio.kern_data_len; 12989 12990 /* 12991 * Clear the no datasync flag, we have to use malloced 12992 * buffers. 12993 */ 12994 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12995 12996 /* 12997 * The difficult thing here is that the size of the various 12998 * S/G segments may be different than the size from the 12999 * remote controller. That'll make it harder when DMAing 13000 * the data back to the other side. 13001 */ 13002 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13003 sizeof(io->io_hdr.remote_sglist[0])) && 13004 (len_to_go > 0); i++) { 13005 local_sglist[i].len = ctl_min(len_to_go, 131072); 13006 CTL_SIZE_8B(local_dma_sglist[i].len, 13007 local_sglist[i].len); 13008 local_sglist[i].addr = 13009 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13010 13011 local_dma_sglist[i].addr = local_sglist[i].addr; 13012 13013 if (local_sglist[i].addr == NULL) { 13014 int j; 13015 13016 printf("malloc failed for %zd bytes!", 13017 local_dma_sglist[i].len); 13018 for (j = 0; j < i; j++) { 13019 free(local_sglist[j].addr, M_CTL); 13020 } 13021 ctl_set_internal_failure(&io->scsiio, 13022 /*sks_valid*/ 1, 13023 /*retry_count*/ 4857); 13024 retval = 1; 13025 goto bailout_error; 13026 13027 } 13028 /* XXX KDM do we need a sync here? */ 13029 13030 len_to_go -= local_sglist[i].len; 13031 } 13032 /* 13033 * Reset the number of S/G entries accordingly. The 13034 * original number of S/G entries is available in 13035 * rem_sg_entries. 13036 */ 13037 io->scsiio.kern_sg_entries = i; 13038 13039#if 0 13040 printf("%s: kern_sg_entries = %d\n", __func__, 13041 io->scsiio.kern_sg_entries); 13042 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13043 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13044 local_sglist[i].addr, local_sglist[i].len, 13045 local_dma_sglist[i].len); 13046#endif 13047 } 13048 13049 13050 return (retval); 13051 13052bailout_error: 13053 13054 ctl_send_datamove_done(io, /*have_lock*/ 0); 13055 13056 return (retval); 13057} 13058 13059static int 13060ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13061 ctl_ha_dt_cb callback) 13062{ 13063 struct ctl_ha_dt_req *rq; 13064 struct ctl_sg_entry *remote_sglist, *local_sglist; 13065 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13066 uint32_t local_used, remote_used, total_used; 13067 int retval; 13068 int i, j; 13069 13070 retval = 0; 13071 13072 rq = ctl_dt_req_alloc(); 13073 13074 /* 13075 * If we failed to allocate the request, and if the DMA didn't fail 13076 * anyway, set busy status. This is just a resource allocation 13077 * failure. 13078 */ 13079 if ((rq == NULL) 13080 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13081 ctl_set_busy(&io->scsiio); 13082 13083 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13084 13085 if (rq != NULL) 13086 ctl_dt_req_free(rq); 13087 13088 /* 13089 * The data move failed. We need to return status back 13090 * to the other controller. No point in trying to DMA 13091 * data to the remote controller. 13092 */ 13093 13094 ctl_send_datamove_done(io, /*have_lock*/ 0); 13095 13096 retval = 1; 13097 13098 goto bailout; 13099 } 13100 13101 local_sglist = io->io_hdr.local_sglist; 13102 local_dma_sglist = io->io_hdr.local_dma_sglist; 13103 remote_sglist = io->io_hdr.remote_sglist; 13104 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13105 local_used = 0; 13106 remote_used = 0; 13107 total_used = 0; 13108 13109 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13110 rq->ret = CTL_HA_STATUS_SUCCESS; 13111 rq->context = io; 13112 callback(rq); 13113 goto bailout; 13114 } 13115 13116 /* 13117 * Pull/push the data over the wire from/to the other controller. 13118 * This takes into account the possibility that the local and 13119 * remote sglists may not be identical in terms of the size of 13120 * the elements and the number of elements. 13121 * 13122 * One fundamental assumption here is that the length allocated for 13123 * both the local and remote sglists is identical. Otherwise, we've 13124 * essentially got a coding error of some sort. 13125 */ 13126 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13127 int isc_ret; 13128 uint32_t cur_len, dma_length; 13129 uint8_t *tmp_ptr; 13130 13131 rq->id = CTL_HA_DATA_CTL; 13132 rq->command = command; 13133 rq->context = io; 13134 13135 /* 13136 * Both pointers should be aligned. But it is possible 13137 * that the allocation length is not. They should both 13138 * also have enough slack left over at the end, though, 13139 * to round up to the next 8 byte boundary. 13140 */ 13141 cur_len = ctl_min(local_sglist[i].len - local_used, 13142 remote_sglist[j].len - remote_used); 13143 13144 /* 13145 * In this case, we have a size issue and need to decrease 13146 * the size, except in the case where we actually have less 13147 * than 8 bytes left. In that case, we need to increase 13148 * the DMA length to get the last bit. 13149 */ 13150 if ((cur_len & 0x7) != 0) { 13151 if (cur_len > 0x7) { 13152 cur_len = cur_len - (cur_len & 0x7); 13153 dma_length = cur_len; 13154 } else { 13155 CTL_SIZE_8B(dma_length, cur_len); 13156 } 13157 13158 } else 13159 dma_length = cur_len; 13160 13161 /* 13162 * If we had to allocate memory for this I/O, instead of using 13163 * the non-cached mirror memory, we'll need to flush the cache 13164 * before trying to DMA to the other controller. 13165 * 13166 * We could end up doing this multiple times for the same 13167 * segment if we have a larger local segment than remote 13168 * segment. That shouldn't be an issue. 13169 */ 13170 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13171 /* 13172 * XXX KDM use bus_dmamap_sync() here. 13173 */ 13174 } 13175 13176 rq->size = dma_length; 13177 13178 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13179 tmp_ptr += local_used; 13180 13181 /* Use physical addresses when talking to ISC hardware */ 13182 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13183 /* XXX KDM use busdma */ 13184#if 0 13185 rq->local = vtophys(tmp_ptr); 13186#endif 13187 } else 13188 rq->local = tmp_ptr; 13189 13190 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13191 tmp_ptr += remote_used; 13192 rq->remote = tmp_ptr; 13193 13194 rq->callback = NULL; 13195 13196 local_used += cur_len; 13197 if (local_used >= local_sglist[i].len) { 13198 i++; 13199 local_used = 0; 13200 } 13201 13202 remote_used += cur_len; 13203 if (remote_used >= remote_sglist[j].len) { 13204 j++; 13205 remote_used = 0; 13206 } 13207 total_used += cur_len; 13208 13209 if (total_used >= io->scsiio.kern_data_len) 13210 rq->callback = callback; 13211 13212 if ((rq->size & 0x7) != 0) { 13213 printf("%s: warning: size %d is not on 8b boundary\n", 13214 __func__, rq->size); 13215 } 13216 if (((uintptr_t)rq->local & 0x7) != 0) { 13217 printf("%s: warning: local %p not on 8b boundary\n", 13218 __func__, rq->local); 13219 } 13220 if (((uintptr_t)rq->remote & 0x7) != 0) { 13221 printf("%s: warning: remote %p not on 8b boundary\n", 13222 __func__, rq->local); 13223 } 13224#if 0 13225 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13226 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13227 rq->local, rq->remote, rq->size); 13228#endif 13229 13230 isc_ret = ctl_dt_single(rq); 13231 if (isc_ret == CTL_HA_STATUS_WAIT) 13232 continue; 13233 13234 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13235 rq->ret = CTL_HA_STATUS_SUCCESS; 13236 } else { 13237 rq->ret = isc_ret; 13238 } 13239 callback(rq); 13240 goto bailout; 13241 } 13242 13243bailout: 13244 return (retval); 13245 13246} 13247 13248static void 13249ctl_datamove_remote_read(union ctl_io *io) 13250{ 13251 int retval; 13252 int i; 13253 13254 /* 13255 * This will send an error to the other controller in the case of a 13256 * failure. 13257 */ 13258 retval = ctl_datamove_remote_sgl_setup(io); 13259 if (retval != 0) 13260 return; 13261 13262 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13263 ctl_datamove_remote_read_cb); 13264 if ((retval != 0) 13265 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13266 /* 13267 * Make sure we free memory if there was an error.. The 13268 * ctl_datamove_remote_xfer() function will send the 13269 * datamove done message, or call the callback with an 13270 * error if there is a problem. 13271 */ 13272 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13273 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13274 } 13275 13276 return; 13277} 13278 13279/* 13280 * Process a datamove request from the other controller. This is used for 13281 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13282 * first. Once that is complete, the data gets DMAed into the remote 13283 * controller's memory. For reads, we DMA from the remote controller's 13284 * memory into our memory first, and then move it out to the FETD. 13285 */ 13286static void 13287ctl_datamove_remote(union ctl_io *io) 13288{ 13289 struct ctl_softc *softc; 13290 13291 softc = control_softc; 13292 13293 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13294 13295 /* 13296 * Note that we look for an aborted I/O here, but don't do some of 13297 * the other checks that ctl_datamove() normally does. 13298 * We don't need to run the datamove delay code, since that should 13299 * have been done if need be on the other controller. 13300 */ 13301 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13302 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13303 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13304 io->io_hdr.nexus.targ_port, 13305 io->io_hdr.nexus.targ_target.id, 13306 io->io_hdr.nexus.targ_lun); 13307 io->io_hdr.port_status = 31338; 13308 ctl_send_datamove_done(io, /*have_lock*/ 0); 13309 return; 13310 } 13311 13312 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13313 ctl_datamove_remote_write(io); 13314 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13315 ctl_datamove_remote_read(io); 13316 } else { 13317 union ctl_ha_msg msg; 13318 struct scsi_sense_data *sense; 13319 uint8_t sks[3]; 13320 int retry_count; 13321 13322 memset(&msg, 0, sizeof(msg)); 13323 13324 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13325 msg.hdr.status = CTL_SCSI_ERROR; 13326 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13327 13328 retry_count = 4243; 13329 13330 sense = &msg.scsi.sense_data; 13331 sks[0] = SSD_SCS_VALID; 13332 sks[1] = (retry_count >> 8) & 0xff; 13333 sks[2] = retry_count & 0xff; 13334 13335 /* "Internal target failure" */ 13336 scsi_set_sense_data(sense, 13337 /*sense_format*/ SSD_TYPE_NONE, 13338 /*current_error*/ 1, 13339 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13340 /*asc*/ 0x44, 13341 /*ascq*/ 0x00, 13342 /*type*/ SSD_ELEM_SKS, 13343 /*size*/ sizeof(sks), 13344 /*data*/ sks, 13345 SSD_ELEM_NONE); 13346 13347 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13348 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13349 ctl_failover_io(io, /*have_lock*/ 1); 13350 return; 13351 } 13352 13353 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13354 CTL_HA_STATUS_SUCCESS) { 13355 /* XXX KDM what to do if this fails? */ 13356 } 13357 return; 13358 } 13359 13360} 13361 13362static int 13363ctl_process_done(union ctl_io *io) 13364{ 13365 struct ctl_lun *lun; 13366 struct ctl_softc *ctl_softc; 13367 void (*fe_done)(union ctl_io *io); 13368 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13369 13370 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13371 13372 fe_done = 13373 control_softc->ctl_ports[targ_port]->fe_done; 13374 13375#ifdef CTL_TIME_IO 13376 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13377 char str[256]; 13378 char path_str[64]; 13379 struct sbuf sb; 13380 13381 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13382 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13383 13384 sbuf_cat(&sb, path_str); 13385 switch (io->io_hdr.io_type) { 13386 case CTL_IO_SCSI: 13387 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13388 sbuf_printf(&sb, "\n"); 13389 sbuf_cat(&sb, path_str); 13390 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13391 io->scsiio.tag_num, io->scsiio.tag_type); 13392 break; 13393 case CTL_IO_TASK: 13394 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13395 "Tag Type: %d\n", io->taskio.task_action, 13396 io->taskio.tag_num, io->taskio.tag_type); 13397 break; 13398 default: 13399 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13400 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13401 break; 13402 } 13403 sbuf_cat(&sb, path_str); 13404 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13405 (intmax_t)time_uptime - io->io_hdr.start_time); 13406 sbuf_finish(&sb); 13407 printf("%s", sbuf_data(&sb)); 13408 } 13409#endif /* CTL_TIME_IO */ 13410 13411 switch (io->io_hdr.io_type) { 13412 case CTL_IO_SCSI: 13413 break; 13414 case CTL_IO_TASK: 13415 if (bootverbose || verbose > 0) 13416 ctl_io_error_print(io, NULL); 13417 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13418 ctl_free_io(io); 13419 else 13420 fe_done(io); 13421 return (CTL_RETVAL_COMPLETE); 13422 break; 13423 default: 13424 printf("ctl_process_done: invalid io type %d\n", 13425 io->io_hdr.io_type); 13426 panic("ctl_process_done: invalid io type %d\n", 13427 io->io_hdr.io_type); 13428 break; /* NOTREACHED */ 13429 } 13430 13431 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13432 if (lun == NULL) { 13433 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13434 io->io_hdr.nexus.targ_mapped_lun)); 13435 fe_done(io); 13436 goto bailout; 13437 } 13438 ctl_softc = lun->ctl_softc; 13439 13440 mtx_lock(&lun->lun_lock); 13441 13442 /* 13443 * Check to see if we have any errors to inject here. We only 13444 * inject errors for commands that don't already have errors set. 13445 */ 13446 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13447 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13448 ctl_inject_error(lun, io); 13449 13450 /* 13451 * XXX KDM how do we treat commands that aren't completed 13452 * successfully? 13453 * 13454 * XXX KDM should we also track I/O latency? 13455 */ 13456 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13457 io->io_hdr.io_type == CTL_IO_SCSI) { 13458#ifdef CTL_TIME_IO 13459 struct bintime cur_bt; 13460#endif 13461 int type; 13462 13463 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13464 CTL_FLAG_DATA_IN) 13465 type = CTL_STATS_READ; 13466 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13467 CTL_FLAG_DATA_OUT) 13468 type = CTL_STATS_WRITE; 13469 else 13470 type = CTL_STATS_NO_IO; 13471 13472 lun->stats.ports[targ_port].bytes[type] += 13473 io->scsiio.kern_total_len; 13474 lun->stats.ports[targ_port].operations[type]++; 13475#ifdef CTL_TIME_IO 13476 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13477 &io->io_hdr.dma_bt); 13478 lun->stats.ports[targ_port].num_dmas[type] += 13479 io->io_hdr.num_dmas; 13480 getbintime(&cur_bt); 13481 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13482 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13483#endif 13484 } 13485 13486 /* 13487 * Remove this from the OOA queue. 13488 */ 13489 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13490 13491 /* 13492 * Run through the blocked queue on this LUN and see if anything 13493 * has become unblocked, now that this transaction is done. 13494 */ 13495 ctl_check_blocked(lun); 13496 13497 /* 13498 * If the LUN has been invalidated, free it if there is nothing 13499 * left on its OOA queue. 13500 */ 13501 if ((lun->flags & CTL_LUN_INVALID) 13502 && TAILQ_EMPTY(&lun->ooa_queue)) { 13503 mtx_unlock(&lun->lun_lock); 13504 mtx_lock(&ctl_softc->ctl_lock); 13505 ctl_free_lun(lun); 13506 mtx_unlock(&ctl_softc->ctl_lock); 13507 } else 13508 mtx_unlock(&lun->lun_lock); 13509 13510 /* 13511 * If this command has been aborted, make sure we set the status 13512 * properly. The FETD is responsible for freeing the I/O and doing 13513 * whatever it needs to do to clean up its state. 13514 */ 13515 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13516 ctl_set_task_aborted(&io->scsiio); 13517 13518 /* 13519 * We print out status for every task management command. For SCSI 13520 * commands, we filter out any unit attention errors; they happen 13521 * on every boot, and would clutter up the log. Note: task 13522 * management commands aren't printed here, they are printed above, 13523 * since they should never even make it down here. 13524 */ 13525 switch (io->io_hdr.io_type) { 13526 case CTL_IO_SCSI: { 13527 int error_code, sense_key, asc, ascq; 13528 13529 sense_key = 0; 13530 13531 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13532 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13533 /* 13534 * Since this is just for printing, no need to 13535 * show errors here. 13536 */ 13537 scsi_extract_sense_len(&io->scsiio.sense_data, 13538 io->scsiio.sense_len, 13539 &error_code, 13540 &sense_key, 13541 &asc, 13542 &ascq, 13543 /*show_errors*/ 0); 13544 } 13545 13546 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13547 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13548 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13549 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13550 13551 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13552 ctl_softc->skipped_prints++; 13553 } else { 13554 uint32_t skipped_prints; 13555 13556 skipped_prints = ctl_softc->skipped_prints; 13557 13558 ctl_softc->skipped_prints = 0; 13559 ctl_softc->last_print_jiffies = time_uptime; 13560 13561 if (skipped_prints > 0) { 13562#ifdef NEEDTOPORT 13563 csevent_log(CSC_CTL | CSC_SHELF_SW | 13564 CTL_ERROR_REPORT, 13565 csevent_LogType_Trace, 13566 csevent_Severity_Information, 13567 csevent_AlertLevel_Green, 13568 csevent_FRU_Firmware, 13569 csevent_FRU_Unknown, 13570 "High CTL error volume, %d prints " 13571 "skipped", skipped_prints); 13572#endif 13573 } 13574 if (bootverbose || verbose > 0) 13575 ctl_io_error_print(io, NULL); 13576 } 13577 } 13578 break; 13579 } 13580 case CTL_IO_TASK: 13581 if (bootverbose || verbose > 0) 13582 ctl_io_error_print(io, NULL); 13583 break; 13584 default: 13585 break; 13586 } 13587 13588 /* 13589 * Tell the FETD or the other shelf controller we're done with this 13590 * command. Note that only SCSI commands get to this point. Task 13591 * management commands are completed above. 13592 * 13593 * We only send status to the other controller if we're in XFER 13594 * mode. In SER_ONLY mode, the I/O is done on the controller that 13595 * received the I/O (from CTL's perspective), and so the status is 13596 * generated there. 13597 * 13598 * XXX KDM if we hold the lock here, we could cause a deadlock 13599 * if the frontend comes back in in this context to queue 13600 * something. 13601 */ 13602 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13603 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13604 union ctl_ha_msg msg; 13605 13606 memset(&msg, 0, sizeof(msg)); 13607 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13608 msg.hdr.original_sc = io->io_hdr.original_sc; 13609 msg.hdr.nexus = io->io_hdr.nexus; 13610 msg.hdr.status = io->io_hdr.status; 13611 msg.scsi.scsi_status = io->scsiio.scsi_status; 13612 msg.scsi.tag_num = io->scsiio.tag_num; 13613 msg.scsi.tag_type = io->scsiio.tag_type; 13614 msg.scsi.sense_len = io->scsiio.sense_len; 13615 msg.scsi.sense_residual = io->scsiio.sense_residual; 13616 msg.scsi.residual = io->scsiio.residual; 13617 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13618 sizeof(io->scsiio.sense_data)); 13619 /* 13620 * We copy this whether or not this is an I/O-related 13621 * command. Otherwise, we'd have to go and check to see 13622 * whether it's a read/write command, and it really isn't 13623 * worth it. 13624 */ 13625 memcpy(&msg.scsi.lbalen, 13626 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13627 sizeof(msg.scsi.lbalen)); 13628 13629 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13630 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13631 /* XXX do something here */ 13632 } 13633 13634 ctl_free_io(io); 13635 } else 13636 fe_done(io); 13637 13638bailout: 13639 13640 return (CTL_RETVAL_COMPLETE); 13641} 13642 13643#ifdef CTL_WITH_CA 13644/* 13645 * Front end should call this if it doesn't do autosense. When the request 13646 * sense comes back in from the initiator, we'll dequeue this and send it. 13647 */ 13648int 13649ctl_queue_sense(union ctl_io *io) 13650{ 13651 struct ctl_lun *lun; 13652 struct ctl_softc *ctl_softc; 13653 uint32_t initidx, targ_lun; 13654 13655 ctl_softc = control_softc; 13656 13657 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13658 13659 /* 13660 * LUN lookup will likely move to the ctl_work_thread() once we 13661 * have our new queueing infrastructure (that doesn't put things on 13662 * a per-LUN queue initially). That is so that we can handle 13663 * things like an INQUIRY to a LUN that we don't have enabled. We 13664 * can't deal with that right now. 13665 */ 13666 mtx_lock(&ctl_softc->ctl_lock); 13667 13668 /* 13669 * If we don't have a LUN for this, just toss the sense 13670 * information. 13671 */ 13672 targ_lun = io->io_hdr.nexus.targ_lun; 13673 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13674 if ((targ_lun < CTL_MAX_LUNS) 13675 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13676 lun = ctl_softc->ctl_luns[targ_lun]; 13677 else 13678 goto bailout; 13679 13680 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13681 13682 mtx_lock(&lun->lun_lock); 13683 /* 13684 * Already have CA set for this LUN...toss the sense information. 13685 */ 13686 if (ctl_is_set(lun->have_ca, initidx)) { 13687 mtx_unlock(&lun->lun_lock); 13688 goto bailout; 13689 } 13690 13691 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13692 ctl_min(sizeof(lun->pending_sense[initidx]), 13693 sizeof(io->scsiio.sense_data))); 13694 ctl_set_mask(lun->have_ca, initidx); 13695 mtx_unlock(&lun->lun_lock); 13696 13697bailout: 13698 mtx_unlock(&ctl_softc->ctl_lock); 13699 13700 ctl_free_io(io); 13701 13702 return (CTL_RETVAL_COMPLETE); 13703} 13704#endif 13705 13706/* 13707 * Primary command inlet from frontend ports. All SCSI and task I/O 13708 * requests must go through this function. 13709 */ 13710int 13711ctl_queue(union ctl_io *io) 13712{ 13713 struct ctl_softc *ctl_softc; 13714 13715 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13716 13717 ctl_softc = control_softc; 13718 13719#ifdef CTL_TIME_IO 13720 io->io_hdr.start_time = time_uptime; 13721 getbintime(&io->io_hdr.start_bt); 13722#endif /* CTL_TIME_IO */ 13723 13724 /* Map FE-specific LUN ID into global one. */ 13725 io->io_hdr.nexus.targ_mapped_lun = 13726 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13727 13728 switch (io->io_hdr.io_type) { 13729 case CTL_IO_SCSI: 13730 case CTL_IO_TASK: 13731 ctl_enqueue_incoming(io); 13732 break; 13733 default: 13734 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13735 return (EINVAL); 13736 } 13737 13738 return (CTL_RETVAL_COMPLETE); 13739} 13740 13741#ifdef CTL_IO_DELAY 13742static void 13743ctl_done_timer_wakeup(void *arg) 13744{ 13745 union ctl_io *io; 13746 13747 io = (union ctl_io *)arg; 13748 ctl_done(io); 13749} 13750#endif /* CTL_IO_DELAY */ 13751 13752void 13753ctl_done(union ctl_io *io) 13754{ 13755 struct ctl_softc *ctl_softc; 13756 13757 ctl_softc = control_softc; 13758 13759 /* 13760 * Enable this to catch duplicate completion issues. 13761 */ 13762#if 0 13763 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13764 printf("%s: type %d msg %d cdb %x iptl: " 13765 "%d:%d:%d:%d tag 0x%04x " 13766 "flag %#x status %x\n", 13767 __func__, 13768 io->io_hdr.io_type, 13769 io->io_hdr.msg_type, 13770 io->scsiio.cdb[0], 13771 io->io_hdr.nexus.initid.id, 13772 io->io_hdr.nexus.targ_port, 13773 io->io_hdr.nexus.targ_target.id, 13774 io->io_hdr.nexus.targ_lun, 13775 (io->io_hdr.io_type == 13776 CTL_IO_TASK) ? 13777 io->taskio.tag_num : 13778 io->scsiio.tag_num, 13779 io->io_hdr.flags, 13780 io->io_hdr.status); 13781 } else 13782 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13783#endif 13784 13785 /* 13786 * This is an internal copy of an I/O, and should not go through 13787 * the normal done processing logic. 13788 */ 13789 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13790 return; 13791 13792 /* 13793 * We need to send a msg to the serializing shelf to finish the IO 13794 * as well. We don't send a finish message to the other shelf if 13795 * this is a task management command. Task management commands 13796 * aren't serialized in the OOA queue, but rather just executed on 13797 * both shelf controllers for commands that originated on that 13798 * controller. 13799 */ 13800 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13801 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13802 union ctl_ha_msg msg_io; 13803 13804 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13805 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13806 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13807 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13808 } 13809 /* continue on to finish IO */ 13810 } 13811#ifdef CTL_IO_DELAY 13812 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13813 struct ctl_lun *lun; 13814 13815 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13816 13817 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13818 } else { 13819 struct ctl_lun *lun; 13820 13821 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13822 13823 if ((lun != NULL) 13824 && (lun->delay_info.done_delay > 0)) { 13825 struct callout *callout; 13826 13827 callout = (struct callout *)&io->io_hdr.timer_bytes; 13828 callout_init(callout, /*mpsafe*/ 1); 13829 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13830 callout_reset(callout, 13831 lun->delay_info.done_delay * hz, 13832 ctl_done_timer_wakeup, io); 13833 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13834 lun->delay_info.done_delay = 0; 13835 return; 13836 } 13837 } 13838#endif /* CTL_IO_DELAY */ 13839 13840 ctl_enqueue_done(io); 13841} 13842 13843int 13844ctl_isc(struct ctl_scsiio *ctsio) 13845{ 13846 struct ctl_lun *lun; 13847 int retval; 13848 13849 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13850 13851 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13852 13853 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13854 13855 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13856 13857 return (retval); 13858} 13859 13860 13861static void 13862ctl_work_thread(void *arg) 13863{ 13864 struct ctl_thread *thr = (struct ctl_thread *)arg; 13865 struct ctl_softc *softc = thr->ctl_softc; 13866 union ctl_io *io; 13867 int retval; 13868 13869 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13870 13871 for (;;) { 13872 retval = 0; 13873 13874 /* 13875 * We handle the queues in this order: 13876 * - ISC 13877 * - done queue (to free up resources, unblock other commands) 13878 * - RtR queue 13879 * - incoming queue 13880 * 13881 * If those queues are empty, we break out of the loop and 13882 * go to sleep. 13883 */ 13884 mtx_lock(&thr->queue_lock); 13885 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13886 if (io != NULL) { 13887 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13888 mtx_unlock(&thr->queue_lock); 13889 ctl_handle_isc(io); 13890 continue; 13891 } 13892 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13893 if (io != NULL) { 13894 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13895 /* clear any blocked commands, call fe_done */ 13896 mtx_unlock(&thr->queue_lock); 13897 retval = ctl_process_done(io); 13898 continue; 13899 } 13900 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13901 if (io != NULL) { 13902 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13903 mtx_unlock(&thr->queue_lock); 13904 if (io->io_hdr.io_type == CTL_IO_TASK) 13905 ctl_run_task(io); 13906 else 13907 ctl_scsiio_precheck(softc, &io->scsiio); 13908 continue; 13909 } 13910 if (!ctl_pause_rtr) { 13911 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13912 if (io != NULL) { 13913 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13914 mtx_unlock(&thr->queue_lock); 13915 retval = ctl_scsiio(&io->scsiio); 13916 if (retval != CTL_RETVAL_COMPLETE) 13917 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13918 continue; 13919 } 13920 } 13921 13922 /* Sleep until we have something to do. */ 13923 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13924 } 13925} 13926 13927static void 13928ctl_lun_thread(void *arg) 13929{ 13930 struct ctl_softc *softc = (struct ctl_softc *)arg; 13931 struct ctl_be_lun *be_lun; 13932 int retval; 13933 13934 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13935 13936 for (;;) { 13937 retval = 0; 13938 mtx_lock(&softc->ctl_lock); 13939 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13940 if (be_lun != NULL) { 13941 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13942 mtx_unlock(&softc->ctl_lock); 13943 ctl_create_lun(be_lun); 13944 continue; 13945 } 13946 13947 /* Sleep until we have something to do. */ 13948 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13949 PDROP | PRIBIO, "-", 0); 13950 } 13951} 13952 13953static void 13954ctl_enqueue_incoming(union ctl_io *io) 13955{ 13956 struct ctl_softc *softc = control_softc; 13957 struct ctl_thread *thr; 13958 u_int idx; 13959 13960 idx = (io->io_hdr.nexus.targ_port * 127 + 13961 io->io_hdr.nexus.initid.id) % worker_threads; 13962 thr = &softc->threads[idx]; 13963 mtx_lock(&thr->queue_lock); 13964 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13965 mtx_unlock(&thr->queue_lock); 13966 wakeup(thr); 13967} 13968 13969static void 13970ctl_enqueue_rtr(union ctl_io *io) 13971{ 13972 struct ctl_softc *softc = control_softc; 13973 struct ctl_thread *thr; 13974 13975 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13976 mtx_lock(&thr->queue_lock); 13977 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13978 mtx_unlock(&thr->queue_lock); 13979 wakeup(thr); 13980} 13981 13982static void 13983ctl_enqueue_done(union ctl_io *io) 13984{ 13985 struct ctl_softc *softc = control_softc; 13986 struct ctl_thread *thr; 13987 13988 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13989 mtx_lock(&thr->queue_lock); 13990 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13991 mtx_unlock(&thr->queue_lock); 13992 wakeup(thr); 13993} 13994 13995static void 13996ctl_enqueue_isc(union ctl_io *io) 13997{ 13998 struct ctl_softc *softc = control_softc; 13999 struct ctl_thread *thr; 14000 14001 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14002 mtx_lock(&thr->queue_lock); 14003 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14004 mtx_unlock(&thr->queue_lock); 14005 wakeup(thr); 14006} 14007 14008/* Initialization and failover */ 14009 14010void 14011ctl_init_isc_msg(void) 14012{ 14013 printf("CTL: Still calling this thing\n"); 14014} 14015 14016/* 14017 * Init component 14018 * Initializes component into configuration defined by bootMode 14019 * (see hasc-sv.c) 14020 * returns hasc_Status: 14021 * OK 14022 * ERROR - fatal error 14023 */ 14024static ctl_ha_comp_status 14025ctl_isc_init(struct ctl_ha_component *c) 14026{ 14027 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14028 14029 c->status = ret; 14030 return ret; 14031} 14032 14033/* Start component 14034 * Starts component in state requested. If component starts successfully, 14035 * it must set its own state to the requestrd state 14036 * When requested state is HASC_STATE_HA, the component may refine it 14037 * by adding _SLAVE or _MASTER flags. 14038 * Currently allowed state transitions are: 14039 * UNKNOWN->HA - initial startup 14040 * UNKNOWN->SINGLE - initial startup when no parter detected 14041 * HA->SINGLE - failover 14042 * returns ctl_ha_comp_status: 14043 * OK - component successfully started in requested state 14044 * FAILED - could not start the requested state, failover may 14045 * be possible 14046 * ERROR - fatal error detected, no future startup possible 14047 */ 14048static ctl_ha_comp_status 14049ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14050{ 14051 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14052 14053 printf("%s: go\n", __func__); 14054 14055 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14056 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14057 ctl_is_single = 0; 14058 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14059 != CTL_HA_STATUS_SUCCESS) { 14060 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14061 ret = CTL_HA_COMP_STATUS_ERROR; 14062 } 14063 } else if (CTL_HA_STATE_IS_HA(c->state) 14064 && CTL_HA_STATE_IS_SINGLE(state)){ 14065 // HA->SINGLE transition 14066 ctl_failover(); 14067 ctl_is_single = 1; 14068 } else { 14069 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14070 c->state, state); 14071 ret = CTL_HA_COMP_STATUS_ERROR; 14072 } 14073 if (CTL_HA_STATE_IS_SINGLE(state)) 14074 ctl_is_single = 1; 14075 14076 c->state = state; 14077 c->status = ret; 14078 return ret; 14079} 14080 14081/* 14082 * Quiesce component 14083 * The component must clear any error conditions (set status to OK) and 14084 * prepare itself to another Start call 14085 * returns ctl_ha_comp_status: 14086 * OK 14087 * ERROR 14088 */ 14089static ctl_ha_comp_status 14090ctl_isc_quiesce(struct ctl_ha_component *c) 14091{ 14092 int ret = CTL_HA_COMP_STATUS_OK; 14093 14094 ctl_pause_rtr = 1; 14095 c->status = ret; 14096 return ret; 14097} 14098 14099struct ctl_ha_component ctl_ha_component_ctlisc = 14100{ 14101 .name = "CTL ISC", 14102 .state = CTL_HA_STATE_UNKNOWN, 14103 .init = ctl_isc_init, 14104 .start = ctl_isc_start, 14105 .quiesce = ctl_isc_quiesce 14106}; 14107 14108/* 14109 * vim: ts=8 14110 */ 14111