ctl.c revision 268686
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 268686 2014-07-15 17:09:02Z mav $"); 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/types.h> 51#include <sys/kthread.h> 52#include <sys/bio.h> 53#include <sys/fcntl.h> 54#include <sys/lock.h> 55#include <sys/module.h> 56#include <sys/mutex.h> 57#include <sys/condvar.h> 58#include <sys/malloc.h> 59#include <sys/conf.h> 60#include <sys/ioccom.h> 61#include <sys/queue.h> 62#include <sys/sbuf.h> 63#include <sys/smp.h> 64#include <sys/endian.h> 65#include <sys/sysctl.h> 66 67#include <cam/cam.h> 68#include <cam/scsi/scsi_all.h> 69#include <cam/scsi/scsi_da.h> 70#include <cam/ctl/ctl_io.h> 71#include <cam/ctl/ctl.h> 72#include <cam/ctl/ctl_frontend.h> 73#include <cam/ctl/ctl_frontend_internal.h> 74#include <cam/ctl/ctl_util.h> 75#include <cam/ctl/ctl_backend.h> 76#include <cam/ctl/ctl_ioctl.h> 77#include <cam/ctl/ctl_ha.h> 78#include <cam/ctl/ctl_private.h> 79#include <cam/ctl/ctl_debug.h> 80#include <cam/ctl/ctl_scsi_all.h> 81#include <cam/ctl/ctl_error.h> 82 83struct ctl_softc *control_softc = NULL; 84 85/* 86 * Size and alignment macros needed for Copan-specific HA hardware. These 87 * can go away when the HA code is re-written, and uses busdma for any 88 * hardware. 89 */ 90#define CTL_ALIGN_8B(target, source, type) \ 91 if (((uint32_t)source & 0x7) != 0) \ 92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93 else \ 94 target = (type)source; 95 96#define CTL_SIZE_8B(target, size) \ 97 if ((size & 0x7) != 0) \ 98 target = size + (0x8 - (size & 0x7)); \ 99 else \ 100 target = size; 101 102#define CTL_ALIGN_8B_MARGIN 16 103 104/* 105 * Template mode pages. 106 */ 107 108/* 109 * Note that these are default values only. The actual values will be 110 * filled in when the user does a mode sense. 111 */ 112static struct copan_power_subpage power_page_default = { 113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 114 /*subpage*/ PWR_SUBPAGE_CODE, 115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 117 /*page_version*/ PWR_VERSION, 118 /* total_luns */ 26, 119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122 0, 0, 0, 0, 0, 0} 123}; 124 125static struct copan_power_subpage power_page_changeable = { 126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 127 /*subpage*/ PWR_SUBPAGE_CODE, 128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 130 /*page_version*/ 0, 131 /* total_luns */ 0, 132 /* max_active_luns*/ 0, 133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135 0, 0, 0, 0, 0, 0} 136}; 137 138static struct copan_aps_subpage aps_page_default = { 139 APS_PAGE_CODE | SMPH_SPF, //page_code 140 APS_SUBPAGE_CODE, //subpage 141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 143 APS_VERSION, //page_version 144 0, //lock_active 145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147 0, 0, 0, 0, 0} //reserved 148}; 149 150static struct copan_aps_subpage aps_page_changeable = { 151 APS_PAGE_CODE | SMPH_SPF, //page_code 152 APS_SUBPAGE_CODE, //subpage 153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 155 0, //page_version 156 0, //lock_active 157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159 0, 0, 0, 0, 0} //reserved 160}; 161 162static struct copan_debugconf_subpage debugconf_page_default = { 163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 164 DBGCNF_SUBPAGE_CODE, /* subpage */ 165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 167 DBGCNF_VERSION, /* page_version */ 168 {CTL_TIME_IO_DEFAULT_SECS>>8, 169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 170}; 171 172static struct copan_debugconf_subpage debugconf_page_changeable = { 173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 174 DBGCNF_SUBPAGE_CODE, /* subpage */ 175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 177 0, /* page_version */ 178 {0xff,0xff}, /* ctl_time_io_secs */ 179}; 180 181static struct scsi_format_page format_page_default = { 182 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 183 /*page_length*/sizeof(struct scsi_format_page) - 2, 184 /*tracks_per_zone*/ {0, 0}, 185 /*alt_sectors_per_zone*/ {0, 0}, 186 /*alt_tracks_per_zone*/ {0, 0}, 187 /*alt_tracks_per_lun*/ {0, 0}, 188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 190 /*bytes_per_sector*/ {0, 0}, 191 /*interleave*/ {0, 0}, 192 /*track_skew*/ {0, 0}, 193 /*cylinder_skew*/ {0, 0}, 194 /*flags*/ SFP_HSEC, 195 /*reserved*/ {0, 0, 0} 196}; 197 198static struct scsi_format_page format_page_changeable = { 199 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 200 /*page_length*/sizeof(struct scsi_format_page) - 2, 201 /*tracks_per_zone*/ {0, 0}, 202 /*alt_sectors_per_zone*/ {0, 0}, 203 /*alt_tracks_per_zone*/ {0, 0}, 204 /*alt_tracks_per_lun*/ {0, 0}, 205 /*sectors_per_track*/ {0, 0}, 206 /*bytes_per_sector*/ {0, 0}, 207 /*interleave*/ {0, 0}, 208 /*track_skew*/ {0, 0}, 209 /*cylinder_skew*/ {0, 0}, 210 /*flags*/ 0, 211 /*reserved*/ {0, 0, 0} 212}; 213 214static struct scsi_rigid_disk_page rigid_disk_page_default = { 215 /*page_code*/SMS_RIGID_DISK_PAGE, 216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 217 /*cylinders*/ {0, 0, 0}, 218 /*heads*/ CTL_DEFAULT_HEADS, 219 /*start_write_precomp*/ {0, 0, 0}, 220 /*start_reduced_current*/ {0, 0, 0}, 221 /*step_rate*/ {0, 0}, 222 /*landing_zone_cylinder*/ {0, 0, 0}, 223 /*rpl*/ SRDP_RPL_DISABLED, 224 /*rotational_offset*/ 0, 225 /*reserved1*/ 0, 226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 227 CTL_DEFAULT_ROTATION_RATE & 0xff}, 228 /*reserved2*/ {0, 0} 229}; 230 231static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 232 /*page_code*/SMS_RIGID_DISK_PAGE, 233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 234 /*cylinders*/ {0, 0, 0}, 235 /*heads*/ 0, 236 /*start_write_precomp*/ {0, 0, 0}, 237 /*start_reduced_current*/ {0, 0, 0}, 238 /*step_rate*/ {0, 0}, 239 /*landing_zone_cylinder*/ {0, 0, 0}, 240 /*rpl*/ 0, 241 /*rotational_offset*/ 0, 242 /*reserved1*/ 0, 243 /*rotation_rate*/ {0, 0}, 244 /*reserved2*/ {0, 0} 245}; 246 247static struct scsi_caching_page caching_page_default = { 248 /*page_code*/SMS_CACHING_PAGE, 249 /*page_length*/sizeof(struct scsi_caching_page) - 2, 250 /*flags1*/ SCP_DISC | SCP_WCE, 251 /*ret_priority*/ 0, 252 /*disable_pf_transfer_len*/ {0xff, 0xff}, 253 /*min_prefetch*/ {0, 0}, 254 /*max_prefetch*/ {0xff, 0xff}, 255 /*max_pf_ceiling*/ {0xff, 0xff}, 256 /*flags2*/ 0, 257 /*cache_segments*/ 0, 258 /*cache_seg_size*/ {0, 0}, 259 /*reserved*/ 0, 260 /*non_cache_seg_size*/ {0, 0, 0} 261}; 262 263static struct scsi_caching_page caching_page_changeable = { 264 /*page_code*/SMS_CACHING_PAGE, 265 /*page_length*/sizeof(struct scsi_caching_page) - 2, 266 /*flags1*/ 0, 267 /*ret_priority*/ 0, 268 /*disable_pf_transfer_len*/ {0, 0}, 269 /*min_prefetch*/ {0, 0}, 270 /*max_prefetch*/ {0, 0}, 271 /*max_pf_ceiling*/ {0, 0}, 272 /*flags2*/ 0, 273 /*cache_segments*/ 0, 274 /*cache_seg_size*/ {0, 0}, 275 /*reserved*/ 0, 276 /*non_cache_seg_size*/ {0, 0, 0} 277}; 278 279static struct scsi_control_page control_page_default = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE, 281 /*page_length*/sizeof(struct scsi_control_page) - 2, 282 /*rlec*/0, 283 /*queue_flags*/0, 284 /*eca_and_aen*/0, 285 /*reserved*/0, 286 /*aen_holdoff_period*/{0, 0} 287}; 288 289static struct scsi_control_page control_page_changeable = { 290 /*page_code*/SMS_CONTROL_MODE_PAGE, 291 /*page_length*/sizeof(struct scsi_control_page) - 2, 292 /*rlec*/SCP_DSENSE, 293 /*queue_flags*/0, 294 /*eca_and_aen*/0, 295 /*reserved*/0, 296 /*aen_holdoff_period*/{0, 0} 297}; 298 299 300/* 301 * XXX KDM move these into the softc. 302 */ 303static int rcv_sync_msg; 304static int persis_offset; 305static uint8_t ctl_pause_rtr; 306static int ctl_is_single = 1; 307static int index_to_aps_page; 308 309SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 310static int worker_threads = -1; 311TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 312SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 313 &worker_threads, 1, "Number of worker threads"); 314static int verbose = 0; 315TUNABLE_INT("kern.cam.ctl.verbose", &verbose); 316SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 317 &verbose, 0, "Show SCSI errors returned to initiator"); 318 319/* 320 * Serial number (0x80), device id (0x83), supported pages (0x00), 321 * Block limits (0xB0) and Logical Block Provisioning (0xB2) 322 */ 323#define SCSI_EVPD_NUM_SUPPORTED_PAGES 5 324 325static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 326 int param); 327static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 328static int ctl_init(void); 329void ctl_shutdown(void); 330static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 331static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 332static void ctl_ioctl_online(void *arg); 333static void ctl_ioctl_offline(void *arg); 334static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 335static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 336static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 337static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 338static int ctl_ioctl_submit_wait(union ctl_io *io); 339static void ctl_ioctl_datamove(union ctl_io *io); 340static void ctl_ioctl_done(union ctl_io *io); 341static void ctl_ioctl_hard_startstop_callback(void *arg, 342 struct cfi_metatask *metatask); 343static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 344static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 345 struct ctl_ooa *ooa_hdr, 346 struct ctl_ooa_entry *kern_entries); 347static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 348 struct thread *td); 349uint32_t ctl_get_resindex(struct ctl_nexus *nexus); 350uint32_t ctl_port_idx(int port_num); 351static uint32_t ctl_map_lun(int port_num, uint32_t lun); 352static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 353#ifdef unused 354static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 355 uint32_t targ_target, uint32_t targ_lun, 356 int can_wait); 357static void ctl_kfree_io(union ctl_io *io); 358#endif /* unused */ 359static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 360 struct ctl_be_lun *be_lun, struct ctl_id target_id); 361static int ctl_free_lun(struct ctl_lun *lun); 362static void ctl_create_lun(struct ctl_be_lun *be_lun); 363/** 364static void ctl_failover_change_pages(struct ctl_softc *softc, 365 struct ctl_scsiio *ctsio, int master); 366**/ 367 368static int ctl_do_mode_select(union ctl_io *io); 369static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 370 uint64_t res_key, uint64_t sa_res_key, 371 uint8_t type, uint32_t residx, 372 struct ctl_scsiio *ctsio, 373 struct scsi_per_res_out *cdb, 374 struct scsi_per_res_out_parms* param); 375static void ctl_pro_preempt_other(struct ctl_lun *lun, 376 union ctl_ha_msg *msg); 377static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 378static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 379static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 380static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 381static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 382 int alloc_len); 383static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 384static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 385static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 386static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 387static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 388static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 389 union ctl_io *ooa_io); 390static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 391 union ctl_io *starting_io); 392static int ctl_check_blocked(struct ctl_lun *lun); 393static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 394 struct ctl_lun *lun, 395 const struct ctl_cmd_entry *entry, 396 struct ctl_scsiio *ctsio); 397//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 398static void ctl_failover(void); 399static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 400 struct ctl_scsiio *ctsio); 401static int ctl_scsiio(struct ctl_scsiio *ctsio); 402 403static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 404static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 405 ctl_ua_type ua_type); 406static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 407 ctl_ua_type ua_type); 408static int ctl_abort_task(union ctl_io *io); 409static void ctl_run_task(union ctl_io *io); 410#ifdef CTL_IO_DELAY 411static void ctl_datamove_timer_wakeup(void *arg); 412static void ctl_done_timer_wakeup(void *arg); 413#endif /* CTL_IO_DELAY */ 414 415static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 416static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 417static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 418static void ctl_datamove_remote_write(union ctl_io *io); 419static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 420static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 421static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 422static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 423 ctl_ha_dt_cb callback); 424static void ctl_datamove_remote_read(union ctl_io *io); 425static void ctl_datamove_remote(union ctl_io *io); 426static int ctl_process_done(union ctl_io *io); 427static void ctl_lun_thread(void *arg); 428static void ctl_work_thread(void *arg); 429static void ctl_enqueue_incoming(union ctl_io *io); 430static void ctl_enqueue_rtr(union ctl_io *io); 431static void ctl_enqueue_done(union ctl_io *io); 432static void ctl_enqueue_isc(union ctl_io *io); 433static const struct ctl_cmd_entry * 434 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 435static const struct ctl_cmd_entry * 436 ctl_validate_command(struct ctl_scsiio *ctsio); 437static int ctl_cmd_applicable(uint8_t lun_type, 438 const struct ctl_cmd_entry *entry); 439 440/* 441 * Load the serialization table. This isn't very pretty, but is probably 442 * the easiest way to do it. 443 */ 444#include "ctl_ser_table.c" 445 446/* 447 * We only need to define open, close and ioctl routines for this driver. 448 */ 449static struct cdevsw ctl_cdevsw = { 450 .d_version = D_VERSION, 451 .d_flags = 0, 452 .d_open = ctl_open, 453 .d_close = ctl_close, 454 .d_ioctl = ctl_ioctl, 455 .d_name = "ctl", 456}; 457 458 459MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 460MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 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 struct ctl_frontend ioctl_frontend = 474{ 475 .name = "ioctl", 476}; 477 478static void 479ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 480 union ctl_ha_msg *msg_info) 481{ 482 struct ctl_scsiio *ctsio; 483 484 if (msg_info->hdr.original_sc == NULL) { 485 printf("%s: original_sc == NULL!\n", __func__); 486 /* XXX KDM now what? */ 487 return; 488 } 489 490 ctsio = &msg_info->hdr.original_sc->scsiio; 491 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 492 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 493 ctsio->io_hdr.status = msg_info->hdr.status; 494 ctsio->scsi_status = msg_info->scsi.scsi_status; 495 ctsio->sense_len = msg_info->scsi.sense_len; 496 ctsio->sense_residual = msg_info->scsi.sense_residual; 497 ctsio->residual = msg_info->scsi.residual; 498 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 499 sizeof(ctsio->sense_data)); 500 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 501 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 502 ctl_enqueue_isc((union ctl_io *)ctsio); 503} 504 505static void 506ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 507 union ctl_ha_msg *msg_info) 508{ 509 struct ctl_scsiio *ctsio; 510 511 if (msg_info->hdr.serializing_sc == NULL) { 512 printf("%s: serializing_sc == NULL!\n", __func__); 513 /* XXX KDM now what? */ 514 return; 515 } 516 517 ctsio = &msg_info->hdr.serializing_sc->scsiio; 518#if 0 519 /* 520 * Attempt to catch the situation where an I/O has 521 * been freed, and we're using it again. 522 */ 523 if (ctsio->io_hdr.io_type == 0xff) { 524 union ctl_io *tmp_io; 525 tmp_io = (union ctl_io *)ctsio; 526 printf("%s: %p use after free!\n", __func__, 527 ctsio); 528 printf("%s: type %d msg %d cdb %x iptl: " 529 "%d:%d:%d:%d tag 0x%04x " 530 "flag %#x status %x\n", 531 __func__, 532 tmp_io->io_hdr.io_type, 533 tmp_io->io_hdr.msg_type, 534 tmp_io->scsiio.cdb[0], 535 tmp_io->io_hdr.nexus.initid.id, 536 tmp_io->io_hdr.nexus.targ_port, 537 tmp_io->io_hdr.nexus.targ_target.id, 538 tmp_io->io_hdr.nexus.targ_lun, 539 (tmp_io->io_hdr.io_type == 540 CTL_IO_TASK) ? 541 tmp_io->taskio.tag_num : 542 tmp_io->scsiio.tag_num, 543 tmp_io->io_hdr.flags, 544 tmp_io->io_hdr.status); 545 } 546#endif 547 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 548 ctl_enqueue_isc((union ctl_io *)ctsio); 549} 550 551/* 552 * ISC (Inter Shelf Communication) event handler. Events from the HA 553 * subsystem come in here. 554 */ 555static void 556ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 557{ 558 struct ctl_softc *ctl_softc; 559 union ctl_io *io; 560 struct ctl_prio *presio; 561 ctl_ha_status isc_status; 562 563 ctl_softc = control_softc; 564 io = NULL; 565 566 567#if 0 568 printf("CTL: Isc Msg event %d\n", event); 569#endif 570 if (event == CTL_HA_EVT_MSG_RECV) { 571 union ctl_ha_msg msg_info; 572 573 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 574 sizeof(msg_info), /*wait*/ 0); 575#if 0 576 printf("CTL: msg_type %d\n", msg_info.msg_type); 577#endif 578 if (isc_status != 0) { 579 printf("Error receiving message, status = %d\n", 580 isc_status); 581 return; 582 } 583 584 switch (msg_info.hdr.msg_type) { 585 case CTL_MSG_SERIALIZE: 586#if 0 587 printf("Serialize\n"); 588#endif 589 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 590 if (io == NULL) { 591 printf("ctl_isc_event_handler: can't allocate " 592 "ctl_io!\n"); 593 /* Bad Juju */ 594 /* Need to set busy and send msg back */ 595 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 596 msg_info.hdr.status = CTL_SCSI_ERROR; 597 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 598 msg_info.scsi.sense_len = 0; 599 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 600 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 601 } 602 goto bailout; 603 } 604 ctl_zero_io(io); 605 // populate ctsio from msg_info 606 io->io_hdr.io_type = CTL_IO_SCSI; 607 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 608 io->io_hdr.original_sc = msg_info.hdr.original_sc; 609#if 0 610 printf("pOrig %x\n", (int)msg_info.original_sc); 611#endif 612 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 613 CTL_FLAG_IO_ACTIVE; 614 /* 615 * If we're in serialization-only mode, we don't 616 * want to go through full done processing. Thus 617 * the COPY flag. 618 * 619 * XXX KDM add another flag that is more specific. 620 */ 621 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 622 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 623 io->io_hdr.nexus = msg_info.hdr.nexus; 624#if 0 625 printf("targ %d, port %d, iid %d, lun %d\n", 626 io->io_hdr.nexus.targ_target.id, 627 io->io_hdr.nexus.targ_port, 628 io->io_hdr.nexus.initid.id, 629 io->io_hdr.nexus.targ_lun); 630#endif 631 io->scsiio.tag_num = msg_info.scsi.tag_num; 632 io->scsiio.tag_type = msg_info.scsi.tag_type; 633 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 634 CTL_MAX_CDBLEN); 635 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 636 const struct ctl_cmd_entry *entry; 637 638 entry = ctl_get_cmd_entry(&io->scsiio); 639 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 640 io->io_hdr.flags |= 641 entry->flags & CTL_FLAG_DATA_MASK; 642 } 643 ctl_enqueue_isc(io); 644 break; 645 646 /* Performed on the Originating SC, XFER mode only */ 647 case CTL_MSG_DATAMOVE: { 648 struct ctl_sg_entry *sgl; 649 int i, j; 650 651 io = msg_info.hdr.original_sc; 652 if (io == NULL) { 653 printf("%s: original_sc == NULL!\n", __func__); 654 /* XXX KDM do something here */ 655 break; 656 } 657 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 658 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 659 /* 660 * Keep track of this, we need to send it back over 661 * when the datamove is complete. 662 */ 663 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 664 665 if (msg_info.dt.sg_sequence == 0) { 666 /* 667 * XXX KDM we use the preallocated S/G list 668 * here, but we'll need to change this to 669 * dynamic allocation if we need larger S/G 670 * lists. 671 */ 672 if (msg_info.dt.kern_sg_entries > 673 sizeof(io->io_hdr.remote_sglist) / 674 sizeof(io->io_hdr.remote_sglist[0])) { 675 printf("%s: number of S/G entries " 676 "needed %u > allocated num %zd\n", 677 __func__, 678 msg_info.dt.kern_sg_entries, 679 sizeof(io->io_hdr.remote_sglist)/ 680 sizeof(io->io_hdr.remote_sglist[0])); 681 682 /* 683 * XXX KDM send a message back to 684 * the other side to shut down the 685 * DMA. The error will come back 686 * through via the normal channel. 687 */ 688 break; 689 } 690 sgl = io->io_hdr.remote_sglist; 691 memset(sgl, 0, 692 sizeof(io->io_hdr.remote_sglist)); 693 694 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 695 696 io->scsiio.kern_sg_entries = 697 msg_info.dt.kern_sg_entries; 698 io->scsiio.rem_sg_entries = 699 msg_info.dt.kern_sg_entries; 700 io->scsiio.kern_data_len = 701 msg_info.dt.kern_data_len; 702 io->scsiio.kern_total_len = 703 msg_info.dt.kern_total_len; 704 io->scsiio.kern_data_resid = 705 msg_info.dt.kern_data_resid; 706 io->scsiio.kern_rel_offset = 707 msg_info.dt.kern_rel_offset; 708 /* 709 * Clear out per-DMA flags. 710 */ 711 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 712 /* 713 * Add per-DMA flags that are set for this 714 * particular DMA request. 715 */ 716 io->io_hdr.flags |= msg_info.dt.flags & 717 CTL_FLAG_RDMA_MASK; 718 } else 719 sgl = (struct ctl_sg_entry *) 720 io->scsiio.kern_data_ptr; 721 722 for (i = msg_info.dt.sent_sg_entries, j = 0; 723 i < (msg_info.dt.sent_sg_entries + 724 msg_info.dt.cur_sg_entries); i++, j++) { 725 sgl[i].addr = msg_info.dt.sg_list[j].addr; 726 sgl[i].len = msg_info.dt.sg_list[j].len; 727 728#if 0 729 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 730 __func__, 731 msg_info.dt.sg_list[j].addr, 732 msg_info.dt.sg_list[j].len, 733 sgl[i].addr, sgl[i].len, j, i); 734#endif 735 } 736#if 0 737 memcpy(&sgl[msg_info.dt.sent_sg_entries], 738 msg_info.dt.sg_list, 739 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 740#endif 741 742 /* 743 * If this is the last piece of the I/O, we've got 744 * the full S/G list. Queue processing in the thread. 745 * Otherwise wait for the next piece. 746 */ 747 if (msg_info.dt.sg_last != 0) 748 ctl_enqueue_isc(io); 749 break; 750 } 751 /* Performed on the Serializing (primary) SC, XFER mode only */ 752 case CTL_MSG_DATAMOVE_DONE: { 753 if (msg_info.hdr.serializing_sc == NULL) { 754 printf("%s: serializing_sc == NULL!\n", 755 __func__); 756 /* XXX KDM now what? */ 757 break; 758 } 759 /* 760 * We grab the sense information here in case 761 * there was a failure, so we can return status 762 * back to the initiator. 763 */ 764 io = msg_info.hdr.serializing_sc; 765 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 766 io->io_hdr.status = msg_info.hdr.status; 767 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 768 io->scsiio.sense_len = msg_info.scsi.sense_len; 769 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 770 io->io_hdr.port_status = msg_info.scsi.fetd_status; 771 io->scsiio.residual = msg_info.scsi.residual; 772 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 773 sizeof(io->scsiio.sense_data)); 774 ctl_enqueue_isc(io); 775 break; 776 } 777 778 /* Preformed on Originating SC, SER_ONLY mode */ 779 case CTL_MSG_R2R: 780 io = msg_info.hdr.original_sc; 781 if (io == NULL) { 782 printf("%s: Major Bummer\n", __func__); 783 return; 784 } else { 785#if 0 786 printf("pOrig %x\n",(int) ctsio); 787#endif 788 } 789 io->io_hdr.msg_type = CTL_MSG_R2R; 790 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 791 ctl_enqueue_isc(io); 792 break; 793 794 /* 795 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 796 * mode. 797 * Performed on the Originating (i.e. secondary) SC in XFER 798 * mode 799 */ 800 case CTL_MSG_FINISH_IO: 801 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 802 ctl_isc_handler_finish_xfer(ctl_softc, 803 &msg_info); 804 else 805 ctl_isc_handler_finish_ser_only(ctl_softc, 806 &msg_info); 807 break; 808 809 /* Preformed on Originating SC */ 810 case CTL_MSG_BAD_JUJU: 811 io = msg_info.hdr.original_sc; 812 if (io == NULL) { 813 printf("%s: Bad JUJU!, original_sc is NULL!\n", 814 __func__); 815 break; 816 } 817 ctl_copy_sense_data(&msg_info, io); 818 /* 819 * IO should have already been cleaned up on other 820 * SC so clear this flag so we won't send a message 821 * back to finish the IO there. 822 */ 823 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 824 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 825 826 /* io = msg_info.hdr.serializing_sc; */ 827 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 828 ctl_enqueue_isc(io); 829 break; 830 831 /* Handle resets sent from the other side */ 832 case CTL_MSG_MANAGE_TASKS: { 833 struct ctl_taskio *taskio; 834 taskio = (struct ctl_taskio *)ctl_alloc_io( 835 (void *)ctl_softc->othersc_pool); 836 if (taskio == NULL) { 837 printf("ctl_isc_event_handler: can't allocate " 838 "ctl_io!\n"); 839 /* Bad Juju */ 840 /* should I just call the proper reset func 841 here??? */ 842 goto bailout; 843 } 844 ctl_zero_io((union ctl_io *)taskio); 845 taskio->io_hdr.io_type = CTL_IO_TASK; 846 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 847 taskio->io_hdr.nexus = msg_info.hdr.nexus; 848 taskio->task_action = msg_info.task.task_action; 849 taskio->tag_num = msg_info.task.tag_num; 850 taskio->tag_type = msg_info.task.tag_type; 851#ifdef CTL_TIME_IO 852 taskio->io_hdr.start_time = time_uptime; 853 getbintime(&taskio->io_hdr.start_bt); 854#if 0 855 cs_prof_gettime(&taskio->io_hdr.start_ticks); 856#endif 857#endif /* CTL_TIME_IO */ 858 ctl_run_task((union ctl_io *)taskio); 859 break; 860 } 861 /* Persistent Reserve action which needs attention */ 862 case CTL_MSG_PERS_ACTION: 863 presio = (struct ctl_prio *)ctl_alloc_io( 864 (void *)ctl_softc->othersc_pool); 865 if (presio == NULL) { 866 printf("ctl_isc_event_handler: can't allocate " 867 "ctl_io!\n"); 868 /* Bad Juju */ 869 /* Need to set busy and send msg back */ 870 goto bailout; 871 } 872 ctl_zero_io((union ctl_io *)presio); 873 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 874 presio->pr_msg = msg_info.pr; 875 ctl_enqueue_isc((union ctl_io *)presio); 876 break; 877 case CTL_MSG_SYNC_FE: 878 rcv_sync_msg = 1; 879 break; 880 case CTL_MSG_APS_LOCK: { 881 // It's quicker to execute this then to 882 // queue it. 883 struct ctl_lun *lun; 884 struct ctl_page_index *page_index; 885 struct copan_aps_subpage *current_sp; 886 uint32_t targ_lun; 887 888 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 889 lun = ctl_softc->ctl_luns[targ_lun]; 890 mtx_lock(&lun->lun_lock); 891 page_index = &lun->mode_pages.index[index_to_aps_page]; 892 current_sp = (struct copan_aps_subpage *) 893 (page_index->page_data + 894 (page_index->page_len * CTL_PAGE_CURRENT)); 895 896 current_sp->lock_active = msg_info.aps.lock_flag; 897 mtx_unlock(&lun->lun_lock); 898 break; 899 } 900 default: 901 printf("How did I get here?\n"); 902 } 903 } else if (event == CTL_HA_EVT_MSG_SENT) { 904 if (param != CTL_HA_STATUS_SUCCESS) { 905 printf("Bad status from ctl_ha_msg_send status %d\n", 906 param); 907 } 908 return; 909 } else if (event == CTL_HA_EVT_DISCONNECT) { 910 printf("CTL: Got a disconnect from Isc\n"); 911 return; 912 } else { 913 printf("ctl_isc_event_handler: Unknown event %d\n", event); 914 return; 915 } 916 917bailout: 918 return; 919} 920 921static void 922ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 923{ 924 struct scsi_sense_data *sense; 925 926 sense = &dest->scsiio.sense_data; 927 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 928 dest->scsiio.scsi_status = src->scsi.scsi_status; 929 dest->scsiio.sense_len = src->scsi.sense_len; 930 dest->io_hdr.status = src->hdr.status; 931} 932 933static int 934ctl_init(void) 935{ 936 struct ctl_softc *softc; 937 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 938 struct ctl_port *port; 939 uint8_t sc_id =0; 940 int i, error, retval; 941 //int isc_retval; 942 943 retval = 0; 944 ctl_pause_rtr = 0; 945 rcv_sync_msg = 0; 946 947 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 948 M_WAITOK | M_ZERO); 949 softc = control_softc; 950 951 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 952 "cam/ctl"); 953 954 softc->dev->si_drv1 = softc; 955 956 /* 957 * By default, return a "bad LUN" peripheral qualifier for unknown 958 * LUNs. The user can override this default using the tunable or 959 * sysctl. See the comment in ctl_inquiry_std() for more details. 960 */ 961 softc->inquiry_pq_no_lun = 1; 962 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 963 &softc->inquiry_pq_no_lun); 964 sysctl_ctx_init(&softc->sysctl_ctx); 965 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 966 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 967 CTLFLAG_RD, 0, "CAM Target Layer"); 968 969 if (softc->sysctl_tree == NULL) { 970 printf("%s: unable to allocate sysctl tree\n", __func__); 971 destroy_dev(softc->dev); 972 free(control_softc, M_DEVBUF); 973 control_softc = NULL; 974 return (ENOMEM); 975 } 976 977 SYSCTL_ADD_INT(&softc->sysctl_ctx, 978 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 979 "inquiry_pq_no_lun", CTLFLAG_RW, 980 &softc->inquiry_pq_no_lun, 0, 981 "Report no lun possible for invalid LUNs"); 982 983 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 984 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 985 softc->open_count = 0; 986 987 /* 988 * Default to actually sending a SYNCHRONIZE CACHE command down to 989 * the drive. 990 */ 991 softc->flags = CTL_FLAG_REAL_SYNC; 992 993 /* 994 * In Copan's HA scheme, the "master" and "slave" roles are 995 * figured out through the slot the controller is in. Although it 996 * is an active/active system, someone has to be in charge. 997 */ 998#ifdef NEEDTOPORT 999 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1000#endif 1001 1002 if (sc_id == 0) { 1003 softc->flags |= CTL_FLAG_MASTER_SHELF; 1004 persis_offset = 0; 1005 } else 1006 persis_offset = CTL_MAX_INITIATORS; 1007 1008 /* 1009 * XXX KDM need to figure out where we want to get our target ID 1010 * and WWID. Is it different on each port? 1011 */ 1012 softc->target.id = 0; 1013 softc->target.wwid[0] = 0x12345678; 1014 softc->target.wwid[1] = 0x87654321; 1015 STAILQ_INIT(&softc->lun_list); 1016 STAILQ_INIT(&softc->pending_lun_queue); 1017 STAILQ_INIT(&softc->fe_list); 1018 STAILQ_INIT(&softc->port_list); 1019 STAILQ_INIT(&softc->be_list); 1020 STAILQ_INIT(&softc->io_pools); 1021 1022 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1023 &internal_pool)!= 0){ 1024 printf("ctl: can't allocate %d entry internal pool, " 1025 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1026 return (ENOMEM); 1027 } 1028 1029 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1030 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1031 printf("ctl: can't allocate %d entry emergency pool, " 1032 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1033 ctl_pool_free(internal_pool); 1034 return (ENOMEM); 1035 } 1036 1037 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1038 &other_pool) != 0) 1039 { 1040 printf("ctl: can't allocate %d entry other SC pool, " 1041 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1042 ctl_pool_free(internal_pool); 1043 ctl_pool_free(emergency_pool); 1044 return (ENOMEM); 1045 } 1046 1047 softc->internal_pool = internal_pool; 1048 softc->emergency_pool = emergency_pool; 1049 softc->othersc_pool = other_pool; 1050 1051 if (worker_threads <= 0) 1052 worker_threads = max(1, mp_ncpus / 4); 1053 if (worker_threads > CTL_MAX_THREADS) 1054 worker_threads = CTL_MAX_THREADS; 1055 1056 for (i = 0; i < worker_threads; i++) { 1057 struct ctl_thread *thr = &softc->threads[i]; 1058 1059 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1060 thr->ctl_softc = softc; 1061 STAILQ_INIT(&thr->incoming_queue); 1062 STAILQ_INIT(&thr->rtr_queue); 1063 STAILQ_INIT(&thr->done_queue); 1064 STAILQ_INIT(&thr->isc_queue); 1065 1066 error = kproc_kthread_add(ctl_work_thread, thr, 1067 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1068 if (error != 0) { 1069 printf("error creating CTL work thread!\n"); 1070 ctl_pool_free(internal_pool); 1071 ctl_pool_free(emergency_pool); 1072 ctl_pool_free(other_pool); 1073 return (error); 1074 } 1075 } 1076 error = kproc_kthread_add(ctl_lun_thread, softc, 1077 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1078 if (error != 0) { 1079 printf("error creating CTL lun thread!\n"); 1080 ctl_pool_free(internal_pool); 1081 ctl_pool_free(emergency_pool); 1082 ctl_pool_free(other_pool); 1083 return (error); 1084 } 1085 if (bootverbose) 1086 printf("ctl: CAM Target Layer loaded\n"); 1087 1088 /* 1089 * Initialize the initiator and portname mappings 1090 */ 1091 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid)); 1092 1093 /* 1094 * Initialize the ioctl front end. 1095 */ 1096 ctl_frontend_register(&ioctl_frontend); 1097 port = &softc->ioctl_info.port; 1098 port->frontend = &ioctl_frontend; 1099 sprintf(softc->ioctl_info.port_name, "ioctl"); 1100 port->port_type = CTL_PORT_IOCTL; 1101 port->num_requested_ctl_io = 100; 1102 port->port_name = softc->ioctl_info.port_name; 1103 port->port_online = ctl_ioctl_online; 1104 port->port_offline = ctl_ioctl_offline; 1105 port->onoff_arg = &softc->ioctl_info; 1106 port->lun_enable = ctl_ioctl_lun_enable; 1107 port->lun_disable = ctl_ioctl_lun_disable; 1108 port->targ_lun_arg = &softc->ioctl_info; 1109 port->fe_datamove = ctl_ioctl_datamove; 1110 port->fe_done = ctl_ioctl_done; 1111 port->max_targets = 15; 1112 port->max_target_id = 15; 1113 1114 if (ctl_port_register(&softc->ioctl_info.port, 1115 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1116 printf("ctl: ioctl front end registration failed, will " 1117 "continue anyway\n"); 1118 } 1119 1120#ifdef CTL_IO_DELAY 1121 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1122 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1123 sizeof(struct callout), CTL_TIMER_BYTES); 1124 return (EINVAL); 1125 } 1126#endif /* CTL_IO_DELAY */ 1127 1128 return (0); 1129} 1130 1131void 1132ctl_shutdown(void) 1133{ 1134 struct ctl_softc *softc; 1135 struct ctl_lun *lun, *next_lun; 1136 struct ctl_io_pool *pool; 1137 1138 softc = (struct ctl_softc *)control_softc; 1139 1140 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1141 printf("ctl: ioctl front end deregistration failed\n"); 1142 1143 mtx_lock(&softc->ctl_lock); 1144 1145 /* 1146 * Free up each LUN. 1147 */ 1148 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1149 next_lun = STAILQ_NEXT(lun, links); 1150 ctl_free_lun(lun); 1151 } 1152 1153 mtx_unlock(&softc->ctl_lock); 1154 1155 ctl_frontend_deregister(&ioctl_frontend); 1156 1157 /* 1158 * This will rip the rug out from under any FETDs or anyone else 1159 * that has a pool allocated. Since we increment our module 1160 * refcount any time someone outside the main CTL module allocates 1161 * a pool, we shouldn't have any problems here. The user won't be 1162 * able to unload the CTL module until client modules have 1163 * successfully unloaded. 1164 */ 1165 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1166 ctl_pool_free(pool); 1167 1168#if 0 1169 ctl_shutdown_thread(softc->work_thread); 1170 mtx_destroy(&softc->queue_lock); 1171#endif 1172 1173 mtx_destroy(&softc->pool_lock); 1174 mtx_destroy(&softc->ctl_lock); 1175 1176 destroy_dev(softc->dev); 1177 1178 sysctl_ctx_free(&softc->sysctl_ctx); 1179 1180 free(control_softc, M_DEVBUF); 1181 control_softc = NULL; 1182 1183 if (bootverbose) 1184 printf("ctl: CAM Target Layer unloaded\n"); 1185} 1186 1187static int 1188ctl_module_event_handler(module_t mod, int what, void *arg) 1189{ 1190 1191 switch (what) { 1192 case MOD_LOAD: 1193 return (ctl_init()); 1194 case MOD_UNLOAD: 1195 return (EBUSY); 1196 default: 1197 return (EOPNOTSUPP); 1198 } 1199} 1200 1201/* 1202 * XXX KDM should we do some access checks here? Bump a reference count to 1203 * prevent a CTL module from being unloaded while someone has it open? 1204 */ 1205static int 1206ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1207{ 1208 return (0); 1209} 1210 1211static int 1212ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1213{ 1214 return (0); 1215} 1216 1217int 1218ctl_port_enable(ctl_port_type port_type) 1219{ 1220 struct ctl_softc *softc; 1221 struct ctl_port *port; 1222 1223 if (ctl_is_single == 0) { 1224 union ctl_ha_msg msg_info; 1225 int isc_retval; 1226 1227#if 0 1228 printf("%s: HA mode, synchronizing frontend enable\n", 1229 __func__); 1230#endif 1231 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1232 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1233 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1234 printf("Sync msg send error retval %d\n", isc_retval); 1235 } 1236 if (!rcv_sync_msg) { 1237 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1238 sizeof(msg_info), 1); 1239 } 1240#if 0 1241 printf("CTL:Frontend Enable\n"); 1242 } else { 1243 printf("%s: single mode, skipping frontend synchronization\n", 1244 __func__); 1245#endif 1246 } 1247 1248 softc = control_softc; 1249 1250 STAILQ_FOREACH(port, &softc->port_list, links) { 1251 if (port_type & port->port_type) 1252 { 1253#if 0 1254 printf("port %d\n", port->targ_port); 1255#endif 1256 ctl_port_online(port); 1257 } 1258 } 1259 1260 return (0); 1261} 1262 1263int 1264ctl_port_disable(ctl_port_type port_type) 1265{ 1266 struct ctl_softc *softc; 1267 struct ctl_port *port; 1268 1269 softc = control_softc; 1270 1271 STAILQ_FOREACH(port, &softc->port_list, links) { 1272 if (port_type & port->port_type) 1273 ctl_port_offline(port); 1274 } 1275 1276 return (0); 1277} 1278 1279/* 1280 * Returns 0 for success, 1 for failure. 1281 * Currently the only failure mode is if there aren't enough entries 1282 * allocated. So, in case of a failure, look at num_entries_dropped, 1283 * reallocate and try again. 1284 */ 1285int 1286ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1287 int *num_entries_filled, int *num_entries_dropped, 1288 ctl_port_type port_type, int no_virtual) 1289{ 1290 struct ctl_softc *softc; 1291 struct ctl_port *port; 1292 int entries_dropped, entries_filled; 1293 int retval; 1294 int i; 1295 1296 softc = control_softc; 1297 1298 retval = 0; 1299 entries_filled = 0; 1300 entries_dropped = 0; 1301 1302 i = 0; 1303 mtx_lock(&softc->ctl_lock); 1304 STAILQ_FOREACH(port, &softc->port_list, links) { 1305 struct ctl_port_entry *entry; 1306 1307 if ((port->port_type & port_type) == 0) 1308 continue; 1309 1310 if ((no_virtual != 0) 1311 && (port->virtual_port != 0)) 1312 continue; 1313 1314 if (entries_filled >= num_entries_alloced) { 1315 entries_dropped++; 1316 continue; 1317 } 1318 entry = &entries[i]; 1319 1320 entry->port_type = port->port_type; 1321 strlcpy(entry->port_name, port->port_name, 1322 sizeof(entry->port_name)); 1323 entry->physical_port = port->physical_port; 1324 entry->virtual_port = port->virtual_port; 1325 entry->wwnn = port->wwnn; 1326 entry->wwpn = port->wwpn; 1327 1328 i++; 1329 entries_filled++; 1330 } 1331 1332 mtx_unlock(&softc->ctl_lock); 1333 1334 if (entries_dropped > 0) 1335 retval = 1; 1336 1337 *num_entries_dropped = entries_dropped; 1338 *num_entries_filled = entries_filled; 1339 1340 return (retval); 1341} 1342 1343static void 1344ctl_ioctl_online(void *arg) 1345{ 1346 struct ctl_ioctl_info *ioctl_info; 1347 1348 ioctl_info = (struct ctl_ioctl_info *)arg; 1349 1350 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1351} 1352 1353static void 1354ctl_ioctl_offline(void *arg) 1355{ 1356 struct ctl_ioctl_info *ioctl_info; 1357 1358 ioctl_info = (struct ctl_ioctl_info *)arg; 1359 1360 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1361} 1362 1363/* 1364 * Remove an initiator by port number and initiator ID. 1365 * Returns 0 for success, 1 for failure. 1366 */ 1367int 1368ctl_remove_initiator(int32_t targ_port, uint32_t iid) 1369{ 1370 struct ctl_softc *softc; 1371 1372 softc = control_softc; 1373 1374 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1375 1376 if ((targ_port < 0) 1377 || (targ_port > CTL_MAX_PORTS)) { 1378 printf("%s: invalid port number %d\n", __func__, targ_port); 1379 return (1); 1380 } 1381 if (iid > CTL_MAX_INIT_PER_PORT) { 1382 printf("%s: initiator ID %u > maximun %u!\n", 1383 __func__, iid, CTL_MAX_INIT_PER_PORT); 1384 return (1); 1385 } 1386 1387 mtx_lock(&softc->ctl_lock); 1388 1389 softc->wwpn_iid[targ_port][iid].in_use = 0; 1390 1391 mtx_unlock(&softc->ctl_lock); 1392 1393 return (0); 1394} 1395 1396/* 1397 * Add an initiator to the initiator map. 1398 * Returns 0 for success, 1 for failure. 1399 */ 1400int 1401ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid) 1402{ 1403 struct ctl_softc *softc; 1404 int retval; 1405 1406 softc = control_softc; 1407 1408 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1409 1410 retval = 0; 1411 1412 if ((targ_port < 0) 1413 || (targ_port > CTL_MAX_PORTS)) { 1414 printf("%s: invalid port number %d\n", __func__, targ_port); 1415 return (1); 1416 } 1417 if (iid > CTL_MAX_INIT_PER_PORT) { 1418 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n", 1419 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1420 return (1); 1421 } 1422 1423 mtx_lock(&softc->ctl_lock); 1424 1425 if (softc->wwpn_iid[targ_port][iid].in_use != 0) { 1426 /* 1427 * We don't treat this as an error. 1428 */ 1429 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) { 1430 printf("%s: port %d iid %u WWPN %#jx arrived again?\n", 1431 __func__, targ_port, iid, (uintmax_t)wwpn); 1432 goto bailout; 1433 } 1434 1435 /* 1436 * This is an error, but what do we do about it? The 1437 * driver is telling us we have a new WWPN for this 1438 * initiator ID, so we pretty much need to use it. 1439 */ 1440 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is " 1441 "still at that address\n", __func__, targ_port, iid, 1442 (uintmax_t)wwpn, 1443 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn); 1444 1445 /* 1446 * XXX KDM clear have_ca and ua_pending on each LUN for 1447 * this initiator. 1448 */ 1449 } 1450 softc->wwpn_iid[targ_port][iid].in_use = 1; 1451 softc->wwpn_iid[targ_port][iid].iid = iid; 1452 softc->wwpn_iid[targ_port][iid].wwpn = wwpn; 1453 softc->wwpn_iid[targ_port][iid].port = targ_port; 1454 1455bailout: 1456 1457 mtx_unlock(&softc->ctl_lock); 1458 1459 return (retval); 1460} 1461 1462static int 1463ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1464{ 1465 return (0); 1466} 1467 1468static int 1469ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1470{ 1471 return (0); 1472} 1473 1474/* 1475 * Data movement routine for the CTL ioctl frontend port. 1476 */ 1477static int 1478ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1479{ 1480 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1481 struct ctl_sg_entry ext_entry, kern_entry; 1482 int ext_sglen, ext_sg_entries, kern_sg_entries; 1483 int ext_sg_start, ext_offset; 1484 int len_to_copy, len_copied; 1485 int kern_watermark, ext_watermark; 1486 int ext_sglist_malloced; 1487 int i, j; 1488 1489 ext_sglist_malloced = 0; 1490 ext_sg_start = 0; 1491 ext_offset = 0; 1492 1493 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1494 1495 /* 1496 * If this flag is set, fake the data transfer. 1497 */ 1498 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1499 ctsio->ext_data_filled = ctsio->ext_data_len; 1500 goto bailout; 1501 } 1502 1503 /* 1504 * To simplify things here, if we have a single buffer, stick it in 1505 * a S/G entry and just make it a single entry S/G list. 1506 */ 1507 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1508 int len_seen; 1509 1510 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1511 1512 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1513 M_WAITOK); 1514 ext_sglist_malloced = 1; 1515 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1516 ext_sglen) != 0) { 1517 ctl_set_internal_failure(ctsio, 1518 /*sks_valid*/ 0, 1519 /*retry_count*/ 0); 1520 goto bailout; 1521 } 1522 ext_sg_entries = ctsio->ext_sg_entries; 1523 len_seen = 0; 1524 for (i = 0; i < ext_sg_entries; i++) { 1525 if ((len_seen + ext_sglist[i].len) >= 1526 ctsio->ext_data_filled) { 1527 ext_sg_start = i; 1528 ext_offset = ctsio->ext_data_filled - len_seen; 1529 break; 1530 } 1531 len_seen += ext_sglist[i].len; 1532 } 1533 } else { 1534 ext_sglist = &ext_entry; 1535 ext_sglist->addr = ctsio->ext_data_ptr; 1536 ext_sglist->len = ctsio->ext_data_len; 1537 ext_sg_entries = 1; 1538 ext_sg_start = 0; 1539 ext_offset = ctsio->ext_data_filled; 1540 } 1541 1542 if (ctsio->kern_sg_entries > 0) { 1543 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1544 kern_sg_entries = ctsio->kern_sg_entries; 1545 } else { 1546 kern_sglist = &kern_entry; 1547 kern_sglist->addr = ctsio->kern_data_ptr; 1548 kern_sglist->len = ctsio->kern_data_len; 1549 kern_sg_entries = 1; 1550 } 1551 1552 1553 kern_watermark = 0; 1554 ext_watermark = ext_offset; 1555 len_copied = 0; 1556 for (i = ext_sg_start, j = 0; 1557 i < ext_sg_entries && j < kern_sg_entries;) { 1558 uint8_t *ext_ptr, *kern_ptr; 1559 1560 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1561 kern_sglist[j].len - kern_watermark); 1562 1563 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1564 ext_ptr = ext_ptr + ext_watermark; 1565 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1566 /* 1567 * XXX KDM fix this! 1568 */ 1569 panic("need to implement bus address support"); 1570#if 0 1571 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1572#endif 1573 } else 1574 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1575 kern_ptr = kern_ptr + kern_watermark; 1576 1577 kern_watermark += len_to_copy; 1578 ext_watermark += len_to_copy; 1579 1580 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1581 CTL_FLAG_DATA_IN) { 1582 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1583 "bytes to user\n", len_to_copy)); 1584 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1585 "to %p\n", kern_ptr, ext_ptr)); 1586 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1587 ctl_set_internal_failure(ctsio, 1588 /*sks_valid*/ 0, 1589 /*retry_count*/ 0); 1590 goto bailout; 1591 } 1592 } else { 1593 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1594 "bytes from user\n", len_to_copy)); 1595 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1596 "to %p\n", ext_ptr, kern_ptr)); 1597 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1598 ctl_set_internal_failure(ctsio, 1599 /*sks_valid*/ 0, 1600 /*retry_count*/0); 1601 goto bailout; 1602 } 1603 } 1604 1605 len_copied += len_to_copy; 1606 1607 if (ext_sglist[i].len == ext_watermark) { 1608 i++; 1609 ext_watermark = 0; 1610 } 1611 1612 if (kern_sglist[j].len == kern_watermark) { 1613 j++; 1614 kern_watermark = 0; 1615 } 1616 } 1617 1618 ctsio->ext_data_filled += len_copied; 1619 1620 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1621 "kern_sg_entries: %d\n", ext_sg_entries, 1622 kern_sg_entries)); 1623 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1624 "kern_data_len = %d\n", ctsio->ext_data_len, 1625 ctsio->kern_data_len)); 1626 1627 1628 /* XXX KDM set residual?? */ 1629bailout: 1630 1631 if (ext_sglist_malloced != 0) 1632 free(ext_sglist, M_CTL); 1633 1634 return (CTL_RETVAL_COMPLETE); 1635} 1636 1637/* 1638 * Serialize a command that went down the "wrong" side, and so was sent to 1639 * this controller for execution. The logic is a little different than the 1640 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1641 * sent back to the other side, but in the success case, we execute the 1642 * command on this side (XFER mode) or tell the other side to execute it 1643 * (SER_ONLY mode). 1644 */ 1645static int 1646ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1647{ 1648 struct ctl_softc *ctl_softc; 1649 union ctl_ha_msg msg_info; 1650 struct ctl_lun *lun; 1651 int retval = 0; 1652 uint32_t targ_lun; 1653 1654 ctl_softc = control_softc; 1655 1656 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1657 lun = ctl_softc->ctl_luns[targ_lun]; 1658 if (lun==NULL) 1659 { 1660 /* 1661 * Why isn't LUN defined? The other side wouldn't 1662 * send a cmd if the LUN is undefined. 1663 */ 1664 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1665 1666 /* "Logical unit not supported" */ 1667 ctl_set_sense_data(&msg_info.scsi.sense_data, 1668 lun, 1669 /*sense_format*/SSD_TYPE_NONE, 1670 /*current_error*/ 1, 1671 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1672 /*asc*/ 0x25, 1673 /*ascq*/ 0x00, 1674 SSD_ELEM_NONE); 1675 1676 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1677 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1678 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1679 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1680 msg_info.hdr.serializing_sc = NULL; 1681 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1682 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1683 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1684 } 1685 return(1); 1686 1687 } 1688 1689 mtx_lock(&lun->lun_lock); 1690 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1691 1692 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1693 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1694 ooa_links))) { 1695 case CTL_ACTION_BLOCK: 1696 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1697 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1698 blocked_links); 1699 break; 1700 case CTL_ACTION_PASS: 1701 case CTL_ACTION_SKIP: 1702 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1703 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1704 ctl_enqueue_rtr((union ctl_io *)ctsio); 1705 } else { 1706 1707 /* send msg back to other side */ 1708 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1709 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1710 msg_info.hdr.msg_type = CTL_MSG_R2R; 1711#if 0 1712 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1713#endif 1714 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1715 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1716 } 1717 } 1718 break; 1719 case CTL_ACTION_OVERLAP: 1720 /* OVERLAPPED COMMANDS ATTEMPTED */ 1721 ctl_set_sense_data(&msg_info.scsi.sense_data, 1722 lun, 1723 /*sense_format*/SSD_TYPE_NONE, 1724 /*current_error*/ 1, 1725 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1726 /*asc*/ 0x4E, 1727 /*ascq*/ 0x00, 1728 SSD_ELEM_NONE); 1729 1730 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1731 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1732 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1733 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1734 msg_info.hdr.serializing_sc = NULL; 1735 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1736#if 0 1737 printf("BAD JUJU:Major Bummer Overlap\n"); 1738#endif 1739 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1740 retval = 1; 1741 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1742 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1743 } 1744 break; 1745 case CTL_ACTION_OVERLAP_TAG: 1746 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1747 ctl_set_sense_data(&msg_info.scsi.sense_data, 1748 lun, 1749 /*sense_format*/SSD_TYPE_NONE, 1750 /*current_error*/ 1, 1751 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1752 /*asc*/ 0x4D, 1753 /*ascq*/ ctsio->tag_num & 0xff, 1754 SSD_ELEM_NONE); 1755 1756 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1757 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1758 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1759 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1760 msg_info.hdr.serializing_sc = NULL; 1761 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1762#if 0 1763 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1764#endif 1765 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1766 retval = 1; 1767 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1768 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1769 } 1770 break; 1771 case CTL_ACTION_ERROR: 1772 default: 1773 /* "Internal target failure" */ 1774 ctl_set_sense_data(&msg_info.scsi.sense_data, 1775 lun, 1776 /*sense_format*/SSD_TYPE_NONE, 1777 /*current_error*/ 1, 1778 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1779 /*asc*/ 0x44, 1780 /*ascq*/ 0x00, 1781 SSD_ELEM_NONE); 1782 1783 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1784 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1785 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1786 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1787 msg_info.hdr.serializing_sc = NULL; 1788 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1789#if 0 1790 printf("BAD JUJU:Major Bummer HW Error\n"); 1791#endif 1792 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1793 retval = 1; 1794 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1795 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1796 } 1797 break; 1798 } 1799 mtx_unlock(&lun->lun_lock); 1800 return (retval); 1801} 1802 1803static int 1804ctl_ioctl_submit_wait(union ctl_io *io) 1805{ 1806 struct ctl_fe_ioctl_params params; 1807 ctl_fe_ioctl_state last_state; 1808 int done, retval; 1809 1810 retval = 0; 1811 1812 bzero(¶ms, sizeof(params)); 1813 1814 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1815 cv_init(¶ms.sem, "ctlioccv"); 1816 params.state = CTL_IOCTL_INPROG; 1817 last_state = params.state; 1818 1819 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1820 1821 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1822 1823 /* This shouldn't happen */ 1824 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1825 return (retval); 1826 1827 done = 0; 1828 1829 do { 1830 mtx_lock(¶ms.ioctl_mtx); 1831 /* 1832 * Check the state here, and don't sleep if the state has 1833 * already changed (i.e. wakeup has already occured, but we 1834 * weren't waiting yet). 1835 */ 1836 if (params.state == last_state) { 1837 /* XXX KDM cv_wait_sig instead? */ 1838 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1839 } 1840 last_state = params.state; 1841 1842 switch (params.state) { 1843 case CTL_IOCTL_INPROG: 1844 /* Why did we wake up? */ 1845 /* XXX KDM error here? */ 1846 mtx_unlock(¶ms.ioctl_mtx); 1847 break; 1848 case CTL_IOCTL_DATAMOVE: 1849 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1850 1851 /* 1852 * change last_state back to INPROG to avoid 1853 * deadlock on subsequent data moves. 1854 */ 1855 params.state = last_state = CTL_IOCTL_INPROG; 1856 1857 mtx_unlock(¶ms.ioctl_mtx); 1858 ctl_ioctl_do_datamove(&io->scsiio); 1859 /* 1860 * Note that in some cases, most notably writes, 1861 * this will queue the I/O and call us back later. 1862 * In other cases, generally reads, this routine 1863 * will immediately call back and wake us up, 1864 * probably using our own context. 1865 */ 1866 io->scsiio.be_move_done(io); 1867 break; 1868 case CTL_IOCTL_DONE: 1869 mtx_unlock(¶ms.ioctl_mtx); 1870 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1871 done = 1; 1872 break; 1873 default: 1874 mtx_unlock(¶ms.ioctl_mtx); 1875 /* XXX KDM error here? */ 1876 break; 1877 } 1878 } while (done == 0); 1879 1880 mtx_destroy(¶ms.ioctl_mtx); 1881 cv_destroy(¶ms.sem); 1882 1883 return (CTL_RETVAL_COMPLETE); 1884} 1885 1886static void 1887ctl_ioctl_datamove(union ctl_io *io) 1888{ 1889 struct ctl_fe_ioctl_params *params; 1890 1891 params = (struct ctl_fe_ioctl_params *) 1892 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1893 1894 mtx_lock(¶ms->ioctl_mtx); 1895 params->state = CTL_IOCTL_DATAMOVE; 1896 cv_broadcast(¶ms->sem); 1897 mtx_unlock(¶ms->ioctl_mtx); 1898} 1899 1900static void 1901ctl_ioctl_done(union ctl_io *io) 1902{ 1903 struct ctl_fe_ioctl_params *params; 1904 1905 params = (struct ctl_fe_ioctl_params *) 1906 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1907 1908 mtx_lock(¶ms->ioctl_mtx); 1909 params->state = CTL_IOCTL_DONE; 1910 cv_broadcast(¶ms->sem); 1911 mtx_unlock(¶ms->ioctl_mtx); 1912} 1913 1914static void 1915ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 1916{ 1917 struct ctl_fe_ioctl_startstop_info *sd_info; 1918 1919 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 1920 1921 sd_info->hs_info.status = metatask->status; 1922 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 1923 sd_info->hs_info.luns_complete = 1924 metatask->taskinfo.startstop.luns_complete; 1925 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 1926 1927 cv_broadcast(&sd_info->sem); 1928} 1929 1930static void 1931ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 1932{ 1933 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 1934 1935 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 1936 1937 mtx_lock(fe_bbr_info->lock); 1938 fe_bbr_info->bbr_info->status = metatask->status; 1939 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 1940 fe_bbr_info->wakeup_done = 1; 1941 mtx_unlock(fe_bbr_info->lock); 1942 1943 cv_broadcast(&fe_bbr_info->sem); 1944} 1945 1946/* 1947 * Returns 0 for success, errno for failure. 1948 */ 1949static int 1950ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1951 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1952{ 1953 union ctl_io *io; 1954 int retval; 1955 1956 retval = 0; 1957 1958 mtx_lock(&lun->lun_lock); 1959 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 1960 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 1961 ooa_links)) { 1962 struct ctl_ooa_entry *entry; 1963 1964 /* 1965 * If we've got more than we can fit, just count the 1966 * remaining entries. 1967 */ 1968 if (*cur_fill_num >= ooa_hdr->alloc_num) 1969 continue; 1970 1971 entry = &kern_entries[*cur_fill_num]; 1972 1973 entry->tag_num = io->scsiio.tag_num; 1974 entry->lun_num = lun->lun; 1975#ifdef CTL_TIME_IO 1976 entry->start_bt = io->io_hdr.start_bt; 1977#endif 1978 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 1979 entry->cdb_len = io->scsiio.cdb_len; 1980 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 1981 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 1982 1983 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 1984 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 1985 1986 if (io->io_hdr.flags & CTL_FLAG_ABORT) 1987 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 1988 1989 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 1990 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 1991 1992 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 1993 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 1994 } 1995 mtx_unlock(&lun->lun_lock); 1996 1997 return (retval); 1998} 1999 2000static void * 2001ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2002 size_t error_str_len) 2003{ 2004 void *kptr; 2005 2006 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2007 2008 if (copyin(user_addr, kptr, len) != 0) { 2009 snprintf(error_str, error_str_len, "Error copying %d bytes " 2010 "from user address %p to kernel address %p", len, 2011 user_addr, kptr); 2012 free(kptr, M_CTL); 2013 return (NULL); 2014 } 2015 2016 return (kptr); 2017} 2018 2019static void 2020ctl_free_args(int num_args, struct ctl_be_arg *args) 2021{ 2022 int i; 2023 2024 if (args == NULL) 2025 return; 2026 2027 for (i = 0; i < num_args; i++) { 2028 free(args[i].kname, M_CTL); 2029 free(args[i].kvalue, M_CTL); 2030 } 2031 2032 free(args, M_CTL); 2033} 2034 2035static struct ctl_be_arg * 2036ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2037 char *error_str, size_t error_str_len) 2038{ 2039 struct ctl_be_arg *args; 2040 int i; 2041 2042 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2043 error_str, error_str_len); 2044 2045 if (args == NULL) 2046 goto bailout; 2047 2048 for (i = 0; i < num_args; i++) { 2049 args[i].kname = NULL; 2050 args[i].kvalue = NULL; 2051 } 2052 2053 for (i = 0; i < num_args; i++) { 2054 uint8_t *tmpptr; 2055 2056 args[i].kname = ctl_copyin_alloc(args[i].name, 2057 args[i].namelen, error_str, error_str_len); 2058 if (args[i].kname == NULL) 2059 goto bailout; 2060 2061 if (args[i].kname[args[i].namelen - 1] != '\0') { 2062 snprintf(error_str, error_str_len, "Argument %d " 2063 "name is not NUL-terminated", i); 2064 goto bailout; 2065 } 2066 2067 if (args[i].flags & CTL_BEARG_RD) { 2068 tmpptr = ctl_copyin_alloc(args[i].value, 2069 args[i].vallen, error_str, error_str_len); 2070 if (tmpptr == NULL) 2071 goto bailout; 2072 if ((args[i].flags & CTL_BEARG_ASCII) 2073 && (tmpptr[args[i].vallen - 1] != '\0')) { 2074 snprintf(error_str, error_str_len, "Argument " 2075 "%d value is not NUL-terminated", i); 2076 goto bailout; 2077 } 2078 args[i].kvalue = tmpptr; 2079 } else { 2080 args[i].kvalue = malloc(args[i].vallen, 2081 M_CTL, M_WAITOK | M_ZERO); 2082 } 2083 } 2084 2085 return (args); 2086bailout: 2087 2088 ctl_free_args(num_args, args); 2089 2090 return (NULL); 2091} 2092 2093static void 2094ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2095{ 2096 int i; 2097 2098 for (i = 0; i < num_args; i++) { 2099 if (args[i].flags & CTL_BEARG_WR) 2100 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2101 } 2102} 2103 2104/* 2105 * Escape characters that are illegal or not recommended in XML. 2106 */ 2107int 2108ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2109{ 2110 int retval; 2111 2112 retval = 0; 2113 2114 for (; *str; str++) { 2115 switch (*str) { 2116 case '&': 2117 retval = sbuf_printf(sb, "&"); 2118 break; 2119 case '>': 2120 retval = sbuf_printf(sb, ">"); 2121 break; 2122 case '<': 2123 retval = sbuf_printf(sb, "<"); 2124 break; 2125 default: 2126 retval = sbuf_putc(sb, *str); 2127 break; 2128 } 2129 2130 if (retval != 0) 2131 break; 2132 2133 } 2134 2135 return (retval); 2136} 2137 2138static int 2139ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2140 struct thread *td) 2141{ 2142 struct ctl_softc *softc; 2143 int retval; 2144 2145 softc = control_softc; 2146 2147 retval = 0; 2148 2149 switch (cmd) { 2150 case CTL_IO: { 2151 union ctl_io *io; 2152 void *pool_tmp; 2153 2154 /* 2155 * If we haven't been "enabled", don't allow any SCSI I/O 2156 * to this FETD. 2157 */ 2158 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2159 retval = EPERM; 2160 break; 2161 } 2162 2163 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2164 if (io == NULL) { 2165 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2166 retval = ENOSPC; 2167 break; 2168 } 2169 2170 /* 2171 * Need to save the pool reference so it doesn't get 2172 * spammed by the user's ctl_io. 2173 */ 2174 pool_tmp = io->io_hdr.pool; 2175 2176 memcpy(io, (void *)addr, sizeof(*io)); 2177 2178 io->io_hdr.pool = pool_tmp; 2179 /* 2180 * No status yet, so make sure the status is set properly. 2181 */ 2182 io->io_hdr.status = CTL_STATUS_NONE; 2183 2184 /* 2185 * The user sets the initiator ID, target and LUN IDs. 2186 */ 2187 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2188 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2189 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2190 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2191 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2192 2193 retval = ctl_ioctl_submit_wait(io); 2194 2195 if (retval != 0) { 2196 ctl_free_io(io); 2197 break; 2198 } 2199 2200 memcpy((void *)addr, io, sizeof(*io)); 2201 2202 /* return this to our pool */ 2203 ctl_free_io(io); 2204 2205 break; 2206 } 2207 case CTL_ENABLE_PORT: 2208 case CTL_DISABLE_PORT: 2209 case CTL_SET_PORT_WWNS: { 2210 struct ctl_port *port; 2211 struct ctl_port_entry *entry; 2212 2213 entry = (struct ctl_port_entry *)addr; 2214 2215 mtx_lock(&softc->ctl_lock); 2216 STAILQ_FOREACH(port, &softc->port_list, links) { 2217 int action, done; 2218 2219 action = 0; 2220 done = 0; 2221 2222 if ((entry->port_type == CTL_PORT_NONE) 2223 && (entry->targ_port == port->targ_port)) { 2224 /* 2225 * If the user only wants to enable or 2226 * disable or set WWNs on a specific port, 2227 * do the operation and we're done. 2228 */ 2229 action = 1; 2230 done = 1; 2231 } else if (entry->port_type & port->port_type) { 2232 /* 2233 * Compare the user's type mask with the 2234 * particular frontend type to see if we 2235 * have a match. 2236 */ 2237 action = 1; 2238 done = 0; 2239 2240 /* 2241 * Make sure the user isn't trying to set 2242 * WWNs on multiple ports at the same time. 2243 */ 2244 if (cmd == CTL_SET_PORT_WWNS) { 2245 printf("%s: Can't set WWNs on " 2246 "multiple ports\n", __func__); 2247 retval = EINVAL; 2248 break; 2249 } 2250 } 2251 if (action != 0) { 2252 /* 2253 * XXX KDM we have to drop the lock here, 2254 * because the online/offline operations 2255 * can potentially block. We need to 2256 * reference count the frontends so they 2257 * can't go away, 2258 */ 2259 mtx_unlock(&softc->ctl_lock); 2260 2261 if (cmd == CTL_ENABLE_PORT) { 2262 struct ctl_lun *lun; 2263 2264 STAILQ_FOREACH(lun, &softc->lun_list, 2265 links) { 2266 port->lun_enable(port->targ_lun_arg, 2267 lun->target, 2268 lun->lun); 2269 } 2270 2271 ctl_port_online(port); 2272 } else if (cmd == CTL_DISABLE_PORT) { 2273 struct ctl_lun *lun; 2274 2275 ctl_port_offline(port); 2276 2277 STAILQ_FOREACH(lun, &softc->lun_list, 2278 links) { 2279 port->lun_disable( 2280 port->targ_lun_arg, 2281 lun->target, 2282 lun->lun); 2283 } 2284 } 2285 2286 mtx_lock(&softc->ctl_lock); 2287 2288 if (cmd == CTL_SET_PORT_WWNS) 2289 ctl_port_set_wwns(port, 2290 (entry->flags & CTL_PORT_WWNN_VALID) ? 2291 1 : 0, entry->wwnn, 2292 (entry->flags & CTL_PORT_WWPN_VALID) ? 2293 1 : 0, entry->wwpn); 2294 } 2295 if (done != 0) 2296 break; 2297 } 2298 mtx_unlock(&softc->ctl_lock); 2299 break; 2300 } 2301 case CTL_GET_PORT_LIST: { 2302 struct ctl_port *port; 2303 struct ctl_port_list *list; 2304 int i; 2305 2306 list = (struct ctl_port_list *)addr; 2307 2308 if (list->alloc_len != (list->alloc_num * 2309 sizeof(struct ctl_port_entry))) { 2310 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2311 "alloc_num %u * sizeof(struct ctl_port_entry) " 2312 "%zu\n", __func__, list->alloc_len, 2313 list->alloc_num, sizeof(struct ctl_port_entry)); 2314 retval = EINVAL; 2315 break; 2316 } 2317 list->fill_len = 0; 2318 list->fill_num = 0; 2319 list->dropped_num = 0; 2320 i = 0; 2321 mtx_lock(&softc->ctl_lock); 2322 STAILQ_FOREACH(port, &softc->port_list, links) { 2323 struct ctl_port_entry entry, *list_entry; 2324 2325 if (list->fill_num >= list->alloc_num) { 2326 list->dropped_num++; 2327 continue; 2328 } 2329 2330 entry.port_type = port->port_type; 2331 strlcpy(entry.port_name, port->port_name, 2332 sizeof(entry.port_name)); 2333 entry.targ_port = port->targ_port; 2334 entry.physical_port = port->physical_port; 2335 entry.virtual_port = port->virtual_port; 2336 entry.wwnn = port->wwnn; 2337 entry.wwpn = port->wwpn; 2338 if (port->status & CTL_PORT_STATUS_ONLINE) 2339 entry.online = 1; 2340 else 2341 entry.online = 0; 2342 2343 list_entry = &list->entries[i]; 2344 2345 retval = copyout(&entry, list_entry, sizeof(entry)); 2346 if (retval != 0) { 2347 printf("%s: CTL_GET_PORT_LIST: copyout " 2348 "returned %d\n", __func__, retval); 2349 break; 2350 } 2351 i++; 2352 list->fill_num++; 2353 list->fill_len += sizeof(entry); 2354 } 2355 mtx_unlock(&softc->ctl_lock); 2356 2357 /* 2358 * If this is non-zero, we had a copyout fault, so there's 2359 * probably no point in attempting to set the status inside 2360 * the structure. 2361 */ 2362 if (retval != 0) 2363 break; 2364 2365 if (list->dropped_num > 0) 2366 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2367 else 2368 list->status = CTL_PORT_LIST_OK; 2369 break; 2370 } 2371 case CTL_DUMP_OOA: { 2372 struct ctl_lun *lun; 2373 union ctl_io *io; 2374 char printbuf[128]; 2375 struct sbuf sb; 2376 2377 mtx_lock(&softc->ctl_lock); 2378 printf("Dumping OOA queues:\n"); 2379 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2380 mtx_lock(&lun->lun_lock); 2381 for (io = (union ctl_io *)TAILQ_FIRST( 2382 &lun->ooa_queue); io != NULL; 2383 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2384 ooa_links)) { 2385 sbuf_new(&sb, printbuf, sizeof(printbuf), 2386 SBUF_FIXEDLEN); 2387 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2388 (intmax_t)lun->lun, 2389 io->scsiio.tag_num, 2390 (io->io_hdr.flags & 2391 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2392 (io->io_hdr.flags & 2393 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2394 (io->io_hdr.flags & 2395 CTL_FLAG_ABORT) ? " ABORT" : "", 2396 (io->io_hdr.flags & 2397 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2398 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2399 sbuf_finish(&sb); 2400 printf("%s\n", sbuf_data(&sb)); 2401 } 2402 mtx_unlock(&lun->lun_lock); 2403 } 2404 printf("OOA queues dump done\n"); 2405 mtx_unlock(&softc->ctl_lock); 2406 break; 2407 } 2408 case CTL_GET_OOA: { 2409 struct ctl_lun *lun; 2410 struct ctl_ooa *ooa_hdr; 2411 struct ctl_ooa_entry *entries; 2412 uint32_t cur_fill_num; 2413 2414 ooa_hdr = (struct ctl_ooa *)addr; 2415 2416 if ((ooa_hdr->alloc_len == 0) 2417 || (ooa_hdr->alloc_num == 0)) { 2418 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2419 "must be non-zero\n", __func__, 2420 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2421 retval = EINVAL; 2422 break; 2423 } 2424 2425 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2426 sizeof(struct ctl_ooa_entry))) { 2427 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2428 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2429 __func__, ooa_hdr->alloc_len, 2430 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2431 retval = EINVAL; 2432 break; 2433 } 2434 2435 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2436 if (entries == NULL) { 2437 printf("%s: could not allocate %d bytes for OOA " 2438 "dump\n", __func__, ooa_hdr->alloc_len); 2439 retval = ENOMEM; 2440 break; 2441 } 2442 2443 mtx_lock(&softc->ctl_lock); 2444 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2445 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2446 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2447 mtx_unlock(&softc->ctl_lock); 2448 free(entries, M_CTL); 2449 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2450 __func__, (uintmax_t)ooa_hdr->lun_num); 2451 retval = EINVAL; 2452 break; 2453 } 2454 2455 cur_fill_num = 0; 2456 2457 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2458 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2459 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2460 ooa_hdr, entries); 2461 if (retval != 0) 2462 break; 2463 } 2464 if (retval != 0) { 2465 mtx_unlock(&softc->ctl_lock); 2466 free(entries, M_CTL); 2467 break; 2468 } 2469 } else { 2470 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2471 2472 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2473 entries); 2474 } 2475 mtx_unlock(&softc->ctl_lock); 2476 2477 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2478 ooa_hdr->fill_len = ooa_hdr->fill_num * 2479 sizeof(struct ctl_ooa_entry); 2480 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2481 if (retval != 0) { 2482 printf("%s: error copying out %d bytes for OOA dump\n", 2483 __func__, ooa_hdr->fill_len); 2484 } 2485 2486 getbintime(&ooa_hdr->cur_bt); 2487 2488 if (cur_fill_num > ooa_hdr->alloc_num) { 2489 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2490 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2491 } else { 2492 ooa_hdr->dropped_num = 0; 2493 ooa_hdr->status = CTL_OOA_OK; 2494 } 2495 2496 free(entries, M_CTL); 2497 break; 2498 } 2499 case CTL_CHECK_OOA: { 2500 union ctl_io *io; 2501 struct ctl_lun *lun; 2502 struct ctl_ooa_info *ooa_info; 2503 2504 2505 ooa_info = (struct ctl_ooa_info *)addr; 2506 2507 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2508 ooa_info->status = CTL_OOA_INVALID_LUN; 2509 break; 2510 } 2511 mtx_lock(&softc->ctl_lock); 2512 lun = softc->ctl_luns[ooa_info->lun_id]; 2513 if (lun == NULL) { 2514 mtx_unlock(&softc->ctl_lock); 2515 ooa_info->status = CTL_OOA_INVALID_LUN; 2516 break; 2517 } 2518 mtx_lock(&lun->lun_lock); 2519 mtx_unlock(&softc->ctl_lock); 2520 ooa_info->num_entries = 0; 2521 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2522 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2523 &io->io_hdr, ooa_links)) { 2524 ooa_info->num_entries++; 2525 } 2526 mtx_unlock(&lun->lun_lock); 2527 2528 ooa_info->status = CTL_OOA_SUCCESS; 2529 2530 break; 2531 } 2532 case CTL_HARD_START: 2533 case CTL_HARD_STOP: { 2534 struct ctl_fe_ioctl_startstop_info ss_info; 2535 struct cfi_metatask *metatask; 2536 struct mtx hs_mtx; 2537 2538 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2539 2540 cv_init(&ss_info.sem, "hard start/stop cv" ); 2541 2542 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2543 if (metatask == NULL) { 2544 retval = ENOMEM; 2545 mtx_destroy(&hs_mtx); 2546 break; 2547 } 2548 2549 if (cmd == CTL_HARD_START) 2550 metatask->tasktype = CFI_TASK_STARTUP; 2551 else 2552 metatask->tasktype = CFI_TASK_SHUTDOWN; 2553 2554 metatask->callback = ctl_ioctl_hard_startstop_callback; 2555 metatask->callback_arg = &ss_info; 2556 2557 cfi_action(metatask); 2558 2559 /* Wait for the callback */ 2560 mtx_lock(&hs_mtx); 2561 cv_wait_sig(&ss_info.sem, &hs_mtx); 2562 mtx_unlock(&hs_mtx); 2563 2564 /* 2565 * All information has been copied from the metatask by the 2566 * time cv_broadcast() is called, so we free the metatask here. 2567 */ 2568 cfi_free_metatask(metatask); 2569 2570 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2571 2572 mtx_destroy(&hs_mtx); 2573 break; 2574 } 2575 case CTL_BBRREAD: { 2576 struct ctl_bbrread_info *bbr_info; 2577 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2578 struct mtx bbr_mtx; 2579 struct cfi_metatask *metatask; 2580 2581 bbr_info = (struct ctl_bbrread_info *)addr; 2582 2583 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2584 2585 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2586 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2587 2588 fe_bbr_info.bbr_info = bbr_info; 2589 fe_bbr_info.lock = &bbr_mtx; 2590 2591 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2592 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2593 2594 if (metatask == NULL) { 2595 mtx_destroy(&bbr_mtx); 2596 cv_destroy(&fe_bbr_info.sem); 2597 retval = ENOMEM; 2598 break; 2599 } 2600 metatask->tasktype = CFI_TASK_BBRREAD; 2601 metatask->callback = ctl_ioctl_bbrread_callback; 2602 metatask->callback_arg = &fe_bbr_info; 2603 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2604 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2605 metatask->taskinfo.bbrread.len = bbr_info->len; 2606 2607 cfi_action(metatask); 2608 2609 mtx_lock(&bbr_mtx); 2610 while (fe_bbr_info.wakeup_done == 0) 2611 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2612 mtx_unlock(&bbr_mtx); 2613 2614 bbr_info->status = metatask->status; 2615 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2616 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2617 memcpy(&bbr_info->sense_data, 2618 &metatask->taskinfo.bbrread.sense_data, 2619 ctl_min(sizeof(bbr_info->sense_data), 2620 sizeof(metatask->taskinfo.bbrread.sense_data))); 2621 2622 cfi_free_metatask(metatask); 2623 2624 mtx_destroy(&bbr_mtx); 2625 cv_destroy(&fe_bbr_info.sem); 2626 2627 break; 2628 } 2629 case CTL_DELAY_IO: { 2630 struct ctl_io_delay_info *delay_info; 2631#ifdef CTL_IO_DELAY 2632 struct ctl_lun *lun; 2633#endif /* CTL_IO_DELAY */ 2634 2635 delay_info = (struct ctl_io_delay_info *)addr; 2636 2637#ifdef CTL_IO_DELAY 2638 mtx_lock(&softc->ctl_lock); 2639 2640 if ((delay_info->lun_id > CTL_MAX_LUNS) 2641 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2642 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2643 } else { 2644 lun = softc->ctl_luns[delay_info->lun_id]; 2645 mtx_lock(&lun->lun_lock); 2646 2647 delay_info->status = CTL_DELAY_STATUS_OK; 2648 2649 switch (delay_info->delay_type) { 2650 case CTL_DELAY_TYPE_CONT: 2651 break; 2652 case CTL_DELAY_TYPE_ONESHOT: 2653 break; 2654 default: 2655 delay_info->status = 2656 CTL_DELAY_STATUS_INVALID_TYPE; 2657 break; 2658 } 2659 2660 switch (delay_info->delay_loc) { 2661 case CTL_DELAY_LOC_DATAMOVE: 2662 lun->delay_info.datamove_type = 2663 delay_info->delay_type; 2664 lun->delay_info.datamove_delay = 2665 delay_info->delay_secs; 2666 break; 2667 case CTL_DELAY_LOC_DONE: 2668 lun->delay_info.done_type = 2669 delay_info->delay_type; 2670 lun->delay_info.done_delay = 2671 delay_info->delay_secs; 2672 break; 2673 default: 2674 delay_info->status = 2675 CTL_DELAY_STATUS_INVALID_LOC; 2676 break; 2677 } 2678 mtx_unlock(&lun->lun_lock); 2679 } 2680 2681 mtx_unlock(&softc->ctl_lock); 2682#else 2683 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2684#endif /* CTL_IO_DELAY */ 2685 break; 2686 } 2687 case CTL_REALSYNC_SET: { 2688 int *syncstate; 2689 2690 syncstate = (int *)addr; 2691 2692 mtx_lock(&softc->ctl_lock); 2693 switch (*syncstate) { 2694 case 0: 2695 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2696 break; 2697 case 1: 2698 softc->flags |= CTL_FLAG_REAL_SYNC; 2699 break; 2700 default: 2701 retval = EINVAL; 2702 break; 2703 } 2704 mtx_unlock(&softc->ctl_lock); 2705 break; 2706 } 2707 case CTL_REALSYNC_GET: { 2708 int *syncstate; 2709 2710 syncstate = (int*)addr; 2711 2712 mtx_lock(&softc->ctl_lock); 2713 if (softc->flags & CTL_FLAG_REAL_SYNC) 2714 *syncstate = 1; 2715 else 2716 *syncstate = 0; 2717 mtx_unlock(&softc->ctl_lock); 2718 2719 break; 2720 } 2721 case CTL_SETSYNC: 2722 case CTL_GETSYNC: { 2723 struct ctl_sync_info *sync_info; 2724 struct ctl_lun *lun; 2725 2726 sync_info = (struct ctl_sync_info *)addr; 2727 2728 mtx_lock(&softc->ctl_lock); 2729 lun = softc->ctl_luns[sync_info->lun_id]; 2730 if (lun == NULL) { 2731 mtx_unlock(&softc->ctl_lock); 2732 sync_info->status = CTL_GS_SYNC_NO_LUN; 2733 } 2734 /* 2735 * Get or set the sync interval. We're not bounds checking 2736 * in the set case, hopefully the user won't do something 2737 * silly. 2738 */ 2739 mtx_lock(&lun->lun_lock); 2740 mtx_unlock(&softc->ctl_lock); 2741 if (cmd == CTL_GETSYNC) 2742 sync_info->sync_interval = lun->sync_interval; 2743 else 2744 lun->sync_interval = sync_info->sync_interval; 2745 mtx_unlock(&lun->lun_lock); 2746 2747 sync_info->status = CTL_GS_SYNC_OK; 2748 2749 break; 2750 } 2751 case CTL_GETSTATS: { 2752 struct ctl_stats *stats; 2753 struct ctl_lun *lun; 2754 int i; 2755 2756 stats = (struct ctl_stats *)addr; 2757 2758 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2759 stats->alloc_len) { 2760 stats->status = CTL_SS_NEED_MORE_SPACE; 2761 stats->num_luns = softc->num_luns; 2762 break; 2763 } 2764 /* 2765 * XXX KDM no locking here. If the LUN list changes, 2766 * things can blow up. 2767 */ 2768 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2769 i++, lun = STAILQ_NEXT(lun, links)) { 2770 retval = copyout(&lun->stats, &stats->lun_stats[i], 2771 sizeof(lun->stats)); 2772 if (retval != 0) 2773 break; 2774 } 2775 stats->num_luns = softc->num_luns; 2776 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2777 softc->num_luns; 2778 stats->status = CTL_SS_OK; 2779#ifdef CTL_TIME_IO 2780 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2781#else 2782 stats->flags = CTL_STATS_FLAG_NONE; 2783#endif 2784 getnanouptime(&stats->timestamp); 2785 break; 2786 } 2787 case CTL_ERROR_INJECT: { 2788 struct ctl_error_desc *err_desc, *new_err_desc; 2789 struct ctl_lun *lun; 2790 2791 err_desc = (struct ctl_error_desc *)addr; 2792 2793 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2794 M_WAITOK | M_ZERO); 2795 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2796 2797 mtx_lock(&softc->ctl_lock); 2798 lun = softc->ctl_luns[err_desc->lun_id]; 2799 if (lun == NULL) { 2800 mtx_unlock(&softc->ctl_lock); 2801 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2802 __func__, (uintmax_t)err_desc->lun_id); 2803 retval = EINVAL; 2804 break; 2805 } 2806 mtx_lock(&lun->lun_lock); 2807 mtx_unlock(&softc->ctl_lock); 2808 2809 /* 2810 * We could do some checking here to verify the validity 2811 * of the request, but given the complexity of error 2812 * injection requests, the checking logic would be fairly 2813 * complex. 2814 * 2815 * For now, if the request is invalid, it just won't get 2816 * executed and might get deleted. 2817 */ 2818 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2819 2820 /* 2821 * XXX KDM check to make sure the serial number is unique, 2822 * in case we somehow manage to wrap. That shouldn't 2823 * happen for a very long time, but it's the right thing to 2824 * do. 2825 */ 2826 new_err_desc->serial = lun->error_serial; 2827 err_desc->serial = lun->error_serial; 2828 lun->error_serial++; 2829 2830 mtx_unlock(&lun->lun_lock); 2831 break; 2832 } 2833 case CTL_ERROR_INJECT_DELETE: { 2834 struct ctl_error_desc *delete_desc, *desc, *desc2; 2835 struct ctl_lun *lun; 2836 int delete_done; 2837 2838 delete_desc = (struct ctl_error_desc *)addr; 2839 delete_done = 0; 2840 2841 mtx_lock(&softc->ctl_lock); 2842 lun = softc->ctl_luns[delete_desc->lun_id]; 2843 if (lun == NULL) { 2844 mtx_unlock(&softc->ctl_lock); 2845 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2846 __func__, (uintmax_t)delete_desc->lun_id); 2847 retval = EINVAL; 2848 break; 2849 } 2850 mtx_lock(&lun->lun_lock); 2851 mtx_unlock(&softc->ctl_lock); 2852 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2853 if (desc->serial != delete_desc->serial) 2854 continue; 2855 2856 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2857 links); 2858 free(desc, M_CTL); 2859 delete_done = 1; 2860 } 2861 mtx_unlock(&lun->lun_lock); 2862 if (delete_done == 0) { 2863 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2864 "error serial %ju on LUN %u\n", __func__, 2865 delete_desc->serial, delete_desc->lun_id); 2866 retval = EINVAL; 2867 break; 2868 } 2869 break; 2870 } 2871 case CTL_DUMP_STRUCTS: { 2872 int i, j, k; 2873 struct ctl_port *port; 2874 struct ctl_frontend *fe; 2875 2876 printf("CTL IID to WWPN map start:\n"); 2877 for (i = 0; i < CTL_MAX_PORTS; i++) { 2878 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2879 if (softc->wwpn_iid[i][j].in_use == 0) 2880 continue; 2881 2882 printf("port %d iid %u WWPN %#jx\n", 2883 softc->wwpn_iid[i][j].port, 2884 softc->wwpn_iid[i][j].iid, 2885 (uintmax_t)softc->wwpn_iid[i][j].wwpn); 2886 } 2887 } 2888 printf("CTL IID to WWPN map end\n"); 2889 printf("CTL Persistent Reservation information start:\n"); 2890 for (i = 0; i < CTL_MAX_LUNS; i++) { 2891 struct ctl_lun *lun; 2892 2893 lun = softc->ctl_luns[i]; 2894 2895 if ((lun == NULL) 2896 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2897 continue; 2898 2899 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2900 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2901 if (lun->per_res[j+k].registered == 0) 2902 continue; 2903 printf("LUN %d port %d iid %d key " 2904 "%#jx\n", i, j, k, 2905 (uintmax_t)scsi_8btou64( 2906 lun->per_res[j+k].res_key.key)); 2907 } 2908 } 2909 } 2910 printf("CTL Persistent Reservation information end\n"); 2911 printf("CTL Ports:\n"); 2912 /* 2913 * XXX KDM calling this without a lock. We'd likely want 2914 * to drop the lock before calling the frontend's dump 2915 * routine anyway. 2916 */ 2917 STAILQ_FOREACH(port, &softc->port_list, links) { 2918 printf("Port %s Frontend %s Type %u pport %d vport %d WWNN " 2919 "%#jx WWPN %#jx\n", port->port_name, 2920 port->frontend->name, port->port_type, 2921 port->physical_port, port->virtual_port, 2922 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2923 } 2924 printf("CTL Port information end\n"); 2925 printf("CTL Frontends:\n"); 2926 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2927 printf("Frontend %s\n", fe->name); 2928 if (fe->fe_dump != NULL) 2929 fe->fe_dump(); 2930 } 2931 printf("CTL Frontend information end\n"); 2932 break; 2933 } 2934 case CTL_LUN_REQ: { 2935 struct ctl_lun_req *lun_req; 2936 struct ctl_backend_driver *backend; 2937 2938 lun_req = (struct ctl_lun_req *)addr; 2939 2940 backend = ctl_backend_find(lun_req->backend); 2941 if (backend == NULL) { 2942 lun_req->status = CTL_LUN_ERROR; 2943 snprintf(lun_req->error_str, 2944 sizeof(lun_req->error_str), 2945 "Backend \"%s\" not found.", 2946 lun_req->backend); 2947 break; 2948 } 2949 if (lun_req->num_be_args > 0) { 2950 lun_req->kern_be_args = ctl_copyin_args( 2951 lun_req->num_be_args, 2952 lun_req->be_args, 2953 lun_req->error_str, 2954 sizeof(lun_req->error_str)); 2955 if (lun_req->kern_be_args == NULL) { 2956 lun_req->status = CTL_LUN_ERROR; 2957 break; 2958 } 2959 } 2960 2961 retval = backend->ioctl(dev, cmd, addr, flag, td); 2962 2963 if (lun_req->num_be_args > 0) { 2964 ctl_copyout_args(lun_req->num_be_args, 2965 lun_req->kern_be_args); 2966 ctl_free_args(lun_req->num_be_args, 2967 lun_req->kern_be_args); 2968 } 2969 break; 2970 } 2971 case CTL_LUN_LIST: { 2972 struct sbuf *sb; 2973 struct ctl_lun *lun; 2974 struct ctl_lun_list *list; 2975 struct ctl_option *opt; 2976 2977 list = (struct ctl_lun_list *)addr; 2978 2979 /* 2980 * Allocate a fixed length sbuf here, based on the length 2981 * of the user's buffer. We could allocate an auto-extending 2982 * buffer, and then tell the user how much larger our 2983 * amount of data is than his buffer, but that presents 2984 * some problems: 2985 * 2986 * 1. The sbuf(9) routines use a blocking malloc, and so 2987 * we can't hold a lock while calling them with an 2988 * auto-extending buffer. 2989 * 2990 * 2. There is not currently a LUN reference counting 2991 * mechanism, outside of outstanding transactions on 2992 * the LUN's OOA queue. So a LUN could go away on us 2993 * while we're getting the LUN number, backend-specific 2994 * information, etc. Thus, given the way things 2995 * currently work, we need to hold the CTL lock while 2996 * grabbing LUN information. 2997 * 2998 * So, from the user's standpoint, the best thing to do is 2999 * allocate what he thinks is a reasonable buffer length, 3000 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3001 * double the buffer length and try again. (And repeat 3002 * that until he succeeds.) 3003 */ 3004 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3005 if (sb == NULL) { 3006 list->status = CTL_LUN_LIST_ERROR; 3007 snprintf(list->error_str, sizeof(list->error_str), 3008 "Unable to allocate %d bytes for LUN list", 3009 list->alloc_len); 3010 break; 3011 } 3012 3013 sbuf_printf(sb, "<ctllunlist>\n"); 3014 3015 mtx_lock(&softc->ctl_lock); 3016 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3017 mtx_lock(&lun->lun_lock); 3018 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3019 (uintmax_t)lun->lun); 3020 3021 /* 3022 * Bail out as soon as we see that we've overfilled 3023 * the buffer. 3024 */ 3025 if (retval != 0) 3026 break; 3027 3028 retval = sbuf_printf(sb, "\t<backend_type>%s" 3029 "</backend_type>\n", 3030 (lun->backend == NULL) ? "none" : 3031 lun->backend->name); 3032 3033 if (retval != 0) 3034 break; 3035 3036 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3037 lun->be_lun->lun_type); 3038 3039 if (retval != 0) 3040 break; 3041 3042 if (lun->backend == NULL) { 3043 retval = sbuf_printf(sb, "</lun>\n"); 3044 if (retval != 0) 3045 break; 3046 continue; 3047 } 3048 3049 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3050 (lun->be_lun->maxlba > 0) ? 3051 lun->be_lun->maxlba + 1 : 0); 3052 3053 if (retval != 0) 3054 break; 3055 3056 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3057 lun->be_lun->blocksize); 3058 3059 if (retval != 0) 3060 break; 3061 3062 retval = sbuf_printf(sb, "\t<serial_number>"); 3063 3064 if (retval != 0) 3065 break; 3066 3067 retval = ctl_sbuf_printf_esc(sb, 3068 lun->be_lun->serial_num); 3069 3070 if (retval != 0) 3071 break; 3072 3073 retval = sbuf_printf(sb, "</serial_number>\n"); 3074 3075 if (retval != 0) 3076 break; 3077 3078 retval = sbuf_printf(sb, "\t<device_id>"); 3079 3080 if (retval != 0) 3081 break; 3082 3083 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3084 3085 if (retval != 0) 3086 break; 3087 3088 retval = sbuf_printf(sb, "</device_id>\n"); 3089 3090 if (retval != 0) 3091 break; 3092 3093 if (lun->backend->lun_info != NULL) { 3094 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3095 if (retval != 0) 3096 break; 3097 } 3098 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3099 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3100 opt->name, opt->value, opt->name); 3101 if (retval != 0) 3102 break; 3103 } 3104 3105 retval = sbuf_printf(sb, "</lun>\n"); 3106 3107 if (retval != 0) 3108 break; 3109 mtx_unlock(&lun->lun_lock); 3110 } 3111 if (lun != NULL) 3112 mtx_unlock(&lun->lun_lock); 3113 mtx_unlock(&softc->ctl_lock); 3114 3115 if ((retval != 0) 3116 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3117 retval = 0; 3118 sbuf_delete(sb); 3119 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3120 snprintf(list->error_str, sizeof(list->error_str), 3121 "Out of space, %d bytes is too small", 3122 list->alloc_len); 3123 break; 3124 } 3125 3126 sbuf_finish(sb); 3127 3128 retval = copyout(sbuf_data(sb), list->lun_xml, 3129 sbuf_len(sb) + 1); 3130 3131 list->fill_len = sbuf_len(sb) + 1; 3132 list->status = CTL_LUN_LIST_OK; 3133 sbuf_delete(sb); 3134 break; 3135 } 3136 case CTL_ISCSI: { 3137 struct ctl_iscsi *ci; 3138 struct ctl_frontend *fe; 3139 3140 ci = (struct ctl_iscsi *)addr; 3141 3142 fe = ctl_frontend_find("iscsi"); 3143 if (fe == NULL) { 3144 ci->status = CTL_ISCSI_ERROR; 3145 snprintf(ci->error_str, sizeof(ci->error_str), 3146 "Frontend \"iscsi\" not found."); 3147 break; 3148 } 3149 3150 retval = fe->ioctl(dev, cmd, addr, flag, td); 3151 break; 3152 } 3153 case CTL_PORT_REQ: { 3154 struct ctl_req *req; 3155 struct ctl_frontend *fe; 3156 3157 req = (struct ctl_req *)addr; 3158 3159 fe = ctl_frontend_find(req->driver); 3160 if (fe == NULL) { 3161 req->status = CTL_LUN_ERROR; 3162 snprintf(req->error_str, sizeof(req->error_str), 3163 "Frontend \"%s\" not found.", req->driver); 3164 break; 3165 } 3166 if (req->num_args > 0) { 3167 req->kern_args = ctl_copyin_args(req->num_args, 3168 req->args, req->error_str, sizeof(req->error_str)); 3169 if (req->kern_args == NULL) { 3170 req->status = CTL_LUN_ERROR; 3171 break; 3172 } 3173 } 3174 3175 retval = fe->ioctl(dev, cmd, addr, flag, td); 3176 3177 if (req->num_args > 0) { 3178 ctl_copyout_args(req->num_args, req->kern_args); 3179 ctl_free_args(req->num_args, req->kern_args); 3180 } 3181 break; 3182 } 3183 case CTL_PORT_LIST: { 3184 struct sbuf *sb; 3185 struct ctl_port *port; 3186 struct ctl_lun_list *list; 3187 struct ctl_option *opt; 3188 3189 list = (struct ctl_lun_list *)addr; 3190 3191 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3192 if (sb == NULL) { 3193 list->status = CTL_LUN_LIST_ERROR; 3194 snprintf(list->error_str, sizeof(list->error_str), 3195 "Unable to allocate %d bytes for LUN list", 3196 list->alloc_len); 3197 break; 3198 } 3199 3200 sbuf_printf(sb, "<ctlportlist>\n"); 3201 3202 mtx_lock(&softc->ctl_lock); 3203 STAILQ_FOREACH(port, &softc->port_list, links) { 3204 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3205 (uintmax_t)port->targ_port); 3206 3207 /* 3208 * Bail out as soon as we see that we've overfilled 3209 * the buffer. 3210 */ 3211 if (retval != 0) 3212 break; 3213 3214 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3215 "</frontend_type>\n", port->frontend->name); 3216 if (retval != 0) 3217 break; 3218 3219 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3220 port->port_type); 3221 if (retval != 0) 3222 break; 3223 3224 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3225 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3226 if (retval != 0) 3227 break; 3228 3229 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3230 port->port_name); 3231 if (retval != 0) 3232 break; 3233 3234 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3235 port->physical_port); 3236 if (retval != 0) 3237 break; 3238 3239 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3240 port->virtual_port); 3241 if (retval != 0) 3242 break; 3243 3244 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3245 (uintmax_t)port->wwnn); 3246 if (retval != 0) 3247 break; 3248 3249 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3250 (uintmax_t)port->wwpn); 3251 if (retval != 0) 3252 break; 3253 3254 STAILQ_FOREACH(opt, &port->options, links) { 3255 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3256 opt->name, opt->value, opt->name); 3257 if (retval != 0) 3258 break; 3259 } 3260 3261 retval = sbuf_printf(sb, "</targ_port>\n"); 3262 if (retval != 0) 3263 break; 3264 } 3265 mtx_unlock(&softc->ctl_lock); 3266 3267 if ((retval != 0) 3268 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3269 retval = 0; 3270 sbuf_delete(sb); 3271 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3272 snprintf(list->error_str, sizeof(list->error_str), 3273 "Out of space, %d bytes is too small", 3274 list->alloc_len); 3275 break; 3276 } 3277 3278 sbuf_finish(sb); 3279 3280 retval = copyout(sbuf_data(sb), list->lun_xml, 3281 sbuf_len(sb) + 1); 3282 3283 list->fill_len = sbuf_len(sb) + 1; 3284 list->status = CTL_LUN_LIST_OK; 3285 sbuf_delete(sb); 3286 break; 3287 } 3288 default: { 3289 /* XXX KDM should we fix this? */ 3290#if 0 3291 struct ctl_backend_driver *backend; 3292 unsigned int type; 3293 int found; 3294 3295 found = 0; 3296 3297 /* 3298 * We encode the backend type as the ioctl type for backend 3299 * ioctls. So parse it out here, and then search for a 3300 * backend of this type. 3301 */ 3302 type = _IOC_TYPE(cmd); 3303 3304 STAILQ_FOREACH(backend, &softc->be_list, links) { 3305 if (backend->type == type) { 3306 found = 1; 3307 break; 3308 } 3309 } 3310 if (found == 0) { 3311 printf("ctl: unknown ioctl command %#lx or backend " 3312 "%d\n", cmd, type); 3313 retval = EINVAL; 3314 break; 3315 } 3316 retval = backend->ioctl(dev, cmd, addr, flag, td); 3317#endif 3318 retval = ENOTTY; 3319 break; 3320 } 3321 } 3322 return (retval); 3323} 3324 3325uint32_t 3326ctl_get_initindex(struct ctl_nexus *nexus) 3327{ 3328 if (nexus->targ_port < CTL_MAX_PORTS) 3329 return (nexus->initid.id + 3330 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3331 else 3332 return (nexus->initid.id + 3333 ((nexus->targ_port - CTL_MAX_PORTS) * 3334 CTL_MAX_INIT_PER_PORT)); 3335} 3336 3337uint32_t 3338ctl_get_resindex(struct ctl_nexus *nexus) 3339{ 3340 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3341} 3342 3343uint32_t 3344ctl_port_idx(int port_num) 3345{ 3346 if (port_num < CTL_MAX_PORTS) 3347 return(port_num); 3348 else 3349 return(port_num - CTL_MAX_PORTS); 3350} 3351 3352static uint32_t 3353ctl_map_lun(int port_num, uint32_t lun_id) 3354{ 3355 struct ctl_port *port; 3356 3357 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3358 if (port == NULL) 3359 return (UINT32_MAX); 3360 if (port->lun_map == NULL) 3361 return (lun_id); 3362 return (port->lun_map(port->targ_lun_arg, lun_id)); 3363} 3364 3365static uint32_t 3366ctl_map_lun_back(int port_num, uint32_t lun_id) 3367{ 3368 struct ctl_port *port; 3369 uint32_t i; 3370 3371 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3372 if (port->lun_map == NULL) 3373 return (lun_id); 3374 for (i = 0; i < CTL_MAX_LUNS; i++) { 3375 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3376 return (i); 3377 } 3378 return (UINT32_MAX); 3379} 3380 3381/* 3382 * Note: This only works for bitmask sizes that are at least 32 bits, and 3383 * that are a power of 2. 3384 */ 3385int 3386ctl_ffz(uint32_t *mask, uint32_t size) 3387{ 3388 uint32_t num_chunks, num_pieces; 3389 int i, j; 3390 3391 num_chunks = (size >> 5); 3392 if (num_chunks == 0) 3393 num_chunks++; 3394 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3395 3396 for (i = 0; i < num_chunks; i++) { 3397 for (j = 0; j < num_pieces; j++) { 3398 if ((mask[i] & (1 << j)) == 0) 3399 return ((i << 5) + j); 3400 } 3401 } 3402 3403 return (-1); 3404} 3405 3406int 3407ctl_set_mask(uint32_t *mask, uint32_t bit) 3408{ 3409 uint32_t chunk, piece; 3410 3411 chunk = bit >> 5; 3412 piece = bit % (sizeof(uint32_t) * 8); 3413 3414 if ((mask[chunk] & (1 << piece)) != 0) 3415 return (-1); 3416 else 3417 mask[chunk] |= (1 << piece); 3418 3419 return (0); 3420} 3421 3422int 3423ctl_clear_mask(uint32_t *mask, uint32_t bit) 3424{ 3425 uint32_t chunk, piece; 3426 3427 chunk = bit >> 5; 3428 piece = bit % (sizeof(uint32_t) * 8); 3429 3430 if ((mask[chunk] & (1 << piece)) == 0) 3431 return (-1); 3432 else 3433 mask[chunk] &= ~(1 << piece); 3434 3435 return (0); 3436} 3437 3438int 3439ctl_is_set(uint32_t *mask, uint32_t bit) 3440{ 3441 uint32_t chunk, piece; 3442 3443 chunk = bit >> 5; 3444 piece = bit % (sizeof(uint32_t) * 8); 3445 3446 if ((mask[chunk] & (1 << piece)) == 0) 3447 return (0); 3448 else 3449 return (1); 3450} 3451 3452#ifdef unused 3453/* 3454 * The bus, target and lun are optional, they can be filled in later. 3455 * can_wait is used to determine whether we can wait on the malloc or not. 3456 */ 3457union ctl_io* 3458ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3459 uint32_t targ_lun, int can_wait) 3460{ 3461 union ctl_io *io; 3462 3463 if (can_wait) 3464 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3465 else 3466 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3467 3468 if (io != NULL) { 3469 io->io_hdr.io_type = io_type; 3470 io->io_hdr.targ_port = targ_port; 3471 /* 3472 * XXX KDM this needs to change/go away. We need to move 3473 * to a preallocated pool of ctl_scsiio structures. 3474 */ 3475 io->io_hdr.nexus.targ_target.id = targ_target; 3476 io->io_hdr.nexus.targ_lun = targ_lun; 3477 } 3478 3479 return (io); 3480} 3481 3482void 3483ctl_kfree_io(union ctl_io *io) 3484{ 3485 free(io, M_CTL); 3486} 3487#endif /* unused */ 3488 3489/* 3490 * ctl_softc, pool_type, total_ctl_io are passed in. 3491 * npool is passed out. 3492 */ 3493int 3494ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3495 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3496{ 3497 uint32_t i; 3498 union ctl_io *cur_io, *next_io; 3499 struct ctl_io_pool *pool; 3500 int retval; 3501 3502 retval = 0; 3503 3504 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3505 M_NOWAIT | M_ZERO); 3506 if (pool == NULL) { 3507 retval = ENOMEM; 3508 goto bailout; 3509 } 3510 3511 pool->type = pool_type; 3512 pool->ctl_softc = ctl_softc; 3513 3514 mtx_lock(&ctl_softc->pool_lock); 3515 pool->id = ctl_softc->cur_pool_id++; 3516 mtx_unlock(&ctl_softc->pool_lock); 3517 3518 pool->flags = CTL_POOL_FLAG_NONE; 3519 pool->refcount = 1; /* Reference for validity. */ 3520 STAILQ_INIT(&pool->free_queue); 3521 3522 /* 3523 * XXX KDM other options here: 3524 * - allocate a page at a time 3525 * - allocate one big chunk of memory. 3526 * Page allocation might work well, but would take a little more 3527 * tracking. 3528 */ 3529 for (i = 0; i < total_ctl_io; i++) { 3530 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3531 M_NOWAIT); 3532 if (cur_io == NULL) { 3533 retval = ENOMEM; 3534 break; 3535 } 3536 cur_io->io_hdr.pool = pool; 3537 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3538 pool->total_ctl_io++; 3539 pool->free_ctl_io++; 3540 } 3541 3542 if (retval != 0) { 3543 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3544 cur_io != NULL; cur_io = next_io) { 3545 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3546 links); 3547 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3548 ctl_io_hdr, links); 3549 free(cur_io, M_CTLIO); 3550 } 3551 3552 free(pool, M_CTL); 3553 goto bailout; 3554 } 3555 mtx_lock(&ctl_softc->pool_lock); 3556 ctl_softc->num_pools++; 3557 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3558 /* 3559 * Increment our usage count if this is an external consumer, so we 3560 * can't get unloaded until the external consumer (most likely a 3561 * FETD) unloads and frees his pool. 3562 * 3563 * XXX KDM will this increment the caller's module use count, or 3564 * mine? 3565 */ 3566#if 0 3567 if ((pool_type != CTL_POOL_EMERGENCY) 3568 && (pool_type != CTL_POOL_INTERNAL) 3569 && (pool_type != CTL_POOL_4OTHERSC)) 3570 MOD_INC_USE_COUNT; 3571#endif 3572 3573 mtx_unlock(&ctl_softc->pool_lock); 3574 3575 *npool = pool; 3576 3577bailout: 3578 3579 return (retval); 3580} 3581 3582static int 3583ctl_pool_acquire(struct ctl_io_pool *pool) 3584{ 3585 3586 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3587 3588 if (pool->flags & CTL_POOL_FLAG_INVALID) 3589 return (EINVAL); 3590 3591 pool->refcount++; 3592 3593 return (0); 3594} 3595 3596static void 3597ctl_pool_release(struct ctl_io_pool *pool) 3598{ 3599 struct ctl_softc *ctl_softc = pool->ctl_softc; 3600 union ctl_io *io; 3601 3602 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3603 3604 if (--pool->refcount != 0) 3605 return; 3606 3607 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3608 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3609 links); 3610 free(io, M_CTLIO); 3611 } 3612 3613 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3614 ctl_softc->num_pools--; 3615 3616 /* 3617 * XXX KDM will this decrement the caller's usage count or mine? 3618 */ 3619#if 0 3620 if ((pool->type != CTL_POOL_EMERGENCY) 3621 && (pool->type != CTL_POOL_INTERNAL) 3622 && (pool->type != CTL_POOL_4OTHERSC)) 3623 MOD_DEC_USE_COUNT; 3624#endif 3625 3626 free(pool, M_CTL); 3627} 3628 3629void 3630ctl_pool_free(struct ctl_io_pool *pool) 3631{ 3632 struct ctl_softc *ctl_softc; 3633 3634 if (pool == NULL) 3635 return; 3636 3637 ctl_softc = pool->ctl_softc; 3638 mtx_lock(&ctl_softc->pool_lock); 3639 pool->flags |= CTL_POOL_FLAG_INVALID; 3640 ctl_pool_release(pool); 3641 mtx_unlock(&ctl_softc->pool_lock); 3642} 3643 3644/* 3645 * This routine does not block (except for spinlocks of course). 3646 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3647 * possible. 3648 */ 3649union ctl_io * 3650ctl_alloc_io(void *pool_ref) 3651{ 3652 union ctl_io *io; 3653 struct ctl_softc *ctl_softc; 3654 struct ctl_io_pool *pool, *npool; 3655 struct ctl_io_pool *emergency_pool; 3656 3657 pool = (struct ctl_io_pool *)pool_ref; 3658 3659 if (pool == NULL) { 3660 printf("%s: pool is NULL\n", __func__); 3661 return (NULL); 3662 } 3663 3664 emergency_pool = NULL; 3665 3666 ctl_softc = pool->ctl_softc; 3667 3668 mtx_lock(&ctl_softc->pool_lock); 3669 /* 3670 * First, try to get the io structure from the user's pool. 3671 */ 3672 if (ctl_pool_acquire(pool) == 0) { 3673 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3674 if (io != NULL) { 3675 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3676 pool->total_allocated++; 3677 pool->free_ctl_io--; 3678 mtx_unlock(&ctl_softc->pool_lock); 3679 return (io); 3680 } else 3681 ctl_pool_release(pool); 3682 } 3683 /* 3684 * If he doesn't have any io structures left, search for an 3685 * emergency pool and grab one from there. 3686 */ 3687 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3688 if (npool->type != CTL_POOL_EMERGENCY) 3689 continue; 3690 3691 if (ctl_pool_acquire(npool) != 0) 3692 continue; 3693 3694 emergency_pool = npool; 3695 3696 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3697 if (io != NULL) { 3698 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3699 npool->total_allocated++; 3700 npool->free_ctl_io--; 3701 mtx_unlock(&ctl_softc->pool_lock); 3702 return (io); 3703 } else 3704 ctl_pool_release(npool); 3705 } 3706 3707 /* Drop the spinlock before we malloc */ 3708 mtx_unlock(&ctl_softc->pool_lock); 3709 3710 /* 3711 * The emergency pool (if it exists) didn't have one, so try an 3712 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3713 */ 3714 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3715 if (io != NULL) { 3716 /* 3717 * If the emergency pool exists but is empty, add this 3718 * ctl_io to its list when it gets freed. 3719 */ 3720 if (emergency_pool != NULL) { 3721 mtx_lock(&ctl_softc->pool_lock); 3722 if (ctl_pool_acquire(emergency_pool) == 0) { 3723 io->io_hdr.pool = emergency_pool; 3724 emergency_pool->total_ctl_io++; 3725 /* 3726 * Need to bump this, otherwise 3727 * total_allocated and total_freed won't 3728 * match when we no longer have anything 3729 * outstanding. 3730 */ 3731 emergency_pool->total_allocated++; 3732 } 3733 mtx_unlock(&ctl_softc->pool_lock); 3734 } else 3735 io->io_hdr.pool = NULL; 3736 } 3737 3738 return (io); 3739} 3740 3741void 3742ctl_free_io(union ctl_io *io) 3743{ 3744 if (io == NULL) 3745 return; 3746 3747 /* 3748 * If this ctl_io has a pool, return it to that pool. 3749 */ 3750 if (io->io_hdr.pool != NULL) { 3751 struct ctl_io_pool *pool; 3752 3753 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3754 mtx_lock(&pool->ctl_softc->pool_lock); 3755 io->io_hdr.io_type = 0xff; 3756 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3757 pool->total_freed++; 3758 pool->free_ctl_io++; 3759 ctl_pool_release(pool); 3760 mtx_unlock(&pool->ctl_softc->pool_lock); 3761 } else { 3762 /* 3763 * Otherwise, just free it. We probably malloced it and 3764 * the emergency pool wasn't available. 3765 */ 3766 free(io, M_CTLIO); 3767 } 3768 3769} 3770 3771void 3772ctl_zero_io(union ctl_io *io) 3773{ 3774 void *pool_ref; 3775 3776 if (io == NULL) 3777 return; 3778 3779 /* 3780 * May need to preserve linked list pointers at some point too. 3781 */ 3782 pool_ref = io->io_hdr.pool; 3783 3784 memset(io, 0, sizeof(*io)); 3785 3786 io->io_hdr.pool = pool_ref; 3787} 3788 3789/* 3790 * This routine is currently used for internal copies of ctl_ios that need 3791 * to persist for some reason after we've already returned status to the 3792 * FETD. (Thus the flag set.) 3793 * 3794 * XXX XXX 3795 * Note that this makes a blind copy of all fields in the ctl_io, except 3796 * for the pool reference. This includes any memory that has been 3797 * allocated! That memory will no longer be valid after done has been 3798 * called, so this would be VERY DANGEROUS for command that actually does 3799 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3800 * start and stop commands, which don't transfer any data, so this is not a 3801 * problem. If it is used for anything else, the caller would also need to 3802 * allocate data buffer space and this routine would need to be modified to 3803 * copy the data buffer(s) as well. 3804 */ 3805void 3806ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3807{ 3808 void *pool_ref; 3809 3810 if ((src == NULL) 3811 || (dest == NULL)) 3812 return; 3813 3814 /* 3815 * May need to preserve linked list pointers at some point too. 3816 */ 3817 pool_ref = dest->io_hdr.pool; 3818 3819 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3820 3821 dest->io_hdr.pool = pool_ref; 3822 /* 3823 * We need to know that this is an internal copy, and doesn't need 3824 * to get passed back to the FETD that allocated it. 3825 */ 3826 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3827} 3828 3829#ifdef NEEDTOPORT 3830static void 3831ctl_update_power_subpage(struct copan_power_subpage *page) 3832{ 3833 int num_luns, num_partitions, config_type; 3834 struct ctl_softc *softc; 3835 cs_BOOL_t aor_present, shelf_50pct_power; 3836 cs_raidset_personality_t rs_type; 3837 int max_active_luns; 3838 3839 softc = control_softc; 3840 3841 /* subtract out the processor LUN */ 3842 num_luns = softc->num_luns - 1; 3843 /* 3844 * Default to 7 LUNs active, which was the only number we allowed 3845 * in the past. 3846 */ 3847 max_active_luns = 7; 3848 3849 num_partitions = config_GetRsPartitionInfo(); 3850 config_type = config_GetConfigType(); 3851 shelf_50pct_power = config_GetShelfPowerMode(); 3852 aor_present = config_IsAorRsPresent(); 3853 3854 rs_type = ddb_GetRsRaidType(1); 3855 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3856 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3857 EPRINT(0, "Unsupported RS type %d!", rs_type); 3858 } 3859 3860 3861 page->total_luns = num_luns; 3862 3863 switch (config_type) { 3864 case 40: 3865 /* 3866 * In a 40 drive configuration, it doesn't matter what DC 3867 * cards we have, whether we have AOR enabled or not, 3868 * partitioning or not, or what type of RAIDset we have. 3869 * In that scenario, we can power up every LUN we present 3870 * to the user. 3871 */ 3872 max_active_luns = num_luns; 3873 3874 break; 3875 case 64: 3876 if (shelf_50pct_power == CS_FALSE) { 3877 /* 25% power */ 3878 if (aor_present == CS_TRUE) { 3879 if (rs_type == 3880 CS_RAIDSET_PERSONALITY_RAID5) { 3881 max_active_luns = 7; 3882 } else if (rs_type == 3883 CS_RAIDSET_PERSONALITY_RAID1){ 3884 max_active_luns = 14; 3885 } else { 3886 /* XXX KDM now what?? */ 3887 } 3888 } else { 3889 if (rs_type == 3890 CS_RAIDSET_PERSONALITY_RAID5) { 3891 max_active_luns = 8; 3892 } else if (rs_type == 3893 CS_RAIDSET_PERSONALITY_RAID1){ 3894 max_active_luns = 16; 3895 } else { 3896 /* XXX KDM now what?? */ 3897 } 3898 } 3899 } else { 3900 /* 50% power */ 3901 /* 3902 * With 50% power in a 64 drive configuration, we 3903 * can power all LUNs we present. 3904 */ 3905 max_active_luns = num_luns; 3906 } 3907 break; 3908 case 112: 3909 if (shelf_50pct_power == CS_FALSE) { 3910 /* 25% power */ 3911 if (aor_present == CS_TRUE) { 3912 if (rs_type == 3913 CS_RAIDSET_PERSONALITY_RAID5) { 3914 max_active_luns = 7; 3915 } else if (rs_type == 3916 CS_RAIDSET_PERSONALITY_RAID1){ 3917 max_active_luns = 14; 3918 } else { 3919 /* XXX KDM now what?? */ 3920 } 3921 } else { 3922 if (rs_type == 3923 CS_RAIDSET_PERSONALITY_RAID5) { 3924 max_active_luns = 8; 3925 } else if (rs_type == 3926 CS_RAIDSET_PERSONALITY_RAID1){ 3927 max_active_luns = 16; 3928 } else { 3929 /* XXX KDM now what?? */ 3930 } 3931 } 3932 } else { 3933 /* 50% power */ 3934 if (aor_present == CS_TRUE) { 3935 if (rs_type == 3936 CS_RAIDSET_PERSONALITY_RAID5) { 3937 max_active_luns = 14; 3938 } else if (rs_type == 3939 CS_RAIDSET_PERSONALITY_RAID1){ 3940 /* 3941 * We're assuming here that disk 3942 * caching is enabled, and so we're 3943 * able to power up half of each 3944 * LUN, and cache all writes. 3945 */ 3946 max_active_luns = num_luns; 3947 } else { 3948 /* XXX KDM now what?? */ 3949 } 3950 } else { 3951 if (rs_type == 3952 CS_RAIDSET_PERSONALITY_RAID5) { 3953 max_active_luns = 15; 3954 } else if (rs_type == 3955 CS_RAIDSET_PERSONALITY_RAID1){ 3956 max_active_luns = 30; 3957 } else { 3958 /* XXX KDM now what?? */ 3959 } 3960 } 3961 } 3962 break; 3963 default: 3964 /* 3965 * In this case, we have an unknown configuration, so we 3966 * just use the default from above. 3967 */ 3968 break; 3969 } 3970 3971 page->max_active_luns = max_active_luns; 3972#if 0 3973 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 3974 page->total_luns, page->max_active_luns); 3975#endif 3976} 3977#endif /* NEEDTOPORT */ 3978 3979/* 3980 * This routine could be used in the future to load default and/or saved 3981 * mode page parameters for a particuar lun. 3982 */ 3983static int 3984ctl_init_page_index(struct ctl_lun *lun) 3985{ 3986 int i; 3987 struct ctl_page_index *page_index; 3988 struct ctl_softc *softc; 3989 3990 memcpy(&lun->mode_pages.index, page_index_template, 3991 sizeof(page_index_template)); 3992 3993 softc = lun->ctl_softc; 3994 3995 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3996 3997 page_index = &lun->mode_pages.index[i]; 3998 /* 3999 * If this is a disk-only mode page, there's no point in 4000 * setting it up. For some pages, we have to have some 4001 * basic information about the disk in order to calculate the 4002 * mode page data. 4003 */ 4004 if ((lun->be_lun->lun_type != T_DIRECT) 4005 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4006 continue; 4007 4008 switch (page_index->page_code & SMPH_PC_MASK) { 4009 case SMS_FORMAT_DEVICE_PAGE: { 4010 struct scsi_format_page *format_page; 4011 4012 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4013 panic("subpage is incorrect!"); 4014 4015 /* 4016 * Sectors per track are set above. Bytes per 4017 * sector need to be set here on a per-LUN basis. 4018 */ 4019 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4020 &format_page_default, 4021 sizeof(format_page_default)); 4022 memcpy(&lun->mode_pages.format_page[ 4023 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4024 sizeof(format_page_changeable)); 4025 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4026 &format_page_default, 4027 sizeof(format_page_default)); 4028 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4029 &format_page_default, 4030 sizeof(format_page_default)); 4031 4032 format_page = &lun->mode_pages.format_page[ 4033 CTL_PAGE_CURRENT]; 4034 scsi_ulto2b(lun->be_lun->blocksize, 4035 format_page->bytes_per_sector); 4036 4037 format_page = &lun->mode_pages.format_page[ 4038 CTL_PAGE_DEFAULT]; 4039 scsi_ulto2b(lun->be_lun->blocksize, 4040 format_page->bytes_per_sector); 4041 4042 format_page = &lun->mode_pages.format_page[ 4043 CTL_PAGE_SAVED]; 4044 scsi_ulto2b(lun->be_lun->blocksize, 4045 format_page->bytes_per_sector); 4046 4047 page_index->page_data = 4048 (uint8_t *)lun->mode_pages.format_page; 4049 break; 4050 } 4051 case SMS_RIGID_DISK_PAGE: { 4052 struct scsi_rigid_disk_page *rigid_disk_page; 4053 uint32_t sectors_per_cylinder; 4054 uint64_t cylinders; 4055#ifndef __XSCALE__ 4056 int shift; 4057#endif /* !__XSCALE__ */ 4058 4059 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4060 panic("invalid subpage value %d", 4061 page_index->subpage); 4062 4063 /* 4064 * Rotation rate and sectors per track are set 4065 * above. We calculate the cylinders here based on 4066 * capacity. Due to the number of heads and 4067 * sectors per track we're using, smaller arrays 4068 * may turn out to have 0 cylinders. Linux and 4069 * FreeBSD don't pay attention to these mode pages 4070 * to figure out capacity, but Solaris does. It 4071 * seems to deal with 0 cylinders just fine, and 4072 * works out a fake geometry based on the capacity. 4073 */ 4074 memcpy(&lun->mode_pages.rigid_disk_page[ 4075 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4076 sizeof(rigid_disk_page_default)); 4077 memcpy(&lun->mode_pages.rigid_disk_page[ 4078 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4079 sizeof(rigid_disk_page_changeable)); 4080 memcpy(&lun->mode_pages.rigid_disk_page[ 4081 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4082 sizeof(rigid_disk_page_default)); 4083 memcpy(&lun->mode_pages.rigid_disk_page[ 4084 CTL_PAGE_SAVED], &rigid_disk_page_default, 4085 sizeof(rigid_disk_page_default)); 4086 4087 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4088 CTL_DEFAULT_HEADS; 4089 4090 /* 4091 * The divide method here will be more accurate, 4092 * probably, but results in floating point being 4093 * used in the kernel on i386 (__udivdi3()). On the 4094 * XScale, though, __udivdi3() is implemented in 4095 * software. 4096 * 4097 * The shift method for cylinder calculation is 4098 * accurate if sectors_per_cylinder is a power of 4099 * 2. Otherwise it might be slightly off -- you 4100 * might have a bit of a truncation problem. 4101 */ 4102#ifdef __XSCALE__ 4103 cylinders = (lun->be_lun->maxlba + 1) / 4104 sectors_per_cylinder; 4105#else 4106 for (shift = 31; shift > 0; shift--) { 4107 if (sectors_per_cylinder & (1 << shift)) 4108 break; 4109 } 4110 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4111#endif 4112 4113 /* 4114 * We've basically got 3 bytes, or 24 bits for the 4115 * cylinder size in the mode page. If we're over, 4116 * just round down to 2^24. 4117 */ 4118 if (cylinders > 0xffffff) 4119 cylinders = 0xffffff; 4120 4121 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4122 CTL_PAGE_CURRENT]; 4123 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4124 4125 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4126 CTL_PAGE_DEFAULT]; 4127 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4128 4129 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4130 CTL_PAGE_SAVED]; 4131 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4132 4133 page_index->page_data = 4134 (uint8_t *)lun->mode_pages.rigid_disk_page; 4135 break; 4136 } 4137 case SMS_CACHING_PAGE: { 4138 4139 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4140 panic("invalid subpage value %d", 4141 page_index->subpage); 4142 /* 4143 * Defaults should be okay here, no calculations 4144 * needed. 4145 */ 4146 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4147 &caching_page_default, 4148 sizeof(caching_page_default)); 4149 memcpy(&lun->mode_pages.caching_page[ 4150 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4151 sizeof(caching_page_changeable)); 4152 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4153 &caching_page_default, 4154 sizeof(caching_page_default)); 4155 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4156 &caching_page_default, 4157 sizeof(caching_page_default)); 4158 page_index->page_data = 4159 (uint8_t *)lun->mode_pages.caching_page; 4160 break; 4161 } 4162 case SMS_CONTROL_MODE_PAGE: { 4163 4164 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4165 panic("invalid subpage value %d", 4166 page_index->subpage); 4167 4168 /* 4169 * Defaults should be okay here, no calculations 4170 * needed. 4171 */ 4172 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4173 &control_page_default, 4174 sizeof(control_page_default)); 4175 memcpy(&lun->mode_pages.control_page[ 4176 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4177 sizeof(control_page_changeable)); 4178 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4179 &control_page_default, 4180 sizeof(control_page_default)); 4181 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4182 &control_page_default, 4183 sizeof(control_page_default)); 4184 page_index->page_data = 4185 (uint8_t *)lun->mode_pages.control_page; 4186 break; 4187 4188 } 4189 case SMS_VENDOR_SPECIFIC_PAGE:{ 4190 switch (page_index->subpage) { 4191 case PWR_SUBPAGE_CODE: { 4192 struct copan_power_subpage *current_page, 4193 *saved_page; 4194 4195 memcpy(&lun->mode_pages.power_subpage[ 4196 CTL_PAGE_CURRENT], 4197 &power_page_default, 4198 sizeof(power_page_default)); 4199 memcpy(&lun->mode_pages.power_subpage[ 4200 CTL_PAGE_CHANGEABLE], 4201 &power_page_changeable, 4202 sizeof(power_page_changeable)); 4203 memcpy(&lun->mode_pages.power_subpage[ 4204 CTL_PAGE_DEFAULT], 4205 &power_page_default, 4206 sizeof(power_page_default)); 4207 memcpy(&lun->mode_pages.power_subpage[ 4208 CTL_PAGE_SAVED], 4209 &power_page_default, 4210 sizeof(power_page_default)); 4211 page_index->page_data = 4212 (uint8_t *)lun->mode_pages.power_subpage; 4213 4214 current_page = (struct copan_power_subpage *) 4215 (page_index->page_data + 4216 (page_index->page_len * 4217 CTL_PAGE_CURRENT)); 4218 saved_page = (struct copan_power_subpage *) 4219 (page_index->page_data + 4220 (page_index->page_len * 4221 CTL_PAGE_SAVED)); 4222 break; 4223 } 4224 case APS_SUBPAGE_CODE: { 4225 struct copan_aps_subpage *current_page, 4226 *saved_page; 4227 4228 // This gets set multiple times but 4229 // it should always be the same. It's 4230 // only done during init so who cares. 4231 index_to_aps_page = i; 4232 4233 memcpy(&lun->mode_pages.aps_subpage[ 4234 CTL_PAGE_CURRENT], 4235 &aps_page_default, 4236 sizeof(aps_page_default)); 4237 memcpy(&lun->mode_pages.aps_subpage[ 4238 CTL_PAGE_CHANGEABLE], 4239 &aps_page_changeable, 4240 sizeof(aps_page_changeable)); 4241 memcpy(&lun->mode_pages.aps_subpage[ 4242 CTL_PAGE_DEFAULT], 4243 &aps_page_default, 4244 sizeof(aps_page_default)); 4245 memcpy(&lun->mode_pages.aps_subpage[ 4246 CTL_PAGE_SAVED], 4247 &aps_page_default, 4248 sizeof(aps_page_default)); 4249 page_index->page_data = 4250 (uint8_t *)lun->mode_pages.aps_subpage; 4251 4252 current_page = (struct copan_aps_subpage *) 4253 (page_index->page_data + 4254 (page_index->page_len * 4255 CTL_PAGE_CURRENT)); 4256 saved_page = (struct copan_aps_subpage *) 4257 (page_index->page_data + 4258 (page_index->page_len * 4259 CTL_PAGE_SAVED)); 4260 break; 4261 } 4262 case DBGCNF_SUBPAGE_CODE: { 4263 struct copan_debugconf_subpage *current_page, 4264 *saved_page; 4265 4266 memcpy(&lun->mode_pages.debugconf_subpage[ 4267 CTL_PAGE_CURRENT], 4268 &debugconf_page_default, 4269 sizeof(debugconf_page_default)); 4270 memcpy(&lun->mode_pages.debugconf_subpage[ 4271 CTL_PAGE_CHANGEABLE], 4272 &debugconf_page_changeable, 4273 sizeof(debugconf_page_changeable)); 4274 memcpy(&lun->mode_pages.debugconf_subpage[ 4275 CTL_PAGE_DEFAULT], 4276 &debugconf_page_default, 4277 sizeof(debugconf_page_default)); 4278 memcpy(&lun->mode_pages.debugconf_subpage[ 4279 CTL_PAGE_SAVED], 4280 &debugconf_page_default, 4281 sizeof(debugconf_page_default)); 4282 page_index->page_data = 4283 (uint8_t *)lun->mode_pages.debugconf_subpage; 4284 4285 current_page = (struct copan_debugconf_subpage *) 4286 (page_index->page_data + 4287 (page_index->page_len * 4288 CTL_PAGE_CURRENT)); 4289 saved_page = (struct copan_debugconf_subpage *) 4290 (page_index->page_data + 4291 (page_index->page_len * 4292 CTL_PAGE_SAVED)); 4293 break; 4294 } 4295 default: 4296 panic("invalid subpage value %d", 4297 page_index->subpage); 4298 break; 4299 } 4300 break; 4301 } 4302 default: 4303 panic("invalid page value %d", 4304 page_index->page_code & SMPH_PC_MASK); 4305 break; 4306 } 4307 } 4308 4309 return (CTL_RETVAL_COMPLETE); 4310} 4311 4312/* 4313 * LUN allocation. 4314 * 4315 * Requirements: 4316 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4317 * wants us to allocate the LUN and he can block. 4318 * - ctl_softc is always set 4319 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4320 * 4321 * Returns 0 for success, non-zero (errno) for failure. 4322 */ 4323static int 4324ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4325 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4326{ 4327 struct ctl_lun *nlun, *lun; 4328 struct ctl_port *port; 4329 struct scsi_vpd_id_descriptor *desc; 4330 struct scsi_vpd_id_t10 *t10id; 4331 const char *scsiname, *vendor; 4332 int lun_number, i, lun_malloced; 4333 int devidlen, idlen1, idlen2, len; 4334 4335 if (be_lun == NULL) 4336 return (EINVAL); 4337 4338 /* 4339 * We currently only support Direct Access or Processor LUN types. 4340 */ 4341 switch (be_lun->lun_type) { 4342 case T_DIRECT: 4343 break; 4344 case T_PROCESSOR: 4345 break; 4346 case T_SEQUENTIAL: 4347 case T_CHANGER: 4348 default: 4349 be_lun->lun_config_status(be_lun->be_lun, 4350 CTL_LUN_CONFIG_FAILURE); 4351 break; 4352 } 4353 if (ctl_lun == NULL) { 4354 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4355 lun_malloced = 1; 4356 } else { 4357 lun_malloced = 0; 4358 lun = ctl_lun; 4359 } 4360 4361 memset(lun, 0, sizeof(*lun)); 4362 if (lun_malloced) 4363 lun->flags = CTL_LUN_MALLOCED; 4364 4365 /* Generate LUN ID. */ 4366 devidlen = max(CTL_DEVID_MIN_LEN, 4367 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4368 idlen1 = sizeof(*t10id) + devidlen; 4369 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4370 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4371 if (scsiname != NULL) { 4372 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4373 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4374 } 4375 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4376 M_CTL, M_WAITOK | M_ZERO); 4377 lun->lun_devid->len = len; 4378 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4379 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4380 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4381 desc->length = idlen1; 4382 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4383 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4384 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4385 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4386 } else { 4387 strncpy(t10id->vendor, vendor, 4388 min(sizeof(t10id->vendor), strlen(vendor))); 4389 } 4390 strncpy((char *)t10id->vendor_spec_id, 4391 (char *)be_lun->device_id, devidlen); 4392 if (scsiname != NULL) { 4393 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4394 desc->length); 4395 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4396 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4397 SVPD_ID_TYPE_SCSI_NAME; 4398 desc->length = idlen2; 4399 strlcpy(desc->identifier, scsiname, idlen2); 4400 } 4401 4402 mtx_lock(&ctl_softc->ctl_lock); 4403 /* 4404 * See if the caller requested a particular LUN number. If so, see 4405 * if it is available. Otherwise, allocate the first available LUN. 4406 */ 4407 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4408 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4409 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4410 mtx_unlock(&ctl_softc->ctl_lock); 4411 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4412 printf("ctl: requested LUN ID %d is higher " 4413 "than CTL_MAX_LUNS - 1 (%d)\n", 4414 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4415 } else { 4416 /* 4417 * XXX KDM return an error, or just assign 4418 * another LUN ID in this case?? 4419 */ 4420 printf("ctl: requested LUN ID %d is already " 4421 "in use\n", be_lun->req_lun_id); 4422 } 4423 if (lun->flags & CTL_LUN_MALLOCED) 4424 free(lun, M_CTL); 4425 be_lun->lun_config_status(be_lun->be_lun, 4426 CTL_LUN_CONFIG_FAILURE); 4427 return (ENOSPC); 4428 } 4429 lun_number = be_lun->req_lun_id; 4430 } else { 4431 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4432 if (lun_number == -1) { 4433 mtx_unlock(&ctl_softc->ctl_lock); 4434 printf("ctl: can't allocate LUN on target %ju, out of " 4435 "LUNs\n", (uintmax_t)target_id.id); 4436 if (lun->flags & CTL_LUN_MALLOCED) 4437 free(lun, M_CTL); 4438 be_lun->lun_config_status(be_lun->be_lun, 4439 CTL_LUN_CONFIG_FAILURE); 4440 return (ENOSPC); 4441 } 4442 } 4443 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4444 4445 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4446 lun->target = target_id; 4447 lun->lun = lun_number; 4448 lun->be_lun = be_lun; 4449 /* 4450 * The processor LUN is always enabled. Disk LUNs come on line 4451 * disabled, and must be enabled by the backend. 4452 */ 4453 lun->flags |= CTL_LUN_DISABLED; 4454 lun->backend = be_lun->be; 4455 be_lun->ctl_lun = lun; 4456 be_lun->lun_id = lun_number; 4457 atomic_add_int(&be_lun->be->num_luns, 1); 4458 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4459 lun->flags |= CTL_LUN_STOPPED; 4460 4461 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4462 lun->flags |= CTL_LUN_INOPERABLE; 4463 4464 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4465 lun->flags |= CTL_LUN_PRIMARY_SC; 4466 4467 lun->ctl_softc = ctl_softc; 4468 TAILQ_INIT(&lun->ooa_queue); 4469 TAILQ_INIT(&lun->blocked_queue); 4470 STAILQ_INIT(&lun->error_list); 4471 4472 /* 4473 * Initialize the mode page index. 4474 */ 4475 ctl_init_page_index(lun); 4476 4477 /* 4478 * Set the poweron UA for all initiators on this LUN only. 4479 */ 4480 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4481 lun->pending_sense[i].ua_pending = CTL_UA_POWERON; 4482 4483 /* 4484 * Now, before we insert this lun on the lun list, set the lun 4485 * inventory changed UA for all other luns. 4486 */ 4487 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4488 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4489 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4490 } 4491 } 4492 4493 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4494 4495 ctl_softc->ctl_luns[lun_number] = lun; 4496 4497 ctl_softc->num_luns++; 4498 4499 /* Setup statistics gathering */ 4500 lun->stats.device_type = be_lun->lun_type; 4501 lun->stats.lun_number = lun_number; 4502 if (lun->stats.device_type == T_DIRECT) 4503 lun->stats.blocksize = be_lun->blocksize; 4504 else 4505 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4506 for (i = 0;i < CTL_MAX_PORTS;i++) 4507 lun->stats.ports[i].targ_port = i; 4508 4509 mtx_unlock(&ctl_softc->ctl_lock); 4510 4511 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4512 4513 /* 4514 * Run through each registered FETD and bring it online if it isn't 4515 * already. Enable the target ID if it hasn't been enabled, and 4516 * enable this particular LUN. 4517 */ 4518 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4519 int retval; 4520 4521 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4522 if (retval != 0) { 4523 printf("ctl_alloc_lun: FETD %s port %d returned error " 4524 "%d for lun_enable on target %ju lun %d\n", 4525 port->port_name, port->targ_port, retval, 4526 (uintmax_t)target_id.id, lun_number); 4527 } else 4528 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4529 } 4530 return (0); 4531} 4532 4533/* 4534 * Delete a LUN. 4535 * Assumptions: 4536 * - LUN has already been marked invalid and any pending I/O has been taken 4537 * care of. 4538 */ 4539static int 4540ctl_free_lun(struct ctl_lun *lun) 4541{ 4542 struct ctl_softc *softc; 4543#if 0 4544 struct ctl_port *port; 4545#endif 4546 struct ctl_lun *nlun; 4547 int i; 4548 4549 softc = lun->ctl_softc; 4550 4551 mtx_assert(&softc->ctl_lock, MA_OWNED); 4552 4553 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4554 4555 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4556 4557 softc->ctl_luns[lun->lun] = NULL; 4558 4559 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4560 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4561 4562 softc->num_luns--; 4563 4564 /* 4565 * XXX KDM this scheme only works for a single target/multiple LUN 4566 * setup. It needs to be revamped for a multiple target scheme. 4567 * 4568 * XXX KDM this results in port->lun_disable() getting called twice, 4569 * once when ctl_disable_lun() is called, and a second time here. 4570 * We really need to re-think the LUN disable semantics. There 4571 * should probably be several steps/levels to LUN removal: 4572 * - disable 4573 * - invalidate 4574 * - free 4575 * 4576 * Right now we only have a disable method when communicating to 4577 * the front end ports, at least for individual LUNs. 4578 */ 4579#if 0 4580 STAILQ_FOREACH(port, &softc->port_list, links) { 4581 int retval; 4582 4583 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4584 lun->lun); 4585 if (retval != 0) { 4586 printf("ctl_free_lun: FETD %s port %d returned error " 4587 "%d for lun_disable on target %ju lun %jd\n", 4588 port->port_name, port->targ_port, retval, 4589 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4590 } 4591 4592 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4593 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4594 4595 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4596 if (retval != 0) { 4597 printf("ctl_free_lun: FETD %s port %d " 4598 "returned error %d for targ_disable on " 4599 "target %ju\n", port->port_name, 4600 port->targ_port, retval, 4601 (uintmax_t)lun->target.id); 4602 } else 4603 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4604 4605 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4606 continue; 4607 4608#if 0 4609 port->port_offline(port->onoff_arg); 4610 port->status &= ~CTL_PORT_STATUS_ONLINE; 4611#endif 4612 } 4613 } 4614#endif 4615 4616 /* 4617 * Tell the backend to free resources, if this LUN has a backend. 4618 */ 4619 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4620 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4621 4622 mtx_destroy(&lun->lun_lock); 4623 free(lun->lun_devid, M_CTL); 4624 if (lun->flags & CTL_LUN_MALLOCED) 4625 free(lun, M_CTL); 4626 4627 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4628 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4629 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4630 } 4631 } 4632 4633 return (0); 4634} 4635 4636static void 4637ctl_create_lun(struct ctl_be_lun *be_lun) 4638{ 4639 struct ctl_softc *ctl_softc; 4640 4641 ctl_softc = control_softc; 4642 4643 /* 4644 * ctl_alloc_lun() should handle all potential failure cases. 4645 */ 4646 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4647} 4648 4649int 4650ctl_add_lun(struct ctl_be_lun *be_lun) 4651{ 4652 struct ctl_softc *ctl_softc = control_softc; 4653 4654 mtx_lock(&ctl_softc->ctl_lock); 4655 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4656 mtx_unlock(&ctl_softc->ctl_lock); 4657 wakeup(&ctl_softc->pending_lun_queue); 4658 4659 return (0); 4660} 4661 4662int 4663ctl_enable_lun(struct ctl_be_lun *be_lun) 4664{ 4665 struct ctl_softc *ctl_softc; 4666 struct ctl_port *port, *nport; 4667 struct ctl_lun *lun; 4668 int retval; 4669 4670 ctl_softc = control_softc; 4671 4672 lun = (struct ctl_lun *)be_lun->ctl_lun; 4673 4674 mtx_lock(&ctl_softc->ctl_lock); 4675 mtx_lock(&lun->lun_lock); 4676 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4677 /* 4678 * eh? Why did we get called if the LUN is already 4679 * enabled? 4680 */ 4681 mtx_unlock(&lun->lun_lock); 4682 mtx_unlock(&ctl_softc->ctl_lock); 4683 return (0); 4684 } 4685 lun->flags &= ~CTL_LUN_DISABLED; 4686 mtx_unlock(&lun->lun_lock); 4687 4688 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4689 nport = STAILQ_NEXT(port, links); 4690 4691 /* 4692 * Drop the lock while we call the FETD's enable routine. 4693 * This can lead to a callback into CTL (at least in the 4694 * case of the internal initiator frontend. 4695 */ 4696 mtx_unlock(&ctl_softc->ctl_lock); 4697 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4698 mtx_lock(&ctl_softc->ctl_lock); 4699 if (retval != 0) { 4700 printf("%s: FETD %s port %d returned error " 4701 "%d for lun_enable on target %ju lun %jd\n", 4702 __func__, port->port_name, port->targ_port, retval, 4703 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4704 } 4705#if 0 4706 else { 4707 /* NOTE: TODO: why does lun enable affect port status? */ 4708 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4709 } 4710#endif 4711 } 4712 4713 mtx_unlock(&ctl_softc->ctl_lock); 4714 4715 return (0); 4716} 4717 4718int 4719ctl_disable_lun(struct ctl_be_lun *be_lun) 4720{ 4721 struct ctl_softc *ctl_softc; 4722 struct ctl_port *port; 4723 struct ctl_lun *lun; 4724 int retval; 4725 4726 ctl_softc = control_softc; 4727 4728 lun = (struct ctl_lun *)be_lun->ctl_lun; 4729 4730 mtx_lock(&ctl_softc->ctl_lock); 4731 mtx_lock(&lun->lun_lock); 4732 if (lun->flags & CTL_LUN_DISABLED) { 4733 mtx_unlock(&lun->lun_lock); 4734 mtx_unlock(&ctl_softc->ctl_lock); 4735 return (0); 4736 } 4737 lun->flags |= CTL_LUN_DISABLED; 4738 mtx_unlock(&lun->lun_lock); 4739 4740 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4741 mtx_unlock(&ctl_softc->ctl_lock); 4742 /* 4743 * Drop the lock before we call the frontend's disable 4744 * routine, to avoid lock order reversals. 4745 * 4746 * XXX KDM what happens if the frontend list changes while 4747 * we're traversing it? It's unlikely, but should be handled. 4748 */ 4749 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4750 lun->lun); 4751 mtx_lock(&ctl_softc->ctl_lock); 4752 if (retval != 0) { 4753 printf("ctl_alloc_lun: FETD %s port %d returned error " 4754 "%d for lun_disable on target %ju lun %jd\n", 4755 port->port_name, port->targ_port, retval, 4756 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4757 } 4758 } 4759 4760 mtx_unlock(&ctl_softc->ctl_lock); 4761 4762 return (0); 4763} 4764 4765int 4766ctl_start_lun(struct ctl_be_lun *be_lun) 4767{ 4768 struct ctl_softc *ctl_softc; 4769 struct ctl_lun *lun; 4770 4771 ctl_softc = control_softc; 4772 4773 lun = (struct ctl_lun *)be_lun->ctl_lun; 4774 4775 mtx_lock(&lun->lun_lock); 4776 lun->flags &= ~CTL_LUN_STOPPED; 4777 mtx_unlock(&lun->lun_lock); 4778 4779 return (0); 4780} 4781 4782int 4783ctl_stop_lun(struct ctl_be_lun *be_lun) 4784{ 4785 struct ctl_softc *ctl_softc; 4786 struct ctl_lun *lun; 4787 4788 ctl_softc = control_softc; 4789 4790 lun = (struct ctl_lun *)be_lun->ctl_lun; 4791 4792 mtx_lock(&lun->lun_lock); 4793 lun->flags |= CTL_LUN_STOPPED; 4794 mtx_unlock(&lun->lun_lock); 4795 4796 return (0); 4797} 4798 4799int 4800ctl_lun_offline(struct ctl_be_lun *be_lun) 4801{ 4802 struct ctl_softc *ctl_softc; 4803 struct ctl_lun *lun; 4804 4805 ctl_softc = control_softc; 4806 4807 lun = (struct ctl_lun *)be_lun->ctl_lun; 4808 4809 mtx_lock(&lun->lun_lock); 4810 lun->flags |= CTL_LUN_OFFLINE; 4811 mtx_unlock(&lun->lun_lock); 4812 4813 return (0); 4814} 4815 4816int 4817ctl_lun_online(struct ctl_be_lun *be_lun) 4818{ 4819 struct ctl_softc *ctl_softc; 4820 struct ctl_lun *lun; 4821 4822 ctl_softc = control_softc; 4823 4824 lun = (struct ctl_lun *)be_lun->ctl_lun; 4825 4826 mtx_lock(&lun->lun_lock); 4827 lun->flags &= ~CTL_LUN_OFFLINE; 4828 mtx_unlock(&lun->lun_lock); 4829 4830 return (0); 4831} 4832 4833int 4834ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4835{ 4836 struct ctl_softc *ctl_softc; 4837 struct ctl_lun *lun; 4838 4839 ctl_softc = control_softc; 4840 4841 lun = (struct ctl_lun *)be_lun->ctl_lun; 4842 4843 mtx_lock(&lun->lun_lock); 4844 4845 /* 4846 * The LUN needs to be disabled before it can be marked invalid. 4847 */ 4848 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4849 mtx_unlock(&lun->lun_lock); 4850 return (-1); 4851 } 4852 /* 4853 * Mark the LUN invalid. 4854 */ 4855 lun->flags |= CTL_LUN_INVALID; 4856 4857 /* 4858 * If there is nothing in the OOA queue, go ahead and free the LUN. 4859 * If we have something in the OOA queue, we'll free it when the 4860 * last I/O completes. 4861 */ 4862 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4863 mtx_unlock(&lun->lun_lock); 4864 mtx_lock(&ctl_softc->ctl_lock); 4865 ctl_free_lun(lun); 4866 mtx_unlock(&ctl_softc->ctl_lock); 4867 } else 4868 mtx_unlock(&lun->lun_lock); 4869 4870 return (0); 4871} 4872 4873int 4874ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4875{ 4876 struct ctl_softc *ctl_softc; 4877 struct ctl_lun *lun; 4878 4879 ctl_softc = control_softc; 4880 lun = (struct ctl_lun *)be_lun->ctl_lun; 4881 4882 mtx_lock(&lun->lun_lock); 4883 lun->flags |= CTL_LUN_INOPERABLE; 4884 mtx_unlock(&lun->lun_lock); 4885 4886 return (0); 4887} 4888 4889int 4890ctl_lun_operable(struct ctl_be_lun *be_lun) 4891{ 4892 struct ctl_softc *ctl_softc; 4893 struct ctl_lun *lun; 4894 4895 ctl_softc = control_softc; 4896 lun = (struct ctl_lun *)be_lun->ctl_lun; 4897 4898 mtx_lock(&lun->lun_lock); 4899 lun->flags &= ~CTL_LUN_INOPERABLE; 4900 mtx_unlock(&lun->lun_lock); 4901 4902 return (0); 4903} 4904 4905int 4906ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 4907 int lock) 4908{ 4909 struct ctl_softc *softc; 4910 struct ctl_lun *lun; 4911 struct copan_aps_subpage *current_sp; 4912 struct ctl_page_index *page_index; 4913 int i; 4914 4915 softc = control_softc; 4916 4917 mtx_lock(&softc->ctl_lock); 4918 4919 lun = (struct ctl_lun *)be_lun->ctl_lun; 4920 mtx_lock(&lun->lun_lock); 4921 4922 page_index = NULL; 4923 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4924 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 4925 APS_PAGE_CODE) 4926 continue; 4927 4928 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 4929 continue; 4930 page_index = &lun->mode_pages.index[i]; 4931 } 4932 4933 if (page_index == NULL) { 4934 mtx_unlock(&lun->lun_lock); 4935 mtx_unlock(&softc->ctl_lock); 4936 printf("%s: APS subpage not found for lun %ju!\n", __func__, 4937 (uintmax_t)lun->lun); 4938 return (1); 4939 } 4940#if 0 4941 if ((softc->aps_locked_lun != 0) 4942 && (softc->aps_locked_lun != lun->lun)) { 4943 printf("%s: attempt to lock LUN %llu when %llu is already " 4944 "locked\n"); 4945 mtx_unlock(&lun->lun_lock); 4946 mtx_unlock(&softc->ctl_lock); 4947 return (1); 4948 } 4949#endif 4950 4951 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 4952 (page_index->page_len * CTL_PAGE_CURRENT)); 4953 4954 if (lock != 0) { 4955 current_sp->lock_active = APS_LOCK_ACTIVE; 4956 softc->aps_locked_lun = lun->lun; 4957 } else { 4958 current_sp->lock_active = 0; 4959 softc->aps_locked_lun = 0; 4960 } 4961 4962 4963 /* 4964 * If we're in HA mode, try to send the lock message to the other 4965 * side. 4966 */ 4967 if (ctl_is_single == 0) { 4968 int isc_retval; 4969 union ctl_ha_msg lock_msg; 4970 4971 lock_msg.hdr.nexus = *nexus; 4972 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 4973 if (lock != 0) 4974 lock_msg.aps.lock_flag = 1; 4975 else 4976 lock_msg.aps.lock_flag = 0; 4977 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 4978 sizeof(lock_msg), 0); 4979 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 4980 printf("%s: APS (lock=%d) error returned from " 4981 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 4982 mtx_unlock(&lun->lun_lock); 4983 mtx_unlock(&softc->ctl_lock); 4984 return (1); 4985 } 4986 } 4987 4988 mtx_unlock(&lun->lun_lock); 4989 mtx_unlock(&softc->ctl_lock); 4990 4991 return (0); 4992} 4993 4994void 4995ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4996{ 4997 struct ctl_lun *lun; 4998 struct ctl_softc *softc; 4999 int i; 5000 5001 softc = control_softc; 5002 5003 lun = (struct ctl_lun *)be_lun->ctl_lun; 5004 5005 mtx_lock(&lun->lun_lock); 5006 5007 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5008 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED; 5009 5010 mtx_unlock(&lun->lun_lock); 5011} 5012 5013/* 5014 * Backend "memory move is complete" callback for requests that never 5015 * make it down to say RAIDCore's configuration code. 5016 */ 5017int 5018ctl_config_move_done(union ctl_io *io) 5019{ 5020 int retval; 5021 5022 retval = CTL_RETVAL_COMPLETE; 5023 5024 5025 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5026 /* 5027 * XXX KDM this shouldn't happen, but what if it does? 5028 */ 5029 if (io->io_hdr.io_type != CTL_IO_SCSI) 5030 panic("I/O type isn't CTL_IO_SCSI!"); 5031 5032 if ((io->io_hdr.port_status == 0) 5033 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5034 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5035 io->io_hdr.status = CTL_SUCCESS; 5036 else if ((io->io_hdr.port_status != 0) 5037 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5038 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5039 /* 5040 * For hardware error sense keys, the sense key 5041 * specific value is defined to be a retry count, 5042 * but we use it to pass back an internal FETD 5043 * error code. XXX KDM Hopefully the FETD is only 5044 * using 16 bits for an error code, since that's 5045 * all the space we have in the sks field. 5046 */ 5047 ctl_set_internal_failure(&io->scsiio, 5048 /*sks_valid*/ 1, 5049 /*retry_count*/ 5050 io->io_hdr.port_status); 5051 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5052 free(io->scsiio.kern_data_ptr, M_CTL); 5053 ctl_done(io); 5054 goto bailout; 5055 } 5056 5057 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5058 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5059 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5060 /* 5061 * XXX KDM just assuming a single pointer here, and not a 5062 * S/G list. If we start using S/G lists for config data, 5063 * we'll need to know how to clean them up here as well. 5064 */ 5065 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5066 free(io->scsiio.kern_data_ptr, M_CTL); 5067 /* Hopefully the user has already set the status... */ 5068 ctl_done(io); 5069 } else { 5070 /* 5071 * XXX KDM now we need to continue data movement. Some 5072 * options: 5073 * - call ctl_scsiio() again? We don't do this for data 5074 * writes, because for those at least we know ahead of 5075 * time where the write will go and how long it is. For 5076 * config writes, though, that information is largely 5077 * contained within the write itself, thus we need to 5078 * parse out the data again. 5079 * 5080 * - Call some other function once the data is in? 5081 */ 5082 5083 /* 5084 * XXX KDM call ctl_scsiio() again for now, and check flag 5085 * bits to see whether we're allocated or not. 5086 */ 5087 retval = ctl_scsiio(&io->scsiio); 5088 } 5089bailout: 5090 return (retval); 5091} 5092 5093/* 5094 * This gets called by a backend driver when it is done with a 5095 * data_submit method. 5096 */ 5097void 5098ctl_data_submit_done(union ctl_io *io) 5099{ 5100 /* 5101 * If the IO_CONT flag is set, we need to call the supplied 5102 * function to continue processing the I/O, instead of completing 5103 * the I/O just yet. 5104 * 5105 * If there is an error, though, we don't want to keep processing. 5106 * Instead, just send status back to the initiator. 5107 */ 5108 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5109 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5110 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5111 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5112 io->scsiio.io_cont(io); 5113 return; 5114 } 5115 ctl_done(io); 5116} 5117 5118/* 5119 * This gets called by a backend driver when it is done with a 5120 * configuration write. 5121 */ 5122void 5123ctl_config_write_done(union ctl_io *io) 5124{ 5125 /* 5126 * If the IO_CONT flag is set, we need to call the supplied 5127 * function to continue processing the I/O, instead of completing 5128 * the I/O just yet. 5129 * 5130 * If there is an error, though, we don't want to keep processing. 5131 * Instead, just send status back to the initiator. 5132 */ 5133 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5134 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5135 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5136 io->scsiio.io_cont(io); 5137 return; 5138 } 5139 /* 5140 * Since a configuration write can be done for commands that actually 5141 * have data allocated, like write buffer, and commands that have 5142 * no data, like start/stop unit, we need to check here. 5143 */ 5144 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5145 free(io->scsiio.kern_data_ptr, M_CTL); 5146 ctl_done(io); 5147} 5148 5149/* 5150 * SCSI release command. 5151 */ 5152int 5153ctl_scsi_release(struct ctl_scsiio *ctsio) 5154{ 5155 int length, longid, thirdparty_id, resv_id; 5156 struct ctl_softc *ctl_softc; 5157 struct ctl_lun *lun; 5158 5159 length = 0; 5160 resv_id = 0; 5161 5162 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5163 5164 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5165 ctl_softc = control_softc; 5166 5167 switch (ctsio->cdb[0]) { 5168 case RELEASE_10: { 5169 struct scsi_release_10 *cdb; 5170 5171 cdb = (struct scsi_release_10 *)ctsio->cdb; 5172 5173 if (cdb->byte2 & SR10_LONGID) 5174 longid = 1; 5175 else 5176 thirdparty_id = cdb->thirdparty_id; 5177 5178 resv_id = cdb->resv_id; 5179 length = scsi_2btoul(cdb->length); 5180 break; 5181 } 5182 } 5183 5184 5185 /* 5186 * XXX KDM right now, we only support LUN reservation. We don't 5187 * support 3rd party reservations, or extent reservations, which 5188 * might actually need the parameter list. If we've gotten this 5189 * far, we've got a LUN reservation. Anything else got kicked out 5190 * above. So, according to SPC, ignore the length. 5191 */ 5192 length = 0; 5193 5194 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5195 && (length > 0)) { 5196 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5197 ctsio->kern_data_len = length; 5198 ctsio->kern_total_len = length; 5199 ctsio->kern_data_resid = 0; 5200 ctsio->kern_rel_offset = 0; 5201 ctsio->kern_sg_entries = 0; 5202 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5203 ctsio->be_move_done = ctl_config_move_done; 5204 ctl_datamove((union ctl_io *)ctsio); 5205 5206 return (CTL_RETVAL_COMPLETE); 5207 } 5208 5209 if (length > 0) 5210 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5211 5212 mtx_lock(&lun->lun_lock); 5213 5214 /* 5215 * According to SPC, it is not an error for an intiator to attempt 5216 * to release a reservation on a LUN that isn't reserved, or that 5217 * is reserved by another initiator. The reservation can only be 5218 * released, though, by the initiator who made it or by one of 5219 * several reset type events. 5220 */ 5221 if (lun->flags & CTL_LUN_RESERVED) { 5222 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5223 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5224 && (ctsio->io_hdr.nexus.targ_target.id == 5225 lun->rsv_nexus.targ_target.id)) { 5226 lun->flags &= ~CTL_LUN_RESERVED; 5227 } 5228 } 5229 5230 mtx_unlock(&lun->lun_lock); 5231 5232 ctsio->scsi_status = SCSI_STATUS_OK; 5233 ctsio->io_hdr.status = CTL_SUCCESS; 5234 5235 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5236 free(ctsio->kern_data_ptr, M_CTL); 5237 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5238 } 5239 5240 ctl_done((union ctl_io *)ctsio); 5241 return (CTL_RETVAL_COMPLETE); 5242} 5243 5244int 5245ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5246{ 5247 int extent, thirdparty, longid; 5248 int resv_id, length; 5249 uint64_t thirdparty_id; 5250 struct ctl_softc *ctl_softc; 5251 struct ctl_lun *lun; 5252 5253 extent = 0; 5254 thirdparty = 0; 5255 longid = 0; 5256 resv_id = 0; 5257 length = 0; 5258 thirdparty_id = 0; 5259 5260 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5261 5262 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5263 ctl_softc = control_softc; 5264 5265 switch (ctsio->cdb[0]) { 5266 case RESERVE_10: { 5267 struct scsi_reserve_10 *cdb; 5268 5269 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5270 5271 if (cdb->byte2 & SR10_LONGID) 5272 longid = 1; 5273 else 5274 thirdparty_id = cdb->thirdparty_id; 5275 5276 resv_id = cdb->resv_id; 5277 length = scsi_2btoul(cdb->length); 5278 break; 5279 } 5280 } 5281 5282 /* 5283 * XXX KDM right now, we only support LUN reservation. We don't 5284 * support 3rd party reservations, or extent reservations, which 5285 * might actually need the parameter list. If we've gotten this 5286 * far, we've got a LUN reservation. Anything else got kicked out 5287 * above. So, according to SPC, ignore the length. 5288 */ 5289 length = 0; 5290 5291 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5292 && (length > 0)) { 5293 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5294 ctsio->kern_data_len = length; 5295 ctsio->kern_total_len = length; 5296 ctsio->kern_data_resid = 0; 5297 ctsio->kern_rel_offset = 0; 5298 ctsio->kern_sg_entries = 0; 5299 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5300 ctsio->be_move_done = ctl_config_move_done; 5301 ctl_datamove((union ctl_io *)ctsio); 5302 5303 return (CTL_RETVAL_COMPLETE); 5304 } 5305 5306 if (length > 0) 5307 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5308 5309 mtx_lock(&lun->lun_lock); 5310 if (lun->flags & CTL_LUN_RESERVED) { 5311 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5312 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5313 || (ctsio->io_hdr.nexus.targ_target.id != 5314 lun->rsv_nexus.targ_target.id)) { 5315 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5316 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5317 goto bailout; 5318 } 5319 } 5320 5321 lun->flags |= CTL_LUN_RESERVED; 5322 lun->rsv_nexus = ctsio->io_hdr.nexus; 5323 5324 ctsio->scsi_status = SCSI_STATUS_OK; 5325 ctsio->io_hdr.status = CTL_SUCCESS; 5326 5327bailout: 5328 mtx_unlock(&lun->lun_lock); 5329 5330 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5331 free(ctsio->kern_data_ptr, M_CTL); 5332 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5333 } 5334 5335 ctl_done((union ctl_io *)ctsio); 5336 return (CTL_RETVAL_COMPLETE); 5337} 5338 5339int 5340ctl_start_stop(struct ctl_scsiio *ctsio) 5341{ 5342 struct scsi_start_stop_unit *cdb; 5343 struct ctl_lun *lun; 5344 struct ctl_softc *ctl_softc; 5345 int retval; 5346 5347 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5348 5349 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5350 ctl_softc = control_softc; 5351 retval = 0; 5352 5353 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5354 5355 /* 5356 * XXX KDM 5357 * We don't support the immediate bit on a stop unit. In order to 5358 * do that, we would need to code up a way to know that a stop is 5359 * pending, and hold off any new commands until it completes, one 5360 * way or another. Then we could accept or reject those commands 5361 * depending on its status. We would almost need to do the reverse 5362 * of what we do below for an immediate start -- return the copy of 5363 * the ctl_io to the FETD with status to send to the host (and to 5364 * free the copy!) and then free the original I/O once the stop 5365 * actually completes. That way, the OOA queue mechanism can work 5366 * to block commands that shouldn't proceed. Another alternative 5367 * would be to put the copy in the queue in place of the original, 5368 * and return the original back to the caller. That could be 5369 * slightly safer.. 5370 */ 5371 if ((cdb->byte2 & SSS_IMMED) 5372 && ((cdb->how & SSS_START) == 0)) { 5373 ctl_set_invalid_field(ctsio, 5374 /*sks_valid*/ 1, 5375 /*command*/ 1, 5376 /*field*/ 1, 5377 /*bit_valid*/ 1, 5378 /*bit*/ 0); 5379 ctl_done((union ctl_io *)ctsio); 5380 return (CTL_RETVAL_COMPLETE); 5381 } 5382 5383 if ((lun->flags & CTL_LUN_PR_RESERVED) 5384 && ((cdb->how & SSS_START)==0)) { 5385 uint32_t residx; 5386 5387 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5388 if (!lun->per_res[residx].registered 5389 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5390 5391 ctl_set_reservation_conflict(ctsio); 5392 ctl_done((union ctl_io *)ctsio); 5393 return (CTL_RETVAL_COMPLETE); 5394 } 5395 } 5396 5397 /* 5398 * If there is no backend on this device, we can't start or stop 5399 * it. In theory we shouldn't get any start/stop commands in the 5400 * first place at this level if the LUN doesn't have a backend. 5401 * That should get stopped by the command decode code. 5402 */ 5403 if (lun->backend == NULL) { 5404 ctl_set_invalid_opcode(ctsio); 5405 ctl_done((union ctl_io *)ctsio); 5406 return (CTL_RETVAL_COMPLETE); 5407 } 5408 5409 /* 5410 * XXX KDM Copan-specific offline behavior. 5411 * Figure out a reasonable way to port this? 5412 */ 5413#ifdef NEEDTOPORT 5414 mtx_lock(&lun->lun_lock); 5415 5416 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5417 && (lun->flags & CTL_LUN_OFFLINE)) { 5418 /* 5419 * If the LUN is offline, and the on/offline bit isn't set, 5420 * reject the start or stop. Otherwise, let it through. 5421 */ 5422 mtx_unlock(&lun->lun_lock); 5423 ctl_set_lun_not_ready(ctsio); 5424 ctl_done((union ctl_io *)ctsio); 5425 } else { 5426 mtx_unlock(&lun->lun_lock); 5427#endif /* NEEDTOPORT */ 5428 /* 5429 * This could be a start or a stop when we're online, 5430 * or a stop/offline or start/online. A start or stop when 5431 * we're offline is covered in the case above. 5432 */ 5433 /* 5434 * In the non-immediate case, we send the request to 5435 * the backend and return status to the user when 5436 * it is done. 5437 * 5438 * In the immediate case, we allocate a new ctl_io 5439 * to hold a copy of the request, and send that to 5440 * the backend. We then set good status on the 5441 * user's request and return it immediately. 5442 */ 5443 if (cdb->byte2 & SSS_IMMED) { 5444 union ctl_io *new_io; 5445 5446 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5447 if (new_io == NULL) { 5448 ctl_set_busy(ctsio); 5449 ctl_done((union ctl_io *)ctsio); 5450 } else { 5451 ctl_copy_io((union ctl_io *)ctsio, 5452 new_io); 5453 retval = lun->backend->config_write(new_io); 5454 ctl_set_success(ctsio); 5455 ctl_done((union ctl_io *)ctsio); 5456 } 5457 } else { 5458 retval = lun->backend->config_write( 5459 (union ctl_io *)ctsio); 5460 } 5461#ifdef NEEDTOPORT 5462 } 5463#endif 5464 return (retval); 5465} 5466 5467/* 5468 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5469 * we don't really do anything with the LBA and length fields if the user 5470 * passes them in. Instead we'll just flush out the cache for the entire 5471 * LUN. 5472 */ 5473int 5474ctl_sync_cache(struct ctl_scsiio *ctsio) 5475{ 5476 struct ctl_lun *lun; 5477 struct ctl_softc *ctl_softc; 5478 uint64_t starting_lba; 5479 uint32_t block_count; 5480 int retval; 5481 5482 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5483 5484 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5485 ctl_softc = control_softc; 5486 retval = 0; 5487 5488 switch (ctsio->cdb[0]) { 5489 case SYNCHRONIZE_CACHE: { 5490 struct scsi_sync_cache *cdb; 5491 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5492 5493 starting_lba = scsi_4btoul(cdb->begin_lba); 5494 block_count = scsi_2btoul(cdb->lb_count); 5495 break; 5496 } 5497 case SYNCHRONIZE_CACHE_16: { 5498 struct scsi_sync_cache_16 *cdb; 5499 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5500 5501 starting_lba = scsi_8btou64(cdb->begin_lba); 5502 block_count = scsi_4btoul(cdb->lb_count); 5503 break; 5504 } 5505 default: 5506 ctl_set_invalid_opcode(ctsio); 5507 ctl_done((union ctl_io *)ctsio); 5508 goto bailout; 5509 break; /* NOTREACHED */ 5510 } 5511 5512 /* 5513 * We check the LBA and length, but don't do anything with them. 5514 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5515 * get flushed. This check will just help satisfy anyone who wants 5516 * to see an error for an out of range LBA. 5517 */ 5518 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5519 ctl_set_lba_out_of_range(ctsio); 5520 ctl_done((union ctl_io *)ctsio); 5521 goto bailout; 5522 } 5523 5524 /* 5525 * If this LUN has no backend, we can't flush the cache anyway. 5526 */ 5527 if (lun->backend == NULL) { 5528 ctl_set_invalid_opcode(ctsio); 5529 ctl_done((union ctl_io *)ctsio); 5530 goto bailout; 5531 } 5532 5533 /* 5534 * Check to see whether we're configured to send the SYNCHRONIZE 5535 * CACHE command directly to the back end. 5536 */ 5537 mtx_lock(&lun->lun_lock); 5538 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5539 && (++(lun->sync_count) >= lun->sync_interval)) { 5540 lun->sync_count = 0; 5541 mtx_unlock(&lun->lun_lock); 5542 retval = lun->backend->config_write((union ctl_io *)ctsio); 5543 } else { 5544 mtx_unlock(&lun->lun_lock); 5545 ctl_set_success(ctsio); 5546 ctl_done((union ctl_io *)ctsio); 5547 } 5548 5549bailout: 5550 5551 return (retval); 5552} 5553 5554int 5555ctl_format(struct ctl_scsiio *ctsio) 5556{ 5557 struct scsi_format *cdb; 5558 struct ctl_lun *lun; 5559 struct ctl_softc *ctl_softc; 5560 int length, defect_list_len; 5561 5562 CTL_DEBUG_PRINT(("ctl_format\n")); 5563 5564 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5565 ctl_softc = control_softc; 5566 5567 cdb = (struct scsi_format *)ctsio->cdb; 5568 5569 length = 0; 5570 if (cdb->byte2 & SF_FMTDATA) { 5571 if (cdb->byte2 & SF_LONGLIST) 5572 length = sizeof(struct scsi_format_header_long); 5573 else 5574 length = sizeof(struct scsi_format_header_short); 5575 } 5576 5577 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5578 && (length > 0)) { 5579 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5580 ctsio->kern_data_len = length; 5581 ctsio->kern_total_len = length; 5582 ctsio->kern_data_resid = 0; 5583 ctsio->kern_rel_offset = 0; 5584 ctsio->kern_sg_entries = 0; 5585 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5586 ctsio->be_move_done = ctl_config_move_done; 5587 ctl_datamove((union ctl_io *)ctsio); 5588 5589 return (CTL_RETVAL_COMPLETE); 5590 } 5591 5592 defect_list_len = 0; 5593 5594 if (cdb->byte2 & SF_FMTDATA) { 5595 if (cdb->byte2 & SF_LONGLIST) { 5596 struct scsi_format_header_long *header; 5597 5598 header = (struct scsi_format_header_long *) 5599 ctsio->kern_data_ptr; 5600 5601 defect_list_len = scsi_4btoul(header->defect_list_len); 5602 if (defect_list_len != 0) { 5603 ctl_set_invalid_field(ctsio, 5604 /*sks_valid*/ 1, 5605 /*command*/ 0, 5606 /*field*/ 2, 5607 /*bit_valid*/ 0, 5608 /*bit*/ 0); 5609 goto bailout; 5610 } 5611 } else { 5612 struct scsi_format_header_short *header; 5613 5614 header = (struct scsi_format_header_short *) 5615 ctsio->kern_data_ptr; 5616 5617 defect_list_len = scsi_2btoul(header->defect_list_len); 5618 if (defect_list_len != 0) { 5619 ctl_set_invalid_field(ctsio, 5620 /*sks_valid*/ 1, 5621 /*command*/ 0, 5622 /*field*/ 2, 5623 /*bit_valid*/ 0, 5624 /*bit*/ 0); 5625 goto bailout; 5626 } 5627 } 5628 } 5629 5630 /* 5631 * The format command will clear out the "Medium format corrupted" 5632 * status if set by the configuration code. That status is really 5633 * just a way to notify the host that we have lost the media, and 5634 * get them to issue a command that will basically make them think 5635 * they're blowing away the media. 5636 */ 5637 mtx_lock(&lun->lun_lock); 5638 lun->flags &= ~CTL_LUN_INOPERABLE; 5639 mtx_unlock(&lun->lun_lock); 5640 5641 ctsio->scsi_status = SCSI_STATUS_OK; 5642 ctsio->io_hdr.status = CTL_SUCCESS; 5643bailout: 5644 5645 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5646 free(ctsio->kern_data_ptr, M_CTL); 5647 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5648 } 5649 5650 ctl_done((union ctl_io *)ctsio); 5651 return (CTL_RETVAL_COMPLETE); 5652} 5653 5654int 5655ctl_read_buffer(struct ctl_scsiio *ctsio) 5656{ 5657 struct scsi_read_buffer *cdb; 5658 struct ctl_lun *lun; 5659 int buffer_offset, len; 5660 static uint8_t descr[4]; 5661 static uint8_t echo_descr[4] = { 0 }; 5662 5663 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5664 5665 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5666 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5667 5668 if (lun->flags & CTL_LUN_PR_RESERVED) { 5669 uint32_t residx; 5670 5671 /* 5672 * XXX KDM need a lock here. 5673 */ 5674 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5675 if ((lun->res_type == SPR_TYPE_EX_AC 5676 && residx != lun->pr_res_idx) 5677 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5678 || lun->res_type == SPR_TYPE_EX_AC_AR) 5679 && !lun->per_res[residx].registered)) { 5680 ctl_set_reservation_conflict(ctsio); 5681 ctl_done((union ctl_io *)ctsio); 5682 return (CTL_RETVAL_COMPLETE); 5683 } 5684 } 5685 5686 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5687 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5688 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5689 ctl_set_invalid_field(ctsio, 5690 /*sks_valid*/ 1, 5691 /*command*/ 1, 5692 /*field*/ 1, 5693 /*bit_valid*/ 1, 5694 /*bit*/ 4); 5695 ctl_done((union ctl_io *)ctsio); 5696 return (CTL_RETVAL_COMPLETE); 5697 } 5698 5699 len = scsi_3btoul(cdb->length); 5700 buffer_offset = scsi_3btoul(cdb->offset); 5701 5702 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5703 ctl_set_invalid_field(ctsio, 5704 /*sks_valid*/ 1, 5705 /*command*/ 1, 5706 /*field*/ 6, 5707 /*bit_valid*/ 0, 5708 /*bit*/ 0); 5709 ctl_done((union ctl_io *)ctsio); 5710 return (CTL_RETVAL_COMPLETE); 5711 } 5712 5713 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5714 descr[0] = 0; 5715 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5716 ctsio->kern_data_ptr = descr; 5717 len = min(len, sizeof(descr)); 5718 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5719 ctsio->kern_data_ptr = echo_descr; 5720 len = min(len, sizeof(echo_descr)); 5721 } else 5722 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5723 ctsio->kern_data_len = len; 5724 ctsio->kern_total_len = len; 5725 ctsio->kern_data_resid = 0; 5726 ctsio->kern_rel_offset = 0; 5727 ctsio->kern_sg_entries = 0; 5728 ctsio->be_move_done = ctl_config_move_done; 5729 ctl_datamove((union ctl_io *)ctsio); 5730 5731 return (CTL_RETVAL_COMPLETE); 5732} 5733 5734int 5735ctl_write_buffer(struct ctl_scsiio *ctsio) 5736{ 5737 struct scsi_write_buffer *cdb; 5738 struct ctl_lun *lun; 5739 int buffer_offset, len; 5740 5741 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5742 5743 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5744 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5745 5746 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5747 ctl_set_invalid_field(ctsio, 5748 /*sks_valid*/ 1, 5749 /*command*/ 1, 5750 /*field*/ 1, 5751 /*bit_valid*/ 1, 5752 /*bit*/ 4); 5753 ctl_done((union ctl_io *)ctsio); 5754 return (CTL_RETVAL_COMPLETE); 5755 } 5756 5757 len = scsi_3btoul(cdb->length); 5758 buffer_offset = scsi_3btoul(cdb->offset); 5759 5760 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5761 ctl_set_invalid_field(ctsio, 5762 /*sks_valid*/ 1, 5763 /*command*/ 1, 5764 /*field*/ 6, 5765 /*bit_valid*/ 0, 5766 /*bit*/ 0); 5767 ctl_done((union ctl_io *)ctsio); 5768 return (CTL_RETVAL_COMPLETE); 5769 } 5770 5771 /* 5772 * If we've got a kernel request that hasn't been malloced yet, 5773 * malloc it and tell the caller the data buffer is here. 5774 */ 5775 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5776 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5777 ctsio->kern_data_len = len; 5778 ctsio->kern_total_len = len; 5779 ctsio->kern_data_resid = 0; 5780 ctsio->kern_rel_offset = 0; 5781 ctsio->kern_sg_entries = 0; 5782 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5783 ctsio->be_move_done = ctl_config_move_done; 5784 ctl_datamove((union ctl_io *)ctsio); 5785 5786 return (CTL_RETVAL_COMPLETE); 5787 } 5788 5789 ctl_done((union ctl_io *)ctsio); 5790 5791 return (CTL_RETVAL_COMPLETE); 5792} 5793 5794int 5795ctl_write_same(struct ctl_scsiio *ctsio) 5796{ 5797 struct ctl_lun *lun; 5798 struct ctl_lba_len_flags *lbalen; 5799 uint64_t lba; 5800 uint32_t num_blocks; 5801 int len, retval; 5802 uint8_t byte2; 5803 5804 retval = CTL_RETVAL_COMPLETE; 5805 5806 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5807 5808 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5809 5810 switch (ctsio->cdb[0]) { 5811 case WRITE_SAME_10: { 5812 struct scsi_write_same_10 *cdb; 5813 5814 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5815 5816 lba = scsi_4btoul(cdb->addr); 5817 num_blocks = scsi_2btoul(cdb->length); 5818 byte2 = cdb->byte2; 5819 break; 5820 } 5821 case WRITE_SAME_16: { 5822 struct scsi_write_same_16 *cdb; 5823 5824 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5825 5826 lba = scsi_8btou64(cdb->addr); 5827 num_blocks = scsi_4btoul(cdb->length); 5828 byte2 = cdb->byte2; 5829 break; 5830 } 5831 default: 5832 /* 5833 * We got a command we don't support. This shouldn't 5834 * happen, commands should be filtered out above us. 5835 */ 5836 ctl_set_invalid_opcode(ctsio); 5837 ctl_done((union ctl_io *)ctsio); 5838 5839 return (CTL_RETVAL_COMPLETE); 5840 break; /* NOTREACHED */ 5841 } 5842 5843 /* 5844 * The first check is to make sure we're in bounds, the second 5845 * check is to catch wrap-around problems. If the lba + num blocks 5846 * is less than the lba, then we've wrapped around and the block 5847 * range is invalid anyway. 5848 */ 5849 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5850 || ((lba + num_blocks) < lba)) { 5851 ctl_set_lba_out_of_range(ctsio); 5852 ctl_done((union ctl_io *)ctsio); 5853 return (CTL_RETVAL_COMPLETE); 5854 } 5855 5856 /* Zero number of blocks means "to the last logical block" */ 5857 if (num_blocks == 0) { 5858 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5859 ctl_set_invalid_field(ctsio, 5860 /*sks_valid*/ 0, 5861 /*command*/ 1, 5862 /*field*/ 0, 5863 /*bit_valid*/ 0, 5864 /*bit*/ 0); 5865 ctl_done((union ctl_io *)ctsio); 5866 return (CTL_RETVAL_COMPLETE); 5867 } 5868 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5869 } 5870 5871 len = lun->be_lun->blocksize; 5872 5873 /* 5874 * If we've got a kernel request that hasn't been malloced yet, 5875 * malloc it and tell the caller the data buffer is here. 5876 */ 5877 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5878 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5879 ctsio->kern_data_len = len; 5880 ctsio->kern_total_len = len; 5881 ctsio->kern_data_resid = 0; 5882 ctsio->kern_rel_offset = 0; 5883 ctsio->kern_sg_entries = 0; 5884 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5885 ctsio->be_move_done = ctl_config_move_done; 5886 ctl_datamove((union ctl_io *)ctsio); 5887 5888 return (CTL_RETVAL_COMPLETE); 5889 } 5890 5891 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5892 lbalen->lba = lba; 5893 lbalen->len = num_blocks; 5894 lbalen->flags = byte2; 5895 retval = lun->backend->config_write((union ctl_io *)ctsio); 5896 5897 return (retval); 5898} 5899 5900int 5901ctl_unmap(struct ctl_scsiio *ctsio) 5902{ 5903 struct ctl_lun *lun; 5904 struct scsi_unmap *cdb; 5905 struct ctl_ptr_len_flags *ptrlen; 5906 struct scsi_unmap_header *hdr; 5907 struct scsi_unmap_desc *buf, *end; 5908 uint64_t lba; 5909 uint32_t num_blocks; 5910 int len, retval; 5911 uint8_t byte2; 5912 5913 retval = CTL_RETVAL_COMPLETE; 5914 5915 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5916 5917 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5918 cdb = (struct scsi_unmap *)ctsio->cdb; 5919 5920 len = scsi_2btoul(cdb->length); 5921 byte2 = cdb->byte2; 5922 5923 /* 5924 * If we've got a kernel request that hasn't been malloced yet, 5925 * malloc it and tell the caller the data buffer is here. 5926 */ 5927 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5928 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5929 ctsio->kern_data_len = len; 5930 ctsio->kern_total_len = len; 5931 ctsio->kern_data_resid = 0; 5932 ctsio->kern_rel_offset = 0; 5933 ctsio->kern_sg_entries = 0; 5934 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5935 ctsio->be_move_done = ctl_config_move_done; 5936 ctl_datamove((union ctl_io *)ctsio); 5937 5938 return (CTL_RETVAL_COMPLETE); 5939 } 5940 5941 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5942 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5943 if (len < sizeof (*hdr) || 5944 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5945 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5946 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5947 ctl_set_invalid_field(ctsio, 5948 /*sks_valid*/ 0, 5949 /*command*/ 0, 5950 /*field*/ 0, 5951 /*bit_valid*/ 0, 5952 /*bit*/ 0); 5953 ctl_done((union ctl_io *)ctsio); 5954 return (CTL_RETVAL_COMPLETE); 5955 } 5956 len = scsi_2btoul(hdr->desc_length); 5957 buf = (struct scsi_unmap_desc *)(hdr + 1); 5958 end = buf + len / sizeof(*buf); 5959 5960 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5961 ptrlen->ptr = (void *)buf; 5962 ptrlen->len = len; 5963 ptrlen->flags = byte2; 5964 5965 for (; buf < end; buf++) { 5966 lba = scsi_8btou64(buf->lba); 5967 num_blocks = scsi_4btoul(buf->length); 5968 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5969 || ((lba + num_blocks) < lba)) { 5970 ctl_set_lba_out_of_range(ctsio); 5971 ctl_done((union ctl_io *)ctsio); 5972 return (CTL_RETVAL_COMPLETE); 5973 } 5974 } 5975 5976 retval = lun->backend->config_write((union ctl_io *)ctsio); 5977 5978 return (retval); 5979} 5980 5981/* 5982 * Note that this function currently doesn't actually do anything inside 5983 * CTL to enforce things if the DQue bit is turned on. 5984 * 5985 * Also note that this function can't be used in the default case, because 5986 * the DQue bit isn't set in the changeable mask for the control mode page 5987 * anyway. This is just here as an example for how to implement a page 5988 * handler, and a placeholder in case we want to allow the user to turn 5989 * tagged queueing on and off. 5990 * 5991 * The D_SENSE bit handling is functional, however, and will turn 5992 * descriptor sense on and off for a given LUN. 5993 */ 5994int 5995ctl_control_page_handler(struct ctl_scsiio *ctsio, 5996 struct ctl_page_index *page_index, uint8_t *page_ptr) 5997{ 5998 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5999 struct ctl_lun *lun; 6000 struct ctl_softc *softc; 6001 int set_ua; 6002 uint32_t initidx; 6003 6004 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6005 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6006 set_ua = 0; 6007 6008 user_cp = (struct scsi_control_page *)page_ptr; 6009 current_cp = (struct scsi_control_page *) 6010 (page_index->page_data + (page_index->page_len * 6011 CTL_PAGE_CURRENT)); 6012 saved_cp = (struct scsi_control_page *) 6013 (page_index->page_data + (page_index->page_len * 6014 CTL_PAGE_SAVED)); 6015 6016 softc = control_softc; 6017 6018 mtx_lock(&lun->lun_lock); 6019 if (((current_cp->rlec & SCP_DSENSE) == 0) 6020 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6021 /* 6022 * Descriptor sense is currently turned off and the user 6023 * wants to turn it on. 6024 */ 6025 current_cp->rlec |= SCP_DSENSE; 6026 saved_cp->rlec |= SCP_DSENSE; 6027 lun->flags |= CTL_LUN_SENSE_DESC; 6028 set_ua = 1; 6029 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6030 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6031 /* 6032 * Descriptor sense is currently turned on, and the user 6033 * wants to turn it off. 6034 */ 6035 current_cp->rlec &= ~SCP_DSENSE; 6036 saved_cp->rlec &= ~SCP_DSENSE; 6037 lun->flags &= ~CTL_LUN_SENSE_DESC; 6038 set_ua = 1; 6039 } 6040 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6041 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6042#ifdef NEEDTOPORT 6043 csevent_log(CSC_CTL | CSC_SHELF_SW | 6044 CTL_UNTAG_TO_UNTAG, 6045 csevent_LogType_Trace, 6046 csevent_Severity_Information, 6047 csevent_AlertLevel_Green, 6048 csevent_FRU_Firmware, 6049 csevent_FRU_Unknown, 6050 "Received untagged to untagged transition"); 6051#endif /* NEEDTOPORT */ 6052 } else { 6053#ifdef NEEDTOPORT 6054 csevent_log(CSC_CTL | CSC_SHELF_SW | 6055 CTL_UNTAG_TO_TAG, 6056 csevent_LogType_ConfigChange, 6057 csevent_Severity_Information, 6058 csevent_AlertLevel_Green, 6059 csevent_FRU_Firmware, 6060 csevent_FRU_Unknown, 6061 "Received untagged to tagged " 6062 "queueing transition"); 6063#endif /* NEEDTOPORT */ 6064 6065 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6066 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6067 set_ua = 1; 6068 } 6069 } else { 6070 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6071#ifdef NEEDTOPORT 6072 csevent_log(CSC_CTL | CSC_SHELF_SW | 6073 CTL_TAG_TO_UNTAG, 6074 csevent_LogType_ConfigChange, 6075 csevent_Severity_Warning, 6076 csevent_AlertLevel_Yellow, 6077 csevent_FRU_Firmware, 6078 csevent_FRU_Unknown, 6079 "Received tagged queueing to untagged " 6080 "transition"); 6081#endif /* NEEDTOPORT */ 6082 6083 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6084 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6085 set_ua = 1; 6086 } else { 6087#ifdef NEEDTOPORT 6088 csevent_log(CSC_CTL | CSC_SHELF_SW | 6089 CTL_TAG_TO_TAG, 6090 csevent_LogType_Trace, 6091 csevent_Severity_Information, 6092 csevent_AlertLevel_Green, 6093 csevent_FRU_Firmware, 6094 csevent_FRU_Unknown, 6095 "Received tagged queueing to tagged " 6096 "queueing transition"); 6097#endif /* NEEDTOPORT */ 6098 } 6099 } 6100 if (set_ua != 0) { 6101 int i; 6102 /* 6103 * Let other initiators know that the mode 6104 * parameters for this LUN have changed. 6105 */ 6106 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6107 if (i == initidx) 6108 continue; 6109 6110 lun->pending_sense[i].ua_pending |= 6111 CTL_UA_MODE_CHANGE; 6112 } 6113 } 6114 mtx_unlock(&lun->lun_lock); 6115 6116 return (0); 6117} 6118 6119int 6120ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6121 struct ctl_page_index *page_index, uint8_t *page_ptr) 6122{ 6123 return (0); 6124} 6125 6126int 6127ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6128 struct ctl_page_index *page_index, int pc) 6129{ 6130 struct copan_power_subpage *page; 6131 6132 page = (struct copan_power_subpage *)page_index->page_data + 6133 (page_index->page_len * pc); 6134 6135 switch (pc) { 6136 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6137 /* 6138 * We don't update the changable bits for this page. 6139 */ 6140 break; 6141 case SMS_PAGE_CTRL_CURRENT >> 6: 6142 case SMS_PAGE_CTRL_DEFAULT >> 6: 6143 case SMS_PAGE_CTRL_SAVED >> 6: 6144#ifdef NEEDTOPORT 6145 ctl_update_power_subpage(page); 6146#endif 6147 break; 6148 default: 6149#ifdef NEEDTOPORT 6150 EPRINT(0, "Invalid PC %d!!", pc); 6151#endif 6152 break; 6153 } 6154 return (0); 6155} 6156 6157 6158int 6159ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6160 struct ctl_page_index *page_index, uint8_t *page_ptr) 6161{ 6162 struct copan_aps_subpage *user_sp; 6163 struct copan_aps_subpage *current_sp; 6164 union ctl_modepage_info *modepage_info; 6165 struct ctl_softc *softc; 6166 struct ctl_lun *lun; 6167 int retval; 6168 6169 retval = CTL_RETVAL_COMPLETE; 6170 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6171 (page_index->page_len * CTL_PAGE_CURRENT)); 6172 softc = control_softc; 6173 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6174 6175 user_sp = (struct copan_aps_subpage *)page_ptr; 6176 6177 modepage_info = (union ctl_modepage_info *) 6178 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6179 6180 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6181 modepage_info->header.subpage = page_index->subpage; 6182 modepage_info->aps.lock_active = user_sp->lock_active; 6183 6184 mtx_lock(&softc->ctl_lock); 6185 6186 /* 6187 * If there is a request to lock the LUN and another LUN is locked 6188 * this is an error. If the requested LUN is already locked ignore 6189 * the request. If no LUN is locked attempt to lock it. 6190 * if there is a request to unlock the LUN and the LUN is currently 6191 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6192 * if another LUN is locked or no LUN is locked. 6193 */ 6194 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6195 if (softc->aps_locked_lun == lun->lun) { 6196 /* 6197 * This LUN is already locked, so we're done. 6198 */ 6199 retval = CTL_RETVAL_COMPLETE; 6200 } else if (softc->aps_locked_lun == 0) { 6201 /* 6202 * No one has the lock, pass the request to the 6203 * backend. 6204 */ 6205 retval = lun->backend->config_write( 6206 (union ctl_io *)ctsio); 6207 } else { 6208 /* 6209 * Someone else has the lock, throw out the request. 6210 */ 6211 ctl_set_already_locked(ctsio); 6212 free(ctsio->kern_data_ptr, M_CTL); 6213 ctl_done((union ctl_io *)ctsio); 6214 6215 /* 6216 * Set the return value so that ctl_do_mode_select() 6217 * won't try to complete the command. We already 6218 * completed it here. 6219 */ 6220 retval = CTL_RETVAL_ERROR; 6221 } 6222 } else if (softc->aps_locked_lun == lun->lun) { 6223 /* 6224 * This LUN is locked, so pass the unlock request to the 6225 * backend. 6226 */ 6227 retval = lun->backend->config_write((union ctl_io *)ctsio); 6228 } 6229 mtx_unlock(&softc->ctl_lock); 6230 6231 return (retval); 6232} 6233 6234int 6235ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6236 struct ctl_page_index *page_index, 6237 uint8_t *page_ptr) 6238{ 6239 uint8_t *c; 6240 int i; 6241 6242 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6243 ctl_time_io_secs = 6244 (c[0] << 8) | 6245 (c[1] << 0) | 6246 0; 6247 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6248 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6249 printf("page data:"); 6250 for (i=0; i<8; i++) 6251 printf(" %.2x",page_ptr[i]); 6252 printf("\n"); 6253 return (0); 6254} 6255 6256int 6257ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6258 struct ctl_page_index *page_index, 6259 int pc) 6260{ 6261 struct copan_debugconf_subpage *page; 6262 6263 page = (struct copan_debugconf_subpage *)page_index->page_data + 6264 (page_index->page_len * pc); 6265 6266 switch (pc) { 6267 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6268 case SMS_PAGE_CTRL_DEFAULT >> 6: 6269 case SMS_PAGE_CTRL_SAVED >> 6: 6270 /* 6271 * We don't update the changable or default bits for this page. 6272 */ 6273 break; 6274 case SMS_PAGE_CTRL_CURRENT >> 6: 6275 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6276 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6277 break; 6278 default: 6279#ifdef NEEDTOPORT 6280 EPRINT(0, "Invalid PC %d!!", pc); 6281#endif /* NEEDTOPORT */ 6282 break; 6283 } 6284 return (0); 6285} 6286 6287 6288static int 6289ctl_do_mode_select(union ctl_io *io) 6290{ 6291 struct scsi_mode_page_header *page_header; 6292 struct ctl_page_index *page_index; 6293 struct ctl_scsiio *ctsio; 6294 int control_dev, page_len; 6295 int page_len_offset, page_len_size; 6296 union ctl_modepage_info *modepage_info; 6297 struct ctl_lun *lun; 6298 int *len_left, *len_used; 6299 int retval, i; 6300 6301 ctsio = &io->scsiio; 6302 page_index = NULL; 6303 page_len = 0; 6304 retval = CTL_RETVAL_COMPLETE; 6305 6306 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6307 6308 if (lun->be_lun->lun_type != T_DIRECT) 6309 control_dev = 1; 6310 else 6311 control_dev = 0; 6312 6313 modepage_info = (union ctl_modepage_info *) 6314 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6315 len_left = &modepage_info->header.len_left; 6316 len_used = &modepage_info->header.len_used; 6317 6318do_next_page: 6319 6320 page_header = (struct scsi_mode_page_header *) 6321 (ctsio->kern_data_ptr + *len_used); 6322 6323 if (*len_left == 0) { 6324 free(ctsio->kern_data_ptr, M_CTL); 6325 ctl_set_success(ctsio); 6326 ctl_done((union ctl_io *)ctsio); 6327 return (CTL_RETVAL_COMPLETE); 6328 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6329 6330 free(ctsio->kern_data_ptr, M_CTL); 6331 ctl_set_param_len_error(ctsio); 6332 ctl_done((union ctl_io *)ctsio); 6333 return (CTL_RETVAL_COMPLETE); 6334 6335 } else if ((page_header->page_code & SMPH_SPF) 6336 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6337 6338 free(ctsio->kern_data_ptr, M_CTL); 6339 ctl_set_param_len_error(ctsio); 6340 ctl_done((union ctl_io *)ctsio); 6341 return (CTL_RETVAL_COMPLETE); 6342 } 6343 6344 6345 /* 6346 * XXX KDM should we do something with the block descriptor? 6347 */ 6348 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6349 6350 if ((control_dev != 0) 6351 && (lun->mode_pages.index[i].page_flags & 6352 CTL_PAGE_FLAG_DISK_ONLY)) 6353 continue; 6354 6355 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6356 (page_header->page_code & SMPH_PC_MASK)) 6357 continue; 6358 6359 /* 6360 * If neither page has a subpage code, then we've got a 6361 * match. 6362 */ 6363 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6364 && ((page_header->page_code & SMPH_SPF) == 0)) { 6365 page_index = &lun->mode_pages.index[i]; 6366 page_len = page_header->page_length; 6367 break; 6368 } 6369 6370 /* 6371 * If both pages have subpages, then the subpage numbers 6372 * have to match. 6373 */ 6374 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6375 && (page_header->page_code & SMPH_SPF)) { 6376 struct scsi_mode_page_header_sp *sph; 6377 6378 sph = (struct scsi_mode_page_header_sp *)page_header; 6379 6380 if (lun->mode_pages.index[i].subpage == 6381 sph->subpage) { 6382 page_index = &lun->mode_pages.index[i]; 6383 page_len = scsi_2btoul(sph->page_length); 6384 break; 6385 } 6386 } 6387 } 6388 6389 /* 6390 * If we couldn't find the page, or if we don't have a mode select 6391 * handler for it, send back an error to the user. 6392 */ 6393 if ((page_index == NULL) 6394 || (page_index->select_handler == NULL)) { 6395 ctl_set_invalid_field(ctsio, 6396 /*sks_valid*/ 1, 6397 /*command*/ 0, 6398 /*field*/ *len_used, 6399 /*bit_valid*/ 0, 6400 /*bit*/ 0); 6401 free(ctsio->kern_data_ptr, M_CTL); 6402 ctl_done((union ctl_io *)ctsio); 6403 return (CTL_RETVAL_COMPLETE); 6404 } 6405 6406 if (page_index->page_code & SMPH_SPF) { 6407 page_len_offset = 2; 6408 page_len_size = 2; 6409 } else { 6410 page_len_size = 1; 6411 page_len_offset = 1; 6412 } 6413 6414 /* 6415 * If the length the initiator gives us isn't the one we specify in 6416 * the mode page header, or if they didn't specify enough data in 6417 * the CDB to avoid truncating this page, kick out the request. 6418 */ 6419 if ((page_len != (page_index->page_len - page_len_offset - 6420 page_len_size)) 6421 || (*len_left < page_index->page_len)) { 6422 6423 6424 ctl_set_invalid_field(ctsio, 6425 /*sks_valid*/ 1, 6426 /*command*/ 0, 6427 /*field*/ *len_used + page_len_offset, 6428 /*bit_valid*/ 0, 6429 /*bit*/ 0); 6430 free(ctsio->kern_data_ptr, M_CTL); 6431 ctl_done((union ctl_io *)ctsio); 6432 return (CTL_RETVAL_COMPLETE); 6433 } 6434 6435 /* 6436 * Run through the mode page, checking to make sure that the bits 6437 * the user changed are actually legal for him to change. 6438 */ 6439 for (i = 0; i < page_index->page_len; i++) { 6440 uint8_t *user_byte, *change_mask, *current_byte; 6441 int bad_bit; 6442 int j; 6443 6444 user_byte = (uint8_t *)page_header + i; 6445 change_mask = page_index->page_data + 6446 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6447 current_byte = page_index->page_data + 6448 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6449 6450 /* 6451 * Check to see whether the user set any bits in this byte 6452 * that he is not allowed to set. 6453 */ 6454 if ((*user_byte & ~(*change_mask)) == 6455 (*current_byte & ~(*change_mask))) 6456 continue; 6457 6458 /* 6459 * Go through bit by bit to determine which one is illegal. 6460 */ 6461 bad_bit = 0; 6462 for (j = 7; j >= 0; j--) { 6463 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6464 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6465 bad_bit = i; 6466 break; 6467 } 6468 } 6469 ctl_set_invalid_field(ctsio, 6470 /*sks_valid*/ 1, 6471 /*command*/ 0, 6472 /*field*/ *len_used + i, 6473 /*bit_valid*/ 1, 6474 /*bit*/ bad_bit); 6475 free(ctsio->kern_data_ptr, M_CTL); 6476 ctl_done((union ctl_io *)ctsio); 6477 return (CTL_RETVAL_COMPLETE); 6478 } 6479 6480 /* 6481 * Decrement these before we call the page handler, since we may 6482 * end up getting called back one way or another before the handler 6483 * returns to this context. 6484 */ 6485 *len_left -= page_index->page_len; 6486 *len_used += page_index->page_len; 6487 6488 retval = page_index->select_handler(ctsio, page_index, 6489 (uint8_t *)page_header); 6490 6491 /* 6492 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6493 * wait until this queued command completes to finish processing 6494 * the mode page. If it returns anything other than 6495 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6496 * already set the sense information, freed the data pointer, and 6497 * completed the io for us. 6498 */ 6499 if (retval != CTL_RETVAL_COMPLETE) 6500 goto bailout_no_done; 6501 6502 /* 6503 * If the initiator sent us more than one page, parse the next one. 6504 */ 6505 if (*len_left > 0) 6506 goto do_next_page; 6507 6508 ctl_set_success(ctsio); 6509 free(ctsio->kern_data_ptr, M_CTL); 6510 ctl_done((union ctl_io *)ctsio); 6511 6512bailout_no_done: 6513 6514 return (CTL_RETVAL_COMPLETE); 6515 6516} 6517 6518int 6519ctl_mode_select(struct ctl_scsiio *ctsio) 6520{ 6521 int param_len, pf, sp; 6522 int header_size, bd_len; 6523 int len_left, len_used; 6524 struct ctl_page_index *page_index; 6525 struct ctl_lun *lun; 6526 int control_dev, page_len; 6527 union ctl_modepage_info *modepage_info; 6528 int retval; 6529 6530 pf = 0; 6531 sp = 0; 6532 page_len = 0; 6533 len_used = 0; 6534 len_left = 0; 6535 retval = 0; 6536 bd_len = 0; 6537 page_index = NULL; 6538 6539 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6540 6541 if (lun->be_lun->lun_type != T_DIRECT) 6542 control_dev = 1; 6543 else 6544 control_dev = 0; 6545 6546 switch (ctsio->cdb[0]) { 6547 case MODE_SELECT_6: { 6548 struct scsi_mode_select_6 *cdb; 6549 6550 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6551 6552 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6553 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6554 6555 param_len = cdb->length; 6556 header_size = sizeof(struct scsi_mode_header_6); 6557 break; 6558 } 6559 case MODE_SELECT_10: { 6560 struct scsi_mode_select_10 *cdb; 6561 6562 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6563 6564 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6565 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6566 6567 param_len = scsi_2btoul(cdb->length); 6568 header_size = sizeof(struct scsi_mode_header_10); 6569 break; 6570 } 6571 default: 6572 ctl_set_invalid_opcode(ctsio); 6573 ctl_done((union ctl_io *)ctsio); 6574 return (CTL_RETVAL_COMPLETE); 6575 break; /* NOTREACHED */ 6576 } 6577 6578 /* 6579 * From SPC-3: 6580 * "A parameter list length of zero indicates that the Data-Out Buffer 6581 * shall be empty. This condition shall not be considered as an error." 6582 */ 6583 if (param_len == 0) { 6584 ctl_set_success(ctsio); 6585 ctl_done((union ctl_io *)ctsio); 6586 return (CTL_RETVAL_COMPLETE); 6587 } 6588 6589 /* 6590 * Since we'll hit this the first time through, prior to 6591 * allocation, we don't need to free a data buffer here. 6592 */ 6593 if (param_len < header_size) { 6594 ctl_set_param_len_error(ctsio); 6595 ctl_done((union ctl_io *)ctsio); 6596 return (CTL_RETVAL_COMPLETE); 6597 } 6598 6599 /* 6600 * Allocate the data buffer and grab the user's data. In theory, 6601 * we shouldn't have to sanity check the parameter list length here 6602 * because the maximum size is 64K. We should be able to malloc 6603 * that much without too many problems. 6604 */ 6605 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6606 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6607 ctsio->kern_data_len = param_len; 6608 ctsio->kern_total_len = param_len; 6609 ctsio->kern_data_resid = 0; 6610 ctsio->kern_rel_offset = 0; 6611 ctsio->kern_sg_entries = 0; 6612 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6613 ctsio->be_move_done = ctl_config_move_done; 6614 ctl_datamove((union ctl_io *)ctsio); 6615 6616 return (CTL_RETVAL_COMPLETE); 6617 } 6618 6619 switch (ctsio->cdb[0]) { 6620 case MODE_SELECT_6: { 6621 struct scsi_mode_header_6 *mh6; 6622 6623 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6624 bd_len = mh6->blk_desc_len; 6625 break; 6626 } 6627 case MODE_SELECT_10: { 6628 struct scsi_mode_header_10 *mh10; 6629 6630 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6631 bd_len = scsi_2btoul(mh10->blk_desc_len); 6632 break; 6633 } 6634 default: 6635 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6636 break; 6637 } 6638 6639 if (param_len < (header_size + bd_len)) { 6640 free(ctsio->kern_data_ptr, M_CTL); 6641 ctl_set_param_len_error(ctsio); 6642 ctl_done((union ctl_io *)ctsio); 6643 return (CTL_RETVAL_COMPLETE); 6644 } 6645 6646 /* 6647 * Set the IO_CONT flag, so that if this I/O gets passed to 6648 * ctl_config_write_done(), it'll get passed back to 6649 * ctl_do_mode_select() for further processing, or completion if 6650 * we're all done. 6651 */ 6652 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6653 ctsio->io_cont = ctl_do_mode_select; 6654 6655 modepage_info = (union ctl_modepage_info *) 6656 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6657 6658 memset(modepage_info, 0, sizeof(*modepage_info)); 6659 6660 len_left = param_len - header_size - bd_len; 6661 len_used = header_size + bd_len; 6662 6663 modepage_info->header.len_left = len_left; 6664 modepage_info->header.len_used = len_used; 6665 6666 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6667} 6668 6669int 6670ctl_mode_sense(struct ctl_scsiio *ctsio) 6671{ 6672 struct ctl_lun *lun; 6673 int pc, page_code, dbd, llba, subpage; 6674 int alloc_len, page_len, header_len, total_len; 6675 struct scsi_mode_block_descr *block_desc; 6676 struct ctl_page_index *page_index; 6677 int control_dev; 6678 6679 dbd = 0; 6680 llba = 0; 6681 block_desc = NULL; 6682 page_index = NULL; 6683 6684 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6685 6686 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6687 6688 if (lun->be_lun->lun_type != T_DIRECT) 6689 control_dev = 1; 6690 else 6691 control_dev = 0; 6692 6693 if (lun->flags & CTL_LUN_PR_RESERVED) { 6694 uint32_t residx; 6695 6696 /* 6697 * XXX KDM need a lock here. 6698 */ 6699 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6700 if ((lun->res_type == SPR_TYPE_EX_AC 6701 && residx != lun->pr_res_idx) 6702 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6703 || lun->res_type == SPR_TYPE_EX_AC_AR) 6704 && !lun->per_res[residx].registered)) { 6705 ctl_set_reservation_conflict(ctsio); 6706 ctl_done((union ctl_io *)ctsio); 6707 return (CTL_RETVAL_COMPLETE); 6708 } 6709 } 6710 6711 switch (ctsio->cdb[0]) { 6712 case MODE_SENSE_6: { 6713 struct scsi_mode_sense_6 *cdb; 6714 6715 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6716 6717 header_len = sizeof(struct scsi_mode_hdr_6); 6718 if (cdb->byte2 & SMS_DBD) 6719 dbd = 1; 6720 else 6721 header_len += sizeof(struct scsi_mode_block_descr); 6722 6723 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6724 page_code = cdb->page & SMS_PAGE_CODE; 6725 subpage = cdb->subpage; 6726 alloc_len = cdb->length; 6727 break; 6728 } 6729 case MODE_SENSE_10: { 6730 struct scsi_mode_sense_10 *cdb; 6731 6732 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6733 6734 header_len = sizeof(struct scsi_mode_hdr_10); 6735 6736 if (cdb->byte2 & SMS_DBD) 6737 dbd = 1; 6738 else 6739 header_len += sizeof(struct scsi_mode_block_descr); 6740 if (cdb->byte2 & SMS10_LLBAA) 6741 llba = 1; 6742 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6743 page_code = cdb->page & SMS_PAGE_CODE; 6744 subpage = cdb->subpage; 6745 alloc_len = scsi_2btoul(cdb->length); 6746 break; 6747 } 6748 default: 6749 ctl_set_invalid_opcode(ctsio); 6750 ctl_done((union ctl_io *)ctsio); 6751 return (CTL_RETVAL_COMPLETE); 6752 break; /* NOTREACHED */ 6753 } 6754 6755 /* 6756 * We have to make a first pass through to calculate the size of 6757 * the pages that match the user's query. Then we allocate enough 6758 * memory to hold it, and actually copy the data into the buffer. 6759 */ 6760 switch (page_code) { 6761 case SMS_ALL_PAGES_PAGE: { 6762 int i; 6763 6764 page_len = 0; 6765 6766 /* 6767 * At the moment, values other than 0 and 0xff here are 6768 * reserved according to SPC-3. 6769 */ 6770 if ((subpage != SMS_SUBPAGE_PAGE_0) 6771 && (subpage != SMS_SUBPAGE_ALL)) { 6772 ctl_set_invalid_field(ctsio, 6773 /*sks_valid*/ 1, 6774 /*command*/ 1, 6775 /*field*/ 3, 6776 /*bit_valid*/ 0, 6777 /*bit*/ 0); 6778 ctl_done((union ctl_io *)ctsio); 6779 return (CTL_RETVAL_COMPLETE); 6780 } 6781 6782 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6783 if ((control_dev != 0) 6784 && (lun->mode_pages.index[i].page_flags & 6785 CTL_PAGE_FLAG_DISK_ONLY)) 6786 continue; 6787 6788 /* 6789 * We don't use this subpage if the user didn't 6790 * request all subpages. 6791 */ 6792 if ((lun->mode_pages.index[i].subpage != 0) 6793 && (subpage == SMS_SUBPAGE_PAGE_0)) 6794 continue; 6795 6796#if 0 6797 printf("found page %#x len %d\n", 6798 lun->mode_pages.index[i].page_code & 6799 SMPH_PC_MASK, 6800 lun->mode_pages.index[i].page_len); 6801#endif 6802 page_len += lun->mode_pages.index[i].page_len; 6803 } 6804 break; 6805 } 6806 default: { 6807 int i; 6808 6809 page_len = 0; 6810 6811 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6812 /* Look for the right page code */ 6813 if ((lun->mode_pages.index[i].page_code & 6814 SMPH_PC_MASK) != page_code) 6815 continue; 6816 6817 /* Look for the right subpage or the subpage wildcard*/ 6818 if ((lun->mode_pages.index[i].subpage != subpage) 6819 && (subpage != SMS_SUBPAGE_ALL)) 6820 continue; 6821 6822 /* Make sure the page is supported for this dev type */ 6823 if ((control_dev != 0) 6824 && (lun->mode_pages.index[i].page_flags & 6825 CTL_PAGE_FLAG_DISK_ONLY)) 6826 continue; 6827 6828#if 0 6829 printf("found page %#x len %d\n", 6830 lun->mode_pages.index[i].page_code & 6831 SMPH_PC_MASK, 6832 lun->mode_pages.index[i].page_len); 6833#endif 6834 6835 page_len += lun->mode_pages.index[i].page_len; 6836 } 6837 6838 if (page_len == 0) { 6839 ctl_set_invalid_field(ctsio, 6840 /*sks_valid*/ 1, 6841 /*command*/ 1, 6842 /*field*/ 2, 6843 /*bit_valid*/ 1, 6844 /*bit*/ 5); 6845 ctl_done((union ctl_io *)ctsio); 6846 return (CTL_RETVAL_COMPLETE); 6847 } 6848 break; 6849 } 6850 } 6851 6852 total_len = header_len + page_len; 6853#if 0 6854 printf("header_len = %d, page_len = %d, total_len = %d\n", 6855 header_len, page_len, total_len); 6856#endif 6857 6858 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6859 ctsio->kern_sg_entries = 0; 6860 ctsio->kern_data_resid = 0; 6861 ctsio->kern_rel_offset = 0; 6862 if (total_len < alloc_len) { 6863 ctsio->residual = alloc_len - total_len; 6864 ctsio->kern_data_len = total_len; 6865 ctsio->kern_total_len = total_len; 6866 } else { 6867 ctsio->residual = 0; 6868 ctsio->kern_data_len = alloc_len; 6869 ctsio->kern_total_len = alloc_len; 6870 } 6871 6872 switch (ctsio->cdb[0]) { 6873 case MODE_SENSE_6: { 6874 struct scsi_mode_hdr_6 *header; 6875 6876 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6877 6878 header->datalen = ctl_min(total_len - 1, 254); 6879 6880 if (dbd) 6881 header->block_descr_len = 0; 6882 else 6883 header->block_descr_len = 6884 sizeof(struct scsi_mode_block_descr); 6885 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6886 break; 6887 } 6888 case MODE_SENSE_10: { 6889 struct scsi_mode_hdr_10 *header; 6890 int datalen; 6891 6892 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6893 6894 datalen = ctl_min(total_len - 2, 65533); 6895 scsi_ulto2b(datalen, header->datalen); 6896 if (dbd) 6897 scsi_ulto2b(0, header->block_descr_len); 6898 else 6899 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6900 header->block_descr_len); 6901 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6902 break; 6903 } 6904 default: 6905 panic("invalid CDB type %#x", ctsio->cdb[0]); 6906 break; /* NOTREACHED */ 6907 } 6908 6909 /* 6910 * If we've got a disk, use its blocksize in the block 6911 * descriptor. Otherwise, just set it to 0. 6912 */ 6913 if (dbd == 0) { 6914 if (control_dev != 0) 6915 scsi_ulto3b(lun->be_lun->blocksize, 6916 block_desc->block_len); 6917 else 6918 scsi_ulto3b(0, block_desc->block_len); 6919 } 6920 6921 switch (page_code) { 6922 case SMS_ALL_PAGES_PAGE: { 6923 int i, data_used; 6924 6925 data_used = header_len; 6926 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6927 struct ctl_page_index *page_index; 6928 6929 page_index = &lun->mode_pages.index[i]; 6930 6931 if ((control_dev != 0) 6932 && (page_index->page_flags & 6933 CTL_PAGE_FLAG_DISK_ONLY)) 6934 continue; 6935 6936 /* 6937 * We don't use this subpage if the user didn't 6938 * request all subpages. We already checked (above) 6939 * to make sure the user only specified a subpage 6940 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6941 */ 6942 if ((page_index->subpage != 0) 6943 && (subpage == SMS_SUBPAGE_PAGE_0)) 6944 continue; 6945 6946 /* 6947 * Call the handler, if it exists, to update the 6948 * page to the latest values. 6949 */ 6950 if (page_index->sense_handler != NULL) 6951 page_index->sense_handler(ctsio, page_index,pc); 6952 6953 memcpy(ctsio->kern_data_ptr + data_used, 6954 page_index->page_data + 6955 (page_index->page_len * pc), 6956 page_index->page_len); 6957 data_used += page_index->page_len; 6958 } 6959 break; 6960 } 6961 default: { 6962 int i, data_used; 6963 6964 data_used = header_len; 6965 6966 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6967 struct ctl_page_index *page_index; 6968 6969 page_index = &lun->mode_pages.index[i]; 6970 6971 /* Look for the right page code */ 6972 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6973 continue; 6974 6975 /* Look for the right subpage or the subpage wildcard*/ 6976 if ((page_index->subpage != subpage) 6977 && (subpage != SMS_SUBPAGE_ALL)) 6978 continue; 6979 6980 /* Make sure the page is supported for this dev type */ 6981 if ((control_dev != 0) 6982 && (page_index->page_flags & 6983 CTL_PAGE_FLAG_DISK_ONLY)) 6984 continue; 6985 6986 /* 6987 * Call the handler, if it exists, to update the 6988 * page to the latest values. 6989 */ 6990 if (page_index->sense_handler != NULL) 6991 page_index->sense_handler(ctsio, page_index,pc); 6992 6993 memcpy(ctsio->kern_data_ptr + data_used, 6994 page_index->page_data + 6995 (page_index->page_len * pc), 6996 page_index->page_len); 6997 data_used += page_index->page_len; 6998 } 6999 break; 7000 } 7001 } 7002 7003 ctsio->scsi_status = SCSI_STATUS_OK; 7004 7005 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7006 ctsio->be_move_done = ctl_config_move_done; 7007 ctl_datamove((union ctl_io *)ctsio); 7008 7009 return (CTL_RETVAL_COMPLETE); 7010} 7011 7012int 7013ctl_read_capacity(struct ctl_scsiio *ctsio) 7014{ 7015 struct scsi_read_capacity *cdb; 7016 struct scsi_read_capacity_data *data; 7017 struct ctl_lun *lun; 7018 uint32_t lba; 7019 7020 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7021 7022 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7023 7024 lba = scsi_4btoul(cdb->addr); 7025 if (((cdb->pmi & SRC_PMI) == 0) 7026 && (lba != 0)) { 7027 ctl_set_invalid_field(/*ctsio*/ ctsio, 7028 /*sks_valid*/ 1, 7029 /*command*/ 1, 7030 /*field*/ 2, 7031 /*bit_valid*/ 0, 7032 /*bit*/ 0); 7033 ctl_done((union ctl_io *)ctsio); 7034 return (CTL_RETVAL_COMPLETE); 7035 } 7036 7037 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7038 7039 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7040 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7041 ctsio->residual = 0; 7042 ctsio->kern_data_len = sizeof(*data); 7043 ctsio->kern_total_len = sizeof(*data); 7044 ctsio->kern_data_resid = 0; 7045 ctsio->kern_rel_offset = 0; 7046 ctsio->kern_sg_entries = 0; 7047 7048 /* 7049 * If the maximum LBA is greater than 0xfffffffe, the user must 7050 * issue a SERVICE ACTION IN (16) command, with the read capacity 7051 * serivce action set. 7052 */ 7053 if (lun->be_lun->maxlba > 0xfffffffe) 7054 scsi_ulto4b(0xffffffff, data->addr); 7055 else 7056 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7057 7058 /* 7059 * XXX KDM this may not be 512 bytes... 7060 */ 7061 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7062 7063 ctsio->scsi_status = SCSI_STATUS_OK; 7064 7065 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7066 ctsio->be_move_done = ctl_config_move_done; 7067 ctl_datamove((union ctl_io *)ctsio); 7068 7069 return (CTL_RETVAL_COMPLETE); 7070} 7071 7072int 7073ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7074{ 7075 struct scsi_read_capacity_16 *cdb; 7076 struct scsi_read_capacity_data_long *data; 7077 struct ctl_lun *lun; 7078 uint64_t lba; 7079 uint32_t alloc_len; 7080 7081 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7082 7083 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7084 7085 alloc_len = scsi_4btoul(cdb->alloc_len); 7086 lba = scsi_8btou64(cdb->addr); 7087 7088 if ((cdb->reladr & SRC16_PMI) 7089 && (lba != 0)) { 7090 ctl_set_invalid_field(/*ctsio*/ ctsio, 7091 /*sks_valid*/ 1, 7092 /*command*/ 1, 7093 /*field*/ 2, 7094 /*bit_valid*/ 0, 7095 /*bit*/ 0); 7096 ctl_done((union ctl_io *)ctsio); 7097 return (CTL_RETVAL_COMPLETE); 7098 } 7099 7100 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7101 7102 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7103 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7104 7105 if (sizeof(*data) < alloc_len) { 7106 ctsio->residual = alloc_len - sizeof(*data); 7107 ctsio->kern_data_len = sizeof(*data); 7108 ctsio->kern_total_len = sizeof(*data); 7109 } else { 7110 ctsio->residual = 0; 7111 ctsio->kern_data_len = alloc_len; 7112 ctsio->kern_total_len = alloc_len; 7113 } 7114 ctsio->kern_data_resid = 0; 7115 ctsio->kern_rel_offset = 0; 7116 ctsio->kern_sg_entries = 0; 7117 7118 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7119 /* XXX KDM this may not be 512 bytes... */ 7120 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7121 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7122 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7123 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7124 data->lalba_lbp[0] |= SRC16_LBPME; 7125 7126 ctsio->scsi_status = SCSI_STATUS_OK; 7127 7128 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7129 ctsio->be_move_done = ctl_config_move_done; 7130 ctl_datamove((union ctl_io *)ctsio); 7131 7132 return (CTL_RETVAL_COMPLETE); 7133} 7134 7135int 7136ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7137{ 7138 struct scsi_maintenance_in *cdb; 7139 int retval; 7140 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7141 int num_target_port_groups, num_target_ports, single; 7142 struct ctl_lun *lun; 7143 struct ctl_softc *softc; 7144 struct ctl_port *port; 7145 struct scsi_target_group_data *rtg_ptr; 7146 struct scsi_target_group_data_extended *rtg_ext_ptr; 7147 struct scsi_target_port_group_descriptor *tpg_desc; 7148 7149 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7150 7151 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7152 softc = control_softc; 7153 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7154 7155 retval = CTL_RETVAL_COMPLETE; 7156 7157 switch (cdb->byte2 & STG_PDF_MASK) { 7158 case STG_PDF_LENGTH: 7159 ext = 0; 7160 break; 7161 case STG_PDF_EXTENDED: 7162 ext = 1; 7163 break; 7164 default: 7165 ctl_set_invalid_field(/*ctsio*/ ctsio, 7166 /*sks_valid*/ 1, 7167 /*command*/ 1, 7168 /*field*/ 2, 7169 /*bit_valid*/ 1, 7170 /*bit*/ 5); 7171 ctl_done((union ctl_io *)ctsio); 7172 return(retval); 7173 } 7174 7175 single = ctl_is_single; 7176 if (single) 7177 num_target_port_groups = 1; 7178 else 7179 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7180 num_target_ports = 0; 7181 mtx_lock(&softc->ctl_lock); 7182 STAILQ_FOREACH(port, &softc->port_list, links) { 7183 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7184 continue; 7185 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7186 continue; 7187 num_target_ports++; 7188 } 7189 mtx_unlock(&softc->ctl_lock); 7190 7191 if (ext) 7192 total_len = sizeof(struct scsi_target_group_data_extended); 7193 else 7194 total_len = sizeof(struct scsi_target_group_data); 7195 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7196 num_target_port_groups + 7197 sizeof(struct scsi_target_port_descriptor) * 7198 num_target_ports * num_target_port_groups; 7199 7200 alloc_len = scsi_4btoul(cdb->length); 7201 7202 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7203 7204 ctsio->kern_sg_entries = 0; 7205 7206 if (total_len < alloc_len) { 7207 ctsio->residual = alloc_len - total_len; 7208 ctsio->kern_data_len = total_len; 7209 ctsio->kern_total_len = total_len; 7210 } else { 7211 ctsio->residual = 0; 7212 ctsio->kern_data_len = alloc_len; 7213 ctsio->kern_total_len = alloc_len; 7214 } 7215 ctsio->kern_data_resid = 0; 7216 ctsio->kern_rel_offset = 0; 7217 7218 if (ext) { 7219 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7220 ctsio->kern_data_ptr; 7221 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7222 rtg_ext_ptr->format_type = 0x10; 7223 rtg_ext_ptr->implicit_transition_time = 0; 7224 tpg_desc = &rtg_ext_ptr->groups[0]; 7225 } else { 7226 rtg_ptr = (struct scsi_target_group_data *) 7227 ctsio->kern_data_ptr; 7228 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7229 tpg_desc = &rtg_ptr->groups[0]; 7230 } 7231 7232 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7233 mtx_lock(&softc->ctl_lock); 7234 for (g = 0; g < num_target_port_groups; g++) { 7235 if (g == pg) 7236 tpg_desc->pref_state = TPG_PRIMARY | 7237 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7238 else 7239 tpg_desc->pref_state = 7240 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7241 tpg_desc->support = TPG_AO_SUP; 7242 if (!single) 7243 tpg_desc->support = TPG_AN_SUP; 7244 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7245 tpg_desc->status = TPG_IMPLICIT; 7246 pc = 0; 7247 STAILQ_FOREACH(port, &softc->port_list, links) { 7248 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7249 continue; 7250 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7251 CTL_MAX_LUNS) 7252 continue; 7253 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7254 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7255 relative_target_port_identifier); 7256 pc++; 7257 } 7258 tpg_desc->target_port_count = pc; 7259 tpg_desc = (struct scsi_target_port_group_descriptor *) 7260 &tpg_desc->descriptors[pc]; 7261 } 7262 mtx_unlock(&softc->ctl_lock); 7263 7264 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7265 ctsio->be_move_done = ctl_config_move_done; 7266 7267 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7268 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7269 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7270 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7271 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7272 7273 ctl_datamove((union ctl_io *)ctsio); 7274 return(retval); 7275} 7276 7277int 7278ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7279{ 7280 struct ctl_lun *lun; 7281 struct scsi_report_supported_opcodes *cdb; 7282 const struct ctl_cmd_entry *entry, *sentry; 7283 struct scsi_report_supported_opcodes_all *all; 7284 struct scsi_report_supported_opcodes_descr *descr; 7285 struct scsi_report_supported_opcodes_one *one; 7286 int retval; 7287 int alloc_len, total_len; 7288 int opcode, service_action, i, j, num; 7289 7290 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7291 7292 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7293 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7294 7295 retval = CTL_RETVAL_COMPLETE; 7296 7297 opcode = cdb->requested_opcode; 7298 service_action = scsi_2btoul(cdb->requested_service_action); 7299 switch (cdb->options & RSO_OPTIONS_MASK) { 7300 case RSO_OPTIONS_ALL: 7301 num = 0; 7302 for (i = 0; i < 256; i++) { 7303 entry = &ctl_cmd_table[i]; 7304 if (entry->flags & CTL_CMD_FLAG_SA5) { 7305 for (j = 0; j < 32; j++) { 7306 sentry = &((const struct ctl_cmd_entry *) 7307 entry->execute)[j]; 7308 if (ctl_cmd_applicable( 7309 lun->be_lun->lun_type, sentry)) 7310 num++; 7311 } 7312 } else { 7313 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7314 entry)) 7315 num++; 7316 } 7317 } 7318 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7319 num * sizeof(struct scsi_report_supported_opcodes_descr); 7320 break; 7321 case RSO_OPTIONS_OC: 7322 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7323 ctl_set_invalid_field(/*ctsio*/ ctsio, 7324 /*sks_valid*/ 1, 7325 /*command*/ 1, 7326 /*field*/ 2, 7327 /*bit_valid*/ 1, 7328 /*bit*/ 2); 7329 ctl_done((union ctl_io *)ctsio); 7330 return (CTL_RETVAL_COMPLETE); 7331 } 7332 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7333 break; 7334 case RSO_OPTIONS_OC_SA: 7335 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7336 service_action >= 32) { 7337 ctl_set_invalid_field(/*ctsio*/ ctsio, 7338 /*sks_valid*/ 1, 7339 /*command*/ 1, 7340 /*field*/ 2, 7341 /*bit_valid*/ 1, 7342 /*bit*/ 2); 7343 ctl_done((union ctl_io *)ctsio); 7344 return (CTL_RETVAL_COMPLETE); 7345 } 7346 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7347 break; 7348 default: 7349 ctl_set_invalid_field(/*ctsio*/ ctsio, 7350 /*sks_valid*/ 1, 7351 /*command*/ 1, 7352 /*field*/ 2, 7353 /*bit_valid*/ 1, 7354 /*bit*/ 2); 7355 ctl_done((union ctl_io *)ctsio); 7356 return (CTL_RETVAL_COMPLETE); 7357 } 7358 7359 alloc_len = scsi_4btoul(cdb->length); 7360 7361 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7362 7363 ctsio->kern_sg_entries = 0; 7364 7365 if (total_len < alloc_len) { 7366 ctsio->residual = alloc_len - total_len; 7367 ctsio->kern_data_len = total_len; 7368 ctsio->kern_total_len = total_len; 7369 } else { 7370 ctsio->residual = 0; 7371 ctsio->kern_data_len = alloc_len; 7372 ctsio->kern_total_len = alloc_len; 7373 } 7374 ctsio->kern_data_resid = 0; 7375 ctsio->kern_rel_offset = 0; 7376 7377 switch (cdb->options & RSO_OPTIONS_MASK) { 7378 case RSO_OPTIONS_ALL: 7379 all = (struct scsi_report_supported_opcodes_all *) 7380 ctsio->kern_data_ptr; 7381 num = 0; 7382 for (i = 0; i < 256; i++) { 7383 entry = &ctl_cmd_table[i]; 7384 if (entry->flags & CTL_CMD_FLAG_SA5) { 7385 for (j = 0; j < 32; j++) { 7386 sentry = &((const struct ctl_cmd_entry *) 7387 entry->execute)[j]; 7388 if (!ctl_cmd_applicable( 7389 lun->be_lun->lun_type, sentry)) 7390 continue; 7391 descr = &all->descr[num++]; 7392 descr->opcode = i; 7393 scsi_ulto2b(j, descr->service_action); 7394 descr->flags = RSO_SERVACTV; 7395 scsi_ulto2b(sentry->length, 7396 descr->cdb_length); 7397 } 7398 } else { 7399 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7400 entry)) 7401 continue; 7402 descr = &all->descr[num++]; 7403 descr->opcode = i; 7404 scsi_ulto2b(0, descr->service_action); 7405 descr->flags = 0; 7406 scsi_ulto2b(entry->length, descr->cdb_length); 7407 } 7408 } 7409 scsi_ulto4b( 7410 num * sizeof(struct scsi_report_supported_opcodes_descr), 7411 all->length); 7412 break; 7413 case RSO_OPTIONS_OC: 7414 one = (struct scsi_report_supported_opcodes_one *) 7415 ctsio->kern_data_ptr; 7416 entry = &ctl_cmd_table[opcode]; 7417 goto fill_one; 7418 case RSO_OPTIONS_OC_SA: 7419 one = (struct scsi_report_supported_opcodes_one *) 7420 ctsio->kern_data_ptr; 7421 entry = &ctl_cmd_table[opcode]; 7422 entry = &((const struct ctl_cmd_entry *) 7423 entry->execute)[service_action]; 7424fill_one: 7425 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7426 one->support = 3; 7427 scsi_ulto2b(entry->length, one->cdb_length); 7428 one->cdb_usage[0] = opcode; 7429 memcpy(&one->cdb_usage[1], entry->usage, 7430 entry->length - 1); 7431 } else 7432 one->support = 1; 7433 break; 7434 } 7435 7436 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7437 ctsio->be_move_done = ctl_config_move_done; 7438 7439 ctl_datamove((union ctl_io *)ctsio); 7440 return(retval); 7441} 7442 7443int 7444ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7445{ 7446 struct ctl_lun *lun; 7447 struct scsi_report_supported_tmf *cdb; 7448 struct scsi_report_supported_tmf_data *data; 7449 int retval; 7450 int alloc_len, total_len; 7451 7452 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7453 7454 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7455 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7456 7457 retval = CTL_RETVAL_COMPLETE; 7458 7459 total_len = sizeof(struct scsi_report_supported_tmf_data); 7460 alloc_len = scsi_4btoul(cdb->length); 7461 7462 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7463 7464 ctsio->kern_sg_entries = 0; 7465 7466 if (total_len < alloc_len) { 7467 ctsio->residual = alloc_len - total_len; 7468 ctsio->kern_data_len = total_len; 7469 ctsio->kern_total_len = total_len; 7470 } else { 7471 ctsio->residual = 0; 7472 ctsio->kern_data_len = alloc_len; 7473 ctsio->kern_total_len = alloc_len; 7474 } 7475 ctsio->kern_data_resid = 0; 7476 ctsio->kern_rel_offset = 0; 7477 7478 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7479 data->byte1 |= RST_ATS | RST_LURS | RST_TRS; 7480 7481 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7482 ctsio->be_move_done = ctl_config_move_done; 7483 7484 ctl_datamove((union ctl_io *)ctsio); 7485 return (retval); 7486} 7487 7488int 7489ctl_report_timestamp(struct ctl_scsiio *ctsio) 7490{ 7491 struct ctl_lun *lun; 7492 struct scsi_report_timestamp *cdb; 7493 struct scsi_report_timestamp_data *data; 7494 struct timeval tv; 7495 int64_t timestamp; 7496 int retval; 7497 int alloc_len, total_len; 7498 7499 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7500 7501 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7502 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7503 7504 retval = CTL_RETVAL_COMPLETE; 7505 7506 total_len = sizeof(struct scsi_report_timestamp_data); 7507 alloc_len = scsi_4btoul(cdb->length); 7508 7509 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7510 7511 ctsio->kern_sg_entries = 0; 7512 7513 if (total_len < alloc_len) { 7514 ctsio->residual = alloc_len - total_len; 7515 ctsio->kern_data_len = total_len; 7516 ctsio->kern_total_len = total_len; 7517 } else { 7518 ctsio->residual = 0; 7519 ctsio->kern_data_len = alloc_len; 7520 ctsio->kern_total_len = alloc_len; 7521 } 7522 ctsio->kern_data_resid = 0; 7523 ctsio->kern_rel_offset = 0; 7524 7525 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7526 scsi_ulto2b(sizeof(*data) - 2, data->length); 7527 data->origin = RTS_ORIG_OUTSIDE; 7528 getmicrotime(&tv); 7529 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7530 scsi_ulto4b(timestamp >> 16, data->timestamp); 7531 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7532 7533 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7534 ctsio->be_move_done = ctl_config_move_done; 7535 7536 ctl_datamove((union ctl_io *)ctsio); 7537 return (retval); 7538} 7539 7540int 7541ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7542{ 7543 struct scsi_per_res_in *cdb; 7544 int alloc_len, total_len = 0; 7545 /* struct scsi_per_res_in_rsrv in_data; */ 7546 struct ctl_lun *lun; 7547 struct ctl_softc *softc; 7548 7549 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7550 7551 softc = control_softc; 7552 7553 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7554 7555 alloc_len = scsi_2btoul(cdb->length); 7556 7557 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7558 7559retry: 7560 mtx_lock(&lun->lun_lock); 7561 switch (cdb->action) { 7562 case SPRI_RK: /* read keys */ 7563 total_len = sizeof(struct scsi_per_res_in_keys) + 7564 lun->pr_key_count * 7565 sizeof(struct scsi_per_res_key); 7566 break; 7567 case SPRI_RR: /* read reservation */ 7568 if (lun->flags & CTL_LUN_PR_RESERVED) 7569 total_len = sizeof(struct scsi_per_res_in_rsrv); 7570 else 7571 total_len = sizeof(struct scsi_per_res_in_header); 7572 break; 7573 case SPRI_RC: /* report capabilities */ 7574 total_len = sizeof(struct scsi_per_res_cap); 7575 break; 7576 default: 7577 panic("Invalid PR type %x", cdb->action); 7578 } 7579 mtx_unlock(&lun->lun_lock); 7580 7581 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7582 7583 if (total_len < alloc_len) { 7584 ctsio->residual = alloc_len - total_len; 7585 ctsio->kern_data_len = total_len; 7586 ctsio->kern_total_len = total_len; 7587 } else { 7588 ctsio->residual = 0; 7589 ctsio->kern_data_len = alloc_len; 7590 ctsio->kern_total_len = alloc_len; 7591 } 7592 7593 ctsio->kern_data_resid = 0; 7594 ctsio->kern_rel_offset = 0; 7595 ctsio->kern_sg_entries = 0; 7596 7597 mtx_lock(&lun->lun_lock); 7598 switch (cdb->action) { 7599 case SPRI_RK: { // read keys 7600 struct scsi_per_res_in_keys *res_keys; 7601 int i, key_count; 7602 7603 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7604 7605 /* 7606 * We had to drop the lock to allocate our buffer, which 7607 * leaves time for someone to come in with another 7608 * persistent reservation. (That is unlikely, though, 7609 * since this should be the only persistent reservation 7610 * command active right now.) 7611 */ 7612 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7613 (lun->pr_key_count * 7614 sizeof(struct scsi_per_res_key)))){ 7615 mtx_unlock(&lun->lun_lock); 7616 free(ctsio->kern_data_ptr, M_CTL); 7617 printf("%s: reservation length changed, retrying\n", 7618 __func__); 7619 goto retry; 7620 } 7621 7622 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7623 7624 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7625 lun->pr_key_count, res_keys->header.length); 7626 7627 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7628 if (!lun->per_res[i].registered) 7629 continue; 7630 7631 /* 7632 * We used lun->pr_key_count to calculate the 7633 * size to allocate. If it turns out the number of 7634 * initiators with the registered flag set is 7635 * larger than that (i.e. they haven't been kept in 7636 * sync), we've got a problem. 7637 */ 7638 if (key_count >= lun->pr_key_count) { 7639#ifdef NEEDTOPORT 7640 csevent_log(CSC_CTL | CSC_SHELF_SW | 7641 CTL_PR_ERROR, 7642 csevent_LogType_Fault, 7643 csevent_AlertLevel_Yellow, 7644 csevent_FRU_ShelfController, 7645 csevent_FRU_Firmware, 7646 csevent_FRU_Unknown, 7647 "registered keys %d >= key " 7648 "count %d", key_count, 7649 lun->pr_key_count); 7650#endif 7651 key_count++; 7652 continue; 7653 } 7654 memcpy(res_keys->keys[key_count].key, 7655 lun->per_res[i].res_key.key, 7656 ctl_min(sizeof(res_keys->keys[key_count].key), 7657 sizeof(lun->per_res[i].res_key))); 7658 key_count++; 7659 } 7660 break; 7661 } 7662 case SPRI_RR: { // read reservation 7663 struct scsi_per_res_in_rsrv *res; 7664 int tmp_len, header_only; 7665 7666 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7667 7668 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7669 7670 if (lun->flags & CTL_LUN_PR_RESERVED) 7671 { 7672 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7673 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7674 res->header.length); 7675 header_only = 0; 7676 } else { 7677 tmp_len = sizeof(struct scsi_per_res_in_header); 7678 scsi_ulto4b(0, res->header.length); 7679 header_only = 1; 7680 } 7681 7682 /* 7683 * We had to drop the lock to allocate our buffer, which 7684 * leaves time for someone to come in with another 7685 * persistent reservation. (That is unlikely, though, 7686 * since this should be the only persistent reservation 7687 * command active right now.) 7688 */ 7689 if (tmp_len != total_len) { 7690 mtx_unlock(&lun->lun_lock); 7691 free(ctsio->kern_data_ptr, M_CTL); 7692 printf("%s: reservation status changed, retrying\n", 7693 __func__); 7694 goto retry; 7695 } 7696 7697 /* 7698 * No reservation held, so we're done. 7699 */ 7700 if (header_only != 0) 7701 break; 7702 7703 /* 7704 * If the registration is an All Registrants type, the key 7705 * is 0, since it doesn't really matter. 7706 */ 7707 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7708 memcpy(res->data.reservation, 7709 &lun->per_res[lun->pr_res_idx].res_key, 7710 sizeof(struct scsi_per_res_key)); 7711 } 7712 res->data.scopetype = lun->res_type; 7713 break; 7714 } 7715 case SPRI_RC: //report capabilities 7716 { 7717 struct scsi_per_res_cap *res_cap; 7718 uint16_t type_mask; 7719 7720 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7721 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7722 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7723 type_mask = SPRI_TM_WR_EX_AR | 7724 SPRI_TM_EX_AC_RO | 7725 SPRI_TM_WR_EX_RO | 7726 SPRI_TM_EX_AC | 7727 SPRI_TM_WR_EX | 7728 SPRI_TM_EX_AC_AR; 7729 scsi_ulto2b(type_mask, res_cap->type_mask); 7730 break; 7731 } 7732 case SPRI_RS: //read full status 7733 default: 7734 /* 7735 * This is a bug, because we just checked for this above, 7736 * and should have returned an error. 7737 */ 7738 panic("Invalid PR type %x", cdb->action); 7739 break; /* NOTREACHED */ 7740 } 7741 mtx_unlock(&lun->lun_lock); 7742 7743 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7744 ctsio->be_move_done = ctl_config_move_done; 7745 7746 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7747 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7748 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7749 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7750 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7751 7752 ctl_datamove((union ctl_io *)ctsio); 7753 7754 return (CTL_RETVAL_COMPLETE); 7755} 7756 7757/* 7758 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7759 * it should return. 7760 */ 7761static int 7762ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7763 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7764 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7765 struct scsi_per_res_out_parms* param) 7766{ 7767 union ctl_ha_msg persis_io; 7768 int retval, i; 7769 int isc_retval; 7770 7771 retval = 0; 7772 7773 mtx_lock(&lun->lun_lock); 7774 if (sa_res_key == 0) { 7775 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7776 /* validate scope and type */ 7777 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7778 SPR_LU_SCOPE) { 7779 mtx_unlock(&lun->lun_lock); 7780 ctl_set_invalid_field(/*ctsio*/ ctsio, 7781 /*sks_valid*/ 1, 7782 /*command*/ 1, 7783 /*field*/ 2, 7784 /*bit_valid*/ 1, 7785 /*bit*/ 4); 7786 ctl_done((union ctl_io *)ctsio); 7787 return (1); 7788 } 7789 7790 if (type>8 || type==2 || type==4 || type==0) { 7791 mtx_unlock(&lun->lun_lock); 7792 ctl_set_invalid_field(/*ctsio*/ ctsio, 7793 /*sks_valid*/ 1, 7794 /*command*/ 1, 7795 /*field*/ 2, 7796 /*bit_valid*/ 1, 7797 /*bit*/ 0); 7798 ctl_done((union ctl_io *)ctsio); 7799 return (1); 7800 } 7801 7802 /* temporarily unregister this nexus */ 7803 lun->per_res[residx].registered = 0; 7804 7805 /* 7806 * Unregister everybody else and build UA for 7807 * them 7808 */ 7809 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7810 if (lun->per_res[i].registered == 0) 7811 continue; 7812 7813 if (!persis_offset 7814 && i <CTL_MAX_INITIATORS) 7815 lun->pending_sense[i].ua_pending |= 7816 CTL_UA_REG_PREEMPT; 7817 else if (persis_offset 7818 && i >= persis_offset) 7819 lun->pending_sense[i-persis_offset 7820 ].ua_pending |= 7821 CTL_UA_REG_PREEMPT; 7822 lun->per_res[i].registered = 0; 7823 memset(&lun->per_res[i].res_key, 0, 7824 sizeof(struct scsi_per_res_key)); 7825 } 7826 lun->per_res[residx].registered = 1; 7827 lun->pr_key_count = 1; 7828 lun->res_type = type; 7829 if (lun->res_type != SPR_TYPE_WR_EX_AR 7830 && lun->res_type != SPR_TYPE_EX_AC_AR) 7831 lun->pr_res_idx = residx; 7832 7833 /* send msg to other side */ 7834 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7835 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7836 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7837 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7838 persis_io.pr.pr_info.res_type = type; 7839 memcpy(persis_io.pr.pr_info.sa_res_key, 7840 param->serv_act_res_key, 7841 sizeof(param->serv_act_res_key)); 7842 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7843 &persis_io, sizeof(persis_io), 0)) > 7844 CTL_HA_STATUS_SUCCESS) { 7845 printf("CTL:Persis Out error returned " 7846 "from ctl_ha_msg_send %d\n", 7847 isc_retval); 7848 } 7849 } else { 7850 /* not all registrants */ 7851 mtx_unlock(&lun->lun_lock); 7852 free(ctsio->kern_data_ptr, M_CTL); 7853 ctl_set_invalid_field(ctsio, 7854 /*sks_valid*/ 1, 7855 /*command*/ 0, 7856 /*field*/ 8, 7857 /*bit_valid*/ 0, 7858 /*bit*/ 0); 7859 ctl_done((union ctl_io *)ctsio); 7860 return (1); 7861 } 7862 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7863 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7864 int found = 0; 7865 7866 if (res_key == sa_res_key) { 7867 /* special case */ 7868 /* 7869 * The spec implies this is not good but doesn't 7870 * say what to do. There are two choices either 7871 * generate a res conflict or check condition 7872 * with illegal field in parameter data. Since 7873 * that is what is done when the sa_res_key is 7874 * zero I'll take that approach since this has 7875 * to do with the sa_res_key. 7876 */ 7877 mtx_unlock(&lun->lun_lock); 7878 free(ctsio->kern_data_ptr, M_CTL); 7879 ctl_set_invalid_field(ctsio, 7880 /*sks_valid*/ 1, 7881 /*command*/ 0, 7882 /*field*/ 8, 7883 /*bit_valid*/ 0, 7884 /*bit*/ 0); 7885 ctl_done((union ctl_io *)ctsio); 7886 return (1); 7887 } 7888 7889 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7890 if (lun->per_res[i].registered 7891 && memcmp(param->serv_act_res_key, 7892 lun->per_res[i].res_key.key, 7893 sizeof(struct scsi_per_res_key)) != 0) 7894 continue; 7895 7896 found = 1; 7897 lun->per_res[i].registered = 0; 7898 memset(&lun->per_res[i].res_key, 0, 7899 sizeof(struct scsi_per_res_key)); 7900 lun->pr_key_count--; 7901 7902 if (!persis_offset 7903 && i < CTL_MAX_INITIATORS) 7904 lun->pending_sense[i].ua_pending |= 7905 CTL_UA_REG_PREEMPT; 7906 else if (persis_offset 7907 && i >= persis_offset) 7908 lun->pending_sense[i-persis_offset].ua_pending|= 7909 CTL_UA_REG_PREEMPT; 7910 } 7911 if (!found) { 7912 mtx_unlock(&lun->lun_lock); 7913 free(ctsio->kern_data_ptr, M_CTL); 7914 ctl_set_reservation_conflict(ctsio); 7915 ctl_done((union ctl_io *)ctsio); 7916 return (CTL_RETVAL_COMPLETE); 7917 } 7918 /* send msg to other side */ 7919 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7920 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7921 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7922 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7923 persis_io.pr.pr_info.res_type = type; 7924 memcpy(persis_io.pr.pr_info.sa_res_key, 7925 param->serv_act_res_key, 7926 sizeof(param->serv_act_res_key)); 7927 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7928 &persis_io, sizeof(persis_io), 0)) > 7929 CTL_HA_STATUS_SUCCESS) { 7930 printf("CTL:Persis Out error returned from " 7931 "ctl_ha_msg_send %d\n", isc_retval); 7932 } 7933 } else { 7934 /* Reserved but not all registrants */ 7935 /* sa_res_key is res holder */ 7936 if (memcmp(param->serv_act_res_key, 7937 lun->per_res[lun->pr_res_idx].res_key.key, 7938 sizeof(struct scsi_per_res_key)) == 0) { 7939 /* validate scope and type */ 7940 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7941 SPR_LU_SCOPE) { 7942 mtx_unlock(&lun->lun_lock); 7943 ctl_set_invalid_field(/*ctsio*/ ctsio, 7944 /*sks_valid*/ 1, 7945 /*command*/ 1, 7946 /*field*/ 2, 7947 /*bit_valid*/ 1, 7948 /*bit*/ 4); 7949 ctl_done((union ctl_io *)ctsio); 7950 return (1); 7951 } 7952 7953 if (type>8 || type==2 || type==4 || type==0) { 7954 mtx_unlock(&lun->lun_lock); 7955 ctl_set_invalid_field(/*ctsio*/ ctsio, 7956 /*sks_valid*/ 1, 7957 /*command*/ 1, 7958 /*field*/ 2, 7959 /*bit_valid*/ 1, 7960 /*bit*/ 0); 7961 ctl_done((union ctl_io *)ctsio); 7962 return (1); 7963 } 7964 7965 /* 7966 * Do the following: 7967 * if sa_res_key != res_key remove all 7968 * registrants w/sa_res_key and generate UA 7969 * for these registrants(Registrations 7970 * Preempted) if it wasn't an exclusive 7971 * reservation generate UA(Reservations 7972 * Preempted) for all other registered nexuses 7973 * if the type has changed. Establish the new 7974 * reservation and holder. If res_key and 7975 * sa_res_key are the same do the above 7976 * except don't unregister the res holder. 7977 */ 7978 7979 /* 7980 * Temporarily unregister so it won't get 7981 * removed or UA generated 7982 */ 7983 lun->per_res[residx].registered = 0; 7984 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7985 if (lun->per_res[i].registered == 0) 7986 continue; 7987 7988 if (memcmp(param->serv_act_res_key, 7989 lun->per_res[i].res_key.key, 7990 sizeof(struct scsi_per_res_key)) == 0) { 7991 lun->per_res[i].registered = 0; 7992 memset(&lun->per_res[i].res_key, 7993 0, 7994 sizeof(struct scsi_per_res_key)); 7995 lun->pr_key_count--; 7996 7997 if (!persis_offset 7998 && i < CTL_MAX_INITIATORS) 7999 lun->pending_sense[i 8000 ].ua_pending |= 8001 CTL_UA_REG_PREEMPT; 8002 else if (persis_offset 8003 && i >= persis_offset) 8004 lun->pending_sense[ 8005 i-persis_offset].ua_pending |= 8006 CTL_UA_REG_PREEMPT; 8007 } else if (type != lun->res_type 8008 && (lun->res_type == SPR_TYPE_WR_EX_RO 8009 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8010 if (!persis_offset 8011 && i < CTL_MAX_INITIATORS) 8012 lun->pending_sense[i 8013 ].ua_pending |= 8014 CTL_UA_RES_RELEASE; 8015 else if (persis_offset 8016 && i >= persis_offset) 8017 lun->pending_sense[ 8018 i-persis_offset 8019 ].ua_pending |= 8020 CTL_UA_RES_RELEASE; 8021 } 8022 } 8023 lun->per_res[residx].registered = 1; 8024 lun->res_type = type; 8025 if (lun->res_type != SPR_TYPE_WR_EX_AR 8026 && lun->res_type != SPR_TYPE_EX_AC_AR) 8027 lun->pr_res_idx = residx; 8028 else 8029 lun->pr_res_idx = 8030 CTL_PR_ALL_REGISTRANTS; 8031 8032 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8033 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8034 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8035 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8036 persis_io.pr.pr_info.res_type = type; 8037 memcpy(persis_io.pr.pr_info.sa_res_key, 8038 param->serv_act_res_key, 8039 sizeof(param->serv_act_res_key)); 8040 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8041 &persis_io, sizeof(persis_io), 0)) > 8042 CTL_HA_STATUS_SUCCESS) { 8043 printf("CTL:Persis Out error returned " 8044 "from ctl_ha_msg_send %d\n", 8045 isc_retval); 8046 } 8047 } else { 8048 /* 8049 * sa_res_key is not the res holder just 8050 * remove registrants 8051 */ 8052 int found=0; 8053 8054 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8055 if (memcmp(param->serv_act_res_key, 8056 lun->per_res[i].res_key.key, 8057 sizeof(struct scsi_per_res_key)) != 0) 8058 continue; 8059 8060 found = 1; 8061 lun->per_res[i].registered = 0; 8062 memset(&lun->per_res[i].res_key, 0, 8063 sizeof(struct scsi_per_res_key)); 8064 lun->pr_key_count--; 8065 8066 if (!persis_offset 8067 && i < CTL_MAX_INITIATORS) 8068 lun->pending_sense[i].ua_pending |= 8069 CTL_UA_REG_PREEMPT; 8070 else if (persis_offset 8071 && i >= persis_offset) 8072 lun->pending_sense[ 8073 i-persis_offset].ua_pending |= 8074 CTL_UA_REG_PREEMPT; 8075 } 8076 8077 if (!found) { 8078 mtx_unlock(&lun->lun_lock); 8079 free(ctsio->kern_data_ptr, M_CTL); 8080 ctl_set_reservation_conflict(ctsio); 8081 ctl_done((union ctl_io *)ctsio); 8082 return (1); 8083 } 8084 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8085 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8086 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8087 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8088 persis_io.pr.pr_info.res_type = type; 8089 memcpy(persis_io.pr.pr_info.sa_res_key, 8090 param->serv_act_res_key, 8091 sizeof(param->serv_act_res_key)); 8092 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8093 &persis_io, sizeof(persis_io), 0)) > 8094 CTL_HA_STATUS_SUCCESS) { 8095 printf("CTL:Persis Out error returned " 8096 "from ctl_ha_msg_send %d\n", 8097 isc_retval); 8098 } 8099 } 8100 } 8101 8102 lun->PRGeneration++; 8103 mtx_unlock(&lun->lun_lock); 8104 8105 return (retval); 8106} 8107 8108static void 8109ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8110{ 8111 int i; 8112 8113 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8114 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8115 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8116 msg->pr.pr_info.sa_res_key, 8117 sizeof(struct scsi_per_res_key)) != 0) { 8118 uint64_t sa_res_key; 8119 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8120 8121 if (sa_res_key == 0) { 8122 /* temporarily unregister this nexus */ 8123 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8124 8125 /* 8126 * Unregister everybody else and build UA for 8127 * them 8128 */ 8129 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8130 if (lun->per_res[i].registered == 0) 8131 continue; 8132 8133 if (!persis_offset 8134 && i < CTL_MAX_INITIATORS) 8135 lun->pending_sense[i].ua_pending |= 8136 CTL_UA_REG_PREEMPT; 8137 else if (persis_offset && i >= persis_offset) 8138 lun->pending_sense[i - 8139 persis_offset].ua_pending |= 8140 CTL_UA_REG_PREEMPT; 8141 lun->per_res[i].registered = 0; 8142 memset(&lun->per_res[i].res_key, 0, 8143 sizeof(struct scsi_per_res_key)); 8144 } 8145 8146 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8147 lun->pr_key_count = 1; 8148 lun->res_type = msg->pr.pr_info.res_type; 8149 if (lun->res_type != SPR_TYPE_WR_EX_AR 8150 && lun->res_type != SPR_TYPE_EX_AC_AR) 8151 lun->pr_res_idx = msg->pr.pr_info.residx; 8152 } else { 8153 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8154 if (memcmp(msg->pr.pr_info.sa_res_key, 8155 lun->per_res[i].res_key.key, 8156 sizeof(struct scsi_per_res_key)) != 0) 8157 continue; 8158 8159 lun->per_res[i].registered = 0; 8160 memset(&lun->per_res[i].res_key, 0, 8161 sizeof(struct scsi_per_res_key)); 8162 lun->pr_key_count--; 8163 8164 if (!persis_offset 8165 && i < persis_offset) 8166 lun->pending_sense[i].ua_pending |= 8167 CTL_UA_REG_PREEMPT; 8168 else if (persis_offset 8169 && i >= persis_offset) 8170 lun->pending_sense[i - 8171 persis_offset].ua_pending |= 8172 CTL_UA_REG_PREEMPT; 8173 } 8174 } 8175 } else { 8176 /* 8177 * Temporarily unregister so it won't get removed 8178 * or UA generated 8179 */ 8180 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8181 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8182 if (lun->per_res[i].registered == 0) 8183 continue; 8184 8185 if (memcmp(msg->pr.pr_info.sa_res_key, 8186 lun->per_res[i].res_key.key, 8187 sizeof(struct scsi_per_res_key)) == 0) { 8188 lun->per_res[i].registered = 0; 8189 memset(&lun->per_res[i].res_key, 0, 8190 sizeof(struct scsi_per_res_key)); 8191 lun->pr_key_count--; 8192 if (!persis_offset 8193 && i < CTL_MAX_INITIATORS) 8194 lun->pending_sense[i].ua_pending |= 8195 CTL_UA_REG_PREEMPT; 8196 else if (persis_offset 8197 && i >= persis_offset) 8198 lun->pending_sense[i - 8199 persis_offset].ua_pending |= 8200 CTL_UA_REG_PREEMPT; 8201 } else if (msg->pr.pr_info.res_type != lun->res_type 8202 && (lun->res_type == SPR_TYPE_WR_EX_RO 8203 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8204 if (!persis_offset 8205 && i < persis_offset) 8206 lun->pending_sense[i 8207 ].ua_pending |= 8208 CTL_UA_RES_RELEASE; 8209 else if (persis_offset 8210 && i >= persis_offset) 8211 lun->pending_sense[i - 8212 persis_offset].ua_pending |= 8213 CTL_UA_RES_RELEASE; 8214 } 8215 } 8216 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8217 lun->res_type = msg->pr.pr_info.res_type; 8218 if (lun->res_type != SPR_TYPE_WR_EX_AR 8219 && lun->res_type != SPR_TYPE_EX_AC_AR) 8220 lun->pr_res_idx = msg->pr.pr_info.residx; 8221 else 8222 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8223 } 8224 lun->PRGeneration++; 8225 8226} 8227 8228 8229int 8230ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8231{ 8232 int retval; 8233 int isc_retval; 8234 u_int32_t param_len; 8235 struct scsi_per_res_out *cdb; 8236 struct ctl_lun *lun; 8237 struct scsi_per_res_out_parms* param; 8238 struct ctl_softc *softc; 8239 uint32_t residx; 8240 uint64_t res_key, sa_res_key; 8241 uint8_t type; 8242 union ctl_ha_msg persis_io; 8243 int i; 8244 8245 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8246 8247 retval = CTL_RETVAL_COMPLETE; 8248 8249 softc = control_softc; 8250 8251 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8252 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8253 8254 /* 8255 * We only support whole-LUN scope. The scope & type are ignored for 8256 * register, register and ignore existing key and clear. 8257 * We sometimes ignore scope and type on preempts too!! 8258 * Verify reservation type here as well. 8259 */ 8260 type = cdb->scope_type & SPR_TYPE_MASK; 8261 if ((cdb->action == SPRO_RESERVE) 8262 || (cdb->action == SPRO_RELEASE)) { 8263 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8264 ctl_set_invalid_field(/*ctsio*/ ctsio, 8265 /*sks_valid*/ 1, 8266 /*command*/ 1, 8267 /*field*/ 2, 8268 /*bit_valid*/ 1, 8269 /*bit*/ 4); 8270 ctl_done((union ctl_io *)ctsio); 8271 return (CTL_RETVAL_COMPLETE); 8272 } 8273 8274 if (type>8 || type==2 || type==4 || type==0) { 8275 ctl_set_invalid_field(/*ctsio*/ ctsio, 8276 /*sks_valid*/ 1, 8277 /*command*/ 1, 8278 /*field*/ 2, 8279 /*bit_valid*/ 1, 8280 /*bit*/ 0); 8281 ctl_done((union ctl_io *)ctsio); 8282 return (CTL_RETVAL_COMPLETE); 8283 } 8284 } 8285 8286 param_len = scsi_4btoul(cdb->length); 8287 8288 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8289 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8290 ctsio->kern_data_len = param_len; 8291 ctsio->kern_total_len = param_len; 8292 ctsio->kern_data_resid = 0; 8293 ctsio->kern_rel_offset = 0; 8294 ctsio->kern_sg_entries = 0; 8295 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8296 ctsio->be_move_done = ctl_config_move_done; 8297 ctl_datamove((union ctl_io *)ctsio); 8298 8299 return (CTL_RETVAL_COMPLETE); 8300 } 8301 8302 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8303 8304 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8305 res_key = scsi_8btou64(param->res_key.key); 8306 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8307 8308 /* 8309 * Validate the reservation key here except for SPRO_REG_IGNO 8310 * This must be done for all other service actions 8311 */ 8312 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8313 mtx_lock(&lun->lun_lock); 8314 if (lun->per_res[residx].registered) { 8315 if (memcmp(param->res_key.key, 8316 lun->per_res[residx].res_key.key, 8317 ctl_min(sizeof(param->res_key), 8318 sizeof(lun->per_res[residx].res_key))) != 0) { 8319 /* 8320 * The current key passed in doesn't match 8321 * the one the initiator previously 8322 * registered. 8323 */ 8324 mtx_unlock(&lun->lun_lock); 8325 free(ctsio->kern_data_ptr, M_CTL); 8326 ctl_set_reservation_conflict(ctsio); 8327 ctl_done((union ctl_io *)ctsio); 8328 return (CTL_RETVAL_COMPLETE); 8329 } 8330 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8331 /* 8332 * We are not registered 8333 */ 8334 mtx_unlock(&lun->lun_lock); 8335 free(ctsio->kern_data_ptr, M_CTL); 8336 ctl_set_reservation_conflict(ctsio); 8337 ctl_done((union ctl_io *)ctsio); 8338 return (CTL_RETVAL_COMPLETE); 8339 } else if (res_key != 0) { 8340 /* 8341 * We are not registered and trying to register but 8342 * the register key isn't zero. 8343 */ 8344 mtx_unlock(&lun->lun_lock); 8345 free(ctsio->kern_data_ptr, M_CTL); 8346 ctl_set_reservation_conflict(ctsio); 8347 ctl_done((union ctl_io *)ctsio); 8348 return (CTL_RETVAL_COMPLETE); 8349 } 8350 mtx_unlock(&lun->lun_lock); 8351 } 8352 8353 switch (cdb->action & SPRO_ACTION_MASK) { 8354 case SPRO_REGISTER: 8355 case SPRO_REG_IGNO: { 8356 8357#if 0 8358 printf("Registration received\n"); 8359#endif 8360 8361 /* 8362 * We don't support any of these options, as we report in 8363 * the read capabilities request (see 8364 * ctl_persistent_reserve_in(), above). 8365 */ 8366 if ((param->flags & SPR_SPEC_I_PT) 8367 || (param->flags & SPR_ALL_TG_PT) 8368 || (param->flags & SPR_APTPL)) { 8369 int bit_ptr; 8370 8371 if (param->flags & SPR_APTPL) 8372 bit_ptr = 0; 8373 else if (param->flags & SPR_ALL_TG_PT) 8374 bit_ptr = 2; 8375 else /* SPR_SPEC_I_PT */ 8376 bit_ptr = 3; 8377 8378 free(ctsio->kern_data_ptr, M_CTL); 8379 ctl_set_invalid_field(ctsio, 8380 /*sks_valid*/ 1, 8381 /*command*/ 0, 8382 /*field*/ 20, 8383 /*bit_valid*/ 1, 8384 /*bit*/ bit_ptr); 8385 ctl_done((union ctl_io *)ctsio); 8386 return (CTL_RETVAL_COMPLETE); 8387 } 8388 8389 mtx_lock(&lun->lun_lock); 8390 8391 /* 8392 * The initiator wants to clear the 8393 * key/unregister. 8394 */ 8395 if (sa_res_key == 0) { 8396 if ((res_key == 0 8397 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8398 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8399 && !lun->per_res[residx].registered)) { 8400 mtx_unlock(&lun->lun_lock); 8401 goto done; 8402 } 8403 8404 lun->per_res[residx].registered = 0; 8405 memset(&lun->per_res[residx].res_key, 8406 0, sizeof(lun->per_res[residx].res_key)); 8407 lun->pr_key_count--; 8408 8409 if (residx == lun->pr_res_idx) { 8410 lun->flags &= ~CTL_LUN_PR_RESERVED; 8411 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8412 8413 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8414 || lun->res_type == SPR_TYPE_EX_AC_RO) 8415 && lun->pr_key_count) { 8416 /* 8417 * If the reservation is a registrants 8418 * only type we need to generate a UA 8419 * for other registered inits. The 8420 * sense code should be RESERVATIONS 8421 * RELEASED 8422 */ 8423 8424 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8425 if (lun->per_res[ 8426 i+persis_offset].registered 8427 == 0) 8428 continue; 8429 lun->pending_sense[i 8430 ].ua_pending |= 8431 CTL_UA_RES_RELEASE; 8432 } 8433 } 8434 lun->res_type = 0; 8435 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8436 if (lun->pr_key_count==0) { 8437 lun->flags &= ~CTL_LUN_PR_RESERVED; 8438 lun->res_type = 0; 8439 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8440 } 8441 } 8442 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8443 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8444 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8445 persis_io.pr.pr_info.residx = residx; 8446 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8447 &persis_io, sizeof(persis_io), 0 )) > 8448 CTL_HA_STATUS_SUCCESS) { 8449 printf("CTL:Persis Out error returned from " 8450 "ctl_ha_msg_send %d\n", isc_retval); 8451 } 8452 } else /* sa_res_key != 0 */ { 8453 8454 /* 8455 * If we aren't registered currently then increment 8456 * the key count and set the registered flag. 8457 */ 8458 if (!lun->per_res[residx].registered) { 8459 lun->pr_key_count++; 8460 lun->per_res[residx].registered = 1; 8461 } 8462 8463 memcpy(&lun->per_res[residx].res_key, 8464 param->serv_act_res_key, 8465 ctl_min(sizeof(param->serv_act_res_key), 8466 sizeof(lun->per_res[residx].res_key))); 8467 8468 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8469 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8470 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8471 persis_io.pr.pr_info.residx = residx; 8472 memcpy(persis_io.pr.pr_info.sa_res_key, 8473 param->serv_act_res_key, 8474 sizeof(param->serv_act_res_key)); 8475 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8476 &persis_io, sizeof(persis_io), 0)) > 8477 CTL_HA_STATUS_SUCCESS) { 8478 printf("CTL:Persis Out error returned from " 8479 "ctl_ha_msg_send %d\n", isc_retval); 8480 } 8481 } 8482 lun->PRGeneration++; 8483 mtx_unlock(&lun->lun_lock); 8484 8485 break; 8486 } 8487 case SPRO_RESERVE: 8488#if 0 8489 printf("Reserve executed type %d\n", type); 8490#endif 8491 mtx_lock(&lun->lun_lock); 8492 if (lun->flags & CTL_LUN_PR_RESERVED) { 8493 /* 8494 * if this isn't the reservation holder and it's 8495 * not a "all registrants" type or if the type is 8496 * different then we have a conflict 8497 */ 8498 if ((lun->pr_res_idx != residx 8499 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8500 || lun->res_type != type) { 8501 mtx_unlock(&lun->lun_lock); 8502 free(ctsio->kern_data_ptr, M_CTL); 8503 ctl_set_reservation_conflict(ctsio); 8504 ctl_done((union ctl_io *)ctsio); 8505 return (CTL_RETVAL_COMPLETE); 8506 } 8507 mtx_unlock(&lun->lun_lock); 8508 } else /* create a reservation */ { 8509 /* 8510 * If it's not an "all registrants" type record 8511 * reservation holder 8512 */ 8513 if (type != SPR_TYPE_WR_EX_AR 8514 && type != SPR_TYPE_EX_AC_AR) 8515 lun->pr_res_idx = residx; /* Res holder */ 8516 else 8517 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8518 8519 lun->flags |= CTL_LUN_PR_RESERVED; 8520 lun->res_type = type; 8521 8522 mtx_unlock(&lun->lun_lock); 8523 8524 /* send msg to other side */ 8525 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8526 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8527 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8528 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8529 persis_io.pr.pr_info.res_type = type; 8530 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8531 &persis_io, sizeof(persis_io), 0)) > 8532 CTL_HA_STATUS_SUCCESS) { 8533 printf("CTL:Persis Out error returned from " 8534 "ctl_ha_msg_send %d\n", isc_retval); 8535 } 8536 } 8537 break; 8538 8539 case SPRO_RELEASE: 8540 mtx_lock(&lun->lun_lock); 8541 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8542 /* No reservation exists return good status */ 8543 mtx_unlock(&lun->lun_lock); 8544 goto done; 8545 } 8546 /* 8547 * Is this nexus a reservation holder? 8548 */ 8549 if (lun->pr_res_idx != residx 8550 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8551 /* 8552 * not a res holder return good status but 8553 * do nothing 8554 */ 8555 mtx_unlock(&lun->lun_lock); 8556 goto done; 8557 } 8558 8559 if (lun->res_type != type) { 8560 mtx_unlock(&lun->lun_lock); 8561 free(ctsio->kern_data_ptr, M_CTL); 8562 ctl_set_illegal_pr_release(ctsio); 8563 ctl_done((union ctl_io *)ctsio); 8564 return (CTL_RETVAL_COMPLETE); 8565 } 8566 8567 /* okay to release */ 8568 lun->flags &= ~CTL_LUN_PR_RESERVED; 8569 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8570 lun->res_type = 0; 8571 8572 /* 8573 * if this isn't an exclusive access 8574 * res generate UA for all other 8575 * registrants. 8576 */ 8577 if (type != SPR_TYPE_EX_AC 8578 && type != SPR_TYPE_WR_EX) { 8579 /* 8580 * temporarily unregister so we don't generate UA 8581 */ 8582 lun->per_res[residx].registered = 0; 8583 8584 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8585 if (lun->per_res[i+persis_offset].registered 8586 == 0) 8587 continue; 8588 lun->pending_sense[i].ua_pending |= 8589 CTL_UA_RES_RELEASE; 8590 } 8591 8592 lun->per_res[residx].registered = 1; 8593 } 8594 mtx_unlock(&lun->lun_lock); 8595 /* Send msg to other side */ 8596 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8597 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8598 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8599 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8600 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8601 printf("CTL:Persis Out error returned from " 8602 "ctl_ha_msg_send %d\n", isc_retval); 8603 } 8604 break; 8605 8606 case SPRO_CLEAR: 8607 /* send msg to other side */ 8608 8609 mtx_lock(&lun->lun_lock); 8610 lun->flags &= ~CTL_LUN_PR_RESERVED; 8611 lun->res_type = 0; 8612 lun->pr_key_count = 0; 8613 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8614 8615 8616 memset(&lun->per_res[residx].res_key, 8617 0, sizeof(lun->per_res[residx].res_key)); 8618 lun->per_res[residx].registered = 0; 8619 8620 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8621 if (lun->per_res[i].registered) { 8622 if (!persis_offset && i < CTL_MAX_INITIATORS) 8623 lun->pending_sense[i].ua_pending |= 8624 CTL_UA_RES_PREEMPT; 8625 else if (persis_offset && i >= persis_offset) 8626 lun->pending_sense[i-persis_offset 8627 ].ua_pending |= CTL_UA_RES_PREEMPT; 8628 8629 memset(&lun->per_res[i].res_key, 8630 0, sizeof(struct scsi_per_res_key)); 8631 lun->per_res[i].registered = 0; 8632 } 8633 lun->PRGeneration++; 8634 mtx_unlock(&lun->lun_lock); 8635 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8636 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8637 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8638 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8639 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8640 printf("CTL:Persis Out error returned from " 8641 "ctl_ha_msg_send %d\n", isc_retval); 8642 } 8643 break; 8644 8645 case SPRO_PREEMPT: { 8646 int nretval; 8647 8648 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8649 residx, ctsio, cdb, param); 8650 if (nretval != 0) 8651 return (CTL_RETVAL_COMPLETE); 8652 break; 8653 } 8654 default: 8655 panic("Invalid PR type %x", cdb->action); 8656 } 8657 8658done: 8659 free(ctsio->kern_data_ptr, M_CTL); 8660 ctl_set_success(ctsio); 8661 ctl_done((union ctl_io *)ctsio); 8662 8663 return (retval); 8664} 8665 8666/* 8667 * This routine is for handling a message from the other SC pertaining to 8668 * persistent reserve out. All the error checking will have been done 8669 * so only perorming the action need be done here to keep the two 8670 * in sync. 8671 */ 8672static void 8673ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8674{ 8675 struct ctl_lun *lun; 8676 struct ctl_softc *softc; 8677 int i; 8678 uint32_t targ_lun; 8679 8680 softc = control_softc; 8681 8682 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8683 lun = softc->ctl_luns[targ_lun]; 8684 mtx_lock(&lun->lun_lock); 8685 switch(msg->pr.pr_info.action) { 8686 case CTL_PR_REG_KEY: 8687 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8688 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8689 lun->pr_key_count++; 8690 } 8691 lun->PRGeneration++; 8692 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8693 msg->pr.pr_info.sa_res_key, 8694 sizeof(struct scsi_per_res_key)); 8695 break; 8696 8697 case CTL_PR_UNREG_KEY: 8698 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8699 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8700 0, sizeof(struct scsi_per_res_key)); 8701 lun->pr_key_count--; 8702 8703 /* XXX Need to see if the reservation has been released */ 8704 /* if so do we need to generate UA? */ 8705 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8706 lun->flags &= ~CTL_LUN_PR_RESERVED; 8707 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8708 8709 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8710 || lun->res_type == SPR_TYPE_EX_AC_RO) 8711 && lun->pr_key_count) { 8712 /* 8713 * If the reservation is a registrants 8714 * only type we need to generate a UA 8715 * for other registered inits. The 8716 * sense code should be RESERVATIONS 8717 * RELEASED 8718 */ 8719 8720 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8721 if (lun->per_res[i+ 8722 persis_offset].registered == 0) 8723 continue; 8724 8725 lun->pending_sense[i 8726 ].ua_pending |= 8727 CTL_UA_RES_RELEASE; 8728 } 8729 } 8730 lun->res_type = 0; 8731 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8732 if (lun->pr_key_count==0) { 8733 lun->flags &= ~CTL_LUN_PR_RESERVED; 8734 lun->res_type = 0; 8735 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8736 } 8737 } 8738 lun->PRGeneration++; 8739 break; 8740 8741 case CTL_PR_RESERVE: 8742 lun->flags |= CTL_LUN_PR_RESERVED; 8743 lun->res_type = msg->pr.pr_info.res_type; 8744 lun->pr_res_idx = msg->pr.pr_info.residx; 8745 8746 break; 8747 8748 case CTL_PR_RELEASE: 8749 /* 8750 * if this isn't an exclusive access res generate UA for all 8751 * other registrants. 8752 */ 8753 if (lun->res_type != SPR_TYPE_EX_AC 8754 && lun->res_type != SPR_TYPE_WR_EX) { 8755 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8756 if (lun->per_res[i+persis_offset].registered) 8757 lun->pending_sense[i].ua_pending |= 8758 CTL_UA_RES_RELEASE; 8759 } 8760 8761 lun->flags &= ~CTL_LUN_PR_RESERVED; 8762 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8763 lun->res_type = 0; 8764 break; 8765 8766 case CTL_PR_PREEMPT: 8767 ctl_pro_preempt_other(lun, msg); 8768 break; 8769 case CTL_PR_CLEAR: 8770 lun->flags &= ~CTL_LUN_PR_RESERVED; 8771 lun->res_type = 0; 8772 lun->pr_key_count = 0; 8773 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8774 8775 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8776 if (lun->per_res[i].registered == 0) 8777 continue; 8778 if (!persis_offset 8779 && i < CTL_MAX_INITIATORS) 8780 lun->pending_sense[i].ua_pending |= 8781 CTL_UA_RES_PREEMPT; 8782 else if (persis_offset 8783 && i >= persis_offset) 8784 lun->pending_sense[i-persis_offset].ua_pending|= 8785 CTL_UA_RES_PREEMPT; 8786 memset(&lun->per_res[i].res_key, 0, 8787 sizeof(struct scsi_per_res_key)); 8788 lun->per_res[i].registered = 0; 8789 } 8790 lun->PRGeneration++; 8791 break; 8792 } 8793 8794 mtx_unlock(&lun->lun_lock); 8795} 8796 8797int 8798ctl_read_write(struct ctl_scsiio *ctsio) 8799{ 8800 struct ctl_lun *lun; 8801 struct ctl_lba_len_flags *lbalen; 8802 uint64_t lba; 8803 uint32_t num_blocks; 8804 int fua, dpo; 8805 int retval; 8806 int isread; 8807 8808 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8809 8810 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8811 8812 fua = 0; 8813 dpo = 0; 8814 8815 retval = CTL_RETVAL_COMPLETE; 8816 8817 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8818 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8819 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8820 uint32_t residx; 8821 8822 /* 8823 * XXX KDM need a lock here. 8824 */ 8825 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8826 if ((lun->res_type == SPR_TYPE_EX_AC 8827 && residx != lun->pr_res_idx) 8828 || ((lun->res_type == SPR_TYPE_EX_AC_RO 8829 || lun->res_type == SPR_TYPE_EX_AC_AR) 8830 && !lun->per_res[residx].registered)) { 8831 ctl_set_reservation_conflict(ctsio); 8832 ctl_done((union ctl_io *)ctsio); 8833 return (CTL_RETVAL_COMPLETE); 8834 } 8835 } 8836 8837 switch (ctsio->cdb[0]) { 8838 case READ_6: 8839 case WRITE_6: { 8840 struct scsi_rw_6 *cdb; 8841 8842 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8843 8844 lba = scsi_3btoul(cdb->addr); 8845 /* only 5 bits are valid in the most significant address byte */ 8846 lba &= 0x1fffff; 8847 num_blocks = cdb->length; 8848 /* 8849 * This is correct according to SBC-2. 8850 */ 8851 if (num_blocks == 0) 8852 num_blocks = 256; 8853 break; 8854 } 8855 case READ_10: 8856 case WRITE_10: { 8857 struct scsi_rw_10 *cdb; 8858 8859 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8860 8861 if (cdb->byte2 & SRW10_FUA) 8862 fua = 1; 8863 if (cdb->byte2 & SRW10_DPO) 8864 dpo = 1; 8865 8866 lba = scsi_4btoul(cdb->addr); 8867 num_blocks = scsi_2btoul(cdb->length); 8868 break; 8869 } 8870 case WRITE_VERIFY_10: { 8871 struct scsi_write_verify_10 *cdb; 8872 8873 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8874 8875 /* 8876 * XXX KDM we should do actual write verify support at some 8877 * point. This is obviously fake, we're just translating 8878 * things to a write. So we don't even bother checking the 8879 * BYTCHK field, since we don't do any verification. If 8880 * the user asks for it, we'll just pretend we did it. 8881 */ 8882 if (cdb->byte2 & SWV_DPO) 8883 dpo = 1; 8884 8885 lba = scsi_4btoul(cdb->addr); 8886 num_blocks = scsi_2btoul(cdb->length); 8887 break; 8888 } 8889 case READ_12: 8890 case WRITE_12: { 8891 struct scsi_rw_12 *cdb; 8892 8893 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8894 8895 if (cdb->byte2 & SRW12_FUA) 8896 fua = 1; 8897 if (cdb->byte2 & SRW12_DPO) 8898 dpo = 1; 8899 lba = scsi_4btoul(cdb->addr); 8900 num_blocks = scsi_4btoul(cdb->length); 8901 break; 8902 } 8903 case WRITE_VERIFY_12: { 8904 struct scsi_write_verify_12 *cdb; 8905 8906 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8907 8908 if (cdb->byte2 & SWV_DPO) 8909 dpo = 1; 8910 8911 lba = scsi_4btoul(cdb->addr); 8912 num_blocks = scsi_4btoul(cdb->length); 8913 8914 break; 8915 } 8916 case READ_16: 8917 case WRITE_16: { 8918 struct scsi_rw_16 *cdb; 8919 8920 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8921 8922 if (cdb->byte2 & SRW12_FUA) 8923 fua = 1; 8924 if (cdb->byte2 & SRW12_DPO) 8925 dpo = 1; 8926 8927 lba = scsi_8btou64(cdb->addr); 8928 num_blocks = scsi_4btoul(cdb->length); 8929 break; 8930 } 8931 case WRITE_VERIFY_16: { 8932 struct scsi_write_verify_16 *cdb; 8933 8934 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8935 8936 if (cdb->byte2 & SWV_DPO) 8937 dpo = 1; 8938 8939 lba = scsi_8btou64(cdb->addr); 8940 num_blocks = scsi_4btoul(cdb->length); 8941 break; 8942 } 8943 default: 8944 /* 8945 * We got a command we don't support. This shouldn't 8946 * happen, commands should be filtered out above us. 8947 */ 8948 ctl_set_invalid_opcode(ctsio); 8949 ctl_done((union ctl_io *)ctsio); 8950 8951 return (CTL_RETVAL_COMPLETE); 8952 break; /* NOTREACHED */ 8953 } 8954 8955 /* 8956 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8957 * interesting for us, but if RAIDCore is in write-back mode, 8958 * getting it to do write-through for a particular transaction may 8959 * not be possible. 8960 */ 8961 8962 /* 8963 * The first check is to make sure we're in bounds, the second 8964 * check is to catch wrap-around problems. If the lba + num blocks 8965 * is less than the lba, then we've wrapped around and the block 8966 * range is invalid anyway. 8967 */ 8968 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8969 || ((lba + num_blocks) < lba)) { 8970 ctl_set_lba_out_of_range(ctsio); 8971 ctl_done((union ctl_io *)ctsio); 8972 return (CTL_RETVAL_COMPLETE); 8973 } 8974 8975 /* 8976 * According to SBC-3, a transfer length of 0 is not an error. 8977 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8978 * translates to 256 blocks for those commands. 8979 */ 8980 if (num_blocks == 0) { 8981 ctl_set_success(ctsio); 8982 ctl_done((union ctl_io *)ctsio); 8983 return (CTL_RETVAL_COMPLETE); 8984 } 8985 8986 lbalen = (struct ctl_lba_len_flags *) 8987 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8988 lbalen->lba = lba; 8989 lbalen->len = num_blocks; 8990 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 8991 8992 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8993 ctsio->kern_rel_offset = 0; 8994 8995 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8996 8997 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8998 8999 return (retval); 9000} 9001 9002static int 9003ctl_cnw_cont(union ctl_io *io) 9004{ 9005 struct ctl_scsiio *ctsio; 9006 struct ctl_lun *lun; 9007 struct ctl_lba_len_flags *lbalen; 9008 int retval; 9009 9010 ctsio = &io->scsiio; 9011 ctsio->io_hdr.status = CTL_STATUS_NONE; 9012 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9013 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9014 lbalen = (struct ctl_lba_len_flags *) 9015 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9016 lbalen->flags = CTL_LLF_WRITE; 9017 9018 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9019 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9020 return (retval); 9021} 9022 9023int 9024ctl_cnw(struct ctl_scsiio *ctsio) 9025{ 9026 struct ctl_lun *lun; 9027 struct ctl_lba_len_flags *lbalen; 9028 uint64_t lba; 9029 uint32_t num_blocks; 9030 int fua, dpo; 9031 int retval; 9032 9033 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9034 9035 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9036 9037 fua = 0; 9038 dpo = 0; 9039 9040 retval = CTL_RETVAL_COMPLETE; 9041 9042 switch (ctsio->cdb[0]) { 9043 case COMPARE_AND_WRITE: { 9044 struct scsi_compare_and_write *cdb; 9045 9046 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9047 9048 if (cdb->byte2 & SRW10_FUA) 9049 fua = 1; 9050 if (cdb->byte2 & SRW10_DPO) 9051 dpo = 1; 9052 lba = scsi_8btou64(cdb->addr); 9053 num_blocks = cdb->length; 9054 break; 9055 } 9056 default: 9057 /* 9058 * We got a command we don't support. This shouldn't 9059 * happen, commands should be filtered out above us. 9060 */ 9061 ctl_set_invalid_opcode(ctsio); 9062 ctl_done((union ctl_io *)ctsio); 9063 9064 return (CTL_RETVAL_COMPLETE); 9065 break; /* NOTREACHED */ 9066 } 9067 9068 /* 9069 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9070 * interesting for us, but if RAIDCore is in write-back mode, 9071 * getting it to do write-through for a particular transaction may 9072 * not be possible. 9073 */ 9074 9075 /* 9076 * The first check is to make sure we're in bounds, the second 9077 * check is to catch wrap-around problems. If the lba + num blocks 9078 * is less than the lba, then we've wrapped around and the block 9079 * range is invalid anyway. 9080 */ 9081 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9082 || ((lba + num_blocks) < lba)) { 9083 ctl_set_lba_out_of_range(ctsio); 9084 ctl_done((union ctl_io *)ctsio); 9085 return (CTL_RETVAL_COMPLETE); 9086 } 9087 9088 /* 9089 * According to SBC-3, a transfer length of 0 is not an error. 9090 */ 9091 if (num_blocks == 0) { 9092 ctl_set_success(ctsio); 9093 ctl_done((union ctl_io *)ctsio); 9094 return (CTL_RETVAL_COMPLETE); 9095 } 9096 9097 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9098 ctsio->kern_rel_offset = 0; 9099 9100 /* 9101 * Set the IO_CONT flag, so that if this I/O gets passed to 9102 * ctl_data_submit_done(), it'll get passed back to 9103 * ctl_ctl_cnw_cont() for further processing. 9104 */ 9105 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9106 ctsio->io_cont = ctl_cnw_cont; 9107 9108 lbalen = (struct ctl_lba_len_flags *) 9109 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9110 lbalen->lba = lba; 9111 lbalen->len = num_blocks; 9112 lbalen->flags = CTL_LLF_COMPARE; 9113 9114 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9115 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9116 return (retval); 9117} 9118 9119int 9120ctl_verify(struct ctl_scsiio *ctsio) 9121{ 9122 struct ctl_lun *lun; 9123 struct ctl_lba_len_flags *lbalen; 9124 uint64_t lba; 9125 uint32_t num_blocks; 9126 int bytchk, dpo; 9127 int retval; 9128 9129 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9130 9131 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9132 9133 bytchk = 0; 9134 dpo = 0; 9135 retval = CTL_RETVAL_COMPLETE; 9136 9137 switch (ctsio->cdb[0]) { 9138 case VERIFY_10: { 9139 struct scsi_verify_10 *cdb; 9140 9141 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9142 if (cdb->byte2 & SVFY_BYTCHK) 9143 bytchk = 1; 9144 if (cdb->byte2 & SVFY_DPO) 9145 dpo = 1; 9146 lba = scsi_4btoul(cdb->addr); 9147 num_blocks = scsi_2btoul(cdb->length); 9148 break; 9149 } 9150 case VERIFY_12: { 9151 struct scsi_verify_12 *cdb; 9152 9153 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9154 if (cdb->byte2 & SVFY_BYTCHK) 9155 bytchk = 1; 9156 if (cdb->byte2 & SVFY_DPO) 9157 dpo = 1; 9158 lba = scsi_4btoul(cdb->addr); 9159 num_blocks = scsi_4btoul(cdb->length); 9160 break; 9161 } 9162 case VERIFY_16: { 9163 struct scsi_rw_16 *cdb; 9164 9165 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9166 if (cdb->byte2 & SVFY_BYTCHK) 9167 bytchk = 1; 9168 if (cdb->byte2 & SVFY_DPO) 9169 dpo = 1; 9170 lba = scsi_8btou64(cdb->addr); 9171 num_blocks = scsi_4btoul(cdb->length); 9172 break; 9173 } 9174 default: 9175 /* 9176 * We got a command we don't support. This shouldn't 9177 * happen, commands should be filtered out above us. 9178 */ 9179 ctl_set_invalid_opcode(ctsio); 9180 ctl_done((union ctl_io *)ctsio); 9181 return (CTL_RETVAL_COMPLETE); 9182 } 9183 9184 /* 9185 * The first check is to make sure we're in bounds, the second 9186 * check is to catch wrap-around problems. If the lba + num blocks 9187 * is less than the lba, then we've wrapped around and the block 9188 * range is invalid anyway. 9189 */ 9190 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9191 || ((lba + num_blocks) < lba)) { 9192 ctl_set_lba_out_of_range(ctsio); 9193 ctl_done((union ctl_io *)ctsio); 9194 return (CTL_RETVAL_COMPLETE); 9195 } 9196 9197 /* 9198 * According to SBC-3, a transfer length of 0 is not an error. 9199 */ 9200 if (num_blocks == 0) { 9201 ctl_set_success(ctsio); 9202 ctl_done((union ctl_io *)ctsio); 9203 return (CTL_RETVAL_COMPLETE); 9204 } 9205 9206 lbalen = (struct ctl_lba_len_flags *) 9207 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9208 lbalen->lba = lba; 9209 lbalen->len = num_blocks; 9210 if (bytchk) { 9211 lbalen->flags = CTL_LLF_COMPARE; 9212 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9213 } else { 9214 lbalen->flags = CTL_LLF_VERIFY; 9215 ctsio->kern_total_len = 0; 9216 } 9217 ctsio->kern_rel_offset = 0; 9218 9219 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9220 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9221 return (retval); 9222} 9223 9224int 9225ctl_report_luns(struct ctl_scsiio *ctsio) 9226{ 9227 struct scsi_report_luns *cdb; 9228 struct scsi_report_luns_data *lun_data; 9229 struct ctl_lun *lun, *request_lun; 9230 int num_luns, retval; 9231 uint32_t alloc_len, lun_datalen; 9232 int num_filled, well_known; 9233 uint32_t initidx, targ_lun_id, lun_id; 9234 9235 retval = CTL_RETVAL_COMPLETE; 9236 well_known = 0; 9237 9238 cdb = (struct scsi_report_luns *)ctsio->cdb; 9239 9240 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9241 9242 mtx_lock(&control_softc->ctl_lock); 9243 num_luns = control_softc->num_luns; 9244 mtx_unlock(&control_softc->ctl_lock); 9245 9246 switch (cdb->select_report) { 9247 case RPL_REPORT_DEFAULT: 9248 case RPL_REPORT_ALL: 9249 break; 9250 case RPL_REPORT_WELLKNOWN: 9251 well_known = 1; 9252 num_luns = 0; 9253 break; 9254 default: 9255 ctl_set_invalid_field(ctsio, 9256 /*sks_valid*/ 1, 9257 /*command*/ 1, 9258 /*field*/ 2, 9259 /*bit_valid*/ 0, 9260 /*bit*/ 0); 9261 ctl_done((union ctl_io *)ctsio); 9262 return (retval); 9263 break; /* NOTREACHED */ 9264 } 9265 9266 alloc_len = scsi_4btoul(cdb->length); 9267 /* 9268 * The initiator has to allocate at least 16 bytes for this request, 9269 * so he can at least get the header and the first LUN. Otherwise 9270 * we reject the request (per SPC-3 rev 14, section 6.21). 9271 */ 9272 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9273 sizeof(struct scsi_report_luns_lundata))) { 9274 ctl_set_invalid_field(ctsio, 9275 /*sks_valid*/ 1, 9276 /*command*/ 1, 9277 /*field*/ 6, 9278 /*bit_valid*/ 0, 9279 /*bit*/ 0); 9280 ctl_done((union ctl_io *)ctsio); 9281 return (retval); 9282 } 9283 9284 request_lun = (struct ctl_lun *) 9285 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9286 9287 lun_datalen = sizeof(*lun_data) + 9288 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9289 9290 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9291 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9292 ctsio->kern_sg_entries = 0; 9293 9294 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9295 9296 mtx_lock(&control_softc->ctl_lock); 9297 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9298 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9299 if (lun_id >= CTL_MAX_LUNS) 9300 continue; 9301 lun = control_softc->ctl_luns[lun_id]; 9302 if (lun == NULL) 9303 continue; 9304 9305 if (targ_lun_id <= 0xff) { 9306 /* 9307 * Peripheral addressing method, bus number 0. 9308 */ 9309 lun_data->luns[num_filled].lundata[0] = 9310 RPL_LUNDATA_ATYP_PERIPH; 9311 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9312 num_filled++; 9313 } else if (targ_lun_id <= 0x3fff) { 9314 /* 9315 * Flat addressing method. 9316 */ 9317 lun_data->luns[num_filled].lundata[0] = 9318 RPL_LUNDATA_ATYP_FLAT | 9319 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9320#ifdef OLDCTLHEADERS 9321 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9322 (targ_lun_id & SRLD_BUS_LUN_MASK); 9323#endif 9324 lun_data->luns[num_filled].lundata[1] = 9325#ifdef OLDCTLHEADERS 9326 targ_lun_id >> SRLD_BUS_LUN_BITS; 9327#endif 9328 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9329 num_filled++; 9330 } else { 9331 printf("ctl_report_luns: bogus LUN number %jd, " 9332 "skipping\n", (intmax_t)targ_lun_id); 9333 } 9334 /* 9335 * According to SPC-3, rev 14 section 6.21: 9336 * 9337 * "The execution of a REPORT LUNS command to any valid and 9338 * installed logical unit shall clear the REPORTED LUNS DATA 9339 * HAS CHANGED unit attention condition for all logical 9340 * units of that target with respect to the requesting 9341 * initiator. A valid and installed logical unit is one 9342 * having a PERIPHERAL QUALIFIER of 000b in the standard 9343 * INQUIRY data (see 6.4.2)." 9344 * 9345 * If request_lun is NULL, the LUN this report luns command 9346 * was issued to is either disabled or doesn't exist. In that 9347 * case, we shouldn't clear any pending lun change unit 9348 * attention. 9349 */ 9350 if (request_lun != NULL) { 9351 mtx_lock(&lun->lun_lock); 9352 lun->pending_sense[initidx].ua_pending &= 9353 ~CTL_UA_LUN_CHANGE; 9354 mtx_unlock(&lun->lun_lock); 9355 } 9356 } 9357 mtx_unlock(&control_softc->ctl_lock); 9358 9359 /* 9360 * It's quite possible that we've returned fewer LUNs than we allocated 9361 * space for. Trim it. 9362 */ 9363 lun_datalen = sizeof(*lun_data) + 9364 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9365 9366 if (lun_datalen < alloc_len) { 9367 ctsio->residual = alloc_len - lun_datalen; 9368 ctsio->kern_data_len = lun_datalen; 9369 ctsio->kern_total_len = lun_datalen; 9370 } else { 9371 ctsio->residual = 0; 9372 ctsio->kern_data_len = alloc_len; 9373 ctsio->kern_total_len = alloc_len; 9374 } 9375 ctsio->kern_data_resid = 0; 9376 ctsio->kern_rel_offset = 0; 9377 ctsio->kern_sg_entries = 0; 9378 9379 /* 9380 * We set this to the actual data length, regardless of how much 9381 * space we actually have to return results. If the user looks at 9382 * this value, he'll know whether or not he allocated enough space 9383 * and reissue the command if necessary. We don't support well 9384 * known logical units, so if the user asks for that, return none. 9385 */ 9386 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9387 9388 /* 9389 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9390 * this request. 9391 */ 9392 ctsio->scsi_status = SCSI_STATUS_OK; 9393 9394 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9395 ctsio->be_move_done = ctl_config_move_done; 9396 ctl_datamove((union ctl_io *)ctsio); 9397 9398 return (retval); 9399} 9400 9401int 9402ctl_request_sense(struct ctl_scsiio *ctsio) 9403{ 9404 struct scsi_request_sense *cdb; 9405 struct scsi_sense_data *sense_ptr; 9406 struct ctl_lun *lun; 9407 uint32_t initidx; 9408 int have_error; 9409 scsi_sense_data_type sense_format; 9410 9411 cdb = (struct scsi_request_sense *)ctsio->cdb; 9412 9413 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9414 9415 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9416 9417 /* 9418 * Determine which sense format the user wants. 9419 */ 9420 if (cdb->byte2 & SRS_DESC) 9421 sense_format = SSD_TYPE_DESC; 9422 else 9423 sense_format = SSD_TYPE_FIXED; 9424 9425 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9426 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9427 ctsio->kern_sg_entries = 0; 9428 9429 /* 9430 * struct scsi_sense_data, which is currently set to 256 bytes, is 9431 * larger than the largest allowed value for the length field in the 9432 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9433 */ 9434 ctsio->residual = 0; 9435 ctsio->kern_data_len = cdb->length; 9436 ctsio->kern_total_len = cdb->length; 9437 9438 ctsio->kern_data_resid = 0; 9439 ctsio->kern_rel_offset = 0; 9440 ctsio->kern_sg_entries = 0; 9441 9442 /* 9443 * If we don't have a LUN, we don't have any pending sense. 9444 */ 9445 if (lun == NULL) 9446 goto no_sense; 9447 9448 have_error = 0; 9449 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9450 /* 9451 * Check for pending sense, and then for pending unit attentions. 9452 * Pending sense gets returned first, then pending unit attentions. 9453 */ 9454 mtx_lock(&lun->lun_lock); 9455 if (ctl_is_set(lun->have_ca, initidx)) { 9456 scsi_sense_data_type stored_format; 9457 9458 /* 9459 * Check to see which sense format was used for the stored 9460 * sense data. 9461 */ 9462 stored_format = scsi_sense_type( 9463 &lun->pending_sense[initidx].sense); 9464 9465 /* 9466 * If the user requested a different sense format than the 9467 * one we stored, then we need to convert it to the other 9468 * format. If we're going from descriptor to fixed format 9469 * sense data, we may lose things in translation, depending 9470 * on what options were used. 9471 * 9472 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9473 * for some reason we'll just copy it out as-is. 9474 */ 9475 if ((stored_format == SSD_TYPE_FIXED) 9476 && (sense_format == SSD_TYPE_DESC)) 9477 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9478 &lun->pending_sense[initidx].sense, 9479 (struct scsi_sense_data_desc *)sense_ptr); 9480 else if ((stored_format == SSD_TYPE_DESC) 9481 && (sense_format == SSD_TYPE_FIXED)) 9482 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9483 &lun->pending_sense[initidx].sense, 9484 (struct scsi_sense_data_fixed *)sense_ptr); 9485 else 9486 memcpy(sense_ptr, &lun->pending_sense[initidx].sense, 9487 ctl_min(sizeof(*sense_ptr), 9488 sizeof(lun->pending_sense[initidx].sense))); 9489 9490 ctl_clear_mask(lun->have_ca, initidx); 9491 have_error = 1; 9492 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) { 9493 ctl_ua_type ua_type; 9494 9495 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending, 9496 sense_ptr, sense_format); 9497 if (ua_type != CTL_UA_NONE) { 9498 have_error = 1; 9499 /* We're reporting this UA, so clear it */ 9500 lun->pending_sense[initidx].ua_pending &= ~ua_type; 9501 } 9502 } 9503 mtx_unlock(&lun->lun_lock); 9504 9505 /* 9506 * We already have a pending error, return it. 9507 */ 9508 if (have_error != 0) { 9509 /* 9510 * We report the SCSI status as OK, since the status of the 9511 * request sense command itself is OK. 9512 */ 9513 ctsio->scsi_status = SCSI_STATUS_OK; 9514 9515 /* 9516 * We report 0 for the sense length, because we aren't doing 9517 * autosense in this case. We're reporting sense as 9518 * parameter data. 9519 */ 9520 ctsio->sense_len = 0; 9521 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9522 ctsio->be_move_done = ctl_config_move_done; 9523 ctl_datamove((union ctl_io *)ctsio); 9524 9525 return (CTL_RETVAL_COMPLETE); 9526 } 9527 9528no_sense: 9529 9530 /* 9531 * No sense information to report, so we report that everything is 9532 * okay. 9533 */ 9534 ctl_set_sense_data(sense_ptr, 9535 lun, 9536 sense_format, 9537 /*current_error*/ 1, 9538 /*sense_key*/ SSD_KEY_NO_SENSE, 9539 /*asc*/ 0x00, 9540 /*ascq*/ 0x00, 9541 SSD_ELEM_NONE); 9542 9543 ctsio->scsi_status = SCSI_STATUS_OK; 9544 9545 /* 9546 * We report 0 for the sense length, because we aren't doing 9547 * autosense in this case. We're reporting sense as parameter data. 9548 */ 9549 ctsio->sense_len = 0; 9550 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9551 ctsio->be_move_done = ctl_config_move_done; 9552 ctl_datamove((union ctl_io *)ctsio); 9553 9554 return (CTL_RETVAL_COMPLETE); 9555} 9556 9557int 9558ctl_tur(struct ctl_scsiio *ctsio) 9559{ 9560 struct ctl_lun *lun; 9561 9562 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9563 9564 CTL_DEBUG_PRINT(("ctl_tur\n")); 9565 9566 if (lun == NULL) 9567 return (EINVAL); 9568 9569 ctsio->scsi_status = SCSI_STATUS_OK; 9570 ctsio->io_hdr.status = CTL_SUCCESS; 9571 9572 ctl_done((union ctl_io *)ctsio); 9573 9574 return (CTL_RETVAL_COMPLETE); 9575} 9576 9577#ifdef notyet 9578static int 9579ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9580{ 9581 9582} 9583#endif 9584 9585static int 9586ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9587{ 9588 struct scsi_vpd_supported_pages *pages; 9589 int sup_page_size; 9590 struct ctl_lun *lun; 9591 9592 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9593 9594 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9595 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9596 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9597 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9598 ctsio->kern_sg_entries = 0; 9599 9600 if (sup_page_size < alloc_len) { 9601 ctsio->residual = alloc_len - sup_page_size; 9602 ctsio->kern_data_len = sup_page_size; 9603 ctsio->kern_total_len = sup_page_size; 9604 } else { 9605 ctsio->residual = 0; 9606 ctsio->kern_data_len = alloc_len; 9607 ctsio->kern_total_len = alloc_len; 9608 } 9609 ctsio->kern_data_resid = 0; 9610 ctsio->kern_rel_offset = 0; 9611 ctsio->kern_sg_entries = 0; 9612 9613 /* 9614 * The control device is always connected. The disk device, on the 9615 * other hand, may not be online all the time. Need to change this 9616 * to figure out whether the disk device is actually online or not. 9617 */ 9618 if (lun != NULL) 9619 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9620 lun->be_lun->lun_type; 9621 else 9622 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9623 9624 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9625 /* Supported VPD pages */ 9626 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9627 /* Serial Number */ 9628 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9629 /* Device Identification */ 9630 pages->page_list[2] = SVPD_DEVICE_ID; 9631 /* Block limits */ 9632 pages->page_list[3] = SVPD_BLOCK_LIMITS; 9633 /* Logical Block Provisioning */ 9634 pages->page_list[4] = SVPD_LBP; 9635 9636 ctsio->scsi_status = SCSI_STATUS_OK; 9637 9638 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9639 ctsio->be_move_done = ctl_config_move_done; 9640 ctl_datamove((union ctl_io *)ctsio); 9641 9642 return (CTL_RETVAL_COMPLETE); 9643} 9644 9645static int 9646ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9647{ 9648 struct scsi_vpd_unit_serial_number *sn_ptr; 9649 struct ctl_lun *lun; 9650 9651 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9652 9653 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9654 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9655 ctsio->kern_sg_entries = 0; 9656 9657 if (sizeof(*sn_ptr) < alloc_len) { 9658 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9659 ctsio->kern_data_len = sizeof(*sn_ptr); 9660 ctsio->kern_total_len = sizeof(*sn_ptr); 9661 } else { 9662 ctsio->residual = 0; 9663 ctsio->kern_data_len = alloc_len; 9664 ctsio->kern_total_len = alloc_len; 9665 } 9666 ctsio->kern_data_resid = 0; 9667 ctsio->kern_rel_offset = 0; 9668 ctsio->kern_sg_entries = 0; 9669 9670 /* 9671 * The control device is always connected. The disk device, on the 9672 * other hand, may not be online all the time. Need to change this 9673 * to figure out whether the disk device is actually online or not. 9674 */ 9675 if (lun != NULL) 9676 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9677 lun->be_lun->lun_type; 9678 else 9679 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9680 9681 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9682 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9683 /* 9684 * If we don't have a LUN, we just leave the serial number as 9685 * all spaces. 9686 */ 9687 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9688 if (lun != NULL) { 9689 strncpy((char *)sn_ptr->serial_num, 9690 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9691 } 9692 ctsio->scsi_status = SCSI_STATUS_OK; 9693 9694 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9695 ctsio->be_move_done = ctl_config_move_done; 9696 ctl_datamove((union ctl_io *)ctsio); 9697 9698 return (CTL_RETVAL_COMPLETE); 9699} 9700 9701 9702static int 9703ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9704{ 9705 struct scsi_vpd_device_id *devid_ptr; 9706 struct scsi_vpd_id_descriptor *desc; 9707 struct ctl_softc *ctl_softc; 9708 struct ctl_lun *lun; 9709 struct ctl_port *port; 9710 int data_len; 9711 uint8_t proto; 9712 9713 ctl_softc = control_softc; 9714 9715 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9716 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9717 9718 data_len = sizeof(struct scsi_vpd_device_id) + 9719 sizeof(struct scsi_vpd_id_descriptor) + 9720 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9721 sizeof(struct scsi_vpd_id_descriptor) + 9722 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9723 if (lun && lun->lun_devid) 9724 data_len += lun->lun_devid->len; 9725 if (port->port_devid) 9726 data_len += port->port_devid->len; 9727 if (port->target_devid) 9728 data_len += port->target_devid->len; 9729 9730 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9731 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9732 ctsio->kern_sg_entries = 0; 9733 9734 if (data_len < alloc_len) { 9735 ctsio->residual = alloc_len - data_len; 9736 ctsio->kern_data_len = data_len; 9737 ctsio->kern_total_len = data_len; 9738 } else { 9739 ctsio->residual = 0; 9740 ctsio->kern_data_len = alloc_len; 9741 ctsio->kern_total_len = alloc_len; 9742 } 9743 ctsio->kern_data_resid = 0; 9744 ctsio->kern_rel_offset = 0; 9745 ctsio->kern_sg_entries = 0; 9746 9747 /* 9748 * The control device is always connected. The disk device, on the 9749 * other hand, may not be online all the time. 9750 */ 9751 if (lun != NULL) 9752 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9753 lun->be_lun->lun_type; 9754 else 9755 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9756 devid_ptr->page_code = SVPD_DEVICE_ID; 9757 scsi_ulto2b(data_len - 4, devid_ptr->length); 9758 9759 if (port->port_type == CTL_PORT_FC) 9760 proto = SCSI_PROTO_FC << 4; 9761 else if (port->port_type == CTL_PORT_ISCSI) 9762 proto = SCSI_PROTO_ISCSI << 4; 9763 else 9764 proto = SCSI_PROTO_SPI << 4; 9765 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9766 9767 /* 9768 * We're using a LUN association here. i.e., this device ID is a 9769 * per-LUN identifier. 9770 */ 9771 if (lun && lun->lun_devid) { 9772 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9773 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9774 lun->lun_devid->len); 9775 } 9776 9777 /* 9778 * This is for the WWPN which is a port association. 9779 */ 9780 if (port->port_devid) { 9781 memcpy(desc, port->port_devid->data, port->port_devid->len); 9782 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9783 port->port_devid->len); 9784 } 9785 9786 /* 9787 * This is for the Relative Target Port(type 4h) identifier 9788 */ 9789 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9790 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9791 SVPD_ID_TYPE_RELTARG; 9792 desc->length = 4; 9793 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9794 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9795 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9796 9797 /* 9798 * This is for the Target Port Group(type 5h) identifier 9799 */ 9800 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9801 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9802 SVPD_ID_TYPE_TPORTGRP; 9803 desc->length = 4; 9804 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9805 &desc->identifier[2]); 9806 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9807 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9808 9809 /* 9810 * This is for the Target identifier 9811 */ 9812 if (port->target_devid) { 9813 memcpy(desc, port->target_devid->data, port->target_devid->len); 9814 } 9815 9816 ctsio->scsi_status = SCSI_STATUS_OK; 9817 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9818 ctsio->be_move_done = ctl_config_move_done; 9819 ctl_datamove((union ctl_io *)ctsio); 9820 9821 return (CTL_RETVAL_COMPLETE); 9822} 9823 9824static int 9825ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9826{ 9827 struct scsi_vpd_block_limits *bl_ptr; 9828 struct ctl_lun *lun; 9829 int bs; 9830 9831 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9832 bs = lun->be_lun->blocksize; 9833 9834 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9835 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9836 ctsio->kern_sg_entries = 0; 9837 9838 if (sizeof(*bl_ptr) < alloc_len) { 9839 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9840 ctsio->kern_data_len = sizeof(*bl_ptr); 9841 ctsio->kern_total_len = sizeof(*bl_ptr); 9842 } else { 9843 ctsio->residual = 0; 9844 ctsio->kern_data_len = alloc_len; 9845 ctsio->kern_total_len = alloc_len; 9846 } 9847 ctsio->kern_data_resid = 0; 9848 ctsio->kern_rel_offset = 0; 9849 ctsio->kern_sg_entries = 0; 9850 9851 /* 9852 * The control device is always connected. The disk device, on the 9853 * other hand, may not be online all the time. Need to change this 9854 * to figure out whether the disk device is actually online or not. 9855 */ 9856 if (lun != NULL) 9857 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9858 lun->be_lun->lun_type; 9859 else 9860 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9861 9862 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9863 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 9864 bl_ptr->max_cmp_write_len = 0xff; 9865 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9866 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 9867 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9868 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9869 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9870 } 9871 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9872 9873 ctsio->scsi_status = SCSI_STATUS_OK; 9874 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9875 ctsio->be_move_done = ctl_config_move_done; 9876 ctl_datamove((union ctl_io *)ctsio); 9877 9878 return (CTL_RETVAL_COMPLETE); 9879} 9880 9881static int 9882ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9883{ 9884 struct scsi_vpd_logical_block_prov *lbp_ptr; 9885 struct ctl_lun *lun; 9886 int bs; 9887 9888 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9889 bs = lun->be_lun->blocksize; 9890 9891 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9892 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9893 ctsio->kern_sg_entries = 0; 9894 9895 if (sizeof(*lbp_ptr) < alloc_len) { 9896 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9897 ctsio->kern_data_len = sizeof(*lbp_ptr); 9898 ctsio->kern_total_len = sizeof(*lbp_ptr); 9899 } else { 9900 ctsio->residual = 0; 9901 ctsio->kern_data_len = alloc_len; 9902 ctsio->kern_total_len = alloc_len; 9903 } 9904 ctsio->kern_data_resid = 0; 9905 ctsio->kern_rel_offset = 0; 9906 ctsio->kern_sg_entries = 0; 9907 9908 /* 9909 * The control device is always connected. The disk device, on the 9910 * other hand, may not be online all the time. Need to change this 9911 * to figure out whether the disk device is actually online or not. 9912 */ 9913 if (lun != NULL) 9914 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9915 lun->be_lun->lun_type; 9916 else 9917 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9918 9919 lbp_ptr->page_code = SVPD_LBP; 9920 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 9921 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10; 9922 9923 ctsio->scsi_status = SCSI_STATUS_OK; 9924 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9925 ctsio->be_move_done = ctl_config_move_done; 9926 ctl_datamove((union ctl_io *)ctsio); 9927 9928 return (CTL_RETVAL_COMPLETE); 9929} 9930 9931static int 9932ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9933{ 9934 struct scsi_inquiry *cdb; 9935 struct ctl_lun *lun; 9936 int alloc_len, retval; 9937 9938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9939 cdb = (struct scsi_inquiry *)ctsio->cdb; 9940 9941 retval = CTL_RETVAL_COMPLETE; 9942 9943 alloc_len = scsi_2btoul(cdb->length); 9944 9945 switch (cdb->page_code) { 9946 case SVPD_SUPPORTED_PAGES: 9947 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9948 break; 9949 case SVPD_UNIT_SERIAL_NUMBER: 9950 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9951 break; 9952 case SVPD_DEVICE_ID: 9953 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9954 break; 9955 case SVPD_BLOCK_LIMITS: 9956 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9957 break; 9958 case SVPD_LBP: 9959 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9960 break; 9961 default: 9962 ctl_set_invalid_field(ctsio, 9963 /*sks_valid*/ 1, 9964 /*command*/ 1, 9965 /*field*/ 2, 9966 /*bit_valid*/ 0, 9967 /*bit*/ 0); 9968 ctl_done((union ctl_io *)ctsio); 9969 retval = CTL_RETVAL_COMPLETE; 9970 break; 9971 } 9972 9973 return (retval); 9974} 9975 9976static int 9977ctl_inquiry_std(struct ctl_scsiio *ctsio) 9978{ 9979 struct scsi_inquiry_data *inq_ptr; 9980 struct scsi_inquiry *cdb; 9981 struct ctl_softc *ctl_softc; 9982 struct ctl_lun *lun; 9983 char *val; 9984 uint32_t alloc_len; 9985 int is_fc; 9986 9987 ctl_softc = control_softc; 9988 9989 /* 9990 * Figure out whether we're talking to a Fibre Channel port or not. 9991 * We treat the ioctl front end, and any SCSI adapters, as packetized 9992 * SCSI front ends. 9993 */ 9994 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type != 9995 CTL_PORT_FC) 9996 is_fc = 0; 9997 else 9998 is_fc = 1; 9999 10000 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10001 cdb = (struct scsi_inquiry *)ctsio->cdb; 10002 alloc_len = scsi_2btoul(cdb->length); 10003 10004 /* 10005 * We malloc the full inquiry data size here and fill it 10006 * in. If the user only asks for less, we'll give him 10007 * that much. 10008 */ 10009 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10010 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10011 ctsio->kern_sg_entries = 0; 10012 ctsio->kern_data_resid = 0; 10013 ctsio->kern_rel_offset = 0; 10014 10015 if (sizeof(*inq_ptr) < alloc_len) { 10016 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10017 ctsio->kern_data_len = sizeof(*inq_ptr); 10018 ctsio->kern_total_len = sizeof(*inq_ptr); 10019 } else { 10020 ctsio->residual = 0; 10021 ctsio->kern_data_len = alloc_len; 10022 ctsio->kern_total_len = alloc_len; 10023 } 10024 10025 /* 10026 * If we have a LUN configured, report it as connected. Otherwise, 10027 * report that it is offline or no device is supported, depending 10028 * on the value of inquiry_pq_no_lun. 10029 * 10030 * According to the spec (SPC-4 r34), the peripheral qualifier 10031 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10032 * 10033 * "A peripheral device having the specified peripheral device type 10034 * is not connected to this logical unit. However, the device 10035 * server is capable of supporting the specified peripheral device 10036 * type on this logical unit." 10037 * 10038 * According to the same spec, the peripheral qualifier 10039 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10040 * 10041 * "The device server is not capable of supporting a peripheral 10042 * device on this logical unit. For this peripheral qualifier the 10043 * peripheral device type shall be set to 1Fh. All other peripheral 10044 * device type values are reserved for this peripheral qualifier." 10045 * 10046 * Given the text, it would seem that we probably want to report that 10047 * the LUN is offline here. There is no LUN connected, but we can 10048 * support a LUN at the given LUN number. 10049 * 10050 * In the real world, though, it sounds like things are a little 10051 * different: 10052 * 10053 * - Linux, when presented with a LUN with the offline peripheral 10054 * qualifier, will create an sg driver instance for it. So when 10055 * you attach it to CTL, you wind up with a ton of sg driver 10056 * instances. (One for every LUN that Linux bothered to probe.) 10057 * Linux does this despite the fact that it issues a REPORT LUNs 10058 * to LUN 0 to get the inventory of supported LUNs. 10059 * 10060 * - There is other anecdotal evidence (from Emulex folks) about 10061 * arrays that use the offline peripheral qualifier for LUNs that 10062 * are on the "passive" path in an active/passive array. 10063 * 10064 * So the solution is provide a hopefully reasonable default 10065 * (return bad/no LUN) and allow the user to change the behavior 10066 * with a tunable/sysctl variable. 10067 */ 10068 if (lun != NULL) 10069 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10070 lun->be_lun->lun_type; 10071 else if (ctl_softc->inquiry_pq_no_lun == 0) 10072 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10073 else 10074 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10075 10076 /* RMB in byte 2 is 0 */ 10077 inq_ptr->version = SCSI_REV_SPC3; 10078 10079 /* 10080 * According to SAM-3, even if a device only supports a single 10081 * level of LUN addressing, it should still set the HISUP bit: 10082 * 10083 * 4.9.1 Logical unit numbers overview 10084 * 10085 * All logical unit number formats described in this standard are 10086 * hierarchical in structure even when only a single level in that 10087 * hierarchy is used. The HISUP bit shall be set to one in the 10088 * standard INQUIRY data (see SPC-2) when any logical unit number 10089 * format described in this standard is used. Non-hierarchical 10090 * formats are outside the scope of this standard. 10091 * 10092 * Therefore we set the HiSup bit here. 10093 * 10094 * The reponse format is 2, per SPC-3. 10095 */ 10096 inq_ptr->response_format = SID_HiSup | 2; 10097 10098 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 10099 CTL_DEBUG_PRINT(("additional_length = %d\n", 10100 inq_ptr->additional_length)); 10101 10102 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT; 10103 /* 16 bit addressing */ 10104 if (is_fc == 0) 10105 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10106 /* XXX set the SID_MultiP bit here if we're actually going to 10107 respond on multiple ports */ 10108 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10109 10110 /* 16 bit data bus, synchronous transfers */ 10111 /* XXX these flags don't apply for FC */ 10112 if (is_fc == 0) 10113 inq_ptr->flags = SID_WBus16 | SID_Sync; 10114 /* 10115 * XXX KDM do we want to support tagged queueing on the control 10116 * device at all? 10117 */ 10118 if ((lun == NULL) 10119 || (lun->be_lun->lun_type != T_PROCESSOR)) 10120 inq_ptr->flags |= SID_CmdQue; 10121 /* 10122 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10123 * We have 8 bytes for the vendor name, and 16 bytes for the device 10124 * name and 4 bytes for the revision. 10125 */ 10126 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10127 "vendor")) == NULL) { 10128 strcpy(inq_ptr->vendor, CTL_VENDOR); 10129 } else { 10130 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10131 strncpy(inq_ptr->vendor, val, 10132 min(sizeof(inq_ptr->vendor), strlen(val))); 10133 } 10134 if (lun == NULL) { 10135 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10136 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10137 switch (lun->be_lun->lun_type) { 10138 case T_DIRECT: 10139 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10140 break; 10141 case T_PROCESSOR: 10142 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10143 break; 10144 default: 10145 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10146 break; 10147 } 10148 } else { 10149 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10150 strncpy(inq_ptr->product, val, 10151 min(sizeof(inq_ptr->product), strlen(val))); 10152 } 10153 10154 /* 10155 * XXX make this a macro somewhere so it automatically gets 10156 * incremented when we make changes. 10157 */ 10158 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10159 "revision")) == NULL) { 10160 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10161 } else { 10162 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10163 strncpy(inq_ptr->revision, val, 10164 min(sizeof(inq_ptr->revision), strlen(val))); 10165 } 10166 10167 /* 10168 * For parallel SCSI, we support double transition and single 10169 * transition clocking. We also support QAS (Quick Arbitration 10170 * and Selection) and Information Unit transfers on both the 10171 * control and array devices. 10172 */ 10173 if (is_fc == 0) 10174 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10175 SID_SPI_IUS; 10176 10177 /* SAM-3 */ 10178 scsi_ulto2b(0x0060, inq_ptr->version1); 10179 /* SPC-3 (no version claimed) XXX should we claim a version? */ 10180 scsi_ulto2b(0x0300, inq_ptr->version2); 10181 if (is_fc) { 10182 /* FCP-2 ANSI INCITS.350:2003 */ 10183 scsi_ulto2b(0x0917, inq_ptr->version3); 10184 } else { 10185 /* SPI-4 ANSI INCITS.362:200x */ 10186 scsi_ulto2b(0x0B56, inq_ptr->version3); 10187 } 10188 10189 if (lun == NULL) { 10190 /* SBC-2 (no version claimed) XXX should we claim a version? */ 10191 scsi_ulto2b(0x0320, inq_ptr->version4); 10192 } else { 10193 switch (lun->be_lun->lun_type) { 10194 case T_DIRECT: 10195 /* 10196 * SBC-2 (no version claimed) XXX should we claim a 10197 * version? 10198 */ 10199 scsi_ulto2b(0x0320, inq_ptr->version4); 10200 break; 10201 case T_PROCESSOR: 10202 default: 10203 break; 10204 } 10205 } 10206 10207 ctsio->scsi_status = SCSI_STATUS_OK; 10208 if (ctsio->kern_data_len > 0) { 10209 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10210 ctsio->be_move_done = ctl_config_move_done; 10211 ctl_datamove((union ctl_io *)ctsio); 10212 } else { 10213 ctsio->io_hdr.status = CTL_SUCCESS; 10214 ctl_done((union ctl_io *)ctsio); 10215 } 10216 10217 return (CTL_RETVAL_COMPLETE); 10218} 10219 10220int 10221ctl_inquiry(struct ctl_scsiio *ctsio) 10222{ 10223 struct scsi_inquiry *cdb; 10224 int retval; 10225 10226 cdb = (struct scsi_inquiry *)ctsio->cdb; 10227 10228 retval = 0; 10229 10230 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10231 10232 /* 10233 * Right now, we don't support the CmdDt inquiry information. 10234 * This would be nice to support in the future. When we do 10235 * support it, we should change this test so that it checks to make 10236 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10237 */ 10238#ifdef notyet 10239 if (((cdb->byte2 & SI_EVPD) 10240 && (cdb->byte2 & SI_CMDDT))) 10241#endif 10242 if (cdb->byte2 & SI_CMDDT) { 10243 /* 10244 * Point to the SI_CMDDT bit. We might change this 10245 * when we support SI_CMDDT, but since both bits would be 10246 * "wrong", this should probably just stay as-is then. 10247 */ 10248 ctl_set_invalid_field(ctsio, 10249 /*sks_valid*/ 1, 10250 /*command*/ 1, 10251 /*field*/ 1, 10252 /*bit_valid*/ 1, 10253 /*bit*/ 1); 10254 ctl_done((union ctl_io *)ctsio); 10255 return (CTL_RETVAL_COMPLETE); 10256 } 10257 if (cdb->byte2 & SI_EVPD) 10258 retval = ctl_inquiry_evpd(ctsio); 10259#ifdef notyet 10260 else if (cdb->byte2 & SI_CMDDT) 10261 retval = ctl_inquiry_cmddt(ctsio); 10262#endif 10263 else 10264 retval = ctl_inquiry_std(ctsio); 10265 10266 return (retval); 10267} 10268 10269/* 10270 * For known CDB types, parse the LBA and length. 10271 */ 10272static int 10273ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10274{ 10275 if (io->io_hdr.io_type != CTL_IO_SCSI) 10276 return (1); 10277 10278 switch (io->scsiio.cdb[0]) { 10279 case COMPARE_AND_WRITE: { 10280 struct scsi_compare_and_write *cdb; 10281 10282 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10283 10284 *lba = scsi_8btou64(cdb->addr); 10285 *len = cdb->length; 10286 break; 10287 } 10288 case READ_6: 10289 case WRITE_6: { 10290 struct scsi_rw_6 *cdb; 10291 10292 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10293 10294 *lba = scsi_3btoul(cdb->addr); 10295 /* only 5 bits are valid in the most significant address byte */ 10296 *lba &= 0x1fffff; 10297 *len = cdb->length; 10298 break; 10299 } 10300 case READ_10: 10301 case WRITE_10: { 10302 struct scsi_rw_10 *cdb; 10303 10304 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10305 10306 *lba = scsi_4btoul(cdb->addr); 10307 *len = scsi_2btoul(cdb->length); 10308 break; 10309 } 10310 case WRITE_VERIFY_10: { 10311 struct scsi_write_verify_10 *cdb; 10312 10313 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10314 10315 *lba = scsi_4btoul(cdb->addr); 10316 *len = scsi_2btoul(cdb->length); 10317 break; 10318 } 10319 case READ_12: 10320 case WRITE_12: { 10321 struct scsi_rw_12 *cdb; 10322 10323 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10324 10325 *lba = scsi_4btoul(cdb->addr); 10326 *len = scsi_4btoul(cdb->length); 10327 break; 10328 } 10329 case WRITE_VERIFY_12: { 10330 struct scsi_write_verify_12 *cdb; 10331 10332 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10333 10334 *lba = scsi_4btoul(cdb->addr); 10335 *len = scsi_4btoul(cdb->length); 10336 break; 10337 } 10338 case READ_16: 10339 case WRITE_16: { 10340 struct scsi_rw_16 *cdb; 10341 10342 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10343 10344 *lba = scsi_8btou64(cdb->addr); 10345 *len = scsi_4btoul(cdb->length); 10346 break; 10347 } 10348 case WRITE_VERIFY_16: { 10349 struct scsi_write_verify_16 *cdb; 10350 10351 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10352 10353 10354 *lba = scsi_8btou64(cdb->addr); 10355 *len = scsi_4btoul(cdb->length); 10356 break; 10357 } 10358 case WRITE_SAME_10: { 10359 struct scsi_write_same_10 *cdb; 10360 10361 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10362 10363 *lba = scsi_4btoul(cdb->addr); 10364 *len = scsi_2btoul(cdb->length); 10365 break; 10366 } 10367 case WRITE_SAME_16: { 10368 struct scsi_write_same_16 *cdb; 10369 10370 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10371 10372 *lba = scsi_8btou64(cdb->addr); 10373 *len = scsi_4btoul(cdb->length); 10374 break; 10375 } 10376 case VERIFY_10: { 10377 struct scsi_verify_10 *cdb; 10378 10379 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10380 10381 *lba = scsi_4btoul(cdb->addr); 10382 *len = scsi_2btoul(cdb->length); 10383 break; 10384 } 10385 case VERIFY_12: { 10386 struct scsi_verify_12 *cdb; 10387 10388 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10389 10390 *lba = scsi_4btoul(cdb->addr); 10391 *len = scsi_4btoul(cdb->length); 10392 break; 10393 } 10394 case VERIFY_16: { 10395 struct scsi_verify_16 *cdb; 10396 10397 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10398 10399 *lba = scsi_8btou64(cdb->addr); 10400 *len = scsi_4btoul(cdb->length); 10401 break; 10402 } 10403 default: 10404 return (1); 10405 break; /* NOTREACHED */ 10406 } 10407 10408 return (0); 10409} 10410 10411static ctl_action 10412ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10413{ 10414 uint64_t endlba1, endlba2; 10415 10416 endlba1 = lba1 + len1 - 1; 10417 endlba2 = lba2 + len2 - 1; 10418 10419 if ((endlba1 < lba2) 10420 || (endlba2 < lba1)) 10421 return (CTL_ACTION_PASS); 10422 else 10423 return (CTL_ACTION_BLOCK); 10424} 10425 10426static ctl_action 10427ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10428{ 10429 uint64_t lba1, lba2; 10430 uint32_t len1, len2; 10431 int retval; 10432 10433 retval = ctl_get_lba_len(io1, &lba1, &len1); 10434 if (retval != 0) 10435 return (CTL_ACTION_ERROR); 10436 10437 retval = ctl_get_lba_len(io2, &lba2, &len2); 10438 if (retval != 0) 10439 return (CTL_ACTION_ERROR); 10440 10441 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10442} 10443 10444static ctl_action 10445ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10446{ 10447 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10448 ctl_serialize_action *serialize_row; 10449 10450 /* 10451 * The initiator attempted multiple untagged commands at the same 10452 * time. Can't do that. 10453 */ 10454 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10455 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10456 && ((pending_io->io_hdr.nexus.targ_port == 10457 ooa_io->io_hdr.nexus.targ_port) 10458 && (pending_io->io_hdr.nexus.initid.id == 10459 ooa_io->io_hdr.nexus.initid.id)) 10460 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10461 return (CTL_ACTION_OVERLAP); 10462 10463 /* 10464 * The initiator attempted to send multiple tagged commands with 10465 * the same ID. (It's fine if different initiators have the same 10466 * tag ID.) 10467 * 10468 * Even if all of those conditions are true, we don't kill the I/O 10469 * if the command ahead of us has been aborted. We won't end up 10470 * sending it to the FETD, and it's perfectly legal to resend a 10471 * command with the same tag number as long as the previous 10472 * instance of this tag number has been aborted somehow. 10473 */ 10474 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10475 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10476 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10477 && ((pending_io->io_hdr.nexus.targ_port == 10478 ooa_io->io_hdr.nexus.targ_port) 10479 && (pending_io->io_hdr.nexus.initid.id == 10480 ooa_io->io_hdr.nexus.initid.id)) 10481 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10482 return (CTL_ACTION_OVERLAP_TAG); 10483 10484 /* 10485 * If we get a head of queue tag, SAM-3 says that we should 10486 * immediately execute it. 10487 * 10488 * What happens if this command would normally block for some other 10489 * reason? e.g. a request sense with a head of queue tag 10490 * immediately after a write. Normally that would block, but this 10491 * will result in its getting executed immediately... 10492 * 10493 * We currently return "pass" instead of "skip", so we'll end up 10494 * going through the rest of the queue to check for overlapped tags. 10495 * 10496 * XXX KDM check for other types of blockage first?? 10497 */ 10498 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10499 return (CTL_ACTION_PASS); 10500 10501 /* 10502 * Ordered tags have to block until all items ahead of them 10503 * have completed. If we get called with an ordered tag, we always 10504 * block, if something else is ahead of us in the queue. 10505 */ 10506 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10507 return (CTL_ACTION_BLOCK); 10508 10509 /* 10510 * Simple tags get blocked until all head of queue and ordered tags 10511 * ahead of them have completed. I'm lumping untagged commands in 10512 * with simple tags here. XXX KDM is that the right thing to do? 10513 */ 10514 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10515 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10516 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10517 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10518 return (CTL_ACTION_BLOCK); 10519 10520 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10521 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10522 10523 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10524 10525 switch (serialize_row[pending_entry->seridx]) { 10526 case CTL_SER_BLOCK: 10527 return (CTL_ACTION_BLOCK); 10528 break; /* NOTREACHED */ 10529 case CTL_SER_EXTENT: 10530 return (ctl_extent_check(pending_io, ooa_io)); 10531 break; /* NOTREACHED */ 10532 case CTL_SER_PASS: 10533 return (CTL_ACTION_PASS); 10534 break; /* NOTREACHED */ 10535 case CTL_SER_SKIP: 10536 return (CTL_ACTION_SKIP); 10537 break; 10538 default: 10539 panic("invalid serialization value %d", 10540 serialize_row[pending_entry->seridx]); 10541 break; /* NOTREACHED */ 10542 } 10543 10544 return (CTL_ACTION_ERROR); 10545} 10546 10547/* 10548 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10549 * Assumptions: 10550 * - pending_io is generally either incoming, or on the blocked queue 10551 * - starting I/O is the I/O we want to start the check with. 10552 */ 10553static ctl_action 10554ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10555 union ctl_io *starting_io) 10556{ 10557 union ctl_io *ooa_io; 10558 ctl_action action; 10559 10560 mtx_assert(&lun->lun_lock, MA_OWNED); 10561 10562 /* 10563 * Run back along the OOA queue, starting with the current 10564 * blocked I/O and going through every I/O before it on the 10565 * queue. If starting_io is NULL, we'll just end up returning 10566 * CTL_ACTION_PASS. 10567 */ 10568 for (ooa_io = starting_io; ooa_io != NULL; 10569 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10570 ooa_links)){ 10571 10572 /* 10573 * This routine just checks to see whether 10574 * cur_blocked is blocked by ooa_io, which is ahead 10575 * of it in the queue. It doesn't queue/dequeue 10576 * cur_blocked. 10577 */ 10578 action = ctl_check_for_blockage(pending_io, ooa_io); 10579 switch (action) { 10580 case CTL_ACTION_BLOCK: 10581 case CTL_ACTION_OVERLAP: 10582 case CTL_ACTION_OVERLAP_TAG: 10583 case CTL_ACTION_SKIP: 10584 case CTL_ACTION_ERROR: 10585 return (action); 10586 break; /* NOTREACHED */ 10587 case CTL_ACTION_PASS: 10588 break; 10589 default: 10590 panic("invalid action %d", action); 10591 break; /* NOTREACHED */ 10592 } 10593 } 10594 10595 return (CTL_ACTION_PASS); 10596} 10597 10598/* 10599 * Assumptions: 10600 * - An I/O has just completed, and has been removed from the per-LUN OOA 10601 * queue, so some items on the blocked queue may now be unblocked. 10602 */ 10603static int 10604ctl_check_blocked(struct ctl_lun *lun) 10605{ 10606 union ctl_io *cur_blocked, *next_blocked; 10607 10608 mtx_assert(&lun->lun_lock, MA_OWNED); 10609 10610 /* 10611 * Run forward from the head of the blocked queue, checking each 10612 * entry against the I/Os prior to it on the OOA queue to see if 10613 * there is still any blockage. 10614 * 10615 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10616 * with our removing a variable on it while it is traversing the 10617 * list. 10618 */ 10619 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10620 cur_blocked != NULL; cur_blocked = next_blocked) { 10621 union ctl_io *prev_ooa; 10622 ctl_action action; 10623 10624 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10625 blocked_links); 10626 10627 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10628 ctl_ooaq, ooa_links); 10629 10630 /* 10631 * If cur_blocked happens to be the first item in the OOA 10632 * queue now, prev_ooa will be NULL, and the action 10633 * returned will just be CTL_ACTION_PASS. 10634 */ 10635 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10636 10637 switch (action) { 10638 case CTL_ACTION_BLOCK: 10639 /* Nothing to do here, still blocked */ 10640 break; 10641 case CTL_ACTION_OVERLAP: 10642 case CTL_ACTION_OVERLAP_TAG: 10643 /* 10644 * This shouldn't happen! In theory we've already 10645 * checked this command for overlap... 10646 */ 10647 break; 10648 case CTL_ACTION_PASS: 10649 case CTL_ACTION_SKIP: { 10650 struct ctl_softc *softc; 10651 const struct ctl_cmd_entry *entry; 10652 uint32_t initidx; 10653 int isc_retval; 10654 10655 /* 10656 * The skip case shouldn't happen, this transaction 10657 * should have never made it onto the blocked queue. 10658 */ 10659 /* 10660 * This I/O is no longer blocked, we can remove it 10661 * from the blocked queue. Since this is a TAILQ 10662 * (doubly linked list), we can do O(1) removals 10663 * from any place on the list. 10664 */ 10665 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10666 blocked_links); 10667 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10668 10669 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10670 /* 10671 * Need to send IO back to original side to 10672 * run 10673 */ 10674 union ctl_ha_msg msg_info; 10675 10676 msg_info.hdr.original_sc = 10677 cur_blocked->io_hdr.original_sc; 10678 msg_info.hdr.serializing_sc = cur_blocked; 10679 msg_info.hdr.msg_type = CTL_MSG_R2R; 10680 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10681 &msg_info, sizeof(msg_info), 0)) > 10682 CTL_HA_STATUS_SUCCESS) { 10683 printf("CTL:Check Blocked error from " 10684 "ctl_ha_msg_send %d\n", 10685 isc_retval); 10686 } 10687 break; 10688 } 10689 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 10690 softc = control_softc; 10691 10692 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 10693 10694 /* 10695 * Check this I/O for LUN state changes that may 10696 * have happened while this command was blocked. 10697 * The LUN state may have been changed by a command 10698 * ahead of us in the queue, so we need to re-check 10699 * for any states that can be caused by SCSI 10700 * commands. 10701 */ 10702 if (ctl_scsiio_lun_check(softc, lun, entry, 10703 &cur_blocked->scsiio) == 0) { 10704 cur_blocked->io_hdr.flags |= 10705 CTL_FLAG_IS_WAS_ON_RTR; 10706 ctl_enqueue_rtr(cur_blocked); 10707 } else 10708 ctl_done(cur_blocked); 10709 break; 10710 } 10711 default: 10712 /* 10713 * This probably shouldn't happen -- we shouldn't 10714 * get CTL_ACTION_ERROR, or anything else. 10715 */ 10716 break; 10717 } 10718 } 10719 10720 return (CTL_RETVAL_COMPLETE); 10721} 10722 10723/* 10724 * This routine (with one exception) checks LUN flags that can be set by 10725 * commands ahead of us in the OOA queue. These flags have to be checked 10726 * when a command initially comes in, and when we pull a command off the 10727 * blocked queue and are preparing to execute it. The reason we have to 10728 * check these flags for commands on the blocked queue is that the LUN 10729 * state may have been changed by a command ahead of us while we're on the 10730 * blocked queue. 10731 * 10732 * Ordering is somewhat important with these checks, so please pay 10733 * careful attention to the placement of any new checks. 10734 */ 10735static int 10736ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 10737 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10738{ 10739 int retval; 10740 10741 retval = 0; 10742 10743 mtx_assert(&lun->lun_lock, MA_OWNED); 10744 10745 /* 10746 * If this shelf is a secondary shelf controller, we have to reject 10747 * any media access commands. 10748 */ 10749#if 0 10750 /* No longer needed for HA */ 10751 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 10752 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 10753 ctl_set_lun_standby(ctsio); 10754 retval = 1; 10755 goto bailout; 10756 } 10757#endif 10758 10759 /* 10760 * Check for a reservation conflict. If this command isn't allowed 10761 * even on reserved LUNs, and if this initiator isn't the one who 10762 * reserved us, reject the command with a reservation conflict. 10763 */ 10764 if ((lun->flags & CTL_LUN_RESERVED) 10765 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10766 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 10767 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 10768 || (ctsio->io_hdr.nexus.targ_target.id != 10769 lun->rsv_nexus.targ_target.id)) { 10770 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10771 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10772 retval = 1; 10773 goto bailout; 10774 } 10775 } 10776 10777 if ( (lun->flags & CTL_LUN_PR_RESERVED) 10778 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 10779 uint32_t residx; 10780 10781 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 10782 /* 10783 * if we aren't registered or it's a res holder type 10784 * reservation and this isn't the res holder then set a 10785 * conflict. 10786 * NOTE: Commands which might be allowed on write exclusive 10787 * type reservations are checked in the particular command 10788 * for a conflict. Read and SSU are the only ones. 10789 */ 10790 if (!lun->per_res[residx].registered 10791 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10792 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10793 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10794 retval = 1; 10795 goto bailout; 10796 } 10797 10798 } 10799 10800 if ((lun->flags & CTL_LUN_OFFLINE) 10801 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 10802 ctl_set_lun_not_ready(ctsio); 10803 retval = 1; 10804 goto bailout; 10805 } 10806 10807 /* 10808 * If the LUN is stopped, see if this particular command is allowed 10809 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 10810 */ 10811 if ((lun->flags & CTL_LUN_STOPPED) 10812 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10813 /* "Logical unit not ready, initializing cmd. required" */ 10814 ctl_set_lun_stopped(ctsio); 10815 retval = 1; 10816 goto bailout; 10817 } 10818 10819 if ((lun->flags & CTL_LUN_INOPERABLE) 10820 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10821 /* "Medium format corrupted" */ 10822 ctl_set_medium_format_corrupted(ctsio); 10823 retval = 1; 10824 goto bailout; 10825 } 10826 10827bailout: 10828 return (retval); 10829 10830} 10831 10832static void 10833ctl_failover_io(union ctl_io *io, int have_lock) 10834{ 10835 ctl_set_busy(&io->scsiio); 10836 ctl_done(io); 10837} 10838 10839static void 10840ctl_failover(void) 10841{ 10842 struct ctl_lun *lun; 10843 struct ctl_softc *ctl_softc; 10844 union ctl_io *next_io, *pending_io; 10845 union ctl_io *io; 10846 int lun_idx; 10847 int i; 10848 10849 ctl_softc = control_softc; 10850 10851 mtx_lock(&ctl_softc->ctl_lock); 10852 /* 10853 * Remove any cmds from the other SC from the rtr queue. These 10854 * will obviously only be for LUNs for which we're the primary. 10855 * We can't send status or get/send data for these commands. 10856 * Since they haven't been executed yet, we can just remove them. 10857 * We'll either abort them or delete them below, depending on 10858 * which HA mode we're in. 10859 */ 10860#ifdef notyet 10861 mtx_lock(&ctl_softc->queue_lock); 10862 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 10863 io != NULL; io = next_io) { 10864 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 10865 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10866 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 10867 ctl_io_hdr, links); 10868 } 10869 mtx_unlock(&ctl_softc->queue_lock); 10870#endif 10871 10872 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 10873 lun = ctl_softc->ctl_luns[lun_idx]; 10874 if (lun==NULL) 10875 continue; 10876 10877 /* 10878 * Processor LUNs are primary on both sides. 10879 * XXX will this always be true? 10880 */ 10881 if (lun->be_lun->lun_type == T_PROCESSOR) 10882 continue; 10883 10884 if ((lun->flags & CTL_LUN_PRIMARY_SC) 10885 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10886 printf("FAILOVER: primary lun %d\n", lun_idx); 10887 /* 10888 * Remove all commands from the other SC. First from the 10889 * blocked queue then from the ooa queue. Once we have 10890 * removed them. Call ctl_check_blocked to see if there 10891 * is anything that can run. 10892 */ 10893 for (io = (union ctl_io *)TAILQ_FIRST( 10894 &lun->blocked_queue); io != NULL; io = next_io) { 10895 10896 next_io = (union ctl_io *)TAILQ_NEXT( 10897 &io->io_hdr, blocked_links); 10898 10899 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10900 TAILQ_REMOVE(&lun->blocked_queue, 10901 &io->io_hdr,blocked_links); 10902 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10903 TAILQ_REMOVE(&lun->ooa_queue, 10904 &io->io_hdr, ooa_links); 10905 10906 ctl_free_io(io); 10907 } 10908 } 10909 10910 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10911 io != NULL; io = next_io) { 10912 10913 next_io = (union ctl_io *)TAILQ_NEXT( 10914 &io->io_hdr, ooa_links); 10915 10916 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10917 10918 TAILQ_REMOVE(&lun->ooa_queue, 10919 &io->io_hdr, 10920 ooa_links); 10921 10922 ctl_free_io(io); 10923 } 10924 } 10925 ctl_check_blocked(lun); 10926 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 10927 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10928 10929 printf("FAILOVER: primary lun %d\n", lun_idx); 10930 /* 10931 * Abort all commands from the other SC. We can't 10932 * send status back for them now. These should get 10933 * cleaned up when they are completed or come out 10934 * for a datamove operation. 10935 */ 10936 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10937 io != NULL; io = next_io) { 10938 next_io = (union ctl_io *)TAILQ_NEXT( 10939 &io->io_hdr, ooa_links); 10940 10941 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10942 io->io_hdr.flags |= CTL_FLAG_ABORT; 10943 } 10944 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10945 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10946 10947 printf("FAILOVER: secondary lun %d\n", lun_idx); 10948 10949 lun->flags |= CTL_LUN_PRIMARY_SC; 10950 10951 /* 10952 * We send all I/O that was sent to this controller 10953 * and redirected to the other side back with 10954 * busy status, and have the initiator retry it. 10955 * Figuring out how much data has been transferred, 10956 * etc. and picking up where we left off would be 10957 * very tricky. 10958 * 10959 * XXX KDM need to remove I/O from the blocked 10960 * queue as well! 10961 */ 10962 for (pending_io = (union ctl_io *)TAILQ_FIRST( 10963 &lun->ooa_queue); pending_io != NULL; 10964 pending_io = next_io) { 10965 10966 next_io = (union ctl_io *)TAILQ_NEXT( 10967 &pending_io->io_hdr, ooa_links); 10968 10969 pending_io->io_hdr.flags &= 10970 ~CTL_FLAG_SENT_2OTHER_SC; 10971 10972 if (pending_io->io_hdr.flags & 10973 CTL_FLAG_IO_ACTIVE) { 10974 pending_io->io_hdr.flags |= 10975 CTL_FLAG_FAILOVER; 10976 } else { 10977 ctl_set_busy(&pending_io->scsiio); 10978 ctl_done(pending_io); 10979 } 10980 } 10981 10982 /* 10983 * Build Unit Attention 10984 */ 10985 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10986 lun->pending_sense[i].ua_pending |= 10987 CTL_UA_ASYM_ACC_CHANGE; 10988 } 10989 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10990 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10991 printf("FAILOVER: secondary lun %d\n", lun_idx); 10992 /* 10993 * if the first io on the OOA is not on the RtR queue 10994 * add it. 10995 */ 10996 lun->flags |= CTL_LUN_PRIMARY_SC; 10997 10998 pending_io = (union ctl_io *)TAILQ_FIRST( 10999 &lun->ooa_queue); 11000 if (pending_io==NULL) { 11001 printf("Nothing on OOA queue\n"); 11002 continue; 11003 } 11004 11005 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11006 if ((pending_io->io_hdr.flags & 11007 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11008 pending_io->io_hdr.flags |= 11009 CTL_FLAG_IS_WAS_ON_RTR; 11010 ctl_enqueue_rtr(pending_io); 11011 } 11012#if 0 11013 else 11014 { 11015 printf("Tag 0x%04x is running\n", 11016 pending_io->scsiio.tag_num); 11017 } 11018#endif 11019 11020 next_io = (union ctl_io *)TAILQ_NEXT( 11021 &pending_io->io_hdr, ooa_links); 11022 for (pending_io=next_io; pending_io != NULL; 11023 pending_io = next_io) { 11024 pending_io->io_hdr.flags &= 11025 ~CTL_FLAG_SENT_2OTHER_SC; 11026 next_io = (union ctl_io *)TAILQ_NEXT( 11027 &pending_io->io_hdr, ooa_links); 11028 if (pending_io->io_hdr.flags & 11029 CTL_FLAG_IS_WAS_ON_RTR) { 11030#if 0 11031 printf("Tag 0x%04x is running\n", 11032 pending_io->scsiio.tag_num); 11033#endif 11034 continue; 11035 } 11036 11037 switch (ctl_check_ooa(lun, pending_io, 11038 (union ctl_io *)TAILQ_PREV( 11039 &pending_io->io_hdr, ctl_ooaq, 11040 ooa_links))) { 11041 11042 case CTL_ACTION_BLOCK: 11043 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11044 &pending_io->io_hdr, 11045 blocked_links); 11046 pending_io->io_hdr.flags |= 11047 CTL_FLAG_BLOCKED; 11048 break; 11049 case CTL_ACTION_PASS: 11050 case CTL_ACTION_SKIP: 11051 pending_io->io_hdr.flags |= 11052 CTL_FLAG_IS_WAS_ON_RTR; 11053 ctl_enqueue_rtr(pending_io); 11054 break; 11055 case CTL_ACTION_OVERLAP: 11056 ctl_set_overlapped_cmd( 11057 (struct ctl_scsiio *)pending_io); 11058 ctl_done(pending_io); 11059 break; 11060 case CTL_ACTION_OVERLAP_TAG: 11061 ctl_set_overlapped_tag( 11062 (struct ctl_scsiio *)pending_io, 11063 pending_io->scsiio.tag_num & 0xff); 11064 ctl_done(pending_io); 11065 break; 11066 case CTL_ACTION_ERROR: 11067 default: 11068 ctl_set_internal_failure( 11069 (struct ctl_scsiio *)pending_io, 11070 0, // sks_valid 11071 0); //retry count 11072 ctl_done(pending_io); 11073 break; 11074 } 11075 } 11076 11077 /* 11078 * Build Unit Attention 11079 */ 11080 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11081 lun->pending_sense[i].ua_pending |= 11082 CTL_UA_ASYM_ACC_CHANGE; 11083 } 11084 } else { 11085 panic("Unhandled HA mode failover, LUN flags = %#x, " 11086 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11087 } 11088 } 11089 ctl_pause_rtr = 0; 11090 mtx_unlock(&ctl_softc->ctl_lock); 11091} 11092 11093static int 11094ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11095{ 11096 struct ctl_lun *lun; 11097 const struct ctl_cmd_entry *entry; 11098 uint32_t initidx, targ_lun; 11099 int retval; 11100 11101 retval = 0; 11102 11103 lun = NULL; 11104 11105 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11106 if ((targ_lun < CTL_MAX_LUNS) 11107 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11108 lun = ctl_softc->ctl_luns[targ_lun]; 11109 /* 11110 * If the LUN is invalid, pretend that it doesn't exist. 11111 * It will go away as soon as all pending I/O has been 11112 * completed. 11113 */ 11114 if (lun->flags & CTL_LUN_DISABLED) { 11115 lun = NULL; 11116 } else { 11117 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11118 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11119 lun->be_lun; 11120 if (lun->be_lun->lun_type == T_PROCESSOR) { 11121 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11122 } 11123 11124 /* 11125 * Every I/O goes into the OOA queue for a 11126 * particular LUN, and stays there until completion. 11127 */ 11128 mtx_lock(&lun->lun_lock); 11129 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11130 ooa_links); 11131 } 11132 } else { 11133 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11134 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11135 } 11136 11137 /* Get command entry and return error if it is unsuppotyed. */ 11138 entry = ctl_validate_command(ctsio); 11139 if (entry == NULL) { 11140 if (lun) 11141 mtx_unlock(&lun->lun_lock); 11142 return (retval); 11143 } 11144 11145 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11146 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11147 11148 /* 11149 * Check to see whether we can send this command to LUNs that don't 11150 * exist. This should pretty much only be the case for inquiry 11151 * and request sense. Further checks, below, really require having 11152 * a LUN, so we can't really check the command anymore. Just put 11153 * it on the rtr queue. 11154 */ 11155 if (lun == NULL) { 11156 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11157 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11158 ctl_enqueue_rtr((union ctl_io *)ctsio); 11159 return (retval); 11160 } 11161 11162 ctl_set_unsupported_lun(ctsio); 11163 ctl_done((union ctl_io *)ctsio); 11164 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11165 return (retval); 11166 } else { 11167 /* 11168 * Make sure we support this particular command on this LUN. 11169 * e.g., we don't support writes to the control LUN. 11170 */ 11171 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11172 mtx_unlock(&lun->lun_lock); 11173 ctl_set_invalid_opcode(ctsio); 11174 ctl_done((union ctl_io *)ctsio); 11175 return (retval); 11176 } 11177 } 11178 11179 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11180 11181 /* 11182 * If we've got a request sense, it'll clear the contingent 11183 * allegiance condition. Otherwise, if we have a CA condition for 11184 * this initiator, clear it, because it sent down a command other 11185 * than request sense. 11186 */ 11187 if ((ctsio->cdb[0] != REQUEST_SENSE) 11188 && (ctl_is_set(lun->have_ca, initidx))) 11189 ctl_clear_mask(lun->have_ca, initidx); 11190 11191 /* 11192 * If the command has this flag set, it handles its own unit 11193 * attention reporting, we shouldn't do anything. Otherwise we 11194 * check for any pending unit attentions, and send them back to the 11195 * initiator. We only do this when a command initially comes in, 11196 * not when we pull it off the blocked queue. 11197 * 11198 * According to SAM-3, section 5.3.2, the order that things get 11199 * presented back to the host is basically unit attentions caused 11200 * by some sort of reset event, busy status, reservation conflicts 11201 * or task set full, and finally any other status. 11202 * 11203 * One issue here is that some of the unit attentions we report 11204 * don't fall into the "reset" category (e.g. "reported luns data 11205 * has changed"). So reporting it here, before the reservation 11206 * check, may be technically wrong. I guess the only thing to do 11207 * would be to check for and report the reset events here, and then 11208 * check for the other unit attention types after we check for a 11209 * reservation conflict. 11210 * 11211 * XXX KDM need to fix this 11212 */ 11213 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11214 ctl_ua_type ua_type; 11215 11216 ua_type = lun->pending_sense[initidx].ua_pending; 11217 if (ua_type != CTL_UA_NONE) { 11218 scsi_sense_data_type sense_format; 11219 11220 if (lun != NULL) 11221 sense_format = (lun->flags & 11222 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11223 SSD_TYPE_FIXED; 11224 else 11225 sense_format = SSD_TYPE_FIXED; 11226 11227 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11228 sense_format); 11229 if (ua_type != CTL_UA_NONE) { 11230 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11231 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11232 CTL_AUTOSENSE; 11233 ctsio->sense_len = SSD_FULL_SIZE; 11234 lun->pending_sense[initidx].ua_pending &= 11235 ~ua_type; 11236 mtx_unlock(&lun->lun_lock); 11237 ctl_done((union ctl_io *)ctsio); 11238 return (retval); 11239 } 11240 } 11241 } 11242 11243 11244 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11245 mtx_unlock(&lun->lun_lock); 11246 ctl_done((union ctl_io *)ctsio); 11247 return (retval); 11248 } 11249 11250 /* 11251 * XXX CHD this is where we want to send IO to other side if 11252 * this LUN is secondary on this SC. We will need to make a copy 11253 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11254 * the copy we send as FROM_OTHER. 11255 * We also need to stuff the address of the original IO so we can 11256 * find it easily. Something similar will need be done on the other 11257 * side so when we are done we can find the copy. 11258 */ 11259 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11260 union ctl_ha_msg msg_info; 11261 int isc_retval; 11262 11263 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11264 11265 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11266 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11267#if 0 11268 printf("1. ctsio %p\n", ctsio); 11269#endif 11270 msg_info.hdr.serializing_sc = NULL; 11271 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11272 msg_info.scsi.tag_num = ctsio->tag_num; 11273 msg_info.scsi.tag_type = ctsio->tag_type; 11274 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11275 11276 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11277 11278 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11279 (void *)&msg_info, sizeof(msg_info), 0)) > 11280 CTL_HA_STATUS_SUCCESS) { 11281 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11282 isc_retval); 11283 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11284 } else { 11285#if 0 11286 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11287#endif 11288 } 11289 11290 /* 11291 * XXX KDM this I/O is off the incoming queue, but hasn't 11292 * been inserted on any other queue. We may need to come 11293 * up with a holding queue while we wait for serialization 11294 * so that we have an idea of what we're waiting for from 11295 * the other side. 11296 */ 11297 mtx_unlock(&lun->lun_lock); 11298 return (retval); 11299 } 11300 11301 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11302 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11303 ctl_ooaq, ooa_links))) { 11304 case CTL_ACTION_BLOCK: 11305 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11306 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11307 blocked_links); 11308 mtx_unlock(&lun->lun_lock); 11309 return (retval); 11310 case CTL_ACTION_PASS: 11311 case CTL_ACTION_SKIP: 11312 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11313 mtx_unlock(&lun->lun_lock); 11314 ctl_enqueue_rtr((union ctl_io *)ctsio); 11315 break; 11316 case CTL_ACTION_OVERLAP: 11317 mtx_unlock(&lun->lun_lock); 11318 ctl_set_overlapped_cmd(ctsio); 11319 ctl_done((union ctl_io *)ctsio); 11320 break; 11321 case CTL_ACTION_OVERLAP_TAG: 11322 mtx_unlock(&lun->lun_lock); 11323 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11324 ctl_done((union ctl_io *)ctsio); 11325 break; 11326 case CTL_ACTION_ERROR: 11327 default: 11328 mtx_unlock(&lun->lun_lock); 11329 ctl_set_internal_failure(ctsio, 11330 /*sks_valid*/ 0, 11331 /*retry_count*/ 0); 11332 ctl_done((union ctl_io *)ctsio); 11333 break; 11334 } 11335 return (retval); 11336} 11337 11338const struct ctl_cmd_entry * 11339ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11340{ 11341 const struct ctl_cmd_entry *entry; 11342 int service_action; 11343 11344 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11345 if (entry->flags & CTL_CMD_FLAG_SA5) { 11346 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11347 entry = &((const struct ctl_cmd_entry *) 11348 entry->execute)[service_action]; 11349 } 11350 return (entry); 11351} 11352 11353const struct ctl_cmd_entry * 11354ctl_validate_command(struct ctl_scsiio *ctsio) 11355{ 11356 const struct ctl_cmd_entry *entry; 11357 int i; 11358 uint8_t diff; 11359 11360 entry = ctl_get_cmd_entry(ctsio); 11361 if (entry->execute == NULL) { 11362 ctl_set_invalid_opcode(ctsio); 11363 ctl_done((union ctl_io *)ctsio); 11364 return (NULL); 11365 } 11366 KASSERT(entry->length > 0, 11367 ("Not defined length for command 0x%02x/0x%02x", 11368 ctsio->cdb[0], ctsio->cdb[1])); 11369 for (i = 1; i < entry->length; i++) { 11370 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11371 if (diff == 0) 11372 continue; 11373 ctl_set_invalid_field(ctsio, 11374 /*sks_valid*/ 1, 11375 /*command*/ 1, 11376 /*field*/ i, 11377 /*bit_valid*/ 1, 11378 /*bit*/ fls(diff) - 1); 11379 ctl_done((union ctl_io *)ctsio); 11380 return (NULL); 11381 } 11382 return (entry); 11383} 11384 11385static int 11386ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11387{ 11388 11389 switch (lun_type) { 11390 case T_PROCESSOR: 11391 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11392 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11393 return (0); 11394 break; 11395 case T_DIRECT: 11396 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11397 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11398 return (0); 11399 break; 11400 default: 11401 return (0); 11402 } 11403 return (1); 11404} 11405 11406static int 11407ctl_scsiio(struct ctl_scsiio *ctsio) 11408{ 11409 int retval; 11410 const struct ctl_cmd_entry *entry; 11411 11412 retval = CTL_RETVAL_COMPLETE; 11413 11414 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11415 11416 entry = ctl_get_cmd_entry(ctsio); 11417 11418 /* 11419 * If this I/O has been aborted, just send it straight to 11420 * ctl_done() without executing it. 11421 */ 11422 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11423 ctl_done((union ctl_io *)ctsio); 11424 goto bailout; 11425 } 11426 11427 /* 11428 * All the checks should have been handled by ctl_scsiio_precheck(). 11429 * We should be clear now to just execute the I/O. 11430 */ 11431 retval = entry->execute(ctsio); 11432 11433bailout: 11434 return (retval); 11435} 11436 11437/* 11438 * Since we only implement one target right now, a bus reset simply resets 11439 * our single target. 11440 */ 11441static int 11442ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11443{ 11444 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11445} 11446 11447static int 11448ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11449 ctl_ua_type ua_type) 11450{ 11451 struct ctl_lun *lun; 11452 int retval; 11453 11454 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11455 union ctl_ha_msg msg_info; 11456 11457 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11458 msg_info.hdr.nexus = io->io_hdr.nexus; 11459 if (ua_type==CTL_UA_TARG_RESET) 11460 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11461 else 11462 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11463 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11464 msg_info.hdr.original_sc = NULL; 11465 msg_info.hdr.serializing_sc = NULL; 11466 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11467 (void *)&msg_info, sizeof(msg_info), 0)) { 11468 } 11469 } 11470 retval = 0; 11471 11472 mtx_lock(&ctl_softc->ctl_lock); 11473 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11474 retval += ctl_lun_reset(lun, io, ua_type); 11475 mtx_unlock(&ctl_softc->ctl_lock); 11476 11477 return (retval); 11478} 11479 11480/* 11481 * The LUN should always be set. The I/O is optional, and is used to 11482 * distinguish between I/Os sent by this initiator, and by other 11483 * initiators. We set unit attention for initiators other than this one. 11484 * SAM-3 is vague on this point. It does say that a unit attention should 11485 * be established for other initiators when a LUN is reset (see section 11486 * 5.7.3), but it doesn't specifically say that the unit attention should 11487 * be established for this particular initiator when a LUN is reset. Here 11488 * is the relevant text, from SAM-3 rev 8: 11489 * 11490 * 5.7.2 When a SCSI initiator port aborts its own tasks 11491 * 11492 * When a SCSI initiator port causes its own task(s) to be aborted, no 11493 * notification that the task(s) have been aborted shall be returned to 11494 * the SCSI initiator port other than the completion response for the 11495 * command or task management function action that caused the task(s) to 11496 * be aborted and notification(s) associated with related effects of the 11497 * action (e.g., a reset unit attention condition). 11498 * 11499 * XXX KDM for now, we're setting unit attention for all initiators. 11500 */ 11501static int 11502ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11503{ 11504 union ctl_io *xio; 11505#if 0 11506 uint32_t initindex; 11507#endif 11508 int i; 11509 11510 mtx_lock(&lun->lun_lock); 11511 /* 11512 * Run through the OOA queue and abort each I/O. 11513 */ 11514#if 0 11515 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11516#endif 11517 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11518 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11519 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11520 } 11521 11522 /* 11523 * This version sets unit attention for every 11524 */ 11525#if 0 11526 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11527 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11528 if (initindex == i) 11529 continue; 11530 lun->pending_sense[i].ua_pending |= ua_type; 11531 } 11532#endif 11533 11534 /* 11535 * A reset (any kind, really) clears reservations established with 11536 * RESERVE/RELEASE. It does not clear reservations established 11537 * with PERSISTENT RESERVE OUT, but we don't support that at the 11538 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11539 * reservations made with the RESERVE/RELEASE commands, because 11540 * those commands are obsolete in SPC-3. 11541 */ 11542 lun->flags &= ~CTL_LUN_RESERVED; 11543 11544 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11545 ctl_clear_mask(lun->have_ca, i); 11546 lun->pending_sense[i].ua_pending |= ua_type; 11547 } 11548 mtx_unlock(&lun->lun_lock); 11549 11550 return (0); 11551} 11552 11553static int 11554ctl_abort_task(union ctl_io *io) 11555{ 11556 union ctl_io *xio; 11557 struct ctl_lun *lun; 11558 struct ctl_softc *ctl_softc; 11559#if 0 11560 struct sbuf sb; 11561 char printbuf[128]; 11562#endif 11563 int found; 11564 uint32_t targ_lun; 11565 11566 ctl_softc = control_softc; 11567 found = 0; 11568 11569 /* 11570 * Look up the LUN. 11571 */ 11572 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11573 mtx_lock(&ctl_softc->ctl_lock); 11574 if ((targ_lun < CTL_MAX_LUNS) 11575 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11576 lun = ctl_softc->ctl_luns[targ_lun]; 11577 else { 11578 mtx_unlock(&ctl_softc->ctl_lock); 11579 goto bailout; 11580 } 11581 11582#if 0 11583 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11584 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11585#endif 11586 11587 mtx_lock(&lun->lun_lock); 11588 mtx_unlock(&ctl_softc->ctl_lock); 11589 /* 11590 * Run through the OOA queue and attempt to find the given I/O. 11591 * The target port, initiator ID, tag type and tag number have to 11592 * match the values that we got from the initiator. If we have an 11593 * untagged command to abort, simply abort the first untagged command 11594 * we come to. We only allow one untagged command at a time of course. 11595 */ 11596#if 0 11597 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11598#endif 11599 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11600 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11601#if 0 11602 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11603 11604 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11605 lun->lun, xio->scsiio.tag_num, 11606 xio->scsiio.tag_type, 11607 (xio->io_hdr.blocked_links.tqe_prev 11608 == NULL) ? "" : " BLOCKED", 11609 (xio->io_hdr.flags & 11610 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11611 (xio->io_hdr.flags & 11612 CTL_FLAG_ABORT) ? " ABORT" : "", 11613 (xio->io_hdr.flags & 11614 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11615 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11616 sbuf_finish(&sb); 11617 printf("%s\n", sbuf_data(&sb)); 11618#endif 11619 11620 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 11621 && (xio->io_hdr.nexus.initid.id == 11622 io->io_hdr.nexus.initid.id)) { 11623 /* 11624 * If the abort says that the task is untagged, the 11625 * task in the queue must be untagged. Otherwise, 11626 * we just check to see whether the tag numbers 11627 * match. This is because the QLogic firmware 11628 * doesn't pass back the tag type in an abort 11629 * request. 11630 */ 11631#if 0 11632 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11633 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11634 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 11635#endif 11636 /* 11637 * XXX KDM we've got problems with FC, because it 11638 * doesn't send down a tag type with aborts. So we 11639 * can only really go by the tag number... 11640 * This may cause problems with parallel SCSI. 11641 * Need to figure that out!! 11642 */ 11643 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11644 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11645 found = 1; 11646 if ((io->io_hdr.flags & 11647 CTL_FLAG_FROM_OTHER_SC) == 0 && 11648 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11649 union ctl_ha_msg msg_info; 11650 11651 io->io_hdr.flags |= 11652 CTL_FLAG_SENT_2OTHER_SC; 11653 msg_info.hdr.nexus = io->io_hdr.nexus; 11654 msg_info.task.task_action = 11655 CTL_TASK_ABORT_TASK; 11656 msg_info.task.tag_num = 11657 io->taskio.tag_num; 11658 msg_info.task.tag_type = 11659 io->taskio.tag_type; 11660 msg_info.hdr.msg_type = 11661 CTL_MSG_MANAGE_TASKS; 11662 msg_info.hdr.original_sc = NULL; 11663 msg_info.hdr.serializing_sc = NULL; 11664#if 0 11665 printf("Sent Abort to other side\n"); 11666#endif 11667 if (CTL_HA_STATUS_SUCCESS != 11668 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11669 (void *)&msg_info, 11670 sizeof(msg_info), 0)) { 11671 } 11672 } 11673#if 0 11674 printf("ctl_abort_task: found I/O to abort\n"); 11675#endif 11676 break; 11677 } 11678 } 11679 } 11680 mtx_unlock(&lun->lun_lock); 11681 11682bailout: 11683 11684 if (found == 0) { 11685 /* 11686 * This isn't really an error. It's entirely possible for 11687 * the abort and command completion to cross on the wire. 11688 * This is more of an informative/diagnostic error. 11689 */ 11690#if 0 11691 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11692 "%d:%d:%d:%d tag %d type %d\n", 11693 io->io_hdr.nexus.initid.id, 11694 io->io_hdr.nexus.targ_port, 11695 io->io_hdr.nexus.targ_target.id, 11696 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11697 io->taskio.tag_type); 11698#endif 11699 return (1); 11700 } else 11701 return (0); 11702} 11703 11704static void 11705ctl_run_task(union ctl_io *io) 11706{ 11707 struct ctl_softc *ctl_softc; 11708 int retval; 11709 const char *task_desc; 11710 11711 CTL_DEBUG_PRINT(("ctl_run_task\n")); 11712 11713 ctl_softc = control_softc; 11714 retval = 0; 11715 11716 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 11717 ("ctl_run_task: Unextected io_type %d\n", 11718 io->io_hdr.io_type)); 11719 11720 task_desc = ctl_scsi_task_string(&io->taskio); 11721 if (task_desc != NULL) { 11722#ifdef NEEDTOPORT 11723 csevent_log(CSC_CTL | CSC_SHELF_SW | 11724 CTL_TASK_REPORT, 11725 csevent_LogType_Trace, 11726 csevent_Severity_Information, 11727 csevent_AlertLevel_Green, 11728 csevent_FRU_Firmware, 11729 csevent_FRU_Unknown, 11730 "CTL: received task: %s",task_desc); 11731#endif 11732 } else { 11733#ifdef NEEDTOPORT 11734 csevent_log(CSC_CTL | CSC_SHELF_SW | 11735 CTL_TASK_REPORT, 11736 csevent_LogType_Trace, 11737 csevent_Severity_Information, 11738 csevent_AlertLevel_Green, 11739 csevent_FRU_Firmware, 11740 csevent_FRU_Unknown, 11741 "CTL: received unknown task " 11742 "type: %d (%#x)", 11743 io->taskio.task_action, 11744 io->taskio.task_action); 11745#endif 11746 } 11747 switch (io->taskio.task_action) { 11748 case CTL_TASK_ABORT_TASK: 11749 retval = ctl_abort_task(io); 11750 break; 11751 case CTL_TASK_ABORT_TASK_SET: 11752 break; 11753 case CTL_TASK_CLEAR_ACA: 11754 break; 11755 case CTL_TASK_CLEAR_TASK_SET: 11756 break; 11757 case CTL_TASK_LUN_RESET: { 11758 struct ctl_lun *lun; 11759 uint32_t targ_lun; 11760 int retval; 11761 11762 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11763 mtx_lock(&ctl_softc->ctl_lock); 11764 if ((targ_lun < CTL_MAX_LUNS) 11765 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11766 lun = ctl_softc->ctl_luns[targ_lun]; 11767 else { 11768 mtx_unlock(&ctl_softc->ctl_lock); 11769 retval = 1; 11770 break; 11771 } 11772 11773 if (!(io->io_hdr.flags & 11774 CTL_FLAG_FROM_OTHER_SC)) { 11775 union ctl_ha_msg msg_info; 11776 11777 io->io_hdr.flags |= 11778 CTL_FLAG_SENT_2OTHER_SC; 11779 msg_info.hdr.msg_type = 11780 CTL_MSG_MANAGE_TASKS; 11781 msg_info.hdr.nexus = io->io_hdr.nexus; 11782 msg_info.task.task_action = 11783 CTL_TASK_LUN_RESET; 11784 msg_info.hdr.original_sc = NULL; 11785 msg_info.hdr.serializing_sc = NULL; 11786 if (CTL_HA_STATUS_SUCCESS != 11787 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11788 (void *)&msg_info, 11789 sizeof(msg_info), 0)) { 11790 } 11791 } 11792 11793 retval = ctl_lun_reset(lun, io, 11794 CTL_UA_LUN_RESET); 11795 mtx_unlock(&ctl_softc->ctl_lock); 11796 break; 11797 } 11798 case CTL_TASK_TARGET_RESET: 11799 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 11800 break; 11801 case CTL_TASK_BUS_RESET: 11802 retval = ctl_bus_reset(ctl_softc, io); 11803 break; 11804 case CTL_TASK_PORT_LOGIN: 11805 break; 11806 case CTL_TASK_PORT_LOGOUT: 11807 break; 11808 default: 11809 printf("ctl_run_task: got unknown task management event %d\n", 11810 io->taskio.task_action); 11811 break; 11812 } 11813 if (retval == 0) 11814 io->io_hdr.status = CTL_SUCCESS; 11815 else 11816 io->io_hdr.status = CTL_ERROR; 11817 11818 /* 11819 * This will queue this I/O to the done queue, but the 11820 * work thread won't be able to process it until we 11821 * return and the lock is released. 11822 */ 11823 ctl_done(io); 11824} 11825 11826/* 11827 * For HA operation. Handle commands that come in from the other 11828 * controller. 11829 */ 11830static void 11831ctl_handle_isc(union ctl_io *io) 11832{ 11833 int free_io; 11834 struct ctl_lun *lun; 11835 struct ctl_softc *ctl_softc; 11836 uint32_t targ_lun; 11837 11838 ctl_softc = control_softc; 11839 11840 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11841 lun = ctl_softc->ctl_luns[targ_lun]; 11842 11843 switch (io->io_hdr.msg_type) { 11844 case CTL_MSG_SERIALIZE: 11845 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 11846 break; 11847 case CTL_MSG_R2R: { 11848 const struct ctl_cmd_entry *entry; 11849 11850 /* 11851 * This is only used in SER_ONLY mode. 11852 */ 11853 free_io = 0; 11854 entry = ctl_get_cmd_entry(&io->scsiio); 11855 mtx_lock(&lun->lun_lock); 11856 if (ctl_scsiio_lun_check(ctl_softc, lun, 11857 entry, (struct ctl_scsiio *)io) != 0) { 11858 mtx_unlock(&lun->lun_lock); 11859 ctl_done(io); 11860 break; 11861 } 11862 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11863 mtx_unlock(&lun->lun_lock); 11864 ctl_enqueue_rtr(io); 11865 break; 11866 } 11867 case CTL_MSG_FINISH_IO: 11868 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 11869 free_io = 0; 11870 ctl_done(io); 11871 } else { 11872 free_io = 1; 11873 mtx_lock(&lun->lun_lock); 11874 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11875 ooa_links); 11876 ctl_check_blocked(lun); 11877 mtx_unlock(&lun->lun_lock); 11878 } 11879 break; 11880 case CTL_MSG_PERS_ACTION: 11881 ctl_hndl_per_res_out_on_other_sc( 11882 (union ctl_ha_msg *)&io->presio.pr_msg); 11883 free_io = 1; 11884 break; 11885 case CTL_MSG_BAD_JUJU: 11886 free_io = 0; 11887 ctl_done(io); 11888 break; 11889 case CTL_MSG_DATAMOVE: 11890 /* Only used in XFER mode */ 11891 free_io = 0; 11892 ctl_datamove_remote(io); 11893 break; 11894 case CTL_MSG_DATAMOVE_DONE: 11895 /* Only used in XFER mode */ 11896 free_io = 0; 11897 io->scsiio.be_move_done(io); 11898 break; 11899 default: 11900 free_io = 1; 11901 printf("%s: Invalid message type %d\n", 11902 __func__, io->io_hdr.msg_type); 11903 break; 11904 } 11905 if (free_io) 11906 ctl_free_io(io); 11907 11908} 11909 11910 11911/* 11912 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11913 * there is no match. 11914 */ 11915static ctl_lun_error_pattern 11916ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11917{ 11918 const struct ctl_cmd_entry *entry; 11919 ctl_lun_error_pattern filtered_pattern, pattern; 11920 11921 pattern = desc->error_pattern; 11922 11923 /* 11924 * XXX KDM we need more data passed into this function to match a 11925 * custom pattern, and we actually need to implement custom pattern 11926 * matching. 11927 */ 11928 if (pattern & CTL_LUN_PAT_CMD) 11929 return (CTL_LUN_PAT_CMD); 11930 11931 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 11932 return (CTL_LUN_PAT_ANY); 11933 11934 entry = ctl_get_cmd_entry(ctsio); 11935 11936 filtered_pattern = entry->pattern & pattern; 11937 11938 /* 11939 * If the user requested specific flags in the pattern (e.g. 11940 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 11941 * flags. 11942 * 11943 * If the user did not specify any flags, it doesn't matter whether 11944 * or not the command supports the flags. 11945 */ 11946 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 11947 (pattern & ~CTL_LUN_PAT_MASK)) 11948 return (CTL_LUN_PAT_NONE); 11949 11950 /* 11951 * If the user asked for a range check, see if the requested LBA 11952 * range overlaps with this command's LBA range. 11953 */ 11954 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 11955 uint64_t lba1; 11956 uint32_t len1; 11957 ctl_action action; 11958 int retval; 11959 11960 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 11961 if (retval != 0) 11962 return (CTL_LUN_PAT_NONE); 11963 11964 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 11965 desc->lba_range.len); 11966 /* 11967 * A "pass" means that the LBA ranges don't overlap, so 11968 * this doesn't match the user's range criteria. 11969 */ 11970 if (action == CTL_ACTION_PASS) 11971 return (CTL_LUN_PAT_NONE); 11972 } 11973 11974 return (filtered_pattern); 11975} 11976 11977static void 11978ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 11979{ 11980 struct ctl_error_desc *desc, *desc2; 11981 11982 mtx_assert(&lun->lun_lock, MA_OWNED); 11983 11984 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 11985 ctl_lun_error_pattern pattern; 11986 /* 11987 * Check to see whether this particular command matches 11988 * the pattern in the descriptor. 11989 */ 11990 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 11991 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 11992 continue; 11993 11994 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 11995 case CTL_LUN_INJ_ABORTED: 11996 ctl_set_aborted(&io->scsiio); 11997 break; 11998 case CTL_LUN_INJ_MEDIUM_ERR: 11999 ctl_set_medium_error(&io->scsiio); 12000 break; 12001 case CTL_LUN_INJ_UA: 12002 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12003 * OCCURRED */ 12004 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12005 break; 12006 case CTL_LUN_INJ_CUSTOM: 12007 /* 12008 * We're assuming the user knows what he is doing. 12009 * Just copy the sense information without doing 12010 * checks. 12011 */ 12012 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12013 ctl_min(sizeof(desc->custom_sense), 12014 sizeof(io->scsiio.sense_data))); 12015 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12016 io->scsiio.sense_len = SSD_FULL_SIZE; 12017 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12018 break; 12019 case CTL_LUN_INJ_NONE: 12020 default: 12021 /* 12022 * If this is an error injection type we don't know 12023 * about, clear the continuous flag (if it is set) 12024 * so it will get deleted below. 12025 */ 12026 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12027 break; 12028 } 12029 /* 12030 * By default, each error injection action is a one-shot 12031 */ 12032 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12033 continue; 12034 12035 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12036 12037 free(desc, M_CTL); 12038 } 12039} 12040 12041#ifdef CTL_IO_DELAY 12042static void 12043ctl_datamove_timer_wakeup(void *arg) 12044{ 12045 union ctl_io *io; 12046 12047 io = (union ctl_io *)arg; 12048 12049 ctl_datamove(io); 12050} 12051#endif /* CTL_IO_DELAY */ 12052 12053void 12054ctl_datamove(union ctl_io *io) 12055{ 12056 void (*fe_datamove)(union ctl_io *io); 12057 12058 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12059 12060 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12061 12062#ifdef CTL_TIME_IO 12063 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12064 char str[256]; 12065 char path_str[64]; 12066 struct sbuf sb; 12067 12068 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12069 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12070 12071 sbuf_cat(&sb, path_str); 12072 switch (io->io_hdr.io_type) { 12073 case CTL_IO_SCSI: 12074 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12075 sbuf_printf(&sb, "\n"); 12076 sbuf_cat(&sb, path_str); 12077 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12078 io->scsiio.tag_num, io->scsiio.tag_type); 12079 break; 12080 case CTL_IO_TASK: 12081 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12082 "Tag Type: %d\n", io->taskio.task_action, 12083 io->taskio.tag_num, io->taskio.tag_type); 12084 break; 12085 default: 12086 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12087 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12088 break; 12089 } 12090 sbuf_cat(&sb, path_str); 12091 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12092 (intmax_t)time_uptime - io->io_hdr.start_time); 12093 sbuf_finish(&sb); 12094 printf("%s", sbuf_data(&sb)); 12095 } 12096#endif /* CTL_TIME_IO */ 12097 12098#ifdef CTL_IO_DELAY 12099 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12100 struct ctl_lun *lun; 12101 12102 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12103 12104 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12105 } else { 12106 struct ctl_lun *lun; 12107 12108 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12109 if ((lun != NULL) 12110 && (lun->delay_info.datamove_delay > 0)) { 12111 struct callout *callout; 12112 12113 callout = (struct callout *)&io->io_hdr.timer_bytes; 12114 callout_init(callout, /*mpsafe*/ 1); 12115 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12116 callout_reset(callout, 12117 lun->delay_info.datamove_delay * hz, 12118 ctl_datamove_timer_wakeup, io); 12119 if (lun->delay_info.datamove_type == 12120 CTL_DELAY_TYPE_ONESHOT) 12121 lun->delay_info.datamove_delay = 0; 12122 return; 12123 } 12124 } 12125#endif 12126 12127 /* 12128 * This command has been aborted. Set the port status, so we fail 12129 * the data move. 12130 */ 12131 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12132 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12133 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12134 io->io_hdr.nexus.targ_port, 12135 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12136 io->io_hdr.nexus.targ_lun); 12137 io->io_hdr.status = CTL_CMD_ABORTED; 12138 io->io_hdr.port_status = 31337; 12139 /* 12140 * Note that the backend, in this case, will get the 12141 * callback in its context. In other cases it may get 12142 * called in the frontend's interrupt thread context. 12143 */ 12144 io->scsiio.be_move_done(io); 12145 return; 12146 } 12147 12148 /* 12149 * If we're in XFER mode and this I/O is from the other shelf 12150 * controller, we need to send the DMA to the other side to 12151 * actually transfer the data to/from the host. In serialize only 12152 * mode the transfer happens below CTL and ctl_datamove() is only 12153 * called on the machine that originally received the I/O. 12154 */ 12155 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12156 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12157 union ctl_ha_msg msg; 12158 uint32_t sg_entries_sent; 12159 int do_sg_copy; 12160 int i; 12161 12162 memset(&msg, 0, sizeof(msg)); 12163 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12164 msg.hdr.original_sc = io->io_hdr.original_sc; 12165 msg.hdr.serializing_sc = io; 12166 msg.hdr.nexus = io->io_hdr.nexus; 12167 msg.dt.flags = io->io_hdr.flags; 12168 /* 12169 * We convert everything into a S/G list here. We can't 12170 * pass by reference, only by value between controllers. 12171 * So we can't pass a pointer to the S/G list, only as many 12172 * S/G entries as we can fit in here. If it's possible for 12173 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12174 * then we need to break this up into multiple transfers. 12175 */ 12176 if (io->scsiio.kern_sg_entries == 0) { 12177 msg.dt.kern_sg_entries = 1; 12178 /* 12179 * If this is in cached memory, flush the cache 12180 * before we send the DMA request to the other 12181 * controller. We want to do this in either the 12182 * read or the write case. The read case is 12183 * straightforward. In the write case, we want to 12184 * make sure nothing is in the local cache that 12185 * could overwrite the DMAed data. 12186 */ 12187 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12188 /* 12189 * XXX KDM use bus_dmamap_sync() here. 12190 */ 12191 } 12192 12193 /* 12194 * Convert to a physical address if this is a 12195 * virtual address. 12196 */ 12197 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12198 msg.dt.sg_list[0].addr = 12199 io->scsiio.kern_data_ptr; 12200 } else { 12201 /* 12202 * XXX KDM use busdma here! 12203 */ 12204#if 0 12205 msg.dt.sg_list[0].addr = (void *) 12206 vtophys(io->scsiio.kern_data_ptr); 12207#endif 12208 } 12209 12210 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12211 do_sg_copy = 0; 12212 } else { 12213 struct ctl_sg_entry *sgl; 12214 12215 do_sg_copy = 1; 12216 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12217 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12218 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12219 /* 12220 * XXX KDM use bus_dmamap_sync() here. 12221 */ 12222 } 12223 } 12224 12225 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12226 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12227 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12228 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12229 msg.dt.sg_sequence = 0; 12230 12231 /* 12232 * Loop until we've sent all of the S/G entries. On the 12233 * other end, we'll recompose these S/G entries into one 12234 * contiguous list before passing it to the 12235 */ 12236 for (sg_entries_sent = 0; sg_entries_sent < 12237 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12238 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12239 sizeof(msg.dt.sg_list[0])), 12240 msg.dt.kern_sg_entries - sg_entries_sent); 12241 12242 if (do_sg_copy != 0) { 12243 struct ctl_sg_entry *sgl; 12244 int j; 12245 12246 sgl = (struct ctl_sg_entry *) 12247 io->scsiio.kern_data_ptr; 12248 /* 12249 * If this is in cached memory, flush the cache 12250 * before we send the DMA request to the other 12251 * controller. We want to do this in either 12252 * the * read or the write case. The read 12253 * case is straightforward. In the write 12254 * case, we want to make sure nothing is 12255 * in the local cache that could overwrite 12256 * the DMAed data. 12257 */ 12258 12259 for (i = sg_entries_sent, j = 0; 12260 i < msg.dt.cur_sg_entries; i++, j++) { 12261 if ((io->io_hdr.flags & 12262 CTL_FLAG_NO_DATASYNC) == 0) { 12263 /* 12264 * XXX KDM use bus_dmamap_sync() 12265 */ 12266 } 12267 if ((io->io_hdr.flags & 12268 CTL_FLAG_BUS_ADDR) == 0) { 12269 /* 12270 * XXX KDM use busdma. 12271 */ 12272#if 0 12273 msg.dt.sg_list[j].addr =(void *) 12274 vtophys(sgl[i].addr); 12275#endif 12276 } else { 12277 msg.dt.sg_list[j].addr = 12278 sgl[i].addr; 12279 } 12280 msg.dt.sg_list[j].len = sgl[i].len; 12281 } 12282 } 12283 12284 sg_entries_sent += msg.dt.cur_sg_entries; 12285 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12286 msg.dt.sg_last = 1; 12287 else 12288 msg.dt.sg_last = 0; 12289 12290 /* 12291 * XXX KDM drop and reacquire the lock here? 12292 */ 12293 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12294 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12295 /* 12296 * XXX do something here. 12297 */ 12298 } 12299 12300 msg.dt.sent_sg_entries = sg_entries_sent; 12301 } 12302 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12303 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12304 ctl_failover_io(io, /*have_lock*/ 0); 12305 12306 } else { 12307 12308 /* 12309 * Lookup the fe_datamove() function for this particular 12310 * front end. 12311 */ 12312 fe_datamove = 12313 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12314 12315 fe_datamove(io); 12316 } 12317} 12318 12319static void 12320ctl_send_datamove_done(union ctl_io *io, int have_lock) 12321{ 12322 union ctl_ha_msg msg; 12323 int isc_status; 12324 12325 memset(&msg, 0, sizeof(msg)); 12326 12327 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12328 msg.hdr.original_sc = io; 12329 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12330 msg.hdr.nexus = io->io_hdr.nexus; 12331 msg.hdr.status = io->io_hdr.status; 12332 msg.scsi.tag_num = io->scsiio.tag_num; 12333 msg.scsi.tag_type = io->scsiio.tag_type; 12334 msg.scsi.scsi_status = io->scsiio.scsi_status; 12335 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12336 sizeof(io->scsiio.sense_data)); 12337 msg.scsi.sense_len = io->scsiio.sense_len; 12338 msg.scsi.sense_residual = io->scsiio.sense_residual; 12339 msg.scsi.fetd_status = io->io_hdr.port_status; 12340 msg.scsi.residual = io->scsiio.residual; 12341 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12342 12343 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12344 ctl_failover_io(io, /*have_lock*/ have_lock); 12345 return; 12346 } 12347 12348 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12349 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12350 /* XXX do something if this fails */ 12351 } 12352 12353} 12354 12355/* 12356 * The DMA to the remote side is done, now we need to tell the other side 12357 * we're done so it can continue with its data movement. 12358 */ 12359static void 12360ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12361{ 12362 union ctl_io *io; 12363 12364 io = rq->context; 12365 12366 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12367 printf("%s: ISC DMA write failed with error %d", __func__, 12368 rq->ret); 12369 ctl_set_internal_failure(&io->scsiio, 12370 /*sks_valid*/ 1, 12371 /*retry_count*/ rq->ret); 12372 } 12373 12374 ctl_dt_req_free(rq); 12375 12376 /* 12377 * In this case, we had to malloc the memory locally. Free it. 12378 */ 12379 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12380 int i; 12381 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12382 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12383 } 12384 /* 12385 * The data is in local and remote memory, so now we need to send 12386 * status (good or back) back to the other side. 12387 */ 12388 ctl_send_datamove_done(io, /*have_lock*/ 0); 12389} 12390 12391/* 12392 * We've moved the data from the host/controller into local memory. Now we 12393 * need to push it over to the remote controller's memory. 12394 */ 12395static int 12396ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12397{ 12398 int retval; 12399 12400 retval = 0; 12401 12402 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12403 ctl_datamove_remote_write_cb); 12404 12405 return (retval); 12406} 12407 12408static void 12409ctl_datamove_remote_write(union ctl_io *io) 12410{ 12411 int retval; 12412 void (*fe_datamove)(union ctl_io *io); 12413 12414 /* 12415 * - Get the data from the host/HBA into local memory. 12416 * - DMA memory from the local controller to the remote controller. 12417 * - Send status back to the remote controller. 12418 */ 12419 12420 retval = ctl_datamove_remote_sgl_setup(io); 12421 if (retval != 0) 12422 return; 12423 12424 /* Switch the pointer over so the FETD knows what to do */ 12425 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12426 12427 /* 12428 * Use a custom move done callback, since we need to send completion 12429 * back to the other controller, not to the backend on this side. 12430 */ 12431 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12432 12433 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12434 12435 fe_datamove(io); 12436 12437 return; 12438 12439} 12440 12441static int 12442ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12443{ 12444#if 0 12445 char str[256]; 12446 char path_str[64]; 12447 struct sbuf sb; 12448#endif 12449 12450 /* 12451 * In this case, we had to malloc the memory locally. Free it. 12452 */ 12453 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12454 int i; 12455 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12456 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12457 } 12458 12459#if 0 12460 scsi_path_string(io, path_str, sizeof(path_str)); 12461 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12462 sbuf_cat(&sb, path_str); 12463 scsi_command_string(&io->scsiio, NULL, &sb); 12464 sbuf_printf(&sb, "\n"); 12465 sbuf_cat(&sb, path_str); 12466 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12467 io->scsiio.tag_num, io->scsiio.tag_type); 12468 sbuf_cat(&sb, path_str); 12469 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12470 io->io_hdr.flags, io->io_hdr.status); 12471 sbuf_finish(&sb); 12472 printk("%s", sbuf_data(&sb)); 12473#endif 12474 12475 12476 /* 12477 * The read is done, now we need to send status (good or bad) back 12478 * to the other side. 12479 */ 12480 ctl_send_datamove_done(io, /*have_lock*/ 0); 12481 12482 return (0); 12483} 12484 12485static void 12486ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12487{ 12488 union ctl_io *io; 12489 void (*fe_datamove)(union ctl_io *io); 12490 12491 io = rq->context; 12492 12493 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12494 printf("%s: ISC DMA read failed with error %d", __func__, 12495 rq->ret); 12496 ctl_set_internal_failure(&io->scsiio, 12497 /*sks_valid*/ 1, 12498 /*retry_count*/ rq->ret); 12499 } 12500 12501 ctl_dt_req_free(rq); 12502 12503 /* Switch the pointer over so the FETD knows what to do */ 12504 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12505 12506 /* 12507 * Use a custom move done callback, since we need to send completion 12508 * back to the other controller, not to the backend on this side. 12509 */ 12510 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12511 12512 /* XXX KDM add checks like the ones in ctl_datamove? */ 12513 12514 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12515 12516 fe_datamove(io); 12517} 12518 12519static int 12520ctl_datamove_remote_sgl_setup(union ctl_io *io) 12521{ 12522 struct ctl_sg_entry *local_sglist, *remote_sglist; 12523 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12524 struct ctl_softc *softc; 12525 int retval; 12526 int i; 12527 12528 retval = 0; 12529 softc = control_softc; 12530 12531 local_sglist = io->io_hdr.local_sglist; 12532 local_dma_sglist = io->io_hdr.local_dma_sglist; 12533 remote_sglist = io->io_hdr.remote_sglist; 12534 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12535 12536 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12537 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12538 local_sglist[i].len = remote_sglist[i].len; 12539 12540 /* 12541 * XXX Detect the situation where the RS-level I/O 12542 * redirector on the other side has already read the 12543 * data off of the AOR RS on this side, and 12544 * transferred it to remote (mirror) memory on the 12545 * other side. Since we already have the data in 12546 * memory here, we just need to use it. 12547 * 12548 * XXX KDM this can probably be removed once we 12549 * get the cache device code in and take the 12550 * current AOR implementation out. 12551 */ 12552#ifdef NEEDTOPORT 12553 if ((remote_sglist[i].addr >= 12554 (void *)vtophys(softc->mirr->addr)) 12555 && (remote_sglist[i].addr < 12556 ((void *)vtophys(softc->mirr->addr) + 12557 CacheMirrorOffset))) { 12558 local_sglist[i].addr = remote_sglist[i].addr - 12559 CacheMirrorOffset; 12560 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12561 CTL_FLAG_DATA_IN) 12562 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12563 } else { 12564 local_sglist[i].addr = remote_sglist[i].addr + 12565 CacheMirrorOffset; 12566 } 12567#endif 12568#if 0 12569 printf("%s: local %p, remote %p, len %d\n", 12570 __func__, local_sglist[i].addr, 12571 remote_sglist[i].addr, local_sglist[i].len); 12572#endif 12573 } 12574 } else { 12575 uint32_t len_to_go; 12576 12577 /* 12578 * In this case, we don't have automatically allocated 12579 * memory for this I/O on this controller. This typically 12580 * happens with internal CTL I/O -- e.g. inquiry, mode 12581 * sense, etc. Anything coming from RAIDCore will have 12582 * a mirror area available. 12583 */ 12584 len_to_go = io->scsiio.kern_data_len; 12585 12586 /* 12587 * Clear the no datasync flag, we have to use malloced 12588 * buffers. 12589 */ 12590 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12591 12592 /* 12593 * The difficult thing here is that the size of the various 12594 * S/G segments may be different than the size from the 12595 * remote controller. That'll make it harder when DMAing 12596 * the data back to the other side. 12597 */ 12598 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12599 sizeof(io->io_hdr.remote_sglist[0])) && 12600 (len_to_go > 0); i++) { 12601 local_sglist[i].len = ctl_min(len_to_go, 131072); 12602 CTL_SIZE_8B(local_dma_sglist[i].len, 12603 local_sglist[i].len); 12604 local_sglist[i].addr = 12605 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12606 12607 local_dma_sglist[i].addr = local_sglist[i].addr; 12608 12609 if (local_sglist[i].addr == NULL) { 12610 int j; 12611 12612 printf("malloc failed for %zd bytes!", 12613 local_dma_sglist[i].len); 12614 for (j = 0; j < i; j++) { 12615 free(local_sglist[j].addr, M_CTL); 12616 } 12617 ctl_set_internal_failure(&io->scsiio, 12618 /*sks_valid*/ 1, 12619 /*retry_count*/ 4857); 12620 retval = 1; 12621 goto bailout_error; 12622 12623 } 12624 /* XXX KDM do we need a sync here? */ 12625 12626 len_to_go -= local_sglist[i].len; 12627 } 12628 /* 12629 * Reset the number of S/G entries accordingly. The 12630 * original number of S/G entries is available in 12631 * rem_sg_entries. 12632 */ 12633 io->scsiio.kern_sg_entries = i; 12634 12635#if 0 12636 printf("%s: kern_sg_entries = %d\n", __func__, 12637 io->scsiio.kern_sg_entries); 12638 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12639 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 12640 local_sglist[i].addr, local_sglist[i].len, 12641 local_dma_sglist[i].len); 12642#endif 12643 } 12644 12645 12646 return (retval); 12647 12648bailout_error: 12649 12650 ctl_send_datamove_done(io, /*have_lock*/ 0); 12651 12652 return (retval); 12653} 12654 12655static int 12656ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12657 ctl_ha_dt_cb callback) 12658{ 12659 struct ctl_ha_dt_req *rq; 12660 struct ctl_sg_entry *remote_sglist, *local_sglist; 12661 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 12662 uint32_t local_used, remote_used, total_used; 12663 int retval; 12664 int i, j; 12665 12666 retval = 0; 12667 12668 rq = ctl_dt_req_alloc(); 12669 12670 /* 12671 * If we failed to allocate the request, and if the DMA didn't fail 12672 * anyway, set busy status. This is just a resource allocation 12673 * failure. 12674 */ 12675 if ((rq == NULL) 12676 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 12677 ctl_set_busy(&io->scsiio); 12678 12679 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 12680 12681 if (rq != NULL) 12682 ctl_dt_req_free(rq); 12683 12684 /* 12685 * The data move failed. We need to return status back 12686 * to the other controller. No point in trying to DMA 12687 * data to the remote controller. 12688 */ 12689 12690 ctl_send_datamove_done(io, /*have_lock*/ 0); 12691 12692 retval = 1; 12693 12694 goto bailout; 12695 } 12696 12697 local_sglist = io->io_hdr.local_sglist; 12698 local_dma_sglist = io->io_hdr.local_dma_sglist; 12699 remote_sglist = io->io_hdr.remote_sglist; 12700 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12701 local_used = 0; 12702 remote_used = 0; 12703 total_used = 0; 12704 12705 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 12706 rq->ret = CTL_HA_STATUS_SUCCESS; 12707 rq->context = io; 12708 callback(rq); 12709 goto bailout; 12710 } 12711 12712 /* 12713 * Pull/push the data over the wire from/to the other controller. 12714 * This takes into account the possibility that the local and 12715 * remote sglists may not be identical in terms of the size of 12716 * the elements and the number of elements. 12717 * 12718 * One fundamental assumption here is that the length allocated for 12719 * both the local and remote sglists is identical. Otherwise, we've 12720 * essentially got a coding error of some sort. 12721 */ 12722 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12723 int isc_ret; 12724 uint32_t cur_len, dma_length; 12725 uint8_t *tmp_ptr; 12726 12727 rq->id = CTL_HA_DATA_CTL; 12728 rq->command = command; 12729 rq->context = io; 12730 12731 /* 12732 * Both pointers should be aligned. But it is possible 12733 * that the allocation length is not. They should both 12734 * also have enough slack left over at the end, though, 12735 * to round up to the next 8 byte boundary. 12736 */ 12737 cur_len = ctl_min(local_sglist[i].len - local_used, 12738 remote_sglist[j].len - remote_used); 12739 12740 /* 12741 * In this case, we have a size issue and need to decrease 12742 * the size, except in the case where we actually have less 12743 * than 8 bytes left. In that case, we need to increase 12744 * the DMA length to get the last bit. 12745 */ 12746 if ((cur_len & 0x7) != 0) { 12747 if (cur_len > 0x7) { 12748 cur_len = cur_len - (cur_len & 0x7); 12749 dma_length = cur_len; 12750 } else { 12751 CTL_SIZE_8B(dma_length, cur_len); 12752 } 12753 12754 } else 12755 dma_length = cur_len; 12756 12757 /* 12758 * If we had to allocate memory for this I/O, instead of using 12759 * the non-cached mirror memory, we'll need to flush the cache 12760 * before trying to DMA to the other controller. 12761 * 12762 * We could end up doing this multiple times for the same 12763 * segment if we have a larger local segment than remote 12764 * segment. That shouldn't be an issue. 12765 */ 12766 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12767 /* 12768 * XXX KDM use bus_dmamap_sync() here. 12769 */ 12770 } 12771 12772 rq->size = dma_length; 12773 12774 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12775 tmp_ptr += local_used; 12776 12777 /* Use physical addresses when talking to ISC hardware */ 12778 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12779 /* XXX KDM use busdma */ 12780#if 0 12781 rq->local = vtophys(tmp_ptr); 12782#endif 12783 } else 12784 rq->local = tmp_ptr; 12785 12786 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12787 tmp_ptr += remote_used; 12788 rq->remote = tmp_ptr; 12789 12790 rq->callback = NULL; 12791 12792 local_used += cur_len; 12793 if (local_used >= local_sglist[i].len) { 12794 i++; 12795 local_used = 0; 12796 } 12797 12798 remote_used += cur_len; 12799 if (remote_used >= remote_sglist[j].len) { 12800 j++; 12801 remote_used = 0; 12802 } 12803 total_used += cur_len; 12804 12805 if (total_used >= io->scsiio.kern_data_len) 12806 rq->callback = callback; 12807 12808 if ((rq->size & 0x7) != 0) { 12809 printf("%s: warning: size %d is not on 8b boundary\n", 12810 __func__, rq->size); 12811 } 12812 if (((uintptr_t)rq->local & 0x7) != 0) { 12813 printf("%s: warning: local %p not on 8b boundary\n", 12814 __func__, rq->local); 12815 } 12816 if (((uintptr_t)rq->remote & 0x7) != 0) { 12817 printf("%s: warning: remote %p not on 8b boundary\n", 12818 __func__, rq->local); 12819 } 12820#if 0 12821 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12822 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12823 rq->local, rq->remote, rq->size); 12824#endif 12825 12826 isc_ret = ctl_dt_single(rq); 12827 if (isc_ret == CTL_HA_STATUS_WAIT) 12828 continue; 12829 12830 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 12831 rq->ret = CTL_HA_STATUS_SUCCESS; 12832 } else { 12833 rq->ret = isc_ret; 12834 } 12835 callback(rq); 12836 goto bailout; 12837 } 12838 12839bailout: 12840 return (retval); 12841 12842} 12843 12844static void 12845ctl_datamove_remote_read(union ctl_io *io) 12846{ 12847 int retval; 12848 int i; 12849 12850 /* 12851 * This will send an error to the other controller in the case of a 12852 * failure. 12853 */ 12854 retval = ctl_datamove_remote_sgl_setup(io); 12855 if (retval != 0) 12856 return; 12857 12858 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12859 ctl_datamove_remote_read_cb); 12860 if ((retval != 0) 12861 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 12862 /* 12863 * Make sure we free memory if there was an error.. The 12864 * ctl_datamove_remote_xfer() function will send the 12865 * datamove done message, or call the callback with an 12866 * error if there is a problem. 12867 */ 12868 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12869 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12870 } 12871 12872 return; 12873} 12874 12875/* 12876 * Process a datamove request from the other controller. This is used for 12877 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12878 * first. Once that is complete, the data gets DMAed into the remote 12879 * controller's memory. For reads, we DMA from the remote controller's 12880 * memory into our memory first, and then move it out to the FETD. 12881 */ 12882static void 12883ctl_datamove_remote(union ctl_io *io) 12884{ 12885 struct ctl_softc *softc; 12886 12887 softc = control_softc; 12888 12889 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 12890 12891 /* 12892 * Note that we look for an aborted I/O here, but don't do some of 12893 * the other checks that ctl_datamove() normally does. We don't 12894 * need to run the task queue, because this I/O is on the ISC 12895 * queue, which is executed by the work thread after the task queue. 12896 * We don't need to run the datamove delay code, since that should 12897 * have been done if need be on the other controller. 12898 */ 12899 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12900 12901 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 12902 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 12903 io->io_hdr.nexus.targ_port, 12904 io->io_hdr.nexus.targ_target.id, 12905 io->io_hdr.nexus.targ_lun); 12906 io->io_hdr.status = CTL_CMD_ABORTED; 12907 io->io_hdr.port_status = 31338; 12908 12909 ctl_send_datamove_done(io, /*have_lock*/ 0); 12910 12911 return; 12912 } 12913 12914 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 12915 ctl_datamove_remote_write(io); 12916 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 12917 ctl_datamove_remote_read(io); 12918 } else { 12919 union ctl_ha_msg msg; 12920 struct scsi_sense_data *sense; 12921 uint8_t sks[3]; 12922 int retry_count; 12923 12924 memset(&msg, 0, sizeof(msg)); 12925 12926 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 12927 msg.hdr.status = CTL_SCSI_ERROR; 12928 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 12929 12930 retry_count = 4243; 12931 12932 sense = &msg.scsi.sense_data; 12933 sks[0] = SSD_SCS_VALID; 12934 sks[1] = (retry_count >> 8) & 0xff; 12935 sks[2] = retry_count & 0xff; 12936 12937 /* "Internal target failure" */ 12938 scsi_set_sense_data(sense, 12939 /*sense_format*/ SSD_TYPE_NONE, 12940 /*current_error*/ 1, 12941 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 12942 /*asc*/ 0x44, 12943 /*ascq*/ 0x00, 12944 /*type*/ SSD_ELEM_SKS, 12945 /*size*/ sizeof(sks), 12946 /*data*/ sks, 12947 SSD_ELEM_NONE); 12948 12949 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12950 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12951 ctl_failover_io(io, /*have_lock*/ 1); 12952 return; 12953 } 12954 12955 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 12956 CTL_HA_STATUS_SUCCESS) { 12957 /* XXX KDM what to do if this fails? */ 12958 } 12959 return; 12960 } 12961 12962} 12963 12964static int 12965ctl_process_done(union ctl_io *io) 12966{ 12967 struct ctl_lun *lun; 12968 struct ctl_softc *ctl_softc; 12969 void (*fe_done)(union ctl_io *io); 12970 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 12971 12972 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12973 12974 fe_done = 12975 control_softc->ctl_ports[targ_port]->fe_done; 12976 12977#ifdef CTL_TIME_IO 12978 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12979 char str[256]; 12980 char path_str[64]; 12981 struct sbuf sb; 12982 12983 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12984 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12985 12986 sbuf_cat(&sb, path_str); 12987 switch (io->io_hdr.io_type) { 12988 case CTL_IO_SCSI: 12989 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12990 sbuf_printf(&sb, "\n"); 12991 sbuf_cat(&sb, path_str); 12992 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12993 io->scsiio.tag_num, io->scsiio.tag_type); 12994 break; 12995 case CTL_IO_TASK: 12996 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12997 "Tag Type: %d\n", io->taskio.task_action, 12998 io->taskio.tag_num, io->taskio.tag_type); 12999 break; 13000 default: 13001 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13002 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13003 break; 13004 } 13005 sbuf_cat(&sb, path_str); 13006 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13007 (intmax_t)time_uptime - io->io_hdr.start_time); 13008 sbuf_finish(&sb); 13009 printf("%s", sbuf_data(&sb)); 13010 } 13011#endif /* CTL_TIME_IO */ 13012 13013 switch (io->io_hdr.io_type) { 13014 case CTL_IO_SCSI: 13015 break; 13016 case CTL_IO_TASK: 13017 if (bootverbose || verbose > 0) 13018 ctl_io_error_print(io, NULL); 13019 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13020 ctl_free_io(io); 13021 else 13022 fe_done(io); 13023 return (CTL_RETVAL_COMPLETE); 13024 break; 13025 default: 13026 printf("ctl_process_done: invalid io type %d\n", 13027 io->io_hdr.io_type); 13028 panic("ctl_process_done: invalid io type %d\n", 13029 io->io_hdr.io_type); 13030 break; /* NOTREACHED */ 13031 } 13032 13033 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13034 if (lun == NULL) { 13035 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13036 io->io_hdr.nexus.targ_mapped_lun)); 13037 fe_done(io); 13038 goto bailout; 13039 } 13040 ctl_softc = lun->ctl_softc; 13041 13042 mtx_lock(&lun->lun_lock); 13043 13044 /* 13045 * Check to see if we have any errors to inject here. We only 13046 * inject errors for commands that don't already have errors set. 13047 */ 13048 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13049 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13050 ctl_inject_error(lun, io); 13051 13052 /* 13053 * XXX KDM how do we treat commands that aren't completed 13054 * successfully? 13055 * 13056 * XXX KDM should we also track I/O latency? 13057 */ 13058 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13059 io->io_hdr.io_type == CTL_IO_SCSI) { 13060#ifdef CTL_TIME_IO 13061 struct bintime cur_bt; 13062#endif 13063 int type; 13064 13065 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13066 CTL_FLAG_DATA_IN) 13067 type = CTL_STATS_READ; 13068 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13069 CTL_FLAG_DATA_OUT) 13070 type = CTL_STATS_WRITE; 13071 else 13072 type = CTL_STATS_NO_IO; 13073 13074 lun->stats.ports[targ_port].bytes[type] += 13075 io->scsiio.kern_total_len; 13076 lun->stats.ports[targ_port].operations[type]++; 13077#ifdef CTL_TIME_IO 13078 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13079 &io->io_hdr.dma_bt); 13080 lun->stats.ports[targ_port].num_dmas[type] += 13081 io->io_hdr.num_dmas; 13082 getbintime(&cur_bt); 13083 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13084 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13085#endif 13086 } 13087 13088 /* 13089 * Remove this from the OOA queue. 13090 */ 13091 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13092 13093 /* 13094 * Run through the blocked queue on this LUN and see if anything 13095 * has become unblocked, now that this transaction is done. 13096 */ 13097 ctl_check_blocked(lun); 13098 13099 /* 13100 * If the LUN has been invalidated, free it if there is nothing 13101 * left on its OOA queue. 13102 */ 13103 if ((lun->flags & CTL_LUN_INVALID) 13104 && TAILQ_EMPTY(&lun->ooa_queue)) { 13105 mtx_unlock(&lun->lun_lock); 13106 mtx_lock(&ctl_softc->ctl_lock); 13107 ctl_free_lun(lun); 13108 mtx_unlock(&ctl_softc->ctl_lock); 13109 } else 13110 mtx_unlock(&lun->lun_lock); 13111 13112 /* 13113 * If this command has been aborted, make sure we set the status 13114 * properly. The FETD is responsible for freeing the I/O and doing 13115 * whatever it needs to do to clean up its state. 13116 */ 13117 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13118 io->io_hdr.status = CTL_CMD_ABORTED; 13119 13120 /* 13121 * We print out status for every task management command. For SCSI 13122 * commands, we filter out any unit attention errors; they happen 13123 * on every boot, and would clutter up the log. Note: task 13124 * management commands aren't printed here, they are printed above, 13125 * since they should never even make it down here. 13126 */ 13127 switch (io->io_hdr.io_type) { 13128 case CTL_IO_SCSI: { 13129 int error_code, sense_key, asc, ascq; 13130 13131 sense_key = 0; 13132 13133 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13134 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13135 /* 13136 * Since this is just for printing, no need to 13137 * show errors here. 13138 */ 13139 scsi_extract_sense_len(&io->scsiio.sense_data, 13140 io->scsiio.sense_len, 13141 &error_code, 13142 &sense_key, 13143 &asc, 13144 &ascq, 13145 /*show_errors*/ 0); 13146 } 13147 13148 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13149 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13150 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13151 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13152 13153 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13154 ctl_softc->skipped_prints++; 13155 } else { 13156 uint32_t skipped_prints; 13157 13158 skipped_prints = ctl_softc->skipped_prints; 13159 13160 ctl_softc->skipped_prints = 0; 13161 ctl_softc->last_print_jiffies = time_uptime; 13162 13163 if (skipped_prints > 0) { 13164#ifdef NEEDTOPORT 13165 csevent_log(CSC_CTL | CSC_SHELF_SW | 13166 CTL_ERROR_REPORT, 13167 csevent_LogType_Trace, 13168 csevent_Severity_Information, 13169 csevent_AlertLevel_Green, 13170 csevent_FRU_Firmware, 13171 csevent_FRU_Unknown, 13172 "High CTL error volume, %d prints " 13173 "skipped", skipped_prints); 13174#endif 13175 } 13176 if (bootverbose || verbose > 0) 13177 ctl_io_error_print(io, NULL); 13178 } 13179 } 13180 break; 13181 } 13182 case CTL_IO_TASK: 13183 if (bootverbose || verbose > 0) 13184 ctl_io_error_print(io, NULL); 13185 break; 13186 default: 13187 break; 13188 } 13189 13190 /* 13191 * Tell the FETD or the other shelf controller we're done with this 13192 * command. Note that only SCSI commands get to this point. Task 13193 * management commands are completed above. 13194 * 13195 * We only send status to the other controller if we're in XFER 13196 * mode. In SER_ONLY mode, the I/O is done on the controller that 13197 * received the I/O (from CTL's perspective), and so the status is 13198 * generated there. 13199 * 13200 * XXX KDM if we hold the lock here, we could cause a deadlock 13201 * if the frontend comes back in in this context to queue 13202 * something. 13203 */ 13204 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13205 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13206 union ctl_ha_msg msg; 13207 13208 memset(&msg, 0, sizeof(msg)); 13209 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13210 msg.hdr.original_sc = io->io_hdr.original_sc; 13211 msg.hdr.nexus = io->io_hdr.nexus; 13212 msg.hdr.status = io->io_hdr.status; 13213 msg.scsi.scsi_status = io->scsiio.scsi_status; 13214 msg.scsi.tag_num = io->scsiio.tag_num; 13215 msg.scsi.tag_type = io->scsiio.tag_type; 13216 msg.scsi.sense_len = io->scsiio.sense_len; 13217 msg.scsi.sense_residual = io->scsiio.sense_residual; 13218 msg.scsi.residual = io->scsiio.residual; 13219 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13220 sizeof(io->scsiio.sense_data)); 13221 /* 13222 * We copy this whether or not this is an I/O-related 13223 * command. Otherwise, we'd have to go and check to see 13224 * whether it's a read/write command, and it really isn't 13225 * worth it. 13226 */ 13227 memcpy(&msg.scsi.lbalen, 13228 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13229 sizeof(msg.scsi.lbalen)); 13230 13231 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13232 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13233 /* XXX do something here */ 13234 } 13235 13236 ctl_free_io(io); 13237 } else 13238 fe_done(io); 13239 13240bailout: 13241 13242 return (CTL_RETVAL_COMPLETE); 13243} 13244 13245/* 13246 * Front end should call this if it doesn't do autosense. When the request 13247 * sense comes back in from the initiator, we'll dequeue this and send it. 13248 */ 13249int 13250ctl_queue_sense(union ctl_io *io) 13251{ 13252 struct ctl_lun *lun; 13253 struct ctl_softc *ctl_softc; 13254 uint32_t initidx, targ_lun; 13255 13256 ctl_softc = control_softc; 13257 13258 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13259 13260 /* 13261 * LUN lookup will likely move to the ctl_work_thread() once we 13262 * have our new queueing infrastructure (that doesn't put things on 13263 * a per-LUN queue initially). That is so that we can handle 13264 * things like an INQUIRY to a LUN that we don't have enabled. We 13265 * can't deal with that right now. 13266 */ 13267 mtx_lock(&ctl_softc->ctl_lock); 13268 13269 /* 13270 * If we don't have a LUN for this, just toss the sense 13271 * information. 13272 */ 13273 targ_lun = io->io_hdr.nexus.targ_lun; 13274 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13275 if ((targ_lun < CTL_MAX_LUNS) 13276 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13277 lun = ctl_softc->ctl_luns[targ_lun]; 13278 else 13279 goto bailout; 13280 13281 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13282 13283 mtx_lock(&lun->lun_lock); 13284 /* 13285 * Already have CA set for this LUN...toss the sense information. 13286 */ 13287 if (ctl_is_set(lun->have_ca, initidx)) { 13288 mtx_unlock(&lun->lun_lock); 13289 goto bailout; 13290 } 13291 13292 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data, 13293 ctl_min(sizeof(lun->pending_sense[initidx].sense), 13294 sizeof(io->scsiio.sense_data))); 13295 ctl_set_mask(lun->have_ca, initidx); 13296 mtx_unlock(&lun->lun_lock); 13297 13298bailout: 13299 mtx_unlock(&ctl_softc->ctl_lock); 13300 13301 ctl_free_io(io); 13302 13303 return (CTL_RETVAL_COMPLETE); 13304} 13305 13306/* 13307 * Primary command inlet from frontend ports. All SCSI and task I/O 13308 * requests must go through this function. 13309 */ 13310int 13311ctl_queue(union ctl_io *io) 13312{ 13313 struct ctl_softc *ctl_softc; 13314 13315 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13316 13317 ctl_softc = control_softc; 13318 13319#ifdef CTL_TIME_IO 13320 io->io_hdr.start_time = time_uptime; 13321 getbintime(&io->io_hdr.start_bt); 13322#endif /* CTL_TIME_IO */ 13323 13324 /* Map FE-specific LUN ID into global one. */ 13325 io->io_hdr.nexus.targ_mapped_lun = 13326 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13327 13328 switch (io->io_hdr.io_type) { 13329 case CTL_IO_SCSI: 13330 case CTL_IO_TASK: 13331 ctl_enqueue_incoming(io); 13332 break; 13333 default: 13334 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13335 return (EINVAL); 13336 } 13337 13338 return (CTL_RETVAL_COMPLETE); 13339} 13340 13341#ifdef CTL_IO_DELAY 13342static void 13343ctl_done_timer_wakeup(void *arg) 13344{ 13345 union ctl_io *io; 13346 13347 io = (union ctl_io *)arg; 13348 ctl_done(io); 13349} 13350#endif /* CTL_IO_DELAY */ 13351 13352void 13353ctl_done(union ctl_io *io) 13354{ 13355 struct ctl_softc *ctl_softc; 13356 13357 ctl_softc = control_softc; 13358 13359 /* 13360 * Enable this to catch duplicate completion issues. 13361 */ 13362#if 0 13363 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13364 printf("%s: type %d msg %d cdb %x iptl: " 13365 "%d:%d:%d:%d tag 0x%04x " 13366 "flag %#x status %x\n", 13367 __func__, 13368 io->io_hdr.io_type, 13369 io->io_hdr.msg_type, 13370 io->scsiio.cdb[0], 13371 io->io_hdr.nexus.initid.id, 13372 io->io_hdr.nexus.targ_port, 13373 io->io_hdr.nexus.targ_target.id, 13374 io->io_hdr.nexus.targ_lun, 13375 (io->io_hdr.io_type == 13376 CTL_IO_TASK) ? 13377 io->taskio.tag_num : 13378 io->scsiio.tag_num, 13379 io->io_hdr.flags, 13380 io->io_hdr.status); 13381 } else 13382 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13383#endif 13384 13385 /* 13386 * This is an internal copy of an I/O, and should not go through 13387 * the normal done processing logic. 13388 */ 13389 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13390 return; 13391 13392 /* 13393 * We need to send a msg to the serializing shelf to finish the IO 13394 * as well. We don't send a finish message to the other shelf if 13395 * this is a task management command. Task management commands 13396 * aren't serialized in the OOA queue, but rather just executed on 13397 * both shelf controllers for commands that originated on that 13398 * controller. 13399 */ 13400 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13401 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13402 union ctl_ha_msg msg_io; 13403 13404 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13405 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13406 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13407 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13408 } 13409 /* continue on to finish IO */ 13410 } 13411#ifdef CTL_IO_DELAY 13412 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13413 struct ctl_lun *lun; 13414 13415 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13416 13417 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13418 } else { 13419 struct ctl_lun *lun; 13420 13421 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13422 13423 if ((lun != NULL) 13424 && (lun->delay_info.done_delay > 0)) { 13425 struct callout *callout; 13426 13427 callout = (struct callout *)&io->io_hdr.timer_bytes; 13428 callout_init(callout, /*mpsafe*/ 1); 13429 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13430 callout_reset(callout, 13431 lun->delay_info.done_delay * hz, 13432 ctl_done_timer_wakeup, io); 13433 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13434 lun->delay_info.done_delay = 0; 13435 return; 13436 } 13437 } 13438#endif /* CTL_IO_DELAY */ 13439 13440 ctl_enqueue_done(io); 13441} 13442 13443int 13444ctl_isc(struct ctl_scsiio *ctsio) 13445{ 13446 struct ctl_lun *lun; 13447 int retval; 13448 13449 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13450 13451 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13452 13453 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13454 13455 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13456 13457 return (retval); 13458} 13459 13460 13461static void 13462ctl_work_thread(void *arg) 13463{ 13464 struct ctl_thread *thr = (struct ctl_thread *)arg; 13465 struct ctl_softc *softc = thr->ctl_softc; 13466 union ctl_io *io; 13467 int retval; 13468 13469 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13470 13471 for (;;) { 13472 retval = 0; 13473 13474 /* 13475 * We handle the queues in this order: 13476 * - ISC 13477 * - done queue (to free up resources, unblock other commands) 13478 * - RtR queue 13479 * - incoming queue 13480 * 13481 * If those queues are empty, we break out of the loop and 13482 * go to sleep. 13483 */ 13484 mtx_lock(&thr->queue_lock); 13485 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13486 if (io != NULL) { 13487 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13488 mtx_unlock(&thr->queue_lock); 13489 ctl_handle_isc(io); 13490 continue; 13491 } 13492 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13493 if (io != NULL) { 13494 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13495 /* clear any blocked commands, call fe_done */ 13496 mtx_unlock(&thr->queue_lock); 13497 retval = ctl_process_done(io); 13498 continue; 13499 } 13500 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13501 if (io != NULL) { 13502 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13503 mtx_unlock(&thr->queue_lock); 13504 if (io->io_hdr.io_type == CTL_IO_TASK) 13505 ctl_run_task(io); 13506 else 13507 ctl_scsiio_precheck(softc, &io->scsiio); 13508 continue; 13509 } 13510 if (!ctl_pause_rtr) { 13511 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13512 if (io != NULL) { 13513 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13514 mtx_unlock(&thr->queue_lock); 13515 retval = ctl_scsiio(&io->scsiio); 13516 if (retval != CTL_RETVAL_COMPLETE) 13517 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13518 continue; 13519 } 13520 } 13521 13522 /* Sleep until we have something to do. */ 13523 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13524 } 13525} 13526 13527static void 13528ctl_lun_thread(void *arg) 13529{ 13530 struct ctl_softc *softc = (struct ctl_softc *)arg; 13531 struct ctl_be_lun *be_lun; 13532 int retval; 13533 13534 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13535 13536 for (;;) { 13537 retval = 0; 13538 mtx_lock(&softc->ctl_lock); 13539 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13540 if (be_lun != NULL) { 13541 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13542 mtx_unlock(&softc->ctl_lock); 13543 ctl_create_lun(be_lun); 13544 continue; 13545 } 13546 13547 /* Sleep until we have something to do. */ 13548 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13549 PDROP | PRIBIO, "-", 0); 13550 } 13551} 13552 13553static void 13554ctl_enqueue_incoming(union ctl_io *io) 13555{ 13556 struct ctl_softc *softc = control_softc; 13557 struct ctl_thread *thr; 13558 u_int idx; 13559 13560 idx = (io->io_hdr.nexus.targ_port * 127 + 13561 io->io_hdr.nexus.initid.id) % worker_threads; 13562 thr = &softc->threads[idx]; 13563 mtx_lock(&thr->queue_lock); 13564 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13565 mtx_unlock(&thr->queue_lock); 13566 wakeup(thr); 13567} 13568 13569static void 13570ctl_enqueue_rtr(union ctl_io *io) 13571{ 13572 struct ctl_softc *softc = control_softc; 13573 struct ctl_thread *thr; 13574 13575 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13576 mtx_lock(&thr->queue_lock); 13577 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13578 mtx_unlock(&thr->queue_lock); 13579 wakeup(thr); 13580} 13581 13582static void 13583ctl_enqueue_done(union ctl_io *io) 13584{ 13585 struct ctl_softc *softc = control_softc; 13586 struct ctl_thread *thr; 13587 13588 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13589 mtx_lock(&thr->queue_lock); 13590 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13591 mtx_unlock(&thr->queue_lock); 13592 wakeup(thr); 13593} 13594 13595static void 13596ctl_enqueue_isc(union ctl_io *io) 13597{ 13598 struct ctl_softc *softc = control_softc; 13599 struct ctl_thread *thr; 13600 13601 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13602 mtx_lock(&thr->queue_lock); 13603 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13604 mtx_unlock(&thr->queue_lock); 13605 wakeup(thr); 13606} 13607 13608/* Initialization and failover */ 13609 13610void 13611ctl_init_isc_msg(void) 13612{ 13613 printf("CTL: Still calling this thing\n"); 13614} 13615 13616/* 13617 * Init component 13618 * Initializes component into configuration defined by bootMode 13619 * (see hasc-sv.c) 13620 * returns hasc_Status: 13621 * OK 13622 * ERROR - fatal error 13623 */ 13624static ctl_ha_comp_status 13625ctl_isc_init(struct ctl_ha_component *c) 13626{ 13627 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13628 13629 c->status = ret; 13630 return ret; 13631} 13632 13633/* Start component 13634 * Starts component in state requested. If component starts successfully, 13635 * it must set its own state to the requestrd state 13636 * When requested state is HASC_STATE_HA, the component may refine it 13637 * by adding _SLAVE or _MASTER flags. 13638 * Currently allowed state transitions are: 13639 * UNKNOWN->HA - initial startup 13640 * UNKNOWN->SINGLE - initial startup when no parter detected 13641 * HA->SINGLE - failover 13642 * returns ctl_ha_comp_status: 13643 * OK - component successfully started in requested state 13644 * FAILED - could not start the requested state, failover may 13645 * be possible 13646 * ERROR - fatal error detected, no future startup possible 13647 */ 13648static ctl_ha_comp_status 13649ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 13650{ 13651 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13652 13653 printf("%s: go\n", __func__); 13654 13655 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 13656 if (c->state == CTL_HA_STATE_UNKNOWN ) { 13657 ctl_is_single = 0; 13658 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 13659 != CTL_HA_STATUS_SUCCESS) { 13660 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 13661 ret = CTL_HA_COMP_STATUS_ERROR; 13662 } 13663 } else if (CTL_HA_STATE_IS_HA(c->state) 13664 && CTL_HA_STATE_IS_SINGLE(state)){ 13665 // HA->SINGLE transition 13666 ctl_failover(); 13667 ctl_is_single = 1; 13668 } else { 13669 printf("ctl_isc_start:Invalid state transition %X->%X\n", 13670 c->state, state); 13671 ret = CTL_HA_COMP_STATUS_ERROR; 13672 } 13673 if (CTL_HA_STATE_IS_SINGLE(state)) 13674 ctl_is_single = 1; 13675 13676 c->state = state; 13677 c->status = ret; 13678 return ret; 13679} 13680 13681/* 13682 * Quiesce component 13683 * The component must clear any error conditions (set status to OK) and 13684 * prepare itself to another Start call 13685 * returns ctl_ha_comp_status: 13686 * OK 13687 * ERROR 13688 */ 13689static ctl_ha_comp_status 13690ctl_isc_quiesce(struct ctl_ha_component *c) 13691{ 13692 int ret = CTL_HA_COMP_STATUS_OK; 13693 13694 ctl_pause_rtr = 1; 13695 c->status = ret; 13696 return ret; 13697} 13698 13699struct ctl_ha_component ctl_ha_component_ctlisc = 13700{ 13701 .name = "CTL ISC", 13702 .state = CTL_HA_STATE_UNKNOWN, 13703 .init = ctl_isc_init, 13704 .start = ctl_isc_start, 13705 .quiesce = ctl_isc_quiesce 13706}; 13707 13708/* 13709 * vim: ts=8 13710 */ 13711