2 * Cronyx-Tau32-PCI adapter driver for FreeBSD.
4 * Copyright (C) 2003-2005 Cronyx Engineering.
5 * Copyright (C) 2003-2005 Kurakin Roman, <rik@FreeBSD.org>
7 * This software is distributed with NO WARRANTIES, not even the implied
8 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 * Authors grant any other persons or organisations a permission to use,
11 * modify and redistribute this software in source and binary forms,
12 * as long as this message is kept with the software, all derivative
13 * works or modified versions.
15 * $Cronyx: if_ce.c,v 1.9.2.8 2005/11/21 14:17:44 rik Exp $
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
21 #include <sys/param.h>
23 #if __FreeBSD_version >= 500000
31 #include <sys/ucred.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #if __FreeBSD_version >= 504000
43 #include <sys/sysctl.h>
50 #if __FreeBSD_version > 501000
51 # include <dev/pci/pcivar.h>
52 # include <dev/pci/pcireg.h>
54 # include <pci/pcivar.h>
55 # include <pci/pcireg.h>
57 #include <machine/bus.h>
59 #include "opt_ng_cronyx.h"
60 #ifdef NETGRAPH_CRONYX
61 # include "opt_netgraph.h"
63 # error #option NETGRAPH missed from configuration
65 # include <netgraph/ng_message.h>
66 # include <netgraph/netgraph.h>
67 # include <dev/ce/ng_ce.h>
69 # include <net/if_types.h>
70 # include <net/if_sppp.h>
71 # define PP_CISCO IFF_LINK2
72 # if __FreeBSD_version < 500000
74 # define NBPFILTER NBPF
77 # define NBPFILTER DEV_BPF
81 #include <dev/cx/machdep.h>
82 #include <dev/ce/ceddk.h>
83 #include <machine/cserial.h>
84 #include <machine/resource.h>
85 #include <machine/pmap.h>
87 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
93 #define IFP2SP(ifp) ((struct sppp*)ifp)
96 #define SP2IFP(sp) ((struct ifnet*)sp)
100 #define PCIR_BAR(x) (PCIR_MAPS + (x) * 4)
103 /* define as our previous return value */
104 #ifndef BUS_PROBE_DEFAULT
105 #define BUS_PROBE_DEFAULT 0
108 #define CE_DEBUG(d,s) ({if (d->chan->debug) {\
109 printf ("%s: ", d->name); printf s;}})
110 #define CE_DEBUG2(d,s) ({if (d->chan->debug>1) {\
111 printf ("%s: ", d->name); printf s;}})
113 #ifndef CALLOUT_MPSAFE
114 #define CALLOUT_MPSAFE 0
118 #define IF_DRAIN(ifq) do { \
121 IF_DEQUEUE(ifq, m); \
130 #define _IF_QLEN(ifq) ((ifq)->ifq_len)
133 #ifndef callout_drain
134 #define callout_drain callout_stop
137 #if __FreeBSD_version >= 504000
138 #define CE_LOCK_NAME "ceX"
140 static int ce_mpsafenet = 1;
141 TUNABLE_INT("debug.ce.mpsafenet", &ce_mpsafenet);
142 SYSCTL_NODE(_debug, OID_AUTO, ce, CTLFLAG_RD, 0, "Cronyx Tau32-PCI Adapters");
143 SYSCTL_INT(_debug_ce, OID_AUTO, mpsafenet, CTLFLAG_RD, &ce_mpsafenet, 0,
144 "Enable/disable MPSAFE network support for Cronyx Tau32-PCI Adapters");
146 #define CE_LOCK(_bd) do { \
148 mtx_lock (&(_bd)->ce_mtx); \
150 #define CE_UNLOCK(_bd) do { \
152 mtx_unlock (&(_bd)->ce_mtx); \
155 #define CE_LOCK_ASSERT(_bd) do { \
157 mtx_assert (&(_bd)->ce_mtx, MA_OWNED); \
160 static int ce_mpsafenet = 0;
162 #define CE_LOCK(_bd) do {} while (0 && (_bd) && ce_mpsafenet)
163 #define CE_UNLOCK(_bd) do {} while (0 && (_bd) && ce_mpsafenet)
164 #define CE_LOCK_ASSERT(_bd) do {} while (0 && (_bd) && ce_mpsafenet)
167 #define CDEV_MAJOR 185
169 static int ce_probe __P((device_t));
170 static int ce_attach __P((device_t));
171 static int ce_detach __P((device_t));
173 static device_method_t ce_methods[] = {
174 /* Device interface */
175 DEVMETHOD(device_probe, ce_probe),
176 DEVMETHOD(device_attach, ce_attach),
177 DEVMETHOD(device_detach, ce_detach),
182 typedef struct _ce_dma_mem_t {
186 #if __FreeBSD_version >= 500000
192 typedef struct _drv_t {
197 struct ifqueue rqueue;
199 char nodename [NG_NODELEN+1];
203 struct ifqueue queue;
204 struct ifqueue hi_queue;
206 struct callout timeout_handle;
210 #if __FreeBSD_version >= 500000
212 #else /* __FreeBSD_version < 500000 */
218 typedef struct _bdrv_t {
220 struct resource *ce_res;
221 struct resource *ce_irq;
224 drv_t channel [NCHAN];
225 #if __FreeBSD_version >= 504000
230 static driver_t ce_driver = {
236 static devclass_t ce_devclass;
238 static void ce_receive (ce_chan_t *c, unsigned char *data, int len);
239 static void ce_transmit (ce_chan_t *c, void *attachment, int len);
240 static void ce_error (ce_chan_t *c, int data);
241 static void ce_up (drv_t *d);
242 static void ce_start (drv_t *d);
243 static void ce_down (drv_t *d);
244 static void ce_watchdog (drv_t *d);
246 extern struct ng_type typestruct;
248 static void ce_ifstart (struct ifnet *ifp);
249 static void ce_tlf (struct sppp *sp);
250 static void ce_tls (struct sppp *sp);
251 static void ce_ifwatchdog (struct ifnet *ifp);
252 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
253 static void ce_initialize (void *softc);
256 static ce_board_t *adapter [NBRD];
257 static drv_t *channel [NBRD*NCHAN];
258 static struct callout led_timo [NBRD];
259 static struct callout timeout_handle;
261 static int ce_destroy = 0;
263 #if __FreeBSD_version < 500000
264 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p);
265 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p);
266 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
268 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
269 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
270 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
272 #if __FreeBSD_version < 500000
273 static struct cdevsw ce_cdevsw = {
274 ce_open, ce_close, noread, nowrite,
275 ce_ioctl, nopoll, nommap, nostrategy,
276 "ce", CDEV_MAJOR, nodump, nopsize,
279 #elif __FreeBSD_version == 500000
280 static struct cdevsw ce_cdevsw = {
281 ce_open, ce_close, noread, nowrite,
282 ce_ioctl, nopoll, nommap, nostrategy,
283 "ce", CDEV_MAJOR, nodump, nopsize,
286 #elif __FreeBSD_version <= 501000
287 static struct cdevsw ce_cdevsw = {
295 .d_strategy = nostrategy,
301 #elif __FreeBSD_version < 502103
302 static struct cdevsw ce_cdevsw = {
310 #elif __FreeBSD_version < 600000
311 static struct cdevsw ce_cdevsw = {
312 .d_version = D_VERSION,
318 .d_flags = D_NEEDGIANT,
320 #else /* __FreeBSD_version >= 600000 */
321 static struct cdevsw ce_cdevsw = {
322 .d_version = D_VERSION,
327 .d_flags = D_NEEDGIANT,
332 * Print the mbuf chain, for debug purposes only.
334 static void printmbuf (struct mbuf *m)
337 for (; m; m=m->m_next) {
338 if (m->m_flags & M_PKTHDR)
339 printf (" HDR %d:", m->m_pkthdr.len);
340 if (m->m_flags & M_EXT)
342 printf (" %d", m->m_len);
348 * Make an mbuf from data.
350 static struct mbuf *makembuf (void *buf, unsigned len)
354 MGETHDR (m, M_DONTWAIT, MT_DATA);
357 MCLGET (m, M_DONTWAIT);
358 if (! (m->m_flags & M_EXT)) {
362 m->m_pkthdr.len = m->m_len = len;
363 bcopy (buf, mtod (m, caddr_t), len);
367 static int ce_probe (device_t dev)
369 if ((pci_get_vendor (dev) == TAU32_PCI_VENDOR_ID) &&
370 (pci_get_device (dev) == TAU32_PCI_DEVICE_ID)) {
371 device_set_desc (dev, "Cronyx-Tau32-PCI serial adapter");
372 return BUS_PROBE_DEFAULT;
377 static void ce_timeout (void *arg)
382 for (i = 0; i < NBRD; ++i) {
383 if (adapter[i] == NULL)
385 for (k = 0; k < NCHAN; ++k) {
391 d = channel[i * NCHAN + k];
396 CE_LOCK ((bdrv_t *)d->board->sys);
397 switch (d->chan->type) {
399 ce_e1_timer (d->chan);
404 CE_UNLOCK ((bdrv_t *)d->board->sys);
410 callout_reset (&timeout_handle, hz, ce_timeout, 0);
414 static void ce_led_off (void *arg)
417 bdrv_t *bd = (bdrv_t *) b->sys;
425 TAU32_LedSet (b->ddk.pControllerObject, 0);
430 static void ce_intr (void *arg)
433 ce_board_t *b = bd->board;
436 #if __FreeBSD_version >= 500000 && defined NETGRAPH
446 TAU32_LedSet (b->ddk.pControllerObject, 1);
448 TAU32_HandleInterrupt (b->ddk.pControllerObject);
450 /* Turn LED off 50 msec later. */
451 callout_reset (&led_timo[b->num], hz/20, ce_led_off, b);
455 /* Pass packets in a lock-free state */
456 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
457 drv_t *d = b->chan[i].sys;
459 if (!d || !d->running)
461 while (_IF_QLEN(&d->rqueue)) {
462 IF_DEQUEUE (&d->rqueue,m);
467 #if __FreeBSD_version >= 500000
468 NG_SEND_DATA_ONLY (error, d->hook, m);
470 ng_queue_data (d->hook, m, 0);
473 IF_DRAIN (&d->rqueue);
476 sppp_input (d->ifp, m);
482 #if __FreeBSD_version >= 500000
484 ce_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
491 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
493 *addr = segs->ds_addr;
497 #define BUS_DMA_ZERO 0
501 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
505 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
506 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
508 #if __FreeBSD_version >= 502000
513 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
514 else printf ("ce%d: ", bnum);
515 printf ("couldn't allocate tag for dma memory\n");
518 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
519 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
521 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
522 else printf ("ce%d: ", bnum);
523 printf ("couldn't allocate mem for dma memory\n");
524 bus_dma_tag_destroy (dmem->dmat);
527 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
528 dmem->size, ce_bus_dmamap_addr, &dmem->phys, 0);
530 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
531 else printf ("ce%d: ", bnum);
532 printf ("couldn't load mem map for dma memory\n");
533 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
534 bus_dma_tag_destroy (dmem->dmat);
537 #if __FreeBSD_version >= 502000
538 bzero (dmem->virt, dmem->size);
544 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
546 bus_dmamap_unload (dmem->dmat, dmem->mapp);
547 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
548 bus_dma_tag_destroy (dmem->dmat);
552 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
554 dmem->virt = contigmalloc (dmem->size, M_DEVBUF, M_WAITOK,
555 0x100000, 0xffffffff, 16, 0);
556 if (dmem->virt == NULL) {
557 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
558 else printf ("ce%d: ", bnum);
559 printf ("couldn't allocate dma memory\n");
562 dmem->phys = vtophys (dmem->virt);
563 bzero (dmem->virt, dmem->size);
568 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
570 contigfree (dmem->virt, dmem->size, M_DEVBUF);
575 * Called if the probe succeeded.
577 static int ce_attach (device_t dev)
579 bdrv_t *bd = device_get_softc (dev);
580 int unit = device_get_unit (dev);
581 #if __FreeBSD_version >= 504000
582 char *ce_ln = CE_LOCK_NAME;
591 b = malloc (sizeof(ce_board_t), M_DEVBUF, M_WAITOK);
593 printf ("ce%d: couldn't allocate memory\n", unit);
596 bzero (b, sizeof(ce_board_t));
600 #if __FreeBSD_version >= 440000
601 pci_enable_busmaster (dev);
604 bd->dmamem.size = TAU32_ControllerObjectSize;
605 if (! ce_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
609 b->ddk.pControllerObject = bd->dmamem.virt;
614 bd->ce_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
615 0, ~0, 1, RF_ACTIVE);
617 printf ("ce%d: cannot map memory\n", unit);
618 ce_bus_dma_mem_free (&bd->dmamem);
622 vbase = (vm_offset_t) rman_get_virtual (bd->ce_res);
624 b->ddk.PciBar1VirtualAddress = (void *)vbase;
625 b->ddk.ControllerObjectPhysicalAddress = bd->dmamem.phys;
626 b->ddk.pErrorNotifyCallback = ce_error_callback;
627 b->ddk.pStatusNotifyCallback = ce_status_callback;
630 TAU32_BeforeReset(&b->ddk);
631 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_ON, 4);
632 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_OFF, 4);
634 if(!TAU32_Initialize(&b->ddk, 0))
636 printf ("ce%d: init adapter error 0x%08x, bus dead bits 0x%08lx\n",
637 unit, b->ddk.InitErrors, b->ddk.DeadBits);
638 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
639 ce_bus_dma_mem_free (&bd->dmamem);
649 bd->ce_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
650 RF_SHAREABLE | RF_ACTIVE);
652 printf ("ce%d: cannot map interrupt\n", unit);
653 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
654 ce_bus_dma_mem_free (&bd->dmamem);
659 #if __FreeBSD_version >= 500000
660 callout_init (&led_timo[unit], ce_mpsafenet ? CALLOUT_MPSAFE : 0);
662 callout_init (&led_timo[unit]);
664 error = bus_setup_intr (dev, bd->ce_irq,
665 #if __FreeBSD_version >= 500013
666 INTR_TYPE_NET|(ce_mpsafenet?INTR_MPSAFE:0),
670 ce_intr, bd, &bd->ce_intrhand);
672 printf ("ce%d: cannot set up irq\n", unit);
673 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
674 bus_release_resource (dev, SYS_RES_MEMORY,
675 PCIR_BAR(0), bd->ce_res);
676 ce_bus_dma_mem_free (&bd->dmamem);
682 switch (b->ddk.Model) {
683 case 1: strcpy (b->name, TAU32_BASE_NAME); break;
684 case 2: strcpy (b->name, TAU32_LITE_NAME); break;
685 case 3: strcpy (b->name, TAU32_ADPCM_NAME); break;
686 default: strcpy (b->name, TAU32_UNKNOWN_NAME); break;
689 printf ("ce%d: %s\n", unit, b->name);
691 for (c = b->chan; c < b->chan + NCHAN; ++c) {
692 c->num = (c - b->chan);
695 d = &bd->channel[c->num];
696 d->dmamem.size = sizeof(ce_buf_t);
697 if (! ce_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
700 channel [b->num * NCHAN + c->num] = d;
701 sprintf (d->name, "ce%d.%d", b->num, c->num);
707 for (c = b->chan; c < b->chan + NCHAN; ++c) {
713 if (ng_make_node_common (&typestruct, &d->node) != 0) {
714 printf ("%s: cannot make common node\n", d->name);
718 #if __FreeBSD_version >= 500000
719 NG_NODE_SET_PRIVATE (d->node, d);
720 callout_init (&d->timeout_handle,
721 ce_mpsafenet ? CALLOUT_MPSAFE : 0);
723 d->node->private = d;
725 sprintf (d->nodename, "%s%d", NG_CE_NODE_TYPE,
726 c->board->num * NCHAN + c->num);
727 if (ng_name_node (d->node, d->nodename)) {
728 printf ("%s: cannot name node\n", d->nodename);
729 #if __FreeBSD_version >= 500000
730 NG_NODE_UNREF (d->node);
737 d->queue.ifq_maxlen = IFQ_MAXLEN;
738 d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
739 d->rqueue.ifq_maxlen = IFQ_MAXLEN;
740 #if __FreeBSD_version >= 500000
741 mtx_init (&d->queue.ifq_mtx, "ce_queue", NULL, MTX_DEF);
742 mtx_init (&d->hi_queue.ifq_mtx, "ce_queue_hi", NULL, MTX_DEF);
743 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
746 #if __FreeBSD_version >= 600031
747 d->ifp = if_alloc(IFT_PPP);
749 d->ifp = malloc (sizeof(struct sppp), M_DEVBUF, M_WAITOK);
750 bzero (d->ifp, sizeof(struct sppp));
753 printf ("%s: cannot if_alloc() interface\n", d->name);
756 d->ifp->if_softc = d;
757 #if __FreeBSD_version > 501000
758 if_initname (d->ifp, "ce", b->num * NCHAN + c->num);
760 d->ifp->if_unit = b->num * NCHAN + c->num;
761 d->ifp->if_name = "ce";
763 d->ifp->if_mtu = PP_MTU;
764 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
765 #if __FreeBSD_version >= 502125
767 d->ifp->if_flags |= IFF_NEEDSGIANT;
769 d->ifp->if_ioctl = ce_sioctl;
770 d->ifp->if_start = ce_ifstart;
771 d->ifp->if_watchdog = ce_ifwatchdog;
772 d->ifp->if_init = ce_initialize;
773 d->rqueue.ifq_maxlen = IFQ_MAXLEN;
774 #if __FreeBSD_version >= 500000
775 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
777 sppp_attach (d->ifp);
779 IFP2SP(d->ifp)->pp_tlf = ce_tlf;
780 IFP2SP(d->ifp)->pp_tls = ce_tls;
781 /* If BPF is in the kernel, call the attach for it.
782 * The header size of PPP or Cisco/HDLC is 4 bytes. */
783 bpfattach (d->ifp, DLT_PPP, 4);
785 ce_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
787 /* Register callback functions. */
788 ce_register_transmit (c, &ce_transmit);
789 ce_register_receive (c, &ce_receive);
790 ce_register_error (c, &ce_error);
791 d->devt = make_dev (&ce_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
792 GID_WHEEL, 0600, "ce%d", b->num*NCHAN+c->num);
795 #if __FreeBSD_version >= 504000
796 ce_ln[2] = '0' + unit;
797 mtx_init (&bd->ce_mtx, ce_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
800 TAU32_EnableInterrupts(b->ddk.pControllerObject);
808 static int ce_detach (device_t dev)
810 bdrv_t *bd = device_get_softc (dev);
811 ce_board_t *b = bd->board;
815 #if __FreeBSD_version >= 504000
816 KASSERT (mtx_initialized (&bd->ce_mtx), ("ce mutex not initialized"));
820 /* Check if the device is busy (open). */
821 for (c = b->chan; c < b->chan + NCHAN; ++c) {
822 drv_t *d = (drv_t*) c->sys;
824 /* XXX Non existen chan! */
825 if (! d || ! d->chan)
834 /* Ok, we can unload driver */
835 /* At first we should disable interrupts */
837 TAU32_DisableInterrupts(b->ddk.pControllerObject);
839 callout_stop (&led_timo[b->num]);
841 for (c = b->chan; c < b->chan + NCHAN; ++c) {
842 drv_t *d = (drv_t*) c->sys;
844 if (! d || ! d->chan)
847 #if __FreeBSD_version >= 410000 && NBPFILTER > 0
848 /* Detach from the packet filter list of interfaces. */
851 /* Detach from the sync PPP list. */
852 sppp_detach (d->ifp);
854 /* Detach from the system list of interfaces. */
856 #if __FreeBSD_version > 600031
859 free (d->ifp, M_DEVBUF);
862 IF_DRAIN (&d->rqueue);
863 #if __FreeBSD_version >= 500000
864 mtx_destroy (&d->rqueue.ifq_mtx);
867 #if __FreeBSD_version >= 500000
869 ng_rmnode_self (d->node);
870 NG_NODE_UNREF (d->node);
873 IF_DRAIN (&d->rqueue);
874 mtx_destroy (&d->queue.ifq_mtx);
875 mtx_destroy (&d->hi_queue.ifq_mtx);
876 mtx_destroy (&d->rqueue.ifq_mtx);
882 destroy_dev (d->devt);
888 callout_drain (&led_timo[b->num]);
890 /* Disable the interrupt request. */
891 bus_teardown_intr (dev, bd->ce_irq, bd->ce_intrhand);
892 bus_deactivate_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
893 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
894 TAU32_DestructiveHalt (b->ddk.pControllerObject, 0);
895 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
899 for (c = b->chan; c < b->chan + NCHAN; ++c) {
900 drv_t *d = (drv_t*) c->sys;
902 if (! d || ! d->chan)
904 channel [b->num * NCHAN + c->num] = 0;
905 /* Deallocate buffers. */
906 ce_bus_dma_mem_free (&d->dmamem);
908 adapter [b->num] = 0;
909 ce_bus_dma_mem_free (&bd->dmamem);
913 #if __FreeBSD_version >= 504000
914 mtx_destroy (&bd->ce_mtx);
920 static void ce_ifstart (struct ifnet *ifp)
922 drv_t *d = ifp->if_softc;
923 bdrv_t *bd = d->board->sys;
930 static void ce_ifwatchdog (struct ifnet *ifp)
932 drv_t *d = ifp->if_softc;
937 static void ce_tlf (struct sppp *sp)
939 drv_t *d = SP2IFP(sp)->if_softc;
941 CE_DEBUG2 (d, ("ce_tlf\n"));
945 static void ce_tls (struct sppp *sp)
947 drv_t *d = SP2IFP(sp)->if_softc;
949 CE_DEBUG2 (d, ("ce_tls\n"));
954 * Process an ioctl request.
956 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
958 drv_t *d = ifp->if_softc;
959 bdrv_t *bd = d->board->sys;
960 int error, s, was_up, should_be_up;
962 #if __FreeBSD_version >= 600034
963 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
965 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
967 error = sppp_ioctl (ifp, cmd, data);
972 if (! (ifp->if_flags & IFF_DEBUG))
974 else if (! d->chan->debug)
978 default: CE_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
979 case SIOCADDMULTI: CE_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
980 case SIOCDELMULTI: CE_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
981 case SIOCSIFFLAGS: CE_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
982 case SIOCSIFADDR: CE_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
985 /* We get here only in case of SIFFLAGS or SIFADDR. */
988 #if __FreeBSD_version >= 600034
989 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
991 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
993 if (! was_up && should_be_up) {
994 /* Interface goes up -- start it. */
997 } else if (was_up && ! should_be_up) {
998 /* Interface is going down -- stop it. */
999 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
1002 CE_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
1009 * Initialization of interface.
1010 * It seems to be never called by upper level?
1012 static void ce_initialize (void *softc)
1016 CE_DEBUG (d, ("ce_initialize\n"));
1021 * Stop the interface. Called on splimp().
1023 static void ce_down (drv_t *d)
1025 CE_DEBUG (d, ("ce_down\n"));
1026 /* Interface is going down -- stop it. */
1027 ce_set_dtr (d->chan, 0);
1028 ce_set_rts (d->chan, 0);
1034 * Start the interface. Called on splimp().
1036 static void ce_up (drv_t *d)
1038 CE_DEBUG (d, ("ce_up\n"));
1039 ce_set_dtr (d->chan, 1);
1040 ce_set_rts (d->chan, 1);
1046 * Start output on the interface. Get another datagram to send
1047 * off of the interface queue, and copy it to the interface
1048 * before starting the output.
1050 static void ce_send (drv_t *d)
1055 CE_DEBUG2 (d, ("ce_send\n"));
1057 /* No output if the interface is down. */
1061 while (ce_transmit_space (d->chan)) {
1062 /* Get the packet to send. */
1064 IF_DEQUEUE (&d->hi_queue, m);
1066 IF_DEQUEUE (&d->queue, m);
1068 m = sppp_dequeue (d->ifp);
1074 #if __FreeBSD_version >= 500000
1075 BPF_MTAP (d->ifp, m);
1077 bpf_mtap (d->ifp, m);
1080 #if __FreeBSD_version >= 490000
1081 len = m_length (m, NULL);
1083 len = m->m_pkthdr.len;
1086 printf ("%s: too long packet: %d bytes: ",
1088 else if (! m->m_next)
1089 ce_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
1091 ce_buf_item_t *item = (ce_buf_item_t*)d->chan->tx_queue;
1092 m_copydata (m, 0, len, item->buf);
1093 ce_send_packet (d->chan, item->buf, len, 0);
1096 /* Set up transmit timeout, if the transmit ring is not empty.*/
1100 d->ifp->if_timer = 10;
1104 #if __FreeBSD_version >= 600034
1105 d->ifp->if_flags |= IFF_DRV_OACTIVE;
1107 d->ifp->if_flags |= IFF_OACTIVE;
1113 * Start output on the interface.
1114 * Always called on splimp().
1116 static void ce_start (drv_t *d)
1120 ce_set_dtr (d->chan, 1);
1122 ce_set_rts (d->chan, 1);
1128 * Handle transmit timeouts.
1129 * Recover after lost transmit interrupts.
1130 * Always called on splimp().
1132 static void ce_watchdog (drv_t *d)
1134 bdrv_t *bd = d->board->sys;
1135 CE_DEBUG (d, ("device timeout\n"));
1139 ce_set_dtr (d->chan, 0);
1140 ce_set_rts (d->chan, 0);
1141 /* ce_stop_chan (d->chan);*/
1142 /* ce_start_chan (d->chan, 1, 1, 0, 0);*/
1143 ce_set_dtr (d->chan, 1);
1144 ce_set_rts (d->chan, 1);
1151 static void ce_transmit (ce_chan_t *c, void *attachment, int len)
1158 ++d->ifp->if_opackets;
1159 #if __FreeBSD_version >= 600034
1160 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1162 d->ifp->if_flags &= ~IFF_OACTIVE;
1164 d->ifp->if_timer = 0;
1169 static void ce_receive (ce_chan_t *c, unsigned char *data, int len)
1177 m = makembuf (data, len);
1179 CE_DEBUG (d, ("no memory for packet\n"));
1181 ++d->ifp->if_iqdrops;
1188 m->m_pkthdr.rcvif = 0;
1189 IF_ENQUEUE(&d->rqueue, m);
1191 ++d->ifp->if_ipackets;
1192 m->m_pkthdr.rcvif = d->ifp;
1193 /* Check if there's a BPF listener on this interface.
1194 * If so, hand off the raw packet to bpf. */
1196 #if __FreeBSD_version >= 500000
1197 BPF_TAP (d->ifp, data, len);
1199 bpf_tap (d->ifp, data, len);
1201 IF_ENQUEUE(&d->rqueue, m);
1205 static void ce_error (ce_chan_t *c, int data)
1211 CE_DEBUG (d, ("frame error\n"));
1213 ++d->ifp->if_ierrors;
1217 CE_DEBUG (d, ("crc error\n"));
1219 ++d->ifp->if_ierrors;
1223 CE_DEBUG (d, ("overrun error\n"));
1225 ++d->ifp->if_collisions;
1226 ++d->ifp->if_ierrors;
1230 CE_DEBUG (d, ("overflow error\n"));
1232 ++d->ifp->if_ierrors;
1236 CE_DEBUG (d, ("underrun error\n"));
1240 ++d->ifp->if_oerrors;
1241 #if __FreeBSD_version >= 600034
1242 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1244 d->ifp->if_flags &= ~IFF_OACTIVE;
1246 d->ifp->if_timer = 0;
1251 CE_DEBUG (d, ("error #%d\n", data));
1257 * You also need read, write, open, close routines.
1258 * This should get you started
1260 #if __FreeBSD_version < 500000
1261 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p)
1263 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1266 int unit = minor (dev);
1269 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
1271 CE_DEBUG2 (d, ("ce_open\n"));
1276 * Only called on the LAST close.
1278 #if __FreeBSD_version < 500000
1279 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p)
1281 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1284 drv_t *d = channel [minor (dev)];
1286 CE_DEBUG2 (d, ("ce_close\n"));
1290 static int ce_modem_status (ce_chan_t *c)
1293 bdrv_t *bd = d->board->sys;
1296 status = d->running ? TIOCM_LE : 0;
1299 if (ce_get_cd (c)) status |= TIOCM_CD;
1300 if (ce_get_cts (c)) status |= TIOCM_CTS;
1301 if (ce_get_dsr (c)) status |= TIOCM_DSR;
1302 if (c->dtr) status |= TIOCM_DTR;
1303 if (c->rts) status |= TIOCM_RTS;
1309 #if __FreeBSD_version < 500000
1310 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1312 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1315 drv_t *d = channel [minor (dev)];
1316 bdrv_t *bd = d->board->sys;
1317 ce_chan_t *c = d->chan;
1318 struct serial_statistics *st;
1319 struct e1_statistics *opte1;
1324 case SERIAL_GETREGISTERED:
1325 CE_DEBUG2 (d, ("ioctl: getregistered\n"));
1326 bzero (mask, sizeof(mask));
1327 for (s=0; s<NBRD*NCHAN; ++s)
1329 mask [s/8] |= 1 << (s & 7);
1330 bcopy (mask, data, sizeof (mask));
1334 case SERIAL_GETPROTO:
1335 CE_DEBUG2 (d, ("ioctl: getproto\n"));
1336 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1337 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1340 case SERIAL_SETPROTO:
1341 CE_DEBUG2 (d, ("ioctl: setproto\n"));
1342 /* Only for superuser! */
1343 #if __FreeBSD_version < 500000
1345 #elsif __FreeBSD_version < 700000
1348 error = priv_check (td, PRIV_DRIVER);
1352 #if __FreeBSD_version >= 600034
1353 if (d->ifp->if_flags & IFF_DRV_RUNNING)
1355 if (d->ifp->if_flags & IFF_RUNNING)
1358 if (! strcmp ("cisco", (char*)data)) {
1359 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1360 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1361 d->ifp->if_flags |= PP_CISCO;
1362 } else if (! strcmp ("fr", (char*)data) && PP_FR) {
1363 d->ifp->if_flags &= ~(PP_CISCO);
1364 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1365 } else if (! strcmp ("ppp", (char*)data)) {
1366 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1367 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1368 d->ifp->if_flags &= ~(PP_CISCO);
1373 case SERIAL_GETKEEPALIVE:
1374 CE_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1375 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1376 (d->ifp->if_flags & PP_CISCO))
1378 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1381 case SERIAL_SETKEEPALIVE:
1382 CE_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1383 /* Only for superuser! */
1384 #if __FreeBSD_version < 500000
1386 #elsif __FreeBSD_version < 700000
1389 error = priv_check (td, PRIV_DRIVER);
1393 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1394 (d->ifp->if_flags & PP_CISCO))
1399 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1401 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1407 case SERIAL_GETMODE:
1408 CE_DEBUG2 (d, ("ioctl: getmode\n"));
1409 *(int*)data = SERIAL_HDLC;
1412 case SERIAL_SETMODE:
1413 /* Only for superuser! */
1414 #if __FreeBSD_version < 500000
1416 #elsif __FreeBSD_version < 700000
1419 error = priv_check (td, PRIV_DRIVER);
1423 if (*(int*)data != SERIAL_HDLC)
1428 CE_DEBUG2 (d, ("ioctl: getcfg\n"));
1433 CE_DEBUG2 (d, ("ioctl: setcfg\n"));
1434 #if __FreeBSD_version < 500000
1436 #elsif __FreeBSD_version < 700000
1439 error = priv_check (td, PRIV_DRIVER);
1443 if (*((char*)data) != 'c')
1447 case SERIAL_GETSTAT:
1448 CE_DEBUG2 (d, ("ioctl: getstat\n"));
1449 st = (struct serial_statistics*) data;
1450 st->rintr = c->rintr;
1451 st->tintr = c->tintr;
1453 st->ibytes = c->ibytes;
1454 st->ipkts = c->ipkts;
1455 st->obytes = c->obytes;
1456 st->opkts = c->opkts;
1457 st->ierrs = c->overrun + c->frame + c->crc;
1458 st->oerrs = c->underrun;
1461 case SERIAL_GETESTAT:
1462 CE_DEBUG2 (d, ("ioctl: getestat\n"));
1463 if (c->type != T_E1)
1465 opte1 = (struct e1_statistics*) data;
1468 if (c->status & ESTS_NOALARM)
1469 opte1->status |= E1_NOALARM;
1470 if (c->status & ESTS_LOS)
1471 opte1->status |= E1_LOS;
1472 if (c->status & ESTS_LOF)
1473 opte1->status |= E1_LOF;
1474 if (c->status & ESTS_AIS)
1475 opte1->status |= E1_AIS;
1476 if (c->status & ESTS_LOMF)
1477 opte1->status |= E1_LOMF;
1478 if (c->status & ESTS_AIS16)
1479 opte1->status |= E1_AIS16;
1480 if (c->status & ESTS_FARLOF)
1481 opte1->status |= E1_FARLOF;
1482 if (c->status & ESTS_FARLOMF)
1483 opte1->status |= E1_FARLOMF;
1484 if (c->status & ESTS_TSTREQ)
1485 opte1->status |= E1_TSTREQ;
1486 if (c->status & ESTS_TSTERR)
1487 opte1->status |= E1_TSTERR;
1489 opte1->cursec = c->cursec;
1490 opte1->totsec = c->totsec + c->cursec;
1492 opte1->currnt.bpv = c->currnt.bpv;
1493 opte1->currnt.fse = c->currnt.fse;
1494 opte1->currnt.crce = c->currnt.crce;
1495 opte1->currnt.rcrce = c->currnt.rcrce;
1496 opte1->currnt.uas = c->currnt.uas;
1497 opte1->currnt.les = c->currnt.les;
1498 opte1->currnt.es = c->currnt.es;
1499 opte1->currnt.bes = c->currnt.bes;
1500 opte1->currnt.ses = c->currnt.ses;
1501 opte1->currnt.oofs = c->currnt.oofs;
1502 opte1->currnt.css = c->currnt.css;
1503 opte1->currnt.dm = c->currnt.dm;
1505 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1506 opte1->total.fse = c->total.fse + c->currnt.fse;
1507 opte1->total.crce = c->total.crce + c->currnt.crce;
1508 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1509 opte1->total.uas = c->total.uas + c->currnt.uas;
1510 opte1->total.les = c->total.les + c->currnt.les;
1511 opte1->total.es = c->total.es + c->currnt.es;
1512 opte1->total.bes = c->total.bes + c->currnt.bes;
1513 opte1->total.ses = c->total.ses + c->currnt.ses;
1514 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1515 opte1->total.css = c->total.css + c->currnt.css;
1516 opte1->total.dm = c->total.dm + c->currnt.dm;
1517 for (s=0; s<48; ++s) {
1518 opte1->interval[s].bpv = c->interval[s].bpv;
1519 opte1->interval[s].fse = c->interval[s].fse;
1520 opte1->interval[s].crce = c->interval[s].crce;
1521 opte1->interval[s].rcrce = c->interval[s].rcrce;
1522 opte1->interval[s].uas = c->interval[s].uas;
1523 opte1->interval[s].les = c->interval[s].les;
1524 opte1->interval[s].es = c->interval[s].es;
1525 opte1->interval[s].bes = c->interval[s].bes;
1526 opte1->interval[s].ses = c->interval[s].ses;
1527 opte1->interval[s].oofs = c->interval[s].oofs;
1528 opte1->interval[s].css = c->interval[s].css;
1529 opte1->interval[s].dm = c->interval[s].dm;
1533 case SERIAL_CLRSTAT:
1534 CE_DEBUG2 (d, ("ioctl: clrstat\n"));
1535 /* Only for superuser! */
1536 #if __FreeBSD_version < 500000
1538 #elsif __FreeBSD_version < 700000
1541 error = priv_check (td, PRIV_DRIVER);
1555 bzero (&c->currnt, sizeof (c->currnt));
1556 bzero (&c->total, sizeof (c->total));
1557 bzero (c->interval, sizeof (c->interval));
1560 case SERIAL_GETLOOP:
1561 CE_DEBUG2 (d, ("ioctl: getloop\n"));
1562 if (c->type != T_E1)
1564 *(int*)data = c->lloop;
1567 case SERIAL_SETLOOP:
1568 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1569 if (c->type != T_E1)
1571 /* Only for superuser! */
1572 #if __FreeBSD_version < 500000
1574 #elsif __FreeBSD_version < 700000
1577 error = priv_check (td, PRIV_DRIVER);
1583 ce_set_lloop (c, *(int*)data);
1588 case SERIAL_GETRLOOP:
1589 CE_DEBUG2 (d, ("ioctl: getrloop\n"));
1590 if (c->type != T_E1)
1592 *(int*)data = c->rloop;
1595 case SERIAL_SETRLOOP:
1596 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1597 if (c->type != T_E1)
1599 /* Only for superuser! */
1600 #if __FreeBSD_version < 500000
1602 #elsif __FreeBSD_version < 700000
1605 error = priv_check (td, PRIV_DRIVER);
1611 ce_set_rloop (c, *(int*)data);
1616 case SERIAL_GETDEBUG:
1617 CE_DEBUG2 (d, ("ioctl: getdebug\n"));
1618 *(int*)data = d->chan->debug;
1621 case SERIAL_SETDEBUG:
1622 CE_DEBUG2 (d, ("ioctl: setdebug\n"));
1623 /* Only for superuser! */
1624 #if __FreeBSD_version < 500000
1626 #elsif __FreeBSD_version < 700000
1629 error = priv_check (td, PRIV_DRIVER);
1633 d->chan->debug = *(int*)data;
1636 d->ifp->if_flags |= IFF_DEBUG;
1638 d->ifp->if_flags &= ~IFF_DEBUG;
1642 case SERIAL_GETBAUD:
1643 CE_DEBUG2 (d, ("ioctl: getbaud\n"));
1644 *(long*)data = c->baud;
1647 case SERIAL_SETBAUD:
1648 CE_DEBUG2 (d, ("ioctl: setbaud\n"));
1649 if (c->type != T_E1 || !c->unfram)
1651 /* Only for superuser! */
1652 #if __FreeBSD_version < 500000
1654 #elsif __FreeBSD_version < 700000
1657 error = priv_check (td, PRIV_DRIVER);
1663 ce_set_baud (c, *(long*)data);
1668 case SERIAL_GETTIMESLOTS:
1669 CE_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1670 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1672 *(u_long*)data = c->ts;
1675 case SERIAL_SETTIMESLOTS:
1676 CE_DEBUG2 (d, ("ioctl: settimeslots\n"));
1677 /* Only for superuser! */
1678 #if __FreeBSD_version < 500000
1680 #elsif __FreeBSD_version < 700000
1683 error = priv_check (td, PRIV_DRIVER);
1687 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1691 ce_set_ts (c, *(u_long*)data);
1696 case SERIAL_GETHIGAIN:
1697 CE_DEBUG2 (d, ("ioctl: gethigain\n"));
1698 if (c->type != T_E1)
1700 *(int*)data = c->higain;
1703 case SERIAL_SETHIGAIN:
1704 CE_DEBUG2 (d, ("ioctl: sethigain\n"));
1705 if (c->type != T_E1)
1707 /* Only for superuser! */
1708 #if __FreeBSD_version < 500000
1710 #elsif __FreeBSD_version < 700000
1713 error = priv_check (td, PRIV_DRIVER);
1719 ce_set_higain (c, *(int*)data);
1724 case SERIAL_GETPHONY:
1725 CE_DEBUG2 (d, ("ioctl: getphony\n"));
1726 *(int*)data = c->phony;
1729 case SERIAL_SETPHONY:
1730 CE_DEBUG2 (d, ("ioctl: setphony\n"));
1731 /* Only for superuser! */
1732 #if __FreeBSD_version < 500000
1734 #elsif __FreeBSD_version < 700000
1737 error = priv_check (td, PRIV_DRIVER);
1743 ce_set_phony (c, *(int*)data);
1748 case SERIAL_GETUNFRAM:
1749 CE_DEBUG2 (d, ("ioctl: getunfram\n"));
1750 if (c->type != T_E1 || c->num != 0)
1752 *(int*)data = c->unfram;
1755 case SERIAL_SETUNFRAM:
1756 CE_DEBUG2 (d, ("ioctl: setunfram\n"));
1757 if (c->type != T_E1 || c->num != 0)
1759 /* Only for superuser! */
1760 #if __FreeBSD_version < 500000
1762 #elsif __FreeBSD_version < 700000
1765 error = priv_check (td, PRIV_DRIVER);
1771 ce_set_unfram (c, *(int*)data);
1776 case SERIAL_GETSCRAMBLER:
1777 CE_DEBUG2 (d, ("ioctl: getscrambler\n"));
1780 *(int*)data = c->scrambler;
1783 case SERIAL_SETSCRAMBLER:
1784 CE_DEBUG2 (d, ("ioctl: setscrambler\n"));
1785 /* Only for superuser! */
1786 #if __FreeBSD_version < 500000
1788 #elsif __FreeBSD_version < 700000
1791 error = priv_check (td, PRIV_DRIVER);
1799 ce_set_scrambler (c, *(int*)data);
1804 case SERIAL_GETMONITOR:
1805 CE_DEBUG2 (d, ("ioctl: getmonitor\n"));
1806 if (c->type != T_E1)
1808 *(int*)data = c->monitor;
1811 case SERIAL_SETMONITOR:
1812 CE_DEBUG2 (d, ("ioctl: setmonitor\n"));
1813 /* Only for superuser! */
1814 #if __FreeBSD_version < 500000
1816 #elsif __FreeBSD_version < 700000
1819 error = priv_check (td, PRIV_DRIVER);
1823 if (c->type != T_E1)
1827 ce_set_monitor (c, *(int*)data);
1832 case SERIAL_GETUSE16:
1833 CE_DEBUG2 (d, ("ioctl: getuse16\n"));
1834 if (c->type != T_E1 || c->unfram)
1836 *(int*)data = c->use16;
1839 case SERIAL_SETUSE16:
1840 CE_DEBUG2 (d, ("ioctl: setuse16\n"));
1841 /* Only for superuser! */
1842 #if __FreeBSD_version < 500000
1844 #elsif __FreeBSD_version < 700000
1847 error = priv_check (td, PRIV_DRIVER);
1851 if (c->type != T_E1)
1855 ce_set_use16 (c, *(int*)data);
1860 case SERIAL_GETCRC4:
1861 CE_DEBUG2 (d, ("ioctl: getcrc4\n"));
1862 if (c->type != T_E1 || c->unfram)
1864 *(int*)data = c->crc4;
1867 case SERIAL_SETCRC4:
1868 CE_DEBUG2 (d, ("ioctl: setcrc4\n"));
1869 /* Only for superuser! */
1870 #if __FreeBSD_version < 500000
1872 #elsif __FreeBSD_version < 700000
1875 error = priv_check (td, PRIV_DRIVER);
1879 if (c->type != T_E1 || c->unfram)
1883 ce_set_crc4 (c, *(int*)data);
1889 CE_DEBUG2 (d, ("ioctl: getclk\n"));
1890 if (c->type != T_E1)
1893 default: *(int*)data = E1CLK_INTERNAL; break;
1894 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1895 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1896 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1901 CE_DEBUG2 (d, ("ioctl: setclk\n"));
1902 /* Only for superuser! */
1903 #if __FreeBSD_version < 500000
1905 #elsif __FreeBSD_version < 700000
1908 error = priv_check (td, PRIV_DRIVER);
1912 if (c->type != T_E1)
1916 switch (*(int*)data) {
1917 default: ce_set_gsyn (c, GSYN_INT); break;
1918 case E1CLK_RECEIVE: ce_set_gsyn (c, GSYN_RCV); break;
1919 case E1CLK_RECEIVE_CHAN0: ce_set_gsyn (c, GSYN_RCV0); break;
1920 case E1CLK_RECEIVE_CHAN1: ce_set_gsyn (c, GSYN_RCV1); break;
1928 CE_DEBUG2 (d, ("ioctl: reset\n"));
1929 /* Only for superuser! */
1930 #if __FreeBSD_version < 500000
1932 #elsif __FreeBSD_version < 700000
1935 error = priv_check (td, PRIV_DRIVER);
1941 /* ce_reset (c->board, 0, 0);*/
1946 case SERIAL_HARDRESET:
1947 CE_DEBUG2 (d, ("ioctl: hardreset\n"));
1948 /* Only for superuser! */
1949 #if __FreeBSD_version < 500000
1951 #elsif __FreeBSD_version < 700000
1954 error = priv_check (td, PRIV_DRIVER);
1960 /* hard_reset (c->board); */
1966 case SERIAL_GETCABLE:
1967 CE_DEBUG2 (d, ("ioctl: getcable\n"));
1968 if (c->type != T_E1)
1972 *(int*)data = CABLE_TP;
1978 CE_DEBUG2 (d, ("ioctl: getdir\n"));
1979 if (c->type != T_E1 && c->type != T_DATA)
1981 *(int*)data = c->dir;
1985 CE_DEBUG2 (d, ("ioctl: setdir\n"));
1986 /* Only for superuser! */
1987 #if __FreeBSD_version < 500000
1989 #elsif __FreeBSD_version < 700000
1992 error = priv_check (td, PRIV_DRIVER);
1998 ce_set_dir (c, *(int*)data);
2003 case TIOCSDTR: /* Set DTR */
2011 case TIOCCDTR: /* Clear DTR */
2019 case TIOCMSET: /* Set DTR/RTS */
2022 ce_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
2023 ce_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
2028 case TIOCMBIS: /* Add DTR/RTS */
2031 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 1);
2032 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 1);
2037 case TIOCMBIC: /* Clear DTR/RTS */
2040 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 0);
2041 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 0);
2046 case TIOCMGET: /* Get modem status */
2047 *(int*)data = ce_modem_status (c);
2054 #if __FreeBSD_version >= 500000
2055 static int ng_ce_constructor (node_p node)
2057 drv_t *d = NG_NODE_PRIVATE (node);
2059 static int ng_ce_constructor (node_p *node)
2061 drv_t *d = (*node)->private;
2063 CE_DEBUG (d, ("Constructor\n"));
2067 static int ng_ce_newhook (node_p node, hook_p hook, const char *name)
2070 #if __FreeBSD_version >= 500000
2071 drv_t *d = NG_NODE_PRIVATE (node);
2073 drv_t *d = node->private;
2075 bdrv_t *bd = d->board->sys;
2077 CE_DEBUG (d, ("Newhook\n"));
2078 /* Attach debug hook */
2079 if (strcmp (name, NG_CE_HOOK_DEBUG) == 0) {
2080 #if __FreeBSD_version >= 500000
2081 NG_HOOK_SET_PRIVATE (hook, NULL);
2085 d->debug_hook = hook;
2089 /* Check for raw hook */
2090 if (strcmp (name, NG_CE_HOOK_RAW) != 0)
2093 #if __FreeBSD_version >= 500000
2094 NG_HOOK_SET_PRIVATE (hook, d);
2107 static char *format_timeslots (u_long s)
2109 static char buf [100];
2113 for (i=1; i<32; ++i)
2115 int prev = (i > 1) & (s >> (i-1));
2116 int next = (i < 31) & (s >> (i+1));
2126 *p++ = '0' + i / 10;
2127 *p++ = '0' + i % 10;
2133 static int print_modems (char *s, ce_chan_t *c, int need_header)
2135 int status = ce_modem_status (c);
2139 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2140 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2141 status & TIOCM_LE ? "On" : "-",
2142 status & TIOCM_DTR ? "On" : "-",
2143 status & TIOCM_DSR ? "On" : "-",
2144 status & TIOCM_RTS ? "On" : "-",
2145 status & TIOCM_CTS ? "On" : "-",
2146 status & TIOCM_CD ? "On" : "-");
2150 static int print_stats (char *s, ce_chan_t *c, int need_header)
2155 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2156 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
2157 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
2158 c->ipkts, c->overrun + c->frame + c->crc,
2159 (unsigned long) c->obytes, c->opkts, c->underrun);
2163 static char *format_e1_status (u_char status)
2165 static char buf [80];
2167 if (status & E1_NOALARM)
2170 if (status & E1_LOS) strcat (buf, ",LOS");
2171 if (status & E1_AIS) strcat (buf, ",AIS");
2172 if (status & E1_LOF) strcat (buf, ",LOF");
2173 if (status & E1_LOMF) strcat (buf, ",LOMF");
2174 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
2175 if (status & E1_AIS16) strcat (buf, ",AIS16");
2176 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
2177 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
2178 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
2184 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
2188 if (numerator < 1 || divider < 1) {
2189 length += sprintf (s+length, leftalign ? "/- " : " -");
2192 n = (int) (0.5 + 1000.0 * numerator / divider);
2194 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
2197 *(s + length) = leftalign ? '/' : ' ';
2200 if (n >= 1000000) n = (n+500) / 1000 * 1000;
2201 else if (n >= 100000) n = (n+50) / 100 * 100;
2202 else if (n >= 10000) n = (n+5) / 10 * 10;
2205 case 1000: length += printf (s+length, ".999"); return length;
2206 case 10000: n = 9990; break;
2207 case 100000: n = 99900; break;
2208 case 1000000: n = 999000; break;
2210 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
2211 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
2212 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
2213 else length += sprintf (s+length, "%d", n/1000);
2218 static int print_e1_stats (char *s, ce_chan_t *c)
2220 struct e1_counters total;
2224 totsec = c->totsec + c->cursec;
2225 total.bpv = c->total.bpv + c->currnt.bpv;
2226 total.fse = c->total.fse + c->currnt.fse;
2227 total.crce = c->total.crce + c->currnt.crce;
2228 total.rcrce = c->total.rcrce + c->currnt.rcrce;
2229 total.uas = c->total.uas + c->currnt.uas;
2230 total.les = c->total.les + c->currnt.les;
2231 total.es = c->total.es + c->currnt.es;
2232 total.bes = c->total.bes + c->currnt.bes;
2233 total.ses = c->total.ses + c->currnt.ses;
2234 total.oofs = c->total.oofs + c->currnt.oofs;
2235 total.css = c->total.css + c->currnt.css;
2236 total.dm = c->total.dm + c->currnt.dm;
2238 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
2240 /* Unavailable seconds, degraded minutes */
2241 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
2242 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
2244 /* Bipolar violations, frame sync errors */
2245 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
2246 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
2248 /* CRC errors, remote CRC errors (E-bit) */
2249 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
2250 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2252 /* Errored seconds, line errored seconds */
2253 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2254 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2256 /* Severely errored seconds, burst errored seconds */
2257 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2258 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2260 /* Out of frame seconds, controlled slip seconds */
2261 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2262 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2264 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2266 /* Print total statistics. */
2267 length += print_frac (s + length, 0, total.uas, totsec);
2268 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2270 length += print_frac (s + length, 0, total.bpv, totsec);
2271 length += print_frac (s + length, 1, total.fse, totsec);
2273 length += print_frac (s + length, 0, total.crce, totsec);
2274 length += print_frac (s + length, 1, total.rcrce, totsec);
2276 length += print_frac (s + length, 0, total.es, totsec);
2277 length += print_frac (s + length, 1, total.les, totsec);
2279 length += print_frac (s + length, 0, total.ses, totsec);
2280 length += print_frac (s + length, 1, total.bes, totsec);
2282 length += print_frac (s + length, 0, total.oofs, totsec);
2283 length += print_frac (s + length, 1, total.css, totsec);
2285 length += sprintf (s + length, " -- Total\n");
2289 static int print_chan (char *s, ce_chan_t *c)
2294 length += sprintf (s + length, "ce%d", c->board->num * NCHAN + c->num);
2296 length += sprintf (s + length, " debug=%d", d->chan->debug);
2298 if (c->board->mux) {
2299 length += sprintf (s + length, " cfg=C");
2301 length += sprintf (s + length, " cfg=A");
2305 length += sprintf (s + length, " %ld", c->baud);
2307 length += sprintf (s + length, " extclock");
2309 if (c->type == T_E1)
2311 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2312 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2313 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2314 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2316 if (c->type == T_E1)
2317 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2319 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2321 if (c->type == T_E1)
2322 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2323 length += sprintf (s + length, "\n");
2327 #if __FreeBSD_version >= 500000
2328 static int ng_ce_rcvmsg (node_p node, item_p item, hook_p lasthook)
2330 drv_t *d = NG_NODE_PRIVATE (node);
2331 struct ng_mesg *msg;
2333 static int ng_ce_rcvmsg (node_p node, struct ng_mesg *msg,
2334 const char *retaddr, struct ng_mesg **rptr)
2336 drv_t *d = node->private;
2338 struct ng_mesg *resp = NULL;
2341 CE_DEBUG (d, ("Rcvmsg\n"));
2342 #if __FreeBSD_version >= 500000
2343 NGI_GET_MSG (item, msg);
2345 switch (msg->header.typecookie) {
2351 printf ("Not implemented yet\n");
2355 case NGM_GENERIC_COOKIE:
2356 switch (msg->header.cmd) {
2361 case NGM_TEXT_STATUS: {
2364 int dl = sizeof (struct ng_mesg) + 730;
2366 #if __FreeBSD_version >= 500000
2367 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2373 MALLOC (resp, struct ng_mesg *, dl,
2374 M_NETGRAPH, M_NOWAIT);
2383 l += print_chan (s + l, d->chan);
2384 l += print_stats (s + l, d->chan, 1);
2385 l += print_modems (s + l, d->chan, 1);
2386 l += print_e1_stats (s + l, d->chan);
2388 l += sprintf (s + l, "Error: node not connect to channel");
2389 #if __FreeBSD_version < 500000
2390 (resp)->header.version = NG_VERSION;
2391 (resp)->header.arglen = strlen (s) + 1;
2392 (resp)->header.token = msg->header.token;
2393 (resp)->header.typecookie = NGM_CE_COOKIE;
2394 (resp)->header.cmd = msg->header.cmd;
2396 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2402 #if __FreeBSD_version >= 500000
2403 NG_RESPOND_MSG (error, node, item, resp);
2407 FREE (msg, M_NETGRAPH);
2412 #if __FreeBSD_version >= 500000
2413 static int ng_ce_rcvdata (hook_p hook, item_p item)
2415 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2417 #if __FreeBSD_version < 502120
2420 struct ng_tag_prio *ptag;
2423 static int ng_ce_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
2425 drv_t *d = hook->node->private;
2427 bdrv_t *bd = d->board->sys;
2431 CE_DEBUG2 (d, ("Rcvdata\n"));
2432 #if __FreeBSD_version >= 500000
2433 NGI_GET_M (item, m);
2434 #if __FreeBSD_version < 502120
2435 NGI_GET_META (item, meta);
2437 NG_FREE_ITEM (item);
2438 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2440 #if __FreeBSD_version < 502120
2441 NG_FREE_META (meta);
2444 if (! hook->private || ! d) {
2445 NG_FREE_DATA (m,meta);
2450 #if __FreeBSD_version >= 502120
2451 /* Check for high priority data */
2452 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2453 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2458 q = (meta && meta->priority > 0) ? &d->hi_queue : &d->queue;
2463 #if __FreeBSD_version >= 500000
2465 if (_IF_QFULL (q)) {
2471 #if __FreeBSD_version < 502120
2472 NG_FREE_META (meta);
2483 NG_FREE_DATA (m, meta);
2494 static int ng_ce_rmnode (node_p node)
2496 #if __FreeBSD_version >= 500000
2497 drv_t *d = NG_NODE_PRIVATE (node);
2499 CE_DEBUG (d, ("Rmnode\n"));
2500 if (d && d->running) {
2501 bdrv_t *bd = d->board->sys;
2509 #if __FreeBSD_version >= 502120
2510 if (node->nd_flags & NGF_REALLY_DIE) {
2512 if (node->nd_flags & NG_REALLY_DIE) {
2514 NG_NODE_SET_PRIVATE (node, NULL);
2515 NG_NODE_UNREF (node);
2517 #if __FreeBSD_version >= 502120
2518 NG_NODE_REVIVE(node); /* Persistant node */
2520 node->nd_flags &= ~NG_INVALID;
2523 #else /* __FreeBSD_version < 500000 */
2524 drv_t *d = node->private;
2526 if (d && d->running) {
2527 bdrv_t *bd = d->board->sys;
2535 node->flags |= NG_INVALID;
2545 static void ng_ce_watchdog (void *arg)
2550 if (d->timeout == 1)
2554 callout_reset (&d->timeout_handle, hz, ng_ce_watchdog, d);
2558 static int ng_ce_connect (hook_p hook)
2560 #if __FreeBSD_version >= 500000
2561 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2563 drv_t *d = hook->node->private;
2567 CE_DEBUG (d, ("Connect\n"));
2568 callout_reset (&d->timeout_handle, hz, ng_ce_watchdog, d);
2574 static int ng_ce_disconnect (hook_p hook)
2576 #if __FreeBSD_version >= 500000
2577 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2579 drv_t *d = hook->node->private;
2583 CE_DEBUG (d, ("Disconnect\n"));
2584 #if __FreeBSD_version >= 500000
2585 if (NG_HOOK_PRIVATE (hook))
2590 bdrv_t *bd = d->board->sys;
2597 /* If we were wait it than it reasserted now, just stop it. */
2598 if (!callout_drain (&d->timeout_handle))
2599 callout_stop (&d->timeout_handle);
2605 static int ce_modevent (module_t mod, int type, void *unused)
2607 #if __FreeBSD_version < 500000
2609 struct cdevsw *cdsw;
2611 static int load_count = 0;
2613 #if __FreeBSD_version < 500000
2614 dev = makedev (CDEV_MAJOR, 0);
2616 #if __FreeBSD_version >= 501114
2617 if (!debug_mpsafenet && ce_mpsafenet) {
2618 printf ("WORNING! Network stack is not MPSAFE. "
2619 "Turning off debug.ce.mpsafenet.\n");
2623 #if __FreeBSD_version >= 502103
2625 ce_cdevsw.d_flags &= ~D_NEEDGIANT;
2630 #if __FreeBSD_version < 500000
2632 (cdsw = devsw (dev)) &&
2633 cdsw->d_maj == CDEV_MAJOR) {
2634 printf ("Tau32-PCI driver is already in system\n");
2638 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2639 if (ng_newtype (&typestruct))
2640 printf ("Failed to register ng_ce\n");
2643 #if __FreeBSD_version <= 500000
2644 cdevsw_add (&ce_cdevsw);
2646 #if __FreeBSD_version >= 500000
2647 callout_init (&timeout_handle, ce_mpsafenet?CALLOUT_MPSAFE:0);
2649 callout_init (&timeout_handle);
2651 callout_reset (&timeout_handle, hz*5, ce_timeout, 0);
2654 if (load_count == 1) {
2655 printf ("Removing device entry for Tau32-PCI\n");
2656 #if __FreeBSD_version <= 500000
2657 cdevsw_remove (&ce_cdevsw);
2659 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2660 ng_rmtype (&typestruct);
2663 /* If we were wait it than it reasserted now, just stop it.
2664 * Actually we shouldn't get this condition. But code could be
2665 * changed in the future, so just be a litle paranoid.
2667 if (!callout_drain (&timeout_handle))
2668 callout_stop (&timeout_handle);
2678 #if __FreeBSD_version >= 502100
2679 static struct ng_type typestruct = {
2680 .version = NG_ABI_VERSION,
2681 .name = NG_CE_NODE_TYPE,
2682 .constructor = ng_ce_constructor,
2683 .rcvmsg = ng_ce_rcvmsg,
2684 .shutdown = ng_ce_rmnode,
2685 .newhook = ng_ce_newhook,
2686 .connect = ng_ce_connect,
2687 .rcvdata = ng_ce_rcvdata,
2688 .disconnect = ng_ce_disconnect,
2690 #else /* __FreeBSD_version < 502100 */
2691 static struct ng_type typestruct = {
2692 #if __FreeBSD_version >= 500000
2706 #if __FreeBSD_version < 500000
2712 #endif /* __FreeBSD_version < 502100 */
2716 #if __FreeBSD_version >= 500000
2718 MODULE_DEPEND (ng_ce, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2720 MODULE_DEPEND (ce, sppp, 1, 1, 1);
2723 DRIVER_MODULE (cemod, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2725 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2727 #else /* if __FreeBSD_version < 500000*/
2729 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ng_mod_event, &typestruct);
2731 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2733 #endif /* __FreeBSD_version < 500000 */