ctl.c revision 249009
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 249009 2013-04-02 09:42:42Z trasz $"); 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/endian.h> 64#include <sys/sysctl.h> 65 66#include <cam/cam.h> 67#include <cam/scsi/scsi_all.h> 68#include <cam/scsi/scsi_da.h> 69#include <cam/ctl/ctl_io.h> 70#include <cam/ctl/ctl.h> 71#include <cam/ctl/ctl_frontend.h> 72#include <cam/ctl/ctl_frontend_internal.h> 73#include <cam/ctl/ctl_util.h> 74#include <cam/ctl/ctl_backend.h> 75#include <cam/ctl/ctl_ioctl.h> 76#include <cam/ctl/ctl_ha.h> 77#include <cam/ctl/ctl_private.h> 78#include <cam/ctl/ctl_debug.h> 79#include <cam/ctl/ctl_scsi_all.h> 80#include <cam/ctl/ctl_error.h> 81 82#include "opt_ctl.h" 83 84struct ctl_softc *control_softc = NULL; 85 86/* 87 * The default is to run with CTL_DONE_THREAD turned on. Completed 88 * transactions are queued for processing by the CTL work thread. When 89 * CTL_DONE_THREAD is not defined, completed transactions are processed in 90 * the caller's context. 91 */ 92#define CTL_DONE_THREAD 93 94/* 95 * * Use the serial number and device ID provided by the backend, rather than 96 * * making up our own. 97 * */ 98#define CTL_USE_BACKEND_SN 99 100/* 101 * Size and alignment macros needed for Copan-specific HA hardware. These 102 * can go away when the HA code is re-written, and uses busdma for any 103 * hardware. 104 */ 105#define CTL_ALIGN_8B(target, source, type) \ 106 if (((uint32_t)source & 0x7) != 0) \ 107 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 108 else \ 109 target = (type)source; 110 111#define CTL_SIZE_8B(target, size) \ 112 if ((size & 0x7) != 0) \ 113 target = size + (0x8 - (size & 0x7)); \ 114 else \ 115 target = size; 116 117#define CTL_ALIGN_8B_MARGIN 16 118 119/* 120 * Template mode pages. 121 */ 122 123/* 124 * Note that these are default values only. The actual values will be 125 * filled in when the user does a mode sense. 126 */ 127static struct copan_power_subpage power_page_default = { 128 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 129 /*subpage*/ PWR_SUBPAGE_CODE, 130 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 131 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 132 /*page_version*/ PWR_VERSION, 133 /* total_luns */ 26, 134 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 135 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 136 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137 0, 0, 0, 0, 0, 0} 138}; 139 140static struct copan_power_subpage power_page_changeable = { 141 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 142 /*subpage*/ PWR_SUBPAGE_CODE, 143 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 144 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 145 /*page_version*/ 0, 146 /* total_luns */ 0, 147 /* max_active_luns*/ 0, 148 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 150 0, 0, 0, 0, 0, 0} 151}; 152 153static struct copan_aps_subpage aps_page_default = { 154 APS_PAGE_CODE | SMPH_SPF, //page_code 155 APS_SUBPAGE_CODE, //subpage 156 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 157 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 158 APS_VERSION, //page_version 159 0, //lock_active 160 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 162 0, 0, 0, 0, 0} //reserved 163}; 164 165static struct copan_aps_subpage aps_page_changeable = { 166 APS_PAGE_CODE | SMPH_SPF, //page_code 167 APS_SUBPAGE_CODE, //subpage 168 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 169 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 170 0, //page_version 171 0, //lock_active 172 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 174 0, 0, 0, 0, 0} //reserved 175}; 176 177static struct copan_debugconf_subpage debugconf_page_default = { 178 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 179 DBGCNF_SUBPAGE_CODE, /* subpage */ 180 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 181 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 182 DBGCNF_VERSION, /* page_version */ 183 {CTL_TIME_IO_DEFAULT_SECS>>8, 184 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 185}; 186 187static struct copan_debugconf_subpage debugconf_page_changeable = { 188 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 189 DBGCNF_SUBPAGE_CODE, /* subpage */ 190 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 191 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 192 0, /* page_version */ 193 {0xff,0xff}, /* ctl_time_io_secs */ 194}; 195 196static struct scsi_format_page format_page_default = { 197 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 198 /*page_length*/sizeof(struct scsi_format_page) - 2, 199 /*tracks_per_zone*/ {0, 0}, 200 /*alt_sectors_per_zone*/ {0, 0}, 201 /*alt_tracks_per_zone*/ {0, 0}, 202 /*alt_tracks_per_lun*/ {0, 0}, 203 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 204 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 205 /*bytes_per_sector*/ {0, 0}, 206 /*interleave*/ {0, 0}, 207 /*track_skew*/ {0, 0}, 208 /*cylinder_skew*/ {0, 0}, 209 /*flags*/ SFP_HSEC, 210 /*reserved*/ {0, 0, 0} 211}; 212 213static struct scsi_format_page format_page_changeable = { 214 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 215 /*page_length*/sizeof(struct scsi_format_page) - 2, 216 /*tracks_per_zone*/ {0, 0}, 217 /*alt_sectors_per_zone*/ {0, 0}, 218 /*alt_tracks_per_zone*/ {0, 0}, 219 /*alt_tracks_per_lun*/ {0, 0}, 220 /*sectors_per_track*/ {0, 0}, 221 /*bytes_per_sector*/ {0, 0}, 222 /*interleave*/ {0, 0}, 223 /*track_skew*/ {0, 0}, 224 /*cylinder_skew*/ {0, 0}, 225 /*flags*/ 0, 226 /*reserved*/ {0, 0, 0} 227}; 228 229static struct scsi_rigid_disk_page rigid_disk_page_default = { 230 /*page_code*/SMS_RIGID_DISK_PAGE, 231 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 232 /*cylinders*/ {0, 0, 0}, 233 /*heads*/ CTL_DEFAULT_HEADS, 234 /*start_write_precomp*/ {0, 0, 0}, 235 /*start_reduced_current*/ {0, 0, 0}, 236 /*step_rate*/ {0, 0}, 237 /*landing_zone_cylinder*/ {0, 0, 0}, 238 /*rpl*/ SRDP_RPL_DISABLED, 239 /*rotational_offset*/ 0, 240 /*reserved1*/ 0, 241 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 242 CTL_DEFAULT_ROTATION_RATE & 0xff}, 243 /*reserved2*/ {0, 0} 244}; 245 246static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 247 /*page_code*/SMS_RIGID_DISK_PAGE, 248 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 249 /*cylinders*/ {0, 0, 0}, 250 /*heads*/ 0, 251 /*start_write_precomp*/ {0, 0, 0}, 252 /*start_reduced_current*/ {0, 0, 0}, 253 /*step_rate*/ {0, 0}, 254 /*landing_zone_cylinder*/ {0, 0, 0}, 255 /*rpl*/ 0, 256 /*rotational_offset*/ 0, 257 /*reserved1*/ 0, 258 /*rotation_rate*/ {0, 0}, 259 /*reserved2*/ {0, 0} 260}; 261 262static struct scsi_caching_page caching_page_default = { 263 /*page_code*/SMS_CACHING_PAGE, 264 /*page_length*/sizeof(struct scsi_caching_page) - 2, 265 /*flags1*/ SCP_DISC | SCP_WCE, 266 /*ret_priority*/ 0, 267 /*disable_pf_transfer_len*/ {0xff, 0xff}, 268 /*min_prefetch*/ {0, 0}, 269 /*max_prefetch*/ {0xff, 0xff}, 270 /*max_pf_ceiling*/ {0xff, 0xff}, 271 /*flags2*/ 0, 272 /*cache_segments*/ 0, 273 /*cache_seg_size*/ {0, 0}, 274 /*reserved*/ 0, 275 /*non_cache_seg_size*/ {0, 0, 0} 276}; 277 278static struct scsi_caching_page caching_page_changeable = { 279 /*page_code*/SMS_CACHING_PAGE, 280 /*page_length*/sizeof(struct scsi_caching_page) - 2, 281 /*flags1*/ 0, 282 /*ret_priority*/ 0, 283 /*disable_pf_transfer_len*/ {0, 0}, 284 /*min_prefetch*/ {0, 0}, 285 /*max_prefetch*/ {0, 0}, 286 /*max_pf_ceiling*/ {0, 0}, 287 /*flags2*/ 0, 288 /*cache_segments*/ 0, 289 /*cache_seg_size*/ {0, 0}, 290 /*reserved*/ 0, 291 /*non_cache_seg_size*/ {0, 0, 0} 292}; 293 294static struct scsi_control_page control_page_default = { 295 /*page_code*/SMS_CONTROL_MODE_PAGE, 296 /*page_length*/sizeof(struct scsi_control_page) - 2, 297 /*rlec*/0, 298 /*queue_flags*/0, 299 /*eca_and_aen*/0, 300 /*reserved*/0, 301 /*aen_holdoff_period*/{0, 0} 302}; 303 304static struct scsi_control_page control_page_changeable = { 305 /*page_code*/SMS_CONTROL_MODE_PAGE, 306 /*page_length*/sizeof(struct scsi_control_page) - 2, 307 /*rlec*/SCP_DSENSE, 308 /*queue_flags*/0, 309 /*eca_and_aen*/0, 310 /*reserved*/0, 311 /*aen_holdoff_period*/{0, 0} 312}; 313 314 315/* 316 * XXX KDM move these into the softc. 317 */ 318static int rcv_sync_msg; 319static int persis_offset; 320static uint8_t ctl_pause_rtr; 321static int ctl_is_single; 322static int index_to_aps_page; 323#ifdef CTL_DISABLE 324int ctl_disable = 1; 325#else 326int ctl_disable = 0; 327#endif 328 329SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 330SYSCTL_INT(_kern_cam_ctl, OID_AUTO, disable, CTLFLAG_RDTUN, &ctl_disable, 0, 331 "Disable CTL"); 332TUNABLE_INT("kern.cam.ctl.disable", &ctl_disable); 333 334/* 335 * Serial number (0x80), device id (0x83), and supported pages (0x00) 336 */ 337#define SCSI_EVPD_NUM_SUPPORTED_PAGES 3 338 339static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 340 int param); 341static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 342static int ctl_init(void); 343void ctl_shutdown(void); 344static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 345static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 346static void ctl_ioctl_online(void *arg); 347static void ctl_ioctl_offline(void *arg); 348static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id); 349static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id); 350static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 351static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 352static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 353static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock); 354static int ctl_ioctl_submit_wait(union ctl_io *io); 355static void ctl_ioctl_datamove(union ctl_io *io); 356static void ctl_ioctl_done(union ctl_io *io); 357static void ctl_ioctl_hard_startstop_callback(void *arg, 358 struct cfi_metatask *metatask); 359static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 360static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 361 struct ctl_ooa *ooa_hdr, 362 struct ctl_ooa_entry *kern_entries); 363static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 364 struct thread *td); 365uint32_t ctl_get_resindex(struct ctl_nexus *nexus); 366uint32_t ctl_port_idx(int port_num); 367#ifdef unused 368static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 369 uint32_t targ_target, uint32_t targ_lun, 370 int can_wait); 371static void ctl_kfree_io(union ctl_io *io); 372#endif /* unused */ 373static void ctl_free_io_internal(union ctl_io *io, int have_lock); 374static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 375 struct ctl_be_lun *be_lun, struct ctl_id target_id); 376static int ctl_free_lun(struct ctl_lun *lun); 377static void ctl_create_lun(struct ctl_be_lun *be_lun); 378/** 379static void ctl_failover_change_pages(struct ctl_softc *softc, 380 struct ctl_scsiio *ctsio, int master); 381**/ 382 383static int ctl_do_mode_select(union ctl_io *io); 384static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 385 uint64_t res_key, uint64_t sa_res_key, 386 uint8_t type, uint32_t residx, 387 struct ctl_scsiio *ctsio, 388 struct scsi_per_res_out *cdb, 389 struct scsi_per_res_out_parms* param); 390static void ctl_pro_preempt_other(struct ctl_lun *lun, 391 union ctl_ha_msg *msg); 392static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 393static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 394static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 395static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 396static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 397static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 398static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 399static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 400static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 401 union ctl_io *ooa_io); 402static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 403 union ctl_io *starting_io); 404static int ctl_check_blocked(struct ctl_lun *lun); 405static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 406 struct ctl_lun *lun, 407 struct ctl_cmd_entry *entry, 408 struct ctl_scsiio *ctsio); 409//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 410static void ctl_failover(void); 411static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 412 struct ctl_scsiio *ctsio); 413static int ctl_scsiio(struct ctl_scsiio *ctsio); 414 415static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 416static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 417 ctl_ua_type ua_type); 418static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 419 ctl_ua_type ua_type); 420static int ctl_abort_task(union ctl_io *io); 421static void ctl_run_task_queue(struct ctl_softc *ctl_softc); 422#ifdef CTL_IO_DELAY 423static void ctl_datamove_timer_wakeup(void *arg); 424static void ctl_done_timer_wakeup(void *arg); 425#endif /* CTL_IO_DELAY */ 426 427static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 428static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 429static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 430static void ctl_datamove_remote_write(union ctl_io *io); 431static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 432static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 433static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 434static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 435 ctl_ha_dt_cb callback); 436static void ctl_datamove_remote_read(union ctl_io *io); 437static void ctl_datamove_remote(union ctl_io *io); 438static int ctl_process_done(union ctl_io *io, int have_lock); 439static void ctl_work_thread(void *arg); 440 441/* 442 * Load the serialization table. This isn't very pretty, but is probably 443 * the easiest way to do it. 444 */ 445#include "ctl_ser_table.c" 446 447/* 448 * We only need to define open, close and ioctl routines for this driver. 449 */ 450static struct cdevsw ctl_cdevsw = { 451 .d_version = D_VERSION, 452 .d_flags = 0, 453 .d_open = ctl_open, 454 .d_close = ctl_close, 455 .d_ioctl = ctl_ioctl, 456 .d_name = "ctl", 457}; 458 459 460MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 461 462static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 463 464static moduledata_t ctl_moduledata = { 465 "ctl", 466 ctl_module_event_handler, 467 NULL 468}; 469 470DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 471MODULE_VERSION(ctl, 1); 472 473static void 474ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 475 union ctl_ha_msg *msg_info) 476{ 477 struct ctl_scsiio *ctsio; 478 479 if (msg_info->hdr.original_sc == NULL) { 480 printf("%s: original_sc == NULL!\n", __func__); 481 /* XXX KDM now what? */ 482 return; 483 } 484 485 ctsio = &msg_info->hdr.original_sc->scsiio; 486 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 487 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 488 ctsio->io_hdr.status = msg_info->hdr.status; 489 ctsio->scsi_status = msg_info->scsi.scsi_status; 490 ctsio->sense_len = msg_info->scsi.sense_len; 491 ctsio->sense_residual = msg_info->scsi.sense_residual; 492 ctsio->residual = msg_info->scsi.residual; 493 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 494 sizeof(ctsio->sense_data)); 495 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 496 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 497 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links); 498 ctl_wakeup_thread(); 499} 500 501static void 502ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 503 union ctl_ha_msg *msg_info) 504{ 505 struct ctl_scsiio *ctsio; 506 507 if (msg_info->hdr.serializing_sc == NULL) { 508 printf("%s: serializing_sc == NULL!\n", __func__); 509 /* XXX KDM now what? */ 510 return; 511 } 512 513 ctsio = &msg_info->hdr.serializing_sc->scsiio; 514#if 0 515 /* 516 * Attempt to catch the situation where an I/O has 517 * been freed, and we're using it again. 518 */ 519 if (ctsio->io_hdr.io_type == 0xff) { 520 union ctl_io *tmp_io; 521 tmp_io = (union ctl_io *)ctsio; 522 printf("%s: %p use after free!\n", __func__, 523 ctsio); 524 printf("%s: type %d msg %d cdb %x iptl: " 525 "%d:%d:%d:%d tag 0x%04x " 526 "flag %#x status %x\n", 527 __func__, 528 tmp_io->io_hdr.io_type, 529 tmp_io->io_hdr.msg_type, 530 tmp_io->scsiio.cdb[0], 531 tmp_io->io_hdr.nexus.initid.id, 532 tmp_io->io_hdr.nexus.targ_port, 533 tmp_io->io_hdr.nexus.targ_target.id, 534 tmp_io->io_hdr.nexus.targ_lun, 535 (tmp_io->io_hdr.io_type == 536 CTL_IO_TASK) ? 537 tmp_io->taskio.tag_num : 538 tmp_io->scsiio.tag_num, 539 tmp_io->io_hdr.flags, 540 tmp_io->io_hdr.status); 541 } 542#endif 543 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 544 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links); 545 ctl_wakeup_thread(); 546} 547 548/* 549 * ISC (Inter Shelf Communication) event handler. Events from the HA 550 * subsystem come in here. 551 */ 552static void 553ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 554{ 555 struct ctl_softc *ctl_softc; 556 union ctl_io *io; 557 struct ctl_prio *presio; 558 ctl_ha_status isc_status; 559 560 ctl_softc = control_softc; 561 io = NULL; 562 563 564#if 0 565 printf("CTL: Isc Msg event %d\n", event); 566#endif 567 if (event == CTL_HA_EVT_MSG_RECV) { 568 union ctl_ha_msg msg_info; 569 570 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 571 sizeof(msg_info), /*wait*/ 0); 572#if 0 573 printf("CTL: msg_type %d\n", msg_info.msg_type); 574#endif 575 if (isc_status != 0) { 576 printf("Error receiving message, status = %d\n", 577 isc_status); 578 return; 579 } 580 mtx_lock(&ctl_softc->ctl_lock); 581 582 switch (msg_info.hdr.msg_type) { 583 case CTL_MSG_SERIALIZE: 584#if 0 585 printf("Serialize\n"); 586#endif 587 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 588 if (io == NULL) { 589 printf("ctl_isc_event_handler: can't allocate " 590 "ctl_io!\n"); 591 /* Bad Juju */ 592 /* Need to set busy and send msg back */ 593 mtx_unlock(&ctl_softc->ctl_lock); 594 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 595 msg_info.hdr.status = CTL_SCSI_ERROR; 596 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 597 msg_info.scsi.sense_len = 0; 598 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 599 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 600 } 601 goto bailout; 602 } 603 ctl_zero_io(io); 604 // populate ctsio from msg_info 605 io->io_hdr.io_type = CTL_IO_SCSI; 606 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 607 io->io_hdr.original_sc = msg_info.hdr.original_sc; 608#if 0 609 printf("pOrig %x\n", (int)msg_info.original_sc); 610#endif 611 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 612 CTL_FLAG_IO_ACTIVE; 613 /* 614 * If we're in serialization-only mode, we don't 615 * want to go through full done processing. Thus 616 * the COPY flag. 617 * 618 * XXX KDM add another flag that is more specific. 619 */ 620 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 621 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 622 io->io_hdr.nexus = msg_info.hdr.nexus; 623#if 0 624 printf("targ %d, port %d, iid %d, lun %d\n", 625 io->io_hdr.nexus.targ_target.id, 626 io->io_hdr.nexus.targ_port, 627 io->io_hdr.nexus.initid.id, 628 io->io_hdr.nexus.targ_lun); 629#endif 630 io->scsiio.tag_num = msg_info.scsi.tag_num; 631 io->scsiio.tag_type = msg_info.scsi.tag_type; 632 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 633 CTL_MAX_CDBLEN); 634 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 635 struct ctl_cmd_entry *entry; 636 uint8_t opcode; 637 638 opcode = io->scsiio.cdb[0]; 639 entry = &ctl_cmd_table[opcode]; 640 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 641 io->io_hdr.flags |= 642 entry->flags & CTL_FLAG_DATA_MASK; 643 } 644 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 645 &io->io_hdr, links); 646 ctl_wakeup_thread(); 647 break; 648 649 /* Performed on the Originating SC, XFER mode only */ 650 case CTL_MSG_DATAMOVE: { 651 struct ctl_sg_entry *sgl; 652 int i, j; 653 654 io = msg_info.hdr.original_sc; 655 if (io == NULL) { 656 printf("%s: original_sc == NULL!\n", __func__); 657 /* XXX KDM do something here */ 658 break; 659 } 660 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 661 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 662 /* 663 * Keep track of this, we need to send it back over 664 * when the datamove is complete. 665 */ 666 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 667 668 if (msg_info.dt.sg_sequence == 0) { 669 /* 670 * XXX KDM we use the preallocated S/G list 671 * here, but we'll need to change this to 672 * dynamic allocation if we need larger S/G 673 * lists. 674 */ 675 if (msg_info.dt.kern_sg_entries > 676 sizeof(io->io_hdr.remote_sglist) / 677 sizeof(io->io_hdr.remote_sglist[0])) { 678 printf("%s: number of S/G entries " 679 "needed %u > allocated num %zd\n", 680 __func__, 681 msg_info.dt.kern_sg_entries, 682 sizeof(io->io_hdr.remote_sglist)/ 683 sizeof(io->io_hdr.remote_sglist[0])); 684 685 /* 686 * XXX KDM send a message back to 687 * the other side to shut down the 688 * DMA. The error will come back 689 * through via the normal channel. 690 */ 691 break; 692 } 693 sgl = io->io_hdr.remote_sglist; 694 memset(sgl, 0, 695 sizeof(io->io_hdr.remote_sglist)); 696 697 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 698 699 io->scsiio.kern_sg_entries = 700 msg_info.dt.kern_sg_entries; 701 io->scsiio.rem_sg_entries = 702 msg_info.dt.kern_sg_entries; 703 io->scsiio.kern_data_len = 704 msg_info.dt.kern_data_len; 705 io->scsiio.kern_total_len = 706 msg_info.dt.kern_total_len; 707 io->scsiio.kern_data_resid = 708 msg_info.dt.kern_data_resid; 709 io->scsiio.kern_rel_offset = 710 msg_info.dt.kern_rel_offset; 711 /* 712 * Clear out per-DMA flags. 713 */ 714 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 715 /* 716 * Add per-DMA flags that are set for this 717 * particular DMA request. 718 */ 719 io->io_hdr.flags |= msg_info.dt.flags & 720 CTL_FLAG_RDMA_MASK; 721 } else 722 sgl = (struct ctl_sg_entry *) 723 io->scsiio.kern_data_ptr; 724 725 for (i = msg_info.dt.sent_sg_entries, j = 0; 726 i < (msg_info.dt.sent_sg_entries + 727 msg_info.dt.cur_sg_entries); i++, j++) { 728 sgl[i].addr = msg_info.dt.sg_list[j].addr; 729 sgl[i].len = msg_info.dt.sg_list[j].len; 730 731#if 0 732 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 733 __func__, 734 msg_info.dt.sg_list[j].addr, 735 msg_info.dt.sg_list[j].len, 736 sgl[i].addr, sgl[i].len, j, i); 737#endif 738 } 739#if 0 740 memcpy(&sgl[msg_info.dt.sent_sg_entries], 741 msg_info.dt.sg_list, 742 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 743#endif 744 745 /* 746 * If this is the last piece of the I/O, we've got 747 * the full S/G list. Queue processing in the thread. 748 * Otherwise wait for the next piece. 749 */ 750 if (msg_info.dt.sg_last != 0) { 751 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 752 &io->io_hdr, links); 753 ctl_wakeup_thread(); 754 } 755 break; 756 } 757 /* Performed on the Serializing (primary) SC, XFER mode only */ 758 case CTL_MSG_DATAMOVE_DONE: { 759 if (msg_info.hdr.serializing_sc == NULL) { 760 printf("%s: serializing_sc == NULL!\n", 761 __func__); 762 /* XXX KDM now what? */ 763 break; 764 } 765 /* 766 * We grab the sense information here in case 767 * there was a failure, so we can return status 768 * back to the initiator. 769 */ 770 io = msg_info.hdr.serializing_sc; 771 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 772 io->io_hdr.status = msg_info.hdr.status; 773 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 774 io->scsiio.sense_len = msg_info.scsi.sense_len; 775 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 776 io->io_hdr.port_status = msg_info.scsi.fetd_status; 777 io->scsiio.residual = msg_info.scsi.residual; 778 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 779 sizeof(io->scsiio.sense_data)); 780 781 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 782 &io->io_hdr, links); 783 ctl_wakeup_thread(); 784 break; 785 } 786 787 /* Preformed on Originating SC, SER_ONLY mode */ 788 case CTL_MSG_R2R: 789 io = msg_info.hdr.original_sc; 790 if (io == NULL) { 791 printf("%s: Major Bummer\n", __func__); 792 mtx_unlock(&ctl_softc->ctl_lock); 793 return; 794 } else { 795#if 0 796 printf("pOrig %x\n",(int) ctsio); 797#endif 798 } 799 io->io_hdr.msg_type = CTL_MSG_R2R; 800 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 801 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 802 &io->io_hdr, links); 803 ctl_wakeup_thread(); 804 break; 805 806 /* 807 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 808 * mode. 809 * Performed on the Originating (i.e. secondary) SC in XFER 810 * mode 811 */ 812 case CTL_MSG_FINISH_IO: 813 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 814 ctl_isc_handler_finish_xfer(ctl_softc, 815 &msg_info); 816 else 817 ctl_isc_handler_finish_ser_only(ctl_softc, 818 &msg_info); 819 break; 820 821 /* Preformed on Originating SC */ 822 case CTL_MSG_BAD_JUJU: 823 io = msg_info.hdr.original_sc; 824 if (io == NULL) { 825 printf("%s: Bad JUJU!, original_sc is NULL!\n", 826 __func__); 827 break; 828 } 829 ctl_copy_sense_data(&msg_info, io); 830 /* 831 * IO should have already been cleaned up on other 832 * SC so clear this flag so we won't send a message 833 * back to finish the IO there. 834 */ 835 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 836 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 837 838 /* io = msg_info.hdr.serializing_sc; */ 839 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 840 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 841 &io->io_hdr, links); 842 ctl_wakeup_thread(); 843 break; 844 845 /* Handle resets sent from the other side */ 846 case CTL_MSG_MANAGE_TASKS: { 847 struct ctl_taskio *taskio; 848 taskio = (struct ctl_taskio *)ctl_alloc_io( 849 (void *)ctl_softc->othersc_pool); 850 if (taskio == NULL) { 851 printf("ctl_isc_event_handler: can't allocate " 852 "ctl_io!\n"); 853 /* Bad Juju */ 854 /* should I just call the proper reset func 855 here??? */ 856 mtx_unlock(&ctl_softc->ctl_lock); 857 goto bailout; 858 } 859 ctl_zero_io((union ctl_io *)taskio); 860 taskio->io_hdr.io_type = CTL_IO_TASK; 861 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 862 taskio->io_hdr.nexus = msg_info.hdr.nexus; 863 taskio->task_action = msg_info.task.task_action; 864 taskio->tag_num = msg_info.task.tag_num; 865 taskio->tag_type = msg_info.task.tag_type; 866#ifdef CTL_TIME_IO 867 taskio->io_hdr.start_time = time_uptime; 868 getbintime(&taskio->io_hdr.start_bt); 869#if 0 870 cs_prof_gettime(&taskio->io_hdr.start_ticks); 871#endif 872#endif /* CTL_TIME_IO */ 873 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, 874 &taskio->io_hdr, links); 875 ctl_softc->flags |= CTL_FLAG_TASK_PENDING; 876 ctl_wakeup_thread(); 877 break; 878 } 879 /* Persistent Reserve action which needs attention */ 880 case CTL_MSG_PERS_ACTION: 881 presio = (struct ctl_prio *)ctl_alloc_io( 882 (void *)ctl_softc->othersc_pool); 883 if (presio == NULL) { 884 printf("ctl_isc_event_handler: can't allocate " 885 "ctl_io!\n"); 886 /* Bad Juju */ 887 /* Need to set busy and send msg back */ 888 mtx_unlock(&ctl_softc->ctl_lock); 889 goto bailout; 890 } 891 ctl_zero_io((union ctl_io *)presio); 892 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 893 presio->pr_msg = msg_info.pr; 894 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 895 &presio->io_hdr, links); 896 ctl_wakeup_thread(); 897 break; 898 case CTL_MSG_SYNC_FE: 899 rcv_sync_msg = 1; 900 break; 901 case CTL_MSG_APS_LOCK: { 902 // It's quicker to execute this then to 903 // queue it. 904 struct ctl_lun *lun; 905 struct ctl_page_index *page_index; 906 struct copan_aps_subpage *current_sp; 907 908 lun = ctl_softc->ctl_luns[msg_info.hdr.nexus.targ_lun]; 909 page_index = &lun->mode_pages.index[index_to_aps_page]; 910 current_sp = (struct copan_aps_subpage *) 911 (page_index->page_data + 912 (page_index->page_len * CTL_PAGE_CURRENT)); 913 914 current_sp->lock_active = msg_info.aps.lock_flag; 915 break; 916 } 917 default: 918 printf("How did I get here?\n"); 919 } 920 mtx_unlock(&ctl_softc->ctl_lock); 921 } else if (event == CTL_HA_EVT_MSG_SENT) { 922 if (param != CTL_HA_STATUS_SUCCESS) { 923 printf("Bad status from ctl_ha_msg_send status %d\n", 924 param); 925 } 926 return; 927 } else if (event == CTL_HA_EVT_DISCONNECT) { 928 printf("CTL: Got a disconnect from Isc\n"); 929 return; 930 } else { 931 printf("ctl_isc_event_handler: Unknown event %d\n", event); 932 return; 933 } 934 935bailout: 936 return; 937} 938 939static void 940ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 941{ 942 struct scsi_sense_data *sense; 943 944 sense = &dest->scsiio.sense_data; 945 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 946 dest->scsiio.scsi_status = src->scsi.scsi_status; 947 dest->scsiio.sense_len = src->scsi.sense_len; 948 dest->io_hdr.status = src->hdr.status; 949} 950 951static int 952ctl_init(void) 953{ 954 struct ctl_softc *softc; 955 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 956 struct ctl_frontend *fe; 957 struct ctl_lun *lun; 958 uint8_t sc_id =0; 959#if 0 960 int i; 961#endif 962 int error, retval; 963 //int isc_retval; 964 965 retval = 0; 966 ctl_pause_rtr = 0; 967 rcv_sync_msg = 0; 968 969 /* If we're disabled, don't initialize. */ 970 if (ctl_disable != 0) 971 return (0); 972 973 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 974 M_WAITOK | M_ZERO); 975 softc = control_softc; 976 977 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 978 "cam/ctl"); 979 980 softc->dev->si_drv1 = softc; 981 982 /* 983 * By default, return a "bad LUN" peripheral qualifier for unknown 984 * LUNs. The user can override this default using the tunable or 985 * sysctl. See the comment in ctl_inquiry_std() for more details. 986 */ 987 softc->inquiry_pq_no_lun = 1; 988 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 989 &softc->inquiry_pq_no_lun); 990 sysctl_ctx_init(&softc->sysctl_ctx); 991 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 992 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 993 CTLFLAG_RD, 0, "CAM Target Layer"); 994 995 if (softc->sysctl_tree == NULL) { 996 printf("%s: unable to allocate sysctl tree\n", __func__); 997 destroy_dev(softc->dev); 998 free(control_softc, M_DEVBUF); 999 control_softc = NULL; 1000 return (ENOMEM); 1001 } 1002 1003 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1004 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1005 "inquiry_pq_no_lun", CTLFLAG_RW, 1006 &softc->inquiry_pq_no_lun, 0, 1007 "Report no lun possible for invalid LUNs"); 1008 1009 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1010 softc->open_count = 0; 1011 1012 /* 1013 * Default to actually sending a SYNCHRONIZE CACHE command down to 1014 * the drive. 1015 */ 1016 softc->flags = CTL_FLAG_REAL_SYNC; 1017 1018 /* 1019 * In Copan's HA scheme, the "master" and "slave" roles are 1020 * figured out through the slot the controller is in. Although it 1021 * is an active/active system, someone has to be in charge. 1022 */ 1023#ifdef NEEDTOPORT 1024 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1025#endif 1026 1027 if (sc_id == 0) { 1028 softc->flags |= CTL_FLAG_MASTER_SHELF; 1029 persis_offset = 0; 1030 } else 1031 persis_offset = CTL_MAX_INITIATORS; 1032 1033 /* 1034 * XXX KDM need to figure out where we want to get our target ID 1035 * and WWID. Is it different on each port? 1036 */ 1037 softc->target.id = 0; 1038 softc->target.wwid[0] = 0x12345678; 1039 softc->target.wwid[1] = 0x87654321; 1040 STAILQ_INIT(&softc->lun_list); 1041 STAILQ_INIT(&softc->pending_lun_queue); 1042 STAILQ_INIT(&softc->task_queue); 1043 STAILQ_INIT(&softc->incoming_queue); 1044 STAILQ_INIT(&softc->rtr_queue); 1045 STAILQ_INIT(&softc->done_queue); 1046 STAILQ_INIT(&softc->isc_queue); 1047 STAILQ_INIT(&softc->fe_list); 1048 STAILQ_INIT(&softc->be_list); 1049 STAILQ_INIT(&softc->io_pools); 1050 1051 lun = &softc->lun; 1052 1053 /* 1054 * We don't bother calling these with ctl_lock held here, because, 1055 * in theory, no one else can try to do anything while we're in our 1056 * module init routine. 1057 */ 1058 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1059 &internal_pool)!= 0){ 1060 printf("ctl: can't allocate %d entry internal pool, " 1061 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1062 return (ENOMEM); 1063 } 1064 1065 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1066 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1067 printf("ctl: can't allocate %d entry emergency pool, " 1068 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1069 ctl_pool_free(softc, internal_pool); 1070 return (ENOMEM); 1071 } 1072 1073 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1074 &other_pool) != 0) 1075 { 1076 printf("ctl: can't allocate %d entry other SC pool, " 1077 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1078 ctl_pool_free(softc, internal_pool); 1079 ctl_pool_free(softc, emergency_pool); 1080 return (ENOMEM); 1081 } 1082 1083 softc->internal_pool = internal_pool; 1084 softc->emergency_pool = emergency_pool; 1085 softc->othersc_pool = other_pool; 1086 1087 ctl_pool_acquire(internal_pool); 1088 ctl_pool_acquire(emergency_pool); 1089 ctl_pool_acquire(other_pool); 1090 1091 /* 1092 * We used to allocate a processor LUN here. The new scheme is to 1093 * just let the user allocate LUNs as he sees fit. 1094 */ 1095#if 0 1096 mtx_lock(&softc->ctl_lock); 1097 ctl_alloc_lun(softc, lun, /*be_lun*/NULL, /*target*/softc->target); 1098 mtx_unlock(&softc->ctl_lock); 1099#endif 1100 1101 error = kproc_create(ctl_work_thread, softc, &softc->work_thread, 0, 0, 1102 "ctl_thrd"); 1103 if (error != 0) { 1104 printf("error creating CTL work thread!\n"); 1105 ctl_free_lun(lun); 1106 ctl_pool_free(softc, internal_pool); 1107 ctl_pool_free(softc, emergency_pool); 1108 ctl_pool_free(softc, other_pool); 1109 return (error); 1110 } 1111 printf("ctl: CAM Target Layer loaded\n"); 1112 1113 /* 1114 * Initialize the initiator and portname mappings 1115 */ 1116 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid)); 1117 1118 /* 1119 * Initialize the ioctl front end. 1120 */ 1121 fe = &softc->ioctl_info.fe; 1122 sprintf(softc->ioctl_info.port_name, "CTL ioctl"); 1123 fe->port_type = CTL_PORT_IOCTL; 1124 fe->num_requested_ctl_io = 100; 1125 fe->port_name = softc->ioctl_info.port_name; 1126 fe->port_online = ctl_ioctl_online; 1127 fe->port_offline = ctl_ioctl_offline; 1128 fe->onoff_arg = &softc->ioctl_info; 1129 fe->targ_enable = ctl_ioctl_targ_enable; 1130 fe->targ_disable = ctl_ioctl_targ_disable; 1131 fe->lun_enable = ctl_ioctl_lun_enable; 1132 fe->lun_disable = ctl_ioctl_lun_disable; 1133 fe->targ_lun_arg = &softc->ioctl_info; 1134 fe->fe_datamove = ctl_ioctl_datamove; 1135 fe->fe_done = ctl_ioctl_done; 1136 fe->max_targets = 15; 1137 fe->max_target_id = 15; 1138 1139 if (ctl_frontend_register(&softc->ioctl_info.fe, 1140 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1141 printf("ctl: ioctl front end registration failed, will " 1142 "continue anyway\n"); 1143 } 1144 1145#ifdef CTL_IO_DELAY 1146 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1147 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1148 sizeof(struct callout), CTL_TIMER_BYTES); 1149 return (EINVAL); 1150 } 1151#endif /* CTL_IO_DELAY */ 1152 1153 return (0); 1154} 1155 1156void 1157ctl_shutdown(void) 1158{ 1159 struct ctl_softc *softc; 1160 struct ctl_lun *lun, *next_lun; 1161 struct ctl_io_pool *pool, *next_pool; 1162 1163 softc = (struct ctl_softc *)control_softc; 1164 1165 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0) 1166 printf("ctl: ioctl front end deregistration failed\n"); 1167 1168 mtx_lock(&softc->ctl_lock); 1169 1170 /* 1171 * Free up each LUN. 1172 */ 1173 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1174 next_lun = STAILQ_NEXT(lun, links); 1175 ctl_free_lun(lun); 1176 } 1177 1178 /* 1179 * This will rip the rug out from under any FETDs or anyone else 1180 * that has a pool allocated. Since we increment our module 1181 * refcount any time someone outside the main CTL module allocates 1182 * a pool, we shouldn't have any problems here. The user won't be 1183 * able to unload the CTL module until client modules have 1184 * successfully unloaded. 1185 */ 1186 for (pool = STAILQ_FIRST(&softc->io_pools); pool != NULL; 1187 pool = next_pool) { 1188 next_pool = STAILQ_NEXT(pool, links); 1189 ctl_pool_free(softc, pool); 1190 } 1191 1192 mtx_unlock(&softc->ctl_lock); 1193 1194#if 0 1195 ctl_shutdown_thread(softc->work_thread); 1196#endif 1197 1198 mtx_destroy(&softc->ctl_lock); 1199 1200 destroy_dev(softc->dev); 1201 1202 sysctl_ctx_free(&softc->sysctl_ctx); 1203 1204 free(control_softc, M_DEVBUF); 1205 control_softc = NULL; 1206 1207 printf("ctl: CAM Target Layer unloaded\n"); 1208} 1209 1210static int 1211ctl_module_event_handler(module_t mod, int what, void *arg) 1212{ 1213 1214 switch (what) { 1215 case MOD_LOAD: 1216 return (ctl_init()); 1217 case MOD_UNLOAD: 1218 return (EBUSY); 1219 default: 1220 return (EOPNOTSUPP); 1221 } 1222} 1223 1224/* 1225 * XXX KDM should we do some access checks here? Bump a reference count to 1226 * prevent a CTL module from being unloaded while someone has it open? 1227 */ 1228static int 1229ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1230{ 1231 return (0); 1232} 1233 1234static int 1235ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1236{ 1237 return (0); 1238} 1239 1240int 1241ctl_port_enable(ctl_port_type port_type) 1242{ 1243 struct ctl_softc *softc; 1244 struct ctl_frontend *fe; 1245 1246 if (ctl_is_single == 0) { 1247 union ctl_ha_msg msg_info; 1248 int isc_retval; 1249 1250#if 0 1251 printf("%s: HA mode, synchronizing frontend enable\n", 1252 __func__); 1253#endif 1254 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1255 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1256 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1257 printf("Sync msg send error retval %d\n", isc_retval); 1258 } 1259 if (!rcv_sync_msg) { 1260 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1261 sizeof(msg_info), 1); 1262 } 1263#if 0 1264 printf("CTL:Frontend Enable\n"); 1265 } else { 1266 printf("%s: single mode, skipping frontend synchronization\n", 1267 __func__); 1268#endif 1269 } 1270 1271 softc = control_softc; 1272 1273 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1274 if (port_type & fe->port_type) 1275 { 1276#if 0 1277 printf("port %d\n", fe->targ_port); 1278#endif 1279 ctl_frontend_online(fe); 1280 } 1281 } 1282 1283 return (0); 1284} 1285 1286int 1287ctl_port_disable(ctl_port_type port_type) 1288{ 1289 struct ctl_softc *softc; 1290 struct ctl_frontend *fe; 1291 1292 softc = control_softc; 1293 1294 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1295 if (port_type & fe->port_type) 1296 ctl_frontend_offline(fe); 1297 } 1298 1299 return (0); 1300} 1301 1302/* 1303 * Returns 0 for success, 1 for failure. 1304 * Currently the only failure mode is if there aren't enough entries 1305 * allocated. So, in case of a failure, look at num_entries_dropped, 1306 * reallocate and try again. 1307 */ 1308int 1309ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1310 int *num_entries_filled, int *num_entries_dropped, 1311 ctl_port_type port_type, int no_virtual) 1312{ 1313 struct ctl_softc *softc; 1314 struct ctl_frontend *fe; 1315 int entries_dropped, entries_filled; 1316 int retval; 1317 int i; 1318 1319 softc = control_softc; 1320 1321 retval = 0; 1322 entries_filled = 0; 1323 entries_dropped = 0; 1324 1325 i = 0; 1326 mtx_lock(&softc->ctl_lock); 1327 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1328 struct ctl_port_entry *entry; 1329 1330 if ((fe->port_type & port_type) == 0) 1331 continue; 1332 1333 if ((no_virtual != 0) 1334 && (fe->virtual_port != 0)) 1335 continue; 1336 1337 if (entries_filled >= num_entries_alloced) { 1338 entries_dropped++; 1339 continue; 1340 } 1341 entry = &entries[i]; 1342 1343 entry->port_type = fe->port_type; 1344 strlcpy(entry->port_name, fe->port_name, 1345 sizeof(entry->port_name)); 1346 entry->physical_port = fe->physical_port; 1347 entry->virtual_port = fe->virtual_port; 1348 entry->wwnn = fe->wwnn; 1349 entry->wwpn = fe->wwpn; 1350 1351 i++; 1352 entries_filled++; 1353 } 1354 1355 mtx_unlock(&softc->ctl_lock); 1356 1357 if (entries_dropped > 0) 1358 retval = 1; 1359 1360 *num_entries_dropped = entries_dropped; 1361 *num_entries_filled = entries_filled; 1362 1363 return (retval); 1364} 1365 1366static void 1367ctl_ioctl_online(void *arg) 1368{ 1369 struct ctl_ioctl_info *ioctl_info; 1370 1371 ioctl_info = (struct ctl_ioctl_info *)arg; 1372 1373 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1374} 1375 1376static void 1377ctl_ioctl_offline(void *arg) 1378{ 1379 struct ctl_ioctl_info *ioctl_info; 1380 1381 ioctl_info = (struct ctl_ioctl_info *)arg; 1382 1383 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1384} 1385 1386/* 1387 * Remove an initiator by port number and initiator ID. 1388 * Returns 0 for success, 1 for failure. 1389 * Assumes the caller does NOT hold the CTL lock. 1390 */ 1391int 1392ctl_remove_initiator(int32_t targ_port, uint32_t iid) 1393{ 1394 struct ctl_softc *softc; 1395 1396 softc = control_softc; 1397 1398 if ((targ_port < 0) 1399 || (targ_port > CTL_MAX_PORTS)) { 1400 printf("%s: invalid port number %d\n", __func__, targ_port); 1401 return (1); 1402 } 1403 if (iid > CTL_MAX_INIT_PER_PORT) { 1404 printf("%s: initiator ID %u > maximun %u!\n", 1405 __func__, iid, CTL_MAX_INIT_PER_PORT); 1406 return (1); 1407 } 1408 1409 mtx_lock(&softc->ctl_lock); 1410 1411 softc->wwpn_iid[targ_port][iid].in_use = 0; 1412 1413 mtx_unlock(&softc->ctl_lock); 1414 1415 return (0); 1416} 1417 1418/* 1419 * Add an initiator to the initiator map. 1420 * Returns 0 for success, 1 for failure. 1421 * Assumes the caller does NOT hold the CTL lock. 1422 */ 1423int 1424ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid) 1425{ 1426 struct ctl_softc *softc; 1427 int retval; 1428 1429 softc = control_softc; 1430 1431 retval = 0; 1432 1433 if ((targ_port < 0) 1434 || (targ_port > CTL_MAX_PORTS)) { 1435 printf("%s: invalid port number %d\n", __func__, targ_port); 1436 return (1); 1437 } 1438 if (iid > CTL_MAX_INIT_PER_PORT) { 1439 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n", 1440 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1441 return (1); 1442 } 1443 1444 mtx_lock(&softc->ctl_lock); 1445 1446 if (softc->wwpn_iid[targ_port][iid].in_use != 0) { 1447 /* 1448 * We don't treat this as an error. 1449 */ 1450 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) { 1451 printf("%s: port %d iid %u WWPN %#jx arrived again?\n", 1452 __func__, targ_port, iid, (uintmax_t)wwpn); 1453 goto bailout; 1454 } 1455 1456 /* 1457 * This is an error, but what do we do about it? The 1458 * driver is telling us we have a new WWPN for this 1459 * initiator ID, so we pretty much need to use it. 1460 */ 1461 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is " 1462 "still at that address\n", __func__, targ_port, iid, 1463 (uintmax_t)wwpn, 1464 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn); 1465 1466 /* 1467 * XXX KDM clear have_ca and ua_pending on each LUN for 1468 * this initiator. 1469 */ 1470 } 1471 softc->wwpn_iid[targ_port][iid].in_use = 1; 1472 softc->wwpn_iid[targ_port][iid].iid = iid; 1473 softc->wwpn_iid[targ_port][iid].wwpn = wwpn; 1474 softc->wwpn_iid[targ_port][iid].port = targ_port; 1475 1476bailout: 1477 1478 mtx_unlock(&softc->ctl_lock); 1479 1480 return (retval); 1481} 1482 1483/* 1484 * XXX KDM should we pretend to do something in the target/lun 1485 * enable/disable functions? 1486 */ 1487static int 1488ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id) 1489{ 1490 return (0); 1491} 1492 1493static int 1494ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id) 1495{ 1496 return (0); 1497} 1498 1499static int 1500ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1501{ 1502 return (0); 1503} 1504 1505static int 1506ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1507{ 1508 return (0); 1509} 1510 1511/* 1512 * Data movement routine for the CTL ioctl frontend port. 1513 */ 1514static int 1515ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1516{ 1517 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1518 struct ctl_sg_entry ext_entry, kern_entry; 1519 int ext_sglen, ext_sg_entries, kern_sg_entries; 1520 int ext_sg_start, ext_offset; 1521 int len_to_copy, len_copied; 1522 int kern_watermark, ext_watermark; 1523 int ext_sglist_malloced; 1524 int i, j; 1525 1526 ext_sglist_malloced = 0; 1527 ext_sg_start = 0; 1528 ext_offset = 0; 1529 1530 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1531 1532 /* 1533 * If this flag is set, fake the data transfer. 1534 */ 1535 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1536 ctsio->ext_data_filled = ctsio->ext_data_len; 1537 goto bailout; 1538 } 1539 1540 /* 1541 * To simplify things here, if we have a single buffer, stick it in 1542 * a S/G entry and just make it a single entry S/G list. 1543 */ 1544 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1545 int len_seen; 1546 1547 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1548 1549 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1550 M_WAITOK); 1551 ext_sglist_malloced = 1; 1552 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1553 ext_sglen) != 0) { 1554 ctl_set_internal_failure(ctsio, 1555 /*sks_valid*/ 0, 1556 /*retry_count*/ 0); 1557 goto bailout; 1558 } 1559 ext_sg_entries = ctsio->ext_sg_entries; 1560 len_seen = 0; 1561 for (i = 0; i < ext_sg_entries; i++) { 1562 if ((len_seen + ext_sglist[i].len) >= 1563 ctsio->ext_data_filled) { 1564 ext_sg_start = i; 1565 ext_offset = ctsio->ext_data_filled - len_seen; 1566 break; 1567 } 1568 len_seen += ext_sglist[i].len; 1569 } 1570 } else { 1571 ext_sglist = &ext_entry; 1572 ext_sglist->addr = ctsio->ext_data_ptr; 1573 ext_sglist->len = ctsio->ext_data_len; 1574 ext_sg_entries = 1; 1575 ext_sg_start = 0; 1576 ext_offset = ctsio->ext_data_filled; 1577 } 1578 1579 if (ctsio->kern_sg_entries > 0) { 1580 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1581 kern_sg_entries = ctsio->kern_sg_entries; 1582 } else { 1583 kern_sglist = &kern_entry; 1584 kern_sglist->addr = ctsio->kern_data_ptr; 1585 kern_sglist->len = ctsio->kern_data_len; 1586 kern_sg_entries = 1; 1587 } 1588 1589 1590 kern_watermark = 0; 1591 ext_watermark = ext_offset; 1592 len_copied = 0; 1593 for (i = ext_sg_start, j = 0; 1594 i < ext_sg_entries && j < kern_sg_entries;) { 1595 uint8_t *ext_ptr, *kern_ptr; 1596 1597 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1598 kern_sglist[j].len - kern_watermark); 1599 1600 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1601 ext_ptr = ext_ptr + ext_watermark; 1602 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1603 /* 1604 * XXX KDM fix this! 1605 */ 1606 panic("need to implement bus address support"); 1607#if 0 1608 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1609#endif 1610 } else 1611 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1612 kern_ptr = kern_ptr + kern_watermark; 1613 1614 kern_watermark += len_to_copy; 1615 ext_watermark += len_to_copy; 1616 1617 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1618 CTL_FLAG_DATA_IN) { 1619 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1620 "bytes to user\n", len_to_copy)); 1621 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1622 "to %p\n", kern_ptr, ext_ptr)); 1623 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1624 ctl_set_internal_failure(ctsio, 1625 /*sks_valid*/ 0, 1626 /*retry_count*/ 0); 1627 goto bailout; 1628 } 1629 } else { 1630 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1631 "bytes from user\n", len_to_copy)); 1632 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1633 "to %p\n", ext_ptr, kern_ptr)); 1634 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1635 ctl_set_internal_failure(ctsio, 1636 /*sks_valid*/ 0, 1637 /*retry_count*/0); 1638 goto bailout; 1639 } 1640 } 1641 1642 len_copied += len_to_copy; 1643 1644 if (ext_sglist[i].len == ext_watermark) { 1645 i++; 1646 ext_watermark = 0; 1647 } 1648 1649 if (kern_sglist[j].len == kern_watermark) { 1650 j++; 1651 kern_watermark = 0; 1652 } 1653 } 1654 1655 ctsio->ext_data_filled += len_copied; 1656 1657 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1658 "kern_sg_entries: %d\n", ext_sg_entries, 1659 kern_sg_entries)); 1660 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1661 "kern_data_len = %d\n", ctsio->ext_data_len, 1662 ctsio->kern_data_len)); 1663 1664 1665 /* XXX KDM set residual?? */ 1666bailout: 1667 1668 if (ext_sglist_malloced != 0) 1669 free(ext_sglist, M_CTL); 1670 1671 return (CTL_RETVAL_COMPLETE); 1672} 1673 1674/* 1675 * Serialize a command that went down the "wrong" side, and so was sent to 1676 * this controller for execution. The logic is a little different than the 1677 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1678 * sent back to the other side, but in the success case, we execute the 1679 * command on this side (XFER mode) or tell the other side to execute it 1680 * (SER_ONLY mode). 1681 */ 1682static int 1683ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock) 1684{ 1685 struct ctl_softc *ctl_softc; 1686 union ctl_ha_msg msg_info; 1687 struct ctl_lun *lun; 1688 int retval = 0; 1689 1690 ctl_softc = control_softc; 1691 if (have_lock == 0) 1692 mtx_lock(&ctl_softc->ctl_lock); 1693 1694 lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun]; 1695 if (lun==NULL) 1696 { 1697 /* 1698 * Why isn't LUN defined? The other side wouldn't 1699 * send a cmd if the LUN is undefined. 1700 */ 1701 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1702 1703 /* "Logical unit not supported" */ 1704 ctl_set_sense_data(&msg_info.scsi.sense_data, 1705 lun, 1706 /*sense_format*/SSD_TYPE_NONE, 1707 /*current_error*/ 1, 1708 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1709 /*asc*/ 0x25, 1710 /*ascq*/ 0x00, 1711 SSD_ELEM_NONE); 1712 1713 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1714 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1715 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1716 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1717 msg_info.hdr.serializing_sc = NULL; 1718 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1719 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1720 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1721 } 1722 if (have_lock == 0) 1723 mtx_unlock(&ctl_softc->ctl_lock); 1724 return(1); 1725 1726 } 1727 1728 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1729 1730 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1731 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1732 ooa_links))) { 1733 case CTL_ACTION_BLOCK: 1734 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1735 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1736 blocked_links); 1737 break; 1738 case CTL_ACTION_PASS: 1739 case CTL_ACTION_SKIP: 1740 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1741 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1742 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 1743 &ctsio->io_hdr, links); 1744 } else { 1745 1746 /* send msg back to other side */ 1747 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1748 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1749 msg_info.hdr.msg_type = CTL_MSG_R2R; 1750#if 0 1751 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1752#endif 1753 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1754 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1755 } 1756 } 1757 break; 1758 case CTL_ACTION_OVERLAP: 1759 /* OVERLAPPED COMMANDS ATTEMPTED */ 1760 ctl_set_sense_data(&msg_info.scsi.sense_data, 1761 lun, 1762 /*sense_format*/SSD_TYPE_NONE, 1763 /*current_error*/ 1, 1764 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1765 /*asc*/ 0x4E, 1766 /*ascq*/ 0x00, 1767 SSD_ELEM_NONE); 1768 1769 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1770 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1771 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1772 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1773 msg_info.hdr.serializing_sc = NULL; 1774 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1775#if 0 1776 printf("BAD JUJU:Major Bummer Overlap\n"); 1777#endif 1778 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1779 retval = 1; 1780 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1781 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1782 } 1783 break; 1784 case CTL_ACTION_OVERLAP_TAG: 1785 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1786 ctl_set_sense_data(&msg_info.scsi.sense_data, 1787 lun, 1788 /*sense_format*/SSD_TYPE_NONE, 1789 /*current_error*/ 1, 1790 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1791 /*asc*/ 0x4D, 1792 /*ascq*/ ctsio->tag_num & 0xff, 1793 SSD_ELEM_NONE); 1794 1795 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1796 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1797 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1798 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1799 msg_info.hdr.serializing_sc = NULL; 1800 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1801#if 0 1802 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1803#endif 1804 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1805 retval = 1; 1806 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1807 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1808 } 1809 break; 1810 case CTL_ACTION_ERROR: 1811 default: 1812 /* "Internal target failure" */ 1813 ctl_set_sense_data(&msg_info.scsi.sense_data, 1814 lun, 1815 /*sense_format*/SSD_TYPE_NONE, 1816 /*current_error*/ 1, 1817 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1818 /*asc*/ 0x44, 1819 /*ascq*/ 0x00, 1820 SSD_ELEM_NONE); 1821 1822 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1823 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1824 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1825 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1826 msg_info.hdr.serializing_sc = NULL; 1827 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1828#if 0 1829 printf("BAD JUJU:Major Bummer HW Error\n"); 1830#endif 1831 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1832 retval = 1; 1833 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1834 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1835 } 1836 break; 1837 } 1838 if (have_lock == 0) 1839 mtx_unlock(&ctl_softc->ctl_lock); 1840 return (retval); 1841} 1842 1843static int 1844ctl_ioctl_submit_wait(union ctl_io *io) 1845{ 1846 struct ctl_fe_ioctl_params params; 1847 ctl_fe_ioctl_state last_state; 1848 int done, retval; 1849 1850 retval = 0; 1851 1852 bzero(¶ms, sizeof(params)); 1853 1854 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1855 cv_init(¶ms.sem, "ctlioccv"); 1856 params.state = CTL_IOCTL_INPROG; 1857 last_state = params.state; 1858 1859 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1860 1861 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1862 1863 /* This shouldn't happen */ 1864 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1865 return (retval); 1866 1867 done = 0; 1868 1869 do { 1870 mtx_lock(¶ms.ioctl_mtx); 1871 /* 1872 * Check the state here, and don't sleep if the state has 1873 * already changed (i.e. wakeup has already occured, but we 1874 * weren't waiting yet). 1875 */ 1876 if (params.state == last_state) { 1877 /* XXX KDM cv_wait_sig instead? */ 1878 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1879 } 1880 last_state = params.state; 1881 1882 switch (params.state) { 1883 case CTL_IOCTL_INPROG: 1884 /* Why did we wake up? */ 1885 /* XXX KDM error here? */ 1886 mtx_unlock(¶ms.ioctl_mtx); 1887 break; 1888 case CTL_IOCTL_DATAMOVE: 1889 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1890 1891 /* 1892 * change last_state back to INPROG to avoid 1893 * deadlock on subsequent data moves. 1894 */ 1895 params.state = last_state = CTL_IOCTL_INPROG; 1896 1897 mtx_unlock(¶ms.ioctl_mtx); 1898 ctl_ioctl_do_datamove(&io->scsiio); 1899 /* 1900 * Note that in some cases, most notably writes, 1901 * this will queue the I/O and call us back later. 1902 * In other cases, generally reads, this routine 1903 * will immediately call back and wake us up, 1904 * probably using our own context. 1905 */ 1906 io->scsiio.be_move_done(io); 1907 break; 1908 case CTL_IOCTL_DONE: 1909 mtx_unlock(¶ms.ioctl_mtx); 1910 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1911 done = 1; 1912 break; 1913 default: 1914 mtx_unlock(¶ms.ioctl_mtx); 1915 /* XXX KDM error here? */ 1916 break; 1917 } 1918 } while (done == 0); 1919 1920 mtx_destroy(¶ms.ioctl_mtx); 1921 cv_destroy(¶ms.sem); 1922 1923 return (CTL_RETVAL_COMPLETE); 1924} 1925 1926static void 1927ctl_ioctl_datamove(union ctl_io *io) 1928{ 1929 struct ctl_fe_ioctl_params *params; 1930 1931 params = (struct ctl_fe_ioctl_params *) 1932 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1933 1934 mtx_lock(¶ms->ioctl_mtx); 1935 params->state = CTL_IOCTL_DATAMOVE; 1936 cv_broadcast(¶ms->sem); 1937 mtx_unlock(¶ms->ioctl_mtx); 1938} 1939 1940static void 1941ctl_ioctl_done(union ctl_io *io) 1942{ 1943 struct ctl_fe_ioctl_params *params; 1944 1945 params = (struct ctl_fe_ioctl_params *) 1946 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1947 1948 mtx_lock(¶ms->ioctl_mtx); 1949 params->state = CTL_IOCTL_DONE; 1950 cv_broadcast(¶ms->sem); 1951 mtx_unlock(¶ms->ioctl_mtx); 1952} 1953 1954static void 1955ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 1956{ 1957 struct ctl_fe_ioctl_startstop_info *sd_info; 1958 1959 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 1960 1961 sd_info->hs_info.status = metatask->status; 1962 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 1963 sd_info->hs_info.luns_complete = 1964 metatask->taskinfo.startstop.luns_complete; 1965 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 1966 1967 cv_broadcast(&sd_info->sem); 1968} 1969 1970static void 1971ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 1972{ 1973 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 1974 1975 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 1976 1977 mtx_lock(fe_bbr_info->lock); 1978 fe_bbr_info->bbr_info->status = metatask->status; 1979 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 1980 fe_bbr_info->wakeup_done = 1; 1981 mtx_unlock(fe_bbr_info->lock); 1982 1983 cv_broadcast(&fe_bbr_info->sem); 1984} 1985 1986/* 1987 * Must be called with the ctl_lock held. 1988 * Returns 0 for success, errno for failure. 1989 */ 1990static int 1991ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1992 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1993{ 1994 union ctl_io *io; 1995 int retval; 1996 1997 retval = 0; 1998 1999 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2000 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2001 ooa_links)) { 2002 struct ctl_ooa_entry *entry; 2003 2004 /* 2005 * If we've got more than we can fit, just count the 2006 * remaining entries. 2007 */ 2008 if (*cur_fill_num >= ooa_hdr->alloc_num) 2009 continue; 2010 2011 entry = &kern_entries[*cur_fill_num]; 2012 2013 entry->tag_num = io->scsiio.tag_num; 2014 entry->lun_num = lun->lun; 2015#ifdef CTL_TIME_IO 2016 entry->start_bt = io->io_hdr.start_bt; 2017#endif 2018 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2019 entry->cdb_len = io->scsiio.cdb_len; 2020 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2021 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2022 2023 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2024 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2025 2026 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2027 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2028 2029 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2030 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2031 2032 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2033 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2034 } 2035 2036 return (retval); 2037} 2038 2039static void * 2040ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2041 size_t error_str_len) 2042{ 2043 void *kptr; 2044 2045 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2046 2047 if (copyin(user_addr, kptr, len) != 0) { 2048 snprintf(error_str, error_str_len, "Error copying %d bytes " 2049 "from user address %p to kernel address %p", len, 2050 user_addr, kptr); 2051 free(kptr, M_CTL); 2052 return (NULL); 2053 } 2054 2055 return (kptr); 2056} 2057 2058static void 2059ctl_free_args(int num_be_args, struct ctl_be_arg *be_args) 2060{ 2061 int i; 2062 2063 if (be_args == NULL) 2064 return; 2065 2066 for (i = 0; i < num_be_args; i++) { 2067 free(be_args[i].kname, M_CTL); 2068 free(be_args[i].kvalue, M_CTL); 2069 } 2070 2071 free(be_args, M_CTL); 2072} 2073 2074static struct ctl_be_arg * 2075ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args, 2076 char *error_str, size_t error_str_len) 2077{ 2078 struct ctl_be_arg *args; 2079 int i; 2080 2081 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args), 2082 error_str, error_str_len); 2083 2084 if (args == NULL) 2085 goto bailout; 2086 2087 for (i = 0; i < num_be_args; i++) { 2088 args[i].kname = NULL; 2089 args[i].kvalue = NULL; 2090 } 2091 2092 for (i = 0; i < num_be_args; i++) { 2093 uint8_t *tmpptr; 2094 2095 args[i].kname = ctl_copyin_alloc(args[i].name, 2096 args[i].namelen, error_str, error_str_len); 2097 if (args[i].kname == NULL) 2098 goto bailout; 2099 2100 if (args[i].kname[args[i].namelen - 1] != '\0') { 2101 snprintf(error_str, error_str_len, "Argument %d " 2102 "name is not NUL-terminated", i); 2103 goto bailout; 2104 } 2105 2106 args[i].kvalue = NULL; 2107 2108 tmpptr = ctl_copyin_alloc(args[i].value, 2109 args[i].vallen, error_str, error_str_len); 2110 if (tmpptr == NULL) 2111 goto bailout; 2112 2113 args[i].kvalue = tmpptr; 2114 2115 if ((args[i].flags & CTL_BEARG_ASCII) 2116 && (tmpptr[args[i].vallen - 1] != '\0')) { 2117 snprintf(error_str, error_str_len, "Argument %d " 2118 "value is not NUL-terminated", i); 2119 goto bailout; 2120 } 2121 } 2122 2123 return (args); 2124bailout: 2125 2126 ctl_free_args(num_be_args, args); 2127 2128 return (NULL); 2129} 2130 2131/* 2132 * Escape characters that are illegal or not recommended in XML. 2133 */ 2134int 2135ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2136{ 2137 int retval; 2138 2139 retval = 0; 2140 2141 for (; *str; str++) { 2142 switch (*str) { 2143 case '&': 2144 retval = sbuf_printf(sb, "&"); 2145 break; 2146 case '>': 2147 retval = sbuf_printf(sb, ">"); 2148 break; 2149 case '<': 2150 retval = sbuf_printf(sb, "<"); 2151 break; 2152 default: 2153 retval = sbuf_putc(sb, *str); 2154 break; 2155 } 2156 2157 if (retval != 0) 2158 break; 2159 2160 } 2161 2162 return (retval); 2163} 2164 2165static int 2166ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2167 struct thread *td) 2168{ 2169 struct ctl_softc *softc; 2170 int retval; 2171 2172 softc = control_softc; 2173 2174 retval = 0; 2175 2176 switch (cmd) { 2177 case CTL_IO: { 2178 union ctl_io *io; 2179 void *pool_tmp; 2180 2181 /* 2182 * If we haven't been "enabled", don't allow any SCSI I/O 2183 * to this FETD. 2184 */ 2185 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2186 retval = -EPERM; 2187 break; 2188 } 2189 2190 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref); 2191 if (io == NULL) { 2192 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2193 retval = -ENOSPC; 2194 break; 2195 } 2196 2197 /* 2198 * Need to save the pool reference so it doesn't get 2199 * spammed by the user's ctl_io. 2200 */ 2201 pool_tmp = io->io_hdr.pool; 2202 2203 memcpy(io, (void *)addr, sizeof(*io)); 2204 2205 io->io_hdr.pool = pool_tmp; 2206 /* 2207 * No status yet, so make sure the status is set properly. 2208 */ 2209 io->io_hdr.status = CTL_STATUS_NONE; 2210 2211 /* 2212 * The user sets the initiator ID, target and LUN IDs. 2213 */ 2214 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port; 2215 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2216 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2217 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2218 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2219 2220 retval = ctl_ioctl_submit_wait(io); 2221 2222 if (retval != 0) { 2223 ctl_free_io(io); 2224 break; 2225 } 2226 2227 memcpy((void *)addr, io, sizeof(*io)); 2228 2229 /* return this to our pool */ 2230 ctl_free_io(io); 2231 2232 break; 2233 } 2234 case CTL_ENABLE_PORT: 2235 case CTL_DISABLE_PORT: 2236 case CTL_SET_PORT_WWNS: { 2237 struct ctl_frontend *fe; 2238 struct ctl_port_entry *entry; 2239 2240 entry = (struct ctl_port_entry *)addr; 2241 2242 mtx_lock(&softc->ctl_lock); 2243 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2244 int action, done; 2245 2246 action = 0; 2247 done = 0; 2248 2249 if ((entry->port_type == CTL_PORT_NONE) 2250 && (entry->targ_port == fe->targ_port)) { 2251 /* 2252 * If the user only wants to enable or 2253 * disable or set WWNs on a specific port, 2254 * do the operation and we're done. 2255 */ 2256 action = 1; 2257 done = 1; 2258 } else if (entry->port_type & fe->port_type) { 2259 /* 2260 * Compare the user's type mask with the 2261 * particular frontend type to see if we 2262 * have a match. 2263 */ 2264 action = 1; 2265 done = 0; 2266 2267 /* 2268 * Make sure the user isn't trying to set 2269 * WWNs on multiple ports at the same time. 2270 */ 2271 if (cmd == CTL_SET_PORT_WWNS) { 2272 printf("%s: Can't set WWNs on " 2273 "multiple ports\n", __func__); 2274 retval = EINVAL; 2275 break; 2276 } 2277 } 2278 if (action != 0) { 2279 /* 2280 * XXX KDM we have to drop the lock here, 2281 * because the online/offline operations 2282 * can potentially block. We need to 2283 * reference count the frontends so they 2284 * can't go away, 2285 */ 2286 mtx_unlock(&softc->ctl_lock); 2287 2288 if (cmd == CTL_ENABLE_PORT) { 2289 struct ctl_lun *lun; 2290 2291 STAILQ_FOREACH(lun, &softc->lun_list, 2292 links) { 2293 fe->lun_enable(fe->targ_lun_arg, 2294 lun->target, 2295 lun->lun); 2296 } 2297 2298 ctl_frontend_online(fe); 2299 } else if (cmd == CTL_DISABLE_PORT) { 2300 struct ctl_lun *lun; 2301 2302 ctl_frontend_offline(fe); 2303 2304 STAILQ_FOREACH(lun, &softc->lun_list, 2305 links) { 2306 fe->lun_disable( 2307 fe->targ_lun_arg, 2308 lun->target, 2309 lun->lun); 2310 } 2311 } 2312 2313 mtx_lock(&softc->ctl_lock); 2314 2315 if (cmd == CTL_SET_PORT_WWNS) 2316 ctl_frontend_set_wwns(fe, 2317 (entry->flags & CTL_PORT_WWNN_VALID) ? 2318 1 : 0, entry->wwnn, 2319 (entry->flags & CTL_PORT_WWPN_VALID) ? 2320 1 : 0, entry->wwpn); 2321 } 2322 if (done != 0) 2323 break; 2324 } 2325 mtx_unlock(&softc->ctl_lock); 2326 break; 2327 } 2328 case CTL_GET_PORT_LIST: { 2329 struct ctl_frontend *fe; 2330 struct ctl_port_list *list; 2331 int i; 2332 2333 list = (struct ctl_port_list *)addr; 2334 2335 if (list->alloc_len != (list->alloc_num * 2336 sizeof(struct ctl_port_entry))) { 2337 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2338 "alloc_num %u * sizeof(struct ctl_port_entry) " 2339 "%zu\n", __func__, list->alloc_len, 2340 list->alloc_num, sizeof(struct ctl_port_entry)); 2341 retval = EINVAL; 2342 break; 2343 } 2344 list->fill_len = 0; 2345 list->fill_num = 0; 2346 list->dropped_num = 0; 2347 i = 0; 2348 mtx_lock(&softc->ctl_lock); 2349 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2350 struct ctl_port_entry entry, *list_entry; 2351 2352 if (list->fill_num >= list->alloc_num) { 2353 list->dropped_num++; 2354 continue; 2355 } 2356 2357 entry.port_type = fe->port_type; 2358 strlcpy(entry.port_name, fe->port_name, 2359 sizeof(entry.port_name)); 2360 entry.targ_port = fe->targ_port; 2361 entry.physical_port = fe->physical_port; 2362 entry.virtual_port = fe->virtual_port; 2363 entry.wwnn = fe->wwnn; 2364 entry.wwpn = fe->wwpn; 2365 if (fe->status & CTL_PORT_STATUS_ONLINE) 2366 entry.online = 1; 2367 else 2368 entry.online = 0; 2369 2370 list_entry = &list->entries[i]; 2371 2372 retval = copyout(&entry, list_entry, sizeof(entry)); 2373 if (retval != 0) { 2374 printf("%s: CTL_GET_PORT_LIST: copyout " 2375 "returned %d\n", __func__, retval); 2376 break; 2377 } 2378 i++; 2379 list->fill_num++; 2380 list->fill_len += sizeof(entry); 2381 } 2382 mtx_unlock(&softc->ctl_lock); 2383 2384 /* 2385 * If this is non-zero, we had a copyout fault, so there's 2386 * probably no point in attempting to set the status inside 2387 * the structure. 2388 */ 2389 if (retval != 0) 2390 break; 2391 2392 if (list->dropped_num > 0) 2393 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2394 else 2395 list->status = CTL_PORT_LIST_OK; 2396 break; 2397 } 2398 case CTL_DUMP_OOA: { 2399 struct ctl_lun *lun; 2400 union ctl_io *io; 2401 char printbuf[128]; 2402 struct sbuf sb; 2403 2404 mtx_lock(&softc->ctl_lock); 2405 printf("Dumping OOA queues:\n"); 2406 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2407 for (io = (union ctl_io *)TAILQ_FIRST( 2408 &lun->ooa_queue); io != NULL; 2409 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2410 ooa_links)) { 2411 sbuf_new(&sb, printbuf, sizeof(printbuf), 2412 SBUF_FIXEDLEN); 2413 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2414 (intmax_t)lun->lun, 2415 io->scsiio.tag_num, 2416 (io->io_hdr.flags & 2417 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2418 (io->io_hdr.flags & 2419 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2420 (io->io_hdr.flags & 2421 CTL_FLAG_ABORT) ? " ABORT" : "", 2422 (io->io_hdr.flags & 2423 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2424 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2425 sbuf_finish(&sb); 2426 printf("%s\n", sbuf_data(&sb)); 2427 } 2428 } 2429 printf("OOA queues dump done\n"); 2430 mtx_unlock(&softc->ctl_lock); 2431 break; 2432 } 2433 case CTL_GET_OOA: { 2434 struct ctl_lun *lun; 2435 struct ctl_ooa *ooa_hdr; 2436 struct ctl_ooa_entry *entries; 2437 uint32_t cur_fill_num; 2438 2439 ooa_hdr = (struct ctl_ooa *)addr; 2440 2441 if ((ooa_hdr->alloc_len == 0) 2442 || (ooa_hdr->alloc_num == 0)) { 2443 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2444 "must be non-zero\n", __func__, 2445 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2446 retval = EINVAL; 2447 break; 2448 } 2449 2450 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2451 sizeof(struct ctl_ooa_entry))) { 2452 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2453 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2454 __func__, ooa_hdr->alloc_len, 2455 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2456 retval = EINVAL; 2457 break; 2458 } 2459 2460 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2461 if (entries == NULL) { 2462 printf("%s: could not allocate %d bytes for OOA " 2463 "dump\n", __func__, ooa_hdr->alloc_len); 2464 retval = ENOMEM; 2465 break; 2466 } 2467 2468 mtx_lock(&softc->ctl_lock); 2469 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2470 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2471 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2472 mtx_unlock(&softc->ctl_lock); 2473 free(entries, M_CTL); 2474 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2475 __func__, (uintmax_t)ooa_hdr->lun_num); 2476 retval = EINVAL; 2477 break; 2478 } 2479 2480 cur_fill_num = 0; 2481 2482 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2483 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2484 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2485 ooa_hdr, entries); 2486 if (retval != 0) 2487 break; 2488 } 2489 if (retval != 0) { 2490 mtx_unlock(&softc->ctl_lock); 2491 free(entries, M_CTL); 2492 break; 2493 } 2494 } else { 2495 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2496 2497 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2498 entries); 2499 } 2500 mtx_unlock(&softc->ctl_lock); 2501 2502 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2503 ooa_hdr->fill_len = ooa_hdr->fill_num * 2504 sizeof(struct ctl_ooa_entry); 2505 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2506 if (retval != 0) { 2507 printf("%s: error copying out %d bytes for OOA dump\n", 2508 __func__, ooa_hdr->fill_len); 2509 } 2510 2511 getbintime(&ooa_hdr->cur_bt); 2512 2513 if (cur_fill_num > ooa_hdr->alloc_num) { 2514 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2515 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2516 } else { 2517 ooa_hdr->dropped_num = 0; 2518 ooa_hdr->status = CTL_OOA_OK; 2519 } 2520 2521 free(entries, M_CTL); 2522 break; 2523 } 2524 case CTL_CHECK_OOA: { 2525 union ctl_io *io; 2526 struct ctl_lun *lun; 2527 struct ctl_ooa_info *ooa_info; 2528 2529 2530 ooa_info = (struct ctl_ooa_info *)addr; 2531 2532 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2533 ooa_info->status = CTL_OOA_INVALID_LUN; 2534 break; 2535 } 2536 mtx_lock(&softc->ctl_lock); 2537 lun = softc->ctl_luns[ooa_info->lun_id]; 2538 if (lun == NULL) { 2539 mtx_unlock(&softc->ctl_lock); 2540 ooa_info->status = CTL_OOA_INVALID_LUN; 2541 break; 2542 } 2543 2544 ooa_info->num_entries = 0; 2545 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2546 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2547 &io->io_hdr, ooa_links)) { 2548 ooa_info->num_entries++; 2549 } 2550 2551 mtx_unlock(&softc->ctl_lock); 2552 ooa_info->status = CTL_OOA_SUCCESS; 2553 2554 break; 2555 } 2556 case CTL_HARD_START: 2557 case CTL_HARD_STOP: { 2558 struct ctl_fe_ioctl_startstop_info ss_info; 2559 struct cfi_metatask *metatask; 2560 struct mtx hs_mtx; 2561 2562 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2563 2564 cv_init(&ss_info.sem, "hard start/stop cv" ); 2565 2566 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2567 if (metatask == NULL) { 2568 retval = ENOMEM; 2569 mtx_destroy(&hs_mtx); 2570 break; 2571 } 2572 2573 if (cmd == CTL_HARD_START) 2574 metatask->tasktype = CFI_TASK_STARTUP; 2575 else 2576 metatask->tasktype = CFI_TASK_SHUTDOWN; 2577 2578 metatask->callback = ctl_ioctl_hard_startstop_callback; 2579 metatask->callback_arg = &ss_info; 2580 2581 cfi_action(metatask); 2582 2583 /* Wait for the callback */ 2584 mtx_lock(&hs_mtx); 2585 cv_wait_sig(&ss_info.sem, &hs_mtx); 2586 mtx_unlock(&hs_mtx); 2587 2588 /* 2589 * All information has been copied from the metatask by the 2590 * time cv_broadcast() is called, so we free the metatask here. 2591 */ 2592 cfi_free_metatask(metatask); 2593 2594 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2595 2596 mtx_destroy(&hs_mtx); 2597 break; 2598 } 2599 case CTL_BBRREAD: { 2600 struct ctl_bbrread_info *bbr_info; 2601 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2602 struct mtx bbr_mtx; 2603 struct cfi_metatask *metatask; 2604 2605 bbr_info = (struct ctl_bbrread_info *)addr; 2606 2607 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2608 2609 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2610 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2611 2612 fe_bbr_info.bbr_info = bbr_info; 2613 fe_bbr_info.lock = &bbr_mtx; 2614 2615 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2616 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2617 2618 if (metatask == NULL) { 2619 mtx_destroy(&bbr_mtx); 2620 cv_destroy(&fe_bbr_info.sem); 2621 retval = ENOMEM; 2622 break; 2623 } 2624 metatask->tasktype = CFI_TASK_BBRREAD; 2625 metatask->callback = ctl_ioctl_bbrread_callback; 2626 metatask->callback_arg = &fe_bbr_info; 2627 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2628 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2629 metatask->taskinfo.bbrread.len = bbr_info->len; 2630 2631 cfi_action(metatask); 2632 2633 mtx_lock(&bbr_mtx); 2634 while (fe_bbr_info.wakeup_done == 0) 2635 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2636 mtx_unlock(&bbr_mtx); 2637 2638 bbr_info->status = metatask->status; 2639 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2640 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2641 memcpy(&bbr_info->sense_data, 2642 &metatask->taskinfo.bbrread.sense_data, 2643 ctl_min(sizeof(bbr_info->sense_data), 2644 sizeof(metatask->taskinfo.bbrread.sense_data))); 2645 2646 cfi_free_metatask(metatask); 2647 2648 mtx_destroy(&bbr_mtx); 2649 cv_destroy(&fe_bbr_info.sem); 2650 2651 break; 2652 } 2653 case CTL_DELAY_IO: { 2654 struct ctl_io_delay_info *delay_info; 2655#ifdef CTL_IO_DELAY 2656 struct ctl_lun *lun; 2657#endif /* CTL_IO_DELAY */ 2658 2659 delay_info = (struct ctl_io_delay_info *)addr; 2660 2661#ifdef CTL_IO_DELAY 2662 mtx_lock(&softc->ctl_lock); 2663 2664 if ((delay_info->lun_id > CTL_MAX_LUNS) 2665 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2666 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2667 } else { 2668 lun = softc->ctl_luns[delay_info->lun_id]; 2669 2670 delay_info->status = CTL_DELAY_STATUS_OK; 2671 2672 switch (delay_info->delay_type) { 2673 case CTL_DELAY_TYPE_CONT: 2674 break; 2675 case CTL_DELAY_TYPE_ONESHOT: 2676 break; 2677 default: 2678 delay_info->status = 2679 CTL_DELAY_STATUS_INVALID_TYPE; 2680 break; 2681 } 2682 2683 switch (delay_info->delay_loc) { 2684 case CTL_DELAY_LOC_DATAMOVE: 2685 lun->delay_info.datamove_type = 2686 delay_info->delay_type; 2687 lun->delay_info.datamove_delay = 2688 delay_info->delay_secs; 2689 break; 2690 case CTL_DELAY_LOC_DONE: 2691 lun->delay_info.done_type = 2692 delay_info->delay_type; 2693 lun->delay_info.done_delay = 2694 delay_info->delay_secs; 2695 break; 2696 default: 2697 delay_info->status = 2698 CTL_DELAY_STATUS_INVALID_LOC; 2699 break; 2700 } 2701 } 2702 2703 mtx_unlock(&softc->ctl_lock); 2704#else 2705 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2706#endif /* CTL_IO_DELAY */ 2707 break; 2708 } 2709 case CTL_REALSYNC_SET: { 2710 int *syncstate; 2711 2712 syncstate = (int *)addr; 2713 2714 mtx_lock(&softc->ctl_lock); 2715 switch (*syncstate) { 2716 case 0: 2717 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2718 break; 2719 case 1: 2720 softc->flags |= CTL_FLAG_REAL_SYNC; 2721 break; 2722 default: 2723 retval = -EINVAL; 2724 break; 2725 } 2726 mtx_unlock(&softc->ctl_lock); 2727 break; 2728 } 2729 case CTL_REALSYNC_GET: { 2730 int *syncstate; 2731 2732 syncstate = (int*)addr; 2733 2734 mtx_lock(&softc->ctl_lock); 2735 if (softc->flags & CTL_FLAG_REAL_SYNC) 2736 *syncstate = 1; 2737 else 2738 *syncstate = 0; 2739 mtx_unlock(&softc->ctl_lock); 2740 2741 break; 2742 } 2743 case CTL_SETSYNC: 2744 case CTL_GETSYNC: { 2745 struct ctl_sync_info *sync_info; 2746 struct ctl_lun *lun; 2747 2748 sync_info = (struct ctl_sync_info *)addr; 2749 2750 mtx_lock(&softc->ctl_lock); 2751 lun = softc->ctl_luns[sync_info->lun_id]; 2752 if (lun == NULL) { 2753 mtx_unlock(&softc->ctl_lock); 2754 sync_info->status = CTL_GS_SYNC_NO_LUN; 2755 } 2756 /* 2757 * Get or set the sync interval. We're not bounds checking 2758 * in the set case, hopefully the user won't do something 2759 * silly. 2760 */ 2761 if (cmd == CTL_GETSYNC) 2762 sync_info->sync_interval = lun->sync_interval; 2763 else 2764 lun->sync_interval = sync_info->sync_interval; 2765 2766 mtx_unlock(&softc->ctl_lock); 2767 2768 sync_info->status = CTL_GS_SYNC_OK; 2769 2770 break; 2771 } 2772 case CTL_GETSTATS: { 2773 struct ctl_stats *stats; 2774 struct ctl_lun *lun; 2775 int i; 2776 2777 stats = (struct ctl_stats *)addr; 2778 2779 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2780 stats->alloc_len) { 2781 stats->status = CTL_SS_NEED_MORE_SPACE; 2782 stats->num_luns = softc->num_luns; 2783 break; 2784 } 2785 /* 2786 * XXX KDM no locking here. If the LUN list changes, 2787 * things can blow up. 2788 */ 2789 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2790 i++, lun = STAILQ_NEXT(lun, links)) { 2791 retval = copyout(&lun->stats, &stats->lun_stats[i], 2792 sizeof(lun->stats)); 2793 if (retval != 0) 2794 break; 2795 } 2796 stats->num_luns = softc->num_luns; 2797 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2798 softc->num_luns; 2799 stats->status = CTL_SS_OK; 2800#ifdef CTL_TIME_IO 2801 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2802#else 2803 stats->flags = CTL_STATS_FLAG_NONE; 2804#endif 2805 getnanouptime(&stats->timestamp); 2806 break; 2807 } 2808 case CTL_ERROR_INJECT: { 2809 struct ctl_error_desc *err_desc, *new_err_desc; 2810 struct ctl_lun *lun; 2811 2812 err_desc = (struct ctl_error_desc *)addr; 2813 2814 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2815 M_WAITOK | M_ZERO); 2816 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2817 2818 mtx_lock(&softc->ctl_lock); 2819 lun = softc->ctl_luns[err_desc->lun_id]; 2820 if (lun == NULL) { 2821 mtx_unlock(&softc->ctl_lock); 2822 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2823 __func__, (uintmax_t)err_desc->lun_id); 2824 retval = EINVAL; 2825 break; 2826 } 2827 2828 /* 2829 * We could do some checking here to verify the validity 2830 * of the request, but given the complexity of error 2831 * injection requests, the checking logic would be fairly 2832 * complex. 2833 * 2834 * For now, if the request is invalid, it just won't get 2835 * executed and might get deleted. 2836 */ 2837 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2838 2839 /* 2840 * XXX KDM check to make sure the serial number is unique, 2841 * in case we somehow manage to wrap. That shouldn't 2842 * happen for a very long time, but it's the right thing to 2843 * do. 2844 */ 2845 new_err_desc->serial = lun->error_serial; 2846 err_desc->serial = lun->error_serial; 2847 lun->error_serial++; 2848 2849 mtx_unlock(&softc->ctl_lock); 2850 break; 2851 } 2852 case CTL_ERROR_INJECT_DELETE: { 2853 struct ctl_error_desc *delete_desc, *desc, *desc2; 2854 struct ctl_lun *lun; 2855 int delete_done; 2856 2857 delete_desc = (struct ctl_error_desc *)addr; 2858 delete_done = 0; 2859 2860 mtx_lock(&softc->ctl_lock); 2861 lun = softc->ctl_luns[delete_desc->lun_id]; 2862 if (lun == NULL) { 2863 mtx_unlock(&softc->ctl_lock); 2864 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2865 __func__, (uintmax_t)delete_desc->lun_id); 2866 retval = EINVAL; 2867 break; 2868 } 2869 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2870 if (desc->serial != delete_desc->serial) 2871 continue; 2872 2873 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2874 links); 2875 free(desc, M_CTL); 2876 delete_done = 1; 2877 } 2878 mtx_unlock(&softc->ctl_lock); 2879 if (delete_done == 0) { 2880 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2881 "error serial %ju on LUN %u\n", __func__, 2882 delete_desc->serial, delete_desc->lun_id); 2883 retval = EINVAL; 2884 break; 2885 } 2886 break; 2887 } 2888 case CTL_DUMP_STRUCTS: { 2889 int i, j, k; 2890 struct ctl_frontend *fe; 2891 2892 printf("CTL IID to WWPN map start:\n"); 2893 for (i = 0; i < CTL_MAX_PORTS; i++) { 2894 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2895 if (softc->wwpn_iid[i][j].in_use == 0) 2896 continue; 2897 2898 printf("port %d iid %u WWPN %#jx\n", 2899 softc->wwpn_iid[i][j].port, 2900 softc->wwpn_iid[i][j].iid, 2901 (uintmax_t)softc->wwpn_iid[i][j].wwpn); 2902 } 2903 } 2904 printf("CTL IID to WWPN map end\n"); 2905 printf("CTL Persistent Reservation information start:\n"); 2906 for (i = 0; i < CTL_MAX_LUNS; i++) { 2907 struct ctl_lun *lun; 2908 2909 lun = softc->ctl_luns[i]; 2910 2911 if ((lun == NULL) 2912 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2913 continue; 2914 2915 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2916 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2917 if (lun->per_res[j+k].registered == 0) 2918 continue; 2919 printf("LUN %d port %d iid %d key " 2920 "%#jx\n", i, j, k, 2921 (uintmax_t)scsi_8btou64( 2922 lun->per_res[j+k].res_key.key)); 2923 } 2924 } 2925 } 2926 printf("CTL Persistent Reservation information end\n"); 2927 printf("CTL Frontends:\n"); 2928 /* 2929 * XXX KDM calling this without a lock. We'd likely want 2930 * to drop the lock before calling the frontend's dump 2931 * routine anyway. 2932 */ 2933 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2934 printf("Frontend %s Type %u pport %d vport %d WWNN " 2935 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type, 2936 fe->physical_port, fe->virtual_port, 2937 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn); 2938 2939 /* 2940 * Frontends are not required to support the dump 2941 * routine. 2942 */ 2943 if (fe->fe_dump == NULL) 2944 continue; 2945 2946 fe->fe_dump(); 2947 } 2948 printf("CTL Frontend information end\n"); 2949 break; 2950 } 2951 case CTL_LUN_REQ: { 2952 struct ctl_lun_req *lun_req; 2953 struct ctl_backend_driver *backend; 2954 2955 lun_req = (struct ctl_lun_req *)addr; 2956 2957 backend = ctl_backend_find(lun_req->backend); 2958 if (backend == NULL) { 2959 lun_req->status = CTL_LUN_ERROR; 2960 snprintf(lun_req->error_str, 2961 sizeof(lun_req->error_str), 2962 "Backend \"%s\" not found.", 2963 lun_req->backend); 2964 break; 2965 } 2966 if (lun_req->num_be_args > 0) { 2967 lun_req->kern_be_args = ctl_copyin_args( 2968 lun_req->num_be_args, 2969 lun_req->be_args, 2970 lun_req->error_str, 2971 sizeof(lun_req->error_str)); 2972 if (lun_req->kern_be_args == NULL) { 2973 lun_req->status = CTL_LUN_ERROR; 2974 break; 2975 } 2976 } 2977 2978 retval = backend->ioctl(dev, cmd, addr, flag, td); 2979 2980 if (lun_req->num_be_args > 0) { 2981 ctl_free_args(lun_req->num_be_args, 2982 lun_req->kern_be_args); 2983 } 2984 break; 2985 } 2986 case CTL_LUN_LIST: { 2987 struct sbuf *sb; 2988 struct ctl_lun *lun; 2989 struct ctl_lun_list *list; 2990 2991 list = (struct ctl_lun_list *)addr; 2992 2993 /* 2994 * Allocate a fixed length sbuf here, based on the length 2995 * of the user's buffer. We could allocate an auto-extending 2996 * buffer, and then tell the user how much larger our 2997 * amount of data is than his buffer, but that presents 2998 * some problems: 2999 * 3000 * 1. The sbuf(9) routines use a blocking malloc, and so 3001 * we can't hold a lock while calling them with an 3002 * auto-extending buffer. 3003 * 3004 * 2. There is not currently a LUN reference counting 3005 * mechanism, outside of outstanding transactions on 3006 * the LUN's OOA queue. So a LUN could go away on us 3007 * while we're getting the LUN number, backend-specific 3008 * information, etc. Thus, given the way things 3009 * currently work, we need to hold the CTL lock while 3010 * grabbing LUN information. 3011 * 3012 * So, from the user's standpoint, the best thing to do is 3013 * allocate what he thinks is a reasonable buffer length, 3014 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3015 * double the buffer length and try again. (And repeat 3016 * that until he succeeds.) 3017 */ 3018 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3019 if (sb == NULL) { 3020 list->status = CTL_LUN_LIST_ERROR; 3021 snprintf(list->error_str, sizeof(list->error_str), 3022 "Unable to allocate %d bytes for LUN list", 3023 list->alloc_len); 3024 break; 3025 } 3026 3027 sbuf_printf(sb, "<ctllunlist>\n"); 3028 3029 mtx_lock(&softc->ctl_lock); 3030 3031 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3032 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3033 (uintmax_t)lun->lun); 3034 3035 /* 3036 * Bail out as soon as we see that we've overfilled 3037 * the buffer. 3038 */ 3039 if (retval != 0) 3040 break; 3041 3042 retval = sbuf_printf(sb, "<backend_type>%s" 3043 "</backend_type>\n", 3044 (lun->backend == NULL) ? "none" : 3045 lun->backend->name); 3046 3047 if (retval != 0) 3048 break; 3049 3050 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n", 3051 lun->be_lun->lun_type); 3052 3053 if (retval != 0) 3054 break; 3055 3056 if (lun->backend == NULL) { 3057 retval = sbuf_printf(sb, "</lun>\n"); 3058 if (retval != 0) 3059 break; 3060 continue; 3061 } 3062 3063 retval = sbuf_printf(sb, "<size>%ju</size>\n", 3064 (lun->be_lun->maxlba > 0) ? 3065 lun->be_lun->maxlba + 1 : 0); 3066 3067 if (retval != 0) 3068 break; 3069 3070 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n", 3071 lun->be_lun->blocksize); 3072 3073 if (retval != 0) 3074 break; 3075 3076 retval = sbuf_printf(sb, "<serial_number>"); 3077 3078 if (retval != 0) 3079 break; 3080 3081 retval = ctl_sbuf_printf_esc(sb, 3082 lun->be_lun->serial_num); 3083 3084 if (retval != 0) 3085 break; 3086 3087 retval = sbuf_printf(sb, "</serial_number>\n"); 3088 3089 if (retval != 0) 3090 break; 3091 3092 retval = sbuf_printf(sb, "<device_id>"); 3093 3094 if (retval != 0) 3095 break; 3096 3097 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3098 3099 if (retval != 0) 3100 break; 3101 3102 retval = sbuf_printf(sb, "</device_id>\n"); 3103 3104 if (retval != 0) 3105 break; 3106 3107 if (lun->backend->lun_info == NULL) { 3108 retval = sbuf_printf(sb, "</lun>\n"); 3109 if (retval != 0) 3110 break; 3111 continue; 3112 } 3113 3114 retval =lun->backend->lun_info(lun->be_lun->be_lun, sb); 3115 3116 if (retval != 0) 3117 break; 3118 3119 retval = sbuf_printf(sb, "</lun>\n"); 3120 3121 if (retval != 0) 3122 break; 3123 } 3124 mtx_unlock(&softc->ctl_lock); 3125 3126 if ((retval != 0) 3127 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3128 retval = 0; 3129 sbuf_delete(sb); 3130 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3131 snprintf(list->error_str, sizeof(list->error_str), 3132 "Out of space, %d bytes is too small", 3133 list->alloc_len); 3134 break; 3135 } 3136 3137 sbuf_finish(sb); 3138 3139 retval = copyout(sbuf_data(sb), list->lun_xml, 3140 sbuf_len(sb) + 1); 3141 3142 list->fill_len = sbuf_len(sb) + 1; 3143 list->status = CTL_LUN_LIST_OK; 3144 sbuf_delete(sb); 3145 break; 3146 } 3147 default: { 3148 /* XXX KDM should we fix this? */ 3149#if 0 3150 struct ctl_backend_driver *backend; 3151 unsigned int type; 3152 int found; 3153 3154 found = 0; 3155 3156 /* 3157 * We encode the backend type as the ioctl type for backend 3158 * ioctls. So parse it out here, and then search for a 3159 * backend of this type. 3160 */ 3161 type = _IOC_TYPE(cmd); 3162 3163 STAILQ_FOREACH(backend, &softc->be_list, links) { 3164 if (backend->type == type) { 3165 found = 1; 3166 break; 3167 } 3168 } 3169 if (found == 0) { 3170 printf("ctl: unknown ioctl command %#lx or backend " 3171 "%d\n", cmd, type); 3172 retval = -EINVAL; 3173 break; 3174 } 3175 retval = backend->ioctl(dev, cmd, addr, flag, td); 3176#endif 3177 retval = ENOTTY; 3178 break; 3179 } 3180 } 3181 return (retval); 3182} 3183 3184uint32_t 3185ctl_get_initindex(struct ctl_nexus *nexus) 3186{ 3187 if (nexus->targ_port < CTL_MAX_PORTS) 3188 return (nexus->initid.id + 3189 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3190 else 3191 return (nexus->initid.id + 3192 ((nexus->targ_port - CTL_MAX_PORTS) * 3193 CTL_MAX_INIT_PER_PORT)); 3194} 3195 3196uint32_t 3197ctl_get_resindex(struct ctl_nexus *nexus) 3198{ 3199 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3200} 3201 3202uint32_t 3203ctl_port_idx(int port_num) 3204{ 3205 if (port_num < CTL_MAX_PORTS) 3206 return(port_num); 3207 else 3208 return(port_num - CTL_MAX_PORTS); 3209} 3210 3211/* 3212 * Note: This only works for bitmask sizes that are at least 32 bits, and 3213 * that are a power of 2. 3214 */ 3215int 3216ctl_ffz(uint32_t *mask, uint32_t size) 3217{ 3218 uint32_t num_chunks, num_pieces; 3219 int i, j; 3220 3221 num_chunks = (size >> 5); 3222 if (num_chunks == 0) 3223 num_chunks++; 3224 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3225 3226 for (i = 0; i < num_chunks; i++) { 3227 for (j = 0; j < num_pieces; j++) { 3228 if ((mask[i] & (1 << j)) == 0) 3229 return ((i << 5) + j); 3230 } 3231 } 3232 3233 return (-1); 3234} 3235 3236int 3237ctl_set_mask(uint32_t *mask, uint32_t bit) 3238{ 3239 uint32_t chunk, piece; 3240 3241 chunk = bit >> 5; 3242 piece = bit % (sizeof(uint32_t) * 8); 3243 3244 if ((mask[chunk] & (1 << piece)) != 0) 3245 return (-1); 3246 else 3247 mask[chunk] |= (1 << piece); 3248 3249 return (0); 3250} 3251 3252int 3253ctl_clear_mask(uint32_t *mask, uint32_t bit) 3254{ 3255 uint32_t chunk, piece; 3256 3257 chunk = bit >> 5; 3258 piece = bit % (sizeof(uint32_t) * 8); 3259 3260 if ((mask[chunk] & (1 << piece)) == 0) 3261 return (-1); 3262 else 3263 mask[chunk] &= ~(1 << piece); 3264 3265 return (0); 3266} 3267 3268int 3269ctl_is_set(uint32_t *mask, uint32_t bit) 3270{ 3271 uint32_t chunk, piece; 3272 3273 chunk = bit >> 5; 3274 piece = bit % (sizeof(uint32_t) * 8); 3275 3276 if ((mask[chunk] & (1 << piece)) == 0) 3277 return (0); 3278 else 3279 return (1); 3280} 3281 3282#ifdef unused 3283/* 3284 * The bus, target and lun are optional, they can be filled in later. 3285 * can_wait is used to determine whether we can wait on the malloc or not. 3286 */ 3287union ctl_io* 3288ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3289 uint32_t targ_lun, int can_wait) 3290{ 3291 union ctl_io *io; 3292 3293 if (can_wait) 3294 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3295 else 3296 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3297 3298 if (io != NULL) { 3299 io->io_hdr.io_type = io_type; 3300 io->io_hdr.targ_port = targ_port; 3301 /* 3302 * XXX KDM this needs to change/go away. We need to move 3303 * to a preallocated pool of ctl_scsiio structures. 3304 */ 3305 io->io_hdr.nexus.targ_target.id = targ_target; 3306 io->io_hdr.nexus.targ_lun = targ_lun; 3307 } 3308 3309 return (io); 3310} 3311 3312void 3313ctl_kfree_io(union ctl_io *io) 3314{ 3315 free(io, M_CTL); 3316} 3317#endif /* unused */ 3318 3319/* 3320 * ctl_softc, pool_type, total_ctl_io are passed in. 3321 * npool is passed out. 3322 */ 3323int 3324ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3325 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3326{ 3327 uint32_t i; 3328 union ctl_io *cur_io, *next_io; 3329 struct ctl_io_pool *pool; 3330 int retval; 3331 3332 retval = 0; 3333 3334 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3335 M_NOWAIT | M_ZERO); 3336 if (pool == NULL) { 3337 retval = -ENOMEM; 3338 goto bailout; 3339 } 3340 3341 pool->type = pool_type; 3342 pool->ctl_softc = ctl_softc; 3343 3344 mtx_lock(&ctl_softc->ctl_lock); 3345 pool->id = ctl_softc->cur_pool_id++; 3346 mtx_unlock(&ctl_softc->ctl_lock); 3347 3348 pool->flags = CTL_POOL_FLAG_NONE; 3349 STAILQ_INIT(&pool->free_queue); 3350 3351 /* 3352 * XXX KDM other options here: 3353 * - allocate a page at a time 3354 * - allocate one big chunk of memory. 3355 * Page allocation might work well, but would take a little more 3356 * tracking. 3357 */ 3358 for (i = 0; i < total_ctl_io; i++) { 3359 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL, 3360 M_NOWAIT); 3361 if (cur_io == NULL) { 3362 retval = ENOMEM; 3363 break; 3364 } 3365 cur_io->io_hdr.pool = pool; 3366 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3367 pool->total_ctl_io++; 3368 pool->free_ctl_io++; 3369 } 3370 3371 if (retval != 0) { 3372 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3373 cur_io != NULL; cur_io = next_io) { 3374 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3375 links); 3376 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3377 ctl_io_hdr, links); 3378 free(cur_io, M_CTL); 3379 } 3380 3381 free(pool, M_CTL); 3382 goto bailout; 3383 } 3384 mtx_lock(&ctl_softc->ctl_lock); 3385 ctl_softc->num_pools++; 3386 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3387 /* 3388 * Increment our usage count if this is an external consumer, so we 3389 * can't get unloaded until the external consumer (most likely a 3390 * FETD) unloads and frees his pool. 3391 * 3392 * XXX KDM will this increment the caller's module use count, or 3393 * mine? 3394 */ 3395#if 0 3396 if ((pool_type != CTL_POOL_EMERGENCY) 3397 && (pool_type != CTL_POOL_INTERNAL) 3398 && (pool_type != CTL_POOL_IOCTL) 3399 && (pool_type != CTL_POOL_4OTHERSC)) 3400 MOD_INC_USE_COUNT; 3401#endif 3402 3403 mtx_unlock(&ctl_softc->ctl_lock); 3404 3405 *npool = pool; 3406 3407bailout: 3408 3409 return (retval); 3410} 3411 3412/* 3413 * Caller must hold ctl_softc->ctl_lock. 3414 */ 3415int 3416ctl_pool_acquire(struct ctl_io_pool *pool) 3417{ 3418 if (pool == NULL) 3419 return (-EINVAL); 3420 3421 if (pool->flags & CTL_POOL_FLAG_INVALID) 3422 return (-EINVAL); 3423 3424 pool->refcount++; 3425 3426 return (0); 3427} 3428 3429/* 3430 * Caller must hold ctl_softc->ctl_lock. 3431 */ 3432int 3433ctl_pool_invalidate(struct ctl_io_pool *pool) 3434{ 3435 if (pool == NULL) 3436 return (-EINVAL); 3437 3438 pool->flags |= CTL_POOL_FLAG_INVALID; 3439 3440 return (0); 3441} 3442 3443/* 3444 * Caller must hold ctl_softc->ctl_lock. 3445 */ 3446int 3447ctl_pool_release(struct ctl_io_pool *pool) 3448{ 3449 if (pool == NULL) 3450 return (-EINVAL); 3451 3452 if ((--pool->refcount == 0) 3453 && (pool->flags & CTL_POOL_FLAG_INVALID)) { 3454 ctl_pool_free(pool->ctl_softc, pool); 3455 } 3456 3457 return (0); 3458} 3459 3460/* 3461 * Must be called with ctl_softc->ctl_lock held. 3462 */ 3463void 3464ctl_pool_free(struct ctl_softc *ctl_softc, struct ctl_io_pool *pool) 3465{ 3466 union ctl_io *cur_io, *next_io; 3467 3468 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3469 cur_io != NULL; cur_io = next_io) { 3470 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3471 links); 3472 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, ctl_io_hdr, 3473 links); 3474 free(cur_io, M_CTL); 3475 } 3476 3477 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3478 ctl_softc->num_pools--; 3479 3480 /* 3481 * XXX KDM will this decrement the caller's usage count or mine? 3482 */ 3483#if 0 3484 if ((pool->type != CTL_POOL_EMERGENCY) 3485 && (pool->type != CTL_POOL_INTERNAL) 3486 && (pool->type != CTL_POOL_IOCTL)) 3487 MOD_DEC_USE_COUNT; 3488#endif 3489 3490 free(pool, M_CTL); 3491} 3492 3493/* 3494 * This routine does not block (except for spinlocks of course). 3495 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3496 * possible. 3497 */ 3498union ctl_io * 3499ctl_alloc_io(void *pool_ref) 3500{ 3501 union ctl_io *io; 3502 struct ctl_softc *ctl_softc; 3503 struct ctl_io_pool *pool, *npool; 3504 struct ctl_io_pool *emergency_pool; 3505 3506 pool = (struct ctl_io_pool *)pool_ref; 3507 3508 if (pool == NULL) { 3509 printf("%s: pool is NULL\n", __func__); 3510 return (NULL); 3511 } 3512 3513 emergency_pool = NULL; 3514 3515 ctl_softc = pool->ctl_softc; 3516 3517 mtx_lock(&ctl_softc->ctl_lock); 3518 /* 3519 * First, try to get the io structure from the user's pool. 3520 */ 3521 if (ctl_pool_acquire(pool) == 0) { 3522 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3523 if (io != NULL) { 3524 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3525 pool->total_allocated++; 3526 pool->free_ctl_io--; 3527 mtx_unlock(&ctl_softc->ctl_lock); 3528 return (io); 3529 } else 3530 ctl_pool_release(pool); 3531 } 3532 /* 3533 * If he doesn't have any io structures left, search for an 3534 * emergency pool and grab one from there. 3535 */ 3536 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3537 if (npool->type != CTL_POOL_EMERGENCY) 3538 continue; 3539 3540 if (ctl_pool_acquire(npool) != 0) 3541 continue; 3542 3543 emergency_pool = npool; 3544 3545 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3546 if (io != NULL) { 3547 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3548 npool->total_allocated++; 3549 npool->free_ctl_io--; 3550 mtx_unlock(&ctl_softc->ctl_lock); 3551 return (io); 3552 } else 3553 ctl_pool_release(npool); 3554 } 3555 3556 /* Drop the spinlock before we malloc */ 3557 mtx_unlock(&ctl_softc->ctl_lock); 3558 3559 /* 3560 * The emergency pool (if it exists) didn't have one, so try an 3561 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3562 */ 3563 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3564 if (io != NULL) { 3565 /* 3566 * If the emergency pool exists but is empty, add this 3567 * ctl_io to its list when it gets freed. 3568 */ 3569 if (emergency_pool != NULL) { 3570 mtx_lock(&ctl_softc->ctl_lock); 3571 if (ctl_pool_acquire(emergency_pool) == 0) { 3572 io->io_hdr.pool = emergency_pool; 3573 emergency_pool->total_ctl_io++; 3574 /* 3575 * Need to bump this, otherwise 3576 * total_allocated and total_freed won't 3577 * match when we no longer have anything 3578 * outstanding. 3579 */ 3580 emergency_pool->total_allocated++; 3581 } 3582 mtx_unlock(&ctl_softc->ctl_lock); 3583 } else 3584 io->io_hdr.pool = NULL; 3585 } 3586 3587 return (io); 3588} 3589 3590static void 3591ctl_free_io_internal(union ctl_io *io, int have_lock) 3592{ 3593 if (io == NULL) 3594 return; 3595 3596 /* 3597 * If this ctl_io has a pool, return it to that pool. 3598 */ 3599 if (io->io_hdr.pool != NULL) { 3600 struct ctl_io_pool *pool; 3601#if 0 3602 struct ctl_softc *ctl_softc; 3603 union ctl_io *tmp_io; 3604 unsigned long xflags; 3605 int i; 3606 3607 ctl_softc = control_softc; 3608#endif 3609 3610 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3611 3612 if (have_lock == 0) 3613 mtx_lock(&pool->ctl_softc->ctl_lock); 3614#if 0 3615 save_flags(xflags); 3616 3617 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST( 3618 &ctl_softc->task_queue); tmp_io != NULL; i++, 3619 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr, 3620 links)) { 3621 if (tmp_io == io) { 3622 printf("%s: %p is still on the task queue!\n", 3623 __func__, tmp_io); 3624 printf("%s: (%d): type %d " 3625 "msg %d cdb %x iptl: " 3626 "%d:%d:%d:%d tag 0x%04x " 3627 "flg %#lx\n", 3628 __func__, i, 3629 tmp_io->io_hdr.io_type, 3630 tmp_io->io_hdr.msg_type, 3631 tmp_io->scsiio.cdb[0], 3632 tmp_io->io_hdr.nexus.initid.id, 3633 tmp_io->io_hdr.nexus.targ_port, 3634 tmp_io->io_hdr.nexus.targ_target.id, 3635 tmp_io->io_hdr.nexus.targ_lun, 3636 (tmp_io->io_hdr.io_type == 3637 CTL_IO_TASK) ? 3638 tmp_io->taskio.tag_num : 3639 tmp_io->scsiio.tag_num, 3640 xflags); 3641 panic("I/O still on the task queue!"); 3642 } 3643 } 3644#endif 3645 io->io_hdr.io_type = 0xff; 3646 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3647 pool->total_freed++; 3648 pool->free_ctl_io++; 3649 ctl_pool_release(pool); 3650 if (have_lock == 0) 3651 mtx_unlock(&pool->ctl_softc->ctl_lock); 3652 } else { 3653 /* 3654 * Otherwise, just free it. We probably malloced it and 3655 * the emergency pool wasn't available. 3656 */ 3657 free(io, M_CTL); 3658 } 3659 3660} 3661 3662void 3663ctl_free_io(union ctl_io *io) 3664{ 3665 ctl_free_io_internal(io, /*have_lock*/ 0); 3666} 3667 3668void 3669ctl_zero_io(union ctl_io *io) 3670{ 3671 void *pool_ref; 3672 3673 if (io == NULL) 3674 return; 3675 3676 /* 3677 * May need to preserve linked list pointers at some point too. 3678 */ 3679 pool_ref = io->io_hdr.pool; 3680 3681 memset(io, 0, sizeof(*io)); 3682 3683 io->io_hdr.pool = pool_ref; 3684} 3685 3686/* 3687 * This routine is currently used for internal copies of ctl_ios that need 3688 * to persist for some reason after we've already returned status to the 3689 * FETD. (Thus the flag set.) 3690 * 3691 * XXX XXX 3692 * Note that this makes a blind copy of all fields in the ctl_io, except 3693 * for the pool reference. This includes any memory that has been 3694 * allocated! That memory will no longer be valid after done has been 3695 * called, so this would be VERY DANGEROUS for command that actually does 3696 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3697 * start and stop commands, which don't transfer any data, so this is not a 3698 * problem. If it is used for anything else, the caller would also need to 3699 * allocate data buffer space and this routine would need to be modified to 3700 * copy the data buffer(s) as well. 3701 */ 3702void 3703ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3704{ 3705 void *pool_ref; 3706 3707 if ((src == NULL) 3708 || (dest == NULL)) 3709 return; 3710 3711 /* 3712 * May need to preserve linked list pointers at some point too. 3713 */ 3714 pool_ref = dest->io_hdr.pool; 3715 3716 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3717 3718 dest->io_hdr.pool = pool_ref; 3719 /* 3720 * We need to know that this is an internal copy, and doesn't need 3721 * to get passed back to the FETD that allocated it. 3722 */ 3723 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3724} 3725 3726#ifdef NEEDTOPORT 3727static void 3728ctl_update_power_subpage(struct copan_power_subpage *page) 3729{ 3730 int num_luns, num_partitions, config_type; 3731 struct ctl_softc *softc; 3732 cs_BOOL_t aor_present, shelf_50pct_power; 3733 cs_raidset_personality_t rs_type; 3734 int max_active_luns; 3735 3736 softc = control_softc; 3737 3738 /* subtract out the processor LUN */ 3739 num_luns = softc->num_luns - 1; 3740 /* 3741 * Default to 7 LUNs active, which was the only number we allowed 3742 * in the past. 3743 */ 3744 max_active_luns = 7; 3745 3746 num_partitions = config_GetRsPartitionInfo(); 3747 config_type = config_GetConfigType(); 3748 shelf_50pct_power = config_GetShelfPowerMode(); 3749 aor_present = config_IsAorRsPresent(); 3750 3751 rs_type = ddb_GetRsRaidType(1); 3752 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3753 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3754 EPRINT(0, "Unsupported RS type %d!", rs_type); 3755 } 3756 3757 3758 page->total_luns = num_luns; 3759 3760 switch (config_type) { 3761 case 40: 3762 /* 3763 * In a 40 drive configuration, it doesn't matter what DC 3764 * cards we have, whether we have AOR enabled or not, 3765 * partitioning or not, or what type of RAIDset we have. 3766 * In that scenario, we can power up every LUN we present 3767 * to the user. 3768 */ 3769 max_active_luns = num_luns; 3770 3771 break; 3772 case 64: 3773 if (shelf_50pct_power == CS_FALSE) { 3774 /* 25% power */ 3775 if (aor_present == CS_TRUE) { 3776 if (rs_type == 3777 CS_RAIDSET_PERSONALITY_RAID5) { 3778 max_active_luns = 7; 3779 } else if (rs_type == 3780 CS_RAIDSET_PERSONALITY_RAID1){ 3781 max_active_luns = 14; 3782 } else { 3783 /* XXX KDM now what?? */ 3784 } 3785 } else { 3786 if (rs_type == 3787 CS_RAIDSET_PERSONALITY_RAID5) { 3788 max_active_luns = 8; 3789 } else if (rs_type == 3790 CS_RAIDSET_PERSONALITY_RAID1){ 3791 max_active_luns = 16; 3792 } else { 3793 /* XXX KDM now what?? */ 3794 } 3795 } 3796 } else { 3797 /* 50% power */ 3798 /* 3799 * With 50% power in a 64 drive configuration, we 3800 * can power all LUNs we present. 3801 */ 3802 max_active_luns = num_luns; 3803 } 3804 break; 3805 case 112: 3806 if (shelf_50pct_power == CS_FALSE) { 3807 /* 25% power */ 3808 if (aor_present == CS_TRUE) { 3809 if (rs_type == 3810 CS_RAIDSET_PERSONALITY_RAID5) { 3811 max_active_luns = 7; 3812 } else if (rs_type == 3813 CS_RAIDSET_PERSONALITY_RAID1){ 3814 max_active_luns = 14; 3815 } else { 3816 /* XXX KDM now what?? */ 3817 } 3818 } else { 3819 if (rs_type == 3820 CS_RAIDSET_PERSONALITY_RAID5) { 3821 max_active_luns = 8; 3822 } else if (rs_type == 3823 CS_RAIDSET_PERSONALITY_RAID1){ 3824 max_active_luns = 16; 3825 } else { 3826 /* XXX KDM now what?? */ 3827 } 3828 } 3829 } else { 3830 /* 50% power */ 3831 if (aor_present == CS_TRUE) { 3832 if (rs_type == 3833 CS_RAIDSET_PERSONALITY_RAID5) { 3834 max_active_luns = 14; 3835 } else if (rs_type == 3836 CS_RAIDSET_PERSONALITY_RAID1){ 3837 /* 3838 * We're assuming here that disk 3839 * caching is enabled, and so we're 3840 * able to power up half of each 3841 * LUN, and cache all writes. 3842 */ 3843 max_active_luns = num_luns; 3844 } else { 3845 /* XXX KDM now what?? */ 3846 } 3847 } else { 3848 if (rs_type == 3849 CS_RAIDSET_PERSONALITY_RAID5) { 3850 max_active_luns = 15; 3851 } else if (rs_type == 3852 CS_RAIDSET_PERSONALITY_RAID1){ 3853 max_active_luns = 30; 3854 } else { 3855 /* XXX KDM now what?? */ 3856 } 3857 } 3858 } 3859 break; 3860 default: 3861 /* 3862 * In this case, we have an unknown configuration, so we 3863 * just use the default from above. 3864 */ 3865 break; 3866 } 3867 3868 page->max_active_luns = max_active_luns; 3869#if 0 3870 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 3871 page->total_luns, page->max_active_luns); 3872#endif 3873} 3874#endif /* NEEDTOPORT */ 3875 3876/* 3877 * This routine could be used in the future to load default and/or saved 3878 * mode page parameters for a particuar lun. 3879 */ 3880static int 3881ctl_init_page_index(struct ctl_lun *lun) 3882{ 3883 int i; 3884 struct ctl_page_index *page_index; 3885 struct ctl_softc *softc; 3886 3887 memcpy(&lun->mode_pages.index, page_index_template, 3888 sizeof(page_index_template)); 3889 3890 softc = lun->ctl_softc; 3891 3892 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3893 3894 page_index = &lun->mode_pages.index[i]; 3895 /* 3896 * If this is a disk-only mode page, there's no point in 3897 * setting it up. For some pages, we have to have some 3898 * basic information about the disk in order to calculate the 3899 * mode page data. 3900 */ 3901 if ((lun->be_lun->lun_type != T_DIRECT) 3902 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3903 continue; 3904 3905 switch (page_index->page_code & SMPH_PC_MASK) { 3906 case SMS_FORMAT_DEVICE_PAGE: { 3907 struct scsi_format_page *format_page; 3908 3909 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3910 panic("subpage is incorrect!"); 3911 3912 /* 3913 * Sectors per track are set above. Bytes per 3914 * sector need to be set here on a per-LUN basis. 3915 */ 3916 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3917 &format_page_default, 3918 sizeof(format_page_default)); 3919 memcpy(&lun->mode_pages.format_page[ 3920 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3921 sizeof(format_page_changeable)); 3922 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3923 &format_page_default, 3924 sizeof(format_page_default)); 3925 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3926 &format_page_default, 3927 sizeof(format_page_default)); 3928 3929 format_page = &lun->mode_pages.format_page[ 3930 CTL_PAGE_CURRENT]; 3931 scsi_ulto2b(lun->be_lun->blocksize, 3932 format_page->bytes_per_sector); 3933 3934 format_page = &lun->mode_pages.format_page[ 3935 CTL_PAGE_DEFAULT]; 3936 scsi_ulto2b(lun->be_lun->blocksize, 3937 format_page->bytes_per_sector); 3938 3939 format_page = &lun->mode_pages.format_page[ 3940 CTL_PAGE_SAVED]; 3941 scsi_ulto2b(lun->be_lun->blocksize, 3942 format_page->bytes_per_sector); 3943 3944 page_index->page_data = 3945 (uint8_t *)lun->mode_pages.format_page; 3946 break; 3947 } 3948 case SMS_RIGID_DISK_PAGE: { 3949 struct scsi_rigid_disk_page *rigid_disk_page; 3950 uint32_t sectors_per_cylinder; 3951 uint64_t cylinders; 3952#ifndef __XSCALE__ 3953 int shift; 3954#endif /* !__XSCALE__ */ 3955 3956 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3957 panic("invalid subpage value %d", 3958 page_index->subpage); 3959 3960 /* 3961 * Rotation rate and sectors per track are set 3962 * above. We calculate the cylinders here based on 3963 * capacity. Due to the number of heads and 3964 * sectors per track we're using, smaller arrays 3965 * may turn out to have 0 cylinders. Linux and 3966 * FreeBSD don't pay attention to these mode pages 3967 * to figure out capacity, but Solaris does. It 3968 * seems to deal with 0 cylinders just fine, and 3969 * works out a fake geometry based on the capacity. 3970 */ 3971 memcpy(&lun->mode_pages.rigid_disk_page[ 3972 CTL_PAGE_CURRENT], &rigid_disk_page_default, 3973 sizeof(rigid_disk_page_default)); 3974 memcpy(&lun->mode_pages.rigid_disk_page[ 3975 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3976 sizeof(rigid_disk_page_changeable)); 3977 memcpy(&lun->mode_pages.rigid_disk_page[ 3978 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3979 sizeof(rigid_disk_page_default)); 3980 memcpy(&lun->mode_pages.rigid_disk_page[ 3981 CTL_PAGE_SAVED], &rigid_disk_page_default, 3982 sizeof(rigid_disk_page_default)); 3983 3984 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3985 CTL_DEFAULT_HEADS; 3986 3987 /* 3988 * The divide method here will be more accurate, 3989 * probably, but results in floating point being 3990 * used in the kernel on i386 (__udivdi3()). On the 3991 * XScale, though, __udivdi3() is implemented in 3992 * software. 3993 * 3994 * The shift method for cylinder calculation is 3995 * accurate if sectors_per_cylinder is a power of 3996 * 2. Otherwise it might be slightly off -- you 3997 * might have a bit of a truncation problem. 3998 */ 3999#ifdef __XSCALE__ 4000 cylinders = (lun->be_lun->maxlba + 1) / 4001 sectors_per_cylinder; 4002#else 4003 for (shift = 31; shift > 0; shift--) { 4004 if (sectors_per_cylinder & (1 << shift)) 4005 break; 4006 } 4007 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4008#endif 4009 4010 /* 4011 * We've basically got 3 bytes, or 24 bits for the 4012 * cylinder size in the mode page. If we're over, 4013 * just round down to 2^24. 4014 */ 4015 if (cylinders > 0xffffff) 4016 cylinders = 0xffffff; 4017 4018 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4019 CTL_PAGE_CURRENT]; 4020 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4021 4022 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4023 CTL_PAGE_DEFAULT]; 4024 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4025 4026 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4027 CTL_PAGE_SAVED]; 4028 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4029 4030 page_index->page_data = 4031 (uint8_t *)lun->mode_pages.rigid_disk_page; 4032 break; 4033 } 4034 case SMS_CACHING_PAGE: { 4035 4036 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4037 panic("invalid subpage value %d", 4038 page_index->subpage); 4039 /* 4040 * Defaults should be okay here, no calculations 4041 * needed. 4042 */ 4043 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4044 &caching_page_default, 4045 sizeof(caching_page_default)); 4046 memcpy(&lun->mode_pages.caching_page[ 4047 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4048 sizeof(caching_page_changeable)); 4049 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4050 &caching_page_default, 4051 sizeof(caching_page_default)); 4052 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4053 &caching_page_default, 4054 sizeof(caching_page_default)); 4055 page_index->page_data = 4056 (uint8_t *)lun->mode_pages.caching_page; 4057 break; 4058 } 4059 case SMS_CONTROL_MODE_PAGE: { 4060 4061 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4062 panic("invalid subpage value %d", 4063 page_index->subpage); 4064 4065 /* 4066 * Defaults should be okay here, no calculations 4067 * needed. 4068 */ 4069 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4070 &control_page_default, 4071 sizeof(control_page_default)); 4072 memcpy(&lun->mode_pages.control_page[ 4073 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4074 sizeof(control_page_changeable)); 4075 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4076 &control_page_default, 4077 sizeof(control_page_default)); 4078 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4079 &control_page_default, 4080 sizeof(control_page_default)); 4081 page_index->page_data = 4082 (uint8_t *)lun->mode_pages.control_page; 4083 break; 4084 4085 } 4086 case SMS_VENDOR_SPECIFIC_PAGE:{ 4087 switch (page_index->subpage) { 4088 case PWR_SUBPAGE_CODE: { 4089 struct copan_power_subpage *current_page, 4090 *saved_page; 4091 4092 memcpy(&lun->mode_pages.power_subpage[ 4093 CTL_PAGE_CURRENT], 4094 &power_page_default, 4095 sizeof(power_page_default)); 4096 memcpy(&lun->mode_pages.power_subpage[ 4097 CTL_PAGE_CHANGEABLE], 4098 &power_page_changeable, 4099 sizeof(power_page_changeable)); 4100 memcpy(&lun->mode_pages.power_subpage[ 4101 CTL_PAGE_DEFAULT], 4102 &power_page_default, 4103 sizeof(power_page_default)); 4104 memcpy(&lun->mode_pages.power_subpage[ 4105 CTL_PAGE_SAVED], 4106 &power_page_default, 4107 sizeof(power_page_default)); 4108 page_index->page_data = 4109 (uint8_t *)lun->mode_pages.power_subpage; 4110 4111 current_page = (struct copan_power_subpage *) 4112 (page_index->page_data + 4113 (page_index->page_len * 4114 CTL_PAGE_CURRENT)); 4115 saved_page = (struct copan_power_subpage *) 4116 (page_index->page_data + 4117 (page_index->page_len * 4118 CTL_PAGE_SAVED)); 4119 break; 4120 } 4121 case APS_SUBPAGE_CODE: { 4122 struct copan_aps_subpage *current_page, 4123 *saved_page; 4124 4125 // This gets set multiple times but 4126 // it should always be the same. It's 4127 // only done during init so who cares. 4128 index_to_aps_page = i; 4129 4130 memcpy(&lun->mode_pages.aps_subpage[ 4131 CTL_PAGE_CURRENT], 4132 &aps_page_default, 4133 sizeof(aps_page_default)); 4134 memcpy(&lun->mode_pages.aps_subpage[ 4135 CTL_PAGE_CHANGEABLE], 4136 &aps_page_changeable, 4137 sizeof(aps_page_changeable)); 4138 memcpy(&lun->mode_pages.aps_subpage[ 4139 CTL_PAGE_DEFAULT], 4140 &aps_page_default, 4141 sizeof(aps_page_default)); 4142 memcpy(&lun->mode_pages.aps_subpage[ 4143 CTL_PAGE_SAVED], 4144 &aps_page_default, 4145 sizeof(aps_page_default)); 4146 page_index->page_data = 4147 (uint8_t *)lun->mode_pages.aps_subpage; 4148 4149 current_page = (struct copan_aps_subpage *) 4150 (page_index->page_data + 4151 (page_index->page_len * 4152 CTL_PAGE_CURRENT)); 4153 saved_page = (struct copan_aps_subpage *) 4154 (page_index->page_data + 4155 (page_index->page_len * 4156 CTL_PAGE_SAVED)); 4157 break; 4158 } 4159 case DBGCNF_SUBPAGE_CODE: { 4160 struct copan_debugconf_subpage *current_page, 4161 *saved_page; 4162 4163 memcpy(&lun->mode_pages.debugconf_subpage[ 4164 CTL_PAGE_CURRENT], 4165 &debugconf_page_default, 4166 sizeof(debugconf_page_default)); 4167 memcpy(&lun->mode_pages.debugconf_subpage[ 4168 CTL_PAGE_CHANGEABLE], 4169 &debugconf_page_changeable, 4170 sizeof(debugconf_page_changeable)); 4171 memcpy(&lun->mode_pages.debugconf_subpage[ 4172 CTL_PAGE_DEFAULT], 4173 &debugconf_page_default, 4174 sizeof(debugconf_page_default)); 4175 memcpy(&lun->mode_pages.debugconf_subpage[ 4176 CTL_PAGE_SAVED], 4177 &debugconf_page_default, 4178 sizeof(debugconf_page_default)); 4179 page_index->page_data = 4180 (uint8_t *)lun->mode_pages.debugconf_subpage; 4181 4182 current_page = (struct copan_debugconf_subpage *) 4183 (page_index->page_data + 4184 (page_index->page_len * 4185 CTL_PAGE_CURRENT)); 4186 saved_page = (struct copan_debugconf_subpage *) 4187 (page_index->page_data + 4188 (page_index->page_len * 4189 CTL_PAGE_SAVED)); 4190 break; 4191 } 4192 default: 4193 panic("invalid subpage value %d", 4194 page_index->subpage); 4195 break; 4196 } 4197 break; 4198 } 4199 default: 4200 panic("invalid page value %d", 4201 page_index->page_code & SMPH_PC_MASK); 4202 break; 4203 } 4204 } 4205 4206 return (CTL_RETVAL_COMPLETE); 4207} 4208 4209/* 4210 * LUN allocation. 4211 * 4212 * Requirements: 4213 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4214 * wants us to allocate the LUN and he can block. 4215 * - ctl_softc is always set 4216 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4217 * 4218 * Returns 0 for success, non-zero (errno) for failure. 4219 */ 4220static int 4221ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4222 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4223{ 4224 struct ctl_lun *nlun, *lun; 4225 struct ctl_frontend *fe; 4226 int lun_number, i; 4227 4228 if (be_lun == NULL) 4229 return (EINVAL); 4230 4231 /* 4232 * We currently only support Direct Access or Processor LUN types. 4233 */ 4234 switch (be_lun->lun_type) { 4235 case T_DIRECT: 4236 break; 4237 case T_PROCESSOR: 4238 break; 4239 case T_SEQUENTIAL: 4240 case T_CHANGER: 4241 default: 4242 be_lun->lun_config_status(be_lun->be_lun, 4243 CTL_LUN_CONFIG_FAILURE); 4244 break; 4245 } 4246 if (ctl_lun == NULL) { 4247 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4248 lun->flags = CTL_LUN_MALLOCED; 4249 } else 4250 lun = ctl_lun; 4251 4252 memset(lun, 0, sizeof(*lun)); 4253 4254 mtx_lock(&ctl_softc->ctl_lock); 4255 /* 4256 * See if the caller requested a particular LUN number. If so, see 4257 * if it is available. Otherwise, allocate the first available LUN. 4258 */ 4259 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4260 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4261 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4262 mtx_unlock(&ctl_softc->ctl_lock); 4263 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4264 printf("ctl: requested LUN ID %d is higher " 4265 "than CTL_MAX_LUNS - 1 (%d)\n", 4266 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4267 } else { 4268 /* 4269 * XXX KDM return an error, or just assign 4270 * another LUN ID in this case?? 4271 */ 4272 printf("ctl: requested LUN ID %d is already " 4273 "in use\n", be_lun->req_lun_id); 4274 } 4275 if (lun->flags & CTL_LUN_MALLOCED) 4276 free(lun, M_CTL); 4277 be_lun->lun_config_status(be_lun->be_lun, 4278 CTL_LUN_CONFIG_FAILURE); 4279 return (ENOSPC); 4280 } 4281 lun_number = be_lun->req_lun_id; 4282 } else { 4283 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4284 if (lun_number == -1) { 4285 mtx_unlock(&ctl_softc->ctl_lock); 4286 printf("ctl: can't allocate LUN on target %ju, out of " 4287 "LUNs\n", (uintmax_t)target_id.id); 4288 if (lun->flags & CTL_LUN_MALLOCED) 4289 free(lun, M_CTL); 4290 be_lun->lun_config_status(be_lun->be_lun, 4291 CTL_LUN_CONFIG_FAILURE); 4292 return (ENOSPC); 4293 } 4294 } 4295 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4296 4297 lun->target = target_id; 4298 lun->lun = lun_number; 4299 lun->be_lun = be_lun; 4300 /* 4301 * The processor LUN is always enabled. Disk LUNs come on line 4302 * disabled, and must be enabled by the backend. 4303 */ 4304 lun->flags = CTL_LUN_DISABLED; 4305 lun->backend = be_lun->be; 4306 be_lun->ctl_lun = lun; 4307 be_lun->lun_id = lun_number; 4308 atomic_add_int(&be_lun->be->num_luns, 1); 4309 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4310 lun->flags |= CTL_LUN_STOPPED; 4311 4312 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4313 lun->flags |= CTL_LUN_INOPERABLE; 4314 4315 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4316 lun->flags |= CTL_LUN_PRIMARY_SC; 4317 4318 lun->ctl_softc = ctl_softc; 4319 TAILQ_INIT(&lun->ooa_queue); 4320 TAILQ_INIT(&lun->blocked_queue); 4321 STAILQ_INIT(&lun->error_list); 4322 4323 /* 4324 * Initialize the mode page index. 4325 */ 4326 ctl_init_page_index(lun); 4327 4328 /* 4329 * Set the poweron UA for all initiators on this LUN only. 4330 */ 4331 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4332 lun->pending_sense[i].ua_pending = CTL_UA_POWERON; 4333 4334 /* 4335 * Now, before we insert this lun on the lun list, set the lun 4336 * inventory changed UA for all other luns. 4337 */ 4338 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4339 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4340 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4341 } 4342 } 4343 4344 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4345 4346 ctl_softc->ctl_luns[lun_number] = lun; 4347 4348 ctl_softc->num_luns++; 4349 4350 /* Setup statistics gathering */ 4351 lun->stats.device_type = be_lun->lun_type; 4352 lun->stats.lun_number = lun_number; 4353 if (lun->stats.device_type == T_DIRECT) 4354 lun->stats.blocksize = be_lun->blocksize; 4355 else 4356 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4357 for (i = 0;i < CTL_MAX_PORTS;i++) 4358 lun->stats.ports[i].targ_port = i; 4359 4360 mtx_unlock(&ctl_softc->ctl_lock); 4361 4362 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4363 4364 /* 4365 * Run through each registered FETD and bring it online if it isn't 4366 * already. Enable the target ID if it hasn't been enabled, and 4367 * enable this particular LUN. 4368 */ 4369 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) { 4370 int retval; 4371 4372 /* 4373 * XXX KDM this only works for ONE TARGET ID. We'll need 4374 * to do things differently if we go to a multiple target 4375 * ID scheme. 4376 */ 4377 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) { 4378 4379 retval = fe->targ_enable(fe->targ_lun_arg, target_id); 4380 if (retval != 0) { 4381 printf("ctl_alloc_lun: FETD %s port %d " 4382 "returned error %d for targ_enable on " 4383 "target %ju\n", fe->port_name, 4384 fe->targ_port, retval, 4385 (uintmax_t)target_id.id); 4386 } else 4387 fe->status |= CTL_PORT_STATUS_TARG_ONLINE; 4388 } 4389 4390 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number); 4391 if (retval != 0) { 4392 printf("ctl_alloc_lun: FETD %s port %d returned error " 4393 "%d for lun_enable on target %ju lun %d\n", 4394 fe->port_name, fe->targ_port, retval, 4395 (uintmax_t)target_id.id, lun_number); 4396 } else 4397 fe->status |= CTL_PORT_STATUS_LUN_ONLINE; 4398 } 4399 return (0); 4400} 4401 4402/* 4403 * Delete a LUN. 4404 * Assumptions: 4405 * - caller holds ctl_softc->ctl_lock. 4406 * - LUN has already been marked invalid and any pending I/O has been taken 4407 * care of. 4408 */ 4409static int 4410ctl_free_lun(struct ctl_lun *lun) 4411{ 4412 struct ctl_softc *softc; 4413#if 0 4414 struct ctl_frontend *fe; 4415#endif 4416 struct ctl_lun *nlun; 4417 union ctl_io *io, *next_io; 4418 int i; 4419 4420 softc = lun->ctl_softc; 4421 4422 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4423 4424 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4425 4426 softc->ctl_luns[lun->lun] = NULL; 4427 4428 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) { 4429 printf("ctl_free_lun: aieee!! freeing a LUN with " 4430 "outstanding I/O!!\n"); 4431 } 4432 4433 /* 4434 * If we have anything pending on the RtR queue, remove it. 4435 */ 4436 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL; 4437 io = next_io) { 4438 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 4439 if ((io->io_hdr.nexus.targ_target.id == lun->target.id) 4440 && (io->io_hdr.nexus.targ_lun == lun->lun)) 4441 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 4442 ctl_io_hdr, links); 4443 } 4444 4445 /* 4446 * Then remove everything from the blocked queue. 4447 */ 4448 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL; 4449 io = next_io) { 4450 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links); 4451 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links); 4452 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 4453 } 4454 4455 /* 4456 * Now clear out the OOA queue, and free all the I/O. 4457 * XXX KDM should we notify the FETD here? We probably need to 4458 * quiesce the LUN before deleting it. 4459 */ 4460 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL; 4461 io = next_io) { 4462 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links); 4463 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 4464 ctl_free_io_internal(io, /*have_lock*/ 1); 4465 } 4466 4467 softc->num_luns--; 4468 4469 /* 4470 * XXX KDM this scheme only works for a single target/multiple LUN 4471 * setup. It needs to be revamped for a multiple target scheme. 4472 * 4473 * XXX KDM this results in fe->lun_disable() getting called twice, 4474 * once when ctl_disable_lun() is called, and a second time here. 4475 * We really need to re-think the LUN disable semantics. There 4476 * should probably be several steps/levels to LUN removal: 4477 * - disable 4478 * - invalidate 4479 * - free 4480 * 4481 * Right now we only have a disable method when communicating to 4482 * the front end ports, at least for individual LUNs. 4483 */ 4484#if 0 4485 STAILQ_FOREACH(fe, &softc->fe_list, links) { 4486 int retval; 4487 4488 retval = fe->lun_disable(fe->targ_lun_arg, lun->target, 4489 lun->lun); 4490 if (retval != 0) { 4491 printf("ctl_free_lun: FETD %s port %d returned error " 4492 "%d for lun_disable on target %ju lun %jd\n", 4493 fe->port_name, fe->targ_port, retval, 4494 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4495 } 4496 4497 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4498 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4499 4500 retval = fe->targ_disable(fe->targ_lun_arg,lun->target); 4501 if (retval != 0) { 4502 printf("ctl_free_lun: FETD %s port %d " 4503 "returned error %d for targ_disable on " 4504 "target %ju\n", fe->port_name, 4505 fe->targ_port, retval, 4506 (uintmax_t)lun->target.id); 4507 } else 4508 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4509 4510 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4511 continue; 4512 4513#if 0 4514 fe->port_offline(fe->onoff_arg); 4515 fe->status &= ~CTL_PORT_STATUS_ONLINE; 4516#endif 4517 } 4518 } 4519#endif 4520 4521 /* 4522 * Tell the backend to free resources, if this LUN has a backend. 4523 */ 4524 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4525 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4526 4527 if (lun->flags & CTL_LUN_MALLOCED) 4528 free(lun, M_CTL); 4529 4530 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4531 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4532 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4533 } 4534 } 4535 4536 return (0); 4537} 4538 4539static void 4540ctl_create_lun(struct ctl_be_lun *be_lun) 4541{ 4542 struct ctl_softc *ctl_softc; 4543 4544 ctl_softc = control_softc; 4545 4546 /* 4547 * ctl_alloc_lun() should handle all potential failure cases. 4548 */ 4549 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4550} 4551 4552int 4553ctl_add_lun(struct ctl_be_lun *be_lun) 4554{ 4555 struct ctl_softc *ctl_softc; 4556 4557 ctl_softc = control_softc; 4558 4559 mtx_lock(&ctl_softc->ctl_lock); 4560 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4561 mtx_unlock(&ctl_softc->ctl_lock); 4562 4563 ctl_wakeup_thread(); 4564 4565 return (0); 4566} 4567 4568int 4569ctl_enable_lun(struct ctl_be_lun *be_lun) 4570{ 4571 struct ctl_softc *ctl_softc; 4572 struct ctl_frontend *fe, *nfe; 4573 struct ctl_lun *lun; 4574 int retval; 4575 4576 ctl_softc = control_softc; 4577 4578 lun = (struct ctl_lun *)be_lun->ctl_lun; 4579 4580 mtx_lock(&ctl_softc->ctl_lock); 4581 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4582 /* 4583 * eh? Why did we get called if the LUN is already 4584 * enabled? 4585 */ 4586 mtx_unlock(&ctl_softc->ctl_lock); 4587 return (0); 4588 } 4589 lun->flags &= ~CTL_LUN_DISABLED; 4590 4591 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) { 4592 nfe = STAILQ_NEXT(fe, links); 4593 4594 /* 4595 * Drop the lock while we call the FETD's enable routine. 4596 * This can lead to a callback into CTL (at least in the 4597 * case of the internal initiator frontend. 4598 */ 4599 mtx_unlock(&ctl_softc->ctl_lock); 4600 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun); 4601 mtx_lock(&ctl_softc->ctl_lock); 4602 if (retval != 0) { 4603 printf("%s: FETD %s port %d returned error " 4604 "%d for lun_enable on target %ju lun %jd\n", 4605 __func__, fe->port_name, fe->targ_port, retval, 4606 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4607 } 4608#if 0 4609 else { 4610 /* NOTE: TODO: why does lun enable affect port status? */ 4611 fe->status |= CTL_PORT_STATUS_LUN_ONLINE; 4612 } 4613#endif 4614 } 4615 4616 mtx_unlock(&ctl_softc->ctl_lock); 4617 4618 return (0); 4619} 4620 4621int 4622ctl_disable_lun(struct ctl_be_lun *be_lun) 4623{ 4624 struct ctl_softc *ctl_softc; 4625 struct ctl_frontend *fe; 4626 struct ctl_lun *lun; 4627 int retval; 4628 4629 ctl_softc = control_softc; 4630 4631 lun = (struct ctl_lun *)be_lun->ctl_lun; 4632 4633 mtx_lock(&ctl_softc->ctl_lock); 4634 4635 if (lun->flags & CTL_LUN_DISABLED) { 4636 mtx_unlock(&ctl_softc->ctl_lock); 4637 return (0); 4638 } 4639 lun->flags |= CTL_LUN_DISABLED; 4640 4641 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) { 4642 mtx_unlock(&ctl_softc->ctl_lock); 4643 /* 4644 * Drop the lock before we call the frontend's disable 4645 * routine, to avoid lock order reversals. 4646 * 4647 * XXX KDM what happens if the frontend list changes while 4648 * we're traversing it? It's unlikely, but should be handled. 4649 */ 4650 retval = fe->lun_disable(fe->targ_lun_arg, lun->target, 4651 lun->lun); 4652 mtx_lock(&ctl_softc->ctl_lock); 4653 if (retval != 0) { 4654 printf("ctl_alloc_lun: FETD %s port %d returned error " 4655 "%d for lun_disable on target %ju lun %jd\n", 4656 fe->port_name, fe->targ_port, retval, 4657 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4658 } 4659 } 4660 4661 mtx_unlock(&ctl_softc->ctl_lock); 4662 4663 return (0); 4664} 4665 4666int 4667ctl_start_lun(struct ctl_be_lun *be_lun) 4668{ 4669 struct ctl_softc *ctl_softc; 4670 struct ctl_lun *lun; 4671 4672 ctl_softc = control_softc; 4673 4674 lun = (struct ctl_lun *)be_lun->ctl_lun; 4675 4676 mtx_lock(&ctl_softc->ctl_lock); 4677 lun->flags &= ~CTL_LUN_STOPPED; 4678 mtx_unlock(&ctl_softc->ctl_lock); 4679 4680 return (0); 4681} 4682 4683int 4684ctl_stop_lun(struct ctl_be_lun *be_lun) 4685{ 4686 struct ctl_softc *ctl_softc; 4687 struct ctl_lun *lun; 4688 4689 ctl_softc = control_softc; 4690 4691 lun = (struct ctl_lun *)be_lun->ctl_lun; 4692 4693 mtx_lock(&ctl_softc->ctl_lock); 4694 lun->flags |= CTL_LUN_STOPPED; 4695 mtx_unlock(&ctl_softc->ctl_lock); 4696 4697 return (0); 4698} 4699 4700int 4701ctl_lun_offline(struct ctl_be_lun *be_lun) 4702{ 4703 struct ctl_softc *ctl_softc; 4704 struct ctl_lun *lun; 4705 4706 ctl_softc = control_softc; 4707 4708 lun = (struct ctl_lun *)be_lun->ctl_lun; 4709 4710 mtx_lock(&ctl_softc->ctl_lock); 4711 lun->flags |= CTL_LUN_OFFLINE; 4712 mtx_unlock(&ctl_softc->ctl_lock); 4713 4714 return (0); 4715} 4716 4717int 4718ctl_lun_online(struct ctl_be_lun *be_lun) 4719{ 4720 struct ctl_softc *ctl_softc; 4721 struct ctl_lun *lun; 4722 4723 ctl_softc = control_softc; 4724 4725 lun = (struct ctl_lun *)be_lun->ctl_lun; 4726 4727 mtx_lock(&ctl_softc->ctl_lock); 4728 lun->flags &= ~CTL_LUN_OFFLINE; 4729 mtx_unlock(&ctl_softc->ctl_lock); 4730 4731 return (0); 4732} 4733 4734int 4735ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4736{ 4737 struct ctl_softc *ctl_softc; 4738 struct ctl_lun *lun; 4739 4740 ctl_softc = control_softc; 4741 4742 lun = (struct ctl_lun *)be_lun->ctl_lun; 4743 4744 mtx_lock(&ctl_softc->ctl_lock); 4745 4746 /* 4747 * The LUN needs to be disabled before it can be marked invalid. 4748 */ 4749 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4750 mtx_unlock(&ctl_softc->ctl_lock); 4751 return (-1); 4752 } 4753 /* 4754 * Mark the LUN invalid. 4755 */ 4756 lun->flags |= CTL_LUN_INVALID; 4757 4758 /* 4759 * If there is nothing in the OOA queue, go ahead and free the LUN. 4760 * If we have something in the OOA queue, we'll free it when the 4761 * last I/O completes. 4762 */ 4763 if (TAILQ_FIRST(&lun->ooa_queue) == NULL) 4764 ctl_free_lun(lun); 4765 mtx_unlock(&ctl_softc->ctl_lock); 4766 4767 return (0); 4768} 4769 4770int 4771ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4772{ 4773 struct ctl_softc *ctl_softc; 4774 struct ctl_lun *lun; 4775 4776 ctl_softc = control_softc; 4777 lun = (struct ctl_lun *)be_lun->ctl_lun; 4778 4779 mtx_lock(&ctl_softc->ctl_lock); 4780 lun->flags |= CTL_LUN_INOPERABLE; 4781 mtx_unlock(&ctl_softc->ctl_lock); 4782 4783 return (0); 4784} 4785 4786int 4787ctl_lun_operable(struct ctl_be_lun *be_lun) 4788{ 4789 struct ctl_softc *ctl_softc; 4790 struct ctl_lun *lun; 4791 4792 ctl_softc = control_softc; 4793 lun = (struct ctl_lun *)be_lun->ctl_lun; 4794 4795 mtx_lock(&ctl_softc->ctl_lock); 4796 lun->flags &= ~CTL_LUN_INOPERABLE; 4797 mtx_unlock(&ctl_softc->ctl_lock); 4798 4799 return (0); 4800} 4801 4802int 4803ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 4804 int lock) 4805{ 4806 struct ctl_softc *softc; 4807 struct ctl_lun *lun; 4808 struct copan_aps_subpage *current_sp; 4809 struct ctl_page_index *page_index; 4810 int i; 4811 4812 softc = control_softc; 4813 4814 mtx_lock(&softc->ctl_lock); 4815 4816 lun = (struct ctl_lun *)be_lun->ctl_lun; 4817 4818 page_index = NULL; 4819 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4820 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 4821 APS_PAGE_CODE) 4822 continue; 4823 4824 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 4825 continue; 4826 page_index = &lun->mode_pages.index[i]; 4827 } 4828 4829 if (page_index == NULL) { 4830 mtx_unlock(&softc->ctl_lock); 4831 printf("%s: APS subpage not found for lun %ju!\n", __func__, 4832 (uintmax_t)lun->lun); 4833 return (1); 4834 } 4835#if 0 4836 if ((softc->aps_locked_lun != 0) 4837 && (softc->aps_locked_lun != lun->lun)) { 4838 printf("%s: attempt to lock LUN %llu when %llu is already " 4839 "locked\n"); 4840 mtx_unlock(&softc->ctl_lock); 4841 return (1); 4842 } 4843#endif 4844 4845 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 4846 (page_index->page_len * CTL_PAGE_CURRENT)); 4847 4848 if (lock != 0) { 4849 current_sp->lock_active = APS_LOCK_ACTIVE; 4850 softc->aps_locked_lun = lun->lun; 4851 } else { 4852 current_sp->lock_active = 0; 4853 softc->aps_locked_lun = 0; 4854 } 4855 4856 4857 /* 4858 * If we're in HA mode, try to send the lock message to the other 4859 * side. 4860 */ 4861 if (ctl_is_single == 0) { 4862 int isc_retval; 4863 union ctl_ha_msg lock_msg; 4864 4865 lock_msg.hdr.nexus = *nexus; 4866 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 4867 if (lock != 0) 4868 lock_msg.aps.lock_flag = 1; 4869 else 4870 lock_msg.aps.lock_flag = 0; 4871 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 4872 sizeof(lock_msg), 0); 4873 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 4874 printf("%s: APS (lock=%d) error returned from " 4875 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 4876 mtx_unlock(&softc->ctl_lock); 4877 return (1); 4878 } 4879 } 4880 4881 mtx_unlock(&softc->ctl_lock); 4882 4883 return (0); 4884} 4885 4886void 4887ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4888{ 4889 struct ctl_lun *lun; 4890 struct ctl_softc *softc; 4891 int i; 4892 4893 softc = control_softc; 4894 4895 mtx_lock(&softc->ctl_lock); 4896 4897 lun = (struct ctl_lun *)be_lun->ctl_lun; 4898 4899 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4900 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED; 4901 4902 mtx_unlock(&softc->ctl_lock); 4903} 4904 4905/* 4906 * Backend "memory move is complete" callback for requests that never 4907 * make it down to say RAIDCore's configuration code. 4908 */ 4909int 4910ctl_config_move_done(union ctl_io *io) 4911{ 4912 int retval; 4913 4914 retval = CTL_RETVAL_COMPLETE; 4915 4916 4917 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4918 /* 4919 * XXX KDM this shouldn't happen, but what if it does? 4920 */ 4921 if (io->io_hdr.io_type != CTL_IO_SCSI) 4922 panic("I/O type isn't CTL_IO_SCSI!"); 4923 4924 if ((io->io_hdr.port_status == 0) 4925 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4926 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 4927 io->io_hdr.status = CTL_SUCCESS; 4928 else if ((io->io_hdr.port_status != 0) 4929 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4930 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 4931 /* 4932 * For hardware error sense keys, the sense key 4933 * specific value is defined to be a retry count, 4934 * but we use it to pass back an internal FETD 4935 * error code. XXX KDM Hopefully the FETD is only 4936 * using 16 bits for an error code, since that's 4937 * all the space we have in the sks field. 4938 */ 4939 ctl_set_internal_failure(&io->scsiio, 4940 /*sks_valid*/ 1, 4941 /*retry_count*/ 4942 io->io_hdr.port_status); 4943 free(io->scsiio.kern_data_ptr, M_CTL); 4944 ctl_done(io); 4945 goto bailout; 4946 } 4947 4948 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 4949 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 4950 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4951 /* 4952 * XXX KDM just assuming a single pointer here, and not a 4953 * S/G list. If we start using S/G lists for config data, 4954 * we'll need to know how to clean them up here as well. 4955 */ 4956 free(io->scsiio.kern_data_ptr, M_CTL); 4957 /* Hopefully the user has already set the status... */ 4958 ctl_done(io); 4959 } else { 4960 /* 4961 * XXX KDM now we need to continue data movement. Some 4962 * options: 4963 * - call ctl_scsiio() again? We don't do this for data 4964 * writes, because for those at least we know ahead of 4965 * time where the write will go and how long it is. For 4966 * config writes, though, that information is largely 4967 * contained within the write itself, thus we need to 4968 * parse out the data again. 4969 * 4970 * - Call some other function once the data is in? 4971 */ 4972 4973 /* 4974 * XXX KDM call ctl_scsiio() again for now, and check flag 4975 * bits to see whether we're allocated or not. 4976 */ 4977 retval = ctl_scsiio(&io->scsiio); 4978 } 4979bailout: 4980 return (retval); 4981} 4982 4983/* 4984 * This gets called by a backend driver when it is done with a 4985 * configuration write. 4986 */ 4987void 4988ctl_config_write_done(union ctl_io *io) 4989{ 4990 /* 4991 * If the IO_CONT flag is set, we need to call the supplied 4992 * function to continue processing the I/O, instead of completing 4993 * the I/O just yet. 4994 * 4995 * If there is an error, though, we don't want to keep processing. 4996 * Instead, just send status back to the initiator. 4997 */ 4998 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 4999 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5000 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5001 io->scsiio.io_cont(io); 5002 return; 5003 } 5004 /* 5005 * Since a configuration write can be done for commands that actually 5006 * have data allocated, like write buffer, and commands that have 5007 * no data, like start/stop unit, we need to check here. 5008 */ 5009 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5010 free(io->scsiio.kern_data_ptr, M_CTL); 5011 ctl_done(io); 5012} 5013 5014/* 5015 * SCSI release command. 5016 */ 5017int 5018ctl_scsi_release(struct ctl_scsiio *ctsio) 5019{ 5020 int length, longid, thirdparty_id, resv_id; 5021 struct ctl_softc *ctl_softc; 5022 struct ctl_lun *lun; 5023 5024 length = 0; 5025 resv_id = 0; 5026 5027 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5028 5029 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5030 ctl_softc = control_softc; 5031 5032 switch (ctsio->cdb[0]) { 5033 case RELEASE: { 5034 struct scsi_release *cdb; 5035 5036 cdb = (struct scsi_release *)ctsio->cdb; 5037 if ((cdb->byte2 & 0x1f) != 0) { 5038 ctl_set_invalid_field(ctsio, 5039 /*sks_valid*/ 1, 5040 /*command*/ 1, 5041 /*field*/ 1, 5042 /*bit_valid*/ 0, 5043 /*bit*/ 0); 5044 ctl_done((union ctl_io *)ctsio); 5045 return (CTL_RETVAL_COMPLETE); 5046 } 5047 break; 5048 } 5049 case RELEASE_10: { 5050 struct scsi_release_10 *cdb; 5051 5052 cdb = (struct scsi_release_10 *)ctsio->cdb; 5053 5054 if ((cdb->byte2 & SR10_EXTENT) != 0) { 5055 ctl_set_invalid_field(ctsio, 5056 /*sks_valid*/ 1, 5057 /*command*/ 1, 5058 /*field*/ 1, 5059 /*bit_valid*/ 1, 5060 /*bit*/ 0); 5061 ctl_done((union ctl_io *)ctsio); 5062 return (CTL_RETVAL_COMPLETE); 5063 5064 } 5065 5066 if ((cdb->byte2 & SR10_3RDPTY) != 0) { 5067 ctl_set_invalid_field(ctsio, 5068 /*sks_valid*/ 1, 5069 /*command*/ 1, 5070 /*field*/ 1, 5071 /*bit_valid*/ 1, 5072 /*bit*/ 4); 5073 ctl_done((union ctl_io *)ctsio); 5074 return (CTL_RETVAL_COMPLETE); 5075 } 5076 5077 if (cdb->byte2 & SR10_LONGID) 5078 longid = 1; 5079 else 5080 thirdparty_id = cdb->thirdparty_id; 5081 5082 resv_id = cdb->resv_id; 5083 length = scsi_2btoul(cdb->length); 5084 break; 5085 } 5086 } 5087 5088 5089 /* 5090 * XXX KDM right now, we only support LUN reservation. We don't 5091 * support 3rd party reservations, or extent reservations, which 5092 * might actually need the parameter list. If we've gotten this 5093 * far, we've got a LUN reservation. Anything else got kicked out 5094 * above. So, according to SPC, ignore the length. 5095 */ 5096 length = 0; 5097 5098 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5099 && (length > 0)) { 5100 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5101 ctsio->kern_data_len = length; 5102 ctsio->kern_total_len = length; 5103 ctsio->kern_data_resid = 0; 5104 ctsio->kern_rel_offset = 0; 5105 ctsio->kern_sg_entries = 0; 5106 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5107 ctsio->be_move_done = ctl_config_move_done; 5108 ctl_datamove((union ctl_io *)ctsio); 5109 5110 return (CTL_RETVAL_COMPLETE); 5111 } 5112 5113 if (length > 0) 5114 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5115 5116 mtx_lock(&ctl_softc->ctl_lock); 5117 5118 /* 5119 * According to SPC, it is not an error for an intiator to attempt 5120 * to release a reservation on a LUN that isn't reserved, or that 5121 * is reserved by another initiator. The reservation can only be 5122 * released, though, by the initiator who made it or by one of 5123 * several reset type events. 5124 */ 5125 if (lun->flags & CTL_LUN_RESERVED) { 5126 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5127 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5128 && (ctsio->io_hdr.nexus.targ_target.id == 5129 lun->rsv_nexus.targ_target.id)) { 5130 lun->flags &= ~CTL_LUN_RESERVED; 5131 } 5132 } 5133 5134 ctsio->scsi_status = SCSI_STATUS_OK; 5135 ctsio->io_hdr.status = CTL_SUCCESS; 5136 5137 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5138 free(ctsio->kern_data_ptr, M_CTL); 5139 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5140 } 5141 5142 mtx_unlock(&ctl_softc->ctl_lock); 5143 5144 ctl_done((union ctl_io *)ctsio); 5145 return (CTL_RETVAL_COMPLETE); 5146} 5147 5148int 5149ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5150{ 5151 int extent, thirdparty, longid; 5152 int resv_id, length; 5153 uint64_t thirdparty_id; 5154 struct ctl_softc *ctl_softc; 5155 struct ctl_lun *lun; 5156 5157 extent = 0; 5158 thirdparty = 0; 5159 longid = 0; 5160 resv_id = 0; 5161 length = 0; 5162 thirdparty_id = 0; 5163 5164 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5165 5166 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5167 ctl_softc = control_softc; 5168 5169 switch (ctsio->cdb[0]) { 5170 case RESERVE: { 5171 struct scsi_reserve *cdb; 5172 5173 cdb = (struct scsi_reserve *)ctsio->cdb; 5174 if ((cdb->byte2 & 0x1f) != 0) { 5175 ctl_set_invalid_field(ctsio, 5176 /*sks_valid*/ 1, 5177 /*command*/ 1, 5178 /*field*/ 1, 5179 /*bit_valid*/ 0, 5180 /*bit*/ 0); 5181 ctl_done((union ctl_io *)ctsio); 5182 return (CTL_RETVAL_COMPLETE); 5183 } 5184 resv_id = cdb->resv_id; 5185 length = scsi_2btoul(cdb->length); 5186 break; 5187 } 5188 case RESERVE_10: { 5189 struct scsi_reserve_10 *cdb; 5190 5191 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5192 5193 if ((cdb->byte2 & SR10_EXTENT) != 0) { 5194 ctl_set_invalid_field(ctsio, 5195 /*sks_valid*/ 1, 5196 /*command*/ 1, 5197 /*field*/ 1, 5198 /*bit_valid*/ 1, 5199 /*bit*/ 0); 5200 ctl_done((union ctl_io *)ctsio); 5201 return (CTL_RETVAL_COMPLETE); 5202 } 5203 if ((cdb->byte2 & SR10_3RDPTY) != 0) { 5204 ctl_set_invalid_field(ctsio, 5205 /*sks_valid*/ 1, 5206 /*command*/ 1, 5207 /*field*/ 1, 5208 /*bit_valid*/ 1, 5209 /*bit*/ 4); 5210 ctl_done((union ctl_io *)ctsio); 5211 return (CTL_RETVAL_COMPLETE); 5212 } 5213 if (cdb->byte2 & SR10_LONGID) 5214 longid = 1; 5215 else 5216 thirdparty_id = cdb->thirdparty_id; 5217 5218 resv_id = cdb->resv_id; 5219 length = scsi_2btoul(cdb->length); 5220 break; 5221 } 5222 } 5223 5224 /* 5225 * XXX KDM right now, we only support LUN reservation. We don't 5226 * support 3rd party reservations, or extent reservations, which 5227 * might actually need the parameter list. If we've gotten this 5228 * far, we've got a LUN reservation. Anything else got kicked out 5229 * above. So, according to SPC, ignore the length. 5230 */ 5231 length = 0; 5232 5233 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5234 && (length > 0)) { 5235 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5236 ctsio->kern_data_len = length; 5237 ctsio->kern_total_len = length; 5238 ctsio->kern_data_resid = 0; 5239 ctsio->kern_rel_offset = 0; 5240 ctsio->kern_sg_entries = 0; 5241 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5242 ctsio->be_move_done = ctl_config_move_done; 5243 ctl_datamove((union ctl_io *)ctsio); 5244 5245 return (CTL_RETVAL_COMPLETE); 5246 } 5247 5248 if (length > 0) 5249 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5250 5251 mtx_lock(&ctl_softc->ctl_lock); 5252 if (lun->flags & CTL_LUN_RESERVED) { 5253 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5254 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5255 || (ctsio->io_hdr.nexus.targ_target.id != 5256 lun->rsv_nexus.targ_target.id)) { 5257 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5258 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5259 goto bailout; 5260 } 5261 } 5262 5263 lun->flags |= CTL_LUN_RESERVED; 5264 lun->rsv_nexus = ctsio->io_hdr.nexus; 5265 5266 ctsio->scsi_status = SCSI_STATUS_OK; 5267 ctsio->io_hdr.status = CTL_SUCCESS; 5268 5269bailout: 5270 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5271 free(ctsio->kern_data_ptr, M_CTL); 5272 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5273 } 5274 5275 mtx_unlock(&ctl_softc->ctl_lock); 5276 5277 ctl_done((union ctl_io *)ctsio); 5278 return (CTL_RETVAL_COMPLETE); 5279} 5280 5281int 5282ctl_start_stop(struct ctl_scsiio *ctsio) 5283{ 5284 struct scsi_start_stop_unit *cdb; 5285 struct ctl_lun *lun; 5286 struct ctl_softc *ctl_softc; 5287 int retval; 5288 5289 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5290 5291 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5292 ctl_softc = control_softc; 5293 retval = 0; 5294 5295 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5296 5297 /* 5298 * XXX KDM 5299 * We don't support the immediate bit on a stop unit. In order to 5300 * do that, we would need to code up a way to know that a stop is 5301 * pending, and hold off any new commands until it completes, one 5302 * way or another. Then we could accept or reject those commands 5303 * depending on its status. We would almost need to do the reverse 5304 * of what we do below for an immediate start -- return the copy of 5305 * the ctl_io to the FETD with status to send to the host (and to 5306 * free the copy!) and then free the original I/O once the stop 5307 * actually completes. That way, the OOA queue mechanism can work 5308 * to block commands that shouldn't proceed. Another alternative 5309 * would be to put the copy in the queue in place of the original, 5310 * and return the original back to the caller. That could be 5311 * slightly safer.. 5312 */ 5313 if ((cdb->byte2 & SSS_IMMED) 5314 && ((cdb->how & SSS_START) == 0)) { 5315 ctl_set_invalid_field(ctsio, 5316 /*sks_valid*/ 1, 5317 /*command*/ 1, 5318 /*field*/ 1, 5319 /*bit_valid*/ 1, 5320 /*bit*/ 0); 5321 ctl_done((union ctl_io *)ctsio); 5322 return (CTL_RETVAL_COMPLETE); 5323 } 5324 5325 /* 5326 * We don't support the power conditions field. We need to check 5327 * this prior to checking the load/eject and start/stop bits. 5328 */ 5329 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) { 5330 ctl_set_invalid_field(ctsio, 5331 /*sks_valid*/ 1, 5332 /*command*/ 1, 5333 /*field*/ 4, 5334 /*bit_valid*/ 1, 5335 /*bit*/ 4); 5336 ctl_done((union ctl_io *)ctsio); 5337 return (CTL_RETVAL_COMPLETE); 5338 } 5339 5340 /* 5341 * Media isn't removable, so we can't load or eject it. 5342 */ 5343 if ((cdb->how & SSS_LOEJ) != 0) { 5344 ctl_set_invalid_field(ctsio, 5345 /*sks_valid*/ 1, 5346 /*command*/ 1, 5347 /*field*/ 4, 5348 /*bit_valid*/ 1, 5349 /*bit*/ 1); 5350 ctl_done((union ctl_io *)ctsio); 5351 return (CTL_RETVAL_COMPLETE); 5352 } 5353 5354 if ((lun->flags & CTL_LUN_PR_RESERVED) 5355 && ((cdb->how & SSS_START)==0)) { 5356 uint32_t residx; 5357 5358 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5359 if (!lun->per_res[residx].registered 5360 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5361 5362 ctl_set_reservation_conflict(ctsio); 5363 ctl_done((union ctl_io *)ctsio); 5364 return (CTL_RETVAL_COMPLETE); 5365 } 5366 } 5367 5368 /* 5369 * If there is no backend on this device, we can't start or stop 5370 * it. In theory we shouldn't get any start/stop commands in the 5371 * first place at this level if the LUN doesn't have a backend. 5372 * That should get stopped by the command decode code. 5373 */ 5374 if (lun->backend == NULL) { 5375 ctl_set_invalid_opcode(ctsio); 5376 ctl_done((union ctl_io *)ctsio); 5377 return (CTL_RETVAL_COMPLETE); 5378 } 5379 5380 /* 5381 * XXX KDM Copan-specific offline behavior. 5382 * Figure out a reasonable way to port this? 5383 */ 5384#ifdef NEEDTOPORT 5385 mtx_lock(&ctl_softc->ctl_lock); 5386 5387 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5388 && (lun->flags & CTL_LUN_OFFLINE)) { 5389 /* 5390 * If the LUN is offline, and the on/offline bit isn't set, 5391 * reject the start or stop. Otherwise, let it through. 5392 */ 5393 mtx_unlock(&ctl_softc->ctl_lock); 5394 ctl_set_lun_not_ready(ctsio); 5395 ctl_done((union ctl_io *)ctsio); 5396 } else { 5397 mtx_unlock(&ctl_softc->ctl_lock); 5398#endif /* NEEDTOPORT */ 5399 /* 5400 * This could be a start or a stop when we're online, 5401 * or a stop/offline or start/online. A start or stop when 5402 * we're offline is covered in the case above. 5403 */ 5404 /* 5405 * In the non-immediate case, we send the request to 5406 * the backend and return status to the user when 5407 * it is done. 5408 * 5409 * In the immediate case, we allocate a new ctl_io 5410 * to hold a copy of the request, and send that to 5411 * the backend. We then set good status on the 5412 * user's request and return it immediately. 5413 */ 5414 if (cdb->byte2 & SSS_IMMED) { 5415 union ctl_io *new_io; 5416 5417 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5418 if (new_io == NULL) { 5419 ctl_set_busy(ctsio); 5420 ctl_done((union ctl_io *)ctsio); 5421 } else { 5422 ctl_copy_io((union ctl_io *)ctsio, 5423 new_io); 5424 retval = lun->backend->config_write(new_io); 5425 ctl_set_success(ctsio); 5426 ctl_done((union ctl_io *)ctsio); 5427 } 5428 } else { 5429 retval = lun->backend->config_write( 5430 (union ctl_io *)ctsio); 5431 } 5432#ifdef NEEDTOPORT 5433 } 5434#endif 5435 return (retval); 5436} 5437 5438/* 5439 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5440 * we don't really do anything with the LBA and length fields if the user 5441 * passes them in. Instead we'll just flush out the cache for the entire 5442 * LUN. 5443 */ 5444int 5445ctl_sync_cache(struct ctl_scsiio *ctsio) 5446{ 5447 struct ctl_lun *lun; 5448 struct ctl_softc *ctl_softc; 5449 uint64_t starting_lba; 5450 uint32_t block_count; 5451 int reladr, immed; 5452 int retval; 5453 5454 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5455 5456 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5457 ctl_softc = control_softc; 5458 retval = 0; 5459 reladr = 0; 5460 immed = 0; 5461 5462 switch (ctsio->cdb[0]) { 5463 case SYNCHRONIZE_CACHE: { 5464 struct scsi_sync_cache *cdb; 5465 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5466 5467 if (cdb->byte2 & SSC_RELADR) 5468 reladr = 1; 5469 5470 if (cdb->byte2 & SSC_IMMED) 5471 immed = 1; 5472 5473 starting_lba = scsi_4btoul(cdb->begin_lba); 5474 block_count = scsi_2btoul(cdb->lb_count); 5475 break; 5476 } 5477 case SYNCHRONIZE_CACHE_16: { 5478 struct scsi_sync_cache_16 *cdb; 5479 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5480 5481 if (cdb->byte2 & SSC_RELADR) 5482 reladr = 1; 5483 5484 if (cdb->byte2 & SSC_IMMED) 5485 immed = 1; 5486 5487 starting_lba = scsi_8btou64(cdb->begin_lba); 5488 block_count = scsi_4btoul(cdb->lb_count); 5489 break; 5490 } 5491 default: 5492 ctl_set_invalid_opcode(ctsio); 5493 ctl_done((union ctl_io *)ctsio); 5494 goto bailout; 5495 break; /* NOTREACHED */ 5496 } 5497 5498 if (immed) { 5499 /* 5500 * We don't support the immediate bit. Since it's in the 5501 * same place for the 10 and 16 byte SYNCHRONIZE CACHE 5502 * commands, we can just return the same error in either 5503 * case. 5504 */ 5505 ctl_set_invalid_field(ctsio, 5506 /*sks_valid*/ 1, 5507 /*command*/ 1, 5508 /*field*/ 1, 5509 /*bit_valid*/ 1, 5510 /*bit*/ 1); 5511 ctl_done((union ctl_io *)ctsio); 5512 goto bailout; 5513 } 5514 5515 if (reladr) { 5516 /* 5517 * We don't support the reladr bit either. It can only be 5518 * used with linked commands, and we don't support linked 5519 * commands. Since the bit is in the same place for the 5520 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can 5521 * just return the same error in either case. 5522 */ 5523 ctl_set_invalid_field(ctsio, 5524 /*sks_valid*/ 1, 5525 /*command*/ 1, 5526 /*field*/ 1, 5527 /*bit_valid*/ 1, 5528 /*bit*/ 0); 5529 ctl_done((union ctl_io *)ctsio); 5530 goto bailout; 5531 } 5532 5533 /* 5534 * We check the LBA and length, but don't do anything with them. 5535 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5536 * get flushed. This check will just help satisfy anyone who wants 5537 * to see an error for an out of range LBA. 5538 */ 5539 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5540 ctl_set_lba_out_of_range(ctsio); 5541 ctl_done((union ctl_io *)ctsio); 5542 goto bailout; 5543 } 5544 5545 /* 5546 * If this LUN has no backend, we can't flush the cache anyway. 5547 */ 5548 if (lun->backend == NULL) { 5549 ctl_set_invalid_opcode(ctsio); 5550 ctl_done((union ctl_io *)ctsio); 5551 goto bailout; 5552 } 5553 5554 /* 5555 * Check to see whether we're configured to send the SYNCHRONIZE 5556 * CACHE command directly to the back end. 5557 */ 5558 mtx_lock(&ctl_softc->ctl_lock); 5559 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5560 && (++(lun->sync_count) >= lun->sync_interval)) { 5561 lun->sync_count = 0; 5562 mtx_unlock(&ctl_softc->ctl_lock); 5563 retval = lun->backend->config_write((union ctl_io *)ctsio); 5564 } else { 5565 mtx_unlock(&ctl_softc->ctl_lock); 5566 ctl_set_success(ctsio); 5567 ctl_done((union ctl_io *)ctsio); 5568 } 5569 5570bailout: 5571 5572 return (retval); 5573} 5574 5575int 5576ctl_format(struct ctl_scsiio *ctsio) 5577{ 5578 struct scsi_format *cdb; 5579 struct ctl_lun *lun; 5580 struct ctl_softc *ctl_softc; 5581 int length, defect_list_len; 5582 5583 CTL_DEBUG_PRINT(("ctl_format\n")); 5584 5585 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5586 ctl_softc = control_softc; 5587 5588 cdb = (struct scsi_format *)ctsio->cdb; 5589 5590 length = 0; 5591 if (cdb->byte2 & SF_FMTDATA) { 5592 if (cdb->byte2 & SF_LONGLIST) 5593 length = sizeof(struct scsi_format_header_long); 5594 else 5595 length = sizeof(struct scsi_format_header_short); 5596 } 5597 5598 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5599 && (length > 0)) { 5600 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5601 ctsio->kern_data_len = length; 5602 ctsio->kern_total_len = length; 5603 ctsio->kern_data_resid = 0; 5604 ctsio->kern_rel_offset = 0; 5605 ctsio->kern_sg_entries = 0; 5606 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5607 ctsio->be_move_done = ctl_config_move_done; 5608 ctl_datamove((union ctl_io *)ctsio); 5609 5610 return (CTL_RETVAL_COMPLETE); 5611 } 5612 5613 defect_list_len = 0; 5614 5615 if (cdb->byte2 & SF_FMTDATA) { 5616 if (cdb->byte2 & SF_LONGLIST) { 5617 struct scsi_format_header_long *header; 5618 5619 header = (struct scsi_format_header_long *) 5620 ctsio->kern_data_ptr; 5621 5622 defect_list_len = scsi_4btoul(header->defect_list_len); 5623 if (defect_list_len != 0) { 5624 ctl_set_invalid_field(ctsio, 5625 /*sks_valid*/ 1, 5626 /*command*/ 0, 5627 /*field*/ 2, 5628 /*bit_valid*/ 0, 5629 /*bit*/ 0); 5630 goto bailout; 5631 } 5632 } else { 5633 struct scsi_format_header_short *header; 5634 5635 header = (struct scsi_format_header_short *) 5636 ctsio->kern_data_ptr; 5637 5638 defect_list_len = scsi_2btoul(header->defect_list_len); 5639 if (defect_list_len != 0) { 5640 ctl_set_invalid_field(ctsio, 5641 /*sks_valid*/ 1, 5642 /*command*/ 0, 5643 /*field*/ 2, 5644 /*bit_valid*/ 0, 5645 /*bit*/ 0); 5646 goto bailout; 5647 } 5648 } 5649 } 5650 5651 /* 5652 * The format command will clear out the "Medium format corrupted" 5653 * status if set by the configuration code. That status is really 5654 * just a way to notify the host that we have lost the media, and 5655 * get them to issue a command that will basically make them think 5656 * they're blowing away the media. 5657 */ 5658 mtx_lock(&ctl_softc->ctl_lock); 5659 lun->flags &= ~CTL_LUN_INOPERABLE; 5660 mtx_unlock(&ctl_softc->ctl_lock); 5661 5662 ctsio->scsi_status = SCSI_STATUS_OK; 5663 ctsio->io_hdr.status = CTL_SUCCESS; 5664bailout: 5665 5666 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5667 free(ctsio->kern_data_ptr, M_CTL); 5668 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5669 } 5670 5671 ctl_done((union ctl_io *)ctsio); 5672 return (CTL_RETVAL_COMPLETE); 5673} 5674 5675int 5676ctl_write_buffer(struct ctl_scsiio *ctsio) 5677{ 5678 struct scsi_write_buffer *cdb; 5679 struct copan_page_header *header; 5680 struct ctl_lun *lun; 5681 struct ctl_softc *ctl_softc; 5682 int buffer_offset, len; 5683 int retval; 5684 5685 header = NULL; 5686 5687 retval = CTL_RETVAL_COMPLETE; 5688 5689 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5690 5691 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5692 ctl_softc = control_softc; 5693 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5694 5695 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5696 ctl_set_invalid_field(ctsio, 5697 /*sks_valid*/ 1, 5698 /*command*/ 1, 5699 /*field*/ 1, 5700 /*bit_valid*/ 1, 5701 /*bit*/ 4); 5702 ctl_done((union ctl_io *)ctsio); 5703 return (CTL_RETVAL_COMPLETE); 5704 } 5705 if (cdb->buffer_id != 0) { 5706 ctl_set_invalid_field(ctsio, 5707 /*sks_valid*/ 1, 5708 /*command*/ 1, 5709 /*field*/ 2, 5710 /*bit_valid*/ 0, 5711 /*bit*/ 0); 5712 ctl_done((union ctl_io *)ctsio); 5713 return (CTL_RETVAL_COMPLETE); 5714 } 5715 5716 len = scsi_3btoul(cdb->length); 5717 buffer_offset = scsi_3btoul(cdb->offset); 5718 5719 if (len > sizeof(lun->write_buffer)) { 5720 ctl_set_invalid_field(ctsio, 5721 /*sks_valid*/ 1, 5722 /*command*/ 1, 5723 /*field*/ 6, 5724 /*bit_valid*/ 0, 5725 /*bit*/ 0); 5726 ctl_done((union ctl_io *)ctsio); 5727 return (CTL_RETVAL_COMPLETE); 5728 } 5729 5730 if (buffer_offset != 0) { 5731 ctl_set_invalid_field(ctsio, 5732 /*sks_valid*/ 1, 5733 /*command*/ 1, 5734 /*field*/ 3, 5735 /*bit_valid*/ 0, 5736 /*bit*/ 0); 5737 ctl_done((union ctl_io *)ctsio); 5738 return (CTL_RETVAL_COMPLETE); 5739 } 5740 5741 /* 5742 * If we've got a kernel request that hasn't been malloced yet, 5743 * malloc it and tell the caller the data buffer is here. 5744 */ 5745 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5746 ctsio->kern_data_ptr = lun->write_buffer; 5747 ctsio->kern_data_len = len; 5748 ctsio->kern_total_len = len; 5749 ctsio->kern_data_resid = 0; 5750 ctsio->kern_rel_offset = 0; 5751 ctsio->kern_sg_entries = 0; 5752 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5753 ctsio->be_move_done = ctl_config_move_done; 5754 ctl_datamove((union ctl_io *)ctsio); 5755 5756 return (CTL_RETVAL_COMPLETE); 5757 } 5758 5759 ctl_done((union ctl_io *)ctsio); 5760 5761 return (CTL_RETVAL_COMPLETE); 5762} 5763 5764/* 5765 * Note that this function currently doesn't actually do anything inside 5766 * CTL to enforce things if the DQue bit is turned on. 5767 * 5768 * Also note that this function can't be used in the default case, because 5769 * the DQue bit isn't set in the changeable mask for the control mode page 5770 * anyway. This is just here as an example for how to implement a page 5771 * handler, and a placeholder in case we want to allow the user to turn 5772 * tagged queueing on and off. 5773 * 5774 * The D_SENSE bit handling is functional, however, and will turn 5775 * descriptor sense on and off for a given LUN. 5776 */ 5777int 5778ctl_control_page_handler(struct ctl_scsiio *ctsio, 5779 struct ctl_page_index *page_index, uint8_t *page_ptr) 5780{ 5781 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5782 struct ctl_lun *lun; 5783 struct ctl_softc *softc; 5784 int set_ua; 5785 uint32_t initidx; 5786 5787 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5788 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5789 set_ua = 0; 5790 5791 user_cp = (struct scsi_control_page *)page_ptr; 5792 current_cp = (struct scsi_control_page *) 5793 (page_index->page_data + (page_index->page_len * 5794 CTL_PAGE_CURRENT)); 5795 saved_cp = (struct scsi_control_page *) 5796 (page_index->page_data + (page_index->page_len * 5797 CTL_PAGE_SAVED)); 5798 5799 softc = control_softc; 5800 5801 mtx_lock(&softc->ctl_lock); 5802 if (((current_cp->rlec & SCP_DSENSE) == 0) 5803 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5804 /* 5805 * Descriptor sense is currently turned off and the user 5806 * wants to turn it on. 5807 */ 5808 current_cp->rlec |= SCP_DSENSE; 5809 saved_cp->rlec |= SCP_DSENSE; 5810 lun->flags |= CTL_LUN_SENSE_DESC; 5811 set_ua = 1; 5812 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5813 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5814 /* 5815 * Descriptor sense is currently turned on, and the user 5816 * wants to turn it off. 5817 */ 5818 current_cp->rlec &= ~SCP_DSENSE; 5819 saved_cp->rlec &= ~SCP_DSENSE; 5820 lun->flags &= ~CTL_LUN_SENSE_DESC; 5821 set_ua = 1; 5822 } 5823 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 5824 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 5825#ifdef NEEDTOPORT 5826 csevent_log(CSC_CTL | CSC_SHELF_SW | 5827 CTL_UNTAG_TO_UNTAG, 5828 csevent_LogType_Trace, 5829 csevent_Severity_Information, 5830 csevent_AlertLevel_Green, 5831 csevent_FRU_Firmware, 5832 csevent_FRU_Unknown, 5833 "Received untagged to untagged transition"); 5834#endif /* NEEDTOPORT */ 5835 } else { 5836#ifdef NEEDTOPORT 5837 csevent_log(CSC_CTL | CSC_SHELF_SW | 5838 CTL_UNTAG_TO_TAG, 5839 csevent_LogType_ConfigChange, 5840 csevent_Severity_Information, 5841 csevent_AlertLevel_Green, 5842 csevent_FRU_Firmware, 5843 csevent_FRU_Unknown, 5844 "Received untagged to tagged " 5845 "queueing transition"); 5846#endif /* NEEDTOPORT */ 5847 5848 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 5849 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 5850 set_ua = 1; 5851 } 5852 } else { 5853 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 5854#ifdef NEEDTOPORT 5855 csevent_log(CSC_CTL | CSC_SHELF_SW | 5856 CTL_TAG_TO_UNTAG, 5857 csevent_LogType_ConfigChange, 5858 csevent_Severity_Warning, 5859 csevent_AlertLevel_Yellow, 5860 csevent_FRU_Firmware, 5861 csevent_FRU_Unknown, 5862 "Received tagged queueing to untagged " 5863 "transition"); 5864#endif /* NEEDTOPORT */ 5865 5866 current_cp->queue_flags |= SCP_QUEUE_DQUE; 5867 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 5868 set_ua = 1; 5869 } else { 5870#ifdef NEEDTOPORT 5871 csevent_log(CSC_CTL | CSC_SHELF_SW | 5872 CTL_TAG_TO_TAG, 5873 csevent_LogType_Trace, 5874 csevent_Severity_Information, 5875 csevent_AlertLevel_Green, 5876 csevent_FRU_Firmware, 5877 csevent_FRU_Unknown, 5878 "Received tagged queueing to tagged " 5879 "queueing transition"); 5880#endif /* NEEDTOPORT */ 5881 } 5882 } 5883 if (set_ua != 0) { 5884 int i; 5885 /* 5886 * Let other initiators know that the mode 5887 * parameters for this LUN have changed. 5888 */ 5889 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 5890 if (i == initidx) 5891 continue; 5892 5893 lun->pending_sense[i].ua_pending |= 5894 CTL_UA_MODE_CHANGE; 5895 } 5896 } 5897 mtx_unlock(&softc->ctl_lock); 5898 5899 return (0); 5900} 5901 5902int 5903ctl_power_sp_handler(struct ctl_scsiio *ctsio, 5904 struct ctl_page_index *page_index, uint8_t *page_ptr) 5905{ 5906 return (0); 5907} 5908 5909int 5910ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 5911 struct ctl_page_index *page_index, int pc) 5912{ 5913 struct copan_power_subpage *page; 5914 5915 page = (struct copan_power_subpage *)page_index->page_data + 5916 (page_index->page_len * pc); 5917 5918 switch (pc) { 5919 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5920 /* 5921 * We don't update the changable bits for this page. 5922 */ 5923 break; 5924 case SMS_PAGE_CTRL_CURRENT >> 6: 5925 case SMS_PAGE_CTRL_DEFAULT >> 6: 5926 case SMS_PAGE_CTRL_SAVED >> 6: 5927#ifdef NEEDTOPORT 5928 ctl_update_power_subpage(page); 5929#endif 5930 break; 5931 default: 5932#ifdef NEEDTOPORT 5933 EPRINT(0, "Invalid PC %d!!", pc); 5934#endif 5935 break; 5936 } 5937 return (0); 5938} 5939 5940 5941int 5942ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 5943 struct ctl_page_index *page_index, uint8_t *page_ptr) 5944{ 5945 struct copan_aps_subpage *user_sp; 5946 struct copan_aps_subpage *current_sp; 5947 union ctl_modepage_info *modepage_info; 5948 struct ctl_softc *softc; 5949 struct ctl_lun *lun; 5950 int retval; 5951 5952 retval = CTL_RETVAL_COMPLETE; 5953 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5954 (page_index->page_len * CTL_PAGE_CURRENT)); 5955 softc = control_softc; 5956 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5957 5958 user_sp = (struct copan_aps_subpage *)page_ptr; 5959 5960 modepage_info = (union ctl_modepage_info *) 5961 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5962 5963 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 5964 modepage_info->header.subpage = page_index->subpage; 5965 modepage_info->aps.lock_active = user_sp->lock_active; 5966 5967 mtx_lock(&softc->ctl_lock); 5968 5969 /* 5970 * If there is a request to lock the LUN and another LUN is locked 5971 * this is an error. If the requested LUN is already locked ignore 5972 * the request. If no LUN is locked attempt to lock it. 5973 * if there is a request to unlock the LUN and the LUN is currently 5974 * locked attempt to unlock it. Otherwise ignore the request. i.e. 5975 * if another LUN is locked or no LUN is locked. 5976 */ 5977 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 5978 if (softc->aps_locked_lun == lun->lun) { 5979 /* 5980 * This LUN is already locked, so we're done. 5981 */ 5982 retval = CTL_RETVAL_COMPLETE; 5983 } else if (softc->aps_locked_lun == 0) { 5984 /* 5985 * No one has the lock, pass the request to the 5986 * backend. 5987 */ 5988 retval = lun->backend->config_write( 5989 (union ctl_io *)ctsio); 5990 } else { 5991 /* 5992 * Someone else has the lock, throw out the request. 5993 */ 5994 ctl_set_already_locked(ctsio); 5995 free(ctsio->kern_data_ptr, M_CTL); 5996 ctl_done((union ctl_io *)ctsio); 5997 5998 /* 5999 * Set the return value so that ctl_do_mode_select() 6000 * won't try to complete the command. We already 6001 * completed it here. 6002 */ 6003 retval = CTL_RETVAL_ERROR; 6004 } 6005 } else if (softc->aps_locked_lun == lun->lun) { 6006 /* 6007 * This LUN is locked, so pass the unlock request to the 6008 * backend. 6009 */ 6010 retval = lun->backend->config_write((union ctl_io *)ctsio); 6011 } 6012 mtx_unlock(&softc->ctl_lock); 6013 6014 return (retval); 6015} 6016 6017int 6018ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6019 struct ctl_page_index *page_index, 6020 uint8_t *page_ptr) 6021{ 6022 uint8_t *c; 6023 int i; 6024 6025 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6026 ctl_time_io_secs = 6027 (c[0] << 8) | 6028 (c[1] << 0) | 6029 0; 6030 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6031 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6032 printf("page data:"); 6033 for (i=0; i<8; i++) 6034 printf(" %.2x",page_ptr[i]); 6035 printf("\n"); 6036 return (0); 6037} 6038 6039int 6040ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6041 struct ctl_page_index *page_index, 6042 int pc) 6043{ 6044 struct copan_debugconf_subpage *page; 6045 6046 page = (struct copan_debugconf_subpage *)page_index->page_data + 6047 (page_index->page_len * pc); 6048 6049 switch (pc) { 6050 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6051 case SMS_PAGE_CTRL_DEFAULT >> 6: 6052 case SMS_PAGE_CTRL_SAVED >> 6: 6053 /* 6054 * We don't update the changable or default bits for this page. 6055 */ 6056 break; 6057 case SMS_PAGE_CTRL_CURRENT >> 6: 6058 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6059 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6060 break; 6061 default: 6062#ifdef NEEDTOPORT 6063 EPRINT(0, "Invalid PC %d!!", pc); 6064#endif /* NEEDTOPORT */ 6065 break; 6066 } 6067 return (0); 6068} 6069 6070 6071static int 6072ctl_do_mode_select(union ctl_io *io) 6073{ 6074 struct scsi_mode_page_header *page_header; 6075 struct ctl_page_index *page_index; 6076 struct ctl_scsiio *ctsio; 6077 int control_dev, page_len; 6078 int page_len_offset, page_len_size; 6079 union ctl_modepage_info *modepage_info; 6080 struct ctl_lun *lun; 6081 int *len_left, *len_used; 6082 int retval, i; 6083 6084 ctsio = &io->scsiio; 6085 page_index = NULL; 6086 page_len = 0; 6087 retval = CTL_RETVAL_COMPLETE; 6088 6089 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6090 6091 if (lun->be_lun->lun_type != T_DIRECT) 6092 control_dev = 1; 6093 else 6094 control_dev = 0; 6095 6096 modepage_info = (union ctl_modepage_info *) 6097 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6098 len_left = &modepage_info->header.len_left; 6099 len_used = &modepage_info->header.len_used; 6100 6101do_next_page: 6102 6103 page_header = (struct scsi_mode_page_header *) 6104 (ctsio->kern_data_ptr + *len_used); 6105 6106 if (*len_left == 0) { 6107 free(ctsio->kern_data_ptr, M_CTL); 6108 ctl_set_success(ctsio); 6109 ctl_done((union ctl_io *)ctsio); 6110 return (CTL_RETVAL_COMPLETE); 6111 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6112 6113 free(ctsio->kern_data_ptr, M_CTL); 6114 ctl_set_param_len_error(ctsio); 6115 ctl_done((union ctl_io *)ctsio); 6116 return (CTL_RETVAL_COMPLETE); 6117 6118 } else if ((page_header->page_code & SMPH_SPF) 6119 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6120 6121 free(ctsio->kern_data_ptr, M_CTL); 6122 ctl_set_param_len_error(ctsio); 6123 ctl_done((union ctl_io *)ctsio); 6124 return (CTL_RETVAL_COMPLETE); 6125 } 6126 6127 6128 /* 6129 * XXX KDM should we do something with the block descriptor? 6130 */ 6131 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6132 6133 if ((control_dev != 0) 6134 && (lun->mode_pages.index[i].page_flags & 6135 CTL_PAGE_FLAG_DISK_ONLY)) 6136 continue; 6137 6138 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6139 (page_header->page_code & SMPH_PC_MASK)) 6140 continue; 6141 6142 /* 6143 * If neither page has a subpage code, then we've got a 6144 * match. 6145 */ 6146 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6147 && ((page_header->page_code & SMPH_SPF) == 0)) { 6148 page_index = &lun->mode_pages.index[i]; 6149 page_len = page_header->page_length; 6150 break; 6151 } 6152 6153 /* 6154 * If both pages have subpages, then the subpage numbers 6155 * have to match. 6156 */ 6157 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6158 && (page_header->page_code & SMPH_SPF)) { 6159 struct scsi_mode_page_header_sp *sph; 6160 6161 sph = (struct scsi_mode_page_header_sp *)page_header; 6162 6163 if (lun->mode_pages.index[i].subpage == 6164 sph->subpage) { 6165 page_index = &lun->mode_pages.index[i]; 6166 page_len = scsi_2btoul(sph->page_length); 6167 break; 6168 } 6169 } 6170 } 6171 6172 /* 6173 * If we couldn't find the page, or if we don't have a mode select 6174 * handler for it, send back an error to the user. 6175 */ 6176 if ((page_index == NULL) 6177 || (page_index->select_handler == NULL)) { 6178 ctl_set_invalid_field(ctsio, 6179 /*sks_valid*/ 1, 6180 /*command*/ 0, 6181 /*field*/ *len_used, 6182 /*bit_valid*/ 0, 6183 /*bit*/ 0); 6184 free(ctsio->kern_data_ptr, M_CTL); 6185 ctl_done((union ctl_io *)ctsio); 6186 return (CTL_RETVAL_COMPLETE); 6187 } 6188 6189 if (page_index->page_code & SMPH_SPF) { 6190 page_len_offset = 2; 6191 page_len_size = 2; 6192 } else { 6193 page_len_size = 1; 6194 page_len_offset = 1; 6195 } 6196 6197 /* 6198 * If the length the initiator gives us isn't the one we specify in 6199 * the mode page header, or if they didn't specify enough data in 6200 * the CDB to avoid truncating this page, kick out the request. 6201 */ 6202 if ((page_len != (page_index->page_len - page_len_offset - 6203 page_len_size)) 6204 || (*len_left < page_index->page_len)) { 6205 6206 6207 ctl_set_invalid_field(ctsio, 6208 /*sks_valid*/ 1, 6209 /*command*/ 0, 6210 /*field*/ *len_used + page_len_offset, 6211 /*bit_valid*/ 0, 6212 /*bit*/ 0); 6213 free(ctsio->kern_data_ptr, M_CTL); 6214 ctl_done((union ctl_io *)ctsio); 6215 return (CTL_RETVAL_COMPLETE); 6216 } 6217 6218 /* 6219 * Run through the mode page, checking to make sure that the bits 6220 * the user changed are actually legal for him to change. 6221 */ 6222 for (i = 0; i < page_index->page_len; i++) { 6223 uint8_t *user_byte, *change_mask, *current_byte; 6224 int bad_bit; 6225 int j; 6226 6227 user_byte = (uint8_t *)page_header + i; 6228 change_mask = page_index->page_data + 6229 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6230 current_byte = page_index->page_data + 6231 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6232 6233 /* 6234 * Check to see whether the user set any bits in this byte 6235 * that he is not allowed to set. 6236 */ 6237 if ((*user_byte & ~(*change_mask)) == 6238 (*current_byte & ~(*change_mask))) 6239 continue; 6240 6241 /* 6242 * Go through bit by bit to determine which one is illegal. 6243 */ 6244 bad_bit = 0; 6245 for (j = 7; j >= 0; j--) { 6246 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6247 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6248 bad_bit = i; 6249 break; 6250 } 6251 } 6252 ctl_set_invalid_field(ctsio, 6253 /*sks_valid*/ 1, 6254 /*command*/ 0, 6255 /*field*/ *len_used + i, 6256 /*bit_valid*/ 1, 6257 /*bit*/ bad_bit); 6258 free(ctsio->kern_data_ptr, M_CTL); 6259 ctl_done((union ctl_io *)ctsio); 6260 return (CTL_RETVAL_COMPLETE); 6261 } 6262 6263 /* 6264 * Decrement these before we call the page handler, since we may 6265 * end up getting called back one way or another before the handler 6266 * returns to this context. 6267 */ 6268 *len_left -= page_index->page_len; 6269 *len_used += page_index->page_len; 6270 6271 retval = page_index->select_handler(ctsio, page_index, 6272 (uint8_t *)page_header); 6273 6274 /* 6275 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6276 * wait until this queued command completes to finish processing 6277 * the mode page. If it returns anything other than 6278 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6279 * already set the sense information, freed the data pointer, and 6280 * completed the io for us. 6281 */ 6282 if (retval != CTL_RETVAL_COMPLETE) 6283 goto bailout_no_done; 6284 6285 /* 6286 * If the initiator sent us more than one page, parse the next one. 6287 */ 6288 if (*len_left > 0) 6289 goto do_next_page; 6290 6291 ctl_set_success(ctsio); 6292 free(ctsio->kern_data_ptr, M_CTL); 6293 ctl_done((union ctl_io *)ctsio); 6294 6295bailout_no_done: 6296 6297 return (CTL_RETVAL_COMPLETE); 6298 6299} 6300 6301int 6302ctl_mode_select(struct ctl_scsiio *ctsio) 6303{ 6304 int param_len, pf, sp; 6305 int header_size, bd_len; 6306 int len_left, len_used; 6307 struct ctl_page_index *page_index; 6308 struct ctl_lun *lun; 6309 int control_dev, page_len; 6310 union ctl_modepage_info *modepage_info; 6311 int retval; 6312 6313 pf = 0; 6314 sp = 0; 6315 page_len = 0; 6316 len_used = 0; 6317 len_left = 0; 6318 retval = 0; 6319 bd_len = 0; 6320 page_index = NULL; 6321 6322 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6323 6324 if (lun->be_lun->lun_type != T_DIRECT) 6325 control_dev = 1; 6326 else 6327 control_dev = 0; 6328 6329 switch (ctsio->cdb[0]) { 6330 case MODE_SELECT_6: { 6331 struct scsi_mode_select_6 *cdb; 6332 6333 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6334 6335 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6336 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6337 6338 param_len = cdb->length; 6339 header_size = sizeof(struct scsi_mode_header_6); 6340 break; 6341 } 6342 case MODE_SELECT_10: { 6343 struct scsi_mode_select_10 *cdb; 6344 6345 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6346 6347 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6348 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6349 6350 param_len = scsi_2btoul(cdb->length); 6351 header_size = sizeof(struct scsi_mode_header_10); 6352 break; 6353 } 6354 default: 6355 ctl_set_invalid_opcode(ctsio); 6356 ctl_done((union ctl_io *)ctsio); 6357 return (CTL_RETVAL_COMPLETE); 6358 break; /* NOTREACHED */ 6359 } 6360 6361 /* 6362 * From SPC-3: 6363 * "A parameter list length of zero indicates that the Data-Out Buffer 6364 * shall be empty. This condition shall not be considered as an error." 6365 */ 6366 if (param_len == 0) { 6367 ctl_set_success(ctsio); 6368 ctl_done((union ctl_io *)ctsio); 6369 return (CTL_RETVAL_COMPLETE); 6370 } 6371 6372 /* 6373 * Since we'll hit this the first time through, prior to 6374 * allocation, we don't need to free a data buffer here. 6375 */ 6376 if (param_len < header_size) { 6377 ctl_set_param_len_error(ctsio); 6378 ctl_done((union ctl_io *)ctsio); 6379 return (CTL_RETVAL_COMPLETE); 6380 } 6381 6382 /* 6383 * Allocate the data buffer and grab the user's data. In theory, 6384 * we shouldn't have to sanity check the parameter list length here 6385 * because the maximum size is 64K. We should be able to malloc 6386 * that much without too many problems. 6387 */ 6388 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6389 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6390 ctsio->kern_data_len = param_len; 6391 ctsio->kern_total_len = param_len; 6392 ctsio->kern_data_resid = 0; 6393 ctsio->kern_rel_offset = 0; 6394 ctsio->kern_sg_entries = 0; 6395 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6396 ctsio->be_move_done = ctl_config_move_done; 6397 ctl_datamove((union ctl_io *)ctsio); 6398 6399 return (CTL_RETVAL_COMPLETE); 6400 } 6401 6402 switch (ctsio->cdb[0]) { 6403 case MODE_SELECT_6: { 6404 struct scsi_mode_header_6 *mh6; 6405 6406 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6407 bd_len = mh6->blk_desc_len; 6408 break; 6409 } 6410 case MODE_SELECT_10: { 6411 struct scsi_mode_header_10 *mh10; 6412 6413 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6414 bd_len = scsi_2btoul(mh10->blk_desc_len); 6415 break; 6416 } 6417 default: 6418 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6419 break; 6420 } 6421 6422 if (param_len < (header_size + bd_len)) { 6423 free(ctsio->kern_data_ptr, M_CTL); 6424 ctl_set_param_len_error(ctsio); 6425 ctl_done((union ctl_io *)ctsio); 6426 return (CTL_RETVAL_COMPLETE); 6427 } 6428 6429 /* 6430 * Set the IO_CONT flag, so that if this I/O gets passed to 6431 * ctl_config_write_done(), it'll get passed back to 6432 * ctl_do_mode_select() for further processing, or completion if 6433 * we're all done. 6434 */ 6435 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6436 ctsio->io_cont = ctl_do_mode_select; 6437 6438 modepage_info = (union ctl_modepage_info *) 6439 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6440 6441 memset(modepage_info, 0, sizeof(*modepage_info)); 6442 6443 len_left = param_len - header_size - bd_len; 6444 len_used = header_size + bd_len; 6445 6446 modepage_info->header.len_left = len_left; 6447 modepage_info->header.len_used = len_used; 6448 6449 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6450} 6451 6452int 6453ctl_mode_sense(struct ctl_scsiio *ctsio) 6454{ 6455 struct ctl_lun *lun; 6456 int pc, page_code, dbd, llba, subpage; 6457 int alloc_len, page_len, header_len, total_len; 6458 struct scsi_mode_block_descr *block_desc; 6459 struct ctl_page_index *page_index; 6460 int control_dev; 6461 6462 dbd = 0; 6463 llba = 0; 6464 block_desc = NULL; 6465 page_index = NULL; 6466 6467 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6468 6469 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6470 6471 if (lun->be_lun->lun_type != T_DIRECT) 6472 control_dev = 1; 6473 else 6474 control_dev = 0; 6475 6476 switch (ctsio->cdb[0]) { 6477 case MODE_SENSE_6: { 6478 struct scsi_mode_sense_6 *cdb; 6479 6480 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6481 6482 header_len = sizeof(struct scsi_mode_hdr_6); 6483 if (cdb->byte2 & SMS_DBD) 6484 dbd = 1; 6485 else 6486 header_len += sizeof(struct scsi_mode_block_descr); 6487 6488 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6489 page_code = cdb->page & SMS_PAGE_CODE; 6490 subpage = cdb->subpage; 6491 alloc_len = cdb->length; 6492 break; 6493 } 6494 case MODE_SENSE_10: { 6495 struct scsi_mode_sense_10 *cdb; 6496 6497 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6498 6499 header_len = sizeof(struct scsi_mode_hdr_10); 6500 6501 if (cdb->byte2 & SMS_DBD) 6502 dbd = 1; 6503 else 6504 header_len += sizeof(struct scsi_mode_block_descr); 6505 if (cdb->byte2 & SMS10_LLBAA) 6506 llba = 1; 6507 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6508 page_code = cdb->page & SMS_PAGE_CODE; 6509 subpage = cdb->subpage; 6510 alloc_len = scsi_2btoul(cdb->length); 6511 break; 6512 } 6513 default: 6514 ctl_set_invalid_opcode(ctsio); 6515 ctl_done((union ctl_io *)ctsio); 6516 return (CTL_RETVAL_COMPLETE); 6517 break; /* NOTREACHED */ 6518 } 6519 6520 /* 6521 * We have to make a first pass through to calculate the size of 6522 * the pages that match the user's query. Then we allocate enough 6523 * memory to hold it, and actually copy the data into the buffer. 6524 */ 6525 switch (page_code) { 6526 case SMS_ALL_PAGES_PAGE: { 6527 int i; 6528 6529 page_len = 0; 6530 6531 /* 6532 * At the moment, values other than 0 and 0xff here are 6533 * reserved according to SPC-3. 6534 */ 6535 if ((subpage != SMS_SUBPAGE_PAGE_0) 6536 && (subpage != SMS_SUBPAGE_ALL)) { 6537 ctl_set_invalid_field(ctsio, 6538 /*sks_valid*/ 1, 6539 /*command*/ 1, 6540 /*field*/ 3, 6541 /*bit_valid*/ 0, 6542 /*bit*/ 0); 6543 ctl_done((union ctl_io *)ctsio); 6544 return (CTL_RETVAL_COMPLETE); 6545 } 6546 6547 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6548 if ((control_dev != 0) 6549 && (lun->mode_pages.index[i].page_flags & 6550 CTL_PAGE_FLAG_DISK_ONLY)) 6551 continue; 6552 6553 /* 6554 * We don't use this subpage if the user didn't 6555 * request all subpages. 6556 */ 6557 if ((lun->mode_pages.index[i].subpage != 0) 6558 && (subpage == SMS_SUBPAGE_PAGE_0)) 6559 continue; 6560 6561#if 0 6562 printf("found page %#x len %d\n", 6563 lun->mode_pages.index[i].page_code & 6564 SMPH_PC_MASK, 6565 lun->mode_pages.index[i].page_len); 6566#endif 6567 page_len += lun->mode_pages.index[i].page_len; 6568 } 6569 break; 6570 } 6571 default: { 6572 int i; 6573 6574 page_len = 0; 6575 6576 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6577 /* Look for the right page code */ 6578 if ((lun->mode_pages.index[i].page_code & 6579 SMPH_PC_MASK) != page_code) 6580 continue; 6581 6582 /* Look for the right subpage or the subpage wildcard*/ 6583 if ((lun->mode_pages.index[i].subpage != subpage) 6584 && (subpage != SMS_SUBPAGE_ALL)) 6585 continue; 6586 6587 /* Make sure the page is supported for this dev type */ 6588 if ((control_dev != 0) 6589 && (lun->mode_pages.index[i].page_flags & 6590 CTL_PAGE_FLAG_DISK_ONLY)) 6591 continue; 6592 6593#if 0 6594 printf("found page %#x len %d\n", 6595 lun->mode_pages.index[i].page_code & 6596 SMPH_PC_MASK, 6597 lun->mode_pages.index[i].page_len); 6598#endif 6599 6600 page_len += lun->mode_pages.index[i].page_len; 6601 } 6602 6603 if (page_len == 0) { 6604 ctl_set_invalid_field(ctsio, 6605 /*sks_valid*/ 1, 6606 /*command*/ 1, 6607 /*field*/ 2, 6608 /*bit_valid*/ 1, 6609 /*bit*/ 5); 6610 ctl_done((union ctl_io *)ctsio); 6611 return (CTL_RETVAL_COMPLETE); 6612 } 6613 break; 6614 } 6615 } 6616 6617 total_len = header_len + page_len; 6618#if 0 6619 printf("header_len = %d, page_len = %d, total_len = %d\n", 6620 header_len, page_len, total_len); 6621#endif 6622 6623 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6624 ctsio->kern_sg_entries = 0; 6625 ctsio->kern_data_resid = 0; 6626 ctsio->kern_rel_offset = 0; 6627 if (total_len < alloc_len) { 6628 ctsio->residual = alloc_len - total_len; 6629 ctsio->kern_data_len = total_len; 6630 ctsio->kern_total_len = total_len; 6631 } else { 6632 ctsio->residual = 0; 6633 ctsio->kern_data_len = alloc_len; 6634 ctsio->kern_total_len = alloc_len; 6635 } 6636 6637 switch (ctsio->cdb[0]) { 6638 case MODE_SENSE_6: { 6639 struct scsi_mode_hdr_6 *header; 6640 6641 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6642 6643 header->datalen = ctl_min(total_len - 1, 254); 6644 6645 if (dbd) 6646 header->block_descr_len = 0; 6647 else 6648 header->block_descr_len = 6649 sizeof(struct scsi_mode_block_descr); 6650 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6651 break; 6652 } 6653 case MODE_SENSE_10: { 6654 struct scsi_mode_hdr_10 *header; 6655 int datalen; 6656 6657 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6658 6659 datalen = ctl_min(total_len - 2, 65533); 6660 scsi_ulto2b(datalen, header->datalen); 6661 if (dbd) 6662 scsi_ulto2b(0, header->block_descr_len); 6663 else 6664 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6665 header->block_descr_len); 6666 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6667 break; 6668 } 6669 default: 6670 panic("invalid CDB type %#x", ctsio->cdb[0]); 6671 break; /* NOTREACHED */ 6672 } 6673 6674 /* 6675 * If we've got a disk, use its blocksize in the block 6676 * descriptor. Otherwise, just set it to 0. 6677 */ 6678 if (dbd == 0) { 6679 if (control_dev != 0) 6680 scsi_ulto3b(lun->be_lun->blocksize, 6681 block_desc->block_len); 6682 else 6683 scsi_ulto3b(0, block_desc->block_len); 6684 } 6685 6686 switch (page_code) { 6687 case SMS_ALL_PAGES_PAGE: { 6688 int i, data_used; 6689 6690 data_used = header_len; 6691 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6692 struct ctl_page_index *page_index; 6693 6694 page_index = &lun->mode_pages.index[i]; 6695 6696 if ((control_dev != 0) 6697 && (page_index->page_flags & 6698 CTL_PAGE_FLAG_DISK_ONLY)) 6699 continue; 6700 6701 /* 6702 * We don't use this subpage if the user didn't 6703 * request all subpages. We already checked (above) 6704 * to make sure the user only specified a subpage 6705 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6706 */ 6707 if ((page_index->subpage != 0) 6708 && (subpage == SMS_SUBPAGE_PAGE_0)) 6709 continue; 6710 6711 /* 6712 * Call the handler, if it exists, to update the 6713 * page to the latest values. 6714 */ 6715 if (page_index->sense_handler != NULL) 6716 page_index->sense_handler(ctsio, page_index,pc); 6717 6718 memcpy(ctsio->kern_data_ptr + data_used, 6719 page_index->page_data + 6720 (page_index->page_len * pc), 6721 page_index->page_len); 6722 data_used += page_index->page_len; 6723 } 6724 break; 6725 } 6726 default: { 6727 int i, data_used; 6728 6729 data_used = header_len; 6730 6731 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6732 struct ctl_page_index *page_index; 6733 6734 page_index = &lun->mode_pages.index[i]; 6735 6736 /* Look for the right page code */ 6737 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6738 continue; 6739 6740 /* Look for the right subpage or the subpage wildcard*/ 6741 if ((page_index->subpage != subpage) 6742 && (subpage != SMS_SUBPAGE_ALL)) 6743 continue; 6744 6745 /* Make sure the page is supported for this dev type */ 6746 if ((control_dev != 0) 6747 && (page_index->page_flags & 6748 CTL_PAGE_FLAG_DISK_ONLY)) 6749 continue; 6750 6751 /* 6752 * Call the handler, if it exists, to update the 6753 * page to the latest values. 6754 */ 6755 if (page_index->sense_handler != NULL) 6756 page_index->sense_handler(ctsio, page_index,pc); 6757 6758 memcpy(ctsio->kern_data_ptr + data_used, 6759 page_index->page_data + 6760 (page_index->page_len * pc), 6761 page_index->page_len); 6762 data_used += page_index->page_len; 6763 } 6764 break; 6765 } 6766 } 6767 6768 ctsio->scsi_status = SCSI_STATUS_OK; 6769 6770 ctsio->be_move_done = ctl_config_move_done; 6771 ctl_datamove((union ctl_io *)ctsio); 6772 6773 return (CTL_RETVAL_COMPLETE); 6774} 6775 6776int 6777ctl_read_capacity(struct ctl_scsiio *ctsio) 6778{ 6779 struct scsi_read_capacity *cdb; 6780 struct scsi_read_capacity_data *data; 6781 struct ctl_lun *lun; 6782 uint32_t lba; 6783 6784 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6785 6786 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6787 6788 lba = scsi_4btoul(cdb->addr); 6789 if (((cdb->pmi & SRC_PMI) == 0) 6790 && (lba != 0)) { 6791 ctl_set_invalid_field(/*ctsio*/ ctsio, 6792 /*sks_valid*/ 1, 6793 /*command*/ 1, 6794 /*field*/ 2, 6795 /*bit_valid*/ 0, 6796 /*bit*/ 0); 6797 ctl_done((union ctl_io *)ctsio); 6798 return (CTL_RETVAL_COMPLETE); 6799 } 6800 6801 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6802 6803 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6804 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6805 ctsio->residual = 0; 6806 ctsio->kern_data_len = sizeof(*data); 6807 ctsio->kern_total_len = sizeof(*data); 6808 ctsio->kern_data_resid = 0; 6809 ctsio->kern_rel_offset = 0; 6810 ctsio->kern_sg_entries = 0; 6811 6812 /* 6813 * If the maximum LBA is greater than 0xfffffffe, the user must 6814 * issue a SERVICE ACTION IN (16) command, with the read capacity 6815 * serivce action set. 6816 */ 6817 if (lun->be_lun->maxlba > 0xfffffffe) 6818 scsi_ulto4b(0xffffffff, data->addr); 6819 else 6820 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6821 6822 /* 6823 * XXX KDM this may not be 512 bytes... 6824 */ 6825 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6826 6827 ctsio->scsi_status = SCSI_STATUS_OK; 6828 6829 ctsio->be_move_done = ctl_config_move_done; 6830 ctl_datamove((union ctl_io *)ctsio); 6831 6832 return (CTL_RETVAL_COMPLETE); 6833} 6834 6835static int 6836ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6837{ 6838 struct scsi_read_capacity_16 *cdb; 6839 struct scsi_read_capacity_data_long *data; 6840 struct ctl_lun *lun; 6841 uint64_t lba; 6842 uint32_t alloc_len; 6843 6844 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6845 6846 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6847 6848 alloc_len = scsi_4btoul(cdb->alloc_len); 6849 lba = scsi_8btou64(cdb->addr); 6850 6851 if ((cdb->reladr & SRC16_PMI) 6852 && (lba != 0)) { 6853 ctl_set_invalid_field(/*ctsio*/ ctsio, 6854 /*sks_valid*/ 1, 6855 /*command*/ 1, 6856 /*field*/ 2, 6857 /*bit_valid*/ 0, 6858 /*bit*/ 0); 6859 ctl_done((union ctl_io *)ctsio); 6860 return (CTL_RETVAL_COMPLETE); 6861 } 6862 6863 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6864 6865 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6866 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6867 6868 if (sizeof(*data) < alloc_len) { 6869 ctsio->residual = alloc_len - sizeof(*data); 6870 ctsio->kern_data_len = sizeof(*data); 6871 ctsio->kern_total_len = sizeof(*data); 6872 } else { 6873 ctsio->residual = 0; 6874 ctsio->kern_data_len = alloc_len; 6875 ctsio->kern_total_len = alloc_len; 6876 } 6877 ctsio->kern_data_resid = 0; 6878 ctsio->kern_rel_offset = 0; 6879 ctsio->kern_sg_entries = 0; 6880 6881 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6882 /* XXX KDM this may not be 512 bytes... */ 6883 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6884 6885 ctsio->scsi_status = SCSI_STATUS_OK; 6886 6887 ctsio->be_move_done = ctl_config_move_done; 6888 ctl_datamove((union ctl_io *)ctsio); 6889 6890 return (CTL_RETVAL_COMPLETE); 6891} 6892 6893int 6894ctl_service_action_in(struct ctl_scsiio *ctsio) 6895{ 6896 struct scsi_service_action_in *cdb; 6897 int retval; 6898 6899 CTL_DEBUG_PRINT(("ctl_service_action_in\n")); 6900 6901 cdb = (struct scsi_service_action_in *)ctsio->cdb; 6902 6903 retval = CTL_RETVAL_COMPLETE; 6904 6905 switch (cdb->service_action) { 6906 case SRC16_SERVICE_ACTION: 6907 retval = ctl_read_capacity_16(ctsio); 6908 break; 6909 default: 6910 ctl_set_invalid_field(/*ctsio*/ ctsio, 6911 /*sks_valid*/ 1, 6912 /*command*/ 1, 6913 /*field*/ 1, 6914 /*bit_valid*/ 1, 6915 /*bit*/ 4); 6916 ctl_done((union ctl_io *)ctsio); 6917 break; 6918 } 6919 6920 return (retval); 6921} 6922 6923int 6924ctl_maintenance_in(struct ctl_scsiio *ctsio) 6925{ 6926 struct scsi_maintenance_in *cdb; 6927 int retval; 6928 int alloc_len, total_len = 0; 6929 int num_target_port_groups; 6930 struct ctl_lun *lun; 6931 struct ctl_softc *softc; 6932 struct scsi_target_group_data *rtg_ptr; 6933 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2; 6934 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2, 6935 *tp_desc_ptr2_1, *tp_desc_ptr2_2; 6936 6937 CTL_DEBUG_PRINT(("ctl_maintenance_in\n")); 6938 6939 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 6940 softc = control_softc; 6941 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6942 6943 retval = CTL_RETVAL_COMPLETE; 6944 mtx_lock(&softc->ctl_lock); 6945 6946 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) { 6947 ctl_set_invalid_field(/*ctsio*/ ctsio, 6948 /*sks_valid*/ 1, 6949 /*command*/ 1, 6950 /*field*/ 1, 6951 /*bit_valid*/ 1, 6952 /*bit*/ 4); 6953 ctl_done((union ctl_io *)ctsio); 6954 return(retval); 6955 } 6956 6957 if (ctl_is_single) 6958 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1; 6959 else 6960 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 6961 6962 total_len = sizeof(struct scsi_target_group_data) + 6963 sizeof(struct scsi_target_port_group_descriptor) * 6964 num_target_port_groups + 6965 sizeof(struct scsi_target_port_descriptor) * 6966 NUM_PORTS_PER_GRP * num_target_port_groups; 6967 6968 alloc_len = scsi_4btoul(cdb->length); 6969 6970 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6971 6972 ctsio->kern_sg_entries = 0; 6973 6974 if (total_len < alloc_len) { 6975 ctsio->residual = alloc_len - total_len; 6976 ctsio->kern_data_len = total_len; 6977 ctsio->kern_total_len = total_len; 6978 } else { 6979 ctsio->residual = 0; 6980 ctsio->kern_data_len = alloc_len; 6981 ctsio->kern_total_len = alloc_len; 6982 } 6983 ctsio->kern_data_resid = 0; 6984 ctsio->kern_rel_offset = 0; 6985 6986 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr; 6987 6988 tpg_desc_ptr1 = &rtg_ptr->groups[0]; 6989 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0]; 6990 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *) 6991 &tp_desc_ptr1_1->desc_list[0]; 6992 6993 6994 6995 if (ctl_is_single == 0) { 6996 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *) 6997 &tp_desc_ptr1_2->desc_list[0]; 6998 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0]; 6999 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *) 7000 &tp_desc_ptr2_1->desc_list[0]; 7001 } else { 7002 tpg_desc_ptr2 = NULL; 7003 tp_desc_ptr2_1 = NULL; 7004 tp_desc_ptr2_2 = NULL; 7005 } 7006 7007 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7008 if (ctl_is_single == 0) { 7009 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7010 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7011 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7012 tpg_desc_ptr2->pref_state = 7013 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7014 } else { 7015 tpg_desc_ptr1->pref_state = 7016 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7017 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7018 } 7019 } else { 7020 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7021 tpg_desc_ptr1->pref_state = 7022 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7023 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7024 } else { 7025 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7026 tpg_desc_ptr2->pref_state = 7027 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7028 } 7029 } 7030 } else { 7031 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7032 } 7033 tpg_desc_ptr1->support = 0; 7034 tpg_desc_ptr1->target_port_group[1] = 1; 7035 tpg_desc_ptr1->status = TPG_IMPLICIT; 7036 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP; 7037 7038 if (ctl_is_single == 0) { 7039 tpg_desc_ptr2->support = 0; 7040 tpg_desc_ptr2->target_port_group[1] = 2; 7041 tpg_desc_ptr2->status = TPG_IMPLICIT; 7042 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP; 7043 7044 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7045 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7046 7047 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9; 7048 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10; 7049 } else { 7050 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7051 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7052 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7053 } else { 7054 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9; 7055 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10; 7056 } 7057 } 7058 7059 mtx_unlock(&softc->ctl_lock); 7060 7061 ctsio->be_move_done = ctl_config_move_done; 7062 7063 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7064 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7065 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7066 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7067 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7068 7069 ctl_datamove((union ctl_io *)ctsio); 7070 return(retval); 7071} 7072 7073int 7074ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7075{ 7076 struct scsi_per_res_in *cdb; 7077 int alloc_len, total_len = 0; 7078 /* struct scsi_per_res_in_rsrv in_data; */ 7079 struct ctl_lun *lun; 7080 struct ctl_softc *softc; 7081 7082 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7083 7084 softc = control_softc; 7085 7086 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7087 7088 alloc_len = scsi_2btoul(cdb->length); 7089 7090 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7091 7092retry: 7093 mtx_lock(&softc->ctl_lock); 7094 switch (cdb->action) { 7095 case SPRI_RK: /* read keys */ 7096 total_len = sizeof(struct scsi_per_res_in_keys) + 7097 lun->pr_key_count * 7098 sizeof(struct scsi_per_res_key); 7099 break; 7100 case SPRI_RR: /* read reservation */ 7101 if (lun->flags & CTL_LUN_PR_RESERVED) 7102 total_len = sizeof(struct scsi_per_res_in_rsrv); 7103 else 7104 total_len = sizeof(struct scsi_per_res_in_header); 7105 break; 7106 case SPRI_RC: /* report capabilities */ 7107 total_len = sizeof(struct scsi_per_res_cap); 7108 break; 7109 case SPRI_RS: /* read full status */ 7110 default: 7111 mtx_unlock(&softc->ctl_lock); 7112 ctl_set_invalid_field(ctsio, 7113 /*sks_valid*/ 1, 7114 /*command*/ 1, 7115 /*field*/ 1, 7116 /*bit_valid*/ 1, 7117 /*bit*/ 0); 7118 ctl_done((union ctl_io *)ctsio); 7119 return (CTL_RETVAL_COMPLETE); 7120 break; /* NOTREACHED */ 7121 } 7122 mtx_unlock(&softc->ctl_lock); 7123 7124 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7125 7126 if (total_len < alloc_len) { 7127 ctsio->residual = alloc_len - total_len; 7128 ctsio->kern_data_len = total_len; 7129 ctsio->kern_total_len = total_len; 7130 } else { 7131 ctsio->residual = 0; 7132 ctsio->kern_data_len = alloc_len; 7133 ctsio->kern_total_len = alloc_len; 7134 } 7135 7136 ctsio->kern_data_resid = 0; 7137 ctsio->kern_rel_offset = 0; 7138 ctsio->kern_sg_entries = 0; 7139 7140 mtx_lock(&softc->ctl_lock); 7141 switch (cdb->action) { 7142 case SPRI_RK: { // read keys 7143 struct scsi_per_res_in_keys *res_keys; 7144 int i, key_count; 7145 7146 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7147 7148 /* 7149 * We had to drop the lock to allocate our buffer, which 7150 * leaves time for someone to come in with another 7151 * persistent reservation. (That is unlikely, though, 7152 * since this should be the only persistent reservation 7153 * command active right now.) 7154 */ 7155 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7156 (lun->pr_key_count * 7157 sizeof(struct scsi_per_res_key)))){ 7158 mtx_unlock(&softc->ctl_lock); 7159 free(ctsio->kern_data_ptr, M_CTL); 7160 printf("%s: reservation length changed, retrying\n", 7161 __func__); 7162 goto retry; 7163 } 7164 7165 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7166 7167 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7168 lun->pr_key_count, res_keys->header.length); 7169 7170 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7171 if (!lun->per_res[i].registered) 7172 continue; 7173 7174 /* 7175 * We used lun->pr_key_count to calculate the 7176 * size to allocate. If it turns out the number of 7177 * initiators with the registered flag set is 7178 * larger than that (i.e. they haven't been kept in 7179 * sync), we've got a problem. 7180 */ 7181 if (key_count >= lun->pr_key_count) { 7182#ifdef NEEDTOPORT 7183 csevent_log(CSC_CTL | CSC_SHELF_SW | 7184 CTL_PR_ERROR, 7185 csevent_LogType_Fault, 7186 csevent_AlertLevel_Yellow, 7187 csevent_FRU_ShelfController, 7188 csevent_FRU_Firmware, 7189 csevent_FRU_Unknown, 7190 "registered keys %d >= key " 7191 "count %d", key_count, 7192 lun->pr_key_count); 7193#endif 7194 key_count++; 7195 continue; 7196 } 7197 memcpy(res_keys->keys[key_count].key, 7198 lun->per_res[i].res_key.key, 7199 ctl_min(sizeof(res_keys->keys[key_count].key), 7200 sizeof(lun->per_res[i].res_key))); 7201 key_count++; 7202 } 7203 break; 7204 } 7205 case SPRI_RR: { // read reservation 7206 struct scsi_per_res_in_rsrv *res; 7207 int tmp_len, header_only; 7208 7209 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7210 7211 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7212 7213 if (lun->flags & CTL_LUN_PR_RESERVED) 7214 { 7215 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7216 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7217 res->header.length); 7218 header_only = 0; 7219 } else { 7220 tmp_len = sizeof(struct scsi_per_res_in_header); 7221 scsi_ulto4b(0, res->header.length); 7222 header_only = 1; 7223 } 7224 7225 /* 7226 * We had to drop the lock to allocate our buffer, which 7227 * leaves time for someone to come in with another 7228 * persistent reservation. (That is unlikely, though, 7229 * since this should be the only persistent reservation 7230 * command active right now.) 7231 */ 7232 if (tmp_len != total_len) { 7233 mtx_unlock(&softc->ctl_lock); 7234 free(ctsio->kern_data_ptr, M_CTL); 7235 printf("%s: reservation status changed, retrying\n", 7236 __func__); 7237 goto retry; 7238 } 7239 7240 /* 7241 * No reservation held, so we're done. 7242 */ 7243 if (header_only != 0) 7244 break; 7245 7246 /* 7247 * If the registration is an All Registrants type, the key 7248 * is 0, since it doesn't really matter. 7249 */ 7250 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7251 memcpy(res->data.reservation, 7252 &lun->per_res[lun->pr_res_idx].res_key, 7253 sizeof(struct scsi_per_res_key)); 7254 } 7255 res->data.scopetype = lun->res_type; 7256 break; 7257 } 7258 case SPRI_RC: //report capabilities 7259 { 7260 struct scsi_per_res_cap *res_cap; 7261 uint16_t type_mask; 7262 7263 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7264 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7265 res_cap->flags2 |= SPRI_TMV; 7266 type_mask = SPRI_TM_WR_EX_AR | 7267 SPRI_TM_EX_AC_RO | 7268 SPRI_TM_WR_EX_RO | 7269 SPRI_TM_EX_AC | 7270 SPRI_TM_WR_EX | 7271 SPRI_TM_EX_AC_AR; 7272 scsi_ulto2b(type_mask, res_cap->type_mask); 7273 break; 7274 } 7275 case SPRI_RS: //read full status 7276 default: 7277 /* 7278 * This is a bug, because we just checked for this above, 7279 * and should have returned an error. 7280 */ 7281 panic("Invalid PR type %x", cdb->action); 7282 break; /* NOTREACHED */ 7283 } 7284 mtx_unlock(&softc->ctl_lock); 7285 7286 ctsio->be_move_done = ctl_config_move_done; 7287 7288 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7289 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7290 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7291 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7292 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7293 7294 ctl_datamove((union ctl_io *)ctsio); 7295 7296 return (CTL_RETVAL_COMPLETE); 7297} 7298 7299/* 7300 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7301 * it should return. 7302 */ 7303static int 7304ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7305 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7306 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7307 struct scsi_per_res_out_parms* param) 7308{ 7309 union ctl_ha_msg persis_io; 7310 int retval, i; 7311 int isc_retval; 7312 7313 retval = 0; 7314 7315 if (sa_res_key == 0) { 7316 mtx_lock(&softc->ctl_lock); 7317 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7318 /* validate scope and type */ 7319 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7320 SPR_LU_SCOPE) { 7321 mtx_unlock(&softc->ctl_lock); 7322 ctl_set_invalid_field(/*ctsio*/ ctsio, 7323 /*sks_valid*/ 1, 7324 /*command*/ 1, 7325 /*field*/ 2, 7326 /*bit_valid*/ 1, 7327 /*bit*/ 4); 7328 ctl_done((union ctl_io *)ctsio); 7329 return (1); 7330 } 7331 7332 if (type>8 || type==2 || type==4 || type==0) { 7333 mtx_unlock(&softc->ctl_lock); 7334 ctl_set_invalid_field(/*ctsio*/ ctsio, 7335 /*sks_valid*/ 1, 7336 /*command*/ 1, 7337 /*field*/ 2, 7338 /*bit_valid*/ 1, 7339 /*bit*/ 0); 7340 ctl_done((union ctl_io *)ctsio); 7341 return (1); 7342 } 7343 7344 /* temporarily unregister this nexus */ 7345 lun->per_res[residx].registered = 0; 7346 7347 /* 7348 * Unregister everybody else and build UA for 7349 * them 7350 */ 7351 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7352 if (lun->per_res[i].registered == 0) 7353 continue; 7354 7355 if (!persis_offset 7356 && i <CTL_MAX_INITIATORS) 7357 lun->pending_sense[i].ua_pending |= 7358 CTL_UA_REG_PREEMPT; 7359 else if (persis_offset 7360 && i >= persis_offset) 7361 lun->pending_sense[i-persis_offset 7362 ].ua_pending |= 7363 CTL_UA_REG_PREEMPT; 7364 lun->per_res[i].registered = 0; 7365 memset(&lun->per_res[i].res_key, 0, 7366 sizeof(struct scsi_per_res_key)); 7367 } 7368 lun->per_res[residx].registered = 1; 7369 lun->pr_key_count = 1; 7370 lun->res_type = type; 7371 if (lun->res_type != SPR_TYPE_WR_EX_AR 7372 && lun->res_type != SPR_TYPE_EX_AC_AR) 7373 lun->pr_res_idx = residx; 7374 7375 mtx_unlock(&softc->ctl_lock); 7376 /* send msg to other side */ 7377 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7378 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7379 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7380 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7381 persis_io.pr.pr_info.res_type = type; 7382 memcpy(persis_io.pr.pr_info.sa_res_key, 7383 param->serv_act_res_key, 7384 sizeof(param->serv_act_res_key)); 7385 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7386 &persis_io, sizeof(persis_io), 0)) > 7387 CTL_HA_STATUS_SUCCESS) { 7388 printf("CTL:Persis Out error returned " 7389 "from ctl_ha_msg_send %d\n", 7390 isc_retval); 7391 } 7392 } else { 7393 /* not all registrants */ 7394 mtx_unlock(&softc->ctl_lock); 7395 free(ctsio->kern_data_ptr, M_CTL); 7396 ctl_set_invalid_field(ctsio, 7397 /*sks_valid*/ 1, 7398 /*command*/ 0, 7399 /*field*/ 8, 7400 /*bit_valid*/ 0, 7401 /*bit*/ 0); 7402 ctl_done((union ctl_io *)ctsio); 7403 return (1); 7404 } 7405 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7406 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7407 int found = 0; 7408 7409 mtx_lock(&softc->ctl_lock); 7410 if (res_key == sa_res_key) { 7411 /* special case */ 7412 /* 7413 * The spec implies this is not good but doesn't 7414 * say what to do. There are two choices either 7415 * generate a res conflict or check condition 7416 * with illegal field in parameter data. Since 7417 * that is what is done when the sa_res_key is 7418 * zero I'll take that approach since this has 7419 * to do with the sa_res_key. 7420 */ 7421 mtx_unlock(&softc->ctl_lock); 7422 free(ctsio->kern_data_ptr, M_CTL); 7423 ctl_set_invalid_field(ctsio, 7424 /*sks_valid*/ 1, 7425 /*command*/ 0, 7426 /*field*/ 8, 7427 /*bit_valid*/ 0, 7428 /*bit*/ 0); 7429 ctl_done((union ctl_io *)ctsio); 7430 return (1); 7431 } 7432 7433 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7434 if (lun->per_res[i].registered 7435 && memcmp(param->serv_act_res_key, 7436 lun->per_res[i].res_key.key, 7437 sizeof(struct scsi_per_res_key)) != 0) 7438 continue; 7439 7440 found = 1; 7441 lun->per_res[i].registered = 0; 7442 memset(&lun->per_res[i].res_key, 0, 7443 sizeof(struct scsi_per_res_key)); 7444 lun->pr_key_count--; 7445 7446 if (!persis_offset 7447 && i < CTL_MAX_INITIATORS) 7448 lun->pending_sense[i].ua_pending |= 7449 CTL_UA_REG_PREEMPT; 7450 else if (persis_offset 7451 && i >= persis_offset) 7452 lun->pending_sense[i-persis_offset].ua_pending|= 7453 CTL_UA_REG_PREEMPT; 7454 } 7455 mtx_unlock(&softc->ctl_lock); 7456 if (!found) { 7457 free(ctsio->kern_data_ptr, M_CTL); 7458 ctl_set_reservation_conflict(ctsio); 7459 ctl_done((union ctl_io *)ctsio); 7460 return (CTL_RETVAL_COMPLETE); 7461 } 7462 /* send msg to other side */ 7463 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7464 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7465 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7466 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7467 persis_io.pr.pr_info.res_type = type; 7468 memcpy(persis_io.pr.pr_info.sa_res_key, 7469 param->serv_act_res_key, 7470 sizeof(param->serv_act_res_key)); 7471 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7472 &persis_io, sizeof(persis_io), 0)) > 7473 CTL_HA_STATUS_SUCCESS) { 7474 printf("CTL:Persis Out error returned from " 7475 "ctl_ha_msg_send %d\n", isc_retval); 7476 } 7477 } else { 7478 /* Reserved but not all registrants */ 7479 /* sa_res_key is res holder */ 7480 if (memcmp(param->serv_act_res_key, 7481 lun->per_res[lun->pr_res_idx].res_key.key, 7482 sizeof(struct scsi_per_res_key)) == 0) { 7483 /* validate scope and type */ 7484 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7485 SPR_LU_SCOPE) { 7486 ctl_set_invalid_field(/*ctsio*/ ctsio, 7487 /*sks_valid*/ 1, 7488 /*command*/ 1, 7489 /*field*/ 2, 7490 /*bit_valid*/ 1, 7491 /*bit*/ 4); 7492 ctl_done((union ctl_io *)ctsio); 7493 return (1); 7494 } 7495 7496 if (type>8 || type==2 || type==4 || type==0) { 7497 ctl_set_invalid_field(/*ctsio*/ ctsio, 7498 /*sks_valid*/ 1, 7499 /*command*/ 1, 7500 /*field*/ 2, 7501 /*bit_valid*/ 1, 7502 /*bit*/ 0); 7503 ctl_done((union ctl_io *)ctsio); 7504 return (1); 7505 } 7506 7507 /* 7508 * Do the following: 7509 * if sa_res_key != res_key remove all 7510 * registrants w/sa_res_key and generate UA 7511 * for these registrants(Registrations 7512 * Preempted) if it wasn't an exclusive 7513 * reservation generate UA(Reservations 7514 * Preempted) for all other registered nexuses 7515 * if the type has changed. Establish the new 7516 * reservation and holder. If res_key and 7517 * sa_res_key are the same do the above 7518 * except don't unregister the res holder. 7519 */ 7520 7521 /* 7522 * Temporarily unregister so it won't get 7523 * removed or UA generated 7524 */ 7525 lun->per_res[residx].registered = 0; 7526 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7527 if (lun->per_res[i].registered == 0) 7528 continue; 7529 7530 if (memcmp(param->serv_act_res_key, 7531 lun->per_res[i].res_key.key, 7532 sizeof(struct scsi_per_res_key)) == 0) { 7533 lun->per_res[i].registered = 0; 7534 memset(&lun->per_res[i].res_key, 7535 0, 7536 sizeof(struct scsi_per_res_key)); 7537 lun->pr_key_count--; 7538 7539 if (!persis_offset 7540 && i < CTL_MAX_INITIATORS) 7541 lun->pending_sense[i 7542 ].ua_pending |= 7543 CTL_UA_REG_PREEMPT; 7544 else if (persis_offset 7545 && i >= persis_offset) 7546 lun->pending_sense[ 7547 i-persis_offset].ua_pending |= 7548 CTL_UA_REG_PREEMPT; 7549 } else if (type != lun->res_type 7550 && (lun->res_type == SPR_TYPE_WR_EX_RO 7551 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7552 if (!persis_offset 7553 && i < CTL_MAX_INITIATORS) 7554 lun->pending_sense[i 7555 ].ua_pending |= 7556 CTL_UA_RES_RELEASE; 7557 else if (persis_offset 7558 && i >= persis_offset) 7559 lun->pending_sense[ 7560 i-persis_offset 7561 ].ua_pending |= 7562 CTL_UA_RES_RELEASE; 7563 } 7564 } 7565 lun->per_res[residx].registered = 1; 7566 lun->res_type = type; 7567 if (lun->res_type != SPR_TYPE_WR_EX_AR 7568 && lun->res_type != SPR_TYPE_EX_AC_AR) 7569 lun->pr_res_idx = residx; 7570 else 7571 lun->pr_res_idx = 7572 CTL_PR_ALL_REGISTRANTS; 7573 7574 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7575 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7576 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7577 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7578 persis_io.pr.pr_info.res_type = type; 7579 memcpy(persis_io.pr.pr_info.sa_res_key, 7580 param->serv_act_res_key, 7581 sizeof(param->serv_act_res_key)); 7582 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7583 &persis_io, sizeof(persis_io), 0)) > 7584 CTL_HA_STATUS_SUCCESS) { 7585 printf("CTL:Persis Out error returned " 7586 "from ctl_ha_msg_send %d\n", 7587 isc_retval); 7588 } 7589 } else { 7590 /* 7591 * sa_res_key is not the res holder just 7592 * remove registrants 7593 */ 7594 int found=0; 7595 mtx_lock(&softc->ctl_lock); 7596 7597 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7598 if (memcmp(param->serv_act_res_key, 7599 lun->per_res[i].res_key.key, 7600 sizeof(struct scsi_per_res_key)) != 0) 7601 continue; 7602 7603 found = 1; 7604 lun->per_res[i].registered = 0; 7605 memset(&lun->per_res[i].res_key, 0, 7606 sizeof(struct scsi_per_res_key)); 7607 lun->pr_key_count--; 7608 7609 if (!persis_offset 7610 && i < CTL_MAX_INITIATORS) 7611 lun->pending_sense[i].ua_pending |= 7612 CTL_UA_REG_PREEMPT; 7613 else if (persis_offset 7614 && i >= persis_offset) 7615 lun->pending_sense[ 7616 i-persis_offset].ua_pending |= 7617 CTL_UA_REG_PREEMPT; 7618 } 7619 7620 if (!found) { 7621 mtx_unlock(&softc->ctl_lock); 7622 free(ctsio->kern_data_ptr, M_CTL); 7623 ctl_set_reservation_conflict(ctsio); 7624 ctl_done((union ctl_io *)ctsio); 7625 return (1); 7626 } 7627 mtx_unlock(&softc->ctl_lock); 7628 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7629 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7630 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7631 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7632 persis_io.pr.pr_info.res_type = type; 7633 memcpy(persis_io.pr.pr_info.sa_res_key, 7634 param->serv_act_res_key, 7635 sizeof(param->serv_act_res_key)); 7636 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7637 &persis_io, sizeof(persis_io), 0)) > 7638 CTL_HA_STATUS_SUCCESS) { 7639 printf("CTL:Persis Out error returned " 7640 "from ctl_ha_msg_send %d\n", 7641 isc_retval); 7642 } 7643 } 7644 } 7645 7646 lun->PRGeneration++; 7647 7648 return (retval); 7649} 7650 7651static void 7652ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7653{ 7654 int i; 7655 7656 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7657 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7658 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 7659 msg->pr.pr_info.sa_res_key, 7660 sizeof(struct scsi_per_res_key)) != 0) { 7661 uint64_t sa_res_key; 7662 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7663 7664 if (sa_res_key == 0) { 7665 /* temporarily unregister this nexus */ 7666 lun->per_res[msg->pr.pr_info.residx].registered = 0; 7667 7668 /* 7669 * Unregister everybody else and build UA for 7670 * them 7671 */ 7672 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7673 if (lun->per_res[i].registered == 0) 7674 continue; 7675 7676 if (!persis_offset 7677 && i < CTL_MAX_INITIATORS) 7678 lun->pending_sense[i].ua_pending |= 7679 CTL_UA_REG_PREEMPT; 7680 else if (persis_offset && i >= persis_offset) 7681 lun->pending_sense[i - 7682 persis_offset].ua_pending |= 7683 CTL_UA_REG_PREEMPT; 7684 lun->per_res[i].registered = 0; 7685 memset(&lun->per_res[i].res_key, 0, 7686 sizeof(struct scsi_per_res_key)); 7687 } 7688 7689 lun->per_res[msg->pr.pr_info.residx].registered = 1; 7690 lun->pr_key_count = 1; 7691 lun->res_type = msg->pr.pr_info.res_type; 7692 if (lun->res_type != SPR_TYPE_WR_EX_AR 7693 && lun->res_type != SPR_TYPE_EX_AC_AR) 7694 lun->pr_res_idx = msg->pr.pr_info.residx; 7695 } else { 7696 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7697 if (memcmp(msg->pr.pr_info.sa_res_key, 7698 lun->per_res[i].res_key.key, 7699 sizeof(struct scsi_per_res_key)) != 0) 7700 continue; 7701 7702 lun->per_res[i].registered = 0; 7703 memset(&lun->per_res[i].res_key, 0, 7704 sizeof(struct scsi_per_res_key)); 7705 lun->pr_key_count--; 7706 7707 if (!persis_offset 7708 && i < persis_offset) 7709 lun->pending_sense[i].ua_pending |= 7710 CTL_UA_REG_PREEMPT; 7711 else if (persis_offset 7712 && i >= persis_offset) 7713 lun->pending_sense[i - 7714 persis_offset].ua_pending |= 7715 CTL_UA_REG_PREEMPT; 7716 } 7717 } 7718 } else { 7719 /* 7720 * Temporarily unregister so it won't get removed 7721 * or UA generated 7722 */ 7723 lun->per_res[msg->pr.pr_info.residx].registered = 0; 7724 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7725 if (lun->per_res[i].registered == 0) 7726 continue; 7727 7728 if (memcmp(msg->pr.pr_info.sa_res_key, 7729 lun->per_res[i].res_key.key, 7730 sizeof(struct scsi_per_res_key)) == 0) { 7731 lun->per_res[i].registered = 0; 7732 memset(&lun->per_res[i].res_key, 0, 7733 sizeof(struct scsi_per_res_key)); 7734 lun->pr_key_count--; 7735 if (!persis_offset 7736 && i < CTL_MAX_INITIATORS) 7737 lun->pending_sense[i].ua_pending |= 7738 CTL_UA_REG_PREEMPT; 7739 else if (persis_offset 7740 && i >= persis_offset) 7741 lun->pending_sense[i - 7742 persis_offset].ua_pending |= 7743 CTL_UA_REG_PREEMPT; 7744 } else if (msg->pr.pr_info.res_type != lun->res_type 7745 && (lun->res_type == SPR_TYPE_WR_EX_RO 7746 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 7747 if (!persis_offset 7748 && i < persis_offset) 7749 lun->pending_sense[i 7750 ].ua_pending |= 7751 CTL_UA_RES_RELEASE; 7752 else if (persis_offset 7753 && i >= persis_offset) 7754 lun->pending_sense[i - 7755 persis_offset].ua_pending |= 7756 CTL_UA_RES_RELEASE; 7757 } 7758 } 7759 lun->per_res[msg->pr.pr_info.residx].registered = 1; 7760 lun->res_type = msg->pr.pr_info.res_type; 7761 if (lun->res_type != SPR_TYPE_WR_EX_AR 7762 && lun->res_type != SPR_TYPE_EX_AC_AR) 7763 lun->pr_res_idx = msg->pr.pr_info.residx; 7764 else 7765 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7766 } 7767 lun->PRGeneration++; 7768 7769} 7770 7771 7772int 7773ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 7774{ 7775 int retval; 7776 int isc_retval; 7777 u_int32_t param_len; 7778 struct scsi_per_res_out *cdb; 7779 struct ctl_lun *lun; 7780 struct scsi_per_res_out_parms* param; 7781 struct ctl_softc *softc; 7782 uint32_t residx; 7783 uint64_t res_key, sa_res_key; 7784 uint8_t type; 7785 union ctl_ha_msg persis_io; 7786 int i; 7787 7788 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 7789 7790 retval = CTL_RETVAL_COMPLETE; 7791 7792 softc = control_softc; 7793 7794 cdb = (struct scsi_per_res_out *)ctsio->cdb; 7795 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7796 7797 /* 7798 * We only support whole-LUN scope. The scope & type are ignored for 7799 * register, register and ignore existing key and clear. 7800 * We sometimes ignore scope and type on preempts too!! 7801 * Verify reservation type here as well. 7802 */ 7803 type = cdb->scope_type & SPR_TYPE_MASK; 7804 if ((cdb->action == SPRO_RESERVE) 7805 || (cdb->action == SPRO_RELEASE)) { 7806 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 7807 ctl_set_invalid_field(/*ctsio*/ ctsio, 7808 /*sks_valid*/ 1, 7809 /*command*/ 1, 7810 /*field*/ 2, 7811 /*bit_valid*/ 1, 7812 /*bit*/ 4); 7813 ctl_done((union ctl_io *)ctsio); 7814 return (CTL_RETVAL_COMPLETE); 7815 } 7816 7817 if (type>8 || type==2 || type==4 || type==0) { 7818 ctl_set_invalid_field(/*ctsio*/ ctsio, 7819 /*sks_valid*/ 1, 7820 /*command*/ 1, 7821 /*field*/ 2, 7822 /*bit_valid*/ 1, 7823 /*bit*/ 0); 7824 ctl_done((union ctl_io *)ctsio); 7825 return (CTL_RETVAL_COMPLETE); 7826 } 7827 } 7828 7829 switch (cdb->action & SPRO_ACTION_MASK) { 7830 case SPRO_REGISTER: 7831 case SPRO_RESERVE: 7832 case SPRO_RELEASE: 7833 case SPRO_CLEAR: 7834 case SPRO_PREEMPT: 7835 case SPRO_REG_IGNO: 7836 break; 7837 case SPRO_REG_MOVE: 7838 case SPRO_PRE_ABO: 7839 default: 7840 ctl_set_invalid_field(/*ctsio*/ ctsio, 7841 /*sks_valid*/ 1, 7842 /*command*/ 1, 7843 /*field*/ 1, 7844 /*bit_valid*/ 1, 7845 /*bit*/ 0); 7846 ctl_done((union ctl_io *)ctsio); 7847 return (CTL_RETVAL_COMPLETE); 7848 break; /* NOTREACHED */ 7849 } 7850 7851 param_len = scsi_4btoul(cdb->length); 7852 7853 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 7854 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 7855 ctsio->kern_data_len = param_len; 7856 ctsio->kern_total_len = param_len; 7857 ctsio->kern_data_resid = 0; 7858 ctsio->kern_rel_offset = 0; 7859 ctsio->kern_sg_entries = 0; 7860 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7861 ctsio->be_move_done = ctl_config_move_done; 7862 ctl_datamove((union ctl_io *)ctsio); 7863 7864 return (CTL_RETVAL_COMPLETE); 7865 } 7866 7867 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 7868 7869 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 7870 res_key = scsi_8btou64(param->res_key.key); 7871 sa_res_key = scsi_8btou64(param->serv_act_res_key); 7872 7873 /* 7874 * Validate the reservation key here except for SPRO_REG_IGNO 7875 * This must be done for all other service actions 7876 */ 7877 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 7878 mtx_lock(&softc->ctl_lock); 7879 if (lun->per_res[residx].registered) { 7880 if (memcmp(param->res_key.key, 7881 lun->per_res[residx].res_key.key, 7882 ctl_min(sizeof(param->res_key), 7883 sizeof(lun->per_res[residx].res_key))) != 0) { 7884 /* 7885 * The current key passed in doesn't match 7886 * the one the initiator previously 7887 * registered. 7888 */ 7889 mtx_unlock(&softc->ctl_lock); 7890 free(ctsio->kern_data_ptr, M_CTL); 7891 ctl_set_reservation_conflict(ctsio); 7892 ctl_done((union ctl_io *)ctsio); 7893 return (CTL_RETVAL_COMPLETE); 7894 } 7895 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 7896 /* 7897 * We are not registered 7898 */ 7899 mtx_unlock(&softc->ctl_lock); 7900 free(ctsio->kern_data_ptr, M_CTL); 7901 ctl_set_reservation_conflict(ctsio); 7902 ctl_done((union ctl_io *)ctsio); 7903 return (CTL_RETVAL_COMPLETE); 7904 } else if (res_key != 0) { 7905 /* 7906 * We are not registered and trying to register but 7907 * the register key isn't zero. 7908 */ 7909 mtx_unlock(&softc->ctl_lock); 7910 free(ctsio->kern_data_ptr, M_CTL); 7911 ctl_set_reservation_conflict(ctsio); 7912 ctl_done((union ctl_io *)ctsio); 7913 return (CTL_RETVAL_COMPLETE); 7914 } 7915 mtx_unlock(&softc->ctl_lock); 7916 } 7917 7918 switch (cdb->action & SPRO_ACTION_MASK) { 7919 case SPRO_REGISTER: 7920 case SPRO_REG_IGNO: { 7921 7922#if 0 7923 printf("Registration received\n"); 7924#endif 7925 7926 /* 7927 * We don't support any of these options, as we report in 7928 * the read capabilities request (see 7929 * ctl_persistent_reserve_in(), above). 7930 */ 7931 if ((param->flags & SPR_SPEC_I_PT) 7932 || (param->flags & SPR_ALL_TG_PT) 7933 || (param->flags & SPR_APTPL)) { 7934 int bit_ptr; 7935 7936 if (param->flags & SPR_APTPL) 7937 bit_ptr = 0; 7938 else if (param->flags & SPR_ALL_TG_PT) 7939 bit_ptr = 2; 7940 else /* SPR_SPEC_I_PT */ 7941 bit_ptr = 3; 7942 7943 free(ctsio->kern_data_ptr, M_CTL); 7944 ctl_set_invalid_field(ctsio, 7945 /*sks_valid*/ 1, 7946 /*command*/ 0, 7947 /*field*/ 20, 7948 /*bit_valid*/ 1, 7949 /*bit*/ bit_ptr); 7950 ctl_done((union ctl_io *)ctsio); 7951 return (CTL_RETVAL_COMPLETE); 7952 } 7953 7954 mtx_lock(&softc->ctl_lock); 7955 7956 /* 7957 * The initiator wants to clear the 7958 * key/unregister. 7959 */ 7960 if (sa_res_key == 0) { 7961 if ((res_key == 0 7962 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 7963 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 7964 && !lun->per_res[residx].registered)) { 7965 mtx_unlock(&softc->ctl_lock); 7966 goto done; 7967 } 7968 7969 lun->per_res[residx].registered = 0; 7970 memset(&lun->per_res[residx].res_key, 7971 0, sizeof(lun->per_res[residx].res_key)); 7972 lun->pr_key_count--; 7973 7974 if (residx == lun->pr_res_idx) { 7975 lun->flags &= ~CTL_LUN_PR_RESERVED; 7976 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 7977 7978 if ((lun->res_type == SPR_TYPE_WR_EX_RO 7979 || lun->res_type == SPR_TYPE_EX_AC_RO) 7980 && lun->pr_key_count) { 7981 /* 7982 * If the reservation is a registrants 7983 * only type we need to generate a UA 7984 * for other registered inits. The 7985 * sense code should be RESERVATIONS 7986 * RELEASED 7987 */ 7988 7989 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 7990 if (lun->per_res[ 7991 i+persis_offset].registered 7992 == 0) 7993 continue; 7994 lun->pending_sense[i 7995 ].ua_pending |= 7996 CTL_UA_RES_RELEASE; 7997 } 7998 } 7999 lun->res_type = 0; 8000 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8001 if (lun->pr_key_count==0) { 8002 lun->flags &= ~CTL_LUN_PR_RESERVED; 8003 lun->res_type = 0; 8004 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8005 } 8006 } 8007 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8008 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8009 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8010 persis_io.pr.pr_info.residx = residx; 8011 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8012 &persis_io, sizeof(persis_io), 0 )) > 8013 CTL_HA_STATUS_SUCCESS) { 8014 printf("CTL:Persis Out error returned from " 8015 "ctl_ha_msg_send %d\n", isc_retval); 8016 } 8017 mtx_unlock(&softc->ctl_lock); 8018 } else /* sa_res_key != 0 */ { 8019 8020 /* 8021 * If we aren't registered currently then increment 8022 * the key count and set the registered flag. 8023 */ 8024 if (!lun->per_res[residx].registered) { 8025 lun->pr_key_count++; 8026 lun->per_res[residx].registered = 1; 8027 } 8028 8029 memcpy(&lun->per_res[residx].res_key, 8030 param->serv_act_res_key, 8031 ctl_min(sizeof(param->serv_act_res_key), 8032 sizeof(lun->per_res[residx].res_key))); 8033 8034 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8035 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8036 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8037 persis_io.pr.pr_info.residx = residx; 8038 memcpy(persis_io.pr.pr_info.sa_res_key, 8039 param->serv_act_res_key, 8040 sizeof(param->serv_act_res_key)); 8041 mtx_unlock(&softc->ctl_lock); 8042 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8043 &persis_io, sizeof(persis_io), 0)) > 8044 CTL_HA_STATUS_SUCCESS) { 8045 printf("CTL:Persis Out error returned from " 8046 "ctl_ha_msg_send %d\n", isc_retval); 8047 } 8048 } 8049 lun->PRGeneration++; 8050 8051 break; 8052 } 8053 case SPRO_RESERVE: 8054#if 0 8055 printf("Reserve executed type %d\n", type); 8056#endif 8057 mtx_lock(&softc->ctl_lock); 8058 if (lun->flags & CTL_LUN_PR_RESERVED) { 8059 /* 8060 * if this isn't the reservation holder and it's 8061 * not a "all registrants" type or if the type is 8062 * different then we have a conflict 8063 */ 8064 if ((lun->pr_res_idx != residx 8065 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8066 || lun->res_type != type) { 8067 mtx_unlock(&softc->ctl_lock); 8068 free(ctsio->kern_data_ptr, M_CTL); 8069 ctl_set_reservation_conflict(ctsio); 8070 ctl_done((union ctl_io *)ctsio); 8071 return (CTL_RETVAL_COMPLETE); 8072 } 8073 } else /* create a reservation */ { 8074 /* 8075 * If it's not an "all registrants" type record 8076 * reservation holder 8077 */ 8078 if (type != SPR_TYPE_WR_EX_AR 8079 && type != SPR_TYPE_EX_AC_AR) 8080 lun->pr_res_idx = residx; /* Res holder */ 8081 else 8082 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8083 8084 lun->flags |= CTL_LUN_PR_RESERVED; 8085 lun->res_type = type; 8086 8087 mtx_unlock(&softc->ctl_lock); 8088 8089 /* send msg to other side */ 8090 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8091 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8092 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8093 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8094 persis_io.pr.pr_info.res_type = type; 8095 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8096 &persis_io, sizeof(persis_io), 0)) > 8097 CTL_HA_STATUS_SUCCESS) { 8098 printf("CTL:Persis Out error returned from " 8099 "ctl_ha_msg_send %d\n", isc_retval); 8100 } 8101 } 8102 break; 8103 8104 case SPRO_RELEASE: 8105 mtx_lock(&softc->ctl_lock); 8106 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8107 /* No reservation exists return good status */ 8108 mtx_unlock(&softc->ctl_lock); 8109 goto done; 8110 } 8111 /* 8112 * Is this nexus a reservation holder? 8113 */ 8114 if (lun->pr_res_idx != residx 8115 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8116 /* 8117 * not a res holder return good status but 8118 * do nothing 8119 */ 8120 mtx_unlock(&softc->ctl_lock); 8121 goto done; 8122 } 8123 8124 if (lun->res_type != type) { 8125 mtx_unlock(&softc->ctl_lock); 8126 free(ctsio->kern_data_ptr, M_CTL); 8127 ctl_set_illegal_pr_release(ctsio); 8128 ctl_done((union ctl_io *)ctsio); 8129 return (CTL_RETVAL_COMPLETE); 8130 } 8131 8132 /* okay to release */ 8133 lun->flags &= ~CTL_LUN_PR_RESERVED; 8134 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8135 lun->res_type = 0; 8136 8137 /* 8138 * if this isn't an exclusive access 8139 * res generate UA for all other 8140 * registrants. 8141 */ 8142 if (type != SPR_TYPE_EX_AC 8143 && type != SPR_TYPE_WR_EX) { 8144 /* 8145 * temporarily unregister so we don't generate UA 8146 */ 8147 lun->per_res[residx].registered = 0; 8148 8149 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8150 if (lun->per_res[i+persis_offset].registered 8151 == 0) 8152 continue; 8153 lun->pending_sense[i].ua_pending |= 8154 CTL_UA_RES_RELEASE; 8155 } 8156 8157 lun->per_res[residx].registered = 1; 8158 } 8159 mtx_unlock(&softc->ctl_lock); 8160 /* Send msg to other side */ 8161 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8162 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8163 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8164 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8165 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8166 printf("CTL:Persis Out error returned from " 8167 "ctl_ha_msg_send %d\n", isc_retval); 8168 } 8169 break; 8170 8171 case SPRO_CLEAR: 8172 /* send msg to other side */ 8173 8174 mtx_lock(&softc->ctl_lock); 8175 lun->flags &= ~CTL_LUN_PR_RESERVED; 8176 lun->res_type = 0; 8177 lun->pr_key_count = 0; 8178 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8179 8180 8181 memset(&lun->per_res[residx].res_key, 8182 0, sizeof(lun->per_res[residx].res_key)); 8183 lun->per_res[residx].registered = 0; 8184 8185 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8186 if (lun->per_res[i].registered) { 8187 if (!persis_offset && i < CTL_MAX_INITIATORS) 8188 lun->pending_sense[i].ua_pending |= 8189 CTL_UA_RES_PREEMPT; 8190 else if (persis_offset && i >= persis_offset) 8191 lun->pending_sense[i-persis_offset 8192 ].ua_pending |= CTL_UA_RES_PREEMPT; 8193 8194 memset(&lun->per_res[i].res_key, 8195 0, sizeof(struct scsi_per_res_key)); 8196 lun->per_res[i].registered = 0; 8197 } 8198 lun->PRGeneration++; 8199 mtx_unlock(&softc->ctl_lock); 8200 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8201 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8202 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8203 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8204 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8205 printf("CTL:Persis Out error returned from " 8206 "ctl_ha_msg_send %d\n", isc_retval); 8207 } 8208 break; 8209 8210 case SPRO_PREEMPT: { 8211 int nretval; 8212 8213 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8214 residx, ctsio, cdb, param); 8215 if (nretval != 0) 8216 return (CTL_RETVAL_COMPLETE); 8217 break; 8218 } 8219 case SPRO_REG_MOVE: 8220 case SPRO_PRE_ABO: 8221 default: 8222 free(ctsio->kern_data_ptr, M_CTL); 8223 ctl_set_invalid_field(/*ctsio*/ ctsio, 8224 /*sks_valid*/ 1, 8225 /*command*/ 1, 8226 /*field*/ 1, 8227 /*bit_valid*/ 1, 8228 /*bit*/ 0); 8229 ctl_done((union ctl_io *)ctsio); 8230 return (CTL_RETVAL_COMPLETE); 8231 break; /* NOTREACHED */ 8232 } 8233 8234done: 8235 free(ctsio->kern_data_ptr, M_CTL); 8236 ctl_set_success(ctsio); 8237 ctl_done((union ctl_io *)ctsio); 8238 8239 return (retval); 8240} 8241 8242/* 8243 * This routine is for handling a message from the other SC pertaining to 8244 * persistent reserve out. All the error checking will have been done 8245 * so only perorming the action need be done here to keep the two 8246 * in sync. 8247 */ 8248static void 8249ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8250{ 8251 struct ctl_lun *lun; 8252 struct ctl_softc *softc; 8253 int i; 8254 8255 softc = control_softc; 8256 8257 mtx_lock(&softc->ctl_lock); 8258 8259 lun = softc->ctl_luns[msg->hdr.nexus.targ_lun]; 8260 switch(msg->pr.pr_info.action) { 8261 case CTL_PR_REG_KEY: 8262 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8263 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8264 lun->pr_key_count++; 8265 } 8266 lun->PRGeneration++; 8267 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8268 msg->pr.pr_info.sa_res_key, 8269 sizeof(struct scsi_per_res_key)); 8270 break; 8271 8272 case CTL_PR_UNREG_KEY: 8273 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8274 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8275 0, sizeof(struct scsi_per_res_key)); 8276 lun->pr_key_count--; 8277 8278 /* XXX Need to see if the reservation has been released */ 8279 /* if so do we need to generate UA? */ 8280 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8281 lun->flags &= ~CTL_LUN_PR_RESERVED; 8282 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8283 8284 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8285 || lun->res_type == SPR_TYPE_EX_AC_RO) 8286 && lun->pr_key_count) { 8287 /* 8288 * If the reservation is a registrants 8289 * only type we need to generate a UA 8290 * for other registered inits. The 8291 * sense code should be RESERVATIONS 8292 * RELEASED 8293 */ 8294 8295 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8296 if (lun->per_res[i+ 8297 persis_offset].registered == 0) 8298 continue; 8299 8300 lun->pending_sense[i 8301 ].ua_pending |= 8302 CTL_UA_RES_RELEASE; 8303 } 8304 } 8305 lun->res_type = 0; 8306 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8307 if (lun->pr_key_count==0) { 8308 lun->flags &= ~CTL_LUN_PR_RESERVED; 8309 lun->res_type = 0; 8310 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8311 } 8312 } 8313 lun->PRGeneration++; 8314 break; 8315 8316 case CTL_PR_RESERVE: 8317 lun->flags |= CTL_LUN_PR_RESERVED; 8318 lun->res_type = msg->pr.pr_info.res_type; 8319 lun->pr_res_idx = msg->pr.pr_info.residx; 8320 8321 break; 8322 8323 case CTL_PR_RELEASE: 8324 /* 8325 * if this isn't an exclusive access res generate UA for all 8326 * other registrants. 8327 */ 8328 if (lun->res_type != SPR_TYPE_EX_AC 8329 && lun->res_type != SPR_TYPE_WR_EX) { 8330 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8331 if (lun->per_res[i+persis_offset].registered) 8332 lun->pending_sense[i].ua_pending |= 8333 CTL_UA_RES_RELEASE; 8334 } 8335 8336 lun->flags &= ~CTL_LUN_PR_RESERVED; 8337 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8338 lun->res_type = 0; 8339 break; 8340 8341 case CTL_PR_PREEMPT: 8342 ctl_pro_preempt_other(lun, msg); 8343 break; 8344 case CTL_PR_CLEAR: 8345 lun->flags &= ~CTL_LUN_PR_RESERVED; 8346 lun->res_type = 0; 8347 lun->pr_key_count = 0; 8348 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8349 8350 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8351 if (lun->per_res[i].registered == 0) 8352 continue; 8353 if (!persis_offset 8354 && i < CTL_MAX_INITIATORS) 8355 lun->pending_sense[i].ua_pending |= 8356 CTL_UA_RES_PREEMPT; 8357 else if (persis_offset 8358 && i >= persis_offset) 8359 lun->pending_sense[i-persis_offset].ua_pending|= 8360 CTL_UA_RES_PREEMPT; 8361 memset(&lun->per_res[i].res_key, 0, 8362 sizeof(struct scsi_per_res_key)); 8363 lun->per_res[i].registered = 0; 8364 } 8365 lun->PRGeneration++; 8366 break; 8367 } 8368 8369 mtx_unlock(&softc->ctl_lock); 8370} 8371 8372int 8373ctl_read_write(struct ctl_scsiio *ctsio) 8374{ 8375 struct ctl_lun *lun; 8376 struct ctl_lba_len lbalen; 8377 uint64_t lba; 8378 uint32_t num_blocks; 8379 int reladdr, fua, dpo, ebp; 8380 int retval; 8381 int isread; 8382 8383 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8384 8385 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8386 8387 reladdr = 0; 8388 fua = 0; 8389 dpo = 0; 8390 ebp = 0; 8391 8392 retval = CTL_RETVAL_COMPLETE; 8393 8394 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8395 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8396 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8397 uint32_t residx; 8398 8399 /* 8400 * XXX KDM need a lock here. 8401 */ 8402 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8403 if ((lun->res_type == SPR_TYPE_EX_AC 8404 && residx != lun->pr_res_idx) 8405 || ((lun->res_type == SPR_TYPE_EX_AC_RO 8406 || lun->res_type == SPR_TYPE_EX_AC_AR) 8407 && !lun->per_res[residx].registered)) { 8408 ctl_set_reservation_conflict(ctsio); 8409 ctl_done((union ctl_io *)ctsio); 8410 return (CTL_RETVAL_COMPLETE); 8411 } 8412 } 8413 8414 switch (ctsio->cdb[0]) { 8415 case READ_6: 8416 case WRITE_6: { 8417 struct scsi_rw_6 *cdb; 8418 8419 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8420 8421 lba = scsi_3btoul(cdb->addr); 8422 /* only 5 bits are valid in the most significant address byte */ 8423 lba &= 0x1fffff; 8424 num_blocks = cdb->length; 8425 /* 8426 * This is correct according to SBC-2. 8427 */ 8428 if (num_blocks == 0) 8429 num_blocks = 256; 8430 break; 8431 } 8432 case READ_10: 8433 case WRITE_10: { 8434 struct scsi_rw_10 *cdb; 8435 8436 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8437 8438 if (cdb->byte2 & SRW10_RELADDR) 8439 reladdr = 1; 8440 if (cdb->byte2 & SRW10_FUA) 8441 fua = 1; 8442 if (cdb->byte2 & SRW10_DPO) 8443 dpo = 1; 8444 8445 if ((cdb->opcode == WRITE_10) 8446 && (cdb->byte2 & SRW10_EBP)) 8447 ebp = 1; 8448 8449 lba = scsi_4btoul(cdb->addr); 8450 num_blocks = scsi_2btoul(cdb->length); 8451 break; 8452 } 8453 case WRITE_VERIFY_10: { 8454 struct scsi_write_verify_10 *cdb; 8455 8456 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8457 8458 /* 8459 * XXX KDM we should do actual write verify support at some 8460 * point. This is obviously fake, we're just translating 8461 * things to a write. So we don't even bother checking the 8462 * BYTCHK field, since we don't do any verification. If 8463 * the user asks for it, we'll just pretend we did it. 8464 */ 8465 if (cdb->byte2 & SWV_DPO) 8466 dpo = 1; 8467 8468 lba = scsi_4btoul(cdb->addr); 8469 num_blocks = scsi_2btoul(cdb->length); 8470 break; 8471 } 8472 case READ_12: 8473 case WRITE_12: { 8474 struct scsi_rw_12 *cdb; 8475 8476 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8477 8478 if (cdb->byte2 & SRW12_RELADDR) 8479 reladdr = 1; 8480 if (cdb->byte2 & SRW12_FUA) 8481 fua = 1; 8482 if (cdb->byte2 & SRW12_DPO) 8483 dpo = 1; 8484 lba = scsi_4btoul(cdb->addr); 8485 num_blocks = scsi_4btoul(cdb->length); 8486 break; 8487 } 8488 case WRITE_VERIFY_12: { 8489 struct scsi_write_verify_12 *cdb; 8490 8491 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8492 8493 if (cdb->byte2 & SWV_DPO) 8494 dpo = 1; 8495 8496 lba = scsi_4btoul(cdb->addr); 8497 num_blocks = scsi_4btoul(cdb->length); 8498 8499 break; 8500 } 8501 case READ_16: 8502 case WRITE_16: { 8503 struct scsi_rw_16 *cdb; 8504 8505 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8506 8507 if (cdb->byte2 & SRW12_RELADDR) 8508 reladdr = 1; 8509 if (cdb->byte2 & SRW12_FUA) 8510 fua = 1; 8511 if (cdb->byte2 & SRW12_DPO) 8512 dpo = 1; 8513 8514 lba = scsi_8btou64(cdb->addr); 8515 num_blocks = scsi_4btoul(cdb->length); 8516 break; 8517 } 8518 case WRITE_VERIFY_16: { 8519 struct scsi_write_verify_16 *cdb; 8520 8521 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8522 8523 if (cdb->byte2 & SWV_DPO) 8524 dpo = 1; 8525 8526 lba = scsi_8btou64(cdb->addr); 8527 num_blocks = scsi_4btoul(cdb->length); 8528 break; 8529 } 8530 default: 8531 /* 8532 * We got a command we don't support. This shouldn't 8533 * happen, commands should be filtered out above us. 8534 */ 8535 ctl_set_invalid_opcode(ctsio); 8536 ctl_done((union ctl_io *)ctsio); 8537 8538 return (CTL_RETVAL_COMPLETE); 8539 break; /* NOTREACHED */ 8540 } 8541 8542 /* 8543 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8544 * interesting for us, but if RAIDCore is in write-back mode, 8545 * getting it to do write-through for a particular transaction may 8546 * not be possible. 8547 */ 8548 /* 8549 * We don't support relative addressing. That also requires 8550 * supporting linked commands, which we don't do. 8551 */ 8552 if (reladdr != 0) { 8553 ctl_set_invalid_field(ctsio, 8554 /*sks_valid*/ 1, 8555 /*command*/ 1, 8556 /*field*/ 1, 8557 /*bit_valid*/ 1, 8558 /*bit*/ 0); 8559 ctl_done((union ctl_io *)ctsio); 8560 return (CTL_RETVAL_COMPLETE); 8561 } 8562 8563 /* 8564 * The first check is to make sure we're in bounds, the second 8565 * check is to catch wrap-around problems. If the lba + num blocks 8566 * is less than the lba, then we've wrapped around and the block 8567 * range is invalid anyway. 8568 */ 8569 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8570 || ((lba + num_blocks) < lba)) { 8571 ctl_set_lba_out_of_range(ctsio); 8572 ctl_done((union ctl_io *)ctsio); 8573 return (CTL_RETVAL_COMPLETE); 8574 } 8575 8576 /* 8577 * According to SBC-3, a transfer length of 0 is not an error. 8578 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8579 * translates to 256 blocks for those commands. 8580 */ 8581 if (num_blocks == 0) { 8582 ctl_set_success(ctsio); 8583 ctl_done((union ctl_io *)ctsio); 8584 return (CTL_RETVAL_COMPLETE); 8585 } 8586 8587 lbalen.lba = lba; 8588 lbalen.len = num_blocks; 8589 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen, 8590 sizeof(lbalen)); 8591 8592 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8593 8594 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8595 8596 return (retval); 8597} 8598 8599int 8600ctl_report_luns(struct ctl_scsiio *ctsio) 8601{ 8602 struct scsi_report_luns *cdb; 8603 struct scsi_report_luns_data *lun_data; 8604 struct ctl_lun *lun, *request_lun; 8605 int num_luns, retval; 8606 uint32_t alloc_len, lun_datalen; 8607 int num_filled, well_known; 8608 uint32_t initidx; 8609 8610 retval = CTL_RETVAL_COMPLETE; 8611 well_known = 0; 8612 8613 cdb = (struct scsi_report_luns *)ctsio->cdb; 8614 8615 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8616 8617 mtx_lock(&control_softc->ctl_lock); 8618 num_luns = control_softc->num_luns; 8619 mtx_unlock(&control_softc->ctl_lock); 8620 8621 switch (cdb->select_report) { 8622 case RPL_REPORT_DEFAULT: 8623 case RPL_REPORT_ALL: 8624 break; 8625 case RPL_REPORT_WELLKNOWN: 8626 well_known = 1; 8627 num_luns = 0; 8628 break; 8629 default: 8630 ctl_set_invalid_field(ctsio, 8631 /*sks_valid*/ 1, 8632 /*command*/ 1, 8633 /*field*/ 2, 8634 /*bit_valid*/ 0, 8635 /*bit*/ 0); 8636 ctl_done((union ctl_io *)ctsio); 8637 return (retval); 8638 break; /* NOTREACHED */ 8639 } 8640 8641 alloc_len = scsi_4btoul(cdb->length); 8642 /* 8643 * The initiator has to allocate at least 16 bytes for this request, 8644 * so he can at least get the header and the first LUN. Otherwise 8645 * we reject the request (per SPC-3 rev 14, section 6.21). 8646 */ 8647 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 8648 sizeof(struct scsi_report_luns_lundata))) { 8649 ctl_set_invalid_field(ctsio, 8650 /*sks_valid*/ 1, 8651 /*command*/ 1, 8652 /*field*/ 6, 8653 /*bit_valid*/ 0, 8654 /*bit*/ 0); 8655 ctl_done((union ctl_io *)ctsio); 8656 return (retval); 8657 } 8658 8659 request_lun = (struct ctl_lun *) 8660 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8661 8662 lun_datalen = sizeof(*lun_data) + 8663 (num_luns * sizeof(struct scsi_report_luns_lundata)); 8664 8665 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 8666 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 8667 ctsio->kern_sg_entries = 0; 8668 8669 if (lun_datalen < alloc_len) { 8670 ctsio->residual = alloc_len - lun_datalen; 8671 ctsio->kern_data_len = lun_datalen; 8672 ctsio->kern_total_len = lun_datalen; 8673 } else { 8674 ctsio->residual = 0; 8675 ctsio->kern_data_len = alloc_len; 8676 ctsio->kern_total_len = alloc_len; 8677 } 8678 ctsio->kern_data_resid = 0; 8679 ctsio->kern_rel_offset = 0; 8680 ctsio->kern_sg_entries = 0; 8681 8682 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8683 8684 /* 8685 * We set this to the actual data length, regardless of how much 8686 * space we actually have to return results. If the user looks at 8687 * this value, he'll know whether or not he allocated enough space 8688 * and reissue the command if necessary. We don't support well 8689 * known logical units, so if the user asks for that, return none. 8690 */ 8691 scsi_ulto4b(lun_datalen - 8, lun_data->length); 8692 8693 mtx_lock(&control_softc->ctl_lock); 8694 for (num_filled = 0, lun = STAILQ_FIRST(&control_softc->lun_list); 8695 (lun != NULL) && (num_filled < num_luns); 8696 lun = STAILQ_NEXT(lun, links)) { 8697 8698 if (lun->lun <= 0xff) { 8699 /* 8700 * Peripheral addressing method, bus number 0. 8701 */ 8702 lun_data->luns[num_filled].lundata[0] = 8703 RPL_LUNDATA_ATYP_PERIPH; 8704 lun_data->luns[num_filled].lundata[1] = lun->lun; 8705 num_filled++; 8706 } else if (lun->lun <= 0x3fff) { 8707 /* 8708 * Flat addressing method. 8709 */ 8710 lun_data->luns[num_filled].lundata[0] = 8711 RPL_LUNDATA_ATYP_FLAT | 8712 (lun->lun & RPL_LUNDATA_FLAT_LUN_MASK); 8713#ifdef OLDCTLHEADERS 8714 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 8715 (lun->lun & SRLD_BUS_LUN_MASK); 8716#endif 8717 lun_data->luns[num_filled].lundata[1] = 8718#ifdef OLDCTLHEADERS 8719 lun->lun >> SRLD_BUS_LUN_BITS; 8720#endif 8721 lun->lun >> RPL_LUNDATA_FLAT_LUN_BITS; 8722 num_filled++; 8723 } else { 8724 printf("ctl_report_luns: bogus LUN number %jd, " 8725 "skipping\n", (intmax_t)lun->lun); 8726 } 8727 /* 8728 * According to SPC-3, rev 14 section 6.21: 8729 * 8730 * "The execution of a REPORT LUNS command to any valid and 8731 * installed logical unit shall clear the REPORTED LUNS DATA 8732 * HAS CHANGED unit attention condition for all logical 8733 * units of that target with respect to the requesting 8734 * initiator. A valid and installed logical unit is one 8735 * having a PERIPHERAL QUALIFIER of 000b in the standard 8736 * INQUIRY data (see 6.4.2)." 8737 * 8738 * If request_lun is NULL, the LUN this report luns command 8739 * was issued to is either disabled or doesn't exist. In that 8740 * case, we shouldn't clear any pending lun change unit 8741 * attention. 8742 */ 8743 if (request_lun != NULL) 8744 lun->pending_sense[initidx].ua_pending &= 8745 ~CTL_UA_LUN_CHANGE; 8746 } 8747 mtx_unlock(&control_softc->ctl_lock); 8748 8749 /* 8750 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 8751 * this request. 8752 */ 8753 ctsio->scsi_status = SCSI_STATUS_OK; 8754 8755 ctsio->be_move_done = ctl_config_move_done; 8756 ctl_datamove((union ctl_io *)ctsio); 8757 8758 return (retval); 8759} 8760 8761int 8762ctl_request_sense(struct ctl_scsiio *ctsio) 8763{ 8764 struct scsi_request_sense *cdb; 8765 struct scsi_sense_data *sense_ptr; 8766 struct ctl_lun *lun; 8767 uint32_t initidx; 8768 int have_error; 8769 scsi_sense_data_type sense_format; 8770 8771 cdb = (struct scsi_request_sense *)ctsio->cdb; 8772 8773 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8774 8775 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 8776 8777 /* 8778 * Determine which sense format the user wants. 8779 */ 8780 if (cdb->byte2 & SRS_DESC) 8781 sense_format = SSD_TYPE_DESC; 8782 else 8783 sense_format = SSD_TYPE_FIXED; 8784 8785 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 8786 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 8787 ctsio->kern_sg_entries = 0; 8788 8789 /* 8790 * struct scsi_sense_data, which is currently set to 256 bytes, is 8791 * larger than the largest allowed value for the length field in the 8792 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 8793 */ 8794 ctsio->residual = 0; 8795 ctsio->kern_data_len = cdb->length; 8796 ctsio->kern_total_len = cdb->length; 8797 8798 ctsio->kern_data_resid = 0; 8799 ctsio->kern_rel_offset = 0; 8800 ctsio->kern_sg_entries = 0; 8801 8802 /* 8803 * If we don't have a LUN, we don't have any pending sense. 8804 */ 8805 if (lun == NULL) 8806 goto no_sense; 8807 8808 have_error = 0; 8809 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8810 /* 8811 * Check for pending sense, and then for pending unit attentions. 8812 * Pending sense gets returned first, then pending unit attentions. 8813 */ 8814 mtx_lock(&lun->ctl_softc->ctl_lock); 8815 if (ctl_is_set(lun->have_ca, initidx)) { 8816 scsi_sense_data_type stored_format; 8817 8818 /* 8819 * Check to see which sense format was used for the stored 8820 * sense data. 8821 */ 8822 stored_format = scsi_sense_type( 8823 &lun->pending_sense[initidx].sense); 8824 8825 /* 8826 * If the user requested a different sense format than the 8827 * one we stored, then we need to convert it to the other 8828 * format. If we're going from descriptor to fixed format 8829 * sense data, we may lose things in translation, depending 8830 * on what options were used. 8831 * 8832 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 8833 * for some reason we'll just copy it out as-is. 8834 */ 8835 if ((stored_format == SSD_TYPE_FIXED) 8836 && (sense_format == SSD_TYPE_DESC)) 8837 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 8838 &lun->pending_sense[initidx].sense, 8839 (struct scsi_sense_data_desc *)sense_ptr); 8840 else if ((stored_format == SSD_TYPE_DESC) 8841 && (sense_format == SSD_TYPE_FIXED)) 8842 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 8843 &lun->pending_sense[initidx].sense, 8844 (struct scsi_sense_data_fixed *)sense_ptr); 8845 else 8846 memcpy(sense_ptr, &lun->pending_sense[initidx].sense, 8847 ctl_min(sizeof(*sense_ptr), 8848 sizeof(lun->pending_sense[initidx].sense))); 8849 8850 ctl_clear_mask(lun->have_ca, initidx); 8851 have_error = 1; 8852 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) { 8853 ctl_ua_type ua_type; 8854 8855 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending, 8856 sense_ptr, sense_format); 8857 if (ua_type != CTL_UA_NONE) { 8858 have_error = 1; 8859 /* We're reporting this UA, so clear it */ 8860 lun->pending_sense[initidx].ua_pending &= ~ua_type; 8861 } 8862 } 8863 mtx_unlock(&lun->ctl_softc->ctl_lock); 8864 8865 /* 8866 * We already have a pending error, return it. 8867 */ 8868 if (have_error != 0) { 8869 /* 8870 * We report the SCSI status as OK, since the status of the 8871 * request sense command itself is OK. 8872 */ 8873 ctsio->scsi_status = SCSI_STATUS_OK; 8874 8875 /* 8876 * We report 0 for the sense length, because we aren't doing 8877 * autosense in this case. We're reporting sense as 8878 * parameter data. 8879 */ 8880 ctsio->sense_len = 0; 8881 8882 ctsio->be_move_done = ctl_config_move_done; 8883 ctl_datamove((union ctl_io *)ctsio); 8884 8885 return (CTL_RETVAL_COMPLETE); 8886 } 8887 8888no_sense: 8889 8890 /* 8891 * No sense information to report, so we report that everything is 8892 * okay. 8893 */ 8894 ctl_set_sense_data(sense_ptr, 8895 lun, 8896 sense_format, 8897 /*current_error*/ 1, 8898 /*sense_key*/ SSD_KEY_NO_SENSE, 8899 /*asc*/ 0x00, 8900 /*ascq*/ 0x00, 8901 SSD_ELEM_NONE); 8902 8903 ctsio->scsi_status = SCSI_STATUS_OK; 8904 8905 /* 8906 * We report 0 for the sense length, because we aren't doing 8907 * autosense in this case. We're reporting sense as parameter data. 8908 */ 8909 ctsio->sense_len = 0; 8910 ctsio->be_move_done = ctl_config_move_done; 8911 ctl_datamove((union ctl_io *)ctsio); 8912 8913 return (CTL_RETVAL_COMPLETE); 8914} 8915 8916int 8917ctl_tur(struct ctl_scsiio *ctsio) 8918{ 8919 struct ctl_lun *lun; 8920 8921 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8922 8923 CTL_DEBUG_PRINT(("ctl_tur\n")); 8924 8925 if (lun == NULL) 8926 return (-EINVAL); 8927 8928 ctsio->scsi_status = SCSI_STATUS_OK; 8929 ctsio->io_hdr.status = CTL_SUCCESS; 8930 8931 ctl_done((union ctl_io *)ctsio); 8932 8933 return (CTL_RETVAL_COMPLETE); 8934} 8935 8936#ifdef notyet 8937static int 8938ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 8939{ 8940 8941} 8942#endif 8943 8944static int 8945ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 8946{ 8947 struct scsi_vpd_supported_pages *pages; 8948 int sup_page_size; 8949 struct ctl_lun *lun; 8950 8951 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8952 8953 sup_page_size = sizeof(struct scsi_vpd_supported_pages) + 8954 SCSI_EVPD_NUM_SUPPORTED_PAGES; 8955 /* 8956 * XXX KDM GFP_??? We probably don't want to wait here, 8957 * unless we end up having a process/thread context. 8958 */ 8959 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 8960 if (ctsio->kern_data_ptr == NULL) { 8961 ctsio->io_hdr.status = CTL_SCSI_ERROR; 8962 ctsio->scsi_status = SCSI_STATUS_BUSY; 8963 ctl_done((union ctl_io *)ctsio); 8964 return (CTL_RETVAL_COMPLETE); 8965 } 8966 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 8967 ctsio->kern_sg_entries = 0; 8968 8969 if (sup_page_size < alloc_len) { 8970 ctsio->residual = alloc_len - sup_page_size; 8971 ctsio->kern_data_len = sup_page_size; 8972 ctsio->kern_total_len = sup_page_size; 8973 } else { 8974 ctsio->residual = 0; 8975 ctsio->kern_data_len = alloc_len; 8976 ctsio->kern_total_len = alloc_len; 8977 } 8978 ctsio->kern_data_resid = 0; 8979 ctsio->kern_rel_offset = 0; 8980 ctsio->kern_sg_entries = 0; 8981 8982 /* 8983 * The control device is always connected. The disk device, on the 8984 * other hand, may not be online all the time. Need to change this 8985 * to figure out whether the disk device is actually online or not. 8986 */ 8987 if (lun != NULL) 8988 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 8989 lun->be_lun->lun_type; 8990 else 8991 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 8992 8993 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 8994 /* Supported VPD pages */ 8995 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 8996 /* Serial Number */ 8997 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 8998 /* Device Identification */ 8999 pages->page_list[2] = SVPD_DEVICE_ID; 9000 9001 ctsio->scsi_status = SCSI_STATUS_OK; 9002 9003 ctsio->be_move_done = ctl_config_move_done; 9004 ctl_datamove((union ctl_io *)ctsio); 9005 9006 return (CTL_RETVAL_COMPLETE); 9007} 9008 9009static int 9010ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9011{ 9012 struct scsi_vpd_unit_serial_number *sn_ptr; 9013 struct ctl_lun *lun; 9014#ifndef CTL_USE_BACKEND_SN 9015 char tmpstr[32]; 9016#endif 9017 9018 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9019 9020 /* XXX KDM which malloc flags here?? */ 9021 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9022 if (ctsio->kern_data_ptr == NULL) { 9023 ctsio->io_hdr.status = CTL_SCSI_ERROR; 9024 ctsio->scsi_status = SCSI_STATUS_BUSY; 9025 ctl_done((union ctl_io *)ctsio); 9026 return (CTL_RETVAL_COMPLETE); 9027 } 9028 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9029 ctsio->kern_sg_entries = 0; 9030 9031 if (sizeof(*sn_ptr) < alloc_len) { 9032 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9033 ctsio->kern_data_len = sizeof(*sn_ptr); 9034 ctsio->kern_total_len = sizeof(*sn_ptr); 9035 } else { 9036 ctsio->residual = 0; 9037 ctsio->kern_data_len = alloc_len; 9038 ctsio->kern_total_len = alloc_len; 9039 } 9040 ctsio->kern_data_resid = 0; 9041 ctsio->kern_rel_offset = 0; 9042 ctsio->kern_sg_entries = 0; 9043 9044 /* 9045 * The control device is always connected. The disk device, on the 9046 * other hand, may not be online all the time. Need to change this 9047 * to figure out whether the disk device is actually online or not. 9048 */ 9049 if (lun != NULL) 9050 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9051 lun->be_lun->lun_type; 9052 else 9053 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9054 9055 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9056 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9057#ifdef CTL_USE_BACKEND_SN 9058 /* 9059 * If we don't have a LUN, we just leave the serial number as 9060 * all spaces. 9061 */ 9062 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9063 if (lun != NULL) { 9064 strncpy((char *)sn_ptr->serial_num, 9065 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9066 } 9067#else 9068 /* 9069 * Note that we're using a non-unique serial number here, 9070 */ 9071 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000"); 9072 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9073 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN, 9074 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4))); 9075#endif 9076 ctsio->scsi_status = SCSI_STATUS_OK; 9077 9078 ctsio->be_move_done = ctl_config_move_done; 9079 ctl_datamove((union ctl_io *)ctsio); 9080 9081 return (CTL_RETVAL_COMPLETE); 9082} 9083 9084 9085static int 9086ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9087{ 9088 struct scsi_vpd_device_id *devid_ptr; 9089 struct scsi_vpd_id_descriptor *desc, *desc1; 9090 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */ 9091 struct scsi_vpd_id_t10 *t10id; 9092 struct ctl_softc *ctl_softc; 9093 struct ctl_lun *lun; 9094 struct ctl_frontend *fe; 9095#ifndef CTL_USE_BACKEND_SN 9096 char tmpstr[32]; 9097#endif /* CTL_USE_BACKEND_SN */ 9098 int devid_len; 9099 9100 ctl_softc = control_softc; 9101 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9102 9103 devid_len = sizeof(struct scsi_vpd_device_id) + 9104 sizeof(struct scsi_vpd_id_descriptor) + 9105 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN + 9106 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN + 9107 sizeof(struct scsi_vpd_id_descriptor) + 9108 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9109 sizeof(struct scsi_vpd_id_descriptor) + 9110 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9111 9112 /* XXX KDM which malloc flags here ?? */ 9113 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO); 9114 if (ctsio->kern_data_ptr == NULL) { 9115 ctsio->io_hdr.status = CTL_SCSI_ERROR; 9116 ctsio->scsi_status = SCSI_STATUS_BUSY; 9117 ctl_done((union ctl_io *)ctsio); 9118 return (CTL_RETVAL_COMPLETE); 9119 } 9120 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9121 ctsio->kern_sg_entries = 0; 9122 9123 if (devid_len < alloc_len) { 9124 ctsio->residual = alloc_len - devid_len; 9125 ctsio->kern_data_len = devid_len; 9126 ctsio->kern_total_len = devid_len; 9127 } else { 9128 ctsio->residual = 0; 9129 ctsio->kern_data_len = alloc_len; 9130 ctsio->kern_total_len = alloc_len; 9131 } 9132 ctsio->kern_data_resid = 0; 9133 ctsio->kern_rel_offset = 0; 9134 ctsio->kern_sg_entries = 0; 9135 9136 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9137 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 9138 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9139 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN); 9140 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] + 9141 CTL_WWPN_LEN); 9142 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] + 9143 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9144 9145 /* 9146 * The control device is always connected. The disk device, on the 9147 * other hand, may not be online all the time. 9148 */ 9149 if (lun != NULL) 9150 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9151 lun->be_lun->lun_type; 9152 else 9153 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9154 9155 devid_ptr->page_code = SVPD_DEVICE_ID; 9156 9157 scsi_ulto2b(devid_len - 4, devid_ptr->length); 9158 9159 mtx_lock(&ctl_softc->ctl_lock); 9160 9161 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9162 9163 /* 9164 * For Fibre channel, 9165 */ 9166 if (fe->port_type == CTL_PORT_FC) 9167 { 9168 desc->proto_codeset = (SCSI_PROTO_FC << 4) | 9169 SVPD_ID_CODESET_ASCII; 9170 desc1->proto_codeset = (SCSI_PROTO_FC << 4) | 9171 SVPD_ID_CODESET_BINARY; 9172 } 9173 else 9174 { 9175 desc->proto_codeset = (SCSI_PROTO_SPI << 4) | 9176 SVPD_ID_CODESET_ASCII; 9177 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) | 9178 SVPD_ID_CODESET_BINARY; 9179 } 9180 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset; 9181 mtx_unlock(&ctl_softc->ctl_lock); 9182 9183 /* 9184 * We're using a LUN association here. i.e., this device ID is a 9185 * per-LUN identifier. 9186 */ 9187 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 9188 desc->length = sizeof(*t10id) + CTL_DEVID_LEN; 9189 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 9190 9191 /* 9192 * desc1 is for the WWPN which is a port asscociation. 9193 */ 9194 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA; 9195 desc1->length = CTL_WWPN_LEN; 9196 /* XXX Call Reggie's get_WWNN func here then add port # to the end */ 9197 /* For testing just create the WWPN */ 9198#if 0 9199 ddb_GetWWNN((char *)desc1->identifier); 9200 9201 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9202 /* This is so Copancontrol will return something sane */ 9203 if (ctsio->io_hdr.nexus.targ_port!=0 && 9204 ctsio->io_hdr.nexus.targ_port!=8) 9205 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1; 9206 else 9207 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port; 9208#endif 9209 9210 be64enc(desc1->identifier, fe->wwpn); 9211 9212 /* 9213 * desc2 is for the Relative Target Port(type 4h) identifier 9214 */ 9215 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9216 | SVPD_ID_TYPE_RELTARG; 9217 desc2->length = 4; 9218//#if 0 9219 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9220 /* This is so Copancontrol will return something sane */ 9221 if (ctsio->io_hdr.nexus.targ_port!=0 && 9222 ctsio->io_hdr.nexus.targ_port!=8) 9223 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1; 9224 else 9225 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port; 9226//#endif 9227 9228 /* 9229 * desc3 is for the Target Port Group(type 5h) identifier 9230 */ 9231 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9232 | SVPD_ID_TYPE_TPORTGRP; 9233 desc3->length = 4; 9234 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single) 9235 desc3->identifier[3] = 1; 9236 else 9237 desc3->identifier[3] = 2; 9238 9239#ifdef CTL_USE_BACKEND_SN 9240 /* 9241 * If we've actually got a backend, copy the device id from the 9242 * per-LUN data. Otherwise, set it to all spaces. 9243 */ 9244 if (lun != NULL) { 9245 /* 9246 * Copy the backend's LUN ID. 9247 */ 9248 strncpy((char *)t10id->vendor_spec_id, 9249 (char *)lun->be_lun->device_id, CTL_DEVID_LEN); 9250 } else { 9251 /* 9252 * No backend, set this to spaces. 9253 */ 9254 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN); 9255 } 9256#else 9257 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d", 9258 (lun != NULL) ? (int)lun->lun : 0); 9259 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN, 9260 sizeof(tmpstr))); 9261#endif 9262 9263 ctsio->scsi_status = SCSI_STATUS_OK; 9264 9265 ctsio->be_move_done = ctl_config_move_done; 9266 ctl_datamove((union ctl_io *)ctsio); 9267 9268 return (CTL_RETVAL_COMPLETE); 9269} 9270 9271static int 9272ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9273{ 9274 struct scsi_inquiry *cdb; 9275 int alloc_len, retval; 9276 9277 cdb = (struct scsi_inquiry *)ctsio->cdb; 9278 9279 retval = CTL_RETVAL_COMPLETE; 9280 9281 alloc_len = scsi_2btoul(cdb->length); 9282 9283 switch (cdb->page_code) { 9284 case SVPD_SUPPORTED_PAGES: 9285 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9286 break; 9287 case SVPD_UNIT_SERIAL_NUMBER: 9288 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9289 break; 9290 case SVPD_DEVICE_ID: 9291 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9292 break; 9293 default: 9294 ctl_set_invalid_field(ctsio, 9295 /*sks_valid*/ 1, 9296 /*command*/ 1, 9297 /*field*/ 2, 9298 /*bit_valid*/ 0, 9299 /*bit*/ 0); 9300 ctl_done((union ctl_io *)ctsio); 9301 retval = CTL_RETVAL_COMPLETE; 9302 break; 9303 } 9304 9305 return (retval); 9306} 9307 9308static int 9309ctl_inquiry_std(struct ctl_scsiio *ctsio) 9310{ 9311 struct scsi_inquiry_data *inq_ptr; 9312 struct scsi_inquiry *cdb; 9313 struct ctl_softc *ctl_softc; 9314 struct ctl_lun *lun; 9315 uint32_t alloc_len; 9316 int is_fc; 9317 9318 ctl_softc = control_softc; 9319 9320 /* 9321 * Figure out whether we're talking to a Fibre Channel port or not. 9322 * We treat the ioctl front end, and any SCSI adapters, as packetized 9323 * SCSI front ends. 9324 */ 9325 mtx_lock(&ctl_softc->ctl_lock); 9326 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type != 9327 CTL_PORT_FC) 9328 is_fc = 0; 9329 else 9330 is_fc = 1; 9331 mtx_unlock(&ctl_softc->ctl_lock); 9332 9333 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9334 cdb = (struct scsi_inquiry *)ctsio->cdb; 9335 alloc_len = scsi_2btoul(cdb->length); 9336 9337 /* 9338 * We malloc the full inquiry data size here and fill it 9339 * in. If the user only asks for less, we'll give him 9340 * that much. 9341 */ 9342 /* XXX KDM what malloc flags should we use here?? */ 9343 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 9344 if (ctsio->kern_data_ptr == NULL) { 9345 ctsio->io_hdr.status = CTL_SCSI_ERROR; 9346 ctsio->scsi_status = SCSI_STATUS_BUSY; 9347 ctl_done((union ctl_io *)ctsio); 9348 return (CTL_RETVAL_COMPLETE); 9349 } 9350 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 9351 ctsio->kern_sg_entries = 0; 9352 ctsio->kern_data_resid = 0; 9353 ctsio->kern_rel_offset = 0; 9354 9355 if (sizeof(*inq_ptr) < alloc_len) { 9356 ctsio->residual = alloc_len - sizeof(*inq_ptr); 9357 ctsio->kern_data_len = sizeof(*inq_ptr); 9358 ctsio->kern_total_len = sizeof(*inq_ptr); 9359 } else { 9360 ctsio->residual = 0; 9361 ctsio->kern_data_len = alloc_len; 9362 ctsio->kern_total_len = alloc_len; 9363 } 9364 9365 /* 9366 * If we have a LUN configured, report it as connected. Otherwise, 9367 * report that it is offline or no device is supported, depending 9368 * on the value of inquiry_pq_no_lun. 9369 * 9370 * According to the spec (SPC-4 r34), the peripheral qualifier 9371 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 9372 * 9373 * "A peripheral device having the specified peripheral device type 9374 * is not connected to this logical unit. However, the device 9375 * server is capable of supporting the specified peripheral device 9376 * type on this logical unit." 9377 * 9378 * According to the same spec, the peripheral qualifier 9379 * SID_QUAL_BAD_LU (011b) is used in this scenario: 9380 * 9381 * "The device server is not capable of supporting a peripheral 9382 * device on this logical unit. For this peripheral qualifier the 9383 * peripheral device type shall be set to 1Fh. All other peripheral 9384 * device type values are reserved for this peripheral qualifier." 9385 * 9386 * Given the text, it would seem that we probably want to report that 9387 * the LUN is offline here. There is no LUN connected, but we can 9388 * support a LUN at the given LUN number. 9389 * 9390 * In the real world, though, it sounds like things are a little 9391 * different: 9392 * 9393 * - Linux, when presented with a LUN with the offline peripheral 9394 * qualifier, will create an sg driver instance for it. So when 9395 * you attach it to CTL, you wind up with a ton of sg driver 9396 * instances. (One for every LUN that Linux bothered to probe.) 9397 * Linux does this despite the fact that it issues a REPORT LUNs 9398 * to LUN 0 to get the inventory of supported LUNs. 9399 * 9400 * - There is other anecdotal evidence (from Emulex folks) about 9401 * arrays that use the offline peripheral qualifier for LUNs that 9402 * are on the "passive" path in an active/passive array. 9403 * 9404 * So the solution is provide a hopefully reasonable default 9405 * (return bad/no LUN) and allow the user to change the behavior 9406 * with a tunable/sysctl variable. 9407 */ 9408 if (lun != NULL) 9409 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9410 lun->be_lun->lun_type; 9411 else if (ctl_softc->inquiry_pq_no_lun == 0) 9412 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9413 else 9414 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 9415 9416 /* RMB in byte 2 is 0 */ 9417 inq_ptr->version = SCSI_REV_SPC3; 9418 9419 /* 9420 * According to SAM-3, even if a device only supports a single 9421 * level of LUN addressing, it should still set the HISUP bit: 9422 * 9423 * 4.9.1 Logical unit numbers overview 9424 * 9425 * All logical unit number formats described in this standard are 9426 * hierarchical in structure even when only a single level in that 9427 * hierarchy is used. The HISUP bit shall be set to one in the 9428 * standard INQUIRY data (see SPC-2) when any logical unit number 9429 * format described in this standard is used. Non-hierarchical 9430 * formats are outside the scope of this standard. 9431 * 9432 * Therefore we set the HiSup bit here. 9433 * 9434 * The reponse format is 2, per SPC-3. 9435 */ 9436 inq_ptr->response_format = SID_HiSup | 2; 9437 9438 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 9439 CTL_DEBUG_PRINT(("additional_length = %d\n", 9440 inq_ptr->additional_length)); 9441 9442 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT; 9443 /* 16 bit addressing */ 9444 if (is_fc == 0) 9445 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 9446 /* XXX set the SID_MultiP bit here if we're actually going to 9447 respond on multiple ports */ 9448 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 9449 9450 /* 16 bit data bus, synchronous transfers */ 9451 /* XXX these flags don't apply for FC */ 9452 if (is_fc == 0) 9453 inq_ptr->flags = SID_WBus16 | SID_Sync; 9454 /* 9455 * XXX KDM do we want to support tagged queueing on the control 9456 * device at all? 9457 */ 9458 if ((lun == NULL) 9459 || (lun->be_lun->lun_type != T_PROCESSOR)) 9460 inq_ptr->flags |= SID_CmdQue; 9461 /* 9462 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 9463 * We have 8 bytes for the vendor name, and 16 bytes for the device 9464 * name and 4 bytes for the revision. 9465 */ 9466 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 9467 if (lun == NULL) { 9468 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 9469 } else { 9470 switch (lun->be_lun->lun_type) { 9471 case T_DIRECT: 9472 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 9473 break; 9474 case T_PROCESSOR: 9475 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 9476 break; 9477 default: 9478 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 9479 break; 9480 } 9481 } 9482 9483 /* 9484 * XXX make this a macro somewhere so it automatically gets 9485 * incremented when we make changes. 9486 */ 9487 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 9488 9489 /* 9490 * For parallel SCSI, we support double transition and single 9491 * transition clocking. We also support QAS (Quick Arbitration 9492 * and Selection) and Information Unit transfers on both the 9493 * control and array devices. 9494 */ 9495 if (is_fc == 0) 9496 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 9497 SID_SPI_IUS; 9498 9499 /* SAM-3 */ 9500 scsi_ulto2b(0x0060, inq_ptr->version1); 9501 /* SPC-3 (no version claimed) XXX should we claim a version? */ 9502 scsi_ulto2b(0x0300, inq_ptr->version2); 9503 if (is_fc) { 9504 /* FCP-2 ANSI INCITS.350:2003 */ 9505 scsi_ulto2b(0x0917, inq_ptr->version3); 9506 } else { 9507 /* SPI-4 ANSI INCITS.362:200x */ 9508 scsi_ulto2b(0x0B56, inq_ptr->version3); 9509 } 9510 9511 if (lun == NULL) { 9512 /* SBC-2 (no version claimed) XXX should we claim a version? */ 9513 scsi_ulto2b(0x0320, inq_ptr->version4); 9514 } else { 9515 switch (lun->be_lun->lun_type) { 9516 case T_DIRECT: 9517 /* 9518 * SBC-2 (no version claimed) XXX should we claim a 9519 * version? 9520 */ 9521 scsi_ulto2b(0x0320, inq_ptr->version4); 9522 break; 9523 case T_PROCESSOR: 9524 default: 9525 break; 9526 } 9527 } 9528 9529 ctsio->scsi_status = SCSI_STATUS_OK; 9530 if (ctsio->kern_data_len > 0) { 9531 ctsio->be_move_done = ctl_config_move_done; 9532 ctl_datamove((union ctl_io *)ctsio); 9533 } else { 9534 ctsio->io_hdr.status = CTL_SUCCESS; 9535 ctl_done((union ctl_io *)ctsio); 9536 } 9537 9538 return (CTL_RETVAL_COMPLETE); 9539} 9540 9541int 9542ctl_inquiry(struct ctl_scsiio *ctsio) 9543{ 9544 struct scsi_inquiry *cdb; 9545 int retval; 9546 9547 cdb = (struct scsi_inquiry *)ctsio->cdb; 9548 9549 retval = 0; 9550 9551 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 9552 9553 /* 9554 * Right now, we don't support the CmdDt inquiry information. 9555 * This would be nice to support in the future. When we do 9556 * support it, we should change this test so that it checks to make 9557 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 9558 */ 9559#ifdef notyet 9560 if (((cdb->byte2 & SI_EVPD) 9561 && (cdb->byte2 & SI_CMDDT))) 9562#endif 9563 if (cdb->byte2 & SI_CMDDT) { 9564 /* 9565 * Point to the SI_CMDDT bit. We might change this 9566 * when we support SI_CMDDT, but since both bits would be 9567 * "wrong", this should probably just stay as-is then. 9568 */ 9569 ctl_set_invalid_field(ctsio, 9570 /*sks_valid*/ 1, 9571 /*command*/ 1, 9572 /*field*/ 1, 9573 /*bit_valid*/ 1, 9574 /*bit*/ 1); 9575 ctl_done((union ctl_io *)ctsio); 9576 return (CTL_RETVAL_COMPLETE); 9577 } 9578 if (cdb->byte2 & SI_EVPD) 9579 retval = ctl_inquiry_evpd(ctsio); 9580#ifdef notyet 9581 else if (cdb->byte2 & SI_CMDDT) 9582 retval = ctl_inquiry_cmddt(ctsio); 9583#endif 9584 else 9585 retval = ctl_inquiry_std(ctsio); 9586 9587 return (retval); 9588} 9589 9590/* 9591 * For known CDB types, parse the LBA and length. 9592 */ 9593static int 9594ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 9595{ 9596 if (io->io_hdr.io_type != CTL_IO_SCSI) 9597 return (1); 9598 9599 switch (io->scsiio.cdb[0]) { 9600 case READ_6: 9601 case WRITE_6: { 9602 struct scsi_rw_6 *cdb; 9603 9604 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 9605 9606 *lba = scsi_3btoul(cdb->addr); 9607 /* only 5 bits are valid in the most significant address byte */ 9608 *lba &= 0x1fffff; 9609 *len = cdb->length; 9610 break; 9611 } 9612 case READ_10: 9613 case WRITE_10: { 9614 struct scsi_rw_10 *cdb; 9615 9616 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 9617 9618 *lba = scsi_4btoul(cdb->addr); 9619 *len = scsi_2btoul(cdb->length); 9620 break; 9621 } 9622 case WRITE_VERIFY_10: { 9623 struct scsi_write_verify_10 *cdb; 9624 9625 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 9626 9627 *lba = scsi_4btoul(cdb->addr); 9628 *len = scsi_2btoul(cdb->length); 9629 break; 9630 } 9631 case READ_12: 9632 case WRITE_12: { 9633 struct scsi_rw_12 *cdb; 9634 9635 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 9636 9637 *lba = scsi_4btoul(cdb->addr); 9638 *len = scsi_4btoul(cdb->length); 9639 break; 9640 } 9641 case WRITE_VERIFY_12: { 9642 struct scsi_write_verify_12 *cdb; 9643 9644 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 9645 9646 *lba = scsi_4btoul(cdb->addr); 9647 *len = scsi_4btoul(cdb->length); 9648 break; 9649 } 9650 case READ_16: 9651 case WRITE_16: { 9652 struct scsi_rw_16 *cdb; 9653 9654 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 9655 9656 *lba = scsi_8btou64(cdb->addr); 9657 *len = scsi_4btoul(cdb->length); 9658 break; 9659 } 9660 case WRITE_VERIFY_16: { 9661 struct scsi_write_verify_16 *cdb; 9662 9663 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 9664 9665 9666 *lba = scsi_8btou64(cdb->addr); 9667 *len = scsi_4btoul(cdb->length); 9668 break; 9669 } 9670 default: 9671 return (1); 9672 break; /* NOTREACHED */ 9673 } 9674 9675 return (0); 9676} 9677 9678static ctl_action 9679ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 9680{ 9681 uint64_t endlba1, endlba2; 9682 9683 endlba1 = lba1 + len1 - 1; 9684 endlba2 = lba2 + len2 - 1; 9685 9686 if ((endlba1 < lba2) 9687 || (endlba2 < lba1)) 9688 return (CTL_ACTION_PASS); 9689 else 9690 return (CTL_ACTION_BLOCK); 9691} 9692 9693static ctl_action 9694ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 9695{ 9696 uint64_t lba1, lba2; 9697 uint32_t len1, len2; 9698 int retval; 9699 9700 retval = ctl_get_lba_len(io1, &lba1, &len1); 9701 if (retval != 0) 9702 return (CTL_ACTION_ERROR); 9703 9704 retval = ctl_get_lba_len(io2, &lba2, &len2); 9705 if (retval != 0) 9706 return (CTL_ACTION_ERROR); 9707 9708 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 9709} 9710 9711static ctl_action 9712ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 9713{ 9714 struct ctl_cmd_entry *pending_entry, *ooa_entry; 9715 ctl_serialize_action *serialize_row; 9716 9717 /* 9718 * The initiator attempted multiple untagged commands at the same 9719 * time. Can't do that. 9720 */ 9721 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 9722 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 9723 && ((pending_io->io_hdr.nexus.targ_port == 9724 ooa_io->io_hdr.nexus.targ_port) 9725 && (pending_io->io_hdr.nexus.initid.id == 9726 ooa_io->io_hdr.nexus.initid.id)) 9727 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 9728 return (CTL_ACTION_OVERLAP); 9729 9730 /* 9731 * The initiator attempted to send multiple tagged commands with 9732 * the same ID. (It's fine if different initiators have the same 9733 * tag ID.) 9734 * 9735 * Even if all of those conditions are true, we don't kill the I/O 9736 * if the command ahead of us has been aborted. We won't end up 9737 * sending it to the FETD, and it's perfectly legal to resend a 9738 * command with the same tag number as long as the previous 9739 * instance of this tag number has been aborted somehow. 9740 */ 9741 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 9742 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 9743 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 9744 && ((pending_io->io_hdr.nexus.targ_port == 9745 ooa_io->io_hdr.nexus.targ_port) 9746 && (pending_io->io_hdr.nexus.initid.id == 9747 ooa_io->io_hdr.nexus.initid.id)) 9748 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 9749 return (CTL_ACTION_OVERLAP_TAG); 9750 9751 /* 9752 * If we get a head of queue tag, SAM-3 says that we should 9753 * immediately execute it. 9754 * 9755 * What happens if this command would normally block for some other 9756 * reason? e.g. a request sense with a head of queue tag 9757 * immediately after a write. Normally that would block, but this 9758 * will result in its getting executed immediately... 9759 * 9760 * We currently return "pass" instead of "skip", so we'll end up 9761 * going through the rest of the queue to check for overlapped tags. 9762 * 9763 * XXX KDM check for other types of blockage first?? 9764 */ 9765 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 9766 return (CTL_ACTION_PASS); 9767 9768 /* 9769 * Ordered tags have to block until all items ahead of them 9770 * have completed. If we get called with an ordered tag, we always 9771 * block, if something else is ahead of us in the queue. 9772 */ 9773 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 9774 return (CTL_ACTION_BLOCK); 9775 9776 /* 9777 * Simple tags get blocked until all head of queue and ordered tags 9778 * ahead of them have completed. I'm lumping untagged commands in 9779 * with simple tags here. XXX KDM is that the right thing to do? 9780 */ 9781 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 9782 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 9783 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 9784 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 9785 return (CTL_ACTION_BLOCK); 9786 9787 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]]; 9788 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]]; 9789 9790 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 9791 9792 switch (serialize_row[pending_entry->seridx]) { 9793 case CTL_SER_BLOCK: 9794 return (CTL_ACTION_BLOCK); 9795 break; /* NOTREACHED */ 9796 case CTL_SER_EXTENT: 9797 return (ctl_extent_check(pending_io, ooa_io)); 9798 break; /* NOTREACHED */ 9799 case CTL_SER_PASS: 9800 return (CTL_ACTION_PASS); 9801 break; /* NOTREACHED */ 9802 case CTL_SER_SKIP: 9803 return (CTL_ACTION_SKIP); 9804 break; 9805 default: 9806 panic("invalid serialization value %d", 9807 serialize_row[pending_entry->seridx]); 9808 break; /* NOTREACHED */ 9809 } 9810 9811 return (CTL_ACTION_ERROR); 9812} 9813 9814/* 9815 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 9816 * Assumptions: 9817 * - caller holds ctl_lock 9818 * - pending_io is generally either incoming, or on the blocked queue 9819 * - starting I/O is the I/O we want to start the check with. 9820 */ 9821static ctl_action 9822ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 9823 union ctl_io *starting_io) 9824{ 9825 union ctl_io *ooa_io; 9826 ctl_action action; 9827 9828 /* 9829 * Run back along the OOA queue, starting with the current 9830 * blocked I/O and going through every I/O before it on the 9831 * queue. If starting_io is NULL, we'll just end up returning 9832 * CTL_ACTION_PASS. 9833 */ 9834 for (ooa_io = starting_io; ooa_io != NULL; 9835 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 9836 ooa_links)){ 9837 9838 /* 9839 * This routine just checks to see whether 9840 * cur_blocked is blocked by ooa_io, which is ahead 9841 * of it in the queue. It doesn't queue/dequeue 9842 * cur_blocked. 9843 */ 9844 action = ctl_check_for_blockage(pending_io, ooa_io); 9845 switch (action) { 9846 case CTL_ACTION_BLOCK: 9847 case CTL_ACTION_OVERLAP: 9848 case CTL_ACTION_OVERLAP_TAG: 9849 case CTL_ACTION_SKIP: 9850 case CTL_ACTION_ERROR: 9851 return (action); 9852 break; /* NOTREACHED */ 9853 case CTL_ACTION_PASS: 9854 break; 9855 default: 9856 panic("invalid action %d", action); 9857 break; /* NOTREACHED */ 9858 } 9859 } 9860 9861 return (CTL_ACTION_PASS); 9862} 9863 9864/* 9865 * Assumptions: 9866 * - An I/O has just completed, and has been removed from the per-LUN OOA 9867 * queue, so some items on the blocked queue may now be unblocked. 9868 * - The caller holds ctl_softc->ctl_lock 9869 */ 9870static int 9871ctl_check_blocked(struct ctl_lun *lun) 9872{ 9873 union ctl_io *cur_blocked, *next_blocked; 9874 9875 /* 9876 * Run forward from the head of the blocked queue, checking each 9877 * entry against the I/Os prior to it on the OOA queue to see if 9878 * there is still any blockage. 9879 * 9880 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 9881 * with our removing a variable on it while it is traversing the 9882 * list. 9883 */ 9884 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 9885 cur_blocked != NULL; cur_blocked = next_blocked) { 9886 union ctl_io *prev_ooa; 9887 ctl_action action; 9888 9889 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 9890 blocked_links); 9891 9892 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 9893 ctl_ooaq, ooa_links); 9894 9895 /* 9896 * If cur_blocked happens to be the first item in the OOA 9897 * queue now, prev_ooa will be NULL, and the action 9898 * returned will just be CTL_ACTION_PASS. 9899 */ 9900 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 9901 9902 switch (action) { 9903 case CTL_ACTION_BLOCK: 9904 /* Nothing to do here, still blocked */ 9905 break; 9906 case CTL_ACTION_OVERLAP: 9907 case CTL_ACTION_OVERLAP_TAG: 9908 /* 9909 * This shouldn't happen! In theory we've already 9910 * checked this command for overlap... 9911 */ 9912 break; 9913 case CTL_ACTION_PASS: 9914 case CTL_ACTION_SKIP: { 9915 struct ctl_softc *softc; 9916 struct ctl_cmd_entry *entry; 9917 uint32_t initidx; 9918 uint8_t opcode; 9919 int isc_retval; 9920 9921 /* 9922 * The skip case shouldn't happen, this transaction 9923 * should have never made it onto the blocked queue. 9924 */ 9925 /* 9926 * This I/O is no longer blocked, we can remove it 9927 * from the blocked queue. Since this is a TAILQ 9928 * (doubly linked list), we can do O(1) removals 9929 * from any place on the list. 9930 */ 9931 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 9932 blocked_links); 9933 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 9934 9935 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 9936 /* 9937 * Need to send IO back to original side to 9938 * run 9939 */ 9940 union ctl_ha_msg msg_info; 9941 9942 msg_info.hdr.original_sc = 9943 cur_blocked->io_hdr.original_sc; 9944 msg_info.hdr.serializing_sc = cur_blocked; 9945 msg_info.hdr.msg_type = CTL_MSG_R2R; 9946 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 9947 &msg_info, sizeof(msg_info), 0)) > 9948 CTL_HA_STATUS_SUCCESS) { 9949 printf("CTL:Check Blocked error from " 9950 "ctl_ha_msg_send %d\n", 9951 isc_retval); 9952 } 9953 break; 9954 } 9955 opcode = cur_blocked->scsiio.cdb[0]; 9956 entry = &ctl_cmd_table[opcode]; 9957 softc = control_softc; 9958 9959 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 9960 9961 /* 9962 * Check this I/O for LUN state changes that may 9963 * have happened while this command was blocked. 9964 * The LUN state may have been changed by a command 9965 * ahead of us in the queue, so we need to re-check 9966 * for any states that can be caused by SCSI 9967 * commands. 9968 */ 9969 if (ctl_scsiio_lun_check(softc, lun, entry, 9970 &cur_blocked->scsiio) == 0) { 9971 cur_blocked->io_hdr.flags |= 9972 CTL_FLAG_IS_WAS_ON_RTR; 9973 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue, 9974 &cur_blocked->io_hdr, links); 9975 /* 9976 * In the non CTL_DONE_THREAD case, we need 9977 * to wake up the work thread here. When 9978 * we're processing completed requests from 9979 * the work thread context, we'll pop back 9980 * around and end up pulling things off the 9981 * RtR queue. When we aren't processing 9982 * things from the work thread context, 9983 * though, we won't ever check the RtR queue. 9984 * So we need to wake up the thread to clear 9985 * things off the queue. Otherwise this 9986 * transaction will just sit on the RtR queue 9987 * until a new I/O comes in. (Which may or 9988 * may not happen...) 9989 */ 9990#ifndef CTL_DONE_THREAD 9991 ctl_wakeup_thread(); 9992#endif 9993 } else 9994 ctl_done_lock(cur_blocked, /*have_lock*/ 1); 9995 break; 9996 } 9997 default: 9998 /* 9999 * This probably shouldn't happen -- we shouldn't 10000 * get CTL_ACTION_ERROR, or anything else. 10001 */ 10002 break; 10003 } 10004 } 10005 10006 return (CTL_RETVAL_COMPLETE); 10007} 10008 10009/* 10010 * This routine (with one exception) checks LUN flags that can be set by 10011 * commands ahead of us in the OOA queue. These flags have to be checked 10012 * when a command initially comes in, and when we pull a command off the 10013 * blocked queue and are preparing to execute it. The reason we have to 10014 * check these flags for commands on the blocked queue is that the LUN 10015 * state may have been changed by a command ahead of us while we're on the 10016 * blocked queue. 10017 * 10018 * Ordering is somewhat important with these checks, so please pay 10019 * careful attention to the placement of any new checks. 10020 */ 10021static int 10022ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 10023 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10024{ 10025 int retval; 10026 10027 retval = 0; 10028 10029 /* 10030 * If this shelf is a secondary shelf controller, we have to reject 10031 * any media access commands. 10032 */ 10033#if 0 10034 /* No longer needed for HA */ 10035 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 10036 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 10037 ctl_set_lun_standby(ctsio); 10038 retval = 1; 10039 goto bailout; 10040 } 10041#endif 10042 10043 /* 10044 * Check for a reservation conflict. If this command isn't allowed 10045 * even on reserved LUNs, and if this initiator isn't the one who 10046 * reserved us, reject the command with a reservation conflict. 10047 */ 10048 if ((lun->flags & CTL_LUN_RESERVED) 10049 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10050 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 10051 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 10052 || (ctsio->io_hdr.nexus.targ_target.id != 10053 lun->rsv_nexus.targ_target.id)) { 10054 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10055 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10056 retval = 1; 10057 goto bailout; 10058 } 10059 } 10060 10061 if ( (lun->flags & CTL_LUN_PR_RESERVED) 10062 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 10063 uint32_t residx; 10064 10065 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 10066 /* 10067 * if we aren't registered or it's a res holder type 10068 * reservation and this isn't the res holder then set a 10069 * conflict. 10070 * NOTE: Commands which might be allowed on write exclusive 10071 * type reservations are checked in the particular command 10072 * for a conflict. Read and SSU are the only ones. 10073 */ 10074 if (!lun->per_res[residx].registered 10075 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10076 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10077 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10078 retval = 1; 10079 goto bailout; 10080 } 10081 10082 } 10083 10084 if ((lun->flags & CTL_LUN_OFFLINE) 10085 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 10086 ctl_set_lun_not_ready(ctsio); 10087 retval = 1; 10088 goto bailout; 10089 } 10090 10091 /* 10092 * If the LUN is stopped, see if this particular command is allowed 10093 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 10094 */ 10095 if ((lun->flags & CTL_LUN_STOPPED) 10096 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10097 /* "Logical unit not ready, initializing cmd. required" */ 10098 ctl_set_lun_stopped(ctsio); 10099 retval = 1; 10100 goto bailout; 10101 } 10102 10103 if ((lun->flags & CTL_LUN_INOPERABLE) 10104 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10105 /* "Medium format corrupted" */ 10106 ctl_set_medium_format_corrupted(ctsio); 10107 retval = 1; 10108 goto bailout; 10109 } 10110 10111bailout: 10112 return (retval); 10113 10114} 10115 10116static void 10117ctl_failover_io(union ctl_io *io, int have_lock) 10118{ 10119 ctl_set_busy(&io->scsiio); 10120 ctl_done_lock(io, have_lock); 10121} 10122 10123static void 10124ctl_failover(void) 10125{ 10126 struct ctl_lun *lun; 10127 struct ctl_softc *ctl_softc; 10128 union ctl_io *next_io, *pending_io; 10129 union ctl_io *io; 10130 int lun_idx; 10131 int i; 10132 10133 ctl_softc = control_softc; 10134 10135 mtx_lock(&ctl_softc->ctl_lock); 10136 /* 10137 * Remove any cmds from the other SC from the rtr queue. These 10138 * will obviously only be for LUNs for which we're the primary. 10139 * We can't send status or get/send data for these commands. 10140 * Since they haven't been executed yet, we can just remove them. 10141 * We'll either abort them or delete them below, depending on 10142 * which HA mode we're in. 10143 */ 10144 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 10145 io != NULL; io = next_io) { 10146 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 10147 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10148 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 10149 ctl_io_hdr, links); 10150 } 10151 10152 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 10153 lun = ctl_softc->ctl_luns[lun_idx]; 10154 if (lun==NULL) 10155 continue; 10156 10157 /* 10158 * Processor LUNs are primary on both sides. 10159 * XXX will this always be true? 10160 */ 10161 if (lun->be_lun->lun_type == T_PROCESSOR) 10162 continue; 10163 10164 if ((lun->flags & CTL_LUN_PRIMARY_SC) 10165 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10166 printf("FAILOVER: primary lun %d\n", lun_idx); 10167 /* 10168 * Remove all commands from the other SC. First from the 10169 * blocked queue then from the ooa queue. Once we have 10170 * removed them. Call ctl_check_blocked to see if there 10171 * is anything that can run. 10172 */ 10173 for (io = (union ctl_io *)TAILQ_FIRST( 10174 &lun->blocked_queue); io != NULL; io = next_io) { 10175 10176 next_io = (union ctl_io *)TAILQ_NEXT( 10177 &io->io_hdr, blocked_links); 10178 10179 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10180 TAILQ_REMOVE(&lun->blocked_queue, 10181 &io->io_hdr,blocked_links); 10182 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10183 TAILQ_REMOVE(&lun->ooa_queue, 10184 &io->io_hdr, ooa_links); 10185 10186 ctl_free_io_internal(io, 1); 10187 } 10188 } 10189 10190 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10191 io != NULL; io = next_io) { 10192 10193 next_io = (union ctl_io *)TAILQ_NEXT( 10194 &io->io_hdr, ooa_links); 10195 10196 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10197 10198 TAILQ_REMOVE(&lun->ooa_queue, 10199 &io->io_hdr, 10200 ooa_links); 10201 10202 ctl_free_io_internal(io, 1); 10203 } 10204 } 10205 ctl_check_blocked(lun); 10206 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 10207 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10208 10209 printf("FAILOVER: primary lun %d\n", lun_idx); 10210 /* 10211 * Abort all commands from the other SC. We can't 10212 * send status back for them now. These should get 10213 * cleaned up when they are completed or come out 10214 * for a datamove operation. 10215 */ 10216 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10217 io != NULL; io = next_io) { 10218 next_io = (union ctl_io *)TAILQ_NEXT( 10219 &io->io_hdr, ooa_links); 10220 10221 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10222 io->io_hdr.flags |= CTL_FLAG_ABORT; 10223 } 10224 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10225 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10226 10227 printf("FAILOVER: secondary lun %d\n", lun_idx); 10228 10229 lun->flags |= CTL_LUN_PRIMARY_SC; 10230 10231 /* 10232 * We send all I/O that was sent to this controller 10233 * and redirected to the other side back with 10234 * busy status, and have the initiator retry it. 10235 * Figuring out how much data has been transferred, 10236 * etc. and picking up where we left off would be 10237 * very tricky. 10238 * 10239 * XXX KDM need to remove I/O from the blocked 10240 * queue as well! 10241 */ 10242 for (pending_io = (union ctl_io *)TAILQ_FIRST( 10243 &lun->ooa_queue); pending_io != NULL; 10244 pending_io = next_io) { 10245 10246 next_io = (union ctl_io *)TAILQ_NEXT( 10247 &pending_io->io_hdr, ooa_links); 10248 10249 pending_io->io_hdr.flags &= 10250 ~CTL_FLAG_SENT_2OTHER_SC; 10251 10252 if (pending_io->io_hdr.flags & 10253 CTL_FLAG_IO_ACTIVE) { 10254 pending_io->io_hdr.flags |= 10255 CTL_FLAG_FAILOVER; 10256 } else { 10257 ctl_set_busy(&pending_io->scsiio); 10258 ctl_done_lock(pending_io, 10259 /*have_lock*/1); 10260 } 10261 } 10262 10263 /* 10264 * Build Unit Attention 10265 */ 10266 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10267 lun->pending_sense[i].ua_pending |= 10268 CTL_UA_ASYM_ACC_CHANGE; 10269 } 10270 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10271 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10272 printf("FAILOVER: secondary lun %d\n", lun_idx); 10273 /* 10274 * if the first io on the OOA is not on the RtR queue 10275 * add it. 10276 */ 10277 lun->flags |= CTL_LUN_PRIMARY_SC; 10278 10279 pending_io = (union ctl_io *)TAILQ_FIRST( 10280 &lun->ooa_queue); 10281 if (pending_io==NULL) { 10282 printf("Nothing on OOA queue\n"); 10283 continue; 10284 } 10285 10286 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10287 if ((pending_io->io_hdr.flags & 10288 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 10289 pending_io->io_hdr.flags |= 10290 CTL_FLAG_IS_WAS_ON_RTR; 10291 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 10292 &pending_io->io_hdr, links); 10293 } 10294#if 0 10295 else 10296 { 10297 printf("Tag 0x%04x is running\n", 10298 pending_io->scsiio.tag_num); 10299 } 10300#endif 10301 10302 next_io = (union ctl_io *)TAILQ_NEXT( 10303 &pending_io->io_hdr, ooa_links); 10304 for (pending_io=next_io; pending_io != NULL; 10305 pending_io = next_io) { 10306 pending_io->io_hdr.flags &= 10307 ~CTL_FLAG_SENT_2OTHER_SC; 10308 next_io = (union ctl_io *)TAILQ_NEXT( 10309 &pending_io->io_hdr, ooa_links); 10310 if (pending_io->io_hdr.flags & 10311 CTL_FLAG_IS_WAS_ON_RTR) { 10312#if 0 10313 printf("Tag 0x%04x is running\n", 10314 pending_io->scsiio.tag_num); 10315#endif 10316 continue; 10317 } 10318 10319 switch (ctl_check_ooa(lun, pending_io, 10320 (union ctl_io *)TAILQ_PREV( 10321 &pending_io->io_hdr, ctl_ooaq, 10322 ooa_links))) { 10323 10324 case CTL_ACTION_BLOCK: 10325 TAILQ_INSERT_TAIL(&lun->blocked_queue, 10326 &pending_io->io_hdr, 10327 blocked_links); 10328 pending_io->io_hdr.flags |= 10329 CTL_FLAG_BLOCKED; 10330 break; 10331 case CTL_ACTION_PASS: 10332 case CTL_ACTION_SKIP: 10333 pending_io->io_hdr.flags |= 10334 CTL_FLAG_IS_WAS_ON_RTR; 10335 STAILQ_INSERT_TAIL( 10336 &ctl_softc->rtr_queue, 10337 &pending_io->io_hdr, links); 10338 break; 10339 case CTL_ACTION_OVERLAP: 10340 ctl_set_overlapped_cmd( 10341 (struct ctl_scsiio *)pending_io); 10342 ctl_done_lock(pending_io, 10343 /*have_lock*/ 1); 10344 break; 10345 case CTL_ACTION_OVERLAP_TAG: 10346 ctl_set_overlapped_tag( 10347 (struct ctl_scsiio *)pending_io, 10348 pending_io->scsiio.tag_num & 0xff); 10349 ctl_done_lock(pending_io, 10350 /*have_lock*/ 1); 10351 break; 10352 case CTL_ACTION_ERROR: 10353 default: 10354 ctl_set_internal_failure( 10355 (struct ctl_scsiio *)pending_io, 10356 0, // sks_valid 10357 0); //retry count 10358 ctl_done_lock(pending_io, 10359 /*have_lock*/ 1); 10360 break; 10361 } 10362 } 10363 10364 /* 10365 * Build Unit Attention 10366 */ 10367 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10368 lun->pending_sense[i].ua_pending |= 10369 CTL_UA_ASYM_ACC_CHANGE; 10370 } 10371 } else { 10372 panic("Unhandled HA mode failover, LUN flags = %#x, " 10373 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 10374 } 10375 } 10376 ctl_pause_rtr = 0; 10377 mtx_unlock(&ctl_softc->ctl_lock); 10378} 10379 10380static int 10381ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 10382{ 10383 struct ctl_lun *lun; 10384 struct ctl_cmd_entry *entry; 10385 uint8_t opcode; 10386 uint32_t initidx; 10387 int retval; 10388 10389 retval = 0; 10390 10391 lun = NULL; 10392 10393 opcode = ctsio->cdb[0]; 10394 10395 mtx_lock(&ctl_softc->ctl_lock); 10396 10397 if ((ctsio->io_hdr.nexus.targ_lun < CTL_MAX_LUNS) 10398 && (ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun] != NULL)) { 10399 lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun]; 10400 /* 10401 * If the LUN is invalid, pretend that it doesn't exist. 10402 * It will go away as soon as all pending I/O has been 10403 * completed. 10404 */ 10405 if (lun->flags & CTL_LUN_DISABLED) { 10406 lun = NULL; 10407 } else { 10408 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 10409 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 10410 lun->be_lun; 10411 if (lun->be_lun->lun_type == T_PROCESSOR) { 10412 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 10413 } 10414 } 10415 } else { 10416 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 10417 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 10418 } 10419 10420 entry = &ctl_cmd_table[opcode]; 10421 10422 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 10423 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 10424 10425 /* 10426 * Check to see whether we can send this command to LUNs that don't 10427 * exist. This should pretty much only be the case for inquiry 10428 * and request sense. Further checks, below, really require having 10429 * a LUN, so we can't really check the command anymore. Just put 10430 * it on the rtr queue. 10431 */ 10432 if (lun == NULL) { 10433 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 10434 goto queue_rtr; 10435 10436 ctl_set_unsupported_lun(ctsio); 10437 mtx_unlock(&ctl_softc->ctl_lock); 10438 ctl_done((union ctl_io *)ctsio); 10439 goto bailout; 10440 } else { 10441 /* 10442 * Every I/O goes into the OOA queue for a particular LUN, and 10443 * stays there until completion. 10444 */ 10445 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 10446 10447 /* 10448 * Make sure we support this particular command on this LUN. 10449 * e.g., we don't support writes to the control LUN. 10450 */ 10451 switch (lun->be_lun->lun_type) { 10452 case T_PROCESSOR: 10453 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 10454 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 10455 == 0)) { 10456 ctl_set_invalid_opcode(ctsio); 10457 mtx_unlock(&ctl_softc->ctl_lock); 10458 ctl_done((union ctl_io *)ctsio); 10459 goto bailout; 10460 } 10461 break; 10462 case T_DIRECT: 10463 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) 10464 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 10465 == 0)){ 10466 ctl_set_invalid_opcode(ctsio); 10467 mtx_unlock(&ctl_softc->ctl_lock); 10468 ctl_done((union ctl_io *)ctsio); 10469 goto bailout; 10470 } 10471 break; 10472 default: 10473 printf("Unsupported CTL LUN type %d\n", 10474 lun->be_lun->lun_type); 10475 panic("Unsupported CTL LUN type %d\n", 10476 lun->be_lun->lun_type); 10477 break; /* NOTREACHED */ 10478 } 10479 } 10480 10481 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 10482 10483 /* 10484 * If we've got a request sense, it'll clear the contingent 10485 * allegiance condition. Otherwise, if we have a CA condition for 10486 * this initiator, clear it, because it sent down a command other 10487 * than request sense. 10488 */ 10489 if ((opcode != REQUEST_SENSE) 10490 && (ctl_is_set(lun->have_ca, initidx))) 10491 ctl_clear_mask(lun->have_ca, initidx); 10492 10493 /* 10494 * If the command has this flag set, it handles its own unit 10495 * attention reporting, we shouldn't do anything. Otherwise we 10496 * check for any pending unit attentions, and send them back to the 10497 * initiator. We only do this when a command initially comes in, 10498 * not when we pull it off the blocked queue. 10499 * 10500 * According to SAM-3, section 5.3.2, the order that things get 10501 * presented back to the host is basically unit attentions caused 10502 * by some sort of reset event, busy status, reservation conflicts 10503 * or task set full, and finally any other status. 10504 * 10505 * One issue here is that some of the unit attentions we report 10506 * don't fall into the "reset" category (e.g. "reported luns data 10507 * has changed"). So reporting it here, before the reservation 10508 * check, may be technically wrong. I guess the only thing to do 10509 * would be to check for and report the reset events here, and then 10510 * check for the other unit attention types after we check for a 10511 * reservation conflict. 10512 * 10513 * XXX KDM need to fix this 10514 */ 10515 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 10516 ctl_ua_type ua_type; 10517 10518 ua_type = lun->pending_sense[initidx].ua_pending; 10519 if (ua_type != CTL_UA_NONE) { 10520 scsi_sense_data_type sense_format; 10521 10522 if (lun != NULL) 10523 sense_format = (lun->flags & 10524 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 10525 SSD_TYPE_FIXED; 10526 else 10527 sense_format = SSD_TYPE_FIXED; 10528 10529 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 10530 sense_format); 10531 if (ua_type != CTL_UA_NONE) { 10532 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 10533 ctsio->io_hdr.status = CTL_SCSI_ERROR | 10534 CTL_AUTOSENSE; 10535 ctsio->sense_len = SSD_FULL_SIZE; 10536 lun->pending_sense[initidx].ua_pending &= 10537 ~ua_type; 10538 mtx_unlock(&ctl_softc->ctl_lock); 10539 ctl_done((union ctl_io *)ctsio); 10540 goto bailout; 10541 } 10542 } 10543 } 10544 10545 10546 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 10547 mtx_unlock(&ctl_softc->ctl_lock); 10548 ctl_done((union ctl_io *)ctsio); 10549 goto bailout; 10550 } 10551 10552 /* 10553 * XXX CHD this is where we want to send IO to other side if 10554 * this LUN is secondary on this SC. We will need to make a copy 10555 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 10556 * the copy we send as FROM_OTHER. 10557 * We also need to stuff the address of the original IO so we can 10558 * find it easily. Something similar will need be done on the other 10559 * side so when we are done we can find the copy. 10560 */ 10561 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 10562 union ctl_ha_msg msg_info; 10563 int isc_retval; 10564 10565 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 10566 10567 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 10568 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 10569#if 0 10570 printf("1. ctsio %p\n", ctsio); 10571#endif 10572 msg_info.hdr.serializing_sc = NULL; 10573 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 10574 msg_info.scsi.tag_num = ctsio->tag_num; 10575 msg_info.scsi.tag_type = ctsio->tag_type; 10576 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 10577 10578 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 10579 10580 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10581 (void *)&msg_info, sizeof(msg_info), 0)) > 10582 CTL_HA_STATUS_SUCCESS) { 10583 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 10584 isc_retval); 10585 printf("CTL:opcode is %x\n",opcode); 10586 } else { 10587#if 0 10588 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 10589#endif 10590 } 10591 10592 /* 10593 * XXX KDM this I/O is off the incoming queue, but hasn't 10594 * been inserted on any other queue. We may need to come 10595 * up with a holding queue while we wait for serialization 10596 * so that we have an idea of what we're waiting for from 10597 * the other side. 10598 */ 10599 goto bailout_unlock; 10600 } 10601 10602 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 10603 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 10604 ctl_ooaq, ooa_links))) { 10605 case CTL_ACTION_BLOCK: 10606 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 10607 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 10608 blocked_links); 10609 goto bailout_unlock; 10610 break; /* NOTREACHED */ 10611 case CTL_ACTION_PASS: 10612 case CTL_ACTION_SKIP: 10613 goto queue_rtr; 10614 break; /* NOTREACHED */ 10615 case CTL_ACTION_OVERLAP: 10616 ctl_set_overlapped_cmd(ctsio); 10617 mtx_unlock(&ctl_softc->ctl_lock); 10618 ctl_done((union ctl_io *)ctsio); 10619 goto bailout; 10620 break; /* NOTREACHED */ 10621 case CTL_ACTION_OVERLAP_TAG: 10622 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 10623 mtx_unlock(&ctl_softc->ctl_lock); 10624 ctl_done((union ctl_io *)ctsio); 10625 goto bailout; 10626 break; /* NOTREACHED */ 10627 case CTL_ACTION_ERROR: 10628 default: 10629 ctl_set_internal_failure(ctsio, 10630 /*sks_valid*/ 0, 10631 /*retry_count*/ 0); 10632 mtx_unlock(&ctl_softc->ctl_lock); 10633 ctl_done((union ctl_io *)ctsio); 10634 goto bailout; 10635 break; /* NOTREACHED */ 10636 } 10637 10638 goto bailout_unlock; 10639 10640queue_rtr: 10641 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 10642 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links); 10643 10644bailout_unlock: 10645 mtx_unlock(&ctl_softc->ctl_lock); 10646 10647bailout: 10648 return (retval); 10649} 10650 10651static int 10652ctl_scsiio(struct ctl_scsiio *ctsio) 10653{ 10654 int retval; 10655 struct ctl_cmd_entry *entry; 10656 10657 retval = CTL_RETVAL_COMPLETE; 10658 10659 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 10660 10661 entry = &ctl_cmd_table[ctsio->cdb[0]]; 10662 10663 /* 10664 * If this I/O has been aborted, just send it straight to 10665 * ctl_done() without executing it. 10666 */ 10667 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 10668 ctl_done((union ctl_io *)ctsio); 10669 goto bailout; 10670 } 10671 10672 /* 10673 * All the checks should have been handled by ctl_scsiio_precheck(). 10674 * We should be clear now to just execute the I/O. 10675 */ 10676 retval = entry->execute(ctsio); 10677 10678bailout: 10679 return (retval); 10680} 10681 10682/* 10683 * Since we only implement one target right now, a bus reset simply resets 10684 * our single target. 10685 */ 10686static int 10687ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 10688{ 10689 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 10690} 10691 10692static int 10693ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 10694 ctl_ua_type ua_type) 10695{ 10696 struct ctl_lun *lun; 10697 int retval; 10698 10699 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 10700 union ctl_ha_msg msg_info; 10701 10702 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 10703 msg_info.hdr.nexus = io->io_hdr.nexus; 10704 if (ua_type==CTL_UA_TARG_RESET) 10705 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 10706 else 10707 msg_info.task.task_action = CTL_TASK_BUS_RESET; 10708 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 10709 msg_info.hdr.original_sc = NULL; 10710 msg_info.hdr.serializing_sc = NULL; 10711 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10712 (void *)&msg_info, sizeof(msg_info), 0)) { 10713 } 10714 } 10715 retval = 0; 10716 10717 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 10718 retval += ctl_lun_reset(lun, io, ua_type); 10719 10720 return (retval); 10721} 10722 10723/* 10724 * The LUN should always be set. The I/O is optional, and is used to 10725 * distinguish between I/Os sent by this initiator, and by other 10726 * initiators. We set unit attention for initiators other than this one. 10727 * SAM-3 is vague on this point. It does say that a unit attention should 10728 * be established for other initiators when a LUN is reset (see section 10729 * 5.7.3), but it doesn't specifically say that the unit attention should 10730 * be established for this particular initiator when a LUN is reset. Here 10731 * is the relevant text, from SAM-3 rev 8: 10732 * 10733 * 5.7.2 When a SCSI initiator port aborts its own tasks 10734 * 10735 * When a SCSI initiator port causes its own task(s) to be aborted, no 10736 * notification that the task(s) have been aborted shall be returned to 10737 * the SCSI initiator port other than the completion response for the 10738 * command or task management function action that caused the task(s) to 10739 * be aborted and notification(s) associated with related effects of the 10740 * action (e.g., a reset unit attention condition). 10741 * 10742 * XXX KDM for now, we're setting unit attention for all initiators. 10743 */ 10744static int 10745ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 10746{ 10747 union ctl_io *xio; 10748#if 0 10749 uint32_t initindex; 10750#endif 10751 int i; 10752 10753 /* 10754 * Run through the OOA queue and abort each I/O. 10755 */ 10756#if 0 10757 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 10758#endif 10759 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 10760 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 10761 xio->io_hdr.flags |= CTL_FLAG_ABORT; 10762 } 10763 10764 /* 10765 * This version sets unit attention for every 10766 */ 10767#if 0 10768 initindex = ctl_get_initindex(&io->io_hdr.nexus); 10769 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10770 if (initindex == i) 10771 continue; 10772 lun->pending_sense[i].ua_pending |= ua_type; 10773 } 10774#endif 10775 10776 /* 10777 * A reset (any kind, really) clears reservations established with 10778 * RESERVE/RELEASE. It does not clear reservations established 10779 * with PERSISTENT RESERVE OUT, but we don't support that at the 10780 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 10781 * reservations made with the RESERVE/RELEASE commands, because 10782 * those commands are obsolete in SPC-3. 10783 */ 10784 lun->flags &= ~CTL_LUN_RESERVED; 10785 10786 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10787 ctl_clear_mask(lun->have_ca, i); 10788 lun->pending_sense[i].ua_pending |= ua_type; 10789 } 10790 10791 return (0); 10792} 10793 10794static int 10795ctl_abort_task(union ctl_io *io) 10796{ 10797 union ctl_io *xio; 10798 struct ctl_lun *lun; 10799 struct ctl_softc *ctl_softc; 10800#if 0 10801 struct sbuf sb; 10802 char printbuf[128]; 10803#endif 10804 int found; 10805 10806 ctl_softc = control_softc; 10807 found = 0; 10808 10809 /* 10810 * Look up the LUN. 10811 */ 10812 if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS) 10813 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL)) 10814 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun]; 10815 else 10816 goto bailout; 10817 10818#if 0 10819 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 10820 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 10821#endif 10822 10823 /* 10824 * Run through the OOA queue and attempt to find the given I/O. 10825 * The target port, initiator ID, tag type and tag number have to 10826 * match the values that we got from the initiator. If we have an 10827 * untagged command to abort, simply abort the first untagged command 10828 * we come to. We only allow one untagged command at a time of course. 10829 */ 10830#if 0 10831 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 10832#endif 10833 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 10834 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 10835#if 0 10836 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 10837 10838 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 10839 lun->lun, xio->scsiio.tag_num, 10840 xio->scsiio.tag_type, 10841 (xio->io_hdr.blocked_links.tqe_prev 10842 == NULL) ? "" : " BLOCKED", 10843 (xio->io_hdr.flags & 10844 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 10845 (xio->io_hdr.flags & 10846 CTL_FLAG_ABORT) ? " ABORT" : "", 10847 (xio->io_hdr.flags & 10848 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 10849 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 10850 sbuf_finish(&sb); 10851 printf("%s\n", sbuf_data(&sb)); 10852#endif 10853 10854 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 10855 && (xio->io_hdr.nexus.initid.id == 10856 io->io_hdr.nexus.initid.id)) { 10857 /* 10858 * If the abort says that the task is untagged, the 10859 * task in the queue must be untagged. Otherwise, 10860 * we just check to see whether the tag numbers 10861 * match. This is because the QLogic firmware 10862 * doesn't pass back the tag type in an abort 10863 * request. 10864 */ 10865#if 0 10866 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 10867 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 10868 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 10869#endif 10870 /* 10871 * XXX KDM we've got problems with FC, because it 10872 * doesn't send down a tag type with aborts. So we 10873 * can only really go by the tag number... 10874 * This may cause problems with parallel SCSI. 10875 * Need to figure that out!! 10876 */ 10877 if (xio->scsiio.tag_num == io->taskio.tag_num) { 10878 xio->io_hdr.flags |= CTL_FLAG_ABORT; 10879 found = 1; 10880 if ((io->io_hdr.flags & 10881 CTL_FLAG_FROM_OTHER_SC) == 0 && 10882 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 10883 union ctl_ha_msg msg_info; 10884 10885 io->io_hdr.flags |= 10886 CTL_FLAG_SENT_2OTHER_SC; 10887 msg_info.hdr.nexus = io->io_hdr.nexus; 10888 msg_info.task.task_action = 10889 CTL_TASK_ABORT_TASK; 10890 msg_info.task.tag_num = 10891 io->taskio.tag_num; 10892 msg_info.task.tag_type = 10893 io->taskio.tag_type; 10894 msg_info.hdr.msg_type = 10895 CTL_MSG_MANAGE_TASKS; 10896 msg_info.hdr.original_sc = NULL; 10897 msg_info.hdr.serializing_sc = NULL; 10898#if 0 10899 printf("Sent Abort to other side\n"); 10900#endif 10901 if (CTL_HA_STATUS_SUCCESS != 10902 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10903 (void *)&msg_info, 10904 sizeof(msg_info), 0)) { 10905 } 10906 } 10907#if 0 10908 printf("ctl_abort_task: found I/O to abort\n"); 10909#endif 10910 break; 10911 } 10912 } 10913 } 10914 10915bailout: 10916 10917 if (found == 0) { 10918 /* 10919 * This isn't really an error. It's entirely possible for 10920 * the abort and command completion to cross on the wire. 10921 * This is more of an informative/diagnostic error. 10922 */ 10923#if 0 10924 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 10925 "%d:%d:%d:%d tag %d type %d\n", 10926 io->io_hdr.nexus.initid.id, 10927 io->io_hdr.nexus.targ_port, 10928 io->io_hdr.nexus.targ_target.id, 10929 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 10930 io->taskio.tag_type); 10931#endif 10932 return (1); 10933 } else 10934 return (0); 10935} 10936 10937/* 10938 * Assumptions: caller holds ctl_softc->ctl_lock 10939 * 10940 * This routine cannot block! It must be callable from an interrupt 10941 * handler as well as from the work thread. 10942 */ 10943static void 10944ctl_run_task_queue(struct ctl_softc *ctl_softc) 10945{ 10946 union ctl_io *io, *next_io; 10947 10948 CTL_DEBUG_PRINT(("ctl_run_task_queue\n")); 10949 10950 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue); 10951 io != NULL; io = next_io) { 10952 int retval; 10953 const char *task_desc; 10954 10955 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 10956 10957 retval = 0; 10958 10959 switch (io->io_hdr.io_type) { 10960 case CTL_IO_TASK: { 10961 task_desc = ctl_scsi_task_string(&io->taskio); 10962 if (task_desc != NULL) { 10963#ifdef NEEDTOPORT 10964 csevent_log(CSC_CTL | CSC_SHELF_SW | 10965 CTL_TASK_REPORT, 10966 csevent_LogType_Trace, 10967 csevent_Severity_Information, 10968 csevent_AlertLevel_Green, 10969 csevent_FRU_Firmware, 10970 csevent_FRU_Unknown, 10971 "CTL: received task: %s",task_desc); 10972#endif 10973 } else { 10974#ifdef NEEDTOPORT 10975 csevent_log(CSC_CTL | CSC_SHELF_SW | 10976 CTL_TASK_REPORT, 10977 csevent_LogType_Trace, 10978 csevent_Severity_Information, 10979 csevent_AlertLevel_Green, 10980 csevent_FRU_Firmware, 10981 csevent_FRU_Unknown, 10982 "CTL: received unknown task " 10983 "type: %d (%#x)", 10984 io->taskio.task_action, 10985 io->taskio.task_action); 10986#endif 10987 } 10988 switch (io->taskio.task_action) { 10989 case CTL_TASK_ABORT_TASK: 10990 retval = ctl_abort_task(io); 10991 break; 10992 case CTL_TASK_ABORT_TASK_SET: 10993 break; 10994 case CTL_TASK_CLEAR_ACA: 10995 break; 10996 case CTL_TASK_CLEAR_TASK_SET: 10997 break; 10998 case CTL_TASK_LUN_RESET: { 10999 struct ctl_lun *lun; 11000 uint32_t targ_lun; 11001 int retval; 11002 11003 targ_lun = io->io_hdr.nexus.targ_lun; 11004 11005 if ((targ_lun < CTL_MAX_LUNS) 11006 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11007 lun = ctl_softc->ctl_luns[targ_lun]; 11008 else { 11009 retval = 1; 11010 break; 11011 } 11012 11013 if (!(io->io_hdr.flags & 11014 CTL_FLAG_FROM_OTHER_SC)) { 11015 union ctl_ha_msg msg_info; 11016 11017 io->io_hdr.flags |= 11018 CTL_FLAG_SENT_2OTHER_SC; 11019 msg_info.hdr.msg_type = 11020 CTL_MSG_MANAGE_TASKS; 11021 msg_info.hdr.nexus = io->io_hdr.nexus; 11022 msg_info.task.task_action = 11023 CTL_TASK_LUN_RESET; 11024 msg_info.hdr.original_sc = NULL; 11025 msg_info.hdr.serializing_sc = NULL; 11026 if (CTL_HA_STATUS_SUCCESS != 11027 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11028 (void *)&msg_info, 11029 sizeof(msg_info), 0)) { 11030 } 11031 } 11032 11033 retval = ctl_lun_reset(lun, io, 11034 CTL_UA_LUN_RESET); 11035 break; 11036 } 11037 case CTL_TASK_TARGET_RESET: 11038 retval = ctl_target_reset(ctl_softc, io, 11039 CTL_UA_TARG_RESET); 11040 break; 11041 case CTL_TASK_BUS_RESET: 11042 retval = ctl_bus_reset(ctl_softc, io); 11043 break; 11044 case CTL_TASK_PORT_LOGIN: 11045 break; 11046 case CTL_TASK_PORT_LOGOUT: 11047 break; 11048 default: 11049 printf("ctl_run_task_queue: got unknown task " 11050 "management event %d\n", 11051 io->taskio.task_action); 11052 break; 11053 } 11054 if (retval == 0) 11055 io->io_hdr.status = CTL_SUCCESS; 11056 else 11057 io->io_hdr.status = CTL_ERROR; 11058 11059 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr, 11060 ctl_io_hdr, links); 11061 /* 11062 * This will queue this I/O to the done queue, but the 11063 * work thread won't be able to process it until we 11064 * return and the lock is released. 11065 */ 11066 ctl_done_lock(io, /*have_lock*/ 1); 11067 break; 11068 } 11069 default: { 11070 11071 printf("%s: invalid I/O type %d msg %d cdb %x" 11072 " iptl: %ju:%d:%ju:%d tag 0x%04x\n", 11073 __func__, io->io_hdr.io_type, 11074 io->io_hdr.msg_type, io->scsiio.cdb[0], 11075 (uintmax_t)io->io_hdr.nexus.initid.id, 11076 io->io_hdr.nexus.targ_port, 11077 (uintmax_t)io->io_hdr.nexus.targ_target.id, 11078 io->io_hdr.nexus.targ_lun, 11079 (io->io_hdr.io_type == CTL_IO_TASK) ? 11080 io->taskio.tag_num : io->scsiio.tag_num); 11081 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr, 11082 ctl_io_hdr, links); 11083 ctl_free_io_internal(io, 1); 11084 break; 11085 } 11086 } 11087 } 11088 11089 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING; 11090} 11091 11092/* 11093 * For HA operation. Handle commands that come in from the other 11094 * controller. 11095 */ 11096static void 11097ctl_handle_isc(union ctl_io *io) 11098{ 11099 int free_io; 11100 struct ctl_lun *lun; 11101 struct ctl_softc *ctl_softc; 11102 11103 ctl_softc = control_softc; 11104 11105 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun]; 11106 11107 switch (io->io_hdr.msg_type) { 11108 case CTL_MSG_SERIALIZE: 11109 free_io = ctl_serialize_other_sc_cmd(&io->scsiio, 11110 /*have_lock*/ 0); 11111 break; 11112 case CTL_MSG_R2R: { 11113 uint8_t opcode; 11114 struct ctl_cmd_entry *entry; 11115 11116 /* 11117 * This is only used in SER_ONLY mode. 11118 */ 11119 free_io = 0; 11120 opcode = io->scsiio.cdb[0]; 11121 entry = &ctl_cmd_table[opcode]; 11122 mtx_lock(&ctl_softc->ctl_lock); 11123 if (ctl_scsiio_lun_check(ctl_softc, lun, 11124 entry, (struct ctl_scsiio *)io) != 0) { 11125 ctl_done_lock(io, /*have_lock*/ 1); 11126 mtx_unlock(&ctl_softc->ctl_lock); 11127 break; 11128 } 11129 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11130 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 11131 &io->io_hdr, links); 11132 mtx_unlock(&ctl_softc->ctl_lock); 11133 break; 11134 } 11135 case CTL_MSG_FINISH_IO: 11136 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 11137 free_io = 0; 11138 ctl_done_lock(io, /*have_lock*/ 0); 11139 } else { 11140 free_io = 1; 11141 mtx_lock(&ctl_softc->ctl_lock); 11142 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11143 ooa_links); 11144 STAILQ_REMOVE(&ctl_softc->task_queue, 11145 &io->io_hdr, ctl_io_hdr, links); 11146 ctl_check_blocked(lun); 11147 mtx_unlock(&ctl_softc->ctl_lock); 11148 } 11149 break; 11150 case CTL_MSG_PERS_ACTION: 11151 ctl_hndl_per_res_out_on_other_sc( 11152 (union ctl_ha_msg *)&io->presio.pr_msg); 11153 free_io = 1; 11154 break; 11155 case CTL_MSG_BAD_JUJU: 11156 free_io = 0; 11157 ctl_done_lock(io, /*have_lock*/ 0); 11158 break; 11159 case CTL_MSG_DATAMOVE: 11160 /* Only used in XFER mode */ 11161 free_io = 0; 11162 ctl_datamove_remote(io); 11163 break; 11164 case CTL_MSG_DATAMOVE_DONE: 11165 /* Only used in XFER mode */ 11166 free_io = 0; 11167 io->scsiio.be_move_done(io); 11168 break; 11169 default: 11170 free_io = 1; 11171 printf("%s: Invalid message type %d\n", 11172 __func__, io->io_hdr.msg_type); 11173 break; 11174 } 11175 if (free_io) 11176 ctl_free_io_internal(io, 0); 11177 11178} 11179 11180 11181/* 11182 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11183 * there is no match. 11184 */ 11185static ctl_lun_error_pattern 11186ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11187{ 11188 struct ctl_cmd_entry *entry; 11189 ctl_lun_error_pattern filtered_pattern, pattern; 11190 uint8_t opcode; 11191 11192 pattern = desc->error_pattern; 11193 11194 /* 11195 * XXX KDM we need more data passed into this function to match a 11196 * custom pattern, and we actually need to implement custom pattern 11197 * matching. 11198 */ 11199 if (pattern & CTL_LUN_PAT_CMD) 11200 return (CTL_LUN_PAT_CMD); 11201 11202 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 11203 return (CTL_LUN_PAT_ANY); 11204 11205 opcode = ctsio->cdb[0]; 11206 entry = &ctl_cmd_table[opcode]; 11207 11208 filtered_pattern = entry->pattern & pattern; 11209 11210 /* 11211 * If the user requested specific flags in the pattern (e.g. 11212 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 11213 * flags. 11214 * 11215 * If the user did not specify any flags, it doesn't matter whether 11216 * or not the command supports the flags. 11217 */ 11218 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 11219 (pattern & ~CTL_LUN_PAT_MASK)) 11220 return (CTL_LUN_PAT_NONE); 11221 11222 /* 11223 * If the user asked for a range check, see if the requested LBA 11224 * range overlaps with this command's LBA range. 11225 */ 11226 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 11227 uint64_t lba1; 11228 uint32_t len1; 11229 ctl_action action; 11230 int retval; 11231 11232 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 11233 if (retval != 0) 11234 return (CTL_LUN_PAT_NONE); 11235 11236 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 11237 desc->lba_range.len); 11238 /* 11239 * A "pass" means that the LBA ranges don't overlap, so 11240 * this doesn't match the user's range criteria. 11241 */ 11242 if (action == CTL_ACTION_PASS) 11243 return (CTL_LUN_PAT_NONE); 11244 } 11245 11246 return (filtered_pattern); 11247} 11248 11249/* 11250 * Called with the CTL lock held. 11251 */ 11252static void 11253ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 11254{ 11255 struct ctl_error_desc *desc, *desc2; 11256 11257 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 11258 ctl_lun_error_pattern pattern; 11259 /* 11260 * Check to see whether this particular command matches 11261 * the pattern in the descriptor. 11262 */ 11263 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 11264 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 11265 continue; 11266 11267 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 11268 case CTL_LUN_INJ_ABORTED: 11269 ctl_set_aborted(&io->scsiio); 11270 break; 11271 case CTL_LUN_INJ_MEDIUM_ERR: 11272 ctl_set_medium_error(&io->scsiio); 11273 break; 11274 case CTL_LUN_INJ_UA: 11275 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 11276 * OCCURRED */ 11277 ctl_set_ua(&io->scsiio, 0x29, 0x00); 11278 break; 11279 case CTL_LUN_INJ_CUSTOM: 11280 /* 11281 * We're assuming the user knows what he is doing. 11282 * Just copy the sense information without doing 11283 * checks. 11284 */ 11285 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 11286 ctl_min(sizeof(desc->custom_sense), 11287 sizeof(io->scsiio.sense_data))); 11288 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 11289 io->scsiio.sense_len = SSD_FULL_SIZE; 11290 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11291 break; 11292 case CTL_LUN_INJ_NONE: 11293 default: 11294 /* 11295 * If this is an error injection type we don't know 11296 * about, clear the continuous flag (if it is set) 11297 * so it will get deleted below. 11298 */ 11299 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 11300 break; 11301 } 11302 /* 11303 * By default, each error injection action is a one-shot 11304 */ 11305 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 11306 continue; 11307 11308 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 11309 11310 free(desc, M_CTL); 11311 } 11312} 11313 11314#ifdef CTL_IO_DELAY 11315static void 11316ctl_datamove_timer_wakeup(void *arg) 11317{ 11318 union ctl_io *io; 11319 11320 io = (union ctl_io *)arg; 11321 11322 ctl_datamove(io); 11323} 11324#endif /* CTL_IO_DELAY */ 11325 11326/* 11327 * Assumption: caller does NOT hold ctl_lock 11328 */ 11329void 11330ctl_datamove(union ctl_io *io) 11331{ 11332 void (*fe_datamove)(union ctl_io *io); 11333 11334 CTL_DEBUG_PRINT(("ctl_datamove\n")); 11335 11336#ifdef CTL_TIME_IO 11337 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 11338 char str[256]; 11339 char path_str[64]; 11340 struct sbuf sb; 11341 11342 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 11343 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 11344 11345 sbuf_cat(&sb, path_str); 11346 switch (io->io_hdr.io_type) { 11347 case CTL_IO_SCSI: 11348 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 11349 sbuf_printf(&sb, "\n"); 11350 sbuf_cat(&sb, path_str); 11351 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 11352 io->scsiio.tag_num, io->scsiio.tag_type); 11353 break; 11354 case CTL_IO_TASK: 11355 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 11356 "Tag Type: %d\n", io->taskio.task_action, 11357 io->taskio.tag_num, io->taskio.tag_type); 11358 break; 11359 default: 11360 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 11361 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 11362 break; 11363 } 11364 sbuf_cat(&sb, path_str); 11365 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 11366 (intmax_t)time_uptime - io->io_hdr.start_time); 11367 sbuf_finish(&sb); 11368 printf("%s", sbuf_data(&sb)); 11369 } 11370#endif /* CTL_TIME_IO */ 11371 11372 mtx_lock(&control_softc->ctl_lock); 11373#ifdef CTL_IO_DELAY 11374 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 11375 struct ctl_lun *lun; 11376 11377 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 11378 11379 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 11380 } else { 11381 struct ctl_lun *lun; 11382 11383 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 11384 if ((lun != NULL) 11385 && (lun->delay_info.datamove_delay > 0)) { 11386 struct callout *callout; 11387 11388 callout = (struct callout *)&io->io_hdr.timer_bytes; 11389 callout_init(callout, /*mpsafe*/ 1); 11390 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 11391 callout_reset(callout, 11392 lun->delay_info.datamove_delay * hz, 11393 ctl_datamove_timer_wakeup, io); 11394 if (lun->delay_info.datamove_type == 11395 CTL_DELAY_TYPE_ONESHOT) 11396 lun->delay_info.datamove_delay = 0; 11397 mtx_unlock(&control_softc->ctl_lock); 11398 return; 11399 } 11400 } 11401#endif 11402 /* 11403 * If we have any pending task management commands, process them 11404 * first. This is necessary to eliminate a race condition with the 11405 * FETD: 11406 * 11407 * - FETD submits a task management command, like an abort. 11408 * - Back end calls fe_datamove() to move the data for the aborted 11409 * command. The FETD can't really accept it, but if it did, it 11410 * would end up transmitting data for a command that the initiator 11411 * told us to abort. 11412 * 11413 * We close the race by processing all pending task management 11414 * commands here (we can't block!), and then check this I/O to see 11415 * if it has been aborted. If so, return it to the back end with 11416 * bad status, so the back end can say return an error to the back end 11417 * and then when the back end returns an error, we can return the 11418 * aborted command to the FETD, so it can clean up its resources. 11419 */ 11420 if (control_softc->flags & CTL_FLAG_TASK_PENDING) 11421 ctl_run_task_queue(control_softc); 11422 11423 /* 11424 * This command has been aborted. Set the port status, so we fail 11425 * the data move. 11426 */ 11427 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 11428 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 11429 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 11430 io->io_hdr.nexus.targ_port, 11431 (uintmax_t)io->io_hdr.nexus.targ_target.id, 11432 io->io_hdr.nexus.targ_lun); 11433 io->io_hdr.status = CTL_CMD_ABORTED; 11434 io->io_hdr.port_status = 31337; 11435 mtx_unlock(&control_softc->ctl_lock); 11436 /* 11437 * Note that the backend, in this case, will get the 11438 * callback in its context. In other cases it may get 11439 * called in the frontend's interrupt thread context. 11440 */ 11441 io->scsiio.be_move_done(io); 11442 return; 11443 } 11444 11445 /* 11446 * If we're in XFER mode and this I/O is from the other shelf 11447 * controller, we need to send the DMA to the other side to 11448 * actually transfer the data to/from the host. In serialize only 11449 * mode the transfer happens below CTL and ctl_datamove() is only 11450 * called on the machine that originally received the I/O. 11451 */ 11452 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 11453 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11454 union ctl_ha_msg msg; 11455 uint32_t sg_entries_sent; 11456 int do_sg_copy; 11457 int i; 11458 11459 memset(&msg, 0, sizeof(msg)); 11460 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 11461 msg.hdr.original_sc = io->io_hdr.original_sc; 11462 msg.hdr.serializing_sc = io; 11463 msg.hdr.nexus = io->io_hdr.nexus; 11464 msg.dt.flags = io->io_hdr.flags; 11465 /* 11466 * We convert everything into a S/G list here. We can't 11467 * pass by reference, only by value between controllers. 11468 * So we can't pass a pointer to the S/G list, only as many 11469 * S/G entries as we can fit in here. If it's possible for 11470 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 11471 * then we need to break this up into multiple transfers. 11472 */ 11473 if (io->scsiio.kern_sg_entries == 0) { 11474 msg.dt.kern_sg_entries = 1; 11475 /* 11476 * If this is in cached memory, flush the cache 11477 * before we send the DMA request to the other 11478 * controller. We want to do this in either the 11479 * read or the write case. The read case is 11480 * straightforward. In the write case, we want to 11481 * make sure nothing is in the local cache that 11482 * could overwrite the DMAed data. 11483 */ 11484 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 11485 /* 11486 * XXX KDM use bus_dmamap_sync() here. 11487 */ 11488 } 11489 11490 /* 11491 * Convert to a physical address if this is a 11492 * virtual address. 11493 */ 11494 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 11495 msg.dt.sg_list[0].addr = 11496 io->scsiio.kern_data_ptr; 11497 } else { 11498 /* 11499 * XXX KDM use busdma here! 11500 */ 11501#if 0 11502 msg.dt.sg_list[0].addr = (void *) 11503 vtophys(io->scsiio.kern_data_ptr); 11504#endif 11505 } 11506 11507 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 11508 do_sg_copy = 0; 11509 } else { 11510 struct ctl_sg_entry *sgl; 11511 11512 do_sg_copy = 1; 11513 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 11514 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 11515 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 11516 /* 11517 * XXX KDM use bus_dmamap_sync() here. 11518 */ 11519 } 11520 } 11521 11522 msg.dt.kern_data_len = io->scsiio.kern_data_len; 11523 msg.dt.kern_total_len = io->scsiio.kern_total_len; 11524 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 11525 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 11526 msg.dt.sg_sequence = 0; 11527 11528 /* 11529 * Loop until we've sent all of the S/G entries. On the 11530 * other end, we'll recompose these S/G entries into one 11531 * contiguous list before passing it to the 11532 */ 11533 for (sg_entries_sent = 0; sg_entries_sent < 11534 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 11535 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 11536 sizeof(msg.dt.sg_list[0])), 11537 msg.dt.kern_sg_entries - sg_entries_sent); 11538 11539 if (do_sg_copy != 0) { 11540 struct ctl_sg_entry *sgl; 11541 int j; 11542 11543 sgl = (struct ctl_sg_entry *) 11544 io->scsiio.kern_data_ptr; 11545 /* 11546 * If this is in cached memory, flush the cache 11547 * before we send the DMA request to the other 11548 * controller. We want to do this in either 11549 * the * read or the write case. The read 11550 * case is straightforward. In the write 11551 * case, we want to make sure nothing is 11552 * in the local cache that could overwrite 11553 * the DMAed data. 11554 */ 11555 11556 for (i = sg_entries_sent, j = 0; 11557 i < msg.dt.cur_sg_entries; i++, j++) { 11558 if ((io->io_hdr.flags & 11559 CTL_FLAG_NO_DATASYNC) == 0) { 11560 /* 11561 * XXX KDM use bus_dmamap_sync() 11562 */ 11563 } 11564 if ((io->io_hdr.flags & 11565 CTL_FLAG_BUS_ADDR) == 0) { 11566 /* 11567 * XXX KDM use busdma. 11568 */ 11569#if 0 11570 msg.dt.sg_list[j].addr =(void *) 11571 vtophys(sgl[i].addr); 11572#endif 11573 } else { 11574 msg.dt.sg_list[j].addr = 11575 sgl[i].addr; 11576 } 11577 msg.dt.sg_list[j].len = sgl[i].len; 11578 } 11579 } 11580 11581 sg_entries_sent += msg.dt.cur_sg_entries; 11582 if (sg_entries_sent >= msg.dt.kern_sg_entries) 11583 msg.dt.sg_last = 1; 11584 else 11585 msg.dt.sg_last = 0; 11586 11587 /* 11588 * XXX KDM drop and reacquire the lock here? 11589 */ 11590 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 11591 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 11592 /* 11593 * XXX do something here. 11594 */ 11595 } 11596 11597 msg.dt.sent_sg_entries = sg_entries_sent; 11598 } 11599 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11600 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 11601 ctl_failover_io(io, /*have_lock*/ 1); 11602 11603 } else { 11604 11605 /* 11606 * Lookup the fe_datamove() function for this particular 11607 * front end. 11608 */ 11609 fe_datamove = 11610 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 11611 mtx_unlock(&control_softc->ctl_lock); 11612 11613 fe_datamove(io); 11614 } 11615} 11616 11617static void 11618ctl_send_datamove_done(union ctl_io *io, int have_lock) 11619{ 11620 union ctl_ha_msg msg; 11621 int isc_status; 11622 11623 memset(&msg, 0, sizeof(msg)); 11624 11625 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 11626 msg.hdr.original_sc = io; 11627 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 11628 msg.hdr.nexus = io->io_hdr.nexus; 11629 msg.hdr.status = io->io_hdr.status; 11630 msg.scsi.tag_num = io->scsiio.tag_num; 11631 msg.scsi.tag_type = io->scsiio.tag_type; 11632 msg.scsi.scsi_status = io->scsiio.scsi_status; 11633 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 11634 sizeof(io->scsiio.sense_data)); 11635 msg.scsi.sense_len = io->scsiio.sense_len; 11636 msg.scsi.sense_residual = io->scsiio.sense_residual; 11637 msg.scsi.fetd_status = io->io_hdr.port_status; 11638 msg.scsi.residual = io->scsiio.residual; 11639 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11640 11641 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 11642 ctl_failover_io(io, /*have_lock*/ have_lock); 11643 return; 11644 } 11645 11646 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 11647 if (isc_status > CTL_HA_STATUS_SUCCESS) { 11648 /* XXX do something if this fails */ 11649 } 11650 11651} 11652 11653/* 11654 * The DMA to the remote side is done, now we need to tell the other side 11655 * we're done so it can continue with its data movement. 11656 */ 11657static void 11658ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 11659{ 11660 union ctl_io *io; 11661 11662 io = rq->context; 11663 11664 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 11665 printf("%s: ISC DMA write failed with error %d", __func__, 11666 rq->ret); 11667 ctl_set_internal_failure(&io->scsiio, 11668 /*sks_valid*/ 1, 11669 /*retry_count*/ rq->ret); 11670 } 11671 11672 ctl_dt_req_free(rq); 11673 11674 /* 11675 * In this case, we had to malloc the memory locally. Free it. 11676 */ 11677 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 11678 int i; 11679 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 11680 free(io->io_hdr.local_sglist[i].addr, M_CTL); 11681 } 11682 /* 11683 * The data is in local and remote memory, so now we need to send 11684 * status (good or back) back to the other side. 11685 */ 11686 ctl_send_datamove_done(io, /*have_lock*/ 0); 11687} 11688 11689/* 11690 * We've moved the data from the host/controller into local memory. Now we 11691 * need to push it over to the remote controller's memory. 11692 */ 11693static int 11694ctl_datamove_remote_dm_write_cb(union ctl_io *io) 11695{ 11696 int retval; 11697 11698 retval = 0; 11699 11700 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 11701 ctl_datamove_remote_write_cb); 11702 11703 return (retval); 11704} 11705 11706static void 11707ctl_datamove_remote_write(union ctl_io *io) 11708{ 11709 int retval; 11710 void (*fe_datamove)(union ctl_io *io); 11711 11712 /* 11713 * - Get the data from the host/HBA into local memory. 11714 * - DMA memory from the local controller to the remote controller. 11715 * - Send status back to the remote controller. 11716 */ 11717 11718 retval = ctl_datamove_remote_sgl_setup(io); 11719 if (retval != 0) 11720 return; 11721 11722 /* Switch the pointer over so the FETD knows what to do */ 11723 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 11724 11725 /* 11726 * Use a custom move done callback, since we need to send completion 11727 * back to the other controller, not to the backend on this side. 11728 */ 11729 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 11730 11731 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 11732 11733 fe_datamove(io); 11734 11735 return; 11736 11737} 11738 11739static int 11740ctl_datamove_remote_dm_read_cb(union ctl_io *io) 11741{ 11742#if 0 11743 char str[256]; 11744 char path_str[64]; 11745 struct sbuf sb; 11746#endif 11747 11748 /* 11749 * In this case, we had to malloc the memory locally. Free it. 11750 */ 11751 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 11752 int i; 11753 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 11754 free(io->io_hdr.local_sglist[i].addr, M_CTL); 11755 } 11756 11757#if 0 11758 scsi_path_string(io, path_str, sizeof(path_str)); 11759 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 11760 sbuf_cat(&sb, path_str); 11761 scsi_command_string(&io->scsiio, NULL, &sb); 11762 sbuf_printf(&sb, "\n"); 11763 sbuf_cat(&sb, path_str); 11764 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 11765 io->scsiio.tag_num, io->scsiio.tag_type); 11766 sbuf_cat(&sb, path_str); 11767 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 11768 io->io_hdr.flags, io->io_hdr.status); 11769 sbuf_finish(&sb); 11770 printk("%s", sbuf_data(&sb)); 11771#endif 11772 11773 11774 /* 11775 * The read is done, now we need to send status (good or bad) back 11776 * to the other side. 11777 */ 11778 ctl_send_datamove_done(io, /*have_lock*/ 0); 11779 11780 return (0); 11781} 11782 11783static void 11784ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 11785{ 11786 union ctl_io *io; 11787 void (*fe_datamove)(union ctl_io *io); 11788 11789 io = rq->context; 11790 11791 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 11792 printf("%s: ISC DMA read failed with error %d", __func__, 11793 rq->ret); 11794 ctl_set_internal_failure(&io->scsiio, 11795 /*sks_valid*/ 1, 11796 /*retry_count*/ rq->ret); 11797 } 11798 11799 ctl_dt_req_free(rq); 11800 11801 /* Switch the pointer over so the FETD knows what to do */ 11802 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 11803 11804 /* 11805 * Use a custom move done callback, since we need to send completion 11806 * back to the other controller, not to the backend on this side. 11807 */ 11808 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 11809 11810 /* XXX KDM add checks like the ones in ctl_datamove? */ 11811 11812 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 11813 11814 fe_datamove(io); 11815} 11816 11817static int 11818ctl_datamove_remote_sgl_setup(union ctl_io *io) 11819{ 11820 struct ctl_sg_entry *local_sglist, *remote_sglist; 11821 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 11822 struct ctl_softc *softc; 11823 int retval; 11824 int i; 11825 11826 retval = 0; 11827 softc = control_softc; 11828 11829 local_sglist = io->io_hdr.local_sglist; 11830 local_dma_sglist = io->io_hdr.local_dma_sglist; 11831 remote_sglist = io->io_hdr.remote_sglist; 11832 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 11833 11834 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 11835 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 11836 local_sglist[i].len = remote_sglist[i].len; 11837 11838 /* 11839 * XXX Detect the situation where the RS-level I/O 11840 * redirector on the other side has already read the 11841 * data off of the AOR RS on this side, and 11842 * transferred it to remote (mirror) memory on the 11843 * other side. Since we already have the data in 11844 * memory here, we just need to use it. 11845 * 11846 * XXX KDM this can probably be removed once we 11847 * get the cache device code in and take the 11848 * current AOR implementation out. 11849 */ 11850#ifdef NEEDTOPORT 11851 if ((remote_sglist[i].addr >= 11852 (void *)vtophys(softc->mirr->addr)) 11853 && (remote_sglist[i].addr < 11854 ((void *)vtophys(softc->mirr->addr) + 11855 CacheMirrorOffset))) { 11856 local_sglist[i].addr = remote_sglist[i].addr - 11857 CacheMirrorOffset; 11858 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 11859 CTL_FLAG_DATA_IN) 11860 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 11861 } else { 11862 local_sglist[i].addr = remote_sglist[i].addr + 11863 CacheMirrorOffset; 11864 } 11865#endif 11866#if 0 11867 printf("%s: local %p, remote %p, len %d\n", 11868 __func__, local_sglist[i].addr, 11869 remote_sglist[i].addr, local_sglist[i].len); 11870#endif 11871 } 11872 } else { 11873 uint32_t len_to_go; 11874 11875 /* 11876 * In this case, we don't have automatically allocated 11877 * memory for this I/O on this controller. This typically 11878 * happens with internal CTL I/O -- e.g. inquiry, mode 11879 * sense, etc. Anything coming from RAIDCore will have 11880 * a mirror area available. 11881 */ 11882 len_to_go = io->scsiio.kern_data_len; 11883 11884 /* 11885 * Clear the no datasync flag, we have to use malloced 11886 * buffers. 11887 */ 11888 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 11889 11890 /* 11891 * The difficult thing here is that the size of the various 11892 * S/G segments may be different than the size from the 11893 * remote controller. That'll make it harder when DMAing 11894 * the data back to the other side. 11895 */ 11896 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 11897 sizeof(io->io_hdr.remote_sglist[0])) && 11898 (len_to_go > 0); i++) { 11899 local_sglist[i].len = ctl_min(len_to_go, 131072); 11900 CTL_SIZE_8B(local_dma_sglist[i].len, 11901 local_sglist[i].len); 11902 local_sglist[i].addr = 11903 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 11904 11905 local_dma_sglist[i].addr = local_sglist[i].addr; 11906 11907 if (local_sglist[i].addr == NULL) { 11908 int j; 11909 11910 printf("malloc failed for %zd bytes!", 11911 local_dma_sglist[i].len); 11912 for (j = 0; j < i; j++) { 11913 free(local_sglist[j].addr, M_CTL); 11914 } 11915 ctl_set_internal_failure(&io->scsiio, 11916 /*sks_valid*/ 1, 11917 /*retry_count*/ 4857); 11918 retval = 1; 11919 goto bailout_error; 11920 11921 } 11922 /* XXX KDM do we need a sync here? */ 11923 11924 len_to_go -= local_sglist[i].len; 11925 } 11926 /* 11927 * Reset the number of S/G entries accordingly. The 11928 * original number of S/G entries is available in 11929 * rem_sg_entries. 11930 */ 11931 io->scsiio.kern_sg_entries = i; 11932 11933#if 0 11934 printf("%s: kern_sg_entries = %d\n", __func__, 11935 io->scsiio.kern_sg_entries); 11936 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 11937 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 11938 local_sglist[i].addr, local_sglist[i].len, 11939 local_dma_sglist[i].len); 11940#endif 11941 } 11942 11943 11944 return (retval); 11945 11946bailout_error: 11947 11948 ctl_send_datamove_done(io, /*have_lock*/ 0); 11949 11950 return (retval); 11951} 11952 11953static int 11954ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 11955 ctl_ha_dt_cb callback) 11956{ 11957 struct ctl_ha_dt_req *rq; 11958 struct ctl_sg_entry *remote_sglist, *local_sglist; 11959 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 11960 uint32_t local_used, remote_used, total_used; 11961 int retval; 11962 int i, j; 11963 11964 retval = 0; 11965 11966 rq = ctl_dt_req_alloc(); 11967 11968 /* 11969 * If we failed to allocate the request, and if the DMA didn't fail 11970 * anyway, set busy status. This is just a resource allocation 11971 * failure. 11972 */ 11973 if ((rq == NULL) 11974 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 11975 ctl_set_busy(&io->scsiio); 11976 11977 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 11978 11979 if (rq != NULL) 11980 ctl_dt_req_free(rq); 11981 11982 /* 11983 * The data move failed. We need to return status back 11984 * to the other controller. No point in trying to DMA 11985 * data to the remote controller. 11986 */ 11987 11988 ctl_send_datamove_done(io, /*have_lock*/ 0); 11989 11990 retval = 1; 11991 11992 goto bailout; 11993 } 11994 11995 local_sglist = io->io_hdr.local_sglist; 11996 local_dma_sglist = io->io_hdr.local_dma_sglist; 11997 remote_sglist = io->io_hdr.remote_sglist; 11998 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 11999 local_used = 0; 12000 remote_used = 0; 12001 total_used = 0; 12002 12003 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 12004 rq->ret = CTL_HA_STATUS_SUCCESS; 12005 rq->context = io; 12006 callback(rq); 12007 goto bailout; 12008 } 12009 12010 /* 12011 * Pull/push the data over the wire from/to the other controller. 12012 * This takes into account the possibility that the local and 12013 * remote sglists may not be identical in terms of the size of 12014 * the elements and the number of elements. 12015 * 12016 * One fundamental assumption here is that the length allocated for 12017 * both the local and remote sglists is identical. Otherwise, we've 12018 * essentially got a coding error of some sort. 12019 */ 12020 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12021 int isc_ret; 12022 uint32_t cur_len, dma_length; 12023 uint8_t *tmp_ptr; 12024 12025 rq->id = CTL_HA_DATA_CTL; 12026 rq->command = command; 12027 rq->context = io; 12028 12029 /* 12030 * Both pointers should be aligned. But it is possible 12031 * that the allocation length is not. They should both 12032 * also have enough slack left over at the end, though, 12033 * to round up to the next 8 byte boundary. 12034 */ 12035 cur_len = ctl_min(local_sglist[i].len - local_used, 12036 remote_sglist[j].len - remote_used); 12037 12038 /* 12039 * In this case, we have a size issue and need to decrease 12040 * the size, except in the case where we actually have less 12041 * than 8 bytes left. In that case, we need to increase 12042 * the DMA length to get the last bit. 12043 */ 12044 if ((cur_len & 0x7) != 0) { 12045 if (cur_len > 0x7) { 12046 cur_len = cur_len - (cur_len & 0x7); 12047 dma_length = cur_len; 12048 } else { 12049 CTL_SIZE_8B(dma_length, cur_len); 12050 } 12051 12052 } else 12053 dma_length = cur_len; 12054 12055 /* 12056 * If we had to allocate memory for this I/O, instead of using 12057 * the non-cached mirror memory, we'll need to flush the cache 12058 * before trying to DMA to the other controller. 12059 * 12060 * We could end up doing this multiple times for the same 12061 * segment if we have a larger local segment than remote 12062 * segment. That shouldn't be an issue. 12063 */ 12064 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12065 /* 12066 * XXX KDM use bus_dmamap_sync() here. 12067 */ 12068 } 12069 12070 rq->size = dma_length; 12071 12072 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12073 tmp_ptr += local_used; 12074 12075 /* Use physical addresses when talking to ISC hardware */ 12076 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12077 /* XXX KDM use busdma */ 12078#if 0 12079 rq->local = vtophys(tmp_ptr); 12080#endif 12081 } else 12082 rq->local = tmp_ptr; 12083 12084 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12085 tmp_ptr += remote_used; 12086 rq->remote = tmp_ptr; 12087 12088 rq->callback = NULL; 12089 12090 local_used += cur_len; 12091 if (local_used >= local_sglist[i].len) { 12092 i++; 12093 local_used = 0; 12094 } 12095 12096 remote_used += cur_len; 12097 if (remote_used >= remote_sglist[j].len) { 12098 j++; 12099 remote_used = 0; 12100 } 12101 total_used += cur_len; 12102 12103 if (total_used >= io->scsiio.kern_data_len) 12104 rq->callback = callback; 12105 12106 if ((rq->size & 0x7) != 0) { 12107 printf("%s: warning: size %d is not on 8b boundary\n", 12108 __func__, rq->size); 12109 } 12110 if (((uintptr_t)rq->local & 0x7) != 0) { 12111 printf("%s: warning: local %p not on 8b boundary\n", 12112 __func__, rq->local); 12113 } 12114 if (((uintptr_t)rq->remote & 0x7) != 0) { 12115 printf("%s: warning: remote %p not on 8b boundary\n", 12116 __func__, rq->local); 12117 } 12118#if 0 12119 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12120 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12121 rq->local, rq->remote, rq->size); 12122#endif 12123 12124 isc_ret = ctl_dt_single(rq); 12125 if (isc_ret == CTL_HA_STATUS_WAIT) 12126 continue; 12127 12128 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 12129 rq->ret = CTL_HA_STATUS_SUCCESS; 12130 } else { 12131 rq->ret = isc_ret; 12132 } 12133 callback(rq); 12134 goto bailout; 12135 } 12136 12137bailout: 12138 return (retval); 12139 12140} 12141 12142static void 12143ctl_datamove_remote_read(union ctl_io *io) 12144{ 12145 int retval; 12146 int i; 12147 12148 /* 12149 * This will send an error to the other controller in the case of a 12150 * failure. 12151 */ 12152 retval = ctl_datamove_remote_sgl_setup(io); 12153 if (retval != 0) 12154 return; 12155 12156 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12157 ctl_datamove_remote_read_cb); 12158 if ((retval != 0) 12159 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 12160 /* 12161 * Make sure we free memory if there was an error.. The 12162 * ctl_datamove_remote_xfer() function will send the 12163 * datamove done message, or call the callback with an 12164 * error if there is a problem. 12165 */ 12166 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12167 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12168 } 12169 12170 return; 12171} 12172 12173/* 12174 * Process a datamove request from the other controller. This is used for 12175 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12176 * first. Once that is complete, the data gets DMAed into the remote 12177 * controller's memory. For reads, we DMA from the remote controller's 12178 * memory into our memory first, and then move it out to the FETD. 12179 * 12180 * Should be called without the ctl_lock held. 12181 */ 12182static void 12183ctl_datamove_remote(union ctl_io *io) 12184{ 12185 struct ctl_softc *softc; 12186 12187 softc = control_softc; 12188 12189 /* 12190 * Note that we look for an aborted I/O here, but don't do some of 12191 * the other checks that ctl_datamove() normally does. We don't 12192 * need to run the task queue, because this I/O is on the ISC 12193 * queue, which is executed by the work thread after the task queue. 12194 * We don't need to run the datamove delay code, since that should 12195 * have been done if need be on the other controller. 12196 */ 12197 mtx_lock(&softc->ctl_lock); 12198 12199 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12200 12201 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 12202 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 12203 io->io_hdr.nexus.targ_port, 12204 io->io_hdr.nexus.targ_target.id, 12205 io->io_hdr.nexus.targ_lun); 12206 io->io_hdr.status = CTL_CMD_ABORTED; 12207 io->io_hdr.port_status = 31338; 12208 12209 mtx_unlock(&softc->ctl_lock); 12210 12211 ctl_send_datamove_done(io, /*have_lock*/ 0); 12212 12213 return; 12214 } 12215 12216 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 12217 mtx_unlock(&softc->ctl_lock); 12218 ctl_datamove_remote_write(io); 12219 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 12220 mtx_unlock(&softc->ctl_lock); 12221 ctl_datamove_remote_read(io); 12222 } else { 12223 union ctl_ha_msg msg; 12224 struct scsi_sense_data *sense; 12225 uint8_t sks[3]; 12226 int retry_count; 12227 12228 memset(&msg, 0, sizeof(msg)); 12229 12230 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 12231 msg.hdr.status = CTL_SCSI_ERROR; 12232 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 12233 12234 retry_count = 4243; 12235 12236 sense = &msg.scsi.sense_data; 12237 sks[0] = SSD_SCS_VALID; 12238 sks[1] = (retry_count >> 8) & 0xff; 12239 sks[2] = retry_count & 0xff; 12240 12241 /* "Internal target failure" */ 12242 scsi_set_sense_data(sense, 12243 /*sense_format*/ SSD_TYPE_NONE, 12244 /*current_error*/ 1, 12245 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 12246 /*asc*/ 0x44, 12247 /*ascq*/ 0x00, 12248 /*type*/ SSD_ELEM_SKS, 12249 /*size*/ sizeof(sks), 12250 /*data*/ sks, 12251 SSD_ELEM_NONE); 12252 12253 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12254 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12255 ctl_failover_io(io, /*have_lock*/ 1); 12256 mtx_unlock(&softc->ctl_lock); 12257 return; 12258 } 12259 12260 mtx_unlock(&softc->ctl_lock); 12261 12262 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 12263 CTL_HA_STATUS_SUCCESS) { 12264 /* XXX KDM what to do if this fails? */ 12265 } 12266 return; 12267 } 12268 12269} 12270 12271static int 12272ctl_process_done(union ctl_io *io, int have_lock) 12273{ 12274 struct ctl_lun *lun; 12275 struct ctl_softc *ctl_softc; 12276 void (*fe_done)(union ctl_io *io); 12277 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 12278 12279 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12280 12281 fe_done = 12282 control_softc->ctl_ports[targ_port]->fe_done; 12283 12284#ifdef CTL_TIME_IO 12285 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12286 char str[256]; 12287 char path_str[64]; 12288 struct sbuf sb; 12289 12290 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12291 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12292 12293 sbuf_cat(&sb, path_str); 12294 switch (io->io_hdr.io_type) { 12295 case CTL_IO_SCSI: 12296 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12297 sbuf_printf(&sb, "\n"); 12298 sbuf_cat(&sb, path_str); 12299 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12300 io->scsiio.tag_num, io->scsiio.tag_type); 12301 break; 12302 case CTL_IO_TASK: 12303 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12304 "Tag Type: %d\n", io->taskio.task_action, 12305 io->taskio.tag_num, io->taskio.tag_type); 12306 break; 12307 default: 12308 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12309 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12310 break; 12311 } 12312 sbuf_cat(&sb, path_str); 12313 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12314 (intmax_t)time_uptime - io->io_hdr.start_time); 12315 sbuf_finish(&sb); 12316 printf("%s", sbuf_data(&sb)); 12317 } 12318#endif /* CTL_TIME_IO */ 12319 12320 switch (io->io_hdr.io_type) { 12321 case CTL_IO_SCSI: 12322 break; 12323 case CTL_IO_TASK: 12324 ctl_io_error_print(io, NULL); 12325 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 12326 ctl_free_io_internal(io, /*have_lock*/ 0); 12327 else 12328 fe_done(io); 12329 return (CTL_RETVAL_COMPLETE); 12330 break; 12331 default: 12332 printf("ctl_process_done: invalid io type %d\n", 12333 io->io_hdr.io_type); 12334 panic("ctl_process_done: invalid io type %d\n", 12335 io->io_hdr.io_type); 12336 break; /* NOTREACHED */ 12337 } 12338 12339 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12340 if (lun == NULL) { 12341 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12342 io->io_hdr.nexus.targ_lun)); 12343 fe_done(io); 12344 goto bailout; 12345 } 12346 ctl_softc = lun->ctl_softc; 12347 12348 /* 12349 * Remove this from the OOA queue. 12350 */ 12351 if (have_lock == 0) 12352 mtx_lock(&ctl_softc->ctl_lock); 12353 12354 /* 12355 * Check to see if we have any errors to inject here. We only 12356 * inject errors for commands that don't already have errors set. 12357 */ 12358 if ((STAILQ_FIRST(&lun->error_list) != NULL) 12359 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 12360 ctl_inject_error(lun, io); 12361 12362 /* 12363 * XXX KDM how do we treat commands that aren't completed 12364 * successfully? 12365 * 12366 * XXX KDM should we also track I/O latency? 12367 */ 12368 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 12369 uint32_t blocksize; 12370#ifdef CTL_TIME_IO 12371 struct bintime cur_bt; 12372#endif 12373 12374 if ((lun->be_lun != NULL) 12375 && (lun->be_lun->blocksize != 0)) 12376 blocksize = lun->be_lun->blocksize; 12377 else 12378 blocksize = 512; 12379 12380 switch (io->io_hdr.io_type) { 12381 case CTL_IO_SCSI: { 12382 int isread; 12383 struct ctl_lba_len lbalen; 12384 12385 isread = 0; 12386 switch (io->scsiio.cdb[0]) { 12387 case READ_6: 12388 case READ_10: 12389 case READ_12: 12390 case READ_16: 12391 isread = 1; 12392 /* FALLTHROUGH */ 12393 case WRITE_6: 12394 case WRITE_10: 12395 case WRITE_12: 12396 case WRITE_16: 12397 case WRITE_VERIFY_10: 12398 case WRITE_VERIFY_12: 12399 case WRITE_VERIFY_16: 12400 memcpy(&lbalen, io->io_hdr.ctl_private[ 12401 CTL_PRIV_LBA_LEN].bytes, sizeof(lbalen)); 12402 12403 if (isread) { 12404 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] += 12405 lbalen.len * blocksize; 12406 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++; 12407 12408#ifdef CTL_TIME_IO 12409 bintime_add( 12410 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ], 12411 &io->io_hdr.dma_bt); 12412 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] += 12413 io->io_hdr.num_dmas; 12414 getbintime(&cur_bt); 12415 bintime_sub(&cur_bt, 12416 &io->io_hdr.start_bt); 12417 12418 bintime_add( 12419 &lun->stats.ports[targ_port].time[CTL_STATS_READ], 12420 &cur_bt); 12421 12422#if 0 12423 cs_prof_gettime(&cur_ticks); 12424 lun->stats.time[CTL_STATS_READ] += 12425 cur_ticks - 12426 io->io_hdr.start_ticks; 12427#endif 12428#if 0 12429 lun->stats.time[CTL_STATS_READ] += 12430 jiffies - io->io_hdr.start_time; 12431#endif 12432#endif /* CTL_TIME_IO */ 12433 } else { 12434 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] += 12435 lbalen.len * blocksize; 12436 lun->stats.ports[targ_port].operations[ 12437 CTL_STATS_WRITE]++; 12438 12439#ifdef CTL_TIME_IO 12440 bintime_add( 12441 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE], 12442 &io->io_hdr.dma_bt); 12443 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] += 12444 io->io_hdr.num_dmas; 12445 getbintime(&cur_bt); 12446 bintime_sub(&cur_bt, 12447 &io->io_hdr.start_bt); 12448 12449 bintime_add( 12450 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE], 12451 &cur_bt); 12452#if 0 12453 cs_prof_gettime(&cur_ticks); 12454 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] += 12455 cur_ticks - 12456 io->io_hdr.start_ticks; 12457 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] += 12458 jiffies - io->io_hdr.start_time; 12459#endif 12460#endif /* CTL_TIME_IO */ 12461 } 12462 break; 12463 default: 12464 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++; 12465 12466#ifdef CTL_TIME_IO 12467 bintime_add( 12468 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO], 12469 &io->io_hdr.dma_bt); 12470 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] += 12471 io->io_hdr.num_dmas; 12472 getbintime(&cur_bt); 12473 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 12474 12475 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO], 12476 &cur_bt); 12477 12478#if 0 12479 cs_prof_gettime(&cur_ticks); 12480 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] += 12481 cur_ticks - 12482 io->io_hdr.start_ticks; 12483 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] += 12484 jiffies - io->io_hdr.start_time; 12485#endif 12486#endif /* CTL_TIME_IO */ 12487 break; 12488 } 12489 break; 12490 } 12491 default: 12492 break; 12493 } 12494 } 12495 12496 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12497 12498 /* 12499 * Run through the blocked queue on this LUN and see if anything 12500 * has become unblocked, now that this transaction is done. 12501 */ 12502 ctl_check_blocked(lun); 12503 12504 /* 12505 * If the LUN has been invalidated, free it if there is nothing 12506 * left on its OOA queue. 12507 */ 12508 if ((lun->flags & CTL_LUN_INVALID) 12509 && (TAILQ_FIRST(&lun->ooa_queue) == NULL)) 12510 ctl_free_lun(lun); 12511 12512 /* 12513 * If this command has been aborted, make sure we set the status 12514 * properly. The FETD is responsible for freeing the I/O and doing 12515 * whatever it needs to do to clean up its state. 12516 */ 12517 if (io->io_hdr.flags & CTL_FLAG_ABORT) 12518 io->io_hdr.status = CTL_CMD_ABORTED; 12519 12520 /* 12521 * We print out status for every task management command. For SCSI 12522 * commands, we filter out any unit attention errors; they happen 12523 * on every boot, and would clutter up the log. Note: task 12524 * management commands aren't printed here, they are printed above, 12525 * since they should never even make it down here. 12526 */ 12527 switch (io->io_hdr.io_type) { 12528 case CTL_IO_SCSI: { 12529 int error_code, sense_key, asc, ascq; 12530 12531 sense_key = 0; 12532 12533 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 12534 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 12535 /* 12536 * Since this is just for printing, no need to 12537 * show errors here. 12538 */ 12539 scsi_extract_sense_len(&io->scsiio.sense_data, 12540 io->scsiio.sense_len, 12541 &error_code, 12542 &sense_key, 12543 &asc, 12544 &ascq, 12545 /*show_errors*/ 0); 12546 } 12547 12548 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 12549 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 12550 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 12551 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 12552 12553 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 12554 ctl_softc->skipped_prints++; 12555 if (have_lock == 0) 12556 mtx_unlock(&ctl_softc->ctl_lock); 12557 } else { 12558 uint32_t skipped_prints; 12559 12560 skipped_prints = ctl_softc->skipped_prints; 12561 12562 ctl_softc->skipped_prints = 0; 12563 ctl_softc->last_print_jiffies = time_uptime; 12564 12565 if (have_lock == 0) 12566 mtx_unlock(&ctl_softc->ctl_lock); 12567 if (skipped_prints > 0) { 12568#ifdef NEEDTOPORT 12569 csevent_log(CSC_CTL | CSC_SHELF_SW | 12570 CTL_ERROR_REPORT, 12571 csevent_LogType_Trace, 12572 csevent_Severity_Information, 12573 csevent_AlertLevel_Green, 12574 csevent_FRU_Firmware, 12575 csevent_FRU_Unknown, 12576 "High CTL error volume, %d prints " 12577 "skipped", skipped_prints); 12578#endif 12579 } 12580 ctl_io_error_print(io, NULL); 12581 } 12582 } else { 12583 if (have_lock == 0) 12584 mtx_unlock(&ctl_softc->ctl_lock); 12585 } 12586 break; 12587 } 12588 case CTL_IO_TASK: 12589 if (have_lock == 0) 12590 mtx_unlock(&ctl_softc->ctl_lock); 12591 ctl_io_error_print(io, NULL); 12592 break; 12593 default: 12594 if (have_lock == 0) 12595 mtx_unlock(&ctl_softc->ctl_lock); 12596 break; 12597 } 12598 12599 /* 12600 * Tell the FETD or the other shelf controller we're done with this 12601 * command. Note that only SCSI commands get to this point. Task 12602 * management commands are completed above. 12603 * 12604 * We only send status to the other controller if we're in XFER 12605 * mode. In SER_ONLY mode, the I/O is done on the controller that 12606 * received the I/O (from CTL's perspective), and so the status is 12607 * generated there. 12608 * 12609 * XXX KDM if we hold the lock here, we could cause a deadlock 12610 * if the frontend comes back in in this context to queue 12611 * something. 12612 */ 12613 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 12614 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12615 union ctl_ha_msg msg; 12616 12617 memset(&msg, 0, sizeof(msg)); 12618 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 12619 msg.hdr.original_sc = io->io_hdr.original_sc; 12620 msg.hdr.nexus = io->io_hdr.nexus; 12621 msg.hdr.status = io->io_hdr.status; 12622 msg.scsi.scsi_status = io->scsiio.scsi_status; 12623 msg.scsi.tag_num = io->scsiio.tag_num; 12624 msg.scsi.tag_type = io->scsiio.tag_type; 12625 msg.scsi.sense_len = io->scsiio.sense_len; 12626 msg.scsi.sense_residual = io->scsiio.sense_residual; 12627 msg.scsi.residual = io->scsiio.residual; 12628 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12629 sizeof(io->scsiio.sense_data)); 12630 /* 12631 * We copy this whether or not this is an I/O-related 12632 * command. Otherwise, we'd have to go and check to see 12633 * whether it's a read/write command, and it really isn't 12634 * worth it. 12635 */ 12636 memcpy(&msg.scsi.lbalen, 12637 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 12638 sizeof(msg.scsi.lbalen)); 12639 12640 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12641 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12642 /* XXX do something here */ 12643 } 12644 12645 ctl_free_io_internal(io, /*have_lock*/ 0); 12646 } else 12647 fe_done(io); 12648 12649bailout: 12650 12651 return (CTL_RETVAL_COMPLETE); 12652} 12653 12654/* 12655 * Front end should call this if it doesn't do autosense. When the request 12656 * sense comes back in from the initiator, we'll dequeue this and send it. 12657 */ 12658int 12659ctl_queue_sense(union ctl_io *io) 12660{ 12661 struct ctl_lun *lun; 12662 struct ctl_softc *ctl_softc; 12663 uint32_t initidx; 12664 12665 ctl_softc = control_softc; 12666 12667 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 12668 12669 /* 12670 * LUN lookup will likely move to the ctl_work_thread() once we 12671 * have our new queueing infrastructure (that doesn't put things on 12672 * a per-LUN queue initially). That is so that we can handle 12673 * things like an INQUIRY to a LUN that we don't have enabled. We 12674 * can't deal with that right now. 12675 */ 12676 mtx_lock(&ctl_softc->ctl_lock); 12677 12678 /* 12679 * If we don't have a LUN for this, just toss the sense 12680 * information. 12681 */ 12682 if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS) 12683 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL)) 12684 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun]; 12685 else 12686 goto bailout; 12687 12688 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12689 12690 /* 12691 * Already have CA set for this LUN...toss the sense information. 12692 */ 12693 if (ctl_is_set(lun->have_ca, initidx)) 12694 goto bailout; 12695 12696 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data, 12697 ctl_min(sizeof(lun->pending_sense[initidx].sense), 12698 sizeof(io->scsiio.sense_data))); 12699 ctl_set_mask(lun->have_ca, initidx); 12700 12701bailout: 12702 mtx_unlock(&ctl_softc->ctl_lock); 12703 12704 ctl_free_io(io); 12705 12706 return (CTL_RETVAL_COMPLETE); 12707} 12708 12709/* 12710 * Primary command inlet from frontend ports. All SCSI and task I/O 12711 * requests must go through this function. 12712 */ 12713int 12714ctl_queue(union ctl_io *io) 12715{ 12716 struct ctl_softc *ctl_softc; 12717 12718 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 12719 12720 ctl_softc = control_softc; 12721 12722#ifdef CTL_TIME_IO 12723 io->io_hdr.start_time = time_uptime; 12724 getbintime(&io->io_hdr.start_bt); 12725#endif /* CTL_TIME_IO */ 12726 12727 mtx_lock(&ctl_softc->ctl_lock); 12728 12729 switch (io->io_hdr.io_type) { 12730 case CTL_IO_SCSI: 12731 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr, 12732 links); 12733 break; 12734 case CTL_IO_TASK: 12735 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links); 12736 /* 12737 * Set the task pending flag. This is necessary to close a 12738 * race condition with the FETD: 12739 * 12740 * - FETD submits a task management command, like an abort. 12741 * - Back end calls fe_datamove() to move the data for the 12742 * aborted command. The FETD can't really accept it, but 12743 * if it did, it would end up transmitting data for a 12744 * command that the initiator told us to abort. 12745 * 12746 * We close the race condition by setting the flag here, 12747 * and checking it in ctl_datamove(), before calling the 12748 * FETD's fe_datamove routine. If we've got a task 12749 * pending, we run the task queue and then check to see 12750 * whether our particular I/O has been aborted. 12751 */ 12752 ctl_softc->flags |= CTL_FLAG_TASK_PENDING; 12753 break; 12754 default: 12755 mtx_unlock(&ctl_softc->ctl_lock); 12756 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 12757 return (-EINVAL); 12758 break; /* NOTREACHED */ 12759 } 12760 mtx_unlock(&ctl_softc->ctl_lock); 12761 12762 ctl_wakeup_thread(); 12763 12764 return (CTL_RETVAL_COMPLETE); 12765} 12766 12767#ifdef CTL_IO_DELAY 12768static void 12769ctl_done_timer_wakeup(void *arg) 12770{ 12771 union ctl_io *io; 12772 12773 io = (union ctl_io *)arg; 12774 ctl_done_lock(io, /*have_lock*/ 0); 12775} 12776#endif /* CTL_IO_DELAY */ 12777 12778void 12779ctl_done_lock(union ctl_io *io, int have_lock) 12780{ 12781 struct ctl_softc *ctl_softc; 12782#ifndef CTL_DONE_THREAD 12783 union ctl_io *xio; 12784#endif /* !CTL_DONE_THREAD */ 12785 12786 ctl_softc = control_softc; 12787 12788 if (have_lock == 0) 12789 mtx_lock(&ctl_softc->ctl_lock); 12790 12791 /* 12792 * Enable this to catch duplicate completion issues. 12793 */ 12794#if 0 12795 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 12796 printf("%s: type %d msg %d cdb %x iptl: " 12797 "%d:%d:%d:%d tag 0x%04x " 12798 "flag %#x status %x\n", 12799 __func__, 12800 io->io_hdr.io_type, 12801 io->io_hdr.msg_type, 12802 io->scsiio.cdb[0], 12803 io->io_hdr.nexus.initid.id, 12804 io->io_hdr.nexus.targ_port, 12805 io->io_hdr.nexus.targ_target.id, 12806 io->io_hdr.nexus.targ_lun, 12807 (io->io_hdr.io_type == 12808 CTL_IO_TASK) ? 12809 io->taskio.tag_num : 12810 io->scsiio.tag_num, 12811 io->io_hdr.flags, 12812 io->io_hdr.status); 12813 } else 12814 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 12815#endif 12816 12817 /* 12818 * This is an internal copy of an I/O, and should not go through 12819 * the normal done processing logic. 12820 */ 12821 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) { 12822 if (have_lock == 0) 12823 mtx_unlock(&ctl_softc->ctl_lock); 12824 return; 12825 } 12826 12827 /* 12828 * We need to send a msg to the serializing shelf to finish the IO 12829 * as well. We don't send a finish message to the other shelf if 12830 * this is a task management command. Task management commands 12831 * aren't serialized in the OOA queue, but rather just executed on 12832 * both shelf controllers for commands that originated on that 12833 * controller. 12834 */ 12835 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 12836 && (io->io_hdr.io_type != CTL_IO_TASK)) { 12837 union ctl_ha_msg msg_io; 12838 12839 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 12840 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 12841 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 12842 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 12843 } 12844 /* continue on to finish IO */ 12845 } 12846#ifdef CTL_IO_DELAY 12847 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12848 struct ctl_lun *lun; 12849 12850 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12851 12852 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12853 } else { 12854 struct ctl_lun *lun; 12855 12856 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12857 12858 if ((lun != NULL) 12859 && (lun->delay_info.done_delay > 0)) { 12860 struct callout *callout; 12861 12862 callout = (struct callout *)&io->io_hdr.timer_bytes; 12863 callout_init(callout, /*mpsafe*/ 1); 12864 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12865 callout_reset(callout, 12866 lun->delay_info.done_delay * hz, 12867 ctl_done_timer_wakeup, io); 12868 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 12869 lun->delay_info.done_delay = 0; 12870 if (have_lock == 0) 12871 mtx_unlock(&ctl_softc->ctl_lock); 12872 return; 12873 } 12874 } 12875#endif /* CTL_IO_DELAY */ 12876 12877 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links); 12878 12879#ifdef CTL_DONE_THREAD 12880 if (have_lock == 0) 12881 mtx_unlock(&ctl_softc->ctl_lock); 12882 12883 ctl_wakeup_thread(); 12884#else /* CTL_DONE_THREAD */ 12885 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue); 12886 xio != NULL; 12887 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) { 12888 12889 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links); 12890 12891 ctl_process_done(xio, /*have_lock*/ 1); 12892 } 12893 if (have_lock == 0) 12894 mtx_unlock(&ctl_softc->ctl_lock); 12895#endif /* CTL_DONE_THREAD */ 12896} 12897 12898void 12899ctl_done(union ctl_io *io) 12900{ 12901 ctl_done_lock(io, /*have_lock*/ 0); 12902} 12903 12904int 12905ctl_isc(struct ctl_scsiio *ctsio) 12906{ 12907 struct ctl_lun *lun; 12908 int retval; 12909 12910 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12911 12912 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 12913 12914 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 12915 12916 retval = lun->backend->data_submit((union ctl_io *)ctsio); 12917 12918 return (retval); 12919} 12920 12921 12922static void 12923ctl_work_thread(void *arg) 12924{ 12925 struct ctl_softc *softc; 12926 union ctl_io *io; 12927 struct ctl_be_lun *be_lun; 12928 int retval; 12929 12930 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 12931 12932 softc = (struct ctl_softc *)arg; 12933 if (softc == NULL) 12934 return; 12935 12936 mtx_lock(&softc->ctl_lock); 12937 for (;;) { 12938 retval = 0; 12939 12940 /* 12941 * We handle the queues in this order: 12942 * - task management 12943 * - ISC 12944 * - done queue (to free up resources, unblock other commands) 12945 * - RtR queue 12946 * - incoming queue 12947 * 12948 * If those queues are empty, we break out of the loop and 12949 * go to sleep. 12950 */ 12951 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue); 12952 if (io != NULL) { 12953 ctl_run_task_queue(softc); 12954 continue; 12955 } 12956 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue); 12957 if (io != NULL) { 12958 STAILQ_REMOVE_HEAD(&softc->isc_queue, links); 12959 ctl_handle_isc(io); 12960 continue; 12961 } 12962 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue); 12963 if (io != NULL) { 12964 STAILQ_REMOVE_HEAD(&softc->done_queue, links); 12965 /* clear any blocked commands, call fe_done */ 12966 mtx_unlock(&softc->ctl_lock); 12967 /* 12968 * XXX KDM 12969 * Call this without a lock for now. This will 12970 * depend on whether there is any way the FETD can 12971 * sleep or deadlock if called with the CTL lock 12972 * held. 12973 */ 12974 retval = ctl_process_done(io, /*have_lock*/ 0); 12975 mtx_lock(&softc->ctl_lock); 12976 continue; 12977 } 12978 if (!ctl_pause_rtr) { 12979 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 12980 if (io != NULL) { 12981 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links); 12982 mtx_unlock(&softc->ctl_lock); 12983 goto execute; 12984 } 12985 } 12986 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue); 12987 if (io != NULL) { 12988 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links); 12989 mtx_unlock(&softc->ctl_lock); 12990 ctl_scsiio_precheck(softc, &io->scsiio); 12991 mtx_lock(&softc->ctl_lock); 12992 continue; 12993 } 12994 /* 12995 * We might want to move this to a separate thread, so that 12996 * configuration requests (in this case LUN creations) 12997 * won't impact the I/O path. 12998 */ 12999 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13000 if (be_lun != NULL) { 13001 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13002 mtx_unlock(&softc->ctl_lock); 13003 ctl_create_lun(be_lun); 13004 mtx_lock(&softc->ctl_lock); 13005 continue; 13006 } 13007 13008 /* XXX KDM use the PDROP flag?? */ 13009 /* Sleep until we have something to do. */ 13010 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "ctl_work", 0); 13011 13012 /* Back to the top of the loop to see what woke us up. */ 13013 continue; 13014 13015execute: 13016 retval = ctl_scsiio(&io->scsiio); 13017 switch (retval) { 13018 case CTL_RETVAL_COMPLETE: 13019 break; 13020 default: 13021 /* 13022 * Probably need to make sure this doesn't happen. 13023 */ 13024 break; 13025 } 13026 mtx_lock(&softc->ctl_lock); 13027 } 13028} 13029 13030void 13031ctl_wakeup_thread() 13032{ 13033 struct ctl_softc *softc; 13034 13035 softc = control_softc; 13036 13037 wakeup(softc); 13038} 13039 13040/* Initialization and failover */ 13041 13042void 13043ctl_init_isc_msg(void) 13044{ 13045 printf("CTL: Still calling this thing\n"); 13046} 13047 13048/* 13049 * Init component 13050 * Initializes component into configuration defined by bootMode 13051 * (see hasc-sv.c) 13052 * returns hasc_Status: 13053 * OK 13054 * ERROR - fatal error 13055 */ 13056static ctl_ha_comp_status 13057ctl_isc_init(struct ctl_ha_component *c) 13058{ 13059 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13060 13061 c->status = ret; 13062 return ret; 13063} 13064 13065/* Start component 13066 * Starts component in state requested. If component starts successfully, 13067 * it must set its own state to the requestrd state 13068 * When requested state is HASC_STATE_HA, the component may refine it 13069 * by adding _SLAVE or _MASTER flags. 13070 * Currently allowed state transitions are: 13071 * UNKNOWN->HA - initial startup 13072 * UNKNOWN->SINGLE - initial startup when no parter detected 13073 * HA->SINGLE - failover 13074 * returns ctl_ha_comp_status: 13075 * OK - component successfully started in requested state 13076 * FAILED - could not start the requested state, failover may 13077 * be possible 13078 * ERROR - fatal error detected, no future startup possible 13079 */ 13080static ctl_ha_comp_status 13081ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 13082{ 13083 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13084 13085 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 13086 if (c->state == CTL_HA_STATE_UNKNOWN ) { 13087 ctl_is_single = 0; 13088 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 13089 != CTL_HA_STATUS_SUCCESS) { 13090 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 13091 ret = CTL_HA_COMP_STATUS_ERROR; 13092 } 13093 } else if (CTL_HA_STATE_IS_HA(c->state) 13094 && CTL_HA_STATE_IS_SINGLE(state)){ 13095 // HA->SINGLE transition 13096 ctl_failover(); 13097 ctl_is_single = 1; 13098 } else { 13099 printf("ctl_isc_start:Invalid state transition %X->%X\n", 13100 c->state, state); 13101 ret = CTL_HA_COMP_STATUS_ERROR; 13102 } 13103 if (CTL_HA_STATE_IS_SINGLE(state)) 13104 ctl_is_single = 1; 13105 13106 c->state = state; 13107 c->status = ret; 13108 return ret; 13109} 13110 13111/* 13112 * Quiesce component 13113 * The component must clear any error conditions (set status to OK) and 13114 * prepare itself to another Start call 13115 * returns ctl_ha_comp_status: 13116 * OK 13117 * ERROR 13118 */ 13119static ctl_ha_comp_status 13120ctl_isc_quiesce(struct ctl_ha_component *c) 13121{ 13122 int ret = CTL_HA_COMP_STATUS_OK; 13123 13124 ctl_pause_rtr = 1; 13125 c->status = ret; 13126 return ret; 13127} 13128 13129struct ctl_ha_component ctl_ha_component_ctlisc = 13130{ 13131 .name = "CTL ISC", 13132 .state = CTL_HA_STATE_UNKNOWN, 13133 .init = ctl_isc_init, 13134 .start = ctl_isc_start, 13135 .quiesce = ctl_isc_quiesce 13136}; 13137 13138/* 13139 * vim: ts=8 13140 */ 13141