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