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