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