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