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>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_sim.h>
62 #include <cam/cam_xpt_sim.h>
63 #include <cam/cam_periph.h>
65 #include <dev/iscsi/initiator/iscsi.h>
66 #include <dev/iscsi/initiator/iscsivar.h>
69 _async(isc_session_t *sp, pduq_t *pq)
75 pdu_free(sp->isc, pq);
79 _reject(isc_session_t *sp, pduq_t *pq)
87 pdu = mtod(pq->mp, pdu_t *);
88 itt = pdu->ipdu.bhs.itt;
89 reject = &pq->pdu.ipdu.reject;
90 sdebug(2, "itt=%x reason=0x%x", ntohl(itt), reject->reason);
91 opq = i_search_hld(sp, itt, 0);
93 iscsi_reject(sp, opq, pq);
95 switch(pq->pdu.ipdu.bhs.opcode) {
96 case ISCSI_LOGOUT_CMD: // XXX: wasabi does this - can't figure out why
97 sdebug(2, "ISCSI_LOGOUT_CMD ...");
100 xdebug("%d] we lost something itt=%x",
101 sp->sid, ntohl(pq->pdu.ipdu.bhs.itt));
104 pdu_free(sp->isc, pq);
108 _r2t(isc_session_t *sp, pduq_t *pq)
113 opq = i_search_hld(sp, pq->pdu.ipdu.bhs.itt, 1);
115 iscsi_r2t(sp, opq, pq);
118 r2t_t *r2t = &pq->pdu.ipdu.r2t;
120 xdebug("%d] we lost something itt=%x r2tSN=%d bo=%x ddtl=%x",
121 sp->sid, ntohl(pq->pdu.ipdu.bhs.itt),
122 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl));
124 pdu_free(sp->isc, pq);
128 _scsi_rsp(isc_session_t *sp, pduq_t *pq)
133 opq = i_search_hld(sp, pq->pdu.ipdu.bhs.itt, 0);
134 debug(5, "itt=%x pq=%p opq=%p", ntohl(pq->pdu.ipdu.bhs.itt), pq, opq);
136 iscsi_done(sp, opq, pq);
137 i_acked_hld(sp, &pq->pdu);
140 xdebug("%d] we lost something itt=%x",
141 sp->sid, ntohl(pq->pdu.ipdu.bhs.itt));
142 pdu_free(sp->isc, pq);
146 _read_data(isc_session_t *sp, pduq_t *pq)
151 opq = i_search_hld(sp, pq->pdu.ipdu.bhs.itt, 1);
153 if(scsi_decap(sp, opq, pq) != 1) {
154 i_remove_hld(sp, opq); // done
155 pdu_free(sp->isc, opq);
159 xdebug("%d] we lost something itt=%x",
160 sp->sid, ntohl(pq->pdu.ipdu.bhs.itt));
161 pdu_free(sp->isc, pq);
165 | the jury is not back with a veredict, user or kernel
168 _nop_out(isc_session_t *sp)
175 sdebug(4, "cws=%d", sp->cws);
178 | only send a nop if window is closed.
180 if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL)
181 // I guess we ran out of resources
183 nop_out = &pq->pdu.ipdu.nop_out;
184 nop_out->opcode = ISCSI_NOP_OUT;
185 nop_out->itt = htonl(sp->sn.itt);
189 if(isc_qout(sp, pq) != 0) {
191 pdu_free(sp->isc, pq);
197 _nop_in(isc_session_t *sp, pduq_t *pq)
199 pdu_t *pp = &pq->pdu;
200 nop_in_t *nop_in = &pp->ipdu.nop_in;
201 bhs_t *bhs = &pp->ipdu.bhs;
205 sdebug(5, "itt=%x ttt=%x", htonl(nop_in->itt), htonl(nop_in->ttt));
206 if(nop_in->itt == -1) {
207 if(pp->ds_len != 0) {
209 | according to RFC 3720 this should be zero
212 xdebug("%d] dslen not zero", sp->sid);
214 if(nop_in->ttt != -1) {
217 | target wants a nop_out
219 bhs->opcode = ISCSI_NOP_OUT;
223 | we are reusing the pdu, so bhs->ttt == nop_in->ttt;
224 | and need to zero out 'Reserved'
227 nop_out = &pp->ipdu.nop_out;
228 nop_out->sn.maxcmd = 0;
229 memset(nop_out->mbz, 0, sizeof(nop_out->mbz));
230 (void)isc_qout(sp, pq); //XXX: should check return?
234 // just making noise?
235 // see 10.9.1: target does not want and answer.
239 if(nop_in->ttt == -1) {
241 | it is an answer to a nop_in from us
243 if(nop_in->itt != -1) {
246 if(sp->flags & ISC_WAIT4PING) {
247 i_nqueue_rsp(sp, pq);
257 pdu_free(sp->isc, pq);
262 i_prepPDU(isc_session_t *sp, pduq_t *pq)
265 pdu_t *pp = &pq->pdu;
266 bhs_t *bhp = &pp->ipdu.bhs;
271 bhp->AHSLength = pp->ahs_len / 4;
273 if(ISOK2DIG(sp->hdrDigest, pp))
278 #if BYTE_ORDER == LITTLE_ENDIAN
279 bhp->DSLength = ((n & 0x00ff0000) >> 16)
281 | ((n & 0x000000ff) << 16);
289 if(ISOK2DIG(sp->dataDigest, pp))
294 len -= sizeof(bhs_t);
295 if(sp->opt.maxBurstLength && (len > sp->opt.maxBurstLength)) {
296 xdebug("%d] pdu len=%zd > %d",
297 sp->sid, len, sp->opt.maxBurstLength);
298 // XXX: when this happens it used to hang ...
305 isc_qout(isc_session_t *sp, pduq_t *pq)
311 if(pq->len == 0 && (error = i_prepPDU(sp, pq)))
314 if(pq->pdu.ipdu.bhs.I)
315 i_nqueue_isnd(sp, pq);
317 if(pq->pdu.ipdu.data_out.opcode == ISCSI_WRITE_DATA)
318 i_nqueue_wsnd(sp, pq);
320 i_nqueue_csnd(sp, pq);
322 sdebug(5, "enqued: pq=%p", pq);
324 mtx_lock(&sp->io_mtx);
325 sp->flags |= ISC_OQNOTEMPTY;
326 if(sp->flags & ISC_OWAITING)
328 mtx_unlock(&sp->io_mtx);
333 | called when a fullPhase is restarted
336 ism_restart(isc_session_t *sp)
340 sdebug(2, "restart ...");
341 lastcmd = iscsi_requeue(sp);
343 if(lastcmd != sp->sn.cmd) {
344 sdebug(1, "resetting CmdSN to=%d (from %d)", lastcmd, sp->sn.cmd);
345 sp->sn.cmd = lastcmd;
348 mtx_lock(&sp->io_mtx);
349 if(sp->flags & ISC_OWAITING) {
352 mtx_unlock(&sp->io_mtx);
354 sdebug(2, "restarted sn.cmd=0x%x lastcmd=0x%x", sp->sn.cmd, lastcmd);
358 ism_recv(isc_session_t *sp, pduq_t *pq)
365 bhs = &pq->pdu.ipdu.bhs;
366 statSN = ntohl(bhs->OpcodeSpecificFields[1]);
369 if(sp->sn.expCmd != sn->cmd) {
370 sdebug(1, "we lost something ... exp=0x%x cmd=0x%x",
371 sn->expCmd, sn->cmd);
374 sdebug(5, "opcode=0x%x itt=0x%x stat#0x%x maxcmd=0x%0x",
375 bhs->opcode, ntohl(bhs->itt), statSN, sp->sn.maxCmd);
377 switch(bhs->opcode) {
378 case ISCSI_READ_DATA: {
379 data_in_t *cmd = &pq->pdu.ipdu.data_in;
386 if(statSN > (sp->sn.stat + 1)) {
387 sdebug(1, "we lost some rec=0x%x exp=0x%x",
388 statSN, sp->sn.stat);
389 // XXX: must do some error recovery here.
391 sp->sn.stat = statSN;
394 switch(bhs->opcode) {
395 case ISCSI_LOGIN_RSP:
397 case ISCSI_LOGOUT_RSP:
398 i_nqueue_rsp(sp, pq);
400 sdebug(3, "wakeup rsp");
403 case ISCSI_NOP_IN: _nop_in(sp, pq); break;
404 case ISCSI_SCSI_RSP: _scsi_rsp(sp, pq); break;
405 case ISCSI_READ_DATA: _read_data(sp, pq); break;
406 case ISCSI_R2T: _r2t(sp, pq); break;
407 case ISCSI_REJECT: _reject(sp, pq); break;
408 case ISCSI_ASYNC: _async(sp, pq); break;
412 sdebug(1, "opcode=0x%x itt=0x%x not implemented yet",
413 bhs->opcode, ntohl(bhs->itt));
419 | go through the out queues looking for work
420 | if either nothing to do, or window is closed
424 proc_out(isc_session_t *sp)
433 while(sp->flags & ISC_LINK_UP) {
437 | check if there is outstanding work in:
438 | 1- the Immediate queue
440 | 3- the cmd queue, only if the command window allows it.
442 which = BIT(0) | BIT(1);
443 if(SNA_GT(sn->cmd, sn->maxCmd) == 0) // if(sn->maxCmd - sn->smc + 1) > 0
446 sdebug(4, "which=%d sn->maxCmd=%d sn->cmd=%d", which, sn->maxCmd, sn->cmd);
448 if((pq = i_dqueue_snd(sp, which)) == NULL)
450 sdebug(4, "pq=%p", pq);
454 switch(bhs->opcode) {
457 bhs->itt = htonl(sn->itt);
459 case ISCSI_LOGIN_CMD:
461 case ISCSI_LOGOUT_CMD:
465 bhs->CmdSN = htonl(sn->cmd);
469 case ISCSI_WRITE_DATA:
470 bhs->ExpStSN = htonl(sn->stat + 1);
474 // XXX: can this happen?
475 xdebug("bad opcode=0x%x sn(cmd=0x%x expCmd=0x%x maxCmd=0x%x expStat=0x%x itt=0x%x)",
477 sn->cmd, sn->expCmd, sn->maxCmd, sn->expStat, sn->itt);
481 sdebug(4, "opcode=0x%x sn(cmd=0x%x expCmd=0x%x maxCmd=0x%x expStat=0x%x itt=0x%x)",
483 sn->cmd, sn->expCmd, sn->maxCmd, sn->expStat, sn->itt);
485 if(bhs->opcode != ISCSI_NOP_OUT)
487 | enqued till ack is received
488 | note: sosend(...) does not mean the packet left
489 | the host so that freeing resources has to wait
491 i_nqueue_hld(sp, pq);
493 error = isc_sendPDU(sp, pq);
494 if(bhs->opcode == ISCSI_NOP_OUT)
495 pdu_free(sp->isc, pq);
497 xdebug("error=%d opcode=0x%x ccb=%p itt=%x",
498 error, bhs->opcode, pq->ccb, ntohl(bhs->itt));
499 i_remove_hld(sp, pq);
502 sp->flags &= ~ISC_LINK_UP;
506 i_rqueue_pdu(sp, pq);
511 xdebug("back to cam");
512 pq->ccb->ccb_h.status |= CAM_REQUEUE_REQ; // some better error?
513 XPT_DONE(sp, pq->ccb);
514 pdu_free(sp->isc, pq);
517 xdebug("we lost it!");
525 | survives link breakdowns.
530 isc_session_t *sp = (isc_session_t *)vp;
535 sp->flags |= ISC_SM_RUNNING;
536 sdebug(3, "started sp->flags=%x", sp->flags);
538 if((sp->flags & ISC_HOLD) == 0) {
539 error = proc_out(sp);
541 sdebug(3, "error=%d", error);
544 mtx_lock(&sp->io_mtx);
545 if((sp->flags & ISC_LINK_UP) == 0) {
546 sdebug(3, "ISC_LINK_UP==0, sp->flags=%x ", sp->flags);
548 sdebug(3, "so_state=%x", sp->soc->so_state);
552 if(!(sp->flags & ISC_OQNOTEMPTY)) {
553 sp->flags |= ISC_OWAITING;
554 if(msleep(&sp->flags, &sp->io_mtx, PRIBIO, "isc_proc", hz*30) == EWOULDBLOCK) {
555 if(sp->flags & ISC_CON_RUNNING)
558 sp->flags &= ~ISC_OWAITING;
560 sp->flags &= ~ISC_OQNOTEMPTY;
561 mtx_unlock(&sp->io_mtx);
562 } while(sp->flags & ISC_SM_RUN);
564 sp->flags &= ~ISC_SM_RUNNING;
565 sdebug(3, "dropped ISC_SM_RUNNING");
568 wakeup(sp); // XXX: do we need this one?
570 #if __FreeBSD_version >= 700000
571 destroy_dev(sp->dev);
574 debug(3, "terminated sp=%p sp->sid=%d", sp, sp->sid);
576 #if __FreeBSD_version >= 800000
585 isc_dump_options(SYSCTL_HANDLER_ARGS)
591 sp = (isc_session_t *)arg1;
593 sprintf(bp, "targetname='%s'", sp->opt.targetName);
595 sprintf(bp, " targetname='%s'", sp->opt.targetAddress);
596 error = SYSCTL_OUT(req, buf, strlen(buf));
602 isc_dump_stats(SYSCTL_HANDLER_ARGS)
605 struct isc_softc *sc;
609 sp = (isc_session_t *)arg1;
614 snprintf(bp, n, "recv=%d sent=%d", sp->stats.nrecv, sp->stats.nsent);
617 snprintf(bp, n, " flags=0x%08x pdus-alloc=%d pdus-max=%d",
618 sp->flags, sc->npdu_alloc, sc->npdu_max);
621 snprintf(bp, n, " cws=%d cmd=%x exp=%x max=%x stat=%x itt=%x",
622 sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
623 error = SYSCTL_OUT(req, buf, strlen(buf));
628 isc_sysctl_targetName(SYSCTL_HANDLER_ARGS)
634 snprintf(buf, sizeof(buf), "%s", *cp);
635 error = SYSCTL_OUT(req, buf, strlen(buf));
640 isc_sysctl_targetAddress(SYSCTL_HANDLER_ARGS)
646 snprintf(buf, sizeof(buf), "%s", *cp);
647 error = SYSCTL_OUT(req, buf, strlen(buf));
652 isc_add_sysctls(isc_session_t *sp)
655 sdebug(6, "sid=%d %s", sp->sid, sp->dev->si_name);
657 sysctl_ctx_init(&sp->clist);
658 sp->oid = SYSCTL_ADD_NODE(&sp->clist,
659 SYSCTL_CHILDREN(sp->isc->oid),
661 sp->dev->si_name+5, // iscsi0
665 SYSCTL_ADD_PROC(&sp->clist,
666 SYSCTL_CHILDREN(sp->oid),
670 (void *)&sp->opt.targetName, 0,
671 isc_sysctl_targetName, "A", "target name");
673 SYSCTL_ADD_PROC(&sp->clist,
674 SYSCTL_CHILDREN(sp->oid),
678 (void *)&sp->opt.targetAddress, 0,
679 isc_sysctl_targetAddress, "A", "target address");
681 SYSCTL_ADD_PROC(&sp->clist,
682 SYSCTL_CHILDREN(sp->oid),
687 isc_dump_stats, "A", "statistics");
689 SYSCTL_ADD_INT(&sp->clist,
690 SYSCTL_CHILDREN(sp->oid),
694 &sp->douio, 0, "enable uio on read");
698 ism_stop(isc_session_t *sp)
700 struct isc_softc *sc = sp->isc;
704 sdebug(2, "terminating");
706 | first stop the receiver
708 isc_stop_receiver(sp);
710 | now stop the xmitter
713 sp->flags &= ~ISC_SM_RUN;
714 while(n-- && (sp->flags & ISC_SM_RUNNING)) {
715 sdebug(2, "n=%d", n);
717 tsleep(sp, PRIBIO, "-", 5*hz);
719 sdebug(2, "final n=%d", n);
720 sp->flags &= ~ISC_FFPHASE;
724 (void)i_pdu_flush(sp);
728 sx_xlock(&sc->unit_sx);
729 free_unr(sc->unit, sp->sid);
730 sx_xunlock(&sc->unit_sx);
732 mtx_lock(&sc->isc_mtx);
733 TAILQ_REMOVE(&sc->isc_sess, sp, sp_link);
735 mtx_unlock(&sc->isc_mtx);
737 #if __FreeBSD_version < 700000
738 destroy_dev(sp->dev);
741 mtx_destroy(&sp->rsp_mtx);
742 mtx_destroy(&sp->rsv_mtx);
743 mtx_destroy(&sp->hld_mtx);
744 mtx_destroy(&sp->snd_mtx);
745 mtx_destroy(&sp->io_mtx);
749 if(sysctl_ctx_free(&sp->clist))
750 xdebug("sysctl_ctx_free failed");
756 ism_start(isc_session_t *sp)
760 | now is a good time to do some initialization
762 TAILQ_INIT(&sp->rsp);
763 TAILQ_INIT(&sp->rsv);
764 TAILQ_INIT(&sp->csnd);
765 TAILQ_INIT(&sp->isnd);
766 TAILQ_INIT(&sp->wsnd);
767 TAILQ_INIT(&sp->hld);
769 mtx_init(&sp->rsv_mtx, "iscsi-rsv", NULL, MTX_DEF);
770 mtx_init(&sp->rsp_mtx, "iscsi-rsp", NULL, MTX_DEF);
771 mtx_init(&sp->snd_mtx, "iscsi-snd", NULL, MTX_DEF);
772 mtx_init(&sp->hld_mtx, "iscsi-hld", NULL, MTX_DEF);
773 mtx_init(&sp->io_mtx, "iscsi-io", NULL, MTX_DEF);
777 sp->flags |= ISC_SM_RUN;
779 debug(4, "starting ism_proc: sp->sid=%d", sp->sid);
781 #if __FreeBSD_version >= 800000
782 return kproc_create(ism_out, sp, &sp->stp, 0, 0, "isc_out %d", sp->sid);
784 return kthread_create(ism_out, sp, &sp->stp, 0, 0, "isc_out %d", sp->sid);