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