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