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