Index: projects/iscsi_opt/sys/dev/iscsi/initiator/isc_cam.c =================================================================== --- projects/iscsi_opt/sys/dev/iscsi/initiator/isc_cam.c (revision 235246) +++ projects/iscsi_opt/sys/dev/iscsi/initiator/isc_cam.c (revision 235247) @@ -1,442 +1,447 @@ /*- * Copyright (c) 2005-2010 Daniel Braniss * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ /* | $Id: isc_cam.c 998 2009-12-20 10:32:45Z danny $ */ #include __FBSDID("$FreeBSD$"); #include "opt_iscsi_initiator.h" #include #include #include #if __FreeBSD_version >= 700000 #include #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void _inq(struct cam_sim *sim, union ccb *ccb) { struct ccb_pathinq *cpi = &ccb->cpi; isc_session_t *sp = cam_sim_softc(sim); debug_called(8); debug(3, "sid=%d target=%d lun=%d", sp->sid, ccb->ccb_h.target_id, ccb->ccb_h.target_lun); cpi->version_num = 1; /* XXX??? */ cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32; cpi->target_sprt = 0; cpi->hba_misc = 0; cpi->hba_eng_cnt = 0; cpi->max_target = 0; //ISCSI_MAX_TARGETS - 1; cpi->initiator_id = ISCSI_MAX_TARGETS; cpi->max_lun = sp->opt.maxluns - 1; cpi->bus_id = cam_sim_bus(sim); cpi->base_transfer_speed = 3300000; // 40000; // XXX: strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); strncpy(cpi->hba_vid, "iSCSI", HBA_IDLEN); strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); cpi->unit_number = cam_sim_unit(sim); cpi->ccb_h.status = CAM_REQ_CMP; #if defined(KNOB_VALID_ADDRESS) cpi->transport = XPORT_ISCSI; cpi->transport_version = 0; #endif } static __inline int _scsi_encap(struct cam_sim *sim, union ccb *ccb) { int ret; #if __FreeBSD_version < 700000 ret = scsi_encap(sim, ccb); #else isc_session_t *sp = cam_sim_softc(sim); mtx_unlock(&sp->cam_mtx); ret = scsi_encap(sim, ccb); mtx_lock(&sp->cam_mtx); #endif return ret; } void ic_lost_target(isc_session_t *sp, int target) { debug_called(8); sdebug(2, "lost target=%d", target); if(sp->cam_path != NULL) { mtx_lock(&sp->cam_mtx); xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL); xpt_free_path(sp->cam_path); mtx_unlock(&sp->cam_mtx); sp->cam_path = 0; // XXX } } static void scan_callback(struct cam_periph *periph, union ccb *ccb) { isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0; debug_called(8); free(ccb, M_TEMP); if(sp->flags & ISC_SCANWAIT) { sp->flags &= ~ISC_SCANWAIT; wakeup(sp); } } static int ic_scan(isc_session_t *sp) { union ccb *ccb; debug_called(8); sdebug(2, "scanning sid=%d", sp->sid); if((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) { xdebug("scan failed (can't allocate CCB)"); return ENOMEM; // XXX } sp->flags &= ~ISC_CAMDEVS; sp->flags |= ISC_SCANWAIT; CAM_LOCK(sp); if(xpt_create_path(&sp->cam_path, xpt_periph, cam_sim_path(sp->cam_sim), 0, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { xdebug("can't create cam path"); CAM_UNLOCK(sp); free(ccb, M_TEMP); return ENODEV; // XXX } xpt_setup_ccb(&ccb->ccb_h, sp->cam_path, 5/*priority (low)*/); ccb->ccb_h.func_code = XPT_SCAN_BUS; ccb->ccb_h.cbfcnp = scan_callback; ccb->crcn.flags = CAM_FLAG_NONE; ccb->ccb_h.spriv_ptr0 = sp; xpt_action(ccb); CAM_UNLOCK(sp); while(sp->flags & ISC_SCANWAIT) tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should // be configurable sdebug(2, "# of luns=%d", sp->target_nluns); if(sp->target_nluns > 0) { sp->flags |= ISC_CAMDEVS; return 0; } return ENODEV; } static void ic_action(struct cam_sim *sim, union ccb *ccb) { isc_session_t *sp = cam_sim_softc(sim); struct ccb_hdr *ccb_h = &ccb->ccb_h; debug_called(8); ccb_h->spriv_ptr0 = sp; sdebug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%d retry_count=%d timeout=%d", ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status, ccb->ccb_h.target_id, ccb->ccb_h.target_lun, ccb->ccb_h.retry_count, ccb_h->timeout); if(sp == NULL) { xdebug("sp == NULL! cannot happen"); return; } switch(ccb_h->func_code) { case XPT_PATH_INQ: { struct ccb_pathinq *cpi = &ccb->cpi; _inq(sim, ccb); cpi->version_num = 1; cpi->target_sprt = 0; cpi->hba_misc = 0; cpi->unit_number = cam_sim_unit(sim); cpi->bus_id = cam_sim_bus(sim); cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */ cpi->transport = XPORT_SPI; cpi->transport_version = 2; cpi->protocol = PROTO_SCSI; cpi->protocol_version = SCSI_REV_2; cpi->maxio = 128*PAGE_SIZE; ccb->ccb_h.status = CAM_REQ_CMP; break; } case XPT_RESET_BUS: // (can just be a stub that does nothing and completes) { struct ccb_pathinq *cpi = &ccb->cpi; debug(3, "XPT_RESET_BUS"); cpi->ccb_h.status = CAM_REQ_CMP; break; } case XPT_SCSI_IO: { struct ccb_scsiio* csio = &ccb->csio; int status, rc; debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]); if(sp == NULL) { ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS; debug(4, "xpt_done.status=%d", ccb_h->status); break; } if(ccb_h->target_lun == CAM_LUN_WILDCARD) { debug(3, "target=%d: bad lun (-1)", ccb_h->target_id); ccb_h->status = CAM_LUN_INVALID; break; } rc = _scsi_encap(sim, ccb); if (rc == 0) return; if (rc == EWOULDBLOCK) { if ((sp->cam_flags & ISC_QFROZEN) == 0) { sp->cam_flags |= ISC_QFROZEN; xpt_freeze_simq(sim, 1); } status = ccb->ccb_h.status &= ~CAM_STATUS_MASK; csio->ccb_h.status = status | CAM_REQUEUE_REQ; break; } break; } case XPT_CALC_GEOMETRY: { struct ccb_calc_geometry *ccg; ccg = &ccb->ccg; debug(4, "sid=%d target=%d lun=%d XPT_CALC_GEOMETRY vsize=%jd bsize=%d", sp->sid, ccb->ccb_h.target_id, ccb->ccb_h.target_lun, ccg->volume_size, ccg->block_size); if(ccg->block_size == 0 || (ccg->volume_size < ccg->block_size)) { // print error message ... /* XXX: what error is appropiate? */ break; } else { int lun, *off, boff; lun = ccb->ccb_h.target_lun; if(lun > ISCSI_MAX_LUNS) { // XXX: xdebug("lun %d > ISCSI_MAX_LUNS!\n", lun); lun %= ISCSI_MAX_LUNS; } off = &sp->target_lun[lun / (sizeof(int)*8)]; boff = BIT(lun % (sizeof(int)*8)); debug(4, "sp->target_nluns=%d *off=%x boff=%x", sp->target_nluns, *off, boff); if((*off & boff) == 0) { sp->target_nluns++; *off |= boff; } cam_calc_geometry(ccg, /*extended*/1); } break; } case XPT_GET_TRAN_SETTINGS: { struct ccb_trans_settings *cts = &ccb->cts; int bus, target; struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi; struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi; bus = cam_sim_bus(sim); target = cts->ccb_h.target_id; debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target); /* disconnect always OK */ cts->protocol = PROTO_SCSI; cts->protocol_version = SCSI_REV_2; cts->transport = XPORT_SPI; cts->transport_version = 2; spi->valid = CTS_SPI_VALID_DISC; spi->flags = CTS_SPI_FLAGS_DISC_ENB; scsi->valid = CTS_SCSI_VALID_TQ; scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; - cts->ccb_h.status = CAM_REQ_CMP; + /* kick a stalled queue */ + if (sp->cam_flags & ISC_QFROZEN) { + sp->space_needed = 0; + cts->ccb_h.status |= CAM_RELEASE_SIMQ; + sp->cam_flags &= ~ISC_QFROZEN; + } break; } default: ccb_h->status = CAM_REQ_INVALID; break; } if (ccb_h->status != CAM_REQ_CMP) debug(2, "command %d status %d", ccb_h->func_code, ccb_h->status); #if __FreeBSD_version < 700000 XPT_DONE(sp, ccb); #else xpt_done(ccb); #endif return; } static void ic_poll(struct cam_sim *sim) { debug_called(4); } int ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp) { debug_called(8); if(sp && sp->cam_sim) { cp->path_id = cam_sim_path(sp->cam_sim); cp->target_id = 0; cp->target_nluns = ISCSI_MAX_LUNS; // XXX: -1? return 0; } return ENXIO; } void ic_destroy(isc_session_t *sp ) { debug_called(8); if(sp->cam_path != NULL) { sdebug(2, "name=%s unit=%d", cam_sim_name(sp->cam_sim), cam_sim_unit(sp->cam_sim)); CAM_LOCK(sp); #if 0 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL); #else xpt_async(XPT_RESET_BUS, sp->cam_path, NULL); #endif xpt_free_path(sp->cam_path); xpt_bus_deregister(cam_sim_path(sp->cam_sim)); cam_sim_free(sp->cam_sim, TRUE /*free_devq*/); CAM_UNLOCK(sp); sdebug(2, "done"); } } int ic_init(isc_session_t *sp) { struct cam_sim *sim; struct cam_devq *devq; debug_called(8); if((devq = cam_simq_alloc(256)) == NULL) return ENOMEM; #if __FreeBSD_version >= 700000 mtx_init(&sp->cam_mtx, "isc-cam", NULL, MTX_DEF); #else isp->cam_mtx = Giant; #endif sim = cam_sim_alloc(ic_action, ic_poll, "iscsi", sp, sp->sid, // unit #if __FreeBSD_version >= 700000 &sp->cam_mtx, #endif 256, // max_dev_transactions 256, // max_tagged_dev_transactions devq); if(sim == NULL) { cam_simq_free(devq); #if __FreeBSD_version >= 700000 mtx_destroy(&sp->cam_mtx); #endif return ENXIO; } CAM_LOCK(sp); if(xpt_bus_register(sim, #if __FreeBSD_version >= 700000 NULL, #endif 0/*bus_number*/) != CAM_SUCCESS) { cam_sim_free(sim, /*free_devq*/TRUE); CAM_UNLOCK(sp); #if __FreeBSD_version >= 700000 mtx_destroy(&sp->cam_mtx); #endif return ENXIO; } sp->cam_sim = sim; CAM_UNLOCK(sp); sdebug(1, "cam subsystem initialized"); ic_scan(sp); return 0; } Index: projects/iscsi_opt/sys/dev/iscsi/initiator/iscsi_subr.c =================================================================== --- projects/iscsi_opt/sys/dev/iscsi/initiator/iscsi_subr.c (revision 235246) +++ projects/iscsi_opt/sys/dev/iscsi/initiator/iscsi_subr.c (revision 235247) @@ -1,615 +1,620 @@ /*- * Copyright (c) 2005-2010 Daniel Braniss * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ /* | $Id: iscsi_subr.c 743 2009-08-08 10:54:53Z danny $ */ #include __FBSDID("$FreeBSD$"); #include "opt_iscsi_initiator.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* | Interface to the SCSI layer */ void iscsi_r2t(isc_session_t *sp, pduq_t *opq, pduq_t *pq) { union ccb *ccb = opq->ccb; struct ccb_scsiio *csio = &ccb->csio; pdu_t *opp = &opq->pdu; bhs_t *bhp = &opp->ipdu.bhs; r2t_t *r2t = &pq->pdu.ipdu.r2t; pduq_t *wpq; int error; debug_called(8); sdebug(4, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt), ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W); switch(bhp->opcode) { case ISCSI_SCSI_CMD: if(opp->ipdu.scsi_req.W) { data_out_t *cmd; u_int ddtl = ntohl(r2t->ddtl); u_int edtl = ntohl(opp->ipdu.scsi_req.edtlen); u_int bleft, bs, dsn, bo; caddr_t bp = csio->data_ptr; bo = ntohl(r2t->bo); bp += MIN(bo, edtl - ddtl); bleft = ddtl; if(sp->opt.maxXmitDataSegmentLength > 0) // danny's RFC bs = MIN(sp->opt.maxXmitDataSegmentLength, ddtl); else bs = ddtl; dsn = 0; sdebug(4, "edtl=%x ddtl=%x bo=%x dsn=%x bs=%x maxX=%x", edtl, ddtl, bo, dsn, bs, sp->opt.maxXmitDataSegmentLength); while(bleft > 0) { wpq = pdu_alloc(sp->isc, M_NOWAIT); // testing ... if(wpq == NULL) { sdebug(3, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt), ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W); sdebug(1, "npdu_max=%d npdu_alloc=%d", sp->isc->npdu_max, sp->isc->npdu_alloc); while((wpq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) { sdebug(2, "waiting..."); isc_r2t_sleep++; #if __FreeBSD_version >= 700000 pause("isc_r2t", hz >> 1); #else tsleep(sp->isc, 0, "isc_r2t", 5*hz); #endif } } cmd = &wpq->pdu.ipdu.data_out; cmd->opcode = ISCSI_WRITE_DATA; cmd->lun[0] = r2t->lun[0]; cmd->lun[1] = r2t->lun[1]; cmd->ttt = r2t->ttt; cmd->itt = r2t->itt; cmd->dsn = htonl(dsn); cmd->bo = htonl(bo); cmd->F = (bs < bleft)? 0: 1; // is this the last one? bs = MIN(bs, bleft); wpq->pdu.ds_len = bs; wpq->pdu.ds_addr = bp; error = isc_qout(sp, wpq); sdebug(6, "bs=%x bo=%x bp=%p dsn=%x error=%d", bs, bo, bp, dsn, error); if(error) break; bo += bs; bp += bs; bleft -= bs; dsn++; } } break; default: // XXX: should not happen ... xdebug("huh? opcode=0x%x", bhp->opcode); } } static int getSenseData(u_int status, union ccb *ccb, pduq_t *pq) { pdu_t *pp = &pq->pdu; struct ccb_scsiio *scsi = (struct ccb_scsiio *)ccb; struct scsi_sense_data *sense = &scsi->sense_data; struct mbuf *m = pq->mp; scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp; caddr_t bp; int sense_len, mustfree = 0; int error_code, sense_key, asc, ascq; bp = mtod(pq->mp, caddr_t); if((sense_len = scsi_2btoul(bp)) == 0) return 0; debug(4, "sense_len=%d", sense_len); /* | according to the specs, the sense data cannot | be larger than 252 ... */ if(sense_len > m->m_len) { bp = malloc(sense_len, M_ISCSI, M_WAITOK); debug(3, "calling i_mbufcopy(len=%d)", sense_len); i_mbufcopy(pq->mp, bp, sense_len); mustfree++; } scsi->scsi_status = status; bcopy(bp+2, sense, min(sense_len, scsi->sense_len)); scsi->sense_resid = 0; if(cmd->flag & (BIT(1)|BIT(2))) scsi->sense_resid = ntohl(pp->ipdu.scsi_rsp.rcnt); scsi_extract_sense_len(sense, scsi->sense_len - scsi->sense_resid, &error_code, &sense_key, &asc, &ascq, /*show_errors*/ 1); debug(3, "sense_len=%d rcnt=%d sense_resid=%d dsl=%d error_code=%x flags=%x", sense_len, ntohl(pp->ipdu.scsi_rsp.rcnt), scsi->sense_resid, pp->ds_len, error_code, sense_key); if(mustfree) free(bp, M_ISCSI); return 1; } /* | Some information is from SAM draft. */ static void _scsi_done(isc_session_t *sp, u_int response, u_int status, union ccb *ccb, pduq_t *pq) { struct ccb_hdr *ccb_h = &ccb->ccb_h; debug_called(8); if(status || response) { sdebug(3, "response=%x status=%x ccb=%p pq=%p", response, status, ccb, pq); if(pq != NULL) sdebug(3, "mp=%p buf=%p len=%d", pq->mp, pq->buf, pq->len); } ccb_h->status = 0; switch(response) { case 0: // Command Completed at Target switch(status) { case 0: // Good, all is ok ccb_h->status = CAM_REQ_CMP; break; case 0x02: // Check Condition if((pq != NULL) && (pq->mp != NULL) && getSenseData(status, ccb, pq)) ccb_h->status |= CAM_AUTOSNS_VALID; case 0x14: // Intermediate-Condition Met case 0x10: // Intermediate case 0x04: // Condition Met ccb_h->status |= CAM_SCSI_STATUS_ERROR; break; case 0x08: ccb_h->status = CAM_BUSY; break; case 0x18: // Reservation Conflict case 0x28: // Task Set Full ccb_h->status = CAM_REQUEUE_REQ; break; default: //case 0x22: // Command Terminated //case 0x30: // ACA Active //case 0x40: // Task Aborted ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED; } break; default: if((response >= 0x80) && (response <= 0xFF)) { // Vendor specific ... } case 1: // target failure ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED; break; } sdebug(5, "ccb_h->status=%x", ccb_h->status); XPT_DONE(sp, ccb); } /* | returns the lowest cmdseq that was not acked */ int iscsi_requeue(isc_session_t *sp) { pduq_t *pq; u_int i, n, last; debug_called(8); i = last = 0; sp->flags |= ISC_HOLD; while((pq = i_dqueue_hld(sp)) != NULL) { i++; if(pq->ccb != NULL) { + if (sp->cam_flags & ISC_QFROZEN) { + sp->space_needed = 0; + pq->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; + sp->cam_flags &= ~ISC_QFROZEN; + } _scsi_done(sp, 0, 0x28, pq->ccb, NULL); n = ntohl(pq->pdu.ipdu.bhs.CmdSN); if(last==0 || (last > n)) last = n; sdebug(2, "last=%x n=%x", last, n); } pdu_free(sp->isc, pq); } sp->flags &= ~ISC_HOLD; return i? last: sp->sn.cmd; } int i_pdu_flush(isc_session_t *sp) { int n = 0; pduq_t *pq; debug_called(8); while((pq = i_dqueue_rsp(sp)) != NULL) { pdu_free(sp->isc, pq); n++; } while((pq = i_dqueue_rsv(sp)) != NULL) { pdu_free(sp->isc, pq); n++; } while((pq = i_dqueue_snd(sp, -1)) != NULL) { pdu_free(sp->isc, pq); n++; } while((pq = i_dqueue_hld(sp)) != NULL) { pdu_free(sp->isc, pq); n++; } while((pq = i_dqueue_wsnd(sp)) != NULL) { pdu_free(sp->isc, pq); n++; } if(n != 0) xdebug("%d pdus recovered, should have been ZERO!", n); return n; } /* | called from ism_destroy. */ void iscsi_cleanup(isc_session_t *sp) { pduq_t *pq, *pqtmp; debug_called(8); TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, pqtmp) { sdebug(3, "hld pq=%p", pq); if(pq->ccb) _scsi_done(sp, 1, 0x40, pq->ccb, NULL); TAILQ_REMOVE(&sp->hld, pq, pq_link); if(pq->buf) { free(pq->buf, M_ISCSIBUF); pq->buf = NULL; } pdu_free(sp->isc, pq); } while((pq = i_dqueue_snd(sp, BIT(0)|BIT(1)|BIT(2))) != NULL) { sdebug(3, "pq=%p", pq); if(pq->ccb) _scsi_done(sp, 1, 0x40, pq->ccb, NULL); if(pq->buf) { free(pq->buf, M_ISCSIBUF); pq->buf = NULL; } pdu_free(sp->isc, pq); } wakeup(&sp->rsp); } void iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq) { pdu_t *pp = &pq->pdu; scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp; debug_called(8); if (opq->ccb != NULL) _scsi_done(sp, cmd->response, cmd->status, opq->ccb, pq); pdu_free(sp->isc, opq); } // see RFC 3720, 10.9.1 page 146 /* | NOTE: | the call to isc_stop_receiver is a kludge, | instead, it should be handled by the userland controller, | but that means that there should be a better way, other than | sending a signal. Somehow, this packet should be supplied to | the userland via read. */ void iscsi_async(isc_session_t *sp, pduq_t *pq) { pdu_t *pp = &pq->pdu; async_t *cmd = &pp->ipdu.async; debug_called(8); sdebug(3, "asyncevent=0x%x asyncVCode=0x%0x", cmd->asyncEvent, cmd->asyncVCode); switch(cmd->asyncEvent) { case 0: // check status ... break; case 1: // target request logout isc_stop_receiver(sp); // XXX: temporary solution break; case 2: // target indicates it wants to drop connection isc_stop_receiver(sp); // XXX: temporary solution break; case 3: // target indicates it will drop all connections. isc_stop_receiver(sp); // XXX: temporary solution break; case 4: // target request parameter negotiation break; default: break; } } void iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq) { union ccb *ccb = opq->ccb; //reject_t *reject = &pq->pdu.ipdu.reject; debug_called(3); //XXX: check RFC 10.17.1 (page 176) ccb->ccb_h.status = CAM_REQ_ABORTED; XPT_DONE(sp, ccb); pdu_free(sp->isc, opq); } /* | deal with lun */ static int dwl(isc_session_t *sp, int lun, u_char *lp) { debug_called(8); sdebug(4, "lun=%d", lun); /* | mapping LUN to iSCSI LUN | check the SAM-2 specs | hint: maxLUNS is a small number, cam's LUN is 32bits | iSCSI is 64bits, scsi is ? */ // XXX: check if this will pass the endian test if(lun < 256) { lp[0] = 0; lp[1] = lun; } else if(lun < 16384) { lp[0] = (1 << 5) | ((lun >> 8) & 0x3f); lp[1] = lun & 0xff; } else { xdebug("lun %d: is unsupported!", lun); return -1; } return 0; } /* | encapsulate the scsi command and */ int scsi_encap(struct cam_sim *sim, union ccb *ccb) { isc_session_t *sp = cam_sim_softc(sim); struct ccb_scsiio *csio = &ccb->csio; struct ccb_hdr *ccb_h = &ccb->ccb_h; pduq_t *pq; scsi_req_t *cmd; struct socket *so = sp->soc; int error = EINVAL; debug_called(8); debug(4, "ccb->sp=%p", ccb_h->spriv_ptr0); sp = ccb_h->spriv_ptr0; if (isc_sowouldblock(sp, ccb)) { return (EWOULDBLOCK); } if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) { debug(2, "ccb->sp=%p", ccb_h->spriv_ptr0); sdebug(1, "pdu_alloc failed sc->npdu_max=%d npdu_alloc=%d", sp->isc->npdu_max, sp->isc->npdu_alloc); while((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) { sdebug(2, "waiting..."); #if __FreeBSD_version >= 700000 isc_encap_sleep++; pause("isc_encap", 5*hz); #else tsleep(sp->isc, 0, "isc_encap", 5*hz); #endif } } cmd = &pq->pdu.ipdu.scsi_req; cmd->opcode = ISCSI_SCSI_CMD; cmd->F = 1; if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) { switch(csio->tag_action) { case MSG_SIMPLE_Q_TAG: cmd->attr = iSCSI_TASK_SIMPLE; break; case MSG_HEAD_OF_Q_TAG: cmd->attr = iSCSI_TASK_HOFQ; break; case MSG_ORDERED_Q_TAG: cmd->attr = iSCSI_TASK_ORDER; break; case MSG_ACA_TASK: cmd->attr = iSCSI_TASK_ACA; break; } } else cmd->attr = iSCSI_TASK_SIMPLE; dwl(sp, ccb_h->target_lun, (u_char *)&cmd->lun); if((ccb_h->flags & CAM_CDB_POINTER) != 0) { if((ccb_h->flags & CAM_CDB_PHYS) == 0) { if(csio->cdb_len > 16) { sdebug(3, "oversize cdb %d > 16", csio->cdb_len); goto invalid; } } else { sdebug(3, "not phys"); goto invalid; } } if(csio->cdb_len > sizeof(cmd->cdb)) xdebug("guevalt! %d > %ld", csio->cdb_len, (long)sizeof(cmd->cdb)); memcpy(cmd->cdb, ccb_h->flags & CAM_CDB_POINTER? csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes, csio->cdb_len); cmd->W = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT; cmd->R = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN; cmd->edtlen = htonl(csio->dxfer_len); pq->ccb = ccb; /* | place it in the out queue */ return (isc_qout(sp, pq)); invalid: ccb->ccb_h.status = CAM_REQ_INVALID; pdu_free(sp->isc, pq); return (error); } int scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq) { union ccb *ccb = opq->ccb; struct ccb_scsiio *csio = &ccb->csio; pdu_t *opp = &opq->pdu; bhs_t *bhp = &opp->ipdu.bhs; debug_called(8); sdebug(6, "pq=%p opq=%p bhp->opcode=0x%x len=%d", pq, opq, bhp->opcode, pq->pdu.ds_len); if(ccb == NULL) { sdebug(1, "itt=0x%x pq=%p opq=%p bhp->opcode=0x%x len=%d", ntohl(pq->pdu.ipdu.bhs.itt), pq, opq, bhp->opcode, pq->pdu.ds_len); xdebug("%d] ccb == NULL!", sp->sid); return 0; } if(pq->pdu.ds_len != 0) { switch(bhp->opcode) { case ISCSI_SCSI_CMD: { scsi_req_t *cmd = &opp->ipdu.scsi_req; sdebug(5, "itt=0x%x opcode=%x R=%d", ntohl(pq->pdu.ipdu.bhs.itt), pq->pdu.ipdu.bhs.opcode, cmd->R); switch(pq->pdu.ipdu.bhs.opcode) { case ISCSI_READ_DATA: // SCSI Data in { caddr_t bp = NULL; // = mtod(pq->mp, caddr_t); data_in_t *rcmd = &pq->pdu.ipdu.data_in; if(cmd->R) { sdebug(5, "copy to=%p from=%p l1=%d l2=%d mp@%p", csio->data_ptr, bp? mtod(pq->mp, caddr_t): 0, ntohl(cmd->edtlen), pq->pdu.ds_len, pq->mp); if(ntohl(cmd->edtlen) >= pq->pdu.ds_len) { int offset, len = pq->pdu.ds_len; if(pq->mp != NULL) { caddr_t dp; offset = ntohl(rcmd->bo); dp = csio->data_ptr + offset; i_mbufcopy(pq->mp, dp, len); } } else { xdebug("edtlen=%d < ds_len=%d", ntohl(cmd->edtlen), pq->pdu.ds_len); } } if(rcmd->S) { /* | contains also the SCSI Status */ _scsi_done(sp, 0, rcmd->status, opq->ccb, NULL); return 0; } else return 1; } break; } } default: sdebug(3, "opcode=%02x", bhp->opcode); break; } } /* | XXX: error ... */ return 1; }