]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/iscsi/initiator/iscsi.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / iscsi / initiator / iscsi.c
1 /*-
2  * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27 /*
28  | $Id: iscsi.c 752 2009-08-20 11:23:28Z danny $
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_iscsi_initiator.h"
35
36 #include <sys/param.h>
37 #include <sys/capability.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/conf.h>
41 #include <sys/bus.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>
47 #include <sys/file.h>
48 #include <sys/uio.h>
49 #include <sys/socketvar.h>
50 #include <sys/socket.h>
51 #include <sys/protosw.h>
52 #include <sys/proc.h>
53 #include <sys/ioccom.h>
54 #include <sys/queue.h>
55 #include <sys/kthread.h>
56 #include <sys/mbuf.h>
57 #include <sys/syslog.h>
58 #include <vm/uma.h>
59 #include <sys/sx.h>
60
61 #include <dev/iscsi/initiator/iscsi.h>
62 #include <dev/iscsi/initiator/iscsivar.h>
63 static char *iscsi_driver_version = "2.2.4.2";
64
65 static struct isc_softc *isc;
66
67 MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI driver");
68 MALLOC_DEFINE(M_ISCSIBUF, "iSCbuf", "iSCSI buffers");
69 MALLOC_DEFINE(M_TMP, "iSCtmp", "iSCSI tmp");
70
71 #ifdef ISCSI_INITIATOR_DEBUG
72 int iscsi_debug = ISCSI_INITIATOR_DEBUG;
73 SYSCTL_INT(_debug, OID_AUTO, iscsi_initiator, CTLFLAG_RW, &iscsi_debug, 0,
74         "iSCSI driver debug flag");
75
76 struct mtx iscsi_dbg_mtx;
77 #endif
78
79 static int max_sessions = MAX_SESSIONS;
80 SYSCTL_INT(_net, OID_AUTO, iscsi_initiator_max_sessions, CTLFLAG_RDTUN, &max_sessions, MAX_SESSIONS,
81            "Max sessions allowed");
82 static int max_pdus = MAX_PDUS;
83 SYSCTL_INT(_net, OID_AUTO, iscsi_initiator_max_pdus, CTLFLAG_RDTUN, &max_pdus, MAX_PDUS,
84            "Max pdu pool");
85
86 static char isid[6+1] = {
87      0x80,
88      'D',
89      'I',
90      'B',
91      '0',
92      '0',
93      0
94 };
95
96 static int      i_create_session(struct cdev *dev, int *ndev);
97
98 static int      i_ping(struct cdev *dev);
99 static int      i_send(struct cdev *dev, caddr_t arg, struct thread *td);
100 static int      i_recv(struct cdev *dev, caddr_t arg, struct thread *td);
101 static int      i_setsoc(isc_session_t *sp, int fd, struct thread *td);
102 static int      i_fullfeature(struct cdev *dev, int flag);
103
104 static d_open_t iscsi_open;
105 static d_close_t iscsi_close;
106 static d_ioctl_t iscsi_ioctl;
107 #ifdef ISCSI_INITIATOR_DEBUG
108 static d_read_t iscsi_read;
109 #endif
110
111 static struct cdevsw iscsi_cdevsw = {
112      .d_version = D_VERSION,
113      .d_open    = iscsi_open,
114      .d_close   = iscsi_close,
115      .d_ioctl   = iscsi_ioctl,
116 #ifdef ISCSI_INITIATOR_DEBUG
117      .d_read    = iscsi_read,
118 #endif
119      .d_name    = "iSCSI",
120 };
121
122 static int
123 iscsi_open(struct cdev *dev, int flags, int otype, struct thread *td)
124 {
125      debug_called(8);
126
127      debug(7, "dev=%d", dev2unit(dev));
128
129      if(dev2unit(dev) > max_sessions) {
130           // should not happen
131           return ENODEV;
132      }
133      return 0;
134 }
135
136 static int
137 iscsi_close(struct cdev *dev, int flag, int otyp, struct thread *td)
138 {
139      isc_session_t      *sp;
140
141      debug_called(8);
142
143      debug(3, "session=%d flag=%x", dev2unit(dev), flag);
144
145      if(dev2unit(dev) == max_sessions) {
146           return 0;
147      }
148      sp = dev->si_drv2;
149      if(sp != NULL) {
150           sdebug(3, "sp->flags=%x", sp->flags );
151           /*
152            | if still in full phase, this probably means
153            | that something went realy bad.
154            | it could be a result from 'shutdown', in which case
155            | we will ignore it (so buffers can be flushed).
156            | the problem is that there is no way of differentiating
157            | between a shutdown procedure and 'iscontrol' dying.
158            */
159           if(sp->flags & ISC_FFPHASE)
160                // delay in case this is a shutdown.
161                tsleep(sp, PRIBIO, "isc-cls", 60*hz);
162           ism_stop(sp);
163      }
164      debug(2, "done");
165      return 0;
166 }
167
168 static int
169 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
170 {
171      struct isc_softc   *sc;
172      isc_session_t      *sp;
173      isc_opt_t          *opt;
174      int                error;
175
176      debug_called(8);
177
178      error = 0;
179      if(dev2unit(dev) == max_sessions) {
180           /*
181            | non Session commands
182            */
183           sc = dev->si_drv1;
184           if(sc == NULL)
185                return ENXIO;
186
187           switch(cmd) {
188           case ISCSISETSES:
189                error = i_create_session(dev, (int *)arg);
190                if(error == 0)
191                     break;
192
193           default:
194                error = ENXIO;
195           }
196           return error;
197      }
198      /*
199       | session commands
200       */
201      sp = dev->si_drv2;
202      if(sp == NULL)
203           return ENXIO;
204
205      sdebug(6, "dev=%d cmd=%d", dev2unit(dev), (int)(cmd & 0xff));
206
207      switch(cmd) {
208      case ISCSISETSOC:
209           error = i_setsoc(sp, *(u_int *)arg, td);
210           break;
211
212      case ISCSISETOPT:
213           opt = (isc_opt_t *)arg;
214           error = i_setopt(sp, opt);
215           break;
216
217      case ISCSISEND:
218           error = i_send(dev, arg, td);
219           break;
220
221      case ISCSIRECV:
222           error = i_recv(dev, arg, td);
223           break;
224
225      case ISCSIPING:
226           error = i_ping(dev);
227           break;
228
229      case ISCSISTART:
230           error = sp->soc == NULL? ENOTCONN: i_fullfeature(dev, 1);
231           if(error == 0) {
232                sp->proc = td->td_proc;
233                SYSCTL_ADD_INT(&sp->clist, SYSCTL_CHILDREN(sp->oid),
234                    OID_AUTO, "pid", CTLFLAG_RD,
235                    &sp->proc->p_pid, sizeof(pid_t), "control process id");
236           }
237           break;
238
239      case ISCSIRESTART:
240           error = sp->soc == NULL? ENOTCONN: i_fullfeature(dev, 2);
241           break;
242
243      case ISCSISTOP:
244           error = i_fullfeature(dev, 0);
245           break;
246           
247      case ISCSISIGNAL: {
248           int sig = *(int *)arg;
249
250           if(sig < 0 || sig > _SIG_MAXSIG)
251                error = EINVAL;
252           else
253                 sp->signal = sig;
254           break;
255      }
256
257      case ISCSIGETCAM: {
258           iscsi_cam_t *cp = (iscsi_cam_t *)arg;
259
260           error = ic_getCamVals(sp, cp);
261           break;
262      }
263
264      default:
265           error = ENOIOCTL;
266      }
267
268      return error;
269 }
270
271 static int
272 iscsi_read(struct cdev *dev, struct uio *uio, int ioflag)
273 {
274 #ifdef  ISCSI_INITIATOR_DEBUG
275      struct isc_softc   *sc;
276      isc_session_t      *sp;
277      pduq_t             *pq;
278      char               buf[1024];
279
280      sc = dev->si_drv1;
281      sp = dev->si_drv2;
282      if(dev2unit(dev) == max_sessions) {
283           sprintf(buf, "/----- Session ------/\n");
284           uiomove(buf, strlen(buf), uio);
285           int   i = 0;
286
287           TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
288                if(uio->uio_resid == 0)
289                     return 0;
290                sprintf(buf, "%03d] '%s' '%s'\n", i++, sp->opt.targetAddress, sp->opt.targetName);
291                uiomove(buf, strlen(buf), uio);
292           }
293           sprintf(buf, "%d/%d /---- free -----/\n", sc->npdu_alloc, sc->npdu_max);
294           i = 0;
295           uiomove(buf, strlen(buf), uio);
296      }
297      else {
298           int   i = 0;
299           struct socket *so = sp->soc;
300 #define pukeit(i, pq) do {\
301                sprintf(buf, "%03d] %06x %02x %06x %06x %jd\n",\
302                        i, ntohl(pq->pdu.ipdu.bhs.CmdSN),\
303                        pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
304                        ntohl(pq->pdu.ipdu.bhs.ExpStSN),\
305                        (intmax_t)pq->ts.sec);\
306                } while(0)
307
308           sprintf(buf, "%d/%d /---- hld -----/\n", sp->stats.nhld, sp->stats.max_hld);
309           uiomove(buf, strlen(buf), uio);
310           TAILQ_FOREACH(pq, &sp->hld, pq_link) {
311                if(uio->uio_resid == 0)
312                     return 0;
313                pukeit(i, pq); i++;
314                uiomove(buf, strlen(buf), uio);
315           }
316           sprintf(buf, "%d/%d /---- rsp -----/\n", sp->stats.nrsp, sp->stats.max_rsp);
317           uiomove(buf, strlen(buf), uio);
318           i = 0;
319           TAILQ_FOREACH(pq, &sp->rsp, pq_link) {
320                if(uio->uio_resid == 0)
321                     return 0;
322                pukeit(i, pq); i++;
323                uiomove(buf, strlen(buf), uio);
324           }
325           sprintf(buf, "%d/%d /---- csnd -----/\n", sp->stats.ncsnd, sp->stats.max_csnd);
326           i = 0;
327           uiomove(buf, strlen(buf), uio);
328           TAILQ_FOREACH(pq, &sp->csnd, pq_link) {
329                if(uio->uio_resid == 0)
330                     return 0;
331                pukeit(i, pq); i++;
332                uiomove(buf, strlen(buf), uio);
333           }
334           sprintf(buf, "%d/%d /---- wsnd -----/\n", sp->stats.nwsnd, sp->stats.max_wsnd);
335           i = 0;
336           uiomove(buf, strlen(buf), uio);
337           TAILQ_FOREACH(pq, &sp->wsnd, pq_link) {
338                if(uio->uio_resid == 0)
339                     return 0;
340                pukeit(i, pq); i++;
341                uiomove(buf, strlen(buf), uio);
342           }
343           sprintf(buf, "%d/%d /---- isnd -----/\n", sp->stats.nisnd, sp->stats.max_isnd);
344           i = 0;
345           uiomove(buf, strlen(buf), uio);
346           TAILQ_FOREACH(pq, &sp->isnd, pq_link) {
347                if(uio->uio_resid == 0)
348                     return 0;
349                pukeit(i, pq); i++;
350                uiomove(buf, strlen(buf), uio);
351           }
352
353           sprintf(buf, "/---- Stats ---/\n");
354           uiomove(buf, strlen(buf), uio);
355
356           sprintf(buf, "recv=%d sent=%d\n", sp->stats.nrecv, sp->stats.nsent);
357           uiomove(buf, strlen(buf), uio);
358
359           sprintf(buf, "flags=%x pdus: alloc=%d max=%d\n", 
360                   sp->flags, sc->npdu_alloc, sc->npdu_max);
361           uiomove(buf, strlen(buf), uio);
362
363           sprintf(buf, "cws=%d last cmd=%x exp=%x max=%x stat=%x itt=%x\n",
364                   sp->cws, sp->sn.cmd, sp->sn.expCmd, sp->sn.maxCmd, sp->sn.stat, sp->sn.itt);
365           uiomove(buf, strlen(buf), uio);
366
367           sprintf(buf, "/---- socket -----/\nso_count=%d so_state=%x\n", so->so_count, so->so_state);
368           uiomove(buf, strlen(buf), uio);
369
370      }
371 #endif
372      return 0;
373 }
374
375 static int
376 i_ping(struct cdev *dev)
377 {
378      return 0;
379 }
380 /*
381  | low level I/O
382  */
383 static int
384 i_setsoc(isc_session_t *sp, int fd, struct thread *td)
385 {
386      int error = 0;
387
388      if(sp->soc != NULL)
389           isc_stop_receiver(sp);
390
391      error = fget(td, fd, CAP_SOCK_ALL, &sp->fp);
392      if(error)
393           return error;
394
395      if((error = fgetsock(td, fd, CAP_SOCK_ALL, &sp->soc, 0)) == 0) {
396           sp->td = td;
397           isc_start_receiver(sp);
398      }
399      else {
400           fdrop(sp->fp, td);
401           sp->fp = NULL;
402      }
403
404      return error;
405 }
406
407 static int
408 i_send(struct cdev *dev, caddr_t arg, struct thread *td)
409 {
410      isc_session_t      *sp = dev->si_drv2;
411      caddr_t            bp;
412      pduq_t             *pq;
413      pdu_t              *pp;
414      int                n, error;
415
416      debug_called(8);
417
418      if(sp->soc == NULL)
419           return ENOTCONN;
420
421      if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL)
422           return EAGAIN;
423      pp = &pq->pdu;
424      pq->pdu = *(pdu_t *)arg;
425      if((error = i_prepPDU(sp, pq)) != 0)
426           goto out;
427
428      bp = NULL;
429      if((pq->len - sizeof(union ipdu_u)) > 0) {
430           pq->buf = bp = malloc(pq->len - sizeof(union ipdu_u), M_ISCSIBUF, M_NOWAIT);
431           if(pq->buf == NULL) {
432                error = EAGAIN;
433                goto out;
434           }
435      }
436      else
437           pq->buf = NULL; // just in case?
438
439      sdebug(2, "len=%d ahs_len=%d ds_len=%d buf=%zu@%p",
440             pq->len, pp->ahs_len, pp->ds_len, pq->len - sizeof(union ipdu_u), bp);
441
442      if(pp->ahs_len) {
443           // XXX: never tested, looks suspicious
444           n = pp->ahs_len;
445           error = copyin(pp->ahs_addr, bp, n);
446           if(error != 0) {
447                sdebug(3, "copyin ahs: error=%d", error);
448                goto out;
449           }
450           pp->ahs_addr = (ahs_t *)bp;
451           bp += n;
452      }
453      if(pp->ds_len) {
454           n = pp->ds_len;
455           error = copyin(pp->ds_addr, bp, n);
456           if(error != 0) {
457                sdebug(3, "copyin ds: error=%d", error);
458                goto out;
459           }
460           pp->ds_addr = bp;
461           bp += n;
462           while(n & 03) {
463                n++;
464                *bp++ = 0;
465           }
466      }
467
468      error = isc_qout(sp, pq);
469      if(error == 0)
470           wakeup(&sp->flags); // XXX: to 'push' proc_out ...
471 out:
472      if(error)
473           pdu_free(sp->isc, pq);
474
475      return error;
476 }
477
478 static int
479 i_recv(struct cdev *dev, caddr_t arg, struct thread *td)
480 {
481      isc_session_t      *sp = dev->si_drv2;
482      pduq_t             *pq;
483      pdu_t              *pp, *up;
484      caddr_t            bp;
485      int                error, mustfree, cnt;
486      size_t             need, have, n;
487
488      debug_called(8);
489
490      if(sp == NULL)
491           return EIO;
492
493      if(sp->soc == NULL)
494           return ENOTCONN;
495      cnt = 6;     // XXX: maybe the user can request a time out?
496      mtx_lock(&sp->rsp_mtx);
497      while((pq = TAILQ_FIRST(&sp->rsp)) == NULL) {
498           msleep(&sp->rsp, &sp->rsp_mtx, PRIBIO, "isc_rsp", hz*10);
499           if(cnt-- == 0) break; // XXX: for now, needs work
500      }
501      if(pq != NULL) {
502           sp->stats.nrsp--;
503           TAILQ_REMOVE(&sp->rsp, pq, pq_link);
504      }
505      mtx_unlock(&sp->rsp_mtx);
506
507      sdebug(6, "cnt=%d", cnt);
508
509      if(pq == NULL) {
510           error = ENOTCONN;
511           sdebug(3, "error=%d sp->flags=%x ", error, sp->flags);
512           return error;
513      }
514      up = (pdu_t *)arg;
515      pp = &pq->pdu;
516      up->ipdu = pp->ipdu;
517      n = 0;
518      up->ds_len = 0;
519      up->ahs_len = 0;
520      error = 0;
521
522      if(pq->mp) {
523           u_int len;
524
525           // Grr...
526           len = 0;
527           if(pp->ahs_len) {
528                len += pp->ahs_len;
529           }
530           if(pp->ds_len) {
531                len += pp->ds_len;
532           }
533
534           mustfree = 0;
535           if(len > pq->mp->m_len) {
536                mustfree++;
537                bp = malloc(len, M_TMP, M_WAITOK);
538                sdebug(4, "need mbufcopy: %d", len);
539                i_mbufcopy(pq->mp, bp, len);
540           } 
541           else
542                bp = mtod(pq->mp, caddr_t);
543
544           if(pp->ahs_len) {
545                need = pp->ahs_len;
546                n = MIN(up->ahs_size, need);
547                error = copyout(bp, (caddr_t)up->ahs_addr, n);
548                up->ahs_len = n;
549                bp += need;
550           }
551           if(!error && pp->ds_len) {
552                need = pp->ds_len;
553                if((have = up->ds_size) == 0) {
554                     have = up->ahs_size - n;
555                     up->ds_addr = (caddr_t)up->ahs_addr + n;
556                }
557                n = MIN(have, need);
558                error = copyout(bp, (caddr_t)up->ds_addr, n);
559                up->ds_len = n;
560           }
561
562           if(mustfree)
563                free(bp, M_TMP);
564      }
565
566      sdebug(6, "len=%d ahs_len=%d ds_len=%d", pq->len, pp->ahs_len, pp->ds_len);
567
568      pdu_free(sp->isc, pq);
569
570      return error;
571 }
572
573 static int
574 i_fullfeature(struct cdev *dev, int flag)
575 {
576      isc_session_t      *sp = dev->si_drv2;
577      int                error;
578
579      sdebug(2, "flag=%d", flag);
580
581      error = 0;
582      switch(flag) {
583      case 0: // stop
584          sp->flags &= ~ISC_FFPHASE;
585          break;
586      case 1: // start
587          sp->flags |= ISC_FFPHASE;
588          error = ic_init(sp);
589          break;
590      case 2: // restart
591          sp->flags |= ISC_FFPHASE;
592          ism_restart(sp);
593          break;
594      }
595      return error;
596 }
597
598 static int
599 i_create_session(struct cdev *dev, int *ndev)
600
601      struct isc_softc   *sc = dev->si_drv1;
602      isc_session_t      *sp;
603      int                error, n;
604
605      debug_called(8);
606
607      sp = malloc(sizeof(isc_session_t), M_ISCSI, M_WAITOK | M_ZERO);
608      if(sp == NULL)
609           return ENOMEM;
610
611      sx_xlock(&sc->unit_sx);
612      if((n = alloc_unr(sc->unit)) < 0) {
613           sx_unlock(&sc->unit_sx);
614           free(sp, M_ISCSI);
615           xdebug("too many sessions!");
616           return EPERM;
617      }
618      sx_unlock(&sc->unit_sx);
619
620      mtx_lock(&sc->isc_mtx);
621      TAILQ_INSERT_TAIL(&sc->isc_sess, sp, sp_link);
622      isc->nsess++;
623      mtx_unlock(&sc->isc_mtx);
624
625      sp->dev = make_dev(&iscsi_cdevsw, n, UID_ROOT, GID_WHEEL, 0600, "iscsi%d", n);
626      *ndev = sp->sid = n;
627      sp->isc = sc;
628      sp->dev->si_drv1 = sc;
629      sp->dev->si_drv2 = sp;
630
631      sp->opt.maxRecvDataSegmentLength = 8192;
632      sp->opt.maxXmitDataSegmentLength = 8192;
633      sp->opt.maxBurstLength = 65536;    // 64k
634      sp->opt.maxluns = ISCSI_MAX_LUNS;
635
636      error = ism_start(sp);
637
638      return error;
639 }
640
641 #ifdef notused
642 static void
643 iscsi_counters(isc_session_t *sp)
644 {
645      int        h, r, s;
646      pduq_t     *pq;
647
648 #define _puke(i, pq) do {\
649                debug(2, "%03d] %06x %02x %x %ld %jd %x\n",\
650                        i, ntohl( pq->pdu.ipdu.bhs.CmdSN), \
651                        pq->pdu.ipdu.bhs.opcode, ntohl(pq->pdu.ipdu.bhs.itt),\
652                        (long)pq->ts.sec, pq->ts.frac, pq->flags);\
653                } while(0)
654
655      h = r = s = 0; 
656      TAILQ_FOREACH(pq, &sp->hld, pq_link) {
657           _puke(h, pq);
658           h++;
659      }
660      TAILQ_FOREACH(pq, &sp->rsp, pq_link) r++;
661      TAILQ_FOREACH(pq, &sp->csnd, pq_link) s++;
662      TAILQ_FOREACH(pq, &sp->wsnd, pq_link) s++;
663      TAILQ_FOREACH(pq, &sp->isnd, pq_link) s++;
664      debug(2, "hld=%d rsp=%d snd=%d", h, r, s);
665 }
666 #endif
667
668 static void
669 iscsi_shutdown(void *v)
670 {
671      struct isc_softc   *sc = v;
672      isc_session_t      *sp;
673      int        n;
674
675      debug_called(8);
676      if(sc == NULL) {
677           xdebug("sc is NULL!");
678           return;
679      }
680 #ifdef DO_EVENTHANDLER
681      if(sc->eh == NULL)
682           debug(2, "sc->eh is NULL");
683      else {
684           EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->eh);
685           debug(2, "done n=%d", sc->nsess);
686      }
687 #endif
688      n = 0;
689      TAILQ_FOREACH(sp, &sc->isc_sess, sp_link) {
690           debug(2, "%2d] sp->flags=0x%08x", n, sp->flags);
691           n++;
692      }
693      debug(2, "done");
694 }
695
696 static void
697 free_pdus(struct isc_softc *sc)
698 {
699
700      debug_called(8);
701
702      if(sc->pdu_zone != NULL) {
703           uma_zdestroy(sc->pdu_zone);
704           sc->pdu_zone = NULL;
705      }
706 }
707
708 static void
709 iscsi_start(void)
710 {
711      debug_called(8);
712
713      TUNABLE_INT_FETCH("net.iscsi_initiator.max_sessions", &max_sessions);
714      TUNABLE_INT_FETCH("net.iscsi_initiator.max_pdus", &max_pdus);
715
716      isc =  malloc(sizeof(struct isc_softc), M_ISCSI, M_ZERO|M_WAITOK);
717      isc->dev = make_dev(&iscsi_cdevsw, max_sessions, UID_ROOT, GID_WHEEL, 0600, "iscsi");
718      isc->dev->si_drv1 = isc;
719      mtx_init(&isc->isc_mtx, "iscsi", NULL, MTX_DEF);
720
721      TAILQ_INIT(&isc->isc_sess);
722      /*
723       | now init the free pdu list
724       */
725      isc->pdu_zone = uma_zcreate("pdu", sizeof(pduq_t),
726                                  NULL, NULL, NULL, NULL,
727                                  0, 0);
728      if(isc->pdu_zone == NULL) {
729           xdebug("iscsi_initiator: uma_zcreate failed");
730           // XXX: should fail...
731      }
732      uma_zone_set_max(isc->pdu_zone, max_pdus);
733      isc->unit = new_unrhdr(0, max_sessions-1, NULL);
734      sx_init(&isc->unit_sx, "iscsi sx");
735
736 #ifdef DO_EVENTHANDLER
737      if((isc->eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, iscsi_shutdown,
738                                         sc, SHUTDOWN_PRI_DEFAULT-1)) == NULL)
739           xdebug("shutdown event registration failed\n");
740 #endif
741      /*
742       | sysctl stuff
743       */
744      sysctl_ctx_init(&isc->clist);
745      isc->oid = SYSCTL_ADD_NODE(&isc->clist,
746                                SYSCTL_STATIC_CHILDREN(_net),
747                                OID_AUTO,
748                                "iscsi_initiator",
749                                CTLFLAG_RD,
750                                0,
751                                "iSCSI Subsystem");
752
753      SYSCTL_ADD_STRING(&isc->clist,
754                        SYSCTL_CHILDREN(isc->oid),
755                        OID_AUTO,
756                        "driver_version",
757                        CTLFLAG_RD,
758                        iscsi_driver_version,
759                        0,
760                        "iscsi driver version");
761  
762      SYSCTL_ADD_STRING(&isc->clist,
763                        SYSCTL_CHILDREN(isc->oid),
764                        OID_AUTO,
765                        "isid",
766                        CTLFLAG_RW,
767                        isid,
768                        6+1,
769                        "initiator part of the Session Identifier");
770
771      SYSCTL_ADD_INT(&isc->clist,
772                     SYSCTL_CHILDREN(isc->oid),
773                     OID_AUTO,
774                     "sessions",
775                     CTLFLAG_RD,
776                     &isc->nsess,
777                     sizeof(isc->nsess),
778                     "number of active session");
779
780      printf("iscsi: version %s\n", iscsi_driver_version);
781 }
782
783 /*
784  | Notes:
785  |      unload SHOULD fail if there is activity
786  |      activity: there is/are active session/s
787  */
788 static void
789 iscsi_stop(void)
790 {
791      isc_session_t      *sp, *sp_tmp;
792
793      debug_called(8);
794
795      /*
796       | go through all the sessions
797       | Note: close should have done this ...
798       */
799      TAILQ_FOREACH_SAFE(sp, &isc->isc_sess, sp_link, sp_tmp) {
800           //XXX: check for activity ...
801           ism_stop(sp);
802           if(sp->cam_sim != NULL)
803                ic_destroy(sp);
804      }
805      mtx_destroy(&isc->isc_mtx);
806      sx_destroy(&isc->unit_sx);
807
808      free_pdus(isc);
809
810      if(isc->dev)
811           destroy_dev(isc->dev);
812
813      if(sysctl_ctx_free(&isc->clist))
814           xdebug("sysctl_ctx_free failed");
815
816      iscsi_shutdown(isc); // XXX: check EVENTHANDLER_ ...
817      free(isc, M_ISCSI);
818 }
819
820 static int
821 iscsi_modevent(module_t mod, int what, void *arg)
822 {
823      debug_called(8);
824
825      switch(what) {
826      case MOD_LOAD:
827           iscsi_start();
828           break;
829
830      case MOD_QUIESCE:
831           if(isc->nsess) {
832                xdebug("iscsi module busy(nsess=%d), cannot unload", isc->nsess);
833                log(LOG_ERR, "iscsi module busy, cannot unload");
834           }
835           return isc->nsess;
836
837      case MOD_SHUTDOWN:
838           break;
839
840      case MOD_UNLOAD:
841           iscsi_stop();
842           break;
843
844      default:
845           break;
846      }
847      return 0;
848 }
849
850 moduledata_t iscsi_mod = {
851          "iscsi",
852          (modeventhand_t) iscsi_modevent,
853          0
854 };
855
856 #ifdef ISCSI_ROOT
857 static void
858 iscsi_rootconf(void)
859 {
860 #if 0
861         nfs_setup_diskless();
862         if (nfs_diskless_valid)
863                 rootdevnames[0] = "nfs:";
864 #endif
865         printf("** iscsi_rootconf **\n");
866 }
867
868 SYSINIT(cpu_rootconf1, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, iscsi_rootconf, NULL)
869 #endif
870
871 DECLARE_MODULE(iscsi, iscsi_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
872 MODULE_DEPEND(iscsi, cam, 1, 1, 1);