ctl.c revision 267639
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 267639 2014-06-19 09:46:43Z 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 * The default is to run with CTL_DONE_THREAD turned on. Completed 87 * transactions are queued for processing by the CTL work thread. When 88 * CTL_DONE_THREAD is not defined, completed transactions are processed in 89 * the caller's context. 90 */ 91#define CTL_DONE_THREAD 92 93/* 94 * Size and alignment macros needed for Copan-specific HA hardware. These 95 * can go away when the HA code is re-written, and uses busdma for any 96 * hardware. 97 */ 98#define CTL_ALIGN_8B(target, source, type) \ 99 if (((uint32_t)source & 0x7) != 0) \ 100 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 101 else \ 102 target = (type)source; 103 104#define CTL_SIZE_8B(target, size) \ 105 if ((size & 0x7) != 0) \ 106 target = size + (0x8 - (size & 0x7)); \ 107 else \ 108 target = size; 109 110#define CTL_ALIGN_8B_MARGIN 16 111 112/* 113 * Template mode pages. 114 */ 115 116/* 117 * Note that these are default values only. The actual values will be 118 * filled in when the user does a mode sense. 119 */ 120static struct copan_power_subpage power_page_default = { 121 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 122 /*subpage*/ PWR_SUBPAGE_CODE, 123 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 124 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 125 /*page_version*/ PWR_VERSION, 126 /* total_luns */ 26, 127 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 128 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 130 0, 0, 0, 0, 0, 0} 131}; 132 133static struct copan_power_subpage power_page_changeable = { 134 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 135 /*subpage*/ PWR_SUBPAGE_CODE, 136 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 137 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 138 /*page_version*/ 0, 139 /* total_luns */ 0, 140 /* max_active_luns*/ 0, 141 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143 0, 0, 0, 0, 0, 0} 144}; 145 146static struct copan_aps_subpage aps_page_default = { 147 APS_PAGE_CODE | SMPH_SPF, //page_code 148 APS_SUBPAGE_CODE, //subpage 149 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 150 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 151 APS_VERSION, //page_version 152 0, //lock_active 153 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 154 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155 0, 0, 0, 0, 0} //reserved 156}; 157 158static struct copan_aps_subpage aps_page_changeable = { 159 APS_PAGE_CODE | SMPH_SPF, //page_code 160 APS_SUBPAGE_CODE, //subpage 161 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 162 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 163 0, //page_version 164 0, //lock_active 165 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 167 0, 0, 0, 0, 0} //reserved 168}; 169 170static struct copan_debugconf_subpage debugconf_page_default = { 171 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 172 DBGCNF_SUBPAGE_CODE, /* subpage */ 173 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 174 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 175 DBGCNF_VERSION, /* page_version */ 176 {CTL_TIME_IO_DEFAULT_SECS>>8, 177 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 178}; 179 180static struct copan_debugconf_subpage debugconf_page_changeable = { 181 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 182 DBGCNF_SUBPAGE_CODE, /* subpage */ 183 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 184 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 185 0, /* page_version */ 186 {0xff,0xff}, /* ctl_time_io_secs */ 187}; 188 189static struct scsi_format_page format_page_default = { 190 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 191 /*page_length*/sizeof(struct scsi_format_page) - 2, 192 /*tracks_per_zone*/ {0, 0}, 193 /*alt_sectors_per_zone*/ {0, 0}, 194 /*alt_tracks_per_zone*/ {0, 0}, 195 /*alt_tracks_per_lun*/ {0, 0}, 196 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 197 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 198 /*bytes_per_sector*/ {0, 0}, 199 /*interleave*/ {0, 0}, 200 /*track_skew*/ {0, 0}, 201 /*cylinder_skew*/ {0, 0}, 202 /*flags*/ SFP_HSEC, 203 /*reserved*/ {0, 0, 0} 204}; 205 206static struct scsi_format_page format_page_changeable = { 207 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 208 /*page_length*/sizeof(struct scsi_format_page) - 2, 209 /*tracks_per_zone*/ {0, 0}, 210 /*alt_sectors_per_zone*/ {0, 0}, 211 /*alt_tracks_per_zone*/ {0, 0}, 212 /*alt_tracks_per_lun*/ {0, 0}, 213 /*sectors_per_track*/ {0, 0}, 214 /*bytes_per_sector*/ {0, 0}, 215 /*interleave*/ {0, 0}, 216 /*track_skew*/ {0, 0}, 217 /*cylinder_skew*/ {0, 0}, 218 /*flags*/ 0, 219 /*reserved*/ {0, 0, 0} 220}; 221 222static struct scsi_rigid_disk_page rigid_disk_page_default = { 223 /*page_code*/SMS_RIGID_DISK_PAGE, 224 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 225 /*cylinders*/ {0, 0, 0}, 226 /*heads*/ CTL_DEFAULT_HEADS, 227 /*start_write_precomp*/ {0, 0, 0}, 228 /*start_reduced_current*/ {0, 0, 0}, 229 /*step_rate*/ {0, 0}, 230 /*landing_zone_cylinder*/ {0, 0, 0}, 231 /*rpl*/ SRDP_RPL_DISABLED, 232 /*rotational_offset*/ 0, 233 /*reserved1*/ 0, 234 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 235 CTL_DEFAULT_ROTATION_RATE & 0xff}, 236 /*reserved2*/ {0, 0} 237}; 238 239static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 240 /*page_code*/SMS_RIGID_DISK_PAGE, 241 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 242 /*cylinders*/ {0, 0, 0}, 243 /*heads*/ 0, 244 /*start_write_precomp*/ {0, 0, 0}, 245 /*start_reduced_current*/ {0, 0, 0}, 246 /*step_rate*/ {0, 0}, 247 /*landing_zone_cylinder*/ {0, 0, 0}, 248 /*rpl*/ 0, 249 /*rotational_offset*/ 0, 250 /*reserved1*/ 0, 251 /*rotation_rate*/ {0, 0}, 252 /*reserved2*/ {0, 0} 253}; 254 255static struct scsi_caching_page caching_page_default = { 256 /*page_code*/SMS_CACHING_PAGE, 257 /*page_length*/sizeof(struct scsi_caching_page) - 2, 258 /*flags1*/ SCP_DISC | SCP_WCE, 259 /*ret_priority*/ 0, 260 /*disable_pf_transfer_len*/ {0xff, 0xff}, 261 /*min_prefetch*/ {0, 0}, 262 /*max_prefetch*/ {0xff, 0xff}, 263 /*max_pf_ceiling*/ {0xff, 0xff}, 264 /*flags2*/ 0, 265 /*cache_segments*/ 0, 266 /*cache_seg_size*/ {0, 0}, 267 /*reserved*/ 0, 268 /*non_cache_seg_size*/ {0, 0, 0} 269}; 270 271static struct scsi_caching_page caching_page_changeable = { 272 /*page_code*/SMS_CACHING_PAGE, 273 /*page_length*/sizeof(struct scsi_caching_page) - 2, 274 /*flags1*/ 0, 275 /*ret_priority*/ 0, 276 /*disable_pf_transfer_len*/ {0, 0}, 277 /*min_prefetch*/ {0, 0}, 278 /*max_prefetch*/ {0, 0}, 279 /*max_pf_ceiling*/ {0, 0}, 280 /*flags2*/ 0, 281 /*cache_segments*/ 0, 282 /*cache_seg_size*/ {0, 0}, 283 /*reserved*/ 0, 284 /*non_cache_seg_size*/ {0, 0, 0} 285}; 286 287static struct scsi_control_page control_page_default = { 288 /*page_code*/SMS_CONTROL_MODE_PAGE, 289 /*page_length*/sizeof(struct scsi_control_page) - 2, 290 /*rlec*/0, 291 /*queue_flags*/0, 292 /*eca_and_aen*/0, 293 /*reserved*/0, 294 /*aen_holdoff_period*/{0, 0} 295}; 296 297static struct scsi_control_page control_page_changeable = { 298 /*page_code*/SMS_CONTROL_MODE_PAGE, 299 /*page_length*/sizeof(struct scsi_control_page) - 2, 300 /*rlec*/SCP_DSENSE, 301 /*queue_flags*/0, 302 /*eca_and_aen*/0, 303 /*reserved*/0, 304 /*aen_holdoff_period*/{0, 0} 305}; 306 307 308/* 309 * XXX KDM move these into the softc. 310 */ 311static int rcv_sync_msg; 312static int persis_offset; 313static uint8_t ctl_pause_rtr; 314static int ctl_is_single = 1; 315static int index_to_aps_page; 316 317SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 318static int worker_threads = 1; 319TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 320SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 321 &worker_threads, 1, "Number of worker threads"); 322static int verbose = 0; 323TUNABLE_INT("kern.cam.ctl.verbose", &verbose); 324SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 325 &verbose, 0, "Show SCSI errors returned to initiator"); 326 327/* 328 * Serial number (0x80), device id (0x83), supported pages (0x00), 329 * Block limits (0xB0) and Logical Block Provisioning (0xB2) 330 */ 331#define SCSI_EVPD_NUM_SUPPORTED_PAGES 5 332 333static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 334 int param); 335static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 336static int ctl_init(void); 337void ctl_shutdown(void); 338static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 339static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 340static void ctl_ioctl_online(void *arg); 341static void ctl_ioctl_offline(void *arg); 342static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id); 343static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id); 344static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 345static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 346static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 347static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock); 348static int ctl_ioctl_submit_wait(union ctl_io *io); 349static void ctl_ioctl_datamove(union ctl_io *io); 350static void ctl_ioctl_done(union ctl_io *io); 351static void ctl_ioctl_hard_startstop_callback(void *arg, 352 struct cfi_metatask *metatask); 353static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 354static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 355 struct ctl_ooa *ooa_hdr, 356 struct ctl_ooa_entry *kern_entries); 357static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 358 struct thread *td); 359uint32_t ctl_get_resindex(struct ctl_nexus *nexus); 360uint32_t ctl_port_idx(int port_num); 361#ifdef unused 362static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 363 uint32_t targ_target, uint32_t targ_lun, 364 int can_wait); 365static void ctl_kfree_io(union ctl_io *io); 366#endif /* unused */ 367static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 368 struct ctl_be_lun *be_lun, struct ctl_id target_id); 369static int ctl_free_lun(struct ctl_lun *lun); 370static void ctl_create_lun(struct ctl_be_lun *be_lun); 371/** 372static void ctl_failover_change_pages(struct ctl_softc *softc, 373 struct ctl_scsiio *ctsio, int master); 374**/ 375 376static int ctl_do_mode_select(union ctl_io *io); 377static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 378 uint64_t res_key, uint64_t sa_res_key, 379 uint8_t type, uint32_t residx, 380 struct ctl_scsiio *ctsio, 381 struct scsi_per_res_out *cdb, 382 struct scsi_per_res_out_parms* param); 383static void ctl_pro_preempt_other(struct ctl_lun *lun, 384 union ctl_ha_msg *msg); 385static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 386static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 387static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 388static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 389static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 390 int alloc_len); 391static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 392static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 393static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 394static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 395static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 396static ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 397 union ctl_io *ooa_io); 398static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 399 union ctl_io *starting_io); 400static int ctl_check_blocked(struct ctl_lun *lun); 401static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 402 struct ctl_lun *lun, 403 struct ctl_cmd_entry *entry, 404 struct ctl_scsiio *ctsio); 405//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 406static void ctl_failover(void); 407static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 408 struct ctl_scsiio *ctsio); 409static int ctl_scsiio(struct ctl_scsiio *ctsio); 410 411static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 412static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 413 ctl_ua_type ua_type); 414static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 415 ctl_ua_type ua_type); 416static int ctl_abort_task(union ctl_io *io); 417static void ctl_run_task_queue(struct ctl_softc *ctl_softc); 418#ifdef CTL_IO_DELAY 419static void ctl_datamove_timer_wakeup(void *arg); 420static void ctl_done_timer_wakeup(void *arg); 421#endif /* CTL_IO_DELAY */ 422 423static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 424static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 425static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 426static void ctl_datamove_remote_write(union ctl_io *io); 427static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 428static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 429static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 430static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 431 ctl_ha_dt_cb callback); 432static void ctl_datamove_remote_read(union ctl_io *io); 433static void ctl_datamove_remote(union ctl_io *io); 434static int ctl_process_done(union ctl_io *io, int have_lock); 435static void ctl_work_thread(void *arg); 436 437/* 438 * Load the serialization table. This isn't very pretty, but is probably 439 * the easiest way to do it. 440 */ 441#include "ctl_ser_table.c" 442 443/* 444 * We only need to define open, close and ioctl routines for this driver. 445 */ 446static struct cdevsw ctl_cdevsw = { 447 .d_version = D_VERSION, 448 .d_flags = 0, 449 .d_open = ctl_open, 450 .d_close = ctl_close, 451 .d_ioctl = ctl_ioctl, 452 .d_name = "ctl", 453}; 454 455 456MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 457 458static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 459 460static moduledata_t ctl_moduledata = { 461 "ctl", 462 ctl_module_event_handler, 463 NULL 464}; 465 466DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 467MODULE_VERSION(ctl, 1); 468 469static void 470ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 471 union ctl_ha_msg *msg_info) 472{ 473 struct ctl_scsiio *ctsio; 474 475 if (msg_info->hdr.original_sc == NULL) { 476 printf("%s: original_sc == NULL!\n", __func__); 477 /* XXX KDM now what? */ 478 return; 479 } 480 481 ctsio = &msg_info->hdr.original_sc->scsiio; 482 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 483 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 484 ctsio->io_hdr.status = msg_info->hdr.status; 485 ctsio->scsi_status = msg_info->scsi.scsi_status; 486 ctsio->sense_len = msg_info->scsi.sense_len; 487 ctsio->sense_residual = msg_info->scsi.sense_residual; 488 ctsio->residual = msg_info->scsi.residual; 489 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 490 sizeof(ctsio->sense_data)); 491 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 492 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 493 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links); 494 ctl_wakeup_thread(); 495} 496 497static void 498ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 499 union ctl_ha_msg *msg_info) 500{ 501 struct ctl_scsiio *ctsio; 502 503 if (msg_info->hdr.serializing_sc == NULL) { 504 printf("%s: serializing_sc == NULL!\n", __func__); 505 /* XXX KDM now what? */ 506 return; 507 } 508 509 ctsio = &msg_info->hdr.serializing_sc->scsiio; 510#if 0 511 /* 512 * Attempt to catch the situation where an I/O has 513 * been freed, and we're using it again. 514 */ 515 if (ctsio->io_hdr.io_type == 0xff) { 516 union ctl_io *tmp_io; 517 tmp_io = (union ctl_io *)ctsio; 518 printf("%s: %p use after free!\n", __func__, 519 ctsio); 520 printf("%s: type %d msg %d cdb %x iptl: " 521 "%d:%d:%d:%d tag 0x%04x " 522 "flag %#x status %x\n", 523 __func__, 524 tmp_io->io_hdr.io_type, 525 tmp_io->io_hdr.msg_type, 526 tmp_io->scsiio.cdb[0], 527 tmp_io->io_hdr.nexus.initid.id, 528 tmp_io->io_hdr.nexus.targ_port, 529 tmp_io->io_hdr.nexus.targ_target.id, 530 tmp_io->io_hdr.nexus.targ_lun, 531 (tmp_io->io_hdr.io_type == 532 CTL_IO_TASK) ? 533 tmp_io->taskio.tag_num : 534 tmp_io->scsiio.tag_num, 535 tmp_io->io_hdr.flags, 536 tmp_io->io_hdr.status); 537 } 538#endif 539 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 540 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links); 541 ctl_wakeup_thread(); 542} 543 544/* 545 * ISC (Inter Shelf Communication) event handler. Events from the HA 546 * subsystem come in here. 547 */ 548static void 549ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 550{ 551 struct ctl_softc *ctl_softc; 552 union ctl_io *io; 553 struct ctl_prio *presio; 554 ctl_ha_status isc_status; 555 556 ctl_softc = control_softc; 557 io = NULL; 558 559 560#if 0 561 printf("CTL: Isc Msg event %d\n", event); 562#endif 563 if (event == CTL_HA_EVT_MSG_RECV) { 564 union ctl_ha_msg msg_info; 565 566 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 567 sizeof(msg_info), /*wait*/ 0); 568#if 0 569 printf("CTL: msg_type %d\n", msg_info.msg_type); 570#endif 571 if (isc_status != 0) { 572 printf("Error receiving message, status = %d\n", 573 isc_status); 574 return; 575 } 576 mtx_lock(&ctl_softc->ctl_lock); 577 578 switch (msg_info.hdr.msg_type) { 579 case CTL_MSG_SERIALIZE: 580#if 0 581 printf("Serialize\n"); 582#endif 583 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 584 if (io == NULL) { 585 printf("ctl_isc_event_handler: can't allocate " 586 "ctl_io!\n"); 587 /* Bad Juju */ 588 /* Need to set busy and send msg back */ 589 mtx_unlock(&ctl_softc->ctl_lock); 590 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 591 msg_info.hdr.status = CTL_SCSI_ERROR; 592 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 593 msg_info.scsi.sense_len = 0; 594 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 595 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 596 } 597 goto bailout; 598 } 599 ctl_zero_io(io); 600 // populate ctsio from msg_info 601 io->io_hdr.io_type = CTL_IO_SCSI; 602 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 603 io->io_hdr.original_sc = msg_info.hdr.original_sc; 604#if 0 605 printf("pOrig %x\n", (int)msg_info.original_sc); 606#endif 607 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 608 CTL_FLAG_IO_ACTIVE; 609 /* 610 * If we're in serialization-only mode, we don't 611 * want to go through full done processing. Thus 612 * the COPY flag. 613 * 614 * XXX KDM add another flag that is more specific. 615 */ 616 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 617 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 618 io->io_hdr.nexus = msg_info.hdr.nexus; 619#if 0 620 printf("targ %d, port %d, iid %d, lun %d\n", 621 io->io_hdr.nexus.targ_target.id, 622 io->io_hdr.nexus.targ_port, 623 io->io_hdr.nexus.initid.id, 624 io->io_hdr.nexus.targ_lun); 625#endif 626 io->scsiio.tag_num = msg_info.scsi.tag_num; 627 io->scsiio.tag_type = msg_info.scsi.tag_type; 628 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 629 CTL_MAX_CDBLEN); 630 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 631 struct ctl_cmd_entry *entry; 632 uint8_t opcode; 633 634 opcode = io->scsiio.cdb[0]; 635 entry = &ctl_cmd_table[opcode]; 636 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 637 io->io_hdr.flags |= 638 entry->flags & CTL_FLAG_DATA_MASK; 639 } 640 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 641 &io->io_hdr, links); 642 ctl_wakeup_thread(); 643 break; 644 645 /* Performed on the Originating SC, XFER mode only */ 646 case CTL_MSG_DATAMOVE: { 647 struct ctl_sg_entry *sgl; 648 int i, j; 649 650 io = msg_info.hdr.original_sc; 651 if (io == NULL) { 652 printf("%s: original_sc == NULL!\n", __func__); 653 /* XXX KDM do something here */ 654 break; 655 } 656 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 657 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 658 /* 659 * Keep track of this, we need to send it back over 660 * when the datamove is complete. 661 */ 662 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 663 664 if (msg_info.dt.sg_sequence == 0) { 665 /* 666 * XXX KDM we use the preallocated S/G list 667 * here, but we'll need to change this to 668 * dynamic allocation if we need larger S/G 669 * lists. 670 */ 671 if (msg_info.dt.kern_sg_entries > 672 sizeof(io->io_hdr.remote_sglist) / 673 sizeof(io->io_hdr.remote_sglist[0])) { 674 printf("%s: number of S/G entries " 675 "needed %u > allocated num %zd\n", 676 __func__, 677 msg_info.dt.kern_sg_entries, 678 sizeof(io->io_hdr.remote_sglist)/ 679 sizeof(io->io_hdr.remote_sglist[0])); 680 681 /* 682 * XXX KDM send a message back to 683 * the other side to shut down the 684 * DMA. The error will come back 685 * through via the normal channel. 686 */ 687 break; 688 } 689 sgl = io->io_hdr.remote_sglist; 690 memset(sgl, 0, 691 sizeof(io->io_hdr.remote_sglist)); 692 693 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 694 695 io->scsiio.kern_sg_entries = 696 msg_info.dt.kern_sg_entries; 697 io->scsiio.rem_sg_entries = 698 msg_info.dt.kern_sg_entries; 699 io->scsiio.kern_data_len = 700 msg_info.dt.kern_data_len; 701 io->scsiio.kern_total_len = 702 msg_info.dt.kern_total_len; 703 io->scsiio.kern_data_resid = 704 msg_info.dt.kern_data_resid; 705 io->scsiio.kern_rel_offset = 706 msg_info.dt.kern_rel_offset; 707 /* 708 * Clear out per-DMA flags. 709 */ 710 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 711 /* 712 * Add per-DMA flags that are set for this 713 * particular DMA request. 714 */ 715 io->io_hdr.flags |= msg_info.dt.flags & 716 CTL_FLAG_RDMA_MASK; 717 } else 718 sgl = (struct ctl_sg_entry *) 719 io->scsiio.kern_data_ptr; 720 721 for (i = msg_info.dt.sent_sg_entries, j = 0; 722 i < (msg_info.dt.sent_sg_entries + 723 msg_info.dt.cur_sg_entries); i++, j++) { 724 sgl[i].addr = msg_info.dt.sg_list[j].addr; 725 sgl[i].len = msg_info.dt.sg_list[j].len; 726 727#if 0 728 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 729 __func__, 730 msg_info.dt.sg_list[j].addr, 731 msg_info.dt.sg_list[j].len, 732 sgl[i].addr, sgl[i].len, j, i); 733#endif 734 } 735#if 0 736 memcpy(&sgl[msg_info.dt.sent_sg_entries], 737 msg_info.dt.sg_list, 738 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 739#endif 740 741 /* 742 * If this is the last piece of the I/O, we've got 743 * the full S/G list. Queue processing in the thread. 744 * Otherwise wait for the next piece. 745 */ 746 if (msg_info.dt.sg_last != 0) { 747 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 748 &io->io_hdr, links); 749 ctl_wakeup_thread(); 750 } 751 break; 752 } 753 /* Performed on the Serializing (primary) SC, XFER mode only */ 754 case CTL_MSG_DATAMOVE_DONE: { 755 if (msg_info.hdr.serializing_sc == NULL) { 756 printf("%s: serializing_sc == NULL!\n", 757 __func__); 758 /* XXX KDM now what? */ 759 break; 760 } 761 /* 762 * We grab the sense information here in case 763 * there was a failure, so we can return status 764 * back to the initiator. 765 */ 766 io = msg_info.hdr.serializing_sc; 767 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 768 io->io_hdr.status = msg_info.hdr.status; 769 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 770 io->scsiio.sense_len = msg_info.scsi.sense_len; 771 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 772 io->io_hdr.port_status = msg_info.scsi.fetd_status; 773 io->scsiio.residual = msg_info.scsi.residual; 774 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 775 sizeof(io->scsiio.sense_data)); 776 777 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 778 &io->io_hdr, links); 779 ctl_wakeup_thread(); 780 break; 781 } 782 783 /* Preformed on Originating SC, SER_ONLY mode */ 784 case CTL_MSG_R2R: 785 io = msg_info.hdr.original_sc; 786 if (io == NULL) { 787 printf("%s: Major Bummer\n", __func__); 788 mtx_unlock(&ctl_softc->ctl_lock); 789 return; 790 } else { 791#if 0 792 printf("pOrig %x\n",(int) ctsio); 793#endif 794 } 795 io->io_hdr.msg_type = CTL_MSG_R2R; 796 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 797 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 798 &io->io_hdr, links); 799 ctl_wakeup_thread(); 800 break; 801 802 /* 803 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 804 * mode. 805 * Performed on the Originating (i.e. secondary) SC in XFER 806 * mode 807 */ 808 case CTL_MSG_FINISH_IO: 809 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 810 ctl_isc_handler_finish_xfer(ctl_softc, 811 &msg_info); 812 else 813 ctl_isc_handler_finish_ser_only(ctl_softc, 814 &msg_info); 815 break; 816 817 /* Preformed on Originating SC */ 818 case CTL_MSG_BAD_JUJU: 819 io = msg_info.hdr.original_sc; 820 if (io == NULL) { 821 printf("%s: Bad JUJU!, original_sc is NULL!\n", 822 __func__); 823 break; 824 } 825 ctl_copy_sense_data(&msg_info, io); 826 /* 827 * IO should have already been cleaned up on other 828 * SC so clear this flag so we won't send a message 829 * back to finish the IO there. 830 */ 831 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 832 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 833 834 /* io = msg_info.hdr.serializing_sc; */ 835 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 836 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 837 &io->io_hdr, links); 838 ctl_wakeup_thread(); 839 break; 840 841 /* Handle resets sent from the other side */ 842 case CTL_MSG_MANAGE_TASKS: { 843 struct ctl_taskio *taskio; 844 taskio = (struct ctl_taskio *)ctl_alloc_io( 845 (void *)ctl_softc->othersc_pool); 846 if (taskio == NULL) { 847 printf("ctl_isc_event_handler: can't allocate " 848 "ctl_io!\n"); 849 /* Bad Juju */ 850 /* should I just call the proper reset func 851 here??? */ 852 mtx_unlock(&ctl_softc->ctl_lock); 853 goto bailout; 854 } 855 ctl_zero_io((union ctl_io *)taskio); 856 taskio->io_hdr.io_type = CTL_IO_TASK; 857 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 858 taskio->io_hdr.nexus = msg_info.hdr.nexus; 859 taskio->task_action = msg_info.task.task_action; 860 taskio->tag_num = msg_info.task.tag_num; 861 taskio->tag_type = msg_info.task.tag_type; 862#ifdef CTL_TIME_IO 863 taskio->io_hdr.start_time = time_uptime; 864 getbintime(&taskio->io_hdr.start_bt); 865#if 0 866 cs_prof_gettime(&taskio->io_hdr.start_ticks); 867#endif 868#endif /* CTL_TIME_IO */ 869 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, 870 &taskio->io_hdr, links); 871 ctl_softc->flags |= CTL_FLAG_TASK_PENDING; 872 ctl_wakeup_thread(); 873 break; 874 } 875 /* Persistent Reserve action which needs attention */ 876 case CTL_MSG_PERS_ACTION: 877 presio = (struct ctl_prio *)ctl_alloc_io( 878 (void *)ctl_softc->othersc_pool); 879 if (presio == NULL) { 880 printf("ctl_isc_event_handler: can't allocate " 881 "ctl_io!\n"); 882 /* Bad Juju */ 883 /* Need to set busy and send msg back */ 884 mtx_unlock(&ctl_softc->ctl_lock); 885 goto bailout; 886 } 887 ctl_zero_io((union ctl_io *)presio); 888 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 889 presio->pr_msg = msg_info.pr; 890 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, 891 &presio->io_hdr, links); 892 ctl_wakeup_thread(); 893 break; 894 case CTL_MSG_SYNC_FE: 895 rcv_sync_msg = 1; 896 break; 897 case CTL_MSG_APS_LOCK: { 898 // It's quicker to execute this then to 899 // queue it. 900 struct ctl_lun *lun; 901 struct ctl_page_index *page_index; 902 struct copan_aps_subpage *current_sp; 903 uint32_t targ_lun; 904 905 targ_lun = msg_info.hdr.nexus.targ_lun; 906 if (msg_info.hdr.nexus.lun_map_fn != NULL) 907 targ_lun = msg_info.hdr.nexus.lun_map_fn(msg_info.hdr.nexus.lun_map_arg, targ_lun); 908 909 lun = ctl_softc->ctl_luns[targ_lun]; 910 page_index = &lun->mode_pages.index[index_to_aps_page]; 911 current_sp = (struct copan_aps_subpage *) 912 (page_index->page_data + 913 (page_index->page_len * CTL_PAGE_CURRENT)); 914 915 current_sp->lock_active = msg_info.aps.lock_flag; 916 break; 917 } 918 default: 919 printf("How did I get here?\n"); 920 } 921 mtx_unlock(&ctl_softc->ctl_lock); 922 } else if (event == CTL_HA_EVT_MSG_SENT) { 923 if (param != CTL_HA_STATUS_SUCCESS) { 924 printf("Bad status from ctl_ha_msg_send status %d\n", 925 param); 926 } 927 return; 928 } else if (event == CTL_HA_EVT_DISCONNECT) { 929 printf("CTL: Got a disconnect from Isc\n"); 930 return; 931 } else { 932 printf("ctl_isc_event_handler: Unknown event %d\n", event); 933 return; 934 } 935 936bailout: 937 return; 938} 939 940static void 941ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 942{ 943 struct scsi_sense_data *sense; 944 945 sense = &dest->scsiio.sense_data; 946 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 947 dest->scsiio.scsi_status = src->scsi.scsi_status; 948 dest->scsiio.sense_len = src->scsi.sense_len; 949 dest->io_hdr.status = src->hdr.status; 950} 951 952static int 953ctl_init(void) 954{ 955 struct ctl_softc *softc; 956 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 957 struct ctl_frontend *fe; 958 uint8_t sc_id =0; 959 int i, error, retval; 960 //int isc_retval; 961 962 retval = 0; 963 ctl_pause_rtr = 0; 964 rcv_sync_msg = 0; 965 966 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 967 M_WAITOK | M_ZERO); 968 softc = control_softc; 969 970 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 971 "cam/ctl"); 972 973 softc->dev->si_drv1 = softc; 974 975 /* 976 * By default, return a "bad LUN" peripheral qualifier for unknown 977 * LUNs. The user can override this default using the tunable or 978 * sysctl. See the comment in ctl_inquiry_std() for more details. 979 */ 980 softc->inquiry_pq_no_lun = 1; 981 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 982 &softc->inquiry_pq_no_lun); 983 sysctl_ctx_init(&softc->sysctl_ctx); 984 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 985 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 986 CTLFLAG_RD, 0, "CAM Target Layer"); 987 988 if (softc->sysctl_tree == NULL) { 989 printf("%s: unable to allocate sysctl tree\n", __func__); 990 destroy_dev(softc->dev); 991 free(control_softc, M_DEVBUF); 992 control_softc = NULL; 993 return (ENOMEM); 994 } 995 996 SYSCTL_ADD_INT(&softc->sysctl_ctx, 997 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 998 "inquiry_pq_no_lun", CTLFLAG_RW, 999 &softc->inquiry_pq_no_lun, 0, 1000 "Report no lun possible for invalid LUNs"); 1001 1002 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1003 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 1004 softc->open_count = 0; 1005 1006 /* 1007 * Default to actually sending a SYNCHRONIZE CACHE command down to 1008 * the drive. 1009 */ 1010 softc->flags = CTL_FLAG_REAL_SYNC; 1011 1012 /* 1013 * In Copan's HA scheme, the "master" and "slave" roles are 1014 * figured out through the slot the controller is in. Although it 1015 * is an active/active system, someone has to be in charge. 1016 */ 1017#ifdef NEEDTOPORT 1018 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1019#endif 1020 1021 if (sc_id == 0) { 1022 softc->flags |= CTL_FLAG_MASTER_SHELF; 1023 persis_offset = 0; 1024 } else 1025 persis_offset = CTL_MAX_INITIATORS; 1026 1027 /* 1028 * XXX KDM need to figure out where we want to get our target ID 1029 * and WWID. Is it different on each port? 1030 */ 1031 softc->target.id = 0; 1032 softc->target.wwid[0] = 0x12345678; 1033 softc->target.wwid[1] = 0x87654321; 1034 STAILQ_INIT(&softc->lun_list); 1035 STAILQ_INIT(&softc->pending_lun_queue); 1036 STAILQ_INIT(&softc->task_queue); 1037 STAILQ_INIT(&softc->incoming_queue); 1038 STAILQ_INIT(&softc->rtr_queue); 1039 STAILQ_INIT(&softc->done_queue); 1040 STAILQ_INIT(&softc->isc_queue); 1041 STAILQ_INIT(&softc->fe_list); 1042 STAILQ_INIT(&softc->be_list); 1043 STAILQ_INIT(&softc->io_pools); 1044 1045 /* 1046 * We don't bother calling these with ctl_lock held here, because, 1047 * in theory, no one else can try to do anything while we're in our 1048 * module init routine. 1049 */ 1050 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1051 &internal_pool)!= 0){ 1052 printf("ctl: can't allocate %d entry internal pool, " 1053 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1054 return (ENOMEM); 1055 } 1056 1057 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1058 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1059 printf("ctl: can't allocate %d entry emergency pool, " 1060 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1061 ctl_pool_free(internal_pool); 1062 return (ENOMEM); 1063 } 1064 1065 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1066 &other_pool) != 0) 1067 { 1068 printf("ctl: can't allocate %d entry other SC pool, " 1069 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1070 ctl_pool_free(internal_pool); 1071 ctl_pool_free(emergency_pool); 1072 return (ENOMEM); 1073 } 1074 1075 softc->internal_pool = internal_pool; 1076 softc->emergency_pool = emergency_pool; 1077 softc->othersc_pool = other_pool; 1078 1079 if (worker_threads > MAXCPU || worker_threads == 0) { 1080 printf("invalid kern.cam.ctl.worker_threads value; " 1081 "setting to 1"); 1082 worker_threads = 1; 1083 } else if (worker_threads < 0) { 1084 if (mp_ncpus > 2) { 1085 /* 1086 * Using more than two worker threads actually hurts 1087 * performance due to lock contention. 1088 */ 1089 worker_threads = 2; 1090 } else { 1091 worker_threads = 1; 1092 } 1093 } 1094 1095 for (i = 0; i < worker_threads; i++) { 1096 error = kproc_kthread_add(ctl_work_thread, softc, 1097 &softc->work_thread, NULL, 0, 0, "ctl", "work%d", i); 1098 if (error != 0) { 1099 printf("error creating CTL work thread!\n"); 1100 ctl_pool_free(internal_pool); 1101 ctl_pool_free(emergency_pool); 1102 ctl_pool_free(other_pool); 1103 return (error); 1104 } 1105 } 1106 if (bootverbose) 1107 printf("ctl: CAM Target Layer loaded\n"); 1108 1109 /* 1110 * Initialize the initiator and portname mappings 1111 */ 1112 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid)); 1113 1114 /* 1115 * Initialize the ioctl front end. 1116 */ 1117 fe = &softc->ioctl_info.fe; 1118 sprintf(softc->ioctl_info.port_name, "CTL ioctl"); 1119 fe->port_type = CTL_PORT_IOCTL; 1120 fe->num_requested_ctl_io = 100; 1121 fe->port_name = softc->ioctl_info.port_name; 1122 fe->port_online = ctl_ioctl_online; 1123 fe->port_offline = ctl_ioctl_offline; 1124 fe->onoff_arg = &softc->ioctl_info; 1125 fe->targ_enable = ctl_ioctl_targ_enable; 1126 fe->targ_disable = ctl_ioctl_targ_disable; 1127 fe->lun_enable = ctl_ioctl_lun_enable; 1128 fe->lun_disable = ctl_ioctl_lun_disable; 1129 fe->targ_lun_arg = &softc->ioctl_info; 1130 fe->fe_datamove = ctl_ioctl_datamove; 1131 fe->fe_done = ctl_ioctl_done; 1132 fe->max_targets = 15; 1133 fe->max_target_id = 15; 1134 1135 if (ctl_frontend_register(&softc->ioctl_info.fe, 1136 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1137 printf("ctl: ioctl front end registration failed, will " 1138 "continue anyway\n"); 1139 } 1140 1141#ifdef CTL_IO_DELAY 1142 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1143 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1144 sizeof(struct callout), CTL_TIMER_BYTES); 1145 return (EINVAL); 1146 } 1147#endif /* CTL_IO_DELAY */ 1148 1149 return (0); 1150} 1151 1152void 1153ctl_shutdown(void) 1154{ 1155 struct ctl_softc *softc; 1156 struct ctl_lun *lun, *next_lun; 1157 struct ctl_io_pool *pool; 1158 1159 softc = (struct ctl_softc *)control_softc; 1160 1161 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0) 1162 printf("ctl: ioctl front end deregistration failed\n"); 1163 1164 mtx_lock(&softc->ctl_lock); 1165 1166 /* 1167 * Free up each LUN. 1168 */ 1169 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1170 next_lun = STAILQ_NEXT(lun, links); 1171 ctl_free_lun(lun); 1172 } 1173 1174 mtx_unlock(&softc->ctl_lock); 1175 1176 /* 1177 * This will rip the rug out from under any FETDs or anyone else 1178 * that has a pool allocated. Since we increment our module 1179 * refcount any time someone outside the main CTL module allocates 1180 * a pool, we shouldn't have any problems here. The user won't be 1181 * able to unload the CTL module until client modules have 1182 * successfully unloaded. 1183 */ 1184 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1185 ctl_pool_free(pool); 1186 1187#if 0 1188 ctl_shutdown_thread(softc->work_thread); 1189#endif 1190 1191 mtx_destroy(&softc->pool_lock); 1192 mtx_destroy(&softc->ctl_lock); 1193 1194 destroy_dev(softc->dev); 1195 1196 sysctl_ctx_free(&softc->sysctl_ctx); 1197 1198 free(control_softc, M_DEVBUF); 1199 control_softc = NULL; 1200 1201 if (bootverbose) 1202 printf("ctl: CAM Target Layer unloaded\n"); 1203} 1204 1205static int 1206ctl_module_event_handler(module_t mod, int what, void *arg) 1207{ 1208 1209 switch (what) { 1210 case MOD_LOAD: 1211 return (ctl_init()); 1212 case MOD_UNLOAD: 1213 return (EBUSY); 1214 default: 1215 return (EOPNOTSUPP); 1216 } 1217} 1218 1219/* 1220 * XXX KDM should we do some access checks here? Bump a reference count to 1221 * prevent a CTL module from being unloaded while someone has it open? 1222 */ 1223static int 1224ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1225{ 1226 return (0); 1227} 1228 1229static int 1230ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1231{ 1232 return (0); 1233} 1234 1235int 1236ctl_port_enable(ctl_port_type port_type) 1237{ 1238 struct ctl_softc *softc; 1239 struct ctl_frontend *fe; 1240 1241 if (ctl_is_single == 0) { 1242 union ctl_ha_msg msg_info; 1243 int isc_retval; 1244 1245#if 0 1246 printf("%s: HA mode, synchronizing frontend enable\n", 1247 __func__); 1248#endif 1249 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1250 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1251 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1252 printf("Sync msg send error retval %d\n", isc_retval); 1253 } 1254 if (!rcv_sync_msg) { 1255 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1256 sizeof(msg_info), 1); 1257 } 1258#if 0 1259 printf("CTL:Frontend Enable\n"); 1260 } else { 1261 printf("%s: single mode, skipping frontend synchronization\n", 1262 __func__); 1263#endif 1264 } 1265 1266 softc = control_softc; 1267 1268 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1269 if (port_type & fe->port_type) 1270 { 1271#if 0 1272 printf("port %d\n", fe->targ_port); 1273#endif 1274 ctl_frontend_online(fe); 1275 } 1276 } 1277 1278 return (0); 1279} 1280 1281int 1282ctl_port_disable(ctl_port_type port_type) 1283{ 1284 struct ctl_softc *softc; 1285 struct ctl_frontend *fe; 1286 1287 softc = control_softc; 1288 1289 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1290 if (port_type & fe->port_type) 1291 ctl_frontend_offline(fe); 1292 } 1293 1294 return (0); 1295} 1296 1297/* 1298 * Returns 0 for success, 1 for failure. 1299 * Currently the only failure mode is if there aren't enough entries 1300 * allocated. So, in case of a failure, look at num_entries_dropped, 1301 * reallocate and try again. 1302 */ 1303int 1304ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1305 int *num_entries_filled, int *num_entries_dropped, 1306 ctl_port_type port_type, int no_virtual) 1307{ 1308 struct ctl_softc *softc; 1309 struct ctl_frontend *fe; 1310 int entries_dropped, entries_filled; 1311 int retval; 1312 int i; 1313 1314 softc = control_softc; 1315 1316 retval = 0; 1317 entries_filled = 0; 1318 entries_dropped = 0; 1319 1320 i = 0; 1321 mtx_lock(&softc->ctl_lock); 1322 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1323 struct ctl_port_entry *entry; 1324 1325 if ((fe->port_type & port_type) == 0) 1326 continue; 1327 1328 if ((no_virtual != 0) 1329 && (fe->virtual_port != 0)) 1330 continue; 1331 1332 if (entries_filled >= num_entries_alloced) { 1333 entries_dropped++; 1334 continue; 1335 } 1336 entry = &entries[i]; 1337 1338 entry->port_type = fe->port_type; 1339 strlcpy(entry->port_name, fe->port_name, 1340 sizeof(entry->port_name)); 1341 entry->physical_port = fe->physical_port; 1342 entry->virtual_port = fe->virtual_port; 1343 entry->wwnn = fe->wwnn; 1344 entry->wwpn = fe->wwpn; 1345 1346 i++; 1347 entries_filled++; 1348 } 1349 1350 mtx_unlock(&softc->ctl_lock); 1351 1352 if (entries_dropped > 0) 1353 retval = 1; 1354 1355 *num_entries_dropped = entries_dropped; 1356 *num_entries_filled = entries_filled; 1357 1358 return (retval); 1359} 1360 1361static void 1362ctl_ioctl_online(void *arg) 1363{ 1364 struct ctl_ioctl_info *ioctl_info; 1365 1366 ioctl_info = (struct ctl_ioctl_info *)arg; 1367 1368 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1369} 1370 1371static void 1372ctl_ioctl_offline(void *arg) 1373{ 1374 struct ctl_ioctl_info *ioctl_info; 1375 1376 ioctl_info = (struct ctl_ioctl_info *)arg; 1377 1378 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1379} 1380 1381/* 1382 * Remove an initiator by port number and initiator ID. 1383 * Returns 0 for success, 1 for failure. 1384 */ 1385int 1386ctl_remove_initiator(int32_t targ_port, uint32_t iid) 1387{ 1388 struct ctl_softc *softc; 1389 1390 softc = control_softc; 1391 1392 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1393 1394 if ((targ_port < 0) 1395 || (targ_port > CTL_MAX_PORTS)) { 1396 printf("%s: invalid port number %d\n", __func__, targ_port); 1397 return (1); 1398 } 1399 if (iid > CTL_MAX_INIT_PER_PORT) { 1400 printf("%s: initiator ID %u > maximun %u!\n", 1401 __func__, iid, CTL_MAX_INIT_PER_PORT); 1402 return (1); 1403 } 1404 1405 mtx_lock(&softc->ctl_lock); 1406 1407 softc->wwpn_iid[targ_port][iid].in_use = 0; 1408 1409 mtx_unlock(&softc->ctl_lock); 1410 1411 return (0); 1412} 1413 1414/* 1415 * Add an initiator to the initiator map. 1416 * Returns 0 for success, 1 for failure. 1417 */ 1418int 1419ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid) 1420{ 1421 struct ctl_softc *softc; 1422 int retval; 1423 1424 softc = control_softc; 1425 1426 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1427 1428 retval = 0; 1429 1430 if ((targ_port < 0) 1431 || (targ_port > CTL_MAX_PORTS)) { 1432 printf("%s: invalid port number %d\n", __func__, targ_port); 1433 return (1); 1434 } 1435 if (iid > CTL_MAX_INIT_PER_PORT) { 1436 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n", 1437 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1438 return (1); 1439 } 1440 1441 mtx_lock(&softc->ctl_lock); 1442 1443 if (softc->wwpn_iid[targ_port][iid].in_use != 0) { 1444 /* 1445 * We don't treat this as an error. 1446 */ 1447 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) { 1448 printf("%s: port %d iid %u WWPN %#jx arrived again?\n", 1449 __func__, targ_port, iid, (uintmax_t)wwpn); 1450 goto bailout; 1451 } 1452 1453 /* 1454 * This is an error, but what do we do about it? The 1455 * driver is telling us we have a new WWPN for this 1456 * initiator ID, so we pretty much need to use it. 1457 */ 1458 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is " 1459 "still at that address\n", __func__, targ_port, iid, 1460 (uintmax_t)wwpn, 1461 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn); 1462 1463 /* 1464 * XXX KDM clear have_ca and ua_pending on each LUN for 1465 * this initiator. 1466 */ 1467 } 1468 softc->wwpn_iid[targ_port][iid].in_use = 1; 1469 softc->wwpn_iid[targ_port][iid].iid = iid; 1470 softc->wwpn_iid[targ_port][iid].wwpn = wwpn; 1471 softc->wwpn_iid[targ_port][iid].port = targ_port; 1472 1473bailout: 1474 1475 mtx_unlock(&softc->ctl_lock); 1476 1477 return (retval); 1478} 1479 1480/* 1481 * XXX KDM should we pretend to do something in the target/lun 1482 * enable/disable functions? 1483 */ 1484static int 1485ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id) 1486{ 1487 return (0); 1488} 1489 1490static int 1491ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id) 1492{ 1493 return (0); 1494} 1495 1496static int 1497ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1498{ 1499 return (0); 1500} 1501 1502static int 1503ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1504{ 1505 return (0); 1506} 1507 1508/* 1509 * Data movement routine for the CTL ioctl frontend port. 1510 */ 1511static int 1512ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1513{ 1514 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1515 struct ctl_sg_entry ext_entry, kern_entry; 1516 int ext_sglen, ext_sg_entries, kern_sg_entries; 1517 int ext_sg_start, ext_offset; 1518 int len_to_copy, len_copied; 1519 int kern_watermark, ext_watermark; 1520 int ext_sglist_malloced; 1521 int i, j; 1522 1523 ext_sglist_malloced = 0; 1524 ext_sg_start = 0; 1525 ext_offset = 0; 1526 1527 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1528 1529 /* 1530 * If this flag is set, fake the data transfer. 1531 */ 1532 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1533 ctsio->ext_data_filled = ctsio->ext_data_len; 1534 goto bailout; 1535 } 1536 1537 /* 1538 * To simplify things here, if we have a single buffer, stick it in 1539 * a S/G entry and just make it a single entry S/G list. 1540 */ 1541 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1542 int len_seen; 1543 1544 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1545 1546 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1547 M_WAITOK); 1548 ext_sglist_malloced = 1; 1549 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1550 ext_sglen) != 0) { 1551 ctl_set_internal_failure(ctsio, 1552 /*sks_valid*/ 0, 1553 /*retry_count*/ 0); 1554 goto bailout; 1555 } 1556 ext_sg_entries = ctsio->ext_sg_entries; 1557 len_seen = 0; 1558 for (i = 0; i < ext_sg_entries; i++) { 1559 if ((len_seen + ext_sglist[i].len) >= 1560 ctsio->ext_data_filled) { 1561 ext_sg_start = i; 1562 ext_offset = ctsio->ext_data_filled - len_seen; 1563 break; 1564 } 1565 len_seen += ext_sglist[i].len; 1566 } 1567 } else { 1568 ext_sglist = &ext_entry; 1569 ext_sglist->addr = ctsio->ext_data_ptr; 1570 ext_sglist->len = ctsio->ext_data_len; 1571 ext_sg_entries = 1; 1572 ext_sg_start = 0; 1573 ext_offset = ctsio->ext_data_filled; 1574 } 1575 1576 if (ctsio->kern_sg_entries > 0) { 1577 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1578 kern_sg_entries = ctsio->kern_sg_entries; 1579 } else { 1580 kern_sglist = &kern_entry; 1581 kern_sglist->addr = ctsio->kern_data_ptr; 1582 kern_sglist->len = ctsio->kern_data_len; 1583 kern_sg_entries = 1; 1584 } 1585 1586 1587 kern_watermark = 0; 1588 ext_watermark = ext_offset; 1589 len_copied = 0; 1590 for (i = ext_sg_start, j = 0; 1591 i < ext_sg_entries && j < kern_sg_entries;) { 1592 uint8_t *ext_ptr, *kern_ptr; 1593 1594 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1595 kern_sglist[j].len - kern_watermark); 1596 1597 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1598 ext_ptr = ext_ptr + ext_watermark; 1599 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1600 /* 1601 * XXX KDM fix this! 1602 */ 1603 panic("need to implement bus address support"); 1604#if 0 1605 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1606#endif 1607 } else 1608 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1609 kern_ptr = kern_ptr + kern_watermark; 1610 1611 kern_watermark += len_to_copy; 1612 ext_watermark += len_to_copy; 1613 1614 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1615 CTL_FLAG_DATA_IN) { 1616 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1617 "bytes to user\n", len_to_copy)); 1618 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1619 "to %p\n", kern_ptr, ext_ptr)); 1620 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1621 ctl_set_internal_failure(ctsio, 1622 /*sks_valid*/ 0, 1623 /*retry_count*/ 0); 1624 goto bailout; 1625 } 1626 } else { 1627 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1628 "bytes from user\n", len_to_copy)); 1629 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1630 "to %p\n", ext_ptr, kern_ptr)); 1631 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1632 ctl_set_internal_failure(ctsio, 1633 /*sks_valid*/ 0, 1634 /*retry_count*/0); 1635 goto bailout; 1636 } 1637 } 1638 1639 len_copied += len_to_copy; 1640 1641 if (ext_sglist[i].len == ext_watermark) { 1642 i++; 1643 ext_watermark = 0; 1644 } 1645 1646 if (kern_sglist[j].len == kern_watermark) { 1647 j++; 1648 kern_watermark = 0; 1649 } 1650 } 1651 1652 ctsio->ext_data_filled += len_copied; 1653 1654 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1655 "kern_sg_entries: %d\n", ext_sg_entries, 1656 kern_sg_entries)); 1657 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1658 "kern_data_len = %d\n", ctsio->ext_data_len, 1659 ctsio->kern_data_len)); 1660 1661 1662 /* XXX KDM set residual?? */ 1663bailout: 1664 1665 if (ext_sglist_malloced != 0) 1666 free(ext_sglist, M_CTL); 1667 1668 return (CTL_RETVAL_COMPLETE); 1669} 1670 1671/* 1672 * Serialize a command that went down the "wrong" side, and so was sent to 1673 * this controller for execution. The logic is a little different than the 1674 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1675 * sent back to the other side, but in the success case, we execute the 1676 * command on this side (XFER mode) or tell the other side to execute it 1677 * (SER_ONLY mode). 1678 */ 1679static int 1680ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock) 1681{ 1682 struct ctl_softc *ctl_softc; 1683 union ctl_ha_msg msg_info; 1684 struct ctl_lun *lun; 1685 int retval = 0; 1686 uint32_t targ_lun; 1687 1688 ctl_softc = control_softc; 1689 if (have_lock == 0) 1690 mtx_lock(&ctl_softc->ctl_lock); 1691 1692 targ_lun = ctsio->io_hdr.nexus.targ_lun; 1693 if (ctsio->io_hdr.nexus.lun_map_fn != NULL) 1694 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun); 1695 lun = ctl_softc->ctl_luns[targ_lun]; 1696 if (lun==NULL) 1697 { 1698 /* 1699 * Why isn't LUN defined? The other side wouldn't 1700 * send a cmd if the LUN is undefined. 1701 */ 1702 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1703 1704 /* "Logical unit not supported" */ 1705 ctl_set_sense_data(&msg_info.scsi.sense_data, 1706 lun, 1707 /*sense_format*/SSD_TYPE_NONE, 1708 /*current_error*/ 1, 1709 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1710 /*asc*/ 0x25, 1711 /*ascq*/ 0x00, 1712 SSD_ELEM_NONE); 1713 1714 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1715 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1716 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1717 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1718 msg_info.hdr.serializing_sc = NULL; 1719 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1720 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1721 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1722 } 1723 if (have_lock == 0) 1724 mtx_unlock(&ctl_softc->ctl_lock); 1725 return(1); 1726 1727 } 1728 1729 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1730 1731 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1732 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1733 ooa_links))) { 1734 case CTL_ACTION_BLOCK: 1735 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1736 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1737 blocked_links); 1738 break; 1739 case CTL_ACTION_PASS: 1740 case CTL_ACTION_SKIP: 1741 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1742 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1743 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 1744 &ctsio->io_hdr, links); 1745 } else { 1746 1747 /* send msg back to other side */ 1748 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1749 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1750 msg_info.hdr.msg_type = CTL_MSG_R2R; 1751#if 0 1752 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1753#endif 1754 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1755 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1756 } 1757 } 1758 break; 1759 case CTL_ACTION_OVERLAP: 1760 /* OVERLAPPED COMMANDS ATTEMPTED */ 1761 ctl_set_sense_data(&msg_info.scsi.sense_data, 1762 lun, 1763 /*sense_format*/SSD_TYPE_NONE, 1764 /*current_error*/ 1, 1765 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1766 /*asc*/ 0x4E, 1767 /*ascq*/ 0x00, 1768 SSD_ELEM_NONE); 1769 1770 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1771 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1772 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1773 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1774 msg_info.hdr.serializing_sc = NULL; 1775 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1776#if 0 1777 printf("BAD JUJU:Major Bummer Overlap\n"); 1778#endif 1779 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1780 retval = 1; 1781 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1782 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1783 } 1784 break; 1785 case CTL_ACTION_OVERLAP_TAG: 1786 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1787 ctl_set_sense_data(&msg_info.scsi.sense_data, 1788 lun, 1789 /*sense_format*/SSD_TYPE_NONE, 1790 /*current_error*/ 1, 1791 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1792 /*asc*/ 0x4D, 1793 /*ascq*/ ctsio->tag_num & 0xff, 1794 SSD_ELEM_NONE); 1795 1796 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1797 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1798 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1799 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1800 msg_info.hdr.serializing_sc = NULL; 1801 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1802#if 0 1803 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1804#endif 1805 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1806 retval = 1; 1807 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1808 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1809 } 1810 break; 1811 case CTL_ACTION_ERROR: 1812 default: 1813 /* "Internal target failure" */ 1814 ctl_set_sense_data(&msg_info.scsi.sense_data, 1815 lun, 1816 /*sense_format*/SSD_TYPE_NONE, 1817 /*current_error*/ 1, 1818 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1819 /*asc*/ 0x44, 1820 /*ascq*/ 0x00, 1821 SSD_ELEM_NONE); 1822 1823 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1824 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1825 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1826 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1827 msg_info.hdr.serializing_sc = NULL; 1828 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1829#if 0 1830 printf("BAD JUJU:Major Bummer HW Error\n"); 1831#endif 1832 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1833 retval = 1; 1834 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1835 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1836 } 1837 break; 1838 } 1839 if (have_lock == 0) 1840 mtx_unlock(&ctl_softc->ctl_lock); 1841 return (retval); 1842} 1843 1844static int 1845ctl_ioctl_submit_wait(union ctl_io *io) 1846{ 1847 struct ctl_fe_ioctl_params params; 1848 ctl_fe_ioctl_state last_state; 1849 int done, retval; 1850 1851 retval = 0; 1852 1853 bzero(¶ms, sizeof(params)); 1854 1855 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1856 cv_init(¶ms.sem, "ctlioccv"); 1857 params.state = CTL_IOCTL_INPROG; 1858 last_state = params.state; 1859 1860 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1861 1862 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1863 1864 /* This shouldn't happen */ 1865 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1866 return (retval); 1867 1868 done = 0; 1869 1870 do { 1871 mtx_lock(¶ms.ioctl_mtx); 1872 /* 1873 * Check the state here, and don't sleep if the state has 1874 * already changed (i.e. wakeup has already occured, but we 1875 * weren't waiting yet). 1876 */ 1877 if (params.state == last_state) { 1878 /* XXX KDM cv_wait_sig instead? */ 1879 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1880 } 1881 last_state = params.state; 1882 1883 switch (params.state) { 1884 case CTL_IOCTL_INPROG: 1885 /* Why did we wake up? */ 1886 /* XXX KDM error here? */ 1887 mtx_unlock(¶ms.ioctl_mtx); 1888 break; 1889 case CTL_IOCTL_DATAMOVE: 1890 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1891 1892 /* 1893 * change last_state back to INPROG to avoid 1894 * deadlock on subsequent data moves. 1895 */ 1896 params.state = last_state = CTL_IOCTL_INPROG; 1897 1898 mtx_unlock(¶ms.ioctl_mtx); 1899 ctl_ioctl_do_datamove(&io->scsiio); 1900 /* 1901 * Note that in some cases, most notably writes, 1902 * this will queue the I/O and call us back later. 1903 * In other cases, generally reads, this routine 1904 * will immediately call back and wake us up, 1905 * probably using our own context. 1906 */ 1907 io->scsiio.be_move_done(io); 1908 break; 1909 case CTL_IOCTL_DONE: 1910 mtx_unlock(¶ms.ioctl_mtx); 1911 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1912 done = 1; 1913 break; 1914 default: 1915 mtx_unlock(¶ms.ioctl_mtx); 1916 /* XXX KDM error here? */ 1917 break; 1918 } 1919 } while (done == 0); 1920 1921 mtx_destroy(¶ms.ioctl_mtx); 1922 cv_destroy(¶ms.sem); 1923 1924 return (CTL_RETVAL_COMPLETE); 1925} 1926 1927static void 1928ctl_ioctl_datamove(union ctl_io *io) 1929{ 1930 struct ctl_fe_ioctl_params *params; 1931 1932 params = (struct ctl_fe_ioctl_params *) 1933 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1934 1935 mtx_lock(¶ms->ioctl_mtx); 1936 params->state = CTL_IOCTL_DATAMOVE; 1937 cv_broadcast(¶ms->sem); 1938 mtx_unlock(¶ms->ioctl_mtx); 1939} 1940 1941static void 1942ctl_ioctl_done(union ctl_io *io) 1943{ 1944 struct ctl_fe_ioctl_params *params; 1945 1946 params = (struct ctl_fe_ioctl_params *) 1947 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1948 1949 mtx_lock(¶ms->ioctl_mtx); 1950 params->state = CTL_IOCTL_DONE; 1951 cv_broadcast(¶ms->sem); 1952 mtx_unlock(¶ms->ioctl_mtx); 1953} 1954 1955static void 1956ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 1957{ 1958 struct ctl_fe_ioctl_startstop_info *sd_info; 1959 1960 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 1961 1962 sd_info->hs_info.status = metatask->status; 1963 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 1964 sd_info->hs_info.luns_complete = 1965 metatask->taskinfo.startstop.luns_complete; 1966 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 1967 1968 cv_broadcast(&sd_info->sem); 1969} 1970 1971static void 1972ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 1973{ 1974 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 1975 1976 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 1977 1978 mtx_lock(fe_bbr_info->lock); 1979 fe_bbr_info->bbr_info->status = metatask->status; 1980 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 1981 fe_bbr_info->wakeup_done = 1; 1982 mtx_unlock(fe_bbr_info->lock); 1983 1984 cv_broadcast(&fe_bbr_info->sem); 1985} 1986 1987/* 1988 * Returns 0 for success, errno for failure. 1989 */ 1990static int 1991ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1992 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1993{ 1994 union ctl_io *io; 1995 int retval; 1996 1997 retval = 0; 1998 1999 mtx_assert(&control_softc->ctl_lock, MA_OWNED); 2000 2001 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2002 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2003 ooa_links)) { 2004 struct ctl_ooa_entry *entry; 2005 2006 /* 2007 * If we've got more than we can fit, just count the 2008 * remaining entries. 2009 */ 2010 if (*cur_fill_num >= ooa_hdr->alloc_num) 2011 continue; 2012 2013 entry = &kern_entries[*cur_fill_num]; 2014 2015 entry->tag_num = io->scsiio.tag_num; 2016 entry->lun_num = lun->lun; 2017#ifdef CTL_TIME_IO 2018 entry->start_bt = io->io_hdr.start_bt; 2019#endif 2020 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2021 entry->cdb_len = io->scsiio.cdb_len; 2022 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2023 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2024 2025 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2026 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2027 2028 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2029 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2030 2031 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2032 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2033 2034 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2035 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2036 } 2037 2038 return (retval); 2039} 2040 2041static void * 2042ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2043 size_t error_str_len) 2044{ 2045 void *kptr; 2046 2047 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2048 2049 if (copyin(user_addr, kptr, len) != 0) { 2050 snprintf(error_str, error_str_len, "Error copying %d bytes " 2051 "from user address %p to kernel address %p", len, 2052 user_addr, kptr); 2053 free(kptr, M_CTL); 2054 return (NULL); 2055 } 2056 2057 return (kptr); 2058} 2059 2060static void 2061ctl_free_args(int num_be_args, struct ctl_be_arg *be_args) 2062{ 2063 int i; 2064 2065 if (be_args == NULL) 2066 return; 2067 2068 for (i = 0; i < num_be_args; i++) { 2069 free(be_args[i].kname, M_CTL); 2070 free(be_args[i].kvalue, M_CTL); 2071 } 2072 2073 free(be_args, M_CTL); 2074} 2075 2076static struct ctl_be_arg * 2077ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args, 2078 char *error_str, size_t error_str_len) 2079{ 2080 struct ctl_be_arg *args; 2081 int i; 2082 2083 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args), 2084 error_str, error_str_len); 2085 2086 if (args == NULL) 2087 goto bailout; 2088 2089 for (i = 0; i < num_be_args; i++) { 2090 args[i].kname = NULL; 2091 args[i].kvalue = NULL; 2092 } 2093 2094 for (i = 0; i < num_be_args; i++) { 2095 uint8_t *tmpptr; 2096 2097 args[i].kname = ctl_copyin_alloc(args[i].name, 2098 args[i].namelen, error_str, error_str_len); 2099 if (args[i].kname == NULL) 2100 goto bailout; 2101 2102 if (args[i].kname[args[i].namelen - 1] != '\0') { 2103 snprintf(error_str, error_str_len, "Argument %d " 2104 "name is not NUL-terminated", i); 2105 goto bailout; 2106 } 2107 2108 args[i].kvalue = NULL; 2109 2110 tmpptr = ctl_copyin_alloc(args[i].value, 2111 args[i].vallen, error_str, error_str_len); 2112 if (tmpptr == NULL) 2113 goto bailout; 2114 2115 args[i].kvalue = tmpptr; 2116 2117 if ((args[i].flags & CTL_BEARG_ASCII) 2118 && (tmpptr[args[i].vallen - 1] != '\0')) { 2119 snprintf(error_str, error_str_len, "Argument %d " 2120 "value is not NUL-terminated", i); 2121 goto bailout; 2122 } 2123 } 2124 2125 return (args); 2126bailout: 2127 2128 ctl_free_args(num_be_args, args); 2129 2130 return (NULL); 2131} 2132 2133/* 2134 * Escape characters that are illegal or not recommended in XML. 2135 */ 2136int 2137ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2138{ 2139 int retval; 2140 2141 retval = 0; 2142 2143 for (; *str; str++) { 2144 switch (*str) { 2145 case '&': 2146 retval = sbuf_printf(sb, "&"); 2147 break; 2148 case '>': 2149 retval = sbuf_printf(sb, ">"); 2150 break; 2151 case '<': 2152 retval = sbuf_printf(sb, "<"); 2153 break; 2154 default: 2155 retval = sbuf_putc(sb, *str); 2156 break; 2157 } 2158 2159 if (retval != 0) 2160 break; 2161 2162 } 2163 2164 return (retval); 2165} 2166 2167static int 2168ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2169 struct thread *td) 2170{ 2171 struct ctl_softc *softc; 2172 int retval; 2173 2174 softc = control_softc; 2175 2176 retval = 0; 2177 2178 switch (cmd) { 2179 case CTL_IO: { 2180 union ctl_io *io; 2181 void *pool_tmp; 2182 2183 /* 2184 * If we haven't been "enabled", don't allow any SCSI I/O 2185 * to this FETD. 2186 */ 2187 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2188 retval = -EPERM; 2189 break; 2190 } 2191 2192 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref); 2193 if (io == NULL) { 2194 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2195 retval = -ENOSPC; 2196 break; 2197 } 2198 2199 /* 2200 * Need to save the pool reference so it doesn't get 2201 * spammed by the user's ctl_io. 2202 */ 2203 pool_tmp = io->io_hdr.pool; 2204 2205 memcpy(io, (void *)addr, sizeof(*io)); 2206 2207 io->io_hdr.pool = pool_tmp; 2208 /* 2209 * No status yet, so make sure the status is set properly. 2210 */ 2211 io->io_hdr.status = CTL_STATUS_NONE; 2212 2213 /* 2214 * The user sets the initiator ID, target and LUN IDs. 2215 */ 2216 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port; 2217 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2218 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2219 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2220 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2221 2222 retval = ctl_ioctl_submit_wait(io); 2223 2224 if (retval != 0) { 2225 ctl_free_io(io); 2226 break; 2227 } 2228 2229 memcpy((void *)addr, io, sizeof(*io)); 2230 2231 /* return this to our pool */ 2232 ctl_free_io(io); 2233 2234 break; 2235 } 2236 case CTL_ENABLE_PORT: 2237 case CTL_DISABLE_PORT: 2238 case CTL_SET_PORT_WWNS: { 2239 struct ctl_frontend *fe; 2240 struct ctl_port_entry *entry; 2241 2242 entry = (struct ctl_port_entry *)addr; 2243 2244 mtx_lock(&softc->ctl_lock); 2245 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2246 int action, done; 2247 2248 action = 0; 2249 done = 0; 2250 2251 if ((entry->port_type == CTL_PORT_NONE) 2252 && (entry->targ_port == fe->targ_port)) { 2253 /* 2254 * If the user only wants to enable or 2255 * disable or set WWNs on a specific port, 2256 * do the operation and we're done. 2257 */ 2258 action = 1; 2259 done = 1; 2260 } else if (entry->port_type & fe->port_type) { 2261 /* 2262 * Compare the user's type mask with the 2263 * particular frontend type to see if we 2264 * have a match. 2265 */ 2266 action = 1; 2267 done = 0; 2268 2269 /* 2270 * Make sure the user isn't trying to set 2271 * WWNs on multiple ports at the same time. 2272 */ 2273 if (cmd == CTL_SET_PORT_WWNS) { 2274 printf("%s: Can't set WWNs on " 2275 "multiple ports\n", __func__); 2276 retval = EINVAL; 2277 break; 2278 } 2279 } 2280 if (action != 0) { 2281 /* 2282 * XXX KDM we have to drop the lock here, 2283 * because the online/offline operations 2284 * can potentially block. We need to 2285 * reference count the frontends so they 2286 * can't go away, 2287 */ 2288 mtx_unlock(&softc->ctl_lock); 2289 2290 if (cmd == CTL_ENABLE_PORT) { 2291 struct ctl_lun *lun; 2292 2293 STAILQ_FOREACH(lun, &softc->lun_list, 2294 links) { 2295 fe->lun_enable(fe->targ_lun_arg, 2296 lun->target, 2297 lun->lun); 2298 } 2299 2300 ctl_frontend_online(fe); 2301 } else if (cmd == CTL_DISABLE_PORT) { 2302 struct ctl_lun *lun; 2303 2304 ctl_frontend_offline(fe); 2305 2306 STAILQ_FOREACH(lun, &softc->lun_list, 2307 links) { 2308 fe->lun_disable( 2309 fe->targ_lun_arg, 2310 lun->target, 2311 lun->lun); 2312 } 2313 } 2314 2315 mtx_lock(&softc->ctl_lock); 2316 2317 if (cmd == CTL_SET_PORT_WWNS) 2318 ctl_frontend_set_wwns(fe, 2319 (entry->flags & CTL_PORT_WWNN_VALID) ? 2320 1 : 0, entry->wwnn, 2321 (entry->flags & CTL_PORT_WWPN_VALID) ? 2322 1 : 0, entry->wwpn); 2323 } 2324 if (done != 0) 2325 break; 2326 } 2327 mtx_unlock(&softc->ctl_lock); 2328 break; 2329 } 2330 case CTL_GET_PORT_LIST: { 2331 struct ctl_frontend *fe; 2332 struct ctl_port_list *list; 2333 int i; 2334 2335 list = (struct ctl_port_list *)addr; 2336 2337 if (list->alloc_len != (list->alloc_num * 2338 sizeof(struct ctl_port_entry))) { 2339 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2340 "alloc_num %u * sizeof(struct ctl_port_entry) " 2341 "%zu\n", __func__, list->alloc_len, 2342 list->alloc_num, sizeof(struct ctl_port_entry)); 2343 retval = EINVAL; 2344 break; 2345 } 2346 list->fill_len = 0; 2347 list->fill_num = 0; 2348 list->dropped_num = 0; 2349 i = 0; 2350 mtx_lock(&softc->ctl_lock); 2351 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2352 struct ctl_port_entry entry, *list_entry; 2353 2354 if (list->fill_num >= list->alloc_num) { 2355 list->dropped_num++; 2356 continue; 2357 } 2358 2359 entry.port_type = fe->port_type; 2360 strlcpy(entry.port_name, fe->port_name, 2361 sizeof(entry.port_name)); 2362 entry.targ_port = fe->targ_port; 2363 entry.physical_port = fe->physical_port; 2364 entry.virtual_port = fe->virtual_port; 2365 entry.wwnn = fe->wwnn; 2366 entry.wwpn = fe->wwpn; 2367 if (fe->status & CTL_PORT_STATUS_ONLINE) 2368 entry.online = 1; 2369 else 2370 entry.online = 0; 2371 2372 list_entry = &list->entries[i]; 2373 2374 retval = copyout(&entry, list_entry, sizeof(entry)); 2375 if (retval != 0) { 2376 printf("%s: CTL_GET_PORT_LIST: copyout " 2377 "returned %d\n", __func__, retval); 2378 break; 2379 } 2380 i++; 2381 list->fill_num++; 2382 list->fill_len += sizeof(entry); 2383 } 2384 mtx_unlock(&softc->ctl_lock); 2385 2386 /* 2387 * If this is non-zero, we had a copyout fault, so there's 2388 * probably no point in attempting to set the status inside 2389 * the structure. 2390 */ 2391 if (retval != 0) 2392 break; 2393 2394 if (list->dropped_num > 0) 2395 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2396 else 2397 list->status = CTL_PORT_LIST_OK; 2398 break; 2399 } 2400 case CTL_DUMP_OOA: { 2401 struct ctl_lun *lun; 2402 union ctl_io *io; 2403 char printbuf[128]; 2404 struct sbuf sb; 2405 2406 mtx_lock(&softc->ctl_lock); 2407 printf("Dumping OOA queues:\n"); 2408 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2409 for (io = (union ctl_io *)TAILQ_FIRST( 2410 &lun->ooa_queue); io != NULL; 2411 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2412 ooa_links)) { 2413 sbuf_new(&sb, printbuf, sizeof(printbuf), 2414 SBUF_FIXEDLEN); 2415 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2416 (intmax_t)lun->lun, 2417 io->scsiio.tag_num, 2418 (io->io_hdr.flags & 2419 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2420 (io->io_hdr.flags & 2421 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2422 (io->io_hdr.flags & 2423 CTL_FLAG_ABORT) ? " ABORT" : "", 2424 (io->io_hdr.flags & 2425 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2426 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2427 sbuf_finish(&sb); 2428 printf("%s\n", sbuf_data(&sb)); 2429 } 2430 } 2431 printf("OOA queues dump done\n"); 2432 mtx_unlock(&softc->ctl_lock); 2433 break; 2434 } 2435 case CTL_GET_OOA: { 2436 struct ctl_lun *lun; 2437 struct ctl_ooa *ooa_hdr; 2438 struct ctl_ooa_entry *entries; 2439 uint32_t cur_fill_num; 2440 2441 ooa_hdr = (struct ctl_ooa *)addr; 2442 2443 if ((ooa_hdr->alloc_len == 0) 2444 || (ooa_hdr->alloc_num == 0)) { 2445 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2446 "must be non-zero\n", __func__, 2447 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2448 retval = EINVAL; 2449 break; 2450 } 2451 2452 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2453 sizeof(struct ctl_ooa_entry))) { 2454 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2455 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2456 __func__, ooa_hdr->alloc_len, 2457 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2458 retval = EINVAL; 2459 break; 2460 } 2461 2462 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2463 if (entries == NULL) { 2464 printf("%s: could not allocate %d bytes for OOA " 2465 "dump\n", __func__, ooa_hdr->alloc_len); 2466 retval = ENOMEM; 2467 break; 2468 } 2469 2470 mtx_lock(&softc->ctl_lock); 2471 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2472 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2473 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2474 mtx_unlock(&softc->ctl_lock); 2475 free(entries, M_CTL); 2476 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2477 __func__, (uintmax_t)ooa_hdr->lun_num); 2478 retval = EINVAL; 2479 break; 2480 } 2481 2482 cur_fill_num = 0; 2483 2484 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2485 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2486 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2487 ooa_hdr, entries); 2488 if (retval != 0) 2489 break; 2490 } 2491 if (retval != 0) { 2492 mtx_unlock(&softc->ctl_lock); 2493 free(entries, M_CTL); 2494 break; 2495 } 2496 } else { 2497 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2498 2499 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2500 entries); 2501 } 2502 mtx_unlock(&softc->ctl_lock); 2503 2504 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2505 ooa_hdr->fill_len = ooa_hdr->fill_num * 2506 sizeof(struct ctl_ooa_entry); 2507 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2508 if (retval != 0) { 2509 printf("%s: error copying out %d bytes for OOA dump\n", 2510 __func__, ooa_hdr->fill_len); 2511 } 2512 2513 getbintime(&ooa_hdr->cur_bt); 2514 2515 if (cur_fill_num > ooa_hdr->alloc_num) { 2516 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2517 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2518 } else { 2519 ooa_hdr->dropped_num = 0; 2520 ooa_hdr->status = CTL_OOA_OK; 2521 } 2522 2523 free(entries, M_CTL); 2524 break; 2525 } 2526 case CTL_CHECK_OOA: { 2527 union ctl_io *io; 2528 struct ctl_lun *lun; 2529 struct ctl_ooa_info *ooa_info; 2530 2531 2532 ooa_info = (struct ctl_ooa_info *)addr; 2533 2534 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2535 ooa_info->status = CTL_OOA_INVALID_LUN; 2536 break; 2537 } 2538 mtx_lock(&softc->ctl_lock); 2539 lun = softc->ctl_luns[ooa_info->lun_id]; 2540 if (lun == NULL) { 2541 mtx_unlock(&softc->ctl_lock); 2542 ooa_info->status = CTL_OOA_INVALID_LUN; 2543 break; 2544 } 2545 2546 ooa_info->num_entries = 0; 2547 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2548 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2549 &io->io_hdr, ooa_links)) { 2550 ooa_info->num_entries++; 2551 } 2552 2553 mtx_unlock(&softc->ctl_lock); 2554 ooa_info->status = CTL_OOA_SUCCESS; 2555 2556 break; 2557 } 2558 case CTL_HARD_START: 2559 case CTL_HARD_STOP: { 2560 struct ctl_fe_ioctl_startstop_info ss_info; 2561 struct cfi_metatask *metatask; 2562 struct mtx hs_mtx; 2563 2564 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2565 2566 cv_init(&ss_info.sem, "hard start/stop cv" ); 2567 2568 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2569 if (metatask == NULL) { 2570 retval = ENOMEM; 2571 mtx_destroy(&hs_mtx); 2572 break; 2573 } 2574 2575 if (cmd == CTL_HARD_START) 2576 metatask->tasktype = CFI_TASK_STARTUP; 2577 else 2578 metatask->tasktype = CFI_TASK_SHUTDOWN; 2579 2580 metatask->callback = ctl_ioctl_hard_startstop_callback; 2581 metatask->callback_arg = &ss_info; 2582 2583 cfi_action(metatask); 2584 2585 /* Wait for the callback */ 2586 mtx_lock(&hs_mtx); 2587 cv_wait_sig(&ss_info.sem, &hs_mtx); 2588 mtx_unlock(&hs_mtx); 2589 2590 /* 2591 * All information has been copied from the metatask by the 2592 * time cv_broadcast() is called, so we free the metatask here. 2593 */ 2594 cfi_free_metatask(metatask); 2595 2596 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2597 2598 mtx_destroy(&hs_mtx); 2599 break; 2600 } 2601 case CTL_BBRREAD: { 2602 struct ctl_bbrread_info *bbr_info; 2603 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2604 struct mtx bbr_mtx; 2605 struct cfi_metatask *metatask; 2606 2607 bbr_info = (struct ctl_bbrread_info *)addr; 2608 2609 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2610 2611 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2612 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2613 2614 fe_bbr_info.bbr_info = bbr_info; 2615 fe_bbr_info.lock = &bbr_mtx; 2616 2617 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2618 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2619 2620 if (metatask == NULL) { 2621 mtx_destroy(&bbr_mtx); 2622 cv_destroy(&fe_bbr_info.sem); 2623 retval = ENOMEM; 2624 break; 2625 } 2626 metatask->tasktype = CFI_TASK_BBRREAD; 2627 metatask->callback = ctl_ioctl_bbrread_callback; 2628 metatask->callback_arg = &fe_bbr_info; 2629 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2630 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2631 metatask->taskinfo.bbrread.len = bbr_info->len; 2632 2633 cfi_action(metatask); 2634 2635 mtx_lock(&bbr_mtx); 2636 while (fe_bbr_info.wakeup_done == 0) 2637 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2638 mtx_unlock(&bbr_mtx); 2639 2640 bbr_info->status = metatask->status; 2641 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2642 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2643 memcpy(&bbr_info->sense_data, 2644 &metatask->taskinfo.bbrread.sense_data, 2645 ctl_min(sizeof(bbr_info->sense_data), 2646 sizeof(metatask->taskinfo.bbrread.sense_data))); 2647 2648 cfi_free_metatask(metatask); 2649 2650 mtx_destroy(&bbr_mtx); 2651 cv_destroy(&fe_bbr_info.sem); 2652 2653 break; 2654 } 2655 case CTL_DELAY_IO: { 2656 struct ctl_io_delay_info *delay_info; 2657#ifdef CTL_IO_DELAY 2658 struct ctl_lun *lun; 2659#endif /* CTL_IO_DELAY */ 2660 2661 delay_info = (struct ctl_io_delay_info *)addr; 2662 2663#ifdef CTL_IO_DELAY 2664 mtx_lock(&softc->ctl_lock); 2665 2666 if ((delay_info->lun_id > CTL_MAX_LUNS) 2667 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2668 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2669 } else { 2670 lun = softc->ctl_luns[delay_info->lun_id]; 2671 2672 delay_info->status = CTL_DELAY_STATUS_OK; 2673 2674 switch (delay_info->delay_type) { 2675 case CTL_DELAY_TYPE_CONT: 2676 break; 2677 case CTL_DELAY_TYPE_ONESHOT: 2678 break; 2679 default: 2680 delay_info->status = 2681 CTL_DELAY_STATUS_INVALID_TYPE; 2682 break; 2683 } 2684 2685 switch (delay_info->delay_loc) { 2686 case CTL_DELAY_LOC_DATAMOVE: 2687 lun->delay_info.datamove_type = 2688 delay_info->delay_type; 2689 lun->delay_info.datamove_delay = 2690 delay_info->delay_secs; 2691 break; 2692 case CTL_DELAY_LOC_DONE: 2693 lun->delay_info.done_type = 2694 delay_info->delay_type; 2695 lun->delay_info.done_delay = 2696 delay_info->delay_secs; 2697 break; 2698 default: 2699 delay_info->status = 2700 CTL_DELAY_STATUS_INVALID_LOC; 2701 break; 2702 } 2703 } 2704 2705 mtx_unlock(&softc->ctl_lock); 2706#else 2707 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2708#endif /* CTL_IO_DELAY */ 2709 break; 2710 } 2711 case CTL_REALSYNC_SET: { 2712 int *syncstate; 2713 2714 syncstate = (int *)addr; 2715 2716 mtx_lock(&softc->ctl_lock); 2717 switch (*syncstate) { 2718 case 0: 2719 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2720 break; 2721 case 1: 2722 softc->flags |= CTL_FLAG_REAL_SYNC; 2723 break; 2724 default: 2725 retval = -EINVAL; 2726 break; 2727 } 2728 mtx_unlock(&softc->ctl_lock); 2729 break; 2730 } 2731 case CTL_REALSYNC_GET: { 2732 int *syncstate; 2733 2734 syncstate = (int*)addr; 2735 2736 mtx_lock(&softc->ctl_lock); 2737 if (softc->flags & CTL_FLAG_REAL_SYNC) 2738 *syncstate = 1; 2739 else 2740 *syncstate = 0; 2741 mtx_unlock(&softc->ctl_lock); 2742 2743 break; 2744 } 2745 case CTL_SETSYNC: 2746 case CTL_GETSYNC: { 2747 struct ctl_sync_info *sync_info; 2748 struct ctl_lun *lun; 2749 2750 sync_info = (struct ctl_sync_info *)addr; 2751 2752 mtx_lock(&softc->ctl_lock); 2753 lun = softc->ctl_luns[sync_info->lun_id]; 2754 if (lun == NULL) { 2755 mtx_unlock(&softc->ctl_lock); 2756 sync_info->status = CTL_GS_SYNC_NO_LUN; 2757 } 2758 /* 2759 * Get or set the sync interval. We're not bounds checking 2760 * in the set case, hopefully the user won't do something 2761 * silly. 2762 */ 2763 if (cmd == CTL_GETSYNC) 2764 sync_info->sync_interval = lun->sync_interval; 2765 else 2766 lun->sync_interval = sync_info->sync_interval; 2767 2768 mtx_unlock(&softc->ctl_lock); 2769 2770 sync_info->status = CTL_GS_SYNC_OK; 2771 2772 break; 2773 } 2774 case CTL_GETSTATS: { 2775 struct ctl_stats *stats; 2776 struct ctl_lun *lun; 2777 int i; 2778 2779 stats = (struct ctl_stats *)addr; 2780 2781 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2782 stats->alloc_len) { 2783 stats->status = CTL_SS_NEED_MORE_SPACE; 2784 stats->num_luns = softc->num_luns; 2785 break; 2786 } 2787 /* 2788 * XXX KDM no locking here. If the LUN list changes, 2789 * things can blow up. 2790 */ 2791 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2792 i++, lun = STAILQ_NEXT(lun, links)) { 2793 retval = copyout(&lun->stats, &stats->lun_stats[i], 2794 sizeof(lun->stats)); 2795 if (retval != 0) 2796 break; 2797 } 2798 stats->num_luns = softc->num_luns; 2799 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2800 softc->num_luns; 2801 stats->status = CTL_SS_OK; 2802#ifdef CTL_TIME_IO 2803 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2804#else 2805 stats->flags = CTL_STATS_FLAG_NONE; 2806#endif 2807 getnanouptime(&stats->timestamp); 2808 break; 2809 } 2810 case CTL_ERROR_INJECT: { 2811 struct ctl_error_desc *err_desc, *new_err_desc; 2812 struct ctl_lun *lun; 2813 2814 err_desc = (struct ctl_error_desc *)addr; 2815 2816 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2817 M_WAITOK | M_ZERO); 2818 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2819 2820 mtx_lock(&softc->ctl_lock); 2821 lun = softc->ctl_luns[err_desc->lun_id]; 2822 if (lun == NULL) { 2823 mtx_unlock(&softc->ctl_lock); 2824 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2825 __func__, (uintmax_t)err_desc->lun_id); 2826 retval = EINVAL; 2827 break; 2828 } 2829 2830 /* 2831 * We could do some checking here to verify the validity 2832 * of the request, but given the complexity of error 2833 * injection requests, the checking logic would be fairly 2834 * complex. 2835 * 2836 * For now, if the request is invalid, it just won't get 2837 * executed and might get deleted. 2838 */ 2839 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2840 2841 /* 2842 * XXX KDM check to make sure the serial number is unique, 2843 * in case we somehow manage to wrap. That shouldn't 2844 * happen for a very long time, but it's the right thing to 2845 * do. 2846 */ 2847 new_err_desc->serial = lun->error_serial; 2848 err_desc->serial = lun->error_serial; 2849 lun->error_serial++; 2850 2851 mtx_unlock(&softc->ctl_lock); 2852 break; 2853 } 2854 case CTL_ERROR_INJECT_DELETE: { 2855 struct ctl_error_desc *delete_desc, *desc, *desc2; 2856 struct ctl_lun *lun; 2857 int delete_done; 2858 2859 delete_desc = (struct ctl_error_desc *)addr; 2860 delete_done = 0; 2861 2862 mtx_lock(&softc->ctl_lock); 2863 lun = softc->ctl_luns[delete_desc->lun_id]; 2864 if (lun == NULL) { 2865 mtx_unlock(&softc->ctl_lock); 2866 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2867 __func__, (uintmax_t)delete_desc->lun_id); 2868 retval = EINVAL; 2869 break; 2870 } 2871 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2872 if (desc->serial != delete_desc->serial) 2873 continue; 2874 2875 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2876 links); 2877 free(desc, M_CTL); 2878 delete_done = 1; 2879 } 2880 mtx_unlock(&softc->ctl_lock); 2881 if (delete_done == 0) { 2882 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2883 "error serial %ju on LUN %u\n", __func__, 2884 delete_desc->serial, delete_desc->lun_id); 2885 retval = EINVAL; 2886 break; 2887 } 2888 break; 2889 } 2890 case CTL_DUMP_STRUCTS: { 2891 int i, j, k; 2892 struct ctl_frontend *fe; 2893 2894 printf("CTL IID to WWPN map start:\n"); 2895 for (i = 0; i < CTL_MAX_PORTS; i++) { 2896 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2897 if (softc->wwpn_iid[i][j].in_use == 0) 2898 continue; 2899 2900 printf("port %d iid %u WWPN %#jx\n", 2901 softc->wwpn_iid[i][j].port, 2902 softc->wwpn_iid[i][j].iid, 2903 (uintmax_t)softc->wwpn_iid[i][j].wwpn); 2904 } 2905 } 2906 printf("CTL IID to WWPN map end\n"); 2907 printf("CTL Persistent Reservation information start:\n"); 2908 for (i = 0; i < CTL_MAX_LUNS; i++) { 2909 struct ctl_lun *lun; 2910 2911 lun = softc->ctl_luns[i]; 2912 2913 if ((lun == NULL) 2914 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2915 continue; 2916 2917 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2918 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2919 if (lun->per_res[j+k].registered == 0) 2920 continue; 2921 printf("LUN %d port %d iid %d key " 2922 "%#jx\n", i, j, k, 2923 (uintmax_t)scsi_8btou64( 2924 lun->per_res[j+k].res_key.key)); 2925 } 2926 } 2927 } 2928 printf("CTL Persistent Reservation information end\n"); 2929 printf("CTL Frontends:\n"); 2930 /* 2931 * XXX KDM calling this without a lock. We'd likely want 2932 * to drop the lock before calling the frontend's dump 2933 * routine anyway. 2934 */ 2935 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2936 printf("Frontend %s Type %u pport %d vport %d WWNN " 2937 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type, 2938 fe->physical_port, fe->virtual_port, 2939 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn); 2940 2941 /* 2942 * Frontends are not required to support the dump 2943 * routine. 2944 */ 2945 if (fe->fe_dump == NULL) 2946 continue; 2947 2948 fe->fe_dump(); 2949 } 2950 printf("CTL Frontend information end\n"); 2951 break; 2952 } 2953 case CTL_LUN_REQ: { 2954 struct ctl_lun_req *lun_req; 2955 struct ctl_backend_driver *backend; 2956 2957 lun_req = (struct ctl_lun_req *)addr; 2958 2959 backend = ctl_backend_find(lun_req->backend); 2960 if (backend == NULL) { 2961 lun_req->status = CTL_LUN_ERROR; 2962 snprintf(lun_req->error_str, 2963 sizeof(lun_req->error_str), 2964 "Backend \"%s\" not found.", 2965 lun_req->backend); 2966 break; 2967 } 2968 if (lun_req->num_be_args > 0) { 2969 lun_req->kern_be_args = ctl_copyin_args( 2970 lun_req->num_be_args, 2971 lun_req->be_args, 2972 lun_req->error_str, 2973 sizeof(lun_req->error_str)); 2974 if (lun_req->kern_be_args == NULL) { 2975 lun_req->status = CTL_LUN_ERROR; 2976 break; 2977 } 2978 } 2979 2980 retval = backend->ioctl(dev, cmd, addr, flag, td); 2981 2982 if (lun_req->num_be_args > 0) { 2983 ctl_free_args(lun_req->num_be_args, 2984 lun_req->kern_be_args); 2985 } 2986 break; 2987 } 2988 case CTL_LUN_LIST: { 2989 struct sbuf *sb; 2990 struct ctl_lun *lun; 2991 struct ctl_lun_list *list; 2992 struct ctl_be_lun_option *opt; 2993 2994 list = (struct ctl_lun_list *)addr; 2995 2996 /* 2997 * Allocate a fixed length sbuf here, based on the length 2998 * of the user's buffer. We could allocate an auto-extending 2999 * buffer, and then tell the user how much larger our 3000 * amount of data is than his buffer, but that presents 3001 * some problems: 3002 * 3003 * 1. The sbuf(9) routines use a blocking malloc, and so 3004 * we can't hold a lock while calling them with an 3005 * auto-extending buffer. 3006 * 3007 * 2. There is not currently a LUN reference counting 3008 * mechanism, outside of outstanding transactions on 3009 * the LUN's OOA queue. So a LUN could go away on us 3010 * while we're getting the LUN number, backend-specific 3011 * information, etc. Thus, given the way things 3012 * currently work, we need to hold the CTL lock while 3013 * grabbing LUN information. 3014 * 3015 * So, from the user's standpoint, the best thing to do is 3016 * allocate what he thinks is a reasonable buffer length, 3017 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3018 * double the buffer length and try again. (And repeat 3019 * that until he succeeds.) 3020 */ 3021 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3022 if (sb == NULL) { 3023 list->status = CTL_LUN_LIST_ERROR; 3024 snprintf(list->error_str, sizeof(list->error_str), 3025 "Unable to allocate %d bytes for LUN list", 3026 list->alloc_len); 3027 break; 3028 } 3029 3030 sbuf_printf(sb, "<ctllunlist>\n"); 3031 3032 mtx_lock(&softc->ctl_lock); 3033 3034 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3035 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3036 (uintmax_t)lun->lun); 3037 3038 /* 3039 * Bail out as soon as we see that we've overfilled 3040 * the buffer. 3041 */ 3042 if (retval != 0) 3043 break; 3044 3045 retval = sbuf_printf(sb, "<backend_type>%s" 3046 "</backend_type>\n", 3047 (lun->backend == NULL) ? "none" : 3048 lun->backend->name); 3049 3050 if (retval != 0) 3051 break; 3052 3053 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n", 3054 lun->be_lun->lun_type); 3055 3056 if (retval != 0) 3057 break; 3058 3059 if (lun->backend == NULL) { 3060 retval = sbuf_printf(sb, "</lun>\n"); 3061 if (retval != 0) 3062 break; 3063 continue; 3064 } 3065 3066 retval = sbuf_printf(sb, "<size>%ju</size>\n", 3067 (lun->be_lun->maxlba > 0) ? 3068 lun->be_lun->maxlba + 1 : 0); 3069 3070 if (retval != 0) 3071 break; 3072 3073 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n", 3074 lun->be_lun->blocksize); 3075 3076 if (retval != 0) 3077 break; 3078 3079 retval = sbuf_printf(sb, "<serial_number>"); 3080 3081 if (retval != 0) 3082 break; 3083 3084 retval = ctl_sbuf_printf_esc(sb, 3085 lun->be_lun->serial_num); 3086 3087 if (retval != 0) 3088 break; 3089 3090 retval = sbuf_printf(sb, "</serial_number>\n"); 3091 3092 if (retval != 0) 3093 break; 3094 3095 retval = sbuf_printf(sb, "<device_id>"); 3096 3097 if (retval != 0) 3098 break; 3099 3100 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3101 3102 if (retval != 0) 3103 break; 3104 3105 retval = sbuf_printf(sb, "</device_id>\n"); 3106 3107 if (retval != 0) 3108 break; 3109 3110 if (lun->backend->lun_info != NULL) { 3111 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3112 if (retval != 0) 3113 break; 3114 } 3115 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3116 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name); 3117 if (retval != 0) 3118 break; 3119 } 3120 3121 retval = sbuf_printf(sb, "</lun>\n"); 3122 3123 if (retval != 0) 3124 break; 3125 } 3126 mtx_unlock(&softc->ctl_lock); 3127 3128 if ((retval != 0) 3129 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3130 retval = 0; 3131 sbuf_delete(sb); 3132 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3133 snprintf(list->error_str, sizeof(list->error_str), 3134 "Out of space, %d bytes is too small", 3135 list->alloc_len); 3136 break; 3137 } 3138 3139 sbuf_finish(sb); 3140 3141 retval = copyout(sbuf_data(sb), list->lun_xml, 3142 sbuf_len(sb) + 1); 3143 3144 list->fill_len = sbuf_len(sb) + 1; 3145 list->status = CTL_LUN_LIST_OK; 3146 sbuf_delete(sb); 3147 break; 3148 } 3149 case CTL_ISCSI: { 3150 struct ctl_iscsi *ci; 3151 struct ctl_frontend *fe; 3152 3153 ci = (struct ctl_iscsi *)addr; 3154 3155 mtx_lock(&softc->ctl_lock); 3156 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3157 if (strcmp(fe->port_name, "iscsi") == 0) 3158 break; 3159 } 3160 mtx_unlock(&softc->ctl_lock); 3161 3162 if (fe == NULL) { 3163 ci->status = CTL_ISCSI_ERROR; 3164 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found."); 3165 break; 3166 } 3167 3168 retval = fe->ioctl(dev, cmd, addr, flag, td); 3169 break; 3170 } 3171 default: { 3172 /* XXX KDM should we fix this? */ 3173#if 0 3174 struct ctl_backend_driver *backend; 3175 unsigned int type; 3176 int found; 3177 3178 found = 0; 3179 3180 /* 3181 * We encode the backend type as the ioctl type for backend 3182 * ioctls. So parse it out here, and then search for a 3183 * backend of this type. 3184 */ 3185 type = _IOC_TYPE(cmd); 3186 3187 STAILQ_FOREACH(backend, &softc->be_list, links) { 3188 if (backend->type == type) { 3189 found = 1; 3190 break; 3191 } 3192 } 3193 if (found == 0) { 3194 printf("ctl: unknown ioctl command %#lx or backend " 3195 "%d\n", cmd, type); 3196 retval = -EINVAL; 3197 break; 3198 } 3199 retval = backend->ioctl(dev, cmd, addr, flag, td); 3200#endif 3201 retval = ENOTTY; 3202 break; 3203 } 3204 } 3205 return (retval); 3206} 3207 3208uint32_t 3209ctl_get_initindex(struct ctl_nexus *nexus) 3210{ 3211 if (nexus->targ_port < CTL_MAX_PORTS) 3212 return (nexus->initid.id + 3213 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3214 else 3215 return (nexus->initid.id + 3216 ((nexus->targ_port - CTL_MAX_PORTS) * 3217 CTL_MAX_INIT_PER_PORT)); 3218} 3219 3220uint32_t 3221ctl_get_resindex(struct ctl_nexus *nexus) 3222{ 3223 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3224} 3225 3226uint32_t 3227ctl_port_idx(int port_num) 3228{ 3229 if (port_num < CTL_MAX_PORTS) 3230 return(port_num); 3231 else 3232 return(port_num - CTL_MAX_PORTS); 3233} 3234 3235/* 3236 * Note: This only works for bitmask sizes that are at least 32 bits, and 3237 * that are a power of 2. 3238 */ 3239int 3240ctl_ffz(uint32_t *mask, uint32_t size) 3241{ 3242 uint32_t num_chunks, num_pieces; 3243 int i, j; 3244 3245 num_chunks = (size >> 5); 3246 if (num_chunks == 0) 3247 num_chunks++; 3248 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3249 3250 for (i = 0; i < num_chunks; i++) { 3251 for (j = 0; j < num_pieces; j++) { 3252 if ((mask[i] & (1 << j)) == 0) 3253 return ((i << 5) + j); 3254 } 3255 } 3256 3257 return (-1); 3258} 3259 3260int 3261ctl_set_mask(uint32_t *mask, uint32_t bit) 3262{ 3263 uint32_t chunk, piece; 3264 3265 chunk = bit >> 5; 3266 piece = bit % (sizeof(uint32_t) * 8); 3267 3268 if ((mask[chunk] & (1 << piece)) != 0) 3269 return (-1); 3270 else 3271 mask[chunk] |= (1 << piece); 3272 3273 return (0); 3274} 3275 3276int 3277ctl_clear_mask(uint32_t *mask, uint32_t bit) 3278{ 3279 uint32_t chunk, piece; 3280 3281 chunk = bit >> 5; 3282 piece = bit % (sizeof(uint32_t) * 8); 3283 3284 if ((mask[chunk] & (1 << piece)) == 0) 3285 return (-1); 3286 else 3287 mask[chunk] &= ~(1 << piece); 3288 3289 return (0); 3290} 3291 3292int 3293ctl_is_set(uint32_t *mask, uint32_t bit) 3294{ 3295 uint32_t chunk, piece; 3296 3297 chunk = bit >> 5; 3298 piece = bit % (sizeof(uint32_t) * 8); 3299 3300 if ((mask[chunk] & (1 << piece)) == 0) 3301 return (0); 3302 else 3303 return (1); 3304} 3305 3306#ifdef unused 3307/* 3308 * The bus, target and lun are optional, they can be filled in later. 3309 * can_wait is used to determine whether we can wait on the malloc or not. 3310 */ 3311union ctl_io* 3312ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3313 uint32_t targ_lun, int can_wait) 3314{ 3315 union ctl_io *io; 3316 3317 if (can_wait) 3318 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3319 else 3320 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3321 3322 if (io != NULL) { 3323 io->io_hdr.io_type = io_type; 3324 io->io_hdr.targ_port = targ_port; 3325 /* 3326 * XXX KDM this needs to change/go away. We need to move 3327 * to a preallocated pool of ctl_scsiio structures. 3328 */ 3329 io->io_hdr.nexus.targ_target.id = targ_target; 3330 io->io_hdr.nexus.targ_lun = targ_lun; 3331 } 3332 3333 return (io); 3334} 3335 3336void 3337ctl_kfree_io(union ctl_io *io) 3338{ 3339 free(io, M_CTL); 3340} 3341#endif /* unused */ 3342 3343/* 3344 * ctl_softc, pool_type, total_ctl_io are passed in. 3345 * npool is passed out. 3346 */ 3347int 3348ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3349 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3350{ 3351 uint32_t i; 3352 union ctl_io *cur_io, *next_io; 3353 struct ctl_io_pool *pool; 3354 int retval; 3355 3356 retval = 0; 3357 3358 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3359 M_NOWAIT | M_ZERO); 3360 if (pool == NULL) { 3361 retval = -ENOMEM; 3362 goto bailout; 3363 } 3364 3365 pool->type = pool_type; 3366 pool->ctl_softc = ctl_softc; 3367 3368 mtx_lock(&ctl_softc->pool_lock); 3369 pool->id = ctl_softc->cur_pool_id++; 3370 mtx_unlock(&ctl_softc->pool_lock); 3371 3372 pool->flags = CTL_POOL_FLAG_NONE; 3373 pool->refcount = 1; /* Reference for validity. */ 3374 STAILQ_INIT(&pool->free_queue); 3375 3376 /* 3377 * XXX KDM other options here: 3378 * - allocate a page at a time 3379 * - allocate one big chunk of memory. 3380 * Page allocation might work well, but would take a little more 3381 * tracking. 3382 */ 3383 for (i = 0; i < total_ctl_io; i++) { 3384 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL, 3385 M_NOWAIT); 3386 if (cur_io == NULL) { 3387 retval = ENOMEM; 3388 break; 3389 } 3390 cur_io->io_hdr.pool = pool; 3391 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3392 pool->total_ctl_io++; 3393 pool->free_ctl_io++; 3394 } 3395 3396 if (retval != 0) { 3397 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3398 cur_io != NULL; cur_io = next_io) { 3399 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3400 links); 3401 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3402 ctl_io_hdr, links); 3403 free(cur_io, M_CTL); 3404 } 3405 3406 free(pool, M_CTL); 3407 goto bailout; 3408 } 3409 mtx_lock(&ctl_softc->pool_lock); 3410 ctl_softc->num_pools++; 3411 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3412 /* 3413 * Increment our usage count if this is an external consumer, so we 3414 * can't get unloaded until the external consumer (most likely a 3415 * FETD) unloads and frees his pool. 3416 * 3417 * XXX KDM will this increment the caller's module use count, or 3418 * mine? 3419 */ 3420#if 0 3421 if ((pool_type != CTL_POOL_EMERGENCY) 3422 && (pool_type != CTL_POOL_INTERNAL) 3423 && (pool_type != CTL_POOL_IOCTL) 3424 && (pool_type != CTL_POOL_4OTHERSC)) 3425 MOD_INC_USE_COUNT; 3426#endif 3427 3428 mtx_unlock(&ctl_softc->pool_lock); 3429 3430 *npool = pool; 3431 3432bailout: 3433 3434 return (retval); 3435} 3436 3437static int 3438ctl_pool_acquire(struct ctl_io_pool *pool) 3439{ 3440 3441 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3442 3443 if (pool->flags & CTL_POOL_FLAG_INVALID) 3444 return (-EINVAL); 3445 3446 pool->refcount++; 3447 3448 return (0); 3449} 3450 3451static void 3452ctl_pool_release(struct ctl_io_pool *pool) 3453{ 3454 struct ctl_softc *ctl_softc = pool->ctl_softc; 3455 union ctl_io *io; 3456 3457 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3458 3459 if (--pool->refcount != 0) 3460 return; 3461 3462 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3463 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3464 links); 3465 free(io, M_CTL); 3466 } 3467 3468 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3469 ctl_softc->num_pools--; 3470 3471 /* 3472 * XXX KDM will this decrement the caller's usage count or mine? 3473 */ 3474#if 0 3475 if ((pool->type != CTL_POOL_EMERGENCY) 3476 && (pool->type != CTL_POOL_INTERNAL) 3477 && (pool->type != CTL_POOL_IOCTL)) 3478 MOD_DEC_USE_COUNT; 3479#endif 3480 3481 free(pool, M_CTL); 3482} 3483 3484void 3485ctl_pool_free(struct ctl_io_pool *pool) 3486{ 3487 struct ctl_softc *ctl_softc; 3488 3489 if (pool == NULL) 3490 return; 3491 3492 ctl_softc = pool->ctl_softc; 3493 mtx_lock(&ctl_softc->pool_lock); 3494 pool->flags |= CTL_POOL_FLAG_INVALID; 3495 ctl_pool_release(pool); 3496 mtx_unlock(&ctl_softc->pool_lock); 3497} 3498 3499/* 3500 * This routine does not block (except for spinlocks of course). 3501 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3502 * possible. 3503 */ 3504union ctl_io * 3505ctl_alloc_io(void *pool_ref) 3506{ 3507 union ctl_io *io; 3508 struct ctl_softc *ctl_softc; 3509 struct ctl_io_pool *pool, *npool; 3510 struct ctl_io_pool *emergency_pool; 3511 3512 pool = (struct ctl_io_pool *)pool_ref; 3513 3514 if (pool == NULL) { 3515 printf("%s: pool is NULL\n", __func__); 3516 return (NULL); 3517 } 3518 3519 emergency_pool = NULL; 3520 3521 ctl_softc = pool->ctl_softc; 3522 3523 mtx_lock(&ctl_softc->pool_lock); 3524 /* 3525 * First, try to get the io structure from the user's pool. 3526 */ 3527 if (ctl_pool_acquire(pool) == 0) { 3528 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3529 if (io != NULL) { 3530 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3531 pool->total_allocated++; 3532 pool->free_ctl_io--; 3533 mtx_unlock(&ctl_softc->pool_lock); 3534 return (io); 3535 } else 3536 ctl_pool_release(pool); 3537 } 3538 /* 3539 * If he doesn't have any io structures left, search for an 3540 * emergency pool and grab one from there. 3541 */ 3542 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3543 if (npool->type != CTL_POOL_EMERGENCY) 3544 continue; 3545 3546 if (ctl_pool_acquire(npool) != 0) 3547 continue; 3548 3549 emergency_pool = npool; 3550 3551 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3552 if (io != NULL) { 3553 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3554 npool->total_allocated++; 3555 npool->free_ctl_io--; 3556 mtx_unlock(&ctl_softc->pool_lock); 3557 return (io); 3558 } else 3559 ctl_pool_release(npool); 3560 } 3561 3562 /* Drop the spinlock before we malloc */ 3563 mtx_unlock(&ctl_softc->pool_lock); 3564 3565 /* 3566 * The emergency pool (if it exists) didn't have one, so try an 3567 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3568 */ 3569 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3570 if (io != NULL) { 3571 /* 3572 * If the emergency pool exists but is empty, add this 3573 * ctl_io to its list when it gets freed. 3574 */ 3575 if (emergency_pool != NULL) { 3576 mtx_lock(&ctl_softc->pool_lock); 3577 if (ctl_pool_acquire(emergency_pool) == 0) { 3578 io->io_hdr.pool = emergency_pool; 3579 emergency_pool->total_ctl_io++; 3580 /* 3581 * Need to bump this, otherwise 3582 * total_allocated and total_freed won't 3583 * match when we no longer have anything 3584 * outstanding. 3585 */ 3586 emergency_pool->total_allocated++; 3587 } 3588 mtx_unlock(&ctl_softc->pool_lock); 3589 } else 3590 io->io_hdr.pool = NULL; 3591 } 3592 3593 return (io); 3594} 3595 3596void 3597ctl_free_io(union ctl_io *io) 3598{ 3599 if (io == NULL) 3600 return; 3601 3602 /* 3603 * If this ctl_io has a pool, return it to that pool. 3604 */ 3605 if (io->io_hdr.pool != NULL) { 3606 struct ctl_io_pool *pool; 3607#if 0 3608 struct ctl_softc *ctl_softc; 3609 union ctl_io *tmp_io; 3610 unsigned long xflags; 3611 int i; 3612 3613 ctl_softc = control_softc; 3614#endif 3615 3616 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3617 3618 mtx_lock(&pool->ctl_softc->pool_lock); 3619#if 0 3620 save_flags(xflags); 3621 3622 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST( 3623 &ctl_softc->task_queue); tmp_io != NULL; i++, 3624 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr, 3625 links)) { 3626 if (tmp_io == io) { 3627 printf("%s: %p is still on the task queue!\n", 3628 __func__, tmp_io); 3629 printf("%s: (%d): type %d " 3630 "msg %d cdb %x iptl: " 3631 "%d:%d:%d:%d tag 0x%04x " 3632 "flg %#lx\n", 3633 __func__, i, 3634 tmp_io->io_hdr.io_type, 3635 tmp_io->io_hdr.msg_type, 3636 tmp_io->scsiio.cdb[0], 3637 tmp_io->io_hdr.nexus.initid.id, 3638 tmp_io->io_hdr.nexus.targ_port, 3639 tmp_io->io_hdr.nexus.targ_target.id, 3640 tmp_io->io_hdr.nexus.targ_lun, 3641 (tmp_io->io_hdr.io_type == 3642 CTL_IO_TASK) ? 3643 tmp_io->taskio.tag_num : 3644 tmp_io->scsiio.tag_num, 3645 xflags); 3646 panic("I/O still on the task queue!"); 3647 } 3648 } 3649#endif 3650 io->io_hdr.io_type = 0xff; 3651 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3652 pool->total_freed++; 3653 pool->free_ctl_io++; 3654 ctl_pool_release(pool); 3655 mtx_unlock(&pool->ctl_softc->pool_lock); 3656 } else { 3657 /* 3658 * Otherwise, just free it. We probably malloced it and 3659 * the emergency pool wasn't available. 3660 */ 3661 free(io, M_CTL); 3662 } 3663 3664} 3665 3666void 3667ctl_zero_io(union ctl_io *io) 3668{ 3669 void *pool_ref; 3670 3671 if (io == NULL) 3672 return; 3673 3674 /* 3675 * May need to preserve linked list pointers at some point too. 3676 */ 3677 pool_ref = io->io_hdr.pool; 3678 3679 memset(io, 0, sizeof(*io)); 3680 3681 io->io_hdr.pool = pool_ref; 3682} 3683 3684/* 3685 * This routine is currently used for internal copies of ctl_ios that need 3686 * to persist for some reason after we've already returned status to the 3687 * FETD. (Thus the flag set.) 3688 * 3689 * XXX XXX 3690 * Note that this makes a blind copy of all fields in the ctl_io, except 3691 * for the pool reference. This includes any memory that has been 3692 * allocated! That memory will no longer be valid after done has been 3693 * called, so this would be VERY DANGEROUS for command that actually does 3694 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3695 * start and stop commands, which don't transfer any data, so this is not a 3696 * problem. If it is used for anything else, the caller would also need to 3697 * allocate data buffer space and this routine would need to be modified to 3698 * copy the data buffer(s) as well. 3699 */ 3700void 3701ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3702{ 3703 void *pool_ref; 3704 3705 if ((src == NULL) 3706 || (dest == NULL)) 3707 return; 3708 3709 /* 3710 * May need to preserve linked list pointers at some point too. 3711 */ 3712 pool_ref = dest->io_hdr.pool; 3713 3714 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3715 3716 dest->io_hdr.pool = pool_ref; 3717 /* 3718 * We need to know that this is an internal copy, and doesn't need 3719 * to get passed back to the FETD that allocated it. 3720 */ 3721 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3722} 3723 3724#ifdef NEEDTOPORT 3725static void 3726ctl_update_power_subpage(struct copan_power_subpage *page) 3727{ 3728 int num_luns, num_partitions, config_type; 3729 struct ctl_softc *softc; 3730 cs_BOOL_t aor_present, shelf_50pct_power; 3731 cs_raidset_personality_t rs_type; 3732 int max_active_luns; 3733 3734 softc = control_softc; 3735 3736 /* subtract out the processor LUN */ 3737 num_luns = softc->num_luns - 1; 3738 /* 3739 * Default to 7 LUNs active, which was the only number we allowed 3740 * in the past. 3741 */ 3742 max_active_luns = 7; 3743 3744 num_partitions = config_GetRsPartitionInfo(); 3745 config_type = config_GetConfigType(); 3746 shelf_50pct_power = config_GetShelfPowerMode(); 3747 aor_present = config_IsAorRsPresent(); 3748 3749 rs_type = ddb_GetRsRaidType(1); 3750 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3751 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3752 EPRINT(0, "Unsupported RS type %d!", rs_type); 3753 } 3754 3755 3756 page->total_luns = num_luns; 3757 3758 switch (config_type) { 3759 case 40: 3760 /* 3761 * In a 40 drive configuration, it doesn't matter what DC 3762 * cards we have, whether we have AOR enabled or not, 3763 * partitioning or not, or what type of RAIDset we have. 3764 * In that scenario, we can power up every LUN we present 3765 * to the user. 3766 */ 3767 max_active_luns = num_luns; 3768 3769 break; 3770 case 64: 3771 if (shelf_50pct_power == CS_FALSE) { 3772 /* 25% power */ 3773 if (aor_present == CS_TRUE) { 3774 if (rs_type == 3775 CS_RAIDSET_PERSONALITY_RAID5) { 3776 max_active_luns = 7; 3777 } else if (rs_type == 3778 CS_RAIDSET_PERSONALITY_RAID1){ 3779 max_active_luns = 14; 3780 } else { 3781 /* XXX KDM now what?? */ 3782 } 3783 } else { 3784 if (rs_type == 3785 CS_RAIDSET_PERSONALITY_RAID5) { 3786 max_active_luns = 8; 3787 } else if (rs_type == 3788 CS_RAIDSET_PERSONALITY_RAID1){ 3789 max_active_luns = 16; 3790 } else { 3791 /* XXX KDM now what?? */ 3792 } 3793 } 3794 } else { 3795 /* 50% power */ 3796 /* 3797 * With 50% power in a 64 drive configuration, we 3798 * can power all LUNs we present. 3799 */ 3800 max_active_luns = num_luns; 3801 } 3802 break; 3803 case 112: 3804 if (shelf_50pct_power == CS_FALSE) { 3805 /* 25% power */ 3806 if (aor_present == CS_TRUE) { 3807 if (rs_type == 3808 CS_RAIDSET_PERSONALITY_RAID5) { 3809 max_active_luns = 7; 3810 } else if (rs_type == 3811 CS_RAIDSET_PERSONALITY_RAID1){ 3812 max_active_luns = 14; 3813 } else { 3814 /* XXX KDM now what?? */ 3815 } 3816 } else { 3817 if (rs_type == 3818 CS_RAIDSET_PERSONALITY_RAID5) { 3819 max_active_luns = 8; 3820 } else if (rs_type == 3821 CS_RAIDSET_PERSONALITY_RAID1){ 3822 max_active_luns = 16; 3823 } else { 3824 /* XXX KDM now what?? */ 3825 } 3826 } 3827 } else { 3828 /* 50% power */ 3829 if (aor_present == CS_TRUE) { 3830 if (rs_type == 3831 CS_RAIDSET_PERSONALITY_RAID5) { 3832 max_active_luns = 14; 3833 } else if (rs_type == 3834 CS_RAIDSET_PERSONALITY_RAID1){ 3835 /* 3836 * We're assuming here that disk 3837 * caching is enabled, and so we're 3838 * able to power up half of each 3839 * LUN, and cache all writes. 3840 */ 3841 max_active_luns = num_luns; 3842 } else { 3843 /* XXX KDM now what?? */ 3844 } 3845 } else { 3846 if (rs_type == 3847 CS_RAIDSET_PERSONALITY_RAID5) { 3848 max_active_luns = 15; 3849 } else if (rs_type == 3850 CS_RAIDSET_PERSONALITY_RAID1){ 3851 max_active_luns = 30; 3852 } else { 3853 /* XXX KDM now what?? */ 3854 } 3855 } 3856 } 3857 break; 3858 default: 3859 /* 3860 * In this case, we have an unknown configuration, so we 3861 * just use the default from above. 3862 */ 3863 break; 3864 } 3865 3866 page->max_active_luns = max_active_luns; 3867#if 0 3868 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 3869 page->total_luns, page->max_active_luns); 3870#endif 3871} 3872#endif /* NEEDTOPORT */ 3873 3874/* 3875 * This routine could be used in the future to load default and/or saved 3876 * mode page parameters for a particuar lun. 3877 */ 3878static int 3879ctl_init_page_index(struct ctl_lun *lun) 3880{ 3881 int i; 3882 struct ctl_page_index *page_index; 3883 struct ctl_softc *softc; 3884 3885 memcpy(&lun->mode_pages.index, page_index_template, 3886 sizeof(page_index_template)); 3887 3888 softc = lun->ctl_softc; 3889 3890 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3891 3892 page_index = &lun->mode_pages.index[i]; 3893 /* 3894 * If this is a disk-only mode page, there's no point in 3895 * setting it up. For some pages, we have to have some 3896 * basic information about the disk in order to calculate the 3897 * mode page data. 3898 */ 3899 if ((lun->be_lun->lun_type != T_DIRECT) 3900 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3901 continue; 3902 3903 switch (page_index->page_code & SMPH_PC_MASK) { 3904 case SMS_FORMAT_DEVICE_PAGE: { 3905 struct scsi_format_page *format_page; 3906 3907 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3908 panic("subpage is incorrect!"); 3909 3910 /* 3911 * Sectors per track are set above. Bytes per 3912 * sector need to be set here on a per-LUN basis. 3913 */ 3914 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3915 &format_page_default, 3916 sizeof(format_page_default)); 3917 memcpy(&lun->mode_pages.format_page[ 3918 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3919 sizeof(format_page_changeable)); 3920 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3921 &format_page_default, 3922 sizeof(format_page_default)); 3923 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3924 &format_page_default, 3925 sizeof(format_page_default)); 3926 3927 format_page = &lun->mode_pages.format_page[ 3928 CTL_PAGE_CURRENT]; 3929 scsi_ulto2b(lun->be_lun->blocksize, 3930 format_page->bytes_per_sector); 3931 3932 format_page = &lun->mode_pages.format_page[ 3933 CTL_PAGE_DEFAULT]; 3934 scsi_ulto2b(lun->be_lun->blocksize, 3935 format_page->bytes_per_sector); 3936 3937 format_page = &lun->mode_pages.format_page[ 3938 CTL_PAGE_SAVED]; 3939 scsi_ulto2b(lun->be_lun->blocksize, 3940 format_page->bytes_per_sector); 3941 3942 page_index->page_data = 3943 (uint8_t *)lun->mode_pages.format_page; 3944 break; 3945 } 3946 case SMS_RIGID_DISK_PAGE: { 3947 struct scsi_rigid_disk_page *rigid_disk_page; 3948 uint32_t sectors_per_cylinder; 3949 uint64_t cylinders; 3950#ifndef __XSCALE__ 3951 int shift; 3952#endif /* !__XSCALE__ */ 3953 3954 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3955 panic("invalid subpage value %d", 3956 page_index->subpage); 3957 3958 /* 3959 * Rotation rate and sectors per track are set 3960 * above. We calculate the cylinders here based on 3961 * capacity. Due to the number of heads and 3962 * sectors per track we're using, smaller arrays 3963 * may turn out to have 0 cylinders. Linux and 3964 * FreeBSD don't pay attention to these mode pages 3965 * to figure out capacity, but Solaris does. It 3966 * seems to deal with 0 cylinders just fine, and 3967 * works out a fake geometry based on the capacity. 3968 */ 3969 memcpy(&lun->mode_pages.rigid_disk_page[ 3970 CTL_PAGE_CURRENT], &rigid_disk_page_default, 3971 sizeof(rigid_disk_page_default)); 3972 memcpy(&lun->mode_pages.rigid_disk_page[ 3973 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3974 sizeof(rigid_disk_page_changeable)); 3975 memcpy(&lun->mode_pages.rigid_disk_page[ 3976 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3977 sizeof(rigid_disk_page_default)); 3978 memcpy(&lun->mode_pages.rigid_disk_page[ 3979 CTL_PAGE_SAVED], &rigid_disk_page_default, 3980 sizeof(rigid_disk_page_default)); 3981 3982 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3983 CTL_DEFAULT_HEADS; 3984 3985 /* 3986 * The divide method here will be more accurate, 3987 * probably, but results in floating point being 3988 * used in the kernel on i386 (__udivdi3()). On the 3989 * XScale, though, __udivdi3() is implemented in 3990 * software. 3991 * 3992 * The shift method for cylinder calculation is 3993 * accurate if sectors_per_cylinder is a power of 3994 * 2. Otherwise it might be slightly off -- you 3995 * might have a bit of a truncation problem. 3996 */ 3997#ifdef __XSCALE__ 3998 cylinders = (lun->be_lun->maxlba + 1) / 3999 sectors_per_cylinder; 4000#else 4001 for (shift = 31; shift > 0; shift--) { 4002 if (sectors_per_cylinder & (1 << shift)) 4003 break; 4004 } 4005 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4006#endif 4007 4008 /* 4009 * We've basically got 3 bytes, or 24 bits for the 4010 * cylinder size in the mode page. If we're over, 4011 * just round down to 2^24. 4012 */ 4013 if (cylinders > 0xffffff) 4014 cylinders = 0xffffff; 4015 4016 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4017 CTL_PAGE_CURRENT]; 4018 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4019 4020 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4021 CTL_PAGE_DEFAULT]; 4022 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4023 4024 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4025 CTL_PAGE_SAVED]; 4026 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4027 4028 page_index->page_data = 4029 (uint8_t *)lun->mode_pages.rigid_disk_page; 4030 break; 4031 } 4032 case SMS_CACHING_PAGE: { 4033 4034 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4035 panic("invalid subpage value %d", 4036 page_index->subpage); 4037 /* 4038 * Defaults should be okay here, no calculations 4039 * needed. 4040 */ 4041 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4042 &caching_page_default, 4043 sizeof(caching_page_default)); 4044 memcpy(&lun->mode_pages.caching_page[ 4045 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4046 sizeof(caching_page_changeable)); 4047 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4048 &caching_page_default, 4049 sizeof(caching_page_default)); 4050 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4051 &caching_page_default, 4052 sizeof(caching_page_default)); 4053 page_index->page_data = 4054 (uint8_t *)lun->mode_pages.caching_page; 4055 break; 4056 } 4057 case SMS_CONTROL_MODE_PAGE: { 4058 4059 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4060 panic("invalid subpage value %d", 4061 page_index->subpage); 4062 4063 /* 4064 * Defaults should be okay here, no calculations 4065 * needed. 4066 */ 4067 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4068 &control_page_default, 4069 sizeof(control_page_default)); 4070 memcpy(&lun->mode_pages.control_page[ 4071 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4072 sizeof(control_page_changeable)); 4073 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4074 &control_page_default, 4075 sizeof(control_page_default)); 4076 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4077 &control_page_default, 4078 sizeof(control_page_default)); 4079 page_index->page_data = 4080 (uint8_t *)lun->mode_pages.control_page; 4081 break; 4082 4083 } 4084 case SMS_VENDOR_SPECIFIC_PAGE:{ 4085 switch (page_index->subpage) { 4086 case PWR_SUBPAGE_CODE: { 4087 struct copan_power_subpage *current_page, 4088 *saved_page; 4089 4090 memcpy(&lun->mode_pages.power_subpage[ 4091 CTL_PAGE_CURRENT], 4092 &power_page_default, 4093 sizeof(power_page_default)); 4094 memcpy(&lun->mode_pages.power_subpage[ 4095 CTL_PAGE_CHANGEABLE], 4096 &power_page_changeable, 4097 sizeof(power_page_changeable)); 4098 memcpy(&lun->mode_pages.power_subpage[ 4099 CTL_PAGE_DEFAULT], 4100 &power_page_default, 4101 sizeof(power_page_default)); 4102 memcpy(&lun->mode_pages.power_subpage[ 4103 CTL_PAGE_SAVED], 4104 &power_page_default, 4105 sizeof(power_page_default)); 4106 page_index->page_data = 4107 (uint8_t *)lun->mode_pages.power_subpage; 4108 4109 current_page = (struct copan_power_subpage *) 4110 (page_index->page_data + 4111 (page_index->page_len * 4112 CTL_PAGE_CURRENT)); 4113 saved_page = (struct copan_power_subpage *) 4114 (page_index->page_data + 4115 (page_index->page_len * 4116 CTL_PAGE_SAVED)); 4117 break; 4118 } 4119 case APS_SUBPAGE_CODE: { 4120 struct copan_aps_subpage *current_page, 4121 *saved_page; 4122 4123 // This gets set multiple times but 4124 // it should always be the same. It's 4125 // only done during init so who cares. 4126 index_to_aps_page = i; 4127 4128 memcpy(&lun->mode_pages.aps_subpage[ 4129 CTL_PAGE_CURRENT], 4130 &aps_page_default, 4131 sizeof(aps_page_default)); 4132 memcpy(&lun->mode_pages.aps_subpage[ 4133 CTL_PAGE_CHANGEABLE], 4134 &aps_page_changeable, 4135 sizeof(aps_page_changeable)); 4136 memcpy(&lun->mode_pages.aps_subpage[ 4137 CTL_PAGE_DEFAULT], 4138 &aps_page_default, 4139 sizeof(aps_page_default)); 4140 memcpy(&lun->mode_pages.aps_subpage[ 4141 CTL_PAGE_SAVED], 4142 &aps_page_default, 4143 sizeof(aps_page_default)); 4144 page_index->page_data = 4145 (uint8_t *)lun->mode_pages.aps_subpage; 4146 4147 current_page = (struct copan_aps_subpage *) 4148 (page_index->page_data + 4149 (page_index->page_len * 4150 CTL_PAGE_CURRENT)); 4151 saved_page = (struct copan_aps_subpage *) 4152 (page_index->page_data + 4153 (page_index->page_len * 4154 CTL_PAGE_SAVED)); 4155 break; 4156 } 4157 case DBGCNF_SUBPAGE_CODE: { 4158 struct copan_debugconf_subpage *current_page, 4159 *saved_page; 4160 4161 memcpy(&lun->mode_pages.debugconf_subpage[ 4162 CTL_PAGE_CURRENT], 4163 &debugconf_page_default, 4164 sizeof(debugconf_page_default)); 4165 memcpy(&lun->mode_pages.debugconf_subpage[ 4166 CTL_PAGE_CHANGEABLE], 4167 &debugconf_page_changeable, 4168 sizeof(debugconf_page_changeable)); 4169 memcpy(&lun->mode_pages.debugconf_subpage[ 4170 CTL_PAGE_DEFAULT], 4171 &debugconf_page_default, 4172 sizeof(debugconf_page_default)); 4173 memcpy(&lun->mode_pages.debugconf_subpage[ 4174 CTL_PAGE_SAVED], 4175 &debugconf_page_default, 4176 sizeof(debugconf_page_default)); 4177 page_index->page_data = 4178 (uint8_t *)lun->mode_pages.debugconf_subpage; 4179 4180 current_page = (struct copan_debugconf_subpage *) 4181 (page_index->page_data + 4182 (page_index->page_len * 4183 CTL_PAGE_CURRENT)); 4184 saved_page = (struct copan_debugconf_subpage *) 4185 (page_index->page_data + 4186 (page_index->page_len * 4187 CTL_PAGE_SAVED)); 4188 break; 4189 } 4190 default: 4191 panic("invalid subpage value %d", 4192 page_index->subpage); 4193 break; 4194 } 4195 break; 4196 } 4197 default: 4198 panic("invalid page value %d", 4199 page_index->page_code & SMPH_PC_MASK); 4200 break; 4201 } 4202 } 4203 4204 return (CTL_RETVAL_COMPLETE); 4205} 4206 4207/* 4208 * LUN allocation. 4209 * 4210 * Requirements: 4211 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4212 * wants us to allocate the LUN and he can block. 4213 * - ctl_softc is always set 4214 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4215 * 4216 * Returns 0 for success, non-zero (errno) for failure. 4217 */ 4218static int 4219ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4220 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4221{ 4222 struct ctl_lun *nlun, *lun; 4223 struct ctl_frontend *fe; 4224 int lun_number, i, lun_malloced; 4225 4226 if (be_lun == NULL) 4227 return (EINVAL); 4228 4229 /* 4230 * We currently only support Direct Access or Processor LUN types. 4231 */ 4232 switch (be_lun->lun_type) { 4233 case T_DIRECT: 4234 break; 4235 case T_PROCESSOR: 4236 break; 4237 case T_SEQUENTIAL: 4238 case T_CHANGER: 4239 default: 4240 be_lun->lun_config_status(be_lun->be_lun, 4241 CTL_LUN_CONFIG_FAILURE); 4242 break; 4243 } 4244 if (ctl_lun == NULL) { 4245 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4246 lun_malloced = 1; 4247 } else { 4248 lun_malloced = 0; 4249 lun = ctl_lun; 4250 } 4251 4252 memset(lun, 0, sizeof(*lun)); 4253 if (lun_malloced) 4254 lun->flags = CTL_LUN_MALLOCED; 4255 4256 mtx_lock(&ctl_softc->ctl_lock); 4257 /* 4258 * See if the caller requested a particular LUN number. If so, see 4259 * if it is available. Otherwise, allocate the first available LUN. 4260 */ 4261 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4262 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4263 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4264 mtx_unlock(&ctl_softc->ctl_lock); 4265 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4266 printf("ctl: requested LUN ID %d is higher " 4267 "than CTL_MAX_LUNS - 1 (%d)\n", 4268 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4269 } else { 4270 /* 4271 * XXX KDM return an error, or just assign 4272 * another LUN ID in this case?? 4273 */ 4274 printf("ctl: requested LUN ID %d is already " 4275 "in use\n", be_lun->req_lun_id); 4276 } 4277 if (lun->flags & CTL_LUN_MALLOCED) 4278 free(lun, M_CTL); 4279 be_lun->lun_config_status(be_lun->be_lun, 4280 CTL_LUN_CONFIG_FAILURE); 4281 return (ENOSPC); 4282 } 4283 lun_number = be_lun->req_lun_id; 4284 } else { 4285 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4286 if (lun_number == -1) { 4287 mtx_unlock(&ctl_softc->ctl_lock); 4288 printf("ctl: can't allocate LUN on target %ju, out of " 4289 "LUNs\n", (uintmax_t)target_id.id); 4290 if (lun->flags & CTL_LUN_MALLOCED) 4291 free(lun, M_CTL); 4292 be_lun->lun_config_status(be_lun->be_lun, 4293 CTL_LUN_CONFIG_FAILURE); 4294 return (ENOSPC); 4295 } 4296 } 4297 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4298 4299 lun->target = target_id; 4300 lun->lun = lun_number; 4301 lun->be_lun = be_lun; 4302 /* 4303 * The processor LUN is always enabled. Disk LUNs come on line 4304 * disabled, and must be enabled by the backend. 4305 */ 4306 lun->flags |= CTL_LUN_DISABLED; 4307 lun->backend = be_lun->be; 4308 be_lun->ctl_lun = lun; 4309 be_lun->lun_id = lun_number; 4310 atomic_add_int(&be_lun->be->num_luns, 1); 4311 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4312 lun->flags |= CTL_LUN_STOPPED; 4313 4314 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4315 lun->flags |= CTL_LUN_INOPERABLE; 4316 4317 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4318 lun->flags |= CTL_LUN_PRIMARY_SC; 4319 4320 lun->ctl_softc = ctl_softc; 4321 TAILQ_INIT(&lun->ooa_queue); 4322 TAILQ_INIT(&lun->blocked_queue); 4323 STAILQ_INIT(&lun->error_list); 4324 4325 /* 4326 * Initialize the mode page index. 4327 */ 4328 ctl_init_page_index(lun); 4329 4330 /* 4331 * Set the poweron UA for all initiators on this LUN only. 4332 */ 4333 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4334 lun->pending_sense[i].ua_pending = CTL_UA_POWERON; 4335 4336 /* 4337 * Now, before we insert this lun on the lun list, set the lun 4338 * inventory changed UA for all other luns. 4339 */ 4340 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4341 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4342 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4343 } 4344 } 4345 4346 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4347 4348 ctl_softc->ctl_luns[lun_number] = lun; 4349 4350 ctl_softc->num_luns++; 4351 4352 /* Setup statistics gathering */ 4353 lun->stats.device_type = be_lun->lun_type; 4354 lun->stats.lun_number = lun_number; 4355 if (lun->stats.device_type == T_DIRECT) 4356 lun->stats.blocksize = be_lun->blocksize; 4357 else 4358 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4359 for (i = 0;i < CTL_MAX_PORTS;i++) 4360 lun->stats.ports[i].targ_port = i; 4361 4362 mtx_unlock(&ctl_softc->ctl_lock); 4363 4364 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4365 4366 /* 4367 * Run through each registered FETD and bring it online if it isn't 4368 * already. Enable the target ID if it hasn't been enabled, and 4369 * enable this particular LUN. 4370 */ 4371 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) { 4372 int retval; 4373 4374 /* 4375 * XXX KDM this only works for ONE TARGET ID. We'll need 4376 * to do things differently if we go to a multiple target 4377 * ID scheme. 4378 */ 4379 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) { 4380 4381 retval = fe->targ_enable(fe->targ_lun_arg, target_id); 4382 if (retval != 0) { 4383 printf("ctl_alloc_lun: FETD %s port %d " 4384 "returned error %d for targ_enable on " 4385 "target %ju\n", fe->port_name, 4386 fe->targ_port, retval, 4387 (uintmax_t)target_id.id); 4388 } else 4389 fe->status |= CTL_PORT_STATUS_TARG_ONLINE; 4390 } 4391 4392 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number); 4393 if (retval != 0) { 4394 printf("ctl_alloc_lun: FETD %s port %d returned error " 4395 "%d for lun_enable on target %ju lun %d\n", 4396 fe->port_name, fe->targ_port, retval, 4397 (uintmax_t)target_id.id, lun_number); 4398 } else 4399 fe->status |= CTL_PORT_STATUS_LUN_ONLINE; 4400 } 4401 return (0); 4402} 4403 4404/* 4405 * Delete a LUN. 4406 * Assumptions: 4407 * - LUN has already been marked invalid and any pending I/O has been taken 4408 * care of. 4409 */ 4410static int 4411ctl_free_lun(struct ctl_lun *lun) 4412{ 4413 struct ctl_softc *softc; 4414#if 0 4415 struct ctl_frontend *fe; 4416#endif 4417 struct ctl_lun *nlun; 4418 union ctl_io *io, *next_io; 4419 int i; 4420 4421 softc = lun->ctl_softc; 4422 4423 mtx_assert(&softc->ctl_lock, MA_OWNED); 4424 4425 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4426 4427 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4428 4429 softc->ctl_luns[lun->lun] = NULL; 4430 4431 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) { 4432 printf("ctl_free_lun: aieee!! freeing a LUN with " 4433 "outstanding I/O!!\n"); 4434 } 4435 4436 /* 4437 * If we have anything pending on the RtR queue, remove it. 4438 */ 4439 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL; 4440 io = next_io) { 4441 uint32_t targ_lun; 4442 4443 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 4444 targ_lun = io->io_hdr.nexus.targ_lun; 4445 if (io->io_hdr.nexus.lun_map_fn != NULL) 4446 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 4447 if ((io->io_hdr.nexus.targ_target.id == lun->target.id) 4448 && (targ_lun == lun->lun)) 4449 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 4450 ctl_io_hdr, links); 4451 } 4452 4453 /* 4454 * Then remove everything from the blocked queue. 4455 */ 4456 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL; 4457 io = next_io) { 4458 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links); 4459 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links); 4460 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 4461 } 4462 4463 /* 4464 * Now clear out the OOA queue, and free all the I/O. 4465 * XXX KDM should we notify the FETD here? We probably need to 4466 * quiesce the LUN before deleting it. 4467 */ 4468 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL; 4469 io = next_io) { 4470 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links); 4471 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 4472 ctl_free_io(io); 4473 } 4474 4475 softc->num_luns--; 4476 4477 /* 4478 * XXX KDM this scheme only works for a single target/multiple LUN 4479 * setup. It needs to be revamped for a multiple target scheme. 4480 * 4481 * XXX KDM this results in fe->lun_disable() getting called twice, 4482 * once when ctl_disable_lun() is called, and a second time here. 4483 * We really need to re-think the LUN disable semantics. There 4484 * should probably be several steps/levels to LUN removal: 4485 * - disable 4486 * - invalidate 4487 * - free 4488 * 4489 * Right now we only have a disable method when communicating to 4490 * the front end ports, at least for individual LUNs. 4491 */ 4492#if 0 4493 STAILQ_FOREACH(fe, &softc->fe_list, links) { 4494 int retval; 4495 4496 retval = fe->lun_disable(fe->targ_lun_arg, lun->target, 4497 lun->lun); 4498 if (retval != 0) { 4499 printf("ctl_free_lun: FETD %s port %d returned error " 4500 "%d for lun_disable on target %ju lun %jd\n", 4501 fe->port_name, fe->targ_port, retval, 4502 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4503 } 4504 4505 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4506 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4507 4508 retval = fe->targ_disable(fe->targ_lun_arg,lun->target); 4509 if (retval != 0) { 4510 printf("ctl_free_lun: FETD %s port %d " 4511 "returned error %d for targ_disable on " 4512 "target %ju\n", fe->port_name, 4513 fe->targ_port, retval, 4514 (uintmax_t)lun->target.id); 4515 } else 4516 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4517 4518 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4519 continue; 4520 4521#if 0 4522 fe->port_offline(fe->onoff_arg); 4523 fe->status &= ~CTL_PORT_STATUS_ONLINE; 4524#endif 4525 } 4526 } 4527#endif 4528 4529 /* 4530 * Tell the backend to free resources, if this LUN has a backend. 4531 */ 4532 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4533 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4534 4535 if (lun->flags & CTL_LUN_MALLOCED) 4536 free(lun, M_CTL); 4537 4538 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4539 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4540 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4541 } 4542 } 4543 4544 return (0); 4545} 4546 4547static void 4548ctl_create_lun(struct ctl_be_lun *be_lun) 4549{ 4550 struct ctl_softc *ctl_softc; 4551 4552 ctl_softc = control_softc; 4553 4554 /* 4555 * ctl_alloc_lun() should handle all potential failure cases. 4556 */ 4557 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4558} 4559 4560int 4561ctl_add_lun(struct ctl_be_lun *be_lun) 4562{ 4563 struct ctl_softc *ctl_softc; 4564 4565 ctl_softc = control_softc; 4566 4567 mtx_lock(&ctl_softc->ctl_lock); 4568 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4569 mtx_unlock(&ctl_softc->ctl_lock); 4570 4571 ctl_wakeup_thread(); 4572 4573 return (0); 4574} 4575 4576int 4577ctl_enable_lun(struct ctl_be_lun *be_lun) 4578{ 4579 struct ctl_softc *ctl_softc; 4580 struct ctl_frontend *fe, *nfe; 4581 struct ctl_lun *lun; 4582 int retval; 4583 4584 ctl_softc = control_softc; 4585 4586 lun = (struct ctl_lun *)be_lun->ctl_lun; 4587 4588 mtx_lock(&ctl_softc->ctl_lock); 4589 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4590 /* 4591 * eh? Why did we get called if the LUN is already 4592 * enabled? 4593 */ 4594 mtx_unlock(&ctl_softc->ctl_lock); 4595 return (0); 4596 } 4597 lun->flags &= ~CTL_LUN_DISABLED; 4598 4599 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) { 4600 nfe = STAILQ_NEXT(fe, links); 4601 4602 /* 4603 * Drop the lock while we call the FETD's enable routine. 4604 * This can lead to a callback into CTL (at least in the 4605 * case of the internal initiator frontend. 4606 */ 4607 mtx_unlock(&ctl_softc->ctl_lock); 4608 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun); 4609 mtx_lock(&ctl_softc->ctl_lock); 4610 if (retval != 0) { 4611 printf("%s: FETD %s port %d returned error " 4612 "%d for lun_enable on target %ju lun %jd\n", 4613 __func__, fe->port_name, fe->targ_port, retval, 4614 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4615 } 4616#if 0 4617 else { 4618 /* NOTE: TODO: why does lun enable affect port status? */ 4619 fe->status |= CTL_PORT_STATUS_LUN_ONLINE; 4620 } 4621#endif 4622 } 4623 4624 mtx_unlock(&ctl_softc->ctl_lock); 4625 4626 return (0); 4627} 4628 4629int 4630ctl_disable_lun(struct ctl_be_lun *be_lun) 4631{ 4632 struct ctl_softc *ctl_softc; 4633 struct ctl_frontend *fe; 4634 struct ctl_lun *lun; 4635 int retval; 4636 4637 ctl_softc = control_softc; 4638 4639 lun = (struct ctl_lun *)be_lun->ctl_lun; 4640 4641 mtx_lock(&ctl_softc->ctl_lock); 4642 4643 if (lun->flags & CTL_LUN_DISABLED) { 4644 mtx_unlock(&ctl_softc->ctl_lock); 4645 return (0); 4646 } 4647 lun->flags |= CTL_LUN_DISABLED; 4648 4649 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) { 4650 mtx_unlock(&ctl_softc->ctl_lock); 4651 /* 4652 * Drop the lock before we call the frontend's disable 4653 * routine, to avoid lock order reversals. 4654 * 4655 * XXX KDM what happens if the frontend list changes while 4656 * we're traversing it? It's unlikely, but should be handled. 4657 */ 4658 retval = fe->lun_disable(fe->targ_lun_arg, lun->target, 4659 lun->lun); 4660 mtx_lock(&ctl_softc->ctl_lock); 4661 if (retval != 0) { 4662 printf("ctl_alloc_lun: FETD %s port %d returned error " 4663 "%d for lun_disable on target %ju lun %jd\n", 4664 fe->port_name, fe->targ_port, retval, 4665 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4666 } 4667 } 4668 4669 mtx_unlock(&ctl_softc->ctl_lock); 4670 4671 return (0); 4672} 4673 4674int 4675ctl_start_lun(struct ctl_be_lun *be_lun) 4676{ 4677 struct ctl_softc *ctl_softc; 4678 struct ctl_lun *lun; 4679 4680 ctl_softc = control_softc; 4681 4682 lun = (struct ctl_lun *)be_lun->ctl_lun; 4683 4684 mtx_lock(&ctl_softc->ctl_lock); 4685 lun->flags &= ~CTL_LUN_STOPPED; 4686 mtx_unlock(&ctl_softc->ctl_lock); 4687 4688 return (0); 4689} 4690 4691int 4692ctl_stop_lun(struct ctl_be_lun *be_lun) 4693{ 4694 struct ctl_softc *ctl_softc; 4695 struct ctl_lun *lun; 4696 4697 ctl_softc = control_softc; 4698 4699 lun = (struct ctl_lun *)be_lun->ctl_lun; 4700 4701 mtx_lock(&ctl_softc->ctl_lock); 4702 lun->flags |= CTL_LUN_STOPPED; 4703 mtx_unlock(&ctl_softc->ctl_lock); 4704 4705 return (0); 4706} 4707 4708int 4709ctl_lun_offline(struct ctl_be_lun *be_lun) 4710{ 4711 struct ctl_softc *ctl_softc; 4712 struct ctl_lun *lun; 4713 4714 ctl_softc = control_softc; 4715 4716 lun = (struct ctl_lun *)be_lun->ctl_lun; 4717 4718 mtx_lock(&ctl_softc->ctl_lock); 4719 lun->flags |= CTL_LUN_OFFLINE; 4720 mtx_unlock(&ctl_softc->ctl_lock); 4721 4722 return (0); 4723} 4724 4725int 4726ctl_lun_online(struct ctl_be_lun *be_lun) 4727{ 4728 struct ctl_softc *ctl_softc; 4729 struct ctl_lun *lun; 4730 4731 ctl_softc = control_softc; 4732 4733 lun = (struct ctl_lun *)be_lun->ctl_lun; 4734 4735 mtx_lock(&ctl_softc->ctl_lock); 4736 lun->flags &= ~CTL_LUN_OFFLINE; 4737 mtx_unlock(&ctl_softc->ctl_lock); 4738 4739 return (0); 4740} 4741 4742int 4743ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4744{ 4745 struct ctl_softc *ctl_softc; 4746 struct ctl_lun *lun; 4747 4748 ctl_softc = control_softc; 4749 4750 lun = (struct ctl_lun *)be_lun->ctl_lun; 4751 4752 mtx_lock(&ctl_softc->ctl_lock); 4753 4754 /* 4755 * The LUN needs to be disabled before it can be marked invalid. 4756 */ 4757 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4758 mtx_unlock(&ctl_softc->ctl_lock); 4759 return (-1); 4760 } 4761 /* 4762 * Mark the LUN invalid. 4763 */ 4764 lun->flags |= CTL_LUN_INVALID; 4765 4766 /* 4767 * If there is nothing in the OOA queue, go ahead and free the LUN. 4768 * If we have something in the OOA queue, we'll free it when the 4769 * last I/O completes. 4770 */ 4771 if (TAILQ_FIRST(&lun->ooa_queue) == NULL) 4772 ctl_free_lun(lun); 4773 mtx_unlock(&ctl_softc->ctl_lock); 4774 4775 return (0); 4776} 4777 4778int 4779ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4780{ 4781 struct ctl_softc *ctl_softc; 4782 struct ctl_lun *lun; 4783 4784 ctl_softc = control_softc; 4785 lun = (struct ctl_lun *)be_lun->ctl_lun; 4786 4787 mtx_lock(&ctl_softc->ctl_lock); 4788 lun->flags |= CTL_LUN_INOPERABLE; 4789 mtx_unlock(&ctl_softc->ctl_lock); 4790 4791 return (0); 4792} 4793 4794int 4795ctl_lun_operable(struct ctl_be_lun *be_lun) 4796{ 4797 struct ctl_softc *ctl_softc; 4798 struct ctl_lun *lun; 4799 4800 ctl_softc = control_softc; 4801 lun = (struct ctl_lun *)be_lun->ctl_lun; 4802 4803 mtx_lock(&ctl_softc->ctl_lock); 4804 lun->flags &= ~CTL_LUN_INOPERABLE; 4805 mtx_unlock(&ctl_softc->ctl_lock); 4806 4807 return (0); 4808} 4809 4810int 4811ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 4812 int lock) 4813{ 4814 struct ctl_softc *softc; 4815 struct ctl_lun *lun; 4816 struct copan_aps_subpage *current_sp; 4817 struct ctl_page_index *page_index; 4818 int i; 4819 4820 softc = control_softc; 4821 4822 mtx_lock(&softc->ctl_lock); 4823 4824 lun = (struct ctl_lun *)be_lun->ctl_lun; 4825 4826 page_index = NULL; 4827 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4828 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 4829 APS_PAGE_CODE) 4830 continue; 4831 4832 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 4833 continue; 4834 page_index = &lun->mode_pages.index[i]; 4835 } 4836 4837 if (page_index == NULL) { 4838 mtx_unlock(&softc->ctl_lock); 4839 printf("%s: APS subpage not found for lun %ju!\n", __func__, 4840 (uintmax_t)lun->lun); 4841 return (1); 4842 } 4843#if 0 4844 if ((softc->aps_locked_lun != 0) 4845 && (softc->aps_locked_lun != lun->lun)) { 4846 printf("%s: attempt to lock LUN %llu when %llu is already " 4847 "locked\n"); 4848 mtx_unlock(&softc->ctl_lock); 4849 return (1); 4850 } 4851#endif 4852 4853 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 4854 (page_index->page_len * CTL_PAGE_CURRENT)); 4855 4856 if (lock != 0) { 4857 current_sp->lock_active = APS_LOCK_ACTIVE; 4858 softc->aps_locked_lun = lun->lun; 4859 } else { 4860 current_sp->lock_active = 0; 4861 softc->aps_locked_lun = 0; 4862 } 4863 4864 4865 /* 4866 * If we're in HA mode, try to send the lock message to the other 4867 * side. 4868 */ 4869 if (ctl_is_single == 0) { 4870 int isc_retval; 4871 union ctl_ha_msg lock_msg; 4872 4873 lock_msg.hdr.nexus = *nexus; 4874 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 4875 if (lock != 0) 4876 lock_msg.aps.lock_flag = 1; 4877 else 4878 lock_msg.aps.lock_flag = 0; 4879 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 4880 sizeof(lock_msg), 0); 4881 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 4882 printf("%s: APS (lock=%d) error returned from " 4883 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 4884 mtx_unlock(&softc->ctl_lock); 4885 return (1); 4886 } 4887 } 4888 4889 mtx_unlock(&softc->ctl_lock); 4890 4891 return (0); 4892} 4893 4894void 4895ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4896{ 4897 struct ctl_lun *lun; 4898 struct ctl_softc *softc; 4899 int i; 4900 4901 softc = control_softc; 4902 4903 mtx_lock(&softc->ctl_lock); 4904 4905 lun = (struct ctl_lun *)be_lun->ctl_lun; 4906 4907 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4908 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED; 4909 4910 mtx_unlock(&softc->ctl_lock); 4911} 4912 4913/* 4914 * Backend "memory move is complete" callback for requests that never 4915 * make it down to say RAIDCore's configuration code. 4916 */ 4917int 4918ctl_config_move_done(union ctl_io *io) 4919{ 4920 int retval; 4921 4922 retval = CTL_RETVAL_COMPLETE; 4923 4924 4925 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4926 /* 4927 * XXX KDM this shouldn't happen, but what if it does? 4928 */ 4929 if (io->io_hdr.io_type != CTL_IO_SCSI) 4930 panic("I/O type isn't CTL_IO_SCSI!"); 4931 4932 if ((io->io_hdr.port_status == 0) 4933 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4934 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 4935 io->io_hdr.status = CTL_SUCCESS; 4936 else if ((io->io_hdr.port_status != 0) 4937 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4938 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 4939 /* 4940 * For hardware error sense keys, the sense key 4941 * specific value is defined to be a retry count, 4942 * but we use it to pass back an internal FETD 4943 * error code. XXX KDM Hopefully the FETD is only 4944 * using 16 bits for an error code, since that's 4945 * all the space we have in the sks field. 4946 */ 4947 ctl_set_internal_failure(&io->scsiio, 4948 /*sks_valid*/ 1, 4949 /*retry_count*/ 4950 io->io_hdr.port_status); 4951 free(io->scsiio.kern_data_ptr, M_CTL); 4952 ctl_done(io); 4953 goto bailout; 4954 } 4955 4956 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 4957 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 4958 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4959 /* 4960 * XXX KDM just assuming a single pointer here, and not a 4961 * S/G list. If we start using S/G lists for config data, 4962 * we'll need to know how to clean them up here as well. 4963 */ 4964 free(io->scsiio.kern_data_ptr, M_CTL); 4965 /* Hopefully the user has already set the status... */ 4966 ctl_done(io); 4967 } else { 4968 /* 4969 * XXX KDM now we need to continue data movement. Some 4970 * options: 4971 * - call ctl_scsiio() again? We don't do this for data 4972 * writes, because for those at least we know ahead of 4973 * time where the write will go and how long it is. For 4974 * config writes, though, that information is largely 4975 * contained within the write itself, thus we need to 4976 * parse out the data again. 4977 * 4978 * - Call some other function once the data is in? 4979 */ 4980 4981 /* 4982 * XXX KDM call ctl_scsiio() again for now, and check flag 4983 * bits to see whether we're allocated or not. 4984 */ 4985 retval = ctl_scsiio(&io->scsiio); 4986 } 4987bailout: 4988 return (retval); 4989} 4990 4991/* 4992 * This gets called by a backend driver when it is done with a 4993 * data_submit method. 4994 */ 4995void 4996ctl_data_submit_done(union ctl_io *io) 4997{ 4998 /* 4999 * If the IO_CONT flag is set, we need to call the supplied 5000 * function to continue processing the I/O, instead of completing 5001 * the I/O just yet. 5002 * 5003 * If there is an error, though, we don't want to keep processing. 5004 * Instead, just send status back to the initiator. 5005 */ 5006 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5007 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5008 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5009 io->scsiio.io_cont(io); 5010 return; 5011 } 5012 ctl_done(io); 5013} 5014 5015/* 5016 * This gets called by a backend driver when it is done with a 5017 * configuration write. 5018 */ 5019void 5020ctl_config_write_done(union ctl_io *io) 5021{ 5022 /* 5023 * If the IO_CONT flag is set, we need to call the supplied 5024 * function to continue processing the I/O, instead of completing 5025 * the I/O just yet. 5026 * 5027 * If there is an error, though, we don't want to keep processing. 5028 * Instead, just send status back to the initiator. 5029 */ 5030 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5031 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5032 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5033 io->scsiio.io_cont(io); 5034 return; 5035 } 5036 /* 5037 * Since a configuration write can be done for commands that actually 5038 * have data allocated, like write buffer, and commands that have 5039 * no data, like start/stop unit, we need to check here. 5040 */ 5041 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5042 free(io->scsiio.kern_data_ptr, M_CTL); 5043 ctl_done(io); 5044} 5045 5046/* 5047 * SCSI release command. 5048 */ 5049int 5050ctl_scsi_release(struct ctl_scsiio *ctsio) 5051{ 5052 int length, longid, thirdparty_id, resv_id; 5053 struct ctl_softc *ctl_softc; 5054 struct ctl_lun *lun; 5055 5056 length = 0; 5057 resv_id = 0; 5058 5059 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5060 5061 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5062 ctl_softc = control_softc; 5063 5064 switch (ctsio->cdb[0]) { 5065 case RELEASE: { 5066 struct scsi_release *cdb; 5067 5068 cdb = (struct scsi_release *)ctsio->cdb; 5069 if ((cdb->byte2 & 0x1f) != 0) { 5070 ctl_set_invalid_field(ctsio, 5071 /*sks_valid*/ 1, 5072 /*command*/ 1, 5073 /*field*/ 1, 5074 /*bit_valid*/ 0, 5075 /*bit*/ 0); 5076 ctl_done((union ctl_io *)ctsio); 5077 return (CTL_RETVAL_COMPLETE); 5078 } 5079 break; 5080 } 5081 case RELEASE_10: { 5082 struct scsi_release_10 *cdb; 5083 5084 cdb = (struct scsi_release_10 *)ctsio->cdb; 5085 5086 if ((cdb->byte2 & SR10_EXTENT) != 0) { 5087 ctl_set_invalid_field(ctsio, 5088 /*sks_valid*/ 1, 5089 /*command*/ 1, 5090 /*field*/ 1, 5091 /*bit_valid*/ 1, 5092 /*bit*/ 0); 5093 ctl_done((union ctl_io *)ctsio); 5094 return (CTL_RETVAL_COMPLETE); 5095 5096 } 5097 5098 if ((cdb->byte2 & SR10_3RDPTY) != 0) { 5099 ctl_set_invalid_field(ctsio, 5100 /*sks_valid*/ 1, 5101 /*command*/ 1, 5102 /*field*/ 1, 5103 /*bit_valid*/ 1, 5104 /*bit*/ 4); 5105 ctl_done((union ctl_io *)ctsio); 5106 return (CTL_RETVAL_COMPLETE); 5107 } 5108 5109 if (cdb->byte2 & SR10_LONGID) 5110 longid = 1; 5111 else 5112 thirdparty_id = cdb->thirdparty_id; 5113 5114 resv_id = cdb->resv_id; 5115 length = scsi_2btoul(cdb->length); 5116 break; 5117 } 5118 } 5119 5120 5121 /* 5122 * XXX KDM right now, we only support LUN reservation. We don't 5123 * support 3rd party reservations, or extent reservations, which 5124 * might actually need the parameter list. If we've gotten this 5125 * far, we've got a LUN reservation. Anything else got kicked out 5126 * above. So, according to SPC, ignore the length. 5127 */ 5128 length = 0; 5129 5130 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5131 && (length > 0)) { 5132 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5133 ctsio->kern_data_len = length; 5134 ctsio->kern_total_len = length; 5135 ctsio->kern_data_resid = 0; 5136 ctsio->kern_rel_offset = 0; 5137 ctsio->kern_sg_entries = 0; 5138 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5139 ctsio->be_move_done = ctl_config_move_done; 5140 ctl_datamove((union ctl_io *)ctsio); 5141 5142 return (CTL_RETVAL_COMPLETE); 5143 } 5144 5145 if (length > 0) 5146 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5147 5148 mtx_lock(&ctl_softc->ctl_lock); 5149 5150 /* 5151 * According to SPC, it is not an error for an intiator to attempt 5152 * to release a reservation on a LUN that isn't reserved, or that 5153 * is reserved by another initiator. The reservation can only be 5154 * released, though, by the initiator who made it or by one of 5155 * several reset type events. 5156 */ 5157 if (lun->flags & CTL_LUN_RESERVED) { 5158 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5159 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5160 && (ctsio->io_hdr.nexus.targ_target.id == 5161 lun->rsv_nexus.targ_target.id)) { 5162 lun->flags &= ~CTL_LUN_RESERVED; 5163 } 5164 } 5165 5166 ctsio->scsi_status = SCSI_STATUS_OK; 5167 ctsio->io_hdr.status = CTL_SUCCESS; 5168 5169 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5170 free(ctsio->kern_data_ptr, M_CTL); 5171 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5172 } 5173 5174 mtx_unlock(&ctl_softc->ctl_lock); 5175 5176 ctl_done((union ctl_io *)ctsio); 5177 return (CTL_RETVAL_COMPLETE); 5178} 5179 5180int 5181ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5182{ 5183 int extent, thirdparty, longid; 5184 int resv_id, length; 5185 uint64_t thirdparty_id; 5186 struct ctl_softc *ctl_softc; 5187 struct ctl_lun *lun; 5188 5189 extent = 0; 5190 thirdparty = 0; 5191 longid = 0; 5192 resv_id = 0; 5193 length = 0; 5194 thirdparty_id = 0; 5195 5196 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5197 5198 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5199 ctl_softc = control_softc; 5200 5201 switch (ctsio->cdb[0]) { 5202 case RESERVE: { 5203 struct scsi_reserve *cdb; 5204 5205 cdb = (struct scsi_reserve *)ctsio->cdb; 5206 if ((cdb->byte2 & 0x1f) != 0) { 5207 ctl_set_invalid_field(ctsio, 5208 /*sks_valid*/ 1, 5209 /*command*/ 1, 5210 /*field*/ 1, 5211 /*bit_valid*/ 0, 5212 /*bit*/ 0); 5213 ctl_done((union ctl_io *)ctsio); 5214 return (CTL_RETVAL_COMPLETE); 5215 } 5216 resv_id = cdb->resv_id; 5217 length = scsi_2btoul(cdb->length); 5218 break; 5219 } 5220 case RESERVE_10: { 5221 struct scsi_reserve_10 *cdb; 5222 5223 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5224 5225 if ((cdb->byte2 & SR10_EXTENT) != 0) { 5226 ctl_set_invalid_field(ctsio, 5227 /*sks_valid*/ 1, 5228 /*command*/ 1, 5229 /*field*/ 1, 5230 /*bit_valid*/ 1, 5231 /*bit*/ 0); 5232 ctl_done((union ctl_io *)ctsio); 5233 return (CTL_RETVAL_COMPLETE); 5234 } 5235 if ((cdb->byte2 & SR10_3RDPTY) != 0) { 5236 ctl_set_invalid_field(ctsio, 5237 /*sks_valid*/ 1, 5238 /*command*/ 1, 5239 /*field*/ 1, 5240 /*bit_valid*/ 1, 5241 /*bit*/ 4); 5242 ctl_done((union ctl_io *)ctsio); 5243 return (CTL_RETVAL_COMPLETE); 5244 } 5245 if (cdb->byte2 & SR10_LONGID) 5246 longid = 1; 5247 else 5248 thirdparty_id = cdb->thirdparty_id; 5249 5250 resv_id = cdb->resv_id; 5251 length = scsi_2btoul(cdb->length); 5252 break; 5253 } 5254 } 5255 5256 /* 5257 * XXX KDM right now, we only support LUN reservation. We don't 5258 * support 3rd party reservations, or extent reservations, which 5259 * might actually need the parameter list. If we've gotten this 5260 * far, we've got a LUN reservation. Anything else got kicked out 5261 * above. So, according to SPC, ignore the length. 5262 */ 5263 length = 0; 5264 5265 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5266 && (length > 0)) { 5267 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5268 ctsio->kern_data_len = length; 5269 ctsio->kern_total_len = length; 5270 ctsio->kern_data_resid = 0; 5271 ctsio->kern_rel_offset = 0; 5272 ctsio->kern_sg_entries = 0; 5273 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5274 ctsio->be_move_done = ctl_config_move_done; 5275 ctl_datamove((union ctl_io *)ctsio); 5276 5277 return (CTL_RETVAL_COMPLETE); 5278 } 5279 5280 if (length > 0) 5281 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5282 5283 mtx_lock(&ctl_softc->ctl_lock); 5284 if (lun->flags & CTL_LUN_RESERVED) { 5285 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5286 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5287 || (ctsio->io_hdr.nexus.targ_target.id != 5288 lun->rsv_nexus.targ_target.id)) { 5289 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5290 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5291 goto bailout; 5292 } 5293 } 5294 5295 lun->flags |= CTL_LUN_RESERVED; 5296 lun->rsv_nexus = ctsio->io_hdr.nexus; 5297 5298 ctsio->scsi_status = SCSI_STATUS_OK; 5299 ctsio->io_hdr.status = CTL_SUCCESS; 5300 5301bailout: 5302 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5303 free(ctsio->kern_data_ptr, M_CTL); 5304 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5305 } 5306 5307 mtx_unlock(&ctl_softc->ctl_lock); 5308 5309 ctl_done((union ctl_io *)ctsio); 5310 return (CTL_RETVAL_COMPLETE); 5311} 5312 5313int 5314ctl_start_stop(struct ctl_scsiio *ctsio) 5315{ 5316 struct scsi_start_stop_unit *cdb; 5317 struct ctl_lun *lun; 5318 struct ctl_softc *ctl_softc; 5319 int retval; 5320 5321 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5322 5323 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5324 ctl_softc = control_softc; 5325 retval = 0; 5326 5327 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5328 5329 /* 5330 * XXX KDM 5331 * We don't support the immediate bit on a stop unit. In order to 5332 * do that, we would need to code up a way to know that a stop is 5333 * pending, and hold off any new commands until it completes, one 5334 * way or another. Then we could accept or reject those commands 5335 * depending on its status. We would almost need to do the reverse 5336 * of what we do below for an immediate start -- return the copy of 5337 * the ctl_io to the FETD with status to send to the host (and to 5338 * free the copy!) and then free the original I/O once the stop 5339 * actually completes. That way, the OOA queue mechanism can work 5340 * to block commands that shouldn't proceed. Another alternative 5341 * would be to put the copy in the queue in place of the original, 5342 * and return the original back to the caller. That could be 5343 * slightly safer.. 5344 */ 5345 if ((cdb->byte2 & SSS_IMMED) 5346 && ((cdb->how & SSS_START) == 0)) { 5347 ctl_set_invalid_field(ctsio, 5348 /*sks_valid*/ 1, 5349 /*command*/ 1, 5350 /*field*/ 1, 5351 /*bit_valid*/ 1, 5352 /*bit*/ 0); 5353 ctl_done((union ctl_io *)ctsio); 5354 return (CTL_RETVAL_COMPLETE); 5355 } 5356 5357 /* 5358 * We don't support the power conditions field. We need to check 5359 * this prior to checking the load/eject and start/stop bits. 5360 */ 5361 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) { 5362 ctl_set_invalid_field(ctsio, 5363 /*sks_valid*/ 1, 5364 /*command*/ 1, 5365 /*field*/ 4, 5366 /*bit_valid*/ 1, 5367 /*bit*/ 4); 5368 ctl_done((union ctl_io *)ctsio); 5369 return (CTL_RETVAL_COMPLETE); 5370 } 5371 5372 /* 5373 * Media isn't removable, so we can't load or eject it. 5374 */ 5375 if ((cdb->how & SSS_LOEJ) != 0) { 5376 ctl_set_invalid_field(ctsio, 5377 /*sks_valid*/ 1, 5378 /*command*/ 1, 5379 /*field*/ 4, 5380 /*bit_valid*/ 1, 5381 /*bit*/ 1); 5382 ctl_done((union ctl_io *)ctsio); 5383 return (CTL_RETVAL_COMPLETE); 5384 } 5385 5386 if ((lun->flags & CTL_LUN_PR_RESERVED) 5387 && ((cdb->how & SSS_START)==0)) { 5388 uint32_t residx; 5389 5390 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5391 if (!lun->per_res[residx].registered 5392 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5393 5394 ctl_set_reservation_conflict(ctsio); 5395 ctl_done((union ctl_io *)ctsio); 5396 return (CTL_RETVAL_COMPLETE); 5397 } 5398 } 5399 5400 /* 5401 * If there is no backend on this device, we can't start or stop 5402 * it. In theory we shouldn't get any start/stop commands in the 5403 * first place at this level if the LUN doesn't have a backend. 5404 * That should get stopped by the command decode code. 5405 */ 5406 if (lun->backend == NULL) { 5407 ctl_set_invalid_opcode(ctsio); 5408 ctl_done((union ctl_io *)ctsio); 5409 return (CTL_RETVAL_COMPLETE); 5410 } 5411 5412 /* 5413 * XXX KDM Copan-specific offline behavior. 5414 * Figure out a reasonable way to port this? 5415 */ 5416#ifdef NEEDTOPORT 5417 mtx_lock(&ctl_softc->ctl_lock); 5418 5419 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5420 && (lun->flags & CTL_LUN_OFFLINE)) { 5421 /* 5422 * If the LUN is offline, and the on/offline bit isn't set, 5423 * reject the start or stop. Otherwise, let it through. 5424 */ 5425 mtx_unlock(&ctl_softc->ctl_lock); 5426 ctl_set_lun_not_ready(ctsio); 5427 ctl_done((union ctl_io *)ctsio); 5428 } else { 5429 mtx_unlock(&ctl_softc->ctl_lock); 5430#endif /* NEEDTOPORT */ 5431 /* 5432 * This could be a start or a stop when we're online, 5433 * or a stop/offline or start/online. A start or stop when 5434 * we're offline is covered in the case above. 5435 */ 5436 /* 5437 * In the non-immediate case, we send the request to 5438 * the backend and return status to the user when 5439 * it is done. 5440 * 5441 * In the immediate case, we allocate a new ctl_io 5442 * to hold a copy of the request, and send that to 5443 * the backend. We then set good status on the 5444 * user's request and return it immediately. 5445 */ 5446 if (cdb->byte2 & SSS_IMMED) { 5447 union ctl_io *new_io; 5448 5449 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5450 if (new_io == NULL) { 5451 ctl_set_busy(ctsio); 5452 ctl_done((union ctl_io *)ctsio); 5453 } else { 5454 ctl_copy_io((union ctl_io *)ctsio, 5455 new_io); 5456 retval = lun->backend->config_write(new_io); 5457 ctl_set_success(ctsio); 5458 ctl_done((union ctl_io *)ctsio); 5459 } 5460 } else { 5461 retval = lun->backend->config_write( 5462 (union ctl_io *)ctsio); 5463 } 5464#ifdef NEEDTOPORT 5465 } 5466#endif 5467 return (retval); 5468} 5469 5470/* 5471 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5472 * we don't really do anything with the LBA and length fields if the user 5473 * passes them in. Instead we'll just flush out the cache for the entire 5474 * LUN. 5475 */ 5476int 5477ctl_sync_cache(struct ctl_scsiio *ctsio) 5478{ 5479 struct ctl_lun *lun; 5480 struct ctl_softc *ctl_softc; 5481 uint64_t starting_lba; 5482 uint32_t block_count; 5483 int reladr, immed; 5484 int retval; 5485 5486 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5487 5488 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5489 ctl_softc = control_softc; 5490 retval = 0; 5491 reladr = 0; 5492 immed = 0; 5493 5494 switch (ctsio->cdb[0]) { 5495 case SYNCHRONIZE_CACHE: { 5496 struct scsi_sync_cache *cdb; 5497 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5498 5499 if (cdb->byte2 & SSC_RELADR) 5500 reladr = 1; 5501 5502 if (cdb->byte2 & SSC_IMMED) 5503 immed = 1; 5504 5505 starting_lba = scsi_4btoul(cdb->begin_lba); 5506 block_count = scsi_2btoul(cdb->lb_count); 5507 break; 5508 } 5509 case SYNCHRONIZE_CACHE_16: { 5510 struct scsi_sync_cache_16 *cdb; 5511 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5512 5513 if (cdb->byte2 & SSC_RELADR) 5514 reladr = 1; 5515 5516 if (cdb->byte2 & SSC_IMMED) 5517 immed = 1; 5518 5519 starting_lba = scsi_8btou64(cdb->begin_lba); 5520 block_count = scsi_4btoul(cdb->lb_count); 5521 break; 5522 } 5523 default: 5524 ctl_set_invalid_opcode(ctsio); 5525 ctl_done((union ctl_io *)ctsio); 5526 goto bailout; 5527 break; /* NOTREACHED */ 5528 } 5529 5530 if (immed) { 5531 /* 5532 * We don't support the immediate bit. Since it's in the 5533 * same place for the 10 and 16 byte SYNCHRONIZE CACHE 5534 * commands, we can just return the same error in either 5535 * case. 5536 */ 5537 ctl_set_invalid_field(ctsio, 5538 /*sks_valid*/ 1, 5539 /*command*/ 1, 5540 /*field*/ 1, 5541 /*bit_valid*/ 1, 5542 /*bit*/ 1); 5543 ctl_done((union ctl_io *)ctsio); 5544 goto bailout; 5545 } 5546 5547 if (reladr) { 5548 /* 5549 * We don't support the reladr bit either. It can only be 5550 * used with linked commands, and we don't support linked 5551 * commands. Since the bit is in the same place for the 5552 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can 5553 * just return the same error in either case. 5554 */ 5555 ctl_set_invalid_field(ctsio, 5556 /*sks_valid*/ 1, 5557 /*command*/ 1, 5558 /*field*/ 1, 5559 /*bit_valid*/ 1, 5560 /*bit*/ 0); 5561 ctl_done((union ctl_io *)ctsio); 5562 goto bailout; 5563 } 5564 5565 /* 5566 * We check the LBA and length, but don't do anything with them. 5567 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5568 * get flushed. This check will just help satisfy anyone who wants 5569 * to see an error for an out of range LBA. 5570 */ 5571 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5572 ctl_set_lba_out_of_range(ctsio); 5573 ctl_done((union ctl_io *)ctsio); 5574 goto bailout; 5575 } 5576 5577 /* 5578 * If this LUN has no backend, we can't flush the cache anyway. 5579 */ 5580 if (lun->backend == NULL) { 5581 ctl_set_invalid_opcode(ctsio); 5582 ctl_done((union ctl_io *)ctsio); 5583 goto bailout; 5584 } 5585 5586 /* 5587 * Check to see whether we're configured to send the SYNCHRONIZE 5588 * CACHE command directly to the back end. 5589 */ 5590 mtx_lock(&ctl_softc->ctl_lock); 5591 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5592 && (++(lun->sync_count) >= lun->sync_interval)) { 5593 lun->sync_count = 0; 5594 mtx_unlock(&ctl_softc->ctl_lock); 5595 retval = lun->backend->config_write((union ctl_io *)ctsio); 5596 } else { 5597 mtx_unlock(&ctl_softc->ctl_lock); 5598 ctl_set_success(ctsio); 5599 ctl_done((union ctl_io *)ctsio); 5600 } 5601 5602bailout: 5603 5604 return (retval); 5605} 5606 5607int 5608ctl_format(struct ctl_scsiio *ctsio) 5609{ 5610 struct scsi_format *cdb; 5611 struct ctl_lun *lun; 5612 struct ctl_softc *ctl_softc; 5613 int length, defect_list_len; 5614 5615 CTL_DEBUG_PRINT(("ctl_format\n")); 5616 5617 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5618 ctl_softc = control_softc; 5619 5620 cdb = (struct scsi_format *)ctsio->cdb; 5621 5622 length = 0; 5623 if (cdb->byte2 & SF_FMTDATA) { 5624 if (cdb->byte2 & SF_LONGLIST) 5625 length = sizeof(struct scsi_format_header_long); 5626 else 5627 length = sizeof(struct scsi_format_header_short); 5628 } 5629 5630 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5631 && (length > 0)) { 5632 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5633 ctsio->kern_data_len = length; 5634 ctsio->kern_total_len = length; 5635 ctsio->kern_data_resid = 0; 5636 ctsio->kern_rel_offset = 0; 5637 ctsio->kern_sg_entries = 0; 5638 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5639 ctsio->be_move_done = ctl_config_move_done; 5640 ctl_datamove((union ctl_io *)ctsio); 5641 5642 return (CTL_RETVAL_COMPLETE); 5643 } 5644 5645 defect_list_len = 0; 5646 5647 if (cdb->byte2 & SF_FMTDATA) { 5648 if (cdb->byte2 & SF_LONGLIST) { 5649 struct scsi_format_header_long *header; 5650 5651 header = (struct scsi_format_header_long *) 5652 ctsio->kern_data_ptr; 5653 5654 defect_list_len = scsi_4btoul(header->defect_list_len); 5655 if (defect_list_len != 0) { 5656 ctl_set_invalid_field(ctsio, 5657 /*sks_valid*/ 1, 5658 /*command*/ 0, 5659 /*field*/ 2, 5660 /*bit_valid*/ 0, 5661 /*bit*/ 0); 5662 goto bailout; 5663 } 5664 } else { 5665 struct scsi_format_header_short *header; 5666 5667 header = (struct scsi_format_header_short *) 5668 ctsio->kern_data_ptr; 5669 5670 defect_list_len = scsi_2btoul(header->defect_list_len); 5671 if (defect_list_len != 0) { 5672 ctl_set_invalid_field(ctsio, 5673 /*sks_valid*/ 1, 5674 /*command*/ 0, 5675 /*field*/ 2, 5676 /*bit_valid*/ 0, 5677 /*bit*/ 0); 5678 goto bailout; 5679 } 5680 } 5681 } 5682 5683 /* 5684 * The format command will clear out the "Medium format corrupted" 5685 * status if set by the configuration code. That status is really 5686 * just a way to notify the host that we have lost the media, and 5687 * get them to issue a command that will basically make them think 5688 * they're blowing away the media. 5689 */ 5690 mtx_lock(&ctl_softc->ctl_lock); 5691 lun->flags &= ~CTL_LUN_INOPERABLE; 5692 mtx_unlock(&ctl_softc->ctl_lock); 5693 5694 ctsio->scsi_status = SCSI_STATUS_OK; 5695 ctsio->io_hdr.status = CTL_SUCCESS; 5696bailout: 5697 5698 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5699 free(ctsio->kern_data_ptr, M_CTL); 5700 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5701 } 5702 5703 ctl_done((union ctl_io *)ctsio); 5704 return (CTL_RETVAL_COMPLETE); 5705} 5706 5707int 5708ctl_write_buffer(struct ctl_scsiio *ctsio) 5709{ 5710 struct scsi_write_buffer *cdb; 5711 struct copan_page_header *header; 5712 struct ctl_lun *lun; 5713 struct ctl_softc *ctl_softc; 5714 int buffer_offset, len; 5715 int retval; 5716 5717 header = NULL; 5718 5719 retval = CTL_RETVAL_COMPLETE; 5720 5721 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5722 5723 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5724 ctl_softc = control_softc; 5725 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5726 5727 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5728 ctl_set_invalid_field(ctsio, 5729 /*sks_valid*/ 1, 5730 /*command*/ 1, 5731 /*field*/ 1, 5732 /*bit_valid*/ 1, 5733 /*bit*/ 4); 5734 ctl_done((union ctl_io *)ctsio); 5735 return (CTL_RETVAL_COMPLETE); 5736 } 5737 if (cdb->buffer_id != 0) { 5738 ctl_set_invalid_field(ctsio, 5739 /*sks_valid*/ 1, 5740 /*command*/ 1, 5741 /*field*/ 2, 5742 /*bit_valid*/ 0, 5743 /*bit*/ 0); 5744 ctl_done((union ctl_io *)ctsio); 5745 return (CTL_RETVAL_COMPLETE); 5746 } 5747 5748 len = scsi_3btoul(cdb->length); 5749 buffer_offset = scsi_3btoul(cdb->offset); 5750 5751 if (len > sizeof(lun->write_buffer)) { 5752 ctl_set_invalid_field(ctsio, 5753 /*sks_valid*/ 1, 5754 /*command*/ 1, 5755 /*field*/ 6, 5756 /*bit_valid*/ 0, 5757 /*bit*/ 0); 5758 ctl_done((union ctl_io *)ctsio); 5759 return (CTL_RETVAL_COMPLETE); 5760 } 5761 5762 if (buffer_offset != 0) { 5763 ctl_set_invalid_field(ctsio, 5764 /*sks_valid*/ 1, 5765 /*command*/ 1, 5766 /*field*/ 3, 5767 /*bit_valid*/ 0, 5768 /*bit*/ 0); 5769 ctl_done((union ctl_io *)ctsio); 5770 return (CTL_RETVAL_COMPLETE); 5771 } 5772 5773 /* 5774 * If we've got a kernel request that hasn't been malloced yet, 5775 * malloc it and tell the caller the data buffer is here. 5776 */ 5777 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5778 ctsio->kern_data_ptr = lun->write_buffer; 5779 ctsio->kern_data_len = len; 5780 ctsio->kern_total_len = len; 5781 ctsio->kern_data_resid = 0; 5782 ctsio->kern_rel_offset = 0; 5783 ctsio->kern_sg_entries = 0; 5784 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5785 ctsio->be_move_done = ctl_config_move_done; 5786 ctl_datamove((union ctl_io *)ctsio); 5787 5788 return (CTL_RETVAL_COMPLETE); 5789 } 5790 5791 ctl_done((union ctl_io *)ctsio); 5792 5793 return (CTL_RETVAL_COMPLETE); 5794} 5795 5796int 5797ctl_write_same(struct ctl_scsiio *ctsio) 5798{ 5799 struct ctl_lun *lun; 5800 struct ctl_lba_len_flags *lbalen; 5801 uint64_t lba; 5802 uint32_t num_blocks; 5803 int len, retval; 5804 uint8_t byte2; 5805 5806 retval = CTL_RETVAL_COMPLETE; 5807 5808 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5809 5810 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5811 5812 switch (ctsio->cdb[0]) { 5813 case WRITE_SAME_10: { 5814 struct scsi_write_same_10 *cdb; 5815 5816 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5817 5818 lba = scsi_4btoul(cdb->addr); 5819 num_blocks = scsi_2btoul(cdb->length); 5820 byte2 = cdb->byte2; 5821 break; 5822 } 5823 case WRITE_SAME_16: { 5824 struct scsi_write_same_16 *cdb; 5825 5826 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5827 5828 lba = scsi_8btou64(cdb->addr); 5829 num_blocks = scsi_4btoul(cdb->length); 5830 byte2 = cdb->byte2; 5831 break; 5832 } 5833 default: 5834 /* 5835 * We got a command we don't support. This shouldn't 5836 * happen, commands should be filtered out above us. 5837 */ 5838 ctl_set_invalid_opcode(ctsio); 5839 ctl_done((union ctl_io *)ctsio); 5840 5841 return (CTL_RETVAL_COMPLETE); 5842 break; /* NOTREACHED */ 5843 } 5844 5845 /* 5846 * The first check is to make sure we're in bounds, the second 5847 * check is to catch wrap-around problems. If the lba + num blocks 5848 * is less than the lba, then we've wrapped around and the block 5849 * range is invalid anyway. 5850 */ 5851 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5852 || ((lba + num_blocks) < lba)) { 5853 ctl_set_lba_out_of_range(ctsio); 5854 ctl_done((union ctl_io *)ctsio); 5855 return (CTL_RETVAL_COMPLETE); 5856 } 5857 5858 /* Zero number of blocks means "to the last logical block" */ 5859 if (num_blocks == 0) { 5860 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5861 ctl_set_invalid_field(ctsio, 5862 /*sks_valid*/ 0, 5863 /*command*/ 1, 5864 /*field*/ 0, 5865 /*bit_valid*/ 0, 5866 /*bit*/ 0); 5867 ctl_done((union ctl_io *)ctsio); 5868 return (CTL_RETVAL_COMPLETE); 5869 } 5870 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5871 } 5872 5873 len = lun->be_lun->blocksize; 5874 5875 /* 5876 * If we've got a kernel request that hasn't been malloced yet, 5877 * malloc it and tell the caller the data buffer is here. 5878 */ 5879 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5880 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5881 ctsio->kern_data_len = len; 5882 ctsio->kern_total_len = len; 5883 ctsio->kern_data_resid = 0; 5884 ctsio->kern_rel_offset = 0; 5885 ctsio->kern_sg_entries = 0; 5886 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5887 ctsio->be_move_done = ctl_config_move_done; 5888 ctl_datamove((union ctl_io *)ctsio); 5889 5890 return (CTL_RETVAL_COMPLETE); 5891 } 5892 5893 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5894 lbalen->lba = lba; 5895 lbalen->len = num_blocks; 5896 lbalen->flags = byte2; 5897 retval = lun->backend->config_write((union ctl_io *)ctsio); 5898 5899 return (retval); 5900} 5901 5902int 5903ctl_unmap(struct ctl_scsiio *ctsio) 5904{ 5905 struct ctl_lun *lun; 5906 struct scsi_unmap *cdb; 5907 struct ctl_ptr_len_flags *ptrlen; 5908 struct scsi_unmap_header *hdr; 5909 struct scsi_unmap_desc *buf, *end; 5910 uint64_t lba; 5911 uint32_t num_blocks; 5912 int len, retval; 5913 uint8_t byte2; 5914 5915 retval = CTL_RETVAL_COMPLETE; 5916 5917 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5918 5919 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5920 cdb = (struct scsi_unmap *)ctsio->cdb; 5921 5922 len = scsi_2btoul(cdb->length); 5923 byte2 = cdb->byte2; 5924 5925 /* 5926 * If we've got a kernel request that hasn't been malloced yet, 5927 * malloc it and tell the caller the data buffer is here. 5928 */ 5929 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5930 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5931 ctsio->kern_data_len = len; 5932 ctsio->kern_total_len = len; 5933 ctsio->kern_data_resid = 0; 5934 ctsio->kern_rel_offset = 0; 5935 ctsio->kern_sg_entries = 0; 5936 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5937 ctsio->be_move_done = ctl_config_move_done; 5938 ctl_datamove((union ctl_io *)ctsio); 5939 5940 return (CTL_RETVAL_COMPLETE); 5941 } 5942 5943 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5944 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5945 if (len < sizeof (*hdr) || 5946 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5947 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5948 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5949 ctl_set_invalid_field(ctsio, 5950 /*sks_valid*/ 0, 5951 /*command*/ 0, 5952 /*field*/ 0, 5953 /*bit_valid*/ 0, 5954 /*bit*/ 0); 5955 ctl_done((union ctl_io *)ctsio); 5956 return (CTL_RETVAL_COMPLETE); 5957 } 5958 len = scsi_2btoul(hdr->desc_length); 5959 buf = (struct scsi_unmap_desc *)(hdr + 1); 5960 end = buf + len / sizeof(*buf); 5961 5962 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5963 ptrlen->ptr = (void *)buf; 5964 ptrlen->len = len; 5965 ptrlen->flags = byte2; 5966 5967 for (; buf < end; buf++) { 5968 lba = scsi_8btou64(buf->lba); 5969 num_blocks = scsi_4btoul(buf->length); 5970 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5971 || ((lba + num_blocks) < lba)) { 5972 ctl_set_lba_out_of_range(ctsio); 5973 ctl_done((union ctl_io *)ctsio); 5974 return (CTL_RETVAL_COMPLETE); 5975 } 5976 } 5977 5978 retval = lun->backend->config_write((union ctl_io *)ctsio); 5979 5980 return (retval); 5981} 5982 5983/* 5984 * Note that this function currently doesn't actually do anything inside 5985 * CTL to enforce things if the DQue bit is turned on. 5986 * 5987 * Also note that this function can't be used in the default case, because 5988 * the DQue bit isn't set in the changeable mask for the control mode page 5989 * anyway. This is just here as an example for how to implement a page 5990 * handler, and a placeholder in case we want to allow the user to turn 5991 * tagged queueing on and off. 5992 * 5993 * The D_SENSE bit handling is functional, however, and will turn 5994 * descriptor sense on and off for a given LUN. 5995 */ 5996int 5997ctl_control_page_handler(struct ctl_scsiio *ctsio, 5998 struct ctl_page_index *page_index, uint8_t *page_ptr) 5999{ 6000 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6001 struct ctl_lun *lun; 6002 struct ctl_softc *softc; 6003 int set_ua; 6004 uint32_t initidx; 6005 6006 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6007 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6008 set_ua = 0; 6009 6010 user_cp = (struct scsi_control_page *)page_ptr; 6011 current_cp = (struct scsi_control_page *) 6012 (page_index->page_data + (page_index->page_len * 6013 CTL_PAGE_CURRENT)); 6014 saved_cp = (struct scsi_control_page *) 6015 (page_index->page_data + (page_index->page_len * 6016 CTL_PAGE_SAVED)); 6017 6018 softc = control_softc; 6019 6020 mtx_lock(&softc->ctl_lock); 6021 if (((current_cp->rlec & SCP_DSENSE) == 0) 6022 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6023 /* 6024 * Descriptor sense is currently turned off and the user 6025 * wants to turn it on. 6026 */ 6027 current_cp->rlec |= SCP_DSENSE; 6028 saved_cp->rlec |= SCP_DSENSE; 6029 lun->flags |= CTL_LUN_SENSE_DESC; 6030 set_ua = 1; 6031 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6032 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6033 /* 6034 * Descriptor sense is currently turned on, and the user 6035 * wants to turn it off. 6036 */ 6037 current_cp->rlec &= ~SCP_DSENSE; 6038 saved_cp->rlec &= ~SCP_DSENSE; 6039 lun->flags &= ~CTL_LUN_SENSE_DESC; 6040 set_ua = 1; 6041 } 6042 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 6043 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6044#ifdef NEEDTOPORT 6045 csevent_log(CSC_CTL | CSC_SHELF_SW | 6046 CTL_UNTAG_TO_UNTAG, 6047 csevent_LogType_Trace, 6048 csevent_Severity_Information, 6049 csevent_AlertLevel_Green, 6050 csevent_FRU_Firmware, 6051 csevent_FRU_Unknown, 6052 "Received untagged to untagged transition"); 6053#endif /* NEEDTOPORT */ 6054 } else { 6055#ifdef NEEDTOPORT 6056 csevent_log(CSC_CTL | CSC_SHELF_SW | 6057 CTL_UNTAG_TO_TAG, 6058 csevent_LogType_ConfigChange, 6059 csevent_Severity_Information, 6060 csevent_AlertLevel_Green, 6061 csevent_FRU_Firmware, 6062 csevent_FRU_Unknown, 6063 "Received untagged to tagged " 6064 "queueing transition"); 6065#endif /* NEEDTOPORT */ 6066 6067 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6068 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 6069 set_ua = 1; 6070 } 6071 } else { 6072 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 6073#ifdef NEEDTOPORT 6074 csevent_log(CSC_CTL | CSC_SHELF_SW | 6075 CTL_TAG_TO_UNTAG, 6076 csevent_LogType_ConfigChange, 6077 csevent_Severity_Warning, 6078 csevent_AlertLevel_Yellow, 6079 csevent_FRU_Firmware, 6080 csevent_FRU_Unknown, 6081 "Received tagged queueing to untagged " 6082 "transition"); 6083#endif /* NEEDTOPORT */ 6084 6085 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6086 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6087 set_ua = 1; 6088 } else { 6089#ifdef NEEDTOPORT 6090 csevent_log(CSC_CTL | CSC_SHELF_SW | 6091 CTL_TAG_TO_TAG, 6092 csevent_LogType_Trace, 6093 csevent_Severity_Information, 6094 csevent_AlertLevel_Green, 6095 csevent_FRU_Firmware, 6096 csevent_FRU_Unknown, 6097 "Received tagged queueing to tagged " 6098 "queueing transition"); 6099#endif /* NEEDTOPORT */ 6100 } 6101 } 6102 if (set_ua != 0) { 6103 int i; 6104 /* 6105 * Let other initiators know that the mode 6106 * parameters for this LUN have changed. 6107 */ 6108 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6109 if (i == initidx) 6110 continue; 6111 6112 lun->pending_sense[i].ua_pending |= 6113 CTL_UA_MODE_CHANGE; 6114 } 6115 } 6116 mtx_unlock(&softc->ctl_lock); 6117 6118 return (0); 6119} 6120 6121int 6122ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6123 struct ctl_page_index *page_index, uint8_t *page_ptr) 6124{ 6125 return (0); 6126} 6127 6128int 6129ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6130 struct ctl_page_index *page_index, int pc) 6131{ 6132 struct copan_power_subpage *page; 6133 6134 page = (struct copan_power_subpage *)page_index->page_data + 6135 (page_index->page_len * pc); 6136 6137 switch (pc) { 6138 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6139 /* 6140 * We don't update the changable bits for this page. 6141 */ 6142 break; 6143 case SMS_PAGE_CTRL_CURRENT >> 6: 6144 case SMS_PAGE_CTRL_DEFAULT >> 6: 6145 case SMS_PAGE_CTRL_SAVED >> 6: 6146#ifdef NEEDTOPORT 6147 ctl_update_power_subpage(page); 6148#endif 6149 break; 6150 default: 6151#ifdef NEEDTOPORT 6152 EPRINT(0, "Invalid PC %d!!", pc); 6153#endif 6154 break; 6155 } 6156 return (0); 6157} 6158 6159 6160int 6161ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6162 struct ctl_page_index *page_index, uint8_t *page_ptr) 6163{ 6164 struct copan_aps_subpage *user_sp; 6165 struct copan_aps_subpage *current_sp; 6166 union ctl_modepage_info *modepage_info; 6167 struct ctl_softc *softc; 6168 struct ctl_lun *lun; 6169 int retval; 6170 6171 retval = CTL_RETVAL_COMPLETE; 6172 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6173 (page_index->page_len * CTL_PAGE_CURRENT)); 6174 softc = control_softc; 6175 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6176 6177 user_sp = (struct copan_aps_subpage *)page_ptr; 6178 6179 modepage_info = (union ctl_modepage_info *) 6180 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6181 6182 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6183 modepage_info->header.subpage = page_index->subpage; 6184 modepage_info->aps.lock_active = user_sp->lock_active; 6185 6186 mtx_lock(&softc->ctl_lock); 6187 6188 /* 6189 * If there is a request to lock the LUN and another LUN is locked 6190 * this is an error. If the requested LUN is already locked ignore 6191 * the request. If no LUN is locked attempt to lock it. 6192 * if there is a request to unlock the LUN and the LUN is currently 6193 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6194 * if another LUN is locked or no LUN is locked. 6195 */ 6196 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6197 if (softc->aps_locked_lun == lun->lun) { 6198 /* 6199 * This LUN is already locked, so we're done. 6200 */ 6201 retval = CTL_RETVAL_COMPLETE; 6202 } else if (softc->aps_locked_lun == 0) { 6203 /* 6204 * No one has the lock, pass the request to the 6205 * backend. 6206 */ 6207 retval = lun->backend->config_write( 6208 (union ctl_io *)ctsio); 6209 } else { 6210 /* 6211 * Someone else has the lock, throw out the request. 6212 */ 6213 ctl_set_already_locked(ctsio); 6214 free(ctsio->kern_data_ptr, M_CTL); 6215 ctl_done((union ctl_io *)ctsio); 6216 6217 /* 6218 * Set the return value so that ctl_do_mode_select() 6219 * won't try to complete the command. We already 6220 * completed it here. 6221 */ 6222 retval = CTL_RETVAL_ERROR; 6223 } 6224 } else if (softc->aps_locked_lun == lun->lun) { 6225 /* 6226 * This LUN is locked, so pass the unlock request to the 6227 * backend. 6228 */ 6229 retval = lun->backend->config_write((union ctl_io *)ctsio); 6230 } 6231 mtx_unlock(&softc->ctl_lock); 6232 6233 return (retval); 6234} 6235 6236int 6237ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6238 struct ctl_page_index *page_index, 6239 uint8_t *page_ptr) 6240{ 6241 uint8_t *c; 6242 int i; 6243 6244 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6245 ctl_time_io_secs = 6246 (c[0] << 8) | 6247 (c[1] << 0) | 6248 0; 6249 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6250 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6251 printf("page data:"); 6252 for (i=0; i<8; i++) 6253 printf(" %.2x",page_ptr[i]); 6254 printf("\n"); 6255 return (0); 6256} 6257 6258int 6259ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6260 struct ctl_page_index *page_index, 6261 int pc) 6262{ 6263 struct copan_debugconf_subpage *page; 6264 6265 page = (struct copan_debugconf_subpage *)page_index->page_data + 6266 (page_index->page_len * pc); 6267 6268 switch (pc) { 6269 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6270 case SMS_PAGE_CTRL_DEFAULT >> 6: 6271 case SMS_PAGE_CTRL_SAVED >> 6: 6272 /* 6273 * We don't update the changable or default bits for this page. 6274 */ 6275 break; 6276 case SMS_PAGE_CTRL_CURRENT >> 6: 6277 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6278 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6279 break; 6280 default: 6281#ifdef NEEDTOPORT 6282 EPRINT(0, "Invalid PC %d!!", pc); 6283#endif /* NEEDTOPORT */ 6284 break; 6285 } 6286 return (0); 6287} 6288 6289 6290static int 6291ctl_do_mode_select(union ctl_io *io) 6292{ 6293 struct scsi_mode_page_header *page_header; 6294 struct ctl_page_index *page_index; 6295 struct ctl_scsiio *ctsio; 6296 int control_dev, page_len; 6297 int page_len_offset, page_len_size; 6298 union ctl_modepage_info *modepage_info; 6299 struct ctl_lun *lun; 6300 int *len_left, *len_used; 6301 int retval, i; 6302 6303 ctsio = &io->scsiio; 6304 page_index = NULL; 6305 page_len = 0; 6306 retval = CTL_RETVAL_COMPLETE; 6307 6308 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6309 6310 if (lun->be_lun->lun_type != T_DIRECT) 6311 control_dev = 1; 6312 else 6313 control_dev = 0; 6314 6315 modepage_info = (union ctl_modepage_info *) 6316 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6317 len_left = &modepage_info->header.len_left; 6318 len_used = &modepage_info->header.len_used; 6319 6320do_next_page: 6321 6322 page_header = (struct scsi_mode_page_header *) 6323 (ctsio->kern_data_ptr + *len_used); 6324 6325 if (*len_left == 0) { 6326 free(ctsio->kern_data_ptr, M_CTL); 6327 ctl_set_success(ctsio); 6328 ctl_done((union ctl_io *)ctsio); 6329 return (CTL_RETVAL_COMPLETE); 6330 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6331 6332 free(ctsio->kern_data_ptr, M_CTL); 6333 ctl_set_param_len_error(ctsio); 6334 ctl_done((union ctl_io *)ctsio); 6335 return (CTL_RETVAL_COMPLETE); 6336 6337 } else if ((page_header->page_code & SMPH_SPF) 6338 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6339 6340 free(ctsio->kern_data_ptr, M_CTL); 6341 ctl_set_param_len_error(ctsio); 6342 ctl_done((union ctl_io *)ctsio); 6343 return (CTL_RETVAL_COMPLETE); 6344 } 6345 6346 6347 /* 6348 * XXX KDM should we do something with the block descriptor? 6349 */ 6350 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6351 6352 if ((control_dev != 0) 6353 && (lun->mode_pages.index[i].page_flags & 6354 CTL_PAGE_FLAG_DISK_ONLY)) 6355 continue; 6356 6357 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6358 (page_header->page_code & SMPH_PC_MASK)) 6359 continue; 6360 6361 /* 6362 * If neither page has a subpage code, then we've got a 6363 * match. 6364 */ 6365 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6366 && ((page_header->page_code & SMPH_SPF) == 0)) { 6367 page_index = &lun->mode_pages.index[i]; 6368 page_len = page_header->page_length; 6369 break; 6370 } 6371 6372 /* 6373 * If both pages have subpages, then the subpage numbers 6374 * have to match. 6375 */ 6376 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6377 && (page_header->page_code & SMPH_SPF)) { 6378 struct scsi_mode_page_header_sp *sph; 6379 6380 sph = (struct scsi_mode_page_header_sp *)page_header; 6381 6382 if (lun->mode_pages.index[i].subpage == 6383 sph->subpage) { 6384 page_index = &lun->mode_pages.index[i]; 6385 page_len = scsi_2btoul(sph->page_length); 6386 break; 6387 } 6388 } 6389 } 6390 6391 /* 6392 * If we couldn't find the page, or if we don't have a mode select 6393 * handler for it, send back an error to the user. 6394 */ 6395 if ((page_index == NULL) 6396 || (page_index->select_handler == NULL)) { 6397 ctl_set_invalid_field(ctsio, 6398 /*sks_valid*/ 1, 6399 /*command*/ 0, 6400 /*field*/ *len_used, 6401 /*bit_valid*/ 0, 6402 /*bit*/ 0); 6403 free(ctsio->kern_data_ptr, M_CTL); 6404 ctl_done((union ctl_io *)ctsio); 6405 return (CTL_RETVAL_COMPLETE); 6406 } 6407 6408 if (page_index->page_code & SMPH_SPF) { 6409 page_len_offset = 2; 6410 page_len_size = 2; 6411 } else { 6412 page_len_size = 1; 6413 page_len_offset = 1; 6414 } 6415 6416 /* 6417 * If the length the initiator gives us isn't the one we specify in 6418 * the mode page header, or if they didn't specify enough data in 6419 * the CDB to avoid truncating this page, kick out the request. 6420 */ 6421 if ((page_len != (page_index->page_len - page_len_offset - 6422 page_len_size)) 6423 || (*len_left < page_index->page_len)) { 6424 6425 6426 ctl_set_invalid_field(ctsio, 6427 /*sks_valid*/ 1, 6428 /*command*/ 0, 6429 /*field*/ *len_used + page_len_offset, 6430 /*bit_valid*/ 0, 6431 /*bit*/ 0); 6432 free(ctsio->kern_data_ptr, M_CTL); 6433 ctl_done((union ctl_io *)ctsio); 6434 return (CTL_RETVAL_COMPLETE); 6435 } 6436 6437 /* 6438 * Run through the mode page, checking to make sure that the bits 6439 * the user changed are actually legal for him to change. 6440 */ 6441 for (i = 0; i < page_index->page_len; i++) { 6442 uint8_t *user_byte, *change_mask, *current_byte; 6443 int bad_bit; 6444 int j; 6445 6446 user_byte = (uint8_t *)page_header + i; 6447 change_mask = page_index->page_data + 6448 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6449 current_byte = page_index->page_data + 6450 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6451 6452 /* 6453 * Check to see whether the user set any bits in this byte 6454 * that he is not allowed to set. 6455 */ 6456 if ((*user_byte & ~(*change_mask)) == 6457 (*current_byte & ~(*change_mask))) 6458 continue; 6459 6460 /* 6461 * Go through bit by bit to determine which one is illegal. 6462 */ 6463 bad_bit = 0; 6464 for (j = 7; j >= 0; j--) { 6465 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6466 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6467 bad_bit = i; 6468 break; 6469 } 6470 } 6471 ctl_set_invalid_field(ctsio, 6472 /*sks_valid*/ 1, 6473 /*command*/ 0, 6474 /*field*/ *len_used + i, 6475 /*bit_valid*/ 1, 6476 /*bit*/ bad_bit); 6477 free(ctsio->kern_data_ptr, M_CTL); 6478 ctl_done((union ctl_io *)ctsio); 6479 return (CTL_RETVAL_COMPLETE); 6480 } 6481 6482 /* 6483 * Decrement these before we call the page handler, since we may 6484 * end up getting called back one way or another before the handler 6485 * returns to this context. 6486 */ 6487 *len_left -= page_index->page_len; 6488 *len_used += page_index->page_len; 6489 6490 retval = page_index->select_handler(ctsio, page_index, 6491 (uint8_t *)page_header); 6492 6493 /* 6494 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6495 * wait until this queued command completes to finish processing 6496 * the mode page. If it returns anything other than 6497 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6498 * already set the sense information, freed the data pointer, and 6499 * completed the io for us. 6500 */ 6501 if (retval != CTL_RETVAL_COMPLETE) 6502 goto bailout_no_done; 6503 6504 /* 6505 * If the initiator sent us more than one page, parse the next one. 6506 */ 6507 if (*len_left > 0) 6508 goto do_next_page; 6509 6510 ctl_set_success(ctsio); 6511 free(ctsio->kern_data_ptr, M_CTL); 6512 ctl_done((union ctl_io *)ctsio); 6513 6514bailout_no_done: 6515 6516 return (CTL_RETVAL_COMPLETE); 6517 6518} 6519 6520int 6521ctl_mode_select(struct ctl_scsiio *ctsio) 6522{ 6523 int param_len, pf, sp; 6524 int header_size, bd_len; 6525 int len_left, len_used; 6526 struct ctl_page_index *page_index; 6527 struct ctl_lun *lun; 6528 int control_dev, page_len; 6529 union ctl_modepage_info *modepage_info; 6530 int retval; 6531 6532 pf = 0; 6533 sp = 0; 6534 page_len = 0; 6535 len_used = 0; 6536 len_left = 0; 6537 retval = 0; 6538 bd_len = 0; 6539 page_index = NULL; 6540 6541 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6542 6543 if (lun->be_lun->lun_type != T_DIRECT) 6544 control_dev = 1; 6545 else 6546 control_dev = 0; 6547 6548 switch (ctsio->cdb[0]) { 6549 case MODE_SELECT_6: { 6550 struct scsi_mode_select_6 *cdb; 6551 6552 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6553 6554 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6555 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6556 6557 param_len = cdb->length; 6558 header_size = sizeof(struct scsi_mode_header_6); 6559 break; 6560 } 6561 case MODE_SELECT_10: { 6562 struct scsi_mode_select_10 *cdb; 6563 6564 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6565 6566 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6567 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6568 6569 param_len = scsi_2btoul(cdb->length); 6570 header_size = sizeof(struct scsi_mode_header_10); 6571 break; 6572 } 6573 default: 6574 ctl_set_invalid_opcode(ctsio); 6575 ctl_done((union ctl_io *)ctsio); 6576 return (CTL_RETVAL_COMPLETE); 6577 break; /* NOTREACHED */ 6578 } 6579 6580 /* 6581 * From SPC-3: 6582 * "A parameter list length of zero indicates that the Data-Out Buffer 6583 * shall be empty. This condition shall not be considered as an error." 6584 */ 6585 if (param_len == 0) { 6586 ctl_set_success(ctsio); 6587 ctl_done((union ctl_io *)ctsio); 6588 return (CTL_RETVAL_COMPLETE); 6589 } 6590 6591 /* 6592 * Since we'll hit this the first time through, prior to 6593 * allocation, we don't need to free a data buffer here. 6594 */ 6595 if (param_len < header_size) { 6596 ctl_set_param_len_error(ctsio); 6597 ctl_done((union ctl_io *)ctsio); 6598 return (CTL_RETVAL_COMPLETE); 6599 } 6600 6601 /* 6602 * Allocate the data buffer and grab the user's data. In theory, 6603 * we shouldn't have to sanity check the parameter list length here 6604 * because the maximum size is 64K. We should be able to malloc 6605 * that much without too many problems. 6606 */ 6607 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6608 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6609 ctsio->kern_data_len = param_len; 6610 ctsio->kern_total_len = param_len; 6611 ctsio->kern_data_resid = 0; 6612 ctsio->kern_rel_offset = 0; 6613 ctsio->kern_sg_entries = 0; 6614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6615 ctsio->be_move_done = ctl_config_move_done; 6616 ctl_datamove((union ctl_io *)ctsio); 6617 6618 return (CTL_RETVAL_COMPLETE); 6619 } 6620 6621 switch (ctsio->cdb[0]) { 6622 case MODE_SELECT_6: { 6623 struct scsi_mode_header_6 *mh6; 6624 6625 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6626 bd_len = mh6->blk_desc_len; 6627 break; 6628 } 6629 case MODE_SELECT_10: { 6630 struct scsi_mode_header_10 *mh10; 6631 6632 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6633 bd_len = scsi_2btoul(mh10->blk_desc_len); 6634 break; 6635 } 6636 default: 6637 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6638 break; 6639 } 6640 6641 if (param_len < (header_size + bd_len)) { 6642 free(ctsio->kern_data_ptr, M_CTL); 6643 ctl_set_param_len_error(ctsio); 6644 ctl_done((union ctl_io *)ctsio); 6645 return (CTL_RETVAL_COMPLETE); 6646 } 6647 6648 /* 6649 * Set the IO_CONT flag, so that if this I/O gets passed to 6650 * ctl_config_write_done(), it'll get passed back to 6651 * ctl_do_mode_select() for further processing, or completion if 6652 * we're all done. 6653 */ 6654 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6655 ctsio->io_cont = ctl_do_mode_select; 6656 6657 modepage_info = (union ctl_modepage_info *) 6658 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6659 6660 memset(modepage_info, 0, sizeof(*modepage_info)); 6661 6662 len_left = param_len - header_size - bd_len; 6663 len_used = header_size + bd_len; 6664 6665 modepage_info->header.len_left = len_left; 6666 modepage_info->header.len_used = len_used; 6667 6668 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6669} 6670 6671int 6672ctl_mode_sense(struct ctl_scsiio *ctsio) 6673{ 6674 struct ctl_lun *lun; 6675 int pc, page_code, dbd, llba, subpage; 6676 int alloc_len, page_len, header_len, total_len; 6677 struct scsi_mode_block_descr *block_desc; 6678 struct ctl_page_index *page_index; 6679 int control_dev; 6680 6681 dbd = 0; 6682 llba = 0; 6683 block_desc = NULL; 6684 page_index = NULL; 6685 6686 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6687 6688 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6689 6690 if (lun->be_lun->lun_type != T_DIRECT) 6691 control_dev = 1; 6692 else 6693 control_dev = 0; 6694 6695 switch (ctsio->cdb[0]) { 6696 case MODE_SENSE_6: { 6697 struct scsi_mode_sense_6 *cdb; 6698 6699 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6700 6701 header_len = sizeof(struct scsi_mode_hdr_6); 6702 if (cdb->byte2 & SMS_DBD) 6703 dbd = 1; 6704 else 6705 header_len += sizeof(struct scsi_mode_block_descr); 6706 6707 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6708 page_code = cdb->page & SMS_PAGE_CODE; 6709 subpage = cdb->subpage; 6710 alloc_len = cdb->length; 6711 break; 6712 } 6713 case MODE_SENSE_10: { 6714 struct scsi_mode_sense_10 *cdb; 6715 6716 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6717 6718 header_len = sizeof(struct scsi_mode_hdr_10); 6719 6720 if (cdb->byte2 & SMS_DBD) 6721 dbd = 1; 6722 else 6723 header_len += sizeof(struct scsi_mode_block_descr); 6724 if (cdb->byte2 & SMS10_LLBAA) 6725 llba = 1; 6726 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6727 page_code = cdb->page & SMS_PAGE_CODE; 6728 subpage = cdb->subpage; 6729 alloc_len = scsi_2btoul(cdb->length); 6730 break; 6731 } 6732 default: 6733 ctl_set_invalid_opcode(ctsio); 6734 ctl_done((union ctl_io *)ctsio); 6735 return (CTL_RETVAL_COMPLETE); 6736 break; /* NOTREACHED */ 6737 } 6738 6739 /* 6740 * We have to make a first pass through to calculate the size of 6741 * the pages that match the user's query. Then we allocate enough 6742 * memory to hold it, and actually copy the data into the buffer. 6743 */ 6744 switch (page_code) { 6745 case SMS_ALL_PAGES_PAGE: { 6746 int i; 6747 6748 page_len = 0; 6749 6750 /* 6751 * At the moment, values other than 0 and 0xff here are 6752 * reserved according to SPC-3. 6753 */ 6754 if ((subpage != SMS_SUBPAGE_PAGE_0) 6755 && (subpage != SMS_SUBPAGE_ALL)) { 6756 ctl_set_invalid_field(ctsio, 6757 /*sks_valid*/ 1, 6758 /*command*/ 1, 6759 /*field*/ 3, 6760 /*bit_valid*/ 0, 6761 /*bit*/ 0); 6762 ctl_done((union ctl_io *)ctsio); 6763 return (CTL_RETVAL_COMPLETE); 6764 } 6765 6766 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6767 if ((control_dev != 0) 6768 && (lun->mode_pages.index[i].page_flags & 6769 CTL_PAGE_FLAG_DISK_ONLY)) 6770 continue; 6771 6772 /* 6773 * We don't use this subpage if the user didn't 6774 * request all subpages. 6775 */ 6776 if ((lun->mode_pages.index[i].subpage != 0) 6777 && (subpage == SMS_SUBPAGE_PAGE_0)) 6778 continue; 6779 6780#if 0 6781 printf("found page %#x len %d\n", 6782 lun->mode_pages.index[i].page_code & 6783 SMPH_PC_MASK, 6784 lun->mode_pages.index[i].page_len); 6785#endif 6786 page_len += lun->mode_pages.index[i].page_len; 6787 } 6788 break; 6789 } 6790 default: { 6791 int i; 6792 6793 page_len = 0; 6794 6795 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6796 /* Look for the right page code */ 6797 if ((lun->mode_pages.index[i].page_code & 6798 SMPH_PC_MASK) != page_code) 6799 continue; 6800 6801 /* Look for the right subpage or the subpage wildcard*/ 6802 if ((lun->mode_pages.index[i].subpage != subpage) 6803 && (subpage != SMS_SUBPAGE_ALL)) 6804 continue; 6805 6806 /* Make sure the page is supported for this dev type */ 6807 if ((control_dev != 0) 6808 && (lun->mode_pages.index[i].page_flags & 6809 CTL_PAGE_FLAG_DISK_ONLY)) 6810 continue; 6811 6812#if 0 6813 printf("found page %#x len %d\n", 6814 lun->mode_pages.index[i].page_code & 6815 SMPH_PC_MASK, 6816 lun->mode_pages.index[i].page_len); 6817#endif 6818 6819 page_len += lun->mode_pages.index[i].page_len; 6820 } 6821 6822 if (page_len == 0) { 6823 ctl_set_invalid_field(ctsio, 6824 /*sks_valid*/ 1, 6825 /*command*/ 1, 6826 /*field*/ 2, 6827 /*bit_valid*/ 1, 6828 /*bit*/ 5); 6829 ctl_done((union ctl_io *)ctsio); 6830 return (CTL_RETVAL_COMPLETE); 6831 } 6832 break; 6833 } 6834 } 6835 6836 total_len = header_len + page_len; 6837#if 0 6838 printf("header_len = %d, page_len = %d, total_len = %d\n", 6839 header_len, page_len, total_len); 6840#endif 6841 6842 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6843 ctsio->kern_sg_entries = 0; 6844 ctsio->kern_data_resid = 0; 6845 ctsio->kern_rel_offset = 0; 6846 if (total_len < alloc_len) { 6847 ctsio->residual = alloc_len - total_len; 6848 ctsio->kern_data_len = total_len; 6849 ctsio->kern_total_len = total_len; 6850 } else { 6851 ctsio->residual = 0; 6852 ctsio->kern_data_len = alloc_len; 6853 ctsio->kern_total_len = alloc_len; 6854 } 6855 6856 switch (ctsio->cdb[0]) { 6857 case MODE_SENSE_6: { 6858 struct scsi_mode_hdr_6 *header; 6859 6860 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6861 6862 header->datalen = ctl_min(total_len - 1, 254); 6863 6864 if (dbd) 6865 header->block_descr_len = 0; 6866 else 6867 header->block_descr_len = 6868 sizeof(struct scsi_mode_block_descr); 6869 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6870 break; 6871 } 6872 case MODE_SENSE_10: { 6873 struct scsi_mode_hdr_10 *header; 6874 int datalen; 6875 6876 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6877 6878 datalen = ctl_min(total_len - 2, 65533); 6879 scsi_ulto2b(datalen, header->datalen); 6880 if (dbd) 6881 scsi_ulto2b(0, header->block_descr_len); 6882 else 6883 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6884 header->block_descr_len); 6885 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6886 break; 6887 } 6888 default: 6889 panic("invalid CDB type %#x", ctsio->cdb[0]); 6890 break; /* NOTREACHED */ 6891 } 6892 6893 /* 6894 * If we've got a disk, use its blocksize in the block 6895 * descriptor. Otherwise, just set it to 0. 6896 */ 6897 if (dbd == 0) { 6898 if (control_dev != 0) 6899 scsi_ulto3b(lun->be_lun->blocksize, 6900 block_desc->block_len); 6901 else 6902 scsi_ulto3b(0, block_desc->block_len); 6903 } 6904 6905 switch (page_code) { 6906 case SMS_ALL_PAGES_PAGE: { 6907 int i, data_used; 6908 6909 data_used = header_len; 6910 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6911 struct ctl_page_index *page_index; 6912 6913 page_index = &lun->mode_pages.index[i]; 6914 6915 if ((control_dev != 0) 6916 && (page_index->page_flags & 6917 CTL_PAGE_FLAG_DISK_ONLY)) 6918 continue; 6919 6920 /* 6921 * We don't use this subpage if the user didn't 6922 * request all subpages. We already checked (above) 6923 * to make sure the user only specified a subpage 6924 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6925 */ 6926 if ((page_index->subpage != 0) 6927 && (subpage == SMS_SUBPAGE_PAGE_0)) 6928 continue; 6929 6930 /* 6931 * Call the handler, if it exists, to update the 6932 * page to the latest values. 6933 */ 6934 if (page_index->sense_handler != NULL) 6935 page_index->sense_handler(ctsio, page_index,pc); 6936 6937 memcpy(ctsio->kern_data_ptr + data_used, 6938 page_index->page_data + 6939 (page_index->page_len * pc), 6940 page_index->page_len); 6941 data_used += page_index->page_len; 6942 } 6943 break; 6944 } 6945 default: { 6946 int i, data_used; 6947 6948 data_used = header_len; 6949 6950 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6951 struct ctl_page_index *page_index; 6952 6953 page_index = &lun->mode_pages.index[i]; 6954 6955 /* Look for the right page code */ 6956 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6957 continue; 6958 6959 /* Look for the right subpage or the subpage wildcard*/ 6960 if ((page_index->subpage != subpage) 6961 && (subpage != SMS_SUBPAGE_ALL)) 6962 continue; 6963 6964 /* Make sure the page is supported for this dev type */ 6965 if ((control_dev != 0) 6966 && (page_index->page_flags & 6967 CTL_PAGE_FLAG_DISK_ONLY)) 6968 continue; 6969 6970 /* 6971 * Call the handler, if it exists, to update the 6972 * page to the latest values. 6973 */ 6974 if (page_index->sense_handler != NULL) 6975 page_index->sense_handler(ctsio, page_index,pc); 6976 6977 memcpy(ctsio->kern_data_ptr + data_used, 6978 page_index->page_data + 6979 (page_index->page_len * pc), 6980 page_index->page_len); 6981 data_used += page_index->page_len; 6982 } 6983 break; 6984 } 6985 } 6986 6987 ctsio->scsi_status = SCSI_STATUS_OK; 6988 6989 ctsio->be_move_done = ctl_config_move_done; 6990 ctl_datamove((union ctl_io *)ctsio); 6991 6992 return (CTL_RETVAL_COMPLETE); 6993} 6994 6995int 6996ctl_read_capacity(struct ctl_scsiio *ctsio) 6997{ 6998 struct scsi_read_capacity *cdb; 6999 struct scsi_read_capacity_data *data; 7000 struct ctl_lun *lun; 7001 uint32_t lba; 7002 7003 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7004 7005 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7006 7007 lba = scsi_4btoul(cdb->addr); 7008 if (((cdb->pmi & SRC_PMI) == 0) 7009 && (lba != 0)) { 7010 ctl_set_invalid_field(/*ctsio*/ ctsio, 7011 /*sks_valid*/ 1, 7012 /*command*/ 1, 7013 /*field*/ 2, 7014 /*bit_valid*/ 0, 7015 /*bit*/ 0); 7016 ctl_done((union ctl_io *)ctsio); 7017 return (CTL_RETVAL_COMPLETE); 7018 } 7019 7020 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7021 7022 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7023 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7024 ctsio->residual = 0; 7025 ctsio->kern_data_len = sizeof(*data); 7026 ctsio->kern_total_len = sizeof(*data); 7027 ctsio->kern_data_resid = 0; 7028 ctsio->kern_rel_offset = 0; 7029 ctsio->kern_sg_entries = 0; 7030 7031 /* 7032 * If the maximum LBA is greater than 0xfffffffe, the user must 7033 * issue a SERVICE ACTION IN (16) command, with the read capacity 7034 * serivce action set. 7035 */ 7036 if (lun->be_lun->maxlba > 0xfffffffe) 7037 scsi_ulto4b(0xffffffff, data->addr); 7038 else 7039 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7040 7041 /* 7042 * XXX KDM this may not be 512 bytes... 7043 */ 7044 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7045 7046 ctsio->scsi_status = SCSI_STATUS_OK; 7047 7048 ctsio->be_move_done = ctl_config_move_done; 7049 ctl_datamove((union ctl_io *)ctsio); 7050 7051 return (CTL_RETVAL_COMPLETE); 7052} 7053 7054static int 7055ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7056{ 7057 struct scsi_read_capacity_16 *cdb; 7058 struct scsi_read_capacity_data_long *data; 7059 struct ctl_lun *lun; 7060 uint64_t lba; 7061 uint32_t alloc_len; 7062 7063 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7064 7065 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7066 7067 alloc_len = scsi_4btoul(cdb->alloc_len); 7068 lba = scsi_8btou64(cdb->addr); 7069 7070 if ((cdb->reladr & SRC16_PMI) 7071 && (lba != 0)) { 7072 ctl_set_invalid_field(/*ctsio*/ ctsio, 7073 /*sks_valid*/ 1, 7074 /*command*/ 1, 7075 /*field*/ 2, 7076 /*bit_valid*/ 0, 7077 /*bit*/ 0); 7078 ctl_done((union ctl_io *)ctsio); 7079 return (CTL_RETVAL_COMPLETE); 7080 } 7081 7082 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7083 7084 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7085 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7086 7087 if (sizeof(*data) < alloc_len) { 7088 ctsio->residual = alloc_len - sizeof(*data); 7089 ctsio->kern_data_len = sizeof(*data); 7090 ctsio->kern_total_len = sizeof(*data); 7091 } else { 7092 ctsio->residual = 0; 7093 ctsio->kern_data_len = alloc_len; 7094 ctsio->kern_total_len = alloc_len; 7095 } 7096 ctsio->kern_data_resid = 0; 7097 ctsio->kern_rel_offset = 0; 7098 ctsio->kern_sg_entries = 0; 7099 7100 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7101 /* XXX KDM this may not be 512 bytes... */ 7102 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7103 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7104 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7105 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7106 data->lalba_lbp[0] |= SRC16_LBPME; 7107 7108 ctsio->scsi_status = SCSI_STATUS_OK; 7109 7110 ctsio->be_move_done = ctl_config_move_done; 7111 ctl_datamove((union ctl_io *)ctsio); 7112 7113 return (CTL_RETVAL_COMPLETE); 7114} 7115 7116int 7117ctl_service_action_in(struct ctl_scsiio *ctsio) 7118{ 7119 struct scsi_service_action_in *cdb; 7120 int retval; 7121 7122 CTL_DEBUG_PRINT(("ctl_service_action_in\n")); 7123 7124 cdb = (struct scsi_service_action_in *)ctsio->cdb; 7125 7126 retval = CTL_RETVAL_COMPLETE; 7127 7128 switch (cdb->service_action) { 7129 case SRC16_SERVICE_ACTION: 7130 retval = ctl_read_capacity_16(ctsio); 7131 break; 7132 default: 7133 ctl_set_invalid_field(/*ctsio*/ ctsio, 7134 /*sks_valid*/ 1, 7135 /*command*/ 1, 7136 /*field*/ 1, 7137 /*bit_valid*/ 1, 7138 /*bit*/ 4); 7139 ctl_done((union ctl_io *)ctsio); 7140 break; 7141 } 7142 7143 return (retval); 7144} 7145 7146int 7147ctl_maintenance_in(struct ctl_scsiio *ctsio) 7148{ 7149 struct scsi_maintenance_in *cdb; 7150 int retval; 7151 int alloc_len, total_len = 0; 7152 int num_target_port_groups, single; 7153 struct ctl_lun *lun; 7154 struct ctl_softc *softc; 7155 struct scsi_target_group_data *rtg_ptr; 7156 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2; 7157 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2, 7158 *tp_desc_ptr2_1, *tp_desc_ptr2_2; 7159 7160 CTL_DEBUG_PRINT(("ctl_maintenance_in\n")); 7161 7162 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7163 softc = control_softc; 7164 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7165 7166 retval = CTL_RETVAL_COMPLETE; 7167 7168 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) { 7169 ctl_set_invalid_field(/*ctsio*/ ctsio, 7170 /*sks_valid*/ 1, 7171 /*command*/ 1, 7172 /*field*/ 1, 7173 /*bit_valid*/ 1, 7174 /*bit*/ 4); 7175 ctl_done((union ctl_io *)ctsio); 7176 return(retval); 7177 } 7178 7179 mtx_lock(&softc->ctl_lock); 7180 single = ctl_is_single; 7181 mtx_unlock(&softc->ctl_lock); 7182 7183 if (single) 7184 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1; 7185 else 7186 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7187 7188 total_len = sizeof(struct scsi_target_group_data) + 7189 sizeof(struct scsi_target_port_group_descriptor) * 7190 num_target_port_groups + 7191 sizeof(struct scsi_target_port_descriptor) * 7192 NUM_PORTS_PER_GRP * num_target_port_groups; 7193 7194 alloc_len = scsi_4btoul(cdb->length); 7195 7196 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7197 7198 ctsio->kern_sg_entries = 0; 7199 7200 if (total_len < alloc_len) { 7201 ctsio->residual = alloc_len - total_len; 7202 ctsio->kern_data_len = total_len; 7203 ctsio->kern_total_len = total_len; 7204 } else { 7205 ctsio->residual = 0; 7206 ctsio->kern_data_len = alloc_len; 7207 ctsio->kern_total_len = alloc_len; 7208 } 7209 ctsio->kern_data_resid = 0; 7210 ctsio->kern_rel_offset = 0; 7211 7212 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr; 7213 7214 tpg_desc_ptr1 = &rtg_ptr->groups[0]; 7215 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0]; 7216 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *) 7217 &tp_desc_ptr1_1->desc_list[0]; 7218 7219 if (single == 0) { 7220 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *) 7221 &tp_desc_ptr1_2->desc_list[0]; 7222 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0]; 7223 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *) 7224 &tp_desc_ptr2_1->desc_list[0]; 7225 } else { 7226 tpg_desc_ptr2 = NULL; 7227 tp_desc_ptr2_1 = NULL; 7228 tp_desc_ptr2_2 = NULL; 7229 } 7230 7231 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7232 if (single == 0) { 7233 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7234 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7235 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7236 tpg_desc_ptr2->pref_state = 7237 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7238 } else { 7239 tpg_desc_ptr1->pref_state = 7240 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7241 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7242 } 7243 } else { 7244 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7245 tpg_desc_ptr1->pref_state = 7246 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7247 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7248 } else { 7249 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7250 tpg_desc_ptr2->pref_state = 7251 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7252 } 7253 } 7254 } else { 7255 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7256 } 7257 tpg_desc_ptr1->support = 0; 7258 tpg_desc_ptr1->target_port_group[1] = 1; 7259 tpg_desc_ptr1->status = TPG_IMPLICIT; 7260 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP; 7261 7262 if (single == 0) { 7263 tpg_desc_ptr2->support = 0; 7264 tpg_desc_ptr2->target_port_group[1] = 2; 7265 tpg_desc_ptr2->status = TPG_IMPLICIT; 7266 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP; 7267 7268 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7269 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7270 7271 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9; 7272 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10; 7273 } else { 7274 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7275 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7276 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7277 } else { 7278 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9; 7279 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10; 7280 } 7281 } 7282 7283 ctsio->be_move_done = ctl_config_move_done; 7284 7285 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7286 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7287 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7288 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7289 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7290 7291 ctl_datamove((union ctl_io *)ctsio); 7292 return(retval); 7293} 7294 7295int 7296ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7297{ 7298 struct scsi_per_res_in *cdb; 7299 int alloc_len, total_len = 0; 7300 /* struct scsi_per_res_in_rsrv in_data; */ 7301 struct ctl_lun *lun; 7302 struct ctl_softc *softc; 7303 7304 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7305 7306 softc = control_softc; 7307 7308 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7309 7310 alloc_len = scsi_2btoul(cdb->length); 7311 7312 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7313 7314retry: 7315 mtx_lock(&softc->ctl_lock); 7316 switch (cdb->action) { 7317 case SPRI_RK: /* read keys */ 7318 total_len = sizeof(struct scsi_per_res_in_keys) + 7319 lun->pr_key_count * 7320 sizeof(struct scsi_per_res_key); 7321 break; 7322 case SPRI_RR: /* read reservation */ 7323 if (lun->flags & CTL_LUN_PR_RESERVED) 7324 total_len = sizeof(struct scsi_per_res_in_rsrv); 7325 else 7326 total_len = sizeof(struct scsi_per_res_in_header); 7327 break; 7328 case SPRI_RC: /* report capabilities */ 7329 total_len = sizeof(struct scsi_per_res_cap); 7330 break; 7331 case SPRI_RS: /* read full status */ 7332 default: 7333 mtx_unlock(&softc->ctl_lock); 7334 ctl_set_invalid_field(ctsio, 7335 /*sks_valid*/ 1, 7336 /*command*/ 1, 7337 /*field*/ 1, 7338 /*bit_valid*/ 1, 7339 /*bit*/ 0); 7340 ctl_done((union ctl_io *)ctsio); 7341 return (CTL_RETVAL_COMPLETE); 7342 break; /* NOTREACHED */ 7343 } 7344 mtx_unlock(&softc->ctl_lock); 7345 7346 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7347 7348 if (total_len < alloc_len) { 7349 ctsio->residual = alloc_len - total_len; 7350 ctsio->kern_data_len = total_len; 7351 ctsio->kern_total_len = total_len; 7352 } else { 7353 ctsio->residual = 0; 7354 ctsio->kern_data_len = alloc_len; 7355 ctsio->kern_total_len = alloc_len; 7356 } 7357 7358 ctsio->kern_data_resid = 0; 7359 ctsio->kern_rel_offset = 0; 7360 ctsio->kern_sg_entries = 0; 7361 7362 mtx_lock(&softc->ctl_lock); 7363 switch (cdb->action) { 7364 case SPRI_RK: { // read keys 7365 struct scsi_per_res_in_keys *res_keys; 7366 int i, key_count; 7367 7368 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7369 7370 /* 7371 * We had to drop the lock to allocate our buffer, which 7372 * leaves time for someone to come in with another 7373 * persistent reservation. (That is unlikely, though, 7374 * since this should be the only persistent reservation 7375 * command active right now.) 7376 */ 7377 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7378 (lun->pr_key_count * 7379 sizeof(struct scsi_per_res_key)))){ 7380 mtx_unlock(&softc->ctl_lock); 7381 free(ctsio->kern_data_ptr, M_CTL); 7382 printf("%s: reservation length changed, retrying\n", 7383 __func__); 7384 goto retry; 7385 } 7386 7387 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7388 7389 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7390 lun->pr_key_count, res_keys->header.length); 7391 7392 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7393 if (!lun->per_res[i].registered) 7394 continue; 7395 7396 /* 7397 * We used lun->pr_key_count to calculate the 7398 * size to allocate. If it turns out the number of 7399 * initiators with the registered flag set is 7400 * larger than that (i.e. they haven't been kept in 7401 * sync), we've got a problem. 7402 */ 7403 if (key_count >= lun->pr_key_count) { 7404#ifdef NEEDTOPORT 7405 csevent_log(CSC_CTL | CSC_SHELF_SW | 7406 CTL_PR_ERROR, 7407 csevent_LogType_Fault, 7408 csevent_AlertLevel_Yellow, 7409 csevent_FRU_ShelfController, 7410 csevent_FRU_Firmware, 7411 csevent_FRU_Unknown, 7412 "registered keys %d >= key " 7413 "count %d", key_count, 7414 lun->pr_key_count); 7415#endif 7416 key_count++; 7417 continue; 7418 } 7419 memcpy(res_keys->keys[key_count].key, 7420 lun->per_res[i].res_key.key, 7421 ctl_min(sizeof(res_keys->keys[key_count].key), 7422 sizeof(lun->per_res[i].res_key))); 7423 key_count++; 7424 } 7425 break; 7426 } 7427 case SPRI_RR: { // read reservation 7428 struct scsi_per_res_in_rsrv *res; 7429 int tmp_len, header_only; 7430 7431 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7432 7433 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7434 7435 if (lun->flags & CTL_LUN_PR_RESERVED) 7436 { 7437 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7438 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7439 res->header.length); 7440 header_only = 0; 7441 } else { 7442 tmp_len = sizeof(struct scsi_per_res_in_header); 7443 scsi_ulto4b(0, res->header.length); 7444 header_only = 1; 7445 } 7446 7447 /* 7448 * We had to drop the lock to allocate our buffer, which 7449 * leaves time for someone to come in with another 7450 * persistent reservation. (That is unlikely, though, 7451 * since this should be the only persistent reservation 7452 * command active right now.) 7453 */ 7454 if (tmp_len != total_len) { 7455 mtx_unlock(&softc->ctl_lock); 7456 free(ctsio->kern_data_ptr, M_CTL); 7457 printf("%s: reservation status changed, retrying\n", 7458 __func__); 7459 goto retry; 7460 } 7461 7462 /* 7463 * No reservation held, so we're done. 7464 */ 7465 if (header_only != 0) 7466 break; 7467 7468 /* 7469 * If the registration is an All Registrants type, the key 7470 * is 0, since it doesn't really matter. 7471 */ 7472 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7473 memcpy(res->data.reservation, 7474 &lun->per_res[lun->pr_res_idx].res_key, 7475 sizeof(struct scsi_per_res_key)); 7476 } 7477 res->data.scopetype = lun->res_type; 7478 break; 7479 } 7480 case SPRI_RC: //report capabilities 7481 { 7482 struct scsi_per_res_cap *res_cap; 7483 uint16_t type_mask; 7484 7485 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7486 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7487 res_cap->flags2 |= SPRI_TMV; 7488 type_mask = SPRI_TM_WR_EX_AR | 7489 SPRI_TM_EX_AC_RO | 7490 SPRI_TM_WR_EX_RO | 7491 SPRI_TM_EX_AC | 7492 SPRI_TM_WR_EX | 7493 SPRI_TM_EX_AC_AR; 7494 scsi_ulto2b(type_mask, res_cap->type_mask); 7495 break; 7496 } 7497 case SPRI_RS: //read full status 7498 default: 7499 /* 7500 * This is a bug, because we just checked for this above, 7501 * and should have returned an error. 7502 */ 7503 panic("Invalid PR type %x", cdb->action); 7504 break; /* NOTREACHED */ 7505 } 7506 mtx_unlock(&softc->ctl_lock); 7507 7508 ctsio->be_move_done = ctl_config_move_done; 7509 7510 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7511 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7512 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7513 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7514 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7515 7516 ctl_datamove((union ctl_io *)ctsio); 7517 7518 return (CTL_RETVAL_COMPLETE); 7519} 7520 7521/* 7522 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7523 * it should return. 7524 */ 7525static int 7526ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7527 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7528 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7529 struct scsi_per_res_out_parms* param) 7530{ 7531 union ctl_ha_msg persis_io; 7532 int retval, i; 7533 int isc_retval; 7534 7535 retval = 0; 7536 7537 if (sa_res_key == 0) { 7538 mtx_lock(&softc->ctl_lock); 7539 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7540 /* validate scope and type */ 7541 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7542 SPR_LU_SCOPE) { 7543 mtx_unlock(&softc->ctl_lock); 7544 ctl_set_invalid_field(/*ctsio*/ ctsio, 7545 /*sks_valid*/ 1, 7546 /*command*/ 1, 7547 /*field*/ 2, 7548 /*bit_valid*/ 1, 7549 /*bit*/ 4); 7550 ctl_done((union ctl_io *)ctsio); 7551 return (1); 7552 } 7553 7554 if (type>8 || type==2 || type==4 || type==0) { 7555 mtx_unlock(&softc->ctl_lock); 7556 ctl_set_invalid_field(/*ctsio*/ ctsio, 7557 /*sks_valid*/ 1, 7558 /*command*/ 1, 7559 /*field*/ 2, 7560 /*bit_valid*/ 1, 7561 /*bit*/ 0); 7562 ctl_done((union ctl_io *)ctsio); 7563 return (1); 7564 } 7565 7566 /* temporarily unregister this nexus */ 7567 lun->per_res[residx].registered = 0; 7568 7569 /* 7570 * Unregister everybody else and build UA for 7571 * them 7572 */ 7573 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7574 if (lun->per_res[i].registered == 0) 7575 continue; 7576 7577 if (!persis_offset 7578 && i <CTL_MAX_INITIATORS) 7579 lun->pending_sense[i].ua_pending |= 7580 CTL_UA_REG_PREEMPT; 7581 else if (persis_offset 7582 && i >= persis_offset) 7583 lun->pending_sense[i-persis_offset 7584 ].ua_pending |= 7585 CTL_UA_REG_PREEMPT; 7586 lun->per_res[i].registered = 0; 7587 memset(&lun->per_res[i].res_key, 0, 7588 sizeof(struct scsi_per_res_key)); 7589 } 7590 lun->per_res[residx].registered = 1; 7591 lun->pr_key_count = 1; 7592 lun->res_type = type; 7593 if (lun->res_type != SPR_TYPE_WR_EX_AR 7594 && lun->res_type != SPR_TYPE_EX_AC_AR) 7595 lun->pr_res_idx = residx; 7596 7597 mtx_unlock(&softc->ctl_lock); 7598 /* send msg to other side */ 7599 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7600 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7601 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7602 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7603 persis_io.pr.pr_info.res_type = type; 7604 memcpy(persis_io.pr.pr_info.sa_res_key, 7605 param->serv_act_res_key, 7606 sizeof(param->serv_act_res_key)); 7607 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7608 &persis_io, sizeof(persis_io), 0)) > 7609 CTL_HA_STATUS_SUCCESS) { 7610 printf("CTL:Persis Out error returned " 7611 "from ctl_ha_msg_send %d\n", 7612 isc_retval); 7613 } 7614 } else { 7615 /* not all registrants */ 7616 mtx_unlock(&softc->ctl_lock); 7617 free(ctsio->kern_data_ptr, M_CTL); 7618 ctl_set_invalid_field(ctsio, 7619 /*sks_valid*/ 1, 7620 /*command*/ 0, 7621 /*field*/ 8, 7622 /*bit_valid*/ 0, 7623 /*bit*/ 0); 7624 ctl_done((union ctl_io *)ctsio); 7625 return (1); 7626 } 7627 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7628 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7629 int found = 0; 7630 7631 mtx_lock(&softc->ctl_lock); 7632 if (res_key == sa_res_key) { 7633 /* special case */ 7634 /* 7635 * The spec implies this is not good but doesn't 7636 * say what to do. There are two choices either 7637 * generate a res conflict or check condition 7638 * with illegal field in parameter data. Since 7639 * that is what is done when the sa_res_key is 7640 * zero I'll take that approach since this has 7641 * to do with the sa_res_key. 7642 */ 7643 mtx_unlock(&softc->ctl_lock); 7644 free(ctsio->kern_data_ptr, M_CTL); 7645 ctl_set_invalid_field(ctsio, 7646 /*sks_valid*/ 1, 7647 /*command*/ 0, 7648 /*field*/ 8, 7649 /*bit_valid*/ 0, 7650 /*bit*/ 0); 7651 ctl_done((union ctl_io *)ctsio); 7652 return (1); 7653 } 7654 7655 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7656 if (lun->per_res[i].registered 7657 && memcmp(param->serv_act_res_key, 7658 lun->per_res[i].res_key.key, 7659 sizeof(struct scsi_per_res_key)) != 0) 7660 continue; 7661 7662 found = 1; 7663 lun->per_res[i].registered = 0; 7664 memset(&lun->per_res[i].res_key, 0, 7665 sizeof(struct scsi_per_res_key)); 7666 lun->pr_key_count--; 7667 7668 if (!persis_offset 7669 && i < CTL_MAX_INITIATORS) 7670 lun->pending_sense[i].ua_pending |= 7671 CTL_UA_REG_PREEMPT; 7672 else if (persis_offset 7673 && i >= persis_offset) 7674 lun->pending_sense[i-persis_offset].ua_pending|= 7675 CTL_UA_REG_PREEMPT; 7676 } 7677 mtx_unlock(&softc->ctl_lock); 7678 if (!found) { 7679 free(ctsio->kern_data_ptr, M_CTL); 7680 ctl_set_reservation_conflict(ctsio); 7681 ctl_done((union ctl_io *)ctsio); 7682 return (CTL_RETVAL_COMPLETE); 7683 } 7684 /* send msg to other side */ 7685 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7686 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7687 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7688 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7689 persis_io.pr.pr_info.res_type = type; 7690 memcpy(persis_io.pr.pr_info.sa_res_key, 7691 param->serv_act_res_key, 7692 sizeof(param->serv_act_res_key)); 7693 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7694 &persis_io, sizeof(persis_io), 0)) > 7695 CTL_HA_STATUS_SUCCESS) { 7696 printf("CTL:Persis Out error returned from " 7697 "ctl_ha_msg_send %d\n", isc_retval); 7698 } 7699 } else { 7700 /* Reserved but not all registrants */ 7701 /* sa_res_key is res holder */ 7702 if (memcmp(param->serv_act_res_key, 7703 lun->per_res[lun->pr_res_idx].res_key.key, 7704 sizeof(struct scsi_per_res_key)) == 0) { 7705 /* validate scope and type */ 7706 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7707 SPR_LU_SCOPE) { 7708 ctl_set_invalid_field(/*ctsio*/ ctsio, 7709 /*sks_valid*/ 1, 7710 /*command*/ 1, 7711 /*field*/ 2, 7712 /*bit_valid*/ 1, 7713 /*bit*/ 4); 7714 ctl_done((union ctl_io *)ctsio); 7715 return (1); 7716 } 7717 7718 if (type>8 || type==2 || type==4 || type==0) { 7719 ctl_set_invalid_field(/*ctsio*/ ctsio, 7720 /*sks_valid*/ 1, 7721 /*command*/ 1, 7722 /*field*/ 2, 7723 /*bit_valid*/ 1, 7724 /*bit*/ 0); 7725 ctl_done((union ctl_io *)ctsio); 7726 return (1); 7727 } 7728 7729 /* 7730 * Do the following: 7731 * if sa_res_key != res_key remove all 7732 * registrants w/sa_res_key and generate UA 7733 * for these registrants(Registrations 7734 * Preempted) if it wasn't an exclusive 7735 * reservation generate UA(Reservations 7736 * Preempted) for all other registered nexuses 7737 * if the type has changed. Establish the new 7738 * reservation and holder. If res_key and 7739 * sa_res_key are the same do the above 7740 * except don't unregister the res holder. 7741 */ 7742 7743 /* 7744 * Temporarily unregister so it won't get 7745 * removed or UA generated 7746 */ 7747 lun->per_res[residx].registered = 0; 7748 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7749 if (lun->per_res[i].registered == 0) 7750 continue; 7751 7752 if (memcmp(param->serv_act_res_key, 7753 lun->per_res[i].res_key.key, 7754 sizeof(struct scsi_per_res_key)) == 0) { 7755 lun->per_res[i].registered = 0; 7756 memset(&lun->per_res[i].res_key, 7757 0, 7758 sizeof(struct scsi_per_res_key)); 7759 lun->pr_key_count--; 7760 7761 if (!persis_offset 7762 && i < CTL_MAX_INITIATORS) 7763 lun->pending_sense[i 7764 ].ua_pending |= 7765 CTL_UA_REG_PREEMPT; 7766 else if (persis_offset 7767 && i >= persis_offset) 7768 lun->pending_sense[ 7769 i-persis_offset].ua_pending |= 7770 CTL_UA_REG_PREEMPT; 7771 } else if (type != lun->res_type 7772 && (lun->res_type == SPR_TYPE_WR_EX_RO 7773 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7774 if (!persis_offset 7775 && i < CTL_MAX_INITIATORS) 7776 lun->pending_sense[i 7777 ].ua_pending |= 7778 CTL_UA_RES_RELEASE; 7779 else if (persis_offset 7780 && i >= persis_offset) 7781 lun->pending_sense[ 7782 i-persis_offset 7783 ].ua_pending |= 7784 CTL_UA_RES_RELEASE; 7785 } 7786 } 7787 lun->per_res[residx].registered = 1; 7788 lun->res_type = type; 7789 if (lun->res_type != SPR_TYPE_WR_EX_AR 7790 && lun->res_type != SPR_TYPE_EX_AC_AR) 7791 lun->pr_res_idx = residx; 7792 else 7793 lun->pr_res_idx = 7794 CTL_PR_ALL_REGISTRANTS; 7795 7796 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7797 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7798 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7799 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7800 persis_io.pr.pr_info.res_type = type; 7801 memcpy(persis_io.pr.pr_info.sa_res_key, 7802 param->serv_act_res_key, 7803 sizeof(param->serv_act_res_key)); 7804 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7805 &persis_io, sizeof(persis_io), 0)) > 7806 CTL_HA_STATUS_SUCCESS) { 7807 printf("CTL:Persis Out error returned " 7808 "from ctl_ha_msg_send %d\n", 7809 isc_retval); 7810 } 7811 } else { 7812 /* 7813 * sa_res_key is not the res holder just 7814 * remove registrants 7815 */ 7816 int found=0; 7817 mtx_lock(&softc->ctl_lock); 7818 7819 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7820 if (memcmp(param->serv_act_res_key, 7821 lun->per_res[i].res_key.key, 7822 sizeof(struct scsi_per_res_key)) != 0) 7823 continue; 7824 7825 found = 1; 7826 lun->per_res[i].registered = 0; 7827 memset(&lun->per_res[i].res_key, 0, 7828 sizeof(struct scsi_per_res_key)); 7829 lun->pr_key_count--; 7830 7831 if (!persis_offset 7832 && i < CTL_MAX_INITIATORS) 7833 lun->pending_sense[i].ua_pending |= 7834 CTL_UA_REG_PREEMPT; 7835 else if (persis_offset 7836 && i >= persis_offset) 7837 lun->pending_sense[ 7838 i-persis_offset].ua_pending |= 7839 CTL_UA_REG_PREEMPT; 7840 } 7841 7842 if (!found) { 7843 mtx_unlock(&softc->ctl_lock); 7844 free(ctsio->kern_data_ptr, M_CTL); 7845 ctl_set_reservation_conflict(ctsio); 7846 ctl_done((union ctl_io *)ctsio); 7847 return (1); 7848 } 7849 mtx_unlock(&softc->ctl_lock); 7850 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7851 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7852 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7853 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7854 persis_io.pr.pr_info.res_type = type; 7855 memcpy(persis_io.pr.pr_info.sa_res_key, 7856 param->serv_act_res_key, 7857 sizeof(param->serv_act_res_key)); 7858 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7859 &persis_io, sizeof(persis_io), 0)) > 7860 CTL_HA_STATUS_SUCCESS) { 7861 printf("CTL:Persis Out error returned " 7862 "from ctl_ha_msg_send %d\n", 7863 isc_retval); 7864 } 7865 } 7866 } 7867 7868 lun->PRGeneration++; 7869 7870 return (retval); 7871} 7872 7873static void 7874ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7875{ 7876 int i; 7877 7878 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7879 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7880 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 7881 msg->pr.pr_info.sa_res_key, 7882 sizeof(struct scsi_per_res_key)) != 0) { 7883 uint64_t sa_res_key; 7884 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7885 7886 if (sa_res_key == 0) { 7887 /* temporarily unregister this nexus */ 7888 lun->per_res[msg->pr.pr_info.residx].registered = 0; 7889 7890 /* 7891 * Unregister everybody else and build UA for 7892 * them 7893 */ 7894 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7895 if (lun->per_res[i].registered == 0) 7896 continue; 7897 7898 if (!persis_offset 7899 && i < CTL_MAX_INITIATORS) 7900 lun->pending_sense[i].ua_pending |= 7901 CTL_UA_REG_PREEMPT; 7902 else if (persis_offset && i >= persis_offset) 7903 lun->pending_sense[i - 7904 persis_offset].ua_pending |= 7905 CTL_UA_REG_PREEMPT; 7906 lun->per_res[i].registered = 0; 7907 memset(&lun->per_res[i].res_key, 0, 7908 sizeof(struct scsi_per_res_key)); 7909 } 7910 7911 lun->per_res[msg->pr.pr_info.residx].registered = 1; 7912 lun->pr_key_count = 1; 7913 lun->res_type = msg->pr.pr_info.res_type; 7914 if (lun->res_type != SPR_TYPE_WR_EX_AR 7915 && lun->res_type != SPR_TYPE_EX_AC_AR) 7916 lun->pr_res_idx = msg->pr.pr_info.residx; 7917 } else { 7918 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7919 if (memcmp(msg->pr.pr_info.sa_res_key, 7920 lun->per_res[i].res_key.key, 7921 sizeof(struct scsi_per_res_key)) != 0) 7922 continue; 7923 7924 lun->per_res[i].registered = 0; 7925 memset(&lun->per_res[i].res_key, 0, 7926 sizeof(struct scsi_per_res_key)); 7927 lun->pr_key_count--; 7928 7929 if (!persis_offset 7930 && i < persis_offset) 7931 lun->pending_sense[i].ua_pending |= 7932 CTL_UA_REG_PREEMPT; 7933 else if (persis_offset 7934 && i >= persis_offset) 7935 lun->pending_sense[i - 7936 persis_offset].ua_pending |= 7937 CTL_UA_REG_PREEMPT; 7938 } 7939 } 7940 } else { 7941 /* 7942 * Temporarily unregister so it won't get removed 7943 * or UA generated 7944 */ 7945 lun->per_res[msg->pr.pr_info.residx].registered = 0; 7946 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7947 if (lun->per_res[i].registered == 0) 7948 continue; 7949 7950 if (memcmp(msg->pr.pr_info.sa_res_key, 7951 lun->per_res[i].res_key.key, 7952 sizeof(struct scsi_per_res_key)) == 0) { 7953 lun->per_res[i].registered = 0; 7954 memset(&lun->per_res[i].res_key, 0, 7955 sizeof(struct scsi_per_res_key)); 7956 lun->pr_key_count--; 7957 if (!persis_offset 7958 && i < CTL_MAX_INITIATORS) 7959 lun->pending_sense[i].ua_pending |= 7960 CTL_UA_REG_PREEMPT; 7961 else if (persis_offset 7962 && i >= persis_offset) 7963 lun->pending_sense[i - 7964 persis_offset].ua_pending |= 7965 CTL_UA_REG_PREEMPT; 7966 } else if (msg->pr.pr_info.res_type != lun->res_type 7967 && (lun->res_type == SPR_TYPE_WR_EX_RO 7968 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 7969 if (!persis_offset 7970 && i < persis_offset) 7971 lun->pending_sense[i 7972 ].ua_pending |= 7973 CTL_UA_RES_RELEASE; 7974 else if (persis_offset 7975 && i >= persis_offset) 7976 lun->pending_sense[i - 7977 persis_offset].ua_pending |= 7978 CTL_UA_RES_RELEASE; 7979 } 7980 } 7981 lun->per_res[msg->pr.pr_info.residx].registered = 1; 7982 lun->res_type = msg->pr.pr_info.res_type; 7983 if (lun->res_type != SPR_TYPE_WR_EX_AR 7984 && lun->res_type != SPR_TYPE_EX_AC_AR) 7985 lun->pr_res_idx = msg->pr.pr_info.residx; 7986 else 7987 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7988 } 7989 lun->PRGeneration++; 7990 7991} 7992 7993 7994int 7995ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 7996{ 7997 int retval; 7998 int isc_retval; 7999 u_int32_t param_len; 8000 struct scsi_per_res_out *cdb; 8001 struct ctl_lun *lun; 8002 struct scsi_per_res_out_parms* param; 8003 struct ctl_softc *softc; 8004 uint32_t residx; 8005 uint64_t res_key, sa_res_key; 8006 uint8_t type; 8007 union ctl_ha_msg persis_io; 8008 int i; 8009 8010 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8011 8012 retval = CTL_RETVAL_COMPLETE; 8013 8014 softc = control_softc; 8015 8016 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8017 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8018 8019 /* 8020 * We only support whole-LUN scope. The scope & type are ignored for 8021 * register, register and ignore existing key and clear. 8022 * We sometimes ignore scope and type on preempts too!! 8023 * Verify reservation type here as well. 8024 */ 8025 type = cdb->scope_type & SPR_TYPE_MASK; 8026 if ((cdb->action == SPRO_RESERVE) 8027 || (cdb->action == SPRO_RELEASE)) { 8028 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8029 ctl_set_invalid_field(/*ctsio*/ ctsio, 8030 /*sks_valid*/ 1, 8031 /*command*/ 1, 8032 /*field*/ 2, 8033 /*bit_valid*/ 1, 8034 /*bit*/ 4); 8035 ctl_done((union ctl_io *)ctsio); 8036 return (CTL_RETVAL_COMPLETE); 8037 } 8038 8039 if (type>8 || type==2 || type==4 || type==0) { 8040 ctl_set_invalid_field(/*ctsio*/ ctsio, 8041 /*sks_valid*/ 1, 8042 /*command*/ 1, 8043 /*field*/ 2, 8044 /*bit_valid*/ 1, 8045 /*bit*/ 0); 8046 ctl_done((union ctl_io *)ctsio); 8047 return (CTL_RETVAL_COMPLETE); 8048 } 8049 } 8050 8051 switch (cdb->action & SPRO_ACTION_MASK) { 8052 case SPRO_REGISTER: 8053 case SPRO_RESERVE: 8054 case SPRO_RELEASE: 8055 case SPRO_CLEAR: 8056 case SPRO_PREEMPT: 8057 case SPRO_REG_IGNO: 8058 break; 8059 case SPRO_REG_MOVE: 8060 case SPRO_PRE_ABO: 8061 default: 8062 ctl_set_invalid_field(/*ctsio*/ ctsio, 8063 /*sks_valid*/ 1, 8064 /*command*/ 1, 8065 /*field*/ 1, 8066 /*bit_valid*/ 1, 8067 /*bit*/ 0); 8068 ctl_done((union ctl_io *)ctsio); 8069 return (CTL_RETVAL_COMPLETE); 8070 break; /* NOTREACHED */ 8071 } 8072 8073 param_len = scsi_4btoul(cdb->length); 8074 8075 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8076 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8077 ctsio->kern_data_len = param_len; 8078 ctsio->kern_total_len = param_len; 8079 ctsio->kern_data_resid = 0; 8080 ctsio->kern_rel_offset = 0; 8081 ctsio->kern_sg_entries = 0; 8082 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8083 ctsio->be_move_done = ctl_config_move_done; 8084 ctl_datamove((union ctl_io *)ctsio); 8085 8086 return (CTL_RETVAL_COMPLETE); 8087 } 8088 8089 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8090 8091 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8092 res_key = scsi_8btou64(param->res_key.key); 8093 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8094 8095 /* 8096 * Validate the reservation key here except for SPRO_REG_IGNO 8097 * This must be done for all other service actions 8098 */ 8099 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8100 mtx_lock(&softc->ctl_lock); 8101 if (lun->per_res[residx].registered) { 8102 if (memcmp(param->res_key.key, 8103 lun->per_res[residx].res_key.key, 8104 ctl_min(sizeof(param->res_key), 8105 sizeof(lun->per_res[residx].res_key))) != 0) { 8106 /* 8107 * The current key passed in doesn't match 8108 * the one the initiator previously 8109 * registered. 8110 */ 8111 mtx_unlock(&softc->ctl_lock); 8112 free(ctsio->kern_data_ptr, M_CTL); 8113 ctl_set_reservation_conflict(ctsio); 8114 ctl_done((union ctl_io *)ctsio); 8115 return (CTL_RETVAL_COMPLETE); 8116 } 8117 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8118 /* 8119 * We are not registered 8120 */ 8121 mtx_unlock(&softc->ctl_lock); 8122 free(ctsio->kern_data_ptr, M_CTL); 8123 ctl_set_reservation_conflict(ctsio); 8124 ctl_done((union ctl_io *)ctsio); 8125 return (CTL_RETVAL_COMPLETE); 8126 } else if (res_key != 0) { 8127 /* 8128 * We are not registered and trying to register but 8129 * the register key isn't zero. 8130 */ 8131 mtx_unlock(&softc->ctl_lock); 8132 free(ctsio->kern_data_ptr, M_CTL); 8133 ctl_set_reservation_conflict(ctsio); 8134 ctl_done((union ctl_io *)ctsio); 8135 return (CTL_RETVAL_COMPLETE); 8136 } 8137 mtx_unlock(&softc->ctl_lock); 8138 } 8139 8140 switch (cdb->action & SPRO_ACTION_MASK) { 8141 case SPRO_REGISTER: 8142 case SPRO_REG_IGNO: { 8143 8144#if 0 8145 printf("Registration received\n"); 8146#endif 8147 8148 /* 8149 * We don't support any of these options, as we report in 8150 * the read capabilities request (see 8151 * ctl_persistent_reserve_in(), above). 8152 */ 8153 if ((param->flags & SPR_SPEC_I_PT) 8154 || (param->flags & SPR_ALL_TG_PT) 8155 || (param->flags & SPR_APTPL)) { 8156 int bit_ptr; 8157 8158 if (param->flags & SPR_APTPL) 8159 bit_ptr = 0; 8160 else if (param->flags & SPR_ALL_TG_PT) 8161 bit_ptr = 2; 8162 else /* SPR_SPEC_I_PT */ 8163 bit_ptr = 3; 8164 8165 free(ctsio->kern_data_ptr, M_CTL); 8166 ctl_set_invalid_field(ctsio, 8167 /*sks_valid*/ 1, 8168 /*command*/ 0, 8169 /*field*/ 20, 8170 /*bit_valid*/ 1, 8171 /*bit*/ bit_ptr); 8172 ctl_done((union ctl_io *)ctsio); 8173 return (CTL_RETVAL_COMPLETE); 8174 } 8175 8176 mtx_lock(&softc->ctl_lock); 8177 8178 /* 8179 * The initiator wants to clear the 8180 * key/unregister. 8181 */ 8182 if (sa_res_key == 0) { 8183 if ((res_key == 0 8184 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8185 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8186 && !lun->per_res[residx].registered)) { 8187 mtx_unlock(&softc->ctl_lock); 8188 goto done; 8189 } 8190 8191 lun->per_res[residx].registered = 0; 8192 memset(&lun->per_res[residx].res_key, 8193 0, sizeof(lun->per_res[residx].res_key)); 8194 lun->pr_key_count--; 8195 8196 if (residx == lun->pr_res_idx) { 8197 lun->flags &= ~CTL_LUN_PR_RESERVED; 8198 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8199 8200 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8201 || lun->res_type == SPR_TYPE_EX_AC_RO) 8202 && lun->pr_key_count) { 8203 /* 8204 * If the reservation is a registrants 8205 * only type we need to generate a UA 8206 * for other registered inits. The 8207 * sense code should be RESERVATIONS 8208 * RELEASED 8209 */ 8210 8211 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8212 if (lun->per_res[ 8213 i+persis_offset].registered 8214 == 0) 8215 continue; 8216 lun->pending_sense[i 8217 ].ua_pending |= 8218 CTL_UA_RES_RELEASE; 8219 } 8220 } 8221 lun->res_type = 0; 8222 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8223 if (lun->pr_key_count==0) { 8224 lun->flags &= ~CTL_LUN_PR_RESERVED; 8225 lun->res_type = 0; 8226 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8227 } 8228 } 8229 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8230 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8231 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8232 persis_io.pr.pr_info.residx = residx; 8233 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8234 &persis_io, sizeof(persis_io), 0 )) > 8235 CTL_HA_STATUS_SUCCESS) { 8236 printf("CTL:Persis Out error returned from " 8237 "ctl_ha_msg_send %d\n", isc_retval); 8238 } 8239 mtx_unlock(&softc->ctl_lock); 8240 } else /* sa_res_key != 0 */ { 8241 8242 /* 8243 * If we aren't registered currently then increment 8244 * the key count and set the registered flag. 8245 */ 8246 if (!lun->per_res[residx].registered) { 8247 lun->pr_key_count++; 8248 lun->per_res[residx].registered = 1; 8249 } 8250 8251 memcpy(&lun->per_res[residx].res_key, 8252 param->serv_act_res_key, 8253 ctl_min(sizeof(param->serv_act_res_key), 8254 sizeof(lun->per_res[residx].res_key))); 8255 8256 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8257 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8258 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8259 persis_io.pr.pr_info.residx = residx; 8260 memcpy(persis_io.pr.pr_info.sa_res_key, 8261 param->serv_act_res_key, 8262 sizeof(param->serv_act_res_key)); 8263 mtx_unlock(&softc->ctl_lock); 8264 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8265 &persis_io, sizeof(persis_io), 0)) > 8266 CTL_HA_STATUS_SUCCESS) { 8267 printf("CTL:Persis Out error returned from " 8268 "ctl_ha_msg_send %d\n", isc_retval); 8269 } 8270 } 8271 lun->PRGeneration++; 8272 8273 break; 8274 } 8275 case SPRO_RESERVE: 8276#if 0 8277 printf("Reserve executed type %d\n", type); 8278#endif 8279 mtx_lock(&softc->ctl_lock); 8280 if (lun->flags & CTL_LUN_PR_RESERVED) { 8281 /* 8282 * if this isn't the reservation holder and it's 8283 * not a "all registrants" type or if the type is 8284 * different then we have a conflict 8285 */ 8286 if ((lun->pr_res_idx != residx 8287 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8288 || lun->res_type != type) { 8289 mtx_unlock(&softc->ctl_lock); 8290 free(ctsio->kern_data_ptr, M_CTL); 8291 ctl_set_reservation_conflict(ctsio); 8292 ctl_done((union ctl_io *)ctsio); 8293 return (CTL_RETVAL_COMPLETE); 8294 } 8295 mtx_unlock(&softc->ctl_lock); 8296 } else /* create a reservation */ { 8297 /* 8298 * If it's not an "all registrants" type record 8299 * reservation holder 8300 */ 8301 if (type != SPR_TYPE_WR_EX_AR 8302 && type != SPR_TYPE_EX_AC_AR) 8303 lun->pr_res_idx = residx; /* Res holder */ 8304 else 8305 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8306 8307 lun->flags |= CTL_LUN_PR_RESERVED; 8308 lun->res_type = type; 8309 8310 mtx_unlock(&softc->ctl_lock); 8311 8312 /* send msg to other side */ 8313 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8314 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8315 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8316 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8317 persis_io.pr.pr_info.res_type = type; 8318 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8319 &persis_io, sizeof(persis_io), 0)) > 8320 CTL_HA_STATUS_SUCCESS) { 8321 printf("CTL:Persis Out error returned from " 8322 "ctl_ha_msg_send %d\n", isc_retval); 8323 } 8324 } 8325 break; 8326 8327 case SPRO_RELEASE: 8328 mtx_lock(&softc->ctl_lock); 8329 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8330 /* No reservation exists return good status */ 8331 mtx_unlock(&softc->ctl_lock); 8332 goto done; 8333 } 8334 /* 8335 * Is this nexus a reservation holder? 8336 */ 8337 if (lun->pr_res_idx != residx 8338 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8339 /* 8340 * not a res holder return good status but 8341 * do nothing 8342 */ 8343 mtx_unlock(&softc->ctl_lock); 8344 goto done; 8345 } 8346 8347 if (lun->res_type != type) { 8348 mtx_unlock(&softc->ctl_lock); 8349 free(ctsio->kern_data_ptr, M_CTL); 8350 ctl_set_illegal_pr_release(ctsio); 8351 ctl_done((union ctl_io *)ctsio); 8352 return (CTL_RETVAL_COMPLETE); 8353 } 8354 8355 /* okay to release */ 8356 lun->flags &= ~CTL_LUN_PR_RESERVED; 8357 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8358 lun->res_type = 0; 8359 8360 /* 8361 * if this isn't an exclusive access 8362 * res generate UA for all other 8363 * registrants. 8364 */ 8365 if (type != SPR_TYPE_EX_AC 8366 && type != SPR_TYPE_WR_EX) { 8367 /* 8368 * temporarily unregister so we don't generate UA 8369 */ 8370 lun->per_res[residx].registered = 0; 8371 8372 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8373 if (lun->per_res[i+persis_offset].registered 8374 == 0) 8375 continue; 8376 lun->pending_sense[i].ua_pending |= 8377 CTL_UA_RES_RELEASE; 8378 } 8379 8380 lun->per_res[residx].registered = 1; 8381 } 8382 mtx_unlock(&softc->ctl_lock); 8383 /* Send msg to other side */ 8384 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8385 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8386 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8387 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8388 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8389 printf("CTL:Persis Out error returned from " 8390 "ctl_ha_msg_send %d\n", isc_retval); 8391 } 8392 break; 8393 8394 case SPRO_CLEAR: 8395 /* send msg to other side */ 8396 8397 mtx_lock(&softc->ctl_lock); 8398 lun->flags &= ~CTL_LUN_PR_RESERVED; 8399 lun->res_type = 0; 8400 lun->pr_key_count = 0; 8401 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8402 8403 8404 memset(&lun->per_res[residx].res_key, 8405 0, sizeof(lun->per_res[residx].res_key)); 8406 lun->per_res[residx].registered = 0; 8407 8408 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8409 if (lun->per_res[i].registered) { 8410 if (!persis_offset && i < CTL_MAX_INITIATORS) 8411 lun->pending_sense[i].ua_pending |= 8412 CTL_UA_RES_PREEMPT; 8413 else if (persis_offset && i >= persis_offset) 8414 lun->pending_sense[i-persis_offset 8415 ].ua_pending |= CTL_UA_RES_PREEMPT; 8416 8417 memset(&lun->per_res[i].res_key, 8418 0, sizeof(struct scsi_per_res_key)); 8419 lun->per_res[i].registered = 0; 8420 } 8421 lun->PRGeneration++; 8422 mtx_unlock(&softc->ctl_lock); 8423 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8424 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8425 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8426 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8427 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8428 printf("CTL:Persis Out error returned from " 8429 "ctl_ha_msg_send %d\n", isc_retval); 8430 } 8431 break; 8432 8433 case SPRO_PREEMPT: { 8434 int nretval; 8435 8436 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8437 residx, ctsio, cdb, param); 8438 if (nretval != 0) 8439 return (CTL_RETVAL_COMPLETE); 8440 break; 8441 } 8442 case SPRO_REG_MOVE: 8443 case SPRO_PRE_ABO: 8444 default: 8445 free(ctsio->kern_data_ptr, M_CTL); 8446 ctl_set_invalid_field(/*ctsio*/ ctsio, 8447 /*sks_valid*/ 1, 8448 /*command*/ 1, 8449 /*field*/ 1, 8450 /*bit_valid*/ 1, 8451 /*bit*/ 0); 8452 ctl_done((union ctl_io *)ctsio); 8453 return (CTL_RETVAL_COMPLETE); 8454 break; /* NOTREACHED */ 8455 } 8456 8457done: 8458 free(ctsio->kern_data_ptr, M_CTL); 8459 ctl_set_success(ctsio); 8460 ctl_done((union ctl_io *)ctsio); 8461 8462 return (retval); 8463} 8464 8465/* 8466 * This routine is for handling a message from the other SC pertaining to 8467 * persistent reserve out. All the error checking will have been done 8468 * so only perorming the action need be done here to keep the two 8469 * in sync. 8470 */ 8471static void 8472ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8473{ 8474 struct ctl_lun *lun; 8475 struct ctl_softc *softc; 8476 int i; 8477 uint32_t targ_lun; 8478 8479 softc = control_softc; 8480 8481 mtx_lock(&softc->ctl_lock); 8482 8483 targ_lun = msg->hdr.nexus.targ_lun; 8484 if (msg->hdr.nexus.lun_map_fn != NULL) 8485 targ_lun = msg->hdr.nexus.lun_map_fn(msg->hdr.nexus.lun_map_arg, targ_lun); 8486 lun = softc->ctl_luns[targ_lun]; 8487 switch(msg->pr.pr_info.action) { 8488 case CTL_PR_REG_KEY: 8489 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8490 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8491 lun->pr_key_count++; 8492 } 8493 lun->PRGeneration++; 8494 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8495 msg->pr.pr_info.sa_res_key, 8496 sizeof(struct scsi_per_res_key)); 8497 break; 8498 8499 case CTL_PR_UNREG_KEY: 8500 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8501 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8502 0, sizeof(struct scsi_per_res_key)); 8503 lun->pr_key_count--; 8504 8505 /* XXX Need to see if the reservation has been released */ 8506 /* if so do we need to generate UA? */ 8507 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8508 lun->flags &= ~CTL_LUN_PR_RESERVED; 8509 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8510 8511 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8512 || lun->res_type == SPR_TYPE_EX_AC_RO) 8513 && lun->pr_key_count) { 8514 /* 8515 * If the reservation is a registrants 8516 * only type we need to generate a UA 8517 * for other registered inits. The 8518 * sense code should be RESERVATIONS 8519 * RELEASED 8520 */ 8521 8522 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8523 if (lun->per_res[i+ 8524 persis_offset].registered == 0) 8525 continue; 8526 8527 lun->pending_sense[i 8528 ].ua_pending |= 8529 CTL_UA_RES_RELEASE; 8530 } 8531 } 8532 lun->res_type = 0; 8533 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8534 if (lun->pr_key_count==0) { 8535 lun->flags &= ~CTL_LUN_PR_RESERVED; 8536 lun->res_type = 0; 8537 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8538 } 8539 } 8540 lun->PRGeneration++; 8541 break; 8542 8543 case CTL_PR_RESERVE: 8544 lun->flags |= CTL_LUN_PR_RESERVED; 8545 lun->res_type = msg->pr.pr_info.res_type; 8546 lun->pr_res_idx = msg->pr.pr_info.residx; 8547 8548 break; 8549 8550 case CTL_PR_RELEASE: 8551 /* 8552 * if this isn't an exclusive access res generate UA for all 8553 * other registrants. 8554 */ 8555 if (lun->res_type != SPR_TYPE_EX_AC 8556 && lun->res_type != SPR_TYPE_WR_EX) { 8557 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8558 if (lun->per_res[i+persis_offset].registered) 8559 lun->pending_sense[i].ua_pending |= 8560 CTL_UA_RES_RELEASE; 8561 } 8562 8563 lun->flags &= ~CTL_LUN_PR_RESERVED; 8564 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8565 lun->res_type = 0; 8566 break; 8567 8568 case CTL_PR_PREEMPT: 8569 ctl_pro_preempt_other(lun, msg); 8570 break; 8571 case CTL_PR_CLEAR: 8572 lun->flags &= ~CTL_LUN_PR_RESERVED; 8573 lun->res_type = 0; 8574 lun->pr_key_count = 0; 8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8576 8577 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8578 if (lun->per_res[i].registered == 0) 8579 continue; 8580 if (!persis_offset 8581 && i < CTL_MAX_INITIATORS) 8582 lun->pending_sense[i].ua_pending |= 8583 CTL_UA_RES_PREEMPT; 8584 else if (persis_offset 8585 && i >= persis_offset) 8586 lun->pending_sense[i-persis_offset].ua_pending|= 8587 CTL_UA_RES_PREEMPT; 8588 memset(&lun->per_res[i].res_key, 0, 8589 sizeof(struct scsi_per_res_key)); 8590 lun->per_res[i].registered = 0; 8591 } 8592 lun->PRGeneration++; 8593 break; 8594 } 8595 8596 mtx_unlock(&softc->ctl_lock); 8597} 8598 8599int 8600ctl_read_write(struct ctl_scsiio *ctsio) 8601{ 8602 struct ctl_lun *lun; 8603 struct ctl_lba_len_flags *lbalen; 8604 uint64_t lba; 8605 uint32_t num_blocks; 8606 int reladdr, fua, dpo, ebp; 8607 int retval; 8608 int isread; 8609 8610 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8611 8612 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8613 8614 reladdr = 0; 8615 fua = 0; 8616 dpo = 0; 8617 ebp = 0; 8618 8619 retval = CTL_RETVAL_COMPLETE; 8620 8621 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8622 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8623 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8624 uint32_t residx; 8625 8626 /* 8627 * XXX KDM need a lock here. 8628 */ 8629 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8630 if ((lun->res_type == SPR_TYPE_EX_AC 8631 && residx != lun->pr_res_idx) 8632 || ((lun->res_type == SPR_TYPE_EX_AC_RO 8633 || lun->res_type == SPR_TYPE_EX_AC_AR) 8634 && !lun->per_res[residx].registered)) { 8635 ctl_set_reservation_conflict(ctsio); 8636 ctl_done((union ctl_io *)ctsio); 8637 return (CTL_RETVAL_COMPLETE); 8638 } 8639 } 8640 8641 switch (ctsio->cdb[0]) { 8642 case READ_6: 8643 case WRITE_6: { 8644 struct scsi_rw_6 *cdb; 8645 8646 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8647 8648 lba = scsi_3btoul(cdb->addr); 8649 /* only 5 bits are valid in the most significant address byte */ 8650 lba &= 0x1fffff; 8651 num_blocks = cdb->length; 8652 /* 8653 * This is correct according to SBC-2. 8654 */ 8655 if (num_blocks == 0) 8656 num_blocks = 256; 8657 break; 8658 } 8659 case READ_10: 8660 case WRITE_10: { 8661 struct scsi_rw_10 *cdb; 8662 8663 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8664 8665 if (cdb->byte2 & SRW10_RELADDR) 8666 reladdr = 1; 8667 if (cdb->byte2 & SRW10_FUA) 8668 fua = 1; 8669 if (cdb->byte2 & SRW10_DPO) 8670 dpo = 1; 8671 8672 if ((cdb->opcode == WRITE_10) 8673 && (cdb->byte2 & SRW10_EBP)) 8674 ebp = 1; 8675 8676 lba = scsi_4btoul(cdb->addr); 8677 num_blocks = scsi_2btoul(cdb->length); 8678 break; 8679 } 8680 case WRITE_VERIFY_10: { 8681 struct scsi_write_verify_10 *cdb; 8682 8683 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8684 8685 /* 8686 * XXX KDM we should do actual write verify support at some 8687 * point. This is obviously fake, we're just translating 8688 * things to a write. So we don't even bother checking the 8689 * BYTCHK field, since we don't do any verification. If 8690 * the user asks for it, we'll just pretend we did it. 8691 */ 8692 if (cdb->byte2 & SWV_DPO) 8693 dpo = 1; 8694 8695 lba = scsi_4btoul(cdb->addr); 8696 num_blocks = scsi_2btoul(cdb->length); 8697 break; 8698 } 8699 case READ_12: 8700 case WRITE_12: { 8701 struct scsi_rw_12 *cdb; 8702 8703 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8704 8705 if (cdb->byte2 & SRW12_RELADDR) 8706 reladdr = 1; 8707 if (cdb->byte2 & SRW12_FUA) 8708 fua = 1; 8709 if (cdb->byte2 & SRW12_DPO) 8710 dpo = 1; 8711 lba = scsi_4btoul(cdb->addr); 8712 num_blocks = scsi_4btoul(cdb->length); 8713 break; 8714 } 8715 case WRITE_VERIFY_12: { 8716 struct scsi_write_verify_12 *cdb; 8717 8718 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8719 8720 if (cdb->byte2 & SWV_DPO) 8721 dpo = 1; 8722 8723 lba = scsi_4btoul(cdb->addr); 8724 num_blocks = scsi_4btoul(cdb->length); 8725 8726 break; 8727 } 8728 case READ_16: 8729 case WRITE_16: { 8730 struct scsi_rw_16 *cdb; 8731 8732 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8733 8734 if (cdb->byte2 & SRW12_RELADDR) 8735 reladdr = 1; 8736 if (cdb->byte2 & SRW12_FUA) 8737 fua = 1; 8738 if (cdb->byte2 & SRW12_DPO) 8739 dpo = 1; 8740 8741 lba = scsi_8btou64(cdb->addr); 8742 num_blocks = scsi_4btoul(cdb->length); 8743 break; 8744 } 8745 case WRITE_VERIFY_16: { 8746 struct scsi_write_verify_16 *cdb; 8747 8748 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8749 8750 if (cdb->byte2 & SWV_DPO) 8751 dpo = 1; 8752 8753 lba = scsi_8btou64(cdb->addr); 8754 num_blocks = scsi_4btoul(cdb->length); 8755 break; 8756 } 8757 default: 8758 /* 8759 * We got a command we don't support. This shouldn't 8760 * happen, commands should be filtered out above us. 8761 */ 8762 ctl_set_invalid_opcode(ctsio); 8763 ctl_done((union ctl_io *)ctsio); 8764 8765 return (CTL_RETVAL_COMPLETE); 8766 break; /* NOTREACHED */ 8767 } 8768 8769 /* 8770 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8771 * interesting for us, but if RAIDCore is in write-back mode, 8772 * getting it to do write-through for a particular transaction may 8773 * not be possible. 8774 */ 8775 /* 8776 * We don't support relative addressing. That also requires 8777 * supporting linked commands, which we don't do. 8778 */ 8779 if (reladdr != 0) { 8780 ctl_set_invalid_field(ctsio, 8781 /*sks_valid*/ 1, 8782 /*command*/ 1, 8783 /*field*/ 1, 8784 /*bit_valid*/ 1, 8785 /*bit*/ 0); 8786 ctl_done((union ctl_io *)ctsio); 8787 return (CTL_RETVAL_COMPLETE); 8788 } 8789 8790 /* 8791 * The first check is to make sure we're in bounds, the second 8792 * check is to catch wrap-around problems. If the lba + num blocks 8793 * is less than the lba, then we've wrapped around and the block 8794 * range is invalid anyway. 8795 */ 8796 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8797 || ((lba + num_blocks) < lba)) { 8798 ctl_set_lba_out_of_range(ctsio); 8799 ctl_done((union ctl_io *)ctsio); 8800 return (CTL_RETVAL_COMPLETE); 8801 } 8802 8803 /* 8804 * According to SBC-3, a transfer length of 0 is not an error. 8805 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8806 * translates to 256 blocks for those commands. 8807 */ 8808 if (num_blocks == 0) { 8809 ctl_set_success(ctsio); 8810 ctl_done((union ctl_io *)ctsio); 8811 return (CTL_RETVAL_COMPLETE); 8812 } 8813 8814 lbalen = (struct ctl_lba_len_flags *) 8815 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8816 lbalen->lba = lba; 8817 lbalen->len = num_blocks; 8818 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 8819 8820 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8821 ctsio->kern_rel_offset = 0; 8822 8823 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8824 8825 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8826 8827 return (retval); 8828} 8829 8830static int 8831ctl_cnw_cont(union ctl_io *io) 8832{ 8833 struct ctl_scsiio *ctsio; 8834 struct ctl_lun *lun; 8835 struct ctl_lba_len_flags *lbalen; 8836 int retval; 8837 8838 ctsio = &io->scsiio; 8839 ctsio->io_hdr.status = CTL_STATUS_NONE; 8840 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8841 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8842 lbalen = (struct ctl_lba_len_flags *) 8843 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8844 lbalen->flags = CTL_LLF_WRITE; 8845 8846 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8847 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8848 return (retval); 8849} 8850 8851int 8852ctl_cnw(struct ctl_scsiio *ctsio) 8853{ 8854 struct ctl_lun *lun; 8855 struct ctl_lba_len_flags *lbalen; 8856 uint64_t lba; 8857 uint32_t num_blocks; 8858 int fua, dpo; 8859 int retval; 8860 8861 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8862 8863 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8864 8865 fua = 0; 8866 dpo = 0; 8867 8868 retval = CTL_RETVAL_COMPLETE; 8869 8870 switch (ctsio->cdb[0]) { 8871 case COMPARE_AND_WRITE: { 8872 struct scsi_compare_and_write *cdb; 8873 8874 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8875 8876 if (cdb->byte2 & SRW10_FUA) 8877 fua = 1; 8878 if (cdb->byte2 & SRW10_DPO) 8879 dpo = 1; 8880 lba = scsi_8btou64(cdb->addr); 8881 num_blocks = cdb->length; 8882 break; 8883 } 8884 default: 8885 /* 8886 * We got a command we don't support. This shouldn't 8887 * happen, commands should be filtered out above us. 8888 */ 8889 ctl_set_invalid_opcode(ctsio); 8890 ctl_done((union ctl_io *)ctsio); 8891 8892 return (CTL_RETVAL_COMPLETE); 8893 break; /* NOTREACHED */ 8894 } 8895 8896 /* 8897 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8898 * interesting for us, but if RAIDCore is in write-back mode, 8899 * getting it to do write-through for a particular transaction may 8900 * not be possible. 8901 */ 8902 8903 /* 8904 * The first check is to make sure we're in bounds, the second 8905 * check is to catch wrap-around problems. If the lba + num blocks 8906 * is less than the lba, then we've wrapped around and the block 8907 * range is invalid anyway. 8908 */ 8909 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8910 || ((lba + num_blocks) < lba)) { 8911 ctl_set_lba_out_of_range(ctsio); 8912 ctl_done((union ctl_io *)ctsio); 8913 return (CTL_RETVAL_COMPLETE); 8914 } 8915 8916 /* 8917 * According to SBC-3, a transfer length of 0 is not an error. 8918 */ 8919 if (num_blocks == 0) { 8920 ctl_set_success(ctsio); 8921 ctl_done((union ctl_io *)ctsio); 8922 return (CTL_RETVAL_COMPLETE); 8923 } 8924 8925 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8926 ctsio->kern_rel_offset = 0; 8927 8928 /* 8929 * Set the IO_CONT flag, so that if this I/O gets passed to 8930 * ctl_data_submit_done(), it'll get passed back to 8931 * ctl_ctl_cnw_cont() for further processing. 8932 */ 8933 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8934 ctsio->io_cont = ctl_cnw_cont; 8935 8936 lbalen = (struct ctl_lba_len_flags *) 8937 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8938 lbalen->lba = lba; 8939 lbalen->len = num_blocks; 8940 lbalen->flags = CTL_LLF_COMPARE; 8941 8942 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8943 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8944 return (retval); 8945} 8946 8947int 8948ctl_verify(struct ctl_scsiio *ctsio) 8949{ 8950 struct ctl_lun *lun; 8951 struct ctl_lba_len_flags *lbalen; 8952 uint64_t lba; 8953 uint32_t num_blocks; 8954 int bytchk, dpo; 8955 int retval; 8956 8957 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8958 8959 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8960 8961 bytchk = 0; 8962 dpo = 0; 8963 retval = CTL_RETVAL_COMPLETE; 8964 8965 switch (ctsio->cdb[0]) { 8966 case VERIFY_10: { 8967 struct scsi_verify_10 *cdb; 8968 8969 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8970 if (cdb->byte2 & SVFY_BYTCHK) 8971 bytchk = 1; 8972 if (cdb->byte2 & SVFY_DPO) 8973 dpo = 1; 8974 lba = scsi_4btoul(cdb->addr); 8975 num_blocks = scsi_2btoul(cdb->length); 8976 break; 8977 } 8978 case VERIFY_12: { 8979 struct scsi_verify_12 *cdb; 8980 8981 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8982 if (cdb->byte2 & SVFY_BYTCHK) 8983 bytchk = 1; 8984 if (cdb->byte2 & SVFY_DPO) 8985 dpo = 1; 8986 lba = scsi_4btoul(cdb->addr); 8987 num_blocks = scsi_4btoul(cdb->length); 8988 break; 8989 } 8990 case VERIFY_16: { 8991 struct scsi_rw_16 *cdb; 8992 8993 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8994 if (cdb->byte2 & SVFY_BYTCHK) 8995 bytchk = 1; 8996 if (cdb->byte2 & SVFY_DPO) 8997 dpo = 1; 8998 lba = scsi_8btou64(cdb->addr); 8999 num_blocks = scsi_4btoul(cdb->length); 9000 break; 9001 } 9002 default: 9003 /* 9004 * We got a command we don't support. This shouldn't 9005 * happen, commands should be filtered out above us. 9006 */ 9007 ctl_set_invalid_opcode(ctsio); 9008 ctl_done((union ctl_io *)ctsio); 9009 return (CTL_RETVAL_COMPLETE); 9010 } 9011 9012 /* 9013 * The first check is to make sure we're in bounds, the second 9014 * check is to catch wrap-around problems. If the lba + num blocks 9015 * is less than the lba, then we've wrapped around and the block 9016 * range is invalid anyway. 9017 */ 9018 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9019 || ((lba + num_blocks) < lba)) { 9020 ctl_set_lba_out_of_range(ctsio); 9021 ctl_done((union ctl_io *)ctsio); 9022 return (CTL_RETVAL_COMPLETE); 9023 } 9024 9025 /* 9026 * According to SBC-3, a transfer length of 0 is not an error. 9027 */ 9028 if (num_blocks == 0) { 9029 ctl_set_success(ctsio); 9030 ctl_done((union ctl_io *)ctsio); 9031 return (CTL_RETVAL_COMPLETE); 9032 } 9033 9034 lbalen = (struct ctl_lba_len_flags *) 9035 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9036 lbalen->lba = lba; 9037 lbalen->len = num_blocks; 9038 if (bytchk) { 9039 lbalen->flags = CTL_LLF_COMPARE; 9040 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9041 } else { 9042 lbalen->flags = CTL_LLF_VERIFY; 9043 ctsio->kern_total_len = 0; 9044 } 9045 ctsio->kern_rel_offset = 0; 9046 9047 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9048 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9049 return (retval); 9050} 9051 9052int 9053ctl_report_luns(struct ctl_scsiio *ctsio) 9054{ 9055 struct scsi_report_luns *cdb; 9056 struct scsi_report_luns_data *lun_data; 9057 struct ctl_lun *lun, *request_lun; 9058 int num_luns, retval; 9059 uint32_t alloc_len, lun_datalen; 9060 int num_filled, well_known; 9061 uint32_t initidx, targ_lun_id, lun_id; 9062 9063 retval = CTL_RETVAL_COMPLETE; 9064 well_known = 0; 9065 9066 cdb = (struct scsi_report_luns *)ctsio->cdb; 9067 9068 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9069 9070 mtx_lock(&control_softc->ctl_lock); 9071 num_luns = control_softc->num_luns; 9072 mtx_unlock(&control_softc->ctl_lock); 9073 9074 switch (cdb->select_report) { 9075 case RPL_REPORT_DEFAULT: 9076 case RPL_REPORT_ALL: 9077 break; 9078 case RPL_REPORT_WELLKNOWN: 9079 well_known = 1; 9080 num_luns = 0; 9081 break; 9082 default: 9083 ctl_set_invalid_field(ctsio, 9084 /*sks_valid*/ 1, 9085 /*command*/ 1, 9086 /*field*/ 2, 9087 /*bit_valid*/ 0, 9088 /*bit*/ 0); 9089 ctl_done((union ctl_io *)ctsio); 9090 return (retval); 9091 break; /* NOTREACHED */ 9092 } 9093 9094 alloc_len = scsi_4btoul(cdb->length); 9095 /* 9096 * The initiator has to allocate at least 16 bytes for this request, 9097 * so he can at least get the header and the first LUN. Otherwise 9098 * we reject the request (per SPC-3 rev 14, section 6.21). 9099 */ 9100 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9101 sizeof(struct scsi_report_luns_lundata))) { 9102 ctl_set_invalid_field(ctsio, 9103 /*sks_valid*/ 1, 9104 /*command*/ 1, 9105 /*field*/ 6, 9106 /*bit_valid*/ 0, 9107 /*bit*/ 0); 9108 ctl_done((union ctl_io *)ctsio); 9109 return (retval); 9110 } 9111 9112 request_lun = (struct ctl_lun *) 9113 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9114 9115 lun_datalen = sizeof(*lun_data) + 9116 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9117 9118 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9119 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9120 ctsio->kern_sg_entries = 0; 9121 9122 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9123 9124 mtx_lock(&control_softc->ctl_lock); 9125 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9126 lun_id = targ_lun_id; 9127 if (ctsio->io_hdr.nexus.lun_map_fn != NULL) 9128 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id); 9129 if (lun_id >= CTL_MAX_LUNS) 9130 continue; 9131 lun = control_softc->ctl_luns[lun_id]; 9132 if (lun == NULL) 9133 continue; 9134 9135 if (targ_lun_id <= 0xff) { 9136 /* 9137 * Peripheral addressing method, bus number 0. 9138 */ 9139 lun_data->luns[num_filled].lundata[0] = 9140 RPL_LUNDATA_ATYP_PERIPH; 9141 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9142 num_filled++; 9143 } else if (targ_lun_id <= 0x3fff) { 9144 /* 9145 * Flat addressing method. 9146 */ 9147 lun_data->luns[num_filled].lundata[0] = 9148 RPL_LUNDATA_ATYP_FLAT | 9149 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9150#ifdef OLDCTLHEADERS 9151 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9152 (targ_lun_id & SRLD_BUS_LUN_MASK); 9153#endif 9154 lun_data->luns[num_filled].lundata[1] = 9155#ifdef OLDCTLHEADERS 9156 targ_lun_id >> SRLD_BUS_LUN_BITS; 9157#endif 9158 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9159 num_filled++; 9160 } else { 9161 printf("ctl_report_luns: bogus LUN number %jd, " 9162 "skipping\n", (intmax_t)targ_lun_id); 9163 } 9164 /* 9165 * According to SPC-3, rev 14 section 6.21: 9166 * 9167 * "The execution of a REPORT LUNS command to any valid and 9168 * installed logical unit shall clear the REPORTED LUNS DATA 9169 * HAS CHANGED unit attention condition for all logical 9170 * units of that target with respect to the requesting 9171 * initiator. A valid and installed logical unit is one 9172 * having a PERIPHERAL QUALIFIER of 000b in the standard 9173 * INQUIRY data (see 6.4.2)." 9174 * 9175 * If request_lun is NULL, the LUN this report luns command 9176 * was issued to is either disabled or doesn't exist. In that 9177 * case, we shouldn't clear any pending lun change unit 9178 * attention. 9179 */ 9180 if (request_lun != NULL) 9181 lun->pending_sense[initidx].ua_pending &= 9182 ~CTL_UA_LUN_CHANGE; 9183 } 9184 mtx_unlock(&control_softc->ctl_lock); 9185 9186 /* 9187 * It's quite possible that we've returned fewer LUNs than we allocated 9188 * space for. Trim it. 9189 */ 9190 lun_datalen = sizeof(*lun_data) + 9191 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9192 9193 if (lun_datalen < alloc_len) { 9194 ctsio->residual = alloc_len - lun_datalen; 9195 ctsio->kern_data_len = lun_datalen; 9196 ctsio->kern_total_len = lun_datalen; 9197 } else { 9198 ctsio->residual = 0; 9199 ctsio->kern_data_len = alloc_len; 9200 ctsio->kern_total_len = alloc_len; 9201 } 9202 ctsio->kern_data_resid = 0; 9203 ctsio->kern_rel_offset = 0; 9204 ctsio->kern_sg_entries = 0; 9205 9206 /* 9207 * We set this to the actual data length, regardless of how much 9208 * space we actually have to return results. If the user looks at 9209 * this value, he'll know whether or not he allocated enough space 9210 * and reissue the command if necessary. We don't support well 9211 * known logical units, so if the user asks for that, return none. 9212 */ 9213 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9214 9215 /* 9216 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9217 * this request. 9218 */ 9219 ctsio->scsi_status = SCSI_STATUS_OK; 9220 9221 ctsio->be_move_done = ctl_config_move_done; 9222 ctl_datamove((union ctl_io *)ctsio); 9223 9224 return (retval); 9225} 9226 9227int 9228ctl_request_sense(struct ctl_scsiio *ctsio) 9229{ 9230 struct scsi_request_sense *cdb; 9231 struct scsi_sense_data *sense_ptr; 9232 struct ctl_lun *lun; 9233 uint32_t initidx; 9234 int have_error; 9235 scsi_sense_data_type sense_format; 9236 9237 cdb = (struct scsi_request_sense *)ctsio->cdb; 9238 9239 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9240 9241 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9242 9243 /* 9244 * Determine which sense format the user wants. 9245 */ 9246 if (cdb->byte2 & SRS_DESC) 9247 sense_format = SSD_TYPE_DESC; 9248 else 9249 sense_format = SSD_TYPE_FIXED; 9250 9251 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9252 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9253 ctsio->kern_sg_entries = 0; 9254 9255 /* 9256 * struct scsi_sense_data, which is currently set to 256 bytes, is 9257 * larger than the largest allowed value for the length field in the 9258 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9259 */ 9260 ctsio->residual = 0; 9261 ctsio->kern_data_len = cdb->length; 9262 ctsio->kern_total_len = cdb->length; 9263 9264 ctsio->kern_data_resid = 0; 9265 ctsio->kern_rel_offset = 0; 9266 ctsio->kern_sg_entries = 0; 9267 9268 /* 9269 * If we don't have a LUN, we don't have any pending sense. 9270 */ 9271 if (lun == NULL) 9272 goto no_sense; 9273 9274 have_error = 0; 9275 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9276 /* 9277 * Check for pending sense, and then for pending unit attentions. 9278 * Pending sense gets returned first, then pending unit attentions. 9279 */ 9280 mtx_lock(&lun->ctl_softc->ctl_lock); 9281 if (ctl_is_set(lun->have_ca, initidx)) { 9282 scsi_sense_data_type stored_format; 9283 9284 /* 9285 * Check to see which sense format was used for the stored 9286 * sense data. 9287 */ 9288 stored_format = scsi_sense_type( 9289 &lun->pending_sense[initidx].sense); 9290 9291 /* 9292 * If the user requested a different sense format than the 9293 * one we stored, then we need to convert it to the other 9294 * format. If we're going from descriptor to fixed format 9295 * sense data, we may lose things in translation, depending 9296 * on what options were used. 9297 * 9298 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9299 * for some reason we'll just copy it out as-is. 9300 */ 9301 if ((stored_format == SSD_TYPE_FIXED) 9302 && (sense_format == SSD_TYPE_DESC)) 9303 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9304 &lun->pending_sense[initidx].sense, 9305 (struct scsi_sense_data_desc *)sense_ptr); 9306 else if ((stored_format == SSD_TYPE_DESC) 9307 && (sense_format == SSD_TYPE_FIXED)) 9308 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9309 &lun->pending_sense[initidx].sense, 9310 (struct scsi_sense_data_fixed *)sense_ptr); 9311 else 9312 memcpy(sense_ptr, &lun->pending_sense[initidx].sense, 9313 ctl_min(sizeof(*sense_ptr), 9314 sizeof(lun->pending_sense[initidx].sense))); 9315 9316 ctl_clear_mask(lun->have_ca, initidx); 9317 have_error = 1; 9318 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) { 9319 ctl_ua_type ua_type; 9320 9321 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending, 9322 sense_ptr, sense_format); 9323 if (ua_type != CTL_UA_NONE) { 9324 have_error = 1; 9325 /* We're reporting this UA, so clear it */ 9326 lun->pending_sense[initidx].ua_pending &= ~ua_type; 9327 } 9328 } 9329 mtx_unlock(&lun->ctl_softc->ctl_lock); 9330 9331 /* 9332 * We already have a pending error, return it. 9333 */ 9334 if (have_error != 0) { 9335 /* 9336 * We report the SCSI status as OK, since the status of the 9337 * request sense command itself is OK. 9338 */ 9339 ctsio->scsi_status = SCSI_STATUS_OK; 9340 9341 /* 9342 * We report 0 for the sense length, because we aren't doing 9343 * autosense in this case. We're reporting sense as 9344 * parameter data. 9345 */ 9346 ctsio->sense_len = 0; 9347 9348 ctsio->be_move_done = ctl_config_move_done; 9349 ctl_datamove((union ctl_io *)ctsio); 9350 9351 return (CTL_RETVAL_COMPLETE); 9352 } 9353 9354no_sense: 9355 9356 /* 9357 * No sense information to report, so we report that everything is 9358 * okay. 9359 */ 9360 ctl_set_sense_data(sense_ptr, 9361 lun, 9362 sense_format, 9363 /*current_error*/ 1, 9364 /*sense_key*/ SSD_KEY_NO_SENSE, 9365 /*asc*/ 0x00, 9366 /*ascq*/ 0x00, 9367 SSD_ELEM_NONE); 9368 9369 ctsio->scsi_status = SCSI_STATUS_OK; 9370 9371 /* 9372 * We report 0 for the sense length, because we aren't doing 9373 * autosense in this case. We're reporting sense as parameter data. 9374 */ 9375 ctsio->sense_len = 0; 9376 ctsio->be_move_done = ctl_config_move_done; 9377 ctl_datamove((union ctl_io *)ctsio); 9378 9379 return (CTL_RETVAL_COMPLETE); 9380} 9381 9382int 9383ctl_tur(struct ctl_scsiio *ctsio) 9384{ 9385 struct ctl_lun *lun; 9386 9387 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9388 9389 CTL_DEBUG_PRINT(("ctl_tur\n")); 9390 9391 if (lun == NULL) 9392 return (-EINVAL); 9393 9394 ctsio->scsi_status = SCSI_STATUS_OK; 9395 ctsio->io_hdr.status = CTL_SUCCESS; 9396 9397 ctl_done((union ctl_io *)ctsio); 9398 9399 return (CTL_RETVAL_COMPLETE); 9400} 9401 9402#ifdef notyet 9403static int 9404ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9405{ 9406 9407} 9408#endif 9409 9410static int 9411ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9412{ 9413 struct scsi_vpd_supported_pages *pages; 9414 int sup_page_size; 9415 struct ctl_lun *lun; 9416 9417 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9418 9419 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9420 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9421 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9422 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9423 ctsio->kern_sg_entries = 0; 9424 9425 if (sup_page_size < alloc_len) { 9426 ctsio->residual = alloc_len - sup_page_size; 9427 ctsio->kern_data_len = sup_page_size; 9428 ctsio->kern_total_len = sup_page_size; 9429 } else { 9430 ctsio->residual = 0; 9431 ctsio->kern_data_len = alloc_len; 9432 ctsio->kern_total_len = alloc_len; 9433 } 9434 ctsio->kern_data_resid = 0; 9435 ctsio->kern_rel_offset = 0; 9436 ctsio->kern_sg_entries = 0; 9437 9438 /* 9439 * The control device is always connected. The disk device, on the 9440 * other hand, may not be online all the time. Need to change this 9441 * to figure out whether the disk device is actually online or not. 9442 */ 9443 if (lun != NULL) 9444 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9445 lun->be_lun->lun_type; 9446 else 9447 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9448 9449 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9450 /* Supported VPD pages */ 9451 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9452 /* Serial Number */ 9453 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9454 /* Device Identification */ 9455 pages->page_list[2] = SVPD_DEVICE_ID; 9456 /* Block limits */ 9457 pages->page_list[3] = SVPD_BLOCK_LIMITS; 9458 /* Logical Block Provisioning */ 9459 pages->page_list[4] = SVPD_LBP; 9460 9461 ctsio->scsi_status = SCSI_STATUS_OK; 9462 9463 ctsio->be_move_done = ctl_config_move_done; 9464 ctl_datamove((union ctl_io *)ctsio); 9465 9466 return (CTL_RETVAL_COMPLETE); 9467} 9468 9469static int 9470ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9471{ 9472 struct scsi_vpd_unit_serial_number *sn_ptr; 9473 struct ctl_lun *lun; 9474 9475 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9476 9477 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9478 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9479 ctsio->kern_sg_entries = 0; 9480 9481 if (sizeof(*sn_ptr) < alloc_len) { 9482 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9483 ctsio->kern_data_len = sizeof(*sn_ptr); 9484 ctsio->kern_total_len = sizeof(*sn_ptr); 9485 } else { 9486 ctsio->residual = 0; 9487 ctsio->kern_data_len = alloc_len; 9488 ctsio->kern_total_len = alloc_len; 9489 } 9490 ctsio->kern_data_resid = 0; 9491 ctsio->kern_rel_offset = 0; 9492 ctsio->kern_sg_entries = 0; 9493 9494 /* 9495 * The control device is always connected. The disk device, on the 9496 * other hand, may not be online all the time. Need to change this 9497 * to figure out whether the disk device is actually online or not. 9498 */ 9499 if (lun != NULL) 9500 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9501 lun->be_lun->lun_type; 9502 else 9503 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9504 9505 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9506 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9507 /* 9508 * If we don't have a LUN, we just leave the serial number as 9509 * all spaces. 9510 */ 9511 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9512 if (lun != NULL) { 9513 strncpy((char *)sn_ptr->serial_num, 9514 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9515 } 9516 ctsio->scsi_status = SCSI_STATUS_OK; 9517 9518 ctsio->be_move_done = ctl_config_move_done; 9519 ctl_datamove((union ctl_io *)ctsio); 9520 9521 return (CTL_RETVAL_COMPLETE); 9522} 9523 9524 9525static int 9526ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9527{ 9528 struct scsi_vpd_device_id *devid_ptr; 9529 struct scsi_vpd_id_descriptor *desc, *desc1; 9530 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */ 9531 struct scsi_vpd_id_t10 *t10id; 9532 struct ctl_softc *ctl_softc; 9533 struct ctl_lun *lun; 9534 struct ctl_frontend *fe; 9535 char *val; 9536 int data_len, devid_len; 9537 9538 ctl_softc = control_softc; 9539 9540 mtx_lock(&ctl_softc->ctl_lock); 9541 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9542 mtx_unlock(&ctl_softc->ctl_lock); 9543 9544 if (fe->devid != NULL) 9545 return ((fe->devid)(ctsio, alloc_len)); 9546 9547 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9548 9549 if (lun == NULL) { 9550 devid_len = CTL_DEVID_MIN_LEN; 9551 } else { 9552 devid_len = max(CTL_DEVID_MIN_LEN, 9553 strnlen(lun->be_lun->device_id, CTL_DEVID_LEN)); 9554 } 9555 9556 data_len = sizeof(struct scsi_vpd_device_id) + 9557 sizeof(struct scsi_vpd_id_descriptor) + 9558 sizeof(struct scsi_vpd_id_t10) + devid_len + 9559 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN + 9560 sizeof(struct scsi_vpd_id_descriptor) + 9561 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9562 sizeof(struct scsi_vpd_id_descriptor) + 9563 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9564 9565 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9566 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9567 ctsio->kern_sg_entries = 0; 9568 9569 if (data_len < alloc_len) { 9570 ctsio->residual = alloc_len - data_len; 9571 ctsio->kern_data_len = data_len; 9572 ctsio->kern_total_len = data_len; 9573 } else { 9574 ctsio->residual = 0; 9575 ctsio->kern_data_len = alloc_len; 9576 ctsio->kern_total_len = alloc_len; 9577 } 9578 ctsio->kern_data_resid = 0; 9579 ctsio->kern_rel_offset = 0; 9580 ctsio->kern_sg_entries = 0; 9581 9582 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9583 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 9584 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9585 sizeof(struct scsi_vpd_id_t10) + devid_len); 9586 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] + 9587 CTL_WWPN_LEN); 9588 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] + 9589 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9590 9591 /* 9592 * The control device is always connected. The disk device, on the 9593 * other hand, may not be online all the time. 9594 */ 9595 if (lun != NULL) 9596 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9597 lun->be_lun->lun_type; 9598 else 9599 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9600 9601 devid_ptr->page_code = SVPD_DEVICE_ID; 9602 9603 scsi_ulto2b(data_len - 4, devid_ptr->length); 9604 9605 mtx_lock(&ctl_softc->ctl_lock); 9606 9607 /* 9608 * For Fibre channel, 9609 */ 9610 if (fe->port_type == CTL_PORT_FC) 9611 { 9612 desc->proto_codeset = (SCSI_PROTO_FC << 4) | 9613 SVPD_ID_CODESET_ASCII; 9614 desc1->proto_codeset = (SCSI_PROTO_FC << 4) | 9615 SVPD_ID_CODESET_BINARY; 9616 } 9617 else 9618 { 9619 desc->proto_codeset = (SCSI_PROTO_SPI << 4) | 9620 SVPD_ID_CODESET_ASCII; 9621 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) | 9622 SVPD_ID_CODESET_BINARY; 9623 } 9624 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset; 9625 mtx_unlock(&ctl_softc->ctl_lock); 9626 9627 /* 9628 * We're using a LUN association here. i.e., this device ID is a 9629 * per-LUN identifier. 9630 */ 9631 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 9632 desc->length = sizeof(*t10id) + devid_len; 9633 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) { 9634 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 9635 } else { 9636 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 9637 strncpy(t10id->vendor, val, 9638 min(sizeof(t10id->vendor), strlen(val))); 9639 } 9640 9641 /* 9642 * desc1 is for the WWPN which is a port asscociation. 9643 */ 9644 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA; 9645 desc1->length = CTL_WWPN_LEN; 9646 /* XXX Call Reggie's get_WWNN func here then add port # to the end */ 9647 /* For testing just create the WWPN */ 9648#if 0 9649 ddb_GetWWNN((char *)desc1->identifier); 9650 9651 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9652 /* This is so Copancontrol will return something sane */ 9653 if (ctsio->io_hdr.nexus.targ_port!=0 && 9654 ctsio->io_hdr.nexus.targ_port!=8) 9655 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1; 9656 else 9657 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port; 9658#endif 9659 9660 be64enc(desc1->identifier, fe->wwpn); 9661 9662 /* 9663 * desc2 is for the Relative Target Port(type 4h) identifier 9664 */ 9665 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9666 | SVPD_ID_TYPE_RELTARG; 9667 desc2->length = 4; 9668//#if 0 9669 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9670 /* This is so Copancontrol will return something sane */ 9671 if (ctsio->io_hdr.nexus.targ_port!=0 && 9672 ctsio->io_hdr.nexus.targ_port!=8) 9673 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1; 9674 else 9675 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port; 9676//#endif 9677 9678 /* 9679 * desc3 is for the Target Port Group(type 5h) identifier 9680 */ 9681 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9682 | SVPD_ID_TYPE_TPORTGRP; 9683 desc3->length = 4; 9684 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single) 9685 desc3->identifier[3] = 1; 9686 else 9687 desc3->identifier[3] = 2; 9688 9689 /* 9690 * If we've actually got a backend, copy the device id from the 9691 * per-LUN data. Otherwise, set it to all spaces. 9692 */ 9693 if (lun != NULL) { 9694 /* 9695 * Copy the backend's LUN ID. 9696 */ 9697 strncpy((char *)t10id->vendor_spec_id, 9698 (char *)lun->be_lun->device_id, devid_len); 9699 } else { 9700 /* 9701 * No backend, set this to spaces. 9702 */ 9703 memset(t10id->vendor_spec_id, 0x20, devid_len); 9704 } 9705 9706 ctsio->scsi_status = SCSI_STATUS_OK; 9707 9708 ctsio->be_move_done = ctl_config_move_done; 9709 ctl_datamove((union ctl_io *)ctsio); 9710 9711 return (CTL_RETVAL_COMPLETE); 9712} 9713 9714static int 9715ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9716{ 9717 struct scsi_vpd_block_limits *bl_ptr; 9718 struct ctl_lun *lun; 9719 int bs; 9720 9721 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9722 bs = lun->be_lun->blocksize; 9723 9724 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9725 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9726 ctsio->kern_sg_entries = 0; 9727 9728 if (sizeof(*bl_ptr) < alloc_len) { 9729 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9730 ctsio->kern_data_len = sizeof(*bl_ptr); 9731 ctsio->kern_total_len = sizeof(*bl_ptr); 9732 } else { 9733 ctsio->residual = 0; 9734 ctsio->kern_data_len = alloc_len; 9735 ctsio->kern_total_len = alloc_len; 9736 } 9737 ctsio->kern_data_resid = 0; 9738 ctsio->kern_rel_offset = 0; 9739 ctsio->kern_sg_entries = 0; 9740 9741 /* 9742 * The control device is always connected. The disk device, on the 9743 * other hand, may not be online all the time. Need to change this 9744 * to figure out whether the disk device is actually online or not. 9745 */ 9746 if (lun != NULL) 9747 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9748 lun->be_lun->lun_type; 9749 else 9750 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9751 9752 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9753 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 9754 bl_ptr->max_cmp_write_len = 0xff; 9755 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9756 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 9757 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9758 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9759 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9760 } 9761 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9762 9763 ctsio->scsi_status = SCSI_STATUS_OK; 9764 ctsio->be_move_done = ctl_config_move_done; 9765 ctl_datamove((union ctl_io *)ctsio); 9766 9767 return (CTL_RETVAL_COMPLETE); 9768} 9769 9770static int 9771ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9772{ 9773 struct scsi_vpd_logical_block_prov *lbp_ptr; 9774 struct ctl_lun *lun; 9775 int bs; 9776 9777 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9778 bs = lun->be_lun->blocksize; 9779 9780 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9781 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9782 ctsio->kern_sg_entries = 0; 9783 9784 if (sizeof(*lbp_ptr) < alloc_len) { 9785 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9786 ctsio->kern_data_len = sizeof(*lbp_ptr); 9787 ctsio->kern_total_len = sizeof(*lbp_ptr); 9788 } else { 9789 ctsio->residual = 0; 9790 ctsio->kern_data_len = alloc_len; 9791 ctsio->kern_total_len = alloc_len; 9792 } 9793 ctsio->kern_data_resid = 0; 9794 ctsio->kern_rel_offset = 0; 9795 ctsio->kern_sg_entries = 0; 9796 9797 /* 9798 * The control device is always connected. The disk device, on the 9799 * other hand, may not be online all the time. Need to change this 9800 * to figure out whether the disk device is actually online or not. 9801 */ 9802 if (lun != NULL) 9803 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9804 lun->be_lun->lun_type; 9805 else 9806 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9807 9808 lbp_ptr->page_code = SVPD_LBP; 9809 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 9810 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10; 9811 9812 ctsio->scsi_status = SCSI_STATUS_OK; 9813 ctsio->be_move_done = ctl_config_move_done; 9814 ctl_datamove((union ctl_io *)ctsio); 9815 9816 return (CTL_RETVAL_COMPLETE); 9817} 9818 9819static int 9820ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9821{ 9822 struct scsi_inquiry *cdb; 9823 struct ctl_lun *lun; 9824 int alloc_len, retval; 9825 9826 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9827 cdb = (struct scsi_inquiry *)ctsio->cdb; 9828 9829 retval = CTL_RETVAL_COMPLETE; 9830 9831 alloc_len = scsi_2btoul(cdb->length); 9832 9833 switch (cdb->page_code) { 9834 case SVPD_SUPPORTED_PAGES: 9835 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9836 break; 9837 case SVPD_UNIT_SERIAL_NUMBER: 9838 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9839 break; 9840 case SVPD_DEVICE_ID: 9841 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9842 break; 9843 case SVPD_BLOCK_LIMITS: 9844 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9845 break; 9846 case SVPD_LBP: 9847 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9848 break; 9849 default: 9850 ctl_set_invalid_field(ctsio, 9851 /*sks_valid*/ 1, 9852 /*command*/ 1, 9853 /*field*/ 2, 9854 /*bit_valid*/ 0, 9855 /*bit*/ 0); 9856 ctl_done((union ctl_io *)ctsio); 9857 retval = CTL_RETVAL_COMPLETE; 9858 break; 9859 } 9860 9861 return (retval); 9862} 9863 9864static int 9865ctl_inquiry_std(struct ctl_scsiio *ctsio) 9866{ 9867 struct scsi_inquiry_data *inq_ptr; 9868 struct scsi_inquiry *cdb; 9869 struct ctl_softc *ctl_softc; 9870 struct ctl_lun *lun; 9871 char *val; 9872 uint32_t alloc_len; 9873 int is_fc; 9874 9875 ctl_softc = control_softc; 9876 9877 /* 9878 * Figure out whether we're talking to a Fibre Channel port or not. 9879 * We treat the ioctl front end, and any SCSI adapters, as packetized 9880 * SCSI front ends. 9881 */ 9882 mtx_lock(&ctl_softc->ctl_lock); 9883 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type != 9884 CTL_PORT_FC) 9885 is_fc = 0; 9886 else 9887 is_fc = 1; 9888 mtx_unlock(&ctl_softc->ctl_lock); 9889 9890 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9891 cdb = (struct scsi_inquiry *)ctsio->cdb; 9892 alloc_len = scsi_2btoul(cdb->length); 9893 9894 /* 9895 * We malloc the full inquiry data size here and fill it 9896 * in. If the user only asks for less, we'll give him 9897 * that much. 9898 */ 9899 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 9900 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 9901 ctsio->kern_sg_entries = 0; 9902 ctsio->kern_data_resid = 0; 9903 ctsio->kern_rel_offset = 0; 9904 9905 if (sizeof(*inq_ptr) < alloc_len) { 9906 ctsio->residual = alloc_len - sizeof(*inq_ptr); 9907 ctsio->kern_data_len = sizeof(*inq_ptr); 9908 ctsio->kern_total_len = sizeof(*inq_ptr); 9909 } else { 9910 ctsio->residual = 0; 9911 ctsio->kern_data_len = alloc_len; 9912 ctsio->kern_total_len = alloc_len; 9913 } 9914 9915 /* 9916 * If we have a LUN configured, report it as connected. Otherwise, 9917 * report that it is offline or no device is supported, depending 9918 * on the value of inquiry_pq_no_lun. 9919 * 9920 * According to the spec (SPC-4 r34), the peripheral qualifier 9921 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 9922 * 9923 * "A peripheral device having the specified peripheral device type 9924 * is not connected to this logical unit. However, the device 9925 * server is capable of supporting the specified peripheral device 9926 * type on this logical unit." 9927 * 9928 * According to the same spec, the peripheral qualifier 9929 * SID_QUAL_BAD_LU (011b) is used in this scenario: 9930 * 9931 * "The device server is not capable of supporting a peripheral 9932 * device on this logical unit. For this peripheral qualifier the 9933 * peripheral device type shall be set to 1Fh. All other peripheral 9934 * device type values are reserved for this peripheral qualifier." 9935 * 9936 * Given the text, it would seem that we probably want to report that 9937 * the LUN is offline here. There is no LUN connected, but we can 9938 * support a LUN at the given LUN number. 9939 * 9940 * In the real world, though, it sounds like things are a little 9941 * different: 9942 * 9943 * - Linux, when presented with a LUN with the offline peripheral 9944 * qualifier, will create an sg driver instance for it. So when 9945 * you attach it to CTL, you wind up with a ton of sg driver 9946 * instances. (One for every LUN that Linux bothered to probe.) 9947 * Linux does this despite the fact that it issues a REPORT LUNs 9948 * to LUN 0 to get the inventory of supported LUNs. 9949 * 9950 * - There is other anecdotal evidence (from Emulex folks) about 9951 * arrays that use the offline peripheral qualifier for LUNs that 9952 * are on the "passive" path in an active/passive array. 9953 * 9954 * So the solution is provide a hopefully reasonable default 9955 * (return bad/no LUN) and allow the user to change the behavior 9956 * with a tunable/sysctl variable. 9957 */ 9958 if (lun != NULL) 9959 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9960 lun->be_lun->lun_type; 9961 else if (ctl_softc->inquiry_pq_no_lun == 0) 9962 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9963 else 9964 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 9965 9966 /* RMB in byte 2 is 0 */ 9967 inq_ptr->version = SCSI_REV_SPC3; 9968 9969 /* 9970 * According to SAM-3, even if a device only supports a single 9971 * level of LUN addressing, it should still set the HISUP bit: 9972 * 9973 * 4.9.1 Logical unit numbers overview 9974 * 9975 * All logical unit number formats described in this standard are 9976 * hierarchical in structure even when only a single level in that 9977 * hierarchy is used. The HISUP bit shall be set to one in the 9978 * standard INQUIRY data (see SPC-2) when any logical unit number 9979 * format described in this standard is used. Non-hierarchical 9980 * formats are outside the scope of this standard. 9981 * 9982 * Therefore we set the HiSup bit here. 9983 * 9984 * The reponse format is 2, per SPC-3. 9985 */ 9986 inq_ptr->response_format = SID_HiSup | 2; 9987 9988 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 9989 CTL_DEBUG_PRINT(("additional_length = %d\n", 9990 inq_ptr->additional_length)); 9991 9992 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT; 9993 /* 16 bit addressing */ 9994 if (is_fc == 0) 9995 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 9996 /* XXX set the SID_MultiP bit here if we're actually going to 9997 respond on multiple ports */ 9998 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 9999 10000 /* 16 bit data bus, synchronous transfers */ 10001 /* XXX these flags don't apply for FC */ 10002 if (is_fc == 0) 10003 inq_ptr->flags = SID_WBus16 | SID_Sync; 10004 /* 10005 * XXX KDM do we want to support tagged queueing on the control 10006 * device at all? 10007 */ 10008 if ((lun == NULL) 10009 || (lun->be_lun->lun_type != T_PROCESSOR)) 10010 inq_ptr->flags |= SID_CmdQue; 10011 /* 10012 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10013 * We have 8 bytes for the vendor name, and 16 bytes for the device 10014 * name and 4 bytes for the revision. 10015 */ 10016 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) { 10017 strcpy(inq_ptr->vendor, CTL_VENDOR); 10018 } else { 10019 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10020 strncpy(inq_ptr->vendor, val, 10021 min(sizeof(inq_ptr->vendor), strlen(val))); 10022 } 10023 if (lun == NULL) { 10024 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10025 } else if ((val = ctl_get_opt(lun->be_lun, "product")) == NULL) { 10026 switch (lun->be_lun->lun_type) { 10027 case T_DIRECT: 10028 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10029 break; 10030 case T_PROCESSOR: 10031 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10032 break; 10033 default: 10034 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10035 break; 10036 } 10037 } else { 10038 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10039 strncpy(inq_ptr->product, val, 10040 min(sizeof(inq_ptr->product), strlen(val))); 10041 } 10042 10043 /* 10044 * XXX make this a macro somewhere so it automatically gets 10045 * incremented when we make changes. 10046 */ 10047 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "revision")) == NULL) { 10048 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10049 } else { 10050 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10051 strncpy(inq_ptr->revision, val, 10052 min(sizeof(inq_ptr->revision), strlen(val))); 10053 } 10054 10055 /* 10056 * For parallel SCSI, we support double transition and single 10057 * transition clocking. We also support QAS (Quick Arbitration 10058 * and Selection) and Information Unit transfers on both the 10059 * control and array devices. 10060 */ 10061 if (is_fc == 0) 10062 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10063 SID_SPI_IUS; 10064 10065 /* SAM-3 */ 10066 scsi_ulto2b(0x0060, inq_ptr->version1); 10067 /* SPC-3 (no version claimed) XXX should we claim a version? */ 10068 scsi_ulto2b(0x0300, inq_ptr->version2); 10069 if (is_fc) { 10070 /* FCP-2 ANSI INCITS.350:2003 */ 10071 scsi_ulto2b(0x0917, inq_ptr->version3); 10072 } else { 10073 /* SPI-4 ANSI INCITS.362:200x */ 10074 scsi_ulto2b(0x0B56, inq_ptr->version3); 10075 } 10076 10077 if (lun == NULL) { 10078 /* SBC-2 (no version claimed) XXX should we claim a version? */ 10079 scsi_ulto2b(0x0320, inq_ptr->version4); 10080 } else { 10081 switch (lun->be_lun->lun_type) { 10082 case T_DIRECT: 10083 /* 10084 * SBC-2 (no version claimed) XXX should we claim a 10085 * version? 10086 */ 10087 scsi_ulto2b(0x0320, inq_ptr->version4); 10088 break; 10089 case T_PROCESSOR: 10090 default: 10091 break; 10092 } 10093 } 10094 10095 ctsio->scsi_status = SCSI_STATUS_OK; 10096 if (ctsio->kern_data_len > 0) { 10097 ctsio->be_move_done = ctl_config_move_done; 10098 ctl_datamove((union ctl_io *)ctsio); 10099 } else { 10100 ctsio->io_hdr.status = CTL_SUCCESS; 10101 ctl_done((union ctl_io *)ctsio); 10102 } 10103 10104 return (CTL_RETVAL_COMPLETE); 10105} 10106 10107int 10108ctl_inquiry(struct ctl_scsiio *ctsio) 10109{ 10110 struct scsi_inquiry *cdb; 10111 int retval; 10112 10113 cdb = (struct scsi_inquiry *)ctsio->cdb; 10114 10115 retval = 0; 10116 10117 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10118 10119 /* 10120 * Right now, we don't support the CmdDt inquiry information. 10121 * This would be nice to support in the future. When we do 10122 * support it, we should change this test so that it checks to make 10123 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10124 */ 10125#ifdef notyet 10126 if (((cdb->byte2 & SI_EVPD) 10127 && (cdb->byte2 & SI_CMDDT))) 10128#endif 10129 if (cdb->byte2 & SI_CMDDT) { 10130 /* 10131 * Point to the SI_CMDDT bit. We might change this 10132 * when we support SI_CMDDT, but since both bits would be 10133 * "wrong", this should probably just stay as-is then. 10134 */ 10135 ctl_set_invalid_field(ctsio, 10136 /*sks_valid*/ 1, 10137 /*command*/ 1, 10138 /*field*/ 1, 10139 /*bit_valid*/ 1, 10140 /*bit*/ 1); 10141 ctl_done((union ctl_io *)ctsio); 10142 return (CTL_RETVAL_COMPLETE); 10143 } 10144 if (cdb->byte2 & SI_EVPD) 10145 retval = ctl_inquiry_evpd(ctsio); 10146#ifdef notyet 10147 else if (cdb->byte2 & SI_CMDDT) 10148 retval = ctl_inquiry_cmddt(ctsio); 10149#endif 10150 else 10151 retval = ctl_inquiry_std(ctsio); 10152 10153 return (retval); 10154} 10155 10156/* 10157 * For known CDB types, parse the LBA and length. 10158 */ 10159static int 10160ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10161{ 10162 if (io->io_hdr.io_type != CTL_IO_SCSI) 10163 return (1); 10164 10165 switch (io->scsiio.cdb[0]) { 10166 case COMPARE_AND_WRITE: { 10167 struct scsi_compare_and_write *cdb; 10168 10169 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10170 10171 *lba = scsi_8btou64(cdb->addr); 10172 *len = cdb->length; 10173 break; 10174 } 10175 case READ_6: 10176 case WRITE_6: { 10177 struct scsi_rw_6 *cdb; 10178 10179 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10180 10181 *lba = scsi_3btoul(cdb->addr); 10182 /* only 5 bits are valid in the most significant address byte */ 10183 *lba &= 0x1fffff; 10184 *len = cdb->length; 10185 break; 10186 } 10187 case READ_10: 10188 case WRITE_10: { 10189 struct scsi_rw_10 *cdb; 10190 10191 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10192 10193 *lba = scsi_4btoul(cdb->addr); 10194 *len = scsi_2btoul(cdb->length); 10195 break; 10196 } 10197 case WRITE_VERIFY_10: { 10198 struct scsi_write_verify_10 *cdb; 10199 10200 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10201 10202 *lba = scsi_4btoul(cdb->addr); 10203 *len = scsi_2btoul(cdb->length); 10204 break; 10205 } 10206 case READ_12: 10207 case WRITE_12: { 10208 struct scsi_rw_12 *cdb; 10209 10210 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10211 10212 *lba = scsi_4btoul(cdb->addr); 10213 *len = scsi_4btoul(cdb->length); 10214 break; 10215 } 10216 case WRITE_VERIFY_12: { 10217 struct scsi_write_verify_12 *cdb; 10218 10219 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10220 10221 *lba = scsi_4btoul(cdb->addr); 10222 *len = scsi_4btoul(cdb->length); 10223 break; 10224 } 10225 case READ_16: 10226 case WRITE_16: { 10227 struct scsi_rw_16 *cdb; 10228 10229 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10230 10231 *lba = scsi_8btou64(cdb->addr); 10232 *len = scsi_4btoul(cdb->length); 10233 break; 10234 } 10235 case WRITE_VERIFY_16: { 10236 struct scsi_write_verify_16 *cdb; 10237 10238 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10239 10240 10241 *lba = scsi_8btou64(cdb->addr); 10242 *len = scsi_4btoul(cdb->length); 10243 break; 10244 } 10245 case WRITE_SAME_10: { 10246 struct scsi_write_same_10 *cdb; 10247 10248 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10249 10250 *lba = scsi_4btoul(cdb->addr); 10251 *len = scsi_2btoul(cdb->length); 10252 break; 10253 } 10254 case WRITE_SAME_16: { 10255 struct scsi_write_same_16 *cdb; 10256 10257 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10258 10259 *lba = scsi_8btou64(cdb->addr); 10260 *len = scsi_4btoul(cdb->length); 10261 break; 10262 } 10263 case VERIFY_10: { 10264 struct scsi_verify_10 *cdb; 10265 10266 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10267 10268 *lba = scsi_4btoul(cdb->addr); 10269 *len = scsi_2btoul(cdb->length); 10270 break; 10271 } 10272 case VERIFY_12: { 10273 struct scsi_verify_12 *cdb; 10274 10275 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10276 10277 *lba = scsi_4btoul(cdb->addr); 10278 *len = scsi_4btoul(cdb->length); 10279 break; 10280 } 10281 case VERIFY_16: { 10282 struct scsi_verify_16 *cdb; 10283 10284 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10285 10286 *lba = scsi_8btou64(cdb->addr); 10287 *len = scsi_4btoul(cdb->length); 10288 break; 10289 } 10290 default: 10291 return (1); 10292 break; /* NOTREACHED */ 10293 } 10294 10295 return (0); 10296} 10297 10298static ctl_action 10299ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10300{ 10301 uint64_t endlba1, endlba2; 10302 10303 endlba1 = lba1 + len1 - 1; 10304 endlba2 = lba2 + len2 - 1; 10305 10306 if ((endlba1 < lba2) 10307 || (endlba2 < lba1)) 10308 return (CTL_ACTION_PASS); 10309 else 10310 return (CTL_ACTION_BLOCK); 10311} 10312 10313static ctl_action 10314ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10315{ 10316 uint64_t lba1, lba2; 10317 uint32_t len1, len2; 10318 int retval; 10319 10320 retval = ctl_get_lba_len(io1, &lba1, &len1); 10321 if (retval != 0) 10322 return (CTL_ACTION_ERROR); 10323 10324 retval = ctl_get_lba_len(io2, &lba2, &len2); 10325 if (retval != 0) 10326 return (CTL_ACTION_ERROR); 10327 10328 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10329} 10330 10331static ctl_action 10332ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10333{ 10334 struct ctl_cmd_entry *pending_entry, *ooa_entry; 10335 ctl_serialize_action *serialize_row; 10336 10337 /* 10338 * The initiator attempted multiple untagged commands at the same 10339 * time. Can't do that. 10340 */ 10341 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10342 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10343 && ((pending_io->io_hdr.nexus.targ_port == 10344 ooa_io->io_hdr.nexus.targ_port) 10345 && (pending_io->io_hdr.nexus.initid.id == 10346 ooa_io->io_hdr.nexus.initid.id)) 10347 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10348 return (CTL_ACTION_OVERLAP); 10349 10350 /* 10351 * The initiator attempted to send multiple tagged commands with 10352 * the same ID. (It's fine if different initiators have the same 10353 * tag ID.) 10354 * 10355 * Even if all of those conditions are true, we don't kill the I/O 10356 * if the command ahead of us has been aborted. We won't end up 10357 * sending it to the FETD, and it's perfectly legal to resend a 10358 * command with the same tag number as long as the previous 10359 * instance of this tag number has been aborted somehow. 10360 */ 10361 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10362 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10363 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10364 && ((pending_io->io_hdr.nexus.targ_port == 10365 ooa_io->io_hdr.nexus.targ_port) 10366 && (pending_io->io_hdr.nexus.initid.id == 10367 ooa_io->io_hdr.nexus.initid.id)) 10368 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10369 return (CTL_ACTION_OVERLAP_TAG); 10370 10371 /* 10372 * If we get a head of queue tag, SAM-3 says that we should 10373 * immediately execute it. 10374 * 10375 * What happens if this command would normally block for some other 10376 * reason? e.g. a request sense with a head of queue tag 10377 * immediately after a write. Normally that would block, but this 10378 * will result in its getting executed immediately... 10379 * 10380 * We currently return "pass" instead of "skip", so we'll end up 10381 * going through the rest of the queue to check for overlapped tags. 10382 * 10383 * XXX KDM check for other types of blockage first?? 10384 */ 10385 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10386 return (CTL_ACTION_PASS); 10387 10388 /* 10389 * Ordered tags have to block until all items ahead of them 10390 * have completed. If we get called with an ordered tag, we always 10391 * block, if something else is ahead of us in the queue. 10392 */ 10393 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10394 return (CTL_ACTION_BLOCK); 10395 10396 /* 10397 * Simple tags get blocked until all head of queue and ordered tags 10398 * ahead of them have completed. I'm lumping untagged commands in 10399 * with simple tags here. XXX KDM is that the right thing to do? 10400 */ 10401 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10402 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10403 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10404 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10405 return (CTL_ACTION_BLOCK); 10406 10407 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]]; 10408 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]]; 10409 10410 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10411 10412 switch (serialize_row[pending_entry->seridx]) { 10413 case CTL_SER_BLOCK: 10414 return (CTL_ACTION_BLOCK); 10415 break; /* NOTREACHED */ 10416 case CTL_SER_EXTENT: 10417 return (ctl_extent_check(pending_io, ooa_io)); 10418 break; /* NOTREACHED */ 10419 case CTL_SER_PASS: 10420 return (CTL_ACTION_PASS); 10421 break; /* NOTREACHED */ 10422 case CTL_SER_SKIP: 10423 return (CTL_ACTION_SKIP); 10424 break; 10425 default: 10426 panic("invalid serialization value %d", 10427 serialize_row[pending_entry->seridx]); 10428 break; /* NOTREACHED */ 10429 } 10430 10431 return (CTL_ACTION_ERROR); 10432} 10433 10434/* 10435 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10436 * Assumptions: 10437 * - pending_io is generally either incoming, or on the blocked queue 10438 * - starting I/O is the I/O we want to start the check with. 10439 */ 10440static ctl_action 10441ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10442 union ctl_io *starting_io) 10443{ 10444 union ctl_io *ooa_io; 10445 ctl_action action; 10446 10447 mtx_assert(&control_softc->ctl_lock, MA_OWNED); 10448 10449 /* 10450 * Run back along the OOA queue, starting with the current 10451 * blocked I/O and going through every I/O before it on the 10452 * queue. If starting_io is NULL, we'll just end up returning 10453 * CTL_ACTION_PASS. 10454 */ 10455 for (ooa_io = starting_io; ooa_io != NULL; 10456 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10457 ooa_links)){ 10458 10459 /* 10460 * This routine just checks to see whether 10461 * cur_blocked is blocked by ooa_io, which is ahead 10462 * of it in the queue. It doesn't queue/dequeue 10463 * cur_blocked. 10464 */ 10465 action = ctl_check_for_blockage(pending_io, ooa_io); 10466 switch (action) { 10467 case CTL_ACTION_BLOCK: 10468 case CTL_ACTION_OVERLAP: 10469 case CTL_ACTION_OVERLAP_TAG: 10470 case CTL_ACTION_SKIP: 10471 case CTL_ACTION_ERROR: 10472 return (action); 10473 break; /* NOTREACHED */ 10474 case CTL_ACTION_PASS: 10475 break; 10476 default: 10477 panic("invalid action %d", action); 10478 break; /* NOTREACHED */ 10479 } 10480 } 10481 10482 return (CTL_ACTION_PASS); 10483} 10484 10485/* 10486 * Assumptions: 10487 * - An I/O has just completed, and has been removed from the per-LUN OOA 10488 * queue, so some items on the blocked queue may now be unblocked. 10489 */ 10490static int 10491ctl_check_blocked(struct ctl_lun *lun) 10492{ 10493 union ctl_io *cur_blocked, *next_blocked; 10494 10495 mtx_assert(&control_softc->ctl_lock, MA_OWNED); 10496 10497 /* 10498 * Run forward from the head of the blocked queue, checking each 10499 * entry against the I/Os prior to it on the OOA queue to see if 10500 * there is still any blockage. 10501 * 10502 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10503 * with our removing a variable on it while it is traversing the 10504 * list. 10505 */ 10506 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10507 cur_blocked != NULL; cur_blocked = next_blocked) { 10508 union ctl_io *prev_ooa; 10509 ctl_action action; 10510 10511 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10512 blocked_links); 10513 10514 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10515 ctl_ooaq, ooa_links); 10516 10517 /* 10518 * If cur_blocked happens to be the first item in the OOA 10519 * queue now, prev_ooa will be NULL, and the action 10520 * returned will just be CTL_ACTION_PASS. 10521 */ 10522 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10523 10524 switch (action) { 10525 case CTL_ACTION_BLOCK: 10526 /* Nothing to do here, still blocked */ 10527 break; 10528 case CTL_ACTION_OVERLAP: 10529 case CTL_ACTION_OVERLAP_TAG: 10530 /* 10531 * This shouldn't happen! In theory we've already 10532 * checked this command for overlap... 10533 */ 10534 break; 10535 case CTL_ACTION_PASS: 10536 case CTL_ACTION_SKIP: { 10537 struct ctl_softc *softc; 10538 struct ctl_cmd_entry *entry; 10539 uint32_t initidx; 10540 uint8_t opcode; 10541 int isc_retval; 10542 10543 /* 10544 * The skip case shouldn't happen, this transaction 10545 * should have never made it onto the blocked queue. 10546 */ 10547 /* 10548 * This I/O is no longer blocked, we can remove it 10549 * from the blocked queue. Since this is a TAILQ 10550 * (doubly linked list), we can do O(1) removals 10551 * from any place on the list. 10552 */ 10553 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10554 blocked_links); 10555 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10556 10557 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10558 /* 10559 * Need to send IO back to original side to 10560 * run 10561 */ 10562 union ctl_ha_msg msg_info; 10563 10564 msg_info.hdr.original_sc = 10565 cur_blocked->io_hdr.original_sc; 10566 msg_info.hdr.serializing_sc = cur_blocked; 10567 msg_info.hdr.msg_type = CTL_MSG_R2R; 10568 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10569 &msg_info, sizeof(msg_info), 0)) > 10570 CTL_HA_STATUS_SUCCESS) { 10571 printf("CTL:Check Blocked error from " 10572 "ctl_ha_msg_send %d\n", 10573 isc_retval); 10574 } 10575 break; 10576 } 10577 opcode = cur_blocked->scsiio.cdb[0]; 10578 entry = &ctl_cmd_table[opcode]; 10579 softc = control_softc; 10580 10581 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 10582 10583 /* 10584 * Check this I/O for LUN state changes that may 10585 * have happened while this command was blocked. 10586 * The LUN state may have been changed by a command 10587 * ahead of us in the queue, so we need to re-check 10588 * for any states that can be caused by SCSI 10589 * commands. 10590 */ 10591 if (ctl_scsiio_lun_check(softc, lun, entry, 10592 &cur_blocked->scsiio) == 0) { 10593 cur_blocked->io_hdr.flags |= 10594 CTL_FLAG_IS_WAS_ON_RTR; 10595 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue, 10596 &cur_blocked->io_hdr, links); 10597 /* 10598 * In the non CTL_DONE_THREAD case, we need 10599 * to wake up the work thread here. When 10600 * we're processing completed requests from 10601 * the work thread context, we'll pop back 10602 * around and end up pulling things off the 10603 * RtR queue. When we aren't processing 10604 * things from the work thread context, 10605 * though, we won't ever check the RtR queue. 10606 * So we need to wake up the thread to clear 10607 * things off the queue. Otherwise this 10608 * transaction will just sit on the RtR queue 10609 * until a new I/O comes in. (Which may or 10610 * may not happen...) 10611 */ 10612#ifndef CTL_DONE_THREAD 10613 ctl_wakeup_thread(); 10614#endif 10615 } else 10616 ctl_done_lock(cur_blocked, /*have_lock*/ 1); 10617 break; 10618 } 10619 default: 10620 /* 10621 * This probably shouldn't happen -- we shouldn't 10622 * get CTL_ACTION_ERROR, or anything else. 10623 */ 10624 break; 10625 } 10626 } 10627 10628 return (CTL_RETVAL_COMPLETE); 10629} 10630 10631/* 10632 * This routine (with one exception) checks LUN flags that can be set by 10633 * commands ahead of us in the OOA queue. These flags have to be checked 10634 * when a command initially comes in, and when we pull a command off the 10635 * blocked queue and are preparing to execute it. The reason we have to 10636 * check these flags for commands on the blocked queue is that the LUN 10637 * state may have been changed by a command ahead of us while we're on the 10638 * blocked queue. 10639 * 10640 * Ordering is somewhat important with these checks, so please pay 10641 * careful attention to the placement of any new checks. 10642 */ 10643static int 10644ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 10645 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10646{ 10647 int retval; 10648 10649 retval = 0; 10650 10651 /* 10652 * If this shelf is a secondary shelf controller, we have to reject 10653 * any media access commands. 10654 */ 10655#if 0 10656 /* No longer needed for HA */ 10657 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 10658 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 10659 ctl_set_lun_standby(ctsio); 10660 retval = 1; 10661 goto bailout; 10662 } 10663#endif 10664 10665 /* 10666 * Check for a reservation conflict. If this command isn't allowed 10667 * even on reserved LUNs, and if this initiator isn't the one who 10668 * reserved us, reject the command with a reservation conflict. 10669 */ 10670 if ((lun->flags & CTL_LUN_RESERVED) 10671 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10672 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 10673 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 10674 || (ctsio->io_hdr.nexus.targ_target.id != 10675 lun->rsv_nexus.targ_target.id)) { 10676 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10677 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10678 retval = 1; 10679 goto bailout; 10680 } 10681 } 10682 10683 if ( (lun->flags & CTL_LUN_PR_RESERVED) 10684 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 10685 uint32_t residx; 10686 10687 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 10688 /* 10689 * if we aren't registered or it's a res holder type 10690 * reservation and this isn't the res holder then set a 10691 * conflict. 10692 * NOTE: Commands which might be allowed on write exclusive 10693 * type reservations are checked in the particular command 10694 * for a conflict. Read and SSU are the only ones. 10695 */ 10696 if (!lun->per_res[residx].registered 10697 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10698 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10699 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10700 retval = 1; 10701 goto bailout; 10702 } 10703 10704 } 10705 10706 if ((lun->flags & CTL_LUN_OFFLINE) 10707 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 10708 ctl_set_lun_not_ready(ctsio); 10709 retval = 1; 10710 goto bailout; 10711 } 10712 10713 /* 10714 * If the LUN is stopped, see if this particular command is allowed 10715 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 10716 */ 10717 if ((lun->flags & CTL_LUN_STOPPED) 10718 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10719 /* "Logical unit not ready, initializing cmd. required" */ 10720 ctl_set_lun_stopped(ctsio); 10721 retval = 1; 10722 goto bailout; 10723 } 10724 10725 if ((lun->flags & CTL_LUN_INOPERABLE) 10726 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10727 /* "Medium format corrupted" */ 10728 ctl_set_medium_format_corrupted(ctsio); 10729 retval = 1; 10730 goto bailout; 10731 } 10732 10733bailout: 10734 return (retval); 10735 10736} 10737 10738static void 10739ctl_failover_io(union ctl_io *io, int have_lock) 10740{ 10741 ctl_set_busy(&io->scsiio); 10742 ctl_done_lock(io, have_lock); 10743} 10744 10745static void 10746ctl_failover(void) 10747{ 10748 struct ctl_lun *lun; 10749 struct ctl_softc *ctl_softc; 10750 union ctl_io *next_io, *pending_io; 10751 union ctl_io *io; 10752 int lun_idx; 10753 int i; 10754 10755 ctl_softc = control_softc; 10756 10757 mtx_lock(&ctl_softc->ctl_lock); 10758 /* 10759 * Remove any cmds from the other SC from the rtr queue. These 10760 * will obviously only be for LUNs for which we're the primary. 10761 * We can't send status or get/send data for these commands. 10762 * Since they haven't been executed yet, we can just remove them. 10763 * We'll either abort them or delete them below, depending on 10764 * which HA mode we're in. 10765 */ 10766 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 10767 io != NULL; io = next_io) { 10768 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 10769 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10770 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 10771 ctl_io_hdr, links); 10772 } 10773 10774 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 10775 lun = ctl_softc->ctl_luns[lun_idx]; 10776 if (lun==NULL) 10777 continue; 10778 10779 /* 10780 * Processor LUNs are primary on both sides. 10781 * XXX will this always be true? 10782 */ 10783 if (lun->be_lun->lun_type == T_PROCESSOR) 10784 continue; 10785 10786 if ((lun->flags & CTL_LUN_PRIMARY_SC) 10787 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10788 printf("FAILOVER: primary lun %d\n", lun_idx); 10789 /* 10790 * Remove all commands from the other SC. First from the 10791 * blocked queue then from the ooa queue. Once we have 10792 * removed them. Call ctl_check_blocked to see if there 10793 * is anything that can run. 10794 */ 10795 for (io = (union ctl_io *)TAILQ_FIRST( 10796 &lun->blocked_queue); io != NULL; io = next_io) { 10797 10798 next_io = (union ctl_io *)TAILQ_NEXT( 10799 &io->io_hdr, blocked_links); 10800 10801 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10802 TAILQ_REMOVE(&lun->blocked_queue, 10803 &io->io_hdr,blocked_links); 10804 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10805 TAILQ_REMOVE(&lun->ooa_queue, 10806 &io->io_hdr, ooa_links); 10807 10808 ctl_free_io(io); 10809 } 10810 } 10811 10812 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10813 io != NULL; io = next_io) { 10814 10815 next_io = (union ctl_io *)TAILQ_NEXT( 10816 &io->io_hdr, ooa_links); 10817 10818 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10819 10820 TAILQ_REMOVE(&lun->ooa_queue, 10821 &io->io_hdr, 10822 ooa_links); 10823 10824 ctl_free_io(io); 10825 } 10826 } 10827 ctl_check_blocked(lun); 10828 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 10829 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10830 10831 printf("FAILOVER: primary lun %d\n", lun_idx); 10832 /* 10833 * Abort all commands from the other SC. We can't 10834 * send status back for them now. These should get 10835 * cleaned up when they are completed or come out 10836 * for a datamove operation. 10837 */ 10838 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10839 io != NULL; io = next_io) { 10840 next_io = (union ctl_io *)TAILQ_NEXT( 10841 &io->io_hdr, ooa_links); 10842 10843 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10844 io->io_hdr.flags |= CTL_FLAG_ABORT; 10845 } 10846 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10847 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10848 10849 printf("FAILOVER: secondary lun %d\n", lun_idx); 10850 10851 lun->flags |= CTL_LUN_PRIMARY_SC; 10852 10853 /* 10854 * We send all I/O that was sent to this controller 10855 * and redirected to the other side back with 10856 * busy status, and have the initiator retry it. 10857 * Figuring out how much data has been transferred, 10858 * etc. and picking up where we left off would be 10859 * very tricky. 10860 * 10861 * XXX KDM need to remove I/O from the blocked 10862 * queue as well! 10863 */ 10864 for (pending_io = (union ctl_io *)TAILQ_FIRST( 10865 &lun->ooa_queue); pending_io != NULL; 10866 pending_io = next_io) { 10867 10868 next_io = (union ctl_io *)TAILQ_NEXT( 10869 &pending_io->io_hdr, ooa_links); 10870 10871 pending_io->io_hdr.flags &= 10872 ~CTL_FLAG_SENT_2OTHER_SC; 10873 10874 if (pending_io->io_hdr.flags & 10875 CTL_FLAG_IO_ACTIVE) { 10876 pending_io->io_hdr.flags |= 10877 CTL_FLAG_FAILOVER; 10878 } else { 10879 ctl_set_busy(&pending_io->scsiio); 10880 ctl_done_lock(pending_io, 10881 /*have_lock*/1); 10882 } 10883 } 10884 10885 /* 10886 * Build Unit Attention 10887 */ 10888 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10889 lun->pending_sense[i].ua_pending |= 10890 CTL_UA_ASYM_ACC_CHANGE; 10891 } 10892 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10893 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10894 printf("FAILOVER: secondary lun %d\n", lun_idx); 10895 /* 10896 * if the first io on the OOA is not on the RtR queue 10897 * add it. 10898 */ 10899 lun->flags |= CTL_LUN_PRIMARY_SC; 10900 10901 pending_io = (union ctl_io *)TAILQ_FIRST( 10902 &lun->ooa_queue); 10903 if (pending_io==NULL) { 10904 printf("Nothing on OOA queue\n"); 10905 continue; 10906 } 10907 10908 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10909 if ((pending_io->io_hdr.flags & 10910 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 10911 pending_io->io_hdr.flags |= 10912 CTL_FLAG_IS_WAS_ON_RTR; 10913 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 10914 &pending_io->io_hdr, links); 10915 } 10916#if 0 10917 else 10918 { 10919 printf("Tag 0x%04x is running\n", 10920 pending_io->scsiio.tag_num); 10921 } 10922#endif 10923 10924 next_io = (union ctl_io *)TAILQ_NEXT( 10925 &pending_io->io_hdr, ooa_links); 10926 for (pending_io=next_io; pending_io != NULL; 10927 pending_io = next_io) { 10928 pending_io->io_hdr.flags &= 10929 ~CTL_FLAG_SENT_2OTHER_SC; 10930 next_io = (union ctl_io *)TAILQ_NEXT( 10931 &pending_io->io_hdr, ooa_links); 10932 if (pending_io->io_hdr.flags & 10933 CTL_FLAG_IS_WAS_ON_RTR) { 10934#if 0 10935 printf("Tag 0x%04x is running\n", 10936 pending_io->scsiio.tag_num); 10937#endif 10938 continue; 10939 } 10940 10941 switch (ctl_check_ooa(lun, pending_io, 10942 (union ctl_io *)TAILQ_PREV( 10943 &pending_io->io_hdr, ctl_ooaq, 10944 ooa_links))) { 10945 10946 case CTL_ACTION_BLOCK: 10947 TAILQ_INSERT_TAIL(&lun->blocked_queue, 10948 &pending_io->io_hdr, 10949 blocked_links); 10950 pending_io->io_hdr.flags |= 10951 CTL_FLAG_BLOCKED; 10952 break; 10953 case CTL_ACTION_PASS: 10954 case CTL_ACTION_SKIP: 10955 pending_io->io_hdr.flags |= 10956 CTL_FLAG_IS_WAS_ON_RTR; 10957 STAILQ_INSERT_TAIL( 10958 &ctl_softc->rtr_queue, 10959 &pending_io->io_hdr, links); 10960 break; 10961 case CTL_ACTION_OVERLAP: 10962 ctl_set_overlapped_cmd( 10963 (struct ctl_scsiio *)pending_io); 10964 ctl_done_lock(pending_io, 10965 /*have_lock*/ 1); 10966 break; 10967 case CTL_ACTION_OVERLAP_TAG: 10968 ctl_set_overlapped_tag( 10969 (struct ctl_scsiio *)pending_io, 10970 pending_io->scsiio.tag_num & 0xff); 10971 ctl_done_lock(pending_io, 10972 /*have_lock*/ 1); 10973 break; 10974 case CTL_ACTION_ERROR: 10975 default: 10976 ctl_set_internal_failure( 10977 (struct ctl_scsiio *)pending_io, 10978 0, // sks_valid 10979 0); //retry count 10980 ctl_done_lock(pending_io, 10981 /*have_lock*/ 1); 10982 break; 10983 } 10984 } 10985 10986 /* 10987 * Build Unit Attention 10988 */ 10989 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10990 lun->pending_sense[i].ua_pending |= 10991 CTL_UA_ASYM_ACC_CHANGE; 10992 } 10993 } else { 10994 panic("Unhandled HA mode failover, LUN flags = %#x, " 10995 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 10996 } 10997 } 10998 ctl_pause_rtr = 0; 10999 mtx_unlock(&ctl_softc->ctl_lock); 11000} 11001 11002static int 11003ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11004{ 11005 struct ctl_lun *lun; 11006 struct ctl_cmd_entry *entry; 11007 uint8_t opcode; 11008 uint32_t initidx, targ_lun; 11009 int retval; 11010 11011 retval = 0; 11012 11013 lun = NULL; 11014 11015 opcode = ctsio->cdb[0]; 11016 11017 mtx_lock(&ctl_softc->ctl_lock); 11018 11019 targ_lun = ctsio->io_hdr.nexus.targ_lun; 11020 if (ctsio->io_hdr.nexus.lun_map_fn != NULL) 11021 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun); 11022 if ((targ_lun < CTL_MAX_LUNS) 11023 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11024 lun = ctl_softc->ctl_luns[targ_lun]; 11025 /* 11026 * If the LUN is invalid, pretend that it doesn't exist. 11027 * It will go away as soon as all pending I/O has been 11028 * completed. 11029 */ 11030 if (lun->flags & CTL_LUN_DISABLED) { 11031 lun = NULL; 11032 } else { 11033 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11034 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11035 lun->be_lun; 11036 if (lun->be_lun->lun_type == T_PROCESSOR) { 11037 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11038 } 11039 } 11040 } else { 11041 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11042 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11043 } 11044 11045 entry = &ctl_cmd_table[opcode]; 11046 11047 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11048 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11049 11050 /* 11051 * Check to see whether we can send this command to LUNs that don't 11052 * exist. This should pretty much only be the case for inquiry 11053 * and request sense. Further checks, below, really require having 11054 * a LUN, so we can't really check the command anymore. Just put 11055 * it on the rtr queue. 11056 */ 11057 if (lun == NULL) { 11058 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 11059 goto queue_rtr; 11060 11061 ctl_set_unsupported_lun(ctsio); 11062 mtx_unlock(&ctl_softc->ctl_lock); 11063 ctl_done((union ctl_io *)ctsio); 11064 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11065 goto bailout; 11066 } else { 11067 /* 11068 * Every I/O goes into the OOA queue for a particular LUN, and 11069 * stays there until completion. 11070 */ 11071 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11072 11073 /* 11074 * Make sure we support this particular command on this LUN. 11075 * e.g., we don't support writes to the control LUN. 11076 */ 11077 switch (lun->be_lun->lun_type) { 11078 case T_PROCESSOR: 11079 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11080 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 11081 == 0)) { 11082 ctl_set_invalid_opcode(ctsio); 11083 mtx_unlock(&ctl_softc->ctl_lock); 11084 ctl_done((union ctl_io *)ctsio); 11085 goto bailout; 11086 } 11087 break; 11088 case T_DIRECT: 11089 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) 11090 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) 11091 == 0)){ 11092 ctl_set_invalid_opcode(ctsio); 11093 mtx_unlock(&ctl_softc->ctl_lock); 11094 ctl_done((union ctl_io *)ctsio); 11095 goto bailout; 11096 } 11097 break; 11098 default: 11099 printf("Unsupported CTL LUN type %d\n", 11100 lun->be_lun->lun_type); 11101 panic("Unsupported CTL LUN type %d\n", 11102 lun->be_lun->lun_type); 11103 break; /* NOTREACHED */ 11104 } 11105 } 11106 11107 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11108 11109 /* 11110 * If we've got a request sense, it'll clear the contingent 11111 * allegiance condition. Otherwise, if we have a CA condition for 11112 * this initiator, clear it, because it sent down a command other 11113 * than request sense. 11114 */ 11115 if ((opcode != REQUEST_SENSE) 11116 && (ctl_is_set(lun->have_ca, initidx))) 11117 ctl_clear_mask(lun->have_ca, initidx); 11118 11119 /* 11120 * If the command has this flag set, it handles its own unit 11121 * attention reporting, we shouldn't do anything. Otherwise we 11122 * check for any pending unit attentions, and send them back to the 11123 * initiator. We only do this when a command initially comes in, 11124 * not when we pull it off the blocked queue. 11125 * 11126 * According to SAM-3, section 5.3.2, the order that things get 11127 * presented back to the host is basically unit attentions caused 11128 * by some sort of reset event, busy status, reservation conflicts 11129 * or task set full, and finally any other status. 11130 * 11131 * One issue here is that some of the unit attentions we report 11132 * don't fall into the "reset" category (e.g. "reported luns data 11133 * has changed"). So reporting it here, before the reservation 11134 * check, may be technically wrong. I guess the only thing to do 11135 * would be to check for and report the reset events here, and then 11136 * check for the other unit attention types after we check for a 11137 * reservation conflict. 11138 * 11139 * XXX KDM need to fix this 11140 */ 11141 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11142 ctl_ua_type ua_type; 11143 11144 ua_type = lun->pending_sense[initidx].ua_pending; 11145 if (ua_type != CTL_UA_NONE) { 11146 scsi_sense_data_type sense_format; 11147 11148 if (lun != NULL) 11149 sense_format = (lun->flags & 11150 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11151 SSD_TYPE_FIXED; 11152 else 11153 sense_format = SSD_TYPE_FIXED; 11154 11155 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11156 sense_format); 11157 if (ua_type != CTL_UA_NONE) { 11158 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11159 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11160 CTL_AUTOSENSE; 11161 ctsio->sense_len = SSD_FULL_SIZE; 11162 lun->pending_sense[initidx].ua_pending &= 11163 ~ua_type; 11164 mtx_unlock(&ctl_softc->ctl_lock); 11165 ctl_done((union ctl_io *)ctsio); 11166 goto bailout; 11167 } 11168 } 11169 } 11170 11171 11172 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11173 mtx_unlock(&ctl_softc->ctl_lock); 11174 ctl_done((union ctl_io *)ctsio); 11175 goto bailout; 11176 } 11177 11178 /* 11179 * XXX CHD this is where we want to send IO to other side if 11180 * this LUN is secondary on this SC. We will need to make a copy 11181 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11182 * the copy we send as FROM_OTHER. 11183 * We also need to stuff the address of the original IO so we can 11184 * find it easily. Something similar will need be done on the other 11185 * side so when we are done we can find the copy. 11186 */ 11187 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11188 union ctl_ha_msg msg_info; 11189 int isc_retval; 11190 11191 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11192 11193 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11194 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11195#if 0 11196 printf("1. ctsio %p\n", ctsio); 11197#endif 11198 msg_info.hdr.serializing_sc = NULL; 11199 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11200 msg_info.scsi.tag_num = ctsio->tag_num; 11201 msg_info.scsi.tag_type = ctsio->tag_type; 11202 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11203 11204 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11205 11206 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11207 (void *)&msg_info, sizeof(msg_info), 0)) > 11208 CTL_HA_STATUS_SUCCESS) { 11209 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11210 isc_retval); 11211 printf("CTL:opcode is %x\n",opcode); 11212 } else { 11213#if 0 11214 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11215#endif 11216 } 11217 11218 /* 11219 * XXX KDM this I/O is off the incoming queue, but hasn't 11220 * been inserted on any other queue. We may need to come 11221 * up with a holding queue while we wait for serialization 11222 * so that we have an idea of what we're waiting for from 11223 * the other side. 11224 */ 11225 goto bailout_unlock; 11226 } 11227 11228 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11229 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11230 ctl_ooaq, ooa_links))) { 11231 case CTL_ACTION_BLOCK: 11232 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11233 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11234 blocked_links); 11235 goto bailout_unlock; 11236 break; /* NOTREACHED */ 11237 case CTL_ACTION_PASS: 11238 case CTL_ACTION_SKIP: 11239 goto queue_rtr; 11240 break; /* NOTREACHED */ 11241 case CTL_ACTION_OVERLAP: 11242 ctl_set_overlapped_cmd(ctsio); 11243 mtx_unlock(&ctl_softc->ctl_lock); 11244 ctl_done((union ctl_io *)ctsio); 11245 goto bailout; 11246 break; /* NOTREACHED */ 11247 case CTL_ACTION_OVERLAP_TAG: 11248 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11249 mtx_unlock(&ctl_softc->ctl_lock); 11250 ctl_done((union ctl_io *)ctsio); 11251 goto bailout; 11252 break; /* NOTREACHED */ 11253 case CTL_ACTION_ERROR: 11254 default: 11255 ctl_set_internal_failure(ctsio, 11256 /*sks_valid*/ 0, 11257 /*retry_count*/ 0); 11258 mtx_unlock(&ctl_softc->ctl_lock); 11259 ctl_done((union ctl_io *)ctsio); 11260 goto bailout; 11261 break; /* NOTREACHED */ 11262 } 11263 11264 goto bailout_unlock; 11265 11266queue_rtr: 11267 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11268 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links); 11269 11270bailout_unlock: 11271 mtx_unlock(&ctl_softc->ctl_lock); 11272 11273bailout: 11274 return (retval); 11275} 11276 11277static int 11278ctl_scsiio(struct ctl_scsiio *ctsio) 11279{ 11280 int retval; 11281 struct ctl_cmd_entry *entry; 11282 11283 retval = CTL_RETVAL_COMPLETE; 11284 11285 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11286 11287 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11288 11289 /* 11290 * If this I/O has been aborted, just send it straight to 11291 * ctl_done() without executing it. 11292 */ 11293 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11294 ctl_done((union ctl_io *)ctsio); 11295 goto bailout; 11296 } 11297 11298 /* 11299 * All the checks should have been handled by ctl_scsiio_precheck(). 11300 * We should be clear now to just execute the I/O. 11301 */ 11302 retval = entry->execute(ctsio); 11303 11304bailout: 11305 return (retval); 11306} 11307 11308/* 11309 * Since we only implement one target right now, a bus reset simply resets 11310 * our single target. 11311 */ 11312static int 11313ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11314{ 11315 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11316} 11317 11318static int 11319ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11320 ctl_ua_type ua_type) 11321{ 11322 struct ctl_lun *lun; 11323 int retval; 11324 11325 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11326 union ctl_ha_msg msg_info; 11327 11328 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11329 msg_info.hdr.nexus = io->io_hdr.nexus; 11330 if (ua_type==CTL_UA_TARG_RESET) 11331 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11332 else 11333 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11334 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11335 msg_info.hdr.original_sc = NULL; 11336 msg_info.hdr.serializing_sc = NULL; 11337 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11338 (void *)&msg_info, sizeof(msg_info), 0)) { 11339 } 11340 } 11341 retval = 0; 11342 11343 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11344 retval += ctl_lun_reset(lun, io, ua_type); 11345 11346 return (retval); 11347} 11348 11349/* 11350 * The LUN should always be set. The I/O is optional, and is used to 11351 * distinguish between I/Os sent by this initiator, and by other 11352 * initiators. We set unit attention for initiators other than this one. 11353 * SAM-3 is vague on this point. It does say that a unit attention should 11354 * be established for other initiators when a LUN is reset (see section 11355 * 5.7.3), but it doesn't specifically say that the unit attention should 11356 * be established for this particular initiator when a LUN is reset. Here 11357 * is the relevant text, from SAM-3 rev 8: 11358 * 11359 * 5.7.2 When a SCSI initiator port aborts its own tasks 11360 * 11361 * When a SCSI initiator port causes its own task(s) to be aborted, no 11362 * notification that the task(s) have been aborted shall be returned to 11363 * the SCSI initiator port other than the completion response for the 11364 * command or task management function action that caused the task(s) to 11365 * be aborted and notification(s) associated with related effects of the 11366 * action (e.g., a reset unit attention condition). 11367 * 11368 * XXX KDM for now, we're setting unit attention for all initiators. 11369 */ 11370static int 11371ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11372{ 11373 union ctl_io *xio; 11374#if 0 11375 uint32_t initindex; 11376#endif 11377 int i; 11378 11379 /* 11380 * Run through the OOA queue and abort each I/O. 11381 */ 11382#if 0 11383 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11384#endif 11385 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11386 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11387 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11388 } 11389 11390 /* 11391 * This version sets unit attention for every 11392 */ 11393#if 0 11394 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11395 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11396 if (initindex == i) 11397 continue; 11398 lun->pending_sense[i].ua_pending |= ua_type; 11399 } 11400#endif 11401 11402 /* 11403 * A reset (any kind, really) clears reservations established with 11404 * RESERVE/RELEASE. It does not clear reservations established 11405 * with PERSISTENT RESERVE OUT, but we don't support that at the 11406 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11407 * reservations made with the RESERVE/RELEASE commands, because 11408 * those commands are obsolete in SPC-3. 11409 */ 11410 lun->flags &= ~CTL_LUN_RESERVED; 11411 11412 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11413 ctl_clear_mask(lun->have_ca, i); 11414 lun->pending_sense[i].ua_pending |= ua_type; 11415 } 11416 11417 return (0); 11418} 11419 11420static int 11421ctl_abort_task(union ctl_io *io) 11422{ 11423 union ctl_io *xio; 11424 struct ctl_lun *lun; 11425 struct ctl_softc *ctl_softc; 11426#if 0 11427 struct sbuf sb; 11428 char printbuf[128]; 11429#endif 11430 int found; 11431 uint32_t targ_lun; 11432 11433 ctl_softc = control_softc; 11434 found = 0; 11435 11436 /* 11437 * Look up the LUN. 11438 */ 11439 targ_lun = io->io_hdr.nexus.targ_lun; 11440 if (io->io_hdr.nexus.lun_map_fn != NULL) 11441 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 11442 if ((targ_lun < CTL_MAX_LUNS) 11443 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11444 lun = ctl_softc->ctl_luns[targ_lun]; 11445 else 11446 goto bailout; 11447 11448#if 0 11449 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11450 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11451#endif 11452 11453 /* 11454 * Run through the OOA queue and attempt to find the given I/O. 11455 * The target port, initiator ID, tag type and tag number have to 11456 * match the values that we got from the initiator. If we have an 11457 * untagged command to abort, simply abort the first untagged command 11458 * we come to. We only allow one untagged command at a time of course. 11459 */ 11460#if 0 11461 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11462#endif 11463 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11464 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11465#if 0 11466 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11467 11468 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11469 lun->lun, xio->scsiio.tag_num, 11470 xio->scsiio.tag_type, 11471 (xio->io_hdr.blocked_links.tqe_prev 11472 == NULL) ? "" : " BLOCKED", 11473 (xio->io_hdr.flags & 11474 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11475 (xio->io_hdr.flags & 11476 CTL_FLAG_ABORT) ? " ABORT" : "", 11477 (xio->io_hdr.flags & 11478 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11479 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11480 sbuf_finish(&sb); 11481 printf("%s\n", sbuf_data(&sb)); 11482#endif 11483 11484 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 11485 && (xio->io_hdr.nexus.initid.id == 11486 io->io_hdr.nexus.initid.id)) { 11487 /* 11488 * If the abort says that the task is untagged, the 11489 * task in the queue must be untagged. Otherwise, 11490 * we just check to see whether the tag numbers 11491 * match. This is because the QLogic firmware 11492 * doesn't pass back the tag type in an abort 11493 * request. 11494 */ 11495#if 0 11496 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11497 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11498 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 11499#endif 11500 /* 11501 * XXX KDM we've got problems with FC, because it 11502 * doesn't send down a tag type with aborts. So we 11503 * can only really go by the tag number... 11504 * This may cause problems with parallel SCSI. 11505 * Need to figure that out!! 11506 */ 11507 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11508 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11509 found = 1; 11510 if ((io->io_hdr.flags & 11511 CTL_FLAG_FROM_OTHER_SC) == 0 && 11512 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11513 union ctl_ha_msg msg_info; 11514 11515 io->io_hdr.flags |= 11516 CTL_FLAG_SENT_2OTHER_SC; 11517 msg_info.hdr.nexus = io->io_hdr.nexus; 11518 msg_info.task.task_action = 11519 CTL_TASK_ABORT_TASK; 11520 msg_info.task.tag_num = 11521 io->taskio.tag_num; 11522 msg_info.task.tag_type = 11523 io->taskio.tag_type; 11524 msg_info.hdr.msg_type = 11525 CTL_MSG_MANAGE_TASKS; 11526 msg_info.hdr.original_sc = NULL; 11527 msg_info.hdr.serializing_sc = NULL; 11528#if 0 11529 printf("Sent Abort to other side\n"); 11530#endif 11531 if (CTL_HA_STATUS_SUCCESS != 11532 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11533 (void *)&msg_info, 11534 sizeof(msg_info), 0)) { 11535 } 11536 } 11537#if 0 11538 printf("ctl_abort_task: found I/O to abort\n"); 11539#endif 11540 break; 11541 } 11542 } 11543 } 11544 11545bailout: 11546 11547 if (found == 0) { 11548 /* 11549 * This isn't really an error. It's entirely possible for 11550 * the abort and command completion to cross on the wire. 11551 * This is more of an informative/diagnostic error. 11552 */ 11553#if 0 11554 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11555 "%d:%d:%d:%d tag %d type %d\n", 11556 io->io_hdr.nexus.initid.id, 11557 io->io_hdr.nexus.targ_port, 11558 io->io_hdr.nexus.targ_target.id, 11559 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11560 io->taskio.tag_type); 11561#endif 11562 return (1); 11563 } else 11564 return (0); 11565} 11566 11567/* 11568 * This routine cannot block! It must be callable from an interrupt 11569 * handler as well as from the work thread. 11570 */ 11571static void 11572ctl_run_task_queue(struct ctl_softc *ctl_softc) 11573{ 11574 union ctl_io *io, *next_io; 11575 11576 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 11577 11578 CTL_DEBUG_PRINT(("ctl_run_task_queue\n")); 11579 11580 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue); 11581 io != NULL; io = next_io) { 11582 int retval; 11583 const char *task_desc; 11584 11585 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11586 11587 retval = 0; 11588 11589 switch (io->io_hdr.io_type) { 11590 case CTL_IO_TASK: { 11591 task_desc = ctl_scsi_task_string(&io->taskio); 11592 if (task_desc != NULL) { 11593#ifdef NEEDTOPORT 11594 csevent_log(CSC_CTL | CSC_SHELF_SW | 11595 CTL_TASK_REPORT, 11596 csevent_LogType_Trace, 11597 csevent_Severity_Information, 11598 csevent_AlertLevel_Green, 11599 csevent_FRU_Firmware, 11600 csevent_FRU_Unknown, 11601 "CTL: received task: %s",task_desc); 11602#endif 11603 } else { 11604#ifdef NEEDTOPORT 11605 csevent_log(CSC_CTL | CSC_SHELF_SW | 11606 CTL_TASK_REPORT, 11607 csevent_LogType_Trace, 11608 csevent_Severity_Information, 11609 csevent_AlertLevel_Green, 11610 csevent_FRU_Firmware, 11611 csevent_FRU_Unknown, 11612 "CTL: received unknown task " 11613 "type: %d (%#x)", 11614 io->taskio.task_action, 11615 io->taskio.task_action); 11616#endif 11617 } 11618 switch (io->taskio.task_action) { 11619 case CTL_TASK_ABORT_TASK: 11620 retval = ctl_abort_task(io); 11621 break; 11622 case CTL_TASK_ABORT_TASK_SET: 11623 break; 11624 case CTL_TASK_CLEAR_ACA: 11625 break; 11626 case CTL_TASK_CLEAR_TASK_SET: 11627 break; 11628 case CTL_TASK_LUN_RESET: { 11629 struct ctl_lun *lun; 11630 uint32_t targ_lun; 11631 int retval; 11632 11633 targ_lun = io->io_hdr.nexus.targ_lun; 11634 if (io->io_hdr.nexus.lun_map_fn != NULL) 11635 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 11636 11637 if ((targ_lun < CTL_MAX_LUNS) 11638 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11639 lun = ctl_softc->ctl_luns[targ_lun]; 11640 else { 11641 retval = 1; 11642 break; 11643 } 11644 11645 if (!(io->io_hdr.flags & 11646 CTL_FLAG_FROM_OTHER_SC)) { 11647 union ctl_ha_msg msg_info; 11648 11649 io->io_hdr.flags |= 11650 CTL_FLAG_SENT_2OTHER_SC; 11651 msg_info.hdr.msg_type = 11652 CTL_MSG_MANAGE_TASKS; 11653 msg_info.hdr.nexus = io->io_hdr.nexus; 11654 msg_info.task.task_action = 11655 CTL_TASK_LUN_RESET; 11656 msg_info.hdr.original_sc = NULL; 11657 msg_info.hdr.serializing_sc = NULL; 11658 if (CTL_HA_STATUS_SUCCESS != 11659 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11660 (void *)&msg_info, 11661 sizeof(msg_info), 0)) { 11662 } 11663 } 11664 11665 retval = ctl_lun_reset(lun, io, 11666 CTL_UA_LUN_RESET); 11667 break; 11668 } 11669 case CTL_TASK_TARGET_RESET: 11670 retval = ctl_target_reset(ctl_softc, io, 11671 CTL_UA_TARG_RESET); 11672 break; 11673 case CTL_TASK_BUS_RESET: 11674 retval = ctl_bus_reset(ctl_softc, io); 11675 break; 11676 case CTL_TASK_PORT_LOGIN: 11677 break; 11678 case CTL_TASK_PORT_LOGOUT: 11679 break; 11680 default: 11681 printf("ctl_run_task_queue: got unknown task " 11682 "management event %d\n", 11683 io->taskio.task_action); 11684 break; 11685 } 11686 if (retval == 0) 11687 io->io_hdr.status = CTL_SUCCESS; 11688 else 11689 io->io_hdr.status = CTL_ERROR; 11690 11691 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr, 11692 ctl_io_hdr, links); 11693 /* 11694 * This will queue this I/O to the done queue, but the 11695 * work thread won't be able to process it until we 11696 * return and the lock is released. 11697 */ 11698 ctl_done_lock(io, /*have_lock*/ 1); 11699 break; 11700 } 11701 default: { 11702 11703 printf("%s: invalid I/O type %d msg %d cdb %x" 11704 " iptl: %ju:%d:%ju:%d tag 0x%04x\n", 11705 __func__, io->io_hdr.io_type, 11706 io->io_hdr.msg_type, io->scsiio.cdb[0], 11707 (uintmax_t)io->io_hdr.nexus.initid.id, 11708 io->io_hdr.nexus.targ_port, 11709 (uintmax_t)io->io_hdr.nexus.targ_target.id, 11710 io->io_hdr.nexus.targ_lun /* XXX */, 11711 (io->io_hdr.io_type == CTL_IO_TASK) ? 11712 io->taskio.tag_num : io->scsiio.tag_num); 11713 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr, 11714 ctl_io_hdr, links); 11715 ctl_free_io(io); 11716 break; 11717 } 11718 } 11719 } 11720 11721 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING; 11722} 11723 11724/* 11725 * For HA operation. Handle commands that come in from the other 11726 * controller. 11727 */ 11728static void 11729ctl_handle_isc(union ctl_io *io) 11730{ 11731 int free_io; 11732 struct ctl_lun *lun; 11733 struct ctl_softc *ctl_softc; 11734 uint32_t targ_lun; 11735 11736 ctl_softc = control_softc; 11737 11738 targ_lun = io->io_hdr.nexus.targ_lun; 11739 if (io->io_hdr.nexus.lun_map_fn != NULL) 11740 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 11741 lun = ctl_softc->ctl_luns[targ_lun]; 11742 11743 switch (io->io_hdr.msg_type) { 11744 case CTL_MSG_SERIALIZE: 11745 free_io = ctl_serialize_other_sc_cmd(&io->scsiio, 11746 /*have_lock*/ 0); 11747 break; 11748 case CTL_MSG_R2R: { 11749 uint8_t opcode; 11750 struct ctl_cmd_entry *entry; 11751 11752 /* 11753 * This is only used in SER_ONLY mode. 11754 */ 11755 free_io = 0; 11756 opcode = io->scsiio.cdb[0]; 11757 entry = &ctl_cmd_table[opcode]; 11758 mtx_lock(&ctl_softc->ctl_lock); 11759 if (ctl_scsiio_lun_check(ctl_softc, lun, 11760 entry, (struct ctl_scsiio *)io) != 0) { 11761 ctl_done_lock(io, /*have_lock*/ 1); 11762 mtx_unlock(&ctl_softc->ctl_lock); 11763 break; 11764 } 11765 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11766 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, 11767 &io->io_hdr, links); 11768 mtx_unlock(&ctl_softc->ctl_lock); 11769 break; 11770 } 11771 case CTL_MSG_FINISH_IO: 11772 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 11773 free_io = 0; 11774 ctl_done_lock(io, /*have_lock*/ 0); 11775 } else { 11776 free_io = 1; 11777 mtx_lock(&ctl_softc->ctl_lock); 11778 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11779 ooa_links); 11780 STAILQ_REMOVE(&ctl_softc->task_queue, 11781 &io->io_hdr, ctl_io_hdr, links); 11782 ctl_check_blocked(lun); 11783 mtx_unlock(&ctl_softc->ctl_lock); 11784 } 11785 break; 11786 case CTL_MSG_PERS_ACTION: 11787 ctl_hndl_per_res_out_on_other_sc( 11788 (union ctl_ha_msg *)&io->presio.pr_msg); 11789 free_io = 1; 11790 break; 11791 case CTL_MSG_BAD_JUJU: 11792 free_io = 0; 11793 ctl_done_lock(io, /*have_lock*/ 0); 11794 break; 11795 case CTL_MSG_DATAMOVE: 11796 /* Only used in XFER mode */ 11797 free_io = 0; 11798 ctl_datamove_remote(io); 11799 break; 11800 case CTL_MSG_DATAMOVE_DONE: 11801 /* Only used in XFER mode */ 11802 free_io = 0; 11803 io->scsiio.be_move_done(io); 11804 break; 11805 default: 11806 free_io = 1; 11807 printf("%s: Invalid message type %d\n", 11808 __func__, io->io_hdr.msg_type); 11809 break; 11810 } 11811 if (free_io) 11812 ctl_free_io(io); 11813 11814} 11815 11816 11817/* 11818 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11819 * there is no match. 11820 */ 11821static ctl_lun_error_pattern 11822ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11823{ 11824 struct ctl_cmd_entry *entry; 11825 ctl_lun_error_pattern filtered_pattern, pattern; 11826 uint8_t opcode; 11827 11828 pattern = desc->error_pattern; 11829 11830 /* 11831 * XXX KDM we need more data passed into this function to match a 11832 * custom pattern, and we actually need to implement custom pattern 11833 * matching. 11834 */ 11835 if (pattern & CTL_LUN_PAT_CMD) 11836 return (CTL_LUN_PAT_CMD); 11837 11838 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 11839 return (CTL_LUN_PAT_ANY); 11840 11841 opcode = ctsio->cdb[0]; 11842 entry = &ctl_cmd_table[opcode]; 11843 11844 filtered_pattern = entry->pattern & pattern; 11845 11846 /* 11847 * If the user requested specific flags in the pattern (e.g. 11848 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 11849 * flags. 11850 * 11851 * If the user did not specify any flags, it doesn't matter whether 11852 * or not the command supports the flags. 11853 */ 11854 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 11855 (pattern & ~CTL_LUN_PAT_MASK)) 11856 return (CTL_LUN_PAT_NONE); 11857 11858 /* 11859 * If the user asked for a range check, see if the requested LBA 11860 * range overlaps with this command's LBA range. 11861 */ 11862 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 11863 uint64_t lba1; 11864 uint32_t len1; 11865 ctl_action action; 11866 int retval; 11867 11868 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 11869 if (retval != 0) 11870 return (CTL_LUN_PAT_NONE); 11871 11872 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 11873 desc->lba_range.len); 11874 /* 11875 * A "pass" means that the LBA ranges don't overlap, so 11876 * this doesn't match the user's range criteria. 11877 */ 11878 if (action == CTL_ACTION_PASS) 11879 return (CTL_LUN_PAT_NONE); 11880 } 11881 11882 return (filtered_pattern); 11883} 11884 11885static void 11886ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 11887{ 11888 struct ctl_error_desc *desc, *desc2; 11889 11890 mtx_assert(&control_softc->ctl_lock, MA_OWNED); 11891 11892 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 11893 ctl_lun_error_pattern pattern; 11894 /* 11895 * Check to see whether this particular command matches 11896 * the pattern in the descriptor. 11897 */ 11898 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 11899 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 11900 continue; 11901 11902 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 11903 case CTL_LUN_INJ_ABORTED: 11904 ctl_set_aborted(&io->scsiio); 11905 break; 11906 case CTL_LUN_INJ_MEDIUM_ERR: 11907 ctl_set_medium_error(&io->scsiio); 11908 break; 11909 case CTL_LUN_INJ_UA: 11910 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 11911 * OCCURRED */ 11912 ctl_set_ua(&io->scsiio, 0x29, 0x00); 11913 break; 11914 case CTL_LUN_INJ_CUSTOM: 11915 /* 11916 * We're assuming the user knows what he is doing. 11917 * Just copy the sense information without doing 11918 * checks. 11919 */ 11920 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 11921 ctl_min(sizeof(desc->custom_sense), 11922 sizeof(io->scsiio.sense_data))); 11923 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 11924 io->scsiio.sense_len = SSD_FULL_SIZE; 11925 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11926 break; 11927 case CTL_LUN_INJ_NONE: 11928 default: 11929 /* 11930 * If this is an error injection type we don't know 11931 * about, clear the continuous flag (if it is set) 11932 * so it will get deleted below. 11933 */ 11934 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 11935 break; 11936 } 11937 /* 11938 * By default, each error injection action is a one-shot 11939 */ 11940 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 11941 continue; 11942 11943 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 11944 11945 free(desc, M_CTL); 11946 } 11947} 11948 11949#ifdef CTL_IO_DELAY 11950static void 11951ctl_datamove_timer_wakeup(void *arg) 11952{ 11953 union ctl_io *io; 11954 11955 io = (union ctl_io *)arg; 11956 11957 ctl_datamove(io); 11958} 11959#endif /* CTL_IO_DELAY */ 11960 11961void 11962ctl_datamove(union ctl_io *io) 11963{ 11964 void (*fe_datamove)(union ctl_io *io); 11965 11966 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 11967 11968 CTL_DEBUG_PRINT(("ctl_datamove\n")); 11969 11970#ifdef CTL_TIME_IO 11971 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 11972 char str[256]; 11973 char path_str[64]; 11974 struct sbuf sb; 11975 11976 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 11977 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 11978 11979 sbuf_cat(&sb, path_str); 11980 switch (io->io_hdr.io_type) { 11981 case CTL_IO_SCSI: 11982 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 11983 sbuf_printf(&sb, "\n"); 11984 sbuf_cat(&sb, path_str); 11985 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 11986 io->scsiio.tag_num, io->scsiio.tag_type); 11987 break; 11988 case CTL_IO_TASK: 11989 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 11990 "Tag Type: %d\n", io->taskio.task_action, 11991 io->taskio.tag_num, io->taskio.tag_type); 11992 break; 11993 default: 11994 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 11995 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 11996 break; 11997 } 11998 sbuf_cat(&sb, path_str); 11999 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12000 (intmax_t)time_uptime - io->io_hdr.start_time); 12001 sbuf_finish(&sb); 12002 printf("%s", sbuf_data(&sb)); 12003 } 12004#endif /* CTL_TIME_IO */ 12005 12006 mtx_lock(&control_softc->ctl_lock); 12007#ifdef CTL_IO_DELAY 12008 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12009 struct ctl_lun *lun; 12010 12011 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12012 12013 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12014 } else { 12015 struct ctl_lun *lun; 12016 12017 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12018 if ((lun != NULL) 12019 && (lun->delay_info.datamove_delay > 0)) { 12020 struct callout *callout; 12021 12022 callout = (struct callout *)&io->io_hdr.timer_bytes; 12023 callout_init(callout, /*mpsafe*/ 1); 12024 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12025 callout_reset(callout, 12026 lun->delay_info.datamove_delay * hz, 12027 ctl_datamove_timer_wakeup, io); 12028 if (lun->delay_info.datamove_type == 12029 CTL_DELAY_TYPE_ONESHOT) 12030 lun->delay_info.datamove_delay = 0; 12031 mtx_unlock(&control_softc->ctl_lock); 12032 return; 12033 } 12034 } 12035#endif 12036 /* 12037 * If we have any pending task management commands, process them 12038 * first. This is necessary to eliminate a race condition with the 12039 * FETD: 12040 * 12041 * - FETD submits a task management command, like an abort. 12042 * - Back end calls fe_datamove() to move the data for the aborted 12043 * command. The FETD can't really accept it, but if it did, it 12044 * would end up transmitting data for a command that the initiator 12045 * told us to abort. 12046 * 12047 * We close the race by processing all pending task management 12048 * commands here (we can't block!), and then check this I/O to see 12049 * if it has been aborted. If so, return it to the back end with 12050 * bad status, so the back end can say return an error to the back end 12051 * and then when the back end returns an error, we can return the 12052 * aborted command to the FETD, so it can clean up its resources. 12053 */ 12054 if (control_softc->flags & CTL_FLAG_TASK_PENDING) 12055 ctl_run_task_queue(control_softc); 12056 12057 /* 12058 * This command has been aborted. Set the port status, so we fail 12059 * the data move. 12060 */ 12061 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12062 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12063 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12064 io->io_hdr.nexus.targ_port, 12065 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12066 io->io_hdr.nexus.targ_lun); 12067 io->io_hdr.status = CTL_CMD_ABORTED; 12068 io->io_hdr.port_status = 31337; 12069 mtx_unlock(&control_softc->ctl_lock); 12070 /* 12071 * Note that the backend, in this case, will get the 12072 * callback in its context. In other cases it may get 12073 * called in the frontend's interrupt thread context. 12074 */ 12075 io->scsiio.be_move_done(io); 12076 return; 12077 } 12078 12079 /* 12080 * If we're in XFER mode and this I/O is from the other shelf 12081 * controller, we need to send the DMA to the other side to 12082 * actually transfer the data to/from the host. In serialize only 12083 * mode the transfer happens below CTL and ctl_datamove() is only 12084 * called on the machine that originally received the I/O. 12085 */ 12086 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12087 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12088 union ctl_ha_msg msg; 12089 uint32_t sg_entries_sent; 12090 int do_sg_copy; 12091 int i; 12092 12093 memset(&msg, 0, sizeof(msg)); 12094 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12095 msg.hdr.original_sc = io->io_hdr.original_sc; 12096 msg.hdr.serializing_sc = io; 12097 msg.hdr.nexus = io->io_hdr.nexus; 12098 msg.dt.flags = io->io_hdr.flags; 12099 /* 12100 * We convert everything into a S/G list here. We can't 12101 * pass by reference, only by value between controllers. 12102 * So we can't pass a pointer to the S/G list, only as many 12103 * S/G entries as we can fit in here. If it's possible for 12104 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12105 * then we need to break this up into multiple transfers. 12106 */ 12107 if (io->scsiio.kern_sg_entries == 0) { 12108 msg.dt.kern_sg_entries = 1; 12109 /* 12110 * If this is in cached memory, flush the cache 12111 * before we send the DMA request to the other 12112 * controller. We want to do this in either the 12113 * read or the write case. The read case is 12114 * straightforward. In the write case, we want to 12115 * make sure nothing is in the local cache that 12116 * could overwrite the DMAed data. 12117 */ 12118 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12119 /* 12120 * XXX KDM use bus_dmamap_sync() here. 12121 */ 12122 } 12123 12124 /* 12125 * Convert to a physical address if this is a 12126 * virtual address. 12127 */ 12128 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12129 msg.dt.sg_list[0].addr = 12130 io->scsiio.kern_data_ptr; 12131 } else { 12132 /* 12133 * XXX KDM use busdma here! 12134 */ 12135#if 0 12136 msg.dt.sg_list[0].addr = (void *) 12137 vtophys(io->scsiio.kern_data_ptr); 12138#endif 12139 } 12140 12141 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12142 do_sg_copy = 0; 12143 } else { 12144 struct ctl_sg_entry *sgl; 12145 12146 do_sg_copy = 1; 12147 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12148 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12149 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12150 /* 12151 * XXX KDM use bus_dmamap_sync() here. 12152 */ 12153 } 12154 } 12155 12156 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12157 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12158 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12159 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12160 msg.dt.sg_sequence = 0; 12161 12162 /* 12163 * Loop until we've sent all of the S/G entries. On the 12164 * other end, we'll recompose these S/G entries into one 12165 * contiguous list before passing it to the 12166 */ 12167 for (sg_entries_sent = 0; sg_entries_sent < 12168 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12169 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12170 sizeof(msg.dt.sg_list[0])), 12171 msg.dt.kern_sg_entries - sg_entries_sent); 12172 12173 if (do_sg_copy != 0) { 12174 struct ctl_sg_entry *sgl; 12175 int j; 12176 12177 sgl = (struct ctl_sg_entry *) 12178 io->scsiio.kern_data_ptr; 12179 /* 12180 * If this is in cached memory, flush the cache 12181 * before we send the DMA request to the other 12182 * controller. We want to do this in either 12183 * the * read or the write case. The read 12184 * case is straightforward. In the write 12185 * case, we want to make sure nothing is 12186 * in the local cache that could overwrite 12187 * the DMAed data. 12188 */ 12189 12190 for (i = sg_entries_sent, j = 0; 12191 i < msg.dt.cur_sg_entries; i++, j++) { 12192 if ((io->io_hdr.flags & 12193 CTL_FLAG_NO_DATASYNC) == 0) { 12194 /* 12195 * XXX KDM use bus_dmamap_sync() 12196 */ 12197 } 12198 if ((io->io_hdr.flags & 12199 CTL_FLAG_BUS_ADDR) == 0) { 12200 /* 12201 * XXX KDM use busdma. 12202 */ 12203#if 0 12204 msg.dt.sg_list[j].addr =(void *) 12205 vtophys(sgl[i].addr); 12206#endif 12207 } else { 12208 msg.dt.sg_list[j].addr = 12209 sgl[i].addr; 12210 } 12211 msg.dt.sg_list[j].len = sgl[i].len; 12212 } 12213 } 12214 12215 sg_entries_sent += msg.dt.cur_sg_entries; 12216 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12217 msg.dt.sg_last = 1; 12218 else 12219 msg.dt.sg_last = 0; 12220 12221 /* 12222 * XXX KDM drop and reacquire the lock here? 12223 */ 12224 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12225 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12226 /* 12227 * XXX do something here. 12228 */ 12229 } 12230 12231 msg.dt.sent_sg_entries = sg_entries_sent; 12232 } 12233 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12234 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12235 ctl_failover_io(io, /*have_lock*/ 1); 12236 12237 } else { 12238 12239 /* 12240 * Lookup the fe_datamove() function for this particular 12241 * front end. 12242 */ 12243 fe_datamove = 12244 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12245 mtx_unlock(&control_softc->ctl_lock); 12246 12247 fe_datamove(io); 12248 } 12249} 12250 12251static void 12252ctl_send_datamove_done(union ctl_io *io, int have_lock) 12253{ 12254 union ctl_ha_msg msg; 12255 int isc_status; 12256 12257 memset(&msg, 0, sizeof(msg)); 12258 12259 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12260 msg.hdr.original_sc = io; 12261 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12262 msg.hdr.nexus = io->io_hdr.nexus; 12263 msg.hdr.status = io->io_hdr.status; 12264 msg.scsi.tag_num = io->scsiio.tag_num; 12265 msg.scsi.tag_type = io->scsiio.tag_type; 12266 msg.scsi.scsi_status = io->scsiio.scsi_status; 12267 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12268 sizeof(io->scsiio.sense_data)); 12269 msg.scsi.sense_len = io->scsiio.sense_len; 12270 msg.scsi.sense_residual = io->scsiio.sense_residual; 12271 msg.scsi.fetd_status = io->io_hdr.port_status; 12272 msg.scsi.residual = io->scsiio.residual; 12273 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12274 12275 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12276 ctl_failover_io(io, /*have_lock*/ have_lock); 12277 return; 12278 } 12279 12280 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12281 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12282 /* XXX do something if this fails */ 12283 } 12284 12285} 12286 12287/* 12288 * The DMA to the remote side is done, now we need to tell the other side 12289 * we're done so it can continue with its data movement. 12290 */ 12291static void 12292ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12293{ 12294 union ctl_io *io; 12295 12296 io = rq->context; 12297 12298 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12299 printf("%s: ISC DMA write failed with error %d", __func__, 12300 rq->ret); 12301 ctl_set_internal_failure(&io->scsiio, 12302 /*sks_valid*/ 1, 12303 /*retry_count*/ rq->ret); 12304 } 12305 12306 ctl_dt_req_free(rq); 12307 12308 /* 12309 * In this case, we had to malloc the memory locally. Free it. 12310 */ 12311 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12312 int i; 12313 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12314 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12315 } 12316 /* 12317 * The data is in local and remote memory, so now we need to send 12318 * status (good or back) back to the other side. 12319 */ 12320 ctl_send_datamove_done(io, /*have_lock*/ 0); 12321} 12322 12323/* 12324 * We've moved the data from the host/controller into local memory. Now we 12325 * need to push it over to the remote controller's memory. 12326 */ 12327static int 12328ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12329{ 12330 int retval; 12331 12332 retval = 0; 12333 12334 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12335 ctl_datamove_remote_write_cb); 12336 12337 return (retval); 12338} 12339 12340static void 12341ctl_datamove_remote_write(union ctl_io *io) 12342{ 12343 int retval; 12344 void (*fe_datamove)(union ctl_io *io); 12345 12346 /* 12347 * - Get the data from the host/HBA into local memory. 12348 * - DMA memory from the local controller to the remote controller. 12349 * - Send status back to the remote controller. 12350 */ 12351 12352 retval = ctl_datamove_remote_sgl_setup(io); 12353 if (retval != 0) 12354 return; 12355 12356 /* Switch the pointer over so the FETD knows what to do */ 12357 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12358 12359 /* 12360 * Use a custom move done callback, since we need to send completion 12361 * back to the other controller, not to the backend on this side. 12362 */ 12363 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12364 12365 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12366 12367 fe_datamove(io); 12368 12369 return; 12370 12371} 12372 12373static int 12374ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12375{ 12376#if 0 12377 char str[256]; 12378 char path_str[64]; 12379 struct sbuf sb; 12380#endif 12381 12382 /* 12383 * In this case, we had to malloc the memory locally. Free it. 12384 */ 12385 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12386 int i; 12387 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12388 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12389 } 12390 12391#if 0 12392 scsi_path_string(io, path_str, sizeof(path_str)); 12393 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12394 sbuf_cat(&sb, path_str); 12395 scsi_command_string(&io->scsiio, NULL, &sb); 12396 sbuf_printf(&sb, "\n"); 12397 sbuf_cat(&sb, path_str); 12398 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12399 io->scsiio.tag_num, io->scsiio.tag_type); 12400 sbuf_cat(&sb, path_str); 12401 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12402 io->io_hdr.flags, io->io_hdr.status); 12403 sbuf_finish(&sb); 12404 printk("%s", sbuf_data(&sb)); 12405#endif 12406 12407 12408 /* 12409 * The read is done, now we need to send status (good or bad) back 12410 * to the other side. 12411 */ 12412 ctl_send_datamove_done(io, /*have_lock*/ 0); 12413 12414 return (0); 12415} 12416 12417static void 12418ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12419{ 12420 union ctl_io *io; 12421 void (*fe_datamove)(union ctl_io *io); 12422 12423 io = rq->context; 12424 12425 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12426 printf("%s: ISC DMA read failed with error %d", __func__, 12427 rq->ret); 12428 ctl_set_internal_failure(&io->scsiio, 12429 /*sks_valid*/ 1, 12430 /*retry_count*/ rq->ret); 12431 } 12432 12433 ctl_dt_req_free(rq); 12434 12435 /* Switch the pointer over so the FETD knows what to do */ 12436 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12437 12438 /* 12439 * Use a custom move done callback, since we need to send completion 12440 * back to the other controller, not to the backend on this side. 12441 */ 12442 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12443 12444 /* XXX KDM add checks like the ones in ctl_datamove? */ 12445 12446 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12447 12448 fe_datamove(io); 12449} 12450 12451static int 12452ctl_datamove_remote_sgl_setup(union ctl_io *io) 12453{ 12454 struct ctl_sg_entry *local_sglist, *remote_sglist; 12455 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12456 struct ctl_softc *softc; 12457 int retval; 12458 int i; 12459 12460 retval = 0; 12461 softc = control_softc; 12462 12463 local_sglist = io->io_hdr.local_sglist; 12464 local_dma_sglist = io->io_hdr.local_dma_sglist; 12465 remote_sglist = io->io_hdr.remote_sglist; 12466 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12467 12468 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12469 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12470 local_sglist[i].len = remote_sglist[i].len; 12471 12472 /* 12473 * XXX Detect the situation where the RS-level I/O 12474 * redirector on the other side has already read the 12475 * data off of the AOR RS on this side, and 12476 * transferred it to remote (mirror) memory on the 12477 * other side. Since we already have the data in 12478 * memory here, we just need to use it. 12479 * 12480 * XXX KDM this can probably be removed once we 12481 * get the cache device code in and take the 12482 * current AOR implementation out. 12483 */ 12484#ifdef NEEDTOPORT 12485 if ((remote_sglist[i].addr >= 12486 (void *)vtophys(softc->mirr->addr)) 12487 && (remote_sglist[i].addr < 12488 ((void *)vtophys(softc->mirr->addr) + 12489 CacheMirrorOffset))) { 12490 local_sglist[i].addr = remote_sglist[i].addr - 12491 CacheMirrorOffset; 12492 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12493 CTL_FLAG_DATA_IN) 12494 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12495 } else { 12496 local_sglist[i].addr = remote_sglist[i].addr + 12497 CacheMirrorOffset; 12498 } 12499#endif 12500#if 0 12501 printf("%s: local %p, remote %p, len %d\n", 12502 __func__, local_sglist[i].addr, 12503 remote_sglist[i].addr, local_sglist[i].len); 12504#endif 12505 } 12506 } else { 12507 uint32_t len_to_go; 12508 12509 /* 12510 * In this case, we don't have automatically allocated 12511 * memory for this I/O on this controller. This typically 12512 * happens with internal CTL I/O -- e.g. inquiry, mode 12513 * sense, etc. Anything coming from RAIDCore will have 12514 * a mirror area available. 12515 */ 12516 len_to_go = io->scsiio.kern_data_len; 12517 12518 /* 12519 * Clear the no datasync flag, we have to use malloced 12520 * buffers. 12521 */ 12522 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12523 12524 /* 12525 * The difficult thing here is that the size of the various 12526 * S/G segments may be different than the size from the 12527 * remote controller. That'll make it harder when DMAing 12528 * the data back to the other side. 12529 */ 12530 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12531 sizeof(io->io_hdr.remote_sglist[0])) && 12532 (len_to_go > 0); i++) { 12533 local_sglist[i].len = ctl_min(len_to_go, 131072); 12534 CTL_SIZE_8B(local_dma_sglist[i].len, 12535 local_sglist[i].len); 12536 local_sglist[i].addr = 12537 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12538 12539 local_dma_sglist[i].addr = local_sglist[i].addr; 12540 12541 if (local_sglist[i].addr == NULL) { 12542 int j; 12543 12544 printf("malloc failed for %zd bytes!", 12545 local_dma_sglist[i].len); 12546 for (j = 0; j < i; j++) { 12547 free(local_sglist[j].addr, M_CTL); 12548 } 12549 ctl_set_internal_failure(&io->scsiio, 12550 /*sks_valid*/ 1, 12551 /*retry_count*/ 4857); 12552 retval = 1; 12553 goto bailout_error; 12554 12555 } 12556 /* XXX KDM do we need a sync here? */ 12557 12558 len_to_go -= local_sglist[i].len; 12559 } 12560 /* 12561 * Reset the number of S/G entries accordingly. The 12562 * original number of S/G entries is available in 12563 * rem_sg_entries. 12564 */ 12565 io->scsiio.kern_sg_entries = i; 12566 12567#if 0 12568 printf("%s: kern_sg_entries = %d\n", __func__, 12569 io->scsiio.kern_sg_entries); 12570 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12571 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 12572 local_sglist[i].addr, local_sglist[i].len, 12573 local_dma_sglist[i].len); 12574#endif 12575 } 12576 12577 12578 return (retval); 12579 12580bailout_error: 12581 12582 ctl_send_datamove_done(io, /*have_lock*/ 0); 12583 12584 return (retval); 12585} 12586 12587static int 12588ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12589 ctl_ha_dt_cb callback) 12590{ 12591 struct ctl_ha_dt_req *rq; 12592 struct ctl_sg_entry *remote_sglist, *local_sglist; 12593 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 12594 uint32_t local_used, remote_used, total_used; 12595 int retval; 12596 int i, j; 12597 12598 retval = 0; 12599 12600 rq = ctl_dt_req_alloc(); 12601 12602 /* 12603 * If we failed to allocate the request, and if the DMA didn't fail 12604 * anyway, set busy status. This is just a resource allocation 12605 * failure. 12606 */ 12607 if ((rq == NULL) 12608 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 12609 ctl_set_busy(&io->scsiio); 12610 12611 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 12612 12613 if (rq != NULL) 12614 ctl_dt_req_free(rq); 12615 12616 /* 12617 * The data move failed. We need to return status back 12618 * to the other controller. No point in trying to DMA 12619 * data to the remote controller. 12620 */ 12621 12622 ctl_send_datamove_done(io, /*have_lock*/ 0); 12623 12624 retval = 1; 12625 12626 goto bailout; 12627 } 12628 12629 local_sglist = io->io_hdr.local_sglist; 12630 local_dma_sglist = io->io_hdr.local_dma_sglist; 12631 remote_sglist = io->io_hdr.remote_sglist; 12632 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12633 local_used = 0; 12634 remote_used = 0; 12635 total_used = 0; 12636 12637 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 12638 rq->ret = CTL_HA_STATUS_SUCCESS; 12639 rq->context = io; 12640 callback(rq); 12641 goto bailout; 12642 } 12643 12644 /* 12645 * Pull/push the data over the wire from/to the other controller. 12646 * This takes into account the possibility that the local and 12647 * remote sglists may not be identical in terms of the size of 12648 * the elements and the number of elements. 12649 * 12650 * One fundamental assumption here is that the length allocated for 12651 * both the local and remote sglists is identical. Otherwise, we've 12652 * essentially got a coding error of some sort. 12653 */ 12654 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12655 int isc_ret; 12656 uint32_t cur_len, dma_length; 12657 uint8_t *tmp_ptr; 12658 12659 rq->id = CTL_HA_DATA_CTL; 12660 rq->command = command; 12661 rq->context = io; 12662 12663 /* 12664 * Both pointers should be aligned. But it is possible 12665 * that the allocation length is not. They should both 12666 * also have enough slack left over at the end, though, 12667 * to round up to the next 8 byte boundary. 12668 */ 12669 cur_len = ctl_min(local_sglist[i].len - local_used, 12670 remote_sglist[j].len - remote_used); 12671 12672 /* 12673 * In this case, we have a size issue and need to decrease 12674 * the size, except in the case where we actually have less 12675 * than 8 bytes left. In that case, we need to increase 12676 * the DMA length to get the last bit. 12677 */ 12678 if ((cur_len & 0x7) != 0) { 12679 if (cur_len > 0x7) { 12680 cur_len = cur_len - (cur_len & 0x7); 12681 dma_length = cur_len; 12682 } else { 12683 CTL_SIZE_8B(dma_length, cur_len); 12684 } 12685 12686 } else 12687 dma_length = cur_len; 12688 12689 /* 12690 * If we had to allocate memory for this I/O, instead of using 12691 * the non-cached mirror memory, we'll need to flush the cache 12692 * before trying to DMA to the other controller. 12693 * 12694 * We could end up doing this multiple times for the same 12695 * segment if we have a larger local segment than remote 12696 * segment. That shouldn't be an issue. 12697 */ 12698 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12699 /* 12700 * XXX KDM use bus_dmamap_sync() here. 12701 */ 12702 } 12703 12704 rq->size = dma_length; 12705 12706 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12707 tmp_ptr += local_used; 12708 12709 /* Use physical addresses when talking to ISC hardware */ 12710 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12711 /* XXX KDM use busdma */ 12712#if 0 12713 rq->local = vtophys(tmp_ptr); 12714#endif 12715 } else 12716 rq->local = tmp_ptr; 12717 12718 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12719 tmp_ptr += remote_used; 12720 rq->remote = tmp_ptr; 12721 12722 rq->callback = NULL; 12723 12724 local_used += cur_len; 12725 if (local_used >= local_sglist[i].len) { 12726 i++; 12727 local_used = 0; 12728 } 12729 12730 remote_used += cur_len; 12731 if (remote_used >= remote_sglist[j].len) { 12732 j++; 12733 remote_used = 0; 12734 } 12735 total_used += cur_len; 12736 12737 if (total_used >= io->scsiio.kern_data_len) 12738 rq->callback = callback; 12739 12740 if ((rq->size & 0x7) != 0) { 12741 printf("%s: warning: size %d is not on 8b boundary\n", 12742 __func__, rq->size); 12743 } 12744 if (((uintptr_t)rq->local & 0x7) != 0) { 12745 printf("%s: warning: local %p not on 8b boundary\n", 12746 __func__, rq->local); 12747 } 12748 if (((uintptr_t)rq->remote & 0x7) != 0) { 12749 printf("%s: warning: remote %p not on 8b boundary\n", 12750 __func__, rq->local); 12751 } 12752#if 0 12753 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12754 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12755 rq->local, rq->remote, rq->size); 12756#endif 12757 12758 isc_ret = ctl_dt_single(rq); 12759 if (isc_ret == CTL_HA_STATUS_WAIT) 12760 continue; 12761 12762 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 12763 rq->ret = CTL_HA_STATUS_SUCCESS; 12764 } else { 12765 rq->ret = isc_ret; 12766 } 12767 callback(rq); 12768 goto bailout; 12769 } 12770 12771bailout: 12772 return (retval); 12773 12774} 12775 12776static void 12777ctl_datamove_remote_read(union ctl_io *io) 12778{ 12779 int retval; 12780 int i; 12781 12782 /* 12783 * This will send an error to the other controller in the case of a 12784 * failure. 12785 */ 12786 retval = ctl_datamove_remote_sgl_setup(io); 12787 if (retval != 0) 12788 return; 12789 12790 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12791 ctl_datamove_remote_read_cb); 12792 if ((retval != 0) 12793 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 12794 /* 12795 * Make sure we free memory if there was an error.. The 12796 * ctl_datamove_remote_xfer() function will send the 12797 * datamove done message, or call the callback with an 12798 * error if there is a problem. 12799 */ 12800 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12801 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12802 } 12803 12804 return; 12805} 12806 12807/* 12808 * Process a datamove request from the other controller. This is used for 12809 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12810 * first. Once that is complete, the data gets DMAed into the remote 12811 * controller's memory. For reads, we DMA from the remote controller's 12812 * memory into our memory first, and then move it out to the FETD. 12813 */ 12814static void 12815ctl_datamove_remote(union ctl_io *io) 12816{ 12817 struct ctl_softc *softc; 12818 12819 softc = control_softc; 12820 12821 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 12822 12823 /* 12824 * Note that we look for an aborted I/O here, but don't do some of 12825 * the other checks that ctl_datamove() normally does. We don't 12826 * need to run the task queue, because this I/O is on the ISC 12827 * queue, which is executed by the work thread after the task queue. 12828 * We don't need to run the datamove delay code, since that should 12829 * have been done if need be on the other controller. 12830 */ 12831 mtx_lock(&softc->ctl_lock); 12832 12833 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12834 12835 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 12836 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 12837 io->io_hdr.nexus.targ_port, 12838 io->io_hdr.nexus.targ_target.id, 12839 io->io_hdr.nexus.targ_lun); 12840 io->io_hdr.status = CTL_CMD_ABORTED; 12841 io->io_hdr.port_status = 31338; 12842 12843 mtx_unlock(&softc->ctl_lock); 12844 12845 ctl_send_datamove_done(io, /*have_lock*/ 0); 12846 12847 return; 12848 } 12849 12850 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 12851 mtx_unlock(&softc->ctl_lock); 12852 ctl_datamove_remote_write(io); 12853 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 12854 mtx_unlock(&softc->ctl_lock); 12855 ctl_datamove_remote_read(io); 12856 } else { 12857 union ctl_ha_msg msg; 12858 struct scsi_sense_data *sense; 12859 uint8_t sks[3]; 12860 int retry_count; 12861 12862 memset(&msg, 0, sizeof(msg)); 12863 12864 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 12865 msg.hdr.status = CTL_SCSI_ERROR; 12866 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 12867 12868 retry_count = 4243; 12869 12870 sense = &msg.scsi.sense_data; 12871 sks[0] = SSD_SCS_VALID; 12872 sks[1] = (retry_count >> 8) & 0xff; 12873 sks[2] = retry_count & 0xff; 12874 12875 /* "Internal target failure" */ 12876 scsi_set_sense_data(sense, 12877 /*sense_format*/ SSD_TYPE_NONE, 12878 /*current_error*/ 1, 12879 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 12880 /*asc*/ 0x44, 12881 /*ascq*/ 0x00, 12882 /*type*/ SSD_ELEM_SKS, 12883 /*size*/ sizeof(sks), 12884 /*data*/ sks, 12885 SSD_ELEM_NONE); 12886 12887 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12888 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12889 ctl_failover_io(io, /*have_lock*/ 1); 12890 mtx_unlock(&softc->ctl_lock); 12891 return; 12892 } 12893 12894 mtx_unlock(&softc->ctl_lock); 12895 12896 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 12897 CTL_HA_STATUS_SUCCESS) { 12898 /* XXX KDM what to do if this fails? */ 12899 } 12900 return; 12901 } 12902 12903} 12904 12905static int 12906ctl_process_done(union ctl_io *io, int have_lock) 12907{ 12908 struct ctl_lun *lun; 12909 struct ctl_softc *ctl_softc; 12910 void (*fe_done)(union ctl_io *io); 12911 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 12912 12913 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12914 12915 fe_done = 12916 control_softc->ctl_ports[targ_port]->fe_done; 12917 12918#ifdef CTL_TIME_IO 12919 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12920 char str[256]; 12921 char path_str[64]; 12922 struct sbuf sb; 12923 12924 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12925 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12926 12927 sbuf_cat(&sb, path_str); 12928 switch (io->io_hdr.io_type) { 12929 case CTL_IO_SCSI: 12930 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12931 sbuf_printf(&sb, "\n"); 12932 sbuf_cat(&sb, path_str); 12933 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12934 io->scsiio.tag_num, io->scsiio.tag_type); 12935 break; 12936 case CTL_IO_TASK: 12937 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12938 "Tag Type: %d\n", io->taskio.task_action, 12939 io->taskio.tag_num, io->taskio.tag_type); 12940 break; 12941 default: 12942 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12943 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12944 break; 12945 } 12946 sbuf_cat(&sb, path_str); 12947 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12948 (intmax_t)time_uptime - io->io_hdr.start_time); 12949 sbuf_finish(&sb); 12950 printf("%s", sbuf_data(&sb)); 12951 } 12952#endif /* CTL_TIME_IO */ 12953 12954 switch (io->io_hdr.io_type) { 12955 case CTL_IO_SCSI: 12956 break; 12957 case CTL_IO_TASK: 12958 if (bootverbose || verbose > 0) 12959 ctl_io_error_print(io, NULL); 12960 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 12961 ctl_free_io(io); 12962 else 12963 fe_done(io); 12964 return (CTL_RETVAL_COMPLETE); 12965 break; 12966 default: 12967 printf("ctl_process_done: invalid io type %d\n", 12968 io->io_hdr.io_type); 12969 panic("ctl_process_done: invalid io type %d\n", 12970 io->io_hdr.io_type); 12971 break; /* NOTREACHED */ 12972 } 12973 12974 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12975 if (lun == NULL) { 12976 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12977 io->io_hdr.nexus.targ_lun)); 12978 fe_done(io); 12979 goto bailout; 12980 } 12981 ctl_softc = lun->ctl_softc; 12982 12983 /* 12984 * Remove this from the OOA queue. 12985 */ 12986 if (have_lock == 0) 12987 mtx_lock(&ctl_softc->ctl_lock); 12988 12989 /* 12990 * Check to see if we have any errors to inject here. We only 12991 * inject errors for commands that don't already have errors set. 12992 */ 12993 if ((STAILQ_FIRST(&lun->error_list) != NULL) 12994 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 12995 ctl_inject_error(lun, io); 12996 12997 /* 12998 * XXX KDM how do we treat commands that aren't completed 12999 * successfully? 13000 * 13001 * XXX KDM should we also track I/O latency? 13002 */ 13003 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 13004 uint32_t blocksize; 13005#ifdef CTL_TIME_IO 13006 struct bintime cur_bt; 13007#endif 13008 13009 if ((lun->be_lun != NULL) 13010 && (lun->be_lun->blocksize != 0)) 13011 blocksize = lun->be_lun->blocksize; 13012 else 13013 blocksize = 512; 13014 13015 switch (io->io_hdr.io_type) { 13016 case CTL_IO_SCSI: { 13017 int isread; 13018 struct ctl_lba_len_flags *lbalen; 13019 13020 isread = 0; 13021 switch (io->scsiio.cdb[0]) { 13022 case READ_6: 13023 case READ_10: 13024 case READ_12: 13025 case READ_16: 13026 isread = 1; 13027 /* FALLTHROUGH */ 13028 case WRITE_6: 13029 case WRITE_10: 13030 case WRITE_12: 13031 case WRITE_16: 13032 case WRITE_VERIFY_10: 13033 case WRITE_VERIFY_12: 13034 case WRITE_VERIFY_16: 13035 lbalen = (struct ctl_lba_len_flags *) 13036 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 13037 13038 if (isread) { 13039 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] += 13040 lbalen->len * blocksize; 13041 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++; 13042 13043#ifdef CTL_TIME_IO 13044 bintime_add( 13045 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ], 13046 &io->io_hdr.dma_bt); 13047 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] += 13048 io->io_hdr.num_dmas; 13049 getbintime(&cur_bt); 13050 bintime_sub(&cur_bt, 13051 &io->io_hdr.start_bt); 13052 13053 bintime_add( 13054 &lun->stats.ports[targ_port].time[CTL_STATS_READ], 13055 &cur_bt); 13056 13057#if 0 13058 cs_prof_gettime(&cur_ticks); 13059 lun->stats.time[CTL_STATS_READ] += 13060 cur_ticks - 13061 io->io_hdr.start_ticks; 13062#endif 13063#if 0 13064 lun->stats.time[CTL_STATS_READ] += 13065 jiffies - io->io_hdr.start_time; 13066#endif 13067#endif /* CTL_TIME_IO */ 13068 } else { 13069 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] += 13070 lbalen->len * blocksize; 13071 lun->stats.ports[targ_port].operations[ 13072 CTL_STATS_WRITE]++; 13073 13074#ifdef CTL_TIME_IO 13075 bintime_add( 13076 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE], 13077 &io->io_hdr.dma_bt); 13078 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] += 13079 io->io_hdr.num_dmas; 13080 getbintime(&cur_bt); 13081 bintime_sub(&cur_bt, 13082 &io->io_hdr.start_bt); 13083 13084 bintime_add( 13085 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE], 13086 &cur_bt); 13087#if 0 13088 cs_prof_gettime(&cur_ticks); 13089 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] += 13090 cur_ticks - 13091 io->io_hdr.start_ticks; 13092 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] += 13093 jiffies - io->io_hdr.start_time; 13094#endif 13095#endif /* CTL_TIME_IO */ 13096 } 13097 break; 13098 default: 13099 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++; 13100 13101#ifdef CTL_TIME_IO 13102 bintime_add( 13103 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO], 13104 &io->io_hdr.dma_bt); 13105 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] += 13106 io->io_hdr.num_dmas; 13107 getbintime(&cur_bt); 13108 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13109 13110 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO], 13111 &cur_bt); 13112 13113#if 0 13114 cs_prof_gettime(&cur_ticks); 13115 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] += 13116 cur_ticks - 13117 io->io_hdr.start_ticks; 13118 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] += 13119 jiffies - io->io_hdr.start_time; 13120#endif 13121#endif /* CTL_TIME_IO */ 13122 break; 13123 } 13124 break; 13125 } 13126 default: 13127 break; 13128 } 13129 } 13130 13131 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13132 13133 /* 13134 * Run through the blocked queue on this LUN and see if anything 13135 * has become unblocked, now that this transaction is done. 13136 */ 13137 ctl_check_blocked(lun); 13138 13139 /* 13140 * If the LUN has been invalidated, free it if there is nothing 13141 * left on its OOA queue. 13142 */ 13143 if ((lun->flags & CTL_LUN_INVALID) 13144 && (TAILQ_FIRST(&lun->ooa_queue) == NULL)) 13145 ctl_free_lun(lun); 13146 13147 /* 13148 * If this command has been aborted, make sure we set the status 13149 * properly. The FETD is responsible for freeing the I/O and doing 13150 * whatever it needs to do to clean up its state. 13151 */ 13152 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13153 io->io_hdr.status = CTL_CMD_ABORTED; 13154 13155 /* 13156 * We print out status for every task management command. For SCSI 13157 * commands, we filter out any unit attention errors; they happen 13158 * on every boot, and would clutter up the log. Note: task 13159 * management commands aren't printed here, they are printed above, 13160 * since they should never even make it down here. 13161 */ 13162 switch (io->io_hdr.io_type) { 13163 case CTL_IO_SCSI: { 13164 int error_code, sense_key, asc, ascq; 13165 13166 sense_key = 0; 13167 13168 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13169 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13170 /* 13171 * Since this is just for printing, no need to 13172 * show errors here. 13173 */ 13174 scsi_extract_sense_len(&io->scsiio.sense_data, 13175 io->scsiio.sense_len, 13176 &error_code, 13177 &sense_key, 13178 &asc, 13179 &ascq, 13180 /*show_errors*/ 0); 13181 } 13182 13183 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13184 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13185 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13186 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13187 13188 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13189 ctl_softc->skipped_prints++; 13190 if (have_lock == 0) 13191 mtx_unlock(&ctl_softc->ctl_lock); 13192 } else { 13193 uint32_t skipped_prints; 13194 13195 skipped_prints = ctl_softc->skipped_prints; 13196 13197 ctl_softc->skipped_prints = 0; 13198 ctl_softc->last_print_jiffies = time_uptime; 13199 13200 if (have_lock == 0) 13201 mtx_unlock(&ctl_softc->ctl_lock); 13202 if (skipped_prints > 0) { 13203#ifdef NEEDTOPORT 13204 csevent_log(CSC_CTL | CSC_SHELF_SW | 13205 CTL_ERROR_REPORT, 13206 csevent_LogType_Trace, 13207 csevent_Severity_Information, 13208 csevent_AlertLevel_Green, 13209 csevent_FRU_Firmware, 13210 csevent_FRU_Unknown, 13211 "High CTL error volume, %d prints " 13212 "skipped", skipped_prints); 13213#endif 13214 } 13215 if (bootverbose || verbose > 0) 13216 ctl_io_error_print(io, NULL); 13217 } 13218 } else { 13219 if (have_lock == 0) 13220 mtx_unlock(&ctl_softc->ctl_lock); 13221 } 13222 break; 13223 } 13224 case CTL_IO_TASK: 13225 if (have_lock == 0) 13226 mtx_unlock(&ctl_softc->ctl_lock); 13227 if (bootverbose || verbose > 0) 13228 ctl_io_error_print(io, NULL); 13229 break; 13230 default: 13231 if (have_lock == 0) 13232 mtx_unlock(&ctl_softc->ctl_lock); 13233 break; 13234 } 13235 13236 /* 13237 * Tell the FETD or the other shelf controller we're done with this 13238 * command. Note that only SCSI commands get to this point. Task 13239 * management commands are completed above. 13240 * 13241 * We only send status to the other controller if we're in XFER 13242 * mode. In SER_ONLY mode, the I/O is done on the controller that 13243 * received the I/O (from CTL's perspective), and so the status is 13244 * generated there. 13245 * 13246 * XXX KDM if we hold the lock here, we could cause a deadlock 13247 * if the frontend comes back in in this context to queue 13248 * something. 13249 */ 13250 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13251 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13252 union ctl_ha_msg msg; 13253 13254 memset(&msg, 0, sizeof(msg)); 13255 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13256 msg.hdr.original_sc = io->io_hdr.original_sc; 13257 msg.hdr.nexus = io->io_hdr.nexus; 13258 msg.hdr.status = io->io_hdr.status; 13259 msg.scsi.scsi_status = io->scsiio.scsi_status; 13260 msg.scsi.tag_num = io->scsiio.tag_num; 13261 msg.scsi.tag_type = io->scsiio.tag_type; 13262 msg.scsi.sense_len = io->scsiio.sense_len; 13263 msg.scsi.sense_residual = io->scsiio.sense_residual; 13264 msg.scsi.residual = io->scsiio.residual; 13265 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13266 sizeof(io->scsiio.sense_data)); 13267 /* 13268 * We copy this whether or not this is an I/O-related 13269 * command. Otherwise, we'd have to go and check to see 13270 * whether it's a read/write command, and it really isn't 13271 * worth it. 13272 */ 13273 memcpy(&msg.scsi.lbalen, 13274 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13275 sizeof(msg.scsi.lbalen)); 13276 13277 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13278 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13279 /* XXX do something here */ 13280 } 13281 13282 ctl_free_io(io); 13283 } else 13284 fe_done(io); 13285 13286bailout: 13287 13288 return (CTL_RETVAL_COMPLETE); 13289} 13290 13291/* 13292 * Front end should call this if it doesn't do autosense. When the request 13293 * sense comes back in from the initiator, we'll dequeue this and send it. 13294 */ 13295int 13296ctl_queue_sense(union ctl_io *io) 13297{ 13298 struct ctl_lun *lun; 13299 struct ctl_softc *ctl_softc; 13300 uint32_t initidx, targ_lun; 13301 13302 ctl_softc = control_softc; 13303 13304 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13305 13306 /* 13307 * LUN lookup will likely move to the ctl_work_thread() once we 13308 * have our new queueing infrastructure (that doesn't put things on 13309 * a per-LUN queue initially). That is so that we can handle 13310 * things like an INQUIRY to a LUN that we don't have enabled. We 13311 * can't deal with that right now. 13312 */ 13313 mtx_lock(&ctl_softc->ctl_lock); 13314 13315 /* 13316 * If we don't have a LUN for this, just toss the sense 13317 * information. 13318 */ 13319 targ_lun = io->io_hdr.nexus.targ_lun; 13320 if (io->io_hdr.nexus.lun_map_fn != NULL) 13321 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 13322 if ((targ_lun < CTL_MAX_LUNS) 13323 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13324 lun = ctl_softc->ctl_luns[targ_lun]; 13325 else 13326 goto bailout; 13327 13328 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13329 13330 /* 13331 * Already have CA set for this LUN...toss the sense information. 13332 */ 13333 if (ctl_is_set(lun->have_ca, initidx)) 13334 goto bailout; 13335 13336 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data, 13337 ctl_min(sizeof(lun->pending_sense[initidx].sense), 13338 sizeof(io->scsiio.sense_data))); 13339 ctl_set_mask(lun->have_ca, initidx); 13340 13341bailout: 13342 mtx_unlock(&ctl_softc->ctl_lock); 13343 13344 ctl_free_io(io); 13345 13346 return (CTL_RETVAL_COMPLETE); 13347} 13348 13349/* 13350 * Primary command inlet from frontend ports. All SCSI and task I/O 13351 * requests must go through this function. 13352 */ 13353int 13354ctl_queue(union ctl_io *io) 13355{ 13356 struct ctl_softc *ctl_softc; 13357 13358 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13359 13360 ctl_softc = control_softc; 13361 13362#ifdef CTL_TIME_IO 13363 io->io_hdr.start_time = time_uptime; 13364 getbintime(&io->io_hdr.start_bt); 13365#endif /* CTL_TIME_IO */ 13366 13367 mtx_lock(&ctl_softc->ctl_lock); 13368 13369 switch (io->io_hdr.io_type) { 13370 case CTL_IO_SCSI: 13371 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr, 13372 links); 13373 break; 13374 case CTL_IO_TASK: 13375 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links); 13376 /* 13377 * Set the task pending flag. This is necessary to close a 13378 * race condition with the FETD: 13379 * 13380 * - FETD submits a task management command, like an abort. 13381 * - Back end calls fe_datamove() to move the data for the 13382 * aborted command. The FETD can't really accept it, but 13383 * if it did, it would end up transmitting data for a 13384 * command that the initiator told us to abort. 13385 * 13386 * We close the race condition by setting the flag here, 13387 * and checking it in ctl_datamove(), before calling the 13388 * FETD's fe_datamove routine. If we've got a task 13389 * pending, we run the task queue and then check to see 13390 * whether our particular I/O has been aborted. 13391 */ 13392 ctl_softc->flags |= CTL_FLAG_TASK_PENDING; 13393 break; 13394 default: 13395 mtx_unlock(&ctl_softc->ctl_lock); 13396 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13397 return (-EINVAL); 13398 break; /* NOTREACHED */ 13399 } 13400 mtx_unlock(&ctl_softc->ctl_lock); 13401 13402 ctl_wakeup_thread(); 13403 13404 return (CTL_RETVAL_COMPLETE); 13405} 13406 13407#ifdef CTL_IO_DELAY 13408static void 13409ctl_done_timer_wakeup(void *arg) 13410{ 13411 union ctl_io *io; 13412 13413 io = (union ctl_io *)arg; 13414 ctl_done_lock(io, /*have_lock*/ 0); 13415} 13416#endif /* CTL_IO_DELAY */ 13417 13418void 13419ctl_done_lock(union ctl_io *io, int have_lock) 13420{ 13421 struct ctl_softc *ctl_softc; 13422#ifndef CTL_DONE_THREAD 13423 union ctl_io *xio; 13424#endif /* !CTL_DONE_THREAD */ 13425 13426 ctl_softc = control_softc; 13427 13428 if (have_lock == 0) 13429 mtx_lock(&ctl_softc->ctl_lock); 13430 13431 /* 13432 * Enable this to catch duplicate completion issues. 13433 */ 13434#if 0 13435 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13436 printf("%s: type %d msg %d cdb %x iptl: " 13437 "%d:%d:%d:%d tag 0x%04x " 13438 "flag %#x status %x\n", 13439 __func__, 13440 io->io_hdr.io_type, 13441 io->io_hdr.msg_type, 13442 io->scsiio.cdb[0], 13443 io->io_hdr.nexus.initid.id, 13444 io->io_hdr.nexus.targ_port, 13445 io->io_hdr.nexus.targ_target.id, 13446 io->io_hdr.nexus.targ_lun, 13447 (io->io_hdr.io_type == 13448 CTL_IO_TASK) ? 13449 io->taskio.tag_num : 13450 io->scsiio.tag_num, 13451 io->io_hdr.flags, 13452 io->io_hdr.status); 13453 } else 13454 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13455#endif 13456 13457 /* 13458 * This is an internal copy of an I/O, and should not go through 13459 * the normal done processing logic. 13460 */ 13461 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) { 13462 if (have_lock == 0) 13463 mtx_unlock(&ctl_softc->ctl_lock); 13464 return; 13465 } 13466 13467 /* 13468 * We need to send a msg to the serializing shelf to finish the IO 13469 * as well. We don't send a finish message to the other shelf if 13470 * this is a task management command. Task management commands 13471 * aren't serialized in the OOA queue, but rather just executed on 13472 * both shelf controllers for commands that originated on that 13473 * controller. 13474 */ 13475 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13476 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13477 union ctl_ha_msg msg_io; 13478 13479 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13480 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13481 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13482 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13483 } 13484 /* continue on to finish IO */ 13485 } 13486#ifdef CTL_IO_DELAY 13487 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13488 struct ctl_lun *lun; 13489 13490 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13491 13492 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13493 } else { 13494 struct ctl_lun *lun; 13495 13496 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13497 13498 if ((lun != NULL) 13499 && (lun->delay_info.done_delay > 0)) { 13500 struct callout *callout; 13501 13502 callout = (struct callout *)&io->io_hdr.timer_bytes; 13503 callout_init(callout, /*mpsafe*/ 1); 13504 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13505 callout_reset(callout, 13506 lun->delay_info.done_delay * hz, 13507 ctl_done_timer_wakeup, io); 13508 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13509 lun->delay_info.done_delay = 0; 13510 if (have_lock == 0) 13511 mtx_unlock(&ctl_softc->ctl_lock); 13512 return; 13513 } 13514 } 13515#endif /* CTL_IO_DELAY */ 13516 13517 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links); 13518 13519#ifdef CTL_DONE_THREAD 13520 if (have_lock == 0) 13521 mtx_unlock(&ctl_softc->ctl_lock); 13522 13523 ctl_wakeup_thread(); 13524#else /* CTL_DONE_THREAD */ 13525 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue); 13526 xio != NULL; 13527 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) { 13528 13529 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links); 13530 13531 ctl_process_done(xio, /*have_lock*/ 1); 13532 } 13533 if (have_lock == 0) 13534 mtx_unlock(&ctl_softc->ctl_lock); 13535#endif /* CTL_DONE_THREAD */ 13536} 13537 13538void 13539ctl_done(union ctl_io *io) 13540{ 13541 ctl_done_lock(io, /*have_lock*/ 0); 13542} 13543 13544int 13545ctl_isc(struct ctl_scsiio *ctsio) 13546{ 13547 struct ctl_lun *lun; 13548 int retval; 13549 13550 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13551 13552 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13553 13554 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13555 13556 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13557 13558 return (retval); 13559} 13560 13561 13562static void 13563ctl_work_thread(void *arg) 13564{ 13565 struct ctl_softc *softc; 13566 union ctl_io *io; 13567 struct ctl_be_lun *be_lun; 13568 int retval; 13569 13570 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13571 13572 softc = (struct ctl_softc *)arg; 13573 if (softc == NULL) 13574 return; 13575 13576 mtx_lock(&softc->ctl_lock); 13577 for (;;) { 13578 retval = 0; 13579 13580 /* 13581 * We handle the queues in this order: 13582 * - task management 13583 * - ISC 13584 * - done queue (to free up resources, unblock other commands) 13585 * - RtR queue 13586 * - incoming queue 13587 * 13588 * If those queues are empty, we break out of the loop and 13589 * go to sleep. 13590 */ 13591 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue); 13592 if (io != NULL) { 13593 ctl_run_task_queue(softc); 13594 continue; 13595 } 13596 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue); 13597 if (io != NULL) { 13598 STAILQ_REMOVE_HEAD(&softc->isc_queue, links); 13599 ctl_handle_isc(io); 13600 continue; 13601 } 13602 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue); 13603 if (io != NULL) { 13604 STAILQ_REMOVE_HEAD(&softc->done_queue, links); 13605 /* clear any blocked commands, call fe_done */ 13606 mtx_unlock(&softc->ctl_lock); 13607 /* 13608 * XXX KDM 13609 * Call this without a lock for now. This will 13610 * depend on whether there is any way the FETD can 13611 * sleep or deadlock if called with the CTL lock 13612 * held. 13613 */ 13614 retval = ctl_process_done(io, /*have_lock*/ 0); 13615 mtx_lock(&softc->ctl_lock); 13616 continue; 13617 } 13618 if (!ctl_pause_rtr) { 13619 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 13620 if (io != NULL) { 13621 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links); 13622 mtx_unlock(&softc->ctl_lock); 13623 retval = ctl_scsiio(&io->scsiio); 13624 if (retval != CTL_RETVAL_COMPLETE) 13625 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13626 mtx_lock(&softc->ctl_lock); 13627 continue; 13628 } 13629 } 13630 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue); 13631 if (io != NULL) { 13632 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links); 13633 mtx_unlock(&softc->ctl_lock); 13634 ctl_scsiio_precheck(softc, &io->scsiio); 13635 mtx_lock(&softc->ctl_lock); 13636 continue; 13637 } 13638 /* 13639 * We might want to move this to a separate thread, so that 13640 * configuration requests (in this case LUN creations) 13641 * won't impact the I/O path. 13642 */ 13643 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13644 if (be_lun != NULL) { 13645 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13646 mtx_unlock(&softc->ctl_lock); 13647 ctl_create_lun(be_lun); 13648 mtx_lock(&softc->ctl_lock); 13649 continue; 13650 } 13651 13652 /* XXX KDM use the PDROP flag?? */ 13653 /* Sleep until we have something to do. */ 13654 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "-", 0); 13655 13656 /* Back to the top of the loop to see what woke us up. */ 13657 continue; 13658 } 13659} 13660 13661void 13662ctl_wakeup_thread() 13663{ 13664 struct ctl_softc *softc; 13665 13666 softc = control_softc; 13667 13668 wakeup_one(softc); 13669} 13670 13671/* Initialization and failover */ 13672 13673void 13674ctl_init_isc_msg(void) 13675{ 13676 printf("CTL: Still calling this thing\n"); 13677} 13678 13679/* 13680 * Init component 13681 * Initializes component into configuration defined by bootMode 13682 * (see hasc-sv.c) 13683 * returns hasc_Status: 13684 * OK 13685 * ERROR - fatal error 13686 */ 13687static ctl_ha_comp_status 13688ctl_isc_init(struct ctl_ha_component *c) 13689{ 13690 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13691 13692 c->status = ret; 13693 return ret; 13694} 13695 13696/* Start component 13697 * Starts component in state requested. If component starts successfully, 13698 * it must set its own state to the requestrd state 13699 * When requested state is HASC_STATE_HA, the component may refine it 13700 * by adding _SLAVE or _MASTER flags. 13701 * Currently allowed state transitions are: 13702 * UNKNOWN->HA - initial startup 13703 * UNKNOWN->SINGLE - initial startup when no parter detected 13704 * HA->SINGLE - failover 13705 * returns ctl_ha_comp_status: 13706 * OK - component successfully started in requested state 13707 * FAILED - could not start the requested state, failover may 13708 * be possible 13709 * ERROR - fatal error detected, no future startup possible 13710 */ 13711static ctl_ha_comp_status 13712ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 13713{ 13714 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13715 13716 printf("%s: go\n", __func__); 13717 13718 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 13719 if (c->state == CTL_HA_STATE_UNKNOWN ) { 13720 ctl_is_single = 0; 13721 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 13722 != CTL_HA_STATUS_SUCCESS) { 13723 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 13724 ret = CTL_HA_COMP_STATUS_ERROR; 13725 } 13726 } else if (CTL_HA_STATE_IS_HA(c->state) 13727 && CTL_HA_STATE_IS_SINGLE(state)){ 13728 // HA->SINGLE transition 13729 ctl_failover(); 13730 ctl_is_single = 1; 13731 } else { 13732 printf("ctl_isc_start:Invalid state transition %X->%X\n", 13733 c->state, state); 13734 ret = CTL_HA_COMP_STATUS_ERROR; 13735 } 13736 if (CTL_HA_STATE_IS_SINGLE(state)) 13737 ctl_is_single = 1; 13738 13739 c->state = state; 13740 c->status = ret; 13741 return ret; 13742} 13743 13744/* 13745 * Quiesce component 13746 * The component must clear any error conditions (set status to OK) and 13747 * prepare itself to another Start call 13748 * returns ctl_ha_comp_status: 13749 * OK 13750 * ERROR 13751 */ 13752static ctl_ha_comp_status 13753ctl_isc_quiesce(struct ctl_ha_component *c) 13754{ 13755 int ret = CTL_HA_COMP_STATUS_OK; 13756 13757 ctl_pause_rtr = 1; 13758 c->status = ret; 13759 return ret; 13760} 13761 13762struct ctl_ha_component ctl_ha_component_ctlisc = 13763{ 13764 .name = "CTL ISC", 13765 .state = CTL_HA_STATE_UNKNOWN, 13766 .init = ctl_isc_init, 13767 .start = ctl_isc_start, 13768 .quiesce = ctl_isc_quiesce 13769}; 13770 13771/* 13772 * vim: ts=8 13773 */ 13774