2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | $Id: iscsi_subr.c 743 2009-08-08 10:54:53Z danny $
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_iscsi_initiator.h"
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/callout.h>
41 #include <sys/malloc.h>
43 #include <sys/kthread.h>
45 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/cam_periph.h>
56 #include <cam/scsi/scsi_message.h>
57 #include <sys/eventhandler.h>
59 #include <dev/iscsi_initiator/iscsi.h>
60 #include <dev/iscsi_initiator/iscsivar.h>
63 | Interface to the SCSI layer
66 iscsi_r2t(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
68 union ccb *ccb = opq->ccb;
69 struct ccb_scsiio *csio = &ccb->csio;
70 pdu_t *opp = &opq->pdu;
71 bhs_t *bhp = &opp->ipdu.bhs;
72 r2t_t *r2t = &pq->pdu.ipdu.r2t;
77 sdebug(4, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt),
78 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W);
82 if(opp->ipdu.scsi_req.W) {
84 u_int ddtl = ntohl(r2t->ddtl);
85 u_int edtl = ntohl(opp->ipdu.scsi_req.edtlen);
86 u_int bleft, bs, dsn, bo;
87 caddr_t bp = csio->data_ptr;
90 bp += MIN(bo, edtl - ddtl);
93 if(sp->opt.maxXmitDataSegmentLength > 0) // danny's RFC
94 bs = MIN(sp->opt.maxXmitDataSegmentLength, ddtl);
98 sdebug(4, "edtl=%x ddtl=%x bo=%x dsn=%x bs=%x maxX=%x",
99 edtl, ddtl, bo, dsn, bs, sp->opt.maxXmitDataSegmentLength);
101 wpq = pdu_alloc(sp->isc, M_NOWAIT); // testing ...
103 sdebug(3, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt),
104 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W);
105 sdebug(1, "npdu_max=%d npdu_alloc=%d", sp->isc->npdu_max, sp->isc->npdu_alloc);
107 while((wpq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
108 sdebug(2, "waiting...");
109 #if __FreeBSD_version >= 700000
110 pause("isc_r2t", 5*hz);
112 tsleep(sp->isc, 0, "isc_r2t", 5*hz);
116 cmd = &wpq->pdu.ipdu.data_out;
117 cmd->opcode = ISCSI_WRITE_DATA;
118 cmd->lun[0] = r2t->lun[0];
119 cmd->lun[1] = r2t->lun[1];
123 cmd->dsn = htonl(dsn);
126 cmd->F = (bs < bleft)? 0: 1; // is this the last one?
129 wpq->pdu.ds_len = bs;
130 wpq->pdu.ds_addr = bp;
132 error = isc_qout(sp, wpq);
133 sdebug(6, "bs=%x bo=%x bp=%p dsn=%x error=%d", bs, bo, bp, dsn, error);
145 // XXX: should not happen ...
146 xdebug("huh? opcode=0x%x", bhp->opcode);
151 getSenseData(u_int status, union ccb *ccb, pduq_t *pq)
153 pdu_t *pp = &pq->pdu;
154 struct ccb_scsiio *scsi = (struct ccb_scsiio *)ccb;
155 struct scsi_sense_data *sense = &scsi->sense_data;
156 struct mbuf *m = pq->mp;
157 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp;
159 int sense_len, mustfree = 0;
160 int error_code, sense_key, asc, ascq;
162 bp = mtod(pq->mp, caddr_t);
163 if((sense_len = scsi_2btoul(bp)) == 0)
165 debug(4, "sense_len=%d", sense_len);
167 | according to the specs, the sense data cannot
168 | be larger than 252 ...
170 if(sense_len > m->m_len) {
171 bp = malloc(sense_len, M_ISCSI, M_WAITOK);
172 debug(3, "calling i_mbufcopy(len=%d)", sense_len);
173 i_mbufcopy(pq->mp, bp, sense_len);
176 scsi->scsi_status = status;
178 bcopy(bp+2, sense, min(sense_len, scsi->sense_len));
179 scsi->sense_resid = 0;
180 if(cmd->flag & (BIT(1)|BIT(2)))
181 scsi->sense_resid = ntohl(pp->ipdu.scsi_rsp.rcnt);
182 scsi_extract_sense_len(sense, scsi->sense_len - scsi->sense_resid,
183 &error_code, &sense_key, &asc, &ascq, /*show_errors*/ 1);
185 debug(3, "sense_len=%d rcnt=%d sense_resid=%d dsl=%d error_code=%x flags=%x",
187 ntohl(pp->ipdu.scsi_rsp.rcnt), scsi->sense_resid,
188 pp->ds_len, error_code, sense_key);
197 | Some information is from SAM draft.
200 _scsi_done(isc_session_t *sp, u_int response, u_int status, union ccb *ccb, pduq_t *pq)
202 struct ccb_hdr *ccb_h = &ccb->ccb_h;
206 if(status || response) {
207 sdebug(3, "response=%x status=%x ccb=%p pq=%p", response, status, ccb, pq);
209 sdebug(3, "mp=%p buf=%p len=%d", pq->mp, pq->buf, pq->len);
213 case 0: // Command Completed at Target
215 case 0: // Good, all is ok
216 ccb_h->status = CAM_REQ_CMP;
219 case 0x02: // Check Condition
220 if((pq != NULL) && (pq->mp != NULL) && getSenseData(status, ccb, pq))
221 ccb_h->status |= CAM_AUTOSNS_VALID;
223 case 0x14: // Intermediate-Condition Met
224 case 0x10: // Intermediate
225 case 0x04: // Condition Met
226 ccb_h->status |= CAM_SCSI_STATUS_ERROR;
230 ccb_h->status = CAM_BUSY;
233 case 0x18: // Reservation Conflict
234 case 0x28: // Task Set Full
235 ccb_h->status = CAM_REQUEUE_REQ;
238 //case 0x22: // Command Terminated
239 //case 0x30: // ACA Active
240 //case 0x40: // Task Aborted
241 ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED;
246 if((response >= 0x80) && (response <= 0xFF)) {
247 // Vendor specific ...
249 case 1: // target failure
250 ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED;
253 sdebug(5, "ccb_h->status=%x", ccb_h->status);
259 | returns the lowest cmdseq that was not acked
262 iscsi_requeue(isc_session_t *sp)
269 sp->flags |= ISC_HOLD;
270 while((pq = i_dqueue_hld(sp)) != NULL) {
272 if(pq->ccb != NULL) {
273 _scsi_done(sp, 0, 0x28, pq->ccb, NULL);
274 n = ntohl(pq->pdu.ipdu.bhs.CmdSN);
275 if(last==0 || (last > n))
277 sdebug(2, "last=%x n=%x", last, n);
279 pdu_free(sp->isc, pq);
281 sp->flags &= ~ISC_HOLD;
282 return i? last: sp->sn.cmd;
286 i_pdu_flush(isc_session_t *sp)
292 while((pq = i_dqueue_rsp(sp)) != NULL) {
293 pdu_free(sp->isc, pq);
296 while((pq = i_dqueue_rsv(sp)) != NULL) {
297 pdu_free(sp->isc, pq);
300 while((pq = i_dqueue_snd(sp, -1)) != NULL) {
301 pdu_free(sp->isc, pq);
304 while((pq = i_dqueue_hld(sp)) != NULL) {
305 pdu_free(sp->isc, pq);
308 while((pq = i_dqueue_wsnd(sp)) != NULL) {
309 pdu_free(sp->isc, pq);
313 xdebug("%d pdus recovered, should have been ZERO!", n);
317 | called from ism_destroy.
320 iscsi_cleanup(isc_session_t *sp)
326 TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, pqtmp) {
327 sdebug(3, "hld pq=%p", pq);
329 _scsi_done(sp, 1, 0x40, pq->ccb, NULL);
330 TAILQ_REMOVE(&sp->hld, pq, pq_link);
332 free(pq->buf, M_ISCSIBUF);
335 pdu_free(sp->isc, pq);
337 while((pq = i_dqueue_snd(sp, BIT(0)|BIT(1)|BIT(2))) != NULL) {
338 sdebug(3, "pq=%p", pq);
340 _scsi_done(sp, 1, 0x40, pq->ccb, NULL);
342 free(pq->buf, M_ISCSIBUF);
345 pdu_free(sp->isc, pq);
352 iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
354 pdu_t *pp = &pq->pdu;
355 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp;
359 _scsi_done(sp, cmd->response, cmd->status, opq->ccb, pq);
361 pdu_free(sp->isc, opq);
364 // see RFC 3720, 10.9.1 page 146
367 | the call to isc_stop_receiver is a kludge,
368 | instead, it should be handled by the userland controller,
369 | but that means that there should be a better way, other than
370 | sending a signal. Somehow, this packet should be supplied to
371 | the userland via read.
374 iscsi_async(isc_session_t *sp, pduq_t *pq)
376 pdu_t *pp = &pq->pdu;
377 async_t *cmd = &pp->ipdu.async;
381 sdebug(3, "asyncevent=0x%x asyncVCode=0x%0x", cmd->asyncEvent, cmd->asyncVCode);
382 switch(cmd->asyncEvent) {
383 case 0: // check status ...
386 case 1: // target request logout
387 isc_stop_receiver(sp); // XXX: temporary solution
390 case 2: // target indicates it wants to drop connection
391 isc_stop_receiver(sp); // XXX: temporary solution
394 case 3: // target indicates it will drop all connections.
395 isc_stop_receiver(sp); // XXX: temporary solution
398 case 4: // target request parameter negotiation
407 iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
409 union ccb *ccb = opq->ccb;
410 //reject_t *reject = &pq->pdu.ipdu.reject;
413 //XXX: check RFC 10.17.1 (page 176)
414 ccb->ccb_h.status = CAM_REQ_ABORTED;
417 pdu_free(sp->isc, opq);
424 dwl(isc_session_t *sp, int lun, u_char *lp)
427 sdebug(4, "lun=%d", lun);
429 | mapping LUN to iSCSI LUN
430 | check the SAM-2 specs
431 | hint: maxLUNS is a small number, cam's LUN is 32bits
432 | iSCSI is 64bits, scsi is ?
434 // XXX: check if this will pass the endian test
440 lp[0] = (1 << 5) | ((lun >> 8) & 0x3f);
444 xdebug("lun %d: is unsupported!", lun);
452 | encapsulate the scsi command and
455 scsi_encap(struct cam_sim *sim, union ccb *ccb)
457 isc_session_t *sp = cam_sim_softc(sim);
458 struct ccb_scsiio *csio = &ccb->csio;
459 struct ccb_hdr *ccb_h = &ccb->ccb_h;
465 debug(4, "ccb->sp=%p", ccb_h->spriv_ptr0);
466 sp = ccb_h->spriv_ptr0;
468 if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
469 debug(2, "ccb->sp=%p", ccb_h->spriv_ptr0);
470 sdebug(1, "pdu_alloc failed sc->npdu_max=%d npdu_alloc=%d",
471 sp->isc->npdu_max, sp->isc->npdu_alloc);
472 while((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) {
473 sdebug(2, "waiting...");
474 #if __FreeBSD_version >= 700000
475 pause("isc_encap", 5*hz);
477 tsleep(sp->isc, 0, "isc_encap", 5*hz);
481 cmd = &pq->pdu.ipdu.scsi_req;
482 cmd->opcode = ISCSI_SCSI_CMD;
485 // this breaks at least Isilon's iscsi target.
487 | map tag option, default is UNTAGGED
489 switch(csio->tag_action) {
490 case MSG_SIMPLE_Q_TAG: cmd->attr = iSCSI_TASK_SIMPLE; break;
491 case MSG_HEAD_OF_Q_TAG: cmd->attr = iSCSI_TASK_HOFQ; break;
492 case MSG_ORDERED_Q_TAG: cmd->attr = iSCSI_TASK_ORDER; break;
493 case MSG_ACA_TASK: cmd->attr = iSCSI_TASK_ACA; break;
496 cmd->attr = iSCSI_TASK_SIMPLE;
499 dwl(sp, ccb_h->target_lun, (u_char *)&cmd->lun);
501 if((ccb_h->flags & CAM_CDB_POINTER) != 0) {
502 if((ccb_h->flags & CAM_CDB_PHYS) == 0) {
503 if(csio->cdb_len > 16) {
504 sdebug(3, "oversize cdb %d > 16", csio->cdb_len);
509 sdebug(3, "not phys");
514 if(csio->cdb_len > sizeof(cmd->cdb))
515 xdebug("guevalt! %d > %ld", csio->cdb_len, (long)sizeof(cmd->cdb));
518 ccb_h->flags & CAM_CDB_POINTER? csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
521 cmd->W = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT;
522 cmd->R = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN;
523 cmd->edtlen = htonl(csio->dxfer_len);
527 | place it in the out queue
529 if(isc_qout(sp, pq) == 0)
532 ccb->ccb_h.status = CAM_REQ_INVALID;
533 pdu_free(sp->isc, pq);
539 scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
541 union ccb *ccb = opq->ccb;
542 struct ccb_scsiio *csio = &ccb->csio;
543 pdu_t *opp = &opq->pdu;
544 bhs_t *bhp = &opp->ipdu.bhs;
547 sdebug(6, "pq=%p opq=%p bhp->opcode=0x%x len=%d",
548 pq, opq, bhp->opcode, pq->pdu.ds_len);
550 sdebug(1, "itt=0x%x pq=%p opq=%p bhp->opcode=0x%x len=%d",
551 ntohl(pq->pdu.ipdu.bhs.itt),
552 pq, opq, bhp->opcode, pq->pdu.ds_len);
553 xdebug("%d] ccb == NULL!", sp->sid);
556 if(pq->pdu.ds_len != 0) {
557 switch(bhp->opcode) {
558 case ISCSI_SCSI_CMD: {
559 scsi_req_t *cmd = &opp->ipdu.scsi_req;
560 sdebug(5, "itt=0x%x opcode=%x R=%d",
561 ntohl(pq->pdu.ipdu.bhs.itt),
562 pq->pdu.ipdu.bhs.opcode, cmd->R);
564 switch(pq->pdu.ipdu.bhs.opcode) {
565 case ISCSI_READ_DATA: // SCSI Data in
567 caddr_t bp = NULL; // = mtod(pq->mp, caddr_t);
568 data_in_t *rcmd = &pq->pdu.ipdu.data_in;
571 sdebug(5, "copy to=%p from=%p l1=%d l2=%d mp@%p",
572 csio->data_ptr, bp? mtod(pq->mp, caddr_t): 0,
573 ntohl(cmd->edtlen), pq->pdu.ds_len, pq->mp);
574 if(ntohl(cmd->edtlen) >= pq->pdu.ds_len) {
575 int offset, len = pq->pdu.ds_len;
580 offset = ntohl(rcmd->bo);
581 dp = csio->data_ptr + offset;
582 i_mbufcopy(pq->mp, dp, len);
586 xdebug("edtlen=%d < ds_len=%d",
587 ntohl(cmd->edtlen), pq->pdu.ds_len);
592 | contains also the SCSI Status
594 _scsi_done(sp, 0, rcmd->status, opq->ccb, NULL);
603 sdebug(3, "opcode=%02x", bhp->opcode);