ctl.c revision 268767
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 268767 2014-07-16 15:57:17Z 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; 315SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 316 &worker_threads, 1, "Number of worker threads"); 317static int verbose = 0; 318SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 319 &verbose, 0, "Show SCSI errors returned to initiator"); 320 321/* 322 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 323 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0) and 324 * Logical Block Provisioning (0xB2) 325 */ 326#define SCSI_EVPD_NUM_SUPPORTED_PAGES 7 327 328static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 329 int param); 330static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 331static int ctl_init(void); 332void ctl_shutdown(void); 333static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 334static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 335static void ctl_ioctl_online(void *arg); 336static void ctl_ioctl_offline(void *arg); 337static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 338static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 339static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 340static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 341static int ctl_ioctl_submit_wait(union ctl_io *io); 342static void ctl_ioctl_datamove(union ctl_io *io); 343static void ctl_ioctl_done(union ctl_io *io); 344static void ctl_ioctl_hard_startstop_callback(void *arg, 345 struct cfi_metatask *metatask); 346static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 347static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 348 struct ctl_ooa *ooa_hdr, 349 struct ctl_ooa_entry *kern_entries); 350static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 351 struct thread *td); 352static uint32_t ctl_map_lun(int port_num, uint32_t lun); 353static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 354#ifdef unused 355static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 356 uint32_t targ_target, uint32_t targ_lun, 357 int can_wait); 358static void ctl_kfree_io(union ctl_io *io); 359#endif /* unused */ 360static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 361 struct ctl_be_lun *be_lun, struct ctl_id target_id); 362static int ctl_free_lun(struct ctl_lun *lun); 363static void ctl_create_lun(struct ctl_be_lun *be_lun); 364/** 365static void ctl_failover_change_pages(struct ctl_softc *softc, 366 struct ctl_scsiio *ctsio, int master); 367**/ 368 369static int ctl_do_mode_select(union ctl_io *io); 370static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 371 uint64_t res_key, uint64_t sa_res_key, 372 uint8_t type, uint32_t residx, 373 struct ctl_scsiio *ctsio, 374 struct scsi_per_res_out *cdb, 375 struct scsi_per_res_out_parms* param); 376static void ctl_pro_preempt_other(struct ctl_lun *lun, 377 union ctl_ha_msg *msg); 378static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 379static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 380static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 381static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 382static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 383 int alloc_len); 384static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 385 int alloc_len); 386static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 387static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 388static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 389static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 390static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 391static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 392 union ctl_io *ooa_io); 393static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 394 union ctl_io *starting_io); 395static int ctl_check_blocked(struct ctl_lun *lun); 396static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 397 struct ctl_lun *lun, 398 const struct ctl_cmd_entry *entry, 399 struct ctl_scsiio *ctsio); 400//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 401static void ctl_failover(void); 402static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 403 struct ctl_scsiio *ctsio); 404static int ctl_scsiio(struct ctl_scsiio *ctsio); 405 406static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 407static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 408 ctl_ua_type ua_type); 409static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 410 ctl_ua_type ua_type); 411static int ctl_abort_task(union ctl_io *io); 412static int ctl_abort_task_set(union ctl_io *io); 413static int ctl_i_t_nexus_reset(union ctl_io *io); 414static void ctl_run_task(union ctl_io *io); 415#ifdef CTL_IO_DELAY 416static void ctl_datamove_timer_wakeup(void *arg); 417static void ctl_done_timer_wakeup(void *arg); 418#endif /* CTL_IO_DELAY */ 419 420static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 421static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 422static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 423static void ctl_datamove_remote_write(union ctl_io *io); 424static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 425static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 426static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 427static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 428 ctl_ha_dt_cb callback); 429static void ctl_datamove_remote_read(union ctl_io *io); 430static void ctl_datamove_remote(union ctl_io *io); 431static int ctl_process_done(union ctl_io *io); 432static void ctl_lun_thread(void *arg); 433static void ctl_work_thread(void *arg); 434static void ctl_enqueue_incoming(union ctl_io *io); 435static void ctl_enqueue_rtr(union ctl_io *io); 436static void ctl_enqueue_done(union ctl_io *io); 437static void ctl_enqueue_isc(union ctl_io *io); 438static const struct ctl_cmd_entry * 439 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 440static const struct ctl_cmd_entry * 441 ctl_validate_command(struct ctl_scsiio *ctsio); 442static int ctl_cmd_applicable(uint8_t lun_type, 443 const struct ctl_cmd_entry *entry); 444 445/* 446 * Load the serialization table. This isn't very pretty, but is probably 447 * the easiest way to do it. 448 */ 449#include "ctl_ser_table.c" 450 451/* 452 * We only need to define open, close and ioctl routines for this driver. 453 */ 454static struct cdevsw ctl_cdevsw = { 455 .d_version = D_VERSION, 456 .d_flags = 0, 457 .d_open = ctl_open, 458 .d_close = ctl_close, 459 .d_ioctl = ctl_ioctl, 460 .d_name = "ctl", 461}; 462 463 464MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 465MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 466 467static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 468 469static moduledata_t ctl_moduledata = { 470 "ctl", 471 ctl_module_event_handler, 472 NULL 473}; 474 475DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 476MODULE_VERSION(ctl, 1); 477 478static struct ctl_frontend ioctl_frontend = 479{ 480 .name = "ioctl", 481}; 482 483static void 484ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 485 union ctl_ha_msg *msg_info) 486{ 487 struct ctl_scsiio *ctsio; 488 489 if (msg_info->hdr.original_sc == NULL) { 490 printf("%s: original_sc == NULL!\n", __func__); 491 /* XXX KDM now what? */ 492 return; 493 } 494 495 ctsio = &msg_info->hdr.original_sc->scsiio; 496 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 497 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 498 ctsio->io_hdr.status = msg_info->hdr.status; 499 ctsio->scsi_status = msg_info->scsi.scsi_status; 500 ctsio->sense_len = msg_info->scsi.sense_len; 501 ctsio->sense_residual = msg_info->scsi.sense_residual; 502 ctsio->residual = msg_info->scsi.residual; 503 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 504 sizeof(ctsio->sense_data)); 505 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 506 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 507 ctl_enqueue_isc((union ctl_io *)ctsio); 508} 509 510static void 511ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 512 union ctl_ha_msg *msg_info) 513{ 514 struct ctl_scsiio *ctsio; 515 516 if (msg_info->hdr.serializing_sc == NULL) { 517 printf("%s: serializing_sc == NULL!\n", __func__); 518 /* XXX KDM now what? */ 519 return; 520 } 521 522 ctsio = &msg_info->hdr.serializing_sc->scsiio; 523#if 0 524 /* 525 * Attempt to catch the situation where an I/O has 526 * been freed, and we're using it again. 527 */ 528 if (ctsio->io_hdr.io_type == 0xff) { 529 union ctl_io *tmp_io; 530 tmp_io = (union ctl_io *)ctsio; 531 printf("%s: %p use after free!\n", __func__, 532 ctsio); 533 printf("%s: type %d msg %d cdb %x iptl: " 534 "%d:%d:%d:%d tag 0x%04x " 535 "flag %#x status %x\n", 536 __func__, 537 tmp_io->io_hdr.io_type, 538 tmp_io->io_hdr.msg_type, 539 tmp_io->scsiio.cdb[0], 540 tmp_io->io_hdr.nexus.initid.id, 541 tmp_io->io_hdr.nexus.targ_port, 542 tmp_io->io_hdr.nexus.targ_target.id, 543 tmp_io->io_hdr.nexus.targ_lun, 544 (tmp_io->io_hdr.io_type == 545 CTL_IO_TASK) ? 546 tmp_io->taskio.tag_num : 547 tmp_io->scsiio.tag_num, 548 tmp_io->io_hdr.flags, 549 tmp_io->io_hdr.status); 550 } 551#endif 552 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 553 ctl_enqueue_isc((union ctl_io *)ctsio); 554} 555 556/* 557 * ISC (Inter Shelf Communication) event handler. Events from the HA 558 * subsystem come in here. 559 */ 560static void 561ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 562{ 563 struct ctl_softc *ctl_softc; 564 union ctl_io *io; 565 struct ctl_prio *presio; 566 ctl_ha_status isc_status; 567 568 ctl_softc = control_softc; 569 io = NULL; 570 571 572#if 0 573 printf("CTL: Isc Msg event %d\n", event); 574#endif 575 if (event == CTL_HA_EVT_MSG_RECV) { 576 union ctl_ha_msg msg_info; 577 578 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 579 sizeof(msg_info), /*wait*/ 0); 580#if 0 581 printf("CTL: msg_type %d\n", msg_info.msg_type); 582#endif 583 if (isc_status != 0) { 584 printf("Error receiving message, status = %d\n", 585 isc_status); 586 return; 587 } 588 589 switch (msg_info.hdr.msg_type) { 590 case CTL_MSG_SERIALIZE: 591#if 0 592 printf("Serialize\n"); 593#endif 594 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 595 if (io == NULL) { 596 printf("ctl_isc_event_handler: can't allocate " 597 "ctl_io!\n"); 598 /* Bad Juju */ 599 /* Need to set busy and send msg back */ 600 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 601 msg_info.hdr.status = CTL_SCSI_ERROR; 602 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 603 msg_info.scsi.sense_len = 0; 604 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 605 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 606 } 607 goto bailout; 608 } 609 ctl_zero_io(io); 610 // populate ctsio from msg_info 611 io->io_hdr.io_type = CTL_IO_SCSI; 612 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 613 io->io_hdr.original_sc = msg_info.hdr.original_sc; 614#if 0 615 printf("pOrig %x\n", (int)msg_info.original_sc); 616#endif 617 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 618 CTL_FLAG_IO_ACTIVE; 619 /* 620 * If we're in serialization-only mode, we don't 621 * want to go through full done processing. Thus 622 * the COPY flag. 623 * 624 * XXX KDM add another flag that is more specific. 625 */ 626 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 627 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 628 io->io_hdr.nexus = msg_info.hdr.nexus; 629#if 0 630 printf("targ %d, port %d, iid %d, lun %d\n", 631 io->io_hdr.nexus.targ_target.id, 632 io->io_hdr.nexus.targ_port, 633 io->io_hdr.nexus.initid.id, 634 io->io_hdr.nexus.targ_lun); 635#endif 636 io->scsiio.tag_num = msg_info.scsi.tag_num; 637 io->scsiio.tag_type = msg_info.scsi.tag_type; 638 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 639 CTL_MAX_CDBLEN); 640 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 641 const struct ctl_cmd_entry *entry; 642 643 entry = ctl_get_cmd_entry(&io->scsiio); 644 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 645 io->io_hdr.flags |= 646 entry->flags & CTL_FLAG_DATA_MASK; 647 } 648 ctl_enqueue_isc(io); 649 break; 650 651 /* Performed on the Originating SC, XFER mode only */ 652 case CTL_MSG_DATAMOVE: { 653 struct ctl_sg_entry *sgl; 654 int i, j; 655 656 io = msg_info.hdr.original_sc; 657 if (io == NULL) { 658 printf("%s: original_sc == NULL!\n", __func__); 659 /* XXX KDM do something here */ 660 break; 661 } 662 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 663 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 664 /* 665 * Keep track of this, we need to send it back over 666 * when the datamove is complete. 667 */ 668 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 669 670 if (msg_info.dt.sg_sequence == 0) { 671 /* 672 * XXX KDM we use the preallocated S/G list 673 * here, but we'll need to change this to 674 * dynamic allocation if we need larger S/G 675 * lists. 676 */ 677 if (msg_info.dt.kern_sg_entries > 678 sizeof(io->io_hdr.remote_sglist) / 679 sizeof(io->io_hdr.remote_sglist[0])) { 680 printf("%s: number of S/G entries " 681 "needed %u > allocated num %zd\n", 682 __func__, 683 msg_info.dt.kern_sg_entries, 684 sizeof(io->io_hdr.remote_sglist)/ 685 sizeof(io->io_hdr.remote_sglist[0])); 686 687 /* 688 * XXX KDM send a message back to 689 * the other side to shut down the 690 * DMA. The error will come back 691 * through via the normal channel. 692 */ 693 break; 694 } 695 sgl = io->io_hdr.remote_sglist; 696 memset(sgl, 0, 697 sizeof(io->io_hdr.remote_sglist)); 698 699 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 700 701 io->scsiio.kern_sg_entries = 702 msg_info.dt.kern_sg_entries; 703 io->scsiio.rem_sg_entries = 704 msg_info.dt.kern_sg_entries; 705 io->scsiio.kern_data_len = 706 msg_info.dt.kern_data_len; 707 io->scsiio.kern_total_len = 708 msg_info.dt.kern_total_len; 709 io->scsiio.kern_data_resid = 710 msg_info.dt.kern_data_resid; 711 io->scsiio.kern_rel_offset = 712 msg_info.dt.kern_rel_offset; 713 /* 714 * Clear out per-DMA flags. 715 */ 716 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 717 /* 718 * Add per-DMA flags that are set for this 719 * particular DMA request. 720 */ 721 io->io_hdr.flags |= msg_info.dt.flags & 722 CTL_FLAG_RDMA_MASK; 723 } else 724 sgl = (struct ctl_sg_entry *) 725 io->scsiio.kern_data_ptr; 726 727 for (i = msg_info.dt.sent_sg_entries, j = 0; 728 i < (msg_info.dt.sent_sg_entries + 729 msg_info.dt.cur_sg_entries); i++, j++) { 730 sgl[i].addr = msg_info.dt.sg_list[j].addr; 731 sgl[i].len = msg_info.dt.sg_list[j].len; 732 733#if 0 734 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 735 __func__, 736 msg_info.dt.sg_list[j].addr, 737 msg_info.dt.sg_list[j].len, 738 sgl[i].addr, sgl[i].len, j, i); 739#endif 740 } 741#if 0 742 memcpy(&sgl[msg_info.dt.sent_sg_entries], 743 msg_info.dt.sg_list, 744 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 745#endif 746 747 /* 748 * If this is the last piece of the I/O, we've got 749 * the full S/G list. Queue processing in the thread. 750 * Otherwise wait for the next piece. 751 */ 752 if (msg_info.dt.sg_last != 0) 753 ctl_enqueue_isc(io); 754 break; 755 } 756 /* Performed on the Serializing (primary) SC, XFER mode only */ 757 case CTL_MSG_DATAMOVE_DONE: { 758 if (msg_info.hdr.serializing_sc == NULL) { 759 printf("%s: serializing_sc == NULL!\n", 760 __func__); 761 /* XXX KDM now what? */ 762 break; 763 } 764 /* 765 * We grab the sense information here in case 766 * there was a failure, so we can return status 767 * back to the initiator. 768 */ 769 io = msg_info.hdr.serializing_sc; 770 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 771 io->io_hdr.status = msg_info.hdr.status; 772 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 773 io->scsiio.sense_len = msg_info.scsi.sense_len; 774 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 775 io->io_hdr.port_status = msg_info.scsi.fetd_status; 776 io->scsiio.residual = msg_info.scsi.residual; 777 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 778 sizeof(io->scsiio.sense_data)); 779 ctl_enqueue_isc(io); 780 break; 781 } 782 783 /* Preformed on Originating SC, SER_ONLY mode */ 784 case CTL_MSG_R2R: 785 io = msg_info.hdr.original_sc; 786 if (io == NULL) { 787 printf("%s: Major Bummer\n", __func__); 788 return; 789 } else { 790#if 0 791 printf("pOrig %x\n",(int) ctsio); 792#endif 793 } 794 io->io_hdr.msg_type = CTL_MSG_R2R; 795 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 796 ctl_enqueue_isc(io); 797 break; 798 799 /* 800 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 801 * mode. 802 * Performed on the Originating (i.e. secondary) SC in XFER 803 * mode 804 */ 805 case CTL_MSG_FINISH_IO: 806 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 807 ctl_isc_handler_finish_xfer(ctl_softc, 808 &msg_info); 809 else 810 ctl_isc_handler_finish_ser_only(ctl_softc, 811 &msg_info); 812 break; 813 814 /* Preformed on Originating SC */ 815 case CTL_MSG_BAD_JUJU: 816 io = msg_info.hdr.original_sc; 817 if (io == NULL) { 818 printf("%s: Bad JUJU!, original_sc is NULL!\n", 819 __func__); 820 break; 821 } 822 ctl_copy_sense_data(&msg_info, io); 823 /* 824 * IO should have already been cleaned up on other 825 * SC so clear this flag so we won't send a message 826 * back to finish the IO there. 827 */ 828 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 829 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 830 831 /* io = msg_info.hdr.serializing_sc; */ 832 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 833 ctl_enqueue_isc(io); 834 break; 835 836 /* Handle resets sent from the other side */ 837 case CTL_MSG_MANAGE_TASKS: { 838 struct ctl_taskio *taskio; 839 taskio = (struct ctl_taskio *)ctl_alloc_io( 840 (void *)ctl_softc->othersc_pool); 841 if (taskio == NULL) { 842 printf("ctl_isc_event_handler: can't allocate " 843 "ctl_io!\n"); 844 /* Bad Juju */ 845 /* should I just call the proper reset func 846 here??? */ 847 goto bailout; 848 } 849 ctl_zero_io((union ctl_io *)taskio); 850 taskio->io_hdr.io_type = CTL_IO_TASK; 851 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 852 taskio->io_hdr.nexus = msg_info.hdr.nexus; 853 taskio->task_action = msg_info.task.task_action; 854 taskio->tag_num = msg_info.task.tag_num; 855 taskio->tag_type = msg_info.task.tag_type; 856#ifdef CTL_TIME_IO 857 taskio->io_hdr.start_time = time_uptime; 858 getbintime(&taskio->io_hdr.start_bt); 859#if 0 860 cs_prof_gettime(&taskio->io_hdr.start_ticks); 861#endif 862#endif /* CTL_TIME_IO */ 863 ctl_run_task((union ctl_io *)taskio); 864 break; 865 } 866 /* Persistent Reserve action which needs attention */ 867 case CTL_MSG_PERS_ACTION: 868 presio = (struct ctl_prio *)ctl_alloc_io( 869 (void *)ctl_softc->othersc_pool); 870 if (presio == NULL) { 871 printf("ctl_isc_event_handler: can't allocate " 872 "ctl_io!\n"); 873 /* Bad Juju */ 874 /* Need to set busy and send msg back */ 875 goto bailout; 876 } 877 ctl_zero_io((union ctl_io *)presio); 878 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 879 presio->pr_msg = msg_info.pr; 880 ctl_enqueue_isc((union ctl_io *)presio); 881 break; 882 case CTL_MSG_SYNC_FE: 883 rcv_sync_msg = 1; 884 break; 885 case CTL_MSG_APS_LOCK: { 886 // It's quicker to execute this then to 887 // queue it. 888 struct ctl_lun *lun; 889 struct ctl_page_index *page_index; 890 struct copan_aps_subpage *current_sp; 891 uint32_t targ_lun; 892 893 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 894 lun = ctl_softc->ctl_luns[targ_lun]; 895 mtx_lock(&lun->lun_lock); 896 page_index = &lun->mode_pages.index[index_to_aps_page]; 897 current_sp = (struct copan_aps_subpage *) 898 (page_index->page_data + 899 (page_index->page_len * CTL_PAGE_CURRENT)); 900 901 current_sp->lock_active = msg_info.aps.lock_flag; 902 mtx_unlock(&lun->lun_lock); 903 break; 904 } 905 default: 906 printf("How did I get here?\n"); 907 } 908 } else if (event == CTL_HA_EVT_MSG_SENT) { 909 if (param != CTL_HA_STATUS_SUCCESS) { 910 printf("Bad status from ctl_ha_msg_send status %d\n", 911 param); 912 } 913 return; 914 } else if (event == CTL_HA_EVT_DISCONNECT) { 915 printf("CTL: Got a disconnect from Isc\n"); 916 return; 917 } else { 918 printf("ctl_isc_event_handler: Unknown event %d\n", event); 919 return; 920 } 921 922bailout: 923 return; 924} 925 926static void 927ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 928{ 929 struct scsi_sense_data *sense; 930 931 sense = &dest->scsiio.sense_data; 932 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 933 dest->scsiio.scsi_status = src->scsi.scsi_status; 934 dest->scsiio.sense_len = src->scsi.sense_len; 935 dest->io_hdr.status = src->hdr.status; 936} 937 938static int 939ctl_init(void) 940{ 941 struct ctl_softc *softc; 942 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 943 struct ctl_port *port; 944 uint8_t sc_id =0; 945 int i, error, retval; 946 //int isc_retval; 947 948 retval = 0; 949 ctl_pause_rtr = 0; 950 rcv_sync_msg = 0; 951 952 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 953 M_WAITOK | M_ZERO); 954 softc = control_softc; 955 956 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 957 "cam/ctl"); 958 959 softc->dev->si_drv1 = softc; 960 961 /* 962 * By default, return a "bad LUN" peripheral qualifier for unknown 963 * LUNs. The user can override this default using the tunable or 964 * sysctl. See the comment in ctl_inquiry_std() for more details. 965 */ 966 softc->inquiry_pq_no_lun = 1; 967 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 968 &softc->inquiry_pq_no_lun); 969 sysctl_ctx_init(&softc->sysctl_ctx); 970 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 971 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 972 CTLFLAG_RD, 0, "CAM Target Layer"); 973 974 if (softc->sysctl_tree == NULL) { 975 printf("%s: unable to allocate sysctl tree\n", __func__); 976 destroy_dev(softc->dev); 977 free(control_softc, M_DEVBUF); 978 control_softc = NULL; 979 return (ENOMEM); 980 } 981 982 SYSCTL_ADD_INT(&softc->sysctl_ctx, 983 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 984 "inquiry_pq_no_lun", CTLFLAG_RW, 985 &softc->inquiry_pq_no_lun, 0, 986 "Report no lun possible for invalid LUNs"); 987 988 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 989 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 990 softc->open_count = 0; 991 992 /* 993 * Default to actually sending a SYNCHRONIZE CACHE command down to 994 * the drive. 995 */ 996 softc->flags = CTL_FLAG_REAL_SYNC; 997 998 /* 999 * In Copan's HA scheme, the "master" and "slave" roles are 1000 * figured out through the slot the controller is in. Although it 1001 * is an active/active system, someone has to be in charge. 1002 */ 1003#ifdef NEEDTOPORT 1004 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1005#endif 1006 1007 if (sc_id == 0) { 1008 softc->flags |= CTL_FLAG_MASTER_SHELF; 1009 persis_offset = 0; 1010 } else 1011 persis_offset = CTL_MAX_INITIATORS; 1012 1013 /* 1014 * XXX KDM need to figure out where we want to get our target ID 1015 * and WWID. Is it different on each port? 1016 */ 1017 softc->target.id = 0; 1018 softc->target.wwid[0] = 0x12345678; 1019 softc->target.wwid[1] = 0x87654321; 1020 STAILQ_INIT(&softc->lun_list); 1021 STAILQ_INIT(&softc->pending_lun_queue); 1022 STAILQ_INIT(&softc->fe_list); 1023 STAILQ_INIT(&softc->port_list); 1024 STAILQ_INIT(&softc->be_list); 1025 STAILQ_INIT(&softc->io_pools); 1026 1027 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1028 &internal_pool)!= 0){ 1029 printf("ctl: can't allocate %d entry internal pool, " 1030 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1031 return (ENOMEM); 1032 } 1033 1034 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1035 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1036 printf("ctl: can't allocate %d entry emergency pool, " 1037 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1038 ctl_pool_free(internal_pool); 1039 return (ENOMEM); 1040 } 1041 1042 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1043 &other_pool) != 0) 1044 { 1045 printf("ctl: can't allocate %d entry other SC pool, " 1046 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1047 ctl_pool_free(internal_pool); 1048 ctl_pool_free(emergency_pool); 1049 return (ENOMEM); 1050 } 1051 1052 softc->internal_pool = internal_pool; 1053 softc->emergency_pool = emergency_pool; 1054 softc->othersc_pool = other_pool; 1055 1056 if (worker_threads <= 0) 1057 worker_threads = max(1, mp_ncpus / 4); 1058 if (worker_threads > CTL_MAX_THREADS) 1059 worker_threads = CTL_MAX_THREADS; 1060 1061 for (i = 0; i < worker_threads; i++) { 1062 struct ctl_thread *thr = &softc->threads[i]; 1063 1064 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1065 thr->ctl_softc = softc; 1066 STAILQ_INIT(&thr->incoming_queue); 1067 STAILQ_INIT(&thr->rtr_queue); 1068 STAILQ_INIT(&thr->done_queue); 1069 STAILQ_INIT(&thr->isc_queue); 1070 1071 error = kproc_kthread_add(ctl_work_thread, thr, 1072 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1073 if (error != 0) { 1074 printf("error creating CTL work thread!\n"); 1075 ctl_pool_free(internal_pool); 1076 ctl_pool_free(emergency_pool); 1077 ctl_pool_free(other_pool); 1078 return (error); 1079 } 1080 } 1081 error = kproc_kthread_add(ctl_lun_thread, softc, 1082 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1083 if (error != 0) { 1084 printf("error creating CTL lun thread!\n"); 1085 ctl_pool_free(internal_pool); 1086 ctl_pool_free(emergency_pool); 1087 ctl_pool_free(other_pool); 1088 return (error); 1089 } 1090 if (bootverbose) 1091 printf("ctl: CAM Target Layer loaded\n"); 1092 1093 /* 1094 * Initialize the ioctl front end. 1095 */ 1096 ctl_frontend_register(&ioctl_frontend); 1097 port = &softc->ioctl_info.port; 1098 port->frontend = &ioctl_frontend; 1099 sprintf(softc->ioctl_info.port_name, "ioctl"); 1100 port->port_type = CTL_PORT_IOCTL; 1101 port->num_requested_ctl_io = 100; 1102 port->port_name = softc->ioctl_info.port_name; 1103 port->port_online = ctl_ioctl_online; 1104 port->port_offline = ctl_ioctl_offline; 1105 port->onoff_arg = &softc->ioctl_info; 1106 port->lun_enable = ctl_ioctl_lun_enable; 1107 port->lun_disable = ctl_ioctl_lun_disable; 1108 port->targ_lun_arg = &softc->ioctl_info; 1109 port->fe_datamove = ctl_ioctl_datamove; 1110 port->fe_done = ctl_ioctl_done; 1111 port->max_targets = 15; 1112 port->max_target_id = 15; 1113 1114 if (ctl_port_register(&softc->ioctl_info.port, 1115 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1116 printf("ctl: ioctl front end registration failed, will " 1117 "continue anyway\n"); 1118 } 1119 1120#ifdef CTL_IO_DELAY 1121 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1122 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1123 sizeof(struct callout), CTL_TIMER_BYTES); 1124 return (EINVAL); 1125 } 1126#endif /* CTL_IO_DELAY */ 1127 1128 return (0); 1129} 1130 1131void 1132ctl_shutdown(void) 1133{ 1134 struct ctl_softc *softc; 1135 struct ctl_lun *lun, *next_lun; 1136 struct ctl_io_pool *pool; 1137 1138 softc = (struct ctl_softc *)control_softc; 1139 1140 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1141 printf("ctl: ioctl front end deregistration failed\n"); 1142 1143 mtx_lock(&softc->ctl_lock); 1144 1145 /* 1146 * Free up each LUN. 1147 */ 1148 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1149 next_lun = STAILQ_NEXT(lun, links); 1150 ctl_free_lun(lun); 1151 } 1152 1153 mtx_unlock(&softc->ctl_lock); 1154 1155 ctl_frontend_deregister(&ioctl_frontend); 1156 1157 /* 1158 * This will rip the rug out from under any FETDs or anyone else 1159 * that has a pool allocated. Since we increment our module 1160 * refcount any time someone outside the main CTL module allocates 1161 * a pool, we shouldn't have any problems here. The user won't be 1162 * able to unload the CTL module until client modules have 1163 * successfully unloaded. 1164 */ 1165 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1166 ctl_pool_free(pool); 1167 1168#if 0 1169 ctl_shutdown_thread(softc->work_thread); 1170 mtx_destroy(&softc->queue_lock); 1171#endif 1172 1173 mtx_destroy(&softc->pool_lock); 1174 mtx_destroy(&softc->ctl_lock); 1175 1176 destroy_dev(softc->dev); 1177 1178 sysctl_ctx_free(&softc->sysctl_ctx); 1179 1180 free(control_softc, M_DEVBUF); 1181 control_softc = NULL; 1182 1183 if (bootverbose) 1184 printf("ctl: CAM Target Layer unloaded\n"); 1185} 1186 1187static int 1188ctl_module_event_handler(module_t mod, int what, void *arg) 1189{ 1190 1191 switch (what) { 1192 case MOD_LOAD: 1193 return (ctl_init()); 1194 case MOD_UNLOAD: 1195 return (EBUSY); 1196 default: 1197 return (EOPNOTSUPP); 1198 } 1199} 1200 1201/* 1202 * XXX KDM should we do some access checks here? Bump a reference count to 1203 * prevent a CTL module from being unloaded while someone has it open? 1204 */ 1205static int 1206ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1207{ 1208 return (0); 1209} 1210 1211static int 1212ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1213{ 1214 return (0); 1215} 1216 1217int 1218ctl_port_enable(ctl_port_type port_type) 1219{ 1220 struct ctl_softc *softc; 1221 struct ctl_port *port; 1222 1223 if (ctl_is_single == 0) { 1224 union ctl_ha_msg msg_info; 1225 int isc_retval; 1226 1227#if 0 1228 printf("%s: HA mode, synchronizing frontend enable\n", 1229 __func__); 1230#endif 1231 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1232 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1233 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1234 printf("Sync msg send error retval %d\n", isc_retval); 1235 } 1236 if (!rcv_sync_msg) { 1237 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1238 sizeof(msg_info), 1); 1239 } 1240#if 0 1241 printf("CTL:Frontend Enable\n"); 1242 } else { 1243 printf("%s: single mode, skipping frontend synchronization\n", 1244 __func__); 1245#endif 1246 } 1247 1248 softc = control_softc; 1249 1250 STAILQ_FOREACH(port, &softc->port_list, links) { 1251 if (port_type & port->port_type) 1252 { 1253#if 0 1254 printf("port %d\n", port->targ_port); 1255#endif 1256 ctl_port_online(port); 1257 } 1258 } 1259 1260 return (0); 1261} 1262 1263int 1264ctl_port_disable(ctl_port_type port_type) 1265{ 1266 struct ctl_softc *softc; 1267 struct ctl_port *port; 1268 1269 softc = control_softc; 1270 1271 STAILQ_FOREACH(port, &softc->port_list, links) { 1272 if (port_type & port->port_type) 1273 ctl_port_offline(port); 1274 } 1275 1276 return (0); 1277} 1278 1279/* 1280 * Returns 0 for success, 1 for failure. 1281 * Currently the only failure mode is if there aren't enough entries 1282 * allocated. So, in case of a failure, look at num_entries_dropped, 1283 * reallocate and try again. 1284 */ 1285int 1286ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1287 int *num_entries_filled, int *num_entries_dropped, 1288 ctl_port_type port_type, int no_virtual) 1289{ 1290 struct ctl_softc *softc; 1291 struct ctl_port *port; 1292 int entries_dropped, entries_filled; 1293 int retval; 1294 int i; 1295 1296 softc = control_softc; 1297 1298 retval = 0; 1299 entries_filled = 0; 1300 entries_dropped = 0; 1301 1302 i = 0; 1303 mtx_lock(&softc->ctl_lock); 1304 STAILQ_FOREACH(port, &softc->port_list, links) { 1305 struct ctl_port_entry *entry; 1306 1307 if ((port->port_type & port_type) == 0) 1308 continue; 1309 1310 if ((no_virtual != 0) 1311 && (port->virtual_port != 0)) 1312 continue; 1313 1314 if (entries_filled >= num_entries_alloced) { 1315 entries_dropped++; 1316 continue; 1317 } 1318 entry = &entries[i]; 1319 1320 entry->port_type = port->port_type; 1321 strlcpy(entry->port_name, port->port_name, 1322 sizeof(entry->port_name)); 1323 entry->physical_port = port->physical_port; 1324 entry->virtual_port = port->virtual_port; 1325 entry->wwnn = port->wwnn; 1326 entry->wwpn = port->wwpn; 1327 1328 i++; 1329 entries_filled++; 1330 } 1331 1332 mtx_unlock(&softc->ctl_lock); 1333 1334 if (entries_dropped > 0) 1335 retval = 1; 1336 1337 *num_entries_dropped = entries_dropped; 1338 *num_entries_filled = entries_filled; 1339 1340 return (retval); 1341} 1342 1343static void 1344ctl_ioctl_online(void *arg) 1345{ 1346 struct ctl_ioctl_info *ioctl_info; 1347 1348 ioctl_info = (struct ctl_ioctl_info *)arg; 1349 1350 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1351} 1352 1353static void 1354ctl_ioctl_offline(void *arg) 1355{ 1356 struct ctl_ioctl_info *ioctl_info; 1357 1358 ioctl_info = (struct ctl_ioctl_info *)arg; 1359 1360 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1361} 1362 1363/* 1364 * Remove an initiator by port number and initiator ID. 1365 * Returns 0 for success, -1 for failure. 1366 */ 1367int 1368ctl_remove_initiator(struct ctl_port *port, int iid) 1369{ 1370 struct ctl_softc *softc = control_softc; 1371 1372 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1373 1374 if (iid > CTL_MAX_INIT_PER_PORT) { 1375 printf("%s: initiator ID %u > maximun %u!\n", 1376 __func__, iid, CTL_MAX_INIT_PER_PORT); 1377 return (-1); 1378 } 1379 1380 mtx_lock(&softc->ctl_lock); 1381 port->wwpn_iid[iid].in_use--; 1382 port->wwpn_iid[iid].last_use = time_uptime; 1383 mtx_unlock(&softc->ctl_lock); 1384 1385 return (0); 1386} 1387 1388/* 1389 * Add an initiator to the initiator map. 1390 * Returns iid for success, < 0 for failure. 1391 */ 1392int 1393ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1394{ 1395 struct ctl_softc *softc = control_softc; 1396 time_t best_time; 1397 int i, best; 1398 1399 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1400 1401 if (iid >= CTL_MAX_INIT_PER_PORT) { 1402 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1403 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1404 free(name, M_CTL); 1405 return (-1); 1406 } 1407 1408 mtx_lock(&softc->ctl_lock); 1409 1410 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1411 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1412 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1413 iid = i; 1414 break; 1415 } 1416 if (name != NULL && port->wwpn_iid[i].name != NULL && 1417 strcmp(name, port->wwpn_iid[i].name) == 0) { 1418 iid = i; 1419 break; 1420 } 1421 } 1422 } 1423 1424 if (iid < 0) { 1425 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1426 if (port->wwpn_iid[i].in_use == 0 && 1427 port->wwpn_iid[i].wwpn == 0 && 1428 port->wwpn_iid[i].name == NULL) { 1429 iid = i; 1430 break; 1431 } 1432 } 1433 } 1434 1435 if (iid < 0) { 1436 best = -1; 1437 best_time = INT32_MAX; 1438 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1439 if (port->wwpn_iid[i].in_use == 0) { 1440 if (port->wwpn_iid[i].last_use < best_time) { 1441 best = i; 1442 best_time = port->wwpn_iid[i].last_use; 1443 } 1444 } 1445 } 1446 iid = best; 1447 } 1448 1449 if (iid < 0) { 1450 mtx_unlock(&softc->ctl_lock); 1451 free(name, M_CTL); 1452 return (-2); 1453 } 1454 1455 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1456 /* 1457 * This is not an error yet. 1458 */ 1459 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1460#if 0 1461 printf("%s: port %d iid %u WWPN %#jx arrived" 1462 " again\n", __func__, port->targ_port, 1463 iid, (uintmax_t)wwpn); 1464#endif 1465 goto take; 1466 } 1467 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1468 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1469#if 0 1470 printf("%s: port %d iid %u name '%s' arrived" 1471 " again\n", __func__, port->targ_port, 1472 iid, name); 1473#endif 1474 goto take; 1475 } 1476 1477 /* 1478 * This is an error, but what do we do about it? The 1479 * driver is telling us we have a new WWPN for this 1480 * initiator ID, so we pretty much need to use it. 1481 */ 1482 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1483 " but WWPN %#jx '%s' is still at that address\n", 1484 __func__, port->targ_port, iid, wwpn, name, 1485 (uintmax_t)port->wwpn_iid[iid].wwpn, 1486 port->wwpn_iid[iid].name); 1487 1488 /* 1489 * XXX KDM clear have_ca and ua_pending on each LUN for 1490 * this initiator. 1491 */ 1492 } 1493take: 1494 free(port->wwpn_iid[iid].name, M_CTL); 1495 port->wwpn_iid[iid].name = name; 1496 port->wwpn_iid[iid].wwpn = wwpn; 1497 port->wwpn_iid[iid].in_use++; 1498 mtx_unlock(&softc->ctl_lock); 1499 1500 return (iid); 1501} 1502 1503static int 1504ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1505{ 1506 int len; 1507 1508 switch (port->port_type) { 1509 case CTL_PORT_FC: 1510 { 1511 struct scsi_transportid_fcp *id = 1512 (struct scsi_transportid_fcp *)buf; 1513 if (port->wwpn_iid[iid].wwpn == 0) 1514 return (0); 1515 memset(id, 0, sizeof(*id)); 1516 id->format_protocol = SCSI_PROTO_FC; 1517 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1518 return (sizeof(*id)); 1519 } 1520 case CTL_PORT_ISCSI: 1521 { 1522 struct scsi_transportid_iscsi_port *id = 1523 (struct scsi_transportid_iscsi_port *)buf; 1524 if (port->wwpn_iid[iid].name == NULL) 1525 return (0); 1526 memset(id, 0, 256); 1527 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1528 SCSI_PROTO_ISCSI; 1529 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1530 len = roundup2(min(len, 252), 4); 1531 scsi_ulto2b(len, id->additional_length); 1532 return (sizeof(*id) + len); 1533 } 1534 case CTL_PORT_SAS: 1535 { 1536 struct scsi_transportid_sas *id = 1537 (struct scsi_transportid_sas *)buf; 1538 if (port->wwpn_iid[iid].wwpn == 0) 1539 return (0); 1540 memset(id, 0, sizeof(*id)); 1541 id->format_protocol = SCSI_PROTO_SAS; 1542 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1543 return (sizeof(*id)); 1544 } 1545 default: 1546 { 1547 struct scsi_transportid_spi *id = 1548 (struct scsi_transportid_spi *)buf; 1549 memset(id, 0, sizeof(*id)); 1550 id->format_protocol = SCSI_PROTO_SPI; 1551 scsi_ulto2b(iid, id->scsi_addr); 1552 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1553 return (sizeof(*id)); 1554 } 1555 } 1556} 1557 1558static int 1559ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1560{ 1561 return (0); 1562} 1563 1564static int 1565ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1566{ 1567 return (0); 1568} 1569 1570/* 1571 * Data movement routine for the CTL ioctl frontend port. 1572 */ 1573static int 1574ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1575{ 1576 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1577 struct ctl_sg_entry ext_entry, kern_entry; 1578 int ext_sglen, ext_sg_entries, kern_sg_entries; 1579 int ext_sg_start, ext_offset; 1580 int len_to_copy, len_copied; 1581 int kern_watermark, ext_watermark; 1582 int ext_sglist_malloced; 1583 int i, j; 1584 1585 ext_sglist_malloced = 0; 1586 ext_sg_start = 0; 1587 ext_offset = 0; 1588 1589 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1590 1591 /* 1592 * If this flag is set, fake the data transfer. 1593 */ 1594 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1595 ctsio->ext_data_filled = ctsio->ext_data_len; 1596 goto bailout; 1597 } 1598 1599 /* 1600 * To simplify things here, if we have a single buffer, stick it in 1601 * a S/G entry and just make it a single entry S/G list. 1602 */ 1603 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1604 int len_seen; 1605 1606 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1607 1608 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1609 M_WAITOK); 1610 ext_sglist_malloced = 1; 1611 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1612 ext_sglen) != 0) { 1613 ctl_set_internal_failure(ctsio, 1614 /*sks_valid*/ 0, 1615 /*retry_count*/ 0); 1616 goto bailout; 1617 } 1618 ext_sg_entries = ctsio->ext_sg_entries; 1619 len_seen = 0; 1620 for (i = 0; i < ext_sg_entries; i++) { 1621 if ((len_seen + ext_sglist[i].len) >= 1622 ctsio->ext_data_filled) { 1623 ext_sg_start = i; 1624 ext_offset = ctsio->ext_data_filled - len_seen; 1625 break; 1626 } 1627 len_seen += ext_sglist[i].len; 1628 } 1629 } else { 1630 ext_sglist = &ext_entry; 1631 ext_sglist->addr = ctsio->ext_data_ptr; 1632 ext_sglist->len = ctsio->ext_data_len; 1633 ext_sg_entries = 1; 1634 ext_sg_start = 0; 1635 ext_offset = ctsio->ext_data_filled; 1636 } 1637 1638 if (ctsio->kern_sg_entries > 0) { 1639 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1640 kern_sg_entries = ctsio->kern_sg_entries; 1641 } else { 1642 kern_sglist = &kern_entry; 1643 kern_sglist->addr = ctsio->kern_data_ptr; 1644 kern_sglist->len = ctsio->kern_data_len; 1645 kern_sg_entries = 1; 1646 } 1647 1648 1649 kern_watermark = 0; 1650 ext_watermark = ext_offset; 1651 len_copied = 0; 1652 for (i = ext_sg_start, j = 0; 1653 i < ext_sg_entries && j < kern_sg_entries;) { 1654 uint8_t *ext_ptr, *kern_ptr; 1655 1656 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1657 kern_sglist[j].len - kern_watermark); 1658 1659 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1660 ext_ptr = ext_ptr + ext_watermark; 1661 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1662 /* 1663 * XXX KDM fix this! 1664 */ 1665 panic("need to implement bus address support"); 1666#if 0 1667 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1668#endif 1669 } else 1670 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1671 kern_ptr = kern_ptr + kern_watermark; 1672 1673 kern_watermark += len_to_copy; 1674 ext_watermark += len_to_copy; 1675 1676 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1677 CTL_FLAG_DATA_IN) { 1678 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1679 "bytes to user\n", len_to_copy)); 1680 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1681 "to %p\n", kern_ptr, ext_ptr)); 1682 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1683 ctl_set_internal_failure(ctsio, 1684 /*sks_valid*/ 0, 1685 /*retry_count*/ 0); 1686 goto bailout; 1687 } 1688 } else { 1689 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1690 "bytes from user\n", len_to_copy)); 1691 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1692 "to %p\n", ext_ptr, kern_ptr)); 1693 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1694 ctl_set_internal_failure(ctsio, 1695 /*sks_valid*/ 0, 1696 /*retry_count*/0); 1697 goto bailout; 1698 } 1699 } 1700 1701 len_copied += len_to_copy; 1702 1703 if (ext_sglist[i].len == ext_watermark) { 1704 i++; 1705 ext_watermark = 0; 1706 } 1707 1708 if (kern_sglist[j].len == kern_watermark) { 1709 j++; 1710 kern_watermark = 0; 1711 } 1712 } 1713 1714 ctsio->ext_data_filled += len_copied; 1715 1716 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1717 "kern_sg_entries: %d\n", ext_sg_entries, 1718 kern_sg_entries)); 1719 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1720 "kern_data_len = %d\n", ctsio->ext_data_len, 1721 ctsio->kern_data_len)); 1722 1723 1724 /* XXX KDM set residual?? */ 1725bailout: 1726 1727 if (ext_sglist_malloced != 0) 1728 free(ext_sglist, M_CTL); 1729 1730 return (CTL_RETVAL_COMPLETE); 1731} 1732 1733/* 1734 * Serialize a command that went down the "wrong" side, and so was sent to 1735 * this controller for execution. The logic is a little different than the 1736 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1737 * sent back to the other side, but in the success case, we execute the 1738 * command on this side (XFER mode) or tell the other side to execute it 1739 * (SER_ONLY mode). 1740 */ 1741static int 1742ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1743{ 1744 struct ctl_softc *ctl_softc; 1745 union ctl_ha_msg msg_info; 1746 struct ctl_lun *lun; 1747 int retval = 0; 1748 uint32_t targ_lun; 1749 1750 ctl_softc = control_softc; 1751 1752 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1753 lun = ctl_softc->ctl_luns[targ_lun]; 1754 if (lun==NULL) 1755 { 1756 /* 1757 * Why isn't LUN defined? The other side wouldn't 1758 * send a cmd if the LUN is undefined. 1759 */ 1760 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1761 1762 /* "Logical unit not supported" */ 1763 ctl_set_sense_data(&msg_info.scsi.sense_data, 1764 lun, 1765 /*sense_format*/SSD_TYPE_NONE, 1766 /*current_error*/ 1, 1767 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1768 /*asc*/ 0x25, 1769 /*ascq*/ 0x00, 1770 SSD_ELEM_NONE); 1771 1772 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1773 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1774 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1775 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1776 msg_info.hdr.serializing_sc = NULL; 1777 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1778 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1779 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1780 } 1781 return(1); 1782 1783 } 1784 1785 mtx_lock(&lun->lun_lock); 1786 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1787 1788 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1789 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1790 ooa_links))) { 1791 case CTL_ACTION_BLOCK: 1792 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1793 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1794 blocked_links); 1795 break; 1796 case CTL_ACTION_PASS: 1797 case CTL_ACTION_SKIP: 1798 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1799 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1800 ctl_enqueue_rtr((union ctl_io *)ctsio); 1801 } else { 1802 1803 /* send msg back to other side */ 1804 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1805 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1806 msg_info.hdr.msg_type = CTL_MSG_R2R; 1807#if 0 1808 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1809#endif 1810 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1811 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1812 } 1813 } 1814 break; 1815 case CTL_ACTION_OVERLAP: 1816 /* OVERLAPPED COMMANDS ATTEMPTED */ 1817 ctl_set_sense_data(&msg_info.scsi.sense_data, 1818 lun, 1819 /*sense_format*/SSD_TYPE_NONE, 1820 /*current_error*/ 1, 1821 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1822 /*asc*/ 0x4E, 1823 /*ascq*/ 0x00, 1824 SSD_ELEM_NONE); 1825 1826 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1827 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1828 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1829 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1830 msg_info.hdr.serializing_sc = NULL; 1831 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1832#if 0 1833 printf("BAD JUJU:Major Bummer Overlap\n"); 1834#endif 1835 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1836 retval = 1; 1837 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1838 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1839 } 1840 break; 1841 case CTL_ACTION_OVERLAP_TAG: 1842 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1843 ctl_set_sense_data(&msg_info.scsi.sense_data, 1844 lun, 1845 /*sense_format*/SSD_TYPE_NONE, 1846 /*current_error*/ 1, 1847 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1848 /*asc*/ 0x4D, 1849 /*ascq*/ ctsio->tag_num & 0xff, 1850 SSD_ELEM_NONE); 1851 1852 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1853 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1854 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1855 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1856 msg_info.hdr.serializing_sc = NULL; 1857 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1858#if 0 1859 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1860#endif 1861 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1862 retval = 1; 1863 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1864 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1865 } 1866 break; 1867 case CTL_ACTION_ERROR: 1868 default: 1869 /* "Internal target failure" */ 1870 ctl_set_sense_data(&msg_info.scsi.sense_data, 1871 lun, 1872 /*sense_format*/SSD_TYPE_NONE, 1873 /*current_error*/ 1, 1874 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1875 /*asc*/ 0x44, 1876 /*ascq*/ 0x00, 1877 SSD_ELEM_NONE); 1878 1879 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1880 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1881 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1882 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1883 msg_info.hdr.serializing_sc = NULL; 1884 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1885#if 0 1886 printf("BAD JUJU:Major Bummer HW Error\n"); 1887#endif 1888 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1889 retval = 1; 1890 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1891 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1892 } 1893 break; 1894 } 1895 mtx_unlock(&lun->lun_lock); 1896 return (retval); 1897} 1898 1899static int 1900ctl_ioctl_submit_wait(union ctl_io *io) 1901{ 1902 struct ctl_fe_ioctl_params params; 1903 ctl_fe_ioctl_state last_state; 1904 int done, retval; 1905 1906 retval = 0; 1907 1908 bzero(¶ms, sizeof(params)); 1909 1910 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1911 cv_init(¶ms.sem, "ctlioccv"); 1912 params.state = CTL_IOCTL_INPROG; 1913 last_state = params.state; 1914 1915 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1916 1917 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1918 1919 /* This shouldn't happen */ 1920 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1921 return (retval); 1922 1923 done = 0; 1924 1925 do { 1926 mtx_lock(¶ms.ioctl_mtx); 1927 /* 1928 * Check the state here, and don't sleep if the state has 1929 * already changed (i.e. wakeup has already occured, but we 1930 * weren't waiting yet). 1931 */ 1932 if (params.state == last_state) { 1933 /* XXX KDM cv_wait_sig instead? */ 1934 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1935 } 1936 last_state = params.state; 1937 1938 switch (params.state) { 1939 case CTL_IOCTL_INPROG: 1940 /* Why did we wake up? */ 1941 /* XXX KDM error here? */ 1942 mtx_unlock(¶ms.ioctl_mtx); 1943 break; 1944 case CTL_IOCTL_DATAMOVE: 1945 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1946 1947 /* 1948 * change last_state back to INPROG to avoid 1949 * deadlock on subsequent data moves. 1950 */ 1951 params.state = last_state = CTL_IOCTL_INPROG; 1952 1953 mtx_unlock(¶ms.ioctl_mtx); 1954 ctl_ioctl_do_datamove(&io->scsiio); 1955 /* 1956 * Note that in some cases, most notably writes, 1957 * this will queue the I/O and call us back later. 1958 * In other cases, generally reads, this routine 1959 * will immediately call back and wake us up, 1960 * probably using our own context. 1961 */ 1962 io->scsiio.be_move_done(io); 1963 break; 1964 case CTL_IOCTL_DONE: 1965 mtx_unlock(¶ms.ioctl_mtx); 1966 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1967 done = 1; 1968 break; 1969 default: 1970 mtx_unlock(¶ms.ioctl_mtx); 1971 /* XXX KDM error here? */ 1972 break; 1973 } 1974 } while (done == 0); 1975 1976 mtx_destroy(¶ms.ioctl_mtx); 1977 cv_destroy(¶ms.sem); 1978 1979 return (CTL_RETVAL_COMPLETE); 1980} 1981 1982static void 1983ctl_ioctl_datamove(union ctl_io *io) 1984{ 1985 struct ctl_fe_ioctl_params *params; 1986 1987 params = (struct ctl_fe_ioctl_params *) 1988 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1989 1990 mtx_lock(¶ms->ioctl_mtx); 1991 params->state = CTL_IOCTL_DATAMOVE; 1992 cv_broadcast(¶ms->sem); 1993 mtx_unlock(¶ms->ioctl_mtx); 1994} 1995 1996static void 1997ctl_ioctl_done(union ctl_io *io) 1998{ 1999 struct ctl_fe_ioctl_params *params; 2000 2001 params = (struct ctl_fe_ioctl_params *) 2002 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2003 2004 mtx_lock(¶ms->ioctl_mtx); 2005 params->state = CTL_IOCTL_DONE; 2006 cv_broadcast(¶ms->sem); 2007 mtx_unlock(¶ms->ioctl_mtx); 2008} 2009 2010static void 2011ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2012{ 2013 struct ctl_fe_ioctl_startstop_info *sd_info; 2014 2015 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2016 2017 sd_info->hs_info.status = metatask->status; 2018 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2019 sd_info->hs_info.luns_complete = 2020 metatask->taskinfo.startstop.luns_complete; 2021 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2022 2023 cv_broadcast(&sd_info->sem); 2024} 2025 2026static void 2027ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2028{ 2029 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2030 2031 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2032 2033 mtx_lock(fe_bbr_info->lock); 2034 fe_bbr_info->bbr_info->status = metatask->status; 2035 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2036 fe_bbr_info->wakeup_done = 1; 2037 mtx_unlock(fe_bbr_info->lock); 2038 2039 cv_broadcast(&fe_bbr_info->sem); 2040} 2041 2042/* 2043 * Returns 0 for success, errno for failure. 2044 */ 2045static int 2046ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2047 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2048{ 2049 union ctl_io *io; 2050 int retval; 2051 2052 retval = 0; 2053 2054 mtx_lock(&lun->lun_lock); 2055 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2056 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2057 ooa_links)) { 2058 struct ctl_ooa_entry *entry; 2059 2060 /* 2061 * If we've got more than we can fit, just count the 2062 * remaining entries. 2063 */ 2064 if (*cur_fill_num >= ooa_hdr->alloc_num) 2065 continue; 2066 2067 entry = &kern_entries[*cur_fill_num]; 2068 2069 entry->tag_num = io->scsiio.tag_num; 2070 entry->lun_num = lun->lun; 2071#ifdef CTL_TIME_IO 2072 entry->start_bt = io->io_hdr.start_bt; 2073#endif 2074 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2075 entry->cdb_len = io->scsiio.cdb_len; 2076 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2077 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2078 2079 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2080 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2081 2082 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2083 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2084 2085 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2086 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2087 2088 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2089 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2090 } 2091 mtx_unlock(&lun->lun_lock); 2092 2093 return (retval); 2094} 2095 2096static void * 2097ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2098 size_t error_str_len) 2099{ 2100 void *kptr; 2101 2102 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2103 2104 if (copyin(user_addr, kptr, len) != 0) { 2105 snprintf(error_str, error_str_len, "Error copying %d bytes " 2106 "from user address %p to kernel address %p", len, 2107 user_addr, kptr); 2108 free(kptr, M_CTL); 2109 return (NULL); 2110 } 2111 2112 return (kptr); 2113} 2114 2115static void 2116ctl_free_args(int num_args, struct ctl_be_arg *args) 2117{ 2118 int i; 2119 2120 if (args == NULL) 2121 return; 2122 2123 for (i = 0; i < num_args; i++) { 2124 free(args[i].kname, M_CTL); 2125 free(args[i].kvalue, M_CTL); 2126 } 2127 2128 free(args, M_CTL); 2129} 2130 2131static struct ctl_be_arg * 2132ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2133 char *error_str, size_t error_str_len) 2134{ 2135 struct ctl_be_arg *args; 2136 int i; 2137 2138 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2139 error_str, error_str_len); 2140 2141 if (args == NULL) 2142 goto bailout; 2143 2144 for (i = 0; i < num_args; i++) { 2145 args[i].kname = NULL; 2146 args[i].kvalue = NULL; 2147 } 2148 2149 for (i = 0; i < num_args; i++) { 2150 uint8_t *tmpptr; 2151 2152 args[i].kname = ctl_copyin_alloc(args[i].name, 2153 args[i].namelen, error_str, error_str_len); 2154 if (args[i].kname == NULL) 2155 goto bailout; 2156 2157 if (args[i].kname[args[i].namelen - 1] != '\0') { 2158 snprintf(error_str, error_str_len, "Argument %d " 2159 "name is not NUL-terminated", i); 2160 goto bailout; 2161 } 2162 2163 if (args[i].flags & CTL_BEARG_RD) { 2164 tmpptr = ctl_copyin_alloc(args[i].value, 2165 args[i].vallen, error_str, error_str_len); 2166 if (tmpptr == NULL) 2167 goto bailout; 2168 if ((args[i].flags & CTL_BEARG_ASCII) 2169 && (tmpptr[args[i].vallen - 1] != '\0')) { 2170 snprintf(error_str, error_str_len, "Argument " 2171 "%d value is not NUL-terminated", i); 2172 goto bailout; 2173 } 2174 args[i].kvalue = tmpptr; 2175 } else { 2176 args[i].kvalue = malloc(args[i].vallen, 2177 M_CTL, M_WAITOK | M_ZERO); 2178 } 2179 } 2180 2181 return (args); 2182bailout: 2183 2184 ctl_free_args(num_args, args); 2185 2186 return (NULL); 2187} 2188 2189static void 2190ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2191{ 2192 int i; 2193 2194 for (i = 0; i < num_args; i++) { 2195 if (args[i].flags & CTL_BEARG_WR) 2196 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2197 } 2198} 2199 2200/* 2201 * Escape characters that are illegal or not recommended in XML. 2202 */ 2203int 2204ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2205{ 2206 int retval; 2207 2208 retval = 0; 2209 2210 for (; *str; str++) { 2211 switch (*str) { 2212 case '&': 2213 retval = sbuf_printf(sb, "&"); 2214 break; 2215 case '>': 2216 retval = sbuf_printf(sb, ">"); 2217 break; 2218 case '<': 2219 retval = sbuf_printf(sb, "<"); 2220 break; 2221 default: 2222 retval = sbuf_putc(sb, *str); 2223 break; 2224 } 2225 2226 if (retval != 0) 2227 break; 2228 2229 } 2230 2231 return (retval); 2232} 2233 2234static int 2235ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2236 struct thread *td) 2237{ 2238 struct ctl_softc *softc; 2239 int retval; 2240 2241 softc = control_softc; 2242 2243 retval = 0; 2244 2245 switch (cmd) { 2246 case CTL_IO: { 2247 union ctl_io *io; 2248 void *pool_tmp; 2249 2250 /* 2251 * If we haven't been "enabled", don't allow any SCSI I/O 2252 * to this FETD. 2253 */ 2254 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2255 retval = EPERM; 2256 break; 2257 } 2258 2259 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2260 if (io == NULL) { 2261 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2262 retval = ENOSPC; 2263 break; 2264 } 2265 2266 /* 2267 * Need to save the pool reference so it doesn't get 2268 * spammed by the user's ctl_io. 2269 */ 2270 pool_tmp = io->io_hdr.pool; 2271 2272 memcpy(io, (void *)addr, sizeof(*io)); 2273 2274 io->io_hdr.pool = pool_tmp; 2275 /* 2276 * No status yet, so make sure the status is set properly. 2277 */ 2278 io->io_hdr.status = CTL_STATUS_NONE; 2279 2280 /* 2281 * The user sets the initiator ID, target and LUN IDs. 2282 */ 2283 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2284 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2285 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2286 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2287 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2288 2289 retval = ctl_ioctl_submit_wait(io); 2290 2291 if (retval != 0) { 2292 ctl_free_io(io); 2293 break; 2294 } 2295 2296 memcpy((void *)addr, io, sizeof(*io)); 2297 2298 /* return this to our pool */ 2299 ctl_free_io(io); 2300 2301 break; 2302 } 2303 case CTL_ENABLE_PORT: 2304 case CTL_DISABLE_PORT: 2305 case CTL_SET_PORT_WWNS: { 2306 struct ctl_port *port; 2307 struct ctl_port_entry *entry; 2308 2309 entry = (struct ctl_port_entry *)addr; 2310 2311 mtx_lock(&softc->ctl_lock); 2312 STAILQ_FOREACH(port, &softc->port_list, links) { 2313 int action, done; 2314 2315 action = 0; 2316 done = 0; 2317 2318 if ((entry->port_type == CTL_PORT_NONE) 2319 && (entry->targ_port == port->targ_port)) { 2320 /* 2321 * If the user only wants to enable or 2322 * disable or set WWNs on a specific port, 2323 * do the operation and we're done. 2324 */ 2325 action = 1; 2326 done = 1; 2327 } else if (entry->port_type & port->port_type) { 2328 /* 2329 * Compare the user's type mask with the 2330 * particular frontend type to see if we 2331 * have a match. 2332 */ 2333 action = 1; 2334 done = 0; 2335 2336 /* 2337 * Make sure the user isn't trying to set 2338 * WWNs on multiple ports at the same time. 2339 */ 2340 if (cmd == CTL_SET_PORT_WWNS) { 2341 printf("%s: Can't set WWNs on " 2342 "multiple ports\n", __func__); 2343 retval = EINVAL; 2344 break; 2345 } 2346 } 2347 if (action != 0) { 2348 /* 2349 * XXX KDM we have to drop the lock here, 2350 * because the online/offline operations 2351 * can potentially block. We need to 2352 * reference count the frontends so they 2353 * can't go away, 2354 */ 2355 mtx_unlock(&softc->ctl_lock); 2356 2357 if (cmd == CTL_ENABLE_PORT) { 2358 struct ctl_lun *lun; 2359 2360 STAILQ_FOREACH(lun, &softc->lun_list, 2361 links) { 2362 port->lun_enable(port->targ_lun_arg, 2363 lun->target, 2364 lun->lun); 2365 } 2366 2367 ctl_port_online(port); 2368 } else if (cmd == CTL_DISABLE_PORT) { 2369 struct ctl_lun *lun; 2370 2371 ctl_port_offline(port); 2372 2373 STAILQ_FOREACH(lun, &softc->lun_list, 2374 links) { 2375 port->lun_disable( 2376 port->targ_lun_arg, 2377 lun->target, 2378 lun->lun); 2379 } 2380 } 2381 2382 mtx_lock(&softc->ctl_lock); 2383 2384 if (cmd == CTL_SET_PORT_WWNS) 2385 ctl_port_set_wwns(port, 2386 (entry->flags & CTL_PORT_WWNN_VALID) ? 2387 1 : 0, entry->wwnn, 2388 (entry->flags & CTL_PORT_WWPN_VALID) ? 2389 1 : 0, entry->wwpn); 2390 } 2391 if (done != 0) 2392 break; 2393 } 2394 mtx_unlock(&softc->ctl_lock); 2395 break; 2396 } 2397 case CTL_GET_PORT_LIST: { 2398 struct ctl_port *port; 2399 struct ctl_port_list *list; 2400 int i; 2401 2402 list = (struct ctl_port_list *)addr; 2403 2404 if (list->alloc_len != (list->alloc_num * 2405 sizeof(struct ctl_port_entry))) { 2406 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2407 "alloc_num %u * sizeof(struct ctl_port_entry) " 2408 "%zu\n", __func__, list->alloc_len, 2409 list->alloc_num, sizeof(struct ctl_port_entry)); 2410 retval = EINVAL; 2411 break; 2412 } 2413 list->fill_len = 0; 2414 list->fill_num = 0; 2415 list->dropped_num = 0; 2416 i = 0; 2417 mtx_lock(&softc->ctl_lock); 2418 STAILQ_FOREACH(port, &softc->port_list, links) { 2419 struct ctl_port_entry entry, *list_entry; 2420 2421 if (list->fill_num >= list->alloc_num) { 2422 list->dropped_num++; 2423 continue; 2424 } 2425 2426 entry.port_type = port->port_type; 2427 strlcpy(entry.port_name, port->port_name, 2428 sizeof(entry.port_name)); 2429 entry.targ_port = port->targ_port; 2430 entry.physical_port = port->physical_port; 2431 entry.virtual_port = port->virtual_port; 2432 entry.wwnn = port->wwnn; 2433 entry.wwpn = port->wwpn; 2434 if (port->status & CTL_PORT_STATUS_ONLINE) 2435 entry.online = 1; 2436 else 2437 entry.online = 0; 2438 2439 list_entry = &list->entries[i]; 2440 2441 retval = copyout(&entry, list_entry, sizeof(entry)); 2442 if (retval != 0) { 2443 printf("%s: CTL_GET_PORT_LIST: copyout " 2444 "returned %d\n", __func__, retval); 2445 break; 2446 } 2447 i++; 2448 list->fill_num++; 2449 list->fill_len += sizeof(entry); 2450 } 2451 mtx_unlock(&softc->ctl_lock); 2452 2453 /* 2454 * If this is non-zero, we had a copyout fault, so there's 2455 * probably no point in attempting to set the status inside 2456 * the structure. 2457 */ 2458 if (retval != 0) 2459 break; 2460 2461 if (list->dropped_num > 0) 2462 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2463 else 2464 list->status = CTL_PORT_LIST_OK; 2465 break; 2466 } 2467 case CTL_DUMP_OOA: { 2468 struct ctl_lun *lun; 2469 union ctl_io *io; 2470 char printbuf[128]; 2471 struct sbuf sb; 2472 2473 mtx_lock(&softc->ctl_lock); 2474 printf("Dumping OOA queues:\n"); 2475 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2476 mtx_lock(&lun->lun_lock); 2477 for (io = (union ctl_io *)TAILQ_FIRST( 2478 &lun->ooa_queue); io != NULL; 2479 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2480 ooa_links)) { 2481 sbuf_new(&sb, printbuf, sizeof(printbuf), 2482 SBUF_FIXEDLEN); 2483 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2484 (intmax_t)lun->lun, 2485 io->scsiio.tag_num, 2486 (io->io_hdr.flags & 2487 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2488 (io->io_hdr.flags & 2489 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2490 (io->io_hdr.flags & 2491 CTL_FLAG_ABORT) ? " ABORT" : "", 2492 (io->io_hdr.flags & 2493 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2494 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2495 sbuf_finish(&sb); 2496 printf("%s\n", sbuf_data(&sb)); 2497 } 2498 mtx_unlock(&lun->lun_lock); 2499 } 2500 printf("OOA queues dump done\n"); 2501 mtx_unlock(&softc->ctl_lock); 2502 break; 2503 } 2504 case CTL_GET_OOA: { 2505 struct ctl_lun *lun; 2506 struct ctl_ooa *ooa_hdr; 2507 struct ctl_ooa_entry *entries; 2508 uint32_t cur_fill_num; 2509 2510 ooa_hdr = (struct ctl_ooa *)addr; 2511 2512 if ((ooa_hdr->alloc_len == 0) 2513 || (ooa_hdr->alloc_num == 0)) { 2514 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2515 "must be non-zero\n", __func__, 2516 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2517 retval = EINVAL; 2518 break; 2519 } 2520 2521 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2522 sizeof(struct ctl_ooa_entry))) { 2523 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2524 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2525 __func__, ooa_hdr->alloc_len, 2526 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2527 retval = EINVAL; 2528 break; 2529 } 2530 2531 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2532 if (entries == NULL) { 2533 printf("%s: could not allocate %d bytes for OOA " 2534 "dump\n", __func__, ooa_hdr->alloc_len); 2535 retval = ENOMEM; 2536 break; 2537 } 2538 2539 mtx_lock(&softc->ctl_lock); 2540 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2541 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2542 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2543 mtx_unlock(&softc->ctl_lock); 2544 free(entries, M_CTL); 2545 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2546 __func__, (uintmax_t)ooa_hdr->lun_num); 2547 retval = EINVAL; 2548 break; 2549 } 2550 2551 cur_fill_num = 0; 2552 2553 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2554 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2555 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2556 ooa_hdr, entries); 2557 if (retval != 0) 2558 break; 2559 } 2560 if (retval != 0) { 2561 mtx_unlock(&softc->ctl_lock); 2562 free(entries, M_CTL); 2563 break; 2564 } 2565 } else { 2566 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2567 2568 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2569 entries); 2570 } 2571 mtx_unlock(&softc->ctl_lock); 2572 2573 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2574 ooa_hdr->fill_len = ooa_hdr->fill_num * 2575 sizeof(struct ctl_ooa_entry); 2576 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2577 if (retval != 0) { 2578 printf("%s: error copying out %d bytes for OOA dump\n", 2579 __func__, ooa_hdr->fill_len); 2580 } 2581 2582 getbintime(&ooa_hdr->cur_bt); 2583 2584 if (cur_fill_num > ooa_hdr->alloc_num) { 2585 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2586 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2587 } else { 2588 ooa_hdr->dropped_num = 0; 2589 ooa_hdr->status = CTL_OOA_OK; 2590 } 2591 2592 free(entries, M_CTL); 2593 break; 2594 } 2595 case CTL_CHECK_OOA: { 2596 union ctl_io *io; 2597 struct ctl_lun *lun; 2598 struct ctl_ooa_info *ooa_info; 2599 2600 2601 ooa_info = (struct ctl_ooa_info *)addr; 2602 2603 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2604 ooa_info->status = CTL_OOA_INVALID_LUN; 2605 break; 2606 } 2607 mtx_lock(&softc->ctl_lock); 2608 lun = softc->ctl_luns[ooa_info->lun_id]; 2609 if (lun == NULL) { 2610 mtx_unlock(&softc->ctl_lock); 2611 ooa_info->status = CTL_OOA_INVALID_LUN; 2612 break; 2613 } 2614 mtx_lock(&lun->lun_lock); 2615 mtx_unlock(&softc->ctl_lock); 2616 ooa_info->num_entries = 0; 2617 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2618 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2619 &io->io_hdr, ooa_links)) { 2620 ooa_info->num_entries++; 2621 } 2622 mtx_unlock(&lun->lun_lock); 2623 2624 ooa_info->status = CTL_OOA_SUCCESS; 2625 2626 break; 2627 } 2628 case CTL_HARD_START: 2629 case CTL_HARD_STOP: { 2630 struct ctl_fe_ioctl_startstop_info ss_info; 2631 struct cfi_metatask *metatask; 2632 struct mtx hs_mtx; 2633 2634 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2635 2636 cv_init(&ss_info.sem, "hard start/stop cv" ); 2637 2638 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2639 if (metatask == NULL) { 2640 retval = ENOMEM; 2641 mtx_destroy(&hs_mtx); 2642 break; 2643 } 2644 2645 if (cmd == CTL_HARD_START) 2646 metatask->tasktype = CFI_TASK_STARTUP; 2647 else 2648 metatask->tasktype = CFI_TASK_SHUTDOWN; 2649 2650 metatask->callback = ctl_ioctl_hard_startstop_callback; 2651 metatask->callback_arg = &ss_info; 2652 2653 cfi_action(metatask); 2654 2655 /* Wait for the callback */ 2656 mtx_lock(&hs_mtx); 2657 cv_wait_sig(&ss_info.sem, &hs_mtx); 2658 mtx_unlock(&hs_mtx); 2659 2660 /* 2661 * All information has been copied from the metatask by the 2662 * time cv_broadcast() is called, so we free the metatask here. 2663 */ 2664 cfi_free_metatask(metatask); 2665 2666 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2667 2668 mtx_destroy(&hs_mtx); 2669 break; 2670 } 2671 case CTL_BBRREAD: { 2672 struct ctl_bbrread_info *bbr_info; 2673 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2674 struct mtx bbr_mtx; 2675 struct cfi_metatask *metatask; 2676 2677 bbr_info = (struct ctl_bbrread_info *)addr; 2678 2679 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2680 2681 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2682 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2683 2684 fe_bbr_info.bbr_info = bbr_info; 2685 fe_bbr_info.lock = &bbr_mtx; 2686 2687 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2688 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2689 2690 if (metatask == NULL) { 2691 mtx_destroy(&bbr_mtx); 2692 cv_destroy(&fe_bbr_info.sem); 2693 retval = ENOMEM; 2694 break; 2695 } 2696 metatask->tasktype = CFI_TASK_BBRREAD; 2697 metatask->callback = ctl_ioctl_bbrread_callback; 2698 metatask->callback_arg = &fe_bbr_info; 2699 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2700 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2701 metatask->taskinfo.bbrread.len = bbr_info->len; 2702 2703 cfi_action(metatask); 2704 2705 mtx_lock(&bbr_mtx); 2706 while (fe_bbr_info.wakeup_done == 0) 2707 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2708 mtx_unlock(&bbr_mtx); 2709 2710 bbr_info->status = metatask->status; 2711 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2712 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2713 memcpy(&bbr_info->sense_data, 2714 &metatask->taskinfo.bbrread.sense_data, 2715 ctl_min(sizeof(bbr_info->sense_data), 2716 sizeof(metatask->taskinfo.bbrread.sense_data))); 2717 2718 cfi_free_metatask(metatask); 2719 2720 mtx_destroy(&bbr_mtx); 2721 cv_destroy(&fe_bbr_info.sem); 2722 2723 break; 2724 } 2725 case CTL_DELAY_IO: { 2726 struct ctl_io_delay_info *delay_info; 2727#ifdef CTL_IO_DELAY 2728 struct ctl_lun *lun; 2729#endif /* CTL_IO_DELAY */ 2730 2731 delay_info = (struct ctl_io_delay_info *)addr; 2732 2733#ifdef CTL_IO_DELAY 2734 mtx_lock(&softc->ctl_lock); 2735 2736 if ((delay_info->lun_id > CTL_MAX_LUNS) 2737 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2738 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2739 } else { 2740 lun = softc->ctl_luns[delay_info->lun_id]; 2741 mtx_lock(&lun->lun_lock); 2742 2743 delay_info->status = CTL_DELAY_STATUS_OK; 2744 2745 switch (delay_info->delay_type) { 2746 case CTL_DELAY_TYPE_CONT: 2747 break; 2748 case CTL_DELAY_TYPE_ONESHOT: 2749 break; 2750 default: 2751 delay_info->status = 2752 CTL_DELAY_STATUS_INVALID_TYPE; 2753 break; 2754 } 2755 2756 switch (delay_info->delay_loc) { 2757 case CTL_DELAY_LOC_DATAMOVE: 2758 lun->delay_info.datamove_type = 2759 delay_info->delay_type; 2760 lun->delay_info.datamove_delay = 2761 delay_info->delay_secs; 2762 break; 2763 case CTL_DELAY_LOC_DONE: 2764 lun->delay_info.done_type = 2765 delay_info->delay_type; 2766 lun->delay_info.done_delay = 2767 delay_info->delay_secs; 2768 break; 2769 default: 2770 delay_info->status = 2771 CTL_DELAY_STATUS_INVALID_LOC; 2772 break; 2773 } 2774 mtx_unlock(&lun->lun_lock); 2775 } 2776 2777 mtx_unlock(&softc->ctl_lock); 2778#else 2779 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2780#endif /* CTL_IO_DELAY */ 2781 break; 2782 } 2783 case CTL_REALSYNC_SET: { 2784 int *syncstate; 2785 2786 syncstate = (int *)addr; 2787 2788 mtx_lock(&softc->ctl_lock); 2789 switch (*syncstate) { 2790 case 0: 2791 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2792 break; 2793 case 1: 2794 softc->flags |= CTL_FLAG_REAL_SYNC; 2795 break; 2796 default: 2797 retval = EINVAL; 2798 break; 2799 } 2800 mtx_unlock(&softc->ctl_lock); 2801 break; 2802 } 2803 case CTL_REALSYNC_GET: { 2804 int *syncstate; 2805 2806 syncstate = (int*)addr; 2807 2808 mtx_lock(&softc->ctl_lock); 2809 if (softc->flags & CTL_FLAG_REAL_SYNC) 2810 *syncstate = 1; 2811 else 2812 *syncstate = 0; 2813 mtx_unlock(&softc->ctl_lock); 2814 2815 break; 2816 } 2817 case CTL_SETSYNC: 2818 case CTL_GETSYNC: { 2819 struct ctl_sync_info *sync_info; 2820 struct ctl_lun *lun; 2821 2822 sync_info = (struct ctl_sync_info *)addr; 2823 2824 mtx_lock(&softc->ctl_lock); 2825 lun = softc->ctl_luns[sync_info->lun_id]; 2826 if (lun == NULL) { 2827 mtx_unlock(&softc->ctl_lock); 2828 sync_info->status = CTL_GS_SYNC_NO_LUN; 2829 } 2830 /* 2831 * Get or set the sync interval. We're not bounds checking 2832 * in the set case, hopefully the user won't do something 2833 * silly. 2834 */ 2835 mtx_lock(&lun->lun_lock); 2836 mtx_unlock(&softc->ctl_lock); 2837 if (cmd == CTL_GETSYNC) 2838 sync_info->sync_interval = lun->sync_interval; 2839 else 2840 lun->sync_interval = sync_info->sync_interval; 2841 mtx_unlock(&lun->lun_lock); 2842 2843 sync_info->status = CTL_GS_SYNC_OK; 2844 2845 break; 2846 } 2847 case CTL_GETSTATS: { 2848 struct ctl_stats *stats; 2849 struct ctl_lun *lun; 2850 int i; 2851 2852 stats = (struct ctl_stats *)addr; 2853 2854 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2855 stats->alloc_len) { 2856 stats->status = CTL_SS_NEED_MORE_SPACE; 2857 stats->num_luns = softc->num_luns; 2858 break; 2859 } 2860 /* 2861 * XXX KDM no locking here. If the LUN list changes, 2862 * things can blow up. 2863 */ 2864 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2865 i++, lun = STAILQ_NEXT(lun, links)) { 2866 retval = copyout(&lun->stats, &stats->lun_stats[i], 2867 sizeof(lun->stats)); 2868 if (retval != 0) 2869 break; 2870 } 2871 stats->num_luns = softc->num_luns; 2872 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2873 softc->num_luns; 2874 stats->status = CTL_SS_OK; 2875#ifdef CTL_TIME_IO 2876 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2877#else 2878 stats->flags = CTL_STATS_FLAG_NONE; 2879#endif 2880 getnanouptime(&stats->timestamp); 2881 break; 2882 } 2883 case CTL_ERROR_INJECT: { 2884 struct ctl_error_desc *err_desc, *new_err_desc; 2885 struct ctl_lun *lun; 2886 2887 err_desc = (struct ctl_error_desc *)addr; 2888 2889 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2890 M_WAITOK | M_ZERO); 2891 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2892 2893 mtx_lock(&softc->ctl_lock); 2894 lun = softc->ctl_luns[err_desc->lun_id]; 2895 if (lun == NULL) { 2896 mtx_unlock(&softc->ctl_lock); 2897 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2898 __func__, (uintmax_t)err_desc->lun_id); 2899 retval = EINVAL; 2900 break; 2901 } 2902 mtx_lock(&lun->lun_lock); 2903 mtx_unlock(&softc->ctl_lock); 2904 2905 /* 2906 * We could do some checking here to verify the validity 2907 * of the request, but given the complexity of error 2908 * injection requests, the checking logic would be fairly 2909 * complex. 2910 * 2911 * For now, if the request is invalid, it just won't get 2912 * executed and might get deleted. 2913 */ 2914 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2915 2916 /* 2917 * XXX KDM check to make sure the serial number is unique, 2918 * in case we somehow manage to wrap. That shouldn't 2919 * happen for a very long time, but it's the right thing to 2920 * do. 2921 */ 2922 new_err_desc->serial = lun->error_serial; 2923 err_desc->serial = lun->error_serial; 2924 lun->error_serial++; 2925 2926 mtx_unlock(&lun->lun_lock); 2927 break; 2928 } 2929 case CTL_ERROR_INJECT_DELETE: { 2930 struct ctl_error_desc *delete_desc, *desc, *desc2; 2931 struct ctl_lun *lun; 2932 int delete_done; 2933 2934 delete_desc = (struct ctl_error_desc *)addr; 2935 delete_done = 0; 2936 2937 mtx_lock(&softc->ctl_lock); 2938 lun = softc->ctl_luns[delete_desc->lun_id]; 2939 if (lun == NULL) { 2940 mtx_unlock(&softc->ctl_lock); 2941 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2942 __func__, (uintmax_t)delete_desc->lun_id); 2943 retval = EINVAL; 2944 break; 2945 } 2946 mtx_lock(&lun->lun_lock); 2947 mtx_unlock(&softc->ctl_lock); 2948 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2949 if (desc->serial != delete_desc->serial) 2950 continue; 2951 2952 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2953 links); 2954 free(desc, M_CTL); 2955 delete_done = 1; 2956 } 2957 mtx_unlock(&lun->lun_lock); 2958 if (delete_done == 0) { 2959 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2960 "error serial %ju on LUN %u\n", __func__, 2961 delete_desc->serial, delete_desc->lun_id); 2962 retval = EINVAL; 2963 break; 2964 } 2965 break; 2966 } 2967 case CTL_DUMP_STRUCTS: { 2968 int i, j, k, idx; 2969 struct ctl_port *port; 2970 struct ctl_frontend *fe; 2971 2972 mtx_lock(&softc->ctl_lock); 2973 printf("CTL Persistent Reservation information start:\n"); 2974 for (i = 0; i < CTL_MAX_LUNS; i++) { 2975 struct ctl_lun *lun; 2976 2977 lun = softc->ctl_luns[i]; 2978 2979 if ((lun == NULL) 2980 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2981 continue; 2982 2983 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2984 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2985 idx = j * CTL_MAX_INIT_PER_PORT + k; 2986 if (lun->per_res[idx].registered == 0) 2987 continue; 2988 printf(" LUN %d port %d iid %d key " 2989 "%#jx\n", i, j, k, 2990 (uintmax_t)scsi_8btou64( 2991 lun->per_res[idx].res_key.key)); 2992 } 2993 } 2994 } 2995 printf("CTL Persistent Reservation information end\n"); 2996 printf("CTL Ports:\n"); 2997 STAILQ_FOREACH(port, &softc->port_list, links) { 2998 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2999 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3000 port->frontend->name, port->port_type, 3001 port->physical_port, port->virtual_port, 3002 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3003 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3004 if (port->wwpn_iid[j].in_use == 0 && 3005 port->wwpn_iid[j].wwpn == 0 && 3006 port->wwpn_iid[j].name == NULL) 3007 continue; 3008 3009 printf(" iid %u use %d WWPN %#jx '%s'\n", 3010 j, port->wwpn_iid[j].in_use, 3011 (uintmax_t)port->wwpn_iid[j].wwpn, 3012 port->wwpn_iid[j].name); 3013 } 3014 } 3015 printf("CTL Port information end\n"); 3016 mtx_unlock(&softc->ctl_lock); 3017 /* 3018 * XXX KDM calling this without a lock. We'd likely want 3019 * to drop the lock before calling the frontend's dump 3020 * routine anyway. 3021 */ 3022 printf("CTL Frontends:\n"); 3023 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3024 printf(" Frontend '%s'\n", fe->name); 3025 if (fe->fe_dump != NULL) 3026 fe->fe_dump(); 3027 } 3028 printf("CTL Frontend information end\n"); 3029 break; 3030 } 3031 case CTL_LUN_REQ: { 3032 struct ctl_lun_req *lun_req; 3033 struct ctl_backend_driver *backend; 3034 3035 lun_req = (struct ctl_lun_req *)addr; 3036 3037 backend = ctl_backend_find(lun_req->backend); 3038 if (backend == NULL) { 3039 lun_req->status = CTL_LUN_ERROR; 3040 snprintf(lun_req->error_str, 3041 sizeof(lun_req->error_str), 3042 "Backend \"%s\" not found.", 3043 lun_req->backend); 3044 break; 3045 } 3046 if (lun_req->num_be_args > 0) { 3047 lun_req->kern_be_args = ctl_copyin_args( 3048 lun_req->num_be_args, 3049 lun_req->be_args, 3050 lun_req->error_str, 3051 sizeof(lun_req->error_str)); 3052 if (lun_req->kern_be_args == NULL) { 3053 lun_req->status = CTL_LUN_ERROR; 3054 break; 3055 } 3056 } 3057 3058 retval = backend->ioctl(dev, cmd, addr, flag, td); 3059 3060 if (lun_req->num_be_args > 0) { 3061 ctl_copyout_args(lun_req->num_be_args, 3062 lun_req->kern_be_args); 3063 ctl_free_args(lun_req->num_be_args, 3064 lun_req->kern_be_args); 3065 } 3066 break; 3067 } 3068 case CTL_LUN_LIST: { 3069 struct sbuf *sb; 3070 struct ctl_lun *lun; 3071 struct ctl_lun_list *list; 3072 struct ctl_option *opt; 3073 3074 list = (struct ctl_lun_list *)addr; 3075 3076 /* 3077 * Allocate a fixed length sbuf here, based on the length 3078 * of the user's buffer. We could allocate an auto-extending 3079 * buffer, and then tell the user how much larger our 3080 * amount of data is than his buffer, but that presents 3081 * some problems: 3082 * 3083 * 1. The sbuf(9) routines use a blocking malloc, and so 3084 * we can't hold a lock while calling them with an 3085 * auto-extending buffer. 3086 * 3087 * 2. There is not currently a LUN reference counting 3088 * mechanism, outside of outstanding transactions on 3089 * the LUN's OOA queue. So a LUN could go away on us 3090 * while we're getting the LUN number, backend-specific 3091 * information, etc. Thus, given the way things 3092 * currently work, we need to hold the CTL lock while 3093 * grabbing LUN information. 3094 * 3095 * So, from the user's standpoint, the best thing to do is 3096 * allocate what he thinks is a reasonable buffer length, 3097 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3098 * double the buffer length and try again. (And repeat 3099 * that until he succeeds.) 3100 */ 3101 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3102 if (sb == NULL) { 3103 list->status = CTL_LUN_LIST_ERROR; 3104 snprintf(list->error_str, sizeof(list->error_str), 3105 "Unable to allocate %d bytes for LUN list", 3106 list->alloc_len); 3107 break; 3108 } 3109 3110 sbuf_printf(sb, "<ctllunlist>\n"); 3111 3112 mtx_lock(&softc->ctl_lock); 3113 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3114 mtx_lock(&lun->lun_lock); 3115 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3116 (uintmax_t)lun->lun); 3117 3118 /* 3119 * Bail out as soon as we see that we've overfilled 3120 * the buffer. 3121 */ 3122 if (retval != 0) 3123 break; 3124 3125 retval = sbuf_printf(sb, "\t<backend_type>%s" 3126 "</backend_type>\n", 3127 (lun->backend == NULL) ? "none" : 3128 lun->backend->name); 3129 3130 if (retval != 0) 3131 break; 3132 3133 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3134 lun->be_lun->lun_type); 3135 3136 if (retval != 0) 3137 break; 3138 3139 if (lun->backend == NULL) { 3140 retval = sbuf_printf(sb, "</lun>\n"); 3141 if (retval != 0) 3142 break; 3143 continue; 3144 } 3145 3146 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3147 (lun->be_lun->maxlba > 0) ? 3148 lun->be_lun->maxlba + 1 : 0); 3149 3150 if (retval != 0) 3151 break; 3152 3153 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3154 lun->be_lun->blocksize); 3155 3156 if (retval != 0) 3157 break; 3158 3159 retval = sbuf_printf(sb, "\t<serial_number>"); 3160 3161 if (retval != 0) 3162 break; 3163 3164 retval = ctl_sbuf_printf_esc(sb, 3165 lun->be_lun->serial_num); 3166 3167 if (retval != 0) 3168 break; 3169 3170 retval = sbuf_printf(sb, "</serial_number>\n"); 3171 3172 if (retval != 0) 3173 break; 3174 3175 retval = sbuf_printf(sb, "\t<device_id>"); 3176 3177 if (retval != 0) 3178 break; 3179 3180 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3181 3182 if (retval != 0) 3183 break; 3184 3185 retval = sbuf_printf(sb, "</device_id>\n"); 3186 3187 if (retval != 0) 3188 break; 3189 3190 if (lun->backend->lun_info != NULL) { 3191 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3192 if (retval != 0) 3193 break; 3194 } 3195 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3196 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3197 opt->name, opt->value, opt->name); 3198 if (retval != 0) 3199 break; 3200 } 3201 3202 retval = sbuf_printf(sb, "</lun>\n"); 3203 3204 if (retval != 0) 3205 break; 3206 mtx_unlock(&lun->lun_lock); 3207 } 3208 if (lun != NULL) 3209 mtx_unlock(&lun->lun_lock); 3210 mtx_unlock(&softc->ctl_lock); 3211 3212 if ((retval != 0) 3213 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3214 retval = 0; 3215 sbuf_delete(sb); 3216 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3217 snprintf(list->error_str, sizeof(list->error_str), 3218 "Out of space, %d bytes is too small", 3219 list->alloc_len); 3220 break; 3221 } 3222 3223 sbuf_finish(sb); 3224 3225 retval = copyout(sbuf_data(sb), list->lun_xml, 3226 sbuf_len(sb) + 1); 3227 3228 list->fill_len = sbuf_len(sb) + 1; 3229 list->status = CTL_LUN_LIST_OK; 3230 sbuf_delete(sb); 3231 break; 3232 } 3233 case CTL_ISCSI: { 3234 struct ctl_iscsi *ci; 3235 struct ctl_frontend *fe; 3236 3237 ci = (struct ctl_iscsi *)addr; 3238 3239 fe = ctl_frontend_find("iscsi"); 3240 if (fe == NULL) { 3241 ci->status = CTL_ISCSI_ERROR; 3242 snprintf(ci->error_str, sizeof(ci->error_str), 3243 "Frontend \"iscsi\" not found."); 3244 break; 3245 } 3246 3247 retval = fe->ioctl(dev, cmd, addr, flag, td); 3248 break; 3249 } 3250 case CTL_PORT_REQ: { 3251 struct ctl_req *req; 3252 struct ctl_frontend *fe; 3253 3254 req = (struct ctl_req *)addr; 3255 3256 fe = ctl_frontend_find(req->driver); 3257 if (fe == NULL) { 3258 req->status = CTL_LUN_ERROR; 3259 snprintf(req->error_str, sizeof(req->error_str), 3260 "Frontend \"%s\" not found.", req->driver); 3261 break; 3262 } 3263 if (req->num_args > 0) { 3264 req->kern_args = ctl_copyin_args(req->num_args, 3265 req->args, req->error_str, sizeof(req->error_str)); 3266 if (req->kern_args == NULL) { 3267 req->status = CTL_LUN_ERROR; 3268 break; 3269 } 3270 } 3271 3272 retval = fe->ioctl(dev, cmd, addr, flag, td); 3273 3274 if (req->num_args > 0) { 3275 ctl_copyout_args(req->num_args, req->kern_args); 3276 ctl_free_args(req->num_args, req->kern_args); 3277 } 3278 break; 3279 } 3280 case CTL_PORT_LIST: { 3281 struct sbuf *sb; 3282 struct ctl_port *port; 3283 struct ctl_lun_list *list; 3284 struct ctl_option *opt; 3285 3286 list = (struct ctl_lun_list *)addr; 3287 3288 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3289 if (sb == NULL) { 3290 list->status = CTL_LUN_LIST_ERROR; 3291 snprintf(list->error_str, sizeof(list->error_str), 3292 "Unable to allocate %d bytes for LUN list", 3293 list->alloc_len); 3294 break; 3295 } 3296 3297 sbuf_printf(sb, "<ctlportlist>\n"); 3298 3299 mtx_lock(&softc->ctl_lock); 3300 STAILQ_FOREACH(port, &softc->port_list, links) { 3301 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3302 (uintmax_t)port->targ_port); 3303 3304 /* 3305 * Bail out as soon as we see that we've overfilled 3306 * the buffer. 3307 */ 3308 if (retval != 0) 3309 break; 3310 3311 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3312 "</frontend_type>\n", port->frontend->name); 3313 if (retval != 0) 3314 break; 3315 3316 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3317 port->port_type); 3318 if (retval != 0) 3319 break; 3320 3321 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3322 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3323 if (retval != 0) 3324 break; 3325 3326 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3327 port->port_name); 3328 if (retval != 0) 3329 break; 3330 3331 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3332 port->physical_port); 3333 if (retval != 0) 3334 break; 3335 3336 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3337 port->virtual_port); 3338 if (retval != 0) 3339 break; 3340 3341 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3342 (uintmax_t)port->wwnn); 3343 if (retval != 0) 3344 break; 3345 3346 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3347 (uintmax_t)port->wwpn); 3348 if (retval != 0) 3349 break; 3350 3351 if (port->port_info != NULL) { 3352 retval = port->port_info(port->onoff_arg, sb); 3353 if (retval != 0) 3354 break; 3355 } 3356 STAILQ_FOREACH(opt, &port->options, links) { 3357 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3358 opt->name, opt->value, opt->name); 3359 if (retval != 0) 3360 break; 3361 } 3362 3363 retval = sbuf_printf(sb, "</targ_port>\n"); 3364 if (retval != 0) 3365 break; 3366 } 3367 mtx_unlock(&softc->ctl_lock); 3368 3369 if ((retval != 0) 3370 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3371 retval = 0; 3372 sbuf_delete(sb); 3373 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3374 snprintf(list->error_str, sizeof(list->error_str), 3375 "Out of space, %d bytes is too small", 3376 list->alloc_len); 3377 break; 3378 } 3379 3380 sbuf_finish(sb); 3381 3382 retval = copyout(sbuf_data(sb), list->lun_xml, 3383 sbuf_len(sb) + 1); 3384 3385 list->fill_len = sbuf_len(sb) + 1; 3386 list->status = CTL_LUN_LIST_OK; 3387 sbuf_delete(sb); 3388 break; 3389 } 3390 default: { 3391 /* XXX KDM should we fix this? */ 3392#if 0 3393 struct ctl_backend_driver *backend; 3394 unsigned int type; 3395 int found; 3396 3397 found = 0; 3398 3399 /* 3400 * We encode the backend type as the ioctl type for backend 3401 * ioctls. So parse it out here, and then search for a 3402 * backend of this type. 3403 */ 3404 type = _IOC_TYPE(cmd); 3405 3406 STAILQ_FOREACH(backend, &softc->be_list, links) { 3407 if (backend->type == type) { 3408 found = 1; 3409 break; 3410 } 3411 } 3412 if (found == 0) { 3413 printf("ctl: unknown ioctl command %#lx or backend " 3414 "%d\n", cmd, type); 3415 retval = EINVAL; 3416 break; 3417 } 3418 retval = backend->ioctl(dev, cmd, addr, flag, td); 3419#endif 3420 retval = ENOTTY; 3421 break; 3422 } 3423 } 3424 return (retval); 3425} 3426 3427uint32_t 3428ctl_get_initindex(struct ctl_nexus *nexus) 3429{ 3430 if (nexus->targ_port < CTL_MAX_PORTS) 3431 return (nexus->initid.id + 3432 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3433 else 3434 return (nexus->initid.id + 3435 ((nexus->targ_port - CTL_MAX_PORTS) * 3436 CTL_MAX_INIT_PER_PORT)); 3437} 3438 3439uint32_t 3440ctl_get_resindex(struct ctl_nexus *nexus) 3441{ 3442 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3443} 3444 3445uint32_t 3446ctl_port_idx(int port_num) 3447{ 3448 if (port_num < CTL_MAX_PORTS) 3449 return(port_num); 3450 else 3451 return(port_num - CTL_MAX_PORTS); 3452} 3453 3454static uint32_t 3455ctl_map_lun(int port_num, uint32_t lun_id) 3456{ 3457 struct ctl_port *port; 3458 3459 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3460 if (port == NULL) 3461 return (UINT32_MAX); 3462 if (port->lun_map == NULL) 3463 return (lun_id); 3464 return (port->lun_map(port->targ_lun_arg, lun_id)); 3465} 3466 3467static uint32_t 3468ctl_map_lun_back(int port_num, uint32_t lun_id) 3469{ 3470 struct ctl_port *port; 3471 uint32_t i; 3472 3473 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3474 if (port->lun_map == NULL) 3475 return (lun_id); 3476 for (i = 0; i < CTL_MAX_LUNS; i++) { 3477 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3478 return (i); 3479 } 3480 return (UINT32_MAX); 3481} 3482 3483/* 3484 * Note: This only works for bitmask sizes that are at least 32 bits, and 3485 * that are a power of 2. 3486 */ 3487int 3488ctl_ffz(uint32_t *mask, uint32_t size) 3489{ 3490 uint32_t num_chunks, num_pieces; 3491 int i, j; 3492 3493 num_chunks = (size >> 5); 3494 if (num_chunks == 0) 3495 num_chunks++; 3496 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3497 3498 for (i = 0; i < num_chunks; i++) { 3499 for (j = 0; j < num_pieces; j++) { 3500 if ((mask[i] & (1 << j)) == 0) 3501 return ((i << 5) + j); 3502 } 3503 } 3504 3505 return (-1); 3506} 3507 3508int 3509ctl_set_mask(uint32_t *mask, uint32_t bit) 3510{ 3511 uint32_t chunk, piece; 3512 3513 chunk = bit >> 5; 3514 piece = bit % (sizeof(uint32_t) * 8); 3515 3516 if ((mask[chunk] & (1 << piece)) != 0) 3517 return (-1); 3518 else 3519 mask[chunk] |= (1 << piece); 3520 3521 return (0); 3522} 3523 3524int 3525ctl_clear_mask(uint32_t *mask, uint32_t bit) 3526{ 3527 uint32_t chunk, piece; 3528 3529 chunk = bit >> 5; 3530 piece = bit % (sizeof(uint32_t) * 8); 3531 3532 if ((mask[chunk] & (1 << piece)) == 0) 3533 return (-1); 3534 else 3535 mask[chunk] &= ~(1 << piece); 3536 3537 return (0); 3538} 3539 3540int 3541ctl_is_set(uint32_t *mask, uint32_t bit) 3542{ 3543 uint32_t chunk, piece; 3544 3545 chunk = bit >> 5; 3546 piece = bit % (sizeof(uint32_t) * 8); 3547 3548 if ((mask[chunk] & (1 << piece)) == 0) 3549 return (0); 3550 else 3551 return (1); 3552} 3553 3554#ifdef unused 3555/* 3556 * The bus, target and lun are optional, they can be filled in later. 3557 * can_wait is used to determine whether we can wait on the malloc or not. 3558 */ 3559union ctl_io* 3560ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3561 uint32_t targ_lun, int can_wait) 3562{ 3563 union ctl_io *io; 3564 3565 if (can_wait) 3566 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3567 else 3568 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3569 3570 if (io != NULL) { 3571 io->io_hdr.io_type = io_type; 3572 io->io_hdr.targ_port = targ_port; 3573 /* 3574 * XXX KDM this needs to change/go away. We need to move 3575 * to a preallocated pool of ctl_scsiio structures. 3576 */ 3577 io->io_hdr.nexus.targ_target.id = targ_target; 3578 io->io_hdr.nexus.targ_lun = targ_lun; 3579 } 3580 3581 return (io); 3582} 3583 3584void 3585ctl_kfree_io(union ctl_io *io) 3586{ 3587 free(io, M_CTL); 3588} 3589#endif /* unused */ 3590 3591/* 3592 * ctl_softc, pool_type, total_ctl_io are passed in. 3593 * npool is passed out. 3594 */ 3595int 3596ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3597 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3598{ 3599 uint32_t i; 3600 union ctl_io *cur_io, *next_io; 3601 struct ctl_io_pool *pool; 3602 int retval; 3603 3604 retval = 0; 3605 3606 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3607 M_NOWAIT | M_ZERO); 3608 if (pool == NULL) { 3609 retval = ENOMEM; 3610 goto bailout; 3611 } 3612 3613 pool->type = pool_type; 3614 pool->ctl_softc = ctl_softc; 3615 3616 mtx_lock(&ctl_softc->pool_lock); 3617 pool->id = ctl_softc->cur_pool_id++; 3618 mtx_unlock(&ctl_softc->pool_lock); 3619 3620 pool->flags = CTL_POOL_FLAG_NONE; 3621 pool->refcount = 1; /* Reference for validity. */ 3622 STAILQ_INIT(&pool->free_queue); 3623 3624 /* 3625 * XXX KDM other options here: 3626 * - allocate a page at a time 3627 * - allocate one big chunk of memory. 3628 * Page allocation might work well, but would take a little more 3629 * tracking. 3630 */ 3631 for (i = 0; i < total_ctl_io; i++) { 3632 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3633 M_NOWAIT); 3634 if (cur_io == NULL) { 3635 retval = ENOMEM; 3636 break; 3637 } 3638 cur_io->io_hdr.pool = pool; 3639 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3640 pool->total_ctl_io++; 3641 pool->free_ctl_io++; 3642 } 3643 3644 if (retval != 0) { 3645 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3646 cur_io != NULL; cur_io = next_io) { 3647 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3648 links); 3649 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3650 ctl_io_hdr, links); 3651 free(cur_io, M_CTLIO); 3652 } 3653 3654 free(pool, M_CTL); 3655 goto bailout; 3656 } 3657 mtx_lock(&ctl_softc->pool_lock); 3658 ctl_softc->num_pools++; 3659 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3660 /* 3661 * Increment our usage count if this is an external consumer, so we 3662 * can't get unloaded until the external consumer (most likely a 3663 * FETD) unloads and frees his pool. 3664 * 3665 * XXX KDM will this increment the caller's module use count, or 3666 * mine? 3667 */ 3668#if 0 3669 if ((pool_type != CTL_POOL_EMERGENCY) 3670 && (pool_type != CTL_POOL_INTERNAL) 3671 && (pool_type != CTL_POOL_4OTHERSC)) 3672 MOD_INC_USE_COUNT; 3673#endif 3674 3675 mtx_unlock(&ctl_softc->pool_lock); 3676 3677 *npool = pool; 3678 3679bailout: 3680 3681 return (retval); 3682} 3683 3684static int 3685ctl_pool_acquire(struct ctl_io_pool *pool) 3686{ 3687 3688 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3689 3690 if (pool->flags & CTL_POOL_FLAG_INVALID) 3691 return (EINVAL); 3692 3693 pool->refcount++; 3694 3695 return (0); 3696} 3697 3698static void 3699ctl_pool_release(struct ctl_io_pool *pool) 3700{ 3701 struct ctl_softc *ctl_softc = pool->ctl_softc; 3702 union ctl_io *io; 3703 3704 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3705 3706 if (--pool->refcount != 0) 3707 return; 3708 3709 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3710 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3711 links); 3712 free(io, M_CTLIO); 3713 } 3714 3715 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3716 ctl_softc->num_pools--; 3717 3718 /* 3719 * XXX KDM will this decrement the caller's usage count or mine? 3720 */ 3721#if 0 3722 if ((pool->type != CTL_POOL_EMERGENCY) 3723 && (pool->type != CTL_POOL_INTERNAL) 3724 && (pool->type != CTL_POOL_4OTHERSC)) 3725 MOD_DEC_USE_COUNT; 3726#endif 3727 3728 free(pool, M_CTL); 3729} 3730 3731void 3732ctl_pool_free(struct ctl_io_pool *pool) 3733{ 3734 struct ctl_softc *ctl_softc; 3735 3736 if (pool == NULL) 3737 return; 3738 3739 ctl_softc = pool->ctl_softc; 3740 mtx_lock(&ctl_softc->pool_lock); 3741 pool->flags |= CTL_POOL_FLAG_INVALID; 3742 ctl_pool_release(pool); 3743 mtx_unlock(&ctl_softc->pool_lock); 3744} 3745 3746/* 3747 * This routine does not block (except for spinlocks of course). 3748 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3749 * possible. 3750 */ 3751union ctl_io * 3752ctl_alloc_io(void *pool_ref) 3753{ 3754 union ctl_io *io; 3755 struct ctl_softc *ctl_softc; 3756 struct ctl_io_pool *pool, *npool; 3757 struct ctl_io_pool *emergency_pool; 3758 3759 pool = (struct ctl_io_pool *)pool_ref; 3760 3761 if (pool == NULL) { 3762 printf("%s: pool is NULL\n", __func__); 3763 return (NULL); 3764 } 3765 3766 emergency_pool = NULL; 3767 3768 ctl_softc = pool->ctl_softc; 3769 3770 mtx_lock(&ctl_softc->pool_lock); 3771 /* 3772 * First, try to get the io structure from the user's pool. 3773 */ 3774 if (ctl_pool_acquire(pool) == 0) { 3775 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3776 if (io != NULL) { 3777 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3778 pool->total_allocated++; 3779 pool->free_ctl_io--; 3780 mtx_unlock(&ctl_softc->pool_lock); 3781 return (io); 3782 } else 3783 ctl_pool_release(pool); 3784 } 3785 /* 3786 * If he doesn't have any io structures left, search for an 3787 * emergency pool and grab one from there. 3788 */ 3789 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3790 if (npool->type != CTL_POOL_EMERGENCY) 3791 continue; 3792 3793 if (ctl_pool_acquire(npool) != 0) 3794 continue; 3795 3796 emergency_pool = npool; 3797 3798 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3799 if (io != NULL) { 3800 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3801 npool->total_allocated++; 3802 npool->free_ctl_io--; 3803 mtx_unlock(&ctl_softc->pool_lock); 3804 return (io); 3805 } else 3806 ctl_pool_release(npool); 3807 } 3808 3809 /* Drop the spinlock before we malloc */ 3810 mtx_unlock(&ctl_softc->pool_lock); 3811 3812 /* 3813 * The emergency pool (if it exists) didn't have one, so try an 3814 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3815 */ 3816 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3817 if (io != NULL) { 3818 /* 3819 * If the emergency pool exists but is empty, add this 3820 * ctl_io to its list when it gets freed. 3821 */ 3822 if (emergency_pool != NULL) { 3823 mtx_lock(&ctl_softc->pool_lock); 3824 if (ctl_pool_acquire(emergency_pool) == 0) { 3825 io->io_hdr.pool = emergency_pool; 3826 emergency_pool->total_ctl_io++; 3827 /* 3828 * Need to bump this, otherwise 3829 * total_allocated and total_freed won't 3830 * match when we no longer have anything 3831 * outstanding. 3832 */ 3833 emergency_pool->total_allocated++; 3834 } 3835 mtx_unlock(&ctl_softc->pool_lock); 3836 } else 3837 io->io_hdr.pool = NULL; 3838 } 3839 3840 return (io); 3841} 3842 3843void 3844ctl_free_io(union ctl_io *io) 3845{ 3846 if (io == NULL) 3847 return; 3848 3849 /* 3850 * If this ctl_io has a pool, return it to that pool. 3851 */ 3852 if (io->io_hdr.pool != NULL) { 3853 struct ctl_io_pool *pool; 3854 3855 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3856 mtx_lock(&pool->ctl_softc->pool_lock); 3857 io->io_hdr.io_type = 0xff; 3858 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3859 pool->total_freed++; 3860 pool->free_ctl_io++; 3861 ctl_pool_release(pool); 3862 mtx_unlock(&pool->ctl_softc->pool_lock); 3863 } else { 3864 /* 3865 * Otherwise, just free it. We probably malloced it and 3866 * the emergency pool wasn't available. 3867 */ 3868 free(io, M_CTLIO); 3869 } 3870 3871} 3872 3873void 3874ctl_zero_io(union ctl_io *io) 3875{ 3876 void *pool_ref; 3877 3878 if (io == NULL) 3879 return; 3880 3881 /* 3882 * May need to preserve linked list pointers at some point too. 3883 */ 3884 pool_ref = io->io_hdr.pool; 3885 3886 memset(io, 0, sizeof(*io)); 3887 3888 io->io_hdr.pool = pool_ref; 3889} 3890 3891/* 3892 * This routine is currently used for internal copies of ctl_ios that need 3893 * to persist for some reason after we've already returned status to the 3894 * FETD. (Thus the flag set.) 3895 * 3896 * XXX XXX 3897 * Note that this makes a blind copy of all fields in the ctl_io, except 3898 * for the pool reference. This includes any memory that has been 3899 * allocated! That memory will no longer be valid after done has been 3900 * called, so this would be VERY DANGEROUS for command that actually does 3901 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3902 * start and stop commands, which don't transfer any data, so this is not a 3903 * problem. If it is used for anything else, the caller would also need to 3904 * allocate data buffer space and this routine would need to be modified to 3905 * copy the data buffer(s) as well. 3906 */ 3907void 3908ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3909{ 3910 void *pool_ref; 3911 3912 if ((src == NULL) 3913 || (dest == NULL)) 3914 return; 3915 3916 /* 3917 * May need to preserve linked list pointers at some point too. 3918 */ 3919 pool_ref = dest->io_hdr.pool; 3920 3921 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3922 3923 dest->io_hdr.pool = pool_ref; 3924 /* 3925 * We need to know that this is an internal copy, and doesn't need 3926 * to get passed back to the FETD that allocated it. 3927 */ 3928 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3929} 3930 3931#ifdef NEEDTOPORT 3932static void 3933ctl_update_power_subpage(struct copan_power_subpage *page) 3934{ 3935 int num_luns, num_partitions, config_type; 3936 struct ctl_softc *softc; 3937 cs_BOOL_t aor_present, shelf_50pct_power; 3938 cs_raidset_personality_t rs_type; 3939 int max_active_luns; 3940 3941 softc = control_softc; 3942 3943 /* subtract out the processor LUN */ 3944 num_luns = softc->num_luns - 1; 3945 /* 3946 * Default to 7 LUNs active, which was the only number we allowed 3947 * in the past. 3948 */ 3949 max_active_luns = 7; 3950 3951 num_partitions = config_GetRsPartitionInfo(); 3952 config_type = config_GetConfigType(); 3953 shelf_50pct_power = config_GetShelfPowerMode(); 3954 aor_present = config_IsAorRsPresent(); 3955 3956 rs_type = ddb_GetRsRaidType(1); 3957 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3958 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3959 EPRINT(0, "Unsupported RS type %d!", rs_type); 3960 } 3961 3962 3963 page->total_luns = num_luns; 3964 3965 switch (config_type) { 3966 case 40: 3967 /* 3968 * In a 40 drive configuration, it doesn't matter what DC 3969 * cards we have, whether we have AOR enabled or not, 3970 * partitioning or not, or what type of RAIDset we have. 3971 * In that scenario, we can power up every LUN we present 3972 * to the user. 3973 */ 3974 max_active_luns = num_luns; 3975 3976 break; 3977 case 64: 3978 if (shelf_50pct_power == CS_FALSE) { 3979 /* 25% power */ 3980 if (aor_present == CS_TRUE) { 3981 if (rs_type == 3982 CS_RAIDSET_PERSONALITY_RAID5) { 3983 max_active_luns = 7; 3984 } else if (rs_type == 3985 CS_RAIDSET_PERSONALITY_RAID1){ 3986 max_active_luns = 14; 3987 } else { 3988 /* XXX KDM now what?? */ 3989 } 3990 } else { 3991 if (rs_type == 3992 CS_RAIDSET_PERSONALITY_RAID5) { 3993 max_active_luns = 8; 3994 } else if (rs_type == 3995 CS_RAIDSET_PERSONALITY_RAID1){ 3996 max_active_luns = 16; 3997 } else { 3998 /* XXX KDM now what?? */ 3999 } 4000 } 4001 } else { 4002 /* 50% power */ 4003 /* 4004 * With 50% power in a 64 drive configuration, we 4005 * can power all LUNs we present. 4006 */ 4007 max_active_luns = num_luns; 4008 } 4009 break; 4010 case 112: 4011 if (shelf_50pct_power == CS_FALSE) { 4012 /* 25% power */ 4013 if (aor_present == CS_TRUE) { 4014 if (rs_type == 4015 CS_RAIDSET_PERSONALITY_RAID5) { 4016 max_active_luns = 7; 4017 } else if (rs_type == 4018 CS_RAIDSET_PERSONALITY_RAID1){ 4019 max_active_luns = 14; 4020 } else { 4021 /* XXX KDM now what?? */ 4022 } 4023 } else { 4024 if (rs_type == 4025 CS_RAIDSET_PERSONALITY_RAID5) { 4026 max_active_luns = 8; 4027 } else if (rs_type == 4028 CS_RAIDSET_PERSONALITY_RAID1){ 4029 max_active_luns = 16; 4030 } else { 4031 /* XXX KDM now what?? */ 4032 } 4033 } 4034 } else { 4035 /* 50% power */ 4036 if (aor_present == CS_TRUE) { 4037 if (rs_type == 4038 CS_RAIDSET_PERSONALITY_RAID5) { 4039 max_active_luns = 14; 4040 } else if (rs_type == 4041 CS_RAIDSET_PERSONALITY_RAID1){ 4042 /* 4043 * We're assuming here that disk 4044 * caching is enabled, and so we're 4045 * able to power up half of each 4046 * LUN, and cache all writes. 4047 */ 4048 max_active_luns = num_luns; 4049 } else { 4050 /* XXX KDM now what?? */ 4051 } 4052 } else { 4053 if (rs_type == 4054 CS_RAIDSET_PERSONALITY_RAID5) { 4055 max_active_luns = 15; 4056 } else if (rs_type == 4057 CS_RAIDSET_PERSONALITY_RAID1){ 4058 max_active_luns = 30; 4059 } else { 4060 /* XXX KDM now what?? */ 4061 } 4062 } 4063 } 4064 break; 4065 default: 4066 /* 4067 * In this case, we have an unknown configuration, so we 4068 * just use the default from above. 4069 */ 4070 break; 4071 } 4072 4073 page->max_active_luns = max_active_luns; 4074#if 0 4075 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4076 page->total_luns, page->max_active_luns); 4077#endif 4078} 4079#endif /* NEEDTOPORT */ 4080 4081/* 4082 * This routine could be used in the future to load default and/or saved 4083 * mode page parameters for a particuar lun. 4084 */ 4085static int 4086ctl_init_page_index(struct ctl_lun *lun) 4087{ 4088 int i; 4089 struct ctl_page_index *page_index; 4090 struct ctl_softc *softc; 4091 4092 memcpy(&lun->mode_pages.index, page_index_template, 4093 sizeof(page_index_template)); 4094 4095 softc = lun->ctl_softc; 4096 4097 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4098 4099 page_index = &lun->mode_pages.index[i]; 4100 /* 4101 * If this is a disk-only mode page, there's no point in 4102 * setting it up. For some pages, we have to have some 4103 * basic information about the disk in order to calculate the 4104 * mode page data. 4105 */ 4106 if ((lun->be_lun->lun_type != T_DIRECT) 4107 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4108 continue; 4109 4110 switch (page_index->page_code & SMPH_PC_MASK) { 4111 case SMS_FORMAT_DEVICE_PAGE: { 4112 struct scsi_format_page *format_page; 4113 4114 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4115 panic("subpage is incorrect!"); 4116 4117 /* 4118 * Sectors per track are set above. Bytes per 4119 * sector need to be set here on a per-LUN basis. 4120 */ 4121 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4122 &format_page_default, 4123 sizeof(format_page_default)); 4124 memcpy(&lun->mode_pages.format_page[ 4125 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4126 sizeof(format_page_changeable)); 4127 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4128 &format_page_default, 4129 sizeof(format_page_default)); 4130 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4131 &format_page_default, 4132 sizeof(format_page_default)); 4133 4134 format_page = &lun->mode_pages.format_page[ 4135 CTL_PAGE_CURRENT]; 4136 scsi_ulto2b(lun->be_lun->blocksize, 4137 format_page->bytes_per_sector); 4138 4139 format_page = &lun->mode_pages.format_page[ 4140 CTL_PAGE_DEFAULT]; 4141 scsi_ulto2b(lun->be_lun->blocksize, 4142 format_page->bytes_per_sector); 4143 4144 format_page = &lun->mode_pages.format_page[ 4145 CTL_PAGE_SAVED]; 4146 scsi_ulto2b(lun->be_lun->blocksize, 4147 format_page->bytes_per_sector); 4148 4149 page_index->page_data = 4150 (uint8_t *)lun->mode_pages.format_page; 4151 break; 4152 } 4153 case SMS_RIGID_DISK_PAGE: { 4154 struct scsi_rigid_disk_page *rigid_disk_page; 4155 uint32_t sectors_per_cylinder; 4156 uint64_t cylinders; 4157#ifndef __XSCALE__ 4158 int shift; 4159#endif /* !__XSCALE__ */ 4160 4161 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4162 panic("invalid subpage value %d", 4163 page_index->subpage); 4164 4165 /* 4166 * Rotation rate and sectors per track are set 4167 * above. We calculate the cylinders here based on 4168 * capacity. Due to the number of heads and 4169 * sectors per track we're using, smaller arrays 4170 * may turn out to have 0 cylinders. Linux and 4171 * FreeBSD don't pay attention to these mode pages 4172 * to figure out capacity, but Solaris does. It 4173 * seems to deal with 0 cylinders just fine, and 4174 * works out a fake geometry based on the capacity. 4175 */ 4176 memcpy(&lun->mode_pages.rigid_disk_page[ 4177 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4178 sizeof(rigid_disk_page_default)); 4179 memcpy(&lun->mode_pages.rigid_disk_page[ 4180 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4181 sizeof(rigid_disk_page_changeable)); 4182 memcpy(&lun->mode_pages.rigid_disk_page[ 4183 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4184 sizeof(rigid_disk_page_default)); 4185 memcpy(&lun->mode_pages.rigid_disk_page[ 4186 CTL_PAGE_SAVED], &rigid_disk_page_default, 4187 sizeof(rigid_disk_page_default)); 4188 4189 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4190 CTL_DEFAULT_HEADS; 4191 4192 /* 4193 * The divide method here will be more accurate, 4194 * probably, but results in floating point being 4195 * used in the kernel on i386 (__udivdi3()). On the 4196 * XScale, though, __udivdi3() is implemented in 4197 * software. 4198 * 4199 * The shift method for cylinder calculation is 4200 * accurate if sectors_per_cylinder is a power of 4201 * 2. Otherwise it might be slightly off -- you 4202 * might have a bit of a truncation problem. 4203 */ 4204#ifdef __XSCALE__ 4205 cylinders = (lun->be_lun->maxlba + 1) / 4206 sectors_per_cylinder; 4207#else 4208 for (shift = 31; shift > 0; shift--) { 4209 if (sectors_per_cylinder & (1 << shift)) 4210 break; 4211 } 4212 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4213#endif 4214 4215 /* 4216 * We've basically got 3 bytes, or 24 bits for the 4217 * cylinder size in the mode page. If we're over, 4218 * just round down to 2^24. 4219 */ 4220 if (cylinders > 0xffffff) 4221 cylinders = 0xffffff; 4222 4223 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4224 CTL_PAGE_CURRENT]; 4225 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4226 4227 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4228 CTL_PAGE_DEFAULT]; 4229 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4230 4231 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4232 CTL_PAGE_SAVED]; 4233 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4234 4235 page_index->page_data = 4236 (uint8_t *)lun->mode_pages.rigid_disk_page; 4237 break; 4238 } 4239 case SMS_CACHING_PAGE: { 4240 4241 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4242 panic("invalid subpage value %d", 4243 page_index->subpage); 4244 /* 4245 * Defaults should be okay here, no calculations 4246 * needed. 4247 */ 4248 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4249 &caching_page_default, 4250 sizeof(caching_page_default)); 4251 memcpy(&lun->mode_pages.caching_page[ 4252 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4253 sizeof(caching_page_changeable)); 4254 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4255 &caching_page_default, 4256 sizeof(caching_page_default)); 4257 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4258 &caching_page_default, 4259 sizeof(caching_page_default)); 4260 page_index->page_data = 4261 (uint8_t *)lun->mode_pages.caching_page; 4262 break; 4263 } 4264 case SMS_CONTROL_MODE_PAGE: { 4265 4266 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4267 panic("invalid subpage value %d", 4268 page_index->subpage); 4269 4270 /* 4271 * Defaults should be okay here, no calculations 4272 * needed. 4273 */ 4274 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4275 &control_page_default, 4276 sizeof(control_page_default)); 4277 memcpy(&lun->mode_pages.control_page[ 4278 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4279 sizeof(control_page_changeable)); 4280 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4281 &control_page_default, 4282 sizeof(control_page_default)); 4283 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4284 &control_page_default, 4285 sizeof(control_page_default)); 4286 page_index->page_data = 4287 (uint8_t *)lun->mode_pages.control_page; 4288 break; 4289 4290 } 4291 case SMS_VENDOR_SPECIFIC_PAGE:{ 4292 switch (page_index->subpage) { 4293 case PWR_SUBPAGE_CODE: { 4294 struct copan_power_subpage *current_page, 4295 *saved_page; 4296 4297 memcpy(&lun->mode_pages.power_subpage[ 4298 CTL_PAGE_CURRENT], 4299 &power_page_default, 4300 sizeof(power_page_default)); 4301 memcpy(&lun->mode_pages.power_subpage[ 4302 CTL_PAGE_CHANGEABLE], 4303 &power_page_changeable, 4304 sizeof(power_page_changeable)); 4305 memcpy(&lun->mode_pages.power_subpage[ 4306 CTL_PAGE_DEFAULT], 4307 &power_page_default, 4308 sizeof(power_page_default)); 4309 memcpy(&lun->mode_pages.power_subpage[ 4310 CTL_PAGE_SAVED], 4311 &power_page_default, 4312 sizeof(power_page_default)); 4313 page_index->page_data = 4314 (uint8_t *)lun->mode_pages.power_subpage; 4315 4316 current_page = (struct copan_power_subpage *) 4317 (page_index->page_data + 4318 (page_index->page_len * 4319 CTL_PAGE_CURRENT)); 4320 saved_page = (struct copan_power_subpage *) 4321 (page_index->page_data + 4322 (page_index->page_len * 4323 CTL_PAGE_SAVED)); 4324 break; 4325 } 4326 case APS_SUBPAGE_CODE: { 4327 struct copan_aps_subpage *current_page, 4328 *saved_page; 4329 4330 // This gets set multiple times but 4331 // it should always be the same. It's 4332 // only done during init so who cares. 4333 index_to_aps_page = i; 4334 4335 memcpy(&lun->mode_pages.aps_subpage[ 4336 CTL_PAGE_CURRENT], 4337 &aps_page_default, 4338 sizeof(aps_page_default)); 4339 memcpy(&lun->mode_pages.aps_subpage[ 4340 CTL_PAGE_CHANGEABLE], 4341 &aps_page_changeable, 4342 sizeof(aps_page_changeable)); 4343 memcpy(&lun->mode_pages.aps_subpage[ 4344 CTL_PAGE_DEFAULT], 4345 &aps_page_default, 4346 sizeof(aps_page_default)); 4347 memcpy(&lun->mode_pages.aps_subpage[ 4348 CTL_PAGE_SAVED], 4349 &aps_page_default, 4350 sizeof(aps_page_default)); 4351 page_index->page_data = 4352 (uint8_t *)lun->mode_pages.aps_subpage; 4353 4354 current_page = (struct copan_aps_subpage *) 4355 (page_index->page_data + 4356 (page_index->page_len * 4357 CTL_PAGE_CURRENT)); 4358 saved_page = (struct copan_aps_subpage *) 4359 (page_index->page_data + 4360 (page_index->page_len * 4361 CTL_PAGE_SAVED)); 4362 break; 4363 } 4364 case DBGCNF_SUBPAGE_CODE: { 4365 struct copan_debugconf_subpage *current_page, 4366 *saved_page; 4367 4368 memcpy(&lun->mode_pages.debugconf_subpage[ 4369 CTL_PAGE_CURRENT], 4370 &debugconf_page_default, 4371 sizeof(debugconf_page_default)); 4372 memcpy(&lun->mode_pages.debugconf_subpage[ 4373 CTL_PAGE_CHANGEABLE], 4374 &debugconf_page_changeable, 4375 sizeof(debugconf_page_changeable)); 4376 memcpy(&lun->mode_pages.debugconf_subpage[ 4377 CTL_PAGE_DEFAULT], 4378 &debugconf_page_default, 4379 sizeof(debugconf_page_default)); 4380 memcpy(&lun->mode_pages.debugconf_subpage[ 4381 CTL_PAGE_SAVED], 4382 &debugconf_page_default, 4383 sizeof(debugconf_page_default)); 4384 page_index->page_data = 4385 (uint8_t *)lun->mode_pages.debugconf_subpage; 4386 4387 current_page = (struct copan_debugconf_subpage *) 4388 (page_index->page_data + 4389 (page_index->page_len * 4390 CTL_PAGE_CURRENT)); 4391 saved_page = (struct copan_debugconf_subpage *) 4392 (page_index->page_data + 4393 (page_index->page_len * 4394 CTL_PAGE_SAVED)); 4395 break; 4396 } 4397 default: 4398 panic("invalid subpage value %d", 4399 page_index->subpage); 4400 break; 4401 } 4402 break; 4403 } 4404 default: 4405 panic("invalid page value %d", 4406 page_index->page_code & SMPH_PC_MASK); 4407 break; 4408 } 4409 } 4410 4411 return (CTL_RETVAL_COMPLETE); 4412} 4413 4414/* 4415 * LUN allocation. 4416 * 4417 * Requirements: 4418 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4419 * wants us to allocate the LUN and he can block. 4420 * - ctl_softc is always set 4421 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4422 * 4423 * Returns 0 for success, non-zero (errno) for failure. 4424 */ 4425static int 4426ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4427 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4428{ 4429 struct ctl_lun *nlun, *lun; 4430 struct ctl_port *port; 4431 struct scsi_vpd_id_descriptor *desc; 4432 struct scsi_vpd_id_t10 *t10id; 4433 const char *eui, *naa, *scsiname, *vendor; 4434 int lun_number, i, lun_malloced; 4435 int devidlen, idlen1, idlen2 = 0, len; 4436 4437 if (be_lun == NULL) 4438 return (EINVAL); 4439 4440 /* 4441 * We currently only support Direct Access or Processor LUN types. 4442 */ 4443 switch (be_lun->lun_type) { 4444 case T_DIRECT: 4445 break; 4446 case T_PROCESSOR: 4447 break; 4448 case T_SEQUENTIAL: 4449 case T_CHANGER: 4450 default: 4451 be_lun->lun_config_status(be_lun->be_lun, 4452 CTL_LUN_CONFIG_FAILURE); 4453 break; 4454 } 4455 if (ctl_lun == NULL) { 4456 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4457 lun_malloced = 1; 4458 } else { 4459 lun_malloced = 0; 4460 lun = ctl_lun; 4461 } 4462 4463 memset(lun, 0, sizeof(*lun)); 4464 if (lun_malloced) 4465 lun->flags = CTL_LUN_MALLOCED; 4466 4467 /* Generate LUN ID. */ 4468 devidlen = max(CTL_DEVID_MIN_LEN, 4469 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4470 idlen1 = sizeof(*t10id) + devidlen; 4471 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4472 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4473 if (scsiname != NULL) { 4474 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4475 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4476 } 4477 eui = ctl_get_opt(&be_lun->options, "eui"); 4478 if (eui != NULL) { 4479 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4480 } 4481 naa = ctl_get_opt(&be_lun->options, "naa"); 4482 if (naa != NULL) { 4483 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4484 } 4485 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4486 M_CTL, M_WAITOK | M_ZERO); 4487 lun->lun_devid->len = len; 4488 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4489 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4490 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4491 desc->length = idlen1; 4492 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4493 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4494 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4495 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4496 } else { 4497 strncpy(t10id->vendor, vendor, 4498 min(sizeof(t10id->vendor), strlen(vendor))); 4499 } 4500 strncpy((char *)t10id->vendor_spec_id, 4501 (char *)be_lun->device_id, devidlen); 4502 if (scsiname != NULL) { 4503 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4504 desc->length); 4505 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4506 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4507 SVPD_ID_TYPE_SCSI_NAME; 4508 desc->length = idlen2; 4509 strlcpy(desc->identifier, scsiname, idlen2); 4510 } 4511 if (eui != NULL) { 4512 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4513 desc->length); 4514 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4515 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4516 SVPD_ID_TYPE_EUI64; 4517 desc->length = 8; 4518 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4519 } 4520 if (naa != NULL) { 4521 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4522 desc->length); 4523 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4524 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4525 SVPD_ID_TYPE_NAA; 4526 desc->length = 8; 4527 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4528 } 4529 4530 mtx_lock(&ctl_softc->ctl_lock); 4531 /* 4532 * See if the caller requested a particular LUN number. If so, see 4533 * if it is available. Otherwise, allocate the first available LUN. 4534 */ 4535 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4536 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4537 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4538 mtx_unlock(&ctl_softc->ctl_lock); 4539 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4540 printf("ctl: requested LUN ID %d is higher " 4541 "than CTL_MAX_LUNS - 1 (%d)\n", 4542 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4543 } else { 4544 /* 4545 * XXX KDM return an error, or just assign 4546 * another LUN ID in this case?? 4547 */ 4548 printf("ctl: requested LUN ID %d is already " 4549 "in use\n", be_lun->req_lun_id); 4550 } 4551 if (lun->flags & CTL_LUN_MALLOCED) 4552 free(lun, M_CTL); 4553 be_lun->lun_config_status(be_lun->be_lun, 4554 CTL_LUN_CONFIG_FAILURE); 4555 return (ENOSPC); 4556 } 4557 lun_number = be_lun->req_lun_id; 4558 } else { 4559 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4560 if (lun_number == -1) { 4561 mtx_unlock(&ctl_softc->ctl_lock); 4562 printf("ctl: can't allocate LUN on target %ju, out of " 4563 "LUNs\n", (uintmax_t)target_id.id); 4564 if (lun->flags & CTL_LUN_MALLOCED) 4565 free(lun, M_CTL); 4566 be_lun->lun_config_status(be_lun->be_lun, 4567 CTL_LUN_CONFIG_FAILURE); 4568 return (ENOSPC); 4569 } 4570 } 4571 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4572 4573 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4574 lun->target = target_id; 4575 lun->lun = lun_number; 4576 lun->be_lun = be_lun; 4577 /* 4578 * The processor LUN is always enabled. Disk LUNs come on line 4579 * disabled, and must be enabled by the backend. 4580 */ 4581 lun->flags |= CTL_LUN_DISABLED; 4582 lun->backend = be_lun->be; 4583 be_lun->ctl_lun = lun; 4584 be_lun->lun_id = lun_number; 4585 atomic_add_int(&be_lun->be->num_luns, 1); 4586 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4587 lun->flags |= CTL_LUN_STOPPED; 4588 4589 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4590 lun->flags |= CTL_LUN_INOPERABLE; 4591 4592 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4593 lun->flags |= CTL_LUN_PRIMARY_SC; 4594 4595 lun->ctl_softc = ctl_softc; 4596 TAILQ_INIT(&lun->ooa_queue); 4597 TAILQ_INIT(&lun->blocked_queue); 4598 STAILQ_INIT(&lun->error_list); 4599 ctl_tpc_init(lun); 4600 4601 /* 4602 * Initialize the mode page index. 4603 */ 4604 ctl_init_page_index(lun); 4605 4606 /* 4607 * Set the poweron UA for all initiators on this LUN only. 4608 */ 4609 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4610 lun->pending_sense[i].ua_pending = CTL_UA_POWERON; 4611 4612 /* 4613 * Now, before we insert this lun on the lun list, set the lun 4614 * inventory changed UA for all other luns. 4615 */ 4616 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4617 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4618 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4619 } 4620 } 4621 4622 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4623 4624 ctl_softc->ctl_luns[lun_number] = lun; 4625 4626 ctl_softc->num_luns++; 4627 4628 /* Setup statistics gathering */ 4629 lun->stats.device_type = be_lun->lun_type; 4630 lun->stats.lun_number = lun_number; 4631 if (lun->stats.device_type == T_DIRECT) 4632 lun->stats.blocksize = be_lun->blocksize; 4633 else 4634 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4635 for (i = 0;i < CTL_MAX_PORTS;i++) 4636 lun->stats.ports[i].targ_port = i; 4637 4638 mtx_unlock(&ctl_softc->ctl_lock); 4639 4640 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4641 4642 /* 4643 * Run through each registered FETD and bring it online if it isn't 4644 * already. Enable the target ID if it hasn't been enabled, and 4645 * enable this particular LUN. 4646 */ 4647 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4648 int retval; 4649 4650 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4651 if (retval != 0) { 4652 printf("ctl_alloc_lun: FETD %s port %d returned error " 4653 "%d for lun_enable on target %ju lun %d\n", 4654 port->port_name, port->targ_port, retval, 4655 (uintmax_t)target_id.id, lun_number); 4656 } else 4657 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4658 } 4659 return (0); 4660} 4661 4662/* 4663 * Delete a LUN. 4664 * Assumptions: 4665 * - LUN has already been marked invalid and any pending I/O has been taken 4666 * care of. 4667 */ 4668static int 4669ctl_free_lun(struct ctl_lun *lun) 4670{ 4671 struct ctl_softc *softc; 4672#if 0 4673 struct ctl_port *port; 4674#endif 4675 struct ctl_lun *nlun; 4676 int i; 4677 4678 softc = lun->ctl_softc; 4679 4680 mtx_assert(&softc->ctl_lock, MA_OWNED); 4681 4682 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4683 4684 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4685 4686 softc->ctl_luns[lun->lun] = NULL; 4687 4688 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4689 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4690 4691 softc->num_luns--; 4692 4693 /* 4694 * XXX KDM this scheme only works for a single target/multiple LUN 4695 * setup. It needs to be revamped for a multiple target scheme. 4696 * 4697 * XXX KDM this results in port->lun_disable() getting called twice, 4698 * once when ctl_disable_lun() is called, and a second time here. 4699 * We really need to re-think the LUN disable semantics. There 4700 * should probably be several steps/levels to LUN removal: 4701 * - disable 4702 * - invalidate 4703 * - free 4704 * 4705 * Right now we only have a disable method when communicating to 4706 * the front end ports, at least for individual LUNs. 4707 */ 4708#if 0 4709 STAILQ_FOREACH(port, &softc->port_list, links) { 4710 int retval; 4711 4712 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4713 lun->lun); 4714 if (retval != 0) { 4715 printf("ctl_free_lun: FETD %s port %d returned error " 4716 "%d for lun_disable on target %ju lun %jd\n", 4717 port->port_name, port->targ_port, retval, 4718 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4719 } 4720 4721 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4722 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4723 4724 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4725 if (retval != 0) { 4726 printf("ctl_free_lun: FETD %s port %d " 4727 "returned error %d for targ_disable on " 4728 "target %ju\n", port->port_name, 4729 port->targ_port, retval, 4730 (uintmax_t)lun->target.id); 4731 } else 4732 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4733 4734 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4735 continue; 4736 4737#if 0 4738 port->port_offline(port->onoff_arg); 4739 port->status &= ~CTL_PORT_STATUS_ONLINE; 4740#endif 4741 } 4742 } 4743#endif 4744 4745 /* 4746 * Tell the backend to free resources, if this LUN has a backend. 4747 */ 4748 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4749 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4750 4751 ctl_tpc_shutdown(lun); 4752 mtx_destroy(&lun->lun_lock); 4753 free(lun->lun_devid, M_CTL); 4754 if (lun->flags & CTL_LUN_MALLOCED) 4755 free(lun, M_CTL); 4756 4757 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4758 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4759 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4760 } 4761 } 4762 4763 return (0); 4764} 4765 4766static void 4767ctl_create_lun(struct ctl_be_lun *be_lun) 4768{ 4769 struct ctl_softc *ctl_softc; 4770 4771 ctl_softc = control_softc; 4772 4773 /* 4774 * ctl_alloc_lun() should handle all potential failure cases. 4775 */ 4776 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4777} 4778 4779int 4780ctl_add_lun(struct ctl_be_lun *be_lun) 4781{ 4782 struct ctl_softc *ctl_softc = control_softc; 4783 4784 mtx_lock(&ctl_softc->ctl_lock); 4785 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4786 mtx_unlock(&ctl_softc->ctl_lock); 4787 wakeup(&ctl_softc->pending_lun_queue); 4788 4789 return (0); 4790} 4791 4792int 4793ctl_enable_lun(struct ctl_be_lun *be_lun) 4794{ 4795 struct ctl_softc *ctl_softc; 4796 struct ctl_port *port, *nport; 4797 struct ctl_lun *lun; 4798 int retval; 4799 4800 ctl_softc = control_softc; 4801 4802 lun = (struct ctl_lun *)be_lun->ctl_lun; 4803 4804 mtx_lock(&ctl_softc->ctl_lock); 4805 mtx_lock(&lun->lun_lock); 4806 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4807 /* 4808 * eh? Why did we get called if the LUN is already 4809 * enabled? 4810 */ 4811 mtx_unlock(&lun->lun_lock); 4812 mtx_unlock(&ctl_softc->ctl_lock); 4813 return (0); 4814 } 4815 lun->flags &= ~CTL_LUN_DISABLED; 4816 mtx_unlock(&lun->lun_lock); 4817 4818 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4819 nport = STAILQ_NEXT(port, links); 4820 4821 /* 4822 * Drop the lock while we call the FETD's enable routine. 4823 * This can lead to a callback into CTL (at least in the 4824 * case of the internal initiator frontend. 4825 */ 4826 mtx_unlock(&ctl_softc->ctl_lock); 4827 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4828 mtx_lock(&ctl_softc->ctl_lock); 4829 if (retval != 0) { 4830 printf("%s: FETD %s port %d returned error " 4831 "%d for lun_enable on target %ju lun %jd\n", 4832 __func__, port->port_name, port->targ_port, retval, 4833 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4834 } 4835#if 0 4836 else { 4837 /* NOTE: TODO: why does lun enable affect port status? */ 4838 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4839 } 4840#endif 4841 } 4842 4843 mtx_unlock(&ctl_softc->ctl_lock); 4844 4845 return (0); 4846} 4847 4848int 4849ctl_disable_lun(struct ctl_be_lun *be_lun) 4850{ 4851 struct ctl_softc *ctl_softc; 4852 struct ctl_port *port; 4853 struct ctl_lun *lun; 4854 int retval; 4855 4856 ctl_softc = control_softc; 4857 4858 lun = (struct ctl_lun *)be_lun->ctl_lun; 4859 4860 mtx_lock(&ctl_softc->ctl_lock); 4861 mtx_lock(&lun->lun_lock); 4862 if (lun->flags & CTL_LUN_DISABLED) { 4863 mtx_unlock(&lun->lun_lock); 4864 mtx_unlock(&ctl_softc->ctl_lock); 4865 return (0); 4866 } 4867 lun->flags |= CTL_LUN_DISABLED; 4868 mtx_unlock(&lun->lun_lock); 4869 4870 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4871 mtx_unlock(&ctl_softc->ctl_lock); 4872 /* 4873 * Drop the lock before we call the frontend's disable 4874 * routine, to avoid lock order reversals. 4875 * 4876 * XXX KDM what happens if the frontend list changes while 4877 * we're traversing it? It's unlikely, but should be handled. 4878 */ 4879 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4880 lun->lun); 4881 mtx_lock(&ctl_softc->ctl_lock); 4882 if (retval != 0) { 4883 printf("ctl_alloc_lun: FETD %s port %d returned error " 4884 "%d for lun_disable on target %ju lun %jd\n", 4885 port->port_name, port->targ_port, retval, 4886 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4887 } 4888 } 4889 4890 mtx_unlock(&ctl_softc->ctl_lock); 4891 4892 return (0); 4893} 4894 4895int 4896ctl_start_lun(struct ctl_be_lun *be_lun) 4897{ 4898 struct ctl_softc *ctl_softc; 4899 struct ctl_lun *lun; 4900 4901 ctl_softc = control_softc; 4902 4903 lun = (struct ctl_lun *)be_lun->ctl_lun; 4904 4905 mtx_lock(&lun->lun_lock); 4906 lun->flags &= ~CTL_LUN_STOPPED; 4907 mtx_unlock(&lun->lun_lock); 4908 4909 return (0); 4910} 4911 4912int 4913ctl_stop_lun(struct ctl_be_lun *be_lun) 4914{ 4915 struct ctl_softc *ctl_softc; 4916 struct ctl_lun *lun; 4917 4918 ctl_softc = control_softc; 4919 4920 lun = (struct ctl_lun *)be_lun->ctl_lun; 4921 4922 mtx_lock(&lun->lun_lock); 4923 lun->flags |= CTL_LUN_STOPPED; 4924 mtx_unlock(&lun->lun_lock); 4925 4926 return (0); 4927} 4928 4929int 4930ctl_lun_offline(struct ctl_be_lun *be_lun) 4931{ 4932 struct ctl_softc *ctl_softc; 4933 struct ctl_lun *lun; 4934 4935 ctl_softc = control_softc; 4936 4937 lun = (struct ctl_lun *)be_lun->ctl_lun; 4938 4939 mtx_lock(&lun->lun_lock); 4940 lun->flags |= CTL_LUN_OFFLINE; 4941 mtx_unlock(&lun->lun_lock); 4942 4943 return (0); 4944} 4945 4946int 4947ctl_lun_online(struct ctl_be_lun *be_lun) 4948{ 4949 struct ctl_softc *ctl_softc; 4950 struct ctl_lun *lun; 4951 4952 ctl_softc = control_softc; 4953 4954 lun = (struct ctl_lun *)be_lun->ctl_lun; 4955 4956 mtx_lock(&lun->lun_lock); 4957 lun->flags &= ~CTL_LUN_OFFLINE; 4958 mtx_unlock(&lun->lun_lock); 4959 4960 return (0); 4961} 4962 4963int 4964ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4965{ 4966 struct ctl_softc *ctl_softc; 4967 struct ctl_lun *lun; 4968 4969 ctl_softc = control_softc; 4970 4971 lun = (struct ctl_lun *)be_lun->ctl_lun; 4972 4973 mtx_lock(&lun->lun_lock); 4974 4975 /* 4976 * The LUN needs to be disabled before it can be marked invalid. 4977 */ 4978 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4979 mtx_unlock(&lun->lun_lock); 4980 return (-1); 4981 } 4982 /* 4983 * Mark the LUN invalid. 4984 */ 4985 lun->flags |= CTL_LUN_INVALID; 4986 4987 /* 4988 * If there is nothing in the OOA queue, go ahead and free the LUN. 4989 * If we have something in the OOA queue, we'll free it when the 4990 * last I/O completes. 4991 */ 4992 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4993 mtx_unlock(&lun->lun_lock); 4994 mtx_lock(&ctl_softc->ctl_lock); 4995 ctl_free_lun(lun); 4996 mtx_unlock(&ctl_softc->ctl_lock); 4997 } else 4998 mtx_unlock(&lun->lun_lock); 4999 5000 return (0); 5001} 5002 5003int 5004ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5005{ 5006 struct ctl_softc *ctl_softc; 5007 struct ctl_lun *lun; 5008 5009 ctl_softc = control_softc; 5010 lun = (struct ctl_lun *)be_lun->ctl_lun; 5011 5012 mtx_lock(&lun->lun_lock); 5013 lun->flags |= CTL_LUN_INOPERABLE; 5014 mtx_unlock(&lun->lun_lock); 5015 5016 return (0); 5017} 5018 5019int 5020ctl_lun_operable(struct ctl_be_lun *be_lun) 5021{ 5022 struct ctl_softc *ctl_softc; 5023 struct ctl_lun *lun; 5024 5025 ctl_softc = control_softc; 5026 lun = (struct ctl_lun *)be_lun->ctl_lun; 5027 5028 mtx_lock(&lun->lun_lock); 5029 lun->flags &= ~CTL_LUN_INOPERABLE; 5030 mtx_unlock(&lun->lun_lock); 5031 5032 return (0); 5033} 5034 5035int 5036ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5037 int lock) 5038{ 5039 struct ctl_softc *softc; 5040 struct ctl_lun *lun; 5041 struct copan_aps_subpage *current_sp; 5042 struct ctl_page_index *page_index; 5043 int i; 5044 5045 softc = control_softc; 5046 5047 mtx_lock(&softc->ctl_lock); 5048 5049 lun = (struct ctl_lun *)be_lun->ctl_lun; 5050 mtx_lock(&lun->lun_lock); 5051 5052 page_index = NULL; 5053 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5054 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5055 APS_PAGE_CODE) 5056 continue; 5057 5058 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5059 continue; 5060 page_index = &lun->mode_pages.index[i]; 5061 } 5062 5063 if (page_index == NULL) { 5064 mtx_unlock(&lun->lun_lock); 5065 mtx_unlock(&softc->ctl_lock); 5066 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5067 (uintmax_t)lun->lun); 5068 return (1); 5069 } 5070#if 0 5071 if ((softc->aps_locked_lun != 0) 5072 && (softc->aps_locked_lun != lun->lun)) { 5073 printf("%s: attempt to lock LUN %llu when %llu is already " 5074 "locked\n"); 5075 mtx_unlock(&lun->lun_lock); 5076 mtx_unlock(&softc->ctl_lock); 5077 return (1); 5078 } 5079#endif 5080 5081 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5082 (page_index->page_len * CTL_PAGE_CURRENT)); 5083 5084 if (lock != 0) { 5085 current_sp->lock_active = APS_LOCK_ACTIVE; 5086 softc->aps_locked_lun = lun->lun; 5087 } else { 5088 current_sp->lock_active = 0; 5089 softc->aps_locked_lun = 0; 5090 } 5091 5092 5093 /* 5094 * If we're in HA mode, try to send the lock message to the other 5095 * side. 5096 */ 5097 if (ctl_is_single == 0) { 5098 int isc_retval; 5099 union ctl_ha_msg lock_msg; 5100 5101 lock_msg.hdr.nexus = *nexus; 5102 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5103 if (lock != 0) 5104 lock_msg.aps.lock_flag = 1; 5105 else 5106 lock_msg.aps.lock_flag = 0; 5107 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5108 sizeof(lock_msg), 0); 5109 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5110 printf("%s: APS (lock=%d) error returned from " 5111 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5112 mtx_unlock(&lun->lun_lock); 5113 mtx_unlock(&softc->ctl_lock); 5114 return (1); 5115 } 5116 } 5117 5118 mtx_unlock(&lun->lun_lock); 5119 mtx_unlock(&softc->ctl_lock); 5120 5121 return (0); 5122} 5123 5124void 5125ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5126{ 5127 struct ctl_lun *lun; 5128 struct ctl_softc *softc; 5129 int i; 5130 5131 softc = control_softc; 5132 5133 lun = (struct ctl_lun *)be_lun->ctl_lun; 5134 5135 mtx_lock(&lun->lun_lock); 5136 5137 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5138 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED; 5139 5140 mtx_unlock(&lun->lun_lock); 5141} 5142 5143/* 5144 * Backend "memory move is complete" callback for requests that never 5145 * make it down to say RAIDCore's configuration code. 5146 */ 5147int 5148ctl_config_move_done(union ctl_io *io) 5149{ 5150 int retval; 5151 5152 retval = CTL_RETVAL_COMPLETE; 5153 5154 5155 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5156 /* 5157 * XXX KDM this shouldn't happen, but what if it does? 5158 */ 5159 if (io->io_hdr.io_type != CTL_IO_SCSI) 5160 panic("I/O type isn't CTL_IO_SCSI!"); 5161 5162 if ((io->io_hdr.port_status == 0) 5163 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5164 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5165 io->io_hdr.status = CTL_SUCCESS; 5166 else 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 /* 5170 * For hardware error sense keys, the sense key 5171 * specific value is defined to be a retry count, 5172 * but we use it to pass back an internal FETD 5173 * error code. XXX KDM Hopefully the FETD is only 5174 * using 16 bits for an error code, since that's 5175 * all the space we have in the sks field. 5176 */ 5177 ctl_set_internal_failure(&io->scsiio, 5178 /*sks_valid*/ 1, 5179 /*retry_count*/ 5180 io->io_hdr.port_status); 5181 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5182 free(io->scsiio.kern_data_ptr, M_CTL); 5183 ctl_done(io); 5184 goto bailout; 5185 } 5186 5187 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5188 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5189 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5190 /* 5191 * XXX KDM just assuming a single pointer here, and not a 5192 * S/G list. If we start using S/G lists for config data, 5193 * we'll need to know how to clean them up here as well. 5194 */ 5195 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5196 free(io->scsiio.kern_data_ptr, M_CTL); 5197 /* Hopefully the user has already set the status... */ 5198 ctl_done(io); 5199 } else { 5200 /* 5201 * XXX KDM now we need to continue data movement. Some 5202 * options: 5203 * - call ctl_scsiio() again? We don't do this for data 5204 * writes, because for those at least we know ahead of 5205 * time where the write will go and how long it is. For 5206 * config writes, though, that information is largely 5207 * contained within the write itself, thus we need to 5208 * parse out the data again. 5209 * 5210 * - Call some other function once the data is in? 5211 */ 5212 5213 /* 5214 * XXX KDM call ctl_scsiio() again for now, and check flag 5215 * bits to see whether we're allocated or not. 5216 */ 5217 retval = ctl_scsiio(&io->scsiio); 5218 } 5219bailout: 5220 return (retval); 5221} 5222 5223/* 5224 * This gets called by a backend driver when it is done with a 5225 * data_submit method. 5226 */ 5227void 5228ctl_data_submit_done(union ctl_io *io) 5229{ 5230 /* 5231 * If the IO_CONT flag is set, we need to call the supplied 5232 * function to continue processing the I/O, instead of completing 5233 * the I/O just yet. 5234 * 5235 * If there is an error, though, we don't want to keep processing. 5236 * Instead, just send status back to the initiator. 5237 */ 5238 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5239 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5240 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5241 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5242 io->scsiio.io_cont(io); 5243 return; 5244 } 5245 ctl_done(io); 5246} 5247 5248/* 5249 * This gets called by a backend driver when it is done with a 5250 * configuration write. 5251 */ 5252void 5253ctl_config_write_done(union ctl_io *io) 5254{ 5255 /* 5256 * If the IO_CONT flag is set, we need to call the supplied 5257 * function to continue processing the I/O, instead of completing 5258 * the I/O just yet. 5259 * 5260 * If there is an error, though, we don't want to keep processing. 5261 * Instead, just send status back to the initiator. 5262 */ 5263 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5264 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5265 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5266 io->scsiio.io_cont(io); 5267 return; 5268 } 5269 /* 5270 * Since a configuration write can be done for commands that actually 5271 * have data allocated, like write buffer, and commands that have 5272 * no data, like start/stop unit, we need to check here. 5273 */ 5274 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5275 free(io->scsiio.kern_data_ptr, M_CTL); 5276 ctl_done(io); 5277} 5278 5279/* 5280 * SCSI release command. 5281 */ 5282int 5283ctl_scsi_release(struct ctl_scsiio *ctsio) 5284{ 5285 int length, longid, thirdparty_id, resv_id; 5286 struct ctl_softc *ctl_softc; 5287 struct ctl_lun *lun; 5288 5289 length = 0; 5290 resv_id = 0; 5291 5292 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5293 5294 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5295 ctl_softc = control_softc; 5296 5297 switch (ctsio->cdb[0]) { 5298 case RELEASE_10: { 5299 struct scsi_release_10 *cdb; 5300 5301 cdb = (struct scsi_release_10 *)ctsio->cdb; 5302 5303 if (cdb->byte2 & SR10_LONGID) 5304 longid = 1; 5305 else 5306 thirdparty_id = cdb->thirdparty_id; 5307 5308 resv_id = cdb->resv_id; 5309 length = scsi_2btoul(cdb->length); 5310 break; 5311 } 5312 } 5313 5314 5315 /* 5316 * XXX KDM right now, we only support LUN reservation. We don't 5317 * support 3rd party reservations, or extent reservations, which 5318 * might actually need the parameter list. If we've gotten this 5319 * far, we've got a LUN reservation. Anything else got kicked out 5320 * above. So, according to SPC, ignore the length. 5321 */ 5322 length = 0; 5323 5324 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5325 && (length > 0)) { 5326 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5327 ctsio->kern_data_len = length; 5328 ctsio->kern_total_len = length; 5329 ctsio->kern_data_resid = 0; 5330 ctsio->kern_rel_offset = 0; 5331 ctsio->kern_sg_entries = 0; 5332 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5333 ctsio->be_move_done = ctl_config_move_done; 5334 ctl_datamove((union ctl_io *)ctsio); 5335 5336 return (CTL_RETVAL_COMPLETE); 5337 } 5338 5339 if (length > 0) 5340 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5341 5342 mtx_lock(&lun->lun_lock); 5343 5344 /* 5345 * According to SPC, it is not an error for an intiator to attempt 5346 * to release a reservation on a LUN that isn't reserved, or that 5347 * is reserved by another initiator. The reservation can only be 5348 * released, though, by the initiator who made it or by one of 5349 * several reset type events. 5350 */ 5351 if (lun->flags & CTL_LUN_RESERVED) { 5352 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5353 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5354 && (ctsio->io_hdr.nexus.targ_target.id == 5355 lun->rsv_nexus.targ_target.id)) { 5356 lun->flags &= ~CTL_LUN_RESERVED; 5357 } 5358 } 5359 5360 mtx_unlock(&lun->lun_lock); 5361 5362 ctsio->scsi_status = SCSI_STATUS_OK; 5363 ctsio->io_hdr.status = CTL_SUCCESS; 5364 5365 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5366 free(ctsio->kern_data_ptr, M_CTL); 5367 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5368 } 5369 5370 ctl_done((union ctl_io *)ctsio); 5371 return (CTL_RETVAL_COMPLETE); 5372} 5373 5374int 5375ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5376{ 5377 int extent, thirdparty, longid; 5378 int resv_id, length; 5379 uint64_t thirdparty_id; 5380 struct ctl_softc *ctl_softc; 5381 struct ctl_lun *lun; 5382 5383 extent = 0; 5384 thirdparty = 0; 5385 longid = 0; 5386 resv_id = 0; 5387 length = 0; 5388 thirdparty_id = 0; 5389 5390 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5391 5392 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5393 ctl_softc = control_softc; 5394 5395 switch (ctsio->cdb[0]) { 5396 case RESERVE_10: { 5397 struct scsi_reserve_10 *cdb; 5398 5399 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5400 5401 if (cdb->byte2 & SR10_LONGID) 5402 longid = 1; 5403 else 5404 thirdparty_id = cdb->thirdparty_id; 5405 5406 resv_id = cdb->resv_id; 5407 length = scsi_2btoul(cdb->length); 5408 break; 5409 } 5410 } 5411 5412 /* 5413 * XXX KDM right now, we only support LUN reservation. We don't 5414 * support 3rd party reservations, or extent reservations, which 5415 * might actually need the parameter list. If we've gotten this 5416 * far, we've got a LUN reservation. Anything else got kicked out 5417 * above. So, according to SPC, ignore the length. 5418 */ 5419 length = 0; 5420 5421 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5422 && (length > 0)) { 5423 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5424 ctsio->kern_data_len = length; 5425 ctsio->kern_total_len = length; 5426 ctsio->kern_data_resid = 0; 5427 ctsio->kern_rel_offset = 0; 5428 ctsio->kern_sg_entries = 0; 5429 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5430 ctsio->be_move_done = ctl_config_move_done; 5431 ctl_datamove((union ctl_io *)ctsio); 5432 5433 return (CTL_RETVAL_COMPLETE); 5434 } 5435 5436 if (length > 0) 5437 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5438 5439 mtx_lock(&lun->lun_lock); 5440 if (lun->flags & CTL_LUN_RESERVED) { 5441 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5442 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5443 || (ctsio->io_hdr.nexus.targ_target.id != 5444 lun->rsv_nexus.targ_target.id)) { 5445 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5446 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5447 goto bailout; 5448 } 5449 } 5450 5451 lun->flags |= CTL_LUN_RESERVED; 5452 lun->rsv_nexus = ctsio->io_hdr.nexus; 5453 5454 ctsio->scsi_status = SCSI_STATUS_OK; 5455 ctsio->io_hdr.status = CTL_SUCCESS; 5456 5457bailout: 5458 mtx_unlock(&lun->lun_lock); 5459 5460 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5461 free(ctsio->kern_data_ptr, M_CTL); 5462 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5463 } 5464 5465 ctl_done((union ctl_io *)ctsio); 5466 return (CTL_RETVAL_COMPLETE); 5467} 5468 5469int 5470ctl_start_stop(struct ctl_scsiio *ctsio) 5471{ 5472 struct scsi_start_stop_unit *cdb; 5473 struct ctl_lun *lun; 5474 struct ctl_softc *ctl_softc; 5475 int retval; 5476 5477 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5478 5479 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5480 ctl_softc = control_softc; 5481 retval = 0; 5482 5483 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5484 5485 /* 5486 * XXX KDM 5487 * We don't support the immediate bit on a stop unit. In order to 5488 * do that, we would need to code up a way to know that a stop is 5489 * pending, and hold off any new commands until it completes, one 5490 * way or another. Then we could accept or reject those commands 5491 * depending on its status. We would almost need to do the reverse 5492 * of what we do below for an immediate start -- return the copy of 5493 * the ctl_io to the FETD with status to send to the host (and to 5494 * free the copy!) and then free the original I/O once the stop 5495 * actually completes. That way, the OOA queue mechanism can work 5496 * to block commands that shouldn't proceed. Another alternative 5497 * would be to put the copy in the queue in place of the original, 5498 * and return the original back to the caller. That could be 5499 * slightly safer.. 5500 */ 5501 if ((cdb->byte2 & SSS_IMMED) 5502 && ((cdb->how & SSS_START) == 0)) { 5503 ctl_set_invalid_field(ctsio, 5504 /*sks_valid*/ 1, 5505 /*command*/ 1, 5506 /*field*/ 1, 5507 /*bit_valid*/ 1, 5508 /*bit*/ 0); 5509 ctl_done((union ctl_io *)ctsio); 5510 return (CTL_RETVAL_COMPLETE); 5511 } 5512 5513 if ((lun->flags & CTL_LUN_PR_RESERVED) 5514 && ((cdb->how & SSS_START)==0)) { 5515 uint32_t residx; 5516 5517 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5518 if (!lun->per_res[residx].registered 5519 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5520 5521 ctl_set_reservation_conflict(ctsio); 5522 ctl_done((union ctl_io *)ctsio); 5523 return (CTL_RETVAL_COMPLETE); 5524 } 5525 } 5526 5527 /* 5528 * If there is no backend on this device, we can't start or stop 5529 * it. In theory we shouldn't get any start/stop commands in the 5530 * first place at this level if the LUN doesn't have a backend. 5531 * That should get stopped by the command decode code. 5532 */ 5533 if (lun->backend == NULL) { 5534 ctl_set_invalid_opcode(ctsio); 5535 ctl_done((union ctl_io *)ctsio); 5536 return (CTL_RETVAL_COMPLETE); 5537 } 5538 5539 /* 5540 * XXX KDM Copan-specific offline behavior. 5541 * Figure out a reasonable way to port this? 5542 */ 5543#ifdef NEEDTOPORT 5544 mtx_lock(&lun->lun_lock); 5545 5546 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5547 && (lun->flags & CTL_LUN_OFFLINE)) { 5548 /* 5549 * If the LUN is offline, and the on/offline bit isn't set, 5550 * reject the start or stop. Otherwise, let it through. 5551 */ 5552 mtx_unlock(&lun->lun_lock); 5553 ctl_set_lun_not_ready(ctsio); 5554 ctl_done((union ctl_io *)ctsio); 5555 } else { 5556 mtx_unlock(&lun->lun_lock); 5557#endif /* NEEDTOPORT */ 5558 /* 5559 * This could be a start or a stop when we're online, 5560 * or a stop/offline or start/online. A start or stop when 5561 * we're offline is covered in the case above. 5562 */ 5563 /* 5564 * In the non-immediate case, we send the request to 5565 * the backend and return status to the user when 5566 * it is done. 5567 * 5568 * In the immediate case, we allocate a new ctl_io 5569 * to hold a copy of the request, and send that to 5570 * the backend. We then set good status on the 5571 * user's request and return it immediately. 5572 */ 5573 if (cdb->byte2 & SSS_IMMED) { 5574 union ctl_io *new_io; 5575 5576 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5577 if (new_io == NULL) { 5578 ctl_set_busy(ctsio); 5579 ctl_done((union ctl_io *)ctsio); 5580 } else { 5581 ctl_copy_io((union ctl_io *)ctsio, 5582 new_io); 5583 retval = lun->backend->config_write(new_io); 5584 ctl_set_success(ctsio); 5585 ctl_done((union ctl_io *)ctsio); 5586 } 5587 } else { 5588 retval = lun->backend->config_write( 5589 (union ctl_io *)ctsio); 5590 } 5591#ifdef NEEDTOPORT 5592 } 5593#endif 5594 return (retval); 5595} 5596 5597/* 5598 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5599 * we don't really do anything with the LBA and length fields if the user 5600 * passes them in. Instead we'll just flush out the cache for the entire 5601 * LUN. 5602 */ 5603int 5604ctl_sync_cache(struct ctl_scsiio *ctsio) 5605{ 5606 struct ctl_lun *lun; 5607 struct ctl_softc *ctl_softc; 5608 uint64_t starting_lba; 5609 uint32_t block_count; 5610 int retval; 5611 5612 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5613 5614 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5615 ctl_softc = control_softc; 5616 retval = 0; 5617 5618 switch (ctsio->cdb[0]) { 5619 case SYNCHRONIZE_CACHE: { 5620 struct scsi_sync_cache *cdb; 5621 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5622 5623 starting_lba = scsi_4btoul(cdb->begin_lba); 5624 block_count = scsi_2btoul(cdb->lb_count); 5625 break; 5626 } 5627 case SYNCHRONIZE_CACHE_16: { 5628 struct scsi_sync_cache_16 *cdb; 5629 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5630 5631 starting_lba = scsi_8btou64(cdb->begin_lba); 5632 block_count = scsi_4btoul(cdb->lb_count); 5633 break; 5634 } 5635 default: 5636 ctl_set_invalid_opcode(ctsio); 5637 ctl_done((union ctl_io *)ctsio); 5638 goto bailout; 5639 break; /* NOTREACHED */ 5640 } 5641 5642 /* 5643 * We check the LBA and length, but don't do anything with them. 5644 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5645 * get flushed. This check will just help satisfy anyone who wants 5646 * to see an error for an out of range LBA. 5647 */ 5648 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5649 ctl_set_lba_out_of_range(ctsio); 5650 ctl_done((union ctl_io *)ctsio); 5651 goto bailout; 5652 } 5653 5654 /* 5655 * If this LUN has no backend, we can't flush the cache anyway. 5656 */ 5657 if (lun->backend == NULL) { 5658 ctl_set_invalid_opcode(ctsio); 5659 ctl_done((union ctl_io *)ctsio); 5660 goto bailout; 5661 } 5662 5663 /* 5664 * Check to see whether we're configured to send the SYNCHRONIZE 5665 * CACHE command directly to the back end. 5666 */ 5667 mtx_lock(&lun->lun_lock); 5668 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5669 && (++(lun->sync_count) >= lun->sync_interval)) { 5670 lun->sync_count = 0; 5671 mtx_unlock(&lun->lun_lock); 5672 retval = lun->backend->config_write((union ctl_io *)ctsio); 5673 } else { 5674 mtx_unlock(&lun->lun_lock); 5675 ctl_set_success(ctsio); 5676 ctl_done((union ctl_io *)ctsio); 5677 } 5678 5679bailout: 5680 5681 return (retval); 5682} 5683 5684int 5685ctl_format(struct ctl_scsiio *ctsio) 5686{ 5687 struct scsi_format *cdb; 5688 struct ctl_lun *lun; 5689 struct ctl_softc *ctl_softc; 5690 int length, defect_list_len; 5691 5692 CTL_DEBUG_PRINT(("ctl_format\n")); 5693 5694 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5695 ctl_softc = control_softc; 5696 5697 cdb = (struct scsi_format *)ctsio->cdb; 5698 5699 length = 0; 5700 if (cdb->byte2 & SF_FMTDATA) { 5701 if (cdb->byte2 & SF_LONGLIST) 5702 length = sizeof(struct scsi_format_header_long); 5703 else 5704 length = sizeof(struct scsi_format_header_short); 5705 } 5706 5707 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5708 && (length > 0)) { 5709 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5710 ctsio->kern_data_len = length; 5711 ctsio->kern_total_len = length; 5712 ctsio->kern_data_resid = 0; 5713 ctsio->kern_rel_offset = 0; 5714 ctsio->kern_sg_entries = 0; 5715 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5716 ctsio->be_move_done = ctl_config_move_done; 5717 ctl_datamove((union ctl_io *)ctsio); 5718 5719 return (CTL_RETVAL_COMPLETE); 5720 } 5721 5722 defect_list_len = 0; 5723 5724 if (cdb->byte2 & SF_FMTDATA) { 5725 if (cdb->byte2 & SF_LONGLIST) { 5726 struct scsi_format_header_long *header; 5727 5728 header = (struct scsi_format_header_long *) 5729 ctsio->kern_data_ptr; 5730 5731 defect_list_len = scsi_4btoul(header->defect_list_len); 5732 if (defect_list_len != 0) { 5733 ctl_set_invalid_field(ctsio, 5734 /*sks_valid*/ 1, 5735 /*command*/ 0, 5736 /*field*/ 2, 5737 /*bit_valid*/ 0, 5738 /*bit*/ 0); 5739 goto bailout; 5740 } 5741 } else { 5742 struct scsi_format_header_short *header; 5743 5744 header = (struct scsi_format_header_short *) 5745 ctsio->kern_data_ptr; 5746 5747 defect_list_len = scsi_2btoul(header->defect_list_len); 5748 if (defect_list_len != 0) { 5749 ctl_set_invalid_field(ctsio, 5750 /*sks_valid*/ 1, 5751 /*command*/ 0, 5752 /*field*/ 2, 5753 /*bit_valid*/ 0, 5754 /*bit*/ 0); 5755 goto bailout; 5756 } 5757 } 5758 } 5759 5760 /* 5761 * The format command will clear out the "Medium format corrupted" 5762 * status if set by the configuration code. That status is really 5763 * just a way to notify the host that we have lost the media, and 5764 * get them to issue a command that will basically make them think 5765 * they're blowing away the media. 5766 */ 5767 mtx_lock(&lun->lun_lock); 5768 lun->flags &= ~CTL_LUN_INOPERABLE; 5769 mtx_unlock(&lun->lun_lock); 5770 5771 ctsio->scsi_status = SCSI_STATUS_OK; 5772 ctsio->io_hdr.status = CTL_SUCCESS; 5773bailout: 5774 5775 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5776 free(ctsio->kern_data_ptr, M_CTL); 5777 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5778 } 5779 5780 ctl_done((union ctl_io *)ctsio); 5781 return (CTL_RETVAL_COMPLETE); 5782} 5783 5784int 5785ctl_read_buffer(struct ctl_scsiio *ctsio) 5786{ 5787 struct scsi_read_buffer *cdb; 5788 struct ctl_lun *lun; 5789 int buffer_offset, len; 5790 static uint8_t descr[4]; 5791 static uint8_t echo_descr[4] = { 0 }; 5792 5793 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5794 5795 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5796 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5797 5798 if (lun->flags & CTL_LUN_PR_RESERVED) { 5799 uint32_t residx; 5800 5801 /* 5802 * XXX KDM need a lock here. 5803 */ 5804 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5805 if ((lun->res_type == SPR_TYPE_EX_AC 5806 && residx != lun->pr_res_idx) 5807 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5808 || lun->res_type == SPR_TYPE_EX_AC_AR) 5809 && !lun->per_res[residx].registered)) { 5810 ctl_set_reservation_conflict(ctsio); 5811 ctl_done((union ctl_io *)ctsio); 5812 return (CTL_RETVAL_COMPLETE); 5813 } 5814 } 5815 5816 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5817 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5818 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5819 ctl_set_invalid_field(ctsio, 5820 /*sks_valid*/ 1, 5821 /*command*/ 1, 5822 /*field*/ 1, 5823 /*bit_valid*/ 1, 5824 /*bit*/ 4); 5825 ctl_done((union ctl_io *)ctsio); 5826 return (CTL_RETVAL_COMPLETE); 5827 } 5828 5829 len = scsi_3btoul(cdb->length); 5830 buffer_offset = scsi_3btoul(cdb->offset); 5831 5832 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5833 ctl_set_invalid_field(ctsio, 5834 /*sks_valid*/ 1, 5835 /*command*/ 1, 5836 /*field*/ 6, 5837 /*bit_valid*/ 0, 5838 /*bit*/ 0); 5839 ctl_done((union ctl_io *)ctsio); 5840 return (CTL_RETVAL_COMPLETE); 5841 } 5842 5843 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5844 descr[0] = 0; 5845 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5846 ctsio->kern_data_ptr = descr; 5847 len = min(len, sizeof(descr)); 5848 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5849 ctsio->kern_data_ptr = echo_descr; 5850 len = min(len, sizeof(echo_descr)); 5851 } else 5852 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5853 ctsio->kern_data_len = len; 5854 ctsio->kern_total_len = len; 5855 ctsio->kern_data_resid = 0; 5856 ctsio->kern_rel_offset = 0; 5857 ctsio->kern_sg_entries = 0; 5858 ctsio->be_move_done = ctl_config_move_done; 5859 ctl_datamove((union ctl_io *)ctsio); 5860 5861 return (CTL_RETVAL_COMPLETE); 5862} 5863 5864int 5865ctl_write_buffer(struct ctl_scsiio *ctsio) 5866{ 5867 struct scsi_write_buffer *cdb; 5868 struct ctl_lun *lun; 5869 int buffer_offset, len; 5870 5871 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5872 5873 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5874 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5875 5876 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5877 ctl_set_invalid_field(ctsio, 5878 /*sks_valid*/ 1, 5879 /*command*/ 1, 5880 /*field*/ 1, 5881 /*bit_valid*/ 1, 5882 /*bit*/ 4); 5883 ctl_done((union ctl_io *)ctsio); 5884 return (CTL_RETVAL_COMPLETE); 5885 } 5886 5887 len = scsi_3btoul(cdb->length); 5888 buffer_offset = scsi_3btoul(cdb->offset); 5889 5890 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5891 ctl_set_invalid_field(ctsio, 5892 /*sks_valid*/ 1, 5893 /*command*/ 1, 5894 /*field*/ 6, 5895 /*bit_valid*/ 0, 5896 /*bit*/ 0); 5897 ctl_done((union ctl_io *)ctsio); 5898 return (CTL_RETVAL_COMPLETE); 5899 } 5900 5901 /* 5902 * If we've got a kernel request that hasn't been malloced yet, 5903 * malloc it and tell the caller the data buffer is here. 5904 */ 5905 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5906 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5907 ctsio->kern_data_len = len; 5908 ctsio->kern_total_len = len; 5909 ctsio->kern_data_resid = 0; 5910 ctsio->kern_rel_offset = 0; 5911 ctsio->kern_sg_entries = 0; 5912 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5913 ctsio->be_move_done = ctl_config_move_done; 5914 ctl_datamove((union ctl_io *)ctsio); 5915 5916 return (CTL_RETVAL_COMPLETE); 5917 } 5918 5919 ctl_done((union ctl_io *)ctsio); 5920 5921 return (CTL_RETVAL_COMPLETE); 5922} 5923 5924int 5925ctl_write_same(struct ctl_scsiio *ctsio) 5926{ 5927 struct ctl_lun *lun; 5928 struct ctl_lba_len_flags *lbalen; 5929 uint64_t lba; 5930 uint32_t num_blocks; 5931 int len, retval; 5932 uint8_t byte2; 5933 5934 retval = CTL_RETVAL_COMPLETE; 5935 5936 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5937 5938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5939 5940 switch (ctsio->cdb[0]) { 5941 case WRITE_SAME_10: { 5942 struct scsi_write_same_10 *cdb; 5943 5944 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5945 5946 lba = scsi_4btoul(cdb->addr); 5947 num_blocks = scsi_2btoul(cdb->length); 5948 byte2 = cdb->byte2; 5949 break; 5950 } 5951 case WRITE_SAME_16: { 5952 struct scsi_write_same_16 *cdb; 5953 5954 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5955 5956 lba = scsi_8btou64(cdb->addr); 5957 num_blocks = scsi_4btoul(cdb->length); 5958 byte2 = cdb->byte2; 5959 break; 5960 } 5961 default: 5962 /* 5963 * We got a command we don't support. This shouldn't 5964 * happen, commands should be filtered out above us. 5965 */ 5966 ctl_set_invalid_opcode(ctsio); 5967 ctl_done((union ctl_io *)ctsio); 5968 5969 return (CTL_RETVAL_COMPLETE); 5970 break; /* NOTREACHED */ 5971 } 5972 5973 /* 5974 * The first check is to make sure we're in bounds, the second 5975 * check is to catch wrap-around problems. If the lba + num blocks 5976 * is less than the lba, then we've wrapped around and the block 5977 * range is invalid anyway. 5978 */ 5979 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5980 || ((lba + num_blocks) < lba)) { 5981 ctl_set_lba_out_of_range(ctsio); 5982 ctl_done((union ctl_io *)ctsio); 5983 return (CTL_RETVAL_COMPLETE); 5984 } 5985 5986 /* Zero number of blocks means "to the last logical block" */ 5987 if (num_blocks == 0) { 5988 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5989 ctl_set_invalid_field(ctsio, 5990 /*sks_valid*/ 0, 5991 /*command*/ 1, 5992 /*field*/ 0, 5993 /*bit_valid*/ 0, 5994 /*bit*/ 0); 5995 ctl_done((union ctl_io *)ctsio); 5996 return (CTL_RETVAL_COMPLETE); 5997 } 5998 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5999 } 6000 6001 len = lun->be_lun->blocksize; 6002 6003 /* 6004 * If we've got a kernel request that hasn't been malloced yet, 6005 * malloc it and tell the caller the data buffer is here. 6006 */ 6007 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6008 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6009 ctsio->kern_data_len = len; 6010 ctsio->kern_total_len = len; 6011 ctsio->kern_data_resid = 0; 6012 ctsio->kern_rel_offset = 0; 6013 ctsio->kern_sg_entries = 0; 6014 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6015 ctsio->be_move_done = ctl_config_move_done; 6016 ctl_datamove((union ctl_io *)ctsio); 6017 6018 return (CTL_RETVAL_COMPLETE); 6019 } 6020 6021 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6022 lbalen->lba = lba; 6023 lbalen->len = num_blocks; 6024 lbalen->flags = byte2; 6025 retval = lun->backend->config_write((union ctl_io *)ctsio); 6026 6027 return (retval); 6028} 6029 6030int 6031ctl_unmap(struct ctl_scsiio *ctsio) 6032{ 6033 struct ctl_lun *lun; 6034 struct scsi_unmap *cdb; 6035 struct ctl_ptr_len_flags *ptrlen; 6036 struct scsi_unmap_header *hdr; 6037 struct scsi_unmap_desc *buf, *end; 6038 uint64_t lba; 6039 uint32_t num_blocks; 6040 int len, retval; 6041 uint8_t byte2; 6042 6043 retval = CTL_RETVAL_COMPLETE; 6044 6045 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6046 6047 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6048 cdb = (struct scsi_unmap *)ctsio->cdb; 6049 6050 len = scsi_2btoul(cdb->length); 6051 byte2 = cdb->byte2; 6052 6053 /* 6054 * If we've got a kernel request that hasn't been malloced yet, 6055 * malloc it and tell the caller the data buffer is here. 6056 */ 6057 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6058 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6059 ctsio->kern_data_len = len; 6060 ctsio->kern_total_len = len; 6061 ctsio->kern_data_resid = 0; 6062 ctsio->kern_rel_offset = 0; 6063 ctsio->kern_sg_entries = 0; 6064 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6065 ctsio->be_move_done = ctl_config_move_done; 6066 ctl_datamove((union ctl_io *)ctsio); 6067 6068 return (CTL_RETVAL_COMPLETE); 6069 } 6070 6071 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6072 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6073 if (len < sizeof (*hdr) || 6074 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6075 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6076 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6077 ctl_set_invalid_field(ctsio, 6078 /*sks_valid*/ 0, 6079 /*command*/ 0, 6080 /*field*/ 0, 6081 /*bit_valid*/ 0, 6082 /*bit*/ 0); 6083 ctl_done((union ctl_io *)ctsio); 6084 return (CTL_RETVAL_COMPLETE); 6085 } 6086 len = scsi_2btoul(hdr->desc_length); 6087 buf = (struct scsi_unmap_desc *)(hdr + 1); 6088 end = buf + len / sizeof(*buf); 6089 6090 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6091 ptrlen->ptr = (void *)buf; 6092 ptrlen->len = len; 6093 ptrlen->flags = byte2; 6094 6095 for (; buf < end; buf++) { 6096 lba = scsi_8btou64(buf->lba); 6097 num_blocks = scsi_4btoul(buf->length); 6098 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6099 || ((lba + num_blocks) < lba)) { 6100 ctl_set_lba_out_of_range(ctsio); 6101 ctl_done((union ctl_io *)ctsio); 6102 return (CTL_RETVAL_COMPLETE); 6103 } 6104 } 6105 6106 retval = lun->backend->config_write((union ctl_io *)ctsio); 6107 6108 return (retval); 6109} 6110 6111/* 6112 * Note that this function currently doesn't actually do anything inside 6113 * CTL to enforce things if the DQue bit is turned on. 6114 * 6115 * Also note that this function can't be used in the default case, because 6116 * the DQue bit isn't set in the changeable mask for the control mode page 6117 * anyway. This is just here as an example for how to implement a page 6118 * handler, and a placeholder in case we want to allow the user to turn 6119 * tagged queueing on and off. 6120 * 6121 * The D_SENSE bit handling is functional, however, and will turn 6122 * descriptor sense on and off for a given LUN. 6123 */ 6124int 6125ctl_control_page_handler(struct ctl_scsiio *ctsio, 6126 struct ctl_page_index *page_index, uint8_t *page_ptr) 6127{ 6128 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6129 struct ctl_lun *lun; 6130 struct ctl_softc *softc; 6131 int set_ua; 6132 uint32_t initidx; 6133 6134 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6135 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6136 set_ua = 0; 6137 6138 user_cp = (struct scsi_control_page *)page_ptr; 6139 current_cp = (struct scsi_control_page *) 6140 (page_index->page_data + (page_index->page_len * 6141 CTL_PAGE_CURRENT)); 6142 saved_cp = (struct scsi_control_page *) 6143 (page_index->page_data + (page_index->page_len * 6144 CTL_PAGE_SAVED)); 6145 6146 softc = control_softc; 6147 6148 mtx_lock(&lun->lun_lock); 6149 if (((current_cp->rlec & SCP_DSENSE) == 0) 6150 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6151 /* 6152 * Descriptor sense is currently turned off and the user 6153 * wants to turn it on. 6154 */ 6155 current_cp->rlec |= SCP_DSENSE; 6156 saved_cp->rlec |= SCP_DSENSE; 6157 lun->flags |= CTL_LUN_SENSE_DESC; 6158 set_ua = 1; 6159 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6160 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6161 /* 6162 * Descriptor sense is currently turned on, and the user 6163 * wants to turn it off. 6164 */ 6165 current_cp->rlec &= ~SCP_DSENSE; 6166 saved_cp->rlec &= ~SCP_DSENSE; 6167 lun->flags &= ~CTL_LUN_SENSE_DESC; 6168 set_ua = 1; 6169 } 6170 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6171 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6172#ifdef NEEDTOPORT 6173 csevent_log(CSC_CTL | CSC_SHELF_SW | 6174 CTL_UNTAG_TO_UNTAG, 6175 csevent_LogType_Trace, 6176 csevent_Severity_Information, 6177 csevent_AlertLevel_Green, 6178 csevent_FRU_Firmware, 6179 csevent_FRU_Unknown, 6180 "Received untagged to untagged transition"); 6181#endif /* NEEDTOPORT */ 6182 } else { 6183#ifdef NEEDTOPORT 6184 csevent_log(CSC_CTL | CSC_SHELF_SW | 6185 CTL_UNTAG_TO_TAG, 6186 csevent_LogType_ConfigChange, 6187 csevent_Severity_Information, 6188 csevent_AlertLevel_Green, 6189 csevent_FRU_Firmware, 6190 csevent_FRU_Unknown, 6191 "Received untagged to tagged " 6192 "queueing transition"); 6193#endif /* NEEDTOPORT */ 6194 6195 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6196 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6197 set_ua = 1; 6198 } 6199 } else { 6200 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6201#ifdef NEEDTOPORT 6202 csevent_log(CSC_CTL | CSC_SHELF_SW | 6203 CTL_TAG_TO_UNTAG, 6204 csevent_LogType_ConfigChange, 6205 csevent_Severity_Warning, 6206 csevent_AlertLevel_Yellow, 6207 csevent_FRU_Firmware, 6208 csevent_FRU_Unknown, 6209 "Received tagged queueing to untagged " 6210 "transition"); 6211#endif /* NEEDTOPORT */ 6212 6213 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6214 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6215 set_ua = 1; 6216 } else { 6217#ifdef NEEDTOPORT 6218 csevent_log(CSC_CTL | CSC_SHELF_SW | 6219 CTL_TAG_TO_TAG, 6220 csevent_LogType_Trace, 6221 csevent_Severity_Information, 6222 csevent_AlertLevel_Green, 6223 csevent_FRU_Firmware, 6224 csevent_FRU_Unknown, 6225 "Received tagged queueing to tagged " 6226 "queueing transition"); 6227#endif /* NEEDTOPORT */ 6228 } 6229 } 6230 if (set_ua != 0) { 6231 int i; 6232 /* 6233 * Let other initiators know that the mode 6234 * parameters for this LUN have changed. 6235 */ 6236 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6237 if (i == initidx) 6238 continue; 6239 6240 lun->pending_sense[i].ua_pending |= 6241 CTL_UA_MODE_CHANGE; 6242 } 6243 } 6244 mtx_unlock(&lun->lun_lock); 6245 6246 return (0); 6247} 6248 6249int 6250ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6251 struct ctl_page_index *page_index, uint8_t *page_ptr) 6252{ 6253 return (0); 6254} 6255 6256int 6257ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6258 struct ctl_page_index *page_index, int pc) 6259{ 6260 struct copan_power_subpage *page; 6261 6262 page = (struct copan_power_subpage *)page_index->page_data + 6263 (page_index->page_len * pc); 6264 6265 switch (pc) { 6266 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6267 /* 6268 * We don't update the changable bits for this page. 6269 */ 6270 break; 6271 case SMS_PAGE_CTRL_CURRENT >> 6: 6272 case SMS_PAGE_CTRL_DEFAULT >> 6: 6273 case SMS_PAGE_CTRL_SAVED >> 6: 6274#ifdef NEEDTOPORT 6275 ctl_update_power_subpage(page); 6276#endif 6277 break; 6278 default: 6279#ifdef NEEDTOPORT 6280 EPRINT(0, "Invalid PC %d!!", pc); 6281#endif 6282 break; 6283 } 6284 return (0); 6285} 6286 6287 6288int 6289ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6290 struct ctl_page_index *page_index, uint8_t *page_ptr) 6291{ 6292 struct copan_aps_subpage *user_sp; 6293 struct copan_aps_subpage *current_sp; 6294 union ctl_modepage_info *modepage_info; 6295 struct ctl_softc *softc; 6296 struct ctl_lun *lun; 6297 int retval; 6298 6299 retval = CTL_RETVAL_COMPLETE; 6300 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6301 (page_index->page_len * CTL_PAGE_CURRENT)); 6302 softc = control_softc; 6303 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6304 6305 user_sp = (struct copan_aps_subpage *)page_ptr; 6306 6307 modepage_info = (union ctl_modepage_info *) 6308 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6309 6310 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6311 modepage_info->header.subpage = page_index->subpage; 6312 modepage_info->aps.lock_active = user_sp->lock_active; 6313 6314 mtx_lock(&softc->ctl_lock); 6315 6316 /* 6317 * If there is a request to lock the LUN and another LUN is locked 6318 * this is an error. If the requested LUN is already locked ignore 6319 * the request. If no LUN is locked attempt to lock it. 6320 * if there is a request to unlock the LUN and the LUN is currently 6321 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6322 * if another LUN is locked or no LUN is locked. 6323 */ 6324 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6325 if (softc->aps_locked_lun == lun->lun) { 6326 /* 6327 * This LUN is already locked, so we're done. 6328 */ 6329 retval = CTL_RETVAL_COMPLETE; 6330 } else if (softc->aps_locked_lun == 0) { 6331 /* 6332 * No one has the lock, pass the request to the 6333 * backend. 6334 */ 6335 retval = lun->backend->config_write( 6336 (union ctl_io *)ctsio); 6337 } else { 6338 /* 6339 * Someone else has the lock, throw out the request. 6340 */ 6341 ctl_set_already_locked(ctsio); 6342 free(ctsio->kern_data_ptr, M_CTL); 6343 ctl_done((union ctl_io *)ctsio); 6344 6345 /* 6346 * Set the return value so that ctl_do_mode_select() 6347 * won't try to complete the command. We already 6348 * completed it here. 6349 */ 6350 retval = CTL_RETVAL_ERROR; 6351 } 6352 } else if (softc->aps_locked_lun == lun->lun) { 6353 /* 6354 * This LUN is locked, so pass the unlock request to the 6355 * backend. 6356 */ 6357 retval = lun->backend->config_write((union ctl_io *)ctsio); 6358 } 6359 mtx_unlock(&softc->ctl_lock); 6360 6361 return (retval); 6362} 6363 6364int 6365ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6366 struct ctl_page_index *page_index, 6367 uint8_t *page_ptr) 6368{ 6369 uint8_t *c; 6370 int i; 6371 6372 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6373 ctl_time_io_secs = 6374 (c[0] << 8) | 6375 (c[1] << 0) | 6376 0; 6377 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6378 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6379 printf("page data:"); 6380 for (i=0; i<8; i++) 6381 printf(" %.2x",page_ptr[i]); 6382 printf("\n"); 6383 return (0); 6384} 6385 6386int 6387ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6388 struct ctl_page_index *page_index, 6389 int pc) 6390{ 6391 struct copan_debugconf_subpage *page; 6392 6393 page = (struct copan_debugconf_subpage *)page_index->page_data + 6394 (page_index->page_len * pc); 6395 6396 switch (pc) { 6397 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6398 case SMS_PAGE_CTRL_DEFAULT >> 6: 6399 case SMS_PAGE_CTRL_SAVED >> 6: 6400 /* 6401 * We don't update the changable or default bits for this page. 6402 */ 6403 break; 6404 case SMS_PAGE_CTRL_CURRENT >> 6: 6405 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6406 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6407 break; 6408 default: 6409#ifdef NEEDTOPORT 6410 EPRINT(0, "Invalid PC %d!!", pc); 6411#endif /* NEEDTOPORT */ 6412 break; 6413 } 6414 return (0); 6415} 6416 6417 6418static int 6419ctl_do_mode_select(union ctl_io *io) 6420{ 6421 struct scsi_mode_page_header *page_header; 6422 struct ctl_page_index *page_index; 6423 struct ctl_scsiio *ctsio; 6424 int control_dev, page_len; 6425 int page_len_offset, page_len_size; 6426 union ctl_modepage_info *modepage_info; 6427 struct ctl_lun *lun; 6428 int *len_left, *len_used; 6429 int retval, i; 6430 6431 ctsio = &io->scsiio; 6432 page_index = NULL; 6433 page_len = 0; 6434 retval = CTL_RETVAL_COMPLETE; 6435 6436 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6437 6438 if (lun->be_lun->lun_type != T_DIRECT) 6439 control_dev = 1; 6440 else 6441 control_dev = 0; 6442 6443 modepage_info = (union ctl_modepage_info *) 6444 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6445 len_left = &modepage_info->header.len_left; 6446 len_used = &modepage_info->header.len_used; 6447 6448do_next_page: 6449 6450 page_header = (struct scsi_mode_page_header *) 6451 (ctsio->kern_data_ptr + *len_used); 6452 6453 if (*len_left == 0) { 6454 free(ctsio->kern_data_ptr, M_CTL); 6455 ctl_set_success(ctsio); 6456 ctl_done((union ctl_io *)ctsio); 6457 return (CTL_RETVAL_COMPLETE); 6458 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6459 6460 free(ctsio->kern_data_ptr, M_CTL); 6461 ctl_set_param_len_error(ctsio); 6462 ctl_done((union ctl_io *)ctsio); 6463 return (CTL_RETVAL_COMPLETE); 6464 6465 } else if ((page_header->page_code & SMPH_SPF) 6466 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6467 6468 free(ctsio->kern_data_ptr, M_CTL); 6469 ctl_set_param_len_error(ctsio); 6470 ctl_done((union ctl_io *)ctsio); 6471 return (CTL_RETVAL_COMPLETE); 6472 } 6473 6474 6475 /* 6476 * XXX KDM should we do something with the block descriptor? 6477 */ 6478 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6479 6480 if ((control_dev != 0) 6481 && (lun->mode_pages.index[i].page_flags & 6482 CTL_PAGE_FLAG_DISK_ONLY)) 6483 continue; 6484 6485 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6486 (page_header->page_code & SMPH_PC_MASK)) 6487 continue; 6488 6489 /* 6490 * If neither page has a subpage code, then we've got a 6491 * match. 6492 */ 6493 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6494 && ((page_header->page_code & SMPH_SPF) == 0)) { 6495 page_index = &lun->mode_pages.index[i]; 6496 page_len = page_header->page_length; 6497 break; 6498 } 6499 6500 /* 6501 * If both pages have subpages, then the subpage numbers 6502 * have to match. 6503 */ 6504 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6505 && (page_header->page_code & SMPH_SPF)) { 6506 struct scsi_mode_page_header_sp *sph; 6507 6508 sph = (struct scsi_mode_page_header_sp *)page_header; 6509 6510 if (lun->mode_pages.index[i].subpage == 6511 sph->subpage) { 6512 page_index = &lun->mode_pages.index[i]; 6513 page_len = scsi_2btoul(sph->page_length); 6514 break; 6515 } 6516 } 6517 } 6518 6519 /* 6520 * If we couldn't find the page, or if we don't have a mode select 6521 * handler for it, send back an error to the user. 6522 */ 6523 if ((page_index == NULL) 6524 || (page_index->select_handler == NULL)) { 6525 ctl_set_invalid_field(ctsio, 6526 /*sks_valid*/ 1, 6527 /*command*/ 0, 6528 /*field*/ *len_used, 6529 /*bit_valid*/ 0, 6530 /*bit*/ 0); 6531 free(ctsio->kern_data_ptr, M_CTL); 6532 ctl_done((union ctl_io *)ctsio); 6533 return (CTL_RETVAL_COMPLETE); 6534 } 6535 6536 if (page_index->page_code & SMPH_SPF) { 6537 page_len_offset = 2; 6538 page_len_size = 2; 6539 } else { 6540 page_len_size = 1; 6541 page_len_offset = 1; 6542 } 6543 6544 /* 6545 * If the length the initiator gives us isn't the one we specify in 6546 * the mode page header, or if they didn't specify enough data in 6547 * the CDB to avoid truncating this page, kick out the request. 6548 */ 6549 if ((page_len != (page_index->page_len - page_len_offset - 6550 page_len_size)) 6551 || (*len_left < page_index->page_len)) { 6552 6553 6554 ctl_set_invalid_field(ctsio, 6555 /*sks_valid*/ 1, 6556 /*command*/ 0, 6557 /*field*/ *len_used + page_len_offset, 6558 /*bit_valid*/ 0, 6559 /*bit*/ 0); 6560 free(ctsio->kern_data_ptr, M_CTL); 6561 ctl_done((union ctl_io *)ctsio); 6562 return (CTL_RETVAL_COMPLETE); 6563 } 6564 6565 /* 6566 * Run through the mode page, checking to make sure that the bits 6567 * the user changed are actually legal for him to change. 6568 */ 6569 for (i = 0; i < page_index->page_len; i++) { 6570 uint8_t *user_byte, *change_mask, *current_byte; 6571 int bad_bit; 6572 int j; 6573 6574 user_byte = (uint8_t *)page_header + i; 6575 change_mask = page_index->page_data + 6576 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6577 current_byte = page_index->page_data + 6578 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6579 6580 /* 6581 * Check to see whether the user set any bits in this byte 6582 * that he is not allowed to set. 6583 */ 6584 if ((*user_byte & ~(*change_mask)) == 6585 (*current_byte & ~(*change_mask))) 6586 continue; 6587 6588 /* 6589 * Go through bit by bit to determine which one is illegal. 6590 */ 6591 bad_bit = 0; 6592 for (j = 7; j >= 0; j--) { 6593 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6594 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6595 bad_bit = i; 6596 break; 6597 } 6598 } 6599 ctl_set_invalid_field(ctsio, 6600 /*sks_valid*/ 1, 6601 /*command*/ 0, 6602 /*field*/ *len_used + i, 6603 /*bit_valid*/ 1, 6604 /*bit*/ bad_bit); 6605 free(ctsio->kern_data_ptr, M_CTL); 6606 ctl_done((union ctl_io *)ctsio); 6607 return (CTL_RETVAL_COMPLETE); 6608 } 6609 6610 /* 6611 * Decrement these before we call the page handler, since we may 6612 * end up getting called back one way or another before the handler 6613 * returns to this context. 6614 */ 6615 *len_left -= page_index->page_len; 6616 *len_used += page_index->page_len; 6617 6618 retval = page_index->select_handler(ctsio, page_index, 6619 (uint8_t *)page_header); 6620 6621 /* 6622 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6623 * wait until this queued command completes to finish processing 6624 * the mode page. If it returns anything other than 6625 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6626 * already set the sense information, freed the data pointer, and 6627 * completed the io for us. 6628 */ 6629 if (retval != CTL_RETVAL_COMPLETE) 6630 goto bailout_no_done; 6631 6632 /* 6633 * If the initiator sent us more than one page, parse the next one. 6634 */ 6635 if (*len_left > 0) 6636 goto do_next_page; 6637 6638 ctl_set_success(ctsio); 6639 free(ctsio->kern_data_ptr, M_CTL); 6640 ctl_done((union ctl_io *)ctsio); 6641 6642bailout_no_done: 6643 6644 return (CTL_RETVAL_COMPLETE); 6645 6646} 6647 6648int 6649ctl_mode_select(struct ctl_scsiio *ctsio) 6650{ 6651 int param_len, pf, sp; 6652 int header_size, bd_len; 6653 int len_left, len_used; 6654 struct ctl_page_index *page_index; 6655 struct ctl_lun *lun; 6656 int control_dev, page_len; 6657 union ctl_modepage_info *modepage_info; 6658 int retval; 6659 6660 pf = 0; 6661 sp = 0; 6662 page_len = 0; 6663 len_used = 0; 6664 len_left = 0; 6665 retval = 0; 6666 bd_len = 0; 6667 page_index = NULL; 6668 6669 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6670 6671 if (lun->be_lun->lun_type != T_DIRECT) 6672 control_dev = 1; 6673 else 6674 control_dev = 0; 6675 6676 switch (ctsio->cdb[0]) { 6677 case MODE_SELECT_6: { 6678 struct scsi_mode_select_6 *cdb; 6679 6680 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6681 6682 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6683 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6684 6685 param_len = cdb->length; 6686 header_size = sizeof(struct scsi_mode_header_6); 6687 break; 6688 } 6689 case MODE_SELECT_10: { 6690 struct scsi_mode_select_10 *cdb; 6691 6692 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6693 6694 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6695 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6696 6697 param_len = scsi_2btoul(cdb->length); 6698 header_size = sizeof(struct scsi_mode_header_10); 6699 break; 6700 } 6701 default: 6702 ctl_set_invalid_opcode(ctsio); 6703 ctl_done((union ctl_io *)ctsio); 6704 return (CTL_RETVAL_COMPLETE); 6705 break; /* NOTREACHED */ 6706 } 6707 6708 /* 6709 * From SPC-3: 6710 * "A parameter list length of zero indicates that the Data-Out Buffer 6711 * shall be empty. This condition shall not be considered as an error." 6712 */ 6713 if (param_len == 0) { 6714 ctl_set_success(ctsio); 6715 ctl_done((union ctl_io *)ctsio); 6716 return (CTL_RETVAL_COMPLETE); 6717 } 6718 6719 /* 6720 * Since we'll hit this the first time through, prior to 6721 * allocation, we don't need to free a data buffer here. 6722 */ 6723 if (param_len < header_size) { 6724 ctl_set_param_len_error(ctsio); 6725 ctl_done((union ctl_io *)ctsio); 6726 return (CTL_RETVAL_COMPLETE); 6727 } 6728 6729 /* 6730 * Allocate the data buffer and grab the user's data. In theory, 6731 * we shouldn't have to sanity check the parameter list length here 6732 * because the maximum size is 64K. We should be able to malloc 6733 * that much without too many problems. 6734 */ 6735 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6736 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6737 ctsio->kern_data_len = param_len; 6738 ctsio->kern_total_len = param_len; 6739 ctsio->kern_data_resid = 0; 6740 ctsio->kern_rel_offset = 0; 6741 ctsio->kern_sg_entries = 0; 6742 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6743 ctsio->be_move_done = ctl_config_move_done; 6744 ctl_datamove((union ctl_io *)ctsio); 6745 6746 return (CTL_RETVAL_COMPLETE); 6747 } 6748 6749 switch (ctsio->cdb[0]) { 6750 case MODE_SELECT_6: { 6751 struct scsi_mode_header_6 *mh6; 6752 6753 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6754 bd_len = mh6->blk_desc_len; 6755 break; 6756 } 6757 case MODE_SELECT_10: { 6758 struct scsi_mode_header_10 *mh10; 6759 6760 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6761 bd_len = scsi_2btoul(mh10->blk_desc_len); 6762 break; 6763 } 6764 default: 6765 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6766 break; 6767 } 6768 6769 if (param_len < (header_size + bd_len)) { 6770 free(ctsio->kern_data_ptr, M_CTL); 6771 ctl_set_param_len_error(ctsio); 6772 ctl_done((union ctl_io *)ctsio); 6773 return (CTL_RETVAL_COMPLETE); 6774 } 6775 6776 /* 6777 * Set the IO_CONT flag, so that if this I/O gets passed to 6778 * ctl_config_write_done(), it'll get passed back to 6779 * ctl_do_mode_select() for further processing, or completion if 6780 * we're all done. 6781 */ 6782 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6783 ctsio->io_cont = ctl_do_mode_select; 6784 6785 modepage_info = (union ctl_modepage_info *) 6786 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6787 6788 memset(modepage_info, 0, sizeof(*modepage_info)); 6789 6790 len_left = param_len - header_size - bd_len; 6791 len_used = header_size + bd_len; 6792 6793 modepage_info->header.len_left = len_left; 6794 modepage_info->header.len_used = len_used; 6795 6796 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6797} 6798 6799int 6800ctl_mode_sense(struct ctl_scsiio *ctsio) 6801{ 6802 struct ctl_lun *lun; 6803 int pc, page_code, dbd, llba, subpage; 6804 int alloc_len, page_len, header_len, total_len; 6805 struct scsi_mode_block_descr *block_desc; 6806 struct ctl_page_index *page_index; 6807 int control_dev; 6808 6809 dbd = 0; 6810 llba = 0; 6811 block_desc = NULL; 6812 page_index = NULL; 6813 6814 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6815 6816 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6817 6818 if (lun->be_lun->lun_type != T_DIRECT) 6819 control_dev = 1; 6820 else 6821 control_dev = 0; 6822 6823 if (lun->flags & CTL_LUN_PR_RESERVED) { 6824 uint32_t residx; 6825 6826 /* 6827 * XXX KDM need a lock here. 6828 */ 6829 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6830 if ((lun->res_type == SPR_TYPE_EX_AC 6831 && residx != lun->pr_res_idx) 6832 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6833 || lun->res_type == SPR_TYPE_EX_AC_AR) 6834 && !lun->per_res[residx].registered)) { 6835 ctl_set_reservation_conflict(ctsio); 6836 ctl_done((union ctl_io *)ctsio); 6837 return (CTL_RETVAL_COMPLETE); 6838 } 6839 } 6840 6841 switch (ctsio->cdb[0]) { 6842 case MODE_SENSE_6: { 6843 struct scsi_mode_sense_6 *cdb; 6844 6845 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6846 6847 header_len = sizeof(struct scsi_mode_hdr_6); 6848 if (cdb->byte2 & SMS_DBD) 6849 dbd = 1; 6850 else 6851 header_len += sizeof(struct scsi_mode_block_descr); 6852 6853 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6854 page_code = cdb->page & SMS_PAGE_CODE; 6855 subpage = cdb->subpage; 6856 alloc_len = cdb->length; 6857 break; 6858 } 6859 case MODE_SENSE_10: { 6860 struct scsi_mode_sense_10 *cdb; 6861 6862 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6863 6864 header_len = sizeof(struct scsi_mode_hdr_10); 6865 6866 if (cdb->byte2 & SMS_DBD) 6867 dbd = 1; 6868 else 6869 header_len += sizeof(struct scsi_mode_block_descr); 6870 if (cdb->byte2 & SMS10_LLBAA) 6871 llba = 1; 6872 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6873 page_code = cdb->page & SMS_PAGE_CODE; 6874 subpage = cdb->subpage; 6875 alloc_len = scsi_2btoul(cdb->length); 6876 break; 6877 } 6878 default: 6879 ctl_set_invalid_opcode(ctsio); 6880 ctl_done((union ctl_io *)ctsio); 6881 return (CTL_RETVAL_COMPLETE); 6882 break; /* NOTREACHED */ 6883 } 6884 6885 /* 6886 * We have to make a first pass through to calculate the size of 6887 * the pages that match the user's query. Then we allocate enough 6888 * memory to hold it, and actually copy the data into the buffer. 6889 */ 6890 switch (page_code) { 6891 case SMS_ALL_PAGES_PAGE: { 6892 int i; 6893 6894 page_len = 0; 6895 6896 /* 6897 * At the moment, values other than 0 and 0xff here are 6898 * reserved according to SPC-3. 6899 */ 6900 if ((subpage != SMS_SUBPAGE_PAGE_0) 6901 && (subpage != SMS_SUBPAGE_ALL)) { 6902 ctl_set_invalid_field(ctsio, 6903 /*sks_valid*/ 1, 6904 /*command*/ 1, 6905 /*field*/ 3, 6906 /*bit_valid*/ 0, 6907 /*bit*/ 0); 6908 ctl_done((union ctl_io *)ctsio); 6909 return (CTL_RETVAL_COMPLETE); 6910 } 6911 6912 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6913 if ((control_dev != 0) 6914 && (lun->mode_pages.index[i].page_flags & 6915 CTL_PAGE_FLAG_DISK_ONLY)) 6916 continue; 6917 6918 /* 6919 * We don't use this subpage if the user didn't 6920 * request all subpages. 6921 */ 6922 if ((lun->mode_pages.index[i].subpage != 0) 6923 && (subpage == SMS_SUBPAGE_PAGE_0)) 6924 continue; 6925 6926#if 0 6927 printf("found page %#x len %d\n", 6928 lun->mode_pages.index[i].page_code & 6929 SMPH_PC_MASK, 6930 lun->mode_pages.index[i].page_len); 6931#endif 6932 page_len += lun->mode_pages.index[i].page_len; 6933 } 6934 break; 6935 } 6936 default: { 6937 int i; 6938 6939 page_len = 0; 6940 6941 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6942 /* Look for the right page code */ 6943 if ((lun->mode_pages.index[i].page_code & 6944 SMPH_PC_MASK) != page_code) 6945 continue; 6946 6947 /* Look for the right subpage or the subpage wildcard*/ 6948 if ((lun->mode_pages.index[i].subpage != subpage) 6949 && (subpage != SMS_SUBPAGE_ALL)) 6950 continue; 6951 6952 /* Make sure the page is supported for this dev type */ 6953 if ((control_dev != 0) 6954 && (lun->mode_pages.index[i].page_flags & 6955 CTL_PAGE_FLAG_DISK_ONLY)) 6956 continue; 6957 6958#if 0 6959 printf("found page %#x len %d\n", 6960 lun->mode_pages.index[i].page_code & 6961 SMPH_PC_MASK, 6962 lun->mode_pages.index[i].page_len); 6963#endif 6964 6965 page_len += lun->mode_pages.index[i].page_len; 6966 } 6967 6968 if (page_len == 0) { 6969 ctl_set_invalid_field(ctsio, 6970 /*sks_valid*/ 1, 6971 /*command*/ 1, 6972 /*field*/ 2, 6973 /*bit_valid*/ 1, 6974 /*bit*/ 5); 6975 ctl_done((union ctl_io *)ctsio); 6976 return (CTL_RETVAL_COMPLETE); 6977 } 6978 break; 6979 } 6980 } 6981 6982 total_len = header_len + page_len; 6983#if 0 6984 printf("header_len = %d, page_len = %d, total_len = %d\n", 6985 header_len, page_len, total_len); 6986#endif 6987 6988 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6989 ctsio->kern_sg_entries = 0; 6990 ctsio->kern_data_resid = 0; 6991 ctsio->kern_rel_offset = 0; 6992 if (total_len < alloc_len) { 6993 ctsio->residual = alloc_len - total_len; 6994 ctsio->kern_data_len = total_len; 6995 ctsio->kern_total_len = total_len; 6996 } else { 6997 ctsio->residual = 0; 6998 ctsio->kern_data_len = alloc_len; 6999 ctsio->kern_total_len = alloc_len; 7000 } 7001 7002 switch (ctsio->cdb[0]) { 7003 case MODE_SENSE_6: { 7004 struct scsi_mode_hdr_6 *header; 7005 7006 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7007 7008 header->datalen = ctl_min(total_len - 1, 254); 7009 7010 if (dbd) 7011 header->block_descr_len = 0; 7012 else 7013 header->block_descr_len = 7014 sizeof(struct scsi_mode_block_descr); 7015 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7016 break; 7017 } 7018 case MODE_SENSE_10: { 7019 struct scsi_mode_hdr_10 *header; 7020 int datalen; 7021 7022 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7023 7024 datalen = ctl_min(total_len - 2, 65533); 7025 scsi_ulto2b(datalen, header->datalen); 7026 if (dbd) 7027 scsi_ulto2b(0, header->block_descr_len); 7028 else 7029 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7030 header->block_descr_len); 7031 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7032 break; 7033 } 7034 default: 7035 panic("invalid CDB type %#x", ctsio->cdb[0]); 7036 break; /* NOTREACHED */ 7037 } 7038 7039 /* 7040 * If we've got a disk, use its blocksize in the block 7041 * descriptor. Otherwise, just set it to 0. 7042 */ 7043 if (dbd == 0) { 7044 if (control_dev != 0) 7045 scsi_ulto3b(lun->be_lun->blocksize, 7046 block_desc->block_len); 7047 else 7048 scsi_ulto3b(0, block_desc->block_len); 7049 } 7050 7051 switch (page_code) { 7052 case SMS_ALL_PAGES_PAGE: { 7053 int i, data_used; 7054 7055 data_used = header_len; 7056 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7057 struct ctl_page_index *page_index; 7058 7059 page_index = &lun->mode_pages.index[i]; 7060 7061 if ((control_dev != 0) 7062 && (page_index->page_flags & 7063 CTL_PAGE_FLAG_DISK_ONLY)) 7064 continue; 7065 7066 /* 7067 * We don't use this subpage if the user didn't 7068 * request all subpages. We already checked (above) 7069 * to make sure the user only specified a subpage 7070 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7071 */ 7072 if ((page_index->subpage != 0) 7073 && (subpage == SMS_SUBPAGE_PAGE_0)) 7074 continue; 7075 7076 /* 7077 * Call the handler, if it exists, to update the 7078 * page to the latest values. 7079 */ 7080 if (page_index->sense_handler != NULL) 7081 page_index->sense_handler(ctsio, page_index,pc); 7082 7083 memcpy(ctsio->kern_data_ptr + data_used, 7084 page_index->page_data + 7085 (page_index->page_len * pc), 7086 page_index->page_len); 7087 data_used += page_index->page_len; 7088 } 7089 break; 7090 } 7091 default: { 7092 int i, data_used; 7093 7094 data_used = header_len; 7095 7096 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7097 struct ctl_page_index *page_index; 7098 7099 page_index = &lun->mode_pages.index[i]; 7100 7101 /* Look for the right page code */ 7102 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7103 continue; 7104 7105 /* Look for the right subpage or the subpage wildcard*/ 7106 if ((page_index->subpage != subpage) 7107 && (subpage != SMS_SUBPAGE_ALL)) 7108 continue; 7109 7110 /* Make sure the page is supported for this dev type */ 7111 if ((control_dev != 0) 7112 && (page_index->page_flags & 7113 CTL_PAGE_FLAG_DISK_ONLY)) 7114 continue; 7115 7116 /* 7117 * Call the handler, if it exists, to update the 7118 * page to the latest values. 7119 */ 7120 if (page_index->sense_handler != NULL) 7121 page_index->sense_handler(ctsio, page_index,pc); 7122 7123 memcpy(ctsio->kern_data_ptr + data_used, 7124 page_index->page_data + 7125 (page_index->page_len * pc), 7126 page_index->page_len); 7127 data_used += page_index->page_len; 7128 } 7129 break; 7130 } 7131 } 7132 7133 ctsio->scsi_status = SCSI_STATUS_OK; 7134 7135 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7136 ctsio->be_move_done = ctl_config_move_done; 7137 ctl_datamove((union ctl_io *)ctsio); 7138 7139 return (CTL_RETVAL_COMPLETE); 7140} 7141 7142int 7143ctl_read_capacity(struct ctl_scsiio *ctsio) 7144{ 7145 struct scsi_read_capacity *cdb; 7146 struct scsi_read_capacity_data *data; 7147 struct ctl_lun *lun; 7148 uint32_t lba; 7149 7150 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7151 7152 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7153 7154 lba = scsi_4btoul(cdb->addr); 7155 if (((cdb->pmi & SRC_PMI) == 0) 7156 && (lba != 0)) { 7157 ctl_set_invalid_field(/*ctsio*/ ctsio, 7158 /*sks_valid*/ 1, 7159 /*command*/ 1, 7160 /*field*/ 2, 7161 /*bit_valid*/ 0, 7162 /*bit*/ 0); 7163 ctl_done((union ctl_io *)ctsio); 7164 return (CTL_RETVAL_COMPLETE); 7165 } 7166 7167 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7168 7169 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7170 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7171 ctsio->residual = 0; 7172 ctsio->kern_data_len = sizeof(*data); 7173 ctsio->kern_total_len = sizeof(*data); 7174 ctsio->kern_data_resid = 0; 7175 ctsio->kern_rel_offset = 0; 7176 ctsio->kern_sg_entries = 0; 7177 7178 /* 7179 * If the maximum LBA is greater than 0xfffffffe, the user must 7180 * issue a SERVICE ACTION IN (16) command, with the read capacity 7181 * serivce action set. 7182 */ 7183 if (lun->be_lun->maxlba > 0xfffffffe) 7184 scsi_ulto4b(0xffffffff, data->addr); 7185 else 7186 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7187 7188 /* 7189 * XXX KDM this may not be 512 bytes... 7190 */ 7191 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7192 7193 ctsio->scsi_status = SCSI_STATUS_OK; 7194 7195 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7196 ctsio->be_move_done = ctl_config_move_done; 7197 ctl_datamove((union ctl_io *)ctsio); 7198 7199 return (CTL_RETVAL_COMPLETE); 7200} 7201 7202int 7203ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7204{ 7205 struct scsi_read_capacity_16 *cdb; 7206 struct scsi_read_capacity_data_long *data; 7207 struct ctl_lun *lun; 7208 uint64_t lba; 7209 uint32_t alloc_len; 7210 7211 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7212 7213 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7214 7215 alloc_len = scsi_4btoul(cdb->alloc_len); 7216 lba = scsi_8btou64(cdb->addr); 7217 7218 if ((cdb->reladr & SRC16_PMI) 7219 && (lba != 0)) { 7220 ctl_set_invalid_field(/*ctsio*/ ctsio, 7221 /*sks_valid*/ 1, 7222 /*command*/ 1, 7223 /*field*/ 2, 7224 /*bit_valid*/ 0, 7225 /*bit*/ 0); 7226 ctl_done((union ctl_io *)ctsio); 7227 return (CTL_RETVAL_COMPLETE); 7228 } 7229 7230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7231 7232 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7233 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7234 7235 if (sizeof(*data) < alloc_len) { 7236 ctsio->residual = alloc_len - sizeof(*data); 7237 ctsio->kern_data_len = sizeof(*data); 7238 ctsio->kern_total_len = sizeof(*data); 7239 } else { 7240 ctsio->residual = 0; 7241 ctsio->kern_data_len = alloc_len; 7242 ctsio->kern_total_len = alloc_len; 7243 } 7244 ctsio->kern_data_resid = 0; 7245 ctsio->kern_rel_offset = 0; 7246 ctsio->kern_sg_entries = 0; 7247 7248 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7249 /* XXX KDM this may not be 512 bytes... */ 7250 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7251 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7252 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7253 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7254 data->lalba_lbp[0] |= SRC16_LBPME; 7255 7256 ctsio->scsi_status = SCSI_STATUS_OK; 7257 7258 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7259 ctsio->be_move_done = ctl_config_move_done; 7260 ctl_datamove((union ctl_io *)ctsio); 7261 7262 return (CTL_RETVAL_COMPLETE); 7263} 7264 7265int 7266ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7267{ 7268 struct scsi_maintenance_in *cdb; 7269 int retval; 7270 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7271 int num_target_port_groups, num_target_ports, single; 7272 struct ctl_lun *lun; 7273 struct ctl_softc *softc; 7274 struct ctl_port *port; 7275 struct scsi_target_group_data *rtg_ptr; 7276 struct scsi_target_group_data_extended *rtg_ext_ptr; 7277 struct scsi_target_port_group_descriptor *tpg_desc; 7278 7279 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7280 7281 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7282 softc = control_softc; 7283 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7284 7285 retval = CTL_RETVAL_COMPLETE; 7286 7287 switch (cdb->byte2 & STG_PDF_MASK) { 7288 case STG_PDF_LENGTH: 7289 ext = 0; 7290 break; 7291 case STG_PDF_EXTENDED: 7292 ext = 1; 7293 break; 7294 default: 7295 ctl_set_invalid_field(/*ctsio*/ ctsio, 7296 /*sks_valid*/ 1, 7297 /*command*/ 1, 7298 /*field*/ 2, 7299 /*bit_valid*/ 1, 7300 /*bit*/ 5); 7301 ctl_done((union ctl_io *)ctsio); 7302 return(retval); 7303 } 7304 7305 single = ctl_is_single; 7306 if (single) 7307 num_target_port_groups = 1; 7308 else 7309 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7310 num_target_ports = 0; 7311 mtx_lock(&softc->ctl_lock); 7312 STAILQ_FOREACH(port, &softc->port_list, links) { 7313 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7314 continue; 7315 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7316 continue; 7317 num_target_ports++; 7318 } 7319 mtx_unlock(&softc->ctl_lock); 7320 7321 if (ext) 7322 total_len = sizeof(struct scsi_target_group_data_extended); 7323 else 7324 total_len = sizeof(struct scsi_target_group_data); 7325 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7326 num_target_port_groups + 7327 sizeof(struct scsi_target_port_descriptor) * 7328 num_target_ports * num_target_port_groups; 7329 7330 alloc_len = scsi_4btoul(cdb->length); 7331 7332 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7333 7334 ctsio->kern_sg_entries = 0; 7335 7336 if (total_len < alloc_len) { 7337 ctsio->residual = alloc_len - total_len; 7338 ctsio->kern_data_len = total_len; 7339 ctsio->kern_total_len = total_len; 7340 } else { 7341 ctsio->residual = 0; 7342 ctsio->kern_data_len = alloc_len; 7343 ctsio->kern_total_len = alloc_len; 7344 } 7345 ctsio->kern_data_resid = 0; 7346 ctsio->kern_rel_offset = 0; 7347 7348 if (ext) { 7349 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7350 ctsio->kern_data_ptr; 7351 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7352 rtg_ext_ptr->format_type = 0x10; 7353 rtg_ext_ptr->implicit_transition_time = 0; 7354 tpg_desc = &rtg_ext_ptr->groups[0]; 7355 } else { 7356 rtg_ptr = (struct scsi_target_group_data *) 7357 ctsio->kern_data_ptr; 7358 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7359 tpg_desc = &rtg_ptr->groups[0]; 7360 } 7361 7362 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7363 mtx_lock(&softc->ctl_lock); 7364 for (g = 0; g < num_target_port_groups; g++) { 7365 if (g == pg) 7366 tpg_desc->pref_state = TPG_PRIMARY | 7367 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7368 else 7369 tpg_desc->pref_state = 7370 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7371 tpg_desc->support = TPG_AO_SUP; 7372 if (!single) 7373 tpg_desc->support |= TPG_AN_SUP; 7374 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7375 tpg_desc->status = TPG_IMPLICIT; 7376 pc = 0; 7377 STAILQ_FOREACH(port, &softc->port_list, links) { 7378 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7379 continue; 7380 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7381 CTL_MAX_LUNS) 7382 continue; 7383 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7384 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7385 relative_target_port_identifier); 7386 pc++; 7387 } 7388 tpg_desc->target_port_count = pc; 7389 tpg_desc = (struct scsi_target_port_group_descriptor *) 7390 &tpg_desc->descriptors[pc]; 7391 } 7392 mtx_unlock(&softc->ctl_lock); 7393 7394 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7395 ctsio->be_move_done = ctl_config_move_done; 7396 7397 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7398 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7399 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7400 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7401 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7402 7403 ctl_datamove((union ctl_io *)ctsio); 7404 return(retval); 7405} 7406 7407int 7408ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7409{ 7410 struct ctl_lun *lun; 7411 struct scsi_report_supported_opcodes *cdb; 7412 const struct ctl_cmd_entry *entry, *sentry; 7413 struct scsi_report_supported_opcodes_all *all; 7414 struct scsi_report_supported_opcodes_descr *descr; 7415 struct scsi_report_supported_opcodes_one *one; 7416 int retval; 7417 int alloc_len, total_len; 7418 int opcode, service_action, i, j, num; 7419 7420 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7421 7422 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7423 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7424 7425 retval = CTL_RETVAL_COMPLETE; 7426 7427 opcode = cdb->requested_opcode; 7428 service_action = scsi_2btoul(cdb->requested_service_action); 7429 switch (cdb->options & RSO_OPTIONS_MASK) { 7430 case RSO_OPTIONS_ALL: 7431 num = 0; 7432 for (i = 0; i < 256; i++) { 7433 entry = &ctl_cmd_table[i]; 7434 if (entry->flags & CTL_CMD_FLAG_SA5) { 7435 for (j = 0; j < 32; j++) { 7436 sentry = &((const struct ctl_cmd_entry *) 7437 entry->execute)[j]; 7438 if (ctl_cmd_applicable( 7439 lun->be_lun->lun_type, sentry)) 7440 num++; 7441 } 7442 } else { 7443 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7444 entry)) 7445 num++; 7446 } 7447 } 7448 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7449 num * sizeof(struct scsi_report_supported_opcodes_descr); 7450 break; 7451 case RSO_OPTIONS_OC: 7452 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7453 ctl_set_invalid_field(/*ctsio*/ ctsio, 7454 /*sks_valid*/ 1, 7455 /*command*/ 1, 7456 /*field*/ 2, 7457 /*bit_valid*/ 1, 7458 /*bit*/ 2); 7459 ctl_done((union ctl_io *)ctsio); 7460 return (CTL_RETVAL_COMPLETE); 7461 } 7462 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7463 break; 7464 case RSO_OPTIONS_OC_SA: 7465 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7466 service_action >= 32) { 7467 ctl_set_invalid_field(/*ctsio*/ ctsio, 7468 /*sks_valid*/ 1, 7469 /*command*/ 1, 7470 /*field*/ 2, 7471 /*bit_valid*/ 1, 7472 /*bit*/ 2); 7473 ctl_done((union ctl_io *)ctsio); 7474 return (CTL_RETVAL_COMPLETE); 7475 } 7476 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7477 break; 7478 default: 7479 ctl_set_invalid_field(/*ctsio*/ ctsio, 7480 /*sks_valid*/ 1, 7481 /*command*/ 1, 7482 /*field*/ 2, 7483 /*bit_valid*/ 1, 7484 /*bit*/ 2); 7485 ctl_done((union ctl_io *)ctsio); 7486 return (CTL_RETVAL_COMPLETE); 7487 } 7488 7489 alloc_len = scsi_4btoul(cdb->length); 7490 7491 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7492 7493 ctsio->kern_sg_entries = 0; 7494 7495 if (total_len < alloc_len) { 7496 ctsio->residual = alloc_len - total_len; 7497 ctsio->kern_data_len = total_len; 7498 ctsio->kern_total_len = total_len; 7499 } else { 7500 ctsio->residual = 0; 7501 ctsio->kern_data_len = alloc_len; 7502 ctsio->kern_total_len = alloc_len; 7503 } 7504 ctsio->kern_data_resid = 0; 7505 ctsio->kern_rel_offset = 0; 7506 7507 switch (cdb->options & RSO_OPTIONS_MASK) { 7508 case RSO_OPTIONS_ALL: 7509 all = (struct scsi_report_supported_opcodes_all *) 7510 ctsio->kern_data_ptr; 7511 num = 0; 7512 for (i = 0; i < 256; i++) { 7513 entry = &ctl_cmd_table[i]; 7514 if (entry->flags & CTL_CMD_FLAG_SA5) { 7515 for (j = 0; j < 32; j++) { 7516 sentry = &((const struct ctl_cmd_entry *) 7517 entry->execute)[j]; 7518 if (!ctl_cmd_applicable( 7519 lun->be_lun->lun_type, sentry)) 7520 continue; 7521 descr = &all->descr[num++]; 7522 descr->opcode = i; 7523 scsi_ulto2b(j, descr->service_action); 7524 descr->flags = RSO_SERVACTV; 7525 scsi_ulto2b(sentry->length, 7526 descr->cdb_length); 7527 } 7528 } else { 7529 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7530 entry)) 7531 continue; 7532 descr = &all->descr[num++]; 7533 descr->opcode = i; 7534 scsi_ulto2b(0, descr->service_action); 7535 descr->flags = 0; 7536 scsi_ulto2b(entry->length, descr->cdb_length); 7537 } 7538 } 7539 scsi_ulto4b( 7540 num * sizeof(struct scsi_report_supported_opcodes_descr), 7541 all->length); 7542 break; 7543 case RSO_OPTIONS_OC: 7544 one = (struct scsi_report_supported_opcodes_one *) 7545 ctsio->kern_data_ptr; 7546 entry = &ctl_cmd_table[opcode]; 7547 goto fill_one; 7548 case RSO_OPTIONS_OC_SA: 7549 one = (struct scsi_report_supported_opcodes_one *) 7550 ctsio->kern_data_ptr; 7551 entry = &ctl_cmd_table[opcode]; 7552 entry = &((const struct ctl_cmd_entry *) 7553 entry->execute)[service_action]; 7554fill_one: 7555 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7556 one->support = 3; 7557 scsi_ulto2b(entry->length, one->cdb_length); 7558 one->cdb_usage[0] = opcode; 7559 memcpy(&one->cdb_usage[1], entry->usage, 7560 entry->length - 1); 7561 } else 7562 one->support = 1; 7563 break; 7564 } 7565 7566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7567 ctsio->be_move_done = ctl_config_move_done; 7568 7569 ctl_datamove((union ctl_io *)ctsio); 7570 return(retval); 7571} 7572 7573int 7574ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7575{ 7576 struct ctl_lun *lun; 7577 struct scsi_report_supported_tmf *cdb; 7578 struct scsi_report_supported_tmf_data *data; 7579 int retval; 7580 int alloc_len, total_len; 7581 7582 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7583 7584 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7585 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7586 7587 retval = CTL_RETVAL_COMPLETE; 7588 7589 total_len = sizeof(struct scsi_report_supported_tmf_data); 7590 alloc_len = scsi_4btoul(cdb->length); 7591 7592 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7593 7594 ctsio->kern_sg_entries = 0; 7595 7596 if (total_len < alloc_len) { 7597 ctsio->residual = alloc_len - total_len; 7598 ctsio->kern_data_len = total_len; 7599 ctsio->kern_total_len = total_len; 7600 } else { 7601 ctsio->residual = 0; 7602 ctsio->kern_data_len = alloc_len; 7603 ctsio->kern_total_len = alloc_len; 7604 } 7605 ctsio->kern_data_resid = 0; 7606 ctsio->kern_rel_offset = 0; 7607 7608 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7609 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7610 data->byte2 |= RST_ITNRS; 7611 7612 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7613 ctsio->be_move_done = ctl_config_move_done; 7614 7615 ctl_datamove((union ctl_io *)ctsio); 7616 return (retval); 7617} 7618 7619int 7620ctl_report_timestamp(struct ctl_scsiio *ctsio) 7621{ 7622 struct ctl_lun *lun; 7623 struct scsi_report_timestamp *cdb; 7624 struct scsi_report_timestamp_data *data; 7625 struct timeval tv; 7626 int64_t timestamp; 7627 int retval; 7628 int alloc_len, total_len; 7629 7630 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7631 7632 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7633 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7634 7635 retval = CTL_RETVAL_COMPLETE; 7636 7637 total_len = sizeof(struct scsi_report_timestamp_data); 7638 alloc_len = scsi_4btoul(cdb->length); 7639 7640 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7641 7642 ctsio->kern_sg_entries = 0; 7643 7644 if (total_len < alloc_len) { 7645 ctsio->residual = alloc_len - total_len; 7646 ctsio->kern_data_len = total_len; 7647 ctsio->kern_total_len = total_len; 7648 } else { 7649 ctsio->residual = 0; 7650 ctsio->kern_data_len = alloc_len; 7651 ctsio->kern_total_len = alloc_len; 7652 } 7653 ctsio->kern_data_resid = 0; 7654 ctsio->kern_rel_offset = 0; 7655 7656 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7657 scsi_ulto2b(sizeof(*data) - 2, data->length); 7658 data->origin = RTS_ORIG_OUTSIDE; 7659 getmicrotime(&tv); 7660 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7661 scsi_ulto4b(timestamp >> 16, data->timestamp); 7662 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7663 7664 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7665 ctsio->be_move_done = ctl_config_move_done; 7666 7667 ctl_datamove((union ctl_io *)ctsio); 7668 return (retval); 7669} 7670 7671int 7672ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7673{ 7674 struct scsi_per_res_in *cdb; 7675 int alloc_len, total_len = 0; 7676 /* struct scsi_per_res_in_rsrv in_data; */ 7677 struct ctl_lun *lun; 7678 struct ctl_softc *softc; 7679 7680 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7681 7682 softc = control_softc; 7683 7684 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7685 7686 alloc_len = scsi_2btoul(cdb->length); 7687 7688 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7689 7690retry: 7691 mtx_lock(&lun->lun_lock); 7692 switch (cdb->action) { 7693 case SPRI_RK: /* read keys */ 7694 total_len = sizeof(struct scsi_per_res_in_keys) + 7695 lun->pr_key_count * 7696 sizeof(struct scsi_per_res_key); 7697 break; 7698 case SPRI_RR: /* read reservation */ 7699 if (lun->flags & CTL_LUN_PR_RESERVED) 7700 total_len = sizeof(struct scsi_per_res_in_rsrv); 7701 else 7702 total_len = sizeof(struct scsi_per_res_in_header); 7703 break; 7704 case SPRI_RC: /* report capabilities */ 7705 total_len = sizeof(struct scsi_per_res_cap); 7706 break; 7707 case SPRI_RS: /* read full status */ 7708 total_len = sizeof(struct scsi_per_res_in_header) + 7709 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7710 lun->pr_key_count; 7711 break; 7712 default: 7713 panic("Invalid PR type %x", cdb->action); 7714 } 7715 mtx_unlock(&lun->lun_lock); 7716 7717 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7718 7719 if (total_len < alloc_len) { 7720 ctsio->residual = alloc_len - total_len; 7721 ctsio->kern_data_len = total_len; 7722 ctsio->kern_total_len = total_len; 7723 } else { 7724 ctsio->residual = 0; 7725 ctsio->kern_data_len = alloc_len; 7726 ctsio->kern_total_len = alloc_len; 7727 } 7728 7729 ctsio->kern_data_resid = 0; 7730 ctsio->kern_rel_offset = 0; 7731 ctsio->kern_sg_entries = 0; 7732 7733 mtx_lock(&lun->lun_lock); 7734 switch (cdb->action) { 7735 case SPRI_RK: { // read keys 7736 struct scsi_per_res_in_keys *res_keys; 7737 int i, key_count; 7738 7739 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7740 7741 /* 7742 * We had to drop the lock to allocate our buffer, which 7743 * leaves time for someone to come in with another 7744 * persistent reservation. (That is unlikely, though, 7745 * since this should be the only persistent reservation 7746 * command active right now.) 7747 */ 7748 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7749 (lun->pr_key_count * 7750 sizeof(struct scsi_per_res_key)))){ 7751 mtx_unlock(&lun->lun_lock); 7752 free(ctsio->kern_data_ptr, M_CTL); 7753 printf("%s: reservation length changed, retrying\n", 7754 __func__); 7755 goto retry; 7756 } 7757 7758 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7759 7760 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7761 lun->pr_key_count, res_keys->header.length); 7762 7763 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7764 if (!lun->per_res[i].registered) 7765 continue; 7766 7767 /* 7768 * We used lun->pr_key_count to calculate the 7769 * size to allocate. If it turns out the number of 7770 * initiators with the registered flag set is 7771 * larger than that (i.e. they haven't been kept in 7772 * sync), we've got a problem. 7773 */ 7774 if (key_count >= lun->pr_key_count) { 7775#ifdef NEEDTOPORT 7776 csevent_log(CSC_CTL | CSC_SHELF_SW | 7777 CTL_PR_ERROR, 7778 csevent_LogType_Fault, 7779 csevent_AlertLevel_Yellow, 7780 csevent_FRU_ShelfController, 7781 csevent_FRU_Firmware, 7782 csevent_FRU_Unknown, 7783 "registered keys %d >= key " 7784 "count %d", key_count, 7785 lun->pr_key_count); 7786#endif 7787 key_count++; 7788 continue; 7789 } 7790 memcpy(res_keys->keys[key_count].key, 7791 lun->per_res[i].res_key.key, 7792 ctl_min(sizeof(res_keys->keys[key_count].key), 7793 sizeof(lun->per_res[i].res_key))); 7794 key_count++; 7795 } 7796 break; 7797 } 7798 case SPRI_RR: { // read reservation 7799 struct scsi_per_res_in_rsrv *res; 7800 int tmp_len, header_only; 7801 7802 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7803 7804 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7805 7806 if (lun->flags & CTL_LUN_PR_RESERVED) 7807 { 7808 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7809 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7810 res->header.length); 7811 header_only = 0; 7812 } else { 7813 tmp_len = sizeof(struct scsi_per_res_in_header); 7814 scsi_ulto4b(0, res->header.length); 7815 header_only = 1; 7816 } 7817 7818 /* 7819 * We had to drop the lock to allocate our buffer, which 7820 * leaves time for someone to come in with another 7821 * persistent reservation. (That is unlikely, though, 7822 * since this should be the only persistent reservation 7823 * command active right now.) 7824 */ 7825 if (tmp_len != total_len) { 7826 mtx_unlock(&lun->lun_lock); 7827 free(ctsio->kern_data_ptr, M_CTL); 7828 printf("%s: reservation status changed, retrying\n", 7829 __func__); 7830 goto retry; 7831 } 7832 7833 /* 7834 * No reservation held, so we're done. 7835 */ 7836 if (header_only != 0) 7837 break; 7838 7839 /* 7840 * If the registration is an All Registrants type, the key 7841 * is 0, since it doesn't really matter. 7842 */ 7843 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7844 memcpy(res->data.reservation, 7845 &lun->per_res[lun->pr_res_idx].res_key, 7846 sizeof(struct scsi_per_res_key)); 7847 } 7848 res->data.scopetype = lun->res_type; 7849 break; 7850 } 7851 case SPRI_RC: //report capabilities 7852 { 7853 struct scsi_per_res_cap *res_cap; 7854 uint16_t type_mask; 7855 7856 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7857 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7858 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7859 type_mask = SPRI_TM_WR_EX_AR | 7860 SPRI_TM_EX_AC_RO | 7861 SPRI_TM_WR_EX_RO | 7862 SPRI_TM_EX_AC | 7863 SPRI_TM_WR_EX | 7864 SPRI_TM_EX_AC_AR; 7865 scsi_ulto2b(type_mask, res_cap->type_mask); 7866 break; 7867 } 7868 case SPRI_RS: { // read full status 7869 struct scsi_per_res_in_full *res_status; 7870 struct scsi_per_res_in_full_desc *res_desc; 7871 struct ctl_port *port; 7872 int i, len; 7873 7874 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7875 7876 /* 7877 * We had to drop the lock to allocate our buffer, which 7878 * leaves time for someone to come in with another 7879 * persistent reservation. (That is unlikely, though, 7880 * since this should be the only persistent reservation 7881 * command active right now.) 7882 */ 7883 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7884 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7885 lun->pr_key_count)){ 7886 mtx_unlock(&lun->lun_lock); 7887 free(ctsio->kern_data_ptr, M_CTL); 7888 printf("%s: reservation length changed, retrying\n", 7889 __func__); 7890 goto retry; 7891 } 7892 7893 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7894 7895 res_desc = &res_status->desc[0]; 7896 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7897 if (!lun->per_res[i].registered) 7898 continue; 7899 7900 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7901 sizeof(res_desc->res_key)); 7902 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7903 (lun->pr_res_idx == i || 7904 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7905 res_desc->flags = SPRI_FULL_R_HOLDER; 7906 res_desc->scopetype = lun->res_type; 7907 } 7908 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7909 res_desc->rel_trgt_port_id); 7910 len = 0; 7911 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7912 if (port != NULL) 7913 len = ctl_create_iid(port, 7914 i % CTL_MAX_INIT_PER_PORT, 7915 res_desc->transport_id); 7916 scsi_ulto4b(len, res_desc->additional_length); 7917 res_desc = (struct scsi_per_res_in_full_desc *) 7918 &res_desc->transport_id[len]; 7919 } 7920 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7921 res_status->header.length); 7922 break; 7923 } 7924 default: 7925 /* 7926 * This is a bug, because we just checked for this above, 7927 * and should have returned an error. 7928 */ 7929 panic("Invalid PR type %x", cdb->action); 7930 break; /* NOTREACHED */ 7931 } 7932 mtx_unlock(&lun->lun_lock); 7933 7934 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7935 ctsio->be_move_done = ctl_config_move_done; 7936 7937 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7938 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7939 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7940 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7941 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7942 7943 ctl_datamove((union ctl_io *)ctsio); 7944 7945 return (CTL_RETVAL_COMPLETE); 7946} 7947 7948/* 7949 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7950 * it should return. 7951 */ 7952static int 7953ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7954 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7955 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7956 struct scsi_per_res_out_parms* param) 7957{ 7958 union ctl_ha_msg persis_io; 7959 int retval, i; 7960 int isc_retval; 7961 7962 retval = 0; 7963 7964 mtx_lock(&lun->lun_lock); 7965 if (sa_res_key == 0) { 7966 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7967 /* validate scope and type */ 7968 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7969 SPR_LU_SCOPE) { 7970 mtx_unlock(&lun->lun_lock); 7971 ctl_set_invalid_field(/*ctsio*/ ctsio, 7972 /*sks_valid*/ 1, 7973 /*command*/ 1, 7974 /*field*/ 2, 7975 /*bit_valid*/ 1, 7976 /*bit*/ 4); 7977 ctl_done((union ctl_io *)ctsio); 7978 return (1); 7979 } 7980 7981 if (type>8 || type==2 || type==4 || type==0) { 7982 mtx_unlock(&lun->lun_lock); 7983 ctl_set_invalid_field(/*ctsio*/ ctsio, 7984 /*sks_valid*/ 1, 7985 /*command*/ 1, 7986 /*field*/ 2, 7987 /*bit_valid*/ 1, 7988 /*bit*/ 0); 7989 ctl_done((union ctl_io *)ctsio); 7990 return (1); 7991 } 7992 7993 /* temporarily unregister this nexus */ 7994 lun->per_res[residx].registered = 0; 7995 7996 /* 7997 * Unregister everybody else and build UA for 7998 * them 7999 */ 8000 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8001 if (lun->per_res[i].registered == 0) 8002 continue; 8003 8004 if (!persis_offset 8005 && i <CTL_MAX_INITIATORS) 8006 lun->pending_sense[i].ua_pending |= 8007 CTL_UA_REG_PREEMPT; 8008 else if (persis_offset 8009 && i >= persis_offset) 8010 lun->pending_sense[i-persis_offset 8011 ].ua_pending |= 8012 CTL_UA_REG_PREEMPT; 8013 lun->per_res[i].registered = 0; 8014 memset(&lun->per_res[i].res_key, 0, 8015 sizeof(struct scsi_per_res_key)); 8016 } 8017 lun->per_res[residx].registered = 1; 8018 lun->pr_key_count = 1; 8019 lun->res_type = type; 8020 if (lun->res_type != SPR_TYPE_WR_EX_AR 8021 && lun->res_type != SPR_TYPE_EX_AC_AR) 8022 lun->pr_res_idx = residx; 8023 8024 /* send msg to other side */ 8025 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8026 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8027 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8028 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8029 persis_io.pr.pr_info.res_type = type; 8030 memcpy(persis_io.pr.pr_info.sa_res_key, 8031 param->serv_act_res_key, 8032 sizeof(param->serv_act_res_key)); 8033 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8034 &persis_io, sizeof(persis_io), 0)) > 8035 CTL_HA_STATUS_SUCCESS) { 8036 printf("CTL:Persis Out error returned " 8037 "from ctl_ha_msg_send %d\n", 8038 isc_retval); 8039 } 8040 } else { 8041 /* not all registrants */ 8042 mtx_unlock(&lun->lun_lock); 8043 free(ctsio->kern_data_ptr, M_CTL); 8044 ctl_set_invalid_field(ctsio, 8045 /*sks_valid*/ 1, 8046 /*command*/ 0, 8047 /*field*/ 8, 8048 /*bit_valid*/ 0, 8049 /*bit*/ 0); 8050 ctl_done((union ctl_io *)ctsio); 8051 return (1); 8052 } 8053 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8054 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8055 int found = 0; 8056 8057 if (res_key == sa_res_key) { 8058 /* special case */ 8059 /* 8060 * The spec implies this is not good but doesn't 8061 * say what to do. There are two choices either 8062 * generate a res conflict or check condition 8063 * with illegal field in parameter data. Since 8064 * that is what is done when the sa_res_key is 8065 * zero I'll take that approach since this has 8066 * to do with the sa_res_key. 8067 */ 8068 mtx_unlock(&lun->lun_lock); 8069 free(ctsio->kern_data_ptr, M_CTL); 8070 ctl_set_invalid_field(ctsio, 8071 /*sks_valid*/ 1, 8072 /*command*/ 0, 8073 /*field*/ 8, 8074 /*bit_valid*/ 0, 8075 /*bit*/ 0); 8076 ctl_done((union ctl_io *)ctsio); 8077 return (1); 8078 } 8079 8080 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8081 if (lun->per_res[i].registered 8082 && memcmp(param->serv_act_res_key, 8083 lun->per_res[i].res_key.key, 8084 sizeof(struct scsi_per_res_key)) != 0) 8085 continue; 8086 8087 found = 1; 8088 lun->per_res[i].registered = 0; 8089 memset(&lun->per_res[i].res_key, 0, 8090 sizeof(struct scsi_per_res_key)); 8091 lun->pr_key_count--; 8092 8093 if (!persis_offset 8094 && i < CTL_MAX_INITIATORS) 8095 lun->pending_sense[i].ua_pending |= 8096 CTL_UA_REG_PREEMPT; 8097 else if (persis_offset 8098 && i >= persis_offset) 8099 lun->pending_sense[i-persis_offset].ua_pending|= 8100 CTL_UA_REG_PREEMPT; 8101 } 8102 if (!found) { 8103 mtx_unlock(&lun->lun_lock); 8104 free(ctsio->kern_data_ptr, M_CTL); 8105 ctl_set_reservation_conflict(ctsio); 8106 ctl_done((union ctl_io *)ctsio); 8107 return (CTL_RETVAL_COMPLETE); 8108 } 8109 /* send msg to other side */ 8110 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8111 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8112 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8113 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8114 persis_io.pr.pr_info.res_type = type; 8115 memcpy(persis_io.pr.pr_info.sa_res_key, 8116 param->serv_act_res_key, 8117 sizeof(param->serv_act_res_key)); 8118 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8119 &persis_io, sizeof(persis_io), 0)) > 8120 CTL_HA_STATUS_SUCCESS) { 8121 printf("CTL:Persis Out error returned from " 8122 "ctl_ha_msg_send %d\n", isc_retval); 8123 } 8124 } else { 8125 /* Reserved but not all registrants */ 8126 /* sa_res_key is res holder */ 8127 if (memcmp(param->serv_act_res_key, 8128 lun->per_res[lun->pr_res_idx].res_key.key, 8129 sizeof(struct scsi_per_res_key)) == 0) { 8130 /* validate scope and type */ 8131 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8132 SPR_LU_SCOPE) { 8133 mtx_unlock(&lun->lun_lock); 8134 ctl_set_invalid_field(/*ctsio*/ ctsio, 8135 /*sks_valid*/ 1, 8136 /*command*/ 1, 8137 /*field*/ 2, 8138 /*bit_valid*/ 1, 8139 /*bit*/ 4); 8140 ctl_done((union ctl_io *)ctsio); 8141 return (1); 8142 } 8143 8144 if (type>8 || type==2 || type==4 || type==0) { 8145 mtx_unlock(&lun->lun_lock); 8146 ctl_set_invalid_field(/*ctsio*/ ctsio, 8147 /*sks_valid*/ 1, 8148 /*command*/ 1, 8149 /*field*/ 2, 8150 /*bit_valid*/ 1, 8151 /*bit*/ 0); 8152 ctl_done((union ctl_io *)ctsio); 8153 return (1); 8154 } 8155 8156 /* 8157 * Do the following: 8158 * if sa_res_key != res_key remove all 8159 * registrants w/sa_res_key and generate UA 8160 * for these registrants(Registrations 8161 * Preempted) if it wasn't an exclusive 8162 * reservation generate UA(Reservations 8163 * Preempted) for all other registered nexuses 8164 * if the type has changed. Establish the new 8165 * reservation and holder. If res_key and 8166 * sa_res_key are the same do the above 8167 * except don't unregister the res holder. 8168 */ 8169 8170 /* 8171 * Temporarily unregister so it won't get 8172 * removed or UA generated 8173 */ 8174 lun->per_res[residx].registered = 0; 8175 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8176 if (lun->per_res[i].registered == 0) 8177 continue; 8178 8179 if (memcmp(param->serv_act_res_key, 8180 lun->per_res[i].res_key.key, 8181 sizeof(struct scsi_per_res_key)) == 0) { 8182 lun->per_res[i].registered = 0; 8183 memset(&lun->per_res[i].res_key, 8184 0, 8185 sizeof(struct scsi_per_res_key)); 8186 lun->pr_key_count--; 8187 8188 if (!persis_offset 8189 && i < CTL_MAX_INITIATORS) 8190 lun->pending_sense[i 8191 ].ua_pending |= 8192 CTL_UA_REG_PREEMPT; 8193 else if (persis_offset 8194 && i >= persis_offset) 8195 lun->pending_sense[ 8196 i-persis_offset].ua_pending |= 8197 CTL_UA_REG_PREEMPT; 8198 } else if (type != lun->res_type 8199 && (lun->res_type == SPR_TYPE_WR_EX_RO 8200 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8201 if (!persis_offset 8202 && i < CTL_MAX_INITIATORS) 8203 lun->pending_sense[i 8204 ].ua_pending |= 8205 CTL_UA_RES_RELEASE; 8206 else if (persis_offset 8207 && i >= persis_offset) 8208 lun->pending_sense[ 8209 i-persis_offset 8210 ].ua_pending |= 8211 CTL_UA_RES_RELEASE; 8212 } 8213 } 8214 lun->per_res[residx].registered = 1; 8215 lun->res_type = type; 8216 if (lun->res_type != SPR_TYPE_WR_EX_AR 8217 && lun->res_type != SPR_TYPE_EX_AC_AR) 8218 lun->pr_res_idx = residx; 8219 else 8220 lun->pr_res_idx = 8221 CTL_PR_ALL_REGISTRANTS; 8222 8223 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8224 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8225 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8226 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8227 persis_io.pr.pr_info.res_type = type; 8228 memcpy(persis_io.pr.pr_info.sa_res_key, 8229 param->serv_act_res_key, 8230 sizeof(param->serv_act_res_key)); 8231 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8232 &persis_io, sizeof(persis_io), 0)) > 8233 CTL_HA_STATUS_SUCCESS) { 8234 printf("CTL:Persis Out error returned " 8235 "from ctl_ha_msg_send %d\n", 8236 isc_retval); 8237 } 8238 } else { 8239 /* 8240 * sa_res_key is not the res holder just 8241 * remove registrants 8242 */ 8243 int found=0; 8244 8245 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8246 if (memcmp(param->serv_act_res_key, 8247 lun->per_res[i].res_key.key, 8248 sizeof(struct scsi_per_res_key)) != 0) 8249 continue; 8250 8251 found = 1; 8252 lun->per_res[i].registered = 0; 8253 memset(&lun->per_res[i].res_key, 0, 8254 sizeof(struct scsi_per_res_key)); 8255 lun->pr_key_count--; 8256 8257 if (!persis_offset 8258 && i < CTL_MAX_INITIATORS) 8259 lun->pending_sense[i].ua_pending |= 8260 CTL_UA_REG_PREEMPT; 8261 else if (persis_offset 8262 && i >= persis_offset) 8263 lun->pending_sense[ 8264 i-persis_offset].ua_pending |= 8265 CTL_UA_REG_PREEMPT; 8266 } 8267 8268 if (!found) { 8269 mtx_unlock(&lun->lun_lock); 8270 free(ctsio->kern_data_ptr, M_CTL); 8271 ctl_set_reservation_conflict(ctsio); 8272 ctl_done((union ctl_io *)ctsio); 8273 return (1); 8274 } 8275 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8276 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8277 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8278 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8279 persis_io.pr.pr_info.res_type = type; 8280 memcpy(persis_io.pr.pr_info.sa_res_key, 8281 param->serv_act_res_key, 8282 sizeof(param->serv_act_res_key)); 8283 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8284 &persis_io, sizeof(persis_io), 0)) > 8285 CTL_HA_STATUS_SUCCESS) { 8286 printf("CTL:Persis Out error returned " 8287 "from ctl_ha_msg_send %d\n", 8288 isc_retval); 8289 } 8290 } 8291 } 8292 8293 lun->PRGeneration++; 8294 mtx_unlock(&lun->lun_lock); 8295 8296 return (retval); 8297} 8298 8299static void 8300ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8301{ 8302 int i; 8303 8304 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8305 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8306 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8307 msg->pr.pr_info.sa_res_key, 8308 sizeof(struct scsi_per_res_key)) != 0) { 8309 uint64_t sa_res_key; 8310 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8311 8312 if (sa_res_key == 0) { 8313 /* temporarily unregister this nexus */ 8314 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8315 8316 /* 8317 * Unregister everybody else and build UA for 8318 * them 8319 */ 8320 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8321 if (lun->per_res[i].registered == 0) 8322 continue; 8323 8324 if (!persis_offset 8325 && i < CTL_MAX_INITIATORS) 8326 lun->pending_sense[i].ua_pending |= 8327 CTL_UA_REG_PREEMPT; 8328 else if (persis_offset && i >= persis_offset) 8329 lun->pending_sense[i - 8330 persis_offset].ua_pending |= 8331 CTL_UA_REG_PREEMPT; 8332 lun->per_res[i].registered = 0; 8333 memset(&lun->per_res[i].res_key, 0, 8334 sizeof(struct scsi_per_res_key)); 8335 } 8336 8337 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8338 lun->pr_key_count = 1; 8339 lun->res_type = msg->pr.pr_info.res_type; 8340 if (lun->res_type != SPR_TYPE_WR_EX_AR 8341 && lun->res_type != SPR_TYPE_EX_AC_AR) 8342 lun->pr_res_idx = msg->pr.pr_info.residx; 8343 } else { 8344 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8345 if (memcmp(msg->pr.pr_info.sa_res_key, 8346 lun->per_res[i].res_key.key, 8347 sizeof(struct scsi_per_res_key)) != 0) 8348 continue; 8349 8350 lun->per_res[i].registered = 0; 8351 memset(&lun->per_res[i].res_key, 0, 8352 sizeof(struct scsi_per_res_key)); 8353 lun->pr_key_count--; 8354 8355 if (!persis_offset 8356 && i < persis_offset) 8357 lun->pending_sense[i].ua_pending |= 8358 CTL_UA_REG_PREEMPT; 8359 else if (persis_offset 8360 && i >= persis_offset) 8361 lun->pending_sense[i - 8362 persis_offset].ua_pending |= 8363 CTL_UA_REG_PREEMPT; 8364 } 8365 } 8366 } else { 8367 /* 8368 * Temporarily unregister so it won't get removed 8369 * or UA generated 8370 */ 8371 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8372 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8373 if (lun->per_res[i].registered == 0) 8374 continue; 8375 8376 if (memcmp(msg->pr.pr_info.sa_res_key, 8377 lun->per_res[i].res_key.key, 8378 sizeof(struct scsi_per_res_key)) == 0) { 8379 lun->per_res[i].registered = 0; 8380 memset(&lun->per_res[i].res_key, 0, 8381 sizeof(struct scsi_per_res_key)); 8382 lun->pr_key_count--; 8383 if (!persis_offset 8384 && i < CTL_MAX_INITIATORS) 8385 lun->pending_sense[i].ua_pending |= 8386 CTL_UA_REG_PREEMPT; 8387 else if (persis_offset 8388 && i >= persis_offset) 8389 lun->pending_sense[i - 8390 persis_offset].ua_pending |= 8391 CTL_UA_REG_PREEMPT; 8392 } else if (msg->pr.pr_info.res_type != lun->res_type 8393 && (lun->res_type == SPR_TYPE_WR_EX_RO 8394 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8395 if (!persis_offset 8396 && i < persis_offset) 8397 lun->pending_sense[i 8398 ].ua_pending |= 8399 CTL_UA_RES_RELEASE; 8400 else if (persis_offset 8401 && i >= persis_offset) 8402 lun->pending_sense[i - 8403 persis_offset].ua_pending |= 8404 CTL_UA_RES_RELEASE; 8405 } 8406 } 8407 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8408 lun->res_type = msg->pr.pr_info.res_type; 8409 if (lun->res_type != SPR_TYPE_WR_EX_AR 8410 && lun->res_type != SPR_TYPE_EX_AC_AR) 8411 lun->pr_res_idx = msg->pr.pr_info.residx; 8412 else 8413 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8414 } 8415 lun->PRGeneration++; 8416 8417} 8418 8419 8420int 8421ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8422{ 8423 int retval; 8424 int isc_retval; 8425 u_int32_t param_len; 8426 struct scsi_per_res_out *cdb; 8427 struct ctl_lun *lun; 8428 struct scsi_per_res_out_parms* param; 8429 struct ctl_softc *softc; 8430 uint32_t residx; 8431 uint64_t res_key, sa_res_key; 8432 uint8_t type; 8433 union ctl_ha_msg persis_io; 8434 int i; 8435 8436 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8437 8438 retval = CTL_RETVAL_COMPLETE; 8439 8440 softc = control_softc; 8441 8442 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8443 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8444 8445 /* 8446 * We only support whole-LUN scope. The scope & type are ignored for 8447 * register, register and ignore existing key and clear. 8448 * We sometimes ignore scope and type on preempts too!! 8449 * Verify reservation type here as well. 8450 */ 8451 type = cdb->scope_type & SPR_TYPE_MASK; 8452 if ((cdb->action == SPRO_RESERVE) 8453 || (cdb->action == SPRO_RELEASE)) { 8454 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8455 ctl_set_invalid_field(/*ctsio*/ ctsio, 8456 /*sks_valid*/ 1, 8457 /*command*/ 1, 8458 /*field*/ 2, 8459 /*bit_valid*/ 1, 8460 /*bit*/ 4); 8461 ctl_done((union ctl_io *)ctsio); 8462 return (CTL_RETVAL_COMPLETE); 8463 } 8464 8465 if (type>8 || type==2 || type==4 || type==0) { 8466 ctl_set_invalid_field(/*ctsio*/ ctsio, 8467 /*sks_valid*/ 1, 8468 /*command*/ 1, 8469 /*field*/ 2, 8470 /*bit_valid*/ 1, 8471 /*bit*/ 0); 8472 ctl_done((union ctl_io *)ctsio); 8473 return (CTL_RETVAL_COMPLETE); 8474 } 8475 } 8476 8477 param_len = scsi_4btoul(cdb->length); 8478 8479 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8480 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8481 ctsio->kern_data_len = param_len; 8482 ctsio->kern_total_len = param_len; 8483 ctsio->kern_data_resid = 0; 8484 ctsio->kern_rel_offset = 0; 8485 ctsio->kern_sg_entries = 0; 8486 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8487 ctsio->be_move_done = ctl_config_move_done; 8488 ctl_datamove((union ctl_io *)ctsio); 8489 8490 return (CTL_RETVAL_COMPLETE); 8491 } 8492 8493 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8494 8495 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8496 res_key = scsi_8btou64(param->res_key.key); 8497 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8498 8499 /* 8500 * Validate the reservation key here except for SPRO_REG_IGNO 8501 * This must be done for all other service actions 8502 */ 8503 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8504 mtx_lock(&lun->lun_lock); 8505 if (lun->per_res[residx].registered) { 8506 if (memcmp(param->res_key.key, 8507 lun->per_res[residx].res_key.key, 8508 ctl_min(sizeof(param->res_key), 8509 sizeof(lun->per_res[residx].res_key))) != 0) { 8510 /* 8511 * The current key passed in doesn't match 8512 * the one the initiator previously 8513 * registered. 8514 */ 8515 mtx_unlock(&lun->lun_lock); 8516 free(ctsio->kern_data_ptr, M_CTL); 8517 ctl_set_reservation_conflict(ctsio); 8518 ctl_done((union ctl_io *)ctsio); 8519 return (CTL_RETVAL_COMPLETE); 8520 } 8521 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8522 /* 8523 * We are not registered 8524 */ 8525 mtx_unlock(&lun->lun_lock); 8526 free(ctsio->kern_data_ptr, M_CTL); 8527 ctl_set_reservation_conflict(ctsio); 8528 ctl_done((union ctl_io *)ctsio); 8529 return (CTL_RETVAL_COMPLETE); 8530 } else if (res_key != 0) { 8531 /* 8532 * We are not registered and trying to register but 8533 * the register key isn't zero. 8534 */ 8535 mtx_unlock(&lun->lun_lock); 8536 free(ctsio->kern_data_ptr, M_CTL); 8537 ctl_set_reservation_conflict(ctsio); 8538 ctl_done((union ctl_io *)ctsio); 8539 return (CTL_RETVAL_COMPLETE); 8540 } 8541 mtx_unlock(&lun->lun_lock); 8542 } 8543 8544 switch (cdb->action & SPRO_ACTION_MASK) { 8545 case SPRO_REGISTER: 8546 case SPRO_REG_IGNO: { 8547 8548#if 0 8549 printf("Registration received\n"); 8550#endif 8551 8552 /* 8553 * We don't support any of these options, as we report in 8554 * the read capabilities request (see 8555 * ctl_persistent_reserve_in(), above). 8556 */ 8557 if ((param->flags & SPR_SPEC_I_PT) 8558 || (param->flags & SPR_ALL_TG_PT) 8559 || (param->flags & SPR_APTPL)) { 8560 int bit_ptr; 8561 8562 if (param->flags & SPR_APTPL) 8563 bit_ptr = 0; 8564 else if (param->flags & SPR_ALL_TG_PT) 8565 bit_ptr = 2; 8566 else /* SPR_SPEC_I_PT */ 8567 bit_ptr = 3; 8568 8569 free(ctsio->kern_data_ptr, M_CTL); 8570 ctl_set_invalid_field(ctsio, 8571 /*sks_valid*/ 1, 8572 /*command*/ 0, 8573 /*field*/ 20, 8574 /*bit_valid*/ 1, 8575 /*bit*/ bit_ptr); 8576 ctl_done((union ctl_io *)ctsio); 8577 return (CTL_RETVAL_COMPLETE); 8578 } 8579 8580 mtx_lock(&lun->lun_lock); 8581 8582 /* 8583 * The initiator wants to clear the 8584 * key/unregister. 8585 */ 8586 if (sa_res_key == 0) { 8587 if ((res_key == 0 8588 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8589 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8590 && !lun->per_res[residx].registered)) { 8591 mtx_unlock(&lun->lun_lock); 8592 goto done; 8593 } 8594 8595 lun->per_res[residx].registered = 0; 8596 memset(&lun->per_res[residx].res_key, 8597 0, sizeof(lun->per_res[residx].res_key)); 8598 lun->pr_key_count--; 8599 8600 if (residx == lun->pr_res_idx) { 8601 lun->flags &= ~CTL_LUN_PR_RESERVED; 8602 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8603 8604 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8605 || lun->res_type == SPR_TYPE_EX_AC_RO) 8606 && lun->pr_key_count) { 8607 /* 8608 * If the reservation is a registrants 8609 * only type we need to generate a UA 8610 * for other registered inits. The 8611 * sense code should be RESERVATIONS 8612 * RELEASED 8613 */ 8614 8615 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8616 if (lun->per_res[ 8617 i+persis_offset].registered 8618 == 0) 8619 continue; 8620 lun->pending_sense[i 8621 ].ua_pending |= 8622 CTL_UA_RES_RELEASE; 8623 } 8624 } 8625 lun->res_type = 0; 8626 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8627 if (lun->pr_key_count==0) { 8628 lun->flags &= ~CTL_LUN_PR_RESERVED; 8629 lun->res_type = 0; 8630 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8631 } 8632 } 8633 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8634 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8635 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8636 persis_io.pr.pr_info.residx = residx; 8637 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8638 &persis_io, sizeof(persis_io), 0 )) > 8639 CTL_HA_STATUS_SUCCESS) { 8640 printf("CTL:Persis Out error returned from " 8641 "ctl_ha_msg_send %d\n", isc_retval); 8642 } 8643 } else /* sa_res_key != 0 */ { 8644 8645 /* 8646 * If we aren't registered currently then increment 8647 * the key count and set the registered flag. 8648 */ 8649 if (!lun->per_res[residx].registered) { 8650 lun->pr_key_count++; 8651 lun->per_res[residx].registered = 1; 8652 } 8653 8654 memcpy(&lun->per_res[residx].res_key, 8655 param->serv_act_res_key, 8656 ctl_min(sizeof(param->serv_act_res_key), 8657 sizeof(lun->per_res[residx].res_key))); 8658 8659 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8660 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8661 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8662 persis_io.pr.pr_info.residx = residx; 8663 memcpy(persis_io.pr.pr_info.sa_res_key, 8664 param->serv_act_res_key, 8665 sizeof(param->serv_act_res_key)); 8666 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8667 &persis_io, sizeof(persis_io), 0)) > 8668 CTL_HA_STATUS_SUCCESS) { 8669 printf("CTL:Persis Out error returned from " 8670 "ctl_ha_msg_send %d\n", isc_retval); 8671 } 8672 } 8673 lun->PRGeneration++; 8674 mtx_unlock(&lun->lun_lock); 8675 8676 break; 8677 } 8678 case SPRO_RESERVE: 8679#if 0 8680 printf("Reserve executed type %d\n", type); 8681#endif 8682 mtx_lock(&lun->lun_lock); 8683 if (lun->flags & CTL_LUN_PR_RESERVED) { 8684 /* 8685 * if this isn't the reservation holder and it's 8686 * not a "all registrants" type or if the type is 8687 * different then we have a conflict 8688 */ 8689 if ((lun->pr_res_idx != residx 8690 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8691 || lun->res_type != type) { 8692 mtx_unlock(&lun->lun_lock); 8693 free(ctsio->kern_data_ptr, M_CTL); 8694 ctl_set_reservation_conflict(ctsio); 8695 ctl_done((union ctl_io *)ctsio); 8696 return (CTL_RETVAL_COMPLETE); 8697 } 8698 mtx_unlock(&lun->lun_lock); 8699 } else /* create a reservation */ { 8700 /* 8701 * If it's not an "all registrants" type record 8702 * reservation holder 8703 */ 8704 if (type != SPR_TYPE_WR_EX_AR 8705 && type != SPR_TYPE_EX_AC_AR) 8706 lun->pr_res_idx = residx; /* Res holder */ 8707 else 8708 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8709 8710 lun->flags |= CTL_LUN_PR_RESERVED; 8711 lun->res_type = type; 8712 8713 mtx_unlock(&lun->lun_lock); 8714 8715 /* send msg to other side */ 8716 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8717 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8718 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8719 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8720 persis_io.pr.pr_info.res_type = type; 8721 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8722 &persis_io, sizeof(persis_io), 0)) > 8723 CTL_HA_STATUS_SUCCESS) { 8724 printf("CTL:Persis Out error returned from " 8725 "ctl_ha_msg_send %d\n", isc_retval); 8726 } 8727 } 8728 break; 8729 8730 case SPRO_RELEASE: 8731 mtx_lock(&lun->lun_lock); 8732 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8733 /* No reservation exists return good status */ 8734 mtx_unlock(&lun->lun_lock); 8735 goto done; 8736 } 8737 /* 8738 * Is this nexus a reservation holder? 8739 */ 8740 if (lun->pr_res_idx != residx 8741 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8742 /* 8743 * not a res holder return good status but 8744 * do nothing 8745 */ 8746 mtx_unlock(&lun->lun_lock); 8747 goto done; 8748 } 8749 8750 if (lun->res_type != type) { 8751 mtx_unlock(&lun->lun_lock); 8752 free(ctsio->kern_data_ptr, M_CTL); 8753 ctl_set_illegal_pr_release(ctsio); 8754 ctl_done((union ctl_io *)ctsio); 8755 return (CTL_RETVAL_COMPLETE); 8756 } 8757 8758 /* okay to release */ 8759 lun->flags &= ~CTL_LUN_PR_RESERVED; 8760 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8761 lun->res_type = 0; 8762 8763 /* 8764 * if this isn't an exclusive access 8765 * res generate UA for all other 8766 * registrants. 8767 */ 8768 if (type != SPR_TYPE_EX_AC 8769 && type != SPR_TYPE_WR_EX) { 8770 /* 8771 * temporarily unregister so we don't generate UA 8772 */ 8773 lun->per_res[residx].registered = 0; 8774 8775 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8776 if (lun->per_res[i+persis_offset].registered 8777 == 0) 8778 continue; 8779 lun->pending_sense[i].ua_pending |= 8780 CTL_UA_RES_RELEASE; 8781 } 8782 8783 lun->per_res[residx].registered = 1; 8784 } 8785 mtx_unlock(&lun->lun_lock); 8786 /* Send msg to other side */ 8787 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8788 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8789 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8790 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8791 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8792 printf("CTL:Persis Out error returned from " 8793 "ctl_ha_msg_send %d\n", isc_retval); 8794 } 8795 break; 8796 8797 case SPRO_CLEAR: 8798 /* send msg to other side */ 8799 8800 mtx_lock(&lun->lun_lock); 8801 lun->flags &= ~CTL_LUN_PR_RESERVED; 8802 lun->res_type = 0; 8803 lun->pr_key_count = 0; 8804 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8805 8806 8807 memset(&lun->per_res[residx].res_key, 8808 0, sizeof(lun->per_res[residx].res_key)); 8809 lun->per_res[residx].registered = 0; 8810 8811 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8812 if (lun->per_res[i].registered) { 8813 if (!persis_offset && i < CTL_MAX_INITIATORS) 8814 lun->pending_sense[i].ua_pending |= 8815 CTL_UA_RES_PREEMPT; 8816 else if (persis_offset && i >= persis_offset) 8817 lun->pending_sense[i-persis_offset 8818 ].ua_pending |= CTL_UA_RES_PREEMPT; 8819 8820 memset(&lun->per_res[i].res_key, 8821 0, sizeof(struct scsi_per_res_key)); 8822 lun->per_res[i].registered = 0; 8823 } 8824 lun->PRGeneration++; 8825 mtx_unlock(&lun->lun_lock); 8826 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8827 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8828 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8829 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8830 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8831 printf("CTL:Persis Out error returned from " 8832 "ctl_ha_msg_send %d\n", isc_retval); 8833 } 8834 break; 8835 8836 case SPRO_PREEMPT: { 8837 int nretval; 8838 8839 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8840 residx, ctsio, cdb, param); 8841 if (nretval != 0) 8842 return (CTL_RETVAL_COMPLETE); 8843 break; 8844 } 8845 default: 8846 panic("Invalid PR type %x", cdb->action); 8847 } 8848 8849done: 8850 free(ctsio->kern_data_ptr, M_CTL); 8851 ctl_set_success(ctsio); 8852 ctl_done((union ctl_io *)ctsio); 8853 8854 return (retval); 8855} 8856 8857/* 8858 * This routine is for handling a message from the other SC pertaining to 8859 * persistent reserve out. All the error checking will have been done 8860 * so only perorming the action need be done here to keep the two 8861 * in sync. 8862 */ 8863static void 8864ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8865{ 8866 struct ctl_lun *lun; 8867 struct ctl_softc *softc; 8868 int i; 8869 uint32_t targ_lun; 8870 8871 softc = control_softc; 8872 8873 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8874 lun = softc->ctl_luns[targ_lun]; 8875 mtx_lock(&lun->lun_lock); 8876 switch(msg->pr.pr_info.action) { 8877 case CTL_PR_REG_KEY: 8878 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8879 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8880 lun->pr_key_count++; 8881 } 8882 lun->PRGeneration++; 8883 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8884 msg->pr.pr_info.sa_res_key, 8885 sizeof(struct scsi_per_res_key)); 8886 break; 8887 8888 case CTL_PR_UNREG_KEY: 8889 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8890 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8891 0, sizeof(struct scsi_per_res_key)); 8892 lun->pr_key_count--; 8893 8894 /* XXX Need to see if the reservation has been released */ 8895 /* if so do we need to generate UA? */ 8896 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8897 lun->flags &= ~CTL_LUN_PR_RESERVED; 8898 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8899 8900 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8901 || lun->res_type == SPR_TYPE_EX_AC_RO) 8902 && lun->pr_key_count) { 8903 /* 8904 * If the reservation is a registrants 8905 * only type we need to generate a UA 8906 * for other registered inits. The 8907 * sense code should be RESERVATIONS 8908 * RELEASED 8909 */ 8910 8911 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8912 if (lun->per_res[i+ 8913 persis_offset].registered == 0) 8914 continue; 8915 8916 lun->pending_sense[i 8917 ].ua_pending |= 8918 CTL_UA_RES_RELEASE; 8919 } 8920 } 8921 lun->res_type = 0; 8922 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8923 if (lun->pr_key_count==0) { 8924 lun->flags &= ~CTL_LUN_PR_RESERVED; 8925 lun->res_type = 0; 8926 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8927 } 8928 } 8929 lun->PRGeneration++; 8930 break; 8931 8932 case CTL_PR_RESERVE: 8933 lun->flags |= CTL_LUN_PR_RESERVED; 8934 lun->res_type = msg->pr.pr_info.res_type; 8935 lun->pr_res_idx = msg->pr.pr_info.residx; 8936 8937 break; 8938 8939 case CTL_PR_RELEASE: 8940 /* 8941 * if this isn't an exclusive access res generate UA for all 8942 * other registrants. 8943 */ 8944 if (lun->res_type != SPR_TYPE_EX_AC 8945 && lun->res_type != SPR_TYPE_WR_EX) { 8946 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8947 if (lun->per_res[i+persis_offset].registered) 8948 lun->pending_sense[i].ua_pending |= 8949 CTL_UA_RES_RELEASE; 8950 } 8951 8952 lun->flags &= ~CTL_LUN_PR_RESERVED; 8953 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8954 lun->res_type = 0; 8955 break; 8956 8957 case CTL_PR_PREEMPT: 8958 ctl_pro_preempt_other(lun, msg); 8959 break; 8960 case CTL_PR_CLEAR: 8961 lun->flags &= ~CTL_LUN_PR_RESERVED; 8962 lun->res_type = 0; 8963 lun->pr_key_count = 0; 8964 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8965 8966 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8967 if (lun->per_res[i].registered == 0) 8968 continue; 8969 if (!persis_offset 8970 && i < CTL_MAX_INITIATORS) 8971 lun->pending_sense[i].ua_pending |= 8972 CTL_UA_RES_PREEMPT; 8973 else if (persis_offset 8974 && i >= persis_offset) 8975 lun->pending_sense[i-persis_offset].ua_pending|= 8976 CTL_UA_RES_PREEMPT; 8977 memset(&lun->per_res[i].res_key, 0, 8978 sizeof(struct scsi_per_res_key)); 8979 lun->per_res[i].registered = 0; 8980 } 8981 lun->PRGeneration++; 8982 break; 8983 } 8984 8985 mtx_unlock(&lun->lun_lock); 8986} 8987 8988int 8989ctl_read_write(struct ctl_scsiio *ctsio) 8990{ 8991 struct ctl_lun *lun; 8992 struct ctl_lba_len_flags *lbalen; 8993 uint64_t lba; 8994 uint32_t num_blocks; 8995 int fua, dpo; 8996 int retval; 8997 int isread; 8998 8999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9000 9001 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9002 9003 fua = 0; 9004 dpo = 0; 9005 9006 retval = CTL_RETVAL_COMPLETE; 9007 9008 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9009 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9010 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9011 uint32_t residx; 9012 9013 /* 9014 * XXX KDM need a lock here. 9015 */ 9016 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9017 if ((lun->res_type == SPR_TYPE_EX_AC 9018 && residx != lun->pr_res_idx) 9019 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9020 || lun->res_type == SPR_TYPE_EX_AC_AR) 9021 && !lun->per_res[residx].registered)) { 9022 ctl_set_reservation_conflict(ctsio); 9023 ctl_done((union ctl_io *)ctsio); 9024 return (CTL_RETVAL_COMPLETE); 9025 } 9026 } 9027 9028 switch (ctsio->cdb[0]) { 9029 case READ_6: 9030 case WRITE_6: { 9031 struct scsi_rw_6 *cdb; 9032 9033 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9034 9035 lba = scsi_3btoul(cdb->addr); 9036 /* only 5 bits are valid in the most significant address byte */ 9037 lba &= 0x1fffff; 9038 num_blocks = cdb->length; 9039 /* 9040 * This is correct according to SBC-2. 9041 */ 9042 if (num_blocks == 0) 9043 num_blocks = 256; 9044 break; 9045 } 9046 case READ_10: 9047 case WRITE_10: { 9048 struct scsi_rw_10 *cdb; 9049 9050 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9051 9052 if (cdb->byte2 & SRW10_FUA) 9053 fua = 1; 9054 if (cdb->byte2 & SRW10_DPO) 9055 dpo = 1; 9056 9057 lba = scsi_4btoul(cdb->addr); 9058 num_blocks = scsi_2btoul(cdb->length); 9059 break; 9060 } 9061 case WRITE_VERIFY_10: { 9062 struct scsi_write_verify_10 *cdb; 9063 9064 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9065 9066 /* 9067 * XXX KDM we should do actual write verify support at some 9068 * point. This is obviously fake, we're just translating 9069 * things to a write. So we don't even bother checking the 9070 * BYTCHK field, since we don't do any verification. If 9071 * the user asks for it, we'll just pretend we did it. 9072 */ 9073 if (cdb->byte2 & SWV_DPO) 9074 dpo = 1; 9075 9076 lba = scsi_4btoul(cdb->addr); 9077 num_blocks = scsi_2btoul(cdb->length); 9078 break; 9079 } 9080 case READ_12: 9081 case WRITE_12: { 9082 struct scsi_rw_12 *cdb; 9083 9084 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9085 9086 if (cdb->byte2 & SRW12_FUA) 9087 fua = 1; 9088 if (cdb->byte2 & SRW12_DPO) 9089 dpo = 1; 9090 lba = scsi_4btoul(cdb->addr); 9091 num_blocks = scsi_4btoul(cdb->length); 9092 break; 9093 } 9094 case WRITE_VERIFY_12: { 9095 struct scsi_write_verify_12 *cdb; 9096 9097 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9098 9099 if (cdb->byte2 & SWV_DPO) 9100 dpo = 1; 9101 9102 lba = scsi_4btoul(cdb->addr); 9103 num_blocks = scsi_4btoul(cdb->length); 9104 9105 break; 9106 } 9107 case READ_16: 9108 case WRITE_16: { 9109 struct scsi_rw_16 *cdb; 9110 9111 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9112 9113 if (cdb->byte2 & SRW12_FUA) 9114 fua = 1; 9115 if (cdb->byte2 & SRW12_DPO) 9116 dpo = 1; 9117 9118 lba = scsi_8btou64(cdb->addr); 9119 num_blocks = scsi_4btoul(cdb->length); 9120 break; 9121 } 9122 case WRITE_VERIFY_16: { 9123 struct scsi_write_verify_16 *cdb; 9124 9125 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9126 9127 if (cdb->byte2 & SWV_DPO) 9128 dpo = 1; 9129 9130 lba = scsi_8btou64(cdb->addr); 9131 num_blocks = scsi_4btoul(cdb->length); 9132 break; 9133 } 9134 default: 9135 /* 9136 * We got a command we don't support. This shouldn't 9137 * happen, commands should be filtered out above us. 9138 */ 9139 ctl_set_invalid_opcode(ctsio); 9140 ctl_done((union ctl_io *)ctsio); 9141 9142 return (CTL_RETVAL_COMPLETE); 9143 break; /* NOTREACHED */ 9144 } 9145 9146 /* 9147 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9148 * interesting for us, but if RAIDCore is in write-back mode, 9149 * getting it to do write-through for a particular transaction may 9150 * not be possible. 9151 */ 9152 9153 /* 9154 * The first check is to make sure we're in bounds, the second 9155 * check is to catch wrap-around problems. If the lba + num blocks 9156 * is less than the lba, then we've wrapped around and the block 9157 * range is invalid anyway. 9158 */ 9159 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9160 || ((lba + num_blocks) < lba)) { 9161 ctl_set_lba_out_of_range(ctsio); 9162 ctl_done((union ctl_io *)ctsio); 9163 return (CTL_RETVAL_COMPLETE); 9164 } 9165 9166 /* 9167 * According to SBC-3, a transfer length of 0 is not an error. 9168 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9169 * translates to 256 blocks for those commands. 9170 */ 9171 if (num_blocks == 0) { 9172 ctl_set_success(ctsio); 9173 ctl_done((union ctl_io *)ctsio); 9174 return (CTL_RETVAL_COMPLETE); 9175 } 9176 9177 lbalen = (struct ctl_lba_len_flags *) 9178 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9179 lbalen->lba = lba; 9180 lbalen->len = num_blocks; 9181 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 9182 9183 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9184 ctsio->kern_rel_offset = 0; 9185 9186 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9187 9188 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9189 9190 return (retval); 9191} 9192 9193static int 9194ctl_cnw_cont(union ctl_io *io) 9195{ 9196 struct ctl_scsiio *ctsio; 9197 struct ctl_lun *lun; 9198 struct ctl_lba_len_flags *lbalen; 9199 int retval; 9200 9201 ctsio = &io->scsiio; 9202 ctsio->io_hdr.status = CTL_STATUS_NONE; 9203 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9204 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9205 lbalen = (struct ctl_lba_len_flags *) 9206 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9207 lbalen->flags = CTL_LLF_WRITE; 9208 9209 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9210 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9211 return (retval); 9212} 9213 9214int 9215ctl_cnw(struct ctl_scsiio *ctsio) 9216{ 9217 struct ctl_lun *lun; 9218 struct ctl_lba_len_flags *lbalen; 9219 uint64_t lba; 9220 uint32_t num_blocks; 9221 int fua, dpo; 9222 int retval; 9223 9224 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9225 9226 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9227 9228 fua = 0; 9229 dpo = 0; 9230 9231 retval = CTL_RETVAL_COMPLETE; 9232 9233 switch (ctsio->cdb[0]) { 9234 case COMPARE_AND_WRITE: { 9235 struct scsi_compare_and_write *cdb; 9236 9237 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9238 9239 if (cdb->byte2 & SRW10_FUA) 9240 fua = 1; 9241 if (cdb->byte2 & SRW10_DPO) 9242 dpo = 1; 9243 lba = scsi_8btou64(cdb->addr); 9244 num_blocks = cdb->length; 9245 break; 9246 } 9247 default: 9248 /* 9249 * We got a command we don't support. This shouldn't 9250 * happen, commands should be filtered out above us. 9251 */ 9252 ctl_set_invalid_opcode(ctsio); 9253 ctl_done((union ctl_io *)ctsio); 9254 9255 return (CTL_RETVAL_COMPLETE); 9256 break; /* NOTREACHED */ 9257 } 9258 9259 /* 9260 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9261 * interesting for us, but if RAIDCore is in write-back mode, 9262 * getting it to do write-through for a particular transaction may 9263 * not be possible. 9264 */ 9265 9266 /* 9267 * The first check is to make sure we're in bounds, the second 9268 * check is to catch wrap-around problems. If the lba + num blocks 9269 * is less than the lba, then we've wrapped around and the block 9270 * range is invalid anyway. 9271 */ 9272 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9273 || ((lba + num_blocks) < lba)) { 9274 ctl_set_lba_out_of_range(ctsio); 9275 ctl_done((union ctl_io *)ctsio); 9276 return (CTL_RETVAL_COMPLETE); 9277 } 9278 9279 /* 9280 * According to SBC-3, a transfer length of 0 is not an error. 9281 */ 9282 if (num_blocks == 0) { 9283 ctl_set_success(ctsio); 9284 ctl_done((union ctl_io *)ctsio); 9285 return (CTL_RETVAL_COMPLETE); 9286 } 9287 9288 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9289 ctsio->kern_rel_offset = 0; 9290 9291 /* 9292 * Set the IO_CONT flag, so that if this I/O gets passed to 9293 * ctl_data_submit_done(), it'll get passed back to 9294 * ctl_ctl_cnw_cont() for further processing. 9295 */ 9296 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9297 ctsio->io_cont = ctl_cnw_cont; 9298 9299 lbalen = (struct ctl_lba_len_flags *) 9300 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9301 lbalen->lba = lba; 9302 lbalen->len = num_blocks; 9303 lbalen->flags = CTL_LLF_COMPARE; 9304 9305 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9306 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9307 return (retval); 9308} 9309 9310int 9311ctl_verify(struct ctl_scsiio *ctsio) 9312{ 9313 struct ctl_lun *lun; 9314 struct ctl_lba_len_flags *lbalen; 9315 uint64_t lba; 9316 uint32_t num_blocks; 9317 int bytchk, dpo; 9318 int retval; 9319 9320 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9321 9322 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9323 9324 bytchk = 0; 9325 dpo = 0; 9326 retval = CTL_RETVAL_COMPLETE; 9327 9328 switch (ctsio->cdb[0]) { 9329 case VERIFY_10: { 9330 struct scsi_verify_10 *cdb; 9331 9332 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9333 if (cdb->byte2 & SVFY_BYTCHK) 9334 bytchk = 1; 9335 if (cdb->byte2 & SVFY_DPO) 9336 dpo = 1; 9337 lba = scsi_4btoul(cdb->addr); 9338 num_blocks = scsi_2btoul(cdb->length); 9339 break; 9340 } 9341 case VERIFY_12: { 9342 struct scsi_verify_12 *cdb; 9343 9344 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9345 if (cdb->byte2 & SVFY_BYTCHK) 9346 bytchk = 1; 9347 if (cdb->byte2 & SVFY_DPO) 9348 dpo = 1; 9349 lba = scsi_4btoul(cdb->addr); 9350 num_blocks = scsi_4btoul(cdb->length); 9351 break; 9352 } 9353 case VERIFY_16: { 9354 struct scsi_rw_16 *cdb; 9355 9356 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9357 if (cdb->byte2 & SVFY_BYTCHK) 9358 bytchk = 1; 9359 if (cdb->byte2 & SVFY_DPO) 9360 dpo = 1; 9361 lba = scsi_8btou64(cdb->addr); 9362 num_blocks = scsi_4btoul(cdb->length); 9363 break; 9364 } 9365 default: 9366 /* 9367 * We got a command we don't support. This shouldn't 9368 * happen, commands should be filtered out above us. 9369 */ 9370 ctl_set_invalid_opcode(ctsio); 9371 ctl_done((union ctl_io *)ctsio); 9372 return (CTL_RETVAL_COMPLETE); 9373 } 9374 9375 /* 9376 * The first check is to make sure we're in bounds, the second 9377 * check is to catch wrap-around problems. If the lba + num blocks 9378 * is less than the lba, then we've wrapped around and the block 9379 * range is invalid anyway. 9380 */ 9381 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9382 || ((lba + num_blocks) < lba)) { 9383 ctl_set_lba_out_of_range(ctsio); 9384 ctl_done((union ctl_io *)ctsio); 9385 return (CTL_RETVAL_COMPLETE); 9386 } 9387 9388 /* 9389 * According to SBC-3, a transfer length of 0 is not an error. 9390 */ 9391 if (num_blocks == 0) { 9392 ctl_set_success(ctsio); 9393 ctl_done((union ctl_io *)ctsio); 9394 return (CTL_RETVAL_COMPLETE); 9395 } 9396 9397 lbalen = (struct ctl_lba_len_flags *) 9398 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9399 lbalen->lba = lba; 9400 lbalen->len = num_blocks; 9401 if (bytchk) { 9402 lbalen->flags = CTL_LLF_COMPARE; 9403 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9404 } else { 9405 lbalen->flags = CTL_LLF_VERIFY; 9406 ctsio->kern_total_len = 0; 9407 } 9408 ctsio->kern_rel_offset = 0; 9409 9410 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9411 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9412 return (retval); 9413} 9414 9415int 9416ctl_report_luns(struct ctl_scsiio *ctsio) 9417{ 9418 struct scsi_report_luns *cdb; 9419 struct scsi_report_luns_data *lun_data; 9420 struct ctl_lun *lun, *request_lun; 9421 int num_luns, retval; 9422 uint32_t alloc_len, lun_datalen; 9423 int num_filled, well_known; 9424 uint32_t initidx, targ_lun_id, lun_id; 9425 9426 retval = CTL_RETVAL_COMPLETE; 9427 well_known = 0; 9428 9429 cdb = (struct scsi_report_luns *)ctsio->cdb; 9430 9431 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9432 9433 mtx_lock(&control_softc->ctl_lock); 9434 num_luns = control_softc->num_luns; 9435 mtx_unlock(&control_softc->ctl_lock); 9436 9437 switch (cdb->select_report) { 9438 case RPL_REPORT_DEFAULT: 9439 case RPL_REPORT_ALL: 9440 break; 9441 case RPL_REPORT_WELLKNOWN: 9442 well_known = 1; 9443 num_luns = 0; 9444 break; 9445 default: 9446 ctl_set_invalid_field(ctsio, 9447 /*sks_valid*/ 1, 9448 /*command*/ 1, 9449 /*field*/ 2, 9450 /*bit_valid*/ 0, 9451 /*bit*/ 0); 9452 ctl_done((union ctl_io *)ctsio); 9453 return (retval); 9454 break; /* NOTREACHED */ 9455 } 9456 9457 alloc_len = scsi_4btoul(cdb->length); 9458 /* 9459 * The initiator has to allocate at least 16 bytes for this request, 9460 * so he can at least get the header and the first LUN. Otherwise 9461 * we reject the request (per SPC-3 rev 14, section 6.21). 9462 */ 9463 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9464 sizeof(struct scsi_report_luns_lundata))) { 9465 ctl_set_invalid_field(ctsio, 9466 /*sks_valid*/ 1, 9467 /*command*/ 1, 9468 /*field*/ 6, 9469 /*bit_valid*/ 0, 9470 /*bit*/ 0); 9471 ctl_done((union ctl_io *)ctsio); 9472 return (retval); 9473 } 9474 9475 request_lun = (struct ctl_lun *) 9476 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9477 9478 lun_datalen = sizeof(*lun_data) + 9479 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9480 9481 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9482 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9483 ctsio->kern_sg_entries = 0; 9484 9485 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9486 9487 mtx_lock(&control_softc->ctl_lock); 9488 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9489 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9490 if (lun_id >= CTL_MAX_LUNS) 9491 continue; 9492 lun = control_softc->ctl_luns[lun_id]; 9493 if (lun == NULL) 9494 continue; 9495 9496 if (targ_lun_id <= 0xff) { 9497 /* 9498 * Peripheral addressing method, bus number 0. 9499 */ 9500 lun_data->luns[num_filled].lundata[0] = 9501 RPL_LUNDATA_ATYP_PERIPH; 9502 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9503 num_filled++; 9504 } else if (targ_lun_id <= 0x3fff) { 9505 /* 9506 * Flat addressing method. 9507 */ 9508 lun_data->luns[num_filled].lundata[0] = 9509 RPL_LUNDATA_ATYP_FLAT | 9510 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9511#ifdef OLDCTLHEADERS 9512 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9513 (targ_lun_id & SRLD_BUS_LUN_MASK); 9514#endif 9515 lun_data->luns[num_filled].lundata[1] = 9516#ifdef OLDCTLHEADERS 9517 targ_lun_id >> SRLD_BUS_LUN_BITS; 9518#endif 9519 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9520 num_filled++; 9521 } else { 9522 printf("ctl_report_luns: bogus LUN number %jd, " 9523 "skipping\n", (intmax_t)targ_lun_id); 9524 } 9525 /* 9526 * According to SPC-3, rev 14 section 6.21: 9527 * 9528 * "The execution of a REPORT LUNS command to any valid and 9529 * installed logical unit shall clear the REPORTED LUNS DATA 9530 * HAS CHANGED unit attention condition for all logical 9531 * units of that target with respect to the requesting 9532 * initiator. A valid and installed logical unit is one 9533 * having a PERIPHERAL QUALIFIER of 000b in the standard 9534 * INQUIRY data (see 6.4.2)." 9535 * 9536 * If request_lun is NULL, the LUN this report luns command 9537 * was issued to is either disabled or doesn't exist. In that 9538 * case, we shouldn't clear any pending lun change unit 9539 * attention. 9540 */ 9541 if (request_lun != NULL) { 9542 mtx_lock(&lun->lun_lock); 9543 lun->pending_sense[initidx].ua_pending &= 9544 ~CTL_UA_LUN_CHANGE; 9545 mtx_unlock(&lun->lun_lock); 9546 } 9547 } 9548 mtx_unlock(&control_softc->ctl_lock); 9549 9550 /* 9551 * It's quite possible that we've returned fewer LUNs than we allocated 9552 * space for. Trim it. 9553 */ 9554 lun_datalen = sizeof(*lun_data) + 9555 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9556 9557 if (lun_datalen < alloc_len) { 9558 ctsio->residual = alloc_len - lun_datalen; 9559 ctsio->kern_data_len = lun_datalen; 9560 ctsio->kern_total_len = lun_datalen; 9561 } else { 9562 ctsio->residual = 0; 9563 ctsio->kern_data_len = alloc_len; 9564 ctsio->kern_total_len = alloc_len; 9565 } 9566 ctsio->kern_data_resid = 0; 9567 ctsio->kern_rel_offset = 0; 9568 ctsio->kern_sg_entries = 0; 9569 9570 /* 9571 * We set this to the actual data length, regardless of how much 9572 * space we actually have to return results. If the user looks at 9573 * this value, he'll know whether or not he allocated enough space 9574 * and reissue the command if necessary. We don't support well 9575 * known logical units, so if the user asks for that, return none. 9576 */ 9577 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9578 9579 /* 9580 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9581 * this request. 9582 */ 9583 ctsio->scsi_status = SCSI_STATUS_OK; 9584 9585 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9586 ctsio->be_move_done = ctl_config_move_done; 9587 ctl_datamove((union ctl_io *)ctsio); 9588 9589 return (retval); 9590} 9591 9592int 9593ctl_request_sense(struct ctl_scsiio *ctsio) 9594{ 9595 struct scsi_request_sense *cdb; 9596 struct scsi_sense_data *sense_ptr; 9597 struct ctl_lun *lun; 9598 uint32_t initidx; 9599 int have_error; 9600 scsi_sense_data_type sense_format; 9601 9602 cdb = (struct scsi_request_sense *)ctsio->cdb; 9603 9604 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9605 9606 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9607 9608 /* 9609 * Determine which sense format the user wants. 9610 */ 9611 if (cdb->byte2 & SRS_DESC) 9612 sense_format = SSD_TYPE_DESC; 9613 else 9614 sense_format = SSD_TYPE_FIXED; 9615 9616 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9617 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9618 ctsio->kern_sg_entries = 0; 9619 9620 /* 9621 * struct scsi_sense_data, which is currently set to 256 bytes, is 9622 * larger than the largest allowed value for the length field in the 9623 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9624 */ 9625 ctsio->residual = 0; 9626 ctsio->kern_data_len = cdb->length; 9627 ctsio->kern_total_len = cdb->length; 9628 9629 ctsio->kern_data_resid = 0; 9630 ctsio->kern_rel_offset = 0; 9631 ctsio->kern_sg_entries = 0; 9632 9633 /* 9634 * If we don't have a LUN, we don't have any pending sense. 9635 */ 9636 if (lun == NULL) 9637 goto no_sense; 9638 9639 have_error = 0; 9640 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9641 /* 9642 * Check for pending sense, and then for pending unit attentions. 9643 * Pending sense gets returned first, then pending unit attentions. 9644 */ 9645 mtx_lock(&lun->lun_lock); 9646 if (ctl_is_set(lun->have_ca, initidx)) { 9647 scsi_sense_data_type stored_format; 9648 9649 /* 9650 * Check to see which sense format was used for the stored 9651 * sense data. 9652 */ 9653 stored_format = scsi_sense_type( 9654 &lun->pending_sense[initidx].sense); 9655 9656 /* 9657 * If the user requested a different sense format than the 9658 * one we stored, then we need to convert it to the other 9659 * format. If we're going from descriptor to fixed format 9660 * sense data, we may lose things in translation, depending 9661 * on what options were used. 9662 * 9663 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9664 * for some reason we'll just copy it out as-is. 9665 */ 9666 if ((stored_format == SSD_TYPE_FIXED) 9667 && (sense_format == SSD_TYPE_DESC)) 9668 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9669 &lun->pending_sense[initidx].sense, 9670 (struct scsi_sense_data_desc *)sense_ptr); 9671 else if ((stored_format == SSD_TYPE_DESC) 9672 && (sense_format == SSD_TYPE_FIXED)) 9673 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9674 &lun->pending_sense[initidx].sense, 9675 (struct scsi_sense_data_fixed *)sense_ptr); 9676 else 9677 memcpy(sense_ptr, &lun->pending_sense[initidx].sense, 9678 ctl_min(sizeof(*sense_ptr), 9679 sizeof(lun->pending_sense[initidx].sense))); 9680 9681 ctl_clear_mask(lun->have_ca, initidx); 9682 have_error = 1; 9683 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) { 9684 ctl_ua_type ua_type; 9685 9686 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending, 9687 sense_ptr, sense_format); 9688 if (ua_type != CTL_UA_NONE) { 9689 have_error = 1; 9690 /* We're reporting this UA, so clear it */ 9691 lun->pending_sense[initidx].ua_pending &= ~ua_type; 9692 } 9693 } 9694 mtx_unlock(&lun->lun_lock); 9695 9696 /* 9697 * We already have a pending error, return it. 9698 */ 9699 if (have_error != 0) { 9700 /* 9701 * We report the SCSI status as OK, since the status of the 9702 * request sense command itself is OK. 9703 */ 9704 ctsio->scsi_status = SCSI_STATUS_OK; 9705 9706 /* 9707 * We report 0 for the sense length, because we aren't doing 9708 * autosense in this case. We're reporting sense as 9709 * parameter data. 9710 */ 9711 ctsio->sense_len = 0; 9712 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9713 ctsio->be_move_done = ctl_config_move_done; 9714 ctl_datamove((union ctl_io *)ctsio); 9715 9716 return (CTL_RETVAL_COMPLETE); 9717 } 9718 9719no_sense: 9720 9721 /* 9722 * No sense information to report, so we report that everything is 9723 * okay. 9724 */ 9725 ctl_set_sense_data(sense_ptr, 9726 lun, 9727 sense_format, 9728 /*current_error*/ 1, 9729 /*sense_key*/ SSD_KEY_NO_SENSE, 9730 /*asc*/ 0x00, 9731 /*ascq*/ 0x00, 9732 SSD_ELEM_NONE); 9733 9734 ctsio->scsi_status = SCSI_STATUS_OK; 9735 9736 /* 9737 * We report 0 for the sense length, because we aren't doing 9738 * autosense in this case. We're reporting sense as parameter data. 9739 */ 9740 ctsio->sense_len = 0; 9741 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9742 ctsio->be_move_done = ctl_config_move_done; 9743 ctl_datamove((union ctl_io *)ctsio); 9744 9745 return (CTL_RETVAL_COMPLETE); 9746} 9747 9748int 9749ctl_tur(struct ctl_scsiio *ctsio) 9750{ 9751 struct ctl_lun *lun; 9752 9753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9754 9755 CTL_DEBUG_PRINT(("ctl_tur\n")); 9756 9757 if (lun == NULL) 9758 return (EINVAL); 9759 9760 ctsio->scsi_status = SCSI_STATUS_OK; 9761 ctsio->io_hdr.status = CTL_SUCCESS; 9762 9763 ctl_done((union ctl_io *)ctsio); 9764 9765 return (CTL_RETVAL_COMPLETE); 9766} 9767 9768#ifdef notyet 9769static int 9770ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9771{ 9772 9773} 9774#endif 9775 9776static int 9777ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9778{ 9779 struct scsi_vpd_supported_pages *pages; 9780 int sup_page_size; 9781 struct ctl_lun *lun; 9782 9783 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9784 9785 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9786 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9787 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9788 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9789 ctsio->kern_sg_entries = 0; 9790 9791 if (sup_page_size < alloc_len) { 9792 ctsio->residual = alloc_len - sup_page_size; 9793 ctsio->kern_data_len = sup_page_size; 9794 ctsio->kern_total_len = sup_page_size; 9795 } else { 9796 ctsio->residual = 0; 9797 ctsio->kern_data_len = alloc_len; 9798 ctsio->kern_total_len = alloc_len; 9799 } 9800 ctsio->kern_data_resid = 0; 9801 ctsio->kern_rel_offset = 0; 9802 ctsio->kern_sg_entries = 0; 9803 9804 /* 9805 * The control device is always connected. The disk device, on the 9806 * other hand, may not be online all the time. Need to change this 9807 * to figure out whether the disk device is actually online or not. 9808 */ 9809 if (lun != NULL) 9810 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9811 lun->be_lun->lun_type; 9812 else 9813 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9814 9815 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9816 /* Supported VPD pages */ 9817 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9818 /* Serial Number */ 9819 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9820 /* Device Identification */ 9821 pages->page_list[2] = SVPD_DEVICE_ID; 9822 /* SCSI Ports */ 9823 pages->page_list[3] = SVPD_SCSI_PORTS; 9824 /* Third-party Copy */ 9825 pages->page_list[4] = SVPD_SCSI_TPC; 9826 /* Block limits */ 9827 pages->page_list[5] = SVPD_BLOCK_LIMITS; 9828 /* Logical Block Provisioning */ 9829 pages->page_list[6] = SVPD_LBP; 9830 9831 ctsio->scsi_status = SCSI_STATUS_OK; 9832 9833 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9834 ctsio->be_move_done = ctl_config_move_done; 9835 ctl_datamove((union ctl_io *)ctsio); 9836 9837 return (CTL_RETVAL_COMPLETE); 9838} 9839 9840static int 9841ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9842{ 9843 struct scsi_vpd_unit_serial_number *sn_ptr; 9844 struct ctl_lun *lun; 9845 9846 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9847 9848 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9849 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9850 ctsio->kern_sg_entries = 0; 9851 9852 if (sizeof(*sn_ptr) < alloc_len) { 9853 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9854 ctsio->kern_data_len = sizeof(*sn_ptr); 9855 ctsio->kern_total_len = sizeof(*sn_ptr); 9856 } else { 9857 ctsio->residual = 0; 9858 ctsio->kern_data_len = alloc_len; 9859 ctsio->kern_total_len = alloc_len; 9860 } 9861 ctsio->kern_data_resid = 0; 9862 ctsio->kern_rel_offset = 0; 9863 ctsio->kern_sg_entries = 0; 9864 9865 /* 9866 * The control device is always connected. The disk device, on the 9867 * other hand, may not be online all the time. Need to change this 9868 * to figure out whether the disk device is actually online or not. 9869 */ 9870 if (lun != NULL) 9871 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9872 lun->be_lun->lun_type; 9873 else 9874 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9875 9876 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9877 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9878 /* 9879 * If we don't have a LUN, we just leave the serial number as 9880 * all spaces. 9881 */ 9882 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9883 if (lun != NULL) { 9884 strncpy((char *)sn_ptr->serial_num, 9885 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9886 } 9887 ctsio->scsi_status = SCSI_STATUS_OK; 9888 9889 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9890 ctsio->be_move_done = ctl_config_move_done; 9891 ctl_datamove((union ctl_io *)ctsio); 9892 9893 return (CTL_RETVAL_COMPLETE); 9894} 9895 9896 9897static int 9898ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9899{ 9900 struct scsi_vpd_device_id *devid_ptr; 9901 struct scsi_vpd_id_descriptor *desc; 9902 struct ctl_softc *ctl_softc; 9903 struct ctl_lun *lun; 9904 struct ctl_port *port; 9905 int data_len; 9906 uint8_t proto; 9907 9908 ctl_softc = control_softc; 9909 9910 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9911 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9912 9913 data_len = sizeof(struct scsi_vpd_device_id) + 9914 sizeof(struct scsi_vpd_id_descriptor) + 9915 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9916 sizeof(struct scsi_vpd_id_descriptor) + 9917 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9918 if (lun && lun->lun_devid) 9919 data_len += lun->lun_devid->len; 9920 if (port->port_devid) 9921 data_len += port->port_devid->len; 9922 if (port->target_devid) 9923 data_len += port->target_devid->len; 9924 9925 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9926 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9927 ctsio->kern_sg_entries = 0; 9928 9929 if (data_len < alloc_len) { 9930 ctsio->residual = alloc_len - data_len; 9931 ctsio->kern_data_len = data_len; 9932 ctsio->kern_total_len = data_len; 9933 } else { 9934 ctsio->residual = 0; 9935 ctsio->kern_data_len = alloc_len; 9936 ctsio->kern_total_len = alloc_len; 9937 } 9938 ctsio->kern_data_resid = 0; 9939 ctsio->kern_rel_offset = 0; 9940 ctsio->kern_sg_entries = 0; 9941 9942 /* 9943 * The control device is always connected. The disk device, on the 9944 * other hand, may not be online all the time. 9945 */ 9946 if (lun != NULL) 9947 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9948 lun->be_lun->lun_type; 9949 else 9950 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9951 devid_ptr->page_code = SVPD_DEVICE_ID; 9952 scsi_ulto2b(data_len - 4, devid_ptr->length); 9953 9954 if (port->port_type == CTL_PORT_FC) 9955 proto = SCSI_PROTO_FC << 4; 9956 else if (port->port_type == CTL_PORT_ISCSI) 9957 proto = SCSI_PROTO_ISCSI << 4; 9958 else 9959 proto = SCSI_PROTO_SPI << 4; 9960 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9961 9962 /* 9963 * We're using a LUN association here. i.e., this device ID is a 9964 * per-LUN identifier. 9965 */ 9966 if (lun && lun->lun_devid) { 9967 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9968 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9969 lun->lun_devid->len); 9970 } 9971 9972 /* 9973 * This is for the WWPN which is a port association. 9974 */ 9975 if (port->port_devid) { 9976 memcpy(desc, port->port_devid->data, port->port_devid->len); 9977 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9978 port->port_devid->len); 9979 } 9980 9981 /* 9982 * This is for the Relative Target Port(type 4h) identifier 9983 */ 9984 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9985 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9986 SVPD_ID_TYPE_RELTARG; 9987 desc->length = 4; 9988 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9989 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9990 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9991 9992 /* 9993 * This is for the Target Port Group(type 5h) identifier 9994 */ 9995 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9996 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9997 SVPD_ID_TYPE_TPORTGRP; 9998 desc->length = 4; 9999 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10000 &desc->identifier[2]); 10001 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10002 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10003 10004 /* 10005 * This is for the Target identifier 10006 */ 10007 if (port->target_devid) { 10008 memcpy(desc, port->target_devid->data, port->target_devid->len); 10009 } 10010 10011 ctsio->scsi_status = SCSI_STATUS_OK; 10012 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10013 ctsio->be_move_done = ctl_config_move_done; 10014 ctl_datamove((union ctl_io *)ctsio); 10015 10016 return (CTL_RETVAL_COMPLETE); 10017} 10018 10019static int 10020ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10021{ 10022 struct ctl_softc *softc = control_softc; 10023 struct scsi_vpd_scsi_ports *sp; 10024 struct scsi_vpd_port_designation *pd; 10025 struct scsi_vpd_port_designation_cont *pdc; 10026 struct ctl_lun *lun; 10027 struct ctl_port *port; 10028 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10029 int num_target_port_groups, single; 10030 10031 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10032 10033 single = ctl_is_single; 10034 if (single) 10035 num_target_port_groups = 1; 10036 else 10037 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10038 num_target_ports = 0; 10039 iid_len = 0; 10040 id_len = 0; 10041 mtx_lock(&softc->ctl_lock); 10042 STAILQ_FOREACH(port, &softc->port_list, links) { 10043 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10044 continue; 10045 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 10046 CTL_MAX_LUNS) 10047 continue; 10048 num_target_ports++; 10049 if (port->init_devid) 10050 iid_len += port->init_devid->len; 10051 if (port->port_devid) 10052 id_len += port->port_devid->len; 10053 } 10054 mtx_unlock(&softc->ctl_lock); 10055 10056 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10057 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10058 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10059 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10060 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10061 ctsio->kern_sg_entries = 0; 10062 10063 if (data_len < alloc_len) { 10064 ctsio->residual = alloc_len - data_len; 10065 ctsio->kern_data_len = data_len; 10066 ctsio->kern_total_len = data_len; 10067 } else { 10068 ctsio->residual = 0; 10069 ctsio->kern_data_len = alloc_len; 10070 ctsio->kern_total_len = alloc_len; 10071 } 10072 ctsio->kern_data_resid = 0; 10073 ctsio->kern_rel_offset = 0; 10074 ctsio->kern_sg_entries = 0; 10075 10076 /* 10077 * The control device is always connected. The disk device, on the 10078 * other hand, may not be online all the time. Need to change this 10079 * to figure out whether the disk device is actually online or not. 10080 */ 10081 if (lun != NULL) 10082 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10083 lun->be_lun->lun_type; 10084 else 10085 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10086 10087 sp->page_code = SVPD_SCSI_PORTS; 10088 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10089 sp->page_length); 10090 pd = &sp->design[0]; 10091 10092 mtx_lock(&softc->ctl_lock); 10093 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10094 pg = 0; 10095 else 10096 pg = 1; 10097 for (g = 0; g < num_target_port_groups; g++) { 10098 STAILQ_FOREACH(port, &softc->port_list, links) { 10099 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10100 continue; 10101 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 10102 CTL_MAX_LUNS) 10103 continue; 10104 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10105 scsi_ulto2b(p, pd->relative_port_id); 10106 if (port->init_devid && g == pg) { 10107 iid_len = port->init_devid->len; 10108 memcpy(pd->initiator_transportid, 10109 port->init_devid->data, port->init_devid->len); 10110 } else 10111 iid_len = 0; 10112 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10113 pdc = (struct scsi_vpd_port_designation_cont *) 10114 (&pd->initiator_transportid[iid_len]); 10115 if (port->port_devid && g == pg) { 10116 id_len = port->port_devid->len; 10117 memcpy(pdc->target_port_descriptors, 10118 port->port_devid->data, port->port_devid->len); 10119 } else 10120 id_len = 0; 10121 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10122 pd = (struct scsi_vpd_port_designation *) 10123 ((uint8_t *)pdc->target_port_descriptors + id_len); 10124 } 10125 } 10126 mtx_unlock(&softc->ctl_lock); 10127 10128 ctsio->scsi_status = SCSI_STATUS_OK; 10129 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10130 ctsio->be_move_done = ctl_config_move_done; 10131 ctl_datamove((union ctl_io *)ctsio); 10132 10133 return (CTL_RETVAL_COMPLETE); 10134} 10135 10136static int 10137ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10138{ 10139 struct scsi_vpd_block_limits *bl_ptr; 10140 struct ctl_lun *lun; 10141 int bs; 10142 10143 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10144 bs = lun->be_lun->blocksize; 10145 10146 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10147 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10148 ctsio->kern_sg_entries = 0; 10149 10150 if (sizeof(*bl_ptr) < alloc_len) { 10151 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10152 ctsio->kern_data_len = sizeof(*bl_ptr); 10153 ctsio->kern_total_len = sizeof(*bl_ptr); 10154 } else { 10155 ctsio->residual = 0; 10156 ctsio->kern_data_len = alloc_len; 10157 ctsio->kern_total_len = alloc_len; 10158 } 10159 ctsio->kern_data_resid = 0; 10160 ctsio->kern_rel_offset = 0; 10161 ctsio->kern_sg_entries = 0; 10162 10163 /* 10164 * The control device is always connected. The disk device, on the 10165 * other hand, may not be online all the time. Need to change this 10166 * to figure out whether the disk device is actually online or not. 10167 */ 10168 if (lun != NULL) 10169 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10170 lun->be_lun->lun_type; 10171 else 10172 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10173 10174 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10175 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10176 bl_ptr->max_cmp_write_len = 0xff; 10177 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10178 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10179 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10180 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10181 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10182 } 10183 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10184 10185 ctsio->scsi_status = SCSI_STATUS_OK; 10186 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10187 ctsio->be_move_done = ctl_config_move_done; 10188 ctl_datamove((union ctl_io *)ctsio); 10189 10190 return (CTL_RETVAL_COMPLETE); 10191} 10192 10193static int 10194ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10195{ 10196 struct scsi_vpd_logical_block_prov *lbp_ptr; 10197 struct ctl_lun *lun; 10198 int bs; 10199 10200 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10201 bs = lun->be_lun->blocksize; 10202 10203 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10204 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10205 ctsio->kern_sg_entries = 0; 10206 10207 if (sizeof(*lbp_ptr) < alloc_len) { 10208 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10209 ctsio->kern_data_len = sizeof(*lbp_ptr); 10210 ctsio->kern_total_len = sizeof(*lbp_ptr); 10211 } else { 10212 ctsio->residual = 0; 10213 ctsio->kern_data_len = alloc_len; 10214 ctsio->kern_total_len = alloc_len; 10215 } 10216 ctsio->kern_data_resid = 0; 10217 ctsio->kern_rel_offset = 0; 10218 ctsio->kern_sg_entries = 0; 10219 10220 /* 10221 * The control device is always connected. The disk device, on the 10222 * other hand, may not be online all the time. Need to change this 10223 * to figure out whether the disk device is actually online or not. 10224 */ 10225 if (lun != NULL) 10226 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10227 lun->be_lun->lun_type; 10228 else 10229 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10230 10231 lbp_ptr->page_code = SVPD_LBP; 10232 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 10233 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10; 10234 10235 ctsio->scsi_status = SCSI_STATUS_OK; 10236 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10237 ctsio->be_move_done = ctl_config_move_done; 10238 ctl_datamove((union ctl_io *)ctsio); 10239 10240 return (CTL_RETVAL_COMPLETE); 10241} 10242 10243static int 10244ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10245{ 10246 struct scsi_inquiry *cdb; 10247 struct ctl_lun *lun; 10248 int alloc_len, retval; 10249 10250 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10251 cdb = (struct scsi_inquiry *)ctsio->cdb; 10252 10253 retval = CTL_RETVAL_COMPLETE; 10254 10255 alloc_len = scsi_2btoul(cdb->length); 10256 10257 switch (cdb->page_code) { 10258 case SVPD_SUPPORTED_PAGES: 10259 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10260 break; 10261 case SVPD_UNIT_SERIAL_NUMBER: 10262 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10263 break; 10264 case SVPD_DEVICE_ID: 10265 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10266 break; 10267 case SVPD_SCSI_PORTS: 10268 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10269 break; 10270 case SVPD_SCSI_TPC: 10271 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10272 break; 10273 case SVPD_BLOCK_LIMITS: 10274 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10275 break; 10276 case SVPD_LBP: 10277 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10278 break; 10279 default: 10280 ctl_set_invalid_field(ctsio, 10281 /*sks_valid*/ 1, 10282 /*command*/ 1, 10283 /*field*/ 2, 10284 /*bit_valid*/ 0, 10285 /*bit*/ 0); 10286 ctl_done((union ctl_io *)ctsio); 10287 retval = CTL_RETVAL_COMPLETE; 10288 break; 10289 } 10290 10291 return (retval); 10292} 10293 10294static int 10295ctl_inquiry_std(struct ctl_scsiio *ctsio) 10296{ 10297 struct scsi_inquiry_data *inq_ptr; 10298 struct scsi_inquiry *cdb; 10299 struct ctl_softc *ctl_softc; 10300 struct ctl_lun *lun; 10301 char *val; 10302 uint32_t alloc_len; 10303 ctl_port_type port_type; 10304 10305 ctl_softc = control_softc; 10306 10307 /* 10308 * Figure out whether we're talking to a Fibre Channel port or not. 10309 * We treat the ioctl front end, and any SCSI adapters, as packetized 10310 * SCSI front ends. 10311 */ 10312 port_type = ctl_softc->ctl_ports[ 10313 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10314 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10315 port_type = CTL_PORT_SCSI; 10316 10317 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10318 cdb = (struct scsi_inquiry *)ctsio->cdb; 10319 alloc_len = scsi_2btoul(cdb->length); 10320 10321 /* 10322 * We malloc the full inquiry data size here and fill it 10323 * in. If the user only asks for less, we'll give him 10324 * that much. 10325 */ 10326 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10327 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10328 ctsio->kern_sg_entries = 0; 10329 ctsio->kern_data_resid = 0; 10330 ctsio->kern_rel_offset = 0; 10331 10332 if (sizeof(*inq_ptr) < alloc_len) { 10333 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10334 ctsio->kern_data_len = sizeof(*inq_ptr); 10335 ctsio->kern_total_len = sizeof(*inq_ptr); 10336 } else { 10337 ctsio->residual = 0; 10338 ctsio->kern_data_len = alloc_len; 10339 ctsio->kern_total_len = alloc_len; 10340 } 10341 10342 /* 10343 * If we have a LUN configured, report it as connected. Otherwise, 10344 * report that it is offline or no device is supported, depending 10345 * on the value of inquiry_pq_no_lun. 10346 * 10347 * According to the spec (SPC-4 r34), the peripheral qualifier 10348 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10349 * 10350 * "A peripheral device having the specified peripheral device type 10351 * is not connected to this logical unit. However, the device 10352 * server is capable of supporting the specified peripheral device 10353 * type on this logical unit." 10354 * 10355 * According to the same spec, the peripheral qualifier 10356 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10357 * 10358 * "The device server is not capable of supporting a peripheral 10359 * device on this logical unit. For this peripheral qualifier the 10360 * peripheral device type shall be set to 1Fh. All other peripheral 10361 * device type values are reserved for this peripheral qualifier." 10362 * 10363 * Given the text, it would seem that we probably want to report that 10364 * the LUN is offline here. There is no LUN connected, but we can 10365 * support a LUN at the given LUN number. 10366 * 10367 * In the real world, though, it sounds like things are a little 10368 * different: 10369 * 10370 * - Linux, when presented with a LUN with the offline peripheral 10371 * qualifier, will create an sg driver instance for it. So when 10372 * you attach it to CTL, you wind up with a ton of sg driver 10373 * instances. (One for every LUN that Linux bothered to probe.) 10374 * Linux does this despite the fact that it issues a REPORT LUNs 10375 * to LUN 0 to get the inventory of supported LUNs. 10376 * 10377 * - There is other anecdotal evidence (from Emulex folks) about 10378 * arrays that use the offline peripheral qualifier for LUNs that 10379 * are on the "passive" path in an active/passive array. 10380 * 10381 * So the solution is provide a hopefully reasonable default 10382 * (return bad/no LUN) and allow the user to change the behavior 10383 * with a tunable/sysctl variable. 10384 */ 10385 if (lun != NULL) 10386 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10387 lun->be_lun->lun_type; 10388 else if (ctl_softc->inquiry_pq_no_lun == 0) 10389 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10390 else 10391 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10392 10393 /* RMB in byte 2 is 0 */ 10394 inq_ptr->version = SCSI_REV_SPC4; 10395 10396 /* 10397 * According to SAM-3, even if a device only supports a single 10398 * level of LUN addressing, it should still set the HISUP bit: 10399 * 10400 * 4.9.1 Logical unit numbers overview 10401 * 10402 * All logical unit number formats described in this standard are 10403 * hierarchical in structure even when only a single level in that 10404 * hierarchy is used. The HISUP bit shall be set to one in the 10405 * standard INQUIRY data (see SPC-2) when any logical unit number 10406 * format described in this standard is used. Non-hierarchical 10407 * formats are outside the scope of this standard. 10408 * 10409 * Therefore we set the HiSup bit here. 10410 * 10411 * The reponse format is 2, per SPC-3. 10412 */ 10413 inq_ptr->response_format = SID_HiSup | 2; 10414 10415 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 10416 CTL_DEBUG_PRINT(("additional_length = %d\n", 10417 inq_ptr->additional_length)); 10418 10419 inq_ptr->spc3_flags = SPC3_SID_3PC; 10420 if (!ctl_is_single) 10421 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT; 10422 /* 16 bit addressing */ 10423 if (port_type == CTL_PORT_SCSI) 10424 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10425 /* XXX set the SID_MultiP bit here if we're actually going to 10426 respond on multiple ports */ 10427 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10428 10429 /* 16 bit data bus, synchronous transfers */ 10430 if (port_type == CTL_PORT_SCSI) 10431 inq_ptr->flags = SID_WBus16 | SID_Sync; 10432 /* 10433 * XXX KDM do we want to support tagged queueing on the control 10434 * device at all? 10435 */ 10436 if ((lun == NULL) 10437 || (lun->be_lun->lun_type != T_PROCESSOR)) 10438 inq_ptr->flags |= SID_CmdQue; 10439 /* 10440 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10441 * We have 8 bytes for the vendor name, and 16 bytes for the device 10442 * name and 4 bytes for the revision. 10443 */ 10444 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10445 "vendor")) == NULL) { 10446 strcpy(inq_ptr->vendor, CTL_VENDOR); 10447 } else { 10448 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10449 strncpy(inq_ptr->vendor, val, 10450 min(sizeof(inq_ptr->vendor), strlen(val))); 10451 } 10452 if (lun == NULL) { 10453 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10454 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10455 switch (lun->be_lun->lun_type) { 10456 case T_DIRECT: 10457 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10458 break; 10459 case T_PROCESSOR: 10460 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10461 break; 10462 default: 10463 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10464 break; 10465 } 10466 } else { 10467 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10468 strncpy(inq_ptr->product, val, 10469 min(sizeof(inq_ptr->product), strlen(val))); 10470 } 10471 10472 /* 10473 * XXX make this a macro somewhere so it automatically gets 10474 * incremented when we make changes. 10475 */ 10476 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10477 "revision")) == NULL) { 10478 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10479 } else { 10480 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10481 strncpy(inq_ptr->revision, val, 10482 min(sizeof(inq_ptr->revision), strlen(val))); 10483 } 10484 10485 /* 10486 * For parallel SCSI, we support double transition and single 10487 * transition clocking. We also support QAS (Quick Arbitration 10488 * and Selection) and Information Unit transfers on both the 10489 * control and array devices. 10490 */ 10491 if (port_type == CTL_PORT_SCSI) 10492 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10493 SID_SPI_IUS; 10494 10495 /* SAM-5 (no version claimed) */ 10496 scsi_ulto2b(0x00A0, inq_ptr->version1); 10497 /* SPC-4 (no version claimed) */ 10498 scsi_ulto2b(0x0460, inq_ptr->version2); 10499 if (port_type == CTL_PORT_FC) { 10500 /* FCP-2 ANSI INCITS.350:2003 */ 10501 scsi_ulto2b(0x0917, inq_ptr->version3); 10502 } else if (port_type == CTL_PORT_SCSI) { 10503 /* SPI-4 ANSI INCITS.362:200x */ 10504 scsi_ulto2b(0x0B56, inq_ptr->version3); 10505 } else if (port_type == CTL_PORT_ISCSI) { 10506 /* iSCSI (no version claimed) */ 10507 scsi_ulto2b(0x0960, inq_ptr->version3); 10508 } else if (port_type == CTL_PORT_SAS) { 10509 /* SAS (no version claimed) */ 10510 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10511 } 10512 10513 if (lun == NULL) { 10514 /* SBC-3 (no version claimed) */ 10515 scsi_ulto2b(0x04C0, inq_ptr->version4); 10516 } else { 10517 switch (lun->be_lun->lun_type) { 10518 case T_DIRECT: 10519 /* SBC-3 (no version claimed) */ 10520 scsi_ulto2b(0x04C0, inq_ptr->version4); 10521 break; 10522 case T_PROCESSOR: 10523 default: 10524 break; 10525 } 10526 } 10527 10528 ctsio->scsi_status = SCSI_STATUS_OK; 10529 if (ctsio->kern_data_len > 0) { 10530 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10531 ctsio->be_move_done = ctl_config_move_done; 10532 ctl_datamove((union ctl_io *)ctsio); 10533 } else { 10534 ctsio->io_hdr.status = CTL_SUCCESS; 10535 ctl_done((union ctl_io *)ctsio); 10536 } 10537 10538 return (CTL_RETVAL_COMPLETE); 10539} 10540 10541int 10542ctl_inquiry(struct ctl_scsiio *ctsio) 10543{ 10544 struct scsi_inquiry *cdb; 10545 int retval; 10546 10547 cdb = (struct scsi_inquiry *)ctsio->cdb; 10548 10549 retval = 0; 10550 10551 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10552 10553 /* 10554 * Right now, we don't support the CmdDt inquiry information. 10555 * This would be nice to support in the future. When we do 10556 * support it, we should change this test so that it checks to make 10557 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10558 */ 10559#ifdef notyet 10560 if (((cdb->byte2 & SI_EVPD) 10561 && (cdb->byte2 & SI_CMDDT))) 10562#endif 10563 if (cdb->byte2 & SI_CMDDT) { 10564 /* 10565 * Point to the SI_CMDDT bit. We might change this 10566 * when we support SI_CMDDT, but since both bits would be 10567 * "wrong", this should probably just stay as-is then. 10568 */ 10569 ctl_set_invalid_field(ctsio, 10570 /*sks_valid*/ 1, 10571 /*command*/ 1, 10572 /*field*/ 1, 10573 /*bit_valid*/ 1, 10574 /*bit*/ 1); 10575 ctl_done((union ctl_io *)ctsio); 10576 return (CTL_RETVAL_COMPLETE); 10577 } 10578 if (cdb->byte2 & SI_EVPD) 10579 retval = ctl_inquiry_evpd(ctsio); 10580#ifdef notyet 10581 else if (cdb->byte2 & SI_CMDDT) 10582 retval = ctl_inquiry_cmddt(ctsio); 10583#endif 10584 else 10585 retval = ctl_inquiry_std(ctsio); 10586 10587 return (retval); 10588} 10589 10590/* 10591 * For known CDB types, parse the LBA and length. 10592 */ 10593static int 10594ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10595{ 10596 if (io->io_hdr.io_type != CTL_IO_SCSI) 10597 return (1); 10598 10599 switch (io->scsiio.cdb[0]) { 10600 case COMPARE_AND_WRITE: { 10601 struct scsi_compare_and_write *cdb; 10602 10603 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10604 10605 *lba = scsi_8btou64(cdb->addr); 10606 *len = cdb->length; 10607 break; 10608 } 10609 case READ_6: 10610 case WRITE_6: { 10611 struct scsi_rw_6 *cdb; 10612 10613 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10614 10615 *lba = scsi_3btoul(cdb->addr); 10616 /* only 5 bits are valid in the most significant address byte */ 10617 *lba &= 0x1fffff; 10618 *len = cdb->length; 10619 break; 10620 } 10621 case READ_10: 10622 case WRITE_10: { 10623 struct scsi_rw_10 *cdb; 10624 10625 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10626 10627 *lba = scsi_4btoul(cdb->addr); 10628 *len = scsi_2btoul(cdb->length); 10629 break; 10630 } 10631 case WRITE_VERIFY_10: { 10632 struct scsi_write_verify_10 *cdb; 10633 10634 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10635 10636 *lba = scsi_4btoul(cdb->addr); 10637 *len = scsi_2btoul(cdb->length); 10638 break; 10639 } 10640 case READ_12: 10641 case WRITE_12: { 10642 struct scsi_rw_12 *cdb; 10643 10644 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10645 10646 *lba = scsi_4btoul(cdb->addr); 10647 *len = scsi_4btoul(cdb->length); 10648 break; 10649 } 10650 case WRITE_VERIFY_12: { 10651 struct scsi_write_verify_12 *cdb; 10652 10653 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10654 10655 *lba = scsi_4btoul(cdb->addr); 10656 *len = scsi_4btoul(cdb->length); 10657 break; 10658 } 10659 case READ_16: 10660 case WRITE_16: { 10661 struct scsi_rw_16 *cdb; 10662 10663 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10664 10665 *lba = scsi_8btou64(cdb->addr); 10666 *len = scsi_4btoul(cdb->length); 10667 break; 10668 } 10669 case WRITE_VERIFY_16: { 10670 struct scsi_write_verify_16 *cdb; 10671 10672 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10673 10674 10675 *lba = scsi_8btou64(cdb->addr); 10676 *len = scsi_4btoul(cdb->length); 10677 break; 10678 } 10679 case WRITE_SAME_10: { 10680 struct scsi_write_same_10 *cdb; 10681 10682 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10683 10684 *lba = scsi_4btoul(cdb->addr); 10685 *len = scsi_2btoul(cdb->length); 10686 break; 10687 } 10688 case WRITE_SAME_16: { 10689 struct scsi_write_same_16 *cdb; 10690 10691 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10692 10693 *lba = scsi_8btou64(cdb->addr); 10694 *len = scsi_4btoul(cdb->length); 10695 break; 10696 } 10697 case VERIFY_10: { 10698 struct scsi_verify_10 *cdb; 10699 10700 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10701 10702 *lba = scsi_4btoul(cdb->addr); 10703 *len = scsi_2btoul(cdb->length); 10704 break; 10705 } 10706 case VERIFY_12: { 10707 struct scsi_verify_12 *cdb; 10708 10709 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10710 10711 *lba = scsi_4btoul(cdb->addr); 10712 *len = scsi_4btoul(cdb->length); 10713 break; 10714 } 10715 case VERIFY_16: { 10716 struct scsi_verify_16 *cdb; 10717 10718 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10719 10720 *lba = scsi_8btou64(cdb->addr); 10721 *len = scsi_4btoul(cdb->length); 10722 break; 10723 } 10724 default: 10725 return (1); 10726 break; /* NOTREACHED */ 10727 } 10728 10729 return (0); 10730} 10731 10732static ctl_action 10733ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10734{ 10735 uint64_t endlba1, endlba2; 10736 10737 endlba1 = lba1 + len1 - 1; 10738 endlba2 = lba2 + len2 - 1; 10739 10740 if ((endlba1 < lba2) 10741 || (endlba2 < lba1)) 10742 return (CTL_ACTION_PASS); 10743 else 10744 return (CTL_ACTION_BLOCK); 10745} 10746 10747static ctl_action 10748ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10749{ 10750 uint64_t lba1, lba2; 10751 uint32_t len1, len2; 10752 int retval; 10753 10754 retval = ctl_get_lba_len(io1, &lba1, &len1); 10755 if (retval != 0) 10756 return (CTL_ACTION_ERROR); 10757 10758 retval = ctl_get_lba_len(io2, &lba2, &len2); 10759 if (retval != 0) 10760 return (CTL_ACTION_ERROR); 10761 10762 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10763} 10764 10765static ctl_action 10766ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10767{ 10768 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10769 ctl_serialize_action *serialize_row; 10770 10771 /* 10772 * The initiator attempted multiple untagged commands at the same 10773 * time. Can't do that. 10774 */ 10775 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10776 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10777 && ((pending_io->io_hdr.nexus.targ_port == 10778 ooa_io->io_hdr.nexus.targ_port) 10779 && (pending_io->io_hdr.nexus.initid.id == 10780 ooa_io->io_hdr.nexus.initid.id)) 10781 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10782 return (CTL_ACTION_OVERLAP); 10783 10784 /* 10785 * The initiator attempted to send multiple tagged commands with 10786 * the same ID. (It's fine if different initiators have the same 10787 * tag ID.) 10788 * 10789 * Even if all of those conditions are true, we don't kill the I/O 10790 * if the command ahead of us has been aborted. We won't end up 10791 * sending it to the FETD, and it's perfectly legal to resend a 10792 * command with the same tag number as long as the previous 10793 * instance of this tag number has been aborted somehow. 10794 */ 10795 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10796 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10797 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10798 && ((pending_io->io_hdr.nexus.targ_port == 10799 ooa_io->io_hdr.nexus.targ_port) 10800 && (pending_io->io_hdr.nexus.initid.id == 10801 ooa_io->io_hdr.nexus.initid.id)) 10802 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10803 return (CTL_ACTION_OVERLAP_TAG); 10804 10805 /* 10806 * If we get a head of queue tag, SAM-3 says that we should 10807 * immediately execute it. 10808 * 10809 * What happens if this command would normally block for some other 10810 * reason? e.g. a request sense with a head of queue tag 10811 * immediately after a write. Normally that would block, but this 10812 * will result in its getting executed immediately... 10813 * 10814 * We currently return "pass" instead of "skip", so we'll end up 10815 * going through the rest of the queue to check for overlapped tags. 10816 * 10817 * XXX KDM check for other types of blockage first?? 10818 */ 10819 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10820 return (CTL_ACTION_PASS); 10821 10822 /* 10823 * Ordered tags have to block until all items ahead of them 10824 * have completed. If we get called with an ordered tag, we always 10825 * block, if something else is ahead of us in the queue. 10826 */ 10827 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10828 return (CTL_ACTION_BLOCK); 10829 10830 /* 10831 * Simple tags get blocked until all head of queue and ordered tags 10832 * ahead of them have completed. I'm lumping untagged commands in 10833 * with simple tags here. XXX KDM is that the right thing to do? 10834 */ 10835 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10836 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10837 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10838 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10839 return (CTL_ACTION_BLOCK); 10840 10841 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10842 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10843 10844 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10845 10846 switch (serialize_row[pending_entry->seridx]) { 10847 case CTL_SER_BLOCK: 10848 return (CTL_ACTION_BLOCK); 10849 break; /* NOTREACHED */ 10850 case CTL_SER_EXTENT: 10851 return (ctl_extent_check(pending_io, ooa_io)); 10852 break; /* NOTREACHED */ 10853 case CTL_SER_PASS: 10854 return (CTL_ACTION_PASS); 10855 break; /* NOTREACHED */ 10856 case CTL_SER_SKIP: 10857 return (CTL_ACTION_SKIP); 10858 break; 10859 default: 10860 panic("invalid serialization value %d", 10861 serialize_row[pending_entry->seridx]); 10862 break; /* NOTREACHED */ 10863 } 10864 10865 return (CTL_ACTION_ERROR); 10866} 10867 10868/* 10869 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10870 * Assumptions: 10871 * - pending_io is generally either incoming, or on the blocked queue 10872 * - starting I/O is the I/O we want to start the check with. 10873 */ 10874static ctl_action 10875ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10876 union ctl_io *starting_io) 10877{ 10878 union ctl_io *ooa_io; 10879 ctl_action action; 10880 10881 mtx_assert(&lun->lun_lock, MA_OWNED); 10882 10883 /* 10884 * Run back along the OOA queue, starting with the current 10885 * blocked I/O and going through every I/O before it on the 10886 * queue. If starting_io is NULL, we'll just end up returning 10887 * CTL_ACTION_PASS. 10888 */ 10889 for (ooa_io = starting_io; ooa_io != NULL; 10890 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10891 ooa_links)){ 10892 10893 /* 10894 * This routine just checks to see whether 10895 * cur_blocked is blocked by ooa_io, which is ahead 10896 * of it in the queue. It doesn't queue/dequeue 10897 * cur_blocked. 10898 */ 10899 action = ctl_check_for_blockage(pending_io, ooa_io); 10900 switch (action) { 10901 case CTL_ACTION_BLOCK: 10902 case CTL_ACTION_OVERLAP: 10903 case CTL_ACTION_OVERLAP_TAG: 10904 case CTL_ACTION_SKIP: 10905 case CTL_ACTION_ERROR: 10906 return (action); 10907 break; /* NOTREACHED */ 10908 case CTL_ACTION_PASS: 10909 break; 10910 default: 10911 panic("invalid action %d", action); 10912 break; /* NOTREACHED */ 10913 } 10914 } 10915 10916 return (CTL_ACTION_PASS); 10917} 10918 10919/* 10920 * Assumptions: 10921 * - An I/O has just completed, and has been removed from the per-LUN OOA 10922 * queue, so some items on the blocked queue may now be unblocked. 10923 */ 10924static int 10925ctl_check_blocked(struct ctl_lun *lun) 10926{ 10927 union ctl_io *cur_blocked, *next_blocked; 10928 10929 mtx_assert(&lun->lun_lock, MA_OWNED); 10930 10931 /* 10932 * Run forward from the head of the blocked queue, checking each 10933 * entry against the I/Os prior to it on the OOA queue to see if 10934 * there is still any blockage. 10935 * 10936 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10937 * with our removing a variable on it while it is traversing the 10938 * list. 10939 */ 10940 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10941 cur_blocked != NULL; cur_blocked = next_blocked) { 10942 union ctl_io *prev_ooa; 10943 ctl_action action; 10944 10945 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10946 blocked_links); 10947 10948 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10949 ctl_ooaq, ooa_links); 10950 10951 /* 10952 * If cur_blocked happens to be the first item in the OOA 10953 * queue now, prev_ooa will be NULL, and the action 10954 * returned will just be CTL_ACTION_PASS. 10955 */ 10956 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10957 10958 switch (action) { 10959 case CTL_ACTION_BLOCK: 10960 /* Nothing to do here, still blocked */ 10961 break; 10962 case CTL_ACTION_OVERLAP: 10963 case CTL_ACTION_OVERLAP_TAG: 10964 /* 10965 * This shouldn't happen! In theory we've already 10966 * checked this command for overlap... 10967 */ 10968 break; 10969 case CTL_ACTION_PASS: 10970 case CTL_ACTION_SKIP: { 10971 struct ctl_softc *softc; 10972 const struct ctl_cmd_entry *entry; 10973 uint32_t initidx; 10974 int isc_retval; 10975 10976 /* 10977 * The skip case shouldn't happen, this transaction 10978 * should have never made it onto the blocked queue. 10979 */ 10980 /* 10981 * This I/O is no longer blocked, we can remove it 10982 * from the blocked queue. Since this is a TAILQ 10983 * (doubly linked list), we can do O(1) removals 10984 * from any place on the list. 10985 */ 10986 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10987 blocked_links); 10988 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10989 10990 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10991 /* 10992 * Need to send IO back to original side to 10993 * run 10994 */ 10995 union ctl_ha_msg msg_info; 10996 10997 msg_info.hdr.original_sc = 10998 cur_blocked->io_hdr.original_sc; 10999 msg_info.hdr.serializing_sc = cur_blocked; 11000 msg_info.hdr.msg_type = CTL_MSG_R2R; 11001 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11002 &msg_info, sizeof(msg_info), 0)) > 11003 CTL_HA_STATUS_SUCCESS) { 11004 printf("CTL:Check Blocked error from " 11005 "ctl_ha_msg_send %d\n", 11006 isc_retval); 11007 } 11008 break; 11009 } 11010 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 11011 softc = control_softc; 11012 11013 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11014 11015 /* 11016 * Check this I/O for LUN state changes that may 11017 * have happened while this command was blocked. 11018 * The LUN state may have been changed by a command 11019 * ahead of us in the queue, so we need to re-check 11020 * for any states that can be caused by SCSI 11021 * commands. 11022 */ 11023 if (ctl_scsiio_lun_check(softc, lun, entry, 11024 &cur_blocked->scsiio) == 0) { 11025 cur_blocked->io_hdr.flags |= 11026 CTL_FLAG_IS_WAS_ON_RTR; 11027 ctl_enqueue_rtr(cur_blocked); 11028 } else 11029 ctl_done(cur_blocked); 11030 break; 11031 } 11032 default: 11033 /* 11034 * This probably shouldn't happen -- we shouldn't 11035 * get CTL_ACTION_ERROR, or anything else. 11036 */ 11037 break; 11038 } 11039 } 11040 11041 return (CTL_RETVAL_COMPLETE); 11042} 11043 11044/* 11045 * This routine (with one exception) checks LUN flags that can be set by 11046 * commands ahead of us in the OOA queue. These flags have to be checked 11047 * when a command initially comes in, and when we pull a command off the 11048 * blocked queue and are preparing to execute it. The reason we have to 11049 * check these flags for commands on the blocked queue is that the LUN 11050 * state may have been changed by a command ahead of us while we're on the 11051 * blocked queue. 11052 * 11053 * Ordering is somewhat important with these checks, so please pay 11054 * careful attention to the placement of any new checks. 11055 */ 11056static int 11057ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11058 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11059{ 11060 int retval; 11061 11062 retval = 0; 11063 11064 mtx_assert(&lun->lun_lock, MA_OWNED); 11065 11066 /* 11067 * If this shelf is a secondary shelf controller, we have to reject 11068 * any media access commands. 11069 */ 11070#if 0 11071 /* No longer needed for HA */ 11072 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11073 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11074 ctl_set_lun_standby(ctsio); 11075 retval = 1; 11076 goto bailout; 11077 } 11078#endif 11079 11080 /* 11081 * Check for a reservation conflict. If this command isn't allowed 11082 * even on reserved LUNs, and if this initiator isn't the one who 11083 * reserved us, reject the command with a reservation conflict. 11084 */ 11085 if ((lun->flags & CTL_LUN_RESERVED) 11086 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11087 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11088 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11089 || (ctsio->io_hdr.nexus.targ_target.id != 11090 lun->rsv_nexus.targ_target.id)) { 11091 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11092 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11093 retval = 1; 11094 goto bailout; 11095 } 11096 } 11097 11098 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11099 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11100 uint32_t residx; 11101 11102 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11103 /* 11104 * if we aren't registered or it's a res holder type 11105 * reservation and this isn't the res holder then set a 11106 * conflict. 11107 * NOTE: Commands which might be allowed on write exclusive 11108 * type reservations are checked in the particular command 11109 * for a conflict. Read and SSU are the only ones. 11110 */ 11111 if (!lun->per_res[residx].registered 11112 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11113 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11114 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11115 retval = 1; 11116 goto bailout; 11117 } 11118 11119 } 11120 11121 if ((lun->flags & CTL_LUN_OFFLINE) 11122 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11123 ctl_set_lun_not_ready(ctsio); 11124 retval = 1; 11125 goto bailout; 11126 } 11127 11128 /* 11129 * If the LUN is stopped, see if this particular command is allowed 11130 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11131 */ 11132 if ((lun->flags & CTL_LUN_STOPPED) 11133 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11134 /* "Logical unit not ready, initializing cmd. required" */ 11135 ctl_set_lun_stopped(ctsio); 11136 retval = 1; 11137 goto bailout; 11138 } 11139 11140 if ((lun->flags & CTL_LUN_INOPERABLE) 11141 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11142 /* "Medium format corrupted" */ 11143 ctl_set_medium_format_corrupted(ctsio); 11144 retval = 1; 11145 goto bailout; 11146 } 11147 11148bailout: 11149 return (retval); 11150 11151} 11152 11153static void 11154ctl_failover_io(union ctl_io *io, int have_lock) 11155{ 11156 ctl_set_busy(&io->scsiio); 11157 ctl_done(io); 11158} 11159 11160static void 11161ctl_failover(void) 11162{ 11163 struct ctl_lun *lun; 11164 struct ctl_softc *ctl_softc; 11165 union ctl_io *next_io, *pending_io; 11166 union ctl_io *io; 11167 int lun_idx; 11168 int i; 11169 11170 ctl_softc = control_softc; 11171 11172 mtx_lock(&ctl_softc->ctl_lock); 11173 /* 11174 * Remove any cmds from the other SC from the rtr queue. These 11175 * will obviously only be for LUNs for which we're the primary. 11176 * We can't send status or get/send data for these commands. 11177 * Since they haven't been executed yet, we can just remove them. 11178 * We'll either abort them or delete them below, depending on 11179 * which HA mode we're in. 11180 */ 11181#ifdef notyet 11182 mtx_lock(&ctl_softc->queue_lock); 11183 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11184 io != NULL; io = next_io) { 11185 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11186 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11187 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11188 ctl_io_hdr, links); 11189 } 11190 mtx_unlock(&ctl_softc->queue_lock); 11191#endif 11192 11193 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11194 lun = ctl_softc->ctl_luns[lun_idx]; 11195 if (lun==NULL) 11196 continue; 11197 11198 /* 11199 * Processor LUNs are primary on both sides. 11200 * XXX will this always be true? 11201 */ 11202 if (lun->be_lun->lun_type == T_PROCESSOR) 11203 continue; 11204 11205 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11206 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11207 printf("FAILOVER: primary lun %d\n", lun_idx); 11208 /* 11209 * Remove all commands from the other SC. First from the 11210 * blocked queue then from the ooa queue. Once we have 11211 * removed them. Call ctl_check_blocked to see if there 11212 * is anything that can run. 11213 */ 11214 for (io = (union ctl_io *)TAILQ_FIRST( 11215 &lun->blocked_queue); io != NULL; io = next_io) { 11216 11217 next_io = (union ctl_io *)TAILQ_NEXT( 11218 &io->io_hdr, blocked_links); 11219 11220 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11221 TAILQ_REMOVE(&lun->blocked_queue, 11222 &io->io_hdr,blocked_links); 11223 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11224 TAILQ_REMOVE(&lun->ooa_queue, 11225 &io->io_hdr, ooa_links); 11226 11227 ctl_free_io(io); 11228 } 11229 } 11230 11231 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11232 io != NULL; io = next_io) { 11233 11234 next_io = (union ctl_io *)TAILQ_NEXT( 11235 &io->io_hdr, ooa_links); 11236 11237 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11238 11239 TAILQ_REMOVE(&lun->ooa_queue, 11240 &io->io_hdr, 11241 ooa_links); 11242 11243 ctl_free_io(io); 11244 } 11245 } 11246 ctl_check_blocked(lun); 11247 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11248 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11249 11250 printf("FAILOVER: primary lun %d\n", lun_idx); 11251 /* 11252 * Abort all commands from the other SC. We can't 11253 * send status back for them now. These should get 11254 * cleaned up when they are completed or come out 11255 * for a datamove operation. 11256 */ 11257 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11258 io != NULL; io = next_io) { 11259 next_io = (union ctl_io *)TAILQ_NEXT( 11260 &io->io_hdr, ooa_links); 11261 11262 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11263 io->io_hdr.flags |= CTL_FLAG_ABORT; 11264 } 11265 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11266 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11267 11268 printf("FAILOVER: secondary lun %d\n", lun_idx); 11269 11270 lun->flags |= CTL_LUN_PRIMARY_SC; 11271 11272 /* 11273 * We send all I/O that was sent to this controller 11274 * and redirected to the other side back with 11275 * busy status, and have the initiator retry it. 11276 * Figuring out how much data has been transferred, 11277 * etc. and picking up where we left off would be 11278 * very tricky. 11279 * 11280 * XXX KDM need to remove I/O from the blocked 11281 * queue as well! 11282 */ 11283 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11284 &lun->ooa_queue); pending_io != NULL; 11285 pending_io = next_io) { 11286 11287 next_io = (union ctl_io *)TAILQ_NEXT( 11288 &pending_io->io_hdr, ooa_links); 11289 11290 pending_io->io_hdr.flags &= 11291 ~CTL_FLAG_SENT_2OTHER_SC; 11292 11293 if (pending_io->io_hdr.flags & 11294 CTL_FLAG_IO_ACTIVE) { 11295 pending_io->io_hdr.flags |= 11296 CTL_FLAG_FAILOVER; 11297 } else { 11298 ctl_set_busy(&pending_io->scsiio); 11299 ctl_done(pending_io); 11300 } 11301 } 11302 11303 /* 11304 * Build Unit Attention 11305 */ 11306 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11307 lun->pending_sense[i].ua_pending |= 11308 CTL_UA_ASYM_ACC_CHANGE; 11309 } 11310 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11311 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11312 printf("FAILOVER: secondary lun %d\n", lun_idx); 11313 /* 11314 * if the first io on the OOA is not on the RtR queue 11315 * add it. 11316 */ 11317 lun->flags |= CTL_LUN_PRIMARY_SC; 11318 11319 pending_io = (union ctl_io *)TAILQ_FIRST( 11320 &lun->ooa_queue); 11321 if (pending_io==NULL) { 11322 printf("Nothing on OOA queue\n"); 11323 continue; 11324 } 11325 11326 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11327 if ((pending_io->io_hdr.flags & 11328 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11329 pending_io->io_hdr.flags |= 11330 CTL_FLAG_IS_WAS_ON_RTR; 11331 ctl_enqueue_rtr(pending_io); 11332 } 11333#if 0 11334 else 11335 { 11336 printf("Tag 0x%04x is running\n", 11337 pending_io->scsiio.tag_num); 11338 } 11339#endif 11340 11341 next_io = (union ctl_io *)TAILQ_NEXT( 11342 &pending_io->io_hdr, ooa_links); 11343 for (pending_io=next_io; pending_io != NULL; 11344 pending_io = next_io) { 11345 pending_io->io_hdr.flags &= 11346 ~CTL_FLAG_SENT_2OTHER_SC; 11347 next_io = (union ctl_io *)TAILQ_NEXT( 11348 &pending_io->io_hdr, ooa_links); 11349 if (pending_io->io_hdr.flags & 11350 CTL_FLAG_IS_WAS_ON_RTR) { 11351#if 0 11352 printf("Tag 0x%04x is running\n", 11353 pending_io->scsiio.tag_num); 11354#endif 11355 continue; 11356 } 11357 11358 switch (ctl_check_ooa(lun, pending_io, 11359 (union ctl_io *)TAILQ_PREV( 11360 &pending_io->io_hdr, ctl_ooaq, 11361 ooa_links))) { 11362 11363 case CTL_ACTION_BLOCK: 11364 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11365 &pending_io->io_hdr, 11366 blocked_links); 11367 pending_io->io_hdr.flags |= 11368 CTL_FLAG_BLOCKED; 11369 break; 11370 case CTL_ACTION_PASS: 11371 case CTL_ACTION_SKIP: 11372 pending_io->io_hdr.flags |= 11373 CTL_FLAG_IS_WAS_ON_RTR; 11374 ctl_enqueue_rtr(pending_io); 11375 break; 11376 case CTL_ACTION_OVERLAP: 11377 ctl_set_overlapped_cmd( 11378 (struct ctl_scsiio *)pending_io); 11379 ctl_done(pending_io); 11380 break; 11381 case CTL_ACTION_OVERLAP_TAG: 11382 ctl_set_overlapped_tag( 11383 (struct ctl_scsiio *)pending_io, 11384 pending_io->scsiio.tag_num & 0xff); 11385 ctl_done(pending_io); 11386 break; 11387 case CTL_ACTION_ERROR: 11388 default: 11389 ctl_set_internal_failure( 11390 (struct ctl_scsiio *)pending_io, 11391 0, // sks_valid 11392 0); //retry count 11393 ctl_done(pending_io); 11394 break; 11395 } 11396 } 11397 11398 /* 11399 * Build Unit Attention 11400 */ 11401 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11402 lun->pending_sense[i].ua_pending |= 11403 CTL_UA_ASYM_ACC_CHANGE; 11404 } 11405 } else { 11406 panic("Unhandled HA mode failover, LUN flags = %#x, " 11407 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11408 } 11409 } 11410 ctl_pause_rtr = 0; 11411 mtx_unlock(&ctl_softc->ctl_lock); 11412} 11413 11414static int 11415ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11416{ 11417 struct ctl_lun *lun; 11418 const struct ctl_cmd_entry *entry; 11419 uint32_t initidx, targ_lun; 11420 int retval; 11421 11422 retval = 0; 11423 11424 lun = NULL; 11425 11426 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11427 if ((targ_lun < CTL_MAX_LUNS) 11428 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11429 lun = ctl_softc->ctl_luns[targ_lun]; 11430 /* 11431 * If the LUN is invalid, pretend that it doesn't exist. 11432 * It will go away as soon as all pending I/O has been 11433 * completed. 11434 */ 11435 if (lun->flags & CTL_LUN_DISABLED) { 11436 lun = NULL; 11437 } else { 11438 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11439 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11440 lun->be_lun; 11441 if (lun->be_lun->lun_type == T_PROCESSOR) { 11442 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11443 } 11444 11445 /* 11446 * Every I/O goes into the OOA queue for a 11447 * particular LUN, and stays there until completion. 11448 */ 11449 mtx_lock(&lun->lun_lock); 11450 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11451 ooa_links); 11452 } 11453 } else { 11454 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11455 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11456 } 11457 11458 /* Get command entry and return error if it is unsuppotyed. */ 11459 entry = ctl_validate_command(ctsio); 11460 if (entry == NULL) { 11461 if (lun) 11462 mtx_unlock(&lun->lun_lock); 11463 return (retval); 11464 } 11465 11466 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11467 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11468 11469 /* 11470 * Check to see whether we can send this command to LUNs that don't 11471 * exist. This should pretty much only be the case for inquiry 11472 * and request sense. Further checks, below, really require having 11473 * a LUN, so we can't really check the command anymore. Just put 11474 * it on the rtr queue. 11475 */ 11476 if (lun == NULL) { 11477 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11478 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11479 ctl_enqueue_rtr((union ctl_io *)ctsio); 11480 return (retval); 11481 } 11482 11483 ctl_set_unsupported_lun(ctsio); 11484 ctl_done((union ctl_io *)ctsio); 11485 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11486 return (retval); 11487 } else { 11488 /* 11489 * Make sure we support this particular command on this LUN. 11490 * e.g., we don't support writes to the control LUN. 11491 */ 11492 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11493 mtx_unlock(&lun->lun_lock); 11494 ctl_set_invalid_opcode(ctsio); 11495 ctl_done((union ctl_io *)ctsio); 11496 return (retval); 11497 } 11498 } 11499 11500 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11501 11502 /* 11503 * If we've got a request sense, it'll clear the contingent 11504 * allegiance condition. Otherwise, if we have a CA condition for 11505 * this initiator, clear it, because it sent down a command other 11506 * than request sense. 11507 */ 11508 if ((ctsio->cdb[0] != REQUEST_SENSE) 11509 && (ctl_is_set(lun->have_ca, initidx))) 11510 ctl_clear_mask(lun->have_ca, initidx); 11511 11512 /* 11513 * If the command has this flag set, it handles its own unit 11514 * attention reporting, we shouldn't do anything. Otherwise we 11515 * check for any pending unit attentions, and send them back to the 11516 * initiator. We only do this when a command initially comes in, 11517 * not when we pull it off the blocked queue. 11518 * 11519 * According to SAM-3, section 5.3.2, the order that things get 11520 * presented back to the host is basically unit attentions caused 11521 * by some sort of reset event, busy status, reservation conflicts 11522 * or task set full, and finally any other status. 11523 * 11524 * One issue here is that some of the unit attentions we report 11525 * don't fall into the "reset" category (e.g. "reported luns data 11526 * has changed"). So reporting it here, before the reservation 11527 * check, may be technically wrong. I guess the only thing to do 11528 * would be to check for and report the reset events here, and then 11529 * check for the other unit attention types after we check for a 11530 * reservation conflict. 11531 * 11532 * XXX KDM need to fix this 11533 */ 11534 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11535 ctl_ua_type ua_type; 11536 11537 ua_type = lun->pending_sense[initidx].ua_pending; 11538 if (ua_type != CTL_UA_NONE) { 11539 scsi_sense_data_type sense_format; 11540 11541 if (lun != NULL) 11542 sense_format = (lun->flags & 11543 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11544 SSD_TYPE_FIXED; 11545 else 11546 sense_format = SSD_TYPE_FIXED; 11547 11548 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11549 sense_format); 11550 if (ua_type != CTL_UA_NONE) { 11551 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11552 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11553 CTL_AUTOSENSE; 11554 ctsio->sense_len = SSD_FULL_SIZE; 11555 lun->pending_sense[initidx].ua_pending &= 11556 ~ua_type; 11557 mtx_unlock(&lun->lun_lock); 11558 ctl_done((union ctl_io *)ctsio); 11559 return (retval); 11560 } 11561 } 11562 } 11563 11564 11565 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11566 mtx_unlock(&lun->lun_lock); 11567 ctl_done((union ctl_io *)ctsio); 11568 return (retval); 11569 } 11570 11571 /* 11572 * XXX CHD this is where we want to send IO to other side if 11573 * this LUN is secondary on this SC. We will need to make a copy 11574 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11575 * the copy we send as FROM_OTHER. 11576 * We also need to stuff the address of the original IO so we can 11577 * find it easily. Something similar will need be done on the other 11578 * side so when we are done we can find the copy. 11579 */ 11580 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11581 union ctl_ha_msg msg_info; 11582 int isc_retval; 11583 11584 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11585 11586 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11587 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11588#if 0 11589 printf("1. ctsio %p\n", ctsio); 11590#endif 11591 msg_info.hdr.serializing_sc = NULL; 11592 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11593 msg_info.scsi.tag_num = ctsio->tag_num; 11594 msg_info.scsi.tag_type = ctsio->tag_type; 11595 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11596 11597 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11598 11599 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11600 (void *)&msg_info, sizeof(msg_info), 0)) > 11601 CTL_HA_STATUS_SUCCESS) { 11602 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11603 isc_retval); 11604 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11605 } else { 11606#if 0 11607 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11608#endif 11609 } 11610 11611 /* 11612 * XXX KDM this I/O is off the incoming queue, but hasn't 11613 * been inserted on any other queue. We may need to come 11614 * up with a holding queue while we wait for serialization 11615 * so that we have an idea of what we're waiting for from 11616 * the other side. 11617 */ 11618 mtx_unlock(&lun->lun_lock); 11619 return (retval); 11620 } 11621 11622 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11623 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11624 ctl_ooaq, ooa_links))) { 11625 case CTL_ACTION_BLOCK: 11626 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11627 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11628 blocked_links); 11629 mtx_unlock(&lun->lun_lock); 11630 return (retval); 11631 case CTL_ACTION_PASS: 11632 case CTL_ACTION_SKIP: 11633 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11634 mtx_unlock(&lun->lun_lock); 11635 ctl_enqueue_rtr((union ctl_io *)ctsio); 11636 break; 11637 case CTL_ACTION_OVERLAP: 11638 mtx_unlock(&lun->lun_lock); 11639 ctl_set_overlapped_cmd(ctsio); 11640 ctl_done((union ctl_io *)ctsio); 11641 break; 11642 case CTL_ACTION_OVERLAP_TAG: 11643 mtx_unlock(&lun->lun_lock); 11644 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11645 ctl_done((union ctl_io *)ctsio); 11646 break; 11647 case CTL_ACTION_ERROR: 11648 default: 11649 mtx_unlock(&lun->lun_lock); 11650 ctl_set_internal_failure(ctsio, 11651 /*sks_valid*/ 0, 11652 /*retry_count*/ 0); 11653 ctl_done((union ctl_io *)ctsio); 11654 break; 11655 } 11656 return (retval); 11657} 11658 11659const struct ctl_cmd_entry * 11660ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11661{ 11662 const struct ctl_cmd_entry *entry; 11663 int service_action; 11664 11665 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11666 if (entry->flags & CTL_CMD_FLAG_SA5) { 11667 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11668 entry = &((const struct ctl_cmd_entry *) 11669 entry->execute)[service_action]; 11670 } 11671 return (entry); 11672} 11673 11674const struct ctl_cmd_entry * 11675ctl_validate_command(struct ctl_scsiio *ctsio) 11676{ 11677 const struct ctl_cmd_entry *entry; 11678 int i; 11679 uint8_t diff; 11680 11681 entry = ctl_get_cmd_entry(ctsio); 11682 if (entry->execute == NULL) { 11683 ctl_set_invalid_opcode(ctsio); 11684 ctl_done((union ctl_io *)ctsio); 11685 return (NULL); 11686 } 11687 KASSERT(entry->length > 0, 11688 ("Not defined length for command 0x%02x/0x%02x", 11689 ctsio->cdb[0], ctsio->cdb[1])); 11690 for (i = 1; i < entry->length; i++) { 11691 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11692 if (diff == 0) 11693 continue; 11694 ctl_set_invalid_field(ctsio, 11695 /*sks_valid*/ 1, 11696 /*command*/ 1, 11697 /*field*/ i, 11698 /*bit_valid*/ 1, 11699 /*bit*/ fls(diff) - 1); 11700 ctl_done((union ctl_io *)ctsio); 11701 return (NULL); 11702 } 11703 return (entry); 11704} 11705 11706static int 11707ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11708{ 11709 11710 switch (lun_type) { 11711 case T_PROCESSOR: 11712 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11713 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11714 return (0); 11715 break; 11716 case T_DIRECT: 11717 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11718 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11719 return (0); 11720 break; 11721 default: 11722 return (0); 11723 } 11724 return (1); 11725} 11726 11727static int 11728ctl_scsiio(struct ctl_scsiio *ctsio) 11729{ 11730 int retval; 11731 const struct ctl_cmd_entry *entry; 11732 11733 retval = CTL_RETVAL_COMPLETE; 11734 11735 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11736 11737 entry = ctl_get_cmd_entry(ctsio); 11738 11739 /* 11740 * If this I/O has been aborted, just send it straight to 11741 * ctl_done() without executing it. 11742 */ 11743 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11744 ctl_done((union ctl_io *)ctsio); 11745 goto bailout; 11746 } 11747 11748 /* 11749 * All the checks should have been handled by ctl_scsiio_precheck(). 11750 * We should be clear now to just execute the I/O. 11751 */ 11752 retval = entry->execute(ctsio); 11753 11754bailout: 11755 return (retval); 11756} 11757 11758/* 11759 * Since we only implement one target right now, a bus reset simply resets 11760 * our single target. 11761 */ 11762static int 11763ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11764{ 11765 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11766} 11767 11768static int 11769ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11770 ctl_ua_type ua_type) 11771{ 11772 struct ctl_lun *lun; 11773 int retval; 11774 11775 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11776 union ctl_ha_msg msg_info; 11777 11778 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11779 msg_info.hdr.nexus = io->io_hdr.nexus; 11780 if (ua_type==CTL_UA_TARG_RESET) 11781 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11782 else 11783 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11784 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11785 msg_info.hdr.original_sc = NULL; 11786 msg_info.hdr.serializing_sc = NULL; 11787 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11788 (void *)&msg_info, sizeof(msg_info), 0)) { 11789 } 11790 } 11791 retval = 0; 11792 11793 mtx_lock(&ctl_softc->ctl_lock); 11794 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11795 retval += ctl_lun_reset(lun, io, ua_type); 11796 mtx_unlock(&ctl_softc->ctl_lock); 11797 11798 return (retval); 11799} 11800 11801/* 11802 * The LUN should always be set. The I/O is optional, and is used to 11803 * distinguish between I/Os sent by this initiator, and by other 11804 * initiators. We set unit attention for initiators other than this one. 11805 * SAM-3 is vague on this point. It does say that a unit attention should 11806 * be established for other initiators when a LUN is reset (see section 11807 * 5.7.3), but it doesn't specifically say that the unit attention should 11808 * be established for this particular initiator when a LUN is reset. Here 11809 * is the relevant text, from SAM-3 rev 8: 11810 * 11811 * 5.7.2 When a SCSI initiator port aborts its own tasks 11812 * 11813 * When a SCSI initiator port causes its own task(s) to be aborted, no 11814 * notification that the task(s) have been aborted shall be returned to 11815 * the SCSI initiator port other than the completion response for the 11816 * command or task management function action that caused the task(s) to 11817 * be aborted and notification(s) associated with related effects of the 11818 * action (e.g., a reset unit attention condition). 11819 * 11820 * XXX KDM for now, we're setting unit attention for all initiators. 11821 */ 11822static int 11823ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11824{ 11825 union ctl_io *xio; 11826#if 0 11827 uint32_t initindex; 11828#endif 11829 int i; 11830 11831 mtx_lock(&lun->lun_lock); 11832 /* 11833 * Run through the OOA queue and abort each I/O. 11834 */ 11835#if 0 11836 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11837#endif 11838 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11839 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11840 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11841 } 11842 11843 /* 11844 * This version sets unit attention for every 11845 */ 11846#if 0 11847 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11848 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11849 if (initindex == i) 11850 continue; 11851 lun->pending_sense[i].ua_pending |= ua_type; 11852 } 11853#endif 11854 11855 /* 11856 * A reset (any kind, really) clears reservations established with 11857 * RESERVE/RELEASE. It does not clear reservations established 11858 * with PERSISTENT RESERVE OUT, but we don't support that at the 11859 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11860 * reservations made with the RESERVE/RELEASE commands, because 11861 * those commands are obsolete in SPC-3. 11862 */ 11863 lun->flags &= ~CTL_LUN_RESERVED; 11864 11865 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11866 ctl_clear_mask(lun->have_ca, i); 11867 lun->pending_sense[i].ua_pending |= ua_type; 11868 } 11869 mtx_unlock(&lun->lun_lock); 11870 11871 return (0); 11872} 11873 11874static int 11875ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11876 int other_sc) 11877{ 11878 union ctl_io *xio; 11879 int found; 11880 11881 mtx_assert(&lun->lun_lock, MA_OWNED); 11882 11883 /* 11884 * Run through the OOA queue and attempt to find the given I/O. 11885 * The target port, initiator ID, tag type and tag number have to 11886 * match the values that we got from the initiator. If we have an 11887 * untagged command to abort, simply abort the first untagged command 11888 * we come to. We only allow one untagged command at a time of course. 11889 */ 11890 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11891 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11892 11893 if ((targ_port == UINT32_MAX || 11894 targ_port == xio->io_hdr.nexus.targ_port) && 11895 (init_id == UINT32_MAX || 11896 init_id == xio->io_hdr.nexus.initid.id)) { 11897 if (targ_port != xio->io_hdr.nexus.targ_port || 11898 init_id != xio->io_hdr.nexus.initid.id) 11899 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11900 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11901 found = 1; 11902 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11903 union ctl_ha_msg msg_info; 11904 11905 msg_info.hdr.nexus = xio->io_hdr.nexus; 11906 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11907 msg_info.task.tag_num = xio->scsiio.tag_num; 11908 msg_info.task.tag_type = xio->scsiio.tag_type; 11909 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11910 msg_info.hdr.original_sc = NULL; 11911 msg_info.hdr.serializing_sc = NULL; 11912 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11913 (void *)&msg_info, sizeof(msg_info), 0); 11914 } 11915 } 11916 } 11917 return (found); 11918} 11919 11920static int 11921ctl_abort_task_set(union ctl_io *io) 11922{ 11923 struct ctl_softc *softc = control_softc; 11924 struct ctl_lun *lun; 11925 uint32_t targ_lun; 11926 11927 /* 11928 * Look up the LUN. 11929 */ 11930 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11931 mtx_lock(&softc->ctl_lock); 11932 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11933 lun = softc->ctl_luns[targ_lun]; 11934 else { 11935 mtx_unlock(&softc->ctl_lock); 11936 return (1); 11937 } 11938 11939 mtx_lock(&lun->lun_lock); 11940 mtx_unlock(&softc->ctl_lock); 11941 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11942 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11943 io->io_hdr.nexus.initid.id, 11944 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11945 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11946 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11947 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11948 } 11949 mtx_unlock(&lun->lun_lock); 11950 return (0); 11951} 11952 11953static int 11954ctl_i_t_nexus_reset(union ctl_io *io) 11955{ 11956 struct ctl_softc *softc = control_softc; 11957 struct ctl_lun *lun; 11958 uint32_t initindex; 11959 11960 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11961 mtx_lock(&softc->ctl_lock); 11962 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11963 mtx_lock(&lun->lun_lock); 11964 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11965 io->io_hdr.nexus.initid.id, 11966 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11967 ctl_clear_mask(lun->have_ca, initindex); 11968 lun->pending_sense[initindex].ua_pending |= CTL_UA_I_T_NEXUS_LOSS; 11969 mtx_unlock(&lun->lun_lock); 11970 } 11971 mtx_unlock(&softc->ctl_lock); 11972 return (0); 11973} 11974 11975static int 11976ctl_abort_task(union ctl_io *io) 11977{ 11978 union ctl_io *xio; 11979 struct ctl_lun *lun; 11980 struct ctl_softc *ctl_softc; 11981#if 0 11982 struct sbuf sb; 11983 char printbuf[128]; 11984#endif 11985 int found; 11986 uint32_t targ_lun; 11987 11988 ctl_softc = control_softc; 11989 found = 0; 11990 11991 /* 11992 * Look up the LUN. 11993 */ 11994 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11995 mtx_lock(&ctl_softc->ctl_lock); 11996 if ((targ_lun < CTL_MAX_LUNS) 11997 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11998 lun = ctl_softc->ctl_luns[targ_lun]; 11999 else { 12000 mtx_unlock(&ctl_softc->ctl_lock); 12001 return (1); 12002 } 12003 12004#if 0 12005 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12006 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12007#endif 12008 12009 mtx_lock(&lun->lun_lock); 12010 mtx_unlock(&ctl_softc->ctl_lock); 12011 /* 12012 * Run through the OOA queue and attempt to find the given I/O. 12013 * The target port, initiator ID, tag type and tag number have to 12014 * match the values that we got from the initiator. If we have an 12015 * untagged command to abort, simply abort the first untagged command 12016 * we come to. We only allow one untagged command at a time of course. 12017 */ 12018#if 0 12019 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12020#endif 12021 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12022 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12023#if 0 12024 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12025 12026 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12027 lun->lun, xio->scsiio.tag_num, 12028 xio->scsiio.tag_type, 12029 (xio->io_hdr.blocked_links.tqe_prev 12030 == NULL) ? "" : " BLOCKED", 12031 (xio->io_hdr.flags & 12032 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12033 (xio->io_hdr.flags & 12034 CTL_FLAG_ABORT) ? " ABORT" : "", 12035 (xio->io_hdr.flags & 12036 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12037 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12038 sbuf_finish(&sb); 12039 printf("%s\n", sbuf_data(&sb)); 12040#endif 12041 12042 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12043 && (xio->io_hdr.nexus.initid.id == 12044 io->io_hdr.nexus.initid.id)) { 12045 /* 12046 * If the abort says that the task is untagged, the 12047 * task in the queue must be untagged. Otherwise, 12048 * we just check to see whether the tag numbers 12049 * match. This is because the QLogic firmware 12050 * doesn't pass back the tag type in an abort 12051 * request. 12052 */ 12053#if 0 12054 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12055 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12056 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12057#endif 12058 /* 12059 * XXX KDM we've got problems with FC, because it 12060 * doesn't send down a tag type with aborts. So we 12061 * can only really go by the tag number... 12062 * This may cause problems with parallel SCSI. 12063 * Need to figure that out!! 12064 */ 12065 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12066 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12067 found = 1; 12068 if ((io->io_hdr.flags & 12069 CTL_FLAG_FROM_OTHER_SC) == 0 && 12070 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12071 union ctl_ha_msg msg_info; 12072 12073 io->io_hdr.flags |= 12074 CTL_FLAG_SENT_2OTHER_SC; 12075 msg_info.hdr.nexus = io->io_hdr.nexus; 12076 msg_info.task.task_action = 12077 CTL_TASK_ABORT_TASK; 12078 msg_info.task.tag_num = 12079 io->taskio.tag_num; 12080 msg_info.task.tag_type = 12081 io->taskio.tag_type; 12082 msg_info.hdr.msg_type = 12083 CTL_MSG_MANAGE_TASKS; 12084 msg_info.hdr.original_sc = NULL; 12085 msg_info.hdr.serializing_sc = NULL; 12086#if 0 12087 printf("Sent Abort to other side\n"); 12088#endif 12089 if (CTL_HA_STATUS_SUCCESS != 12090 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12091 (void *)&msg_info, 12092 sizeof(msg_info), 0)) { 12093 } 12094 } 12095#if 0 12096 printf("ctl_abort_task: found I/O to abort\n"); 12097#endif 12098 break; 12099 } 12100 } 12101 } 12102 mtx_unlock(&lun->lun_lock); 12103 12104 if (found == 0) { 12105 /* 12106 * This isn't really an error. It's entirely possible for 12107 * the abort and command completion to cross on the wire. 12108 * This is more of an informative/diagnostic error. 12109 */ 12110#if 0 12111 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12112 "%d:%d:%d:%d tag %d type %d\n", 12113 io->io_hdr.nexus.initid.id, 12114 io->io_hdr.nexus.targ_port, 12115 io->io_hdr.nexus.targ_target.id, 12116 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12117 io->taskio.tag_type); 12118#endif 12119 } 12120 return (0); 12121} 12122 12123static void 12124ctl_run_task(union ctl_io *io) 12125{ 12126 struct ctl_softc *ctl_softc = control_softc; 12127 int retval = 1; 12128 const char *task_desc; 12129 12130 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12131 12132 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12133 ("ctl_run_task: Unextected io_type %d\n", 12134 io->io_hdr.io_type)); 12135 12136 task_desc = ctl_scsi_task_string(&io->taskio); 12137 if (task_desc != NULL) { 12138#ifdef NEEDTOPORT 12139 csevent_log(CSC_CTL | CSC_SHELF_SW | 12140 CTL_TASK_REPORT, 12141 csevent_LogType_Trace, 12142 csevent_Severity_Information, 12143 csevent_AlertLevel_Green, 12144 csevent_FRU_Firmware, 12145 csevent_FRU_Unknown, 12146 "CTL: received task: %s",task_desc); 12147#endif 12148 } else { 12149#ifdef NEEDTOPORT 12150 csevent_log(CSC_CTL | CSC_SHELF_SW | 12151 CTL_TASK_REPORT, 12152 csevent_LogType_Trace, 12153 csevent_Severity_Information, 12154 csevent_AlertLevel_Green, 12155 csevent_FRU_Firmware, 12156 csevent_FRU_Unknown, 12157 "CTL: received unknown task " 12158 "type: %d (%#x)", 12159 io->taskio.task_action, 12160 io->taskio.task_action); 12161#endif 12162 } 12163 switch (io->taskio.task_action) { 12164 case CTL_TASK_ABORT_TASK: 12165 retval = ctl_abort_task(io); 12166 break; 12167 case CTL_TASK_ABORT_TASK_SET: 12168 case CTL_TASK_CLEAR_TASK_SET: 12169 retval = ctl_abort_task_set(io); 12170 break; 12171 case CTL_TASK_CLEAR_ACA: 12172 break; 12173 case CTL_TASK_I_T_NEXUS_RESET: 12174 retval = ctl_i_t_nexus_reset(io); 12175 break; 12176 case CTL_TASK_LUN_RESET: { 12177 struct ctl_lun *lun; 12178 uint32_t targ_lun; 12179 12180 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12181 mtx_lock(&ctl_softc->ctl_lock); 12182 if ((targ_lun < CTL_MAX_LUNS) 12183 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12184 lun = ctl_softc->ctl_luns[targ_lun]; 12185 else { 12186 mtx_unlock(&ctl_softc->ctl_lock); 12187 retval = 1; 12188 break; 12189 } 12190 12191 if (!(io->io_hdr.flags & 12192 CTL_FLAG_FROM_OTHER_SC)) { 12193 union ctl_ha_msg msg_info; 12194 12195 io->io_hdr.flags |= 12196 CTL_FLAG_SENT_2OTHER_SC; 12197 msg_info.hdr.msg_type = 12198 CTL_MSG_MANAGE_TASKS; 12199 msg_info.hdr.nexus = io->io_hdr.nexus; 12200 msg_info.task.task_action = 12201 CTL_TASK_LUN_RESET; 12202 msg_info.hdr.original_sc = NULL; 12203 msg_info.hdr.serializing_sc = NULL; 12204 if (CTL_HA_STATUS_SUCCESS != 12205 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12206 (void *)&msg_info, 12207 sizeof(msg_info), 0)) { 12208 } 12209 } 12210 12211 retval = ctl_lun_reset(lun, io, 12212 CTL_UA_LUN_RESET); 12213 mtx_unlock(&ctl_softc->ctl_lock); 12214 break; 12215 } 12216 case CTL_TASK_TARGET_RESET: 12217 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12218 break; 12219 case CTL_TASK_BUS_RESET: 12220 retval = ctl_bus_reset(ctl_softc, io); 12221 break; 12222 case CTL_TASK_PORT_LOGIN: 12223 break; 12224 case CTL_TASK_PORT_LOGOUT: 12225 break; 12226 default: 12227 printf("ctl_run_task: got unknown task management event %d\n", 12228 io->taskio.task_action); 12229 break; 12230 } 12231 if (retval == 0) 12232 io->io_hdr.status = CTL_SUCCESS; 12233 else 12234 io->io_hdr.status = CTL_ERROR; 12235 ctl_done(io); 12236} 12237 12238/* 12239 * For HA operation. Handle commands that come in from the other 12240 * controller. 12241 */ 12242static void 12243ctl_handle_isc(union ctl_io *io) 12244{ 12245 int free_io; 12246 struct ctl_lun *lun; 12247 struct ctl_softc *ctl_softc; 12248 uint32_t targ_lun; 12249 12250 ctl_softc = control_softc; 12251 12252 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12253 lun = ctl_softc->ctl_luns[targ_lun]; 12254 12255 switch (io->io_hdr.msg_type) { 12256 case CTL_MSG_SERIALIZE: 12257 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12258 break; 12259 case CTL_MSG_R2R: { 12260 const struct ctl_cmd_entry *entry; 12261 12262 /* 12263 * This is only used in SER_ONLY mode. 12264 */ 12265 free_io = 0; 12266 entry = ctl_get_cmd_entry(&io->scsiio); 12267 mtx_lock(&lun->lun_lock); 12268 if (ctl_scsiio_lun_check(ctl_softc, lun, 12269 entry, (struct ctl_scsiio *)io) != 0) { 12270 mtx_unlock(&lun->lun_lock); 12271 ctl_done(io); 12272 break; 12273 } 12274 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12275 mtx_unlock(&lun->lun_lock); 12276 ctl_enqueue_rtr(io); 12277 break; 12278 } 12279 case CTL_MSG_FINISH_IO: 12280 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12281 free_io = 0; 12282 ctl_done(io); 12283 } else { 12284 free_io = 1; 12285 mtx_lock(&lun->lun_lock); 12286 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12287 ooa_links); 12288 ctl_check_blocked(lun); 12289 mtx_unlock(&lun->lun_lock); 12290 } 12291 break; 12292 case CTL_MSG_PERS_ACTION: 12293 ctl_hndl_per_res_out_on_other_sc( 12294 (union ctl_ha_msg *)&io->presio.pr_msg); 12295 free_io = 1; 12296 break; 12297 case CTL_MSG_BAD_JUJU: 12298 free_io = 0; 12299 ctl_done(io); 12300 break; 12301 case CTL_MSG_DATAMOVE: 12302 /* Only used in XFER mode */ 12303 free_io = 0; 12304 ctl_datamove_remote(io); 12305 break; 12306 case CTL_MSG_DATAMOVE_DONE: 12307 /* Only used in XFER mode */ 12308 free_io = 0; 12309 io->scsiio.be_move_done(io); 12310 break; 12311 default: 12312 free_io = 1; 12313 printf("%s: Invalid message type %d\n", 12314 __func__, io->io_hdr.msg_type); 12315 break; 12316 } 12317 if (free_io) 12318 ctl_free_io(io); 12319 12320} 12321 12322 12323/* 12324 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12325 * there is no match. 12326 */ 12327static ctl_lun_error_pattern 12328ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12329{ 12330 const struct ctl_cmd_entry *entry; 12331 ctl_lun_error_pattern filtered_pattern, pattern; 12332 12333 pattern = desc->error_pattern; 12334 12335 /* 12336 * XXX KDM we need more data passed into this function to match a 12337 * custom pattern, and we actually need to implement custom pattern 12338 * matching. 12339 */ 12340 if (pattern & CTL_LUN_PAT_CMD) 12341 return (CTL_LUN_PAT_CMD); 12342 12343 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12344 return (CTL_LUN_PAT_ANY); 12345 12346 entry = ctl_get_cmd_entry(ctsio); 12347 12348 filtered_pattern = entry->pattern & pattern; 12349 12350 /* 12351 * If the user requested specific flags in the pattern (e.g. 12352 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12353 * flags. 12354 * 12355 * If the user did not specify any flags, it doesn't matter whether 12356 * or not the command supports the flags. 12357 */ 12358 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12359 (pattern & ~CTL_LUN_PAT_MASK)) 12360 return (CTL_LUN_PAT_NONE); 12361 12362 /* 12363 * If the user asked for a range check, see if the requested LBA 12364 * range overlaps with this command's LBA range. 12365 */ 12366 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12367 uint64_t lba1; 12368 uint32_t len1; 12369 ctl_action action; 12370 int retval; 12371 12372 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12373 if (retval != 0) 12374 return (CTL_LUN_PAT_NONE); 12375 12376 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12377 desc->lba_range.len); 12378 /* 12379 * A "pass" means that the LBA ranges don't overlap, so 12380 * this doesn't match the user's range criteria. 12381 */ 12382 if (action == CTL_ACTION_PASS) 12383 return (CTL_LUN_PAT_NONE); 12384 } 12385 12386 return (filtered_pattern); 12387} 12388 12389static void 12390ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12391{ 12392 struct ctl_error_desc *desc, *desc2; 12393 12394 mtx_assert(&lun->lun_lock, MA_OWNED); 12395 12396 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12397 ctl_lun_error_pattern pattern; 12398 /* 12399 * Check to see whether this particular command matches 12400 * the pattern in the descriptor. 12401 */ 12402 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12403 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12404 continue; 12405 12406 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12407 case CTL_LUN_INJ_ABORTED: 12408 ctl_set_aborted(&io->scsiio); 12409 break; 12410 case CTL_LUN_INJ_MEDIUM_ERR: 12411 ctl_set_medium_error(&io->scsiio); 12412 break; 12413 case CTL_LUN_INJ_UA: 12414 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12415 * OCCURRED */ 12416 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12417 break; 12418 case CTL_LUN_INJ_CUSTOM: 12419 /* 12420 * We're assuming the user knows what he is doing. 12421 * Just copy the sense information without doing 12422 * checks. 12423 */ 12424 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12425 ctl_min(sizeof(desc->custom_sense), 12426 sizeof(io->scsiio.sense_data))); 12427 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12428 io->scsiio.sense_len = SSD_FULL_SIZE; 12429 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12430 break; 12431 case CTL_LUN_INJ_NONE: 12432 default: 12433 /* 12434 * If this is an error injection type we don't know 12435 * about, clear the continuous flag (if it is set) 12436 * so it will get deleted below. 12437 */ 12438 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12439 break; 12440 } 12441 /* 12442 * By default, each error injection action is a one-shot 12443 */ 12444 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12445 continue; 12446 12447 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12448 12449 free(desc, M_CTL); 12450 } 12451} 12452 12453#ifdef CTL_IO_DELAY 12454static void 12455ctl_datamove_timer_wakeup(void *arg) 12456{ 12457 union ctl_io *io; 12458 12459 io = (union ctl_io *)arg; 12460 12461 ctl_datamove(io); 12462} 12463#endif /* CTL_IO_DELAY */ 12464 12465void 12466ctl_datamove(union ctl_io *io) 12467{ 12468 void (*fe_datamove)(union ctl_io *io); 12469 12470 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12471 12472 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12473 12474#ifdef CTL_TIME_IO 12475 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12476 char str[256]; 12477 char path_str[64]; 12478 struct sbuf sb; 12479 12480 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12481 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12482 12483 sbuf_cat(&sb, path_str); 12484 switch (io->io_hdr.io_type) { 12485 case CTL_IO_SCSI: 12486 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12487 sbuf_printf(&sb, "\n"); 12488 sbuf_cat(&sb, path_str); 12489 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12490 io->scsiio.tag_num, io->scsiio.tag_type); 12491 break; 12492 case CTL_IO_TASK: 12493 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12494 "Tag Type: %d\n", io->taskio.task_action, 12495 io->taskio.tag_num, io->taskio.tag_type); 12496 break; 12497 default: 12498 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12499 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12500 break; 12501 } 12502 sbuf_cat(&sb, path_str); 12503 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12504 (intmax_t)time_uptime - io->io_hdr.start_time); 12505 sbuf_finish(&sb); 12506 printf("%s", sbuf_data(&sb)); 12507 } 12508#endif /* CTL_TIME_IO */ 12509 12510#ifdef CTL_IO_DELAY 12511 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12512 struct ctl_lun *lun; 12513 12514 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12515 12516 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12517 } else { 12518 struct ctl_lun *lun; 12519 12520 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12521 if ((lun != NULL) 12522 && (lun->delay_info.datamove_delay > 0)) { 12523 struct callout *callout; 12524 12525 callout = (struct callout *)&io->io_hdr.timer_bytes; 12526 callout_init(callout, /*mpsafe*/ 1); 12527 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12528 callout_reset(callout, 12529 lun->delay_info.datamove_delay * hz, 12530 ctl_datamove_timer_wakeup, io); 12531 if (lun->delay_info.datamove_type == 12532 CTL_DELAY_TYPE_ONESHOT) 12533 lun->delay_info.datamove_delay = 0; 12534 return; 12535 } 12536 } 12537#endif 12538 12539 /* 12540 * This command has been aborted. Set the port status, so we fail 12541 * the data move. 12542 */ 12543 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12544 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12545 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12546 io->io_hdr.nexus.targ_port, 12547 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12548 io->io_hdr.nexus.targ_lun); 12549 io->io_hdr.port_status = 31337; 12550 /* 12551 * Note that the backend, in this case, will get the 12552 * callback in its context. In other cases it may get 12553 * called in the frontend's interrupt thread context. 12554 */ 12555 io->scsiio.be_move_done(io); 12556 return; 12557 } 12558 12559 /* 12560 * If we're in XFER mode and this I/O is from the other shelf 12561 * controller, we need to send the DMA to the other side to 12562 * actually transfer the data to/from the host. In serialize only 12563 * mode the transfer happens below CTL and ctl_datamove() is only 12564 * called on the machine that originally received the I/O. 12565 */ 12566 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12567 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12568 union ctl_ha_msg msg; 12569 uint32_t sg_entries_sent; 12570 int do_sg_copy; 12571 int i; 12572 12573 memset(&msg, 0, sizeof(msg)); 12574 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12575 msg.hdr.original_sc = io->io_hdr.original_sc; 12576 msg.hdr.serializing_sc = io; 12577 msg.hdr.nexus = io->io_hdr.nexus; 12578 msg.dt.flags = io->io_hdr.flags; 12579 /* 12580 * We convert everything into a S/G list here. We can't 12581 * pass by reference, only by value between controllers. 12582 * So we can't pass a pointer to the S/G list, only as many 12583 * S/G entries as we can fit in here. If it's possible for 12584 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12585 * then we need to break this up into multiple transfers. 12586 */ 12587 if (io->scsiio.kern_sg_entries == 0) { 12588 msg.dt.kern_sg_entries = 1; 12589 /* 12590 * If this is in cached memory, flush the cache 12591 * before we send the DMA request to the other 12592 * controller. We want to do this in either the 12593 * read or the write case. The read case is 12594 * straightforward. In the write case, we want to 12595 * make sure nothing is in the local cache that 12596 * could overwrite the DMAed data. 12597 */ 12598 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12599 /* 12600 * XXX KDM use bus_dmamap_sync() here. 12601 */ 12602 } 12603 12604 /* 12605 * Convert to a physical address if this is a 12606 * virtual address. 12607 */ 12608 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12609 msg.dt.sg_list[0].addr = 12610 io->scsiio.kern_data_ptr; 12611 } else { 12612 /* 12613 * XXX KDM use busdma here! 12614 */ 12615#if 0 12616 msg.dt.sg_list[0].addr = (void *) 12617 vtophys(io->scsiio.kern_data_ptr); 12618#endif 12619 } 12620 12621 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12622 do_sg_copy = 0; 12623 } else { 12624 struct ctl_sg_entry *sgl; 12625 12626 do_sg_copy = 1; 12627 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12628 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12629 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12630 /* 12631 * XXX KDM use bus_dmamap_sync() here. 12632 */ 12633 } 12634 } 12635 12636 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12637 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12638 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12639 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12640 msg.dt.sg_sequence = 0; 12641 12642 /* 12643 * Loop until we've sent all of the S/G entries. On the 12644 * other end, we'll recompose these S/G entries into one 12645 * contiguous list before passing it to the 12646 */ 12647 for (sg_entries_sent = 0; sg_entries_sent < 12648 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12649 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12650 sizeof(msg.dt.sg_list[0])), 12651 msg.dt.kern_sg_entries - sg_entries_sent); 12652 12653 if (do_sg_copy != 0) { 12654 struct ctl_sg_entry *sgl; 12655 int j; 12656 12657 sgl = (struct ctl_sg_entry *) 12658 io->scsiio.kern_data_ptr; 12659 /* 12660 * If this is in cached memory, flush the cache 12661 * before we send the DMA request to the other 12662 * controller. We want to do this in either 12663 * the * read or the write case. The read 12664 * case is straightforward. In the write 12665 * case, we want to make sure nothing is 12666 * in the local cache that could overwrite 12667 * the DMAed data. 12668 */ 12669 12670 for (i = sg_entries_sent, j = 0; 12671 i < msg.dt.cur_sg_entries; i++, j++) { 12672 if ((io->io_hdr.flags & 12673 CTL_FLAG_NO_DATASYNC) == 0) { 12674 /* 12675 * XXX KDM use bus_dmamap_sync() 12676 */ 12677 } 12678 if ((io->io_hdr.flags & 12679 CTL_FLAG_BUS_ADDR) == 0) { 12680 /* 12681 * XXX KDM use busdma. 12682 */ 12683#if 0 12684 msg.dt.sg_list[j].addr =(void *) 12685 vtophys(sgl[i].addr); 12686#endif 12687 } else { 12688 msg.dt.sg_list[j].addr = 12689 sgl[i].addr; 12690 } 12691 msg.dt.sg_list[j].len = sgl[i].len; 12692 } 12693 } 12694 12695 sg_entries_sent += msg.dt.cur_sg_entries; 12696 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12697 msg.dt.sg_last = 1; 12698 else 12699 msg.dt.sg_last = 0; 12700 12701 /* 12702 * XXX KDM drop and reacquire the lock here? 12703 */ 12704 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12705 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12706 /* 12707 * XXX do something here. 12708 */ 12709 } 12710 12711 msg.dt.sent_sg_entries = sg_entries_sent; 12712 } 12713 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12714 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12715 ctl_failover_io(io, /*have_lock*/ 0); 12716 12717 } else { 12718 12719 /* 12720 * Lookup the fe_datamove() function for this particular 12721 * front end. 12722 */ 12723 fe_datamove = 12724 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12725 12726 fe_datamove(io); 12727 } 12728} 12729 12730static void 12731ctl_send_datamove_done(union ctl_io *io, int have_lock) 12732{ 12733 union ctl_ha_msg msg; 12734 int isc_status; 12735 12736 memset(&msg, 0, sizeof(msg)); 12737 12738 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12739 msg.hdr.original_sc = io; 12740 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12741 msg.hdr.nexus = io->io_hdr.nexus; 12742 msg.hdr.status = io->io_hdr.status; 12743 msg.scsi.tag_num = io->scsiio.tag_num; 12744 msg.scsi.tag_type = io->scsiio.tag_type; 12745 msg.scsi.scsi_status = io->scsiio.scsi_status; 12746 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12747 sizeof(io->scsiio.sense_data)); 12748 msg.scsi.sense_len = io->scsiio.sense_len; 12749 msg.scsi.sense_residual = io->scsiio.sense_residual; 12750 msg.scsi.fetd_status = io->io_hdr.port_status; 12751 msg.scsi.residual = io->scsiio.residual; 12752 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12753 12754 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12755 ctl_failover_io(io, /*have_lock*/ have_lock); 12756 return; 12757 } 12758 12759 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12760 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12761 /* XXX do something if this fails */ 12762 } 12763 12764} 12765 12766/* 12767 * The DMA to the remote side is done, now we need to tell the other side 12768 * we're done so it can continue with its data movement. 12769 */ 12770static void 12771ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12772{ 12773 union ctl_io *io; 12774 12775 io = rq->context; 12776 12777 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12778 printf("%s: ISC DMA write failed with error %d", __func__, 12779 rq->ret); 12780 ctl_set_internal_failure(&io->scsiio, 12781 /*sks_valid*/ 1, 12782 /*retry_count*/ rq->ret); 12783 } 12784 12785 ctl_dt_req_free(rq); 12786 12787 /* 12788 * In this case, we had to malloc the memory locally. Free it. 12789 */ 12790 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12791 int i; 12792 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12793 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12794 } 12795 /* 12796 * The data is in local and remote memory, so now we need to send 12797 * status (good or back) back to the other side. 12798 */ 12799 ctl_send_datamove_done(io, /*have_lock*/ 0); 12800} 12801 12802/* 12803 * We've moved the data from the host/controller into local memory. Now we 12804 * need to push it over to the remote controller's memory. 12805 */ 12806static int 12807ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12808{ 12809 int retval; 12810 12811 retval = 0; 12812 12813 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12814 ctl_datamove_remote_write_cb); 12815 12816 return (retval); 12817} 12818 12819static void 12820ctl_datamove_remote_write(union ctl_io *io) 12821{ 12822 int retval; 12823 void (*fe_datamove)(union ctl_io *io); 12824 12825 /* 12826 * - Get the data from the host/HBA into local memory. 12827 * - DMA memory from the local controller to the remote controller. 12828 * - Send status back to the remote controller. 12829 */ 12830 12831 retval = ctl_datamove_remote_sgl_setup(io); 12832 if (retval != 0) 12833 return; 12834 12835 /* Switch the pointer over so the FETD knows what to do */ 12836 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12837 12838 /* 12839 * Use a custom move done callback, since we need to send completion 12840 * back to the other controller, not to the backend on this side. 12841 */ 12842 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12843 12844 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12845 12846 fe_datamove(io); 12847 12848 return; 12849 12850} 12851 12852static int 12853ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12854{ 12855#if 0 12856 char str[256]; 12857 char path_str[64]; 12858 struct sbuf sb; 12859#endif 12860 12861 /* 12862 * In this case, we had to malloc the memory locally. Free it. 12863 */ 12864 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12865 int i; 12866 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12867 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12868 } 12869 12870#if 0 12871 scsi_path_string(io, path_str, sizeof(path_str)); 12872 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12873 sbuf_cat(&sb, path_str); 12874 scsi_command_string(&io->scsiio, NULL, &sb); 12875 sbuf_printf(&sb, "\n"); 12876 sbuf_cat(&sb, path_str); 12877 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12878 io->scsiio.tag_num, io->scsiio.tag_type); 12879 sbuf_cat(&sb, path_str); 12880 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12881 io->io_hdr.flags, io->io_hdr.status); 12882 sbuf_finish(&sb); 12883 printk("%s", sbuf_data(&sb)); 12884#endif 12885 12886 12887 /* 12888 * The read is done, now we need to send status (good or bad) back 12889 * to the other side. 12890 */ 12891 ctl_send_datamove_done(io, /*have_lock*/ 0); 12892 12893 return (0); 12894} 12895 12896static void 12897ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12898{ 12899 union ctl_io *io; 12900 void (*fe_datamove)(union ctl_io *io); 12901 12902 io = rq->context; 12903 12904 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12905 printf("%s: ISC DMA read failed with error %d", __func__, 12906 rq->ret); 12907 ctl_set_internal_failure(&io->scsiio, 12908 /*sks_valid*/ 1, 12909 /*retry_count*/ rq->ret); 12910 } 12911 12912 ctl_dt_req_free(rq); 12913 12914 /* Switch the pointer over so the FETD knows what to do */ 12915 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12916 12917 /* 12918 * Use a custom move done callback, since we need to send completion 12919 * back to the other controller, not to the backend on this side. 12920 */ 12921 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12922 12923 /* XXX KDM add checks like the ones in ctl_datamove? */ 12924 12925 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12926 12927 fe_datamove(io); 12928} 12929 12930static int 12931ctl_datamove_remote_sgl_setup(union ctl_io *io) 12932{ 12933 struct ctl_sg_entry *local_sglist, *remote_sglist; 12934 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12935 struct ctl_softc *softc; 12936 int retval; 12937 int i; 12938 12939 retval = 0; 12940 softc = control_softc; 12941 12942 local_sglist = io->io_hdr.local_sglist; 12943 local_dma_sglist = io->io_hdr.local_dma_sglist; 12944 remote_sglist = io->io_hdr.remote_sglist; 12945 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12946 12947 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12948 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12949 local_sglist[i].len = remote_sglist[i].len; 12950 12951 /* 12952 * XXX Detect the situation where the RS-level I/O 12953 * redirector on the other side has already read the 12954 * data off of the AOR RS on this side, and 12955 * transferred it to remote (mirror) memory on the 12956 * other side. Since we already have the data in 12957 * memory here, we just need to use it. 12958 * 12959 * XXX KDM this can probably be removed once we 12960 * get the cache device code in and take the 12961 * current AOR implementation out. 12962 */ 12963#ifdef NEEDTOPORT 12964 if ((remote_sglist[i].addr >= 12965 (void *)vtophys(softc->mirr->addr)) 12966 && (remote_sglist[i].addr < 12967 ((void *)vtophys(softc->mirr->addr) + 12968 CacheMirrorOffset))) { 12969 local_sglist[i].addr = remote_sglist[i].addr - 12970 CacheMirrorOffset; 12971 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12972 CTL_FLAG_DATA_IN) 12973 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12974 } else { 12975 local_sglist[i].addr = remote_sglist[i].addr + 12976 CacheMirrorOffset; 12977 } 12978#endif 12979#if 0 12980 printf("%s: local %p, remote %p, len %d\n", 12981 __func__, local_sglist[i].addr, 12982 remote_sglist[i].addr, local_sglist[i].len); 12983#endif 12984 } 12985 } else { 12986 uint32_t len_to_go; 12987 12988 /* 12989 * In this case, we don't have automatically allocated 12990 * memory for this I/O on this controller. This typically 12991 * happens with internal CTL I/O -- e.g. inquiry, mode 12992 * sense, etc. Anything coming from RAIDCore will have 12993 * a mirror area available. 12994 */ 12995 len_to_go = io->scsiio.kern_data_len; 12996 12997 /* 12998 * Clear the no datasync flag, we have to use malloced 12999 * buffers. 13000 */ 13001 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13002 13003 /* 13004 * The difficult thing here is that the size of the various 13005 * S/G segments may be different than the size from the 13006 * remote controller. That'll make it harder when DMAing 13007 * the data back to the other side. 13008 */ 13009 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13010 sizeof(io->io_hdr.remote_sglist[0])) && 13011 (len_to_go > 0); i++) { 13012 local_sglist[i].len = ctl_min(len_to_go, 131072); 13013 CTL_SIZE_8B(local_dma_sglist[i].len, 13014 local_sglist[i].len); 13015 local_sglist[i].addr = 13016 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13017 13018 local_dma_sglist[i].addr = local_sglist[i].addr; 13019 13020 if (local_sglist[i].addr == NULL) { 13021 int j; 13022 13023 printf("malloc failed for %zd bytes!", 13024 local_dma_sglist[i].len); 13025 for (j = 0; j < i; j++) { 13026 free(local_sglist[j].addr, M_CTL); 13027 } 13028 ctl_set_internal_failure(&io->scsiio, 13029 /*sks_valid*/ 1, 13030 /*retry_count*/ 4857); 13031 retval = 1; 13032 goto bailout_error; 13033 13034 } 13035 /* XXX KDM do we need a sync here? */ 13036 13037 len_to_go -= local_sglist[i].len; 13038 } 13039 /* 13040 * Reset the number of S/G entries accordingly. The 13041 * original number of S/G entries is available in 13042 * rem_sg_entries. 13043 */ 13044 io->scsiio.kern_sg_entries = i; 13045 13046#if 0 13047 printf("%s: kern_sg_entries = %d\n", __func__, 13048 io->scsiio.kern_sg_entries); 13049 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13050 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13051 local_sglist[i].addr, local_sglist[i].len, 13052 local_dma_sglist[i].len); 13053#endif 13054 } 13055 13056 13057 return (retval); 13058 13059bailout_error: 13060 13061 ctl_send_datamove_done(io, /*have_lock*/ 0); 13062 13063 return (retval); 13064} 13065 13066static int 13067ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13068 ctl_ha_dt_cb callback) 13069{ 13070 struct ctl_ha_dt_req *rq; 13071 struct ctl_sg_entry *remote_sglist, *local_sglist; 13072 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13073 uint32_t local_used, remote_used, total_used; 13074 int retval; 13075 int i, j; 13076 13077 retval = 0; 13078 13079 rq = ctl_dt_req_alloc(); 13080 13081 /* 13082 * If we failed to allocate the request, and if the DMA didn't fail 13083 * anyway, set busy status. This is just a resource allocation 13084 * failure. 13085 */ 13086 if ((rq == NULL) 13087 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13088 ctl_set_busy(&io->scsiio); 13089 13090 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13091 13092 if (rq != NULL) 13093 ctl_dt_req_free(rq); 13094 13095 /* 13096 * The data move failed. We need to return status back 13097 * to the other controller. No point in trying to DMA 13098 * data to the remote controller. 13099 */ 13100 13101 ctl_send_datamove_done(io, /*have_lock*/ 0); 13102 13103 retval = 1; 13104 13105 goto bailout; 13106 } 13107 13108 local_sglist = io->io_hdr.local_sglist; 13109 local_dma_sglist = io->io_hdr.local_dma_sglist; 13110 remote_sglist = io->io_hdr.remote_sglist; 13111 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13112 local_used = 0; 13113 remote_used = 0; 13114 total_used = 0; 13115 13116 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13117 rq->ret = CTL_HA_STATUS_SUCCESS; 13118 rq->context = io; 13119 callback(rq); 13120 goto bailout; 13121 } 13122 13123 /* 13124 * Pull/push the data over the wire from/to the other controller. 13125 * This takes into account the possibility that the local and 13126 * remote sglists may not be identical in terms of the size of 13127 * the elements and the number of elements. 13128 * 13129 * One fundamental assumption here is that the length allocated for 13130 * both the local and remote sglists is identical. Otherwise, we've 13131 * essentially got a coding error of some sort. 13132 */ 13133 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13134 int isc_ret; 13135 uint32_t cur_len, dma_length; 13136 uint8_t *tmp_ptr; 13137 13138 rq->id = CTL_HA_DATA_CTL; 13139 rq->command = command; 13140 rq->context = io; 13141 13142 /* 13143 * Both pointers should be aligned. But it is possible 13144 * that the allocation length is not. They should both 13145 * also have enough slack left over at the end, though, 13146 * to round up to the next 8 byte boundary. 13147 */ 13148 cur_len = ctl_min(local_sglist[i].len - local_used, 13149 remote_sglist[j].len - remote_used); 13150 13151 /* 13152 * In this case, we have a size issue and need to decrease 13153 * the size, except in the case where we actually have less 13154 * than 8 bytes left. In that case, we need to increase 13155 * the DMA length to get the last bit. 13156 */ 13157 if ((cur_len & 0x7) != 0) { 13158 if (cur_len > 0x7) { 13159 cur_len = cur_len - (cur_len & 0x7); 13160 dma_length = cur_len; 13161 } else { 13162 CTL_SIZE_8B(dma_length, cur_len); 13163 } 13164 13165 } else 13166 dma_length = cur_len; 13167 13168 /* 13169 * If we had to allocate memory for this I/O, instead of using 13170 * the non-cached mirror memory, we'll need to flush the cache 13171 * before trying to DMA to the other controller. 13172 * 13173 * We could end up doing this multiple times for the same 13174 * segment if we have a larger local segment than remote 13175 * segment. That shouldn't be an issue. 13176 */ 13177 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13178 /* 13179 * XXX KDM use bus_dmamap_sync() here. 13180 */ 13181 } 13182 13183 rq->size = dma_length; 13184 13185 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13186 tmp_ptr += local_used; 13187 13188 /* Use physical addresses when talking to ISC hardware */ 13189 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13190 /* XXX KDM use busdma */ 13191#if 0 13192 rq->local = vtophys(tmp_ptr); 13193#endif 13194 } else 13195 rq->local = tmp_ptr; 13196 13197 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13198 tmp_ptr += remote_used; 13199 rq->remote = tmp_ptr; 13200 13201 rq->callback = NULL; 13202 13203 local_used += cur_len; 13204 if (local_used >= local_sglist[i].len) { 13205 i++; 13206 local_used = 0; 13207 } 13208 13209 remote_used += cur_len; 13210 if (remote_used >= remote_sglist[j].len) { 13211 j++; 13212 remote_used = 0; 13213 } 13214 total_used += cur_len; 13215 13216 if (total_used >= io->scsiio.kern_data_len) 13217 rq->callback = callback; 13218 13219 if ((rq->size & 0x7) != 0) { 13220 printf("%s: warning: size %d is not on 8b boundary\n", 13221 __func__, rq->size); 13222 } 13223 if (((uintptr_t)rq->local & 0x7) != 0) { 13224 printf("%s: warning: local %p not on 8b boundary\n", 13225 __func__, rq->local); 13226 } 13227 if (((uintptr_t)rq->remote & 0x7) != 0) { 13228 printf("%s: warning: remote %p not on 8b boundary\n", 13229 __func__, rq->local); 13230 } 13231#if 0 13232 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13233 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13234 rq->local, rq->remote, rq->size); 13235#endif 13236 13237 isc_ret = ctl_dt_single(rq); 13238 if (isc_ret == CTL_HA_STATUS_WAIT) 13239 continue; 13240 13241 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13242 rq->ret = CTL_HA_STATUS_SUCCESS; 13243 } else { 13244 rq->ret = isc_ret; 13245 } 13246 callback(rq); 13247 goto bailout; 13248 } 13249 13250bailout: 13251 return (retval); 13252 13253} 13254 13255static void 13256ctl_datamove_remote_read(union ctl_io *io) 13257{ 13258 int retval; 13259 int i; 13260 13261 /* 13262 * This will send an error to the other controller in the case of a 13263 * failure. 13264 */ 13265 retval = ctl_datamove_remote_sgl_setup(io); 13266 if (retval != 0) 13267 return; 13268 13269 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13270 ctl_datamove_remote_read_cb); 13271 if ((retval != 0) 13272 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13273 /* 13274 * Make sure we free memory if there was an error.. The 13275 * ctl_datamove_remote_xfer() function will send the 13276 * datamove done message, or call the callback with an 13277 * error if there is a problem. 13278 */ 13279 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13280 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13281 } 13282 13283 return; 13284} 13285 13286/* 13287 * Process a datamove request from the other controller. This is used for 13288 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13289 * first. Once that is complete, the data gets DMAed into the remote 13290 * controller's memory. For reads, we DMA from the remote controller's 13291 * memory into our memory first, and then move it out to the FETD. 13292 */ 13293static void 13294ctl_datamove_remote(union ctl_io *io) 13295{ 13296 struct ctl_softc *softc; 13297 13298 softc = control_softc; 13299 13300 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13301 13302 /* 13303 * Note that we look for an aborted I/O here, but don't do some of 13304 * the other checks that ctl_datamove() normally does. 13305 * We don't need to run the datamove delay code, since that should 13306 * have been done if need be on the other controller. 13307 */ 13308 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13309 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13310 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13311 io->io_hdr.nexus.targ_port, 13312 io->io_hdr.nexus.targ_target.id, 13313 io->io_hdr.nexus.targ_lun); 13314 io->io_hdr.port_status = 31338; 13315 ctl_send_datamove_done(io, /*have_lock*/ 0); 13316 return; 13317 } 13318 13319 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13320 ctl_datamove_remote_write(io); 13321 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13322 ctl_datamove_remote_read(io); 13323 } else { 13324 union ctl_ha_msg msg; 13325 struct scsi_sense_data *sense; 13326 uint8_t sks[3]; 13327 int retry_count; 13328 13329 memset(&msg, 0, sizeof(msg)); 13330 13331 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13332 msg.hdr.status = CTL_SCSI_ERROR; 13333 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13334 13335 retry_count = 4243; 13336 13337 sense = &msg.scsi.sense_data; 13338 sks[0] = SSD_SCS_VALID; 13339 sks[1] = (retry_count >> 8) & 0xff; 13340 sks[2] = retry_count & 0xff; 13341 13342 /* "Internal target failure" */ 13343 scsi_set_sense_data(sense, 13344 /*sense_format*/ SSD_TYPE_NONE, 13345 /*current_error*/ 1, 13346 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13347 /*asc*/ 0x44, 13348 /*ascq*/ 0x00, 13349 /*type*/ SSD_ELEM_SKS, 13350 /*size*/ sizeof(sks), 13351 /*data*/ sks, 13352 SSD_ELEM_NONE); 13353 13354 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13355 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13356 ctl_failover_io(io, /*have_lock*/ 1); 13357 return; 13358 } 13359 13360 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13361 CTL_HA_STATUS_SUCCESS) { 13362 /* XXX KDM what to do if this fails? */ 13363 } 13364 return; 13365 } 13366 13367} 13368 13369static int 13370ctl_process_done(union ctl_io *io) 13371{ 13372 struct ctl_lun *lun; 13373 struct ctl_softc *ctl_softc; 13374 void (*fe_done)(union ctl_io *io); 13375 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13376 13377 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13378 13379 fe_done = 13380 control_softc->ctl_ports[targ_port]->fe_done; 13381 13382#ifdef CTL_TIME_IO 13383 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13384 char str[256]; 13385 char path_str[64]; 13386 struct sbuf sb; 13387 13388 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13389 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13390 13391 sbuf_cat(&sb, path_str); 13392 switch (io->io_hdr.io_type) { 13393 case CTL_IO_SCSI: 13394 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13395 sbuf_printf(&sb, "\n"); 13396 sbuf_cat(&sb, path_str); 13397 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13398 io->scsiio.tag_num, io->scsiio.tag_type); 13399 break; 13400 case CTL_IO_TASK: 13401 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13402 "Tag Type: %d\n", io->taskio.task_action, 13403 io->taskio.tag_num, io->taskio.tag_type); 13404 break; 13405 default: 13406 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13407 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13408 break; 13409 } 13410 sbuf_cat(&sb, path_str); 13411 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13412 (intmax_t)time_uptime - io->io_hdr.start_time); 13413 sbuf_finish(&sb); 13414 printf("%s", sbuf_data(&sb)); 13415 } 13416#endif /* CTL_TIME_IO */ 13417 13418 switch (io->io_hdr.io_type) { 13419 case CTL_IO_SCSI: 13420 break; 13421 case CTL_IO_TASK: 13422 if (bootverbose || verbose > 0) 13423 ctl_io_error_print(io, NULL); 13424 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13425 ctl_free_io(io); 13426 else 13427 fe_done(io); 13428 return (CTL_RETVAL_COMPLETE); 13429 break; 13430 default: 13431 printf("ctl_process_done: invalid io type %d\n", 13432 io->io_hdr.io_type); 13433 panic("ctl_process_done: invalid io type %d\n", 13434 io->io_hdr.io_type); 13435 break; /* NOTREACHED */ 13436 } 13437 13438 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13439 if (lun == NULL) { 13440 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13441 io->io_hdr.nexus.targ_mapped_lun)); 13442 fe_done(io); 13443 goto bailout; 13444 } 13445 ctl_softc = lun->ctl_softc; 13446 13447 mtx_lock(&lun->lun_lock); 13448 13449 /* 13450 * Check to see if we have any errors to inject here. We only 13451 * inject errors for commands that don't already have errors set. 13452 */ 13453 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13454 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13455 ctl_inject_error(lun, io); 13456 13457 /* 13458 * XXX KDM how do we treat commands that aren't completed 13459 * successfully? 13460 * 13461 * XXX KDM should we also track I/O latency? 13462 */ 13463 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13464 io->io_hdr.io_type == CTL_IO_SCSI) { 13465#ifdef CTL_TIME_IO 13466 struct bintime cur_bt; 13467#endif 13468 int type; 13469 13470 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13471 CTL_FLAG_DATA_IN) 13472 type = CTL_STATS_READ; 13473 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13474 CTL_FLAG_DATA_OUT) 13475 type = CTL_STATS_WRITE; 13476 else 13477 type = CTL_STATS_NO_IO; 13478 13479 lun->stats.ports[targ_port].bytes[type] += 13480 io->scsiio.kern_total_len; 13481 lun->stats.ports[targ_port].operations[type]++; 13482#ifdef CTL_TIME_IO 13483 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13484 &io->io_hdr.dma_bt); 13485 lun->stats.ports[targ_port].num_dmas[type] += 13486 io->io_hdr.num_dmas; 13487 getbintime(&cur_bt); 13488 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13489 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13490#endif 13491 } 13492 13493 /* 13494 * Remove this from the OOA queue. 13495 */ 13496 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13497 13498 /* 13499 * Run through the blocked queue on this LUN and see if anything 13500 * has become unblocked, now that this transaction is done. 13501 */ 13502 ctl_check_blocked(lun); 13503 13504 /* 13505 * If the LUN has been invalidated, free it if there is nothing 13506 * left on its OOA queue. 13507 */ 13508 if ((lun->flags & CTL_LUN_INVALID) 13509 && TAILQ_EMPTY(&lun->ooa_queue)) { 13510 mtx_unlock(&lun->lun_lock); 13511 mtx_lock(&ctl_softc->ctl_lock); 13512 ctl_free_lun(lun); 13513 mtx_unlock(&ctl_softc->ctl_lock); 13514 } else 13515 mtx_unlock(&lun->lun_lock); 13516 13517 /* 13518 * If this command has been aborted, make sure we set the status 13519 * properly. The FETD is responsible for freeing the I/O and doing 13520 * whatever it needs to do to clean up its state. 13521 */ 13522 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13523 ctl_set_task_aborted(&io->scsiio); 13524 13525 /* 13526 * We print out status for every task management command. For SCSI 13527 * commands, we filter out any unit attention errors; they happen 13528 * on every boot, and would clutter up the log. Note: task 13529 * management commands aren't printed here, they are printed above, 13530 * since they should never even make it down here. 13531 */ 13532 switch (io->io_hdr.io_type) { 13533 case CTL_IO_SCSI: { 13534 int error_code, sense_key, asc, ascq; 13535 13536 sense_key = 0; 13537 13538 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13539 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13540 /* 13541 * Since this is just for printing, no need to 13542 * show errors here. 13543 */ 13544 scsi_extract_sense_len(&io->scsiio.sense_data, 13545 io->scsiio.sense_len, 13546 &error_code, 13547 &sense_key, 13548 &asc, 13549 &ascq, 13550 /*show_errors*/ 0); 13551 } 13552 13553 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13554 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13555 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13556 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13557 13558 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13559 ctl_softc->skipped_prints++; 13560 } else { 13561 uint32_t skipped_prints; 13562 13563 skipped_prints = ctl_softc->skipped_prints; 13564 13565 ctl_softc->skipped_prints = 0; 13566 ctl_softc->last_print_jiffies = time_uptime; 13567 13568 if (skipped_prints > 0) { 13569#ifdef NEEDTOPORT 13570 csevent_log(CSC_CTL | CSC_SHELF_SW | 13571 CTL_ERROR_REPORT, 13572 csevent_LogType_Trace, 13573 csevent_Severity_Information, 13574 csevent_AlertLevel_Green, 13575 csevent_FRU_Firmware, 13576 csevent_FRU_Unknown, 13577 "High CTL error volume, %d prints " 13578 "skipped", skipped_prints); 13579#endif 13580 } 13581 if (bootverbose || verbose > 0) 13582 ctl_io_error_print(io, NULL); 13583 } 13584 } 13585 break; 13586 } 13587 case CTL_IO_TASK: 13588 if (bootverbose || verbose > 0) 13589 ctl_io_error_print(io, NULL); 13590 break; 13591 default: 13592 break; 13593 } 13594 13595 /* 13596 * Tell the FETD or the other shelf controller we're done with this 13597 * command. Note that only SCSI commands get to this point. Task 13598 * management commands are completed above. 13599 * 13600 * We only send status to the other controller if we're in XFER 13601 * mode. In SER_ONLY mode, the I/O is done on the controller that 13602 * received the I/O (from CTL's perspective), and so the status is 13603 * generated there. 13604 * 13605 * XXX KDM if we hold the lock here, we could cause a deadlock 13606 * if the frontend comes back in in this context to queue 13607 * something. 13608 */ 13609 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13610 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13611 union ctl_ha_msg msg; 13612 13613 memset(&msg, 0, sizeof(msg)); 13614 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13615 msg.hdr.original_sc = io->io_hdr.original_sc; 13616 msg.hdr.nexus = io->io_hdr.nexus; 13617 msg.hdr.status = io->io_hdr.status; 13618 msg.scsi.scsi_status = io->scsiio.scsi_status; 13619 msg.scsi.tag_num = io->scsiio.tag_num; 13620 msg.scsi.tag_type = io->scsiio.tag_type; 13621 msg.scsi.sense_len = io->scsiio.sense_len; 13622 msg.scsi.sense_residual = io->scsiio.sense_residual; 13623 msg.scsi.residual = io->scsiio.residual; 13624 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13625 sizeof(io->scsiio.sense_data)); 13626 /* 13627 * We copy this whether or not this is an I/O-related 13628 * command. Otherwise, we'd have to go and check to see 13629 * whether it's a read/write command, and it really isn't 13630 * worth it. 13631 */ 13632 memcpy(&msg.scsi.lbalen, 13633 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13634 sizeof(msg.scsi.lbalen)); 13635 13636 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13637 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13638 /* XXX do something here */ 13639 } 13640 13641 ctl_free_io(io); 13642 } else 13643 fe_done(io); 13644 13645bailout: 13646 13647 return (CTL_RETVAL_COMPLETE); 13648} 13649 13650/* 13651 * Front end should call this if it doesn't do autosense. When the request 13652 * sense comes back in from the initiator, we'll dequeue this and send it. 13653 */ 13654int 13655ctl_queue_sense(union ctl_io *io) 13656{ 13657 struct ctl_lun *lun; 13658 struct ctl_softc *ctl_softc; 13659 uint32_t initidx, targ_lun; 13660 13661 ctl_softc = control_softc; 13662 13663 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13664 13665 /* 13666 * LUN lookup will likely move to the ctl_work_thread() once we 13667 * have our new queueing infrastructure (that doesn't put things on 13668 * a per-LUN queue initially). That is so that we can handle 13669 * things like an INQUIRY to a LUN that we don't have enabled. We 13670 * can't deal with that right now. 13671 */ 13672 mtx_lock(&ctl_softc->ctl_lock); 13673 13674 /* 13675 * If we don't have a LUN for this, just toss the sense 13676 * information. 13677 */ 13678 targ_lun = io->io_hdr.nexus.targ_lun; 13679 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13680 if ((targ_lun < CTL_MAX_LUNS) 13681 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13682 lun = ctl_softc->ctl_luns[targ_lun]; 13683 else 13684 goto bailout; 13685 13686 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13687 13688 mtx_lock(&lun->lun_lock); 13689 /* 13690 * Already have CA set for this LUN...toss the sense information. 13691 */ 13692 if (ctl_is_set(lun->have_ca, initidx)) { 13693 mtx_unlock(&lun->lun_lock); 13694 goto bailout; 13695 } 13696 13697 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data, 13698 ctl_min(sizeof(lun->pending_sense[initidx].sense), 13699 sizeof(io->scsiio.sense_data))); 13700 ctl_set_mask(lun->have_ca, initidx); 13701 mtx_unlock(&lun->lun_lock); 13702 13703bailout: 13704 mtx_unlock(&ctl_softc->ctl_lock); 13705 13706 ctl_free_io(io); 13707 13708 return (CTL_RETVAL_COMPLETE); 13709} 13710 13711/* 13712 * Primary command inlet from frontend ports. All SCSI and task I/O 13713 * requests must go through this function. 13714 */ 13715int 13716ctl_queue(union ctl_io *io) 13717{ 13718 struct ctl_softc *ctl_softc; 13719 13720 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13721 13722 ctl_softc = control_softc; 13723 13724#ifdef CTL_TIME_IO 13725 io->io_hdr.start_time = time_uptime; 13726 getbintime(&io->io_hdr.start_bt); 13727#endif /* CTL_TIME_IO */ 13728 13729 /* Map FE-specific LUN ID into global one. */ 13730 io->io_hdr.nexus.targ_mapped_lun = 13731 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13732 13733 switch (io->io_hdr.io_type) { 13734 case CTL_IO_SCSI: 13735 case CTL_IO_TASK: 13736 ctl_enqueue_incoming(io); 13737 break; 13738 default: 13739 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13740 return (EINVAL); 13741 } 13742 13743 return (CTL_RETVAL_COMPLETE); 13744} 13745 13746#ifdef CTL_IO_DELAY 13747static void 13748ctl_done_timer_wakeup(void *arg) 13749{ 13750 union ctl_io *io; 13751 13752 io = (union ctl_io *)arg; 13753 ctl_done(io); 13754} 13755#endif /* CTL_IO_DELAY */ 13756 13757void 13758ctl_done(union ctl_io *io) 13759{ 13760 struct ctl_softc *ctl_softc; 13761 13762 ctl_softc = control_softc; 13763 13764 /* 13765 * Enable this to catch duplicate completion issues. 13766 */ 13767#if 0 13768 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13769 printf("%s: type %d msg %d cdb %x iptl: " 13770 "%d:%d:%d:%d tag 0x%04x " 13771 "flag %#x status %x\n", 13772 __func__, 13773 io->io_hdr.io_type, 13774 io->io_hdr.msg_type, 13775 io->scsiio.cdb[0], 13776 io->io_hdr.nexus.initid.id, 13777 io->io_hdr.nexus.targ_port, 13778 io->io_hdr.nexus.targ_target.id, 13779 io->io_hdr.nexus.targ_lun, 13780 (io->io_hdr.io_type == 13781 CTL_IO_TASK) ? 13782 io->taskio.tag_num : 13783 io->scsiio.tag_num, 13784 io->io_hdr.flags, 13785 io->io_hdr.status); 13786 } else 13787 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13788#endif 13789 13790 /* 13791 * This is an internal copy of an I/O, and should not go through 13792 * the normal done processing logic. 13793 */ 13794 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13795 return; 13796 13797 /* 13798 * We need to send a msg to the serializing shelf to finish the IO 13799 * as well. We don't send a finish message to the other shelf if 13800 * this is a task management command. Task management commands 13801 * aren't serialized in the OOA queue, but rather just executed on 13802 * both shelf controllers for commands that originated on that 13803 * controller. 13804 */ 13805 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13806 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13807 union ctl_ha_msg msg_io; 13808 13809 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13810 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13811 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13812 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13813 } 13814 /* continue on to finish IO */ 13815 } 13816#ifdef CTL_IO_DELAY 13817 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13818 struct ctl_lun *lun; 13819 13820 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13821 13822 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13823 } else { 13824 struct ctl_lun *lun; 13825 13826 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13827 13828 if ((lun != NULL) 13829 && (lun->delay_info.done_delay > 0)) { 13830 struct callout *callout; 13831 13832 callout = (struct callout *)&io->io_hdr.timer_bytes; 13833 callout_init(callout, /*mpsafe*/ 1); 13834 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13835 callout_reset(callout, 13836 lun->delay_info.done_delay * hz, 13837 ctl_done_timer_wakeup, io); 13838 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13839 lun->delay_info.done_delay = 0; 13840 return; 13841 } 13842 } 13843#endif /* CTL_IO_DELAY */ 13844 13845 ctl_enqueue_done(io); 13846} 13847 13848int 13849ctl_isc(struct ctl_scsiio *ctsio) 13850{ 13851 struct ctl_lun *lun; 13852 int retval; 13853 13854 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13855 13856 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13857 13858 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13859 13860 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13861 13862 return (retval); 13863} 13864 13865 13866static void 13867ctl_work_thread(void *arg) 13868{ 13869 struct ctl_thread *thr = (struct ctl_thread *)arg; 13870 struct ctl_softc *softc = thr->ctl_softc; 13871 union ctl_io *io; 13872 int retval; 13873 13874 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13875 13876 for (;;) { 13877 retval = 0; 13878 13879 /* 13880 * We handle the queues in this order: 13881 * - ISC 13882 * - done queue (to free up resources, unblock other commands) 13883 * - RtR queue 13884 * - incoming queue 13885 * 13886 * If those queues are empty, we break out of the loop and 13887 * go to sleep. 13888 */ 13889 mtx_lock(&thr->queue_lock); 13890 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13891 if (io != NULL) { 13892 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13893 mtx_unlock(&thr->queue_lock); 13894 ctl_handle_isc(io); 13895 continue; 13896 } 13897 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13898 if (io != NULL) { 13899 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13900 /* clear any blocked commands, call fe_done */ 13901 mtx_unlock(&thr->queue_lock); 13902 retval = ctl_process_done(io); 13903 continue; 13904 } 13905 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13906 if (io != NULL) { 13907 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13908 mtx_unlock(&thr->queue_lock); 13909 if (io->io_hdr.io_type == CTL_IO_TASK) 13910 ctl_run_task(io); 13911 else 13912 ctl_scsiio_precheck(softc, &io->scsiio); 13913 continue; 13914 } 13915 if (!ctl_pause_rtr) { 13916 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13917 if (io != NULL) { 13918 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13919 mtx_unlock(&thr->queue_lock); 13920 retval = ctl_scsiio(&io->scsiio); 13921 if (retval != CTL_RETVAL_COMPLETE) 13922 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13923 continue; 13924 } 13925 } 13926 13927 /* Sleep until we have something to do. */ 13928 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13929 } 13930} 13931 13932static void 13933ctl_lun_thread(void *arg) 13934{ 13935 struct ctl_softc *softc = (struct ctl_softc *)arg; 13936 struct ctl_be_lun *be_lun; 13937 int retval; 13938 13939 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13940 13941 for (;;) { 13942 retval = 0; 13943 mtx_lock(&softc->ctl_lock); 13944 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13945 if (be_lun != NULL) { 13946 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13947 mtx_unlock(&softc->ctl_lock); 13948 ctl_create_lun(be_lun); 13949 continue; 13950 } 13951 13952 /* Sleep until we have something to do. */ 13953 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13954 PDROP | PRIBIO, "-", 0); 13955 } 13956} 13957 13958static void 13959ctl_enqueue_incoming(union ctl_io *io) 13960{ 13961 struct ctl_softc *softc = control_softc; 13962 struct ctl_thread *thr; 13963 u_int idx; 13964 13965 idx = (io->io_hdr.nexus.targ_port * 127 + 13966 io->io_hdr.nexus.initid.id) % worker_threads; 13967 thr = &softc->threads[idx]; 13968 mtx_lock(&thr->queue_lock); 13969 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13970 mtx_unlock(&thr->queue_lock); 13971 wakeup(thr); 13972} 13973 13974static void 13975ctl_enqueue_rtr(union ctl_io *io) 13976{ 13977 struct ctl_softc *softc = control_softc; 13978 struct ctl_thread *thr; 13979 13980 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13981 mtx_lock(&thr->queue_lock); 13982 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13983 mtx_unlock(&thr->queue_lock); 13984 wakeup(thr); 13985} 13986 13987static void 13988ctl_enqueue_done(union ctl_io *io) 13989{ 13990 struct ctl_softc *softc = control_softc; 13991 struct ctl_thread *thr; 13992 13993 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13994 mtx_lock(&thr->queue_lock); 13995 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13996 mtx_unlock(&thr->queue_lock); 13997 wakeup(thr); 13998} 13999 14000static void 14001ctl_enqueue_isc(union ctl_io *io) 14002{ 14003 struct ctl_softc *softc = control_softc; 14004 struct ctl_thread *thr; 14005 14006 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14007 mtx_lock(&thr->queue_lock); 14008 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14009 mtx_unlock(&thr->queue_lock); 14010 wakeup(thr); 14011} 14012 14013/* Initialization and failover */ 14014 14015void 14016ctl_init_isc_msg(void) 14017{ 14018 printf("CTL: Still calling this thing\n"); 14019} 14020 14021/* 14022 * Init component 14023 * Initializes component into configuration defined by bootMode 14024 * (see hasc-sv.c) 14025 * returns hasc_Status: 14026 * OK 14027 * ERROR - fatal error 14028 */ 14029static ctl_ha_comp_status 14030ctl_isc_init(struct ctl_ha_component *c) 14031{ 14032 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14033 14034 c->status = ret; 14035 return ret; 14036} 14037 14038/* Start component 14039 * Starts component in state requested. If component starts successfully, 14040 * it must set its own state to the requestrd state 14041 * When requested state is HASC_STATE_HA, the component may refine it 14042 * by adding _SLAVE or _MASTER flags. 14043 * Currently allowed state transitions are: 14044 * UNKNOWN->HA - initial startup 14045 * UNKNOWN->SINGLE - initial startup when no parter detected 14046 * HA->SINGLE - failover 14047 * returns ctl_ha_comp_status: 14048 * OK - component successfully started in requested state 14049 * FAILED - could not start the requested state, failover may 14050 * be possible 14051 * ERROR - fatal error detected, no future startup possible 14052 */ 14053static ctl_ha_comp_status 14054ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14055{ 14056 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14057 14058 printf("%s: go\n", __func__); 14059 14060 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14061 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14062 ctl_is_single = 0; 14063 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14064 != CTL_HA_STATUS_SUCCESS) { 14065 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14066 ret = CTL_HA_COMP_STATUS_ERROR; 14067 } 14068 } else if (CTL_HA_STATE_IS_HA(c->state) 14069 && CTL_HA_STATE_IS_SINGLE(state)){ 14070 // HA->SINGLE transition 14071 ctl_failover(); 14072 ctl_is_single = 1; 14073 } else { 14074 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14075 c->state, state); 14076 ret = CTL_HA_COMP_STATUS_ERROR; 14077 } 14078 if (CTL_HA_STATE_IS_SINGLE(state)) 14079 ctl_is_single = 1; 14080 14081 c->state = state; 14082 c->status = ret; 14083 return ret; 14084} 14085 14086/* 14087 * Quiesce component 14088 * The component must clear any error conditions (set status to OK) and 14089 * prepare itself to another Start call 14090 * returns ctl_ha_comp_status: 14091 * OK 14092 * ERROR 14093 */ 14094static ctl_ha_comp_status 14095ctl_isc_quiesce(struct ctl_ha_component *c) 14096{ 14097 int ret = CTL_HA_COMP_STATUS_OK; 14098 14099 ctl_pause_rtr = 1; 14100 c->status = ret; 14101 return ret; 14102} 14103 14104struct ctl_ha_component ctl_ha_component_ctlisc = 14105{ 14106 .name = "CTL ISC", 14107 .state = CTL_HA_STATE_UNKNOWN, 14108 .init = ctl_isc_init, 14109 .start = ctl_isc_start, 14110 .quiesce = ctl_isc_quiesce 14111}; 14112 14113/* 14114 * vim: ts=8 14115 */ 14116