scsi_ctl.c revision 249009
1/*- 2 * Copyright (c) 2008, 2009 Silicon Graphics International Corp. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions, and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * substantially similar to the "NO WARRANTY" disclaimer below 13 * ("Disclaimer") and any redistribution must be conditioned upon 14 * including a substantially similar Disclaimer requirement for further 15 * binary redistribution. 16 * 17 * NO WARRANTY 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGES. 29 * 30 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $ 31 */ 32/* 33 * Peripheral driver interface between CAM and CTL (CAM Target Layer). 34 * 35 * Author: Ken Merry <ken@FreeBSD.org> 36 */ 37 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: head/sys/cam/ctl/scsi_ctl.c 249009 2013-04-02 09:42:42Z trasz $"); 40 41#include <sys/param.h> 42#include <sys/queue.h> 43#include <sys/systm.h> 44#include <sys/kernel.h> 45#include <sys/lock.h> 46#include <sys/mutex.h> 47#include <sys/condvar.h> 48#include <sys/malloc.h> 49#include <sys/bus.h> 50#include <sys/endian.h> 51#include <sys/sbuf.h> 52#include <sys/sysctl.h> 53#include <sys/types.h> 54#include <sys/systm.h> 55#include <machine/bus.h> 56 57#include <cam/cam.h> 58#include <cam/cam_ccb.h> 59#include <cam/cam_periph.h> 60#include <cam/cam_queue.h> 61#include <cam/cam_xpt_periph.h> 62#include <cam/cam_debug.h> 63#include <cam/cam_sim.h> 64#include <cam/cam_xpt.h> 65 66#include <cam/scsi/scsi_all.h> 67#include <cam/scsi/scsi_message.h> 68 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_util.h> 73#include <cam/ctl/ctl_error.h> 74 75typedef enum { 76 CTLFE_CCB_DEFAULT = 0x00, 77 CTLFE_CCB_WAITING = 0x01 78} ctlfe_ccb_types; 79 80struct ctlfe_softc { 81 struct ctl_frontend fe; 82 path_id_t path_id; 83 struct cam_sim *sim; 84 char port_name[DEV_IDLEN]; 85 STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list; 86 STAILQ_ENTRY(ctlfe_softc) links; 87}; 88 89STAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list; 90struct mtx ctlfe_list_mtx; 91static char ctlfe_mtx_desc[] = "ctlfelist"; 92static int ctlfe_dma_enabled = 1; 93#ifdef CTLFE_INIT_ENABLE 94static int ctlfe_max_targets = 1; 95static int ctlfe_num_targets = 0; 96#endif 97 98typedef enum { 99 CTLFE_LUN_NONE = 0x00, 100 CTLFE_LUN_WILDCARD = 0x01 101} ctlfe_lun_flags; 102 103struct ctlfe_lun_softc { 104 struct ctlfe_softc *parent_softc; 105 struct cam_periph *periph; 106 ctlfe_lun_flags flags; 107 struct callout dma_callout; 108 uint64_t ccbs_alloced; 109 uint64_t ccbs_freed; 110 uint64_t ctios_sent; 111 uint64_t ctios_returned; 112 uint64_t atios_sent; 113 uint64_t atios_returned; 114 uint64_t inots_sent; 115 uint64_t inots_returned; 116 /* bus_dma_tag_t dma_tag; */ 117 TAILQ_HEAD(, ccb_hdr) work_queue; 118 STAILQ_ENTRY(ctlfe_lun_softc) links; 119}; 120 121typedef enum { 122 CTLFE_CMD_NONE = 0x00, 123 CTLFE_CMD_PIECEWISE = 0x01 124} ctlfe_cmd_flags; 125 126/* 127 * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h. 128 * Currently that is 600 bytes. 129 */ 130struct ctlfe_lun_cmd_info { 131 int cur_transfer_index; 132 ctlfe_cmd_flags flags; 133 /* 134 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16 135 * bytes on amd64. So with 32 elements, this is 256 bytes on 136 * i386 and 512 bytes on amd64. 137 */ 138 bus_dma_segment_t cam_sglist[32]; 139}; 140 141/* 142 * When we register the adapter/bus, request that this many ctl_ios be 143 * allocated. This should be the maximum supported by the adapter, but we 144 * currently don't have a way to get that back from the path inquiry. 145 * XXX KDM add that to the path inquiry. 146 */ 147#define CTLFE_REQ_CTL_IO 4096 148/* 149 * Number of Accept Target I/O CCBs to allocate and queue down to the 150 * adapter per LUN. 151 * XXX KDM should this be controlled by CTL? 152 */ 153#define CTLFE_ATIO_PER_LUN 1024 154/* 155 * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to 156 * allocate and queue down to the adapter per LUN. 157 * XXX KDM should this be controlled by CTL? 158 */ 159#define CTLFE_IN_PER_LUN 1024 160 161/* 162 * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending 163 * status to the initiator. The SIM is expected to have its own timeouts, 164 * so we're not putting this timeout around the CCB execution time. The 165 * SIM should timeout and let us know if it has an issue. 166 */ 167#define CTLFE_DMA_TIMEOUT 60 168 169/* 170 * Turn this on to enable extra debugging prints. 171 */ 172#if 0 173#define CTLFE_DEBUG 174#endif 175 176/* 177 * Use randomly assigned WWNN/WWPN values. This is to work around an issue 178 * in the FreeBSD initiator that makes it unable to rescan the target if 179 * the target gets rebooted and the WWNN/WWPN stay the same. 180 */ 181#if 0 182#define RANDOM_WWNN 183#endif 184 185SYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW, 186 &ctlfe_dma_enabled, 0, "DMA enabled"); 187MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface"); 188 189#define ccb_type ppriv_field0 190/* This is only used in the ATIO */ 191#define io_ptr ppriv_ptr1 192 193/* This is only used in the CTIO */ 194#define ccb_atio ppriv_ptr1 195 196int ctlfeinitialize(void); 197void ctlfeshutdown(void); 198static periph_init_t ctlfeinit; 199static void ctlfeasync(void *callback_arg, uint32_t code, 200 struct cam_path *path, void *arg); 201static periph_ctor_t ctlferegister; 202static periph_oninv_t ctlfeoninvalidate; 203static periph_dtor_t ctlfecleanup; 204static periph_start_t ctlfestart; 205static void ctlfedone(struct cam_periph *periph, 206 union ccb *done_ccb); 207 208static void ctlfe_onoffline(void *arg, int online); 209static void ctlfe_online(void *arg); 210static void ctlfe_offline(void *arg); 211static int ctlfe_targ_enable(void *arg, struct ctl_id targ_id); 212static int ctlfe_targ_disable(void *arg, struct ctl_id targ_id); 213static int ctlfe_lun_enable(void *arg, struct ctl_id targ_id, 214 int lun_id); 215static int ctlfe_lun_disable(void *arg, struct ctl_id targ_id, 216 int lun_id); 217static void ctlfe_dump_sim(struct cam_sim *sim); 218static void ctlfe_dump_queue(struct ctlfe_lun_softc *softc); 219static void ctlfe_dma_timeout(void *arg); 220static void ctlfe_datamove_done(union ctl_io *io); 221static void ctlfe_dump(void); 222 223static struct periph_driver ctlfe_driver = 224{ 225 ctlfeinit, "ctl", 226 TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0 227}; 228 229static int ctlfe_module_event_handler(module_t, int /*modeventtype_t*/, void *); 230 231/* 232 * We're not using PERIPHDRIVER_DECLARE(), because it runs at SI_SUB_DRIVERS, 233 * and that happens before CTL gets initialised. 234 */ 235static moduledata_t ctlfe_moduledata = { 236 "ctlfe", 237 ctlfe_module_event_handler, 238 NULL 239}; 240 241DECLARE_MODULE(ctlfe, ctlfe_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); 242MODULE_VERSION(ctlfe, 1); 243MODULE_DEPEND(ctlfe, ctl, 1, 1, 1); 244MODULE_DEPEND(ctlfe, cam, 1, 1, 1); 245 246extern struct ctl_softc *control_softc; 247extern int ctl_disable; 248 249#ifdef seems_unused 250int 251ctlfeinitialize(void) 252{ 253 cam_status status; 254 255 /* Don't initialize if we're disabled */ 256 if (ctl_disable != 0) 257 return (0); 258 259 STAILQ_INIT(&ctlfe_softc_list); 260 261 mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF); 262 263 xpt_lock_buses(); 264 periphdriver_register(&ctlfe_driver); 265 xpt_unlock_buses(); 266 267 status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED | 268 AC_CONTRACT, ctlfeasync, NULL, NULL); 269 270 if (status != CAM_REQ_CMP) { 271 printf("ctl: Failed to attach async callback due to CAM " 272 "status 0x%x!\n", status); 273 } 274 275 return (0); 276} 277#endif 278 279void 280ctlfeshutdown(void) 281{ 282 return; 283} 284 285void 286ctlfeinit(void) 287{ 288 cam_status status; 289 290 /* Don't initialize if we're disabled */ 291 if (ctl_disable != 0) 292 return; 293 294 STAILQ_INIT(&ctlfe_softc_list); 295 296 mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF); 297 298 KASSERT(control_softc != NULL, ("CTL is not initialized!")); 299 300 status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED | 301 AC_CONTRACT, ctlfeasync, NULL, NULL); 302 303 if (status != CAM_REQ_CMP) { 304 printf("ctl: Failed to attach async callback due to CAM " 305 "status 0x%x!\n", status); 306 } 307} 308 309static int 310ctlfe_module_event_handler(module_t mod, int what, void *arg) 311{ 312 313 switch (what) { 314 case MOD_LOAD: 315 periphdriver_register(&ctlfe_driver); 316 return (0); 317 case MOD_UNLOAD: 318 return (EBUSY); 319 default: 320 return (EOPNOTSUPP); 321 } 322} 323 324static void 325ctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg) 326{ 327 328#ifdef CTLFEDEBUG 329 printf("%s: entered\n", __func__); 330#endif 331 332 /* 333 * When a new path gets registered, and it is capable of target 334 * mode, go ahead and attach. Later on, we may need to be more 335 * selective, but for now this will be sufficient. 336 */ 337 switch (code) { 338 case AC_PATH_REGISTERED: { 339 struct ctl_frontend *fe; 340 struct ctlfe_softc *bus_softc; 341 struct ccb_pathinq *cpi; 342 int retval; 343 344 cpi = (struct ccb_pathinq *)arg; 345 346 /* Don't attach if it doesn't support target mode */ 347 if ((cpi->target_sprt & PIT_PROCESSOR) == 0) { 348#ifdef CTLFEDEBUG 349 printf("%s: SIM %s%d doesn't support target mode\n", 350 __func__, cpi->dev_name, cpi->unit_number); 351#endif 352 break; 353 } 354 355#ifdef CTLFE_INIT_ENABLE 356 if (ctlfe_num_targets >= ctlfe_max_targets) { 357 union ccb *ccb; 358 struct cam_sim *sim; 359 360 ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, 361 M_NOWAIT | M_ZERO); 362 if (ccb == NULL) { 363 printf("%s: unable to malloc CCB!\n", __func__); 364 return; 365 } 366 xpt_setup_ccb(&ccb->ccb_h, cpi->ccb_h.path, 367 CAM_PRIORITY_NONE); 368 369 sim = xpt_path_sim(cpi->ccb_h.path); 370 371 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB; 372 ccb->knob.xport_specific.valid = KNOB_VALID_ROLE; 373 ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR; 374 375 /* We should hold the SIM lock here */ 376 mtx_assert(sim->mtx, MA_OWNED); 377 378 xpt_action(ccb); 379 380 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 381 CAM_REQ_CMP) { 382 printf("%s: SIM %s%d (path id %d) initiator " 383 "enable failed with status %#x\n", 384 __func__, cpi->dev_name, 385 cpi->unit_number, cpi->ccb_h.path_id, 386 ccb->ccb_h.status); 387 } else { 388 printf("%s: SIM %s%d (path id %d) initiator " 389 "enable succeeded\n", 390 __func__, cpi->dev_name, 391 cpi->unit_number, cpi->ccb_h.path_id); 392 } 393 394 free(ccb, M_TEMP); 395 396 break; 397 } else { 398 ctlfe_num_targets++; 399 } 400 401 printf("%s: ctlfe_num_targets = %d\n", __func__, 402 ctlfe_num_targets); 403#endif /* CTLFE_INIT_ENABLE */ 404 405 /* 406 * We're in an interrupt context here, so we have to 407 * use M_NOWAIT. Of course this means trouble if we 408 * can't allocate memory. 409 */ 410 bus_softc = malloc(sizeof(*bus_softc), M_CTLFE, 411 M_NOWAIT | M_ZERO); 412 if (bus_softc == NULL) { 413 printf("%s: unable to malloc %zd bytes for softc\n", 414 __func__, sizeof(*bus_softc)); 415 return; 416 } 417 418 bus_softc->path_id = cpi->ccb_h.path_id; 419 bus_softc->sim = xpt_path_sim(cpi->ccb_h.path); 420 STAILQ_INIT(&bus_softc->lun_softc_list); 421 422 fe = &bus_softc->fe; 423 424 /* 425 * XXX KDM should we be more accurate here ? 426 */ 427 if (cpi->transport == XPORT_FC) 428 fe->port_type = CTL_PORT_FC; 429 else 430 fe->port_type = CTL_PORT_SCSI; 431 432 /* XXX KDM what should the real number be here? */ 433 fe->num_requested_ctl_io = 4096; 434 snprintf(bus_softc->port_name, sizeof(bus_softc->port_name), 435 "%s%d", cpi->dev_name, cpi->unit_number); 436 /* 437 * XXX KDM it would be nice to allocate storage in the 438 * frontend structure itself. 439 */ 440 fe->port_name = bus_softc->port_name; 441 fe->physical_port = cpi->unit_number; 442 fe->virtual_port = cpi->bus_id; 443 fe->port_online = ctlfe_online; 444 fe->port_offline = ctlfe_offline; 445 fe->onoff_arg = bus_softc; 446 fe->targ_enable = ctlfe_targ_enable; 447 fe->targ_disable = ctlfe_targ_disable; 448 fe->lun_enable = ctlfe_lun_enable; 449 fe->lun_disable = ctlfe_lun_disable; 450 fe->targ_lun_arg = bus_softc; 451 fe->fe_datamove = ctlfe_datamove_done; 452 fe->fe_done = ctlfe_datamove_done; 453 fe->fe_dump = ctlfe_dump; 454 /* 455 * XXX KDM the path inquiry doesn't give us the maximum 456 * number of targets supported. 457 */ 458 fe->max_targets = cpi->max_target; 459 fe->max_target_id = cpi->max_target; 460 461 /* 462 * XXX KDM need to figure out whether we're the master or 463 * slave. 464 */ 465#ifdef CTLFEDEBUG 466 printf("%s: calling ctl_frontend_register() for %s%d\n", 467 __func__, cpi->dev_name, cpi->unit_number); 468#endif 469 retval = ctl_frontend_register(fe, /*master_SC*/ 1); 470 if (retval != 0) { 471 printf("%s: ctl_frontend_register() failed with " 472 "error %d!\n", __func__, retval); 473 free(bus_softc, M_CTLFE); 474 break; 475 } else { 476 mtx_lock(&ctlfe_list_mtx); 477 STAILQ_INSERT_TAIL(&ctlfe_softc_list, bus_softc, links); 478 mtx_unlock(&ctlfe_list_mtx); 479 } 480 481 break; 482 } 483 case AC_PATH_DEREGISTERED: { 484 struct ctlfe_softc *softc = NULL; 485 486 mtx_lock(&ctlfe_list_mtx); 487 STAILQ_FOREACH(softc, &ctlfe_softc_list, links) { 488 if (softc->path_id == xpt_path_path_id(path)) { 489 STAILQ_REMOVE(&ctlfe_softc_list, softc, 490 ctlfe_softc, links); 491 break; 492 } 493 } 494 mtx_unlock(&ctlfe_list_mtx); 495 496 if (softc != NULL) { 497 /* 498 * XXX KDM are we certain at this point that there 499 * are no outstanding commands for this frontend? 500 */ 501 ctl_frontend_deregister(&softc->fe); 502 free(softc, M_CTLFE); 503 } 504 break; 505 } 506 case AC_CONTRACT: { 507 struct ac_contract *ac; 508 509 ac = (struct ac_contract *)arg; 510 511 switch (ac->contract_number) { 512 case AC_CONTRACT_DEV_CHG: { 513 struct ac_device_changed *dev_chg; 514 struct ctlfe_softc *softc; 515 int retval, found; 516 517 dev_chg = (struct ac_device_changed *)ac->contract_data; 518 519 printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n", 520 __func__, dev_chg->wwpn, dev_chg->port, 521 xpt_path_path_id(path), dev_chg->target, 522 (dev_chg->arrived == 0) ? "left" : "arrived"); 523 524 found = 0; 525 526 mtx_lock(&ctlfe_list_mtx); 527 STAILQ_FOREACH(softc, &ctlfe_softc_list, links) { 528 if (softc->path_id == xpt_path_path_id(path)) { 529 found = 1; 530 break; 531 } 532 } 533 mtx_unlock(&ctlfe_list_mtx); 534 535 if (found == 0) { 536 printf("%s: CTL port for CAM path %u not " 537 "found!\n", __func__, 538 xpt_path_path_id(path)); 539 break; 540 } 541 if (dev_chg->arrived != 0) { 542 retval = ctl_add_initiator(dev_chg->wwpn, 543 softc->fe.targ_port, dev_chg->target); 544 } else { 545 retval = ctl_remove_initiator( 546 softc->fe.targ_port, dev_chg->target); 547 } 548 549 if (retval != 0) { 550 printf("%s: could not %s port %d iid %u " 551 "WWPN %#jx!\n", __func__, 552 (dev_chg->arrived != 0) ? "add" : 553 "remove", softc->fe.targ_port, 554 dev_chg->target, 555 (uintmax_t)dev_chg->wwpn); 556 } 557 break; 558 } 559 default: 560 printf("%s: unsupported contract number %ju\n", 561 __func__, (uintmax_t)ac->contract_number); 562 break; 563 } 564 break; 565 } 566 default: 567 break; 568 } 569} 570 571static cam_status 572ctlferegister(struct cam_periph *periph, void *arg) 573{ 574 struct ctlfe_softc *bus_softc; 575 struct ctlfe_lun_softc *softc; 576 struct cam_sim *sim; 577 union ccb en_lun_ccb; 578 cam_status status; 579 int i; 580 581 softc = (struct ctlfe_lun_softc *)arg; 582 bus_softc = softc->parent_softc; 583 sim = xpt_path_sim(periph->path); 584 585 TAILQ_INIT(&softc->work_queue); 586 softc->periph = periph; 587 588 callout_init_mtx(&softc->dma_callout, sim->mtx, /*flags*/ 0); 589 periph->softc = softc; 590 591 xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE); 592 en_lun_ccb.ccb_h.func_code = XPT_EN_LUN; 593 en_lun_ccb.cel.grp6_len = 0; 594 en_lun_ccb.cel.grp7_len = 0; 595 en_lun_ccb.cel.enable = 1; 596 xpt_action(&en_lun_ccb); 597 status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK); 598 if (status != CAM_REQ_CMP) { 599 xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n", 600 __func__, en_lun_ccb.ccb_h.status); 601 return (status); 602 } 603 604 status = CAM_REQ_CMP; 605 606 for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) { 607 union ccb *new_ccb; 608 609 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE, 610 M_ZERO|M_NOWAIT); 611 if (new_ccb == NULL) { 612 status = CAM_RESRC_UNAVAIL; 613 break; 614 } 615 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1); 616 new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO; 617 new_ccb->ccb_h.cbfcnp = ctlfedone; 618 xpt_action(new_ccb); 619 softc->atios_sent++; 620 status = new_ccb->ccb_h.status; 621 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 622 free(new_ccb, M_CTLFE); 623 break; 624 } 625 } 626 627 status = cam_periph_acquire(periph); 628 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 629 xpt_print(periph->path, "%s: could not acquire reference " 630 "count, status = %#x\n", __func__, status); 631 return (status); 632 } 633 634 if (i == 0) { 635 xpt_print(periph->path, "%s: could not allocate ATIO CCBs, " 636 "status 0x%x\n", __func__, status); 637 return (CAM_REQ_CMP_ERR); 638 } 639 640 for (i = 0; i < CTLFE_IN_PER_LUN; i++) { 641 union ccb *new_ccb; 642 643 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE, 644 M_ZERO|M_NOWAIT); 645 if (new_ccb == NULL) { 646 status = CAM_RESRC_UNAVAIL; 647 break; 648 } 649 650 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1); 651 new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY; 652 new_ccb->ccb_h.cbfcnp = ctlfedone; 653 xpt_action(new_ccb); 654 softc->inots_sent++; 655 status = new_ccb->ccb_h.status; 656 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 657 /* 658 * Note that we don't free the CCB here. If the 659 * status is not CAM_REQ_INPROG, then we're 660 * probably talking to a SIM that says it is 661 * target-capable but doesn't support the 662 * XPT_IMMEDIATE_NOTIFY CCB. i.e. it supports the 663 * older API. In that case, it'll call xpt_done() 664 * on the CCB, and we need to free it in our done 665 * routine as a result. 666 */ 667 break; 668 } 669 } 670 if ((i == 0) 671 || (status != CAM_REQ_INPROG)) { 672 xpt_print(periph->path, "%s: could not allocate immediate " 673 "notify CCBs, status 0x%x\n", __func__, status); 674 return (CAM_REQ_CMP_ERR); 675 } 676 return (CAM_REQ_CMP); 677} 678 679static void 680ctlfeoninvalidate(struct cam_periph *periph) 681{ 682 union ccb en_lun_ccb; 683 cam_status status; 684 struct ctlfe_lun_softc *softc; 685 686 softc = (struct ctlfe_lun_softc *)periph->softc; 687 688 xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE); 689 en_lun_ccb.ccb_h.func_code = XPT_EN_LUN; 690 en_lun_ccb.cel.grp6_len = 0; 691 en_lun_ccb.cel.grp7_len = 0; 692 en_lun_ccb.cel.enable = 0; 693 xpt_action(&en_lun_ccb); 694 status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK); 695 if (status != CAM_REQ_CMP) { 696 xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n", 697 __func__, en_lun_ccb.ccb_h.status); 698 /* 699 * XXX KDM what do we do now? 700 */ 701 } 702 xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju " 703 "INOTs outstanding, %d refs\n", softc->atios_sent - 704 softc->atios_returned, softc->inots_sent - 705 softc->inots_returned, periph->refcount); 706} 707 708static void 709ctlfecleanup(struct cam_periph *periph) 710{ 711 struct ctlfe_lun_softc *softc; 712 struct ctlfe_softc *bus_softc; 713 714 xpt_print(periph->path, "%s: Called\n", __func__); 715 716 softc = (struct ctlfe_lun_softc *)periph->softc; 717 bus_softc = softc->parent_softc; 718 719 STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links); 720 721 /* 722 * XXX KDM is there anything else that needs to be done here? 723 */ 724 725 callout_stop(&softc->dma_callout); 726 727 free(softc, M_CTLFE); 728} 729 730static void 731ctlfestart(struct cam_periph *periph, union ccb *start_ccb) 732{ 733 struct ctlfe_lun_softc *softc; 734 struct ccb_hdr *ccb_h; 735 736 softc = (struct ctlfe_lun_softc *)periph->softc; 737 738 softc->ccbs_alloced++; 739 740 start_ccb->ccb_h.ccb_type = CTLFE_CCB_DEFAULT; 741 742 ccb_h = TAILQ_FIRST(&softc->work_queue); 743 if (periph->immediate_priority <= periph->pinfo.priority) { 744 panic("shouldn't get to the CCB waiting case!"); 745 start_ccb->ccb_h.ccb_type = CTLFE_CCB_WAITING; 746 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 747 periph_links.sle); 748 periph->immediate_priority = CAM_PRIORITY_NONE; 749 wakeup(&periph->ccb_list); 750 } else if (ccb_h == NULL) { 751 softc->ccbs_freed++; 752 xpt_release_ccb(start_ccb); 753 } else { 754 struct ccb_accept_tio *atio; 755 struct ccb_scsiio *csio; 756 uint8_t *data_ptr; 757 uint32_t dxfer_len; 758 ccb_flags flags; 759 union ctl_io *io; 760 uint8_t scsi_status; 761 762 /* Take the ATIO off the work queue */ 763 TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe); 764 atio = (struct ccb_accept_tio *)ccb_h; 765 io = (union ctl_io *)ccb_h->io_ptr; 766 csio = &start_ccb->csio; 767 768 flags = atio->ccb_h.flags & 769 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK); 770 771 if ((io == NULL) 772 || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 773 /* 774 * We're done, send status back. 775 */ 776 flags |= CAM_SEND_STATUS; 777 if (io == NULL) { 778 scsi_status = SCSI_STATUS_BUSY; 779 csio->sense_len = 0; 780 } else if ((io->io_hdr.status & CTL_STATUS_MASK) == 781 CTL_CMD_ABORTED) { 782 io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED; 783 784 /* 785 * If this command was aborted, we don't 786 * need to send status back to the SIM. 787 * Just free the CTIO and ctl_io, and 788 * recycle the ATIO back to the SIM. 789 */ 790 xpt_print(periph->path, "%s: aborted " 791 "command 0x%04x discarded\n", 792 __func__, io->scsiio.tag_num); 793 ctl_free_io(io); 794 /* 795 * For a wildcard attachment, commands can 796 * come in with a specific target/lun. Reset 797 * the target and LUN fields back to the 798 * wildcard values before we send them back 799 * down to the SIM. The SIM has a wildcard 800 * LUN enabled, not whatever target/lun 801 * these happened to be. 802 */ 803 if (softc->flags & CTLFE_LUN_WILDCARD) { 804 atio->ccb_h.target_id = 805 CAM_TARGET_WILDCARD; 806 atio->ccb_h.target_lun = 807 CAM_LUN_WILDCARD; 808 } 809 810 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) { 811 cam_release_devq(periph->path, 812 /*relsim_flags*/0, 813 /*reduction*/0, 814 /*timeout*/0, 815 /*getcount_only*/0); 816 atio->ccb_h.status &= ~CAM_DEV_QFRZN; 817 } 818 819 ccb_h = TAILQ_FIRST(&softc->work_queue); 820 821 if (atio->ccb_h.func_code != 822 XPT_ACCEPT_TARGET_IO) { 823 xpt_print(periph->path, "%s: func_code " 824 "is %#x\n", __func__, 825 atio->ccb_h.func_code); 826 } 827 start_ccb->ccb_h.func_code = XPT_ABORT; 828 start_ccb->cab.abort_ccb = (union ccb *)atio; 829 start_ccb->ccb_h.cbfcnp = ctlfedone; 830 831 /* Tell the SIM that we've aborted this ATIO */ 832 xpt_action(start_ccb); 833 softc->ccbs_freed++; 834 xpt_release_ccb(start_ccb); 835 836 /* 837 * Send the ATIO back down to the SIM. 838 */ 839 xpt_action((union ccb *)atio); 840 softc->atios_sent++; 841 842 /* 843 * If we still have work to do, ask for 844 * another CCB. Otherwise, deactivate our 845 * callout. 846 */ 847 if (ccb_h != NULL) 848 xpt_schedule(periph, /*priority*/ 1); 849 else 850 callout_stop(&softc->dma_callout); 851 852 return; 853 } else { 854 io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED; 855 scsi_status = io->scsiio.scsi_status; 856 csio->sense_len = io->scsiio.sense_len; 857 } 858 data_ptr = NULL; 859 dxfer_len = 0; 860 if (io == NULL) { 861 printf("%s: tag %04x io is NULL\n", __func__, 862 atio->tag_id); 863 } else { 864#ifdef CTLFEDEBUG 865 printf("%s: tag %04x status %x\n", __func__, 866 atio->tag_id, io->io_hdr.status); 867#endif 868 } 869 csio->sglist_cnt = 0; 870 if (csio->sense_len != 0) { 871 csio->sense_data = io->scsiio.sense_data; 872 flags |= CAM_SEND_SENSE; 873 } else if (scsi_status == SCSI_STATUS_CHECK_COND) { 874 xpt_print(periph->path, "%s: check condition " 875 "with no sense\n", __func__); 876 } 877 } else { 878 struct ctlfe_lun_cmd_info *cmd_info; 879 880 /* 881 * Datamove call, we need to setup the S/G list. 882 */ 883 884 cmd_info = (struct ctlfe_lun_cmd_info *) 885 io->io_hdr.port_priv; 886 887 KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE, 888 ("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < " 889 "CTL_PORT_PRIV_SIZE %d", __func__, 890 sizeof(*cmd_info), CTL_PORT_PRIV_SIZE)); 891 io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED; 892 893 /* 894 * Need to zero this, in case it has been used for 895 * a previous datamove for this particular I/O. 896 */ 897 bzero(cmd_info, sizeof(*cmd_info)); 898 scsi_status = 0; 899 900 /* 901 * Set the direction, relative to the initiator. 902 */ 903 flags &= ~CAM_DIR_MASK; 904 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 905 CTL_FLAG_DATA_IN) 906 flags |= CAM_DIR_IN; 907 else 908 flags |= CAM_DIR_OUT; 909 910 csio->cdb_len = atio->cdb_len; 911 912 flags &= ~CAM_DATA_MASK; 913 if (io->scsiio.kern_sg_entries == 0) { 914 /* No S/G list */ 915 data_ptr = io->scsiio.kern_data_ptr; 916 dxfer_len = io->scsiio.kern_data_len; 917 csio->sglist_cnt = 0; 918 919 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) 920 flags |= CAM_DATA_PADDR; 921 else 922 flags |= CAM_DATA_VADDR; 923 } else if (io->scsiio.kern_sg_entries <= 924 (sizeof(cmd_info->cam_sglist)/ 925 sizeof(cmd_info->cam_sglist[0]))) { 926 /* 927 * S/G list with physical or virtual pointers. 928 * Just populate the CAM S/G list with the 929 * pointers. 930 */ 931 int i; 932 struct ctl_sg_entry *ctl_sglist; 933 bus_dma_segment_t *cam_sglist; 934 935 ctl_sglist = (struct ctl_sg_entry *) 936 io->scsiio.kern_data_ptr; 937 cam_sglist = cmd_info->cam_sglist; 938 939 for (i = 0; i < io->scsiio.kern_sg_entries;i++){ 940 cam_sglist[i].ds_addr = 941 (bus_addr_t)ctl_sglist[i].addr; 942 cam_sglist[i].ds_len = 943 ctl_sglist[i].len; 944 } 945 csio->sglist_cnt = io->scsiio.kern_sg_entries; 946 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) 947 flags |= CAM_DATA_SG_PADDR; 948 else 949 flags &= ~CAM_DATA_SG; 950 data_ptr = (uint8_t *)cam_sglist; 951 dxfer_len = io->scsiio.kern_data_len; 952 } else { 953 /* S/G list with virtual pointers */ 954 struct ctl_sg_entry *sglist; 955 int *ti; 956 957 /* 958 * If we have more S/G list pointers than 959 * will fit in the available storage in the 960 * cmd_info structure inside the ctl_io header, 961 * then we need to send down the pointers 962 * one element at a time. 963 */ 964 965 sglist = (struct ctl_sg_entry *) 966 io->scsiio.kern_data_ptr; 967 ti = &cmd_info->cur_transfer_index; 968 data_ptr = sglist[*ti].addr; 969 dxfer_len = sglist[*ti].len; 970 csio->sglist_cnt = 0; 971 cmd_info->flags |= CTLFE_CMD_PIECEWISE; 972 (*ti)++; 973 } 974 975 io->scsiio.ext_data_filled += dxfer_len; 976 977 if (io->scsiio.ext_data_filled > 978 io->scsiio.kern_total_len) { 979 xpt_print(periph->path, "%s: tag 0x%04x " 980 "fill len %u > total %u\n", 981 __func__, io->scsiio.tag_num, 982 io->scsiio.ext_data_filled, 983 io->scsiio.kern_total_len); 984 } 985 } 986 987#ifdef CTLFEDEBUG 988 printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__, 989 (flags & CAM_SEND_STATUS) ? "done" : "datamove", 990 atio->tag_id, flags, data_ptr, dxfer_len); 991#endif 992 993 /* 994 * Valid combinations: 995 * - CAM_SEND_STATUS, SCATTER_VALID = 0, dxfer_len = 0, 996 * sglist_cnt = 0 997 * - CAM_SEND_STATUS = 0, SCATTER_VALID = 0, dxfer_len != 0, 998 * sglist_cnt = 0 999 * - CAM_SEND_STATUS = 0, SCATTER_VALID, dxfer_len != 0, 1000 * sglist_cnt != 0 1001 */ 1002#ifdef CTLFEDEBUG 1003 if (((flags & CAM_SEND_STATUS) 1004 && (((flags & CAM_SCATTER_VALID) != 0) 1005 || (dxfer_len != 0) 1006 || (csio->sglist_cnt != 0))) 1007 || (((flags & CAM_SEND_STATUS) == 0) 1008 && (dxfer_len == 0)) 1009 || ((flags & CAM_SCATTER_VALID) 1010 && (csio->sglist_cnt == 0)) 1011 || (((flags & CAM_SCATTER_VALID) == 0) 1012 && (csio->sglist_cnt != 0))) { 1013 printf("%s: tag %04x cdb %02x flags %#x dxfer_len " 1014 "%d sg %u\n", __func__, atio->tag_id, 1015 atio->cdb_io.cdb_bytes[0], flags, dxfer_len, 1016 csio->sglist_cnt); 1017 if (io != NULL) { 1018 printf("%s: tag %04x io status %#x\n", __func__, 1019 atio->tag_id, io->io_hdr.status); 1020 } else { 1021 printf("%s: tag %04x no associated io\n", 1022 __func__, atio->tag_id); 1023 } 1024 } 1025#endif 1026 cam_fill_ctio(csio, 1027 /*retries*/ 2, 1028 ctlfedone, 1029 flags, 1030 (flags & CAM_TAG_ACTION_VALID) ? 1031 MSG_SIMPLE_Q_TAG : 0, 1032 atio->tag_id, 1033 atio->init_id, 1034 scsi_status, 1035 /*data_ptr*/ data_ptr, 1036 /*dxfer_len*/ dxfer_len, 1037 /*timeout*/ 5 * 1000); 1038 start_ccb->ccb_h.ccb_atio = atio; 1039 if (((flags & CAM_SEND_STATUS) == 0) 1040 && (io != NULL)) 1041 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 1042 1043 softc->ctios_sent++; 1044 1045 xpt_action(start_ccb); 1046 1047 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1048 cam_release_devq(periph->path, 1049 /*relsim_flags*/0, 1050 /*reduction*/0, 1051 /*timeout*/0, 1052 /*getcount_only*/0); 1053 atio->ccb_h.status &= ~CAM_DEV_QFRZN; 1054 } 1055 1056 ccb_h = TAILQ_FIRST(&softc->work_queue); 1057 } 1058 /* 1059 * If we still have work to do, ask for another CCB. Otherwise, 1060 * deactivate our callout. 1061 */ 1062 if (ccb_h != NULL) 1063 xpt_schedule(periph, /*priority*/ 1); 1064 else 1065 callout_stop(&softc->dma_callout); 1066} 1067 1068static void 1069ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb) 1070{ 1071 struct ctlfe_lun_softc *softc; 1072 1073 softc = (struct ctlfe_lun_softc *)periph->softc; 1074 1075 switch (ccb->ccb_h.func_code) { 1076 case XPT_ACCEPT_TARGET_IO: 1077 softc->atios_returned++; 1078 break; 1079 case XPT_IMMEDIATE_NOTIFY: 1080 case XPT_NOTIFY_ACKNOWLEDGE: 1081 softc->inots_returned++; 1082 break; 1083 default: 1084 break; 1085 } 1086 1087 free(ccb, M_CTLFE); 1088 1089 KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: " 1090 "atios_returned %ju > atios_sent %ju", __func__, 1091 softc->atios_returned, softc->atios_sent)); 1092 KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: " 1093 "inots_returned %ju > inots_sent %ju", __func__, 1094 softc->inots_returned, softc->inots_sent)); 1095 1096 /* 1097 * If we have received all of our CCBs, we can release our 1098 * reference on the peripheral driver. It will probably go away 1099 * now. 1100 */ 1101 if ((softc->atios_returned == softc->atios_sent) 1102 && (softc->inots_returned == softc->inots_sent)) { 1103 cam_periph_release_locked(periph); 1104 } 1105} 1106 1107static int 1108ctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset) 1109{ 1110 uint64_t lba; 1111 uint32_t num_blocks, nbc; 1112 uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)? 1113 atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes; 1114 1115 nbc = offset >> 9; /* ASSUMING 512 BYTE BLOCKS */ 1116 1117 switch (cmdbyt[0]) { 1118 case READ_6: 1119 case WRITE_6: 1120 { 1121 struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt; 1122 lba = scsi_3btoul(cdb->addr); 1123 lba &= 0x1fffff; 1124 num_blocks = cdb->length; 1125 if (num_blocks == 0) 1126 num_blocks = 256; 1127 lba += nbc; 1128 num_blocks -= nbc; 1129 scsi_ulto3b(lba, cdb->addr); 1130 cdb->length = num_blocks; 1131 break; 1132 } 1133 case READ_10: 1134 case WRITE_10: 1135 { 1136 struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt; 1137 lba = scsi_4btoul(cdb->addr); 1138 num_blocks = scsi_2btoul(cdb->length); 1139 lba += nbc; 1140 num_blocks -= nbc; 1141 scsi_ulto4b(lba, cdb->addr); 1142 scsi_ulto2b(num_blocks, cdb->length); 1143 break; 1144 } 1145 case READ_12: 1146 case WRITE_12: 1147 { 1148 struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt; 1149 lba = scsi_4btoul(cdb->addr); 1150 num_blocks = scsi_4btoul(cdb->length); 1151 lba += nbc; 1152 num_blocks -= nbc; 1153 scsi_ulto4b(lba, cdb->addr); 1154 scsi_ulto4b(num_blocks, cdb->length); 1155 break; 1156 } 1157 case READ_16: 1158 case WRITE_16: 1159 { 1160 struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt; 1161 lba = scsi_8btou64(cdb->addr); 1162 num_blocks = scsi_4btoul(cdb->length); 1163 lba += nbc; 1164 num_blocks -= nbc; 1165 scsi_u64to8b(lba, cdb->addr); 1166 scsi_ulto4b(num_blocks, cdb->length); 1167 break; 1168 } 1169 default: 1170 return -1; 1171 } 1172 return (0); 1173} 1174 1175static void 1176ctlfedone(struct cam_periph *periph, union ccb *done_ccb) 1177{ 1178 struct ctlfe_lun_softc *softc; 1179 struct ctlfe_softc *bus_softc; 1180 struct ccb_accept_tio *atio = NULL; 1181 union ctl_io *io = NULL; 1182 1183#ifdef CTLFE_DEBUG 1184 printf("%s: entered, func_code = %#x, type = %#lx\n", __func__, 1185 done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type); 1186#endif 1187 1188 softc = (struct ctlfe_lun_softc *)periph->softc; 1189 bus_softc = softc->parent_softc; 1190 1191 if (done_ccb->ccb_h.ccb_type == CTLFE_CCB_WAITING) { 1192 panic("shouldn't get to the CCB waiting case!"); 1193 wakeup(&done_ccb->ccb_h.cbfcnp); 1194 return; 1195 } 1196 1197 /* 1198 * If the peripheral is invalid, ATIOs and immediate notify CCBs 1199 * need to be freed. Most of the ATIOs and INOTs that come back 1200 * will be CCBs that are being returned from the SIM as a result of 1201 * our disabling the LUN. 1202 * 1203 * Other CCB types are handled in their respective cases below. 1204 */ 1205 if (periph->flags & CAM_PERIPH_INVALID) { 1206 switch (done_ccb->ccb_h.func_code) { 1207 case XPT_ACCEPT_TARGET_IO: 1208 case XPT_IMMEDIATE_NOTIFY: 1209 case XPT_NOTIFY_ACKNOWLEDGE: 1210 ctlfe_free_ccb(periph, done_ccb); 1211 return; 1212 default: 1213 break; 1214 } 1215 1216 } 1217 switch (done_ccb->ccb_h.func_code) { 1218 case XPT_ACCEPT_TARGET_IO: { 1219 1220 atio = &done_ccb->atio; 1221 1222 softc->atios_returned++; 1223 1224 resubmit: 1225 /* 1226 * Allocate a ctl_io, pass it to CTL, and wait for the 1227 * datamove or done. 1228 */ 1229 io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref); 1230 if (io == NULL) { 1231 atio->ccb_h.flags &= ~CAM_DIR_MASK; 1232 atio->ccb_h.flags |= CAM_DIR_NONE; 1233 1234 printf("%s: ctl_alloc_io failed!\n", __func__); 1235 1236 /* 1237 * XXX KDM need to set SCSI_STATUS_BUSY, but there 1238 * is no field in the ATIO structure to do that, 1239 * and we aren't able to allocate a ctl_io here. 1240 * What to do? 1241 */ 1242 atio->sense_len = 0; 1243 done_ccb->ccb_h.io_ptr = NULL; 1244 TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h, 1245 periph_links.tqe); 1246 xpt_schedule(periph, /*priority*/ 1); 1247 break; 1248 } 1249 ctl_zero_io(io); 1250 1251 /* Save pointers on both sides */ 1252 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb; 1253 done_ccb->ccb_h.io_ptr = io; 1254 1255 /* 1256 * Only SCSI I/O comes down this path, resets, etc. come 1257 * down the immediate notify path below. 1258 */ 1259 io->io_hdr.io_type = CTL_IO_SCSI; 1260 io->io_hdr.nexus.initid.id = atio->init_id; 1261 io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port; 1262 io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id; 1263 io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun; 1264 io->scsiio.tag_num = atio->tag_id; 1265 switch (atio->tag_action) { 1266 case CAM_TAG_ACTION_NONE: 1267 io->scsiio.tag_type = CTL_TAG_UNTAGGED; 1268 break; 1269 case MSG_SIMPLE_TASK: 1270 io->scsiio.tag_type = CTL_TAG_SIMPLE; 1271 break; 1272 case MSG_HEAD_OF_QUEUE_TASK: 1273 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE; 1274 break; 1275 case MSG_ORDERED_TASK: 1276 io->scsiio.tag_type = CTL_TAG_ORDERED; 1277 break; 1278 case MSG_ACA_TASK: 1279 io->scsiio.tag_type = CTL_TAG_ACA; 1280 break; 1281 default: 1282 io->scsiio.tag_type = CTL_TAG_UNTAGGED; 1283 printf("%s: unhandled tag type %#x!!\n", __func__, 1284 atio->tag_action); 1285 break; 1286 } 1287 if (atio->cdb_len > sizeof(io->scsiio.cdb)) { 1288 printf("%s: WARNING: CDB len %d > ctl_io space %zd\n", 1289 __func__, atio->cdb_len, sizeof(io->scsiio.cdb)); 1290 } 1291 io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb)); 1292 bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb, 1293 io->scsiio.cdb_len); 1294 1295#ifdef CTLFEDEBUG 1296 printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__, 1297 (uintmax_t)io->io_hdr.nexus.initid.id, 1298 io->io_hdr.nexus.targ_port, 1299 (uintmax_t)io->io_hdr.nexus.targ_target.id, 1300 io->io_hdr.nexus.targ_lun, 1301 io->scsiio.tag_num, io->scsiio.cdb[0]); 1302#endif 1303 1304 ctl_queue(io); 1305 break; 1306 } 1307 case XPT_CONT_TARGET_IO: { 1308 int srr = 0; 1309 uint32_t srr_off = 0; 1310 1311 atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio; 1312 io = (union ctl_io *)atio->ccb_h.io_ptr; 1313 1314 softc->ctios_returned++; 1315#ifdef CTLFEDEBUG 1316 printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n", 1317 __func__, atio->tag_id, done_ccb->ccb_h.flags); 1318#endif 1319 /* 1320 * Handle SRR case were the data pointer is pushed back hack 1321 */ 1322 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV 1323 && done_ccb->csio.msg_ptr != NULL 1324 && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED 1325 && done_ccb->csio.msg_ptr[1] == 5 1326 && done_ccb->csio.msg_ptr[2] == 0) { 1327 srr = 1; 1328 srr_off = 1329 (done_ccb->csio.msg_ptr[3] << 24) 1330 | (done_ccb->csio.msg_ptr[4] << 16) 1331 | (done_ccb->csio.msg_ptr[5] << 8) 1332 | (done_ccb->csio.msg_ptr[6]); 1333 } 1334 1335 if (srr && (done_ccb->ccb_h.flags & CAM_SEND_STATUS)) { 1336 /* 1337 * If status was being sent, the back end data is now 1338 * history. Hack it up and resubmit a new command with 1339 * the CDB adjusted. If the SIM does the right thing, 1340 * all of the resid math should work. 1341 */ 1342 softc->ccbs_freed++; 1343 xpt_release_ccb(done_ccb); 1344 ctl_free_io(io); 1345 if (ctlfe_adjust_cdb(atio, srr_off) == 0) { 1346 done_ccb = (union ccb *)atio; 1347 goto resubmit; 1348 } 1349 /* 1350 * Fall through to doom.... 1351 */ 1352 } else if (srr) { 1353 /* 1354 * If we have an srr and we're still sending data, we 1355 * should be able to adjust offsets and cycle again. 1356 */ 1357 io->scsiio.kern_rel_offset = 1358 io->scsiio.ext_data_filled = srr_off; 1359 io->scsiio.ext_data_len = io->scsiio.kern_total_len - 1360 io->scsiio.kern_rel_offset; 1361 softc->ccbs_freed++; 1362 io->scsiio.io_hdr.status = CTL_STATUS_NONE; 1363 xpt_release_ccb(done_ccb); 1364 TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h, 1365 periph_links.tqe); 1366 xpt_schedule(periph, /*priority*/ 1); 1367 return; 1368 } 1369 1370 /* 1371 * If we were sending status back to the initiator, free up 1372 * resources. If we were doing a datamove, call the 1373 * datamove done routine. 1374 */ 1375 if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) { 1376 softc->ccbs_freed++; 1377 xpt_release_ccb(done_ccb); 1378 ctl_free_io(io); 1379 /* 1380 * For a wildcard attachment, commands can come in 1381 * with a specific target/lun. Reset the target 1382 * and LUN fields back to the wildcard values before 1383 * we send them back down to the SIM. The SIM has 1384 * a wildcard LUN enabled, not whatever target/lun 1385 * these happened to be. 1386 */ 1387 if (softc->flags & CTLFE_LUN_WILDCARD) { 1388 atio->ccb_h.target_id = CAM_TARGET_WILDCARD; 1389 atio->ccb_h.target_lun = CAM_LUN_WILDCARD; 1390 } 1391 if (periph->flags & CAM_PERIPH_INVALID) { 1392 ctlfe_free_ccb(periph, (union ccb *)atio); 1393 return; 1394 } else { 1395 xpt_action((union ccb *)atio); 1396 softc->atios_sent++; 1397 } 1398 } else { 1399 struct ctlfe_lun_cmd_info *cmd_info; 1400 struct ccb_scsiio *csio; 1401 1402 csio = &done_ccb->csio; 1403 cmd_info = (struct ctlfe_lun_cmd_info *) 1404 io->io_hdr.port_priv; 1405 1406 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1407 1408 io->scsiio.ext_data_len += csio->dxfer_len; 1409 if (io->scsiio.ext_data_len > 1410 io->scsiio.kern_total_len) { 1411 xpt_print(periph->path, "%s: tag 0x%04x " 1412 "done len %u > total %u sent %u\n", 1413 __func__, io->scsiio.tag_num, 1414 io->scsiio.ext_data_len, 1415 io->scsiio.kern_total_len, 1416 io->scsiio.ext_data_filled); 1417 } 1418 /* 1419 * Translate CAM status to CTL status. Success 1420 * does not change the overall, ctl_io status. In 1421 * that case we just set port_status to 0. If we 1422 * have a failure, though, set a data phase error 1423 * for the overall ctl_io. 1424 */ 1425 switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) { 1426 case CAM_REQ_CMP: 1427 io->io_hdr.port_status = 0; 1428 break; 1429 default: 1430 /* 1431 * XXX KDM we probably need to figure out a 1432 * standard set of errors that the SIM 1433 * drivers should return in the event of a 1434 * data transfer failure. A data phase 1435 * error will at least point the user to a 1436 * data transfer error of some sort. 1437 * Hopefully the SIM printed out some 1438 * additional information to give the user 1439 * a clue what happened. 1440 */ 1441 io->io_hdr.port_status = 0xbad1; 1442 ctl_set_data_phase_error(&io->scsiio); 1443 /* 1444 * XXX KDM figure out residual. 1445 */ 1446 break; 1447 } 1448 /* 1449 * If we had to break this S/G list into multiple 1450 * pieces, figure out where we are in the list, and 1451 * continue sending pieces if necessary. 1452 */ 1453 if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) 1454 && (io->io_hdr.port_status == 0) 1455 && (cmd_info->cur_transfer_index < 1456 io->scsiio.kern_sg_entries)) { 1457 struct ctl_sg_entry *sglist; 1458 ccb_flags flags; 1459 uint8_t scsi_status; 1460 uint8_t *data_ptr; 1461 uint32_t dxfer_len; 1462 int *ti; 1463 1464 sglist = (struct ctl_sg_entry *) 1465 io->scsiio.kern_data_ptr; 1466 ti = &cmd_info->cur_transfer_index; 1467 flags = atio->ccb_h.flags & 1468 (CAM_DIS_DISCONNECT| 1469 CAM_TAG_ACTION_VALID| 1470 CAM_DIR_MASK); 1471 1472 /* 1473 * Set the direction, relative to the initiator. 1474 */ 1475 flags &= ~CAM_DIR_MASK; 1476 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1477 CTL_FLAG_DATA_IN) 1478 flags |= CAM_DIR_IN; 1479 else 1480 flags |= CAM_DIR_OUT; 1481 1482 data_ptr = sglist[*ti].addr; 1483 dxfer_len = sglist[*ti].len; 1484 (*ti)++; 1485 1486 scsi_status = 0; 1487 1488 if (((flags & CAM_SEND_STATUS) == 0) 1489 && (dxfer_len == 0)) { 1490 printf("%s: tag %04x no status or " 1491 "len cdb = %02x\n", __func__, 1492 atio->tag_id, 1493 atio->cdb_io.cdb_bytes[0]); 1494 printf("%s: tag %04x io status %#x\n", 1495 __func__, atio->tag_id, 1496 io->io_hdr.status); 1497 } 1498 1499 cam_fill_ctio(csio, 1500 /*retries*/ 2, 1501 ctlfedone, 1502 flags, 1503 (flags & CAM_TAG_ACTION_VALID) ? 1504 MSG_SIMPLE_Q_TAG : 0, 1505 atio->tag_id, 1506 atio->init_id, 1507 scsi_status, 1508 /*data_ptr*/ data_ptr, 1509 /*dxfer_len*/ dxfer_len, 1510 /*timeout*/ 5 * 1000); 1511 1512 csio->resid = 0; 1513 csio->ccb_h.ccb_atio = atio; 1514 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 1515 softc->ctios_sent++; 1516 xpt_action((union ccb *)csio); 1517 } else { 1518 /* 1519 * Release the CTIO. The ATIO will be sent back 1520 * down to the SIM once we send status. 1521 */ 1522 softc->ccbs_freed++; 1523 xpt_release_ccb(done_ccb); 1524 1525 /* Call the backend move done callback */ 1526 io->scsiio.be_move_done(io); 1527 } 1528 } 1529 break; 1530 } 1531 case XPT_IMMEDIATE_NOTIFY: { 1532 union ctl_io *io; 1533 struct ccb_immediate_notify *inot; 1534 cam_status status; 1535 int frozen; 1536 1537 inot = &done_ccb->cin1; 1538 1539 softc->inots_returned++; 1540 1541 frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 1542 1543 printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x " 1544 "seq %#x\n", __func__, inot->ccb_h.status, 1545 inot->tag_id, inot->seq_id); 1546 1547 io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref); 1548 if (io != NULL) { 1549 int send_ctl_io; 1550 1551 send_ctl_io = 1; 1552 1553 ctl_zero_io(io); 1554 io->io_hdr.io_type = CTL_IO_TASK; 1555 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb; 1556 inot->ccb_h.io_ptr = io; 1557 io->io_hdr.nexus.initid.id = inot->initiator_id; 1558 io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port; 1559 io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id; 1560 io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun; 1561 /* XXX KDM should this be the tag_id? */ 1562 io->taskio.tag_num = inot->seq_id; 1563 1564 status = inot->ccb_h.status & CAM_STATUS_MASK; 1565 switch (status) { 1566 case CAM_SCSI_BUS_RESET: 1567 io->taskio.task_action = CTL_TASK_BUS_RESET; 1568 break; 1569 case CAM_BDR_SENT: 1570 io->taskio.task_action = CTL_TASK_TARGET_RESET; 1571 break; 1572 case CAM_MESSAGE_RECV: 1573 switch (inot->arg) { 1574 case MSG_ABORT_TASK_SET: 1575 /* 1576 * XXX KDM this isn't currently 1577 * supported by CTL. It ends up 1578 * being a no-op. 1579 */ 1580 io->taskio.task_action = 1581 CTL_TASK_ABORT_TASK_SET; 1582 break; 1583 case MSG_TARGET_RESET: 1584 io->taskio.task_action = 1585 CTL_TASK_TARGET_RESET; 1586 break; 1587 case MSG_ABORT_TASK: 1588 io->taskio.task_action = 1589 CTL_TASK_ABORT_TASK; 1590 break; 1591 case MSG_LOGICAL_UNIT_RESET: 1592 io->taskio.task_action = 1593 CTL_TASK_LUN_RESET; 1594 break; 1595 case MSG_CLEAR_TASK_SET: 1596 /* 1597 * XXX KDM this isn't currently 1598 * supported by CTL. It ends up 1599 * being a no-op. 1600 */ 1601 io->taskio.task_action = 1602 CTL_TASK_CLEAR_TASK_SET; 1603 break; 1604 case MSG_CLEAR_ACA: 1605 io->taskio.task_action = 1606 CTL_TASK_CLEAR_ACA; 1607 break; 1608 case MSG_NOOP: 1609 send_ctl_io = 0; 1610 break; 1611 default: 1612 xpt_print(periph->path, "%s: " 1613 "unsupported message 0x%x\n", 1614 __func__, inot->arg); 1615 send_ctl_io = 0; 1616 break; 1617 } 1618 break; 1619 case CAM_REQ_ABORTED: 1620 /* 1621 * This request was sent back by the driver. 1622 * XXX KDM what do we do here? 1623 */ 1624 send_ctl_io = 0; 1625 break; 1626 case CAM_REQ_INVALID: 1627 case CAM_PROVIDE_FAIL: 1628 default: 1629 /* 1630 * We should only get here if we're talking 1631 * to a talking to a SIM that is target 1632 * capable but supports the old API. In 1633 * that case, we need to just free the CCB. 1634 * If we actually send a notify acknowledge, 1635 * it will send that back with an error as 1636 * well. 1637 */ 1638 1639 if ((status != CAM_REQ_INVALID) 1640 && (status != CAM_PROVIDE_FAIL)) 1641 xpt_print(periph->path, "%s: " 1642 "unsupported CAM status " 1643 "0x%x\n", __func__, status); 1644 1645 ctl_free_io(io); 1646 ctlfe_free_ccb(periph, done_ccb); 1647 1648 return; 1649 } 1650 if (send_ctl_io != 0) { 1651 ctl_queue(io); 1652 } else { 1653 ctl_free_io(io); 1654 done_ccb->ccb_h.status = CAM_REQ_INPROG; 1655 done_ccb->ccb_h.func_code = 1656 XPT_NOTIFY_ACKNOWLEDGE; 1657 xpt_action(done_ccb); 1658 } 1659 } else { 1660 xpt_print(periph->path, "%s: could not allocate " 1661 "ctl_io for immediate notify!\n", __func__); 1662 /* requeue this to the adapter */ 1663 done_ccb->ccb_h.status = CAM_REQ_INPROG; 1664 done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE; 1665 xpt_action(done_ccb); 1666 } 1667 1668 if (frozen != 0) { 1669 cam_release_devq(periph->path, 1670 /*relsim_flags*/ 0, 1671 /*opening reduction*/ 0, 1672 /*timeout*/ 0, 1673 /*getcount_only*/ 0); 1674 } 1675 break; 1676 } 1677 case XPT_NOTIFY_ACKNOWLEDGE: 1678 /* 1679 * Queue this back down to the SIM as an immediate notify. 1680 */ 1681 done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY; 1682 xpt_action(done_ccb); 1683 softc->inots_sent++; 1684 break; 1685 case XPT_ABORT: 1686 /* 1687 * XPT_ABORT is an immediate CCB, we shouldn't get here. 1688 */ 1689 panic("%s: XPT_ABORT CCB returned!", __func__); 1690 break; 1691 case XPT_SET_SIM_KNOB: 1692 case XPT_GET_SIM_KNOB: 1693 break; 1694 default: 1695 panic("%s: unexpected CCB type %#x", __func__, 1696 done_ccb->ccb_h.func_code); 1697 break; 1698 } 1699} 1700 1701static void 1702ctlfe_onoffline(void *arg, int online) 1703{ 1704 struct ctlfe_softc *bus_softc; 1705 union ccb *ccb; 1706 cam_status status; 1707 struct cam_path *path; 1708 struct cam_sim *sim; 1709 int set_wwnn; 1710 1711 bus_softc = (struct ctlfe_softc *)arg; 1712 1713 set_wwnn = 0; 1714 1715 sim = bus_softc->sim; 1716 1717 mtx_assert(sim->mtx, MA_OWNED); 1718 1719 status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id, 1720 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 1721 if (status != CAM_REQ_CMP) { 1722 printf("%s: unable to create path!\n", __func__); 1723 return; 1724 } 1725 ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_NOWAIT | M_ZERO); 1726 if (ccb == NULL) { 1727 printf("%s: unable to malloc CCB!\n", __func__); 1728 xpt_free_path(path); 1729 return; 1730 } 1731 xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE); 1732 1733 /* 1734 * Copan WWN format: 1735 * 1736 * Bits 63-60: 0x5 NAA, IEEE registered name 1737 * Bits 59-36: 0x000ED5 IEEE Company name assigned to Copan 1738 * Bits 35-12: Copan SSN (Sequential Serial Number) 1739 * Bits 11-8: Type of port: 1740 * 1 == N-Port 1741 * 2 == F-Port 1742 * 3 == NL-Port 1743 * Bits 7-0: 0 == Node Name, >0 == Port Number 1744 */ 1745 1746 if (online != 0) { 1747 1748 ccb->ccb_h.func_code = XPT_GET_SIM_KNOB; 1749 1750 1751 xpt_action(ccb); 1752 1753 1754 if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){ 1755#ifdef RANDOM_WWNN 1756 uint64_t random_bits; 1757#endif 1758 1759 printf("%s: %s current WWNN %#jx\n", __func__, 1760 bus_softc->port_name, 1761 ccb->knob.xport_specific.fc.wwnn); 1762 printf("%s: %s current WWPN %#jx\n", __func__, 1763 bus_softc->port_name, 1764 ccb->knob.xport_specific.fc.wwpn); 1765 1766#ifdef RANDOM_WWNN 1767 arc4rand(&random_bits, sizeof(random_bits), 0); 1768#endif 1769 1770 /* 1771 * XXX KDM this is a bit of a kludge for now. We 1772 * take the current WWNN/WWPN from the card, and 1773 * replace the company identifier and the NL-Port 1774 * indicator and the port number (for the WWPN). 1775 * This should be replaced later with ddb_GetWWNN, 1776 * or possibly a more centralized scheme. (It 1777 * would be nice to have the WWNN/WWPN for each 1778 * port stored in the ctl_frontend structure.) 1779 */ 1780#ifdef RANDOM_WWNN 1781 ccb->knob.xport_specific.fc.wwnn = 1782 (random_bits & 1783 0x0000000fffffff00ULL) | 1784 /* Company ID */ 0x5000ED5000000000ULL | 1785 /* NL-Port */ 0x0300; 1786 ccb->knob.xport_specific.fc.wwpn = 1787 (random_bits & 1788 0x0000000fffffff00ULL) | 1789 /* Company ID */ 0x5000ED5000000000ULL | 1790 /* NL-Port */ 0x3000 | 1791 /* Port Num */ (bus_softc->fe.targ_port & 0xff); 1792 1793 /* 1794 * This is a bit of an API break/reversal, but if 1795 * we're doing the random WWNN that's a little 1796 * different anyway. So record what we're actually 1797 * using with the frontend code so it's reported 1798 * accurately. 1799 */ 1800 bus_softc->fe.wwnn = 1801 ccb->knob.xport_specific.fc.wwnn; 1802 bus_softc->fe.wwpn = 1803 ccb->knob.xport_specific.fc.wwpn; 1804 set_wwnn = 1; 1805#else /* RANDOM_WWNN */ 1806 /* 1807 * If the user has specified a WWNN/WWPN, send them 1808 * down to the SIM. Otherwise, record what the SIM 1809 * has reported. 1810 */ 1811 if ((bus_softc->fe.wwnn != 0) 1812 && (bus_softc->fe.wwpn != 0)) { 1813 ccb->knob.xport_specific.fc.wwnn = 1814 bus_softc->fe.wwnn; 1815 ccb->knob.xport_specific.fc.wwpn = 1816 bus_softc->fe.wwpn; 1817 set_wwnn = 1; 1818 } else { 1819 bus_softc->fe.wwnn = 1820 ccb->knob.xport_specific.fc.wwnn; 1821 bus_softc->fe.wwpn = 1822 ccb->knob.xport_specific.fc.wwpn; 1823 } 1824#endif /* RANDOM_WWNN */ 1825 1826 1827 if (set_wwnn != 0) { 1828 printf("%s: %s new WWNN %#jx\n", __func__, 1829 bus_softc->port_name, 1830 ccb->knob.xport_specific.fc.wwnn); 1831 printf("%s: %s new WWPN %#jx\n", __func__, 1832 bus_softc->port_name, 1833 ccb->knob.xport_specific.fc.wwpn); 1834 } 1835 } else { 1836 printf("%s: %s has no valid WWNN/WWPN\n", __func__, 1837 bus_softc->port_name); 1838 } 1839 } 1840 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB; 1841 ccb->knob.xport_specific.valid = KNOB_VALID_ROLE; 1842 if (set_wwnn != 0) 1843 ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS; 1844 1845 if (online != 0) 1846 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET; 1847 else 1848 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE; 1849 1850 xpt_action(ccb); 1851 1852 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1853 printf("%s: SIM %s (path id %d) target %s failed with " 1854 "status %#x\n", 1855 __func__, bus_softc->port_name, bus_softc->path_id, 1856 (online != 0) ? "enable" : "disable", 1857 ccb->ccb_h.status); 1858 } else { 1859 printf("%s: SIM %s (path id %d) target %s succeeded\n", 1860 __func__, bus_softc->port_name, bus_softc->path_id, 1861 (online != 0) ? "enable" : "disable"); 1862 } 1863 1864 xpt_free_path(path); 1865 1866 free(ccb, M_TEMP); 1867 1868 return; 1869} 1870 1871static void 1872ctlfe_online(void *arg) 1873{ 1874 struct ctlfe_softc *bus_softc; 1875 struct cam_path *path; 1876 cam_status status; 1877 struct ctlfe_lun_softc *lun_softc; 1878 struct cam_sim *sim; 1879 1880 bus_softc = (struct ctlfe_softc *)arg; 1881 sim = bus_softc->sim; 1882 1883 CAM_SIM_LOCK(sim); 1884 1885 /* 1886 * Create the wildcard LUN before bringing the port online. 1887 */ 1888 status = xpt_create_path(&path, /*periph*/ NULL, 1889 bus_softc->path_id, CAM_TARGET_WILDCARD, 1890 CAM_LUN_WILDCARD); 1891 if (status != CAM_REQ_CMP) { 1892 printf("%s: unable to create path for wildcard periph\n", 1893 __func__); 1894 CAM_SIM_UNLOCK(sim); 1895 return; 1896 } 1897 1898 lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, 1899 M_NOWAIT | M_ZERO); 1900 if (lun_softc == NULL) { 1901 xpt_print(path, "%s: unable to allocate softc for " 1902 "wildcard periph\n", __func__); 1903 xpt_free_path(path); 1904 CAM_SIM_UNLOCK(sim); 1905 return; 1906 } 1907 1908 lun_softc->parent_softc = bus_softc; 1909 lun_softc->flags |= CTLFE_LUN_WILDCARD; 1910 1911 STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, lun_softc, links); 1912 1913 1914 status = cam_periph_alloc(ctlferegister, 1915 ctlfeoninvalidate, 1916 ctlfecleanup, 1917 ctlfestart, 1918 "ctl", 1919 CAM_PERIPH_BIO, 1920 path, 1921 ctlfeasync, 1922 0, 1923 lun_softc); 1924 1925 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1926 const struct cam_status_entry *entry; 1927 1928 entry = cam_fetch_status_entry(status); 1929 1930 printf("%s: CAM error %s (%#x) returned from " 1931 "cam_periph_alloc()\n", __func__, (entry != NULL) ? 1932 entry->status_text : "Unknown", status); 1933 } 1934 1935 xpt_free_path(path); 1936 1937 ctlfe_onoffline(arg, /*online*/ 1); 1938 1939 CAM_SIM_UNLOCK(sim); 1940} 1941 1942static void 1943ctlfe_offline(void *arg) 1944{ 1945 struct ctlfe_softc *bus_softc; 1946 struct cam_path *path; 1947 cam_status status; 1948 struct cam_periph *periph; 1949 struct cam_sim *sim; 1950 1951 bus_softc = (struct ctlfe_softc *)arg; 1952 sim = bus_softc->sim; 1953 1954 CAM_SIM_LOCK(sim); 1955 1956 ctlfe_onoffline(arg, /*online*/ 0); 1957 1958 /* 1959 * Disable the wildcard LUN for this port now that we have taken 1960 * the port offline. 1961 */ 1962 status = xpt_create_path(&path, /*periph*/ NULL, 1963 bus_softc->path_id, CAM_TARGET_WILDCARD, 1964 CAM_LUN_WILDCARD); 1965 if (status != CAM_REQ_CMP) { 1966 CAM_SIM_UNLOCK(sim); 1967 printf("%s: unable to create path for wildcard periph\n", 1968 __func__); 1969 return; 1970 } 1971 1972 1973 if ((periph = cam_periph_find(path, "ctl")) != NULL) 1974 cam_periph_invalidate(periph); 1975 1976 xpt_free_path(path); 1977 1978 CAM_SIM_UNLOCK(sim); 1979} 1980 1981static int 1982ctlfe_targ_enable(void *arg, struct ctl_id targ_id) 1983{ 1984 return (0); 1985} 1986 1987static int 1988ctlfe_targ_disable(void *arg, struct ctl_id targ_id) 1989{ 1990 return (0); 1991} 1992 1993/* 1994 * This will get called to enable a LUN on every bus that is attached to 1995 * CTL. So we only need to create a path/periph for this particular bus. 1996 */ 1997static int 1998ctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1999{ 2000 struct ctlfe_softc *bus_softc; 2001 struct ctlfe_lun_softc *softc; 2002 struct cam_path *path; 2003 struct cam_periph *periph; 2004 struct cam_sim *sim; 2005 cam_status status; 2006 2007 bus_softc = (struct ctlfe_softc *)arg; 2008 sim = bus_softc->sim; 2009 2010 status = xpt_create_path_unlocked(&path, /*periph*/ NULL, 2011 bus_softc->path_id, 2012 targ_id.id, lun_id); 2013 /* XXX KDM need some way to return status to CTL here? */ 2014 if (status != CAM_REQ_CMP) { 2015 printf("%s: could not create path, status %#x\n", __func__, 2016 status); 2017 return (1); 2018 } 2019 2020 softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO); 2021 CAM_SIM_LOCK(sim); 2022 periph = cam_periph_find(path, "ctl"); 2023 if (periph != NULL) { 2024 /* We've already got a periph, no need to alloc a new one. */ 2025 xpt_free_path(path); 2026 free(softc, M_CTLFE); 2027 CAM_SIM_UNLOCK(sim); 2028 return (0); 2029 } 2030 2031 softc->parent_softc = bus_softc; 2032 STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links); 2033 2034 status = cam_periph_alloc(ctlferegister, 2035 ctlfeoninvalidate, 2036 ctlfecleanup, 2037 ctlfestart, 2038 "ctl", 2039 CAM_PERIPH_BIO, 2040 path, 2041 ctlfeasync, 2042 0, 2043 softc); 2044 2045 xpt_free_path(path); 2046 2047 CAM_SIM_UNLOCK(sim); 2048 2049 return (0); 2050} 2051 2052/* 2053 * This will get called when the user removes a LUN to disable that LUN 2054 * on every bus that is attached to CTL. 2055 */ 2056static int 2057ctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 2058{ 2059 struct ctlfe_softc *softc; 2060 struct ctlfe_lun_softc *lun_softc; 2061 struct cam_sim *sim; 2062 2063 softc = (struct ctlfe_softc *)arg; 2064 sim = softc->sim; 2065 2066 CAM_SIM_LOCK(sim); 2067 STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) { 2068 struct cam_path *path; 2069 2070 path = lun_softc->periph->path; 2071 2072 if ((xpt_path_target_id(path) == targ_id.id) 2073 && (xpt_path_lun_id(path) == lun_id)) { 2074 break; 2075 } 2076 } 2077 if (lun_softc == NULL) { 2078 CAM_SIM_UNLOCK(sim); 2079 printf("%s: can't find target %d lun %d\n", __func__, 2080 targ_id.id, lun_id); 2081 return (1); 2082 } 2083 2084 cam_periph_invalidate(lun_softc->periph); 2085 2086 CAM_SIM_UNLOCK(sim); 2087 2088 return (0); 2089} 2090 2091static void 2092ctlfe_dump_sim(struct cam_sim *sim) 2093{ 2094 int i; 2095 2096 printf("%s%d: max tagged openings: %d, max dev openings: %d\n", 2097 sim->sim_name, sim->unit_number, 2098 sim->max_tagged_dev_openings, sim->max_dev_openings); 2099 printf("%s%d: max_ccbs: %u, ccb_count: %u\n", 2100 sim->sim_name, sim->unit_number, 2101 sim->max_ccbs, sim->ccb_count); 2102 printf("%s%d: ccb_freeq is %sempty\n", 2103 sim->sim_name, sim->unit_number, 2104 (SLIST_FIRST(&sim->ccb_freeq) == NULL) ? "" : "NOT "); 2105 printf("%s%d: alloc_queue.entries %d, alloc_openings %d\n", 2106 sim->sim_name, sim->unit_number, 2107 sim->devq->alloc_queue.entries, sim->devq->alloc_openings); 2108 printf("%s%d: qfrozen_cnt:", sim->sim_name, sim->unit_number); 2109 for (i = 0; i < CAM_RL_VALUES; i++) { 2110 printf("%s%u", (i != 0) ? ":" : "", 2111 sim->devq->alloc_queue.qfrozen_cnt[i]); 2112 } 2113 printf("\n"); 2114} 2115 2116/* 2117 * Assumes that the SIM lock is held. 2118 */ 2119static void 2120ctlfe_dump_queue(struct ctlfe_lun_softc *softc) 2121{ 2122 struct ccb_hdr *hdr; 2123 struct cam_periph *periph; 2124 int num_items; 2125 2126 periph = softc->periph; 2127 num_items = 0; 2128 2129 TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) { 2130 union ctl_io *io; 2131 2132 io = hdr->io_ptr; 2133 2134 num_items++; 2135 2136 /* 2137 * This can happen when we get an ATIO but can't allocate 2138 * a ctl_io. See the XPT_ACCEPT_TARGET_IO case in ctlfedone(). 2139 */ 2140 if (io == NULL) { 2141 struct ccb_scsiio *csio; 2142 2143 csio = (struct ccb_scsiio *)hdr; 2144 2145 xpt_print(periph->path, "CCB %#x ctl_io allocation " 2146 "failed\n", csio->tag_id); 2147 continue; 2148 } 2149 2150 /* 2151 * Only regular SCSI I/O is put on the work 2152 * queue, so we can print sense here. There may be no 2153 * sense if it's no the queue for a DMA, but this serves to 2154 * print out the CCB as well. 2155 * 2156 * XXX KDM switch this over to scsi_sense_print() when 2157 * CTL is merged in with CAM. 2158 */ 2159 ctl_io_error_print(io, NULL); 2160 2161 /* 2162 * We're sending status back to the 2163 * initiator, so we're on the queue waiting 2164 * for a CTIO to do that. 2165 */ 2166 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 2167 continue; 2168 2169 /* 2170 * Otherwise, we're on the queue waiting to 2171 * do a data transfer. 2172 */ 2173 xpt_print(periph->path, "Total %u, Current %u, Resid %u\n", 2174 io->scsiio.kern_total_len, io->scsiio.kern_data_len, 2175 io->scsiio.kern_data_resid); 2176 } 2177 2178 xpt_print(periph->path, "%d requests total waiting for CCBs\n", 2179 num_items); 2180 xpt_print(periph->path, "%ju CCBs oustanding (%ju allocated, %ju " 2181 "freed)\n", (uintmax_t)(softc->ccbs_alloced - 2182 softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced, 2183 (uintmax_t)softc->ccbs_freed); 2184 xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju " 2185 "returned\n", (uintmax_t)(softc->ctios_sent - 2186 softc->ctios_returned), softc->ctios_sent, 2187 softc->ctios_returned); 2188} 2189 2190/* 2191 * This function is called when we fail to get a CCB for a DMA or status return 2192 * to the initiator within the specified time period. 2193 * 2194 * The callout code should insure that we hold the sim mutex here. 2195 */ 2196static void 2197ctlfe_dma_timeout(void *arg) 2198{ 2199 struct ctlfe_lun_softc *softc; 2200 struct cam_periph *periph; 2201 struct cam_sim *sim; 2202 int num_queued; 2203 2204 softc = (struct ctlfe_lun_softc *)arg; 2205 periph = softc->periph; 2206 sim = xpt_path_sim(periph->path); 2207 num_queued = 0; 2208 2209 /* 2210 * Nothing to do... 2211 */ 2212 if (TAILQ_FIRST(&softc->work_queue) == NULL) { 2213 xpt_print(periph->path, "TIMEOUT triggered after %d " 2214 "seconds, but nothing on work queue??\n", 2215 CTLFE_DMA_TIMEOUT); 2216 return; 2217 } 2218 2219 xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to " 2220 "start\n", CTLFE_DMA_TIMEOUT); 2221 2222 ctlfe_dump_queue(softc); 2223 2224 ctlfe_dump_sim(sim); 2225 2226 xpt_print(periph->path, "calling xpt_schedule() to attempt to " 2227 "unstick our queue\n"); 2228 2229 xpt_schedule(periph, /*priority*/ 1); 2230 2231 xpt_print(periph->path, "xpt_schedule() call complete\n"); 2232} 2233 2234/* 2235 * Datamove/done routine called by CTL. Put ourselves on the queue to 2236 * receive a CCB from CAM so we can queue the continue I/O request down 2237 * to the adapter. 2238 */ 2239static void 2240ctlfe_datamove_done(union ctl_io *io) 2241{ 2242 union ccb *ccb; 2243 struct cam_sim *sim; 2244 struct cam_periph *periph; 2245 struct ctlfe_lun_softc *softc; 2246 2247 ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2248 2249 sim = xpt_path_sim(ccb->ccb_h.path); 2250 2251 CAM_SIM_LOCK(sim); 2252 2253 periph = xpt_path_periph(ccb->ccb_h.path); 2254 2255 softc = (struct ctlfe_lun_softc *)periph->softc; 2256 2257 if (io->io_hdr.io_type == CTL_IO_TASK) { 2258 /* 2259 * Task management commands don't require any further 2260 * communication back to the adapter. Requeue the CCB 2261 * to the adapter, and free the CTL I/O. 2262 */ 2263 xpt_print(ccb->ccb_h.path, "%s: returning task I/O " 2264 "tag %#x seq %#x\n", __func__, 2265 ccb->cin1.tag_id, ccb->cin1.seq_id); 2266 /* 2267 * Send the notify acknowledge down to the SIM, to let it 2268 * know we processed the task management command. 2269 */ 2270 ccb->ccb_h.status = CAM_REQ_INPROG; 2271 ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE; 2272 xpt_action(ccb); 2273 ctl_free_io(io); 2274 } else { 2275 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 2276 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED; 2277 else 2278 io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED; 2279 2280 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, 2281 periph_links.tqe); 2282 2283 /* 2284 * Reset the timeout for our latest active DMA. 2285 */ 2286 callout_reset(&softc->dma_callout, 2287 CTLFE_DMA_TIMEOUT * hz, 2288 ctlfe_dma_timeout, softc); 2289 /* 2290 * Ask for the CAM transport layer to send us a CCB to do 2291 * the DMA or send status, unless ctlfe_dma_enabled is set 2292 * to 0. 2293 */ 2294 if (ctlfe_dma_enabled != 0) 2295 xpt_schedule(periph, /*priority*/ 1); 2296 } 2297 2298 CAM_SIM_UNLOCK(sim); 2299} 2300 2301static void 2302ctlfe_dump(void) 2303{ 2304 struct ctlfe_softc *bus_softc; 2305 2306 STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) { 2307 struct ctlfe_lun_softc *lun_softc; 2308 2309 ctlfe_dump_sim(bus_softc->sim); 2310 2311 STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) { 2312 ctlfe_dump_queue(lun_softc); 2313 } 2314 } 2315} 2316