ctl.c revision 268550
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 268550 2014-07-12 02:24:52Z 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 * The default is to run with CTL_DONE_THREAD turned on. Completed 87 * transactions are queued for processing by the CTL work thread. When 88 * CTL_DONE_THREAD is not defined, completed transactions are processed in 89 * the caller's context. 90 */ 91#define CTL_DONE_THREAD 92 93/* 94 * Use the serial number and device ID provided by the backend, rather than 95 * making up our own. 96 */ 97#define CTL_USE_BACKEND_SN 98 99/* 100 * Size and alignment macros needed for Copan-specific HA hardware. These 101 * can go away when the HA code is re-written, and uses busdma for any 102 * hardware. 103 */ 104#define CTL_ALIGN_8B(target, source, type) \ 105 if (((uint32_t)source & 0x7) != 0) \ 106 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 107 else \ 108 target = (type)source; 109 110#define CTL_SIZE_8B(target, size) \ 111 if ((size & 0x7) != 0) \ 112 target = size + (0x8 - (size & 0x7)); \ 113 else \ 114 target = size; 115 116#define CTL_ALIGN_8B_MARGIN 16 117 118/* 119 * Template mode pages. 120 */ 121 122/* 123 * Note that these are default values only. The actual values will be 124 * filled in when the user does a mode sense. 125 */ 126static struct copan_power_subpage power_page_default = { 127 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 128 /*subpage*/ PWR_SUBPAGE_CODE, 129 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 130 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 131 /*page_version*/ PWR_VERSION, 132 /* total_luns */ 26, 133 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 134 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136 0, 0, 0, 0, 0, 0} 137}; 138 139static struct copan_power_subpage power_page_changeable = { 140 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 141 /*subpage*/ PWR_SUBPAGE_CODE, 142 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 143 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 144 /*page_version*/ 0, 145 /* total_luns */ 0, 146 /* max_active_luns*/ 0, 147 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149 0, 0, 0, 0, 0, 0} 150}; 151 152static struct copan_aps_subpage aps_page_default = { 153 APS_PAGE_CODE | SMPH_SPF, //page_code 154 APS_SUBPAGE_CODE, //subpage 155 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 156 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 157 APS_VERSION, //page_version 158 0, //lock_active 159 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 161 0, 0, 0, 0, 0} //reserved 162}; 163 164static struct copan_aps_subpage aps_page_changeable = { 165 APS_PAGE_CODE | SMPH_SPF, //page_code 166 APS_SUBPAGE_CODE, //subpage 167 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 168 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 169 0, //page_version 170 0, //lock_active 171 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173 0, 0, 0, 0, 0} //reserved 174}; 175 176static struct copan_debugconf_subpage debugconf_page_default = { 177 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 178 DBGCNF_SUBPAGE_CODE, /* subpage */ 179 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 180 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 181 DBGCNF_VERSION, /* page_version */ 182 {CTL_TIME_IO_DEFAULT_SECS>>8, 183 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 184}; 185 186static struct copan_debugconf_subpage debugconf_page_changeable = { 187 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 188 DBGCNF_SUBPAGE_CODE, /* subpage */ 189 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 190 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 191 0, /* page_version */ 192 {0xff,0xff}, /* ctl_time_io_secs */ 193}; 194 195static struct scsi_format_page format_page_default = { 196 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 197 /*page_length*/sizeof(struct scsi_format_page) - 2, 198 /*tracks_per_zone*/ {0, 0}, 199 /*alt_sectors_per_zone*/ {0, 0}, 200 /*alt_tracks_per_zone*/ {0, 0}, 201 /*alt_tracks_per_lun*/ {0, 0}, 202 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 203 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 204 /*bytes_per_sector*/ {0, 0}, 205 /*interleave*/ {0, 0}, 206 /*track_skew*/ {0, 0}, 207 /*cylinder_skew*/ {0, 0}, 208 /*flags*/ SFP_HSEC, 209 /*reserved*/ {0, 0, 0} 210}; 211 212static struct scsi_format_page format_page_changeable = { 213 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 214 /*page_length*/sizeof(struct scsi_format_page) - 2, 215 /*tracks_per_zone*/ {0, 0}, 216 /*alt_sectors_per_zone*/ {0, 0}, 217 /*alt_tracks_per_zone*/ {0, 0}, 218 /*alt_tracks_per_lun*/ {0, 0}, 219 /*sectors_per_track*/ {0, 0}, 220 /*bytes_per_sector*/ {0, 0}, 221 /*interleave*/ {0, 0}, 222 /*track_skew*/ {0, 0}, 223 /*cylinder_skew*/ {0, 0}, 224 /*flags*/ 0, 225 /*reserved*/ {0, 0, 0} 226}; 227 228static struct scsi_rigid_disk_page rigid_disk_page_default = { 229 /*page_code*/SMS_RIGID_DISK_PAGE, 230 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 231 /*cylinders*/ {0, 0, 0}, 232 /*heads*/ CTL_DEFAULT_HEADS, 233 /*start_write_precomp*/ {0, 0, 0}, 234 /*start_reduced_current*/ {0, 0, 0}, 235 /*step_rate*/ {0, 0}, 236 /*landing_zone_cylinder*/ {0, 0, 0}, 237 /*rpl*/ SRDP_RPL_DISABLED, 238 /*rotational_offset*/ 0, 239 /*reserved1*/ 0, 240 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 241 CTL_DEFAULT_ROTATION_RATE & 0xff}, 242 /*reserved2*/ {0, 0} 243}; 244 245static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 246 /*page_code*/SMS_RIGID_DISK_PAGE, 247 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 248 /*cylinders*/ {0, 0, 0}, 249 /*heads*/ 0, 250 /*start_write_precomp*/ {0, 0, 0}, 251 /*start_reduced_current*/ {0, 0, 0}, 252 /*step_rate*/ {0, 0}, 253 /*landing_zone_cylinder*/ {0, 0, 0}, 254 /*rpl*/ 0, 255 /*rotational_offset*/ 0, 256 /*reserved1*/ 0, 257 /*rotation_rate*/ {0, 0}, 258 /*reserved2*/ {0, 0} 259}; 260 261static struct scsi_caching_page caching_page_default = { 262 /*page_code*/SMS_CACHING_PAGE, 263 /*page_length*/sizeof(struct scsi_caching_page) - 2, 264 /*flags1*/ SCP_DISC | SCP_WCE, 265 /*ret_priority*/ 0, 266 /*disable_pf_transfer_len*/ {0xff, 0xff}, 267 /*min_prefetch*/ {0, 0}, 268 /*max_prefetch*/ {0xff, 0xff}, 269 /*max_pf_ceiling*/ {0xff, 0xff}, 270 /*flags2*/ 0, 271 /*cache_segments*/ 0, 272 /*cache_seg_size*/ {0, 0}, 273 /*reserved*/ 0, 274 /*non_cache_seg_size*/ {0, 0, 0} 275}; 276 277static struct scsi_caching_page caching_page_changeable = { 278 /*page_code*/SMS_CACHING_PAGE, 279 /*page_length*/sizeof(struct scsi_caching_page) - 2, 280 /*flags1*/ 0, 281 /*ret_priority*/ 0, 282 /*disable_pf_transfer_len*/ {0, 0}, 283 /*min_prefetch*/ {0, 0}, 284 /*max_prefetch*/ {0, 0}, 285 /*max_pf_ceiling*/ {0, 0}, 286 /*flags2*/ 0, 287 /*cache_segments*/ 0, 288 /*cache_seg_size*/ {0, 0}, 289 /*reserved*/ 0, 290 /*non_cache_seg_size*/ {0, 0, 0} 291}; 292 293static struct scsi_control_page control_page_default = { 294 /*page_code*/SMS_CONTROL_MODE_PAGE, 295 /*page_length*/sizeof(struct scsi_control_page) - 2, 296 /*rlec*/0, 297 /*queue_flags*/0, 298 /*eca_and_aen*/0, 299 /*reserved*/0, 300 /*aen_holdoff_period*/{0, 0} 301}; 302 303static struct scsi_control_page control_page_changeable = { 304 /*page_code*/SMS_CONTROL_MODE_PAGE, 305 /*page_length*/sizeof(struct scsi_control_page) - 2, 306 /*rlec*/SCP_DSENSE, 307 /*queue_flags*/0, 308 /*eca_and_aen*/0, 309 /*reserved*/0, 310 /*aen_holdoff_period*/{0, 0} 311}; 312 313 314/* 315 * XXX KDM move these into the softc. 316 */ 317static int rcv_sync_msg; 318static int persis_offset; 319static uint8_t ctl_pause_rtr; 320static int ctl_is_single = 1; 321static int index_to_aps_page; 322 323SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 324static int worker_threads = 1; 325TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 326SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 327 &worker_threads, 1, "Number of worker threads"); 328static int verbose = 0; 329TUNABLE_INT("kern.cam.ctl.verbose", &verbose); 330SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 331 &verbose, 0, "Show SCSI errors returned to initiator"); 332 333/* 334 * Serial number (0x80), device id (0x83), supported pages (0x00), 335 * Block limits (0xB0) and Logical Block Provisioning (0xB2) 336 */ 337#define SCSI_EVPD_NUM_SUPPORTED_PAGES 5 338 339static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 340 int param); 341static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 342static int ctl_init(void); 343void ctl_shutdown(void); 344static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 345static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 346static void ctl_ioctl_online(void *arg); 347static void ctl_ioctl_offline(void *arg); 348static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id); 349static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id); 350static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 351static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 352static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 353static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock); 354static int ctl_ioctl_submit_wait(union ctl_io *io); 355static void ctl_ioctl_datamove(union ctl_io *io); 356static void ctl_ioctl_done(union ctl_io *io); 357static void ctl_ioctl_hard_startstop_callback(void *arg, 358 struct cfi_metatask *metatask); 359static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 360static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 361 struct ctl_ooa *ooa_hdr, 362 struct ctl_ooa_entry *kern_entries); 363static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 364 struct thread *td); 365uint32_t ctl_get_resindex(struct ctl_nexus *nexus); 366uint32_t ctl_port_idx(int port_num); 367#ifdef unused 368static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 369 uint32_t targ_target, uint32_t targ_lun, 370 int can_wait); 371static void ctl_kfree_io(union ctl_io *io); 372#endif /* unused */ 373static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 374 struct ctl_be_lun *be_lun, struct ctl_id target_id); 375static int ctl_free_lun(struct ctl_lun *lun); 376static void ctl_create_lun(struct ctl_be_lun *be_lun); 377/** 378static void ctl_failover_change_pages(struct ctl_softc *softc, 379 struct ctl_scsiio *ctsio, int master); 380**/ 381 382static int ctl_do_mode_select(union ctl_io *io); 383static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 384 uint64_t res_key, uint64_t sa_res_key, 385 uint8_t type, uint32_t residx, 386 struct ctl_scsiio *ctsio, 387 struct scsi_per_res_out *cdb, 388 struct scsi_per_res_out_parms* param); 389static void ctl_pro_preempt_other(struct ctl_lun *lun, 390 union ctl_ha_msg *msg); 391static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 392static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 393static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 394static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 395static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 396 int alloc_len); 397static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 398static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 399static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 400static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 401static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 402static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 403 union ctl_io *ooa_io); 404static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 405 union ctl_io *starting_io); 406static int ctl_check_blocked(struct ctl_lun *lun); 407static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 408 struct ctl_lun *lun, 409 struct ctl_cmd_entry *entry, 410 struct ctl_scsiio *ctsio); 411//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 412static void ctl_failover(void); 413static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 414 struct ctl_scsiio *ctsio); 415static int ctl_scsiio(struct ctl_scsiio *ctsio); 416 417static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 418static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 419 ctl_ua_type ua_type); 420static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 421 ctl_ua_type ua_type); 422static int ctl_abort_task(union ctl_io *io); 423static void ctl_run_task_queue(struct ctl_softc *ctl_softc); 424#ifdef CTL_IO_DELAY 425static void ctl_datamove_timer_wakeup(void *arg); 426static void ctl_done_timer_wakeup(void *arg); 427#endif /* CTL_IO_DELAY */ 428 429static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 430static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 431static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 432static void ctl_datamove_remote_write(union ctl_io *io); 433static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 434static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 435static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 436static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 437 ctl_ha_dt_cb callback); 438static void ctl_datamove_remote_read(union ctl_io *io); 439static void ctl_datamove_remote(union ctl_io *io); 440static int ctl_process_done(union ctl_io *io, int have_lock); 441static void ctl_work_thread(void *arg); 442 443/* 444 * Load the serialization table. This isn't very pretty, but is probably 445 * the easiest way to do it. 446 */ 447#include "ctl_ser_table.c" 448 449/* 450 * We only need to define open, close and ioctl routines for this driver. 451 */ 452static struct cdevsw ctl_cdevsw = { 453 .d_version = D_VERSION, 454 .d_flags = 0, 455 .d_open = ctl_open, 456 .d_close = ctl_close, 457 .d_ioctl = ctl_ioctl, 458 .d_name = "ctl", 459}; 460 461 462MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 463 464static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 465 466static moduledata_t ctl_moduledata = { 467 "ctl", 468 ctl_module_event_handler, 469 NULL 470}; 471 472DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 473MODULE_VERSION(ctl, 1); 474 475static void 476ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 477 union ctl_ha_msg *msg_info) 478{ 479 struct ctl_scsiio *ctsio; 480 481 if (msg_info->hdr.original_sc == NULL) { 482 printf("%s: original_sc == NULL!\n", __func__); 483 /* XXX KDM now what? */ 484 return; 485 } 486 487 ctsio = &msg_info->hdr.original_sc->scsiio; 488 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 489 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 490 ctsio->io_hdr.status = msg_info->hdr.status; 491 ctsio->scsi_status = msg_info->scsi.scsi_status; 492 ctsio->sense_len = msg_info->scsi.sense_len; 493 ctsio->sense_residual = msg_info->scsi.sense_residual; 494 ctsio->residual = msg_info->scsi.residual; 495 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 496 sizeof(ctsio->sense_data)); 497 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 498 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 499 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links); 500 ctl_wakeup_thread(); 501} 502 503static void 504ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 505 union ctl_ha_msg *msg_info) 506{ 507 struct ctl_scsiio *ctsio; 508 509 if (msg_info->hdr.serializing_sc == NULL) { 510 printf("%s: serializing_sc == NULL!\n", __func__); 511 /* XXX KDM now what? */ 512 return; 513 } 514 515 ctsio = &msg_info->hdr.serializing_sc->scsiio; 516#if 0 517 /* 518 * Attempt to catch the situation where an I/O has 519 * been freed, and we're using it again. 520 */ 521 if (ctsio->io_hdr.io_type == 0xff) { 522 union ctl_io *tmp_io; 523 tmp_io = (union ctl_io *)ctsio; 524 printf("%s: %p use after free!\n", __func__, 525 ctsio); 526 printf("%s: type %d msg %d cdb %x iptl: " 527 "%d:%d:%d:%d tag 0x%04x " 528 "flag %#x status %x\n", 529 __func__, 530 tmp_io->io_hdr.io_type, 531 tmp_io->io_hdr.msg_type, 532 tmp_io->scsiio.cdb[0], 533 tmp_io->io_hdr.nexus.initid.id, 534 tmp_io->io_hdr.nexus.targ_port, 535 tmp_io->io_hdr.nexus.targ_target.id, 536 tmp_io->io_hdr.nexus.targ_lun, 537 (tmp_io->io_hdr.io_type == 538 CTL_IO_TASK) ? 539 tmp_io->taskio.tag_num : 540 tmp_io->scsiio.tag_num, 541 tmp_io->io_hdr.flags, 542 tmp_io->io_hdr.status); 543 } 544#endif 545 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 546 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links); 547 ctl_wakeup_thread(); 548} 549 550/* 551 * ISC (Inter Shelf Communication) event handler. Events from the HA 552 * subsystem come in here. 553 */ 554static void 555ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 556{ 557 struct ctl_softc *ctl_softc; 558 union ctl_io *io; 559 struct ctl_prio *presio; 560 ctl_ha_status isc_status; 561 562 ctl_softc = control_softc; 563 io = NULL; 564 565 566#if 0 567 printf("CTL: Isc Msg event %d\n", event); 568#endif 569 if (event == CTL_HA_EVT_MSG_RECV) { 570 union ctl_ha_msg msg_info; 571 572 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 573 sizeof(msg_info), /*wait*/ 0); 574#if 0 575 printf("CTL: msg_type %d\n", msg_info.msg_type); 576#endif 577 if (isc_status != 0) { 578 printf("Error receiving message, status = %d\n", 579 isc_status); 580 return; 581 } 582 mtx_lock(&ctl_softc->ctl_lock); 583 584 switch (msg_info.hdr.msg_type) { 585 case CTL_MSG_SERIALIZE: 586#if 0 587 printf("Serialize\n"); 588#endif 589 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 590 if (io == NULL) { 591 printf("ctl_isc_event_handler: can't allocate " 592 "ctl_io!\n"); 593 /* Bad Juju */ 594 /* Need to set busy and send msg back */ 595 mtx_unlock(&ctl_softc->ctl_lock); 596 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 597 msg_info.hdr.status = CTL_SCSI_ERROR; 598 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 599 msg_info.scsi.sense_len = 0; 600 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 601 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 602 } 603 goto bailout; 604 } 605 ctl_zero_io(io); 606 // populate ctsio from msg_info 607 io->io_hdr.io_type = CTL_IO_SCSI; 608 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 609 io->io_hdr.original_sc = msg_info.hdr.original_sc; 610#if 0 611 printf("pOrig %x\n", (int)msg_info.original_sc); 612#endif 613 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 614 CTL_FLAG_IO_ACTIVE; 615 /* 616 * If we're in serialization-only mode, we don't 617 * want to go through full done processing. Thus 618 * the COPY flag. 619 * 620 * XXX KDM add another flag that is more specific. 621 */ 622 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 623 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 624 io->io_hdr.nexus = msg_info.hdr.nexus; 625#if 0 626 printf("targ %d, port %d, iid %d, lun %d\n", 627 io->io_hdr.nexus.targ_target.id, 628 io->io_hdr.nexus.targ_port, 629 io->io_hdr.nexus.initid.id, 630 io->io_hdr.nexus.targ_lun); 631#endif 632 io->scsiio.tag_num = msg_info.scsi.tag_num; 633 io->scsiio.tag_type = msg_info.scsi.tag_type; 634 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 635 CTL_MAX_CDBLEN); 636 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 637 struct ctl_cmd_entry *entry; 638 uint8_t opcode; 639 640 opcode = io->scsiio.cdb[0]; 641 entry = &ctl_cmd_table[opcode]; 642 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 643 io->io_hdr.flags |= 644 entry->flags & CTL_FLAG_DATA_MASK; 645 } 646 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 647 &io->io_hdr, links); 648 ctl_wakeup_thread(); 649 break; 650 651 /* Performed on the Originating SC, XFER mode only */ 652 case CTL_MSG_DATAMOVE: { 653 struct ctl_sg_entry *sgl; 654 int i, j; 655 656 io = msg_info.hdr.original_sc; 657 if (io == NULL) { 658 printf("%s: original_sc == NULL!\n", __func__); 659 /* XXX KDM do something here */ 660 break; 661 } 662 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 663 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 664 /* 665 * Keep track of this, we need to send it back over 666 * when the datamove is complete. 667 */ 668 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 669 670 if (msg_info.dt.sg_sequence == 0) { 671 /* 672 * XXX KDM we use the preallocated S/G list 673 * here, but we'll need to change this to 674 * dynamic allocation if we need larger S/G 675 * lists. 676 */ 677 if (msg_info.dt.kern_sg_entries > 678 sizeof(io->io_hdr.remote_sglist) / 679 sizeof(io->io_hdr.remote_sglist[0])) { 680 printf("%s: number of S/G entries " 681 "needed %u > allocated num %zd\n", 682 __func__, 683 msg_info.dt.kern_sg_entries, 684 sizeof(io->io_hdr.remote_sglist)/ 685 sizeof(io->io_hdr.remote_sglist[0])); 686 687 /* 688 * XXX KDM send a message back to 689 * the other side to shut down the 690 * DMA. The error will come back 691 * through via the normal channel. 692 */ 693 break; 694 } 695 sgl = io->io_hdr.remote_sglist; 696 memset(sgl, 0, 697 sizeof(io->io_hdr.remote_sglist)); 698 699 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 700 701 io->scsiio.kern_sg_entries = 702 msg_info.dt.kern_sg_entries; 703 io->scsiio.rem_sg_entries = 704 msg_info.dt.kern_sg_entries; 705 io->scsiio.kern_data_len = 706 msg_info.dt.kern_data_len; 707 io->scsiio.kern_total_len = 708 msg_info.dt.kern_total_len; 709 io->scsiio.kern_data_resid = 710 msg_info.dt.kern_data_resid; 711 io->scsiio.kern_rel_offset = 712 msg_info.dt.kern_rel_offset; 713 /* 714 * Clear out per-DMA flags. 715 */ 716 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 717 /* 718 * Add per-DMA flags that are set for this 719 * particular DMA request. 720 */ 721 io->io_hdr.flags |= msg_info.dt.flags & 722 CTL_FLAG_RDMA_MASK; 723 } else 724 sgl = (struct ctl_sg_entry *) 725 io->scsiio.kern_data_ptr; 726 727 for (i = msg_info.dt.sent_sg_entries, j = 0; 728 i < (msg_info.dt.sent_sg_entries + 729 msg_info.dt.cur_sg_entries); i++, j++) { 730 sgl[i].addr = msg_info.dt.sg_list[j].addr; 731 sgl[i].len = msg_info.dt.sg_list[j].len; 732 733#if 0 734 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 735 __func__, 736 msg_info.dt.sg_list[j].addr, 737 msg_info.dt.sg_list[j].len, 738 sgl[i].addr, sgl[i].len, j, i); 739#endif 740 } 741#if 0 742 memcpy(&sgl[msg_info.dt.sent_sg_entries], 743 msg_info.dt.sg_list, 744 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 745#endif 746 747 /* 748 * If this is the last piece of the I/O, we've got 749 * the full S/G list. Queue processing in the thread. 750 * Otherwise wait for the next piece. 751 */ 752 if (msg_info.dt.sg_last != 0) { 753 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 754 &io->io_hdr, links); 755 ctl_wakeup_thread(); 756 } 757 break; 758 } 759 /* Performed on the Serializing (primary) SC, XFER mode only */ 760 case CTL_MSG_DATAMOVE_DONE: { 761 if (msg_info.hdr.serializing_sc == NULL) { 762 printf("%s: serializing_sc == NULL!\n", 763 __func__); 764 /* XXX KDM now what? */ 765 break; 766 } 767 /* 768 * We grab the sense information here in case 769 * there was a failure, so we can return status 770 * back to the initiator. 771 */ 772 io = msg_info.hdr.serializing_sc; 773 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 774 io->io_hdr.status = msg_info.hdr.status; 775 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 776 io->scsiio.sense_len = msg_info.scsi.sense_len; 777 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 778 io->io_hdr.port_status = msg_info.scsi.fetd_status; 779 io->scsiio.residual = msg_info.scsi.residual; 780 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 781 sizeof(io->scsiio.sense_data)); 782 783 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 784 &io->io_hdr, links); 785 ctl_wakeup_thread(); 786 break; 787 } 788 789 /* Preformed on Originating SC, SER_ONLY mode */ 790 case CTL_MSG_R2R: 791 io = msg_info.hdr.original_sc; 792 if (io == NULL) { 793 printf("%s: Major Bummer\n", __func__); 794 mtx_unlock(&ctl_softc->ctl_lock); 795 return; 796 } else { 797#if 0 798 printf("pOrig %x\n",(int) ctsio); 799#endif 800 } 801 io->io_hdr.msg_type = CTL_MSG_R2R; 802 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 803 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 804 &io->io_hdr, links); 805 ctl_wakeup_thread(); 806 break; 807 808 /* 809 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 810 * mode. 811 * Performed on the Originating (i.e. secondary) SC in XFER 812 * mode 813 */ 814 case CTL_MSG_FINISH_IO: 815 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 816 ctl_isc_handler_finish_xfer(ctl_softc, 817 &msg_info); 818 else 819 ctl_isc_handler_finish_ser_only(ctl_softc, 820 &msg_info); 821 break; 822 823 /* Preformed on Originating SC */ 824 case CTL_MSG_BAD_JUJU: 825 io = msg_info.hdr.original_sc; 826 if (io == NULL) { 827 printf("%s: Bad JUJU!, original_sc is NULL!\n", 828 __func__); 829 break; 830 } 831 ctl_copy_sense_data(&msg_info, io); 832 /* 833 * IO should have already been cleaned up on other 834 * SC so clear this flag so we won't send a message 835 * back to finish the IO there. 836 */ 837 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 838 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 839 840 /* io = msg_info.hdr.serializing_sc; */ 841 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 842 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 843 &io->io_hdr, links); 844 ctl_wakeup_thread(); 845 break; 846 847 /* Handle resets sent from the other side */ 848 case CTL_MSG_MANAGE_TASKS: { 849 struct ctl_taskio *taskio; 850 taskio = (struct ctl_taskio *)ctl_alloc_io( 851 (void *)ctl_softc->othersc_pool); 852 if (taskio == NULL) { 853 printf("ctl_isc_event_handler: can't allocate " 854 "ctl_io!\n"); 855 /* Bad Juju */ 856 /* should I just call the proper reset func 857 here??? */ 858 mtx_unlock(&ctl_softc->ctl_lock); 859 goto bailout; 860 } 861 ctl_zero_io((union ctl_io *)taskio); 862 taskio->io_hdr.io_type = CTL_IO_TASK; 863 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 864 taskio->io_hdr.nexus = msg_info.hdr.nexus; 865 taskio->task_action = msg_info.task.task_action; 866 taskio->tag_num = msg_info.task.tag_num; 867 taskio->tag_type = msg_info.task.tag_type; 868#ifdef CTL_TIME_IO 869 taskio->io_hdr.start_time = time_uptime; 870 getbintime(&taskio->io_hdr.start_bt); 871#if 0 872 cs_prof_gettime(&taskio->io_hdr.start_ticks); 873#endif 874#endif /* CTL_TIME_IO */ 875 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, 876 &taskio->io_hdr, links); 877 ctl_softc->flags |= CTL_FLAG_TASK_PENDING; 878 ctl_wakeup_thread(); 879 break; 880 } 881 /* Persistent Reserve action which needs attention */ 882 case CTL_MSG_PERS_ACTION: 883 presio = (struct ctl_prio *)ctl_alloc_io( 884 (void *)ctl_softc->othersc_pool); 885 if (presio == NULL) { 886 printf("ctl_isc_event_handler: can't allocate " 887 "ctl_io!\n"); 888 /* Bad Juju */ 889 /* Need to set busy and send msg back */ 890 mtx_unlock(&ctl_softc->ctl_lock); 891 goto bailout; 892 } 893 ctl_zero_io((union ctl_io *)presio); 894 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 895 presio->pr_msg = msg_info.pr; 896 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 897 &presio->io_hdr, links); 898 ctl_wakeup_thread(); 899 break; 900 case CTL_MSG_SYNC_FE: 901 rcv_sync_msg = 1; 902 break; 903 case CTL_MSG_APS_LOCK: { 904 // It's quicker to execute this then to 905 // queue it. 906 struct ctl_lun *lun; 907 struct ctl_page_index *page_index; 908 struct copan_aps_subpage *current_sp; 909 uint32_t targ_lun; 910 911 targ_lun = msg_info.hdr.nexus.targ_lun; 912 if (msg_info.hdr.nexus.lun_map_fn != NULL) 913 targ_lun = msg_info.hdr.nexus.lun_map_fn(msg_info.hdr.nexus.lun_map_arg, targ_lun); 914 915 lun = ctl_softc->ctl_luns[targ_lun]; 916 page_index = &lun->mode_pages.index[index_to_aps_page]; 917 current_sp = (struct copan_aps_subpage *) 918 (page_index->page_data + 919 (page_index->page_len * CTL_PAGE_CURRENT)); 920 921 current_sp->lock_active = msg_info.aps.lock_flag; 922 break; 923 } 924 default: 925 printf("How did I get here?\n"); 926 } 927 mtx_unlock(&ctl_softc->ctl_lock); 928 } else if (event == CTL_HA_EVT_MSG_SENT) { 929 if (param != CTL_HA_STATUS_SUCCESS) { 930 printf("Bad status from ctl_ha_msg_send status %d\n", 931 param); 932 } 933 return; 934 } else if (event == CTL_HA_EVT_DISCONNECT) { 935 printf("CTL: Got a disconnect from Isc\n"); 936 return; 937 } else { 938 printf("ctl_isc_event_handler: Unknown event %d\n", event); 939 return; 940 } 941 942bailout: 943 return; 944} 945 946static void 947ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 948{ 949 struct scsi_sense_data *sense; 950 951 sense = &dest->scsiio.sense_data; 952 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 953 dest->scsiio.scsi_status = src->scsi.scsi_status; 954 dest->scsiio.sense_len = src->scsi.sense_len; 955 dest->io_hdr.status = src->hdr.status; 956} 957 958static int 959ctl_init(void) 960{ 961 struct ctl_softc *softc; 962 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 963 struct ctl_frontend *fe; 964 uint8_t sc_id =0; 965 int i, error, retval; 966 //int isc_retval; 967 968 retval = 0; 969 ctl_pause_rtr = 0; 970 rcv_sync_msg = 0; 971 972 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 973 M_WAITOK | M_ZERO); 974 softc = control_softc; 975 976 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 977 "cam/ctl"); 978 979 softc->dev->si_drv1 = softc; 980 981 /* 982 * By default, return a "bad LUN" peripheral qualifier for unknown 983 * LUNs. The user can override this default using the tunable or 984 * sysctl. See the comment in ctl_inquiry_std() for more details. 985 */ 986 softc->inquiry_pq_no_lun = 1; 987 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 988 &softc->inquiry_pq_no_lun); 989 sysctl_ctx_init(&softc->sysctl_ctx); 990 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 991 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 992 CTLFLAG_RD, 0, "CAM Target Layer"); 993 994 if (softc->sysctl_tree == NULL) { 995 printf("%s: unable to allocate sysctl tree\n", __func__); 996 destroy_dev(softc->dev); 997 free(control_softc, M_DEVBUF); 998 control_softc = NULL; 999 return (ENOMEM); 1000 } 1001 1002 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1003 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1004 "inquiry_pq_no_lun", CTLFLAG_RW, 1005 &softc->inquiry_pq_no_lun, 0, 1006 "Report no lun possible for invalid LUNs"); 1007 1008 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1009 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 1010 softc->open_count = 0; 1011 1012 /* 1013 * Default to actually sending a SYNCHRONIZE CACHE command down to 1014 * the drive. 1015 */ 1016 softc->flags = CTL_FLAG_REAL_SYNC; 1017 1018 /* 1019 * In Copan's HA scheme, the "master" and "slave" roles are 1020 * figured out through the slot the controller is in. Although it 1021 * is an active/active system, someone has to be in charge. 1022 */ 1023#ifdef NEEDTOPORT 1024 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1025#endif 1026 1027 if (sc_id == 0) { 1028 softc->flags |= CTL_FLAG_MASTER_SHELF; 1029 persis_offset = 0; 1030 } else 1031 persis_offset = CTL_MAX_INITIATORS; 1032 1033 /* 1034 * XXX KDM need to figure out where we want to get our target ID 1035 * and WWID. Is it different on each port? 1036 */ 1037 softc->target.id = 0; 1038 softc->target.wwid[0] = 0x12345678; 1039 softc->target.wwid[1] = 0x87654321; 1040 STAILQ_INIT(&softc->lun_list); 1041 STAILQ_INIT(&softc->pending_lun_queue); 1042 STAILQ_INIT(&softc->task_queue); 1043 STAILQ_INIT(&softc->incoming_queue); 1044 STAILQ_INIT(&softc->rtr_queue); 1045 STAILQ_INIT(&softc->done_queue); 1046 STAILQ_INIT(&softc->isc_queue); 1047 STAILQ_INIT(&softc->fe_list); 1048 STAILQ_INIT(&softc->be_list); 1049 STAILQ_INIT(&softc->io_pools); 1050 1051 /* 1052 * We don't bother calling these with ctl_lock held here, because, 1053 * in theory, no one else can try to do anything while we're in our 1054 * module init routine. 1055 */ 1056 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1057 &internal_pool)!= 0){ 1058 printf("ctl: can't allocate %d entry internal pool, " 1059 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1060 return (ENOMEM); 1061 } 1062 1063 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1064 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1065 printf("ctl: can't allocate %d entry emergency pool, " 1066 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1067 ctl_pool_free(internal_pool); 1068 return (ENOMEM); 1069 } 1070 1071 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1072 &other_pool) != 0) 1073 { 1074 printf("ctl: can't allocate %d entry other SC pool, " 1075 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1076 ctl_pool_free(internal_pool); 1077 ctl_pool_free(emergency_pool); 1078 return (ENOMEM); 1079 } 1080 1081 softc->internal_pool = internal_pool; 1082 softc->emergency_pool = emergency_pool; 1083 softc->othersc_pool = other_pool; 1084 1085 if (worker_threads > MAXCPU || worker_threads == 0) { 1086 printf("invalid kern.cam.ctl.worker_threads value; " 1087 "setting to 1"); 1088 worker_threads = 1; 1089 } else if (worker_threads < 0) { 1090 if (mp_ncpus > 2) { 1091 /* 1092 * Using more than two worker threads actually hurts 1093 * performance due to lock contention. 1094 */ 1095 worker_threads = 2; 1096 } else { 1097 worker_threads = 1; 1098 } 1099 } 1100 1101 for (i = 0; i < worker_threads; i++) { 1102 error = kproc_kthread_add(ctl_work_thread, softc, 1103 &softc->work_thread, NULL, 0, 0, "ctl", "work%d", i); 1104 if (error != 0) { 1105 printf("error creating CTL work thread!\n"); 1106 ctl_pool_free(internal_pool); 1107 ctl_pool_free(emergency_pool); 1108 ctl_pool_free(other_pool); 1109 return (error); 1110 } 1111 } 1112 if (bootverbose) 1113 printf("ctl: CAM Target Layer loaded\n"); 1114 1115 /* 1116 * Initialize the initiator and portname mappings 1117 */ 1118 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid)); 1119 1120 /* 1121 * Initialize the ioctl front end. 1122 */ 1123 fe = &softc->ioctl_info.fe; 1124 sprintf(softc->ioctl_info.port_name, "CTL ioctl"); 1125 fe->port_type = CTL_PORT_IOCTL; 1126 fe->num_requested_ctl_io = 100; 1127 fe->port_name = softc->ioctl_info.port_name; 1128 fe->port_online = ctl_ioctl_online; 1129 fe->port_offline = ctl_ioctl_offline; 1130 fe->onoff_arg = &softc->ioctl_info; 1131 fe->targ_enable = ctl_ioctl_targ_enable; 1132 fe->targ_disable = ctl_ioctl_targ_disable; 1133 fe->lun_enable = ctl_ioctl_lun_enable; 1134 fe->lun_disable = ctl_ioctl_lun_disable; 1135 fe->targ_lun_arg = &softc->ioctl_info; 1136 fe->fe_datamove = ctl_ioctl_datamove; 1137 fe->fe_done = ctl_ioctl_done; 1138 fe->max_targets = 15; 1139 fe->max_target_id = 15; 1140 1141 if (ctl_frontend_register(&softc->ioctl_info.fe, 1142 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1143 printf("ctl: ioctl front end registration failed, will " 1144 "continue anyway\n"); 1145 } 1146 1147#ifdef CTL_IO_DELAY 1148 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1149 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1150 sizeof(struct callout), CTL_TIMER_BYTES); 1151 return (EINVAL); 1152 } 1153#endif /* CTL_IO_DELAY */ 1154 1155 return (0); 1156} 1157 1158void 1159ctl_shutdown(void) 1160{ 1161 struct ctl_softc *softc; 1162 struct ctl_lun *lun, *next_lun; 1163 struct ctl_io_pool *pool; 1164 1165 softc = (struct ctl_softc *)control_softc; 1166 1167 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0) 1168 printf("ctl: ioctl front end deregistration failed\n"); 1169 1170 mtx_lock(&softc->ctl_lock); 1171 1172 /* 1173 * Free up each LUN. 1174 */ 1175 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1176 next_lun = STAILQ_NEXT(lun, links); 1177 ctl_free_lun(lun); 1178 } 1179 1180 mtx_unlock(&softc->ctl_lock); 1181 1182 /* 1183 * This will rip the rug out from under any FETDs or anyone else 1184 * that has a pool allocated. Since we increment our module 1185 * refcount any time someone outside the main CTL module allocates 1186 * a pool, we shouldn't have any problems here. The user won't be 1187 * able to unload the CTL module until client modules have 1188 * successfully unloaded. 1189 */ 1190 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1191 ctl_pool_free(pool); 1192 1193#if 0 1194 ctl_shutdown_thread(softc->work_thread); 1195#endif 1196 1197 mtx_destroy(&softc->pool_lock); 1198 mtx_destroy(&softc->ctl_lock); 1199 1200 destroy_dev(softc->dev); 1201 1202 sysctl_ctx_free(&softc->sysctl_ctx); 1203 1204 free(control_softc, M_DEVBUF); 1205 control_softc = NULL; 1206 1207 if (bootverbose) 1208 printf("ctl: CAM Target Layer unloaded\n"); 1209} 1210 1211static int 1212ctl_module_event_handler(module_t mod, int what, void *arg) 1213{ 1214 1215 switch (what) { 1216 case MOD_LOAD: 1217 return (ctl_init()); 1218 case MOD_UNLOAD: 1219 return (EBUSY); 1220 default: 1221 return (EOPNOTSUPP); 1222 } 1223} 1224 1225/* 1226 * XXX KDM should we do some access checks here? Bump a reference count to 1227 * prevent a CTL module from being unloaded while someone has it open? 1228 */ 1229static int 1230ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1231{ 1232 return (0); 1233} 1234 1235static int 1236ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1237{ 1238 return (0); 1239} 1240 1241int 1242ctl_port_enable(ctl_port_type port_type) 1243{ 1244 struct ctl_softc *softc; 1245 struct ctl_frontend *fe; 1246 1247 if (ctl_is_single == 0) { 1248 union ctl_ha_msg msg_info; 1249 int isc_retval; 1250 1251#if 0 1252 printf("%s: HA mode, synchronizing frontend enable\n", 1253 __func__); 1254#endif 1255 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1256 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1257 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1258 printf("Sync msg send error retval %d\n", isc_retval); 1259 } 1260 if (!rcv_sync_msg) { 1261 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1262 sizeof(msg_info), 1); 1263 } 1264#if 0 1265 printf("CTL:Frontend Enable\n"); 1266 } else { 1267 printf("%s: single mode, skipping frontend synchronization\n", 1268 __func__); 1269#endif 1270 } 1271 1272 softc = control_softc; 1273 1274 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1275 if (port_type & fe->port_type) 1276 { 1277#if 0 1278 printf("port %d\n", fe->targ_port); 1279#endif 1280 ctl_frontend_online(fe); 1281 } 1282 } 1283 1284 return (0); 1285} 1286 1287int 1288ctl_port_disable(ctl_port_type port_type) 1289{ 1290 struct ctl_softc *softc; 1291 struct ctl_frontend *fe; 1292 1293 softc = control_softc; 1294 1295 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1296 if (port_type & fe->port_type) 1297 ctl_frontend_offline(fe); 1298 } 1299 1300 return (0); 1301} 1302 1303/* 1304 * Returns 0 for success, 1 for failure. 1305 * Currently the only failure mode is if there aren't enough entries 1306 * allocated. So, in case of a failure, look at num_entries_dropped, 1307 * reallocate and try again. 1308 */ 1309int 1310ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1311 int *num_entries_filled, int *num_entries_dropped, 1312 ctl_port_type port_type, int no_virtual) 1313{ 1314 struct ctl_softc *softc; 1315 struct ctl_frontend *fe; 1316 int entries_dropped, entries_filled; 1317 int retval; 1318 int i; 1319 1320 softc = control_softc; 1321 1322 retval = 0; 1323 entries_filled = 0; 1324 entries_dropped = 0; 1325 1326 i = 0; 1327 mtx_lock(&softc->ctl_lock); 1328 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1329 struct ctl_port_entry *entry; 1330 1331 if ((fe->port_type & port_type) == 0) 1332 continue; 1333 1334 if ((no_virtual != 0) 1335 && (fe->virtual_port != 0)) 1336 continue; 1337 1338 if (entries_filled >= num_entries_alloced) { 1339 entries_dropped++; 1340 continue; 1341 } 1342 entry = &entries[i]; 1343 1344 entry->port_type = fe->port_type; 1345 strlcpy(entry->port_name, fe->port_name, 1346 sizeof(entry->port_name)); 1347 entry->physical_port = fe->physical_port; 1348 entry->virtual_port = fe->virtual_port; 1349 entry->wwnn = fe->wwnn; 1350 entry->wwpn = fe->wwpn; 1351 1352 i++; 1353 entries_filled++; 1354 } 1355 1356 mtx_unlock(&softc->ctl_lock); 1357 1358 if (entries_dropped > 0) 1359 retval = 1; 1360 1361 *num_entries_dropped = entries_dropped; 1362 *num_entries_filled = entries_filled; 1363 1364 return (retval); 1365} 1366 1367static void 1368ctl_ioctl_online(void *arg) 1369{ 1370 struct ctl_ioctl_info *ioctl_info; 1371 1372 ioctl_info = (struct ctl_ioctl_info *)arg; 1373 1374 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1375} 1376 1377static void 1378ctl_ioctl_offline(void *arg) 1379{ 1380 struct ctl_ioctl_info *ioctl_info; 1381 1382 ioctl_info = (struct ctl_ioctl_info *)arg; 1383 1384 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1385} 1386 1387/* 1388 * Remove an initiator by port number and initiator ID. 1389 * Returns 0 for success, 1 for failure. 1390 */ 1391int 1392ctl_remove_initiator(int32_t targ_port, uint32_t iid) 1393{ 1394 struct ctl_softc *softc; 1395 1396 softc = control_softc; 1397 1398 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1399 1400 if ((targ_port < 0) 1401 || (targ_port > CTL_MAX_PORTS)) { 1402 printf("%s: invalid port number %d\n", __func__, targ_port); 1403 return (1); 1404 } 1405 if (iid > CTL_MAX_INIT_PER_PORT) { 1406 printf("%s: initiator ID %u > maximun %u!\n", 1407 __func__, iid, CTL_MAX_INIT_PER_PORT); 1408 return (1); 1409 } 1410 1411 mtx_lock(&softc->ctl_lock); 1412 1413 softc->wwpn_iid[targ_port][iid].in_use = 0; 1414 1415 mtx_unlock(&softc->ctl_lock); 1416 1417 return (0); 1418} 1419 1420/* 1421 * Add an initiator to the initiator map. 1422 * Returns 0 for success, 1 for failure. 1423 */ 1424int 1425ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid) 1426{ 1427 struct ctl_softc *softc; 1428 int retval; 1429 1430 softc = control_softc; 1431 1432 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1433 1434 retval = 0; 1435 1436 if ((targ_port < 0) 1437 || (targ_port > CTL_MAX_PORTS)) { 1438 printf("%s: invalid port number %d\n", __func__, targ_port); 1439 return (1); 1440 } 1441 if (iid > CTL_MAX_INIT_PER_PORT) { 1442 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n", 1443 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1444 return (1); 1445 } 1446 1447 mtx_lock(&softc->ctl_lock); 1448 1449 if (softc->wwpn_iid[targ_port][iid].in_use != 0) { 1450 /* 1451 * We don't treat this as an error. 1452 */ 1453 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) { 1454 printf("%s: port %d iid %u WWPN %#jx arrived again?\n", 1455 __func__, targ_port, iid, (uintmax_t)wwpn); 1456 goto bailout; 1457 } 1458 1459 /* 1460 * This is an error, but what do we do about it? The 1461 * driver is telling us we have a new WWPN for this 1462 * initiator ID, so we pretty much need to use it. 1463 */ 1464 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is " 1465 "still at that address\n", __func__, targ_port, iid, 1466 (uintmax_t)wwpn, 1467 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn); 1468 1469 /* 1470 * XXX KDM clear have_ca and ua_pending on each LUN for 1471 * this initiator. 1472 */ 1473 } 1474 softc->wwpn_iid[targ_port][iid].in_use = 1; 1475 softc->wwpn_iid[targ_port][iid].iid = iid; 1476 softc->wwpn_iid[targ_port][iid].wwpn = wwpn; 1477 softc->wwpn_iid[targ_port][iid].port = targ_port; 1478 1479bailout: 1480 1481 mtx_unlock(&softc->ctl_lock); 1482 1483 return (retval); 1484} 1485 1486/* 1487 * XXX KDM should we pretend to do something in the target/lun 1488 * enable/disable functions? 1489 */ 1490static int 1491ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id) 1492{ 1493 return (0); 1494} 1495 1496static int 1497ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id) 1498{ 1499 return (0); 1500} 1501 1502static int 1503ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1504{ 1505 return (0); 1506} 1507 1508static int 1509ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1510{ 1511 return (0); 1512} 1513 1514/* 1515 * Data movement routine for the CTL ioctl frontend port. 1516 */ 1517static int 1518ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1519{ 1520 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1521 struct ctl_sg_entry ext_entry, kern_entry; 1522 int ext_sglen, ext_sg_entries, kern_sg_entries; 1523 int ext_sg_start, ext_offset; 1524 int len_to_copy, len_copied; 1525 int kern_watermark, ext_watermark; 1526 int ext_sglist_malloced; 1527 int i, j; 1528 1529 ext_sglist_malloced = 0; 1530 ext_sg_start = 0; 1531 ext_offset = 0; 1532 1533 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1534 1535 /* 1536 * If this flag is set, fake the data transfer. 1537 */ 1538 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1539 ctsio->ext_data_filled = ctsio->ext_data_len; 1540 goto bailout; 1541 } 1542 1543 /* 1544 * To simplify things here, if we have a single buffer, stick it in 1545 * a S/G entry and just make it a single entry S/G list. 1546 */ 1547 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1548 int len_seen; 1549 1550 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1551 1552 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1553 M_WAITOK); 1554 ext_sglist_malloced = 1; 1555 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1556 ext_sglen) != 0) { 1557 ctl_set_internal_failure(ctsio, 1558 /*sks_valid*/ 0, 1559 /*retry_count*/ 0); 1560 goto bailout; 1561 } 1562 ext_sg_entries = ctsio->ext_sg_entries; 1563 len_seen = 0; 1564 for (i = 0; i < ext_sg_entries; i++) { 1565 if ((len_seen + ext_sglist[i].len) >= 1566 ctsio->ext_data_filled) { 1567 ext_sg_start = i; 1568 ext_offset = ctsio->ext_data_filled - len_seen; 1569 break; 1570 } 1571 len_seen += ext_sglist[i].len; 1572 } 1573 } else { 1574 ext_sglist = &ext_entry; 1575 ext_sglist->addr = ctsio->ext_data_ptr; 1576 ext_sglist->len = ctsio->ext_data_len; 1577 ext_sg_entries = 1; 1578 ext_sg_start = 0; 1579 ext_offset = ctsio->ext_data_filled; 1580 } 1581 1582 if (ctsio->kern_sg_entries > 0) { 1583 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1584 kern_sg_entries = ctsio->kern_sg_entries; 1585 } else { 1586 kern_sglist = &kern_entry; 1587 kern_sglist->addr = ctsio->kern_data_ptr; 1588 kern_sglist->len = ctsio->kern_data_len; 1589 kern_sg_entries = 1; 1590 } 1591 1592 1593 kern_watermark = 0; 1594 ext_watermark = ext_offset; 1595 len_copied = 0; 1596 for (i = ext_sg_start, j = 0; 1597 i < ext_sg_entries && j < kern_sg_entries;) { 1598 uint8_t *ext_ptr, *kern_ptr; 1599 1600 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1601 kern_sglist[j].len - kern_watermark); 1602 1603 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1604 ext_ptr = ext_ptr + ext_watermark; 1605 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1606 /* 1607 * XXX KDM fix this! 1608 */ 1609 panic("need to implement bus address support"); 1610#if 0 1611 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1612#endif 1613 } else 1614 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1615 kern_ptr = kern_ptr + kern_watermark; 1616 1617 kern_watermark += len_to_copy; 1618 ext_watermark += len_to_copy; 1619 1620 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1621 CTL_FLAG_DATA_IN) { 1622 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1623 "bytes to user\n", len_to_copy)); 1624 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1625 "to %p\n", kern_ptr, ext_ptr)); 1626 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1627 ctl_set_internal_failure(ctsio, 1628 /*sks_valid*/ 0, 1629 /*retry_count*/ 0); 1630 goto bailout; 1631 } 1632 } else { 1633 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1634 "bytes from user\n", len_to_copy)); 1635 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1636 "to %p\n", ext_ptr, kern_ptr)); 1637 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1638 ctl_set_internal_failure(ctsio, 1639 /*sks_valid*/ 0, 1640 /*retry_count*/0); 1641 goto bailout; 1642 } 1643 } 1644 1645 len_copied += len_to_copy; 1646 1647 if (ext_sglist[i].len == ext_watermark) { 1648 i++; 1649 ext_watermark = 0; 1650 } 1651 1652 if (kern_sglist[j].len == kern_watermark) { 1653 j++; 1654 kern_watermark = 0; 1655 } 1656 } 1657 1658 ctsio->ext_data_filled += len_copied; 1659 1660 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1661 "kern_sg_entries: %d\n", ext_sg_entries, 1662 kern_sg_entries)); 1663 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1664 "kern_data_len = %d\n", ctsio->ext_data_len, 1665 ctsio->kern_data_len)); 1666 1667 1668 /* XXX KDM set residual?? */ 1669bailout: 1670 1671 if (ext_sglist_malloced != 0) 1672 free(ext_sglist, M_CTL); 1673 1674 return (CTL_RETVAL_COMPLETE); 1675} 1676 1677/* 1678 * Serialize a command that went down the "wrong" side, and so was sent to 1679 * this controller for execution. The logic is a little different than the 1680 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1681 * sent back to the other side, but in the success case, we execute the 1682 * command on this side (XFER mode) or tell the other side to execute it 1683 * (SER_ONLY mode). 1684 */ 1685static int 1686ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock) 1687{ 1688 struct ctl_softc *ctl_softc; 1689 union ctl_ha_msg msg_info; 1690 struct ctl_lun *lun; 1691 int retval = 0; 1692 uint32_t targ_lun; 1693 1694 ctl_softc = control_softc; 1695 if (have_lock == 0) 1696 mtx_lock(&ctl_softc->ctl_lock); 1697 1698 targ_lun = ctsio->io_hdr.nexus.targ_lun; 1699 if (ctsio->io_hdr.nexus.lun_map_fn != NULL) 1700 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun); 1701 lun = ctl_softc->ctl_luns[targ_lun]; 1702 if (lun==NULL) 1703 { 1704 /* 1705 * Why isn't LUN defined? The other side wouldn't 1706 * send a cmd if the LUN is undefined. 1707 */ 1708 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1709 1710 /* "Logical unit not supported" */ 1711 ctl_set_sense_data(&msg_info.scsi.sense_data, 1712 lun, 1713 /*sense_format*/SSD_TYPE_NONE, 1714 /*current_error*/ 1, 1715 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1716 /*asc*/ 0x25, 1717 /*ascq*/ 0x00, 1718 SSD_ELEM_NONE); 1719 1720 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1721 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1722 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1723 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1724 msg_info.hdr.serializing_sc = NULL; 1725 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1726 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1727 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1728 } 1729 if (have_lock == 0) 1730 mtx_unlock(&ctl_softc->ctl_lock); 1731 return(1); 1732 1733 } 1734 1735 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1736 1737 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1738 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1739 ooa_links))) { 1740 case CTL_ACTION_BLOCK: 1741 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1742 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1743 blocked_links); 1744 break; 1745 case CTL_ACTION_PASS: 1746 case CTL_ACTION_SKIP: 1747 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1748 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1749 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 1750 &ctsio->io_hdr, links); 1751 } else { 1752 1753 /* send msg back to other side */ 1754 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1755 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1756 msg_info.hdr.msg_type = CTL_MSG_R2R; 1757#if 0 1758 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1759#endif 1760 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1761 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1762 } 1763 } 1764 break; 1765 case CTL_ACTION_OVERLAP: 1766 /* OVERLAPPED COMMANDS ATTEMPTED */ 1767 ctl_set_sense_data(&msg_info.scsi.sense_data, 1768 lun, 1769 /*sense_format*/SSD_TYPE_NONE, 1770 /*current_error*/ 1, 1771 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1772 /*asc*/ 0x4E, 1773 /*ascq*/ 0x00, 1774 SSD_ELEM_NONE); 1775 1776 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1777 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1778 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1779 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1780 msg_info.hdr.serializing_sc = NULL; 1781 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1782#if 0 1783 printf("BAD JUJU:Major Bummer Overlap\n"); 1784#endif 1785 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1786 retval = 1; 1787 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1788 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1789 } 1790 break; 1791 case CTL_ACTION_OVERLAP_TAG: 1792 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1793 ctl_set_sense_data(&msg_info.scsi.sense_data, 1794 lun, 1795 /*sense_format*/SSD_TYPE_NONE, 1796 /*current_error*/ 1, 1797 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1798 /*asc*/ 0x4D, 1799 /*ascq*/ ctsio->tag_num & 0xff, 1800 SSD_ELEM_NONE); 1801 1802 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1803 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1804 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1805 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1806 msg_info.hdr.serializing_sc = NULL; 1807 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1808#if 0 1809 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1810#endif 1811 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1812 retval = 1; 1813 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1814 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1815 } 1816 break; 1817 case CTL_ACTION_ERROR: 1818 default: 1819 /* "Internal target failure" */ 1820 ctl_set_sense_data(&msg_info.scsi.sense_data, 1821 lun, 1822 /*sense_format*/SSD_TYPE_NONE, 1823 /*current_error*/ 1, 1824 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1825 /*asc*/ 0x44, 1826 /*ascq*/ 0x00, 1827 SSD_ELEM_NONE); 1828 1829 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1830 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1831 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1832 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1833 msg_info.hdr.serializing_sc = NULL; 1834 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1835#if 0 1836 printf("BAD JUJU:Major Bummer HW Error\n"); 1837#endif 1838 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1839 retval = 1; 1840 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1841 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1842 } 1843 break; 1844 } 1845 if (have_lock == 0) 1846 mtx_unlock(&ctl_softc->ctl_lock); 1847 return (retval); 1848} 1849 1850static int 1851ctl_ioctl_submit_wait(union ctl_io *io) 1852{ 1853 struct ctl_fe_ioctl_params params; 1854 ctl_fe_ioctl_state last_state; 1855 int done, retval; 1856 1857 retval = 0; 1858 1859 bzero(¶ms, sizeof(params)); 1860 1861 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1862 cv_init(¶ms.sem, "ctlioccv"); 1863 params.state = CTL_IOCTL_INPROG; 1864 last_state = params.state; 1865 1866 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1867 1868 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1869 1870 /* This shouldn't happen */ 1871 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1872 return (retval); 1873 1874 done = 0; 1875 1876 do { 1877 mtx_lock(¶ms.ioctl_mtx); 1878 /* 1879 * Check the state here, and don't sleep if the state has 1880 * already changed (i.e. wakeup has already occured, but we 1881 * weren't waiting yet). 1882 */ 1883 if (params.state == last_state) { 1884 /* XXX KDM cv_wait_sig instead? */ 1885 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1886 } 1887 last_state = params.state; 1888 1889 switch (params.state) { 1890 case CTL_IOCTL_INPROG: 1891 /* Why did we wake up? */ 1892 /* XXX KDM error here? */ 1893 mtx_unlock(¶ms.ioctl_mtx); 1894 break; 1895 case CTL_IOCTL_DATAMOVE: 1896 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1897 1898 /* 1899 * change last_state back to INPROG to avoid 1900 * deadlock on subsequent data moves. 1901 */ 1902 params.state = last_state = CTL_IOCTL_INPROG; 1903 1904 mtx_unlock(¶ms.ioctl_mtx); 1905 ctl_ioctl_do_datamove(&io->scsiio); 1906 /* 1907 * Note that in some cases, most notably writes, 1908 * this will queue the I/O and call us back later. 1909 * In other cases, generally reads, this routine 1910 * will immediately call back and wake us up, 1911 * probably using our own context. 1912 */ 1913 io->scsiio.be_move_done(io); 1914 break; 1915 case CTL_IOCTL_DONE: 1916 mtx_unlock(¶ms.ioctl_mtx); 1917 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1918 done = 1; 1919 break; 1920 default: 1921 mtx_unlock(¶ms.ioctl_mtx); 1922 /* XXX KDM error here? */ 1923 break; 1924 } 1925 } while (done == 0); 1926 1927 mtx_destroy(¶ms.ioctl_mtx); 1928 cv_destroy(¶ms.sem); 1929 1930 return (CTL_RETVAL_COMPLETE); 1931} 1932 1933static void 1934ctl_ioctl_datamove(union ctl_io *io) 1935{ 1936 struct ctl_fe_ioctl_params *params; 1937 1938 params = (struct ctl_fe_ioctl_params *) 1939 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1940 1941 mtx_lock(¶ms->ioctl_mtx); 1942 params->state = CTL_IOCTL_DATAMOVE; 1943 cv_broadcast(¶ms->sem); 1944 mtx_unlock(¶ms->ioctl_mtx); 1945} 1946 1947static void 1948ctl_ioctl_done(union ctl_io *io) 1949{ 1950 struct ctl_fe_ioctl_params *params; 1951 1952 params = (struct ctl_fe_ioctl_params *) 1953 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1954 1955 mtx_lock(¶ms->ioctl_mtx); 1956 params->state = CTL_IOCTL_DONE; 1957 cv_broadcast(¶ms->sem); 1958 mtx_unlock(¶ms->ioctl_mtx); 1959} 1960 1961static void 1962ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 1963{ 1964 struct ctl_fe_ioctl_startstop_info *sd_info; 1965 1966 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 1967 1968 sd_info->hs_info.status = metatask->status; 1969 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 1970 sd_info->hs_info.luns_complete = 1971 metatask->taskinfo.startstop.luns_complete; 1972 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 1973 1974 cv_broadcast(&sd_info->sem); 1975} 1976 1977static void 1978ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 1979{ 1980 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 1981 1982 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 1983 1984 mtx_lock(fe_bbr_info->lock); 1985 fe_bbr_info->bbr_info->status = metatask->status; 1986 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 1987 fe_bbr_info->wakeup_done = 1; 1988 mtx_unlock(fe_bbr_info->lock); 1989 1990 cv_broadcast(&fe_bbr_info->sem); 1991} 1992 1993/* 1994 * Returns 0 for success, errno for failure. 1995 */ 1996static int 1997ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1998 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1999{ 2000 union ctl_io *io; 2001 int retval; 2002 2003 retval = 0; 2004 2005 mtx_assert(&control_softc->ctl_lock, MA_OWNED); 2006 2007 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2008 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2009 ooa_links)) { 2010 struct ctl_ooa_entry *entry; 2011 2012 /* 2013 * If we've got more than we can fit, just count the 2014 * remaining entries. 2015 */ 2016 if (*cur_fill_num >= ooa_hdr->alloc_num) 2017 continue; 2018 2019 entry = &kern_entries[*cur_fill_num]; 2020 2021 entry->tag_num = io->scsiio.tag_num; 2022 entry->lun_num = lun->lun; 2023#ifdef CTL_TIME_IO 2024 entry->start_bt = io->io_hdr.start_bt; 2025#endif 2026 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2027 entry->cdb_len = io->scsiio.cdb_len; 2028 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2029 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2030 2031 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2032 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2033 2034 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2035 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2036 2037 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2038 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2039 2040 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2041 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2042 } 2043 2044 return (retval); 2045} 2046 2047static void * 2048ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2049 size_t error_str_len) 2050{ 2051 void *kptr; 2052 2053 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2054 2055 if (copyin(user_addr, kptr, len) != 0) { 2056 snprintf(error_str, error_str_len, "Error copying %d bytes " 2057 "from user address %p to kernel address %p", len, 2058 user_addr, kptr); 2059 free(kptr, M_CTL); 2060 return (NULL); 2061 } 2062 2063 return (kptr); 2064} 2065 2066static void 2067ctl_free_args(int num_be_args, struct ctl_be_arg *be_args) 2068{ 2069 int i; 2070 2071 if (be_args == NULL) 2072 return; 2073 2074 for (i = 0; i < num_be_args; i++) { 2075 free(be_args[i].kname, M_CTL); 2076 free(be_args[i].kvalue, M_CTL); 2077 } 2078 2079 free(be_args, M_CTL); 2080} 2081 2082static struct ctl_be_arg * 2083ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args, 2084 char *error_str, size_t error_str_len) 2085{ 2086 struct ctl_be_arg *args; 2087 int i; 2088 2089 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args), 2090 error_str, error_str_len); 2091 2092 if (args == NULL) 2093 goto bailout; 2094 2095 for (i = 0; i < num_be_args; i++) { 2096 args[i].kname = NULL; 2097 args[i].kvalue = NULL; 2098 } 2099 2100 for (i = 0; i < num_be_args; i++) { 2101 uint8_t *tmpptr; 2102 2103 args[i].kname = ctl_copyin_alloc(args[i].name, 2104 args[i].namelen, error_str, error_str_len); 2105 if (args[i].kname == NULL) 2106 goto bailout; 2107 2108 if (args[i].kname[args[i].namelen - 1] != '\0') { 2109 snprintf(error_str, error_str_len, "Argument %d " 2110 "name is not NUL-terminated", i); 2111 goto bailout; 2112 } 2113 2114 args[i].kvalue = NULL; 2115 2116 tmpptr = ctl_copyin_alloc(args[i].value, 2117 args[i].vallen, error_str, error_str_len); 2118 if (tmpptr == NULL) 2119 goto bailout; 2120 2121 args[i].kvalue = tmpptr; 2122 2123 if ((args[i].flags & CTL_BEARG_ASCII) 2124 && (tmpptr[args[i].vallen - 1] != '\0')) { 2125 snprintf(error_str, error_str_len, "Argument %d " 2126 "value is not NUL-terminated", i); 2127 goto bailout; 2128 } 2129 } 2130 2131 return (args); 2132bailout: 2133 2134 ctl_free_args(num_be_args, args); 2135 2136 return (NULL); 2137} 2138 2139/* 2140 * Escape characters that are illegal or not recommended in XML. 2141 */ 2142int 2143ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2144{ 2145 int retval; 2146 2147 retval = 0; 2148 2149 for (; *str; str++) { 2150 switch (*str) { 2151 case '&': 2152 retval = sbuf_printf(sb, "&"); 2153 break; 2154 case '>': 2155 retval = sbuf_printf(sb, ">"); 2156 break; 2157 case '<': 2158 retval = sbuf_printf(sb, "<"); 2159 break; 2160 default: 2161 retval = sbuf_putc(sb, *str); 2162 break; 2163 } 2164 2165 if (retval != 0) 2166 break; 2167 2168 } 2169 2170 return (retval); 2171} 2172 2173static int 2174ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2175 struct thread *td) 2176{ 2177 struct ctl_softc *softc; 2178 int retval; 2179 2180 softc = control_softc; 2181 2182 retval = 0; 2183 2184 switch (cmd) { 2185 case CTL_IO: { 2186 union ctl_io *io; 2187 void *pool_tmp; 2188 2189 /* 2190 * If we haven't been "enabled", don't allow any SCSI I/O 2191 * to this FETD. 2192 */ 2193 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2194 retval = EPERM; 2195 break; 2196 } 2197 2198 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref); 2199 if (io == NULL) { 2200 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2201 retval = ENOSPC; 2202 break; 2203 } 2204 2205 /* 2206 * Need to save the pool reference so it doesn't get 2207 * spammed by the user's ctl_io. 2208 */ 2209 pool_tmp = io->io_hdr.pool; 2210 2211 memcpy(io, (void *)addr, sizeof(*io)); 2212 2213 io->io_hdr.pool = pool_tmp; 2214 /* 2215 * No status yet, so make sure the status is set properly. 2216 */ 2217 io->io_hdr.status = CTL_STATUS_NONE; 2218 2219 /* 2220 * The user sets the initiator ID, target and LUN IDs. 2221 */ 2222 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port; 2223 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2224 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2225 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2226 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2227 2228 retval = ctl_ioctl_submit_wait(io); 2229 2230 if (retval != 0) { 2231 ctl_free_io(io); 2232 break; 2233 } 2234 2235 memcpy((void *)addr, io, sizeof(*io)); 2236 2237 /* return this to our pool */ 2238 ctl_free_io(io); 2239 2240 break; 2241 } 2242 case CTL_ENABLE_PORT: 2243 case CTL_DISABLE_PORT: 2244 case CTL_SET_PORT_WWNS: { 2245 struct ctl_frontend *fe; 2246 struct ctl_port_entry *entry; 2247 2248 entry = (struct ctl_port_entry *)addr; 2249 2250 mtx_lock(&softc->ctl_lock); 2251 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2252 int action, done; 2253 2254 action = 0; 2255 done = 0; 2256 2257 if ((entry->port_type == CTL_PORT_NONE) 2258 && (entry->targ_port == fe->targ_port)) { 2259 /* 2260 * If the user only wants to enable or 2261 * disable or set WWNs on a specific port, 2262 * do the operation and we're done. 2263 */ 2264 action = 1; 2265 done = 1; 2266 } else if (entry->port_type & fe->port_type) { 2267 /* 2268 * Compare the user's type mask with the 2269 * particular frontend type to see if we 2270 * have a match. 2271 */ 2272 action = 1; 2273 done = 0; 2274 2275 /* 2276 * Make sure the user isn't trying to set 2277 * WWNs on multiple ports at the same time. 2278 */ 2279 if (cmd == CTL_SET_PORT_WWNS) { 2280 printf("%s: Can't set WWNs on " 2281 "multiple ports\n", __func__); 2282 retval = EINVAL; 2283 break; 2284 } 2285 } 2286 if (action != 0) { 2287 /* 2288 * XXX KDM we have to drop the lock here, 2289 * because the online/offline operations 2290 * can potentially block. We need to 2291 * reference count the frontends so they 2292 * can't go away, 2293 */ 2294 mtx_unlock(&softc->ctl_lock); 2295 2296 if (cmd == CTL_ENABLE_PORT) { 2297 struct ctl_lun *lun; 2298 2299 STAILQ_FOREACH(lun, &softc->lun_list, 2300 links) { 2301 fe->lun_enable(fe->targ_lun_arg, 2302 lun->target, 2303 lun->lun); 2304 } 2305 2306 ctl_frontend_online(fe); 2307 } else if (cmd == CTL_DISABLE_PORT) { 2308 struct ctl_lun *lun; 2309 2310 ctl_frontend_offline(fe); 2311 2312 STAILQ_FOREACH(lun, &softc->lun_list, 2313 links) { 2314 fe->lun_disable( 2315 fe->targ_lun_arg, 2316 lun->target, 2317 lun->lun); 2318 } 2319 } 2320 2321 mtx_lock(&softc->ctl_lock); 2322 2323 if (cmd == CTL_SET_PORT_WWNS) 2324 ctl_frontend_set_wwns(fe, 2325 (entry->flags & CTL_PORT_WWNN_VALID) ? 2326 1 : 0, entry->wwnn, 2327 (entry->flags & CTL_PORT_WWPN_VALID) ? 2328 1 : 0, entry->wwpn); 2329 } 2330 if (done != 0) 2331 break; 2332 } 2333 mtx_unlock(&softc->ctl_lock); 2334 break; 2335 } 2336 case CTL_GET_PORT_LIST: { 2337 struct ctl_frontend *fe; 2338 struct ctl_port_list *list; 2339 int i; 2340 2341 list = (struct ctl_port_list *)addr; 2342 2343 if (list->alloc_len != (list->alloc_num * 2344 sizeof(struct ctl_port_entry))) { 2345 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2346 "alloc_num %u * sizeof(struct ctl_port_entry) " 2347 "%zu\n", __func__, list->alloc_len, 2348 list->alloc_num, sizeof(struct ctl_port_entry)); 2349 retval = EINVAL; 2350 break; 2351 } 2352 list->fill_len = 0; 2353 list->fill_num = 0; 2354 list->dropped_num = 0; 2355 i = 0; 2356 mtx_lock(&softc->ctl_lock); 2357 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2358 struct ctl_port_entry entry, *list_entry; 2359 2360 if (list->fill_num >= list->alloc_num) { 2361 list->dropped_num++; 2362 continue; 2363 } 2364 2365 entry.port_type = fe->port_type; 2366 strlcpy(entry.port_name, fe->port_name, 2367 sizeof(entry.port_name)); 2368 entry.targ_port = fe->targ_port; 2369 entry.physical_port = fe->physical_port; 2370 entry.virtual_port = fe->virtual_port; 2371 entry.wwnn = fe->wwnn; 2372 entry.wwpn = fe->wwpn; 2373 if (fe->status & CTL_PORT_STATUS_ONLINE) 2374 entry.online = 1; 2375 else 2376 entry.online = 0; 2377 2378 list_entry = &list->entries[i]; 2379 2380 retval = copyout(&entry, list_entry, sizeof(entry)); 2381 if (retval != 0) { 2382 printf("%s: CTL_GET_PORT_LIST: copyout " 2383 "returned %d\n", __func__, retval); 2384 break; 2385 } 2386 i++; 2387 list->fill_num++; 2388 list->fill_len += sizeof(entry); 2389 } 2390 mtx_unlock(&softc->ctl_lock); 2391 2392 /* 2393 * If this is non-zero, we had a copyout fault, so there's 2394 * probably no point in attempting to set the status inside 2395 * the structure. 2396 */ 2397 if (retval != 0) 2398 break; 2399 2400 if (list->dropped_num > 0) 2401 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2402 else 2403 list->status = CTL_PORT_LIST_OK; 2404 break; 2405 } 2406 case CTL_DUMP_OOA: { 2407 struct ctl_lun *lun; 2408 union ctl_io *io; 2409 char printbuf[128]; 2410 struct sbuf sb; 2411 2412 mtx_lock(&softc->ctl_lock); 2413 printf("Dumping OOA queues:\n"); 2414 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2415 for (io = (union ctl_io *)TAILQ_FIRST( 2416 &lun->ooa_queue); io != NULL; 2417 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2418 ooa_links)) { 2419 sbuf_new(&sb, printbuf, sizeof(printbuf), 2420 SBUF_FIXEDLEN); 2421 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2422 (intmax_t)lun->lun, 2423 io->scsiio.tag_num, 2424 (io->io_hdr.flags & 2425 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2426 (io->io_hdr.flags & 2427 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2428 (io->io_hdr.flags & 2429 CTL_FLAG_ABORT) ? " ABORT" : "", 2430 (io->io_hdr.flags & 2431 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2432 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2433 sbuf_finish(&sb); 2434 printf("%s\n", sbuf_data(&sb)); 2435 } 2436 } 2437 printf("OOA queues dump done\n"); 2438 mtx_unlock(&softc->ctl_lock); 2439 break; 2440 } 2441 case CTL_GET_OOA: { 2442 struct ctl_lun *lun; 2443 struct ctl_ooa *ooa_hdr; 2444 struct ctl_ooa_entry *entries; 2445 uint32_t cur_fill_num; 2446 2447 ooa_hdr = (struct ctl_ooa *)addr; 2448 2449 if ((ooa_hdr->alloc_len == 0) 2450 || (ooa_hdr->alloc_num == 0)) { 2451 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2452 "must be non-zero\n", __func__, 2453 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2454 retval = EINVAL; 2455 break; 2456 } 2457 2458 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2459 sizeof(struct ctl_ooa_entry))) { 2460 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2461 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2462 __func__, ooa_hdr->alloc_len, 2463 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2464 retval = EINVAL; 2465 break; 2466 } 2467 2468 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2469 if (entries == NULL) { 2470 printf("%s: could not allocate %d bytes for OOA " 2471 "dump\n", __func__, ooa_hdr->alloc_len); 2472 retval = ENOMEM; 2473 break; 2474 } 2475 2476 mtx_lock(&softc->ctl_lock); 2477 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2478 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2479 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2480 mtx_unlock(&softc->ctl_lock); 2481 free(entries, M_CTL); 2482 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2483 __func__, (uintmax_t)ooa_hdr->lun_num); 2484 retval = EINVAL; 2485 break; 2486 } 2487 2488 cur_fill_num = 0; 2489 2490 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2491 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2492 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2493 ooa_hdr, entries); 2494 if (retval != 0) 2495 break; 2496 } 2497 if (retval != 0) { 2498 mtx_unlock(&softc->ctl_lock); 2499 free(entries, M_CTL); 2500 break; 2501 } 2502 } else { 2503 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2504 2505 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2506 entries); 2507 } 2508 mtx_unlock(&softc->ctl_lock); 2509 2510 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2511 ooa_hdr->fill_len = ooa_hdr->fill_num * 2512 sizeof(struct ctl_ooa_entry); 2513 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2514 if (retval != 0) { 2515 printf("%s: error copying out %d bytes for OOA dump\n", 2516 __func__, ooa_hdr->fill_len); 2517 } 2518 2519 getbintime(&ooa_hdr->cur_bt); 2520 2521 if (cur_fill_num > ooa_hdr->alloc_num) { 2522 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2523 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2524 } else { 2525 ooa_hdr->dropped_num = 0; 2526 ooa_hdr->status = CTL_OOA_OK; 2527 } 2528 2529 free(entries, M_CTL); 2530 break; 2531 } 2532 case CTL_CHECK_OOA: { 2533 union ctl_io *io; 2534 struct ctl_lun *lun; 2535 struct ctl_ooa_info *ooa_info; 2536 2537 2538 ooa_info = (struct ctl_ooa_info *)addr; 2539 2540 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2541 ooa_info->status = CTL_OOA_INVALID_LUN; 2542 break; 2543 } 2544 mtx_lock(&softc->ctl_lock); 2545 lun = softc->ctl_luns[ooa_info->lun_id]; 2546 if (lun == NULL) { 2547 mtx_unlock(&softc->ctl_lock); 2548 ooa_info->status = CTL_OOA_INVALID_LUN; 2549 break; 2550 } 2551 2552 ooa_info->num_entries = 0; 2553 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2554 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2555 &io->io_hdr, ooa_links)) { 2556 ooa_info->num_entries++; 2557 } 2558 2559 mtx_unlock(&softc->ctl_lock); 2560 ooa_info->status = CTL_OOA_SUCCESS; 2561 2562 break; 2563 } 2564 case CTL_HARD_START: 2565 case CTL_HARD_STOP: { 2566 struct ctl_fe_ioctl_startstop_info ss_info; 2567 struct cfi_metatask *metatask; 2568 struct mtx hs_mtx; 2569 2570 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2571 2572 cv_init(&ss_info.sem, "hard start/stop cv" ); 2573 2574 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2575 if (metatask == NULL) { 2576 retval = ENOMEM; 2577 mtx_destroy(&hs_mtx); 2578 break; 2579 } 2580 2581 if (cmd == CTL_HARD_START) 2582 metatask->tasktype = CFI_TASK_STARTUP; 2583 else 2584 metatask->tasktype = CFI_TASK_SHUTDOWN; 2585 2586 metatask->callback = ctl_ioctl_hard_startstop_callback; 2587 metatask->callback_arg = &ss_info; 2588 2589 cfi_action(metatask); 2590 2591 /* Wait for the callback */ 2592 mtx_lock(&hs_mtx); 2593 cv_wait_sig(&ss_info.sem, &hs_mtx); 2594 mtx_unlock(&hs_mtx); 2595 2596 /* 2597 * All information has been copied from the metatask by the 2598 * time cv_broadcast() is called, so we free the metatask here. 2599 */ 2600 cfi_free_metatask(metatask); 2601 2602 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2603 2604 mtx_destroy(&hs_mtx); 2605 break; 2606 } 2607 case CTL_BBRREAD: { 2608 struct ctl_bbrread_info *bbr_info; 2609 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2610 struct mtx bbr_mtx; 2611 struct cfi_metatask *metatask; 2612 2613 bbr_info = (struct ctl_bbrread_info *)addr; 2614 2615 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2616 2617 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2618 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2619 2620 fe_bbr_info.bbr_info = bbr_info; 2621 fe_bbr_info.lock = &bbr_mtx; 2622 2623 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2624 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2625 2626 if (metatask == NULL) { 2627 mtx_destroy(&bbr_mtx); 2628 cv_destroy(&fe_bbr_info.sem); 2629 retval = ENOMEM; 2630 break; 2631 } 2632 metatask->tasktype = CFI_TASK_BBRREAD; 2633 metatask->callback = ctl_ioctl_bbrread_callback; 2634 metatask->callback_arg = &fe_bbr_info; 2635 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2636 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2637 metatask->taskinfo.bbrread.len = bbr_info->len; 2638 2639 cfi_action(metatask); 2640 2641 mtx_lock(&bbr_mtx); 2642 while (fe_bbr_info.wakeup_done == 0) 2643 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2644 mtx_unlock(&bbr_mtx); 2645 2646 bbr_info->status = metatask->status; 2647 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2648 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2649 memcpy(&bbr_info->sense_data, 2650 &metatask->taskinfo.bbrread.sense_data, 2651 ctl_min(sizeof(bbr_info->sense_data), 2652 sizeof(metatask->taskinfo.bbrread.sense_data))); 2653 2654 cfi_free_metatask(metatask); 2655 2656 mtx_destroy(&bbr_mtx); 2657 cv_destroy(&fe_bbr_info.sem); 2658 2659 break; 2660 } 2661 case CTL_DELAY_IO: { 2662 struct ctl_io_delay_info *delay_info; 2663#ifdef CTL_IO_DELAY 2664 struct ctl_lun *lun; 2665#endif /* CTL_IO_DELAY */ 2666 2667 delay_info = (struct ctl_io_delay_info *)addr; 2668 2669#ifdef CTL_IO_DELAY 2670 mtx_lock(&softc->ctl_lock); 2671 2672 if ((delay_info->lun_id > CTL_MAX_LUNS) 2673 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2674 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2675 } else { 2676 lun = softc->ctl_luns[delay_info->lun_id]; 2677 2678 delay_info->status = CTL_DELAY_STATUS_OK; 2679 2680 switch (delay_info->delay_type) { 2681 case CTL_DELAY_TYPE_CONT: 2682 break; 2683 case CTL_DELAY_TYPE_ONESHOT: 2684 break; 2685 default: 2686 delay_info->status = 2687 CTL_DELAY_STATUS_INVALID_TYPE; 2688 break; 2689 } 2690 2691 switch (delay_info->delay_loc) { 2692 case CTL_DELAY_LOC_DATAMOVE: 2693 lun->delay_info.datamove_type = 2694 delay_info->delay_type; 2695 lun->delay_info.datamove_delay = 2696 delay_info->delay_secs; 2697 break; 2698 case CTL_DELAY_LOC_DONE: 2699 lun->delay_info.done_type = 2700 delay_info->delay_type; 2701 lun->delay_info.done_delay = 2702 delay_info->delay_secs; 2703 break; 2704 default: 2705 delay_info->status = 2706 CTL_DELAY_STATUS_INVALID_LOC; 2707 break; 2708 } 2709 } 2710 2711 mtx_unlock(&softc->ctl_lock); 2712#else 2713 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2714#endif /* CTL_IO_DELAY */ 2715 break; 2716 } 2717 case CTL_REALSYNC_SET: { 2718 int *syncstate; 2719 2720 syncstate = (int *)addr; 2721 2722 mtx_lock(&softc->ctl_lock); 2723 switch (*syncstate) { 2724 case 0: 2725 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2726 break; 2727 case 1: 2728 softc->flags |= CTL_FLAG_REAL_SYNC; 2729 break; 2730 default: 2731 retval = EINVAL; 2732 break; 2733 } 2734 mtx_unlock(&softc->ctl_lock); 2735 break; 2736 } 2737 case CTL_REALSYNC_GET: { 2738 int *syncstate; 2739 2740 syncstate = (int*)addr; 2741 2742 mtx_lock(&softc->ctl_lock); 2743 if (softc->flags & CTL_FLAG_REAL_SYNC) 2744 *syncstate = 1; 2745 else 2746 *syncstate = 0; 2747 mtx_unlock(&softc->ctl_lock); 2748 2749 break; 2750 } 2751 case CTL_SETSYNC: 2752 case CTL_GETSYNC: { 2753 struct ctl_sync_info *sync_info; 2754 struct ctl_lun *lun; 2755 2756 sync_info = (struct ctl_sync_info *)addr; 2757 2758 mtx_lock(&softc->ctl_lock); 2759 lun = softc->ctl_luns[sync_info->lun_id]; 2760 if (lun == NULL) { 2761 mtx_unlock(&softc->ctl_lock); 2762 sync_info->status = CTL_GS_SYNC_NO_LUN; 2763 } 2764 /* 2765 * Get or set the sync interval. We're not bounds checking 2766 * in the set case, hopefully the user won't do something 2767 * silly. 2768 */ 2769 if (cmd == CTL_GETSYNC) 2770 sync_info->sync_interval = lun->sync_interval; 2771 else 2772 lun->sync_interval = sync_info->sync_interval; 2773 2774 mtx_unlock(&softc->ctl_lock); 2775 2776 sync_info->status = CTL_GS_SYNC_OK; 2777 2778 break; 2779 } 2780 case CTL_GETSTATS: { 2781 struct ctl_stats *stats; 2782 struct ctl_lun *lun; 2783 int i; 2784 2785 stats = (struct ctl_stats *)addr; 2786 2787 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2788 stats->alloc_len) { 2789 stats->status = CTL_SS_NEED_MORE_SPACE; 2790 stats->num_luns = softc->num_luns; 2791 break; 2792 } 2793 /* 2794 * XXX KDM no locking here. If the LUN list changes, 2795 * things can blow up. 2796 */ 2797 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2798 i++, lun = STAILQ_NEXT(lun, links)) { 2799 retval = copyout(&lun->stats, &stats->lun_stats[i], 2800 sizeof(lun->stats)); 2801 if (retval != 0) 2802 break; 2803 } 2804 stats->num_luns = softc->num_luns; 2805 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2806 softc->num_luns; 2807 stats->status = CTL_SS_OK; 2808#ifdef CTL_TIME_IO 2809 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2810#else 2811 stats->flags = CTL_STATS_FLAG_NONE; 2812#endif 2813 getnanouptime(&stats->timestamp); 2814 break; 2815 } 2816 case CTL_ERROR_INJECT: { 2817 struct ctl_error_desc *err_desc, *new_err_desc; 2818 struct ctl_lun *lun; 2819 2820 err_desc = (struct ctl_error_desc *)addr; 2821 2822 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2823 M_WAITOK | M_ZERO); 2824 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2825 2826 mtx_lock(&softc->ctl_lock); 2827 lun = softc->ctl_luns[err_desc->lun_id]; 2828 if (lun == NULL) { 2829 mtx_unlock(&softc->ctl_lock); 2830 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2831 __func__, (uintmax_t)err_desc->lun_id); 2832 retval = EINVAL; 2833 break; 2834 } 2835 2836 /* 2837 * We could do some checking here to verify the validity 2838 * of the request, but given the complexity of error 2839 * injection requests, the checking logic would be fairly 2840 * complex. 2841 * 2842 * For now, if the request is invalid, it just won't get 2843 * executed and might get deleted. 2844 */ 2845 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2846 2847 /* 2848 * XXX KDM check to make sure the serial number is unique, 2849 * in case we somehow manage to wrap. That shouldn't 2850 * happen for a very long time, but it's the right thing to 2851 * do. 2852 */ 2853 new_err_desc->serial = lun->error_serial; 2854 err_desc->serial = lun->error_serial; 2855 lun->error_serial++; 2856 2857 mtx_unlock(&softc->ctl_lock); 2858 break; 2859 } 2860 case CTL_ERROR_INJECT_DELETE: { 2861 struct ctl_error_desc *delete_desc, *desc, *desc2; 2862 struct ctl_lun *lun; 2863 int delete_done; 2864 2865 delete_desc = (struct ctl_error_desc *)addr; 2866 delete_done = 0; 2867 2868 mtx_lock(&softc->ctl_lock); 2869 lun = softc->ctl_luns[delete_desc->lun_id]; 2870 if (lun == NULL) { 2871 mtx_unlock(&softc->ctl_lock); 2872 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2873 __func__, (uintmax_t)delete_desc->lun_id); 2874 retval = EINVAL; 2875 break; 2876 } 2877 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2878 if (desc->serial != delete_desc->serial) 2879 continue; 2880 2881 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2882 links); 2883 free(desc, M_CTL); 2884 delete_done = 1; 2885 } 2886 mtx_unlock(&softc->ctl_lock); 2887 if (delete_done == 0) { 2888 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2889 "error serial %ju on LUN %u\n", __func__, 2890 delete_desc->serial, delete_desc->lun_id); 2891 retval = EINVAL; 2892 break; 2893 } 2894 break; 2895 } 2896 case CTL_DUMP_STRUCTS: { 2897 int i, j, k; 2898 struct ctl_frontend *fe; 2899 2900 printf("CTL IID to WWPN map start:\n"); 2901 for (i = 0; i < CTL_MAX_PORTS; i++) { 2902 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2903 if (softc->wwpn_iid[i][j].in_use == 0) 2904 continue; 2905 2906 printf("port %d iid %u WWPN %#jx\n", 2907 softc->wwpn_iid[i][j].port, 2908 softc->wwpn_iid[i][j].iid, 2909 (uintmax_t)softc->wwpn_iid[i][j].wwpn); 2910 } 2911 } 2912 printf("CTL IID to WWPN map end\n"); 2913 printf("CTL Persistent Reservation information start:\n"); 2914 for (i = 0; i < CTL_MAX_LUNS; i++) { 2915 struct ctl_lun *lun; 2916 2917 lun = softc->ctl_luns[i]; 2918 2919 if ((lun == NULL) 2920 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2921 continue; 2922 2923 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2924 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2925 if (lun->per_res[j+k].registered == 0) 2926 continue; 2927 printf("LUN %d port %d iid %d key " 2928 "%#jx\n", i, j, k, 2929 (uintmax_t)scsi_8btou64( 2930 lun->per_res[j+k].res_key.key)); 2931 } 2932 } 2933 } 2934 printf("CTL Persistent Reservation information end\n"); 2935 printf("CTL Frontends:\n"); 2936 /* 2937 * XXX KDM calling this without a lock. We'd likely want 2938 * to drop the lock before calling the frontend's dump 2939 * routine anyway. 2940 */ 2941 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2942 printf("Frontend %s Type %u pport %d vport %d WWNN " 2943 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type, 2944 fe->physical_port, fe->virtual_port, 2945 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn); 2946 2947 /* 2948 * Frontends are not required to support the dump 2949 * routine. 2950 */ 2951 if (fe->fe_dump == NULL) 2952 continue; 2953 2954 fe->fe_dump(); 2955 } 2956 printf("CTL Frontend information end\n"); 2957 break; 2958 } 2959 case CTL_LUN_REQ: { 2960 struct ctl_lun_req *lun_req; 2961 struct ctl_backend_driver *backend; 2962 2963 lun_req = (struct ctl_lun_req *)addr; 2964 2965 backend = ctl_backend_find(lun_req->backend); 2966 if (backend == NULL) { 2967 lun_req->status = CTL_LUN_ERROR; 2968 snprintf(lun_req->error_str, 2969 sizeof(lun_req->error_str), 2970 "Backend \"%s\" not found.", 2971 lun_req->backend); 2972 break; 2973 } 2974 if (lun_req->num_be_args > 0) { 2975 lun_req->kern_be_args = ctl_copyin_args( 2976 lun_req->num_be_args, 2977 lun_req->be_args, 2978 lun_req->error_str, 2979 sizeof(lun_req->error_str)); 2980 if (lun_req->kern_be_args == NULL) { 2981 lun_req->status = CTL_LUN_ERROR; 2982 break; 2983 } 2984 } 2985 2986 retval = backend->ioctl(dev, cmd, addr, flag, td); 2987 2988 if (lun_req->num_be_args > 0) { 2989 ctl_free_args(lun_req->num_be_args, 2990 lun_req->kern_be_args); 2991 } 2992 break; 2993 } 2994 case CTL_LUN_LIST: { 2995 struct sbuf *sb; 2996 struct ctl_lun *lun; 2997 struct ctl_lun_list *list; 2998 struct ctl_be_lun_option *opt; 2999 3000 list = (struct ctl_lun_list *)addr; 3001 3002 /* 3003 * Allocate a fixed length sbuf here, based on the length 3004 * of the user's buffer. We could allocate an auto-extending 3005 * buffer, and then tell the user how much larger our 3006 * amount of data is than his buffer, but that presents 3007 * some problems: 3008 * 3009 * 1. The sbuf(9) routines use a blocking malloc, and so 3010 * we can't hold a lock while calling them with an 3011 * auto-extending buffer. 3012 * 3013 * 2. There is not currently a LUN reference counting 3014 * mechanism, outside of outstanding transactions on 3015 * the LUN's OOA queue. So a LUN could go away on us 3016 * while we're getting the LUN number, backend-specific 3017 * information, etc. Thus, given the way things 3018 * currently work, we need to hold the CTL lock while 3019 * grabbing LUN information. 3020 * 3021 * So, from the user's standpoint, the best thing to do is 3022 * allocate what he thinks is a reasonable buffer length, 3023 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3024 * double the buffer length and try again. (And repeat 3025 * that until he succeeds.) 3026 */ 3027 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3028 if (sb == NULL) { 3029 list->status = CTL_LUN_LIST_ERROR; 3030 snprintf(list->error_str, sizeof(list->error_str), 3031 "Unable to allocate %d bytes for LUN list", 3032 list->alloc_len); 3033 break; 3034 } 3035 3036 sbuf_printf(sb, "<ctllunlist>\n"); 3037 3038 mtx_lock(&softc->ctl_lock); 3039 3040 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3041 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3042 (uintmax_t)lun->lun); 3043 3044 /* 3045 * Bail out as soon as we see that we've overfilled 3046 * the buffer. 3047 */ 3048 if (retval != 0) 3049 break; 3050 3051 retval = sbuf_printf(sb, "<backend_type>%s" 3052 "</backend_type>\n", 3053 (lun->backend == NULL) ? "none" : 3054 lun->backend->name); 3055 3056 if (retval != 0) 3057 break; 3058 3059 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n", 3060 lun->be_lun->lun_type); 3061 3062 if (retval != 0) 3063 break; 3064 3065 if (lun->backend == NULL) { 3066 retval = sbuf_printf(sb, "</lun>\n"); 3067 if (retval != 0) 3068 break; 3069 continue; 3070 } 3071 3072 retval = sbuf_printf(sb, "<size>%ju</size>\n", 3073 (lun->be_lun->maxlba > 0) ? 3074 lun->be_lun->maxlba + 1 : 0); 3075 3076 if (retval != 0) 3077 break; 3078 3079 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n", 3080 lun->be_lun->blocksize); 3081 3082 if (retval != 0) 3083 break; 3084 3085 retval = sbuf_printf(sb, "<serial_number>"); 3086 3087 if (retval != 0) 3088 break; 3089 3090 retval = ctl_sbuf_printf_esc(sb, 3091 lun->be_lun->serial_num); 3092 3093 if (retval != 0) 3094 break; 3095 3096 retval = sbuf_printf(sb, "</serial_number>\n"); 3097 3098 if (retval != 0) 3099 break; 3100 3101 retval = sbuf_printf(sb, "<device_id>"); 3102 3103 if (retval != 0) 3104 break; 3105 3106 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3107 3108 if (retval != 0) 3109 break; 3110 3111 retval = sbuf_printf(sb, "</device_id>\n"); 3112 3113 if (retval != 0) 3114 break; 3115 3116 if (lun->backend->lun_info != NULL) { 3117 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3118 if (retval != 0) 3119 break; 3120 } 3121 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3122 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name); 3123 if (retval != 0) 3124 break; 3125 } 3126 3127 retval = sbuf_printf(sb, "</lun>\n"); 3128 3129 if (retval != 0) 3130 break; 3131 } 3132 mtx_unlock(&softc->ctl_lock); 3133 3134 if ((retval != 0) 3135 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3136 retval = 0; 3137 sbuf_delete(sb); 3138 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3139 snprintf(list->error_str, sizeof(list->error_str), 3140 "Out of space, %d bytes is too small", 3141 list->alloc_len); 3142 break; 3143 } 3144 3145 sbuf_finish(sb); 3146 3147 retval = copyout(sbuf_data(sb), list->lun_xml, 3148 sbuf_len(sb) + 1); 3149 3150 list->fill_len = sbuf_len(sb) + 1; 3151 list->status = CTL_LUN_LIST_OK; 3152 sbuf_delete(sb); 3153 break; 3154 } 3155 case CTL_ISCSI: { 3156 struct ctl_iscsi *ci; 3157 struct ctl_frontend *fe; 3158 3159 ci = (struct ctl_iscsi *)addr; 3160 3161 mtx_lock(&softc->ctl_lock); 3162 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3163 if (strcmp(fe->port_name, "iscsi") == 0) 3164 break; 3165 } 3166 mtx_unlock(&softc->ctl_lock); 3167 3168 if (fe == NULL) { 3169 ci->status = CTL_ISCSI_ERROR; 3170 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found."); 3171 break; 3172 } 3173 3174 retval = fe->ioctl(dev, cmd, addr, flag, td); 3175 break; 3176 } 3177 default: { 3178 /* XXX KDM should we fix this? */ 3179#if 0 3180 struct ctl_backend_driver *backend; 3181 unsigned int type; 3182 int found; 3183 3184 found = 0; 3185 3186 /* 3187 * We encode the backend type as the ioctl type for backend 3188 * ioctls. So parse it out here, and then search for a 3189 * backend of this type. 3190 */ 3191 type = _IOC_TYPE(cmd); 3192 3193 STAILQ_FOREACH(backend, &softc->be_list, links) { 3194 if (backend->type == type) { 3195 found = 1; 3196 break; 3197 } 3198 } 3199 if (found == 0) { 3200 printf("ctl: unknown ioctl command %#lx or backend " 3201 "%d\n", cmd, type); 3202 retval = EINVAL; 3203 break; 3204 } 3205 retval = backend->ioctl(dev, cmd, addr, flag, td); 3206#endif 3207 retval = ENOTTY; 3208 break; 3209 } 3210 } 3211 return (retval); 3212} 3213 3214uint32_t 3215ctl_get_initindex(struct ctl_nexus *nexus) 3216{ 3217 if (nexus->targ_port < CTL_MAX_PORTS) 3218 return (nexus->initid.id + 3219 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3220 else 3221 return (nexus->initid.id + 3222 ((nexus->targ_port - CTL_MAX_PORTS) * 3223 CTL_MAX_INIT_PER_PORT)); 3224} 3225 3226uint32_t 3227ctl_get_resindex(struct ctl_nexus *nexus) 3228{ 3229 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3230} 3231 3232uint32_t 3233ctl_port_idx(int port_num) 3234{ 3235 if (port_num < CTL_MAX_PORTS) 3236 return(port_num); 3237 else 3238 return(port_num - CTL_MAX_PORTS); 3239} 3240 3241/* 3242 * Note: This only works for bitmask sizes that are at least 32 bits, and 3243 * that are a power of 2. 3244 */ 3245int 3246ctl_ffz(uint32_t *mask, uint32_t size) 3247{ 3248 uint32_t num_chunks, num_pieces; 3249 int i, j; 3250 3251 num_chunks = (size >> 5); 3252 if (num_chunks == 0) 3253 num_chunks++; 3254 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3255 3256 for (i = 0; i < num_chunks; i++) { 3257 for (j = 0; j < num_pieces; j++) { 3258 if ((mask[i] & (1 << j)) == 0) 3259 return ((i << 5) + j); 3260 } 3261 } 3262 3263 return (-1); 3264} 3265 3266int 3267ctl_set_mask(uint32_t *mask, uint32_t bit) 3268{ 3269 uint32_t chunk, piece; 3270 3271 chunk = bit >> 5; 3272 piece = bit % (sizeof(uint32_t) * 8); 3273 3274 if ((mask[chunk] & (1 << piece)) != 0) 3275 return (-1); 3276 else 3277 mask[chunk] |= (1 << piece); 3278 3279 return (0); 3280} 3281 3282int 3283ctl_clear_mask(uint32_t *mask, uint32_t bit) 3284{ 3285 uint32_t chunk, piece; 3286 3287 chunk = bit >> 5; 3288 piece = bit % (sizeof(uint32_t) * 8); 3289 3290 if ((mask[chunk] & (1 << piece)) == 0) 3291 return (-1); 3292 else 3293 mask[chunk] &= ~(1 << piece); 3294 3295 return (0); 3296} 3297 3298int 3299ctl_is_set(uint32_t *mask, uint32_t bit) 3300{ 3301 uint32_t chunk, piece; 3302 3303 chunk = bit >> 5; 3304 piece = bit % (sizeof(uint32_t) * 8); 3305 3306 if ((mask[chunk] & (1 << piece)) == 0) 3307 return (0); 3308 else 3309 return (1); 3310} 3311 3312#ifdef unused 3313/* 3314 * The bus, target and lun are optional, they can be filled in later. 3315 * can_wait is used to determine whether we can wait on the malloc or not. 3316 */ 3317union ctl_io* 3318ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3319 uint32_t targ_lun, int can_wait) 3320{ 3321 union ctl_io *io; 3322 3323 if (can_wait) 3324 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3325 else 3326 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3327 3328 if (io != NULL) { 3329 io->io_hdr.io_type = io_type; 3330 io->io_hdr.targ_port = targ_port; 3331 /* 3332 * XXX KDM this needs to change/go away. We need to move 3333 * to a preallocated pool of ctl_scsiio structures. 3334 */ 3335 io->io_hdr.nexus.targ_target.id = targ_target; 3336 io->io_hdr.nexus.targ_lun = targ_lun; 3337 } 3338 3339 return (io); 3340} 3341 3342void 3343ctl_kfree_io(union ctl_io *io) 3344{ 3345 free(io, M_CTL); 3346} 3347#endif /* unused */ 3348 3349/* 3350 * ctl_softc, pool_type, total_ctl_io are passed in. 3351 * npool is passed out. 3352 */ 3353int 3354ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3355 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3356{ 3357 uint32_t i; 3358 union ctl_io *cur_io, *next_io; 3359 struct ctl_io_pool *pool; 3360 int retval; 3361 3362 retval = 0; 3363 3364 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3365 M_NOWAIT | M_ZERO); 3366 if (pool == NULL) { 3367 retval = ENOMEM; 3368 goto bailout; 3369 } 3370 3371 pool->type = pool_type; 3372 pool->ctl_softc = ctl_softc; 3373 3374 mtx_lock(&ctl_softc->pool_lock); 3375 pool->id = ctl_softc->cur_pool_id++; 3376 mtx_unlock(&ctl_softc->pool_lock); 3377 3378 pool->flags = CTL_POOL_FLAG_NONE; 3379 pool->refcount = 1; /* Reference for validity. */ 3380 STAILQ_INIT(&pool->free_queue); 3381 3382 /* 3383 * XXX KDM other options here: 3384 * - allocate a page at a time 3385 * - allocate one big chunk of memory. 3386 * Page allocation might work well, but would take a little more 3387 * tracking. 3388 */ 3389 for (i = 0; i < total_ctl_io; i++) { 3390 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL, 3391 M_NOWAIT); 3392 if (cur_io == NULL) { 3393 retval = ENOMEM; 3394 break; 3395 } 3396 cur_io->io_hdr.pool = pool; 3397 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3398 pool->total_ctl_io++; 3399 pool->free_ctl_io++; 3400 } 3401 3402 if (retval != 0) { 3403 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3404 cur_io != NULL; cur_io = next_io) { 3405 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3406 links); 3407 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3408 ctl_io_hdr, links); 3409 free(cur_io, M_CTL); 3410 } 3411 3412 free(pool, M_CTL); 3413 goto bailout; 3414 } 3415 mtx_lock(&ctl_softc->pool_lock); 3416 ctl_softc->num_pools++; 3417 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3418 /* 3419 * Increment our usage count if this is an external consumer, so we 3420 * can't get unloaded until the external consumer (most likely a 3421 * FETD) unloads and frees his pool. 3422 * 3423 * XXX KDM will this increment the caller's module use count, or 3424 * mine? 3425 */ 3426#if 0 3427 if ((pool_type != CTL_POOL_EMERGENCY) 3428 && (pool_type != CTL_POOL_INTERNAL) 3429 && (pool_type != CTL_POOL_IOCTL) 3430 && (pool_type != CTL_POOL_4OTHERSC)) 3431 MOD_INC_USE_COUNT; 3432#endif 3433 3434 mtx_unlock(&ctl_softc->pool_lock); 3435 3436 *npool = pool; 3437 3438bailout: 3439 3440 return (retval); 3441} 3442 3443static int 3444ctl_pool_acquire(struct ctl_io_pool *pool) 3445{ 3446 3447 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3448 3449 if (pool->flags & CTL_POOL_FLAG_INVALID) 3450 return (EINVAL); 3451 3452 pool->refcount++; 3453 3454 return (0); 3455} 3456 3457static void 3458ctl_pool_release(struct ctl_io_pool *pool) 3459{ 3460 struct ctl_softc *ctl_softc = pool->ctl_softc; 3461 union ctl_io *io; 3462 3463 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3464 3465 if (--pool->refcount != 0) 3466 return; 3467 3468 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3469 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3470 links); 3471 free(io, M_CTL); 3472 } 3473 3474 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3475 ctl_softc->num_pools--; 3476 3477 /* 3478 * XXX KDM will this decrement the caller's usage count or mine? 3479 */ 3480#if 0 3481 if ((pool->type != CTL_POOL_EMERGENCY) 3482 && (pool->type != CTL_POOL_INTERNAL) 3483 && (pool->type != CTL_POOL_IOCTL)) 3484 MOD_DEC_USE_COUNT; 3485#endif 3486 3487 free(pool, M_CTL); 3488} 3489 3490void 3491ctl_pool_free(struct ctl_io_pool *pool) 3492{ 3493 struct ctl_softc *ctl_softc; 3494 3495 if (pool == NULL) 3496 return; 3497 3498 ctl_softc = pool->ctl_softc; 3499 mtx_lock(&ctl_softc->pool_lock); 3500 pool->flags |= CTL_POOL_FLAG_INVALID; 3501 ctl_pool_release(pool); 3502 mtx_unlock(&ctl_softc->pool_lock); 3503} 3504 3505/* 3506 * This routine does not block (except for spinlocks of course). 3507 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3508 * possible. 3509 */ 3510union ctl_io * 3511ctl_alloc_io(void *pool_ref) 3512{ 3513 union ctl_io *io; 3514 struct ctl_softc *ctl_softc; 3515 struct ctl_io_pool *pool, *npool; 3516 struct ctl_io_pool *emergency_pool; 3517 3518 pool = (struct ctl_io_pool *)pool_ref; 3519 3520 if (pool == NULL) { 3521 printf("%s: pool is NULL\n", __func__); 3522 return (NULL); 3523 } 3524 3525 emergency_pool = NULL; 3526 3527 ctl_softc = pool->ctl_softc; 3528 3529 mtx_lock(&ctl_softc->pool_lock); 3530 /* 3531 * First, try to get the io structure from the user's pool. 3532 */ 3533 if (ctl_pool_acquire(pool) == 0) { 3534 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3535 if (io != NULL) { 3536 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3537 pool->total_allocated++; 3538 pool->free_ctl_io--; 3539 mtx_unlock(&ctl_softc->pool_lock); 3540 return (io); 3541 } else 3542 ctl_pool_release(pool); 3543 } 3544 /* 3545 * If he doesn't have any io structures left, search for an 3546 * emergency pool and grab one from there. 3547 */ 3548 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3549 if (npool->type != CTL_POOL_EMERGENCY) 3550 continue; 3551 3552 if (ctl_pool_acquire(npool) != 0) 3553 continue; 3554 3555 emergency_pool = npool; 3556 3557 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3558 if (io != NULL) { 3559 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3560 npool->total_allocated++; 3561 npool->free_ctl_io--; 3562 mtx_unlock(&ctl_softc->pool_lock); 3563 return (io); 3564 } else 3565 ctl_pool_release(npool); 3566 } 3567 3568 /* Drop the spinlock before we malloc */ 3569 mtx_unlock(&ctl_softc->pool_lock); 3570 3571 /* 3572 * The emergency pool (if it exists) didn't have one, so try an 3573 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3574 */ 3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3576 if (io != NULL) { 3577 /* 3578 * If the emergency pool exists but is empty, add this 3579 * ctl_io to its list when it gets freed. 3580 */ 3581 if (emergency_pool != NULL) { 3582 mtx_lock(&ctl_softc->pool_lock); 3583 if (ctl_pool_acquire(emergency_pool) == 0) { 3584 io->io_hdr.pool = emergency_pool; 3585 emergency_pool->total_ctl_io++; 3586 /* 3587 * Need to bump this, otherwise 3588 * total_allocated and total_freed won't 3589 * match when we no longer have anything 3590 * outstanding. 3591 */ 3592 emergency_pool->total_allocated++; 3593 } 3594 mtx_unlock(&ctl_softc->pool_lock); 3595 } else 3596 io->io_hdr.pool = NULL; 3597 } 3598 3599 return (io); 3600} 3601 3602void 3603ctl_free_io(union ctl_io *io) 3604{ 3605 if (io == NULL) 3606 return; 3607 3608 /* 3609 * If this ctl_io has a pool, return it to that pool. 3610 */ 3611 if (io->io_hdr.pool != NULL) { 3612 struct ctl_io_pool *pool; 3613#if 0 3614 struct ctl_softc *ctl_softc; 3615 union ctl_io *tmp_io; 3616 unsigned long xflags; 3617 int i; 3618 3619 ctl_softc = control_softc; 3620#endif 3621 3622 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3623 3624 mtx_lock(&pool->ctl_softc->pool_lock); 3625#if 0 3626 save_flags(xflags); 3627 3628 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST( 3629 &ctl_softc->task_queue); tmp_io != NULL; i++, 3630 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr, 3631 links)) { 3632 if (tmp_io == io) { 3633 printf("%s: %p is still on the task queue!\n", 3634 __func__, tmp_io); 3635 printf("%s: (%d): type %d " 3636 "msg %d cdb %x iptl: " 3637 "%d:%d:%d:%d tag 0x%04x " 3638 "flg %#lx\n", 3639 __func__, i, 3640 tmp_io->io_hdr.io_type, 3641 tmp_io->io_hdr.msg_type, 3642 tmp_io->scsiio.cdb[0], 3643 tmp_io->io_hdr.nexus.initid.id, 3644 tmp_io->io_hdr.nexus.targ_port, 3645 tmp_io->io_hdr.nexus.targ_target.id, 3646 tmp_io->io_hdr.nexus.targ_lun, 3647 (tmp_io->io_hdr.io_type == 3648 CTL_IO_TASK) ? 3649 tmp_io->taskio.tag_num : 3650 tmp_io->scsiio.tag_num, 3651 xflags); 3652 panic("I/O still on the task queue!"); 3653 } 3654 } 3655#endif 3656 io->io_hdr.io_type = 0xff; 3657 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3658 pool->total_freed++; 3659 pool->free_ctl_io++; 3660 ctl_pool_release(pool); 3661 mtx_unlock(&pool->ctl_softc->pool_lock); 3662 } else { 3663 /* 3664 * Otherwise, just free it. We probably malloced it and 3665 * the emergency pool wasn't available. 3666 */ 3667 free(io, M_CTL); 3668 } 3669 3670} 3671 3672void 3673ctl_zero_io(union ctl_io *io) 3674{ 3675 void *pool_ref; 3676 3677 if (io == NULL) 3678 return; 3679 3680 /* 3681 * May need to preserve linked list pointers at some point too. 3682 */ 3683 pool_ref = io->io_hdr.pool; 3684 3685 memset(io, 0, sizeof(*io)); 3686 3687 io->io_hdr.pool = pool_ref; 3688} 3689 3690/* 3691 * This routine is currently used for internal copies of ctl_ios that need 3692 * to persist for some reason after we've already returned status to the 3693 * FETD. (Thus the flag set.) 3694 * 3695 * XXX XXX 3696 * Note that this makes a blind copy of all fields in the ctl_io, except 3697 * for the pool reference. This includes any memory that has been 3698 * allocated! That memory will no longer be valid after done has been 3699 * called, so this would be VERY DANGEROUS for command that actually does 3700 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3701 * start and stop commands, which don't transfer any data, so this is not a 3702 * problem. If it is used for anything else, the caller would also need to 3703 * allocate data buffer space and this routine would need to be modified to 3704 * copy the data buffer(s) as well. 3705 */ 3706void 3707ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3708{ 3709 void *pool_ref; 3710 3711 if ((src == NULL) 3712 || (dest == NULL)) 3713 return; 3714 3715 /* 3716 * May need to preserve linked list pointers at some point too. 3717 */ 3718 pool_ref = dest->io_hdr.pool; 3719 3720 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3721 3722 dest->io_hdr.pool = pool_ref; 3723 /* 3724 * We need to know that this is an internal copy, and doesn't need 3725 * to get passed back to the FETD that allocated it. 3726 */ 3727 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3728} 3729 3730#ifdef NEEDTOPORT 3731static void 3732ctl_update_power_subpage(struct copan_power_subpage *page) 3733{ 3734 int num_luns, num_partitions, config_type; 3735 struct ctl_softc *softc; 3736 cs_BOOL_t aor_present, shelf_50pct_power; 3737 cs_raidset_personality_t rs_type; 3738 int max_active_luns; 3739 3740 softc = control_softc; 3741 3742 /* subtract out the processor LUN */ 3743 num_luns = softc->num_luns - 1; 3744 /* 3745 * Default to 7 LUNs active, which was the only number we allowed 3746 * in the past. 3747 */ 3748 max_active_luns = 7; 3749 3750 num_partitions = config_GetRsPartitionInfo(); 3751 config_type = config_GetConfigType(); 3752 shelf_50pct_power = config_GetShelfPowerMode(); 3753 aor_present = config_IsAorRsPresent(); 3754 3755 rs_type = ddb_GetRsRaidType(1); 3756 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3757 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3758 EPRINT(0, "Unsupported RS type %d!", rs_type); 3759 } 3760 3761 3762 page->total_luns = num_luns; 3763 3764 switch (config_type) { 3765 case 40: 3766 /* 3767 * In a 40 drive configuration, it doesn't matter what DC 3768 * cards we have, whether we have AOR enabled or not, 3769 * partitioning or not, or what type of RAIDset we have. 3770 * In that scenario, we can power up every LUN we present 3771 * to the user. 3772 */ 3773 max_active_luns = num_luns; 3774 3775 break; 3776 case 64: 3777 if (shelf_50pct_power == CS_FALSE) { 3778 /* 25% power */ 3779 if (aor_present == CS_TRUE) { 3780 if (rs_type == 3781 CS_RAIDSET_PERSONALITY_RAID5) { 3782 max_active_luns = 7; 3783 } else if (rs_type == 3784 CS_RAIDSET_PERSONALITY_RAID1){ 3785 max_active_luns = 14; 3786 } else { 3787 /* XXX KDM now what?? */ 3788 } 3789 } else { 3790 if (rs_type == 3791 CS_RAIDSET_PERSONALITY_RAID5) { 3792 max_active_luns = 8; 3793 } else if (rs_type == 3794 CS_RAIDSET_PERSONALITY_RAID1){ 3795 max_active_luns = 16; 3796 } else { 3797 /* XXX KDM now what?? */ 3798 } 3799 } 3800 } else { 3801 /* 50% power */ 3802 /* 3803 * With 50% power in a 64 drive configuration, we 3804 * can power all LUNs we present. 3805 */ 3806 max_active_luns = num_luns; 3807 } 3808 break; 3809 case 112: 3810 if (shelf_50pct_power == CS_FALSE) { 3811 /* 25% power */ 3812 if (aor_present == CS_TRUE) { 3813 if (rs_type == 3814 CS_RAIDSET_PERSONALITY_RAID5) { 3815 max_active_luns = 7; 3816 } else if (rs_type == 3817 CS_RAIDSET_PERSONALITY_RAID1){ 3818 max_active_luns = 14; 3819 } else { 3820 /* XXX KDM now what?? */ 3821 } 3822 } else { 3823 if (rs_type == 3824 CS_RAIDSET_PERSONALITY_RAID5) { 3825 max_active_luns = 8; 3826 } else if (rs_type == 3827 CS_RAIDSET_PERSONALITY_RAID1){ 3828 max_active_luns = 16; 3829 } else { 3830 /* XXX KDM now what?? */ 3831 } 3832 } 3833 } else { 3834 /* 50% power */ 3835 if (aor_present == CS_TRUE) { 3836 if (rs_type == 3837 CS_RAIDSET_PERSONALITY_RAID5) { 3838 max_active_luns = 14; 3839 } else if (rs_type == 3840 CS_RAIDSET_PERSONALITY_RAID1){ 3841 /* 3842 * We're assuming here that disk 3843 * caching is enabled, and so we're 3844 * able to power up half of each 3845 * LUN, and cache all writes. 3846 */ 3847 max_active_luns = num_luns; 3848 } else { 3849 /* XXX KDM now what?? */ 3850 } 3851 } else { 3852 if (rs_type == 3853 CS_RAIDSET_PERSONALITY_RAID5) { 3854 max_active_luns = 15; 3855 } else if (rs_type == 3856 CS_RAIDSET_PERSONALITY_RAID1){ 3857 max_active_luns = 30; 3858 } else { 3859 /* XXX KDM now what?? */ 3860 } 3861 } 3862 } 3863 break; 3864 default: 3865 /* 3866 * In this case, we have an unknown configuration, so we 3867 * just use the default from above. 3868 */ 3869 break; 3870 } 3871 3872 page->max_active_luns = max_active_luns; 3873#if 0 3874 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 3875 page->total_luns, page->max_active_luns); 3876#endif 3877} 3878#endif /* NEEDTOPORT */ 3879 3880/* 3881 * This routine could be used in the future to load default and/or saved 3882 * mode page parameters for a particuar lun. 3883 */ 3884static int 3885ctl_init_page_index(struct ctl_lun *lun) 3886{ 3887 int i; 3888 struct ctl_page_index *page_index; 3889 struct ctl_softc *softc; 3890 3891 memcpy(&lun->mode_pages.index, page_index_template, 3892 sizeof(page_index_template)); 3893 3894 softc = lun->ctl_softc; 3895 3896 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3897 3898 page_index = &lun->mode_pages.index[i]; 3899 /* 3900 * If this is a disk-only mode page, there's no point in 3901 * setting it up. For some pages, we have to have some 3902 * basic information about the disk in order to calculate the 3903 * mode page data. 3904 */ 3905 if ((lun->be_lun->lun_type != T_DIRECT) 3906 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3907 continue; 3908 3909 switch (page_index->page_code & SMPH_PC_MASK) { 3910 case SMS_FORMAT_DEVICE_PAGE: { 3911 struct scsi_format_page *format_page; 3912 3913 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3914 panic("subpage is incorrect!"); 3915 3916 /* 3917 * Sectors per track are set above. Bytes per 3918 * sector need to be set here on a per-LUN basis. 3919 */ 3920 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3921 &format_page_default, 3922 sizeof(format_page_default)); 3923 memcpy(&lun->mode_pages.format_page[ 3924 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3925 sizeof(format_page_changeable)); 3926 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3927 &format_page_default, 3928 sizeof(format_page_default)); 3929 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3930 &format_page_default, 3931 sizeof(format_page_default)); 3932 3933 format_page = &lun->mode_pages.format_page[ 3934 CTL_PAGE_CURRENT]; 3935 scsi_ulto2b(lun->be_lun->blocksize, 3936 format_page->bytes_per_sector); 3937 3938 format_page = &lun->mode_pages.format_page[ 3939 CTL_PAGE_DEFAULT]; 3940 scsi_ulto2b(lun->be_lun->blocksize, 3941 format_page->bytes_per_sector); 3942 3943 format_page = &lun->mode_pages.format_page[ 3944 CTL_PAGE_SAVED]; 3945 scsi_ulto2b(lun->be_lun->blocksize, 3946 format_page->bytes_per_sector); 3947 3948 page_index->page_data = 3949 (uint8_t *)lun->mode_pages.format_page; 3950 break; 3951 } 3952 case SMS_RIGID_DISK_PAGE: { 3953 struct scsi_rigid_disk_page *rigid_disk_page; 3954 uint32_t sectors_per_cylinder; 3955 uint64_t cylinders; 3956#ifndef __XSCALE__ 3957 int shift; 3958#endif /* !__XSCALE__ */ 3959 3960 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3961 panic("invalid subpage value %d", 3962 page_index->subpage); 3963 3964 /* 3965 * Rotation rate and sectors per track are set 3966 * above. We calculate the cylinders here based on 3967 * capacity. Due to the number of heads and 3968 * sectors per track we're using, smaller arrays 3969 * may turn out to have 0 cylinders. Linux and 3970 * FreeBSD don't pay attention to these mode pages 3971 * to figure out capacity, but Solaris does. It 3972 * seems to deal with 0 cylinders just fine, and 3973 * works out a fake geometry based on the capacity. 3974 */ 3975 memcpy(&lun->mode_pages.rigid_disk_page[ 3976 CTL_PAGE_CURRENT], &rigid_disk_page_default, 3977 sizeof(rigid_disk_page_default)); 3978 memcpy(&lun->mode_pages.rigid_disk_page[ 3979 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3980 sizeof(rigid_disk_page_changeable)); 3981 memcpy(&lun->mode_pages.rigid_disk_page[ 3982 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3983 sizeof(rigid_disk_page_default)); 3984 memcpy(&lun->mode_pages.rigid_disk_page[ 3985 CTL_PAGE_SAVED], &rigid_disk_page_default, 3986 sizeof(rigid_disk_page_default)); 3987 3988 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3989 CTL_DEFAULT_HEADS; 3990 3991 /* 3992 * The divide method here will be more accurate, 3993 * probably, but results in floating point being 3994 * used in the kernel on i386 (__udivdi3()). On the 3995 * XScale, though, __udivdi3() is implemented in 3996 * software. 3997 * 3998 * The shift method for cylinder calculation is 3999 * accurate if sectors_per_cylinder is a power of 4000 * 2. Otherwise it might be slightly off -- you 4001 * might have a bit of a truncation problem. 4002 */ 4003#ifdef __XSCALE__ 4004 cylinders = (lun->be_lun->maxlba + 1) / 4005 sectors_per_cylinder; 4006#else 4007 for (shift = 31; shift > 0; shift--) { 4008 if (sectors_per_cylinder & (1 << shift)) 4009 break; 4010 } 4011 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4012#endif 4013 4014 /* 4015 * We've basically got 3 bytes, or 24 bits for the 4016 * cylinder size in the mode page. If we're over, 4017 * just round down to 2^24. 4018 */ 4019 if (cylinders > 0xffffff) 4020 cylinders = 0xffffff; 4021 4022 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4023 CTL_PAGE_CURRENT]; 4024 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4025 4026 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4027 CTL_PAGE_DEFAULT]; 4028 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4029 4030 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4031 CTL_PAGE_SAVED]; 4032 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4033 4034 page_index->page_data = 4035 (uint8_t *)lun->mode_pages.rigid_disk_page; 4036 break; 4037 } 4038 case SMS_CACHING_PAGE: { 4039 4040 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4041 panic("invalid subpage value %d", 4042 page_index->subpage); 4043 /* 4044 * Defaults should be okay here, no calculations 4045 * needed. 4046 */ 4047 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4048 &caching_page_default, 4049 sizeof(caching_page_default)); 4050 memcpy(&lun->mode_pages.caching_page[ 4051 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4052 sizeof(caching_page_changeable)); 4053 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4054 &caching_page_default, 4055 sizeof(caching_page_default)); 4056 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4057 &caching_page_default, 4058 sizeof(caching_page_default)); 4059 page_index->page_data = 4060 (uint8_t *)lun->mode_pages.caching_page; 4061 break; 4062 } 4063 case SMS_CONTROL_MODE_PAGE: { 4064 4065 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4066 panic("invalid subpage value %d", 4067 page_index->subpage); 4068 4069 /* 4070 * Defaults should be okay here, no calculations 4071 * needed. 4072 */ 4073 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4074 &control_page_default, 4075 sizeof(control_page_default)); 4076 memcpy(&lun->mode_pages.control_page[ 4077 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4078 sizeof(control_page_changeable)); 4079 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4080 &control_page_default, 4081 sizeof(control_page_default)); 4082 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4083 &control_page_default, 4084 sizeof(control_page_default)); 4085 page_index->page_data = 4086 (uint8_t *)lun->mode_pages.control_page; 4087 break; 4088 4089 } 4090 case SMS_VENDOR_SPECIFIC_PAGE:{ 4091 switch (page_index->subpage) { 4092 case PWR_SUBPAGE_CODE: { 4093 struct copan_power_subpage *current_page, 4094 *saved_page; 4095 4096 memcpy(&lun->mode_pages.power_subpage[ 4097 CTL_PAGE_CURRENT], 4098 &power_page_default, 4099 sizeof(power_page_default)); 4100 memcpy(&lun->mode_pages.power_subpage[ 4101 CTL_PAGE_CHANGEABLE], 4102 &power_page_changeable, 4103 sizeof(power_page_changeable)); 4104 memcpy(&lun->mode_pages.power_subpage[ 4105 CTL_PAGE_DEFAULT], 4106 &power_page_default, 4107 sizeof(power_page_default)); 4108 memcpy(&lun->mode_pages.power_subpage[ 4109 CTL_PAGE_SAVED], 4110 &power_page_default, 4111 sizeof(power_page_default)); 4112 page_index->page_data = 4113 (uint8_t *)lun->mode_pages.power_subpage; 4114 4115 current_page = (struct copan_power_subpage *) 4116 (page_index->page_data + 4117 (page_index->page_len * 4118 CTL_PAGE_CURRENT)); 4119 saved_page = (struct copan_power_subpage *) 4120 (page_index->page_data + 4121 (page_index->page_len * 4122 CTL_PAGE_SAVED)); 4123 break; 4124 } 4125 case APS_SUBPAGE_CODE: { 4126 struct copan_aps_subpage *current_page, 4127 *saved_page; 4128 4129 // This gets set multiple times but 4130 // it should always be the same. It's 4131 // only done during init so who cares. 4132 index_to_aps_page = i; 4133 4134 memcpy(&lun->mode_pages.aps_subpage[ 4135 CTL_PAGE_CURRENT], 4136 &aps_page_default, 4137 sizeof(aps_page_default)); 4138 memcpy(&lun->mode_pages.aps_subpage[ 4139 CTL_PAGE_CHANGEABLE], 4140 &aps_page_changeable, 4141 sizeof(aps_page_changeable)); 4142 memcpy(&lun->mode_pages.aps_subpage[ 4143 CTL_PAGE_DEFAULT], 4144 &aps_page_default, 4145 sizeof(aps_page_default)); 4146 memcpy(&lun->mode_pages.aps_subpage[ 4147 CTL_PAGE_SAVED], 4148 &aps_page_default, 4149 sizeof(aps_page_default)); 4150 page_index->page_data = 4151 (uint8_t *)lun->mode_pages.aps_subpage; 4152 4153 current_page = (struct copan_aps_subpage *) 4154 (page_index->page_data + 4155 (page_index->page_len * 4156 CTL_PAGE_CURRENT)); 4157 saved_page = (struct copan_aps_subpage *) 4158 (page_index->page_data + 4159 (page_index->page_len * 4160 CTL_PAGE_SAVED)); 4161 break; 4162 } 4163 case DBGCNF_SUBPAGE_CODE: { 4164 struct copan_debugconf_subpage *current_page, 4165 *saved_page; 4166 4167 memcpy(&lun->mode_pages.debugconf_subpage[ 4168 CTL_PAGE_CURRENT], 4169 &debugconf_page_default, 4170 sizeof(debugconf_page_default)); 4171 memcpy(&lun->mode_pages.debugconf_subpage[ 4172 CTL_PAGE_CHANGEABLE], 4173 &debugconf_page_changeable, 4174 sizeof(debugconf_page_changeable)); 4175 memcpy(&lun->mode_pages.debugconf_subpage[ 4176 CTL_PAGE_DEFAULT], 4177 &debugconf_page_default, 4178 sizeof(debugconf_page_default)); 4179 memcpy(&lun->mode_pages.debugconf_subpage[ 4180 CTL_PAGE_SAVED], 4181 &debugconf_page_default, 4182 sizeof(debugconf_page_default)); 4183 page_index->page_data = 4184 (uint8_t *)lun->mode_pages.debugconf_subpage; 4185 4186 current_page = (struct copan_debugconf_subpage *) 4187 (page_index->page_data + 4188 (page_index->page_len * 4189 CTL_PAGE_CURRENT)); 4190 saved_page = (struct copan_debugconf_subpage *) 4191 (page_index->page_data + 4192 (page_index->page_len * 4193 CTL_PAGE_SAVED)); 4194 break; 4195 } 4196 default: 4197 panic("invalid subpage value %d", 4198 page_index->subpage); 4199 break; 4200 } 4201 break; 4202 } 4203 default: 4204 panic("invalid page value %d", 4205 page_index->page_code & SMPH_PC_MASK); 4206 break; 4207 } 4208 } 4209 4210 return (CTL_RETVAL_COMPLETE); 4211} 4212 4213/* 4214 * LUN allocation. 4215 * 4216 * Requirements: 4217 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4218 * wants us to allocate the LUN and he can block. 4219 * - ctl_softc is always set 4220 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4221 * 4222 * Returns 0 for success, non-zero (errno) for failure. 4223 */ 4224static int 4225ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4226 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4227{ 4228 struct ctl_lun *nlun, *lun; 4229 struct ctl_frontend *fe; 4230 int lun_number, i, lun_malloced; 4231 4232 if (be_lun == NULL) 4233 return (EINVAL); 4234 4235 /* 4236 * We currently only support Direct Access or Processor LUN types. 4237 */ 4238 switch (be_lun->lun_type) { 4239 case T_DIRECT: 4240 break; 4241 case T_PROCESSOR: 4242 break; 4243 case T_SEQUENTIAL: 4244 case T_CHANGER: 4245 default: 4246 be_lun->lun_config_status(be_lun->be_lun, 4247 CTL_LUN_CONFIG_FAILURE); 4248 break; 4249 } 4250 if (ctl_lun == NULL) { 4251 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4252 lun_malloced = 1; 4253 } else { 4254 lun_malloced = 0; 4255 lun = ctl_lun; 4256 } 4257 4258 memset(lun, 0, sizeof(*lun)); 4259 if (lun_malloced) 4260 lun->flags = CTL_LUN_MALLOCED; 4261 4262 mtx_lock(&ctl_softc->ctl_lock); 4263 /* 4264 * See if the caller requested a particular LUN number. If so, see 4265 * if it is available. Otherwise, allocate the first available LUN. 4266 */ 4267 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4268 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4269 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4270 mtx_unlock(&ctl_softc->ctl_lock); 4271 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4272 printf("ctl: requested LUN ID %d is higher " 4273 "than CTL_MAX_LUNS - 1 (%d)\n", 4274 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4275 } else { 4276 /* 4277 * XXX KDM return an error, or just assign 4278 * another LUN ID in this case?? 4279 */ 4280 printf("ctl: requested LUN ID %d is already " 4281 "in use\n", be_lun->req_lun_id); 4282 } 4283 if (lun->flags & CTL_LUN_MALLOCED) 4284 free(lun, M_CTL); 4285 be_lun->lun_config_status(be_lun->be_lun, 4286 CTL_LUN_CONFIG_FAILURE); 4287 return (ENOSPC); 4288 } 4289 lun_number = be_lun->req_lun_id; 4290 } else { 4291 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4292 if (lun_number == -1) { 4293 mtx_unlock(&ctl_softc->ctl_lock); 4294 printf("ctl: can't allocate LUN on target %ju, out of " 4295 "LUNs\n", (uintmax_t)target_id.id); 4296 if (lun->flags & CTL_LUN_MALLOCED) 4297 free(lun, M_CTL); 4298 be_lun->lun_config_status(be_lun->be_lun, 4299 CTL_LUN_CONFIG_FAILURE); 4300 return (ENOSPC); 4301 } 4302 } 4303 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4304 4305 lun->target = target_id; 4306 lun->lun = lun_number; 4307 lun->be_lun = be_lun; 4308 /* 4309 * The processor LUN is always enabled. Disk LUNs come on line 4310 * disabled, and must be enabled by the backend. 4311 */ 4312 lun->flags |= CTL_LUN_DISABLED; 4313 lun->backend = be_lun->be; 4314 be_lun->ctl_lun = lun; 4315 be_lun->lun_id = lun_number; 4316 atomic_add_int(&be_lun->be->num_luns, 1); 4317 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4318 lun->flags |= CTL_LUN_STOPPED; 4319 4320 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4321 lun->flags |= CTL_LUN_INOPERABLE; 4322 4323 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4324 lun->flags |= CTL_LUN_PRIMARY_SC; 4325 4326 lun->ctl_softc = ctl_softc; 4327 TAILQ_INIT(&lun->ooa_queue); 4328 TAILQ_INIT(&lun->blocked_queue); 4329 STAILQ_INIT(&lun->error_list); 4330 4331 /* 4332 * Initialize the mode page index. 4333 */ 4334 ctl_init_page_index(lun); 4335 4336 /* 4337 * Set the poweron UA for all initiators on this LUN only. 4338 */ 4339 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4340 lun->pending_sense[i].ua_pending = CTL_UA_POWERON; 4341 4342 /* 4343 * Now, before we insert this lun on the lun list, set the lun 4344 * inventory changed UA for all other luns. 4345 */ 4346 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4347 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4348 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4349 } 4350 } 4351 4352 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4353 4354 ctl_softc->ctl_luns[lun_number] = lun; 4355 4356 ctl_softc->num_luns++; 4357 4358 /* Setup statistics gathering */ 4359 lun->stats.device_type = be_lun->lun_type; 4360 lun->stats.lun_number = lun_number; 4361 if (lun->stats.device_type == T_DIRECT) 4362 lun->stats.blocksize = be_lun->blocksize; 4363 else 4364 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4365 for (i = 0;i < CTL_MAX_PORTS;i++) 4366 lun->stats.ports[i].targ_port = i; 4367 4368 mtx_unlock(&ctl_softc->ctl_lock); 4369 4370 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4371 4372 /* 4373 * Run through each registered FETD and bring it online if it isn't 4374 * already. Enable the target ID if it hasn't been enabled, and 4375 * enable this particular LUN. 4376 */ 4377 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) { 4378 int retval; 4379 4380 /* 4381 * XXX KDM this only works for ONE TARGET ID. We'll need 4382 * to do things differently if we go to a multiple target 4383 * ID scheme. 4384 */ 4385 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) { 4386 4387 retval = fe->targ_enable(fe->targ_lun_arg, target_id); 4388 if (retval != 0) { 4389 printf("ctl_alloc_lun: FETD %s port %d " 4390 "returned error %d for targ_enable on " 4391 "target %ju\n", fe->port_name, 4392 fe->targ_port, retval, 4393 (uintmax_t)target_id.id); 4394 } else 4395 fe->status |= CTL_PORT_STATUS_TARG_ONLINE; 4396 } 4397 4398 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number); 4399 if (retval != 0) { 4400 printf("ctl_alloc_lun: FETD %s port %d returned error " 4401 "%d for lun_enable on target %ju lun %d\n", 4402 fe->port_name, fe->targ_port, retval, 4403 (uintmax_t)target_id.id, lun_number); 4404 } else 4405 fe->status |= CTL_PORT_STATUS_LUN_ONLINE; 4406 } 4407 return (0); 4408} 4409 4410/* 4411 * Delete a LUN. 4412 * Assumptions: 4413 * - LUN has already been marked invalid and any pending I/O has been taken 4414 * care of. 4415 */ 4416static int 4417ctl_free_lun(struct ctl_lun *lun) 4418{ 4419 struct ctl_softc *softc; 4420#if 0 4421 struct ctl_frontend *fe; 4422#endif 4423 struct ctl_lun *nlun; 4424 union ctl_io *io, *next_io; 4425 int i; 4426 4427 softc = lun->ctl_softc; 4428 4429 mtx_assert(&softc->ctl_lock, MA_OWNED); 4430 4431 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4432 4433 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4434 4435 softc->ctl_luns[lun->lun] = NULL; 4436 4437 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) { 4438 printf("ctl_free_lun: aieee!! freeing a LUN with " 4439 "outstanding I/O!!\n"); 4440 } 4441 4442 /* 4443 * If we have anything pending on the RtR queue, remove it. 4444 */ 4445 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL; 4446 io = next_io) { 4447 uint32_t targ_lun; 4448 4449 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 4450 targ_lun = io->io_hdr.nexus.targ_lun; 4451 if (io->io_hdr.nexus.lun_map_fn != NULL) 4452 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 4453 if ((io->io_hdr.nexus.targ_target.id == lun->target.id) 4454 && (targ_lun == lun->lun)) 4455 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 4456 ctl_io_hdr, links); 4457 } 4458 4459 /* 4460 * Then remove everything from the blocked queue. 4461 */ 4462 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL; 4463 io = next_io) { 4464 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links); 4465 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links); 4466 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 4467 } 4468 4469 /* 4470 * Now clear out the OOA queue, and free all the I/O. 4471 * XXX KDM should we notify the FETD here? We probably need to 4472 * quiesce the LUN before deleting it. 4473 */ 4474 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL; 4475 io = next_io) { 4476 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links); 4477 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 4478 ctl_free_io(io); 4479 } 4480 4481 softc->num_luns--; 4482 4483 /* 4484 * XXX KDM this scheme only works for a single target/multiple LUN 4485 * setup. It needs to be revamped for a multiple target scheme. 4486 * 4487 * XXX KDM this results in fe->lun_disable() getting called twice, 4488 * once when ctl_disable_lun() is called, and a second time here. 4489 * We really need to re-think the LUN disable semantics. There 4490 * should probably be several steps/levels to LUN removal: 4491 * - disable 4492 * - invalidate 4493 * - free 4494 * 4495 * Right now we only have a disable method when communicating to 4496 * the front end ports, at least for individual LUNs. 4497 */ 4498#if 0 4499 STAILQ_FOREACH(fe, &softc->fe_list, links) { 4500 int retval; 4501 4502 retval = fe->lun_disable(fe->targ_lun_arg, lun->target, 4503 lun->lun); 4504 if (retval != 0) { 4505 printf("ctl_free_lun: FETD %s port %d returned error " 4506 "%d for lun_disable on target %ju lun %jd\n", 4507 fe->port_name, fe->targ_port, retval, 4508 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4509 } 4510 4511 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4512 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4513 4514 retval = fe->targ_disable(fe->targ_lun_arg,lun->target); 4515 if (retval != 0) { 4516 printf("ctl_free_lun: FETD %s port %d " 4517 "returned error %d for targ_disable on " 4518 "target %ju\n", fe->port_name, 4519 fe->targ_port, retval, 4520 (uintmax_t)lun->target.id); 4521 } else 4522 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4523 4524 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4525 continue; 4526 4527#if 0 4528 fe->port_offline(fe->onoff_arg); 4529 fe->status &= ~CTL_PORT_STATUS_ONLINE; 4530#endif 4531 } 4532 } 4533#endif 4534 4535 /* 4536 * Tell the backend to free resources, if this LUN has a backend. 4537 */ 4538 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4539 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4540 4541 if (lun->flags & CTL_LUN_MALLOCED) 4542 free(lun, M_CTL); 4543 4544 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4545 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4546 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4547 } 4548 } 4549 4550 return (0); 4551} 4552 4553static void 4554ctl_create_lun(struct ctl_be_lun *be_lun) 4555{ 4556 struct ctl_softc *ctl_softc; 4557 4558 ctl_softc = control_softc; 4559 4560 /* 4561 * ctl_alloc_lun() should handle all potential failure cases. 4562 */ 4563 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4564} 4565 4566int 4567ctl_add_lun(struct ctl_be_lun *be_lun) 4568{ 4569 struct ctl_softc *ctl_softc; 4570 4571 ctl_softc = control_softc; 4572 4573 mtx_lock(&ctl_softc->ctl_lock); 4574 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4575 mtx_unlock(&ctl_softc->ctl_lock); 4576 4577 ctl_wakeup_thread(); 4578 4579 return (0); 4580} 4581 4582int 4583ctl_enable_lun(struct ctl_be_lun *be_lun) 4584{ 4585 struct ctl_softc *ctl_softc; 4586 struct ctl_frontend *fe, *nfe; 4587 struct ctl_lun *lun; 4588 int retval; 4589 4590 ctl_softc = control_softc; 4591 4592 lun = (struct ctl_lun *)be_lun->ctl_lun; 4593 4594 mtx_lock(&ctl_softc->ctl_lock); 4595 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4596 /* 4597 * eh? Why did we get called if the LUN is already 4598 * enabled? 4599 */ 4600 mtx_unlock(&ctl_softc->ctl_lock); 4601 return (0); 4602 } 4603 lun->flags &= ~CTL_LUN_DISABLED; 4604 4605 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) { 4606 nfe = STAILQ_NEXT(fe, links); 4607 4608 /* 4609 * Drop the lock while we call the FETD's enable routine. 4610 * This can lead to a callback into CTL (at least in the 4611 * case of the internal initiator frontend. 4612 */ 4613 mtx_unlock(&ctl_softc->ctl_lock); 4614 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun); 4615 mtx_lock(&ctl_softc->ctl_lock); 4616 if (retval != 0) { 4617 printf("%s: FETD %s port %d returned error " 4618 "%d for lun_enable on target %ju lun %jd\n", 4619 __func__, fe->port_name, fe->targ_port, retval, 4620 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4621 } 4622#if 0 4623 else { 4624 /* NOTE: TODO: why does lun enable affect port status? */ 4625 fe->status |= CTL_PORT_STATUS_LUN_ONLINE; 4626 } 4627#endif 4628 } 4629 4630 mtx_unlock(&ctl_softc->ctl_lock); 4631 4632 return (0); 4633} 4634 4635int 4636ctl_disable_lun(struct ctl_be_lun *be_lun) 4637{ 4638 struct ctl_softc *ctl_softc; 4639 struct ctl_frontend *fe; 4640 struct ctl_lun *lun; 4641 int retval; 4642 4643 ctl_softc = control_softc; 4644 4645 lun = (struct ctl_lun *)be_lun->ctl_lun; 4646 4647 mtx_lock(&ctl_softc->ctl_lock); 4648 4649 if (lun->flags & CTL_LUN_DISABLED) { 4650 mtx_unlock(&ctl_softc->ctl_lock); 4651 return (0); 4652 } 4653 lun->flags |= CTL_LUN_DISABLED; 4654 4655 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) { 4656 mtx_unlock(&ctl_softc->ctl_lock); 4657 /* 4658 * Drop the lock before we call the frontend's disable 4659 * routine, to avoid lock order reversals. 4660 * 4661 * XXX KDM what happens if the frontend list changes while 4662 * we're traversing it? It's unlikely, but should be handled. 4663 */ 4664 retval = fe->lun_disable(fe->targ_lun_arg, lun->target, 4665 lun->lun); 4666 mtx_lock(&ctl_softc->ctl_lock); 4667 if (retval != 0) { 4668 printf("ctl_alloc_lun: FETD %s port %d returned error " 4669 "%d for lun_disable on target %ju lun %jd\n", 4670 fe->port_name, fe->targ_port, retval, 4671 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4672 } 4673 } 4674 4675 mtx_unlock(&ctl_softc->ctl_lock); 4676 4677 return (0); 4678} 4679 4680int 4681ctl_start_lun(struct ctl_be_lun *be_lun) 4682{ 4683 struct ctl_softc *ctl_softc; 4684 struct ctl_lun *lun; 4685 4686 ctl_softc = control_softc; 4687 4688 lun = (struct ctl_lun *)be_lun->ctl_lun; 4689 4690 mtx_lock(&ctl_softc->ctl_lock); 4691 lun->flags &= ~CTL_LUN_STOPPED; 4692 mtx_unlock(&ctl_softc->ctl_lock); 4693 4694 return (0); 4695} 4696 4697int 4698ctl_stop_lun(struct ctl_be_lun *be_lun) 4699{ 4700 struct ctl_softc *ctl_softc; 4701 struct ctl_lun *lun; 4702 4703 ctl_softc = control_softc; 4704 4705 lun = (struct ctl_lun *)be_lun->ctl_lun; 4706 4707 mtx_lock(&ctl_softc->ctl_lock); 4708 lun->flags |= CTL_LUN_STOPPED; 4709 mtx_unlock(&ctl_softc->ctl_lock); 4710 4711 return (0); 4712} 4713 4714int 4715ctl_lun_offline(struct ctl_be_lun *be_lun) 4716{ 4717 struct ctl_softc *ctl_softc; 4718 struct ctl_lun *lun; 4719 4720 ctl_softc = control_softc; 4721 4722 lun = (struct ctl_lun *)be_lun->ctl_lun; 4723 4724 mtx_lock(&ctl_softc->ctl_lock); 4725 lun->flags |= CTL_LUN_OFFLINE; 4726 mtx_unlock(&ctl_softc->ctl_lock); 4727 4728 return (0); 4729} 4730 4731int 4732ctl_lun_online(struct ctl_be_lun *be_lun) 4733{ 4734 struct ctl_softc *ctl_softc; 4735 struct ctl_lun *lun; 4736 4737 ctl_softc = control_softc; 4738 4739 lun = (struct ctl_lun *)be_lun->ctl_lun; 4740 4741 mtx_lock(&ctl_softc->ctl_lock); 4742 lun->flags &= ~CTL_LUN_OFFLINE; 4743 mtx_unlock(&ctl_softc->ctl_lock); 4744 4745 return (0); 4746} 4747 4748int 4749ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4750{ 4751 struct ctl_softc *ctl_softc; 4752 struct ctl_lun *lun; 4753 4754 ctl_softc = control_softc; 4755 4756 lun = (struct ctl_lun *)be_lun->ctl_lun; 4757 4758 mtx_lock(&ctl_softc->ctl_lock); 4759 4760 /* 4761 * The LUN needs to be disabled before it can be marked invalid. 4762 */ 4763 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4764 mtx_unlock(&ctl_softc->ctl_lock); 4765 return (-1); 4766 } 4767 /* 4768 * Mark the LUN invalid. 4769 */ 4770 lun->flags |= CTL_LUN_INVALID; 4771 4772 /* 4773 * If there is nothing in the OOA queue, go ahead and free the LUN. 4774 * If we have something in the OOA queue, we'll free it when the 4775 * last I/O completes. 4776 */ 4777 if (TAILQ_FIRST(&lun->ooa_queue) == NULL) 4778 ctl_free_lun(lun); 4779 mtx_unlock(&ctl_softc->ctl_lock); 4780 4781 return (0); 4782} 4783 4784int 4785ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4786{ 4787 struct ctl_softc *ctl_softc; 4788 struct ctl_lun *lun; 4789 4790 ctl_softc = control_softc; 4791 lun = (struct ctl_lun *)be_lun->ctl_lun; 4792 4793 mtx_lock(&ctl_softc->ctl_lock); 4794 lun->flags |= CTL_LUN_INOPERABLE; 4795 mtx_unlock(&ctl_softc->ctl_lock); 4796 4797 return (0); 4798} 4799 4800int 4801ctl_lun_operable(struct ctl_be_lun *be_lun) 4802{ 4803 struct ctl_softc *ctl_softc; 4804 struct ctl_lun *lun; 4805 4806 ctl_softc = control_softc; 4807 lun = (struct ctl_lun *)be_lun->ctl_lun; 4808 4809 mtx_lock(&ctl_softc->ctl_lock); 4810 lun->flags &= ~CTL_LUN_INOPERABLE; 4811 mtx_unlock(&ctl_softc->ctl_lock); 4812 4813 return (0); 4814} 4815 4816int 4817ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 4818 int lock) 4819{ 4820 struct ctl_softc *softc; 4821 struct ctl_lun *lun; 4822 struct copan_aps_subpage *current_sp; 4823 struct ctl_page_index *page_index; 4824 int i; 4825 4826 softc = control_softc; 4827 4828 mtx_lock(&softc->ctl_lock); 4829 4830 lun = (struct ctl_lun *)be_lun->ctl_lun; 4831 4832 page_index = NULL; 4833 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4834 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 4835 APS_PAGE_CODE) 4836 continue; 4837 4838 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 4839 continue; 4840 page_index = &lun->mode_pages.index[i]; 4841 } 4842 4843 if (page_index == NULL) { 4844 mtx_unlock(&softc->ctl_lock); 4845 printf("%s: APS subpage not found for lun %ju!\n", __func__, 4846 (uintmax_t)lun->lun); 4847 return (1); 4848 } 4849#if 0 4850 if ((softc->aps_locked_lun != 0) 4851 && (softc->aps_locked_lun != lun->lun)) { 4852 printf("%s: attempt to lock LUN %llu when %llu is already " 4853 "locked\n"); 4854 mtx_unlock(&softc->ctl_lock); 4855 return (1); 4856 } 4857#endif 4858 4859 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 4860 (page_index->page_len * CTL_PAGE_CURRENT)); 4861 4862 if (lock != 0) { 4863 current_sp->lock_active = APS_LOCK_ACTIVE; 4864 softc->aps_locked_lun = lun->lun; 4865 } else { 4866 current_sp->lock_active = 0; 4867 softc->aps_locked_lun = 0; 4868 } 4869 4870 4871 /* 4872 * If we're in HA mode, try to send the lock message to the other 4873 * side. 4874 */ 4875 if (ctl_is_single == 0) { 4876 int isc_retval; 4877 union ctl_ha_msg lock_msg; 4878 4879 lock_msg.hdr.nexus = *nexus; 4880 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 4881 if (lock != 0) 4882 lock_msg.aps.lock_flag = 1; 4883 else 4884 lock_msg.aps.lock_flag = 0; 4885 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 4886 sizeof(lock_msg), 0); 4887 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 4888 printf("%s: APS (lock=%d) error returned from " 4889 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 4890 mtx_unlock(&softc->ctl_lock); 4891 return (1); 4892 } 4893 } 4894 4895 mtx_unlock(&softc->ctl_lock); 4896 4897 return (0); 4898} 4899 4900void 4901ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4902{ 4903 struct ctl_lun *lun; 4904 struct ctl_softc *softc; 4905 int i; 4906 4907 softc = control_softc; 4908 4909 mtx_lock(&softc->ctl_lock); 4910 4911 lun = (struct ctl_lun *)be_lun->ctl_lun; 4912 4913 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4914 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED; 4915 4916 mtx_unlock(&softc->ctl_lock); 4917} 4918 4919/* 4920 * Backend "memory move is complete" callback for requests that never 4921 * make it down to say RAIDCore's configuration code. 4922 */ 4923int 4924ctl_config_move_done(union ctl_io *io) 4925{ 4926 int retval; 4927 4928 retval = CTL_RETVAL_COMPLETE; 4929 4930 4931 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4932 /* 4933 * XXX KDM this shouldn't happen, but what if it does? 4934 */ 4935 if (io->io_hdr.io_type != CTL_IO_SCSI) 4936 panic("I/O type isn't CTL_IO_SCSI!"); 4937 4938 if ((io->io_hdr.port_status == 0) 4939 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4940 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 4941 io->io_hdr.status = CTL_SUCCESS; 4942 else if ((io->io_hdr.port_status != 0) 4943 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4944 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 4945 /* 4946 * For hardware error sense keys, the sense key 4947 * specific value is defined to be a retry count, 4948 * but we use it to pass back an internal FETD 4949 * error code. XXX KDM Hopefully the FETD is only 4950 * using 16 bits for an error code, since that's 4951 * all the space we have in the sks field. 4952 */ 4953 ctl_set_internal_failure(&io->scsiio, 4954 /*sks_valid*/ 1, 4955 /*retry_count*/ 4956 io->io_hdr.port_status); 4957 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4958 free(io->scsiio.kern_data_ptr, M_CTL); 4959 ctl_done(io); 4960 goto bailout; 4961 } 4962 4963 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 4964 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 4965 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4966 /* 4967 * XXX KDM just assuming a single pointer here, and not a 4968 * S/G list. If we start using S/G lists for config data, 4969 * we'll need to know how to clean them up here as well. 4970 */ 4971 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4972 free(io->scsiio.kern_data_ptr, M_CTL); 4973 /* Hopefully the user has already set the status... */ 4974 ctl_done(io); 4975 } else { 4976 /* 4977 * XXX KDM now we need to continue data movement. Some 4978 * options: 4979 * - call ctl_scsiio() again? We don't do this for data 4980 * writes, because for those at least we know ahead of 4981 * time where the write will go and how long it is. For 4982 * config writes, though, that information is largely 4983 * contained within the write itself, thus we need to 4984 * parse out the data again. 4985 * 4986 * - Call some other function once the data is in? 4987 */ 4988 4989 /* 4990 * XXX KDM call ctl_scsiio() again for now, and check flag 4991 * bits to see whether we're allocated or not. 4992 */ 4993 retval = ctl_scsiio(&io->scsiio); 4994 } 4995bailout: 4996 return (retval); 4997} 4998 4999/* 5000 * This gets called by a backend driver when it is done with a 5001 * data_submit method. 5002 */ 5003void 5004ctl_data_submit_done(union ctl_io *io) 5005{ 5006 /* 5007 * If the IO_CONT flag is set, we need to call the supplied 5008 * function to continue processing the I/O, instead of completing 5009 * the I/O just yet. 5010 * 5011 * If there is an error, though, we don't want to keep processing. 5012 * Instead, just send status back to the initiator. 5013 */ 5014 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5015 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5016 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5017 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5018 io->scsiio.io_cont(io); 5019 return; 5020 } 5021 ctl_done(io); 5022} 5023 5024/* 5025 * This gets called by a backend driver when it is done with a 5026 * configuration write. 5027 */ 5028void 5029ctl_config_write_done(union ctl_io *io) 5030{ 5031 /* 5032 * If the IO_CONT flag is set, we need to call the supplied 5033 * function to continue processing the I/O, instead of completing 5034 * the I/O just yet. 5035 * 5036 * If there is an error, though, we don't want to keep processing. 5037 * Instead, just send status back to the initiator. 5038 */ 5039 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5040 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5041 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5042 io->scsiio.io_cont(io); 5043 return; 5044 } 5045 /* 5046 * Since a configuration write can be done for commands that actually 5047 * have data allocated, like write buffer, and commands that have 5048 * no data, like start/stop unit, we need to check here. 5049 */ 5050 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5051 free(io->scsiio.kern_data_ptr, M_CTL); 5052 ctl_done(io); 5053} 5054 5055/* 5056 * SCSI release command. 5057 */ 5058int 5059ctl_scsi_release(struct ctl_scsiio *ctsio) 5060{ 5061 int length, longid, thirdparty_id, resv_id; 5062 struct ctl_softc *ctl_softc; 5063 struct ctl_lun *lun; 5064 5065 length = 0; 5066 resv_id = 0; 5067 5068 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5069 5070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5071 ctl_softc = control_softc; 5072 5073 switch (ctsio->cdb[0]) { 5074 case RELEASE: { 5075 struct scsi_release *cdb; 5076 5077 cdb = (struct scsi_release *)ctsio->cdb; 5078 if ((cdb->byte2 & 0x1f) != 0) { 5079 ctl_set_invalid_field(ctsio, 5080 /*sks_valid*/ 1, 5081 /*command*/ 1, 5082 /*field*/ 1, 5083 /*bit_valid*/ 0, 5084 /*bit*/ 0); 5085 ctl_done((union ctl_io *)ctsio); 5086 return (CTL_RETVAL_COMPLETE); 5087 } 5088 break; 5089 } 5090 case RELEASE_10: { 5091 struct scsi_release_10 *cdb; 5092 5093 cdb = (struct scsi_release_10 *)ctsio->cdb; 5094 5095 if ((cdb->byte2 & SR10_EXTENT) != 0) { 5096 ctl_set_invalid_field(ctsio, 5097 /*sks_valid*/ 1, 5098 /*command*/ 1, 5099 /*field*/ 1, 5100 /*bit_valid*/ 1, 5101 /*bit*/ 0); 5102 ctl_done((union ctl_io *)ctsio); 5103 return (CTL_RETVAL_COMPLETE); 5104 5105 } 5106 5107 if ((cdb->byte2 & SR10_3RDPTY) != 0) { 5108 ctl_set_invalid_field(ctsio, 5109 /*sks_valid*/ 1, 5110 /*command*/ 1, 5111 /*field*/ 1, 5112 /*bit_valid*/ 1, 5113 /*bit*/ 4); 5114 ctl_done((union ctl_io *)ctsio); 5115 return (CTL_RETVAL_COMPLETE); 5116 } 5117 5118 if (cdb->byte2 & SR10_LONGID) 5119 longid = 1; 5120 else 5121 thirdparty_id = cdb->thirdparty_id; 5122 5123 resv_id = cdb->resv_id; 5124 length = scsi_2btoul(cdb->length); 5125 break; 5126 } 5127 } 5128 5129 5130 /* 5131 * XXX KDM right now, we only support LUN reservation. We don't 5132 * support 3rd party reservations, or extent reservations, which 5133 * might actually need the parameter list. If we've gotten this 5134 * far, we've got a LUN reservation. Anything else got kicked out 5135 * above. So, according to SPC, ignore the length. 5136 */ 5137 length = 0; 5138 5139 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5140 && (length > 0)) { 5141 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5142 ctsio->kern_data_len = length; 5143 ctsio->kern_total_len = length; 5144 ctsio->kern_data_resid = 0; 5145 ctsio->kern_rel_offset = 0; 5146 ctsio->kern_sg_entries = 0; 5147 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5148 ctsio->be_move_done = ctl_config_move_done; 5149 ctl_datamove((union ctl_io *)ctsio); 5150 5151 return (CTL_RETVAL_COMPLETE); 5152 } 5153 5154 if (length > 0) 5155 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5156 5157 mtx_lock(&ctl_softc->ctl_lock); 5158 5159 /* 5160 * According to SPC, it is not an error for an intiator to attempt 5161 * to release a reservation on a LUN that isn't reserved, or that 5162 * is reserved by another initiator. The reservation can only be 5163 * released, though, by the initiator who made it or by one of 5164 * several reset type events. 5165 */ 5166 if (lun->flags & CTL_LUN_RESERVED) { 5167 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5168 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5169 && (ctsio->io_hdr.nexus.targ_target.id == 5170 lun->rsv_nexus.targ_target.id)) { 5171 lun->flags &= ~CTL_LUN_RESERVED; 5172 } 5173 } 5174 5175 ctsio->scsi_status = SCSI_STATUS_OK; 5176 ctsio->io_hdr.status = CTL_SUCCESS; 5177 5178 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5179 free(ctsio->kern_data_ptr, M_CTL); 5180 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5181 } 5182 5183 mtx_unlock(&ctl_softc->ctl_lock); 5184 5185 ctl_done((union ctl_io *)ctsio); 5186 return (CTL_RETVAL_COMPLETE); 5187} 5188 5189int 5190ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5191{ 5192 int extent, thirdparty, longid; 5193 int resv_id, length; 5194 uint64_t thirdparty_id; 5195 struct ctl_softc *ctl_softc; 5196 struct ctl_lun *lun; 5197 5198 extent = 0; 5199 thirdparty = 0; 5200 longid = 0; 5201 resv_id = 0; 5202 length = 0; 5203 thirdparty_id = 0; 5204 5205 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5206 5207 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5208 ctl_softc = control_softc; 5209 5210 switch (ctsio->cdb[0]) { 5211 case RESERVE: { 5212 struct scsi_reserve *cdb; 5213 5214 cdb = (struct scsi_reserve *)ctsio->cdb; 5215 if ((cdb->byte2 & 0x1f) != 0) { 5216 ctl_set_invalid_field(ctsio, 5217 /*sks_valid*/ 1, 5218 /*command*/ 1, 5219 /*field*/ 1, 5220 /*bit_valid*/ 0, 5221 /*bit*/ 0); 5222 ctl_done((union ctl_io *)ctsio); 5223 return (CTL_RETVAL_COMPLETE); 5224 } 5225 resv_id = cdb->resv_id; 5226 length = scsi_2btoul(cdb->length); 5227 break; 5228 } 5229 case RESERVE_10: { 5230 struct scsi_reserve_10 *cdb; 5231 5232 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5233 5234 if ((cdb->byte2 & SR10_EXTENT) != 0) { 5235 ctl_set_invalid_field(ctsio, 5236 /*sks_valid*/ 1, 5237 /*command*/ 1, 5238 /*field*/ 1, 5239 /*bit_valid*/ 1, 5240 /*bit*/ 0); 5241 ctl_done((union ctl_io *)ctsio); 5242 return (CTL_RETVAL_COMPLETE); 5243 } 5244 if ((cdb->byte2 & SR10_3RDPTY) != 0) { 5245 ctl_set_invalid_field(ctsio, 5246 /*sks_valid*/ 1, 5247 /*command*/ 1, 5248 /*field*/ 1, 5249 /*bit_valid*/ 1, 5250 /*bit*/ 4); 5251 ctl_done((union ctl_io *)ctsio); 5252 return (CTL_RETVAL_COMPLETE); 5253 } 5254 if (cdb->byte2 & SR10_LONGID) 5255 longid = 1; 5256 else 5257 thirdparty_id = cdb->thirdparty_id; 5258 5259 resv_id = cdb->resv_id; 5260 length = scsi_2btoul(cdb->length); 5261 break; 5262 } 5263 } 5264 5265 /* 5266 * XXX KDM right now, we only support LUN reservation. We don't 5267 * support 3rd party reservations, or extent reservations, which 5268 * might actually need the parameter list. If we've gotten this 5269 * far, we've got a LUN reservation. Anything else got kicked out 5270 * above. So, according to SPC, ignore the length. 5271 */ 5272 length = 0; 5273 5274 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5275 && (length > 0)) { 5276 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5277 ctsio->kern_data_len = length; 5278 ctsio->kern_total_len = length; 5279 ctsio->kern_data_resid = 0; 5280 ctsio->kern_rel_offset = 0; 5281 ctsio->kern_sg_entries = 0; 5282 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5283 ctsio->be_move_done = ctl_config_move_done; 5284 ctl_datamove((union ctl_io *)ctsio); 5285 5286 return (CTL_RETVAL_COMPLETE); 5287 } 5288 5289 if (length > 0) 5290 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5291 5292 mtx_lock(&ctl_softc->ctl_lock); 5293 if (lun->flags & CTL_LUN_RESERVED) { 5294 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5295 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5296 || (ctsio->io_hdr.nexus.targ_target.id != 5297 lun->rsv_nexus.targ_target.id)) { 5298 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5299 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5300 goto bailout; 5301 } 5302 } 5303 5304 lun->flags |= CTL_LUN_RESERVED; 5305 lun->rsv_nexus = ctsio->io_hdr.nexus; 5306 5307 ctsio->scsi_status = SCSI_STATUS_OK; 5308 ctsio->io_hdr.status = CTL_SUCCESS; 5309 5310bailout: 5311 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5312 free(ctsio->kern_data_ptr, M_CTL); 5313 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5314 } 5315 5316 mtx_unlock(&ctl_softc->ctl_lock); 5317 5318 ctl_done((union ctl_io *)ctsio); 5319 return (CTL_RETVAL_COMPLETE); 5320} 5321 5322int 5323ctl_start_stop(struct ctl_scsiio *ctsio) 5324{ 5325 struct scsi_start_stop_unit *cdb; 5326 struct ctl_lun *lun; 5327 struct ctl_softc *ctl_softc; 5328 int retval; 5329 5330 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5331 5332 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5333 ctl_softc = control_softc; 5334 retval = 0; 5335 5336 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5337 5338 /* 5339 * XXX KDM 5340 * We don't support the immediate bit on a stop unit. In order to 5341 * do that, we would need to code up a way to know that a stop is 5342 * pending, and hold off any new commands until it completes, one 5343 * way or another. Then we could accept or reject those commands 5344 * depending on its status. We would almost need to do the reverse 5345 * of what we do below for an immediate start -- return the copy of 5346 * the ctl_io to the FETD with status to send to the host (and to 5347 * free the copy!) and then free the original I/O once the stop 5348 * actually completes. That way, the OOA queue mechanism can work 5349 * to block commands that shouldn't proceed. Another alternative 5350 * would be to put the copy in the queue in place of the original, 5351 * and return the original back to the caller. That could be 5352 * slightly safer.. 5353 */ 5354 if ((cdb->byte2 & SSS_IMMED) 5355 && ((cdb->how & SSS_START) == 0)) { 5356 ctl_set_invalid_field(ctsio, 5357 /*sks_valid*/ 1, 5358 /*command*/ 1, 5359 /*field*/ 1, 5360 /*bit_valid*/ 1, 5361 /*bit*/ 0); 5362 ctl_done((union ctl_io *)ctsio); 5363 return (CTL_RETVAL_COMPLETE); 5364 } 5365 5366 /* 5367 * We don't support the power conditions field. We need to check 5368 * this prior to checking the load/eject and start/stop bits. 5369 */ 5370 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) { 5371 ctl_set_invalid_field(ctsio, 5372 /*sks_valid*/ 1, 5373 /*command*/ 1, 5374 /*field*/ 4, 5375 /*bit_valid*/ 1, 5376 /*bit*/ 4); 5377 ctl_done((union ctl_io *)ctsio); 5378 return (CTL_RETVAL_COMPLETE); 5379 } 5380 5381 /* 5382 * Media isn't removable, so we can't load or eject it. 5383 */ 5384 if ((cdb->how & SSS_LOEJ) != 0) { 5385 ctl_set_invalid_field(ctsio, 5386 /*sks_valid*/ 1, 5387 /*command*/ 1, 5388 /*field*/ 4, 5389 /*bit_valid*/ 1, 5390 /*bit*/ 1); 5391 ctl_done((union ctl_io *)ctsio); 5392 return (CTL_RETVAL_COMPLETE); 5393 } 5394 5395 if ((lun->flags & CTL_LUN_PR_RESERVED) 5396 && ((cdb->how & SSS_START)==0)) { 5397 uint32_t residx; 5398 5399 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5400 if (!lun->per_res[residx].registered 5401 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5402 5403 ctl_set_reservation_conflict(ctsio); 5404 ctl_done((union ctl_io *)ctsio); 5405 return (CTL_RETVAL_COMPLETE); 5406 } 5407 } 5408 5409 /* 5410 * If there is no backend on this device, we can't start or stop 5411 * it. In theory we shouldn't get any start/stop commands in the 5412 * first place at this level if the LUN doesn't have a backend. 5413 * That should get stopped by the command decode code. 5414 */ 5415 if (lun->backend == NULL) { 5416 ctl_set_invalid_opcode(ctsio); 5417 ctl_done((union ctl_io *)ctsio); 5418 return (CTL_RETVAL_COMPLETE); 5419 } 5420 5421 /* 5422 * XXX KDM Copan-specific offline behavior. 5423 * Figure out a reasonable way to port this? 5424 */ 5425#ifdef NEEDTOPORT 5426 mtx_lock(&ctl_softc->ctl_lock); 5427 5428 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5429 && (lun->flags & CTL_LUN_OFFLINE)) { 5430 /* 5431 * If the LUN is offline, and the on/offline bit isn't set, 5432 * reject the start or stop. Otherwise, let it through. 5433 */ 5434 mtx_unlock(&ctl_softc->ctl_lock); 5435 ctl_set_lun_not_ready(ctsio); 5436 ctl_done((union ctl_io *)ctsio); 5437 } else { 5438 mtx_unlock(&ctl_softc->ctl_lock); 5439#endif /* NEEDTOPORT */ 5440 /* 5441 * This could be a start or a stop when we're online, 5442 * or a stop/offline or start/online. A start or stop when 5443 * we're offline is covered in the case above. 5444 */ 5445 /* 5446 * In the non-immediate case, we send the request to 5447 * the backend and return status to the user when 5448 * it is done. 5449 * 5450 * In the immediate case, we allocate a new ctl_io 5451 * to hold a copy of the request, and send that to 5452 * the backend. We then set good status on the 5453 * user's request and return it immediately. 5454 */ 5455 if (cdb->byte2 & SSS_IMMED) { 5456 union ctl_io *new_io; 5457 5458 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5459 if (new_io == NULL) { 5460 ctl_set_busy(ctsio); 5461 ctl_done((union ctl_io *)ctsio); 5462 } else { 5463 ctl_copy_io((union ctl_io *)ctsio, 5464 new_io); 5465 retval = lun->backend->config_write(new_io); 5466 ctl_set_success(ctsio); 5467 ctl_done((union ctl_io *)ctsio); 5468 } 5469 } else { 5470 retval = lun->backend->config_write( 5471 (union ctl_io *)ctsio); 5472 } 5473#ifdef NEEDTOPORT 5474 } 5475#endif 5476 return (retval); 5477} 5478 5479/* 5480 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5481 * we don't really do anything with the LBA and length fields if the user 5482 * passes them in. Instead we'll just flush out the cache for the entire 5483 * LUN. 5484 */ 5485int 5486ctl_sync_cache(struct ctl_scsiio *ctsio) 5487{ 5488 struct ctl_lun *lun; 5489 struct ctl_softc *ctl_softc; 5490 uint64_t starting_lba; 5491 uint32_t block_count; 5492 int reladr, immed; 5493 int retval; 5494 5495 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5496 5497 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5498 ctl_softc = control_softc; 5499 retval = 0; 5500 reladr = 0; 5501 immed = 0; 5502 5503 switch (ctsio->cdb[0]) { 5504 case SYNCHRONIZE_CACHE: { 5505 struct scsi_sync_cache *cdb; 5506 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5507 5508 if (cdb->byte2 & SSC_RELADR) 5509 reladr = 1; 5510 5511 if (cdb->byte2 & SSC_IMMED) 5512 immed = 1; 5513 5514 starting_lba = scsi_4btoul(cdb->begin_lba); 5515 block_count = scsi_2btoul(cdb->lb_count); 5516 break; 5517 } 5518 case SYNCHRONIZE_CACHE_16: { 5519 struct scsi_sync_cache_16 *cdb; 5520 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5521 5522 if (cdb->byte2 & SSC_RELADR) 5523 reladr = 1; 5524 5525 if (cdb->byte2 & SSC_IMMED) 5526 immed = 1; 5527 5528 starting_lba = scsi_8btou64(cdb->begin_lba); 5529 block_count = scsi_4btoul(cdb->lb_count); 5530 break; 5531 } 5532 default: 5533 ctl_set_invalid_opcode(ctsio); 5534 ctl_done((union ctl_io *)ctsio); 5535 goto bailout; 5536 break; /* NOTREACHED */ 5537 } 5538 5539 if (immed) { 5540 /* 5541 * We don't support the immediate bit. Since it's in the 5542 * same place for the 10 and 16 byte SYNCHRONIZE CACHE 5543 * commands, we can just return the same error in either 5544 * case. 5545 */ 5546 ctl_set_invalid_field(ctsio, 5547 /*sks_valid*/ 1, 5548 /*command*/ 1, 5549 /*field*/ 1, 5550 /*bit_valid*/ 1, 5551 /*bit*/ 1); 5552 ctl_done((union ctl_io *)ctsio); 5553 goto bailout; 5554 } 5555 5556 if (reladr) { 5557 /* 5558 * We don't support the reladr bit either. It can only be 5559 * used with linked commands, and we don't support linked 5560 * commands. Since the bit is in the same place for the 5561 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can 5562 * just return the same error in either case. 5563 */ 5564 ctl_set_invalid_field(ctsio, 5565 /*sks_valid*/ 1, 5566 /*command*/ 1, 5567 /*field*/ 1, 5568 /*bit_valid*/ 1, 5569 /*bit*/ 0); 5570 ctl_done((union ctl_io *)ctsio); 5571 goto bailout; 5572 } 5573 5574 /* 5575 * We check the LBA and length, but don't do anything with them. 5576 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5577 * get flushed. This check will just help satisfy anyone who wants 5578 * to see an error for an out of range LBA. 5579 */ 5580 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5581 ctl_set_lba_out_of_range(ctsio); 5582 ctl_done((union ctl_io *)ctsio); 5583 goto bailout; 5584 } 5585 5586 /* 5587 * If this LUN has no backend, we can't flush the cache anyway. 5588 */ 5589 if (lun->backend == NULL) { 5590 ctl_set_invalid_opcode(ctsio); 5591 ctl_done((union ctl_io *)ctsio); 5592 goto bailout; 5593 } 5594 5595 /* 5596 * Check to see whether we're configured to send the SYNCHRONIZE 5597 * CACHE command directly to the back end. 5598 */ 5599 mtx_lock(&ctl_softc->ctl_lock); 5600 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5601 && (++(lun->sync_count) >= lun->sync_interval)) { 5602 lun->sync_count = 0; 5603 mtx_unlock(&ctl_softc->ctl_lock); 5604 retval = lun->backend->config_write((union ctl_io *)ctsio); 5605 } else { 5606 mtx_unlock(&ctl_softc->ctl_lock); 5607 ctl_set_success(ctsio); 5608 ctl_done((union ctl_io *)ctsio); 5609 } 5610 5611bailout: 5612 5613 return (retval); 5614} 5615 5616int 5617ctl_format(struct ctl_scsiio *ctsio) 5618{ 5619 struct scsi_format *cdb; 5620 struct ctl_lun *lun; 5621 struct ctl_softc *ctl_softc; 5622 int length, defect_list_len; 5623 5624 CTL_DEBUG_PRINT(("ctl_format\n")); 5625 5626 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5627 ctl_softc = control_softc; 5628 5629 cdb = (struct scsi_format *)ctsio->cdb; 5630 5631 length = 0; 5632 if (cdb->byte2 & SF_FMTDATA) { 5633 if (cdb->byte2 & SF_LONGLIST) 5634 length = sizeof(struct scsi_format_header_long); 5635 else 5636 length = sizeof(struct scsi_format_header_short); 5637 } 5638 5639 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5640 && (length > 0)) { 5641 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5642 ctsio->kern_data_len = length; 5643 ctsio->kern_total_len = length; 5644 ctsio->kern_data_resid = 0; 5645 ctsio->kern_rel_offset = 0; 5646 ctsio->kern_sg_entries = 0; 5647 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5648 ctsio->be_move_done = ctl_config_move_done; 5649 ctl_datamove((union ctl_io *)ctsio); 5650 5651 return (CTL_RETVAL_COMPLETE); 5652 } 5653 5654 defect_list_len = 0; 5655 5656 if (cdb->byte2 & SF_FMTDATA) { 5657 if (cdb->byte2 & SF_LONGLIST) { 5658 struct scsi_format_header_long *header; 5659 5660 header = (struct scsi_format_header_long *) 5661 ctsio->kern_data_ptr; 5662 5663 defect_list_len = scsi_4btoul(header->defect_list_len); 5664 if (defect_list_len != 0) { 5665 ctl_set_invalid_field(ctsio, 5666 /*sks_valid*/ 1, 5667 /*command*/ 0, 5668 /*field*/ 2, 5669 /*bit_valid*/ 0, 5670 /*bit*/ 0); 5671 goto bailout; 5672 } 5673 } else { 5674 struct scsi_format_header_short *header; 5675 5676 header = (struct scsi_format_header_short *) 5677 ctsio->kern_data_ptr; 5678 5679 defect_list_len = scsi_2btoul(header->defect_list_len); 5680 if (defect_list_len != 0) { 5681 ctl_set_invalid_field(ctsio, 5682 /*sks_valid*/ 1, 5683 /*command*/ 0, 5684 /*field*/ 2, 5685 /*bit_valid*/ 0, 5686 /*bit*/ 0); 5687 goto bailout; 5688 } 5689 } 5690 } 5691 5692 /* 5693 * The format command will clear out the "Medium format corrupted" 5694 * status if set by the configuration code. That status is really 5695 * just a way to notify the host that we have lost the media, and 5696 * get them to issue a command that will basically make them think 5697 * they're blowing away the media. 5698 */ 5699 mtx_lock(&ctl_softc->ctl_lock); 5700 lun->flags &= ~CTL_LUN_INOPERABLE; 5701 mtx_unlock(&ctl_softc->ctl_lock); 5702 5703 ctsio->scsi_status = SCSI_STATUS_OK; 5704 ctsio->io_hdr.status = CTL_SUCCESS; 5705bailout: 5706 5707 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5708 free(ctsio->kern_data_ptr, M_CTL); 5709 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5710 } 5711 5712 ctl_done((union ctl_io *)ctsio); 5713 return (CTL_RETVAL_COMPLETE); 5714} 5715 5716int 5717ctl_read_buffer(struct ctl_scsiio *ctsio) 5718{ 5719 struct scsi_read_buffer *cdb; 5720 struct ctl_lun *lun; 5721 int buffer_offset, len; 5722 static uint8_t descr[4]; 5723 static uint8_t echo_descr[4] = { 0 }; 5724 5725 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5726 5727 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5728 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5729 5730 if (lun->flags & CTL_LUN_PR_RESERVED) { 5731 uint32_t residx; 5732 5733 /* 5734 * XXX KDM need a lock here. 5735 */ 5736 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5737 if ((lun->res_type == SPR_TYPE_EX_AC 5738 && residx != lun->pr_res_idx) 5739 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5740 || lun->res_type == SPR_TYPE_EX_AC_AR) 5741 && !lun->per_res[residx].registered)) { 5742 ctl_set_reservation_conflict(ctsio); 5743 ctl_done((union ctl_io *)ctsio); 5744 return (CTL_RETVAL_COMPLETE); 5745 } 5746 } 5747 5748 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5749 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5750 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5751 ctl_set_invalid_field(ctsio, 5752 /*sks_valid*/ 1, 5753 /*command*/ 1, 5754 /*field*/ 1, 5755 /*bit_valid*/ 1, 5756 /*bit*/ 4); 5757 ctl_done((union ctl_io *)ctsio); 5758 return (CTL_RETVAL_COMPLETE); 5759 } 5760 if (cdb->buffer_id != 0) { 5761 ctl_set_invalid_field(ctsio, 5762 /*sks_valid*/ 1, 5763 /*command*/ 1, 5764 /*field*/ 2, 5765 /*bit_valid*/ 0, 5766 /*bit*/ 0); 5767 ctl_done((union ctl_io *)ctsio); 5768 return (CTL_RETVAL_COMPLETE); 5769 } 5770 5771 len = scsi_3btoul(cdb->length); 5772 buffer_offset = scsi_3btoul(cdb->offset); 5773 5774 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5775 ctl_set_invalid_field(ctsio, 5776 /*sks_valid*/ 1, 5777 /*command*/ 1, 5778 /*field*/ 6, 5779 /*bit_valid*/ 0, 5780 /*bit*/ 0); 5781 ctl_done((union ctl_io *)ctsio); 5782 return (CTL_RETVAL_COMPLETE); 5783 } 5784 5785 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5786 descr[0] = 0; 5787 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5788 ctsio->kern_data_ptr = descr; 5789 len = min(len, sizeof(descr)); 5790 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5791 ctsio->kern_data_ptr = echo_descr; 5792 len = min(len, sizeof(echo_descr)); 5793 } else 5794 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5795 ctsio->kern_data_len = len; 5796 ctsio->kern_total_len = len; 5797 ctsio->kern_data_resid = 0; 5798 ctsio->kern_rel_offset = 0; 5799 ctsio->kern_sg_entries = 0; 5800 ctsio->be_move_done = ctl_config_move_done; 5801 ctl_datamove((union ctl_io *)ctsio); 5802 5803 return (CTL_RETVAL_COMPLETE); 5804} 5805 5806int 5807ctl_write_buffer(struct ctl_scsiio *ctsio) 5808{ 5809 struct scsi_write_buffer *cdb; 5810 struct ctl_lun *lun; 5811 int buffer_offset, len; 5812 5813 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5814 5815 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5816 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5817 5818 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 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 if (cdb->buffer_id != 0) { 5829 ctl_set_invalid_field(ctsio, 5830 /*sks_valid*/ 1, 5831 /*command*/ 1, 5832 /*field*/ 2, 5833 /*bit_valid*/ 0, 5834 /*bit*/ 0); 5835 ctl_done((union ctl_io *)ctsio); 5836 return (CTL_RETVAL_COMPLETE); 5837 } 5838 5839 len = scsi_3btoul(cdb->length); 5840 buffer_offset = scsi_3btoul(cdb->offset); 5841 5842 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5843 ctl_set_invalid_field(ctsio, 5844 /*sks_valid*/ 1, 5845 /*command*/ 1, 5846 /*field*/ 6, 5847 /*bit_valid*/ 0, 5848 /*bit*/ 0); 5849 ctl_done((union ctl_io *)ctsio); 5850 return (CTL_RETVAL_COMPLETE); 5851 } 5852 5853 /* 5854 * If we've got a kernel request that hasn't been malloced yet, 5855 * malloc it and tell the caller the data buffer is here. 5856 */ 5857 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5858 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5859 ctsio->kern_data_len = len; 5860 ctsio->kern_total_len = len; 5861 ctsio->kern_data_resid = 0; 5862 ctsio->kern_rel_offset = 0; 5863 ctsio->kern_sg_entries = 0; 5864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5865 ctsio->be_move_done = ctl_config_move_done; 5866 ctl_datamove((union ctl_io *)ctsio); 5867 5868 return (CTL_RETVAL_COMPLETE); 5869 } 5870 5871 ctl_done((union ctl_io *)ctsio); 5872 5873 return (CTL_RETVAL_COMPLETE); 5874} 5875 5876int 5877ctl_write_same(struct ctl_scsiio *ctsio) 5878{ 5879 struct ctl_lun *lun; 5880 struct ctl_lba_len_flags *lbalen; 5881 uint64_t lba; 5882 uint32_t num_blocks; 5883 int len, retval; 5884 uint8_t byte2; 5885 5886 retval = CTL_RETVAL_COMPLETE; 5887 5888 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5889 5890 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5891 5892 switch (ctsio->cdb[0]) { 5893 case WRITE_SAME_10: { 5894 struct scsi_write_same_10 *cdb; 5895 5896 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5897 5898 lba = scsi_4btoul(cdb->addr); 5899 num_blocks = scsi_2btoul(cdb->length); 5900 byte2 = cdb->byte2; 5901 break; 5902 } 5903 case WRITE_SAME_16: { 5904 struct scsi_write_same_16 *cdb; 5905 5906 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5907 5908 lba = scsi_8btou64(cdb->addr); 5909 num_blocks = scsi_4btoul(cdb->length); 5910 byte2 = cdb->byte2; 5911 break; 5912 } 5913 default: 5914 /* 5915 * We got a command we don't support. This shouldn't 5916 * happen, commands should be filtered out above us. 5917 */ 5918 ctl_set_invalid_opcode(ctsio); 5919 ctl_done((union ctl_io *)ctsio); 5920 5921 return (CTL_RETVAL_COMPLETE); 5922 break; /* NOTREACHED */ 5923 } 5924 5925 /* 5926 * The first check is to make sure we're in bounds, the second 5927 * check is to catch wrap-around problems. If the lba + num blocks 5928 * is less than the lba, then we've wrapped around and the block 5929 * range is invalid anyway. 5930 */ 5931 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5932 || ((lba + num_blocks) < lba)) { 5933 ctl_set_lba_out_of_range(ctsio); 5934 ctl_done((union ctl_io *)ctsio); 5935 return (CTL_RETVAL_COMPLETE); 5936 } 5937 5938 /* Zero number of blocks means "to the last logical block" */ 5939 if (num_blocks == 0) { 5940 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5941 ctl_set_invalid_field(ctsio, 5942 /*sks_valid*/ 0, 5943 /*command*/ 1, 5944 /*field*/ 0, 5945 /*bit_valid*/ 0, 5946 /*bit*/ 0); 5947 ctl_done((union ctl_io *)ctsio); 5948 return (CTL_RETVAL_COMPLETE); 5949 } 5950 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5951 } 5952 5953 len = lun->be_lun->blocksize; 5954 5955 /* 5956 * If we've got a kernel request that hasn't been malloced yet, 5957 * malloc it and tell the caller the data buffer is here. 5958 */ 5959 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5960 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5961 ctsio->kern_data_len = len; 5962 ctsio->kern_total_len = len; 5963 ctsio->kern_data_resid = 0; 5964 ctsio->kern_rel_offset = 0; 5965 ctsio->kern_sg_entries = 0; 5966 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5967 ctsio->be_move_done = ctl_config_move_done; 5968 ctl_datamove((union ctl_io *)ctsio); 5969 5970 return (CTL_RETVAL_COMPLETE); 5971 } 5972 5973 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5974 lbalen->lba = lba; 5975 lbalen->len = num_blocks; 5976 lbalen->flags = byte2; 5977 retval = lun->backend->config_write((union ctl_io *)ctsio); 5978 5979 return (retval); 5980} 5981 5982int 5983ctl_unmap(struct ctl_scsiio *ctsio) 5984{ 5985 struct ctl_lun *lun; 5986 struct scsi_unmap *cdb; 5987 struct ctl_ptr_len_flags *ptrlen; 5988 struct scsi_unmap_header *hdr; 5989 struct scsi_unmap_desc *buf, *end; 5990 uint64_t lba; 5991 uint32_t num_blocks; 5992 int len, retval; 5993 uint8_t byte2; 5994 5995 retval = CTL_RETVAL_COMPLETE; 5996 5997 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5998 5999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6000 cdb = (struct scsi_unmap *)ctsio->cdb; 6001 6002 len = scsi_2btoul(cdb->length); 6003 byte2 = cdb->byte2; 6004 6005 /* 6006 * If we've got a kernel request that hasn't been malloced yet, 6007 * malloc it and tell the caller the data buffer is here. 6008 */ 6009 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6010 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6011 ctsio->kern_data_len = len; 6012 ctsio->kern_total_len = len; 6013 ctsio->kern_data_resid = 0; 6014 ctsio->kern_rel_offset = 0; 6015 ctsio->kern_sg_entries = 0; 6016 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6017 ctsio->be_move_done = ctl_config_move_done; 6018 ctl_datamove((union ctl_io *)ctsio); 6019 6020 return (CTL_RETVAL_COMPLETE); 6021 } 6022 6023 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6024 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6025 if (len < sizeof (*hdr) || 6026 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6027 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6028 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6029 ctl_set_invalid_field(ctsio, 6030 /*sks_valid*/ 0, 6031 /*command*/ 0, 6032 /*field*/ 0, 6033 /*bit_valid*/ 0, 6034 /*bit*/ 0); 6035 ctl_done((union ctl_io *)ctsio); 6036 return (CTL_RETVAL_COMPLETE); 6037 } 6038 len = scsi_2btoul(hdr->desc_length); 6039 buf = (struct scsi_unmap_desc *)(hdr + 1); 6040 end = buf + len / sizeof(*buf); 6041 6042 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6043 ptrlen->ptr = (void *)buf; 6044 ptrlen->len = len; 6045 ptrlen->flags = byte2; 6046 6047 for (; buf < end; buf++) { 6048 lba = scsi_8btou64(buf->lba); 6049 num_blocks = scsi_4btoul(buf->length); 6050 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6051 || ((lba + num_blocks) < lba)) { 6052 ctl_set_lba_out_of_range(ctsio); 6053 ctl_done((union ctl_io *)ctsio); 6054 return (CTL_RETVAL_COMPLETE); 6055 } 6056 } 6057 6058 retval = lun->backend->config_write((union ctl_io *)ctsio); 6059 6060 return (retval); 6061} 6062 6063/* 6064 * Note that this function currently doesn't actually do anything inside 6065 * CTL to enforce things if the DQue bit is turned on. 6066 * 6067 * Also note that this function can't be used in the default case, because 6068 * the DQue bit isn't set in the changeable mask for the control mode page 6069 * anyway. This is just here as an example for how to implement a page 6070 * handler, and a placeholder in case we want to allow the user to turn 6071 * tagged queueing on and off. 6072 * 6073 * The D_SENSE bit handling is functional, however, and will turn 6074 * descriptor sense on and off for a given LUN. 6075 */ 6076int 6077ctl_control_page_handler(struct ctl_scsiio *ctsio, 6078 struct ctl_page_index *page_index, uint8_t *page_ptr) 6079{ 6080 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6081 struct ctl_lun *lun; 6082 struct ctl_softc *softc; 6083 int set_ua; 6084 uint32_t initidx; 6085 6086 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6087 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6088 set_ua = 0; 6089 6090 user_cp = (struct scsi_control_page *)page_ptr; 6091 current_cp = (struct scsi_control_page *) 6092 (page_index->page_data + (page_index->page_len * 6093 CTL_PAGE_CURRENT)); 6094 saved_cp = (struct scsi_control_page *) 6095 (page_index->page_data + (page_index->page_len * 6096 CTL_PAGE_SAVED)); 6097 6098 softc = control_softc; 6099 6100 mtx_lock(&softc->ctl_lock); 6101 if (((current_cp->rlec & SCP_DSENSE) == 0) 6102 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6103 /* 6104 * Descriptor sense is currently turned off and the user 6105 * wants to turn it on. 6106 */ 6107 current_cp->rlec |= SCP_DSENSE; 6108 saved_cp->rlec |= SCP_DSENSE; 6109 lun->flags |= CTL_LUN_SENSE_DESC; 6110 set_ua = 1; 6111 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6112 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6113 /* 6114 * Descriptor sense is currently turned on, and the user 6115 * wants to turn it off. 6116 */ 6117 current_cp->rlec &= ~SCP_DSENSE; 6118 saved_cp->rlec &= ~SCP_DSENSE; 6119 lun->flags &= ~CTL_LUN_SENSE_DESC; 6120 set_ua = 1; 6121 } 6122 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6123 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6124#ifdef NEEDTOPORT 6125 csevent_log(CSC_CTL | CSC_SHELF_SW | 6126 CTL_UNTAG_TO_UNTAG, 6127 csevent_LogType_Trace, 6128 csevent_Severity_Information, 6129 csevent_AlertLevel_Green, 6130 csevent_FRU_Firmware, 6131 csevent_FRU_Unknown, 6132 "Received untagged to untagged transition"); 6133#endif /* NEEDTOPORT */ 6134 } else { 6135#ifdef NEEDTOPORT 6136 csevent_log(CSC_CTL | CSC_SHELF_SW | 6137 CTL_UNTAG_TO_TAG, 6138 csevent_LogType_ConfigChange, 6139 csevent_Severity_Information, 6140 csevent_AlertLevel_Green, 6141 csevent_FRU_Firmware, 6142 csevent_FRU_Unknown, 6143 "Received untagged to tagged " 6144 "queueing transition"); 6145#endif /* NEEDTOPORT */ 6146 6147 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6148 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6149 set_ua = 1; 6150 } 6151 } else { 6152 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6153#ifdef NEEDTOPORT 6154 csevent_log(CSC_CTL | CSC_SHELF_SW | 6155 CTL_TAG_TO_UNTAG, 6156 csevent_LogType_ConfigChange, 6157 csevent_Severity_Warning, 6158 csevent_AlertLevel_Yellow, 6159 csevent_FRU_Firmware, 6160 csevent_FRU_Unknown, 6161 "Received tagged queueing to untagged " 6162 "transition"); 6163#endif /* NEEDTOPORT */ 6164 6165 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6166 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6167 set_ua = 1; 6168 } else { 6169#ifdef NEEDTOPORT 6170 csevent_log(CSC_CTL | CSC_SHELF_SW | 6171 CTL_TAG_TO_TAG, 6172 csevent_LogType_Trace, 6173 csevent_Severity_Information, 6174 csevent_AlertLevel_Green, 6175 csevent_FRU_Firmware, 6176 csevent_FRU_Unknown, 6177 "Received tagged queueing to tagged " 6178 "queueing transition"); 6179#endif /* NEEDTOPORT */ 6180 } 6181 } 6182 if (set_ua != 0) { 6183 int i; 6184 /* 6185 * Let other initiators know that the mode 6186 * parameters for this LUN have changed. 6187 */ 6188 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6189 if (i == initidx) 6190 continue; 6191 6192 lun->pending_sense[i].ua_pending |= 6193 CTL_UA_MODE_CHANGE; 6194 } 6195 } 6196 mtx_unlock(&softc->ctl_lock); 6197 6198 return (0); 6199} 6200 6201int 6202ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6203 struct ctl_page_index *page_index, uint8_t *page_ptr) 6204{ 6205 return (0); 6206} 6207 6208int 6209ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6210 struct ctl_page_index *page_index, int pc) 6211{ 6212 struct copan_power_subpage *page; 6213 6214 page = (struct copan_power_subpage *)page_index->page_data + 6215 (page_index->page_len * pc); 6216 6217 switch (pc) { 6218 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6219 /* 6220 * We don't update the changable bits for this page. 6221 */ 6222 break; 6223 case SMS_PAGE_CTRL_CURRENT >> 6: 6224 case SMS_PAGE_CTRL_DEFAULT >> 6: 6225 case SMS_PAGE_CTRL_SAVED >> 6: 6226#ifdef NEEDTOPORT 6227 ctl_update_power_subpage(page); 6228#endif 6229 break; 6230 default: 6231#ifdef NEEDTOPORT 6232 EPRINT(0, "Invalid PC %d!!", pc); 6233#endif 6234 break; 6235 } 6236 return (0); 6237} 6238 6239 6240int 6241ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6242 struct ctl_page_index *page_index, uint8_t *page_ptr) 6243{ 6244 struct copan_aps_subpage *user_sp; 6245 struct copan_aps_subpage *current_sp; 6246 union ctl_modepage_info *modepage_info; 6247 struct ctl_softc *softc; 6248 struct ctl_lun *lun; 6249 int retval; 6250 6251 retval = CTL_RETVAL_COMPLETE; 6252 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6253 (page_index->page_len * CTL_PAGE_CURRENT)); 6254 softc = control_softc; 6255 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6256 6257 user_sp = (struct copan_aps_subpage *)page_ptr; 6258 6259 modepage_info = (union ctl_modepage_info *) 6260 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6261 6262 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6263 modepage_info->header.subpage = page_index->subpage; 6264 modepage_info->aps.lock_active = user_sp->lock_active; 6265 6266 mtx_lock(&softc->ctl_lock); 6267 6268 /* 6269 * If there is a request to lock the LUN and another LUN is locked 6270 * this is an error. If the requested LUN is already locked ignore 6271 * the request. If no LUN is locked attempt to lock it. 6272 * if there is a request to unlock the LUN and the LUN is currently 6273 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6274 * if another LUN is locked or no LUN is locked. 6275 */ 6276 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6277 if (softc->aps_locked_lun == lun->lun) { 6278 /* 6279 * This LUN is already locked, so we're done. 6280 */ 6281 retval = CTL_RETVAL_COMPLETE; 6282 } else if (softc->aps_locked_lun == 0) { 6283 /* 6284 * No one has the lock, pass the request to the 6285 * backend. 6286 */ 6287 retval = lun->backend->config_write( 6288 (union ctl_io *)ctsio); 6289 } else { 6290 /* 6291 * Someone else has the lock, throw out the request. 6292 */ 6293 ctl_set_already_locked(ctsio); 6294 free(ctsio->kern_data_ptr, M_CTL); 6295 ctl_done((union ctl_io *)ctsio); 6296 6297 /* 6298 * Set the return value so that ctl_do_mode_select() 6299 * won't try to complete the command. We already 6300 * completed it here. 6301 */ 6302 retval = CTL_RETVAL_ERROR; 6303 } 6304 } else if (softc->aps_locked_lun == lun->lun) { 6305 /* 6306 * This LUN is locked, so pass the unlock request to the 6307 * backend. 6308 */ 6309 retval = lun->backend->config_write((union ctl_io *)ctsio); 6310 } 6311 mtx_unlock(&softc->ctl_lock); 6312 6313 return (retval); 6314} 6315 6316int 6317ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6318 struct ctl_page_index *page_index, 6319 uint8_t *page_ptr) 6320{ 6321 uint8_t *c; 6322 int i; 6323 6324 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6325 ctl_time_io_secs = 6326 (c[0] << 8) | 6327 (c[1] << 0) | 6328 0; 6329 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6330 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6331 printf("page data:"); 6332 for (i=0; i<8; i++) 6333 printf(" %.2x",page_ptr[i]); 6334 printf("\n"); 6335 return (0); 6336} 6337 6338int 6339ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6340 struct ctl_page_index *page_index, 6341 int pc) 6342{ 6343 struct copan_debugconf_subpage *page; 6344 6345 page = (struct copan_debugconf_subpage *)page_index->page_data + 6346 (page_index->page_len * pc); 6347 6348 switch (pc) { 6349 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6350 case SMS_PAGE_CTRL_DEFAULT >> 6: 6351 case SMS_PAGE_CTRL_SAVED >> 6: 6352 /* 6353 * We don't update the changable or default bits for this page. 6354 */ 6355 break; 6356 case SMS_PAGE_CTRL_CURRENT >> 6: 6357 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6358 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6359 break; 6360 default: 6361#ifdef NEEDTOPORT 6362 EPRINT(0, "Invalid PC %d!!", pc); 6363#endif /* NEEDTOPORT */ 6364 break; 6365 } 6366 return (0); 6367} 6368 6369 6370static int 6371ctl_do_mode_select(union ctl_io *io) 6372{ 6373 struct scsi_mode_page_header *page_header; 6374 struct ctl_page_index *page_index; 6375 struct ctl_scsiio *ctsio; 6376 int control_dev, page_len; 6377 int page_len_offset, page_len_size; 6378 union ctl_modepage_info *modepage_info; 6379 struct ctl_lun *lun; 6380 int *len_left, *len_used; 6381 int retval, i; 6382 6383 ctsio = &io->scsiio; 6384 page_index = NULL; 6385 page_len = 0; 6386 retval = CTL_RETVAL_COMPLETE; 6387 6388 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6389 6390 if (lun->be_lun->lun_type != T_DIRECT) 6391 control_dev = 1; 6392 else 6393 control_dev = 0; 6394 6395 modepage_info = (union ctl_modepage_info *) 6396 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6397 len_left = &modepage_info->header.len_left; 6398 len_used = &modepage_info->header.len_used; 6399 6400do_next_page: 6401 6402 page_header = (struct scsi_mode_page_header *) 6403 (ctsio->kern_data_ptr + *len_used); 6404 6405 if (*len_left == 0) { 6406 free(ctsio->kern_data_ptr, M_CTL); 6407 ctl_set_success(ctsio); 6408 ctl_done((union ctl_io *)ctsio); 6409 return (CTL_RETVAL_COMPLETE); 6410 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6411 6412 free(ctsio->kern_data_ptr, M_CTL); 6413 ctl_set_param_len_error(ctsio); 6414 ctl_done((union ctl_io *)ctsio); 6415 return (CTL_RETVAL_COMPLETE); 6416 6417 } else if ((page_header->page_code & SMPH_SPF) 6418 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6419 6420 free(ctsio->kern_data_ptr, M_CTL); 6421 ctl_set_param_len_error(ctsio); 6422 ctl_done((union ctl_io *)ctsio); 6423 return (CTL_RETVAL_COMPLETE); 6424 } 6425 6426 6427 /* 6428 * XXX KDM should we do something with the block descriptor? 6429 */ 6430 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6431 6432 if ((control_dev != 0) 6433 && (lun->mode_pages.index[i].page_flags & 6434 CTL_PAGE_FLAG_DISK_ONLY)) 6435 continue; 6436 6437 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6438 (page_header->page_code & SMPH_PC_MASK)) 6439 continue; 6440 6441 /* 6442 * If neither page has a subpage code, then we've got a 6443 * match. 6444 */ 6445 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6446 && ((page_header->page_code & SMPH_SPF) == 0)) { 6447 page_index = &lun->mode_pages.index[i]; 6448 page_len = page_header->page_length; 6449 break; 6450 } 6451 6452 /* 6453 * If both pages have subpages, then the subpage numbers 6454 * have to match. 6455 */ 6456 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6457 && (page_header->page_code & SMPH_SPF)) { 6458 struct scsi_mode_page_header_sp *sph; 6459 6460 sph = (struct scsi_mode_page_header_sp *)page_header; 6461 6462 if (lun->mode_pages.index[i].subpage == 6463 sph->subpage) { 6464 page_index = &lun->mode_pages.index[i]; 6465 page_len = scsi_2btoul(sph->page_length); 6466 break; 6467 } 6468 } 6469 } 6470 6471 /* 6472 * If we couldn't find the page, or if we don't have a mode select 6473 * handler for it, send back an error to the user. 6474 */ 6475 if ((page_index == NULL) 6476 || (page_index->select_handler == NULL)) { 6477 ctl_set_invalid_field(ctsio, 6478 /*sks_valid*/ 1, 6479 /*command*/ 0, 6480 /*field*/ *len_used, 6481 /*bit_valid*/ 0, 6482 /*bit*/ 0); 6483 free(ctsio->kern_data_ptr, M_CTL); 6484 ctl_done((union ctl_io *)ctsio); 6485 return (CTL_RETVAL_COMPLETE); 6486 } 6487 6488 if (page_index->page_code & SMPH_SPF) { 6489 page_len_offset = 2; 6490 page_len_size = 2; 6491 } else { 6492 page_len_size = 1; 6493 page_len_offset = 1; 6494 } 6495 6496 /* 6497 * If the length the initiator gives us isn't the one we specify in 6498 * the mode page header, or if they didn't specify enough data in 6499 * the CDB to avoid truncating this page, kick out the request. 6500 */ 6501 if ((page_len != (page_index->page_len - page_len_offset - 6502 page_len_size)) 6503 || (*len_left < page_index->page_len)) { 6504 6505 6506 ctl_set_invalid_field(ctsio, 6507 /*sks_valid*/ 1, 6508 /*command*/ 0, 6509 /*field*/ *len_used + page_len_offset, 6510 /*bit_valid*/ 0, 6511 /*bit*/ 0); 6512 free(ctsio->kern_data_ptr, M_CTL); 6513 ctl_done((union ctl_io *)ctsio); 6514 return (CTL_RETVAL_COMPLETE); 6515 } 6516 6517 /* 6518 * Run through the mode page, checking to make sure that the bits 6519 * the user changed are actually legal for him to change. 6520 */ 6521 for (i = 0; i < page_index->page_len; i++) { 6522 uint8_t *user_byte, *change_mask, *current_byte; 6523 int bad_bit; 6524 int j; 6525 6526 user_byte = (uint8_t *)page_header + i; 6527 change_mask = page_index->page_data + 6528 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6529 current_byte = page_index->page_data + 6530 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6531 6532 /* 6533 * Check to see whether the user set any bits in this byte 6534 * that he is not allowed to set. 6535 */ 6536 if ((*user_byte & ~(*change_mask)) == 6537 (*current_byte & ~(*change_mask))) 6538 continue; 6539 6540 /* 6541 * Go through bit by bit to determine which one is illegal. 6542 */ 6543 bad_bit = 0; 6544 for (j = 7; j >= 0; j--) { 6545 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6546 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6547 bad_bit = i; 6548 break; 6549 } 6550 } 6551 ctl_set_invalid_field(ctsio, 6552 /*sks_valid*/ 1, 6553 /*command*/ 0, 6554 /*field*/ *len_used + i, 6555 /*bit_valid*/ 1, 6556 /*bit*/ bad_bit); 6557 free(ctsio->kern_data_ptr, M_CTL); 6558 ctl_done((union ctl_io *)ctsio); 6559 return (CTL_RETVAL_COMPLETE); 6560 } 6561 6562 /* 6563 * Decrement these before we call the page handler, since we may 6564 * end up getting called back one way or another before the handler 6565 * returns to this context. 6566 */ 6567 *len_left -= page_index->page_len; 6568 *len_used += page_index->page_len; 6569 6570 retval = page_index->select_handler(ctsio, page_index, 6571 (uint8_t *)page_header); 6572 6573 /* 6574 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6575 * wait until this queued command completes to finish processing 6576 * the mode page. If it returns anything other than 6577 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6578 * already set the sense information, freed the data pointer, and 6579 * completed the io for us. 6580 */ 6581 if (retval != CTL_RETVAL_COMPLETE) 6582 goto bailout_no_done; 6583 6584 /* 6585 * If the initiator sent us more than one page, parse the next one. 6586 */ 6587 if (*len_left > 0) 6588 goto do_next_page; 6589 6590 ctl_set_success(ctsio); 6591 free(ctsio->kern_data_ptr, M_CTL); 6592 ctl_done((union ctl_io *)ctsio); 6593 6594bailout_no_done: 6595 6596 return (CTL_RETVAL_COMPLETE); 6597 6598} 6599 6600int 6601ctl_mode_select(struct ctl_scsiio *ctsio) 6602{ 6603 int param_len, pf, sp; 6604 int header_size, bd_len; 6605 int len_left, len_used; 6606 struct ctl_page_index *page_index; 6607 struct ctl_lun *lun; 6608 int control_dev, page_len; 6609 union ctl_modepage_info *modepage_info; 6610 int retval; 6611 6612 pf = 0; 6613 sp = 0; 6614 page_len = 0; 6615 len_used = 0; 6616 len_left = 0; 6617 retval = 0; 6618 bd_len = 0; 6619 page_index = NULL; 6620 6621 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6622 6623 if (lun->be_lun->lun_type != T_DIRECT) 6624 control_dev = 1; 6625 else 6626 control_dev = 0; 6627 6628 switch (ctsio->cdb[0]) { 6629 case MODE_SELECT_6: { 6630 struct scsi_mode_select_6 *cdb; 6631 6632 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6633 6634 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6635 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6636 6637 param_len = cdb->length; 6638 header_size = sizeof(struct scsi_mode_header_6); 6639 break; 6640 } 6641 case MODE_SELECT_10: { 6642 struct scsi_mode_select_10 *cdb; 6643 6644 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6645 6646 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6647 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6648 6649 param_len = scsi_2btoul(cdb->length); 6650 header_size = sizeof(struct scsi_mode_header_10); 6651 break; 6652 } 6653 default: 6654 ctl_set_invalid_opcode(ctsio); 6655 ctl_done((union ctl_io *)ctsio); 6656 return (CTL_RETVAL_COMPLETE); 6657 break; /* NOTREACHED */ 6658 } 6659 6660 /* 6661 * From SPC-3: 6662 * "A parameter list length of zero indicates that the Data-Out Buffer 6663 * shall be empty. This condition shall not be considered as an error." 6664 */ 6665 if (param_len == 0) { 6666 ctl_set_success(ctsio); 6667 ctl_done((union ctl_io *)ctsio); 6668 return (CTL_RETVAL_COMPLETE); 6669 } 6670 6671 /* 6672 * Since we'll hit this the first time through, prior to 6673 * allocation, we don't need to free a data buffer here. 6674 */ 6675 if (param_len < header_size) { 6676 ctl_set_param_len_error(ctsio); 6677 ctl_done((union ctl_io *)ctsio); 6678 return (CTL_RETVAL_COMPLETE); 6679 } 6680 6681 /* 6682 * Allocate the data buffer and grab the user's data. In theory, 6683 * we shouldn't have to sanity check the parameter list length here 6684 * because the maximum size is 64K. We should be able to malloc 6685 * that much without too many problems. 6686 */ 6687 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6688 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6689 ctsio->kern_data_len = param_len; 6690 ctsio->kern_total_len = param_len; 6691 ctsio->kern_data_resid = 0; 6692 ctsio->kern_rel_offset = 0; 6693 ctsio->kern_sg_entries = 0; 6694 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6695 ctsio->be_move_done = ctl_config_move_done; 6696 ctl_datamove((union ctl_io *)ctsio); 6697 6698 return (CTL_RETVAL_COMPLETE); 6699 } 6700 6701 switch (ctsio->cdb[0]) { 6702 case MODE_SELECT_6: { 6703 struct scsi_mode_header_6 *mh6; 6704 6705 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6706 bd_len = mh6->blk_desc_len; 6707 break; 6708 } 6709 case MODE_SELECT_10: { 6710 struct scsi_mode_header_10 *mh10; 6711 6712 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6713 bd_len = scsi_2btoul(mh10->blk_desc_len); 6714 break; 6715 } 6716 default: 6717 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6718 break; 6719 } 6720 6721 if (param_len < (header_size + bd_len)) { 6722 free(ctsio->kern_data_ptr, M_CTL); 6723 ctl_set_param_len_error(ctsio); 6724 ctl_done((union ctl_io *)ctsio); 6725 return (CTL_RETVAL_COMPLETE); 6726 } 6727 6728 /* 6729 * Set the IO_CONT flag, so that if this I/O gets passed to 6730 * ctl_config_write_done(), it'll get passed back to 6731 * ctl_do_mode_select() for further processing, or completion if 6732 * we're all done. 6733 */ 6734 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6735 ctsio->io_cont = ctl_do_mode_select; 6736 6737 modepage_info = (union ctl_modepage_info *) 6738 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6739 6740 memset(modepage_info, 0, sizeof(*modepage_info)); 6741 6742 len_left = param_len - header_size - bd_len; 6743 len_used = header_size + bd_len; 6744 6745 modepage_info->header.len_left = len_left; 6746 modepage_info->header.len_used = len_used; 6747 6748 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6749} 6750 6751int 6752ctl_mode_sense(struct ctl_scsiio *ctsio) 6753{ 6754 struct ctl_lun *lun; 6755 int pc, page_code, dbd, llba, subpage; 6756 int alloc_len, page_len, header_len, total_len; 6757 struct scsi_mode_block_descr *block_desc; 6758 struct ctl_page_index *page_index; 6759 int control_dev; 6760 6761 dbd = 0; 6762 llba = 0; 6763 block_desc = NULL; 6764 page_index = NULL; 6765 6766 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6767 6768 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6769 6770 if (lun->be_lun->lun_type != T_DIRECT) 6771 control_dev = 1; 6772 else 6773 control_dev = 0; 6774 6775 switch (ctsio->cdb[0]) { 6776 case MODE_SENSE_6: { 6777 struct scsi_mode_sense_6 *cdb; 6778 6779 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6780 6781 header_len = sizeof(struct scsi_mode_hdr_6); 6782 if (cdb->byte2 & SMS_DBD) 6783 dbd = 1; 6784 else 6785 header_len += sizeof(struct scsi_mode_block_descr); 6786 6787 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6788 page_code = cdb->page & SMS_PAGE_CODE; 6789 subpage = cdb->subpage; 6790 alloc_len = cdb->length; 6791 break; 6792 } 6793 case MODE_SENSE_10: { 6794 struct scsi_mode_sense_10 *cdb; 6795 6796 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6797 6798 header_len = sizeof(struct scsi_mode_hdr_10); 6799 6800 if (cdb->byte2 & SMS_DBD) 6801 dbd = 1; 6802 else 6803 header_len += sizeof(struct scsi_mode_block_descr); 6804 if (cdb->byte2 & SMS10_LLBAA) 6805 llba = 1; 6806 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6807 page_code = cdb->page & SMS_PAGE_CODE; 6808 subpage = cdb->subpage; 6809 alloc_len = scsi_2btoul(cdb->length); 6810 break; 6811 } 6812 default: 6813 ctl_set_invalid_opcode(ctsio); 6814 ctl_done((union ctl_io *)ctsio); 6815 return (CTL_RETVAL_COMPLETE); 6816 break; /* NOTREACHED */ 6817 } 6818 6819 /* 6820 * We have to make a first pass through to calculate the size of 6821 * the pages that match the user's query. Then we allocate enough 6822 * memory to hold it, and actually copy the data into the buffer. 6823 */ 6824 switch (page_code) { 6825 case SMS_ALL_PAGES_PAGE: { 6826 int i; 6827 6828 page_len = 0; 6829 6830 /* 6831 * At the moment, values other than 0 and 0xff here are 6832 * reserved according to SPC-3. 6833 */ 6834 if ((subpage != SMS_SUBPAGE_PAGE_0) 6835 && (subpage != SMS_SUBPAGE_ALL)) { 6836 ctl_set_invalid_field(ctsio, 6837 /*sks_valid*/ 1, 6838 /*command*/ 1, 6839 /*field*/ 3, 6840 /*bit_valid*/ 0, 6841 /*bit*/ 0); 6842 ctl_done((union ctl_io *)ctsio); 6843 return (CTL_RETVAL_COMPLETE); 6844 } 6845 6846 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6847 if ((control_dev != 0) 6848 && (lun->mode_pages.index[i].page_flags & 6849 CTL_PAGE_FLAG_DISK_ONLY)) 6850 continue; 6851 6852 /* 6853 * We don't use this subpage if the user didn't 6854 * request all subpages. 6855 */ 6856 if ((lun->mode_pages.index[i].subpage != 0) 6857 && (subpage == SMS_SUBPAGE_PAGE_0)) 6858 continue; 6859 6860#if 0 6861 printf("found page %#x len %d\n", 6862 lun->mode_pages.index[i].page_code & 6863 SMPH_PC_MASK, 6864 lun->mode_pages.index[i].page_len); 6865#endif 6866 page_len += lun->mode_pages.index[i].page_len; 6867 } 6868 break; 6869 } 6870 default: { 6871 int i; 6872 6873 page_len = 0; 6874 6875 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6876 /* Look for the right page code */ 6877 if ((lun->mode_pages.index[i].page_code & 6878 SMPH_PC_MASK) != page_code) 6879 continue; 6880 6881 /* Look for the right subpage or the subpage wildcard*/ 6882 if ((lun->mode_pages.index[i].subpage != subpage) 6883 && (subpage != SMS_SUBPAGE_ALL)) 6884 continue; 6885 6886 /* Make sure the page is supported for this dev type */ 6887 if ((control_dev != 0) 6888 && (lun->mode_pages.index[i].page_flags & 6889 CTL_PAGE_FLAG_DISK_ONLY)) 6890 continue; 6891 6892#if 0 6893 printf("found page %#x len %d\n", 6894 lun->mode_pages.index[i].page_code & 6895 SMPH_PC_MASK, 6896 lun->mode_pages.index[i].page_len); 6897#endif 6898 6899 page_len += lun->mode_pages.index[i].page_len; 6900 } 6901 6902 if (page_len == 0) { 6903 ctl_set_invalid_field(ctsio, 6904 /*sks_valid*/ 1, 6905 /*command*/ 1, 6906 /*field*/ 2, 6907 /*bit_valid*/ 1, 6908 /*bit*/ 5); 6909 ctl_done((union ctl_io *)ctsio); 6910 return (CTL_RETVAL_COMPLETE); 6911 } 6912 break; 6913 } 6914 } 6915 6916 total_len = header_len + page_len; 6917#if 0 6918 printf("header_len = %d, page_len = %d, total_len = %d\n", 6919 header_len, page_len, total_len); 6920#endif 6921 6922 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6923 ctsio->kern_sg_entries = 0; 6924 ctsio->kern_data_resid = 0; 6925 ctsio->kern_rel_offset = 0; 6926 if (total_len < alloc_len) { 6927 ctsio->residual = alloc_len - total_len; 6928 ctsio->kern_data_len = total_len; 6929 ctsio->kern_total_len = total_len; 6930 } else { 6931 ctsio->residual = 0; 6932 ctsio->kern_data_len = alloc_len; 6933 ctsio->kern_total_len = alloc_len; 6934 } 6935 6936 switch (ctsio->cdb[0]) { 6937 case MODE_SENSE_6: { 6938 struct scsi_mode_hdr_6 *header; 6939 6940 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6941 6942 header->datalen = ctl_min(total_len - 1, 254); 6943 6944 if (dbd) 6945 header->block_descr_len = 0; 6946 else 6947 header->block_descr_len = 6948 sizeof(struct scsi_mode_block_descr); 6949 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6950 break; 6951 } 6952 case MODE_SENSE_10: { 6953 struct scsi_mode_hdr_10 *header; 6954 int datalen; 6955 6956 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6957 6958 datalen = ctl_min(total_len - 2, 65533); 6959 scsi_ulto2b(datalen, header->datalen); 6960 if (dbd) 6961 scsi_ulto2b(0, header->block_descr_len); 6962 else 6963 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6964 header->block_descr_len); 6965 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6966 break; 6967 } 6968 default: 6969 panic("invalid CDB type %#x", ctsio->cdb[0]); 6970 break; /* NOTREACHED */ 6971 } 6972 6973 /* 6974 * If we've got a disk, use its blocksize in the block 6975 * descriptor. Otherwise, just set it to 0. 6976 */ 6977 if (dbd == 0) { 6978 if (control_dev != 0) 6979 scsi_ulto3b(lun->be_lun->blocksize, 6980 block_desc->block_len); 6981 else 6982 scsi_ulto3b(0, block_desc->block_len); 6983 } 6984 6985 switch (page_code) { 6986 case SMS_ALL_PAGES_PAGE: { 6987 int i, data_used; 6988 6989 data_used = header_len; 6990 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6991 struct ctl_page_index *page_index; 6992 6993 page_index = &lun->mode_pages.index[i]; 6994 6995 if ((control_dev != 0) 6996 && (page_index->page_flags & 6997 CTL_PAGE_FLAG_DISK_ONLY)) 6998 continue; 6999 7000 /* 7001 * We don't use this subpage if the user didn't 7002 * request all subpages. We already checked (above) 7003 * to make sure the user only specified a subpage 7004 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7005 */ 7006 if ((page_index->subpage != 0) 7007 && (subpage == SMS_SUBPAGE_PAGE_0)) 7008 continue; 7009 7010 /* 7011 * Call the handler, if it exists, to update the 7012 * page to the latest values. 7013 */ 7014 if (page_index->sense_handler != NULL) 7015 page_index->sense_handler(ctsio, page_index,pc); 7016 7017 memcpy(ctsio->kern_data_ptr + data_used, 7018 page_index->page_data + 7019 (page_index->page_len * pc), 7020 page_index->page_len); 7021 data_used += page_index->page_len; 7022 } 7023 break; 7024 } 7025 default: { 7026 int i, data_used; 7027 7028 data_used = header_len; 7029 7030 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7031 struct ctl_page_index *page_index; 7032 7033 page_index = &lun->mode_pages.index[i]; 7034 7035 /* Look for the right page code */ 7036 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7037 continue; 7038 7039 /* Look for the right subpage or the subpage wildcard*/ 7040 if ((page_index->subpage != subpage) 7041 && (subpage != SMS_SUBPAGE_ALL)) 7042 continue; 7043 7044 /* Make sure the page is supported for this dev type */ 7045 if ((control_dev != 0) 7046 && (page_index->page_flags & 7047 CTL_PAGE_FLAG_DISK_ONLY)) 7048 continue; 7049 7050 /* 7051 * Call the handler, if it exists, to update the 7052 * page to the latest values. 7053 */ 7054 if (page_index->sense_handler != NULL) 7055 page_index->sense_handler(ctsio, page_index,pc); 7056 7057 memcpy(ctsio->kern_data_ptr + data_used, 7058 page_index->page_data + 7059 (page_index->page_len * pc), 7060 page_index->page_len); 7061 data_used += page_index->page_len; 7062 } 7063 break; 7064 } 7065 } 7066 7067 ctsio->scsi_status = SCSI_STATUS_OK; 7068 7069 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7070 ctsio->be_move_done = ctl_config_move_done; 7071 ctl_datamove((union ctl_io *)ctsio); 7072 7073 return (CTL_RETVAL_COMPLETE); 7074} 7075 7076int 7077ctl_read_capacity(struct ctl_scsiio *ctsio) 7078{ 7079 struct scsi_read_capacity *cdb; 7080 struct scsi_read_capacity_data *data; 7081 struct ctl_lun *lun; 7082 uint32_t lba; 7083 7084 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7085 7086 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7087 7088 lba = scsi_4btoul(cdb->addr); 7089 if (((cdb->pmi & SRC_PMI) == 0) 7090 && (lba != 0)) { 7091 ctl_set_invalid_field(/*ctsio*/ ctsio, 7092 /*sks_valid*/ 1, 7093 /*command*/ 1, 7094 /*field*/ 2, 7095 /*bit_valid*/ 0, 7096 /*bit*/ 0); 7097 ctl_done((union ctl_io *)ctsio); 7098 return (CTL_RETVAL_COMPLETE); 7099 } 7100 7101 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7102 7103 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7104 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7105 ctsio->residual = 0; 7106 ctsio->kern_data_len = sizeof(*data); 7107 ctsio->kern_total_len = sizeof(*data); 7108 ctsio->kern_data_resid = 0; 7109 ctsio->kern_rel_offset = 0; 7110 ctsio->kern_sg_entries = 0; 7111 7112 /* 7113 * If the maximum LBA is greater than 0xfffffffe, the user must 7114 * issue a SERVICE ACTION IN (16) command, with the read capacity 7115 * serivce action set. 7116 */ 7117 if (lun->be_lun->maxlba > 0xfffffffe) 7118 scsi_ulto4b(0xffffffff, data->addr); 7119 else 7120 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7121 7122 /* 7123 * XXX KDM this may not be 512 bytes... 7124 */ 7125 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7126 7127 ctsio->scsi_status = SCSI_STATUS_OK; 7128 7129 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7130 ctsio->be_move_done = ctl_config_move_done; 7131 ctl_datamove((union ctl_io *)ctsio); 7132 7133 return (CTL_RETVAL_COMPLETE); 7134} 7135 7136static int 7137ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7138{ 7139 struct scsi_read_capacity_16 *cdb; 7140 struct scsi_read_capacity_data_long *data; 7141 struct ctl_lun *lun; 7142 uint64_t lba; 7143 uint32_t alloc_len; 7144 7145 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7146 7147 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7148 7149 alloc_len = scsi_4btoul(cdb->alloc_len); 7150 lba = scsi_8btou64(cdb->addr); 7151 7152 if ((cdb->reladr & SRC16_PMI) 7153 && (lba != 0)) { 7154 ctl_set_invalid_field(/*ctsio*/ ctsio, 7155 /*sks_valid*/ 1, 7156 /*command*/ 1, 7157 /*field*/ 2, 7158 /*bit_valid*/ 0, 7159 /*bit*/ 0); 7160 ctl_done((union ctl_io *)ctsio); 7161 return (CTL_RETVAL_COMPLETE); 7162 } 7163 7164 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7165 7166 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7167 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7168 7169 if (sizeof(*data) < alloc_len) { 7170 ctsio->residual = alloc_len - sizeof(*data); 7171 ctsio->kern_data_len = sizeof(*data); 7172 ctsio->kern_total_len = sizeof(*data); 7173 } else { 7174 ctsio->residual = 0; 7175 ctsio->kern_data_len = alloc_len; 7176 ctsio->kern_total_len = alloc_len; 7177 } 7178 ctsio->kern_data_resid = 0; 7179 ctsio->kern_rel_offset = 0; 7180 ctsio->kern_sg_entries = 0; 7181 7182 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7183 /* XXX KDM this may not be 512 bytes... */ 7184 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7185 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7186 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7187 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7188 data->lalba_lbp[0] |= SRC16_LBPME; 7189 7190 ctsio->scsi_status = SCSI_STATUS_OK; 7191 7192 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7193 ctsio->be_move_done = ctl_config_move_done; 7194 ctl_datamove((union ctl_io *)ctsio); 7195 7196 return (CTL_RETVAL_COMPLETE); 7197} 7198 7199int 7200ctl_service_action_in(struct ctl_scsiio *ctsio) 7201{ 7202 struct scsi_service_action_in *cdb; 7203 int retval; 7204 7205 CTL_DEBUG_PRINT(("ctl_service_action_in\n")); 7206 7207 cdb = (struct scsi_service_action_in *)ctsio->cdb; 7208 7209 retval = CTL_RETVAL_COMPLETE; 7210 7211 switch (cdb->service_action) { 7212 case SRC16_SERVICE_ACTION: 7213 retval = ctl_read_capacity_16(ctsio); 7214 break; 7215 default: 7216 ctl_set_invalid_field(/*ctsio*/ ctsio, 7217 /*sks_valid*/ 1, 7218 /*command*/ 1, 7219 /*field*/ 1, 7220 /*bit_valid*/ 1, 7221 /*bit*/ 4); 7222 ctl_done((union ctl_io *)ctsio); 7223 break; 7224 } 7225 7226 return (retval); 7227} 7228 7229int 7230ctl_maintenance_in(struct ctl_scsiio *ctsio) 7231{ 7232 struct scsi_maintenance_in *cdb; 7233 int retval; 7234 int alloc_len, total_len = 0; 7235 int num_target_port_groups, single; 7236 struct ctl_lun *lun; 7237 struct ctl_softc *softc; 7238 struct scsi_target_group_data *rtg_ptr; 7239 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2; 7240 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2, 7241 *tp_desc_ptr2_1, *tp_desc_ptr2_2; 7242 7243 CTL_DEBUG_PRINT(("ctl_maintenance_in\n")); 7244 7245 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7246 softc = control_softc; 7247 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7248 7249 retval = CTL_RETVAL_COMPLETE; 7250 7251 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) { 7252 ctl_set_invalid_field(/*ctsio*/ ctsio, 7253 /*sks_valid*/ 1, 7254 /*command*/ 1, 7255 /*field*/ 1, 7256 /*bit_valid*/ 1, 7257 /*bit*/ 4); 7258 ctl_done((union ctl_io *)ctsio); 7259 return(retval); 7260 } 7261 7262 mtx_lock(&softc->ctl_lock); 7263 single = ctl_is_single; 7264 mtx_unlock(&softc->ctl_lock); 7265 7266 if (single) 7267 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1; 7268 else 7269 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7270 7271 total_len = sizeof(struct scsi_target_group_data) + 7272 sizeof(struct scsi_target_port_group_descriptor) * 7273 num_target_port_groups + 7274 sizeof(struct scsi_target_port_descriptor) * 7275 NUM_PORTS_PER_GRP * num_target_port_groups; 7276 7277 alloc_len = scsi_4btoul(cdb->length); 7278 7279 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7280 7281 ctsio->kern_sg_entries = 0; 7282 7283 if (total_len < alloc_len) { 7284 ctsio->residual = alloc_len - total_len; 7285 ctsio->kern_data_len = total_len; 7286 ctsio->kern_total_len = total_len; 7287 } else { 7288 ctsio->residual = 0; 7289 ctsio->kern_data_len = alloc_len; 7290 ctsio->kern_total_len = alloc_len; 7291 } 7292 ctsio->kern_data_resid = 0; 7293 ctsio->kern_rel_offset = 0; 7294 7295 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr; 7296 7297 tpg_desc_ptr1 = &rtg_ptr->groups[0]; 7298 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0]; 7299 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *) 7300 &tp_desc_ptr1_1->desc_list[0]; 7301 7302 if (single == 0) { 7303 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *) 7304 &tp_desc_ptr1_2->desc_list[0]; 7305 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0]; 7306 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *) 7307 &tp_desc_ptr2_1->desc_list[0]; 7308 } else { 7309 tpg_desc_ptr2 = NULL; 7310 tp_desc_ptr2_1 = NULL; 7311 tp_desc_ptr2_2 = NULL; 7312 } 7313 7314 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7315 if (single == 0) { 7316 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7317 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7318 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7319 tpg_desc_ptr2->pref_state = 7320 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7321 } else { 7322 tpg_desc_ptr1->pref_state = 7323 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7324 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7325 } 7326 } else { 7327 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7328 tpg_desc_ptr1->pref_state = 7329 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7330 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7331 } else { 7332 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7333 tpg_desc_ptr2->pref_state = 7334 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7335 } 7336 } 7337 } else { 7338 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7339 } 7340 tpg_desc_ptr1->support = 0; 7341 tpg_desc_ptr1->target_port_group[1] = 1; 7342 tpg_desc_ptr1->status = TPG_IMPLICIT; 7343 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP; 7344 7345 if (single == 0) { 7346 tpg_desc_ptr2->support = 0; 7347 tpg_desc_ptr2->target_port_group[1] = 2; 7348 tpg_desc_ptr2->status = TPG_IMPLICIT; 7349 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP; 7350 7351 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7352 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7353 7354 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9; 7355 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10; 7356 } else { 7357 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7358 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7359 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7360 } else { 7361 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9; 7362 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10; 7363 } 7364 } 7365 7366 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7367 ctsio->be_move_done = ctl_config_move_done; 7368 7369 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7370 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7371 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7372 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7373 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7374 7375 ctl_datamove((union ctl_io *)ctsio); 7376 return(retval); 7377} 7378 7379int 7380ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7381{ 7382 struct scsi_per_res_in *cdb; 7383 int alloc_len, total_len = 0; 7384 /* struct scsi_per_res_in_rsrv in_data; */ 7385 struct ctl_lun *lun; 7386 struct ctl_softc *softc; 7387 7388 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7389 7390 softc = control_softc; 7391 7392 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7393 7394 alloc_len = scsi_2btoul(cdb->length); 7395 7396 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7397 7398retry: 7399 mtx_lock(&softc->ctl_lock); 7400 switch (cdb->action) { 7401 case SPRI_RK: /* read keys */ 7402 total_len = sizeof(struct scsi_per_res_in_keys) + 7403 lun->pr_key_count * 7404 sizeof(struct scsi_per_res_key); 7405 break; 7406 case SPRI_RR: /* read reservation */ 7407 if (lun->flags & CTL_LUN_PR_RESERVED) 7408 total_len = sizeof(struct scsi_per_res_in_rsrv); 7409 else 7410 total_len = sizeof(struct scsi_per_res_in_header); 7411 break; 7412 case SPRI_RC: /* report capabilities */ 7413 total_len = sizeof(struct scsi_per_res_cap); 7414 break; 7415 case SPRI_RS: /* read full status */ 7416 default: 7417 mtx_unlock(&softc->ctl_lock); 7418 ctl_set_invalid_field(ctsio, 7419 /*sks_valid*/ 1, 7420 /*command*/ 1, 7421 /*field*/ 1, 7422 /*bit_valid*/ 1, 7423 /*bit*/ 0); 7424 ctl_done((union ctl_io *)ctsio); 7425 return (CTL_RETVAL_COMPLETE); 7426 break; /* NOTREACHED */ 7427 } 7428 mtx_unlock(&softc->ctl_lock); 7429 7430 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7431 7432 if (total_len < alloc_len) { 7433 ctsio->residual = alloc_len - total_len; 7434 ctsio->kern_data_len = total_len; 7435 ctsio->kern_total_len = total_len; 7436 } else { 7437 ctsio->residual = 0; 7438 ctsio->kern_data_len = alloc_len; 7439 ctsio->kern_total_len = alloc_len; 7440 } 7441 7442 ctsio->kern_data_resid = 0; 7443 ctsio->kern_rel_offset = 0; 7444 ctsio->kern_sg_entries = 0; 7445 7446 mtx_lock(&softc->ctl_lock); 7447 switch (cdb->action) { 7448 case SPRI_RK: { // read keys 7449 struct scsi_per_res_in_keys *res_keys; 7450 int i, key_count; 7451 7452 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7453 7454 /* 7455 * We had to drop the lock to allocate our buffer, which 7456 * leaves time for someone to come in with another 7457 * persistent reservation. (That is unlikely, though, 7458 * since this should be the only persistent reservation 7459 * command active right now.) 7460 */ 7461 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7462 (lun->pr_key_count * 7463 sizeof(struct scsi_per_res_key)))){ 7464 mtx_unlock(&softc->ctl_lock); 7465 free(ctsio->kern_data_ptr, M_CTL); 7466 printf("%s: reservation length changed, retrying\n", 7467 __func__); 7468 goto retry; 7469 } 7470 7471 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7472 7473 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7474 lun->pr_key_count, res_keys->header.length); 7475 7476 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7477 if (!lun->per_res[i].registered) 7478 continue; 7479 7480 /* 7481 * We used lun->pr_key_count to calculate the 7482 * size to allocate. If it turns out the number of 7483 * initiators with the registered flag set is 7484 * larger than that (i.e. they haven't been kept in 7485 * sync), we've got a problem. 7486 */ 7487 if (key_count >= lun->pr_key_count) { 7488#ifdef NEEDTOPORT 7489 csevent_log(CSC_CTL | CSC_SHELF_SW | 7490 CTL_PR_ERROR, 7491 csevent_LogType_Fault, 7492 csevent_AlertLevel_Yellow, 7493 csevent_FRU_ShelfController, 7494 csevent_FRU_Firmware, 7495 csevent_FRU_Unknown, 7496 "registered keys %d >= key " 7497 "count %d", key_count, 7498 lun->pr_key_count); 7499#endif 7500 key_count++; 7501 continue; 7502 } 7503 memcpy(res_keys->keys[key_count].key, 7504 lun->per_res[i].res_key.key, 7505 ctl_min(sizeof(res_keys->keys[key_count].key), 7506 sizeof(lun->per_res[i].res_key))); 7507 key_count++; 7508 } 7509 break; 7510 } 7511 case SPRI_RR: { // read reservation 7512 struct scsi_per_res_in_rsrv *res; 7513 int tmp_len, header_only; 7514 7515 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7516 7517 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7518 7519 if (lun->flags & CTL_LUN_PR_RESERVED) 7520 { 7521 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7522 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7523 res->header.length); 7524 header_only = 0; 7525 } else { 7526 tmp_len = sizeof(struct scsi_per_res_in_header); 7527 scsi_ulto4b(0, res->header.length); 7528 header_only = 1; 7529 } 7530 7531 /* 7532 * We had to drop the lock to allocate our buffer, which 7533 * leaves time for someone to come in with another 7534 * persistent reservation. (That is unlikely, though, 7535 * since this should be the only persistent reservation 7536 * command active right now.) 7537 */ 7538 if (tmp_len != total_len) { 7539 mtx_unlock(&softc->ctl_lock); 7540 free(ctsio->kern_data_ptr, M_CTL); 7541 printf("%s: reservation status changed, retrying\n", 7542 __func__); 7543 goto retry; 7544 } 7545 7546 /* 7547 * No reservation held, so we're done. 7548 */ 7549 if (header_only != 0) 7550 break; 7551 7552 /* 7553 * If the registration is an All Registrants type, the key 7554 * is 0, since it doesn't really matter. 7555 */ 7556 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7557 memcpy(res->data.reservation, 7558 &lun->per_res[lun->pr_res_idx].res_key, 7559 sizeof(struct scsi_per_res_key)); 7560 } 7561 res->data.scopetype = lun->res_type; 7562 break; 7563 } 7564 case SPRI_RC: //report capabilities 7565 { 7566 struct scsi_per_res_cap *res_cap; 7567 uint16_t type_mask; 7568 7569 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7570 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7571 res_cap->flags2 |= SPRI_TMV; 7572 type_mask = SPRI_TM_WR_EX_AR | 7573 SPRI_TM_EX_AC_RO | 7574 SPRI_TM_WR_EX_RO | 7575 SPRI_TM_EX_AC | 7576 SPRI_TM_WR_EX | 7577 SPRI_TM_EX_AC_AR; 7578 scsi_ulto2b(type_mask, res_cap->type_mask); 7579 break; 7580 } 7581 case SPRI_RS: //read full status 7582 default: 7583 /* 7584 * This is a bug, because we just checked for this above, 7585 * and should have returned an error. 7586 */ 7587 panic("Invalid PR type %x", cdb->action); 7588 break; /* NOTREACHED */ 7589 } 7590 mtx_unlock(&softc->ctl_lock); 7591 7592 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7593 ctsio->be_move_done = ctl_config_move_done; 7594 7595 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7596 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7597 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7598 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7599 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7600 7601 ctl_datamove((union ctl_io *)ctsio); 7602 7603 return (CTL_RETVAL_COMPLETE); 7604} 7605 7606/* 7607 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7608 * it should return. 7609 */ 7610static int 7611ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7612 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7613 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7614 struct scsi_per_res_out_parms* param) 7615{ 7616 union ctl_ha_msg persis_io; 7617 int retval, i; 7618 int isc_retval; 7619 7620 retval = 0; 7621 7622 if (sa_res_key == 0) { 7623 mtx_lock(&softc->ctl_lock); 7624 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7625 /* validate scope and type */ 7626 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7627 SPR_LU_SCOPE) { 7628 mtx_unlock(&softc->ctl_lock); 7629 ctl_set_invalid_field(/*ctsio*/ ctsio, 7630 /*sks_valid*/ 1, 7631 /*command*/ 1, 7632 /*field*/ 2, 7633 /*bit_valid*/ 1, 7634 /*bit*/ 4); 7635 ctl_done((union ctl_io *)ctsio); 7636 return (1); 7637 } 7638 7639 if (type>8 || type==2 || type==4 || type==0) { 7640 mtx_unlock(&softc->ctl_lock); 7641 ctl_set_invalid_field(/*ctsio*/ ctsio, 7642 /*sks_valid*/ 1, 7643 /*command*/ 1, 7644 /*field*/ 2, 7645 /*bit_valid*/ 1, 7646 /*bit*/ 0); 7647 ctl_done((union ctl_io *)ctsio); 7648 return (1); 7649 } 7650 7651 /* temporarily unregister this nexus */ 7652 lun->per_res[residx].registered = 0; 7653 7654 /* 7655 * Unregister everybody else and build UA for 7656 * them 7657 */ 7658 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7659 if (lun->per_res[i].registered == 0) 7660 continue; 7661 7662 if (!persis_offset 7663 && i <CTL_MAX_INITIATORS) 7664 lun->pending_sense[i].ua_pending |= 7665 CTL_UA_REG_PREEMPT; 7666 else if (persis_offset 7667 && i >= persis_offset) 7668 lun->pending_sense[i-persis_offset 7669 ].ua_pending |= 7670 CTL_UA_REG_PREEMPT; 7671 lun->per_res[i].registered = 0; 7672 memset(&lun->per_res[i].res_key, 0, 7673 sizeof(struct scsi_per_res_key)); 7674 } 7675 lun->per_res[residx].registered = 1; 7676 lun->pr_key_count = 1; 7677 lun->res_type = type; 7678 if (lun->res_type != SPR_TYPE_WR_EX_AR 7679 && lun->res_type != SPR_TYPE_EX_AC_AR) 7680 lun->pr_res_idx = residx; 7681 7682 mtx_unlock(&softc->ctl_lock); 7683 /* send msg to other side */ 7684 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7685 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7686 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7687 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7688 persis_io.pr.pr_info.res_type = type; 7689 memcpy(persis_io.pr.pr_info.sa_res_key, 7690 param->serv_act_res_key, 7691 sizeof(param->serv_act_res_key)); 7692 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7693 &persis_io, sizeof(persis_io), 0)) > 7694 CTL_HA_STATUS_SUCCESS) { 7695 printf("CTL:Persis Out error returned " 7696 "from ctl_ha_msg_send %d\n", 7697 isc_retval); 7698 } 7699 } else { 7700 /* not all registrants */ 7701 mtx_unlock(&softc->ctl_lock); 7702 free(ctsio->kern_data_ptr, M_CTL); 7703 ctl_set_invalid_field(ctsio, 7704 /*sks_valid*/ 1, 7705 /*command*/ 0, 7706 /*field*/ 8, 7707 /*bit_valid*/ 0, 7708 /*bit*/ 0); 7709 ctl_done((union ctl_io *)ctsio); 7710 return (1); 7711 } 7712 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7713 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7714 int found = 0; 7715 7716 mtx_lock(&softc->ctl_lock); 7717 if (res_key == sa_res_key) { 7718 /* special case */ 7719 /* 7720 * The spec implies this is not good but doesn't 7721 * say what to do. There are two choices either 7722 * generate a res conflict or check condition 7723 * with illegal field in parameter data. Since 7724 * that is what is done when the sa_res_key is 7725 * zero I'll take that approach since this has 7726 * to do with the sa_res_key. 7727 */ 7728 mtx_unlock(&softc->ctl_lock); 7729 free(ctsio->kern_data_ptr, M_CTL); 7730 ctl_set_invalid_field(ctsio, 7731 /*sks_valid*/ 1, 7732 /*command*/ 0, 7733 /*field*/ 8, 7734 /*bit_valid*/ 0, 7735 /*bit*/ 0); 7736 ctl_done((union ctl_io *)ctsio); 7737 return (1); 7738 } 7739 7740 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7741 if (lun->per_res[i].registered 7742 && memcmp(param->serv_act_res_key, 7743 lun->per_res[i].res_key.key, 7744 sizeof(struct scsi_per_res_key)) != 0) 7745 continue; 7746 7747 found = 1; 7748 lun->per_res[i].registered = 0; 7749 memset(&lun->per_res[i].res_key, 0, 7750 sizeof(struct scsi_per_res_key)); 7751 lun->pr_key_count--; 7752 7753 if (!persis_offset 7754 && i < CTL_MAX_INITIATORS) 7755 lun->pending_sense[i].ua_pending |= 7756 CTL_UA_REG_PREEMPT; 7757 else if (persis_offset 7758 && i >= persis_offset) 7759 lun->pending_sense[i-persis_offset].ua_pending|= 7760 CTL_UA_REG_PREEMPT; 7761 } 7762 mtx_unlock(&softc->ctl_lock); 7763 if (!found) { 7764 free(ctsio->kern_data_ptr, M_CTL); 7765 ctl_set_reservation_conflict(ctsio); 7766 ctl_done((union ctl_io *)ctsio); 7767 return (CTL_RETVAL_COMPLETE); 7768 } 7769 /* send msg to other side */ 7770 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7771 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7772 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7773 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7774 persis_io.pr.pr_info.res_type = type; 7775 memcpy(persis_io.pr.pr_info.sa_res_key, 7776 param->serv_act_res_key, 7777 sizeof(param->serv_act_res_key)); 7778 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7779 &persis_io, sizeof(persis_io), 0)) > 7780 CTL_HA_STATUS_SUCCESS) { 7781 printf("CTL:Persis Out error returned from " 7782 "ctl_ha_msg_send %d\n", isc_retval); 7783 } 7784 } else { 7785 /* Reserved but not all registrants */ 7786 /* sa_res_key is res holder */ 7787 if (memcmp(param->serv_act_res_key, 7788 lun->per_res[lun->pr_res_idx].res_key.key, 7789 sizeof(struct scsi_per_res_key)) == 0) { 7790 /* validate scope and type */ 7791 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7792 SPR_LU_SCOPE) { 7793 ctl_set_invalid_field(/*ctsio*/ ctsio, 7794 /*sks_valid*/ 1, 7795 /*command*/ 1, 7796 /*field*/ 2, 7797 /*bit_valid*/ 1, 7798 /*bit*/ 4); 7799 ctl_done((union ctl_io *)ctsio); 7800 return (1); 7801 } 7802 7803 if (type>8 || type==2 || type==4 || type==0) { 7804 ctl_set_invalid_field(/*ctsio*/ ctsio, 7805 /*sks_valid*/ 1, 7806 /*command*/ 1, 7807 /*field*/ 2, 7808 /*bit_valid*/ 1, 7809 /*bit*/ 0); 7810 ctl_done((union ctl_io *)ctsio); 7811 return (1); 7812 } 7813 7814 /* 7815 * Do the following: 7816 * if sa_res_key != res_key remove all 7817 * registrants w/sa_res_key and generate UA 7818 * for these registrants(Registrations 7819 * Preempted) if it wasn't an exclusive 7820 * reservation generate UA(Reservations 7821 * Preempted) for all other registered nexuses 7822 * if the type has changed. Establish the new 7823 * reservation and holder. If res_key and 7824 * sa_res_key are the same do the above 7825 * except don't unregister the res holder. 7826 */ 7827 7828 /* 7829 * Temporarily unregister so it won't get 7830 * removed or UA generated 7831 */ 7832 lun->per_res[residx].registered = 0; 7833 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7834 if (lun->per_res[i].registered == 0) 7835 continue; 7836 7837 if (memcmp(param->serv_act_res_key, 7838 lun->per_res[i].res_key.key, 7839 sizeof(struct scsi_per_res_key)) == 0) { 7840 lun->per_res[i].registered = 0; 7841 memset(&lun->per_res[i].res_key, 7842 0, 7843 sizeof(struct scsi_per_res_key)); 7844 lun->pr_key_count--; 7845 7846 if (!persis_offset 7847 && i < CTL_MAX_INITIATORS) 7848 lun->pending_sense[i 7849 ].ua_pending |= 7850 CTL_UA_REG_PREEMPT; 7851 else if (persis_offset 7852 && i >= persis_offset) 7853 lun->pending_sense[ 7854 i-persis_offset].ua_pending |= 7855 CTL_UA_REG_PREEMPT; 7856 } else if (type != lun->res_type 7857 && (lun->res_type == SPR_TYPE_WR_EX_RO 7858 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7859 if (!persis_offset 7860 && i < CTL_MAX_INITIATORS) 7861 lun->pending_sense[i 7862 ].ua_pending |= 7863 CTL_UA_RES_RELEASE; 7864 else if (persis_offset 7865 && i >= persis_offset) 7866 lun->pending_sense[ 7867 i-persis_offset 7868 ].ua_pending |= 7869 CTL_UA_RES_RELEASE; 7870 } 7871 } 7872 lun->per_res[residx].registered = 1; 7873 lun->res_type = type; 7874 if (lun->res_type != SPR_TYPE_WR_EX_AR 7875 && lun->res_type != SPR_TYPE_EX_AC_AR) 7876 lun->pr_res_idx = residx; 7877 else 7878 lun->pr_res_idx = 7879 CTL_PR_ALL_REGISTRANTS; 7880 7881 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7882 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7883 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7884 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7885 persis_io.pr.pr_info.res_type = type; 7886 memcpy(persis_io.pr.pr_info.sa_res_key, 7887 param->serv_act_res_key, 7888 sizeof(param->serv_act_res_key)); 7889 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7890 &persis_io, sizeof(persis_io), 0)) > 7891 CTL_HA_STATUS_SUCCESS) { 7892 printf("CTL:Persis Out error returned " 7893 "from ctl_ha_msg_send %d\n", 7894 isc_retval); 7895 } 7896 } else { 7897 /* 7898 * sa_res_key is not the res holder just 7899 * remove registrants 7900 */ 7901 int found=0; 7902 mtx_lock(&softc->ctl_lock); 7903 7904 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7905 if (memcmp(param->serv_act_res_key, 7906 lun->per_res[i].res_key.key, 7907 sizeof(struct scsi_per_res_key)) != 0) 7908 continue; 7909 7910 found = 1; 7911 lun->per_res[i].registered = 0; 7912 memset(&lun->per_res[i].res_key, 0, 7913 sizeof(struct scsi_per_res_key)); 7914 lun->pr_key_count--; 7915 7916 if (!persis_offset 7917 && i < CTL_MAX_INITIATORS) 7918 lun->pending_sense[i].ua_pending |= 7919 CTL_UA_REG_PREEMPT; 7920 else if (persis_offset 7921 && i >= persis_offset) 7922 lun->pending_sense[ 7923 i-persis_offset].ua_pending |= 7924 CTL_UA_REG_PREEMPT; 7925 } 7926 7927 if (!found) { 7928 mtx_unlock(&softc->ctl_lock); 7929 free(ctsio->kern_data_ptr, M_CTL); 7930 ctl_set_reservation_conflict(ctsio); 7931 ctl_done((union ctl_io *)ctsio); 7932 return (1); 7933 } 7934 mtx_unlock(&softc->ctl_lock); 7935 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7936 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7937 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7938 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7939 persis_io.pr.pr_info.res_type = type; 7940 memcpy(persis_io.pr.pr_info.sa_res_key, 7941 param->serv_act_res_key, 7942 sizeof(param->serv_act_res_key)); 7943 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7944 &persis_io, sizeof(persis_io), 0)) > 7945 CTL_HA_STATUS_SUCCESS) { 7946 printf("CTL:Persis Out error returned " 7947 "from ctl_ha_msg_send %d\n", 7948 isc_retval); 7949 } 7950 } 7951 } 7952 7953 lun->PRGeneration++; 7954 7955 return (retval); 7956} 7957 7958static void 7959ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7960{ 7961 int i; 7962 7963 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7964 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7965 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 7966 msg->pr.pr_info.sa_res_key, 7967 sizeof(struct scsi_per_res_key)) != 0) { 7968 uint64_t sa_res_key; 7969 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7970 7971 if (sa_res_key == 0) { 7972 /* temporarily unregister this nexus */ 7973 lun->per_res[msg->pr.pr_info.residx].registered = 0; 7974 7975 /* 7976 * Unregister everybody else and build UA for 7977 * them 7978 */ 7979 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7980 if (lun->per_res[i].registered == 0) 7981 continue; 7982 7983 if (!persis_offset 7984 && i < CTL_MAX_INITIATORS) 7985 lun->pending_sense[i].ua_pending |= 7986 CTL_UA_REG_PREEMPT; 7987 else if (persis_offset && i >= persis_offset) 7988 lun->pending_sense[i - 7989 persis_offset].ua_pending |= 7990 CTL_UA_REG_PREEMPT; 7991 lun->per_res[i].registered = 0; 7992 memset(&lun->per_res[i].res_key, 0, 7993 sizeof(struct scsi_per_res_key)); 7994 } 7995 7996 lun->per_res[msg->pr.pr_info.residx].registered = 1; 7997 lun->pr_key_count = 1; 7998 lun->res_type = msg->pr.pr_info.res_type; 7999 if (lun->res_type != SPR_TYPE_WR_EX_AR 8000 && lun->res_type != SPR_TYPE_EX_AC_AR) 8001 lun->pr_res_idx = msg->pr.pr_info.residx; 8002 } else { 8003 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8004 if (memcmp(msg->pr.pr_info.sa_res_key, 8005 lun->per_res[i].res_key.key, 8006 sizeof(struct scsi_per_res_key)) != 0) 8007 continue; 8008 8009 lun->per_res[i].registered = 0; 8010 memset(&lun->per_res[i].res_key, 0, 8011 sizeof(struct scsi_per_res_key)); 8012 lun->pr_key_count--; 8013 8014 if (!persis_offset 8015 && i < persis_offset) 8016 lun->pending_sense[i].ua_pending |= 8017 CTL_UA_REG_PREEMPT; 8018 else if (persis_offset 8019 && i >= persis_offset) 8020 lun->pending_sense[i - 8021 persis_offset].ua_pending |= 8022 CTL_UA_REG_PREEMPT; 8023 } 8024 } 8025 } else { 8026 /* 8027 * Temporarily unregister so it won't get removed 8028 * or UA generated 8029 */ 8030 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8031 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8032 if (lun->per_res[i].registered == 0) 8033 continue; 8034 8035 if (memcmp(msg->pr.pr_info.sa_res_key, 8036 lun->per_res[i].res_key.key, 8037 sizeof(struct scsi_per_res_key)) == 0) { 8038 lun->per_res[i].registered = 0; 8039 memset(&lun->per_res[i].res_key, 0, 8040 sizeof(struct scsi_per_res_key)); 8041 lun->pr_key_count--; 8042 if (!persis_offset 8043 && i < CTL_MAX_INITIATORS) 8044 lun->pending_sense[i].ua_pending |= 8045 CTL_UA_REG_PREEMPT; 8046 else if (persis_offset 8047 && i >= persis_offset) 8048 lun->pending_sense[i - 8049 persis_offset].ua_pending |= 8050 CTL_UA_REG_PREEMPT; 8051 } else if (msg->pr.pr_info.res_type != lun->res_type 8052 && (lun->res_type == SPR_TYPE_WR_EX_RO 8053 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8054 if (!persis_offset 8055 && i < persis_offset) 8056 lun->pending_sense[i 8057 ].ua_pending |= 8058 CTL_UA_RES_RELEASE; 8059 else if (persis_offset 8060 && i >= persis_offset) 8061 lun->pending_sense[i - 8062 persis_offset].ua_pending |= 8063 CTL_UA_RES_RELEASE; 8064 } 8065 } 8066 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8067 lun->res_type = msg->pr.pr_info.res_type; 8068 if (lun->res_type != SPR_TYPE_WR_EX_AR 8069 && lun->res_type != SPR_TYPE_EX_AC_AR) 8070 lun->pr_res_idx = msg->pr.pr_info.residx; 8071 else 8072 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8073 } 8074 lun->PRGeneration++; 8075 8076} 8077 8078 8079int 8080ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8081{ 8082 int retval; 8083 int isc_retval; 8084 u_int32_t param_len; 8085 struct scsi_per_res_out *cdb; 8086 struct ctl_lun *lun; 8087 struct scsi_per_res_out_parms* param; 8088 struct ctl_softc *softc; 8089 uint32_t residx; 8090 uint64_t res_key, sa_res_key; 8091 uint8_t type; 8092 union ctl_ha_msg persis_io; 8093 int i; 8094 8095 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8096 8097 retval = CTL_RETVAL_COMPLETE; 8098 8099 softc = control_softc; 8100 8101 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8102 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8103 8104 /* 8105 * We only support whole-LUN scope. The scope & type are ignored for 8106 * register, register and ignore existing key and clear. 8107 * We sometimes ignore scope and type on preempts too!! 8108 * Verify reservation type here as well. 8109 */ 8110 type = cdb->scope_type & SPR_TYPE_MASK; 8111 if ((cdb->action == SPRO_RESERVE) 8112 || (cdb->action == SPRO_RELEASE)) { 8113 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8114 ctl_set_invalid_field(/*ctsio*/ ctsio, 8115 /*sks_valid*/ 1, 8116 /*command*/ 1, 8117 /*field*/ 2, 8118 /*bit_valid*/ 1, 8119 /*bit*/ 4); 8120 ctl_done((union ctl_io *)ctsio); 8121 return (CTL_RETVAL_COMPLETE); 8122 } 8123 8124 if (type>8 || type==2 || type==4 || type==0) { 8125 ctl_set_invalid_field(/*ctsio*/ ctsio, 8126 /*sks_valid*/ 1, 8127 /*command*/ 1, 8128 /*field*/ 2, 8129 /*bit_valid*/ 1, 8130 /*bit*/ 0); 8131 ctl_done((union ctl_io *)ctsio); 8132 return (CTL_RETVAL_COMPLETE); 8133 } 8134 } 8135 8136 switch (cdb->action & SPRO_ACTION_MASK) { 8137 case SPRO_REGISTER: 8138 case SPRO_RESERVE: 8139 case SPRO_RELEASE: 8140 case SPRO_CLEAR: 8141 case SPRO_PREEMPT: 8142 case SPRO_REG_IGNO: 8143 break; 8144 case SPRO_REG_MOVE: 8145 case SPRO_PRE_ABO: 8146 default: 8147 ctl_set_invalid_field(/*ctsio*/ ctsio, 8148 /*sks_valid*/ 1, 8149 /*command*/ 1, 8150 /*field*/ 1, 8151 /*bit_valid*/ 1, 8152 /*bit*/ 0); 8153 ctl_done((union ctl_io *)ctsio); 8154 return (CTL_RETVAL_COMPLETE); 8155 break; /* NOTREACHED */ 8156 } 8157 8158 param_len = scsi_4btoul(cdb->length); 8159 8160 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8161 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8162 ctsio->kern_data_len = param_len; 8163 ctsio->kern_total_len = param_len; 8164 ctsio->kern_data_resid = 0; 8165 ctsio->kern_rel_offset = 0; 8166 ctsio->kern_sg_entries = 0; 8167 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8168 ctsio->be_move_done = ctl_config_move_done; 8169 ctl_datamove((union ctl_io *)ctsio); 8170 8171 return (CTL_RETVAL_COMPLETE); 8172 } 8173 8174 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8175 8176 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8177 res_key = scsi_8btou64(param->res_key.key); 8178 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8179 8180 /* 8181 * Validate the reservation key here except for SPRO_REG_IGNO 8182 * This must be done for all other service actions 8183 */ 8184 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8185 mtx_lock(&softc->ctl_lock); 8186 if (lun->per_res[residx].registered) { 8187 if (memcmp(param->res_key.key, 8188 lun->per_res[residx].res_key.key, 8189 ctl_min(sizeof(param->res_key), 8190 sizeof(lun->per_res[residx].res_key))) != 0) { 8191 /* 8192 * The current key passed in doesn't match 8193 * the one the initiator previously 8194 * registered. 8195 */ 8196 mtx_unlock(&softc->ctl_lock); 8197 free(ctsio->kern_data_ptr, M_CTL); 8198 ctl_set_reservation_conflict(ctsio); 8199 ctl_done((union ctl_io *)ctsio); 8200 return (CTL_RETVAL_COMPLETE); 8201 } 8202 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8203 /* 8204 * We are not registered 8205 */ 8206 mtx_unlock(&softc->ctl_lock); 8207 free(ctsio->kern_data_ptr, M_CTL); 8208 ctl_set_reservation_conflict(ctsio); 8209 ctl_done((union ctl_io *)ctsio); 8210 return (CTL_RETVAL_COMPLETE); 8211 } else if (res_key != 0) { 8212 /* 8213 * We are not registered and trying to register but 8214 * the register key isn't zero. 8215 */ 8216 mtx_unlock(&softc->ctl_lock); 8217 free(ctsio->kern_data_ptr, M_CTL); 8218 ctl_set_reservation_conflict(ctsio); 8219 ctl_done((union ctl_io *)ctsio); 8220 return (CTL_RETVAL_COMPLETE); 8221 } 8222 mtx_unlock(&softc->ctl_lock); 8223 } 8224 8225 switch (cdb->action & SPRO_ACTION_MASK) { 8226 case SPRO_REGISTER: 8227 case SPRO_REG_IGNO: { 8228 8229#if 0 8230 printf("Registration received\n"); 8231#endif 8232 8233 /* 8234 * We don't support any of these options, as we report in 8235 * the read capabilities request (see 8236 * ctl_persistent_reserve_in(), above). 8237 */ 8238 if ((param->flags & SPR_SPEC_I_PT) 8239 || (param->flags & SPR_ALL_TG_PT) 8240 || (param->flags & SPR_APTPL)) { 8241 int bit_ptr; 8242 8243 if (param->flags & SPR_APTPL) 8244 bit_ptr = 0; 8245 else if (param->flags & SPR_ALL_TG_PT) 8246 bit_ptr = 2; 8247 else /* SPR_SPEC_I_PT */ 8248 bit_ptr = 3; 8249 8250 free(ctsio->kern_data_ptr, M_CTL); 8251 ctl_set_invalid_field(ctsio, 8252 /*sks_valid*/ 1, 8253 /*command*/ 0, 8254 /*field*/ 20, 8255 /*bit_valid*/ 1, 8256 /*bit*/ bit_ptr); 8257 ctl_done((union ctl_io *)ctsio); 8258 return (CTL_RETVAL_COMPLETE); 8259 } 8260 8261 mtx_lock(&softc->ctl_lock); 8262 8263 /* 8264 * The initiator wants to clear the 8265 * key/unregister. 8266 */ 8267 if (sa_res_key == 0) { 8268 if ((res_key == 0 8269 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8270 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8271 && !lun->per_res[residx].registered)) { 8272 mtx_unlock(&softc->ctl_lock); 8273 goto done; 8274 } 8275 8276 lun->per_res[residx].registered = 0; 8277 memset(&lun->per_res[residx].res_key, 8278 0, sizeof(lun->per_res[residx].res_key)); 8279 lun->pr_key_count--; 8280 8281 if (residx == lun->pr_res_idx) { 8282 lun->flags &= ~CTL_LUN_PR_RESERVED; 8283 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8284 8285 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8286 || lun->res_type == SPR_TYPE_EX_AC_RO) 8287 && lun->pr_key_count) { 8288 /* 8289 * If the reservation is a registrants 8290 * only type we need to generate a UA 8291 * for other registered inits. The 8292 * sense code should be RESERVATIONS 8293 * RELEASED 8294 */ 8295 8296 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8297 if (lun->per_res[ 8298 i+persis_offset].registered 8299 == 0) 8300 continue; 8301 lun->pending_sense[i 8302 ].ua_pending |= 8303 CTL_UA_RES_RELEASE; 8304 } 8305 } 8306 lun->res_type = 0; 8307 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8308 if (lun->pr_key_count==0) { 8309 lun->flags &= ~CTL_LUN_PR_RESERVED; 8310 lun->res_type = 0; 8311 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8312 } 8313 } 8314 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8315 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8316 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8317 persis_io.pr.pr_info.residx = residx; 8318 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8319 &persis_io, sizeof(persis_io), 0 )) > 8320 CTL_HA_STATUS_SUCCESS) { 8321 printf("CTL:Persis Out error returned from " 8322 "ctl_ha_msg_send %d\n", isc_retval); 8323 } 8324 mtx_unlock(&softc->ctl_lock); 8325 } else /* sa_res_key != 0 */ { 8326 8327 /* 8328 * If we aren't registered currently then increment 8329 * the key count and set the registered flag. 8330 */ 8331 if (!lun->per_res[residx].registered) { 8332 lun->pr_key_count++; 8333 lun->per_res[residx].registered = 1; 8334 } 8335 8336 memcpy(&lun->per_res[residx].res_key, 8337 param->serv_act_res_key, 8338 ctl_min(sizeof(param->serv_act_res_key), 8339 sizeof(lun->per_res[residx].res_key))); 8340 8341 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8342 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8343 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8344 persis_io.pr.pr_info.residx = residx; 8345 memcpy(persis_io.pr.pr_info.sa_res_key, 8346 param->serv_act_res_key, 8347 sizeof(param->serv_act_res_key)); 8348 mtx_unlock(&softc->ctl_lock); 8349 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8350 &persis_io, sizeof(persis_io), 0)) > 8351 CTL_HA_STATUS_SUCCESS) { 8352 printf("CTL:Persis Out error returned from " 8353 "ctl_ha_msg_send %d\n", isc_retval); 8354 } 8355 } 8356 lun->PRGeneration++; 8357 8358 break; 8359 } 8360 case SPRO_RESERVE: 8361#if 0 8362 printf("Reserve executed type %d\n", type); 8363#endif 8364 mtx_lock(&softc->ctl_lock); 8365 if (lun->flags & CTL_LUN_PR_RESERVED) { 8366 /* 8367 * if this isn't the reservation holder and it's 8368 * not a "all registrants" type or if the type is 8369 * different then we have a conflict 8370 */ 8371 if ((lun->pr_res_idx != residx 8372 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8373 || lun->res_type != type) { 8374 mtx_unlock(&softc->ctl_lock); 8375 free(ctsio->kern_data_ptr, M_CTL); 8376 ctl_set_reservation_conflict(ctsio); 8377 ctl_done((union ctl_io *)ctsio); 8378 return (CTL_RETVAL_COMPLETE); 8379 } 8380 mtx_unlock(&softc->ctl_lock); 8381 } else /* create a reservation */ { 8382 /* 8383 * If it's not an "all registrants" type record 8384 * reservation holder 8385 */ 8386 if (type != SPR_TYPE_WR_EX_AR 8387 && type != SPR_TYPE_EX_AC_AR) 8388 lun->pr_res_idx = residx; /* Res holder */ 8389 else 8390 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8391 8392 lun->flags |= CTL_LUN_PR_RESERVED; 8393 lun->res_type = type; 8394 8395 mtx_unlock(&softc->ctl_lock); 8396 8397 /* send msg to other side */ 8398 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8399 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8400 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8401 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8402 persis_io.pr.pr_info.res_type = type; 8403 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8404 &persis_io, sizeof(persis_io), 0)) > 8405 CTL_HA_STATUS_SUCCESS) { 8406 printf("CTL:Persis Out error returned from " 8407 "ctl_ha_msg_send %d\n", isc_retval); 8408 } 8409 } 8410 break; 8411 8412 case SPRO_RELEASE: 8413 mtx_lock(&softc->ctl_lock); 8414 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8415 /* No reservation exists return good status */ 8416 mtx_unlock(&softc->ctl_lock); 8417 goto done; 8418 } 8419 /* 8420 * Is this nexus a reservation holder? 8421 */ 8422 if (lun->pr_res_idx != residx 8423 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8424 /* 8425 * not a res holder return good status but 8426 * do nothing 8427 */ 8428 mtx_unlock(&softc->ctl_lock); 8429 goto done; 8430 } 8431 8432 if (lun->res_type != type) { 8433 mtx_unlock(&softc->ctl_lock); 8434 free(ctsio->kern_data_ptr, M_CTL); 8435 ctl_set_illegal_pr_release(ctsio); 8436 ctl_done((union ctl_io *)ctsio); 8437 return (CTL_RETVAL_COMPLETE); 8438 } 8439 8440 /* okay to release */ 8441 lun->flags &= ~CTL_LUN_PR_RESERVED; 8442 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8443 lun->res_type = 0; 8444 8445 /* 8446 * if this isn't an exclusive access 8447 * res generate UA for all other 8448 * registrants. 8449 */ 8450 if (type != SPR_TYPE_EX_AC 8451 && type != SPR_TYPE_WR_EX) { 8452 /* 8453 * temporarily unregister so we don't generate UA 8454 */ 8455 lun->per_res[residx].registered = 0; 8456 8457 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8458 if (lun->per_res[i+persis_offset].registered 8459 == 0) 8460 continue; 8461 lun->pending_sense[i].ua_pending |= 8462 CTL_UA_RES_RELEASE; 8463 } 8464 8465 lun->per_res[residx].registered = 1; 8466 } 8467 mtx_unlock(&softc->ctl_lock); 8468 /* Send msg to other side */ 8469 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8470 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8471 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8472 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8473 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8474 printf("CTL:Persis Out error returned from " 8475 "ctl_ha_msg_send %d\n", isc_retval); 8476 } 8477 break; 8478 8479 case SPRO_CLEAR: 8480 /* send msg to other side */ 8481 8482 mtx_lock(&softc->ctl_lock); 8483 lun->flags &= ~CTL_LUN_PR_RESERVED; 8484 lun->res_type = 0; 8485 lun->pr_key_count = 0; 8486 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8487 8488 8489 memset(&lun->per_res[residx].res_key, 8490 0, sizeof(lun->per_res[residx].res_key)); 8491 lun->per_res[residx].registered = 0; 8492 8493 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8494 if (lun->per_res[i].registered) { 8495 if (!persis_offset && i < CTL_MAX_INITIATORS) 8496 lun->pending_sense[i].ua_pending |= 8497 CTL_UA_RES_PREEMPT; 8498 else if (persis_offset && i >= persis_offset) 8499 lun->pending_sense[i-persis_offset 8500 ].ua_pending |= CTL_UA_RES_PREEMPT; 8501 8502 memset(&lun->per_res[i].res_key, 8503 0, sizeof(struct scsi_per_res_key)); 8504 lun->per_res[i].registered = 0; 8505 } 8506 lun->PRGeneration++; 8507 mtx_unlock(&softc->ctl_lock); 8508 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8509 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8510 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8511 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8512 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8513 printf("CTL:Persis Out error returned from " 8514 "ctl_ha_msg_send %d\n", isc_retval); 8515 } 8516 break; 8517 8518 case SPRO_PREEMPT: { 8519 int nretval; 8520 8521 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8522 residx, ctsio, cdb, param); 8523 if (nretval != 0) 8524 return (CTL_RETVAL_COMPLETE); 8525 break; 8526 } 8527 case SPRO_REG_MOVE: 8528 case SPRO_PRE_ABO: 8529 default: 8530 free(ctsio->kern_data_ptr, M_CTL); 8531 ctl_set_invalid_field(/*ctsio*/ ctsio, 8532 /*sks_valid*/ 1, 8533 /*command*/ 1, 8534 /*field*/ 1, 8535 /*bit_valid*/ 1, 8536 /*bit*/ 0); 8537 ctl_done((union ctl_io *)ctsio); 8538 return (CTL_RETVAL_COMPLETE); 8539 break; /* NOTREACHED */ 8540 } 8541 8542done: 8543 free(ctsio->kern_data_ptr, M_CTL); 8544 ctl_set_success(ctsio); 8545 ctl_done((union ctl_io *)ctsio); 8546 8547 return (retval); 8548} 8549 8550/* 8551 * This routine is for handling a message from the other SC pertaining to 8552 * persistent reserve out. All the error checking will have been done 8553 * so only perorming the action need be done here to keep the two 8554 * in sync. 8555 */ 8556static void 8557ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8558{ 8559 struct ctl_lun *lun; 8560 struct ctl_softc *softc; 8561 int i; 8562 uint32_t targ_lun; 8563 8564 softc = control_softc; 8565 8566 mtx_lock(&softc->ctl_lock); 8567 8568 targ_lun = msg->hdr.nexus.targ_lun; 8569 if (msg->hdr.nexus.lun_map_fn != NULL) 8570 targ_lun = msg->hdr.nexus.lun_map_fn(msg->hdr.nexus.lun_map_arg, targ_lun); 8571 lun = softc->ctl_luns[targ_lun]; 8572 switch(msg->pr.pr_info.action) { 8573 case CTL_PR_REG_KEY: 8574 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8575 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8576 lun->pr_key_count++; 8577 } 8578 lun->PRGeneration++; 8579 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8580 msg->pr.pr_info.sa_res_key, 8581 sizeof(struct scsi_per_res_key)); 8582 break; 8583 8584 case CTL_PR_UNREG_KEY: 8585 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8586 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8587 0, sizeof(struct scsi_per_res_key)); 8588 lun->pr_key_count--; 8589 8590 /* XXX Need to see if the reservation has been released */ 8591 /* if so do we need to generate UA? */ 8592 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8593 lun->flags &= ~CTL_LUN_PR_RESERVED; 8594 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8595 8596 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8597 || lun->res_type == SPR_TYPE_EX_AC_RO) 8598 && lun->pr_key_count) { 8599 /* 8600 * If the reservation is a registrants 8601 * only type we need to generate a UA 8602 * for other registered inits. The 8603 * sense code should be RESERVATIONS 8604 * RELEASED 8605 */ 8606 8607 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8608 if (lun->per_res[i+ 8609 persis_offset].registered == 0) 8610 continue; 8611 8612 lun->pending_sense[i 8613 ].ua_pending |= 8614 CTL_UA_RES_RELEASE; 8615 } 8616 } 8617 lun->res_type = 0; 8618 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8619 if (lun->pr_key_count==0) { 8620 lun->flags &= ~CTL_LUN_PR_RESERVED; 8621 lun->res_type = 0; 8622 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8623 } 8624 } 8625 lun->PRGeneration++; 8626 break; 8627 8628 case CTL_PR_RESERVE: 8629 lun->flags |= CTL_LUN_PR_RESERVED; 8630 lun->res_type = msg->pr.pr_info.res_type; 8631 lun->pr_res_idx = msg->pr.pr_info.residx; 8632 8633 break; 8634 8635 case CTL_PR_RELEASE: 8636 /* 8637 * if this isn't an exclusive access res generate UA for all 8638 * other registrants. 8639 */ 8640 if (lun->res_type != SPR_TYPE_EX_AC 8641 && lun->res_type != SPR_TYPE_WR_EX) { 8642 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8643 if (lun->per_res[i+persis_offset].registered) 8644 lun->pending_sense[i].ua_pending |= 8645 CTL_UA_RES_RELEASE; 8646 } 8647 8648 lun->flags &= ~CTL_LUN_PR_RESERVED; 8649 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8650 lun->res_type = 0; 8651 break; 8652 8653 case CTL_PR_PREEMPT: 8654 ctl_pro_preempt_other(lun, msg); 8655 break; 8656 case CTL_PR_CLEAR: 8657 lun->flags &= ~CTL_LUN_PR_RESERVED; 8658 lun->res_type = 0; 8659 lun->pr_key_count = 0; 8660 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8661 8662 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8663 if (lun->per_res[i].registered == 0) 8664 continue; 8665 if (!persis_offset 8666 && i < CTL_MAX_INITIATORS) 8667 lun->pending_sense[i].ua_pending |= 8668 CTL_UA_RES_PREEMPT; 8669 else if (persis_offset 8670 && i >= persis_offset) 8671 lun->pending_sense[i-persis_offset].ua_pending|= 8672 CTL_UA_RES_PREEMPT; 8673 memset(&lun->per_res[i].res_key, 0, 8674 sizeof(struct scsi_per_res_key)); 8675 lun->per_res[i].registered = 0; 8676 } 8677 lun->PRGeneration++; 8678 break; 8679 } 8680 8681 mtx_unlock(&softc->ctl_lock); 8682} 8683 8684int 8685ctl_read_write(struct ctl_scsiio *ctsio) 8686{ 8687 struct ctl_lun *lun; 8688 struct ctl_lba_len_flags *lbalen; 8689 uint64_t lba; 8690 uint32_t num_blocks; 8691 int reladdr, fua, dpo, ebp; 8692 int retval; 8693 int isread; 8694 8695 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8696 8697 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8698 8699 reladdr = 0; 8700 fua = 0; 8701 dpo = 0; 8702 ebp = 0; 8703 8704 retval = CTL_RETVAL_COMPLETE; 8705 8706 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8707 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8708 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8709 uint32_t residx; 8710 8711 /* 8712 * XXX KDM need a lock here. 8713 */ 8714 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8715 if ((lun->res_type == SPR_TYPE_EX_AC 8716 && residx != lun->pr_res_idx) 8717 || ((lun->res_type == SPR_TYPE_EX_AC_RO 8718 || lun->res_type == SPR_TYPE_EX_AC_AR) 8719 && !lun->per_res[residx].registered)) { 8720 ctl_set_reservation_conflict(ctsio); 8721 ctl_done((union ctl_io *)ctsio); 8722 return (CTL_RETVAL_COMPLETE); 8723 } 8724 } 8725 8726 switch (ctsio->cdb[0]) { 8727 case READ_6: 8728 case WRITE_6: { 8729 struct scsi_rw_6 *cdb; 8730 8731 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8732 8733 lba = scsi_3btoul(cdb->addr); 8734 /* only 5 bits are valid in the most significant address byte */ 8735 lba &= 0x1fffff; 8736 num_blocks = cdb->length; 8737 /* 8738 * This is correct according to SBC-2. 8739 */ 8740 if (num_blocks == 0) 8741 num_blocks = 256; 8742 break; 8743 } 8744 case READ_10: 8745 case WRITE_10: { 8746 struct scsi_rw_10 *cdb; 8747 8748 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8749 8750 if (cdb->byte2 & SRW10_RELADDR) 8751 reladdr = 1; 8752 if (cdb->byte2 & SRW10_FUA) 8753 fua = 1; 8754 if (cdb->byte2 & SRW10_DPO) 8755 dpo = 1; 8756 8757 if ((cdb->opcode == WRITE_10) 8758 && (cdb->byte2 & SRW10_EBP)) 8759 ebp = 1; 8760 8761 lba = scsi_4btoul(cdb->addr); 8762 num_blocks = scsi_2btoul(cdb->length); 8763 break; 8764 } 8765 case WRITE_VERIFY_10: { 8766 struct scsi_write_verify_10 *cdb; 8767 8768 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8769 8770 /* 8771 * XXX KDM we should do actual write verify support at some 8772 * point. This is obviously fake, we're just translating 8773 * things to a write. So we don't even bother checking the 8774 * BYTCHK field, since we don't do any verification. If 8775 * the user asks for it, we'll just pretend we did it. 8776 */ 8777 if (cdb->byte2 & SWV_DPO) 8778 dpo = 1; 8779 8780 lba = scsi_4btoul(cdb->addr); 8781 num_blocks = scsi_2btoul(cdb->length); 8782 break; 8783 } 8784 case READ_12: 8785 case WRITE_12: { 8786 struct scsi_rw_12 *cdb; 8787 8788 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8789 8790 if (cdb->byte2 & SRW12_RELADDR) 8791 reladdr = 1; 8792 if (cdb->byte2 & SRW12_FUA) 8793 fua = 1; 8794 if (cdb->byte2 & SRW12_DPO) 8795 dpo = 1; 8796 lba = scsi_4btoul(cdb->addr); 8797 num_blocks = scsi_4btoul(cdb->length); 8798 break; 8799 } 8800 case WRITE_VERIFY_12: { 8801 struct scsi_write_verify_12 *cdb; 8802 8803 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8804 8805 if (cdb->byte2 & SWV_DPO) 8806 dpo = 1; 8807 8808 lba = scsi_4btoul(cdb->addr); 8809 num_blocks = scsi_4btoul(cdb->length); 8810 8811 break; 8812 } 8813 case READ_16: 8814 case WRITE_16: { 8815 struct scsi_rw_16 *cdb; 8816 8817 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8818 8819 if (cdb->byte2 & SRW12_RELADDR) 8820 reladdr = 1; 8821 if (cdb->byte2 & SRW12_FUA) 8822 fua = 1; 8823 if (cdb->byte2 & SRW12_DPO) 8824 dpo = 1; 8825 8826 lba = scsi_8btou64(cdb->addr); 8827 num_blocks = scsi_4btoul(cdb->length); 8828 break; 8829 } 8830 case WRITE_VERIFY_16: { 8831 struct scsi_write_verify_16 *cdb; 8832 8833 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8834 8835 if (cdb->byte2 & SWV_DPO) 8836 dpo = 1; 8837 8838 lba = scsi_8btou64(cdb->addr); 8839 num_blocks = scsi_4btoul(cdb->length); 8840 break; 8841 } 8842 default: 8843 /* 8844 * We got a command we don't support. This shouldn't 8845 * happen, commands should be filtered out above us. 8846 */ 8847 ctl_set_invalid_opcode(ctsio); 8848 ctl_done((union ctl_io *)ctsio); 8849 8850 return (CTL_RETVAL_COMPLETE); 8851 break; /* NOTREACHED */ 8852 } 8853 8854 /* 8855 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8856 * interesting for us, but if RAIDCore is in write-back mode, 8857 * getting it to do write-through for a particular transaction may 8858 * not be possible. 8859 */ 8860 /* 8861 * We don't support relative addressing. That also requires 8862 * supporting linked commands, which we don't do. 8863 */ 8864 if (reladdr != 0) { 8865 ctl_set_invalid_field(ctsio, 8866 /*sks_valid*/ 1, 8867 /*command*/ 1, 8868 /*field*/ 1, 8869 /*bit_valid*/ 1, 8870 /*bit*/ 0); 8871 ctl_done((union ctl_io *)ctsio); 8872 return (CTL_RETVAL_COMPLETE); 8873 } 8874 8875 /* 8876 * The first check is to make sure we're in bounds, the second 8877 * check is to catch wrap-around problems. If the lba + num blocks 8878 * is less than the lba, then we've wrapped around and the block 8879 * range is invalid anyway. 8880 */ 8881 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8882 || ((lba + num_blocks) < lba)) { 8883 ctl_set_lba_out_of_range(ctsio); 8884 ctl_done((union ctl_io *)ctsio); 8885 return (CTL_RETVAL_COMPLETE); 8886 } 8887 8888 /* 8889 * According to SBC-3, a transfer length of 0 is not an error. 8890 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8891 * translates to 256 blocks for those commands. 8892 */ 8893 if (num_blocks == 0) { 8894 ctl_set_success(ctsio); 8895 ctl_done((union ctl_io *)ctsio); 8896 return (CTL_RETVAL_COMPLETE); 8897 } 8898 8899 lbalen = (struct ctl_lba_len_flags *) 8900 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8901 lbalen->lba = lba; 8902 lbalen->len = num_blocks; 8903 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 8904 8905 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8906 ctsio->kern_rel_offset = 0; 8907 8908 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8909 8910 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8911 8912 return (retval); 8913} 8914 8915static int 8916ctl_cnw_cont(union ctl_io *io) 8917{ 8918 struct ctl_scsiio *ctsio; 8919 struct ctl_lun *lun; 8920 struct ctl_lba_len_flags *lbalen; 8921 int retval; 8922 8923 ctsio = &io->scsiio; 8924 ctsio->io_hdr.status = CTL_STATUS_NONE; 8925 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8926 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8927 lbalen = (struct ctl_lba_len_flags *) 8928 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8929 lbalen->flags = CTL_LLF_WRITE; 8930 8931 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8932 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8933 return (retval); 8934} 8935 8936int 8937ctl_cnw(struct ctl_scsiio *ctsio) 8938{ 8939 struct ctl_lun *lun; 8940 struct ctl_lba_len_flags *lbalen; 8941 uint64_t lba; 8942 uint32_t num_blocks; 8943 int fua, dpo; 8944 int retval; 8945 8946 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8947 8948 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8949 8950 fua = 0; 8951 dpo = 0; 8952 8953 retval = CTL_RETVAL_COMPLETE; 8954 8955 switch (ctsio->cdb[0]) { 8956 case COMPARE_AND_WRITE: { 8957 struct scsi_compare_and_write *cdb; 8958 8959 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8960 8961 if (cdb->byte2 & SRW10_FUA) 8962 fua = 1; 8963 if (cdb->byte2 & SRW10_DPO) 8964 dpo = 1; 8965 lba = scsi_8btou64(cdb->addr); 8966 num_blocks = cdb->length; 8967 break; 8968 } 8969 default: 8970 /* 8971 * We got a command we don't support. This shouldn't 8972 * happen, commands should be filtered out above us. 8973 */ 8974 ctl_set_invalid_opcode(ctsio); 8975 ctl_done((union ctl_io *)ctsio); 8976 8977 return (CTL_RETVAL_COMPLETE); 8978 break; /* NOTREACHED */ 8979 } 8980 8981 /* 8982 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8983 * interesting for us, but if RAIDCore is in write-back mode, 8984 * getting it to do write-through for a particular transaction may 8985 * not be possible. 8986 */ 8987 8988 /* 8989 * The first check is to make sure we're in bounds, the second 8990 * check is to catch wrap-around problems. If the lba + num blocks 8991 * is less than the lba, then we've wrapped around and the block 8992 * range is invalid anyway. 8993 */ 8994 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8995 || ((lba + num_blocks) < lba)) { 8996 ctl_set_lba_out_of_range(ctsio); 8997 ctl_done((union ctl_io *)ctsio); 8998 return (CTL_RETVAL_COMPLETE); 8999 } 9000 9001 /* 9002 * According to SBC-3, a transfer length of 0 is not an error. 9003 */ 9004 if (num_blocks == 0) { 9005 ctl_set_success(ctsio); 9006 ctl_done((union ctl_io *)ctsio); 9007 return (CTL_RETVAL_COMPLETE); 9008 } 9009 9010 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9011 ctsio->kern_rel_offset = 0; 9012 9013 /* 9014 * Set the IO_CONT flag, so that if this I/O gets passed to 9015 * ctl_data_submit_done(), it'll get passed back to 9016 * ctl_ctl_cnw_cont() for further processing. 9017 */ 9018 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9019 ctsio->io_cont = ctl_cnw_cont; 9020 9021 lbalen = (struct ctl_lba_len_flags *) 9022 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9023 lbalen->lba = lba; 9024 lbalen->len = num_blocks; 9025 lbalen->flags = CTL_LLF_COMPARE; 9026 9027 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9028 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9029 return (retval); 9030} 9031 9032int 9033ctl_verify(struct ctl_scsiio *ctsio) 9034{ 9035 struct ctl_lun *lun; 9036 struct ctl_lba_len_flags *lbalen; 9037 uint64_t lba; 9038 uint32_t num_blocks; 9039 int bytchk, dpo; 9040 int retval; 9041 9042 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9043 9044 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9045 9046 bytchk = 0; 9047 dpo = 0; 9048 retval = CTL_RETVAL_COMPLETE; 9049 9050 switch (ctsio->cdb[0]) { 9051 case VERIFY_10: { 9052 struct scsi_verify_10 *cdb; 9053 9054 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9055 if (cdb->byte2 & SVFY_BYTCHK) 9056 bytchk = 1; 9057 if (cdb->byte2 & SVFY_DPO) 9058 dpo = 1; 9059 lba = scsi_4btoul(cdb->addr); 9060 num_blocks = scsi_2btoul(cdb->length); 9061 break; 9062 } 9063 case VERIFY_12: { 9064 struct scsi_verify_12 *cdb; 9065 9066 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9067 if (cdb->byte2 & SVFY_BYTCHK) 9068 bytchk = 1; 9069 if (cdb->byte2 & SVFY_DPO) 9070 dpo = 1; 9071 lba = scsi_4btoul(cdb->addr); 9072 num_blocks = scsi_4btoul(cdb->length); 9073 break; 9074 } 9075 case VERIFY_16: { 9076 struct scsi_rw_16 *cdb; 9077 9078 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9079 if (cdb->byte2 & SVFY_BYTCHK) 9080 bytchk = 1; 9081 if (cdb->byte2 & SVFY_DPO) 9082 dpo = 1; 9083 lba = scsi_8btou64(cdb->addr); 9084 num_blocks = scsi_4btoul(cdb->length); 9085 break; 9086 } 9087 default: 9088 /* 9089 * We got a command we don't support. This shouldn't 9090 * happen, commands should be filtered out above us. 9091 */ 9092 ctl_set_invalid_opcode(ctsio); 9093 ctl_done((union ctl_io *)ctsio); 9094 return (CTL_RETVAL_COMPLETE); 9095 } 9096 9097 /* 9098 * The first check is to make sure we're in bounds, the second 9099 * check is to catch wrap-around problems. If the lba + num blocks 9100 * is less than the lba, then we've wrapped around and the block 9101 * range is invalid anyway. 9102 */ 9103 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9104 || ((lba + num_blocks) < lba)) { 9105 ctl_set_lba_out_of_range(ctsio); 9106 ctl_done((union ctl_io *)ctsio); 9107 return (CTL_RETVAL_COMPLETE); 9108 } 9109 9110 /* 9111 * According to SBC-3, a transfer length of 0 is not an error. 9112 */ 9113 if (num_blocks == 0) { 9114 ctl_set_success(ctsio); 9115 ctl_done((union ctl_io *)ctsio); 9116 return (CTL_RETVAL_COMPLETE); 9117 } 9118 9119 lbalen = (struct ctl_lba_len_flags *) 9120 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9121 lbalen->lba = lba; 9122 lbalen->len = num_blocks; 9123 if (bytchk) { 9124 lbalen->flags = CTL_LLF_COMPARE; 9125 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9126 } else { 9127 lbalen->flags = CTL_LLF_VERIFY; 9128 ctsio->kern_total_len = 0; 9129 } 9130 ctsio->kern_rel_offset = 0; 9131 9132 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9133 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9134 return (retval); 9135} 9136 9137int 9138ctl_report_luns(struct ctl_scsiio *ctsio) 9139{ 9140 struct scsi_report_luns *cdb; 9141 struct scsi_report_luns_data *lun_data; 9142 struct ctl_lun *lun, *request_lun; 9143 int num_luns, retval; 9144 uint32_t alloc_len, lun_datalen; 9145 int num_filled, well_known; 9146 uint32_t initidx, targ_lun_id, lun_id; 9147 9148 retval = CTL_RETVAL_COMPLETE; 9149 well_known = 0; 9150 9151 cdb = (struct scsi_report_luns *)ctsio->cdb; 9152 9153 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9154 9155 mtx_lock(&control_softc->ctl_lock); 9156 num_luns = control_softc->num_luns; 9157 mtx_unlock(&control_softc->ctl_lock); 9158 9159 switch (cdb->select_report) { 9160 case RPL_REPORT_DEFAULT: 9161 case RPL_REPORT_ALL: 9162 break; 9163 case RPL_REPORT_WELLKNOWN: 9164 well_known = 1; 9165 num_luns = 0; 9166 break; 9167 default: 9168 ctl_set_invalid_field(ctsio, 9169 /*sks_valid*/ 1, 9170 /*command*/ 1, 9171 /*field*/ 2, 9172 /*bit_valid*/ 0, 9173 /*bit*/ 0); 9174 ctl_done((union ctl_io *)ctsio); 9175 return (retval); 9176 break; /* NOTREACHED */ 9177 } 9178 9179 alloc_len = scsi_4btoul(cdb->length); 9180 /* 9181 * The initiator has to allocate at least 16 bytes for this request, 9182 * so he can at least get the header and the first LUN. Otherwise 9183 * we reject the request (per SPC-3 rev 14, section 6.21). 9184 */ 9185 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9186 sizeof(struct scsi_report_luns_lundata))) { 9187 ctl_set_invalid_field(ctsio, 9188 /*sks_valid*/ 1, 9189 /*command*/ 1, 9190 /*field*/ 6, 9191 /*bit_valid*/ 0, 9192 /*bit*/ 0); 9193 ctl_done((union ctl_io *)ctsio); 9194 return (retval); 9195 } 9196 9197 request_lun = (struct ctl_lun *) 9198 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9199 9200 lun_datalen = sizeof(*lun_data) + 9201 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9202 9203 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9204 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9205 ctsio->kern_sg_entries = 0; 9206 9207 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9208 9209 mtx_lock(&control_softc->ctl_lock); 9210 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9211 lun_id = targ_lun_id; 9212 if (ctsio->io_hdr.nexus.lun_map_fn != NULL) 9213 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id); 9214 if (lun_id >= CTL_MAX_LUNS) 9215 continue; 9216 lun = control_softc->ctl_luns[lun_id]; 9217 if (lun == NULL) 9218 continue; 9219 9220 if (targ_lun_id <= 0xff) { 9221 /* 9222 * Peripheral addressing method, bus number 0. 9223 */ 9224 lun_data->luns[num_filled].lundata[0] = 9225 RPL_LUNDATA_ATYP_PERIPH; 9226 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9227 num_filled++; 9228 } else if (targ_lun_id <= 0x3fff) { 9229 /* 9230 * Flat addressing method. 9231 */ 9232 lun_data->luns[num_filled].lundata[0] = 9233 RPL_LUNDATA_ATYP_FLAT | 9234 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9235#ifdef OLDCTLHEADERS 9236 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9237 (targ_lun_id & SRLD_BUS_LUN_MASK); 9238#endif 9239 lun_data->luns[num_filled].lundata[1] = 9240#ifdef OLDCTLHEADERS 9241 targ_lun_id >> SRLD_BUS_LUN_BITS; 9242#endif 9243 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9244 num_filled++; 9245 } else { 9246 printf("ctl_report_luns: bogus LUN number %jd, " 9247 "skipping\n", (intmax_t)targ_lun_id); 9248 } 9249 /* 9250 * According to SPC-3, rev 14 section 6.21: 9251 * 9252 * "The execution of a REPORT LUNS command to any valid and 9253 * installed logical unit shall clear the REPORTED LUNS DATA 9254 * HAS CHANGED unit attention condition for all logical 9255 * units of that target with respect to the requesting 9256 * initiator. A valid and installed logical unit is one 9257 * having a PERIPHERAL QUALIFIER of 000b in the standard 9258 * INQUIRY data (see 6.4.2)." 9259 * 9260 * If request_lun is NULL, the LUN this report luns command 9261 * was issued to is either disabled or doesn't exist. In that 9262 * case, we shouldn't clear any pending lun change unit 9263 * attention. 9264 */ 9265 if (request_lun != NULL) 9266 lun->pending_sense[initidx].ua_pending &= 9267 ~CTL_UA_LUN_CHANGE; 9268 } 9269 mtx_unlock(&control_softc->ctl_lock); 9270 9271 /* 9272 * It's quite possible that we've returned fewer LUNs than we allocated 9273 * space for. Trim it. 9274 */ 9275 lun_datalen = sizeof(*lun_data) + 9276 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9277 9278 if (lun_datalen < alloc_len) { 9279 ctsio->residual = alloc_len - lun_datalen; 9280 ctsio->kern_data_len = lun_datalen; 9281 ctsio->kern_total_len = lun_datalen; 9282 } else { 9283 ctsio->residual = 0; 9284 ctsio->kern_data_len = alloc_len; 9285 ctsio->kern_total_len = alloc_len; 9286 } 9287 ctsio->kern_data_resid = 0; 9288 ctsio->kern_rel_offset = 0; 9289 ctsio->kern_sg_entries = 0; 9290 9291 /* 9292 * We set this to the actual data length, regardless of how much 9293 * space we actually have to return results. If the user looks at 9294 * this value, he'll know whether or not he allocated enough space 9295 * and reissue the command if necessary. We don't support well 9296 * known logical units, so if the user asks for that, return none. 9297 */ 9298 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9299 9300 /* 9301 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9302 * this request. 9303 */ 9304 ctsio->scsi_status = SCSI_STATUS_OK; 9305 9306 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9307 ctsio->be_move_done = ctl_config_move_done; 9308 ctl_datamove((union ctl_io *)ctsio); 9309 9310 return (retval); 9311} 9312 9313int 9314ctl_request_sense(struct ctl_scsiio *ctsio) 9315{ 9316 struct scsi_request_sense *cdb; 9317 struct scsi_sense_data *sense_ptr; 9318 struct ctl_lun *lun; 9319 uint32_t initidx; 9320 int have_error; 9321 scsi_sense_data_type sense_format; 9322 9323 cdb = (struct scsi_request_sense *)ctsio->cdb; 9324 9325 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9326 9327 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9328 9329 /* 9330 * Determine which sense format the user wants. 9331 */ 9332 if (cdb->byte2 & SRS_DESC) 9333 sense_format = SSD_TYPE_DESC; 9334 else 9335 sense_format = SSD_TYPE_FIXED; 9336 9337 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9338 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9339 ctsio->kern_sg_entries = 0; 9340 9341 /* 9342 * struct scsi_sense_data, which is currently set to 256 bytes, is 9343 * larger than the largest allowed value for the length field in the 9344 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9345 */ 9346 ctsio->residual = 0; 9347 ctsio->kern_data_len = cdb->length; 9348 ctsio->kern_total_len = cdb->length; 9349 9350 ctsio->kern_data_resid = 0; 9351 ctsio->kern_rel_offset = 0; 9352 ctsio->kern_sg_entries = 0; 9353 9354 /* 9355 * If we don't have a LUN, we don't have any pending sense. 9356 */ 9357 if (lun == NULL) 9358 goto no_sense; 9359 9360 have_error = 0; 9361 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9362 /* 9363 * Check for pending sense, and then for pending unit attentions. 9364 * Pending sense gets returned first, then pending unit attentions. 9365 */ 9366 mtx_lock(&lun->ctl_softc->ctl_lock); 9367 if (ctl_is_set(lun->have_ca, initidx)) { 9368 scsi_sense_data_type stored_format; 9369 9370 /* 9371 * Check to see which sense format was used for the stored 9372 * sense data. 9373 */ 9374 stored_format = scsi_sense_type( 9375 &lun->pending_sense[initidx].sense); 9376 9377 /* 9378 * If the user requested a different sense format than the 9379 * one we stored, then we need to convert it to the other 9380 * format. If we're going from descriptor to fixed format 9381 * sense data, we may lose things in translation, depending 9382 * on what options were used. 9383 * 9384 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9385 * for some reason we'll just copy it out as-is. 9386 */ 9387 if ((stored_format == SSD_TYPE_FIXED) 9388 && (sense_format == SSD_TYPE_DESC)) 9389 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9390 &lun->pending_sense[initidx].sense, 9391 (struct scsi_sense_data_desc *)sense_ptr); 9392 else if ((stored_format == SSD_TYPE_DESC) 9393 && (sense_format == SSD_TYPE_FIXED)) 9394 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9395 &lun->pending_sense[initidx].sense, 9396 (struct scsi_sense_data_fixed *)sense_ptr); 9397 else 9398 memcpy(sense_ptr, &lun->pending_sense[initidx].sense, 9399 ctl_min(sizeof(*sense_ptr), 9400 sizeof(lun->pending_sense[initidx].sense))); 9401 9402 ctl_clear_mask(lun->have_ca, initidx); 9403 have_error = 1; 9404 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) { 9405 ctl_ua_type ua_type; 9406 9407 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending, 9408 sense_ptr, sense_format); 9409 if (ua_type != CTL_UA_NONE) { 9410 have_error = 1; 9411 /* We're reporting this UA, so clear it */ 9412 lun->pending_sense[initidx].ua_pending &= ~ua_type; 9413 } 9414 } 9415 mtx_unlock(&lun->ctl_softc->ctl_lock); 9416 9417 /* 9418 * We already have a pending error, return it. 9419 */ 9420 if (have_error != 0) { 9421 /* 9422 * We report the SCSI status as OK, since the status of the 9423 * request sense command itself is OK. 9424 */ 9425 ctsio->scsi_status = SCSI_STATUS_OK; 9426 9427 /* 9428 * We report 0 for the sense length, because we aren't doing 9429 * autosense in this case. We're reporting sense as 9430 * parameter data. 9431 */ 9432 ctsio->sense_len = 0; 9433 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9434 ctsio->be_move_done = ctl_config_move_done; 9435 ctl_datamove((union ctl_io *)ctsio); 9436 9437 return (CTL_RETVAL_COMPLETE); 9438 } 9439 9440no_sense: 9441 9442 /* 9443 * No sense information to report, so we report that everything is 9444 * okay. 9445 */ 9446 ctl_set_sense_data(sense_ptr, 9447 lun, 9448 sense_format, 9449 /*current_error*/ 1, 9450 /*sense_key*/ SSD_KEY_NO_SENSE, 9451 /*asc*/ 0x00, 9452 /*ascq*/ 0x00, 9453 SSD_ELEM_NONE); 9454 9455 ctsio->scsi_status = SCSI_STATUS_OK; 9456 9457 /* 9458 * We report 0 for the sense length, because we aren't doing 9459 * autosense in this case. We're reporting sense as parameter data. 9460 */ 9461 ctsio->sense_len = 0; 9462 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9463 ctsio->be_move_done = ctl_config_move_done; 9464 ctl_datamove((union ctl_io *)ctsio); 9465 9466 return (CTL_RETVAL_COMPLETE); 9467} 9468 9469int 9470ctl_tur(struct ctl_scsiio *ctsio) 9471{ 9472 struct ctl_lun *lun; 9473 9474 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9475 9476 CTL_DEBUG_PRINT(("ctl_tur\n")); 9477 9478 if (lun == NULL) 9479 return (EINVAL); 9480 9481 ctsio->scsi_status = SCSI_STATUS_OK; 9482 ctsio->io_hdr.status = CTL_SUCCESS; 9483 9484 ctl_done((union ctl_io *)ctsio); 9485 9486 return (CTL_RETVAL_COMPLETE); 9487} 9488 9489#ifdef notyet 9490static int 9491ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9492{ 9493 9494} 9495#endif 9496 9497static int 9498ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9499{ 9500 struct scsi_vpd_supported_pages *pages; 9501 int sup_page_size; 9502 struct ctl_lun *lun; 9503 9504 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9505 9506 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9507 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9508 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9509 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9510 ctsio->kern_sg_entries = 0; 9511 9512 if (sup_page_size < alloc_len) { 9513 ctsio->residual = alloc_len - sup_page_size; 9514 ctsio->kern_data_len = sup_page_size; 9515 ctsio->kern_total_len = sup_page_size; 9516 } else { 9517 ctsio->residual = 0; 9518 ctsio->kern_data_len = alloc_len; 9519 ctsio->kern_total_len = alloc_len; 9520 } 9521 ctsio->kern_data_resid = 0; 9522 ctsio->kern_rel_offset = 0; 9523 ctsio->kern_sg_entries = 0; 9524 9525 /* 9526 * The control device is always connected. The disk device, on the 9527 * other hand, may not be online all the time. Need to change this 9528 * to figure out whether the disk device is actually online or not. 9529 */ 9530 if (lun != NULL) 9531 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9532 lun->be_lun->lun_type; 9533 else 9534 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9535 9536 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9537 /* Supported VPD pages */ 9538 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9539 /* Serial Number */ 9540 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9541 /* Device Identification */ 9542 pages->page_list[2] = SVPD_DEVICE_ID; 9543 /* Block limits */ 9544 pages->page_list[3] = SVPD_BLOCK_LIMITS; 9545 /* Logical Block Provisioning */ 9546 pages->page_list[4] = SVPD_LBP; 9547 9548 ctsio->scsi_status = SCSI_STATUS_OK; 9549 9550 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9551 ctsio->be_move_done = ctl_config_move_done; 9552 ctl_datamove((union ctl_io *)ctsio); 9553 9554 return (CTL_RETVAL_COMPLETE); 9555} 9556 9557static int 9558ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9559{ 9560 struct scsi_vpd_unit_serial_number *sn_ptr; 9561 struct ctl_lun *lun; 9562#ifndef CTL_USE_BACKEND_SN 9563 char tmpstr[32]; 9564#endif 9565 9566 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9567 9568 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9569 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9570 ctsio->kern_sg_entries = 0; 9571 9572 if (sizeof(*sn_ptr) < alloc_len) { 9573 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9574 ctsio->kern_data_len = sizeof(*sn_ptr); 9575 ctsio->kern_total_len = sizeof(*sn_ptr); 9576 } else { 9577 ctsio->residual = 0; 9578 ctsio->kern_data_len = alloc_len; 9579 ctsio->kern_total_len = alloc_len; 9580 } 9581 ctsio->kern_data_resid = 0; 9582 ctsio->kern_rel_offset = 0; 9583 ctsio->kern_sg_entries = 0; 9584 9585 /* 9586 * The control device is always connected. The disk device, on the 9587 * other hand, may not be online all the time. Need to change this 9588 * to figure out whether the disk device is actually online or not. 9589 */ 9590 if (lun != NULL) 9591 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9592 lun->be_lun->lun_type; 9593 else 9594 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9595 9596 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9597 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9598#ifdef CTL_USE_BACKEND_SN 9599 /* 9600 * If we don't have a LUN, we just leave the serial number as 9601 * all spaces. 9602 */ 9603 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9604 if (lun != NULL) { 9605 strncpy((char *)sn_ptr->serial_num, 9606 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9607 } 9608#else 9609 /* 9610 * Note that we're using a non-unique serial number here, 9611 */ 9612 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000"); 9613 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9614 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN, 9615 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4))); 9616#endif 9617 ctsio->scsi_status = SCSI_STATUS_OK; 9618 9619 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9620 ctsio->be_move_done = ctl_config_move_done; 9621 ctl_datamove((union ctl_io *)ctsio); 9622 9623 return (CTL_RETVAL_COMPLETE); 9624} 9625 9626 9627static int 9628ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9629{ 9630 struct scsi_vpd_device_id *devid_ptr; 9631 struct scsi_vpd_id_descriptor *desc, *desc1; 9632 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */ 9633 struct scsi_vpd_id_t10 *t10id; 9634 struct ctl_softc *ctl_softc; 9635 struct ctl_lun *lun; 9636 struct ctl_frontend *fe; 9637 char *val; 9638#ifndef CTL_USE_BACKEND_SN 9639 char tmpstr[32]; 9640#endif /* CTL_USE_BACKEND_SN */ 9641 int devid_len; 9642 9643 ctl_softc = control_softc; 9644 9645 mtx_lock(&ctl_softc->ctl_lock); 9646 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9647 mtx_unlock(&ctl_softc->ctl_lock); 9648 9649 if (fe->devid != NULL) 9650 return ((fe->devid)(ctsio, alloc_len)); 9651 9652 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9653 9654 devid_len = sizeof(struct scsi_vpd_device_id) + 9655 sizeof(struct scsi_vpd_id_descriptor) + 9656 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN + 9657 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN + 9658 sizeof(struct scsi_vpd_id_descriptor) + 9659 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9660 sizeof(struct scsi_vpd_id_descriptor) + 9661 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9662 9663 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO); 9664 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9665 ctsio->kern_sg_entries = 0; 9666 9667 if (devid_len < alloc_len) { 9668 ctsio->residual = alloc_len - devid_len; 9669 ctsio->kern_data_len = devid_len; 9670 ctsio->kern_total_len = devid_len; 9671 } else { 9672 ctsio->residual = 0; 9673 ctsio->kern_data_len = alloc_len; 9674 ctsio->kern_total_len = alloc_len; 9675 } 9676 ctsio->kern_data_resid = 0; 9677 ctsio->kern_rel_offset = 0; 9678 ctsio->kern_sg_entries = 0; 9679 9680 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9681 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 9682 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9683 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN); 9684 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] + 9685 CTL_WWPN_LEN); 9686 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] + 9687 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9688 9689 /* 9690 * The control device is always connected. The disk device, on the 9691 * other hand, may not be online all the time. 9692 */ 9693 if (lun != NULL) 9694 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9695 lun->be_lun->lun_type; 9696 else 9697 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9698 9699 devid_ptr->page_code = SVPD_DEVICE_ID; 9700 9701 scsi_ulto2b(devid_len - 4, devid_ptr->length); 9702 9703 mtx_lock(&ctl_softc->ctl_lock); 9704 9705 /* 9706 * For Fibre channel, 9707 */ 9708 if (fe->port_type == CTL_PORT_FC) 9709 { 9710 desc->proto_codeset = (SCSI_PROTO_FC << 4) | 9711 SVPD_ID_CODESET_ASCII; 9712 desc1->proto_codeset = (SCSI_PROTO_FC << 4) | 9713 SVPD_ID_CODESET_BINARY; 9714 } 9715 else 9716 { 9717 desc->proto_codeset = (SCSI_PROTO_SPI << 4) | 9718 SVPD_ID_CODESET_ASCII; 9719 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) | 9720 SVPD_ID_CODESET_BINARY; 9721 } 9722 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset; 9723 mtx_unlock(&ctl_softc->ctl_lock); 9724 9725 /* 9726 * We're using a LUN association here. i.e., this device ID is a 9727 * per-LUN identifier. 9728 */ 9729 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 9730 desc->length = sizeof(*t10id) + CTL_DEVID_LEN; 9731 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) { 9732 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 9733 } else { 9734 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 9735 strncpy(t10id->vendor, val, 9736 min(sizeof(t10id->vendor), strlen(val))); 9737 } 9738 9739 /* 9740 * desc1 is for the WWPN which is a port asscociation. 9741 */ 9742 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA; 9743 desc1->length = CTL_WWPN_LEN; 9744 /* XXX Call Reggie's get_WWNN func here then add port # to the end */ 9745 /* For testing just create the WWPN */ 9746#if 0 9747 ddb_GetWWNN((char *)desc1->identifier); 9748 9749 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9750 /* This is so Copancontrol will return something sane */ 9751 if (ctsio->io_hdr.nexus.targ_port!=0 && 9752 ctsio->io_hdr.nexus.targ_port!=8) 9753 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1; 9754 else 9755 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port; 9756#endif 9757 9758 be64enc(desc1->identifier, fe->wwpn); 9759 9760 /* 9761 * desc2 is for the Relative Target Port(type 4h) identifier 9762 */ 9763 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9764 | SVPD_ID_TYPE_RELTARG; 9765 desc2->length = 4; 9766//#if 0 9767 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9768 /* This is so Copancontrol will return something sane */ 9769 if (ctsio->io_hdr.nexus.targ_port!=0 && 9770 ctsio->io_hdr.nexus.targ_port!=8) 9771 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1; 9772 else 9773 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port; 9774//#endif 9775 9776 /* 9777 * desc3 is for the Target Port Group(type 5h) identifier 9778 */ 9779 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9780 | SVPD_ID_TYPE_TPORTGRP; 9781 desc3->length = 4; 9782 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single) 9783 desc3->identifier[3] = 1; 9784 else 9785 desc3->identifier[3] = 2; 9786 9787#ifdef CTL_USE_BACKEND_SN 9788 /* 9789 * If we've actually got a backend, copy the device id from the 9790 * per-LUN data. Otherwise, set it to all spaces. 9791 */ 9792 if (lun != NULL) { 9793 /* 9794 * Copy the backend's LUN ID. 9795 */ 9796 strncpy((char *)t10id->vendor_spec_id, 9797 (char *)lun->be_lun->device_id, CTL_DEVID_LEN); 9798 } else { 9799 /* 9800 * No backend, set this to spaces. 9801 */ 9802 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN); 9803 } 9804#else 9805 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d", 9806 (lun != NULL) ? (int)lun->lun : 0); 9807 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN, 9808 sizeof(tmpstr))); 9809#endif 9810 9811 ctsio->scsi_status = SCSI_STATUS_OK; 9812 9813 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9814 ctsio->be_move_done = ctl_config_move_done; 9815 ctl_datamove((union ctl_io *)ctsio); 9816 9817 return (CTL_RETVAL_COMPLETE); 9818} 9819 9820static int 9821ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9822{ 9823 struct scsi_vpd_block_limits *bl_ptr; 9824 struct ctl_lun *lun; 9825 int bs; 9826 9827 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9828 bs = lun->be_lun->blocksize; 9829 9830 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9831 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9832 ctsio->kern_sg_entries = 0; 9833 9834 if (sizeof(*bl_ptr) < alloc_len) { 9835 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9836 ctsio->kern_data_len = sizeof(*bl_ptr); 9837 ctsio->kern_total_len = sizeof(*bl_ptr); 9838 } else { 9839 ctsio->residual = 0; 9840 ctsio->kern_data_len = alloc_len; 9841 ctsio->kern_total_len = alloc_len; 9842 } 9843 ctsio->kern_data_resid = 0; 9844 ctsio->kern_rel_offset = 0; 9845 ctsio->kern_sg_entries = 0; 9846 9847 /* 9848 * The control device is always connected. The disk device, on the 9849 * other hand, may not be online all the time. Need to change this 9850 * to figure out whether the disk device is actually online or not. 9851 */ 9852 if (lun != NULL) 9853 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9854 lun->be_lun->lun_type; 9855 else 9856 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9857 9858 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9859 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 9860 bl_ptr->max_cmp_write_len = 0xff; 9861 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9862 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 9863 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9864 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9865 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9866 } 9867 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9868 9869 ctsio->scsi_status = SCSI_STATUS_OK; 9870 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9871 ctsio->be_move_done = ctl_config_move_done; 9872 ctl_datamove((union ctl_io *)ctsio); 9873 9874 return (CTL_RETVAL_COMPLETE); 9875} 9876 9877static int 9878ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9879{ 9880 struct scsi_vpd_logical_block_prov *lbp_ptr; 9881 struct ctl_lun *lun; 9882 int bs; 9883 9884 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9885 bs = lun->be_lun->blocksize; 9886 9887 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9888 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9889 ctsio->kern_sg_entries = 0; 9890 9891 if (sizeof(*lbp_ptr) < alloc_len) { 9892 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9893 ctsio->kern_data_len = sizeof(*lbp_ptr); 9894 ctsio->kern_total_len = sizeof(*lbp_ptr); 9895 } else { 9896 ctsio->residual = 0; 9897 ctsio->kern_data_len = alloc_len; 9898 ctsio->kern_total_len = alloc_len; 9899 } 9900 ctsio->kern_data_resid = 0; 9901 ctsio->kern_rel_offset = 0; 9902 ctsio->kern_sg_entries = 0; 9903 9904 /* 9905 * The control device is always connected. The disk device, on the 9906 * other hand, may not be online all the time. Need to change this 9907 * to figure out whether the disk device is actually online or not. 9908 */ 9909 if (lun != NULL) 9910 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9911 lun->be_lun->lun_type; 9912 else 9913 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9914 9915 lbp_ptr->page_code = SVPD_LBP; 9916 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 9917 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10; 9918 9919 ctsio->scsi_status = SCSI_STATUS_OK; 9920 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9921 ctsio->be_move_done = ctl_config_move_done; 9922 ctl_datamove((union ctl_io *)ctsio); 9923 9924 return (CTL_RETVAL_COMPLETE); 9925} 9926 9927static int 9928ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9929{ 9930 struct scsi_inquiry *cdb; 9931 struct ctl_lun *lun; 9932 int alloc_len, retval; 9933 9934 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9935 cdb = (struct scsi_inquiry *)ctsio->cdb; 9936 9937 retval = CTL_RETVAL_COMPLETE; 9938 9939 alloc_len = scsi_2btoul(cdb->length); 9940 9941 switch (cdb->page_code) { 9942 case SVPD_SUPPORTED_PAGES: 9943 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9944 break; 9945 case SVPD_UNIT_SERIAL_NUMBER: 9946 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9947 break; 9948 case SVPD_DEVICE_ID: 9949 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9950 break; 9951 case SVPD_BLOCK_LIMITS: 9952 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9953 break; 9954 case SVPD_LBP: 9955 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9956 break; 9957 default: 9958 ctl_set_invalid_field(ctsio, 9959 /*sks_valid*/ 1, 9960 /*command*/ 1, 9961 /*field*/ 2, 9962 /*bit_valid*/ 0, 9963 /*bit*/ 0); 9964 ctl_done((union ctl_io *)ctsio); 9965 retval = CTL_RETVAL_COMPLETE; 9966 break; 9967 } 9968 9969 return (retval); 9970} 9971 9972static int 9973ctl_inquiry_std(struct ctl_scsiio *ctsio) 9974{ 9975 struct scsi_inquiry_data *inq_ptr; 9976 struct scsi_inquiry *cdb; 9977 struct ctl_softc *ctl_softc; 9978 struct ctl_lun *lun; 9979 char *val; 9980 uint32_t alloc_len; 9981 int is_fc; 9982 9983 ctl_softc = control_softc; 9984 9985 /* 9986 * Figure out whether we're talking to a Fibre Channel port or not. 9987 * We treat the ioctl front end, and any SCSI adapters, as packetized 9988 * SCSI front ends. 9989 */ 9990 mtx_lock(&ctl_softc->ctl_lock); 9991 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type != 9992 CTL_PORT_FC) 9993 is_fc = 0; 9994 else 9995 is_fc = 1; 9996 mtx_unlock(&ctl_softc->ctl_lock); 9997 9998 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9999 cdb = (struct scsi_inquiry *)ctsio->cdb; 10000 alloc_len = scsi_2btoul(cdb->length); 10001 10002 /* 10003 * We malloc the full inquiry data size here and fill it 10004 * in. If the user only asks for less, we'll give him 10005 * that much. 10006 */ 10007 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10008 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10009 ctsio->kern_sg_entries = 0; 10010 ctsio->kern_data_resid = 0; 10011 ctsio->kern_rel_offset = 0; 10012 10013 if (sizeof(*inq_ptr) < alloc_len) { 10014 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10015 ctsio->kern_data_len = sizeof(*inq_ptr); 10016 ctsio->kern_total_len = sizeof(*inq_ptr); 10017 } else { 10018 ctsio->residual = 0; 10019 ctsio->kern_data_len = alloc_len; 10020 ctsio->kern_total_len = alloc_len; 10021 } 10022 10023 /* 10024 * If we have a LUN configured, report it as connected. Otherwise, 10025 * report that it is offline or no device is supported, depending 10026 * on the value of inquiry_pq_no_lun. 10027 * 10028 * According to the spec (SPC-4 r34), the peripheral qualifier 10029 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10030 * 10031 * "A peripheral device having the specified peripheral device type 10032 * is not connected to this logical unit. However, the device 10033 * server is capable of supporting the specified peripheral device 10034 * type on this logical unit." 10035 * 10036 * According to the same spec, the peripheral qualifier 10037 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10038 * 10039 * "The device server is not capable of supporting a peripheral 10040 * device on this logical unit. For this peripheral qualifier the 10041 * peripheral device type shall be set to 1Fh. All other peripheral 10042 * device type values are reserved for this peripheral qualifier." 10043 * 10044 * Given the text, it would seem that we probably want to report that 10045 * the LUN is offline here. There is no LUN connected, but we can 10046 * support a LUN at the given LUN number. 10047 * 10048 * In the real world, though, it sounds like things are a little 10049 * different: 10050 * 10051 * - Linux, when presented with a LUN with the offline peripheral 10052 * qualifier, will create an sg driver instance for it. So when 10053 * you attach it to CTL, you wind up with a ton of sg driver 10054 * instances. (One for every LUN that Linux bothered to probe.) 10055 * Linux does this despite the fact that it issues a REPORT LUNs 10056 * to LUN 0 to get the inventory of supported LUNs. 10057 * 10058 * - There is other anecdotal evidence (from Emulex folks) about 10059 * arrays that use the offline peripheral qualifier for LUNs that 10060 * are on the "passive" path in an active/passive array. 10061 * 10062 * So the solution is provide a hopefully reasonable default 10063 * (return bad/no LUN) and allow the user to change the behavior 10064 * with a tunable/sysctl variable. 10065 */ 10066 if (lun != NULL) 10067 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10068 lun->be_lun->lun_type; 10069 else if (ctl_softc->inquiry_pq_no_lun == 0) 10070 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10071 else 10072 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10073 10074 /* RMB in byte 2 is 0 */ 10075 inq_ptr->version = SCSI_REV_SPC3; 10076 10077 /* 10078 * According to SAM-3, even if a device only supports a single 10079 * level of LUN addressing, it should still set the HISUP bit: 10080 * 10081 * 4.9.1 Logical unit numbers overview 10082 * 10083 * All logical unit number formats described in this standard are 10084 * hierarchical in structure even when only a single level in that 10085 * hierarchy is used. The HISUP bit shall be set to one in the 10086 * standard INQUIRY data (see SPC-2) when any logical unit number 10087 * format described in this standard is used. Non-hierarchical 10088 * formats are outside the scope of this standard. 10089 * 10090 * Therefore we set the HiSup bit here. 10091 * 10092 * The reponse format is 2, per SPC-3. 10093 */ 10094 inq_ptr->response_format = SID_HiSup | 2; 10095 10096 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 10097 CTL_DEBUG_PRINT(("additional_length = %d\n", 10098 inq_ptr->additional_length)); 10099 10100 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT; 10101 /* 16 bit addressing */ 10102 if (is_fc == 0) 10103 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10104 /* XXX set the SID_MultiP bit here if we're actually going to 10105 respond on multiple ports */ 10106 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10107 10108 /* 16 bit data bus, synchronous transfers */ 10109 /* XXX these flags don't apply for FC */ 10110 if (is_fc == 0) 10111 inq_ptr->flags = SID_WBus16 | SID_Sync; 10112 /* 10113 * XXX KDM do we want to support tagged queueing on the control 10114 * device at all? 10115 */ 10116 if ((lun == NULL) 10117 || (lun->be_lun->lun_type != T_PROCESSOR)) 10118 inq_ptr->flags |= SID_CmdQue; 10119 /* 10120 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10121 * We have 8 bytes for the vendor name, and 16 bytes for the device 10122 * name and 4 bytes for the revision. 10123 */ 10124 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) { 10125 strcpy(inq_ptr->vendor, CTL_VENDOR); 10126 } else { 10127 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10128 strncpy(inq_ptr->vendor, val, 10129 min(sizeof(inq_ptr->vendor), strlen(val))); 10130 } 10131 if (lun == NULL) { 10132 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10133 } else if ((val = ctl_get_opt(lun->be_lun, "product")) == NULL) { 10134 switch (lun->be_lun->lun_type) { 10135 case T_DIRECT: 10136 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10137 break; 10138 case T_PROCESSOR: 10139 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10140 break; 10141 default: 10142 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10143 break; 10144 } 10145 } else { 10146 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10147 strncpy(inq_ptr->product, val, 10148 min(sizeof(inq_ptr->product), strlen(val))); 10149 } 10150 10151 /* 10152 * XXX make this a macro somewhere so it automatically gets 10153 * incremented when we make changes. 10154 */ 10155 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "revision")) == NULL) { 10156 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10157 } else { 10158 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10159 strncpy(inq_ptr->revision, val, 10160 min(sizeof(inq_ptr->revision), strlen(val))); 10161 } 10162 10163 /* 10164 * For parallel SCSI, we support double transition and single 10165 * transition clocking. We also support QAS (Quick Arbitration 10166 * and Selection) and Information Unit transfers on both the 10167 * control and array devices. 10168 */ 10169 if (is_fc == 0) 10170 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10171 SID_SPI_IUS; 10172 10173 /* SAM-3 */ 10174 scsi_ulto2b(0x0060, inq_ptr->version1); 10175 /* SPC-3 (no version claimed) XXX should we claim a version? */ 10176 scsi_ulto2b(0x0300, inq_ptr->version2); 10177 if (is_fc) { 10178 /* FCP-2 ANSI INCITS.350:2003 */ 10179 scsi_ulto2b(0x0917, inq_ptr->version3); 10180 } else { 10181 /* SPI-4 ANSI INCITS.362:200x */ 10182 scsi_ulto2b(0x0B56, inq_ptr->version3); 10183 } 10184 10185 if (lun == NULL) { 10186 /* SBC-2 (no version claimed) XXX should we claim a version? */ 10187 scsi_ulto2b(0x0320, inq_ptr->version4); 10188 } else { 10189 switch (lun->be_lun->lun_type) { 10190 case T_DIRECT: 10191 /* 10192 * SBC-2 (no version claimed) XXX should we claim a 10193 * version? 10194 */ 10195 scsi_ulto2b(0x0320, inq_ptr->version4); 10196 break; 10197 case T_PROCESSOR: 10198 default: 10199 break; 10200 } 10201 } 10202 10203 ctsio->scsi_status = SCSI_STATUS_OK; 10204 if (ctsio->kern_data_len > 0) { 10205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10206 ctsio->be_move_done = ctl_config_move_done; 10207 ctl_datamove((union ctl_io *)ctsio); 10208 } else { 10209 ctsio->io_hdr.status = CTL_SUCCESS; 10210 ctl_done((union ctl_io *)ctsio); 10211 } 10212 10213 return (CTL_RETVAL_COMPLETE); 10214} 10215 10216int 10217ctl_inquiry(struct ctl_scsiio *ctsio) 10218{ 10219 struct scsi_inquiry *cdb; 10220 int retval; 10221 10222 cdb = (struct scsi_inquiry *)ctsio->cdb; 10223 10224 retval = 0; 10225 10226 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10227 10228 /* 10229 * Right now, we don't support the CmdDt inquiry information. 10230 * This would be nice to support in the future. When we do 10231 * support it, we should change this test so that it checks to make 10232 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10233 */ 10234#ifdef notyet 10235 if (((cdb->byte2 & SI_EVPD) 10236 && (cdb->byte2 & SI_CMDDT))) 10237#endif 10238 if (cdb->byte2 & SI_CMDDT) { 10239 /* 10240 * Point to the SI_CMDDT bit. We might change this 10241 * when we support SI_CMDDT, but since both bits would be 10242 * "wrong", this should probably just stay as-is then. 10243 */ 10244 ctl_set_invalid_field(ctsio, 10245 /*sks_valid*/ 1, 10246 /*command*/ 1, 10247 /*field*/ 1, 10248 /*bit_valid*/ 1, 10249 /*bit*/ 1); 10250 ctl_done((union ctl_io *)ctsio); 10251 return (CTL_RETVAL_COMPLETE); 10252 } 10253 if (cdb->byte2 & SI_EVPD) 10254 retval = ctl_inquiry_evpd(ctsio); 10255#ifdef notyet 10256 else if (cdb->byte2 & SI_CMDDT) 10257 retval = ctl_inquiry_cmddt(ctsio); 10258#endif 10259 else 10260 retval = ctl_inquiry_std(ctsio); 10261 10262 return (retval); 10263} 10264 10265/* 10266 * For known CDB types, parse the LBA and length. 10267 */ 10268static int 10269ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10270{ 10271 if (io->io_hdr.io_type != CTL_IO_SCSI) 10272 return (1); 10273 10274 switch (io->scsiio.cdb[0]) { 10275 case COMPARE_AND_WRITE: { 10276 struct scsi_compare_and_write *cdb; 10277 10278 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10279 10280 *lba = scsi_8btou64(cdb->addr); 10281 *len = cdb->length; 10282 break; 10283 } 10284 case READ_6: 10285 case WRITE_6: { 10286 struct scsi_rw_6 *cdb; 10287 10288 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10289 10290 *lba = scsi_3btoul(cdb->addr); 10291 /* only 5 bits are valid in the most significant address byte */ 10292 *lba &= 0x1fffff; 10293 *len = cdb->length; 10294 break; 10295 } 10296 case READ_10: 10297 case WRITE_10: { 10298 struct scsi_rw_10 *cdb; 10299 10300 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10301 10302 *lba = scsi_4btoul(cdb->addr); 10303 *len = scsi_2btoul(cdb->length); 10304 break; 10305 } 10306 case WRITE_VERIFY_10: { 10307 struct scsi_write_verify_10 *cdb; 10308 10309 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10310 10311 *lba = scsi_4btoul(cdb->addr); 10312 *len = scsi_2btoul(cdb->length); 10313 break; 10314 } 10315 case READ_12: 10316 case WRITE_12: { 10317 struct scsi_rw_12 *cdb; 10318 10319 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10320 10321 *lba = scsi_4btoul(cdb->addr); 10322 *len = scsi_4btoul(cdb->length); 10323 break; 10324 } 10325 case WRITE_VERIFY_12: { 10326 struct scsi_write_verify_12 *cdb; 10327 10328 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10329 10330 *lba = scsi_4btoul(cdb->addr); 10331 *len = scsi_4btoul(cdb->length); 10332 break; 10333 } 10334 case READ_16: 10335 case WRITE_16: { 10336 struct scsi_rw_16 *cdb; 10337 10338 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10339 10340 *lba = scsi_8btou64(cdb->addr); 10341 *len = scsi_4btoul(cdb->length); 10342 break; 10343 } 10344 case WRITE_VERIFY_16: { 10345 struct scsi_write_verify_16 *cdb; 10346 10347 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10348 10349 10350 *lba = scsi_8btou64(cdb->addr); 10351 *len = scsi_4btoul(cdb->length); 10352 break; 10353 } 10354 case WRITE_SAME_10: { 10355 struct scsi_write_same_10 *cdb; 10356 10357 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10358 10359 *lba = scsi_4btoul(cdb->addr); 10360 *len = scsi_2btoul(cdb->length); 10361 break; 10362 } 10363 case WRITE_SAME_16: { 10364 struct scsi_write_same_16 *cdb; 10365 10366 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10367 10368 *lba = scsi_8btou64(cdb->addr); 10369 *len = scsi_4btoul(cdb->length); 10370 break; 10371 } 10372 case VERIFY_10: { 10373 struct scsi_verify_10 *cdb; 10374 10375 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10376 10377 *lba = scsi_4btoul(cdb->addr); 10378 *len = scsi_2btoul(cdb->length); 10379 break; 10380 } 10381 case VERIFY_12: { 10382 struct scsi_verify_12 *cdb; 10383 10384 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10385 10386 *lba = scsi_4btoul(cdb->addr); 10387 *len = scsi_4btoul(cdb->length); 10388 break; 10389 } 10390 case VERIFY_16: { 10391 struct scsi_verify_16 *cdb; 10392 10393 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10394 10395 *lba = scsi_8btou64(cdb->addr); 10396 *len = scsi_4btoul(cdb->length); 10397 break; 10398 } 10399 default: 10400 return (1); 10401 break; /* NOTREACHED */ 10402 } 10403 10404 return (0); 10405} 10406 10407static ctl_action 10408ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10409{ 10410 uint64_t endlba1, endlba2; 10411 10412 endlba1 = lba1 + len1 - 1; 10413 endlba2 = lba2 + len2 - 1; 10414 10415 if ((endlba1 < lba2) 10416 || (endlba2 < lba1)) 10417 return (CTL_ACTION_PASS); 10418 else 10419 return (CTL_ACTION_BLOCK); 10420} 10421 10422static ctl_action 10423ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10424{ 10425 uint64_t lba1, lba2; 10426 uint32_t len1, len2; 10427 int retval; 10428 10429 retval = ctl_get_lba_len(io1, &lba1, &len1); 10430 if (retval != 0) 10431 return (CTL_ACTION_ERROR); 10432 10433 retval = ctl_get_lba_len(io2, &lba2, &len2); 10434 if (retval != 0) 10435 return (CTL_ACTION_ERROR); 10436 10437 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10438} 10439 10440static ctl_action 10441ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10442{ 10443 struct ctl_cmd_entry *pending_entry, *ooa_entry; 10444 ctl_serialize_action *serialize_row; 10445 10446 /* 10447 * The initiator attempted multiple untagged commands at the same 10448 * time. Can't do that. 10449 */ 10450 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10451 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10452 && ((pending_io->io_hdr.nexus.targ_port == 10453 ooa_io->io_hdr.nexus.targ_port) 10454 && (pending_io->io_hdr.nexus.initid.id == 10455 ooa_io->io_hdr.nexus.initid.id)) 10456 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10457 return (CTL_ACTION_OVERLAP); 10458 10459 /* 10460 * The initiator attempted to send multiple tagged commands with 10461 * the same ID. (It's fine if different initiators have the same 10462 * tag ID.) 10463 * 10464 * Even if all of those conditions are true, we don't kill the I/O 10465 * if the command ahead of us has been aborted. We won't end up 10466 * sending it to the FETD, and it's perfectly legal to resend a 10467 * command with the same tag number as long as the previous 10468 * instance of this tag number has been aborted somehow. 10469 */ 10470 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10471 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10472 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10473 && ((pending_io->io_hdr.nexus.targ_port == 10474 ooa_io->io_hdr.nexus.targ_port) 10475 && (pending_io->io_hdr.nexus.initid.id == 10476 ooa_io->io_hdr.nexus.initid.id)) 10477 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10478 return (CTL_ACTION_OVERLAP_TAG); 10479 10480 /* 10481 * If we get a head of queue tag, SAM-3 says that we should 10482 * immediately execute it. 10483 * 10484 * What happens if this command would normally block for some other 10485 * reason? e.g. a request sense with a head of queue tag 10486 * immediately after a write. Normally that would block, but this 10487 * will result in its getting executed immediately... 10488 * 10489 * We currently return "pass" instead of "skip", so we'll end up 10490 * going through the rest of the queue to check for overlapped tags. 10491 * 10492 * XXX KDM check for other types of blockage first?? 10493 */ 10494 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10495 return (CTL_ACTION_PASS); 10496 10497 /* 10498 * Ordered tags have to block until all items ahead of them 10499 * have completed. If we get called with an ordered tag, we always 10500 * block, if something else is ahead of us in the queue. 10501 */ 10502 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10503 return (CTL_ACTION_BLOCK); 10504 10505 /* 10506 * Simple tags get blocked until all head of queue and ordered tags 10507 * ahead of them have completed. I'm lumping untagged commands in 10508 * with simple tags here. XXX KDM is that the right thing to do? 10509 */ 10510 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10511 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10512 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10513 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10514 return (CTL_ACTION_BLOCK); 10515 10516 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]]; 10517 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]]; 10518 10519 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10520 10521 switch (serialize_row[pending_entry->seridx]) { 10522 case CTL_SER_BLOCK: 10523 return (CTL_ACTION_BLOCK); 10524 break; /* NOTREACHED */ 10525 case CTL_SER_EXTENT: 10526 return (ctl_extent_check(pending_io, ooa_io)); 10527 break; /* NOTREACHED */ 10528 case CTL_SER_PASS: 10529 return (CTL_ACTION_PASS); 10530 break; /* NOTREACHED */ 10531 case CTL_SER_SKIP: 10532 return (CTL_ACTION_SKIP); 10533 break; 10534 default: 10535 panic("invalid serialization value %d", 10536 serialize_row[pending_entry->seridx]); 10537 break; /* NOTREACHED */ 10538 } 10539 10540 return (CTL_ACTION_ERROR); 10541} 10542 10543/* 10544 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10545 * Assumptions: 10546 * - pending_io is generally either incoming, or on the blocked queue 10547 * - starting I/O is the I/O we want to start the check with. 10548 */ 10549static ctl_action 10550ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10551 union ctl_io *starting_io) 10552{ 10553 union ctl_io *ooa_io; 10554 ctl_action action; 10555 10556 mtx_assert(&control_softc->ctl_lock, MA_OWNED); 10557 10558 /* 10559 * Run back along the OOA queue, starting with the current 10560 * blocked I/O and going through every I/O before it on the 10561 * queue. If starting_io is NULL, we'll just end up returning 10562 * CTL_ACTION_PASS. 10563 */ 10564 for (ooa_io = starting_io; ooa_io != NULL; 10565 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10566 ooa_links)){ 10567 10568 /* 10569 * This routine just checks to see whether 10570 * cur_blocked is blocked by ooa_io, which is ahead 10571 * of it in the queue. It doesn't queue/dequeue 10572 * cur_blocked. 10573 */ 10574 action = ctl_check_for_blockage(pending_io, ooa_io); 10575 switch (action) { 10576 case CTL_ACTION_BLOCK: 10577 case CTL_ACTION_OVERLAP: 10578 case CTL_ACTION_OVERLAP_TAG: 10579 case CTL_ACTION_SKIP: 10580 case CTL_ACTION_ERROR: 10581 return (action); 10582 break; /* NOTREACHED */ 10583 case CTL_ACTION_PASS: 10584 break; 10585 default: 10586 panic("invalid action %d", action); 10587 break; /* NOTREACHED */ 10588 } 10589 } 10590 10591 return (CTL_ACTION_PASS); 10592} 10593 10594/* 10595 * Assumptions: 10596 * - An I/O has just completed, and has been removed from the per-LUN OOA 10597 * queue, so some items on the blocked queue may now be unblocked. 10598 */ 10599static int 10600ctl_check_blocked(struct ctl_lun *lun) 10601{ 10602 union ctl_io *cur_blocked, *next_blocked; 10603 10604 mtx_assert(&control_softc->ctl_lock, MA_OWNED); 10605 10606 /* 10607 * Run forward from the head of the blocked queue, checking each 10608 * entry against the I/Os prior to it on the OOA queue to see if 10609 * there is still any blockage. 10610 * 10611 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10612 * with our removing a variable on it while it is traversing the 10613 * list. 10614 */ 10615 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10616 cur_blocked != NULL; cur_blocked = next_blocked) { 10617 union ctl_io *prev_ooa; 10618 ctl_action action; 10619 10620 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10621 blocked_links); 10622 10623 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10624 ctl_ooaq, ooa_links); 10625 10626 /* 10627 * If cur_blocked happens to be the first item in the OOA 10628 * queue now, prev_ooa will be NULL, and the action 10629 * returned will just be CTL_ACTION_PASS. 10630 */ 10631 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10632 10633 switch (action) { 10634 case CTL_ACTION_BLOCK: 10635 /* Nothing to do here, still blocked */ 10636 break; 10637 case CTL_ACTION_OVERLAP: 10638 case CTL_ACTION_OVERLAP_TAG: 10639 /* 10640 * This shouldn't happen! In theory we've already 10641 * checked this command for overlap... 10642 */ 10643 break; 10644 case CTL_ACTION_PASS: 10645 case CTL_ACTION_SKIP: { 10646 struct ctl_softc *softc; 10647 struct ctl_cmd_entry *entry; 10648 uint32_t initidx; 10649 uint8_t opcode; 10650 int isc_retval; 10651 10652 /* 10653 * The skip case shouldn't happen, this transaction 10654 * should have never made it onto the blocked queue. 10655 */ 10656 /* 10657 * This I/O is no longer blocked, we can remove it 10658 * from the blocked queue. Since this is a TAILQ 10659 * (doubly linked list), we can do O(1) removals 10660 * from any place on the list. 10661 */ 10662 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10663 blocked_links); 10664 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10665 10666 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10667 /* 10668 * Need to send IO back to original side to 10669 * run 10670 */ 10671 union ctl_ha_msg msg_info; 10672 10673 msg_info.hdr.original_sc = 10674 cur_blocked->io_hdr.original_sc; 10675 msg_info.hdr.serializing_sc = cur_blocked; 10676 msg_info.hdr.msg_type = CTL_MSG_R2R; 10677 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10678 &msg_info, sizeof(msg_info), 0)) > 10679 CTL_HA_STATUS_SUCCESS) { 10680 printf("CTL:Check Blocked error from " 10681 "ctl_ha_msg_send %d\n", 10682 isc_retval); 10683 } 10684 break; 10685 } 10686 opcode = cur_blocked->scsiio.cdb[0]; 10687 entry = &ctl_cmd_table[opcode]; 10688 softc = control_softc; 10689 10690 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 10691 10692 /* 10693 * Check this I/O for LUN state changes that may 10694 * have happened while this command was blocked. 10695 * The LUN state may have been changed by a command 10696 * ahead of us in the queue, so we need to re-check 10697 * for any states that can be caused by SCSI 10698 * commands. 10699 */ 10700 if (ctl_scsiio_lun_check(softc, lun, entry, 10701 &cur_blocked->scsiio) == 0) { 10702 cur_blocked->io_hdr.flags |= 10703 CTL_FLAG_IS_WAS_ON_RTR; 10704 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue, 10705 &cur_blocked->io_hdr, links); 10706 /* 10707 * In the non CTL_DONE_THREAD case, we need 10708 * to wake up the work thread here. When 10709 * we're processing completed requests from 10710 * the work thread context, we'll pop back 10711 * around and end up pulling things off the 10712 * RtR queue. When we aren't processing 10713 * things from the work thread context, 10714 * though, we won't ever check the RtR queue. 10715 * So we need to wake up the thread to clear 10716 * things off the queue. Otherwise this 10717 * transaction will just sit on the RtR queue 10718 * until a new I/O comes in. (Which may or 10719 * may not happen...) 10720 */ 10721#ifndef CTL_DONE_THREAD 10722 ctl_wakeup_thread(); 10723#endif 10724 } else 10725 ctl_done_lock(cur_blocked, /*have_lock*/ 1); 10726 break; 10727 } 10728 default: 10729 /* 10730 * This probably shouldn't happen -- we shouldn't 10731 * get CTL_ACTION_ERROR, or anything else. 10732 */ 10733 break; 10734 } 10735 } 10736 10737 return (CTL_RETVAL_COMPLETE); 10738} 10739 10740/* 10741 * This routine (with one exception) checks LUN flags that can be set by 10742 * commands ahead of us in the OOA queue. These flags have to be checked 10743 * when a command initially comes in, and when we pull a command off the 10744 * blocked queue and are preparing to execute it. The reason we have to 10745 * check these flags for commands on the blocked queue is that the LUN 10746 * state may have been changed by a command ahead of us while we're on the 10747 * blocked queue. 10748 * 10749 * Ordering is somewhat important with these checks, so please pay 10750 * careful attention to the placement of any new checks. 10751 */ 10752static int 10753ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 10754 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10755{ 10756 int retval; 10757 10758 retval = 0; 10759 10760 /* 10761 * If this shelf is a secondary shelf controller, we have to reject 10762 * any media access commands. 10763 */ 10764#if 0 10765 /* No longer needed for HA */ 10766 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 10767 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 10768 ctl_set_lun_standby(ctsio); 10769 retval = 1; 10770 goto bailout; 10771 } 10772#endif 10773 10774 /* 10775 * Check for a reservation conflict. If this command isn't allowed 10776 * even on reserved LUNs, and if this initiator isn't the one who 10777 * reserved us, reject the command with a reservation conflict. 10778 */ 10779 if ((lun->flags & CTL_LUN_RESERVED) 10780 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10781 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 10782 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 10783 || (ctsio->io_hdr.nexus.targ_target.id != 10784 lun->rsv_nexus.targ_target.id)) { 10785 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10786 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10787 retval = 1; 10788 goto bailout; 10789 } 10790 } 10791 10792 if ( (lun->flags & CTL_LUN_PR_RESERVED) 10793 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 10794 uint32_t residx; 10795 10796 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 10797 /* 10798 * if we aren't registered or it's a res holder type 10799 * reservation and this isn't the res holder then set a 10800 * conflict. 10801 * NOTE: Commands which might be allowed on write exclusive 10802 * type reservations are checked in the particular command 10803 * for a conflict. Read and SSU are the only ones. 10804 */ 10805 if (!lun->per_res[residx].registered 10806 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10807 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10808 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10809 retval = 1; 10810 goto bailout; 10811 } 10812 10813 } 10814 10815 if ((lun->flags & CTL_LUN_OFFLINE) 10816 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 10817 ctl_set_lun_not_ready(ctsio); 10818 retval = 1; 10819 goto bailout; 10820 } 10821 10822 /* 10823 * If the LUN is stopped, see if this particular command is allowed 10824 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 10825 */ 10826 if ((lun->flags & CTL_LUN_STOPPED) 10827 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10828 /* "Logical unit not ready, initializing cmd. required" */ 10829 ctl_set_lun_stopped(ctsio); 10830 retval = 1; 10831 goto bailout; 10832 } 10833 10834 if ((lun->flags & CTL_LUN_INOPERABLE) 10835 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10836 /* "Medium format corrupted" */ 10837 ctl_set_medium_format_corrupted(ctsio); 10838 retval = 1; 10839 goto bailout; 10840 } 10841 10842bailout: 10843 return (retval); 10844 10845} 10846 10847static void 10848ctl_failover_io(union ctl_io *io, int have_lock) 10849{ 10850 ctl_set_busy(&io->scsiio); 10851 ctl_done_lock(io, have_lock); 10852} 10853 10854static void 10855ctl_failover(void) 10856{ 10857 struct ctl_lun *lun; 10858 struct ctl_softc *ctl_softc; 10859 union ctl_io *next_io, *pending_io; 10860 union ctl_io *io; 10861 int lun_idx; 10862 int i; 10863 10864 ctl_softc = control_softc; 10865 10866 mtx_lock(&ctl_softc->ctl_lock); 10867 /* 10868 * Remove any cmds from the other SC from the rtr queue. These 10869 * will obviously only be for LUNs for which we're the primary. 10870 * We can't send status or get/send data for these commands. 10871 * Since they haven't been executed yet, we can just remove them. 10872 * We'll either abort them or delete them below, depending on 10873 * which HA mode we're in. 10874 */ 10875 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 10876 io != NULL; io = next_io) { 10877 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 10878 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10879 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 10880 ctl_io_hdr, links); 10881 } 10882 10883 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 10884 lun = ctl_softc->ctl_luns[lun_idx]; 10885 if (lun==NULL) 10886 continue; 10887 10888 /* 10889 * Processor LUNs are primary on both sides. 10890 * XXX will this always be true? 10891 */ 10892 if (lun->be_lun->lun_type == T_PROCESSOR) 10893 continue; 10894 10895 if ((lun->flags & CTL_LUN_PRIMARY_SC) 10896 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10897 printf("FAILOVER: primary lun %d\n", lun_idx); 10898 /* 10899 * Remove all commands from the other SC. First from the 10900 * blocked queue then from the ooa queue. Once we have 10901 * removed them. Call ctl_check_blocked to see if there 10902 * is anything that can run. 10903 */ 10904 for (io = (union ctl_io *)TAILQ_FIRST( 10905 &lun->blocked_queue); io != NULL; io = next_io) { 10906 10907 next_io = (union ctl_io *)TAILQ_NEXT( 10908 &io->io_hdr, blocked_links); 10909 10910 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10911 TAILQ_REMOVE(&lun->blocked_queue, 10912 &io->io_hdr,blocked_links); 10913 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10914 TAILQ_REMOVE(&lun->ooa_queue, 10915 &io->io_hdr, ooa_links); 10916 10917 ctl_free_io(io); 10918 } 10919 } 10920 10921 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10922 io != NULL; io = next_io) { 10923 10924 next_io = (union ctl_io *)TAILQ_NEXT( 10925 &io->io_hdr, ooa_links); 10926 10927 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10928 10929 TAILQ_REMOVE(&lun->ooa_queue, 10930 &io->io_hdr, 10931 ooa_links); 10932 10933 ctl_free_io(io); 10934 } 10935 } 10936 ctl_check_blocked(lun); 10937 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 10938 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10939 10940 printf("FAILOVER: primary lun %d\n", lun_idx); 10941 /* 10942 * Abort all commands from the other SC. We can't 10943 * send status back for them now. These should get 10944 * cleaned up when they are completed or come out 10945 * for a datamove operation. 10946 */ 10947 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10948 io != NULL; io = next_io) { 10949 next_io = (union ctl_io *)TAILQ_NEXT( 10950 &io->io_hdr, ooa_links); 10951 10952 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10953 io->io_hdr.flags |= CTL_FLAG_ABORT; 10954 } 10955 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10956 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10957 10958 printf("FAILOVER: secondary lun %d\n", lun_idx); 10959 10960 lun->flags |= CTL_LUN_PRIMARY_SC; 10961 10962 /* 10963 * We send all I/O that was sent to this controller 10964 * and redirected to the other side back with 10965 * busy status, and have the initiator retry it. 10966 * Figuring out how much data has been transferred, 10967 * etc. and picking up where we left off would be 10968 * very tricky. 10969 * 10970 * XXX KDM need to remove I/O from the blocked 10971 * queue as well! 10972 */ 10973 for (pending_io = (union ctl_io *)TAILQ_FIRST( 10974 &lun->ooa_queue); pending_io != NULL; 10975 pending_io = next_io) { 10976 10977 next_io = (union ctl_io *)TAILQ_NEXT( 10978 &pending_io->io_hdr, ooa_links); 10979 10980 pending_io->io_hdr.flags &= 10981 ~CTL_FLAG_SENT_2OTHER_SC; 10982 10983 if (pending_io->io_hdr.flags & 10984 CTL_FLAG_IO_ACTIVE) { 10985 pending_io->io_hdr.flags |= 10986 CTL_FLAG_FAILOVER; 10987 } else { 10988 ctl_set_busy(&pending_io->scsiio); 10989 ctl_done_lock(pending_io, 10990 /*have_lock*/1); 10991 } 10992 } 10993 10994 /* 10995 * Build Unit Attention 10996 */ 10997 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10998 lun->pending_sense[i].ua_pending |= 10999 CTL_UA_ASYM_ACC_CHANGE; 11000 } 11001 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11002 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11003 printf("FAILOVER: secondary lun %d\n", lun_idx); 11004 /* 11005 * if the first io on the OOA is not on the RtR queue 11006 * add it. 11007 */ 11008 lun->flags |= CTL_LUN_PRIMARY_SC; 11009 11010 pending_io = (union ctl_io *)TAILQ_FIRST( 11011 &lun->ooa_queue); 11012 if (pending_io==NULL) { 11013 printf("Nothing on OOA queue\n"); 11014 continue; 11015 } 11016 11017 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11018 if ((pending_io->io_hdr.flags & 11019 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11020 pending_io->io_hdr.flags |= 11021 CTL_FLAG_IS_WAS_ON_RTR; 11022 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 11023 &pending_io->io_hdr, links); 11024 } 11025#if 0 11026 else 11027 { 11028 printf("Tag 0x%04x is running\n", 11029 pending_io->scsiio.tag_num); 11030 } 11031#endif 11032 11033 next_io = (union ctl_io *)TAILQ_NEXT( 11034 &pending_io->io_hdr, ooa_links); 11035 for (pending_io=next_io; pending_io != NULL; 11036 pending_io = next_io) { 11037 pending_io->io_hdr.flags &= 11038 ~CTL_FLAG_SENT_2OTHER_SC; 11039 next_io = (union ctl_io *)TAILQ_NEXT( 11040 &pending_io->io_hdr, ooa_links); 11041 if (pending_io->io_hdr.flags & 11042 CTL_FLAG_IS_WAS_ON_RTR) { 11043#if 0 11044 printf("Tag 0x%04x is running\n", 11045 pending_io->scsiio.tag_num); 11046#endif 11047 continue; 11048 } 11049 11050 switch (ctl_check_ooa(lun, pending_io, 11051 (union ctl_io *)TAILQ_PREV( 11052 &pending_io->io_hdr, ctl_ooaq, 11053 ooa_links))) { 11054 11055 case CTL_ACTION_BLOCK: 11056 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11057 &pending_io->io_hdr, 11058 blocked_links); 11059 pending_io->io_hdr.flags |= 11060 CTL_FLAG_BLOCKED; 11061 break; 11062 case CTL_ACTION_PASS: 11063 case CTL_ACTION_SKIP: 11064 pending_io->io_hdr.flags |= 11065 CTL_FLAG_IS_WAS_ON_RTR; 11066 STAILQ_INSERT_TAIL( 11067 &ctl_softc->rtr_queue, 11068 &pending_io->io_hdr, links); 11069 break; 11070 case CTL_ACTION_OVERLAP: 11071 ctl_set_overlapped_cmd( 11072 (struct ctl_scsiio *)pending_io); 11073 ctl_done_lock(pending_io, 11074 /*have_lock*/ 1); 11075 break; 11076 case CTL_ACTION_OVERLAP_TAG: 11077 ctl_set_overlapped_tag( 11078 (struct ctl_scsiio *)pending_io, 11079 pending_io->scsiio.tag_num & 0xff); 11080 ctl_done_lock(pending_io, 11081 /*have_lock*/ 1); 11082 break; 11083 case CTL_ACTION_ERROR: 11084 default: 11085 ctl_set_internal_failure( 11086 (struct ctl_scsiio *)pending_io, 11087 0, // sks_valid 11088 0); //retry count 11089 ctl_done_lock(pending_io, 11090 /*have_lock*/ 1); 11091 break; 11092 } 11093 } 11094 11095 /* 11096 * Build Unit Attention 11097 */ 11098 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11099 lun->pending_sense[i].ua_pending |= 11100 CTL_UA_ASYM_ACC_CHANGE; 11101 } 11102 } else { 11103 panic("Unhandled HA mode failover, LUN flags = %#x, " 11104 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11105 } 11106 } 11107 ctl_pause_rtr = 0; 11108 mtx_unlock(&ctl_softc->ctl_lock); 11109} 11110 11111static int 11112ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11113{ 11114 struct ctl_lun *lun; 11115 struct ctl_cmd_entry *entry; 11116 uint8_t opcode; 11117 uint32_t initidx, targ_lun; 11118 int retval; 11119 11120 retval = 0; 11121 11122 lun = NULL; 11123 11124 opcode = ctsio->cdb[0]; 11125 11126 mtx_lock(&ctl_softc->ctl_lock); 11127 11128 targ_lun = ctsio->io_hdr.nexus.targ_lun; 11129 if (ctsio->io_hdr.nexus.lun_map_fn != NULL) 11130 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun); 11131 if ((targ_lun < CTL_MAX_LUNS) 11132 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11133 lun = ctl_softc->ctl_luns[targ_lun]; 11134 /* 11135 * If the LUN is invalid, pretend that it doesn't exist. 11136 * It will go away as soon as all pending I/O has been 11137 * completed. 11138 */ 11139 if (lun->flags & CTL_LUN_DISABLED) { 11140 lun = NULL; 11141 } else { 11142 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11143 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11144 lun->be_lun; 11145 if (lun->be_lun->lun_type == T_PROCESSOR) { 11146 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11147 } 11148 } 11149 } else { 11150 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11151 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11152 } 11153 11154 entry = &ctl_cmd_table[opcode]; 11155 11156 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11157 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11158 11159 /* 11160 * Check to see whether we can send this command to LUNs that don't 11161 * exist. This should pretty much only be the case for inquiry 11162 * and request sense. Further checks, below, really require having 11163 * a LUN, so we can't really check the command anymore. Just put 11164 * it on the rtr queue. 11165 */ 11166 if (lun == NULL) { 11167 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 11168 goto queue_rtr; 11169 11170 ctl_set_unsupported_lun(ctsio); 11171 mtx_unlock(&ctl_softc->ctl_lock); 11172 ctl_done((union ctl_io *)ctsio); 11173 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11174 goto bailout; 11175 } else { 11176 /* 11177 * Every I/O goes into the OOA queue for a particular LUN, and 11178 * stays there until completion. 11179 */ 11180 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11181 11182 /* 11183 * Make sure we support this particular command on this LUN. 11184 * e.g., we don't support writes to the control LUN. 11185 */ 11186 switch (lun->be_lun->lun_type) { 11187 case T_PROCESSOR: 11188 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11189 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 11190 == 0)) { 11191 ctl_set_invalid_opcode(ctsio); 11192 mtx_unlock(&ctl_softc->ctl_lock); 11193 ctl_done((union ctl_io *)ctsio); 11194 goto bailout; 11195 } 11196 break; 11197 case T_DIRECT: 11198 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) 11199 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 11200 == 0)){ 11201 ctl_set_invalid_opcode(ctsio); 11202 mtx_unlock(&ctl_softc->ctl_lock); 11203 ctl_done((union ctl_io *)ctsio); 11204 goto bailout; 11205 } 11206 break; 11207 default: 11208 printf("Unsupported CTL LUN type %d\n", 11209 lun->be_lun->lun_type); 11210 panic("Unsupported CTL LUN type %d\n", 11211 lun->be_lun->lun_type); 11212 break; /* NOTREACHED */ 11213 } 11214 } 11215 11216 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11217 11218 /* 11219 * If we've got a request sense, it'll clear the contingent 11220 * allegiance condition. Otherwise, if we have a CA condition for 11221 * this initiator, clear it, because it sent down a command other 11222 * than request sense. 11223 */ 11224 if ((opcode != REQUEST_SENSE) 11225 && (ctl_is_set(lun->have_ca, initidx))) 11226 ctl_clear_mask(lun->have_ca, initidx); 11227 11228 /* 11229 * If the command has this flag set, it handles its own unit 11230 * attention reporting, we shouldn't do anything. Otherwise we 11231 * check for any pending unit attentions, and send them back to the 11232 * initiator. We only do this when a command initially comes in, 11233 * not when we pull it off the blocked queue. 11234 * 11235 * According to SAM-3, section 5.3.2, the order that things get 11236 * presented back to the host is basically unit attentions caused 11237 * by some sort of reset event, busy status, reservation conflicts 11238 * or task set full, and finally any other status. 11239 * 11240 * One issue here is that some of the unit attentions we report 11241 * don't fall into the "reset" category (e.g. "reported luns data 11242 * has changed"). So reporting it here, before the reservation 11243 * check, may be technically wrong. I guess the only thing to do 11244 * would be to check for and report the reset events here, and then 11245 * check for the other unit attention types after we check for a 11246 * reservation conflict. 11247 * 11248 * XXX KDM need to fix this 11249 */ 11250 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11251 ctl_ua_type ua_type; 11252 11253 ua_type = lun->pending_sense[initidx].ua_pending; 11254 if (ua_type != CTL_UA_NONE) { 11255 scsi_sense_data_type sense_format; 11256 11257 if (lun != NULL) 11258 sense_format = (lun->flags & 11259 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11260 SSD_TYPE_FIXED; 11261 else 11262 sense_format = SSD_TYPE_FIXED; 11263 11264 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11265 sense_format); 11266 if (ua_type != CTL_UA_NONE) { 11267 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11268 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11269 CTL_AUTOSENSE; 11270 ctsio->sense_len = SSD_FULL_SIZE; 11271 lun->pending_sense[initidx].ua_pending &= 11272 ~ua_type; 11273 mtx_unlock(&ctl_softc->ctl_lock); 11274 ctl_done((union ctl_io *)ctsio); 11275 goto bailout; 11276 } 11277 } 11278 } 11279 11280 11281 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11282 mtx_unlock(&ctl_softc->ctl_lock); 11283 ctl_done((union ctl_io *)ctsio); 11284 goto bailout; 11285 } 11286 11287 /* 11288 * XXX CHD this is where we want to send IO to other side if 11289 * this LUN is secondary on this SC. We will need to make a copy 11290 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11291 * the copy we send as FROM_OTHER. 11292 * We also need to stuff the address of the original IO so we can 11293 * find it easily. Something similar will need be done on the other 11294 * side so when we are done we can find the copy. 11295 */ 11296 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11297 union ctl_ha_msg msg_info; 11298 int isc_retval; 11299 11300 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11301 11302 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11303 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11304#if 0 11305 printf("1. ctsio %p\n", ctsio); 11306#endif 11307 msg_info.hdr.serializing_sc = NULL; 11308 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11309 msg_info.scsi.tag_num = ctsio->tag_num; 11310 msg_info.scsi.tag_type = ctsio->tag_type; 11311 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11312 11313 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11314 11315 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11316 (void *)&msg_info, sizeof(msg_info), 0)) > 11317 CTL_HA_STATUS_SUCCESS) { 11318 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11319 isc_retval); 11320 printf("CTL:opcode is %x\n",opcode); 11321 } else { 11322#if 0 11323 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11324#endif 11325 } 11326 11327 /* 11328 * XXX KDM this I/O is off the incoming queue, but hasn't 11329 * been inserted on any other queue. We may need to come 11330 * up with a holding queue while we wait for serialization 11331 * so that we have an idea of what we're waiting for from 11332 * the other side. 11333 */ 11334 goto bailout_unlock; 11335 } 11336 11337 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11338 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11339 ctl_ooaq, ooa_links))) { 11340 case CTL_ACTION_BLOCK: 11341 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11342 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11343 blocked_links); 11344 goto bailout_unlock; 11345 break; /* NOTREACHED */ 11346 case CTL_ACTION_PASS: 11347 case CTL_ACTION_SKIP: 11348 goto queue_rtr; 11349 break; /* NOTREACHED */ 11350 case CTL_ACTION_OVERLAP: 11351 ctl_set_overlapped_cmd(ctsio); 11352 mtx_unlock(&ctl_softc->ctl_lock); 11353 ctl_done((union ctl_io *)ctsio); 11354 goto bailout; 11355 break; /* NOTREACHED */ 11356 case CTL_ACTION_OVERLAP_TAG: 11357 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11358 mtx_unlock(&ctl_softc->ctl_lock); 11359 ctl_done((union ctl_io *)ctsio); 11360 goto bailout; 11361 break; /* NOTREACHED */ 11362 case CTL_ACTION_ERROR: 11363 default: 11364 ctl_set_internal_failure(ctsio, 11365 /*sks_valid*/ 0, 11366 /*retry_count*/ 0); 11367 mtx_unlock(&ctl_softc->ctl_lock); 11368 ctl_done((union ctl_io *)ctsio); 11369 goto bailout; 11370 break; /* NOTREACHED */ 11371 } 11372 11373 goto bailout_unlock; 11374 11375queue_rtr: 11376 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11377 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links); 11378 11379bailout_unlock: 11380 mtx_unlock(&ctl_softc->ctl_lock); 11381 11382bailout: 11383 return (retval); 11384} 11385 11386static int 11387ctl_scsiio(struct ctl_scsiio *ctsio) 11388{ 11389 int retval; 11390 struct ctl_cmd_entry *entry; 11391 11392 retval = CTL_RETVAL_COMPLETE; 11393 11394 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11395 11396 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11397 11398 /* 11399 * If this I/O has been aborted, just send it straight to 11400 * ctl_done() without executing it. 11401 */ 11402 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11403 ctl_done((union ctl_io *)ctsio); 11404 goto bailout; 11405 } 11406 11407 /* 11408 * All the checks should have been handled by ctl_scsiio_precheck(). 11409 * We should be clear now to just execute the I/O. 11410 */ 11411 retval = entry->execute(ctsio); 11412 11413bailout: 11414 return (retval); 11415} 11416 11417/* 11418 * Since we only implement one target right now, a bus reset simply resets 11419 * our single target. 11420 */ 11421static int 11422ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11423{ 11424 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11425} 11426 11427static int 11428ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11429 ctl_ua_type ua_type) 11430{ 11431 struct ctl_lun *lun; 11432 int retval; 11433 11434 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11435 union ctl_ha_msg msg_info; 11436 11437 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11438 msg_info.hdr.nexus = io->io_hdr.nexus; 11439 if (ua_type==CTL_UA_TARG_RESET) 11440 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11441 else 11442 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11443 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11444 msg_info.hdr.original_sc = NULL; 11445 msg_info.hdr.serializing_sc = NULL; 11446 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11447 (void *)&msg_info, sizeof(msg_info), 0)) { 11448 } 11449 } 11450 retval = 0; 11451 11452 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11453 retval += ctl_lun_reset(lun, io, ua_type); 11454 11455 return (retval); 11456} 11457 11458/* 11459 * The LUN should always be set. The I/O is optional, and is used to 11460 * distinguish between I/Os sent by this initiator, and by other 11461 * initiators. We set unit attention for initiators other than this one. 11462 * SAM-3 is vague on this point. It does say that a unit attention should 11463 * be established for other initiators when a LUN is reset (see section 11464 * 5.7.3), but it doesn't specifically say that the unit attention should 11465 * be established for this particular initiator when a LUN is reset. Here 11466 * is the relevant text, from SAM-3 rev 8: 11467 * 11468 * 5.7.2 When a SCSI initiator port aborts its own tasks 11469 * 11470 * When a SCSI initiator port causes its own task(s) to be aborted, no 11471 * notification that the task(s) have been aborted shall be returned to 11472 * the SCSI initiator port other than the completion response for the 11473 * command or task management function action that caused the task(s) to 11474 * be aborted and notification(s) associated with related effects of the 11475 * action (e.g., a reset unit attention condition). 11476 * 11477 * XXX KDM for now, we're setting unit attention for all initiators. 11478 */ 11479static int 11480ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11481{ 11482 union ctl_io *xio; 11483#if 0 11484 uint32_t initindex; 11485#endif 11486 int i; 11487 11488 /* 11489 * Run through the OOA queue and abort each I/O. 11490 */ 11491#if 0 11492 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11493#endif 11494 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11495 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11496 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11497 } 11498 11499 /* 11500 * This version sets unit attention for every 11501 */ 11502#if 0 11503 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11504 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11505 if (initindex == i) 11506 continue; 11507 lun->pending_sense[i].ua_pending |= ua_type; 11508 } 11509#endif 11510 11511 /* 11512 * A reset (any kind, really) clears reservations established with 11513 * RESERVE/RELEASE. It does not clear reservations established 11514 * with PERSISTENT RESERVE OUT, but we don't support that at the 11515 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11516 * reservations made with the RESERVE/RELEASE commands, because 11517 * those commands are obsolete in SPC-3. 11518 */ 11519 lun->flags &= ~CTL_LUN_RESERVED; 11520 11521 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11522 ctl_clear_mask(lun->have_ca, i); 11523 lun->pending_sense[i].ua_pending |= ua_type; 11524 } 11525 11526 return (0); 11527} 11528 11529static int 11530ctl_abort_task(union ctl_io *io) 11531{ 11532 union ctl_io *xio; 11533 struct ctl_lun *lun; 11534 struct ctl_softc *ctl_softc; 11535#if 0 11536 struct sbuf sb; 11537 char printbuf[128]; 11538#endif 11539 int found; 11540 uint32_t targ_lun; 11541 11542 ctl_softc = control_softc; 11543 found = 0; 11544 11545 /* 11546 * Look up the LUN. 11547 */ 11548 targ_lun = io->io_hdr.nexus.targ_lun; 11549 if (io->io_hdr.nexus.lun_map_fn != NULL) 11550 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 11551 if ((targ_lun < CTL_MAX_LUNS) 11552 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11553 lun = ctl_softc->ctl_luns[targ_lun]; 11554 else 11555 goto bailout; 11556 11557#if 0 11558 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11559 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11560#endif 11561 11562 /* 11563 * Run through the OOA queue and attempt to find the given I/O. 11564 * The target port, initiator ID, tag type and tag number have to 11565 * match the values that we got from the initiator. If we have an 11566 * untagged command to abort, simply abort the first untagged command 11567 * we come to. We only allow one untagged command at a time of course. 11568 */ 11569#if 0 11570 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11571#endif 11572 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11573 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11574#if 0 11575 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11576 11577 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11578 lun->lun, xio->scsiio.tag_num, 11579 xio->scsiio.tag_type, 11580 (xio->io_hdr.blocked_links.tqe_prev 11581 == NULL) ? "" : " BLOCKED", 11582 (xio->io_hdr.flags & 11583 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11584 (xio->io_hdr.flags & 11585 CTL_FLAG_ABORT) ? " ABORT" : "", 11586 (xio->io_hdr.flags & 11587 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11588 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11589 sbuf_finish(&sb); 11590 printf("%s\n", sbuf_data(&sb)); 11591#endif 11592 11593 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 11594 && (xio->io_hdr.nexus.initid.id == 11595 io->io_hdr.nexus.initid.id)) { 11596 /* 11597 * If the abort says that the task is untagged, the 11598 * task in the queue must be untagged. Otherwise, 11599 * we just check to see whether the tag numbers 11600 * match. This is because the QLogic firmware 11601 * doesn't pass back the tag type in an abort 11602 * request. 11603 */ 11604#if 0 11605 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11606 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11607 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 11608#endif 11609 /* 11610 * XXX KDM we've got problems with FC, because it 11611 * doesn't send down a tag type with aborts. So we 11612 * can only really go by the tag number... 11613 * This may cause problems with parallel SCSI. 11614 * Need to figure that out!! 11615 */ 11616 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11617 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11618 found = 1; 11619 if ((io->io_hdr.flags & 11620 CTL_FLAG_FROM_OTHER_SC) == 0 && 11621 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11622 union ctl_ha_msg msg_info; 11623 11624 io->io_hdr.flags |= 11625 CTL_FLAG_SENT_2OTHER_SC; 11626 msg_info.hdr.nexus = io->io_hdr.nexus; 11627 msg_info.task.task_action = 11628 CTL_TASK_ABORT_TASK; 11629 msg_info.task.tag_num = 11630 io->taskio.tag_num; 11631 msg_info.task.tag_type = 11632 io->taskio.tag_type; 11633 msg_info.hdr.msg_type = 11634 CTL_MSG_MANAGE_TASKS; 11635 msg_info.hdr.original_sc = NULL; 11636 msg_info.hdr.serializing_sc = NULL; 11637#if 0 11638 printf("Sent Abort to other side\n"); 11639#endif 11640 if (CTL_HA_STATUS_SUCCESS != 11641 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11642 (void *)&msg_info, 11643 sizeof(msg_info), 0)) { 11644 } 11645 } 11646#if 0 11647 printf("ctl_abort_task: found I/O to abort\n"); 11648#endif 11649 break; 11650 } 11651 } 11652 } 11653 11654bailout: 11655 11656 if (found == 0) { 11657 /* 11658 * This isn't really an error. It's entirely possible for 11659 * the abort and command completion to cross on the wire. 11660 * This is more of an informative/diagnostic error. 11661 */ 11662#if 0 11663 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11664 "%d:%d:%d:%d tag %d type %d\n", 11665 io->io_hdr.nexus.initid.id, 11666 io->io_hdr.nexus.targ_port, 11667 io->io_hdr.nexus.targ_target.id, 11668 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11669 io->taskio.tag_type); 11670#endif 11671 return (1); 11672 } else 11673 return (0); 11674} 11675 11676/* 11677 * This routine cannot block! It must be callable from an interrupt 11678 * handler as well as from the work thread. 11679 */ 11680static void 11681ctl_run_task_queue(struct ctl_softc *ctl_softc) 11682{ 11683 union ctl_io *io, *next_io; 11684 11685 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 11686 11687 CTL_DEBUG_PRINT(("ctl_run_task_queue\n")); 11688 11689 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue); 11690 io != NULL; io = next_io) { 11691 int retval; 11692 const char *task_desc; 11693 11694 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11695 11696 retval = 0; 11697 11698 switch (io->io_hdr.io_type) { 11699 case CTL_IO_TASK: { 11700 task_desc = ctl_scsi_task_string(&io->taskio); 11701 if (task_desc != NULL) { 11702#ifdef NEEDTOPORT 11703 csevent_log(CSC_CTL | CSC_SHELF_SW | 11704 CTL_TASK_REPORT, 11705 csevent_LogType_Trace, 11706 csevent_Severity_Information, 11707 csevent_AlertLevel_Green, 11708 csevent_FRU_Firmware, 11709 csevent_FRU_Unknown, 11710 "CTL: received task: %s",task_desc); 11711#endif 11712 } else { 11713#ifdef NEEDTOPORT 11714 csevent_log(CSC_CTL | CSC_SHELF_SW | 11715 CTL_TASK_REPORT, 11716 csevent_LogType_Trace, 11717 csevent_Severity_Information, 11718 csevent_AlertLevel_Green, 11719 csevent_FRU_Firmware, 11720 csevent_FRU_Unknown, 11721 "CTL: received unknown task " 11722 "type: %d (%#x)", 11723 io->taskio.task_action, 11724 io->taskio.task_action); 11725#endif 11726 } 11727 switch (io->taskio.task_action) { 11728 case CTL_TASK_ABORT_TASK: 11729 retval = ctl_abort_task(io); 11730 break; 11731 case CTL_TASK_ABORT_TASK_SET: 11732 break; 11733 case CTL_TASK_CLEAR_ACA: 11734 break; 11735 case CTL_TASK_CLEAR_TASK_SET: 11736 break; 11737 case CTL_TASK_LUN_RESET: { 11738 struct ctl_lun *lun; 11739 uint32_t targ_lun; 11740 int retval; 11741 11742 targ_lun = io->io_hdr.nexus.targ_lun; 11743 if (io->io_hdr.nexus.lun_map_fn != NULL) 11744 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 11745 11746 if ((targ_lun < CTL_MAX_LUNS) 11747 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11748 lun = ctl_softc->ctl_luns[targ_lun]; 11749 else { 11750 retval = 1; 11751 break; 11752 } 11753 11754 if (!(io->io_hdr.flags & 11755 CTL_FLAG_FROM_OTHER_SC)) { 11756 union ctl_ha_msg msg_info; 11757 11758 io->io_hdr.flags |= 11759 CTL_FLAG_SENT_2OTHER_SC; 11760 msg_info.hdr.msg_type = 11761 CTL_MSG_MANAGE_TASKS; 11762 msg_info.hdr.nexus = io->io_hdr.nexus; 11763 msg_info.task.task_action = 11764 CTL_TASK_LUN_RESET; 11765 msg_info.hdr.original_sc = NULL; 11766 msg_info.hdr.serializing_sc = NULL; 11767 if (CTL_HA_STATUS_SUCCESS != 11768 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11769 (void *)&msg_info, 11770 sizeof(msg_info), 0)) { 11771 } 11772 } 11773 11774 retval = ctl_lun_reset(lun, io, 11775 CTL_UA_LUN_RESET); 11776 break; 11777 } 11778 case CTL_TASK_TARGET_RESET: 11779 retval = ctl_target_reset(ctl_softc, io, 11780 CTL_UA_TARG_RESET); 11781 break; 11782 case CTL_TASK_BUS_RESET: 11783 retval = ctl_bus_reset(ctl_softc, io); 11784 break; 11785 case CTL_TASK_PORT_LOGIN: 11786 break; 11787 case CTL_TASK_PORT_LOGOUT: 11788 break; 11789 default: 11790 printf("ctl_run_task_queue: got unknown task " 11791 "management event %d\n", 11792 io->taskio.task_action); 11793 break; 11794 } 11795 if (retval == 0) 11796 io->io_hdr.status = CTL_SUCCESS; 11797 else 11798 io->io_hdr.status = CTL_ERROR; 11799 11800 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr, 11801 ctl_io_hdr, links); 11802 /* 11803 * This will queue this I/O to the done queue, but the 11804 * work thread won't be able to process it until we 11805 * return and the lock is released. 11806 */ 11807 ctl_done_lock(io, /*have_lock*/ 1); 11808 break; 11809 } 11810 default: { 11811 11812 printf("%s: invalid I/O type %d msg %d cdb %x" 11813 " iptl: %ju:%d:%ju:%d tag 0x%04x\n", 11814 __func__, io->io_hdr.io_type, 11815 io->io_hdr.msg_type, io->scsiio.cdb[0], 11816 (uintmax_t)io->io_hdr.nexus.initid.id, 11817 io->io_hdr.nexus.targ_port, 11818 (uintmax_t)io->io_hdr.nexus.targ_target.id, 11819 io->io_hdr.nexus.targ_lun /* XXX */, 11820 (io->io_hdr.io_type == CTL_IO_TASK) ? 11821 io->taskio.tag_num : io->scsiio.tag_num); 11822 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr, 11823 ctl_io_hdr, links); 11824 ctl_free_io(io); 11825 break; 11826 } 11827 } 11828 } 11829 11830 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING; 11831} 11832 11833/* 11834 * For HA operation. Handle commands that come in from the other 11835 * controller. 11836 */ 11837static void 11838ctl_handle_isc(union ctl_io *io) 11839{ 11840 int free_io; 11841 struct ctl_lun *lun; 11842 struct ctl_softc *ctl_softc; 11843 uint32_t targ_lun; 11844 11845 ctl_softc = control_softc; 11846 11847 targ_lun = io->io_hdr.nexus.targ_lun; 11848 if (io->io_hdr.nexus.lun_map_fn != NULL) 11849 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 11850 lun = ctl_softc->ctl_luns[targ_lun]; 11851 11852 switch (io->io_hdr.msg_type) { 11853 case CTL_MSG_SERIALIZE: 11854 free_io = ctl_serialize_other_sc_cmd(&io->scsiio, 11855 /*have_lock*/ 0); 11856 break; 11857 case CTL_MSG_R2R: { 11858 uint8_t opcode; 11859 struct ctl_cmd_entry *entry; 11860 11861 /* 11862 * This is only used in SER_ONLY mode. 11863 */ 11864 free_io = 0; 11865 opcode = io->scsiio.cdb[0]; 11866 entry = &ctl_cmd_table[opcode]; 11867 mtx_lock(&ctl_softc->ctl_lock); 11868 if (ctl_scsiio_lun_check(ctl_softc, lun, 11869 entry, (struct ctl_scsiio *)io) != 0) { 11870 ctl_done_lock(io, /*have_lock*/ 1); 11871 mtx_unlock(&ctl_softc->ctl_lock); 11872 break; 11873 } 11874 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11875 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 11876 &io->io_hdr, links); 11877 mtx_unlock(&ctl_softc->ctl_lock); 11878 break; 11879 } 11880 case CTL_MSG_FINISH_IO: 11881 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 11882 free_io = 0; 11883 ctl_done_lock(io, /*have_lock*/ 0); 11884 } else { 11885 free_io = 1; 11886 mtx_lock(&ctl_softc->ctl_lock); 11887 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11888 ooa_links); 11889 STAILQ_REMOVE(&ctl_softc->task_queue, 11890 &io->io_hdr, ctl_io_hdr, links); 11891 ctl_check_blocked(lun); 11892 mtx_unlock(&ctl_softc->ctl_lock); 11893 } 11894 break; 11895 case CTL_MSG_PERS_ACTION: 11896 ctl_hndl_per_res_out_on_other_sc( 11897 (union ctl_ha_msg *)&io->presio.pr_msg); 11898 free_io = 1; 11899 break; 11900 case CTL_MSG_BAD_JUJU: 11901 free_io = 0; 11902 ctl_done_lock(io, /*have_lock*/ 0); 11903 break; 11904 case CTL_MSG_DATAMOVE: 11905 /* Only used in XFER mode */ 11906 free_io = 0; 11907 ctl_datamove_remote(io); 11908 break; 11909 case CTL_MSG_DATAMOVE_DONE: 11910 /* Only used in XFER mode */ 11911 free_io = 0; 11912 io->scsiio.be_move_done(io); 11913 break; 11914 default: 11915 free_io = 1; 11916 printf("%s: Invalid message type %d\n", 11917 __func__, io->io_hdr.msg_type); 11918 break; 11919 } 11920 if (free_io) 11921 ctl_free_io(io); 11922 11923} 11924 11925 11926/* 11927 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11928 * there is no match. 11929 */ 11930static ctl_lun_error_pattern 11931ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11932{ 11933 struct ctl_cmd_entry *entry; 11934 ctl_lun_error_pattern filtered_pattern, pattern; 11935 uint8_t opcode; 11936 11937 pattern = desc->error_pattern; 11938 11939 /* 11940 * XXX KDM we need more data passed into this function to match a 11941 * custom pattern, and we actually need to implement custom pattern 11942 * matching. 11943 */ 11944 if (pattern & CTL_LUN_PAT_CMD) 11945 return (CTL_LUN_PAT_CMD); 11946 11947 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 11948 return (CTL_LUN_PAT_ANY); 11949 11950 opcode = ctsio->cdb[0]; 11951 entry = &ctl_cmd_table[opcode]; 11952 11953 filtered_pattern = entry->pattern & pattern; 11954 11955 /* 11956 * If the user requested specific flags in the pattern (e.g. 11957 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 11958 * flags. 11959 * 11960 * If the user did not specify any flags, it doesn't matter whether 11961 * or not the command supports the flags. 11962 */ 11963 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 11964 (pattern & ~CTL_LUN_PAT_MASK)) 11965 return (CTL_LUN_PAT_NONE); 11966 11967 /* 11968 * If the user asked for a range check, see if the requested LBA 11969 * range overlaps with this command's LBA range. 11970 */ 11971 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 11972 uint64_t lba1; 11973 uint32_t len1; 11974 ctl_action action; 11975 int retval; 11976 11977 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 11978 if (retval != 0) 11979 return (CTL_LUN_PAT_NONE); 11980 11981 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 11982 desc->lba_range.len); 11983 /* 11984 * A "pass" means that the LBA ranges don't overlap, so 11985 * this doesn't match the user's range criteria. 11986 */ 11987 if (action == CTL_ACTION_PASS) 11988 return (CTL_LUN_PAT_NONE); 11989 } 11990 11991 return (filtered_pattern); 11992} 11993 11994static void 11995ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 11996{ 11997 struct ctl_error_desc *desc, *desc2; 11998 11999 mtx_assert(&control_softc->ctl_lock, MA_OWNED); 12000 12001 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12002 ctl_lun_error_pattern pattern; 12003 /* 12004 * Check to see whether this particular command matches 12005 * the pattern in the descriptor. 12006 */ 12007 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12008 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12009 continue; 12010 12011 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12012 case CTL_LUN_INJ_ABORTED: 12013 ctl_set_aborted(&io->scsiio); 12014 break; 12015 case CTL_LUN_INJ_MEDIUM_ERR: 12016 ctl_set_medium_error(&io->scsiio); 12017 break; 12018 case CTL_LUN_INJ_UA: 12019 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12020 * OCCURRED */ 12021 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12022 break; 12023 case CTL_LUN_INJ_CUSTOM: 12024 /* 12025 * We're assuming the user knows what he is doing. 12026 * Just copy the sense information without doing 12027 * checks. 12028 */ 12029 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12030 ctl_min(sizeof(desc->custom_sense), 12031 sizeof(io->scsiio.sense_data))); 12032 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12033 io->scsiio.sense_len = SSD_FULL_SIZE; 12034 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12035 break; 12036 case CTL_LUN_INJ_NONE: 12037 default: 12038 /* 12039 * If this is an error injection type we don't know 12040 * about, clear the continuous flag (if it is set) 12041 * so it will get deleted below. 12042 */ 12043 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12044 break; 12045 } 12046 /* 12047 * By default, each error injection action is a one-shot 12048 */ 12049 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12050 continue; 12051 12052 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12053 12054 free(desc, M_CTL); 12055 } 12056} 12057 12058#ifdef CTL_IO_DELAY 12059static void 12060ctl_datamove_timer_wakeup(void *arg) 12061{ 12062 union ctl_io *io; 12063 12064 io = (union ctl_io *)arg; 12065 12066 ctl_datamove(io); 12067} 12068#endif /* CTL_IO_DELAY */ 12069 12070void 12071ctl_datamove(union ctl_io *io) 12072{ 12073 void (*fe_datamove)(union ctl_io *io); 12074 12075 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12076 12077 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12078 12079#ifdef CTL_TIME_IO 12080 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12081 char str[256]; 12082 char path_str[64]; 12083 struct sbuf sb; 12084 12085 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12086 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12087 12088 sbuf_cat(&sb, path_str); 12089 switch (io->io_hdr.io_type) { 12090 case CTL_IO_SCSI: 12091 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12092 sbuf_printf(&sb, "\n"); 12093 sbuf_cat(&sb, path_str); 12094 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12095 io->scsiio.tag_num, io->scsiio.tag_type); 12096 break; 12097 case CTL_IO_TASK: 12098 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12099 "Tag Type: %d\n", io->taskio.task_action, 12100 io->taskio.tag_num, io->taskio.tag_type); 12101 break; 12102 default: 12103 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12104 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12105 break; 12106 } 12107 sbuf_cat(&sb, path_str); 12108 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12109 (intmax_t)time_uptime - io->io_hdr.start_time); 12110 sbuf_finish(&sb); 12111 printf("%s", sbuf_data(&sb)); 12112 } 12113#endif /* CTL_TIME_IO */ 12114 12115 mtx_lock(&control_softc->ctl_lock); 12116#ifdef CTL_IO_DELAY 12117 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12118 struct ctl_lun *lun; 12119 12120 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12121 12122 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12123 } else { 12124 struct ctl_lun *lun; 12125 12126 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12127 if ((lun != NULL) 12128 && (lun->delay_info.datamove_delay > 0)) { 12129 struct callout *callout; 12130 12131 callout = (struct callout *)&io->io_hdr.timer_bytes; 12132 callout_init(callout, /*mpsafe*/ 1); 12133 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12134 callout_reset(callout, 12135 lun->delay_info.datamove_delay * hz, 12136 ctl_datamove_timer_wakeup, io); 12137 if (lun->delay_info.datamove_type == 12138 CTL_DELAY_TYPE_ONESHOT) 12139 lun->delay_info.datamove_delay = 0; 12140 mtx_unlock(&control_softc->ctl_lock); 12141 return; 12142 } 12143 } 12144#endif 12145 /* 12146 * If we have any pending task management commands, process them 12147 * first. This is necessary to eliminate a race condition with the 12148 * FETD: 12149 * 12150 * - FETD submits a task management command, like an abort. 12151 * - Back end calls fe_datamove() to move the data for the aborted 12152 * command. The FETD can't really accept it, but if it did, it 12153 * would end up transmitting data for a command that the initiator 12154 * told us to abort. 12155 * 12156 * We close the race by processing all pending task management 12157 * commands here (we can't block!), and then check this I/O to see 12158 * if it has been aborted. If so, return it to the back end with 12159 * bad status, so the back end can say return an error to the back end 12160 * and then when the back end returns an error, we can return the 12161 * aborted command to the FETD, so it can clean up its resources. 12162 */ 12163 if (control_softc->flags & CTL_FLAG_TASK_PENDING) 12164 ctl_run_task_queue(control_softc); 12165 12166 /* 12167 * This command has been aborted. Set the port status, so we fail 12168 * the data move. 12169 */ 12170 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12171 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12172 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12173 io->io_hdr.nexus.targ_port, 12174 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12175 io->io_hdr.nexus.targ_lun); 12176 io->io_hdr.status = CTL_CMD_ABORTED; 12177 io->io_hdr.port_status = 31337; 12178 mtx_unlock(&control_softc->ctl_lock); 12179 /* 12180 * Note that the backend, in this case, will get the 12181 * callback in its context. In other cases it may get 12182 * called in the frontend's interrupt thread context. 12183 */ 12184 io->scsiio.be_move_done(io); 12185 return; 12186 } 12187 12188 /* 12189 * If we're in XFER mode and this I/O is from the other shelf 12190 * controller, we need to send the DMA to the other side to 12191 * actually transfer the data to/from the host. In serialize only 12192 * mode the transfer happens below CTL and ctl_datamove() is only 12193 * called on the machine that originally received the I/O. 12194 */ 12195 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12196 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12197 union ctl_ha_msg msg; 12198 uint32_t sg_entries_sent; 12199 int do_sg_copy; 12200 int i; 12201 12202 memset(&msg, 0, sizeof(msg)); 12203 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12204 msg.hdr.original_sc = io->io_hdr.original_sc; 12205 msg.hdr.serializing_sc = io; 12206 msg.hdr.nexus = io->io_hdr.nexus; 12207 msg.dt.flags = io->io_hdr.flags; 12208 /* 12209 * We convert everything into a S/G list here. We can't 12210 * pass by reference, only by value between controllers. 12211 * So we can't pass a pointer to the S/G list, only as many 12212 * S/G entries as we can fit in here. If it's possible for 12213 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12214 * then we need to break this up into multiple transfers. 12215 */ 12216 if (io->scsiio.kern_sg_entries == 0) { 12217 msg.dt.kern_sg_entries = 1; 12218 /* 12219 * If this is in cached memory, flush the cache 12220 * before we send the DMA request to the other 12221 * controller. We want to do this in either the 12222 * read or the write case. The read case is 12223 * straightforward. In the write case, we want to 12224 * make sure nothing is in the local cache that 12225 * could overwrite the DMAed data. 12226 */ 12227 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12228 /* 12229 * XXX KDM use bus_dmamap_sync() here. 12230 */ 12231 } 12232 12233 /* 12234 * Convert to a physical address if this is a 12235 * virtual address. 12236 */ 12237 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12238 msg.dt.sg_list[0].addr = 12239 io->scsiio.kern_data_ptr; 12240 } else { 12241 /* 12242 * XXX KDM use busdma here! 12243 */ 12244#if 0 12245 msg.dt.sg_list[0].addr = (void *) 12246 vtophys(io->scsiio.kern_data_ptr); 12247#endif 12248 } 12249 12250 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12251 do_sg_copy = 0; 12252 } else { 12253 struct ctl_sg_entry *sgl; 12254 12255 do_sg_copy = 1; 12256 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12257 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12258 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12259 /* 12260 * XXX KDM use bus_dmamap_sync() here. 12261 */ 12262 } 12263 } 12264 12265 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12266 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12267 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12268 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12269 msg.dt.sg_sequence = 0; 12270 12271 /* 12272 * Loop until we've sent all of the S/G entries. On the 12273 * other end, we'll recompose these S/G entries into one 12274 * contiguous list before passing it to the 12275 */ 12276 for (sg_entries_sent = 0; sg_entries_sent < 12277 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12278 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12279 sizeof(msg.dt.sg_list[0])), 12280 msg.dt.kern_sg_entries - sg_entries_sent); 12281 12282 if (do_sg_copy != 0) { 12283 struct ctl_sg_entry *sgl; 12284 int j; 12285 12286 sgl = (struct ctl_sg_entry *) 12287 io->scsiio.kern_data_ptr; 12288 /* 12289 * If this is in cached memory, flush the cache 12290 * before we send the DMA request to the other 12291 * controller. We want to do this in either 12292 * the * read or the write case. The read 12293 * case is straightforward. In the write 12294 * case, we want to make sure nothing is 12295 * in the local cache that could overwrite 12296 * the DMAed data. 12297 */ 12298 12299 for (i = sg_entries_sent, j = 0; 12300 i < msg.dt.cur_sg_entries; i++, j++) { 12301 if ((io->io_hdr.flags & 12302 CTL_FLAG_NO_DATASYNC) == 0) { 12303 /* 12304 * XXX KDM use bus_dmamap_sync() 12305 */ 12306 } 12307 if ((io->io_hdr.flags & 12308 CTL_FLAG_BUS_ADDR) == 0) { 12309 /* 12310 * XXX KDM use busdma. 12311 */ 12312#if 0 12313 msg.dt.sg_list[j].addr =(void *) 12314 vtophys(sgl[i].addr); 12315#endif 12316 } else { 12317 msg.dt.sg_list[j].addr = 12318 sgl[i].addr; 12319 } 12320 msg.dt.sg_list[j].len = sgl[i].len; 12321 } 12322 } 12323 12324 sg_entries_sent += msg.dt.cur_sg_entries; 12325 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12326 msg.dt.sg_last = 1; 12327 else 12328 msg.dt.sg_last = 0; 12329 12330 /* 12331 * XXX KDM drop and reacquire the lock here? 12332 */ 12333 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12334 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12335 /* 12336 * XXX do something here. 12337 */ 12338 } 12339 12340 msg.dt.sent_sg_entries = sg_entries_sent; 12341 } 12342 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12343 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12344 ctl_failover_io(io, /*have_lock*/ 1); 12345 12346 } else { 12347 12348 /* 12349 * Lookup the fe_datamove() function for this particular 12350 * front end. 12351 */ 12352 fe_datamove = 12353 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12354 mtx_unlock(&control_softc->ctl_lock); 12355 12356 fe_datamove(io); 12357 } 12358} 12359 12360static void 12361ctl_send_datamove_done(union ctl_io *io, int have_lock) 12362{ 12363 union ctl_ha_msg msg; 12364 int isc_status; 12365 12366 memset(&msg, 0, sizeof(msg)); 12367 12368 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12369 msg.hdr.original_sc = io; 12370 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12371 msg.hdr.nexus = io->io_hdr.nexus; 12372 msg.hdr.status = io->io_hdr.status; 12373 msg.scsi.tag_num = io->scsiio.tag_num; 12374 msg.scsi.tag_type = io->scsiio.tag_type; 12375 msg.scsi.scsi_status = io->scsiio.scsi_status; 12376 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12377 sizeof(io->scsiio.sense_data)); 12378 msg.scsi.sense_len = io->scsiio.sense_len; 12379 msg.scsi.sense_residual = io->scsiio.sense_residual; 12380 msg.scsi.fetd_status = io->io_hdr.port_status; 12381 msg.scsi.residual = io->scsiio.residual; 12382 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12383 12384 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12385 ctl_failover_io(io, /*have_lock*/ have_lock); 12386 return; 12387 } 12388 12389 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12390 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12391 /* XXX do something if this fails */ 12392 } 12393 12394} 12395 12396/* 12397 * The DMA to the remote side is done, now we need to tell the other side 12398 * we're done so it can continue with its data movement. 12399 */ 12400static void 12401ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12402{ 12403 union ctl_io *io; 12404 12405 io = rq->context; 12406 12407 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12408 printf("%s: ISC DMA write failed with error %d", __func__, 12409 rq->ret); 12410 ctl_set_internal_failure(&io->scsiio, 12411 /*sks_valid*/ 1, 12412 /*retry_count*/ rq->ret); 12413 } 12414 12415 ctl_dt_req_free(rq); 12416 12417 /* 12418 * In this case, we had to malloc the memory locally. Free it. 12419 */ 12420 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12421 int i; 12422 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12423 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12424 } 12425 /* 12426 * The data is in local and remote memory, so now we need to send 12427 * status (good or back) back to the other side. 12428 */ 12429 ctl_send_datamove_done(io, /*have_lock*/ 0); 12430} 12431 12432/* 12433 * We've moved the data from the host/controller into local memory. Now we 12434 * need to push it over to the remote controller's memory. 12435 */ 12436static int 12437ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12438{ 12439 int retval; 12440 12441 retval = 0; 12442 12443 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12444 ctl_datamove_remote_write_cb); 12445 12446 return (retval); 12447} 12448 12449static void 12450ctl_datamove_remote_write(union ctl_io *io) 12451{ 12452 int retval; 12453 void (*fe_datamove)(union ctl_io *io); 12454 12455 /* 12456 * - Get the data from the host/HBA into local memory. 12457 * - DMA memory from the local controller to the remote controller. 12458 * - Send status back to the remote controller. 12459 */ 12460 12461 retval = ctl_datamove_remote_sgl_setup(io); 12462 if (retval != 0) 12463 return; 12464 12465 /* Switch the pointer over so the FETD knows what to do */ 12466 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12467 12468 /* 12469 * Use a custom move done callback, since we need to send completion 12470 * back to the other controller, not to the backend on this side. 12471 */ 12472 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12473 12474 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12475 12476 fe_datamove(io); 12477 12478 return; 12479 12480} 12481 12482static int 12483ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12484{ 12485#if 0 12486 char str[256]; 12487 char path_str[64]; 12488 struct sbuf sb; 12489#endif 12490 12491 /* 12492 * In this case, we had to malloc the memory locally. Free it. 12493 */ 12494 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12495 int i; 12496 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12497 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12498 } 12499 12500#if 0 12501 scsi_path_string(io, path_str, sizeof(path_str)); 12502 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12503 sbuf_cat(&sb, path_str); 12504 scsi_command_string(&io->scsiio, NULL, &sb); 12505 sbuf_printf(&sb, "\n"); 12506 sbuf_cat(&sb, path_str); 12507 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12508 io->scsiio.tag_num, io->scsiio.tag_type); 12509 sbuf_cat(&sb, path_str); 12510 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12511 io->io_hdr.flags, io->io_hdr.status); 12512 sbuf_finish(&sb); 12513 printk("%s", sbuf_data(&sb)); 12514#endif 12515 12516 12517 /* 12518 * The read is done, now we need to send status (good or bad) back 12519 * to the other side. 12520 */ 12521 ctl_send_datamove_done(io, /*have_lock*/ 0); 12522 12523 return (0); 12524} 12525 12526static void 12527ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12528{ 12529 union ctl_io *io; 12530 void (*fe_datamove)(union ctl_io *io); 12531 12532 io = rq->context; 12533 12534 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12535 printf("%s: ISC DMA read failed with error %d", __func__, 12536 rq->ret); 12537 ctl_set_internal_failure(&io->scsiio, 12538 /*sks_valid*/ 1, 12539 /*retry_count*/ rq->ret); 12540 } 12541 12542 ctl_dt_req_free(rq); 12543 12544 /* Switch the pointer over so the FETD knows what to do */ 12545 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12546 12547 /* 12548 * Use a custom move done callback, since we need to send completion 12549 * back to the other controller, not to the backend on this side. 12550 */ 12551 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12552 12553 /* XXX KDM add checks like the ones in ctl_datamove? */ 12554 12555 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12556 12557 fe_datamove(io); 12558} 12559 12560static int 12561ctl_datamove_remote_sgl_setup(union ctl_io *io) 12562{ 12563 struct ctl_sg_entry *local_sglist, *remote_sglist; 12564 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12565 struct ctl_softc *softc; 12566 int retval; 12567 int i; 12568 12569 retval = 0; 12570 softc = control_softc; 12571 12572 local_sglist = io->io_hdr.local_sglist; 12573 local_dma_sglist = io->io_hdr.local_dma_sglist; 12574 remote_sglist = io->io_hdr.remote_sglist; 12575 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12576 12577 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12578 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12579 local_sglist[i].len = remote_sglist[i].len; 12580 12581 /* 12582 * XXX Detect the situation where the RS-level I/O 12583 * redirector on the other side has already read the 12584 * data off of the AOR RS on this side, and 12585 * transferred it to remote (mirror) memory on the 12586 * other side. Since we already have the data in 12587 * memory here, we just need to use it. 12588 * 12589 * XXX KDM this can probably be removed once we 12590 * get the cache device code in and take the 12591 * current AOR implementation out. 12592 */ 12593#ifdef NEEDTOPORT 12594 if ((remote_sglist[i].addr >= 12595 (void *)vtophys(softc->mirr->addr)) 12596 && (remote_sglist[i].addr < 12597 ((void *)vtophys(softc->mirr->addr) + 12598 CacheMirrorOffset))) { 12599 local_sglist[i].addr = remote_sglist[i].addr - 12600 CacheMirrorOffset; 12601 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12602 CTL_FLAG_DATA_IN) 12603 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12604 } else { 12605 local_sglist[i].addr = remote_sglist[i].addr + 12606 CacheMirrorOffset; 12607 } 12608#endif 12609#if 0 12610 printf("%s: local %p, remote %p, len %d\n", 12611 __func__, local_sglist[i].addr, 12612 remote_sglist[i].addr, local_sglist[i].len); 12613#endif 12614 } 12615 } else { 12616 uint32_t len_to_go; 12617 12618 /* 12619 * In this case, we don't have automatically allocated 12620 * memory for this I/O on this controller. This typically 12621 * happens with internal CTL I/O -- e.g. inquiry, mode 12622 * sense, etc. Anything coming from RAIDCore will have 12623 * a mirror area available. 12624 */ 12625 len_to_go = io->scsiio.kern_data_len; 12626 12627 /* 12628 * Clear the no datasync flag, we have to use malloced 12629 * buffers. 12630 */ 12631 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12632 12633 /* 12634 * The difficult thing here is that the size of the various 12635 * S/G segments may be different than the size from the 12636 * remote controller. That'll make it harder when DMAing 12637 * the data back to the other side. 12638 */ 12639 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12640 sizeof(io->io_hdr.remote_sglist[0])) && 12641 (len_to_go > 0); i++) { 12642 local_sglist[i].len = ctl_min(len_to_go, 131072); 12643 CTL_SIZE_8B(local_dma_sglist[i].len, 12644 local_sglist[i].len); 12645 local_sglist[i].addr = 12646 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12647 12648 local_dma_sglist[i].addr = local_sglist[i].addr; 12649 12650 if (local_sglist[i].addr == NULL) { 12651 int j; 12652 12653 printf("malloc failed for %zd bytes!", 12654 local_dma_sglist[i].len); 12655 for (j = 0; j < i; j++) { 12656 free(local_sglist[j].addr, M_CTL); 12657 } 12658 ctl_set_internal_failure(&io->scsiio, 12659 /*sks_valid*/ 1, 12660 /*retry_count*/ 4857); 12661 retval = 1; 12662 goto bailout_error; 12663 12664 } 12665 /* XXX KDM do we need a sync here? */ 12666 12667 len_to_go -= local_sglist[i].len; 12668 } 12669 /* 12670 * Reset the number of S/G entries accordingly. The 12671 * original number of S/G entries is available in 12672 * rem_sg_entries. 12673 */ 12674 io->scsiio.kern_sg_entries = i; 12675 12676#if 0 12677 printf("%s: kern_sg_entries = %d\n", __func__, 12678 io->scsiio.kern_sg_entries); 12679 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12680 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 12681 local_sglist[i].addr, local_sglist[i].len, 12682 local_dma_sglist[i].len); 12683#endif 12684 } 12685 12686 12687 return (retval); 12688 12689bailout_error: 12690 12691 ctl_send_datamove_done(io, /*have_lock*/ 0); 12692 12693 return (retval); 12694} 12695 12696static int 12697ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12698 ctl_ha_dt_cb callback) 12699{ 12700 struct ctl_ha_dt_req *rq; 12701 struct ctl_sg_entry *remote_sglist, *local_sglist; 12702 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 12703 uint32_t local_used, remote_used, total_used; 12704 int retval; 12705 int i, j; 12706 12707 retval = 0; 12708 12709 rq = ctl_dt_req_alloc(); 12710 12711 /* 12712 * If we failed to allocate the request, and if the DMA didn't fail 12713 * anyway, set busy status. This is just a resource allocation 12714 * failure. 12715 */ 12716 if ((rq == NULL) 12717 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 12718 ctl_set_busy(&io->scsiio); 12719 12720 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 12721 12722 if (rq != NULL) 12723 ctl_dt_req_free(rq); 12724 12725 /* 12726 * The data move failed. We need to return status back 12727 * to the other controller. No point in trying to DMA 12728 * data to the remote controller. 12729 */ 12730 12731 ctl_send_datamove_done(io, /*have_lock*/ 0); 12732 12733 retval = 1; 12734 12735 goto bailout; 12736 } 12737 12738 local_sglist = io->io_hdr.local_sglist; 12739 local_dma_sglist = io->io_hdr.local_dma_sglist; 12740 remote_sglist = io->io_hdr.remote_sglist; 12741 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12742 local_used = 0; 12743 remote_used = 0; 12744 total_used = 0; 12745 12746 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 12747 rq->ret = CTL_HA_STATUS_SUCCESS; 12748 rq->context = io; 12749 callback(rq); 12750 goto bailout; 12751 } 12752 12753 /* 12754 * Pull/push the data over the wire from/to the other controller. 12755 * This takes into account the possibility that the local and 12756 * remote sglists may not be identical in terms of the size of 12757 * the elements and the number of elements. 12758 * 12759 * One fundamental assumption here is that the length allocated for 12760 * both the local and remote sglists is identical. Otherwise, we've 12761 * essentially got a coding error of some sort. 12762 */ 12763 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12764 int isc_ret; 12765 uint32_t cur_len, dma_length; 12766 uint8_t *tmp_ptr; 12767 12768 rq->id = CTL_HA_DATA_CTL; 12769 rq->command = command; 12770 rq->context = io; 12771 12772 /* 12773 * Both pointers should be aligned. But it is possible 12774 * that the allocation length is not. They should both 12775 * also have enough slack left over at the end, though, 12776 * to round up to the next 8 byte boundary. 12777 */ 12778 cur_len = ctl_min(local_sglist[i].len - local_used, 12779 remote_sglist[j].len - remote_used); 12780 12781 /* 12782 * In this case, we have a size issue and need to decrease 12783 * the size, except in the case where we actually have less 12784 * than 8 bytes left. In that case, we need to increase 12785 * the DMA length to get the last bit. 12786 */ 12787 if ((cur_len & 0x7) != 0) { 12788 if (cur_len > 0x7) { 12789 cur_len = cur_len - (cur_len & 0x7); 12790 dma_length = cur_len; 12791 } else { 12792 CTL_SIZE_8B(dma_length, cur_len); 12793 } 12794 12795 } else 12796 dma_length = cur_len; 12797 12798 /* 12799 * If we had to allocate memory for this I/O, instead of using 12800 * the non-cached mirror memory, we'll need to flush the cache 12801 * before trying to DMA to the other controller. 12802 * 12803 * We could end up doing this multiple times for the same 12804 * segment if we have a larger local segment than remote 12805 * segment. That shouldn't be an issue. 12806 */ 12807 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12808 /* 12809 * XXX KDM use bus_dmamap_sync() here. 12810 */ 12811 } 12812 12813 rq->size = dma_length; 12814 12815 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12816 tmp_ptr += local_used; 12817 12818 /* Use physical addresses when talking to ISC hardware */ 12819 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12820 /* XXX KDM use busdma */ 12821#if 0 12822 rq->local = vtophys(tmp_ptr); 12823#endif 12824 } else 12825 rq->local = tmp_ptr; 12826 12827 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12828 tmp_ptr += remote_used; 12829 rq->remote = tmp_ptr; 12830 12831 rq->callback = NULL; 12832 12833 local_used += cur_len; 12834 if (local_used >= local_sglist[i].len) { 12835 i++; 12836 local_used = 0; 12837 } 12838 12839 remote_used += cur_len; 12840 if (remote_used >= remote_sglist[j].len) { 12841 j++; 12842 remote_used = 0; 12843 } 12844 total_used += cur_len; 12845 12846 if (total_used >= io->scsiio.kern_data_len) 12847 rq->callback = callback; 12848 12849 if ((rq->size & 0x7) != 0) { 12850 printf("%s: warning: size %d is not on 8b boundary\n", 12851 __func__, rq->size); 12852 } 12853 if (((uintptr_t)rq->local & 0x7) != 0) { 12854 printf("%s: warning: local %p not on 8b boundary\n", 12855 __func__, rq->local); 12856 } 12857 if (((uintptr_t)rq->remote & 0x7) != 0) { 12858 printf("%s: warning: remote %p not on 8b boundary\n", 12859 __func__, rq->local); 12860 } 12861#if 0 12862 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12863 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12864 rq->local, rq->remote, rq->size); 12865#endif 12866 12867 isc_ret = ctl_dt_single(rq); 12868 if (isc_ret == CTL_HA_STATUS_WAIT) 12869 continue; 12870 12871 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 12872 rq->ret = CTL_HA_STATUS_SUCCESS; 12873 } else { 12874 rq->ret = isc_ret; 12875 } 12876 callback(rq); 12877 goto bailout; 12878 } 12879 12880bailout: 12881 return (retval); 12882 12883} 12884 12885static void 12886ctl_datamove_remote_read(union ctl_io *io) 12887{ 12888 int retval; 12889 int i; 12890 12891 /* 12892 * This will send an error to the other controller in the case of a 12893 * failure. 12894 */ 12895 retval = ctl_datamove_remote_sgl_setup(io); 12896 if (retval != 0) 12897 return; 12898 12899 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12900 ctl_datamove_remote_read_cb); 12901 if ((retval != 0) 12902 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 12903 /* 12904 * Make sure we free memory if there was an error.. The 12905 * ctl_datamove_remote_xfer() function will send the 12906 * datamove done message, or call the callback with an 12907 * error if there is a problem. 12908 */ 12909 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12910 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12911 } 12912 12913 return; 12914} 12915 12916/* 12917 * Process a datamove request from the other controller. This is used for 12918 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12919 * first. Once that is complete, the data gets DMAed into the remote 12920 * controller's memory. For reads, we DMA from the remote controller's 12921 * memory into our memory first, and then move it out to the FETD. 12922 */ 12923static void 12924ctl_datamove_remote(union ctl_io *io) 12925{ 12926 struct ctl_softc *softc; 12927 12928 softc = control_softc; 12929 12930 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 12931 12932 /* 12933 * Note that we look for an aborted I/O here, but don't do some of 12934 * the other checks that ctl_datamove() normally does. We don't 12935 * need to run the task queue, because this I/O is on the ISC 12936 * queue, which is executed by the work thread after the task queue. 12937 * We don't need to run the datamove delay code, since that should 12938 * have been done if need be on the other controller. 12939 */ 12940 mtx_lock(&softc->ctl_lock); 12941 12942 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12943 12944 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 12945 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 12946 io->io_hdr.nexus.targ_port, 12947 io->io_hdr.nexus.targ_target.id, 12948 io->io_hdr.nexus.targ_lun); 12949 io->io_hdr.status = CTL_CMD_ABORTED; 12950 io->io_hdr.port_status = 31338; 12951 12952 mtx_unlock(&softc->ctl_lock); 12953 12954 ctl_send_datamove_done(io, /*have_lock*/ 0); 12955 12956 return; 12957 } 12958 12959 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 12960 mtx_unlock(&softc->ctl_lock); 12961 ctl_datamove_remote_write(io); 12962 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 12963 mtx_unlock(&softc->ctl_lock); 12964 ctl_datamove_remote_read(io); 12965 } else { 12966 union ctl_ha_msg msg; 12967 struct scsi_sense_data *sense; 12968 uint8_t sks[3]; 12969 int retry_count; 12970 12971 memset(&msg, 0, sizeof(msg)); 12972 12973 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 12974 msg.hdr.status = CTL_SCSI_ERROR; 12975 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 12976 12977 retry_count = 4243; 12978 12979 sense = &msg.scsi.sense_data; 12980 sks[0] = SSD_SCS_VALID; 12981 sks[1] = (retry_count >> 8) & 0xff; 12982 sks[2] = retry_count & 0xff; 12983 12984 /* "Internal target failure" */ 12985 scsi_set_sense_data(sense, 12986 /*sense_format*/ SSD_TYPE_NONE, 12987 /*current_error*/ 1, 12988 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 12989 /*asc*/ 0x44, 12990 /*ascq*/ 0x00, 12991 /*type*/ SSD_ELEM_SKS, 12992 /*size*/ sizeof(sks), 12993 /*data*/ sks, 12994 SSD_ELEM_NONE); 12995 12996 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12997 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12998 ctl_failover_io(io, /*have_lock*/ 1); 12999 mtx_unlock(&softc->ctl_lock); 13000 return; 13001 } 13002 13003 mtx_unlock(&softc->ctl_lock); 13004 13005 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13006 CTL_HA_STATUS_SUCCESS) { 13007 /* XXX KDM what to do if this fails? */ 13008 } 13009 return; 13010 } 13011 13012} 13013 13014static int 13015ctl_process_done(union ctl_io *io, int have_lock) 13016{ 13017 struct ctl_lun *lun; 13018 struct ctl_softc *ctl_softc; 13019 void (*fe_done)(union ctl_io *io); 13020 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13021 13022 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13023 13024 fe_done = 13025 control_softc->ctl_ports[targ_port]->fe_done; 13026 13027#ifdef CTL_TIME_IO 13028 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13029 char str[256]; 13030 char path_str[64]; 13031 struct sbuf sb; 13032 13033 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13034 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13035 13036 sbuf_cat(&sb, path_str); 13037 switch (io->io_hdr.io_type) { 13038 case CTL_IO_SCSI: 13039 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13040 sbuf_printf(&sb, "\n"); 13041 sbuf_cat(&sb, path_str); 13042 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13043 io->scsiio.tag_num, io->scsiio.tag_type); 13044 break; 13045 case CTL_IO_TASK: 13046 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13047 "Tag Type: %d\n", io->taskio.task_action, 13048 io->taskio.tag_num, io->taskio.tag_type); 13049 break; 13050 default: 13051 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13052 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13053 break; 13054 } 13055 sbuf_cat(&sb, path_str); 13056 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13057 (intmax_t)time_uptime - io->io_hdr.start_time); 13058 sbuf_finish(&sb); 13059 printf("%s", sbuf_data(&sb)); 13060 } 13061#endif /* CTL_TIME_IO */ 13062 13063 switch (io->io_hdr.io_type) { 13064 case CTL_IO_SCSI: 13065 break; 13066 case CTL_IO_TASK: 13067 if (bootverbose || verbose > 0) 13068 ctl_io_error_print(io, NULL); 13069 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13070 ctl_free_io(io); 13071 else 13072 fe_done(io); 13073 return (CTL_RETVAL_COMPLETE); 13074 break; 13075 default: 13076 printf("ctl_process_done: invalid io type %d\n", 13077 io->io_hdr.io_type); 13078 panic("ctl_process_done: invalid io type %d\n", 13079 io->io_hdr.io_type); 13080 break; /* NOTREACHED */ 13081 } 13082 13083 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13084 if (lun == NULL) { 13085 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13086 io->io_hdr.nexus.targ_lun)); 13087 fe_done(io); 13088 goto bailout; 13089 } 13090 ctl_softc = lun->ctl_softc; 13091 13092 /* 13093 * Remove this from the OOA queue. 13094 */ 13095 if (have_lock == 0) 13096 mtx_lock(&ctl_softc->ctl_lock); 13097 13098 /* 13099 * Check to see if we have any errors to inject here. We only 13100 * inject errors for commands that don't already have errors set. 13101 */ 13102 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13103 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13104 ctl_inject_error(lun, io); 13105 13106 /* 13107 * XXX KDM how do we treat commands that aren't completed 13108 * successfully? 13109 * 13110 * XXX KDM should we also track I/O latency? 13111 */ 13112 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 13113 uint32_t blocksize; 13114#ifdef CTL_TIME_IO 13115 struct bintime cur_bt; 13116#endif 13117 13118 if ((lun->be_lun != NULL) 13119 && (lun->be_lun->blocksize != 0)) 13120 blocksize = lun->be_lun->blocksize; 13121 else 13122 blocksize = 512; 13123 13124 switch (io->io_hdr.io_type) { 13125 case CTL_IO_SCSI: { 13126 int isread; 13127 struct ctl_lba_len_flags *lbalen; 13128 13129 isread = 0; 13130 switch (io->scsiio.cdb[0]) { 13131 case READ_6: 13132 case READ_10: 13133 case READ_12: 13134 case READ_16: 13135 isread = 1; 13136 /* FALLTHROUGH */ 13137 case WRITE_6: 13138 case WRITE_10: 13139 case WRITE_12: 13140 case WRITE_16: 13141 case WRITE_VERIFY_10: 13142 case WRITE_VERIFY_12: 13143 case WRITE_VERIFY_16: 13144 lbalen = (struct ctl_lba_len_flags *) 13145 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 13146 13147 if (isread) { 13148 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] += 13149 lbalen->len * blocksize; 13150 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++; 13151 13152#ifdef CTL_TIME_IO 13153 bintime_add( 13154 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ], 13155 &io->io_hdr.dma_bt); 13156 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] += 13157 io->io_hdr.num_dmas; 13158 getbintime(&cur_bt); 13159 bintime_sub(&cur_bt, 13160 &io->io_hdr.start_bt); 13161 13162 bintime_add( 13163 &lun->stats.ports[targ_port].time[CTL_STATS_READ], 13164 &cur_bt); 13165 13166#if 0 13167 cs_prof_gettime(&cur_ticks); 13168 lun->stats.time[CTL_STATS_READ] += 13169 cur_ticks - 13170 io->io_hdr.start_ticks; 13171#endif 13172#if 0 13173 lun->stats.time[CTL_STATS_READ] += 13174 jiffies - io->io_hdr.start_time; 13175#endif 13176#endif /* CTL_TIME_IO */ 13177 } else { 13178 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] += 13179 lbalen->len * blocksize; 13180 lun->stats.ports[targ_port].operations[ 13181 CTL_STATS_WRITE]++; 13182 13183#ifdef CTL_TIME_IO 13184 bintime_add( 13185 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE], 13186 &io->io_hdr.dma_bt); 13187 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] += 13188 io->io_hdr.num_dmas; 13189 getbintime(&cur_bt); 13190 bintime_sub(&cur_bt, 13191 &io->io_hdr.start_bt); 13192 13193 bintime_add( 13194 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE], 13195 &cur_bt); 13196#if 0 13197 cs_prof_gettime(&cur_ticks); 13198 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] += 13199 cur_ticks - 13200 io->io_hdr.start_ticks; 13201 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] += 13202 jiffies - io->io_hdr.start_time; 13203#endif 13204#endif /* CTL_TIME_IO */ 13205 } 13206 break; 13207 default: 13208 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++; 13209 13210#ifdef CTL_TIME_IO 13211 bintime_add( 13212 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO], 13213 &io->io_hdr.dma_bt); 13214 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] += 13215 io->io_hdr.num_dmas; 13216 getbintime(&cur_bt); 13217 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13218 13219 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO], 13220 &cur_bt); 13221 13222#if 0 13223 cs_prof_gettime(&cur_ticks); 13224 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] += 13225 cur_ticks - 13226 io->io_hdr.start_ticks; 13227 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] += 13228 jiffies - io->io_hdr.start_time; 13229#endif 13230#endif /* CTL_TIME_IO */ 13231 break; 13232 } 13233 break; 13234 } 13235 default: 13236 break; 13237 } 13238 } 13239 13240 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13241 13242 /* 13243 * Run through the blocked queue on this LUN and see if anything 13244 * has become unblocked, now that this transaction is done. 13245 */ 13246 ctl_check_blocked(lun); 13247 13248 /* 13249 * If the LUN has been invalidated, free it if there is nothing 13250 * left on its OOA queue. 13251 */ 13252 if ((lun->flags & CTL_LUN_INVALID) 13253 && (TAILQ_FIRST(&lun->ooa_queue) == NULL)) 13254 ctl_free_lun(lun); 13255 13256 /* 13257 * If this command has been aborted, make sure we set the status 13258 * properly. The FETD is responsible for freeing the I/O and doing 13259 * whatever it needs to do to clean up its state. 13260 */ 13261 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13262 io->io_hdr.status = CTL_CMD_ABORTED; 13263 13264 /* 13265 * We print out status for every task management command. For SCSI 13266 * commands, we filter out any unit attention errors; they happen 13267 * on every boot, and would clutter up the log. Note: task 13268 * management commands aren't printed here, they are printed above, 13269 * since they should never even make it down here. 13270 */ 13271 switch (io->io_hdr.io_type) { 13272 case CTL_IO_SCSI: { 13273 int error_code, sense_key, asc, ascq; 13274 13275 sense_key = 0; 13276 13277 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13278 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13279 /* 13280 * Since this is just for printing, no need to 13281 * show errors here. 13282 */ 13283 scsi_extract_sense_len(&io->scsiio.sense_data, 13284 io->scsiio.sense_len, 13285 &error_code, 13286 &sense_key, 13287 &asc, 13288 &ascq, 13289 /*show_errors*/ 0); 13290 } 13291 13292 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13293 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13294 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13295 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13296 13297 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13298 ctl_softc->skipped_prints++; 13299 if (have_lock == 0) 13300 mtx_unlock(&ctl_softc->ctl_lock); 13301 } else { 13302 uint32_t skipped_prints; 13303 13304 skipped_prints = ctl_softc->skipped_prints; 13305 13306 ctl_softc->skipped_prints = 0; 13307 ctl_softc->last_print_jiffies = time_uptime; 13308 13309 if (have_lock == 0) 13310 mtx_unlock(&ctl_softc->ctl_lock); 13311 if (skipped_prints > 0) { 13312#ifdef NEEDTOPORT 13313 csevent_log(CSC_CTL | CSC_SHELF_SW | 13314 CTL_ERROR_REPORT, 13315 csevent_LogType_Trace, 13316 csevent_Severity_Information, 13317 csevent_AlertLevel_Green, 13318 csevent_FRU_Firmware, 13319 csevent_FRU_Unknown, 13320 "High CTL error volume, %d prints " 13321 "skipped", skipped_prints); 13322#endif 13323 } 13324 if (bootverbose || verbose > 0) 13325 ctl_io_error_print(io, NULL); 13326 } 13327 } else { 13328 if (have_lock == 0) 13329 mtx_unlock(&ctl_softc->ctl_lock); 13330 } 13331 break; 13332 } 13333 case CTL_IO_TASK: 13334 if (have_lock == 0) 13335 mtx_unlock(&ctl_softc->ctl_lock); 13336 if (bootverbose || verbose > 0) 13337 ctl_io_error_print(io, NULL); 13338 break; 13339 default: 13340 if (have_lock == 0) 13341 mtx_unlock(&ctl_softc->ctl_lock); 13342 break; 13343 } 13344 13345 /* 13346 * Tell the FETD or the other shelf controller we're done with this 13347 * command. Note that only SCSI commands get to this point. Task 13348 * management commands are completed above. 13349 * 13350 * We only send status to the other controller if we're in XFER 13351 * mode. In SER_ONLY mode, the I/O is done on the controller that 13352 * received the I/O (from CTL's perspective), and so the status is 13353 * generated there. 13354 * 13355 * XXX KDM if we hold the lock here, we could cause a deadlock 13356 * if the frontend comes back in in this context to queue 13357 * something. 13358 */ 13359 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13360 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13361 union ctl_ha_msg msg; 13362 13363 memset(&msg, 0, sizeof(msg)); 13364 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13365 msg.hdr.original_sc = io->io_hdr.original_sc; 13366 msg.hdr.nexus = io->io_hdr.nexus; 13367 msg.hdr.status = io->io_hdr.status; 13368 msg.scsi.scsi_status = io->scsiio.scsi_status; 13369 msg.scsi.tag_num = io->scsiio.tag_num; 13370 msg.scsi.tag_type = io->scsiio.tag_type; 13371 msg.scsi.sense_len = io->scsiio.sense_len; 13372 msg.scsi.sense_residual = io->scsiio.sense_residual; 13373 msg.scsi.residual = io->scsiio.residual; 13374 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13375 sizeof(io->scsiio.sense_data)); 13376 /* 13377 * We copy this whether or not this is an I/O-related 13378 * command. Otherwise, we'd have to go and check to see 13379 * whether it's a read/write command, and it really isn't 13380 * worth it. 13381 */ 13382 memcpy(&msg.scsi.lbalen, 13383 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13384 sizeof(msg.scsi.lbalen)); 13385 13386 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13387 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13388 /* XXX do something here */ 13389 } 13390 13391 ctl_free_io(io); 13392 } else 13393 fe_done(io); 13394 13395bailout: 13396 13397 return (CTL_RETVAL_COMPLETE); 13398} 13399 13400/* 13401 * Front end should call this if it doesn't do autosense. When the request 13402 * sense comes back in from the initiator, we'll dequeue this and send it. 13403 */ 13404int 13405ctl_queue_sense(union ctl_io *io) 13406{ 13407 struct ctl_lun *lun; 13408 struct ctl_softc *ctl_softc; 13409 uint32_t initidx, targ_lun; 13410 13411 ctl_softc = control_softc; 13412 13413 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13414 13415 /* 13416 * LUN lookup will likely move to the ctl_work_thread() once we 13417 * have our new queueing infrastructure (that doesn't put things on 13418 * a per-LUN queue initially). That is so that we can handle 13419 * things like an INQUIRY to a LUN that we don't have enabled. We 13420 * can't deal with that right now. 13421 */ 13422 mtx_lock(&ctl_softc->ctl_lock); 13423 13424 /* 13425 * If we don't have a LUN for this, just toss the sense 13426 * information. 13427 */ 13428 targ_lun = io->io_hdr.nexus.targ_lun; 13429 if (io->io_hdr.nexus.lun_map_fn != NULL) 13430 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 13431 if ((targ_lun < CTL_MAX_LUNS) 13432 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13433 lun = ctl_softc->ctl_luns[targ_lun]; 13434 else 13435 goto bailout; 13436 13437 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13438 13439 /* 13440 * Already have CA set for this LUN...toss the sense information. 13441 */ 13442 if (ctl_is_set(lun->have_ca, initidx)) 13443 goto bailout; 13444 13445 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data, 13446 ctl_min(sizeof(lun->pending_sense[initidx].sense), 13447 sizeof(io->scsiio.sense_data))); 13448 ctl_set_mask(lun->have_ca, initidx); 13449 13450bailout: 13451 mtx_unlock(&ctl_softc->ctl_lock); 13452 13453 ctl_free_io(io); 13454 13455 return (CTL_RETVAL_COMPLETE); 13456} 13457 13458/* 13459 * Primary command inlet from frontend ports. All SCSI and task I/O 13460 * requests must go through this function. 13461 */ 13462int 13463ctl_queue(union ctl_io *io) 13464{ 13465 struct ctl_softc *ctl_softc; 13466 13467 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13468 13469 ctl_softc = control_softc; 13470 13471#ifdef CTL_TIME_IO 13472 io->io_hdr.start_time = time_uptime; 13473 getbintime(&io->io_hdr.start_bt); 13474#endif /* CTL_TIME_IO */ 13475 13476 mtx_lock(&ctl_softc->ctl_lock); 13477 13478 switch (io->io_hdr.io_type) { 13479 case CTL_IO_SCSI: 13480 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr, 13481 links); 13482 break; 13483 case CTL_IO_TASK: 13484 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links); 13485 /* 13486 * Set the task pending flag. This is necessary to close a 13487 * race condition with the FETD: 13488 * 13489 * - FETD submits a task management command, like an abort. 13490 * - Back end calls fe_datamove() to move the data for the 13491 * aborted command. The FETD can't really accept it, but 13492 * if it did, it would end up transmitting data for a 13493 * command that the initiator told us to abort. 13494 * 13495 * We close the race condition by setting the flag here, 13496 * and checking it in ctl_datamove(), before calling the 13497 * FETD's fe_datamove routine. If we've got a task 13498 * pending, we run the task queue and then check to see 13499 * whether our particular I/O has been aborted. 13500 */ 13501 ctl_softc->flags |= CTL_FLAG_TASK_PENDING; 13502 break; 13503 default: 13504 mtx_unlock(&ctl_softc->ctl_lock); 13505 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13506 return (EINVAL); 13507 break; /* NOTREACHED */ 13508 } 13509 mtx_unlock(&ctl_softc->ctl_lock); 13510 13511 ctl_wakeup_thread(); 13512 13513 return (CTL_RETVAL_COMPLETE); 13514} 13515 13516#ifdef CTL_IO_DELAY 13517static void 13518ctl_done_timer_wakeup(void *arg) 13519{ 13520 union ctl_io *io; 13521 13522 io = (union ctl_io *)arg; 13523 ctl_done_lock(io, /*have_lock*/ 0); 13524} 13525#endif /* CTL_IO_DELAY */ 13526 13527void 13528ctl_done_lock(union ctl_io *io, int have_lock) 13529{ 13530 struct ctl_softc *ctl_softc; 13531#ifndef CTL_DONE_THREAD 13532 union ctl_io *xio; 13533#endif /* !CTL_DONE_THREAD */ 13534 13535 ctl_softc = control_softc; 13536 13537 if (have_lock == 0) 13538 mtx_lock(&ctl_softc->ctl_lock); 13539 13540 /* 13541 * Enable this to catch duplicate completion issues. 13542 */ 13543#if 0 13544 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13545 printf("%s: type %d msg %d cdb %x iptl: " 13546 "%d:%d:%d:%d tag 0x%04x " 13547 "flag %#x status %x\n", 13548 __func__, 13549 io->io_hdr.io_type, 13550 io->io_hdr.msg_type, 13551 io->scsiio.cdb[0], 13552 io->io_hdr.nexus.initid.id, 13553 io->io_hdr.nexus.targ_port, 13554 io->io_hdr.nexus.targ_target.id, 13555 io->io_hdr.nexus.targ_lun, 13556 (io->io_hdr.io_type == 13557 CTL_IO_TASK) ? 13558 io->taskio.tag_num : 13559 io->scsiio.tag_num, 13560 io->io_hdr.flags, 13561 io->io_hdr.status); 13562 } else 13563 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13564#endif 13565 13566 /* 13567 * This is an internal copy of an I/O, and should not go through 13568 * the normal done processing logic. 13569 */ 13570 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) { 13571 if (have_lock == 0) 13572 mtx_unlock(&ctl_softc->ctl_lock); 13573 return; 13574 } 13575 13576 /* 13577 * We need to send a msg to the serializing shelf to finish the IO 13578 * as well. We don't send a finish message to the other shelf if 13579 * this is a task management command. Task management commands 13580 * aren't serialized in the OOA queue, but rather just executed on 13581 * both shelf controllers for commands that originated on that 13582 * controller. 13583 */ 13584 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13585 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13586 union ctl_ha_msg msg_io; 13587 13588 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13589 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13590 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13591 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13592 } 13593 /* continue on to finish IO */ 13594 } 13595#ifdef CTL_IO_DELAY 13596 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13597 struct ctl_lun *lun; 13598 13599 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13600 13601 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13602 } else { 13603 struct ctl_lun *lun; 13604 13605 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13606 13607 if ((lun != NULL) 13608 && (lun->delay_info.done_delay > 0)) { 13609 struct callout *callout; 13610 13611 callout = (struct callout *)&io->io_hdr.timer_bytes; 13612 callout_init(callout, /*mpsafe*/ 1); 13613 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13614 callout_reset(callout, 13615 lun->delay_info.done_delay * hz, 13616 ctl_done_timer_wakeup, io); 13617 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13618 lun->delay_info.done_delay = 0; 13619 if (have_lock == 0) 13620 mtx_unlock(&ctl_softc->ctl_lock); 13621 return; 13622 } 13623 } 13624#endif /* CTL_IO_DELAY */ 13625 13626 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links); 13627 13628#ifdef CTL_DONE_THREAD 13629 if (have_lock == 0) 13630 mtx_unlock(&ctl_softc->ctl_lock); 13631 13632 ctl_wakeup_thread(); 13633#else /* CTL_DONE_THREAD */ 13634 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue); 13635 xio != NULL; 13636 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) { 13637 13638 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links); 13639 13640 ctl_process_done(xio, /*have_lock*/ 1); 13641 } 13642 if (have_lock == 0) 13643 mtx_unlock(&ctl_softc->ctl_lock); 13644#endif /* CTL_DONE_THREAD */ 13645} 13646 13647void 13648ctl_done(union ctl_io *io) 13649{ 13650 ctl_done_lock(io, /*have_lock*/ 0); 13651} 13652 13653int 13654ctl_isc(struct ctl_scsiio *ctsio) 13655{ 13656 struct ctl_lun *lun; 13657 int retval; 13658 13659 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13660 13661 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13662 13663 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13664 13665 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13666 13667 return (retval); 13668} 13669 13670 13671static void 13672ctl_work_thread(void *arg) 13673{ 13674 struct ctl_softc *softc; 13675 union ctl_io *io; 13676 struct ctl_be_lun *be_lun; 13677 int retval; 13678 13679 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13680 13681 softc = (struct ctl_softc *)arg; 13682 if (softc == NULL) 13683 return; 13684 13685 mtx_lock(&softc->ctl_lock); 13686 for (;;) { 13687 retval = 0; 13688 13689 /* 13690 * We handle the queues in this order: 13691 * - task management 13692 * - ISC 13693 * - done queue (to free up resources, unblock other commands) 13694 * - RtR queue 13695 * - incoming queue 13696 * 13697 * If those queues are empty, we break out of the loop and 13698 * go to sleep. 13699 */ 13700 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue); 13701 if (io != NULL) { 13702 ctl_run_task_queue(softc); 13703 continue; 13704 } 13705 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue); 13706 if (io != NULL) { 13707 STAILQ_REMOVE_HEAD(&softc->isc_queue, links); 13708 ctl_handle_isc(io); 13709 continue; 13710 } 13711 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue); 13712 if (io != NULL) { 13713 STAILQ_REMOVE_HEAD(&softc->done_queue, links); 13714 /* clear any blocked commands, call fe_done */ 13715 mtx_unlock(&softc->ctl_lock); 13716 /* 13717 * XXX KDM 13718 * Call this without a lock for now. This will 13719 * depend on whether there is any way the FETD can 13720 * sleep or deadlock if called with the CTL lock 13721 * held. 13722 */ 13723 retval = ctl_process_done(io, /*have_lock*/ 0); 13724 mtx_lock(&softc->ctl_lock); 13725 continue; 13726 } 13727 if (!ctl_pause_rtr) { 13728 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 13729 if (io != NULL) { 13730 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links); 13731 mtx_unlock(&softc->ctl_lock); 13732 retval = ctl_scsiio(&io->scsiio); 13733 if (retval != CTL_RETVAL_COMPLETE) 13734 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13735 mtx_lock(&softc->ctl_lock); 13736 continue; 13737 } 13738 } 13739 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue); 13740 if (io != NULL) { 13741 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links); 13742 mtx_unlock(&softc->ctl_lock); 13743 ctl_scsiio_precheck(softc, &io->scsiio); 13744 mtx_lock(&softc->ctl_lock); 13745 continue; 13746 } 13747 /* 13748 * We might want to move this to a separate thread, so that 13749 * configuration requests (in this case LUN creations) 13750 * won't impact the I/O path. 13751 */ 13752 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13753 if (be_lun != NULL) { 13754 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13755 mtx_unlock(&softc->ctl_lock); 13756 ctl_create_lun(be_lun); 13757 mtx_lock(&softc->ctl_lock); 13758 continue; 13759 } 13760 13761 /* XXX KDM use the PDROP flag?? */ 13762 /* Sleep until we have something to do. */ 13763 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "-", 0); 13764 13765 /* Back to the top of the loop to see what woke us up. */ 13766 continue; 13767 } 13768} 13769 13770void 13771ctl_wakeup_thread() 13772{ 13773 struct ctl_softc *softc; 13774 13775 softc = control_softc; 13776 13777 wakeup_one(softc); 13778} 13779 13780/* Initialization and failover */ 13781 13782void 13783ctl_init_isc_msg(void) 13784{ 13785 printf("CTL: Still calling this thing\n"); 13786} 13787 13788/* 13789 * Init component 13790 * Initializes component into configuration defined by bootMode 13791 * (see hasc-sv.c) 13792 * returns hasc_Status: 13793 * OK 13794 * ERROR - fatal error 13795 */ 13796static ctl_ha_comp_status 13797ctl_isc_init(struct ctl_ha_component *c) 13798{ 13799 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13800 13801 c->status = ret; 13802 return ret; 13803} 13804 13805/* Start component 13806 * Starts component in state requested. If component starts successfully, 13807 * it must set its own state to the requestrd state 13808 * When requested state is HASC_STATE_HA, the component may refine it 13809 * by adding _SLAVE or _MASTER flags. 13810 * Currently allowed state transitions are: 13811 * UNKNOWN->HA - initial startup 13812 * UNKNOWN->SINGLE - initial startup when no parter detected 13813 * HA->SINGLE - failover 13814 * returns ctl_ha_comp_status: 13815 * OK - component successfully started in requested state 13816 * FAILED - could not start the requested state, failover may 13817 * be possible 13818 * ERROR - fatal error detected, no future startup possible 13819 */ 13820static ctl_ha_comp_status 13821ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 13822{ 13823 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13824 13825 printf("%s: go\n", __func__); 13826 13827 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 13828 if (c->state == CTL_HA_STATE_UNKNOWN ) { 13829 ctl_is_single = 0; 13830 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 13831 != CTL_HA_STATUS_SUCCESS) { 13832 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 13833 ret = CTL_HA_COMP_STATUS_ERROR; 13834 } 13835 } else if (CTL_HA_STATE_IS_HA(c->state) 13836 && CTL_HA_STATE_IS_SINGLE(state)){ 13837 // HA->SINGLE transition 13838 ctl_failover(); 13839 ctl_is_single = 1; 13840 } else { 13841 printf("ctl_isc_start:Invalid state transition %X->%X\n", 13842 c->state, state); 13843 ret = CTL_HA_COMP_STATUS_ERROR; 13844 } 13845 if (CTL_HA_STATE_IS_SINGLE(state)) 13846 ctl_is_single = 1; 13847 13848 c->state = state; 13849 c->status = ret; 13850 return ret; 13851} 13852 13853/* 13854 * Quiesce component 13855 * The component must clear any error conditions (set status to OK) and 13856 * prepare itself to another Start call 13857 * returns ctl_ha_comp_status: 13858 * OK 13859 * ERROR 13860 */ 13861static ctl_ha_comp_status 13862ctl_isc_quiesce(struct ctl_ha_component *c) 13863{ 13864 int ret = CTL_HA_COMP_STATUS_OK; 13865 13866 ctl_pause_rtr = 1; 13867 c->status = ret; 13868 return ret; 13869} 13870 13871struct ctl_ha_component ctl_ha_component_ctlisc = 13872{ 13873 .name = "CTL ISC", 13874 .state = CTL_HA_STATE_UNKNOWN, 13875 .init = ctl_isc_init, 13876 .start = ctl_isc_start, 13877 .quiesce = ctl_isc_quiesce 13878}; 13879 13880/* 13881 * vim: ts=8 13882 */ 13883