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