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/queue.h>
52 #include <sys/kthread.h>
54 #include <sys/syslog.h>
55 #include <sys/eventhandler.h>
56 #include <sys/mutex2.h>
57 #include <sys/devfs.h>
60 #include <bus/cam/cam.h>
61 #include <dev/disk/iscsi/initiator/iscsi.h>
62 #include <dev/disk/iscsi/initiator/iscsivar.h>
64 static char *iscsi_driver_version = "2.1.0";
66 static struct isc_softc isc;
68 MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI driver");
70 struct objcache_malloc_args iscsi_malloc_args = {
71 sizeof(pduq_t), M_ISCSI
74 #ifdef ISCSI_INITIATOR_DEBUG
75 int iscsi_debug = ISCSI_INITIATOR_DEBUG;
76 SYSCTL_INT(_debug, OID_AUTO, iscsi_initiator, CTLFLAG_RW, &iscsi_debug, 0, "iSCSI driver debug flag");
78 struct lock iscsi_dbg_lock;
82 static char isid[6+1] = {
92 static int i_create_session(struct cdev *dev, int *ndev);
94 static int i_ping(struct cdev *dev);
95 static int i_send(struct cdev *dev, caddr_t arg, struct thread *td);
96 static int i_recv(struct cdev *dev, caddr_t arg, struct thread *td);
97 static int i_setsoc(isc_session_t *sp, int fd, struct thread *td);
99 static void free_pdus(struct isc_softc *sc);
101 static d_open_t iscsi_open;
102 static d_close_t iscsi_close;
103 static d_ioctl_t iscsi_ioctl;
104 #ifdef ISCSI_INITIATOR_DEBUG
105 static d_read_t iscsi_read;
108 static struct dev_ops iscsi_ops = {
109 .head = { "iscsi", 0, D_DISK},
110 .d_open = iscsi_open,
111 .d_close = iscsi_close,
112 .d_ioctl = iscsi_ioctl,
113 #ifdef ISCSI_INITIATOR_DEBUG
114 .d_read = iscsi_read,
119 iscsi_open(struct dev_open_args *ap)
121 cdev_t dev = ap->a_head.a_dev;
125 debug(7, "dev=%d", dev->si_uminor);
127 if(minor(dev) > MAX_SESSIONS) {
132 /* Make sure the device is passed */
133 if (dev->si_drv1 == NULL)
134 dev->si_drv1 = (struct isc *)isc.dev->si_drv1;
136 if(minor(dev) == MAX_SESSIONS) {
138 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
140 // this should be in iscsi_start
141 if(sc->cam_sim == NULL)
149 iscsi_close(struct dev_close_args *ap)
151 cdev_t dev = ap->a_head.a_dev;
152 int flag = ap->a_fflag;
157 debug(3, "flag=%x", flag);
159 if(minor(dev) == MAX_SESSIONS) {
162 sp = (isc_session_t *)dev->si_drv2;
164 sdebug(2, "session=%d flags=%x", minor(dev), sp->flags );
166 | if still in full phase, this probably means
167 | that something went realy bad.
168 | it could be a result from 'shutdown', in which case
169 | we will ignore it (so buffers can be flushed).
170 | the problem is that there is no way of differentiating
171 | between a shutdown procedure and 'iscontrol' dying.
173 if(sp->flags & ISC_FFPHASE)
174 // delay in case this is a shutdown.
175 tsleep(sp, 0, "isc-cls", 60*hz);
183 iscsi_ioctl(struct dev_ioctl_args *ap)
186 cdev_t dev = ap->a_head.a_dev;
187 caddr_t arg = ap->a_data;
192 sc = (struct isc *)dev->si_drv1;
196 if(minor(dev) == MAX_SESSIONS) {
198 | non Session commands
205 error = i_create_session(dev, (int *)arg);
211 error = ENXIO; // XXX:
215 sp = (isc_session_t *)dev->si_drv2;
222 sdebug(6, "dev=%d cmd=%d", minor(dev), (int)(ap->a_cmd & 0xff));
226 error = i_setsoc(sp, *(u_int *)arg, curthread);
230 opt = (isc_opt_t *)arg;
231 error = i_setopt(sp, opt);
235 error = i_send(dev, arg, curthread);
239 error = i_recv(dev, arg, curthread);
247 error = sp->soc == NULL? ENOTCONN: ism_fullfeature(dev, 1);
249 sp->proc = curthread->td_proc;
250 SYSCTL_ADD_UINT(&sp->clist,
251 SYSCTL_CHILDREN(sp->oid),
255 &sp->proc->p_pid, sizeof(pid_t), "control process id");
260 error = sp->soc == NULL? ENOTCONN: ism_fullfeature(dev, 2);
264 error = ism_fullfeature(dev, 0);
268 int sig = *(int *)arg;
270 if(sig < 0 || sig > _SIG_MAXSIG)
278 iscsi_cam_t *cp = (iscsi_cam_t *)arg;
280 error = ic_getCamVals(sp, cp);
292 iscsi_read(struct dev_read_args *ra)
294 #ifdef ISCSI_INITIATOR_DEBUG
295 struct isc_softc *sc;
296 cdev_t dev = ra->a_head.a_dev;
297 struct uio *uio = ra->a_uio;
302 sc = (struct isc_softc *)dev->si_drv1;
303 sp = (isc_session_t *)dev->si_drv2;
305 if(minor(dev) == MAX_SESSIONS) {
306 ksprintf(buf, "/----- Session ------/\n");
307 uiomove(buf, strlen(buf), uio);
310 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
311 if(uio->uio_resid == 0)
313 ksprintf(buf, "%03d] '%s' '%s'\n", i++, sp->opt.targetAddress, sp->opt.targetName);
314 uiomove(buf, strlen(buf), uio);
316 ksprintf(buf, "%d/%d /---- free -----/\n", sc->npdu_alloc, sc->npdu_max);
318 uiomove(buf, strlen(buf), uio);
319 TAILQ_FOREACH(pq, &sc->freepdu, pq_link) {
320 if(uio->uio_resid == 0)
322 ksprintf(buf, "%03d] %06x\n", i++, ntohl(pq->pdu.ipdu.bhs.itt));
323 uiomove(buf, strlen(buf), uio);
328 struct socket *so = sp->soc;
329 #define pukeit(i, pq) do {\
330 ksprintf(buf, "%03d] %06x %02x %x %ld\n",\
331 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
332 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
333 (long)pq->ts.tv_sec);\
336 ksprintf(buf, "%d/%d /---- hld -----/\n", sp->stats.nhld, sp->stats.max_hld);
337 uiomove(buf, strlen(buf), uio);
338 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
339 if(uio->uio_resid == 0)
342 uiomove(buf, strlen(buf), uio);
344 ksprintf(buf, "%d/%d /---- rsp -----/\n", sp->stats.nrsp, sp->stats.max_rsp);
345 uiomove(buf, strlen(buf), uio);
347 TAILQ_FOREACH(pq, &sp->rsp, pq_link) {
348 if(uio->uio_resid == 0)
351 uiomove(buf, strlen(buf), uio);
353 ksprintf(buf, "%d/%d /---- csnd -----/\n", sp->stats.ncsnd, sp->stats.max_csnd);
355 uiomove(buf, strlen(buf), uio);
356 TAILQ_FOREACH(pq, &sp->csnd, pq_link) {
357 if(uio->uio_resid == 0)
360 uiomove(buf, strlen(buf), uio);
362 ksprintf(buf, "%d/%d /---- wsnd -----/\n", sp->stats.nwsnd, sp->stats.max_wsnd);
364 uiomove(buf, strlen(buf), uio);
365 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) {
366 if(uio->uio_resid == 0)
369 uiomove(buf, strlen(buf), uio);
371 ksprintf(buf, "%d/%d /---- isnd -----/\n", sp->stats.nisnd, sp->stats.max_isnd);
373 uiomove(buf, strlen(buf), uio);
374 TAILQ_FOREACH(pq, &sp->isnd, pq_link) {
375 if(uio->uio_resid == 0)
378 uiomove(buf, strlen(buf), uio);
381 ksprintf(buf, "/---- Stats ---/\n");
382 uiomove(buf, strlen(buf), uio);
384 ksprintf(buf, "recv=%d sent=%d\n", sp->stats.nrecv, sp->stats.nsent);
385 uiomove(buf, strlen(buf), uio);
387 ksprintf(buf, "flags=%x pdus: alloc=%d max=%d\n",
388 sp->flags, sc->npdu_alloc, sc->npdu_max);
389 uiomove(buf, strlen(buf), uio);
391 ksprintf(buf, "cws=%d last cmd=%x exp=%x max=%x stat=%x itt=%x\n",
392 sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
393 uiomove(buf, strlen(buf), uio);
396 ksprintf(buf, "/---- socket -----/\nso_state=%x\n", so->so_state);
397 uiomove(buf, strlen(buf), uio);
405 i_ping(struct cdev *dev)
413 i_setsoc(isc_session_t *sp, int fd, struct thread *td)
419 isc_stop_receiver(sp);
427 if ((error = holdsock(td->td_proc->p_fd, fd, &fp)) == 0) {
428 sp->soc = fp->f_data;
430 isc_start_receiver(sp);
437 i_send(struct cdev *dev, caddr_t arg, struct thread *td)
439 isc_session_t *sp = (isc_session_t *)dev->si_drv2;
440 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
451 if((pq = pdu_alloc(sc, M_NOWAIT)) == NULL)
454 pq->pdu = *(pdu_t *)arg;
456 if((error = i_prepPDU(sp, pq)) != 0)
459 sdebug(3, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
461 pq->buf = bp = kmalloc(pq->len - sizeof(union ipdu_u), M_ISCSI, M_NOWAIT);
462 if(pq->buf == NULL) {
469 error = copyin(pp->ahs, bp, n);
471 sdebug(3, "copyin ahs: error=%d", error);
474 pp->ahs = (ahs_t *)bp;
479 error = copyin(pp->ds, bp, n);
481 sdebug(3, "copyin ds: error=%d", error);
492 error = isc_qout(sp, pq);
495 wakeup(&sp->flags); // XXX: to 'push' proc_out ...
505 | NOTE: must calculate digest if requiered.
508 i_recv(struct cdev *dev, caddr_t arg, struct thread *td)
510 isc_session_t *sp = (isc_session_t *)dev->si_drv2;
514 int error, mustfree, cnt;
515 size_t need, have, n;
525 cnt = 6; // XXX: maybe the user can request a time out?
526 iscsi_lock_ex(&sp->rsp_mtx);
527 while((pq = TAILQ_FIRST(&sp->rsp)) == NULL) {
528 issleep(&sp->rsp, &sp->rsp_mtx, 0, "isc_rsp", hz*10);
529 if(cnt-- == 0) break; // XXX: for now, needs work
534 TAILQ_REMOVE(&sp->rsp, pq, pq_link);
536 iscsi_unlock_ex(&sp->rsp_mtx);
538 sdebug(4, "cnt=%d", cnt);
542 sdebug(3, "error=%d sp->flags=%x ", error, sp->flags);
570 if(len > pq->mp->m_len) {
572 bp = kmalloc(len, M_ISCSI, M_INTWAIT);
573 sdebug(4, "need mbufcopy: %d", len);
574 i_mbufcopy(pq->mp, bp, len);
577 bp = mtod(pq->mp, caddr_t);
583 n = MIN(up->ahs_size, need);
584 error = copyout(bp, (caddr_t)up->ahs, n);
588 if(!error && pp->ds_len) {
592 if((have = up->ds_size) == 0) {
593 have = up->ahs_size - n;
594 up->ds = (caddr_t)up->ahs + n;
597 error = copyout(bp, (caddr_t)up->ds, n);
605 sdebug(6, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
607 pdu_free(sp->isc, pq);
613 i_create_session(struct cdev *dev, int *ndev)
615 struct isc_softc *sc = (struct isc_softc *)dev->si_drv1;
620 sp = (isc_session_t *)kmalloc(sizeof *sp, M_ISCSI, M_WAITOK | M_ZERO);
623 lockmgr(&sc->lock, LK_EXCLUSIVE);
625 | search for the lowest unused sid
627 for(n = 0; n < MAX_SESSIONS; n++)
628 if(sc->sessions[n] == NULL)
630 if(n == MAX_SESSIONS) {
631 lockmgr(&sc->lock, LK_RELEASE);
635 TAILQ_INSERT_TAIL(&sc->isc_sess, sp, sp_link);
637 lockmgr(&sc->lock, LK_RELEASE);
639 sc->sessions[n] = sp;
640 debug(8, "n is %d", n);
641 sp->dev = make_dev(&iscsi_ops, n, UID_ROOT, GID_WHEEL, 0600, "iscsi%d", n);
643 reference_dev(sp->dev);
644 udev_dict_set_cstr(sp->dev, "subsystem", "disk");
645 udev_dict_set_cstr(sp->dev, "disk-type", "network");
649 sp->dev->si_drv1 = sc;
650 sp->dev->si_drv2 = sp;
652 sp->opt.maxRecvDataSegmentLength = 8192;
653 sp->opt.maxXmitDataSegmentLength = 8192;
655 sp->opt.maxBurstLength = 65536; // 64k
657 sdebug(2, "sessionID=%d sp=%p", n, sp);
658 error = ism_start(sp);
665 iscsi_counters(isc_session_t *sp)
670 #define _puke(i, pq) do {\
671 debug(2, "%03d] %06x %02x %x %ld %jd %x\n",\
672 i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
673 pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
674 (long)pq->ts.sec, pq->ts.frac, pq->flags);\
678 TAILQ_FOREACH(pq, &sp->hld, pq_link) {
682 TAILQ_FOREACH(pq, &sp->rsp, pq_link) r++;
683 TAILQ_FOREACH(pq, &sp->csnd, pq_link) s++;
684 TAILQ_FOREACH(pq, &sp->wsnd, pq_link) s++;
685 TAILQ_FOREACH(pq, &sp->isnd, pq_link) s++;
686 debug(2, "hld=%d rsp=%d snd=%d", h, r, s);
691 iscsi_shutdown(void *v)
693 struct isc_softc *sc = (struct isc_softc *)v;
699 xdebug("sc is NULL!");
703 debug(2, "sc->eh is NULL");
705 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->eh);
706 debug(2, "done n=%d", sc->nsess);
709 TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
710 debug(2, "%2d] sp->flags=0x%08x", n, sp->flags);
717 init_pdus(struct isc_softc *sc)
721 sc->pdu_zone = objcache_create("pdu", 0, 0,
723 objcache_malloc_alloc,
724 objcache_malloc_free,
727 if(sc->pdu_zone == NULL) {
728 kprintf("iscsi_initiator: objcache_create failed");
731 TAILQ_INIT(&sc->freepdu);
737 free_pdus(struct isc_softc *sc)
743 if(sc->pdu_zone != NULL) {
744 TAILQ_FOREACH(pq, &sc->freepdu, pq_link) {
745 TAILQ_REMOVE(&sc->freepdu, pq, pq_link);
746 objcache_put(sc->pdu_zone, pq);
748 objcache_destroy(sc->pdu_zone);
756 struct isc_softc *sc = &isc;
760 memset(sc, 0, sizeof(struct isc_softc));
762 sc->dev = make_dev(&iscsi_ops, MAX_SESSIONS, UID_ROOT, GID_WHEEL, 0600, "iscsi");
765 sc->dev->si_drv1 = sc;
767 reference_dev(sc->dev);
769 TAILQ_INIT(&sc->isc_sess);
770 if(init_pdus(sc) != 0)
771 xdebug("pdu zone init failed!"); // XXX: should cause terminal failure ...
773 lockinit(&sc->lock, "iscsi", 0, LK_CANRECURSE);
774 lockinit(&sc->pdu_lock, "iscsi pdu pool", 0, LK_CANRECURSE);
777 // XXX: this will cause a panic if the
778 // module is loaded too early
785 #ifdef DO_EVENTHANDLER
786 if((sc->eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, iscsi_shutdown,
787 sc, SHUTDOWN_PRI_DEFAULT-1)) == NULL)
788 xdebug("shutdown event registration failed\n");
793 sysctl_ctx_init(&sc->clist);
794 sc->oid = SYSCTL_ADD_NODE(&sc->clist,
795 SYSCTL_STATIC_CHILDREN(_net),
802 SYSCTL_ADD_STRING(&sc->clist,
803 SYSCTL_CHILDREN(sc->oid),
807 iscsi_driver_version,
809 "iscsi driver version");
811 SYSCTL_ADD_STRING(&sc->clist,
812 SYSCTL_CHILDREN(sc->oid),
818 "initiator part of the Session Identifier");
820 SYSCTL_ADD_INT(&sc->clist,
821 SYSCTL_CHILDREN(sc->oid),
827 "number of active session");
829 kprintf("iscsi: version %s\n", iscsi_driver_version);
834 | unload SHOULD fail if there is activity
835 | activity: there is/are active session/s
840 struct isc_softc *sc = &isc;
841 isc_session_t *sp, *sp_tmp;
846 | go through all the sessions
847 | Note: close should have done this ...
849 TAILQ_FOREACH_MUTABLE(sp, &sc->isc_sess, sp_link, sp_tmp) {
850 //XXX: check for activity ...
853 if(sc->cam_sim != NULL)
856 lockuninit(&sc->lock);
857 lockuninit(&sc->pdu_lock);
861 release_dev(sc->dev);
862 destroy_dev(sc->dev);
863 //dev_ops_remove(&sc->dev, -1, 0);
866 if(sysctl_ctx_free(&sc->clist))
867 xdebug("sysctl_ctx_free failed");
869 iscsi_shutdown(sc); // XXX: check EVENTHANDLER_ ...
873 iscsi_modevent(module_t mod, int what, void *arg)
895 moduledata_t iscsi_mod = {
897 (modeventhand_t) iscsi_modevent,
906 nfs_setup_diskless();
907 if (nfs_diskless_valid)
908 rootdevnames[0] = "nfs:";
910 kprintf("** iscsi_rootconf **\n");
913 SYSINIT(cpu_rootconf1, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, iscsi_rootconf, NULL);
916 DECLARE_MODULE(iscsi_initiator, iscsi_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
917 MODULE_DEPEND(iscsi_initiator, cam, 1, 1, 1);