scsi_ctl.c revision 316139
1/*- 2 * Copyright (c) 2008, 2009 Silicon Graphics International Corp. 3 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification. 12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 13 * substantially similar to the "NO WARRANTY" disclaimer below 14 * ("Disclaimer") and any redistribution must be conditioned upon 15 * including a substantially similar Disclaimer requirement for further 16 * binary redistribution. 17 * 18 * NO WARRANTY 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGES. 30 * 31 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $ 32 */ 33/* 34 * Peripheral driver interface between CAM and CTL (CAM Target Layer). 35 * 36 * Author: Ken Merry <ken@FreeBSD.org> 37 */ 38 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: stable/11/sys/cam/ctl/scsi_ctl.c 316139 2017-03-29 15:25:34Z mav $"); 41 42#include <sys/param.h> 43#include <sys/queue.h> 44#include <sys/systm.h> 45#include <sys/kernel.h> 46#include <sys/lock.h> 47#include <sys/mutex.h> 48#include <sys/condvar.h> 49#include <sys/malloc.h> 50#include <sys/bus.h> 51#include <sys/endian.h> 52#include <sys/sbuf.h> 53#include <sys/sysctl.h> 54#include <sys/types.h> 55#include <sys/systm.h> 56#include <sys/taskqueue.h> 57#include <machine/bus.h> 58 59#include <cam/cam.h> 60#include <cam/cam_ccb.h> 61#include <cam/cam_periph.h> 62#include <cam/cam_queue.h> 63#include <cam/cam_xpt_periph.h> 64#include <cam/cam_debug.h> 65#include <cam/cam_sim.h> 66#include <cam/cam_xpt.h> 67 68#include <cam/scsi/scsi_all.h> 69#include <cam/scsi/scsi_message.h> 70 71#include <cam/ctl/ctl_io.h> 72#include <cam/ctl/ctl.h> 73#include <cam/ctl/ctl_frontend.h> 74#include <cam/ctl/ctl_util.h> 75#include <cam/ctl/ctl_error.h> 76 77struct ctlfe_softc { 78 struct ctl_port port; 79 path_id_t path_id; 80 target_id_t target_id; 81 uint32_t hba_misc; 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"; 93 94typedef enum { 95 CTLFE_LUN_NONE = 0x00, 96 CTLFE_LUN_WILDCARD = 0x01 97} ctlfe_lun_flags; 98 99struct ctlfe_lun_softc { 100 struct ctlfe_softc *parent_softc; 101 struct cam_periph *periph; 102 ctlfe_lun_flags flags; 103 int ctios_sent; /* Number of active CTIOs */ 104 int refcount; /* Number of active xpt_action() */ 105 int atios_alloced; /* Number of ATIOs not freed */ 106 int inots_alloced; /* Number of INOTs not freed */ 107 struct task refdrain_task; 108 STAILQ_HEAD(, ccb_hdr) work_queue; 109 LIST_HEAD(, ccb_hdr) atio_list; /* List of ATIOs queued to SIM. */ 110 LIST_HEAD(, ccb_hdr) inot_list; /* List of INOTs queued to SIM. */ 111 STAILQ_ENTRY(ctlfe_lun_softc) links; 112}; 113 114typedef enum { 115 CTLFE_CMD_NONE = 0x00, 116 CTLFE_CMD_PIECEWISE = 0x01 117} ctlfe_cmd_flags; 118 119struct ctlfe_cmd_info { 120 int cur_transfer_index; 121 size_t cur_transfer_off; 122 ctlfe_cmd_flags flags; 123 /* 124 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16 125 * bytes on amd64. So with 32 elements, this is 256 bytes on 126 * i386 and 512 bytes on amd64. 127 */ 128#define CTLFE_MAX_SEGS 32 129 bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS]; 130}; 131 132/* 133 * When we register the adapter/bus, request that this many ctl_ios be 134 * allocated. This should be the maximum supported by the adapter, but we 135 * currently don't have a way to get that back from the path inquiry. 136 * XXX KDM add that to the path inquiry. 137 */ 138#define CTLFE_REQ_CTL_IO 4096 139/* 140 * Number of Accept Target I/O CCBs to allocate and queue down to the 141 * adapter per LUN. 142 * XXX KDM should this be controlled by CTL? 143 */ 144#define CTLFE_ATIO_PER_LUN 1024 145/* 146 * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to 147 * allocate and queue down to the adapter per LUN. 148 * XXX KDM should this be controlled by CTL? 149 */ 150#define CTLFE_IN_PER_LUN 1024 151 152/* 153 * Timeout (in seconds) on CTIO CCB doing DMA or sending status 154 */ 155#define CTLFE_TIMEOUT 5 156 157/* 158 * Turn this on to enable extra debugging prints. 159 */ 160#if 0 161#define CTLFE_DEBUG 162#endif 163 164MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface"); 165 166#define io_ptr ppriv_ptr0 167 168/* This is only used in the CTIO */ 169#define ccb_atio ppriv_ptr1 170 171#define PRIV_CCB(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[0]) 172#define PRIV_INFO(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[1]) 173 174static int ctlfeinitialize(void); 175static int ctlfeshutdown(void); 176static periph_init_t ctlfeperiphinit; 177static periph_deinit_t ctlfeperiphdeinit; 178static void ctlfeasync(void *callback_arg, uint32_t code, 179 struct cam_path *path, void *arg); 180static periph_ctor_t ctlferegister; 181static periph_oninv_t ctlfeoninvalidate; 182static periph_dtor_t ctlfecleanup; 183static periph_start_t ctlfestart; 184static void ctlfedone(struct cam_periph *periph, 185 union ccb *done_ccb); 186 187static void ctlfe_onoffline(void *arg, int online); 188static void ctlfe_online(void *arg); 189static void ctlfe_offline(void *arg); 190static int ctlfe_lun_enable(void *arg, int lun_id); 191static int ctlfe_lun_disable(void *arg, int lun_id); 192static void ctlfe_dump_sim(struct cam_sim *sim); 193static void ctlfe_dump_queue(struct ctlfe_lun_softc *softc); 194static void ctlfe_datamove(union ctl_io *io); 195static void ctlfe_done(union ctl_io *io); 196static void ctlfe_dump(void); 197static void ctlfe_free_ccb(struct cam_periph *periph, 198 union ccb *ccb); 199static void ctlfe_requeue_ccb(struct cam_periph *periph, 200 union ccb *ccb, int unlock); 201 202static struct periph_driver ctlfe_driver = 203{ 204 ctlfeperiphinit, "ctl", 205 TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0, 206 CAM_PERIPH_DRV_EARLY, 207 ctlfeperiphdeinit 208}; 209 210static struct ctl_frontend ctlfe_frontend = 211{ 212 .name = "camtgt", 213 .init = ctlfeinitialize, 214 .fe_dump = ctlfe_dump, 215 .shutdown = ctlfeshutdown, 216}; 217CTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend); 218 219static int 220ctlfeinitialize(void) 221{ 222 223 STAILQ_INIT(&ctlfe_softc_list); 224 mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF); 225 periphdriver_register(&ctlfe_driver); 226 return (0); 227} 228 229static int 230ctlfeshutdown(void) 231{ 232 int error; 233 234 error = periphdriver_unregister(&ctlfe_driver); 235 if (error != 0) 236 return (error); 237 mtx_destroy(&ctlfe_list_mtx); 238 return (0); 239} 240 241static void 242ctlfeperiphinit(void) 243{ 244 cam_status status; 245 246 status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED | 247 AC_CONTRACT, ctlfeasync, NULL, NULL); 248 if (status != CAM_REQ_CMP) { 249 printf("ctl: Failed to attach async callback due to CAM " 250 "status 0x%x!\n", status); 251 } 252} 253 254static int 255ctlfeperiphdeinit(void) 256{ 257 258 /* XXX: It would be good to tear down active ports here. */ 259 if (!TAILQ_EMPTY(&ctlfe_driver.units)) 260 return (EBUSY); 261 xpt_register_async(0, ctlfeasync, NULL, NULL); 262 return (0); 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 /* 312 * We're in an interrupt context here, so we have to 313 * use M_NOWAIT. Of course this means trouble if we 314 * can't allocate memory. 315 */ 316 softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO); 317 if (softc == NULL) { 318 printf("%s: unable to malloc %zd bytes for softc\n", 319 __func__, sizeof(*softc)); 320 return; 321 } 322 323 softc->path_id = cpi->ccb_h.path_id; 324 softc->target_id = cpi->initiator_id; 325 softc->sim = xpt_path_sim(path); 326 softc->hba_misc = cpi->hba_misc; 327 if (cpi->maxio != 0) 328 softc->maxio = cpi->maxio; 329 else 330 softc->maxio = DFLTPHYS; 331 mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF); 332 STAILQ_INIT(&softc->lun_softc_list); 333 334 port = &softc->port; 335 port->frontend = &ctlfe_frontend; 336 337 /* 338 * XXX KDM should we be more accurate here ? 339 */ 340 if (cpi->transport == XPORT_FC) 341 port->port_type = CTL_PORT_FC; 342 else if (cpi->transport == XPORT_SAS) 343 port->port_type = CTL_PORT_SAS; 344 else 345 port->port_type = CTL_PORT_SCSI; 346 347 /* XXX KDM what should the real number be here? */ 348 port->num_requested_ctl_io = CTLFE_REQ_CTL_IO; 349 snprintf(softc->port_name, sizeof(softc->port_name), 350 "%s%d", cpi->dev_name, cpi->unit_number); 351 /* 352 * XXX KDM it would be nice to allocate storage in the 353 * frontend structure itself. 354 */ 355 port->port_name = softc->port_name; 356 port->physical_port = cpi->bus_id; 357 port->virtual_port = 0; 358 port->port_online = ctlfe_online; 359 port->port_offline = ctlfe_offline; 360 port->onoff_arg = softc; 361 port->lun_enable = ctlfe_lun_enable; 362 port->lun_disable = ctlfe_lun_disable; 363 port->targ_lun_arg = softc; 364 port->fe_datamove = ctlfe_datamove; 365 port->fe_done = ctlfe_done; 366 /* 367 * XXX KDM the path inquiry doesn't give us the maximum 368 * number of targets supported. 369 */ 370 port->max_targets = cpi->max_target; 371 port->max_target_id = cpi->max_target; 372 port->targ_port = -1; 373 374 retval = ctl_port_register(port); 375 if (retval != 0) { 376 printf("%s: ctl_port_register() failed with " 377 "error %d!\n", __func__, retval); 378 mtx_destroy(&softc->lun_softc_mtx); 379 free(softc, M_CTLFE); 380 break; 381 } else { 382 mtx_lock(&ctlfe_list_mtx); 383 STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links); 384 mtx_unlock(&ctlfe_list_mtx); 385 } 386 387 break; 388 } 389 case AC_PATH_DEREGISTERED: { 390 391 if (softc != NULL) { 392 /* 393 * XXX KDM are we certain at this point that there 394 * are no outstanding commands for this frontend? 395 */ 396 mtx_lock(&ctlfe_list_mtx); 397 STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc, 398 links); 399 mtx_unlock(&ctlfe_list_mtx); 400 ctl_port_deregister(&softc->port); 401 mtx_destroy(&softc->lun_softc_mtx); 402 free(softc, M_CTLFE); 403 } 404 break; 405 } 406 case AC_CONTRACT: { 407 struct ac_contract *ac; 408 409 ac = (struct ac_contract *)arg; 410 411 switch (ac->contract_number) { 412 case AC_CONTRACT_DEV_CHG: { 413 struct ac_device_changed *dev_chg; 414 int retval; 415 416 dev_chg = (struct ac_device_changed *)ac->contract_data; 417 418 printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n", 419 __func__, dev_chg->wwpn, dev_chg->port, 420 xpt_path_path_id(path), dev_chg->target, 421 (dev_chg->arrived == 0) ? "left" : "arrived"); 422 423 if (softc == NULL) { 424 printf("%s: CTL port for CAM path %u not " 425 "found!\n", __func__, 426 xpt_path_path_id(path)); 427 break; 428 } 429 if (dev_chg->arrived != 0) { 430 retval = ctl_add_initiator(&softc->port, 431 dev_chg->target, dev_chg->wwpn, NULL); 432 } else { 433 retval = ctl_remove_initiator(&softc->port, 434 dev_chg->target); 435 } 436 437 if (retval < 0) { 438 printf("%s: could not %s port %d iid %u " 439 "WWPN %#jx!\n", __func__, 440 (dev_chg->arrived != 0) ? "add" : 441 "remove", softc->port.targ_port, 442 dev_chg->target, 443 (uintmax_t)dev_chg->wwpn); 444 } 445 break; 446 } 447 default: 448 printf("%s: unsupported contract number %ju\n", 449 __func__, (uintmax_t)ac->contract_number); 450 break; 451 } 452 break; 453 } 454 default: 455 break; 456 } 457} 458 459static cam_status 460ctlferegister(struct cam_periph *periph, void *arg) 461{ 462 struct ctlfe_softc *bus_softc; 463 struct ctlfe_lun_softc *softc; 464 union ccb ccb; 465 cam_status status; 466 int i; 467 468 softc = (struct ctlfe_lun_softc *)arg; 469 bus_softc = softc->parent_softc; 470 471 STAILQ_INIT(&softc->work_queue); 472 LIST_INIT(&softc->atio_list); 473 LIST_INIT(&softc->inot_list); 474 softc->periph = periph; 475 periph->softc = softc; 476 477 /* Increase device openings to maximum for the SIM. */ 478 if (bus_softc->sim->max_tagged_dev_openings > 479 bus_softc->sim->max_dev_openings) { 480 cam_release_devq(periph->path, 481 /*relsim_flags*/RELSIM_ADJUST_OPENINGS, 482 /*openings*/bus_softc->sim->max_tagged_dev_openings, 483 /*timeout*/0, 484 /*getcount_only*/1); 485 } 486 487 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NONE); 488 ccb.ccb_h.func_code = XPT_EN_LUN; 489 ccb.cel.grp6_len = 0; 490 ccb.cel.grp7_len = 0; 491 ccb.cel.enable = 1; 492 xpt_action(&ccb); 493 status = (ccb.ccb_h.status & CAM_STATUS_MASK); 494 if (status != CAM_REQ_CMP) { 495 xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n", 496 __func__, ccb.ccb_h.status); 497 return (status); 498 } 499 500 status = CAM_REQ_CMP; 501 502 for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) { 503 union ccb *new_ccb; 504 union ctl_io *new_io; 505 struct ctlfe_cmd_info *cmd_info; 506 507 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE, 508 M_ZERO|M_NOWAIT); 509 if (new_ccb == NULL) { 510 status = CAM_RESRC_UNAVAIL; 511 break; 512 } 513 new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref); 514 if (new_io == NULL) { 515 free(new_ccb, M_CTLFE); 516 status = CAM_RESRC_UNAVAIL; 517 break; 518 } 519 cmd_info = malloc(sizeof(*cmd_info), M_CTLFE, 520 M_ZERO | M_NOWAIT); 521 if (cmd_info == NULL) { 522 ctl_free_io(new_io); 523 free(new_ccb, M_CTLFE); 524 status = CAM_RESRC_UNAVAIL; 525 break; 526 } 527 PRIV_INFO(new_io) = cmd_info; 528 softc->atios_alloced++; 529 new_ccb->ccb_h.io_ptr = new_io; 530 LIST_INSERT_HEAD(&softc->atio_list, &new_ccb->ccb_h, periph_links.le); 531 532 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1); 533 new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO; 534 new_ccb->ccb_h.cbfcnp = ctlfedone; 535 new_ccb->ccb_h.flags |= CAM_UNLOCKED; 536 xpt_action(new_ccb); 537 status = new_ccb->ccb_h.status; 538 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 539 free(cmd_info, M_CTLFE); 540 ctl_free_io(new_io); 541 free(new_ccb, M_CTLFE); 542 break; 543 } 544 } 545 546 status = cam_periph_acquire(periph); 547 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 548 xpt_print(periph->path, "%s: could not acquire reference " 549 "count, status = %#x\n", __func__, status); 550 return (status); 551 } 552 553 if (i == 0) { 554 xpt_print(periph->path, "%s: could not allocate ATIO CCBs, " 555 "status 0x%x\n", __func__, status); 556 return (CAM_REQ_CMP_ERR); 557 } 558 559 for (i = 0; i < CTLFE_IN_PER_LUN; i++) { 560 union ccb *new_ccb; 561 union ctl_io *new_io; 562 563 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE, 564 M_ZERO|M_NOWAIT); 565 if (new_ccb == NULL) { 566 status = CAM_RESRC_UNAVAIL; 567 break; 568 } 569 new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref); 570 if (new_io == NULL) { 571 free(new_ccb, M_CTLFE); 572 status = CAM_RESRC_UNAVAIL; 573 break; 574 } 575 softc->inots_alloced++; 576 new_ccb->ccb_h.io_ptr = new_io; 577 LIST_INSERT_HEAD(&softc->inot_list, &new_ccb->ccb_h, periph_links.le); 578 579 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1); 580 new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY; 581 new_ccb->ccb_h.cbfcnp = ctlfedone; 582 new_ccb->ccb_h.flags |= CAM_UNLOCKED; 583 xpt_action(new_ccb); 584 status = new_ccb->ccb_h.status; 585 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 586 /* 587 * Note that we don't free the CCB here. If the 588 * status is not CAM_REQ_INPROG, then we're 589 * probably talking to a SIM that says it is 590 * target-capable but doesn't support the 591 * XPT_IMMEDIATE_NOTIFY CCB. i.e. it supports the 592 * older API. In that case, it'll call xpt_done() 593 * on the CCB, and we need to free it in our done 594 * routine as a result. 595 */ 596 break; 597 } 598 } 599 if ((i == 0) 600 || (status != CAM_REQ_INPROG)) { 601 xpt_print(periph->path, "%s: could not allocate immediate " 602 "notify CCBs, status 0x%x\n", __func__, status); 603 return (CAM_REQ_CMP_ERR); 604 } 605 mtx_lock(&bus_softc->lun_softc_mtx); 606 STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links); 607 mtx_unlock(&bus_softc->lun_softc_mtx); 608 return (CAM_REQ_CMP); 609} 610 611static void 612ctlfeoninvalidate(struct cam_periph *periph) 613{ 614 struct ctlfe_lun_softc *softc = (struct ctlfe_lun_softc *)periph->softc; 615 struct ctlfe_softc *bus_softc; 616 union ccb ccb; 617 struct ccb_hdr *hdr; 618 cam_status status; 619 620 /* Abort all ATIOs and INOTs queued to SIM. */ 621 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NONE); 622 ccb.ccb_h.func_code = XPT_ABORT; 623 LIST_FOREACH(hdr, &softc->atio_list, periph_links.le) { 624 ccb.cab.abort_ccb = (union ccb *)hdr; 625 xpt_action(&ccb); 626 } 627 LIST_FOREACH(hdr, &softc->inot_list, periph_links.le) { 628 ccb.cab.abort_ccb = (union ccb *)hdr; 629 xpt_action(&ccb); 630 } 631 632 /* Disable the LUN in SIM. */ 633 ccb.ccb_h.func_code = XPT_EN_LUN; 634 ccb.cel.grp6_len = 0; 635 ccb.cel.grp7_len = 0; 636 ccb.cel.enable = 0; 637 xpt_action(&ccb); 638 status = (ccb.ccb_h.status & CAM_STATUS_MASK); 639 if (status != CAM_REQ_CMP) { 640 xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n", 641 __func__, ccb.ccb_h.status); 642 /* 643 * XXX KDM what do we do now? 644 */ 645 } 646 647 bus_softc = softc->parent_softc; 648 mtx_lock(&bus_softc->lun_softc_mtx); 649 STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links); 650 mtx_unlock(&bus_softc->lun_softc_mtx); 651} 652 653static void 654ctlfecleanup(struct cam_periph *periph) 655{ 656 struct ctlfe_lun_softc *softc; 657 658 softc = (struct ctlfe_lun_softc *)periph->softc; 659 660 KASSERT(softc->ctios_sent == 0, ("%s: ctios_sent %d != 0", 661 __func__, softc->ctios_sent)); 662 KASSERT(softc->refcount == 0, ("%s: refcount %d != 0", 663 __func__, softc->refcount)); 664 KASSERT(softc->atios_alloced == 0, ("%s: atios_alloced %d != 0", 665 __func__, softc->atios_alloced)); 666 KASSERT(softc->inots_alloced == 0, ("%s: inots_alloced %d != 0", 667 __func__, softc->inots_alloced)); 668 669 free(softc, M_CTLFE); 670} 671 672static void 673ctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io, 674 ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len, 675 u_int16_t *sglist_cnt) 676{ 677 struct ctlfe_softc *bus_softc; 678 struct ctlfe_cmd_info *cmd_info; 679 struct ctl_sg_entry *ctl_sglist; 680 bus_dma_segment_t *cam_sglist; 681 size_t off; 682 int i, idx; 683 684 cmd_info = PRIV_INFO(io); 685 bus_softc = softc->parent_softc; 686 687 /* 688 * Set the direction, relative to the initiator. 689 */ 690 *flags &= ~CAM_DIR_MASK; 691 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 692 *flags |= CAM_DIR_IN; 693 else 694 *flags |= CAM_DIR_OUT; 695 696 *flags &= ~CAM_DATA_MASK; 697 idx = cmd_info->cur_transfer_index; 698 off = cmd_info->cur_transfer_off; 699 cmd_info->flags &= ~CTLFE_CMD_PIECEWISE; 700 if (io->scsiio.kern_sg_entries == 0) { /* No S/G list. */ 701 702 /* One time shift for SRR offset. */ 703 off += io->scsiio.ext_data_filled; 704 io->scsiio.ext_data_filled = 0; 705 706 *data_ptr = io->scsiio.kern_data_ptr + off; 707 if (io->scsiio.kern_data_len - off <= bus_softc->maxio) { 708 *dxfer_len = io->scsiio.kern_data_len - off; 709 } else { 710 *dxfer_len = bus_softc->maxio; 711 cmd_info->cur_transfer_off += bus_softc->maxio; 712 cmd_info->flags |= CTLFE_CMD_PIECEWISE; 713 } 714 *sglist_cnt = 0; 715 716 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) 717 *flags |= CAM_DATA_PADDR; 718 else 719 *flags |= CAM_DATA_VADDR; 720 } else { /* S/G list with physical or virtual pointers. */ 721 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 722 723 /* One time shift for SRR offset. */ 724 while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) { 725 io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off; 726 idx++; 727 off = 0; 728 } 729 off += io->scsiio.ext_data_filled; 730 io->scsiio.ext_data_filled = 0; 731 732 cam_sglist = cmd_info->cam_sglist; 733 *dxfer_len = 0; 734 for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) { 735 cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off; 736 if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) { 737 cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off; 738 *dxfer_len += cam_sglist[i].ds_len; 739 } else { 740 cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len; 741 cmd_info->cur_transfer_index = idx + i; 742 cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off; 743 cmd_info->flags |= CTLFE_CMD_PIECEWISE; 744 *dxfer_len += cam_sglist[i].ds_len; 745 if (ctl_sglist[i].len != 0) 746 i++; 747 break; 748 } 749 if (i == (CTLFE_MAX_SEGS - 1) && 750 idx + i < (io->scsiio.kern_sg_entries - 1)) { 751 cmd_info->cur_transfer_index = idx + i + 1; 752 cmd_info->cur_transfer_off = 0; 753 cmd_info->flags |= CTLFE_CMD_PIECEWISE; 754 i++; 755 break; 756 } 757 off = 0; 758 } 759 *sglist_cnt = i; 760 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) 761 *flags |= CAM_DATA_SG_PADDR; 762 else 763 *flags |= CAM_DATA_SG; 764 *data_ptr = (uint8_t *)cam_sglist; 765 } 766} 767 768static void 769ctlfestart(struct cam_periph *periph, union ccb *start_ccb) 770{ 771 struct ctlfe_lun_softc *softc; 772 struct ctlfe_cmd_info *cmd_info; 773 struct ccb_hdr *ccb_h; 774 struct ccb_accept_tio *atio; 775 struct ccb_scsiio *csio; 776 uint8_t *data_ptr; 777 uint32_t dxfer_len; 778 ccb_flags flags; 779 union ctl_io *io; 780 uint8_t scsi_status; 781 782 softc = (struct ctlfe_lun_softc *)periph->softc; 783 784next: 785 /* Take the ATIO off the work queue */ 786 ccb_h = STAILQ_FIRST(&softc->work_queue); 787 if (ccb_h == NULL) { 788 xpt_release_ccb(start_ccb); 789 return; 790 } 791 STAILQ_REMOVE_HEAD(&softc->work_queue, periph_links.stqe); 792 atio = (struct ccb_accept_tio *)ccb_h; 793 io = (union ctl_io *)ccb_h->io_ptr; 794 csio = &start_ccb->csio; 795 796 flags = atio->ccb_h.flags & 797 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK); 798 cmd_info = PRIV_INFO(io); 799 cmd_info->cur_transfer_index = 0; 800 cmd_info->cur_transfer_off = 0; 801 cmd_info->flags = 0; 802 803 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) { 804 /* 805 * Datamove call, we need to setup the S/G list. 806 */ 807 ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len, 808 &csio->sglist_cnt); 809 } else { 810 /* 811 * We're done, send status back. 812 */ 813 if ((io->io_hdr.flags & CTL_FLAG_ABORT) && 814 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) { 815 io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED; 816 817 /* Tell the SIM that we've aborted this ATIO */ 818#ifdef CTLFEDEBUG 819 printf("%s: tag %04x abort\n", __func__, atio->tag_id); 820#endif 821 KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO, 822 ("func_code %#x is not ATIO", atio->ccb_h.func_code)); 823 start_ccb->ccb_h.func_code = XPT_ABORT; 824 start_ccb->cab.abort_ccb = (union ccb *)atio; 825 xpt_action(start_ccb); 826 827 ctlfe_requeue_ccb(periph, (union ccb *)atio, 828 /* unlock */0); 829 830 /* XPT_ABORT is not queued, so we can take next I/O. */ 831 goto next; 832 } 833 data_ptr = NULL; 834 dxfer_len = 0; 835 csio->sglist_cnt = 0; 836 } 837 scsi_status = 0; 838 if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) && 839 (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 && 840 ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 || 841 io->io_hdr.status == CTL_SUCCESS)) { 842 flags |= CAM_SEND_STATUS; 843 scsi_status = io->scsiio.scsi_status; 844 csio->sense_len = io->scsiio.sense_len; 845#ifdef CTLFEDEBUG 846 printf("%s: tag %04x status %x\n", __func__, 847 atio->tag_id, io->io_hdr.status); 848#endif 849 if (csio->sense_len != 0) { 850 csio->sense_data = io->scsiio.sense_data; 851 flags |= CAM_SEND_SENSE; 852 } 853 } 854 855#ifdef CTLFEDEBUG 856 printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__, 857 (flags & CAM_SEND_STATUS) ? "done" : "datamove", 858 atio->tag_id, flags, data_ptr, dxfer_len); 859#endif 860 861 /* 862 * Valid combinations: 863 * - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0, 864 * sglist_cnt = 0 865 * - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0, 866 * sglist_cnt = 0 867 * - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0, 868 * sglist_cnt != 0 869 */ 870#ifdef CTLFEDEBUG 871 if (((flags & CAM_SEND_STATUS) 872 && (((flags & CAM_DATA_SG) != 0) 873 || (dxfer_len != 0) 874 || (csio->sglist_cnt != 0))) 875 || (((flags & CAM_SEND_STATUS) == 0) 876 && (dxfer_len == 0)) 877 || ((flags & CAM_DATA_SG) 878 && (csio->sglist_cnt == 0)) 879 || (((flags & CAM_DATA_SG) == 0) 880 && (csio->sglist_cnt != 0))) { 881 printf("%s: tag %04x cdb %02x flags %#x dxfer_len " 882 "%d sg %u\n", __func__, atio->tag_id, 883 atio_cdb_ptr(atio)[0], flags, dxfer_len, 884 csio->sglist_cnt); 885 printf("%s: tag %04x io status %#x\n", __func__, 886 atio->tag_id, io->io_hdr.status); 887 } 888#endif 889 cam_fill_ctio(csio, 890 /*retries*/ 2, 891 ctlfedone, 892 flags, 893 (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0, 894 atio->tag_id, 895 atio->init_id, 896 scsi_status, 897 /*data_ptr*/ data_ptr, 898 /*dxfer_len*/ dxfer_len, 899 /*timeout*/ CTLFE_TIMEOUT * 1000); 900 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 901 start_ccb->ccb_h.ccb_atio = atio; 902 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 903 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 904 io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED); 905 906 softc->ctios_sent++; 907 softc->refcount++; 908 cam_periph_unlock(periph); 909 xpt_action(start_ccb); 910 cam_periph_lock(periph); 911 softc->refcount--; 912 913 /* 914 * If we still have work to do, ask for another CCB. 915 */ 916 if (!STAILQ_EMPTY(&softc->work_queue)) 917 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 918} 919 920static void 921ctlfe_drain(void *context, int pending) 922{ 923 struct cam_periph *periph = context; 924 struct ctlfe_lun_softc *softc = periph->softc; 925 926 cam_periph_lock(periph); 927 while (softc->refcount != 0) { 928 cam_periph_sleep(periph, &softc->refcount, PRIBIO, 929 "ctlfe_drain", 1); 930 } 931 cam_periph_unlock(periph); 932 cam_periph_release(periph); 933} 934 935static void 936ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb) 937{ 938 struct ctlfe_lun_softc *softc; 939 union ctl_io *io; 940 struct ctlfe_cmd_info *cmd_info; 941 942 softc = (struct ctlfe_lun_softc *)periph->softc; 943 io = ccb->ccb_h.io_ptr; 944 945 switch (ccb->ccb_h.func_code) { 946 case XPT_ACCEPT_TARGET_IO: 947 softc->atios_alloced--; 948 cmd_info = PRIV_INFO(io); 949 free(cmd_info, M_CTLFE); 950 break; 951 case XPT_IMMEDIATE_NOTIFY: 952 case XPT_NOTIFY_ACKNOWLEDGE: 953 softc->inots_alloced--; 954 break; 955 default: 956 break; 957 } 958 959 ctl_free_io(io); 960 free(ccb, M_CTLFE); 961 962 KASSERT(softc->atios_alloced >= 0, ("%s: atios_alloced %d < 0", 963 __func__, softc->atios_alloced)); 964 KASSERT(softc->inots_alloced >= 0, ("%s: inots_alloced %d < 0", 965 __func__, softc->inots_alloced)); 966 967 /* 968 * If we have received all of our CCBs, we can release our 969 * reference on the peripheral driver. It will probably go away 970 * now. 971 */ 972 if (softc->atios_alloced == 0 && softc->inots_alloced == 0) { 973 if (softc->refcount == 0) { 974 cam_periph_release_locked(periph); 975 } else { 976 TASK_INIT(&softc->refdrain_task, 0, ctlfe_drain, periph); 977 taskqueue_enqueue(taskqueue_thread, 978 &softc->refdrain_task); 979 } 980 } 981} 982 983/* 984 * Send the ATIO/INOT back to the SIM, or free it if periph was invalidated. 985 */ 986static void 987ctlfe_requeue_ccb(struct cam_periph *periph, union ccb *ccb, int unlock) 988{ 989 struct ctlfe_lun_softc *softc; 990 struct mtx *mtx; 991 992 if (periph->flags & CAM_PERIPH_INVALID) { 993 mtx = cam_periph_mtx(periph); 994 ctlfe_free_ccb(periph, ccb); 995 if (unlock) 996 mtx_unlock(mtx); 997 return; 998 } 999 softc = (struct ctlfe_lun_softc *)periph->softc; 1000 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) 1001 LIST_INSERT_HEAD(&softc->atio_list, &ccb->ccb_h, periph_links.le); 1002 else 1003 LIST_INSERT_HEAD(&softc->inot_list, &ccb->ccb_h, periph_links.le); 1004 if (unlock) 1005 cam_periph_unlock(periph); 1006 1007 /* 1008 * For a wildcard attachment, commands can come in with a specific 1009 * target/lun. Reset the target and LUN fields back to the wildcard 1010 * values before we send them back down to the SIM. 1011 */ 1012 if (softc->flags & CTLFE_LUN_WILDCARD) { 1013 ccb->ccb_h.target_id = CAM_TARGET_WILDCARD; 1014 ccb->ccb_h.target_lun = CAM_LUN_WILDCARD; 1015 } 1016 1017 xpt_action(ccb); 1018} 1019 1020static int 1021ctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset) 1022{ 1023 uint64_t lba; 1024 uint32_t num_blocks, nbc; 1025 uint8_t *cmdbyt = atio_cdb_ptr(atio); 1026 1027 nbc = offset >> 9; /* ASSUMING 512 BYTE BLOCKS */ 1028 1029 switch (cmdbyt[0]) { 1030 case READ_6: 1031 case WRITE_6: 1032 { 1033 struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt; 1034 lba = scsi_3btoul(cdb->addr); 1035 lba &= 0x1fffff; 1036 num_blocks = cdb->length; 1037 if (num_blocks == 0) 1038 num_blocks = 256; 1039 lba += nbc; 1040 num_blocks -= nbc; 1041 scsi_ulto3b(lba, cdb->addr); 1042 cdb->length = num_blocks; 1043 break; 1044 } 1045 case READ_10: 1046 case WRITE_10: 1047 { 1048 struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt; 1049 lba = scsi_4btoul(cdb->addr); 1050 num_blocks = scsi_2btoul(cdb->length); 1051 lba += nbc; 1052 num_blocks -= nbc; 1053 scsi_ulto4b(lba, cdb->addr); 1054 scsi_ulto2b(num_blocks, cdb->length); 1055 break; 1056 } 1057 case READ_12: 1058 case WRITE_12: 1059 { 1060 struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt; 1061 lba = scsi_4btoul(cdb->addr); 1062 num_blocks = scsi_4btoul(cdb->length); 1063 lba += nbc; 1064 num_blocks -= nbc; 1065 scsi_ulto4b(lba, cdb->addr); 1066 scsi_ulto4b(num_blocks, cdb->length); 1067 break; 1068 } 1069 case READ_16: 1070 case WRITE_16: 1071 { 1072 struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt; 1073 lba = scsi_8btou64(cdb->addr); 1074 num_blocks = scsi_4btoul(cdb->length); 1075 lba += nbc; 1076 num_blocks -= nbc; 1077 scsi_u64to8b(lba, cdb->addr); 1078 scsi_ulto4b(num_blocks, cdb->length); 1079 break; 1080 } 1081 default: 1082 return -1; 1083 } 1084 return (0); 1085} 1086 1087static void 1088ctlfedone(struct cam_periph *periph, union ccb *done_ccb) 1089{ 1090 struct ctlfe_lun_softc *softc; 1091 struct ctlfe_softc *bus_softc; 1092 struct ctlfe_cmd_info *cmd_info; 1093 struct ccb_accept_tio *atio = NULL; 1094 union ctl_io *io = NULL; 1095 struct mtx *mtx; 1096 cam_status status; 1097 1098 KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0, 1099 ("CCB in ctlfedone() without CAM_UNLOCKED flag")); 1100#ifdef CTLFE_DEBUG 1101 printf("%s: entered, func_code = %#x\n", __func__, 1102 done_ccb->ccb_h.func_code); 1103#endif 1104 1105 /* 1106 * At this point CTL has no known use case for device queue freezes. 1107 * In case some SIM think different -- drop its freeze right here. 1108 */ 1109 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1110 cam_release_devq(periph->path, 1111 /*relsim_flags*/0, 1112 /*reduction*/0, 1113 /*timeout*/0, 1114 /*getcount_only*/0); 1115 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 1116 } 1117 1118 softc = (struct ctlfe_lun_softc *)periph->softc; 1119 bus_softc = softc->parent_softc; 1120 mtx = cam_periph_mtx(periph); 1121 mtx_lock(mtx); 1122 1123 switch (done_ccb->ccb_h.func_code) { 1124 case XPT_ACCEPT_TARGET_IO: { 1125 1126 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1127 atio = &done_ccb->atio; 1128 status = atio->ccb_h.status & CAM_STATUS_MASK; 1129 if (status != CAM_CDB_RECVD) { 1130 ctlfe_free_ccb(periph, done_ccb); 1131 goto out; 1132 } 1133 1134 resubmit: 1135 /* 1136 * Allocate a ctl_io, pass it to CTL, and wait for the 1137 * datamove or done. 1138 */ 1139 mtx_unlock(mtx); 1140 io = done_ccb->ccb_h.io_ptr; 1141 cmd_info = PRIV_INFO(io); 1142 ctl_zero_io(io); 1143 1144 /* Save pointers on both sides */ 1145 PRIV_CCB(io) = done_ccb; 1146 PRIV_INFO(io) = cmd_info; 1147 done_ccb->ccb_h.io_ptr = io; 1148 1149 /* 1150 * Only SCSI I/O comes down this path, resets, etc. come 1151 * down the immediate notify path below. 1152 */ 1153 io->io_hdr.io_type = CTL_IO_SCSI; 1154 io->io_hdr.nexus.initid = atio->init_id; 1155 io->io_hdr.nexus.targ_port = bus_softc->port.targ_port; 1156 if (bus_softc->hba_misc & PIM_EXTLUNS) { 1157 io->io_hdr.nexus.targ_lun = ctl_decode_lun( 1158 CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun)); 1159 } else { 1160 io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun; 1161 } 1162 io->scsiio.tag_num = atio->tag_id; 1163 switch (atio->tag_action) { 1164 case CAM_TAG_ACTION_NONE: 1165 io->scsiio.tag_type = CTL_TAG_UNTAGGED; 1166 break; 1167 case MSG_SIMPLE_TASK: 1168 io->scsiio.tag_type = CTL_TAG_SIMPLE; 1169 break; 1170 case MSG_HEAD_OF_QUEUE_TASK: 1171 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE; 1172 break; 1173 case MSG_ORDERED_TASK: 1174 io->scsiio.tag_type = CTL_TAG_ORDERED; 1175 break; 1176 case MSG_ACA_TASK: 1177 io->scsiio.tag_type = CTL_TAG_ACA; 1178 break; 1179 default: 1180 io->scsiio.tag_type = CTL_TAG_UNTAGGED; 1181 printf("%s: unhandled tag type %#x!!\n", __func__, 1182 atio->tag_action); 1183 break; 1184 } 1185 if (atio->cdb_len > sizeof(io->scsiio.cdb)) { 1186 printf("%s: WARNING: CDB len %d > ctl_io space %zd\n", 1187 __func__, atio->cdb_len, sizeof(io->scsiio.cdb)); 1188 } 1189 io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb)); 1190 bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len); 1191 1192#ifdef CTLFEDEBUG 1193 printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__, 1194 io->io_hdr.nexus.initid, 1195 io->io_hdr.nexus.targ_port, 1196 io->io_hdr.nexus.targ_lun, 1197 io->scsiio.tag_num, io->scsiio.cdb[0]); 1198#endif 1199 1200 ctl_queue(io); 1201 return; 1202 } 1203 case XPT_CONT_TARGET_IO: { 1204 int srr = 0; 1205 uint32_t srr_off = 0; 1206 1207 atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio; 1208 io = (union ctl_io *)atio->ccb_h.io_ptr; 1209 1210 softc->ctios_sent--; 1211#ifdef CTLFEDEBUG 1212 printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n", 1213 __func__, atio->tag_id, done_ccb->ccb_h.flags); 1214#endif 1215 /* 1216 * Handle SRR case were the data pointer is pushed back hack 1217 */ 1218 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV 1219 && done_ccb->csio.msg_ptr != NULL 1220 && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED 1221 && done_ccb->csio.msg_ptr[1] == 5 1222 && done_ccb->csio.msg_ptr[2] == 0) { 1223 srr = 1; 1224 srr_off = 1225 (done_ccb->csio.msg_ptr[3] << 24) 1226 | (done_ccb->csio.msg_ptr[4] << 16) 1227 | (done_ccb->csio.msg_ptr[5] << 8) 1228 | (done_ccb->csio.msg_ptr[6]); 1229 } 1230 1231 /* 1232 * If we have an SRR and we're still sending data, we 1233 * should be able to adjust offsets and cycle again. 1234 * It is possible only if offset is from this datamove. 1235 */ 1236 if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) && 1237 srr_off >= io->scsiio.kern_rel_offset && 1238 srr_off < io->scsiio.kern_rel_offset + 1239 io->scsiio.kern_data_len) { 1240 io->scsiio.kern_data_resid = 1241 io->scsiio.kern_rel_offset + 1242 io->scsiio.kern_data_len - srr_off; 1243 io->scsiio.ext_data_filled = srr_off; 1244 io->scsiio.io_hdr.status = CTL_STATUS_NONE; 1245 io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED; 1246 xpt_release_ccb(done_ccb); 1247 STAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h, 1248 periph_links.stqe); 1249 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1250 break; 1251 } 1252 1253 /* 1254 * If status was being sent, the back end data is now history. 1255 * Hack it up and resubmit a new command with the CDB adjusted. 1256 * If the SIM does the right thing, all of the resid math 1257 * should work. 1258 */ 1259 if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) { 1260 xpt_release_ccb(done_ccb); 1261 if (ctlfe_adjust_cdb(atio, srr_off) == 0) { 1262 done_ccb = (union ccb *)atio; 1263 goto resubmit; 1264 } 1265 /* 1266 * Fall through to doom.... 1267 */ 1268 } 1269 1270 if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) && 1271 (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 1272 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1273 1274 /* 1275 * If we were sending status back to the initiator, free up 1276 * resources. If we were doing a datamove, call the 1277 * datamove done routine. 1278 */ 1279 if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) { 1280 /* 1281 * If we asked to send sense data but it wasn't sent, 1282 * queue the I/O back to CTL for later REQUEST SENSE. 1283 */ 1284 if ((done_ccb->ccb_h.flags & CAM_SEND_SENSE) != 0 && 1285 (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && 1286 (done_ccb->ccb_h.status & CAM_SENT_SENSE) == 0 && 1287 (io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref)) != NULL) { 1288 PRIV_INFO(io) = PRIV_INFO( 1289 (union ctl_io *)atio->ccb_h.io_ptr); 1290 ctl_queue_sense(atio->ccb_h.io_ptr); 1291 atio->ccb_h.io_ptr = io; 1292 } 1293 1294 /* Abort ATIO if CTIO sending status has failed. */ 1295 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != 1296 CAM_REQ_CMP) { 1297 done_ccb->ccb_h.func_code = XPT_ABORT; 1298 done_ccb->cab.abort_ccb = (union ccb *)atio; 1299 xpt_action(done_ccb); 1300 } 1301 1302 xpt_release_ccb(done_ccb); 1303 ctlfe_requeue_ccb(periph, (union ccb *)atio, 1304 /* unlock */1); 1305 return; 1306 } else { 1307 struct ctlfe_cmd_info *cmd_info; 1308 struct ccb_scsiio *csio; 1309 1310 csio = &done_ccb->csio; 1311 cmd_info = PRIV_INFO(io); 1312 1313 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1314 1315 /* 1316 * Translate CAM status to CTL status. Success 1317 * does not change the overall, ctl_io status. In 1318 * that case we just set port_status to 0. If we 1319 * have a failure, though, set a data phase error 1320 * for the overall ctl_io. 1321 */ 1322 switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) { 1323 case CAM_REQ_CMP: 1324 io->scsiio.kern_data_resid -= 1325 csio->dxfer_len - csio->resid; 1326 io->io_hdr.port_status = 0; 1327 break; 1328 default: 1329 /* 1330 * XXX KDM we probably need to figure out a 1331 * standard set of errors that the SIM 1332 * drivers should return in the event of a 1333 * data transfer failure. A data phase 1334 * error will at least point the user to a 1335 * data transfer error of some sort. 1336 * Hopefully the SIM printed out some 1337 * additional information to give the user 1338 * a clue what happened. 1339 */ 1340 io->io_hdr.port_status = 0xbad1; 1341 ctl_set_data_phase_error(&io->scsiio); 1342 /* 1343 * XXX KDM figure out residual. 1344 */ 1345 break; 1346 } 1347 /* 1348 * If we had to break this S/G list into multiple 1349 * pieces, figure out where we are in the list, and 1350 * continue sending pieces if necessary. 1351 */ 1352 if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) && 1353 io->io_hdr.port_status == 0 && csio->resid == 0) { 1354 ccb_flags flags; 1355 uint8_t *data_ptr; 1356 uint32_t dxfer_len; 1357 1358 flags = atio->ccb_h.flags & 1359 (CAM_DIS_DISCONNECT| 1360 CAM_TAG_ACTION_VALID); 1361 1362 ctlfedata(softc, io, &flags, &data_ptr, 1363 &dxfer_len, &csio->sglist_cnt); 1364 1365 if (((flags & CAM_SEND_STATUS) == 0) 1366 && (dxfer_len == 0)) { 1367 printf("%s: tag %04x no status or " 1368 "len cdb = %02x\n", __func__, 1369 atio->tag_id, 1370 atio_cdb_ptr(atio)[0]); 1371 printf("%s: tag %04x io status %#x\n", 1372 __func__, atio->tag_id, 1373 io->io_hdr.status); 1374 } 1375 1376 cam_fill_ctio(csio, 1377 /*retries*/ 2, 1378 ctlfedone, 1379 flags, 1380 (flags & CAM_TAG_ACTION_VALID) ? 1381 MSG_SIMPLE_Q_TAG : 0, 1382 atio->tag_id, 1383 atio->init_id, 1384 0, 1385 /*data_ptr*/ data_ptr, 1386 /*dxfer_len*/ dxfer_len, 1387 CTLFE_TIMEOUT * 1000); 1388 1389 csio->ccb_h.flags |= CAM_UNLOCKED; 1390 csio->resid = 0; 1391 csio->ccb_h.ccb_atio = atio; 1392 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 1393 softc->ctios_sent++; 1394 mtx_unlock(mtx); 1395 xpt_action((union ccb *)csio); 1396 } else { 1397 /* 1398 * Release the CTIO. The ATIO will be sent back 1399 * down to the SIM once we send status. 1400 */ 1401 xpt_release_ccb(done_ccb); 1402 mtx_unlock(mtx); 1403 1404 /* Call the backend move done callback */ 1405 io->scsiio.be_move_done(io); 1406 } 1407 return; 1408 } 1409 break; 1410 } 1411 case XPT_IMMEDIATE_NOTIFY: { 1412 union ctl_io *io; 1413 struct ccb_immediate_notify *inot; 1414 int send_ctl_io; 1415 1416 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); 1417 inot = &done_ccb->cin1; 1418 io = done_ccb->ccb_h.io_ptr; 1419 ctl_zero_io(io); 1420 1421 send_ctl_io = 1; 1422 1423 io->io_hdr.io_type = CTL_IO_TASK; 1424 PRIV_CCB(io) = done_ccb; 1425 inot->ccb_h.io_ptr = io; 1426 io->io_hdr.nexus.initid = inot->initiator_id; 1427 io->io_hdr.nexus.targ_port = bus_softc->port.targ_port; 1428 if (bus_softc->hba_misc & PIM_EXTLUNS) { 1429 io->io_hdr.nexus.targ_lun = ctl_decode_lun( 1430 CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun)); 1431 } else { 1432 io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun; 1433 } 1434 /* XXX KDM should this be the tag_id? */ 1435 io->taskio.tag_num = inot->seq_id; 1436 1437 status = inot->ccb_h.status & CAM_STATUS_MASK; 1438 switch (status) { 1439 case CAM_SCSI_BUS_RESET: 1440 io->taskio.task_action = CTL_TASK_BUS_RESET; 1441 break; 1442 case CAM_BDR_SENT: 1443 io->taskio.task_action = CTL_TASK_TARGET_RESET; 1444 break; 1445 case CAM_MESSAGE_RECV: 1446 switch (inot->arg) { 1447 case MSG_ABORT_TASK_SET: 1448 io->taskio.task_action = 1449 CTL_TASK_ABORT_TASK_SET; 1450 break; 1451 case MSG_TARGET_RESET: 1452 io->taskio.task_action = CTL_TASK_TARGET_RESET; 1453 break; 1454 case MSG_ABORT_TASK: 1455 io->taskio.task_action = CTL_TASK_ABORT_TASK; 1456 break; 1457 case MSG_LOGICAL_UNIT_RESET: 1458 io->taskio.task_action = CTL_TASK_LUN_RESET; 1459 break; 1460 case MSG_CLEAR_TASK_SET: 1461 io->taskio.task_action = 1462 CTL_TASK_CLEAR_TASK_SET; 1463 break; 1464 case MSG_CLEAR_ACA: 1465 io->taskio.task_action = CTL_TASK_CLEAR_ACA; 1466 break; 1467 case MSG_QUERY_TASK: 1468 io->taskio.task_action = CTL_TASK_QUERY_TASK; 1469 break; 1470 case MSG_QUERY_TASK_SET: 1471 io->taskio.task_action = 1472 CTL_TASK_QUERY_TASK_SET; 1473 break; 1474 case MSG_QUERY_ASYNC_EVENT: 1475 io->taskio.task_action = 1476 CTL_TASK_QUERY_ASYNC_EVENT; 1477 break; 1478 case MSG_NOOP: 1479 send_ctl_io = 0; 1480 break; 1481 default: 1482 xpt_print(periph->path, 1483 "%s: unsupported INOT message 0x%x\n", 1484 __func__, inot->arg); 1485 send_ctl_io = 0; 1486 break; 1487 } 1488 break; 1489 default: 1490 xpt_print(periph->path, 1491 "%s: unsupported INOT status 0x%x\n", 1492 __func__, status); 1493 /* FALLTHROUGH */ 1494 case CAM_REQ_ABORTED: 1495 case CAM_REQ_INVALID: 1496 case CAM_DEV_NOT_THERE: 1497 case CAM_PROVIDE_FAIL: 1498 ctlfe_free_ccb(periph, done_ccb); 1499 goto out; 1500 } 1501 if (send_ctl_io != 0) { 1502 ctl_queue(io); 1503 } else { 1504 done_ccb->ccb_h.status = CAM_REQ_INPROG; 1505 done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE; 1506 xpt_action(done_ccb); 1507 } 1508 break; 1509 } 1510 case XPT_NOTIFY_ACKNOWLEDGE: 1511 /* Queue this back down to the SIM as an immediate notify. */ 1512 done_ccb->ccb_h.status = CAM_REQ_INPROG; 1513 done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY; 1514 ctlfe_requeue_ccb(periph, done_ccb, /* unlock */1); 1515 return; 1516 case XPT_SET_SIM_KNOB: 1517 case XPT_GET_SIM_KNOB: 1518 case XPT_GET_SIM_KNOB_OLD: 1519 break; 1520 default: 1521 panic("%s: unexpected CCB type %#x", __func__, 1522 done_ccb->ccb_h.func_code); 1523 break; 1524 } 1525 1526out: 1527 mtx_unlock(mtx); 1528} 1529 1530static void 1531ctlfe_onoffline(void *arg, int online) 1532{ 1533 struct ctlfe_softc *bus_softc = arg; 1534 union ccb *ccb; 1535 cam_status status; 1536 struct cam_path *path; 1537 int set_wwnn = 0; 1538 1539 status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id, 1540 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 1541 if (status != CAM_REQ_CMP) { 1542 printf("%s: unable to create path!\n", __func__); 1543 return; 1544 } 1545 ccb = xpt_alloc_ccb(); 1546 xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE); 1547 ccb->ccb_h.func_code = XPT_GET_SIM_KNOB; 1548 xpt_action(ccb); 1549 1550 /* Check whether we should change WWNs. */ 1551 if (online != 0) { 1552 if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){ 1553 printf("%s: %s current WWNN %#jx\n", __func__, 1554 bus_softc->port_name, 1555 ccb->knob.xport_specific.fc.wwnn); 1556 printf("%s: %s current WWPN %#jx\n", __func__, 1557 bus_softc->port_name, 1558 ccb->knob.xport_specific.fc.wwpn); 1559 1560 /* 1561 * If the user has specified a WWNN/WWPN, send them 1562 * down to the SIM. Otherwise, record what the SIM 1563 * has reported. 1564 */ 1565 if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn 1566 != ccb->knob.xport_specific.fc.wwnn) { 1567 ccb->knob.xport_specific.fc.wwnn = 1568 bus_softc->port.wwnn; 1569 set_wwnn = 1; 1570 } else { 1571 ctl_port_set_wwns(&bus_softc->port, 1572 true, ccb->knob.xport_specific.fc.wwnn, 1573 false, 0); 1574 } 1575 if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn 1576 != ccb->knob.xport_specific.fc.wwpn) { 1577 ccb->knob.xport_specific.fc.wwpn = 1578 bus_softc->port.wwpn; 1579 set_wwnn = 1; 1580 } else { 1581 ctl_port_set_wwns(&bus_softc->port, 1582 false, 0, 1583 true, ccb->knob.xport_specific.fc.wwpn); 1584 } 1585 } else { 1586 printf("%s: %s has no valid WWNN/WWPN\n", __func__, 1587 bus_softc->port_name); 1588 if (bus_softc->port.wwnn != 0) { 1589 ccb->knob.xport_specific.fc.wwnn = 1590 bus_softc->port.wwnn; 1591 set_wwnn = 1; 1592 } 1593 if (bus_softc->port.wwpn != 0) { 1594 ccb->knob.xport_specific.fc.wwpn = 1595 bus_softc->port.wwpn; 1596 set_wwnn = 1; 1597 } 1598 } 1599 } 1600 if (set_wwnn) { 1601 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB; 1602 ccb->knob.xport_specific.valid = KNOB_VALID_ADDRESS; 1603 xpt_action(ccb); 1604 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1605 printf("%s: %s (path id %d) failed set WWNs: %#x\n", 1606 __func__, bus_softc->port_name, bus_softc->path_id, 1607 ccb->ccb_h.status); 1608 } else { 1609 printf("%s: %s new WWNN %#jx\n", __func__, 1610 bus_softc->port_name, 1611 ccb->knob.xport_specific.fc.wwnn); 1612 printf("%s: %s new WWPN %#jx\n", __func__, 1613 bus_softc->port_name, 1614 ccb->knob.xport_specific.fc.wwpn); 1615 } 1616 } 1617 1618 /* Check whether we should change role. */ 1619 if ((ccb->knob.xport_specific.valid & KNOB_VALID_ROLE) == 0 || 1620 ((online != 0) ^ 1621 ((ccb->knob.xport_specific.fc.role & KNOB_ROLE_TARGET) != 0)) != 0) { 1622 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB; 1623 ccb->knob.xport_specific.valid = KNOB_VALID_ROLE; 1624 if (online) 1625 ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET; 1626 else 1627 ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET; 1628 xpt_action(ccb); 1629 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1630 printf("%s: %s (path id %d) failed %s target role: %#x\n", 1631 __func__, bus_softc->port_name, bus_softc->path_id, 1632 online ? "enable" : "disable", ccb->ccb_h.status); 1633 } else { 1634 printf("%s: %s (path id %d) target role %s succeeded\n", 1635 __func__, bus_softc->port_name, bus_softc->path_id, 1636 online ? "enable" : "disable"); 1637 } 1638 } 1639 1640 xpt_free_path(path); 1641 xpt_free_ccb(ccb); 1642} 1643 1644static void 1645ctlfe_online(void *arg) 1646{ 1647 struct ctlfe_softc *bus_softc; 1648 struct cam_path *path; 1649 cam_status status; 1650 struct ctlfe_lun_softc *lun_softc; 1651 struct cam_periph *periph; 1652 1653 bus_softc = (struct ctlfe_softc *)arg; 1654 1655 /* 1656 * Create the wildcard LUN before bringing the port online. 1657 */ 1658 status = xpt_create_path(&path, /*periph*/ NULL, 1659 bus_softc->path_id, CAM_TARGET_WILDCARD, 1660 CAM_LUN_WILDCARD); 1661 if (status != CAM_REQ_CMP) { 1662 printf("%s: unable to create path for wildcard periph\n", 1663 __func__); 1664 return; 1665 } 1666 1667 lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO); 1668 1669 xpt_path_lock(path); 1670 periph = cam_periph_find(path, "ctl"); 1671 if (periph != NULL) { 1672 /* We've already got a periph, no need to alloc a new one. */ 1673 xpt_path_unlock(path); 1674 xpt_free_path(path); 1675 free(lun_softc, M_CTLFE); 1676 return; 1677 } 1678 lun_softc->parent_softc = bus_softc; 1679 lun_softc->flags |= CTLFE_LUN_WILDCARD; 1680 1681 status = cam_periph_alloc(ctlferegister, 1682 ctlfeoninvalidate, 1683 ctlfecleanup, 1684 ctlfestart, 1685 "ctl", 1686 CAM_PERIPH_BIO, 1687 path, 1688 ctlfeasync, 1689 0, 1690 lun_softc); 1691 1692 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1693 const struct cam_status_entry *entry; 1694 1695 entry = cam_fetch_status_entry(status); 1696 printf("%s: CAM error %s (%#x) returned from " 1697 "cam_periph_alloc()\n", __func__, (entry != NULL) ? 1698 entry->status_text : "Unknown", status); 1699 free(lun_softc, M_CTLFE); 1700 } 1701 1702 xpt_path_unlock(path); 1703 ctlfe_onoffline(arg, /*online*/ 1); 1704 xpt_free_path(path); 1705} 1706 1707static void 1708ctlfe_offline(void *arg) 1709{ 1710 struct ctlfe_softc *bus_softc; 1711 struct cam_path *path; 1712 cam_status status; 1713 struct cam_periph *periph; 1714 1715 bus_softc = (struct ctlfe_softc *)arg; 1716 1717 ctlfe_onoffline(arg, /*online*/ 0); 1718 1719 /* 1720 * Disable the wildcard LUN for this port now that we have taken 1721 * the port offline. 1722 */ 1723 status = xpt_create_path(&path, /*periph*/ NULL, 1724 bus_softc->path_id, CAM_TARGET_WILDCARD, 1725 CAM_LUN_WILDCARD); 1726 if (status != CAM_REQ_CMP) { 1727 printf("%s: unable to create path for wildcard periph\n", 1728 __func__); 1729 return; 1730 } 1731 xpt_path_lock(path); 1732 if ((periph = cam_periph_find(path, "ctl")) != NULL) 1733 cam_periph_invalidate(periph); 1734 xpt_path_unlock(path); 1735 xpt_free_path(path); 1736} 1737 1738/* 1739 * This will get called to enable a LUN on every bus that is attached to 1740 * CTL. So we only need to create a path/periph for this particular bus. 1741 */ 1742static int 1743ctlfe_lun_enable(void *arg, int lun_id) 1744{ 1745 struct ctlfe_softc *bus_softc; 1746 struct ctlfe_lun_softc *softc; 1747 struct cam_path *path; 1748 struct cam_periph *periph; 1749 cam_status status; 1750 1751 bus_softc = (struct ctlfe_softc *)arg; 1752 if (bus_softc->hba_misc & PIM_EXTLUNS) 1753 lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id)); 1754 1755 status = xpt_create_path(&path, /*periph*/ NULL, 1756 bus_softc->path_id, bus_softc->target_id, lun_id); 1757 /* XXX KDM need some way to return status to CTL here? */ 1758 if (status != CAM_REQ_CMP) { 1759 printf("%s: could not create path, status %#x\n", __func__, 1760 status); 1761 return (1); 1762 } 1763 1764 softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO); 1765 xpt_path_lock(path); 1766 periph = cam_periph_find(path, "ctl"); 1767 if (periph != NULL) { 1768 /* We've already got a periph, no need to alloc a new one. */ 1769 xpt_path_unlock(path); 1770 xpt_free_path(path); 1771 free(softc, M_CTLFE); 1772 return (0); 1773 } 1774 softc->parent_softc = bus_softc; 1775 1776 status = cam_periph_alloc(ctlferegister, 1777 ctlfeoninvalidate, 1778 ctlfecleanup, 1779 ctlfestart, 1780 "ctl", 1781 CAM_PERIPH_BIO, 1782 path, 1783 ctlfeasync, 1784 0, 1785 softc); 1786 1787 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1788 const struct cam_status_entry *entry; 1789 1790 entry = cam_fetch_status_entry(status); 1791 printf("%s: CAM error %s (%#x) returned from " 1792 "cam_periph_alloc()\n", __func__, (entry != NULL) ? 1793 entry->status_text : "Unknown", status); 1794 free(softc, M_CTLFE); 1795 } 1796 1797 xpt_path_unlock(path); 1798 xpt_free_path(path); 1799 return (0); 1800} 1801 1802/* 1803 * This will get called when the user removes a LUN to disable that LUN 1804 * on every bus that is attached to CTL. 1805 */ 1806static int 1807ctlfe_lun_disable(void *arg, int lun_id) 1808{ 1809 struct ctlfe_softc *softc; 1810 struct ctlfe_lun_softc *lun_softc; 1811 1812 softc = (struct ctlfe_softc *)arg; 1813 if (softc->hba_misc & PIM_EXTLUNS) 1814 lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id)); 1815 1816 mtx_lock(&softc->lun_softc_mtx); 1817 STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) { 1818 struct cam_path *path; 1819 1820 path = lun_softc->periph->path; 1821 1822 if ((xpt_path_target_id(path) == softc->target_id) 1823 && (xpt_path_lun_id(path) == lun_id)) { 1824 break; 1825 } 1826 } 1827 if (lun_softc == NULL) { 1828 mtx_unlock(&softc->lun_softc_mtx); 1829 printf("%s: can't find lun %d\n", __func__, lun_id); 1830 return (1); 1831 } 1832 cam_periph_acquire(lun_softc->periph); 1833 mtx_unlock(&softc->lun_softc_mtx); 1834 1835 cam_periph_lock(lun_softc->periph); 1836 cam_periph_invalidate(lun_softc->periph); 1837 cam_periph_unlock(lun_softc->periph); 1838 cam_periph_release(lun_softc->periph); 1839 return (0); 1840} 1841 1842static void 1843ctlfe_dump_sim(struct cam_sim *sim) 1844{ 1845 1846 printf("%s%d: max dev openings: %d, max tagged dev openings: %d\n", 1847 sim->sim_name, sim->unit_number, sim->max_dev_openings, 1848 sim->max_tagged_dev_openings); 1849} 1850 1851/* 1852 * Assumes that the SIM lock is held. 1853 */ 1854static void 1855ctlfe_dump_queue(struct ctlfe_lun_softc *softc) 1856{ 1857 struct cam_periph *periph = softc->periph; 1858 struct ccb_hdr *hdr; 1859 struct ccb_getdevstats cgds; 1860 int num_items; 1861 1862 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1863 cgds.ccb_h.func_code = XPT_GDEV_STATS; 1864 xpt_action((union ccb *)&cgds); 1865 if ((cgds.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1866 xpt_print(periph->path, "devq: openings %d, active %d, " 1867 "allocated %d, queued %d, held %d\n", 1868 cgds.dev_openings, cgds.dev_active, cgds.allocated, 1869 cgds.queued, cgds.held); 1870 } 1871 1872 num_items = 0; 1873 1874 STAILQ_FOREACH(hdr, &softc->work_queue, periph_links.stqe) { 1875 union ctl_io *io = hdr->io_ptr; 1876 1877 num_items++; 1878 1879 /* 1880 * Only regular SCSI I/O is put on the work 1881 * queue, so we can print sense here. There may be no 1882 * sense if it's no the queue for a DMA, but this serves to 1883 * print out the CCB as well. 1884 * 1885 * XXX KDM switch this over to scsi_sense_print() when 1886 * CTL is merged in with CAM. 1887 */ 1888 ctl_io_error_print(io, NULL); 1889 1890 /* 1891 * Print DMA status if we are DMA_QUEUED. 1892 */ 1893 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) { 1894 xpt_print(periph->path, 1895 "Total %u, Current %u, Resid %u\n", 1896 io->scsiio.kern_total_len, 1897 io->scsiio.kern_data_len, 1898 io->scsiio.kern_data_resid); 1899 } 1900 } 1901 1902 xpt_print(periph->path, "%d requests waiting for CCBs\n", num_items); 1903 xpt_print(periph->path, "%d CTIOs outstanding\n", softc->ctios_sent); 1904} 1905 1906/* 1907 * Datamove/done routine called by CTL. Put ourselves on the queue to 1908 * receive a CCB from CAM so we can queue the continue I/O request down 1909 * to the adapter. 1910 */ 1911static void 1912ctlfe_datamove(union ctl_io *io) 1913{ 1914 union ccb *ccb; 1915 struct cam_periph *periph; 1916 struct ctlfe_lun_softc *softc; 1917 1918 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 1919 ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type)); 1920 1921 io->scsiio.ext_data_filled = 0; 1922 ccb = PRIV_CCB(io); 1923 periph = xpt_path_periph(ccb->ccb_h.path); 1924 cam_periph_lock(periph); 1925 softc = (struct ctlfe_lun_softc *)periph->softc; 1926 io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED; 1927 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 1928 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED; 1929 STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, 1930 periph_links.stqe); 1931 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1932 cam_periph_unlock(periph); 1933} 1934 1935static void 1936ctlfe_done(union ctl_io *io) 1937{ 1938 union ccb *ccb; 1939 struct cam_periph *periph; 1940 struct ctlfe_lun_softc *softc; 1941 1942 ccb = PRIV_CCB(io); 1943 periph = xpt_path_periph(ccb->ccb_h.path); 1944 cam_periph_lock(periph); 1945 softc = (struct ctlfe_lun_softc *)periph->softc; 1946 1947 if (io->io_hdr.io_type == CTL_IO_TASK) { 1948 /* 1949 * Send the notify acknowledge down to the SIM, to let it 1950 * know we processed the task management command. 1951 */ 1952 ccb->ccb_h.status = CAM_REQ_INPROG; 1953 ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE; 1954 switch (io->taskio.task_status) { 1955 case CTL_TASK_FUNCTION_COMPLETE: 1956 ccb->cna2.arg = CAM_RSP_TMF_COMPLETE; 1957 break; 1958 case CTL_TASK_FUNCTION_SUCCEEDED: 1959 ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED; 1960 ccb->ccb_h.flags |= CAM_SEND_STATUS; 1961 break; 1962 case CTL_TASK_FUNCTION_REJECTED: 1963 ccb->cna2.arg = CAM_RSP_TMF_REJECTED; 1964 ccb->ccb_h.flags |= CAM_SEND_STATUS; 1965 break; 1966 case CTL_TASK_LUN_DOES_NOT_EXIST: 1967 ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN; 1968 ccb->ccb_h.flags |= CAM_SEND_STATUS; 1969 break; 1970 case CTL_TASK_FUNCTION_NOT_SUPPORTED: 1971 ccb->cna2.arg = CAM_RSP_TMF_FAILED; 1972 ccb->ccb_h.flags |= CAM_SEND_STATUS; 1973 break; 1974 } 1975 ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8; 1976 xpt_action(ccb); 1977 } else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) { 1978 ctlfe_requeue_ccb(periph, ccb, /* unlock */1); 1979 return; 1980 } else { 1981 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED; 1982 STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, 1983 periph_links.stqe); 1984 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1985 } 1986 1987 cam_periph_unlock(periph); 1988} 1989 1990static void 1991ctlfe_dump(void) 1992{ 1993 struct ctlfe_softc *bus_softc; 1994 struct ctlfe_lun_softc *lun_softc; 1995 1996 STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) { 1997 ctlfe_dump_sim(bus_softc->sim); 1998 STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) 1999 ctlfe_dump_queue(lun_softc); 2000 } 2001} 2002