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