ctl.c revision 268447
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 268447 2014-07-09 04:37:50Z mav $"); 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/types.h> 51#include <sys/kthread.h> 52#include <sys/bio.h> 53#include <sys/fcntl.h> 54#include <sys/lock.h> 55#include <sys/module.h> 56#include <sys/mutex.h> 57#include <sys/condvar.h> 58#include <sys/malloc.h> 59#include <sys/conf.h> 60#include <sys/ioccom.h> 61#include <sys/queue.h> 62#include <sys/sbuf.h> 63#include <sys/smp.h> 64#include <sys/endian.h> 65#include <sys/sysctl.h> 66 67#include <cam/cam.h> 68#include <cam/scsi/scsi_all.h> 69#include <cam/scsi/scsi_da.h> 70#include <cam/ctl/ctl_io.h> 71#include <cam/ctl/ctl.h> 72#include <cam/ctl/ctl_frontend.h> 73#include <cam/ctl/ctl_frontend_internal.h> 74#include <cam/ctl/ctl_util.h> 75#include <cam/ctl/ctl_backend.h> 76#include <cam/ctl/ctl_ioctl.h> 77#include <cam/ctl/ctl_ha.h> 78#include <cam/ctl/ctl_private.h> 79#include <cam/ctl/ctl_debug.h> 80#include <cam/ctl/ctl_scsi_all.h> 81#include <cam/ctl/ctl_error.h> 82 83struct ctl_softc *control_softc = NULL; 84 85/* 86 * Size and alignment macros needed for Copan-specific HA hardware. These 87 * can go away when the HA code is re-written, and uses busdma for any 88 * hardware. 89 */ 90#define CTL_ALIGN_8B(target, source, type) \ 91 if (((uint32_t)source & 0x7) != 0) \ 92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93 else \ 94 target = (type)source; 95 96#define CTL_SIZE_8B(target, size) \ 97 if ((size & 0x7) != 0) \ 98 target = size + (0x8 - (size & 0x7)); \ 99 else \ 100 target = size; 101 102#define CTL_ALIGN_8B_MARGIN 16 103 104/* 105 * Template mode pages. 106 */ 107 108/* 109 * Note that these are default values only. The actual values will be 110 * filled in when the user does a mode sense. 111 */ 112static struct copan_power_subpage power_page_default = { 113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 114 /*subpage*/ PWR_SUBPAGE_CODE, 115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 117 /*page_version*/ PWR_VERSION, 118 /* total_luns */ 26, 119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122 0, 0, 0, 0, 0, 0} 123}; 124 125static struct copan_power_subpage power_page_changeable = { 126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 127 /*subpage*/ PWR_SUBPAGE_CODE, 128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 130 /*page_version*/ 0, 131 /* total_luns */ 0, 132 /* max_active_luns*/ 0, 133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135 0, 0, 0, 0, 0, 0} 136}; 137 138static struct copan_aps_subpage aps_page_default = { 139 APS_PAGE_CODE | SMPH_SPF, //page_code 140 APS_SUBPAGE_CODE, //subpage 141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 143 APS_VERSION, //page_version 144 0, //lock_active 145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147 0, 0, 0, 0, 0} //reserved 148}; 149 150static struct copan_aps_subpage aps_page_changeable = { 151 APS_PAGE_CODE | SMPH_SPF, //page_code 152 APS_SUBPAGE_CODE, //subpage 153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 155 0, //page_version 156 0, //lock_active 157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159 0, 0, 0, 0, 0} //reserved 160}; 161 162static struct copan_debugconf_subpage debugconf_page_default = { 163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 164 DBGCNF_SUBPAGE_CODE, /* subpage */ 165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 167 DBGCNF_VERSION, /* page_version */ 168 {CTL_TIME_IO_DEFAULT_SECS>>8, 169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 170}; 171 172static struct copan_debugconf_subpage debugconf_page_changeable = { 173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 174 DBGCNF_SUBPAGE_CODE, /* subpage */ 175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 177 0, /* page_version */ 178 {0xff,0xff}, /* ctl_time_io_secs */ 179}; 180 181static struct scsi_format_page format_page_default = { 182 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 183 /*page_length*/sizeof(struct scsi_format_page) - 2, 184 /*tracks_per_zone*/ {0, 0}, 185 /*alt_sectors_per_zone*/ {0, 0}, 186 /*alt_tracks_per_zone*/ {0, 0}, 187 /*alt_tracks_per_lun*/ {0, 0}, 188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 190 /*bytes_per_sector*/ {0, 0}, 191 /*interleave*/ {0, 0}, 192 /*track_skew*/ {0, 0}, 193 /*cylinder_skew*/ {0, 0}, 194 /*flags*/ SFP_HSEC, 195 /*reserved*/ {0, 0, 0} 196}; 197 198static struct scsi_format_page format_page_changeable = { 199 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 200 /*page_length*/sizeof(struct scsi_format_page) - 2, 201 /*tracks_per_zone*/ {0, 0}, 202 /*alt_sectors_per_zone*/ {0, 0}, 203 /*alt_tracks_per_zone*/ {0, 0}, 204 /*alt_tracks_per_lun*/ {0, 0}, 205 /*sectors_per_track*/ {0, 0}, 206 /*bytes_per_sector*/ {0, 0}, 207 /*interleave*/ {0, 0}, 208 /*track_skew*/ {0, 0}, 209 /*cylinder_skew*/ {0, 0}, 210 /*flags*/ 0, 211 /*reserved*/ {0, 0, 0} 212}; 213 214static struct scsi_rigid_disk_page rigid_disk_page_default = { 215 /*page_code*/SMS_RIGID_DISK_PAGE, 216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 217 /*cylinders*/ {0, 0, 0}, 218 /*heads*/ CTL_DEFAULT_HEADS, 219 /*start_write_precomp*/ {0, 0, 0}, 220 /*start_reduced_current*/ {0, 0, 0}, 221 /*step_rate*/ {0, 0}, 222 /*landing_zone_cylinder*/ {0, 0, 0}, 223 /*rpl*/ SRDP_RPL_DISABLED, 224 /*rotational_offset*/ 0, 225 /*reserved1*/ 0, 226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 227 CTL_DEFAULT_ROTATION_RATE & 0xff}, 228 /*reserved2*/ {0, 0} 229}; 230 231static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 232 /*page_code*/SMS_RIGID_DISK_PAGE, 233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 234 /*cylinders*/ {0, 0, 0}, 235 /*heads*/ 0, 236 /*start_write_precomp*/ {0, 0, 0}, 237 /*start_reduced_current*/ {0, 0, 0}, 238 /*step_rate*/ {0, 0}, 239 /*landing_zone_cylinder*/ {0, 0, 0}, 240 /*rpl*/ 0, 241 /*rotational_offset*/ 0, 242 /*reserved1*/ 0, 243 /*rotation_rate*/ {0, 0}, 244 /*reserved2*/ {0, 0} 245}; 246 247static struct scsi_caching_page caching_page_default = { 248 /*page_code*/SMS_CACHING_PAGE, 249 /*page_length*/sizeof(struct scsi_caching_page) - 2, 250 /*flags1*/ SCP_DISC | SCP_WCE, 251 /*ret_priority*/ 0, 252 /*disable_pf_transfer_len*/ {0xff, 0xff}, 253 /*min_prefetch*/ {0, 0}, 254 /*max_prefetch*/ {0xff, 0xff}, 255 /*max_pf_ceiling*/ {0xff, 0xff}, 256 /*flags2*/ 0, 257 /*cache_segments*/ 0, 258 /*cache_seg_size*/ {0, 0}, 259 /*reserved*/ 0, 260 /*non_cache_seg_size*/ {0, 0, 0} 261}; 262 263static struct scsi_caching_page caching_page_changeable = { 264 /*page_code*/SMS_CACHING_PAGE, 265 /*page_length*/sizeof(struct scsi_caching_page) - 2, 266 /*flags1*/ 0, 267 /*ret_priority*/ 0, 268 /*disable_pf_transfer_len*/ {0, 0}, 269 /*min_prefetch*/ {0, 0}, 270 /*max_prefetch*/ {0, 0}, 271 /*max_pf_ceiling*/ {0, 0}, 272 /*flags2*/ 0, 273 /*cache_segments*/ 0, 274 /*cache_seg_size*/ {0, 0}, 275 /*reserved*/ 0, 276 /*non_cache_seg_size*/ {0, 0, 0} 277}; 278 279static struct scsi_control_page control_page_default = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE, 281 /*page_length*/sizeof(struct scsi_control_page) - 2, 282 /*rlec*/0, 283 /*queue_flags*/0, 284 /*eca_and_aen*/0, 285 /*flags4*/SCP_TAS, 286 /*aen_holdoff_period*/{0, 0}, 287 /*busy_timeout_period*/{0, 0}, 288 /*extended_selftest_completion_time*/{0, 0} 289}; 290 291static struct scsi_control_page control_page_changeable = { 292 /*page_code*/SMS_CONTROL_MODE_PAGE, 293 /*page_length*/sizeof(struct scsi_control_page) - 2, 294 /*rlec*/SCP_DSENSE, 295 /*queue_flags*/0, 296 /*eca_and_aen*/0, 297 /*flags4*/0, 298 /*aen_holdoff_period*/{0, 0}, 299 /*busy_timeout_period*/{0, 0}, 300 /*extended_selftest_completion_time*/{0, 0} 301}; 302 303 304/* 305 * XXX KDM move these into the softc. 306 */ 307static int rcv_sync_msg; 308static int persis_offset; 309static uint8_t ctl_pause_rtr; 310static int ctl_is_single = 1; 311static int index_to_aps_page; 312 313SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 314static int worker_threads = -1; 315SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 316 &worker_threads, 1, "Number of worker threads"); 317static int verbose = 0; 318SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 319 &verbose, 0, "Show SCSI errors returned to initiator"); 320 321/* 322 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 323 * SCSI Ports (0x88), Block limits (0xB0) and 324 * Logical Block Provisioning (0xB2) 325 */ 326#define SCSI_EVPD_NUM_SUPPORTED_PAGES 6 327 328static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 329 int param); 330static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 331static int ctl_init(void); 332void ctl_shutdown(void); 333static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 334static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 335static void ctl_ioctl_online(void *arg); 336static void ctl_ioctl_offline(void *arg); 337static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 338static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 339static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 340static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 341static int ctl_ioctl_submit_wait(union ctl_io *io); 342static void ctl_ioctl_datamove(union ctl_io *io); 343static void ctl_ioctl_done(union ctl_io *io); 344static void ctl_ioctl_hard_startstop_callback(void *arg, 345 struct cfi_metatask *metatask); 346static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 347static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 348 struct ctl_ooa *ooa_hdr, 349 struct ctl_ooa_entry *kern_entries); 350static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 351 struct thread *td); 352uint32_t ctl_get_resindex(struct ctl_nexus *nexus); 353uint32_t ctl_port_idx(int port_num); 354static uint32_t ctl_map_lun(int port_num, uint32_t lun); 355static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 356#ifdef unused 357static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 358 uint32_t targ_target, uint32_t targ_lun, 359 int can_wait); 360static void ctl_kfree_io(union ctl_io *io); 361#endif /* unused */ 362static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 363 struct ctl_be_lun *be_lun, struct ctl_id target_id); 364static int ctl_free_lun(struct ctl_lun *lun); 365static void ctl_create_lun(struct ctl_be_lun *be_lun); 366/** 367static void ctl_failover_change_pages(struct ctl_softc *softc, 368 struct ctl_scsiio *ctsio, int master); 369**/ 370 371static int ctl_do_mode_select(union ctl_io *io); 372static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 373 uint64_t res_key, uint64_t sa_res_key, 374 uint8_t type, uint32_t residx, 375 struct ctl_scsiio *ctsio, 376 struct scsi_per_res_out *cdb, 377 struct scsi_per_res_out_parms* param); 378static void ctl_pro_preempt_other(struct ctl_lun *lun, 379 union ctl_ha_msg *msg); 380static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 381static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 382static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 383static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 384static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 385 int alloc_len); 386static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 387 int alloc_len); 388static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 389static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 390static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 391static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 392static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 393static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 394 union ctl_io *ooa_io); 395static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 396 union ctl_io *starting_io); 397static int ctl_check_blocked(struct ctl_lun *lun); 398static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 399 struct ctl_lun *lun, 400 const struct ctl_cmd_entry *entry, 401 struct ctl_scsiio *ctsio); 402//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 403static void ctl_failover(void); 404static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 405 struct ctl_scsiio *ctsio); 406static int ctl_scsiio(struct ctl_scsiio *ctsio); 407 408static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 409static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 410 ctl_ua_type ua_type); 411static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 412 ctl_ua_type ua_type); 413static int ctl_abort_task(union ctl_io *io); 414static int ctl_abort_task_set(union ctl_io *io); 415static int ctl_i_t_nexus_reset(union ctl_io *io); 416static void ctl_run_task(union ctl_io *io); 417#ifdef CTL_IO_DELAY 418static void ctl_datamove_timer_wakeup(void *arg); 419static void ctl_done_timer_wakeup(void *arg); 420#endif /* CTL_IO_DELAY */ 421 422static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 423static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 424static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 425static void ctl_datamove_remote_write(union ctl_io *io); 426static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 427static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 428static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 429static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 430 ctl_ha_dt_cb callback); 431static void ctl_datamove_remote_read(union ctl_io *io); 432static void ctl_datamove_remote(union ctl_io *io); 433static int ctl_process_done(union ctl_io *io); 434static void ctl_lun_thread(void *arg); 435static void ctl_work_thread(void *arg); 436static void ctl_enqueue_incoming(union ctl_io *io); 437static void ctl_enqueue_rtr(union ctl_io *io); 438static void ctl_enqueue_done(union ctl_io *io); 439static void ctl_enqueue_isc(union ctl_io *io); 440static const struct ctl_cmd_entry * 441 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 442static const struct ctl_cmd_entry * 443 ctl_validate_command(struct ctl_scsiio *ctsio); 444static int ctl_cmd_applicable(uint8_t lun_type, 445 const struct ctl_cmd_entry *entry); 446 447/* 448 * Load the serialization table. This isn't very pretty, but is probably 449 * the easiest way to do it. 450 */ 451#include "ctl_ser_table.c" 452 453/* 454 * We only need to define open, close and ioctl routines for this driver. 455 */ 456static struct cdevsw ctl_cdevsw = { 457 .d_version = D_VERSION, 458 .d_flags = 0, 459 .d_open = ctl_open, 460 .d_close = ctl_close, 461 .d_ioctl = ctl_ioctl, 462 .d_name = "ctl", 463}; 464 465 466MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 467MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 468 469static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 470 471static moduledata_t ctl_moduledata = { 472 "ctl", 473 ctl_module_event_handler, 474 NULL 475}; 476 477DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 478MODULE_VERSION(ctl, 1); 479 480static struct ctl_frontend ioctl_frontend = 481{ 482 .name = "ioctl", 483}; 484 485static void 486ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 487 union ctl_ha_msg *msg_info) 488{ 489 struct ctl_scsiio *ctsio; 490 491 if (msg_info->hdr.original_sc == NULL) { 492 printf("%s: original_sc == NULL!\n", __func__); 493 /* XXX KDM now what? */ 494 return; 495 } 496 497 ctsio = &msg_info->hdr.original_sc->scsiio; 498 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 499 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 500 ctsio->io_hdr.status = msg_info->hdr.status; 501 ctsio->scsi_status = msg_info->scsi.scsi_status; 502 ctsio->sense_len = msg_info->scsi.sense_len; 503 ctsio->sense_residual = msg_info->scsi.sense_residual; 504 ctsio->residual = msg_info->scsi.residual; 505 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 506 sizeof(ctsio->sense_data)); 507 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 508 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 509 ctl_enqueue_isc((union ctl_io *)ctsio); 510} 511 512static void 513ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 514 union ctl_ha_msg *msg_info) 515{ 516 struct ctl_scsiio *ctsio; 517 518 if (msg_info->hdr.serializing_sc == NULL) { 519 printf("%s: serializing_sc == NULL!\n", __func__); 520 /* XXX KDM now what? */ 521 return; 522 } 523 524 ctsio = &msg_info->hdr.serializing_sc->scsiio; 525#if 0 526 /* 527 * Attempt to catch the situation where an I/O has 528 * been freed, and we're using it again. 529 */ 530 if (ctsio->io_hdr.io_type == 0xff) { 531 union ctl_io *tmp_io; 532 tmp_io = (union ctl_io *)ctsio; 533 printf("%s: %p use after free!\n", __func__, 534 ctsio); 535 printf("%s: type %d msg %d cdb %x iptl: " 536 "%d:%d:%d:%d tag 0x%04x " 537 "flag %#x status %x\n", 538 __func__, 539 tmp_io->io_hdr.io_type, 540 tmp_io->io_hdr.msg_type, 541 tmp_io->scsiio.cdb[0], 542 tmp_io->io_hdr.nexus.initid.id, 543 tmp_io->io_hdr.nexus.targ_port, 544 tmp_io->io_hdr.nexus.targ_target.id, 545 tmp_io->io_hdr.nexus.targ_lun, 546 (tmp_io->io_hdr.io_type == 547 CTL_IO_TASK) ? 548 tmp_io->taskio.tag_num : 549 tmp_io->scsiio.tag_num, 550 tmp_io->io_hdr.flags, 551 tmp_io->io_hdr.status); 552 } 553#endif 554 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 555 ctl_enqueue_isc((union ctl_io *)ctsio); 556} 557 558/* 559 * ISC (Inter Shelf Communication) event handler. Events from the HA 560 * subsystem come in here. 561 */ 562static void 563ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 564{ 565 struct ctl_softc *ctl_softc; 566 union ctl_io *io; 567 struct ctl_prio *presio; 568 ctl_ha_status isc_status; 569 570 ctl_softc = control_softc; 571 io = NULL; 572 573 574#if 0 575 printf("CTL: Isc Msg event %d\n", event); 576#endif 577 if (event == CTL_HA_EVT_MSG_RECV) { 578 union ctl_ha_msg msg_info; 579 580 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 581 sizeof(msg_info), /*wait*/ 0); 582#if 0 583 printf("CTL: msg_type %d\n", msg_info.msg_type); 584#endif 585 if (isc_status != 0) { 586 printf("Error receiving message, status = %d\n", 587 isc_status); 588 return; 589 } 590 591 switch (msg_info.hdr.msg_type) { 592 case CTL_MSG_SERIALIZE: 593#if 0 594 printf("Serialize\n"); 595#endif 596 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 597 if (io == NULL) { 598 printf("ctl_isc_event_handler: can't allocate " 599 "ctl_io!\n"); 600 /* Bad Juju */ 601 /* Need to set busy and send msg back */ 602 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 603 msg_info.hdr.status = CTL_SCSI_ERROR; 604 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 605 msg_info.scsi.sense_len = 0; 606 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 607 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 608 } 609 goto bailout; 610 } 611 ctl_zero_io(io); 612 // populate ctsio from msg_info 613 io->io_hdr.io_type = CTL_IO_SCSI; 614 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 615 io->io_hdr.original_sc = msg_info.hdr.original_sc; 616#if 0 617 printf("pOrig %x\n", (int)msg_info.original_sc); 618#endif 619 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 620 CTL_FLAG_IO_ACTIVE; 621 /* 622 * If we're in serialization-only mode, we don't 623 * want to go through full done processing. Thus 624 * the COPY flag. 625 * 626 * XXX KDM add another flag that is more specific. 627 */ 628 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 629 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 630 io->io_hdr.nexus = msg_info.hdr.nexus; 631#if 0 632 printf("targ %d, port %d, iid %d, lun %d\n", 633 io->io_hdr.nexus.targ_target.id, 634 io->io_hdr.nexus.targ_port, 635 io->io_hdr.nexus.initid.id, 636 io->io_hdr.nexus.targ_lun); 637#endif 638 io->scsiio.tag_num = msg_info.scsi.tag_num; 639 io->scsiio.tag_type = msg_info.scsi.tag_type; 640 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 641 CTL_MAX_CDBLEN); 642 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 643 const struct ctl_cmd_entry *entry; 644 645 entry = ctl_get_cmd_entry(&io->scsiio); 646 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 647 io->io_hdr.flags |= 648 entry->flags & CTL_FLAG_DATA_MASK; 649 } 650 ctl_enqueue_isc(io); 651 break; 652 653 /* Performed on the Originating SC, XFER mode only */ 654 case CTL_MSG_DATAMOVE: { 655 struct ctl_sg_entry *sgl; 656 int i, j; 657 658 io = msg_info.hdr.original_sc; 659 if (io == NULL) { 660 printf("%s: original_sc == NULL!\n", __func__); 661 /* XXX KDM do something here */ 662 break; 663 } 664 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 665 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 666 /* 667 * Keep track of this, we need to send it back over 668 * when the datamove is complete. 669 */ 670 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 671 672 if (msg_info.dt.sg_sequence == 0) { 673 /* 674 * XXX KDM we use the preallocated S/G list 675 * here, but we'll need to change this to 676 * dynamic allocation if we need larger S/G 677 * lists. 678 */ 679 if (msg_info.dt.kern_sg_entries > 680 sizeof(io->io_hdr.remote_sglist) / 681 sizeof(io->io_hdr.remote_sglist[0])) { 682 printf("%s: number of S/G entries " 683 "needed %u > allocated num %zd\n", 684 __func__, 685 msg_info.dt.kern_sg_entries, 686 sizeof(io->io_hdr.remote_sglist)/ 687 sizeof(io->io_hdr.remote_sglist[0])); 688 689 /* 690 * XXX KDM send a message back to 691 * the other side to shut down the 692 * DMA. The error will come back 693 * through via the normal channel. 694 */ 695 break; 696 } 697 sgl = io->io_hdr.remote_sglist; 698 memset(sgl, 0, 699 sizeof(io->io_hdr.remote_sglist)); 700 701 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 702 703 io->scsiio.kern_sg_entries = 704 msg_info.dt.kern_sg_entries; 705 io->scsiio.rem_sg_entries = 706 msg_info.dt.kern_sg_entries; 707 io->scsiio.kern_data_len = 708 msg_info.dt.kern_data_len; 709 io->scsiio.kern_total_len = 710 msg_info.dt.kern_total_len; 711 io->scsiio.kern_data_resid = 712 msg_info.dt.kern_data_resid; 713 io->scsiio.kern_rel_offset = 714 msg_info.dt.kern_rel_offset; 715 /* 716 * Clear out per-DMA flags. 717 */ 718 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 719 /* 720 * Add per-DMA flags that are set for this 721 * particular DMA request. 722 */ 723 io->io_hdr.flags |= msg_info.dt.flags & 724 CTL_FLAG_RDMA_MASK; 725 } else 726 sgl = (struct ctl_sg_entry *) 727 io->scsiio.kern_data_ptr; 728 729 for (i = msg_info.dt.sent_sg_entries, j = 0; 730 i < (msg_info.dt.sent_sg_entries + 731 msg_info.dt.cur_sg_entries); i++, j++) { 732 sgl[i].addr = msg_info.dt.sg_list[j].addr; 733 sgl[i].len = msg_info.dt.sg_list[j].len; 734 735#if 0 736 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 737 __func__, 738 msg_info.dt.sg_list[j].addr, 739 msg_info.dt.sg_list[j].len, 740 sgl[i].addr, sgl[i].len, j, i); 741#endif 742 } 743#if 0 744 memcpy(&sgl[msg_info.dt.sent_sg_entries], 745 msg_info.dt.sg_list, 746 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 747#endif 748 749 /* 750 * If this is the last piece of the I/O, we've got 751 * the full S/G list. Queue processing in the thread. 752 * Otherwise wait for the next piece. 753 */ 754 if (msg_info.dt.sg_last != 0) 755 ctl_enqueue_isc(io); 756 break; 757 } 758 /* Performed on the Serializing (primary) SC, XFER mode only */ 759 case CTL_MSG_DATAMOVE_DONE: { 760 if (msg_info.hdr.serializing_sc == NULL) { 761 printf("%s: serializing_sc == NULL!\n", 762 __func__); 763 /* XXX KDM now what? */ 764 break; 765 } 766 /* 767 * We grab the sense information here in case 768 * there was a failure, so we can return status 769 * back to the initiator. 770 */ 771 io = msg_info.hdr.serializing_sc; 772 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 773 io->io_hdr.status = msg_info.hdr.status; 774 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 775 io->scsiio.sense_len = msg_info.scsi.sense_len; 776 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 777 io->io_hdr.port_status = msg_info.scsi.fetd_status; 778 io->scsiio.residual = msg_info.scsi.residual; 779 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 780 sizeof(io->scsiio.sense_data)); 781 ctl_enqueue_isc(io); 782 break; 783 } 784 785 /* Preformed on Originating SC, SER_ONLY mode */ 786 case CTL_MSG_R2R: 787 io = msg_info.hdr.original_sc; 788 if (io == NULL) { 789 printf("%s: Major Bummer\n", __func__); 790 return; 791 } else { 792#if 0 793 printf("pOrig %x\n",(int) ctsio); 794#endif 795 } 796 io->io_hdr.msg_type = CTL_MSG_R2R; 797 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 798 ctl_enqueue_isc(io); 799 break; 800 801 /* 802 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 803 * mode. 804 * Performed on the Originating (i.e. secondary) SC in XFER 805 * mode 806 */ 807 case CTL_MSG_FINISH_IO: 808 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 809 ctl_isc_handler_finish_xfer(ctl_softc, 810 &msg_info); 811 else 812 ctl_isc_handler_finish_ser_only(ctl_softc, 813 &msg_info); 814 break; 815 816 /* Preformed on Originating SC */ 817 case CTL_MSG_BAD_JUJU: 818 io = msg_info.hdr.original_sc; 819 if (io == NULL) { 820 printf("%s: Bad JUJU!, original_sc is NULL!\n", 821 __func__); 822 break; 823 } 824 ctl_copy_sense_data(&msg_info, io); 825 /* 826 * IO should have already been cleaned up on other 827 * SC so clear this flag so we won't send a message 828 * back to finish the IO there. 829 */ 830 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 831 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 832 833 /* io = msg_info.hdr.serializing_sc; */ 834 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 835 ctl_enqueue_isc(io); 836 break; 837 838 /* Handle resets sent from the other side */ 839 case CTL_MSG_MANAGE_TASKS: { 840 struct ctl_taskio *taskio; 841 taskio = (struct ctl_taskio *)ctl_alloc_io( 842 (void *)ctl_softc->othersc_pool); 843 if (taskio == NULL) { 844 printf("ctl_isc_event_handler: can't allocate " 845 "ctl_io!\n"); 846 /* Bad Juju */ 847 /* should I just call the proper reset func 848 here??? */ 849 goto bailout; 850 } 851 ctl_zero_io((union ctl_io *)taskio); 852 taskio->io_hdr.io_type = CTL_IO_TASK; 853 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 854 taskio->io_hdr.nexus = msg_info.hdr.nexus; 855 taskio->task_action = msg_info.task.task_action; 856 taskio->tag_num = msg_info.task.tag_num; 857 taskio->tag_type = msg_info.task.tag_type; 858#ifdef CTL_TIME_IO 859 taskio->io_hdr.start_time = time_uptime; 860 getbintime(&taskio->io_hdr.start_bt); 861#if 0 862 cs_prof_gettime(&taskio->io_hdr.start_ticks); 863#endif 864#endif /* CTL_TIME_IO */ 865 ctl_run_task((union ctl_io *)taskio); 866 break; 867 } 868 /* Persistent Reserve action which needs attention */ 869 case CTL_MSG_PERS_ACTION: 870 presio = (struct ctl_prio *)ctl_alloc_io( 871 (void *)ctl_softc->othersc_pool); 872 if (presio == NULL) { 873 printf("ctl_isc_event_handler: can't allocate " 874 "ctl_io!\n"); 875 /* Bad Juju */ 876 /* Need to set busy and send msg back */ 877 goto bailout; 878 } 879 ctl_zero_io((union ctl_io *)presio); 880 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 881 presio->pr_msg = msg_info.pr; 882 ctl_enqueue_isc((union ctl_io *)presio); 883 break; 884 case CTL_MSG_SYNC_FE: 885 rcv_sync_msg = 1; 886 break; 887 case CTL_MSG_APS_LOCK: { 888 // It's quicker to execute this then to 889 // queue it. 890 struct ctl_lun *lun; 891 struct ctl_page_index *page_index; 892 struct copan_aps_subpage *current_sp; 893 uint32_t targ_lun; 894 895 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 896 lun = ctl_softc->ctl_luns[targ_lun]; 897 mtx_lock(&lun->lun_lock); 898 page_index = &lun->mode_pages.index[index_to_aps_page]; 899 current_sp = (struct copan_aps_subpage *) 900 (page_index->page_data + 901 (page_index->page_len * CTL_PAGE_CURRENT)); 902 903 current_sp->lock_active = msg_info.aps.lock_flag; 904 mtx_unlock(&lun->lun_lock); 905 break; 906 } 907 default: 908 printf("How did I get here?\n"); 909 } 910 } else if (event == CTL_HA_EVT_MSG_SENT) { 911 if (param != CTL_HA_STATUS_SUCCESS) { 912 printf("Bad status from ctl_ha_msg_send status %d\n", 913 param); 914 } 915 return; 916 } else if (event == CTL_HA_EVT_DISCONNECT) { 917 printf("CTL: Got a disconnect from Isc\n"); 918 return; 919 } else { 920 printf("ctl_isc_event_handler: Unknown event %d\n", event); 921 return; 922 } 923 924bailout: 925 return; 926} 927 928static void 929ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 930{ 931 struct scsi_sense_data *sense; 932 933 sense = &dest->scsiio.sense_data; 934 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 935 dest->scsiio.scsi_status = src->scsi.scsi_status; 936 dest->scsiio.sense_len = src->scsi.sense_len; 937 dest->io_hdr.status = src->hdr.status; 938} 939 940static int 941ctl_init(void) 942{ 943 struct ctl_softc *softc; 944 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 945 struct ctl_port *port; 946 uint8_t sc_id =0; 947 int i, error, retval; 948 //int isc_retval; 949 950 retval = 0; 951 ctl_pause_rtr = 0; 952 rcv_sync_msg = 0; 953 954 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 955 M_WAITOK | M_ZERO); 956 softc = control_softc; 957 958 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 959 "cam/ctl"); 960 961 softc->dev->si_drv1 = softc; 962 963 /* 964 * By default, return a "bad LUN" peripheral qualifier for unknown 965 * LUNs. The user can override this default using the tunable or 966 * sysctl. See the comment in ctl_inquiry_std() for more details. 967 */ 968 softc->inquiry_pq_no_lun = 1; 969 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 970 &softc->inquiry_pq_no_lun); 971 sysctl_ctx_init(&softc->sysctl_ctx); 972 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 973 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 974 CTLFLAG_RD, 0, "CAM Target Layer"); 975 976 if (softc->sysctl_tree == NULL) { 977 printf("%s: unable to allocate sysctl tree\n", __func__); 978 destroy_dev(softc->dev); 979 free(control_softc, M_DEVBUF); 980 control_softc = NULL; 981 return (ENOMEM); 982 } 983 984 SYSCTL_ADD_INT(&softc->sysctl_ctx, 985 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 986 "inquiry_pq_no_lun", CTLFLAG_RW, 987 &softc->inquiry_pq_no_lun, 0, 988 "Report no lun possible for invalid LUNs"); 989 990 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 991 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 992 softc->open_count = 0; 993 994 /* 995 * Default to actually sending a SYNCHRONIZE CACHE command down to 996 * the drive. 997 */ 998 softc->flags = CTL_FLAG_REAL_SYNC; 999 1000 /* 1001 * In Copan's HA scheme, the "master" and "slave" roles are 1002 * figured out through the slot the controller is in. Although it 1003 * is an active/active system, someone has to be in charge. 1004 */ 1005#ifdef NEEDTOPORT 1006 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1007#endif 1008 1009 if (sc_id == 0) { 1010 softc->flags |= CTL_FLAG_MASTER_SHELF; 1011 persis_offset = 0; 1012 } else 1013 persis_offset = CTL_MAX_INITIATORS; 1014 1015 /* 1016 * XXX KDM need to figure out where we want to get our target ID 1017 * and WWID. Is it different on each port? 1018 */ 1019 softc->target.id = 0; 1020 softc->target.wwid[0] = 0x12345678; 1021 softc->target.wwid[1] = 0x87654321; 1022 STAILQ_INIT(&softc->lun_list); 1023 STAILQ_INIT(&softc->pending_lun_queue); 1024 STAILQ_INIT(&softc->fe_list); 1025 STAILQ_INIT(&softc->port_list); 1026 STAILQ_INIT(&softc->be_list); 1027 STAILQ_INIT(&softc->io_pools); 1028 1029 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1030 &internal_pool)!= 0){ 1031 printf("ctl: can't allocate %d entry internal pool, " 1032 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1033 return (ENOMEM); 1034 } 1035 1036 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1037 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1038 printf("ctl: can't allocate %d entry emergency pool, " 1039 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1040 ctl_pool_free(internal_pool); 1041 return (ENOMEM); 1042 } 1043 1044 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1045 &other_pool) != 0) 1046 { 1047 printf("ctl: can't allocate %d entry other SC pool, " 1048 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1049 ctl_pool_free(internal_pool); 1050 ctl_pool_free(emergency_pool); 1051 return (ENOMEM); 1052 } 1053 1054 softc->internal_pool = internal_pool; 1055 softc->emergency_pool = emergency_pool; 1056 softc->othersc_pool = other_pool; 1057 1058 if (worker_threads <= 0) 1059 worker_threads = max(1, mp_ncpus / 4); 1060 if (worker_threads > CTL_MAX_THREADS) 1061 worker_threads = CTL_MAX_THREADS; 1062 1063 for (i = 0; i < worker_threads; i++) { 1064 struct ctl_thread *thr = &softc->threads[i]; 1065 1066 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1067 thr->ctl_softc = softc; 1068 STAILQ_INIT(&thr->incoming_queue); 1069 STAILQ_INIT(&thr->rtr_queue); 1070 STAILQ_INIT(&thr->done_queue); 1071 STAILQ_INIT(&thr->isc_queue); 1072 1073 error = kproc_kthread_add(ctl_work_thread, thr, 1074 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1075 if (error != 0) { 1076 printf("error creating CTL work thread!\n"); 1077 ctl_pool_free(internal_pool); 1078 ctl_pool_free(emergency_pool); 1079 ctl_pool_free(other_pool); 1080 return (error); 1081 } 1082 } 1083 error = kproc_kthread_add(ctl_lun_thread, softc, 1084 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1085 if (error != 0) { 1086 printf("error creating CTL lun thread!\n"); 1087 ctl_pool_free(internal_pool); 1088 ctl_pool_free(emergency_pool); 1089 ctl_pool_free(other_pool); 1090 return (error); 1091 } 1092 if (bootverbose) 1093 printf("ctl: CAM Target Layer loaded\n"); 1094 1095 /* 1096 * Initialize the ioctl front end. 1097 */ 1098 ctl_frontend_register(&ioctl_frontend); 1099 port = &softc->ioctl_info.port; 1100 port->frontend = &ioctl_frontend; 1101 sprintf(softc->ioctl_info.port_name, "ioctl"); 1102 port->port_type = CTL_PORT_IOCTL; 1103 port->num_requested_ctl_io = 100; 1104 port->port_name = softc->ioctl_info.port_name; 1105 port->port_online = ctl_ioctl_online; 1106 port->port_offline = ctl_ioctl_offline; 1107 port->onoff_arg = &softc->ioctl_info; 1108 port->lun_enable = ctl_ioctl_lun_enable; 1109 port->lun_disable = ctl_ioctl_lun_disable; 1110 port->targ_lun_arg = &softc->ioctl_info; 1111 port->fe_datamove = ctl_ioctl_datamove; 1112 port->fe_done = ctl_ioctl_done; 1113 port->max_targets = 15; 1114 port->max_target_id = 15; 1115 1116 if (ctl_port_register(&softc->ioctl_info.port, 1117 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1118 printf("ctl: ioctl front end registration failed, will " 1119 "continue anyway\n"); 1120 } 1121 1122#ifdef CTL_IO_DELAY 1123 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1124 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1125 sizeof(struct callout), CTL_TIMER_BYTES); 1126 return (EINVAL); 1127 } 1128#endif /* CTL_IO_DELAY */ 1129 1130 return (0); 1131} 1132 1133void 1134ctl_shutdown(void) 1135{ 1136 struct ctl_softc *softc; 1137 struct ctl_lun *lun, *next_lun; 1138 struct ctl_io_pool *pool; 1139 1140 softc = (struct ctl_softc *)control_softc; 1141 1142 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1143 printf("ctl: ioctl front end deregistration failed\n"); 1144 1145 mtx_lock(&softc->ctl_lock); 1146 1147 /* 1148 * Free up each LUN. 1149 */ 1150 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1151 next_lun = STAILQ_NEXT(lun, links); 1152 ctl_free_lun(lun); 1153 } 1154 1155 mtx_unlock(&softc->ctl_lock); 1156 1157 ctl_frontend_deregister(&ioctl_frontend); 1158 1159 /* 1160 * This will rip the rug out from under any FETDs or anyone else 1161 * that has a pool allocated. Since we increment our module 1162 * refcount any time someone outside the main CTL module allocates 1163 * a pool, we shouldn't have any problems here. The user won't be 1164 * able to unload the CTL module until client modules have 1165 * successfully unloaded. 1166 */ 1167 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1168 ctl_pool_free(pool); 1169 1170#if 0 1171 ctl_shutdown_thread(softc->work_thread); 1172 mtx_destroy(&softc->queue_lock); 1173#endif 1174 1175 mtx_destroy(&softc->pool_lock); 1176 mtx_destroy(&softc->ctl_lock); 1177 1178 destroy_dev(softc->dev); 1179 1180 sysctl_ctx_free(&softc->sysctl_ctx); 1181 1182 free(control_softc, M_DEVBUF); 1183 control_softc = NULL; 1184 1185 if (bootverbose) 1186 printf("ctl: CAM Target Layer unloaded\n"); 1187} 1188 1189static int 1190ctl_module_event_handler(module_t mod, int what, void *arg) 1191{ 1192 1193 switch (what) { 1194 case MOD_LOAD: 1195 return (ctl_init()); 1196 case MOD_UNLOAD: 1197 return (EBUSY); 1198 default: 1199 return (EOPNOTSUPP); 1200 } 1201} 1202 1203/* 1204 * XXX KDM should we do some access checks here? Bump a reference count to 1205 * prevent a CTL module from being unloaded while someone has it open? 1206 */ 1207static int 1208ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1209{ 1210 return (0); 1211} 1212 1213static int 1214ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1215{ 1216 return (0); 1217} 1218 1219int 1220ctl_port_enable(ctl_port_type port_type) 1221{ 1222 struct ctl_softc *softc; 1223 struct ctl_port *port; 1224 1225 if (ctl_is_single == 0) { 1226 union ctl_ha_msg msg_info; 1227 int isc_retval; 1228 1229#if 0 1230 printf("%s: HA mode, synchronizing frontend enable\n", 1231 __func__); 1232#endif 1233 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1234 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1235 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1236 printf("Sync msg send error retval %d\n", isc_retval); 1237 } 1238 if (!rcv_sync_msg) { 1239 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1240 sizeof(msg_info), 1); 1241 } 1242#if 0 1243 printf("CTL:Frontend Enable\n"); 1244 } else { 1245 printf("%s: single mode, skipping frontend synchronization\n", 1246 __func__); 1247#endif 1248 } 1249 1250 softc = control_softc; 1251 1252 STAILQ_FOREACH(port, &softc->port_list, links) { 1253 if (port_type & port->port_type) 1254 { 1255#if 0 1256 printf("port %d\n", port->targ_port); 1257#endif 1258 ctl_port_online(port); 1259 } 1260 } 1261 1262 return (0); 1263} 1264 1265int 1266ctl_port_disable(ctl_port_type port_type) 1267{ 1268 struct ctl_softc *softc; 1269 struct ctl_port *port; 1270 1271 softc = control_softc; 1272 1273 STAILQ_FOREACH(port, &softc->port_list, links) { 1274 if (port_type & port->port_type) 1275 ctl_port_offline(port); 1276 } 1277 1278 return (0); 1279} 1280 1281/* 1282 * Returns 0 for success, 1 for failure. 1283 * Currently the only failure mode is if there aren't enough entries 1284 * allocated. So, in case of a failure, look at num_entries_dropped, 1285 * reallocate and try again. 1286 */ 1287int 1288ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1289 int *num_entries_filled, int *num_entries_dropped, 1290 ctl_port_type port_type, int no_virtual) 1291{ 1292 struct ctl_softc *softc; 1293 struct ctl_port *port; 1294 int entries_dropped, entries_filled; 1295 int retval; 1296 int i; 1297 1298 softc = control_softc; 1299 1300 retval = 0; 1301 entries_filled = 0; 1302 entries_dropped = 0; 1303 1304 i = 0; 1305 mtx_lock(&softc->ctl_lock); 1306 STAILQ_FOREACH(port, &softc->port_list, links) { 1307 struct ctl_port_entry *entry; 1308 1309 if ((port->port_type & port_type) == 0) 1310 continue; 1311 1312 if ((no_virtual != 0) 1313 && (port->virtual_port != 0)) 1314 continue; 1315 1316 if (entries_filled >= num_entries_alloced) { 1317 entries_dropped++; 1318 continue; 1319 } 1320 entry = &entries[i]; 1321 1322 entry->port_type = port->port_type; 1323 strlcpy(entry->port_name, port->port_name, 1324 sizeof(entry->port_name)); 1325 entry->physical_port = port->physical_port; 1326 entry->virtual_port = port->virtual_port; 1327 entry->wwnn = port->wwnn; 1328 entry->wwpn = port->wwpn; 1329 1330 i++; 1331 entries_filled++; 1332 } 1333 1334 mtx_unlock(&softc->ctl_lock); 1335 1336 if (entries_dropped > 0) 1337 retval = 1; 1338 1339 *num_entries_dropped = entries_dropped; 1340 *num_entries_filled = entries_filled; 1341 1342 return (retval); 1343} 1344 1345static void 1346ctl_ioctl_online(void *arg) 1347{ 1348 struct ctl_ioctl_info *ioctl_info; 1349 1350 ioctl_info = (struct ctl_ioctl_info *)arg; 1351 1352 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1353} 1354 1355static void 1356ctl_ioctl_offline(void *arg) 1357{ 1358 struct ctl_ioctl_info *ioctl_info; 1359 1360 ioctl_info = (struct ctl_ioctl_info *)arg; 1361 1362 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1363} 1364 1365/* 1366 * Remove an initiator by port number and initiator ID. 1367 * Returns 0 for success, -1 for failure. 1368 */ 1369int 1370ctl_remove_initiator(struct ctl_port *port, int iid) 1371{ 1372 struct ctl_softc *softc = control_softc; 1373 1374 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1375 1376 if (iid > CTL_MAX_INIT_PER_PORT) { 1377 printf("%s: initiator ID %u > maximun %u!\n", 1378 __func__, iid, CTL_MAX_INIT_PER_PORT); 1379 return (-1); 1380 } 1381 1382 mtx_lock(&softc->ctl_lock); 1383 port->wwpn_iid[iid].in_use--; 1384 port->wwpn_iid[iid].last_use = time_uptime; 1385 mtx_unlock(&softc->ctl_lock); 1386 1387 return (0); 1388} 1389 1390/* 1391 * Add an initiator to the initiator map. 1392 * Returns iid for success, < 0 for failure. 1393 */ 1394int 1395ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1396{ 1397 struct ctl_softc *softc = control_softc; 1398 time_t best_time; 1399 int i, best; 1400 1401 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1402 1403 if (iid >= CTL_MAX_INIT_PER_PORT) { 1404 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1405 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1406 free(name, M_CTL); 1407 return (-1); 1408 } 1409 1410 mtx_lock(&softc->ctl_lock); 1411 1412 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1413 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1414 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1415 iid = i; 1416 break; 1417 } 1418 if (name != NULL && port->wwpn_iid[i].name != NULL && 1419 strcmp(name, port->wwpn_iid[i].name) == 0) { 1420 iid = i; 1421 break; 1422 } 1423 } 1424 } 1425 1426 if (iid < 0) { 1427 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1428 if (port->wwpn_iid[i].in_use == 0 && 1429 port->wwpn_iid[i].wwpn == 0 && 1430 port->wwpn_iid[i].name == NULL) { 1431 iid = i; 1432 break; 1433 } 1434 } 1435 } 1436 1437 if (iid < 0) { 1438 best = -1; 1439 best_time = INT32_MAX; 1440 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1441 if (port->wwpn_iid[i].in_use == 0) { 1442 if (port->wwpn_iid[i].last_use < best_time) { 1443 best = i; 1444 best_time = port->wwpn_iid[i].last_use; 1445 } 1446 } 1447 } 1448 iid = best; 1449 } 1450 1451 if (iid < 0) { 1452 mtx_unlock(&softc->ctl_lock); 1453 free(name, M_CTL); 1454 return (-2); 1455 } 1456 1457 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1458 /* 1459 * This is not an error yet. 1460 */ 1461 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1462#if 0 1463 printf("%s: port %d iid %u WWPN %#jx arrived" 1464 " again\n", __func__, port->targ_port, 1465 iid, (uintmax_t)wwpn); 1466#endif 1467 goto take; 1468 } 1469 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1470 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1471#if 0 1472 printf("%s: port %d iid %u name '%s' arrived" 1473 " again\n", __func__, port->targ_port, 1474 iid, name); 1475#endif 1476 goto take; 1477 } 1478 1479 /* 1480 * This is an error, but what do we do about it? The 1481 * driver is telling us we have a new WWPN for this 1482 * initiator ID, so we pretty much need to use it. 1483 */ 1484 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1485 " but WWPN %#jx '%s' is still at that address\n", 1486 __func__, port->targ_port, iid, wwpn, name, 1487 (uintmax_t)port->wwpn_iid[iid].wwpn, 1488 port->wwpn_iid[iid].name); 1489 1490 /* 1491 * XXX KDM clear have_ca and ua_pending on each LUN for 1492 * this initiator. 1493 */ 1494 } 1495take: 1496 free(port->wwpn_iid[iid].name, M_CTL); 1497 port->wwpn_iid[iid].name = name; 1498 port->wwpn_iid[iid].wwpn = wwpn; 1499 port->wwpn_iid[iid].in_use++; 1500 mtx_unlock(&softc->ctl_lock); 1501 1502 return (iid); 1503} 1504 1505static int 1506ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1507{ 1508 int len; 1509 1510 switch (port->port_type) { 1511 case CTL_PORT_FC: 1512 { 1513 struct scsi_transportid_fcp *id = 1514 (struct scsi_transportid_fcp *)buf; 1515 if (port->wwpn_iid[iid].wwpn == 0) 1516 return (0); 1517 memset(id, 0, sizeof(*id)); 1518 id->format_protocol = SCSI_PROTO_FC; 1519 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1520 return (sizeof(*id)); 1521 } 1522 case CTL_PORT_ISCSI: 1523 { 1524 struct scsi_transportid_iscsi_port *id = 1525 (struct scsi_transportid_iscsi_port *)buf; 1526 if (port->wwpn_iid[iid].name == NULL) 1527 return (0); 1528 memset(id, 0, 256); 1529 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1530 SCSI_PROTO_ISCSI; 1531 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1532 len = roundup2(min(len, 252), 4); 1533 scsi_ulto2b(len, id->additional_length); 1534 return (sizeof(*id) + len); 1535 } 1536 case CTL_PORT_SAS: 1537 { 1538 struct scsi_transportid_sas *id = 1539 (struct scsi_transportid_sas *)buf; 1540 if (port->wwpn_iid[iid].wwpn == 0) 1541 return (0); 1542 memset(id, 0, sizeof(*id)); 1543 id->format_protocol = SCSI_PROTO_SAS; 1544 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1545 return (sizeof(*id)); 1546 } 1547 default: 1548 { 1549 struct scsi_transportid_spi *id = 1550 (struct scsi_transportid_spi *)buf; 1551 memset(id, 0, sizeof(*id)); 1552 id->format_protocol = SCSI_PROTO_SPI; 1553 scsi_ulto2b(iid, id->scsi_addr); 1554 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1555 return (sizeof(*id)); 1556 } 1557 } 1558} 1559 1560static int 1561ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1562{ 1563 return (0); 1564} 1565 1566static int 1567ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1568{ 1569 return (0); 1570} 1571 1572/* 1573 * Data movement routine for the CTL ioctl frontend port. 1574 */ 1575static int 1576ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1577{ 1578 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1579 struct ctl_sg_entry ext_entry, kern_entry; 1580 int ext_sglen, ext_sg_entries, kern_sg_entries; 1581 int ext_sg_start, ext_offset; 1582 int len_to_copy, len_copied; 1583 int kern_watermark, ext_watermark; 1584 int ext_sglist_malloced; 1585 int i, j; 1586 1587 ext_sglist_malloced = 0; 1588 ext_sg_start = 0; 1589 ext_offset = 0; 1590 1591 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1592 1593 /* 1594 * If this flag is set, fake the data transfer. 1595 */ 1596 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1597 ctsio->ext_data_filled = ctsio->ext_data_len; 1598 goto bailout; 1599 } 1600 1601 /* 1602 * To simplify things here, if we have a single buffer, stick it in 1603 * a S/G entry and just make it a single entry S/G list. 1604 */ 1605 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1606 int len_seen; 1607 1608 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1609 1610 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1611 M_WAITOK); 1612 ext_sglist_malloced = 1; 1613 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1614 ext_sglen) != 0) { 1615 ctl_set_internal_failure(ctsio, 1616 /*sks_valid*/ 0, 1617 /*retry_count*/ 0); 1618 goto bailout; 1619 } 1620 ext_sg_entries = ctsio->ext_sg_entries; 1621 len_seen = 0; 1622 for (i = 0; i < ext_sg_entries; i++) { 1623 if ((len_seen + ext_sglist[i].len) >= 1624 ctsio->ext_data_filled) { 1625 ext_sg_start = i; 1626 ext_offset = ctsio->ext_data_filled - len_seen; 1627 break; 1628 } 1629 len_seen += ext_sglist[i].len; 1630 } 1631 } else { 1632 ext_sglist = &ext_entry; 1633 ext_sglist->addr = ctsio->ext_data_ptr; 1634 ext_sglist->len = ctsio->ext_data_len; 1635 ext_sg_entries = 1; 1636 ext_sg_start = 0; 1637 ext_offset = ctsio->ext_data_filled; 1638 } 1639 1640 if (ctsio->kern_sg_entries > 0) { 1641 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1642 kern_sg_entries = ctsio->kern_sg_entries; 1643 } else { 1644 kern_sglist = &kern_entry; 1645 kern_sglist->addr = ctsio->kern_data_ptr; 1646 kern_sglist->len = ctsio->kern_data_len; 1647 kern_sg_entries = 1; 1648 } 1649 1650 1651 kern_watermark = 0; 1652 ext_watermark = ext_offset; 1653 len_copied = 0; 1654 for (i = ext_sg_start, j = 0; 1655 i < ext_sg_entries && j < kern_sg_entries;) { 1656 uint8_t *ext_ptr, *kern_ptr; 1657 1658 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1659 kern_sglist[j].len - kern_watermark); 1660 1661 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1662 ext_ptr = ext_ptr + ext_watermark; 1663 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1664 /* 1665 * XXX KDM fix this! 1666 */ 1667 panic("need to implement bus address support"); 1668#if 0 1669 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1670#endif 1671 } else 1672 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1673 kern_ptr = kern_ptr + kern_watermark; 1674 1675 kern_watermark += len_to_copy; 1676 ext_watermark += len_to_copy; 1677 1678 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1679 CTL_FLAG_DATA_IN) { 1680 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1681 "bytes to user\n", len_to_copy)); 1682 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1683 "to %p\n", kern_ptr, ext_ptr)); 1684 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1685 ctl_set_internal_failure(ctsio, 1686 /*sks_valid*/ 0, 1687 /*retry_count*/ 0); 1688 goto bailout; 1689 } 1690 } else { 1691 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1692 "bytes from user\n", len_to_copy)); 1693 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1694 "to %p\n", ext_ptr, kern_ptr)); 1695 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1696 ctl_set_internal_failure(ctsio, 1697 /*sks_valid*/ 0, 1698 /*retry_count*/0); 1699 goto bailout; 1700 } 1701 } 1702 1703 len_copied += len_to_copy; 1704 1705 if (ext_sglist[i].len == ext_watermark) { 1706 i++; 1707 ext_watermark = 0; 1708 } 1709 1710 if (kern_sglist[j].len == kern_watermark) { 1711 j++; 1712 kern_watermark = 0; 1713 } 1714 } 1715 1716 ctsio->ext_data_filled += len_copied; 1717 1718 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1719 "kern_sg_entries: %d\n", ext_sg_entries, 1720 kern_sg_entries)); 1721 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1722 "kern_data_len = %d\n", ctsio->ext_data_len, 1723 ctsio->kern_data_len)); 1724 1725 1726 /* XXX KDM set residual?? */ 1727bailout: 1728 1729 if (ext_sglist_malloced != 0) 1730 free(ext_sglist, M_CTL); 1731 1732 return (CTL_RETVAL_COMPLETE); 1733} 1734 1735/* 1736 * Serialize a command that went down the "wrong" side, and so was sent to 1737 * this controller for execution. The logic is a little different than the 1738 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1739 * sent back to the other side, but in the success case, we execute the 1740 * command on this side (XFER mode) or tell the other side to execute it 1741 * (SER_ONLY mode). 1742 */ 1743static int 1744ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1745{ 1746 struct ctl_softc *ctl_softc; 1747 union ctl_ha_msg msg_info; 1748 struct ctl_lun *lun; 1749 int retval = 0; 1750 uint32_t targ_lun; 1751 1752 ctl_softc = control_softc; 1753 1754 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1755 lun = ctl_softc->ctl_luns[targ_lun]; 1756 if (lun==NULL) 1757 { 1758 /* 1759 * Why isn't LUN defined? The other side wouldn't 1760 * send a cmd if the LUN is undefined. 1761 */ 1762 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1763 1764 /* "Logical unit not supported" */ 1765 ctl_set_sense_data(&msg_info.scsi.sense_data, 1766 lun, 1767 /*sense_format*/SSD_TYPE_NONE, 1768 /*current_error*/ 1, 1769 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1770 /*asc*/ 0x25, 1771 /*ascq*/ 0x00, 1772 SSD_ELEM_NONE); 1773 1774 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1775 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1776 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1777 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1778 msg_info.hdr.serializing_sc = NULL; 1779 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1780 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1781 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1782 } 1783 return(1); 1784 1785 } 1786 1787 mtx_lock(&lun->lun_lock); 1788 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1789 1790 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1791 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1792 ooa_links))) { 1793 case CTL_ACTION_BLOCK: 1794 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1795 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1796 blocked_links); 1797 break; 1798 case CTL_ACTION_PASS: 1799 case CTL_ACTION_SKIP: 1800 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1801 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1802 ctl_enqueue_rtr((union ctl_io *)ctsio); 1803 } else { 1804 1805 /* send msg back to other side */ 1806 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1807 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1808 msg_info.hdr.msg_type = CTL_MSG_R2R; 1809#if 0 1810 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1811#endif 1812 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1813 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1814 } 1815 } 1816 break; 1817 case CTL_ACTION_OVERLAP: 1818 /* OVERLAPPED COMMANDS ATTEMPTED */ 1819 ctl_set_sense_data(&msg_info.scsi.sense_data, 1820 lun, 1821 /*sense_format*/SSD_TYPE_NONE, 1822 /*current_error*/ 1, 1823 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1824 /*asc*/ 0x4E, 1825 /*ascq*/ 0x00, 1826 SSD_ELEM_NONE); 1827 1828 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1829 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1830 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1831 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1832 msg_info.hdr.serializing_sc = NULL; 1833 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1834#if 0 1835 printf("BAD JUJU:Major Bummer Overlap\n"); 1836#endif 1837 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1838 retval = 1; 1839 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1840 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1841 } 1842 break; 1843 case CTL_ACTION_OVERLAP_TAG: 1844 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1845 ctl_set_sense_data(&msg_info.scsi.sense_data, 1846 lun, 1847 /*sense_format*/SSD_TYPE_NONE, 1848 /*current_error*/ 1, 1849 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1850 /*asc*/ 0x4D, 1851 /*ascq*/ ctsio->tag_num & 0xff, 1852 SSD_ELEM_NONE); 1853 1854 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1855 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1856 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1857 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1858 msg_info.hdr.serializing_sc = NULL; 1859 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1860#if 0 1861 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1862#endif 1863 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1864 retval = 1; 1865 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1866 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1867 } 1868 break; 1869 case CTL_ACTION_ERROR: 1870 default: 1871 /* "Internal target failure" */ 1872 ctl_set_sense_data(&msg_info.scsi.sense_data, 1873 lun, 1874 /*sense_format*/SSD_TYPE_NONE, 1875 /*current_error*/ 1, 1876 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1877 /*asc*/ 0x44, 1878 /*ascq*/ 0x00, 1879 SSD_ELEM_NONE); 1880 1881 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1882 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1883 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1884 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1885 msg_info.hdr.serializing_sc = NULL; 1886 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1887#if 0 1888 printf("BAD JUJU:Major Bummer HW Error\n"); 1889#endif 1890 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1891 retval = 1; 1892 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1893 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1894 } 1895 break; 1896 } 1897 mtx_unlock(&lun->lun_lock); 1898 return (retval); 1899} 1900 1901static int 1902ctl_ioctl_submit_wait(union ctl_io *io) 1903{ 1904 struct ctl_fe_ioctl_params params; 1905 ctl_fe_ioctl_state last_state; 1906 int done, retval; 1907 1908 retval = 0; 1909 1910 bzero(¶ms, sizeof(params)); 1911 1912 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1913 cv_init(¶ms.sem, "ctlioccv"); 1914 params.state = CTL_IOCTL_INPROG; 1915 last_state = params.state; 1916 1917 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1918 1919 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1920 1921 /* This shouldn't happen */ 1922 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1923 return (retval); 1924 1925 done = 0; 1926 1927 do { 1928 mtx_lock(¶ms.ioctl_mtx); 1929 /* 1930 * Check the state here, and don't sleep if the state has 1931 * already changed (i.e. wakeup has already occured, but we 1932 * weren't waiting yet). 1933 */ 1934 if (params.state == last_state) { 1935 /* XXX KDM cv_wait_sig instead? */ 1936 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1937 } 1938 last_state = params.state; 1939 1940 switch (params.state) { 1941 case CTL_IOCTL_INPROG: 1942 /* Why did we wake up? */ 1943 /* XXX KDM error here? */ 1944 mtx_unlock(¶ms.ioctl_mtx); 1945 break; 1946 case CTL_IOCTL_DATAMOVE: 1947 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1948 1949 /* 1950 * change last_state back to INPROG to avoid 1951 * deadlock on subsequent data moves. 1952 */ 1953 params.state = last_state = CTL_IOCTL_INPROG; 1954 1955 mtx_unlock(¶ms.ioctl_mtx); 1956 ctl_ioctl_do_datamove(&io->scsiio); 1957 /* 1958 * Note that in some cases, most notably writes, 1959 * this will queue the I/O and call us back later. 1960 * In other cases, generally reads, this routine 1961 * will immediately call back and wake us up, 1962 * probably using our own context. 1963 */ 1964 io->scsiio.be_move_done(io); 1965 break; 1966 case CTL_IOCTL_DONE: 1967 mtx_unlock(¶ms.ioctl_mtx); 1968 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1969 done = 1; 1970 break; 1971 default: 1972 mtx_unlock(¶ms.ioctl_mtx); 1973 /* XXX KDM error here? */ 1974 break; 1975 } 1976 } while (done == 0); 1977 1978 mtx_destroy(¶ms.ioctl_mtx); 1979 cv_destroy(¶ms.sem); 1980 1981 return (CTL_RETVAL_COMPLETE); 1982} 1983 1984static void 1985ctl_ioctl_datamove(union ctl_io *io) 1986{ 1987 struct ctl_fe_ioctl_params *params; 1988 1989 params = (struct ctl_fe_ioctl_params *) 1990 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1991 1992 mtx_lock(¶ms->ioctl_mtx); 1993 params->state = CTL_IOCTL_DATAMOVE; 1994 cv_broadcast(¶ms->sem); 1995 mtx_unlock(¶ms->ioctl_mtx); 1996} 1997 1998static void 1999ctl_ioctl_done(union ctl_io *io) 2000{ 2001 struct ctl_fe_ioctl_params *params; 2002 2003 params = (struct ctl_fe_ioctl_params *) 2004 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2005 2006 mtx_lock(¶ms->ioctl_mtx); 2007 params->state = CTL_IOCTL_DONE; 2008 cv_broadcast(¶ms->sem); 2009 mtx_unlock(¶ms->ioctl_mtx); 2010} 2011 2012static void 2013ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2014{ 2015 struct ctl_fe_ioctl_startstop_info *sd_info; 2016 2017 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2018 2019 sd_info->hs_info.status = metatask->status; 2020 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2021 sd_info->hs_info.luns_complete = 2022 metatask->taskinfo.startstop.luns_complete; 2023 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2024 2025 cv_broadcast(&sd_info->sem); 2026} 2027 2028static void 2029ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2030{ 2031 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2032 2033 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2034 2035 mtx_lock(fe_bbr_info->lock); 2036 fe_bbr_info->bbr_info->status = metatask->status; 2037 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2038 fe_bbr_info->wakeup_done = 1; 2039 mtx_unlock(fe_bbr_info->lock); 2040 2041 cv_broadcast(&fe_bbr_info->sem); 2042} 2043 2044/* 2045 * Returns 0 for success, errno for failure. 2046 */ 2047static int 2048ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2049 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2050{ 2051 union ctl_io *io; 2052 int retval; 2053 2054 retval = 0; 2055 2056 mtx_lock(&lun->lun_lock); 2057 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2058 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2059 ooa_links)) { 2060 struct ctl_ooa_entry *entry; 2061 2062 /* 2063 * If we've got more than we can fit, just count the 2064 * remaining entries. 2065 */ 2066 if (*cur_fill_num >= ooa_hdr->alloc_num) 2067 continue; 2068 2069 entry = &kern_entries[*cur_fill_num]; 2070 2071 entry->tag_num = io->scsiio.tag_num; 2072 entry->lun_num = lun->lun; 2073#ifdef CTL_TIME_IO 2074 entry->start_bt = io->io_hdr.start_bt; 2075#endif 2076 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2077 entry->cdb_len = io->scsiio.cdb_len; 2078 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2079 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2080 2081 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2082 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2083 2084 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2085 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2086 2087 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2088 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2089 2090 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2091 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2092 } 2093 mtx_unlock(&lun->lun_lock); 2094 2095 return (retval); 2096} 2097 2098static void * 2099ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2100 size_t error_str_len) 2101{ 2102 void *kptr; 2103 2104 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2105 2106 if (copyin(user_addr, kptr, len) != 0) { 2107 snprintf(error_str, error_str_len, "Error copying %d bytes " 2108 "from user address %p to kernel address %p", len, 2109 user_addr, kptr); 2110 free(kptr, M_CTL); 2111 return (NULL); 2112 } 2113 2114 return (kptr); 2115} 2116 2117static void 2118ctl_free_args(int num_args, struct ctl_be_arg *args) 2119{ 2120 int i; 2121 2122 if (args == NULL) 2123 return; 2124 2125 for (i = 0; i < num_args; i++) { 2126 free(args[i].kname, M_CTL); 2127 free(args[i].kvalue, M_CTL); 2128 } 2129 2130 free(args, M_CTL); 2131} 2132 2133static struct ctl_be_arg * 2134ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2135 char *error_str, size_t error_str_len) 2136{ 2137 struct ctl_be_arg *args; 2138 int i; 2139 2140 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2141 error_str, error_str_len); 2142 2143 if (args == NULL) 2144 goto bailout; 2145 2146 for (i = 0; i < num_args; i++) { 2147 args[i].kname = NULL; 2148 args[i].kvalue = NULL; 2149 } 2150 2151 for (i = 0; i < num_args; i++) { 2152 uint8_t *tmpptr; 2153 2154 args[i].kname = ctl_copyin_alloc(args[i].name, 2155 args[i].namelen, error_str, error_str_len); 2156 if (args[i].kname == NULL) 2157 goto bailout; 2158 2159 if (args[i].kname[args[i].namelen - 1] != '\0') { 2160 snprintf(error_str, error_str_len, "Argument %d " 2161 "name is not NUL-terminated", i); 2162 goto bailout; 2163 } 2164 2165 if (args[i].flags & CTL_BEARG_RD) { 2166 tmpptr = ctl_copyin_alloc(args[i].value, 2167 args[i].vallen, error_str, error_str_len); 2168 if (tmpptr == NULL) 2169 goto bailout; 2170 if ((args[i].flags & CTL_BEARG_ASCII) 2171 && (tmpptr[args[i].vallen - 1] != '\0')) { 2172 snprintf(error_str, error_str_len, "Argument " 2173 "%d value is not NUL-terminated", i); 2174 goto bailout; 2175 } 2176 args[i].kvalue = tmpptr; 2177 } else { 2178 args[i].kvalue = malloc(args[i].vallen, 2179 M_CTL, M_WAITOK | M_ZERO); 2180 } 2181 } 2182 2183 return (args); 2184bailout: 2185 2186 ctl_free_args(num_args, args); 2187 2188 return (NULL); 2189} 2190 2191static void 2192ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2193{ 2194 int i; 2195 2196 for (i = 0; i < num_args; i++) { 2197 if (args[i].flags & CTL_BEARG_WR) 2198 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2199 } 2200} 2201 2202/* 2203 * Escape characters that are illegal or not recommended in XML. 2204 */ 2205int 2206ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2207{ 2208 int retval; 2209 2210 retval = 0; 2211 2212 for (; *str; str++) { 2213 switch (*str) { 2214 case '&': 2215 retval = sbuf_printf(sb, "&"); 2216 break; 2217 case '>': 2218 retval = sbuf_printf(sb, ">"); 2219 break; 2220 case '<': 2221 retval = sbuf_printf(sb, "<"); 2222 break; 2223 default: 2224 retval = sbuf_putc(sb, *str); 2225 break; 2226 } 2227 2228 if (retval != 0) 2229 break; 2230 2231 } 2232 2233 return (retval); 2234} 2235 2236static int 2237ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2238 struct thread *td) 2239{ 2240 struct ctl_softc *softc; 2241 int retval; 2242 2243 softc = control_softc; 2244 2245 retval = 0; 2246 2247 switch (cmd) { 2248 case CTL_IO: { 2249 union ctl_io *io; 2250 void *pool_tmp; 2251 2252 /* 2253 * If we haven't been "enabled", don't allow any SCSI I/O 2254 * to this FETD. 2255 */ 2256 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2257 retval = EPERM; 2258 break; 2259 } 2260 2261 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2262 if (io == NULL) { 2263 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2264 retval = ENOSPC; 2265 break; 2266 } 2267 2268 /* 2269 * Need to save the pool reference so it doesn't get 2270 * spammed by the user's ctl_io. 2271 */ 2272 pool_tmp = io->io_hdr.pool; 2273 2274 memcpy(io, (void *)addr, sizeof(*io)); 2275 2276 io->io_hdr.pool = pool_tmp; 2277 /* 2278 * No status yet, so make sure the status is set properly. 2279 */ 2280 io->io_hdr.status = CTL_STATUS_NONE; 2281 2282 /* 2283 * The user sets the initiator ID, target and LUN IDs. 2284 */ 2285 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2286 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2287 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2288 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2289 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2290 2291 retval = ctl_ioctl_submit_wait(io); 2292 2293 if (retval != 0) { 2294 ctl_free_io(io); 2295 break; 2296 } 2297 2298 memcpy((void *)addr, io, sizeof(*io)); 2299 2300 /* return this to our pool */ 2301 ctl_free_io(io); 2302 2303 break; 2304 } 2305 case CTL_ENABLE_PORT: 2306 case CTL_DISABLE_PORT: 2307 case CTL_SET_PORT_WWNS: { 2308 struct ctl_port *port; 2309 struct ctl_port_entry *entry; 2310 2311 entry = (struct ctl_port_entry *)addr; 2312 2313 mtx_lock(&softc->ctl_lock); 2314 STAILQ_FOREACH(port, &softc->port_list, links) { 2315 int action, done; 2316 2317 action = 0; 2318 done = 0; 2319 2320 if ((entry->port_type == CTL_PORT_NONE) 2321 && (entry->targ_port == port->targ_port)) { 2322 /* 2323 * If the user only wants to enable or 2324 * disable or set WWNs on a specific port, 2325 * do the operation and we're done. 2326 */ 2327 action = 1; 2328 done = 1; 2329 } else if (entry->port_type & port->port_type) { 2330 /* 2331 * Compare the user's type mask with the 2332 * particular frontend type to see if we 2333 * have a match. 2334 */ 2335 action = 1; 2336 done = 0; 2337 2338 /* 2339 * Make sure the user isn't trying to set 2340 * WWNs on multiple ports at the same time. 2341 */ 2342 if (cmd == CTL_SET_PORT_WWNS) { 2343 printf("%s: Can't set WWNs on " 2344 "multiple ports\n", __func__); 2345 retval = EINVAL; 2346 break; 2347 } 2348 } 2349 if (action != 0) { 2350 /* 2351 * XXX KDM we have to drop the lock here, 2352 * because the online/offline operations 2353 * can potentially block. We need to 2354 * reference count the frontends so they 2355 * can't go away, 2356 */ 2357 mtx_unlock(&softc->ctl_lock); 2358 2359 if (cmd == CTL_ENABLE_PORT) { 2360 struct ctl_lun *lun; 2361 2362 STAILQ_FOREACH(lun, &softc->lun_list, 2363 links) { 2364 port->lun_enable(port->targ_lun_arg, 2365 lun->target, 2366 lun->lun); 2367 } 2368 2369 ctl_port_online(port); 2370 } else if (cmd == CTL_DISABLE_PORT) { 2371 struct ctl_lun *lun; 2372 2373 ctl_port_offline(port); 2374 2375 STAILQ_FOREACH(lun, &softc->lun_list, 2376 links) { 2377 port->lun_disable( 2378 port->targ_lun_arg, 2379 lun->target, 2380 lun->lun); 2381 } 2382 } 2383 2384 mtx_lock(&softc->ctl_lock); 2385 2386 if (cmd == CTL_SET_PORT_WWNS) 2387 ctl_port_set_wwns(port, 2388 (entry->flags & CTL_PORT_WWNN_VALID) ? 2389 1 : 0, entry->wwnn, 2390 (entry->flags & CTL_PORT_WWPN_VALID) ? 2391 1 : 0, entry->wwpn); 2392 } 2393 if (done != 0) 2394 break; 2395 } 2396 mtx_unlock(&softc->ctl_lock); 2397 break; 2398 } 2399 case CTL_GET_PORT_LIST: { 2400 struct ctl_port *port; 2401 struct ctl_port_list *list; 2402 int i; 2403 2404 list = (struct ctl_port_list *)addr; 2405 2406 if (list->alloc_len != (list->alloc_num * 2407 sizeof(struct ctl_port_entry))) { 2408 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2409 "alloc_num %u * sizeof(struct ctl_port_entry) " 2410 "%zu\n", __func__, list->alloc_len, 2411 list->alloc_num, sizeof(struct ctl_port_entry)); 2412 retval = EINVAL; 2413 break; 2414 } 2415 list->fill_len = 0; 2416 list->fill_num = 0; 2417 list->dropped_num = 0; 2418 i = 0; 2419 mtx_lock(&softc->ctl_lock); 2420 STAILQ_FOREACH(port, &softc->port_list, links) { 2421 struct ctl_port_entry entry, *list_entry; 2422 2423 if (list->fill_num >= list->alloc_num) { 2424 list->dropped_num++; 2425 continue; 2426 } 2427 2428 entry.port_type = port->port_type; 2429 strlcpy(entry.port_name, port->port_name, 2430 sizeof(entry.port_name)); 2431 entry.targ_port = port->targ_port; 2432 entry.physical_port = port->physical_port; 2433 entry.virtual_port = port->virtual_port; 2434 entry.wwnn = port->wwnn; 2435 entry.wwpn = port->wwpn; 2436 if (port->status & CTL_PORT_STATUS_ONLINE) 2437 entry.online = 1; 2438 else 2439 entry.online = 0; 2440 2441 list_entry = &list->entries[i]; 2442 2443 retval = copyout(&entry, list_entry, sizeof(entry)); 2444 if (retval != 0) { 2445 printf("%s: CTL_GET_PORT_LIST: copyout " 2446 "returned %d\n", __func__, retval); 2447 break; 2448 } 2449 i++; 2450 list->fill_num++; 2451 list->fill_len += sizeof(entry); 2452 } 2453 mtx_unlock(&softc->ctl_lock); 2454 2455 /* 2456 * If this is non-zero, we had a copyout fault, so there's 2457 * probably no point in attempting to set the status inside 2458 * the structure. 2459 */ 2460 if (retval != 0) 2461 break; 2462 2463 if (list->dropped_num > 0) 2464 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2465 else 2466 list->status = CTL_PORT_LIST_OK; 2467 break; 2468 } 2469 case CTL_DUMP_OOA: { 2470 struct ctl_lun *lun; 2471 union ctl_io *io; 2472 char printbuf[128]; 2473 struct sbuf sb; 2474 2475 mtx_lock(&softc->ctl_lock); 2476 printf("Dumping OOA queues:\n"); 2477 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2478 mtx_lock(&lun->lun_lock); 2479 for (io = (union ctl_io *)TAILQ_FIRST( 2480 &lun->ooa_queue); io != NULL; 2481 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2482 ooa_links)) { 2483 sbuf_new(&sb, printbuf, sizeof(printbuf), 2484 SBUF_FIXEDLEN); 2485 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2486 (intmax_t)lun->lun, 2487 io->scsiio.tag_num, 2488 (io->io_hdr.flags & 2489 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2490 (io->io_hdr.flags & 2491 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2492 (io->io_hdr.flags & 2493 CTL_FLAG_ABORT) ? " ABORT" : "", 2494 (io->io_hdr.flags & 2495 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2496 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2497 sbuf_finish(&sb); 2498 printf("%s\n", sbuf_data(&sb)); 2499 } 2500 mtx_unlock(&lun->lun_lock); 2501 } 2502 printf("OOA queues dump done\n"); 2503 mtx_unlock(&softc->ctl_lock); 2504 break; 2505 } 2506 case CTL_GET_OOA: { 2507 struct ctl_lun *lun; 2508 struct ctl_ooa *ooa_hdr; 2509 struct ctl_ooa_entry *entries; 2510 uint32_t cur_fill_num; 2511 2512 ooa_hdr = (struct ctl_ooa *)addr; 2513 2514 if ((ooa_hdr->alloc_len == 0) 2515 || (ooa_hdr->alloc_num == 0)) { 2516 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2517 "must be non-zero\n", __func__, 2518 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2519 retval = EINVAL; 2520 break; 2521 } 2522 2523 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2524 sizeof(struct ctl_ooa_entry))) { 2525 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2526 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2527 __func__, ooa_hdr->alloc_len, 2528 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2529 retval = EINVAL; 2530 break; 2531 } 2532 2533 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2534 if (entries == NULL) { 2535 printf("%s: could not allocate %d bytes for OOA " 2536 "dump\n", __func__, ooa_hdr->alloc_len); 2537 retval = ENOMEM; 2538 break; 2539 } 2540 2541 mtx_lock(&softc->ctl_lock); 2542 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2543 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2544 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2545 mtx_unlock(&softc->ctl_lock); 2546 free(entries, M_CTL); 2547 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2548 __func__, (uintmax_t)ooa_hdr->lun_num); 2549 retval = EINVAL; 2550 break; 2551 } 2552 2553 cur_fill_num = 0; 2554 2555 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2556 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2557 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2558 ooa_hdr, entries); 2559 if (retval != 0) 2560 break; 2561 } 2562 if (retval != 0) { 2563 mtx_unlock(&softc->ctl_lock); 2564 free(entries, M_CTL); 2565 break; 2566 } 2567 } else { 2568 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2569 2570 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2571 entries); 2572 } 2573 mtx_unlock(&softc->ctl_lock); 2574 2575 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2576 ooa_hdr->fill_len = ooa_hdr->fill_num * 2577 sizeof(struct ctl_ooa_entry); 2578 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2579 if (retval != 0) { 2580 printf("%s: error copying out %d bytes for OOA dump\n", 2581 __func__, ooa_hdr->fill_len); 2582 } 2583 2584 getbintime(&ooa_hdr->cur_bt); 2585 2586 if (cur_fill_num > ooa_hdr->alloc_num) { 2587 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2588 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2589 } else { 2590 ooa_hdr->dropped_num = 0; 2591 ooa_hdr->status = CTL_OOA_OK; 2592 } 2593 2594 free(entries, M_CTL); 2595 break; 2596 } 2597 case CTL_CHECK_OOA: { 2598 union ctl_io *io; 2599 struct ctl_lun *lun; 2600 struct ctl_ooa_info *ooa_info; 2601 2602 2603 ooa_info = (struct ctl_ooa_info *)addr; 2604 2605 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2606 ooa_info->status = CTL_OOA_INVALID_LUN; 2607 break; 2608 } 2609 mtx_lock(&softc->ctl_lock); 2610 lun = softc->ctl_luns[ooa_info->lun_id]; 2611 if (lun == NULL) { 2612 mtx_unlock(&softc->ctl_lock); 2613 ooa_info->status = CTL_OOA_INVALID_LUN; 2614 break; 2615 } 2616 mtx_lock(&lun->lun_lock); 2617 mtx_unlock(&softc->ctl_lock); 2618 ooa_info->num_entries = 0; 2619 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2620 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2621 &io->io_hdr, ooa_links)) { 2622 ooa_info->num_entries++; 2623 } 2624 mtx_unlock(&lun->lun_lock); 2625 2626 ooa_info->status = CTL_OOA_SUCCESS; 2627 2628 break; 2629 } 2630 case CTL_HARD_START: 2631 case CTL_HARD_STOP: { 2632 struct ctl_fe_ioctl_startstop_info ss_info; 2633 struct cfi_metatask *metatask; 2634 struct mtx hs_mtx; 2635 2636 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2637 2638 cv_init(&ss_info.sem, "hard start/stop cv" ); 2639 2640 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2641 if (metatask == NULL) { 2642 retval = ENOMEM; 2643 mtx_destroy(&hs_mtx); 2644 break; 2645 } 2646 2647 if (cmd == CTL_HARD_START) 2648 metatask->tasktype = CFI_TASK_STARTUP; 2649 else 2650 metatask->tasktype = CFI_TASK_SHUTDOWN; 2651 2652 metatask->callback = ctl_ioctl_hard_startstop_callback; 2653 metatask->callback_arg = &ss_info; 2654 2655 cfi_action(metatask); 2656 2657 /* Wait for the callback */ 2658 mtx_lock(&hs_mtx); 2659 cv_wait_sig(&ss_info.sem, &hs_mtx); 2660 mtx_unlock(&hs_mtx); 2661 2662 /* 2663 * All information has been copied from the metatask by the 2664 * time cv_broadcast() is called, so we free the metatask here. 2665 */ 2666 cfi_free_metatask(metatask); 2667 2668 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2669 2670 mtx_destroy(&hs_mtx); 2671 break; 2672 } 2673 case CTL_BBRREAD: { 2674 struct ctl_bbrread_info *bbr_info; 2675 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2676 struct mtx bbr_mtx; 2677 struct cfi_metatask *metatask; 2678 2679 bbr_info = (struct ctl_bbrread_info *)addr; 2680 2681 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2682 2683 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2684 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2685 2686 fe_bbr_info.bbr_info = bbr_info; 2687 fe_bbr_info.lock = &bbr_mtx; 2688 2689 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2690 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2691 2692 if (metatask == NULL) { 2693 mtx_destroy(&bbr_mtx); 2694 cv_destroy(&fe_bbr_info.sem); 2695 retval = ENOMEM; 2696 break; 2697 } 2698 metatask->tasktype = CFI_TASK_BBRREAD; 2699 metatask->callback = ctl_ioctl_bbrread_callback; 2700 metatask->callback_arg = &fe_bbr_info; 2701 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2702 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2703 metatask->taskinfo.bbrread.len = bbr_info->len; 2704 2705 cfi_action(metatask); 2706 2707 mtx_lock(&bbr_mtx); 2708 while (fe_bbr_info.wakeup_done == 0) 2709 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2710 mtx_unlock(&bbr_mtx); 2711 2712 bbr_info->status = metatask->status; 2713 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2714 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2715 memcpy(&bbr_info->sense_data, 2716 &metatask->taskinfo.bbrread.sense_data, 2717 ctl_min(sizeof(bbr_info->sense_data), 2718 sizeof(metatask->taskinfo.bbrread.sense_data))); 2719 2720 cfi_free_metatask(metatask); 2721 2722 mtx_destroy(&bbr_mtx); 2723 cv_destroy(&fe_bbr_info.sem); 2724 2725 break; 2726 } 2727 case CTL_DELAY_IO: { 2728 struct ctl_io_delay_info *delay_info; 2729#ifdef CTL_IO_DELAY 2730 struct ctl_lun *lun; 2731#endif /* CTL_IO_DELAY */ 2732 2733 delay_info = (struct ctl_io_delay_info *)addr; 2734 2735#ifdef CTL_IO_DELAY 2736 mtx_lock(&softc->ctl_lock); 2737 2738 if ((delay_info->lun_id > CTL_MAX_LUNS) 2739 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2740 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2741 } else { 2742 lun = softc->ctl_luns[delay_info->lun_id]; 2743 mtx_lock(&lun->lun_lock); 2744 2745 delay_info->status = CTL_DELAY_STATUS_OK; 2746 2747 switch (delay_info->delay_type) { 2748 case CTL_DELAY_TYPE_CONT: 2749 break; 2750 case CTL_DELAY_TYPE_ONESHOT: 2751 break; 2752 default: 2753 delay_info->status = 2754 CTL_DELAY_STATUS_INVALID_TYPE; 2755 break; 2756 } 2757 2758 switch (delay_info->delay_loc) { 2759 case CTL_DELAY_LOC_DATAMOVE: 2760 lun->delay_info.datamove_type = 2761 delay_info->delay_type; 2762 lun->delay_info.datamove_delay = 2763 delay_info->delay_secs; 2764 break; 2765 case CTL_DELAY_LOC_DONE: 2766 lun->delay_info.done_type = 2767 delay_info->delay_type; 2768 lun->delay_info.done_delay = 2769 delay_info->delay_secs; 2770 break; 2771 default: 2772 delay_info->status = 2773 CTL_DELAY_STATUS_INVALID_LOC; 2774 break; 2775 } 2776 mtx_unlock(&lun->lun_lock); 2777 } 2778 2779 mtx_unlock(&softc->ctl_lock); 2780#else 2781 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2782#endif /* CTL_IO_DELAY */ 2783 break; 2784 } 2785 case CTL_REALSYNC_SET: { 2786 int *syncstate; 2787 2788 syncstate = (int *)addr; 2789 2790 mtx_lock(&softc->ctl_lock); 2791 switch (*syncstate) { 2792 case 0: 2793 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2794 break; 2795 case 1: 2796 softc->flags |= CTL_FLAG_REAL_SYNC; 2797 break; 2798 default: 2799 retval = EINVAL; 2800 break; 2801 } 2802 mtx_unlock(&softc->ctl_lock); 2803 break; 2804 } 2805 case CTL_REALSYNC_GET: { 2806 int *syncstate; 2807 2808 syncstate = (int*)addr; 2809 2810 mtx_lock(&softc->ctl_lock); 2811 if (softc->flags & CTL_FLAG_REAL_SYNC) 2812 *syncstate = 1; 2813 else 2814 *syncstate = 0; 2815 mtx_unlock(&softc->ctl_lock); 2816 2817 break; 2818 } 2819 case CTL_SETSYNC: 2820 case CTL_GETSYNC: { 2821 struct ctl_sync_info *sync_info; 2822 struct ctl_lun *lun; 2823 2824 sync_info = (struct ctl_sync_info *)addr; 2825 2826 mtx_lock(&softc->ctl_lock); 2827 lun = softc->ctl_luns[sync_info->lun_id]; 2828 if (lun == NULL) { 2829 mtx_unlock(&softc->ctl_lock); 2830 sync_info->status = CTL_GS_SYNC_NO_LUN; 2831 } 2832 /* 2833 * Get or set the sync interval. We're not bounds checking 2834 * in the set case, hopefully the user won't do something 2835 * silly. 2836 */ 2837 mtx_lock(&lun->lun_lock); 2838 mtx_unlock(&softc->ctl_lock); 2839 if (cmd == CTL_GETSYNC) 2840 sync_info->sync_interval = lun->sync_interval; 2841 else 2842 lun->sync_interval = sync_info->sync_interval; 2843 mtx_unlock(&lun->lun_lock); 2844 2845 sync_info->status = CTL_GS_SYNC_OK; 2846 2847 break; 2848 } 2849 case CTL_GETSTATS: { 2850 struct ctl_stats *stats; 2851 struct ctl_lun *lun; 2852 int i; 2853 2854 stats = (struct ctl_stats *)addr; 2855 2856 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2857 stats->alloc_len) { 2858 stats->status = CTL_SS_NEED_MORE_SPACE; 2859 stats->num_luns = softc->num_luns; 2860 break; 2861 } 2862 /* 2863 * XXX KDM no locking here. If the LUN list changes, 2864 * things can blow up. 2865 */ 2866 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2867 i++, lun = STAILQ_NEXT(lun, links)) { 2868 retval = copyout(&lun->stats, &stats->lun_stats[i], 2869 sizeof(lun->stats)); 2870 if (retval != 0) 2871 break; 2872 } 2873 stats->num_luns = softc->num_luns; 2874 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2875 softc->num_luns; 2876 stats->status = CTL_SS_OK; 2877#ifdef CTL_TIME_IO 2878 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2879#else 2880 stats->flags = CTL_STATS_FLAG_NONE; 2881#endif 2882 getnanouptime(&stats->timestamp); 2883 break; 2884 } 2885 case CTL_ERROR_INJECT: { 2886 struct ctl_error_desc *err_desc, *new_err_desc; 2887 struct ctl_lun *lun; 2888 2889 err_desc = (struct ctl_error_desc *)addr; 2890 2891 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2892 M_WAITOK | M_ZERO); 2893 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2894 2895 mtx_lock(&softc->ctl_lock); 2896 lun = softc->ctl_luns[err_desc->lun_id]; 2897 if (lun == NULL) { 2898 mtx_unlock(&softc->ctl_lock); 2899 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2900 __func__, (uintmax_t)err_desc->lun_id); 2901 retval = EINVAL; 2902 break; 2903 } 2904 mtx_lock(&lun->lun_lock); 2905 mtx_unlock(&softc->ctl_lock); 2906 2907 /* 2908 * We could do some checking here to verify the validity 2909 * of the request, but given the complexity of error 2910 * injection requests, the checking logic would be fairly 2911 * complex. 2912 * 2913 * For now, if the request is invalid, it just won't get 2914 * executed and might get deleted. 2915 */ 2916 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2917 2918 /* 2919 * XXX KDM check to make sure the serial number is unique, 2920 * in case we somehow manage to wrap. That shouldn't 2921 * happen for a very long time, but it's the right thing to 2922 * do. 2923 */ 2924 new_err_desc->serial = lun->error_serial; 2925 err_desc->serial = lun->error_serial; 2926 lun->error_serial++; 2927 2928 mtx_unlock(&lun->lun_lock); 2929 break; 2930 } 2931 case CTL_ERROR_INJECT_DELETE: { 2932 struct ctl_error_desc *delete_desc, *desc, *desc2; 2933 struct ctl_lun *lun; 2934 int delete_done; 2935 2936 delete_desc = (struct ctl_error_desc *)addr; 2937 delete_done = 0; 2938 2939 mtx_lock(&softc->ctl_lock); 2940 lun = softc->ctl_luns[delete_desc->lun_id]; 2941 if (lun == NULL) { 2942 mtx_unlock(&softc->ctl_lock); 2943 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2944 __func__, (uintmax_t)delete_desc->lun_id); 2945 retval = EINVAL; 2946 break; 2947 } 2948 mtx_lock(&lun->lun_lock); 2949 mtx_unlock(&softc->ctl_lock); 2950 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2951 if (desc->serial != delete_desc->serial) 2952 continue; 2953 2954 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2955 links); 2956 free(desc, M_CTL); 2957 delete_done = 1; 2958 } 2959 mtx_unlock(&lun->lun_lock); 2960 if (delete_done == 0) { 2961 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2962 "error serial %ju on LUN %u\n", __func__, 2963 delete_desc->serial, delete_desc->lun_id); 2964 retval = EINVAL; 2965 break; 2966 } 2967 break; 2968 } 2969 case CTL_DUMP_STRUCTS: { 2970 int i, j, k, idx; 2971 struct ctl_port *port; 2972 struct ctl_frontend *fe; 2973 2974 mtx_lock(&softc->ctl_lock); 2975 printf("CTL Persistent Reservation information start:\n"); 2976 for (i = 0; i < CTL_MAX_LUNS; i++) { 2977 struct ctl_lun *lun; 2978 2979 lun = softc->ctl_luns[i]; 2980 2981 if ((lun == NULL) 2982 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2983 continue; 2984 2985 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2986 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2987 idx = j * CTL_MAX_INIT_PER_PORT + k; 2988 if (lun->per_res[idx].registered == 0) 2989 continue; 2990 printf(" LUN %d port %d iid %d key " 2991 "%#jx\n", i, j, k, 2992 (uintmax_t)scsi_8btou64( 2993 lun->per_res[idx].res_key.key)); 2994 } 2995 } 2996 } 2997 printf("CTL Persistent Reservation information end\n"); 2998 printf("CTL Ports:\n"); 2999 STAILQ_FOREACH(port, &softc->port_list, links) { 3000 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3001 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3002 port->frontend->name, port->port_type, 3003 port->physical_port, port->virtual_port, 3004 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3005 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3006 if (port->wwpn_iid[j].in_use == 0 && 3007 port->wwpn_iid[j].wwpn == 0 && 3008 port->wwpn_iid[j].name == NULL) 3009 continue; 3010 3011 printf(" iid %u use %d WWPN %#jx '%s'\n", 3012 j, port->wwpn_iid[j].in_use, 3013 (uintmax_t)port->wwpn_iid[j].wwpn, 3014 port->wwpn_iid[j].name); 3015 } 3016 } 3017 printf("CTL Port information end\n"); 3018 mtx_unlock(&softc->ctl_lock); 3019 /* 3020 * XXX KDM calling this without a lock. We'd likely want 3021 * to drop the lock before calling the frontend's dump 3022 * routine anyway. 3023 */ 3024 printf("CTL Frontends:\n"); 3025 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3026 printf(" Frontend '%s'\n", fe->name); 3027 if (fe->fe_dump != NULL) 3028 fe->fe_dump(); 3029 } 3030 printf("CTL Frontend information end\n"); 3031 break; 3032 } 3033 case CTL_LUN_REQ: { 3034 struct ctl_lun_req *lun_req; 3035 struct ctl_backend_driver *backend; 3036 3037 lun_req = (struct ctl_lun_req *)addr; 3038 3039 backend = ctl_backend_find(lun_req->backend); 3040 if (backend == NULL) { 3041 lun_req->status = CTL_LUN_ERROR; 3042 snprintf(lun_req->error_str, 3043 sizeof(lun_req->error_str), 3044 "Backend \"%s\" not found.", 3045 lun_req->backend); 3046 break; 3047 } 3048 if (lun_req->num_be_args > 0) { 3049 lun_req->kern_be_args = ctl_copyin_args( 3050 lun_req->num_be_args, 3051 lun_req->be_args, 3052 lun_req->error_str, 3053 sizeof(lun_req->error_str)); 3054 if (lun_req->kern_be_args == NULL) { 3055 lun_req->status = CTL_LUN_ERROR; 3056 break; 3057 } 3058 } 3059 3060 retval = backend->ioctl(dev, cmd, addr, flag, td); 3061 3062 if (lun_req->num_be_args > 0) { 3063 ctl_copyout_args(lun_req->num_be_args, 3064 lun_req->kern_be_args); 3065 ctl_free_args(lun_req->num_be_args, 3066 lun_req->kern_be_args); 3067 } 3068 break; 3069 } 3070 case CTL_LUN_LIST: { 3071 struct sbuf *sb; 3072 struct ctl_lun *lun; 3073 struct ctl_lun_list *list; 3074 struct ctl_option *opt; 3075 3076 list = (struct ctl_lun_list *)addr; 3077 3078 /* 3079 * Allocate a fixed length sbuf here, based on the length 3080 * of the user's buffer. We could allocate an auto-extending 3081 * buffer, and then tell the user how much larger our 3082 * amount of data is than his buffer, but that presents 3083 * some problems: 3084 * 3085 * 1. The sbuf(9) routines use a blocking malloc, and so 3086 * we can't hold a lock while calling them with an 3087 * auto-extending buffer. 3088 * 3089 * 2. There is not currently a LUN reference counting 3090 * mechanism, outside of outstanding transactions on 3091 * the LUN's OOA queue. So a LUN could go away on us 3092 * while we're getting the LUN number, backend-specific 3093 * information, etc. Thus, given the way things 3094 * currently work, we need to hold the CTL lock while 3095 * grabbing LUN information. 3096 * 3097 * So, from the user's standpoint, the best thing to do is 3098 * allocate what he thinks is a reasonable buffer length, 3099 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3100 * double the buffer length and try again. (And repeat 3101 * that until he succeeds.) 3102 */ 3103 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3104 if (sb == NULL) { 3105 list->status = CTL_LUN_LIST_ERROR; 3106 snprintf(list->error_str, sizeof(list->error_str), 3107 "Unable to allocate %d bytes for LUN list", 3108 list->alloc_len); 3109 break; 3110 } 3111 3112 sbuf_printf(sb, "<ctllunlist>\n"); 3113 3114 mtx_lock(&softc->ctl_lock); 3115 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3116 mtx_lock(&lun->lun_lock); 3117 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3118 (uintmax_t)lun->lun); 3119 3120 /* 3121 * Bail out as soon as we see that we've overfilled 3122 * the buffer. 3123 */ 3124 if (retval != 0) 3125 break; 3126 3127 retval = sbuf_printf(sb, "\t<backend_type>%s" 3128 "</backend_type>\n", 3129 (lun->backend == NULL) ? "none" : 3130 lun->backend->name); 3131 3132 if (retval != 0) 3133 break; 3134 3135 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3136 lun->be_lun->lun_type); 3137 3138 if (retval != 0) 3139 break; 3140 3141 if (lun->backend == NULL) { 3142 retval = sbuf_printf(sb, "</lun>\n"); 3143 if (retval != 0) 3144 break; 3145 continue; 3146 } 3147 3148 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3149 (lun->be_lun->maxlba > 0) ? 3150 lun->be_lun->maxlba + 1 : 0); 3151 3152 if (retval != 0) 3153 break; 3154 3155 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3156 lun->be_lun->blocksize); 3157 3158 if (retval != 0) 3159 break; 3160 3161 retval = sbuf_printf(sb, "\t<serial_number>"); 3162 3163 if (retval != 0) 3164 break; 3165 3166 retval = ctl_sbuf_printf_esc(sb, 3167 lun->be_lun->serial_num); 3168 3169 if (retval != 0) 3170 break; 3171 3172 retval = sbuf_printf(sb, "</serial_number>\n"); 3173 3174 if (retval != 0) 3175 break; 3176 3177 retval = sbuf_printf(sb, "\t<device_id>"); 3178 3179 if (retval != 0) 3180 break; 3181 3182 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3183 3184 if (retval != 0) 3185 break; 3186 3187 retval = sbuf_printf(sb, "</device_id>\n"); 3188 3189 if (retval != 0) 3190 break; 3191 3192 if (lun->backend->lun_info != NULL) { 3193 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3194 if (retval != 0) 3195 break; 3196 } 3197 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3198 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3199 opt->name, opt->value, opt->name); 3200 if (retval != 0) 3201 break; 3202 } 3203 3204 retval = sbuf_printf(sb, "</lun>\n"); 3205 3206 if (retval != 0) 3207 break; 3208 mtx_unlock(&lun->lun_lock); 3209 } 3210 if (lun != NULL) 3211 mtx_unlock(&lun->lun_lock); 3212 mtx_unlock(&softc->ctl_lock); 3213 3214 if ((retval != 0) 3215 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3216 retval = 0; 3217 sbuf_delete(sb); 3218 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3219 snprintf(list->error_str, sizeof(list->error_str), 3220 "Out of space, %d bytes is too small", 3221 list->alloc_len); 3222 break; 3223 } 3224 3225 sbuf_finish(sb); 3226 3227 retval = copyout(sbuf_data(sb), list->lun_xml, 3228 sbuf_len(sb) + 1); 3229 3230 list->fill_len = sbuf_len(sb) + 1; 3231 list->status = CTL_LUN_LIST_OK; 3232 sbuf_delete(sb); 3233 break; 3234 } 3235 case CTL_ISCSI: { 3236 struct ctl_iscsi *ci; 3237 struct ctl_frontend *fe; 3238 3239 ci = (struct ctl_iscsi *)addr; 3240 3241 fe = ctl_frontend_find("iscsi"); 3242 if (fe == NULL) { 3243 ci->status = CTL_ISCSI_ERROR; 3244 snprintf(ci->error_str, sizeof(ci->error_str), 3245 "Frontend \"iscsi\" not found."); 3246 break; 3247 } 3248 3249 retval = fe->ioctl(dev, cmd, addr, flag, td); 3250 break; 3251 } 3252 case CTL_PORT_REQ: { 3253 struct ctl_req *req; 3254 struct ctl_frontend *fe; 3255 3256 req = (struct ctl_req *)addr; 3257 3258 fe = ctl_frontend_find(req->driver); 3259 if (fe == NULL) { 3260 req->status = CTL_LUN_ERROR; 3261 snprintf(req->error_str, sizeof(req->error_str), 3262 "Frontend \"%s\" not found.", req->driver); 3263 break; 3264 } 3265 if (req->num_args > 0) { 3266 req->kern_args = ctl_copyin_args(req->num_args, 3267 req->args, req->error_str, sizeof(req->error_str)); 3268 if (req->kern_args == NULL) { 3269 req->status = CTL_LUN_ERROR; 3270 break; 3271 } 3272 } 3273 3274 retval = fe->ioctl(dev, cmd, addr, flag, td); 3275 3276 if (req->num_args > 0) { 3277 ctl_copyout_args(req->num_args, req->kern_args); 3278 ctl_free_args(req->num_args, req->kern_args); 3279 } 3280 break; 3281 } 3282 case CTL_PORT_LIST: { 3283 struct sbuf *sb; 3284 struct ctl_port *port; 3285 struct ctl_lun_list *list; 3286 struct ctl_option *opt; 3287 3288 list = (struct ctl_lun_list *)addr; 3289 3290 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3291 if (sb == NULL) { 3292 list->status = CTL_LUN_LIST_ERROR; 3293 snprintf(list->error_str, sizeof(list->error_str), 3294 "Unable to allocate %d bytes for LUN list", 3295 list->alloc_len); 3296 break; 3297 } 3298 3299 sbuf_printf(sb, "<ctlportlist>\n"); 3300 3301 mtx_lock(&softc->ctl_lock); 3302 STAILQ_FOREACH(port, &softc->port_list, links) { 3303 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3304 (uintmax_t)port->targ_port); 3305 3306 /* 3307 * Bail out as soon as we see that we've overfilled 3308 * the buffer. 3309 */ 3310 if (retval != 0) 3311 break; 3312 3313 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3314 "</frontend_type>\n", port->frontend->name); 3315 if (retval != 0) 3316 break; 3317 3318 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3319 port->port_type); 3320 if (retval != 0) 3321 break; 3322 3323 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3324 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3325 if (retval != 0) 3326 break; 3327 3328 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3329 port->port_name); 3330 if (retval != 0) 3331 break; 3332 3333 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3334 port->physical_port); 3335 if (retval != 0) 3336 break; 3337 3338 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3339 port->virtual_port); 3340 if (retval != 0) 3341 break; 3342 3343 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3344 (uintmax_t)port->wwnn); 3345 if (retval != 0) 3346 break; 3347 3348 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3349 (uintmax_t)port->wwpn); 3350 if (retval != 0) 3351 break; 3352 3353 if (port->port_info != NULL) { 3354 retval = port->port_info(port->onoff_arg, sb); 3355 if (retval != 0) 3356 break; 3357 } 3358 STAILQ_FOREACH(opt, &port->options, links) { 3359 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3360 opt->name, opt->value, opt->name); 3361 if (retval != 0) 3362 break; 3363 } 3364 3365 retval = sbuf_printf(sb, "</targ_port>\n"); 3366 if (retval != 0) 3367 break; 3368 } 3369 mtx_unlock(&softc->ctl_lock); 3370 3371 if ((retval != 0) 3372 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3373 retval = 0; 3374 sbuf_delete(sb); 3375 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3376 snprintf(list->error_str, sizeof(list->error_str), 3377 "Out of space, %d bytes is too small", 3378 list->alloc_len); 3379 break; 3380 } 3381 3382 sbuf_finish(sb); 3383 3384 retval = copyout(sbuf_data(sb), list->lun_xml, 3385 sbuf_len(sb) + 1); 3386 3387 list->fill_len = sbuf_len(sb) + 1; 3388 list->status = CTL_LUN_LIST_OK; 3389 sbuf_delete(sb); 3390 break; 3391 } 3392 default: { 3393 /* XXX KDM should we fix this? */ 3394#if 0 3395 struct ctl_backend_driver *backend; 3396 unsigned int type; 3397 int found; 3398 3399 found = 0; 3400 3401 /* 3402 * We encode the backend type as the ioctl type for backend 3403 * ioctls. So parse it out here, and then search for a 3404 * backend of this type. 3405 */ 3406 type = _IOC_TYPE(cmd); 3407 3408 STAILQ_FOREACH(backend, &softc->be_list, links) { 3409 if (backend->type == type) { 3410 found = 1; 3411 break; 3412 } 3413 } 3414 if (found == 0) { 3415 printf("ctl: unknown ioctl command %#lx or backend " 3416 "%d\n", cmd, type); 3417 retval = EINVAL; 3418 break; 3419 } 3420 retval = backend->ioctl(dev, cmd, addr, flag, td); 3421#endif 3422 retval = ENOTTY; 3423 break; 3424 } 3425 } 3426 return (retval); 3427} 3428 3429uint32_t 3430ctl_get_initindex(struct ctl_nexus *nexus) 3431{ 3432 if (nexus->targ_port < CTL_MAX_PORTS) 3433 return (nexus->initid.id + 3434 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3435 else 3436 return (nexus->initid.id + 3437 ((nexus->targ_port - CTL_MAX_PORTS) * 3438 CTL_MAX_INIT_PER_PORT)); 3439} 3440 3441uint32_t 3442ctl_get_resindex(struct ctl_nexus *nexus) 3443{ 3444 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3445} 3446 3447uint32_t 3448ctl_port_idx(int port_num) 3449{ 3450 if (port_num < CTL_MAX_PORTS) 3451 return(port_num); 3452 else 3453 return(port_num - CTL_MAX_PORTS); 3454} 3455 3456static uint32_t 3457ctl_map_lun(int port_num, uint32_t lun_id) 3458{ 3459 struct ctl_port *port; 3460 3461 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3462 if (port == NULL) 3463 return (UINT32_MAX); 3464 if (port->lun_map == NULL) 3465 return (lun_id); 3466 return (port->lun_map(port->targ_lun_arg, lun_id)); 3467} 3468 3469static uint32_t 3470ctl_map_lun_back(int port_num, uint32_t lun_id) 3471{ 3472 struct ctl_port *port; 3473 uint32_t i; 3474 3475 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3476 if (port->lun_map == NULL) 3477 return (lun_id); 3478 for (i = 0; i < CTL_MAX_LUNS; i++) { 3479 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3480 return (i); 3481 } 3482 return (UINT32_MAX); 3483} 3484 3485/* 3486 * Note: This only works for bitmask sizes that are at least 32 bits, and 3487 * that are a power of 2. 3488 */ 3489int 3490ctl_ffz(uint32_t *mask, uint32_t size) 3491{ 3492 uint32_t num_chunks, num_pieces; 3493 int i, j; 3494 3495 num_chunks = (size >> 5); 3496 if (num_chunks == 0) 3497 num_chunks++; 3498 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3499 3500 for (i = 0; i < num_chunks; i++) { 3501 for (j = 0; j < num_pieces; j++) { 3502 if ((mask[i] & (1 << j)) == 0) 3503 return ((i << 5) + j); 3504 } 3505 } 3506 3507 return (-1); 3508} 3509 3510int 3511ctl_set_mask(uint32_t *mask, uint32_t bit) 3512{ 3513 uint32_t chunk, piece; 3514 3515 chunk = bit >> 5; 3516 piece = bit % (sizeof(uint32_t) * 8); 3517 3518 if ((mask[chunk] & (1 << piece)) != 0) 3519 return (-1); 3520 else 3521 mask[chunk] |= (1 << piece); 3522 3523 return (0); 3524} 3525 3526int 3527ctl_clear_mask(uint32_t *mask, uint32_t bit) 3528{ 3529 uint32_t chunk, piece; 3530 3531 chunk = bit >> 5; 3532 piece = bit % (sizeof(uint32_t) * 8); 3533 3534 if ((mask[chunk] & (1 << piece)) == 0) 3535 return (-1); 3536 else 3537 mask[chunk] &= ~(1 << piece); 3538 3539 return (0); 3540} 3541 3542int 3543ctl_is_set(uint32_t *mask, uint32_t bit) 3544{ 3545 uint32_t chunk, piece; 3546 3547 chunk = bit >> 5; 3548 piece = bit % (sizeof(uint32_t) * 8); 3549 3550 if ((mask[chunk] & (1 << piece)) == 0) 3551 return (0); 3552 else 3553 return (1); 3554} 3555 3556#ifdef unused 3557/* 3558 * The bus, target and lun are optional, they can be filled in later. 3559 * can_wait is used to determine whether we can wait on the malloc or not. 3560 */ 3561union ctl_io* 3562ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3563 uint32_t targ_lun, int can_wait) 3564{ 3565 union ctl_io *io; 3566 3567 if (can_wait) 3568 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3569 else 3570 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3571 3572 if (io != NULL) { 3573 io->io_hdr.io_type = io_type; 3574 io->io_hdr.targ_port = targ_port; 3575 /* 3576 * XXX KDM this needs to change/go away. We need to move 3577 * to a preallocated pool of ctl_scsiio structures. 3578 */ 3579 io->io_hdr.nexus.targ_target.id = targ_target; 3580 io->io_hdr.nexus.targ_lun = targ_lun; 3581 } 3582 3583 return (io); 3584} 3585 3586void 3587ctl_kfree_io(union ctl_io *io) 3588{ 3589 free(io, M_CTL); 3590} 3591#endif /* unused */ 3592 3593/* 3594 * ctl_softc, pool_type, total_ctl_io are passed in. 3595 * npool is passed out. 3596 */ 3597int 3598ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3599 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3600{ 3601 uint32_t i; 3602 union ctl_io *cur_io, *next_io; 3603 struct ctl_io_pool *pool; 3604 int retval; 3605 3606 retval = 0; 3607 3608 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3609 M_NOWAIT | M_ZERO); 3610 if (pool == NULL) { 3611 retval = ENOMEM; 3612 goto bailout; 3613 } 3614 3615 pool->type = pool_type; 3616 pool->ctl_softc = ctl_softc; 3617 3618 mtx_lock(&ctl_softc->pool_lock); 3619 pool->id = ctl_softc->cur_pool_id++; 3620 mtx_unlock(&ctl_softc->pool_lock); 3621 3622 pool->flags = CTL_POOL_FLAG_NONE; 3623 pool->refcount = 1; /* Reference for validity. */ 3624 STAILQ_INIT(&pool->free_queue); 3625 3626 /* 3627 * XXX KDM other options here: 3628 * - allocate a page at a time 3629 * - allocate one big chunk of memory. 3630 * Page allocation might work well, but would take a little more 3631 * tracking. 3632 */ 3633 for (i = 0; i < total_ctl_io; i++) { 3634 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3635 M_NOWAIT); 3636 if (cur_io == NULL) { 3637 retval = ENOMEM; 3638 break; 3639 } 3640 cur_io->io_hdr.pool = pool; 3641 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3642 pool->total_ctl_io++; 3643 pool->free_ctl_io++; 3644 } 3645 3646 if (retval != 0) { 3647 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3648 cur_io != NULL; cur_io = next_io) { 3649 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3650 links); 3651 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3652 ctl_io_hdr, links); 3653 free(cur_io, M_CTLIO); 3654 } 3655 3656 free(pool, M_CTL); 3657 goto bailout; 3658 } 3659 mtx_lock(&ctl_softc->pool_lock); 3660 ctl_softc->num_pools++; 3661 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3662 /* 3663 * Increment our usage count if this is an external consumer, so we 3664 * can't get unloaded until the external consumer (most likely a 3665 * FETD) unloads and frees his pool. 3666 * 3667 * XXX KDM will this increment the caller's module use count, or 3668 * mine? 3669 */ 3670#if 0 3671 if ((pool_type != CTL_POOL_EMERGENCY) 3672 && (pool_type != CTL_POOL_INTERNAL) 3673 && (pool_type != CTL_POOL_4OTHERSC)) 3674 MOD_INC_USE_COUNT; 3675#endif 3676 3677 mtx_unlock(&ctl_softc->pool_lock); 3678 3679 *npool = pool; 3680 3681bailout: 3682 3683 return (retval); 3684} 3685 3686static int 3687ctl_pool_acquire(struct ctl_io_pool *pool) 3688{ 3689 3690 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3691 3692 if (pool->flags & CTL_POOL_FLAG_INVALID) 3693 return (EINVAL); 3694 3695 pool->refcount++; 3696 3697 return (0); 3698} 3699 3700static void 3701ctl_pool_release(struct ctl_io_pool *pool) 3702{ 3703 struct ctl_softc *ctl_softc = pool->ctl_softc; 3704 union ctl_io *io; 3705 3706 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3707 3708 if (--pool->refcount != 0) 3709 return; 3710 3711 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3712 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3713 links); 3714 free(io, M_CTLIO); 3715 } 3716 3717 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3718 ctl_softc->num_pools--; 3719 3720 /* 3721 * XXX KDM will this decrement the caller's usage count or mine? 3722 */ 3723#if 0 3724 if ((pool->type != CTL_POOL_EMERGENCY) 3725 && (pool->type != CTL_POOL_INTERNAL) 3726 && (pool->type != CTL_POOL_4OTHERSC)) 3727 MOD_DEC_USE_COUNT; 3728#endif 3729 3730 free(pool, M_CTL); 3731} 3732 3733void 3734ctl_pool_free(struct ctl_io_pool *pool) 3735{ 3736 struct ctl_softc *ctl_softc; 3737 3738 if (pool == NULL) 3739 return; 3740 3741 ctl_softc = pool->ctl_softc; 3742 mtx_lock(&ctl_softc->pool_lock); 3743 pool->flags |= CTL_POOL_FLAG_INVALID; 3744 ctl_pool_release(pool); 3745 mtx_unlock(&ctl_softc->pool_lock); 3746} 3747 3748/* 3749 * This routine does not block (except for spinlocks of course). 3750 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3751 * possible. 3752 */ 3753union ctl_io * 3754ctl_alloc_io(void *pool_ref) 3755{ 3756 union ctl_io *io; 3757 struct ctl_softc *ctl_softc; 3758 struct ctl_io_pool *pool, *npool; 3759 struct ctl_io_pool *emergency_pool; 3760 3761 pool = (struct ctl_io_pool *)pool_ref; 3762 3763 if (pool == NULL) { 3764 printf("%s: pool is NULL\n", __func__); 3765 return (NULL); 3766 } 3767 3768 emergency_pool = NULL; 3769 3770 ctl_softc = pool->ctl_softc; 3771 3772 mtx_lock(&ctl_softc->pool_lock); 3773 /* 3774 * First, try to get the io structure from the user's pool. 3775 */ 3776 if (ctl_pool_acquire(pool) == 0) { 3777 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3778 if (io != NULL) { 3779 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3780 pool->total_allocated++; 3781 pool->free_ctl_io--; 3782 mtx_unlock(&ctl_softc->pool_lock); 3783 return (io); 3784 } else 3785 ctl_pool_release(pool); 3786 } 3787 /* 3788 * If he doesn't have any io structures left, search for an 3789 * emergency pool and grab one from there. 3790 */ 3791 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3792 if (npool->type != CTL_POOL_EMERGENCY) 3793 continue; 3794 3795 if (ctl_pool_acquire(npool) != 0) 3796 continue; 3797 3798 emergency_pool = npool; 3799 3800 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3801 if (io != NULL) { 3802 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3803 npool->total_allocated++; 3804 npool->free_ctl_io--; 3805 mtx_unlock(&ctl_softc->pool_lock); 3806 return (io); 3807 } else 3808 ctl_pool_release(npool); 3809 } 3810 3811 /* Drop the spinlock before we malloc */ 3812 mtx_unlock(&ctl_softc->pool_lock); 3813 3814 /* 3815 * The emergency pool (if it exists) didn't have one, so try an 3816 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3817 */ 3818 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3819 if (io != NULL) { 3820 /* 3821 * If the emergency pool exists but is empty, add this 3822 * ctl_io to its list when it gets freed. 3823 */ 3824 if (emergency_pool != NULL) { 3825 mtx_lock(&ctl_softc->pool_lock); 3826 if (ctl_pool_acquire(emergency_pool) == 0) { 3827 io->io_hdr.pool = emergency_pool; 3828 emergency_pool->total_ctl_io++; 3829 /* 3830 * Need to bump this, otherwise 3831 * total_allocated and total_freed won't 3832 * match when we no longer have anything 3833 * outstanding. 3834 */ 3835 emergency_pool->total_allocated++; 3836 } 3837 mtx_unlock(&ctl_softc->pool_lock); 3838 } else 3839 io->io_hdr.pool = NULL; 3840 } 3841 3842 return (io); 3843} 3844 3845void 3846ctl_free_io(union ctl_io *io) 3847{ 3848 if (io == NULL) 3849 return; 3850 3851 /* 3852 * If this ctl_io has a pool, return it to that pool. 3853 */ 3854 if (io->io_hdr.pool != NULL) { 3855 struct ctl_io_pool *pool; 3856 3857 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3858 mtx_lock(&pool->ctl_softc->pool_lock); 3859 io->io_hdr.io_type = 0xff; 3860 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3861 pool->total_freed++; 3862 pool->free_ctl_io++; 3863 ctl_pool_release(pool); 3864 mtx_unlock(&pool->ctl_softc->pool_lock); 3865 } else { 3866 /* 3867 * Otherwise, just free it. We probably malloced it and 3868 * the emergency pool wasn't available. 3869 */ 3870 free(io, M_CTLIO); 3871 } 3872 3873} 3874 3875void 3876ctl_zero_io(union ctl_io *io) 3877{ 3878 void *pool_ref; 3879 3880 if (io == NULL) 3881 return; 3882 3883 /* 3884 * May need to preserve linked list pointers at some point too. 3885 */ 3886 pool_ref = io->io_hdr.pool; 3887 3888 memset(io, 0, sizeof(*io)); 3889 3890 io->io_hdr.pool = pool_ref; 3891} 3892 3893/* 3894 * This routine is currently used for internal copies of ctl_ios that need 3895 * to persist for some reason after we've already returned status to the 3896 * FETD. (Thus the flag set.) 3897 * 3898 * XXX XXX 3899 * Note that this makes a blind copy of all fields in the ctl_io, except 3900 * for the pool reference. This includes any memory that has been 3901 * allocated! That memory will no longer be valid after done has been 3902 * called, so this would be VERY DANGEROUS for command that actually does 3903 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3904 * start and stop commands, which don't transfer any data, so this is not a 3905 * problem. If it is used for anything else, the caller would also need to 3906 * allocate data buffer space and this routine would need to be modified to 3907 * copy the data buffer(s) as well. 3908 */ 3909void 3910ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3911{ 3912 void *pool_ref; 3913 3914 if ((src == NULL) 3915 || (dest == NULL)) 3916 return; 3917 3918 /* 3919 * May need to preserve linked list pointers at some point too. 3920 */ 3921 pool_ref = dest->io_hdr.pool; 3922 3923 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3924 3925 dest->io_hdr.pool = pool_ref; 3926 /* 3927 * We need to know that this is an internal copy, and doesn't need 3928 * to get passed back to the FETD that allocated it. 3929 */ 3930 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3931} 3932 3933#ifdef NEEDTOPORT 3934static void 3935ctl_update_power_subpage(struct copan_power_subpage *page) 3936{ 3937 int num_luns, num_partitions, config_type; 3938 struct ctl_softc *softc; 3939 cs_BOOL_t aor_present, shelf_50pct_power; 3940 cs_raidset_personality_t rs_type; 3941 int max_active_luns; 3942 3943 softc = control_softc; 3944 3945 /* subtract out the processor LUN */ 3946 num_luns = softc->num_luns - 1; 3947 /* 3948 * Default to 7 LUNs active, which was the only number we allowed 3949 * in the past. 3950 */ 3951 max_active_luns = 7; 3952 3953 num_partitions = config_GetRsPartitionInfo(); 3954 config_type = config_GetConfigType(); 3955 shelf_50pct_power = config_GetShelfPowerMode(); 3956 aor_present = config_IsAorRsPresent(); 3957 3958 rs_type = ddb_GetRsRaidType(1); 3959 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3960 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3961 EPRINT(0, "Unsupported RS type %d!", rs_type); 3962 } 3963 3964 3965 page->total_luns = num_luns; 3966 3967 switch (config_type) { 3968 case 40: 3969 /* 3970 * In a 40 drive configuration, it doesn't matter what DC 3971 * cards we have, whether we have AOR enabled or not, 3972 * partitioning or not, or what type of RAIDset we have. 3973 * In that scenario, we can power up every LUN we present 3974 * to the user. 3975 */ 3976 max_active_luns = num_luns; 3977 3978 break; 3979 case 64: 3980 if (shelf_50pct_power == CS_FALSE) { 3981 /* 25% power */ 3982 if (aor_present == CS_TRUE) { 3983 if (rs_type == 3984 CS_RAIDSET_PERSONALITY_RAID5) { 3985 max_active_luns = 7; 3986 } else if (rs_type == 3987 CS_RAIDSET_PERSONALITY_RAID1){ 3988 max_active_luns = 14; 3989 } else { 3990 /* XXX KDM now what?? */ 3991 } 3992 } else { 3993 if (rs_type == 3994 CS_RAIDSET_PERSONALITY_RAID5) { 3995 max_active_luns = 8; 3996 } else if (rs_type == 3997 CS_RAIDSET_PERSONALITY_RAID1){ 3998 max_active_luns = 16; 3999 } else { 4000 /* XXX KDM now what?? */ 4001 } 4002 } 4003 } else { 4004 /* 50% power */ 4005 /* 4006 * With 50% power in a 64 drive configuration, we 4007 * can power all LUNs we present. 4008 */ 4009 max_active_luns = num_luns; 4010 } 4011 break; 4012 case 112: 4013 if (shelf_50pct_power == CS_FALSE) { 4014 /* 25% power */ 4015 if (aor_present == CS_TRUE) { 4016 if (rs_type == 4017 CS_RAIDSET_PERSONALITY_RAID5) { 4018 max_active_luns = 7; 4019 } else if (rs_type == 4020 CS_RAIDSET_PERSONALITY_RAID1){ 4021 max_active_luns = 14; 4022 } else { 4023 /* XXX KDM now what?? */ 4024 } 4025 } else { 4026 if (rs_type == 4027 CS_RAIDSET_PERSONALITY_RAID5) { 4028 max_active_luns = 8; 4029 } else if (rs_type == 4030 CS_RAIDSET_PERSONALITY_RAID1){ 4031 max_active_luns = 16; 4032 } else { 4033 /* XXX KDM now what?? */ 4034 } 4035 } 4036 } else { 4037 /* 50% power */ 4038 if (aor_present == CS_TRUE) { 4039 if (rs_type == 4040 CS_RAIDSET_PERSONALITY_RAID5) { 4041 max_active_luns = 14; 4042 } else if (rs_type == 4043 CS_RAIDSET_PERSONALITY_RAID1){ 4044 /* 4045 * We're assuming here that disk 4046 * caching is enabled, and so we're 4047 * able to power up half of each 4048 * LUN, and cache all writes. 4049 */ 4050 max_active_luns = num_luns; 4051 } else { 4052 /* XXX KDM now what?? */ 4053 } 4054 } else { 4055 if (rs_type == 4056 CS_RAIDSET_PERSONALITY_RAID5) { 4057 max_active_luns = 15; 4058 } else if (rs_type == 4059 CS_RAIDSET_PERSONALITY_RAID1){ 4060 max_active_luns = 30; 4061 } else { 4062 /* XXX KDM now what?? */ 4063 } 4064 } 4065 } 4066 break; 4067 default: 4068 /* 4069 * In this case, we have an unknown configuration, so we 4070 * just use the default from above. 4071 */ 4072 break; 4073 } 4074 4075 page->max_active_luns = max_active_luns; 4076#if 0 4077 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4078 page->total_luns, page->max_active_luns); 4079#endif 4080} 4081#endif /* NEEDTOPORT */ 4082 4083/* 4084 * This routine could be used in the future to load default and/or saved 4085 * mode page parameters for a particuar lun. 4086 */ 4087static int 4088ctl_init_page_index(struct ctl_lun *lun) 4089{ 4090 int i; 4091 struct ctl_page_index *page_index; 4092 struct ctl_softc *softc; 4093 4094 memcpy(&lun->mode_pages.index, page_index_template, 4095 sizeof(page_index_template)); 4096 4097 softc = lun->ctl_softc; 4098 4099 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4100 4101 page_index = &lun->mode_pages.index[i]; 4102 /* 4103 * If this is a disk-only mode page, there's no point in 4104 * setting it up. For some pages, we have to have some 4105 * basic information about the disk in order to calculate the 4106 * mode page data. 4107 */ 4108 if ((lun->be_lun->lun_type != T_DIRECT) 4109 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4110 continue; 4111 4112 switch (page_index->page_code & SMPH_PC_MASK) { 4113 case SMS_FORMAT_DEVICE_PAGE: { 4114 struct scsi_format_page *format_page; 4115 4116 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4117 panic("subpage is incorrect!"); 4118 4119 /* 4120 * Sectors per track are set above. Bytes per 4121 * sector need to be set here on a per-LUN basis. 4122 */ 4123 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4124 &format_page_default, 4125 sizeof(format_page_default)); 4126 memcpy(&lun->mode_pages.format_page[ 4127 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4128 sizeof(format_page_changeable)); 4129 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4130 &format_page_default, 4131 sizeof(format_page_default)); 4132 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4133 &format_page_default, 4134 sizeof(format_page_default)); 4135 4136 format_page = &lun->mode_pages.format_page[ 4137 CTL_PAGE_CURRENT]; 4138 scsi_ulto2b(lun->be_lun->blocksize, 4139 format_page->bytes_per_sector); 4140 4141 format_page = &lun->mode_pages.format_page[ 4142 CTL_PAGE_DEFAULT]; 4143 scsi_ulto2b(lun->be_lun->blocksize, 4144 format_page->bytes_per_sector); 4145 4146 format_page = &lun->mode_pages.format_page[ 4147 CTL_PAGE_SAVED]; 4148 scsi_ulto2b(lun->be_lun->blocksize, 4149 format_page->bytes_per_sector); 4150 4151 page_index->page_data = 4152 (uint8_t *)lun->mode_pages.format_page; 4153 break; 4154 } 4155 case SMS_RIGID_DISK_PAGE: { 4156 struct scsi_rigid_disk_page *rigid_disk_page; 4157 uint32_t sectors_per_cylinder; 4158 uint64_t cylinders; 4159#ifndef __XSCALE__ 4160 int shift; 4161#endif /* !__XSCALE__ */ 4162 4163 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4164 panic("invalid subpage value %d", 4165 page_index->subpage); 4166 4167 /* 4168 * Rotation rate and sectors per track are set 4169 * above. We calculate the cylinders here based on 4170 * capacity. Due to the number of heads and 4171 * sectors per track we're using, smaller arrays 4172 * may turn out to have 0 cylinders. Linux and 4173 * FreeBSD don't pay attention to these mode pages 4174 * to figure out capacity, but Solaris does. It 4175 * seems to deal with 0 cylinders just fine, and 4176 * works out a fake geometry based on the capacity. 4177 */ 4178 memcpy(&lun->mode_pages.rigid_disk_page[ 4179 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4180 sizeof(rigid_disk_page_default)); 4181 memcpy(&lun->mode_pages.rigid_disk_page[ 4182 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4183 sizeof(rigid_disk_page_changeable)); 4184 memcpy(&lun->mode_pages.rigid_disk_page[ 4185 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4186 sizeof(rigid_disk_page_default)); 4187 memcpy(&lun->mode_pages.rigid_disk_page[ 4188 CTL_PAGE_SAVED], &rigid_disk_page_default, 4189 sizeof(rigid_disk_page_default)); 4190 4191 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4192 CTL_DEFAULT_HEADS; 4193 4194 /* 4195 * The divide method here will be more accurate, 4196 * probably, but results in floating point being 4197 * used in the kernel on i386 (__udivdi3()). On the 4198 * XScale, though, __udivdi3() is implemented in 4199 * software. 4200 * 4201 * The shift method for cylinder calculation is 4202 * accurate if sectors_per_cylinder is a power of 4203 * 2. Otherwise it might be slightly off -- you 4204 * might have a bit of a truncation problem. 4205 */ 4206#ifdef __XSCALE__ 4207 cylinders = (lun->be_lun->maxlba + 1) / 4208 sectors_per_cylinder; 4209#else 4210 for (shift = 31; shift > 0; shift--) { 4211 if (sectors_per_cylinder & (1 << shift)) 4212 break; 4213 } 4214 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4215#endif 4216 4217 /* 4218 * We've basically got 3 bytes, or 24 bits for the 4219 * cylinder size in the mode page. If we're over, 4220 * just round down to 2^24. 4221 */ 4222 if (cylinders > 0xffffff) 4223 cylinders = 0xffffff; 4224 4225 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4226 CTL_PAGE_CURRENT]; 4227 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4228 4229 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4230 CTL_PAGE_DEFAULT]; 4231 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4232 4233 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4234 CTL_PAGE_SAVED]; 4235 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4236 4237 page_index->page_data = 4238 (uint8_t *)lun->mode_pages.rigid_disk_page; 4239 break; 4240 } 4241 case SMS_CACHING_PAGE: { 4242 4243 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4244 panic("invalid subpage value %d", 4245 page_index->subpage); 4246 /* 4247 * Defaults should be okay here, no calculations 4248 * needed. 4249 */ 4250 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4251 &caching_page_default, 4252 sizeof(caching_page_default)); 4253 memcpy(&lun->mode_pages.caching_page[ 4254 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4255 sizeof(caching_page_changeable)); 4256 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4257 &caching_page_default, 4258 sizeof(caching_page_default)); 4259 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4260 &caching_page_default, 4261 sizeof(caching_page_default)); 4262 page_index->page_data = 4263 (uint8_t *)lun->mode_pages.caching_page; 4264 break; 4265 } 4266 case SMS_CONTROL_MODE_PAGE: { 4267 4268 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4269 panic("invalid subpage value %d", 4270 page_index->subpage); 4271 4272 /* 4273 * Defaults should be okay here, no calculations 4274 * needed. 4275 */ 4276 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4277 &control_page_default, 4278 sizeof(control_page_default)); 4279 memcpy(&lun->mode_pages.control_page[ 4280 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4281 sizeof(control_page_changeable)); 4282 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4283 &control_page_default, 4284 sizeof(control_page_default)); 4285 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4286 &control_page_default, 4287 sizeof(control_page_default)); 4288 page_index->page_data = 4289 (uint8_t *)lun->mode_pages.control_page; 4290 break; 4291 4292 } 4293 case SMS_VENDOR_SPECIFIC_PAGE:{ 4294 switch (page_index->subpage) { 4295 case PWR_SUBPAGE_CODE: { 4296 struct copan_power_subpage *current_page, 4297 *saved_page; 4298 4299 memcpy(&lun->mode_pages.power_subpage[ 4300 CTL_PAGE_CURRENT], 4301 &power_page_default, 4302 sizeof(power_page_default)); 4303 memcpy(&lun->mode_pages.power_subpage[ 4304 CTL_PAGE_CHANGEABLE], 4305 &power_page_changeable, 4306 sizeof(power_page_changeable)); 4307 memcpy(&lun->mode_pages.power_subpage[ 4308 CTL_PAGE_DEFAULT], 4309 &power_page_default, 4310 sizeof(power_page_default)); 4311 memcpy(&lun->mode_pages.power_subpage[ 4312 CTL_PAGE_SAVED], 4313 &power_page_default, 4314 sizeof(power_page_default)); 4315 page_index->page_data = 4316 (uint8_t *)lun->mode_pages.power_subpage; 4317 4318 current_page = (struct copan_power_subpage *) 4319 (page_index->page_data + 4320 (page_index->page_len * 4321 CTL_PAGE_CURRENT)); 4322 saved_page = (struct copan_power_subpage *) 4323 (page_index->page_data + 4324 (page_index->page_len * 4325 CTL_PAGE_SAVED)); 4326 break; 4327 } 4328 case APS_SUBPAGE_CODE: { 4329 struct copan_aps_subpage *current_page, 4330 *saved_page; 4331 4332 // This gets set multiple times but 4333 // it should always be the same. It's 4334 // only done during init so who cares. 4335 index_to_aps_page = i; 4336 4337 memcpy(&lun->mode_pages.aps_subpage[ 4338 CTL_PAGE_CURRENT], 4339 &aps_page_default, 4340 sizeof(aps_page_default)); 4341 memcpy(&lun->mode_pages.aps_subpage[ 4342 CTL_PAGE_CHANGEABLE], 4343 &aps_page_changeable, 4344 sizeof(aps_page_changeable)); 4345 memcpy(&lun->mode_pages.aps_subpage[ 4346 CTL_PAGE_DEFAULT], 4347 &aps_page_default, 4348 sizeof(aps_page_default)); 4349 memcpy(&lun->mode_pages.aps_subpage[ 4350 CTL_PAGE_SAVED], 4351 &aps_page_default, 4352 sizeof(aps_page_default)); 4353 page_index->page_data = 4354 (uint8_t *)lun->mode_pages.aps_subpage; 4355 4356 current_page = (struct copan_aps_subpage *) 4357 (page_index->page_data + 4358 (page_index->page_len * 4359 CTL_PAGE_CURRENT)); 4360 saved_page = (struct copan_aps_subpage *) 4361 (page_index->page_data + 4362 (page_index->page_len * 4363 CTL_PAGE_SAVED)); 4364 break; 4365 } 4366 case DBGCNF_SUBPAGE_CODE: { 4367 struct copan_debugconf_subpage *current_page, 4368 *saved_page; 4369 4370 memcpy(&lun->mode_pages.debugconf_subpage[ 4371 CTL_PAGE_CURRENT], 4372 &debugconf_page_default, 4373 sizeof(debugconf_page_default)); 4374 memcpy(&lun->mode_pages.debugconf_subpage[ 4375 CTL_PAGE_CHANGEABLE], 4376 &debugconf_page_changeable, 4377 sizeof(debugconf_page_changeable)); 4378 memcpy(&lun->mode_pages.debugconf_subpage[ 4379 CTL_PAGE_DEFAULT], 4380 &debugconf_page_default, 4381 sizeof(debugconf_page_default)); 4382 memcpy(&lun->mode_pages.debugconf_subpage[ 4383 CTL_PAGE_SAVED], 4384 &debugconf_page_default, 4385 sizeof(debugconf_page_default)); 4386 page_index->page_data = 4387 (uint8_t *)lun->mode_pages.debugconf_subpage; 4388 4389 current_page = (struct copan_debugconf_subpage *) 4390 (page_index->page_data + 4391 (page_index->page_len * 4392 CTL_PAGE_CURRENT)); 4393 saved_page = (struct copan_debugconf_subpage *) 4394 (page_index->page_data + 4395 (page_index->page_len * 4396 CTL_PAGE_SAVED)); 4397 break; 4398 } 4399 default: 4400 panic("invalid subpage value %d", 4401 page_index->subpage); 4402 break; 4403 } 4404 break; 4405 } 4406 default: 4407 panic("invalid page value %d", 4408 page_index->page_code & SMPH_PC_MASK); 4409 break; 4410 } 4411 } 4412 4413 return (CTL_RETVAL_COMPLETE); 4414} 4415 4416/* 4417 * LUN allocation. 4418 * 4419 * Requirements: 4420 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4421 * wants us to allocate the LUN and he can block. 4422 * - ctl_softc is always set 4423 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4424 * 4425 * Returns 0 for success, non-zero (errno) for failure. 4426 */ 4427static int 4428ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4429 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4430{ 4431 struct ctl_lun *nlun, *lun; 4432 struct ctl_port *port; 4433 struct scsi_vpd_id_descriptor *desc; 4434 struct scsi_vpd_id_t10 *t10id; 4435 const char *eui, *naa, *scsiname, *vendor; 4436 int lun_number, i, lun_malloced; 4437 int devidlen, idlen1, idlen2 = 0, len; 4438 4439 if (be_lun == NULL) 4440 return (EINVAL); 4441 4442 /* 4443 * We currently only support Direct Access or Processor LUN types. 4444 */ 4445 switch (be_lun->lun_type) { 4446 case T_DIRECT: 4447 break; 4448 case T_PROCESSOR: 4449 break; 4450 case T_SEQUENTIAL: 4451 case T_CHANGER: 4452 default: 4453 be_lun->lun_config_status(be_lun->be_lun, 4454 CTL_LUN_CONFIG_FAILURE); 4455 break; 4456 } 4457 if (ctl_lun == NULL) { 4458 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4459 lun_malloced = 1; 4460 } else { 4461 lun_malloced = 0; 4462 lun = ctl_lun; 4463 } 4464 4465 memset(lun, 0, sizeof(*lun)); 4466 if (lun_malloced) 4467 lun->flags = CTL_LUN_MALLOCED; 4468 4469 /* Generate LUN ID. */ 4470 devidlen = max(CTL_DEVID_MIN_LEN, 4471 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4472 idlen1 = sizeof(*t10id) + devidlen; 4473 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4474 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4475 if (scsiname != NULL) { 4476 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4477 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4478 } 4479 eui = ctl_get_opt(&be_lun->options, "eui"); 4480 if (eui != NULL) { 4481 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4482 } 4483 naa = ctl_get_opt(&be_lun->options, "naa"); 4484 if (naa != NULL) { 4485 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4486 } 4487 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4488 M_CTL, M_WAITOK | M_ZERO); 4489 lun->lun_devid->len = len; 4490 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4491 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4492 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4493 desc->length = idlen1; 4494 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4495 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4496 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4497 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4498 } else { 4499 strncpy(t10id->vendor, vendor, 4500 min(sizeof(t10id->vendor), strlen(vendor))); 4501 } 4502 strncpy((char *)t10id->vendor_spec_id, 4503 (char *)be_lun->device_id, devidlen); 4504 if (scsiname != NULL) { 4505 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4506 desc->length); 4507 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4508 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4509 SVPD_ID_TYPE_SCSI_NAME; 4510 desc->length = idlen2; 4511 strlcpy(desc->identifier, scsiname, idlen2); 4512 } 4513 if (eui != NULL) { 4514 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4515 desc->length); 4516 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4517 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4518 SVPD_ID_TYPE_EUI64; 4519 desc->length = 8; 4520 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4521 } 4522 if (naa != NULL) { 4523 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4524 desc->length); 4525 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4526 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4527 SVPD_ID_TYPE_NAA; 4528 desc->length = 8; 4529 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4530 } 4531 4532 mtx_lock(&ctl_softc->ctl_lock); 4533 /* 4534 * See if the caller requested a particular LUN number. If so, see 4535 * if it is available. Otherwise, allocate the first available LUN. 4536 */ 4537 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4538 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4539 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4540 mtx_unlock(&ctl_softc->ctl_lock); 4541 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4542 printf("ctl: requested LUN ID %d is higher " 4543 "than CTL_MAX_LUNS - 1 (%d)\n", 4544 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4545 } else { 4546 /* 4547 * XXX KDM return an error, or just assign 4548 * another LUN ID in this case?? 4549 */ 4550 printf("ctl: requested LUN ID %d is already " 4551 "in use\n", be_lun->req_lun_id); 4552 } 4553 if (lun->flags & CTL_LUN_MALLOCED) 4554 free(lun, M_CTL); 4555 be_lun->lun_config_status(be_lun->be_lun, 4556 CTL_LUN_CONFIG_FAILURE); 4557 return (ENOSPC); 4558 } 4559 lun_number = be_lun->req_lun_id; 4560 } else { 4561 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4562 if (lun_number == -1) { 4563 mtx_unlock(&ctl_softc->ctl_lock); 4564 printf("ctl: can't allocate LUN on target %ju, out of " 4565 "LUNs\n", (uintmax_t)target_id.id); 4566 if (lun->flags & CTL_LUN_MALLOCED) 4567 free(lun, M_CTL); 4568 be_lun->lun_config_status(be_lun->be_lun, 4569 CTL_LUN_CONFIG_FAILURE); 4570 return (ENOSPC); 4571 } 4572 } 4573 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4574 4575 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4576 lun->target = target_id; 4577 lun->lun = lun_number; 4578 lun->be_lun = be_lun; 4579 /* 4580 * The processor LUN is always enabled. Disk LUNs come on line 4581 * disabled, and must be enabled by the backend. 4582 */ 4583 lun->flags |= CTL_LUN_DISABLED; 4584 lun->backend = be_lun->be; 4585 be_lun->ctl_lun = lun; 4586 be_lun->lun_id = lun_number; 4587 atomic_add_int(&be_lun->be->num_luns, 1); 4588 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4589 lun->flags |= CTL_LUN_STOPPED; 4590 4591 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4592 lun->flags |= CTL_LUN_INOPERABLE; 4593 4594 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4595 lun->flags |= CTL_LUN_PRIMARY_SC; 4596 4597 lun->ctl_softc = ctl_softc; 4598 TAILQ_INIT(&lun->ooa_queue); 4599 TAILQ_INIT(&lun->blocked_queue); 4600 STAILQ_INIT(&lun->error_list); 4601 4602 /* 4603 * Initialize the mode page index. 4604 */ 4605 ctl_init_page_index(lun); 4606 4607 /* 4608 * Set the poweron UA for all initiators on this LUN only. 4609 */ 4610 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4611 lun->pending_sense[i].ua_pending = CTL_UA_POWERON; 4612 4613 /* 4614 * Now, before we insert this lun on the lun list, set the lun 4615 * inventory changed UA for all other luns. 4616 */ 4617 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4618 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4619 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4620 } 4621 } 4622 4623 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4624 4625 ctl_softc->ctl_luns[lun_number] = lun; 4626 4627 ctl_softc->num_luns++; 4628 4629 /* Setup statistics gathering */ 4630 lun->stats.device_type = be_lun->lun_type; 4631 lun->stats.lun_number = lun_number; 4632 if (lun->stats.device_type == T_DIRECT) 4633 lun->stats.blocksize = be_lun->blocksize; 4634 else 4635 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4636 for (i = 0;i < CTL_MAX_PORTS;i++) 4637 lun->stats.ports[i].targ_port = i; 4638 4639 mtx_unlock(&ctl_softc->ctl_lock); 4640 4641 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4642 4643 /* 4644 * Run through each registered FETD and bring it online if it isn't 4645 * already. Enable the target ID if it hasn't been enabled, and 4646 * enable this particular LUN. 4647 */ 4648 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4649 int retval; 4650 4651 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4652 if (retval != 0) { 4653 printf("ctl_alloc_lun: FETD %s port %d returned error " 4654 "%d for lun_enable on target %ju lun %d\n", 4655 port->port_name, port->targ_port, retval, 4656 (uintmax_t)target_id.id, lun_number); 4657 } else 4658 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4659 } 4660 return (0); 4661} 4662 4663/* 4664 * Delete a LUN. 4665 * Assumptions: 4666 * - LUN has already been marked invalid and any pending I/O has been taken 4667 * care of. 4668 */ 4669static int 4670ctl_free_lun(struct ctl_lun *lun) 4671{ 4672 struct ctl_softc *softc; 4673#if 0 4674 struct ctl_port *port; 4675#endif 4676 struct ctl_lun *nlun; 4677 int i; 4678 4679 softc = lun->ctl_softc; 4680 4681 mtx_assert(&softc->ctl_lock, MA_OWNED); 4682 4683 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4684 4685 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4686 4687 softc->ctl_luns[lun->lun] = NULL; 4688 4689 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4690 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4691 4692 softc->num_luns--; 4693 4694 /* 4695 * XXX KDM this scheme only works for a single target/multiple LUN 4696 * setup. It needs to be revamped for a multiple target scheme. 4697 * 4698 * XXX KDM this results in port->lun_disable() getting called twice, 4699 * once when ctl_disable_lun() is called, and a second time here. 4700 * We really need to re-think the LUN disable semantics. There 4701 * should probably be several steps/levels to LUN removal: 4702 * - disable 4703 * - invalidate 4704 * - free 4705 * 4706 * Right now we only have a disable method when communicating to 4707 * the front end ports, at least for individual LUNs. 4708 */ 4709#if 0 4710 STAILQ_FOREACH(port, &softc->port_list, links) { 4711 int retval; 4712 4713 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4714 lun->lun); 4715 if (retval != 0) { 4716 printf("ctl_free_lun: FETD %s port %d returned error " 4717 "%d for lun_disable on target %ju lun %jd\n", 4718 port->port_name, port->targ_port, retval, 4719 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4720 } 4721 4722 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4723 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4724 4725 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4726 if (retval != 0) { 4727 printf("ctl_free_lun: FETD %s port %d " 4728 "returned error %d for targ_disable on " 4729 "target %ju\n", port->port_name, 4730 port->targ_port, retval, 4731 (uintmax_t)lun->target.id); 4732 } else 4733 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4734 4735 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4736 continue; 4737 4738#if 0 4739 port->port_offline(port->onoff_arg); 4740 port->status &= ~CTL_PORT_STATUS_ONLINE; 4741#endif 4742 } 4743 } 4744#endif 4745 4746 /* 4747 * Tell the backend to free resources, if this LUN has a backend. 4748 */ 4749 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4750 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4751 4752 mtx_destroy(&lun->lun_lock); 4753 free(lun->lun_devid, M_CTL); 4754 if (lun->flags & CTL_LUN_MALLOCED) 4755 free(lun, M_CTL); 4756 4757 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4758 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4759 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4760 } 4761 } 4762 4763 return (0); 4764} 4765 4766static void 4767ctl_create_lun(struct ctl_be_lun *be_lun) 4768{ 4769 struct ctl_softc *ctl_softc; 4770 4771 ctl_softc = control_softc; 4772 4773 /* 4774 * ctl_alloc_lun() should handle all potential failure cases. 4775 */ 4776 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4777} 4778 4779int 4780ctl_add_lun(struct ctl_be_lun *be_lun) 4781{ 4782 struct ctl_softc *ctl_softc = control_softc; 4783 4784 mtx_lock(&ctl_softc->ctl_lock); 4785 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4786 mtx_unlock(&ctl_softc->ctl_lock); 4787 wakeup(&ctl_softc->pending_lun_queue); 4788 4789 return (0); 4790} 4791 4792int 4793ctl_enable_lun(struct ctl_be_lun *be_lun) 4794{ 4795 struct ctl_softc *ctl_softc; 4796 struct ctl_port *port, *nport; 4797 struct ctl_lun *lun; 4798 int retval; 4799 4800 ctl_softc = control_softc; 4801 4802 lun = (struct ctl_lun *)be_lun->ctl_lun; 4803 4804 mtx_lock(&ctl_softc->ctl_lock); 4805 mtx_lock(&lun->lun_lock); 4806 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4807 /* 4808 * eh? Why did we get called if the LUN is already 4809 * enabled? 4810 */ 4811 mtx_unlock(&lun->lun_lock); 4812 mtx_unlock(&ctl_softc->ctl_lock); 4813 return (0); 4814 } 4815 lun->flags &= ~CTL_LUN_DISABLED; 4816 mtx_unlock(&lun->lun_lock); 4817 4818 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4819 nport = STAILQ_NEXT(port, links); 4820 4821 /* 4822 * Drop the lock while we call the FETD's enable routine. 4823 * This can lead to a callback into CTL (at least in the 4824 * case of the internal initiator frontend. 4825 */ 4826 mtx_unlock(&ctl_softc->ctl_lock); 4827 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4828 mtx_lock(&ctl_softc->ctl_lock); 4829 if (retval != 0) { 4830 printf("%s: FETD %s port %d returned error " 4831 "%d for lun_enable on target %ju lun %jd\n", 4832 __func__, port->port_name, port->targ_port, retval, 4833 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4834 } 4835#if 0 4836 else { 4837 /* NOTE: TODO: why does lun enable affect port status? */ 4838 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4839 } 4840#endif 4841 } 4842 4843 mtx_unlock(&ctl_softc->ctl_lock); 4844 4845 return (0); 4846} 4847 4848int 4849ctl_disable_lun(struct ctl_be_lun *be_lun) 4850{ 4851 struct ctl_softc *ctl_softc; 4852 struct ctl_port *port; 4853 struct ctl_lun *lun; 4854 int retval; 4855 4856 ctl_softc = control_softc; 4857 4858 lun = (struct ctl_lun *)be_lun->ctl_lun; 4859 4860 mtx_lock(&ctl_softc->ctl_lock); 4861 mtx_lock(&lun->lun_lock); 4862 if (lun->flags & CTL_LUN_DISABLED) { 4863 mtx_unlock(&lun->lun_lock); 4864 mtx_unlock(&ctl_softc->ctl_lock); 4865 return (0); 4866 } 4867 lun->flags |= CTL_LUN_DISABLED; 4868 mtx_unlock(&lun->lun_lock); 4869 4870 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4871 mtx_unlock(&ctl_softc->ctl_lock); 4872 /* 4873 * Drop the lock before we call the frontend's disable 4874 * routine, to avoid lock order reversals. 4875 * 4876 * XXX KDM what happens if the frontend list changes while 4877 * we're traversing it? It's unlikely, but should be handled. 4878 */ 4879 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4880 lun->lun); 4881 mtx_lock(&ctl_softc->ctl_lock); 4882 if (retval != 0) { 4883 printf("ctl_alloc_lun: FETD %s port %d returned error " 4884 "%d for lun_disable on target %ju lun %jd\n", 4885 port->port_name, port->targ_port, retval, 4886 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4887 } 4888 } 4889 4890 mtx_unlock(&ctl_softc->ctl_lock); 4891 4892 return (0); 4893} 4894 4895int 4896ctl_start_lun(struct ctl_be_lun *be_lun) 4897{ 4898 struct ctl_softc *ctl_softc; 4899 struct ctl_lun *lun; 4900 4901 ctl_softc = control_softc; 4902 4903 lun = (struct ctl_lun *)be_lun->ctl_lun; 4904 4905 mtx_lock(&lun->lun_lock); 4906 lun->flags &= ~CTL_LUN_STOPPED; 4907 mtx_unlock(&lun->lun_lock); 4908 4909 return (0); 4910} 4911 4912int 4913ctl_stop_lun(struct ctl_be_lun *be_lun) 4914{ 4915 struct ctl_softc *ctl_softc; 4916 struct ctl_lun *lun; 4917 4918 ctl_softc = control_softc; 4919 4920 lun = (struct ctl_lun *)be_lun->ctl_lun; 4921 4922 mtx_lock(&lun->lun_lock); 4923 lun->flags |= CTL_LUN_STOPPED; 4924 mtx_unlock(&lun->lun_lock); 4925 4926 return (0); 4927} 4928 4929int 4930ctl_lun_offline(struct ctl_be_lun *be_lun) 4931{ 4932 struct ctl_softc *ctl_softc; 4933 struct ctl_lun *lun; 4934 4935 ctl_softc = control_softc; 4936 4937 lun = (struct ctl_lun *)be_lun->ctl_lun; 4938 4939 mtx_lock(&lun->lun_lock); 4940 lun->flags |= CTL_LUN_OFFLINE; 4941 mtx_unlock(&lun->lun_lock); 4942 4943 return (0); 4944} 4945 4946int 4947ctl_lun_online(struct ctl_be_lun *be_lun) 4948{ 4949 struct ctl_softc *ctl_softc; 4950 struct ctl_lun *lun; 4951 4952 ctl_softc = control_softc; 4953 4954 lun = (struct ctl_lun *)be_lun->ctl_lun; 4955 4956 mtx_lock(&lun->lun_lock); 4957 lun->flags &= ~CTL_LUN_OFFLINE; 4958 mtx_unlock(&lun->lun_lock); 4959 4960 return (0); 4961} 4962 4963int 4964ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4965{ 4966 struct ctl_softc *ctl_softc; 4967 struct ctl_lun *lun; 4968 4969 ctl_softc = control_softc; 4970 4971 lun = (struct ctl_lun *)be_lun->ctl_lun; 4972 4973 mtx_lock(&lun->lun_lock); 4974 4975 /* 4976 * The LUN needs to be disabled before it can be marked invalid. 4977 */ 4978 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4979 mtx_unlock(&lun->lun_lock); 4980 return (-1); 4981 } 4982 /* 4983 * Mark the LUN invalid. 4984 */ 4985 lun->flags |= CTL_LUN_INVALID; 4986 4987 /* 4988 * If there is nothing in the OOA queue, go ahead and free the LUN. 4989 * If we have something in the OOA queue, we'll free it when the 4990 * last I/O completes. 4991 */ 4992 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4993 mtx_unlock(&lun->lun_lock); 4994 mtx_lock(&ctl_softc->ctl_lock); 4995 ctl_free_lun(lun); 4996 mtx_unlock(&ctl_softc->ctl_lock); 4997 } else 4998 mtx_unlock(&lun->lun_lock); 4999 5000 return (0); 5001} 5002 5003int 5004ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5005{ 5006 struct ctl_softc *ctl_softc; 5007 struct ctl_lun *lun; 5008 5009 ctl_softc = control_softc; 5010 lun = (struct ctl_lun *)be_lun->ctl_lun; 5011 5012 mtx_lock(&lun->lun_lock); 5013 lun->flags |= CTL_LUN_INOPERABLE; 5014 mtx_unlock(&lun->lun_lock); 5015 5016 return (0); 5017} 5018 5019int 5020ctl_lun_operable(struct ctl_be_lun *be_lun) 5021{ 5022 struct ctl_softc *ctl_softc; 5023 struct ctl_lun *lun; 5024 5025 ctl_softc = control_softc; 5026 lun = (struct ctl_lun *)be_lun->ctl_lun; 5027 5028 mtx_lock(&lun->lun_lock); 5029 lun->flags &= ~CTL_LUN_INOPERABLE; 5030 mtx_unlock(&lun->lun_lock); 5031 5032 return (0); 5033} 5034 5035int 5036ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5037 int lock) 5038{ 5039 struct ctl_softc *softc; 5040 struct ctl_lun *lun; 5041 struct copan_aps_subpage *current_sp; 5042 struct ctl_page_index *page_index; 5043 int i; 5044 5045 softc = control_softc; 5046 5047 mtx_lock(&softc->ctl_lock); 5048 5049 lun = (struct ctl_lun *)be_lun->ctl_lun; 5050 mtx_lock(&lun->lun_lock); 5051 5052 page_index = NULL; 5053 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5054 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5055 APS_PAGE_CODE) 5056 continue; 5057 5058 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5059 continue; 5060 page_index = &lun->mode_pages.index[i]; 5061 } 5062 5063 if (page_index == NULL) { 5064 mtx_unlock(&lun->lun_lock); 5065 mtx_unlock(&softc->ctl_lock); 5066 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5067 (uintmax_t)lun->lun); 5068 return (1); 5069 } 5070#if 0 5071 if ((softc->aps_locked_lun != 0) 5072 && (softc->aps_locked_lun != lun->lun)) { 5073 printf("%s: attempt to lock LUN %llu when %llu is already " 5074 "locked\n"); 5075 mtx_unlock(&lun->lun_lock); 5076 mtx_unlock(&softc->ctl_lock); 5077 return (1); 5078 } 5079#endif 5080 5081 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5082 (page_index->page_len * CTL_PAGE_CURRENT)); 5083 5084 if (lock != 0) { 5085 current_sp->lock_active = APS_LOCK_ACTIVE; 5086 softc->aps_locked_lun = lun->lun; 5087 } else { 5088 current_sp->lock_active = 0; 5089 softc->aps_locked_lun = 0; 5090 } 5091 5092 5093 /* 5094 * If we're in HA mode, try to send the lock message to the other 5095 * side. 5096 */ 5097 if (ctl_is_single == 0) { 5098 int isc_retval; 5099 union ctl_ha_msg lock_msg; 5100 5101 lock_msg.hdr.nexus = *nexus; 5102 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5103 if (lock != 0) 5104 lock_msg.aps.lock_flag = 1; 5105 else 5106 lock_msg.aps.lock_flag = 0; 5107 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5108 sizeof(lock_msg), 0); 5109 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5110 printf("%s: APS (lock=%d) error returned from " 5111 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5112 mtx_unlock(&lun->lun_lock); 5113 mtx_unlock(&softc->ctl_lock); 5114 return (1); 5115 } 5116 } 5117 5118 mtx_unlock(&lun->lun_lock); 5119 mtx_unlock(&softc->ctl_lock); 5120 5121 return (0); 5122} 5123 5124void 5125ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5126{ 5127 struct ctl_lun *lun; 5128 struct ctl_softc *softc; 5129 int i; 5130 5131 softc = control_softc; 5132 5133 lun = (struct ctl_lun *)be_lun->ctl_lun; 5134 5135 mtx_lock(&lun->lun_lock); 5136 5137 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5138 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED; 5139 5140 mtx_unlock(&lun->lun_lock); 5141} 5142 5143/* 5144 * Backend "memory move is complete" callback for requests that never 5145 * make it down to say RAIDCore's configuration code. 5146 */ 5147int 5148ctl_config_move_done(union ctl_io *io) 5149{ 5150 int retval; 5151 5152 retval = CTL_RETVAL_COMPLETE; 5153 5154 5155 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5156 /* 5157 * XXX KDM this shouldn't happen, but what if it does? 5158 */ 5159 if (io->io_hdr.io_type != CTL_IO_SCSI) 5160 panic("I/O type isn't CTL_IO_SCSI!"); 5161 5162 if ((io->io_hdr.port_status == 0) 5163 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5164 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5165 io->io_hdr.status = CTL_SUCCESS; 5166 else if ((io->io_hdr.port_status != 0) 5167 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5168 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5169 /* 5170 * For hardware error sense keys, the sense key 5171 * specific value is defined to be a retry count, 5172 * but we use it to pass back an internal FETD 5173 * error code. XXX KDM Hopefully the FETD is only 5174 * using 16 bits for an error code, since that's 5175 * all the space we have in the sks field. 5176 */ 5177 ctl_set_internal_failure(&io->scsiio, 5178 /*sks_valid*/ 1, 5179 /*retry_count*/ 5180 io->io_hdr.port_status); 5181 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5182 free(io->scsiio.kern_data_ptr, M_CTL); 5183 ctl_done(io); 5184 goto bailout; 5185 } 5186 5187 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5188 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5189 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5190 /* 5191 * XXX KDM just assuming a single pointer here, and not a 5192 * S/G list. If we start using S/G lists for config data, 5193 * we'll need to know how to clean them up here as well. 5194 */ 5195 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5196 free(io->scsiio.kern_data_ptr, M_CTL); 5197 /* Hopefully the user has already set the status... */ 5198 ctl_done(io); 5199 } else { 5200 /* 5201 * XXX KDM now we need to continue data movement. Some 5202 * options: 5203 * - call ctl_scsiio() again? We don't do this for data 5204 * writes, because for those at least we know ahead of 5205 * time where the write will go and how long it is. For 5206 * config writes, though, that information is largely 5207 * contained within the write itself, thus we need to 5208 * parse out the data again. 5209 * 5210 * - Call some other function once the data is in? 5211 */ 5212 5213 /* 5214 * XXX KDM call ctl_scsiio() again for now, and check flag 5215 * bits to see whether we're allocated or not. 5216 */ 5217 retval = ctl_scsiio(&io->scsiio); 5218 } 5219bailout: 5220 return (retval); 5221} 5222 5223/* 5224 * This gets called by a backend driver when it is done with a 5225 * data_submit method. 5226 */ 5227void 5228ctl_data_submit_done(union ctl_io *io) 5229{ 5230 /* 5231 * If the IO_CONT flag is set, we need to call the supplied 5232 * function to continue processing the I/O, instead of completing 5233 * the I/O just yet. 5234 * 5235 * If there is an error, though, we don't want to keep processing. 5236 * Instead, just send status back to the initiator. 5237 */ 5238 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5239 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5240 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5241 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5242 io->scsiio.io_cont(io); 5243 return; 5244 } 5245 ctl_done(io); 5246} 5247 5248/* 5249 * This gets called by a backend driver when it is done with a 5250 * configuration write. 5251 */ 5252void 5253ctl_config_write_done(union ctl_io *io) 5254{ 5255 /* 5256 * If the IO_CONT flag is set, we need to call the supplied 5257 * function to continue processing the I/O, instead of completing 5258 * the I/O just yet. 5259 * 5260 * If there is an error, though, we don't want to keep processing. 5261 * Instead, just send status back to the initiator. 5262 */ 5263 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5264 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5265 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5266 io->scsiio.io_cont(io); 5267 return; 5268 } 5269 /* 5270 * Since a configuration write can be done for commands that actually 5271 * have data allocated, like write buffer, and commands that have 5272 * no data, like start/stop unit, we need to check here. 5273 */ 5274 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5275 free(io->scsiio.kern_data_ptr, M_CTL); 5276 ctl_done(io); 5277} 5278 5279/* 5280 * SCSI release command. 5281 */ 5282int 5283ctl_scsi_release(struct ctl_scsiio *ctsio) 5284{ 5285 int length, longid, thirdparty_id, resv_id; 5286 struct ctl_softc *ctl_softc; 5287 struct ctl_lun *lun; 5288 5289 length = 0; 5290 resv_id = 0; 5291 5292 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5293 5294 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5295 ctl_softc = control_softc; 5296 5297 switch (ctsio->cdb[0]) { 5298 case RELEASE_10: { 5299 struct scsi_release_10 *cdb; 5300 5301 cdb = (struct scsi_release_10 *)ctsio->cdb; 5302 5303 if (cdb->byte2 & SR10_LONGID) 5304 longid = 1; 5305 else 5306 thirdparty_id = cdb->thirdparty_id; 5307 5308 resv_id = cdb->resv_id; 5309 length = scsi_2btoul(cdb->length); 5310 break; 5311 } 5312 } 5313 5314 5315 /* 5316 * XXX KDM right now, we only support LUN reservation. We don't 5317 * support 3rd party reservations, or extent reservations, which 5318 * might actually need the parameter list. If we've gotten this 5319 * far, we've got a LUN reservation. Anything else got kicked out 5320 * above. So, according to SPC, ignore the length. 5321 */ 5322 length = 0; 5323 5324 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5325 && (length > 0)) { 5326 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5327 ctsio->kern_data_len = length; 5328 ctsio->kern_total_len = length; 5329 ctsio->kern_data_resid = 0; 5330 ctsio->kern_rel_offset = 0; 5331 ctsio->kern_sg_entries = 0; 5332 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5333 ctsio->be_move_done = ctl_config_move_done; 5334 ctl_datamove((union ctl_io *)ctsio); 5335 5336 return (CTL_RETVAL_COMPLETE); 5337 } 5338 5339 if (length > 0) 5340 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5341 5342 mtx_lock(&lun->lun_lock); 5343 5344 /* 5345 * According to SPC, it is not an error for an intiator to attempt 5346 * to release a reservation on a LUN that isn't reserved, or that 5347 * is reserved by another initiator. The reservation can only be 5348 * released, though, by the initiator who made it or by one of 5349 * several reset type events. 5350 */ 5351 if (lun->flags & CTL_LUN_RESERVED) { 5352 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5353 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5354 && (ctsio->io_hdr.nexus.targ_target.id == 5355 lun->rsv_nexus.targ_target.id)) { 5356 lun->flags &= ~CTL_LUN_RESERVED; 5357 } 5358 } 5359 5360 mtx_unlock(&lun->lun_lock); 5361 5362 ctsio->scsi_status = SCSI_STATUS_OK; 5363 ctsio->io_hdr.status = CTL_SUCCESS; 5364 5365 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5366 free(ctsio->kern_data_ptr, M_CTL); 5367 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5368 } 5369 5370 ctl_done((union ctl_io *)ctsio); 5371 return (CTL_RETVAL_COMPLETE); 5372} 5373 5374int 5375ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5376{ 5377 int extent, thirdparty, longid; 5378 int resv_id, length; 5379 uint64_t thirdparty_id; 5380 struct ctl_softc *ctl_softc; 5381 struct ctl_lun *lun; 5382 5383 extent = 0; 5384 thirdparty = 0; 5385 longid = 0; 5386 resv_id = 0; 5387 length = 0; 5388 thirdparty_id = 0; 5389 5390 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5391 5392 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5393 ctl_softc = control_softc; 5394 5395 switch (ctsio->cdb[0]) { 5396 case RESERVE_10: { 5397 struct scsi_reserve_10 *cdb; 5398 5399 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5400 5401 if (cdb->byte2 & SR10_LONGID) 5402 longid = 1; 5403 else 5404 thirdparty_id = cdb->thirdparty_id; 5405 5406 resv_id = cdb->resv_id; 5407 length = scsi_2btoul(cdb->length); 5408 break; 5409 } 5410 } 5411 5412 /* 5413 * XXX KDM right now, we only support LUN reservation. We don't 5414 * support 3rd party reservations, or extent reservations, which 5415 * might actually need the parameter list. If we've gotten this 5416 * far, we've got a LUN reservation. Anything else got kicked out 5417 * above. So, according to SPC, ignore the length. 5418 */ 5419 length = 0; 5420 5421 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5422 && (length > 0)) { 5423 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5424 ctsio->kern_data_len = length; 5425 ctsio->kern_total_len = length; 5426 ctsio->kern_data_resid = 0; 5427 ctsio->kern_rel_offset = 0; 5428 ctsio->kern_sg_entries = 0; 5429 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5430 ctsio->be_move_done = ctl_config_move_done; 5431 ctl_datamove((union ctl_io *)ctsio); 5432 5433 return (CTL_RETVAL_COMPLETE); 5434 } 5435 5436 if (length > 0) 5437 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5438 5439 mtx_lock(&lun->lun_lock); 5440 if (lun->flags & CTL_LUN_RESERVED) { 5441 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5442 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5443 || (ctsio->io_hdr.nexus.targ_target.id != 5444 lun->rsv_nexus.targ_target.id)) { 5445 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5446 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5447 goto bailout; 5448 } 5449 } 5450 5451 lun->flags |= CTL_LUN_RESERVED; 5452 lun->rsv_nexus = ctsio->io_hdr.nexus; 5453 5454 ctsio->scsi_status = SCSI_STATUS_OK; 5455 ctsio->io_hdr.status = CTL_SUCCESS; 5456 5457bailout: 5458 mtx_unlock(&lun->lun_lock); 5459 5460 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5461 free(ctsio->kern_data_ptr, M_CTL); 5462 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5463 } 5464 5465 ctl_done((union ctl_io *)ctsio); 5466 return (CTL_RETVAL_COMPLETE); 5467} 5468 5469int 5470ctl_start_stop(struct ctl_scsiio *ctsio) 5471{ 5472 struct scsi_start_stop_unit *cdb; 5473 struct ctl_lun *lun; 5474 struct ctl_softc *ctl_softc; 5475 int retval; 5476 5477 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5478 5479 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5480 ctl_softc = control_softc; 5481 retval = 0; 5482 5483 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5484 5485 /* 5486 * XXX KDM 5487 * We don't support the immediate bit on a stop unit. In order to 5488 * do that, we would need to code up a way to know that a stop is 5489 * pending, and hold off any new commands until it completes, one 5490 * way or another. Then we could accept or reject those commands 5491 * depending on its status. We would almost need to do the reverse 5492 * of what we do below for an immediate start -- return the copy of 5493 * the ctl_io to the FETD with status to send to the host (and to 5494 * free the copy!) and then free the original I/O once the stop 5495 * actually completes. That way, the OOA queue mechanism can work 5496 * to block commands that shouldn't proceed. Another alternative 5497 * would be to put the copy in the queue in place of the original, 5498 * and return the original back to the caller. That could be 5499 * slightly safer.. 5500 */ 5501 if ((cdb->byte2 & SSS_IMMED) 5502 && ((cdb->how & SSS_START) == 0)) { 5503 ctl_set_invalid_field(ctsio, 5504 /*sks_valid*/ 1, 5505 /*command*/ 1, 5506 /*field*/ 1, 5507 /*bit_valid*/ 1, 5508 /*bit*/ 0); 5509 ctl_done((union ctl_io *)ctsio); 5510 return (CTL_RETVAL_COMPLETE); 5511 } 5512 5513 if ((lun->flags & CTL_LUN_PR_RESERVED) 5514 && ((cdb->how & SSS_START)==0)) { 5515 uint32_t residx; 5516 5517 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5518 if (!lun->per_res[residx].registered 5519 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5520 5521 ctl_set_reservation_conflict(ctsio); 5522 ctl_done((union ctl_io *)ctsio); 5523 return (CTL_RETVAL_COMPLETE); 5524 } 5525 } 5526 5527 /* 5528 * If there is no backend on this device, we can't start or stop 5529 * it. In theory we shouldn't get any start/stop commands in the 5530 * first place at this level if the LUN doesn't have a backend. 5531 * That should get stopped by the command decode code. 5532 */ 5533 if (lun->backend == NULL) { 5534 ctl_set_invalid_opcode(ctsio); 5535 ctl_done((union ctl_io *)ctsio); 5536 return (CTL_RETVAL_COMPLETE); 5537 } 5538 5539 /* 5540 * XXX KDM Copan-specific offline behavior. 5541 * Figure out a reasonable way to port this? 5542 */ 5543#ifdef NEEDTOPORT 5544 mtx_lock(&lun->lun_lock); 5545 5546 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5547 && (lun->flags & CTL_LUN_OFFLINE)) { 5548 /* 5549 * If the LUN is offline, and the on/offline bit isn't set, 5550 * reject the start or stop. Otherwise, let it through. 5551 */ 5552 mtx_unlock(&lun->lun_lock); 5553 ctl_set_lun_not_ready(ctsio); 5554 ctl_done((union ctl_io *)ctsio); 5555 } else { 5556 mtx_unlock(&lun->lun_lock); 5557#endif /* NEEDTOPORT */ 5558 /* 5559 * This could be a start or a stop when we're online, 5560 * or a stop/offline or start/online. A start or stop when 5561 * we're offline is covered in the case above. 5562 */ 5563 /* 5564 * In the non-immediate case, we send the request to 5565 * the backend and return status to the user when 5566 * it is done. 5567 * 5568 * In the immediate case, we allocate a new ctl_io 5569 * to hold a copy of the request, and send that to 5570 * the backend. We then set good status on the 5571 * user's request and return it immediately. 5572 */ 5573 if (cdb->byte2 & SSS_IMMED) { 5574 union ctl_io *new_io; 5575 5576 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5577 if (new_io == NULL) { 5578 ctl_set_busy(ctsio); 5579 ctl_done((union ctl_io *)ctsio); 5580 } else { 5581 ctl_copy_io((union ctl_io *)ctsio, 5582 new_io); 5583 retval = lun->backend->config_write(new_io); 5584 ctl_set_success(ctsio); 5585 ctl_done((union ctl_io *)ctsio); 5586 } 5587 } else { 5588 retval = lun->backend->config_write( 5589 (union ctl_io *)ctsio); 5590 } 5591#ifdef NEEDTOPORT 5592 } 5593#endif 5594 return (retval); 5595} 5596 5597/* 5598 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5599 * we don't really do anything with the LBA and length fields if the user 5600 * passes them in. Instead we'll just flush out the cache for the entire 5601 * LUN. 5602 */ 5603int 5604ctl_sync_cache(struct ctl_scsiio *ctsio) 5605{ 5606 struct ctl_lun *lun; 5607 struct ctl_softc *ctl_softc; 5608 uint64_t starting_lba; 5609 uint32_t block_count; 5610 int retval; 5611 5612 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5613 5614 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5615 ctl_softc = control_softc; 5616 retval = 0; 5617 5618 switch (ctsio->cdb[0]) { 5619 case SYNCHRONIZE_CACHE: { 5620 struct scsi_sync_cache *cdb; 5621 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5622 5623 starting_lba = scsi_4btoul(cdb->begin_lba); 5624 block_count = scsi_2btoul(cdb->lb_count); 5625 break; 5626 } 5627 case SYNCHRONIZE_CACHE_16: { 5628 struct scsi_sync_cache_16 *cdb; 5629 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5630 5631 starting_lba = scsi_8btou64(cdb->begin_lba); 5632 block_count = scsi_4btoul(cdb->lb_count); 5633 break; 5634 } 5635 default: 5636 ctl_set_invalid_opcode(ctsio); 5637 ctl_done((union ctl_io *)ctsio); 5638 goto bailout; 5639 break; /* NOTREACHED */ 5640 } 5641 5642 /* 5643 * We check the LBA and length, but don't do anything with them. 5644 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5645 * get flushed. This check will just help satisfy anyone who wants 5646 * to see an error for an out of range LBA. 5647 */ 5648 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5649 ctl_set_lba_out_of_range(ctsio); 5650 ctl_done((union ctl_io *)ctsio); 5651 goto bailout; 5652 } 5653 5654 /* 5655 * If this LUN has no backend, we can't flush the cache anyway. 5656 */ 5657 if (lun->backend == NULL) { 5658 ctl_set_invalid_opcode(ctsio); 5659 ctl_done((union ctl_io *)ctsio); 5660 goto bailout; 5661 } 5662 5663 /* 5664 * Check to see whether we're configured to send the SYNCHRONIZE 5665 * CACHE command directly to the back end. 5666 */ 5667 mtx_lock(&lun->lun_lock); 5668 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5669 && (++(lun->sync_count) >= lun->sync_interval)) { 5670 lun->sync_count = 0; 5671 mtx_unlock(&lun->lun_lock); 5672 retval = lun->backend->config_write((union ctl_io *)ctsio); 5673 } else { 5674 mtx_unlock(&lun->lun_lock); 5675 ctl_set_success(ctsio); 5676 ctl_done((union ctl_io *)ctsio); 5677 } 5678 5679bailout: 5680 5681 return (retval); 5682} 5683 5684int 5685ctl_format(struct ctl_scsiio *ctsio) 5686{ 5687 struct scsi_format *cdb; 5688 struct ctl_lun *lun; 5689 struct ctl_softc *ctl_softc; 5690 int length, defect_list_len; 5691 5692 CTL_DEBUG_PRINT(("ctl_format\n")); 5693 5694 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5695 ctl_softc = control_softc; 5696 5697 cdb = (struct scsi_format *)ctsio->cdb; 5698 5699 length = 0; 5700 if (cdb->byte2 & SF_FMTDATA) { 5701 if (cdb->byte2 & SF_LONGLIST) 5702 length = sizeof(struct scsi_format_header_long); 5703 else 5704 length = sizeof(struct scsi_format_header_short); 5705 } 5706 5707 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5708 && (length > 0)) { 5709 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5710 ctsio->kern_data_len = length; 5711 ctsio->kern_total_len = length; 5712 ctsio->kern_data_resid = 0; 5713 ctsio->kern_rel_offset = 0; 5714 ctsio->kern_sg_entries = 0; 5715 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5716 ctsio->be_move_done = ctl_config_move_done; 5717 ctl_datamove((union ctl_io *)ctsio); 5718 5719 return (CTL_RETVAL_COMPLETE); 5720 } 5721 5722 defect_list_len = 0; 5723 5724 if (cdb->byte2 & SF_FMTDATA) { 5725 if (cdb->byte2 & SF_LONGLIST) { 5726 struct scsi_format_header_long *header; 5727 5728 header = (struct scsi_format_header_long *) 5729 ctsio->kern_data_ptr; 5730 5731 defect_list_len = scsi_4btoul(header->defect_list_len); 5732 if (defect_list_len != 0) { 5733 ctl_set_invalid_field(ctsio, 5734 /*sks_valid*/ 1, 5735 /*command*/ 0, 5736 /*field*/ 2, 5737 /*bit_valid*/ 0, 5738 /*bit*/ 0); 5739 goto bailout; 5740 } 5741 } else { 5742 struct scsi_format_header_short *header; 5743 5744 header = (struct scsi_format_header_short *) 5745 ctsio->kern_data_ptr; 5746 5747 defect_list_len = scsi_2btoul(header->defect_list_len); 5748 if (defect_list_len != 0) { 5749 ctl_set_invalid_field(ctsio, 5750 /*sks_valid*/ 1, 5751 /*command*/ 0, 5752 /*field*/ 2, 5753 /*bit_valid*/ 0, 5754 /*bit*/ 0); 5755 goto bailout; 5756 } 5757 } 5758 } 5759 5760 /* 5761 * The format command will clear out the "Medium format corrupted" 5762 * status if set by the configuration code. That status is really 5763 * just a way to notify the host that we have lost the media, and 5764 * get them to issue a command that will basically make them think 5765 * they're blowing away the media. 5766 */ 5767 mtx_lock(&lun->lun_lock); 5768 lun->flags &= ~CTL_LUN_INOPERABLE; 5769 mtx_unlock(&lun->lun_lock); 5770 5771 ctsio->scsi_status = SCSI_STATUS_OK; 5772 ctsio->io_hdr.status = CTL_SUCCESS; 5773bailout: 5774 5775 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5776 free(ctsio->kern_data_ptr, M_CTL); 5777 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5778 } 5779 5780 ctl_done((union ctl_io *)ctsio); 5781 return (CTL_RETVAL_COMPLETE); 5782} 5783 5784int 5785ctl_read_buffer(struct ctl_scsiio *ctsio) 5786{ 5787 struct scsi_read_buffer *cdb; 5788 struct ctl_lun *lun; 5789 int buffer_offset, len; 5790 static uint8_t descr[4]; 5791 static uint8_t echo_descr[4] = { 0 }; 5792 5793 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5794 5795 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5796 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5797 5798 if (lun->flags & CTL_LUN_PR_RESERVED) { 5799 uint32_t residx; 5800 5801 /* 5802 * XXX KDM need a lock here. 5803 */ 5804 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5805 if ((lun->res_type == SPR_TYPE_EX_AC 5806 && residx != lun->pr_res_idx) 5807 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5808 || lun->res_type == SPR_TYPE_EX_AC_AR) 5809 && !lun->per_res[residx].registered)) { 5810 ctl_set_reservation_conflict(ctsio); 5811 ctl_done((union ctl_io *)ctsio); 5812 return (CTL_RETVAL_COMPLETE); 5813 } 5814 } 5815 5816 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5817 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5818 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5819 ctl_set_invalid_field(ctsio, 5820 /*sks_valid*/ 1, 5821 /*command*/ 1, 5822 /*field*/ 1, 5823 /*bit_valid*/ 1, 5824 /*bit*/ 4); 5825 ctl_done((union ctl_io *)ctsio); 5826 return (CTL_RETVAL_COMPLETE); 5827 } 5828 5829 len = scsi_3btoul(cdb->length); 5830 buffer_offset = scsi_3btoul(cdb->offset); 5831 5832 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5833 ctl_set_invalid_field(ctsio, 5834 /*sks_valid*/ 1, 5835 /*command*/ 1, 5836 /*field*/ 6, 5837 /*bit_valid*/ 0, 5838 /*bit*/ 0); 5839 ctl_done((union ctl_io *)ctsio); 5840 return (CTL_RETVAL_COMPLETE); 5841 } 5842 5843 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5844 descr[0] = 0; 5845 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5846 ctsio->kern_data_ptr = descr; 5847 len = min(len, sizeof(descr)); 5848 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5849 ctsio->kern_data_ptr = echo_descr; 5850 len = min(len, sizeof(echo_descr)); 5851 } else 5852 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5853 ctsio->kern_data_len = len; 5854 ctsio->kern_total_len = len; 5855 ctsio->kern_data_resid = 0; 5856 ctsio->kern_rel_offset = 0; 5857 ctsio->kern_sg_entries = 0; 5858 ctsio->be_move_done = ctl_config_move_done; 5859 ctl_datamove((union ctl_io *)ctsio); 5860 5861 return (CTL_RETVAL_COMPLETE); 5862} 5863 5864int 5865ctl_write_buffer(struct ctl_scsiio *ctsio) 5866{ 5867 struct scsi_write_buffer *cdb; 5868 struct ctl_lun *lun; 5869 int buffer_offset, len; 5870 5871 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5872 5873 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5874 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5875 5876 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5877 ctl_set_invalid_field(ctsio, 5878 /*sks_valid*/ 1, 5879 /*command*/ 1, 5880 /*field*/ 1, 5881 /*bit_valid*/ 1, 5882 /*bit*/ 4); 5883 ctl_done((union ctl_io *)ctsio); 5884 return (CTL_RETVAL_COMPLETE); 5885 } 5886 5887 len = scsi_3btoul(cdb->length); 5888 buffer_offset = scsi_3btoul(cdb->offset); 5889 5890 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5891 ctl_set_invalid_field(ctsio, 5892 /*sks_valid*/ 1, 5893 /*command*/ 1, 5894 /*field*/ 6, 5895 /*bit_valid*/ 0, 5896 /*bit*/ 0); 5897 ctl_done((union ctl_io *)ctsio); 5898 return (CTL_RETVAL_COMPLETE); 5899 } 5900 5901 /* 5902 * If we've got a kernel request that hasn't been malloced yet, 5903 * malloc it and tell the caller the data buffer is here. 5904 */ 5905 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5906 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5907 ctsio->kern_data_len = len; 5908 ctsio->kern_total_len = len; 5909 ctsio->kern_data_resid = 0; 5910 ctsio->kern_rel_offset = 0; 5911 ctsio->kern_sg_entries = 0; 5912 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5913 ctsio->be_move_done = ctl_config_move_done; 5914 ctl_datamove((union ctl_io *)ctsio); 5915 5916 return (CTL_RETVAL_COMPLETE); 5917 } 5918 5919 ctl_done((union ctl_io *)ctsio); 5920 5921 return (CTL_RETVAL_COMPLETE); 5922} 5923 5924int 5925ctl_write_same(struct ctl_scsiio *ctsio) 5926{ 5927 struct ctl_lun *lun; 5928 struct ctl_lba_len_flags *lbalen; 5929 uint64_t lba; 5930 uint32_t num_blocks; 5931 int len, retval; 5932 uint8_t byte2; 5933 5934 retval = CTL_RETVAL_COMPLETE; 5935 5936 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5937 5938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5939 5940 switch (ctsio->cdb[0]) { 5941 case WRITE_SAME_10: { 5942 struct scsi_write_same_10 *cdb; 5943 5944 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5945 5946 lba = scsi_4btoul(cdb->addr); 5947 num_blocks = scsi_2btoul(cdb->length); 5948 byte2 = cdb->byte2; 5949 break; 5950 } 5951 case WRITE_SAME_16: { 5952 struct scsi_write_same_16 *cdb; 5953 5954 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5955 5956 lba = scsi_8btou64(cdb->addr); 5957 num_blocks = scsi_4btoul(cdb->length); 5958 byte2 = cdb->byte2; 5959 break; 5960 } 5961 default: 5962 /* 5963 * We got a command we don't support. This shouldn't 5964 * happen, commands should be filtered out above us. 5965 */ 5966 ctl_set_invalid_opcode(ctsio); 5967 ctl_done((union ctl_io *)ctsio); 5968 5969 return (CTL_RETVAL_COMPLETE); 5970 break; /* NOTREACHED */ 5971 } 5972 5973 /* 5974 * The first check is to make sure we're in bounds, the second 5975 * check is to catch wrap-around problems. If the lba + num blocks 5976 * is less than the lba, then we've wrapped around and the block 5977 * range is invalid anyway. 5978 */ 5979 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5980 || ((lba + num_blocks) < lba)) { 5981 ctl_set_lba_out_of_range(ctsio); 5982 ctl_done((union ctl_io *)ctsio); 5983 return (CTL_RETVAL_COMPLETE); 5984 } 5985 5986 /* Zero number of blocks means "to the last logical block" */ 5987 if (num_blocks == 0) { 5988 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5989 ctl_set_invalid_field(ctsio, 5990 /*sks_valid*/ 0, 5991 /*command*/ 1, 5992 /*field*/ 0, 5993 /*bit_valid*/ 0, 5994 /*bit*/ 0); 5995 ctl_done((union ctl_io *)ctsio); 5996 return (CTL_RETVAL_COMPLETE); 5997 } 5998 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5999 } 6000 6001 len = lun->be_lun->blocksize; 6002 6003 /* 6004 * If we've got a kernel request that hasn't been malloced yet, 6005 * malloc it and tell the caller the data buffer is here. 6006 */ 6007 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6008 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6009 ctsio->kern_data_len = len; 6010 ctsio->kern_total_len = len; 6011 ctsio->kern_data_resid = 0; 6012 ctsio->kern_rel_offset = 0; 6013 ctsio->kern_sg_entries = 0; 6014 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6015 ctsio->be_move_done = ctl_config_move_done; 6016 ctl_datamove((union ctl_io *)ctsio); 6017 6018 return (CTL_RETVAL_COMPLETE); 6019 } 6020 6021 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6022 lbalen->lba = lba; 6023 lbalen->len = num_blocks; 6024 lbalen->flags = byte2; 6025 retval = lun->backend->config_write((union ctl_io *)ctsio); 6026 6027 return (retval); 6028} 6029 6030int 6031ctl_unmap(struct ctl_scsiio *ctsio) 6032{ 6033 struct ctl_lun *lun; 6034 struct scsi_unmap *cdb; 6035 struct ctl_ptr_len_flags *ptrlen; 6036 struct scsi_unmap_header *hdr; 6037 struct scsi_unmap_desc *buf, *end; 6038 uint64_t lba; 6039 uint32_t num_blocks; 6040 int len, retval; 6041 uint8_t byte2; 6042 6043 retval = CTL_RETVAL_COMPLETE; 6044 6045 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6046 6047 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6048 cdb = (struct scsi_unmap *)ctsio->cdb; 6049 6050 len = scsi_2btoul(cdb->length); 6051 byte2 = cdb->byte2; 6052 6053 /* 6054 * If we've got a kernel request that hasn't been malloced yet, 6055 * malloc it and tell the caller the data buffer is here. 6056 */ 6057 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6058 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6059 ctsio->kern_data_len = len; 6060 ctsio->kern_total_len = len; 6061 ctsio->kern_data_resid = 0; 6062 ctsio->kern_rel_offset = 0; 6063 ctsio->kern_sg_entries = 0; 6064 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6065 ctsio->be_move_done = ctl_config_move_done; 6066 ctl_datamove((union ctl_io *)ctsio); 6067 6068 return (CTL_RETVAL_COMPLETE); 6069 } 6070 6071 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6072 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6073 if (len < sizeof (*hdr) || 6074 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6075 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6076 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6077 ctl_set_invalid_field(ctsio, 6078 /*sks_valid*/ 0, 6079 /*command*/ 0, 6080 /*field*/ 0, 6081 /*bit_valid*/ 0, 6082 /*bit*/ 0); 6083 ctl_done((union ctl_io *)ctsio); 6084 return (CTL_RETVAL_COMPLETE); 6085 } 6086 len = scsi_2btoul(hdr->desc_length); 6087 buf = (struct scsi_unmap_desc *)(hdr + 1); 6088 end = buf + len / sizeof(*buf); 6089 6090 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6091 ptrlen->ptr = (void *)buf; 6092 ptrlen->len = len; 6093 ptrlen->flags = byte2; 6094 6095 for (; buf < end; buf++) { 6096 lba = scsi_8btou64(buf->lba); 6097 num_blocks = scsi_4btoul(buf->length); 6098 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6099 || ((lba + num_blocks) < lba)) { 6100 ctl_set_lba_out_of_range(ctsio); 6101 ctl_done((union ctl_io *)ctsio); 6102 return (CTL_RETVAL_COMPLETE); 6103 } 6104 } 6105 6106 retval = lun->backend->config_write((union ctl_io *)ctsio); 6107 6108 return (retval); 6109} 6110 6111/* 6112 * Note that this function currently doesn't actually do anything inside 6113 * CTL to enforce things if the DQue bit is turned on. 6114 * 6115 * Also note that this function can't be used in the default case, because 6116 * the DQue bit isn't set in the changeable mask for the control mode page 6117 * anyway. This is just here as an example for how to implement a page 6118 * handler, and a placeholder in case we want to allow the user to turn 6119 * tagged queueing on and off. 6120 * 6121 * The D_SENSE bit handling is functional, however, and will turn 6122 * descriptor sense on and off for a given LUN. 6123 */ 6124int 6125ctl_control_page_handler(struct ctl_scsiio *ctsio, 6126 struct ctl_page_index *page_index, uint8_t *page_ptr) 6127{ 6128 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6129 struct ctl_lun *lun; 6130 struct ctl_softc *softc; 6131 int set_ua; 6132 uint32_t initidx; 6133 6134 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6135 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6136 set_ua = 0; 6137 6138 user_cp = (struct scsi_control_page *)page_ptr; 6139 current_cp = (struct scsi_control_page *) 6140 (page_index->page_data + (page_index->page_len * 6141 CTL_PAGE_CURRENT)); 6142 saved_cp = (struct scsi_control_page *) 6143 (page_index->page_data + (page_index->page_len * 6144 CTL_PAGE_SAVED)); 6145 6146 softc = control_softc; 6147 6148 mtx_lock(&lun->lun_lock); 6149 if (((current_cp->rlec & SCP_DSENSE) == 0) 6150 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6151 /* 6152 * Descriptor sense is currently turned off and the user 6153 * wants to turn it on. 6154 */ 6155 current_cp->rlec |= SCP_DSENSE; 6156 saved_cp->rlec |= SCP_DSENSE; 6157 lun->flags |= CTL_LUN_SENSE_DESC; 6158 set_ua = 1; 6159 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6160 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6161 /* 6162 * Descriptor sense is currently turned on, and the user 6163 * wants to turn it off. 6164 */ 6165 current_cp->rlec &= ~SCP_DSENSE; 6166 saved_cp->rlec &= ~SCP_DSENSE; 6167 lun->flags &= ~CTL_LUN_SENSE_DESC; 6168 set_ua = 1; 6169 } 6170 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6171 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6172#ifdef NEEDTOPORT 6173 csevent_log(CSC_CTL | CSC_SHELF_SW | 6174 CTL_UNTAG_TO_UNTAG, 6175 csevent_LogType_Trace, 6176 csevent_Severity_Information, 6177 csevent_AlertLevel_Green, 6178 csevent_FRU_Firmware, 6179 csevent_FRU_Unknown, 6180 "Received untagged to untagged transition"); 6181#endif /* NEEDTOPORT */ 6182 } else { 6183#ifdef NEEDTOPORT 6184 csevent_log(CSC_CTL | CSC_SHELF_SW | 6185 CTL_UNTAG_TO_TAG, 6186 csevent_LogType_ConfigChange, 6187 csevent_Severity_Information, 6188 csevent_AlertLevel_Green, 6189 csevent_FRU_Firmware, 6190 csevent_FRU_Unknown, 6191 "Received untagged to tagged " 6192 "queueing transition"); 6193#endif /* NEEDTOPORT */ 6194 6195 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6196 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6197 set_ua = 1; 6198 } 6199 } else { 6200 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6201#ifdef NEEDTOPORT 6202 csevent_log(CSC_CTL | CSC_SHELF_SW | 6203 CTL_TAG_TO_UNTAG, 6204 csevent_LogType_ConfigChange, 6205 csevent_Severity_Warning, 6206 csevent_AlertLevel_Yellow, 6207 csevent_FRU_Firmware, 6208 csevent_FRU_Unknown, 6209 "Received tagged queueing to untagged " 6210 "transition"); 6211#endif /* NEEDTOPORT */ 6212 6213 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6214 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6215 set_ua = 1; 6216 } else { 6217#ifdef NEEDTOPORT 6218 csevent_log(CSC_CTL | CSC_SHELF_SW | 6219 CTL_TAG_TO_TAG, 6220 csevent_LogType_Trace, 6221 csevent_Severity_Information, 6222 csevent_AlertLevel_Green, 6223 csevent_FRU_Firmware, 6224 csevent_FRU_Unknown, 6225 "Received tagged queueing to tagged " 6226 "queueing transition"); 6227#endif /* NEEDTOPORT */ 6228 } 6229 } 6230 if (set_ua != 0) { 6231 int i; 6232 /* 6233 * Let other initiators know that the mode 6234 * parameters for this LUN have changed. 6235 */ 6236 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6237 if (i == initidx) 6238 continue; 6239 6240 lun->pending_sense[i].ua_pending |= 6241 CTL_UA_MODE_CHANGE; 6242 } 6243 } 6244 mtx_unlock(&lun->lun_lock); 6245 6246 return (0); 6247} 6248 6249int 6250ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6251 struct ctl_page_index *page_index, uint8_t *page_ptr) 6252{ 6253 return (0); 6254} 6255 6256int 6257ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6258 struct ctl_page_index *page_index, int pc) 6259{ 6260 struct copan_power_subpage *page; 6261 6262 page = (struct copan_power_subpage *)page_index->page_data + 6263 (page_index->page_len * pc); 6264 6265 switch (pc) { 6266 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6267 /* 6268 * We don't update the changable bits for this page. 6269 */ 6270 break; 6271 case SMS_PAGE_CTRL_CURRENT >> 6: 6272 case SMS_PAGE_CTRL_DEFAULT >> 6: 6273 case SMS_PAGE_CTRL_SAVED >> 6: 6274#ifdef NEEDTOPORT 6275 ctl_update_power_subpage(page); 6276#endif 6277 break; 6278 default: 6279#ifdef NEEDTOPORT 6280 EPRINT(0, "Invalid PC %d!!", pc); 6281#endif 6282 break; 6283 } 6284 return (0); 6285} 6286 6287 6288int 6289ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6290 struct ctl_page_index *page_index, uint8_t *page_ptr) 6291{ 6292 struct copan_aps_subpage *user_sp; 6293 struct copan_aps_subpage *current_sp; 6294 union ctl_modepage_info *modepage_info; 6295 struct ctl_softc *softc; 6296 struct ctl_lun *lun; 6297 int retval; 6298 6299 retval = CTL_RETVAL_COMPLETE; 6300 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6301 (page_index->page_len * CTL_PAGE_CURRENT)); 6302 softc = control_softc; 6303 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6304 6305 user_sp = (struct copan_aps_subpage *)page_ptr; 6306 6307 modepage_info = (union ctl_modepage_info *) 6308 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6309 6310 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6311 modepage_info->header.subpage = page_index->subpage; 6312 modepage_info->aps.lock_active = user_sp->lock_active; 6313 6314 mtx_lock(&softc->ctl_lock); 6315 6316 /* 6317 * If there is a request to lock the LUN and another LUN is locked 6318 * this is an error. If the requested LUN is already locked ignore 6319 * the request. If no LUN is locked attempt to lock it. 6320 * if there is a request to unlock the LUN and the LUN is currently 6321 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6322 * if another LUN is locked or no LUN is locked. 6323 */ 6324 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6325 if (softc->aps_locked_lun == lun->lun) { 6326 /* 6327 * This LUN is already locked, so we're done. 6328 */ 6329 retval = CTL_RETVAL_COMPLETE; 6330 } else if (softc->aps_locked_lun == 0) { 6331 /* 6332 * No one has the lock, pass the request to the 6333 * backend. 6334 */ 6335 retval = lun->backend->config_write( 6336 (union ctl_io *)ctsio); 6337 } else { 6338 /* 6339 * Someone else has the lock, throw out the request. 6340 */ 6341 ctl_set_already_locked(ctsio); 6342 free(ctsio->kern_data_ptr, M_CTL); 6343 ctl_done((union ctl_io *)ctsio); 6344 6345 /* 6346 * Set the return value so that ctl_do_mode_select() 6347 * won't try to complete the command. We already 6348 * completed it here. 6349 */ 6350 retval = CTL_RETVAL_ERROR; 6351 } 6352 } else if (softc->aps_locked_lun == lun->lun) { 6353 /* 6354 * This LUN is locked, so pass the unlock request to the 6355 * backend. 6356 */ 6357 retval = lun->backend->config_write((union ctl_io *)ctsio); 6358 } 6359 mtx_unlock(&softc->ctl_lock); 6360 6361 return (retval); 6362} 6363 6364int 6365ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6366 struct ctl_page_index *page_index, 6367 uint8_t *page_ptr) 6368{ 6369 uint8_t *c; 6370 int i; 6371 6372 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6373 ctl_time_io_secs = 6374 (c[0] << 8) | 6375 (c[1] << 0) | 6376 0; 6377 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6378 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6379 printf("page data:"); 6380 for (i=0; i<8; i++) 6381 printf(" %.2x",page_ptr[i]); 6382 printf("\n"); 6383 return (0); 6384} 6385 6386int 6387ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6388 struct ctl_page_index *page_index, 6389 int pc) 6390{ 6391 struct copan_debugconf_subpage *page; 6392 6393 page = (struct copan_debugconf_subpage *)page_index->page_data + 6394 (page_index->page_len * pc); 6395 6396 switch (pc) { 6397 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6398 case SMS_PAGE_CTRL_DEFAULT >> 6: 6399 case SMS_PAGE_CTRL_SAVED >> 6: 6400 /* 6401 * We don't update the changable or default bits for this page. 6402 */ 6403 break; 6404 case SMS_PAGE_CTRL_CURRENT >> 6: 6405 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6406 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6407 break; 6408 default: 6409#ifdef NEEDTOPORT 6410 EPRINT(0, "Invalid PC %d!!", pc); 6411#endif /* NEEDTOPORT */ 6412 break; 6413 } 6414 return (0); 6415} 6416 6417 6418static int 6419ctl_do_mode_select(union ctl_io *io) 6420{ 6421 struct scsi_mode_page_header *page_header; 6422 struct ctl_page_index *page_index; 6423 struct ctl_scsiio *ctsio; 6424 int control_dev, page_len; 6425 int page_len_offset, page_len_size; 6426 union ctl_modepage_info *modepage_info; 6427 struct ctl_lun *lun; 6428 int *len_left, *len_used; 6429 int retval, i; 6430 6431 ctsio = &io->scsiio; 6432 page_index = NULL; 6433 page_len = 0; 6434 retval = CTL_RETVAL_COMPLETE; 6435 6436 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6437 6438 if (lun->be_lun->lun_type != T_DIRECT) 6439 control_dev = 1; 6440 else 6441 control_dev = 0; 6442 6443 modepage_info = (union ctl_modepage_info *) 6444 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6445 len_left = &modepage_info->header.len_left; 6446 len_used = &modepage_info->header.len_used; 6447 6448do_next_page: 6449 6450 page_header = (struct scsi_mode_page_header *) 6451 (ctsio->kern_data_ptr + *len_used); 6452 6453 if (*len_left == 0) { 6454 free(ctsio->kern_data_ptr, M_CTL); 6455 ctl_set_success(ctsio); 6456 ctl_done((union ctl_io *)ctsio); 6457 return (CTL_RETVAL_COMPLETE); 6458 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6459 6460 free(ctsio->kern_data_ptr, M_CTL); 6461 ctl_set_param_len_error(ctsio); 6462 ctl_done((union ctl_io *)ctsio); 6463 return (CTL_RETVAL_COMPLETE); 6464 6465 } else if ((page_header->page_code & SMPH_SPF) 6466 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6467 6468 free(ctsio->kern_data_ptr, M_CTL); 6469 ctl_set_param_len_error(ctsio); 6470 ctl_done((union ctl_io *)ctsio); 6471 return (CTL_RETVAL_COMPLETE); 6472 } 6473 6474 6475 /* 6476 * XXX KDM should we do something with the block descriptor? 6477 */ 6478 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6479 6480 if ((control_dev != 0) 6481 && (lun->mode_pages.index[i].page_flags & 6482 CTL_PAGE_FLAG_DISK_ONLY)) 6483 continue; 6484 6485 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6486 (page_header->page_code & SMPH_PC_MASK)) 6487 continue; 6488 6489 /* 6490 * If neither page has a subpage code, then we've got a 6491 * match. 6492 */ 6493 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6494 && ((page_header->page_code & SMPH_SPF) == 0)) { 6495 page_index = &lun->mode_pages.index[i]; 6496 page_len = page_header->page_length; 6497 break; 6498 } 6499 6500 /* 6501 * If both pages have subpages, then the subpage numbers 6502 * have to match. 6503 */ 6504 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6505 && (page_header->page_code & SMPH_SPF)) { 6506 struct scsi_mode_page_header_sp *sph; 6507 6508 sph = (struct scsi_mode_page_header_sp *)page_header; 6509 6510 if (lun->mode_pages.index[i].subpage == 6511 sph->subpage) { 6512 page_index = &lun->mode_pages.index[i]; 6513 page_len = scsi_2btoul(sph->page_length); 6514 break; 6515 } 6516 } 6517 } 6518 6519 /* 6520 * If we couldn't find the page, or if we don't have a mode select 6521 * handler for it, send back an error to the user. 6522 */ 6523 if ((page_index == NULL) 6524 || (page_index->select_handler == NULL)) { 6525 ctl_set_invalid_field(ctsio, 6526 /*sks_valid*/ 1, 6527 /*command*/ 0, 6528 /*field*/ *len_used, 6529 /*bit_valid*/ 0, 6530 /*bit*/ 0); 6531 free(ctsio->kern_data_ptr, M_CTL); 6532 ctl_done((union ctl_io *)ctsio); 6533 return (CTL_RETVAL_COMPLETE); 6534 } 6535 6536 if (page_index->page_code & SMPH_SPF) { 6537 page_len_offset = 2; 6538 page_len_size = 2; 6539 } else { 6540 page_len_size = 1; 6541 page_len_offset = 1; 6542 } 6543 6544 /* 6545 * If the length the initiator gives us isn't the one we specify in 6546 * the mode page header, or if they didn't specify enough data in 6547 * the CDB to avoid truncating this page, kick out the request. 6548 */ 6549 if ((page_len != (page_index->page_len - page_len_offset - 6550 page_len_size)) 6551 || (*len_left < page_index->page_len)) { 6552 6553 6554 ctl_set_invalid_field(ctsio, 6555 /*sks_valid*/ 1, 6556 /*command*/ 0, 6557 /*field*/ *len_used + page_len_offset, 6558 /*bit_valid*/ 0, 6559 /*bit*/ 0); 6560 free(ctsio->kern_data_ptr, M_CTL); 6561 ctl_done((union ctl_io *)ctsio); 6562 return (CTL_RETVAL_COMPLETE); 6563 } 6564 6565 /* 6566 * Run through the mode page, checking to make sure that the bits 6567 * the user changed are actually legal for him to change. 6568 */ 6569 for (i = 0; i < page_index->page_len; i++) { 6570 uint8_t *user_byte, *change_mask, *current_byte; 6571 int bad_bit; 6572 int j; 6573 6574 user_byte = (uint8_t *)page_header + i; 6575 change_mask = page_index->page_data + 6576 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6577 current_byte = page_index->page_data + 6578 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6579 6580 /* 6581 * Check to see whether the user set any bits in this byte 6582 * that he is not allowed to set. 6583 */ 6584 if ((*user_byte & ~(*change_mask)) == 6585 (*current_byte & ~(*change_mask))) 6586 continue; 6587 6588 /* 6589 * Go through bit by bit to determine which one is illegal. 6590 */ 6591 bad_bit = 0; 6592 for (j = 7; j >= 0; j--) { 6593 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6594 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6595 bad_bit = i; 6596 break; 6597 } 6598 } 6599 ctl_set_invalid_field(ctsio, 6600 /*sks_valid*/ 1, 6601 /*command*/ 0, 6602 /*field*/ *len_used + i, 6603 /*bit_valid*/ 1, 6604 /*bit*/ bad_bit); 6605 free(ctsio->kern_data_ptr, M_CTL); 6606 ctl_done((union ctl_io *)ctsio); 6607 return (CTL_RETVAL_COMPLETE); 6608 } 6609 6610 /* 6611 * Decrement these before we call the page handler, since we may 6612 * end up getting called back one way or another before the handler 6613 * returns to this context. 6614 */ 6615 *len_left -= page_index->page_len; 6616 *len_used += page_index->page_len; 6617 6618 retval = page_index->select_handler(ctsio, page_index, 6619 (uint8_t *)page_header); 6620 6621 /* 6622 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6623 * wait until this queued command completes to finish processing 6624 * the mode page. If it returns anything other than 6625 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6626 * already set the sense information, freed the data pointer, and 6627 * completed the io for us. 6628 */ 6629 if (retval != CTL_RETVAL_COMPLETE) 6630 goto bailout_no_done; 6631 6632 /* 6633 * If the initiator sent us more than one page, parse the next one. 6634 */ 6635 if (*len_left > 0) 6636 goto do_next_page; 6637 6638 ctl_set_success(ctsio); 6639 free(ctsio->kern_data_ptr, M_CTL); 6640 ctl_done((union ctl_io *)ctsio); 6641 6642bailout_no_done: 6643 6644 return (CTL_RETVAL_COMPLETE); 6645 6646} 6647 6648int 6649ctl_mode_select(struct ctl_scsiio *ctsio) 6650{ 6651 int param_len, pf, sp; 6652 int header_size, bd_len; 6653 int len_left, len_used; 6654 struct ctl_page_index *page_index; 6655 struct ctl_lun *lun; 6656 int control_dev, page_len; 6657 union ctl_modepage_info *modepage_info; 6658 int retval; 6659 6660 pf = 0; 6661 sp = 0; 6662 page_len = 0; 6663 len_used = 0; 6664 len_left = 0; 6665 retval = 0; 6666 bd_len = 0; 6667 page_index = NULL; 6668 6669 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6670 6671 if (lun->be_lun->lun_type != T_DIRECT) 6672 control_dev = 1; 6673 else 6674 control_dev = 0; 6675 6676 switch (ctsio->cdb[0]) { 6677 case MODE_SELECT_6: { 6678 struct scsi_mode_select_6 *cdb; 6679 6680 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6681 6682 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6683 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6684 6685 param_len = cdb->length; 6686 header_size = sizeof(struct scsi_mode_header_6); 6687 break; 6688 } 6689 case MODE_SELECT_10: { 6690 struct scsi_mode_select_10 *cdb; 6691 6692 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6693 6694 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6695 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6696 6697 param_len = scsi_2btoul(cdb->length); 6698 header_size = sizeof(struct scsi_mode_header_10); 6699 break; 6700 } 6701 default: 6702 ctl_set_invalid_opcode(ctsio); 6703 ctl_done((union ctl_io *)ctsio); 6704 return (CTL_RETVAL_COMPLETE); 6705 break; /* NOTREACHED */ 6706 } 6707 6708 /* 6709 * From SPC-3: 6710 * "A parameter list length of zero indicates that the Data-Out Buffer 6711 * shall be empty. This condition shall not be considered as an error." 6712 */ 6713 if (param_len == 0) { 6714 ctl_set_success(ctsio); 6715 ctl_done((union ctl_io *)ctsio); 6716 return (CTL_RETVAL_COMPLETE); 6717 } 6718 6719 /* 6720 * Since we'll hit this the first time through, prior to 6721 * allocation, we don't need to free a data buffer here. 6722 */ 6723 if (param_len < header_size) { 6724 ctl_set_param_len_error(ctsio); 6725 ctl_done((union ctl_io *)ctsio); 6726 return (CTL_RETVAL_COMPLETE); 6727 } 6728 6729 /* 6730 * Allocate the data buffer and grab the user's data. In theory, 6731 * we shouldn't have to sanity check the parameter list length here 6732 * because the maximum size is 64K. We should be able to malloc 6733 * that much without too many problems. 6734 */ 6735 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6736 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6737 ctsio->kern_data_len = param_len; 6738 ctsio->kern_total_len = param_len; 6739 ctsio->kern_data_resid = 0; 6740 ctsio->kern_rel_offset = 0; 6741 ctsio->kern_sg_entries = 0; 6742 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6743 ctsio->be_move_done = ctl_config_move_done; 6744 ctl_datamove((union ctl_io *)ctsio); 6745 6746 return (CTL_RETVAL_COMPLETE); 6747 } 6748 6749 switch (ctsio->cdb[0]) { 6750 case MODE_SELECT_6: { 6751 struct scsi_mode_header_6 *mh6; 6752 6753 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6754 bd_len = mh6->blk_desc_len; 6755 break; 6756 } 6757 case MODE_SELECT_10: { 6758 struct scsi_mode_header_10 *mh10; 6759 6760 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6761 bd_len = scsi_2btoul(mh10->blk_desc_len); 6762 break; 6763 } 6764 default: 6765 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6766 break; 6767 } 6768 6769 if (param_len < (header_size + bd_len)) { 6770 free(ctsio->kern_data_ptr, M_CTL); 6771 ctl_set_param_len_error(ctsio); 6772 ctl_done((union ctl_io *)ctsio); 6773 return (CTL_RETVAL_COMPLETE); 6774 } 6775 6776 /* 6777 * Set the IO_CONT flag, so that if this I/O gets passed to 6778 * ctl_config_write_done(), it'll get passed back to 6779 * ctl_do_mode_select() for further processing, or completion if 6780 * we're all done. 6781 */ 6782 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6783 ctsio->io_cont = ctl_do_mode_select; 6784 6785 modepage_info = (union ctl_modepage_info *) 6786 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6787 6788 memset(modepage_info, 0, sizeof(*modepage_info)); 6789 6790 len_left = param_len - header_size - bd_len; 6791 len_used = header_size + bd_len; 6792 6793 modepage_info->header.len_left = len_left; 6794 modepage_info->header.len_used = len_used; 6795 6796 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6797} 6798 6799int 6800ctl_mode_sense(struct ctl_scsiio *ctsio) 6801{ 6802 struct ctl_lun *lun; 6803 int pc, page_code, dbd, llba, subpage; 6804 int alloc_len, page_len, header_len, total_len; 6805 struct scsi_mode_block_descr *block_desc; 6806 struct ctl_page_index *page_index; 6807 int control_dev; 6808 6809 dbd = 0; 6810 llba = 0; 6811 block_desc = NULL; 6812 page_index = NULL; 6813 6814 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6815 6816 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6817 6818 if (lun->be_lun->lun_type != T_DIRECT) 6819 control_dev = 1; 6820 else 6821 control_dev = 0; 6822 6823 if (lun->flags & CTL_LUN_PR_RESERVED) { 6824 uint32_t residx; 6825 6826 /* 6827 * XXX KDM need a lock here. 6828 */ 6829 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6830 if ((lun->res_type == SPR_TYPE_EX_AC 6831 && residx != lun->pr_res_idx) 6832 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6833 || lun->res_type == SPR_TYPE_EX_AC_AR) 6834 && !lun->per_res[residx].registered)) { 6835 ctl_set_reservation_conflict(ctsio); 6836 ctl_done((union ctl_io *)ctsio); 6837 return (CTL_RETVAL_COMPLETE); 6838 } 6839 } 6840 6841 switch (ctsio->cdb[0]) { 6842 case MODE_SENSE_6: { 6843 struct scsi_mode_sense_6 *cdb; 6844 6845 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6846 6847 header_len = sizeof(struct scsi_mode_hdr_6); 6848 if (cdb->byte2 & SMS_DBD) 6849 dbd = 1; 6850 else 6851 header_len += sizeof(struct scsi_mode_block_descr); 6852 6853 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6854 page_code = cdb->page & SMS_PAGE_CODE; 6855 subpage = cdb->subpage; 6856 alloc_len = cdb->length; 6857 break; 6858 } 6859 case MODE_SENSE_10: { 6860 struct scsi_mode_sense_10 *cdb; 6861 6862 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6863 6864 header_len = sizeof(struct scsi_mode_hdr_10); 6865 6866 if (cdb->byte2 & SMS_DBD) 6867 dbd = 1; 6868 else 6869 header_len += sizeof(struct scsi_mode_block_descr); 6870 if (cdb->byte2 & SMS10_LLBAA) 6871 llba = 1; 6872 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6873 page_code = cdb->page & SMS_PAGE_CODE; 6874 subpage = cdb->subpage; 6875 alloc_len = scsi_2btoul(cdb->length); 6876 break; 6877 } 6878 default: 6879 ctl_set_invalid_opcode(ctsio); 6880 ctl_done((union ctl_io *)ctsio); 6881 return (CTL_RETVAL_COMPLETE); 6882 break; /* NOTREACHED */ 6883 } 6884 6885 /* 6886 * We have to make a first pass through to calculate the size of 6887 * the pages that match the user's query. Then we allocate enough 6888 * memory to hold it, and actually copy the data into the buffer. 6889 */ 6890 switch (page_code) { 6891 case SMS_ALL_PAGES_PAGE: { 6892 int i; 6893 6894 page_len = 0; 6895 6896 /* 6897 * At the moment, values other than 0 and 0xff here are 6898 * reserved according to SPC-3. 6899 */ 6900 if ((subpage != SMS_SUBPAGE_PAGE_0) 6901 && (subpage != SMS_SUBPAGE_ALL)) { 6902 ctl_set_invalid_field(ctsio, 6903 /*sks_valid*/ 1, 6904 /*command*/ 1, 6905 /*field*/ 3, 6906 /*bit_valid*/ 0, 6907 /*bit*/ 0); 6908 ctl_done((union ctl_io *)ctsio); 6909 return (CTL_RETVAL_COMPLETE); 6910 } 6911 6912 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6913 if ((control_dev != 0) 6914 && (lun->mode_pages.index[i].page_flags & 6915 CTL_PAGE_FLAG_DISK_ONLY)) 6916 continue; 6917 6918 /* 6919 * We don't use this subpage if the user didn't 6920 * request all subpages. 6921 */ 6922 if ((lun->mode_pages.index[i].subpage != 0) 6923 && (subpage == SMS_SUBPAGE_PAGE_0)) 6924 continue; 6925 6926#if 0 6927 printf("found page %#x len %d\n", 6928 lun->mode_pages.index[i].page_code & 6929 SMPH_PC_MASK, 6930 lun->mode_pages.index[i].page_len); 6931#endif 6932 page_len += lun->mode_pages.index[i].page_len; 6933 } 6934 break; 6935 } 6936 default: { 6937 int i; 6938 6939 page_len = 0; 6940 6941 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6942 /* Look for the right page code */ 6943 if ((lun->mode_pages.index[i].page_code & 6944 SMPH_PC_MASK) != page_code) 6945 continue; 6946 6947 /* Look for the right subpage or the subpage wildcard*/ 6948 if ((lun->mode_pages.index[i].subpage != subpage) 6949 && (subpage != SMS_SUBPAGE_ALL)) 6950 continue; 6951 6952 /* Make sure the page is supported for this dev type */ 6953 if ((control_dev != 0) 6954 && (lun->mode_pages.index[i].page_flags & 6955 CTL_PAGE_FLAG_DISK_ONLY)) 6956 continue; 6957 6958#if 0 6959 printf("found page %#x len %d\n", 6960 lun->mode_pages.index[i].page_code & 6961 SMPH_PC_MASK, 6962 lun->mode_pages.index[i].page_len); 6963#endif 6964 6965 page_len += lun->mode_pages.index[i].page_len; 6966 } 6967 6968 if (page_len == 0) { 6969 ctl_set_invalid_field(ctsio, 6970 /*sks_valid*/ 1, 6971 /*command*/ 1, 6972 /*field*/ 2, 6973 /*bit_valid*/ 1, 6974 /*bit*/ 5); 6975 ctl_done((union ctl_io *)ctsio); 6976 return (CTL_RETVAL_COMPLETE); 6977 } 6978 break; 6979 } 6980 } 6981 6982 total_len = header_len + page_len; 6983#if 0 6984 printf("header_len = %d, page_len = %d, total_len = %d\n", 6985 header_len, page_len, total_len); 6986#endif 6987 6988 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6989 ctsio->kern_sg_entries = 0; 6990 ctsio->kern_data_resid = 0; 6991 ctsio->kern_rel_offset = 0; 6992 if (total_len < alloc_len) { 6993 ctsio->residual = alloc_len - total_len; 6994 ctsio->kern_data_len = total_len; 6995 ctsio->kern_total_len = total_len; 6996 } else { 6997 ctsio->residual = 0; 6998 ctsio->kern_data_len = alloc_len; 6999 ctsio->kern_total_len = alloc_len; 7000 } 7001 7002 switch (ctsio->cdb[0]) { 7003 case MODE_SENSE_6: { 7004 struct scsi_mode_hdr_6 *header; 7005 7006 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7007 7008 header->datalen = ctl_min(total_len - 1, 254); 7009 7010 if (dbd) 7011 header->block_descr_len = 0; 7012 else 7013 header->block_descr_len = 7014 sizeof(struct scsi_mode_block_descr); 7015 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7016 break; 7017 } 7018 case MODE_SENSE_10: { 7019 struct scsi_mode_hdr_10 *header; 7020 int datalen; 7021 7022 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7023 7024 datalen = ctl_min(total_len - 2, 65533); 7025 scsi_ulto2b(datalen, header->datalen); 7026 if (dbd) 7027 scsi_ulto2b(0, header->block_descr_len); 7028 else 7029 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7030 header->block_descr_len); 7031 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7032 break; 7033 } 7034 default: 7035 panic("invalid CDB type %#x", ctsio->cdb[0]); 7036 break; /* NOTREACHED */ 7037 } 7038 7039 /* 7040 * If we've got a disk, use its blocksize in the block 7041 * descriptor. Otherwise, just set it to 0. 7042 */ 7043 if (dbd == 0) { 7044 if (control_dev != 0) 7045 scsi_ulto3b(lun->be_lun->blocksize, 7046 block_desc->block_len); 7047 else 7048 scsi_ulto3b(0, block_desc->block_len); 7049 } 7050 7051 switch (page_code) { 7052 case SMS_ALL_PAGES_PAGE: { 7053 int i, data_used; 7054 7055 data_used = header_len; 7056 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7057 struct ctl_page_index *page_index; 7058 7059 page_index = &lun->mode_pages.index[i]; 7060 7061 if ((control_dev != 0) 7062 && (page_index->page_flags & 7063 CTL_PAGE_FLAG_DISK_ONLY)) 7064 continue; 7065 7066 /* 7067 * We don't use this subpage if the user didn't 7068 * request all subpages. We already checked (above) 7069 * to make sure the user only specified a subpage 7070 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7071 */ 7072 if ((page_index->subpage != 0) 7073 && (subpage == SMS_SUBPAGE_PAGE_0)) 7074 continue; 7075 7076 /* 7077 * Call the handler, if it exists, to update the 7078 * page to the latest values. 7079 */ 7080 if (page_index->sense_handler != NULL) 7081 page_index->sense_handler(ctsio, page_index,pc); 7082 7083 memcpy(ctsio->kern_data_ptr + data_used, 7084 page_index->page_data + 7085 (page_index->page_len * pc), 7086 page_index->page_len); 7087 data_used += page_index->page_len; 7088 } 7089 break; 7090 } 7091 default: { 7092 int i, data_used; 7093 7094 data_used = header_len; 7095 7096 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7097 struct ctl_page_index *page_index; 7098 7099 page_index = &lun->mode_pages.index[i]; 7100 7101 /* Look for the right page code */ 7102 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7103 continue; 7104 7105 /* Look for the right subpage or the subpage wildcard*/ 7106 if ((page_index->subpage != subpage) 7107 && (subpage != SMS_SUBPAGE_ALL)) 7108 continue; 7109 7110 /* Make sure the page is supported for this dev type */ 7111 if ((control_dev != 0) 7112 && (page_index->page_flags & 7113 CTL_PAGE_FLAG_DISK_ONLY)) 7114 continue; 7115 7116 /* 7117 * Call the handler, if it exists, to update the 7118 * page to the latest values. 7119 */ 7120 if (page_index->sense_handler != NULL) 7121 page_index->sense_handler(ctsio, page_index,pc); 7122 7123 memcpy(ctsio->kern_data_ptr + data_used, 7124 page_index->page_data + 7125 (page_index->page_len * pc), 7126 page_index->page_len); 7127 data_used += page_index->page_len; 7128 } 7129 break; 7130 } 7131 } 7132 7133 ctsio->scsi_status = SCSI_STATUS_OK; 7134 7135 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7136 ctsio->be_move_done = ctl_config_move_done; 7137 ctl_datamove((union ctl_io *)ctsio); 7138 7139 return (CTL_RETVAL_COMPLETE); 7140} 7141 7142int 7143ctl_read_capacity(struct ctl_scsiio *ctsio) 7144{ 7145 struct scsi_read_capacity *cdb; 7146 struct scsi_read_capacity_data *data; 7147 struct ctl_lun *lun; 7148 uint32_t lba; 7149 7150 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7151 7152 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7153 7154 lba = scsi_4btoul(cdb->addr); 7155 if (((cdb->pmi & SRC_PMI) == 0) 7156 && (lba != 0)) { 7157 ctl_set_invalid_field(/*ctsio*/ ctsio, 7158 /*sks_valid*/ 1, 7159 /*command*/ 1, 7160 /*field*/ 2, 7161 /*bit_valid*/ 0, 7162 /*bit*/ 0); 7163 ctl_done((union ctl_io *)ctsio); 7164 return (CTL_RETVAL_COMPLETE); 7165 } 7166 7167 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7168 7169 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7170 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7171 ctsio->residual = 0; 7172 ctsio->kern_data_len = sizeof(*data); 7173 ctsio->kern_total_len = sizeof(*data); 7174 ctsio->kern_data_resid = 0; 7175 ctsio->kern_rel_offset = 0; 7176 ctsio->kern_sg_entries = 0; 7177 7178 /* 7179 * If the maximum LBA is greater than 0xfffffffe, the user must 7180 * issue a SERVICE ACTION IN (16) command, with the read capacity 7181 * serivce action set. 7182 */ 7183 if (lun->be_lun->maxlba > 0xfffffffe) 7184 scsi_ulto4b(0xffffffff, data->addr); 7185 else 7186 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7187 7188 /* 7189 * XXX KDM this may not be 512 bytes... 7190 */ 7191 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7192 7193 ctsio->scsi_status = SCSI_STATUS_OK; 7194 7195 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7196 ctsio->be_move_done = ctl_config_move_done; 7197 ctl_datamove((union ctl_io *)ctsio); 7198 7199 return (CTL_RETVAL_COMPLETE); 7200} 7201 7202int 7203ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7204{ 7205 struct scsi_read_capacity_16 *cdb; 7206 struct scsi_read_capacity_data_long *data; 7207 struct ctl_lun *lun; 7208 uint64_t lba; 7209 uint32_t alloc_len; 7210 7211 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7212 7213 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7214 7215 alloc_len = scsi_4btoul(cdb->alloc_len); 7216 lba = scsi_8btou64(cdb->addr); 7217 7218 if ((cdb->reladr & SRC16_PMI) 7219 && (lba != 0)) { 7220 ctl_set_invalid_field(/*ctsio*/ ctsio, 7221 /*sks_valid*/ 1, 7222 /*command*/ 1, 7223 /*field*/ 2, 7224 /*bit_valid*/ 0, 7225 /*bit*/ 0); 7226 ctl_done((union ctl_io *)ctsio); 7227 return (CTL_RETVAL_COMPLETE); 7228 } 7229 7230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7231 7232 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7233 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7234 7235 if (sizeof(*data) < alloc_len) { 7236 ctsio->residual = alloc_len - sizeof(*data); 7237 ctsio->kern_data_len = sizeof(*data); 7238 ctsio->kern_total_len = sizeof(*data); 7239 } else { 7240 ctsio->residual = 0; 7241 ctsio->kern_data_len = alloc_len; 7242 ctsio->kern_total_len = alloc_len; 7243 } 7244 ctsio->kern_data_resid = 0; 7245 ctsio->kern_rel_offset = 0; 7246 ctsio->kern_sg_entries = 0; 7247 7248 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7249 /* XXX KDM this may not be 512 bytes... */ 7250 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7251 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7252 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7253 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7254 data->lalba_lbp[0] |= SRC16_LBPME; 7255 7256 ctsio->scsi_status = SCSI_STATUS_OK; 7257 7258 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7259 ctsio->be_move_done = ctl_config_move_done; 7260 ctl_datamove((union ctl_io *)ctsio); 7261 7262 return (CTL_RETVAL_COMPLETE); 7263} 7264 7265int 7266ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7267{ 7268 struct scsi_maintenance_in *cdb; 7269 int retval; 7270 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7271 int num_target_port_groups, num_target_ports, single; 7272 struct ctl_lun *lun; 7273 struct ctl_softc *softc; 7274 struct ctl_port *port; 7275 struct scsi_target_group_data *rtg_ptr; 7276 struct scsi_target_group_data_extended *rtg_ext_ptr; 7277 struct scsi_target_port_group_descriptor *tpg_desc; 7278 7279 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7280 7281 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7282 softc = control_softc; 7283 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7284 7285 retval = CTL_RETVAL_COMPLETE; 7286 7287 switch (cdb->byte2 & STG_PDF_MASK) { 7288 case STG_PDF_LENGTH: 7289 ext = 0; 7290 break; 7291 case STG_PDF_EXTENDED: 7292 ext = 1; 7293 break; 7294 default: 7295 ctl_set_invalid_field(/*ctsio*/ ctsio, 7296 /*sks_valid*/ 1, 7297 /*command*/ 1, 7298 /*field*/ 2, 7299 /*bit_valid*/ 1, 7300 /*bit*/ 5); 7301 ctl_done((union ctl_io *)ctsio); 7302 return(retval); 7303 } 7304 7305 single = ctl_is_single; 7306 if (single) 7307 num_target_port_groups = 1; 7308 else 7309 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7310 num_target_ports = 0; 7311 mtx_lock(&softc->ctl_lock); 7312 STAILQ_FOREACH(port, &softc->port_list, links) { 7313 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7314 continue; 7315 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7316 continue; 7317 num_target_ports++; 7318 } 7319 mtx_unlock(&softc->ctl_lock); 7320 7321 if (ext) 7322 total_len = sizeof(struct scsi_target_group_data_extended); 7323 else 7324 total_len = sizeof(struct scsi_target_group_data); 7325 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7326 num_target_port_groups + 7327 sizeof(struct scsi_target_port_descriptor) * 7328 num_target_ports * num_target_port_groups; 7329 7330 alloc_len = scsi_4btoul(cdb->length); 7331 7332 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7333 7334 ctsio->kern_sg_entries = 0; 7335 7336 if (total_len < alloc_len) { 7337 ctsio->residual = alloc_len - total_len; 7338 ctsio->kern_data_len = total_len; 7339 ctsio->kern_total_len = total_len; 7340 } else { 7341 ctsio->residual = 0; 7342 ctsio->kern_data_len = alloc_len; 7343 ctsio->kern_total_len = alloc_len; 7344 } 7345 ctsio->kern_data_resid = 0; 7346 ctsio->kern_rel_offset = 0; 7347 7348 if (ext) { 7349 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7350 ctsio->kern_data_ptr; 7351 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7352 rtg_ext_ptr->format_type = 0x10; 7353 rtg_ext_ptr->implicit_transition_time = 0; 7354 tpg_desc = &rtg_ext_ptr->groups[0]; 7355 } else { 7356 rtg_ptr = (struct scsi_target_group_data *) 7357 ctsio->kern_data_ptr; 7358 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7359 tpg_desc = &rtg_ptr->groups[0]; 7360 } 7361 7362 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7363 mtx_lock(&softc->ctl_lock); 7364 for (g = 0; g < num_target_port_groups; g++) { 7365 if (g == pg) 7366 tpg_desc->pref_state = TPG_PRIMARY | 7367 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7368 else 7369 tpg_desc->pref_state = 7370 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7371 tpg_desc->support = TPG_AO_SUP; 7372 if (!single) 7373 tpg_desc->support |= TPG_AN_SUP; 7374 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7375 tpg_desc->status = TPG_IMPLICIT; 7376 pc = 0; 7377 STAILQ_FOREACH(port, &softc->port_list, links) { 7378 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7379 continue; 7380 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7381 CTL_MAX_LUNS) 7382 continue; 7383 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7384 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7385 relative_target_port_identifier); 7386 pc++; 7387 } 7388 tpg_desc->target_port_count = pc; 7389 tpg_desc = (struct scsi_target_port_group_descriptor *) 7390 &tpg_desc->descriptors[pc]; 7391 } 7392 mtx_unlock(&softc->ctl_lock); 7393 7394 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7395 ctsio->be_move_done = ctl_config_move_done; 7396 7397 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7398 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7399 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7400 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7401 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7402 7403 ctl_datamove((union ctl_io *)ctsio); 7404 return(retval); 7405} 7406 7407int 7408ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7409{ 7410 struct ctl_lun *lun; 7411 struct scsi_report_supported_opcodes *cdb; 7412 const struct ctl_cmd_entry *entry, *sentry; 7413 struct scsi_report_supported_opcodes_all *all; 7414 struct scsi_report_supported_opcodes_descr *descr; 7415 struct scsi_report_supported_opcodes_one *one; 7416 int retval; 7417 int alloc_len, total_len; 7418 int opcode, service_action, i, j, num; 7419 7420 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7421 7422 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7423 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7424 7425 retval = CTL_RETVAL_COMPLETE; 7426 7427 opcode = cdb->requested_opcode; 7428 service_action = scsi_2btoul(cdb->requested_service_action); 7429 switch (cdb->options & RSO_OPTIONS_MASK) { 7430 case RSO_OPTIONS_ALL: 7431 num = 0; 7432 for (i = 0; i < 256; i++) { 7433 entry = &ctl_cmd_table[i]; 7434 if (entry->flags & CTL_CMD_FLAG_SA5) { 7435 for (j = 0; j < 32; j++) { 7436 sentry = &((const struct ctl_cmd_entry *) 7437 entry->execute)[j]; 7438 if (ctl_cmd_applicable( 7439 lun->be_lun->lun_type, sentry)) 7440 num++; 7441 } 7442 } else { 7443 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7444 entry)) 7445 num++; 7446 } 7447 } 7448 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7449 num * sizeof(struct scsi_report_supported_opcodes_descr); 7450 break; 7451 case RSO_OPTIONS_OC: 7452 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7453 ctl_set_invalid_field(/*ctsio*/ ctsio, 7454 /*sks_valid*/ 1, 7455 /*command*/ 1, 7456 /*field*/ 2, 7457 /*bit_valid*/ 1, 7458 /*bit*/ 2); 7459 ctl_done((union ctl_io *)ctsio); 7460 return (CTL_RETVAL_COMPLETE); 7461 } 7462 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7463 break; 7464 case RSO_OPTIONS_OC_SA: 7465 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7466 service_action >= 32) { 7467 ctl_set_invalid_field(/*ctsio*/ ctsio, 7468 /*sks_valid*/ 1, 7469 /*command*/ 1, 7470 /*field*/ 2, 7471 /*bit_valid*/ 1, 7472 /*bit*/ 2); 7473 ctl_done((union ctl_io *)ctsio); 7474 return (CTL_RETVAL_COMPLETE); 7475 } 7476 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7477 break; 7478 default: 7479 ctl_set_invalid_field(/*ctsio*/ ctsio, 7480 /*sks_valid*/ 1, 7481 /*command*/ 1, 7482 /*field*/ 2, 7483 /*bit_valid*/ 1, 7484 /*bit*/ 2); 7485 ctl_done((union ctl_io *)ctsio); 7486 return (CTL_RETVAL_COMPLETE); 7487 } 7488 7489 alloc_len = scsi_4btoul(cdb->length); 7490 7491 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7492 7493 ctsio->kern_sg_entries = 0; 7494 7495 if (total_len < alloc_len) { 7496 ctsio->residual = alloc_len - total_len; 7497 ctsio->kern_data_len = total_len; 7498 ctsio->kern_total_len = total_len; 7499 } else { 7500 ctsio->residual = 0; 7501 ctsio->kern_data_len = alloc_len; 7502 ctsio->kern_total_len = alloc_len; 7503 } 7504 ctsio->kern_data_resid = 0; 7505 ctsio->kern_rel_offset = 0; 7506 7507 switch (cdb->options & RSO_OPTIONS_MASK) { 7508 case RSO_OPTIONS_ALL: 7509 all = (struct scsi_report_supported_opcodes_all *) 7510 ctsio->kern_data_ptr; 7511 num = 0; 7512 for (i = 0; i < 256; i++) { 7513 entry = &ctl_cmd_table[i]; 7514 if (entry->flags & CTL_CMD_FLAG_SA5) { 7515 for (j = 0; j < 32; j++) { 7516 sentry = &((const struct ctl_cmd_entry *) 7517 entry->execute)[j]; 7518 if (!ctl_cmd_applicable( 7519 lun->be_lun->lun_type, sentry)) 7520 continue; 7521 descr = &all->descr[num++]; 7522 descr->opcode = i; 7523 scsi_ulto2b(j, descr->service_action); 7524 descr->flags = RSO_SERVACTV; 7525 scsi_ulto2b(sentry->length, 7526 descr->cdb_length); 7527 } 7528 } else { 7529 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7530 entry)) 7531 continue; 7532 descr = &all->descr[num++]; 7533 descr->opcode = i; 7534 scsi_ulto2b(0, descr->service_action); 7535 descr->flags = 0; 7536 scsi_ulto2b(entry->length, descr->cdb_length); 7537 } 7538 } 7539 scsi_ulto4b( 7540 num * sizeof(struct scsi_report_supported_opcodes_descr), 7541 all->length); 7542 break; 7543 case RSO_OPTIONS_OC: 7544 one = (struct scsi_report_supported_opcodes_one *) 7545 ctsio->kern_data_ptr; 7546 entry = &ctl_cmd_table[opcode]; 7547 goto fill_one; 7548 case RSO_OPTIONS_OC_SA: 7549 one = (struct scsi_report_supported_opcodes_one *) 7550 ctsio->kern_data_ptr; 7551 entry = &ctl_cmd_table[opcode]; 7552 entry = &((const struct ctl_cmd_entry *) 7553 entry->execute)[service_action]; 7554fill_one: 7555 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7556 one->support = 3; 7557 scsi_ulto2b(entry->length, one->cdb_length); 7558 one->cdb_usage[0] = opcode; 7559 memcpy(&one->cdb_usage[1], entry->usage, 7560 entry->length - 1); 7561 } else 7562 one->support = 1; 7563 break; 7564 } 7565 7566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7567 ctsio->be_move_done = ctl_config_move_done; 7568 7569 ctl_datamove((union ctl_io *)ctsio); 7570 return(retval); 7571} 7572 7573int 7574ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7575{ 7576 struct ctl_lun *lun; 7577 struct scsi_report_supported_tmf *cdb; 7578 struct scsi_report_supported_tmf_data *data; 7579 int retval; 7580 int alloc_len, total_len; 7581 7582 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7583 7584 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7585 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7586 7587 retval = CTL_RETVAL_COMPLETE; 7588 7589 total_len = sizeof(struct scsi_report_supported_tmf_data); 7590 alloc_len = scsi_4btoul(cdb->length); 7591 7592 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7593 7594 ctsio->kern_sg_entries = 0; 7595 7596 if (total_len < alloc_len) { 7597 ctsio->residual = alloc_len - total_len; 7598 ctsio->kern_data_len = total_len; 7599 ctsio->kern_total_len = total_len; 7600 } else { 7601 ctsio->residual = 0; 7602 ctsio->kern_data_len = alloc_len; 7603 ctsio->kern_total_len = alloc_len; 7604 } 7605 ctsio->kern_data_resid = 0; 7606 ctsio->kern_rel_offset = 0; 7607 7608 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7609 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7610 data->byte2 |= RST_ITNRS; 7611 7612 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7613 ctsio->be_move_done = ctl_config_move_done; 7614 7615 ctl_datamove((union ctl_io *)ctsio); 7616 return (retval); 7617} 7618 7619int 7620ctl_report_timestamp(struct ctl_scsiio *ctsio) 7621{ 7622 struct ctl_lun *lun; 7623 struct scsi_report_timestamp *cdb; 7624 struct scsi_report_timestamp_data *data; 7625 struct timeval tv; 7626 int64_t timestamp; 7627 int retval; 7628 int alloc_len, total_len; 7629 7630 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7631 7632 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7633 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7634 7635 retval = CTL_RETVAL_COMPLETE; 7636 7637 total_len = sizeof(struct scsi_report_timestamp_data); 7638 alloc_len = scsi_4btoul(cdb->length); 7639 7640 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7641 7642 ctsio->kern_sg_entries = 0; 7643 7644 if (total_len < alloc_len) { 7645 ctsio->residual = alloc_len - total_len; 7646 ctsio->kern_data_len = total_len; 7647 ctsio->kern_total_len = total_len; 7648 } else { 7649 ctsio->residual = 0; 7650 ctsio->kern_data_len = alloc_len; 7651 ctsio->kern_total_len = alloc_len; 7652 } 7653 ctsio->kern_data_resid = 0; 7654 ctsio->kern_rel_offset = 0; 7655 7656 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7657 scsi_ulto2b(sizeof(*data) - 2, data->length); 7658 data->origin = RTS_ORIG_OUTSIDE; 7659 getmicrotime(&tv); 7660 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7661 scsi_ulto4b(timestamp >> 16, data->timestamp); 7662 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7663 7664 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7665 ctsio->be_move_done = ctl_config_move_done; 7666 7667 ctl_datamove((union ctl_io *)ctsio); 7668 return (retval); 7669} 7670 7671int 7672ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7673{ 7674 struct scsi_per_res_in *cdb; 7675 int alloc_len, total_len = 0; 7676 /* struct scsi_per_res_in_rsrv in_data; */ 7677 struct ctl_lun *lun; 7678 struct ctl_softc *softc; 7679 7680 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7681 7682 softc = control_softc; 7683 7684 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7685 7686 alloc_len = scsi_2btoul(cdb->length); 7687 7688 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7689 7690retry: 7691 mtx_lock(&lun->lun_lock); 7692 switch (cdb->action) { 7693 case SPRI_RK: /* read keys */ 7694 total_len = sizeof(struct scsi_per_res_in_keys) + 7695 lun->pr_key_count * 7696 sizeof(struct scsi_per_res_key); 7697 break; 7698 case SPRI_RR: /* read reservation */ 7699 if (lun->flags & CTL_LUN_PR_RESERVED) 7700 total_len = sizeof(struct scsi_per_res_in_rsrv); 7701 else 7702 total_len = sizeof(struct scsi_per_res_in_header); 7703 break; 7704 case SPRI_RC: /* report capabilities */ 7705 total_len = sizeof(struct scsi_per_res_cap); 7706 break; 7707 case SPRI_RS: /* read full status */ 7708 total_len = sizeof(struct scsi_per_res_in_header) + 7709 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7710 lun->pr_key_count; 7711 break; 7712 default: 7713 panic("Invalid PR type %x", cdb->action); 7714 } 7715 mtx_unlock(&lun->lun_lock); 7716 7717 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7718 7719 if (total_len < alloc_len) { 7720 ctsio->residual = alloc_len - total_len; 7721 ctsio->kern_data_len = total_len; 7722 ctsio->kern_total_len = total_len; 7723 } else { 7724 ctsio->residual = 0; 7725 ctsio->kern_data_len = alloc_len; 7726 ctsio->kern_total_len = alloc_len; 7727 } 7728 7729 ctsio->kern_data_resid = 0; 7730 ctsio->kern_rel_offset = 0; 7731 ctsio->kern_sg_entries = 0; 7732 7733 mtx_lock(&lun->lun_lock); 7734 switch (cdb->action) { 7735 case SPRI_RK: { // read keys 7736 struct scsi_per_res_in_keys *res_keys; 7737 int i, key_count; 7738 7739 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7740 7741 /* 7742 * We had to drop the lock to allocate our buffer, which 7743 * leaves time for someone to come in with another 7744 * persistent reservation. (That is unlikely, though, 7745 * since this should be the only persistent reservation 7746 * command active right now.) 7747 */ 7748 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7749 (lun->pr_key_count * 7750 sizeof(struct scsi_per_res_key)))){ 7751 mtx_unlock(&lun->lun_lock); 7752 free(ctsio->kern_data_ptr, M_CTL); 7753 printf("%s: reservation length changed, retrying\n", 7754 __func__); 7755 goto retry; 7756 } 7757 7758 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7759 7760 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7761 lun->pr_key_count, res_keys->header.length); 7762 7763 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7764 if (!lun->per_res[i].registered) 7765 continue; 7766 7767 /* 7768 * We used lun->pr_key_count to calculate the 7769 * size to allocate. If it turns out the number of 7770 * initiators with the registered flag set is 7771 * larger than that (i.e. they haven't been kept in 7772 * sync), we've got a problem. 7773 */ 7774 if (key_count >= lun->pr_key_count) { 7775#ifdef NEEDTOPORT 7776 csevent_log(CSC_CTL | CSC_SHELF_SW | 7777 CTL_PR_ERROR, 7778 csevent_LogType_Fault, 7779 csevent_AlertLevel_Yellow, 7780 csevent_FRU_ShelfController, 7781 csevent_FRU_Firmware, 7782 csevent_FRU_Unknown, 7783 "registered keys %d >= key " 7784 "count %d", key_count, 7785 lun->pr_key_count); 7786#endif 7787 key_count++; 7788 continue; 7789 } 7790 memcpy(res_keys->keys[key_count].key, 7791 lun->per_res[i].res_key.key, 7792 ctl_min(sizeof(res_keys->keys[key_count].key), 7793 sizeof(lun->per_res[i].res_key))); 7794 key_count++; 7795 } 7796 break; 7797 } 7798 case SPRI_RR: { // read reservation 7799 struct scsi_per_res_in_rsrv *res; 7800 int tmp_len, header_only; 7801 7802 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7803 7804 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7805 7806 if (lun->flags & CTL_LUN_PR_RESERVED) 7807 { 7808 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7809 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7810 res->header.length); 7811 header_only = 0; 7812 } else { 7813 tmp_len = sizeof(struct scsi_per_res_in_header); 7814 scsi_ulto4b(0, res->header.length); 7815 header_only = 1; 7816 } 7817 7818 /* 7819 * We had to drop the lock to allocate our buffer, which 7820 * leaves time for someone to come in with another 7821 * persistent reservation. (That is unlikely, though, 7822 * since this should be the only persistent reservation 7823 * command active right now.) 7824 */ 7825 if (tmp_len != total_len) { 7826 mtx_unlock(&lun->lun_lock); 7827 free(ctsio->kern_data_ptr, M_CTL); 7828 printf("%s: reservation status changed, retrying\n", 7829 __func__); 7830 goto retry; 7831 } 7832 7833 /* 7834 * No reservation held, so we're done. 7835 */ 7836 if (header_only != 0) 7837 break; 7838 7839 /* 7840 * If the registration is an All Registrants type, the key 7841 * is 0, since it doesn't really matter. 7842 */ 7843 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7844 memcpy(res->data.reservation, 7845 &lun->per_res[lun->pr_res_idx].res_key, 7846 sizeof(struct scsi_per_res_key)); 7847 } 7848 res->data.scopetype = lun->res_type; 7849 break; 7850 } 7851 case SPRI_RC: //report capabilities 7852 { 7853 struct scsi_per_res_cap *res_cap; 7854 uint16_t type_mask; 7855 7856 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7857 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7858 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7859 type_mask = SPRI_TM_WR_EX_AR | 7860 SPRI_TM_EX_AC_RO | 7861 SPRI_TM_WR_EX_RO | 7862 SPRI_TM_EX_AC | 7863 SPRI_TM_WR_EX | 7864 SPRI_TM_EX_AC_AR; 7865 scsi_ulto2b(type_mask, res_cap->type_mask); 7866 break; 7867 } 7868 case SPRI_RS: { // read full status 7869 struct scsi_per_res_in_full *res_status; 7870 struct scsi_per_res_in_full_desc *res_desc; 7871 struct ctl_port *port; 7872 int i, len; 7873 7874 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7875 7876 /* 7877 * We had to drop the lock to allocate our buffer, which 7878 * leaves time for someone to come in with another 7879 * persistent reservation. (That is unlikely, though, 7880 * since this should be the only persistent reservation 7881 * command active right now.) 7882 */ 7883 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7884 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7885 lun->pr_key_count)){ 7886 mtx_unlock(&lun->lun_lock); 7887 free(ctsio->kern_data_ptr, M_CTL); 7888 printf("%s: reservation length changed, retrying\n", 7889 __func__); 7890 goto retry; 7891 } 7892 7893 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7894 7895 res_desc = &res_status->desc[0]; 7896 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7897 if (!lun->per_res[i].registered) 7898 continue; 7899 7900 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7901 sizeof(res_desc->res_key)); 7902 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7903 (lun->pr_res_idx == i || 7904 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7905 res_desc->flags = SPRI_FULL_R_HOLDER; 7906 res_desc->scopetype = lun->res_type; 7907 } 7908 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7909 res_desc->rel_trgt_port_id); 7910 len = 0; 7911 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7912 if (port != NULL) 7913 len = ctl_create_iid(port, 7914 i % CTL_MAX_INIT_PER_PORT, 7915 res_desc->transport_id); 7916 scsi_ulto4b(len, res_desc->additional_length); 7917 res_desc = (struct scsi_per_res_in_full_desc *) 7918 &res_desc->transport_id[len]; 7919 } 7920 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7921 res_status->header.length); 7922 break; 7923 } 7924 default: 7925 /* 7926 * This is a bug, because we just checked for this above, 7927 * and should have returned an error. 7928 */ 7929 panic("Invalid PR type %x", cdb->action); 7930 break; /* NOTREACHED */ 7931 } 7932 mtx_unlock(&lun->lun_lock); 7933 7934 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7935 ctsio->be_move_done = ctl_config_move_done; 7936 7937 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7938 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7939 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7940 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7941 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7942 7943 ctl_datamove((union ctl_io *)ctsio); 7944 7945 return (CTL_RETVAL_COMPLETE); 7946} 7947 7948/* 7949 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7950 * it should return. 7951 */ 7952static int 7953ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7954 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7955 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7956 struct scsi_per_res_out_parms* param) 7957{ 7958 union ctl_ha_msg persis_io; 7959 int retval, i; 7960 int isc_retval; 7961 7962 retval = 0; 7963 7964 mtx_lock(&lun->lun_lock); 7965 if (sa_res_key == 0) { 7966 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7967 /* validate scope and type */ 7968 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7969 SPR_LU_SCOPE) { 7970 mtx_unlock(&lun->lun_lock); 7971 ctl_set_invalid_field(/*ctsio*/ ctsio, 7972 /*sks_valid*/ 1, 7973 /*command*/ 1, 7974 /*field*/ 2, 7975 /*bit_valid*/ 1, 7976 /*bit*/ 4); 7977 ctl_done((union ctl_io *)ctsio); 7978 return (1); 7979 } 7980 7981 if (type>8 || type==2 || type==4 || type==0) { 7982 mtx_unlock(&lun->lun_lock); 7983 ctl_set_invalid_field(/*ctsio*/ ctsio, 7984 /*sks_valid*/ 1, 7985 /*command*/ 1, 7986 /*field*/ 2, 7987 /*bit_valid*/ 1, 7988 /*bit*/ 0); 7989 ctl_done((union ctl_io *)ctsio); 7990 return (1); 7991 } 7992 7993 /* temporarily unregister this nexus */ 7994 lun->per_res[residx].registered = 0; 7995 7996 /* 7997 * Unregister everybody else and build UA for 7998 * them 7999 */ 8000 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8001 if (lun->per_res[i].registered == 0) 8002 continue; 8003 8004 if (!persis_offset 8005 && i <CTL_MAX_INITIATORS) 8006 lun->pending_sense[i].ua_pending |= 8007 CTL_UA_REG_PREEMPT; 8008 else if (persis_offset 8009 && i >= persis_offset) 8010 lun->pending_sense[i-persis_offset 8011 ].ua_pending |= 8012 CTL_UA_REG_PREEMPT; 8013 lun->per_res[i].registered = 0; 8014 memset(&lun->per_res[i].res_key, 0, 8015 sizeof(struct scsi_per_res_key)); 8016 } 8017 lun->per_res[residx].registered = 1; 8018 lun->pr_key_count = 1; 8019 lun->res_type = type; 8020 if (lun->res_type != SPR_TYPE_WR_EX_AR 8021 && lun->res_type != SPR_TYPE_EX_AC_AR) 8022 lun->pr_res_idx = residx; 8023 8024 /* send msg to other side */ 8025 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8026 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8027 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8028 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8029 persis_io.pr.pr_info.res_type = type; 8030 memcpy(persis_io.pr.pr_info.sa_res_key, 8031 param->serv_act_res_key, 8032 sizeof(param->serv_act_res_key)); 8033 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8034 &persis_io, sizeof(persis_io), 0)) > 8035 CTL_HA_STATUS_SUCCESS) { 8036 printf("CTL:Persis Out error returned " 8037 "from ctl_ha_msg_send %d\n", 8038 isc_retval); 8039 } 8040 } else { 8041 /* not all registrants */ 8042 mtx_unlock(&lun->lun_lock); 8043 free(ctsio->kern_data_ptr, M_CTL); 8044 ctl_set_invalid_field(ctsio, 8045 /*sks_valid*/ 1, 8046 /*command*/ 0, 8047 /*field*/ 8, 8048 /*bit_valid*/ 0, 8049 /*bit*/ 0); 8050 ctl_done((union ctl_io *)ctsio); 8051 return (1); 8052 } 8053 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8054 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8055 int found = 0; 8056 8057 if (res_key == sa_res_key) { 8058 /* special case */ 8059 /* 8060 * The spec implies this is not good but doesn't 8061 * say what to do. There are two choices either 8062 * generate a res conflict or check condition 8063 * with illegal field in parameter data. Since 8064 * that is what is done when the sa_res_key is 8065 * zero I'll take that approach since this has 8066 * to do with the sa_res_key. 8067 */ 8068 mtx_unlock(&lun->lun_lock); 8069 free(ctsio->kern_data_ptr, M_CTL); 8070 ctl_set_invalid_field(ctsio, 8071 /*sks_valid*/ 1, 8072 /*command*/ 0, 8073 /*field*/ 8, 8074 /*bit_valid*/ 0, 8075 /*bit*/ 0); 8076 ctl_done((union ctl_io *)ctsio); 8077 return (1); 8078 } 8079 8080 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8081 if (lun->per_res[i].registered 8082 && memcmp(param->serv_act_res_key, 8083 lun->per_res[i].res_key.key, 8084 sizeof(struct scsi_per_res_key)) != 0) 8085 continue; 8086 8087 found = 1; 8088 lun->per_res[i].registered = 0; 8089 memset(&lun->per_res[i].res_key, 0, 8090 sizeof(struct scsi_per_res_key)); 8091 lun->pr_key_count--; 8092 8093 if (!persis_offset 8094 && i < CTL_MAX_INITIATORS) 8095 lun->pending_sense[i].ua_pending |= 8096 CTL_UA_REG_PREEMPT; 8097 else if (persis_offset 8098 && i >= persis_offset) 8099 lun->pending_sense[i-persis_offset].ua_pending|= 8100 CTL_UA_REG_PREEMPT; 8101 } 8102 if (!found) { 8103 mtx_unlock(&lun->lun_lock); 8104 free(ctsio->kern_data_ptr, M_CTL); 8105 ctl_set_reservation_conflict(ctsio); 8106 ctl_done((union ctl_io *)ctsio); 8107 return (CTL_RETVAL_COMPLETE); 8108 } 8109 /* send msg to other side */ 8110 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8111 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8112 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8113 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8114 persis_io.pr.pr_info.res_type = type; 8115 memcpy(persis_io.pr.pr_info.sa_res_key, 8116 param->serv_act_res_key, 8117 sizeof(param->serv_act_res_key)); 8118 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8119 &persis_io, sizeof(persis_io), 0)) > 8120 CTL_HA_STATUS_SUCCESS) { 8121 printf("CTL:Persis Out error returned from " 8122 "ctl_ha_msg_send %d\n", isc_retval); 8123 } 8124 } else { 8125 /* Reserved but not all registrants */ 8126 /* sa_res_key is res holder */ 8127 if (memcmp(param->serv_act_res_key, 8128 lun->per_res[lun->pr_res_idx].res_key.key, 8129 sizeof(struct scsi_per_res_key)) == 0) { 8130 /* validate scope and type */ 8131 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8132 SPR_LU_SCOPE) { 8133 mtx_unlock(&lun->lun_lock); 8134 ctl_set_invalid_field(/*ctsio*/ ctsio, 8135 /*sks_valid*/ 1, 8136 /*command*/ 1, 8137 /*field*/ 2, 8138 /*bit_valid*/ 1, 8139 /*bit*/ 4); 8140 ctl_done((union ctl_io *)ctsio); 8141 return (1); 8142 } 8143 8144 if (type>8 || type==2 || type==4 || type==0) { 8145 mtx_unlock(&lun->lun_lock); 8146 ctl_set_invalid_field(/*ctsio*/ ctsio, 8147 /*sks_valid*/ 1, 8148 /*command*/ 1, 8149 /*field*/ 2, 8150 /*bit_valid*/ 1, 8151 /*bit*/ 0); 8152 ctl_done((union ctl_io *)ctsio); 8153 return (1); 8154 } 8155 8156 /* 8157 * Do the following: 8158 * if sa_res_key != res_key remove all 8159 * registrants w/sa_res_key and generate UA 8160 * for these registrants(Registrations 8161 * Preempted) if it wasn't an exclusive 8162 * reservation generate UA(Reservations 8163 * Preempted) for all other registered nexuses 8164 * if the type has changed. Establish the new 8165 * reservation and holder. If res_key and 8166 * sa_res_key are the same do the above 8167 * except don't unregister the res holder. 8168 */ 8169 8170 /* 8171 * Temporarily unregister so it won't get 8172 * removed or UA generated 8173 */ 8174 lun->per_res[residx].registered = 0; 8175 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8176 if (lun->per_res[i].registered == 0) 8177 continue; 8178 8179 if (memcmp(param->serv_act_res_key, 8180 lun->per_res[i].res_key.key, 8181 sizeof(struct scsi_per_res_key)) == 0) { 8182 lun->per_res[i].registered = 0; 8183 memset(&lun->per_res[i].res_key, 8184 0, 8185 sizeof(struct scsi_per_res_key)); 8186 lun->pr_key_count--; 8187 8188 if (!persis_offset 8189 && i < CTL_MAX_INITIATORS) 8190 lun->pending_sense[i 8191 ].ua_pending |= 8192 CTL_UA_REG_PREEMPT; 8193 else if (persis_offset 8194 && i >= persis_offset) 8195 lun->pending_sense[ 8196 i-persis_offset].ua_pending |= 8197 CTL_UA_REG_PREEMPT; 8198 } else if (type != lun->res_type 8199 && (lun->res_type == SPR_TYPE_WR_EX_RO 8200 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8201 if (!persis_offset 8202 && i < CTL_MAX_INITIATORS) 8203 lun->pending_sense[i 8204 ].ua_pending |= 8205 CTL_UA_RES_RELEASE; 8206 else if (persis_offset 8207 && i >= persis_offset) 8208 lun->pending_sense[ 8209 i-persis_offset 8210 ].ua_pending |= 8211 CTL_UA_RES_RELEASE; 8212 } 8213 } 8214 lun->per_res[residx].registered = 1; 8215 lun->res_type = type; 8216 if (lun->res_type != SPR_TYPE_WR_EX_AR 8217 && lun->res_type != SPR_TYPE_EX_AC_AR) 8218 lun->pr_res_idx = residx; 8219 else 8220 lun->pr_res_idx = 8221 CTL_PR_ALL_REGISTRANTS; 8222 8223 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8224 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8225 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8226 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8227 persis_io.pr.pr_info.res_type = type; 8228 memcpy(persis_io.pr.pr_info.sa_res_key, 8229 param->serv_act_res_key, 8230 sizeof(param->serv_act_res_key)); 8231 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8232 &persis_io, sizeof(persis_io), 0)) > 8233 CTL_HA_STATUS_SUCCESS) { 8234 printf("CTL:Persis Out error returned " 8235 "from ctl_ha_msg_send %d\n", 8236 isc_retval); 8237 } 8238 } else { 8239 /* 8240 * sa_res_key is not the res holder just 8241 * remove registrants 8242 */ 8243 int found=0; 8244 8245 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8246 if (memcmp(param->serv_act_res_key, 8247 lun->per_res[i].res_key.key, 8248 sizeof(struct scsi_per_res_key)) != 0) 8249 continue; 8250 8251 found = 1; 8252 lun->per_res[i].registered = 0; 8253 memset(&lun->per_res[i].res_key, 0, 8254 sizeof(struct scsi_per_res_key)); 8255 lun->pr_key_count--; 8256 8257 if (!persis_offset 8258 && i < CTL_MAX_INITIATORS) 8259 lun->pending_sense[i].ua_pending |= 8260 CTL_UA_REG_PREEMPT; 8261 else if (persis_offset 8262 && i >= persis_offset) 8263 lun->pending_sense[ 8264 i-persis_offset].ua_pending |= 8265 CTL_UA_REG_PREEMPT; 8266 } 8267 8268 if (!found) { 8269 mtx_unlock(&lun->lun_lock); 8270 free(ctsio->kern_data_ptr, M_CTL); 8271 ctl_set_reservation_conflict(ctsio); 8272 ctl_done((union ctl_io *)ctsio); 8273 return (1); 8274 } 8275 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8276 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8277 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8278 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8279 persis_io.pr.pr_info.res_type = type; 8280 memcpy(persis_io.pr.pr_info.sa_res_key, 8281 param->serv_act_res_key, 8282 sizeof(param->serv_act_res_key)); 8283 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8284 &persis_io, sizeof(persis_io), 0)) > 8285 CTL_HA_STATUS_SUCCESS) { 8286 printf("CTL:Persis Out error returned " 8287 "from ctl_ha_msg_send %d\n", 8288 isc_retval); 8289 } 8290 } 8291 } 8292 8293 lun->PRGeneration++; 8294 mtx_unlock(&lun->lun_lock); 8295 8296 return (retval); 8297} 8298 8299static void 8300ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8301{ 8302 int i; 8303 8304 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8305 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8306 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8307 msg->pr.pr_info.sa_res_key, 8308 sizeof(struct scsi_per_res_key)) != 0) { 8309 uint64_t sa_res_key; 8310 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8311 8312 if (sa_res_key == 0) { 8313 /* temporarily unregister this nexus */ 8314 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8315 8316 /* 8317 * Unregister everybody else and build UA for 8318 * them 8319 */ 8320 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8321 if (lun->per_res[i].registered == 0) 8322 continue; 8323 8324 if (!persis_offset 8325 && i < CTL_MAX_INITIATORS) 8326 lun->pending_sense[i].ua_pending |= 8327 CTL_UA_REG_PREEMPT; 8328 else if (persis_offset && i >= persis_offset) 8329 lun->pending_sense[i - 8330 persis_offset].ua_pending |= 8331 CTL_UA_REG_PREEMPT; 8332 lun->per_res[i].registered = 0; 8333 memset(&lun->per_res[i].res_key, 0, 8334 sizeof(struct scsi_per_res_key)); 8335 } 8336 8337 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8338 lun->pr_key_count = 1; 8339 lun->res_type = msg->pr.pr_info.res_type; 8340 if (lun->res_type != SPR_TYPE_WR_EX_AR 8341 && lun->res_type != SPR_TYPE_EX_AC_AR) 8342 lun->pr_res_idx = msg->pr.pr_info.residx; 8343 } else { 8344 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8345 if (memcmp(msg->pr.pr_info.sa_res_key, 8346 lun->per_res[i].res_key.key, 8347 sizeof(struct scsi_per_res_key)) != 0) 8348 continue; 8349 8350 lun->per_res[i].registered = 0; 8351 memset(&lun->per_res[i].res_key, 0, 8352 sizeof(struct scsi_per_res_key)); 8353 lun->pr_key_count--; 8354 8355 if (!persis_offset 8356 && i < persis_offset) 8357 lun->pending_sense[i].ua_pending |= 8358 CTL_UA_REG_PREEMPT; 8359 else if (persis_offset 8360 && i >= persis_offset) 8361 lun->pending_sense[i - 8362 persis_offset].ua_pending |= 8363 CTL_UA_REG_PREEMPT; 8364 } 8365 } 8366 } else { 8367 /* 8368 * Temporarily unregister so it won't get removed 8369 * or UA generated 8370 */ 8371 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8372 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8373 if (lun->per_res[i].registered == 0) 8374 continue; 8375 8376 if (memcmp(msg->pr.pr_info.sa_res_key, 8377 lun->per_res[i].res_key.key, 8378 sizeof(struct scsi_per_res_key)) == 0) { 8379 lun->per_res[i].registered = 0; 8380 memset(&lun->per_res[i].res_key, 0, 8381 sizeof(struct scsi_per_res_key)); 8382 lun->pr_key_count--; 8383 if (!persis_offset 8384 && i < CTL_MAX_INITIATORS) 8385 lun->pending_sense[i].ua_pending |= 8386 CTL_UA_REG_PREEMPT; 8387 else if (persis_offset 8388 && i >= persis_offset) 8389 lun->pending_sense[i - 8390 persis_offset].ua_pending |= 8391 CTL_UA_REG_PREEMPT; 8392 } else if (msg->pr.pr_info.res_type != lun->res_type 8393 && (lun->res_type == SPR_TYPE_WR_EX_RO 8394 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8395 if (!persis_offset 8396 && i < persis_offset) 8397 lun->pending_sense[i 8398 ].ua_pending |= 8399 CTL_UA_RES_RELEASE; 8400 else if (persis_offset 8401 && i >= persis_offset) 8402 lun->pending_sense[i - 8403 persis_offset].ua_pending |= 8404 CTL_UA_RES_RELEASE; 8405 } 8406 } 8407 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8408 lun->res_type = msg->pr.pr_info.res_type; 8409 if (lun->res_type != SPR_TYPE_WR_EX_AR 8410 && lun->res_type != SPR_TYPE_EX_AC_AR) 8411 lun->pr_res_idx = msg->pr.pr_info.residx; 8412 else 8413 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8414 } 8415 lun->PRGeneration++; 8416 8417} 8418 8419 8420int 8421ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8422{ 8423 int retval; 8424 int isc_retval; 8425 u_int32_t param_len; 8426 struct scsi_per_res_out *cdb; 8427 struct ctl_lun *lun; 8428 struct scsi_per_res_out_parms* param; 8429 struct ctl_softc *softc; 8430 uint32_t residx; 8431 uint64_t res_key, sa_res_key; 8432 uint8_t type; 8433 union ctl_ha_msg persis_io; 8434 int i; 8435 8436 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8437 8438 retval = CTL_RETVAL_COMPLETE; 8439 8440 softc = control_softc; 8441 8442 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8443 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8444 8445 /* 8446 * We only support whole-LUN scope. The scope & type are ignored for 8447 * register, register and ignore existing key and clear. 8448 * We sometimes ignore scope and type on preempts too!! 8449 * Verify reservation type here as well. 8450 */ 8451 type = cdb->scope_type & SPR_TYPE_MASK; 8452 if ((cdb->action == SPRO_RESERVE) 8453 || (cdb->action == SPRO_RELEASE)) { 8454 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8455 ctl_set_invalid_field(/*ctsio*/ ctsio, 8456 /*sks_valid*/ 1, 8457 /*command*/ 1, 8458 /*field*/ 2, 8459 /*bit_valid*/ 1, 8460 /*bit*/ 4); 8461 ctl_done((union ctl_io *)ctsio); 8462 return (CTL_RETVAL_COMPLETE); 8463 } 8464 8465 if (type>8 || type==2 || type==4 || type==0) { 8466 ctl_set_invalid_field(/*ctsio*/ ctsio, 8467 /*sks_valid*/ 1, 8468 /*command*/ 1, 8469 /*field*/ 2, 8470 /*bit_valid*/ 1, 8471 /*bit*/ 0); 8472 ctl_done((union ctl_io *)ctsio); 8473 return (CTL_RETVAL_COMPLETE); 8474 } 8475 } 8476 8477 param_len = scsi_4btoul(cdb->length); 8478 8479 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8480 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8481 ctsio->kern_data_len = param_len; 8482 ctsio->kern_total_len = param_len; 8483 ctsio->kern_data_resid = 0; 8484 ctsio->kern_rel_offset = 0; 8485 ctsio->kern_sg_entries = 0; 8486 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8487 ctsio->be_move_done = ctl_config_move_done; 8488 ctl_datamove((union ctl_io *)ctsio); 8489 8490 return (CTL_RETVAL_COMPLETE); 8491 } 8492 8493 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8494 8495 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8496 res_key = scsi_8btou64(param->res_key.key); 8497 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8498 8499 /* 8500 * Validate the reservation key here except for SPRO_REG_IGNO 8501 * This must be done for all other service actions 8502 */ 8503 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8504 mtx_lock(&lun->lun_lock); 8505 if (lun->per_res[residx].registered) { 8506 if (memcmp(param->res_key.key, 8507 lun->per_res[residx].res_key.key, 8508 ctl_min(sizeof(param->res_key), 8509 sizeof(lun->per_res[residx].res_key))) != 0) { 8510 /* 8511 * The current key passed in doesn't match 8512 * the one the initiator previously 8513 * registered. 8514 */ 8515 mtx_unlock(&lun->lun_lock); 8516 free(ctsio->kern_data_ptr, M_CTL); 8517 ctl_set_reservation_conflict(ctsio); 8518 ctl_done((union ctl_io *)ctsio); 8519 return (CTL_RETVAL_COMPLETE); 8520 } 8521 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8522 /* 8523 * We are not registered 8524 */ 8525 mtx_unlock(&lun->lun_lock); 8526 free(ctsio->kern_data_ptr, M_CTL); 8527 ctl_set_reservation_conflict(ctsio); 8528 ctl_done((union ctl_io *)ctsio); 8529 return (CTL_RETVAL_COMPLETE); 8530 } else if (res_key != 0) { 8531 /* 8532 * We are not registered and trying to register but 8533 * the register key isn't zero. 8534 */ 8535 mtx_unlock(&lun->lun_lock); 8536 free(ctsio->kern_data_ptr, M_CTL); 8537 ctl_set_reservation_conflict(ctsio); 8538 ctl_done((union ctl_io *)ctsio); 8539 return (CTL_RETVAL_COMPLETE); 8540 } 8541 mtx_unlock(&lun->lun_lock); 8542 } 8543 8544 switch (cdb->action & SPRO_ACTION_MASK) { 8545 case SPRO_REGISTER: 8546 case SPRO_REG_IGNO: { 8547 8548#if 0 8549 printf("Registration received\n"); 8550#endif 8551 8552 /* 8553 * We don't support any of these options, as we report in 8554 * the read capabilities request (see 8555 * ctl_persistent_reserve_in(), above). 8556 */ 8557 if ((param->flags & SPR_SPEC_I_PT) 8558 || (param->flags & SPR_ALL_TG_PT) 8559 || (param->flags & SPR_APTPL)) { 8560 int bit_ptr; 8561 8562 if (param->flags & SPR_APTPL) 8563 bit_ptr = 0; 8564 else if (param->flags & SPR_ALL_TG_PT) 8565 bit_ptr = 2; 8566 else /* SPR_SPEC_I_PT */ 8567 bit_ptr = 3; 8568 8569 free(ctsio->kern_data_ptr, M_CTL); 8570 ctl_set_invalid_field(ctsio, 8571 /*sks_valid*/ 1, 8572 /*command*/ 0, 8573 /*field*/ 20, 8574 /*bit_valid*/ 1, 8575 /*bit*/ bit_ptr); 8576 ctl_done((union ctl_io *)ctsio); 8577 return (CTL_RETVAL_COMPLETE); 8578 } 8579 8580 mtx_lock(&lun->lun_lock); 8581 8582 /* 8583 * The initiator wants to clear the 8584 * key/unregister. 8585 */ 8586 if (sa_res_key == 0) { 8587 if ((res_key == 0 8588 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8589 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8590 && !lun->per_res[residx].registered)) { 8591 mtx_unlock(&lun->lun_lock); 8592 goto done; 8593 } 8594 8595 lun->per_res[residx].registered = 0; 8596 memset(&lun->per_res[residx].res_key, 8597 0, sizeof(lun->per_res[residx].res_key)); 8598 lun->pr_key_count--; 8599 8600 if (residx == lun->pr_res_idx) { 8601 lun->flags &= ~CTL_LUN_PR_RESERVED; 8602 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8603 8604 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8605 || lun->res_type == SPR_TYPE_EX_AC_RO) 8606 && lun->pr_key_count) { 8607 /* 8608 * If the reservation is a registrants 8609 * only type we need to generate a UA 8610 * for other registered inits. The 8611 * sense code should be RESERVATIONS 8612 * RELEASED 8613 */ 8614 8615 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8616 if (lun->per_res[ 8617 i+persis_offset].registered 8618 == 0) 8619 continue; 8620 lun->pending_sense[i 8621 ].ua_pending |= 8622 CTL_UA_RES_RELEASE; 8623 } 8624 } 8625 lun->res_type = 0; 8626 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8627 if (lun->pr_key_count==0) { 8628 lun->flags &= ~CTL_LUN_PR_RESERVED; 8629 lun->res_type = 0; 8630 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8631 } 8632 } 8633 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8634 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8635 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8636 persis_io.pr.pr_info.residx = residx; 8637 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8638 &persis_io, sizeof(persis_io), 0 )) > 8639 CTL_HA_STATUS_SUCCESS) { 8640 printf("CTL:Persis Out error returned from " 8641 "ctl_ha_msg_send %d\n", isc_retval); 8642 } 8643 } else /* sa_res_key != 0 */ { 8644 8645 /* 8646 * If we aren't registered currently then increment 8647 * the key count and set the registered flag. 8648 */ 8649 if (!lun->per_res[residx].registered) { 8650 lun->pr_key_count++; 8651 lun->per_res[residx].registered = 1; 8652 } 8653 8654 memcpy(&lun->per_res[residx].res_key, 8655 param->serv_act_res_key, 8656 ctl_min(sizeof(param->serv_act_res_key), 8657 sizeof(lun->per_res[residx].res_key))); 8658 8659 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8660 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8661 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8662 persis_io.pr.pr_info.residx = residx; 8663 memcpy(persis_io.pr.pr_info.sa_res_key, 8664 param->serv_act_res_key, 8665 sizeof(param->serv_act_res_key)); 8666 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8667 &persis_io, sizeof(persis_io), 0)) > 8668 CTL_HA_STATUS_SUCCESS) { 8669 printf("CTL:Persis Out error returned from " 8670 "ctl_ha_msg_send %d\n", isc_retval); 8671 } 8672 } 8673 lun->PRGeneration++; 8674 mtx_unlock(&lun->lun_lock); 8675 8676 break; 8677 } 8678 case SPRO_RESERVE: 8679#if 0 8680 printf("Reserve executed type %d\n", type); 8681#endif 8682 mtx_lock(&lun->lun_lock); 8683 if (lun->flags & CTL_LUN_PR_RESERVED) { 8684 /* 8685 * if this isn't the reservation holder and it's 8686 * not a "all registrants" type or if the type is 8687 * different then we have a conflict 8688 */ 8689 if ((lun->pr_res_idx != residx 8690 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8691 || lun->res_type != type) { 8692 mtx_unlock(&lun->lun_lock); 8693 free(ctsio->kern_data_ptr, M_CTL); 8694 ctl_set_reservation_conflict(ctsio); 8695 ctl_done((union ctl_io *)ctsio); 8696 return (CTL_RETVAL_COMPLETE); 8697 } 8698 mtx_unlock(&lun->lun_lock); 8699 } else /* create a reservation */ { 8700 /* 8701 * If it's not an "all registrants" type record 8702 * reservation holder 8703 */ 8704 if (type != SPR_TYPE_WR_EX_AR 8705 && type != SPR_TYPE_EX_AC_AR) 8706 lun->pr_res_idx = residx; /* Res holder */ 8707 else 8708 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8709 8710 lun->flags |= CTL_LUN_PR_RESERVED; 8711 lun->res_type = type; 8712 8713 mtx_unlock(&lun->lun_lock); 8714 8715 /* send msg to other side */ 8716 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8717 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8718 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8719 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8720 persis_io.pr.pr_info.res_type = type; 8721 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8722 &persis_io, sizeof(persis_io), 0)) > 8723 CTL_HA_STATUS_SUCCESS) { 8724 printf("CTL:Persis Out error returned from " 8725 "ctl_ha_msg_send %d\n", isc_retval); 8726 } 8727 } 8728 break; 8729 8730 case SPRO_RELEASE: 8731 mtx_lock(&lun->lun_lock); 8732 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8733 /* No reservation exists return good status */ 8734 mtx_unlock(&lun->lun_lock); 8735 goto done; 8736 } 8737 /* 8738 * Is this nexus a reservation holder? 8739 */ 8740 if (lun->pr_res_idx != residx 8741 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8742 /* 8743 * not a res holder return good status but 8744 * do nothing 8745 */ 8746 mtx_unlock(&lun->lun_lock); 8747 goto done; 8748 } 8749 8750 if (lun->res_type != type) { 8751 mtx_unlock(&lun->lun_lock); 8752 free(ctsio->kern_data_ptr, M_CTL); 8753 ctl_set_illegal_pr_release(ctsio); 8754 ctl_done((union ctl_io *)ctsio); 8755 return (CTL_RETVAL_COMPLETE); 8756 } 8757 8758 /* okay to release */ 8759 lun->flags &= ~CTL_LUN_PR_RESERVED; 8760 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8761 lun->res_type = 0; 8762 8763 /* 8764 * if this isn't an exclusive access 8765 * res generate UA for all other 8766 * registrants. 8767 */ 8768 if (type != SPR_TYPE_EX_AC 8769 && type != SPR_TYPE_WR_EX) { 8770 /* 8771 * temporarily unregister so we don't generate UA 8772 */ 8773 lun->per_res[residx].registered = 0; 8774 8775 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8776 if (lun->per_res[i+persis_offset].registered 8777 == 0) 8778 continue; 8779 lun->pending_sense[i].ua_pending |= 8780 CTL_UA_RES_RELEASE; 8781 } 8782 8783 lun->per_res[residx].registered = 1; 8784 } 8785 mtx_unlock(&lun->lun_lock); 8786 /* Send msg to other side */ 8787 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8788 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8789 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8790 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8791 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8792 printf("CTL:Persis Out error returned from " 8793 "ctl_ha_msg_send %d\n", isc_retval); 8794 } 8795 break; 8796 8797 case SPRO_CLEAR: 8798 /* send msg to other side */ 8799 8800 mtx_lock(&lun->lun_lock); 8801 lun->flags &= ~CTL_LUN_PR_RESERVED; 8802 lun->res_type = 0; 8803 lun->pr_key_count = 0; 8804 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8805 8806 8807 memset(&lun->per_res[residx].res_key, 8808 0, sizeof(lun->per_res[residx].res_key)); 8809 lun->per_res[residx].registered = 0; 8810 8811 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8812 if (lun->per_res[i].registered) { 8813 if (!persis_offset && i < CTL_MAX_INITIATORS) 8814 lun->pending_sense[i].ua_pending |= 8815 CTL_UA_RES_PREEMPT; 8816 else if (persis_offset && i >= persis_offset) 8817 lun->pending_sense[i-persis_offset 8818 ].ua_pending |= CTL_UA_RES_PREEMPT; 8819 8820 memset(&lun->per_res[i].res_key, 8821 0, sizeof(struct scsi_per_res_key)); 8822 lun->per_res[i].registered = 0; 8823 } 8824 lun->PRGeneration++; 8825 mtx_unlock(&lun->lun_lock); 8826 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8827 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8828 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8829 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8830 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8831 printf("CTL:Persis Out error returned from " 8832 "ctl_ha_msg_send %d\n", isc_retval); 8833 } 8834 break; 8835 8836 case SPRO_PREEMPT: { 8837 int nretval; 8838 8839 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8840 residx, ctsio, cdb, param); 8841 if (nretval != 0) 8842 return (CTL_RETVAL_COMPLETE); 8843 break; 8844 } 8845 default: 8846 panic("Invalid PR type %x", cdb->action); 8847 } 8848 8849done: 8850 free(ctsio->kern_data_ptr, M_CTL); 8851 ctl_set_success(ctsio); 8852 ctl_done((union ctl_io *)ctsio); 8853 8854 return (retval); 8855} 8856 8857/* 8858 * This routine is for handling a message from the other SC pertaining to 8859 * persistent reserve out. All the error checking will have been done 8860 * so only perorming the action need be done here to keep the two 8861 * in sync. 8862 */ 8863static void 8864ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8865{ 8866 struct ctl_lun *lun; 8867 struct ctl_softc *softc; 8868 int i; 8869 uint32_t targ_lun; 8870 8871 softc = control_softc; 8872 8873 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8874 lun = softc->ctl_luns[targ_lun]; 8875 mtx_lock(&lun->lun_lock); 8876 switch(msg->pr.pr_info.action) { 8877 case CTL_PR_REG_KEY: 8878 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8879 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8880 lun->pr_key_count++; 8881 } 8882 lun->PRGeneration++; 8883 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8884 msg->pr.pr_info.sa_res_key, 8885 sizeof(struct scsi_per_res_key)); 8886 break; 8887 8888 case CTL_PR_UNREG_KEY: 8889 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8890 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8891 0, sizeof(struct scsi_per_res_key)); 8892 lun->pr_key_count--; 8893 8894 /* XXX Need to see if the reservation has been released */ 8895 /* if so do we need to generate UA? */ 8896 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8897 lun->flags &= ~CTL_LUN_PR_RESERVED; 8898 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8899 8900 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8901 || lun->res_type == SPR_TYPE_EX_AC_RO) 8902 && lun->pr_key_count) { 8903 /* 8904 * If the reservation is a registrants 8905 * only type we need to generate a UA 8906 * for other registered inits. The 8907 * sense code should be RESERVATIONS 8908 * RELEASED 8909 */ 8910 8911 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8912 if (lun->per_res[i+ 8913 persis_offset].registered == 0) 8914 continue; 8915 8916 lun->pending_sense[i 8917 ].ua_pending |= 8918 CTL_UA_RES_RELEASE; 8919 } 8920 } 8921 lun->res_type = 0; 8922 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8923 if (lun->pr_key_count==0) { 8924 lun->flags &= ~CTL_LUN_PR_RESERVED; 8925 lun->res_type = 0; 8926 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8927 } 8928 } 8929 lun->PRGeneration++; 8930 break; 8931 8932 case CTL_PR_RESERVE: 8933 lun->flags |= CTL_LUN_PR_RESERVED; 8934 lun->res_type = msg->pr.pr_info.res_type; 8935 lun->pr_res_idx = msg->pr.pr_info.residx; 8936 8937 break; 8938 8939 case CTL_PR_RELEASE: 8940 /* 8941 * if this isn't an exclusive access res generate UA for all 8942 * other registrants. 8943 */ 8944 if (lun->res_type != SPR_TYPE_EX_AC 8945 && lun->res_type != SPR_TYPE_WR_EX) { 8946 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8947 if (lun->per_res[i+persis_offset].registered) 8948 lun->pending_sense[i].ua_pending |= 8949 CTL_UA_RES_RELEASE; 8950 } 8951 8952 lun->flags &= ~CTL_LUN_PR_RESERVED; 8953 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8954 lun->res_type = 0; 8955 break; 8956 8957 case CTL_PR_PREEMPT: 8958 ctl_pro_preempt_other(lun, msg); 8959 break; 8960 case CTL_PR_CLEAR: 8961 lun->flags &= ~CTL_LUN_PR_RESERVED; 8962 lun->res_type = 0; 8963 lun->pr_key_count = 0; 8964 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8965 8966 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8967 if (lun->per_res[i].registered == 0) 8968 continue; 8969 if (!persis_offset 8970 && i < CTL_MAX_INITIATORS) 8971 lun->pending_sense[i].ua_pending |= 8972 CTL_UA_RES_PREEMPT; 8973 else if (persis_offset 8974 && i >= persis_offset) 8975 lun->pending_sense[i-persis_offset].ua_pending|= 8976 CTL_UA_RES_PREEMPT; 8977 memset(&lun->per_res[i].res_key, 0, 8978 sizeof(struct scsi_per_res_key)); 8979 lun->per_res[i].registered = 0; 8980 } 8981 lun->PRGeneration++; 8982 break; 8983 } 8984 8985 mtx_unlock(&lun->lun_lock); 8986} 8987 8988int 8989ctl_read_write(struct ctl_scsiio *ctsio) 8990{ 8991 struct ctl_lun *lun; 8992 struct ctl_lba_len_flags *lbalen; 8993 uint64_t lba; 8994 uint32_t num_blocks; 8995 int fua, dpo; 8996 int retval; 8997 int isread; 8998 8999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9000 9001 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9002 9003 fua = 0; 9004 dpo = 0; 9005 9006 retval = CTL_RETVAL_COMPLETE; 9007 9008 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9009 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9010 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9011 uint32_t residx; 9012 9013 /* 9014 * XXX KDM need a lock here. 9015 */ 9016 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9017 if ((lun->res_type == SPR_TYPE_EX_AC 9018 && residx != lun->pr_res_idx) 9019 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9020 || lun->res_type == SPR_TYPE_EX_AC_AR) 9021 && !lun->per_res[residx].registered)) { 9022 ctl_set_reservation_conflict(ctsio); 9023 ctl_done((union ctl_io *)ctsio); 9024 return (CTL_RETVAL_COMPLETE); 9025 } 9026 } 9027 9028 switch (ctsio->cdb[0]) { 9029 case READ_6: 9030 case WRITE_6: { 9031 struct scsi_rw_6 *cdb; 9032 9033 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9034 9035 lba = scsi_3btoul(cdb->addr); 9036 /* only 5 bits are valid in the most significant address byte */ 9037 lba &= 0x1fffff; 9038 num_blocks = cdb->length; 9039 /* 9040 * This is correct according to SBC-2. 9041 */ 9042 if (num_blocks == 0) 9043 num_blocks = 256; 9044 break; 9045 } 9046 case READ_10: 9047 case WRITE_10: { 9048 struct scsi_rw_10 *cdb; 9049 9050 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9051 9052 if (cdb->byte2 & SRW10_FUA) 9053 fua = 1; 9054 if (cdb->byte2 & SRW10_DPO) 9055 dpo = 1; 9056 9057 lba = scsi_4btoul(cdb->addr); 9058 num_blocks = scsi_2btoul(cdb->length); 9059 break; 9060 } 9061 case WRITE_VERIFY_10: { 9062 struct scsi_write_verify_10 *cdb; 9063 9064 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9065 9066 /* 9067 * XXX KDM we should do actual write verify support at some 9068 * point. This is obviously fake, we're just translating 9069 * things to a write. So we don't even bother checking the 9070 * BYTCHK field, since we don't do any verification. If 9071 * the user asks for it, we'll just pretend we did it. 9072 */ 9073 if (cdb->byte2 & SWV_DPO) 9074 dpo = 1; 9075 9076 lba = scsi_4btoul(cdb->addr); 9077 num_blocks = scsi_2btoul(cdb->length); 9078 break; 9079 } 9080 case READ_12: 9081 case WRITE_12: { 9082 struct scsi_rw_12 *cdb; 9083 9084 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9085 9086 if (cdb->byte2 & SRW12_FUA) 9087 fua = 1; 9088 if (cdb->byte2 & SRW12_DPO) 9089 dpo = 1; 9090 lba = scsi_4btoul(cdb->addr); 9091 num_blocks = scsi_4btoul(cdb->length); 9092 break; 9093 } 9094 case WRITE_VERIFY_12: { 9095 struct scsi_write_verify_12 *cdb; 9096 9097 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9098 9099 if (cdb->byte2 & SWV_DPO) 9100 dpo = 1; 9101 9102 lba = scsi_4btoul(cdb->addr); 9103 num_blocks = scsi_4btoul(cdb->length); 9104 9105 break; 9106 } 9107 case READ_16: 9108 case WRITE_16: { 9109 struct scsi_rw_16 *cdb; 9110 9111 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9112 9113 if (cdb->byte2 & SRW12_FUA) 9114 fua = 1; 9115 if (cdb->byte2 & SRW12_DPO) 9116 dpo = 1; 9117 9118 lba = scsi_8btou64(cdb->addr); 9119 num_blocks = scsi_4btoul(cdb->length); 9120 break; 9121 } 9122 case WRITE_VERIFY_16: { 9123 struct scsi_write_verify_16 *cdb; 9124 9125 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9126 9127 if (cdb->byte2 & SWV_DPO) 9128 dpo = 1; 9129 9130 lba = scsi_8btou64(cdb->addr); 9131 num_blocks = scsi_4btoul(cdb->length); 9132 break; 9133 } 9134 default: 9135 /* 9136 * We got a command we don't support. This shouldn't 9137 * happen, commands should be filtered out above us. 9138 */ 9139 ctl_set_invalid_opcode(ctsio); 9140 ctl_done((union ctl_io *)ctsio); 9141 9142 return (CTL_RETVAL_COMPLETE); 9143 break; /* NOTREACHED */ 9144 } 9145 9146 /* 9147 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9148 * interesting for us, but if RAIDCore is in write-back mode, 9149 * getting it to do write-through for a particular transaction may 9150 * not be possible. 9151 */ 9152 9153 /* 9154 * The first check is to make sure we're in bounds, the second 9155 * check is to catch wrap-around problems. If the lba + num blocks 9156 * is less than the lba, then we've wrapped around and the block 9157 * range is invalid anyway. 9158 */ 9159 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9160 || ((lba + num_blocks) < lba)) { 9161 ctl_set_lba_out_of_range(ctsio); 9162 ctl_done((union ctl_io *)ctsio); 9163 return (CTL_RETVAL_COMPLETE); 9164 } 9165 9166 /* 9167 * According to SBC-3, a transfer length of 0 is not an error. 9168 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9169 * translates to 256 blocks for those commands. 9170 */ 9171 if (num_blocks == 0) { 9172 ctl_set_success(ctsio); 9173 ctl_done((union ctl_io *)ctsio); 9174 return (CTL_RETVAL_COMPLETE); 9175 } 9176 9177 lbalen = (struct ctl_lba_len_flags *) 9178 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9179 lbalen->lba = lba; 9180 lbalen->len = num_blocks; 9181 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 9182 9183 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9184 ctsio->kern_rel_offset = 0; 9185 9186 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9187 9188 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9189 9190 return (retval); 9191} 9192 9193static int 9194ctl_cnw_cont(union ctl_io *io) 9195{ 9196 struct ctl_scsiio *ctsio; 9197 struct ctl_lun *lun; 9198 struct ctl_lba_len_flags *lbalen; 9199 int retval; 9200 9201 ctsio = &io->scsiio; 9202 ctsio->io_hdr.status = CTL_STATUS_NONE; 9203 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9204 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9205 lbalen = (struct ctl_lba_len_flags *) 9206 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9207 lbalen->flags = CTL_LLF_WRITE; 9208 9209 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9210 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9211 return (retval); 9212} 9213 9214int 9215ctl_cnw(struct ctl_scsiio *ctsio) 9216{ 9217 struct ctl_lun *lun; 9218 struct ctl_lba_len_flags *lbalen; 9219 uint64_t lba; 9220 uint32_t num_blocks; 9221 int fua, dpo; 9222 int retval; 9223 9224 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9225 9226 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9227 9228 fua = 0; 9229 dpo = 0; 9230 9231 retval = CTL_RETVAL_COMPLETE; 9232 9233 switch (ctsio->cdb[0]) { 9234 case COMPARE_AND_WRITE: { 9235 struct scsi_compare_and_write *cdb; 9236 9237 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9238 9239 if (cdb->byte2 & SRW10_FUA) 9240 fua = 1; 9241 if (cdb->byte2 & SRW10_DPO) 9242 dpo = 1; 9243 lba = scsi_8btou64(cdb->addr); 9244 num_blocks = cdb->length; 9245 break; 9246 } 9247 default: 9248 /* 9249 * We got a command we don't support. This shouldn't 9250 * happen, commands should be filtered out above us. 9251 */ 9252 ctl_set_invalid_opcode(ctsio); 9253 ctl_done((union ctl_io *)ctsio); 9254 9255 return (CTL_RETVAL_COMPLETE); 9256 break; /* NOTREACHED */ 9257 } 9258 9259 /* 9260 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 9261 * interesting for us, but if RAIDCore is in write-back mode, 9262 * getting it to do write-through for a particular transaction may 9263 * not be possible. 9264 */ 9265 9266 /* 9267 * The first check is to make sure we're in bounds, the second 9268 * check is to catch wrap-around problems. If the lba + num blocks 9269 * is less than the lba, then we've wrapped around and the block 9270 * range is invalid anyway. 9271 */ 9272 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9273 || ((lba + num_blocks) < lba)) { 9274 ctl_set_lba_out_of_range(ctsio); 9275 ctl_done((union ctl_io *)ctsio); 9276 return (CTL_RETVAL_COMPLETE); 9277 } 9278 9279 /* 9280 * According to SBC-3, a transfer length of 0 is not an error. 9281 */ 9282 if (num_blocks == 0) { 9283 ctl_set_success(ctsio); 9284 ctl_done((union ctl_io *)ctsio); 9285 return (CTL_RETVAL_COMPLETE); 9286 } 9287 9288 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9289 ctsio->kern_rel_offset = 0; 9290 9291 /* 9292 * Set the IO_CONT flag, so that if this I/O gets passed to 9293 * ctl_data_submit_done(), it'll get passed back to 9294 * ctl_ctl_cnw_cont() for further processing. 9295 */ 9296 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9297 ctsio->io_cont = ctl_cnw_cont; 9298 9299 lbalen = (struct ctl_lba_len_flags *) 9300 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9301 lbalen->lba = lba; 9302 lbalen->len = num_blocks; 9303 lbalen->flags = CTL_LLF_COMPARE; 9304 9305 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9306 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9307 return (retval); 9308} 9309 9310int 9311ctl_verify(struct ctl_scsiio *ctsio) 9312{ 9313 struct ctl_lun *lun; 9314 struct ctl_lba_len_flags *lbalen; 9315 uint64_t lba; 9316 uint32_t num_blocks; 9317 int bytchk, dpo; 9318 int retval; 9319 9320 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9321 9322 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9323 9324 bytchk = 0; 9325 dpo = 0; 9326 retval = CTL_RETVAL_COMPLETE; 9327 9328 switch (ctsio->cdb[0]) { 9329 case VERIFY_10: { 9330 struct scsi_verify_10 *cdb; 9331 9332 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9333 if (cdb->byte2 & SVFY_BYTCHK) 9334 bytchk = 1; 9335 if (cdb->byte2 & SVFY_DPO) 9336 dpo = 1; 9337 lba = scsi_4btoul(cdb->addr); 9338 num_blocks = scsi_2btoul(cdb->length); 9339 break; 9340 } 9341 case VERIFY_12: { 9342 struct scsi_verify_12 *cdb; 9343 9344 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9345 if (cdb->byte2 & SVFY_BYTCHK) 9346 bytchk = 1; 9347 if (cdb->byte2 & SVFY_DPO) 9348 dpo = 1; 9349 lba = scsi_4btoul(cdb->addr); 9350 num_blocks = scsi_4btoul(cdb->length); 9351 break; 9352 } 9353 case VERIFY_16: { 9354 struct scsi_rw_16 *cdb; 9355 9356 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9357 if (cdb->byte2 & SVFY_BYTCHK) 9358 bytchk = 1; 9359 if (cdb->byte2 & SVFY_DPO) 9360 dpo = 1; 9361 lba = scsi_8btou64(cdb->addr); 9362 num_blocks = scsi_4btoul(cdb->length); 9363 break; 9364 } 9365 default: 9366 /* 9367 * We got a command we don't support. This shouldn't 9368 * happen, commands should be filtered out above us. 9369 */ 9370 ctl_set_invalid_opcode(ctsio); 9371 ctl_done((union ctl_io *)ctsio); 9372 return (CTL_RETVAL_COMPLETE); 9373 } 9374 9375 /* 9376 * The first check is to make sure we're in bounds, the second 9377 * check is to catch wrap-around problems. If the lba + num blocks 9378 * is less than the lba, then we've wrapped around and the block 9379 * range is invalid anyway. 9380 */ 9381 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9382 || ((lba + num_blocks) < lba)) { 9383 ctl_set_lba_out_of_range(ctsio); 9384 ctl_done((union ctl_io *)ctsio); 9385 return (CTL_RETVAL_COMPLETE); 9386 } 9387 9388 /* 9389 * According to SBC-3, a transfer length of 0 is not an error. 9390 */ 9391 if (num_blocks == 0) { 9392 ctl_set_success(ctsio); 9393 ctl_done((union ctl_io *)ctsio); 9394 return (CTL_RETVAL_COMPLETE); 9395 } 9396 9397 lbalen = (struct ctl_lba_len_flags *) 9398 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9399 lbalen->lba = lba; 9400 lbalen->len = num_blocks; 9401 if (bytchk) { 9402 lbalen->flags = CTL_LLF_COMPARE; 9403 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9404 } else { 9405 lbalen->flags = CTL_LLF_VERIFY; 9406 ctsio->kern_total_len = 0; 9407 } 9408 ctsio->kern_rel_offset = 0; 9409 9410 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9411 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9412 return (retval); 9413} 9414 9415int 9416ctl_report_luns(struct ctl_scsiio *ctsio) 9417{ 9418 struct scsi_report_luns *cdb; 9419 struct scsi_report_luns_data *lun_data; 9420 struct ctl_lun *lun, *request_lun; 9421 int num_luns, retval; 9422 uint32_t alloc_len, lun_datalen; 9423 int num_filled, well_known; 9424 uint32_t initidx, targ_lun_id, lun_id; 9425 9426 retval = CTL_RETVAL_COMPLETE; 9427 well_known = 0; 9428 9429 cdb = (struct scsi_report_luns *)ctsio->cdb; 9430 9431 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9432 9433 mtx_lock(&control_softc->ctl_lock); 9434 num_luns = control_softc->num_luns; 9435 mtx_unlock(&control_softc->ctl_lock); 9436 9437 switch (cdb->select_report) { 9438 case RPL_REPORT_DEFAULT: 9439 case RPL_REPORT_ALL: 9440 break; 9441 case RPL_REPORT_WELLKNOWN: 9442 well_known = 1; 9443 num_luns = 0; 9444 break; 9445 default: 9446 ctl_set_invalid_field(ctsio, 9447 /*sks_valid*/ 1, 9448 /*command*/ 1, 9449 /*field*/ 2, 9450 /*bit_valid*/ 0, 9451 /*bit*/ 0); 9452 ctl_done((union ctl_io *)ctsio); 9453 return (retval); 9454 break; /* NOTREACHED */ 9455 } 9456 9457 alloc_len = scsi_4btoul(cdb->length); 9458 /* 9459 * The initiator has to allocate at least 16 bytes for this request, 9460 * so he can at least get the header and the first LUN. Otherwise 9461 * we reject the request (per SPC-3 rev 14, section 6.21). 9462 */ 9463 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9464 sizeof(struct scsi_report_luns_lundata))) { 9465 ctl_set_invalid_field(ctsio, 9466 /*sks_valid*/ 1, 9467 /*command*/ 1, 9468 /*field*/ 6, 9469 /*bit_valid*/ 0, 9470 /*bit*/ 0); 9471 ctl_done((union ctl_io *)ctsio); 9472 return (retval); 9473 } 9474 9475 request_lun = (struct ctl_lun *) 9476 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9477 9478 lun_datalen = sizeof(*lun_data) + 9479 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9480 9481 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9482 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9483 ctsio->kern_sg_entries = 0; 9484 9485 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9486 9487 mtx_lock(&control_softc->ctl_lock); 9488 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9489 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9490 if (lun_id >= CTL_MAX_LUNS) 9491 continue; 9492 lun = control_softc->ctl_luns[lun_id]; 9493 if (lun == NULL) 9494 continue; 9495 9496 if (targ_lun_id <= 0xff) { 9497 /* 9498 * Peripheral addressing method, bus number 0. 9499 */ 9500 lun_data->luns[num_filled].lundata[0] = 9501 RPL_LUNDATA_ATYP_PERIPH; 9502 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9503 num_filled++; 9504 } else if (targ_lun_id <= 0x3fff) { 9505 /* 9506 * Flat addressing method. 9507 */ 9508 lun_data->luns[num_filled].lundata[0] = 9509 RPL_LUNDATA_ATYP_FLAT | 9510 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9511#ifdef OLDCTLHEADERS 9512 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9513 (targ_lun_id & SRLD_BUS_LUN_MASK); 9514#endif 9515 lun_data->luns[num_filled].lundata[1] = 9516#ifdef OLDCTLHEADERS 9517 targ_lun_id >> SRLD_BUS_LUN_BITS; 9518#endif 9519 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9520 num_filled++; 9521 } else { 9522 printf("ctl_report_luns: bogus LUN number %jd, " 9523 "skipping\n", (intmax_t)targ_lun_id); 9524 } 9525 /* 9526 * According to SPC-3, rev 14 section 6.21: 9527 * 9528 * "The execution of a REPORT LUNS command to any valid and 9529 * installed logical unit shall clear the REPORTED LUNS DATA 9530 * HAS CHANGED unit attention condition for all logical 9531 * units of that target with respect to the requesting 9532 * initiator. A valid and installed logical unit is one 9533 * having a PERIPHERAL QUALIFIER of 000b in the standard 9534 * INQUIRY data (see 6.4.2)." 9535 * 9536 * If request_lun is NULL, the LUN this report luns command 9537 * was issued to is either disabled or doesn't exist. In that 9538 * case, we shouldn't clear any pending lun change unit 9539 * attention. 9540 */ 9541 if (request_lun != NULL) { 9542 mtx_lock(&lun->lun_lock); 9543 lun->pending_sense[initidx].ua_pending &= 9544 ~CTL_UA_LUN_CHANGE; 9545 mtx_unlock(&lun->lun_lock); 9546 } 9547 } 9548 mtx_unlock(&control_softc->ctl_lock); 9549 9550 /* 9551 * It's quite possible that we've returned fewer LUNs than we allocated 9552 * space for. Trim it. 9553 */ 9554 lun_datalen = sizeof(*lun_data) + 9555 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9556 9557 if (lun_datalen < alloc_len) { 9558 ctsio->residual = alloc_len - lun_datalen; 9559 ctsio->kern_data_len = lun_datalen; 9560 ctsio->kern_total_len = lun_datalen; 9561 } else { 9562 ctsio->residual = 0; 9563 ctsio->kern_data_len = alloc_len; 9564 ctsio->kern_total_len = alloc_len; 9565 } 9566 ctsio->kern_data_resid = 0; 9567 ctsio->kern_rel_offset = 0; 9568 ctsio->kern_sg_entries = 0; 9569 9570 /* 9571 * We set this to the actual data length, regardless of how much 9572 * space we actually have to return results. If the user looks at 9573 * this value, he'll know whether or not he allocated enough space 9574 * and reissue the command if necessary. We don't support well 9575 * known logical units, so if the user asks for that, return none. 9576 */ 9577 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9578 9579 /* 9580 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9581 * this request. 9582 */ 9583 ctsio->scsi_status = SCSI_STATUS_OK; 9584 9585 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9586 ctsio->be_move_done = ctl_config_move_done; 9587 ctl_datamove((union ctl_io *)ctsio); 9588 9589 return (retval); 9590} 9591 9592int 9593ctl_request_sense(struct ctl_scsiio *ctsio) 9594{ 9595 struct scsi_request_sense *cdb; 9596 struct scsi_sense_data *sense_ptr; 9597 struct ctl_lun *lun; 9598 uint32_t initidx; 9599 int have_error; 9600 scsi_sense_data_type sense_format; 9601 9602 cdb = (struct scsi_request_sense *)ctsio->cdb; 9603 9604 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9605 9606 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9607 9608 /* 9609 * Determine which sense format the user wants. 9610 */ 9611 if (cdb->byte2 & SRS_DESC) 9612 sense_format = SSD_TYPE_DESC; 9613 else 9614 sense_format = SSD_TYPE_FIXED; 9615 9616 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9617 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9618 ctsio->kern_sg_entries = 0; 9619 9620 /* 9621 * struct scsi_sense_data, which is currently set to 256 bytes, is 9622 * larger than the largest allowed value for the length field in the 9623 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9624 */ 9625 ctsio->residual = 0; 9626 ctsio->kern_data_len = cdb->length; 9627 ctsio->kern_total_len = cdb->length; 9628 9629 ctsio->kern_data_resid = 0; 9630 ctsio->kern_rel_offset = 0; 9631 ctsio->kern_sg_entries = 0; 9632 9633 /* 9634 * If we don't have a LUN, we don't have any pending sense. 9635 */ 9636 if (lun == NULL) 9637 goto no_sense; 9638 9639 have_error = 0; 9640 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9641 /* 9642 * Check for pending sense, and then for pending unit attentions. 9643 * Pending sense gets returned first, then pending unit attentions. 9644 */ 9645 mtx_lock(&lun->lun_lock); 9646 if (ctl_is_set(lun->have_ca, initidx)) { 9647 scsi_sense_data_type stored_format; 9648 9649 /* 9650 * Check to see which sense format was used for the stored 9651 * sense data. 9652 */ 9653 stored_format = scsi_sense_type( 9654 &lun->pending_sense[initidx].sense); 9655 9656 /* 9657 * If the user requested a different sense format than the 9658 * one we stored, then we need to convert it to the other 9659 * format. If we're going from descriptor to fixed format 9660 * sense data, we may lose things in translation, depending 9661 * on what options were used. 9662 * 9663 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9664 * for some reason we'll just copy it out as-is. 9665 */ 9666 if ((stored_format == SSD_TYPE_FIXED) 9667 && (sense_format == SSD_TYPE_DESC)) 9668 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9669 &lun->pending_sense[initidx].sense, 9670 (struct scsi_sense_data_desc *)sense_ptr); 9671 else if ((stored_format == SSD_TYPE_DESC) 9672 && (sense_format == SSD_TYPE_FIXED)) 9673 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9674 &lun->pending_sense[initidx].sense, 9675 (struct scsi_sense_data_fixed *)sense_ptr); 9676 else 9677 memcpy(sense_ptr, &lun->pending_sense[initidx].sense, 9678 ctl_min(sizeof(*sense_ptr), 9679 sizeof(lun->pending_sense[initidx].sense))); 9680 9681 ctl_clear_mask(lun->have_ca, initidx); 9682 have_error = 1; 9683 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) { 9684 ctl_ua_type ua_type; 9685 9686 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending, 9687 sense_ptr, sense_format); 9688 if (ua_type != CTL_UA_NONE) { 9689 have_error = 1; 9690 /* We're reporting this UA, so clear it */ 9691 lun->pending_sense[initidx].ua_pending &= ~ua_type; 9692 } 9693 } 9694 mtx_unlock(&lun->lun_lock); 9695 9696 /* 9697 * We already have a pending error, return it. 9698 */ 9699 if (have_error != 0) { 9700 /* 9701 * We report the SCSI status as OK, since the status of the 9702 * request sense command itself is OK. 9703 */ 9704 ctsio->scsi_status = SCSI_STATUS_OK; 9705 9706 /* 9707 * We report 0 for the sense length, because we aren't doing 9708 * autosense in this case. We're reporting sense as 9709 * parameter data. 9710 */ 9711 ctsio->sense_len = 0; 9712 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9713 ctsio->be_move_done = ctl_config_move_done; 9714 ctl_datamove((union ctl_io *)ctsio); 9715 9716 return (CTL_RETVAL_COMPLETE); 9717 } 9718 9719no_sense: 9720 9721 /* 9722 * No sense information to report, so we report that everything is 9723 * okay. 9724 */ 9725 ctl_set_sense_data(sense_ptr, 9726 lun, 9727 sense_format, 9728 /*current_error*/ 1, 9729 /*sense_key*/ SSD_KEY_NO_SENSE, 9730 /*asc*/ 0x00, 9731 /*ascq*/ 0x00, 9732 SSD_ELEM_NONE); 9733 9734 ctsio->scsi_status = SCSI_STATUS_OK; 9735 9736 /* 9737 * We report 0 for the sense length, because we aren't doing 9738 * autosense in this case. We're reporting sense as parameter data. 9739 */ 9740 ctsio->sense_len = 0; 9741 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9742 ctsio->be_move_done = ctl_config_move_done; 9743 ctl_datamove((union ctl_io *)ctsio); 9744 9745 return (CTL_RETVAL_COMPLETE); 9746} 9747 9748int 9749ctl_tur(struct ctl_scsiio *ctsio) 9750{ 9751 struct ctl_lun *lun; 9752 9753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9754 9755 CTL_DEBUG_PRINT(("ctl_tur\n")); 9756 9757 if (lun == NULL) 9758 return (EINVAL); 9759 9760 ctsio->scsi_status = SCSI_STATUS_OK; 9761 ctsio->io_hdr.status = CTL_SUCCESS; 9762 9763 ctl_done((union ctl_io *)ctsio); 9764 9765 return (CTL_RETVAL_COMPLETE); 9766} 9767 9768#ifdef notyet 9769static int 9770ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9771{ 9772 9773} 9774#endif 9775 9776static int 9777ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9778{ 9779 struct scsi_vpd_supported_pages *pages; 9780 int sup_page_size; 9781 struct ctl_lun *lun; 9782 9783 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9784 9785 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9786 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9787 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9788 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9789 ctsio->kern_sg_entries = 0; 9790 9791 if (sup_page_size < alloc_len) { 9792 ctsio->residual = alloc_len - sup_page_size; 9793 ctsio->kern_data_len = sup_page_size; 9794 ctsio->kern_total_len = sup_page_size; 9795 } else { 9796 ctsio->residual = 0; 9797 ctsio->kern_data_len = alloc_len; 9798 ctsio->kern_total_len = alloc_len; 9799 } 9800 ctsio->kern_data_resid = 0; 9801 ctsio->kern_rel_offset = 0; 9802 ctsio->kern_sg_entries = 0; 9803 9804 /* 9805 * The control device is always connected. The disk device, on the 9806 * other hand, may not be online all the time. Need to change this 9807 * to figure out whether the disk device is actually online or not. 9808 */ 9809 if (lun != NULL) 9810 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9811 lun->be_lun->lun_type; 9812 else 9813 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9814 9815 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9816 /* Supported VPD pages */ 9817 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9818 /* Serial Number */ 9819 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9820 /* Device Identification */ 9821 pages->page_list[2] = SVPD_DEVICE_ID; 9822 /* SCSI Ports */ 9823 pages->page_list[3] = SVPD_SCSI_PORTS; 9824 /* Block limits */ 9825 pages->page_list[4] = SVPD_BLOCK_LIMITS; 9826 /* Logical Block Provisioning */ 9827 pages->page_list[5] = SVPD_LBP; 9828 9829 ctsio->scsi_status = SCSI_STATUS_OK; 9830 9831 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9832 ctsio->be_move_done = ctl_config_move_done; 9833 ctl_datamove((union ctl_io *)ctsio); 9834 9835 return (CTL_RETVAL_COMPLETE); 9836} 9837 9838static int 9839ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9840{ 9841 struct scsi_vpd_unit_serial_number *sn_ptr; 9842 struct ctl_lun *lun; 9843 9844 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9845 9846 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9847 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9848 ctsio->kern_sg_entries = 0; 9849 9850 if (sizeof(*sn_ptr) < alloc_len) { 9851 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9852 ctsio->kern_data_len = sizeof(*sn_ptr); 9853 ctsio->kern_total_len = sizeof(*sn_ptr); 9854 } else { 9855 ctsio->residual = 0; 9856 ctsio->kern_data_len = alloc_len; 9857 ctsio->kern_total_len = alloc_len; 9858 } 9859 ctsio->kern_data_resid = 0; 9860 ctsio->kern_rel_offset = 0; 9861 ctsio->kern_sg_entries = 0; 9862 9863 /* 9864 * The control device is always connected. The disk device, on the 9865 * other hand, may not be online all the time. Need to change this 9866 * to figure out whether the disk device is actually online or not. 9867 */ 9868 if (lun != NULL) 9869 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9870 lun->be_lun->lun_type; 9871 else 9872 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9873 9874 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9875 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9876 /* 9877 * If we don't have a LUN, we just leave the serial number as 9878 * all spaces. 9879 */ 9880 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9881 if (lun != NULL) { 9882 strncpy((char *)sn_ptr->serial_num, 9883 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9884 } 9885 ctsio->scsi_status = SCSI_STATUS_OK; 9886 9887 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9888 ctsio->be_move_done = ctl_config_move_done; 9889 ctl_datamove((union ctl_io *)ctsio); 9890 9891 return (CTL_RETVAL_COMPLETE); 9892} 9893 9894 9895static int 9896ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9897{ 9898 struct scsi_vpd_device_id *devid_ptr; 9899 struct scsi_vpd_id_descriptor *desc; 9900 struct ctl_softc *ctl_softc; 9901 struct ctl_lun *lun; 9902 struct ctl_port *port; 9903 int data_len; 9904 uint8_t proto; 9905 9906 ctl_softc = control_softc; 9907 9908 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9909 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9910 9911 data_len = sizeof(struct scsi_vpd_device_id) + 9912 sizeof(struct scsi_vpd_id_descriptor) + 9913 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9914 sizeof(struct scsi_vpd_id_descriptor) + 9915 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9916 if (lun && lun->lun_devid) 9917 data_len += lun->lun_devid->len; 9918 if (port->port_devid) 9919 data_len += port->port_devid->len; 9920 if (port->target_devid) 9921 data_len += port->target_devid->len; 9922 9923 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9924 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9925 ctsio->kern_sg_entries = 0; 9926 9927 if (data_len < alloc_len) { 9928 ctsio->residual = alloc_len - data_len; 9929 ctsio->kern_data_len = data_len; 9930 ctsio->kern_total_len = data_len; 9931 } else { 9932 ctsio->residual = 0; 9933 ctsio->kern_data_len = alloc_len; 9934 ctsio->kern_total_len = alloc_len; 9935 } 9936 ctsio->kern_data_resid = 0; 9937 ctsio->kern_rel_offset = 0; 9938 ctsio->kern_sg_entries = 0; 9939 9940 /* 9941 * The control device is always connected. The disk device, on the 9942 * other hand, may not be online all the time. 9943 */ 9944 if (lun != NULL) 9945 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9946 lun->be_lun->lun_type; 9947 else 9948 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9949 devid_ptr->page_code = SVPD_DEVICE_ID; 9950 scsi_ulto2b(data_len - 4, devid_ptr->length); 9951 9952 if (port->port_type == CTL_PORT_FC) 9953 proto = SCSI_PROTO_FC << 4; 9954 else if (port->port_type == CTL_PORT_ISCSI) 9955 proto = SCSI_PROTO_ISCSI << 4; 9956 else 9957 proto = SCSI_PROTO_SPI << 4; 9958 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9959 9960 /* 9961 * We're using a LUN association here. i.e., this device ID is a 9962 * per-LUN identifier. 9963 */ 9964 if (lun && lun->lun_devid) { 9965 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9966 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9967 lun->lun_devid->len); 9968 } 9969 9970 /* 9971 * This is for the WWPN which is a port association. 9972 */ 9973 if (port->port_devid) { 9974 memcpy(desc, port->port_devid->data, port->port_devid->len); 9975 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9976 port->port_devid->len); 9977 } 9978 9979 /* 9980 * This is for the Relative Target Port(type 4h) identifier 9981 */ 9982 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9983 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9984 SVPD_ID_TYPE_RELTARG; 9985 desc->length = 4; 9986 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9987 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9988 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9989 9990 /* 9991 * This is for the Target Port Group(type 5h) identifier 9992 */ 9993 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9994 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9995 SVPD_ID_TYPE_TPORTGRP; 9996 desc->length = 4; 9997 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9998 &desc->identifier[2]); 9999 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10000 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10001 10002 /* 10003 * This is for the Target identifier 10004 */ 10005 if (port->target_devid) { 10006 memcpy(desc, port->target_devid->data, port->target_devid->len); 10007 } 10008 10009 ctsio->scsi_status = SCSI_STATUS_OK; 10010 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10011 ctsio->be_move_done = ctl_config_move_done; 10012 ctl_datamove((union ctl_io *)ctsio); 10013 10014 return (CTL_RETVAL_COMPLETE); 10015} 10016 10017static int 10018ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10019{ 10020 struct ctl_softc *softc = control_softc; 10021 struct scsi_vpd_scsi_ports *sp; 10022 struct scsi_vpd_port_designation *pd; 10023 struct scsi_vpd_port_designation_cont *pdc; 10024 struct ctl_lun *lun; 10025 struct ctl_port *port; 10026 int data_len, num_target_ports, id_len, g, pg, p; 10027 int num_target_port_groups, single; 10028 10029 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10030 10031 single = ctl_is_single; 10032 if (single) 10033 num_target_port_groups = 1; 10034 else 10035 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10036 num_target_ports = 0; 10037 id_len = 0; 10038 mtx_lock(&softc->ctl_lock); 10039 STAILQ_FOREACH(port, &softc->port_list, links) { 10040 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10041 continue; 10042 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 10043 CTL_MAX_LUNS) 10044 continue; 10045 num_target_ports++; 10046 if (port->port_devid) 10047 id_len += port->port_devid->len; 10048 } 10049 mtx_unlock(&softc->ctl_lock); 10050 10051 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10052 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10053 sizeof(struct scsi_vpd_port_designation_cont)) + id_len; 10054 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10055 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10056 ctsio->kern_sg_entries = 0; 10057 10058 if (data_len < alloc_len) { 10059 ctsio->residual = alloc_len - data_len; 10060 ctsio->kern_data_len = data_len; 10061 ctsio->kern_total_len = data_len; 10062 } else { 10063 ctsio->residual = 0; 10064 ctsio->kern_data_len = alloc_len; 10065 ctsio->kern_total_len = alloc_len; 10066 } 10067 ctsio->kern_data_resid = 0; 10068 ctsio->kern_rel_offset = 0; 10069 ctsio->kern_sg_entries = 0; 10070 10071 /* 10072 * The control device is always connected. The disk device, on the 10073 * other hand, may not be online all the time. Need to change this 10074 * to figure out whether the disk device is actually online or not. 10075 */ 10076 if (lun != NULL) 10077 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10078 lun->be_lun->lun_type; 10079 else 10080 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10081 10082 sp->page_code = SVPD_SCSI_PORTS; 10083 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10084 sp->page_length); 10085 pd = &sp->design[0]; 10086 10087 mtx_lock(&softc->ctl_lock); 10088 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10089 pg = 0; 10090 else 10091 pg = 1; 10092 for (g = 0; g < num_target_port_groups; g++) { 10093 STAILQ_FOREACH(port, &softc->port_list, links) { 10094 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10095 continue; 10096 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 10097 CTL_MAX_LUNS) 10098 continue; 10099 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10100 scsi_ulto2b(p, pd->relative_port_id); 10101 scsi_ulto2b(0, pd->initiator_transportid_length); 10102 pdc = (struct scsi_vpd_port_designation_cont *) 10103 &pd->initiator_transportid[0]; 10104 if (port->port_devid && g == pg) { 10105 id_len = port->port_devid->len; 10106 scsi_ulto2b(port->port_devid->len, 10107 pdc->target_port_descriptors_length); 10108 memcpy(pdc->target_port_descriptors, 10109 port->port_devid->data, port->port_devid->len); 10110 } else { 10111 id_len = 0; 10112 scsi_ulto2b(0, pdc->target_port_descriptors_length); 10113 } 10114 pd = (struct scsi_vpd_port_designation *) 10115 ((uint8_t *)pdc->target_port_descriptors + id_len); 10116 } 10117 } 10118 mtx_unlock(&softc->ctl_lock); 10119 10120 ctsio->scsi_status = SCSI_STATUS_OK; 10121 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10122 ctsio->be_move_done = ctl_config_move_done; 10123 ctl_datamove((union ctl_io *)ctsio); 10124 10125 return (CTL_RETVAL_COMPLETE); 10126} 10127 10128static int 10129ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10130{ 10131 struct scsi_vpd_block_limits *bl_ptr; 10132 struct ctl_lun *lun; 10133 int bs; 10134 10135 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10136 bs = lun->be_lun->blocksize; 10137 10138 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10139 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10140 ctsio->kern_sg_entries = 0; 10141 10142 if (sizeof(*bl_ptr) < alloc_len) { 10143 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10144 ctsio->kern_data_len = sizeof(*bl_ptr); 10145 ctsio->kern_total_len = sizeof(*bl_ptr); 10146 } else { 10147 ctsio->residual = 0; 10148 ctsio->kern_data_len = alloc_len; 10149 ctsio->kern_total_len = alloc_len; 10150 } 10151 ctsio->kern_data_resid = 0; 10152 ctsio->kern_rel_offset = 0; 10153 ctsio->kern_sg_entries = 0; 10154 10155 /* 10156 * The control device is always connected. The disk device, on the 10157 * other hand, may not be online all the time. Need to change this 10158 * to figure out whether the disk device is actually online or not. 10159 */ 10160 if (lun != NULL) 10161 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10162 lun->be_lun->lun_type; 10163 else 10164 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10165 10166 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10167 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10168 bl_ptr->max_cmp_write_len = 0xff; 10169 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10170 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10171 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10172 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10173 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10174 } 10175 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10176 10177 ctsio->scsi_status = SCSI_STATUS_OK; 10178 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10179 ctsio->be_move_done = ctl_config_move_done; 10180 ctl_datamove((union ctl_io *)ctsio); 10181 10182 return (CTL_RETVAL_COMPLETE); 10183} 10184 10185static int 10186ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10187{ 10188 struct scsi_vpd_logical_block_prov *lbp_ptr; 10189 struct ctl_lun *lun; 10190 int bs; 10191 10192 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10193 bs = lun->be_lun->blocksize; 10194 10195 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10196 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10197 ctsio->kern_sg_entries = 0; 10198 10199 if (sizeof(*lbp_ptr) < alloc_len) { 10200 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10201 ctsio->kern_data_len = sizeof(*lbp_ptr); 10202 ctsio->kern_total_len = sizeof(*lbp_ptr); 10203 } else { 10204 ctsio->residual = 0; 10205 ctsio->kern_data_len = alloc_len; 10206 ctsio->kern_total_len = alloc_len; 10207 } 10208 ctsio->kern_data_resid = 0; 10209 ctsio->kern_rel_offset = 0; 10210 ctsio->kern_sg_entries = 0; 10211 10212 /* 10213 * The control device is always connected. The disk device, on the 10214 * other hand, may not be online all the time. Need to change this 10215 * to figure out whether the disk device is actually online or not. 10216 */ 10217 if (lun != NULL) 10218 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10219 lun->be_lun->lun_type; 10220 else 10221 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10222 10223 lbp_ptr->page_code = SVPD_LBP; 10224 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 10225 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10; 10226 10227 ctsio->scsi_status = SCSI_STATUS_OK; 10228 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10229 ctsio->be_move_done = ctl_config_move_done; 10230 ctl_datamove((union ctl_io *)ctsio); 10231 10232 return (CTL_RETVAL_COMPLETE); 10233} 10234 10235static int 10236ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10237{ 10238 struct scsi_inquiry *cdb; 10239 struct ctl_lun *lun; 10240 int alloc_len, retval; 10241 10242 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10243 cdb = (struct scsi_inquiry *)ctsio->cdb; 10244 10245 retval = CTL_RETVAL_COMPLETE; 10246 10247 alloc_len = scsi_2btoul(cdb->length); 10248 10249 switch (cdb->page_code) { 10250 case SVPD_SUPPORTED_PAGES: 10251 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10252 break; 10253 case SVPD_UNIT_SERIAL_NUMBER: 10254 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10255 break; 10256 case SVPD_DEVICE_ID: 10257 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10258 break; 10259 case SVPD_SCSI_PORTS: 10260 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10261 break; 10262 case SVPD_BLOCK_LIMITS: 10263 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10264 break; 10265 case SVPD_LBP: 10266 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10267 break; 10268 default: 10269 ctl_set_invalid_field(ctsio, 10270 /*sks_valid*/ 1, 10271 /*command*/ 1, 10272 /*field*/ 2, 10273 /*bit_valid*/ 0, 10274 /*bit*/ 0); 10275 ctl_done((union ctl_io *)ctsio); 10276 retval = CTL_RETVAL_COMPLETE; 10277 break; 10278 } 10279 10280 return (retval); 10281} 10282 10283static int 10284ctl_inquiry_std(struct ctl_scsiio *ctsio) 10285{ 10286 struct scsi_inquiry_data *inq_ptr; 10287 struct scsi_inquiry *cdb; 10288 struct ctl_softc *ctl_softc; 10289 struct ctl_lun *lun; 10290 char *val; 10291 uint32_t alloc_len; 10292 int is_fc; 10293 10294 ctl_softc = control_softc; 10295 10296 /* 10297 * Figure out whether we're talking to a Fibre Channel port or not. 10298 * We treat the ioctl front end, and any SCSI adapters, as packetized 10299 * SCSI front ends. 10300 */ 10301 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type != 10302 CTL_PORT_FC) 10303 is_fc = 0; 10304 else 10305 is_fc = 1; 10306 10307 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10308 cdb = (struct scsi_inquiry *)ctsio->cdb; 10309 alloc_len = scsi_2btoul(cdb->length); 10310 10311 /* 10312 * We malloc the full inquiry data size here and fill it 10313 * in. If the user only asks for less, we'll give him 10314 * that much. 10315 */ 10316 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10317 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10318 ctsio->kern_sg_entries = 0; 10319 ctsio->kern_data_resid = 0; 10320 ctsio->kern_rel_offset = 0; 10321 10322 if (sizeof(*inq_ptr) < alloc_len) { 10323 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10324 ctsio->kern_data_len = sizeof(*inq_ptr); 10325 ctsio->kern_total_len = sizeof(*inq_ptr); 10326 } else { 10327 ctsio->residual = 0; 10328 ctsio->kern_data_len = alloc_len; 10329 ctsio->kern_total_len = alloc_len; 10330 } 10331 10332 /* 10333 * If we have a LUN configured, report it as connected. Otherwise, 10334 * report that it is offline or no device is supported, depending 10335 * on the value of inquiry_pq_no_lun. 10336 * 10337 * According to the spec (SPC-4 r34), the peripheral qualifier 10338 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10339 * 10340 * "A peripheral device having the specified peripheral device type 10341 * is not connected to this logical unit. However, the device 10342 * server is capable of supporting the specified peripheral device 10343 * type on this logical unit." 10344 * 10345 * According to the same spec, the peripheral qualifier 10346 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10347 * 10348 * "The device server is not capable of supporting a peripheral 10349 * device on this logical unit. For this peripheral qualifier the 10350 * peripheral device type shall be set to 1Fh. All other peripheral 10351 * device type values are reserved for this peripheral qualifier." 10352 * 10353 * Given the text, it would seem that we probably want to report that 10354 * the LUN is offline here. There is no LUN connected, but we can 10355 * support a LUN at the given LUN number. 10356 * 10357 * In the real world, though, it sounds like things are a little 10358 * different: 10359 * 10360 * - Linux, when presented with a LUN with the offline peripheral 10361 * qualifier, will create an sg driver instance for it. So when 10362 * you attach it to CTL, you wind up with a ton of sg driver 10363 * instances. (One for every LUN that Linux bothered to probe.) 10364 * Linux does this despite the fact that it issues a REPORT LUNs 10365 * to LUN 0 to get the inventory of supported LUNs. 10366 * 10367 * - There is other anecdotal evidence (from Emulex folks) about 10368 * arrays that use the offline peripheral qualifier for LUNs that 10369 * are on the "passive" path in an active/passive array. 10370 * 10371 * So the solution is provide a hopefully reasonable default 10372 * (return bad/no LUN) and allow the user to change the behavior 10373 * with a tunable/sysctl variable. 10374 */ 10375 if (lun != NULL) 10376 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10377 lun->be_lun->lun_type; 10378 else if (ctl_softc->inquiry_pq_no_lun == 0) 10379 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10380 else 10381 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10382 10383 /* RMB in byte 2 is 0 */ 10384 inq_ptr->version = SCSI_REV_SPC3; 10385 10386 /* 10387 * According to SAM-3, even if a device only supports a single 10388 * level of LUN addressing, it should still set the HISUP bit: 10389 * 10390 * 4.9.1 Logical unit numbers overview 10391 * 10392 * All logical unit number formats described in this standard are 10393 * hierarchical in structure even when only a single level in that 10394 * hierarchy is used. The HISUP bit shall be set to one in the 10395 * standard INQUIRY data (see SPC-2) when any logical unit number 10396 * format described in this standard is used. Non-hierarchical 10397 * formats are outside the scope of this standard. 10398 * 10399 * Therefore we set the HiSup bit here. 10400 * 10401 * The reponse format is 2, per SPC-3. 10402 */ 10403 inq_ptr->response_format = SID_HiSup | 2; 10404 10405 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 10406 CTL_DEBUG_PRINT(("additional_length = %d\n", 10407 inq_ptr->additional_length)); 10408 10409 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT; 10410 /* 16 bit addressing */ 10411 if (is_fc == 0) 10412 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10413 /* XXX set the SID_MultiP bit here if we're actually going to 10414 respond on multiple ports */ 10415 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10416 10417 /* 16 bit data bus, synchronous transfers */ 10418 /* XXX these flags don't apply for FC */ 10419 if (is_fc == 0) 10420 inq_ptr->flags = SID_WBus16 | SID_Sync; 10421 /* 10422 * XXX KDM do we want to support tagged queueing on the control 10423 * device at all? 10424 */ 10425 if ((lun == NULL) 10426 || (lun->be_lun->lun_type != T_PROCESSOR)) 10427 inq_ptr->flags |= SID_CmdQue; 10428 /* 10429 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10430 * We have 8 bytes for the vendor name, and 16 bytes for the device 10431 * name and 4 bytes for the revision. 10432 */ 10433 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10434 "vendor")) == NULL) { 10435 strcpy(inq_ptr->vendor, CTL_VENDOR); 10436 } else { 10437 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10438 strncpy(inq_ptr->vendor, val, 10439 min(sizeof(inq_ptr->vendor), strlen(val))); 10440 } 10441 if (lun == NULL) { 10442 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10443 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10444 switch (lun->be_lun->lun_type) { 10445 case T_DIRECT: 10446 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10447 break; 10448 case T_PROCESSOR: 10449 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10450 break; 10451 default: 10452 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10453 break; 10454 } 10455 } else { 10456 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10457 strncpy(inq_ptr->product, val, 10458 min(sizeof(inq_ptr->product), strlen(val))); 10459 } 10460 10461 /* 10462 * XXX make this a macro somewhere so it automatically gets 10463 * incremented when we make changes. 10464 */ 10465 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10466 "revision")) == NULL) { 10467 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10468 } else { 10469 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10470 strncpy(inq_ptr->revision, val, 10471 min(sizeof(inq_ptr->revision), strlen(val))); 10472 } 10473 10474 /* 10475 * For parallel SCSI, we support double transition and single 10476 * transition clocking. We also support QAS (Quick Arbitration 10477 * and Selection) and Information Unit transfers on both the 10478 * control and array devices. 10479 */ 10480 if (is_fc == 0) 10481 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10482 SID_SPI_IUS; 10483 10484 /* SAM-3 */ 10485 scsi_ulto2b(0x0060, inq_ptr->version1); 10486 /* SPC-3 (no version claimed) XXX should we claim a version? */ 10487 scsi_ulto2b(0x0300, inq_ptr->version2); 10488 if (is_fc) { 10489 /* FCP-2 ANSI INCITS.350:2003 */ 10490 scsi_ulto2b(0x0917, inq_ptr->version3); 10491 } else { 10492 /* SPI-4 ANSI INCITS.362:200x */ 10493 scsi_ulto2b(0x0B56, inq_ptr->version3); 10494 } 10495 10496 if (lun == NULL) { 10497 /* SBC-2 (no version claimed) XXX should we claim a version? */ 10498 scsi_ulto2b(0x0320, inq_ptr->version4); 10499 } else { 10500 switch (lun->be_lun->lun_type) { 10501 case T_DIRECT: 10502 /* 10503 * SBC-2 (no version claimed) XXX should we claim a 10504 * version? 10505 */ 10506 scsi_ulto2b(0x0320, inq_ptr->version4); 10507 break; 10508 case T_PROCESSOR: 10509 default: 10510 break; 10511 } 10512 } 10513 10514 ctsio->scsi_status = SCSI_STATUS_OK; 10515 if (ctsio->kern_data_len > 0) { 10516 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10517 ctsio->be_move_done = ctl_config_move_done; 10518 ctl_datamove((union ctl_io *)ctsio); 10519 } else { 10520 ctsio->io_hdr.status = CTL_SUCCESS; 10521 ctl_done((union ctl_io *)ctsio); 10522 } 10523 10524 return (CTL_RETVAL_COMPLETE); 10525} 10526 10527int 10528ctl_inquiry(struct ctl_scsiio *ctsio) 10529{ 10530 struct scsi_inquiry *cdb; 10531 int retval; 10532 10533 cdb = (struct scsi_inquiry *)ctsio->cdb; 10534 10535 retval = 0; 10536 10537 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10538 10539 /* 10540 * Right now, we don't support the CmdDt inquiry information. 10541 * This would be nice to support in the future. When we do 10542 * support it, we should change this test so that it checks to make 10543 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10544 */ 10545#ifdef notyet 10546 if (((cdb->byte2 & SI_EVPD) 10547 && (cdb->byte2 & SI_CMDDT))) 10548#endif 10549 if (cdb->byte2 & SI_CMDDT) { 10550 /* 10551 * Point to the SI_CMDDT bit. We might change this 10552 * when we support SI_CMDDT, but since both bits would be 10553 * "wrong", this should probably just stay as-is then. 10554 */ 10555 ctl_set_invalid_field(ctsio, 10556 /*sks_valid*/ 1, 10557 /*command*/ 1, 10558 /*field*/ 1, 10559 /*bit_valid*/ 1, 10560 /*bit*/ 1); 10561 ctl_done((union ctl_io *)ctsio); 10562 return (CTL_RETVAL_COMPLETE); 10563 } 10564 if (cdb->byte2 & SI_EVPD) 10565 retval = ctl_inquiry_evpd(ctsio); 10566#ifdef notyet 10567 else if (cdb->byte2 & SI_CMDDT) 10568 retval = ctl_inquiry_cmddt(ctsio); 10569#endif 10570 else 10571 retval = ctl_inquiry_std(ctsio); 10572 10573 return (retval); 10574} 10575 10576/* 10577 * For known CDB types, parse the LBA and length. 10578 */ 10579static int 10580ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10581{ 10582 if (io->io_hdr.io_type != CTL_IO_SCSI) 10583 return (1); 10584 10585 switch (io->scsiio.cdb[0]) { 10586 case COMPARE_AND_WRITE: { 10587 struct scsi_compare_and_write *cdb; 10588 10589 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10590 10591 *lba = scsi_8btou64(cdb->addr); 10592 *len = cdb->length; 10593 break; 10594 } 10595 case READ_6: 10596 case WRITE_6: { 10597 struct scsi_rw_6 *cdb; 10598 10599 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10600 10601 *lba = scsi_3btoul(cdb->addr); 10602 /* only 5 bits are valid in the most significant address byte */ 10603 *lba &= 0x1fffff; 10604 *len = cdb->length; 10605 break; 10606 } 10607 case READ_10: 10608 case WRITE_10: { 10609 struct scsi_rw_10 *cdb; 10610 10611 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10612 10613 *lba = scsi_4btoul(cdb->addr); 10614 *len = scsi_2btoul(cdb->length); 10615 break; 10616 } 10617 case WRITE_VERIFY_10: { 10618 struct scsi_write_verify_10 *cdb; 10619 10620 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10621 10622 *lba = scsi_4btoul(cdb->addr); 10623 *len = scsi_2btoul(cdb->length); 10624 break; 10625 } 10626 case READ_12: 10627 case WRITE_12: { 10628 struct scsi_rw_12 *cdb; 10629 10630 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10631 10632 *lba = scsi_4btoul(cdb->addr); 10633 *len = scsi_4btoul(cdb->length); 10634 break; 10635 } 10636 case WRITE_VERIFY_12: { 10637 struct scsi_write_verify_12 *cdb; 10638 10639 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10640 10641 *lba = scsi_4btoul(cdb->addr); 10642 *len = scsi_4btoul(cdb->length); 10643 break; 10644 } 10645 case READ_16: 10646 case WRITE_16: { 10647 struct scsi_rw_16 *cdb; 10648 10649 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10650 10651 *lba = scsi_8btou64(cdb->addr); 10652 *len = scsi_4btoul(cdb->length); 10653 break; 10654 } 10655 case WRITE_VERIFY_16: { 10656 struct scsi_write_verify_16 *cdb; 10657 10658 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10659 10660 10661 *lba = scsi_8btou64(cdb->addr); 10662 *len = scsi_4btoul(cdb->length); 10663 break; 10664 } 10665 case WRITE_SAME_10: { 10666 struct scsi_write_same_10 *cdb; 10667 10668 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10669 10670 *lba = scsi_4btoul(cdb->addr); 10671 *len = scsi_2btoul(cdb->length); 10672 break; 10673 } 10674 case WRITE_SAME_16: { 10675 struct scsi_write_same_16 *cdb; 10676 10677 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10678 10679 *lba = scsi_8btou64(cdb->addr); 10680 *len = scsi_4btoul(cdb->length); 10681 break; 10682 } 10683 case VERIFY_10: { 10684 struct scsi_verify_10 *cdb; 10685 10686 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10687 10688 *lba = scsi_4btoul(cdb->addr); 10689 *len = scsi_2btoul(cdb->length); 10690 break; 10691 } 10692 case VERIFY_12: { 10693 struct scsi_verify_12 *cdb; 10694 10695 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10696 10697 *lba = scsi_4btoul(cdb->addr); 10698 *len = scsi_4btoul(cdb->length); 10699 break; 10700 } 10701 case VERIFY_16: { 10702 struct scsi_verify_16 *cdb; 10703 10704 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10705 10706 *lba = scsi_8btou64(cdb->addr); 10707 *len = scsi_4btoul(cdb->length); 10708 break; 10709 } 10710 default: 10711 return (1); 10712 break; /* NOTREACHED */ 10713 } 10714 10715 return (0); 10716} 10717 10718static ctl_action 10719ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10720{ 10721 uint64_t endlba1, endlba2; 10722 10723 endlba1 = lba1 + len1 - 1; 10724 endlba2 = lba2 + len2 - 1; 10725 10726 if ((endlba1 < lba2) 10727 || (endlba2 < lba1)) 10728 return (CTL_ACTION_PASS); 10729 else 10730 return (CTL_ACTION_BLOCK); 10731} 10732 10733static ctl_action 10734ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10735{ 10736 uint64_t lba1, lba2; 10737 uint32_t len1, len2; 10738 int retval; 10739 10740 retval = ctl_get_lba_len(io1, &lba1, &len1); 10741 if (retval != 0) 10742 return (CTL_ACTION_ERROR); 10743 10744 retval = ctl_get_lba_len(io2, &lba2, &len2); 10745 if (retval != 0) 10746 return (CTL_ACTION_ERROR); 10747 10748 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10749} 10750 10751static ctl_action 10752ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10753{ 10754 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10755 ctl_serialize_action *serialize_row; 10756 10757 /* 10758 * The initiator attempted multiple untagged commands at the same 10759 * time. Can't do that. 10760 */ 10761 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10762 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10763 && ((pending_io->io_hdr.nexus.targ_port == 10764 ooa_io->io_hdr.nexus.targ_port) 10765 && (pending_io->io_hdr.nexus.initid.id == 10766 ooa_io->io_hdr.nexus.initid.id)) 10767 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10768 return (CTL_ACTION_OVERLAP); 10769 10770 /* 10771 * The initiator attempted to send multiple tagged commands with 10772 * the same ID. (It's fine if different initiators have the same 10773 * tag ID.) 10774 * 10775 * Even if all of those conditions are true, we don't kill the I/O 10776 * if the command ahead of us has been aborted. We won't end up 10777 * sending it to the FETD, and it's perfectly legal to resend a 10778 * command with the same tag number as long as the previous 10779 * instance of this tag number has been aborted somehow. 10780 */ 10781 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10782 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10783 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10784 && ((pending_io->io_hdr.nexus.targ_port == 10785 ooa_io->io_hdr.nexus.targ_port) 10786 && (pending_io->io_hdr.nexus.initid.id == 10787 ooa_io->io_hdr.nexus.initid.id)) 10788 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10789 return (CTL_ACTION_OVERLAP_TAG); 10790 10791 /* 10792 * If we get a head of queue tag, SAM-3 says that we should 10793 * immediately execute it. 10794 * 10795 * What happens if this command would normally block for some other 10796 * reason? e.g. a request sense with a head of queue tag 10797 * immediately after a write. Normally that would block, but this 10798 * will result in its getting executed immediately... 10799 * 10800 * We currently return "pass" instead of "skip", so we'll end up 10801 * going through the rest of the queue to check for overlapped tags. 10802 * 10803 * XXX KDM check for other types of blockage first?? 10804 */ 10805 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10806 return (CTL_ACTION_PASS); 10807 10808 /* 10809 * Ordered tags have to block until all items ahead of them 10810 * have completed. If we get called with an ordered tag, we always 10811 * block, if something else is ahead of us in the queue. 10812 */ 10813 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10814 return (CTL_ACTION_BLOCK); 10815 10816 /* 10817 * Simple tags get blocked until all head of queue and ordered tags 10818 * ahead of them have completed. I'm lumping untagged commands in 10819 * with simple tags here. XXX KDM is that the right thing to do? 10820 */ 10821 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10822 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10823 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10824 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10825 return (CTL_ACTION_BLOCK); 10826 10827 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10828 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10829 10830 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10831 10832 switch (serialize_row[pending_entry->seridx]) { 10833 case CTL_SER_BLOCK: 10834 return (CTL_ACTION_BLOCK); 10835 break; /* NOTREACHED */ 10836 case CTL_SER_EXTENT: 10837 return (ctl_extent_check(pending_io, ooa_io)); 10838 break; /* NOTREACHED */ 10839 case CTL_SER_PASS: 10840 return (CTL_ACTION_PASS); 10841 break; /* NOTREACHED */ 10842 case CTL_SER_SKIP: 10843 return (CTL_ACTION_SKIP); 10844 break; 10845 default: 10846 panic("invalid serialization value %d", 10847 serialize_row[pending_entry->seridx]); 10848 break; /* NOTREACHED */ 10849 } 10850 10851 return (CTL_ACTION_ERROR); 10852} 10853 10854/* 10855 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10856 * Assumptions: 10857 * - pending_io is generally either incoming, or on the blocked queue 10858 * - starting I/O is the I/O we want to start the check with. 10859 */ 10860static ctl_action 10861ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10862 union ctl_io *starting_io) 10863{ 10864 union ctl_io *ooa_io; 10865 ctl_action action; 10866 10867 mtx_assert(&lun->lun_lock, MA_OWNED); 10868 10869 /* 10870 * Run back along the OOA queue, starting with the current 10871 * blocked I/O and going through every I/O before it on the 10872 * queue. If starting_io is NULL, we'll just end up returning 10873 * CTL_ACTION_PASS. 10874 */ 10875 for (ooa_io = starting_io; ooa_io != NULL; 10876 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10877 ooa_links)){ 10878 10879 /* 10880 * This routine just checks to see whether 10881 * cur_blocked is blocked by ooa_io, which is ahead 10882 * of it in the queue. It doesn't queue/dequeue 10883 * cur_blocked. 10884 */ 10885 action = ctl_check_for_blockage(pending_io, ooa_io); 10886 switch (action) { 10887 case CTL_ACTION_BLOCK: 10888 case CTL_ACTION_OVERLAP: 10889 case CTL_ACTION_OVERLAP_TAG: 10890 case CTL_ACTION_SKIP: 10891 case CTL_ACTION_ERROR: 10892 return (action); 10893 break; /* NOTREACHED */ 10894 case CTL_ACTION_PASS: 10895 break; 10896 default: 10897 panic("invalid action %d", action); 10898 break; /* NOTREACHED */ 10899 } 10900 } 10901 10902 return (CTL_ACTION_PASS); 10903} 10904 10905/* 10906 * Assumptions: 10907 * - An I/O has just completed, and has been removed from the per-LUN OOA 10908 * queue, so some items on the blocked queue may now be unblocked. 10909 */ 10910static int 10911ctl_check_blocked(struct ctl_lun *lun) 10912{ 10913 union ctl_io *cur_blocked, *next_blocked; 10914 10915 mtx_assert(&lun->lun_lock, MA_OWNED); 10916 10917 /* 10918 * Run forward from the head of the blocked queue, checking each 10919 * entry against the I/Os prior to it on the OOA queue to see if 10920 * there is still any blockage. 10921 * 10922 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10923 * with our removing a variable on it while it is traversing the 10924 * list. 10925 */ 10926 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10927 cur_blocked != NULL; cur_blocked = next_blocked) { 10928 union ctl_io *prev_ooa; 10929 ctl_action action; 10930 10931 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10932 blocked_links); 10933 10934 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10935 ctl_ooaq, ooa_links); 10936 10937 /* 10938 * If cur_blocked happens to be the first item in the OOA 10939 * queue now, prev_ooa will be NULL, and the action 10940 * returned will just be CTL_ACTION_PASS. 10941 */ 10942 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10943 10944 switch (action) { 10945 case CTL_ACTION_BLOCK: 10946 /* Nothing to do here, still blocked */ 10947 break; 10948 case CTL_ACTION_OVERLAP: 10949 case CTL_ACTION_OVERLAP_TAG: 10950 /* 10951 * This shouldn't happen! In theory we've already 10952 * checked this command for overlap... 10953 */ 10954 break; 10955 case CTL_ACTION_PASS: 10956 case CTL_ACTION_SKIP: { 10957 struct ctl_softc *softc; 10958 const struct ctl_cmd_entry *entry; 10959 uint32_t initidx; 10960 int isc_retval; 10961 10962 /* 10963 * The skip case shouldn't happen, this transaction 10964 * should have never made it onto the blocked queue. 10965 */ 10966 /* 10967 * This I/O is no longer blocked, we can remove it 10968 * from the blocked queue. Since this is a TAILQ 10969 * (doubly linked list), we can do O(1) removals 10970 * from any place on the list. 10971 */ 10972 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10973 blocked_links); 10974 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10975 10976 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10977 /* 10978 * Need to send IO back to original side to 10979 * run 10980 */ 10981 union ctl_ha_msg msg_info; 10982 10983 msg_info.hdr.original_sc = 10984 cur_blocked->io_hdr.original_sc; 10985 msg_info.hdr.serializing_sc = cur_blocked; 10986 msg_info.hdr.msg_type = CTL_MSG_R2R; 10987 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10988 &msg_info, sizeof(msg_info), 0)) > 10989 CTL_HA_STATUS_SUCCESS) { 10990 printf("CTL:Check Blocked error from " 10991 "ctl_ha_msg_send %d\n", 10992 isc_retval); 10993 } 10994 break; 10995 } 10996 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 10997 softc = control_softc; 10998 10999 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11000 11001 /* 11002 * Check this I/O for LUN state changes that may 11003 * have happened while this command was blocked. 11004 * The LUN state may have been changed by a command 11005 * ahead of us in the queue, so we need to re-check 11006 * for any states that can be caused by SCSI 11007 * commands. 11008 */ 11009 if (ctl_scsiio_lun_check(softc, lun, entry, 11010 &cur_blocked->scsiio) == 0) { 11011 cur_blocked->io_hdr.flags |= 11012 CTL_FLAG_IS_WAS_ON_RTR; 11013 ctl_enqueue_rtr(cur_blocked); 11014 } else 11015 ctl_done(cur_blocked); 11016 break; 11017 } 11018 default: 11019 /* 11020 * This probably shouldn't happen -- we shouldn't 11021 * get CTL_ACTION_ERROR, or anything else. 11022 */ 11023 break; 11024 } 11025 } 11026 11027 return (CTL_RETVAL_COMPLETE); 11028} 11029 11030/* 11031 * This routine (with one exception) checks LUN flags that can be set by 11032 * commands ahead of us in the OOA queue. These flags have to be checked 11033 * when a command initially comes in, and when we pull a command off the 11034 * blocked queue and are preparing to execute it. The reason we have to 11035 * check these flags for commands on the blocked queue is that the LUN 11036 * state may have been changed by a command ahead of us while we're on the 11037 * blocked queue. 11038 * 11039 * Ordering is somewhat important with these checks, so please pay 11040 * careful attention to the placement of any new checks. 11041 */ 11042static int 11043ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11044 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11045{ 11046 int retval; 11047 11048 retval = 0; 11049 11050 mtx_assert(&lun->lun_lock, MA_OWNED); 11051 11052 /* 11053 * If this shelf is a secondary shelf controller, we have to reject 11054 * any media access commands. 11055 */ 11056#if 0 11057 /* No longer needed for HA */ 11058 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11059 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11060 ctl_set_lun_standby(ctsio); 11061 retval = 1; 11062 goto bailout; 11063 } 11064#endif 11065 11066 /* 11067 * Check for a reservation conflict. If this command isn't allowed 11068 * even on reserved LUNs, and if this initiator isn't the one who 11069 * reserved us, reject the command with a reservation conflict. 11070 */ 11071 if ((lun->flags & CTL_LUN_RESERVED) 11072 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11073 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11074 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11075 || (ctsio->io_hdr.nexus.targ_target.id != 11076 lun->rsv_nexus.targ_target.id)) { 11077 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11078 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11079 retval = 1; 11080 goto bailout; 11081 } 11082 } 11083 11084 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11085 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11086 uint32_t residx; 11087 11088 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11089 /* 11090 * if we aren't registered or it's a res holder type 11091 * reservation and this isn't the res holder then set a 11092 * conflict. 11093 * NOTE: Commands which might be allowed on write exclusive 11094 * type reservations are checked in the particular command 11095 * for a conflict. Read and SSU are the only ones. 11096 */ 11097 if (!lun->per_res[residx].registered 11098 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11099 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11100 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11101 retval = 1; 11102 goto bailout; 11103 } 11104 11105 } 11106 11107 if ((lun->flags & CTL_LUN_OFFLINE) 11108 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11109 ctl_set_lun_not_ready(ctsio); 11110 retval = 1; 11111 goto bailout; 11112 } 11113 11114 /* 11115 * If the LUN is stopped, see if this particular command is allowed 11116 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11117 */ 11118 if ((lun->flags & CTL_LUN_STOPPED) 11119 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11120 /* "Logical unit not ready, initializing cmd. required" */ 11121 ctl_set_lun_stopped(ctsio); 11122 retval = 1; 11123 goto bailout; 11124 } 11125 11126 if ((lun->flags & CTL_LUN_INOPERABLE) 11127 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11128 /* "Medium format corrupted" */ 11129 ctl_set_medium_format_corrupted(ctsio); 11130 retval = 1; 11131 goto bailout; 11132 } 11133 11134bailout: 11135 return (retval); 11136 11137} 11138 11139static void 11140ctl_failover_io(union ctl_io *io, int have_lock) 11141{ 11142 ctl_set_busy(&io->scsiio); 11143 ctl_done(io); 11144} 11145 11146static void 11147ctl_failover(void) 11148{ 11149 struct ctl_lun *lun; 11150 struct ctl_softc *ctl_softc; 11151 union ctl_io *next_io, *pending_io; 11152 union ctl_io *io; 11153 int lun_idx; 11154 int i; 11155 11156 ctl_softc = control_softc; 11157 11158 mtx_lock(&ctl_softc->ctl_lock); 11159 /* 11160 * Remove any cmds from the other SC from the rtr queue. These 11161 * will obviously only be for LUNs for which we're the primary. 11162 * We can't send status or get/send data for these commands. 11163 * Since they haven't been executed yet, we can just remove them. 11164 * We'll either abort them or delete them below, depending on 11165 * which HA mode we're in. 11166 */ 11167#ifdef notyet 11168 mtx_lock(&ctl_softc->queue_lock); 11169 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11170 io != NULL; io = next_io) { 11171 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11172 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11173 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11174 ctl_io_hdr, links); 11175 } 11176 mtx_unlock(&ctl_softc->queue_lock); 11177#endif 11178 11179 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11180 lun = ctl_softc->ctl_luns[lun_idx]; 11181 if (lun==NULL) 11182 continue; 11183 11184 /* 11185 * Processor LUNs are primary on both sides. 11186 * XXX will this always be true? 11187 */ 11188 if (lun->be_lun->lun_type == T_PROCESSOR) 11189 continue; 11190 11191 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11192 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11193 printf("FAILOVER: primary lun %d\n", lun_idx); 11194 /* 11195 * Remove all commands from the other SC. First from the 11196 * blocked queue then from the ooa queue. Once we have 11197 * removed them. Call ctl_check_blocked to see if there 11198 * is anything that can run. 11199 */ 11200 for (io = (union ctl_io *)TAILQ_FIRST( 11201 &lun->blocked_queue); io != NULL; io = next_io) { 11202 11203 next_io = (union ctl_io *)TAILQ_NEXT( 11204 &io->io_hdr, blocked_links); 11205 11206 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11207 TAILQ_REMOVE(&lun->blocked_queue, 11208 &io->io_hdr,blocked_links); 11209 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11210 TAILQ_REMOVE(&lun->ooa_queue, 11211 &io->io_hdr, ooa_links); 11212 11213 ctl_free_io(io); 11214 } 11215 } 11216 11217 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11218 io != NULL; io = next_io) { 11219 11220 next_io = (union ctl_io *)TAILQ_NEXT( 11221 &io->io_hdr, ooa_links); 11222 11223 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11224 11225 TAILQ_REMOVE(&lun->ooa_queue, 11226 &io->io_hdr, 11227 ooa_links); 11228 11229 ctl_free_io(io); 11230 } 11231 } 11232 ctl_check_blocked(lun); 11233 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11234 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11235 11236 printf("FAILOVER: primary lun %d\n", lun_idx); 11237 /* 11238 * Abort all commands from the other SC. We can't 11239 * send status back for them now. These should get 11240 * cleaned up when they are completed or come out 11241 * for a datamove operation. 11242 */ 11243 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11244 io != NULL; io = next_io) { 11245 next_io = (union ctl_io *)TAILQ_NEXT( 11246 &io->io_hdr, ooa_links); 11247 11248 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11249 io->io_hdr.flags |= CTL_FLAG_ABORT; 11250 } 11251 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11252 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11253 11254 printf("FAILOVER: secondary lun %d\n", lun_idx); 11255 11256 lun->flags |= CTL_LUN_PRIMARY_SC; 11257 11258 /* 11259 * We send all I/O that was sent to this controller 11260 * and redirected to the other side back with 11261 * busy status, and have the initiator retry it. 11262 * Figuring out how much data has been transferred, 11263 * etc. and picking up where we left off would be 11264 * very tricky. 11265 * 11266 * XXX KDM need to remove I/O from the blocked 11267 * queue as well! 11268 */ 11269 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11270 &lun->ooa_queue); pending_io != NULL; 11271 pending_io = next_io) { 11272 11273 next_io = (union ctl_io *)TAILQ_NEXT( 11274 &pending_io->io_hdr, ooa_links); 11275 11276 pending_io->io_hdr.flags &= 11277 ~CTL_FLAG_SENT_2OTHER_SC; 11278 11279 if (pending_io->io_hdr.flags & 11280 CTL_FLAG_IO_ACTIVE) { 11281 pending_io->io_hdr.flags |= 11282 CTL_FLAG_FAILOVER; 11283 } else { 11284 ctl_set_busy(&pending_io->scsiio); 11285 ctl_done(pending_io); 11286 } 11287 } 11288 11289 /* 11290 * Build Unit Attention 11291 */ 11292 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11293 lun->pending_sense[i].ua_pending |= 11294 CTL_UA_ASYM_ACC_CHANGE; 11295 } 11296 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11297 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11298 printf("FAILOVER: secondary lun %d\n", lun_idx); 11299 /* 11300 * if the first io on the OOA is not on the RtR queue 11301 * add it. 11302 */ 11303 lun->flags |= CTL_LUN_PRIMARY_SC; 11304 11305 pending_io = (union ctl_io *)TAILQ_FIRST( 11306 &lun->ooa_queue); 11307 if (pending_io==NULL) { 11308 printf("Nothing on OOA queue\n"); 11309 continue; 11310 } 11311 11312 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11313 if ((pending_io->io_hdr.flags & 11314 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11315 pending_io->io_hdr.flags |= 11316 CTL_FLAG_IS_WAS_ON_RTR; 11317 ctl_enqueue_rtr(pending_io); 11318 } 11319#if 0 11320 else 11321 { 11322 printf("Tag 0x%04x is running\n", 11323 pending_io->scsiio.tag_num); 11324 } 11325#endif 11326 11327 next_io = (union ctl_io *)TAILQ_NEXT( 11328 &pending_io->io_hdr, ooa_links); 11329 for (pending_io=next_io; pending_io != NULL; 11330 pending_io = next_io) { 11331 pending_io->io_hdr.flags &= 11332 ~CTL_FLAG_SENT_2OTHER_SC; 11333 next_io = (union ctl_io *)TAILQ_NEXT( 11334 &pending_io->io_hdr, ooa_links); 11335 if (pending_io->io_hdr.flags & 11336 CTL_FLAG_IS_WAS_ON_RTR) { 11337#if 0 11338 printf("Tag 0x%04x is running\n", 11339 pending_io->scsiio.tag_num); 11340#endif 11341 continue; 11342 } 11343 11344 switch (ctl_check_ooa(lun, pending_io, 11345 (union ctl_io *)TAILQ_PREV( 11346 &pending_io->io_hdr, ctl_ooaq, 11347 ooa_links))) { 11348 11349 case CTL_ACTION_BLOCK: 11350 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11351 &pending_io->io_hdr, 11352 blocked_links); 11353 pending_io->io_hdr.flags |= 11354 CTL_FLAG_BLOCKED; 11355 break; 11356 case CTL_ACTION_PASS: 11357 case CTL_ACTION_SKIP: 11358 pending_io->io_hdr.flags |= 11359 CTL_FLAG_IS_WAS_ON_RTR; 11360 ctl_enqueue_rtr(pending_io); 11361 break; 11362 case CTL_ACTION_OVERLAP: 11363 ctl_set_overlapped_cmd( 11364 (struct ctl_scsiio *)pending_io); 11365 ctl_done(pending_io); 11366 break; 11367 case CTL_ACTION_OVERLAP_TAG: 11368 ctl_set_overlapped_tag( 11369 (struct ctl_scsiio *)pending_io, 11370 pending_io->scsiio.tag_num & 0xff); 11371 ctl_done(pending_io); 11372 break; 11373 case CTL_ACTION_ERROR: 11374 default: 11375 ctl_set_internal_failure( 11376 (struct ctl_scsiio *)pending_io, 11377 0, // sks_valid 11378 0); //retry count 11379 ctl_done(pending_io); 11380 break; 11381 } 11382 } 11383 11384 /* 11385 * Build Unit Attention 11386 */ 11387 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11388 lun->pending_sense[i].ua_pending |= 11389 CTL_UA_ASYM_ACC_CHANGE; 11390 } 11391 } else { 11392 panic("Unhandled HA mode failover, LUN flags = %#x, " 11393 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11394 } 11395 } 11396 ctl_pause_rtr = 0; 11397 mtx_unlock(&ctl_softc->ctl_lock); 11398} 11399 11400static int 11401ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11402{ 11403 struct ctl_lun *lun; 11404 const struct ctl_cmd_entry *entry; 11405 uint32_t initidx, targ_lun; 11406 int retval; 11407 11408 retval = 0; 11409 11410 lun = NULL; 11411 11412 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11413 if ((targ_lun < CTL_MAX_LUNS) 11414 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11415 lun = ctl_softc->ctl_luns[targ_lun]; 11416 /* 11417 * If the LUN is invalid, pretend that it doesn't exist. 11418 * It will go away as soon as all pending I/O has been 11419 * completed. 11420 */ 11421 if (lun->flags & CTL_LUN_DISABLED) { 11422 lun = NULL; 11423 } else { 11424 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11425 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11426 lun->be_lun; 11427 if (lun->be_lun->lun_type == T_PROCESSOR) { 11428 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11429 } 11430 11431 /* 11432 * Every I/O goes into the OOA queue for a 11433 * particular LUN, and stays there until completion. 11434 */ 11435 mtx_lock(&lun->lun_lock); 11436 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11437 ooa_links); 11438 } 11439 } else { 11440 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11441 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11442 } 11443 11444 /* Get command entry and return error if it is unsuppotyed. */ 11445 entry = ctl_validate_command(ctsio); 11446 if (entry == NULL) { 11447 if (lun) 11448 mtx_unlock(&lun->lun_lock); 11449 return (retval); 11450 } 11451 11452 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11453 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11454 11455 /* 11456 * Check to see whether we can send this command to LUNs that don't 11457 * exist. This should pretty much only be the case for inquiry 11458 * and request sense. Further checks, below, really require having 11459 * a LUN, so we can't really check the command anymore. Just put 11460 * it on the rtr queue. 11461 */ 11462 if (lun == NULL) { 11463 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11464 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11465 ctl_enqueue_rtr((union ctl_io *)ctsio); 11466 return (retval); 11467 } 11468 11469 ctl_set_unsupported_lun(ctsio); 11470 ctl_done((union ctl_io *)ctsio); 11471 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11472 return (retval); 11473 } else { 11474 /* 11475 * Make sure we support this particular command on this LUN. 11476 * e.g., we don't support writes to the control LUN. 11477 */ 11478 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11479 mtx_unlock(&lun->lun_lock); 11480 ctl_set_invalid_opcode(ctsio); 11481 ctl_done((union ctl_io *)ctsio); 11482 return (retval); 11483 } 11484 } 11485 11486 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11487 11488 /* 11489 * If we've got a request sense, it'll clear the contingent 11490 * allegiance condition. Otherwise, if we have a CA condition for 11491 * this initiator, clear it, because it sent down a command other 11492 * than request sense. 11493 */ 11494 if ((ctsio->cdb[0] != REQUEST_SENSE) 11495 && (ctl_is_set(lun->have_ca, initidx))) 11496 ctl_clear_mask(lun->have_ca, initidx); 11497 11498 /* 11499 * If the command has this flag set, it handles its own unit 11500 * attention reporting, we shouldn't do anything. Otherwise we 11501 * check for any pending unit attentions, and send them back to the 11502 * initiator. We only do this when a command initially comes in, 11503 * not when we pull it off the blocked queue. 11504 * 11505 * According to SAM-3, section 5.3.2, the order that things get 11506 * presented back to the host is basically unit attentions caused 11507 * by some sort of reset event, busy status, reservation conflicts 11508 * or task set full, and finally any other status. 11509 * 11510 * One issue here is that some of the unit attentions we report 11511 * don't fall into the "reset" category (e.g. "reported luns data 11512 * has changed"). So reporting it here, before the reservation 11513 * check, may be technically wrong. I guess the only thing to do 11514 * would be to check for and report the reset events here, and then 11515 * check for the other unit attention types after we check for a 11516 * reservation conflict. 11517 * 11518 * XXX KDM need to fix this 11519 */ 11520 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11521 ctl_ua_type ua_type; 11522 11523 ua_type = lun->pending_sense[initidx].ua_pending; 11524 if (ua_type != CTL_UA_NONE) { 11525 scsi_sense_data_type sense_format; 11526 11527 if (lun != NULL) 11528 sense_format = (lun->flags & 11529 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11530 SSD_TYPE_FIXED; 11531 else 11532 sense_format = SSD_TYPE_FIXED; 11533 11534 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11535 sense_format); 11536 if (ua_type != CTL_UA_NONE) { 11537 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11538 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11539 CTL_AUTOSENSE; 11540 ctsio->sense_len = SSD_FULL_SIZE; 11541 lun->pending_sense[initidx].ua_pending &= 11542 ~ua_type; 11543 mtx_unlock(&lun->lun_lock); 11544 ctl_done((union ctl_io *)ctsio); 11545 return (retval); 11546 } 11547 } 11548 } 11549 11550 11551 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11552 mtx_unlock(&lun->lun_lock); 11553 ctl_done((union ctl_io *)ctsio); 11554 return (retval); 11555 } 11556 11557 /* 11558 * XXX CHD this is where we want to send IO to other side if 11559 * this LUN is secondary on this SC. We will need to make a copy 11560 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11561 * the copy we send as FROM_OTHER. 11562 * We also need to stuff the address of the original IO so we can 11563 * find it easily. Something similar will need be done on the other 11564 * side so when we are done we can find the copy. 11565 */ 11566 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11567 union ctl_ha_msg msg_info; 11568 int isc_retval; 11569 11570 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11571 11572 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11573 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11574#if 0 11575 printf("1. ctsio %p\n", ctsio); 11576#endif 11577 msg_info.hdr.serializing_sc = NULL; 11578 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11579 msg_info.scsi.tag_num = ctsio->tag_num; 11580 msg_info.scsi.tag_type = ctsio->tag_type; 11581 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11582 11583 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11584 11585 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11586 (void *)&msg_info, sizeof(msg_info), 0)) > 11587 CTL_HA_STATUS_SUCCESS) { 11588 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11589 isc_retval); 11590 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11591 } else { 11592#if 0 11593 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11594#endif 11595 } 11596 11597 /* 11598 * XXX KDM this I/O is off the incoming queue, but hasn't 11599 * been inserted on any other queue. We may need to come 11600 * up with a holding queue while we wait for serialization 11601 * so that we have an idea of what we're waiting for from 11602 * the other side. 11603 */ 11604 mtx_unlock(&lun->lun_lock); 11605 return (retval); 11606 } 11607 11608 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11609 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11610 ctl_ooaq, ooa_links))) { 11611 case CTL_ACTION_BLOCK: 11612 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11613 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11614 blocked_links); 11615 mtx_unlock(&lun->lun_lock); 11616 return (retval); 11617 case CTL_ACTION_PASS: 11618 case CTL_ACTION_SKIP: 11619 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11620 mtx_unlock(&lun->lun_lock); 11621 ctl_enqueue_rtr((union ctl_io *)ctsio); 11622 break; 11623 case CTL_ACTION_OVERLAP: 11624 mtx_unlock(&lun->lun_lock); 11625 ctl_set_overlapped_cmd(ctsio); 11626 ctl_done((union ctl_io *)ctsio); 11627 break; 11628 case CTL_ACTION_OVERLAP_TAG: 11629 mtx_unlock(&lun->lun_lock); 11630 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11631 ctl_done((union ctl_io *)ctsio); 11632 break; 11633 case CTL_ACTION_ERROR: 11634 default: 11635 mtx_unlock(&lun->lun_lock); 11636 ctl_set_internal_failure(ctsio, 11637 /*sks_valid*/ 0, 11638 /*retry_count*/ 0); 11639 ctl_done((union ctl_io *)ctsio); 11640 break; 11641 } 11642 return (retval); 11643} 11644 11645const struct ctl_cmd_entry * 11646ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11647{ 11648 const struct ctl_cmd_entry *entry; 11649 int service_action; 11650 11651 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11652 if (entry->flags & CTL_CMD_FLAG_SA5) { 11653 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11654 entry = &((const struct ctl_cmd_entry *) 11655 entry->execute)[service_action]; 11656 } 11657 return (entry); 11658} 11659 11660const struct ctl_cmd_entry * 11661ctl_validate_command(struct ctl_scsiio *ctsio) 11662{ 11663 const struct ctl_cmd_entry *entry; 11664 int i; 11665 uint8_t diff; 11666 11667 entry = ctl_get_cmd_entry(ctsio); 11668 if (entry->execute == NULL) { 11669 ctl_set_invalid_opcode(ctsio); 11670 ctl_done((union ctl_io *)ctsio); 11671 return (NULL); 11672 } 11673 KASSERT(entry->length > 0, 11674 ("Not defined length for command 0x%02x/0x%02x", 11675 ctsio->cdb[0], ctsio->cdb[1])); 11676 for (i = 1; i < entry->length; i++) { 11677 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11678 if (diff == 0) 11679 continue; 11680 ctl_set_invalid_field(ctsio, 11681 /*sks_valid*/ 1, 11682 /*command*/ 1, 11683 /*field*/ i, 11684 /*bit_valid*/ 1, 11685 /*bit*/ fls(diff) - 1); 11686 ctl_done((union ctl_io *)ctsio); 11687 return (NULL); 11688 } 11689 return (entry); 11690} 11691 11692static int 11693ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11694{ 11695 11696 switch (lun_type) { 11697 case T_PROCESSOR: 11698 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11699 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11700 return (0); 11701 break; 11702 case T_DIRECT: 11703 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11704 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11705 return (0); 11706 break; 11707 default: 11708 return (0); 11709 } 11710 return (1); 11711} 11712 11713static int 11714ctl_scsiio(struct ctl_scsiio *ctsio) 11715{ 11716 int retval; 11717 const struct ctl_cmd_entry *entry; 11718 11719 retval = CTL_RETVAL_COMPLETE; 11720 11721 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11722 11723 entry = ctl_get_cmd_entry(ctsio); 11724 11725 /* 11726 * If this I/O has been aborted, just send it straight to 11727 * ctl_done() without executing it. 11728 */ 11729 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11730 ctl_done((union ctl_io *)ctsio); 11731 goto bailout; 11732 } 11733 11734 /* 11735 * All the checks should have been handled by ctl_scsiio_precheck(). 11736 * We should be clear now to just execute the I/O. 11737 */ 11738 retval = entry->execute(ctsio); 11739 11740bailout: 11741 return (retval); 11742} 11743 11744/* 11745 * Since we only implement one target right now, a bus reset simply resets 11746 * our single target. 11747 */ 11748static int 11749ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11750{ 11751 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11752} 11753 11754static int 11755ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11756 ctl_ua_type ua_type) 11757{ 11758 struct ctl_lun *lun; 11759 int retval; 11760 11761 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11762 union ctl_ha_msg msg_info; 11763 11764 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11765 msg_info.hdr.nexus = io->io_hdr.nexus; 11766 if (ua_type==CTL_UA_TARG_RESET) 11767 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11768 else 11769 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11770 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11771 msg_info.hdr.original_sc = NULL; 11772 msg_info.hdr.serializing_sc = NULL; 11773 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11774 (void *)&msg_info, sizeof(msg_info), 0)) { 11775 } 11776 } 11777 retval = 0; 11778 11779 mtx_lock(&ctl_softc->ctl_lock); 11780 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11781 retval += ctl_lun_reset(lun, io, ua_type); 11782 mtx_unlock(&ctl_softc->ctl_lock); 11783 11784 return (retval); 11785} 11786 11787/* 11788 * The LUN should always be set. The I/O is optional, and is used to 11789 * distinguish between I/Os sent by this initiator, and by other 11790 * initiators. We set unit attention for initiators other than this one. 11791 * SAM-3 is vague on this point. It does say that a unit attention should 11792 * be established for other initiators when a LUN is reset (see section 11793 * 5.7.3), but it doesn't specifically say that the unit attention should 11794 * be established for this particular initiator when a LUN is reset. Here 11795 * is the relevant text, from SAM-3 rev 8: 11796 * 11797 * 5.7.2 When a SCSI initiator port aborts its own tasks 11798 * 11799 * When a SCSI initiator port causes its own task(s) to be aborted, no 11800 * notification that the task(s) have been aborted shall be returned to 11801 * the SCSI initiator port other than the completion response for the 11802 * command or task management function action that caused the task(s) to 11803 * be aborted and notification(s) associated with related effects of the 11804 * action (e.g., a reset unit attention condition). 11805 * 11806 * XXX KDM for now, we're setting unit attention for all initiators. 11807 */ 11808static int 11809ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11810{ 11811 union ctl_io *xio; 11812#if 0 11813 uint32_t initindex; 11814#endif 11815 int i; 11816 11817 mtx_lock(&lun->lun_lock); 11818 /* 11819 * Run through the OOA queue and abort each I/O. 11820 */ 11821#if 0 11822 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11823#endif 11824 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11825 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11826 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11827 } 11828 11829 /* 11830 * This version sets unit attention for every 11831 */ 11832#if 0 11833 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11834 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11835 if (initindex == i) 11836 continue; 11837 lun->pending_sense[i].ua_pending |= ua_type; 11838 } 11839#endif 11840 11841 /* 11842 * A reset (any kind, really) clears reservations established with 11843 * RESERVE/RELEASE. It does not clear reservations established 11844 * with PERSISTENT RESERVE OUT, but we don't support that at the 11845 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11846 * reservations made with the RESERVE/RELEASE commands, because 11847 * those commands are obsolete in SPC-3. 11848 */ 11849 lun->flags &= ~CTL_LUN_RESERVED; 11850 11851 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11852 ctl_clear_mask(lun->have_ca, i); 11853 lun->pending_sense[i].ua_pending |= ua_type; 11854 } 11855 mtx_unlock(&lun->lun_lock); 11856 11857 return (0); 11858} 11859 11860static int 11861ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11862 int other_sc) 11863{ 11864 union ctl_io *xio; 11865 int found; 11866 11867 mtx_assert(&lun->lun_lock, MA_OWNED); 11868 11869 /* 11870 * Run through the OOA queue and attempt to find the given I/O. 11871 * The target port, initiator ID, tag type and tag number have to 11872 * match the values that we got from the initiator. If we have an 11873 * untagged command to abort, simply abort the first untagged command 11874 * we come to. We only allow one untagged command at a time of course. 11875 */ 11876 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11877 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11878 11879 if ((targ_port == UINT32_MAX || 11880 targ_port == xio->io_hdr.nexus.targ_port) && 11881 (init_id == UINT32_MAX || 11882 init_id == xio->io_hdr.nexus.initid.id)) { 11883 if (targ_port != xio->io_hdr.nexus.targ_port || 11884 init_id != xio->io_hdr.nexus.initid.id) 11885 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11886 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11887 found = 1; 11888 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11889 union ctl_ha_msg msg_info; 11890 11891 msg_info.hdr.nexus = xio->io_hdr.nexus; 11892 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11893 msg_info.task.tag_num = xio->scsiio.tag_num; 11894 msg_info.task.tag_type = xio->scsiio.tag_type; 11895 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11896 msg_info.hdr.original_sc = NULL; 11897 msg_info.hdr.serializing_sc = NULL; 11898 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11899 (void *)&msg_info, sizeof(msg_info), 0); 11900 } 11901 } 11902 } 11903 return (found); 11904} 11905 11906static int 11907ctl_abort_task_set(union ctl_io *io) 11908{ 11909 struct ctl_softc *softc = control_softc; 11910 struct ctl_lun *lun; 11911 uint32_t targ_lun; 11912 11913 /* 11914 * Look up the LUN. 11915 */ 11916 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11917 mtx_lock(&softc->ctl_lock); 11918 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11919 lun = softc->ctl_luns[targ_lun]; 11920 else { 11921 mtx_unlock(&softc->ctl_lock); 11922 return (1); 11923 } 11924 11925 mtx_lock(&lun->lun_lock); 11926 mtx_unlock(&softc->ctl_lock); 11927 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11928 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11929 io->io_hdr.nexus.initid.id, 11930 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11931 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11932 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11933 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11934 } 11935 mtx_unlock(&lun->lun_lock); 11936 return (0); 11937} 11938 11939static int 11940ctl_i_t_nexus_reset(union ctl_io *io) 11941{ 11942 struct ctl_softc *softc = control_softc; 11943 struct ctl_lun *lun; 11944 uint32_t initindex; 11945 11946 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11947 mtx_lock(&softc->ctl_lock); 11948 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11949 mtx_lock(&lun->lun_lock); 11950 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11951 io->io_hdr.nexus.initid.id, 11952 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11953 ctl_clear_mask(lun->have_ca, initindex); 11954 lun->pending_sense[initindex].ua_pending |= CTL_UA_I_T_NEXUS_LOSS; 11955 mtx_unlock(&lun->lun_lock); 11956 } 11957 mtx_unlock(&softc->ctl_lock); 11958 return (0); 11959} 11960 11961static int 11962ctl_abort_task(union ctl_io *io) 11963{ 11964 union ctl_io *xio; 11965 struct ctl_lun *lun; 11966 struct ctl_softc *ctl_softc; 11967#if 0 11968 struct sbuf sb; 11969 char printbuf[128]; 11970#endif 11971 int found; 11972 uint32_t targ_lun; 11973 11974 ctl_softc = control_softc; 11975 found = 0; 11976 11977 /* 11978 * Look up the LUN. 11979 */ 11980 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11981 mtx_lock(&ctl_softc->ctl_lock); 11982 if ((targ_lun < CTL_MAX_LUNS) 11983 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11984 lun = ctl_softc->ctl_luns[targ_lun]; 11985 else { 11986 mtx_unlock(&ctl_softc->ctl_lock); 11987 return (1); 11988 } 11989 11990#if 0 11991 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11992 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11993#endif 11994 11995 mtx_lock(&lun->lun_lock); 11996 mtx_unlock(&ctl_softc->ctl_lock); 11997 /* 11998 * Run through the OOA queue and attempt to find the given I/O. 11999 * The target port, initiator ID, tag type and tag number have to 12000 * match the values that we got from the initiator. If we have an 12001 * untagged command to abort, simply abort the first untagged command 12002 * we come to. We only allow one untagged command at a time of course. 12003 */ 12004#if 0 12005 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12006#endif 12007 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12008 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12009#if 0 12010 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12011 12012 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12013 lun->lun, xio->scsiio.tag_num, 12014 xio->scsiio.tag_type, 12015 (xio->io_hdr.blocked_links.tqe_prev 12016 == NULL) ? "" : " BLOCKED", 12017 (xio->io_hdr.flags & 12018 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12019 (xio->io_hdr.flags & 12020 CTL_FLAG_ABORT) ? " ABORT" : "", 12021 (xio->io_hdr.flags & 12022 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12023 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12024 sbuf_finish(&sb); 12025 printf("%s\n", sbuf_data(&sb)); 12026#endif 12027 12028 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12029 && (xio->io_hdr.nexus.initid.id == 12030 io->io_hdr.nexus.initid.id)) { 12031 /* 12032 * If the abort says that the task is untagged, the 12033 * task in the queue must be untagged. Otherwise, 12034 * we just check to see whether the tag numbers 12035 * match. This is because the QLogic firmware 12036 * doesn't pass back the tag type in an abort 12037 * request. 12038 */ 12039#if 0 12040 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12041 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12042 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12043#endif 12044 /* 12045 * XXX KDM we've got problems with FC, because it 12046 * doesn't send down a tag type with aborts. So we 12047 * can only really go by the tag number... 12048 * This may cause problems with parallel SCSI. 12049 * Need to figure that out!! 12050 */ 12051 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12052 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12053 found = 1; 12054 if ((io->io_hdr.flags & 12055 CTL_FLAG_FROM_OTHER_SC) == 0 && 12056 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12057 union ctl_ha_msg msg_info; 12058 12059 io->io_hdr.flags |= 12060 CTL_FLAG_SENT_2OTHER_SC; 12061 msg_info.hdr.nexus = io->io_hdr.nexus; 12062 msg_info.task.task_action = 12063 CTL_TASK_ABORT_TASK; 12064 msg_info.task.tag_num = 12065 io->taskio.tag_num; 12066 msg_info.task.tag_type = 12067 io->taskio.tag_type; 12068 msg_info.hdr.msg_type = 12069 CTL_MSG_MANAGE_TASKS; 12070 msg_info.hdr.original_sc = NULL; 12071 msg_info.hdr.serializing_sc = NULL; 12072#if 0 12073 printf("Sent Abort to other side\n"); 12074#endif 12075 if (CTL_HA_STATUS_SUCCESS != 12076 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12077 (void *)&msg_info, 12078 sizeof(msg_info), 0)) { 12079 } 12080 } 12081#if 0 12082 printf("ctl_abort_task: found I/O to abort\n"); 12083#endif 12084 break; 12085 } 12086 } 12087 } 12088 mtx_unlock(&lun->lun_lock); 12089 12090 if (found == 0) { 12091 /* 12092 * This isn't really an error. It's entirely possible for 12093 * the abort and command completion to cross on the wire. 12094 * This is more of an informative/diagnostic error. 12095 */ 12096#if 0 12097 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12098 "%d:%d:%d:%d tag %d type %d\n", 12099 io->io_hdr.nexus.initid.id, 12100 io->io_hdr.nexus.targ_port, 12101 io->io_hdr.nexus.targ_target.id, 12102 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12103 io->taskio.tag_type); 12104#endif 12105 } 12106 return (0); 12107} 12108 12109static void 12110ctl_run_task(union ctl_io *io) 12111{ 12112 struct ctl_softc *ctl_softc = control_softc; 12113 int retval = 1; 12114 const char *task_desc; 12115 12116 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12117 12118 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12119 ("ctl_run_task: Unextected io_type %d\n", 12120 io->io_hdr.io_type)); 12121 12122 task_desc = ctl_scsi_task_string(&io->taskio); 12123 if (task_desc != NULL) { 12124#ifdef NEEDTOPORT 12125 csevent_log(CSC_CTL | CSC_SHELF_SW | 12126 CTL_TASK_REPORT, 12127 csevent_LogType_Trace, 12128 csevent_Severity_Information, 12129 csevent_AlertLevel_Green, 12130 csevent_FRU_Firmware, 12131 csevent_FRU_Unknown, 12132 "CTL: received task: %s",task_desc); 12133#endif 12134 } else { 12135#ifdef NEEDTOPORT 12136 csevent_log(CSC_CTL | CSC_SHELF_SW | 12137 CTL_TASK_REPORT, 12138 csevent_LogType_Trace, 12139 csevent_Severity_Information, 12140 csevent_AlertLevel_Green, 12141 csevent_FRU_Firmware, 12142 csevent_FRU_Unknown, 12143 "CTL: received unknown task " 12144 "type: %d (%#x)", 12145 io->taskio.task_action, 12146 io->taskio.task_action); 12147#endif 12148 } 12149 switch (io->taskio.task_action) { 12150 case CTL_TASK_ABORT_TASK: 12151 retval = ctl_abort_task(io); 12152 break; 12153 case CTL_TASK_ABORT_TASK_SET: 12154 case CTL_TASK_CLEAR_TASK_SET: 12155 retval = ctl_abort_task_set(io); 12156 break; 12157 case CTL_TASK_CLEAR_ACA: 12158 break; 12159 case CTL_TASK_I_T_NEXUS_RESET: 12160 retval = ctl_i_t_nexus_reset(io); 12161 break; 12162 case CTL_TASK_LUN_RESET: { 12163 struct ctl_lun *lun; 12164 uint32_t targ_lun; 12165 12166 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12167 mtx_lock(&ctl_softc->ctl_lock); 12168 if ((targ_lun < CTL_MAX_LUNS) 12169 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12170 lun = ctl_softc->ctl_luns[targ_lun]; 12171 else { 12172 mtx_unlock(&ctl_softc->ctl_lock); 12173 retval = 1; 12174 break; 12175 } 12176 12177 if (!(io->io_hdr.flags & 12178 CTL_FLAG_FROM_OTHER_SC)) { 12179 union ctl_ha_msg msg_info; 12180 12181 io->io_hdr.flags |= 12182 CTL_FLAG_SENT_2OTHER_SC; 12183 msg_info.hdr.msg_type = 12184 CTL_MSG_MANAGE_TASKS; 12185 msg_info.hdr.nexus = io->io_hdr.nexus; 12186 msg_info.task.task_action = 12187 CTL_TASK_LUN_RESET; 12188 msg_info.hdr.original_sc = NULL; 12189 msg_info.hdr.serializing_sc = NULL; 12190 if (CTL_HA_STATUS_SUCCESS != 12191 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12192 (void *)&msg_info, 12193 sizeof(msg_info), 0)) { 12194 } 12195 } 12196 12197 retval = ctl_lun_reset(lun, io, 12198 CTL_UA_LUN_RESET); 12199 mtx_unlock(&ctl_softc->ctl_lock); 12200 break; 12201 } 12202 case CTL_TASK_TARGET_RESET: 12203 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12204 break; 12205 case CTL_TASK_BUS_RESET: 12206 retval = ctl_bus_reset(ctl_softc, io); 12207 break; 12208 case CTL_TASK_PORT_LOGIN: 12209 break; 12210 case CTL_TASK_PORT_LOGOUT: 12211 break; 12212 default: 12213 printf("ctl_run_task: got unknown task management event %d\n", 12214 io->taskio.task_action); 12215 break; 12216 } 12217 if (retval == 0) 12218 io->io_hdr.status = CTL_SUCCESS; 12219 else 12220 io->io_hdr.status = CTL_ERROR; 12221 ctl_done(io); 12222} 12223 12224/* 12225 * For HA operation. Handle commands that come in from the other 12226 * controller. 12227 */ 12228static void 12229ctl_handle_isc(union ctl_io *io) 12230{ 12231 int free_io; 12232 struct ctl_lun *lun; 12233 struct ctl_softc *ctl_softc; 12234 uint32_t targ_lun; 12235 12236 ctl_softc = control_softc; 12237 12238 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12239 lun = ctl_softc->ctl_luns[targ_lun]; 12240 12241 switch (io->io_hdr.msg_type) { 12242 case CTL_MSG_SERIALIZE: 12243 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12244 break; 12245 case CTL_MSG_R2R: { 12246 const struct ctl_cmd_entry *entry; 12247 12248 /* 12249 * This is only used in SER_ONLY mode. 12250 */ 12251 free_io = 0; 12252 entry = ctl_get_cmd_entry(&io->scsiio); 12253 mtx_lock(&lun->lun_lock); 12254 if (ctl_scsiio_lun_check(ctl_softc, lun, 12255 entry, (struct ctl_scsiio *)io) != 0) { 12256 mtx_unlock(&lun->lun_lock); 12257 ctl_done(io); 12258 break; 12259 } 12260 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12261 mtx_unlock(&lun->lun_lock); 12262 ctl_enqueue_rtr(io); 12263 break; 12264 } 12265 case CTL_MSG_FINISH_IO: 12266 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12267 free_io = 0; 12268 ctl_done(io); 12269 } else { 12270 free_io = 1; 12271 mtx_lock(&lun->lun_lock); 12272 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12273 ooa_links); 12274 ctl_check_blocked(lun); 12275 mtx_unlock(&lun->lun_lock); 12276 } 12277 break; 12278 case CTL_MSG_PERS_ACTION: 12279 ctl_hndl_per_res_out_on_other_sc( 12280 (union ctl_ha_msg *)&io->presio.pr_msg); 12281 free_io = 1; 12282 break; 12283 case CTL_MSG_BAD_JUJU: 12284 free_io = 0; 12285 ctl_done(io); 12286 break; 12287 case CTL_MSG_DATAMOVE: 12288 /* Only used in XFER mode */ 12289 free_io = 0; 12290 ctl_datamove_remote(io); 12291 break; 12292 case CTL_MSG_DATAMOVE_DONE: 12293 /* Only used in XFER mode */ 12294 free_io = 0; 12295 io->scsiio.be_move_done(io); 12296 break; 12297 default: 12298 free_io = 1; 12299 printf("%s: Invalid message type %d\n", 12300 __func__, io->io_hdr.msg_type); 12301 break; 12302 } 12303 if (free_io) 12304 ctl_free_io(io); 12305 12306} 12307 12308 12309/* 12310 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12311 * there is no match. 12312 */ 12313static ctl_lun_error_pattern 12314ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12315{ 12316 const struct ctl_cmd_entry *entry; 12317 ctl_lun_error_pattern filtered_pattern, pattern; 12318 12319 pattern = desc->error_pattern; 12320 12321 /* 12322 * XXX KDM we need more data passed into this function to match a 12323 * custom pattern, and we actually need to implement custom pattern 12324 * matching. 12325 */ 12326 if (pattern & CTL_LUN_PAT_CMD) 12327 return (CTL_LUN_PAT_CMD); 12328 12329 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12330 return (CTL_LUN_PAT_ANY); 12331 12332 entry = ctl_get_cmd_entry(ctsio); 12333 12334 filtered_pattern = entry->pattern & pattern; 12335 12336 /* 12337 * If the user requested specific flags in the pattern (e.g. 12338 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12339 * flags. 12340 * 12341 * If the user did not specify any flags, it doesn't matter whether 12342 * or not the command supports the flags. 12343 */ 12344 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12345 (pattern & ~CTL_LUN_PAT_MASK)) 12346 return (CTL_LUN_PAT_NONE); 12347 12348 /* 12349 * If the user asked for a range check, see if the requested LBA 12350 * range overlaps with this command's LBA range. 12351 */ 12352 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12353 uint64_t lba1; 12354 uint32_t len1; 12355 ctl_action action; 12356 int retval; 12357 12358 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12359 if (retval != 0) 12360 return (CTL_LUN_PAT_NONE); 12361 12362 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12363 desc->lba_range.len); 12364 /* 12365 * A "pass" means that the LBA ranges don't overlap, so 12366 * this doesn't match the user's range criteria. 12367 */ 12368 if (action == CTL_ACTION_PASS) 12369 return (CTL_LUN_PAT_NONE); 12370 } 12371 12372 return (filtered_pattern); 12373} 12374 12375static void 12376ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12377{ 12378 struct ctl_error_desc *desc, *desc2; 12379 12380 mtx_assert(&lun->lun_lock, MA_OWNED); 12381 12382 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12383 ctl_lun_error_pattern pattern; 12384 /* 12385 * Check to see whether this particular command matches 12386 * the pattern in the descriptor. 12387 */ 12388 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12389 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12390 continue; 12391 12392 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12393 case CTL_LUN_INJ_ABORTED: 12394 ctl_set_aborted(&io->scsiio); 12395 break; 12396 case CTL_LUN_INJ_MEDIUM_ERR: 12397 ctl_set_medium_error(&io->scsiio); 12398 break; 12399 case CTL_LUN_INJ_UA: 12400 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12401 * OCCURRED */ 12402 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12403 break; 12404 case CTL_LUN_INJ_CUSTOM: 12405 /* 12406 * We're assuming the user knows what he is doing. 12407 * Just copy the sense information without doing 12408 * checks. 12409 */ 12410 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12411 ctl_min(sizeof(desc->custom_sense), 12412 sizeof(io->scsiio.sense_data))); 12413 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12414 io->scsiio.sense_len = SSD_FULL_SIZE; 12415 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12416 break; 12417 case CTL_LUN_INJ_NONE: 12418 default: 12419 /* 12420 * If this is an error injection type we don't know 12421 * about, clear the continuous flag (if it is set) 12422 * so it will get deleted below. 12423 */ 12424 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12425 break; 12426 } 12427 /* 12428 * By default, each error injection action is a one-shot 12429 */ 12430 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12431 continue; 12432 12433 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12434 12435 free(desc, M_CTL); 12436 } 12437} 12438 12439#ifdef CTL_IO_DELAY 12440static void 12441ctl_datamove_timer_wakeup(void *arg) 12442{ 12443 union ctl_io *io; 12444 12445 io = (union ctl_io *)arg; 12446 12447 ctl_datamove(io); 12448} 12449#endif /* CTL_IO_DELAY */ 12450 12451void 12452ctl_datamove(union ctl_io *io) 12453{ 12454 void (*fe_datamove)(union ctl_io *io); 12455 12456 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12457 12458 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12459 12460#ifdef CTL_TIME_IO 12461 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12462 char str[256]; 12463 char path_str[64]; 12464 struct sbuf sb; 12465 12466 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12467 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12468 12469 sbuf_cat(&sb, path_str); 12470 switch (io->io_hdr.io_type) { 12471 case CTL_IO_SCSI: 12472 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12473 sbuf_printf(&sb, "\n"); 12474 sbuf_cat(&sb, path_str); 12475 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12476 io->scsiio.tag_num, io->scsiio.tag_type); 12477 break; 12478 case CTL_IO_TASK: 12479 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12480 "Tag Type: %d\n", io->taskio.task_action, 12481 io->taskio.tag_num, io->taskio.tag_type); 12482 break; 12483 default: 12484 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12485 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12486 break; 12487 } 12488 sbuf_cat(&sb, path_str); 12489 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12490 (intmax_t)time_uptime - io->io_hdr.start_time); 12491 sbuf_finish(&sb); 12492 printf("%s", sbuf_data(&sb)); 12493 } 12494#endif /* CTL_TIME_IO */ 12495 12496#ifdef CTL_IO_DELAY 12497 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12498 struct ctl_lun *lun; 12499 12500 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12501 12502 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12503 } else { 12504 struct ctl_lun *lun; 12505 12506 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12507 if ((lun != NULL) 12508 && (lun->delay_info.datamove_delay > 0)) { 12509 struct callout *callout; 12510 12511 callout = (struct callout *)&io->io_hdr.timer_bytes; 12512 callout_init(callout, /*mpsafe*/ 1); 12513 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12514 callout_reset(callout, 12515 lun->delay_info.datamove_delay * hz, 12516 ctl_datamove_timer_wakeup, io); 12517 if (lun->delay_info.datamove_type == 12518 CTL_DELAY_TYPE_ONESHOT) 12519 lun->delay_info.datamove_delay = 0; 12520 return; 12521 } 12522 } 12523#endif 12524 12525 /* 12526 * This command has been aborted. Set the port status, so we fail 12527 * the data move. 12528 */ 12529 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12530 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12531 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12532 io->io_hdr.nexus.targ_port, 12533 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12534 io->io_hdr.nexus.targ_lun); 12535 io->io_hdr.port_status = 31337; 12536 /* 12537 * Note that the backend, in this case, will get the 12538 * callback in its context. In other cases it may get 12539 * called in the frontend's interrupt thread context. 12540 */ 12541 io->scsiio.be_move_done(io); 12542 return; 12543 } 12544 12545 /* 12546 * If we're in XFER mode and this I/O is from the other shelf 12547 * controller, we need to send the DMA to the other side to 12548 * actually transfer the data to/from the host. In serialize only 12549 * mode the transfer happens below CTL and ctl_datamove() is only 12550 * called on the machine that originally received the I/O. 12551 */ 12552 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12553 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12554 union ctl_ha_msg msg; 12555 uint32_t sg_entries_sent; 12556 int do_sg_copy; 12557 int i; 12558 12559 memset(&msg, 0, sizeof(msg)); 12560 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12561 msg.hdr.original_sc = io->io_hdr.original_sc; 12562 msg.hdr.serializing_sc = io; 12563 msg.hdr.nexus = io->io_hdr.nexus; 12564 msg.dt.flags = io->io_hdr.flags; 12565 /* 12566 * We convert everything into a S/G list here. We can't 12567 * pass by reference, only by value between controllers. 12568 * So we can't pass a pointer to the S/G list, only as many 12569 * S/G entries as we can fit in here. If it's possible for 12570 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12571 * then we need to break this up into multiple transfers. 12572 */ 12573 if (io->scsiio.kern_sg_entries == 0) { 12574 msg.dt.kern_sg_entries = 1; 12575 /* 12576 * If this is in cached memory, flush the cache 12577 * before we send the DMA request to the other 12578 * controller. We want to do this in either the 12579 * read or the write case. The read case is 12580 * straightforward. In the write case, we want to 12581 * make sure nothing is in the local cache that 12582 * could overwrite the DMAed data. 12583 */ 12584 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12585 /* 12586 * XXX KDM use bus_dmamap_sync() here. 12587 */ 12588 } 12589 12590 /* 12591 * Convert to a physical address if this is a 12592 * virtual address. 12593 */ 12594 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12595 msg.dt.sg_list[0].addr = 12596 io->scsiio.kern_data_ptr; 12597 } else { 12598 /* 12599 * XXX KDM use busdma here! 12600 */ 12601#if 0 12602 msg.dt.sg_list[0].addr = (void *) 12603 vtophys(io->scsiio.kern_data_ptr); 12604#endif 12605 } 12606 12607 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12608 do_sg_copy = 0; 12609 } else { 12610 struct ctl_sg_entry *sgl; 12611 12612 do_sg_copy = 1; 12613 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12614 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12615 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12616 /* 12617 * XXX KDM use bus_dmamap_sync() here. 12618 */ 12619 } 12620 } 12621 12622 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12623 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12624 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12625 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12626 msg.dt.sg_sequence = 0; 12627 12628 /* 12629 * Loop until we've sent all of the S/G entries. On the 12630 * other end, we'll recompose these S/G entries into one 12631 * contiguous list before passing it to the 12632 */ 12633 for (sg_entries_sent = 0; sg_entries_sent < 12634 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12635 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12636 sizeof(msg.dt.sg_list[0])), 12637 msg.dt.kern_sg_entries - sg_entries_sent); 12638 12639 if (do_sg_copy != 0) { 12640 struct ctl_sg_entry *sgl; 12641 int j; 12642 12643 sgl = (struct ctl_sg_entry *) 12644 io->scsiio.kern_data_ptr; 12645 /* 12646 * If this is in cached memory, flush the cache 12647 * before we send the DMA request to the other 12648 * controller. We want to do this in either 12649 * the * read or the write case. The read 12650 * case is straightforward. In the write 12651 * case, we want to make sure nothing is 12652 * in the local cache that could overwrite 12653 * the DMAed data. 12654 */ 12655 12656 for (i = sg_entries_sent, j = 0; 12657 i < msg.dt.cur_sg_entries; i++, j++) { 12658 if ((io->io_hdr.flags & 12659 CTL_FLAG_NO_DATASYNC) == 0) { 12660 /* 12661 * XXX KDM use bus_dmamap_sync() 12662 */ 12663 } 12664 if ((io->io_hdr.flags & 12665 CTL_FLAG_BUS_ADDR) == 0) { 12666 /* 12667 * XXX KDM use busdma. 12668 */ 12669#if 0 12670 msg.dt.sg_list[j].addr =(void *) 12671 vtophys(sgl[i].addr); 12672#endif 12673 } else { 12674 msg.dt.sg_list[j].addr = 12675 sgl[i].addr; 12676 } 12677 msg.dt.sg_list[j].len = sgl[i].len; 12678 } 12679 } 12680 12681 sg_entries_sent += msg.dt.cur_sg_entries; 12682 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12683 msg.dt.sg_last = 1; 12684 else 12685 msg.dt.sg_last = 0; 12686 12687 /* 12688 * XXX KDM drop and reacquire the lock here? 12689 */ 12690 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12691 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12692 /* 12693 * XXX do something here. 12694 */ 12695 } 12696 12697 msg.dt.sent_sg_entries = sg_entries_sent; 12698 } 12699 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12700 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12701 ctl_failover_io(io, /*have_lock*/ 0); 12702 12703 } else { 12704 12705 /* 12706 * Lookup the fe_datamove() function for this particular 12707 * front end. 12708 */ 12709 fe_datamove = 12710 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12711 12712 fe_datamove(io); 12713 } 12714} 12715 12716static void 12717ctl_send_datamove_done(union ctl_io *io, int have_lock) 12718{ 12719 union ctl_ha_msg msg; 12720 int isc_status; 12721 12722 memset(&msg, 0, sizeof(msg)); 12723 12724 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12725 msg.hdr.original_sc = io; 12726 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12727 msg.hdr.nexus = io->io_hdr.nexus; 12728 msg.hdr.status = io->io_hdr.status; 12729 msg.scsi.tag_num = io->scsiio.tag_num; 12730 msg.scsi.tag_type = io->scsiio.tag_type; 12731 msg.scsi.scsi_status = io->scsiio.scsi_status; 12732 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12733 sizeof(io->scsiio.sense_data)); 12734 msg.scsi.sense_len = io->scsiio.sense_len; 12735 msg.scsi.sense_residual = io->scsiio.sense_residual; 12736 msg.scsi.fetd_status = io->io_hdr.port_status; 12737 msg.scsi.residual = io->scsiio.residual; 12738 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12739 12740 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12741 ctl_failover_io(io, /*have_lock*/ have_lock); 12742 return; 12743 } 12744 12745 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12746 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12747 /* XXX do something if this fails */ 12748 } 12749 12750} 12751 12752/* 12753 * The DMA to the remote side is done, now we need to tell the other side 12754 * we're done so it can continue with its data movement. 12755 */ 12756static void 12757ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12758{ 12759 union ctl_io *io; 12760 12761 io = rq->context; 12762 12763 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12764 printf("%s: ISC DMA write failed with error %d", __func__, 12765 rq->ret); 12766 ctl_set_internal_failure(&io->scsiio, 12767 /*sks_valid*/ 1, 12768 /*retry_count*/ rq->ret); 12769 } 12770 12771 ctl_dt_req_free(rq); 12772 12773 /* 12774 * In this case, we had to malloc the memory locally. Free it. 12775 */ 12776 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12777 int i; 12778 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12779 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12780 } 12781 /* 12782 * The data is in local and remote memory, so now we need to send 12783 * status (good or back) back to the other side. 12784 */ 12785 ctl_send_datamove_done(io, /*have_lock*/ 0); 12786} 12787 12788/* 12789 * We've moved the data from the host/controller into local memory. Now we 12790 * need to push it over to the remote controller's memory. 12791 */ 12792static int 12793ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12794{ 12795 int retval; 12796 12797 retval = 0; 12798 12799 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12800 ctl_datamove_remote_write_cb); 12801 12802 return (retval); 12803} 12804 12805static void 12806ctl_datamove_remote_write(union ctl_io *io) 12807{ 12808 int retval; 12809 void (*fe_datamove)(union ctl_io *io); 12810 12811 /* 12812 * - Get the data from the host/HBA into local memory. 12813 * - DMA memory from the local controller to the remote controller. 12814 * - Send status back to the remote controller. 12815 */ 12816 12817 retval = ctl_datamove_remote_sgl_setup(io); 12818 if (retval != 0) 12819 return; 12820 12821 /* Switch the pointer over so the FETD knows what to do */ 12822 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12823 12824 /* 12825 * Use a custom move done callback, since we need to send completion 12826 * back to the other controller, not to the backend on this side. 12827 */ 12828 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12829 12830 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12831 12832 fe_datamove(io); 12833 12834 return; 12835 12836} 12837 12838static int 12839ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12840{ 12841#if 0 12842 char str[256]; 12843 char path_str[64]; 12844 struct sbuf sb; 12845#endif 12846 12847 /* 12848 * In this case, we had to malloc the memory locally. Free it. 12849 */ 12850 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12851 int i; 12852 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12853 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12854 } 12855 12856#if 0 12857 scsi_path_string(io, path_str, sizeof(path_str)); 12858 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12859 sbuf_cat(&sb, path_str); 12860 scsi_command_string(&io->scsiio, NULL, &sb); 12861 sbuf_printf(&sb, "\n"); 12862 sbuf_cat(&sb, path_str); 12863 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12864 io->scsiio.tag_num, io->scsiio.tag_type); 12865 sbuf_cat(&sb, path_str); 12866 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12867 io->io_hdr.flags, io->io_hdr.status); 12868 sbuf_finish(&sb); 12869 printk("%s", sbuf_data(&sb)); 12870#endif 12871 12872 12873 /* 12874 * The read is done, now we need to send status (good or bad) back 12875 * to the other side. 12876 */ 12877 ctl_send_datamove_done(io, /*have_lock*/ 0); 12878 12879 return (0); 12880} 12881 12882static void 12883ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12884{ 12885 union ctl_io *io; 12886 void (*fe_datamove)(union ctl_io *io); 12887 12888 io = rq->context; 12889 12890 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12891 printf("%s: ISC DMA read failed with error %d", __func__, 12892 rq->ret); 12893 ctl_set_internal_failure(&io->scsiio, 12894 /*sks_valid*/ 1, 12895 /*retry_count*/ rq->ret); 12896 } 12897 12898 ctl_dt_req_free(rq); 12899 12900 /* Switch the pointer over so the FETD knows what to do */ 12901 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12902 12903 /* 12904 * Use a custom move done callback, since we need to send completion 12905 * back to the other controller, not to the backend on this side. 12906 */ 12907 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12908 12909 /* XXX KDM add checks like the ones in ctl_datamove? */ 12910 12911 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12912 12913 fe_datamove(io); 12914} 12915 12916static int 12917ctl_datamove_remote_sgl_setup(union ctl_io *io) 12918{ 12919 struct ctl_sg_entry *local_sglist, *remote_sglist; 12920 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12921 struct ctl_softc *softc; 12922 int retval; 12923 int i; 12924 12925 retval = 0; 12926 softc = control_softc; 12927 12928 local_sglist = io->io_hdr.local_sglist; 12929 local_dma_sglist = io->io_hdr.local_dma_sglist; 12930 remote_sglist = io->io_hdr.remote_sglist; 12931 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12932 12933 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12934 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12935 local_sglist[i].len = remote_sglist[i].len; 12936 12937 /* 12938 * XXX Detect the situation where the RS-level I/O 12939 * redirector on the other side has already read the 12940 * data off of the AOR RS on this side, and 12941 * transferred it to remote (mirror) memory on the 12942 * other side. Since we already have the data in 12943 * memory here, we just need to use it. 12944 * 12945 * XXX KDM this can probably be removed once we 12946 * get the cache device code in and take the 12947 * current AOR implementation out. 12948 */ 12949#ifdef NEEDTOPORT 12950 if ((remote_sglist[i].addr >= 12951 (void *)vtophys(softc->mirr->addr)) 12952 && (remote_sglist[i].addr < 12953 ((void *)vtophys(softc->mirr->addr) + 12954 CacheMirrorOffset))) { 12955 local_sglist[i].addr = remote_sglist[i].addr - 12956 CacheMirrorOffset; 12957 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12958 CTL_FLAG_DATA_IN) 12959 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12960 } else { 12961 local_sglist[i].addr = remote_sglist[i].addr + 12962 CacheMirrorOffset; 12963 } 12964#endif 12965#if 0 12966 printf("%s: local %p, remote %p, len %d\n", 12967 __func__, local_sglist[i].addr, 12968 remote_sglist[i].addr, local_sglist[i].len); 12969#endif 12970 } 12971 } else { 12972 uint32_t len_to_go; 12973 12974 /* 12975 * In this case, we don't have automatically allocated 12976 * memory for this I/O on this controller. This typically 12977 * happens with internal CTL I/O -- e.g. inquiry, mode 12978 * sense, etc. Anything coming from RAIDCore will have 12979 * a mirror area available. 12980 */ 12981 len_to_go = io->scsiio.kern_data_len; 12982 12983 /* 12984 * Clear the no datasync flag, we have to use malloced 12985 * buffers. 12986 */ 12987 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12988 12989 /* 12990 * The difficult thing here is that the size of the various 12991 * S/G segments may be different than the size from the 12992 * remote controller. That'll make it harder when DMAing 12993 * the data back to the other side. 12994 */ 12995 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12996 sizeof(io->io_hdr.remote_sglist[0])) && 12997 (len_to_go > 0); i++) { 12998 local_sglist[i].len = ctl_min(len_to_go, 131072); 12999 CTL_SIZE_8B(local_dma_sglist[i].len, 13000 local_sglist[i].len); 13001 local_sglist[i].addr = 13002 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13003 13004 local_dma_sglist[i].addr = local_sglist[i].addr; 13005 13006 if (local_sglist[i].addr == NULL) { 13007 int j; 13008 13009 printf("malloc failed for %zd bytes!", 13010 local_dma_sglist[i].len); 13011 for (j = 0; j < i; j++) { 13012 free(local_sglist[j].addr, M_CTL); 13013 } 13014 ctl_set_internal_failure(&io->scsiio, 13015 /*sks_valid*/ 1, 13016 /*retry_count*/ 4857); 13017 retval = 1; 13018 goto bailout_error; 13019 13020 } 13021 /* XXX KDM do we need a sync here? */ 13022 13023 len_to_go -= local_sglist[i].len; 13024 } 13025 /* 13026 * Reset the number of S/G entries accordingly. The 13027 * original number of S/G entries is available in 13028 * rem_sg_entries. 13029 */ 13030 io->scsiio.kern_sg_entries = i; 13031 13032#if 0 13033 printf("%s: kern_sg_entries = %d\n", __func__, 13034 io->scsiio.kern_sg_entries); 13035 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13036 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13037 local_sglist[i].addr, local_sglist[i].len, 13038 local_dma_sglist[i].len); 13039#endif 13040 } 13041 13042 13043 return (retval); 13044 13045bailout_error: 13046 13047 ctl_send_datamove_done(io, /*have_lock*/ 0); 13048 13049 return (retval); 13050} 13051 13052static int 13053ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13054 ctl_ha_dt_cb callback) 13055{ 13056 struct ctl_ha_dt_req *rq; 13057 struct ctl_sg_entry *remote_sglist, *local_sglist; 13058 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13059 uint32_t local_used, remote_used, total_used; 13060 int retval; 13061 int i, j; 13062 13063 retval = 0; 13064 13065 rq = ctl_dt_req_alloc(); 13066 13067 /* 13068 * If we failed to allocate the request, and if the DMA didn't fail 13069 * anyway, set busy status. This is just a resource allocation 13070 * failure. 13071 */ 13072 if ((rq == NULL) 13073 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13074 ctl_set_busy(&io->scsiio); 13075 13076 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13077 13078 if (rq != NULL) 13079 ctl_dt_req_free(rq); 13080 13081 /* 13082 * The data move failed. We need to return status back 13083 * to the other controller. No point in trying to DMA 13084 * data to the remote controller. 13085 */ 13086 13087 ctl_send_datamove_done(io, /*have_lock*/ 0); 13088 13089 retval = 1; 13090 13091 goto bailout; 13092 } 13093 13094 local_sglist = io->io_hdr.local_sglist; 13095 local_dma_sglist = io->io_hdr.local_dma_sglist; 13096 remote_sglist = io->io_hdr.remote_sglist; 13097 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13098 local_used = 0; 13099 remote_used = 0; 13100 total_used = 0; 13101 13102 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13103 rq->ret = CTL_HA_STATUS_SUCCESS; 13104 rq->context = io; 13105 callback(rq); 13106 goto bailout; 13107 } 13108 13109 /* 13110 * Pull/push the data over the wire from/to the other controller. 13111 * This takes into account the possibility that the local and 13112 * remote sglists may not be identical in terms of the size of 13113 * the elements and the number of elements. 13114 * 13115 * One fundamental assumption here is that the length allocated for 13116 * both the local and remote sglists is identical. Otherwise, we've 13117 * essentially got a coding error of some sort. 13118 */ 13119 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13120 int isc_ret; 13121 uint32_t cur_len, dma_length; 13122 uint8_t *tmp_ptr; 13123 13124 rq->id = CTL_HA_DATA_CTL; 13125 rq->command = command; 13126 rq->context = io; 13127 13128 /* 13129 * Both pointers should be aligned. But it is possible 13130 * that the allocation length is not. They should both 13131 * also have enough slack left over at the end, though, 13132 * to round up to the next 8 byte boundary. 13133 */ 13134 cur_len = ctl_min(local_sglist[i].len - local_used, 13135 remote_sglist[j].len - remote_used); 13136 13137 /* 13138 * In this case, we have a size issue and need to decrease 13139 * the size, except in the case where we actually have less 13140 * than 8 bytes left. In that case, we need to increase 13141 * the DMA length to get the last bit. 13142 */ 13143 if ((cur_len & 0x7) != 0) { 13144 if (cur_len > 0x7) { 13145 cur_len = cur_len - (cur_len & 0x7); 13146 dma_length = cur_len; 13147 } else { 13148 CTL_SIZE_8B(dma_length, cur_len); 13149 } 13150 13151 } else 13152 dma_length = cur_len; 13153 13154 /* 13155 * If we had to allocate memory for this I/O, instead of using 13156 * the non-cached mirror memory, we'll need to flush the cache 13157 * before trying to DMA to the other controller. 13158 * 13159 * We could end up doing this multiple times for the same 13160 * segment if we have a larger local segment than remote 13161 * segment. That shouldn't be an issue. 13162 */ 13163 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13164 /* 13165 * XXX KDM use bus_dmamap_sync() here. 13166 */ 13167 } 13168 13169 rq->size = dma_length; 13170 13171 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13172 tmp_ptr += local_used; 13173 13174 /* Use physical addresses when talking to ISC hardware */ 13175 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13176 /* XXX KDM use busdma */ 13177#if 0 13178 rq->local = vtophys(tmp_ptr); 13179#endif 13180 } else 13181 rq->local = tmp_ptr; 13182 13183 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13184 tmp_ptr += remote_used; 13185 rq->remote = tmp_ptr; 13186 13187 rq->callback = NULL; 13188 13189 local_used += cur_len; 13190 if (local_used >= local_sglist[i].len) { 13191 i++; 13192 local_used = 0; 13193 } 13194 13195 remote_used += cur_len; 13196 if (remote_used >= remote_sglist[j].len) { 13197 j++; 13198 remote_used = 0; 13199 } 13200 total_used += cur_len; 13201 13202 if (total_used >= io->scsiio.kern_data_len) 13203 rq->callback = callback; 13204 13205 if ((rq->size & 0x7) != 0) { 13206 printf("%s: warning: size %d is not on 8b boundary\n", 13207 __func__, rq->size); 13208 } 13209 if (((uintptr_t)rq->local & 0x7) != 0) { 13210 printf("%s: warning: local %p not on 8b boundary\n", 13211 __func__, rq->local); 13212 } 13213 if (((uintptr_t)rq->remote & 0x7) != 0) { 13214 printf("%s: warning: remote %p not on 8b boundary\n", 13215 __func__, rq->local); 13216 } 13217#if 0 13218 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13219 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13220 rq->local, rq->remote, rq->size); 13221#endif 13222 13223 isc_ret = ctl_dt_single(rq); 13224 if (isc_ret == CTL_HA_STATUS_WAIT) 13225 continue; 13226 13227 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13228 rq->ret = CTL_HA_STATUS_SUCCESS; 13229 } else { 13230 rq->ret = isc_ret; 13231 } 13232 callback(rq); 13233 goto bailout; 13234 } 13235 13236bailout: 13237 return (retval); 13238 13239} 13240 13241static void 13242ctl_datamove_remote_read(union ctl_io *io) 13243{ 13244 int retval; 13245 int i; 13246 13247 /* 13248 * This will send an error to the other controller in the case of a 13249 * failure. 13250 */ 13251 retval = ctl_datamove_remote_sgl_setup(io); 13252 if (retval != 0) 13253 return; 13254 13255 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13256 ctl_datamove_remote_read_cb); 13257 if ((retval != 0) 13258 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13259 /* 13260 * Make sure we free memory if there was an error.. The 13261 * ctl_datamove_remote_xfer() function will send the 13262 * datamove done message, or call the callback with an 13263 * error if there is a problem. 13264 */ 13265 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13266 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13267 } 13268 13269 return; 13270} 13271 13272/* 13273 * Process a datamove request from the other controller. This is used for 13274 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13275 * first. Once that is complete, the data gets DMAed into the remote 13276 * controller's memory. For reads, we DMA from the remote controller's 13277 * memory into our memory first, and then move it out to the FETD. 13278 */ 13279static void 13280ctl_datamove_remote(union ctl_io *io) 13281{ 13282 struct ctl_softc *softc; 13283 13284 softc = control_softc; 13285 13286 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13287 13288 /* 13289 * Note that we look for an aborted I/O here, but don't do some of 13290 * the other checks that ctl_datamove() normally does. 13291 * We don't need to run the datamove delay code, since that should 13292 * have been done if need be on the other controller. 13293 */ 13294 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13295 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13296 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13297 io->io_hdr.nexus.targ_port, 13298 io->io_hdr.nexus.targ_target.id, 13299 io->io_hdr.nexus.targ_lun); 13300 io->io_hdr.port_status = 31338; 13301 ctl_send_datamove_done(io, /*have_lock*/ 0); 13302 return; 13303 } 13304 13305 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13306 ctl_datamove_remote_write(io); 13307 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13308 ctl_datamove_remote_read(io); 13309 } else { 13310 union ctl_ha_msg msg; 13311 struct scsi_sense_data *sense; 13312 uint8_t sks[3]; 13313 int retry_count; 13314 13315 memset(&msg, 0, sizeof(msg)); 13316 13317 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13318 msg.hdr.status = CTL_SCSI_ERROR; 13319 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13320 13321 retry_count = 4243; 13322 13323 sense = &msg.scsi.sense_data; 13324 sks[0] = SSD_SCS_VALID; 13325 sks[1] = (retry_count >> 8) & 0xff; 13326 sks[2] = retry_count & 0xff; 13327 13328 /* "Internal target failure" */ 13329 scsi_set_sense_data(sense, 13330 /*sense_format*/ SSD_TYPE_NONE, 13331 /*current_error*/ 1, 13332 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13333 /*asc*/ 0x44, 13334 /*ascq*/ 0x00, 13335 /*type*/ SSD_ELEM_SKS, 13336 /*size*/ sizeof(sks), 13337 /*data*/ sks, 13338 SSD_ELEM_NONE); 13339 13340 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13341 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13342 ctl_failover_io(io, /*have_lock*/ 1); 13343 return; 13344 } 13345 13346 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13347 CTL_HA_STATUS_SUCCESS) { 13348 /* XXX KDM what to do if this fails? */ 13349 } 13350 return; 13351 } 13352 13353} 13354 13355static int 13356ctl_process_done(union ctl_io *io) 13357{ 13358 struct ctl_lun *lun; 13359 struct ctl_softc *ctl_softc; 13360 void (*fe_done)(union ctl_io *io); 13361 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13362 13363 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13364 13365 fe_done = 13366 control_softc->ctl_ports[targ_port]->fe_done; 13367 13368#ifdef CTL_TIME_IO 13369 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13370 char str[256]; 13371 char path_str[64]; 13372 struct sbuf sb; 13373 13374 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13375 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13376 13377 sbuf_cat(&sb, path_str); 13378 switch (io->io_hdr.io_type) { 13379 case CTL_IO_SCSI: 13380 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13381 sbuf_printf(&sb, "\n"); 13382 sbuf_cat(&sb, path_str); 13383 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13384 io->scsiio.tag_num, io->scsiio.tag_type); 13385 break; 13386 case CTL_IO_TASK: 13387 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13388 "Tag Type: %d\n", io->taskio.task_action, 13389 io->taskio.tag_num, io->taskio.tag_type); 13390 break; 13391 default: 13392 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13393 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13394 break; 13395 } 13396 sbuf_cat(&sb, path_str); 13397 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13398 (intmax_t)time_uptime - io->io_hdr.start_time); 13399 sbuf_finish(&sb); 13400 printf("%s", sbuf_data(&sb)); 13401 } 13402#endif /* CTL_TIME_IO */ 13403 13404 switch (io->io_hdr.io_type) { 13405 case CTL_IO_SCSI: 13406 break; 13407 case CTL_IO_TASK: 13408 if (bootverbose || verbose > 0) 13409 ctl_io_error_print(io, NULL); 13410 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13411 ctl_free_io(io); 13412 else 13413 fe_done(io); 13414 return (CTL_RETVAL_COMPLETE); 13415 break; 13416 default: 13417 printf("ctl_process_done: invalid io type %d\n", 13418 io->io_hdr.io_type); 13419 panic("ctl_process_done: invalid io type %d\n", 13420 io->io_hdr.io_type); 13421 break; /* NOTREACHED */ 13422 } 13423 13424 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13425 if (lun == NULL) { 13426 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13427 io->io_hdr.nexus.targ_mapped_lun)); 13428 fe_done(io); 13429 goto bailout; 13430 } 13431 ctl_softc = lun->ctl_softc; 13432 13433 mtx_lock(&lun->lun_lock); 13434 13435 /* 13436 * Check to see if we have any errors to inject here. We only 13437 * inject errors for commands that don't already have errors set. 13438 */ 13439 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13440 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13441 ctl_inject_error(lun, io); 13442 13443 /* 13444 * XXX KDM how do we treat commands that aren't completed 13445 * successfully? 13446 * 13447 * XXX KDM should we also track I/O latency? 13448 */ 13449 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13450 io->io_hdr.io_type == CTL_IO_SCSI) { 13451#ifdef CTL_TIME_IO 13452 struct bintime cur_bt; 13453#endif 13454 int type; 13455 13456 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13457 CTL_FLAG_DATA_IN) 13458 type = CTL_STATS_READ; 13459 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13460 CTL_FLAG_DATA_OUT) 13461 type = CTL_STATS_WRITE; 13462 else 13463 type = CTL_STATS_NO_IO; 13464 13465 lun->stats.ports[targ_port].bytes[type] += 13466 io->scsiio.kern_total_len; 13467 lun->stats.ports[targ_port].operations[type]++; 13468#ifdef CTL_TIME_IO 13469 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13470 &io->io_hdr.dma_bt); 13471 lun->stats.ports[targ_port].num_dmas[type] += 13472 io->io_hdr.num_dmas; 13473 getbintime(&cur_bt); 13474 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13475 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13476#endif 13477 } 13478 13479 /* 13480 * Remove this from the OOA queue. 13481 */ 13482 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13483 13484 /* 13485 * Run through the blocked queue on this LUN and see if anything 13486 * has become unblocked, now that this transaction is done. 13487 */ 13488 ctl_check_blocked(lun); 13489 13490 /* 13491 * If the LUN has been invalidated, free it if there is nothing 13492 * left on its OOA queue. 13493 */ 13494 if ((lun->flags & CTL_LUN_INVALID) 13495 && TAILQ_EMPTY(&lun->ooa_queue)) { 13496 mtx_unlock(&lun->lun_lock); 13497 mtx_lock(&ctl_softc->ctl_lock); 13498 ctl_free_lun(lun); 13499 mtx_unlock(&ctl_softc->ctl_lock); 13500 } else 13501 mtx_unlock(&lun->lun_lock); 13502 13503 /* 13504 * If this command has been aborted, make sure we set the status 13505 * properly. The FETD is responsible for freeing the I/O and doing 13506 * whatever it needs to do to clean up its state. 13507 */ 13508 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13509 ctl_set_task_aborted(&io->scsiio); 13510 13511 /* 13512 * We print out status for every task management command. For SCSI 13513 * commands, we filter out any unit attention errors; they happen 13514 * on every boot, and would clutter up the log. Note: task 13515 * management commands aren't printed here, they are printed above, 13516 * since they should never even make it down here. 13517 */ 13518 switch (io->io_hdr.io_type) { 13519 case CTL_IO_SCSI: { 13520 int error_code, sense_key, asc, ascq; 13521 13522 sense_key = 0; 13523 13524 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13525 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13526 /* 13527 * Since this is just for printing, no need to 13528 * show errors here. 13529 */ 13530 scsi_extract_sense_len(&io->scsiio.sense_data, 13531 io->scsiio.sense_len, 13532 &error_code, 13533 &sense_key, 13534 &asc, 13535 &ascq, 13536 /*show_errors*/ 0); 13537 } 13538 13539 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13540 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13541 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13542 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13543 13544 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13545 ctl_softc->skipped_prints++; 13546 } else { 13547 uint32_t skipped_prints; 13548 13549 skipped_prints = ctl_softc->skipped_prints; 13550 13551 ctl_softc->skipped_prints = 0; 13552 ctl_softc->last_print_jiffies = time_uptime; 13553 13554 if (skipped_prints > 0) { 13555#ifdef NEEDTOPORT 13556 csevent_log(CSC_CTL | CSC_SHELF_SW | 13557 CTL_ERROR_REPORT, 13558 csevent_LogType_Trace, 13559 csevent_Severity_Information, 13560 csevent_AlertLevel_Green, 13561 csevent_FRU_Firmware, 13562 csevent_FRU_Unknown, 13563 "High CTL error volume, %d prints " 13564 "skipped", skipped_prints); 13565#endif 13566 } 13567 if (bootverbose || verbose > 0) 13568 ctl_io_error_print(io, NULL); 13569 } 13570 } 13571 break; 13572 } 13573 case CTL_IO_TASK: 13574 if (bootverbose || verbose > 0) 13575 ctl_io_error_print(io, NULL); 13576 break; 13577 default: 13578 break; 13579 } 13580 13581 /* 13582 * Tell the FETD or the other shelf controller we're done with this 13583 * command. Note that only SCSI commands get to this point. Task 13584 * management commands are completed above. 13585 * 13586 * We only send status to the other controller if we're in XFER 13587 * mode. In SER_ONLY mode, the I/O is done on the controller that 13588 * received the I/O (from CTL's perspective), and so the status is 13589 * generated there. 13590 * 13591 * XXX KDM if we hold the lock here, we could cause a deadlock 13592 * if the frontend comes back in in this context to queue 13593 * something. 13594 */ 13595 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13596 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13597 union ctl_ha_msg msg; 13598 13599 memset(&msg, 0, sizeof(msg)); 13600 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13601 msg.hdr.original_sc = io->io_hdr.original_sc; 13602 msg.hdr.nexus = io->io_hdr.nexus; 13603 msg.hdr.status = io->io_hdr.status; 13604 msg.scsi.scsi_status = io->scsiio.scsi_status; 13605 msg.scsi.tag_num = io->scsiio.tag_num; 13606 msg.scsi.tag_type = io->scsiio.tag_type; 13607 msg.scsi.sense_len = io->scsiio.sense_len; 13608 msg.scsi.sense_residual = io->scsiio.sense_residual; 13609 msg.scsi.residual = io->scsiio.residual; 13610 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13611 sizeof(io->scsiio.sense_data)); 13612 /* 13613 * We copy this whether or not this is an I/O-related 13614 * command. Otherwise, we'd have to go and check to see 13615 * whether it's a read/write command, and it really isn't 13616 * worth it. 13617 */ 13618 memcpy(&msg.scsi.lbalen, 13619 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13620 sizeof(msg.scsi.lbalen)); 13621 13622 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13623 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13624 /* XXX do something here */ 13625 } 13626 13627 ctl_free_io(io); 13628 } else 13629 fe_done(io); 13630 13631bailout: 13632 13633 return (CTL_RETVAL_COMPLETE); 13634} 13635 13636/* 13637 * Front end should call this if it doesn't do autosense. When the request 13638 * sense comes back in from the initiator, we'll dequeue this and send it. 13639 */ 13640int 13641ctl_queue_sense(union ctl_io *io) 13642{ 13643 struct ctl_lun *lun; 13644 struct ctl_softc *ctl_softc; 13645 uint32_t initidx, targ_lun; 13646 13647 ctl_softc = control_softc; 13648 13649 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13650 13651 /* 13652 * LUN lookup will likely move to the ctl_work_thread() once we 13653 * have our new queueing infrastructure (that doesn't put things on 13654 * a per-LUN queue initially). That is so that we can handle 13655 * things like an INQUIRY to a LUN that we don't have enabled. We 13656 * can't deal with that right now. 13657 */ 13658 mtx_lock(&ctl_softc->ctl_lock); 13659 13660 /* 13661 * If we don't have a LUN for this, just toss the sense 13662 * information. 13663 */ 13664 targ_lun = io->io_hdr.nexus.targ_lun; 13665 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13666 if ((targ_lun < CTL_MAX_LUNS) 13667 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13668 lun = ctl_softc->ctl_luns[targ_lun]; 13669 else 13670 goto bailout; 13671 13672 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13673 13674 mtx_lock(&lun->lun_lock); 13675 /* 13676 * Already have CA set for this LUN...toss the sense information. 13677 */ 13678 if (ctl_is_set(lun->have_ca, initidx)) { 13679 mtx_unlock(&lun->lun_lock); 13680 goto bailout; 13681 } 13682 13683 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data, 13684 ctl_min(sizeof(lun->pending_sense[initidx].sense), 13685 sizeof(io->scsiio.sense_data))); 13686 ctl_set_mask(lun->have_ca, initidx); 13687 mtx_unlock(&lun->lun_lock); 13688 13689bailout: 13690 mtx_unlock(&ctl_softc->ctl_lock); 13691 13692 ctl_free_io(io); 13693 13694 return (CTL_RETVAL_COMPLETE); 13695} 13696 13697/* 13698 * Primary command inlet from frontend ports. All SCSI and task I/O 13699 * requests must go through this function. 13700 */ 13701int 13702ctl_queue(union ctl_io *io) 13703{ 13704 struct ctl_softc *ctl_softc; 13705 13706 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13707 13708 ctl_softc = control_softc; 13709 13710#ifdef CTL_TIME_IO 13711 io->io_hdr.start_time = time_uptime; 13712 getbintime(&io->io_hdr.start_bt); 13713#endif /* CTL_TIME_IO */ 13714 13715 /* Map FE-specific LUN ID into global one. */ 13716 io->io_hdr.nexus.targ_mapped_lun = 13717 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13718 13719 switch (io->io_hdr.io_type) { 13720 case CTL_IO_SCSI: 13721 case CTL_IO_TASK: 13722 ctl_enqueue_incoming(io); 13723 break; 13724 default: 13725 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13726 return (EINVAL); 13727 } 13728 13729 return (CTL_RETVAL_COMPLETE); 13730} 13731 13732#ifdef CTL_IO_DELAY 13733static void 13734ctl_done_timer_wakeup(void *arg) 13735{ 13736 union ctl_io *io; 13737 13738 io = (union ctl_io *)arg; 13739 ctl_done(io); 13740} 13741#endif /* CTL_IO_DELAY */ 13742 13743void 13744ctl_done(union ctl_io *io) 13745{ 13746 struct ctl_softc *ctl_softc; 13747 13748 ctl_softc = control_softc; 13749 13750 /* 13751 * Enable this to catch duplicate completion issues. 13752 */ 13753#if 0 13754 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13755 printf("%s: type %d msg %d cdb %x iptl: " 13756 "%d:%d:%d:%d tag 0x%04x " 13757 "flag %#x status %x\n", 13758 __func__, 13759 io->io_hdr.io_type, 13760 io->io_hdr.msg_type, 13761 io->scsiio.cdb[0], 13762 io->io_hdr.nexus.initid.id, 13763 io->io_hdr.nexus.targ_port, 13764 io->io_hdr.nexus.targ_target.id, 13765 io->io_hdr.nexus.targ_lun, 13766 (io->io_hdr.io_type == 13767 CTL_IO_TASK) ? 13768 io->taskio.tag_num : 13769 io->scsiio.tag_num, 13770 io->io_hdr.flags, 13771 io->io_hdr.status); 13772 } else 13773 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13774#endif 13775 13776 /* 13777 * This is an internal copy of an I/O, and should not go through 13778 * the normal done processing logic. 13779 */ 13780 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13781 return; 13782 13783 /* 13784 * We need to send a msg to the serializing shelf to finish the IO 13785 * as well. We don't send a finish message to the other shelf if 13786 * this is a task management command. Task management commands 13787 * aren't serialized in the OOA queue, but rather just executed on 13788 * both shelf controllers for commands that originated on that 13789 * controller. 13790 */ 13791 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13792 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13793 union ctl_ha_msg msg_io; 13794 13795 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13796 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13797 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13798 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13799 } 13800 /* continue on to finish IO */ 13801 } 13802#ifdef CTL_IO_DELAY 13803 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13804 struct ctl_lun *lun; 13805 13806 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13807 13808 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13809 } else { 13810 struct ctl_lun *lun; 13811 13812 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13813 13814 if ((lun != NULL) 13815 && (lun->delay_info.done_delay > 0)) { 13816 struct callout *callout; 13817 13818 callout = (struct callout *)&io->io_hdr.timer_bytes; 13819 callout_init(callout, /*mpsafe*/ 1); 13820 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13821 callout_reset(callout, 13822 lun->delay_info.done_delay * hz, 13823 ctl_done_timer_wakeup, io); 13824 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13825 lun->delay_info.done_delay = 0; 13826 return; 13827 } 13828 } 13829#endif /* CTL_IO_DELAY */ 13830 13831 ctl_enqueue_done(io); 13832} 13833 13834int 13835ctl_isc(struct ctl_scsiio *ctsio) 13836{ 13837 struct ctl_lun *lun; 13838 int retval; 13839 13840 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13841 13842 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13843 13844 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13845 13846 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13847 13848 return (retval); 13849} 13850 13851 13852static void 13853ctl_work_thread(void *arg) 13854{ 13855 struct ctl_thread *thr = (struct ctl_thread *)arg; 13856 struct ctl_softc *softc = thr->ctl_softc; 13857 union ctl_io *io; 13858 int retval; 13859 13860 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13861 13862 for (;;) { 13863 retval = 0; 13864 13865 /* 13866 * We handle the queues in this order: 13867 * - ISC 13868 * - done queue (to free up resources, unblock other commands) 13869 * - RtR queue 13870 * - incoming queue 13871 * 13872 * If those queues are empty, we break out of the loop and 13873 * go to sleep. 13874 */ 13875 mtx_lock(&thr->queue_lock); 13876 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13877 if (io != NULL) { 13878 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13879 mtx_unlock(&thr->queue_lock); 13880 ctl_handle_isc(io); 13881 continue; 13882 } 13883 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13884 if (io != NULL) { 13885 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13886 /* clear any blocked commands, call fe_done */ 13887 mtx_unlock(&thr->queue_lock); 13888 retval = ctl_process_done(io); 13889 continue; 13890 } 13891 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13892 if (io != NULL) { 13893 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13894 mtx_unlock(&thr->queue_lock); 13895 if (io->io_hdr.io_type == CTL_IO_TASK) 13896 ctl_run_task(io); 13897 else 13898 ctl_scsiio_precheck(softc, &io->scsiio); 13899 continue; 13900 } 13901 if (!ctl_pause_rtr) { 13902 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13903 if (io != NULL) { 13904 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13905 mtx_unlock(&thr->queue_lock); 13906 retval = ctl_scsiio(&io->scsiio); 13907 if (retval != CTL_RETVAL_COMPLETE) 13908 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13909 continue; 13910 } 13911 } 13912 13913 /* Sleep until we have something to do. */ 13914 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13915 } 13916} 13917 13918static void 13919ctl_lun_thread(void *arg) 13920{ 13921 struct ctl_softc *softc = (struct ctl_softc *)arg; 13922 struct ctl_be_lun *be_lun; 13923 int retval; 13924 13925 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13926 13927 for (;;) { 13928 retval = 0; 13929 mtx_lock(&softc->ctl_lock); 13930 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13931 if (be_lun != NULL) { 13932 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13933 mtx_unlock(&softc->ctl_lock); 13934 ctl_create_lun(be_lun); 13935 continue; 13936 } 13937 13938 /* Sleep until we have something to do. */ 13939 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13940 PDROP | PRIBIO, "-", 0); 13941 } 13942} 13943 13944static void 13945ctl_enqueue_incoming(union ctl_io *io) 13946{ 13947 struct ctl_softc *softc = control_softc; 13948 struct ctl_thread *thr; 13949 u_int idx; 13950 13951 idx = (io->io_hdr.nexus.targ_port * 127 + 13952 io->io_hdr.nexus.initid.id) % worker_threads; 13953 thr = &softc->threads[idx]; 13954 mtx_lock(&thr->queue_lock); 13955 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13956 mtx_unlock(&thr->queue_lock); 13957 wakeup(thr); 13958} 13959 13960static void 13961ctl_enqueue_rtr(union ctl_io *io) 13962{ 13963 struct ctl_softc *softc = control_softc; 13964 struct ctl_thread *thr; 13965 13966 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13967 mtx_lock(&thr->queue_lock); 13968 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13969 mtx_unlock(&thr->queue_lock); 13970 wakeup(thr); 13971} 13972 13973static void 13974ctl_enqueue_done(union ctl_io *io) 13975{ 13976 struct ctl_softc *softc = control_softc; 13977 struct ctl_thread *thr; 13978 13979 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13980 mtx_lock(&thr->queue_lock); 13981 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13982 mtx_unlock(&thr->queue_lock); 13983 wakeup(thr); 13984} 13985 13986static void 13987ctl_enqueue_isc(union ctl_io *io) 13988{ 13989 struct ctl_softc *softc = control_softc; 13990 struct ctl_thread *thr; 13991 13992 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13993 mtx_lock(&thr->queue_lock); 13994 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13995 mtx_unlock(&thr->queue_lock); 13996 wakeup(thr); 13997} 13998 13999/* Initialization and failover */ 14000 14001void 14002ctl_init_isc_msg(void) 14003{ 14004 printf("CTL: Still calling this thing\n"); 14005} 14006 14007/* 14008 * Init component 14009 * Initializes component into configuration defined by bootMode 14010 * (see hasc-sv.c) 14011 * returns hasc_Status: 14012 * OK 14013 * ERROR - fatal error 14014 */ 14015static ctl_ha_comp_status 14016ctl_isc_init(struct ctl_ha_component *c) 14017{ 14018 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14019 14020 c->status = ret; 14021 return ret; 14022} 14023 14024/* Start component 14025 * Starts component in state requested. If component starts successfully, 14026 * it must set its own state to the requestrd state 14027 * When requested state is HASC_STATE_HA, the component may refine it 14028 * by adding _SLAVE or _MASTER flags. 14029 * Currently allowed state transitions are: 14030 * UNKNOWN->HA - initial startup 14031 * UNKNOWN->SINGLE - initial startup when no parter detected 14032 * HA->SINGLE - failover 14033 * returns ctl_ha_comp_status: 14034 * OK - component successfully started in requested state 14035 * FAILED - could not start the requested state, failover may 14036 * be possible 14037 * ERROR - fatal error detected, no future startup possible 14038 */ 14039static ctl_ha_comp_status 14040ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14041{ 14042 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14043 14044 printf("%s: go\n", __func__); 14045 14046 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14047 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14048 ctl_is_single = 0; 14049 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14050 != CTL_HA_STATUS_SUCCESS) { 14051 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14052 ret = CTL_HA_COMP_STATUS_ERROR; 14053 } 14054 } else if (CTL_HA_STATE_IS_HA(c->state) 14055 && CTL_HA_STATE_IS_SINGLE(state)){ 14056 // HA->SINGLE transition 14057 ctl_failover(); 14058 ctl_is_single = 1; 14059 } else { 14060 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14061 c->state, state); 14062 ret = CTL_HA_COMP_STATUS_ERROR; 14063 } 14064 if (CTL_HA_STATE_IS_SINGLE(state)) 14065 ctl_is_single = 1; 14066 14067 c->state = state; 14068 c->status = ret; 14069 return ret; 14070} 14071 14072/* 14073 * Quiesce component 14074 * The component must clear any error conditions (set status to OK) and 14075 * prepare itself to another Start call 14076 * returns ctl_ha_comp_status: 14077 * OK 14078 * ERROR 14079 */ 14080static ctl_ha_comp_status 14081ctl_isc_quiesce(struct ctl_ha_component *c) 14082{ 14083 int ret = CTL_HA_COMP_STATUS_OK; 14084 14085 ctl_pause_rtr = 1; 14086 c->status = ret; 14087 return ret; 14088} 14089 14090struct ctl_ha_component ctl_ha_component_ctlisc = 14091{ 14092 .name = "CTL ISC", 14093 .state = CTL_HA_STATE_UNKNOWN, 14094 .init = ctl_isc_init, 14095 .start = ctl_isc_start, 14096 .quiesce = ctl_isc_quiesce 14097}; 14098 14099/* 14100 * vim: ts=8 14101 */ 14102