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 | iSCSI - Session Manager
29 | $Id: isc_sm.c 743 2009-08-08 10:54:53Z danny $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_iscsi_initiator.h"
37 #include <sys/param.h>
38 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/ctype.h>
43 #include <sys/errno.h>
44 #include <sys/sysctl.h>
47 #include <sys/socketvar.h>
48 #include <sys/socket.h>
49 #include <sys/protosw.h>
51 #include <sys/ioccom.h>
52 #include <sys/queue.h>
53 #include <sys/kthread.h>
54 #include <sys/syslog.h>
61 #include <cam/cam_ccb.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_xpt_sim.h>
64 #include <cam/cam_periph.h>
66 #include <dev/iscsi_initiator/iscsi.h>
67 #include <dev/iscsi_initiator/iscsivar.h>
70 _async(isc_session_t *sp, pduq_t *pq)
76 pdu_free(sp->isc, pq);
80 _reject(isc_session_t *sp, pduq_t *pq)
88 pdu = mtod(pq->mp, pdu_t *);
89 itt = pdu->ipdu.bhs.itt;
90 reject = &pq->pdu.ipdu.reject;
91 sdebug(2, "itt=%x reason=0x%x", ntohl(itt), reject->reason);
92 opq = i_search_hld(sp, itt, 0);
94 iscsi_reject(sp, opq, pq);
96 switch(pq->pdu.ipdu.bhs.opcode) {
97 case ISCSI_LOGOUT_CMD: // XXX: wasabi does this - can't figure out why
98 sdebug(2, "ISCSI_LOGOUT_CMD ...");
101 xdebug("%d] we lost something itt=%x",
102 sp->sid, ntohl(pq->pdu.ipdu.bhs.itt));
105 pdu_free(sp->isc, pq);
109 _r2t(isc_session_t *sp, pduq_t *pq)
114 opq = i_search_hld(sp, pq->pdu.ipdu.bhs.itt, 1);
116 iscsi_r2t(sp, opq, pq);
119 r2t_t *r2t = &pq->pdu.ipdu.r2t;
121 xdebug("%d] we lost something itt=%x r2tSN=%d bo=%x ddtl=%x",
122 sp->sid, ntohl(pq->pdu.ipdu.bhs.itt),
123 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl));
125 pdu_free(sp->isc, pq);
129 _scsi_rsp(isc_session_t *sp, pduq_t *pq)
134 opq = i_search_hld(sp, pq->pdu.ipdu.bhs.itt, 0);
135 debug(5, "itt=%x pq=%p opq=%p", ntohl(pq->pdu.ipdu.bhs.itt), pq, opq);
137 iscsi_done(sp, opq, pq);
138 i_acked_hld(sp, &pq->pdu);
141 xdebug("%d] we lost something itt=%x",
142 sp->sid, ntohl(pq->pdu.ipdu.bhs.itt));
143 pdu_free(sp->isc, pq);
147 _read_data(isc_session_t *sp, pduq_t *pq)
152 opq = i_search_hld(sp, pq->pdu.ipdu.bhs.itt, 1);
154 if(scsi_decap(sp, opq, pq) != 1) {
155 i_remove_hld(sp, opq); // done
156 pdu_free(sp->isc, opq);
160 xdebug("%d] we lost something itt=%x",
161 sp->sid, ntohl(pq->pdu.ipdu.bhs.itt));
162 pdu_free(sp->isc, pq);
166 | the jury is not back with a veredict, user or kernel
169 _nop_out(isc_session_t *sp)
176 sdebug(4, "cws=%d", sp->cws);
179 | only send a nop if window is closed.
181 if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL)
182 // I guess we ran out of resources
184 nop_out = &pq->pdu.ipdu.nop_out;
185 nop_out->opcode = ISCSI_NOP_OUT;
186 nop_out->itt = htonl(sp->sn.itt);
190 if(isc_qout(sp, pq) != 0) {
192 pdu_free(sp->isc, pq);
198 _nop_in(isc_session_t *sp, pduq_t *pq)
200 pdu_t *pp = &pq->pdu;
201 nop_in_t *nop_in = &pp->ipdu.nop_in;
202 bhs_t *bhs = &pp->ipdu.bhs;
206 sdebug(5, "itt=%x ttt=%x", htonl(nop_in->itt), htonl(nop_in->ttt));
207 if(nop_in->itt == -1) {
208 if(pp->ds_len != 0) {
210 | according to RFC 3720 this should be zero
213 xdebug("%d] dslen not zero", sp->sid);
215 if(nop_in->ttt != -1) {
218 | target wants a nop_out
220 bhs->opcode = ISCSI_NOP_OUT;
224 | we are reusing the pdu, so bhs->ttt == nop_in->ttt;
225 | and need to zero out 'Reserved'
228 nop_out = &pp->ipdu.nop_out;
229 nop_out->sn.maxcmd = 0;
230 memset(nop_out->mbz, 0, sizeof(nop_out->mbz));
231 (void)isc_qout(sp, pq); //XXX: should check return?
235 // just making noise?
236 // see 10.9.1: target does not want and answer.
240 if(nop_in->ttt == -1) {
242 | it is an answer to a nop_in from us
244 if(nop_in->itt != -1) {
247 if(sp->flags & ISC_WAIT4PING) {
248 i_nqueue_rsp(sp, pq);
258 pdu_free(sp->isc, pq);
263 i_prepPDU(isc_session_t *sp, pduq_t *pq)
266 pdu_t *pp = &pq->pdu;
267 bhs_t *bhp = &pp->ipdu.bhs;
272 bhp->AHSLength = pp->ahs_len / 4;
274 if(ISOK2DIG(sp->hdrDigest, pp))
279 #if BYTE_ORDER == LITTLE_ENDIAN
280 bhp->DSLength = ((n & 0x00ff0000) >> 16)
282 | ((n & 0x000000ff) << 16);
290 if(ISOK2DIG(sp->dataDigest, pp))
295 len -= sizeof(bhs_t);
296 if(sp->opt.maxBurstLength && (len > sp->opt.maxBurstLength)) {
297 xdebug("%d] pdu len=%zd > %d",
298 sp->sid, len, sp->opt.maxBurstLength);
299 // XXX: when this happens it used to hang ...
306 isc_qout(isc_session_t *sp, pduq_t *pq)
312 if(pq->len == 0 && (error = i_prepPDU(sp, pq)))
315 if(pq->pdu.ipdu.bhs.I)
316 i_nqueue_isnd(sp, pq);
318 if(pq->pdu.ipdu.data_out.opcode == ISCSI_WRITE_DATA)
319 i_nqueue_wsnd(sp, pq);
321 i_nqueue_csnd(sp, pq);
323 sdebug(5, "enqued: pq=%p", pq);
325 mtx_lock(&sp->io_mtx);
326 sp->flags |= ISC_OQNOTEMPTY;
327 if(sp->flags & ISC_OWAITING)
329 mtx_unlock(&sp->io_mtx);
334 | called when a fullPhase is restarted
337 ism_restart(isc_session_t *sp)
341 sdebug(2, "restart ...");
342 lastcmd = iscsi_requeue(sp);
344 if(lastcmd != sp->sn.cmd) {
345 sdebug(1, "resetting CmdSN to=%d (from %d)", lastcmd, sp->sn.cmd);
346 sp->sn.cmd = lastcmd;
349 mtx_lock(&sp->io_mtx);
350 if(sp->flags & ISC_OWAITING) {
353 mtx_unlock(&sp->io_mtx);
355 sdebug(2, "restarted sn.cmd=0x%x lastcmd=0x%x", sp->sn.cmd, lastcmd);
359 ism_recv(isc_session_t *sp, pduq_t *pq)
366 bhs = &pq->pdu.ipdu.bhs;
367 statSN = ntohl(bhs->OpcodeSpecificFields[1]);
370 if(sp->sn.expCmd != sn->cmd) {
371 sdebug(1, "we lost something ... exp=0x%x cmd=0x%x",
372 sn->expCmd, sn->cmd);
375 sdebug(5, "opcode=0x%x itt=0x%x stat#0x%x maxcmd=0x%0x",
376 bhs->opcode, ntohl(bhs->itt), statSN, sp->sn.maxCmd);
378 switch(bhs->opcode) {
379 case ISCSI_READ_DATA: {
380 data_in_t *cmd = &pq->pdu.ipdu.data_in;
387 if(statSN > (sp->sn.stat + 1)) {
388 sdebug(1, "we lost some rec=0x%x exp=0x%x",
389 statSN, sp->sn.stat);
390 // XXX: must do some error recovery here.
392 sp->sn.stat = statSN;
395 switch(bhs->opcode) {
396 case ISCSI_LOGIN_RSP:
398 case ISCSI_LOGOUT_RSP:
399 i_nqueue_rsp(sp, pq);
401 sdebug(3, "wakeup rsp");
404 case ISCSI_NOP_IN: _nop_in(sp, pq); break;
405 case ISCSI_SCSI_RSP: _scsi_rsp(sp, pq); break;
406 case ISCSI_READ_DATA: _read_data(sp, pq); break;
407 case ISCSI_R2T: _r2t(sp, pq); break;
408 case ISCSI_REJECT: _reject(sp, pq); break;
409 case ISCSI_ASYNC: _async(sp, pq); break;
413 sdebug(1, "opcode=0x%x itt=0x%x not implemented yet",
414 bhs->opcode, ntohl(bhs->itt));
420 | go through the out queues looking for work
421 | if either nothing to do, or window is closed
425 proc_out(isc_session_t *sp)
434 while(sp->flags & ISC_LINK_UP) {
438 | check if there is outstanding work in:
439 | 1- the Immediate queue
441 | 3- the cmd queue, only if the command window allows it.
443 which = BIT(0) | BIT(1);
444 if(SNA_GT(sn->cmd, sn->maxCmd) == 0) // if(sn->maxCmd - sn->smc + 1) > 0
447 sdebug(4, "which=%d sn->maxCmd=%d sn->cmd=%d", which, sn->maxCmd, sn->cmd);
449 if((pq = i_dqueue_snd(sp, which)) == NULL)
451 sdebug(4, "pq=%p", pq);
455 switch(bhs->opcode) {
458 bhs->itt = htonl(sn->itt);
460 case ISCSI_LOGIN_CMD:
462 case ISCSI_LOGOUT_CMD:
466 bhs->CmdSN = htonl(sn->cmd);
470 case ISCSI_WRITE_DATA:
471 bhs->ExpStSN = htonl(sn->stat + 1);
475 // XXX: can this happen?
476 xdebug("bad opcode=0x%x sn(cmd=0x%x expCmd=0x%x maxCmd=0x%x expStat=0x%x itt=0x%x)",
478 sn->cmd, sn->expCmd, sn->maxCmd, sn->expStat, sn->itt);
482 sdebug(4, "opcode=0x%x sn(cmd=0x%x expCmd=0x%x maxCmd=0x%x expStat=0x%x itt=0x%x)",
484 sn->cmd, sn->expCmd, sn->maxCmd, sn->expStat, sn->itt);
486 if(bhs->opcode != ISCSI_NOP_OUT)
488 | enqued till ack is received
489 | note: sosend(...) does not mean the packet left
490 | the host so that freeing resources has to wait
492 i_nqueue_hld(sp, pq);
494 error = isc_sendPDU(sp, pq);
495 if(bhs->opcode == ISCSI_NOP_OUT)
496 pdu_free(sp->isc, pq);
498 xdebug("error=%d opcode=0x%x ccb=%p itt=%x",
499 error, bhs->opcode, pq->ccb, ntohl(bhs->itt));
500 i_remove_hld(sp, pq);
503 sp->flags &= ~ISC_LINK_UP;
507 i_rqueue_pdu(sp, pq);
512 xdebug("back to cam");
513 pq->ccb->ccb_h.status |= CAM_REQUEUE_REQ; // some better error?
514 XPT_DONE(sp, pq->ccb);
515 pdu_free(sp->isc, pq);
518 xdebug("we lost it!");
526 | survives link breakdowns.
531 isc_session_t *sp = (isc_session_t *)vp;
536 sp->flags |= ISC_SM_RUNNING;
537 sdebug(3, "started sp->flags=%x", sp->flags);
539 if((sp->flags & ISC_HOLD) == 0) {
540 error = proc_out(sp);
542 sdebug(3, "error=%d", error);
545 mtx_lock(&sp->io_mtx);
546 if((sp->flags & ISC_LINK_UP) == 0) {
547 sdebug(3, "ISC_LINK_UP==0, sp->flags=%x ", sp->flags);
549 sdebug(3, "so_state=%x", sp->soc->so_state);
553 if(!(sp->flags & ISC_OQNOTEMPTY)) {
554 sp->flags |= ISC_OWAITING;
555 if(msleep(&sp->flags, &sp->io_mtx, PRIBIO, "isc_proc", hz*30) == EWOULDBLOCK) {
556 if(sp->flags & ISC_CON_RUNNING)
559 sp->flags &= ~ISC_OWAITING;
561 sp->flags &= ~ISC_OQNOTEMPTY;
562 mtx_unlock(&sp->io_mtx);
563 } while(sp->flags & ISC_SM_RUN);
565 sp->flags &= ~ISC_SM_RUNNING;
566 sdebug(3, "dropped ISC_SM_RUNNING");
569 wakeup(sp); // XXX: do we need this one?
571 #if __FreeBSD_version >= 700000
572 destroy_dev(sp->dev);
575 debug(3, "terminated sp=%p sp->sid=%d", sp, sp->sid);
577 #if __FreeBSD_version >= 800000
586 isc_dump_options(SYSCTL_HANDLER_ARGS)
592 sbuf_new_for_sysctl(&sb, NULL, 128, req);
594 sp = (isc_session_t *)arg1;
595 sbuf_printf(&sb, "targetname='%s'", sp->opt.targetName);
596 sbuf_printf(&sb, " targetaddress='%s'", sp->opt.targetAddress);
597 error = sbuf_finish(&sb);
604 isc_dump_stats(SYSCTL_HANDLER_ARGS)
607 struct isc_softc *sc;
611 sp = (isc_session_t *)arg1;
614 sbuf_new_for_sysctl(&sb, NULL, 128, req);
616 sbuf_printf(&sb, "recv=%d sent=%d", sp->stats.nrecv, sp->stats.nsent);
617 sbuf_printf(&sb, " flags=0x%08x pdus-alloc=%d pdus-max=%d",
618 sp->flags, sc->npdu_alloc, sc->npdu_max);
619 sbuf_printf(&sb, " cws=%d cmd=%x exp=%x max=%x stat=%x itt=%x",
620 sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
621 error = sbuf_finish(&sb);
627 isc_add_sysctls(isc_session_t *sp)
630 sdebug(6, "sid=%d %s", sp->sid, devtoname(sp->dev));
632 sysctl_ctx_init(&sp->clist);
633 sp->oid = SYSCTL_ADD_NODE(&sp->clist,
634 SYSCTL_CHILDREN(sp->isc->oid),
636 devtoname(sp->dev) + 5, // iscsi0
640 SYSCTL_ADD_PROC(&sp->clist,
641 SYSCTL_CHILDREN(sp->oid),
644 CTLTYPE_STRING | CTLFLAG_RD,
645 (void *)&sp->opt.targetName, 0,
646 sysctl_handle_string, "A", "target name");
648 SYSCTL_ADD_PROC(&sp->clist,
649 SYSCTL_CHILDREN(sp->oid),
652 CTLTYPE_STRING | CTLFLAG_RD,
653 (void *)&sp->opt.targetAddress, 0,
654 sysctl_handle_string, "A", "target address");
656 SYSCTL_ADD_PROC(&sp->clist,
657 SYSCTL_CHILDREN(sp->oid),
660 CTLTYPE_STRING | CTLFLAG_RD,
662 isc_dump_stats, "A", "statistics");
664 SYSCTL_ADD_INT(&sp->clist,
665 SYSCTL_CHILDREN(sp->oid),
669 &sp->douio, 0, "enable uio on read");
673 ism_stop(isc_session_t *sp)
675 struct isc_softc *sc = sp->isc;
679 sdebug(2, "terminating");
681 | first stop the receiver
683 isc_stop_receiver(sp);
685 | now stop the xmitter
688 sp->flags &= ~ISC_SM_RUN;
689 while(n-- && (sp->flags & ISC_SM_RUNNING)) {
690 sdebug(2, "n=%d", n);
692 tsleep(sp, PRIBIO, "-", 5*hz);
694 sdebug(2, "final n=%d", n);
695 sp->flags &= ~ISC_FFPHASE;
699 (void)i_pdu_flush(sp);
703 sx_xlock(&sc->unit_sx);
704 free_unr(sc->unit, sp->sid);
705 sx_xunlock(&sc->unit_sx);
707 mtx_lock(&sc->isc_mtx);
708 TAILQ_REMOVE(&sc->isc_sess, sp, sp_link);
710 mtx_unlock(&sc->isc_mtx);
712 #if __FreeBSD_version < 700000
713 destroy_dev(sp->dev);
716 mtx_destroy(&sp->rsp_mtx);
717 mtx_destroy(&sp->rsv_mtx);
718 mtx_destroy(&sp->hld_mtx);
719 mtx_destroy(&sp->snd_mtx);
720 mtx_destroy(&sp->io_mtx);
724 if(sysctl_ctx_free(&sp->clist))
725 xdebug("sysctl_ctx_free failed");
731 ism_start(isc_session_t *sp)
735 | now is a good time to do some initialization
737 TAILQ_INIT(&sp->rsp);
738 TAILQ_INIT(&sp->rsv);
739 TAILQ_INIT(&sp->csnd);
740 TAILQ_INIT(&sp->isnd);
741 TAILQ_INIT(&sp->wsnd);
742 TAILQ_INIT(&sp->hld);
744 mtx_init(&sp->rsv_mtx, "iscsi-rsv", NULL, MTX_DEF);
745 mtx_init(&sp->rsp_mtx, "iscsi-rsp", NULL, MTX_DEF);
746 mtx_init(&sp->snd_mtx, "iscsi-snd", NULL, MTX_DEF);
747 mtx_init(&sp->hld_mtx, "iscsi-hld", NULL, MTX_DEF);
748 mtx_init(&sp->io_mtx, "iscsi-io", NULL, MTX_DEF);
752 sp->flags |= ISC_SM_RUN;
754 debug(4, "starting ism_proc: sp->sid=%d", sp->sid);
756 #if __FreeBSD_version >= 800000
757 return kproc_create(ism_out, sp, &sp->stp, 0, 0, "isc_out %d", sp->sid);
759 return kthread_create(ism_out, sp, &sp->stp, 0, 0, "isc_out %d", sp->sid);