2 * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 | $Id: iscsi_subr.c 743 2009-08-08 10:54:53Z danny $
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_iscsi_initiator.h"
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/callout.h>
39 #include <sys/malloc.h>
41 #include <sys/kthread.h>
43 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
49 #include <cam/cam_ccb.h>
50 #include <cam/cam_sim.h>
51 #include <cam/cam_xpt_sim.h>
52 #include <cam/cam_periph.h>
53 #include <cam/scsi/scsi_message.h>
54 #include <sys/eventhandler.h>
56 #include <dev/iscsi/initiator/iscsi.h>
57 #include <dev/iscsi/initiator/iscsivar.h>
60 | Interface to the SCSI layer
63 iscsi_r2t(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
65 union ccb *ccb = opq->ccb;
66 struct ccb_scsiio *csio = &ccb->csio;
67 pdu_t *opp = &opq->pdu;
68 bhs_t *bhp = &opp->ipdu.bhs;
69 r2t_t *r2t = &pq->pdu.ipdu.r2t;
74 sdebug(4, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt),
75 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W);
79 if(opp->ipdu.scsi_req.W) {
81 u_int ddtl = ntohl(r2t->ddtl);
82 u_int edtl = ntohl(opp->ipdu.scsi_req.edtlen);
83 u_int bleft, bs, dsn, bo;
84 caddr_t bp = csio->data_ptr;
89 if(sp->opt.maxXmitDataSegmentLength > 0) // danny's RFC
90 bs = MIN(sp->opt.maxXmitDataSegmentLength, ddtl);
94 sdebug(4, "edtl=%x ddtl=%x bo=%x dsn=%x bs=%x maxX=%x",
95 edtl, ddtl, bo, dsn, bs, sp->opt.maxXmitDataSegmentLength);
97 wpq = pdu_alloc(sp->isc, M_NOWAIT); // testing ...
99 sdebug(3, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt),
100 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W);
101 sdebug(1, "npdu_max=%d npdu_alloc=%d", sp->isc->npdu_max, sp->isc->npdu_alloc);
103 while((wpq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
104 sdebug(2, "waiting...");
105 #if __FreeBSD_version >= 700000
106 pause("isc_r2t", 5*hz);
108 tsleep(sp->isc, 0, "isc_r2t", 5*hz);
112 cmd = &wpq->pdu.ipdu.data_out;
113 cmd->opcode = ISCSI_WRITE_DATA;
114 cmd->lun[0] = r2t->lun[0];
115 cmd->lun[1] = r2t->lun[1];
119 cmd->dsn = htonl(dsn);
122 cmd->F = (bs < bleft)? 0: 1; // is this the last one?
125 wpq->pdu.ds_len = bs;
126 wpq->pdu.ds_addr = bp;
128 error = isc_qout(sp, wpq);
129 sdebug(6, "bs=%x bo=%x bp=%p dsn=%x error=%d", bs, bo, bp, dsn, error);
141 // XXX: should not happen ...
142 xdebug("huh? opcode=0x%x", bhp->opcode);
147 getSenseData(u_int status, union ccb *ccb, pduq_t *pq)
149 pdu_t *pp = &pq->pdu;
150 struct ccb_scsiio *scsi = (struct ccb_scsiio *)ccb;
151 struct scsi_sense_data *sense = &scsi->sense_data;
152 struct mbuf *m = pq->mp;
153 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp;
155 int sense_len, mustfree = 0;
157 bp = mtod(pq->mp, caddr_t);
158 if((sense_len = scsi_2btoul(bp)) == 0)
160 debug(4, "sense_len=%d", sense_len);
162 | according to the specs, the sense data cannot
163 | be larger than 252 ...
165 if(sense_len > m->m_len) {
166 bp = malloc(sense_len, M_ISCSI, M_WAITOK);
167 debug(3, "calling i_mbufcopy(len=%d)", sense_len);
168 i_mbufcopy(pq->mp, bp, sense_len);
171 scsi->scsi_status = status;
173 bcopy(bp+2, sense, min(sense_len, scsi->sense_len));
174 scsi->sense_resid = 0;
175 if(cmd->flag & (BIT(1)|BIT(2)))
176 scsi->sense_resid = ntohl(pp->ipdu.scsi_rsp.rcnt);
177 debug(3, "sense_len=%d rcnt=%d sense_resid=%d dsl=%d error_code=%x flags=%x",
179 ntohl(pp->ipdu.scsi_rsp.rcnt), scsi->sense_resid,
180 pp->ds_len, sense->error_code, sense->flags);
189 | Some information is from SAM draft.
192 _scsi_done(isc_session_t *sp, u_int response, u_int status, union ccb *ccb, pduq_t *pq)
194 struct ccb_hdr *ccb_h = &ccb->ccb_h;
198 if(status || response) {
199 sdebug(3, "response=%x status=%x ccb=%p pq=%p", response, status, ccb, pq);
201 sdebug(3, "mp=%p buf=%p len=%d", pq->mp, pq->buf, pq->len);
205 case 0: // Command Completed at Target
207 case 0: // Good, all is ok
208 ccb_h->status = CAM_REQ_CMP;
211 case 0x02: // Check Condition
212 if((pq != NULL) && (pq->mp != NULL) && getSenseData(status, ccb, pq))
213 ccb_h->status |= CAM_AUTOSNS_VALID;
215 case 0x14: // Intermediate-Condition Met
216 case 0x10: // Intermediate
217 case 0x04: // Condition Met
218 ccb_h->status |= CAM_SCSI_STATUS_ERROR;
222 ccb_h->status = CAM_BUSY;
225 case 0x18: // Reservation Conflict
226 case 0x28: // Task Set Full
227 ccb_h->status = CAM_REQUEUE_REQ;
230 //case 0x22: // Command Terminated
231 //case 0x30: // ACA Active
232 //case 0x40: // Task Aborted
233 ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED;
238 if((response >= 0x80) && (response <= 0xFF)) {
239 // Vendor specific ...
241 case 1: // target failure
242 ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED;
245 sdebug(5, "ccb_h->status=%x", ccb_h->status);
251 | returns the lowest cmdseq that was not acked
254 iscsi_requeue(isc_session_t *sp)
261 sp->flags |= ISC_HOLD;
262 while((pq = i_dqueue_hld(sp)) != NULL) {
264 if(pq->ccb != NULL) {
265 _scsi_done(sp, 0, 0x28, pq->ccb, NULL);
266 n = ntohl(pq->pdu.ipdu.bhs.CmdSN);
267 if(last==0 || (last > n))
269 sdebug(2, "last=%x n=%x", last, n);
271 pdu_free(sp->isc, pq);
273 sp->flags &= ~ISC_HOLD;
274 return i? last: sp->sn.cmd;
278 i_pdu_flush(isc_session_t *sp)
284 while((pq = i_dqueue_rsp(sp)) != NULL) {
285 pdu_free(sp->isc, pq);
288 while((pq = i_dqueue_rsv(sp)) != NULL) {
289 pdu_free(sp->isc, pq);
292 while((pq = i_dqueue_snd(sp, -1)) != NULL) {
293 pdu_free(sp->isc, pq);
296 while((pq = i_dqueue_hld(sp)) != NULL) {
297 pdu_free(sp->isc, pq);
300 while((pq = i_dqueue_wsnd(sp)) != NULL) {
301 pdu_free(sp->isc, pq);
305 xdebug("%d pdus recovered, should have been ZERO!", n);
309 | called from ism_destroy.
312 iscsi_cleanup(isc_session_t *sp)
318 TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, pqtmp) {
319 sdebug(3, "hld pq=%p", pq);
321 _scsi_done(sp, 1, 0x40, pq->ccb, NULL);
322 TAILQ_REMOVE(&sp->hld, pq, pq_link);
324 free(pq->buf, M_ISCSIBUF);
327 pdu_free(sp->isc, pq);
329 while((pq = i_dqueue_snd(sp, BIT(0)|BIT(1)|BIT(2))) != NULL) {
330 sdebug(3, "pq=%p", pq);
332 _scsi_done(sp, 1, 0x40, pq->ccb, NULL);
334 free(pq->buf, M_ISCSIBUF);
337 pdu_free(sp->isc, pq);
344 iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
346 pdu_t *pp = &pq->pdu;
347 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp;
351 _scsi_done(sp, cmd->response, cmd->status, opq->ccb, pq);
353 pdu_free(sp->isc, opq);
356 // see RFC 3720, 10.9.1 page 146
359 | the call to isc_stop_receiver is a kludge,
360 | instead, it should be handled by the userland controller,
361 | but that means that there should be a better way, other than
362 | sending a signal. Somehow, this packet should be supplied to
363 | the userland via read.
366 iscsi_async(isc_session_t *sp, pduq_t *pq)
368 pdu_t *pp = &pq->pdu;
369 async_t *cmd = &pp->ipdu.async;
373 sdebug(3, "asyncevent=0x%x asyncVCode=0x%0x", cmd->asyncEvent, cmd->asyncVCode);
374 switch(cmd->asyncEvent) {
375 case 0: // check status ...
378 case 1: // target request logout
379 isc_stop_receiver(sp); // XXX: temporary solution
382 case 2: // target indicates it wants to drop connection
383 isc_stop_receiver(sp); // XXX: temporary solution
386 case 3: // target indicates it will drop all connections.
387 isc_stop_receiver(sp); // XXX: temporary solution
390 case 4: // target request parameter negotiation
399 iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
401 union ccb *ccb = opq->ccb;
402 //reject_t *reject = &pq->pdu.ipdu.reject;
405 //XXX: check RFC 10.17.1 (page 176)
406 ccb->ccb_h.status = CAM_REQ_ABORTED;
409 pdu_free(sp->isc, opq);
416 dwl(isc_session_t *sp, int lun, u_char *lp)
419 sdebug(4, "lun=%d", lun);
421 | mapping LUN to iSCSI LUN
422 | check the SAM-2 specs
423 | hint: maxLUNS is a small number, cam's LUN is 32bits
424 | iSCSI is 64bits, scsi is ?
426 // XXX: check if this will pass the endian test
432 lp[0] = (1 << 5) | ((lun >> 8) & 0x3f);
436 xdebug("lun %d: is unsupported!", lun);
444 | encapsulate the scsi command and
447 scsi_encap(struct cam_sim *sim, union ccb *ccb)
449 isc_session_t *sp = cam_sim_softc(sim);
450 struct ccb_scsiio *csio = &ccb->csio;
451 struct ccb_hdr *ccb_h = &ccb->ccb_h;
457 debug(4, "ccb->sp=%p", ccb_h->spriv_ptr0);
458 sp = ccb_h->spriv_ptr0;
460 if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
461 debug(2, "ccb->sp=%p", ccb_h->spriv_ptr0);
462 sdebug(1, "pdu_alloc failed sc->npdu_max=%d npdu_alloc=%d",
463 sp->isc->npdu_max, sp->isc->npdu_alloc);
464 while((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
465 sdebug(2, "waiting...");
466 #if __FreeBSD_version >= 700000
467 pause("isc_encap", 5*hz);
469 tsleep(sp->isc, 0, "isc_encap", 5*hz);
473 cmd = &pq->pdu.ipdu.scsi_req;
474 cmd->opcode = ISCSI_SCSI_CMD;
477 | map tag option, default is UNTAGGED
479 switch(csio->tag_action) {
480 case MSG_SIMPLE_Q_TAG: cmd->attr = iSCSI_TASK_SIMPLE; break;
481 case MSG_HEAD_OF_Q_TAG: cmd->attr = iSCSI_TASK_HOFQ; break;
482 case MSG_ORDERED_Q_TAG: cmd->attr = iSCSI_TASK_ORDER; break;
483 case MSG_ACA_TASK: cmd->attr = iSCSI_TASK_ACA; break;
486 dwl(sp, ccb_h->target_lun, (u_char *)&cmd->lun);
488 if((ccb_h->flags & CAM_CDB_POINTER) != 0) {
489 if((ccb_h->flags & CAM_CDB_PHYS) == 0) {
490 if(csio->cdb_len > 16) {
491 sdebug(3, "oversize cdb %d > 16", csio->cdb_len);
496 sdebug(3, "not phys");
501 if(csio->cdb_len > sizeof(cmd->cdb))
502 xdebug("guevalt! %d > %ld", csio->cdb_len, (long)sizeof(cmd->cdb));
505 ccb_h->flags & CAM_CDB_POINTER? csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
508 cmd->W = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT;
509 cmd->R = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN;
510 cmd->edtlen = htonl(csio->dxfer_len);
514 | place it in the out queue
516 if(isc_qout(sp, pq) == 0)
519 ccb->ccb_h.status = CAM_REQ_INVALID;
520 pdu_free(sp->isc, pq);
526 scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
528 union ccb *ccb = opq->ccb;
529 struct ccb_scsiio *csio = &ccb->csio;
530 pdu_t *opp = &opq->pdu;
531 bhs_t *bhp = &opp->ipdu.bhs;
534 sdebug(6, "pq=%p opq=%p bhp->opcode=0x%x len=%d",
535 pq, opq, bhp->opcode, pq->pdu.ds_len);
537 sdebug(1, "itt=0x%x pq=%p opq=%p bhp->opcode=0x%x len=%d",
538 ntohl(pq->pdu.ipdu.bhs.itt),
539 pq, opq, bhp->opcode, pq->pdu.ds_len);
540 xdebug("%d] ccb == NULL!", sp->sid);
543 if(pq->pdu.ds_len != 0) {
544 switch(bhp->opcode) {
545 case ISCSI_SCSI_CMD: {
546 scsi_req_t *cmd = &opp->ipdu.scsi_req;
547 sdebug(5, "itt=0x%x opcode=%x R=%d",
548 ntohl(pq->pdu.ipdu.bhs.itt),
549 pq->pdu.ipdu.bhs.opcode, cmd->R);
551 switch(pq->pdu.ipdu.bhs.opcode) {
552 case ISCSI_READ_DATA: // SCSI Data in
554 caddr_t bp = NULL; // = mtod(pq->mp, caddr_t);
555 data_in_t *rcmd = &pq->pdu.ipdu.data_in;
558 sdebug(5, "copy to=%p from=%p l1=%d l2=%d mp@%p",
559 csio->data_ptr, bp? mtod(pq->mp, caddr_t): 0,
560 ntohl(cmd->edtlen), pq->pdu.ds_len, pq->mp);
561 if(ntohl(cmd->edtlen) >= pq->pdu.ds_len) {
562 int offset, len = pq->pdu.ds_len;
567 offset = ntohl(rcmd->bo);
568 dp = csio->data_ptr + offset;
569 i_mbufcopy(pq->mp, dp, len);
573 xdebug("edtlen=%d < ds_len=%d",
574 ntohl(cmd->edtlen), pq->pdu.ds_len);
579 | contains also the SCSI Status
581 _scsi_done(sp, 0, rcmd->status, opq->ccb, NULL);
590 sdebug(3, "opcode=%02x", bhp->opcode);