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;
87 bp += MIN(bo, edtl - ddtl);
90 if(sp->opt.maxXmitDataSegmentLength > 0) // danny's RFC
91 bs = MIN(sp->opt.maxXmitDataSegmentLength, ddtl);
95 sdebug(4, "edtl=%x ddtl=%x bo=%x dsn=%x bs=%x maxX=%x",
96 edtl, ddtl, bo, dsn, bs, sp->opt.maxXmitDataSegmentLength);
98 wpq = pdu_alloc(sp->isc, M_NOWAIT); // testing ...
100 sdebug(3, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt),
101 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W);
102 sdebug(1, "npdu_max=%d npdu_alloc=%d", sp->isc->npdu_max, sp->isc->npdu_alloc);
104 while((wpq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
105 sdebug(2, "waiting...");
106 #if __FreeBSD_version >= 700000
107 pause("isc_r2t", 5*hz);
109 tsleep(sp->isc, 0, "isc_r2t", 5*hz);
113 cmd = &wpq->pdu.ipdu.data_out;
114 cmd->opcode = ISCSI_WRITE_DATA;
115 cmd->lun[0] = r2t->lun[0];
116 cmd->lun[1] = r2t->lun[1];
120 cmd->dsn = htonl(dsn);
123 cmd->F = (bs < bleft)? 0: 1; // is this the last one?
126 wpq->pdu.ds_len = bs;
127 wpq->pdu.ds_addr = bp;
129 error = isc_qout(sp, wpq);
130 sdebug(6, "bs=%x bo=%x bp=%p dsn=%x error=%d", bs, bo, bp, dsn, error);
142 // XXX: should not happen ...
143 xdebug("huh? opcode=0x%x", bhp->opcode);
148 getSenseData(u_int status, union ccb *ccb, pduq_t *pq)
150 pdu_t *pp = &pq->pdu;
151 struct ccb_scsiio *scsi = (struct ccb_scsiio *)ccb;
152 struct scsi_sense_data *sense = &scsi->sense_data;
153 struct mbuf *m = pq->mp;
154 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp;
156 int sense_len, mustfree = 0;
157 int error_code, sense_key, asc, ascq;
159 bp = mtod(pq->mp, caddr_t);
160 if((sense_len = scsi_2btoul(bp)) == 0)
162 debug(4, "sense_len=%d", sense_len);
164 | according to the specs, the sense data cannot
165 | be larger than 252 ...
167 if(sense_len > m->m_len) {
168 bp = malloc(sense_len, M_ISCSI, M_WAITOK);
169 debug(3, "calling i_mbufcopy(len=%d)", sense_len);
170 i_mbufcopy(pq->mp, bp, sense_len);
173 scsi->scsi_status = status;
175 bcopy(bp+2, sense, min(sense_len, scsi->sense_len));
176 scsi->sense_resid = 0;
177 if(cmd->flag & (BIT(1)|BIT(2)))
178 scsi->sense_resid = ntohl(pp->ipdu.scsi_rsp.rcnt);
180 scsi_extract_sense_len(sense, scsi->sense_len - scsi->sense_resid,
181 &error_code, &sense_key, &asc, &ascq, /*show_errors*/ 1);
183 debug(3, "sense_len=%d rcnt=%d sense_resid=%d dsl=%d error_code=%x flags=%x",
185 ntohl(pp->ipdu.scsi_rsp.rcnt), scsi->sense_resid,
186 pp->ds_len, error_code, sense_key);
195 | Some information is from SAM draft.
198 _scsi_done(isc_session_t *sp, u_int response, u_int status, union ccb *ccb, pduq_t *pq)
200 struct ccb_hdr *ccb_h = &ccb->ccb_h;
204 if(status || response) {
205 sdebug(3, "response=%x status=%x ccb=%p pq=%p", response, status, ccb, pq);
207 sdebug(3, "mp=%p buf=%p len=%d", pq->mp, pq->buf, pq->len);
211 case 0: // Command Completed at Target
213 case 0: // Good, all is ok
214 ccb_h->status = CAM_REQ_CMP;
217 case 0x02: // Check Condition
218 if((pq != NULL) && (pq->mp != NULL) && getSenseData(status, ccb, pq))
219 ccb_h->status |= CAM_AUTOSNS_VALID;
221 case 0x14: // Intermediate-Condition Met
222 case 0x10: // Intermediate
223 case 0x04: // Condition Met
224 ccb_h->status |= CAM_SCSI_STATUS_ERROR;
228 ccb_h->status = CAM_BUSY;
231 case 0x18: // Reservation Conflict
232 case 0x28: // Task Set Full
233 ccb_h->status = CAM_REQUEUE_REQ;
236 //case 0x22: // Command Terminated
237 //case 0x30: // ACA Active
238 //case 0x40: // Task Aborted
239 ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED;
244 if((response >= 0x80) && (response <= 0xFF)) {
245 // Vendor specific ...
247 case 1: // target failure
248 ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED;
251 sdebug(5, "ccb_h->status=%x", ccb_h->status);
257 | returns the lowest cmdseq that was not acked
260 iscsi_requeue(isc_session_t *sp)
267 sp->flags |= ISC_HOLD;
268 while((pq = i_dqueue_hld(sp)) != NULL) {
270 if(pq->ccb != NULL) {
271 _scsi_done(sp, 0, 0x28, pq->ccb, NULL);
272 n = ntohl(pq->pdu.ipdu.bhs.CmdSN);
273 if(last==0 || (last > n))
275 sdebug(2, "last=%x n=%x", last, n);
277 pdu_free(sp->isc, pq);
279 sp->flags &= ~ISC_HOLD;
280 return i? last: sp->sn.cmd;
284 i_pdu_flush(isc_session_t *sp)
290 while((pq = i_dqueue_rsp(sp)) != NULL) {
291 pdu_free(sp->isc, pq);
294 while((pq = i_dqueue_rsv(sp)) != NULL) {
295 pdu_free(sp->isc, pq);
298 while((pq = i_dqueue_snd(sp, -1)) != NULL) {
299 pdu_free(sp->isc, pq);
302 while((pq = i_dqueue_hld(sp)) != NULL) {
303 pdu_free(sp->isc, pq);
306 while((pq = i_dqueue_wsnd(sp)) != NULL) {
307 pdu_free(sp->isc, pq);
311 xdebug("%d pdus recovered, should have been ZERO!", n);
315 | called from ism_destroy.
318 iscsi_cleanup(isc_session_t *sp)
324 TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, pqtmp) {
325 sdebug(3, "hld pq=%p", pq);
327 _scsi_done(sp, 1, 0x40, pq->ccb, NULL);
328 TAILQ_REMOVE(&sp->hld, pq, pq_link);
330 free(pq->buf, M_ISCSIBUF);
333 pdu_free(sp->isc, pq);
335 while((pq = i_dqueue_snd(sp, BIT(0)|BIT(1)|BIT(2))) != NULL) {
336 sdebug(3, "pq=%p", pq);
338 _scsi_done(sp, 1, 0x40, pq->ccb, NULL);
340 free(pq->buf, M_ISCSIBUF);
343 pdu_free(sp->isc, pq);
350 iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
352 pdu_t *pp = &pq->pdu;
353 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp;
357 _scsi_done(sp, cmd->response, cmd->status, opq->ccb, pq);
359 pdu_free(sp->isc, opq);
362 // see RFC 3720, 10.9.1 page 146
365 | the call to isc_stop_receiver is a kludge,
366 | instead, it should be handled by the userland controller,
367 | but that means that there should be a better way, other than
368 | sending a signal. Somehow, this packet should be supplied to
369 | the userland via read.
372 iscsi_async(isc_session_t *sp, pduq_t *pq)
374 pdu_t *pp = &pq->pdu;
375 async_t *cmd = &pp->ipdu.async;
379 sdebug(3, "asyncevent=0x%x asyncVCode=0x%0x", cmd->asyncEvent, cmd->asyncVCode);
380 switch(cmd->asyncEvent) {
381 case 0: // check status ...
384 case 1: // target request logout
385 isc_stop_receiver(sp); // XXX: temporary solution
388 case 2: // target indicates it wants to drop connection
389 isc_stop_receiver(sp); // XXX: temporary solution
392 case 3: // target indicates it will drop all connections.
393 isc_stop_receiver(sp); // XXX: temporary solution
396 case 4: // target request parameter negotiation
405 iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
407 union ccb *ccb = opq->ccb;
408 //reject_t *reject = &pq->pdu.ipdu.reject;
411 //XXX: check RFC 10.17.1 (page 176)
412 ccb->ccb_h.status = CAM_REQ_ABORTED;
415 pdu_free(sp->isc, opq);
422 dwl(isc_session_t *sp, int lun, u_char *lp)
425 sdebug(4, "lun=%d", lun);
427 | mapping LUN to iSCSI LUN
428 | check the SAM-2 specs
429 | hint: maxLUNS is a small number, cam's LUN is 32bits
430 | iSCSI is 64bits, scsi is ?
432 // XXX: check if this will pass the endian test
438 lp[0] = (1 << 5) | ((lun >> 8) & 0x3f);
442 xdebug("lun %d: is unsupported!", lun);
450 | encapsulate the scsi command and
453 scsi_encap(struct cam_sim *sim, union ccb *ccb)
455 isc_session_t *sp = cam_sim_softc(sim);
456 struct ccb_scsiio *csio = &ccb->csio;
457 struct ccb_hdr *ccb_h = &ccb->ccb_h;
463 debug(4, "ccb->sp=%p", ccb_h->spriv_ptr0);
464 sp = ccb_h->spriv_ptr0;
466 if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
467 debug(2, "ccb->sp=%p", ccb_h->spriv_ptr0);
468 sdebug(1, "pdu_alloc failed sc->npdu_max=%d npdu_alloc=%d",
469 sp->isc->npdu_max, sp->isc->npdu_alloc);
470 while((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
471 sdebug(2, "waiting...");
472 #if __FreeBSD_version >= 700000
473 pause("isc_encap", 5*hz);
475 tsleep(sp->isc, 0, "isc_encap", 5*hz);
479 cmd = &pq->pdu.ipdu.scsi_req;
480 cmd->opcode = ISCSI_SCSI_CMD;
483 | map tag option, default is UNTAGGED
485 switch(csio->tag_action) {
486 case MSG_SIMPLE_Q_TAG: cmd->attr = iSCSI_TASK_SIMPLE; break;
487 case MSG_HEAD_OF_Q_TAG: cmd->attr = iSCSI_TASK_HOFQ; break;
488 case MSG_ORDERED_Q_TAG: cmd->attr = iSCSI_TASK_ORDER; break;
489 case MSG_ACA_TASK: cmd->attr = iSCSI_TASK_ACA; break;
492 dwl(sp, ccb_h->target_lun, (u_char *)&cmd->lun);
494 if((ccb_h->flags & CAM_CDB_POINTER) != 0) {
495 if((ccb_h->flags & CAM_CDB_PHYS) == 0) {
496 if(csio->cdb_len > 16) {
497 sdebug(3, "oversize cdb %d > 16", csio->cdb_len);
502 sdebug(3, "not phys");
507 if(csio->cdb_len > sizeof(cmd->cdb))
508 xdebug("guevalt! %d > %ld", csio->cdb_len, (long)sizeof(cmd->cdb));
511 ccb_h->flags & CAM_CDB_POINTER? csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
514 cmd->W = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT;
515 cmd->R = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN;
516 cmd->edtlen = htonl(csio->dxfer_len);
520 | place it in the out queue
522 if(isc_qout(sp, pq) == 0)
525 ccb->ccb_h.status = CAM_REQ_INVALID;
526 pdu_free(sp->isc, pq);
532 scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
534 union ccb *ccb = opq->ccb;
535 struct ccb_scsiio *csio = &ccb->csio;
536 pdu_t *opp = &opq->pdu;
537 bhs_t *bhp = &opp->ipdu.bhs;
540 sdebug(6, "pq=%p opq=%p bhp->opcode=0x%x len=%d",
541 pq, opq, bhp->opcode, pq->pdu.ds_len);
543 sdebug(1, "itt=0x%x pq=%p opq=%p bhp->opcode=0x%x len=%d",
544 ntohl(pq->pdu.ipdu.bhs.itt),
545 pq, opq, bhp->opcode, pq->pdu.ds_len);
546 xdebug("%d] ccb == NULL!", sp->sid);
549 if(pq->pdu.ds_len != 0) {
550 switch(bhp->opcode) {
551 case ISCSI_SCSI_CMD: {
552 scsi_req_t *cmd = &opp->ipdu.scsi_req;
553 sdebug(5, "itt=0x%x opcode=%x R=%d",
554 ntohl(pq->pdu.ipdu.bhs.itt),
555 pq->pdu.ipdu.bhs.opcode, cmd->R);
557 switch(pq->pdu.ipdu.bhs.opcode) {
558 case ISCSI_READ_DATA: // SCSI Data in
560 caddr_t bp = NULL; // = mtod(pq->mp, caddr_t);
561 data_in_t *rcmd = &pq->pdu.ipdu.data_in;
564 sdebug(5, "copy to=%p from=%p l1=%d l2=%d mp@%p",
565 csio->data_ptr, bp? mtod(pq->mp, caddr_t): 0,
566 ntohl(cmd->edtlen), pq->pdu.ds_len, pq->mp);
567 if(ntohl(cmd->edtlen) >= pq->pdu.ds_len) {
568 int offset, len = pq->pdu.ds_len;
573 offset = ntohl(rcmd->bo);
574 dp = csio->data_ptr + offset;
575 i_mbufcopy(pq->mp, dp, len);
579 xdebug("edtlen=%d < ds_len=%d",
580 ntohl(cmd->edtlen), pq->pdu.ds_len);
585 | contains also the SCSI Status
587 _scsi_done(sp, 0, rcmd->status, opq->ccb, NULL);
596 sdebug(3, "opcode=%02x", bhp->opcode);