The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/cp/if_cp.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Cronyx-Tau-PCI adapter driver for FreeBSD.
    3  * Supports PPP/HDLC, Cisco/HDLC and FrameRelay protocol in synchronous mode,
    4  * and asyncronous channels with full modem control.
    5  * Keepalive protocol implemented in both Cisco and PPP modes.
    6  *
    7  * Copyright (C) 1999-2004 Cronyx Engineering.
    8  * Author: Kurakin Roman, <rik@cronyx.ru>
    9  *
   10  * Copyright (C) 1999-2002 Cronyx Engineering.
   11  * Author: Serge Vakulenko, <vak@cronyx.ru>
   12  *
   13  * This software is distributed with NO WARRANTIES, not even the implied
   14  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   15  *
   16  * Authors grant any other persons or organisations a permission to use,
   17  * modify and redistribute this software in source and binary forms,
   18  * as long as this message is kept with the software, all derivative
   19  * works or modified versions.
   20  *
   21  * Cronyx Id: if_cp.c,v 1.1.2.41 2004/06/23 17:09:13 rik Exp $
   22  */
   23 
   24 #include <sys/cdefs.h>
   25 __FBSDID("$FreeBSD: src/sys/dev/cp/if_cp.c,v 1.12.2.3 2005/07/25 07:08:26 rik Exp $");
   26 
   27 #include <sys/param.h>
   28 #include <sys/ucred.h>
   29 #include <sys/proc.h>
   30 #include <sys/systm.h>
   31 #include <sys/mbuf.h>
   32 #include <sys/kernel.h>
   33 #include <sys/module.h>
   34 #include <sys/conf.h>
   35 #include <sys/malloc.h>
   36 #include <sys/socket.h>
   37 #include <sys/sockio.h>
   38 #include <sys/sysctl.h>
   39 #include <sys/tty.h>
   40 #include <sys/bus.h>
   41 #include <vm/vm.h>
   42 #include <vm/pmap.h>
   43 #include <net/if.h>
   44 #include <dev/pci/pcivar.h>
   45 #include <dev/pci/pcireg.h>
   46 #include <machine/bus.h>
   47 #include <sys/rman.h>
   48 #include "opt_ng_cronyx.h"
   49 #ifdef NETGRAPH_CRONYX
   50 #   include "opt_netgraph.h"
   51 #   ifndef NETGRAPH
   52 #       error #option   NETGRAPH missed from configuration
   53 #   endif
   54 #   include <netgraph/ng_message.h>
   55 #   include <netgraph/netgraph.h>
   56 #   include <dev/cp/ng_cp.h>
   57 #else
   58 #   include <net/if_sppp.h>
   59 #   define PP_CISCO IFF_LINK2
   60 #   include <net/bpf.h>
   61 #endif
   62 #include <dev/cx/machdep.h>
   63 #include <dev/cp/cpddk.h>
   64 #include <machine/cserial.h>
   65 #include <machine/resource.h>
   66 #include <machine/pmap.h>
   67 
   68 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
   69 #ifndef PP_FR
   70 #define PP_FR 0
   71 #endif
   72 
   73 #define CP_DEBUG(d,s)   ({if (d->chan->debug) {\
   74                                 printf ("%s: ", d->name); printf s;}})
   75 #define CP_DEBUG2(d,s)  ({if (d->chan->debug>1) {\
   76                                 printf ("%s: ", d->name); printf s;}})
   77 #define CP_LOCK_NAME    "cpX"
   78 
   79 static  int     cp_mpsafenet = 1;
   80 TUNABLE_INT("debug.cp.mpsafenet", &cp_mpsafenet);
   81 SYSCTL_NODE(_debug, OID_AUTO, cp, CTLFLAG_RD, 0, "Cronyx Tau-PCI Adapters");
   82 SYSCTL_INT(_debug_cp, OID_AUTO, mpsafenet, CTLFLAG_RD, &cp_mpsafenet, 0,
   83         "Enable/disable MPSAFE network support for Cronyx Tau-PCI Adapters");
   84 
   85 #define CP_LOCK(_bd)            do { \
   86                                     if (cp_mpsafenet) \
   87                                         mtx_lock (&(_bd)->cp_mtx); \
   88                                 } while (0)
   89 #define CP_UNLOCK(_bd)          do { \
   90                                     if (cp_mpsafenet) \
   91                                         mtx_unlock (&(_bd)->cp_mtx); \
   92                                 } while (0)
   93 
   94 #define CP_LOCK_ASSERT(_bd)     do { \
   95                                     if (cp_mpsafenet) \
   96                                         mtx_assert (&(_bd)->cp_mtx, MA_OWNED); \
   97                                 } while (0)
   98 
   99 #define CDEV_MAJOR      134
  100 
  101 static  int cp_probe            __P((device_t));
  102 static  int cp_attach           __P((device_t));
  103 static  int cp_detach           __P((device_t));
  104 
  105 static  device_method_t cp_methods[] = {
  106         /* Device interface */
  107         DEVMETHOD(device_probe,         cp_probe),
  108         DEVMETHOD(device_attach,        cp_attach),
  109         DEVMETHOD(device_detach,        cp_detach),
  110 
  111         {0, 0}
  112 };
  113 
  114 typedef struct _cp_dma_mem_t {
  115         unsigned long   phys;
  116         void            *virt;
  117         size_t          size;
  118         bus_dma_tag_t   dmat;
  119         bus_dmamap_t    mapp;
  120 } cp_dma_mem_t;
  121 
  122 typedef struct _drv_t {
  123         char    name [8];
  124         int     running;
  125         cp_chan_t       *chan;
  126         cp_board_t      *board;
  127         cp_dma_mem_t    dmamem;
  128 #ifdef NETGRAPH
  129         char    nodename [NG_NODELEN+1];
  130         hook_p  hook;
  131         hook_p  debug_hook;
  132         node_p  node;
  133         struct  ifqueue queue;
  134         struct  ifqueue hi_queue;
  135         short   timeout;
  136         struct  callout timeout_handle;
  137 #else
  138         struct  ifqueue queue;
  139         struct  sppp pp;
  140 #endif
  141         struct  cdev *devt;
  142 } drv_t;
  143 
  144 typedef struct _bdrv_t {
  145         cp_board_t      *board;
  146         struct resource *cp_res;
  147         struct resource *cp_irq;
  148         void            *cp_intrhand;
  149         cp_dma_mem_t    dmamem;
  150         drv_t           channel [NCHAN];
  151         struct mtx      cp_mtx;
  152 } bdrv_t;
  153 
  154 static  driver_t cp_driver = {
  155         "cp",
  156         cp_methods,
  157         sizeof(bdrv_t),
  158 };
  159 
  160 static  devclass_t cp_devclass;
  161 
  162 static void cp_receive (cp_chan_t *c, unsigned char *data, int len);
  163 static void cp_transmit (cp_chan_t *c, void *attachment, int len);
  164 static void cp_error (cp_chan_t *c, int data);
  165 static void cp_up (drv_t *d);
  166 static void cp_start (drv_t *d);
  167 static void cp_down (drv_t *d);
  168 static void cp_watchdog (drv_t *d);
  169 #ifdef NETGRAPH
  170 extern struct ng_type typestruct;
  171 #else
  172 static void cp_ifstart (struct ifnet *ifp);
  173 static void cp_tlf (struct sppp *sp);
  174 static void cp_tls (struct sppp *sp);
  175 static void cp_ifwatchdog (struct ifnet *ifp);
  176 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
  177 static void cp_initialize (void *softc);
  178 #endif
  179 
  180 static cp_board_t *adapter [NBRD];
  181 static drv_t *channel [NBRD*NCHAN];
  182 static struct callout led_timo [NBRD];
  183 static struct callout timeout_handle;
  184 
  185 static int cp_destroy = 0;
  186 
  187 /*
  188  * Print the mbuf chain, for debug purposes only.
  189  */
  190 static void printmbuf (struct mbuf *m)
  191 {
  192         printf ("mbuf:");
  193         for (; m; m=m->m_next) {
  194                 if (m->m_flags & M_PKTHDR)
  195                         printf (" HDR %d:", m->m_pkthdr.len);
  196                 if (m->m_flags & M_EXT)
  197                         printf (" EXT:");
  198                 printf (" %d", m->m_len);
  199         }
  200         printf ("\n");
  201 }
  202 
  203 /*
  204  * Make an mbuf from data.
  205  */
  206 static struct mbuf *makembuf (void *buf, unsigned len)
  207 {
  208         struct mbuf *m;
  209 
  210         MGETHDR (m, M_DONTWAIT, MT_DATA);
  211         if (! m)
  212                 return 0;
  213         MCLGET (m, M_DONTWAIT);
  214         if (! (m->m_flags & M_EXT)) {
  215                 m_freem (m);
  216                 return 0;
  217         }
  218         m->m_pkthdr.len = m->m_len = len;
  219         bcopy (buf, mtod (m, caddr_t), len);
  220         return m;
  221 }
  222 
  223 static int cp_probe (device_t dev)
  224 {
  225         if ((pci_get_vendor (dev) == cp_vendor_id) &&
  226             (pci_get_device (dev) == cp_device_id)) {
  227                 device_set_desc (dev, "Cronyx-Tau-PCI serial adapter");
  228                 return 0;
  229         }
  230         return ENXIO;
  231 }
  232 
  233 static void cp_timeout (void *arg)
  234 {
  235         drv_t *d;
  236         int s, i, k;
  237 
  238         for (i = 0; i < NBRD; ++i) {
  239                 if (adapter[i] == NULL)
  240                         continue;
  241                 for (k = 0; k < NCHAN; ++k) {
  242                         s = splimp ();
  243                         if (cp_destroy) {
  244                                 splx (s);
  245                                 return;
  246                         }
  247                         d = channel[i * NCHAN + k];
  248                         if (!d) {
  249                                 splx (s);
  250                                 continue;
  251                         }
  252                         CP_LOCK ((bdrv_t *)d->board->sys);
  253                         switch (d->chan->type) {
  254                         case T_G703:
  255                                 cp_g703_timer (d->chan);
  256                                 break;
  257                         case T_E1:
  258                                 cp_e1_timer (d->chan);
  259                                 break;
  260                         case T_E3:
  261                         case T_T3:
  262                         case T_STS1:
  263                                 cp_e3_timer (d->chan);
  264                                 break;
  265                         default:
  266                                 break;
  267                         }
  268                         CP_UNLOCK ((bdrv_t *)d->board->sys);
  269                         splx (s);
  270                 }
  271         }
  272         s = splimp ();
  273         if (!cp_destroy)
  274                 callout_reset (&timeout_handle, hz, cp_timeout, 0);
  275         splx (s);
  276 }
  277 
  278 static void cp_led_off (void *arg)
  279 {
  280         cp_board_t *b = arg;
  281         bdrv_t *bd = (bdrv_t *) b->sys;
  282         int s;
  283         s = splimp ();
  284         if (cp_destroy) {
  285                 splx (s);
  286                 return;
  287         }
  288         CP_LOCK (bd);
  289         cp_led (b, 0);
  290         CP_UNLOCK (bd);
  291         splx (s);
  292 }
  293 
  294 static void cp_intr (void *arg)
  295 {
  296         bdrv_t *bd = arg;
  297         cp_board_t *b = bd->board;
  298 #ifndef NETGRAPH
  299         int i;
  300 #endif
  301         int s = splimp ();
  302         if (cp_destroy) {
  303                 splx (s);
  304                 return;
  305         }
  306         CP_LOCK (bd);
  307         /* Check if we are ready */
  308         if (b->sys == NULL) {
  309                 /* Not we are not, just cleanup. */
  310                 cp_interrupt_poll (b, 1);
  311                 CP_UNLOCK (bd);
  312                 return;
  313         }
  314         /* Turn LED on. */
  315         cp_led (b, 1);
  316 
  317         cp_interrupt (b);
  318 
  319         /* Turn LED off 50 msec later. */
  320         callout_reset (&led_timo[b->num], hz/20, cp_led_off, b);
  321         CP_UNLOCK (bd);
  322         splx (s);
  323 
  324 #ifndef NETGRAPH
  325         /* Pass packets in a lock-free state */
  326         for (i = 0; i < NCHAN && b->chan[i].type; i++) {
  327                 drv_t *d = b->chan[i].sys;
  328                 struct mbuf *m;
  329                 if (!d || !d->running)
  330                         continue;
  331                 while (_IF_QLEN(&d->queue)) {
  332                         IF_DEQUEUE (&d->queue,m);
  333                         if (!m)
  334                                 continue;
  335                         sppp_input (&d->pp.pp_if, m);   
  336                 }
  337         }
  338 #endif
  339 }
  340 
  341 extern struct cdevsw cp_cdevsw;
  342 
  343 static void
  344 cp_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
  345 {
  346         unsigned long *addr;
  347 
  348         if (error)
  349                 return;
  350 
  351         KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
  352         addr = arg;
  353         *addr = segs->ds_addr;
  354 }
  355 
  356 static int
  357 cp_bus_dma_mem_alloc (int bnum, int cnum, cp_dma_mem_t *dmem)
  358 {
  359         int error;
  360 
  361         error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
  362                 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
  363                 dmem->size, 0, NULL, NULL, &dmem->dmat);
  364         if (error) {
  365                 if (cnum >= 0)  printf ("cp%d-%d: ", bnum, cnum);
  366                 else            printf ("cp%d: ", bnum);
  367                 printf ("couldn't allocate tag for dma memory\n");
  368                 return 0;
  369         }
  370         error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
  371                 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
  372         if (error) {
  373                 if (cnum >= 0)  printf ("cp%d-%d: ", bnum, cnum);
  374                 else            printf ("cp%d: ", bnum);
  375                 printf ("couldn't allocate mem for dma memory\n");
  376                 bus_dma_tag_destroy (dmem->dmat);
  377                 return 0;
  378         }
  379         error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
  380                 dmem->size, cp_bus_dmamap_addr, &dmem->phys, 0);
  381         if (error) {
  382                 if (cnum >= 0)  printf ("cp%d-%d: ", bnum, cnum);
  383                 else            printf ("cp%d: ", bnum);
  384                 printf ("couldn't load mem map for dma memory\n");
  385                 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
  386                 bus_dma_tag_destroy (dmem->dmat);
  387                 return 0;
  388         }
  389         return 1;
  390 }
  391 
  392 static void
  393 cp_bus_dma_mem_free (cp_dma_mem_t *dmem)
  394 {
  395         bus_dmamap_unload (dmem->dmat, dmem->mapp);
  396         bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
  397         bus_dma_tag_destroy (dmem->dmat);
  398 }
  399 
  400 /*
  401  * Called if the probe succeeded.
  402  */
  403 static int cp_attach (device_t dev)
  404 {
  405         bdrv_t *bd = device_get_softc (dev);
  406         int unit = device_get_unit (dev);
  407         char *cp_ln = CP_LOCK_NAME;
  408         unsigned short res;
  409         vm_offset_t vbase;
  410         int rid, error;
  411         cp_board_t *b;
  412         cp_chan_t *c;
  413         drv_t *d;
  414         int s = splimp ();
  415 
  416         b = malloc (sizeof(cp_board_t), M_DEVBUF, M_WAITOK);
  417         if (!b) {
  418                 printf ("cp%d: couldn't allocate memory\n", unit);              
  419                 splx (s);
  420                 return (ENXIO);
  421         }
  422         bzero (b, sizeof(cp_board_t));
  423 
  424         bd->board = b;
  425         rid = PCIR_BAR(0);
  426         bd->cp_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
  427                         0, ~0, 1, RF_ACTIVE);
  428         if (! bd->cp_res) {
  429                 printf ("cp%d: cannot map memory\n", unit);
  430                 free (b, M_DEVBUF);
  431                 splx (s);
  432                 return (ENXIO);
  433         }
  434         vbase = (vm_offset_t) rman_get_virtual (bd->cp_res);
  435 
  436         cp_ln[2] = '' + unit;
  437         mtx_init (&bd->cp_mtx, cp_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
  438         res = cp_init (b, unit, (u_char*) vbase);
  439         if (res) {
  440                 printf ("cp%d: can't init, error code:%x\n", unit, res);
  441                 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
  442                 free (b, M_DEVBUF);
  443                 splx (s);
  444                 return (ENXIO);
  445         }
  446 
  447         bd->dmamem.size = sizeof(cp_qbuf_t);
  448         if (! cp_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
  449                 free (b, M_DEVBUF);
  450                 splx (s);
  451                 return (ENXIO);
  452         }
  453         CP_LOCK (bd);
  454         cp_reset (b, bd->dmamem.virt, bd->dmamem.phys);
  455         CP_UNLOCK (bd);
  456 
  457         rid = 0;
  458         bd->cp_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
  459                         RF_SHAREABLE | RF_ACTIVE);
  460         if (! bd->cp_irq) {
  461                 cp_destroy = 1;
  462                 printf ("cp%d: cannot map interrupt\n", unit);  
  463                 bus_release_resource (dev, SYS_RES_MEMORY,
  464                                 PCIR_BAR(0), bd->cp_res);
  465                 mtx_destroy (&bd->cp_mtx);
  466                 free (b, M_DEVBUF);
  467                 splx (s);
  468                 return (ENXIO);
  469         }
  470         callout_init (&led_timo[unit], cp_mpsafenet ? CALLOUT_MPSAFE : 0);
  471         error  = bus_setup_intr (dev, bd->cp_irq,
  472                                 INTR_TYPE_NET|(cp_mpsafenet?INTR_MPSAFE:0),
  473                                 cp_intr, bd, &bd->cp_intrhand);
  474         if (error) {
  475                 cp_destroy = 1;
  476                 printf ("cp%d: cannot set up irq\n", unit);
  477                 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
  478                 bus_release_resource (dev, SYS_RES_MEMORY,
  479                                 PCIR_BAR(0), bd->cp_res);
  480                 mtx_destroy (&bd->cp_mtx);
  481                 free (b, M_DEVBUF);
  482                 splx (s);
  483                 return (ENXIO);
  484         }
  485         printf ("cp%d: %s, clock %ld MHz\n", unit, b->name, b->osc / 1000000);
  486 
  487         for (c = b->chan; c < b->chan + NCHAN; ++c) {
  488                 if (! c->type)
  489                         continue;
  490                 d = &bd->channel[c->num];
  491                 d->dmamem.size = sizeof(cp_buf_t);
  492                 if (! cp_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
  493                         continue;
  494                 channel [b->num*NCHAN + c->num] = d;
  495                 sprintf (d->name, "cp%d.%d", b->num, c->num);
  496                 d->board = b;
  497                 d->chan = c;
  498                 c->sys = d;
  499 #ifdef NETGRAPH
  500                 if (ng_make_node_common (&typestruct, &d->node) != 0) {
  501                         printf ("%s: cannot make common node\n", d->name);
  502                         d->node = NULL;
  503                         continue;
  504                 }
  505                 NG_NODE_SET_PRIVATE (d->node, d);
  506                 sprintf (d->nodename, "%s%d", NG_CP_NODE_TYPE,
  507                          c->board->num*NCHAN + c->num);
  508                 if (ng_name_node (d->node, d->nodename)) {
  509                         printf ("%s: cannot name node\n", d->nodename);
  510                         NG_NODE_UNREF (d->node);
  511                         continue;
  512                 }
  513                 d->queue.ifq_maxlen = IFQ_MAXLEN;
  514                 d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
  515                 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
  516                 mtx_init (&d->hi_queue.ifq_mtx, "cp_queue_hi", NULL, MTX_DEF);
  517                 callout_init (&d->timeout_handle,
  518                              cp_mpsafenet ? CALLOUT_MPSAFE : 0);
  519 #else /*NETGRAPH*/
  520                 d->pp.pp_if.if_softc    = d;
  521                 if_initname (&d->pp.pp_if, "cp", b->num * NCHAN + c->num);
  522                 d->pp.pp_if.if_mtu      = PP_MTU;
  523                 d->pp.pp_if.if_flags    = IFF_POINTOPOINT | IFF_MULTICAST;
  524                 if (!cp_mpsafenet)
  525                         d->pp.pp_if.if_flags |= IFF_NEEDSGIANT;
  526                 d->pp.pp_if.if_ioctl    = cp_sioctl;
  527                 d->pp.pp_if.if_start    = cp_ifstart;
  528                 d->pp.pp_if.if_watchdog = cp_ifwatchdog;
  529                 d->pp.pp_if.if_init     = cp_initialize;
  530                 d->queue.ifq_maxlen     = NRBUF;
  531                 mtx_init (&d->queue.ifq_mtx, "cp_queue", NULL, MTX_DEF);
  532                 sppp_attach (&d->pp.pp_if);
  533                 if_attach (&d->pp.pp_if);
  534                 d->pp.pp_tlf            = cp_tlf;
  535                 d->pp.pp_tls            = cp_tls;
  536                 /* If BPF is in the kernel, call the attach for it.
  537                  * The header size of PPP or Cisco/HDLC is 4 bytes. */
  538                 bpfattach (&d->pp.pp_if, DLT_PPP, 4);
  539 #endif /*NETGRAPH*/
  540                 cp_start_e1 (c);
  541                 cp_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
  542 
  543                 /* Register callback functions. */
  544                 cp_register_transmit (c, &cp_transmit);
  545                 cp_register_receive (c, &cp_receive);
  546                 cp_register_error (c, &cp_error);
  547                 d->devt = make_dev (&cp_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
  548                                 GID_WHEEL, 0600, "cp%d", b->num*NCHAN+c->num);
  549         }
  550         CP_LOCK (bd);
  551         b->sys = bd;
  552         adapter[unit] = b;
  553         CP_UNLOCK (bd);
  554         splx (s);
  555         return 0;
  556 }
  557 
  558 static int cp_detach (device_t dev)
  559 {
  560         bdrv_t *bd = device_get_softc (dev);
  561         cp_board_t *b = bd->board;
  562         cp_chan_t *c;
  563         int s;
  564 
  565         KASSERT (mtx_initialized (&bd->cp_mtx), ("cp mutex not initialized"));
  566         s = splimp ();
  567         CP_LOCK (bd);
  568         /* Check if the device is busy (open). */
  569         for (c = b->chan; c < b->chan + NCHAN; ++c) {
  570                 drv_t *d = (drv_t*) c->sys;
  571 
  572                 if (! d || ! d->chan->type)
  573                         continue;
  574                 if (d->running) {
  575                         CP_UNLOCK (bd);
  576                         splx (s);
  577                         return EBUSY;
  578                 }
  579         }
  580 
  581         /* Ok, we can unload driver */
  582         /* At first we should stop all channels */
  583         for (c = b->chan; c < b->chan + NCHAN; ++c) {
  584                 drv_t *d = (drv_t*) c->sys;
  585 
  586                 if (! d || ! d->chan->type)
  587                         continue;
  588 
  589                 cp_stop_chan (c);
  590                 cp_stop_e1 (c);
  591                 cp_set_dtr (d->chan, 0);
  592                 cp_set_rts (d->chan, 0);
  593         }
  594 
  595         /* Reset the adapter. */
  596         cp_destroy = 1;
  597         cp_interrupt_poll (b, 1);
  598         cp_led_off (b);
  599         cp_reset (b, 0 ,0);
  600         callout_stop (&led_timo[b->num]);
  601 
  602         for (c=b->chan; c<b->chan+NCHAN; ++c) {
  603                 drv_t *d = (drv_t*) c->sys;
  604 
  605                 if (! d || ! d->chan->type)
  606                         continue;
  607 #ifndef NETGRAPH
  608                 /* Detach from the packet filter list of interfaces. */
  609                 bpfdetach (&d->pp.pp_if);
  610 
  611                 /* Detach from the sync PPP list. */
  612                 sppp_detach (&d->pp.pp_if);
  613 
  614                 /* Detach from the system list of interfaces. */
  615                 if_detach (&d->pp.pp_if);
  616                 IF_DRAIN (&d->queue);
  617                 mtx_destroy (&d->queue.ifq_mtx);
  618 #else
  619                 if (d->node) {
  620                         ng_rmnode_self (d->node);
  621                         NG_NODE_UNREF (d->node);
  622                         d->node = NULL;
  623                 }
  624                 mtx_destroy (&d->queue.ifq_mtx);
  625                 mtx_destroy (&d->hi_queue.ifq_mtx);
  626 #endif
  627                 destroy_dev (d->devt);
  628         }
  629 
  630         b->sys = NULL;
  631         CP_UNLOCK (bd);
  632 
  633         /* Disable the interrupt request. */
  634         bus_teardown_intr (dev, bd->cp_irq, bd->cp_intrhand);
  635         bus_deactivate_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
  636         bus_release_resource (dev, SYS_RES_IRQ, 0, bd->cp_irq);
  637         bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->cp_res);
  638 
  639         CP_LOCK (bd);
  640         cp_led_off (b);
  641         CP_UNLOCK (bd);
  642         callout_drain (&led_timo[b->num]);
  643         splx (s);
  644 
  645         s = splimp ();
  646         for (c = b->chan; c < b->chan + NCHAN; ++c) {
  647                 drv_t *d = (drv_t*) c->sys;
  648 
  649                 if (! d || ! d->chan->type)
  650                         continue;
  651                 channel [b->num*NCHAN + c->num] = 0;
  652                 /* Deallocate buffers. */
  653                 cp_bus_dma_mem_free (&d->dmamem);
  654         }
  655         adapter [b->num] = 0;
  656         cp_bus_dma_mem_free (&bd->dmamem);
  657         free (b, M_DEVBUF);
  658         splx (s);
  659         mtx_destroy (&bd->cp_mtx);
  660         return 0;
  661 }
  662 
  663 #ifndef NETGRAPH
  664 static void cp_ifstart (struct ifnet *ifp)
  665 {
  666         drv_t *d = ifp->if_softc;
  667         bdrv_t *bd = d->board->sys;
  668 
  669         CP_LOCK (bd);
  670         cp_start (d);
  671         CP_UNLOCK (bd);
  672 }
  673 
  674 static void cp_ifwatchdog (struct ifnet *ifp)
  675 {
  676         drv_t *d = ifp->if_softc;
  677 
  678         cp_watchdog (d);
  679 }
  680 
  681 static void cp_tlf (struct sppp *sp)
  682 {
  683         drv_t *d = sp->pp_if.if_softc;
  684 
  685         CP_DEBUG2 (d, ("cp_tlf\n"));
  686         /* XXXRIK: Don't forget to protect them by LOCK, or kill them. */
  687 /*      cp_set_dtr (d->chan, 0);*/
  688 /*      cp_set_rts (d->chan, 0);*/
  689         if (!(d->pp.pp_flags & PP_FR) && !(d->pp.pp_if.if_flags & PP_CISCO))
  690                 sp->pp_down (sp);
  691 }
  692 
  693 static void cp_tls (struct sppp *sp)
  694 {
  695         drv_t *d = sp->pp_if.if_softc;
  696 
  697         CP_DEBUG2 (d, ("cp_tls\n"));
  698         if (!(d->pp.pp_flags & PP_FR) && !(d->pp.pp_if.if_flags & PP_CISCO))
  699                 sp->pp_up (sp);
  700 }
  701 
  702 /*
  703  * Process an ioctl request.
  704  */
  705 static int cp_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
  706 {
  707         drv_t *d = ifp->if_softc;
  708         bdrv_t *bd = d->board->sys;
  709         int error, s, was_up, should_be_up;
  710 
  711         was_up = (ifp->if_flags & IFF_RUNNING) != 0;
  712         error = sppp_ioctl (ifp, cmd, data);
  713 
  714         if (error)
  715                 return error;
  716 
  717         if (! (ifp->if_flags & IFF_DEBUG))
  718                 d->chan->debug = 0;
  719         else if (! d->chan->debug)
  720                 d->chan->debug = 1;
  721 
  722         switch (cmd) {
  723         default:           CP_DEBUG2 (d, ("ioctl 0x%lx\n", cmd));   return 0;
  724         case SIOCADDMULTI: CP_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
  725         case SIOCDELMULTI: CP_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
  726         case SIOCSIFFLAGS: CP_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
  727         case SIOCSIFADDR:  CP_DEBUG2 (d, ("ioctl SIOCSIFADDR\n"));  break;
  728         }
  729 
  730         /* We get here only in case of SIFFLAGS or SIFADDR. */
  731         s = splimp ();
  732         CP_LOCK (bd);
  733         should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
  734         if (! was_up && should_be_up) {
  735                 /* Interface goes up -- start it. */
  736                 cp_up (d);
  737                 cp_start (d);
  738         } else if (was_up && ! should_be_up) {
  739                 /* Interface is going down -- stop it. */
  740 /*              if ((d->pp.pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
  741                 cp_down (d);
  742         }
  743         CP_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
  744         CP_UNLOCK (bd);
  745         splx (s);
  746         return 0;
  747 }
  748 
  749 /*
  750  * Initialization of interface.
  751  * It seems to be never called by upper level?
  752  */
  753 static void cp_initialize (void *softc)
  754 {
  755         drv_t *d = softc;
  756 
  757         CP_DEBUG (d, ("cp_initialize\n"));
  758 }
  759 #endif /*NETGRAPH*/
  760 
  761 /*
  762  * Stop the interface.  Called on splimp().
  763  */
  764 static void cp_down (drv_t *d)
  765 {
  766         CP_DEBUG (d, ("cp_down\n"));
  767         /* Interface is going down -- stop it. */
  768         cp_set_dtr (d->chan, 0);
  769         cp_set_rts (d->chan, 0);
  770 
  771         d->running = 0;
  772 }
  773 
  774 /*
  775  * Start the interface.  Called on splimp().
  776  */
  777 static void cp_up (drv_t *d)
  778 {
  779         CP_DEBUG (d, ("cp_up\n"));
  780         cp_set_dtr (d->chan, 1);
  781         cp_set_rts (d->chan, 1);
  782         d->running = 1;
  783 }
  784 
  785 /*
  786  * Start output on the interface.  Get another datagram to send
  787  * off of the interface queue, and copy it to the interface
  788  * before starting the output.
  789  */
  790 static void cp_send (drv_t *d)
  791 {
  792         struct mbuf *m;
  793         u_short len;
  794 
  795         CP_DEBUG2 (d, ("cp_send, tn=%d te=%d\n", d->chan->tn, d->chan->te));
  796 
  797         /* No output if the interface is down. */
  798         if (! d->running)
  799                 return;
  800 
  801         /* No output if the modem is off. */
  802         if (! (d->chan->lloop || d->chan->type != T_SERIAL ||
  803                 cp_get_dsr (d->chan)))
  804                 return;
  805 
  806         while (cp_transmit_space (d->chan)) {
  807                 /* Get the packet to send. */
  808 #ifdef NETGRAPH
  809                 IF_DEQUEUE (&d->hi_queue, m);
  810                 if (! m)
  811                         IF_DEQUEUE (&d->queue, m);
  812 #else
  813                 m = sppp_dequeue (&d->pp.pp_if);
  814 #endif
  815                 if (! m)
  816                         return;
  817 #ifndef NETGRAPH
  818                 if (d->pp.pp_if.if_bpf)
  819                         BPF_MTAP (&d->pp.pp_if, m);
  820 #endif
  821                 len = m_length (m, NULL);
  822                 if (len >= BUFSZ)
  823                         printf ("%s: too long packet: %d bytes: ",
  824                                 d->name, len);
  825                 else if (! m->m_next)
  826                         cp_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
  827                 else {
  828                         u_char *buf = d->chan->tbuf[d->chan->te];
  829                         m_copydata (m, 0, len, buf);
  830                         cp_send_packet (d->chan, buf, len, 0);
  831                 }
  832                 m_freem (m);
  833                 /* Set up transmit timeout, if the transmit ring is not empty.*/
  834 #ifdef NETGRAPH
  835                 d->timeout = 10;
  836 #else
  837                 d->pp.pp_if.if_timer = 10;
  838 #endif
  839         }
  840 #ifndef NETGRAPH
  841         d->pp.pp_if.if_flags |= IFF_OACTIVE;
  842 #endif
  843 }
  844 
  845 /*
  846  * Start output on the interface.
  847  * Always called on splimp().
  848  */
  849 static void cp_start (drv_t *d)
  850 {
  851         if (d->running) {
  852                 if (! d->chan->dtr)
  853                         cp_set_dtr (d->chan, 1);
  854                 if (! d->chan->rts)
  855                         cp_set_rts (d->chan, 1);
  856                 cp_send (d);
  857         }
  858 }
  859 
  860 /*
  861  * Handle transmit timeouts.
  862  * Recover after lost transmit interrupts.
  863  * Always called on splimp().
  864  */
  865 static void cp_watchdog (drv_t *d)
  866 {
  867         bdrv_t *bd = d->board->sys;
  868         CP_DEBUG (d, ("device timeout\n"));
  869         if (d->running) {
  870                 int s = splimp ();
  871 
  872                 CP_LOCK (bd);
  873                 cp_stop_chan (d->chan);
  874                 cp_stop_e1 (d->chan);
  875                 cp_start_e1 (d->chan);
  876                 cp_start_chan (d->chan, 1, 1, 0, 0);
  877                 cp_set_dtr (d->chan, 1);
  878                 cp_set_rts (d->chan, 1);
  879                 cp_start (d);
  880                 CP_UNLOCK (bd);
  881                 splx (s);
  882         }
  883 }
  884 
  885 static void cp_transmit (cp_chan_t *c, void *attachment, int len)
  886 {
  887         drv_t *d = c->sys;
  888 
  889 #ifdef NETGRAPH
  890         d->timeout = 0;
  891 #else
  892         ++d->pp.pp_if.if_opackets;
  893         d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
  894         d->pp.pp_if.if_timer = 0;
  895 #endif
  896         cp_start (d);
  897 }
  898 
  899 static void cp_receive (cp_chan_t *c, unsigned char *data, int len)
  900 {
  901         drv_t *d = c->sys;
  902         struct mbuf *m;
  903 #ifdef NETGRAPH
  904         int error;
  905 #endif
  906 
  907         if (! d->running)
  908                 return;
  909 
  910         m = makembuf (data, len);
  911         if (! m) {
  912                 CP_DEBUG (d, ("no memory for packet\n"));
  913 #ifndef NETGRAPH
  914                 ++d->pp.pp_if.if_iqdrops;
  915 #endif
  916                 return;
  917         }
  918         if (c->debug > 1)
  919                 printmbuf (m);
  920 #ifdef NETGRAPH
  921         m->m_pkthdr.rcvif = 0;
  922         NG_SEND_DATA_ONLY (error, d->hook, m);
  923 #else
  924         ++d->pp.pp_if.if_ipackets;
  925         m->m_pkthdr.rcvif = &d->pp.pp_if;
  926         /* Check if there's a BPF listener on this interface.
  927          * If so, hand off the raw packet to bpf. */
  928         if (d->pp.pp_if.if_bpf)
  929                 BPF_TAP (&d->pp.pp_if, data, len);
  930         IF_ENQUEUE (&d->queue, m);
  931 #endif
  932 }
  933 
  934 static void cp_error (cp_chan_t *c, int data)
  935 {
  936         drv_t *d = c->sys;
  937 
  938         switch (data) {
  939         case CP_FRAME:
  940                 CP_DEBUG (d, ("frame error\n"));
  941 #ifndef NETGRAPH
  942                 ++d->pp.pp_if.if_ierrors;
  943 #endif
  944                 break;
  945         case CP_CRC:
  946                 CP_DEBUG (d, ("crc error\n"));
  947 #ifndef NETGRAPH
  948                 ++d->pp.pp_if.if_ierrors;
  949 #endif
  950                 break;
  951         case CP_OVERRUN:
  952                 CP_DEBUG (d, ("overrun error\n"));
  953 #ifndef NETGRAPH
  954                 ++d->pp.pp_if.if_collisions;
  955                 ++d->pp.pp_if.if_ierrors;
  956 #endif
  957                 break;
  958         case CP_OVERFLOW:
  959                 CP_DEBUG (d, ("overflow error\n"));
  960 #ifndef NETGRAPH
  961                 ++d->pp.pp_if.if_ierrors;
  962 #endif
  963                 break;
  964         case CP_UNDERRUN:
  965                 CP_DEBUG (d, ("underrun error\n"));
  966 #ifdef NETGRAPH
  967                 d->timeout = 0;
  968 #else
  969                 ++d->pp.pp_if.if_oerrors;
  970                 d->pp.pp_if.if_flags &= ~IFF_OACTIVE;
  971                 d->pp.pp_if.if_timer = 0;
  972 #endif
  973                 cp_start (d);
  974                 break;
  975         default:
  976                 CP_DEBUG (d, ("error #%d\n", data));
  977                 break;
  978         }
  979 }
  980 
  981 /*
  982  * You also need read, write, open, close routines.
  983  * This should get you started
  984  */
  985 static int cp_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
  986 {
  987         int unit = minor (dev);
  988         drv_t *d;
  989 
  990         if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
  991                 return ENXIO;
  992         CP_DEBUG2 (d, ("cp_open\n"));
  993         return 0;
  994 }
  995 
  996 /*
  997  * Only called on the LAST close.
  998  */
  999 static int cp_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
 1000 {
 1001         drv_t *d = channel [minor (dev)];
 1002 
 1003         CP_DEBUG2 (d, ("cp_close\n"));
 1004         return 0;
 1005 }
 1006 
 1007 static int cp_modem_status (cp_chan_t *c)
 1008 {
 1009         drv_t *d = c->sys;
 1010         bdrv_t *bd = d->board->sys;
 1011         int status, s;
 1012 
 1013         status = d->running ? TIOCM_LE : 0;
 1014         s = splimp ();
 1015         CP_LOCK (bd);
 1016         if (cp_get_cd  (c)) status |= TIOCM_CD;
 1017         if (cp_get_cts (c)) status |= TIOCM_CTS;
 1018         if (cp_get_dsr (c)) status |= TIOCM_DSR;
 1019         if (c->dtr)         status |= TIOCM_DTR;
 1020         if (c->rts)         status |= TIOCM_RTS;
 1021         CP_UNLOCK (bd);
 1022         splx (s);
 1023         return status;
 1024 }
 1025 
 1026 static int cp_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
 1027 {
 1028         drv_t *d = channel [minor (dev)];
 1029         bdrv_t *bd = d->board->sys;
 1030         cp_chan_t *c = d->chan;
 1031         struct serial_statistics *st;
 1032         struct e1_statistics *opte1;
 1033         struct e3_statistics *opte3;
 1034         int error, s;
 1035         char mask[16];
 1036 
 1037         switch (cmd) {
 1038         case SERIAL_GETREGISTERED:
 1039                 CP_DEBUG2 (d, ("ioctl: getregistered\n"));
 1040                 bzero (mask, sizeof(mask));
 1041                 for (s=0; s<NBRD*NCHAN; ++s)
 1042                         if (channel [s])
 1043                                 mask [s/8] |= 1 << (s & 7);
 1044                 bcopy (mask, data, sizeof (mask));
 1045                 return 0;
 1046 
 1047 #ifndef NETGRAPH
 1048         case SERIAL_GETPROTO:
 1049                 CP_DEBUG2 (d, ("ioctl: getproto\n"));
 1050                 strcpy ((char*)data, (d->pp.pp_flags & PP_FR) ? "fr" :
 1051                         (d->pp.pp_if.if_flags & PP_CISCO) ? "cisco" : "ppp");
 1052                 return 0;
 1053 
 1054         case SERIAL_SETPROTO:
 1055                 CP_DEBUG2 (d, ("ioctl: setproto\n"));
 1056                 /* Only for superuser! */
 1057                 error = suser (td);
 1058                 if (error)
 1059                         return error;
 1060                 if (d->pp.pp_if.if_flags & IFF_RUNNING)
 1061                         return EBUSY;
 1062                 if (! strcmp ("cisco", (char*)data)) {
 1063                         d->pp.pp_flags &= ~(PP_FR);
 1064                         d->pp.pp_flags |= PP_KEEPALIVE;
 1065                         d->pp.pp_if.if_flags |= PP_CISCO;
 1066                 } else if (! strcmp ("fr", (char*)data) && PP_FR) {
 1067                         d->pp.pp_if.if_flags &= ~(PP_CISCO);
 1068                         d->pp.pp_flags |= PP_FR | PP_KEEPALIVE;
 1069                 } else if (! strcmp ("ppp", (char*)data)) {
 1070                         d->pp.pp_flags &= ~PP_FR;
 1071                         d->pp.pp_flags &= ~PP_KEEPALIVE;
 1072                         d->pp.pp_if.if_flags &= ~(PP_CISCO);
 1073                 } else
 1074                         return EINVAL;
 1075                 return 0;
 1076 
 1077         case SERIAL_GETKEEPALIVE:
 1078                 CP_DEBUG2 (d, ("ioctl: getkeepalive\n"));
 1079                 if ((d->pp.pp_flags & PP_FR) ||
 1080                         (d->pp.pp_if.if_flags & PP_CISCO))
 1081                         return EINVAL;
 1082                 *(int*)data = (d->pp.pp_flags & PP_KEEPALIVE) ? 1 : 0;
 1083                 return 0;
 1084 
 1085         case SERIAL_SETKEEPALIVE:
 1086                 CP_DEBUG2 (d, ("ioctl: setkeepalive\n"));
 1087                 /* Only for superuser! */
 1088                 error = suser (td);
 1089                 if (error)
 1090                         return error;
 1091                 if ((d->pp.pp_flags & PP_FR) ||
 1092                         (d->pp.pp_if.if_flags & PP_CISCO))
 1093                         return EINVAL;
 1094                 s = splimp ();
 1095                 CP_LOCK (bd);
 1096                 if (*(int*)data)
 1097                         d->pp.pp_flags |= PP_KEEPALIVE;
 1098                 else
 1099                         d->pp.pp_flags &= ~PP_KEEPALIVE;
 1100                 CP_UNLOCK (bd);
 1101                 splx (s);
 1102                 return 0;
 1103 #endif /*NETGRAPH*/
 1104 
 1105         case SERIAL_GETMODE:
 1106                 CP_DEBUG2 (d, ("ioctl: getmode\n"));
 1107                 *(int*)data = SERIAL_HDLC;
 1108                 return 0;
 1109 
 1110         case SERIAL_SETMODE:
 1111                 /* Only for superuser! */
 1112                 error = suser (td);
 1113                 if (error)
 1114                         return error;
 1115                 if (*(int*)data != SERIAL_HDLC)
 1116                         return EINVAL;
 1117                 return 0;
 1118 
 1119         case SERIAL_GETCFG:
 1120                 CP_DEBUG2 (d, ("ioctl: getcfg\n"));
 1121                 if (c->type != T_E1 || c->unfram)
 1122                         return EINVAL;
 1123                 *(char*)data = c->board->mux ? 'c' : 'a';
 1124                 return 0;
 1125 
 1126         case SERIAL_SETCFG:
 1127                 CP_DEBUG2 (d, ("ioctl: setcfg\n"));
 1128                 error = suser (td);
 1129                 if (error)
 1130                         return error;
 1131                 if (c->type != T_E1)
 1132                         return EINVAL;
 1133                 s = splimp ();
 1134                 CP_LOCK (bd);
 1135                 cp_set_mux (c->board, *((char*)data) == 'c');
 1136                 CP_UNLOCK (bd);
 1137                 splx (s);
 1138                 return 0;
 1139 
 1140         case SERIAL_GETSTAT:
 1141                 CP_DEBUG2 (d, ("ioctl: getstat\n"));
 1142                 st = (struct serial_statistics*) data;
 1143                 st->rintr  = c->rintr;
 1144                 st->tintr  = c->tintr;
 1145                 st->mintr  = 0;
 1146                 st->ibytes = c->ibytes;
 1147                 st->ipkts  = c->ipkts;
 1148                 st->obytes = c->obytes;
 1149                 st->opkts  = c->opkts;
 1150                 st->ierrs  = c->overrun + c->frame + c->crc;
 1151                 st->oerrs  = c->underrun;
 1152                 return 0;
 1153 
 1154         case SERIAL_GETESTAT:
 1155                 CP_DEBUG2 (d, ("ioctl: getestat\n"));
 1156                 if (c->type != T_E1 && c->type != T_G703)
 1157                         return EINVAL;
 1158                 opte1 = (struct e1_statistics*) data;
 1159                 opte1->status       = c->status;
 1160                 opte1->cursec       = c->cursec;
 1161                 opte1->totsec       = c->totsec + c->cursec;
 1162 
 1163                 opte1->currnt.bpv   = c->currnt.bpv;
 1164                 opte1->currnt.fse   = c->currnt.fse;
 1165                 opte1->currnt.crce  = c->currnt.crce;
 1166                 opte1->currnt.rcrce = c->currnt.rcrce;
 1167                 opte1->currnt.uas   = c->currnt.uas;
 1168                 opte1->currnt.les   = c->currnt.les;
 1169                 opte1->currnt.es    = c->currnt.es;
 1170                 opte1->currnt.bes   = c->currnt.bes;
 1171                 opte1->currnt.ses   = c->currnt.ses;
 1172                 opte1->currnt.oofs  = c->currnt.oofs;
 1173                 opte1->currnt.css   = c->currnt.css;
 1174                 opte1->currnt.dm    = c->currnt.dm;
 1175 
 1176                 opte1->total.bpv    = c->total.bpv   + c->currnt.bpv;
 1177                 opte1->total.fse    = c->total.fse   + c->currnt.fse;
 1178                 opte1->total.crce   = c->total.crce  + c->currnt.crce;
 1179                 opte1->total.rcrce  = c->total.rcrce + c->currnt.rcrce;
 1180                 opte1->total.uas    = c->total.uas   + c->currnt.uas;
 1181                 opte1->total.les    = c->total.les   + c->currnt.les;
 1182                 opte1->total.es     = c->total.es    + c->currnt.es;
 1183                 opte1->total.bes    = c->total.bes   + c->currnt.bes;
 1184                 opte1->total.ses    = c->total.ses   + c->currnt.ses;
 1185                 opte1->total.oofs   = c->total.oofs  + c->currnt.oofs;
 1186                 opte1->total.css    = c->total.css   + c->currnt.css;
 1187                 opte1->total.dm     = c->total.dm    + c->currnt.dm;
 1188                 for (s=0; s<48; ++s) {
 1189                         opte1->interval[s].bpv   = c->interval[s].bpv;
 1190                         opte1->interval[s].fse   = c->interval[s].fse;
 1191                         opte1->interval[s].crce  = c->interval[s].crce;
 1192                         opte1->interval[s].rcrce = c->interval[s].rcrce;
 1193                         opte1->interval[s].uas   = c->interval[s].uas;
 1194                         opte1->interval[s].les   = c->interval[s].les;
 1195                         opte1->interval[s].es    = c->interval[s].es;
 1196                         opte1->interval[s].bes   = c->interval[s].bes;
 1197                         opte1->interval[s].ses   = c->interval[s].ses;
 1198                         opte1->interval[s].oofs  = c->interval[s].oofs;
 1199                         opte1->interval[s].css   = c->interval[s].css;
 1200                         opte1->interval[s].dm    = c->interval[s].dm;
 1201                 }
 1202                 return 0;
 1203 
 1204         case SERIAL_GETE3STAT:
 1205                 CP_DEBUG2 (d, ("ioctl: gete3stat\n"));
 1206                 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
 1207                         return EINVAL;
 1208                 opte3 = (struct e3_statistics*) data;
 1209 
 1210                 opte3->status = c->e3status;
 1211                 opte3->cursec = (c->e3csec_5 * 2 + 1) / 10;
 1212                 opte3->totsec = c->e3tsec + opte3->cursec;
 1213 
 1214                 opte3->ccv = c->e3ccv;
 1215                 opte3->tcv = c->e3tcv + opte3->ccv;
 1216 
 1217                 for (s = 0; s < 48; ++s) {
 1218                         opte3->icv[s] = c->e3icv[s];
 1219                 }
 1220                 return 0;
 1221                 
 1222         case SERIAL_CLRSTAT:
 1223                 CP_DEBUG2 (d, ("ioctl: clrstat\n"));
 1224                 /* Only for superuser! */
 1225                 error = suser (td);
 1226                 if (error)
 1227                         return error;
 1228                 c->rintr    = 0;
 1229                 c->tintr    = 0;
 1230                 c->ibytes   = 0;
 1231                 c->obytes   = 0;
 1232                 c->ipkts    = 0;
 1233                 c->opkts    = 0;
 1234                 c->overrun  = 0;
 1235                 c->frame    = 0;
 1236                 c->crc      = 0;
 1237                 c->underrun = 0;
 1238                 bzero (&c->currnt, sizeof (c->currnt));
 1239                 bzero (&c->total, sizeof (c->total));
 1240                 bzero (c->interval, sizeof (c->interval));
 1241                 c->e3ccv    = 0;
 1242                 c->e3tcv    = 0;
 1243                 bzero (c->e3icv, sizeof (c->e3icv));
 1244                 return 0;
 1245 
 1246         case SERIAL_GETBAUD:
 1247                 CP_DEBUG2 (d, ("ioctl: getbaud\n"));
 1248                 *(long*)data = c->baud;
 1249                 return 0;
 1250 
 1251         case SERIAL_SETBAUD:
 1252                 CP_DEBUG2 (d, ("ioctl: setbaud\n"));
 1253                 /* Only for superuser! */
 1254                 error = suser (td);
 1255                 if (error)
 1256                         return error;
 1257                 s = splimp ();
 1258                 CP_LOCK (bd);
 1259                 cp_set_baud (c, *(long*)data);
 1260                 CP_UNLOCK (bd);
 1261                 splx (s);
 1262                 return 0;
 1263 
 1264         case SERIAL_GETLOOP:
 1265                 CP_DEBUG2 (d, ("ioctl: getloop\n"));
 1266                 *(int*)data = c->lloop;
 1267                 return 0;
 1268 
 1269         case SERIAL_SETLOOP:
 1270                 CP_DEBUG2 (d, ("ioctl: setloop\n"));
 1271                 /* Only for superuser! */
 1272                 error = suser (td);
 1273                 if (error)
 1274                         return error;
 1275                 s = splimp ();
 1276                 CP_LOCK (bd);
 1277                 cp_set_lloop (c, *(int*)data);
 1278                 CP_UNLOCK (bd);
 1279                 splx (s);
 1280                 return 0;
 1281 
 1282         case SERIAL_GETDPLL:
 1283                 CP_DEBUG2 (d, ("ioctl: getdpll\n"));
 1284                 if (c->type != T_SERIAL)
 1285                         return EINVAL;
 1286                 *(int*)data = c->dpll;
 1287                 return 0;
 1288 
 1289         case SERIAL_SETDPLL:
 1290                 CP_DEBUG2 (d, ("ioctl: setdpll\n"));
 1291                 /* Only for superuser! */
 1292                 error = suser (td);
 1293                 if (error)
 1294                         return error;
 1295                 if (c->type != T_SERIAL)
 1296                         return EINVAL;
 1297                 s = splimp ();
 1298                 CP_LOCK (bd);
 1299                 cp_set_dpll (c, *(int*)data);
 1300                 CP_UNLOCK (bd);
 1301                 splx (s);
 1302                 return 0;
 1303 
 1304         case SERIAL_GETNRZI:
 1305                 CP_DEBUG2 (d, ("ioctl: getnrzi\n"));
 1306                 if (c->type != T_SERIAL)
 1307                         return EINVAL;
 1308                 *(int*)data = c->nrzi;
 1309                 return 0;
 1310 
 1311         case SERIAL_SETNRZI:
 1312                 CP_DEBUG2 (d, ("ioctl: setnrzi\n"));
 1313                 /* Only for superuser! */
 1314                 error = suser (td);
 1315                 if (error)
 1316                         return error;
 1317                 if (c->type != T_SERIAL)
 1318                         return EINVAL;
 1319                 s = splimp ();
 1320                 CP_LOCK (bd);
 1321                 cp_set_nrzi (c, *(int*)data);
 1322                 CP_UNLOCK (bd);
 1323                 splx (s);
 1324                 return 0;
 1325 
 1326         case SERIAL_GETDEBUG:
 1327                 CP_DEBUG2 (d, ("ioctl: getdebug\n"));
 1328                 *(int*)data = d->chan->debug;
 1329                 return 0;
 1330 
 1331         case SERIAL_SETDEBUG:
 1332                 CP_DEBUG2 (d, ("ioctl: setdebug\n"));
 1333                 /* Only for superuser! */
 1334                 error = suser (td);
 1335                 if (error)
 1336                         return error;
 1337                 d->chan->debug = *(int*)data;
 1338 #ifndef NETGRAPH
 1339                 if (d->chan->debug)
 1340                         d->pp.pp_if.if_flags |= IFF_DEBUG;
 1341                 else
 1342                         d->pp.pp_if.if_flags &= ~IFF_DEBUG;
 1343 #endif
 1344                 return 0;
 1345 
 1346         case SERIAL_GETHIGAIN:
 1347                 CP_DEBUG2 (d, ("ioctl: gethigain\n"));
 1348                 if (c->type != T_E1)
 1349                         return EINVAL;
 1350                 *(int*)data = c->higain;
 1351                 return 0;
 1352 
 1353         case SERIAL_SETHIGAIN:
 1354                 CP_DEBUG2 (d, ("ioctl: sethigain\n"));
 1355                 /* Only for superuser! */
 1356                 error = suser (td);
 1357                 if (error)
 1358                         return error;
 1359                 if (c->type != T_E1)
 1360                         return EINVAL;
 1361                 s = splimp ();
 1362                 CP_LOCK (bd);
 1363                 cp_set_higain (c, *(int*)data);
 1364                 CP_UNLOCK (bd);
 1365                 splx (s);
 1366                 return 0;
 1367 
 1368         case SERIAL_GETPHONY:
 1369                 CP_DEBUG2 (d, ("ioctl: getphony\n"));
 1370                 if (c->type != T_E1)
 1371                         return EINVAL;
 1372                 *(int*)data = c->phony;
 1373                 return 0;
 1374 
 1375         case SERIAL_SETPHONY:
 1376                 CP_DEBUG2 (d, ("ioctl: setphony\n"));
 1377                 /* Only for superuser! */
 1378                 error = suser (td);
 1379                 if (error)
 1380                         return error;
 1381                 if (c->type != T_E1)
 1382                         return EINVAL;
 1383                 s = splimp ();
 1384                 CP_LOCK (bd);
 1385                 cp_set_phony (c, *(int*)data);
 1386                 CP_UNLOCK (bd);
 1387                 splx (s);
 1388                 return 0;
 1389 
 1390         case SERIAL_GETUNFRAM:
 1391                 CP_DEBUG2 (d, ("ioctl: getunfram\n"));
 1392                 if (c->type != T_E1)
 1393                         return EINVAL;
 1394                 *(int*)data = c->unfram;
 1395                 return 0;
 1396 
 1397         case SERIAL_SETUNFRAM:
 1398                 CP_DEBUG2 (d, ("ioctl: setunfram\n"));
 1399                 /* Only for superuser! */
 1400                 error = suser (td);
 1401                 if (error)
 1402                         return error;
 1403                 if (c->type != T_E1)
 1404                         return EINVAL;
 1405                 s = splimp ();
 1406                 CP_LOCK (bd);
 1407                 cp_set_unfram (c, *(int*)data);
 1408                 CP_UNLOCK (bd);
 1409                 splx (s);
 1410                 return 0;
 1411 
 1412         case SERIAL_GETSCRAMBLER:
 1413                 CP_DEBUG2 (d, ("ioctl: getscrambler\n"));
 1414                 if (c->type != T_G703 && !c->unfram)
 1415                         return EINVAL;
 1416                 *(int*)data = c->scrambler;
 1417                 return 0;
 1418 
 1419         case SERIAL_SETSCRAMBLER:
 1420                 CP_DEBUG2 (d, ("ioctl: setscrambler\n"));
 1421                 /* Only for superuser! */
 1422                 error = suser (td);
 1423                 if (error)
 1424                         return error;
 1425                 if (c->type != T_G703 && !c->unfram)
 1426                         return EINVAL;
 1427                 s = splimp ();
 1428                 CP_LOCK (bd);
 1429                 cp_set_scrambler (c, *(int*)data);
 1430                 CP_UNLOCK (bd);
 1431                 splx (s);
 1432                 return 0;
 1433 
 1434         case SERIAL_GETMONITOR:
 1435                 CP_DEBUG2 (d, ("ioctl: getmonitor\n"));
 1436                 if (c->type != T_E1 &&
 1437                     c->type != T_E3 &&
 1438                     c->type != T_T3 &&
 1439                     c->type != T_STS1)
 1440                         return EINVAL;
 1441                 *(int*)data = c->monitor;
 1442                 return 0;
 1443 
 1444         case SERIAL_SETMONITOR:
 1445                 CP_DEBUG2 (d, ("ioctl: setmonitor\n"));
 1446                 /* Only for superuser! */
 1447                 error = suser (td);
 1448                 if (error)
 1449                         return error;
 1450                 if (c->type != T_E1)
 1451                         return EINVAL;
 1452                 s = splimp ();
 1453                 CP_LOCK (bd);
 1454                 cp_set_monitor (c, *(int*)data);
 1455                 CP_UNLOCK (bd);
 1456                 splx (s);
 1457                 return 0;
 1458 
 1459         case SERIAL_GETUSE16:
 1460                 CP_DEBUG2 (d, ("ioctl: getuse16\n"));
 1461                 if (c->type != T_E1 || c->unfram)
 1462                         return EINVAL;
 1463                 *(int*)data = c->use16;
 1464                 return 0;
 1465 
 1466         case SERIAL_SETUSE16:
 1467                 CP_DEBUG2 (d, ("ioctl: setuse16\n"));
 1468                 /* Only for superuser! */
 1469                 error = suser (td);
 1470                 if (error)
 1471                         return error;
 1472                 if (c->type != T_E1)
 1473                         return EINVAL;
 1474                 s = splimp ();
 1475                 CP_LOCK (bd);
 1476                 cp_set_use16 (c, *(int*)data);
 1477                 CP_UNLOCK (bd);
 1478                 splx (s);
 1479                 return 0;
 1480 
 1481         case SERIAL_GETCRC4:
 1482                 CP_DEBUG2 (d, ("ioctl: getcrc4\n"));
 1483                 if (c->type != T_E1 || c->unfram)
 1484                         return EINVAL;
 1485                 *(int*)data = c->crc4;
 1486                 return 0;
 1487 
 1488         case SERIAL_SETCRC4:
 1489                 CP_DEBUG2 (d, ("ioctl: setcrc4\n"));
 1490                 /* Only for superuser! */
 1491                 error = suser (td);
 1492                 if (error)
 1493                         return error;
 1494                 if (c->type != T_E1)
 1495                         return EINVAL;
 1496                 s = splimp ();
 1497                 CP_LOCK (bd);
 1498                 cp_set_crc4 (c, *(int*)data);
 1499                 CP_UNLOCK (bd);
 1500                 splx (s);
 1501                 return 0;
 1502 
 1503         case SERIAL_GETCLK:
 1504                 CP_DEBUG2 (d, ("ioctl: getclk\n"));
 1505                 if (c->type != T_E1 &&
 1506                     c->type != T_G703 &&
 1507                     c->type != T_E3 &&
 1508                     c->type != T_T3 &&
 1509                     c->type != T_STS1)
 1510                         return EINVAL;
 1511                 switch (c->gsyn) {
 1512                 default:        *(int*)data = E1CLK_INTERNAL;           break;
 1513                 case GSYN_RCV:  *(int*)data = E1CLK_RECEIVE;            break;
 1514                 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0;      break;
 1515                 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1;      break;
 1516                 case GSYN_RCV2: *(int*)data = E1CLK_RECEIVE_CHAN2;      break;
 1517                 case GSYN_RCV3: *(int*)data = E1CLK_RECEIVE_CHAN3;      break;
 1518                 }
 1519                 return 0;
 1520 
 1521         case SERIAL_SETCLK:
 1522                 CP_DEBUG2 (d, ("ioctl: setclk\n"));
 1523                 /* Only for superuser! */
 1524                 error = suser (td);
 1525                 if (error)
 1526                         return error;
 1527                 if (c->type != T_E1 &&
 1528                     c->type != T_G703 &&
 1529                     c->type != T_E3 &&
 1530                     c->type != T_T3 &&
 1531                     c->type != T_STS1)
 1532                         return EINVAL;
 1533                 s = splimp ();
 1534                 CP_LOCK (bd);
 1535                 switch (*(int*)data) {
 1536                 default:                  cp_set_gsyn (c, GSYN_INT);  break;
 1537                 case E1CLK_RECEIVE:       cp_set_gsyn (c, GSYN_RCV);  break;
 1538                 case E1CLK_RECEIVE_CHAN0: cp_set_gsyn (c, GSYN_RCV0); break;
 1539                 case E1CLK_RECEIVE_CHAN1: cp_set_gsyn (c, GSYN_RCV1); break;
 1540                 case E1CLK_RECEIVE_CHAN2: cp_set_gsyn (c, GSYN_RCV2); break;
 1541                 case E1CLK_RECEIVE_CHAN3: cp_set_gsyn (c, GSYN_RCV3); break;
 1542                 }
 1543                 CP_UNLOCK (bd);
 1544                 splx (s);
 1545                 return 0;
 1546 
 1547         case SERIAL_GETTIMESLOTS:
 1548                 CP_DEBUG2 (d, ("ioctl: gettimeslots\n"));
 1549                 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
 1550                         return EINVAL;
 1551                 *(u_long*)data = c->ts;
 1552                 return 0;
 1553 
 1554         case SERIAL_SETTIMESLOTS:
 1555                 CP_DEBUG2 (d, ("ioctl: settimeslots\n"));
 1556                 /* Only for superuser! */
 1557                 error = suser (td);
 1558                 if (error)
 1559                         return error;
 1560                 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
 1561                         return EINVAL;
 1562                 s = splimp ();
 1563                 CP_LOCK (bd);
 1564                 cp_set_ts (c, *(u_long*)data);
 1565                 CP_UNLOCK (bd);
 1566                 splx (s);
 1567                 return 0;
 1568 
 1569         case SERIAL_GETINVCLK:
 1570                 CP_DEBUG2 (d, ("ioctl: getinvclk\n"));
 1571 #if 1
 1572                 return EINVAL;
 1573 #else
 1574                 if (c->type != T_SERIAL)
 1575                         return EINVAL;
 1576                 *(int*)data = c->invtxc;
 1577                 return 0;
 1578 #endif
 1579 
 1580         case SERIAL_SETINVCLK:
 1581                 CP_DEBUG2 (d, ("ioctl: setinvclk\n"));
 1582                 /* Only for superuser! */
 1583                 error = suser (td);
 1584                 if (error)
 1585                         return error;
 1586                 if (c->type != T_SERIAL)
 1587                         return EINVAL;
 1588                 s = splimp ();
 1589                 CP_LOCK (bd);
 1590                 cp_set_invtxc (c, *(int*)data);
 1591                 cp_set_invrxc (c, *(int*)data);
 1592                 CP_UNLOCK (bd);
 1593                 splx (s);
 1594                 return 0;
 1595 
 1596         case SERIAL_GETINVTCLK:
 1597                 CP_DEBUG2 (d, ("ioctl: getinvtclk\n"));
 1598                 if (c->type != T_SERIAL)
 1599                         return EINVAL;
 1600                 *(int*)data = c->invtxc;
 1601                 return 0;
 1602 
 1603         case SERIAL_SETINVTCLK:
 1604                 CP_DEBUG2 (d, ("ioctl: setinvtclk\n"));
 1605                 /* Only for superuser! */
 1606                 error = suser (td);
 1607                 if (error)
 1608                         return error;
 1609                 if (c->type != T_SERIAL)
 1610                         return EINVAL;
 1611                 s = splimp ();
 1612                 CP_LOCK (bd);
 1613                 cp_set_invtxc (c, *(int*)data);
 1614                 CP_UNLOCK (bd);
 1615                 splx (s);
 1616                 return 0;
 1617 
 1618         case SERIAL_GETINVRCLK:
 1619                 CP_DEBUG2 (d, ("ioctl: getinvrclk\n"));
 1620                 if (c->type != T_SERIAL)
 1621                         return EINVAL;
 1622                 *(int*)data = c->invrxc;
 1623                 return 0;
 1624 
 1625         case SERIAL_SETINVRCLK:
 1626                 CP_DEBUG2 (d, ("ioctl: setinvrclk\n"));
 1627                 /* Only for superuser! */
 1628                 error = suser (td);
 1629                 if (error)
 1630                         return error;
 1631                 if (c->type != T_SERIAL)
 1632                         return EINVAL;
 1633                 s = splimp ();
 1634                 CP_LOCK (bd);
 1635                 cp_set_invrxc (c, *(int*)data);
 1636                 CP_UNLOCK (bd);
 1637                 splx (s);
 1638                 return 0;
 1639 
 1640         case SERIAL_GETLEVEL:
 1641                 CP_DEBUG2 (d, ("ioctl: getlevel\n"));
 1642                 if (c->type != T_G703)
 1643                         return EINVAL;
 1644                 s = splimp ();
 1645                 CP_LOCK (bd);
 1646                 *(int*)data = cp_get_lq (c);
 1647                 CP_UNLOCK (bd);
 1648                 splx (s);
 1649                 return 0;
 1650 
 1651 #if 0
 1652         case SERIAL_RESET:
 1653                 CP_DEBUG2 (d, ("ioctl: reset\n"));
 1654                 /* Only for superuser! */
 1655                 error = suser (td);
 1656                 if (error)
 1657                         return error;
 1658                 s = splimp ();
 1659                 CP_LOCK (bd);
 1660                 cp_reset (c->board, 0, 0);
 1661                 CP_UNLOCK (bd);
 1662                 splx (s);
 1663                 return 0;
 1664 
 1665         case SERIAL_HARDRESET:
 1666                 CP_DEBUG2 (d, ("ioctl: hardreset\n"));
 1667                 /* Only for superuser! */
 1668                 error = suser (td);
 1669                 if (error)
 1670                         return error;
 1671                 s = splimp ();
 1672                 CP_LOCK (bd);
 1673                 /* hard_reset (c->board); */
 1674                 CP_UNLOCK (bd);
 1675                 splx (s);
 1676                 return 0;
 1677 #endif
 1678 
 1679         case SERIAL_GETCABLE:
 1680                 CP_DEBUG2 (d, ("ioctl: getcable\n"));
 1681                 if (c->type != T_SERIAL)
 1682                         return EINVAL;
 1683                 s = splimp ();
 1684                 CP_LOCK (bd);
 1685                 *(int*)data = cp_get_cable (c);
 1686                 CP_UNLOCK (bd);
 1687                 splx (s);
 1688                 return 0;
 1689 
 1690         case SERIAL_GETDIR:
 1691                 CP_DEBUG2 (d, ("ioctl: getdir\n"));
 1692                 if (c->type != T_E1 && c->type != T_DATA)
 1693                         return EINVAL;
 1694                 *(int*)data = c->dir;
 1695                 return 0;
 1696 
 1697         case SERIAL_SETDIR:
 1698                 CP_DEBUG2 (d, ("ioctl: setdir\n"));
 1699                 /* Only for superuser! */
 1700                 error = suser (td);
 1701                 if (error)
 1702                         return error;
 1703                 s = splimp ();
 1704                 CP_LOCK (bd);
 1705                 cp_set_dir (c, *(int*)data);
 1706                 CP_UNLOCK (bd);
 1707                 splx (s);
 1708                 return 0;
 1709 
 1710         case SERIAL_GETRLOOP:
 1711                 CP_DEBUG2 (d, ("ioctl: getrloop\n"));
 1712                 if (c->type != T_G703 &&
 1713                     c->type != T_E3 &&
 1714                     c->type != T_T3 &&
 1715                     c->type != T_STS1)
 1716                         return EINVAL;
 1717                 *(int*)data = cp_get_rloop (c);
 1718                 return 0;
 1719 
 1720         case SERIAL_SETRLOOP:
 1721                 CP_DEBUG2 (d, ("ioctl: setloop\n"));
 1722                 if (c->type != T_E3 && c->type != T_T3 && c->type != T_STS1)
 1723                         return EINVAL;
 1724                 /* Only for superuser! */
 1725                 error = suser (td);
 1726                 if (error)
 1727                         return error;
 1728                 s = splimp ();
 1729                 CP_LOCK (bd);
 1730                 cp_set_rloop (c, *(int*)data);
 1731                 CP_UNLOCK (bd);
 1732                 splx (s);
 1733                 return 0;
 1734 
 1735         case SERIAL_GETCABLEN:
 1736                 CP_DEBUG2 (d, ("ioctl: getcablen\n"));
 1737                 if (c->type != T_T3 && c->type != T_STS1)
 1738                         return EINVAL;
 1739                 *(int*)data = c->cablen;
 1740                 return 0;
 1741 
 1742         case SERIAL_SETCABLEN:
 1743                 CP_DEBUG2 (d, ("ioctl: setloop\n"));
 1744                 if (c->type != T_T3 && c->type != T_STS1)
 1745                         return EINVAL;
 1746                 /* Only for superuser! */
 1747                 error = suser (td);
 1748                 if (error)
 1749                         return error;
 1750                 s = splimp ();
 1751                 CP_LOCK (bd);
 1752                 cp_set_cablen (c, *(int*)data);
 1753                 CP_UNLOCK (bd);
 1754                 splx (s);
 1755                 return 0;
 1756 
 1757         case TIOCSDTR:  /* Set DTR */
 1758                 s = splimp ();
 1759                 CP_LOCK (bd);
 1760                 cp_set_dtr (c, 1);
 1761                 CP_UNLOCK (bd);
 1762                 splx (s);
 1763                 return 0;
 1764 
 1765         case TIOCCDTR:  /* Clear DTR */
 1766                 s = splimp ();
 1767                 CP_LOCK (bd);
 1768                 cp_set_dtr (c, 0);
 1769                 CP_UNLOCK (bd);
 1770                 splx (s);
 1771                 return 0;
 1772 
 1773         case TIOCMSET:  /* Set DTR/RTS */
 1774                 s = splimp ();
 1775                 CP_LOCK (bd);
 1776                 cp_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
 1777                 cp_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
 1778                 CP_UNLOCK (bd);
 1779                 splx (s);
 1780                 return 0;
 1781 
 1782         case TIOCMBIS:  /* Add DTR/RTS */
 1783                 s = splimp ();
 1784                 CP_LOCK (bd);
 1785                 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 1);
 1786                 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 1);
 1787                 CP_UNLOCK (bd);
 1788                 splx (s);
 1789                 return 0;
 1790 
 1791         case TIOCMBIC:  /* Clear DTR/RTS */
 1792                 s = splimp ();
 1793                 CP_LOCK (bd);
 1794                 if (*(int*)data & TIOCM_DTR) cp_set_dtr (c, 0);
 1795                 if (*(int*)data & TIOCM_RTS) cp_set_rts (c, 0);
 1796                 CP_UNLOCK (bd);
 1797                 splx (s);
 1798                 return 0;
 1799 
 1800         case TIOCMGET:  /* Get modem status */
 1801                 *(int*)data = cp_modem_status (c);
 1802                 return 0;
 1803         }
 1804         return ENOTTY;
 1805 }
 1806 
 1807 static struct cdevsw cp_cdevsw = {
 1808         .d_version  = D_VERSION,
 1809         .d_open     = cp_open,
 1810         .d_close    = cp_close,
 1811         .d_ioctl    = cp_ioctl,
 1812         .d_name     = "cp",
 1813         .d_maj      = CDEV_MAJOR,
 1814         .d_flags    = D_NEEDGIANT,
 1815 };
 1816 
 1817 #ifdef NETGRAPH
 1818 static int ng_cp_constructor (node_p node)
 1819 {
 1820         drv_t *d = NG_NODE_PRIVATE (node);
 1821         CP_DEBUG (d, ("Constructor\n"));
 1822         return EINVAL;
 1823 }
 1824 
 1825 static int ng_cp_newhook (node_p node, hook_p hook, const char *name)
 1826 {
 1827         int s;
 1828         drv_t *d = NG_NODE_PRIVATE (node);
 1829         bdrv_t *bd = d->board->sys;
 1830 
 1831         CP_DEBUG (d, ("Newhook\n"));
 1832         /* Attach debug hook */
 1833         if (strcmp (name, NG_CP_HOOK_DEBUG) == 0) {
 1834                 NG_HOOK_SET_PRIVATE (hook, NULL);
 1835                 d->debug_hook = hook;
 1836                 return 0;
 1837         }
 1838 
 1839         /* Check for raw hook */
 1840         if (strcmp (name, NG_CP_HOOK_RAW) != 0)
 1841                 return EINVAL;
 1842 
 1843         NG_HOOK_SET_PRIVATE (hook, d);
 1844         d->hook = hook;
 1845         s = splimp ();
 1846         CP_LOCK (bd);
 1847         cp_up (d);
 1848         CP_UNLOCK (bd);
 1849         splx (s);
 1850         return 0;
 1851 }
 1852 
 1853 static char *format_timeslots (u_long s)
 1854 {
 1855         static char buf [100];
 1856         char *p = buf;
 1857         int i;
 1858 
 1859         for (i=1; i<32; ++i)
 1860                 if ((s >> i) & 1) {
 1861                         int prev = (i > 1)  & (s >> (i-1));
 1862                         int next = (i < 31) & (s >> (i+1));
 1863 
 1864                         if (prev) {
 1865                                 if (next)
 1866                                         continue;
 1867                                 *p++ = '-';
 1868                         } else if (p > buf)
 1869                                 *p++ = ',';
 1870 
 1871                         if (i >= 10)
 1872                                 *p++ = '' + i / 10;
 1873                         *p++ = '' + i % 10;
 1874                 }
 1875         *p = 0;
 1876         return buf;
 1877 }
 1878 
 1879 static int print_modems (char *s, cp_chan_t *c, int need_header)
 1880 {
 1881         int status = cp_modem_status (c);
 1882         int length = 0;
 1883 
 1884         if (need_header)
 1885                 length += sprintf (s + length, "  LE   DTR  DSR  RTS  CTS  CD\n");
 1886         length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
 1887                 status & TIOCM_LE  ? "On" : "-",
 1888                 status & TIOCM_DTR ? "On" : "-",
 1889                 status & TIOCM_DSR ? "On" : "-",
 1890                 status & TIOCM_RTS ? "On" : "-",
 1891                 status & TIOCM_CTS ? "On" : "-",
 1892                 status & TIOCM_CD  ? "On" : "-");
 1893         return length;
 1894 }
 1895 
 1896 static int print_stats (char *s, cp_chan_t *c, int need_header)
 1897 {
 1898         int length = 0;
 1899 
 1900         if (need_header)
 1901                 length += sprintf (s + length, "  Rintr   Tintr   Mintr   Ibytes   Ipkts   Ierrs   Obytes   Opkts   Oerrs\n");
 1902         length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
 1903                 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
 1904                 c->ipkts, c->overrun + c->frame + c->crc,
 1905                 (unsigned long) c->obytes, c->opkts, c->underrun);
 1906         return length;
 1907 }
 1908 
 1909 static char *format_e1_status (u_char status)
 1910 {
 1911         static char buf [80];
 1912 
 1913         if (status & E1_NOALARM)
 1914                 return "Ok";
 1915         buf[0] = 0;
 1916         if (status & E1_LOS)     strcat (buf, ",LOS");
 1917         if (status & E1_AIS)     strcat (buf, ",AIS");
 1918         if (status & E1_LOF)     strcat (buf, ",LOF");
 1919         if (status & E1_LOMF)    strcat (buf, ",LOMF");
 1920         if (status & E1_FARLOF)  strcat (buf, ",FARLOF");
 1921         if (status & E1_AIS16)   strcat (buf, ",AIS16");
 1922         if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
 1923         if (status & E1_TSTREQ)  strcat (buf, ",TSTREQ");
 1924         if (status & E1_TSTERR)  strcat (buf, ",TSTERR");
 1925         if (buf[0] == ',')
 1926                 return buf+1;
 1927         return "Unknown";
 1928 }
 1929 
 1930 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
 1931 {
 1932         int n, length = 0;
 1933 
 1934         if (numerator < 1 || divider < 1) {
 1935                 length += sprintf (s+length, leftalign ? "/-   " : "    -");
 1936                 return length;
 1937         }
 1938         n = (int) (0.5 + 1000.0 * numerator / divider);
 1939         if (n < 1000) {
 1940                 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
 1941                 return length;
 1942         }
 1943         *(s + length) = leftalign ? '/' : ' ';
 1944         length ++;
 1945 
 1946         if      (n >= 1000000) n = (n+500) / 1000 * 1000;
 1947         else if (n >= 100000)  n = (n+50)  / 100 * 100;
 1948         else if (n >= 10000)   n = (n+5)   / 10 * 10;
 1949 
 1950         switch (n) {
 1951         case 1000:    length += printf (s+length, ".999"); return length;
 1952         case 10000:   n = 9990;   break;
 1953         case 100000:  n = 99900;  break;
 1954         case 1000000: n = 999000; break;
 1955         }
 1956         if (n < 10000)        length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
 1957         else if (n < 100000)  length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
 1958         else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
 1959         else                  length += sprintf (s+length, "%d", n/1000);
 1960 
 1961         return length;
 1962 }
 1963 
 1964 static int print_e1_stats (char *s, cp_chan_t *c)
 1965 {
 1966         struct e1_counters total;
 1967         u_long totsec;
 1968         int length = 0;
 1969 
 1970         totsec          = c->totsec + c->cursec;
 1971         total.bpv       = c->total.bpv   + c->currnt.bpv;
 1972         total.fse       = c->total.fse   + c->currnt.fse;
 1973         total.crce      = c->total.crce  + c->currnt.crce;
 1974         total.rcrce     = c->total.rcrce + c->currnt.rcrce;
 1975         total.uas       = c->total.uas   + c->currnt.uas;
 1976         total.les       = c->total.les   + c->currnt.les;
 1977         total.es        = c->total.es    + c->currnt.es;
 1978         total.bes       = c->total.bes   + c->currnt.bes;
 1979         total.ses       = c->total.ses   + c->currnt.ses;
 1980         total.oofs      = c->total.oofs  + c->currnt.oofs;
 1981         total.css       = c->total.css   + c->currnt.css;
 1982         total.dm        = c->total.dm    + c->currnt.dm;
 1983 
 1984         length += sprintf (s + length, " Unav/Degr  Bpv/Fsyn  CRC/RCRC  Err/Lerr  Sev/Bur   Oof/Slp  Status\n");
 1985 
 1986         /* Unavailable seconds, degraded minutes */
 1987         length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
 1988         length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
 1989 
 1990         /* Bipolar violations, frame sync errors */
 1991         length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
 1992         length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
 1993 
 1994         /* CRC errors, remote CRC errors (E-bit) */
 1995         length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
 1996         length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
 1997 
 1998         /* Errored seconds, line errored seconds */
 1999         length += print_frac (s + length, 0, c->currnt.es, c->cursec);
 2000         length += print_frac (s + length, 1, c->currnt.les, c->cursec);
 2001 
 2002         /* Severely errored seconds, burst errored seconds */
 2003         length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
 2004         length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
 2005 
 2006         /* Out of frame seconds, controlled slip seconds */
 2007         length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
 2008         length += print_frac (s + length, 1, c->currnt.css, c->cursec);
 2009 
 2010         length += sprintf (s + length, " %s\n", format_e1_status (c->status));
 2011 
 2012         /* Print total statistics. */
 2013         length += print_frac (s + length, 0, total.uas, totsec);
 2014         length += print_frac (s + length, 1, 60 * total.dm, totsec);
 2015 
 2016         length += print_frac (s + length, 0, total.bpv, totsec);
 2017         length += print_frac (s + length, 1, total.fse, totsec);
 2018 
 2019         length += print_frac (s + length, 0, total.crce, totsec);
 2020         length += print_frac (s + length, 1, total.rcrce, totsec);
 2021 
 2022         length += print_frac (s + length, 0, total.es, totsec);
 2023         length += print_frac (s + length, 1, total.les, totsec);
 2024 
 2025         length += print_frac (s + length, 0, total.ses, totsec);
 2026         length += print_frac (s + length, 1, total.bes, totsec);
 2027 
 2028         length += print_frac (s + length, 0, total.oofs, totsec);
 2029         length += print_frac (s + length, 1, total.css, totsec);
 2030 
 2031         length += sprintf (s + length, " -- Total\n");
 2032         return length;
 2033 }
 2034 
 2035 static int print_chan (char *s, cp_chan_t *c)
 2036 {
 2037         drv_t *d = c->sys;
 2038         bdrv_t *bd = d->board->sys;
 2039         int length = 0;
 2040 
 2041         length += sprintf (s + length, "cp%d", c->board->num * NCHAN + c->num);
 2042         if (d->chan->debug)
 2043                 length += sprintf (s + length, " debug=%d", d->chan->debug);
 2044 
 2045         if (c->board->mux) {
 2046                 length += sprintf (s + length, " cfg=C");
 2047         } else {
 2048                 length += sprintf (s + length, " cfg=A");
 2049         }
 2050 
 2051         if (c->baud)
 2052                 length += sprintf (s + length, " %ld", c->baud);
 2053         else
 2054                 length += sprintf (s + length, " extclock");
 2055 
 2056         if (c->type == T_E1 || c->type == T_G703)
 2057                 switch (c->gsyn) {
 2058                 case GSYN_INT   : length += sprintf (s + length, " syn=int");     break;
 2059                 case GSYN_RCV   : length += sprintf (s + length, " syn=rcv");     break;
 2060                 case GSYN_RCV0  : length += sprintf (s + length, " syn=rcv0");    break;
 2061                 case GSYN_RCV1  : length += sprintf (s + length, " syn=rcv1");    break;
 2062                 case GSYN_RCV2  : length += sprintf (s + length, " syn=rcv2");    break;
 2063                 case GSYN_RCV3  : length += sprintf (s + length, " syn=rcv3");    break;
 2064                 }
 2065         if (c->type == T_SERIAL) {
 2066                 length += sprintf (s + length, " dpll=%s",   c->dpll   ? "on" : "off");
 2067                 length += sprintf (s + length, " nrzi=%s",   c->nrzi   ? "on" : "off");
 2068                 length += sprintf (s + length, " invclk=%s", c->invtxc ? "on" : "off");
 2069         }
 2070         if (c->type == T_E1)
 2071                 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
 2072 
 2073         length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
 2074 
 2075         if (c->type == T_E1)
 2076                 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
 2077         if (c->type == T_G703) {
 2078                 int lq, x;
 2079 
 2080                 x = splimp ();
 2081                 CP_LOCK (bd);
 2082                 lq = cp_get_lq (c);
 2083                 CP_UNLOCK (bd);
 2084                 splx (x);
 2085                 length += sprintf (s + length, " (level=-%.1fdB)", lq / 10.0);
 2086         }
 2087         length += sprintf (s + length, "\n");
 2088         return length;
 2089 }
 2090 
 2091 static int ng_cp_rcvmsg (node_p node, item_p item, hook_p lasthook)
 2092 {
 2093         drv_t *d = NG_NODE_PRIVATE (node);
 2094         struct ng_mesg *msg;
 2095         struct ng_mesg *resp = NULL;
 2096         int error = 0;
 2097 
 2098         CP_DEBUG (d, ("Rcvmsg\n"));
 2099         NGI_GET_MSG (item, msg);
 2100         switch (msg->header.typecookie) {
 2101         default:
 2102                 error = EINVAL;
 2103                 break;
 2104 
 2105         case NGM_CP_COOKIE:
 2106                 printf ("Not implemented yet\n");
 2107                 error = EINVAL;
 2108                 break;
 2109 
 2110         case NGM_GENERIC_COOKIE:
 2111                 switch (msg->header.cmd) {
 2112                 default:
 2113                         error = EINVAL;
 2114                         break;
 2115 
 2116                 case NGM_TEXT_STATUS: {
 2117                         char *s;
 2118                         int l = 0;
 2119                         int dl = sizeof (struct ng_mesg) + 730;
 2120 
 2121                         NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
 2122                         if (! resp) {
 2123                                 error = ENOMEM;
 2124                                 break;
 2125                         }
 2126                         s = (resp)->data;
 2127                         if (d) {
 2128                         l += print_chan (s + l, d->chan);
 2129                         l += print_stats (s + l, d->chan, 1);
 2130                         l += print_modems (s + l, d->chan, 1);
 2131                         l += print_e1_stats (s + l, d->chan);
 2132                         } else
 2133                                 l += sprintf (s + l, "Error: node not connect to channel");
 2134                         strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
 2135                         }
 2136                         break;
 2137                 }
 2138                 break;
 2139         }
 2140         NG_RESPOND_MSG (error, node, item, resp);
 2141         NG_FREE_MSG (msg);
 2142         return error;
 2143 }
 2144 
 2145 static int ng_cp_rcvdata (hook_p hook, item_p item)
 2146 {
 2147         drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
 2148         struct mbuf *m;
 2149         struct ng_tag_prio *ptag;
 2150         bdrv_t *bd = d->board->sys;
 2151         struct ifqueue *q;
 2152         int s;
 2153 
 2154         CP_DEBUG2 (d, ("Rcvdata\n"));
 2155         NGI_GET_M (item, m);
 2156         NG_FREE_ITEM (item);
 2157         if (! NG_HOOK_PRIVATE (hook) || ! d) {
 2158                 NG_FREE_M (m);
 2159                 return ENETDOWN;
 2160         }
 2161 
 2162         /* Check for high priority data */
 2163         if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
 2164             NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
 2165                 q = &d->hi_queue;
 2166         else
 2167                 q = &d->queue;
 2168 
 2169         s = splimp ();
 2170         CP_LOCK (bd);
 2171         IF_LOCK (q);
 2172         if (_IF_QFULL (q)) {
 2173                 _IF_DROP (q);
 2174                 IF_UNLOCK (q);
 2175                 CP_UNLOCK (bd);
 2176                 splx (s);
 2177                 NG_FREE_M (m);
 2178                 return ENOBUFS;
 2179         }
 2180         _IF_ENQUEUE (q, m);
 2181         IF_UNLOCK (q);
 2182         cp_start (d);
 2183         CP_UNLOCK (bd);
 2184         splx (s);
 2185         return 0;
 2186 }
 2187 
 2188 static int ng_cp_rmnode (node_p node)
 2189 {
 2190         drv_t *d = NG_NODE_PRIVATE (node);
 2191 
 2192         CP_DEBUG (d, ("Rmnode\n"));
 2193         if (d && d->running) {
 2194                 bdrv_t *bd = d->board->sys;
 2195                 int s = splimp ();
 2196                 CP_LOCK (bd);
 2197                 cp_down (d);
 2198                 CP_UNLOCK (bd);
 2199                 splx (s);
 2200         }
 2201 #ifdef  KLD_MODULE
 2202         if (node->nd_flags & NGF_REALLY_DIE) {
 2203                 NG_NODE_SET_PRIVATE (node, NULL);
 2204                 NG_NODE_UNREF (node);
 2205         }
 2206         NG_NODE_REVIVE(node);           /* Persistant node */
 2207 #endif
 2208         return 0;
 2209 }
 2210 
 2211 static void ng_cp_watchdog (void *arg)
 2212 {
 2213         drv_t *d = arg;
 2214 
 2215         if (d) {
 2216                 if (d->timeout == 1)
 2217                         cp_watchdog (d);
 2218                 if (d->timeout)
 2219                         d->timeout--;
 2220                 callout_reset (&d->timeout_handle, hz, ng_cp_watchdog, d);
 2221         }
 2222 }
 2223 
 2224 static int ng_cp_connect (hook_p hook)
 2225 {
 2226         drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
 2227 
 2228         if (d) {
 2229                 CP_DEBUG (d, ("Connect\n"));
 2230                 callout_reset (&d->timeout_handle, hz, ng_cp_watchdog, d);
 2231         }
 2232         
 2233         return 0;
 2234 }
 2235 
 2236 static int ng_cp_disconnect (hook_p hook)
 2237 {
 2238         drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
 2239 
 2240         if (d) {
 2241                 CP_DEBUG (d, ("Disconnect\n"));
 2242                 if (NG_HOOK_PRIVATE (hook))
 2243                 {
 2244                         bdrv_t *bd = d->board->sys;
 2245                         int s = splimp ();
 2246                         CP_LOCK (bd);
 2247                         cp_down (d);
 2248                         CP_UNLOCK (bd);
 2249                         splx (s);
 2250                 }
 2251                 /* If we were wait it than it reasserted now, just stop it. */
 2252                 if (!callout_drain (&d->timeout_handle))
 2253                         callout_stop (&d->timeout_handle);
 2254         }
 2255         return 0;
 2256 }
 2257 #endif
 2258 
 2259 static int cp_modevent (module_t mod, int type, void *unused)
 2260 {
 2261         static int load_count = 0;
 2262 
 2263         if (!debug_mpsafenet && cp_mpsafenet) {
 2264                 printf ("WORNING! Network stack is not MPSAFE. "
 2265                         "Turning off debug.cp.mpsafenet.\n");
 2266                 cp_mpsafenet = 0;
 2267         }
 2268         if (cp_mpsafenet)
 2269                 cp_cdevsw.d_flags &= ~D_NEEDGIANT;
 2270 
 2271         switch (type) {
 2272         case MOD_LOAD:
 2273 #ifdef NETGRAPH
 2274                 if (ng_newtype (&typestruct))
 2275                         printf ("Failed to register ng_cp\n");
 2276 #endif
 2277                 ++load_count;
 2278                 callout_init (&timeout_handle, cp_mpsafenet?CALLOUT_MPSAFE:0);
 2279                 callout_reset (&timeout_handle, hz*5, cp_timeout, 0);
 2280                 break;
 2281         case MOD_UNLOAD:
 2282                 if (load_count == 1) {
 2283                         printf ("Removing device entry for Tau-PCI\n");
 2284 #ifdef NETGRAPH
 2285                         ng_rmtype (&typestruct);
 2286 #endif                  
 2287                 }
 2288                 /* If we were wait it than it reasserted now, just stop it.
 2289                  * Actually we shouldn't get this condition. But code could be
 2290                  * changed in the future, so just be a litle paranoid.
 2291                  */
 2292                 if (!callout_drain (&timeout_handle))
 2293                         callout_stop (&timeout_handle);
 2294                 --load_count;
 2295                 break;
 2296         case MOD_SHUTDOWN:
 2297                 break;
 2298         }
 2299         return 0;
 2300 }
 2301 
 2302 #ifdef NETGRAPH
 2303 static struct ng_type typestruct = {
 2304         .version        = NG_ABI_VERSION,
 2305         .name           = NG_CP_NODE_TYPE,
 2306         .constructor    = ng_cp_constructor,
 2307         .rcvmsg         = ng_cp_rcvmsg,
 2308         .shutdown       = ng_cp_rmnode,
 2309         .newhook        = ng_cp_newhook,
 2310         .connect        = ng_cp_connect,
 2311         .rcvdata        = ng_cp_rcvdata,
 2312         .disconnect     = ng_cp_disconnect,
 2313 };
 2314 #endif /*NETGRAPH*/
 2315 
 2316 #ifdef NETGRAPH
 2317 MODULE_DEPEND (ng_cp, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
 2318 #else
 2319 MODULE_DEPEND (cp, sppp, 1, 1, 1);
 2320 #endif
 2321 DRIVER_MODULE (cp, pci, cp_driver, cp_devclass, cp_modevent, NULL);
 2322 MODULE_VERSION (cp, 1);

Cache object: 7a5ba840c5ab00258f9b3acefabbe13c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.