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>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_sim.h>
52 #include <cam/cam_xpt_sim.h>
53 #include <cam/cam_periph.h>
54 #include <cam/scsi/scsi_message.h>
55 #include <sys/eventhandler.h>
57 #include <dev/iscsi_initiator/iscsi.h>
58 #include <dev/iscsi_initiator/iscsivar.h>
61 | Interface to the SCSI layer
64 iscsi_r2t(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
66 union ccb *ccb = opq->ccb;
67 struct ccb_scsiio *csio = &ccb->csio;
68 pdu_t *opp = &opq->pdu;
69 bhs_t *bhp = &opp->ipdu.bhs;
70 r2t_t *r2t = &pq->pdu.ipdu.r2t;
75 sdebug(4, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt),
76 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W);
80 if(opp->ipdu.scsi_req.W) {
82 u_int ddtl = ntohl(r2t->ddtl);
83 u_int edtl = ntohl(opp->ipdu.scsi_req.edtlen);
84 u_int bleft, bs, dsn, bo;
85 caddr_t bp = csio->data_ptr;
88 bp += MIN(bo, edtl - ddtl);
91 if(sp->opt.maxXmitDataSegmentLength > 0) // danny's RFC
92 bs = MIN(sp->opt.maxXmitDataSegmentLength, ddtl);
96 sdebug(4, "edtl=%x ddtl=%x bo=%x dsn=%x bs=%x maxX=%x",
97 edtl, ddtl, bo, dsn, bs, sp->opt.maxXmitDataSegmentLength);
99 wpq = pdu_alloc(sp->isc, M_NOWAIT); // testing ...
101 sdebug(3, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt),
102 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W);
103 sdebug(1, "npdu_max=%d npdu_alloc=%d", sp->isc->npdu_max, sp->isc->npdu_alloc);
105 while((wpq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
106 sdebug(2, "waiting...");
107 #if __FreeBSD_version >= 700000
108 pause("isc_r2t", 5*hz);
110 tsleep(sp->isc, 0, "isc_r2t", 5*hz);
114 cmd = &wpq->pdu.ipdu.data_out;
115 cmd->opcode = ISCSI_WRITE_DATA;
116 cmd->lun[0] = r2t->lun[0];
117 cmd->lun[1] = r2t->lun[1];
121 cmd->dsn = htonl(dsn);
124 cmd->F = (bs < bleft)? 0: 1; // is this the last one?
127 wpq->pdu.ds_len = bs;
128 wpq->pdu.ds_addr = bp;
130 error = isc_qout(sp, wpq);
131 sdebug(6, "bs=%x bo=%x bp=%p dsn=%x error=%d", bs, bo, bp, dsn, error);
143 // XXX: should not happen ...
144 xdebug("huh? opcode=0x%x", bhp->opcode);
149 getSenseData(u_int status, union ccb *ccb, pduq_t *pq)
151 pdu_t *pp = &pq->pdu;
152 struct ccb_scsiio *scsi = (struct ccb_scsiio *)ccb;
153 struct scsi_sense_data *sense = &scsi->sense_data;
154 struct mbuf *m = pq->mp;
155 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp;
157 int sense_len, mustfree = 0;
158 int error_code, sense_key, asc, ascq;
160 bp = mtod(pq->mp, caddr_t);
161 if((sense_len = scsi_2btoul(bp)) == 0)
163 debug(4, "sense_len=%d", sense_len);
165 | according to the specs, the sense data cannot
166 | be larger than 252 ...
168 if(sense_len > m->m_len) {
169 bp = malloc(sense_len, M_ISCSI, M_WAITOK);
170 debug(3, "calling i_mbufcopy(len=%d)", sense_len);
171 i_mbufcopy(pq->mp, bp, sense_len);
174 scsi->scsi_status = status;
176 bcopy(bp+2, sense, min(sense_len, scsi->sense_len));
177 scsi->sense_resid = 0;
178 if(cmd->flag & (BIT(1)|BIT(2)))
179 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 // this breaks at least Isilon's iscsi target.
485 | map tag option, default is UNTAGGED
487 switch(csio->tag_action) {
488 case MSG_SIMPLE_Q_TAG: cmd->attr = iSCSI_TASK_SIMPLE; break;
489 case MSG_HEAD_OF_Q_TAG: cmd->attr = iSCSI_TASK_HOFQ; break;
490 case MSG_ORDERED_Q_TAG: cmd->attr = iSCSI_TASK_ORDER; break;
491 case MSG_ACA_TASK: cmd->attr = iSCSI_TASK_ACA; break;
494 cmd->attr = iSCSI_TASK_SIMPLE;
497 dwl(sp, ccb_h->target_lun, (u_char *)&cmd->lun);
499 if((ccb_h->flags & CAM_CDB_POINTER) != 0) {
500 if((ccb_h->flags & CAM_CDB_PHYS) == 0) {
501 if(csio->cdb_len > 16) {
502 sdebug(3, "oversize cdb %d > 16", csio->cdb_len);
507 sdebug(3, "not phys");
512 if(csio->cdb_len > sizeof(cmd->cdb))
513 xdebug("guevalt! %d > %ld", csio->cdb_len, (long)sizeof(cmd->cdb));
516 ccb_h->flags & CAM_CDB_POINTER? csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
519 cmd->W = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT;
520 cmd->R = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN;
521 cmd->edtlen = htonl(csio->dxfer_len);
525 | place it in the out queue
527 if(isc_qout(sp, pq) == 0)
530 ccb->ccb_h.status = CAM_REQ_INVALID;
531 pdu_free(sp->isc, pq);
537 scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
539 union ccb *ccb = opq->ccb;
540 struct ccb_scsiio *csio = &ccb->csio;
541 pdu_t *opp = &opq->pdu;
542 bhs_t *bhp = &opp->ipdu.bhs;
545 sdebug(6, "pq=%p opq=%p bhp->opcode=0x%x len=%d",
546 pq, opq, bhp->opcode, pq->pdu.ds_len);
548 sdebug(1, "itt=0x%x pq=%p opq=%p bhp->opcode=0x%x len=%d",
549 ntohl(pq->pdu.ipdu.bhs.itt),
550 pq, opq, bhp->opcode, pq->pdu.ds_len);
551 xdebug("%d] ccb == NULL!", sp->sid);
554 if(pq->pdu.ds_len != 0) {
555 switch(bhp->opcode) {
556 case ISCSI_SCSI_CMD: {
557 scsi_req_t *cmd = &opp->ipdu.scsi_req;
558 sdebug(5, "itt=0x%x opcode=%x R=%d",
559 ntohl(pq->pdu.ipdu.bhs.itt),
560 pq->pdu.ipdu.bhs.opcode, cmd->R);
562 switch(pq->pdu.ipdu.bhs.opcode) {
563 case ISCSI_READ_DATA: // SCSI Data in
565 caddr_t bp = NULL; // = mtod(pq->mp, caddr_t);
566 data_in_t *rcmd = &pq->pdu.ipdu.data_in;
569 sdebug(5, "copy to=%p from=%p l1=%d l2=%d mp@%p",
570 csio->data_ptr, bp? mtod(pq->mp, caddr_t): 0,
571 ntohl(cmd->edtlen), pq->pdu.ds_len, pq->mp);
572 if(ntohl(cmd->edtlen) >= pq->pdu.ds_len) {
573 int offset, len = pq->pdu.ds_len;
578 offset = ntohl(rcmd->bo);
579 dp = csio->data_ptr + offset;
580 i_mbufcopy(pq->mp, dp, len);
584 xdebug("edtlen=%d < ds_len=%d",
585 ntohl(cmd->edtlen), pq->pdu.ds_len);
590 | contains also the SCSI Status
592 _scsi_done(sp, 0, rcmd->status, opq->ccb, NULL);
601 sdebug(3, "opcode=%02x", bhp->opcode);