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/mutex.h>
58 #include <sys/mutex2.h>
59 #include <vfs/devfs/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)
153 cdev_t dev = ap->a_head.a_dev;
154 int flag = ap->a_fflag;
159 debug(3, "flag=%x", flag);
161 sc = (struct isc *)dev->si_drv1;
162 if(minor(dev) == MAX_SESSIONS) {
165 sp = (isc_session_t *)dev->si_drv2;
167 sdebug(2, "session=%d flags=%x", minor(dev), sp->flags );
169 | if still in full phase, this probably means
170 | that something went realy bad.
171 | it could be a result from 'shutdown', in which case
172 | we will ignore it (so buffers can be flushed).
173 | the problem is that there is no way of differentiating
174 | between a shutdown procedure and 'iscontrol' dying.
176 if(sp->flags & ISC_FFPHASE)
177 // delay in case this is a shutdown.
178 tsleep(sp, 0, "isc-cls", 60*hz);
186 iscsi_ioctl(struct dev_ioctl_args *ap)
189 cdev_t dev = ap->a_head.a_dev;
190 caddr_t arg = ap->a_data;
195 sc = (struct isc *)dev->si_drv1;
199 if(minor(dev) == MAX_SESSIONS) {
201 | non Session commands
208 error = i_create_session(dev, (int *)arg);
214 error = ENXIO; // XXX:
218 sp = (isc_session_t *)dev->si_drv2;
225 sdebug(6, "dev=%d cmd=%d", minor(dev), (int)(ap->a_cmd & 0xff));
229 error = i_setsoc(sp, *(u_int *)arg, curthread);
233 opt = (isc_opt_t *)arg;
234 error = i_setopt(sp, opt);
238 error = i_send(dev, arg, curthread);
242 error = i_recv(dev, arg, curthread);
250 error = sp->soc == NULL? ENOTCONN: ism_fullfeature(dev, 1);
252 sp->proc = curthread->td_proc;
253 SYSCTL_ADD_UINT(&sp->clist,
254 SYSCTL_CHILDREN(sp->oid),
258 &sp->proc->p_pid, sizeof(pid_t), "control process id");
263 error = sp->soc == NULL? ENOTCONN: ism_fullfeature(dev, 2);
267 error = ism_fullfeature(dev, 0);
271 int sig = *(int *)arg;
273 if(sig < 0 || sig > _SIG_MAXSIG)
281 iscsi_cam_t *cp = (iscsi_cam_t *)arg;
283 error = ic_getCamVals(sp, cp);
295 iscsi_read(struct dev_read_args *ra)
297 #ifdef ISCSI_INITIATOR_DEBUG
298 struct isc_softc *sc;
299 cdev_t dev = ra->a_head.a_dev;
300 struct uio *uio = ra->a_uio;
305 sc = (struct isc_softc *)dev->si_drv1;
306 sp = (isc_session_t *)dev->si_drv2;
308 if(minor(dev) == MAX_SESSIONS) {
309 ksprintf(buf, "/----- Session ------/\n");
310 uiomove(buf, strlen(buf), uio);
313 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
314 if(uio->uio_resid == 0)
316 ksprintf(buf, "%03d] '%s' '%s'\n", i++, sp->opt.targetAddress, sp->opt.targetName);
317 uiomove(buf, strlen(buf), uio);
319 ksprintf(buf, "%d/%d /---- free -----/\n", sc->npdu_alloc, sc->npdu_max);
321 uiomove(buf, strlen(buf), uio);
322 TAILQ_FOREACH(pq, &sc->freepdu, pq_link) {
323 if(uio->uio_resid == 0)
325 ksprintf(buf, "%03d] %06x\n", i++, ntohl(pq->pdu.ipdu.bhs.itt));
326 uiomove(buf, strlen(buf), uio);
331 struct socket *so = sp->soc;
332 #define pukeit(i, pq) do {\
333 ksprintf(buf, "%03d] %06x %02x %x %ld\n",\
334 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
335 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
336 (long)pq->ts.tv_sec);\
339 ksprintf(buf, "%d/%d /---- hld -----/\n", sp->stats.nhld, sp->stats.max_hld);
340 uiomove(buf, strlen(buf), uio);
341 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
342 if(uio->uio_resid == 0)
345 uiomove(buf, strlen(buf), uio);
347 ksprintf(buf, "%d/%d /---- rsp -----/\n", sp->stats.nrsp, sp->stats.max_rsp);
348 uiomove(buf, strlen(buf), uio);
350 TAILQ_FOREACH(pq, &sp->rsp, pq_link) {
351 if(uio->uio_resid == 0)
354 uiomove(buf, strlen(buf), uio);
356 ksprintf(buf, "%d/%d /---- csnd -----/\n", sp->stats.ncsnd, sp->stats.max_csnd);
358 uiomove(buf, strlen(buf), uio);
359 TAILQ_FOREACH(pq, &sp->csnd, pq_link) {
360 if(uio->uio_resid == 0)
363 uiomove(buf, strlen(buf), uio);
365 ksprintf(buf, "%d/%d /---- wsnd -----/\n", sp->stats.nwsnd, sp->stats.max_wsnd);
367 uiomove(buf, strlen(buf), uio);
368 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) {
369 if(uio->uio_resid == 0)
372 uiomove(buf, strlen(buf), uio);
374 ksprintf(buf, "%d/%d /---- isnd -----/\n", sp->stats.nisnd, sp->stats.max_isnd);
376 uiomove(buf, strlen(buf), uio);
377 TAILQ_FOREACH(pq, &sp->isnd, pq_link) {
378 if(uio->uio_resid == 0)
381 uiomove(buf, strlen(buf), uio);
384 ksprintf(buf, "/---- Stats ---/\n");
385 uiomove(buf, strlen(buf), uio);
387 ksprintf(buf, "recv=%d sent=%d\n", sp->stats.nrecv, sp->stats.nsent);
388 uiomove(buf, strlen(buf), uio);
390 ksprintf(buf, "flags=%x pdus: alloc=%d max=%d\n",
391 sp->flags, sc->npdu_alloc, sc->npdu_max);
392 uiomove(buf, strlen(buf), uio);
394 ksprintf(buf, "cws=%d last cmd=%x exp=%x max=%x stat=%x itt=%x\n",
395 sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
396 uiomove(buf, strlen(buf), uio);
399 ksprintf(buf, "/---- socket -----/\nso_state=%x\n", so->so_state);
400 uiomove(buf, strlen(buf), uio);
408 i_ping(struct cdev *dev)
416 i_setsoc(isc_session_t *sp, int fd, struct thread *td)
422 isc_stop_receiver(sp);
430 if ((error = holdsock(td->td_proc->p_fd, fd, &fp)) == 0) {
432 sp->soc = fp->f_data;
434 isc_start_receiver(sp);
441 i_send(struct cdev *dev, caddr_t arg, struct thread *td)
443 isc_session_t *sp = (isc_session_t *)dev->si_drv2;
444 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
455 if((pq = pdu_alloc(sc, M_NOWAIT)) == NULL)
458 pq->pdu = *(pdu_t *)arg;
460 if((error = i_prepPDU(sp, pq)) != 0)
463 sdebug(3, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
465 pq->buf = bp = kmalloc(pq->len - sizeof(union ipdu_u), M_ISCSI, M_NOWAIT);
466 if(pq->buf == NULL) {
473 error = copyin(pp->ahs, bp, n);
475 sdebug(3, "copyin ahs: error=%d", error);
478 pp->ahs = (ahs_t *)bp;
483 error = copyin(pp->ds, bp, n);
485 sdebug(3, "copyin ds: error=%d", error);
496 error = isc_qout(sp, pq);
499 wakeup(&sp->flags); // XXX: to 'push' proc_out ...
509 | NOTE: must calculate digest if requiered.
512 i_recv(struct cdev *dev, caddr_t arg, struct thread *td)
514 isc_session_t *sp = (isc_session_t *)dev->si_drv2;
518 int error, mustfree, cnt;
519 size_t need, have, n;
529 cnt = 6; // XXX: maybe the user can request a time out?
530 iscsi_lock_ex(&sp->rsp_mtx);
531 while((pq = TAILQ_FIRST(&sp->rsp)) == NULL) {
532 issleep(&sp->rsp, &sp->rsp_mtx, 0, "isc_rsp", hz*10);
533 if(cnt-- == 0) break; // XXX: for now, needs work
538 TAILQ_REMOVE(&sp->rsp, pq, pq_link);
540 iscsi_unlock_ex(&sp->rsp_mtx);
542 sdebug(4, "cnt=%d", cnt);
546 sdebug(3, "error=%d sp->flags=%x ", error, sp->flags);
574 if(len > pq->mp->m_len) {
576 bp = kmalloc(len, M_ISCSI, M_INTWAIT);
577 sdebug(4, "need mbufcopy: %d", len);
578 i_mbufcopy(pq->mp, bp, len);
581 bp = mtod(pq->mp, caddr_t);
587 n = MIN(up->ahs_size, need);
588 error = copyout(bp, (caddr_t)up->ahs, n);
592 if(!error && pp->ds_len) {
596 if((have = up->ds_size) == 0) {
597 have = up->ahs_size - n;
598 up->ds = (caddr_t)up->ahs + n;
601 error = copyout(bp, (caddr_t)up->ds, n);
609 sdebug(6, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
611 pdu_free(sp->isc, pq);
617 i_create_session(struct cdev *dev, int *ndev)
619 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
624 sp = (isc_session_t *)kmalloc(sizeof *sp, M_ISCSI, M_WAITOK | M_ZERO);
627 lockmgr(&sc->lock, LK_EXCLUSIVE);
629 | search for the lowest unused sid
631 for(n = 0; n < MAX_SESSIONS; n++)
632 if(sc->sessions[n] == NULL)
634 if(n == MAX_SESSIONS) {
635 lockmgr(&sc->lock, LK_RELEASE);
639 TAILQ_INSERT_TAIL(&sc->isc_sess, sp, sp_link);
641 lockmgr(&sc->lock, LK_RELEASE);
643 sc->sessions[n] = sp;
644 debug(8, "n is %d", n);
645 sp->dev = make_dev(&iscsi_ops, n, UID_ROOT, GID_WHEEL, 0600, "iscsi%d", n);
648 reference_dev(sp->dev);
652 sp->dev->si_drv1 = sc;
653 sp->dev->si_drv2 = sp;
655 sp->opt.maxRecvDataSegmentLength = 8192;
656 sp->opt.maxXmitDataSegmentLength = 8192;
658 sp->opt.maxBurstLength = 65536; // 64k
660 sdebug(2, "sessionID=%d sp=%p", n, sp);
661 error = ism_start(sp);
668 iscsi_counters(isc_session_t *sp)
673 #define _puke(i, pq) do {\
674 debug(2, "%03d] %06x %02x %x %ld %jd %x\n",\
675 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
676 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
677 (long)pq->ts.sec, pq->ts.frac, pq->flags);\
681 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
685 TAILQ_FOREACH(pq, &sp->rsp, pq_link) r++;
686 TAILQ_FOREACH(pq, &sp->csnd, pq_link) s++;
687 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) s++;
688 TAILQ_FOREACH(pq, &sp->isnd, pq_link) s++;
689 debug(2, "hld=%d rsp=%d snd=%d", h, r, s);
694 iscsi_shutdown(void *v)
696 struct isc_softc *sc = (struct isc_softc *)v;
702 xdebug("sc is NULL!");
706 debug(2, "sc->eh is NULL");
708 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->eh);
709 debug(2, "done n=%d", sc->nsess);
712 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
713 debug(2, "%2d] sp->flags=0x%08x", n, sp->flags);
720 init_pdus(struct isc_softc *sc)
724 sc->pdu_zone = objcache_create("pdu", 0, 0,
726 objcache_malloc_alloc,
727 objcache_malloc_free,
730 if(sc->pdu_zone == NULL) {
731 kprintf("iscsi_initiator: objcache_create failed");
734 TAILQ_INIT(&sc->freepdu);
740 free_pdus(struct isc_softc *sc)
746 if(sc->pdu_zone != NULL) {
747 TAILQ_FOREACH(pq, &sc->freepdu, pq_link) {
748 TAILQ_REMOVE(&sc->freepdu, pq, pq_link);
749 objcache_put(sc->pdu_zone, pq);
751 objcache_destroy(sc->pdu_zone);
759 struct isc_softc *sc = &isc;
763 memset(sc, 0, sizeof(struct isc_softc));
765 sc->dev = make_dev(&iscsi_ops, MAX_SESSIONS, UID_ROOT, GID_WHEEL, 0600, "iscsi");
768 sc->dev->si_drv1 = sc;
770 reference_dev(sc->dev);
772 TAILQ_INIT(&sc->isc_sess);
773 if(init_pdus(sc) != 0)
774 xdebug("pdu zone init failed!"); // XXX: should cause terminal failure ...
776 lockinit(&sc->lock, "iscsi", 0, LK_CANRECURSE);
777 lockinit(&sc->pdu_lock, "iscsi pdu pool", 0, LK_CANRECURSE);
780 // XXX: this will cause a panic if the
781 // module is loaded too early
788 #ifdef DO_EVENTHANDLER
789 if((sc->eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, iscsi_shutdown,
790 sc, SHUTDOWN_PRI_DEFAULT-1)) == NULL)
791 xdebug("shutdown event registration failed\n");
796 sysctl_ctx_init(&sc->clist);
797 sc->oid = SYSCTL_ADD_NODE(&sc->clist,
798 SYSCTL_STATIC_CHILDREN(_net),
805 SYSCTL_ADD_STRING(&sc->clist,
806 SYSCTL_CHILDREN(sc->oid),
810 iscsi_driver_version,
812 "iscsi driver version");
814 SYSCTL_ADD_STRING(&sc->clist,
815 SYSCTL_CHILDREN(sc->oid),
821 "initiator part of the Session Identifier");
823 SYSCTL_ADD_INT(&sc->clist,
824 SYSCTL_CHILDREN(sc->oid),
830 "number of active session");
832 kprintf("iscsi: version %s\n", iscsi_driver_version);
837 | unload SHOULD fail if there is activity
838 | activity: there is/are active session/s
843 struct isc_softc *sc = &isc;
844 isc_session_t *sp, *sp_tmp;
849 | go through all the sessions
850 | Note: close should have done this ...
852 TAILQ_FOREACH_MUTABLE(sp, &sc->isc_sess, sp_link, sp_tmp) {
853 //XXX: check for activity ...
856 if(sc->cam_sim != NULL)
859 lockuninit(&sc->lock);
860 lockuninit(&sc->pdu_lock);
864 release_dev(sc->dev);
865 destroy_dev(sc->dev);
866 //dev_ops_remove(&sc->dev, -1, 0);
869 if(sysctl_ctx_free(&sc->clist))
870 xdebug("sysctl_ctx_free failed");
872 iscsi_shutdown(sc); // XXX: check EVENTHANDLER_ ...
876 iscsi_modevent(module_t mod, int what, void *arg)
898 moduledata_t iscsi_mod = {
900 (modeventhand_t) iscsi_modevent,
909 nfs_setup_diskless();
910 if (nfs_diskless_valid)
911 rootdevnames[0] = "nfs:";
913 kprintf("** iscsi_rootconf **\n");
916 SYSINIT(cpu_rootconf1, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, iscsi_rootconf, NULL)
919 DECLARE_MODULE(iscsi, iscsi_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
920 MODULE_DEPEND(iscsi, cam, 1, 1, 1);