2 * Copyright (c) 2005-2007 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
29 | $Id: iscsi.c,v 1.35 2007/04/22 08:58:29 danny Exp 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>
39 #include <sys/module.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/ctype.h>
45 #include <sys/errno.h>
46 #include <sys/sysctl.h>
49 #include <sys/socketvar.h>
50 #include <sys/socket.h>
51 #include <sys/protosw.h>
53 #include <sys/ioccom.h>
54 #include <sys/queue.h>
55 #include <sys/kthread.h>
57 #include <sys/syslog.h>
60 #include <dev/iscsi/initiator/iscsi.h>
61 #include <dev/iscsi/initiator/iscsivar.h>
63 static char *iscsi_driver_version = "2.0.99";
65 static struct isc_softc isc;
67 MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI driver");
69 #ifdef ISCSI_INITIATOR_DEBUG
70 int iscsi_debug = ISCSI_INITIATOR_DEBUG;
71 SYSCTL_INT(_debug, OID_AUTO, iscsi_initiator, CTLFLAG_RW, &iscsi_debug, 0,
72 "iSCSI driver debug flag");
74 struct mtx iscsi_dbg_mtx;
78 static char isid[6+1] = {
88 static int i_create_session(struct cdev *dev, int *ndev);
90 static int i_ping(struct cdev *dev);
91 static int i_send(struct cdev *dev, caddr_t arg, struct thread *td);
92 static int i_recv(struct cdev *dev, caddr_t arg, struct thread *td);
93 static int i_setsoc(isc_session_t *sp, int fd, struct thread *td);
95 static d_open_t iscsi_open;
96 static d_close_t iscsi_close;
97 static d_ioctl_t iscsi_ioctl;
98 #ifdef ISCSI_INITIATOR_DEBUG
99 static d_read_t iscsi_read;
102 static struct cdevsw iscsi_cdevsw = {
103 .d_version = D_VERSION,
104 .d_open = iscsi_open,
105 .d_close = iscsi_close,
106 .d_ioctl = iscsi_ioctl,
107 #ifdef ISCSI_INITIATOR_DEBUG
108 .d_read = iscsi_read,
114 iscsi_open(struct cdev *dev, int flags, int otype, struct thread *td)
118 debug(7, "dev=%d", minor(dev));
120 if(minor(dev) > MAX_SESSIONS) {
124 if(minor(dev) == MAX_SESSIONS) {
126 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
128 // this should be in iscsi_start
129 if(sc->cam_sim == NULL)
137 iscsi_close(struct cdev *dev, int flag, int otyp, struct thread *td)
144 debug(3, "flag=%x", flag);
146 sc = (struct isc *)dev->si_drv1;
147 if(minor(dev) == MAX_SESSIONS) {
150 sp = (isc_session_t *)dev->si_drv2;
152 sdebug(2, "session=%d flags=%x", minor(dev), sp->flags );
154 | if still in full phase, this probably means
155 | that something went realy bad.
156 | it could be a result from 'shutdown', in which case
157 | we will ignore it (so buffers can be flushed).
158 | the problem is that there is no way of differentiating
159 | between a shutdown procedure and 'iscontrol' dying.
161 if(sp->flags & ISC_FFPHASE)
162 // delay in case this is a shutdown.
163 tsleep(sp, PRIBIO, "isc-cls", 60*hz);
171 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
178 sc = (struct isc *)dev->si_drv1;
182 if(minor(dev) == MAX_SESSIONS) {
184 | non Session commands
191 error = i_create_session(dev, (int *)arg);
197 error = ENXIO; // XXX:
201 sp = (isc_session_t *)dev->si_drv2;
208 sdebug(6, "dev=%d cmd=%d", minor(dev), (int)(cmd & 0xff));
212 error = i_setsoc(sp, *(u_int *)arg, td);
216 opt = (isc_opt_t *)arg;
217 error = i_setopt(sp, opt);
221 error = i_send(dev, arg, td);
225 error = i_recv(dev, arg, td);
233 error = sp->soc == NULL? ENOTCONN: ism_fullfeature(dev, 1);
235 sp->proc = td->td_proc;
236 SYSCTL_ADD_UINT(&sp->clist,
237 SYSCTL_CHILDREN(sp->oid),
241 &sp->proc->p_pid, sizeof(pid_t), "control process id");
246 error = sp->soc == NULL? ENOTCONN: ism_fullfeature(dev, 2);
250 error = ism_fullfeature(dev, 0);
254 int sig = *(int *)arg;
256 if(sig < 0 || sig > _SIG_MAXSIG)
264 iscsi_cam_t *cp = (iscsi_cam_t *)arg;
266 error = ic_getCamVals(sp, cp);
278 iscsi_read(struct cdev *dev, struct uio *uio, int ioflag)
280 #ifdef ISCSI_INITIATOR_DEBUG
281 struct isc_softc *sc;
286 sc = (struct isc_softc *)dev->si_drv1;
287 sp = (isc_session_t *)dev->si_drv2;
288 if(minor(dev) == MAX_SESSIONS) {
289 sprintf(buf, "/----- Session ------/\n");
290 uiomove(buf, strlen(buf), uio);
293 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
294 if(uio->uio_resid == 0)
296 sprintf(buf, "%03d] '%s' '%s'\n", i++, sp->opt.targetAddress, sp->opt.targetName);
297 uiomove(buf, strlen(buf), uio);
302 struct socket *so = sp->soc;
303 #define pukeit(i, pq) do {\
304 sprintf(buf, "%03d] %06x %02x %x %ld %jd\n",\
305 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
306 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
307 (long)pq->ts.sec, pq->ts.frac);\
310 sprintf(buf, "%d/%d /---- hld -----/\n", sp->stats.nhld, sp->stats.max_hld);
311 uiomove(buf, strlen(buf), uio);
312 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
313 if(uio->uio_resid == 0)
316 uiomove(buf, strlen(buf), uio);
318 sprintf(buf, "%d/%d /---- rsp -----/\n", sp->stats.nrsp, sp->stats.max_rsp);
319 uiomove(buf, strlen(buf), uio);
321 TAILQ_FOREACH(pq, &sp->rsp, pq_link) {
322 if(uio->uio_resid == 0)
325 uiomove(buf, strlen(buf), uio);
327 sprintf(buf, "%d/%d /---- csnd -----/\n", sp->stats.ncsnd, sp->stats.max_csnd);
329 uiomove(buf, strlen(buf), uio);
330 TAILQ_FOREACH(pq, &sp->csnd, pq_link) {
331 if(uio->uio_resid == 0)
334 uiomove(buf, strlen(buf), uio);
336 sprintf(buf, "%d/%d /---- wsnd -----/\n", sp->stats.nwsnd, sp->stats.max_wsnd);
338 uiomove(buf, strlen(buf), uio);
339 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) {
340 if(uio->uio_resid == 0)
343 uiomove(buf, strlen(buf), uio);
345 sprintf(buf, "%d/%d /---- isnd -----/\n", sp->stats.nisnd, sp->stats.max_isnd);
347 uiomove(buf, strlen(buf), uio);
348 TAILQ_FOREACH(pq, &sp->isnd, pq_link) {
349 if(uio->uio_resid == 0)
352 uiomove(buf, strlen(buf), uio);
355 sprintf(buf, "/---- Stats ---/\n");
356 uiomove(buf, strlen(buf), uio);
358 sprintf(buf, "recv=%d sent=%d\n", sp->stats.nrecv, sp->stats.nsent);
359 uiomove(buf, strlen(buf), uio);
361 sprintf(buf, "flags=%x pdus: alloc=%d max=%d\n",
362 sp->flags, sc->npdu_alloc, sc->npdu_max);
363 uiomove(buf, strlen(buf), uio);
365 sprintf(buf, "cws=%d last cmd=%x exp=%x max=%x stat=%x itt=%x\n",
366 sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
367 uiomove(buf, strlen(buf), uio);
369 sprintf(buf, "/---- socket -----/\nso_count=%d so_state=%x\n", so->so_count, so->so_state);
370 uiomove(buf, strlen(buf), uio);
378 i_ping(struct cdev *dev)
386 i_setsoc(isc_session_t *sp, int fd, struct thread *td)
391 isc_stop_receiver(sp);
393 error = fget(td, fd, &sp->fp);
397 if((error = fgetsock(td, fd, &sp->soc, 0)) == 0) {
399 isc_start_receiver(sp);
410 i_send(struct cdev *dev, caddr_t arg, struct thread *td)
412 isc_session_t *sp = (isc_session_t *)dev->si_drv2;
413 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
424 if((pq = pdu_alloc(sc, 0)) == NULL)
428 pq->pdu = *(pdu_t *)arg;
429 if((error = i_prepPDU(sp, pq)) != 0)
432 sdebug(4, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
434 pq->buf = bp = malloc(pq->len - sizeof(union ipdu_u), M_ISCSI, M_WAITOK);
438 copyin(pp->ahs, bp, n);
439 pp->ahs = (ahs_t *)bp;
444 copyin(pp->ds, bp, n); // can fail ...
453 error = isc_qout(sp, pq);
459 | NOTE: must calculate digest if requiered.
462 i_recv(struct cdev *dev, caddr_t arg, struct thread *td)
464 isc_session_t *sp = (isc_session_t *)dev->si_drv2;
468 int error, mustfree, cnt;
469 size_t need, have, n;
478 cnt = 6; // XXX: maybe the user can request a time out?
479 mtx_lock(&sp->rsp_mtx);
480 while((pq = TAILQ_FIRST(&sp->rsp)) == NULL) {
481 msleep(&sp->rsp, &sp->rsp_mtx, PRIBIO, "isc_rsp", hz*10);
482 if(cnt-- == 0) break; // XXX: for now, needs work
486 TAILQ_REMOVE(&sp->rsp, pq, pq_link);
488 mtx_unlock(&sp->rsp_mtx);
490 sdebug(3, "cnt=%d", cnt);
494 sdebug(3, "error=%d sp->flags=%x ", error, sp->flags);
522 if(len > pq->mp->m_len) {
524 bp = malloc(len, M_ISCSI, M_WAITOK);
525 sdebug(4, "need mbufcopy: %d", len);
526 i_mbufcopy(pq->mp, bp, len);
529 bp = mtod(pq->mp, caddr_t);
535 n = MIN(up->ahs_size, need);
536 error = copyout(bp, (caddr_t)up->ahs, n);
540 if(!error && pp->ds_len) {
544 if((have = up->ds_size) == 0) {
545 have = up->ahs_size - n;
546 up->ds = (caddr_t)up->ahs + n;
549 error = copyout(bp, (caddr_t)up->ds, n);
557 sdebug(6, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
559 pdu_free(sp->isc, pq);
565 i_create_session(struct cdev *dev, int *ndev)
567 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
572 sp = (isc_session_t *)malloc(sizeof *sp, M_ISCSI, M_WAITOK | M_ZERO);
577 | search for the lowest unused sid
579 for(n = 0; n < MAX_SESSIONS; n++)
580 if(sc->sessions[n] == NULL)
582 if(n == MAX_SESSIONS) {
583 mtx_unlock(&sc->mtx);
587 TAILQ_INSERT_TAIL(&sc->isc_sess, sp, sp_link);
589 mtx_unlock(&sc->mtx);
591 sc->sessions[n] = sp;
592 sp->dev = make_dev(&iscsi_cdevsw, n, UID_ROOT, GID_WHEEL, 0600, "iscsi%d", n);
595 sp->dev->si_drv1 = sc;
596 sp->dev->si_drv2 = sp;
598 sp->opt.maxRecvDataSegmentLength = 8192;
599 sp->opt.maxXmitDataSegmentLength = 8192;
601 sp->opt.maxBurstLength = 65536; // 64k
603 sdebug(2, "sessionID=%d", n);
604 error = ism_start(sp);
606 sdebug(2, "error=%d", error);
613 iscsi_counters(isc_session_t *sp)
618 #define _puke(i, pq) do {\
619 debug(2, "%03d] %06x %02x %x %ld %jd %x\n",\
620 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
621 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
622 (long)pq->ts.sec, pq->ts.frac, pq->flags);\
626 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
630 TAILQ_FOREACH(pq, &sp->rsp, pq_link) r++;
631 TAILQ_FOREACH(pq, &sp->csnd, pq_link) s++;
632 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) s++;
633 TAILQ_FOREACH(pq, &sp->isnd, pq_link) s++;
634 debug(2, "hld=%d rsp=%d snd=%d", h, r, s);
639 iscsi_shutdown(void *v)
641 struct isc_softc *sc = (struct isc_softc *)v;
647 xdebug("sc is NULL!");
651 debug(2, "sc->eh is NULL!");
653 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->eh);
654 debug(2, "done n=%d", sc->nsess);
657 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
658 debug(2, "%2d] sp->flags=0x%08x", n, sp->flags);
666 struct isc_softc *sc = &isc;
670 memset(sc, 0, sizeof(struct isc_softc));
672 sc->dev = make_dev(&iscsi_cdevsw, MAX_SESSIONS, UID_ROOT, GID_WHEEL, 0600, "iscsi");
673 sc->dev->si_drv1 = sc;
675 TAILQ_INIT(&sc->isc_sess);
677 sc->pdu_zone = uma_zcreate("pdu", sizeof(pduq_t),
678 NULL, NULL, NULL, NULL,
680 if(sc->pdu_zone == NULL) {
681 printf("iscsi_initiator: uma_zcreate failed");
682 // XXX: and now what?
684 uma_zone_set_max(sc->pdu_zone, MAX_PDUS*2+1);
685 mtx_init(&sc->mtx, "iscsi", NULL, MTX_DEF);
686 mtx_init(&sc->pdu_mtx, "iscsi pdu pool", NULL, MTX_DEF);
689 // XXX: this will cause a panic if the
690 // module is loaded too early
696 if((sc->eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, iscsi_shutdown,
697 sc, SHUTDOWN_PRI_DEFAULT-1)) == NULL)
698 xdebug("shutdown event registration failed\n");
702 sysctl_ctx_init(&sc->clist);
703 sc->oid = SYSCTL_ADD_NODE(&sc->clist,
704 SYSCTL_STATIC_CHILDREN(_net),
711 SYSCTL_ADD_STRING(&sc->clist,
712 SYSCTL_CHILDREN(sc->oid),
716 iscsi_driver_version,
718 "iscsi driver version");
720 SYSCTL_ADD_STRING(&sc->clist,
721 SYSCTL_CHILDREN(sc->oid),
727 "initiator part of the Session Identifier");
729 SYSCTL_ADD_INT(&sc->clist,
730 SYSCTL_CHILDREN(sc->oid),
736 "number of active session");
741 | unload SHOULD fail if there is activity
742 | activity: there is/are active session/s
747 struct isc_softc *sc = &isc;
748 isc_session_t *sp, *sp_tmp;
753 | go through all the sessions
754 | Note: close should have done this ...
756 TAILQ_FOREACH_SAFE(sp, &sc->isc_sess, sp_link, sp_tmp) {
757 //XXX: check for activity ...
760 if(sc->cam_sim != NULL)
763 mtx_destroy(&sc->mtx);
764 mtx_destroy(&sc->pdu_mtx);
765 uma_zdestroy(sc->pdu_zone);
768 destroy_dev(sc->dev);
770 if(sysctl_ctx_free(&sc->clist))
771 xdebug("sysctl_ctx_free failed");
775 iscsi_modevent(module_t mod, int what, void *arg)
787 xdebug("iscsi module busy(nsess=%d), cannot unload", isc.nsess);
788 log(LOG_ERR, "iscsi module busy, cannot unload");
804 moduledata_t iscsi_mod = {
806 (modeventhand_t) iscsi_modevent,
810 DECLARE_MODULE(iscsi, iscsi_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
811 MODULE_DEPEND(iscsi, cam, 1, 1, 1);