2 * Copyright (c) 2005-2008 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
26 * $FreeBSD: src/sys/dev/iscsi/initiator/iscsi.c,v 1.4 2008/11/25 07:17:11 scottl Exp $
30 | $Id: iscsi.c,v 1.35 2007/04/22 08:58:29 danny Exp danny $
33 #include "opt_iscsi_initiator.h"
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.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>
55 #include <sys/syslog.h>
56 #include <sys/eventhandler.h>
57 #include <sys/mutex2.h>
58 #include <sys/devfs.h>
61 #include <bus/cam/cam.h>
62 #include <dev/disk/iscsi/initiator/iscsi.h>
63 #include <dev/disk/iscsi/initiator/iscsivar.h>
65 static char *iscsi_driver_version = "2.1.0";
67 static struct isc_softc isc;
69 MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI driver");
71 struct objcache_malloc_args iscsi_malloc_args = {
72 sizeof(pduq_t), M_ISCSI
75 #ifdef ISCSI_INITIATOR_DEBUG
76 int iscsi_debug = ISCSI_INITIATOR_DEBUG;
77 SYSCTL_INT(_debug, OID_AUTO, iscsi_initiator, CTLFLAG_RW, &iscsi_debug, 0, "iSCSI driver debug flag");
79 struct lock iscsi_dbg_lock;
83 static char isid[6+1] = {
93 static int i_create_session(struct cdev *dev, int *ndev);
95 static int i_ping(struct cdev *dev);
96 static int i_send(struct cdev *dev, caddr_t arg, struct thread *td);
97 static int i_recv(struct cdev *dev, caddr_t arg, struct thread *td);
98 static int i_setsoc(isc_session_t *sp, int fd, struct thread *td);
100 static void free_pdus(struct isc_softc *sc);
102 static d_open_t iscsi_open;
103 static d_close_t iscsi_close;
104 static d_ioctl_t iscsi_ioctl;
105 #ifdef ISCSI_INITIATOR_DEBUG
106 static d_read_t iscsi_read;
109 static struct dev_ops iscsi_ops = {
110 .head = { "iscsi", ISCSI_CDEV_MAJOR, D_DISK},
111 .d_open = iscsi_open,
112 .d_close = iscsi_close,
113 .d_ioctl = iscsi_ioctl,
114 #ifdef ISCSI_INITIATOR_DEBUG
115 .d_read = iscsi_read,
120 iscsi_open(struct dev_open_args *ap)
122 cdev_t dev = ap->a_head.a_dev;
126 debug(7, "dev=%d", dev->si_uminor);
128 if(minor(dev) > MAX_SESSIONS) {
133 /* Make sure the device is passed */
134 if (dev->si_drv1 == NULL)
135 dev->si_drv1 = (struct isc *)isc.dev->si_drv1;
137 if(minor(dev) == MAX_SESSIONS) {
139 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
141 // this should be in iscsi_start
142 if(sc->cam_sim == NULL)
150 iscsi_close(struct dev_close_args *ap)
152 cdev_t dev = ap->a_head.a_dev;
153 int flag = ap->a_fflag;
158 debug(3, "flag=%x", flag);
160 if(minor(dev) == MAX_SESSIONS) {
163 sp = (isc_session_t *)dev->si_drv2;
165 sdebug(2, "session=%d flags=%x", minor(dev), sp->flags );
167 | if still in full phase, this probably means
168 | that something went realy bad.
169 | it could be a result from 'shutdown', in which case
170 | we will ignore it (so buffers can be flushed).
171 | the problem is that there is no way of differentiating
172 | between a shutdown procedure and 'iscontrol' dying.
174 if(sp->flags & ISC_FFPHASE)
175 // delay in case this is a shutdown.
176 tsleep(sp, 0, "isc-cls", 60*hz);
184 iscsi_ioctl(struct dev_ioctl_args *ap)
187 cdev_t dev = ap->a_head.a_dev;
188 caddr_t arg = ap->a_data;
193 sc = (struct isc *)dev->si_drv1;
197 if(minor(dev) == MAX_SESSIONS) {
199 | non Session commands
206 error = i_create_session(dev, (int *)arg);
212 error = ENXIO; // XXX:
216 sp = (isc_session_t *)dev->si_drv2;
223 sdebug(6, "dev=%d cmd=%d", minor(dev), (int)(ap->a_cmd & 0xff));
227 error = i_setsoc(sp, *(u_int *)arg, curthread);
231 opt = (isc_opt_t *)arg;
232 error = i_setopt(sp, opt);
236 error = i_send(dev, arg, curthread);
240 error = i_recv(dev, arg, curthread);
248 error = sp->soc == NULL? ENOTCONN: ism_fullfeature(dev, 1);
250 sp->proc = curthread->td_proc;
251 SYSCTL_ADD_UINT(&sp->clist,
252 SYSCTL_CHILDREN(sp->oid),
256 &sp->proc->p_pid, sizeof(pid_t), "control process id");
261 error = sp->soc == NULL? ENOTCONN: ism_fullfeature(dev, 2);
265 error = ism_fullfeature(dev, 0);
269 int sig = *(int *)arg;
271 if(sig < 0 || sig > _SIG_MAXSIG)
279 iscsi_cam_t *cp = (iscsi_cam_t *)arg;
281 error = ic_getCamVals(sp, cp);
293 iscsi_read(struct dev_read_args *ra)
295 #ifdef ISCSI_INITIATOR_DEBUG
296 struct isc_softc *sc;
297 cdev_t dev = ra->a_head.a_dev;
298 struct uio *uio = ra->a_uio;
303 sc = (struct isc_softc *)dev->si_drv1;
304 sp = (isc_session_t *)dev->si_drv2;
306 if(minor(dev) == MAX_SESSIONS) {
307 ksprintf(buf, "/----- Session ------/\n");
308 uiomove(buf, strlen(buf), uio);
311 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
312 if(uio->uio_resid == 0)
314 ksprintf(buf, "%03d] '%s' '%s'\n", i++, sp->opt.targetAddress, sp->opt.targetName);
315 uiomove(buf, strlen(buf), uio);
317 ksprintf(buf, "%d/%d /---- free -----/\n", sc->npdu_alloc, sc->npdu_max);
319 uiomove(buf, strlen(buf), uio);
320 TAILQ_FOREACH(pq, &sc->freepdu, pq_link) {
321 if(uio->uio_resid == 0)
323 ksprintf(buf, "%03d] %06x\n", i++, ntohl(pq->pdu.ipdu.bhs.itt));
324 uiomove(buf, strlen(buf), uio);
329 struct socket *so = sp->soc;
330 #define pukeit(i, pq) do {\
331 ksprintf(buf, "%03d] %06x %02x %x %ld\n",\
332 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
333 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
334 (long)pq->ts.tv_sec);\
337 ksprintf(buf, "%d/%d /---- hld -----/\n", sp->stats.nhld, sp->stats.max_hld);
338 uiomove(buf, strlen(buf), uio);
339 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
340 if(uio->uio_resid == 0)
343 uiomove(buf, strlen(buf), uio);
345 ksprintf(buf, "%d/%d /---- rsp -----/\n", sp->stats.nrsp, sp->stats.max_rsp);
346 uiomove(buf, strlen(buf), uio);
348 TAILQ_FOREACH(pq, &sp->rsp, pq_link) {
349 if(uio->uio_resid == 0)
352 uiomove(buf, strlen(buf), uio);
354 ksprintf(buf, "%d/%d /---- csnd -----/\n", sp->stats.ncsnd, sp->stats.max_csnd);
356 uiomove(buf, strlen(buf), uio);
357 TAILQ_FOREACH(pq, &sp->csnd, pq_link) {
358 if(uio->uio_resid == 0)
361 uiomove(buf, strlen(buf), uio);
363 ksprintf(buf, "%d/%d /---- wsnd -----/\n", sp->stats.nwsnd, sp->stats.max_wsnd);
365 uiomove(buf, strlen(buf), uio);
366 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) {
367 if(uio->uio_resid == 0)
370 uiomove(buf, strlen(buf), uio);
372 ksprintf(buf, "%d/%d /---- isnd -----/\n", sp->stats.nisnd, sp->stats.max_isnd);
374 uiomove(buf, strlen(buf), uio);
375 TAILQ_FOREACH(pq, &sp->isnd, pq_link) {
376 if(uio->uio_resid == 0)
379 uiomove(buf, strlen(buf), uio);
382 ksprintf(buf, "/---- Stats ---/\n");
383 uiomove(buf, strlen(buf), uio);
385 ksprintf(buf, "recv=%d sent=%d\n", sp->stats.nrecv, sp->stats.nsent);
386 uiomove(buf, strlen(buf), uio);
388 ksprintf(buf, "flags=%x pdus: alloc=%d max=%d\n",
389 sp->flags, sc->npdu_alloc, sc->npdu_max);
390 uiomove(buf, strlen(buf), uio);
392 ksprintf(buf, "cws=%d last cmd=%x exp=%x max=%x stat=%x itt=%x\n",
393 sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
394 uiomove(buf, strlen(buf), uio);
397 ksprintf(buf, "/---- socket -----/\nso_state=%x\n", so->so_state);
398 uiomove(buf, strlen(buf), uio);
406 i_ping(struct cdev *dev)
414 i_setsoc(isc_session_t *sp, int fd, struct thread *td)
420 isc_stop_receiver(sp);
428 if ((error = holdsock(td->td_proc->p_fd, fd, &fp)) == 0) {
429 sp->soc = fp->f_data;
431 isc_start_receiver(sp);
438 i_send(struct cdev *dev, caddr_t arg, struct thread *td)
440 isc_session_t *sp = (isc_session_t *)dev->si_drv2;
441 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
452 if((pq = pdu_alloc(sc, M_NOWAIT)) == NULL)
455 pq->pdu = *(pdu_t *)arg;
457 if((error = i_prepPDU(sp, pq)) != 0)
460 sdebug(3, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
462 pq->buf = bp = kmalloc(pq->len - sizeof(union ipdu_u), M_ISCSI, M_NOWAIT);
463 if(pq->buf == NULL) {
470 error = copyin(pp->ahs, bp, n);
472 sdebug(3, "copyin ahs: error=%d", error);
475 pp->ahs = (ahs_t *)bp;
480 error = copyin(pp->ds, bp, n);
482 sdebug(3, "copyin ds: error=%d", error);
493 error = isc_qout(sp, pq);
496 wakeup(&sp->flags); // XXX: to 'push' proc_out ...
506 | NOTE: must calculate digest if requiered.
509 i_recv(struct cdev *dev, caddr_t arg, struct thread *td)
511 isc_session_t *sp = (isc_session_t *)dev->si_drv2;
515 int error, mustfree, cnt;
516 size_t need, have, n;
526 cnt = 6; // XXX: maybe the user can request a time out?
527 iscsi_lock_ex(&sp->rsp_mtx);
528 while((pq = TAILQ_FIRST(&sp->rsp)) == NULL) {
529 issleep(&sp->rsp, &sp->rsp_mtx, 0, "isc_rsp", hz*10);
530 if(cnt-- == 0) break; // XXX: for now, needs work
535 TAILQ_REMOVE(&sp->rsp, pq, pq_link);
537 iscsi_unlock_ex(&sp->rsp_mtx);
539 sdebug(4, "cnt=%d", cnt);
543 sdebug(3, "error=%d sp->flags=%x ", error, sp->flags);
571 if(len > pq->mp->m_len) {
573 bp = kmalloc(len, M_ISCSI, M_INTWAIT);
574 sdebug(4, "need mbufcopy: %d", len);
575 i_mbufcopy(pq->mp, bp, len);
578 bp = mtod(pq->mp, caddr_t);
584 n = MIN(up->ahs_size, need);
585 error = copyout(bp, (caddr_t)up->ahs, n);
589 if(!error && pp->ds_len) {
593 if((have = up->ds_size) == 0) {
594 have = up->ahs_size - n;
595 up->ds = (caddr_t)up->ahs + n;
598 error = copyout(bp, (caddr_t)up->ds, n);
606 sdebug(6, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
608 pdu_free(sp->isc, pq);
614 i_create_session(struct cdev *dev, int *ndev)
616 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
621 sp = (isc_session_t *)kmalloc(sizeof *sp, M_ISCSI, M_WAITOK | M_ZERO);
624 lockmgr(&sc->lock, LK_EXCLUSIVE);
626 | search for the lowest unused sid
628 for(n = 0; n < MAX_SESSIONS; n++)
629 if(sc->sessions[n] == NULL)
631 if(n == MAX_SESSIONS) {
632 lockmgr(&sc->lock, LK_RELEASE);
636 TAILQ_INSERT_TAIL(&sc->isc_sess, sp, sp_link);
638 lockmgr(&sc->lock, LK_RELEASE);
640 sc->sessions[n] = sp;
641 debug(8, "n is %d", n);
642 sp->dev = make_dev(&iscsi_ops, n, UID_ROOT, GID_WHEEL, 0600, "iscsi%d", n);
644 reference_dev(sp->dev);
645 udev_dict_set_cstr(sp->dev, "subsystem", "disk");
646 udev_dict_set_cstr(sp->dev, "disk-type", "network");
650 sp->dev->si_drv1 = sc;
651 sp->dev->si_drv2 = sp;
653 sp->opt.maxRecvDataSegmentLength = 8192;
654 sp->opt.maxXmitDataSegmentLength = 8192;
656 sp->opt.maxBurstLength = 65536; // 64k
658 sdebug(2, "sessionID=%d sp=%p", n, sp);
659 error = ism_start(sp);
666 iscsi_counters(isc_session_t *sp)
671 #define _puke(i, pq) do {\
672 debug(2, "%03d] %06x %02x %x %ld %jd %x\n",\
673 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
674 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
675 (long)pq->ts.sec, pq->ts.frac, pq->flags);\
679 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
683 TAILQ_FOREACH(pq, &sp->rsp, pq_link) r++;
684 TAILQ_FOREACH(pq, &sp->csnd, pq_link) s++;
685 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) s++;
686 TAILQ_FOREACH(pq, &sp->isnd, pq_link) s++;
687 debug(2, "hld=%d rsp=%d snd=%d", h, r, s);
692 iscsi_shutdown(void *v)
694 struct isc_softc *sc = (struct isc_softc *)v;
700 xdebug("sc is NULL!");
704 debug(2, "sc->eh is NULL");
706 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->eh);
707 debug(2, "done n=%d", sc->nsess);
710 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
711 debug(2, "%2d] sp->flags=0x%08x", n, sp->flags);
718 init_pdus(struct isc_softc *sc)
722 sc->pdu_zone = objcache_create("pdu", 0, 0,
724 objcache_malloc_alloc,
725 objcache_malloc_free,
728 if(sc->pdu_zone == NULL) {
729 kprintf("iscsi_initiator: objcache_create failed");
732 TAILQ_INIT(&sc->freepdu);
738 free_pdus(struct isc_softc *sc)
744 if(sc->pdu_zone != NULL) {
745 TAILQ_FOREACH(pq, &sc->freepdu, pq_link) {
746 TAILQ_REMOVE(&sc->freepdu, pq, pq_link);
747 objcache_put(sc->pdu_zone, pq);
749 objcache_destroy(sc->pdu_zone);
757 struct isc_softc *sc = &isc;
761 memset(sc, 0, sizeof(struct isc_softc));
763 sc->dev = make_dev(&iscsi_ops, MAX_SESSIONS, UID_ROOT, GID_WHEEL, 0600, "iscsi");
766 sc->dev->si_drv1 = sc;
768 reference_dev(sc->dev);
770 TAILQ_INIT(&sc->isc_sess);
771 if(init_pdus(sc) != 0)
772 xdebug("pdu zone init failed!"); // XXX: should cause terminal failure ...
774 lockinit(&sc->lock, "iscsi", 0, LK_CANRECURSE);
775 lockinit(&sc->pdu_lock, "iscsi pdu pool", 0, LK_CANRECURSE);
778 // XXX: this will cause a panic if the
779 // module is loaded too early
786 #ifdef DO_EVENTHANDLER
787 if((sc->eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, iscsi_shutdown,
788 sc, SHUTDOWN_PRI_DEFAULT-1)) == NULL)
789 xdebug("shutdown event registration failed\n");
794 sysctl_ctx_init(&sc->clist);
795 sc->oid = SYSCTL_ADD_NODE(&sc->clist,
796 SYSCTL_STATIC_CHILDREN(_net),
803 SYSCTL_ADD_STRING(&sc->clist,
804 SYSCTL_CHILDREN(sc->oid),
808 iscsi_driver_version,
810 "iscsi driver version");
812 SYSCTL_ADD_STRING(&sc->clist,
813 SYSCTL_CHILDREN(sc->oid),
819 "initiator part of the Session Identifier");
821 SYSCTL_ADD_INT(&sc->clist,
822 SYSCTL_CHILDREN(sc->oid),
828 "number of active session");
830 kprintf("iscsi: version %s\n", iscsi_driver_version);
835 | unload SHOULD fail if there is activity
836 | activity: there is/are active session/s
841 struct isc_softc *sc = &isc;
842 isc_session_t *sp, *sp_tmp;
847 | go through all the sessions
848 | Note: close should have done this ...
850 TAILQ_FOREACH_MUTABLE(sp, &sc->isc_sess, sp_link, sp_tmp) {
851 //XXX: check for activity ...
854 if(sc->cam_sim != NULL)
857 lockuninit(&sc->lock);
858 lockuninit(&sc->pdu_lock);
862 release_dev(sc->dev);
863 destroy_dev(sc->dev);
864 //dev_ops_remove(&sc->dev, -1, 0);
867 if(sysctl_ctx_free(&sc->clist))
868 xdebug("sysctl_ctx_free failed");
870 iscsi_shutdown(sc); // XXX: check EVENTHANDLER_ ...
874 iscsi_modevent(module_t mod, int what, void *arg)
896 moduledata_t iscsi_mod = {
898 (modeventhand_t) iscsi_modevent,
907 nfs_setup_diskless();
908 if (nfs_diskless_valid)
909 rootdevnames[0] = "nfs:";
911 kprintf("** iscsi_rootconf **\n");
914 SYSINIT(cpu_rootconf1, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, iscsi_rootconf, NULL)
917 DECLARE_MODULE(iscsi_initiator, iscsi_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
918 MODULE_DEPEND(iscsi_initiator, cam, 1, 1, 1);