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