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