freebsd-dev/sys/dev/iscsi_initiator/iscsi_subr.c

609 lines
16 KiB
C
Raw Normal View History

/*-
* Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
* 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_iscsi_initiator.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/uio.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt_sim.h>
#include <cam/cam_periph.h>
#include <cam/scsi/scsi_message.h>
#include <sys/eventhandler.h>
#include <dev/iscsi_initiator/iscsi.h>
#include <dev/iscsi_initiator/iscsivar.h>
/*
| 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...");
#if __FreeBSD_version >= 700000
pause("isc_r2t", 5*hz);
#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);
Add descriptor sense support to CAM, and honor sense residuals properly in CAM. Desriptor sense is a new sense data format that originated in SPC-3. Among other things, it allows for an 8-byte info field, which is necessary to pass back block numbers larger than 4 bytes. This change adds a number of new functions to scsi_all.c (and therefore libcam) that abstract out most access to sense data. This includes a bump of CAM_VERSION, because the CCB ABI has changed. Userland programs that use the CAM pass(4) driver will need to be recompiled. camcontrol.c: Change uses of scsi_extract_sense() to use scsi_extract_sense_len(). Use scsi_get_sks() instead of accessing sense key specific data directly. scsi_modes: Update the control mode page to the latest version (SPC-4). scsi_cmds.c, scsi_target.c: Change references to struct scsi_sense_data to struct scsi_sense_data_fixed. This should be changed to allow the user to specify fixed or descriptor sense, and then use scsi_set_sense_data() to build the sense data. ps3cdrom.c: Use scsi_set_sense_data() instead of setting sense data manually. cam_periph.c: Use scsi_extract_sense_len() instead of using scsi_extract_sense() or accessing sense data directly. cam_ccb.h: Bump the CAM_VERSION from 0x15 to 0x16. The change of struct scsi_sense_data from 32 to 252 bytes changes the size of struct ccb_scsiio, but not the size of union ccb. So the version must be bumped to prevent structure mis-matches. scsi_all.h: Lots of updated SCSI sense data and other structures. Add function prototypes for the new sense data functions. Take out the inline implementation of scsi_extract_sense(). It is now too large to put in a header file. Add macros to calculate whether fields are present and filled in fixed and descriptor sense data scsi_all.c: In scsi_op_desc(), allow the user to pass in NULL inquiry data, and we'll assume a direct access device in that case. Changed the SCSI RESERVED sense key name and description to COMPLETED, as it is now defined in the spec. Change the error recovery action for a number of read errors to prevent lots of retries when the drive has said that the block isn't accessible. This speeds up reconstruction of the block by any RAID software running on top of the drive (e.g. ZFS). In scsi_sense_desc(), allow for invalid sense key numbers. This allows calling this routine without checking the input values first. Change scsi_error_action() to use scsi_extract_sense_len(), and handle things when invalid asc/ascq values are encountered. Add a new routine, scsi_desc_iterate(), that will call the supplied function for every descriptor in descriptor format sense data. Add scsi_set_sense_data(), and scsi_set_sense_data_va(), which build descriptor and fixed format sense data. They currently default to fixed format sense data. Add a number of scsi_get_*() functions, which get different types of sense data fields from either fixed or descriptor format sense data, if the data is present. Add a number of scsi_*_sbuf() functions, which print formatted versions of various sense data fields. These functions work for either fixed or descriptor sense. Add a number of scsi_sense_*_sbuf() functions, which have a standard calling interface and print the indicated field. These functions take descriptors only. Add scsi_sense_desc_sbuf(), which will print a formatted version of the given sense descriptor. Pull out a majority of the scsi_sense_sbuf() function and put it into scsi_sense_only_sbuf(). This allows callers that don't use struct ccb_scsiio to easily utilize the printing routines. Revamp that function to handle descriptor sense and use the new sense fetching and printing routines. Move scsi_extract_sense() into scsi_all.c, and implement it in terms of the new function, scsi_extract_sense_len(). The _len() version takes a length (which should be the sense length - residual) and can indicate which fields are present and valid in the sense data. Add a couple of new scsi_get_*() routines to get the sense key, asc, and ascq only. mly.c: Rename struct scsi_sense_data to struct scsi_sense_data_fixed. sbp_targ.c: Use the new sense fetching routines to get sense data instead of accessing it directly. sbp.c: Change the firewire/SCSI sense data transformation code to use struct scsi_sense_data_fixed instead of struct scsi_sense_data. This should be changed later to use scsi_set_sense_data(). ciss.c: Calculate the sense residual properly. Use scsi_get_sense_key() to fetch the sense key. mps_sas.c, mpt_cam.c: Set the sense residual properly. iir.c: Use scsi_set_sense_data() instead of building sense data by hand. iscsi_subr.c: Use scsi_extract_sense_len() instead of grabbing sense data directly. umass.c: Use scsi_set_sense_data() to build sense data. Grab the sense key using scsi_get_sense_key(). Calculate the sense residual properly. isp_freebsd.h: Use scsi_get_*() routines to grab asc, ascq, and sense key values. Calculate and set the sense residual. MFC after: 3 days Sponsored by: Spectra Logic Corporation
2011-10-03 20:32:55 +00:00
scsi_extract_sense_len(sense, scsi->sense_len - scsi->sense_resid,
&error_code, &sense_key, &asc, &ascq, /*show_errors*/ 1);
Add descriptor sense support to CAM, and honor sense residuals properly in CAM. Desriptor sense is a new sense data format that originated in SPC-3. Among other things, it allows for an 8-byte info field, which is necessary to pass back block numbers larger than 4 bytes. This change adds a number of new functions to scsi_all.c (and therefore libcam) that abstract out most access to sense data. This includes a bump of CAM_VERSION, because the CCB ABI has changed. Userland programs that use the CAM pass(4) driver will need to be recompiled. camcontrol.c: Change uses of scsi_extract_sense() to use scsi_extract_sense_len(). Use scsi_get_sks() instead of accessing sense key specific data directly. scsi_modes: Update the control mode page to the latest version (SPC-4). scsi_cmds.c, scsi_target.c: Change references to struct scsi_sense_data to struct scsi_sense_data_fixed. This should be changed to allow the user to specify fixed or descriptor sense, and then use scsi_set_sense_data() to build the sense data. ps3cdrom.c: Use scsi_set_sense_data() instead of setting sense data manually. cam_periph.c: Use scsi_extract_sense_len() instead of using scsi_extract_sense() or accessing sense data directly. cam_ccb.h: Bump the CAM_VERSION from 0x15 to 0x16. The change of struct scsi_sense_data from 32 to 252 bytes changes the size of struct ccb_scsiio, but not the size of union ccb. So the version must be bumped to prevent structure mis-matches. scsi_all.h: Lots of updated SCSI sense data and other structures. Add function prototypes for the new sense data functions. Take out the inline implementation of scsi_extract_sense(). It is now too large to put in a header file. Add macros to calculate whether fields are present and filled in fixed and descriptor sense data scsi_all.c: In scsi_op_desc(), allow the user to pass in NULL inquiry data, and we'll assume a direct access device in that case. Changed the SCSI RESERVED sense key name and description to COMPLETED, as it is now defined in the spec. Change the error recovery action for a number of read errors to prevent lots of retries when the drive has said that the block isn't accessible. This speeds up reconstruction of the block by any RAID software running on top of the drive (e.g. ZFS). In scsi_sense_desc(), allow for invalid sense key numbers. This allows calling this routine without checking the input values first. Change scsi_error_action() to use scsi_extract_sense_len(), and handle things when invalid asc/ascq values are encountered. Add a new routine, scsi_desc_iterate(), that will call the supplied function for every descriptor in descriptor format sense data. Add scsi_set_sense_data(), and scsi_set_sense_data_va(), which build descriptor and fixed format sense data. They currently default to fixed format sense data. Add a number of scsi_get_*() functions, which get different types of sense data fields from either fixed or descriptor format sense data, if the data is present. Add a number of scsi_*_sbuf() functions, which print formatted versions of various sense data fields. These functions work for either fixed or descriptor sense. Add a number of scsi_sense_*_sbuf() functions, which have a standard calling interface and print the indicated field. These functions take descriptors only. Add scsi_sense_desc_sbuf(), which will print a formatted version of the given sense descriptor. Pull out a majority of the scsi_sense_sbuf() function and put it into scsi_sense_only_sbuf(). This allows callers that don't use struct ccb_scsiio to easily utilize the printing routines. Revamp that function to handle descriptor sense and use the new sense fetching and printing routines. Move scsi_extract_sense() into scsi_all.c, and implement it in terms of the new function, scsi_extract_sense_len(). The _len() version takes a length (which should be the sense length - residual) and can indicate which fields are present and valid in the sense data. Add a couple of new scsi_get_*() routines to get the sense key, asc, and ascq only. mly.c: Rename struct scsi_sense_data to struct scsi_sense_data_fixed. sbp_targ.c: Use the new sense fetching routines to get sense data instead of accessing it directly. sbp.c: Change the firewire/SCSI sense data transformation code to use struct scsi_sense_data_fixed instead of struct scsi_sense_data. This should be changed later to use scsi_set_sense_data(). ciss.c: Calculate the sense residual properly. Use scsi_get_sense_key() to fetch the sense key. mps_sas.c, mpt_cam.c: Set the sense residual properly. iir.c: Use scsi_set_sense_data() instead of building sense data by hand. iscsi_subr.c: Use scsi_extract_sense_len() instead of grabbing sense data directly. umass.c: Use scsi_set_sense_data() to build sense data. Grab the sense key using scsi_get_sense_key(). Calculate the sense residual properly. isp_freebsd.h: Use scsi_get_*() routines to grab asc, ascq, and sense key values. Calculate and set the sense residual. MFC after: 3 days Sponsored by: Spectra Logic Corporation
2011-10-03 20:32:55 +00:00
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,
Add descriptor sense support to CAM, and honor sense residuals properly in CAM. Desriptor sense is a new sense data format that originated in SPC-3. Among other things, it allows for an 8-byte info field, which is necessary to pass back block numbers larger than 4 bytes. This change adds a number of new functions to scsi_all.c (and therefore libcam) that abstract out most access to sense data. This includes a bump of CAM_VERSION, because the CCB ABI has changed. Userland programs that use the CAM pass(4) driver will need to be recompiled. camcontrol.c: Change uses of scsi_extract_sense() to use scsi_extract_sense_len(). Use scsi_get_sks() instead of accessing sense key specific data directly. scsi_modes: Update the control mode page to the latest version (SPC-4). scsi_cmds.c, scsi_target.c: Change references to struct scsi_sense_data to struct scsi_sense_data_fixed. This should be changed to allow the user to specify fixed or descriptor sense, and then use scsi_set_sense_data() to build the sense data. ps3cdrom.c: Use scsi_set_sense_data() instead of setting sense data manually. cam_periph.c: Use scsi_extract_sense_len() instead of using scsi_extract_sense() or accessing sense data directly. cam_ccb.h: Bump the CAM_VERSION from 0x15 to 0x16. The change of struct scsi_sense_data from 32 to 252 bytes changes the size of struct ccb_scsiio, but not the size of union ccb. So the version must be bumped to prevent structure mis-matches. scsi_all.h: Lots of updated SCSI sense data and other structures. Add function prototypes for the new sense data functions. Take out the inline implementation of scsi_extract_sense(). It is now too large to put in a header file. Add macros to calculate whether fields are present and filled in fixed and descriptor sense data scsi_all.c: In scsi_op_desc(), allow the user to pass in NULL inquiry data, and we'll assume a direct access device in that case. Changed the SCSI RESERVED sense key name and description to COMPLETED, as it is now defined in the spec. Change the error recovery action for a number of read errors to prevent lots of retries when the drive has said that the block isn't accessible. This speeds up reconstruction of the block by any RAID software running on top of the drive (e.g. ZFS). In scsi_sense_desc(), allow for invalid sense key numbers. This allows calling this routine without checking the input values first. Change scsi_error_action() to use scsi_extract_sense_len(), and handle things when invalid asc/ascq values are encountered. Add a new routine, scsi_desc_iterate(), that will call the supplied function for every descriptor in descriptor format sense data. Add scsi_set_sense_data(), and scsi_set_sense_data_va(), which build descriptor and fixed format sense data. They currently default to fixed format sense data. Add a number of scsi_get_*() functions, which get different types of sense data fields from either fixed or descriptor format sense data, if the data is present. Add a number of scsi_*_sbuf() functions, which print formatted versions of various sense data fields. These functions work for either fixed or descriptor sense. Add a number of scsi_sense_*_sbuf() functions, which have a standard calling interface and print the indicated field. These functions take descriptors only. Add scsi_sense_desc_sbuf(), which will print a formatted version of the given sense descriptor. Pull out a majority of the scsi_sense_sbuf() function and put it into scsi_sense_only_sbuf(). This allows callers that don't use struct ccb_scsiio to easily utilize the printing routines. Revamp that function to handle descriptor sense and use the new sense fetching and printing routines. Move scsi_extract_sense() into scsi_all.c, and implement it in terms of the new function, scsi_extract_sense_len(). The _len() version takes a length (which should be the sense length - residual) and can indicate which fields are present and valid in the sense data. Add a couple of new scsi_get_*() routines to get the sense key, asc, and ascq only. mly.c: Rename struct scsi_sense_data to struct scsi_sense_data_fixed. sbp_targ.c: Use the new sense fetching routines to get sense data instead of accessing it directly. sbp.c: Change the firewire/SCSI sense data transformation code to use struct scsi_sense_data_fixed instead of struct scsi_sense_data. This should be changed later to use scsi_set_sense_data(). ciss.c: Calculate the sense residual properly. Use scsi_get_sense_key() to fetch the sense key. mps_sas.c, mpt_cam.c: Set the sense residual properly. iir.c: Use scsi_set_sense_data() instead of building sense data by hand. iscsi_subr.c: Use scsi_extract_sense_len() instead of grabbing sense data directly. umass.c: Use scsi_set_sense_data() to build sense data. Grab the sense key using scsi_get_sense_key(). Calculate the sense residual properly. isp_freebsd.h: Use scsi_get_*() routines to grab asc, ascq, and sense key values. Calculate and set the sense residual. MFC after: 3 days Sponsored by: Spectra Logic Corporation
2011-10-03 20:32:55 +00:00
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) {
_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);
_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(8);
//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;
debug_called(8);
debug(4, "ccb->sp=%p", ccb_h->spriv_ptr0);
sp = ccb_h->spriv_ptr0;
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
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 0
// this breaks at least Isilon's iscsi target.
/*
| map tag option, default is UNTAGGED
*/
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;
#endif
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
*/
if(isc_qout(sp, pq) == 0)
return 1;
invalid:
ccb->ccb_h.status = CAM_REQ_INVALID;
pdu_free(sp->isc, pq);
return 0;
}
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;
}