ctl.c revision 288768
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id$ 36 */ 37/* 38 * CAM Target Layer, a SCSI device emulation subsystem. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42 43#define _CTL_C 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 288768 2015-10-05 10:35:36Z mav $"); 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/ctype.h> 51#include <sys/kernel.h> 52#include <sys/types.h> 53#include <sys/kthread.h> 54#include <sys/bio.h> 55#include <sys/fcntl.h> 56#include <sys/lock.h> 57#include <sys/module.h> 58#include <sys/mutex.h> 59#include <sys/condvar.h> 60#include <sys/malloc.h> 61#include <sys/conf.h> 62#include <sys/ioccom.h> 63#include <sys/queue.h> 64#include <sys/sbuf.h> 65#include <sys/smp.h> 66#include <sys/endian.h> 67#include <sys/sysctl.h> 68#include <vm/uma.h> 69 70#include <cam/cam.h> 71#include <cam/scsi/scsi_all.h> 72#include <cam/scsi/scsi_da.h> 73#include <cam/ctl/ctl_io.h> 74#include <cam/ctl/ctl.h> 75#include <cam/ctl/ctl_frontend.h> 76#include <cam/ctl/ctl_util.h> 77#include <cam/ctl/ctl_backend.h> 78#include <cam/ctl/ctl_ioctl.h> 79#include <cam/ctl/ctl_ha.h> 80#include <cam/ctl/ctl_private.h> 81#include <cam/ctl/ctl_debug.h> 82#include <cam/ctl/ctl_scsi_all.h> 83#include <cam/ctl/ctl_error.h> 84 85struct ctl_softc *control_softc = NULL; 86 87/* 88 * Template mode pages. 89 */ 90 91/* 92 * Note that these are default values only. The actual values will be 93 * filled in when the user does a mode sense. 94 */ 95const static struct copan_debugconf_subpage debugconf_page_default = { 96 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 97 DBGCNF_SUBPAGE_CODE, /* subpage */ 98 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 99 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 100 DBGCNF_VERSION, /* page_version */ 101 {CTL_TIME_IO_DEFAULT_SECS>>8, 102 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 103}; 104 105const static struct copan_debugconf_subpage debugconf_page_changeable = { 106 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 107 DBGCNF_SUBPAGE_CODE, /* subpage */ 108 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 109 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 110 0, /* page_version */ 111 {0xff,0xff}, /* ctl_time_io_secs */ 112}; 113 114const static struct scsi_da_rw_recovery_page rw_er_page_default = { 115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 117 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 118 /*read_retry_count*/0, 119 /*correction_span*/0, 120 /*head_offset_count*/0, 121 /*data_strobe_offset_cnt*/0, 122 /*byte8*/SMS_RWER_LBPERE, 123 /*write_retry_count*/0, 124 /*reserved2*/0, 125 /*recovery_time_limit*/{0, 0}, 126}; 127 128const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 129 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 130 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 131 /*byte3*/0, 132 /*read_retry_count*/0, 133 /*correction_span*/0, 134 /*head_offset_count*/0, 135 /*data_strobe_offset_cnt*/0, 136 /*byte8*/0, 137 /*write_retry_count*/0, 138 /*reserved2*/0, 139 /*recovery_time_limit*/{0, 0}, 140}; 141 142const static struct scsi_format_page format_page_default = { 143 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 144 /*page_length*/sizeof(struct scsi_format_page) - 2, 145 /*tracks_per_zone*/ {0, 0}, 146 /*alt_sectors_per_zone*/ {0, 0}, 147 /*alt_tracks_per_zone*/ {0, 0}, 148 /*alt_tracks_per_lun*/ {0, 0}, 149 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 150 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 151 /*bytes_per_sector*/ {0, 0}, 152 /*interleave*/ {0, 0}, 153 /*track_skew*/ {0, 0}, 154 /*cylinder_skew*/ {0, 0}, 155 /*flags*/ SFP_HSEC, 156 /*reserved*/ {0, 0, 0} 157}; 158 159const static struct scsi_format_page format_page_changeable = { 160 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 161 /*page_length*/sizeof(struct scsi_format_page) - 2, 162 /*tracks_per_zone*/ {0, 0}, 163 /*alt_sectors_per_zone*/ {0, 0}, 164 /*alt_tracks_per_zone*/ {0, 0}, 165 /*alt_tracks_per_lun*/ {0, 0}, 166 /*sectors_per_track*/ {0, 0}, 167 /*bytes_per_sector*/ {0, 0}, 168 /*interleave*/ {0, 0}, 169 /*track_skew*/ {0, 0}, 170 /*cylinder_skew*/ {0, 0}, 171 /*flags*/ 0, 172 /*reserved*/ {0, 0, 0} 173}; 174 175const static struct scsi_rigid_disk_page rigid_disk_page_default = { 176 /*page_code*/SMS_RIGID_DISK_PAGE, 177 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 178 /*cylinders*/ {0, 0, 0}, 179 /*heads*/ CTL_DEFAULT_HEADS, 180 /*start_write_precomp*/ {0, 0, 0}, 181 /*start_reduced_current*/ {0, 0, 0}, 182 /*step_rate*/ {0, 0}, 183 /*landing_zone_cylinder*/ {0, 0, 0}, 184 /*rpl*/ SRDP_RPL_DISABLED, 185 /*rotational_offset*/ 0, 186 /*reserved1*/ 0, 187 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 188 CTL_DEFAULT_ROTATION_RATE & 0xff}, 189 /*reserved2*/ {0, 0} 190}; 191 192const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 193 /*page_code*/SMS_RIGID_DISK_PAGE, 194 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 195 /*cylinders*/ {0, 0, 0}, 196 /*heads*/ 0, 197 /*start_write_precomp*/ {0, 0, 0}, 198 /*start_reduced_current*/ {0, 0, 0}, 199 /*step_rate*/ {0, 0}, 200 /*landing_zone_cylinder*/ {0, 0, 0}, 201 /*rpl*/ 0, 202 /*rotational_offset*/ 0, 203 /*reserved1*/ 0, 204 /*rotation_rate*/ {0, 0}, 205 /*reserved2*/ {0, 0} 206}; 207 208const static struct scsi_caching_page caching_page_default = { 209 /*page_code*/SMS_CACHING_PAGE, 210 /*page_length*/sizeof(struct scsi_caching_page) - 2, 211 /*flags1*/ SCP_DISC | SCP_WCE, 212 /*ret_priority*/ 0, 213 /*disable_pf_transfer_len*/ {0xff, 0xff}, 214 /*min_prefetch*/ {0, 0}, 215 /*max_prefetch*/ {0xff, 0xff}, 216 /*max_pf_ceiling*/ {0xff, 0xff}, 217 /*flags2*/ 0, 218 /*cache_segments*/ 0, 219 /*cache_seg_size*/ {0, 0}, 220 /*reserved*/ 0, 221 /*non_cache_seg_size*/ {0, 0, 0} 222}; 223 224const static struct scsi_caching_page caching_page_changeable = { 225 /*page_code*/SMS_CACHING_PAGE, 226 /*page_length*/sizeof(struct scsi_caching_page) - 2, 227 /*flags1*/ SCP_WCE | SCP_RCD, 228 /*ret_priority*/ 0, 229 /*disable_pf_transfer_len*/ {0, 0}, 230 /*min_prefetch*/ {0, 0}, 231 /*max_prefetch*/ {0, 0}, 232 /*max_pf_ceiling*/ {0, 0}, 233 /*flags2*/ 0, 234 /*cache_segments*/ 0, 235 /*cache_seg_size*/ {0, 0}, 236 /*reserved*/ 0, 237 /*non_cache_seg_size*/ {0, 0, 0} 238}; 239 240const static struct scsi_control_page control_page_default = { 241 /*page_code*/SMS_CONTROL_MODE_PAGE, 242 /*page_length*/sizeof(struct scsi_control_page) - 2, 243 /*rlec*/0, 244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 245 /*eca_and_aen*/0, 246 /*flags4*/SCP_TAS, 247 /*aen_holdoff_period*/{0, 0}, 248 /*busy_timeout_period*/{0, 0}, 249 /*extended_selftest_completion_time*/{0, 0} 250}; 251 252const static struct scsi_control_page control_page_changeable = { 253 /*page_code*/SMS_CONTROL_MODE_PAGE, 254 /*page_length*/sizeof(struct scsi_control_page) - 2, 255 /*rlec*/SCP_DSENSE, 256 /*queue_flags*/SCP_QUEUE_ALG_MASK, 257 /*eca_and_aen*/SCP_SWP, 258 /*flags4*/0, 259 /*aen_holdoff_period*/{0, 0}, 260 /*busy_timeout_period*/{0, 0}, 261 /*extended_selftest_completion_time*/{0, 0} 262}; 263 264const static struct scsi_info_exceptions_page ie_page_default = { 265 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 266 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 267 /*info_flags*/SIEP_FLAGS_DEXCPT, 268 /*mrie*/0, 269 /*interval_timer*/{0, 0, 0, 0}, 270 /*report_count*/{0, 0, 0, 0} 271}; 272 273const static struct scsi_info_exceptions_page ie_page_changeable = { 274 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 275 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 276 /*info_flags*/0, 277 /*mrie*/0, 278 /*interval_timer*/{0, 0, 0, 0}, 279 /*report_count*/{0, 0, 0, 0} 280}; 281 282#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 283 284const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 286 /*subpage_code*/0x02, 287 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 288 /*flags*/0, 289 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 290 /*descr*/{}}, 291 {{/*flags*/0, 292 /*resource*/0x01, 293 /*reserved*/{0, 0}, 294 /*count*/{0, 0, 0, 0}}, 295 {/*flags*/0, 296 /*resource*/0x02, 297 /*reserved*/{0, 0}, 298 /*count*/{0, 0, 0, 0}}, 299 {/*flags*/0, 300 /*resource*/0xf1, 301 /*reserved*/{0, 0}, 302 /*count*/{0, 0, 0, 0}}, 303 {/*flags*/0, 304 /*resource*/0xf2, 305 /*reserved*/{0, 0}, 306 /*count*/{0, 0, 0, 0}} 307 } 308}; 309 310const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 311 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 312 /*subpage_code*/0x02, 313 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 314 /*flags*/0, 315 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 316 /*descr*/{}}, 317 {{/*flags*/0, 318 /*resource*/0, 319 /*reserved*/{0, 0}, 320 /*count*/{0, 0, 0, 0}}, 321 {/*flags*/0, 322 /*resource*/0, 323 /*reserved*/{0, 0}, 324 /*count*/{0, 0, 0, 0}}, 325 {/*flags*/0, 326 /*resource*/0, 327 /*reserved*/{0, 0}, 328 /*count*/{0, 0, 0, 0}}, 329 {/*flags*/0, 330 /*resource*/0, 331 /*reserved*/{0, 0}, 332 /*count*/{0, 0, 0, 0}} 333 } 334}; 335 336SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 337static int worker_threads = -1; 338TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 339SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 340 &worker_threads, 1, "Number of worker threads"); 341static int ctl_debug = CTL_DEBUG_NONE; 342TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 343SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 344 &ctl_debug, 0, "Enabled debug flags"); 345 346/* 347 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 348 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 349 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 350 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 351 */ 352#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 353 354static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 355 int param); 356static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 357static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 358static int ctl_init(void); 359void ctl_shutdown(void); 360static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 361static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 362static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 363static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 364 struct ctl_ooa *ooa_hdr, 365 struct ctl_ooa_entry *kern_entries); 366static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 367 struct thread *td); 368static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 369 struct ctl_be_lun *be_lun); 370static int ctl_free_lun(struct ctl_lun *lun); 371static void ctl_create_lun(struct ctl_be_lun *be_lun); 372static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 373 374static int ctl_do_mode_select(union ctl_io *io); 375static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 376 uint64_t res_key, uint64_t sa_res_key, 377 uint8_t type, uint32_t residx, 378 struct ctl_scsiio *ctsio, 379 struct scsi_per_res_out *cdb, 380 struct scsi_per_res_out_parms* param); 381static void ctl_pro_preempt_other(struct ctl_lun *lun, 382 union ctl_ha_msg *msg); 383static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 384static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 385static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 386static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 387static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 388static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 389static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 390 int alloc_len); 391static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 392 int alloc_len); 393static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 394static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 395static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 396static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 397static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 398static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 399 bool seq); 400static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 401static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 402 union ctl_io *pending_io, union ctl_io *ooa_io); 403static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 404 union ctl_io *starting_io); 405static int ctl_check_blocked(struct ctl_lun *lun); 406static int ctl_scsiio_lun_check(struct ctl_lun *lun, 407 const struct ctl_cmd_entry *entry, 408 struct ctl_scsiio *ctsio); 409static void ctl_failover_lun(struct ctl_lun *lun); 410static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 411 struct ctl_scsiio *ctsio); 412static int ctl_scsiio(struct ctl_scsiio *ctsio); 413 414static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 415static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 416 ctl_ua_type ua_type); 417static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 418 ctl_ua_type ua_type); 419static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 420static int ctl_abort_task(union ctl_io *io); 421static int ctl_abort_task_set(union ctl_io *io); 422static int ctl_query_task(union ctl_io *io, int task_set); 423static int ctl_i_t_nexus_reset(union ctl_io *io); 424static int ctl_query_async_event(union ctl_io *io); 425static void ctl_run_task(union ctl_io *io); 426#ifdef CTL_IO_DELAY 427static void ctl_datamove_timer_wakeup(void *arg); 428static void ctl_done_timer_wakeup(void *arg); 429#endif /* CTL_IO_DELAY */ 430 431static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 432static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 433static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 434static void ctl_datamove_remote_write(union ctl_io *io); 435static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 436static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 437static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 438static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 439 ctl_ha_dt_cb callback); 440static void ctl_datamove_remote_read(union ctl_io *io); 441static void ctl_datamove_remote(union ctl_io *io); 442static int ctl_process_done(union ctl_io *io); 443static void ctl_lun_thread(void *arg); 444static void ctl_thresh_thread(void *arg); 445static void ctl_work_thread(void *arg); 446static void ctl_enqueue_incoming(union ctl_io *io); 447static void ctl_enqueue_rtr(union ctl_io *io); 448static void ctl_enqueue_done(union ctl_io *io); 449static void ctl_enqueue_isc(union ctl_io *io); 450static const struct ctl_cmd_entry * 451 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 452static const struct ctl_cmd_entry * 453 ctl_validate_command(struct ctl_scsiio *ctsio); 454static int ctl_cmd_applicable(uint8_t lun_type, 455 const struct ctl_cmd_entry *entry); 456 457static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 458static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 459static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 460static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 461 462/* 463 * Load the serialization table. This isn't very pretty, but is probably 464 * the easiest way to do it. 465 */ 466#include "ctl_ser_table.c" 467 468/* 469 * We only need to define open, close and ioctl routines for this driver. 470 */ 471static struct cdevsw ctl_cdevsw = { 472 .d_version = D_VERSION, 473 .d_flags = 0, 474 .d_open = ctl_open, 475 .d_close = ctl_close, 476 .d_ioctl = ctl_ioctl, 477 .d_name = "ctl", 478}; 479 480 481MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 482 483static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 484 485static moduledata_t ctl_moduledata = { 486 "ctl", 487 ctl_module_event_handler, 488 NULL 489}; 490 491DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 492MODULE_VERSION(ctl, 1); 493 494static struct ctl_frontend ha_frontend = 495{ 496 .name = "ha", 497}; 498 499static void 500ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 501 union ctl_ha_msg *msg_info) 502{ 503 struct ctl_scsiio *ctsio; 504 505 if (msg_info->hdr.original_sc == NULL) { 506 printf("%s: original_sc == NULL!\n", __func__); 507 /* XXX KDM now what? */ 508 return; 509 } 510 511 ctsio = &msg_info->hdr.original_sc->scsiio; 512 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 513 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 514 ctsio->io_hdr.status = msg_info->hdr.status; 515 ctsio->scsi_status = msg_info->scsi.scsi_status; 516 ctsio->sense_len = msg_info->scsi.sense_len; 517 ctsio->sense_residual = msg_info->scsi.sense_residual; 518 ctsio->residual = msg_info->scsi.residual; 519 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 520 msg_info->scsi.sense_len); 521 ctl_enqueue_isc((union ctl_io *)ctsio); 522} 523 524static void 525ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 526 union ctl_ha_msg *msg_info) 527{ 528 struct ctl_scsiio *ctsio; 529 530 if (msg_info->hdr.serializing_sc == NULL) { 531 printf("%s: serializing_sc == NULL!\n", __func__); 532 /* XXX KDM now what? */ 533 return; 534 } 535 536 ctsio = &msg_info->hdr.serializing_sc->scsiio; 537 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 538 ctl_enqueue_isc((union ctl_io *)ctsio); 539} 540 541void 542ctl_isc_announce_lun(struct ctl_lun *lun) 543{ 544 struct ctl_softc *softc = lun->ctl_softc; 545 union ctl_ha_msg *msg; 546 struct ctl_ha_msg_lun_pr_key pr_key; 547 int i, k; 548 549 if (softc->ha_link != CTL_HA_LINK_ONLINE) 550 return; 551 mtx_lock(&lun->lun_lock); 552 i = sizeof(msg->lun); 553 if (lun->lun_devid) 554 i += lun->lun_devid->len; 555 i += sizeof(pr_key) * lun->pr_key_count; 556alloc: 557 mtx_unlock(&lun->lun_lock); 558 msg = malloc(i, M_CTL, M_WAITOK); 559 mtx_lock(&lun->lun_lock); 560 k = sizeof(msg->lun); 561 if (lun->lun_devid) 562 k += lun->lun_devid->len; 563 k += sizeof(pr_key) * lun->pr_key_count; 564 if (i < k) { 565 free(msg, M_CTL); 566 i = k; 567 goto alloc; 568 } 569 bzero(&msg->lun, sizeof(msg->lun)); 570 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 571 msg->hdr.nexus.targ_lun = lun->lun; 572 msg->hdr.nexus.targ_mapped_lun = lun->lun; 573 msg->lun.flags = lun->flags; 574 msg->lun.pr_generation = lun->PRGeneration; 575 msg->lun.pr_res_idx = lun->pr_res_idx; 576 msg->lun.pr_res_type = lun->res_type; 577 msg->lun.pr_key_count = lun->pr_key_count; 578 i = 0; 579 if (lun->lun_devid) { 580 msg->lun.lun_devid_len = lun->lun_devid->len; 581 memcpy(&msg->lun.data[i], lun->lun_devid->data, 582 msg->lun.lun_devid_len); 583 i += msg->lun.lun_devid_len; 584 } 585 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 586 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 587 continue; 588 pr_key.pr_iid = k; 589 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 590 i += sizeof(pr_key); 591 } 592 mtx_unlock(&lun->lun_lock); 593 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 594 M_WAITOK); 595 free(msg, M_CTL); 596} 597 598void 599ctl_isc_announce_port(struct ctl_port *port) 600{ 601 struct ctl_softc *softc = control_softc; 602 union ctl_ha_msg *msg; 603 int i; 604 605 if (port->targ_port < softc->port_min || 606 port->targ_port >= softc->port_max || 607 softc->ha_link != CTL_HA_LINK_ONLINE) 608 return; 609 i = sizeof(msg->port) + strlen(port->port_name) + 1; 610 if (port->lun_map) 611 i += sizeof(uint32_t) * CTL_MAX_LUNS; 612 if (port->port_devid) 613 i += port->port_devid->len; 614 if (port->target_devid) 615 i += port->target_devid->len; 616 msg = malloc(i, M_CTL, M_WAITOK); 617 bzero(&msg->port, sizeof(msg->port)); 618 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 619 msg->hdr.nexus.targ_port = port->targ_port; 620 msg->port.port_type = port->port_type; 621 msg->port.physical_port = port->physical_port; 622 msg->port.virtual_port = port->virtual_port; 623 msg->port.status = port->status; 624 i = 0; 625 msg->port.name_len = sprintf(&msg->port.data[i], 626 "%d:%s", softc->ha_id, port->port_name) + 1; 627 i += msg->port.name_len; 628 if (port->lun_map) { 629 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 630 memcpy(&msg->port.data[i], port->lun_map, 631 msg->port.lun_map_len); 632 i += msg->port.lun_map_len; 633 } 634 if (port->port_devid) { 635 msg->port.port_devid_len = port->port_devid->len; 636 memcpy(&msg->port.data[i], port->port_devid->data, 637 msg->port.port_devid_len); 638 i += msg->port.port_devid_len; 639 } 640 if (port->target_devid) { 641 msg->port.target_devid_len = port->target_devid->len; 642 memcpy(&msg->port.data[i], port->target_devid->data, 643 msg->port.target_devid_len); 644 i += msg->port.target_devid_len; 645 } 646 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 647 M_WAITOK); 648 free(msg, M_CTL); 649} 650 651static void 652ctl_isc_ha_link_up(struct ctl_softc *softc) 653{ 654 struct ctl_port *port; 655 struct ctl_lun *lun; 656 657 STAILQ_FOREACH(port, &softc->port_list, links) 658 ctl_isc_announce_port(port); 659 STAILQ_FOREACH(lun, &softc->lun_list, links) 660 ctl_isc_announce_lun(lun); 661} 662 663static void 664ctl_isc_ha_link_down(struct ctl_softc *softc) 665{ 666 struct ctl_port *port; 667 struct ctl_lun *lun; 668 union ctl_io *io; 669 670 mtx_lock(&softc->ctl_lock); 671 STAILQ_FOREACH(lun, &softc->lun_list, links) { 672 mtx_lock(&lun->lun_lock); 673 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 674 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 675 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 676 } 677 mtx_unlock(&lun->lun_lock); 678 679 mtx_unlock(&softc->ctl_lock); 680 io = ctl_alloc_io(softc->othersc_pool); 681 mtx_lock(&softc->ctl_lock); 682 ctl_zero_io(io); 683 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 684 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 685 ctl_enqueue_isc(io); 686 } 687 688 STAILQ_FOREACH(port, &softc->port_list, links) { 689 if (port->targ_port >= softc->port_min && 690 port->targ_port < softc->port_max) 691 continue; 692 port->status &= ~CTL_PORT_STATUS_ONLINE; 693 } 694 mtx_unlock(&softc->ctl_lock); 695} 696 697static void 698ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 699{ 700 struct ctl_lun *lun; 701 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 702 703 mtx_lock(&softc->ctl_lock); 704 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 705 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 706 mtx_lock(&lun->lun_lock); 707 mtx_unlock(&softc->ctl_lock); 708 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 709 msg->ua.ua_set) 710 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 711 if (msg->ua.ua_all) { 712 if (msg->ua.ua_set) 713 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 714 else 715 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 716 } else { 717 if (msg->ua.ua_set) 718 ctl_est_ua(lun, iid, msg->ua.ua_type); 719 else 720 ctl_clr_ua(lun, iid, msg->ua.ua_type); 721 } 722 mtx_unlock(&lun->lun_lock); 723 } else 724 mtx_unlock(&softc->ctl_lock); 725} 726 727static void 728ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 729{ 730 struct ctl_lun *lun; 731 struct ctl_ha_msg_lun_pr_key pr_key; 732 int i, k; 733 ctl_lun_flags oflags; 734 uint32_t targ_lun; 735 736 targ_lun = msg->hdr.nexus.targ_mapped_lun; 737 mtx_lock(&softc->ctl_lock); 738 if ((targ_lun >= CTL_MAX_LUNS) || 739 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 740 mtx_unlock(&softc->ctl_lock); 741 return; 742 } 743 mtx_lock(&lun->lun_lock); 744 mtx_unlock(&softc->ctl_lock); 745 if (lun->flags & CTL_LUN_DISABLED) { 746 mtx_unlock(&lun->lun_lock); 747 return; 748 } 749 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 750 if (msg->lun.lun_devid_len != i || (i > 0 && 751 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 752 mtx_unlock(&lun->lun_lock); 753 printf("%s: Received conflicting HA LUN %d\n", 754 __func__, msg->hdr.nexus.targ_lun); 755 return; 756 } else { 757 /* Record whether peer is primary. */ 758 oflags = lun->flags; 759 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 760 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 761 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 762 else 763 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 764 if (oflags != lun->flags) 765 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 766 767 /* If peer is primary and we are not -- use data */ 768 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 769 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 770 lun->PRGeneration = msg->lun.pr_generation; 771 lun->pr_res_idx = msg->lun.pr_res_idx; 772 lun->res_type = msg->lun.pr_res_type; 773 lun->pr_key_count = msg->lun.pr_key_count; 774 for (k = 0; k < CTL_MAX_INITIATORS; k++) 775 ctl_clr_prkey(lun, k); 776 for (k = 0; k < msg->lun.pr_key_count; k++) { 777 memcpy(&pr_key, &msg->lun.data[i], 778 sizeof(pr_key)); 779 ctl_alloc_prkey(lun, pr_key.pr_iid); 780 ctl_set_prkey(lun, pr_key.pr_iid, 781 pr_key.pr_key); 782 i += sizeof(pr_key); 783 } 784 } 785 786 mtx_unlock(&lun->lun_lock); 787 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 788 __func__, msg->hdr.nexus.targ_lun, 789 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 790 "primary" : "secondary")); 791 792 /* If we are primary but peer doesn't know -- notify */ 793 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 794 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 795 ctl_isc_announce_lun(lun); 796 } 797} 798 799static void 800ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 801{ 802 struct ctl_port *port; 803 struct ctl_lun *lun; 804 int i, new; 805 806 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 807 if (port == NULL) { 808 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 809 msg->hdr.nexus.targ_port)); 810 new = 1; 811 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 812 port->frontend = &ha_frontend; 813 port->targ_port = msg->hdr.nexus.targ_port; 814 } else if (port->frontend == &ha_frontend) { 815 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 816 msg->hdr.nexus.targ_port)); 817 new = 0; 818 } else { 819 printf("%s: Received conflicting HA port %d\n", 820 __func__, msg->hdr.nexus.targ_port); 821 return; 822 } 823 port->port_type = msg->port.port_type; 824 port->physical_port = msg->port.physical_port; 825 port->virtual_port = msg->port.virtual_port; 826 port->status = msg->port.status; 827 i = 0; 828 free(port->port_name, M_CTL); 829 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 830 M_CTL); 831 i += msg->port.name_len; 832 if (msg->port.lun_map_len != 0) { 833 if (port->lun_map == NULL) 834 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 835 M_CTL, M_WAITOK); 836 memcpy(port->lun_map, &msg->port.data[i], 837 sizeof(uint32_t) * CTL_MAX_LUNS); 838 i += msg->port.lun_map_len; 839 } else { 840 free(port->lun_map, M_CTL); 841 port->lun_map = NULL; 842 } 843 if (msg->port.port_devid_len != 0) { 844 if (port->port_devid == NULL || 845 port->port_devid->len != msg->port.port_devid_len) { 846 free(port->port_devid, M_CTL); 847 port->port_devid = malloc(sizeof(struct ctl_devid) + 848 msg->port.port_devid_len, M_CTL, M_WAITOK); 849 } 850 memcpy(port->port_devid->data, &msg->port.data[i], 851 msg->port.port_devid_len); 852 port->port_devid->len = msg->port.port_devid_len; 853 i += msg->port.port_devid_len; 854 } else { 855 free(port->port_devid, M_CTL); 856 port->port_devid = NULL; 857 } 858 if (msg->port.target_devid_len != 0) { 859 if (port->target_devid == NULL || 860 port->target_devid->len != msg->port.target_devid_len) { 861 free(port->target_devid, M_CTL); 862 port->target_devid = malloc(sizeof(struct ctl_devid) + 863 msg->port.target_devid_len, M_CTL, M_WAITOK); 864 } 865 memcpy(port->target_devid->data, &msg->port.data[i], 866 msg->port.target_devid_len); 867 port->target_devid->len = msg->port.target_devid_len; 868 i += msg->port.target_devid_len; 869 } else { 870 free(port->port_devid, M_CTL); 871 port->port_devid = NULL; 872 } 873 if (new) { 874 if (ctl_port_register(port) != 0) { 875 printf("%s: ctl_port_register() failed with error\n", 876 __func__); 877 } 878 } 879 mtx_lock(&softc->ctl_lock); 880 STAILQ_FOREACH(lun, &softc->lun_list, links) { 881 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 882 continue; 883 mtx_lock(&lun->lun_lock); 884 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 885 mtx_unlock(&lun->lun_lock); 886 } 887 mtx_unlock(&softc->ctl_lock); 888} 889 890/* 891 * ISC (Inter Shelf Communication) event handler. Events from the HA 892 * subsystem come in here. 893 */ 894static void 895ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 896{ 897 struct ctl_softc *softc; 898 union ctl_io *io; 899 struct ctl_prio *presio; 900 ctl_ha_status isc_status; 901 902 softc = control_softc; 903 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 904 if (event == CTL_HA_EVT_MSG_RECV) { 905 union ctl_ha_msg *msg, msgbuf; 906 907 if (param > sizeof(msgbuf)) 908 msg = malloc(param, M_CTL, M_WAITOK); 909 else 910 msg = &msgbuf; 911 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 912 M_WAITOK); 913 if (isc_status != CTL_HA_STATUS_SUCCESS) { 914 printf("%s: Error receiving message: %d\n", 915 __func__, isc_status); 916 if (msg != &msgbuf) 917 free(msg, M_CTL); 918 return; 919 } 920 921 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 922 switch (msg->hdr.msg_type) { 923 case CTL_MSG_SERIALIZE: 924 io = ctl_alloc_io(softc->othersc_pool); 925 ctl_zero_io(io); 926 // populate ctsio from msg 927 io->io_hdr.io_type = CTL_IO_SCSI; 928 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 929 io->io_hdr.original_sc = msg->hdr.original_sc; 930 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 931 CTL_FLAG_IO_ACTIVE; 932 /* 933 * If we're in serialization-only mode, we don't 934 * want to go through full done processing. Thus 935 * the COPY flag. 936 * 937 * XXX KDM add another flag that is more specific. 938 */ 939 if (softc->ha_mode != CTL_HA_MODE_XFER) 940 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 941 io->io_hdr.nexus = msg->hdr.nexus; 942#if 0 943 printf("port %u, iid %u, lun %u\n", 944 io->io_hdr.nexus.targ_port, 945 io->io_hdr.nexus.initid, 946 io->io_hdr.nexus.targ_lun); 947#endif 948 io->scsiio.tag_num = msg->scsi.tag_num; 949 io->scsiio.tag_type = msg->scsi.tag_type; 950#ifdef CTL_TIME_IO 951 io->io_hdr.start_time = time_uptime; 952 getbintime(&io->io_hdr.start_bt); 953#endif /* CTL_TIME_IO */ 954 io->scsiio.cdb_len = msg->scsi.cdb_len; 955 memcpy(io->scsiio.cdb, msg->scsi.cdb, 956 CTL_MAX_CDBLEN); 957 if (softc->ha_mode == CTL_HA_MODE_XFER) { 958 const struct ctl_cmd_entry *entry; 959 960 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 961 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 962 io->io_hdr.flags |= 963 entry->flags & CTL_FLAG_DATA_MASK; 964 } 965 ctl_enqueue_isc(io); 966 break; 967 968 /* Performed on the Originating SC, XFER mode only */ 969 case CTL_MSG_DATAMOVE: { 970 struct ctl_sg_entry *sgl; 971 int i, j; 972 973 io = msg->hdr.original_sc; 974 if (io == NULL) { 975 printf("%s: original_sc == NULL!\n", __func__); 976 /* XXX KDM do something here */ 977 break; 978 } 979 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 980 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 981 /* 982 * Keep track of this, we need to send it back over 983 * when the datamove is complete. 984 */ 985 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 986 if (msg->hdr.status == CTL_SUCCESS) 987 io->io_hdr.status = msg->hdr.status; 988 989 if (msg->dt.sg_sequence == 0) { 990 i = msg->dt.kern_sg_entries + 991 io->scsiio.kern_data_len / 992 CTL_HA_DATAMOVE_SEGMENT + 1; 993 sgl = malloc(sizeof(*sgl) * i, M_CTL, 994 M_WAITOK | M_ZERO); 995 io->io_hdr.remote_sglist = sgl; 996 io->io_hdr.local_sglist = 997 &sgl[msg->dt.kern_sg_entries]; 998 999 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1000 1001 io->scsiio.kern_sg_entries = 1002 msg->dt.kern_sg_entries; 1003 io->scsiio.rem_sg_entries = 1004 msg->dt.kern_sg_entries; 1005 io->scsiio.kern_data_len = 1006 msg->dt.kern_data_len; 1007 io->scsiio.kern_total_len = 1008 msg->dt.kern_total_len; 1009 io->scsiio.kern_data_resid = 1010 msg->dt.kern_data_resid; 1011 io->scsiio.kern_rel_offset = 1012 msg->dt.kern_rel_offset; 1013 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1014 io->io_hdr.flags |= msg->dt.flags & 1015 CTL_FLAG_BUS_ADDR; 1016 } else 1017 sgl = (struct ctl_sg_entry *) 1018 io->scsiio.kern_data_ptr; 1019 1020 for (i = msg->dt.sent_sg_entries, j = 0; 1021 i < (msg->dt.sent_sg_entries + 1022 msg->dt.cur_sg_entries); i++, j++) { 1023 sgl[i].addr = msg->dt.sg_list[j].addr; 1024 sgl[i].len = msg->dt.sg_list[j].len; 1025 1026#if 0 1027 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 1028 __func__, 1029 msg->dt.sg_list[j].addr, 1030 msg->dt.sg_list[j].len, 1031 sgl[i].addr, sgl[i].len, j, i); 1032#endif 1033 } 1034 1035 /* 1036 * If this is the last piece of the I/O, we've got 1037 * the full S/G list. Queue processing in the thread. 1038 * Otherwise wait for the next piece. 1039 */ 1040 if (msg->dt.sg_last != 0) 1041 ctl_enqueue_isc(io); 1042 break; 1043 } 1044 /* Performed on the Serializing (primary) SC, XFER mode only */ 1045 case CTL_MSG_DATAMOVE_DONE: { 1046 if (msg->hdr.serializing_sc == NULL) { 1047 printf("%s: serializing_sc == NULL!\n", 1048 __func__); 1049 /* XXX KDM now what? */ 1050 break; 1051 } 1052 /* 1053 * We grab the sense information here in case 1054 * there was a failure, so we can return status 1055 * back to the initiator. 1056 */ 1057 io = msg->hdr.serializing_sc; 1058 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1059 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1060 io->io_hdr.port_status = msg->scsi.fetd_status; 1061 io->scsiio.residual = msg->scsi.residual; 1062 if (msg->hdr.status != CTL_STATUS_NONE) { 1063 io->io_hdr.status = msg->hdr.status; 1064 io->scsiio.scsi_status = msg->scsi.scsi_status; 1065 io->scsiio.sense_len = msg->scsi.sense_len; 1066 io->scsiio.sense_residual =msg->scsi.sense_residual; 1067 memcpy(&io->scsiio.sense_data, 1068 &msg->scsi.sense_data, 1069 msg->scsi.sense_len); 1070 if (msg->hdr.status == CTL_SUCCESS) 1071 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1072 } 1073 ctl_enqueue_isc(io); 1074 break; 1075 } 1076 1077 /* Preformed on Originating SC, SER_ONLY mode */ 1078 case CTL_MSG_R2R: 1079 io = msg->hdr.original_sc; 1080 if (io == NULL) { 1081 printf("%s: original_sc == NULL!\n", 1082 __func__); 1083 break; 1084 } 1085 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1086 io->io_hdr.msg_type = CTL_MSG_R2R; 1087 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1088 ctl_enqueue_isc(io); 1089 break; 1090 1091 /* 1092 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1093 * mode. 1094 * Performed on the Originating (i.e. secondary) SC in XFER 1095 * mode 1096 */ 1097 case CTL_MSG_FINISH_IO: 1098 if (softc->ha_mode == CTL_HA_MODE_XFER) 1099 ctl_isc_handler_finish_xfer(softc, msg); 1100 else 1101 ctl_isc_handler_finish_ser_only(softc, msg); 1102 break; 1103 1104 /* Preformed on Originating SC */ 1105 case CTL_MSG_BAD_JUJU: 1106 io = msg->hdr.original_sc; 1107 if (io == NULL) { 1108 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1109 __func__); 1110 break; 1111 } 1112 ctl_copy_sense_data(msg, io); 1113 /* 1114 * IO should have already been cleaned up on other 1115 * SC so clear this flag so we won't send a message 1116 * back to finish the IO there. 1117 */ 1118 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1119 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1120 1121 /* io = msg->hdr.serializing_sc; */ 1122 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1123 ctl_enqueue_isc(io); 1124 break; 1125 1126 /* Handle resets sent from the other side */ 1127 case CTL_MSG_MANAGE_TASKS: { 1128 struct ctl_taskio *taskio; 1129 taskio = (struct ctl_taskio *)ctl_alloc_io( 1130 softc->othersc_pool); 1131 ctl_zero_io((union ctl_io *)taskio); 1132 taskio->io_hdr.io_type = CTL_IO_TASK; 1133 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1134 taskio->io_hdr.nexus = msg->hdr.nexus; 1135 taskio->task_action = msg->task.task_action; 1136 taskio->tag_num = msg->task.tag_num; 1137 taskio->tag_type = msg->task.tag_type; 1138#ifdef CTL_TIME_IO 1139 taskio->io_hdr.start_time = time_uptime; 1140 getbintime(&taskio->io_hdr.start_bt); 1141#endif /* CTL_TIME_IO */ 1142 ctl_run_task((union ctl_io *)taskio); 1143 break; 1144 } 1145 /* Persistent Reserve action which needs attention */ 1146 case CTL_MSG_PERS_ACTION: 1147 presio = (struct ctl_prio *)ctl_alloc_io( 1148 softc->othersc_pool); 1149 ctl_zero_io((union ctl_io *)presio); 1150 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1151 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1152 presio->io_hdr.nexus = msg->hdr.nexus; 1153 presio->pr_msg = msg->pr; 1154 ctl_enqueue_isc((union ctl_io *)presio); 1155 break; 1156 case CTL_MSG_UA: 1157 ctl_isc_ua(softc, msg, param); 1158 break; 1159 case CTL_MSG_PORT_SYNC: 1160 ctl_isc_port_sync(softc, msg, param); 1161 break; 1162 case CTL_MSG_LUN_SYNC: 1163 ctl_isc_lun_sync(softc, msg, param); 1164 break; 1165 default: 1166 printf("Received HA message of unknown type %d\n", 1167 msg->hdr.msg_type); 1168 break; 1169 } 1170 if (msg != &msgbuf) 1171 free(msg, M_CTL); 1172 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1173 printf("CTL: HA link status changed from %d to %d\n", 1174 softc->ha_link, param); 1175 if (param == softc->ha_link) 1176 return; 1177 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1178 softc->ha_link = param; 1179 ctl_isc_ha_link_down(softc); 1180 } else { 1181 softc->ha_link = param; 1182 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1183 ctl_isc_ha_link_up(softc); 1184 } 1185 return; 1186 } else { 1187 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1188 return; 1189 } 1190} 1191 1192static void 1193ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1194{ 1195 1196 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1197 src->scsi.sense_len); 1198 dest->scsiio.scsi_status = src->scsi.scsi_status; 1199 dest->scsiio.sense_len = src->scsi.sense_len; 1200 dest->io_hdr.status = src->hdr.status; 1201} 1202 1203static void 1204ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1205{ 1206 1207 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1208 src->scsiio.sense_len); 1209 dest->scsi.scsi_status = src->scsiio.scsi_status; 1210 dest->scsi.sense_len = src->scsiio.sense_len; 1211 dest->hdr.status = src->io_hdr.status; 1212} 1213 1214void 1215ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1216{ 1217 struct ctl_softc *softc = lun->ctl_softc; 1218 ctl_ua_type *pu; 1219 1220 if (initidx < softc->init_min || initidx >= softc->init_max) 1221 return; 1222 mtx_assert(&lun->lun_lock, MA_OWNED); 1223 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1224 if (pu == NULL) 1225 return; 1226 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1227} 1228 1229void 1230ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1231{ 1232 int i; 1233 1234 mtx_assert(&lun->lun_lock, MA_OWNED); 1235 if (lun->pending_ua[port] == NULL) 1236 return; 1237 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1238 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1239 continue; 1240 lun->pending_ua[port][i] |= ua; 1241 } 1242} 1243 1244void 1245ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1246{ 1247 struct ctl_softc *softc = lun->ctl_softc; 1248 int i; 1249 1250 mtx_assert(&lun->lun_lock, MA_OWNED); 1251 for (i = softc->port_min; i < softc->port_max; i++) 1252 ctl_est_ua_port(lun, i, except, ua); 1253} 1254 1255void 1256ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1257{ 1258 struct ctl_softc *softc = lun->ctl_softc; 1259 ctl_ua_type *pu; 1260 1261 if (initidx < softc->init_min || initidx >= softc->init_max) 1262 return; 1263 mtx_assert(&lun->lun_lock, MA_OWNED); 1264 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1265 if (pu == NULL) 1266 return; 1267 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1268} 1269 1270void 1271ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1272{ 1273 struct ctl_softc *softc = lun->ctl_softc; 1274 int i, j; 1275 1276 mtx_assert(&lun->lun_lock, MA_OWNED); 1277 for (i = softc->port_min; i < softc->port_max; i++) { 1278 if (lun->pending_ua[i] == NULL) 1279 continue; 1280 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1281 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1282 continue; 1283 lun->pending_ua[i][j] &= ~ua; 1284 } 1285 } 1286} 1287 1288void 1289ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1290 ctl_ua_type ua_type) 1291{ 1292 struct ctl_lun *lun; 1293 1294 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1295 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1296 mtx_lock(&lun->lun_lock); 1297 ctl_clr_ua(lun, initidx, ua_type); 1298 mtx_unlock(&lun->lun_lock); 1299 } 1300} 1301 1302static int 1303ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1304{ 1305 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1306 struct ctl_lun *lun; 1307 struct ctl_lun_req ireq; 1308 int error, value; 1309 1310 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1311 error = sysctl_handle_int(oidp, &value, 0, req); 1312 if ((error != 0) || (req->newptr == NULL)) 1313 return (error); 1314 1315 mtx_lock(&softc->ctl_lock); 1316 if (value == 0) 1317 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1318 else 1319 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1320 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1321 mtx_unlock(&softc->ctl_lock); 1322 bzero(&ireq, sizeof(ireq)); 1323 ireq.reqtype = CTL_LUNREQ_MODIFY; 1324 ireq.reqdata.modify.lun_id = lun->lun; 1325 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1326 curthread); 1327 if (ireq.status != CTL_LUN_OK) { 1328 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1329 __func__, ireq.status, ireq.error_str); 1330 } 1331 mtx_lock(&softc->ctl_lock); 1332 } 1333 mtx_unlock(&softc->ctl_lock); 1334 return (0); 1335} 1336 1337static int 1338ctl_init(void) 1339{ 1340 struct ctl_softc *softc; 1341 void *other_pool; 1342 int i, error, retval; 1343 1344 retval = 0; 1345 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1346 M_WAITOK | M_ZERO); 1347 softc = control_softc; 1348 1349 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1350 "cam/ctl"); 1351 1352 softc->dev->si_drv1 = softc; 1353 1354 sysctl_ctx_init(&softc->sysctl_ctx); 1355 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1356 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1357 CTLFLAG_RD, 0, "CAM Target Layer"); 1358 1359 if (softc->sysctl_tree == NULL) { 1360 printf("%s: unable to allocate sysctl tree\n", __func__); 1361 destroy_dev(softc->dev); 1362 free(control_softc, M_DEVBUF); 1363 control_softc = NULL; 1364 return (ENOMEM); 1365 } 1366 1367 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1368 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1369 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1370 softc->open_count = 0; 1371 1372 /* 1373 * Default to actually sending a SYNCHRONIZE CACHE command down to 1374 * the drive. 1375 */ 1376 softc->flags = CTL_FLAG_REAL_SYNC; 1377 1378 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1379 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1380 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1381 1382 /* 1383 * In Copan's HA scheme, the "master" and "slave" roles are 1384 * figured out through the slot the controller is in. Although it 1385 * is an active/active system, someone has to be in charge. 1386 */ 1387 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1388 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1389 "HA head ID (0 - no HA)"); 1390 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1391 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1392 softc->is_single = 1; 1393 softc->port_cnt = CTL_MAX_PORTS; 1394 softc->port_min = 0; 1395 } else { 1396 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1397 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1398 } 1399 softc->port_max = softc->port_min + softc->port_cnt; 1400 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1401 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1402 1403 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1404 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1405 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1406 1407 STAILQ_INIT(&softc->lun_list); 1408 STAILQ_INIT(&softc->pending_lun_queue); 1409 STAILQ_INIT(&softc->fe_list); 1410 STAILQ_INIT(&softc->port_list); 1411 STAILQ_INIT(&softc->be_list); 1412 ctl_tpc_init(softc); 1413 1414 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1415 &other_pool) != 0) 1416 { 1417 printf("ctl: can't allocate %d entry other SC pool, " 1418 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1419 return (ENOMEM); 1420 } 1421 softc->othersc_pool = other_pool; 1422 1423 if (worker_threads <= 0) 1424 worker_threads = max(1, mp_ncpus / 4); 1425 if (worker_threads > CTL_MAX_THREADS) 1426 worker_threads = CTL_MAX_THREADS; 1427 1428 for (i = 0; i < worker_threads; i++) { 1429 struct ctl_thread *thr = &softc->threads[i]; 1430 1431 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1432 thr->ctl_softc = softc; 1433 STAILQ_INIT(&thr->incoming_queue); 1434 STAILQ_INIT(&thr->rtr_queue); 1435 STAILQ_INIT(&thr->done_queue); 1436 STAILQ_INIT(&thr->isc_queue); 1437 1438 error = kproc_kthread_add(ctl_work_thread, thr, 1439 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1440 if (error != 0) { 1441 printf("error creating CTL work thread!\n"); 1442 ctl_pool_free(other_pool); 1443 return (error); 1444 } 1445 } 1446 error = kproc_kthread_add(ctl_lun_thread, softc, 1447 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1448 if (error != 0) { 1449 printf("error creating CTL lun thread!\n"); 1450 ctl_pool_free(other_pool); 1451 return (error); 1452 } 1453 error = kproc_kthread_add(ctl_thresh_thread, softc, 1454 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1455 if (error != 0) { 1456 printf("error creating CTL threshold thread!\n"); 1457 ctl_pool_free(other_pool); 1458 return (error); 1459 } 1460 1461 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1462 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1463 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1464 1465 if (softc->is_single == 0) { 1466 ctl_frontend_register(&ha_frontend); 1467 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1468 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1469 softc->is_single = 1; 1470 } else 1471 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1472 != CTL_HA_STATUS_SUCCESS) { 1473 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1474 softc->is_single = 1; 1475 } 1476 } 1477 return (0); 1478} 1479 1480void 1481ctl_shutdown(void) 1482{ 1483 struct ctl_softc *softc; 1484 struct ctl_lun *lun, *next_lun; 1485 1486 softc = (struct ctl_softc *)control_softc; 1487 1488 if (softc->is_single == 0) { 1489 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1490 != CTL_HA_STATUS_SUCCESS) { 1491 printf("ctl_shutdown: ctl_ha_msg_deregister failed.\n"); 1492 } 1493 if (ctl_ha_msg_shutdown(softc) != CTL_HA_STATUS_SUCCESS) { 1494 printf("ctl_shutdown: ctl_ha_msg_shutdown failed.\n"); 1495 } 1496 ctl_frontend_deregister(&ha_frontend); 1497 } 1498 1499 mtx_lock(&softc->ctl_lock); 1500 1501 /* 1502 * Free up each LUN. 1503 */ 1504 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1505 next_lun = STAILQ_NEXT(lun, links); 1506 ctl_free_lun(lun); 1507 } 1508 1509 mtx_unlock(&softc->ctl_lock); 1510 1511#if 0 1512 ctl_shutdown_thread(softc->work_thread); 1513 mtx_destroy(&softc->queue_lock); 1514#endif 1515 1516 ctl_tpc_shutdown(softc); 1517 uma_zdestroy(softc->io_zone); 1518 mtx_destroy(&softc->ctl_lock); 1519 1520 destroy_dev(softc->dev); 1521 1522 sysctl_ctx_free(&softc->sysctl_ctx); 1523 1524 free(control_softc, M_DEVBUF); 1525 control_softc = NULL; 1526} 1527 1528static int 1529ctl_module_event_handler(module_t mod, int what, void *arg) 1530{ 1531 1532 switch (what) { 1533 case MOD_LOAD: 1534 return (ctl_init()); 1535 case MOD_UNLOAD: 1536 return (EBUSY); 1537 default: 1538 return (EOPNOTSUPP); 1539 } 1540} 1541 1542/* 1543 * XXX KDM should we do some access checks here? Bump a reference count to 1544 * prevent a CTL module from being unloaded while someone has it open? 1545 */ 1546static int 1547ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1548{ 1549 return (0); 1550} 1551 1552static int 1553ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1554{ 1555 return (0); 1556} 1557 1558/* 1559 * Remove an initiator by port number and initiator ID. 1560 * Returns 0 for success, -1 for failure. 1561 */ 1562int 1563ctl_remove_initiator(struct ctl_port *port, int iid) 1564{ 1565 struct ctl_softc *softc = control_softc; 1566 1567 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1568 1569 if (iid > CTL_MAX_INIT_PER_PORT) { 1570 printf("%s: initiator ID %u > maximun %u!\n", 1571 __func__, iid, CTL_MAX_INIT_PER_PORT); 1572 return (-1); 1573 } 1574 1575 mtx_lock(&softc->ctl_lock); 1576 port->wwpn_iid[iid].in_use--; 1577 port->wwpn_iid[iid].last_use = time_uptime; 1578 mtx_unlock(&softc->ctl_lock); 1579 1580 return (0); 1581} 1582 1583/* 1584 * Add an initiator to the initiator map. 1585 * Returns iid for success, < 0 for failure. 1586 */ 1587int 1588ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1589{ 1590 struct ctl_softc *softc = control_softc; 1591 time_t best_time; 1592 int i, best; 1593 1594 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1595 1596 if (iid >= CTL_MAX_INIT_PER_PORT) { 1597 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1598 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1599 free(name, M_CTL); 1600 return (-1); 1601 } 1602 1603 mtx_lock(&softc->ctl_lock); 1604 1605 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1606 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1607 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1608 iid = i; 1609 break; 1610 } 1611 if (name != NULL && port->wwpn_iid[i].name != NULL && 1612 strcmp(name, port->wwpn_iid[i].name) == 0) { 1613 iid = i; 1614 break; 1615 } 1616 } 1617 } 1618 1619 if (iid < 0) { 1620 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1621 if (port->wwpn_iid[i].in_use == 0 && 1622 port->wwpn_iid[i].wwpn == 0 && 1623 port->wwpn_iid[i].name == NULL) { 1624 iid = i; 1625 break; 1626 } 1627 } 1628 } 1629 1630 if (iid < 0) { 1631 best = -1; 1632 best_time = INT32_MAX; 1633 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1634 if (port->wwpn_iid[i].in_use == 0) { 1635 if (port->wwpn_iid[i].last_use < best_time) { 1636 best = i; 1637 best_time = port->wwpn_iid[i].last_use; 1638 } 1639 } 1640 } 1641 iid = best; 1642 } 1643 1644 if (iid < 0) { 1645 mtx_unlock(&softc->ctl_lock); 1646 free(name, M_CTL); 1647 return (-2); 1648 } 1649 1650 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1651 /* 1652 * This is not an error yet. 1653 */ 1654 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1655#if 0 1656 printf("%s: port %d iid %u WWPN %#jx arrived" 1657 " again\n", __func__, port->targ_port, 1658 iid, (uintmax_t)wwpn); 1659#endif 1660 goto take; 1661 } 1662 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1663 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1664#if 0 1665 printf("%s: port %d iid %u name '%s' arrived" 1666 " again\n", __func__, port->targ_port, 1667 iid, name); 1668#endif 1669 goto take; 1670 } 1671 1672 /* 1673 * This is an error, but what do we do about it? The 1674 * driver is telling us we have a new WWPN for this 1675 * initiator ID, so we pretty much need to use it. 1676 */ 1677 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1678 " but WWPN %#jx '%s' is still at that address\n", 1679 __func__, port->targ_port, iid, wwpn, name, 1680 (uintmax_t)port->wwpn_iid[iid].wwpn, 1681 port->wwpn_iid[iid].name); 1682 1683 /* 1684 * XXX KDM clear have_ca and ua_pending on each LUN for 1685 * this initiator. 1686 */ 1687 } 1688take: 1689 free(port->wwpn_iid[iid].name, M_CTL); 1690 port->wwpn_iid[iid].name = name; 1691 port->wwpn_iid[iid].wwpn = wwpn; 1692 port->wwpn_iid[iid].in_use++; 1693 mtx_unlock(&softc->ctl_lock); 1694 1695 return (iid); 1696} 1697 1698static int 1699ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1700{ 1701 int len; 1702 1703 switch (port->port_type) { 1704 case CTL_PORT_FC: 1705 { 1706 struct scsi_transportid_fcp *id = 1707 (struct scsi_transportid_fcp *)buf; 1708 if (port->wwpn_iid[iid].wwpn == 0) 1709 return (0); 1710 memset(id, 0, sizeof(*id)); 1711 id->format_protocol = SCSI_PROTO_FC; 1712 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1713 return (sizeof(*id)); 1714 } 1715 case CTL_PORT_ISCSI: 1716 { 1717 struct scsi_transportid_iscsi_port *id = 1718 (struct scsi_transportid_iscsi_port *)buf; 1719 if (port->wwpn_iid[iid].name == NULL) 1720 return (0); 1721 memset(id, 0, 256); 1722 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1723 SCSI_PROTO_ISCSI; 1724 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1725 len = roundup2(min(len, 252), 4); 1726 scsi_ulto2b(len, id->additional_length); 1727 return (sizeof(*id) + len); 1728 } 1729 case CTL_PORT_SAS: 1730 { 1731 struct scsi_transportid_sas *id = 1732 (struct scsi_transportid_sas *)buf; 1733 if (port->wwpn_iid[iid].wwpn == 0) 1734 return (0); 1735 memset(id, 0, sizeof(*id)); 1736 id->format_protocol = SCSI_PROTO_SAS; 1737 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1738 return (sizeof(*id)); 1739 } 1740 default: 1741 { 1742 struct scsi_transportid_spi *id = 1743 (struct scsi_transportid_spi *)buf; 1744 memset(id, 0, sizeof(*id)); 1745 id->format_protocol = SCSI_PROTO_SPI; 1746 scsi_ulto2b(iid, id->scsi_addr); 1747 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1748 return (sizeof(*id)); 1749 } 1750 } 1751} 1752 1753/* 1754 * Serialize a command that went down the "wrong" side, and so was sent to 1755 * this controller for execution. The logic is a little different than the 1756 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1757 * sent back to the other side, but in the success case, we execute the 1758 * command on this side (XFER mode) or tell the other side to execute it 1759 * (SER_ONLY mode). 1760 */ 1761static int 1762ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1763{ 1764 struct ctl_softc *softc; 1765 union ctl_ha_msg msg_info; 1766 struct ctl_lun *lun; 1767 const struct ctl_cmd_entry *entry; 1768 int retval = 0; 1769 uint32_t targ_lun; 1770 1771 softc = control_softc; 1772 1773 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1774 mtx_lock(&softc->ctl_lock); 1775 if ((targ_lun < CTL_MAX_LUNS) && 1776 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 1777 mtx_lock(&lun->lun_lock); 1778 mtx_unlock(&softc->ctl_lock); 1779 /* 1780 * If the LUN is invalid, pretend that it doesn't exist. 1781 * It will go away as soon as all pending I/O has been 1782 * completed. 1783 */ 1784 if (lun->flags & CTL_LUN_DISABLED) { 1785 mtx_unlock(&lun->lun_lock); 1786 lun = NULL; 1787 } 1788 } else { 1789 mtx_unlock(&softc->ctl_lock); 1790 lun = NULL; 1791 } 1792 if (lun == NULL) { 1793 /* 1794 * The other node would not send this request to us unless 1795 * received announce that we are primary node for this LUN. 1796 * If this LUN does not exist now, it is probably result of 1797 * a race, so respond to initiator in the most opaque way. 1798 */ 1799 ctl_set_busy(ctsio); 1800 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1801 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1802 msg_info.hdr.serializing_sc = NULL; 1803 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1804 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1805 sizeof(msg_info.scsi), M_WAITOK); 1806 return(1); 1807 } 1808 1809 entry = ctl_get_cmd_entry(ctsio, NULL); 1810 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 1811 mtx_unlock(&lun->lun_lock); 1812 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1813 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1814 msg_info.hdr.serializing_sc = NULL; 1815 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1816 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1817 sizeof(msg_info.scsi), M_WAITOK); 1818 return(1); 1819 } 1820 1821 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 1822 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 1823 1824 /* 1825 * Every I/O goes into the OOA queue for a 1826 * particular LUN, and stays there until completion. 1827 */ 1828#ifdef CTL_TIME_IO 1829 if (TAILQ_EMPTY(&lun->ooa_queue)) 1830 lun->idle_time += getsbinuptime() - lun->last_busy; 1831#endif 1832 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1833 1834 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1835 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1836 ooa_links))) { 1837 case CTL_ACTION_BLOCK: 1838 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1839 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1840 blocked_links); 1841 mtx_unlock(&lun->lun_lock); 1842 break; 1843 case CTL_ACTION_PASS: 1844 case CTL_ACTION_SKIP: 1845 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1846 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1847 ctl_enqueue_rtr((union ctl_io *)ctsio); 1848 mtx_unlock(&lun->lun_lock); 1849 } else { 1850 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 1851 mtx_unlock(&lun->lun_lock); 1852 1853 /* send msg back to other side */ 1854 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1855 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1856 msg_info.hdr.msg_type = CTL_MSG_R2R; 1857 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1858 sizeof(msg_info.hdr), M_WAITOK); 1859 } 1860 break; 1861 case CTL_ACTION_OVERLAP: 1862 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1863 mtx_unlock(&lun->lun_lock); 1864 retval = 1; 1865 1866 ctl_set_overlapped_cmd(ctsio); 1867 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1868 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1869 msg_info.hdr.serializing_sc = NULL; 1870 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1871 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1872 sizeof(msg_info.scsi), M_WAITOK); 1873 break; 1874 case CTL_ACTION_OVERLAP_TAG: 1875 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1876 mtx_unlock(&lun->lun_lock); 1877 retval = 1; 1878 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 1879 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1880 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1881 msg_info.hdr.serializing_sc = NULL; 1882 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1883 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1884 sizeof(msg_info.scsi), M_WAITOK); 1885 break; 1886 case CTL_ACTION_ERROR: 1887 default: 1888 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1889 mtx_unlock(&lun->lun_lock); 1890 retval = 1; 1891 1892 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 1893 /*retry_count*/ 0); 1894 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1895 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1896 msg_info.hdr.serializing_sc = NULL; 1897 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1898 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1899 sizeof(msg_info.scsi), M_WAITOK); 1900 break; 1901 } 1902 return (retval); 1903} 1904 1905/* 1906 * Returns 0 for success, errno for failure. 1907 */ 1908static int 1909ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1910 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1911{ 1912 union ctl_io *io; 1913 int retval; 1914 1915 retval = 0; 1916 1917 mtx_lock(&lun->lun_lock); 1918 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 1919 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 1920 ooa_links)) { 1921 struct ctl_ooa_entry *entry; 1922 1923 /* 1924 * If we've got more than we can fit, just count the 1925 * remaining entries. 1926 */ 1927 if (*cur_fill_num >= ooa_hdr->alloc_num) 1928 continue; 1929 1930 entry = &kern_entries[*cur_fill_num]; 1931 1932 entry->tag_num = io->scsiio.tag_num; 1933 entry->lun_num = lun->lun; 1934#ifdef CTL_TIME_IO 1935 entry->start_bt = io->io_hdr.start_bt; 1936#endif 1937 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 1938 entry->cdb_len = io->scsiio.cdb_len; 1939 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 1940 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 1941 1942 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 1943 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 1944 1945 if (io->io_hdr.flags & CTL_FLAG_ABORT) 1946 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 1947 1948 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 1949 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 1950 1951 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 1952 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 1953 } 1954 mtx_unlock(&lun->lun_lock); 1955 1956 return (retval); 1957} 1958 1959static void * 1960ctl_copyin_alloc(void *user_addr, int len, char *error_str, 1961 size_t error_str_len) 1962{ 1963 void *kptr; 1964 1965 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 1966 1967 if (copyin(user_addr, kptr, len) != 0) { 1968 snprintf(error_str, error_str_len, "Error copying %d bytes " 1969 "from user address %p to kernel address %p", len, 1970 user_addr, kptr); 1971 free(kptr, M_CTL); 1972 return (NULL); 1973 } 1974 1975 return (kptr); 1976} 1977 1978static void 1979ctl_free_args(int num_args, struct ctl_be_arg *args) 1980{ 1981 int i; 1982 1983 if (args == NULL) 1984 return; 1985 1986 for (i = 0; i < num_args; i++) { 1987 free(args[i].kname, M_CTL); 1988 free(args[i].kvalue, M_CTL); 1989 } 1990 1991 free(args, M_CTL); 1992} 1993 1994static struct ctl_be_arg * 1995ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 1996 char *error_str, size_t error_str_len) 1997{ 1998 struct ctl_be_arg *args; 1999 int i; 2000 2001 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2002 error_str, error_str_len); 2003 2004 if (args == NULL) 2005 goto bailout; 2006 2007 for (i = 0; i < num_args; i++) { 2008 args[i].kname = NULL; 2009 args[i].kvalue = NULL; 2010 } 2011 2012 for (i = 0; i < num_args; i++) { 2013 uint8_t *tmpptr; 2014 2015 args[i].kname = ctl_copyin_alloc(args[i].name, 2016 args[i].namelen, error_str, error_str_len); 2017 if (args[i].kname == NULL) 2018 goto bailout; 2019 2020 if (args[i].kname[args[i].namelen - 1] != '\0') { 2021 snprintf(error_str, error_str_len, "Argument %d " 2022 "name is not NUL-terminated", i); 2023 goto bailout; 2024 } 2025 2026 if (args[i].flags & CTL_BEARG_RD) { 2027 tmpptr = ctl_copyin_alloc(args[i].value, 2028 args[i].vallen, error_str, error_str_len); 2029 if (tmpptr == NULL) 2030 goto bailout; 2031 if ((args[i].flags & CTL_BEARG_ASCII) 2032 && (tmpptr[args[i].vallen - 1] != '\0')) { 2033 snprintf(error_str, error_str_len, "Argument " 2034 "%d value is not NUL-terminated", i); 2035 goto bailout; 2036 } 2037 args[i].kvalue = tmpptr; 2038 } else { 2039 args[i].kvalue = malloc(args[i].vallen, 2040 M_CTL, M_WAITOK | M_ZERO); 2041 } 2042 } 2043 2044 return (args); 2045bailout: 2046 2047 ctl_free_args(num_args, args); 2048 2049 return (NULL); 2050} 2051 2052static void 2053ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2054{ 2055 int i; 2056 2057 for (i = 0; i < num_args; i++) { 2058 if (args[i].flags & CTL_BEARG_WR) 2059 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2060 } 2061} 2062 2063/* 2064 * Escape characters that are illegal or not recommended in XML. 2065 */ 2066int 2067ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2068{ 2069 char *end = str + size; 2070 int retval; 2071 2072 retval = 0; 2073 2074 for (; *str && str < end; str++) { 2075 switch (*str) { 2076 case '&': 2077 retval = sbuf_printf(sb, "&"); 2078 break; 2079 case '>': 2080 retval = sbuf_printf(sb, ">"); 2081 break; 2082 case '<': 2083 retval = sbuf_printf(sb, "<"); 2084 break; 2085 default: 2086 retval = sbuf_putc(sb, *str); 2087 break; 2088 } 2089 2090 if (retval != 0) 2091 break; 2092 2093 } 2094 2095 return (retval); 2096} 2097 2098static void 2099ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2100{ 2101 struct scsi_vpd_id_descriptor *desc; 2102 int i; 2103 2104 if (id == NULL || id->len < 4) 2105 return; 2106 desc = (struct scsi_vpd_id_descriptor *)id->data; 2107 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2108 case SVPD_ID_TYPE_T10: 2109 sbuf_printf(sb, "t10."); 2110 break; 2111 case SVPD_ID_TYPE_EUI64: 2112 sbuf_printf(sb, "eui."); 2113 break; 2114 case SVPD_ID_TYPE_NAA: 2115 sbuf_printf(sb, "naa."); 2116 break; 2117 case SVPD_ID_TYPE_SCSI_NAME: 2118 break; 2119 } 2120 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2121 case SVPD_ID_CODESET_BINARY: 2122 for (i = 0; i < desc->length; i++) 2123 sbuf_printf(sb, "%02x", desc->identifier[i]); 2124 break; 2125 case SVPD_ID_CODESET_ASCII: 2126 sbuf_printf(sb, "%.*s", (int)desc->length, 2127 (char *)desc->identifier); 2128 break; 2129 case SVPD_ID_CODESET_UTF8: 2130 sbuf_printf(sb, "%s", (char *)desc->identifier); 2131 break; 2132 } 2133} 2134 2135static int 2136ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2137 struct thread *td) 2138{ 2139 struct ctl_softc *softc; 2140 struct ctl_lun *lun; 2141 int retval; 2142 2143 softc = control_softc; 2144 2145 retval = 0; 2146 2147 switch (cmd) { 2148 case CTL_IO: 2149 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2150 break; 2151 case CTL_ENABLE_PORT: 2152 case CTL_DISABLE_PORT: 2153 case CTL_SET_PORT_WWNS: { 2154 struct ctl_port *port; 2155 struct ctl_port_entry *entry; 2156 2157 entry = (struct ctl_port_entry *)addr; 2158 2159 mtx_lock(&softc->ctl_lock); 2160 STAILQ_FOREACH(port, &softc->port_list, links) { 2161 int action, done; 2162 2163 if (port->targ_port < softc->port_min || 2164 port->targ_port >= softc->port_max) 2165 continue; 2166 2167 action = 0; 2168 done = 0; 2169 if ((entry->port_type == CTL_PORT_NONE) 2170 && (entry->targ_port == port->targ_port)) { 2171 /* 2172 * If the user only wants to enable or 2173 * disable or set WWNs on a specific port, 2174 * do the operation and we're done. 2175 */ 2176 action = 1; 2177 done = 1; 2178 } else if (entry->port_type & port->port_type) { 2179 /* 2180 * Compare the user's type mask with the 2181 * particular frontend type to see if we 2182 * have a match. 2183 */ 2184 action = 1; 2185 done = 0; 2186 2187 /* 2188 * Make sure the user isn't trying to set 2189 * WWNs on multiple ports at the same time. 2190 */ 2191 if (cmd == CTL_SET_PORT_WWNS) { 2192 printf("%s: Can't set WWNs on " 2193 "multiple ports\n", __func__); 2194 retval = EINVAL; 2195 break; 2196 } 2197 } 2198 if (action == 0) 2199 continue; 2200 2201 /* 2202 * XXX KDM we have to drop the lock here, because 2203 * the online/offline operations can potentially 2204 * block. We need to reference count the frontends 2205 * so they can't go away, 2206 */ 2207 if (cmd == CTL_ENABLE_PORT) { 2208 mtx_unlock(&softc->ctl_lock); 2209 ctl_port_online(port); 2210 mtx_lock(&softc->ctl_lock); 2211 } else if (cmd == CTL_DISABLE_PORT) { 2212 mtx_unlock(&softc->ctl_lock); 2213 ctl_port_offline(port); 2214 mtx_lock(&softc->ctl_lock); 2215 } else if (cmd == CTL_SET_PORT_WWNS) { 2216 ctl_port_set_wwns(port, 2217 (entry->flags & CTL_PORT_WWNN_VALID) ? 2218 1 : 0, entry->wwnn, 2219 (entry->flags & CTL_PORT_WWPN_VALID) ? 2220 1 : 0, entry->wwpn); 2221 } 2222 if (done != 0) 2223 break; 2224 } 2225 mtx_unlock(&softc->ctl_lock); 2226 break; 2227 } 2228 case CTL_GET_PORT_LIST: { 2229 struct ctl_port *port; 2230 struct ctl_port_list *list; 2231 int i; 2232 2233 list = (struct ctl_port_list *)addr; 2234 2235 if (list->alloc_len != (list->alloc_num * 2236 sizeof(struct ctl_port_entry))) { 2237 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2238 "alloc_num %u * sizeof(struct ctl_port_entry) " 2239 "%zu\n", __func__, list->alloc_len, 2240 list->alloc_num, sizeof(struct ctl_port_entry)); 2241 retval = EINVAL; 2242 break; 2243 } 2244 list->fill_len = 0; 2245 list->fill_num = 0; 2246 list->dropped_num = 0; 2247 i = 0; 2248 mtx_lock(&softc->ctl_lock); 2249 STAILQ_FOREACH(port, &softc->port_list, links) { 2250 struct ctl_port_entry entry, *list_entry; 2251 2252 if (list->fill_num >= list->alloc_num) { 2253 list->dropped_num++; 2254 continue; 2255 } 2256 2257 entry.port_type = port->port_type; 2258 strlcpy(entry.port_name, port->port_name, 2259 sizeof(entry.port_name)); 2260 entry.targ_port = port->targ_port; 2261 entry.physical_port = port->physical_port; 2262 entry.virtual_port = port->virtual_port; 2263 entry.wwnn = port->wwnn; 2264 entry.wwpn = port->wwpn; 2265 if (port->status & CTL_PORT_STATUS_ONLINE) 2266 entry.online = 1; 2267 else 2268 entry.online = 0; 2269 2270 list_entry = &list->entries[i]; 2271 2272 retval = copyout(&entry, list_entry, sizeof(entry)); 2273 if (retval != 0) { 2274 printf("%s: CTL_GET_PORT_LIST: copyout " 2275 "returned %d\n", __func__, retval); 2276 break; 2277 } 2278 i++; 2279 list->fill_num++; 2280 list->fill_len += sizeof(entry); 2281 } 2282 mtx_unlock(&softc->ctl_lock); 2283 2284 /* 2285 * If this is non-zero, we had a copyout fault, so there's 2286 * probably no point in attempting to set the status inside 2287 * the structure. 2288 */ 2289 if (retval != 0) 2290 break; 2291 2292 if (list->dropped_num > 0) 2293 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2294 else 2295 list->status = CTL_PORT_LIST_OK; 2296 break; 2297 } 2298 case CTL_DUMP_OOA: { 2299 union ctl_io *io; 2300 char printbuf[128]; 2301 struct sbuf sb; 2302 2303 mtx_lock(&softc->ctl_lock); 2304 printf("Dumping OOA queues:\n"); 2305 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2306 mtx_lock(&lun->lun_lock); 2307 for (io = (union ctl_io *)TAILQ_FIRST( 2308 &lun->ooa_queue); io != NULL; 2309 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2310 ooa_links)) { 2311 sbuf_new(&sb, printbuf, sizeof(printbuf), 2312 SBUF_FIXEDLEN); 2313 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2314 (intmax_t)lun->lun, 2315 io->scsiio.tag_num, 2316 (io->io_hdr.flags & 2317 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2318 (io->io_hdr.flags & 2319 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2320 (io->io_hdr.flags & 2321 CTL_FLAG_ABORT) ? " ABORT" : "", 2322 (io->io_hdr.flags & 2323 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2324 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2325 sbuf_finish(&sb); 2326 printf("%s\n", sbuf_data(&sb)); 2327 } 2328 mtx_unlock(&lun->lun_lock); 2329 } 2330 printf("OOA queues dump done\n"); 2331 mtx_unlock(&softc->ctl_lock); 2332 break; 2333 } 2334 case CTL_GET_OOA: { 2335 struct ctl_ooa *ooa_hdr; 2336 struct ctl_ooa_entry *entries; 2337 uint32_t cur_fill_num; 2338 2339 ooa_hdr = (struct ctl_ooa *)addr; 2340 2341 if ((ooa_hdr->alloc_len == 0) 2342 || (ooa_hdr->alloc_num == 0)) { 2343 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2344 "must be non-zero\n", __func__, 2345 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2346 retval = EINVAL; 2347 break; 2348 } 2349 2350 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2351 sizeof(struct ctl_ooa_entry))) { 2352 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2353 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2354 __func__, ooa_hdr->alloc_len, 2355 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2356 retval = EINVAL; 2357 break; 2358 } 2359 2360 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2361 if (entries == NULL) { 2362 printf("%s: could not allocate %d bytes for OOA " 2363 "dump\n", __func__, ooa_hdr->alloc_len); 2364 retval = ENOMEM; 2365 break; 2366 } 2367 2368 mtx_lock(&softc->ctl_lock); 2369 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2370 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2371 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2372 mtx_unlock(&softc->ctl_lock); 2373 free(entries, M_CTL); 2374 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2375 __func__, (uintmax_t)ooa_hdr->lun_num); 2376 retval = EINVAL; 2377 break; 2378 } 2379 2380 cur_fill_num = 0; 2381 2382 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2383 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2384 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2385 ooa_hdr, entries); 2386 if (retval != 0) 2387 break; 2388 } 2389 if (retval != 0) { 2390 mtx_unlock(&softc->ctl_lock); 2391 free(entries, M_CTL); 2392 break; 2393 } 2394 } else { 2395 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2396 2397 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2398 entries); 2399 } 2400 mtx_unlock(&softc->ctl_lock); 2401 2402 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2403 ooa_hdr->fill_len = ooa_hdr->fill_num * 2404 sizeof(struct ctl_ooa_entry); 2405 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2406 if (retval != 0) { 2407 printf("%s: error copying out %d bytes for OOA dump\n", 2408 __func__, ooa_hdr->fill_len); 2409 } 2410 2411 getbintime(&ooa_hdr->cur_bt); 2412 2413 if (cur_fill_num > ooa_hdr->alloc_num) { 2414 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2415 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2416 } else { 2417 ooa_hdr->dropped_num = 0; 2418 ooa_hdr->status = CTL_OOA_OK; 2419 } 2420 2421 free(entries, M_CTL); 2422 break; 2423 } 2424 case CTL_CHECK_OOA: { 2425 union ctl_io *io; 2426 struct ctl_ooa_info *ooa_info; 2427 2428 2429 ooa_info = (struct ctl_ooa_info *)addr; 2430 2431 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2432 ooa_info->status = CTL_OOA_INVALID_LUN; 2433 break; 2434 } 2435 mtx_lock(&softc->ctl_lock); 2436 lun = softc->ctl_luns[ooa_info->lun_id]; 2437 if (lun == NULL) { 2438 mtx_unlock(&softc->ctl_lock); 2439 ooa_info->status = CTL_OOA_INVALID_LUN; 2440 break; 2441 } 2442 mtx_lock(&lun->lun_lock); 2443 mtx_unlock(&softc->ctl_lock); 2444 ooa_info->num_entries = 0; 2445 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2446 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2447 &io->io_hdr, ooa_links)) { 2448 ooa_info->num_entries++; 2449 } 2450 mtx_unlock(&lun->lun_lock); 2451 2452 ooa_info->status = CTL_OOA_SUCCESS; 2453 2454 break; 2455 } 2456 case CTL_DELAY_IO: { 2457 struct ctl_io_delay_info *delay_info; 2458 2459 delay_info = (struct ctl_io_delay_info *)addr; 2460 2461#ifdef CTL_IO_DELAY 2462 mtx_lock(&softc->ctl_lock); 2463 2464 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2465 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2466 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2467 } else { 2468 lun = softc->ctl_luns[delay_info->lun_id]; 2469 mtx_lock(&lun->lun_lock); 2470 2471 delay_info->status = CTL_DELAY_STATUS_OK; 2472 2473 switch (delay_info->delay_type) { 2474 case CTL_DELAY_TYPE_CONT: 2475 break; 2476 case CTL_DELAY_TYPE_ONESHOT: 2477 break; 2478 default: 2479 delay_info->status = 2480 CTL_DELAY_STATUS_INVALID_TYPE; 2481 break; 2482 } 2483 2484 switch (delay_info->delay_loc) { 2485 case CTL_DELAY_LOC_DATAMOVE: 2486 lun->delay_info.datamove_type = 2487 delay_info->delay_type; 2488 lun->delay_info.datamove_delay = 2489 delay_info->delay_secs; 2490 break; 2491 case CTL_DELAY_LOC_DONE: 2492 lun->delay_info.done_type = 2493 delay_info->delay_type; 2494 lun->delay_info.done_delay = 2495 delay_info->delay_secs; 2496 break; 2497 default: 2498 delay_info->status = 2499 CTL_DELAY_STATUS_INVALID_LOC; 2500 break; 2501 } 2502 mtx_unlock(&lun->lun_lock); 2503 } 2504 2505 mtx_unlock(&softc->ctl_lock); 2506#else 2507 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2508#endif /* CTL_IO_DELAY */ 2509 break; 2510 } 2511 case CTL_REALSYNC_SET: { 2512 int *syncstate; 2513 2514 syncstate = (int *)addr; 2515 2516 mtx_lock(&softc->ctl_lock); 2517 switch (*syncstate) { 2518 case 0: 2519 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2520 break; 2521 case 1: 2522 softc->flags |= CTL_FLAG_REAL_SYNC; 2523 break; 2524 default: 2525 retval = EINVAL; 2526 break; 2527 } 2528 mtx_unlock(&softc->ctl_lock); 2529 break; 2530 } 2531 case CTL_REALSYNC_GET: { 2532 int *syncstate; 2533 2534 syncstate = (int*)addr; 2535 2536 mtx_lock(&softc->ctl_lock); 2537 if (softc->flags & CTL_FLAG_REAL_SYNC) 2538 *syncstate = 1; 2539 else 2540 *syncstate = 0; 2541 mtx_unlock(&softc->ctl_lock); 2542 2543 break; 2544 } 2545 case CTL_SETSYNC: 2546 case CTL_GETSYNC: { 2547 struct ctl_sync_info *sync_info; 2548 2549 sync_info = (struct ctl_sync_info *)addr; 2550 2551 mtx_lock(&softc->ctl_lock); 2552 lun = softc->ctl_luns[sync_info->lun_id]; 2553 if (lun == NULL) { 2554 mtx_unlock(&softc->ctl_lock); 2555 sync_info->status = CTL_GS_SYNC_NO_LUN; 2556 break; 2557 } 2558 /* 2559 * Get or set the sync interval. We're not bounds checking 2560 * in the set case, hopefully the user won't do something 2561 * silly. 2562 */ 2563 mtx_lock(&lun->lun_lock); 2564 mtx_unlock(&softc->ctl_lock); 2565 if (cmd == CTL_GETSYNC) 2566 sync_info->sync_interval = lun->sync_interval; 2567 else 2568 lun->sync_interval = sync_info->sync_interval; 2569 mtx_unlock(&lun->lun_lock); 2570 2571 sync_info->status = CTL_GS_SYNC_OK; 2572 2573 break; 2574 } 2575 case CTL_GETSTATS: { 2576 struct ctl_stats *stats; 2577 int i; 2578 2579 stats = (struct ctl_stats *)addr; 2580 2581 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2582 stats->alloc_len) { 2583 stats->status = CTL_SS_NEED_MORE_SPACE; 2584 stats->num_luns = softc->num_luns; 2585 break; 2586 } 2587 /* 2588 * XXX KDM no locking here. If the LUN list changes, 2589 * things can blow up. 2590 */ 2591 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2592 i++, lun = STAILQ_NEXT(lun, links)) { 2593 retval = copyout(&lun->stats, &stats->lun_stats[i], 2594 sizeof(lun->stats)); 2595 if (retval != 0) 2596 break; 2597 } 2598 stats->num_luns = softc->num_luns; 2599 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2600 softc->num_luns; 2601 stats->status = CTL_SS_OK; 2602#ifdef CTL_TIME_IO 2603 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2604#else 2605 stats->flags = CTL_STATS_FLAG_NONE; 2606#endif 2607 getnanouptime(&stats->timestamp); 2608 break; 2609 } 2610 case CTL_ERROR_INJECT: { 2611 struct ctl_error_desc *err_desc, *new_err_desc; 2612 2613 err_desc = (struct ctl_error_desc *)addr; 2614 2615 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2616 M_WAITOK | M_ZERO); 2617 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2618 2619 mtx_lock(&softc->ctl_lock); 2620 lun = softc->ctl_luns[err_desc->lun_id]; 2621 if (lun == NULL) { 2622 mtx_unlock(&softc->ctl_lock); 2623 free(new_err_desc, M_CTL); 2624 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2625 __func__, (uintmax_t)err_desc->lun_id); 2626 retval = EINVAL; 2627 break; 2628 } 2629 mtx_lock(&lun->lun_lock); 2630 mtx_unlock(&softc->ctl_lock); 2631 2632 /* 2633 * We could do some checking here to verify the validity 2634 * of the request, but given the complexity of error 2635 * injection requests, the checking logic would be fairly 2636 * complex. 2637 * 2638 * For now, if the request is invalid, it just won't get 2639 * executed and might get deleted. 2640 */ 2641 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2642 2643 /* 2644 * XXX KDM check to make sure the serial number is unique, 2645 * in case we somehow manage to wrap. That shouldn't 2646 * happen for a very long time, but it's the right thing to 2647 * do. 2648 */ 2649 new_err_desc->serial = lun->error_serial; 2650 err_desc->serial = lun->error_serial; 2651 lun->error_serial++; 2652 2653 mtx_unlock(&lun->lun_lock); 2654 break; 2655 } 2656 case CTL_ERROR_INJECT_DELETE: { 2657 struct ctl_error_desc *delete_desc, *desc, *desc2; 2658 int delete_done; 2659 2660 delete_desc = (struct ctl_error_desc *)addr; 2661 delete_done = 0; 2662 2663 mtx_lock(&softc->ctl_lock); 2664 lun = softc->ctl_luns[delete_desc->lun_id]; 2665 if (lun == NULL) { 2666 mtx_unlock(&softc->ctl_lock); 2667 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2668 __func__, (uintmax_t)delete_desc->lun_id); 2669 retval = EINVAL; 2670 break; 2671 } 2672 mtx_lock(&lun->lun_lock); 2673 mtx_unlock(&softc->ctl_lock); 2674 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2675 if (desc->serial != delete_desc->serial) 2676 continue; 2677 2678 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2679 links); 2680 free(desc, M_CTL); 2681 delete_done = 1; 2682 } 2683 mtx_unlock(&lun->lun_lock); 2684 if (delete_done == 0) { 2685 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2686 "error serial %ju on LUN %u\n", __func__, 2687 delete_desc->serial, delete_desc->lun_id); 2688 retval = EINVAL; 2689 break; 2690 } 2691 break; 2692 } 2693 case CTL_DUMP_STRUCTS: { 2694 int i, j, k; 2695 struct ctl_port *port; 2696 struct ctl_frontend *fe; 2697 2698 mtx_lock(&softc->ctl_lock); 2699 printf("CTL Persistent Reservation information start:\n"); 2700 for (i = 0; i < CTL_MAX_LUNS; i++) { 2701 lun = softc->ctl_luns[i]; 2702 2703 if ((lun == NULL) 2704 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2705 continue; 2706 2707 for (j = 0; j < CTL_MAX_PORTS; j++) { 2708 if (lun->pr_keys[j] == NULL) 2709 continue; 2710 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2711 if (lun->pr_keys[j][k] == 0) 2712 continue; 2713 printf(" LUN %d port %d iid %d key " 2714 "%#jx\n", i, j, k, 2715 (uintmax_t)lun->pr_keys[j][k]); 2716 } 2717 } 2718 } 2719 printf("CTL Persistent Reservation information end\n"); 2720 printf("CTL Ports:\n"); 2721 STAILQ_FOREACH(port, &softc->port_list, links) { 2722 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2723 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2724 port->frontend->name, port->port_type, 2725 port->physical_port, port->virtual_port, 2726 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2727 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2728 if (port->wwpn_iid[j].in_use == 0 && 2729 port->wwpn_iid[j].wwpn == 0 && 2730 port->wwpn_iid[j].name == NULL) 2731 continue; 2732 2733 printf(" iid %u use %d WWPN %#jx '%s'\n", 2734 j, port->wwpn_iid[j].in_use, 2735 (uintmax_t)port->wwpn_iid[j].wwpn, 2736 port->wwpn_iid[j].name); 2737 } 2738 } 2739 printf("CTL Port information end\n"); 2740 mtx_unlock(&softc->ctl_lock); 2741 /* 2742 * XXX KDM calling this without a lock. We'd likely want 2743 * to drop the lock before calling the frontend's dump 2744 * routine anyway. 2745 */ 2746 printf("CTL Frontends:\n"); 2747 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2748 printf(" Frontend '%s'\n", fe->name); 2749 if (fe->fe_dump != NULL) 2750 fe->fe_dump(); 2751 } 2752 printf("CTL Frontend information end\n"); 2753 break; 2754 } 2755 case CTL_LUN_REQ: { 2756 struct ctl_lun_req *lun_req; 2757 struct ctl_backend_driver *backend; 2758 2759 lun_req = (struct ctl_lun_req *)addr; 2760 2761 backend = ctl_backend_find(lun_req->backend); 2762 if (backend == NULL) { 2763 lun_req->status = CTL_LUN_ERROR; 2764 snprintf(lun_req->error_str, 2765 sizeof(lun_req->error_str), 2766 "Backend \"%s\" not found.", 2767 lun_req->backend); 2768 break; 2769 } 2770 if (lun_req->num_be_args > 0) { 2771 lun_req->kern_be_args = ctl_copyin_args( 2772 lun_req->num_be_args, 2773 lun_req->be_args, 2774 lun_req->error_str, 2775 sizeof(lun_req->error_str)); 2776 if (lun_req->kern_be_args == NULL) { 2777 lun_req->status = CTL_LUN_ERROR; 2778 break; 2779 } 2780 } 2781 2782 retval = backend->ioctl(dev, cmd, addr, flag, td); 2783 2784 if (lun_req->num_be_args > 0) { 2785 ctl_copyout_args(lun_req->num_be_args, 2786 lun_req->kern_be_args); 2787 ctl_free_args(lun_req->num_be_args, 2788 lun_req->kern_be_args); 2789 } 2790 break; 2791 } 2792 case CTL_LUN_LIST: { 2793 struct sbuf *sb; 2794 struct ctl_lun_list *list; 2795 struct ctl_option *opt; 2796 2797 list = (struct ctl_lun_list *)addr; 2798 2799 /* 2800 * Allocate a fixed length sbuf here, based on the length 2801 * of the user's buffer. We could allocate an auto-extending 2802 * buffer, and then tell the user how much larger our 2803 * amount of data is than his buffer, but that presents 2804 * some problems: 2805 * 2806 * 1. The sbuf(9) routines use a blocking malloc, and so 2807 * we can't hold a lock while calling them with an 2808 * auto-extending buffer. 2809 * 2810 * 2. There is not currently a LUN reference counting 2811 * mechanism, outside of outstanding transactions on 2812 * the LUN's OOA queue. So a LUN could go away on us 2813 * while we're getting the LUN number, backend-specific 2814 * information, etc. Thus, given the way things 2815 * currently work, we need to hold the CTL lock while 2816 * grabbing LUN information. 2817 * 2818 * So, from the user's standpoint, the best thing to do is 2819 * allocate what he thinks is a reasonable buffer length, 2820 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 2821 * double the buffer length and try again. (And repeat 2822 * that until he succeeds.) 2823 */ 2824 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 2825 if (sb == NULL) { 2826 list->status = CTL_LUN_LIST_ERROR; 2827 snprintf(list->error_str, sizeof(list->error_str), 2828 "Unable to allocate %d bytes for LUN list", 2829 list->alloc_len); 2830 break; 2831 } 2832 2833 sbuf_printf(sb, "<ctllunlist>\n"); 2834 2835 mtx_lock(&softc->ctl_lock); 2836 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2837 mtx_lock(&lun->lun_lock); 2838 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 2839 (uintmax_t)lun->lun); 2840 2841 /* 2842 * Bail out as soon as we see that we've overfilled 2843 * the buffer. 2844 */ 2845 if (retval != 0) 2846 break; 2847 2848 retval = sbuf_printf(sb, "\t<backend_type>%s" 2849 "</backend_type>\n", 2850 (lun->backend == NULL) ? "none" : 2851 lun->backend->name); 2852 2853 if (retval != 0) 2854 break; 2855 2856 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 2857 lun->be_lun->lun_type); 2858 2859 if (retval != 0) 2860 break; 2861 2862 if (lun->backend == NULL) { 2863 retval = sbuf_printf(sb, "</lun>\n"); 2864 if (retval != 0) 2865 break; 2866 continue; 2867 } 2868 2869 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 2870 (lun->be_lun->maxlba > 0) ? 2871 lun->be_lun->maxlba + 1 : 0); 2872 2873 if (retval != 0) 2874 break; 2875 2876 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 2877 lun->be_lun->blocksize); 2878 2879 if (retval != 0) 2880 break; 2881 2882 retval = sbuf_printf(sb, "\t<serial_number>"); 2883 2884 if (retval != 0) 2885 break; 2886 2887 retval = ctl_sbuf_printf_esc(sb, 2888 lun->be_lun->serial_num, 2889 sizeof(lun->be_lun->serial_num)); 2890 2891 if (retval != 0) 2892 break; 2893 2894 retval = sbuf_printf(sb, "</serial_number>\n"); 2895 2896 if (retval != 0) 2897 break; 2898 2899 retval = sbuf_printf(sb, "\t<device_id>"); 2900 2901 if (retval != 0) 2902 break; 2903 2904 retval = ctl_sbuf_printf_esc(sb, 2905 lun->be_lun->device_id, 2906 sizeof(lun->be_lun->device_id)); 2907 2908 if (retval != 0) 2909 break; 2910 2911 retval = sbuf_printf(sb, "</device_id>\n"); 2912 2913 if (retval != 0) 2914 break; 2915 2916 if (lun->backend->lun_info != NULL) { 2917 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 2918 if (retval != 0) 2919 break; 2920 } 2921 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 2922 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 2923 opt->name, opt->value, opt->name); 2924 if (retval != 0) 2925 break; 2926 } 2927 2928 retval = sbuf_printf(sb, "</lun>\n"); 2929 2930 if (retval != 0) 2931 break; 2932 mtx_unlock(&lun->lun_lock); 2933 } 2934 if (lun != NULL) 2935 mtx_unlock(&lun->lun_lock); 2936 mtx_unlock(&softc->ctl_lock); 2937 2938 if ((retval != 0) 2939 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 2940 retval = 0; 2941 sbuf_delete(sb); 2942 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 2943 snprintf(list->error_str, sizeof(list->error_str), 2944 "Out of space, %d bytes is too small", 2945 list->alloc_len); 2946 break; 2947 } 2948 2949 sbuf_finish(sb); 2950 2951 retval = copyout(sbuf_data(sb), list->lun_xml, 2952 sbuf_len(sb) + 1); 2953 2954 list->fill_len = sbuf_len(sb) + 1; 2955 list->status = CTL_LUN_LIST_OK; 2956 sbuf_delete(sb); 2957 break; 2958 } 2959 case CTL_ISCSI: { 2960 struct ctl_iscsi *ci; 2961 struct ctl_frontend *fe; 2962 2963 ci = (struct ctl_iscsi *)addr; 2964 2965 fe = ctl_frontend_find("iscsi"); 2966 if (fe == NULL) { 2967 ci->status = CTL_ISCSI_ERROR; 2968 snprintf(ci->error_str, sizeof(ci->error_str), 2969 "Frontend \"iscsi\" not found."); 2970 break; 2971 } 2972 2973 retval = fe->ioctl(dev, cmd, addr, flag, td); 2974 break; 2975 } 2976 case CTL_PORT_REQ: { 2977 struct ctl_req *req; 2978 struct ctl_frontend *fe; 2979 2980 req = (struct ctl_req *)addr; 2981 2982 fe = ctl_frontend_find(req->driver); 2983 if (fe == NULL) { 2984 req->status = CTL_LUN_ERROR; 2985 snprintf(req->error_str, sizeof(req->error_str), 2986 "Frontend \"%s\" not found.", req->driver); 2987 break; 2988 } 2989 if (req->num_args > 0) { 2990 req->kern_args = ctl_copyin_args(req->num_args, 2991 req->args, req->error_str, sizeof(req->error_str)); 2992 if (req->kern_args == NULL) { 2993 req->status = CTL_LUN_ERROR; 2994 break; 2995 } 2996 } 2997 2998 if (fe->ioctl) 2999 retval = fe->ioctl(dev, cmd, addr, flag, td); 3000 else 3001 retval = ENODEV; 3002 3003 if (req->num_args > 0) { 3004 ctl_copyout_args(req->num_args, req->kern_args); 3005 ctl_free_args(req->num_args, req->kern_args); 3006 } 3007 break; 3008 } 3009 case CTL_PORT_LIST: { 3010 struct sbuf *sb; 3011 struct ctl_port *port; 3012 struct ctl_lun_list *list; 3013 struct ctl_option *opt; 3014 int j; 3015 uint32_t plun; 3016 3017 list = (struct ctl_lun_list *)addr; 3018 3019 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3020 if (sb == NULL) { 3021 list->status = CTL_LUN_LIST_ERROR; 3022 snprintf(list->error_str, sizeof(list->error_str), 3023 "Unable to allocate %d bytes for LUN list", 3024 list->alloc_len); 3025 break; 3026 } 3027 3028 sbuf_printf(sb, "<ctlportlist>\n"); 3029 3030 mtx_lock(&softc->ctl_lock); 3031 STAILQ_FOREACH(port, &softc->port_list, links) { 3032 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3033 (uintmax_t)port->targ_port); 3034 3035 /* 3036 * Bail out as soon as we see that we've overfilled 3037 * the buffer. 3038 */ 3039 if (retval != 0) 3040 break; 3041 3042 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3043 "</frontend_type>\n", port->frontend->name); 3044 if (retval != 0) 3045 break; 3046 3047 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3048 port->port_type); 3049 if (retval != 0) 3050 break; 3051 3052 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3053 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3054 if (retval != 0) 3055 break; 3056 3057 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3058 port->port_name); 3059 if (retval != 0) 3060 break; 3061 3062 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3063 port->physical_port); 3064 if (retval != 0) 3065 break; 3066 3067 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3068 port->virtual_port); 3069 if (retval != 0) 3070 break; 3071 3072 if (port->target_devid != NULL) { 3073 sbuf_printf(sb, "\t<target>"); 3074 ctl_id_sbuf(port->target_devid, sb); 3075 sbuf_printf(sb, "</target>\n"); 3076 } 3077 3078 if (port->port_devid != NULL) { 3079 sbuf_printf(sb, "\t<port>"); 3080 ctl_id_sbuf(port->port_devid, sb); 3081 sbuf_printf(sb, "</port>\n"); 3082 } 3083 3084 if (port->port_info != NULL) { 3085 retval = port->port_info(port->onoff_arg, sb); 3086 if (retval != 0) 3087 break; 3088 } 3089 STAILQ_FOREACH(opt, &port->options, links) { 3090 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3091 opt->name, opt->value, opt->name); 3092 if (retval != 0) 3093 break; 3094 } 3095 3096 if (port->lun_map != NULL) { 3097 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3098 for (j = 0; j < CTL_MAX_LUNS; j++) { 3099 plun = ctl_lun_map_from_port(port, j); 3100 if (plun >= CTL_MAX_LUNS) 3101 continue; 3102 sbuf_printf(sb, 3103 "\t<lun id=\"%u\">%u</lun>\n", 3104 j, plun); 3105 } 3106 } 3107 3108 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3109 if (port->wwpn_iid[j].in_use == 0 || 3110 (port->wwpn_iid[j].wwpn == 0 && 3111 port->wwpn_iid[j].name == NULL)) 3112 continue; 3113 3114 if (port->wwpn_iid[j].name != NULL) 3115 retval = sbuf_printf(sb, 3116 "\t<initiator id=\"%u\">%s</initiator>\n", 3117 j, port->wwpn_iid[j].name); 3118 else 3119 retval = sbuf_printf(sb, 3120 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3121 j, port->wwpn_iid[j].wwpn); 3122 if (retval != 0) 3123 break; 3124 } 3125 if (retval != 0) 3126 break; 3127 3128 retval = sbuf_printf(sb, "</targ_port>\n"); 3129 if (retval != 0) 3130 break; 3131 } 3132 mtx_unlock(&softc->ctl_lock); 3133 3134 if ((retval != 0) 3135 || ((retval = sbuf_printf(sb, "</ctlportlist>\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_LUN_MAP: { 3156 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3157 struct ctl_port *port; 3158 3159 mtx_lock(&softc->ctl_lock); 3160 if (lm->port < softc->port_min || 3161 lm->port >= softc->port_max || 3162 (port = softc->ctl_ports[lm->port]) == NULL) { 3163 mtx_unlock(&softc->ctl_lock); 3164 return (ENXIO); 3165 } 3166 if (port->status & CTL_PORT_STATUS_ONLINE) { 3167 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3168 if (ctl_lun_map_to_port(port, lun->lun) >= 3169 CTL_MAX_LUNS) 3170 continue; 3171 mtx_lock(&lun->lun_lock); 3172 ctl_est_ua_port(lun, lm->port, -1, 3173 CTL_UA_LUN_CHANGE); 3174 mtx_unlock(&lun->lun_lock); 3175 } 3176 } 3177 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3178 if (lm->plun < CTL_MAX_LUNS) { 3179 if (lm->lun == UINT32_MAX) 3180 retval = ctl_lun_map_unset(port, lm->plun); 3181 else if (lm->lun < CTL_MAX_LUNS && 3182 softc->ctl_luns[lm->lun] != NULL) 3183 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3184 else 3185 return (ENXIO); 3186 } else if (lm->plun == UINT32_MAX) { 3187 if (lm->lun == UINT32_MAX) 3188 retval = ctl_lun_map_deinit(port); 3189 else 3190 retval = ctl_lun_map_init(port); 3191 } else 3192 return (ENXIO); 3193 if (port->status & CTL_PORT_STATUS_ONLINE) 3194 ctl_isc_announce_port(port); 3195 break; 3196 } 3197 default: { 3198 /* XXX KDM should we fix this? */ 3199#if 0 3200 struct ctl_backend_driver *backend; 3201 unsigned int type; 3202 int found; 3203 3204 found = 0; 3205 3206 /* 3207 * We encode the backend type as the ioctl type for backend 3208 * ioctls. So parse it out here, and then search for a 3209 * backend of this type. 3210 */ 3211 type = _IOC_TYPE(cmd); 3212 3213 STAILQ_FOREACH(backend, &softc->be_list, links) { 3214 if (backend->type == type) { 3215 found = 1; 3216 break; 3217 } 3218 } 3219 if (found == 0) { 3220 printf("ctl: unknown ioctl command %#lx or backend " 3221 "%d\n", cmd, type); 3222 retval = EINVAL; 3223 break; 3224 } 3225 retval = backend->ioctl(dev, cmd, addr, flag, td); 3226#endif 3227 retval = ENOTTY; 3228 break; 3229 } 3230 } 3231 return (retval); 3232} 3233 3234uint32_t 3235ctl_get_initindex(struct ctl_nexus *nexus) 3236{ 3237 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3238} 3239 3240int 3241ctl_lun_map_init(struct ctl_port *port) 3242{ 3243 struct ctl_softc *softc = control_softc; 3244 struct ctl_lun *lun; 3245 uint32_t i; 3246 3247 if (port->lun_map == NULL) 3248 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3249 M_CTL, M_NOWAIT); 3250 if (port->lun_map == NULL) 3251 return (ENOMEM); 3252 for (i = 0; i < CTL_MAX_LUNS; i++) 3253 port->lun_map[i] = UINT32_MAX; 3254 if (port->status & CTL_PORT_STATUS_ONLINE) { 3255 if (port->lun_disable != NULL) { 3256 STAILQ_FOREACH(lun, &softc->lun_list, links) 3257 port->lun_disable(port->targ_lun_arg, lun->lun); 3258 } 3259 ctl_isc_announce_port(port); 3260 } 3261 return (0); 3262} 3263 3264int 3265ctl_lun_map_deinit(struct ctl_port *port) 3266{ 3267 struct ctl_softc *softc = control_softc; 3268 struct ctl_lun *lun; 3269 3270 if (port->lun_map == NULL) 3271 return (0); 3272 free(port->lun_map, M_CTL); 3273 port->lun_map = NULL; 3274 if (port->status & CTL_PORT_STATUS_ONLINE) { 3275 if (port->lun_enable != NULL) { 3276 STAILQ_FOREACH(lun, &softc->lun_list, links) 3277 port->lun_enable(port->targ_lun_arg, lun->lun); 3278 } 3279 ctl_isc_announce_port(port); 3280 } 3281 return (0); 3282} 3283 3284int 3285ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3286{ 3287 int status; 3288 uint32_t old; 3289 3290 if (port->lun_map == NULL) { 3291 status = ctl_lun_map_init(port); 3292 if (status != 0) 3293 return (status); 3294 } 3295 old = port->lun_map[plun]; 3296 port->lun_map[plun] = glun; 3297 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3298 if (port->lun_enable != NULL) 3299 port->lun_enable(port->targ_lun_arg, plun); 3300 ctl_isc_announce_port(port); 3301 } 3302 return (0); 3303} 3304 3305int 3306ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3307{ 3308 uint32_t old; 3309 3310 if (port->lun_map == NULL) 3311 return (0); 3312 old = port->lun_map[plun]; 3313 port->lun_map[plun] = UINT32_MAX; 3314 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3315 if (port->lun_disable != NULL) 3316 port->lun_disable(port->targ_lun_arg, plun); 3317 ctl_isc_announce_port(port); 3318 } 3319 return (0); 3320} 3321 3322uint32_t 3323ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3324{ 3325 3326 if (port == NULL) 3327 return (UINT32_MAX); 3328 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3329 return (lun_id); 3330 return (port->lun_map[lun_id]); 3331} 3332 3333uint32_t 3334ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3335{ 3336 uint32_t i; 3337 3338 if (port == NULL) 3339 return (UINT32_MAX); 3340 if (port->lun_map == NULL) 3341 return (lun_id); 3342 for (i = 0; i < CTL_MAX_LUNS; i++) { 3343 if (port->lun_map[i] == lun_id) 3344 return (i); 3345 } 3346 return (UINT32_MAX); 3347} 3348 3349static struct ctl_port * 3350ctl_io_port(struct ctl_io_hdr *io_hdr) 3351{ 3352 3353 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3354} 3355 3356int 3357ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3358{ 3359 int i; 3360 3361 for (i = first; i < last; i++) { 3362 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3363 return (i); 3364 } 3365 return (-1); 3366} 3367 3368int 3369ctl_set_mask(uint32_t *mask, uint32_t bit) 3370{ 3371 uint32_t chunk, piece; 3372 3373 chunk = bit >> 5; 3374 piece = bit % (sizeof(uint32_t) * 8); 3375 3376 if ((mask[chunk] & (1 << piece)) != 0) 3377 return (-1); 3378 else 3379 mask[chunk] |= (1 << piece); 3380 3381 return (0); 3382} 3383 3384int 3385ctl_clear_mask(uint32_t *mask, uint32_t bit) 3386{ 3387 uint32_t chunk, piece; 3388 3389 chunk = bit >> 5; 3390 piece = bit % (sizeof(uint32_t) * 8); 3391 3392 if ((mask[chunk] & (1 << piece)) == 0) 3393 return (-1); 3394 else 3395 mask[chunk] &= ~(1 << piece); 3396 3397 return (0); 3398} 3399 3400int 3401ctl_is_set(uint32_t *mask, uint32_t bit) 3402{ 3403 uint32_t chunk, piece; 3404 3405 chunk = bit >> 5; 3406 piece = bit % (sizeof(uint32_t) * 8); 3407 3408 if ((mask[chunk] & (1 << piece)) == 0) 3409 return (0); 3410 else 3411 return (1); 3412} 3413 3414static uint64_t 3415ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3416{ 3417 uint64_t *t; 3418 3419 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3420 if (t == NULL) 3421 return (0); 3422 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3423} 3424 3425static void 3426ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3427{ 3428 uint64_t *t; 3429 3430 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3431 if (t == NULL) 3432 return; 3433 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3434} 3435 3436static void 3437ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3438{ 3439 uint64_t *p; 3440 u_int i; 3441 3442 i = residx/CTL_MAX_INIT_PER_PORT; 3443 if (lun->pr_keys[i] != NULL) 3444 return; 3445 mtx_unlock(&lun->lun_lock); 3446 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3447 M_WAITOK | M_ZERO); 3448 mtx_lock(&lun->lun_lock); 3449 if (lun->pr_keys[i] == NULL) 3450 lun->pr_keys[i] = p; 3451 else 3452 free(p, M_CTL); 3453} 3454 3455static void 3456ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3457{ 3458 uint64_t *t; 3459 3460 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3461 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3462 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3463} 3464 3465/* 3466 * ctl_softc, pool_name, total_ctl_io are passed in. 3467 * npool is passed out. 3468 */ 3469int 3470ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3471 uint32_t total_ctl_io, void **npool) 3472{ 3473#ifdef IO_POOLS 3474 struct ctl_io_pool *pool; 3475 3476 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3477 M_NOWAIT | M_ZERO); 3478 if (pool == NULL) 3479 return (ENOMEM); 3480 3481 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3482 pool->ctl_softc = ctl_softc; 3483 pool->zone = uma_zsecond_create(pool->name, NULL, 3484 NULL, NULL, NULL, ctl_softc->io_zone); 3485 /* uma_prealloc(pool->zone, total_ctl_io); */ 3486 3487 *npool = pool; 3488#else 3489 *npool = ctl_softc->io_zone; 3490#endif 3491 return (0); 3492} 3493 3494void 3495ctl_pool_free(struct ctl_io_pool *pool) 3496{ 3497 3498 if (pool == NULL) 3499 return; 3500 3501#ifdef IO_POOLS 3502 uma_zdestroy(pool->zone); 3503 free(pool, M_CTL); 3504#endif 3505} 3506 3507union ctl_io * 3508ctl_alloc_io(void *pool_ref) 3509{ 3510 union ctl_io *io; 3511#ifdef IO_POOLS 3512 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3513 3514 io = uma_zalloc(pool->zone, M_WAITOK); 3515#else 3516 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3517#endif 3518 if (io != NULL) 3519 io->io_hdr.pool = pool_ref; 3520 return (io); 3521} 3522 3523union ctl_io * 3524ctl_alloc_io_nowait(void *pool_ref) 3525{ 3526 union ctl_io *io; 3527#ifdef IO_POOLS 3528 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3529 3530 io = uma_zalloc(pool->zone, M_NOWAIT); 3531#else 3532 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3533#endif 3534 if (io != NULL) 3535 io->io_hdr.pool = pool_ref; 3536 return (io); 3537} 3538 3539void 3540ctl_free_io(union ctl_io *io) 3541{ 3542#ifdef IO_POOLS 3543 struct ctl_io_pool *pool; 3544#endif 3545 3546 if (io == NULL) 3547 return; 3548 3549#ifdef IO_POOLS 3550 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3551 uma_zfree(pool->zone, io); 3552#else 3553 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3554#endif 3555} 3556 3557void 3558ctl_zero_io(union ctl_io *io) 3559{ 3560 void *pool_ref; 3561 3562 if (io == NULL) 3563 return; 3564 3565 /* 3566 * May need to preserve linked list pointers at some point too. 3567 */ 3568 pool_ref = io->io_hdr.pool; 3569 memset(io, 0, sizeof(*io)); 3570 io->io_hdr.pool = pool_ref; 3571} 3572 3573/* 3574 * This routine is currently used for internal copies of ctl_ios that need 3575 * to persist for some reason after we've already returned status to the 3576 * FETD. (Thus the flag set.) 3577 * 3578 * XXX XXX 3579 * Note that this makes a blind copy of all fields in the ctl_io, except 3580 * for the pool reference. This includes any memory that has been 3581 * allocated! That memory will no longer be valid after done has been 3582 * called, so this would be VERY DANGEROUS for command that actually does 3583 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3584 * start and stop commands, which don't transfer any data, so this is not a 3585 * problem. If it is used for anything else, the caller would also need to 3586 * allocate data buffer space and this routine would need to be modified to 3587 * copy the data buffer(s) as well. 3588 */ 3589void 3590ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3591{ 3592 void *pool_ref; 3593 3594 if ((src == NULL) 3595 || (dest == NULL)) 3596 return; 3597 3598 /* 3599 * May need to preserve linked list pointers at some point too. 3600 */ 3601 pool_ref = dest->io_hdr.pool; 3602 3603 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3604 3605 dest->io_hdr.pool = pool_ref; 3606 /* 3607 * We need to know that this is an internal copy, and doesn't need 3608 * to get passed back to the FETD that allocated it. 3609 */ 3610 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3611} 3612 3613int 3614ctl_expand_number(const char *buf, uint64_t *num) 3615{ 3616 char *endptr; 3617 uint64_t number; 3618 unsigned shift; 3619 3620 number = strtoq(buf, &endptr, 0); 3621 3622 switch (tolower((unsigned char)*endptr)) { 3623 case 'e': 3624 shift = 60; 3625 break; 3626 case 'p': 3627 shift = 50; 3628 break; 3629 case 't': 3630 shift = 40; 3631 break; 3632 case 'g': 3633 shift = 30; 3634 break; 3635 case 'm': 3636 shift = 20; 3637 break; 3638 case 'k': 3639 shift = 10; 3640 break; 3641 case 'b': 3642 case '\0': /* No unit. */ 3643 *num = number; 3644 return (0); 3645 default: 3646 /* Unrecognized unit. */ 3647 return (-1); 3648 } 3649 3650 if ((number << shift) >> shift != number) { 3651 /* Overflow */ 3652 return (-1); 3653 } 3654 *num = number << shift; 3655 return (0); 3656} 3657 3658 3659/* 3660 * This routine could be used in the future to load default and/or saved 3661 * mode page parameters for a particuar lun. 3662 */ 3663static int 3664ctl_init_page_index(struct ctl_lun *lun) 3665{ 3666 int i; 3667 struct ctl_page_index *page_index; 3668 const char *value; 3669 uint64_t ival; 3670 3671 memcpy(&lun->mode_pages.index, page_index_template, 3672 sizeof(page_index_template)); 3673 3674 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3675 3676 page_index = &lun->mode_pages.index[i]; 3677 /* 3678 * If this is a disk-only mode page, there's no point in 3679 * setting it up. For some pages, we have to have some 3680 * basic information about the disk in order to calculate the 3681 * mode page data. 3682 */ 3683 if ((lun->be_lun->lun_type != T_DIRECT) 3684 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3685 continue; 3686 3687 switch (page_index->page_code & SMPH_PC_MASK) { 3688 case SMS_RW_ERROR_RECOVERY_PAGE: { 3689 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3690 panic("subpage is incorrect!"); 3691 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3692 &rw_er_page_default, 3693 sizeof(rw_er_page_default)); 3694 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3695 &rw_er_page_changeable, 3696 sizeof(rw_er_page_changeable)); 3697 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3698 &rw_er_page_default, 3699 sizeof(rw_er_page_default)); 3700 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3701 &rw_er_page_default, 3702 sizeof(rw_er_page_default)); 3703 page_index->page_data = 3704 (uint8_t *)lun->mode_pages.rw_er_page; 3705 break; 3706 } 3707 case SMS_FORMAT_DEVICE_PAGE: { 3708 struct scsi_format_page *format_page; 3709 3710 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3711 panic("subpage is incorrect!"); 3712 3713 /* 3714 * Sectors per track are set above. Bytes per 3715 * sector need to be set here on a per-LUN basis. 3716 */ 3717 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3718 &format_page_default, 3719 sizeof(format_page_default)); 3720 memcpy(&lun->mode_pages.format_page[ 3721 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3722 sizeof(format_page_changeable)); 3723 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3724 &format_page_default, 3725 sizeof(format_page_default)); 3726 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3727 &format_page_default, 3728 sizeof(format_page_default)); 3729 3730 format_page = &lun->mode_pages.format_page[ 3731 CTL_PAGE_CURRENT]; 3732 scsi_ulto2b(lun->be_lun->blocksize, 3733 format_page->bytes_per_sector); 3734 3735 format_page = &lun->mode_pages.format_page[ 3736 CTL_PAGE_DEFAULT]; 3737 scsi_ulto2b(lun->be_lun->blocksize, 3738 format_page->bytes_per_sector); 3739 3740 format_page = &lun->mode_pages.format_page[ 3741 CTL_PAGE_SAVED]; 3742 scsi_ulto2b(lun->be_lun->blocksize, 3743 format_page->bytes_per_sector); 3744 3745 page_index->page_data = 3746 (uint8_t *)lun->mode_pages.format_page; 3747 break; 3748 } 3749 case SMS_RIGID_DISK_PAGE: { 3750 struct scsi_rigid_disk_page *rigid_disk_page; 3751 uint32_t sectors_per_cylinder; 3752 uint64_t cylinders; 3753#ifndef __XSCALE__ 3754 int shift; 3755#endif /* !__XSCALE__ */ 3756 3757 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3758 panic("invalid subpage value %d", 3759 page_index->subpage); 3760 3761 /* 3762 * Rotation rate and sectors per track are set 3763 * above. We calculate the cylinders here based on 3764 * capacity. Due to the number of heads and 3765 * sectors per track we're using, smaller arrays 3766 * may turn out to have 0 cylinders. Linux and 3767 * FreeBSD don't pay attention to these mode pages 3768 * to figure out capacity, but Solaris does. It 3769 * seems to deal with 0 cylinders just fine, and 3770 * works out a fake geometry based on the capacity. 3771 */ 3772 memcpy(&lun->mode_pages.rigid_disk_page[ 3773 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3774 sizeof(rigid_disk_page_default)); 3775 memcpy(&lun->mode_pages.rigid_disk_page[ 3776 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3777 sizeof(rigid_disk_page_changeable)); 3778 3779 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3780 CTL_DEFAULT_HEADS; 3781 3782 /* 3783 * The divide method here will be more accurate, 3784 * probably, but results in floating point being 3785 * used in the kernel on i386 (__udivdi3()). On the 3786 * XScale, though, __udivdi3() is implemented in 3787 * software. 3788 * 3789 * The shift method for cylinder calculation is 3790 * accurate if sectors_per_cylinder is a power of 3791 * 2. Otherwise it might be slightly off -- you 3792 * might have a bit of a truncation problem. 3793 */ 3794#ifdef __XSCALE__ 3795 cylinders = (lun->be_lun->maxlba + 1) / 3796 sectors_per_cylinder; 3797#else 3798 for (shift = 31; shift > 0; shift--) { 3799 if (sectors_per_cylinder & (1 << shift)) 3800 break; 3801 } 3802 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3803#endif 3804 3805 /* 3806 * We've basically got 3 bytes, or 24 bits for the 3807 * cylinder size in the mode page. If we're over, 3808 * just round down to 2^24. 3809 */ 3810 if (cylinders > 0xffffff) 3811 cylinders = 0xffffff; 3812 3813 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3814 CTL_PAGE_DEFAULT]; 3815 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3816 3817 if ((value = ctl_get_opt(&lun->be_lun->options, 3818 "rpm")) != NULL) { 3819 scsi_ulto2b(strtol(value, NULL, 0), 3820 rigid_disk_page->rotation_rate); 3821 } 3822 3823 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 3824 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3825 sizeof(rigid_disk_page_default)); 3826 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 3827 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3828 sizeof(rigid_disk_page_default)); 3829 3830 page_index->page_data = 3831 (uint8_t *)lun->mode_pages.rigid_disk_page; 3832 break; 3833 } 3834 case SMS_CACHING_PAGE: { 3835 struct scsi_caching_page *caching_page; 3836 3837 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3838 panic("invalid subpage value %d", 3839 page_index->subpage); 3840 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3841 &caching_page_default, 3842 sizeof(caching_page_default)); 3843 memcpy(&lun->mode_pages.caching_page[ 3844 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3845 sizeof(caching_page_changeable)); 3846 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3847 &caching_page_default, 3848 sizeof(caching_page_default)); 3849 caching_page = &lun->mode_pages.caching_page[ 3850 CTL_PAGE_SAVED]; 3851 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 3852 if (value != NULL && strcmp(value, "off") == 0) 3853 caching_page->flags1 &= ~SCP_WCE; 3854 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 3855 if (value != NULL && strcmp(value, "off") == 0) 3856 caching_page->flags1 |= SCP_RCD; 3857 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 3858 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3859 sizeof(caching_page_default)); 3860 page_index->page_data = 3861 (uint8_t *)lun->mode_pages.caching_page; 3862 break; 3863 } 3864 case SMS_CONTROL_MODE_PAGE: { 3865 struct scsi_control_page *control_page; 3866 3867 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3868 panic("invalid subpage value %d", 3869 page_index->subpage); 3870 3871 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 3872 &control_page_default, 3873 sizeof(control_page_default)); 3874 memcpy(&lun->mode_pages.control_page[ 3875 CTL_PAGE_CHANGEABLE], &control_page_changeable, 3876 sizeof(control_page_changeable)); 3877 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 3878 &control_page_default, 3879 sizeof(control_page_default)); 3880 control_page = &lun->mode_pages.control_page[ 3881 CTL_PAGE_SAVED]; 3882 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 3883 if (value != NULL && strcmp(value, "unrestricted") == 0) { 3884 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 3885 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 3886 } 3887 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 3888 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 3889 sizeof(control_page_default)); 3890 page_index->page_data = 3891 (uint8_t *)lun->mode_pages.control_page; 3892 break; 3893 3894 } 3895 case SMS_INFO_EXCEPTIONS_PAGE: { 3896 switch (page_index->subpage) { 3897 case SMS_SUBPAGE_PAGE_0: 3898 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 3899 &ie_page_default, 3900 sizeof(ie_page_default)); 3901 memcpy(&lun->mode_pages.ie_page[ 3902 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 3903 sizeof(ie_page_changeable)); 3904 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 3905 &ie_page_default, 3906 sizeof(ie_page_default)); 3907 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 3908 &ie_page_default, 3909 sizeof(ie_page_default)); 3910 page_index->page_data = 3911 (uint8_t *)lun->mode_pages.ie_page; 3912 break; 3913 case 0x02: { 3914 struct ctl_logical_block_provisioning_page *page; 3915 3916 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 3917 &lbp_page_default, 3918 sizeof(lbp_page_default)); 3919 memcpy(&lun->mode_pages.lbp_page[ 3920 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 3921 sizeof(lbp_page_changeable)); 3922 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 3923 &lbp_page_default, 3924 sizeof(lbp_page_default)); 3925 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 3926 value = ctl_get_opt(&lun->be_lun->options, 3927 "avail-threshold"); 3928 if (value != NULL && 3929 ctl_expand_number(value, &ival) == 0) { 3930 page->descr[0].flags |= SLBPPD_ENABLED | 3931 SLBPPD_ARMING_DEC; 3932 if (lun->be_lun->blocksize) 3933 ival /= lun->be_lun->blocksize; 3934 else 3935 ival /= 512; 3936 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3937 page->descr[0].count); 3938 } 3939 value = ctl_get_opt(&lun->be_lun->options, 3940 "used-threshold"); 3941 if (value != NULL && 3942 ctl_expand_number(value, &ival) == 0) { 3943 page->descr[1].flags |= SLBPPD_ENABLED | 3944 SLBPPD_ARMING_INC; 3945 if (lun->be_lun->blocksize) 3946 ival /= lun->be_lun->blocksize; 3947 else 3948 ival /= 512; 3949 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3950 page->descr[1].count); 3951 } 3952 value = ctl_get_opt(&lun->be_lun->options, 3953 "pool-avail-threshold"); 3954 if (value != NULL && 3955 ctl_expand_number(value, &ival) == 0) { 3956 page->descr[2].flags |= SLBPPD_ENABLED | 3957 SLBPPD_ARMING_DEC; 3958 if (lun->be_lun->blocksize) 3959 ival /= lun->be_lun->blocksize; 3960 else 3961 ival /= 512; 3962 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3963 page->descr[2].count); 3964 } 3965 value = ctl_get_opt(&lun->be_lun->options, 3966 "pool-used-threshold"); 3967 if (value != NULL && 3968 ctl_expand_number(value, &ival) == 0) { 3969 page->descr[3].flags |= SLBPPD_ENABLED | 3970 SLBPPD_ARMING_INC; 3971 if (lun->be_lun->blocksize) 3972 ival /= lun->be_lun->blocksize; 3973 else 3974 ival /= 512; 3975 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3976 page->descr[3].count); 3977 } 3978 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 3979 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 3980 sizeof(lbp_page_default)); 3981 page_index->page_data = 3982 (uint8_t *)lun->mode_pages.lbp_page; 3983 }} 3984 break; 3985 } 3986 case SMS_VENDOR_SPECIFIC_PAGE:{ 3987 switch (page_index->subpage) { 3988 case DBGCNF_SUBPAGE_CODE: { 3989 struct copan_debugconf_subpage *current_page, 3990 *saved_page; 3991 3992 memcpy(&lun->mode_pages.debugconf_subpage[ 3993 CTL_PAGE_CURRENT], 3994 &debugconf_page_default, 3995 sizeof(debugconf_page_default)); 3996 memcpy(&lun->mode_pages.debugconf_subpage[ 3997 CTL_PAGE_CHANGEABLE], 3998 &debugconf_page_changeable, 3999 sizeof(debugconf_page_changeable)); 4000 memcpy(&lun->mode_pages.debugconf_subpage[ 4001 CTL_PAGE_DEFAULT], 4002 &debugconf_page_default, 4003 sizeof(debugconf_page_default)); 4004 memcpy(&lun->mode_pages.debugconf_subpage[ 4005 CTL_PAGE_SAVED], 4006 &debugconf_page_default, 4007 sizeof(debugconf_page_default)); 4008 page_index->page_data = 4009 (uint8_t *)lun->mode_pages.debugconf_subpage; 4010 4011 current_page = (struct copan_debugconf_subpage *) 4012 (page_index->page_data + 4013 (page_index->page_len * 4014 CTL_PAGE_CURRENT)); 4015 saved_page = (struct copan_debugconf_subpage *) 4016 (page_index->page_data + 4017 (page_index->page_len * 4018 CTL_PAGE_SAVED)); 4019 break; 4020 } 4021 default: 4022 panic("invalid subpage value %d", 4023 page_index->subpage); 4024 break; 4025 } 4026 break; 4027 } 4028 default: 4029 panic("invalid page value %d", 4030 page_index->page_code & SMPH_PC_MASK); 4031 break; 4032 } 4033 } 4034 4035 return (CTL_RETVAL_COMPLETE); 4036} 4037 4038static int 4039ctl_init_log_page_index(struct ctl_lun *lun) 4040{ 4041 struct ctl_page_index *page_index; 4042 int i, j, k, prev; 4043 4044 memcpy(&lun->log_pages.index, log_page_index_template, 4045 sizeof(log_page_index_template)); 4046 4047 prev = -1; 4048 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4049 4050 page_index = &lun->log_pages.index[i]; 4051 /* 4052 * If this is a disk-only mode page, there's no point in 4053 * setting it up. For some pages, we have to have some 4054 * basic information about the disk in order to calculate the 4055 * mode page data. 4056 */ 4057 if ((lun->be_lun->lun_type != T_DIRECT) 4058 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4059 continue; 4060 4061 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4062 lun->backend->lun_attr == NULL) 4063 continue; 4064 4065 if (page_index->page_code != prev) { 4066 lun->log_pages.pages_page[j] = page_index->page_code; 4067 prev = page_index->page_code; 4068 j++; 4069 } 4070 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4071 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4072 k++; 4073 } 4074 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4075 lun->log_pages.index[0].page_len = j; 4076 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4077 lun->log_pages.index[1].page_len = k * 2; 4078 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4079 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4080 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4081 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4082 4083 return (CTL_RETVAL_COMPLETE); 4084} 4085 4086static int 4087hex2bin(const char *str, uint8_t *buf, int buf_size) 4088{ 4089 int i; 4090 u_char c; 4091 4092 memset(buf, 0, buf_size); 4093 while (isspace(str[0])) 4094 str++; 4095 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4096 str += 2; 4097 buf_size *= 2; 4098 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4099 c = str[i]; 4100 if (isdigit(c)) 4101 c -= '0'; 4102 else if (isalpha(c)) 4103 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4104 else 4105 break; 4106 if (c >= 16) 4107 break; 4108 if ((i & 1) == 0) 4109 buf[i / 2] |= (c << 4); 4110 else 4111 buf[i / 2] |= c; 4112 } 4113 return ((i + 1) / 2); 4114} 4115 4116/* 4117 * LUN allocation. 4118 * 4119 * Requirements: 4120 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4121 * wants us to allocate the LUN and he can block. 4122 * - ctl_softc is always set 4123 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4124 * 4125 * Returns 0 for success, non-zero (errno) for failure. 4126 */ 4127static int 4128ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4129 struct ctl_be_lun *const be_lun) 4130{ 4131 struct ctl_lun *nlun, *lun; 4132 struct scsi_vpd_id_descriptor *desc; 4133 struct scsi_vpd_id_t10 *t10id; 4134 const char *eui, *naa, *scsiname, *vendor; 4135 int lun_number, i, lun_malloced; 4136 int devidlen, idlen1, idlen2 = 0, len; 4137 4138 if (be_lun == NULL) 4139 return (EINVAL); 4140 4141 /* 4142 * We currently only support Direct Access or Processor LUN types. 4143 */ 4144 switch (be_lun->lun_type) { 4145 case T_DIRECT: 4146 break; 4147 case T_PROCESSOR: 4148 break; 4149 case T_SEQUENTIAL: 4150 case T_CHANGER: 4151 default: 4152 be_lun->lun_config_status(be_lun->be_lun, 4153 CTL_LUN_CONFIG_FAILURE); 4154 break; 4155 } 4156 if (ctl_lun == NULL) { 4157 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4158 lun_malloced = 1; 4159 } else { 4160 lun_malloced = 0; 4161 lun = ctl_lun; 4162 } 4163 4164 memset(lun, 0, sizeof(*lun)); 4165 if (lun_malloced) 4166 lun->flags = CTL_LUN_MALLOCED; 4167 4168 /* Generate LUN ID. */ 4169 devidlen = max(CTL_DEVID_MIN_LEN, 4170 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4171 idlen1 = sizeof(*t10id) + devidlen; 4172 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4173 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4174 if (scsiname != NULL) { 4175 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4176 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4177 } 4178 eui = ctl_get_opt(&be_lun->options, "eui"); 4179 if (eui != NULL) { 4180 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4181 } 4182 naa = ctl_get_opt(&be_lun->options, "naa"); 4183 if (naa != NULL) { 4184 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4185 } 4186 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4187 M_CTL, M_WAITOK | M_ZERO); 4188 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4189 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4190 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4191 desc->length = idlen1; 4192 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4193 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4194 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4195 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4196 } else { 4197 strncpy(t10id->vendor, vendor, 4198 min(sizeof(t10id->vendor), strlen(vendor))); 4199 } 4200 strncpy((char *)t10id->vendor_spec_id, 4201 (char *)be_lun->device_id, devidlen); 4202 if (scsiname != NULL) { 4203 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4204 desc->length); 4205 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4206 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4207 SVPD_ID_TYPE_SCSI_NAME; 4208 desc->length = idlen2; 4209 strlcpy(desc->identifier, scsiname, idlen2); 4210 } 4211 if (eui != NULL) { 4212 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4213 desc->length); 4214 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4215 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4216 SVPD_ID_TYPE_EUI64; 4217 desc->length = hex2bin(eui, desc->identifier, 16); 4218 desc->length = desc->length > 12 ? 16 : 4219 (desc->length > 8 ? 12 : 8); 4220 len -= 16 - desc->length; 4221 } 4222 if (naa != NULL) { 4223 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4224 desc->length); 4225 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4226 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4227 SVPD_ID_TYPE_NAA; 4228 desc->length = hex2bin(naa, desc->identifier, 16); 4229 desc->length = desc->length > 8 ? 16 : 8; 4230 len -= 16 - desc->length; 4231 } 4232 lun->lun_devid->len = len; 4233 4234 mtx_lock(&ctl_softc->ctl_lock); 4235 /* 4236 * See if the caller requested a particular LUN number. If so, see 4237 * if it is available. Otherwise, allocate the first available LUN. 4238 */ 4239 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4240 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4241 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4242 mtx_unlock(&ctl_softc->ctl_lock); 4243 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4244 printf("ctl: requested LUN ID %d is higher " 4245 "than CTL_MAX_LUNS - 1 (%d)\n", 4246 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4247 } else { 4248 /* 4249 * XXX KDM return an error, or just assign 4250 * another LUN ID in this case?? 4251 */ 4252 printf("ctl: requested LUN ID %d is already " 4253 "in use\n", be_lun->req_lun_id); 4254 } 4255 if (lun->flags & CTL_LUN_MALLOCED) 4256 free(lun, M_CTL); 4257 be_lun->lun_config_status(be_lun->be_lun, 4258 CTL_LUN_CONFIG_FAILURE); 4259 return (ENOSPC); 4260 } 4261 lun_number = be_lun->req_lun_id; 4262 } else { 4263 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4264 if (lun_number == -1) { 4265 mtx_unlock(&ctl_softc->ctl_lock); 4266 printf("ctl: can't allocate LUN, out of LUNs\n"); 4267 if (lun->flags & CTL_LUN_MALLOCED) 4268 free(lun, M_CTL); 4269 be_lun->lun_config_status(be_lun->be_lun, 4270 CTL_LUN_CONFIG_FAILURE); 4271 return (ENOSPC); 4272 } 4273 } 4274 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4275 4276 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4277 lun->lun = lun_number; 4278 lun->be_lun = be_lun; 4279 /* 4280 * The processor LUN is always enabled. Disk LUNs come on line 4281 * disabled, and must be enabled by the backend. 4282 */ 4283 lun->flags |= CTL_LUN_DISABLED; 4284 lun->backend = be_lun->be; 4285 be_lun->ctl_lun = lun; 4286 be_lun->lun_id = lun_number; 4287 atomic_add_int(&be_lun->be->num_luns, 1); 4288 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4289 lun->flags |= CTL_LUN_OFFLINE; 4290 4291 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4292 lun->flags |= CTL_LUN_STOPPED; 4293 4294 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4295 lun->flags |= CTL_LUN_INOPERABLE; 4296 4297 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4298 lun->flags |= CTL_LUN_PRIMARY_SC; 4299 4300 lun->ctl_softc = ctl_softc; 4301#ifdef CTL_TIME_IO 4302 lun->last_busy = getsbinuptime(); 4303#endif 4304 TAILQ_INIT(&lun->ooa_queue); 4305 TAILQ_INIT(&lun->blocked_queue); 4306 STAILQ_INIT(&lun->error_list); 4307 ctl_tpc_lun_init(lun); 4308 4309 /* 4310 * Initialize the mode and log page index. 4311 */ 4312 ctl_init_page_index(lun); 4313 ctl_init_log_page_index(lun); 4314 4315 /* 4316 * Now, before we insert this lun on the lun list, set the lun 4317 * inventory changed UA for all other luns. 4318 */ 4319 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4320 mtx_lock(&nlun->lun_lock); 4321 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4322 mtx_unlock(&nlun->lun_lock); 4323 } 4324 4325 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4326 4327 ctl_softc->ctl_luns[lun_number] = lun; 4328 4329 ctl_softc->num_luns++; 4330 4331 /* Setup statistics gathering */ 4332 lun->stats.device_type = be_lun->lun_type; 4333 lun->stats.lun_number = lun_number; 4334 if (lun->stats.device_type == T_DIRECT) 4335 lun->stats.blocksize = be_lun->blocksize; 4336 else 4337 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4338 for (i = 0;i < CTL_MAX_PORTS;i++) 4339 lun->stats.ports[i].targ_port = i; 4340 4341 mtx_unlock(&ctl_softc->ctl_lock); 4342 4343 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4344 return (0); 4345} 4346 4347/* 4348 * Delete a LUN. 4349 * Assumptions: 4350 * - LUN has already been marked invalid and any pending I/O has been taken 4351 * care of. 4352 */ 4353static int 4354ctl_free_lun(struct ctl_lun *lun) 4355{ 4356 struct ctl_softc *softc; 4357 struct ctl_lun *nlun; 4358 int i; 4359 4360 softc = lun->ctl_softc; 4361 4362 mtx_assert(&softc->ctl_lock, MA_OWNED); 4363 4364 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4365 4366 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4367 4368 softc->ctl_luns[lun->lun] = NULL; 4369 4370 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4371 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4372 4373 softc->num_luns--; 4374 4375 /* 4376 * Tell the backend to free resources, if this LUN has a backend. 4377 */ 4378 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4379 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4380 4381 ctl_tpc_lun_shutdown(lun); 4382 mtx_destroy(&lun->lun_lock); 4383 free(lun->lun_devid, M_CTL); 4384 for (i = 0; i < CTL_MAX_PORTS; i++) 4385 free(lun->pending_ua[i], M_CTL); 4386 for (i = 0; i < CTL_MAX_PORTS; i++) 4387 free(lun->pr_keys[i], M_CTL); 4388 free(lun->write_buffer, M_CTL); 4389 if (lun->flags & CTL_LUN_MALLOCED) 4390 free(lun, M_CTL); 4391 4392 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4393 mtx_lock(&nlun->lun_lock); 4394 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4395 mtx_unlock(&nlun->lun_lock); 4396 } 4397 4398 return (0); 4399} 4400 4401static void 4402ctl_create_lun(struct ctl_be_lun *be_lun) 4403{ 4404 struct ctl_softc *softc; 4405 4406 softc = control_softc; 4407 4408 /* 4409 * ctl_alloc_lun() should handle all potential failure cases. 4410 */ 4411 ctl_alloc_lun(softc, NULL, be_lun); 4412} 4413 4414int 4415ctl_add_lun(struct ctl_be_lun *be_lun) 4416{ 4417 struct ctl_softc *softc = control_softc; 4418 4419 mtx_lock(&softc->ctl_lock); 4420 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4421 mtx_unlock(&softc->ctl_lock); 4422 wakeup(&softc->pending_lun_queue); 4423 4424 return (0); 4425} 4426 4427int 4428ctl_enable_lun(struct ctl_be_lun *be_lun) 4429{ 4430 struct ctl_softc *softc; 4431 struct ctl_port *port, *nport; 4432 struct ctl_lun *lun; 4433 int retval; 4434 4435 lun = (struct ctl_lun *)be_lun->ctl_lun; 4436 softc = lun->ctl_softc; 4437 4438 mtx_lock(&softc->ctl_lock); 4439 mtx_lock(&lun->lun_lock); 4440 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4441 /* 4442 * eh? Why did we get called if the LUN is already 4443 * enabled? 4444 */ 4445 mtx_unlock(&lun->lun_lock); 4446 mtx_unlock(&softc->ctl_lock); 4447 return (0); 4448 } 4449 lun->flags &= ~CTL_LUN_DISABLED; 4450 mtx_unlock(&lun->lun_lock); 4451 4452 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4453 nport = STAILQ_NEXT(port, links); 4454 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4455 port->lun_map != NULL || port->lun_enable == NULL) 4456 continue; 4457 4458 /* 4459 * Drop the lock while we call the FETD's enable routine. 4460 * This can lead to a callback into CTL (at least in the 4461 * case of the internal initiator frontend. 4462 */ 4463 mtx_unlock(&softc->ctl_lock); 4464 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4465 mtx_lock(&softc->ctl_lock); 4466 if (retval != 0) { 4467 printf("%s: FETD %s port %d returned error " 4468 "%d for lun_enable on lun %jd\n", 4469 __func__, port->port_name, port->targ_port, 4470 retval, (intmax_t)lun->lun); 4471 } 4472 } 4473 4474 mtx_unlock(&softc->ctl_lock); 4475 ctl_isc_announce_lun(lun); 4476 4477 return (0); 4478} 4479 4480int 4481ctl_disable_lun(struct ctl_be_lun *be_lun) 4482{ 4483 struct ctl_softc *softc; 4484 struct ctl_port *port; 4485 struct ctl_lun *lun; 4486 int retval; 4487 4488 lun = (struct ctl_lun *)be_lun->ctl_lun; 4489 softc = lun->ctl_softc; 4490 4491 mtx_lock(&softc->ctl_lock); 4492 mtx_lock(&lun->lun_lock); 4493 if (lun->flags & CTL_LUN_DISABLED) { 4494 mtx_unlock(&lun->lun_lock); 4495 mtx_unlock(&softc->ctl_lock); 4496 return (0); 4497 } 4498 lun->flags |= CTL_LUN_DISABLED; 4499 mtx_unlock(&lun->lun_lock); 4500 4501 STAILQ_FOREACH(port, &softc->port_list, links) { 4502 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4503 port->lun_map != NULL || port->lun_disable == NULL) 4504 continue; 4505 4506 /* 4507 * Drop the lock before we call the frontend's disable 4508 * routine, to avoid lock order reversals. 4509 * 4510 * XXX KDM what happens if the frontend list changes while 4511 * we're traversing it? It's unlikely, but should be handled. 4512 */ 4513 mtx_unlock(&softc->ctl_lock); 4514 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4515 mtx_lock(&softc->ctl_lock); 4516 if (retval != 0) { 4517 printf("%s: FETD %s port %d returned error " 4518 "%d for lun_disable on lun %jd\n", 4519 __func__, port->port_name, port->targ_port, 4520 retval, (intmax_t)lun->lun); 4521 } 4522 } 4523 4524 mtx_unlock(&softc->ctl_lock); 4525 ctl_isc_announce_lun(lun); 4526 4527 return (0); 4528} 4529 4530int 4531ctl_start_lun(struct ctl_be_lun *be_lun) 4532{ 4533 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4534 4535 mtx_lock(&lun->lun_lock); 4536 lun->flags &= ~CTL_LUN_STOPPED; 4537 mtx_unlock(&lun->lun_lock); 4538 return (0); 4539} 4540 4541int 4542ctl_stop_lun(struct ctl_be_lun *be_lun) 4543{ 4544 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4545 4546 mtx_lock(&lun->lun_lock); 4547 lun->flags |= CTL_LUN_STOPPED; 4548 mtx_unlock(&lun->lun_lock); 4549 return (0); 4550} 4551 4552int 4553ctl_lun_offline(struct ctl_be_lun *be_lun) 4554{ 4555 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4556 4557 mtx_lock(&lun->lun_lock); 4558 lun->flags |= CTL_LUN_OFFLINE; 4559 mtx_unlock(&lun->lun_lock); 4560 return (0); 4561} 4562 4563int 4564ctl_lun_online(struct ctl_be_lun *be_lun) 4565{ 4566 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4567 4568 mtx_lock(&lun->lun_lock); 4569 lun->flags &= ~CTL_LUN_OFFLINE; 4570 mtx_unlock(&lun->lun_lock); 4571 return (0); 4572} 4573 4574int 4575ctl_lun_primary(struct ctl_be_lun *be_lun) 4576{ 4577 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4578 4579 mtx_lock(&lun->lun_lock); 4580 lun->flags |= CTL_LUN_PRIMARY_SC; 4581 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4582 mtx_unlock(&lun->lun_lock); 4583 ctl_isc_announce_lun(lun); 4584 return (0); 4585} 4586 4587int 4588ctl_lun_secondary(struct ctl_be_lun *be_lun) 4589{ 4590 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4591 4592 mtx_lock(&lun->lun_lock); 4593 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4594 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4595 mtx_unlock(&lun->lun_lock); 4596 ctl_isc_announce_lun(lun); 4597 return (0); 4598} 4599 4600int 4601ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4602{ 4603 struct ctl_softc *softc; 4604 struct ctl_lun *lun; 4605 4606 lun = (struct ctl_lun *)be_lun->ctl_lun; 4607 softc = lun->ctl_softc; 4608 4609 mtx_lock(&lun->lun_lock); 4610 4611 /* 4612 * The LUN needs to be disabled before it can be marked invalid. 4613 */ 4614 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4615 mtx_unlock(&lun->lun_lock); 4616 return (-1); 4617 } 4618 /* 4619 * Mark the LUN invalid. 4620 */ 4621 lun->flags |= CTL_LUN_INVALID; 4622 4623 /* 4624 * If there is nothing in the OOA queue, go ahead and free the LUN. 4625 * If we have something in the OOA queue, we'll free it when the 4626 * last I/O completes. 4627 */ 4628 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4629 mtx_unlock(&lun->lun_lock); 4630 mtx_lock(&softc->ctl_lock); 4631 ctl_free_lun(lun); 4632 mtx_unlock(&softc->ctl_lock); 4633 } else 4634 mtx_unlock(&lun->lun_lock); 4635 4636 return (0); 4637} 4638 4639int 4640ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4641{ 4642 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4643 4644 mtx_lock(&lun->lun_lock); 4645 lun->flags |= CTL_LUN_INOPERABLE; 4646 mtx_unlock(&lun->lun_lock); 4647 return (0); 4648} 4649 4650int 4651ctl_lun_operable(struct ctl_be_lun *be_lun) 4652{ 4653 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4654 4655 mtx_lock(&lun->lun_lock); 4656 lun->flags &= ~CTL_LUN_INOPERABLE; 4657 mtx_unlock(&lun->lun_lock); 4658 return (0); 4659} 4660 4661void 4662ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4663{ 4664 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4665 union ctl_ha_msg msg; 4666 4667 mtx_lock(&lun->lun_lock); 4668 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 4669 mtx_unlock(&lun->lun_lock); 4670 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4671 /* Send msg to other side. */ 4672 bzero(&msg.ua, sizeof(msg.ua)); 4673 msg.hdr.msg_type = CTL_MSG_UA; 4674 msg.hdr.nexus.initid = -1; 4675 msg.hdr.nexus.targ_port = -1; 4676 msg.hdr.nexus.targ_lun = lun->lun; 4677 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4678 msg.ua.ua_all = 1; 4679 msg.ua.ua_set = 1; 4680 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED; 4681 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4682 M_WAITOK); 4683 } 4684} 4685 4686/* 4687 * Backend "memory move is complete" callback for requests that never 4688 * make it down to say RAIDCore's configuration code. 4689 */ 4690int 4691ctl_config_move_done(union ctl_io *io) 4692{ 4693 int retval; 4694 4695 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4696 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4697 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4698 4699 if ((io->io_hdr.port_status != 0) && 4700 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4701 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4702 /* 4703 * For hardware error sense keys, the sense key 4704 * specific value is defined to be a retry count, 4705 * but we use it to pass back an internal FETD 4706 * error code. XXX KDM Hopefully the FETD is only 4707 * using 16 bits for an error code, since that's 4708 * all the space we have in the sks field. 4709 */ 4710 ctl_set_internal_failure(&io->scsiio, 4711 /*sks_valid*/ 1, 4712 /*retry_count*/ 4713 io->io_hdr.port_status); 4714 } 4715 4716 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4717 ctl_data_print(io); 4718 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4719 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4720 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4721 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4722 /* 4723 * XXX KDM just assuming a single pointer here, and not a 4724 * S/G list. If we start using S/G lists for config data, 4725 * we'll need to know how to clean them up here as well. 4726 */ 4727 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4728 free(io->scsiio.kern_data_ptr, M_CTL); 4729 ctl_done(io); 4730 retval = CTL_RETVAL_COMPLETE; 4731 } else { 4732 /* 4733 * XXX KDM now we need to continue data movement. Some 4734 * options: 4735 * - call ctl_scsiio() again? We don't do this for data 4736 * writes, because for those at least we know ahead of 4737 * time where the write will go and how long it is. For 4738 * config writes, though, that information is largely 4739 * contained within the write itself, thus we need to 4740 * parse out the data again. 4741 * 4742 * - Call some other function once the data is in? 4743 */ 4744 4745 /* 4746 * XXX KDM call ctl_scsiio() again for now, and check flag 4747 * bits to see whether we're allocated or not. 4748 */ 4749 retval = ctl_scsiio(&io->scsiio); 4750 } 4751 return (retval); 4752} 4753 4754/* 4755 * This gets called by a backend driver when it is done with a 4756 * data_submit method. 4757 */ 4758void 4759ctl_data_submit_done(union ctl_io *io) 4760{ 4761 /* 4762 * If the IO_CONT flag is set, we need to call the supplied 4763 * function to continue processing the I/O, instead of completing 4764 * the I/O just yet. 4765 * 4766 * If there is an error, though, we don't want to keep processing. 4767 * Instead, just send status back to the initiator. 4768 */ 4769 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4770 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4771 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4772 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4773 io->scsiio.io_cont(io); 4774 return; 4775 } 4776 ctl_done(io); 4777} 4778 4779/* 4780 * This gets called by a backend driver when it is done with a 4781 * configuration write. 4782 */ 4783void 4784ctl_config_write_done(union ctl_io *io) 4785{ 4786 uint8_t *buf; 4787 4788 /* 4789 * If the IO_CONT flag is set, we need to call the supplied 4790 * function to continue processing the I/O, instead of completing 4791 * the I/O just yet. 4792 * 4793 * If there is an error, though, we don't want to keep processing. 4794 * Instead, just send status back to the initiator. 4795 */ 4796 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4797 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4798 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4799 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4800 io->scsiio.io_cont(io); 4801 return; 4802 } 4803 /* 4804 * Since a configuration write can be done for commands that actually 4805 * have data allocated, like write buffer, and commands that have 4806 * no data, like start/stop unit, we need to check here. 4807 */ 4808 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4809 buf = io->scsiio.kern_data_ptr; 4810 else 4811 buf = NULL; 4812 ctl_done(io); 4813 if (buf) 4814 free(buf, M_CTL); 4815} 4816 4817void 4818ctl_config_read_done(union ctl_io *io) 4819{ 4820 uint8_t *buf; 4821 4822 /* 4823 * If there is some error -- we are done, skip data transfer. 4824 */ 4825 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 4826 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4827 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 4828 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4829 buf = io->scsiio.kern_data_ptr; 4830 else 4831 buf = NULL; 4832 ctl_done(io); 4833 if (buf) 4834 free(buf, M_CTL); 4835 return; 4836 } 4837 4838 /* 4839 * If the IO_CONT flag is set, we need to call the supplied 4840 * function to continue processing the I/O, instead of completing 4841 * the I/O just yet. 4842 */ 4843 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 4844 io->scsiio.io_cont(io); 4845 return; 4846 } 4847 4848 ctl_datamove(io); 4849} 4850 4851/* 4852 * SCSI release command. 4853 */ 4854int 4855ctl_scsi_release(struct ctl_scsiio *ctsio) 4856{ 4857 int length, longid, thirdparty_id, resv_id; 4858 struct ctl_lun *lun; 4859 uint32_t residx; 4860 4861 length = 0; 4862 resv_id = 0; 4863 4864 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 4865 4866 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 4867 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4868 4869 switch (ctsio->cdb[0]) { 4870 case RELEASE_10: { 4871 struct scsi_release_10 *cdb; 4872 4873 cdb = (struct scsi_release_10 *)ctsio->cdb; 4874 4875 if (cdb->byte2 & SR10_LONGID) 4876 longid = 1; 4877 else 4878 thirdparty_id = cdb->thirdparty_id; 4879 4880 resv_id = cdb->resv_id; 4881 length = scsi_2btoul(cdb->length); 4882 break; 4883 } 4884 } 4885 4886 4887 /* 4888 * XXX KDM right now, we only support LUN reservation. We don't 4889 * support 3rd party reservations, or extent reservations, which 4890 * might actually need the parameter list. If we've gotten this 4891 * far, we've got a LUN reservation. Anything else got kicked out 4892 * above. So, according to SPC, ignore the length. 4893 */ 4894 length = 0; 4895 4896 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 4897 && (length > 0)) { 4898 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 4899 ctsio->kern_data_len = length; 4900 ctsio->kern_total_len = length; 4901 ctsio->kern_data_resid = 0; 4902 ctsio->kern_rel_offset = 0; 4903 ctsio->kern_sg_entries = 0; 4904 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 4905 ctsio->be_move_done = ctl_config_move_done; 4906 ctl_datamove((union ctl_io *)ctsio); 4907 4908 return (CTL_RETVAL_COMPLETE); 4909 } 4910 4911 if (length > 0) 4912 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 4913 4914 mtx_lock(&lun->lun_lock); 4915 4916 /* 4917 * According to SPC, it is not an error for an intiator to attempt 4918 * to release a reservation on a LUN that isn't reserved, or that 4919 * is reserved by another initiator. The reservation can only be 4920 * released, though, by the initiator who made it or by one of 4921 * several reset type events. 4922 */ 4923 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 4924 lun->flags &= ~CTL_LUN_RESERVED; 4925 4926 mtx_unlock(&lun->lun_lock); 4927 4928 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 4929 free(ctsio->kern_data_ptr, M_CTL); 4930 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 4931 } 4932 4933 ctl_set_success(ctsio); 4934 ctl_done((union ctl_io *)ctsio); 4935 return (CTL_RETVAL_COMPLETE); 4936} 4937 4938int 4939ctl_scsi_reserve(struct ctl_scsiio *ctsio) 4940{ 4941 int extent, thirdparty, longid; 4942 int resv_id, length; 4943 uint64_t thirdparty_id; 4944 struct ctl_lun *lun; 4945 uint32_t residx; 4946 4947 extent = 0; 4948 thirdparty = 0; 4949 longid = 0; 4950 resv_id = 0; 4951 length = 0; 4952 thirdparty_id = 0; 4953 4954 CTL_DEBUG_PRINT(("ctl_reserve\n")); 4955 4956 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 4957 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4958 4959 switch (ctsio->cdb[0]) { 4960 case RESERVE_10: { 4961 struct scsi_reserve_10 *cdb; 4962 4963 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 4964 4965 if (cdb->byte2 & SR10_LONGID) 4966 longid = 1; 4967 else 4968 thirdparty_id = cdb->thirdparty_id; 4969 4970 resv_id = cdb->resv_id; 4971 length = scsi_2btoul(cdb->length); 4972 break; 4973 } 4974 } 4975 4976 /* 4977 * XXX KDM right now, we only support LUN reservation. We don't 4978 * support 3rd party reservations, or extent reservations, which 4979 * might actually need the parameter list. If we've gotten this 4980 * far, we've got a LUN reservation. Anything else got kicked out 4981 * above. So, according to SPC, ignore the length. 4982 */ 4983 length = 0; 4984 4985 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 4986 && (length > 0)) { 4987 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 4988 ctsio->kern_data_len = length; 4989 ctsio->kern_total_len = length; 4990 ctsio->kern_data_resid = 0; 4991 ctsio->kern_rel_offset = 0; 4992 ctsio->kern_sg_entries = 0; 4993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 4994 ctsio->be_move_done = ctl_config_move_done; 4995 ctl_datamove((union ctl_io *)ctsio); 4996 4997 return (CTL_RETVAL_COMPLETE); 4998 } 4999 5000 if (length > 0) 5001 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5002 5003 mtx_lock(&lun->lun_lock); 5004 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5005 ctl_set_reservation_conflict(ctsio); 5006 goto bailout; 5007 } 5008 5009 lun->flags |= CTL_LUN_RESERVED; 5010 lun->res_idx = residx; 5011 5012 ctl_set_success(ctsio); 5013 5014bailout: 5015 mtx_unlock(&lun->lun_lock); 5016 5017 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5018 free(ctsio->kern_data_ptr, M_CTL); 5019 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5020 } 5021 5022 ctl_done((union ctl_io *)ctsio); 5023 return (CTL_RETVAL_COMPLETE); 5024} 5025 5026int 5027ctl_start_stop(struct ctl_scsiio *ctsio) 5028{ 5029 struct scsi_start_stop_unit *cdb; 5030 struct ctl_lun *lun; 5031 int retval; 5032 5033 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5034 5035 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5036 retval = 0; 5037 5038 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5039 5040 /* 5041 * XXX KDM 5042 * We don't support the immediate bit on a stop unit. In order to 5043 * do that, we would need to code up a way to know that a stop is 5044 * pending, and hold off any new commands until it completes, one 5045 * way or another. Then we could accept or reject those commands 5046 * depending on its status. We would almost need to do the reverse 5047 * of what we do below for an immediate start -- return the copy of 5048 * the ctl_io to the FETD with status to send to the host (and to 5049 * free the copy!) and then free the original I/O once the stop 5050 * actually completes. That way, the OOA queue mechanism can work 5051 * to block commands that shouldn't proceed. Another alternative 5052 * would be to put the copy in the queue in place of the original, 5053 * and return the original back to the caller. That could be 5054 * slightly safer.. 5055 */ 5056 if ((cdb->byte2 & SSS_IMMED) 5057 && ((cdb->how & SSS_START) == 0)) { 5058 ctl_set_invalid_field(ctsio, 5059 /*sks_valid*/ 1, 5060 /*command*/ 1, 5061 /*field*/ 1, 5062 /*bit_valid*/ 1, 5063 /*bit*/ 0); 5064 ctl_done((union ctl_io *)ctsio); 5065 return (CTL_RETVAL_COMPLETE); 5066 } 5067 5068 if ((lun->flags & CTL_LUN_PR_RESERVED) 5069 && ((cdb->how & SSS_START)==0)) { 5070 uint32_t residx; 5071 5072 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5073 if (ctl_get_prkey(lun, residx) == 0 5074 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5075 5076 ctl_set_reservation_conflict(ctsio); 5077 ctl_done((union ctl_io *)ctsio); 5078 return (CTL_RETVAL_COMPLETE); 5079 } 5080 } 5081 5082 /* 5083 * If there is no backend on this device, we can't start or stop 5084 * it. In theory we shouldn't get any start/stop commands in the 5085 * first place at this level if the LUN doesn't have a backend. 5086 * That should get stopped by the command decode code. 5087 */ 5088 if (lun->backend == NULL) { 5089 ctl_set_invalid_opcode(ctsio); 5090 ctl_done((union ctl_io *)ctsio); 5091 return (CTL_RETVAL_COMPLETE); 5092 } 5093 5094 /* 5095 * XXX KDM Copan-specific offline behavior. 5096 * Figure out a reasonable way to port this? 5097 */ 5098#ifdef NEEDTOPORT 5099 mtx_lock(&lun->lun_lock); 5100 5101 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5102 && (lun->flags & CTL_LUN_OFFLINE)) { 5103 /* 5104 * If the LUN is offline, and the on/offline bit isn't set, 5105 * reject the start or stop. Otherwise, let it through. 5106 */ 5107 mtx_unlock(&lun->lun_lock); 5108 ctl_set_lun_not_ready(ctsio); 5109 ctl_done((union ctl_io *)ctsio); 5110 } else { 5111 mtx_unlock(&lun->lun_lock); 5112#endif /* NEEDTOPORT */ 5113 /* 5114 * This could be a start or a stop when we're online, 5115 * or a stop/offline or start/online. A start or stop when 5116 * we're offline is covered in the case above. 5117 */ 5118 /* 5119 * In the non-immediate case, we send the request to 5120 * the backend and return status to the user when 5121 * it is done. 5122 * 5123 * In the immediate case, we allocate a new ctl_io 5124 * to hold a copy of the request, and send that to 5125 * the backend. We then set good status on the 5126 * user's request and return it immediately. 5127 */ 5128 if (cdb->byte2 & SSS_IMMED) { 5129 union ctl_io *new_io; 5130 5131 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5132 ctl_copy_io((union ctl_io *)ctsio, new_io); 5133 retval = lun->backend->config_write(new_io); 5134 ctl_set_success(ctsio); 5135 ctl_done((union ctl_io *)ctsio); 5136 } else { 5137 retval = lun->backend->config_write( 5138 (union ctl_io *)ctsio); 5139 } 5140#ifdef NEEDTOPORT 5141 } 5142#endif 5143 return (retval); 5144} 5145 5146/* 5147 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5148 * we don't really do anything with the LBA and length fields if the user 5149 * passes them in. Instead we'll just flush out the cache for the entire 5150 * LUN. 5151 */ 5152int 5153ctl_sync_cache(struct ctl_scsiio *ctsio) 5154{ 5155 struct ctl_lun *lun; 5156 struct ctl_softc *softc; 5157 struct ctl_lba_len_flags *lbalen; 5158 uint64_t starting_lba; 5159 uint32_t block_count; 5160 int retval; 5161 uint8_t byte2; 5162 5163 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5164 5165 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5166 softc = lun->ctl_softc; 5167 retval = 0; 5168 5169 switch (ctsio->cdb[0]) { 5170 case SYNCHRONIZE_CACHE: { 5171 struct scsi_sync_cache *cdb; 5172 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5173 5174 starting_lba = scsi_4btoul(cdb->begin_lba); 5175 block_count = scsi_2btoul(cdb->lb_count); 5176 byte2 = cdb->byte2; 5177 break; 5178 } 5179 case SYNCHRONIZE_CACHE_16: { 5180 struct scsi_sync_cache_16 *cdb; 5181 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5182 5183 starting_lba = scsi_8btou64(cdb->begin_lba); 5184 block_count = scsi_4btoul(cdb->lb_count); 5185 byte2 = cdb->byte2; 5186 break; 5187 } 5188 default: 5189 ctl_set_invalid_opcode(ctsio); 5190 ctl_done((union ctl_io *)ctsio); 5191 goto bailout; 5192 break; /* NOTREACHED */ 5193 } 5194 5195 /* 5196 * We check the LBA and length, but don't do anything with them. 5197 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5198 * get flushed. This check will just help satisfy anyone who wants 5199 * to see an error for an out of range LBA. 5200 */ 5201 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5202 ctl_set_lba_out_of_range(ctsio); 5203 ctl_done((union ctl_io *)ctsio); 5204 goto bailout; 5205 } 5206 5207 /* 5208 * If this LUN has no backend, we can't flush the cache anyway. 5209 */ 5210 if (lun->backend == NULL) { 5211 ctl_set_invalid_opcode(ctsio); 5212 ctl_done((union ctl_io *)ctsio); 5213 goto bailout; 5214 } 5215 5216 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5217 lbalen->lba = starting_lba; 5218 lbalen->len = block_count; 5219 lbalen->flags = byte2; 5220 5221 /* 5222 * Check to see whether we're configured to send the SYNCHRONIZE 5223 * CACHE command directly to the back end. 5224 */ 5225 mtx_lock(&lun->lun_lock); 5226 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5227 && (++(lun->sync_count) >= lun->sync_interval)) { 5228 lun->sync_count = 0; 5229 mtx_unlock(&lun->lun_lock); 5230 retval = lun->backend->config_write((union ctl_io *)ctsio); 5231 } else { 5232 mtx_unlock(&lun->lun_lock); 5233 ctl_set_success(ctsio); 5234 ctl_done((union ctl_io *)ctsio); 5235 } 5236 5237bailout: 5238 5239 return (retval); 5240} 5241 5242int 5243ctl_format(struct ctl_scsiio *ctsio) 5244{ 5245 struct scsi_format *cdb; 5246 struct ctl_lun *lun; 5247 int length, defect_list_len; 5248 5249 CTL_DEBUG_PRINT(("ctl_format\n")); 5250 5251 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5252 5253 cdb = (struct scsi_format *)ctsio->cdb; 5254 5255 length = 0; 5256 if (cdb->byte2 & SF_FMTDATA) { 5257 if (cdb->byte2 & SF_LONGLIST) 5258 length = sizeof(struct scsi_format_header_long); 5259 else 5260 length = sizeof(struct scsi_format_header_short); 5261 } 5262 5263 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5264 && (length > 0)) { 5265 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5266 ctsio->kern_data_len = length; 5267 ctsio->kern_total_len = length; 5268 ctsio->kern_data_resid = 0; 5269 ctsio->kern_rel_offset = 0; 5270 ctsio->kern_sg_entries = 0; 5271 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5272 ctsio->be_move_done = ctl_config_move_done; 5273 ctl_datamove((union ctl_io *)ctsio); 5274 5275 return (CTL_RETVAL_COMPLETE); 5276 } 5277 5278 defect_list_len = 0; 5279 5280 if (cdb->byte2 & SF_FMTDATA) { 5281 if (cdb->byte2 & SF_LONGLIST) { 5282 struct scsi_format_header_long *header; 5283 5284 header = (struct scsi_format_header_long *) 5285 ctsio->kern_data_ptr; 5286 5287 defect_list_len = scsi_4btoul(header->defect_list_len); 5288 if (defect_list_len != 0) { 5289 ctl_set_invalid_field(ctsio, 5290 /*sks_valid*/ 1, 5291 /*command*/ 0, 5292 /*field*/ 2, 5293 /*bit_valid*/ 0, 5294 /*bit*/ 0); 5295 goto bailout; 5296 } 5297 } else { 5298 struct scsi_format_header_short *header; 5299 5300 header = (struct scsi_format_header_short *) 5301 ctsio->kern_data_ptr; 5302 5303 defect_list_len = scsi_2btoul(header->defect_list_len); 5304 if (defect_list_len != 0) { 5305 ctl_set_invalid_field(ctsio, 5306 /*sks_valid*/ 1, 5307 /*command*/ 0, 5308 /*field*/ 2, 5309 /*bit_valid*/ 0, 5310 /*bit*/ 0); 5311 goto bailout; 5312 } 5313 } 5314 } 5315 5316 /* 5317 * The format command will clear out the "Medium format corrupted" 5318 * status if set by the configuration code. That status is really 5319 * just a way to notify the host that we have lost the media, and 5320 * get them to issue a command that will basically make them think 5321 * they're blowing away the media. 5322 */ 5323 mtx_lock(&lun->lun_lock); 5324 lun->flags &= ~CTL_LUN_INOPERABLE; 5325 mtx_unlock(&lun->lun_lock); 5326 5327 ctl_set_success(ctsio); 5328bailout: 5329 5330 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5331 free(ctsio->kern_data_ptr, M_CTL); 5332 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5333 } 5334 5335 ctl_done((union ctl_io *)ctsio); 5336 return (CTL_RETVAL_COMPLETE); 5337} 5338 5339int 5340ctl_read_buffer(struct ctl_scsiio *ctsio) 5341{ 5342 struct scsi_read_buffer *cdb; 5343 struct ctl_lun *lun; 5344 int buffer_offset, len; 5345 static uint8_t descr[4]; 5346 static uint8_t echo_descr[4] = { 0 }; 5347 5348 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5349 5350 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5351 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5352 5353 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5354 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5355 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5356 ctl_set_invalid_field(ctsio, 5357 /*sks_valid*/ 1, 5358 /*command*/ 1, 5359 /*field*/ 1, 5360 /*bit_valid*/ 1, 5361 /*bit*/ 4); 5362 ctl_done((union ctl_io *)ctsio); 5363 return (CTL_RETVAL_COMPLETE); 5364 } 5365 5366 len = scsi_3btoul(cdb->length); 5367 buffer_offset = scsi_3btoul(cdb->offset); 5368 5369 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5370 ctl_set_invalid_field(ctsio, 5371 /*sks_valid*/ 1, 5372 /*command*/ 1, 5373 /*field*/ 6, 5374 /*bit_valid*/ 0, 5375 /*bit*/ 0); 5376 ctl_done((union ctl_io *)ctsio); 5377 return (CTL_RETVAL_COMPLETE); 5378 } 5379 5380 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5381 descr[0] = 0; 5382 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5383 ctsio->kern_data_ptr = descr; 5384 len = min(len, sizeof(descr)); 5385 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5386 ctsio->kern_data_ptr = echo_descr; 5387 len = min(len, sizeof(echo_descr)); 5388 } else { 5389 if (lun->write_buffer == NULL) { 5390 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5391 M_CTL, M_WAITOK); 5392 } 5393 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5394 } 5395 ctsio->kern_data_len = len; 5396 ctsio->kern_total_len = len; 5397 ctsio->kern_data_resid = 0; 5398 ctsio->kern_rel_offset = 0; 5399 ctsio->kern_sg_entries = 0; 5400 ctl_set_success(ctsio); 5401 ctsio->be_move_done = ctl_config_move_done; 5402 ctl_datamove((union ctl_io *)ctsio); 5403 return (CTL_RETVAL_COMPLETE); 5404} 5405 5406int 5407ctl_write_buffer(struct ctl_scsiio *ctsio) 5408{ 5409 struct scsi_write_buffer *cdb; 5410 struct ctl_lun *lun; 5411 int buffer_offset, len; 5412 5413 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5414 5415 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5416 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5417 5418 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5419 ctl_set_invalid_field(ctsio, 5420 /*sks_valid*/ 1, 5421 /*command*/ 1, 5422 /*field*/ 1, 5423 /*bit_valid*/ 1, 5424 /*bit*/ 4); 5425 ctl_done((union ctl_io *)ctsio); 5426 return (CTL_RETVAL_COMPLETE); 5427 } 5428 5429 len = scsi_3btoul(cdb->length); 5430 buffer_offset = scsi_3btoul(cdb->offset); 5431 5432 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5433 ctl_set_invalid_field(ctsio, 5434 /*sks_valid*/ 1, 5435 /*command*/ 1, 5436 /*field*/ 6, 5437 /*bit_valid*/ 0, 5438 /*bit*/ 0); 5439 ctl_done((union ctl_io *)ctsio); 5440 return (CTL_RETVAL_COMPLETE); 5441 } 5442 5443 /* 5444 * If we've got a kernel request that hasn't been malloced yet, 5445 * malloc it and tell the caller the data buffer is here. 5446 */ 5447 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5448 if (lun->write_buffer == NULL) { 5449 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5450 M_CTL, M_WAITOK); 5451 } 5452 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5453 ctsio->kern_data_len = len; 5454 ctsio->kern_total_len = len; 5455 ctsio->kern_data_resid = 0; 5456 ctsio->kern_rel_offset = 0; 5457 ctsio->kern_sg_entries = 0; 5458 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5459 ctsio->be_move_done = ctl_config_move_done; 5460 ctl_datamove((union ctl_io *)ctsio); 5461 5462 return (CTL_RETVAL_COMPLETE); 5463 } 5464 5465 ctl_set_success(ctsio); 5466 ctl_done((union ctl_io *)ctsio); 5467 return (CTL_RETVAL_COMPLETE); 5468} 5469 5470int 5471ctl_write_same(struct ctl_scsiio *ctsio) 5472{ 5473 struct ctl_lun *lun; 5474 struct ctl_lba_len_flags *lbalen; 5475 uint64_t lba; 5476 uint32_t num_blocks; 5477 int len, retval; 5478 uint8_t byte2; 5479 5480 retval = CTL_RETVAL_COMPLETE; 5481 5482 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5483 5484 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5485 5486 switch (ctsio->cdb[0]) { 5487 case WRITE_SAME_10: { 5488 struct scsi_write_same_10 *cdb; 5489 5490 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5491 5492 lba = scsi_4btoul(cdb->addr); 5493 num_blocks = scsi_2btoul(cdb->length); 5494 byte2 = cdb->byte2; 5495 break; 5496 } 5497 case WRITE_SAME_16: { 5498 struct scsi_write_same_16 *cdb; 5499 5500 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5501 5502 lba = scsi_8btou64(cdb->addr); 5503 num_blocks = scsi_4btoul(cdb->length); 5504 byte2 = cdb->byte2; 5505 break; 5506 } 5507 default: 5508 /* 5509 * We got a command we don't support. This shouldn't 5510 * happen, commands should be filtered out above us. 5511 */ 5512 ctl_set_invalid_opcode(ctsio); 5513 ctl_done((union ctl_io *)ctsio); 5514 5515 return (CTL_RETVAL_COMPLETE); 5516 break; /* NOTREACHED */ 5517 } 5518 5519 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5520 if ((byte2 & SWS_UNMAP) == 0 && 5521 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5522 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5523 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5524 ctl_done((union ctl_io *)ctsio); 5525 return (CTL_RETVAL_COMPLETE); 5526 } 5527 5528 /* 5529 * The first check is to make sure we're in bounds, the second 5530 * check is to catch wrap-around problems. If the lba + num blocks 5531 * is less than the lba, then we've wrapped around and the block 5532 * range is invalid anyway. 5533 */ 5534 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5535 || ((lba + num_blocks) < lba)) { 5536 ctl_set_lba_out_of_range(ctsio); 5537 ctl_done((union ctl_io *)ctsio); 5538 return (CTL_RETVAL_COMPLETE); 5539 } 5540 5541 /* Zero number of blocks means "to the last logical block" */ 5542 if (num_blocks == 0) { 5543 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5544 ctl_set_invalid_field(ctsio, 5545 /*sks_valid*/ 0, 5546 /*command*/ 1, 5547 /*field*/ 0, 5548 /*bit_valid*/ 0, 5549 /*bit*/ 0); 5550 ctl_done((union ctl_io *)ctsio); 5551 return (CTL_RETVAL_COMPLETE); 5552 } 5553 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5554 } 5555 5556 len = lun->be_lun->blocksize; 5557 5558 /* 5559 * If we've got a kernel request that hasn't been malloced yet, 5560 * malloc it and tell the caller the data buffer is here. 5561 */ 5562 if ((byte2 & SWS_NDOB) == 0 && 5563 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5564 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5565 ctsio->kern_data_len = len; 5566 ctsio->kern_total_len = len; 5567 ctsio->kern_data_resid = 0; 5568 ctsio->kern_rel_offset = 0; 5569 ctsio->kern_sg_entries = 0; 5570 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5571 ctsio->be_move_done = ctl_config_move_done; 5572 ctl_datamove((union ctl_io *)ctsio); 5573 5574 return (CTL_RETVAL_COMPLETE); 5575 } 5576 5577 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5578 lbalen->lba = lba; 5579 lbalen->len = num_blocks; 5580 lbalen->flags = byte2; 5581 retval = lun->backend->config_write((union ctl_io *)ctsio); 5582 5583 return (retval); 5584} 5585 5586int 5587ctl_unmap(struct ctl_scsiio *ctsio) 5588{ 5589 struct ctl_lun *lun; 5590 struct scsi_unmap *cdb; 5591 struct ctl_ptr_len_flags *ptrlen; 5592 struct scsi_unmap_header *hdr; 5593 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5594 uint64_t lba; 5595 uint32_t num_blocks; 5596 int len, retval; 5597 uint8_t byte2; 5598 5599 retval = CTL_RETVAL_COMPLETE; 5600 5601 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5602 5603 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5604 cdb = (struct scsi_unmap *)ctsio->cdb; 5605 5606 len = scsi_2btoul(cdb->length); 5607 byte2 = cdb->byte2; 5608 5609 /* 5610 * If we've got a kernel request that hasn't been malloced yet, 5611 * malloc it and tell the caller the data buffer is here. 5612 */ 5613 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5614 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5615 ctsio->kern_data_len = len; 5616 ctsio->kern_total_len = len; 5617 ctsio->kern_data_resid = 0; 5618 ctsio->kern_rel_offset = 0; 5619 ctsio->kern_sg_entries = 0; 5620 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5621 ctsio->be_move_done = ctl_config_move_done; 5622 ctl_datamove((union ctl_io *)ctsio); 5623 5624 return (CTL_RETVAL_COMPLETE); 5625 } 5626 5627 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5628 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5629 if (len < sizeof (*hdr) || 5630 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5631 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5632 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5633 ctl_set_invalid_field(ctsio, 5634 /*sks_valid*/ 0, 5635 /*command*/ 0, 5636 /*field*/ 0, 5637 /*bit_valid*/ 0, 5638 /*bit*/ 0); 5639 goto done; 5640 } 5641 len = scsi_2btoul(hdr->desc_length); 5642 buf = (struct scsi_unmap_desc *)(hdr + 1); 5643 end = buf + len / sizeof(*buf); 5644 5645 endnz = buf; 5646 for (range = buf; range < end; range++) { 5647 lba = scsi_8btou64(range->lba); 5648 num_blocks = scsi_4btoul(range->length); 5649 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5650 || ((lba + num_blocks) < lba)) { 5651 ctl_set_lba_out_of_range(ctsio); 5652 ctl_done((union ctl_io *)ctsio); 5653 return (CTL_RETVAL_COMPLETE); 5654 } 5655 if (num_blocks != 0) 5656 endnz = range + 1; 5657 } 5658 5659 /* 5660 * Block backend can not handle zero last range. 5661 * Filter it out and return if there is nothing left. 5662 */ 5663 len = (uint8_t *)endnz - (uint8_t *)buf; 5664 if (len == 0) { 5665 ctl_set_success(ctsio); 5666 goto done; 5667 } 5668 5669 mtx_lock(&lun->lun_lock); 5670 ptrlen = (struct ctl_ptr_len_flags *) 5671 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5672 ptrlen->ptr = (void *)buf; 5673 ptrlen->len = len; 5674 ptrlen->flags = byte2; 5675 ctl_check_blocked(lun); 5676 mtx_unlock(&lun->lun_lock); 5677 5678 retval = lun->backend->config_write((union ctl_io *)ctsio); 5679 return (retval); 5680 5681done: 5682 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5683 free(ctsio->kern_data_ptr, M_CTL); 5684 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5685 } 5686 ctl_done((union ctl_io *)ctsio); 5687 return (CTL_RETVAL_COMPLETE); 5688} 5689 5690/* 5691 * Note that this function currently doesn't actually do anything inside 5692 * CTL to enforce things if the DQue bit is turned on. 5693 * 5694 * Also note that this function can't be used in the default case, because 5695 * the DQue bit isn't set in the changeable mask for the control mode page 5696 * anyway. This is just here as an example for how to implement a page 5697 * handler, and a placeholder in case we want to allow the user to turn 5698 * tagged queueing on and off. 5699 * 5700 * The D_SENSE bit handling is functional, however, and will turn 5701 * descriptor sense on and off for a given LUN. 5702 */ 5703int 5704ctl_control_page_handler(struct ctl_scsiio *ctsio, 5705 struct ctl_page_index *page_index, uint8_t *page_ptr) 5706{ 5707 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5708 struct ctl_lun *lun; 5709 int set_ua; 5710 uint32_t initidx; 5711 5712 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5713 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5714 set_ua = 0; 5715 5716 user_cp = (struct scsi_control_page *)page_ptr; 5717 current_cp = (struct scsi_control_page *) 5718 (page_index->page_data + (page_index->page_len * 5719 CTL_PAGE_CURRENT)); 5720 saved_cp = (struct scsi_control_page *) 5721 (page_index->page_data + (page_index->page_len * 5722 CTL_PAGE_SAVED)); 5723 5724 mtx_lock(&lun->lun_lock); 5725 if (((current_cp->rlec & SCP_DSENSE) == 0) 5726 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5727 /* 5728 * Descriptor sense is currently turned off and the user 5729 * wants to turn it on. 5730 */ 5731 current_cp->rlec |= SCP_DSENSE; 5732 saved_cp->rlec |= SCP_DSENSE; 5733 lun->flags |= CTL_LUN_SENSE_DESC; 5734 set_ua = 1; 5735 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5736 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5737 /* 5738 * Descriptor sense is currently turned on, and the user 5739 * wants to turn it off. 5740 */ 5741 current_cp->rlec &= ~SCP_DSENSE; 5742 saved_cp->rlec &= ~SCP_DSENSE; 5743 lun->flags &= ~CTL_LUN_SENSE_DESC; 5744 set_ua = 1; 5745 } 5746 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5747 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5748 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5749 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5750 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5751 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5752 set_ua = 1; 5753 } 5754 if ((current_cp->eca_and_aen & SCP_SWP) != 5755 (user_cp->eca_and_aen & SCP_SWP)) { 5756 current_cp->eca_and_aen &= ~SCP_SWP; 5757 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5758 saved_cp->eca_and_aen &= ~SCP_SWP; 5759 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5760 set_ua = 1; 5761 } 5762 if (set_ua != 0) 5763 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5764 mtx_unlock(&lun->lun_lock); 5765 5766 return (0); 5767} 5768 5769int 5770ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5771 struct ctl_page_index *page_index, uint8_t *page_ptr) 5772{ 5773 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5774 struct ctl_lun *lun; 5775 int set_ua; 5776 uint32_t initidx; 5777 5778 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5779 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5780 set_ua = 0; 5781 5782 user_cp = (struct scsi_caching_page *)page_ptr; 5783 current_cp = (struct scsi_caching_page *) 5784 (page_index->page_data + (page_index->page_len * 5785 CTL_PAGE_CURRENT)); 5786 saved_cp = (struct scsi_caching_page *) 5787 (page_index->page_data + (page_index->page_len * 5788 CTL_PAGE_SAVED)); 5789 5790 mtx_lock(&lun->lun_lock); 5791 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 5792 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 5793 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5794 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5795 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5796 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5797 set_ua = 1; 5798 } 5799 if (set_ua != 0) 5800 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5801 mtx_unlock(&lun->lun_lock); 5802 5803 return (0); 5804} 5805 5806int 5807ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 5808 struct ctl_page_index *page_index, 5809 uint8_t *page_ptr) 5810{ 5811 uint8_t *c; 5812 int i; 5813 5814 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 5815 ctl_time_io_secs = 5816 (c[0] << 8) | 5817 (c[1] << 0) | 5818 0; 5819 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 5820 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 5821 printf("page data:"); 5822 for (i=0; i<8; i++) 5823 printf(" %.2x",page_ptr[i]); 5824 printf("\n"); 5825 return (0); 5826} 5827 5828int 5829ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 5830 struct ctl_page_index *page_index, 5831 int pc) 5832{ 5833 struct copan_debugconf_subpage *page; 5834 5835 page = (struct copan_debugconf_subpage *)page_index->page_data + 5836 (page_index->page_len * pc); 5837 5838 switch (pc) { 5839 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5840 case SMS_PAGE_CTRL_DEFAULT >> 6: 5841 case SMS_PAGE_CTRL_SAVED >> 6: 5842 /* 5843 * We don't update the changable or default bits for this page. 5844 */ 5845 break; 5846 case SMS_PAGE_CTRL_CURRENT >> 6: 5847 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 5848 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 5849 break; 5850 default: 5851#ifdef NEEDTOPORT 5852 EPRINT(0, "Invalid PC %d!!", pc); 5853#endif /* NEEDTOPORT */ 5854 break; 5855 } 5856 return (0); 5857} 5858 5859 5860static int 5861ctl_do_mode_select(union ctl_io *io) 5862{ 5863 struct scsi_mode_page_header *page_header; 5864 struct ctl_page_index *page_index; 5865 struct ctl_scsiio *ctsio; 5866 int control_dev, page_len; 5867 int page_len_offset, page_len_size; 5868 union ctl_modepage_info *modepage_info; 5869 struct ctl_lun *lun; 5870 int *len_left, *len_used; 5871 int retval, i; 5872 5873 ctsio = &io->scsiio; 5874 page_index = NULL; 5875 page_len = 0; 5876 retval = CTL_RETVAL_COMPLETE; 5877 5878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5879 5880 if (lun->be_lun->lun_type != T_DIRECT) 5881 control_dev = 1; 5882 else 5883 control_dev = 0; 5884 5885 modepage_info = (union ctl_modepage_info *) 5886 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5887 len_left = &modepage_info->header.len_left; 5888 len_used = &modepage_info->header.len_used; 5889 5890do_next_page: 5891 5892 page_header = (struct scsi_mode_page_header *) 5893 (ctsio->kern_data_ptr + *len_used); 5894 5895 if (*len_left == 0) { 5896 free(ctsio->kern_data_ptr, M_CTL); 5897 ctl_set_success(ctsio); 5898 ctl_done((union ctl_io *)ctsio); 5899 return (CTL_RETVAL_COMPLETE); 5900 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5901 5902 free(ctsio->kern_data_ptr, M_CTL); 5903 ctl_set_param_len_error(ctsio); 5904 ctl_done((union ctl_io *)ctsio); 5905 return (CTL_RETVAL_COMPLETE); 5906 5907 } else if ((page_header->page_code & SMPH_SPF) 5908 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5909 5910 free(ctsio->kern_data_ptr, M_CTL); 5911 ctl_set_param_len_error(ctsio); 5912 ctl_done((union ctl_io *)ctsio); 5913 return (CTL_RETVAL_COMPLETE); 5914 } 5915 5916 5917 /* 5918 * XXX KDM should we do something with the block descriptor? 5919 */ 5920 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5921 5922 if ((control_dev != 0) 5923 && (lun->mode_pages.index[i].page_flags & 5924 CTL_PAGE_FLAG_DISK_ONLY)) 5925 continue; 5926 5927 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5928 (page_header->page_code & SMPH_PC_MASK)) 5929 continue; 5930 5931 /* 5932 * If neither page has a subpage code, then we've got a 5933 * match. 5934 */ 5935 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 5936 && ((page_header->page_code & SMPH_SPF) == 0)) { 5937 page_index = &lun->mode_pages.index[i]; 5938 page_len = page_header->page_length; 5939 break; 5940 } 5941 5942 /* 5943 * If both pages have subpages, then the subpage numbers 5944 * have to match. 5945 */ 5946 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 5947 && (page_header->page_code & SMPH_SPF)) { 5948 struct scsi_mode_page_header_sp *sph; 5949 5950 sph = (struct scsi_mode_page_header_sp *)page_header; 5951 5952 if (lun->mode_pages.index[i].subpage == 5953 sph->subpage) { 5954 page_index = &lun->mode_pages.index[i]; 5955 page_len = scsi_2btoul(sph->page_length); 5956 break; 5957 } 5958 } 5959 } 5960 5961 /* 5962 * If we couldn't find the page, or if we don't have a mode select 5963 * handler for it, send back an error to the user. 5964 */ 5965 if ((page_index == NULL) 5966 || (page_index->select_handler == NULL)) { 5967 ctl_set_invalid_field(ctsio, 5968 /*sks_valid*/ 1, 5969 /*command*/ 0, 5970 /*field*/ *len_used, 5971 /*bit_valid*/ 0, 5972 /*bit*/ 0); 5973 free(ctsio->kern_data_ptr, M_CTL); 5974 ctl_done((union ctl_io *)ctsio); 5975 return (CTL_RETVAL_COMPLETE); 5976 } 5977 5978 if (page_index->page_code & SMPH_SPF) { 5979 page_len_offset = 2; 5980 page_len_size = 2; 5981 } else { 5982 page_len_size = 1; 5983 page_len_offset = 1; 5984 } 5985 5986 /* 5987 * If the length the initiator gives us isn't the one we specify in 5988 * the mode page header, or if they didn't specify enough data in 5989 * the CDB to avoid truncating this page, kick out the request. 5990 */ 5991 if ((page_len != (page_index->page_len - page_len_offset - 5992 page_len_size)) 5993 || (*len_left < page_index->page_len)) { 5994 5995 5996 ctl_set_invalid_field(ctsio, 5997 /*sks_valid*/ 1, 5998 /*command*/ 0, 5999 /*field*/ *len_used + page_len_offset, 6000 /*bit_valid*/ 0, 6001 /*bit*/ 0); 6002 free(ctsio->kern_data_ptr, M_CTL); 6003 ctl_done((union ctl_io *)ctsio); 6004 return (CTL_RETVAL_COMPLETE); 6005 } 6006 6007 /* 6008 * Run through the mode page, checking to make sure that the bits 6009 * the user changed are actually legal for him to change. 6010 */ 6011 for (i = 0; i < page_index->page_len; i++) { 6012 uint8_t *user_byte, *change_mask, *current_byte; 6013 int bad_bit; 6014 int j; 6015 6016 user_byte = (uint8_t *)page_header + i; 6017 change_mask = page_index->page_data + 6018 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6019 current_byte = page_index->page_data + 6020 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6021 6022 /* 6023 * Check to see whether the user set any bits in this byte 6024 * that he is not allowed to set. 6025 */ 6026 if ((*user_byte & ~(*change_mask)) == 6027 (*current_byte & ~(*change_mask))) 6028 continue; 6029 6030 /* 6031 * Go through bit by bit to determine which one is illegal. 6032 */ 6033 bad_bit = 0; 6034 for (j = 7; j >= 0; j--) { 6035 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6036 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6037 bad_bit = i; 6038 break; 6039 } 6040 } 6041 ctl_set_invalid_field(ctsio, 6042 /*sks_valid*/ 1, 6043 /*command*/ 0, 6044 /*field*/ *len_used + i, 6045 /*bit_valid*/ 1, 6046 /*bit*/ bad_bit); 6047 free(ctsio->kern_data_ptr, M_CTL); 6048 ctl_done((union ctl_io *)ctsio); 6049 return (CTL_RETVAL_COMPLETE); 6050 } 6051 6052 /* 6053 * Decrement these before we call the page handler, since we may 6054 * end up getting called back one way or another before the handler 6055 * returns to this context. 6056 */ 6057 *len_left -= page_index->page_len; 6058 *len_used += page_index->page_len; 6059 6060 retval = page_index->select_handler(ctsio, page_index, 6061 (uint8_t *)page_header); 6062 6063 /* 6064 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6065 * wait until this queued command completes to finish processing 6066 * the mode page. If it returns anything other than 6067 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6068 * already set the sense information, freed the data pointer, and 6069 * completed the io for us. 6070 */ 6071 if (retval != CTL_RETVAL_COMPLETE) 6072 goto bailout_no_done; 6073 6074 /* 6075 * If the initiator sent us more than one page, parse the next one. 6076 */ 6077 if (*len_left > 0) 6078 goto do_next_page; 6079 6080 ctl_set_success(ctsio); 6081 free(ctsio->kern_data_ptr, M_CTL); 6082 ctl_done((union ctl_io *)ctsio); 6083 6084bailout_no_done: 6085 6086 return (CTL_RETVAL_COMPLETE); 6087 6088} 6089 6090int 6091ctl_mode_select(struct ctl_scsiio *ctsio) 6092{ 6093 int param_len, pf, sp; 6094 int header_size, bd_len; 6095 int len_left, len_used; 6096 struct ctl_page_index *page_index; 6097 struct ctl_lun *lun; 6098 int control_dev, page_len; 6099 union ctl_modepage_info *modepage_info; 6100 int retval; 6101 6102 pf = 0; 6103 sp = 0; 6104 page_len = 0; 6105 len_used = 0; 6106 len_left = 0; 6107 retval = 0; 6108 bd_len = 0; 6109 page_index = NULL; 6110 6111 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6112 6113 if (lun->be_lun->lun_type != T_DIRECT) 6114 control_dev = 1; 6115 else 6116 control_dev = 0; 6117 6118 switch (ctsio->cdb[0]) { 6119 case MODE_SELECT_6: { 6120 struct scsi_mode_select_6 *cdb; 6121 6122 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6123 6124 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6125 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6126 6127 param_len = cdb->length; 6128 header_size = sizeof(struct scsi_mode_header_6); 6129 break; 6130 } 6131 case MODE_SELECT_10: { 6132 struct scsi_mode_select_10 *cdb; 6133 6134 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6135 6136 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6137 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6138 6139 param_len = scsi_2btoul(cdb->length); 6140 header_size = sizeof(struct scsi_mode_header_10); 6141 break; 6142 } 6143 default: 6144 ctl_set_invalid_opcode(ctsio); 6145 ctl_done((union ctl_io *)ctsio); 6146 return (CTL_RETVAL_COMPLETE); 6147 break; /* NOTREACHED */ 6148 } 6149 6150 /* 6151 * From SPC-3: 6152 * "A parameter list length of zero indicates that the Data-Out Buffer 6153 * shall be empty. This condition shall not be considered as an error." 6154 */ 6155 if (param_len == 0) { 6156 ctl_set_success(ctsio); 6157 ctl_done((union ctl_io *)ctsio); 6158 return (CTL_RETVAL_COMPLETE); 6159 } 6160 6161 /* 6162 * Since we'll hit this the first time through, prior to 6163 * allocation, we don't need to free a data buffer here. 6164 */ 6165 if (param_len < header_size) { 6166 ctl_set_param_len_error(ctsio); 6167 ctl_done((union ctl_io *)ctsio); 6168 return (CTL_RETVAL_COMPLETE); 6169 } 6170 6171 /* 6172 * Allocate the data buffer and grab the user's data. In theory, 6173 * we shouldn't have to sanity check the parameter list length here 6174 * because the maximum size is 64K. We should be able to malloc 6175 * that much without too many problems. 6176 */ 6177 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6178 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6179 ctsio->kern_data_len = param_len; 6180 ctsio->kern_total_len = param_len; 6181 ctsio->kern_data_resid = 0; 6182 ctsio->kern_rel_offset = 0; 6183 ctsio->kern_sg_entries = 0; 6184 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6185 ctsio->be_move_done = ctl_config_move_done; 6186 ctl_datamove((union ctl_io *)ctsio); 6187 6188 return (CTL_RETVAL_COMPLETE); 6189 } 6190 6191 switch (ctsio->cdb[0]) { 6192 case MODE_SELECT_6: { 6193 struct scsi_mode_header_6 *mh6; 6194 6195 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6196 bd_len = mh6->blk_desc_len; 6197 break; 6198 } 6199 case MODE_SELECT_10: { 6200 struct scsi_mode_header_10 *mh10; 6201 6202 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6203 bd_len = scsi_2btoul(mh10->blk_desc_len); 6204 break; 6205 } 6206 default: 6207 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6208 break; 6209 } 6210 6211 if (param_len < (header_size + bd_len)) { 6212 free(ctsio->kern_data_ptr, M_CTL); 6213 ctl_set_param_len_error(ctsio); 6214 ctl_done((union ctl_io *)ctsio); 6215 return (CTL_RETVAL_COMPLETE); 6216 } 6217 6218 /* 6219 * Set the IO_CONT flag, so that if this I/O gets passed to 6220 * ctl_config_write_done(), it'll get passed back to 6221 * ctl_do_mode_select() for further processing, or completion if 6222 * we're all done. 6223 */ 6224 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6225 ctsio->io_cont = ctl_do_mode_select; 6226 6227 modepage_info = (union ctl_modepage_info *) 6228 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6229 6230 memset(modepage_info, 0, sizeof(*modepage_info)); 6231 6232 len_left = param_len - header_size - bd_len; 6233 len_used = header_size + bd_len; 6234 6235 modepage_info->header.len_left = len_left; 6236 modepage_info->header.len_used = len_used; 6237 6238 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6239} 6240 6241int 6242ctl_mode_sense(struct ctl_scsiio *ctsio) 6243{ 6244 struct ctl_lun *lun; 6245 int pc, page_code, dbd, llba, subpage; 6246 int alloc_len, page_len, header_len, total_len; 6247 struct scsi_mode_block_descr *block_desc; 6248 struct ctl_page_index *page_index; 6249 int control_dev; 6250 6251 dbd = 0; 6252 llba = 0; 6253 block_desc = NULL; 6254 page_index = NULL; 6255 6256 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6257 6258 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6259 6260 if (lun->be_lun->lun_type != T_DIRECT) 6261 control_dev = 1; 6262 else 6263 control_dev = 0; 6264 6265 switch (ctsio->cdb[0]) { 6266 case MODE_SENSE_6: { 6267 struct scsi_mode_sense_6 *cdb; 6268 6269 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6270 6271 header_len = sizeof(struct scsi_mode_hdr_6); 6272 if (cdb->byte2 & SMS_DBD) 6273 dbd = 1; 6274 else 6275 header_len += sizeof(struct scsi_mode_block_descr); 6276 6277 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6278 page_code = cdb->page & SMS_PAGE_CODE; 6279 subpage = cdb->subpage; 6280 alloc_len = cdb->length; 6281 break; 6282 } 6283 case MODE_SENSE_10: { 6284 struct scsi_mode_sense_10 *cdb; 6285 6286 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6287 6288 header_len = sizeof(struct scsi_mode_hdr_10); 6289 6290 if (cdb->byte2 & SMS_DBD) 6291 dbd = 1; 6292 else 6293 header_len += sizeof(struct scsi_mode_block_descr); 6294 if (cdb->byte2 & SMS10_LLBAA) 6295 llba = 1; 6296 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6297 page_code = cdb->page & SMS_PAGE_CODE; 6298 subpage = cdb->subpage; 6299 alloc_len = scsi_2btoul(cdb->length); 6300 break; 6301 } 6302 default: 6303 ctl_set_invalid_opcode(ctsio); 6304 ctl_done((union ctl_io *)ctsio); 6305 return (CTL_RETVAL_COMPLETE); 6306 break; /* NOTREACHED */ 6307 } 6308 6309 /* 6310 * We have to make a first pass through to calculate the size of 6311 * the pages that match the user's query. Then we allocate enough 6312 * memory to hold it, and actually copy the data into the buffer. 6313 */ 6314 switch (page_code) { 6315 case SMS_ALL_PAGES_PAGE: { 6316 int i; 6317 6318 page_len = 0; 6319 6320 /* 6321 * At the moment, values other than 0 and 0xff here are 6322 * reserved according to SPC-3. 6323 */ 6324 if ((subpage != SMS_SUBPAGE_PAGE_0) 6325 && (subpage != SMS_SUBPAGE_ALL)) { 6326 ctl_set_invalid_field(ctsio, 6327 /*sks_valid*/ 1, 6328 /*command*/ 1, 6329 /*field*/ 3, 6330 /*bit_valid*/ 0, 6331 /*bit*/ 0); 6332 ctl_done((union ctl_io *)ctsio); 6333 return (CTL_RETVAL_COMPLETE); 6334 } 6335 6336 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6337 if ((control_dev != 0) 6338 && (lun->mode_pages.index[i].page_flags & 6339 CTL_PAGE_FLAG_DISK_ONLY)) 6340 continue; 6341 6342 /* 6343 * We don't use this subpage if the user didn't 6344 * request all subpages. 6345 */ 6346 if ((lun->mode_pages.index[i].subpage != 0) 6347 && (subpage == SMS_SUBPAGE_PAGE_0)) 6348 continue; 6349 6350#if 0 6351 printf("found page %#x len %d\n", 6352 lun->mode_pages.index[i].page_code & 6353 SMPH_PC_MASK, 6354 lun->mode_pages.index[i].page_len); 6355#endif 6356 page_len += lun->mode_pages.index[i].page_len; 6357 } 6358 break; 6359 } 6360 default: { 6361 int i; 6362 6363 page_len = 0; 6364 6365 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6366 /* Look for the right page code */ 6367 if ((lun->mode_pages.index[i].page_code & 6368 SMPH_PC_MASK) != page_code) 6369 continue; 6370 6371 /* Look for the right subpage or the subpage wildcard*/ 6372 if ((lun->mode_pages.index[i].subpage != subpage) 6373 && (subpage != SMS_SUBPAGE_ALL)) 6374 continue; 6375 6376 /* Make sure the page is supported for this dev type */ 6377 if ((control_dev != 0) 6378 && (lun->mode_pages.index[i].page_flags & 6379 CTL_PAGE_FLAG_DISK_ONLY)) 6380 continue; 6381 6382#if 0 6383 printf("found page %#x len %d\n", 6384 lun->mode_pages.index[i].page_code & 6385 SMPH_PC_MASK, 6386 lun->mode_pages.index[i].page_len); 6387#endif 6388 6389 page_len += lun->mode_pages.index[i].page_len; 6390 } 6391 6392 if (page_len == 0) { 6393 ctl_set_invalid_field(ctsio, 6394 /*sks_valid*/ 1, 6395 /*command*/ 1, 6396 /*field*/ 2, 6397 /*bit_valid*/ 1, 6398 /*bit*/ 5); 6399 ctl_done((union ctl_io *)ctsio); 6400 return (CTL_RETVAL_COMPLETE); 6401 } 6402 break; 6403 } 6404 } 6405 6406 total_len = header_len + page_len; 6407#if 0 6408 printf("header_len = %d, page_len = %d, total_len = %d\n", 6409 header_len, page_len, total_len); 6410#endif 6411 6412 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6413 ctsio->kern_sg_entries = 0; 6414 ctsio->kern_data_resid = 0; 6415 ctsio->kern_rel_offset = 0; 6416 if (total_len < alloc_len) { 6417 ctsio->residual = alloc_len - total_len; 6418 ctsio->kern_data_len = total_len; 6419 ctsio->kern_total_len = total_len; 6420 } else { 6421 ctsio->residual = 0; 6422 ctsio->kern_data_len = alloc_len; 6423 ctsio->kern_total_len = alloc_len; 6424 } 6425 6426 switch (ctsio->cdb[0]) { 6427 case MODE_SENSE_6: { 6428 struct scsi_mode_hdr_6 *header; 6429 6430 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6431 6432 header->datalen = MIN(total_len - 1, 254); 6433 if (control_dev == 0) { 6434 header->dev_specific = 0x10; /* DPOFUA */ 6435 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6436 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6437 .eca_and_aen & SCP_SWP) != 0) 6438 header->dev_specific |= 0x80; /* WP */ 6439 } 6440 if (dbd) 6441 header->block_descr_len = 0; 6442 else 6443 header->block_descr_len = 6444 sizeof(struct scsi_mode_block_descr); 6445 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6446 break; 6447 } 6448 case MODE_SENSE_10: { 6449 struct scsi_mode_hdr_10 *header; 6450 int datalen; 6451 6452 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6453 6454 datalen = MIN(total_len - 2, 65533); 6455 scsi_ulto2b(datalen, header->datalen); 6456 if (control_dev == 0) { 6457 header->dev_specific = 0x10; /* DPOFUA */ 6458 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6459 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6460 .eca_and_aen & SCP_SWP) != 0) 6461 header->dev_specific |= 0x80; /* WP */ 6462 } 6463 if (dbd) 6464 scsi_ulto2b(0, header->block_descr_len); 6465 else 6466 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6467 header->block_descr_len); 6468 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6469 break; 6470 } 6471 default: 6472 panic("invalid CDB type %#x", ctsio->cdb[0]); 6473 break; /* NOTREACHED */ 6474 } 6475 6476 /* 6477 * If we've got a disk, use its blocksize in the block 6478 * descriptor. Otherwise, just set it to 0. 6479 */ 6480 if (dbd == 0) { 6481 if (control_dev == 0) 6482 scsi_ulto3b(lun->be_lun->blocksize, 6483 block_desc->block_len); 6484 else 6485 scsi_ulto3b(0, block_desc->block_len); 6486 } 6487 6488 switch (page_code) { 6489 case SMS_ALL_PAGES_PAGE: { 6490 int i, data_used; 6491 6492 data_used = header_len; 6493 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6494 struct ctl_page_index *page_index; 6495 6496 page_index = &lun->mode_pages.index[i]; 6497 6498 if ((control_dev != 0) 6499 && (page_index->page_flags & 6500 CTL_PAGE_FLAG_DISK_ONLY)) 6501 continue; 6502 6503 /* 6504 * We don't use this subpage if the user didn't 6505 * request all subpages. We already checked (above) 6506 * to make sure the user only specified a subpage 6507 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6508 */ 6509 if ((page_index->subpage != 0) 6510 && (subpage == SMS_SUBPAGE_PAGE_0)) 6511 continue; 6512 6513 /* 6514 * Call the handler, if it exists, to update the 6515 * page to the latest values. 6516 */ 6517 if (page_index->sense_handler != NULL) 6518 page_index->sense_handler(ctsio, page_index,pc); 6519 6520 memcpy(ctsio->kern_data_ptr + data_used, 6521 page_index->page_data + 6522 (page_index->page_len * pc), 6523 page_index->page_len); 6524 data_used += page_index->page_len; 6525 } 6526 break; 6527 } 6528 default: { 6529 int i, data_used; 6530 6531 data_used = header_len; 6532 6533 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6534 struct ctl_page_index *page_index; 6535 6536 page_index = &lun->mode_pages.index[i]; 6537 6538 /* Look for the right page code */ 6539 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6540 continue; 6541 6542 /* Look for the right subpage or the subpage wildcard*/ 6543 if ((page_index->subpage != subpage) 6544 && (subpage != SMS_SUBPAGE_ALL)) 6545 continue; 6546 6547 /* Make sure the page is supported for this dev type */ 6548 if ((control_dev != 0) 6549 && (page_index->page_flags & 6550 CTL_PAGE_FLAG_DISK_ONLY)) 6551 continue; 6552 6553 /* 6554 * Call the handler, if it exists, to update the 6555 * page to the latest values. 6556 */ 6557 if (page_index->sense_handler != NULL) 6558 page_index->sense_handler(ctsio, page_index,pc); 6559 6560 memcpy(ctsio->kern_data_ptr + data_used, 6561 page_index->page_data + 6562 (page_index->page_len * pc), 6563 page_index->page_len); 6564 data_used += page_index->page_len; 6565 } 6566 break; 6567 } 6568 } 6569 6570 ctl_set_success(ctsio); 6571 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6572 ctsio->be_move_done = ctl_config_move_done; 6573 ctl_datamove((union ctl_io *)ctsio); 6574 return (CTL_RETVAL_COMPLETE); 6575} 6576 6577int 6578ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6579 struct ctl_page_index *page_index, 6580 int pc) 6581{ 6582 struct ctl_lun *lun; 6583 struct scsi_log_param_header *phdr; 6584 uint8_t *data; 6585 uint64_t val; 6586 6587 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6588 data = page_index->page_data; 6589 6590 if (lun->backend->lun_attr != NULL && 6591 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6592 != UINT64_MAX) { 6593 phdr = (struct scsi_log_param_header *)data; 6594 scsi_ulto2b(0x0001, phdr->param_code); 6595 phdr->param_control = SLP_LBIN | SLP_LP; 6596 phdr->param_len = 8; 6597 data = (uint8_t *)(phdr + 1); 6598 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6599 data[4] = 0x02; /* per-pool */ 6600 data += phdr->param_len; 6601 } 6602 6603 if (lun->backend->lun_attr != NULL && 6604 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6605 != UINT64_MAX) { 6606 phdr = (struct scsi_log_param_header *)data; 6607 scsi_ulto2b(0x0002, phdr->param_code); 6608 phdr->param_control = SLP_LBIN | SLP_LP; 6609 phdr->param_len = 8; 6610 data = (uint8_t *)(phdr + 1); 6611 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6612 data[4] = 0x01; /* per-LUN */ 6613 data += phdr->param_len; 6614 } 6615 6616 if (lun->backend->lun_attr != NULL && 6617 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6618 != UINT64_MAX) { 6619 phdr = (struct scsi_log_param_header *)data; 6620 scsi_ulto2b(0x00f1, phdr->param_code); 6621 phdr->param_control = SLP_LBIN | SLP_LP; 6622 phdr->param_len = 8; 6623 data = (uint8_t *)(phdr + 1); 6624 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6625 data[4] = 0x02; /* per-pool */ 6626 data += phdr->param_len; 6627 } 6628 6629 if (lun->backend->lun_attr != NULL && 6630 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6631 != UINT64_MAX) { 6632 phdr = (struct scsi_log_param_header *)data; 6633 scsi_ulto2b(0x00f2, phdr->param_code); 6634 phdr->param_control = SLP_LBIN | SLP_LP; 6635 phdr->param_len = 8; 6636 data = (uint8_t *)(phdr + 1); 6637 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6638 data[4] = 0x02; /* per-pool */ 6639 data += phdr->param_len; 6640 } 6641 6642 page_index->page_len = data - page_index->page_data; 6643 return (0); 6644} 6645 6646int 6647ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6648 struct ctl_page_index *page_index, 6649 int pc) 6650{ 6651 struct ctl_lun *lun; 6652 struct stat_page *data; 6653 uint64_t rn, wn, rb, wb; 6654 struct bintime rt, wt; 6655 int i; 6656 6657 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6658 data = (struct stat_page *)page_index->page_data; 6659 6660 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6661 data->sap.hdr.param_control = SLP_LBIN; 6662 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6663 sizeof(struct scsi_log_param_header); 6664 rn = wn = rb = wb = 0; 6665 bintime_clear(&rt); 6666 bintime_clear(&wt); 6667 for (i = 0; i < CTL_MAX_PORTS; i++) { 6668 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6669 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6670 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6671 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6672 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6673 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6674 } 6675 scsi_u64to8b(rn, data->sap.read_num); 6676 scsi_u64to8b(wn, data->sap.write_num); 6677 if (lun->stats.blocksize > 0) { 6678 scsi_u64to8b(wb / lun->stats.blocksize, 6679 data->sap.recvieved_lba); 6680 scsi_u64to8b(rb / lun->stats.blocksize, 6681 data->sap.transmitted_lba); 6682 } 6683 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6684 data->sap.read_int); 6685 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6686 data->sap.write_int); 6687 scsi_u64to8b(0, data->sap.weighted_num); 6688 scsi_u64to8b(0, data->sap.weighted_int); 6689 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6690 data->it.hdr.param_control = SLP_LBIN; 6691 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6692 sizeof(struct scsi_log_param_header); 6693#ifdef CTL_TIME_IO 6694 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6695#endif 6696 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6697 data->it.hdr.param_control = SLP_LBIN; 6698 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6699 sizeof(struct scsi_log_param_header); 6700 scsi_ulto4b(3, data->ti.exponent); 6701 scsi_ulto4b(1, data->ti.integer); 6702 6703 page_index->page_len = sizeof(*data); 6704 return (0); 6705} 6706 6707int 6708ctl_log_sense(struct ctl_scsiio *ctsio) 6709{ 6710 struct ctl_lun *lun; 6711 int i, pc, page_code, subpage; 6712 int alloc_len, total_len; 6713 struct ctl_page_index *page_index; 6714 struct scsi_log_sense *cdb; 6715 struct scsi_log_header *header; 6716 6717 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6718 6719 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6720 cdb = (struct scsi_log_sense *)ctsio->cdb; 6721 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6722 page_code = cdb->page & SLS_PAGE_CODE; 6723 subpage = cdb->subpage; 6724 alloc_len = scsi_2btoul(cdb->length); 6725 6726 page_index = NULL; 6727 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6728 page_index = &lun->log_pages.index[i]; 6729 6730 /* Look for the right page code */ 6731 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6732 continue; 6733 6734 /* Look for the right subpage or the subpage wildcard*/ 6735 if (page_index->subpage != subpage) 6736 continue; 6737 6738 break; 6739 } 6740 if (i >= CTL_NUM_LOG_PAGES) { 6741 ctl_set_invalid_field(ctsio, 6742 /*sks_valid*/ 1, 6743 /*command*/ 1, 6744 /*field*/ 2, 6745 /*bit_valid*/ 0, 6746 /*bit*/ 0); 6747 ctl_done((union ctl_io *)ctsio); 6748 return (CTL_RETVAL_COMPLETE); 6749 } 6750 6751 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6752 6753 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6754 ctsio->kern_sg_entries = 0; 6755 ctsio->kern_data_resid = 0; 6756 ctsio->kern_rel_offset = 0; 6757 if (total_len < alloc_len) { 6758 ctsio->residual = alloc_len - total_len; 6759 ctsio->kern_data_len = total_len; 6760 ctsio->kern_total_len = total_len; 6761 } else { 6762 ctsio->residual = 0; 6763 ctsio->kern_data_len = alloc_len; 6764 ctsio->kern_total_len = alloc_len; 6765 } 6766 6767 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6768 header->page = page_index->page_code; 6769 if (page_index->subpage) { 6770 header->page |= SL_SPF; 6771 header->subpage = page_index->subpage; 6772 } 6773 scsi_ulto2b(page_index->page_len, header->datalen); 6774 6775 /* 6776 * Call the handler, if it exists, to update the 6777 * page to the latest values. 6778 */ 6779 if (page_index->sense_handler != NULL) 6780 page_index->sense_handler(ctsio, page_index, pc); 6781 6782 memcpy(header + 1, page_index->page_data, page_index->page_len); 6783 6784 ctl_set_success(ctsio); 6785 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6786 ctsio->be_move_done = ctl_config_move_done; 6787 ctl_datamove((union ctl_io *)ctsio); 6788 return (CTL_RETVAL_COMPLETE); 6789} 6790 6791int 6792ctl_read_capacity(struct ctl_scsiio *ctsio) 6793{ 6794 struct scsi_read_capacity *cdb; 6795 struct scsi_read_capacity_data *data; 6796 struct ctl_lun *lun; 6797 uint32_t lba; 6798 6799 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6800 6801 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6802 6803 lba = scsi_4btoul(cdb->addr); 6804 if (((cdb->pmi & SRC_PMI) == 0) 6805 && (lba != 0)) { 6806 ctl_set_invalid_field(/*ctsio*/ ctsio, 6807 /*sks_valid*/ 1, 6808 /*command*/ 1, 6809 /*field*/ 2, 6810 /*bit_valid*/ 0, 6811 /*bit*/ 0); 6812 ctl_done((union ctl_io *)ctsio); 6813 return (CTL_RETVAL_COMPLETE); 6814 } 6815 6816 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6817 6818 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6819 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6820 ctsio->residual = 0; 6821 ctsio->kern_data_len = sizeof(*data); 6822 ctsio->kern_total_len = sizeof(*data); 6823 ctsio->kern_data_resid = 0; 6824 ctsio->kern_rel_offset = 0; 6825 ctsio->kern_sg_entries = 0; 6826 6827 /* 6828 * If the maximum LBA is greater than 0xfffffffe, the user must 6829 * issue a SERVICE ACTION IN (16) command, with the read capacity 6830 * serivce action set. 6831 */ 6832 if (lun->be_lun->maxlba > 0xfffffffe) 6833 scsi_ulto4b(0xffffffff, data->addr); 6834 else 6835 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6836 6837 /* 6838 * XXX KDM this may not be 512 bytes... 6839 */ 6840 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6841 6842 ctl_set_success(ctsio); 6843 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6844 ctsio->be_move_done = ctl_config_move_done; 6845 ctl_datamove((union ctl_io *)ctsio); 6846 return (CTL_RETVAL_COMPLETE); 6847} 6848 6849int 6850ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6851{ 6852 struct scsi_read_capacity_16 *cdb; 6853 struct scsi_read_capacity_data_long *data; 6854 struct ctl_lun *lun; 6855 uint64_t lba; 6856 uint32_t alloc_len; 6857 6858 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6859 6860 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6861 6862 alloc_len = scsi_4btoul(cdb->alloc_len); 6863 lba = scsi_8btou64(cdb->addr); 6864 6865 if ((cdb->reladr & SRC16_PMI) 6866 && (lba != 0)) { 6867 ctl_set_invalid_field(/*ctsio*/ ctsio, 6868 /*sks_valid*/ 1, 6869 /*command*/ 1, 6870 /*field*/ 2, 6871 /*bit_valid*/ 0, 6872 /*bit*/ 0); 6873 ctl_done((union ctl_io *)ctsio); 6874 return (CTL_RETVAL_COMPLETE); 6875 } 6876 6877 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6878 6879 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6880 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6881 6882 if (sizeof(*data) < alloc_len) { 6883 ctsio->residual = alloc_len - sizeof(*data); 6884 ctsio->kern_data_len = sizeof(*data); 6885 ctsio->kern_total_len = sizeof(*data); 6886 } else { 6887 ctsio->residual = 0; 6888 ctsio->kern_data_len = alloc_len; 6889 ctsio->kern_total_len = alloc_len; 6890 } 6891 ctsio->kern_data_resid = 0; 6892 ctsio->kern_rel_offset = 0; 6893 ctsio->kern_sg_entries = 0; 6894 6895 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6896 /* XXX KDM this may not be 512 bytes... */ 6897 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6898 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6899 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6900 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6901 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6902 6903 ctl_set_success(ctsio); 6904 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6905 ctsio->be_move_done = ctl_config_move_done; 6906 ctl_datamove((union ctl_io *)ctsio); 6907 return (CTL_RETVAL_COMPLETE); 6908} 6909 6910int 6911ctl_get_lba_status(struct ctl_scsiio *ctsio) 6912{ 6913 struct scsi_get_lba_status *cdb; 6914 struct scsi_get_lba_status_data *data; 6915 struct ctl_lun *lun; 6916 struct ctl_lba_len_flags *lbalen; 6917 uint64_t lba; 6918 uint32_t alloc_len, total_len; 6919 int retval; 6920 6921 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6922 6923 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6924 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6925 lba = scsi_8btou64(cdb->addr); 6926 alloc_len = scsi_4btoul(cdb->alloc_len); 6927 6928 if (lba > lun->be_lun->maxlba) { 6929 ctl_set_lba_out_of_range(ctsio); 6930 ctl_done((union ctl_io *)ctsio); 6931 return (CTL_RETVAL_COMPLETE); 6932 } 6933 6934 total_len = sizeof(*data) + sizeof(data->descr[0]); 6935 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6936 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6937 6938 if (total_len < alloc_len) { 6939 ctsio->residual = alloc_len - total_len; 6940 ctsio->kern_data_len = total_len; 6941 ctsio->kern_total_len = total_len; 6942 } else { 6943 ctsio->residual = 0; 6944 ctsio->kern_data_len = alloc_len; 6945 ctsio->kern_total_len = alloc_len; 6946 } 6947 ctsio->kern_data_resid = 0; 6948 ctsio->kern_rel_offset = 0; 6949 ctsio->kern_sg_entries = 0; 6950 6951 /* Fill dummy data in case backend can't tell anything. */ 6952 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6953 scsi_u64to8b(lba, data->descr[0].addr); 6954 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6955 data->descr[0].length); 6956 data->descr[0].status = 0; /* Mapped or unknown. */ 6957 6958 ctl_set_success(ctsio); 6959 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6960 ctsio->be_move_done = ctl_config_move_done; 6961 6962 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6963 lbalen->lba = lba; 6964 lbalen->len = total_len; 6965 lbalen->flags = 0; 6966 retval = lun->backend->config_read((union ctl_io *)ctsio); 6967 return (CTL_RETVAL_COMPLETE); 6968} 6969 6970int 6971ctl_read_defect(struct ctl_scsiio *ctsio) 6972{ 6973 struct scsi_read_defect_data_10 *ccb10; 6974 struct scsi_read_defect_data_12 *ccb12; 6975 struct scsi_read_defect_data_hdr_10 *data10; 6976 struct scsi_read_defect_data_hdr_12 *data12; 6977 uint32_t alloc_len, data_len; 6978 uint8_t format; 6979 6980 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 6981 6982 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 6983 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 6984 format = ccb10->format; 6985 alloc_len = scsi_2btoul(ccb10->alloc_length); 6986 data_len = sizeof(*data10); 6987 } else { 6988 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 6989 format = ccb12->format; 6990 alloc_len = scsi_4btoul(ccb12->alloc_length); 6991 data_len = sizeof(*data12); 6992 } 6993 if (alloc_len == 0) { 6994 ctl_set_success(ctsio); 6995 ctl_done((union ctl_io *)ctsio); 6996 return (CTL_RETVAL_COMPLETE); 6997 } 6998 6999 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7000 if (data_len < alloc_len) { 7001 ctsio->residual = alloc_len - data_len; 7002 ctsio->kern_data_len = data_len; 7003 ctsio->kern_total_len = data_len; 7004 } else { 7005 ctsio->residual = 0; 7006 ctsio->kern_data_len = alloc_len; 7007 ctsio->kern_total_len = alloc_len; 7008 } 7009 ctsio->kern_data_resid = 0; 7010 ctsio->kern_rel_offset = 0; 7011 ctsio->kern_sg_entries = 0; 7012 7013 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7014 data10 = (struct scsi_read_defect_data_hdr_10 *) 7015 ctsio->kern_data_ptr; 7016 data10->format = format; 7017 scsi_ulto2b(0, data10->length); 7018 } else { 7019 data12 = (struct scsi_read_defect_data_hdr_12 *) 7020 ctsio->kern_data_ptr; 7021 data12->format = format; 7022 scsi_ulto2b(0, data12->generation); 7023 scsi_ulto4b(0, data12->length); 7024 } 7025 7026 ctl_set_success(ctsio); 7027 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7028 ctsio->be_move_done = ctl_config_move_done; 7029 ctl_datamove((union ctl_io *)ctsio); 7030 return (CTL_RETVAL_COMPLETE); 7031} 7032 7033int 7034ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7035{ 7036 struct scsi_maintenance_in *cdb; 7037 int retval; 7038 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7039 int num_target_port_groups, num_target_ports; 7040 struct ctl_lun *lun; 7041 struct ctl_softc *softc; 7042 struct ctl_port *port; 7043 struct scsi_target_group_data *rtg_ptr; 7044 struct scsi_target_group_data_extended *rtg_ext_ptr; 7045 struct scsi_target_port_group_descriptor *tpg_desc; 7046 7047 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7048 7049 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7051 softc = lun->ctl_softc; 7052 7053 retval = CTL_RETVAL_COMPLETE; 7054 7055 switch (cdb->byte2 & STG_PDF_MASK) { 7056 case STG_PDF_LENGTH: 7057 ext = 0; 7058 break; 7059 case STG_PDF_EXTENDED: 7060 ext = 1; 7061 break; 7062 default: 7063 ctl_set_invalid_field(/*ctsio*/ ctsio, 7064 /*sks_valid*/ 1, 7065 /*command*/ 1, 7066 /*field*/ 2, 7067 /*bit_valid*/ 1, 7068 /*bit*/ 5); 7069 ctl_done((union ctl_io *)ctsio); 7070 return(retval); 7071 } 7072 7073 if (softc->is_single) 7074 num_target_port_groups = 1; 7075 else 7076 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7077 num_target_ports = 0; 7078 mtx_lock(&softc->ctl_lock); 7079 STAILQ_FOREACH(port, &softc->port_list, links) { 7080 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7081 continue; 7082 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7083 continue; 7084 num_target_ports++; 7085 } 7086 mtx_unlock(&softc->ctl_lock); 7087 7088 if (ext) 7089 total_len = sizeof(struct scsi_target_group_data_extended); 7090 else 7091 total_len = sizeof(struct scsi_target_group_data); 7092 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7093 num_target_port_groups + 7094 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7095 7096 alloc_len = scsi_4btoul(cdb->length); 7097 7098 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7099 7100 ctsio->kern_sg_entries = 0; 7101 7102 if (total_len < alloc_len) { 7103 ctsio->residual = alloc_len - total_len; 7104 ctsio->kern_data_len = total_len; 7105 ctsio->kern_total_len = total_len; 7106 } else { 7107 ctsio->residual = 0; 7108 ctsio->kern_data_len = alloc_len; 7109 ctsio->kern_total_len = alloc_len; 7110 } 7111 ctsio->kern_data_resid = 0; 7112 ctsio->kern_rel_offset = 0; 7113 7114 if (ext) { 7115 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7116 ctsio->kern_data_ptr; 7117 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7118 rtg_ext_ptr->format_type = 0x10; 7119 rtg_ext_ptr->implicit_transition_time = 0; 7120 tpg_desc = &rtg_ext_ptr->groups[0]; 7121 } else { 7122 rtg_ptr = (struct scsi_target_group_data *) 7123 ctsio->kern_data_ptr; 7124 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7125 tpg_desc = &rtg_ptr->groups[0]; 7126 } 7127 7128 mtx_lock(&softc->ctl_lock); 7129 pg = softc->port_min / softc->port_cnt; 7130 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7131 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7132 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7133 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7134 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7135 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7136 else 7137 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7138 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7139 os = gs; 7140 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7141 } else 7142 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7143 for (g = 0; g < num_target_port_groups; g++) { 7144 tpg_desc->pref_state = (g == pg) ? gs : os; 7145 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7146 TPG_U_SUP | TPG_T_SUP; 7147 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7148 tpg_desc->status = TPG_IMPLICIT; 7149 pc = 0; 7150 STAILQ_FOREACH(port, &softc->port_list, links) { 7151 if (port->targ_port < g * softc->port_cnt || 7152 port->targ_port >= (g + 1) * softc->port_cnt) 7153 continue; 7154 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7155 continue; 7156 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7157 continue; 7158 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7159 relative_target_port_identifier); 7160 pc++; 7161 } 7162 tpg_desc->target_port_count = pc; 7163 tpg_desc = (struct scsi_target_port_group_descriptor *) 7164 &tpg_desc->descriptors[pc]; 7165 } 7166 mtx_unlock(&softc->ctl_lock); 7167 7168 ctl_set_success(ctsio); 7169 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7170 ctsio->be_move_done = ctl_config_move_done; 7171 ctl_datamove((union ctl_io *)ctsio); 7172 return(retval); 7173} 7174 7175int 7176ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7177{ 7178 struct ctl_lun *lun; 7179 struct scsi_report_supported_opcodes *cdb; 7180 const struct ctl_cmd_entry *entry, *sentry; 7181 struct scsi_report_supported_opcodes_all *all; 7182 struct scsi_report_supported_opcodes_descr *descr; 7183 struct scsi_report_supported_opcodes_one *one; 7184 int retval; 7185 int alloc_len, total_len; 7186 int opcode, service_action, i, j, num; 7187 7188 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7189 7190 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7191 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7192 7193 retval = CTL_RETVAL_COMPLETE; 7194 7195 opcode = cdb->requested_opcode; 7196 service_action = scsi_2btoul(cdb->requested_service_action); 7197 switch (cdb->options & RSO_OPTIONS_MASK) { 7198 case RSO_OPTIONS_ALL: 7199 num = 0; 7200 for (i = 0; i < 256; i++) { 7201 entry = &ctl_cmd_table[i]; 7202 if (entry->flags & CTL_CMD_FLAG_SA5) { 7203 for (j = 0; j < 32; j++) { 7204 sentry = &((const struct ctl_cmd_entry *) 7205 entry->execute)[j]; 7206 if (ctl_cmd_applicable( 7207 lun->be_lun->lun_type, sentry)) 7208 num++; 7209 } 7210 } else { 7211 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7212 entry)) 7213 num++; 7214 } 7215 } 7216 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7217 num * sizeof(struct scsi_report_supported_opcodes_descr); 7218 break; 7219 case RSO_OPTIONS_OC: 7220 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7221 ctl_set_invalid_field(/*ctsio*/ ctsio, 7222 /*sks_valid*/ 1, 7223 /*command*/ 1, 7224 /*field*/ 2, 7225 /*bit_valid*/ 1, 7226 /*bit*/ 2); 7227 ctl_done((union ctl_io *)ctsio); 7228 return (CTL_RETVAL_COMPLETE); 7229 } 7230 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7231 break; 7232 case RSO_OPTIONS_OC_SA: 7233 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7234 service_action >= 32) { 7235 ctl_set_invalid_field(/*ctsio*/ ctsio, 7236 /*sks_valid*/ 1, 7237 /*command*/ 1, 7238 /*field*/ 2, 7239 /*bit_valid*/ 1, 7240 /*bit*/ 2); 7241 ctl_done((union ctl_io *)ctsio); 7242 return (CTL_RETVAL_COMPLETE); 7243 } 7244 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7245 break; 7246 default: 7247 ctl_set_invalid_field(/*ctsio*/ ctsio, 7248 /*sks_valid*/ 1, 7249 /*command*/ 1, 7250 /*field*/ 2, 7251 /*bit_valid*/ 1, 7252 /*bit*/ 2); 7253 ctl_done((union ctl_io *)ctsio); 7254 return (CTL_RETVAL_COMPLETE); 7255 } 7256 7257 alloc_len = scsi_4btoul(cdb->length); 7258 7259 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7260 7261 ctsio->kern_sg_entries = 0; 7262 7263 if (total_len < alloc_len) { 7264 ctsio->residual = alloc_len - total_len; 7265 ctsio->kern_data_len = total_len; 7266 ctsio->kern_total_len = total_len; 7267 } else { 7268 ctsio->residual = 0; 7269 ctsio->kern_data_len = alloc_len; 7270 ctsio->kern_total_len = alloc_len; 7271 } 7272 ctsio->kern_data_resid = 0; 7273 ctsio->kern_rel_offset = 0; 7274 7275 switch (cdb->options & RSO_OPTIONS_MASK) { 7276 case RSO_OPTIONS_ALL: 7277 all = (struct scsi_report_supported_opcodes_all *) 7278 ctsio->kern_data_ptr; 7279 num = 0; 7280 for (i = 0; i < 256; i++) { 7281 entry = &ctl_cmd_table[i]; 7282 if (entry->flags & CTL_CMD_FLAG_SA5) { 7283 for (j = 0; j < 32; j++) { 7284 sentry = &((const struct ctl_cmd_entry *) 7285 entry->execute)[j]; 7286 if (!ctl_cmd_applicable( 7287 lun->be_lun->lun_type, sentry)) 7288 continue; 7289 descr = &all->descr[num++]; 7290 descr->opcode = i; 7291 scsi_ulto2b(j, descr->service_action); 7292 descr->flags = RSO_SERVACTV; 7293 scsi_ulto2b(sentry->length, 7294 descr->cdb_length); 7295 } 7296 } else { 7297 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7298 entry)) 7299 continue; 7300 descr = &all->descr[num++]; 7301 descr->opcode = i; 7302 scsi_ulto2b(0, descr->service_action); 7303 descr->flags = 0; 7304 scsi_ulto2b(entry->length, descr->cdb_length); 7305 } 7306 } 7307 scsi_ulto4b( 7308 num * sizeof(struct scsi_report_supported_opcodes_descr), 7309 all->length); 7310 break; 7311 case RSO_OPTIONS_OC: 7312 one = (struct scsi_report_supported_opcodes_one *) 7313 ctsio->kern_data_ptr; 7314 entry = &ctl_cmd_table[opcode]; 7315 goto fill_one; 7316 case RSO_OPTIONS_OC_SA: 7317 one = (struct scsi_report_supported_opcodes_one *) 7318 ctsio->kern_data_ptr; 7319 entry = &ctl_cmd_table[opcode]; 7320 entry = &((const struct ctl_cmd_entry *) 7321 entry->execute)[service_action]; 7322fill_one: 7323 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7324 one->support = 3; 7325 scsi_ulto2b(entry->length, one->cdb_length); 7326 one->cdb_usage[0] = opcode; 7327 memcpy(&one->cdb_usage[1], entry->usage, 7328 entry->length - 1); 7329 } else 7330 one->support = 1; 7331 break; 7332 } 7333 7334 ctl_set_success(ctsio); 7335 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7336 ctsio->be_move_done = ctl_config_move_done; 7337 ctl_datamove((union ctl_io *)ctsio); 7338 return(retval); 7339} 7340 7341int 7342ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7343{ 7344 struct scsi_report_supported_tmf *cdb; 7345 struct scsi_report_supported_tmf_data *data; 7346 int retval; 7347 int alloc_len, total_len; 7348 7349 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7350 7351 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7352 7353 retval = CTL_RETVAL_COMPLETE; 7354 7355 total_len = sizeof(struct scsi_report_supported_tmf_data); 7356 alloc_len = scsi_4btoul(cdb->length); 7357 7358 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7359 7360 ctsio->kern_sg_entries = 0; 7361 7362 if (total_len < alloc_len) { 7363 ctsio->residual = alloc_len - total_len; 7364 ctsio->kern_data_len = total_len; 7365 ctsio->kern_total_len = total_len; 7366 } else { 7367 ctsio->residual = 0; 7368 ctsio->kern_data_len = alloc_len; 7369 ctsio->kern_total_len = alloc_len; 7370 } 7371 ctsio->kern_data_resid = 0; 7372 ctsio->kern_rel_offset = 0; 7373 7374 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7375 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7376 RST_TRS; 7377 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7378 7379 ctl_set_success(ctsio); 7380 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7381 ctsio->be_move_done = ctl_config_move_done; 7382 ctl_datamove((union ctl_io *)ctsio); 7383 return (retval); 7384} 7385 7386int 7387ctl_report_timestamp(struct ctl_scsiio *ctsio) 7388{ 7389 struct scsi_report_timestamp *cdb; 7390 struct scsi_report_timestamp_data *data; 7391 struct timeval tv; 7392 int64_t timestamp; 7393 int retval; 7394 int alloc_len, total_len; 7395 7396 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7397 7398 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7399 7400 retval = CTL_RETVAL_COMPLETE; 7401 7402 total_len = sizeof(struct scsi_report_timestamp_data); 7403 alloc_len = scsi_4btoul(cdb->length); 7404 7405 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7406 7407 ctsio->kern_sg_entries = 0; 7408 7409 if (total_len < alloc_len) { 7410 ctsio->residual = alloc_len - total_len; 7411 ctsio->kern_data_len = total_len; 7412 ctsio->kern_total_len = total_len; 7413 } else { 7414 ctsio->residual = 0; 7415 ctsio->kern_data_len = alloc_len; 7416 ctsio->kern_total_len = alloc_len; 7417 } 7418 ctsio->kern_data_resid = 0; 7419 ctsio->kern_rel_offset = 0; 7420 7421 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7422 scsi_ulto2b(sizeof(*data) - 2, data->length); 7423 data->origin = RTS_ORIG_OUTSIDE; 7424 getmicrotime(&tv); 7425 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7426 scsi_ulto4b(timestamp >> 16, data->timestamp); 7427 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7428 7429 ctl_set_success(ctsio); 7430 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7431 ctsio->be_move_done = ctl_config_move_done; 7432 ctl_datamove((union ctl_io *)ctsio); 7433 return (retval); 7434} 7435 7436int 7437ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7438{ 7439 struct scsi_per_res_in *cdb; 7440 int alloc_len, total_len = 0; 7441 /* struct scsi_per_res_in_rsrv in_data; */ 7442 struct ctl_lun *lun; 7443 struct ctl_softc *softc; 7444 uint64_t key; 7445 7446 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7447 7448 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7449 7450 alloc_len = scsi_2btoul(cdb->length); 7451 7452 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7453 softc = lun->ctl_softc; 7454 7455retry: 7456 mtx_lock(&lun->lun_lock); 7457 switch (cdb->action) { 7458 case SPRI_RK: /* read keys */ 7459 total_len = sizeof(struct scsi_per_res_in_keys) + 7460 lun->pr_key_count * 7461 sizeof(struct scsi_per_res_key); 7462 break; 7463 case SPRI_RR: /* read reservation */ 7464 if (lun->flags & CTL_LUN_PR_RESERVED) 7465 total_len = sizeof(struct scsi_per_res_in_rsrv); 7466 else 7467 total_len = sizeof(struct scsi_per_res_in_header); 7468 break; 7469 case SPRI_RC: /* report capabilities */ 7470 total_len = sizeof(struct scsi_per_res_cap); 7471 break; 7472 case SPRI_RS: /* read full status */ 7473 total_len = sizeof(struct scsi_per_res_in_header) + 7474 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7475 lun->pr_key_count; 7476 break; 7477 default: 7478 panic("Invalid PR type %x", cdb->action); 7479 } 7480 mtx_unlock(&lun->lun_lock); 7481 7482 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7483 7484 if (total_len < alloc_len) { 7485 ctsio->residual = alloc_len - total_len; 7486 ctsio->kern_data_len = total_len; 7487 ctsio->kern_total_len = total_len; 7488 } else { 7489 ctsio->residual = 0; 7490 ctsio->kern_data_len = alloc_len; 7491 ctsio->kern_total_len = alloc_len; 7492 } 7493 7494 ctsio->kern_data_resid = 0; 7495 ctsio->kern_rel_offset = 0; 7496 ctsio->kern_sg_entries = 0; 7497 7498 mtx_lock(&lun->lun_lock); 7499 switch (cdb->action) { 7500 case SPRI_RK: { // read keys 7501 struct scsi_per_res_in_keys *res_keys; 7502 int i, key_count; 7503 7504 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7505 7506 /* 7507 * We had to drop the lock to allocate our buffer, which 7508 * leaves time for someone to come in with another 7509 * persistent reservation. (That is unlikely, though, 7510 * since this should be the only persistent reservation 7511 * command active right now.) 7512 */ 7513 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7514 (lun->pr_key_count * 7515 sizeof(struct scsi_per_res_key)))){ 7516 mtx_unlock(&lun->lun_lock); 7517 free(ctsio->kern_data_ptr, M_CTL); 7518 printf("%s: reservation length changed, retrying\n", 7519 __func__); 7520 goto retry; 7521 } 7522 7523 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7524 7525 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7526 lun->pr_key_count, res_keys->header.length); 7527 7528 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7529 if ((key = ctl_get_prkey(lun, i)) == 0) 7530 continue; 7531 7532 /* 7533 * We used lun->pr_key_count to calculate the 7534 * size to allocate. If it turns out the number of 7535 * initiators with the registered flag set is 7536 * larger than that (i.e. they haven't been kept in 7537 * sync), we've got a problem. 7538 */ 7539 if (key_count >= lun->pr_key_count) { 7540#ifdef NEEDTOPORT 7541 csevent_log(CSC_CTL | CSC_SHELF_SW | 7542 CTL_PR_ERROR, 7543 csevent_LogType_Fault, 7544 csevent_AlertLevel_Yellow, 7545 csevent_FRU_ShelfController, 7546 csevent_FRU_Firmware, 7547 csevent_FRU_Unknown, 7548 "registered keys %d >= key " 7549 "count %d", key_count, 7550 lun->pr_key_count); 7551#endif 7552 key_count++; 7553 continue; 7554 } 7555 scsi_u64to8b(key, res_keys->keys[key_count].key); 7556 key_count++; 7557 } 7558 break; 7559 } 7560 case SPRI_RR: { // read reservation 7561 struct scsi_per_res_in_rsrv *res; 7562 int tmp_len, header_only; 7563 7564 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7565 7566 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7567 7568 if (lun->flags & CTL_LUN_PR_RESERVED) 7569 { 7570 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7571 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7572 res->header.length); 7573 header_only = 0; 7574 } else { 7575 tmp_len = sizeof(struct scsi_per_res_in_header); 7576 scsi_ulto4b(0, res->header.length); 7577 header_only = 1; 7578 } 7579 7580 /* 7581 * We had to drop the lock to allocate our buffer, which 7582 * leaves time for someone to come in with another 7583 * persistent reservation. (That is unlikely, though, 7584 * since this should be the only persistent reservation 7585 * command active right now.) 7586 */ 7587 if (tmp_len != total_len) { 7588 mtx_unlock(&lun->lun_lock); 7589 free(ctsio->kern_data_ptr, M_CTL); 7590 printf("%s: reservation status changed, retrying\n", 7591 __func__); 7592 goto retry; 7593 } 7594 7595 /* 7596 * No reservation held, so we're done. 7597 */ 7598 if (header_only != 0) 7599 break; 7600 7601 /* 7602 * If the registration is an All Registrants type, the key 7603 * is 0, since it doesn't really matter. 7604 */ 7605 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7606 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7607 res->data.reservation); 7608 } 7609 res->data.scopetype = lun->res_type; 7610 break; 7611 } 7612 case SPRI_RC: //report capabilities 7613 { 7614 struct scsi_per_res_cap *res_cap; 7615 uint16_t type_mask; 7616 7617 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7618 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7619 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7620 type_mask = SPRI_TM_WR_EX_AR | 7621 SPRI_TM_EX_AC_RO | 7622 SPRI_TM_WR_EX_RO | 7623 SPRI_TM_EX_AC | 7624 SPRI_TM_WR_EX | 7625 SPRI_TM_EX_AC_AR; 7626 scsi_ulto2b(type_mask, res_cap->type_mask); 7627 break; 7628 } 7629 case SPRI_RS: { // read full status 7630 struct scsi_per_res_in_full *res_status; 7631 struct scsi_per_res_in_full_desc *res_desc; 7632 struct ctl_port *port; 7633 int i, len; 7634 7635 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7636 7637 /* 7638 * We had to drop the lock to allocate our buffer, which 7639 * leaves time for someone to come in with another 7640 * persistent reservation. (That is unlikely, though, 7641 * since this should be the only persistent reservation 7642 * command active right now.) 7643 */ 7644 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7645 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7646 lun->pr_key_count)){ 7647 mtx_unlock(&lun->lun_lock); 7648 free(ctsio->kern_data_ptr, M_CTL); 7649 printf("%s: reservation length changed, retrying\n", 7650 __func__); 7651 goto retry; 7652 } 7653 7654 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7655 7656 res_desc = &res_status->desc[0]; 7657 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7658 if ((key = ctl_get_prkey(lun, i)) == 0) 7659 continue; 7660 7661 scsi_u64to8b(key, res_desc->res_key.key); 7662 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7663 (lun->pr_res_idx == i || 7664 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7665 res_desc->flags = SPRI_FULL_R_HOLDER; 7666 res_desc->scopetype = lun->res_type; 7667 } 7668 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7669 res_desc->rel_trgt_port_id); 7670 len = 0; 7671 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7672 if (port != NULL) 7673 len = ctl_create_iid(port, 7674 i % CTL_MAX_INIT_PER_PORT, 7675 res_desc->transport_id); 7676 scsi_ulto4b(len, res_desc->additional_length); 7677 res_desc = (struct scsi_per_res_in_full_desc *) 7678 &res_desc->transport_id[len]; 7679 } 7680 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7681 res_status->header.length); 7682 break; 7683 } 7684 default: 7685 /* 7686 * This is a bug, because we just checked for this above, 7687 * and should have returned an error. 7688 */ 7689 panic("Invalid PR type %x", cdb->action); 7690 break; /* NOTREACHED */ 7691 } 7692 mtx_unlock(&lun->lun_lock); 7693 7694 ctl_set_success(ctsio); 7695 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7696 ctsio->be_move_done = ctl_config_move_done; 7697 ctl_datamove((union ctl_io *)ctsio); 7698 return (CTL_RETVAL_COMPLETE); 7699} 7700 7701/* 7702 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7703 * it should return. 7704 */ 7705static int 7706ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7707 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7708 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7709 struct scsi_per_res_out_parms* param) 7710{ 7711 union ctl_ha_msg persis_io; 7712 int i; 7713 7714 mtx_lock(&lun->lun_lock); 7715 if (sa_res_key == 0) { 7716 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7717 /* validate scope and type */ 7718 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7719 SPR_LU_SCOPE) { 7720 mtx_unlock(&lun->lun_lock); 7721 ctl_set_invalid_field(/*ctsio*/ ctsio, 7722 /*sks_valid*/ 1, 7723 /*command*/ 1, 7724 /*field*/ 2, 7725 /*bit_valid*/ 1, 7726 /*bit*/ 4); 7727 ctl_done((union ctl_io *)ctsio); 7728 return (1); 7729 } 7730 7731 if (type>8 || type==2 || type==4 || type==0) { 7732 mtx_unlock(&lun->lun_lock); 7733 ctl_set_invalid_field(/*ctsio*/ ctsio, 7734 /*sks_valid*/ 1, 7735 /*command*/ 1, 7736 /*field*/ 2, 7737 /*bit_valid*/ 1, 7738 /*bit*/ 0); 7739 ctl_done((union ctl_io *)ctsio); 7740 return (1); 7741 } 7742 7743 /* 7744 * Unregister everybody else and build UA for 7745 * them 7746 */ 7747 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7748 if (i == residx || ctl_get_prkey(lun, i) == 0) 7749 continue; 7750 7751 ctl_clr_prkey(lun, i); 7752 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7753 } 7754 lun->pr_key_count = 1; 7755 lun->res_type = type; 7756 if (lun->res_type != SPR_TYPE_WR_EX_AR 7757 && lun->res_type != SPR_TYPE_EX_AC_AR) 7758 lun->pr_res_idx = residx; 7759 lun->PRGeneration++; 7760 mtx_unlock(&lun->lun_lock); 7761 7762 /* send msg to other side */ 7763 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7764 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7765 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7766 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7767 persis_io.pr.pr_info.res_type = type; 7768 memcpy(persis_io.pr.pr_info.sa_res_key, 7769 param->serv_act_res_key, 7770 sizeof(param->serv_act_res_key)); 7771 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7772 sizeof(persis_io.pr), M_WAITOK); 7773 } else { 7774 /* not all registrants */ 7775 mtx_unlock(&lun->lun_lock); 7776 free(ctsio->kern_data_ptr, M_CTL); 7777 ctl_set_invalid_field(ctsio, 7778 /*sks_valid*/ 1, 7779 /*command*/ 0, 7780 /*field*/ 8, 7781 /*bit_valid*/ 0, 7782 /*bit*/ 0); 7783 ctl_done((union ctl_io *)ctsio); 7784 return (1); 7785 } 7786 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7787 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7788 int found = 0; 7789 7790 if (res_key == sa_res_key) { 7791 /* special case */ 7792 /* 7793 * The spec implies this is not good but doesn't 7794 * say what to do. There are two choices either 7795 * generate a res conflict or check condition 7796 * with illegal field in parameter data. Since 7797 * that is what is done when the sa_res_key is 7798 * zero I'll take that approach since this has 7799 * to do with the sa_res_key. 7800 */ 7801 mtx_unlock(&lun->lun_lock); 7802 free(ctsio->kern_data_ptr, M_CTL); 7803 ctl_set_invalid_field(ctsio, 7804 /*sks_valid*/ 1, 7805 /*command*/ 0, 7806 /*field*/ 8, 7807 /*bit_valid*/ 0, 7808 /*bit*/ 0); 7809 ctl_done((union ctl_io *)ctsio); 7810 return (1); 7811 } 7812 7813 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7814 if (ctl_get_prkey(lun, i) != sa_res_key) 7815 continue; 7816 7817 found = 1; 7818 ctl_clr_prkey(lun, i); 7819 lun->pr_key_count--; 7820 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7821 } 7822 if (!found) { 7823 mtx_unlock(&lun->lun_lock); 7824 free(ctsio->kern_data_ptr, M_CTL); 7825 ctl_set_reservation_conflict(ctsio); 7826 ctl_done((union ctl_io *)ctsio); 7827 return (CTL_RETVAL_COMPLETE); 7828 } 7829 lun->PRGeneration++; 7830 mtx_unlock(&lun->lun_lock); 7831 7832 /* send msg to other side */ 7833 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7834 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7835 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7836 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7837 persis_io.pr.pr_info.res_type = type; 7838 memcpy(persis_io.pr.pr_info.sa_res_key, 7839 param->serv_act_res_key, 7840 sizeof(param->serv_act_res_key)); 7841 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7842 sizeof(persis_io.pr), M_WAITOK); 7843 } else { 7844 /* Reserved but not all registrants */ 7845 /* sa_res_key is res holder */ 7846 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7847 /* validate scope and type */ 7848 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7849 SPR_LU_SCOPE) { 7850 mtx_unlock(&lun->lun_lock); 7851 ctl_set_invalid_field(/*ctsio*/ ctsio, 7852 /*sks_valid*/ 1, 7853 /*command*/ 1, 7854 /*field*/ 2, 7855 /*bit_valid*/ 1, 7856 /*bit*/ 4); 7857 ctl_done((union ctl_io *)ctsio); 7858 return (1); 7859 } 7860 7861 if (type>8 || type==2 || type==4 || type==0) { 7862 mtx_unlock(&lun->lun_lock); 7863 ctl_set_invalid_field(/*ctsio*/ ctsio, 7864 /*sks_valid*/ 1, 7865 /*command*/ 1, 7866 /*field*/ 2, 7867 /*bit_valid*/ 1, 7868 /*bit*/ 0); 7869 ctl_done((union ctl_io *)ctsio); 7870 return (1); 7871 } 7872 7873 /* 7874 * Do the following: 7875 * if sa_res_key != res_key remove all 7876 * registrants w/sa_res_key and generate UA 7877 * for these registrants(Registrations 7878 * Preempted) if it wasn't an exclusive 7879 * reservation generate UA(Reservations 7880 * Preempted) for all other registered nexuses 7881 * if the type has changed. Establish the new 7882 * reservation and holder. If res_key and 7883 * sa_res_key are the same do the above 7884 * except don't unregister the res holder. 7885 */ 7886 7887 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7888 if (i == residx || ctl_get_prkey(lun, i) == 0) 7889 continue; 7890 7891 if (sa_res_key == ctl_get_prkey(lun, i)) { 7892 ctl_clr_prkey(lun, i); 7893 lun->pr_key_count--; 7894 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7895 } else if (type != lun->res_type 7896 && (lun->res_type == SPR_TYPE_WR_EX_RO 7897 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7898 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7899 } 7900 } 7901 lun->res_type = type; 7902 if (lun->res_type != SPR_TYPE_WR_EX_AR 7903 && lun->res_type != SPR_TYPE_EX_AC_AR) 7904 lun->pr_res_idx = residx; 7905 else 7906 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7907 lun->PRGeneration++; 7908 mtx_unlock(&lun->lun_lock); 7909 7910 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7911 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7912 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7913 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7914 persis_io.pr.pr_info.res_type = type; 7915 memcpy(persis_io.pr.pr_info.sa_res_key, 7916 param->serv_act_res_key, 7917 sizeof(param->serv_act_res_key)); 7918 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7919 sizeof(persis_io.pr), M_WAITOK); 7920 } else { 7921 /* 7922 * sa_res_key is not the res holder just 7923 * remove registrants 7924 */ 7925 int found=0; 7926 7927 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7928 if (sa_res_key != ctl_get_prkey(lun, i)) 7929 continue; 7930 7931 found = 1; 7932 ctl_clr_prkey(lun, i); 7933 lun->pr_key_count--; 7934 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7935 } 7936 7937 if (!found) { 7938 mtx_unlock(&lun->lun_lock); 7939 free(ctsio->kern_data_ptr, M_CTL); 7940 ctl_set_reservation_conflict(ctsio); 7941 ctl_done((union ctl_io *)ctsio); 7942 return (1); 7943 } 7944 lun->PRGeneration++; 7945 mtx_unlock(&lun->lun_lock); 7946 7947 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7948 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7949 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7950 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7951 persis_io.pr.pr_info.res_type = type; 7952 memcpy(persis_io.pr.pr_info.sa_res_key, 7953 param->serv_act_res_key, 7954 sizeof(param->serv_act_res_key)); 7955 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7956 sizeof(persis_io.pr), M_WAITOK); 7957 } 7958 } 7959 return (0); 7960} 7961 7962static void 7963ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7964{ 7965 uint64_t sa_res_key; 7966 int i; 7967 7968 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7969 7970 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7971 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7972 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 7973 if (sa_res_key == 0) { 7974 /* 7975 * Unregister everybody else and build UA for 7976 * them 7977 */ 7978 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7979 if (i == msg->pr.pr_info.residx || 7980 ctl_get_prkey(lun, i) == 0) 7981 continue; 7982 7983 ctl_clr_prkey(lun, i); 7984 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7985 } 7986 7987 lun->pr_key_count = 1; 7988 lun->res_type = msg->pr.pr_info.res_type; 7989 if (lun->res_type != SPR_TYPE_WR_EX_AR 7990 && lun->res_type != SPR_TYPE_EX_AC_AR) 7991 lun->pr_res_idx = msg->pr.pr_info.residx; 7992 } else { 7993 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7994 if (sa_res_key == ctl_get_prkey(lun, i)) 7995 continue; 7996 7997 ctl_clr_prkey(lun, i); 7998 lun->pr_key_count--; 7999 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8000 } 8001 } 8002 } else { 8003 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8004 if (i == msg->pr.pr_info.residx || 8005 ctl_get_prkey(lun, i) == 0) 8006 continue; 8007 8008 if (sa_res_key == ctl_get_prkey(lun, i)) { 8009 ctl_clr_prkey(lun, i); 8010 lun->pr_key_count--; 8011 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8012 } else if (msg->pr.pr_info.res_type != lun->res_type 8013 && (lun->res_type == SPR_TYPE_WR_EX_RO 8014 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8015 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8016 } 8017 } 8018 lun->res_type = msg->pr.pr_info.res_type; 8019 if (lun->res_type != SPR_TYPE_WR_EX_AR 8020 && lun->res_type != SPR_TYPE_EX_AC_AR) 8021 lun->pr_res_idx = msg->pr.pr_info.residx; 8022 else 8023 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8024 } 8025 lun->PRGeneration++; 8026 8027} 8028 8029 8030int 8031ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8032{ 8033 int retval; 8034 u_int32_t param_len; 8035 struct scsi_per_res_out *cdb; 8036 struct ctl_lun *lun; 8037 struct scsi_per_res_out_parms* param; 8038 struct ctl_softc *softc; 8039 uint32_t residx; 8040 uint64_t res_key, sa_res_key, key; 8041 uint8_t type; 8042 union ctl_ha_msg persis_io; 8043 int i; 8044 8045 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8046 8047 retval = CTL_RETVAL_COMPLETE; 8048 8049 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8051 softc = lun->ctl_softc; 8052 8053 /* 8054 * We only support whole-LUN scope. The scope & type are ignored for 8055 * register, register and ignore existing key and clear. 8056 * We sometimes ignore scope and type on preempts too!! 8057 * Verify reservation type here as well. 8058 */ 8059 type = cdb->scope_type & SPR_TYPE_MASK; 8060 if ((cdb->action == SPRO_RESERVE) 8061 || (cdb->action == SPRO_RELEASE)) { 8062 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8063 ctl_set_invalid_field(/*ctsio*/ ctsio, 8064 /*sks_valid*/ 1, 8065 /*command*/ 1, 8066 /*field*/ 2, 8067 /*bit_valid*/ 1, 8068 /*bit*/ 4); 8069 ctl_done((union ctl_io *)ctsio); 8070 return (CTL_RETVAL_COMPLETE); 8071 } 8072 8073 if (type>8 || type==2 || type==4 || type==0) { 8074 ctl_set_invalid_field(/*ctsio*/ ctsio, 8075 /*sks_valid*/ 1, 8076 /*command*/ 1, 8077 /*field*/ 2, 8078 /*bit_valid*/ 1, 8079 /*bit*/ 0); 8080 ctl_done((union ctl_io *)ctsio); 8081 return (CTL_RETVAL_COMPLETE); 8082 } 8083 } 8084 8085 param_len = scsi_4btoul(cdb->length); 8086 8087 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8088 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8089 ctsio->kern_data_len = param_len; 8090 ctsio->kern_total_len = param_len; 8091 ctsio->kern_data_resid = 0; 8092 ctsio->kern_rel_offset = 0; 8093 ctsio->kern_sg_entries = 0; 8094 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8095 ctsio->be_move_done = ctl_config_move_done; 8096 ctl_datamove((union ctl_io *)ctsio); 8097 8098 return (CTL_RETVAL_COMPLETE); 8099 } 8100 8101 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8102 8103 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8104 res_key = scsi_8btou64(param->res_key.key); 8105 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8106 8107 /* 8108 * Validate the reservation key here except for SPRO_REG_IGNO 8109 * This must be done for all other service actions 8110 */ 8111 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8112 mtx_lock(&lun->lun_lock); 8113 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8114 if (res_key != key) { 8115 /* 8116 * The current key passed in doesn't match 8117 * the one the initiator previously 8118 * registered. 8119 */ 8120 mtx_unlock(&lun->lun_lock); 8121 free(ctsio->kern_data_ptr, M_CTL); 8122 ctl_set_reservation_conflict(ctsio); 8123 ctl_done((union ctl_io *)ctsio); 8124 return (CTL_RETVAL_COMPLETE); 8125 } 8126 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8127 /* 8128 * We are not registered 8129 */ 8130 mtx_unlock(&lun->lun_lock); 8131 free(ctsio->kern_data_ptr, M_CTL); 8132 ctl_set_reservation_conflict(ctsio); 8133 ctl_done((union ctl_io *)ctsio); 8134 return (CTL_RETVAL_COMPLETE); 8135 } else if (res_key != 0) { 8136 /* 8137 * We are not registered and trying to register but 8138 * the register key isn't zero. 8139 */ 8140 mtx_unlock(&lun->lun_lock); 8141 free(ctsio->kern_data_ptr, M_CTL); 8142 ctl_set_reservation_conflict(ctsio); 8143 ctl_done((union ctl_io *)ctsio); 8144 return (CTL_RETVAL_COMPLETE); 8145 } 8146 mtx_unlock(&lun->lun_lock); 8147 } 8148 8149 switch (cdb->action & SPRO_ACTION_MASK) { 8150 case SPRO_REGISTER: 8151 case SPRO_REG_IGNO: { 8152 8153#if 0 8154 printf("Registration received\n"); 8155#endif 8156 8157 /* 8158 * We don't support any of these options, as we report in 8159 * the read capabilities request (see 8160 * ctl_persistent_reserve_in(), above). 8161 */ 8162 if ((param->flags & SPR_SPEC_I_PT) 8163 || (param->flags & SPR_ALL_TG_PT) 8164 || (param->flags & SPR_APTPL)) { 8165 int bit_ptr; 8166 8167 if (param->flags & SPR_APTPL) 8168 bit_ptr = 0; 8169 else if (param->flags & SPR_ALL_TG_PT) 8170 bit_ptr = 2; 8171 else /* SPR_SPEC_I_PT */ 8172 bit_ptr = 3; 8173 8174 free(ctsio->kern_data_ptr, M_CTL); 8175 ctl_set_invalid_field(ctsio, 8176 /*sks_valid*/ 1, 8177 /*command*/ 0, 8178 /*field*/ 20, 8179 /*bit_valid*/ 1, 8180 /*bit*/ bit_ptr); 8181 ctl_done((union ctl_io *)ctsio); 8182 return (CTL_RETVAL_COMPLETE); 8183 } 8184 8185 mtx_lock(&lun->lun_lock); 8186 8187 /* 8188 * The initiator wants to clear the 8189 * key/unregister. 8190 */ 8191 if (sa_res_key == 0) { 8192 if ((res_key == 0 8193 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8194 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8195 && ctl_get_prkey(lun, residx) == 0)) { 8196 mtx_unlock(&lun->lun_lock); 8197 goto done; 8198 } 8199 8200 ctl_clr_prkey(lun, residx); 8201 lun->pr_key_count--; 8202 8203 if (residx == lun->pr_res_idx) { 8204 lun->flags &= ~CTL_LUN_PR_RESERVED; 8205 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8206 8207 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8208 || lun->res_type == SPR_TYPE_EX_AC_RO) 8209 && lun->pr_key_count) { 8210 /* 8211 * If the reservation is a registrants 8212 * only type we need to generate a UA 8213 * for other registered inits. The 8214 * sense code should be RESERVATIONS 8215 * RELEASED 8216 */ 8217 8218 for (i = softc->init_min; i < softc->init_max; i++){ 8219 if (ctl_get_prkey(lun, i) == 0) 8220 continue; 8221 ctl_est_ua(lun, i, 8222 CTL_UA_RES_RELEASE); 8223 } 8224 } 8225 lun->res_type = 0; 8226 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8227 if (lun->pr_key_count==0) { 8228 lun->flags &= ~CTL_LUN_PR_RESERVED; 8229 lun->res_type = 0; 8230 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8231 } 8232 } 8233 lun->PRGeneration++; 8234 mtx_unlock(&lun->lun_lock); 8235 8236 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8237 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8238 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8239 persis_io.pr.pr_info.residx = residx; 8240 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8241 sizeof(persis_io.pr), M_WAITOK); 8242 } else /* sa_res_key != 0 */ { 8243 8244 /* 8245 * If we aren't registered currently then increment 8246 * the key count and set the registered flag. 8247 */ 8248 ctl_alloc_prkey(lun, residx); 8249 if (ctl_get_prkey(lun, residx) == 0) 8250 lun->pr_key_count++; 8251 ctl_set_prkey(lun, residx, sa_res_key); 8252 lun->PRGeneration++; 8253 mtx_unlock(&lun->lun_lock); 8254 8255 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8256 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8257 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8258 persis_io.pr.pr_info.residx = residx; 8259 memcpy(persis_io.pr.pr_info.sa_res_key, 8260 param->serv_act_res_key, 8261 sizeof(param->serv_act_res_key)); 8262 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8263 sizeof(persis_io.pr), M_WAITOK); 8264 } 8265 8266 break; 8267 } 8268 case SPRO_RESERVE: 8269#if 0 8270 printf("Reserve executed type %d\n", type); 8271#endif 8272 mtx_lock(&lun->lun_lock); 8273 if (lun->flags & CTL_LUN_PR_RESERVED) { 8274 /* 8275 * if this isn't the reservation holder and it's 8276 * not a "all registrants" type or if the type is 8277 * different then we have a conflict 8278 */ 8279 if ((lun->pr_res_idx != residx 8280 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8281 || lun->res_type != type) { 8282 mtx_unlock(&lun->lun_lock); 8283 free(ctsio->kern_data_ptr, M_CTL); 8284 ctl_set_reservation_conflict(ctsio); 8285 ctl_done((union ctl_io *)ctsio); 8286 return (CTL_RETVAL_COMPLETE); 8287 } 8288 mtx_unlock(&lun->lun_lock); 8289 } else /* create a reservation */ { 8290 /* 8291 * If it's not an "all registrants" type record 8292 * reservation holder 8293 */ 8294 if (type != SPR_TYPE_WR_EX_AR 8295 && type != SPR_TYPE_EX_AC_AR) 8296 lun->pr_res_idx = residx; /* Res holder */ 8297 else 8298 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8299 8300 lun->flags |= CTL_LUN_PR_RESERVED; 8301 lun->res_type = type; 8302 8303 mtx_unlock(&lun->lun_lock); 8304 8305 /* send msg to other side */ 8306 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8307 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8308 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8309 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8310 persis_io.pr.pr_info.res_type = type; 8311 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8312 sizeof(persis_io.pr), M_WAITOK); 8313 } 8314 break; 8315 8316 case SPRO_RELEASE: 8317 mtx_lock(&lun->lun_lock); 8318 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8319 /* No reservation exists return good status */ 8320 mtx_unlock(&lun->lun_lock); 8321 goto done; 8322 } 8323 /* 8324 * Is this nexus a reservation holder? 8325 */ 8326 if (lun->pr_res_idx != residx 8327 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8328 /* 8329 * not a res holder return good status but 8330 * do nothing 8331 */ 8332 mtx_unlock(&lun->lun_lock); 8333 goto done; 8334 } 8335 8336 if (lun->res_type != type) { 8337 mtx_unlock(&lun->lun_lock); 8338 free(ctsio->kern_data_ptr, M_CTL); 8339 ctl_set_illegal_pr_release(ctsio); 8340 ctl_done((union ctl_io *)ctsio); 8341 return (CTL_RETVAL_COMPLETE); 8342 } 8343 8344 /* okay to release */ 8345 lun->flags &= ~CTL_LUN_PR_RESERVED; 8346 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8347 lun->res_type = 0; 8348 8349 /* 8350 * if this isn't an exclusive access 8351 * res generate UA for all other 8352 * registrants. 8353 */ 8354 if (type != SPR_TYPE_EX_AC 8355 && type != SPR_TYPE_WR_EX) { 8356 for (i = softc->init_min; i < softc->init_max; i++) { 8357 if (i == residx || ctl_get_prkey(lun, i) == 0) 8358 continue; 8359 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8360 } 8361 } 8362 mtx_unlock(&lun->lun_lock); 8363 8364 /* Send msg to other side */ 8365 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8366 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8367 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8368 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8369 sizeof(persis_io.pr), M_WAITOK); 8370 break; 8371 8372 case SPRO_CLEAR: 8373 /* send msg to other side */ 8374 8375 mtx_lock(&lun->lun_lock); 8376 lun->flags &= ~CTL_LUN_PR_RESERVED; 8377 lun->res_type = 0; 8378 lun->pr_key_count = 0; 8379 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8380 8381 ctl_clr_prkey(lun, residx); 8382 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8383 if (ctl_get_prkey(lun, i) != 0) { 8384 ctl_clr_prkey(lun, i); 8385 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8386 } 8387 lun->PRGeneration++; 8388 mtx_unlock(&lun->lun_lock); 8389 8390 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8391 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8392 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8393 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8394 sizeof(persis_io.pr), M_WAITOK); 8395 break; 8396 8397 case SPRO_PREEMPT: 8398 case SPRO_PRE_ABO: { 8399 int nretval; 8400 8401 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8402 residx, ctsio, cdb, param); 8403 if (nretval != 0) 8404 return (CTL_RETVAL_COMPLETE); 8405 break; 8406 } 8407 default: 8408 panic("Invalid PR type %x", cdb->action); 8409 } 8410 8411done: 8412 free(ctsio->kern_data_ptr, M_CTL); 8413 ctl_set_success(ctsio); 8414 ctl_done((union ctl_io *)ctsio); 8415 8416 return (retval); 8417} 8418 8419/* 8420 * This routine is for handling a message from the other SC pertaining to 8421 * persistent reserve out. All the error checking will have been done 8422 * so only perorming the action need be done here to keep the two 8423 * in sync. 8424 */ 8425static void 8426ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8427{ 8428 struct ctl_lun *lun; 8429 struct ctl_softc *softc; 8430 int i; 8431 uint32_t residx, targ_lun; 8432 8433 softc = control_softc; 8434 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8435 mtx_lock(&softc->ctl_lock); 8436 if ((targ_lun >= CTL_MAX_LUNS) || 8437 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8438 mtx_unlock(&softc->ctl_lock); 8439 return; 8440 } 8441 mtx_lock(&lun->lun_lock); 8442 mtx_unlock(&softc->ctl_lock); 8443 if (lun->flags & CTL_LUN_DISABLED) { 8444 mtx_unlock(&lun->lun_lock); 8445 return; 8446 } 8447 residx = ctl_get_initindex(&msg->hdr.nexus); 8448 switch(msg->pr.pr_info.action) { 8449 case CTL_PR_REG_KEY: 8450 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8451 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8452 lun->pr_key_count++; 8453 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8454 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8455 lun->PRGeneration++; 8456 break; 8457 8458 case CTL_PR_UNREG_KEY: 8459 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8460 lun->pr_key_count--; 8461 8462 /* XXX Need to see if the reservation has been released */ 8463 /* if so do we need to generate UA? */ 8464 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8465 lun->flags &= ~CTL_LUN_PR_RESERVED; 8466 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8467 8468 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8469 || lun->res_type == SPR_TYPE_EX_AC_RO) 8470 && lun->pr_key_count) { 8471 /* 8472 * If the reservation is a registrants 8473 * only type we need to generate a UA 8474 * for other registered inits. The 8475 * sense code should be RESERVATIONS 8476 * RELEASED 8477 */ 8478 8479 for (i = softc->init_min; i < softc->init_max; i++) { 8480 if (ctl_get_prkey(lun, i) == 0) 8481 continue; 8482 8483 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8484 } 8485 } 8486 lun->res_type = 0; 8487 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8488 if (lun->pr_key_count==0) { 8489 lun->flags &= ~CTL_LUN_PR_RESERVED; 8490 lun->res_type = 0; 8491 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8492 } 8493 } 8494 lun->PRGeneration++; 8495 break; 8496 8497 case CTL_PR_RESERVE: 8498 lun->flags |= CTL_LUN_PR_RESERVED; 8499 lun->res_type = msg->pr.pr_info.res_type; 8500 lun->pr_res_idx = msg->pr.pr_info.residx; 8501 8502 break; 8503 8504 case CTL_PR_RELEASE: 8505 /* 8506 * if this isn't an exclusive access res generate UA for all 8507 * other registrants. 8508 */ 8509 if (lun->res_type != SPR_TYPE_EX_AC 8510 && lun->res_type != SPR_TYPE_WR_EX) { 8511 for (i = softc->init_min; i < softc->init_max; i++) 8512 if (i == residx || ctl_get_prkey(lun, i) == 0) 8513 continue; 8514 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8515 } 8516 8517 lun->flags &= ~CTL_LUN_PR_RESERVED; 8518 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8519 lun->res_type = 0; 8520 break; 8521 8522 case CTL_PR_PREEMPT: 8523 ctl_pro_preempt_other(lun, msg); 8524 break; 8525 case CTL_PR_CLEAR: 8526 lun->flags &= ~CTL_LUN_PR_RESERVED; 8527 lun->res_type = 0; 8528 lun->pr_key_count = 0; 8529 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8530 8531 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8532 if (ctl_get_prkey(lun, i) == 0) 8533 continue; 8534 ctl_clr_prkey(lun, i); 8535 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8536 } 8537 lun->PRGeneration++; 8538 break; 8539 } 8540 8541 mtx_unlock(&lun->lun_lock); 8542} 8543 8544int 8545ctl_read_write(struct ctl_scsiio *ctsio) 8546{ 8547 struct ctl_lun *lun; 8548 struct ctl_lba_len_flags *lbalen; 8549 uint64_t lba; 8550 uint32_t num_blocks; 8551 int flags, retval; 8552 int isread; 8553 8554 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8555 8556 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8557 8558 flags = 0; 8559 retval = CTL_RETVAL_COMPLETE; 8560 8561 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8562 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8563 switch (ctsio->cdb[0]) { 8564 case READ_6: 8565 case WRITE_6: { 8566 struct scsi_rw_6 *cdb; 8567 8568 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8569 8570 lba = scsi_3btoul(cdb->addr); 8571 /* only 5 bits are valid in the most significant address byte */ 8572 lba &= 0x1fffff; 8573 num_blocks = cdb->length; 8574 /* 8575 * This is correct according to SBC-2. 8576 */ 8577 if (num_blocks == 0) 8578 num_blocks = 256; 8579 break; 8580 } 8581 case READ_10: 8582 case WRITE_10: { 8583 struct scsi_rw_10 *cdb; 8584 8585 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8586 if (cdb->byte2 & SRW10_FUA) 8587 flags |= CTL_LLF_FUA; 8588 if (cdb->byte2 & SRW10_DPO) 8589 flags |= CTL_LLF_DPO; 8590 lba = scsi_4btoul(cdb->addr); 8591 num_blocks = scsi_2btoul(cdb->length); 8592 break; 8593 } 8594 case WRITE_VERIFY_10: { 8595 struct scsi_write_verify_10 *cdb; 8596 8597 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8598 flags |= CTL_LLF_FUA; 8599 if (cdb->byte2 & SWV_DPO) 8600 flags |= CTL_LLF_DPO; 8601 lba = scsi_4btoul(cdb->addr); 8602 num_blocks = scsi_2btoul(cdb->length); 8603 break; 8604 } 8605 case READ_12: 8606 case WRITE_12: { 8607 struct scsi_rw_12 *cdb; 8608 8609 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8610 if (cdb->byte2 & SRW12_FUA) 8611 flags |= CTL_LLF_FUA; 8612 if (cdb->byte2 & SRW12_DPO) 8613 flags |= CTL_LLF_DPO; 8614 lba = scsi_4btoul(cdb->addr); 8615 num_blocks = scsi_4btoul(cdb->length); 8616 break; 8617 } 8618 case WRITE_VERIFY_12: { 8619 struct scsi_write_verify_12 *cdb; 8620 8621 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8622 flags |= CTL_LLF_FUA; 8623 if (cdb->byte2 & SWV_DPO) 8624 flags |= CTL_LLF_DPO; 8625 lba = scsi_4btoul(cdb->addr); 8626 num_blocks = scsi_4btoul(cdb->length); 8627 break; 8628 } 8629 case READ_16: 8630 case WRITE_16: { 8631 struct scsi_rw_16 *cdb; 8632 8633 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8634 if (cdb->byte2 & SRW12_FUA) 8635 flags |= CTL_LLF_FUA; 8636 if (cdb->byte2 & SRW12_DPO) 8637 flags |= CTL_LLF_DPO; 8638 lba = scsi_8btou64(cdb->addr); 8639 num_blocks = scsi_4btoul(cdb->length); 8640 break; 8641 } 8642 case WRITE_ATOMIC_16: { 8643 struct scsi_rw_16 *cdb; 8644 8645 if (lun->be_lun->atomicblock == 0) { 8646 ctl_set_invalid_opcode(ctsio); 8647 ctl_done((union ctl_io *)ctsio); 8648 return (CTL_RETVAL_COMPLETE); 8649 } 8650 8651 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8652 if (cdb->byte2 & SRW12_FUA) 8653 flags |= CTL_LLF_FUA; 8654 if (cdb->byte2 & SRW12_DPO) 8655 flags |= CTL_LLF_DPO; 8656 lba = scsi_8btou64(cdb->addr); 8657 num_blocks = scsi_4btoul(cdb->length); 8658 if (num_blocks > lun->be_lun->atomicblock) { 8659 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8660 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8661 /*bit*/ 0); 8662 ctl_done((union ctl_io *)ctsio); 8663 return (CTL_RETVAL_COMPLETE); 8664 } 8665 break; 8666 } 8667 case WRITE_VERIFY_16: { 8668 struct scsi_write_verify_16 *cdb; 8669 8670 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8671 flags |= CTL_LLF_FUA; 8672 if (cdb->byte2 & SWV_DPO) 8673 flags |= CTL_LLF_DPO; 8674 lba = scsi_8btou64(cdb->addr); 8675 num_blocks = scsi_4btoul(cdb->length); 8676 break; 8677 } 8678 default: 8679 /* 8680 * We got a command we don't support. This shouldn't 8681 * happen, commands should be filtered out above us. 8682 */ 8683 ctl_set_invalid_opcode(ctsio); 8684 ctl_done((union ctl_io *)ctsio); 8685 8686 return (CTL_RETVAL_COMPLETE); 8687 break; /* NOTREACHED */ 8688 } 8689 8690 /* 8691 * The first check is to make sure we're in bounds, the second 8692 * check is to catch wrap-around problems. If the lba + num blocks 8693 * is less than the lba, then we've wrapped around and the block 8694 * range is invalid anyway. 8695 */ 8696 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8697 || ((lba + num_blocks) < lba)) { 8698 ctl_set_lba_out_of_range(ctsio); 8699 ctl_done((union ctl_io *)ctsio); 8700 return (CTL_RETVAL_COMPLETE); 8701 } 8702 8703 /* 8704 * According to SBC-3, a transfer length of 0 is not an error. 8705 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8706 * translates to 256 blocks for those commands. 8707 */ 8708 if (num_blocks == 0) { 8709 ctl_set_success(ctsio); 8710 ctl_done((union ctl_io *)ctsio); 8711 return (CTL_RETVAL_COMPLETE); 8712 } 8713 8714 /* Set FUA and/or DPO if caches are disabled. */ 8715 if (isread) { 8716 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8717 SCP_RCD) != 0) 8718 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8719 } else { 8720 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8721 SCP_WCE) == 0) 8722 flags |= CTL_LLF_FUA; 8723 } 8724 8725 lbalen = (struct ctl_lba_len_flags *) 8726 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8727 lbalen->lba = lba; 8728 lbalen->len = num_blocks; 8729 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8730 8731 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8732 ctsio->kern_rel_offset = 0; 8733 8734 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8735 8736 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8737 8738 return (retval); 8739} 8740 8741static int 8742ctl_cnw_cont(union ctl_io *io) 8743{ 8744 struct ctl_scsiio *ctsio; 8745 struct ctl_lun *lun; 8746 struct ctl_lba_len_flags *lbalen; 8747 int retval; 8748 8749 ctsio = &io->scsiio; 8750 ctsio->io_hdr.status = CTL_STATUS_NONE; 8751 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8752 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8753 lbalen = (struct ctl_lba_len_flags *) 8754 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8755 lbalen->flags &= ~CTL_LLF_COMPARE; 8756 lbalen->flags |= CTL_LLF_WRITE; 8757 8758 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8759 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8760 return (retval); 8761} 8762 8763int 8764ctl_cnw(struct ctl_scsiio *ctsio) 8765{ 8766 struct ctl_lun *lun; 8767 struct ctl_lba_len_flags *lbalen; 8768 uint64_t lba; 8769 uint32_t num_blocks; 8770 int flags, retval; 8771 8772 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8773 8774 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8775 8776 flags = 0; 8777 retval = CTL_RETVAL_COMPLETE; 8778 8779 switch (ctsio->cdb[0]) { 8780 case COMPARE_AND_WRITE: { 8781 struct scsi_compare_and_write *cdb; 8782 8783 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8784 if (cdb->byte2 & SRW10_FUA) 8785 flags |= CTL_LLF_FUA; 8786 if (cdb->byte2 & SRW10_DPO) 8787 flags |= CTL_LLF_DPO; 8788 lba = scsi_8btou64(cdb->addr); 8789 num_blocks = cdb->length; 8790 break; 8791 } 8792 default: 8793 /* 8794 * We got a command we don't support. This shouldn't 8795 * happen, commands should be filtered out above us. 8796 */ 8797 ctl_set_invalid_opcode(ctsio); 8798 ctl_done((union ctl_io *)ctsio); 8799 8800 return (CTL_RETVAL_COMPLETE); 8801 break; /* NOTREACHED */ 8802 } 8803 8804 /* 8805 * The first check is to make sure we're in bounds, the second 8806 * check is to catch wrap-around problems. If the lba + num blocks 8807 * is less than the lba, then we've wrapped around and the block 8808 * range is invalid anyway. 8809 */ 8810 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8811 || ((lba + num_blocks) < lba)) { 8812 ctl_set_lba_out_of_range(ctsio); 8813 ctl_done((union ctl_io *)ctsio); 8814 return (CTL_RETVAL_COMPLETE); 8815 } 8816 8817 /* 8818 * According to SBC-3, a transfer length of 0 is not an error. 8819 */ 8820 if (num_blocks == 0) { 8821 ctl_set_success(ctsio); 8822 ctl_done((union ctl_io *)ctsio); 8823 return (CTL_RETVAL_COMPLETE); 8824 } 8825 8826 /* Set FUA if write cache is disabled. */ 8827 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8828 SCP_WCE) == 0) 8829 flags |= CTL_LLF_FUA; 8830 8831 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8832 ctsio->kern_rel_offset = 0; 8833 8834 /* 8835 * Set the IO_CONT flag, so that if this I/O gets passed to 8836 * ctl_data_submit_done(), it'll get passed back to 8837 * ctl_ctl_cnw_cont() for further processing. 8838 */ 8839 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8840 ctsio->io_cont = ctl_cnw_cont; 8841 8842 lbalen = (struct ctl_lba_len_flags *) 8843 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8844 lbalen->lba = lba; 8845 lbalen->len = num_blocks; 8846 lbalen->flags = CTL_LLF_COMPARE | flags; 8847 8848 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8849 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8850 return (retval); 8851} 8852 8853int 8854ctl_verify(struct ctl_scsiio *ctsio) 8855{ 8856 struct ctl_lun *lun; 8857 struct ctl_lba_len_flags *lbalen; 8858 uint64_t lba; 8859 uint32_t num_blocks; 8860 int bytchk, flags; 8861 int retval; 8862 8863 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8864 8865 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8866 8867 bytchk = 0; 8868 flags = CTL_LLF_FUA; 8869 retval = CTL_RETVAL_COMPLETE; 8870 8871 switch (ctsio->cdb[0]) { 8872 case VERIFY_10: { 8873 struct scsi_verify_10 *cdb; 8874 8875 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8876 if (cdb->byte2 & SVFY_BYTCHK) 8877 bytchk = 1; 8878 if (cdb->byte2 & SVFY_DPO) 8879 flags |= CTL_LLF_DPO; 8880 lba = scsi_4btoul(cdb->addr); 8881 num_blocks = scsi_2btoul(cdb->length); 8882 break; 8883 } 8884 case VERIFY_12: { 8885 struct scsi_verify_12 *cdb; 8886 8887 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8888 if (cdb->byte2 & SVFY_BYTCHK) 8889 bytchk = 1; 8890 if (cdb->byte2 & SVFY_DPO) 8891 flags |= CTL_LLF_DPO; 8892 lba = scsi_4btoul(cdb->addr); 8893 num_blocks = scsi_4btoul(cdb->length); 8894 break; 8895 } 8896 case VERIFY_16: { 8897 struct scsi_rw_16 *cdb; 8898 8899 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8900 if (cdb->byte2 & SVFY_BYTCHK) 8901 bytchk = 1; 8902 if (cdb->byte2 & SVFY_DPO) 8903 flags |= CTL_LLF_DPO; 8904 lba = scsi_8btou64(cdb->addr); 8905 num_blocks = scsi_4btoul(cdb->length); 8906 break; 8907 } 8908 default: 8909 /* 8910 * We got a command we don't support. This shouldn't 8911 * happen, commands should be filtered out above us. 8912 */ 8913 ctl_set_invalid_opcode(ctsio); 8914 ctl_done((union ctl_io *)ctsio); 8915 return (CTL_RETVAL_COMPLETE); 8916 } 8917 8918 /* 8919 * The first check is to make sure we're in bounds, the second 8920 * check is to catch wrap-around problems. If the lba + num blocks 8921 * is less than the lba, then we've wrapped around and the block 8922 * range is invalid anyway. 8923 */ 8924 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8925 || ((lba + num_blocks) < lba)) { 8926 ctl_set_lba_out_of_range(ctsio); 8927 ctl_done((union ctl_io *)ctsio); 8928 return (CTL_RETVAL_COMPLETE); 8929 } 8930 8931 /* 8932 * According to SBC-3, a transfer length of 0 is not an error. 8933 */ 8934 if (num_blocks == 0) { 8935 ctl_set_success(ctsio); 8936 ctl_done((union ctl_io *)ctsio); 8937 return (CTL_RETVAL_COMPLETE); 8938 } 8939 8940 lbalen = (struct ctl_lba_len_flags *) 8941 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8942 lbalen->lba = lba; 8943 lbalen->len = num_blocks; 8944 if (bytchk) { 8945 lbalen->flags = CTL_LLF_COMPARE | flags; 8946 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8947 } else { 8948 lbalen->flags = CTL_LLF_VERIFY | flags; 8949 ctsio->kern_total_len = 0; 8950 } 8951 ctsio->kern_rel_offset = 0; 8952 8953 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8954 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8955 return (retval); 8956} 8957 8958int 8959ctl_report_luns(struct ctl_scsiio *ctsio) 8960{ 8961 struct ctl_softc *softc = control_softc; 8962 struct scsi_report_luns *cdb; 8963 struct scsi_report_luns_data *lun_data; 8964 struct ctl_lun *lun, *request_lun; 8965 struct ctl_port *port; 8966 int num_luns, retval; 8967 uint32_t alloc_len, lun_datalen; 8968 int num_filled, well_known; 8969 uint32_t initidx, targ_lun_id, lun_id; 8970 8971 retval = CTL_RETVAL_COMPLETE; 8972 well_known = 0; 8973 8974 cdb = (struct scsi_report_luns *)ctsio->cdb; 8975 port = ctl_io_port(&ctsio->io_hdr); 8976 8977 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8978 8979 mtx_lock(&softc->ctl_lock); 8980 num_luns = 0; 8981 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 8982 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 8983 num_luns++; 8984 } 8985 mtx_unlock(&softc->ctl_lock); 8986 8987 switch (cdb->select_report) { 8988 case RPL_REPORT_DEFAULT: 8989 case RPL_REPORT_ALL: 8990 break; 8991 case RPL_REPORT_WELLKNOWN: 8992 well_known = 1; 8993 num_luns = 0; 8994 break; 8995 default: 8996 ctl_set_invalid_field(ctsio, 8997 /*sks_valid*/ 1, 8998 /*command*/ 1, 8999 /*field*/ 2, 9000 /*bit_valid*/ 0, 9001 /*bit*/ 0); 9002 ctl_done((union ctl_io *)ctsio); 9003 return (retval); 9004 break; /* NOTREACHED */ 9005 } 9006 9007 alloc_len = scsi_4btoul(cdb->length); 9008 /* 9009 * The initiator has to allocate at least 16 bytes for this request, 9010 * so he can at least get the header and the first LUN. Otherwise 9011 * we reject the request (per SPC-3 rev 14, section 6.21). 9012 */ 9013 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9014 sizeof(struct scsi_report_luns_lundata))) { 9015 ctl_set_invalid_field(ctsio, 9016 /*sks_valid*/ 1, 9017 /*command*/ 1, 9018 /*field*/ 6, 9019 /*bit_valid*/ 0, 9020 /*bit*/ 0); 9021 ctl_done((union ctl_io *)ctsio); 9022 return (retval); 9023 } 9024 9025 request_lun = (struct ctl_lun *) 9026 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9027 9028 lun_datalen = sizeof(*lun_data) + 9029 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9030 9031 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9032 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9033 ctsio->kern_sg_entries = 0; 9034 9035 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9036 9037 mtx_lock(&softc->ctl_lock); 9038 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9039 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9040 if (lun_id >= CTL_MAX_LUNS) 9041 continue; 9042 lun = softc->ctl_luns[lun_id]; 9043 if (lun == NULL) 9044 continue; 9045 9046 if (targ_lun_id <= 0xff) { 9047 /* 9048 * Peripheral addressing method, bus number 0. 9049 */ 9050 lun_data->luns[num_filled].lundata[0] = 9051 RPL_LUNDATA_ATYP_PERIPH; 9052 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9053 num_filled++; 9054 } else if (targ_lun_id <= 0x3fff) { 9055 /* 9056 * Flat addressing method. 9057 */ 9058 lun_data->luns[num_filled].lundata[0] = 9059 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9060 lun_data->luns[num_filled].lundata[1] = 9061 (targ_lun_id & 0xff); 9062 num_filled++; 9063 } else if (targ_lun_id <= 0xffffff) { 9064 /* 9065 * Extended flat addressing method. 9066 */ 9067 lun_data->luns[num_filled].lundata[0] = 9068 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9069 scsi_ulto3b(targ_lun_id, 9070 &lun_data->luns[num_filled].lundata[1]); 9071 num_filled++; 9072 } else { 9073 printf("ctl_report_luns: bogus LUN number %jd, " 9074 "skipping\n", (intmax_t)targ_lun_id); 9075 } 9076 /* 9077 * According to SPC-3, rev 14 section 6.21: 9078 * 9079 * "The execution of a REPORT LUNS command to any valid and 9080 * installed logical unit shall clear the REPORTED LUNS DATA 9081 * HAS CHANGED unit attention condition for all logical 9082 * units of that target with respect to the requesting 9083 * initiator. A valid and installed logical unit is one 9084 * having a PERIPHERAL QUALIFIER of 000b in the standard 9085 * INQUIRY data (see 6.4.2)." 9086 * 9087 * If request_lun is NULL, the LUN this report luns command 9088 * was issued to is either disabled or doesn't exist. In that 9089 * case, we shouldn't clear any pending lun change unit 9090 * attention. 9091 */ 9092 if (request_lun != NULL) { 9093 mtx_lock(&lun->lun_lock); 9094 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9095 mtx_unlock(&lun->lun_lock); 9096 } 9097 } 9098 mtx_unlock(&softc->ctl_lock); 9099 9100 /* 9101 * It's quite possible that we've returned fewer LUNs than we allocated 9102 * space for. Trim it. 9103 */ 9104 lun_datalen = sizeof(*lun_data) + 9105 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9106 9107 if (lun_datalen < alloc_len) { 9108 ctsio->residual = alloc_len - lun_datalen; 9109 ctsio->kern_data_len = lun_datalen; 9110 ctsio->kern_total_len = lun_datalen; 9111 } else { 9112 ctsio->residual = 0; 9113 ctsio->kern_data_len = alloc_len; 9114 ctsio->kern_total_len = alloc_len; 9115 } 9116 ctsio->kern_data_resid = 0; 9117 ctsio->kern_rel_offset = 0; 9118 ctsio->kern_sg_entries = 0; 9119 9120 /* 9121 * We set this to the actual data length, regardless of how much 9122 * space we actually have to return results. If the user looks at 9123 * this value, he'll know whether or not he allocated enough space 9124 * and reissue the command if necessary. We don't support well 9125 * known logical units, so if the user asks for that, return none. 9126 */ 9127 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9128 9129 /* 9130 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9131 * this request. 9132 */ 9133 ctl_set_success(ctsio); 9134 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9135 ctsio->be_move_done = ctl_config_move_done; 9136 ctl_datamove((union ctl_io *)ctsio); 9137 return (retval); 9138} 9139 9140int 9141ctl_request_sense(struct ctl_scsiio *ctsio) 9142{ 9143 struct scsi_request_sense *cdb; 9144 struct scsi_sense_data *sense_ptr; 9145 struct ctl_softc *ctl_softc; 9146 struct ctl_lun *lun; 9147 uint32_t initidx; 9148 int have_error; 9149 scsi_sense_data_type sense_format; 9150 ctl_ua_type ua_type; 9151 9152 cdb = (struct scsi_request_sense *)ctsio->cdb; 9153 9154 ctl_softc = control_softc; 9155 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9156 9157 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9158 9159 /* 9160 * Determine which sense format the user wants. 9161 */ 9162 if (cdb->byte2 & SRS_DESC) 9163 sense_format = SSD_TYPE_DESC; 9164 else 9165 sense_format = SSD_TYPE_FIXED; 9166 9167 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9168 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9169 ctsio->kern_sg_entries = 0; 9170 9171 /* 9172 * struct scsi_sense_data, which is currently set to 256 bytes, is 9173 * larger than the largest allowed value for the length field in the 9174 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9175 */ 9176 ctsio->residual = 0; 9177 ctsio->kern_data_len = cdb->length; 9178 ctsio->kern_total_len = cdb->length; 9179 9180 ctsio->kern_data_resid = 0; 9181 ctsio->kern_rel_offset = 0; 9182 ctsio->kern_sg_entries = 0; 9183 9184 /* 9185 * If we don't have a LUN, we don't have any pending sense. 9186 */ 9187 if (lun == NULL) 9188 goto no_sense; 9189 9190 have_error = 0; 9191 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9192 /* 9193 * Check for pending sense, and then for pending unit attentions. 9194 * Pending sense gets returned first, then pending unit attentions. 9195 */ 9196 mtx_lock(&lun->lun_lock); 9197#ifdef CTL_WITH_CA 9198 if (ctl_is_set(lun->have_ca, initidx)) { 9199 scsi_sense_data_type stored_format; 9200 9201 /* 9202 * Check to see which sense format was used for the stored 9203 * sense data. 9204 */ 9205 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9206 9207 /* 9208 * If the user requested a different sense format than the 9209 * one we stored, then we need to convert it to the other 9210 * format. If we're going from descriptor to fixed format 9211 * sense data, we may lose things in translation, depending 9212 * on what options were used. 9213 * 9214 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9215 * for some reason we'll just copy it out as-is. 9216 */ 9217 if ((stored_format == SSD_TYPE_FIXED) 9218 && (sense_format == SSD_TYPE_DESC)) 9219 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9220 &lun->pending_sense[initidx], 9221 (struct scsi_sense_data_desc *)sense_ptr); 9222 else if ((stored_format == SSD_TYPE_DESC) 9223 && (sense_format == SSD_TYPE_FIXED)) 9224 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9225 &lun->pending_sense[initidx], 9226 (struct scsi_sense_data_fixed *)sense_ptr); 9227 else 9228 memcpy(sense_ptr, &lun->pending_sense[initidx], 9229 MIN(sizeof(*sense_ptr), 9230 sizeof(lun->pending_sense[initidx]))); 9231 9232 ctl_clear_mask(lun->have_ca, initidx); 9233 have_error = 1; 9234 } else 9235#endif 9236 { 9237 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9238 if (ua_type != CTL_UA_NONE) 9239 have_error = 1; 9240 if (ua_type == CTL_UA_LUN_CHANGE) { 9241 mtx_unlock(&lun->lun_lock); 9242 mtx_lock(&ctl_softc->ctl_lock); 9243 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9244 mtx_unlock(&ctl_softc->ctl_lock); 9245 mtx_lock(&lun->lun_lock); 9246 } 9247 9248 } 9249 mtx_unlock(&lun->lun_lock); 9250 9251 /* 9252 * We already have a pending error, return it. 9253 */ 9254 if (have_error != 0) { 9255 /* 9256 * We report the SCSI status as OK, since the status of the 9257 * request sense command itself is OK. 9258 * We report 0 for the sense length, because we aren't doing 9259 * autosense in this case. We're reporting sense as 9260 * parameter data. 9261 */ 9262 ctl_set_success(ctsio); 9263 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9264 ctsio->be_move_done = ctl_config_move_done; 9265 ctl_datamove((union ctl_io *)ctsio); 9266 return (CTL_RETVAL_COMPLETE); 9267 } 9268 9269no_sense: 9270 9271 /* 9272 * No sense information to report, so we report that everything is 9273 * okay. 9274 */ 9275 ctl_set_sense_data(sense_ptr, 9276 lun, 9277 sense_format, 9278 /*current_error*/ 1, 9279 /*sense_key*/ SSD_KEY_NO_SENSE, 9280 /*asc*/ 0x00, 9281 /*ascq*/ 0x00, 9282 SSD_ELEM_NONE); 9283 9284 /* 9285 * We report 0 for the sense length, because we aren't doing 9286 * autosense in this case. We're reporting sense as parameter data. 9287 */ 9288 ctl_set_success(ctsio); 9289 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9290 ctsio->be_move_done = ctl_config_move_done; 9291 ctl_datamove((union ctl_io *)ctsio); 9292 return (CTL_RETVAL_COMPLETE); 9293} 9294 9295int 9296ctl_tur(struct ctl_scsiio *ctsio) 9297{ 9298 9299 CTL_DEBUG_PRINT(("ctl_tur\n")); 9300 9301 ctl_set_success(ctsio); 9302 ctl_done((union ctl_io *)ctsio); 9303 9304 return (CTL_RETVAL_COMPLETE); 9305} 9306 9307/* 9308 * SCSI VPD page 0x00, the Supported VPD Pages page. 9309 */ 9310static int 9311ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9312{ 9313 struct scsi_vpd_supported_pages *pages; 9314 int sup_page_size; 9315 struct ctl_lun *lun; 9316 int p; 9317 9318 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9319 9320 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9321 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9322 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9323 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9324 ctsio->kern_sg_entries = 0; 9325 9326 if (sup_page_size < alloc_len) { 9327 ctsio->residual = alloc_len - sup_page_size; 9328 ctsio->kern_data_len = sup_page_size; 9329 ctsio->kern_total_len = sup_page_size; 9330 } else { 9331 ctsio->residual = 0; 9332 ctsio->kern_data_len = alloc_len; 9333 ctsio->kern_total_len = alloc_len; 9334 } 9335 ctsio->kern_data_resid = 0; 9336 ctsio->kern_rel_offset = 0; 9337 ctsio->kern_sg_entries = 0; 9338 9339 /* 9340 * The control device is always connected. The disk device, on the 9341 * other hand, may not be online all the time. Need to change this 9342 * to figure out whether the disk device is actually online or not. 9343 */ 9344 if (lun != NULL) 9345 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9346 lun->be_lun->lun_type; 9347 else 9348 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9349 9350 p = 0; 9351 /* Supported VPD pages */ 9352 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9353 /* Serial Number */ 9354 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9355 /* Device Identification */ 9356 pages->page_list[p++] = SVPD_DEVICE_ID; 9357 /* Extended INQUIRY Data */ 9358 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9359 /* Mode Page Policy */ 9360 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9361 /* SCSI Ports */ 9362 pages->page_list[p++] = SVPD_SCSI_PORTS; 9363 /* Third-party Copy */ 9364 pages->page_list[p++] = SVPD_SCSI_TPC; 9365 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9366 /* Block limits */ 9367 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9368 /* Block Device Characteristics */ 9369 pages->page_list[p++] = SVPD_BDC; 9370 /* Logical Block Provisioning */ 9371 pages->page_list[p++] = SVPD_LBP; 9372 } 9373 pages->length = p; 9374 9375 ctl_set_success(ctsio); 9376 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9377 ctsio->be_move_done = ctl_config_move_done; 9378 ctl_datamove((union ctl_io *)ctsio); 9379 return (CTL_RETVAL_COMPLETE); 9380} 9381 9382/* 9383 * SCSI VPD page 0x80, the Unit Serial Number page. 9384 */ 9385static int 9386ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9387{ 9388 struct scsi_vpd_unit_serial_number *sn_ptr; 9389 struct ctl_lun *lun; 9390 int data_len; 9391 9392 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9393 9394 data_len = 4 + CTL_SN_LEN; 9395 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9396 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9397 if (data_len < alloc_len) { 9398 ctsio->residual = alloc_len - data_len; 9399 ctsio->kern_data_len = data_len; 9400 ctsio->kern_total_len = data_len; 9401 } else { 9402 ctsio->residual = 0; 9403 ctsio->kern_data_len = alloc_len; 9404 ctsio->kern_total_len = alloc_len; 9405 } 9406 ctsio->kern_data_resid = 0; 9407 ctsio->kern_rel_offset = 0; 9408 ctsio->kern_sg_entries = 0; 9409 9410 /* 9411 * The control device is always connected. The disk device, on the 9412 * other hand, may not be online all the time. Need to change this 9413 * to figure out whether the disk device is actually online or not. 9414 */ 9415 if (lun != NULL) 9416 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9417 lun->be_lun->lun_type; 9418 else 9419 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9420 9421 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9422 sn_ptr->length = CTL_SN_LEN; 9423 /* 9424 * If we don't have a LUN, we just leave the serial number as 9425 * all spaces. 9426 */ 9427 if (lun != NULL) { 9428 strncpy((char *)sn_ptr->serial_num, 9429 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9430 } else 9431 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9432 9433 ctl_set_success(ctsio); 9434 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9435 ctsio->be_move_done = ctl_config_move_done; 9436 ctl_datamove((union ctl_io *)ctsio); 9437 return (CTL_RETVAL_COMPLETE); 9438} 9439 9440 9441/* 9442 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9443 */ 9444static int 9445ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9446{ 9447 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9448 struct ctl_lun *lun; 9449 int data_len; 9450 9451 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9452 9453 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9454 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9455 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9456 ctsio->kern_sg_entries = 0; 9457 9458 if (data_len < alloc_len) { 9459 ctsio->residual = alloc_len - data_len; 9460 ctsio->kern_data_len = data_len; 9461 ctsio->kern_total_len = data_len; 9462 } else { 9463 ctsio->residual = 0; 9464 ctsio->kern_data_len = alloc_len; 9465 ctsio->kern_total_len = alloc_len; 9466 } 9467 ctsio->kern_data_resid = 0; 9468 ctsio->kern_rel_offset = 0; 9469 ctsio->kern_sg_entries = 0; 9470 9471 /* 9472 * The control device is always connected. The disk device, on the 9473 * other hand, may not be online all the time. 9474 */ 9475 if (lun != NULL) 9476 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9477 lun->be_lun->lun_type; 9478 else 9479 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9480 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9481 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9482 /* 9483 * We support head of queue, ordered and simple tags. 9484 */ 9485 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9486 /* 9487 * Volatile cache supported. 9488 */ 9489 eid_ptr->flags3 = SVPD_EID_V_SUP; 9490 9491 /* 9492 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9493 * attention for a particular IT nexus on all LUNs once we report 9494 * it to that nexus once. This bit is required as of SPC-4. 9495 */ 9496 eid_ptr->flags4 = SVPD_EID_LUICLT; 9497 9498 /* 9499 * XXX KDM in order to correctly answer this, we would need 9500 * information from the SIM to determine how much sense data it 9501 * can send. So this would really be a path inquiry field, most 9502 * likely. This can be set to a maximum of 252 according to SPC-4, 9503 * but the hardware may or may not be able to support that much. 9504 * 0 just means that the maximum sense data length is not reported. 9505 */ 9506 eid_ptr->max_sense_length = 0; 9507 9508 ctl_set_success(ctsio); 9509 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9510 ctsio->be_move_done = ctl_config_move_done; 9511 ctl_datamove((union ctl_io *)ctsio); 9512 return (CTL_RETVAL_COMPLETE); 9513} 9514 9515static int 9516ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9517{ 9518 struct scsi_vpd_mode_page_policy *mpp_ptr; 9519 struct ctl_lun *lun; 9520 int data_len; 9521 9522 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9523 9524 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9525 sizeof(struct scsi_vpd_mode_page_policy_descr); 9526 9527 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9528 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9529 ctsio->kern_sg_entries = 0; 9530 9531 if (data_len < alloc_len) { 9532 ctsio->residual = alloc_len - data_len; 9533 ctsio->kern_data_len = data_len; 9534 ctsio->kern_total_len = data_len; 9535 } else { 9536 ctsio->residual = 0; 9537 ctsio->kern_data_len = alloc_len; 9538 ctsio->kern_total_len = alloc_len; 9539 } 9540 ctsio->kern_data_resid = 0; 9541 ctsio->kern_rel_offset = 0; 9542 ctsio->kern_sg_entries = 0; 9543 9544 /* 9545 * The control device is always connected. The disk device, on the 9546 * other hand, may not be online all the time. 9547 */ 9548 if (lun != NULL) 9549 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9550 lun->be_lun->lun_type; 9551 else 9552 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9553 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9554 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9555 mpp_ptr->descr[0].page_code = 0x3f; 9556 mpp_ptr->descr[0].subpage_code = 0xff; 9557 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9558 9559 ctl_set_success(ctsio); 9560 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9561 ctsio->be_move_done = ctl_config_move_done; 9562 ctl_datamove((union ctl_io *)ctsio); 9563 return (CTL_RETVAL_COMPLETE); 9564} 9565 9566/* 9567 * SCSI VPD page 0x83, the Device Identification page. 9568 */ 9569static int 9570ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9571{ 9572 struct scsi_vpd_device_id *devid_ptr; 9573 struct scsi_vpd_id_descriptor *desc; 9574 struct ctl_softc *softc; 9575 struct ctl_lun *lun; 9576 struct ctl_port *port; 9577 int data_len; 9578 uint8_t proto; 9579 9580 softc = control_softc; 9581 9582 port = ctl_io_port(&ctsio->io_hdr); 9583 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9584 9585 data_len = sizeof(struct scsi_vpd_device_id) + 9586 sizeof(struct scsi_vpd_id_descriptor) + 9587 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9588 sizeof(struct scsi_vpd_id_descriptor) + 9589 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9590 if (lun && lun->lun_devid) 9591 data_len += lun->lun_devid->len; 9592 if (port && port->port_devid) 9593 data_len += port->port_devid->len; 9594 if (port && port->target_devid) 9595 data_len += port->target_devid->len; 9596 9597 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9598 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9599 ctsio->kern_sg_entries = 0; 9600 9601 if (data_len < alloc_len) { 9602 ctsio->residual = alloc_len - data_len; 9603 ctsio->kern_data_len = data_len; 9604 ctsio->kern_total_len = data_len; 9605 } else { 9606 ctsio->residual = 0; 9607 ctsio->kern_data_len = alloc_len; 9608 ctsio->kern_total_len = alloc_len; 9609 } 9610 ctsio->kern_data_resid = 0; 9611 ctsio->kern_rel_offset = 0; 9612 ctsio->kern_sg_entries = 0; 9613 9614 /* 9615 * The control device is always connected. The disk device, on the 9616 * other hand, may not be online all the time. 9617 */ 9618 if (lun != NULL) 9619 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9620 lun->be_lun->lun_type; 9621 else 9622 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9623 devid_ptr->page_code = SVPD_DEVICE_ID; 9624 scsi_ulto2b(data_len - 4, devid_ptr->length); 9625 9626 if (port && port->port_type == CTL_PORT_FC) 9627 proto = SCSI_PROTO_FC << 4; 9628 else if (port && port->port_type == CTL_PORT_ISCSI) 9629 proto = SCSI_PROTO_ISCSI << 4; 9630 else 9631 proto = SCSI_PROTO_SPI << 4; 9632 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9633 9634 /* 9635 * We're using a LUN association here. i.e., this device ID is a 9636 * per-LUN identifier. 9637 */ 9638 if (lun && lun->lun_devid) { 9639 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9640 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9641 lun->lun_devid->len); 9642 } 9643 9644 /* 9645 * This is for the WWPN which is a port association. 9646 */ 9647 if (port && port->port_devid) { 9648 memcpy(desc, port->port_devid->data, port->port_devid->len); 9649 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9650 port->port_devid->len); 9651 } 9652 9653 /* 9654 * This is for the Relative Target Port(type 4h) identifier 9655 */ 9656 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9657 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9658 SVPD_ID_TYPE_RELTARG; 9659 desc->length = 4; 9660 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9661 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9662 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9663 9664 /* 9665 * This is for the Target Port Group(type 5h) identifier 9666 */ 9667 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9668 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9669 SVPD_ID_TYPE_TPORTGRP; 9670 desc->length = 4; 9671 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 9672 &desc->identifier[2]); 9673 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9674 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9675 9676 /* 9677 * This is for the Target identifier 9678 */ 9679 if (port && port->target_devid) { 9680 memcpy(desc, port->target_devid->data, port->target_devid->len); 9681 } 9682 9683 ctl_set_success(ctsio); 9684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9685 ctsio->be_move_done = ctl_config_move_done; 9686 ctl_datamove((union ctl_io *)ctsio); 9687 return (CTL_RETVAL_COMPLETE); 9688} 9689 9690static int 9691ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9692{ 9693 struct ctl_softc *softc = control_softc; 9694 struct scsi_vpd_scsi_ports *sp; 9695 struct scsi_vpd_port_designation *pd; 9696 struct scsi_vpd_port_designation_cont *pdc; 9697 struct ctl_lun *lun; 9698 struct ctl_port *port; 9699 int data_len, num_target_ports, iid_len, id_len; 9700 9701 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9702 9703 num_target_ports = 0; 9704 iid_len = 0; 9705 id_len = 0; 9706 mtx_lock(&softc->ctl_lock); 9707 STAILQ_FOREACH(port, &softc->port_list, links) { 9708 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9709 continue; 9710 if (lun != NULL && 9711 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9712 continue; 9713 num_target_ports++; 9714 if (port->init_devid) 9715 iid_len += port->init_devid->len; 9716 if (port->port_devid) 9717 id_len += port->port_devid->len; 9718 } 9719 mtx_unlock(&softc->ctl_lock); 9720 9721 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9722 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9723 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9724 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9725 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9726 ctsio->kern_sg_entries = 0; 9727 9728 if (data_len < alloc_len) { 9729 ctsio->residual = alloc_len - data_len; 9730 ctsio->kern_data_len = data_len; 9731 ctsio->kern_total_len = data_len; 9732 } else { 9733 ctsio->residual = 0; 9734 ctsio->kern_data_len = alloc_len; 9735 ctsio->kern_total_len = alloc_len; 9736 } 9737 ctsio->kern_data_resid = 0; 9738 ctsio->kern_rel_offset = 0; 9739 ctsio->kern_sg_entries = 0; 9740 9741 /* 9742 * The control device is always connected. The disk device, on the 9743 * other hand, may not be online all the time. Need to change this 9744 * to figure out whether the disk device is actually online or not. 9745 */ 9746 if (lun != NULL) 9747 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9748 lun->be_lun->lun_type; 9749 else 9750 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9751 9752 sp->page_code = SVPD_SCSI_PORTS; 9753 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9754 sp->page_length); 9755 pd = &sp->design[0]; 9756 9757 mtx_lock(&softc->ctl_lock); 9758 STAILQ_FOREACH(port, &softc->port_list, links) { 9759 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9760 continue; 9761 if (lun != NULL && 9762 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9763 continue; 9764 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9765 if (port->init_devid) { 9766 iid_len = port->init_devid->len; 9767 memcpy(pd->initiator_transportid, 9768 port->init_devid->data, port->init_devid->len); 9769 } else 9770 iid_len = 0; 9771 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9772 pdc = (struct scsi_vpd_port_designation_cont *) 9773 (&pd->initiator_transportid[iid_len]); 9774 if (port->port_devid) { 9775 id_len = port->port_devid->len; 9776 memcpy(pdc->target_port_descriptors, 9777 port->port_devid->data, port->port_devid->len); 9778 } else 9779 id_len = 0; 9780 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9781 pd = (struct scsi_vpd_port_designation *) 9782 ((uint8_t *)pdc->target_port_descriptors + id_len); 9783 } 9784 mtx_unlock(&softc->ctl_lock); 9785 9786 ctl_set_success(ctsio); 9787 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9788 ctsio->be_move_done = ctl_config_move_done; 9789 ctl_datamove((union ctl_io *)ctsio); 9790 return (CTL_RETVAL_COMPLETE); 9791} 9792 9793static int 9794ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9795{ 9796 struct scsi_vpd_block_limits *bl_ptr; 9797 struct ctl_lun *lun; 9798 int bs; 9799 9800 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9801 9802 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9803 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9804 ctsio->kern_sg_entries = 0; 9805 9806 if (sizeof(*bl_ptr) < alloc_len) { 9807 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9808 ctsio->kern_data_len = sizeof(*bl_ptr); 9809 ctsio->kern_total_len = sizeof(*bl_ptr); 9810 } else { 9811 ctsio->residual = 0; 9812 ctsio->kern_data_len = alloc_len; 9813 ctsio->kern_total_len = alloc_len; 9814 } 9815 ctsio->kern_data_resid = 0; 9816 ctsio->kern_rel_offset = 0; 9817 ctsio->kern_sg_entries = 0; 9818 9819 /* 9820 * The control device is always connected. The disk device, on the 9821 * other hand, may not be online all the time. Need to change this 9822 * to figure out whether the disk device is actually online or not. 9823 */ 9824 if (lun != NULL) 9825 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9826 lun->be_lun->lun_type; 9827 else 9828 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9829 9830 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9831 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9832 bl_ptr->max_cmp_write_len = 0xff; 9833 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9834 if (lun != NULL) { 9835 bs = lun->be_lun->blocksize; 9836 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9837 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9838 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9839 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9840 if (lun->be_lun->ublockexp != 0) { 9841 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9842 bl_ptr->opt_unmap_grain); 9843 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9844 bl_ptr->unmap_grain_align); 9845 } 9846 } 9847 scsi_ulto4b(lun->be_lun->atomicblock, 9848 bl_ptr->max_atomic_transfer_length); 9849 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9850 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9851 } 9852 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9853 9854 ctl_set_success(ctsio); 9855 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9856 ctsio->be_move_done = ctl_config_move_done; 9857 ctl_datamove((union ctl_io *)ctsio); 9858 return (CTL_RETVAL_COMPLETE); 9859} 9860 9861static int 9862ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9863{ 9864 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9865 struct ctl_lun *lun; 9866 const char *value; 9867 u_int i; 9868 9869 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9870 9871 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9872 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9873 ctsio->kern_sg_entries = 0; 9874 9875 if (sizeof(*bdc_ptr) < alloc_len) { 9876 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9877 ctsio->kern_data_len = sizeof(*bdc_ptr); 9878 ctsio->kern_total_len = sizeof(*bdc_ptr); 9879 } else { 9880 ctsio->residual = 0; 9881 ctsio->kern_data_len = alloc_len; 9882 ctsio->kern_total_len = alloc_len; 9883 } 9884 ctsio->kern_data_resid = 0; 9885 ctsio->kern_rel_offset = 0; 9886 ctsio->kern_sg_entries = 0; 9887 9888 /* 9889 * The control device is always connected. The disk device, on the 9890 * other hand, may not be online all the time. Need to change this 9891 * to figure out whether the disk device is actually online or not. 9892 */ 9893 if (lun != NULL) 9894 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9895 lun->be_lun->lun_type; 9896 else 9897 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9898 bdc_ptr->page_code = SVPD_BDC; 9899 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9900 if (lun != NULL && 9901 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9902 i = strtol(value, NULL, 0); 9903 else 9904 i = CTL_DEFAULT_ROTATION_RATE; 9905 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9906 if (lun != NULL && 9907 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9908 i = strtol(value, NULL, 0); 9909 else 9910 i = 0; 9911 bdc_ptr->wab_wac_ff = (i & 0x0f); 9912 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9913 9914 ctl_set_success(ctsio); 9915 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9916 ctsio->be_move_done = ctl_config_move_done; 9917 ctl_datamove((union ctl_io *)ctsio); 9918 return (CTL_RETVAL_COMPLETE); 9919} 9920 9921static int 9922ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9923{ 9924 struct scsi_vpd_logical_block_prov *lbp_ptr; 9925 struct ctl_lun *lun; 9926 9927 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9928 9929 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9930 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9931 ctsio->kern_sg_entries = 0; 9932 9933 if (sizeof(*lbp_ptr) < alloc_len) { 9934 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9935 ctsio->kern_data_len = sizeof(*lbp_ptr); 9936 ctsio->kern_total_len = sizeof(*lbp_ptr); 9937 } else { 9938 ctsio->residual = 0; 9939 ctsio->kern_data_len = alloc_len; 9940 ctsio->kern_total_len = alloc_len; 9941 } 9942 ctsio->kern_data_resid = 0; 9943 ctsio->kern_rel_offset = 0; 9944 ctsio->kern_sg_entries = 0; 9945 9946 /* 9947 * The control device is always connected. The disk device, on the 9948 * other hand, may not be online all the time. Need to change this 9949 * to figure out whether the disk device is actually online or not. 9950 */ 9951 if (lun != NULL) 9952 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9953 lun->be_lun->lun_type; 9954 else 9955 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9956 9957 lbp_ptr->page_code = SVPD_LBP; 9958 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9959 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9960 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9961 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9962 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9963 lbp_ptr->prov_type = SVPD_LBP_THIN; 9964 } 9965 9966 ctl_set_success(ctsio); 9967 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9968 ctsio->be_move_done = ctl_config_move_done; 9969 ctl_datamove((union ctl_io *)ctsio); 9970 return (CTL_RETVAL_COMPLETE); 9971} 9972 9973/* 9974 * INQUIRY with the EVPD bit set. 9975 */ 9976static int 9977ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9978{ 9979 struct ctl_lun *lun; 9980 struct scsi_inquiry *cdb; 9981 int alloc_len, retval; 9982 9983 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9984 cdb = (struct scsi_inquiry *)ctsio->cdb; 9985 alloc_len = scsi_2btoul(cdb->length); 9986 9987 switch (cdb->page_code) { 9988 case SVPD_SUPPORTED_PAGES: 9989 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9990 break; 9991 case SVPD_UNIT_SERIAL_NUMBER: 9992 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9993 break; 9994 case SVPD_DEVICE_ID: 9995 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9996 break; 9997 case SVPD_EXTENDED_INQUIRY_DATA: 9998 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 9999 break; 10000 case SVPD_MODE_PAGE_POLICY: 10001 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10002 break; 10003 case SVPD_SCSI_PORTS: 10004 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10005 break; 10006 case SVPD_SCSI_TPC: 10007 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10008 break; 10009 case SVPD_BLOCK_LIMITS: 10010 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10011 goto err; 10012 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10013 break; 10014 case SVPD_BDC: 10015 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10016 goto err; 10017 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10018 break; 10019 case SVPD_LBP: 10020 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10021 goto err; 10022 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10023 break; 10024 default: 10025err: 10026 ctl_set_invalid_field(ctsio, 10027 /*sks_valid*/ 1, 10028 /*command*/ 1, 10029 /*field*/ 2, 10030 /*bit_valid*/ 0, 10031 /*bit*/ 0); 10032 ctl_done((union ctl_io *)ctsio); 10033 retval = CTL_RETVAL_COMPLETE; 10034 break; 10035 } 10036 10037 return (retval); 10038} 10039 10040/* 10041 * Standard INQUIRY data. 10042 */ 10043static int 10044ctl_inquiry_std(struct ctl_scsiio *ctsio) 10045{ 10046 struct scsi_inquiry_data *inq_ptr; 10047 struct scsi_inquiry *cdb; 10048 struct ctl_softc *softc; 10049 struct ctl_port *port; 10050 struct ctl_lun *lun; 10051 char *val; 10052 uint32_t alloc_len, data_len; 10053 ctl_port_type port_type; 10054 10055 softc = control_softc; 10056 10057 /* 10058 * Figure out whether we're talking to a Fibre Channel port or not. 10059 * We treat the ioctl front end, and any SCSI adapters, as packetized 10060 * SCSI front ends. 10061 */ 10062 port = ctl_io_port(&ctsio->io_hdr); 10063 if (port != NULL) 10064 port_type = port->port_type; 10065 else 10066 port_type = CTL_PORT_SCSI; 10067 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10068 port_type = CTL_PORT_SCSI; 10069 10070 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10071 cdb = (struct scsi_inquiry *)ctsio->cdb; 10072 alloc_len = scsi_2btoul(cdb->length); 10073 10074 /* 10075 * We malloc the full inquiry data size here and fill it 10076 * in. If the user only asks for less, we'll give him 10077 * that much. 10078 */ 10079 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10080 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10081 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10082 ctsio->kern_sg_entries = 0; 10083 ctsio->kern_data_resid = 0; 10084 ctsio->kern_rel_offset = 0; 10085 10086 if (data_len < alloc_len) { 10087 ctsio->residual = alloc_len - data_len; 10088 ctsio->kern_data_len = data_len; 10089 ctsio->kern_total_len = data_len; 10090 } else { 10091 ctsio->residual = 0; 10092 ctsio->kern_data_len = alloc_len; 10093 ctsio->kern_total_len = alloc_len; 10094 } 10095 10096 if (lun != NULL) { 10097 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10098 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10099 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10100 lun->be_lun->lun_type; 10101 } else { 10102 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10103 lun->be_lun->lun_type; 10104 } 10105 } else 10106 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10107 10108 /* RMB in byte 2 is 0 */ 10109 inq_ptr->version = SCSI_REV_SPC4; 10110 10111 /* 10112 * According to SAM-3, even if a device only supports a single 10113 * level of LUN addressing, it should still set the HISUP bit: 10114 * 10115 * 4.9.1 Logical unit numbers overview 10116 * 10117 * All logical unit number formats described in this standard are 10118 * hierarchical in structure even when only a single level in that 10119 * hierarchy is used. The HISUP bit shall be set to one in the 10120 * standard INQUIRY data (see SPC-2) when any logical unit number 10121 * format described in this standard is used. Non-hierarchical 10122 * formats are outside the scope of this standard. 10123 * 10124 * Therefore we set the HiSup bit here. 10125 * 10126 * The reponse format is 2, per SPC-3. 10127 */ 10128 inq_ptr->response_format = SID_HiSup | 2; 10129 10130 inq_ptr->additional_length = data_len - 10131 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10132 CTL_DEBUG_PRINT(("additional_length = %d\n", 10133 inq_ptr->additional_length)); 10134 10135 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10136 /* 16 bit addressing */ 10137 if (port_type == CTL_PORT_SCSI) 10138 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10139 /* XXX set the SID_MultiP bit here if we're actually going to 10140 respond on multiple ports */ 10141 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10142 10143 /* 16 bit data bus, synchronous transfers */ 10144 if (port_type == CTL_PORT_SCSI) 10145 inq_ptr->flags = SID_WBus16 | SID_Sync; 10146 /* 10147 * XXX KDM do we want to support tagged queueing on the control 10148 * device at all? 10149 */ 10150 if ((lun == NULL) 10151 || (lun->be_lun->lun_type != T_PROCESSOR)) 10152 inq_ptr->flags |= SID_CmdQue; 10153 /* 10154 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10155 * We have 8 bytes for the vendor name, and 16 bytes for the device 10156 * name and 4 bytes for the revision. 10157 */ 10158 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10159 "vendor")) == NULL) { 10160 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10161 } else { 10162 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10163 strncpy(inq_ptr->vendor, val, 10164 min(sizeof(inq_ptr->vendor), strlen(val))); 10165 } 10166 if (lun == NULL) { 10167 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10168 sizeof(inq_ptr->product)); 10169 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10170 switch (lun->be_lun->lun_type) { 10171 case T_DIRECT: 10172 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10173 sizeof(inq_ptr->product)); 10174 break; 10175 case T_PROCESSOR: 10176 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10177 sizeof(inq_ptr->product)); 10178 break; 10179 default: 10180 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10181 sizeof(inq_ptr->product)); 10182 break; 10183 } 10184 } else { 10185 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10186 strncpy(inq_ptr->product, val, 10187 min(sizeof(inq_ptr->product), strlen(val))); 10188 } 10189 10190 /* 10191 * XXX make this a macro somewhere so it automatically gets 10192 * incremented when we make changes. 10193 */ 10194 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10195 "revision")) == NULL) { 10196 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10197 } else { 10198 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10199 strncpy(inq_ptr->revision, val, 10200 min(sizeof(inq_ptr->revision), strlen(val))); 10201 } 10202 10203 /* 10204 * For parallel SCSI, we support double transition and single 10205 * transition clocking. We also support QAS (Quick Arbitration 10206 * and Selection) and Information Unit transfers on both the 10207 * control and array devices. 10208 */ 10209 if (port_type == CTL_PORT_SCSI) 10210 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10211 SID_SPI_IUS; 10212 10213 /* SAM-5 (no version claimed) */ 10214 scsi_ulto2b(0x00A0, inq_ptr->version1); 10215 /* SPC-4 (no version claimed) */ 10216 scsi_ulto2b(0x0460, inq_ptr->version2); 10217 if (port_type == CTL_PORT_FC) { 10218 /* FCP-2 ANSI INCITS.350:2003 */ 10219 scsi_ulto2b(0x0917, inq_ptr->version3); 10220 } else if (port_type == CTL_PORT_SCSI) { 10221 /* SPI-4 ANSI INCITS.362:200x */ 10222 scsi_ulto2b(0x0B56, inq_ptr->version3); 10223 } else if (port_type == CTL_PORT_ISCSI) { 10224 /* iSCSI (no version claimed) */ 10225 scsi_ulto2b(0x0960, inq_ptr->version3); 10226 } else if (port_type == CTL_PORT_SAS) { 10227 /* SAS (no version claimed) */ 10228 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10229 } 10230 10231 if (lun == NULL) { 10232 /* SBC-4 (no version claimed) */ 10233 scsi_ulto2b(0x0600, inq_ptr->version4); 10234 } else { 10235 switch (lun->be_lun->lun_type) { 10236 case T_DIRECT: 10237 /* SBC-4 (no version claimed) */ 10238 scsi_ulto2b(0x0600, inq_ptr->version4); 10239 break; 10240 case T_PROCESSOR: 10241 default: 10242 break; 10243 } 10244 } 10245 10246 ctl_set_success(ctsio); 10247 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10248 ctsio->be_move_done = ctl_config_move_done; 10249 ctl_datamove((union ctl_io *)ctsio); 10250 return (CTL_RETVAL_COMPLETE); 10251} 10252 10253int 10254ctl_inquiry(struct ctl_scsiio *ctsio) 10255{ 10256 struct scsi_inquiry *cdb; 10257 int retval; 10258 10259 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10260 10261 cdb = (struct scsi_inquiry *)ctsio->cdb; 10262 if (cdb->byte2 & SI_EVPD) 10263 retval = ctl_inquiry_evpd(ctsio); 10264 else if (cdb->page_code == 0) 10265 retval = ctl_inquiry_std(ctsio); 10266 else { 10267 ctl_set_invalid_field(ctsio, 10268 /*sks_valid*/ 1, 10269 /*command*/ 1, 10270 /*field*/ 2, 10271 /*bit_valid*/ 0, 10272 /*bit*/ 0); 10273 ctl_done((union ctl_io *)ctsio); 10274 return (CTL_RETVAL_COMPLETE); 10275 } 10276 10277 return (retval); 10278} 10279 10280/* 10281 * For known CDB types, parse the LBA and length. 10282 */ 10283static int 10284ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10285{ 10286 if (io->io_hdr.io_type != CTL_IO_SCSI) 10287 return (1); 10288 10289 switch (io->scsiio.cdb[0]) { 10290 case COMPARE_AND_WRITE: { 10291 struct scsi_compare_and_write *cdb; 10292 10293 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10294 10295 *lba = scsi_8btou64(cdb->addr); 10296 *len = cdb->length; 10297 break; 10298 } 10299 case READ_6: 10300 case WRITE_6: { 10301 struct scsi_rw_6 *cdb; 10302 10303 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10304 10305 *lba = scsi_3btoul(cdb->addr); 10306 /* only 5 bits are valid in the most significant address byte */ 10307 *lba &= 0x1fffff; 10308 *len = cdb->length; 10309 break; 10310 } 10311 case READ_10: 10312 case WRITE_10: { 10313 struct scsi_rw_10 *cdb; 10314 10315 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10316 10317 *lba = scsi_4btoul(cdb->addr); 10318 *len = scsi_2btoul(cdb->length); 10319 break; 10320 } 10321 case WRITE_VERIFY_10: { 10322 struct scsi_write_verify_10 *cdb; 10323 10324 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10325 10326 *lba = scsi_4btoul(cdb->addr); 10327 *len = scsi_2btoul(cdb->length); 10328 break; 10329 } 10330 case READ_12: 10331 case WRITE_12: { 10332 struct scsi_rw_12 *cdb; 10333 10334 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10335 10336 *lba = scsi_4btoul(cdb->addr); 10337 *len = scsi_4btoul(cdb->length); 10338 break; 10339 } 10340 case WRITE_VERIFY_12: { 10341 struct scsi_write_verify_12 *cdb; 10342 10343 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10344 10345 *lba = scsi_4btoul(cdb->addr); 10346 *len = scsi_4btoul(cdb->length); 10347 break; 10348 } 10349 case READ_16: 10350 case WRITE_16: 10351 case WRITE_ATOMIC_16: { 10352 struct scsi_rw_16 *cdb; 10353 10354 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10355 10356 *lba = scsi_8btou64(cdb->addr); 10357 *len = scsi_4btoul(cdb->length); 10358 break; 10359 } 10360 case WRITE_VERIFY_16: { 10361 struct scsi_write_verify_16 *cdb; 10362 10363 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10364 10365 *lba = scsi_8btou64(cdb->addr); 10366 *len = scsi_4btoul(cdb->length); 10367 break; 10368 } 10369 case WRITE_SAME_10: { 10370 struct scsi_write_same_10 *cdb; 10371 10372 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10373 10374 *lba = scsi_4btoul(cdb->addr); 10375 *len = scsi_2btoul(cdb->length); 10376 break; 10377 } 10378 case WRITE_SAME_16: { 10379 struct scsi_write_same_16 *cdb; 10380 10381 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10382 10383 *lba = scsi_8btou64(cdb->addr); 10384 *len = scsi_4btoul(cdb->length); 10385 break; 10386 } 10387 case VERIFY_10: { 10388 struct scsi_verify_10 *cdb; 10389 10390 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10391 10392 *lba = scsi_4btoul(cdb->addr); 10393 *len = scsi_2btoul(cdb->length); 10394 break; 10395 } 10396 case VERIFY_12: { 10397 struct scsi_verify_12 *cdb; 10398 10399 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10400 10401 *lba = scsi_4btoul(cdb->addr); 10402 *len = scsi_4btoul(cdb->length); 10403 break; 10404 } 10405 case VERIFY_16: { 10406 struct scsi_verify_16 *cdb; 10407 10408 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10409 10410 *lba = scsi_8btou64(cdb->addr); 10411 *len = scsi_4btoul(cdb->length); 10412 break; 10413 } 10414 case UNMAP: { 10415 *lba = 0; 10416 *len = UINT64_MAX; 10417 break; 10418 } 10419 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10420 struct scsi_get_lba_status *cdb; 10421 10422 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10423 *lba = scsi_8btou64(cdb->addr); 10424 *len = UINT32_MAX; 10425 break; 10426 } 10427 default: 10428 return (1); 10429 break; /* NOTREACHED */ 10430 } 10431 10432 return (0); 10433} 10434 10435static ctl_action 10436ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10437 bool seq) 10438{ 10439 uint64_t endlba1, endlba2; 10440 10441 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10442 endlba2 = lba2 + len2 - 1; 10443 10444 if ((endlba1 < lba2) || (endlba2 < lba1)) 10445 return (CTL_ACTION_PASS); 10446 else 10447 return (CTL_ACTION_BLOCK); 10448} 10449 10450static int 10451ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10452{ 10453 struct ctl_ptr_len_flags *ptrlen; 10454 struct scsi_unmap_desc *buf, *end, *range; 10455 uint64_t lba; 10456 uint32_t len; 10457 10458 /* If not UNMAP -- go other way. */ 10459 if (io->io_hdr.io_type != CTL_IO_SCSI || 10460 io->scsiio.cdb[0] != UNMAP) 10461 return (CTL_ACTION_ERROR); 10462 10463 /* If UNMAP without data -- block and wait for data. */ 10464 ptrlen = (struct ctl_ptr_len_flags *) 10465 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10466 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10467 ptrlen->ptr == NULL) 10468 return (CTL_ACTION_BLOCK); 10469 10470 /* UNMAP with data -- check for collision. */ 10471 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10472 end = buf + ptrlen->len / sizeof(*buf); 10473 for (range = buf; range < end; range++) { 10474 lba = scsi_8btou64(range->lba); 10475 len = scsi_4btoul(range->length); 10476 if ((lba < lba2 + len2) && (lba + len > lba2)) 10477 return (CTL_ACTION_BLOCK); 10478 } 10479 return (CTL_ACTION_PASS); 10480} 10481 10482static ctl_action 10483ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10484{ 10485 uint64_t lba1, lba2; 10486 uint64_t len1, len2; 10487 int retval; 10488 10489 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10490 return (CTL_ACTION_ERROR); 10491 10492 retval = ctl_extent_check_unmap(io1, lba2, len2); 10493 if (retval != CTL_ACTION_ERROR) 10494 return (retval); 10495 10496 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10497 return (CTL_ACTION_ERROR); 10498 10499 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10500} 10501 10502static ctl_action 10503ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10504{ 10505 uint64_t lba1, lba2; 10506 uint64_t len1, len2; 10507 10508 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10509 return (CTL_ACTION_ERROR); 10510 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10511 return (CTL_ACTION_ERROR); 10512 10513 if (lba1 + len1 == lba2) 10514 return (CTL_ACTION_BLOCK); 10515 return (CTL_ACTION_PASS); 10516} 10517 10518static ctl_action 10519ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10520 union ctl_io *ooa_io) 10521{ 10522 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10523 ctl_serialize_action *serialize_row; 10524 10525 /* 10526 * The initiator attempted multiple untagged commands at the same 10527 * time. Can't do that. 10528 */ 10529 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10530 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10531 && ((pending_io->io_hdr.nexus.targ_port == 10532 ooa_io->io_hdr.nexus.targ_port) 10533 && (pending_io->io_hdr.nexus.initid == 10534 ooa_io->io_hdr.nexus.initid)) 10535 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10536 CTL_FLAG_STATUS_SENT)) == 0)) 10537 return (CTL_ACTION_OVERLAP); 10538 10539 /* 10540 * The initiator attempted to send multiple tagged commands with 10541 * the same ID. (It's fine if different initiators have the same 10542 * tag ID.) 10543 * 10544 * Even if all of those conditions are true, we don't kill the I/O 10545 * if the command ahead of us has been aborted. We won't end up 10546 * sending it to the FETD, and it's perfectly legal to resend a 10547 * command with the same tag number as long as the previous 10548 * instance of this tag number has been aborted somehow. 10549 */ 10550 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10551 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10552 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10553 && ((pending_io->io_hdr.nexus.targ_port == 10554 ooa_io->io_hdr.nexus.targ_port) 10555 && (pending_io->io_hdr.nexus.initid == 10556 ooa_io->io_hdr.nexus.initid)) 10557 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10558 CTL_FLAG_STATUS_SENT)) == 0)) 10559 return (CTL_ACTION_OVERLAP_TAG); 10560 10561 /* 10562 * If we get a head of queue tag, SAM-3 says that we should 10563 * immediately execute it. 10564 * 10565 * What happens if this command would normally block for some other 10566 * reason? e.g. a request sense with a head of queue tag 10567 * immediately after a write. Normally that would block, but this 10568 * will result in its getting executed immediately... 10569 * 10570 * We currently return "pass" instead of "skip", so we'll end up 10571 * going through the rest of the queue to check for overlapped tags. 10572 * 10573 * XXX KDM check for other types of blockage first?? 10574 */ 10575 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10576 return (CTL_ACTION_PASS); 10577 10578 /* 10579 * Ordered tags have to block until all items ahead of them 10580 * have completed. If we get called with an ordered tag, we always 10581 * block, if something else is ahead of us in the queue. 10582 */ 10583 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10584 return (CTL_ACTION_BLOCK); 10585 10586 /* 10587 * Simple tags get blocked until all head of queue and ordered tags 10588 * ahead of them have completed. I'm lumping untagged commands in 10589 * with simple tags here. XXX KDM is that the right thing to do? 10590 */ 10591 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10592 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10593 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10594 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10595 return (CTL_ACTION_BLOCK); 10596 10597 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10598 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10599 10600 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10601 10602 switch (serialize_row[pending_entry->seridx]) { 10603 case CTL_SER_BLOCK: 10604 return (CTL_ACTION_BLOCK); 10605 case CTL_SER_EXTENT: 10606 return (ctl_extent_check(ooa_io, pending_io, 10607 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10608 case CTL_SER_EXTENTOPT: 10609 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10610 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10611 return (ctl_extent_check(ooa_io, pending_io, 10612 (lun->be_lun && 10613 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10614 return (CTL_ACTION_PASS); 10615 case CTL_SER_EXTENTSEQ: 10616 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10617 return (ctl_extent_check_seq(ooa_io, pending_io)); 10618 return (CTL_ACTION_PASS); 10619 case CTL_SER_PASS: 10620 return (CTL_ACTION_PASS); 10621 case CTL_SER_BLOCKOPT: 10622 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10623 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10624 return (CTL_ACTION_BLOCK); 10625 return (CTL_ACTION_PASS); 10626 case CTL_SER_SKIP: 10627 return (CTL_ACTION_SKIP); 10628 default: 10629 panic("invalid serialization value %d", 10630 serialize_row[pending_entry->seridx]); 10631 } 10632 10633 return (CTL_ACTION_ERROR); 10634} 10635 10636/* 10637 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10638 * Assumptions: 10639 * - pending_io is generally either incoming, or on the blocked queue 10640 * - starting I/O is the I/O we want to start the check with. 10641 */ 10642static ctl_action 10643ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10644 union ctl_io *starting_io) 10645{ 10646 union ctl_io *ooa_io; 10647 ctl_action action; 10648 10649 mtx_assert(&lun->lun_lock, MA_OWNED); 10650 10651 /* 10652 * Run back along the OOA queue, starting with the current 10653 * blocked I/O and going through every I/O before it on the 10654 * queue. If starting_io is NULL, we'll just end up returning 10655 * CTL_ACTION_PASS. 10656 */ 10657 for (ooa_io = starting_io; ooa_io != NULL; 10658 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10659 ooa_links)){ 10660 10661 /* 10662 * This routine just checks to see whether 10663 * cur_blocked is blocked by ooa_io, which is ahead 10664 * of it in the queue. It doesn't queue/dequeue 10665 * cur_blocked. 10666 */ 10667 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10668 switch (action) { 10669 case CTL_ACTION_BLOCK: 10670 case CTL_ACTION_OVERLAP: 10671 case CTL_ACTION_OVERLAP_TAG: 10672 case CTL_ACTION_SKIP: 10673 case CTL_ACTION_ERROR: 10674 return (action); 10675 break; /* NOTREACHED */ 10676 case CTL_ACTION_PASS: 10677 break; 10678 default: 10679 panic("invalid action %d", action); 10680 break; /* NOTREACHED */ 10681 } 10682 } 10683 10684 return (CTL_ACTION_PASS); 10685} 10686 10687/* 10688 * Assumptions: 10689 * - An I/O has just completed, and has been removed from the per-LUN OOA 10690 * queue, so some items on the blocked queue may now be unblocked. 10691 */ 10692static int 10693ctl_check_blocked(struct ctl_lun *lun) 10694{ 10695 struct ctl_softc *softc = lun->ctl_softc; 10696 union ctl_io *cur_blocked, *next_blocked; 10697 10698 mtx_assert(&lun->lun_lock, MA_OWNED); 10699 10700 /* 10701 * Run forward from the head of the blocked queue, checking each 10702 * entry against the I/Os prior to it on the OOA queue to see if 10703 * there is still any blockage. 10704 * 10705 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10706 * with our removing a variable on it while it is traversing the 10707 * list. 10708 */ 10709 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10710 cur_blocked != NULL; cur_blocked = next_blocked) { 10711 union ctl_io *prev_ooa; 10712 ctl_action action; 10713 10714 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10715 blocked_links); 10716 10717 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10718 ctl_ooaq, ooa_links); 10719 10720 /* 10721 * If cur_blocked happens to be the first item in the OOA 10722 * queue now, prev_ooa will be NULL, and the action 10723 * returned will just be CTL_ACTION_PASS. 10724 */ 10725 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10726 10727 switch (action) { 10728 case CTL_ACTION_BLOCK: 10729 /* Nothing to do here, still blocked */ 10730 break; 10731 case CTL_ACTION_OVERLAP: 10732 case CTL_ACTION_OVERLAP_TAG: 10733 /* 10734 * This shouldn't happen! In theory we've already 10735 * checked this command for overlap... 10736 */ 10737 break; 10738 case CTL_ACTION_PASS: 10739 case CTL_ACTION_SKIP: { 10740 const struct ctl_cmd_entry *entry; 10741 10742 /* 10743 * The skip case shouldn't happen, this transaction 10744 * should have never made it onto the blocked queue. 10745 */ 10746 /* 10747 * This I/O is no longer blocked, we can remove it 10748 * from the blocked queue. Since this is a TAILQ 10749 * (doubly linked list), we can do O(1) removals 10750 * from any place on the list. 10751 */ 10752 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10753 blocked_links); 10754 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10755 10756 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 10757 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 10758 /* 10759 * Need to send IO back to original side to 10760 * run 10761 */ 10762 union ctl_ha_msg msg_info; 10763 10764 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 10765 msg_info.hdr.original_sc = 10766 cur_blocked->io_hdr.original_sc; 10767 msg_info.hdr.serializing_sc = cur_blocked; 10768 msg_info.hdr.msg_type = CTL_MSG_R2R; 10769 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 10770 sizeof(msg_info.hdr), M_NOWAIT); 10771 break; 10772 } 10773 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 10774 10775 /* 10776 * Check this I/O for LUN state changes that may 10777 * have happened while this command was blocked. 10778 * The LUN state may have been changed by a command 10779 * ahead of us in the queue, so we need to re-check 10780 * for any states that can be caused by SCSI 10781 * commands. 10782 */ 10783 if (ctl_scsiio_lun_check(lun, entry, 10784 &cur_blocked->scsiio) == 0) { 10785 cur_blocked->io_hdr.flags |= 10786 CTL_FLAG_IS_WAS_ON_RTR; 10787 ctl_enqueue_rtr(cur_blocked); 10788 } else 10789 ctl_done(cur_blocked); 10790 break; 10791 } 10792 default: 10793 /* 10794 * This probably shouldn't happen -- we shouldn't 10795 * get CTL_ACTION_ERROR, or anything else. 10796 */ 10797 break; 10798 } 10799 } 10800 10801 return (CTL_RETVAL_COMPLETE); 10802} 10803 10804/* 10805 * This routine (with one exception) checks LUN flags that can be set by 10806 * commands ahead of us in the OOA queue. These flags have to be checked 10807 * when a command initially comes in, and when we pull a command off the 10808 * blocked queue and are preparing to execute it. The reason we have to 10809 * check these flags for commands on the blocked queue is that the LUN 10810 * state may have been changed by a command ahead of us while we're on the 10811 * blocked queue. 10812 * 10813 * Ordering is somewhat important with these checks, so please pay 10814 * careful attention to the placement of any new checks. 10815 */ 10816static int 10817ctl_scsiio_lun_check(struct ctl_lun *lun, 10818 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10819{ 10820 struct ctl_softc *softc = lun->ctl_softc; 10821 int retval; 10822 uint32_t residx; 10823 10824 retval = 0; 10825 10826 mtx_assert(&lun->lun_lock, MA_OWNED); 10827 10828 /* 10829 * If this shelf is a secondary shelf controller, we may have to 10830 * reject some commands disallowed by HA mode and link state. 10831 */ 10832 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 10833 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 10834 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 10835 ctl_set_lun_unavail(ctsio); 10836 retval = 1; 10837 goto bailout; 10838 } 10839 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 10840 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 10841 ctl_set_lun_transit(ctsio); 10842 retval = 1; 10843 goto bailout; 10844 } 10845 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 10846 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 10847 ctl_set_lun_standby(ctsio); 10848 retval = 1; 10849 goto bailout; 10850 } 10851 10852 /* The rest of checks are only done on executing side */ 10853 if (softc->ha_mode == CTL_HA_MODE_XFER) 10854 goto bailout; 10855 } 10856 10857 if (entry->pattern & CTL_LUN_PAT_WRITE) { 10858 if (lun->be_lun && 10859 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 10860 ctl_set_hw_write_protected(ctsio); 10861 retval = 1; 10862 goto bailout; 10863 } 10864 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 10865 .eca_and_aen & SCP_SWP) != 0) { 10866 ctl_set_sense(ctsio, /*current_error*/ 1, 10867 /*sense_key*/ SSD_KEY_DATA_PROTECT, 10868 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 10869 retval = 1; 10870 goto bailout; 10871 } 10872 } 10873 10874 /* 10875 * Check for a reservation conflict. If this command isn't allowed 10876 * even on reserved LUNs, and if this initiator isn't the one who 10877 * reserved us, reject the command with a reservation conflict. 10878 */ 10879 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 10880 if ((lun->flags & CTL_LUN_RESERVED) 10881 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10882 if (lun->res_idx != residx) { 10883 ctl_set_reservation_conflict(ctsio); 10884 retval = 1; 10885 goto bailout; 10886 } 10887 } 10888 10889 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 10890 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 10891 /* No reservation or command is allowed. */; 10892 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 10893 (lun->res_type == SPR_TYPE_WR_EX || 10894 lun->res_type == SPR_TYPE_WR_EX_RO || 10895 lun->res_type == SPR_TYPE_WR_EX_AR)) { 10896 /* The command is allowed for Write Exclusive resv. */; 10897 } else { 10898 /* 10899 * if we aren't registered or it's a res holder type 10900 * reservation and this isn't the res holder then set a 10901 * conflict. 10902 */ 10903 if (ctl_get_prkey(lun, residx) == 0 10904 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10905 ctl_set_reservation_conflict(ctsio); 10906 retval = 1; 10907 goto bailout; 10908 } 10909 } 10910 10911 if ((lun->flags & CTL_LUN_OFFLINE) 10912 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) { 10913 ctl_set_lun_not_ready(ctsio); 10914 retval = 1; 10915 goto bailout; 10916 } 10917 10918 if ((lun->flags & CTL_LUN_STOPPED) 10919 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10920 /* "Logical unit not ready, initializing cmd. required" */ 10921 ctl_set_lun_stopped(ctsio); 10922 retval = 1; 10923 goto bailout; 10924 } 10925 10926 if ((lun->flags & CTL_LUN_INOPERABLE) 10927 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10928 /* "Medium format corrupted" */ 10929 ctl_set_medium_format_corrupted(ctsio); 10930 retval = 1; 10931 goto bailout; 10932 } 10933 10934bailout: 10935 return (retval); 10936} 10937 10938static void 10939ctl_failover_io(union ctl_io *io, int have_lock) 10940{ 10941 ctl_set_busy(&io->scsiio); 10942 ctl_done(io); 10943} 10944 10945static void 10946ctl_failover_lun(struct ctl_lun *lun) 10947{ 10948 struct ctl_softc *softc = lun->ctl_softc; 10949 struct ctl_io_hdr *io, *next_io; 10950 10951 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun)); 10952 if (softc->ha_mode == CTL_HA_MODE_XFER) { 10953 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 10954 /* We are master */ 10955 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 10956 if (io->flags & CTL_FLAG_IO_ACTIVE) { 10957 io->flags |= CTL_FLAG_ABORT; 10958 io->flags |= CTL_FLAG_FAILOVER; 10959 } else { /* This can be only due to DATAMOVE */ 10960 io->msg_type = CTL_MSG_DATAMOVE_DONE; 10961 io->flags |= CTL_FLAG_IO_ACTIVE; 10962 io->port_status = 31340; 10963 ctl_enqueue_isc((union ctl_io *)io); 10964 } 10965 } 10966 /* We are slave */ 10967 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 10968 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10969 if (io->flags & CTL_FLAG_IO_ACTIVE) { 10970 io->flags |= CTL_FLAG_FAILOVER; 10971 } else { 10972 ctl_set_busy(&((union ctl_io *)io)-> 10973 scsiio); 10974 ctl_done((union ctl_io *)io); 10975 } 10976 } 10977 } 10978 } else { /* SERIALIZE modes */ 10979 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 10980 next_io) { 10981 /* We are master */ 10982 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 10983 TAILQ_REMOVE(&lun->blocked_queue, io, 10984 blocked_links); 10985 io->flags &= ~CTL_FLAG_BLOCKED; 10986 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 10987 ctl_free_io((union ctl_io *)io); 10988 } 10989 } 10990 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 10991 /* We are master */ 10992 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 10993 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 10994 ctl_free_io((union ctl_io *)io); 10995 } 10996 /* We are slave */ 10997 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 10998 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10999 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11000 ctl_set_busy(&((union ctl_io *)io)-> 11001 scsiio); 11002 ctl_done((union ctl_io *)io); 11003 } 11004 } 11005 } 11006 ctl_check_blocked(lun); 11007 } 11008} 11009 11010static int 11011ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11012{ 11013 struct ctl_lun *lun; 11014 const struct ctl_cmd_entry *entry; 11015 uint32_t initidx, targ_lun; 11016 int retval; 11017 11018 retval = 0; 11019 11020 lun = NULL; 11021 11022 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11023 if ((targ_lun < CTL_MAX_LUNS) 11024 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11025 /* 11026 * If the LUN is invalid, pretend that it doesn't exist. 11027 * It will go away as soon as all pending I/O has been 11028 * completed. 11029 */ 11030 mtx_lock(&lun->lun_lock); 11031 if (lun->flags & CTL_LUN_DISABLED) { 11032 mtx_unlock(&lun->lun_lock); 11033 lun = NULL; 11034 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11035 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11036 } else { 11037 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11038 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11039 lun->be_lun; 11040 11041 /* 11042 * Every I/O goes into the OOA queue for a 11043 * particular LUN, and stays there until completion. 11044 */ 11045#ifdef CTL_TIME_IO 11046 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11047 lun->idle_time += getsbinuptime() - 11048 lun->last_busy; 11049 } 11050#endif 11051 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11052 ooa_links); 11053 } 11054 } else { 11055 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11056 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11057 } 11058 11059 /* Get command entry and return error if it is unsuppotyed. */ 11060 entry = ctl_validate_command(ctsio); 11061 if (entry == NULL) { 11062 if (lun) 11063 mtx_unlock(&lun->lun_lock); 11064 return (retval); 11065 } 11066 11067 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11068 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11069 11070 /* 11071 * Check to see whether we can send this command to LUNs that don't 11072 * exist. This should pretty much only be the case for inquiry 11073 * and request sense. Further checks, below, really require having 11074 * a LUN, so we can't really check the command anymore. Just put 11075 * it on the rtr queue. 11076 */ 11077 if (lun == NULL) { 11078 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11079 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11080 ctl_enqueue_rtr((union ctl_io *)ctsio); 11081 return (retval); 11082 } 11083 11084 ctl_set_unsupported_lun(ctsio); 11085 ctl_done((union ctl_io *)ctsio); 11086 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11087 return (retval); 11088 } else { 11089 /* 11090 * Make sure we support this particular command on this LUN. 11091 * e.g., we don't support writes to the control LUN. 11092 */ 11093 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11094 mtx_unlock(&lun->lun_lock); 11095 ctl_set_invalid_opcode(ctsio); 11096 ctl_done((union ctl_io *)ctsio); 11097 return (retval); 11098 } 11099 } 11100 11101 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11102 11103#ifdef CTL_WITH_CA 11104 /* 11105 * If we've got a request sense, it'll clear the contingent 11106 * allegiance condition. Otherwise, if we have a CA condition for 11107 * this initiator, clear it, because it sent down a command other 11108 * than request sense. 11109 */ 11110 if ((ctsio->cdb[0] != REQUEST_SENSE) 11111 && (ctl_is_set(lun->have_ca, initidx))) 11112 ctl_clear_mask(lun->have_ca, initidx); 11113#endif 11114 11115 /* 11116 * If the command has this flag set, it handles its own unit 11117 * attention reporting, we shouldn't do anything. Otherwise we 11118 * check for any pending unit attentions, and send them back to the 11119 * initiator. We only do this when a command initially comes in, 11120 * not when we pull it off the blocked queue. 11121 * 11122 * According to SAM-3, section 5.3.2, the order that things get 11123 * presented back to the host is basically unit attentions caused 11124 * by some sort of reset event, busy status, reservation conflicts 11125 * or task set full, and finally any other status. 11126 * 11127 * One issue here is that some of the unit attentions we report 11128 * don't fall into the "reset" category (e.g. "reported luns data 11129 * has changed"). So reporting it here, before the reservation 11130 * check, may be technically wrong. I guess the only thing to do 11131 * would be to check for and report the reset events here, and then 11132 * check for the other unit attention types after we check for a 11133 * reservation conflict. 11134 * 11135 * XXX KDM need to fix this 11136 */ 11137 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11138 ctl_ua_type ua_type; 11139 11140 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11141 SSD_TYPE_NONE); 11142 if (ua_type != CTL_UA_NONE) { 11143 mtx_unlock(&lun->lun_lock); 11144 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11145 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11146 ctsio->sense_len = SSD_FULL_SIZE; 11147 ctl_done((union ctl_io *)ctsio); 11148 return (retval); 11149 } 11150 } 11151 11152 11153 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11154 mtx_unlock(&lun->lun_lock); 11155 ctl_done((union ctl_io *)ctsio); 11156 return (retval); 11157 } 11158 11159 /* 11160 * XXX CHD this is where we want to send IO to other side if 11161 * this LUN is secondary on this SC. We will need to make a copy 11162 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11163 * the copy we send as FROM_OTHER. 11164 * We also need to stuff the address of the original IO so we can 11165 * find it easily. Something similar will need be done on the other 11166 * side so when we are done we can find the copy. 11167 */ 11168 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11169 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0) { 11170 union ctl_ha_msg msg_info; 11171 int isc_retval; 11172 11173 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11174 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11175 mtx_unlock(&lun->lun_lock); 11176 11177 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11178 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11179 msg_info.hdr.serializing_sc = NULL; 11180 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11181 msg_info.scsi.tag_num = ctsio->tag_num; 11182 msg_info.scsi.tag_type = ctsio->tag_type; 11183 msg_info.scsi.cdb_len = ctsio->cdb_len; 11184 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11185 11186 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11187 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11188 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11189 ctl_set_busy(ctsio); 11190 ctl_done((union ctl_io *)ctsio); 11191 return (retval); 11192 } 11193 return (retval); 11194 } 11195 11196 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11197 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11198 ctl_ooaq, ooa_links))) { 11199 case CTL_ACTION_BLOCK: 11200 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11201 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11202 blocked_links); 11203 mtx_unlock(&lun->lun_lock); 11204 return (retval); 11205 case CTL_ACTION_PASS: 11206 case CTL_ACTION_SKIP: 11207 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11208 mtx_unlock(&lun->lun_lock); 11209 ctl_enqueue_rtr((union ctl_io *)ctsio); 11210 break; 11211 case CTL_ACTION_OVERLAP: 11212 mtx_unlock(&lun->lun_lock); 11213 ctl_set_overlapped_cmd(ctsio); 11214 ctl_done((union ctl_io *)ctsio); 11215 break; 11216 case CTL_ACTION_OVERLAP_TAG: 11217 mtx_unlock(&lun->lun_lock); 11218 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11219 ctl_done((union ctl_io *)ctsio); 11220 break; 11221 case CTL_ACTION_ERROR: 11222 default: 11223 mtx_unlock(&lun->lun_lock); 11224 ctl_set_internal_failure(ctsio, 11225 /*sks_valid*/ 0, 11226 /*retry_count*/ 0); 11227 ctl_done((union ctl_io *)ctsio); 11228 break; 11229 } 11230 return (retval); 11231} 11232 11233const struct ctl_cmd_entry * 11234ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11235{ 11236 const struct ctl_cmd_entry *entry; 11237 int service_action; 11238 11239 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11240 if (sa) 11241 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11242 if (entry->flags & CTL_CMD_FLAG_SA5) { 11243 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11244 entry = &((const struct ctl_cmd_entry *) 11245 entry->execute)[service_action]; 11246 } 11247 return (entry); 11248} 11249 11250const struct ctl_cmd_entry * 11251ctl_validate_command(struct ctl_scsiio *ctsio) 11252{ 11253 const struct ctl_cmd_entry *entry; 11254 int i, sa; 11255 uint8_t diff; 11256 11257 entry = ctl_get_cmd_entry(ctsio, &sa); 11258 if (entry->execute == NULL) { 11259 if (sa) 11260 ctl_set_invalid_field(ctsio, 11261 /*sks_valid*/ 1, 11262 /*command*/ 1, 11263 /*field*/ 1, 11264 /*bit_valid*/ 1, 11265 /*bit*/ 4); 11266 else 11267 ctl_set_invalid_opcode(ctsio); 11268 ctl_done((union ctl_io *)ctsio); 11269 return (NULL); 11270 } 11271 KASSERT(entry->length > 0, 11272 ("Not defined length for command 0x%02x/0x%02x", 11273 ctsio->cdb[0], ctsio->cdb[1])); 11274 for (i = 1; i < entry->length; i++) { 11275 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11276 if (diff == 0) 11277 continue; 11278 ctl_set_invalid_field(ctsio, 11279 /*sks_valid*/ 1, 11280 /*command*/ 1, 11281 /*field*/ i, 11282 /*bit_valid*/ 1, 11283 /*bit*/ fls(diff) - 1); 11284 ctl_done((union ctl_io *)ctsio); 11285 return (NULL); 11286 } 11287 return (entry); 11288} 11289 11290static int 11291ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11292{ 11293 11294 switch (lun_type) { 11295 case T_PROCESSOR: 11296 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11297 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11298 return (0); 11299 break; 11300 case T_DIRECT: 11301 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11302 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11303 return (0); 11304 break; 11305 default: 11306 return (0); 11307 } 11308 return (1); 11309} 11310 11311static int 11312ctl_scsiio(struct ctl_scsiio *ctsio) 11313{ 11314 int retval; 11315 const struct ctl_cmd_entry *entry; 11316 11317 retval = CTL_RETVAL_COMPLETE; 11318 11319 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11320 11321 entry = ctl_get_cmd_entry(ctsio, NULL); 11322 11323 /* 11324 * If this I/O has been aborted, just send it straight to 11325 * ctl_done() without executing it. 11326 */ 11327 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11328 ctl_done((union ctl_io *)ctsio); 11329 goto bailout; 11330 } 11331 11332 /* 11333 * All the checks should have been handled by ctl_scsiio_precheck(). 11334 * We should be clear now to just execute the I/O. 11335 */ 11336 retval = entry->execute(ctsio); 11337 11338bailout: 11339 return (retval); 11340} 11341 11342/* 11343 * Since we only implement one target right now, a bus reset simply resets 11344 * our single target. 11345 */ 11346static int 11347ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11348{ 11349 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11350} 11351 11352static int 11353ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11354 ctl_ua_type ua_type) 11355{ 11356 struct ctl_port *port; 11357 struct ctl_lun *lun; 11358 int retval; 11359 11360 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11361 union ctl_ha_msg msg_info; 11362 11363 msg_info.hdr.nexus = io->io_hdr.nexus; 11364 if (ua_type==CTL_UA_TARG_RESET) 11365 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11366 else 11367 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11368 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11369 msg_info.hdr.original_sc = NULL; 11370 msg_info.hdr.serializing_sc = NULL; 11371 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11372 sizeof(msg_info.task), M_WAITOK); 11373 } 11374 retval = 0; 11375 11376 mtx_lock(&softc->ctl_lock); 11377 port = softc->ctl_ports[io->io_hdr.nexus.targ_port]; 11378 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11379 if (port != NULL && 11380 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11381 continue; 11382 retval += ctl_do_lun_reset(lun, io, ua_type); 11383 } 11384 mtx_unlock(&softc->ctl_lock); 11385 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11386 return (retval); 11387} 11388 11389/* 11390 * The LUN should always be set. The I/O is optional, and is used to 11391 * distinguish between I/Os sent by this initiator, and by other 11392 * initiators. We set unit attention for initiators other than this one. 11393 * SAM-3 is vague on this point. It does say that a unit attention should 11394 * be established for other initiators when a LUN is reset (see section 11395 * 5.7.3), but it doesn't specifically say that the unit attention should 11396 * be established for this particular initiator when a LUN is reset. Here 11397 * is the relevant text, from SAM-3 rev 8: 11398 * 11399 * 5.7.2 When a SCSI initiator port aborts its own tasks 11400 * 11401 * When a SCSI initiator port causes its own task(s) to be aborted, no 11402 * notification that the task(s) have been aborted shall be returned to 11403 * the SCSI initiator port other than the completion response for the 11404 * command or task management function action that caused the task(s) to 11405 * be aborted and notification(s) associated with related effects of the 11406 * action (e.g., a reset unit attention condition). 11407 * 11408 * XXX KDM for now, we're setting unit attention for all initiators. 11409 */ 11410static int 11411ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11412{ 11413 union ctl_io *xio; 11414#if 0 11415 uint32_t initidx; 11416#endif 11417#ifdef CTL_WITH_CA 11418 int i; 11419#endif 11420 11421 mtx_lock(&lun->lun_lock); 11422 /* 11423 * Run through the OOA queue and abort each I/O. 11424 */ 11425 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11426 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11427 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11428 } 11429 11430 /* 11431 * This version sets unit attention for every 11432 */ 11433#if 0 11434 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11435 ctl_est_ua_all(lun, initidx, ua_type); 11436#else 11437 ctl_est_ua_all(lun, -1, ua_type); 11438#endif 11439 11440 /* 11441 * A reset (any kind, really) clears reservations established with 11442 * RESERVE/RELEASE. It does not clear reservations established 11443 * with PERSISTENT RESERVE OUT, but we don't support that at the 11444 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11445 * reservations made with the RESERVE/RELEASE commands, because 11446 * those commands are obsolete in SPC-3. 11447 */ 11448 lun->flags &= ~CTL_LUN_RESERVED; 11449 11450#ifdef CTL_WITH_CA 11451 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11452 ctl_clear_mask(lun->have_ca, i); 11453#endif 11454 mtx_unlock(&lun->lun_lock); 11455 11456 return (0); 11457} 11458 11459static int 11460ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11461{ 11462 struct ctl_lun *lun; 11463 uint32_t targ_lun; 11464 int retval; 11465 11466 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11467 mtx_lock(&softc->ctl_lock); 11468 if ((targ_lun >= CTL_MAX_LUNS) || 11469 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11470 mtx_unlock(&softc->ctl_lock); 11471 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11472 return (1); 11473 } 11474 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11475 mtx_unlock(&softc->ctl_lock); 11476 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11477 11478 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11479 union ctl_ha_msg msg_info; 11480 11481 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11482 msg_info.hdr.nexus = io->io_hdr.nexus; 11483 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11484 msg_info.hdr.original_sc = NULL; 11485 msg_info.hdr.serializing_sc = NULL; 11486 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11487 sizeof(msg_info.task), M_WAITOK); 11488 } 11489 return (retval); 11490} 11491 11492static void 11493ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11494 int other_sc) 11495{ 11496 union ctl_io *xio; 11497 11498 mtx_assert(&lun->lun_lock, MA_OWNED); 11499 11500 /* 11501 * Run through the OOA queue and attempt to find the given I/O. 11502 * The target port, initiator ID, tag type and tag number have to 11503 * match the values that we got from the initiator. If we have an 11504 * untagged command to abort, simply abort the first untagged command 11505 * we come to. We only allow one untagged command at a time of course. 11506 */ 11507 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11508 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11509 11510 if ((targ_port == UINT32_MAX || 11511 targ_port == xio->io_hdr.nexus.targ_port) && 11512 (init_id == UINT32_MAX || 11513 init_id == xio->io_hdr.nexus.initid)) { 11514 if (targ_port != xio->io_hdr.nexus.targ_port || 11515 init_id != xio->io_hdr.nexus.initid) 11516 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11517 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11518 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11519 union ctl_ha_msg msg_info; 11520 11521 msg_info.hdr.nexus = xio->io_hdr.nexus; 11522 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11523 msg_info.task.tag_num = xio->scsiio.tag_num; 11524 msg_info.task.tag_type = xio->scsiio.tag_type; 11525 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11526 msg_info.hdr.original_sc = NULL; 11527 msg_info.hdr.serializing_sc = NULL; 11528 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11529 sizeof(msg_info.task), M_NOWAIT); 11530 } 11531 } 11532 } 11533} 11534 11535static int 11536ctl_abort_task_set(union ctl_io *io) 11537{ 11538 struct ctl_softc *softc = control_softc; 11539 struct ctl_lun *lun; 11540 uint32_t targ_lun; 11541 11542 /* 11543 * Look up the LUN. 11544 */ 11545 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11546 mtx_lock(&softc->ctl_lock); 11547 if ((targ_lun >= CTL_MAX_LUNS) || 11548 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11549 mtx_unlock(&softc->ctl_lock); 11550 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11551 return (1); 11552 } 11553 11554 mtx_lock(&lun->lun_lock); 11555 mtx_unlock(&softc->ctl_lock); 11556 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11557 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11558 io->io_hdr.nexus.initid, 11559 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11560 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11561 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11562 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11563 } 11564 mtx_unlock(&lun->lun_lock); 11565 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11566 return (0); 11567} 11568 11569static int 11570ctl_i_t_nexus_reset(union ctl_io *io) 11571{ 11572 struct ctl_softc *softc = control_softc; 11573 struct ctl_lun *lun; 11574 uint32_t initidx; 11575 11576 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11577 union ctl_ha_msg msg_info; 11578 11579 msg_info.hdr.nexus = io->io_hdr.nexus; 11580 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11581 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11582 msg_info.hdr.original_sc = NULL; 11583 msg_info.hdr.serializing_sc = NULL; 11584 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11585 sizeof(msg_info.task), M_WAITOK); 11586 } 11587 11588 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11589 mtx_lock(&softc->ctl_lock); 11590 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11591 mtx_lock(&lun->lun_lock); 11592 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11593 io->io_hdr.nexus.initid, 1); 11594#ifdef CTL_WITH_CA 11595 ctl_clear_mask(lun->have_ca, initidx); 11596#endif 11597 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11598 lun->flags &= ~CTL_LUN_RESERVED; 11599 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 11600 mtx_unlock(&lun->lun_lock); 11601 } 11602 mtx_unlock(&softc->ctl_lock); 11603 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11604 return (0); 11605} 11606 11607static int 11608ctl_abort_task(union ctl_io *io) 11609{ 11610 union ctl_io *xio; 11611 struct ctl_lun *lun; 11612 struct ctl_softc *softc; 11613#if 0 11614 struct sbuf sb; 11615 char printbuf[128]; 11616#endif 11617 int found; 11618 uint32_t targ_lun; 11619 11620 softc = control_softc; 11621 found = 0; 11622 11623 /* 11624 * Look up the LUN. 11625 */ 11626 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11627 mtx_lock(&softc->ctl_lock); 11628 if ((targ_lun >= CTL_MAX_LUNS) || 11629 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11630 mtx_unlock(&softc->ctl_lock); 11631 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11632 return (1); 11633 } 11634 11635#if 0 11636 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11637 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11638#endif 11639 11640 mtx_lock(&lun->lun_lock); 11641 mtx_unlock(&softc->ctl_lock); 11642 /* 11643 * Run through the OOA queue and attempt to find the given I/O. 11644 * The target port, initiator ID, tag type and tag number have to 11645 * match the values that we got from the initiator. If we have an 11646 * untagged command to abort, simply abort the first untagged command 11647 * we come to. We only allow one untagged command at a time of course. 11648 */ 11649 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11650 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11651#if 0 11652 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11653 11654 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11655 lun->lun, xio->scsiio.tag_num, 11656 xio->scsiio.tag_type, 11657 (xio->io_hdr.blocked_links.tqe_prev 11658 == NULL) ? "" : " BLOCKED", 11659 (xio->io_hdr.flags & 11660 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11661 (xio->io_hdr.flags & 11662 CTL_FLAG_ABORT) ? " ABORT" : "", 11663 (xio->io_hdr.flags & 11664 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11665 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11666 sbuf_finish(&sb); 11667 printf("%s\n", sbuf_data(&sb)); 11668#endif 11669 11670 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11671 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11672 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11673 continue; 11674 11675 /* 11676 * If the abort says that the task is untagged, the 11677 * task in the queue must be untagged. Otherwise, 11678 * we just check to see whether the tag numbers 11679 * match. This is because the QLogic firmware 11680 * doesn't pass back the tag type in an abort 11681 * request. 11682 */ 11683#if 0 11684 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11685 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11686 || (xio->scsiio.tag_num == io->taskio.tag_num)) 11687#endif 11688 /* 11689 * XXX KDM we've got problems with FC, because it 11690 * doesn't send down a tag type with aborts. So we 11691 * can only really go by the tag number... 11692 * This may cause problems with parallel SCSI. 11693 * Need to figure that out!! 11694 */ 11695 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11696 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11697 found = 1; 11698 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 11699 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11700 union ctl_ha_msg msg_info; 11701 11702 msg_info.hdr.nexus = io->io_hdr.nexus; 11703 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11704 msg_info.task.tag_num = io->taskio.tag_num; 11705 msg_info.task.tag_type = io->taskio.tag_type; 11706 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11707 msg_info.hdr.original_sc = NULL; 11708 msg_info.hdr.serializing_sc = NULL; 11709#if 0 11710 printf("Sent Abort to other side\n"); 11711#endif 11712 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11713 sizeof(msg_info.task), M_NOWAIT); 11714 } 11715#if 0 11716 printf("ctl_abort_task: found I/O to abort\n"); 11717#endif 11718 } 11719 } 11720 mtx_unlock(&lun->lun_lock); 11721 11722 if (found == 0) { 11723 /* 11724 * This isn't really an error. It's entirely possible for 11725 * the abort and command completion to cross on the wire. 11726 * This is more of an informative/diagnostic error. 11727 */ 11728#if 0 11729 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11730 "%u:%u:%u tag %d type %d\n", 11731 io->io_hdr.nexus.initid, 11732 io->io_hdr.nexus.targ_port, 11733 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11734 io->taskio.tag_type); 11735#endif 11736 } 11737 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11738 return (0); 11739} 11740 11741static int 11742ctl_query_task(union ctl_io *io, int task_set) 11743{ 11744 union ctl_io *xio; 11745 struct ctl_lun *lun; 11746 struct ctl_softc *softc; 11747 int found = 0; 11748 uint32_t targ_lun; 11749 11750 softc = control_softc; 11751 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11752 mtx_lock(&softc->ctl_lock); 11753 if ((targ_lun >= CTL_MAX_LUNS) || 11754 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11755 mtx_unlock(&softc->ctl_lock); 11756 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11757 return (1); 11758 } 11759 mtx_lock(&lun->lun_lock); 11760 mtx_unlock(&softc->ctl_lock); 11761 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11762 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11763 11764 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11765 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11766 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11767 continue; 11768 11769 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 11770 found = 1; 11771 break; 11772 } 11773 } 11774 mtx_unlock(&lun->lun_lock); 11775 if (found) 11776 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 11777 else 11778 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11779 return (0); 11780} 11781 11782static int 11783ctl_query_async_event(union ctl_io *io) 11784{ 11785 struct ctl_lun *lun; 11786 struct ctl_softc *softc; 11787 ctl_ua_type ua; 11788 uint32_t targ_lun, initidx; 11789 11790 softc = control_softc; 11791 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11792 mtx_lock(&softc->ctl_lock); 11793 if ((targ_lun >= CTL_MAX_LUNS) || 11794 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11795 mtx_unlock(&softc->ctl_lock); 11796 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11797 return (1); 11798 } 11799 mtx_lock(&lun->lun_lock); 11800 mtx_unlock(&softc->ctl_lock); 11801 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11802 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 11803 mtx_unlock(&lun->lun_lock); 11804 if (ua != CTL_UA_NONE) 11805 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 11806 else 11807 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11808 return (0); 11809} 11810 11811static void 11812ctl_run_task(union ctl_io *io) 11813{ 11814 struct ctl_softc *softc = control_softc; 11815 int retval = 1; 11816 11817 CTL_DEBUG_PRINT(("ctl_run_task\n")); 11818 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 11819 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 11820 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 11821 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 11822 switch (io->taskio.task_action) { 11823 case CTL_TASK_ABORT_TASK: 11824 retval = ctl_abort_task(io); 11825 break; 11826 case CTL_TASK_ABORT_TASK_SET: 11827 case CTL_TASK_CLEAR_TASK_SET: 11828 retval = ctl_abort_task_set(io); 11829 break; 11830 case CTL_TASK_CLEAR_ACA: 11831 break; 11832 case CTL_TASK_I_T_NEXUS_RESET: 11833 retval = ctl_i_t_nexus_reset(io); 11834 break; 11835 case CTL_TASK_LUN_RESET: 11836 retval = ctl_lun_reset(softc, io); 11837 break; 11838 case CTL_TASK_TARGET_RESET: 11839 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 11840 break; 11841 case CTL_TASK_BUS_RESET: 11842 retval = ctl_bus_reset(softc, io); 11843 break; 11844 case CTL_TASK_PORT_LOGIN: 11845 break; 11846 case CTL_TASK_PORT_LOGOUT: 11847 break; 11848 case CTL_TASK_QUERY_TASK: 11849 retval = ctl_query_task(io, 0); 11850 break; 11851 case CTL_TASK_QUERY_TASK_SET: 11852 retval = ctl_query_task(io, 1); 11853 break; 11854 case CTL_TASK_QUERY_ASYNC_EVENT: 11855 retval = ctl_query_async_event(io); 11856 break; 11857 default: 11858 printf("%s: got unknown task management event %d\n", 11859 __func__, io->taskio.task_action); 11860 break; 11861 } 11862 if (retval == 0) 11863 io->io_hdr.status = CTL_SUCCESS; 11864 else 11865 io->io_hdr.status = CTL_ERROR; 11866 ctl_done(io); 11867} 11868 11869/* 11870 * For HA operation. Handle commands that come in from the other 11871 * controller. 11872 */ 11873static void 11874ctl_handle_isc(union ctl_io *io) 11875{ 11876 int free_io; 11877 struct ctl_lun *lun; 11878 struct ctl_softc *softc; 11879 uint32_t targ_lun; 11880 11881 softc = control_softc; 11882 11883 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11884 lun = softc->ctl_luns[targ_lun]; 11885 11886 switch (io->io_hdr.msg_type) { 11887 case CTL_MSG_SERIALIZE: 11888 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 11889 break; 11890 case CTL_MSG_R2R: { 11891 const struct ctl_cmd_entry *entry; 11892 11893 /* 11894 * This is only used in SER_ONLY mode. 11895 */ 11896 free_io = 0; 11897 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11898 mtx_lock(&lun->lun_lock); 11899 if (ctl_scsiio_lun_check(lun, 11900 entry, (struct ctl_scsiio *)io) != 0) { 11901 mtx_unlock(&lun->lun_lock); 11902 ctl_done(io); 11903 break; 11904 } 11905 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11906 mtx_unlock(&lun->lun_lock); 11907 ctl_enqueue_rtr(io); 11908 break; 11909 } 11910 case CTL_MSG_FINISH_IO: 11911 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11912 free_io = 0; 11913 ctl_done(io); 11914 } else { 11915 free_io = 1; 11916 mtx_lock(&lun->lun_lock); 11917 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11918 ooa_links); 11919 ctl_check_blocked(lun); 11920 mtx_unlock(&lun->lun_lock); 11921 } 11922 break; 11923 case CTL_MSG_PERS_ACTION: 11924 ctl_hndl_per_res_out_on_other_sc( 11925 (union ctl_ha_msg *)&io->presio.pr_msg); 11926 free_io = 1; 11927 break; 11928 case CTL_MSG_BAD_JUJU: 11929 free_io = 0; 11930 ctl_done(io); 11931 break; 11932 case CTL_MSG_DATAMOVE: 11933 /* Only used in XFER mode */ 11934 free_io = 0; 11935 ctl_datamove_remote(io); 11936 break; 11937 case CTL_MSG_DATAMOVE_DONE: 11938 /* Only used in XFER mode */ 11939 free_io = 0; 11940 io->scsiio.be_move_done(io); 11941 break; 11942 case CTL_MSG_FAILOVER: 11943 mtx_lock(&lun->lun_lock); 11944 ctl_failover_lun(lun); 11945 mtx_unlock(&lun->lun_lock); 11946 free_io = 1; 11947 break; 11948 default: 11949 free_io = 1; 11950 printf("%s: Invalid message type %d\n", 11951 __func__, io->io_hdr.msg_type); 11952 break; 11953 } 11954 if (free_io) 11955 ctl_free_io(io); 11956 11957} 11958 11959 11960/* 11961 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11962 * there is no match. 11963 */ 11964static ctl_lun_error_pattern 11965ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11966{ 11967 const struct ctl_cmd_entry *entry; 11968 ctl_lun_error_pattern filtered_pattern, pattern; 11969 11970 pattern = desc->error_pattern; 11971 11972 /* 11973 * XXX KDM we need more data passed into this function to match a 11974 * custom pattern, and we actually need to implement custom pattern 11975 * matching. 11976 */ 11977 if (pattern & CTL_LUN_PAT_CMD) 11978 return (CTL_LUN_PAT_CMD); 11979 11980 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 11981 return (CTL_LUN_PAT_ANY); 11982 11983 entry = ctl_get_cmd_entry(ctsio, NULL); 11984 11985 filtered_pattern = entry->pattern & pattern; 11986 11987 /* 11988 * If the user requested specific flags in the pattern (e.g. 11989 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 11990 * flags. 11991 * 11992 * If the user did not specify any flags, it doesn't matter whether 11993 * or not the command supports the flags. 11994 */ 11995 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 11996 (pattern & ~CTL_LUN_PAT_MASK)) 11997 return (CTL_LUN_PAT_NONE); 11998 11999 /* 12000 * If the user asked for a range check, see if the requested LBA 12001 * range overlaps with this command's LBA range. 12002 */ 12003 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12004 uint64_t lba1; 12005 uint64_t len1; 12006 ctl_action action; 12007 int retval; 12008 12009 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12010 if (retval != 0) 12011 return (CTL_LUN_PAT_NONE); 12012 12013 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12014 desc->lba_range.len, FALSE); 12015 /* 12016 * A "pass" means that the LBA ranges don't overlap, so 12017 * this doesn't match the user's range criteria. 12018 */ 12019 if (action == CTL_ACTION_PASS) 12020 return (CTL_LUN_PAT_NONE); 12021 } 12022 12023 return (filtered_pattern); 12024} 12025 12026static void 12027ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12028{ 12029 struct ctl_error_desc *desc, *desc2; 12030 12031 mtx_assert(&lun->lun_lock, MA_OWNED); 12032 12033 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12034 ctl_lun_error_pattern pattern; 12035 /* 12036 * Check to see whether this particular command matches 12037 * the pattern in the descriptor. 12038 */ 12039 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12040 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12041 continue; 12042 12043 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12044 case CTL_LUN_INJ_ABORTED: 12045 ctl_set_aborted(&io->scsiio); 12046 break; 12047 case CTL_LUN_INJ_MEDIUM_ERR: 12048 ctl_set_medium_error(&io->scsiio, 12049 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12050 CTL_FLAG_DATA_OUT); 12051 break; 12052 case CTL_LUN_INJ_UA: 12053 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12054 * OCCURRED */ 12055 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12056 break; 12057 case CTL_LUN_INJ_CUSTOM: 12058 /* 12059 * We're assuming the user knows what he is doing. 12060 * Just copy the sense information without doing 12061 * checks. 12062 */ 12063 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12064 MIN(sizeof(desc->custom_sense), 12065 sizeof(io->scsiio.sense_data))); 12066 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12067 io->scsiio.sense_len = SSD_FULL_SIZE; 12068 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12069 break; 12070 case CTL_LUN_INJ_NONE: 12071 default: 12072 /* 12073 * If this is an error injection type we don't know 12074 * about, clear the continuous flag (if it is set) 12075 * so it will get deleted below. 12076 */ 12077 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12078 break; 12079 } 12080 /* 12081 * By default, each error injection action is a one-shot 12082 */ 12083 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12084 continue; 12085 12086 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12087 12088 free(desc, M_CTL); 12089 } 12090} 12091 12092#ifdef CTL_IO_DELAY 12093static void 12094ctl_datamove_timer_wakeup(void *arg) 12095{ 12096 union ctl_io *io; 12097 12098 io = (union ctl_io *)arg; 12099 12100 ctl_datamove(io); 12101} 12102#endif /* CTL_IO_DELAY */ 12103 12104void 12105ctl_datamove(union ctl_io *io) 12106{ 12107 struct ctl_lun *lun; 12108 void (*fe_datamove)(union ctl_io *io); 12109 12110 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12111 12112 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12113 12114 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12115#ifdef CTL_TIME_IO 12116 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12117 char str[256]; 12118 char path_str[64]; 12119 struct sbuf sb; 12120 12121 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12122 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12123 12124 sbuf_cat(&sb, path_str); 12125 switch (io->io_hdr.io_type) { 12126 case CTL_IO_SCSI: 12127 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12128 sbuf_printf(&sb, "\n"); 12129 sbuf_cat(&sb, path_str); 12130 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12131 io->scsiio.tag_num, io->scsiio.tag_type); 12132 break; 12133 case CTL_IO_TASK: 12134 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12135 "Tag Type: %d\n", io->taskio.task_action, 12136 io->taskio.tag_num, io->taskio.tag_type); 12137 break; 12138 default: 12139 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12140 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12141 break; 12142 } 12143 sbuf_cat(&sb, path_str); 12144 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12145 (intmax_t)time_uptime - io->io_hdr.start_time); 12146 sbuf_finish(&sb); 12147 printf("%s", sbuf_data(&sb)); 12148 } 12149#endif /* CTL_TIME_IO */ 12150 12151#ifdef CTL_IO_DELAY 12152 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12153 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12154 } else { 12155 if ((lun != NULL) 12156 && (lun->delay_info.datamove_delay > 0)) { 12157 12158 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12159 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12160 callout_reset(&io->io_hdr.delay_callout, 12161 lun->delay_info.datamove_delay * hz, 12162 ctl_datamove_timer_wakeup, io); 12163 if (lun->delay_info.datamove_type == 12164 CTL_DELAY_TYPE_ONESHOT) 12165 lun->delay_info.datamove_delay = 0; 12166 return; 12167 } 12168 } 12169#endif 12170 12171 /* 12172 * This command has been aborted. Set the port status, so we fail 12173 * the data move. 12174 */ 12175 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12176 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12177 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12178 io->io_hdr.nexus.targ_port, 12179 io->io_hdr.nexus.targ_lun); 12180 io->io_hdr.port_status = 31337; 12181 /* 12182 * Note that the backend, in this case, will get the 12183 * callback in its context. In other cases it may get 12184 * called in the frontend's interrupt thread context. 12185 */ 12186 io->scsiio.be_move_done(io); 12187 return; 12188 } 12189 12190 /* Don't confuse frontend with zero length data move. */ 12191 if (io->scsiio.kern_data_len == 0) { 12192 io->scsiio.be_move_done(io); 12193 return; 12194 } 12195 12196 /* 12197 * If we're in XFER mode and this I/O is from the other shelf 12198 * controller, we need to send the DMA to the other side to 12199 * actually transfer the data to/from the host. In serialize only 12200 * mode the transfer happens below CTL and ctl_datamove() is only 12201 * called on the machine that originally received the I/O. 12202 */ 12203 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12204 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12205 union ctl_ha_msg msg; 12206 uint32_t sg_entries_sent; 12207 int do_sg_copy; 12208 int i; 12209 12210 memset(&msg, 0, sizeof(msg)); 12211 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12212 msg.hdr.original_sc = io->io_hdr.original_sc; 12213 msg.hdr.serializing_sc = io; 12214 msg.hdr.nexus = io->io_hdr.nexus; 12215 msg.hdr.status = io->io_hdr.status; 12216 msg.dt.flags = io->io_hdr.flags; 12217 /* 12218 * We convert everything into a S/G list here. We can't 12219 * pass by reference, only by value between controllers. 12220 * So we can't pass a pointer to the S/G list, only as many 12221 * S/G entries as we can fit in here. If it's possible for 12222 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12223 * then we need to break this up into multiple transfers. 12224 */ 12225 if (io->scsiio.kern_sg_entries == 0) { 12226 msg.dt.kern_sg_entries = 1; 12227#if 0 12228 /* 12229 * Convert to a physical address if this is a 12230 * virtual address. 12231 */ 12232 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12233 msg.dt.sg_list[0].addr = 12234 io->scsiio.kern_data_ptr; 12235 } else { 12236 /* 12237 * XXX KDM use busdma here! 12238 */ 12239 msg.dt.sg_list[0].addr = (void *) 12240 vtophys(io->scsiio.kern_data_ptr); 12241 } 12242#else 12243 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12244 ("HA does not support BUS_ADDR")); 12245 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 12246#endif 12247 12248 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12249 do_sg_copy = 0; 12250 } else { 12251 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12252 do_sg_copy = 1; 12253 } 12254 12255 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12256 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12257 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12258 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12259 msg.dt.sg_sequence = 0; 12260 12261 /* 12262 * Loop until we've sent all of the S/G entries. On the 12263 * other end, we'll recompose these S/G entries into one 12264 * contiguous list before passing it to the 12265 */ 12266 for (sg_entries_sent = 0; sg_entries_sent < 12267 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12268 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12269 sizeof(msg.dt.sg_list[0])), 12270 msg.dt.kern_sg_entries - sg_entries_sent); 12271 12272 if (do_sg_copy != 0) { 12273 struct ctl_sg_entry *sgl; 12274 int j; 12275 12276 sgl = (struct ctl_sg_entry *) 12277 io->scsiio.kern_data_ptr; 12278 /* 12279 * If this is in cached memory, flush the cache 12280 * before we send the DMA request to the other 12281 * controller. We want to do this in either 12282 * the * read or the write case. The read 12283 * case is straightforward. In the write 12284 * case, we want to make sure nothing is 12285 * in the local cache that could overwrite 12286 * the DMAed data. 12287 */ 12288 12289 for (i = sg_entries_sent, j = 0; 12290 i < msg.dt.cur_sg_entries; i++, j++) { 12291#if 0 12292 if ((io->io_hdr.flags & 12293 CTL_FLAG_BUS_ADDR) == 0) { 12294 /* 12295 * XXX KDM use busdma. 12296 */ 12297 msg.dt.sg_list[j].addr =(void *) 12298 vtophys(sgl[i].addr); 12299 } else { 12300 msg.dt.sg_list[j].addr = 12301 sgl[i].addr; 12302 } 12303#else 12304 KASSERT((io->io_hdr.flags & 12305 CTL_FLAG_BUS_ADDR) == 0, 12306 ("HA does not support BUS_ADDR")); 12307 msg.dt.sg_list[j].addr = sgl[i].addr; 12308#endif 12309 msg.dt.sg_list[j].len = sgl[i].len; 12310 } 12311 } 12312 12313 sg_entries_sent += msg.dt.cur_sg_entries; 12314 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12315 msg.dt.sg_last = 1; 12316 else 12317 msg.dt.sg_last = 0; 12318 12319 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12320 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 12321 sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries, 12322 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 12323 io->io_hdr.port_status = 31341; 12324 io->scsiio.be_move_done(io); 12325 return; 12326 } 12327 12328 msg.dt.sent_sg_entries = sg_entries_sent; 12329 } 12330 12331 /* 12332 * Officially handover the request from us to peer. 12333 * If failover has just happened, then we must return error. 12334 * If failover happen just after, then it is not our problem. 12335 */ 12336 if (lun) 12337 mtx_lock(&lun->lun_lock); 12338 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12339 if (lun) 12340 mtx_unlock(&lun->lun_lock); 12341 io->io_hdr.port_status = 31342; 12342 io->scsiio.be_move_done(io); 12343 return; 12344 } 12345 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12346 if (lun) 12347 mtx_unlock(&lun->lun_lock); 12348 } else { 12349 12350 /* 12351 * Lookup the fe_datamove() function for this particular 12352 * front end. 12353 */ 12354 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 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 12365 memset(&msg, 0, sizeof(msg)); 12366 12367 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12368 msg.hdr.original_sc = io; 12369 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12370 msg.hdr.nexus = io->io_hdr.nexus; 12371 msg.hdr.status = io->io_hdr.status; 12372 msg.scsi.tag_num = io->scsiio.tag_num; 12373 msg.scsi.tag_type = io->scsiio.tag_type; 12374 msg.scsi.scsi_status = io->scsiio.scsi_status; 12375 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12376 io->scsiio.sense_len); 12377 msg.scsi.sense_len = io->scsiio.sense_len; 12378 msg.scsi.sense_residual = io->scsiio.sense_residual; 12379 msg.scsi.fetd_status = io->io_hdr.port_status; 12380 msg.scsi.residual = io->scsiio.residual; 12381 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12382 12383 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12384 ctl_failover_io(io, /*have_lock*/ have_lock); 12385 return; 12386 } 12387 12388 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12389 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12390 msg.scsi.sense_len, M_WAITOK); 12391} 12392 12393/* 12394 * The DMA to the remote side is done, now we need to tell the other side 12395 * we're done so it can continue with its data movement. 12396 */ 12397static void 12398ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12399{ 12400 union ctl_io *io; 12401 int i; 12402 12403 io = rq->context; 12404 12405 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12406 printf("%s: ISC DMA write failed with error %d", __func__, 12407 rq->ret); 12408 ctl_set_internal_failure(&io->scsiio, 12409 /*sks_valid*/ 1, 12410 /*retry_count*/ rq->ret); 12411 } 12412 12413 ctl_dt_req_free(rq); 12414 12415 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12416 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12417 free(io->io_hdr.remote_sglist, M_CTL); 12418 io->io_hdr.remote_sglist = NULL; 12419 io->io_hdr.local_sglist = NULL; 12420 12421 /* 12422 * The data is in local and remote memory, so now we need to send 12423 * status (good or back) back to the other side. 12424 */ 12425 ctl_send_datamove_done(io, /*have_lock*/ 0); 12426} 12427 12428/* 12429 * We've moved the data from the host/controller into local memory. Now we 12430 * need to push it over to the remote controller's memory. 12431 */ 12432static int 12433ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12434{ 12435 int retval; 12436 12437 retval = 0; 12438 12439 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12440 ctl_datamove_remote_write_cb); 12441 12442 return (retval); 12443} 12444 12445static void 12446ctl_datamove_remote_write(union ctl_io *io) 12447{ 12448 int retval; 12449 void (*fe_datamove)(union ctl_io *io); 12450 12451 /* 12452 * - Get the data from the host/HBA into local memory. 12453 * - DMA memory from the local controller to the remote controller. 12454 * - Send status back to the remote controller. 12455 */ 12456 12457 retval = ctl_datamove_remote_sgl_setup(io); 12458 if (retval != 0) 12459 return; 12460 12461 /* Switch the pointer over so the FETD knows what to do */ 12462 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12463 12464 /* 12465 * Use a custom move done callback, since we need to send completion 12466 * back to the other controller, not to the backend on this side. 12467 */ 12468 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12469 12470 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12471 12472 fe_datamove(io); 12473 12474 return; 12475 12476} 12477 12478static int 12479ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12480{ 12481#if 0 12482 char str[256]; 12483 char path_str[64]; 12484 struct sbuf sb; 12485#endif 12486 int i; 12487 12488 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12489 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12490 free(io->io_hdr.remote_sglist, M_CTL); 12491 io->io_hdr.remote_sglist = NULL; 12492 io->io_hdr.local_sglist = NULL; 12493 12494#if 0 12495 scsi_path_string(io, path_str, sizeof(path_str)); 12496 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12497 sbuf_cat(&sb, path_str); 12498 scsi_command_string(&io->scsiio, NULL, &sb); 12499 sbuf_printf(&sb, "\n"); 12500 sbuf_cat(&sb, path_str); 12501 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12502 io->scsiio.tag_num, io->scsiio.tag_type); 12503 sbuf_cat(&sb, path_str); 12504 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12505 io->io_hdr.flags, io->io_hdr.status); 12506 sbuf_finish(&sb); 12507 printk("%s", sbuf_data(&sb)); 12508#endif 12509 12510 12511 /* 12512 * The read is done, now we need to send status (good or bad) back 12513 * to the other side. 12514 */ 12515 ctl_send_datamove_done(io, /*have_lock*/ 0); 12516 12517 return (0); 12518} 12519 12520static void 12521ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12522{ 12523 union ctl_io *io; 12524 void (*fe_datamove)(union ctl_io *io); 12525 12526 io = rq->context; 12527 12528 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12529 printf("%s: ISC DMA read failed with error %d\n", __func__, 12530 rq->ret); 12531 ctl_set_internal_failure(&io->scsiio, 12532 /*sks_valid*/ 1, 12533 /*retry_count*/ rq->ret); 12534 } 12535 12536 ctl_dt_req_free(rq); 12537 12538 /* Switch the pointer over so the FETD knows what to do */ 12539 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12540 12541 /* 12542 * Use a custom move done callback, since we need to send completion 12543 * back to the other controller, not to the backend on this side. 12544 */ 12545 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12546 12547 /* XXX KDM add checks like the ones in ctl_datamove? */ 12548 12549 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12550 12551 fe_datamove(io); 12552} 12553 12554static int 12555ctl_datamove_remote_sgl_setup(union ctl_io *io) 12556{ 12557 struct ctl_sg_entry *local_sglist, *remote_sglist; 12558 struct ctl_softc *softc; 12559 uint32_t len_to_go; 12560 int retval; 12561 int i; 12562 12563 retval = 0; 12564 softc = control_softc; 12565 local_sglist = io->io_hdr.local_sglist; 12566 remote_sglist = io->io_hdr.remote_sglist; 12567 len_to_go = io->scsiio.kern_data_len; 12568 12569 /* 12570 * The difficult thing here is that the size of the various 12571 * S/G segments may be different than the size from the 12572 * remote controller. That'll make it harder when DMAing 12573 * the data back to the other side. 12574 */ 12575 for (i = 0; len_to_go > 0; i++) { 12576 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12577 local_sglist[i].addr = 12578 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12579 12580 len_to_go -= local_sglist[i].len; 12581 } 12582 /* 12583 * Reset the number of S/G entries accordingly. The original 12584 * number of S/G entries is available in rem_sg_entries. 12585 */ 12586 io->scsiio.kern_sg_entries = i; 12587 12588#if 0 12589 printf("%s: kern_sg_entries = %d\n", __func__, 12590 io->scsiio.kern_sg_entries); 12591 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12592 printf("%s: sg[%d] = %p, %d\n", __func__, i, 12593 local_sglist[i].addr, local_sglist[i].len); 12594#endif 12595 12596 return (retval); 12597} 12598 12599static int 12600ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12601 ctl_ha_dt_cb callback) 12602{ 12603 struct ctl_ha_dt_req *rq; 12604 struct ctl_sg_entry *remote_sglist, *local_sglist; 12605 uint32_t local_used, remote_used, total_used; 12606 int i, j, isc_ret; 12607 12608 rq = ctl_dt_req_alloc(); 12609 12610 /* 12611 * If we failed to allocate the request, and if the DMA didn't fail 12612 * anyway, set busy status. This is just a resource allocation 12613 * failure. 12614 */ 12615 if ((rq == NULL) 12616 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12617 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12618 ctl_set_busy(&io->scsiio); 12619 12620 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12621 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12622 12623 if (rq != NULL) 12624 ctl_dt_req_free(rq); 12625 12626 /* 12627 * The data move failed. We need to return status back 12628 * to the other controller. No point in trying to DMA 12629 * data to the remote controller. 12630 */ 12631 12632 ctl_send_datamove_done(io, /*have_lock*/ 0); 12633 12634 return (1); 12635 } 12636 12637 local_sglist = io->io_hdr.local_sglist; 12638 remote_sglist = io->io_hdr.remote_sglist; 12639 local_used = 0; 12640 remote_used = 0; 12641 total_used = 0; 12642 12643 /* 12644 * Pull/push the data over the wire from/to the other controller. 12645 * This takes into account the possibility that the local and 12646 * remote sglists may not be identical in terms of the size of 12647 * the elements and the number of elements. 12648 * 12649 * One fundamental assumption here is that the length allocated for 12650 * both the local and remote sglists is identical. Otherwise, we've 12651 * essentially got a coding error of some sort. 12652 */ 12653 isc_ret = CTL_HA_STATUS_SUCCESS; 12654 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12655 uint32_t cur_len; 12656 uint8_t *tmp_ptr; 12657 12658 rq->command = command; 12659 rq->context = io; 12660 12661 /* 12662 * Both pointers should be aligned. But it is possible 12663 * that the allocation length is not. They should both 12664 * also have enough slack left over at the end, though, 12665 * to round up to the next 8 byte boundary. 12666 */ 12667 cur_len = MIN(local_sglist[i].len - local_used, 12668 remote_sglist[j].len - remote_used); 12669 rq->size = cur_len; 12670 12671 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12672 tmp_ptr += local_used; 12673 12674#if 0 12675 /* Use physical addresses when talking to ISC hardware */ 12676 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12677 /* XXX KDM use busdma */ 12678 rq->local = vtophys(tmp_ptr); 12679 } else 12680 rq->local = tmp_ptr; 12681#else 12682 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12683 ("HA does not support BUS_ADDR")); 12684 rq->local = tmp_ptr; 12685#endif 12686 12687 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12688 tmp_ptr += remote_used; 12689 rq->remote = tmp_ptr; 12690 12691 rq->callback = NULL; 12692 12693 local_used += cur_len; 12694 if (local_used >= local_sglist[i].len) { 12695 i++; 12696 local_used = 0; 12697 } 12698 12699 remote_used += cur_len; 12700 if (remote_used >= remote_sglist[j].len) { 12701 j++; 12702 remote_used = 0; 12703 } 12704 total_used += cur_len; 12705 12706 if (total_used >= io->scsiio.kern_data_len) 12707 rq->callback = callback; 12708 12709#if 0 12710 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12711 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12712 rq->local, rq->remote, rq->size); 12713#endif 12714 12715 isc_ret = ctl_dt_single(rq); 12716 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12717 break; 12718 } 12719 if (isc_ret != CTL_HA_STATUS_WAIT) { 12720 rq->ret = isc_ret; 12721 callback(rq); 12722 } 12723 12724 return (0); 12725} 12726 12727static void 12728ctl_datamove_remote_read(union ctl_io *io) 12729{ 12730 int retval; 12731 int i; 12732 12733 /* 12734 * This will send an error to the other controller in the case of a 12735 * failure. 12736 */ 12737 retval = ctl_datamove_remote_sgl_setup(io); 12738 if (retval != 0) 12739 return; 12740 12741 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12742 ctl_datamove_remote_read_cb); 12743 if (retval != 0) { 12744 /* 12745 * Make sure we free memory if there was an error.. The 12746 * ctl_datamove_remote_xfer() function will send the 12747 * datamove done message, or call the callback with an 12748 * error if there is a problem. 12749 */ 12750 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12751 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12752 free(io->io_hdr.remote_sglist, M_CTL); 12753 io->io_hdr.remote_sglist = NULL; 12754 io->io_hdr.local_sglist = NULL; 12755 } 12756 12757 return; 12758} 12759 12760/* 12761 * Process a datamove request from the other controller. This is used for 12762 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12763 * first. Once that is complete, the data gets DMAed into the remote 12764 * controller's memory. For reads, we DMA from the remote controller's 12765 * memory into our memory first, and then move it out to the FETD. 12766 */ 12767static void 12768ctl_datamove_remote(union ctl_io *io) 12769{ 12770 12771 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12772 12773 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12774 ctl_failover_io(io, /*have_lock*/ 0); 12775 return; 12776 } 12777 12778 /* 12779 * Note that we look for an aborted I/O here, but don't do some of 12780 * the other checks that ctl_datamove() normally does. 12781 * We don't need to run the datamove delay code, since that should 12782 * have been done if need be on the other controller. 12783 */ 12784 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12785 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12786 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12787 io->io_hdr.nexus.targ_port, 12788 io->io_hdr.nexus.targ_lun); 12789 io->io_hdr.port_status = 31338; 12790 ctl_send_datamove_done(io, /*have_lock*/ 0); 12791 return; 12792 } 12793 12794 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12795 ctl_datamove_remote_write(io); 12796 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12797 ctl_datamove_remote_read(io); 12798 else { 12799 io->io_hdr.port_status = 31339; 12800 ctl_send_datamove_done(io, /*have_lock*/ 0); 12801 } 12802} 12803 12804static int 12805ctl_process_done(union ctl_io *io) 12806{ 12807 struct ctl_lun *lun; 12808 struct ctl_softc *softc = control_softc; 12809 void (*fe_done)(union ctl_io *io); 12810 union ctl_ha_msg msg; 12811 uint32_t targ_port = io->io_hdr.nexus.targ_port; 12812 12813 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12814 12815 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) 12816 fe_done = softc->ctl_ports[targ_port]->fe_done; 12817 else 12818 fe_done = NULL; 12819 12820#ifdef CTL_TIME_IO 12821 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12822 char str[256]; 12823 char path_str[64]; 12824 struct sbuf sb; 12825 12826 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12827 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12828 12829 sbuf_cat(&sb, path_str); 12830 switch (io->io_hdr.io_type) { 12831 case CTL_IO_SCSI: 12832 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12833 sbuf_printf(&sb, "\n"); 12834 sbuf_cat(&sb, path_str); 12835 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12836 io->scsiio.tag_num, io->scsiio.tag_type); 12837 break; 12838 case CTL_IO_TASK: 12839 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12840 "Tag Type: %d\n", io->taskio.task_action, 12841 io->taskio.tag_num, io->taskio.tag_type); 12842 break; 12843 default: 12844 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12845 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12846 break; 12847 } 12848 sbuf_cat(&sb, path_str); 12849 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12850 (intmax_t)time_uptime - io->io_hdr.start_time); 12851 sbuf_finish(&sb); 12852 printf("%s", sbuf_data(&sb)); 12853 } 12854#endif /* CTL_TIME_IO */ 12855 12856 switch (io->io_hdr.io_type) { 12857 case CTL_IO_SCSI: 12858 break; 12859 case CTL_IO_TASK: 12860 if (ctl_debug & CTL_DEBUG_INFO) 12861 ctl_io_error_print(io, NULL); 12862 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 12863 ctl_free_io(io); 12864 else 12865 fe_done(io); 12866 return (CTL_RETVAL_COMPLETE); 12867 default: 12868 panic("ctl_process_done: invalid io type %d\n", 12869 io->io_hdr.io_type); 12870 break; /* NOTREACHED */ 12871 } 12872 12873 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12874 if (lun == NULL) { 12875 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12876 io->io_hdr.nexus.targ_mapped_lun)); 12877 goto bailout; 12878 } 12879 12880 mtx_lock(&lun->lun_lock); 12881 12882 /* 12883 * Check to see if we have any errors to inject here. We only 12884 * inject errors for commands that don't already have errors set. 12885 */ 12886 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 12887 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 12888 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 12889 ctl_inject_error(lun, io); 12890 12891 /* 12892 * XXX KDM how do we treat commands that aren't completed 12893 * successfully? 12894 * 12895 * XXX KDM should we also track I/O latency? 12896 */ 12897 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 12898 io->io_hdr.io_type == CTL_IO_SCSI) { 12899#ifdef CTL_TIME_IO 12900 struct bintime cur_bt; 12901#endif 12902 int type; 12903 12904 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12905 CTL_FLAG_DATA_IN) 12906 type = CTL_STATS_READ; 12907 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12908 CTL_FLAG_DATA_OUT) 12909 type = CTL_STATS_WRITE; 12910 else 12911 type = CTL_STATS_NO_IO; 12912 12913 lun->stats.ports[targ_port].bytes[type] += 12914 io->scsiio.kern_total_len; 12915 lun->stats.ports[targ_port].operations[type]++; 12916#ifdef CTL_TIME_IO 12917 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 12918 &io->io_hdr.dma_bt); 12919 lun->stats.ports[targ_port].num_dmas[type] += 12920 io->io_hdr.num_dmas; 12921 getbintime(&cur_bt); 12922 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 12923 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 12924#endif 12925 } 12926 12927 /* 12928 * Remove this from the OOA queue. 12929 */ 12930 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12931#ifdef CTL_TIME_IO 12932 if (TAILQ_EMPTY(&lun->ooa_queue)) 12933 lun->last_busy = getsbinuptime(); 12934#endif 12935 12936 /* 12937 * Run through the blocked queue on this LUN and see if anything 12938 * has become unblocked, now that this transaction is done. 12939 */ 12940 ctl_check_blocked(lun); 12941 12942 /* 12943 * If the LUN has been invalidated, free it if there is nothing 12944 * left on its OOA queue. 12945 */ 12946 if ((lun->flags & CTL_LUN_INVALID) 12947 && TAILQ_EMPTY(&lun->ooa_queue)) { 12948 mtx_unlock(&lun->lun_lock); 12949 mtx_lock(&softc->ctl_lock); 12950 ctl_free_lun(lun); 12951 mtx_unlock(&softc->ctl_lock); 12952 } else 12953 mtx_unlock(&lun->lun_lock); 12954 12955bailout: 12956 12957 /* 12958 * If this command has been aborted, make sure we set the status 12959 * properly. The FETD is responsible for freeing the I/O and doing 12960 * whatever it needs to do to clean up its state. 12961 */ 12962 if (io->io_hdr.flags & CTL_FLAG_ABORT) 12963 ctl_set_task_aborted(&io->scsiio); 12964 12965 /* 12966 * If enabled, print command error status. 12967 */ 12968 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 12969 (ctl_debug & CTL_DEBUG_INFO) != 0) 12970 ctl_io_error_print(io, NULL); 12971 12972 /* 12973 * Tell the FETD or the other shelf controller we're done with this 12974 * command. Note that only SCSI commands get to this point. Task 12975 * management commands are completed above. 12976 */ 12977 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 12978 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 12979 memset(&msg, 0, sizeof(msg)); 12980 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 12981 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12982 msg.hdr.nexus = io->io_hdr.nexus; 12983 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12984 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 12985 M_WAITOK); 12986 } 12987 if ((softc->ha_mode == CTL_HA_MODE_XFER) 12988 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12989 memset(&msg, 0, sizeof(msg)); 12990 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 12991 msg.hdr.original_sc = io->io_hdr.original_sc; 12992 msg.hdr.nexus = io->io_hdr.nexus; 12993 msg.hdr.status = io->io_hdr.status; 12994 msg.scsi.scsi_status = io->scsiio.scsi_status; 12995 msg.scsi.tag_num = io->scsiio.tag_num; 12996 msg.scsi.tag_type = io->scsiio.tag_type; 12997 msg.scsi.sense_len = io->scsiio.sense_len; 12998 msg.scsi.sense_residual = io->scsiio.sense_residual; 12999 msg.scsi.residual = io->scsiio.residual; 13000 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13001 io->scsiio.sense_len); 13002 13003 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13004 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 13005 msg.scsi.sense_len, M_WAITOK); 13006 ctl_free_io(io); 13007 } else 13008 fe_done(io); 13009 13010 return (CTL_RETVAL_COMPLETE); 13011} 13012 13013#ifdef CTL_WITH_CA 13014/* 13015 * Front end should call this if it doesn't do autosense. When the request 13016 * sense comes back in from the initiator, we'll dequeue this and send it. 13017 */ 13018int 13019ctl_queue_sense(union ctl_io *io) 13020{ 13021 struct ctl_lun *lun; 13022 struct ctl_port *port; 13023 struct ctl_softc *softc; 13024 uint32_t initidx, targ_lun; 13025 13026 softc = control_softc; 13027 13028 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13029 13030 /* 13031 * LUN lookup will likely move to the ctl_work_thread() once we 13032 * have our new queueing infrastructure (that doesn't put things on 13033 * a per-LUN queue initially). That is so that we can handle 13034 * things like an INQUIRY to a LUN that we don't have enabled. We 13035 * can't deal with that right now. 13036 */ 13037 mtx_lock(&softc->ctl_lock); 13038 13039 /* 13040 * If we don't have a LUN for this, just toss the sense 13041 * information. 13042 */ 13043 port = ctl_io_port(&ctsio->io_hdr); 13044 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13045 if ((targ_lun < CTL_MAX_LUNS) 13046 && (softc->ctl_luns[targ_lun] != NULL)) 13047 lun = softc->ctl_luns[targ_lun]; 13048 else 13049 goto bailout; 13050 13051 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13052 13053 mtx_lock(&lun->lun_lock); 13054 /* 13055 * Already have CA set for this LUN...toss the sense information. 13056 */ 13057 if (ctl_is_set(lun->have_ca, initidx)) { 13058 mtx_unlock(&lun->lun_lock); 13059 goto bailout; 13060 } 13061 13062 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13063 MIN(sizeof(lun->pending_sense[initidx]), 13064 sizeof(io->scsiio.sense_data))); 13065 ctl_set_mask(lun->have_ca, initidx); 13066 mtx_unlock(&lun->lun_lock); 13067 13068bailout: 13069 mtx_unlock(&softc->ctl_lock); 13070 13071 ctl_free_io(io); 13072 13073 return (CTL_RETVAL_COMPLETE); 13074} 13075#endif 13076 13077/* 13078 * Primary command inlet from frontend ports. All SCSI and task I/O 13079 * requests must go through this function. 13080 */ 13081int 13082ctl_queue(union ctl_io *io) 13083{ 13084 struct ctl_port *port; 13085 13086 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13087 13088#ifdef CTL_TIME_IO 13089 io->io_hdr.start_time = time_uptime; 13090 getbintime(&io->io_hdr.start_bt); 13091#endif /* CTL_TIME_IO */ 13092 13093 /* Map FE-specific LUN ID into global one. */ 13094 port = ctl_io_port(&io->io_hdr); 13095 io->io_hdr.nexus.targ_mapped_lun = 13096 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13097 13098 switch (io->io_hdr.io_type) { 13099 case CTL_IO_SCSI: 13100 case CTL_IO_TASK: 13101 if (ctl_debug & CTL_DEBUG_CDB) 13102 ctl_io_print(io); 13103 ctl_enqueue_incoming(io); 13104 break; 13105 default: 13106 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13107 return (EINVAL); 13108 } 13109 13110 return (CTL_RETVAL_COMPLETE); 13111} 13112 13113#ifdef CTL_IO_DELAY 13114static void 13115ctl_done_timer_wakeup(void *arg) 13116{ 13117 union ctl_io *io; 13118 13119 io = (union ctl_io *)arg; 13120 ctl_done(io); 13121} 13122#endif /* CTL_IO_DELAY */ 13123 13124void 13125ctl_done(union ctl_io *io) 13126{ 13127 13128 /* 13129 * Enable this to catch duplicate completion issues. 13130 */ 13131#if 0 13132 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13133 printf("%s: type %d msg %d cdb %x iptl: " 13134 "%u:%u:%u tag 0x%04x " 13135 "flag %#x status %x\n", 13136 __func__, 13137 io->io_hdr.io_type, 13138 io->io_hdr.msg_type, 13139 io->scsiio.cdb[0], 13140 io->io_hdr.nexus.initid, 13141 io->io_hdr.nexus.targ_port, 13142 io->io_hdr.nexus.targ_lun, 13143 (io->io_hdr.io_type == 13144 CTL_IO_TASK) ? 13145 io->taskio.tag_num : 13146 io->scsiio.tag_num, 13147 io->io_hdr.flags, 13148 io->io_hdr.status); 13149 } else 13150 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13151#endif 13152 13153 /* 13154 * This is an internal copy of an I/O, and should not go through 13155 * the normal done processing logic. 13156 */ 13157 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13158 return; 13159 13160#ifdef CTL_IO_DELAY 13161 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13162 struct ctl_lun *lun; 13163 13164 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13165 13166 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13167 } else { 13168 struct ctl_lun *lun; 13169 13170 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13171 13172 if ((lun != NULL) 13173 && (lun->delay_info.done_delay > 0)) { 13174 13175 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13176 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13177 callout_reset(&io->io_hdr.delay_callout, 13178 lun->delay_info.done_delay * hz, 13179 ctl_done_timer_wakeup, io); 13180 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13181 lun->delay_info.done_delay = 0; 13182 return; 13183 } 13184 } 13185#endif /* CTL_IO_DELAY */ 13186 13187 ctl_enqueue_done(io); 13188} 13189 13190static void 13191ctl_work_thread(void *arg) 13192{ 13193 struct ctl_thread *thr = (struct ctl_thread *)arg; 13194 struct ctl_softc *softc = thr->ctl_softc; 13195 union ctl_io *io; 13196 int retval; 13197 13198 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13199 13200 for (;;) { 13201 retval = 0; 13202 13203 /* 13204 * We handle the queues in this order: 13205 * - ISC 13206 * - done queue (to free up resources, unblock other commands) 13207 * - RtR queue 13208 * - incoming queue 13209 * 13210 * If those queues are empty, we break out of the loop and 13211 * go to sleep. 13212 */ 13213 mtx_lock(&thr->queue_lock); 13214 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13215 if (io != NULL) { 13216 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13217 mtx_unlock(&thr->queue_lock); 13218 ctl_handle_isc(io); 13219 continue; 13220 } 13221 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13222 if (io != NULL) { 13223 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13224 /* clear any blocked commands, call fe_done */ 13225 mtx_unlock(&thr->queue_lock); 13226 retval = ctl_process_done(io); 13227 continue; 13228 } 13229 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13230 if (io != NULL) { 13231 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13232 mtx_unlock(&thr->queue_lock); 13233 if (io->io_hdr.io_type == CTL_IO_TASK) 13234 ctl_run_task(io); 13235 else 13236 ctl_scsiio_precheck(softc, &io->scsiio); 13237 continue; 13238 } 13239 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13240 if (io != NULL) { 13241 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13242 mtx_unlock(&thr->queue_lock); 13243 retval = ctl_scsiio(&io->scsiio); 13244 if (retval != CTL_RETVAL_COMPLETE) 13245 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13246 continue; 13247 } 13248 13249 /* Sleep until we have something to do. */ 13250 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13251 } 13252} 13253 13254static void 13255ctl_lun_thread(void *arg) 13256{ 13257 struct ctl_softc *softc = (struct ctl_softc *)arg; 13258 struct ctl_be_lun *be_lun; 13259 int retval; 13260 13261 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13262 13263 for (;;) { 13264 retval = 0; 13265 mtx_lock(&softc->ctl_lock); 13266 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13267 if (be_lun != NULL) { 13268 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13269 mtx_unlock(&softc->ctl_lock); 13270 ctl_create_lun(be_lun); 13271 continue; 13272 } 13273 13274 /* Sleep until we have something to do. */ 13275 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13276 PDROP | PRIBIO, "-", 0); 13277 } 13278} 13279 13280static void 13281ctl_thresh_thread(void *arg) 13282{ 13283 struct ctl_softc *softc = (struct ctl_softc *)arg; 13284 struct ctl_lun *lun; 13285 struct ctl_be_lun *be_lun; 13286 struct scsi_da_rw_recovery_page *rwpage; 13287 struct ctl_logical_block_provisioning_page *page; 13288 const char *attr; 13289 union ctl_ha_msg msg; 13290 uint64_t thres, val; 13291 int i, e, set; 13292 13293 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13294 13295 for (;;) { 13296 mtx_lock(&softc->ctl_lock); 13297 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13298 be_lun = lun->be_lun; 13299 if ((lun->flags & CTL_LUN_DISABLED) || 13300 (lun->flags & CTL_LUN_OFFLINE) || 13301 lun->backend->lun_attr == NULL) 13302 continue; 13303 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13304 softc->ha_mode == CTL_HA_MODE_XFER) 13305 continue; 13306 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13307 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13308 continue; 13309 e = 0; 13310 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13311 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13312 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13313 continue; 13314 thres = scsi_4btoul(page->descr[i].count); 13315 thres <<= CTL_LBP_EXPONENT; 13316 switch (page->descr[i].resource) { 13317 case 0x01: 13318 attr = "blocksavail"; 13319 break; 13320 case 0x02: 13321 attr = "blocksused"; 13322 break; 13323 case 0xf1: 13324 attr = "poolblocksavail"; 13325 break; 13326 case 0xf2: 13327 attr = "poolblocksused"; 13328 break; 13329 default: 13330 continue; 13331 } 13332 mtx_unlock(&softc->ctl_lock); // XXX 13333 val = lun->backend->lun_attr( 13334 lun->be_lun->be_lun, attr); 13335 mtx_lock(&softc->ctl_lock); 13336 if (val == UINT64_MAX) 13337 continue; 13338 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13339 == SLBPPD_ARMING_INC) 13340 e = (val >= thres); 13341 else 13342 e = (val <= thres); 13343 if (e) 13344 break; 13345 } 13346 mtx_lock(&lun->lun_lock); 13347 if (e) { 13348 scsi_u64to8b((uint8_t *)&page->descr[i] - 13349 (uint8_t *)page, lun->ua_tpt_info); 13350 if (lun->lasttpt == 0 || 13351 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13352 lun->lasttpt = time_uptime; 13353 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13354 set = 1; 13355 } else 13356 set = 0; 13357 } else { 13358 lun->lasttpt = 0; 13359 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13360 set = -1; 13361 } 13362 mtx_unlock(&lun->lun_lock); 13363 if (set != 0 && 13364 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13365 /* Send msg to other side. */ 13366 bzero(&msg.ua, sizeof(msg.ua)); 13367 msg.hdr.msg_type = CTL_MSG_UA; 13368 msg.hdr.nexus.initid = -1; 13369 msg.hdr.nexus.targ_port = -1; 13370 msg.hdr.nexus.targ_lun = lun->lun; 13371 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13372 msg.ua.ua_all = 1; 13373 msg.ua.ua_set = (set > 0); 13374 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13375 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13376 mtx_unlock(&softc->ctl_lock); // XXX 13377 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13378 sizeof(msg.ua), M_WAITOK); 13379 mtx_lock(&softc->ctl_lock); 13380 } 13381 } 13382 mtx_unlock(&softc->ctl_lock); 13383 pause("-", CTL_LBP_PERIOD * hz); 13384 } 13385} 13386 13387static void 13388ctl_enqueue_incoming(union ctl_io *io) 13389{ 13390 struct ctl_softc *softc = control_softc; 13391 struct ctl_thread *thr; 13392 u_int idx; 13393 13394 idx = (io->io_hdr.nexus.targ_port * 127 + 13395 io->io_hdr.nexus.initid) % worker_threads; 13396 thr = &softc->threads[idx]; 13397 mtx_lock(&thr->queue_lock); 13398 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13399 mtx_unlock(&thr->queue_lock); 13400 wakeup(thr); 13401} 13402 13403static void 13404ctl_enqueue_rtr(union ctl_io *io) 13405{ 13406 struct ctl_softc *softc = control_softc; 13407 struct ctl_thread *thr; 13408 13409 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13410 mtx_lock(&thr->queue_lock); 13411 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13412 mtx_unlock(&thr->queue_lock); 13413 wakeup(thr); 13414} 13415 13416static void 13417ctl_enqueue_done(union ctl_io *io) 13418{ 13419 struct ctl_softc *softc = control_softc; 13420 struct ctl_thread *thr; 13421 13422 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13423 mtx_lock(&thr->queue_lock); 13424 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13425 mtx_unlock(&thr->queue_lock); 13426 wakeup(thr); 13427} 13428 13429static void 13430ctl_enqueue_isc(union ctl_io *io) 13431{ 13432 struct ctl_softc *softc = control_softc; 13433 struct ctl_thread *thr; 13434 13435 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13436 mtx_lock(&thr->queue_lock); 13437 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13438 mtx_unlock(&thr->queue_lock); 13439 wakeup(thr); 13440} 13441 13442/* 13443 * vim: ts=8 13444 */ 13445