scsi_ctl.c revision 245228
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: head/sys/cam/ctl/scsi_ctl.c 245228 2013-01-09 17:02:08Z ken $"); 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 { 76245228Sken CTLFE_CCB_DEFAULT = 0x00, 77229997Sken CTLFE_CCB_WAITING = 0x01 78229997Sken} ctlfe_ccb_types; 79229997Sken 80229997Skenstruct ctlfe_softc { 81229997Sken struct ctl_frontend fe; 82229997Sken path_id_t path_id; 83229997Sken struct cam_sim *sim; 84229997Sken char port_name[DEV_IDLEN]; 85229997Sken STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list; 86229997Sken STAILQ_ENTRY(ctlfe_softc) links; 87229997Sken}; 88229997Sken 89229997SkenSTAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list; 90229997Skenstruct mtx ctlfe_list_mtx; 91229997Skenstatic char ctlfe_mtx_desc[] = "ctlfelist"; 92229997Skenstatic int ctlfe_dma_enabled = 1; 93229997Sken#ifdef CTLFE_INIT_ENABLE 94229997Skenstatic int ctlfe_max_targets = 1; 95229997Skenstatic int ctlfe_num_targets = 0; 96229997Sken#endif 97229997Sken 98229997Skentypedef enum { 99229997Sken CTLFE_LUN_NONE = 0x00, 100229997Sken CTLFE_LUN_WILDCARD = 0x01 101229997Sken} ctlfe_lun_flags; 102229997Sken 103229997Skenstruct ctlfe_lun_softc { 104229997Sken struct ctlfe_softc *parent_softc; 105229997Sken struct cam_periph *periph; 106229997Sken ctlfe_lun_flags flags; 107229997Sken struct callout dma_callout; 108229997Sken uint64_t ccbs_alloced; 109229997Sken uint64_t ccbs_freed; 110229997Sken uint64_t ctios_sent; 111229997Sken uint64_t ctios_returned; 112229997Sken uint64_t atios_sent; 113229997Sken uint64_t atios_returned; 114229997Sken uint64_t inots_sent; 115229997Sken uint64_t inots_returned; 116229997Sken /* bus_dma_tag_t dma_tag; */ 117229997Sken TAILQ_HEAD(, ccb_hdr) work_queue; 118229997Sken STAILQ_ENTRY(ctlfe_lun_softc) links; 119229997Sken}; 120229997Sken 121229997Skentypedef enum { 122229997Sken CTLFE_CMD_NONE = 0x00, 123229997Sken CTLFE_CMD_PIECEWISE = 0x01 124229997Sken} ctlfe_cmd_flags; 125229997Sken 126229997Sken/* 127229997Sken * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h. 128229997Sken * Currently that is 600 bytes. 129229997Sken */ 130229997Skenstruct ctlfe_lun_cmd_info { 131229997Sken int cur_transfer_index; 132229997Sken ctlfe_cmd_flags flags; 133229997Sken /* 134229997Sken * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16 135229997Sken * bytes on amd64. So with 32 elements, this is 256 bytes on 136229997Sken * i386 and 512 bytes on amd64. 137229997Sken */ 138229997Sken bus_dma_segment_t cam_sglist[32]; 139229997Sken}; 140229997Sken 141229997Sken/* 142229997Sken * When we register the adapter/bus, request that this many ctl_ios be 143229997Sken * allocated. This should be the maximum supported by the adapter, but we 144229997Sken * currently don't have a way to get that back from the path inquiry. 145229997Sken * XXX KDM add that to the path inquiry. 146229997Sken */ 147229997Sken#define CTLFE_REQ_CTL_IO 4096 148229997Sken/* 149229997Sken * Number of Accept Target I/O CCBs to allocate and queue down to the 150229997Sken * adapter per LUN. 151229997Sken * XXX KDM should this be controlled by CTL? 152229997Sken */ 153229997Sken#define CTLFE_ATIO_PER_LUN 1024 154229997Sken/* 155229997Sken * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to 156229997Sken * allocate and queue down to the adapter per LUN. 157229997Sken * XXX KDM should this be controlled by CTL? 158229997Sken */ 159229997Sken#define CTLFE_IN_PER_LUN 1024 160229997Sken 161229997Sken/* 162229997Sken * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending 163229997Sken * status to the initiator. The SIM is expected to have its own timeouts, 164229997Sken * so we're not putting this timeout around the CCB execution time. The 165229997Sken * SIM should timeout and let us know if it has an issue. 166229997Sken */ 167229997Sken#define CTLFE_DMA_TIMEOUT 60 168229997Sken 169229997Sken/* 170229997Sken * Turn this on to enable extra debugging prints. 171229997Sken */ 172229997Sken#if 0 173229997Sken#define CTLFE_DEBUG 174229997Sken#endif 175229997Sken 176229997Sken/* 177229997Sken * Use randomly assigned WWNN/WWPN values. This is to work around an issue 178229997Sken * in the FreeBSD initiator that makes it unable to rescan the target if 179229997Sken * the target gets rebooted and the WWNN/WWPN stay the same. 180229997Sken */ 181229997Sken#if 0 182229997Sken#define RANDOM_WWNN 183229997Sken#endif 184229997Sken 185229997SkenSYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW, 186229997Sken &ctlfe_dma_enabled, 0, "DMA enabled"); 187229997SkenMALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface"); 188229997Sken 189229997Sken#define ccb_type ppriv_field0 190229997Sken/* This is only used in the ATIO */ 191229997Sken#define io_ptr ppriv_ptr1 192229997Sken 193229997Sken/* This is only used in the CTIO */ 194229997Sken#define ccb_atio ppriv_ptr1 195229997Sken 196229997Skenint ctlfeinitialize(void); 197229997Skenvoid ctlfeshutdown(void); 198229997Skenstatic periph_init_t ctlfeinit; 199229997Skenstatic void ctlfeasync(void *callback_arg, uint32_t code, 200229997Sken struct cam_path *path, void *arg); 201229997Skenstatic periph_ctor_t ctlferegister; 202229997Skenstatic periph_oninv_t ctlfeoninvalidate; 203229997Skenstatic periph_dtor_t ctlfecleanup; 204229997Skenstatic periph_start_t ctlfestart; 205229997Skenstatic void ctlfedone(struct cam_periph *periph, 206229997Sken union ccb *done_ccb); 207229997Sken 208229997Skenstatic void ctlfe_onoffline(void *arg, int online); 209229997Skenstatic void ctlfe_online(void *arg); 210229997Skenstatic void ctlfe_offline(void *arg); 211229997Skenstatic int ctlfe_targ_enable(void *arg, struct ctl_id targ_id); 212229997Skenstatic int ctlfe_targ_disable(void *arg, struct ctl_id targ_id); 213229997Skenstatic int ctlfe_lun_enable(void *arg, struct ctl_id targ_id, 214229997Sken int lun_id); 215229997Skenstatic int ctlfe_lun_disable(void *arg, struct ctl_id targ_id, 216229997Sken int lun_id); 217229997Skenstatic void ctlfe_dump_sim(struct cam_sim *sim); 218229997Skenstatic void ctlfe_dump_queue(struct ctlfe_lun_softc *softc); 219229997Skenstatic void ctlfe_dma_timeout(void *arg); 220229997Skenstatic void ctlfe_datamove_done(union ctl_io *io); 221229997Skenstatic void ctlfe_dump(void); 222229997Sken 223229997Skenstatic struct periph_driver ctlfe_driver = 224229997Sken{ 225229997Sken ctlfeinit, "ctl", 226229997Sken TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0 227229997Sken}; 228229997SkenPERIPHDRIVER_DECLARE(ctl, ctlfe_driver); 229229997Sken 230229997Skenextern struct ctl_softc *control_softc; 231237726Skenextern int ctl_disable; 232229997Sken 233229997Skenint 234229997Skenctlfeinitialize(void) 235229997Sken{ 236229997Sken cam_status status; 237229997Sken 238237726Sken /* Don't initialize if we're disabled */ 239237726Sken if (ctl_disable != 0) 240237726Sken return (0); 241237726Sken 242229997Sken STAILQ_INIT(&ctlfe_softc_list); 243229997Sken 244229997Sken mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF); 245229997Sken 246229997Sken xpt_lock_buses(); 247229997Sken periphdriver_register(&ctlfe_driver); 248229997Sken xpt_unlock_buses(); 249229997Sken 250229997Sken status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED | 251229997Sken AC_CONTRACT, ctlfeasync, NULL, NULL); 252229997Sken 253229997Sken if (status != CAM_REQ_CMP) { 254229997Sken printf("ctl: Failed to attach async callback due to CAM " 255229997Sken "status 0x%x!\n", status); 256229997Sken } 257229997Sken 258229997Sken return (0); 259229997Sken} 260229997Sken 261229997Skenvoid 262229997Skenctlfeshutdown(void) 263229997Sken{ 264229997Sken return; 265229997Sken} 266229997Sken 267229997Skenvoid 268229997Skenctlfeinit(void) 269229997Sken{ 270229997Sken cam_status status; 271229997Sken 272237726Sken /* Don't initialize if we're disabled */ 273237726Sken if (ctl_disable != 0) 274237726Sken return; 275237726Sken 276229997Sken STAILQ_INIT(&ctlfe_softc_list); 277229997Sken 278229997Sken mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF); 279229997Sken 280229997Sken KASSERT(control_softc != NULL, ("CTL is not initialized!")); 281229997Sken 282229997Sken status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED | 283229997Sken AC_CONTRACT, ctlfeasync, NULL, NULL); 284229997Sken 285229997Sken if (status != CAM_REQ_CMP) { 286229997Sken printf("ctl: Failed to attach async callback due to CAM " 287229997Sken "status 0x%x!\n", status); 288229997Sken } 289229997Sken} 290229997Sken 291229997Skenstatic void 292229997Skenctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg) 293229997Sken{ 294229997Sken 295229997Sken#ifdef CTLFEDEBUG 296229997Sken printf("%s: entered\n", __func__); 297229997Sken#endif 298229997Sken 299229997Sken /* 300229997Sken * When a new path gets registered, and it is capable of target 301229997Sken * mode, go ahead and attach. Later on, we may need to be more 302229997Sken * selective, but for now this will be sufficient. 303229997Sken */ 304229997Sken switch (code) { 305229997Sken case AC_PATH_REGISTERED: { 306229997Sken struct ctl_frontend *fe; 307229997Sken struct ctlfe_softc *bus_softc; 308229997Sken struct ccb_pathinq *cpi; 309229997Sken int retval; 310229997Sken 311229997Sken cpi = (struct ccb_pathinq *)arg; 312229997Sken 313229997Sken /* Don't attach if it doesn't support target mode */ 314229997Sken if ((cpi->target_sprt & PIT_PROCESSOR) == 0) { 315230033Sken#ifdef CTLFEDEBUG 316229997Sken printf("%s: SIM %s%d doesn't support target mode\n", 317229997Sken __func__, cpi->dev_name, cpi->unit_number); 318230033Sken#endif 319229997Sken break; 320229997Sken } 321229997Sken 322229997Sken#ifdef CTLFE_INIT_ENABLE 323229997Sken if (ctlfe_num_targets >= ctlfe_max_targets) { 324229997Sken union ccb *ccb; 325229997Sken struct cam_sim *sim; 326229997Sken 327229997Sken ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, 328229997Sken M_NOWAIT | M_ZERO); 329229997Sken if (ccb == NULL) { 330229997Sken printf("%s: unable to malloc CCB!\n", __func__); 331229997Sken return; 332229997Sken } 333229997Sken xpt_setup_ccb(&ccb->ccb_h, cpi->ccb_h.path, 334242174Smav CAM_PRIORITY_NONE); 335229997Sken 336229997Sken sim = xpt_path_sim(cpi->ccb_h.path); 337229997Sken 338229997Sken ccb->ccb_h.func_code = XPT_SET_SIM_KNOB; 339229997Sken ccb->knob.xport_specific.valid = KNOB_VALID_ROLE; 340229997Sken ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR; 341229997Sken 342229997Sken /* We should hold the SIM lock here */ 343229997Sken mtx_assert(sim->mtx, MA_OWNED); 344229997Sken 345229997Sken xpt_action(ccb); 346229997Sken 347229997Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 348229997Sken CAM_REQ_CMP) { 349229997Sken printf("%s: SIM %s%d (path id %d) initiator " 350229997Sken "enable failed with status %#x\n", 351229997Sken __func__, cpi->dev_name, 352229997Sken cpi->unit_number, cpi->ccb_h.path_id, 353229997Sken ccb->ccb_h.status); 354229997Sken } else { 355229997Sken printf("%s: SIM %s%d (path id %d) initiator " 356229997Sken "enable succeeded\n", 357229997Sken __func__, cpi->dev_name, 358229997Sken cpi->unit_number, cpi->ccb_h.path_id); 359229997Sken } 360229997Sken 361229997Sken free(ccb, M_TEMP); 362229997Sken 363229997Sken break; 364229997Sken } else { 365229997Sken ctlfe_num_targets++; 366229997Sken } 367229997Sken 368229997Sken printf("%s: ctlfe_num_targets = %d\n", __func__, 369229997Sken ctlfe_num_targets); 370229997Sken#endif /* CTLFE_INIT_ENABLE */ 371229997Sken 372229997Sken /* 373229997Sken * We're in an interrupt context here, so we have to 374229997Sken * use M_NOWAIT. Of course this means trouble if we 375229997Sken * can't allocate memory. 376229997Sken */ 377229997Sken bus_softc = malloc(sizeof(*bus_softc), M_CTLFE, 378229997Sken M_NOWAIT | M_ZERO); 379229997Sken if (bus_softc == NULL) { 380229997Sken printf("%s: unable to malloc %zd bytes for softc\n", 381229997Sken __func__, sizeof(*bus_softc)); 382229997Sken return; 383229997Sken } 384229997Sken 385229997Sken bus_softc->path_id = cpi->ccb_h.path_id; 386229997Sken bus_softc->sim = xpt_path_sim(cpi->ccb_h.path); 387229997Sken STAILQ_INIT(&bus_softc->lun_softc_list); 388229997Sken 389229997Sken fe = &bus_softc->fe; 390229997Sken 391229997Sken /* 392229997Sken * XXX KDM should we be more accurate here ? 393229997Sken */ 394229997Sken if (cpi->transport == XPORT_FC) 395229997Sken fe->port_type = CTL_PORT_FC; 396229997Sken else 397229997Sken fe->port_type = CTL_PORT_SCSI; 398229997Sken 399229997Sken /* XXX KDM what should the real number be here? */ 400229997Sken fe->num_requested_ctl_io = 4096; 401229997Sken snprintf(bus_softc->port_name, sizeof(bus_softc->port_name), 402229997Sken "%s%d", cpi->dev_name, cpi->unit_number); 403229997Sken /* 404229997Sken * XXX KDM it would be nice to allocate storage in the 405229997Sken * frontend structure itself. 406229997Sken */ 407229997Sken fe->port_name = bus_softc->port_name; 408229997Sken fe->physical_port = cpi->unit_number; 409229997Sken fe->virtual_port = cpi->bus_id; 410229997Sken fe->port_online = ctlfe_online; 411229997Sken fe->port_offline = ctlfe_offline; 412229997Sken fe->onoff_arg = bus_softc; 413229997Sken fe->targ_enable = ctlfe_targ_enable; 414229997Sken fe->targ_disable = ctlfe_targ_disable; 415229997Sken fe->lun_enable = ctlfe_lun_enable; 416229997Sken fe->lun_disable = ctlfe_lun_disable; 417229997Sken fe->targ_lun_arg = bus_softc; 418229997Sken fe->fe_datamove = ctlfe_datamove_done; 419229997Sken fe->fe_done = ctlfe_datamove_done; 420229997Sken fe->fe_dump = ctlfe_dump; 421229997Sken /* 422229997Sken * XXX KDM the path inquiry doesn't give us the maximum 423229997Sken * number of targets supported. 424229997Sken */ 425229997Sken fe->max_targets = cpi->max_target; 426229997Sken fe->max_target_id = cpi->max_target; 427229997Sken 428229997Sken /* 429229997Sken * XXX KDM need to figure out whether we're the master or 430229997Sken * slave. 431229997Sken */ 432230033Sken#ifdef CTLFEDEBUG 433229997Sken printf("%s: calling ctl_frontend_register() for %s%d\n", 434229997Sken __func__, cpi->dev_name, cpi->unit_number); 435230033Sken#endif 436229997Sken retval = ctl_frontend_register(fe, /*master_SC*/ 1); 437229997Sken if (retval != 0) { 438229997Sken printf("%s: ctl_frontend_register() failed with " 439229997Sken "error %d!\n", __func__, retval); 440229997Sken free(bus_softc, M_CTLFE); 441229997Sken break; 442229997Sken } else { 443229997Sken mtx_lock(&ctlfe_list_mtx); 444229997Sken STAILQ_INSERT_TAIL(&ctlfe_softc_list, bus_softc, links); 445229997Sken mtx_unlock(&ctlfe_list_mtx); 446229997Sken } 447229997Sken 448245228Sken break; 449245228Sken } 450245228Sken case AC_PATH_DEREGISTERED: { 451245228Sken struct ctlfe_softc *softc = NULL; 452245228Sken 453245228Sken mtx_lock(&ctlfe_list_mtx); 454245228Sken STAILQ_FOREACH(softc, &ctlfe_softc_list, links) { 455245228Sken if (softc->path_id == xpt_path_path_id(path)) { 456245228Sken STAILQ_REMOVE(&ctlfe_softc_list, softc, 457245228Sken ctlfe_softc, links); 458245228Sken break; 459245228Sken } 460229997Sken } 461245228Sken mtx_unlock(&ctlfe_list_mtx); 462245228Sken 463245228Sken if (softc != NULL) { 464245228Sken /* 465245228Sken * XXX KDM are we certain at this point that there 466245228Sken * are no outstanding commands for this frontend? 467245228Sken */ 468245228Sken ctl_frontend_deregister(&softc->fe); 469245228Sken free(softc, M_CTLFE); 470229997Sken } 471229997Sken break; 472229997Sken } 473229997Sken case AC_CONTRACT: { 474229997Sken struct ac_contract *ac; 475229997Sken 476229997Sken ac = (struct ac_contract *)arg; 477229997Sken 478229997Sken switch (ac->contract_number) { 479229997Sken case AC_CONTRACT_DEV_CHG: { 480229997Sken struct ac_device_changed *dev_chg; 481229997Sken struct ctlfe_softc *softc; 482229997Sken int retval, found; 483229997Sken 484229997Sken dev_chg = (struct ac_device_changed *)ac->contract_data; 485229997Sken 486236426Smjacob printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n", 487229997Sken __func__, dev_chg->wwpn, dev_chg->port, 488229997Sken xpt_path_path_id(path), dev_chg->target, 489229997Sken (dev_chg->arrived == 0) ? "left" : "arrived"); 490229997Sken 491229997Sken found = 0; 492229997Sken 493229997Sken mtx_lock(&ctlfe_list_mtx); 494229997Sken STAILQ_FOREACH(softc, &ctlfe_softc_list, links) { 495229997Sken if (softc->path_id == xpt_path_path_id(path)) { 496229997Sken found = 1; 497229997Sken break; 498229997Sken } 499229997Sken } 500229997Sken mtx_unlock(&ctlfe_list_mtx); 501229997Sken 502229997Sken if (found == 0) { 503229997Sken printf("%s: CTL port for CAM path %u not " 504229997Sken "found!\n", __func__, 505229997Sken xpt_path_path_id(path)); 506229997Sken break; 507229997Sken } 508229997Sken if (dev_chg->arrived != 0) { 509229997Sken retval = ctl_add_initiator(dev_chg->wwpn, 510229997Sken softc->fe.targ_port, dev_chg->target); 511229997Sken } else { 512229997Sken retval = ctl_remove_initiator( 513229997Sken softc->fe.targ_port, dev_chg->target); 514229997Sken } 515229997Sken 516229997Sken if (retval != 0) { 517229997Sken printf("%s: could not %s port %d iid %u " 518229997Sken "WWPN %#jx!\n", __func__, 519229997Sken (dev_chg->arrived != 0) ? "add" : 520229997Sken "remove", softc->fe.targ_port, 521229997Sken dev_chg->target, 522229997Sken (uintmax_t)dev_chg->wwpn); 523229997Sken } 524229997Sken break; 525229997Sken } 526229997Sken default: 527229997Sken printf("%s: unsupported contract number %ju\n", 528229997Sken __func__, (uintmax_t)ac->contract_number); 529229997Sken break; 530229997Sken } 531229997Sken break; 532229997Sken } 533229997Sken default: 534229997Sken break; 535229997Sken } 536229997Sken} 537229997Sken 538229997Skenstatic cam_status 539229997Skenctlferegister(struct cam_periph *periph, void *arg) 540229997Sken{ 541229997Sken struct ctlfe_softc *bus_softc; 542229997Sken struct ctlfe_lun_softc *softc; 543229997Sken struct cam_sim *sim; 544229997Sken union ccb en_lun_ccb; 545229997Sken cam_status status; 546229997Sken int i; 547229997Sken 548229997Sken softc = (struct ctlfe_lun_softc *)arg; 549229997Sken bus_softc = softc->parent_softc; 550229997Sken sim = xpt_path_sim(periph->path); 551229997Sken 552229997Sken TAILQ_INIT(&softc->work_queue); 553229997Sken softc->periph = periph; 554229997Sken 555229997Sken callout_init_mtx(&softc->dma_callout, sim->mtx, /*flags*/ 0); 556229997Sken periph->softc = softc; 557229997Sken 558242174Smav xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE); 559229997Sken en_lun_ccb.ccb_h.func_code = XPT_EN_LUN; 560229997Sken en_lun_ccb.cel.grp6_len = 0; 561229997Sken en_lun_ccb.cel.grp7_len = 0; 562229997Sken en_lun_ccb.cel.enable = 1; 563229997Sken xpt_action(&en_lun_ccb); 564229997Sken status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK); 565229997Sken if (status != CAM_REQ_CMP) { 566229997Sken xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n", 567229997Sken __func__, en_lun_ccb.ccb_h.status); 568229997Sken return (status); 569229997Sken } 570229997Sken 571229997Sken status = CAM_REQ_CMP; 572229997Sken 573229997Sken for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) { 574229997Sken union ccb *new_ccb; 575229997Sken 576229997Sken new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE, 577236426Smjacob M_ZERO|M_NOWAIT); 578229997Sken if (new_ccb == NULL) { 579229997Sken status = CAM_RESRC_UNAVAIL; 580229997Sken break; 581229997Sken } 582229997Sken xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1); 583229997Sken new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO; 584229997Sken new_ccb->ccb_h.cbfcnp = ctlfedone; 585229997Sken xpt_action(new_ccb); 586229997Sken softc->atios_sent++; 587229997Sken status = new_ccb->ccb_h.status; 588229997Sken if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 589229997Sken free(new_ccb, M_CTLFE); 590229997Sken break; 591229997Sken } 592229997Sken } 593229997Sken 594229997Sken status = cam_periph_acquire(periph); 595229997Sken if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 596229997Sken xpt_print(periph->path, "%s: could not acquire reference " 597229997Sken "count, status = %#x\n", __func__, status); 598229997Sken return (status); 599229997Sken } 600229997Sken 601229997Sken if (i == 0) { 602229997Sken xpt_print(periph->path, "%s: could not allocate ATIO CCBs, " 603229997Sken "status 0x%x\n", __func__, status); 604229997Sken return (CAM_REQ_CMP_ERR); 605229997Sken } 606229997Sken 607229997Sken for (i = 0; i < CTLFE_IN_PER_LUN; i++) { 608229997Sken union ccb *new_ccb; 609229997Sken 610229997Sken new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE, 611236426Smjacob M_ZERO|M_NOWAIT); 612229997Sken if (new_ccb == NULL) { 613229997Sken status = CAM_RESRC_UNAVAIL; 614229997Sken break; 615229997Sken } 616229997Sken 617229997Sken xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1); 618229997Sken new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY; 619229997Sken new_ccb->ccb_h.cbfcnp = ctlfedone; 620229997Sken xpt_action(new_ccb); 621229997Sken softc->inots_sent++; 622229997Sken status = new_ccb->ccb_h.status; 623237601Sken if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 624237601Sken /* 625237601Sken * Note that we don't free the CCB here. If the 626237601Sken * status is not CAM_REQ_INPROG, then we're 627237601Sken * probably talking to a SIM that says it is 628237601Sken * target-capable but doesn't support the 629237601Sken * XPT_IMMEDIATE_NOTIFY CCB. i.e. it supports the 630237601Sken * older API. In that case, it'll call xpt_done() 631237601Sken * on the CCB, and we need to free it in our done 632237601Sken * routine as a result. 633237601Sken */ 634229997Sken break; 635229997Sken } 636229997Sken } 637237601Sken if ((i == 0) 638237601Sken || (status != CAM_REQ_INPROG)) { 639229997Sken xpt_print(periph->path, "%s: could not allocate immediate " 640229997Sken "notify CCBs, status 0x%x\n", __func__, status); 641229997Sken return (CAM_REQ_CMP_ERR); 642229997Sken } 643229997Sken return (CAM_REQ_CMP); 644229997Sken} 645229997Sken 646229997Skenstatic void 647229997Skenctlfeoninvalidate(struct cam_periph *periph) 648229997Sken{ 649229997Sken union ccb en_lun_ccb; 650229997Sken cam_status status; 651229997Sken struct ctlfe_lun_softc *softc; 652229997Sken 653229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 654229997Sken 655242174Smav xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE); 656229997Sken en_lun_ccb.ccb_h.func_code = XPT_EN_LUN; 657229997Sken en_lun_ccb.cel.grp6_len = 0; 658229997Sken en_lun_ccb.cel.grp7_len = 0; 659229997Sken en_lun_ccb.cel.enable = 0; 660229997Sken xpt_action(&en_lun_ccb); 661229997Sken status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK); 662229997Sken if (status != CAM_REQ_CMP) { 663229997Sken xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n", 664229997Sken __func__, en_lun_ccb.ccb_h.status); 665229997Sken /* 666229997Sken * XXX KDM what do we do now? 667229997Sken */ 668229997Sken } 669229997Sken xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju " 670229997Sken "INOTs outstanding, %d refs\n", softc->atios_sent - 671229997Sken softc->atios_returned, softc->inots_sent - 672229997Sken softc->inots_returned, periph->refcount); 673229997Sken} 674229997Sken 675229997Skenstatic void 676229997Skenctlfecleanup(struct cam_periph *periph) 677229997Sken{ 678229997Sken struct ctlfe_lun_softc *softc; 679229997Sken struct ctlfe_softc *bus_softc; 680229997Sken 681229997Sken xpt_print(periph->path, "%s: Called\n", __func__); 682229997Sken 683229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 684229997Sken bus_softc = softc->parent_softc; 685229997Sken 686245228Sken STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links); 687229997Sken 688229997Sken /* 689229997Sken * XXX KDM is there anything else that needs to be done here? 690229997Sken */ 691245228Sken 692245228Sken callout_stop(&softc->dma_callout); 693245228Sken 694229997Sken free(softc, M_CTLFE); 695229997Sken} 696229997Sken 697229997Skenstatic void 698229997Skenctlfestart(struct cam_periph *periph, union ccb *start_ccb) 699229997Sken{ 700229997Sken struct ctlfe_lun_softc *softc; 701229997Sken struct ccb_hdr *ccb_h; 702229997Sken 703229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 704229997Sken 705229997Sken softc->ccbs_alloced++; 706229997Sken 707245228Sken start_ccb->ccb_h.ccb_type = CTLFE_CCB_DEFAULT; 708245228Sken 709229997Sken ccb_h = TAILQ_FIRST(&softc->work_queue); 710229997Sken if (periph->immediate_priority <= periph->pinfo.priority) { 711229997Sken panic("shouldn't get to the CCB waiting case!"); 712229997Sken start_ccb->ccb_h.ccb_type = CTLFE_CCB_WAITING; 713229997Sken SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 714229997Sken periph_links.sle); 715229997Sken periph->immediate_priority = CAM_PRIORITY_NONE; 716229997Sken wakeup(&periph->ccb_list); 717229997Sken } else if (ccb_h == NULL) { 718229997Sken softc->ccbs_freed++; 719229997Sken xpt_release_ccb(start_ccb); 720229997Sken } else { 721229997Sken struct ccb_accept_tio *atio; 722229997Sken struct ccb_scsiio *csio; 723229997Sken uint8_t *data_ptr; 724229997Sken uint32_t dxfer_len; 725229997Sken ccb_flags flags; 726229997Sken union ctl_io *io; 727229997Sken uint8_t scsi_status; 728229997Sken 729229997Sken /* Take the ATIO off the work queue */ 730229997Sken TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe); 731229997Sken atio = (struct ccb_accept_tio *)ccb_h; 732229997Sken io = (union ctl_io *)ccb_h->io_ptr; 733229997Sken csio = &start_ccb->csio; 734229997Sken 735229997Sken flags = atio->ccb_h.flags & 736229997Sken (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK); 737229997Sken 738229997Sken if ((io == NULL) 739229997Sken || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 740229997Sken /* 741229997Sken * We're done, send status back. 742229997Sken */ 743229997Sken flags |= CAM_SEND_STATUS; 744229997Sken if (io == NULL) { 745229997Sken scsi_status = SCSI_STATUS_BUSY; 746229997Sken csio->sense_len = 0; 747229997Sken } else if ((io->io_hdr.status & CTL_STATUS_MASK) == 748229997Sken CTL_CMD_ABORTED) { 749229997Sken io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED; 750229997Sken 751229997Sken /* 752229997Sken * If this command was aborted, we don't 753229997Sken * need to send status back to the SIM. 754229997Sken * Just free the CTIO and ctl_io, and 755229997Sken * recycle the ATIO back to the SIM. 756229997Sken */ 757229997Sken xpt_print(periph->path, "%s: aborted " 758229997Sken "command 0x%04x discarded\n", 759229997Sken __func__, io->scsiio.tag_num); 760229997Sken ctl_free_io(io); 761229997Sken /* 762229997Sken * For a wildcard attachment, commands can 763229997Sken * come in with a specific target/lun. Reset 764229997Sken * the target and LUN fields back to the 765229997Sken * wildcard values before we send them back 766229997Sken * down to the SIM. The SIM has a wildcard 767229997Sken * LUN enabled, not whatever target/lun 768229997Sken * these happened to be. 769229997Sken */ 770229997Sken if (softc->flags & CTLFE_LUN_WILDCARD) { 771229997Sken atio->ccb_h.target_id = 772229997Sken CAM_TARGET_WILDCARD; 773229997Sken atio->ccb_h.target_lun = 774229997Sken CAM_LUN_WILDCARD; 775229997Sken } 776229997Sken 777229997Sken if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) { 778229997Sken cam_release_devq(periph->path, 779229997Sken /*relsim_flags*/0, 780229997Sken /*reduction*/0, 781229997Sken /*timeout*/0, 782229997Sken /*getcount_only*/0); 783229997Sken atio->ccb_h.status &= ~CAM_DEV_QFRZN; 784229997Sken } 785229997Sken 786229997Sken ccb_h = TAILQ_FIRST(&softc->work_queue); 787229997Sken 788229997Sken if (atio->ccb_h.func_code != 789229997Sken XPT_ACCEPT_TARGET_IO) { 790229997Sken xpt_print(periph->path, "%s: func_code " 791229997Sken "is %#x\n", __func__, 792229997Sken atio->ccb_h.func_code); 793229997Sken } 794229997Sken start_ccb->ccb_h.func_code = XPT_ABORT; 795229997Sken start_ccb->cab.abort_ccb = (union ccb *)atio; 796229997Sken start_ccb->ccb_h.cbfcnp = ctlfedone; 797229997Sken 798229997Sken /* Tell the SIM that we've aborted this ATIO */ 799229997Sken xpt_action(start_ccb); 800229997Sken softc->ccbs_freed++; 801229997Sken xpt_release_ccb(start_ccb); 802229997Sken 803229997Sken /* 804229997Sken * Send the ATIO back down to the SIM. 805229997Sken */ 806229997Sken xpt_action((union ccb *)atio); 807229997Sken softc->atios_sent++; 808229997Sken 809229997Sken /* 810229997Sken * If we still have work to do, ask for 811229997Sken * another CCB. Otherwise, deactivate our 812229997Sken * callout. 813229997Sken */ 814229997Sken if (ccb_h != NULL) 815229997Sken xpt_schedule(periph, /*priority*/ 1); 816229997Sken else 817229997Sken callout_stop(&softc->dma_callout); 818229997Sken 819229997Sken return; 820229997Sken } else { 821229997Sken io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED; 822229997Sken scsi_status = io->scsiio.scsi_status; 823229997Sken csio->sense_len = io->scsiio.sense_len; 824229997Sken } 825229997Sken data_ptr = NULL; 826229997Sken dxfer_len = 0; 827229997Sken if (io == NULL) { 828229997Sken printf("%s: tag %04x io is NULL\n", __func__, 829229997Sken atio->tag_id); 830229997Sken } else { 831229997Sken#ifdef CTLFEDEBUG 832229997Sken printf("%s: tag %04x status %x\n", __func__, 833229997Sken atio->tag_id, io->io_hdr.status); 834229997Sken#endif 835229997Sken } 836229997Sken csio->sglist_cnt = 0; 837229997Sken if (csio->sense_len != 0) { 838229997Sken csio->sense_data = io->scsiio.sense_data; 839229997Sken flags |= CAM_SEND_SENSE; 840229997Sken } else if (scsi_status == SCSI_STATUS_CHECK_COND) { 841229997Sken xpt_print(periph->path, "%s: check condition " 842229997Sken "with no sense\n", __func__); 843229997Sken } 844229997Sken } else { 845229997Sken struct ctlfe_lun_cmd_info *cmd_info; 846229997Sken 847229997Sken /* 848229997Sken * Datamove call, we need to setup the S/G list. 849229997Sken */ 850229997Sken 851229997Sken cmd_info = (struct ctlfe_lun_cmd_info *) 852229997Sken io->io_hdr.port_priv; 853229997Sken 854229997Sken KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE, 855229997Sken ("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < " 856229997Sken "CTL_PORT_PRIV_SIZE %d", __func__, 857229997Sken sizeof(*cmd_info), CTL_PORT_PRIV_SIZE)); 858229997Sken io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED; 859229997Sken 860229997Sken /* 861229997Sken * Need to zero this, in case it has been used for 862229997Sken * a previous datamove for this particular I/O. 863229997Sken */ 864229997Sken bzero(cmd_info, sizeof(*cmd_info)); 865229997Sken scsi_status = 0; 866229997Sken 867229997Sken /* 868229997Sken * Set the direction, relative to the initiator. 869229997Sken */ 870229997Sken flags &= ~CAM_DIR_MASK; 871229997Sken if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 872229997Sken CTL_FLAG_DATA_IN) 873229997Sken flags |= CAM_DIR_IN; 874229997Sken else 875229997Sken flags |= CAM_DIR_OUT; 876229997Sken 877229997Sken csio->cdb_len = atio->cdb_len; 878229997Sken 879229997Sken if (io->scsiio.kern_sg_entries == 0) { 880229997Sken /* No S/G list */ 881229997Sken data_ptr = io->scsiio.kern_data_ptr; 882229997Sken dxfer_len = io->scsiio.kern_data_len; 883229997Sken csio->sglist_cnt = 0; 884229997Sken 885229997Sken if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) 886229997Sken flags |= CAM_DATA_PHYS; 887229997Sken } else if (io->scsiio.kern_sg_entries <= 888229997Sken (sizeof(cmd_info->cam_sglist)/ 889229997Sken sizeof(cmd_info->cam_sglist[0]))) { 890229997Sken /* 891229997Sken * S/G list with physical or virtual pointers. 892229997Sken * Just populate the CAM S/G list with the 893229997Sken * pointers. 894229997Sken */ 895229997Sken int i; 896229997Sken struct ctl_sg_entry *ctl_sglist; 897229997Sken bus_dma_segment_t *cam_sglist; 898229997Sken 899229997Sken ctl_sglist = (struct ctl_sg_entry *) 900229997Sken io->scsiio.kern_data_ptr; 901229997Sken cam_sglist = cmd_info->cam_sglist; 902229997Sken 903229997Sken for (i = 0; i < io->scsiio.kern_sg_entries;i++){ 904229997Sken cam_sglist[i].ds_addr = 905229997Sken (bus_addr_t)ctl_sglist[i].addr; 906229997Sken cam_sglist[i].ds_len = 907229997Sken ctl_sglist[i].len; 908229997Sken } 909229997Sken csio->sglist_cnt = io->scsiio.kern_sg_entries; 910229997Sken flags |= CAM_SCATTER_VALID; 911229997Sken if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) 912229997Sken flags |= CAM_SG_LIST_PHYS; 913229997Sken else 914229997Sken flags &= ~CAM_SG_LIST_PHYS; 915229997Sken data_ptr = (uint8_t *)cam_sglist; 916229997Sken dxfer_len = io->scsiio.kern_data_len; 917229997Sken } else { 918229997Sken /* S/G list with virtual pointers */ 919229997Sken struct ctl_sg_entry *sglist; 920229997Sken int *ti; 921229997Sken 922229997Sken /* 923245228Sken * If we have more S/G list pointers than 924245228Sken * will fit in the available storage in the 925245228Sken * cmd_info structure inside the ctl_io header, 926245228Sken * then we need to send down the pointers 927245228Sken * one element at a time. 928229997Sken */ 929245228Sken 930229997Sken sglist = (struct ctl_sg_entry *) 931229997Sken io->scsiio.kern_data_ptr; 932229997Sken ti = &cmd_info->cur_transfer_index; 933229997Sken data_ptr = sglist[*ti].addr; 934229997Sken dxfer_len = sglist[*ti].len; 935229997Sken csio->sglist_cnt = 0; 936229997Sken cmd_info->flags |= CTLFE_CMD_PIECEWISE; 937229997Sken (*ti)++; 938229997Sken } 939229997Sken 940229997Sken io->scsiio.ext_data_filled += dxfer_len; 941229997Sken 942229997Sken if (io->scsiio.ext_data_filled > 943229997Sken io->scsiio.kern_total_len) { 944229997Sken xpt_print(periph->path, "%s: tag 0x%04x " 945229997Sken "fill len %u > total %u\n", 946229997Sken __func__, io->scsiio.tag_num, 947229997Sken io->scsiio.ext_data_filled, 948229997Sken io->scsiio.kern_total_len); 949229997Sken } 950229997Sken } 951229997Sken 952229997Sken#ifdef CTLFEDEBUG 953229997Sken printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__, 954229997Sken (flags & CAM_SEND_STATUS) ? "done" : "datamove", 955229997Sken atio->tag_id, flags, data_ptr, dxfer_len); 956229997Sken#endif 957229997Sken 958229997Sken /* 959229997Sken * Valid combinations: 960229997Sken * - CAM_SEND_STATUS, SCATTER_VALID = 0, dxfer_len = 0, 961229997Sken * sglist_cnt = 0 962229997Sken * - CAM_SEND_STATUS = 0, SCATTER_VALID = 0, dxfer_len != 0, 963229997Sken * sglist_cnt = 0 964229997Sken * - CAM_SEND_STATUS = 0, SCATTER_VALID, dxfer_len != 0, 965229997Sken * sglist_cnt != 0 966229997Sken */ 967229997Sken#ifdef CTLFEDEBUG 968229997Sken if (((flags & CAM_SEND_STATUS) 969229997Sken && (((flags & CAM_SCATTER_VALID) != 0) 970229997Sken || (dxfer_len != 0) 971229997Sken || (csio->sglist_cnt != 0))) 972229997Sken || (((flags & CAM_SEND_STATUS) == 0) 973229997Sken && (dxfer_len == 0)) 974229997Sken || ((flags & CAM_SCATTER_VALID) 975229997Sken && (csio->sglist_cnt == 0)) 976229997Sken || (((flags & CAM_SCATTER_VALID) == 0) 977229997Sken && (csio->sglist_cnt != 0))) { 978229997Sken printf("%s: tag %04x cdb %02x flags %#x dxfer_len " 979229997Sken "%d sg %u\n", __func__, atio->tag_id, 980229997Sken atio->cdb_io.cdb_bytes[0], flags, dxfer_len, 981229997Sken csio->sglist_cnt); 982229997Sken if (io != NULL) { 983229997Sken printf("%s: tag %04x io status %#x\n", __func__, 984229997Sken atio->tag_id, io->io_hdr.status); 985229997Sken } else { 986229997Sken printf("%s: tag %04x no associated io\n", 987229997Sken __func__, atio->tag_id); 988229997Sken } 989229997Sken } 990229997Sken#endif 991229997Sken cam_fill_ctio(csio, 992229997Sken /*retries*/ 2, 993229997Sken ctlfedone, 994229997Sken flags, 995229997Sken (flags & CAM_TAG_ACTION_VALID) ? 996229997Sken MSG_SIMPLE_Q_TAG : 0, 997229997Sken atio->tag_id, 998229997Sken atio->init_id, 999229997Sken scsi_status, 1000229997Sken /*data_ptr*/ data_ptr, 1001229997Sken /*dxfer_len*/ dxfer_len, 1002229997Sken /*timeout*/ 5 * 1000); 1003229997Sken start_ccb->ccb_h.ccb_atio = atio; 1004229997Sken if (((flags & CAM_SEND_STATUS) == 0) 1005229997Sken && (io != NULL)) 1006229997Sken io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 1007229997Sken 1008229997Sken softc->ctios_sent++; 1009229997Sken 1010229997Sken xpt_action(start_ccb); 1011229997Sken 1012229997Sken if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1013229997Sken cam_release_devq(periph->path, 1014229997Sken /*relsim_flags*/0, 1015229997Sken /*reduction*/0, 1016229997Sken /*timeout*/0, 1017229997Sken /*getcount_only*/0); 1018229997Sken atio->ccb_h.status &= ~CAM_DEV_QFRZN; 1019229997Sken } 1020229997Sken 1021229997Sken ccb_h = TAILQ_FIRST(&softc->work_queue); 1022229997Sken } 1023229997Sken /* 1024229997Sken * If we still have work to do, ask for another CCB. Otherwise, 1025229997Sken * deactivate our callout. 1026229997Sken */ 1027229997Sken if (ccb_h != NULL) 1028229997Sken xpt_schedule(periph, /*priority*/ 1); 1029229997Sken else 1030229997Sken callout_stop(&softc->dma_callout); 1031229997Sken} 1032229997Sken 1033229997Skenstatic void 1034229997Skenctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb) 1035229997Sken{ 1036229997Sken struct ctlfe_lun_softc *softc; 1037229997Sken 1038229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 1039229997Sken 1040229997Sken switch (ccb->ccb_h.func_code) { 1041229997Sken case XPT_ACCEPT_TARGET_IO: 1042229997Sken softc->atios_returned++; 1043229997Sken break; 1044229997Sken case XPT_IMMEDIATE_NOTIFY: 1045229997Sken case XPT_NOTIFY_ACKNOWLEDGE: 1046229997Sken softc->inots_returned++; 1047229997Sken break; 1048229997Sken default: 1049229997Sken break; 1050229997Sken } 1051229997Sken 1052229997Sken free(ccb, M_CTLFE); 1053229997Sken 1054229997Sken KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: " 1055229997Sken "atios_returned %ju > atios_sent %ju", __func__, 1056229997Sken softc->atios_returned, softc->atios_sent)); 1057229997Sken KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: " 1058229997Sken "inots_returned %ju > inots_sent %ju", __func__, 1059229997Sken softc->inots_returned, softc->inots_sent)); 1060229997Sken 1061229997Sken /* 1062229997Sken * If we have received all of our CCBs, we can release our 1063229997Sken * reference on the peripheral driver. It will probably go away 1064229997Sken * now. 1065229997Sken */ 1066229997Sken if ((softc->atios_returned == softc->atios_sent) 1067229997Sken && (softc->inots_returned == softc->inots_sent)) { 1068229997Sken cam_periph_release_locked(periph); 1069229997Sken } 1070229997Sken} 1071229997Sken 1072238870Smjacobstatic int 1073238870Smjacobctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset) 1074238870Smjacob{ 1075238870Smjacob uint64_t lba; 1076238870Smjacob uint32_t num_blocks, nbc; 1077238870Smjacob uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)? 1078238870Smjacob atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes; 1079238870Smjacob 1080238870Smjacob nbc = offset >> 9; /* ASSUMING 512 BYTE BLOCKS */ 1081238870Smjacob 1082238870Smjacob switch (cmdbyt[0]) { 1083238870Smjacob case READ_6: 1084238870Smjacob case WRITE_6: 1085238870Smjacob { 1086238870Smjacob struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt; 1087238870Smjacob lba = scsi_3btoul(cdb->addr); 1088238870Smjacob lba &= 0x1fffff; 1089238870Smjacob num_blocks = cdb->length; 1090238870Smjacob if (num_blocks == 0) 1091238870Smjacob num_blocks = 256; 1092238870Smjacob lba += nbc; 1093238870Smjacob num_blocks -= nbc; 1094238870Smjacob scsi_ulto3b(lba, cdb->addr); 1095238870Smjacob cdb->length = num_blocks; 1096238870Smjacob break; 1097238870Smjacob } 1098238870Smjacob case READ_10: 1099238870Smjacob case WRITE_10: 1100238870Smjacob { 1101238870Smjacob struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt; 1102238870Smjacob lba = scsi_4btoul(cdb->addr); 1103238870Smjacob num_blocks = scsi_2btoul(cdb->length); 1104238870Smjacob lba += nbc; 1105238870Smjacob num_blocks -= nbc; 1106238870Smjacob scsi_ulto4b(lba, cdb->addr); 1107238870Smjacob scsi_ulto2b(num_blocks, cdb->length); 1108238870Smjacob break; 1109238870Smjacob } 1110238870Smjacob case READ_12: 1111238870Smjacob case WRITE_12: 1112238870Smjacob { 1113238870Smjacob struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt; 1114238870Smjacob lba = scsi_4btoul(cdb->addr); 1115238870Smjacob num_blocks = scsi_4btoul(cdb->length); 1116238870Smjacob lba += nbc; 1117238870Smjacob num_blocks -= nbc; 1118238870Smjacob scsi_ulto4b(lba, cdb->addr); 1119238870Smjacob scsi_ulto4b(num_blocks, cdb->length); 1120238870Smjacob break; 1121238870Smjacob } 1122238870Smjacob case READ_16: 1123238870Smjacob case WRITE_16: 1124238870Smjacob { 1125238870Smjacob struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt; 1126238870Smjacob lba = scsi_8btou64(cdb->addr); 1127238870Smjacob num_blocks = scsi_4btoul(cdb->length); 1128238870Smjacob lba += nbc; 1129238870Smjacob num_blocks -= nbc; 1130238870Smjacob scsi_u64to8b(lba, cdb->addr); 1131238870Smjacob scsi_ulto4b(num_blocks, cdb->length); 1132238870Smjacob break; 1133238870Smjacob } 1134238870Smjacob default: 1135238870Smjacob return -1; 1136238870Smjacob } 1137238870Smjacob return (0); 1138238870Smjacob} 1139238870Smjacob 1140229997Skenstatic void 1141229997Skenctlfedone(struct cam_periph *periph, union ccb *done_ccb) 1142229997Sken{ 1143229997Sken struct ctlfe_lun_softc *softc; 1144229997Sken struct ctlfe_softc *bus_softc; 1145238870Smjacob struct ccb_accept_tio *atio = NULL; 1146238870Smjacob union ctl_io *io = NULL; 1147229997Sken 1148229997Sken#ifdef CTLFE_DEBUG 1149229997Sken printf("%s: entered, func_code = %#x, type = %#lx\n", __func__, 1150229997Sken done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type); 1151229997Sken#endif 1152229997Sken 1153229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 1154229997Sken bus_softc = softc->parent_softc; 1155229997Sken 1156229997Sken if (done_ccb->ccb_h.ccb_type == CTLFE_CCB_WAITING) { 1157229997Sken panic("shouldn't get to the CCB waiting case!"); 1158229997Sken wakeup(&done_ccb->ccb_h.cbfcnp); 1159229997Sken return; 1160229997Sken } 1161229997Sken 1162229997Sken /* 1163229997Sken * If the peripheral is invalid, ATIOs and immediate notify CCBs 1164229997Sken * need to be freed. Most of the ATIOs and INOTs that come back 1165229997Sken * will be CCBs that are being returned from the SIM as a result of 1166229997Sken * our disabling the LUN. 1167229997Sken * 1168229997Sken * Other CCB types are handled in their respective cases below. 1169229997Sken */ 1170229997Sken if (periph->flags & CAM_PERIPH_INVALID) { 1171229997Sken switch (done_ccb->ccb_h.func_code) { 1172229997Sken case XPT_ACCEPT_TARGET_IO: 1173229997Sken case XPT_IMMEDIATE_NOTIFY: 1174229997Sken case XPT_NOTIFY_ACKNOWLEDGE: 1175229997Sken ctlfe_free_ccb(periph, done_ccb); 1176229997Sken return; 1177229997Sken default: 1178229997Sken break; 1179229997Sken } 1180229997Sken 1181229997Sken } 1182229997Sken switch (done_ccb->ccb_h.func_code) { 1183229997Sken case XPT_ACCEPT_TARGET_IO: { 1184229997Sken 1185229997Sken atio = &done_ccb->atio; 1186229997Sken 1187229997Sken softc->atios_returned++; 1188229997Sken 1189238870Smjacob resubmit: 1190229997Sken /* 1191229997Sken * Allocate a ctl_io, pass it to CTL, and wait for the 1192229997Sken * datamove or done. 1193229997Sken */ 1194229997Sken io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref); 1195229997Sken if (io == NULL) { 1196229997Sken atio->ccb_h.flags &= ~CAM_DIR_MASK; 1197229997Sken atio->ccb_h.flags |= CAM_DIR_NONE; 1198229997Sken 1199229997Sken printf("%s: ctl_alloc_io failed!\n", __func__); 1200229997Sken 1201229997Sken /* 1202229997Sken * XXX KDM need to set SCSI_STATUS_BUSY, but there 1203229997Sken * is no field in the ATIO structure to do that, 1204229997Sken * and we aren't able to allocate a ctl_io here. 1205229997Sken * What to do? 1206229997Sken */ 1207229997Sken atio->sense_len = 0; 1208229997Sken done_ccb->ccb_h.io_ptr = NULL; 1209229997Sken TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h, 1210229997Sken periph_links.tqe); 1211229997Sken xpt_schedule(periph, /*priority*/ 1); 1212229997Sken break; 1213229997Sken } 1214229997Sken ctl_zero_io(io); 1215229997Sken 1216229997Sken /* Save pointers on both sides */ 1217229997Sken io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb; 1218229997Sken done_ccb->ccb_h.io_ptr = io; 1219229997Sken 1220229997Sken /* 1221229997Sken * Only SCSI I/O comes down this path, resets, etc. come 1222229997Sken * down the immediate notify path below. 1223229997Sken */ 1224229997Sken io->io_hdr.io_type = CTL_IO_SCSI; 1225229997Sken io->io_hdr.nexus.initid.id = atio->init_id; 1226229997Sken io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port; 1227229997Sken io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id; 1228229997Sken io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun; 1229229997Sken io->scsiio.tag_num = atio->tag_id; 1230229997Sken switch (atio->tag_action) { 1231229997Sken case CAM_TAG_ACTION_NONE: 1232229997Sken io->scsiio.tag_type = CTL_TAG_UNTAGGED; 1233229997Sken break; 1234229997Sken case MSG_SIMPLE_TASK: 1235229997Sken io->scsiio.tag_type = CTL_TAG_SIMPLE; 1236229997Sken break; 1237229997Sken case MSG_HEAD_OF_QUEUE_TASK: 1238229997Sken io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE; 1239229997Sken break; 1240229997Sken case MSG_ORDERED_TASK: 1241229997Sken io->scsiio.tag_type = CTL_TAG_ORDERED; 1242229997Sken break; 1243229997Sken case MSG_ACA_TASK: 1244229997Sken io->scsiio.tag_type = CTL_TAG_ACA; 1245229997Sken break; 1246229997Sken default: 1247229997Sken io->scsiio.tag_type = CTL_TAG_UNTAGGED; 1248229997Sken printf("%s: unhandled tag type %#x!!\n", __func__, 1249229997Sken atio->tag_action); 1250229997Sken break; 1251229997Sken } 1252229997Sken if (atio->cdb_len > sizeof(io->scsiio.cdb)) { 1253229997Sken printf("%s: WARNING: CDB len %d > ctl_io space %zd\n", 1254229997Sken __func__, atio->cdb_len, sizeof(io->scsiio.cdb)); 1255229997Sken } 1256229997Sken io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb)); 1257229997Sken bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb, 1258229997Sken io->scsiio.cdb_len); 1259229997Sken 1260229997Sken#ifdef CTLFEDEBUG 1261229997Sken printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__, 1262229997Sken (uintmax_t)io->io_hdr.nexus.initid.id, 1263229997Sken io->io_hdr.nexus.targ_port, 1264229997Sken (uintmax_t)io->io_hdr.nexus.targ_target.id, 1265229997Sken io->io_hdr.nexus.targ_lun, 1266229997Sken io->scsiio.tag_num, io->scsiio.cdb[0]); 1267229997Sken#endif 1268229997Sken 1269229997Sken ctl_queue(io); 1270229997Sken break; 1271229997Sken } 1272229997Sken case XPT_CONT_TARGET_IO: { 1273238870Smjacob int srr = 0; 1274238870Smjacob uint32_t srr_off = 0; 1275229997Sken 1276229997Sken atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio; 1277229997Sken io = (union ctl_io *)atio->ccb_h.io_ptr; 1278229997Sken 1279229997Sken softc->ctios_returned++; 1280229997Sken#ifdef CTLFEDEBUG 1281229997Sken printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n", 1282229997Sken __func__, atio->tag_id, done_ccb->ccb_h.flags); 1283229997Sken#endif 1284229997Sken /* 1285238870Smjacob * Handle SRR case were the data pointer is pushed back hack 1286238870Smjacob */ 1287238870Smjacob if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV 1288238870Smjacob && done_ccb->csio.msg_ptr != NULL 1289238870Smjacob && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED 1290238870Smjacob && done_ccb->csio.msg_ptr[1] == 5 1291238870Smjacob && done_ccb->csio.msg_ptr[2] == 0) { 1292238870Smjacob srr = 1; 1293238870Smjacob srr_off = 1294238870Smjacob (done_ccb->csio.msg_ptr[3] << 24) 1295238870Smjacob | (done_ccb->csio.msg_ptr[4] << 16) 1296238870Smjacob | (done_ccb->csio.msg_ptr[5] << 8) 1297238870Smjacob | (done_ccb->csio.msg_ptr[6]); 1298238870Smjacob } 1299238870Smjacob 1300238870Smjacob if (srr && (done_ccb->ccb_h.flags & CAM_SEND_STATUS)) { 1301238870Smjacob /* 1302238870Smjacob * If status was being sent, the back end data is now 1303238870Smjacob * history. Hack it up and resubmit a new command with 1304238870Smjacob * the CDB adjusted. If the SIM does the right thing, 1305238870Smjacob * all of the resid math should work. 1306238870Smjacob */ 1307238870Smjacob softc->ccbs_freed++; 1308238870Smjacob xpt_release_ccb(done_ccb); 1309238870Smjacob ctl_free_io(io); 1310238870Smjacob if (ctlfe_adjust_cdb(atio, srr_off) == 0) { 1311238870Smjacob done_ccb = (union ccb *)atio; 1312238870Smjacob goto resubmit; 1313238870Smjacob } 1314238870Smjacob /* 1315238870Smjacob * Fall through to doom.... 1316238870Smjacob */ 1317238870Smjacob } else if (srr) { 1318238870Smjacob /* 1319238870Smjacob * If we have an srr and we're still sending data, we 1320238870Smjacob * should be able to adjust offsets and cycle again. 1321238870Smjacob */ 1322238870Smjacob io->scsiio.kern_rel_offset = 1323238870Smjacob io->scsiio.ext_data_filled = srr_off; 1324238870Smjacob io->scsiio.ext_data_len = io->scsiio.kern_total_len - 1325238870Smjacob io->scsiio.kern_rel_offset; 1326238870Smjacob softc->ccbs_freed++; 1327238870Smjacob io->scsiio.io_hdr.status = CTL_STATUS_NONE; 1328238870Smjacob xpt_release_ccb(done_ccb); 1329238870Smjacob TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h, 1330238870Smjacob periph_links.tqe); 1331238870Smjacob xpt_schedule(periph, /*priority*/ 1); 1332238870Smjacob return; 1333238870Smjacob } 1334238870Smjacob 1335238870Smjacob /* 1336229997Sken * If we were sending status back to the initiator, free up 1337229997Sken * resources. If we were doing a datamove, call the 1338229997Sken * datamove done routine. 1339229997Sken */ 1340229997Sken if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) { 1341229997Sken softc->ccbs_freed++; 1342229997Sken xpt_release_ccb(done_ccb); 1343229997Sken ctl_free_io(io); 1344229997Sken /* 1345229997Sken * For a wildcard attachment, commands can come in 1346229997Sken * with a specific target/lun. Reset the target 1347229997Sken * and LUN fields back to the wildcard values before 1348229997Sken * we send them back down to the SIM. The SIM has 1349229997Sken * a wildcard LUN enabled, not whatever target/lun 1350229997Sken * these happened to be. 1351229997Sken */ 1352229997Sken if (softc->flags & CTLFE_LUN_WILDCARD) { 1353229997Sken atio->ccb_h.target_id = CAM_TARGET_WILDCARD; 1354229997Sken atio->ccb_h.target_lun = CAM_LUN_WILDCARD; 1355229997Sken } 1356229997Sken if (periph->flags & CAM_PERIPH_INVALID) { 1357229997Sken ctlfe_free_ccb(periph, (union ccb *)atio); 1358229997Sken return; 1359229997Sken } else { 1360229997Sken xpt_action((union ccb *)atio); 1361229997Sken softc->atios_sent++; 1362229997Sken } 1363229997Sken } else { 1364229997Sken struct ctlfe_lun_cmd_info *cmd_info; 1365229997Sken struct ccb_scsiio *csio; 1366229997Sken 1367229997Sken csio = &done_ccb->csio; 1368229997Sken cmd_info = (struct ctlfe_lun_cmd_info *) 1369229997Sken io->io_hdr.port_priv; 1370229997Sken 1371229997Sken io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1372229997Sken 1373229997Sken io->scsiio.ext_data_len += csio->dxfer_len; 1374229997Sken if (io->scsiio.ext_data_len > 1375229997Sken io->scsiio.kern_total_len) { 1376229997Sken xpt_print(periph->path, "%s: tag 0x%04x " 1377229997Sken "done len %u > total %u sent %u\n", 1378229997Sken __func__, io->scsiio.tag_num, 1379229997Sken io->scsiio.ext_data_len, 1380229997Sken io->scsiio.kern_total_len, 1381229997Sken io->scsiio.ext_data_filled); 1382229997Sken } 1383229997Sken /* 1384229997Sken * Translate CAM status to CTL status. Success 1385229997Sken * does not change the overall, ctl_io status. In 1386229997Sken * that case we just set port_status to 0. If we 1387229997Sken * have a failure, though, set a data phase error 1388229997Sken * for the overall ctl_io. 1389229997Sken */ 1390229997Sken switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) { 1391229997Sken case CAM_REQ_CMP: 1392229997Sken io->io_hdr.port_status = 0; 1393229997Sken break; 1394229997Sken default: 1395229997Sken /* 1396245228Sken * XXX KDM we probably need to figure out a 1397245228Sken * standard set of errors that the SIM 1398245228Sken * drivers should return in the event of a 1399245228Sken * data transfer failure. A data phase 1400245228Sken * error will at least point the user to a 1401245228Sken * data transfer error of some sort. 1402245228Sken * Hopefully the SIM printed out some 1403245228Sken * additional information to give the user 1404245228Sken * a clue what happened. 1405229997Sken */ 1406229997Sken io->io_hdr.port_status = 0xbad1; 1407229997Sken ctl_set_data_phase_error(&io->scsiio); 1408229997Sken /* 1409229997Sken * XXX KDM figure out residual. 1410229997Sken */ 1411229997Sken break; 1412229997Sken } 1413229997Sken /* 1414229997Sken * If we had to break this S/G list into multiple 1415229997Sken * pieces, figure out where we are in the list, and 1416229997Sken * continue sending pieces if necessary. 1417229997Sken */ 1418229997Sken if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) 1419229997Sken && (io->io_hdr.port_status == 0) 1420229997Sken && (cmd_info->cur_transfer_index < 1421229997Sken io->scsiio.kern_sg_entries)) { 1422229997Sken struct ctl_sg_entry *sglist; 1423229997Sken ccb_flags flags; 1424229997Sken uint8_t scsi_status; 1425229997Sken uint8_t *data_ptr; 1426229997Sken uint32_t dxfer_len; 1427229997Sken int *ti; 1428229997Sken 1429229997Sken sglist = (struct ctl_sg_entry *) 1430229997Sken io->scsiio.kern_data_ptr; 1431229997Sken ti = &cmd_info->cur_transfer_index; 1432229997Sken flags = atio->ccb_h.flags & 1433229997Sken (CAM_DIS_DISCONNECT| 1434229997Sken CAM_TAG_ACTION_VALID| 1435229997Sken CAM_DIR_MASK); 1436229997Sken 1437229997Sken /* 1438229997Sken * Set the direction, relative to the initiator. 1439229997Sken */ 1440229997Sken flags &= ~CAM_DIR_MASK; 1441229997Sken if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1442229997Sken CTL_FLAG_DATA_IN) 1443229997Sken flags |= CAM_DIR_IN; 1444229997Sken else 1445229997Sken flags |= CAM_DIR_OUT; 1446229997Sken 1447229997Sken data_ptr = sglist[*ti].addr; 1448229997Sken dxfer_len = sglist[*ti].len; 1449229997Sken (*ti)++; 1450229997Sken 1451229997Sken scsi_status = 0; 1452229997Sken 1453229997Sken if (((flags & CAM_SEND_STATUS) == 0) 1454229997Sken && (dxfer_len == 0)) { 1455229997Sken printf("%s: tag %04x no status or " 1456229997Sken "len cdb = %02x\n", __func__, 1457229997Sken atio->tag_id, 1458229997Sken atio->cdb_io.cdb_bytes[0]); 1459229997Sken printf("%s: tag %04x io status %#x\n", 1460229997Sken __func__, atio->tag_id, 1461229997Sken io->io_hdr.status); 1462229997Sken } 1463229997Sken 1464229997Sken cam_fill_ctio(csio, 1465229997Sken /*retries*/ 2, 1466229997Sken ctlfedone, 1467229997Sken flags, 1468229997Sken (flags & CAM_TAG_ACTION_VALID) ? 1469229997Sken MSG_SIMPLE_Q_TAG : 0, 1470229997Sken atio->tag_id, 1471229997Sken atio->init_id, 1472229997Sken scsi_status, 1473229997Sken /*data_ptr*/ data_ptr, 1474229997Sken /*dxfer_len*/ dxfer_len, 1475229997Sken /*timeout*/ 5 * 1000); 1476229997Sken 1477229997Sken csio->resid = 0; 1478229997Sken csio->ccb_h.ccb_atio = atio; 1479229997Sken io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 1480229997Sken softc->ctios_sent++; 1481229997Sken xpt_action((union ccb *)csio); 1482229997Sken } else { 1483229997Sken /* 1484229997Sken * Release the CTIO. The ATIO will be sent back 1485229997Sken * down to the SIM once we send status. 1486229997Sken */ 1487229997Sken softc->ccbs_freed++; 1488229997Sken xpt_release_ccb(done_ccb); 1489229997Sken 1490229997Sken /* Call the backend move done callback */ 1491229997Sken io->scsiio.be_move_done(io); 1492229997Sken } 1493229997Sken } 1494229997Sken break; 1495229997Sken } 1496229997Sken case XPT_IMMEDIATE_NOTIFY: { 1497229997Sken union ctl_io *io; 1498229997Sken struct ccb_immediate_notify *inot; 1499229997Sken cam_status status; 1500229997Sken int frozen; 1501229997Sken 1502229997Sken inot = &done_ccb->cin1; 1503229997Sken 1504229997Sken softc->inots_returned++; 1505229997Sken 1506229997Sken frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 1507229997Sken 1508229997Sken printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x " 1509229997Sken "seq %#x\n", __func__, inot->ccb_h.status, 1510229997Sken inot->tag_id, inot->seq_id); 1511229997Sken 1512229997Sken io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref); 1513229997Sken if (io != NULL) { 1514229997Sken int send_ctl_io; 1515229997Sken 1516229997Sken send_ctl_io = 1; 1517229997Sken 1518229997Sken ctl_zero_io(io); 1519229997Sken io->io_hdr.io_type = CTL_IO_TASK; 1520229997Sken io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb; 1521229997Sken inot->ccb_h.io_ptr = io; 1522229997Sken io->io_hdr.nexus.initid.id = inot->initiator_id; 1523229997Sken io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port; 1524229997Sken io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id; 1525229997Sken io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun; 1526229997Sken /* XXX KDM should this be the tag_id? */ 1527229997Sken io->taskio.tag_num = inot->seq_id; 1528229997Sken 1529229997Sken status = inot->ccb_h.status & CAM_STATUS_MASK; 1530229997Sken switch (status) { 1531229997Sken case CAM_SCSI_BUS_RESET: 1532229997Sken io->taskio.task_action = CTL_TASK_BUS_RESET; 1533229997Sken break; 1534229997Sken case CAM_BDR_SENT: 1535229997Sken io->taskio.task_action = CTL_TASK_TARGET_RESET; 1536229997Sken break; 1537229997Sken case CAM_MESSAGE_RECV: 1538229997Sken switch (inot->arg) { 1539229997Sken case MSG_ABORT_TASK_SET: 1540229997Sken /* 1541229997Sken * XXX KDM this isn't currently 1542229997Sken * supported by CTL. It ends up 1543229997Sken * being a no-op. 1544229997Sken */ 1545229997Sken io->taskio.task_action = 1546229997Sken CTL_TASK_ABORT_TASK_SET; 1547229997Sken break; 1548229997Sken case MSG_TARGET_RESET: 1549229997Sken io->taskio.task_action = 1550229997Sken CTL_TASK_TARGET_RESET; 1551229997Sken break; 1552229997Sken case MSG_ABORT_TASK: 1553229997Sken io->taskio.task_action = 1554229997Sken CTL_TASK_ABORT_TASK; 1555229997Sken break; 1556229997Sken case MSG_LOGICAL_UNIT_RESET: 1557229997Sken io->taskio.task_action = 1558229997Sken CTL_TASK_LUN_RESET; 1559229997Sken break; 1560229997Sken case MSG_CLEAR_TASK_SET: 1561229997Sken /* 1562229997Sken * XXX KDM this isn't currently 1563229997Sken * supported by CTL. It ends up 1564229997Sken * being a no-op. 1565229997Sken */ 1566229997Sken io->taskio.task_action = 1567229997Sken CTL_TASK_CLEAR_TASK_SET; 1568229997Sken break; 1569229997Sken case MSG_CLEAR_ACA: 1570229997Sken io->taskio.task_action = 1571229997Sken CTL_TASK_CLEAR_ACA; 1572229997Sken break; 1573229997Sken case MSG_NOOP: 1574229997Sken send_ctl_io = 0; 1575229997Sken break; 1576229997Sken default: 1577229997Sken xpt_print(periph->path, "%s: " 1578229997Sken "unsupported message 0x%x\n", 1579229997Sken __func__, inot->arg); 1580229997Sken send_ctl_io = 0; 1581229997Sken break; 1582229997Sken } 1583229997Sken break; 1584229997Sken case CAM_REQ_ABORTED: 1585229997Sken /* 1586229997Sken * This request was sent back by the driver. 1587229997Sken * XXX KDM what do we do here? 1588229997Sken */ 1589229997Sken send_ctl_io = 0; 1590229997Sken break; 1591237601Sken case CAM_REQ_INVALID: 1592237601Sken case CAM_PROVIDE_FAIL: 1593229997Sken default: 1594237601Sken /* 1595237601Sken * We should only get here if we're talking 1596237601Sken * to a talking to a SIM that is target 1597237601Sken * capable but supports the old API. In 1598237601Sken * that case, we need to just free the CCB. 1599237601Sken * If we actually send a notify acknowledge, 1600237601Sken * it will send that back with an error as 1601237601Sken * well. 1602237601Sken */ 1603237601Sken 1604237601Sken if ((status != CAM_REQ_INVALID) 1605237601Sken && (status != CAM_PROVIDE_FAIL)) 1606237601Sken xpt_print(periph->path, "%s: " 1607237601Sken "unsupported CAM status " 1608237601Sken "0x%x\n", __func__, status); 1609237601Sken 1610237601Sken ctl_free_io(io); 1611237601Sken ctlfe_free_ccb(periph, done_ccb); 1612237601Sken 1613237601Sken return; 1614229997Sken } 1615229997Sken if (send_ctl_io != 0) { 1616229997Sken ctl_queue(io); 1617229997Sken } else { 1618229997Sken ctl_free_io(io); 1619229997Sken done_ccb->ccb_h.status = CAM_REQ_INPROG; 1620229997Sken done_ccb->ccb_h.func_code = 1621229997Sken XPT_NOTIFY_ACKNOWLEDGE; 1622229997Sken xpt_action(done_ccb); 1623229997Sken } 1624229997Sken } else { 1625229997Sken xpt_print(periph->path, "%s: could not allocate " 1626229997Sken "ctl_io for immediate notify!\n", __func__); 1627229997Sken /* requeue this to the adapter */ 1628229997Sken done_ccb->ccb_h.status = CAM_REQ_INPROG; 1629229997Sken done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE; 1630229997Sken xpt_action(done_ccb); 1631229997Sken } 1632229997Sken 1633229997Sken if (frozen != 0) { 1634229997Sken cam_release_devq(periph->path, 1635229997Sken /*relsim_flags*/ 0, 1636229997Sken /*opening reduction*/ 0, 1637229997Sken /*timeout*/ 0, 1638229997Sken /*getcount_only*/ 0); 1639229997Sken } 1640229997Sken break; 1641229997Sken } 1642229997Sken case XPT_NOTIFY_ACKNOWLEDGE: 1643229997Sken /* 1644229997Sken * Queue this back down to the SIM as an immediate notify. 1645229997Sken */ 1646229997Sken done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY; 1647229997Sken xpt_action(done_ccb); 1648229997Sken softc->inots_sent++; 1649229997Sken break; 1650229997Sken case XPT_ABORT: 1651229997Sken /* 1652229997Sken * XPT_ABORT is an immediate CCB, we shouldn't get here. 1653229997Sken */ 1654229997Sken panic("%s: XPT_ABORT CCB returned!", __func__); 1655229997Sken break; 1656229997Sken case XPT_SET_SIM_KNOB: 1657229997Sken case XPT_GET_SIM_KNOB: 1658229997Sken break; 1659229997Sken default: 1660229997Sken panic("%s: unexpected CCB type %#x", __func__, 1661229997Sken done_ccb->ccb_h.func_code); 1662229997Sken break; 1663229997Sken } 1664229997Sken} 1665229997Sken 1666229997Skenstatic void 1667229997Skenctlfe_onoffline(void *arg, int online) 1668229997Sken{ 1669229997Sken struct ctlfe_softc *bus_softc; 1670229997Sken union ccb *ccb; 1671229997Sken cam_status status; 1672229997Sken struct cam_path *path; 1673229997Sken struct cam_sim *sim; 1674229997Sken int set_wwnn; 1675229997Sken 1676229997Sken bus_softc = (struct ctlfe_softc *)arg; 1677229997Sken 1678229997Sken set_wwnn = 0; 1679229997Sken 1680244052Sken sim = bus_softc->sim; 1681244052Sken 1682245228Sken mtx_assert(sim->mtx, MA_OWNED); 1683245228Sken 1684229997Sken status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id, 1685229997Sken CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 1686229997Sken if (status != CAM_REQ_CMP) { 1687229997Sken printf("%s: unable to create path!\n", __func__); 1688229997Sken return; 1689229997Sken } 1690245228Sken ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_NOWAIT | M_ZERO); 1691245228Sken if (ccb == NULL) { 1692245228Sken printf("%s: unable to malloc CCB!\n", __func__); 1693245228Sken xpt_free_path(path); 1694245228Sken return; 1695245228Sken } 1696242174Smav xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE); 1697229997Sken 1698229997Sken /* 1699229997Sken * Copan WWN format: 1700229997Sken * 1701229997Sken * Bits 63-60: 0x5 NAA, IEEE registered name 1702229997Sken * Bits 59-36: 0x000ED5 IEEE Company name assigned to Copan 1703229997Sken * Bits 35-12: Copan SSN (Sequential Serial Number) 1704229997Sken * Bits 11-8: Type of port: 1705229997Sken * 1 == N-Port 1706229997Sken * 2 == F-Port 1707229997Sken * 3 == NL-Port 1708229997Sken * Bits 7-0: 0 == Node Name, >0 == Port Number 1709229997Sken */ 1710229997Sken 1711229997Sken if (online != 0) { 1712229997Sken 1713229997Sken ccb->ccb_h.func_code = XPT_GET_SIM_KNOB; 1714229997Sken 1715229997Sken 1716229997Sken xpt_action(ccb); 1717229997Sken 1718229997Sken 1719229997Sken if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){ 1720229997Sken#ifdef RANDOM_WWNN 1721229997Sken uint64_t random_bits; 1722229997Sken#endif 1723229997Sken 1724229997Sken printf("%s: %s current WWNN %#jx\n", __func__, 1725229997Sken bus_softc->port_name, 1726229997Sken ccb->knob.xport_specific.fc.wwnn); 1727229997Sken printf("%s: %s current WWPN %#jx\n", __func__, 1728229997Sken bus_softc->port_name, 1729229997Sken ccb->knob.xport_specific.fc.wwpn); 1730229997Sken 1731229997Sken#ifdef RANDOM_WWNN 1732229997Sken arc4rand(&random_bits, sizeof(random_bits), 0); 1733229997Sken#endif 1734229997Sken 1735229997Sken /* 1736229997Sken * XXX KDM this is a bit of a kludge for now. We 1737229997Sken * take the current WWNN/WWPN from the card, and 1738229997Sken * replace the company identifier and the NL-Port 1739229997Sken * indicator and the port number (for the WWPN). 1740229997Sken * This should be replaced later with ddb_GetWWNN, 1741229997Sken * or possibly a more centralized scheme. (It 1742229997Sken * would be nice to have the WWNN/WWPN for each 1743229997Sken * port stored in the ctl_frontend structure.) 1744229997Sken */ 1745229997Sken#ifdef RANDOM_WWNN 1746229997Sken ccb->knob.xport_specific.fc.wwnn = 1747229997Sken (random_bits & 1748229997Sken 0x0000000fffffff00ULL) | 1749229997Sken /* Company ID */ 0x5000ED5000000000ULL | 1750229997Sken /* NL-Port */ 0x0300; 1751229997Sken ccb->knob.xport_specific.fc.wwpn = 1752229997Sken (random_bits & 1753229997Sken 0x0000000fffffff00ULL) | 1754229997Sken /* Company ID */ 0x5000ED5000000000ULL | 1755229997Sken /* NL-Port */ 0x3000 | 1756229997Sken /* Port Num */ (bus_softc->fe.targ_port & 0xff); 1757229997Sken 1758229997Sken /* 1759229997Sken * This is a bit of an API break/reversal, but if 1760229997Sken * we're doing the random WWNN that's a little 1761229997Sken * different anyway. So record what we're actually 1762229997Sken * using with the frontend code so it's reported 1763229997Sken * accurately. 1764229997Sken */ 1765229997Sken bus_softc->fe.wwnn = 1766229997Sken ccb->knob.xport_specific.fc.wwnn; 1767229997Sken bus_softc->fe.wwpn = 1768229997Sken ccb->knob.xport_specific.fc.wwpn; 1769229997Sken set_wwnn = 1; 1770229997Sken#else /* RANDOM_WWNN */ 1771229997Sken /* 1772229997Sken * If the user has specified a WWNN/WWPN, send them 1773229997Sken * down to the SIM. Otherwise, record what the SIM 1774229997Sken * has reported. 1775229997Sken */ 1776229997Sken if ((bus_softc->fe.wwnn != 0) 1777229997Sken && (bus_softc->fe.wwpn != 0)) { 1778229997Sken ccb->knob.xport_specific.fc.wwnn = 1779229997Sken bus_softc->fe.wwnn; 1780229997Sken ccb->knob.xport_specific.fc.wwpn = 1781229997Sken bus_softc->fe.wwpn; 1782229997Sken set_wwnn = 1; 1783229997Sken } else { 1784229997Sken bus_softc->fe.wwnn = 1785229997Sken ccb->knob.xport_specific.fc.wwnn; 1786229997Sken bus_softc->fe.wwpn = 1787229997Sken ccb->knob.xport_specific.fc.wwpn; 1788229997Sken } 1789229997Sken#endif /* RANDOM_WWNN */ 1790229997Sken 1791229997Sken 1792229997Sken if (set_wwnn != 0) { 1793229997Sken printf("%s: %s new WWNN %#jx\n", __func__, 1794229997Sken bus_softc->port_name, 1795229997Sken ccb->knob.xport_specific.fc.wwnn); 1796229997Sken printf("%s: %s new WWPN %#jx\n", __func__, 1797229997Sken bus_softc->port_name, 1798229997Sken ccb->knob.xport_specific.fc.wwpn); 1799229997Sken } 1800229997Sken } else { 1801229997Sken printf("%s: %s has no valid WWNN/WWPN\n", __func__, 1802229997Sken bus_softc->port_name); 1803229997Sken } 1804229997Sken } 1805229997Sken ccb->ccb_h.func_code = XPT_SET_SIM_KNOB; 1806229997Sken ccb->knob.xport_specific.valid = KNOB_VALID_ROLE; 1807229997Sken if (set_wwnn != 0) 1808229997Sken ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS; 1809229997Sken 1810229997Sken if (online != 0) 1811229997Sken ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET; 1812229997Sken else 1813229997Sken ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE; 1814229997Sken 1815229997Sken xpt_action(ccb); 1816229997Sken 1817229997Sken if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1818229997Sken printf("%s: SIM %s (path id %d) target %s failed with " 1819229997Sken "status %#x\n", 1820229997Sken __func__, bus_softc->port_name, bus_softc->path_id, 1821229997Sken (online != 0) ? "enable" : "disable", 1822229997Sken ccb->ccb_h.status); 1823229997Sken } else { 1824229997Sken printf("%s: SIM %s (path id %d) target %s succeeded\n", 1825229997Sken __func__, bus_softc->port_name, bus_softc->path_id, 1826229997Sken (online != 0) ? "enable" : "disable"); 1827229997Sken } 1828229997Sken 1829229997Sken xpt_free_path(path); 1830229997Sken 1831244016Sken free(ccb, M_TEMP); 1832244016Sken 1833229997Sken return; 1834229997Sken} 1835229997Sken 1836229997Skenstatic void 1837229997Skenctlfe_online(void *arg) 1838229997Sken{ 1839245228Sken struct ctlfe_softc *bus_softc; 1840245228Sken struct cam_path *path; 1841245228Sken cam_status status; 1842245228Sken struct ctlfe_lun_softc *lun_softc; 1843245228Sken struct cam_sim *sim; 1844245228Sken 1845245228Sken bus_softc = (struct ctlfe_softc *)arg; 1846245228Sken sim = bus_softc->sim; 1847245228Sken 1848245228Sken CAM_SIM_LOCK(sim); 1849245228Sken 1850245228Sken /* 1851245228Sken * Create the wildcard LUN before bringing the port online. 1852245228Sken */ 1853245228Sken status = xpt_create_path(&path, /*periph*/ NULL, 1854245228Sken bus_softc->path_id, CAM_TARGET_WILDCARD, 1855245228Sken CAM_LUN_WILDCARD); 1856245228Sken if (status != CAM_REQ_CMP) { 1857245228Sken printf("%s: unable to create path for wildcard periph\n", 1858245228Sken __func__); 1859245228Sken CAM_SIM_UNLOCK(sim); 1860245228Sken return; 1861245228Sken } 1862245228Sken 1863245228Sken lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, 1864245228Sken M_NOWAIT | M_ZERO); 1865245228Sken if (lun_softc == NULL) { 1866245228Sken xpt_print(path, "%s: unable to allocate softc for " 1867245228Sken "wildcard periph\n", __func__); 1868245228Sken xpt_free_path(path); 1869245228Sken CAM_SIM_UNLOCK(sim); 1870245228Sken return; 1871245228Sken } 1872245228Sken 1873245228Sken lun_softc->parent_softc = bus_softc; 1874245228Sken lun_softc->flags |= CTLFE_LUN_WILDCARD; 1875245228Sken 1876245228Sken STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, lun_softc, links); 1877245228Sken 1878245228Sken 1879245228Sken status = cam_periph_alloc(ctlferegister, 1880245228Sken ctlfeoninvalidate, 1881245228Sken ctlfecleanup, 1882245228Sken ctlfestart, 1883245228Sken "ctl", 1884245228Sken CAM_PERIPH_BIO, 1885245228Sken path, 1886245228Sken ctlfeasync, 1887245228Sken 0, 1888245228Sken lun_softc); 1889245228Sken 1890245228Sken if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1891245228Sken const struct cam_status_entry *entry; 1892245228Sken 1893245228Sken entry = cam_fetch_status_entry(status); 1894245228Sken 1895245228Sken printf("%s: CAM error %s (%#x) returned from " 1896245228Sken "cam_periph_alloc()\n", __func__, (entry != NULL) ? 1897245228Sken entry->status_text : "Unknown", status); 1898245228Sken } 1899245228Sken 1900245228Sken xpt_free_path(path); 1901245228Sken 1902229997Sken ctlfe_onoffline(arg, /*online*/ 1); 1903245228Sken 1904245228Sken CAM_SIM_UNLOCK(sim); 1905229997Sken} 1906229997Sken 1907229997Skenstatic void 1908229997Skenctlfe_offline(void *arg) 1909229997Sken{ 1910245228Sken struct ctlfe_softc *bus_softc; 1911245228Sken struct cam_path *path; 1912245228Sken cam_status status; 1913245228Sken struct cam_periph *periph; 1914245228Sken struct cam_sim *sim; 1915245228Sken 1916245228Sken bus_softc = (struct ctlfe_softc *)arg; 1917245228Sken sim = bus_softc->sim; 1918245228Sken 1919245228Sken CAM_SIM_LOCK(sim); 1920245228Sken 1921229997Sken ctlfe_onoffline(arg, /*online*/ 0); 1922245228Sken 1923245228Sken /* 1924245228Sken * Disable the wildcard LUN for this port now that we have taken 1925245228Sken * the port offline. 1926245228Sken */ 1927245228Sken status = xpt_create_path(&path, /*periph*/ NULL, 1928245228Sken bus_softc->path_id, CAM_TARGET_WILDCARD, 1929245228Sken CAM_LUN_WILDCARD); 1930245228Sken if (status != CAM_REQ_CMP) { 1931245228Sken CAM_SIM_UNLOCK(sim); 1932245228Sken printf("%s: unable to create path for wildcard periph\n", 1933245228Sken __func__); 1934245228Sken return; 1935245228Sken } 1936245228Sken 1937245228Sken 1938245228Sken if ((periph = cam_periph_find(path, "ctl")) != NULL) 1939245228Sken cam_periph_invalidate(periph); 1940245228Sken 1941245228Sken xpt_free_path(path); 1942245228Sken 1943245228Sken CAM_SIM_UNLOCK(sim); 1944229997Sken} 1945229997Sken 1946229997Skenstatic int 1947229997Skenctlfe_targ_enable(void *arg, struct ctl_id targ_id) 1948229997Sken{ 1949229997Sken return (0); 1950229997Sken} 1951229997Sken 1952229997Skenstatic int 1953229997Skenctlfe_targ_disable(void *arg, struct ctl_id targ_id) 1954229997Sken{ 1955229997Sken return (0); 1956229997Sken} 1957229997Sken 1958229997Sken/* 1959229997Sken * This will get called to enable a LUN on every bus that is attached to 1960229997Sken * CTL. So we only need to create a path/periph for this particular bus. 1961229997Sken */ 1962229997Skenstatic int 1963229997Skenctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1964229997Sken{ 1965229997Sken struct ctlfe_softc *bus_softc; 1966229997Sken struct ctlfe_lun_softc *softc; 1967229997Sken struct cam_path *path; 1968229997Sken struct cam_periph *periph; 1969229997Sken struct cam_sim *sim; 1970229997Sken cam_status status; 1971229997Sken 1972229997Sken 1973229997Sken bus_softc = (struct ctlfe_softc *)arg; 1974244052Sken sim = bus_softc->sim; 1975229997Sken 1976245228Sken status = xpt_create_path_unlocked(&path, /*periph*/ NULL, 1977245228Sken bus_softc->path_id, 1978245228Sken targ_id.id, lun_id); 1979229997Sken /* XXX KDM need some way to return status to CTL here? */ 1980229997Sken if (status != CAM_REQ_CMP) { 1981229997Sken printf("%s: could not create path, status %#x\n", __func__, 1982229997Sken status); 1983229997Sken return (1); 1984229997Sken } 1985229997Sken 1986229997Sken softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO); 1987244052Sken CAM_SIM_LOCK(sim); 1988229997Sken periph = cam_periph_find(path, "ctl"); 1989229997Sken if (periph != NULL) { 1990229997Sken /* We've already got a periph, no need to alloc a new one. */ 1991229997Sken xpt_free_path(path); 1992229997Sken free(softc, M_CTLFE); 1993244052Sken CAM_SIM_UNLOCK(sim); 1994229997Sken return (0); 1995229997Sken } 1996229997Sken 1997229997Sken softc->parent_softc = bus_softc; 1998229997Sken STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links); 1999229997Sken 2000229997Sken status = cam_periph_alloc(ctlferegister, 2001229997Sken ctlfeoninvalidate, 2002229997Sken ctlfecleanup, 2003229997Sken ctlfestart, 2004229997Sken "ctl", 2005229997Sken CAM_PERIPH_BIO, 2006229997Sken path, 2007229997Sken ctlfeasync, 2008229997Sken 0, 2009229997Sken softc); 2010229997Sken 2011244016Sken xpt_free_path(path); 2012244016Sken 2013244052Sken CAM_SIM_UNLOCK(sim); 2014229997Sken 2015229997Sken return (0); 2016229997Sken} 2017229997Sken 2018229997Sken/* 2019245228Sken * This will get called when the user removes a LUN to disable that LUN 2020245228Sken * on every bus that is attached to CTL. 2021229997Sken */ 2022229997Skenstatic int 2023229997Skenctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 2024229997Sken{ 2025229997Sken struct ctlfe_softc *softc; 2026229997Sken struct ctlfe_lun_softc *lun_softc; 2027245228Sken struct cam_sim *sim; 2028229997Sken 2029229997Sken softc = (struct ctlfe_softc *)arg; 2030245228Sken sim = softc->sim; 2031229997Sken 2032245228Sken CAM_SIM_LOCK(sim); 2033229997Sken STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) { 2034229997Sken struct cam_path *path; 2035229997Sken 2036229997Sken path = lun_softc->periph->path; 2037229997Sken 2038229997Sken if ((xpt_path_target_id(path) == targ_id.id) 2039229997Sken && (xpt_path_lun_id(path) == lun_id)) { 2040229997Sken break; 2041229997Sken } 2042229997Sken } 2043229997Sken if (lun_softc == NULL) { 2044245228Sken CAM_SIM_UNLOCK(sim); 2045229997Sken printf("%s: can't find target %d lun %d\n", __func__, 2046229997Sken targ_id.id, lun_id); 2047229997Sken return (1); 2048229997Sken } 2049229997Sken 2050229997Sken cam_periph_invalidate(lun_softc->periph); 2051229997Sken 2052245228Sken CAM_SIM_UNLOCK(sim); 2053229997Sken 2054229997Sken return (0); 2055229997Sken} 2056229997Sken 2057229997Skenstatic void 2058229997Skenctlfe_dump_sim(struct cam_sim *sim) 2059229997Sken{ 2060229997Sken int i; 2061229997Sken 2062229997Sken printf("%s%d: max tagged openings: %d, max dev openings: %d\n", 2063229997Sken sim->sim_name, sim->unit_number, 2064229997Sken sim->max_tagged_dev_openings, sim->max_dev_openings); 2065229997Sken printf("%s%d: max_ccbs: %u, ccb_count: %u\n", 2066229997Sken sim->sim_name, sim->unit_number, 2067229997Sken sim->max_ccbs, sim->ccb_count); 2068229997Sken printf("%s%d: ccb_freeq is %sempty\n", 2069229997Sken sim->sim_name, sim->unit_number, 2070229997Sken (SLIST_FIRST(&sim->ccb_freeq) == NULL) ? "" : "NOT "); 2071229997Sken printf("%s%d: alloc_queue.entries %d, alloc_openings %d\n", 2072229997Sken sim->sim_name, sim->unit_number, 2073229997Sken sim->devq->alloc_queue.entries, sim->devq->alloc_openings); 2074229997Sken printf("%s%d: qfrozen_cnt:", sim->sim_name, sim->unit_number); 2075229997Sken for (i = 0; i < CAM_RL_VALUES; i++) { 2076229997Sken printf("%s%u", (i != 0) ? ":" : "", 2077229997Sken sim->devq->alloc_queue.qfrozen_cnt[i]); 2078229997Sken } 2079229997Sken printf("\n"); 2080229997Sken} 2081229997Sken 2082229997Sken/* 2083229997Sken * Assumes that the SIM lock is held. 2084229997Sken */ 2085229997Skenstatic void 2086229997Skenctlfe_dump_queue(struct ctlfe_lun_softc *softc) 2087229997Sken{ 2088229997Sken struct ccb_hdr *hdr; 2089229997Sken struct cam_periph *periph; 2090229997Sken int num_items; 2091229997Sken 2092229997Sken periph = softc->periph; 2093229997Sken num_items = 0; 2094229997Sken 2095229997Sken TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) { 2096229997Sken union ctl_io *io; 2097229997Sken 2098229997Sken io = hdr->io_ptr; 2099229997Sken 2100229997Sken num_items++; 2101229997Sken 2102229997Sken /* 2103229997Sken * This can happen when we get an ATIO but can't allocate 2104229997Sken * a ctl_io. See the XPT_ACCEPT_TARGET_IO case in ctlfedone(). 2105229997Sken */ 2106229997Sken if (io == NULL) { 2107229997Sken struct ccb_scsiio *csio; 2108229997Sken 2109229997Sken csio = (struct ccb_scsiio *)hdr; 2110229997Sken 2111229997Sken xpt_print(periph->path, "CCB %#x ctl_io allocation " 2112229997Sken "failed\n", csio->tag_id); 2113229997Sken continue; 2114229997Sken } 2115229997Sken 2116229997Sken /* 2117229997Sken * Only regular SCSI I/O is put on the work 2118229997Sken * queue, so we can print sense here. There may be no 2119229997Sken * sense if it's no the queue for a DMA, but this serves to 2120229997Sken * print out the CCB as well. 2121229997Sken * 2122229997Sken * XXX KDM switch this over to scsi_sense_print() when 2123229997Sken * CTL is merged in with CAM. 2124229997Sken */ 2125229997Sken ctl_io_error_print(io, NULL); 2126229997Sken 2127229997Sken /* 2128229997Sken * We're sending status back to the 2129229997Sken * initiator, so we're on the queue waiting 2130229997Sken * for a CTIO to do that. 2131229997Sken */ 2132229997Sken if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 2133229997Sken continue; 2134229997Sken 2135229997Sken /* 2136229997Sken * Otherwise, we're on the queue waiting to 2137229997Sken * do a data transfer. 2138229997Sken */ 2139229997Sken xpt_print(periph->path, "Total %u, Current %u, Resid %u\n", 2140229997Sken io->scsiio.kern_total_len, io->scsiio.kern_data_len, 2141229997Sken io->scsiio.kern_data_resid); 2142229997Sken } 2143229997Sken 2144229997Sken xpt_print(periph->path, "%d requests total waiting for CCBs\n", 2145229997Sken num_items); 2146229997Sken xpt_print(periph->path, "%ju CCBs oustanding (%ju allocated, %ju " 2147229997Sken "freed)\n", (uintmax_t)(softc->ccbs_alloced - 2148229997Sken softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced, 2149229997Sken (uintmax_t)softc->ccbs_freed); 2150229997Sken xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju " 2151229997Sken "returned\n", (uintmax_t)(softc->ctios_sent - 2152229997Sken softc->ctios_returned), softc->ctios_sent, 2153229997Sken softc->ctios_returned); 2154229997Sken} 2155229997Sken 2156229997Sken/* 2157229997Sken * This function is called when we fail to get a CCB for a DMA or status return 2158229997Sken * to the initiator within the specified time period. 2159229997Sken * 2160229997Sken * The callout code should insure that we hold the sim mutex here. 2161229997Sken */ 2162229997Skenstatic void 2163229997Skenctlfe_dma_timeout(void *arg) 2164229997Sken{ 2165229997Sken struct ctlfe_lun_softc *softc; 2166229997Sken struct cam_periph *periph; 2167229997Sken struct cam_sim *sim; 2168229997Sken int num_queued; 2169229997Sken 2170229997Sken softc = (struct ctlfe_lun_softc *)arg; 2171229997Sken periph = softc->periph; 2172229997Sken sim = xpt_path_sim(periph->path); 2173229997Sken num_queued = 0; 2174229997Sken 2175229997Sken /* 2176229997Sken * Nothing to do... 2177229997Sken */ 2178229997Sken if (TAILQ_FIRST(&softc->work_queue) == NULL) { 2179229997Sken xpt_print(periph->path, "TIMEOUT triggered after %d " 2180229997Sken "seconds, but nothing on work queue??\n", 2181229997Sken CTLFE_DMA_TIMEOUT); 2182229997Sken return; 2183229997Sken } 2184229997Sken 2185229997Sken xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to " 2186229997Sken "start\n", CTLFE_DMA_TIMEOUT); 2187229997Sken 2188229997Sken ctlfe_dump_queue(softc); 2189229997Sken 2190229997Sken ctlfe_dump_sim(sim); 2191229997Sken 2192229997Sken xpt_print(periph->path, "calling xpt_schedule() to attempt to " 2193229997Sken "unstick our queue\n"); 2194229997Sken 2195229997Sken xpt_schedule(periph, /*priority*/ 1); 2196229997Sken 2197229997Sken xpt_print(periph->path, "xpt_schedule() call complete\n"); 2198229997Sken} 2199229997Sken 2200229997Sken/* 2201229997Sken * Datamove/done routine called by CTL. Put ourselves on the queue to 2202229997Sken * receive a CCB from CAM so we can queue the continue I/O request down 2203229997Sken * to the adapter. 2204229997Sken */ 2205229997Skenstatic void 2206229997Skenctlfe_datamove_done(union ctl_io *io) 2207229997Sken{ 2208229997Sken union ccb *ccb; 2209229997Sken struct cam_sim *sim; 2210229997Sken struct cam_periph *periph; 2211229997Sken struct ctlfe_lun_softc *softc; 2212229997Sken 2213229997Sken ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2214229997Sken 2215229997Sken sim = xpt_path_sim(ccb->ccb_h.path); 2216229997Sken 2217245228Sken CAM_SIM_LOCK(sim); 2218229997Sken 2219229997Sken periph = xpt_path_periph(ccb->ccb_h.path); 2220229997Sken 2221229997Sken softc = (struct ctlfe_lun_softc *)periph->softc; 2222229997Sken 2223229997Sken if (io->io_hdr.io_type == CTL_IO_TASK) { 2224229997Sken /* 2225229997Sken * Task management commands don't require any further 2226229997Sken * communication back to the adapter. Requeue the CCB 2227229997Sken * to the adapter, and free the CTL I/O. 2228229997Sken */ 2229229997Sken xpt_print(ccb->ccb_h.path, "%s: returning task I/O " 2230229997Sken "tag %#x seq %#x\n", __func__, 2231229997Sken ccb->cin1.tag_id, ccb->cin1.seq_id); 2232229997Sken /* 2233229997Sken * Send the notify acknowledge down to the SIM, to let it 2234229997Sken * know we processed the task management command. 2235229997Sken */ 2236229997Sken ccb->ccb_h.status = CAM_REQ_INPROG; 2237229997Sken ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE; 2238229997Sken xpt_action(ccb); 2239229997Sken ctl_free_io(io); 2240229997Sken } else { 2241229997Sken if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 2242229997Sken io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED; 2243229997Sken else 2244229997Sken io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED; 2245229997Sken 2246229997Sken TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, 2247229997Sken periph_links.tqe); 2248229997Sken 2249229997Sken /* 2250229997Sken * Reset the timeout for our latest active DMA. 2251229997Sken */ 2252229997Sken callout_reset(&softc->dma_callout, 2253229997Sken CTLFE_DMA_TIMEOUT * hz, 2254229997Sken ctlfe_dma_timeout, softc); 2255229997Sken /* 2256229997Sken * Ask for the CAM transport layer to send us a CCB to do 2257229997Sken * the DMA or send status, unless ctlfe_dma_enabled is set 2258229997Sken * to 0. 2259229997Sken */ 2260229997Sken if (ctlfe_dma_enabled != 0) 2261229997Sken xpt_schedule(periph, /*priority*/ 1); 2262229997Sken } 2263229997Sken 2264245228Sken CAM_SIM_UNLOCK(sim); 2265229997Sken} 2266229997Sken 2267229997Skenstatic void 2268229997Skenctlfe_dump(void) 2269229997Sken{ 2270229997Sken struct ctlfe_softc *bus_softc; 2271229997Sken 2272229997Sken STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) { 2273229997Sken struct ctlfe_lun_softc *lun_softc; 2274229997Sken 2275229997Sken ctlfe_dump_sim(bus_softc->sim); 2276229997Sken 2277229997Sken STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) { 2278229997Sken ctlfe_dump_queue(lun_softc); 2279229997Sken } 2280229997Sken } 2281229997Sken} 2282