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