scsi_ctl.c revision 268677
1229997Sken/*- 2229997Sken * Copyright (c) 2008, 2009 Silicon Graphics International Corp. 3229997Sken * All rights reserved. 4229997Sken * 5229997Sken * Redistribution and use in source and binary forms, with or without 6229997Sken * modification, are permitted provided that the following conditions 7229997Sken * are met: 8229997Sken * 1. Redistributions of source code must retain the above copyright 9229997Sken * notice, this list of conditions, and the following disclaimer, 10229997Sken * without modification. 11229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12229997Sken * substantially similar to the "NO WARRANTY" disclaimer below 13229997Sken * ("Disclaimer") and any redistribution must be conditioned upon 14229997Sken * including a substantially similar Disclaimer requirement for further 15229997Sken * binary redistribution. 16229997Sken * 17229997Sken * NO WARRANTY 18229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28229997Sken * POSSIBILITY OF SUCH DAMAGES. 29229997Sken * 30229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $ 31229997Sken */ 32229997Sken/* 33229997Sken * Peripheral driver interface between CAM and CTL (CAM Target Layer). 34229997Sken * 35229997Sken * Author: Ken Merry <ken@FreeBSD.org> 36229997Sken */ 37229997Sken 38229997Sken#include <sys/cdefs.h> 39229997Sken__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/scsi_ctl.c 268677 2014-07-15 16:57:30Z mav $"); 40229997Sken 41229997Sken#include <sys/param.h> 42229997Sken#include <sys/queue.h> 43229997Sken#include <sys/systm.h> 44229997Sken#include <sys/kernel.h> 45229997Sken#include <sys/lock.h> 46229997Sken#include <sys/mutex.h> 47229997Sken#include <sys/condvar.h> 48229997Sken#include <sys/malloc.h> 49229997Sken#include <sys/bus.h> 50229997Sken#include <sys/endian.h> 51229997Sken#include <sys/sbuf.h> 52229997Sken#include <sys/sysctl.h> 53229997Sken#include <sys/types.h> 54229997Sken#include <sys/systm.h> 55229997Sken#include <machine/bus.h> 56229997Sken 57229997Sken#include <cam/cam.h> 58229997Sken#include <cam/cam_ccb.h> 59229997Sken#include <cam/cam_periph.h> 60229997Sken#include <cam/cam_queue.h> 61229997Sken#include <cam/cam_xpt_periph.h> 62229997Sken#include <cam/cam_debug.h> 63229997Sken#include <cam/cam_sim.h> 64229997Sken#include <cam/cam_xpt.h> 65229997Sken 66229997Sken#include <cam/scsi/scsi_all.h> 67229997Sken#include <cam/scsi/scsi_message.h> 68229997Sken 69229997Sken#include <cam/ctl/ctl_io.h> 70229997Sken#include <cam/ctl/ctl.h> 71229997Sken#include <cam/ctl/ctl_frontend.h> 72229997Sken#include <cam/ctl/ctl_util.h> 73229997Sken#include <cam/ctl/ctl_error.h> 74229997Sken 75229997Skentypedef enum { 76260387Sscottl CTLFE_CCB_DEFAULT = 0x00 77229997Sken} ctlfe_ccb_types; 78229997Sken 79229997Skenstruct ctlfe_softc { 80268677Smav struct ctl_port port; 81229997Sken path_id_t path_id; 82265641Smav u_int maxio; 83229997Sken struct cam_sim *sim; 84229997Sken char port_name[DEV_IDLEN]; 85260387Sscottl struct mtx lun_softc_mtx; 86229997Sken STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list; 87229997Sken STAILQ_ENTRY(ctlfe_softc) links; 88229997Sken}; 89229997Sken 90229997SkenSTAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list; 91229997Skenstruct mtx ctlfe_list_mtx; 92229997Skenstatic char ctlfe_mtx_desc[] = "ctlfelist"; 93229997Skenstatic int ctlfe_dma_enabled = 1; 94229997Sken#ifdef CTLFE_INIT_ENABLE 95229997Skenstatic int ctlfe_max_targets = 1; 96229997Skenstatic int ctlfe_num_targets = 0; 97229997Sken#endif 98229997Sken 99229997Skentypedef enum { 100229997Sken CTLFE_LUN_NONE = 0x00, 101229997Sken CTLFE_LUN_WILDCARD = 0x01 102229997Sken} ctlfe_lun_flags; 103229997Sken 104229997Skenstruct ctlfe_lun_softc { 105229997Sken struct ctlfe_softc *parent_softc; 106229997Sken struct cam_periph *periph; 107229997Sken ctlfe_lun_flags flags; 108229997Sken struct callout dma_callout; 109229997Sken uint64_t ccbs_alloced; 110229997Sken uint64_t ccbs_freed; 111229997Sken uint64_t ctios_sent; 112229997Sken uint64_t ctios_returned; 113229997Sken uint64_t atios_sent; 114229997Sken uint64_t atios_returned; 115229997Sken uint64_t inots_sent; 116229997Sken uint64_t inots_returned; 117229997Sken /* bus_dma_tag_t dma_tag; */ 118229997Sken TAILQ_HEAD(, ccb_hdr) work_queue; 119229997Sken STAILQ_ENTRY(ctlfe_lun_softc) links; 120229997Sken}; 121229997Sken 122229997Skentypedef enum { 123229997Sken CTLFE_CMD_NONE = 0x00, 124229997Sken CTLFE_CMD_PIECEWISE = 0x01 125229997Sken} ctlfe_cmd_flags; 126229997Sken 127229997Sken/* 128229997Sken * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h. 129229997Sken * Currently that is 600 bytes. 130229997Sken */ 131229997Skenstruct ctlfe_lun_cmd_info { 132229997Sken int cur_transfer_index; 133265641Smav size_t cur_transfer_off; 134229997Sken ctlfe_cmd_flags flags; 135229997Sken /* 136229997Sken * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16 137229997Sken * bytes on amd64. So with 32 elements, this is 256 bytes on 138229997Sken * i386 and 512 bytes on amd64. 139229997Sken */ 140265641Smav#define CTLFE_MAX_SEGS 32 141265641Smav bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS]; 142229997Sken}; 143229997Sken 144229997Sken/* 145229997Sken * When we register the adapter/bus, request that this many ctl_ios be 146229997Sken * allocated. This should be the maximum supported by the adapter, but we 147229997Sken * currently don't have a way to get that back from the path inquiry. 148229997Sken * XXX KDM add that to the path inquiry. 149229997Sken */ 150229997Sken#define CTLFE_REQ_CTL_IO 4096 151229997Sken/* 152229997Sken * Number of Accept Target I/O CCBs to allocate and queue down to the 153229997Sken * adapter per LUN. 154229997Sken * XXX KDM should this be controlled by CTL? 155229997Sken */ 156229997Sken#define CTLFE_ATIO_PER_LUN 1024 157229997Sken/* 158229997Sken * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to 159229997Sken * allocate and queue down to the adapter per LUN. 160229997Sken * XXX KDM should this be controlled by CTL? 161229997Sken */ 162229997Sken#define CTLFE_IN_PER_LUN 1024 163229997Sken 164229997Sken/* 165229997Sken * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending 166229997Sken * status to the initiator. The SIM is expected to have its own timeouts, 167229997Sken * so we're not putting this timeout around the CCB execution time. The 168229997Sken * SIM should timeout and let us know if it has an issue. 169229997Sken */ 170229997Sken#define CTLFE_DMA_TIMEOUT 60 171229997Sken 172229997Sken/* 173229997Sken * Turn this on to enable extra debugging prints. 174229997Sken */ 175229997Sken#if 0 176229997Sken#define CTLFE_DEBUG 177229997Sken#endif 178229997Sken 179229997Sken/* 180229997Sken * Use randomly assigned WWNN/WWPN values. This is to work around an issue 181229997Sken * in the FreeBSD initiator that makes it unable to rescan the target if 182229997Sken * the target gets rebooted and the WWNN/WWPN stay the same. 183229997Sken */ 184229997Sken#if 0 185229997Sken#define RANDOM_WWNN 186229997Sken#endif 187229997Sken 188229997SkenSYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW, 189229997Sken &ctlfe_dma_enabled, 0, "DMA enabled"); 190229997SkenMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface"); 191229997Sken 192229997Sken#define ccb_type ppriv_field0 193229997Sken/* This is only used in the ATIO */ 194229997Sken#define io_ptr ppriv_ptr1 195229997Sken 196229997Sken/* This is only used in the CTIO */ 197229997Sken#define ccb_atio ppriv_ptr1 198229997Sken 199229997Skenint ctlfeinitialize(void); 200229997Skenvoid ctlfeshutdown(void); 201268677Smavstatic periph_init_t ctlfeperiphinit; 202229997Skenstatic void ctlfeasync(void *callback_arg, uint32_t code, 203229997Sken struct cam_path *path, void *arg); 204229997Skenstatic periph_ctor_t ctlferegister; 205229997Skenstatic periph_oninv_t ctlfeoninvalidate; 206229997Skenstatic periph_dtor_t ctlfecleanup; 207229997Skenstatic periph_start_t ctlfestart; 208229997Skenstatic void ctlfedone(struct cam_periph *periph, 209229997Sken union ccb *done_ccb); 210229997Sken 211229997Skenstatic void ctlfe_onoffline(void *arg, int online); 212229997Skenstatic void ctlfe_online(void *arg); 213229997Skenstatic void ctlfe_offline(void *arg); 214229997Skenstatic int ctlfe_lun_enable(void *arg, struct ctl_id targ_id, 215229997Sken int lun_id); 216229997Skenstatic int ctlfe_lun_disable(void *arg, struct ctl_id targ_id, 217229997Sken int lun_id); 218229997Skenstatic void ctlfe_dump_sim(struct cam_sim *sim); 219229997Skenstatic void ctlfe_dump_queue(struct ctlfe_lun_softc *softc); 220229997Skenstatic void ctlfe_dma_timeout(void *arg); 221229997Skenstatic void ctlfe_datamove_done(union ctl_io *io); 222229997Skenstatic void ctlfe_dump(void); 223229997Sken 224229997Skenstatic struct periph_driver ctlfe_driver = 225229997Sken{ 226268677Smav ctlfeperiphinit, "ctl", 227229997Sken TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0 228229997Sken}; 229229997Sken 230268677Smavstatic struct ctl_frontend ctlfe_frontend = 231268677Smav{ 232268677Smav .name = "camtarget", 233268677Smav .init = ctlfeinitialize, 234268677Smav .fe_dump = ctlfe_dump, 235268677Smav .shutdown = ctlfeshutdown, 236249009Strasz}; 237268677SmavCTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend); 238249009Strasz 239229997Skenextern struct ctl_softc *control_softc; 240229997Sken 241229997Skenvoid 242229997Skenctlfeshutdown(void) 243229997Sken{ 244229997Sken return; 245229997Sken} 246229997Sken 247268677Smavint 248268677Smavctlfeinitialize(void) 249229997Sken{ 250229997Sken 251229997Sken STAILQ_INIT(&ctlfe_softc_list); 252229997Sken mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF); 253268677Smav periphdriver_register(&ctlfe_driver); 254268677Smav return (0); 255268677Smav} 256229997Sken 257268677Smavvoid 258268677Smavctlfeperiphinit(void) 259268677Smav{ 260268677Smav cam_status status; 261229997Sken 262229997Sken status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED | 263229997Sken AC_CONTRACT, ctlfeasync, NULL, NULL); 264229997Sken if (status != CAM_REQ_CMP) { 265229997Sken printf("ctl: Failed to attach async callback due to CAM " 266229997Sken "status 0x%x!\n", status); 267229997Sken } 268229997Sken} 269229997Sken 270229997Skenstatic void 271229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg) 272229997Sken{ 273229997Sken 274229997Sken#ifdef CTLFEDEBUG 275229997Sken printf("%s: entered\n", __func__); 276229997Sken#endif 277229997Sken 278229997Sken /* 279229997Sken * When a new path gets registered, and it is capable of target 280229997Sken * mode, go ahead and attach. Later on, we may need to be more 281229997Sken * selective, but for now this will be sufficient. 282229997Sken */ 283229997Sken switch (code) { 284229997Sken case AC_PATH_REGISTERED: { 285268677Smav struct ctl_port *port; 286229997Sken struct ctlfe_softc *bus_softc; 287229997Sken struct ccb_pathinq *cpi; 288229997Sken int retval; 289229997Sken 290229997Sken cpi = (struct ccb_pathinq *)arg; 291229997Sken 292229997Sken /* Don't attach if it doesn't support target mode */ 293229997Sken if ((cpi->target_sprt & PIT_PROCESSOR) == 0) { 294230033Sken#ifdef CTLFEDEBUG 295229997Sken printf("%s: SIM %s%d doesn't support target mode\n", 296229997Sken __func__, cpi->dev_name, cpi->unit_number); 297230033Sken#endif 298229997Sken break; 299229997Sken } 300229997Sken 301229997Sken#ifdef CTLFE_INIT_ENABLE 302229997Sken if (ctlfe_num_targets >= ctlfe_max_targets) { 303229997Sken union ccb *ccb; 304229997Sken 305229997Sken ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, 306229997Sken M_NOWAIT | M_ZERO); 307229997Sken if (ccb == NULL) { 308229997Sken printf("%s: unable to malloc CCB!\n", __func__); 309229997Sken return; 310229997Sken } 311260387Sscottl xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE); 312229997Sken 313229997Sken ccb->ccb_h.func_code = XPT_SET_SIM_KNOB; 314229997Sken ccb->knob.xport_specific.valid = KNOB_VALID_ROLE; 315229997Sken ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR; 316229997Sken 317229997Sken xpt_action(ccb); 318229997Sken 319229997Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 320229997Sken CAM_REQ_CMP) { 321229997Sken printf("%s: SIM %s%d (path id %d) initiator " 322229997Sken "enable failed with status %#x\n", 323229997Sken __func__, cpi->dev_name, 324229997Sken cpi->unit_number, cpi->ccb_h.path_id, 325229997Sken ccb->ccb_h.status); 326229997Sken } else { 327229997Sken printf("%s: SIM %s%d (path id %d) initiator " 328229997Sken "enable succeeded\n", 329229997Sken __func__, cpi->dev_name, 330229997Sken cpi->unit_number, cpi->ccb_h.path_id); 331229997Sken } 332229997Sken 333229997Sken free(ccb, M_TEMP); 334229997Sken 335229997Sken break; 336229997Sken } else { 337229997Sken ctlfe_num_targets++; 338229997Sken } 339229997Sken 340229997Sken printf("%s: ctlfe_num_targets = %d\n", __func__, 341229997Sken ctlfe_num_targets); 342229997Sken#endif /* CTLFE_INIT_ENABLE */ 343229997Sken 344229997Sken /* 345229997Sken * We're in an interrupt context here, so we have to 346229997Sken * use M_NOWAIT. Of course this means trouble if we 347229997Sken * can't allocate memory. 348229997Sken */ 349229997Sken bus_softc = malloc(sizeof(*bus_softc), M_CTLFE, 350229997Sken M_NOWAIT | M_ZERO); 351229997Sken if (bus_softc == NULL) { 352229997Sken printf("%s: unable to malloc %zd bytes for softc\n", 353229997Sken __func__, sizeof(*bus_softc)); 354229997Sken return; 355229997Sken } 356229997Sken 357229997Sken bus_softc->path_id = cpi->ccb_h.path_id; 358260387Sscottl bus_softc->sim = xpt_path_sim(path); 359265641Smav if (cpi->maxio != 0) 360265641Smav bus_softc->maxio = cpi->maxio; 361265641Smav else 362265641Smav bus_softc->maxio = DFLTPHYS; 363260387Sscottl mtx_init(&bus_softc->lun_softc_mtx, "LUN softc mtx", NULL, 364260387Sscottl MTX_DEF); 365229997Sken STAILQ_INIT(&bus_softc->lun_softc_list); 366229997Sken 367268677Smav port = &bus_softc->port; 368268677Smav port->frontend = &ctlfe_frontend; 369229997Sken 370229997Sken /* 371229997Sken * XXX KDM should we be more accurate here ? 372229997Sken */ 373229997Sken if (cpi->transport == XPORT_FC) 374268677Smav port->port_type = CTL_PORT_FC; 375229997Sken else 376268677Smav port->port_type = CTL_PORT_SCSI; 377229997Sken 378229997Sken /* XXX KDM what should the real number be here? */ 379268677Smav port->num_requested_ctl_io = 4096; 380229997Sken snprintf(bus_softc->port_name, sizeof(bus_softc->port_name), 381229997Sken "%s%d", cpi->dev_name, cpi->unit_number); 382229997Sken /* 383229997Sken * XXX KDM it would be nice to allocate storage in the 384229997Sken * frontend structure itself. 385229997Sken */ 386268677Smav port->port_name = bus_softc->port_name; 387268677Smav port->physical_port = cpi->unit_number; 388268677Smav port->virtual_port = cpi->bus_id; 389268677Smav port->port_online = ctlfe_online; 390268677Smav port->port_offline = ctlfe_offline; 391268677Smav port->onoff_arg = bus_softc; 392268677Smav port->lun_enable = ctlfe_lun_enable; 393268677Smav port->lun_disable = ctlfe_lun_disable; 394268677Smav port->targ_lun_arg = bus_softc; 395268677Smav port->fe_datamove = ctlfe_datamove_done; 396268677Smav port->fe_done = ctlfe_datamove_done; 397229997Sken /* 398229997Sken * XXX KDM the path inquiry doesn't give us the maximum 399229997Sken * number of targets supported. 400229997Sken */ 401268677Smav port->max_targets = cpi->max_target; 402268677Smav port->max_target_id = cpi->max_target; 403229997Sken 404229997Sken /* 405229997Sken * XXX KDM need to figure out whether we're the master or 406229997Sken * slave. 407229997Sken */ 408230033Sken#ifdef CTLFEDEBUG 409268677Smav printf("%s: calling ctl_port_register() for %s%d\n", 410229997Sken __func__, cpi->dev_name, cpi->unit_number); 411230033Sken#endif 412268677Smav retval = ctl_port_register(port, /*master_SC*/ 1); 413229997Sken if (retval != 0) { 414268677Smav printf("%s: ctl_port_register() failed with " 415229997Sken "error %d!\n", __func__, retval); 416260387Sscottl mtx_destroy(&bus_softc->lun_softc_mtx); 417229997Sken free(bus_softc, M_CTLFE); 418229997Sken break; 419229997Sken } else { 420229997Sken mtx_lock(&ctlfe_list_mtx); 421229997Sken STAILQ_INSERT_TAIL(&ctlfe_softc_list, bus_softc, links); 422229997Sken mtx_unlock(&ctlfe_list_mtx); 423229997Sken } 424229997Sken 425245228Sken break; 426245228Sken } 427245228Sken case AC_PATH_DEREGISTERED: { 428245228Sken struct ctlfe_softc *softc = NULL; 429245228Sken 430245228Sken mtx_lock(&ctlfe_list_mtx); 431245228Sken STAILQ_FOREACH(softc, &ctlfe_softc_list, links) { 432245228Sken if (softc->path_id == xpt_path_path_id(path)) { 433245228Sken STAILQ_REMOVE(&ctlfe_softc_list, softc, 434245228Sken ctlfe_softc, links); 435245228Sken break; 436245228Sken } 437229997Sken } 438245228Sken mtx_unlock(&ctlfe_list_mtx); 439245228Sken 440245228Sken if (softc != NULL) { 441245228Sken /* 442245228Sken * XXX KDM are we certain at this point that there 443245228Sken * are no outstanding commands for this frontend? 444245228Sken */ 445268677Smav ctl_port_deregister(&softc->port); 446260387Sscottl mtx_destroy(&softc->lun_softc_mtx); 447245228Sken free(softc, M_CTLFE); 448229997Sken } 449229997Sken break; 450229997Sken } 451229997Sken case AC_CONTRACT: { 452229997Sken struct ac_contract *ac; 453229997Sken 454229997Sken ac = (struct ac_contract *)arg; 455229997Sken 456229997Sken switch (ac->contract_number) { 457229997Sken case AC_CONTRACT_DEV_CHG: { 458229997Sken struct ac_device_changed *dev_chg; 459229997Sken struct ctlfe_softc *softc; 460229997Sken int retval, found; 461229997Sken 462229997Sken dev_chg = (struct ac_device_changed *)ac->contract_data; 463229997Sken 464236426Smjacob printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n", 465229997Sken __func__, dev_chg->wwpn, dev_chg->port, 466229997Sken xpt_path_path_id(path), dev_chg->target, 467229997Sken (dev_chg->arrived == 0) ? "left" : "arrived"); 468229997Sken 469229997Sken found = 0; 470229997Sken 471229997Sken mtx_lock(&ctlfe_list_mtx); 472229997Sken STAILQ_FOREACH(softc, &ctlfe_softc_list, links) { 473229997Sken if (softc->path_id == xpt_path_path_id(path)) { 474229997Sken found = 1; 475229997Sken break; 476229997Sken } 477229997Sken } 478229997Sken mtx_unlock(&ctlfe_list_mtx); 479229997Sken 480229997Sken if (found == 0) { 481229997Sken printf("%s: CTL port for CAM path %u not " 482229997Sken "found!\n", __func__, 483229997Sken xpt_path_path_id(path)); 484229997Sken break; 485229997Sken } 486229997Sken if (dev_chg->arrived != 0) { 487229997Sken retval = ctl_add_initiator(dev_chg->wwpn, 488268677Smav softc->port.targ_port, dev_chg->target); 489229997Sken } else { 490229997Sken retval = ctl_remove_initiator( 491268677Smav softc->port.targ_port, dev_chg->target); 492229997Sken } 493229997Sken 494229997Sken if (retval != 0) { 495229997Sken printf("%s: could not %s port %d iid %u " 496229997Sken "WWPN %#jx!\n", __func__, 497229997Sken (dev_chg->arrived != 0) ? "add" : 498268677Smav "remove", softc->port.targ_port, 499229997Sken dev_chg->target, 500229997Sken (uintmax_t)dev_chg->wwpn); 501229997Sken } 502229997Sken break; 503229997Sken } 504229997Sken default: 505229997Sken printf("%s: unsupported contract number %ju\n", 506229997Sken __func__, (uintmax_t)ac->contract_number); 507229997Sken break; 508229997Sken } 509229997Sken break; 510229997Sken } 511229997Sken default: 512229997Sken break; 513229997Sken } 514229997Sken} 515229997Sken 516229997Skenstatic cam_status 517229997Skenctlferegister(struct cam_periph *periph, void *arg) 518229997Sken{ 519229997Sken struct ctlfe_softc *bus_softc; 520229997Sken struct ctlfe_lun_softc *softc; 521229997Sken union ccb en_lun_ccb; 522229997Sken cam_status status; 523229997Sken int i; 524229997Sken 525229997Sken softc = (struct ctlfe_lun_softc *)arg; 526229997Sken bus_softc = softc->parent_softc; 527229997Sken 528229997Sken TAILQ_INIT(&softc->work_queue); 529229997Sken softc->periph = periph; 530229997Sken 531260387Sscottl callout_init_mtx(&softc->dma_callout, xpt_path_mtx(periph->path), 532260387Sscottl /*flags*/ 0); 533229997Sken periph->softc = softc; 534229997Sken 535242174Smav xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE); 536229997Sken en_lun_ccb.ccb_h.func_code = XPT_EN_LUN; 537229997Sken en_lun_ccb.cel.grp6_len = 0; 538229997Sken en_lun_ccb.cel.grp7_len = 0; 539229997Sken en_lun_ccb.cel.enable = 1; 540229997Sken xpt_action(&en_lun_ccb); 541229997Sken status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK); 542229997Sken if (status != CAM_REQ_CMP) { 543229997Sken xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n", 544229997Sken __func__, en_lun_ccb.ccb_h.status); 545229997Sken return (status); 546229997Sken } 547229997Sken 548229997Sken status = CAM_REQ_CMP; 549229997Sken 550229997Sken for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) { 551229997Sken union ccb *new_ccb; 552229997Sken 553229997Sken new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE, 554236426Smjacob M_ZERO|M_NOWAIT); 555229997Sken if (new_ccb == NULL) { 556229997Sken status = CAM_RESRC_UNAVAIL; 557229997Sken break; 558229997Sken } 559229997Sken xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1); 560229997Sken new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO; 561229997Sken new_ccb->ccb_h.cbfcnp = ctlfedone; 562260387Sscottl new_ccb->ccb_h.flags |= CAM_UNLOCKED; 563229997Sken xpt_action(new_ccb); 564229997Sken softc->atios_sent++; 565229997Sken status = new_ccb->ccb_h.status; 566229997Sken if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 567229997Sken free(new_ccb, M_CTLFE); 568229997Sken break; 569229997Sken } 570229997Sken } 571229997Sken 572229997Sken status = cam_periph_acquire(periph); 573229997Sken if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 574229997Sken xpt_print(periph->path, "%s: could not acquire reference " 575229997Sken "count, status = %#x\n", __func__, status); 576229997Sken return (status); 577229997Sken } 578229997Sken 579229997Sken if (i == 0) { 580229997Sken xpt_print(periph->path, "%s: could not allocate ATIO CCBs, " 581229997Sken "status 0x%x\n", __func__, status); 582229997Sken return (CAM_REQ_CMP_ERR); 583229997Sken } 584229997Sken 585229997Sken for (i = 0; i < CTLFE_IN_PER_LUN; i++) { 586229997Sken union ccb *new_ccb; 587229997Sken 588229997Sken new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE, 589236426Smjacob M_ZERO|M_NOWAIT); 590229997Sken if (new_ccb == NULL) { 591229997Sken status = CAM_RESRC_UNAVAIL; 592229997Sken break; 593229997Sken } 594229997Sken 595229997Sken xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1); 596229997Sken new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY; 597229997Sken new_ccb->ccb_h.cbfcnp = ctlfedone; 598260387Sscottl new_ccb->ccb_h.flags |= CAM_UNLOCKED; 599229997Sken xpt_action(new_ccb); 600229997Sken softc->inots_sent++; 601229997Sken status = new_ccb->ccb_h.status; 602237601Sken if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 603237601Sken /* 604237601Sken * Note that we don't free the CCB here. If the 605237601Sken * status is not CAM_REQ_INPROG, then we're 606237601Sken * probably talking to a SIM that says it is 607237601Sken * target-capable but doesn't support the 608237601Sken * XPT_IMMEDIATE_NOTIFY CCB. i.e. it supports the 609237601Sken * older API. In that case, it'll call xpt_done() 610237601Sken * on the CCB, and we need to free it in our done 611237601Sken * routine as a result. 612237601Sken */ 613229997Sken break; 614229997Sken } 615229997Sken } 616237601Sken if ((i == 0) 617237601Sken || (status != CAM_REQ_INPROG)) { 618229997Sken xpt_print(periph->path, "%s: could not allocate immediate " 619229997Sken "notify CCBs, status 0x%x\n", __func__, status); 620229997Sken return (CAM_REQ_CMP_ERR); 621229997Sken } 622229997Sken return (CAM_REQ_CMP); 623229997Sken} 624229997Sken 625229997Skenstatic void 626229997Skenctlfeoninvalidate(struct cam_periph *periph) 627229997Sken{ 628229997Sken union ccb en_lun_ccb; 629229997Sken cam_status status; 630260387Sscottl struct ctlfe_softc *bus_softc; 631229997Sken struct ctlfe_lun_softc *softc; 632229997Sken 633229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 634229997Sken 635242174Smav xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE); 636229997Sken en_lun_ccb.ccb_h.func_code = XPT_EN_LUN; 637229997Sken en_lun_ccb.cel.grp6_len = 0; 638229997Sken en_lun_ccb.cel.grp7_len = 0; 639229997Sken en_lun_ccb.cel.enable = 0; 640229997Sken xpt_action(&en_lun_ccb); 641229997Sken status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK); 642229997Sken if (status != CAM_REQ_CMP) { 643229997Sken xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n", 644229997Sken __func__, en_lun_ccb.ccb_h.status); 645229997Sken /* 646229997Sken * XXX KDM what do we do now? 647229997Sken */ 648229997Sken } 649229997Sken xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju " 650229997Sken "INOTs outstanding, %d refs\n", softc->atios_sent - 651229997Sken softc->atios_returned, softc->inots_sent - 652229997Sken softc->inots_returned, periph->refcount); 653260387Sscottl 654260387Sscottl bus_softc = softc->parent_softc; 655260387Sscottl mtx_lock(&bus_softc->lun_softc_mtx); 656260387Sscottl STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links); 657260387Sscottl mtx_unlock(&bus_softc->lun_softc_mtx); 658229997Sken} 659229997Sken 660229997Skenstatic void 661229997Skenctlfecleanup(struct cam_periph *periph) 662229997Sken{ 663229997Sken struct ctlfe_lun_softc *softc; 664229997Sken 665229997Sken xpt_print(periph->path, "%s: Called\n", __func__); 666229997Sken 667229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 668229997Sken 669229997Sken /* 670229997Sken * XXX KDM is there anything else that needs to be done here? 671229997Sken */ 672245228Sken 673245228Sken callout_stop(&softc->dma_callout); 674245228Sken 675229997Sken free(softc, M_CTLFE); 676229997Sken} 677229997Sken 678229997Skenstatic void 679265641Smavctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io, 680265641Smav ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len, 681265641Smav u_int16_t *sglist_cnt) 682265641Smav{ 683265641Smav struct ctlfe_softc *bus_softc; 684265641Smav struct ctlfe_lun_cmd_info *cmd_info; 685265641Smav struct ctl_sg_entry *ctl_sglist; 686265641Smav bus_dma_segment_t *cam_sglist; 687265641Smav size_t off; 688265641Smav int i, idx; 689265641Smav 690265641Smav cmd_info = (struct ctlfe_lun_cmd_info *)io->io_hdr.port_priv; 691265641Smav bus_softc = softc->parent_softc; 692265641Smav 693265641Smav /* 694265641Smav * Set the direction, relative to the initiator. 695265641Smav */ 696265641Smav *flags &= ~CAM_DIR_MASK; 697265641Smav if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 698265641Smav *flags |= CAM_DIR_IN; 699265641Smav else 700265641Smav *flags |= CAM_DIR_OUT; 701265641Smav 702265641Smav *flags &= ~CAM_DATA_MASK; 703265641Smav idx = cmd_info->cur_transfer_index; 704265641Smav off = cmd_info->cur_transfer_off; 705265641Smav cmd_info->flags &= ~CTLFE_CMD_PIECEWISE; 706265641Smav if (io->scsiio.kern_sg_entries == 0) { 707265641Smav /* No S/G list. */ 708265641Smav *data_ptr = io->scsiio.kern_data_ptr + off; 709265641Smav if (io->scsiio.kern_data_len - off <= bus_softc->maxio) { 710265641Smav *dxfer_len = io->scsiio.kern_data_len - off; 711265641Smav } else { 712265641Smav *dxfer_len = bus_softc->maxio; 713265641Smav cmd_info->cur_transfer_index = -1; 714265641Smav cmd_info->cur_transfer_off = bus_softc->maxio; 715265641Smav cmd_info->flags |= CTLFE_CMD_PIECEWISE; 716265641Smav } 717265641Smav *sglist_cnt = 0; 718265641Smav 719265641Smav if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) 720265641Smav *flags |= CAM_DATA_PADDR; 721265641Smav else 722265641Smav *flags |= CAM_DATA_VADDR; 723265641Smav } else { 724265641Smav /* S/G list with physical or virtual pointers. */ 725265641Smav ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 726265641Smav cam_sglist = cmd_info->cam_sglist; 727265641Smav *dxfer_len = 0; 728265641Smav for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) { 729265641Smav cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off; 730265641Smav if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) { 731265641Smav cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off; 732265641Smav *dxfer_len += cam_sglist[i].ds_len; 733265641Smav } else { 734265641Smav cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len; 735265641Smav cmd_info->cur_transfer_index = idx + i; 736265641Smav cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off; 737265641Smav cmd_info->flags |= CTLFE_CMD_PIECEWISE; 738265641Smav *dxfer_len += cam_sglist[i].ds_len; 739265641Smav if (ctl_sglist[i].len != 0) 740265641Smav i++; 741265641Smav break; 742265641Smav } 743265641Smav if (i == (CTLFE_MAX_SEGS - 1) && 744265641Smav idx + i < (io->scsiio.kern_sg_entries - 1)) { 745265641Smav cmd_info->cur_transfer_index = idx + i + 1; 746265641Smav cmd_info->cur_transfer_off = 0; 747265641Smav cmd_info->flags |= CTLFE_CMD_PIECEWISE; 748265641Smav i++; 749265641Smav break; 750265641Smav } 751265641Smav off = 0; 752265641Smav } 753265641Smav *sglist_cnt = i; 754265641Smav if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) 755265641Smav *flags |= CAM_DATA_SG_PADDR; 756265641Smav else 757265641Smav *flags |= CAM_DATA_SG; 758265641Smav *data_ptr = (uint8_t *)cam_sglist; 759265641Smav } 760265641Smav} 761265641Smav 762265641Smavstatic void 763229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb) 764229997Sken{ 765229997Sken struct ctlfe_lun_softc *softc; 766229997Sken struct ccb_hdr *ccb_h; 767229997Sken 768229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 769229997Sken 770229997Sken softc->ccbs_alloced++; 771229997Sken 772245228Sken start_ccb->ccb_h.ccb_type = CTLFE_CCB_DEFAULT; 773245228Sken 774229997Sken ccb_h = TAILQ_FIRST(&softc->work_queue); 775260387Sscottl if (ccb_h == NULL) { 776229997Sken softc->ccbs_freed++; 777229997Sken xpt_release_ccb(start_ccb); 778229997Sken } else { 779229997Sken struct ccb_accept_tio *atio; 780229997Sken struct ccb_scsiio *csio; 781229997Sken uint8_t *data_ptr; 782229997Sken uint32_t dxfer_len; 783229997Sken ccb_flags flags; 784229997Sken union ctl_io *io; 785229997Sken uint8_t scsi_status; 786229997Sken 787229997Sken /* Take the ATIO off the work queue */ 788229997Sken TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe); 789229997Sken atio = (struct ccb_accept_tio *)ccb_h; 790229997Sken io = (union ctl_io *)ccb_h->io_ptr; 791229997Sken csio = &start_ccb->csio; 792229997Sken 793229997Sken flags = atio->ccb_h.flags & 794229997Sken (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK); 795229997Sken 796229997Sken if ((io == NULL) 797229997Sken || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 798229997Sken /* 799229997Sken * We're done, send status back. 800229997Sken */ 801229997Sken flags |= CAM_SEND_STATUS; 802229997Sken if (io == NULL) { 803229997Sken scsi_status = SCSI_STATUS_BUSY; 804229997Sken csio->sense_len = 0; 805229997Sken } else if ((io->io_hdr.status & CTL_STATUS_MASK) == 806229997Sken CTL_CMD_ABORTED) { 807229997Sken io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED; 808229997Sken 809229997Sken /* 810229997Sken * If this command was aborted, we don't 811229997Sken * need to send status back to the SIM. 812229997Sken * Just free the CTIO and ctl_io, and 813229997Sken * recycle the ATIO back to the SIM. 814229997Sken */ 815229997Sken xpt_print(periph->path, "%s: aborted " 816229997Sken "command 0x%04x discarded\n", 817229997Sken __func__, io->scsiio.tag_num); 818229997Sken ctl_free_io(io); 819229997Sken /* 820229997Sken * For a wildcard attachment, commands can 821229997Sken * come in with a specific target/lun. Reset 822229997Sken * the target and LUN fields back to the 823229997Sken * wildcard values before we send them back 824229997Sken * down to the SIM. The SIM has a wildcard 825229997Sken * LUN enabled, not whatever target/lun 826229997Sken * these happened to be. 827229997Sken */ 828229997Sken if (softc->flags & CTLFE_LUN_WILDCARD) { 829229997Sken atio->ccb_h.target_id = 830229997Sken CAM_TARGET_WILDCARD; 831229997Sken atio->ccb_h.target_lun = 832229997Sken CAM_LUN_WILDCARD; 833229997Sken } 834229997Sken 835229997Sken if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) { 836229997Sken cam_release_devq(periph->path, 837229997Sken /*relsim_flags*/0, 838229997Sken /*reduction*/0, 839229997Sken /*timeout*/0, 840229997Sken /*getcount_only*/0); 841229997Sken atio->ccb_h.status &= ~CAM_DEV_QFRZN; 842229997Sken } 843229997Sken 844229997Sken ccb_h = TAILQ_FIRST(&softc->work_queue); 845229997Sken 846229997Sken if (atio->ccb_h.func_code != 847229997Sken XPT_ACCEPT_TARGET_IO) { 848229997Sken xpt_print(periph->path, "%s: func_code " 849229997Sken "is %#x\n", __func__, 850229997Sken atio->ccb_h.func_code); 851229997Sken } 852229997Sken start_ccb->ccb_h.func_code = XPT_ABORT; 853229997Sken start_ccb->cab.abort_ccb = (union ccb *)atio; 854229997Sken 855229997Sken /* Tell the SIM that we've aborted this ATIO */ 856229997Sken xpt_action(start_ccb); 857229997Sken softc->ccbs_freed++; 858229997Sken xpt_release_ccb(start_ccb); 859229997Sken 860229997Sken /* 861229997Sken * Send the ATIO back down to the SIM. 862229997Sken */ 863229997Sken xpt_action((union ccb *)atio); 864229997Sken softc->atios_sent++; 865229997Sken 866229997Sken /* 867229997Sken * If we still have work to do, ask for 868229997Sken * another CCB. Otherwise, deactivate our 869229997Sken * callout. 870229997Sken */ 871229997Sken if (ccb_h != NULL) 872229997Sken xpt_schedule(periph, /*priority*/ 1); 873229997Sken else 874229997Sken callout_stop(&softc->dma_callout); 875229997Sken 876229997Sken return; 877229997Sken } else { 878229997Sken io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED; 879229997Sken scsi_status = io->scsiio.scsi_status; 880229997Sken csio->sense_len = io->scsiio.sense_len; 881229997Sken } 882229997Sken data_ptr = NULL; 883229997Sken dxfer_len = 0; 884229997Sken if (io == NULL) { 885229997Sken printf("%s: tag %04x io is NULL\n", __func__, 886229997Sken atio->tag_id); 887229997Sken } else { 888229997Sken#ifdef CTLFEDEBUG 889229997Sken printf("%s: tag %04x status %x\n", __func__, 890229997Sken atio->tag_id, io->io_hdr.status); 891229997Sken#endif 892229997Sken } 893229997Sken csio->sglist_cnt = 0; 894229997Sken if (csio->sense_len != 0) { 895229997Sken csio->sense_data = io->scsiio.sense_data; 896229997Sken flags |= CAM_SEND_SENSE; 897229997Sken } else if (scsi_status == SCSI_STATUS_CHECK_COND) { 898229997Sken xpt_print(periph->path, "%s: check condition " 899229997Sken "with no sense\n", __func__); 900229997Sken } 901229997Sken } else { 902229997Sken struct ctlfe_lun_cmd_info *cmd_info; 903229997Sken 904229997Sken /* 905229997Sken * Datamove call, we need to setup the S/G list. 906229997Sken */ 907229997Sken 908229997Sken cmd_info = (struct ctlfe_lun_cmd_info *) 909229997Sken io->io_hdr.port_priv; 910229997Sken 911229997Sken KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE, 912229997Sken ("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < " 913229997Sken "CTL_PORT_PRIV_SIZE %d", __func__, 914229997Sken sizeof(*cmd_info), CTL_PORT_PRIV_SIZE)); 915229997Sken io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED; 916229997Sken 917229997Sken /* 918229997Sken * Need to zero this, in case it has been used for 919229997Sken * a previous datamove for this particular I/O. 920229997Sken */ 921229997Sken bzero(cmd_info, sizeof(*cmd_info)); 922229997Sken scsi_status = 0; 923229997Sken 924229997Sken csio->cdb_len = atio->cdb_len; 925229997Sken 926265641Smav ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len, 927265641Smav &csio->sglist_cnt); 928229997Sken 929229997Sken io->scsiio.ext_data_filled += dxfer_len; 930229997Sken 931229997Sken if (io->scsiio.ext_data_filled > 932229997Sken io->scsiio.kern_total_len) { 933229997Sken xpt_print(periph->path, "%s: tag 0x%04x " 934229997Sken "fill len %u > total %u\n", 935229997Sken __func__, io->scsiio.tag_num, 936229997Sken io->scsiio.ext_data_filled, 937229997Sken io->scsiio.kern_total_len); 938229997Sken } 939229997Sken } 940229997Sken 941229997Sken#ifdef CTLFEDEBUG 942229997Sken printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__, 943229997Sken (flags & CAM_SEND_STATUS) ? "done" : "datamove", 944229997Sken atio->tag_id, flags, data_ptr, dxfer_len); 945229997Sken#endif 946229997Sken 947229997Sken /* 948229997Sken * Valid combinations: 949255117Smav * - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0, 950229997Sken * sglist_cnt = 0 951255117Smav * - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0, 952229997Sken * sglist_cnt = 0 953255117Smav * - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0, 954229997Sken * sglist_cnt != 0 955229997Sken */ 956229997Sken#ifdef CTLFEDEBUG 957229997Sken if (((flags & CAM_SEND_STATUS) 958255117Smav && (((flags & CAM_DATA_SG) != 0) 959229997Sken || (dxfer_len != 0) 960229997Sken || (csio->sglist_cnt != 0))) 961229997Sken || (((flags & CAM_SEND_STATUS) == 0) 962229997Sken && (dxfer_len == 0)) 963255117Smav || ((flags & CAM_DATA_SG) 964229997Sken && (csio->sglist_cnt == 0)) 965255117Smav || (((flags & CAM_DATA_SG) == 0) 966229997Sken && (csio->sglist_cnt != 0))) { 967229997Sken printf("%s: tag %04x cdb %02x flags %#x dxfer_len " 968229997Sken "%d sg %u\n", __func__, atio->tag_id, 969229997Sken atio->cdb_io.cdb_bytes[0], flags, dxfer_len, 970229997Sken csio->sglist_cnt); 971229997Sken if (io != NULL) { 972229997Sken printf("%s: tag %04x io status %#x\n", __func__, 973229997Sken atio->tag_id, io->io_hdr.status); 974229997Sken } else { 975229997Sken printf("%s: tag %04x no associated io\n", 976229997Sken __func__, atio->tag_id); 977229997Sken } 978229997Sken } 979229997Sken#endif 980229997Sken cam_fill_ctio(csio, 981229997Sken /*retries*/ 2, 982229997Sken ctlfedone, 983229997Sken flags, 984229997Sken (flags & CAM_TAG_ACTION_VALID) ? 985229997Sken MSG_SIMPLE_Q_TAG : 0, 986229997Sken atio->tag_id, 987229997Sken atio->init_id, 988229997Sken scsi_status, 989229997Sken /*data_ptr*/ data_ptr, 990229997Sken /*dxfer_len*/ dxfer_len, 991229997Sken /*timeout*/ 5 * 1000); 992260387Sscottl start_ccb->ccb_h.flags |= CAM_UNLOCKED; 993229997Sken start_ccb->ccb_h.ccb_atio = atio; 994229997Sken if (((flags & CAM_SEND_STATUS) == 0) 995229997Sken && (io != NULL)) 996229997Sken io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 997229997Sken 998229997Sken softc->ctios_sent++; 999229997Sken 1000260387Sscottl cam_periph_unlock(periph); 1001229997Sken xpt_action(start_ccb); 1002260387Sscottl cam_periph_lock(periph); 1003229997Sken 1004229997Sken if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1005229997Sken cam_release_devq(periph->path, 1006229997Sken /*relsim_flags*/0, 1007229997Sken /*reduction*/0, 1008229997Sken /*timeout*/0, 1009229997Sken /*getcount_only*/0); 1010229997Sken atio->ccb_h.status &= ~CAM_DEV_QFRZN; 1011229997Sken } 1012229997Sken 1013229997Sken ccb_h = TAILQ_FIRST(&softc->work_queue); 1014229997Sken } 1015229997Sken /* 1016229997Sken * If we still have work to do, ask for another CCB. Otherwise, 1017229997Sken * deactivate our callout. 1018229997Sken */ 1019229997Sken if (ccb_h != NULL) 1020229997Sken xpt_schedule(periph, /*priority*/ 1); 1021229997Sken else 1022229997Sken callout_stop(&softc->dma_callout); 1023229997Sken} 1024229997Sken 1025229997Skenstatic void 1026229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb) 1027229997Sken{ 1028229997Sken struct ctlfe_lun_softc *softc; 1029229997Sken 1030229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 1031229997Sken 1032229997Sken switch (ccb->ccb_h.func_code) { 1033229997Sken case XPT_ACCEPT_TARGET_IO: 1034229997Sken softc->atios_returned++; 1035229997Sken break; 1036229997Sken case XPT_IMMEDIATE_NOTIFY: 1037229997Sken case XPT_NOTIFY_ACKNOWLEDGE: 1038229997Sken softc->inots_returned++; 1039229997Sken break; 1040229997Sken default: 1041229997Sken break; 1042229997Sken } 1043229997Sken 1044229997Sken free(ccb, M_CTLFE); 1045229997Sken 1046229997Sken KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: " 1047229997Sken "atios_returned %ju > atios_sent %ju", __func__, 1048229997Sken softc->atios_returned, softc->atios_sent)); 1049229997Sken KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: " 1050229997Sken "inots_returned %ju > inots_sent %ju", __func__, 1051229997Sken softc->inots_returned, softc->inots_sent)); 1052229997Sken 1053229997Sken /* 1054229997Sken * If we have received all of our CCBs, we can release our 1055229997Sken * reference on the peripheral driver. It will probably go away 1056229997Sken * now. 1057229997Sken */ 1058229997Sken if ((softc->atios_returned == softc->atios_sent) 1059229997Sken && (softc->inots_returned == softc->inots_sent)) { 1060229997Sken cam_periph_release_locked(periph); 1061229997Sken } 1062229997Sken} 1063229997Sken 1064238870Smjacobstatic int 1065238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset) 1066238870Smjacob{ 1067238870Smjacob uint64_t lba; 1068238870Smjacob uint32_t num_blocks, nbc; 1069238870Smjacob uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)? 1070238870Smjacob atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes; 1071238870Smjacob 1072238870Smjacob nbc = offset >> 9; /* ASSUMING 512 BYTE BLOCKS */ 1073238870Smjacob 1074238870Smjacob switch (cmdbyt[0]) { 1075238870Smjacob case READ_6: 1076238870Smjacob case WRITE_6: 1077238870Smjacob { 1078238870Smjacob struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt; 1079238870Smjacob lba = scsi_3btoul(cdb->addr); 1080238870Smjacob lba &= 0x1fffff; 1081238870Smjacob num_blocks = cdb->length; 1082238870Smjacob if (num_blocks == 0) 1083238870Smjacob num_blocks = 256; 1084238870Smjacob lba += nbc; 1085238870Smjacob num_blocks -= nbc; 1086238870Smjacob scsi_ulto3b(lba, cdb->addr); 1087238870Smjacob cdb->length = num_blocks; 1088238870Smjacob break; 1089238870Smjacob } 1090238870Smjacob case READ_10: 1091238870Smjacob case WRITE_10: 1092238870Smjacob { 1093238870Smjacob struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt; 1094238870Smjacob lba = scsi_4btoul(cdb->addr); 1095238870Smjacob num_blocks = scsi_2btoul(cdb->length); 1096238870Smjacob lba += nbc; 1097238870Smjacob num_blocks -= nbc; 1098238870Smjacob scsi_ulto4b(lba, cdb->addr); 1099238870Smjacob scsi_ulto2b(num_blocks, cdb->length); 1100238870Smjacob break; 1101238870Smjacob } 1102238870Smjacob case READ_12: 1103238870Smjacob case WRITE_12: 1104238870Smjacob { 1105238870Smjacob struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt; 1106238870Smjacob lba = scsi_4btoul(cdb->addr); 1107238870Smjacob num_blocks = scsi_4btoul(cdb->length); 1108238870Smjacob lba += nbc; 1109238870Smjacob num_blocks -= nbc; 1110238870Smjacob scsi_ulto4b(lba, cdb->addr); 1111238870Smjacob scsi_ulto4b(num_blocks, cdb->length); 1112238870Smjacob break; 1113238870Smjacob } 1114238870Smjacob case READ_16: 1115238870Smjacob case WRITE_16: 1116238870Smjacob { 1117238870Smjacob struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt; 1118238870Smjacob lba = scsi_8btou64(cdb->addr); 1119238870Smjacob num_blocks = scsi_4btoul(cdb->length); 1120238870Smjacob lba += nbc; 1121238870Smjacob num_blocks -= nbc; 1122238870Smjacob scsi_u64to8b(lba, cdb->addr); 1123238870Smjacob scsi_ulto4b(num_blocks, cdb->length); 1124238870Smjacob break; 1125238870Smjacob } 1126238870Smjacob default: 1127238870Smjacob return -1; 1128238870Smjacob } 1129238870Smjacob return (0); 1130238870Smjacob} 1131238870Smjacob 1132229997Skenstatic void 1133229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb) 1134229997Sken{ 1135229997Sken struct ctlfe_lun_softc *softc; 1136229997Sken struct ctlfe_softc *bus_softc; 1137238870Smjacob struct ccb_accept_tio *atio = NULL; 1138238870Smjacob union ctl_io *io = NULL; 1139260387Sscottl struct mtx *mtx; 1140229997Sken 1141260387Sscottl KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0, 1142260387Sscottl ("CCB in ctlfedone() without CAM_UNLOCKED flag")); 1143229997Sken#ifdef CTLFE_DEBUG 1144229997Sken printf("%s: entered, func_code = %#x, type = %#lx\n", __func__, 1145229997Sken done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type); 1146229997Sken#endif 1147229997Sken 1148229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 1149229997Sken bus_softc = softc->parent_softc; 1150260387Sscottl mtx = cam_periph_mtx(periph); 1151260387Sscottl mtx_lock(mtx); 1152229997Sken 1153229997Sken /* 1154229997Sken * If the peripheral is invalid, ATIOs and immediate notify CCBs 1155229997Sken * need to be freed. Most of the ATIOs and INOTs that come back 1156229997Sken * will be CCBs that are being returned from the SIM as a result of 1157229997Sken * our disabling the LUN. 1158229997Sken * 1159229997Sken * Other CCB types are handled in their respective cases below. 1160229997Sken */ 1161229997Sken if (periph->flags & CAM_PERIPH_INVALID) { 1162229997Sken switch (done_ccb->ccb_h.func_code) { 1163229997Sken case XPT_ACCEPT_TARGET_IO: 1164229997Sken case XPT_IMMEDIATE_NOTIFY: 1165229997Sken case XPT_NOTIFY_ACKNOWLEDGE: 1166229997Sken ctlfe_free_ccb(periph, done_ccb); 1167260387Sscottl goto out; 1168229997Sken default: 1169229997Sken break; 1170229997Sken } 1171229997Sken 1172229997Sken } 1173229997Sken switch (done_ccb->ccb_h.func_code) { 1174229997Sken case XPT_ACCEPT_TARGET_IO: { 1175229997Sken 1176229997Sken atio = &done_ccb->atio; 1177229997Sken 1178229997Sken softc->atios_returned++; 1179229997Sken 1180238870Smjacob resubmit: 1181229997Sken /* 1182229997Sken * Allocate a ctl_io, pass it to CTL, and wait for the 1183229997Sken * datamove or done. 1184229997Sken */ 1185268677Smav io = ctl_alloc_io(bus_softc->port.ctl_pool_ref); 1186229997Sken if (io == NULL) { 1187229997Sken atio->ccb_h.flags &= ~CAM_DIR_MASK; 1188229997Sken atio->ccb_h.flags |= CAM_DIR_NONE; 1189229997Sken 1190229997Sken printf("%s: ctl_alloc_io failed!\n", __func__); 1191229997Sken 1192229997Sken /* 1193229997Sken * XXX KDM need to set SCSI_STATUS_BUSY, but there 1194229997Sken * is no field in the ATIO structure to do that, 1195229997Sken * and we aren't able to allocate a ctl_io here. 1196229997Sken * What to do? 1197229997Sken */ 1198229997Sken atio->sense_len = 0; 1199229997Sken done_ccb->ccb_h.io_ptr = NULL; 1200229997Sken TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h, 1201229997Sken periph_links.tqe); 1202229997Sken xpt_schedule(periph, /*priority*/ 1); 1203229997Sken break; 1204229997Sken } 1205260387Sscottl mtx_unlock(mtx); 1206229997Sken ctl_zero_io(io); 1207229997Sken 1208229997Sken /* Save pointers on both sides */ 1209229997Sken io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb; 1210229997Sken done_ccb->ccb_h.io_ptr = io; 1211229997Sken 1212229997Sken /* 1213229997Sken * Only SCSI I/O comes down this path, resets, etc. come 1214229997Sken * down the immediate notify path below. 1215229997Sken */ 1216229997Sken io->io_hdr.io_type = CTL_IO_SCSI; 1217229997Sken io->io_hdr.nexus.initid.id = atio->init_id; 1218268677Smav io->io_hdr.nexus.targ_port = bus_softc->port.targ_port; 1219229997Sken io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id; 1220229997Sken io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun; 1221229997Sken io->scsiio.tag_num = atio->tag_id; 1222229997Sken switch (atio->tag_action) { 1223229997Sken case CAM_TAG_ACTION_NONE: 1224229997Sken io->scsiio.tag_type = CTL_TAG_UNTAGGED; 1225229997Sken break; 1226229997Sken case MSG_SIMPLE_TASK: 1227229997Sken io->scsiio.tag_type = CTL_TAG_SIMPLE; 1228229997Sken break; 1229229997Sken case MSG_HEAD_OF_QUEUE_TASK: 1230229997Sken io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE; 1231229997Sken break; 1232229997Sken case MSG_ORDERED_TASK: 1233229997Sken io->scsiio.tag_type = CTL_TAG_ORDERED; 1234229997Sken break; 1235229997Sken case MSG_ACA_TASK: 1236229997Sken io->scsiio.tag_type = CTL_TAG_ACA; 1237229997Sken break; 1238229997Sken default: 1239229997Sken io->scsiio.tag_type = CTL_TAG_UNTAGGED; 1240229997Sken printf("%s: unhandled tag type %#x!!\n", __func__, 1241229997Sken atio->tag_action); 1242229997Sken break; 1243229997Sken } 1244229997Sken if (atio->cdb_len > sizeof(io->scsiio.cdb)) { 1245229997Sken printf("%s: WARNING: CDB len %d > ctl_io space %zd\n", 1246229997Sken __func__, atio->cdb_len, sizeof(io->scsiio.cdb)); 1247229997Sken } 1248229997Sken io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb)); 1249229997Sken bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb, 1250229997Sken io->scsiio.cdb_len); 1251229997Sken 1252229997Sken#ifdef CTLFEDEBUG 1253229997Sken printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__, 1254229997Sken (uintmax_t)io->io_hdr.nexus.initid.id, 1255229997Sken io->io_hdr.nexus.targ_port, 1256229997Sken (uintmax_t)io->io_hdr.nexus.targ_target.id, 1257229997Sken io->io_hdr.nexus.targ_lun, 1258229997Sken io->scsiio.tag_num, io->scsiio.cdb[0]); 1259229997Sken#endif 1260229997Sken 1261229997Sken ctl_queue(io); 1262260387Sscottl return; 1263229997Sken } 1264229997Sken case XPT_CONT_TARGET_IO: { 1265238870Smjacob int srr = 0; 1266238870Smjacob uint32_t srr_off = 0; 1267229997Sken 1268229997Sken atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio; 1269229997Sken io = (union ctl_io *)atio->ccb_h.io_ptr; 1270229997Sken 1271229997Sken softc->ctios_returned++; 1272229997Sken#ifdef CTLFEDEBUG 1273229997Sken printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n", 1274229997Sken __func__, atio->tag_id, done_ccb->ccb_h.flags); 1275229997Sken#endif 1276229997Sken /* 1277238870Smjacob * Handle SRR case were the data pointer is pushed back hack 1278238870Smjacob */ 1279238870Smjacob if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV 1280238870Smjacob && done_ccb->csio.msg_ptr != NULL 1281238870Smjacob && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED 1282238870Smjacob && done_ccb->csio.msg_ptr[1] == 5 1283238870Smjacob && done_ccb->csio.msg_ptr[2] == 0) { 1284238870Smjacob srr = 1; 1285238870Smjacob srr_off = 1286238870Smjacob (done_ccb->csio.msg_ptr[3] << 24) 1287238870Smjacob | (done_ccb->csio.msg_ptr[4] << 16) 1288238870Smjacob | (done_ccb->csio.msg_ptr[5] << 8) 1289238870Smjacob | (done_ccb->csio.msg_ptr[6]); 1290238870Smjacob } 1291238870Smjacob 1292238870Smjacob if (srr && (done_ccb->ccb_h.flags & CAM_SEND_STATUS)) { 1293238870Smjacob /* 1294238870Smjacob * If status was being sent, the back end data is now 1295238870Smjacob * history. Hack it up and resubmit a new command with 1296238870Smjacob * the CDB adjusted. If the SIM does the right thing, 1297238870Smjacob * all of the resid math should work. 1298238870Smjacob */ 1299238870Smjacob softc->ccbs_freed++; 1300238870Smjacob xpt_release_ccb(done_ccb); 1301238870Smjacob ctl_free_io(io); 1302238870Smjacob if (ctlfe_adjust_cdb(atio, srr_off) == 0) { 1303238870Smjacob done_ccb = (union ccb *)atio; 1304238870Smjacob goto resubmit; 1305238870Smjacob } 1306238870Smjacob /* 1307238870Smjacob * Fall through to doom.... 1308238870Smjacob */ 1309238870Smjacob } else if (srr) { 1310238870Smjacob /* 1311238870Smjacob * If we have an srr and we're still sending data, we 1312238870Smjacob * should be able to adjust offsets and cycle again. 1313238870Smjacob */ 1314238870Smjacob io->scsiio.kern_rel_offset = 1315238870Smjacob io->scsiio.ext_data_filled = srr_off; 1316238870Smjacob io->scsiio.ext_data_len = io->scsiio.kern_total_len - 1317238870Smjacob io->scsiio.kern_rel_offset; 1318238870Smjacob softc->ccbs_freed++; 1319238870Smjacob io->scsiio.io_hdr.status = CTL_STATUS_NONE; 1320238870Smjacob xpt_release_ccb(done_ccb); 1321238870Smjacob TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h, 1322238870Smjacob periph_links.tqe); 1323238870Smjacob xpt_schedule(periph, /*priority*/ 1); 1324260387Sscottl break; 1325238870Smjacob } 1326238870Smjacob 1327238870Smjacob /* 1328229997Sken * If we were sending status back to the initiator, free up 1329229997Sken * resources. If we were doing a datamove, call the 1330229997Sken * datamove done routine. 1331229997Sken */ 1332229997Sken if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) { 1333229997Sken softc->ccbs_freed++; 1334229997Sken xpt_release_ccb(done_ccb); 1335229997Sken ctl_free_io(io); 1336229997Sken /* 1337229997Sken * For a wildcard attachment, commands can come in 1338229997Sken * with a specific target/lun. Reset the target 1339229997Sken * and LUN fields back to the wildcard values before 1340229997Sken * we send them back down to the SIM. The SIM has 1341229997Sken * a wildcard LUN enabled, not whatever target/lun 1342229997Sken * these happened to be. 1343229997Sken */ 1344229997Sken if (softc->flags & CTLFE_LUN_WILDCARD) { 1345229997Sken atio->ccb_h.target_id = CAM_TARGET_WILDCARD; 1346229997Sken atio->ccb_h.target_lun = CAM_LUN_WILDCARD; 1347229997Sken } 1348229997Sken if (periph->flags & CAM_PERIPH_INVALID) { 1349229997Sken ctlfe_free_ccb(periph, (union ccb *)atio); 1350229997Sken } else { 1351260387Sscottl softc->atios_sent++; 1352260387Sscottl mtx_unlock(mtx); 1353229997Sken xpt_action((union ccb *)atio); 1354260387Sscottl return; 1355229997Sken } 1356229997Sken } else { 1357229997Sken struct ctlfe_lun_cmd_info *cmd_info; 1358229997Sken struct ccb_scsiio *csio; 1359229997Sken 1360229997Sken csio = &done_ccb->csio; 1361229997Sken cmd_info = (struct ctlfe_lun_cmd_info *) 1362229997Sken io->io_hdr.port_priv; 1363229997Sken 1364229997Sken io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1365229997Sken 1366229997Sken io->scsiio.ext_data_len += csio->dxfer_len; 1367229997Sken if (io->scsiio.ext_data_len > 1368229997Sken io->scsiio.kern_total_len) { 1369229997Sken xpt_print(periph->path, "%s: tag 0x%04x " 1370229997Sken "done len %u > total %u sent %u\n", 1371229997Sken __func__, io->scsiio.tag_num, 1372229997Sken io->scsiio.ext_data_len, 1373229997Sken io->scsiio.kern_total_len, 1374229997Sken io->scsiio.ext_data_filled); 1375229997Sken } 1376229997Sken /* 1377229997Sken * Translate CAM status to CTL status. Success 1378229997Sken * does not change the overall, ctl_io status. In 1379229997Sken * that case we just set port_status to 0. If we 1380229997Sken * have a failure, though, set a data phase error 1381229997Sken * for the overall ctl_io. 1382229997Sken */ 1383229997Sken switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) { 1384229997Sken case CAM_REQ_CMP: 1385229997Sken io->io_hdr.port_status = 0; 1386229997Sken break; 1387229997Sken default: 1388229997Sken /* 1389245228Sken * XXX KDM we probably need to figure out a 1390245228Sken * standard set of errors that the SIM 1391245228Sken * drivers should return in the event of a 1392245228Sken * data transfer failure. A data phase 1393245228Sken * error will at least point the user to a 1394245228Sken * data transfer error of some sort. 1395245228Sken * Hopefully the SIM printed out some 1396245228Sken * additional information to give the user 1397245228Sken * a clue what happened. 1398229997Sken */ 1399229997Sken io->io_hdr.port_status = 0xbad1; 1400229997Sken ctl_set_data_phase_error(&io->scsiio); 1401229997Sken /* 1402229997Sken * XXX KDM figure out residual. 1403229997Sken */ 1404229997Sken break; 1405229997Sken } 1406229997Sken /* 1407229997Sken * If we had to break this S/G list into multiple 1408229997Sken * pieces, figure out where we are in the list, and 1409229997Sken * continue sending pieces if necessary. 1410229997Sken */ 1411229997Sken if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) 1412265641Smav && (io->io_hdr.port_status == 0)) { 1413229997Sken ccb_flags flags; 1414229997Sken uint8_t scsi_status; 1415229997Sken uint8_t *data_ptr; 1416229997Sken uint32_t dxfer_len; 1417229997Sken 1418229997Sken flags = atio->ccb_h.flags & 1419229997Sken (CAM_DIS_DISCONNECT| 1420265641Smav CAM_TAG_ACTION_VALID); 1421229997Sken 1422265641Smav ctlfedata(softc, io, &flags, &data_ptr, 1423265641Smav &dxfer_len, &csio->sglist_cnt); 1424229997Sken 1425229997Sken scsi_status = 0; 1426229997Sken 1427229997Sken if (((flags & CAM_SEND_STATUS) == 0) 1428229997Sken && (dxfer_len == 0)) { 1429229997Sken printf("%s: tag %04x no status or " 1430229997Sken "len cdb = %02x\n", __func__, 1431229997Sken atio->tag_id, 1432229997Sken atio->cdb_io.cdb_bytes[0]); 1433229997Sken printf("%s: tag %04x io status %#x\n", 1434229997Sken __func__, atio->tag_id, 1435229997Sken io->io_hdr.status); 1436229997Sken } 1437229997Sken 1438229997Sken cam_fill_ctio(csio, 1439229997Sken /*retries*/ 2, 1440229997Sken ctlfedone, 1441229997Sken flags, 1442229997Sken (flags & CAM_TAG_ACTION_VALID) ? 1443229997Sken MSG_SIMPLE_Q_TAG : 0, 1444229997Sken atio->tag_id, 1445229997Sken atio->init_id, 1446229997Sken scsi_status, 1447229997Sken /*data_ptr*/ data_ptr, 1448229997Sken /*dxfer_len*/ dxfer_len, 1449229997Sken /*timeout*/ 5 * 1000); 1450229997Sken 1451260387Sscottl csio->ccb_h.flags |= CAM_UNLOCKED; 1452229997Sken csio->resid = 0; 1453229997Sken csio->ccb_h.ccb_atio = atio; 1454229997Sken io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 1455229997Sken softc->ctios_sent++; 1456260387Sscottl mtx_unlock(mtx); 1457229997Sken xpt_action((union ccb *)csio); 1458229997Sken } else { 1459229997Sken /* 1460229997Sken * Release the CTIO. The ATIO will be sent back 1461229997Sken * down to the SIM once we send status. 1462229997Sken */ 1463229997Sken softc->ccbs_freed++; 1464229997Sken xpt_release_ccb(done_ccb); 1465260387Sscottl mtx_unlock(mtx); 1466229997Sken 1467229997Sken /* Call the backend move done callback */ 1468229997Sken io->scsiio.be_move_done(io); 1469229997Sken } 1470260387Sscottl return; 1471229997Sken } 1472229997Sken break; 1473229997Sken } 1474229997Sken case XPT_IMMEDIATE_NOTIFY: { 1475229997Sken union ctl_io *io; 1476229997Sken struct ccb_immediate_notify *inot; 1477229997Sken cam_status status; 1478229997Sken int frozen; 1479229997Sken 1480229997Sken inot = &done_ccb->cin1; 1481229997Sken 1482229997Sken softc->inots_returned++; 1483229997Sken 1484229997Sken frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 1485229997Sken 1486229997Sken printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x " 1487229997Sken "seq %#x\n", __func__, inot->ccb_h.status, 1488229997Sken inot->tag_id, inot->seq_id); 1489229997Sken 1490268677Smav io = ctl_alloc_io(bus_softc->port.ctl_pool_ref); 1491229997Sken if (io != NULL) { 1492229997Sken int send_ctl_io; 1493229997Sken 1494229997Sken send_ctl_io = 1; 1495229997Sken 1496229997Sken ctl_zero_io(io); 1497229997Sken io->io_hdr.io_type = CTL_IO_TASK; 1498229997Sken io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb; 1499229997Sken inot->ccb_h.io_ptr = io; 1500229997Sken io->io_hdr.nexus.initid.id = inot->initiator_id; 1501268677Smav io->io_hdr.nexus.targ_port = bus_softc->port.targ_port; 1502229997Sken io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id; 1503229997Sken io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun; 1504229997Sken /* XXX KDM should this be the tag_id? */ 1505229997Sken io->taskio.tag_num = inot->seq_id; 1506229997Sken 1507229997Sken status = inot->ccb_h.status & CAM_STATUS_MASK; 1508229997Sken switch (status) { 1509229997Sken case CAM_SCSI_BUS_RESET: 1510229997Sken io->taskio.task_action = CTL_TASK_BUS_RESET; 1511229997Sken break; 1512229997Sken case CAM_BDR_SENT: 1513229997Sken io->taskio.task_action = CTL_TASK_TARGET_RESET; 1514229997Sken break; 1515229997Sken case CAM_MESSAGE_RECV: 1516229997Sken switch (inot->arg) { 1517229997Sken case MSG_ABORT_TASK_SET: 1518229997Sken /* 1519229997Sken * XXX KDM this isn't currently 1520229997Sken * supported by CTL. It ends up 1521229997Sken * being a no-op. 1522229997Sken */ 1523229997Sken io->taskio.task_action = 1524229997Sken CTL_TASK_ABORT_TASK_SET; 1525229997Sken break; 1526229997Sken case MSG_TARGET_RESET: 1527229997Sken io->taskio.task_action = 1528229997Sken CTL_TASK_TARGET_RESET; 1529229997Sken break; 1530229997Sken case MSG_ABORT_TASK: 1531229997Sken io->taskio.task_action = 1532229997Sken CTL_TASK_ABORT_TASK; 1533229997Sken break; 1534229997Sken case MSG_LOGICAL_UNIT_RESET: 1535229997Sken io->taskio.task_action = 1536229997Sken CTL_TASK_LUN_RESET; 1537229997Sken break; 1538229997Sken case MSG_CLEAR_TASK_SET: 1539229997Sken /* 1540229997Sken * XXX KDM this isn't currently 1541229997Sken * supported by CTL. It ends up 1542229997Sken * being a no-op. 1543229997Sken */ 1544229997Sken io->taskio.task_action = 1545229997Sken CTL_TASK_CLEAR_TASK_SET; 1546229997Sken break; 1547229997Sken case MSG_CLEAR_ACA: 1548229997Sken io->taskio.task_action = 1549229997Sken CTL_TASK_CLEAR_ACA; 1550229997Sken break; 1551229997Sken case MSG_NOOP: 1552229997Sken send_ctl_io = 0; 1553229997Sken break; 1554229997Sken default: 1555229997Sken xpt_print(periph->path, "%s: " 1556229997Sken "unsupported message 0x%x\n", 1557229997Sken __func__, inot->arg); 1558229997Sken send_ctl_io = 0; 1559229997Sken break; 1560229997Sken } 1561229997Sken break; 1562229997Sken case CAM_REQ_ABORTED: 1563229997Sken /* 1564229997Sken * This request was sent back by the driver. 1565229997Sken * XXX KDM what do we do here? 1566229997Sken */ 1567229997Sken send_ctl_io = 0; 1568229997Sken break; 1569237601Sken case CAM_REQ_INVALID: 1570237601Sken case CAM_PROVIDE_FAIL: 1571229997Sken default: 1572237601Sken /* 1573237601Sken * We should only get here if we're talking 1574237601Sken * to a talking to a SIM that is target 1575237601Sken * capable but supports the old API. In 1576237601Sken * that case, we need to just free the CCB. 1577237601Sken * If we actually send a notify acknowledge, 1578237601Sken * it will send that back with an error as 1579237601Sken * well. 1580237601Sken */ 1581237601Sken 1582237601Sken if ((status != CAM_REQ_INVALID) 1583237601Sken && (status != CAM_PROVIDE_FAIL)) 1584237601Sken xpt_print(periph->path, "%s: " 1585237601Sken "unsupported CAM status " 1586237601Sken "0x%x\n", __func__, status); 1587237601Sken 1588237601Sken ctl_free_io(io); 1589237601Sken ctlfe_free_ccb(periph, done_ccb); 1590237601Sken 1591260387Sscottl goto out; 1592229997Sken } 1593229997Sken if (send_ctl_io != 0) { 1594229997Sken ctl_queue(io); 1595229997Sken } else { 1596229997Sken ctl_free_io(io); 1597229997Sken done_ccb->ccb_h.status = CAM_REQ_INPROG; 1598229997Sken done_ccb->ccb_h.func_code = 1599229997Sken XPT_NOTIFY_ACKNOWLEDGE; 1600229997Sken xpt_action(done_ccb); 1601229997Sken } 1602229997Sken } else { 1603229997Sken xpt_print(periph->path, "%s: could not allocate " 1604229997Sken "ctl_io for immediate notify!\n", __func__); 1605229997Sken /* requeue this to the adapter */ 1606229997Sken done_ccb->ccb_h.status = CAM_REQ_INPROG; 1607229997Sken done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE; 1608229997Sken xpt_action(done_ccb); 1609229997Sken } 1610229997Sken 1611229997Sken if (frozen != 0) { 1612229997Sken cam_release_devq(periph->path, 1613229997Sken /*relsim_flags*/ 0, 1614229997Sken /*opening reduction*/ 0, 1615229997Sken /*timeout*/ 0, 1616229997Sken /*getcount_only*/ 0); 1617229997Sken } 1618229997Sken break; 1619229997Sken } 1620229997Sken case XPT_NOTIFY_ACKNOWLEDGE: 1621229997Sken /* 1622229997Sken * Queue this back down to the SIM as an immediate notify. 1623229997Sken */ 1624229997Sken done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY; 1625229997Sken xpt_action(done_ccb); 1626229997Sken softc->inots_sent++; 1627229997Sken break; 1628229997Sken case XPT_SET_SIM_KNOB: 1629229997Sken case XPT_GET_SIM_KNOB: 1630229997Sken break; 1631229997Sken default: 1632229997Sken panic("%s: unexpected CCB type %#x", __func__, 1633229997Sken done_ccb->ccb_h.func_code); 1634229997Sken break; 1635229997Sken } 1636260387Sscottl 1637260387Sscottlout: 1638260387Sscottl mtx_unlock(mtx); 1639229997Sken} 1640229997Sken 1641229997Skenstatic void 1642229997Skenctlfe_onoffline(void *arg, int online) 1643229997Sken{ 1644229997Sken struct ctlfe_softc *bus_softc; 1645229997Sken union ccb *ccb; 1646229997Sken cam_status status; 1647229997Sken struct cam_path *path; 1648229997Sken int set_wwnn; 1649229997Sken 1650229997Sken bus_softc = (struct ctlfe_softc *)arg; 1651229997Sken 1652229997Sken set_wwnn = 0; 1653229997Sken 1654229997Sken status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id, 1655229997Sken CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 1656229997Sken if (status != CAM_REQ_CMP) { 1657229997Sken printf("%s: unable to create path!\n", __func__); 1658229997Sken return; 1659229997Sken } 1660245228Sken ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_NOWAIT | M_ZERO); 1661245228Sken if (ccb == NULL) { 1662245228Sken printf("%s: unable to malloc CCB!\n", __func__); 1663245228Sken xpt_free_path(path); 1664245228Sken return; 1665245228Sken } 1666242174Smav xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE); 1667229997Sken 1668229997Sken /* 1669229997Sken * Copan WWN format: 1670229997Sken * 1671229997Sken * Bits 63-60: 0x5 NAA, IEEE registered name 1672229997Sken * Bits 59-36: 0x000ED5 IEEE Company name assigned to Copan 1673229997Sken * Bits 35-12: Copan SSN (Sequential Serial Number) 1674229997Sken * Bits 11-8: Type of port: 1675229997Sken * 1 == N-Port 1676229997Sken * 2 == F-Port 1677229997Sken * 3 == NL-Port 1678229997Sken * Bits 7-0: 0 == Node Name, >0 == Port Number 1679229997Sken */ 1680229997Sken 1681229997Sken if (online != 0) { 1682229997Sken 1683229997Sken ccb->ccb_h.func_code = XPT_GET_SIM_KNOB; 1684229997Sken 1685229997Sken 1686229997Sken xpt_action(ccb); 1687229997Sken 1688229997Sken 1689229997Sken if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){ 1690229997Sken#ifdef RANDOM_WWNN 1691229997Sken uint64_t random_bits; 1692229997Sken#endif 1693229997Sken 1694229997Sken printf("%s: %s current WWNN %#jx\n", __func__, 1695229997Sken bus_softc->port_name, 1696229997Sken ccb->knob.xport_specific.fc.wwnn); 1697229997Sken printf("%s: %s current WWPN %#jx\n", __func__, 1698229997Sken bus_softc->port_name, 1699229997Sken ccb->knob.xport_specific.fc.wwpn); 1700229997Sken 1701229997Sken#ifdef RANDOM_WWNN 1702229997Sken arc4rand(&random_bits, sizeof(random_bits), 0); 1703229997Sken#endif 1704229997Sken 1705229997Sken /* 1706229997Sken * XXX KDM this is a bit of a kludge for now. We 1707229997Sken * take the current WWNN/WWPN from the card, and 1708229997Sken * replace the company identifier and the NL-Port 1709229997Sken * indicator and the port number (for the WWPN). 1710229997Sken * This should be replaced later with ddb_GetWWNN, 1711229997Sken * or possibly a more centralized scheme. (It 1712229997Sken * would be nice to have the WWNN/WWPN for each 1713268677Smav * port stored in the ctl_port structure.) 1714229997Sken */ 1715229997Sken#ifdef RANDOM_WWNN 1716229997Sken ccb->knob.xport_specific.fc.wwnn = 1717229997Sken (random_bits & 1718229997Sken 0x0000000fffffff00ULL) | 1719229997Sken /* Company ID */ 0x5000ED5000000000ULL | 1720229997Sken /* NL-Port */ 0x0300; 1721229997Sken ccb->knob.xport_specific.fc.wwpn = 1722229997Sken (random_bits & 1723229997Sken 0x0000000fffffff00ULL) | 1724229997Sken /* Company ID */ 0x5000ED5000000000ULL | 1725229997Sken /* NL-Port */ 0x3000 | 1726268677Smav /* Port Num */ (bus_softc->port.targ_port & 0xff); 1727229997Sken 1728229997Sken /* 1729229997Sken * This is a bit of an API break/reversal, but if 1730229997Sken * we're doing the random WWNN that's a little 1731229997Sken * different anyway. So record what we're actually 1732229997Sken * using with the frontend code so it's reported 1733229997Sken * accurately. 1734229997Sken */ 1735268677Smav bus_softc->port.wwnn = 1736229997Sken ccb->knob.xport_specific.fc.wwnn; 1737268677Smav bus_softc->port.wwpn = 1738229997Sken ccb->knob.xport_specific.fc.wwpn; 1739229997Sken set_wwnn = 1; 1740229997Sken#else /* RANDOM_WWNN */ 1741229997Sken /* 1742229997Sken * If the user has specified a WWNN/WWPN, send them 1743229997Sken * down to the SIM. Otherwise, record what the SIM 1744229997Sken * has reported. 1745229997Sken */ 1746268677Smav if ((bus_softc->port.wwnn != 0) 1747268677Smav && (bus_softc->port.wwpn != 0)) { 1748229997Sken ccb->knob.xport_specific.fc.wwnn = 1749268677Smav bus_softc->port.wwnn; 1750229997Sken ccb->knob.xport_specific.fc.wwpn = 1751268677Smav bus_softc->port.wwpn; 1752229997Sken set_wwnn = 1; 1753229997Sken } else { 1754268677Smav bus_softc->port.wwnn = 1755229997Sken ccb->knob.xport_specific.fc.wwnn; 1756268677Smav bus_softc->port.wwpn = 1757229997Sken ccb->knob.xport_specific.fc.wwpn; 1758229997Sken } 1759229997Sken#endif /* RANDOM_WWNN */ 1760229997Sken 1761229997Sken 1762229997Sken if (set_wwnn != 0) { 1763229997Sken printf("%s: %s new WWNN %#jx\n", __func__, 1764229997Sken bus_softc->port_name, 1765229997Sken ccb->knob.xport_specific.fc.wwnn); 1766229997Sken printf("%s: %s new WWPN %#jx\n", __func__, 1767229997Sken bus_softc->port_name, 1768229997Sken ccb->knob.xport_specific.fc.wwpn); 1769229997Sken } 1770229997Sken } else { 1771229997Sken printf("%s: %s has no valid WWNN/WWPN\n", __func__, 1772229997Sken bus_softc->port_name); 1773229997Sken } 1774229997Sken } 1775229997Sken ccb->ccb_h.func_code = XPT_SET_SIM_KNOB; 1776229997Sken ccb->knob.xport_specific.valid = KNOB_VALID_ROLE; 1777229997Sken if (set_wwnn != 0) 1778229997Sken ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS; 1779229997Sken 1780229997Sken if (online != 0) 1781229997Sken ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET; 1782229997Sken else 1783229997Sken ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE; 1784229997Sken 1785229997Sken xpt_action(ccb); 1786229997Sken 1787229997Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1788229997Sken printf("%s: SIM %s (path id %d) target %s failed with " 1789229997Sken "status %#x\n", 1790229997Sken __func__, bus_softc->port_name, bus_softc->path_id, 1791229997Sken (online != 0) ? "enable" : "disable", 1792229997Sken ccb->ccb_h.status); 1793229997Sken } else { 1794229997Sken printf("%s: SIM %s (path id %d) target %s succeeded\n", 1795229997Sken __func__, bus_softc->port_name, bus_softc->path_id, 1796229997Sken (online != 0) ? "enable" : "disable"); 1797229997Sken } 1798229997Sken 1799229997Sken xpt_free_path(path); 1800229997Sken 1801244016Sken free(ccb, M_TEMP); 1802244016Sken 1803229997Sken return; 1804229997Sken} 1805229997Sken 1806229997Skenstatic void 1807229997Skenctlfe_online(void *arg) 1808229997Sken{ 1809245228Sken struct ctlfe_softc *bus_softc; 1810245228Sken struct cam_path *path; 1811245228Sken cam_status status; 1812245228Sken struct ctlfe_lun_softc *lun_softc; 1813245228Sken 1814245228Sken bus_softc = (struct ctlfe_softc *)arg; 1815245228Sken 1816245228Sken /* 1817245228Sken * Create the wildcard LUN before bringing the port online. 1818245228Sken */ 1819245228Sken status = xpt_create_path(&path, /*periph*/ NULL, 1820245228Sken bus_softc->path_id, CAM_TARGET_WILDCARD, 1821245228Sken CAM_LUN_WILDCARD); 1822245228Sken if (status != CAM_REQ_CMP) { 1823245228Sken printf("%s: unable to create path for wildcard periph\n", 1824245228Sken __func__); 1825245228Sken return; 1826245228Sken } 1827245228Sken 1828245228Sken lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, 1829245228Sken M_NOWAIT | M_ZERO); 1830245228Sken if (lun_softc == NULL) { 1831245228Sken xpt_print(path, "%s: unable to allocate softc for " 1832245228Sken "wildcard periph\n", __func__); 1833245228Sken xpt_free_path(path); 1834245228Sken return; 1835245228Sken } 1836245228Sken 1837260387Sscottl xpt_path_lock(path); 1838245228Sken lun_softc->parent_softc = bus_softc; 1839245228Sken lun_softc->flags |= CTLFE_LUN_WILDCARD; 1840245228Sken 1841260387Sscottl mtx_lock(&bus_softc->lun_softc_mtx); 1842245228Sken STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, lun_softc, links); 1843260387Sscottl mtx_unlock(&bus_softc->lun_softc_mtx); 1844245228Sken 1845245228Sken status = cam_periph_alloc(ctlferegister, 1846245228Sken ctlfeoninvalidate, 1847245228Sken ctlfecleanup, 1848245228Sken ctlfestart, 1849245228Sken "ctl", 1850245228Sken CAM_PERIPH_BIO, 1851245228Sken path, 1852245228Sken ctlfeasync, 1853245228Sken 0, 1854245228Sken lun_softc); 1855245228Sken 1856245228Sken if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1857245228Sken const struct cam_status_entry *entry; 1858245228Sken 1859245228Sken entry = cam_fetch_status_entry(status); 1860245228Sken 1861245228Sken printf("%s: CAM error %s (%#x) returned from " 1862245228Sken "cam_periph_alloc()\n", __func__, (entry != NULL) ? 1863245228Sken entry->status_text : "Unknown", status); 1864245228Sken } 1865245228Sken 1866229997Sken ctlfe_onoffline(arg, /*online*/ 1); 1867245228Sken 1868260387Sscottl xpt_path_unlock(path); 1869260387Sscottl xpt_free_path(path); 1870229997Sken} 1871229997Sken 1872229997Skenstatic void 1873229997Skenctlfe_offline(void *arg) 1874229997Sken{ 1875245228Sken struct ctlfe_softc *bus_softc; 1876245228Sken struct cam_path *path; 1877245228Sken cam_status status; 1878245228Sken struct cam_periph *periph; 1879245228Sken 1880245228Sken bus_softc = (struct ctlfe_softc *)arg; 1881245228Sken 1882245228Sken /* 1883245228Sken * Disable the wildcard LUN for this port now that we have taken 1884245228Sken * the port offline. 1885245228Sken */ 1886245228Sken status = xpt_create_path(&path, /*periph*/ NULL, 1887245228Sken bus_softc->path_id, CAM_TARGET_WILDCARD, 1888245228Sken CAM_LUN_WILDCARD); 1889245228Sken if (status != CAM_REQ_CMP) { 1890245228Sken printf("%s: unable to create path for wildcard periph\n", 1891245228Sken __func__); 1892245228Sken return; 1893245228Sken } 1894245228Sken 1895260387Sscottl xpt_path_lock(path); 1896245228Sken 1897260387Sscottl ctlfe_onoffline(arg, /*online*/ 0); 1898260387Sscottl 1899245228Sken if ((periph = cam_periph_find(path, "ctl")) != NULL) 1900245228Sken cam_periph_invalidate(periph); 1901245228Sken 1902260387Sscottl xpt_path_unlock(path); 1903245228Sken xpt_free_path(path); 1904229997Sken} 1905229997Sken 1906229997Sken/* 1907229997Sken * This will get called to enable a LUN on every bus that is attached to 1908229997Sken * CTL. So we only need to create a path/periph for this particular bus. 1909229997Sken */ 1910229997Skenstatic int 1911229997Skenctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1912229997Sken{ 1913229997Sken struct ctlfe_softc *bus_softc; 1914229997Sken struct ctlfe_lun_softc *softc; 1915229997Sken struct cam_path *path; 1916229997Sken struct cam_periph *periph; 1917229997Sken cam_status status; 1918229997Sken 1919229997Sken bus_softc = (struct ctlfe_softc *)arg; 1920229997Sken 1921260387Sscottl status = xpt_create_path(&path, /*periph*/ NULL, 1922260387Sscottl bus_softc->path_id, 1923260387Sscottl targ_id.id, lun_id); 1924229997Sken /* XXX KDM need some way to return status to CTL here? */ 1925229997Sken if (status != CAM_REQ_CMP) { 1926229997Sken printf("%s: could not create path, status %#x\n", __func__, 1927229997Sken status); 1928229997Sken return (1); 1929229997Sken } 1930229997Sken 1931229997Sken softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO); 1932260387Sscottl xpt_path_lock(path); 1933229997Sken periph = cam_periph_find(path, "ctl"); 1934229997Sken if (periph != NULL) { 1935229997Sken /* We've already got a periph, no need to alloc a new one. */ 1936260387Sscottl xpt_path_unlock(path); 1937229997Sken xpt_free_path(path); 1938229997Sken free(softc, M_CTLFE); 1939229997Sken return (0); 1940229997Sken } 1941229997Sken 1942229997Sken softc->parent_softc = bus_softc; 1943260387Sscottl mtx_lock(&bus_softc->lun_softc_mtx); 1944229997Sken STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links); 1945260387Sscottl mtx_unlock(&bus_softc->lun_softc_mtx); 1946229997Sken 1947229997Sken status = cam_periph_alloc(ctlferegister, 1948229997Sken ctlfeoninvalidate, 1949229997Sken ctlfecleanup, 1950229997Sken ctlfestart, 1951229997Sken "ctl", 1952229997Sken CAM_PERIPH_BIO, 1953229997Sken path, 1954229997Sken ctlfeasync, 1955229997Sken 0, 1956229997Sken softc); 1957229997Sken 1958260387Sscottl xpt_path_unlock(path); 1959244016Sken xpt_free_path(path); 1960229997Sken return (0); 1961229997Sken} 1962229997Sken 1963229997Sken/* 1964245228Sken * This will get called when the user removes a LUN to disable that LUN 1965245228Sken * on every bus that is attached to CTL. 1966229997Sken */ 1967229997Skenstatic int 1968229997Skenctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1969229997Sken{ 1970229997Sken struct ctlfe_softc *softc; 1971229997Sken struct ctlfe_lun_softc *lun_softc; 1972229997Sken 1973229997Sken softc = (struct ctlfe_softc *)arg; 1974229997Sken 1975260387Sscottl mtx_lock(&softc->lun_softc_mtx); 1976229997Sken STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) { 1977229997Sken struct cam_path *path; 1978229997Sken 1979229997Sken path = lun_softc->periph->path; 1980229997Sken 1981229997Sken if ((xpt_path_target_id(path) == targ_id.id) 1982229997Sken && (xpt_path_lun_id(path) == lun_id)) { 1983229997Sken break; 1984229997Sken } 1985229997Sken } 1986229997Sken if (lun_softc == NULL) { 1987260387Sscottl mtx_unlock(&softc->lun_softc_mtx); 1988229997Sken printf("%s: can't find target %d lun %d\n", __func__, 1989229997Sken targ_id.id, lun_id); 1990229997Sken return (1); 1991229997Sken } 1992260387Sscottl cam_periph_acquire(lun_softc->periph); 1993260387Sscottl mtx_unlock(&softc->lun_softc_mtx); 1994229997Sken 1995260387Sscottl cam_periph_lock(lun_softc->periph); 1996229997Sken cam_periph_invalidate(lun_softc->periph); 1997260387Sscottl cam_periph_unlock(lun_softc->periph); 1998260387Sscottl cam_periph_release(lun_softc->periph); 1999229997Sken return (0); 2000229997Sken} 2001229997Sken 2002229997Skenstatic void 2003229997Skenctlfe_dump_sim(struct cam_sim *sim) 2004229997Sken{ 2005229997Sken 2006229997Sken printf("%s%d: max tagged openings: %d, max dev openings: %d\n", 2007229997Sken sim->sim_name, sim->unit_number, 2008229997Sken sim->max_tagged_dev_openings, sim->max_dev_openings); 2009229997Sken printf("\n"); 2010229997Sken} 2011229997Sken 2012229997Sken/* 2013229997Sken * Assumes that the SIM lock is held. 2014229997Sken */ 2015229997Skenstatic void 2016229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc) 2017229997Sken{ 2018229997Sken struct ccb_hdr *hdr; 2019229997Sken struct cam_periph *periph; 2020229997Sken int num_items; 2021229997Sken 2022229997Sken periph = softc->periph; 2023229997Sken num_items = 0; 2024229997Sken 2025229997Sken TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) { 2026229997Sken union ctl_io *io; 2027229997Sken 2028229997Sken io = hdr->io_ptr; 2029229997Sken 2030229997Sken num_items++; 2031229997Sken 2032229997Sken /* 2033229997Sken * This can happen when we get an ATIO but can't allocate 2034229997Sken * a ctl_io. See the XPT_ACCEPT_TARGET_IO case in ctlfedone(). 2035229997Sken */ 2036229997Sken if (io == NULL) { 2037229997Sken struct ccb_scsiio *csio; 2038229997Sken 2039229997Sken csio = (struct ccb_scsiio *)hdr; 2040229997Sken 2041229997Sken xpt_print(periph->path, "CCB %#x ctl_io allocation " 2042229997Sken "failed\n", csio->tag_id); 2043229997Sken continue; 2044229997Sken } 2045229997Sken 2046229997Sken /* 2047229997Sken * Only regular SCSI I/O is put on the work 2048229997Sken * queue, so we can print sense here. There may be no 2049229997Sken * sense if it's no the queue for a DMA, but this serves to 2050229997Sken * print out the CCB as well. 2051229997Sken * 2052229997Sken * XXX KDM switch this over to scsi_sense_print() when 2053229997Sken * CTL is merged in with CAM. 2054229997Sken */ 2055229997Sken ctl_io_error_print(io, NULL); 2056229997Sken 2057229997Sken /* 2058229997Sken * We're sending status back to the 2059229997Sken * initiator, so we're on the queue waiting 2060229997Sken * for a CTIO to do that. 2061229997Sken */ 2062229997Sken if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 2063229997Sken continue; 2064229997Sken 2065229997Sken /* 2066229997Sken * Otherwise, we're on the queue waiting to 2067229997Sken * do a data transfer. 2068229997Sken */ 2069229997Sken xpt_print(periph->path, "Total %u, Current %u, Resid %u\n", 2070229997Sken io->scsiio.kern_total_len, io->scsiio.kern_data_len, 2071229997Sken io->scsiio.kern_data_resid); 2072229997Sken } 2073229997Sken 2074229997Sken xpt_print(periph->path, "%d requests total waiting for CCBs\n", 2075229997Sken num_items); 2076250460Seadler xpt_print(periph->path, "%ju CCBs outstanding (%ju allocated, %ju " 2077229997Sken "freed)\n", (uintmax_t)(softc->ccbs_alloced - 2078229997Sken softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced, 2079229997Sken (uintmax_t)softc->ccbs_freed); 2080229997Sken xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju " 2081229997Sken "returned\n", (uintmax_t)(softc->ctios_sent - 2082229997Sken softc->ctios_returned), softc->ctios_sent, 2083229997Sken softc->ctios_returned); 2084229997Sken} 2085229997Sken 2086229997Sken/* 2087229997Sken * This function is called when we fail to get a CCB for a DMA or status return 2088229997Sken * to the initiator within the specified time period. 2089229997Sken * 2090229997Sken * The callout code should insure that we hold the sim mutex here. 2091229997Sken */ 2092229997Skenstatic void 2093229997Skenctlfe_dma_timeout(void *arg) 2094229997Sken{ 2095229997Sken struct ctlfe_lun_softc *softc; 2096229997Sken struct cam_periph *periph; 2097229997Sken struct cam_sim *sim; 2098229997Sken int num_queued; 2099229997Sken 2100229997Sken softc = (struct ctlfe_lun_softc *)arg; 2101229997Sken periph = softc->periph; 2102229997Sken sim = xpt_path_sim(periph->path); 2103229997Sken num_queued = 0; 2104229997Sken 2105229997Sken /* 2106229997Sken * Nothing to do... 2107229997Sken */ 2108229997Sken if (TAILQ_FIRST(&softc->work_queue) == NULL) { 2109229997Sken xpt_print(periph->path, "TIMEOUT triggered after %d " 2110229997Sken "seconds, but nothing on work queue??\n", 2111229997Sken CTLFE_DMA_TIMEOUT); 2112229997Sken return; 2113229997Sken } 2114229997Sken 2115229997Sken xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to " 2116229997Sken "start\n", CTLFE_DMA_TIMEOUT); 2117229997Sken 2118229997Sken ctlfe_dump_queue(softc); 2119229997Sken 2120229997Sken ctlfe_dump_sim(sim); 2121229997Sken 2122229997Sken xpt_print(periph->path, "calling xpt_schedule() to attempt to " 2123229997Sken "unstick our queue\n"); 2124229997Sken 2125229997Sken xpt_schedule(periph, /*priority*/ 1); 2126229997Sken 2127229997Sken xpt_print(periph->path, "xpt_schedule() call complete\n"); 2128229997Sken} 2129229997Sken 2130229997Sken/* 2131229997Sken * Datamove/done routine called by CTL. Put ourselves on the queue to 2132229997Sken * receive a CCB from CAM so we can queue the continue I/O request down 2133229997Sken * to the adapter. 2134229997Sken */ 2135229997Skenstatic void 2136229997Skenctlfe_datamove_done(union ctl_io *io) 2137229997Sken{ 2138229997Sken union ccb *ccb; 2139229997Sken struct cam_periph *periph; 2140229997Sken struct ctlfe_lun_softc *softc; 2141229997Sken 2142229997Sken ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2143229997Sken 2144229997Sken periph = xpt_path_periph(ccb->ccb_h.path); 2145260387Sscottl cam_periph_lock(periph); 2146229997Sken 2147229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 2148229997Sken 2149229997Sken if (io->io_hdr.io_type == CTL_IO_TASK) { 2150229997Sken /* 2151229997Sken * Task management commands don't require any further 2152229997Sken * communication back to the adapter. Requeue the CCB 2153229997Sken * to the adapter, and free the CTL I/O. 2154229997Sken */ 2155229997Sken xpt_print(ccb->ccb_h.path, "%s: returning task I/O " 2156229997Sken "tag %#x seq %#x\n", __func__, 2157229997Sken ccb->cin1.tag_id, ccb->cin1.seq_id); 2158229997Sken /* 2159229997Sken * Send the notify acknowledge down to the SIM, to let it 2160229997Sken * know we processed the task management command. 2161229997Sken */ 2162229997Sken ccb->ccb_h.status = CAM_REQ_INPROG; 2163229997Sken ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE; 2164229997Sken xpt_action(ccb); 2165229997Sken ctl_free_io(io); 2166229997Sken } else { 2167229997Sken if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 2168229997Sken io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED; 2169229997Sken else 2170229997Sken io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED; 2171229997Sken 2172229997Sken TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, 2173229997Sken periph_links.tqe); 2174229997Sken 2175229997Sken /* 2176229997Sken * Reset the timeout for our latest active DMA. 2177229997Sken */ 2178229997Sken callout_reset(&softc->dma_callout, 2179229997Sken CTLFE_DMA_TIMEOUT * hz, 2180229997Sken ctlfe_dma_timeout, softc); 2181229997Sken /* 2182229997Sken * Ask for the CAM transport layer to send us a CCB to do 2183229997Sken * the DMA or send status, unless ctlfe_dma_enabled is set 2184229997Sken * to 0. 2185229997Sken */ 2186229997Sken if (ctlfe_dma_enabled != 0) 2187229997Sken xpt_schedule(periph, /*priority*/ 1); 2188229997Sken } 2189229997Sken 2190260387Sscottl cam_periph_unlock(periph); 2191229997Sken} 2192229997Sken 2193229997Skenstatic void 2194229997Skenctlfe_dump(void) 2195229997Sken{ 2196229997Sken struct ctlfe_softc *bus_softc; 2197229997Sken 2198229997Sken STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) { 2199229997Sken struct ctlfe_lun_softc *lun_softc; 2200229997Sken 2201229997Sken ctlfe_dump_sim(bus_softc->sim); 2202229997Sken 2203229997Sken STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) { 2204229997Sken ctlfe_dump_queue(lun_softc); 2205229997Sken } 2206229997Sken } 2207229997Sken} 2208