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