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
74 #include <dev/cx/machdep.h>
75 #include <dev/ce/ceddk.h>
76 #include <machine/cserial.h>
77 #include <machine/resource.h>
78 #include <machine/pmap.h>
80 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
86 #define IFP2SP(ifp) ((struct sppp*)ifp)
89 #define SP2IFP(sp) ((struct ifnet*)sp)
93 #define PCIR_BAR(x) (PCIR_MAPS + (x) * 4)
96 /* define as our previous return value */
97 #ifndef BUS_PROBE_DEFAULT
98 #define BUS_PROBE_DEFAULT 0
101 #define CE_DEBUG(d,s) ({if (d->chan->debug) {\
102 printf ("%s: ", d->name); printf s;}})
103 #define CE_DEBUG2(d,s) ({if (d->chan->debug>1) {\
104 printf ("%s: ", d->name); printf s;}})
106 #ifndef CALLOUT_MPSAFE
107 #define CALLOUT_MPSAFE 0
111 #define IF_DRAIN(ifq) do { \
114 IF_DEQUEUE(ifq, m); \
123 #define _IF_QLEN(ifq) ((ifq)->ifq_len)
126 #ifndef callout_drain
127 #define callout_drain callout_stop
130 #if __FreeBSD_version >= 504000
131 #define CE_LOCK_NAME "ceX"
133 static int ce_mpsafenet = 1;
134 TUNABLE_INT("debug.ce.mpsafenet", &ce_mpsafenet);
135 SYSCTL_NODE(_debug, OID_AUTO, ce, CTLFLAG_RD, 0, "Cronyx Tau32-PCI Adapters");
136 SYSCTL_INT(_debug_ce, OID_AUTO, mpsafenet, CTLFLAG_RD, &ce_mpsafenet, 0,
137 "Enable/disable MPSAFE network support for Cronyx Tau32-PCI Adapters");
139 #define CE_LOCK(_bd) do { \
141 mtx_lock (&(_bd)->ce_mtx); \
143 #define CE_UNLOCK(_bd) do { \
145 mtx_unlock (&(_bd)->ce_mtx); \
148 #define CE_LOCK_ASSERT(_bd) do { \
150 mtx_assert (&(_bd)->ce_mtx, MA_OWNED); \
153 static int ce_mpsafenet = 0;
155 #define CE_LOCK(_bd) do {} while (0 && (_bd) && ce_mpsafenet)
156 #define CE_UNLOCK(_bd) do {} while (0 && (_bd) && ce_mpsafenet)
157 #define CE_LOCK_ASSERT(_bd) do {} while (0 && (_bd) && ce_mpsafenet)
160 #define CDEV_MAJOR 185
162 static int ce_probe __P((device_t));
163 static int ce_attach __P((device_t));
164 static int ce_detach __P((device_t));
166 static device_method_t ce_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_probe, ce_probe),
169 DEVMETHOD(device_attach, ce_attach),
170 DEVMETHOD(device_detach, ce_detach),
175 typedef struct _ce_dma_mem_t {
179 #if __FreeBSD_version >= 500000
185 typedef struct _drv_t {
190 struct ifqueue rqueue;
192 char nodename [NG_NODELEN+1];
196 struct ifqueue queue;
197 struct ifqueue hi_queue;
199 struct callout timeout_handle;
203 #if __FreeBSD_version >= 500000
205 #else /* __FreeBSD_version < 500000 */
211 typedef struct _bdrv_t {
213 struct resource *ce_res;
214 struct resource *ce_irq;
217 drv_t channel [NCHAN];
218 #if __FreeBSD_version >= 504000
223 static driver_t ce_driver = {
229 static devclass_t ce_devclass;
231 static void ce_receive (ce_chan_t *c, unsigned char *data, int len);
232 static void ce_transmit (ce_chan_t *c, void *attachment, int len);
233 static void ce_error (ce_chan_t *c, int data);
234 static void ce_up (drv_t *d);
235 static void ce_start (drv_t *d);
236 static void ce_down (drv_t *d);
237 static void ce_watchdog (drv_t *d);
239 extern struct ng_type typestruct;
241 static void ce_ifstart (struct ifnet *ifp);
242 static void ce_tlf (struct sppp *sp);
243 static void ce_tls (struct sppp *sp);
244 static void ce_ifwatchdog (struct ifnet *ifp);
245 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
246 static void ce_initialize (void *softc);
249 static ce_board_t *adapter [NBRD];
250 static drv_t *channel [NBRD*NCHAN];
251 static struct callout led_timo [NBRD];
252 static struct callout timeout_handle;
254 static int ce_destroy = 0;
256 #if __FreeBSD_version < 500000
257 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p);
258 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p);
259 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p);
261 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td);
262 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td);
263 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
265 #if __FreeBSD_version < 500000
266 static struct cdevsw ce_cdevsw = {
267 ce_open, ce_close, noread, nowrite,
268 ce_ioctl, nopoll, nommap, nostrategy,
269 "ce", CDEV_MAJOR, nodump, nopsize,
272 #elif __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 <= 501000
280 static struct cdevsw ce_cdevsw = {
288 .d_strategy = nostrategy,
294 #elif __FreeBSD_version < 502103
295 static struct cdevsw ce_cdevsw = {
303 #elif __FreeBSD_version < 600000
304 static struct cdevsw ce_cdevsw = {
305 .d_version = D_VERSION,
311 .d_flags = D_NEEDGIANT,
313 #else /* __FreeBSD_version >= 600000 */
314 static struct cdevsw ce_cdevsw = {
315 .d_version = D_VERSION,
320 .d_flags = D_NEEDGIANT,
325 * Print the mbuf chain, for debug purposes only.
327 static void printmbuf (struct mbuf *m)
330 for (; m; m=m->m_next) {
331 if (m->m_flags & M_PKTHDR)
332 printf (" HDR %d:", m->m_pkthdr.len);
333 if (m->m_flags & M_EXT)
335 printf (" %d", m->m_len);
341 * Make an mbuf from data.
343 static struct mbuf *makembuf (void *buf, unsigned len)
347 MGETHDR (m, M_DONTWAIT, MT_DATA);
350 MCLGET (m, M_DONTWAIT);
351 if (! (m->m_flags & M_EXT)) {
355 m->m_pkthdr.len = m->m_len = len;
356 bcopy (buf, mtod (m, caddr_t), len);
360 static int ce_probe (device_t dev)
362 if ((pci_get_vendor (dev) == TAU32_PCI_VENDOR_ID) &&
363 (pci_get_device (dev) == TAU32_PCI_DEVICE_ID)) {
364 device_set_desc (dev, "Cronyx-Tau32-PCI serial adapter");
365 return BUS_PROBE_DEFAULT;
370 static void ce_timeout (void *arg)
375 for (i = 0; i < NBRD; ++i) {
376 if (adapter[i] == NULL)
378 for (k = 0; k < NCHAN; ++k) {
384 d = channel[i * NCHAN + k];
389 CE_LOCK ((bdrv_t *)d->board->sys);
390 switch (d->chan->type) {
392 ce_e1_timer (d->chan);
397 CE_UNLOCK ((bdrv_t *)d->board->sys);
403 callout_reset (&timeout_handle, hz, ce_timeout, 0);
407 static void ce_led_off (void *arg)
410 bdrv_t *bd = (bdrv_t *) b->sys;
418 TAU32_LedSet (b->ddk.pControllerObject, 0);
423 static void ce_intr (void *arg)
426 ce_board_t *b = bd->board;
429 #if __FreeBSD_version >= 500000 && defined NETGRAPH
439 TAU32_LedSet (b->ddk.pControllerObject, 1);
441 TAU32_HandleInterrupt (b->ddk.pControllerObject);
443 /* Turn LED off 50 msec later. */
444 callout_reset (&led_timo[b->num], hz/20, ce_led_off, b);
448 /* Pass packets in a lock-free state */
449 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
450 drv_t *d = b->chan[i].sys;
452 if (!d || !d->running)
454 while (_IF_QLEN(&d->rqueue)) {
455 IF_DEQUEUE (&d->rqueue,m);
460 #if __FreeBSD_version >= 500000
461 NG_SEND_DATA_ONLY (error, d->hook, m);
463 ng_queue_data (d->hook, m, 0);
466 IF_DRAIN (&d->rqueue);
469 sppp_input (d->ifp, m);
475 #if __FreeBSD_version >= 500000
477 ce_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
484 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
486 *addr = segs->ds_addr;
490 #define BUS_DMA_ZERO 0
494 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
498 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
499 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
501 #if __FreeBSD_version >= 502000
506 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
507 else printf ("ce%d: ", bnum);
508 printf ("couldn't allocate tag for dma memory\n");
511 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
512 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
514 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
515 else printf ("ce%d: ", bnum);
516 printf ("couldn't allocate mem for dma memory\n");
517 bus_dma_tag_destroy (dmem->dmat);
520 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
521 dmem->size, ce_bus_dmamap_addr, &dmem->phys, 0);
523 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
524 else printf ("ce%d: ", bnum);
525 printf ("couldn't load mem map for dma memory\n");
526 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
527 bus_dma_tag_destroy (dmem->dmat);
530 #if __FreeBSD_version >= 502000
531 bzero (dmem->virt, dmem->size);
537 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
539 bus_dmamap_unload (dmem->dmat, dmem->mapp);
540 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
541 bus_dma_tag_destroy (dmem->dmat);
545 ce_bus_dma_mem_alloc (int bnum, int cnum, ce_dma_mem_t *dmem)
547 dmem->virt = contigmalloc (dmem->size, M_DEVBUF, M_WAITOK,
548 0x100000, 0xffffffff, 16, 0);
549 if (dmem->virt == NULL) {
550 if (cnum >= 0) printf ("ce%d-%d: ", bnum, cnum);
551 else printf ("ce%d: ", bnum);
552 printf ("couldn't allocate dma memory\n");
555 dmem->phys = vtophys (dmem->virt);
556 bzero (dmem->virt, dmem->size);
561 ce_bus_dma_mem_free (ce_dma_mem_t *dmem)
563 contigfree (dmem->virt, dmem->size, M_DEVBUF);
568 * Called if the probe succeeded.
570 static int ce_attach (device_t dev)
572 bdrv_t *bd = device_get_softc (dev);
573 int unit = device_get_unit (dev);
574 #if __FreeBSD_version >= 504000
575 char *ce_ln = CE_LOCK_NAME;
584 b = malloc (sizeof(ce_board_t), M_DEVBUF, M_WAITOK);
586 printf ("ce%d: couldn't allocate memory\n", unit);
589 bzero (b, sizeof(ce_board_t));
593 #if __FreeBSD_version >= 440000
594 pci_enable_busmaster (dev);
597 bd->dmamem.size = TAU32_ControllerObjectSize;
598 if (! ce_bus_dma_mem_alloc (unit, -1, &bd->dmamem)) {
602 b->ddk.pControllerObject = bd->dmamem.virt;
607 bd->ce_res = bus_alloc_resource (dev, SYS_RES_MEMORY, &rid,
608 0, ~0, 1, RF_ACTIVE);
610 printf ("ce%d: cannot map memory\n", unit);
611 ce_bus_dma_mem_free (&bd->dmamem);
615 vbase = (vm_offset_t) rman_get_virtual (bd->ce_res);
617 b->ddk.PciBar1VirtualAddress = (void *)vbase;
618 b->ddk.ControllerObjectPhysicalAddress = bd->dmamem.phys;
619 b->ddk.pErrorNotifyCallback = ce_error_callback;
620 b->ddk.pStatusNotifyCallback = ce_status_callback;
623 TAU32_BeforeReset(&b->ddk);
624 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_ON, 4);
625 pci_write_config (dev, TAU32_PCI_RESET_ADDRESS, TAU32_PCI_RESET_OFF, 4);
627 if(!TAU32_Initialize(&b->ddk, 0))
629 printf ("ce%d: init adapter error 0x%08x, bus dead bits 0x%08lx\n",
630 unit, b->ddk.InitErrors, b->ddk.DeadBits);
631 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
632 ce_bus_dma_mem_free (&bd->dmamem);
642 bd->ce_irq = bus_alloc_resource (dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
643 RF_SHAREABLE | RF_ACTIVE);
645 printf ("ce%d: cannot map interrupt\n", unit);
646 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
647 ce_bus_dma_mem_free (&bd->dmamem);
652 #if __FreeBSD_version >= 500000
653 callout_init (&led_timo[unit], ce_mpsafenet ? CALLOUT_MPSAFE : 0);
655 callout_init (&led_timo[unit]);
657 error = bus_setup_intr (dev, bd->ce_irq,
658 #if __FreeBSD_version >= 500013
659 INTR_TYPE_NET|(ce_mpsafenet?INTR_MPSAFE:0),
663 NULL, ce_intr, bd, &bd->ce_intrhand);
665 printf ("ce%d: cannot set up irq\n", unit);
666 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
667 bus_release_resource (dev, SYS_RES_MEMORY,
668 PCIR_BAR(0), bd->ce_res);
669 ce_bus_dma_mem_free (&bd->dmamem);
675 switch (b->ddk.Model) {
676 case 1: strcpy (b->name, TAU32_BASE_NAME); break;
677 case 2: strcpy (b->name, TAU32_LITE_NAME); break;
678 case 3: strcpy (b->name, TAU32_ADPCM_NAME); break;
679 default: strcpy (b->name, TAU32_UNKNOWN_NAME); break;
682 printf ("ce%d: %s\n", unit, b->name);
684 for (c = b->chan; c < b->chan + NCHAN; ++c) {
685 c->num = (c - b->chan);
688 d = &bd->channel[c->num];
689 d->dmamem.size = sizeof(ce_buf_t);
690 if (! ce_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
693 channel [b->num * NCHAN + c->num] = d;
694 sprintf (d->name, "ce%d.%d", b->num, c->num);
700 for (c = b->chan; c < b->chan + NCHAN; ++c) {
706 if (ng_make_node_common (&typestruct, &d->node) != 0) {
707 printf ("%s: cannot make common node\n", d->name);
711 #if __FreeBSD_version >= 500000
712 NG_NODE_SET_PRIVATE (d->node, d);
713 callout_init (&d->timeout_handle,
714 ce_mpsafenet ? CALLOUT_MPSAFE : 0);
716 d->node->private = d;
718 sprintf (d->nodename, "%s%d", NG_CE_NODE_TYPE,
719 c->board->num * NCHAN + c->num);
720 if (ng_name_node (d->node, d->nodename)) {
721 printf ("%s: cannot name node\n", d->nodename);
722 #if __FreeBSD_version >= 500000
723 NG_NODE_UNREF (d->node);
730 d->queue.ifq_maxlen = IFQ_MAXLEN;
731 d->hi_queue.ifq_maxlen = IFQ_MAXLEN;
732 d->rqueue.ifq_maxlen = IFQ_MAXLEN;
733 #if __FreeBSD_version >= 500000
734 mtx_init (&d->queue.ifq_mtx, "ce_queue", NULL, MTX_DEF);
735 mtx_init (&d->hi_queue.ifq_mtx, "ce_queue_hi", NULL, MTX_DEF);
736 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
739 #if __FreeBSD_version >= 600031
740 d->ifp = if_alloc(IFT_PPP);
742 d->ifp = malloc (sizeof(struct sppp), M_DEVBUF, M_WAITOK);
743 bzero (d->ifp, sizeof(struct sppp));
746 printf ("%s: cannot if_alloc() interface\n", d->name);
749 d->ifp->if_softc = d;
750 #if __FreeBSD_version > 501000
751 if_initname (d->ifp, "ce", b->num * NCHAN + c->num);
753 d->ifp->if_unit = b->num * NCHAN + c->num;
754 d->ifp->if_name = "ce";
756 d->ifp->if_mtu = PP_MTU;
757 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
758 #if __FreeBSD_version >= 502125
760 d->ifp->if_flags |= IFF_NEEDSGIANT;
762 d->ifp->if_ioctl = ce_sioctl;
763 d->ifp->if_start = ce_ifstart;
764 d->ifp->if_watchdog = ce_ifwatchdog;
765 d->ifp->if_init = ce_initialize;
766 d->rqueue.ifq_maxlen = IFQ_MAXLEN;
767 #if __FreeBSD_version >= 500000
768 mtx_init (&d->rqueue.ifq_mtx, "ce_rqueue", NULL, MTX_DEF);
770 sppp_attach (d->ifp);
772 IFP2SP(d->ifp)->pp_tlf = ce_tlf;
773 IFP2SP(d->ifp)->pp_tls = ce_tls;
774 /* If BPF is in the kernel, call the attach for it.
775 * The header size of PPP or Cisco/HDLC is 4 bytes. */
776 bpfattach (d->ifp, DLT_PPP, 4);
778 ce_start_chan (c, 1, 1, d->dmamem.virt, d->dmamem.phys);
780 /* Register callback functions. */
781 ce_register_transmit (c, &ce_transmit);
782 ce_register_receive (c, &ce_receive);
783 ce_register_error (c, &ce_error);
784 d->devt = make_dev (&ce_cdevsw, b->num*NCHAN+c->num, UID_ROOT,
785 GID_WHEEL, 0600, "ce%d", b->num*NCHAN+c->num);
788 #if __FreeBSD_version >= 504000
789 ce_ln[2] = '0' + unit;
790 mtx_init (&bd->ce_mtx, ce_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
793 TAU32_EnableInterrupts(b->ddk.pControllerObject);
801 static int ce_detach (device_t dev)
803 bdrv_t *bd = device_get_softc (dev);
804 ce_board_t *b = bd->board;
808 #if __FreeBSD_version >= 504000
809 KASSERT (mtx_initialized (&bd->ce_mtx), ("ce mutex not initialized"));
813 /* Check if the device is busy (open). */
814 for (c = b->chan; c < b->chan + NCHAN; ++c) {
815 drv_t *d = (drv_t*) c->sys;
817 /* XXX Non existen chan! */
818 if (! d || ! d->chan)
827 /* Ok, we can unload driver */
828 /* At first we should disable interrupts */
830 TAU32_DisableInterrupts(b->ddk.pControllerObject);
832 callout_stop (&led_timo[b->num]);
834 for (c = b->chan; c < b->chan + NCHAN; ++c) {
835 drv_t *d = (drv_t*) c->sys;
837 if (! d || ! d->chan)
840 /* Detach from the packet filter list of interfaces. */
843 /* Detach from the sync PPP list. */
844 sppp_detach (d->ifp);
846 /* Detach from the system list of interfaces. */
848 #if __FreeBSD_version > 600031
851 free (d->ifp, M_DEVBUF);
854 IF_DRAIN (&d->rqueue);
855 #if __FreeBSD_version >= 500000
856 mtx_destroy (&d->rqueue.ifq_mtx);
859 #if __FreeBSD_version >= 500000
861 ng_rmnode_self (d->node);
862 NG_NODE_UNREF (d->node);
865 IF_DRAIN (&d->rqueue);
866 mtx_destroy (&d->queue.ifq_mtx);
867 mtx_destroy (&d->hi_queue.ifq_mtx);
868 mtx_destroy (&d->rqueue.ifq_mtx);
874 destroy_dev (d->devt);
880 callout_drain (&led_timo[b->num]);
882 /* Disable the interrupt request. */
883 bus_teardown_intr (dev, bd->ce_irq, bd->ce_intrhand);
884 bus_release_resource (dev, SYS_RES_IRQ, 0, bd->ce_irq);
885 TAU32_DestructiveHalt (b->ddk.pControllerObject, 0);
886 bus_release_resource (dev, SYS_RES_MEMORY, PCIR_BAR(0), bd->ce_res);
890 for (c = b->chan; c < b->chan + NCHAN; ++c) {
891 drv_t *d = (drv_t*) c->sys;
893 if (! d || ! d->chan)
895 channel [b->num * NCHAN + c->num] = 0;
896 /* Deallocate buffers. */
897 ce_bus_dma_mem_free (&d->dmamem);
899 adapter [b->num] = 0;
900 ce_bus_dma_mem_free (&bd->dmamem);
904 #if __FreeBSD_version >= 504000
905 mtx_destroy (&bd->ce_mtx);
911 static void ce_ifstart (struct ifnet *ifp)
913 drv_t *d = ifp->if_softc;
914 bdrv_t *bd = d->board->sys;
921 static void ce_ifwatchdog (struct ifnet *ifp)
923 drv_t *d = ifp->if_softc;
928 static void ce_tlf (struct sppp *sp)
930 drv_t *d = SP2IFP(sp)->if_softc;
932 CE_DEBUG2 (d, ("ce_tlf\n"));
936 static void ce_tls (struct sppp *sp)
938 drv_t *d = SP2IFP(sp)->if_softc;
940 CE_DEBUG2 (d, ("ce_tls\n"));
945 * Process an ioctl request.
947 static int ce_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
949 drv_t *d = ifp->if_softc;
950 bdrv_t *bd = d->board->sys;
951 int error, s, was_up, should_be_up;
953 #if __FreeBSD_version >= 600034
954 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
956 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
958 error = sppp_ioctl (ifp, cmd, data);
963 if (! (ifp->if_flags & IFF_DEBUG))
966 d->chan->debug = d->chan->debug_shadow;
969 default: CE_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
970 case SIOCADDMULTI: CE_DEBUG2 (d, ("ioctl SIOCADDMULTI\n")); return 0;
971 case SIOCDELMULTI: CE_DEBUG2 (d, ("ioctl SIOCDELMULTI\n")); return 0;
972 case SIOCSIFFLAGS: CE_DEBUG2 (d, ("ioctl SIOCSIFFLAGS\n")); break;
973 case SIOCSIFADDR: CE_DEBUG2 (d, ("ioctl SIOCSIFADDR\n")); break;
976 /* We get here only in case of SIFFLAGS or SIFADDR. */
979 #if __FreeBSD_version >= 600034
980 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
982 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
984 if (! was_up && should_be_up) {
985 /* Interface goes up -- start it. */
988 } else if (was_up && ! should_be_up) {
989 /* Interface is going down -- stop it. */
990 /* if ((IFP2SP(ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
993 CE_DEBUG (d, ("ioctl 0x%lx p4\n", cmd));
1000 * Initialization of interface.
1001 * It seems to be never called by upper level?
1003 static void ce_initialize (void *softc)
1007 CE_DEBUG (d, ("ce_initialize\n"));
1012 * Stop the interface. Called on splimp().
1014 static void ce_down (drv_t *d)
1016 CE_DEBUG (d, ("ce_down\n"));
1017 /* Interface is going down -- stop it. */
1018 ce_set_dtr (d->chan, 0);
1019 ce_set_rts (d->chan, 0);
1025 * Start the interface. Called on splimp().
1027 static void ce_up (drv_t *d)
1029 CE_DEBUG (d, ("ce_up\n"));
1030 ce_set_dtr (d->chan, 1);
1031 ce_set_rts (d->chan, 1);
1037 * Start output on the interface. Get another datagram to send
1038 * off of the interface queue, and copy it to the interface
1039 * before starting the output.
1041 static void ce_send (drv_t *d)
1046 CE_DEBUG2 (d, ("ce_send\n"));
1048 /* No output if the interface is down. */
1052 while (ce_transmit_space (d->chan)) {
1053 /* Get the packet to send. */
1055 IF_DEQUEUE (&d->hi_queue, m);
1057 IF_DEQUEUE (&d->queue, m);
1059 m = sppp_dequeue (d->ifp);
1064 #if __FreeBSD_version >= 500000
1065 BPF_MTAP (d->ifp, m);
1068 bpf_mtap (d->ifp, m);
1071 #if __FreeBSD_version >= 490000
1072 len = m_length (m, NULL);
1074 len = m->m_pkthdr.len;
1077 printf ("%s: too long packet: %d bytes: ",
1079 else if (! m->m_next)
1080 ce_send_packet (d->chan, (u_char*) mtod (m, caddr_t), len, 0);
1082 ce_buf_item_t *item = (ce_buf_item_t*)d->chan->tx_queue;
1083 m_copydata (m, 0, len, item->buf);
1084 ce_send_packet (d->chan, item->buf, len, 0);
1087 /* Set up transmit timeout, if the transmit ring is not empty.*/
1091 d->ifp->if_timer = 10;
1095 #if __FreeBSD_version >= 600034
1096 d->ifp->if_flags |= IFF_DRV_OACTIVE;
1098 d->ifp->if_flags |= IFF_OACTIVE;
1104 * Start output on the interface.
1105 * Always called on splimp().
1107 static void ce_start (drv_t *d)
1111 ce_set_dtr (d->chan, 1);
1113 ce_set_rts (d->chan, 1);
1119 * Handle transmit timeouts.
1120 * Recover after lost transmit interrupts.
1121 * Always called on splimp().
1123 static void ce_watchdog (drv_t *d)
1125 bdrv_t *bd = d->board->sys;
1126 CE_DEBUG (d, ("device timeout\n"));
1130 ce_set_dtr (d->chan, 0);
1131 ce_set_rts (d->chan, 0);
1132 /* ce_stop_chan (d->chan);*/
1133 /* ce_start_chan (d->chan, 1, 1, 0, 0);*/
1134 ce_set_dtr (d->chan, 1);
1135 ce_set_rts (d->chan, 1);
1142 static void ce_transmit (ce_chan_t *c, void *attachment, int len)
1149 ++d->ifp->if_opackets;
1150 #if __FreeBSD_version >= 600034
1151 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1153 d->ifp->if_flags &= ~IFF_OACTIVE;
1155 d->ifp->if_timer = 0;
1160 static void ce_receive (ce_chan_t *c, unsigned char *data, int len)
1168 m = makembuf (data, len);
1170 CE_DEBUG (d, ("no memory for packet\n"));
1172 ++d->ifp->if_iqdrops;
1179 m->m_pkthdr.rcvif = 0;
1180 IF_ENQUEUE(&d->rqueue, m);
1182 ++d->ifp->if_ipackets;
1183 m->m_pkthdr.rcvif = d->ifp;
1184 /* Check if there's a BPF listener on this interface.
1185 * If so, hand off the raw packet to bpf. */
1186 #if __FreeBSD_version >= 500000
1187 BPF_TAP (d->ifp, data, len);
1190 bpf_tap (d->ifp, data, len);
1192 IF_ENQUEUE(&d->rqueue, m);
1196 static void ce_error (ce_chan_t *c, int data)
1202 CE_DEBUG (d, ("frame error\n"));
1204 ++d->ifp->if_ierrors;
1208 CE_DEBUG (d, ("crc error\n"));
1210 ++d->ifp->if_ierrors;
1214 CE_DEBUG (d, ("overrun error\n"));
1216 ++d->ifp->if_collisions;
1217 ++d->ifp->if_ierrors;
1221 CE_DEBUG (d, ("overflow error\n"));
1223 ++d->ifp->if_ierrors;
1227 CE_DEBUG (d, ("underrun error\n"));
1231 ++d->ifp->if_oerrors;
1232 #if __FreeBSD_version >= 600034
1233 d->ifp->if_flags &= ~IFF_DRV_OACTIVE;
1235 d->ifp->if_flags &= ~IFF_OACTIVE;
1237 d->ifp->if_timer = 0;
1242 CE_DEBUG (d, ("error #%d\n", data));
1248 * You also need read, write, open, close routines.
1249 * This should get you started
1251 #if __FreeBSD_version < 500000
1252 static int ce_open (dev_t dev, int oflags, int devtype, struct proc *p)
1254 static int ce_open (struct cdev *dev, int oflags, int devtype, struct thread *td)
1257 int unit = minor (dev);
1260 if (unit >= NBRD*NCHAN || ! (d = channel[unit]))
1262 CE_DEBUG2 (d, ("ce_open\n"));
1267 * Only called on the LAST close.
1269 #if __FreeBSD_version < 500000
1270 static int ce_close (dev_t dev, int fflag, int devtype, struct proc *p)
1272 static int ce_close (struct cdev *dev, int fflag, int devtype, struct thread *td)
1275 drv_t *d = channel [minor (dev)];
1277 CE_DEBUG2 (d, ("ce_close\n"));
1281 static int ce_modem_status (ce_chan_t *c)
1284 bdrv_t *bd = d->board->sys;
1287 status = d->running ? TIOCM_LE : 0;
1290 if (ce_get_cd (c)) status |= TIOCM_CD;
1291 if (ce_get_cts (c)) status |= TIOCM_CTS;
1292 if (ce_get_dsr (c)) status |= TIOCM_DSR;
1293 if (c->dtr) status |= TIOCM_DTR;
1294 if (c->rts) status |= TIOCM_RTS;
1300 #if __FreeBSD_version < 500000
1301 static int ce_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1303 static int ce_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1306 drv_t *d = channel [minor (dev)];
1307 bdrv_t *bd = d->board->sys;
1308 ce_chan_t *c = d->chan;
1309 struct serial_statistics *st;
1310 struct e1_statistics *opte1;
1315 case SERIAL_GETREGISTERED:
1316 CE_DEBUG2 (d, ("ioctl: getregistered\n"));
1317 bzero (mask, sizeof(mask));
1318 for (s=0; s<NBRD*NCHAN; ++s)
1320 mask [s/8] |= 1 << (s & 7);
1321 bcopy (mask, data, sizeof (mask));
1325 case SERIAL_GETPROTO:
1326 CE_DEBUG2 (d, ("ioctl: getproto\n"));
1327 strcpy ((char*)data, (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1328 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1331 case SERIAL_SETPROTO:
1332 CE_DEBUG2 (d, ("ioctl: setproto\n"));
1333 /* Only for superuser! */
1334 #if __FreeBSD_version < 500000
1336 #elsif __FreeBSD_version < 700000
1339 error = priv_check (td, PRIV_DRIVER);
1343 #if __FreeBSD_version >= 600034
1344 if (d->ifp->if_flags & IFF_DRV_RUNNING)
1346 if (d->ifp->if_flags & IFF_RUNNING)
1349 if (! strcmp ("cisco", (char*)data)) {
1350 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1351 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1352 d->ifp->if_flags |= PP_CISCO;
1353 } else if (! strcmp ("fr", (char*)data) && PP_FR) {
1354 d->ifp->if_flags &= ~(PP_CISCO);
1355 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1356 } else if (! strcmp ("ppp", (char*)data)) {
1357 IFP2SP(d->ifp)->pp_flags &= ~PP_FR;
1358 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1359 d->ifp->if_flags &= ~(PP_CISCO);
1364 case SERIAL_GETKEEPALIVE:
1365 CE_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1366 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1367 (d->ifp->if_flags & PP_CISCO))
1369 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1372 case SERIAL_SETKEEPALIVE:
1373 CE_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1374 /* Only for superuser! */
1375 #if __FreeBSD_version < 500000
1377 #elsif __FreeBSD_version < 700000
1380 error = priv_check (td, PRIV_DRIVER);
1384 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1385 (d->ifp->if_flags & PP_CISCO))
1390 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1392 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1398 case SERIAL_GETMODE:
1399 CE_DEBUG2 (d, ("ioctl: getmode\n"));
1400 *(int*)data = SERIAL_HDLC;
1403 case SERIAL_SETMODE:
1404 /* Only for superuser! */
1405 #if __FreeBSD_version < 500000
1407 #elsif __FreeBSD_version < 700000
1410 error = priv_check (td, PRIV_DRIVER);
1414 if (*(int*)data != SERIAL_HDLC)
1419 CE_DEBUG2 (d, ("ioctl: getcfg\n"));
1424 CE_DEBUG2 (d, ("ioctl: setcfg\n"));
1425 #if __FreeBSD_version < 500000
1427 #elsif __FreeBSD_version < 700000
1430 error = priv_check (td, PRIV_DRIVER);
1434 if (*((char*)data) != 'c')
1438 case SERIAL_GETSTAT:
1439 CE_DEBUG2 (d, ("ioctl: getstat\n"));
1440 st = (struct serial_statistics*) data;
1441 st->rintr = c->rintr;
1442 st->tintr = c->tintr;
1444 st->ibytes = c->ibytes;
1445 st->ipkts = c->ipkts;
1446 st->obytes = c->obytes;
1447 st->opkts = c->opkts;
1448 st->ierrs = c->overrun + c->frame + c->crc;
1449 st->oerrs = c->underrun;
1452 case SERIAL_GETESTAT:
1453 CE_DEBUG2 (d, ("ioctl: getestat\n"));
1454 if (c->type != T_E1)
1456 opte1 = (struct e1_statistics*) data;
1459 if (c->status & ESTS_NOALARM)
1460 opte1->status |= E1_NOALARM;
1461 if (c->status & ESTS_LOS)
1462 opte1->status |= E1_LOS;
1463 if (c->status & ESTS_LOF)
1464 opte1->status |= E1_LOF;
1465 if (c->status & ESTS_AIS)
1466 opte1->status |= E1_AIS;
1467 if (c->status & ESTS_LOMF)
1468 opte1->status |= E1_LOMF;
1469 if (c->status & ESTS_AIS16)
1470 opte1->status |= E1_AIS16;
1471 if (c->status & ESTS_FARLOF)
1472 opte1->status |= E1_FARLOF;
1473 if (c->status & ESTS_FARLOMF)
1474 opte1->status |= E1_FARLOMF;
1475 if (c->status & ESTS_TSTREQ)
1476 opte1->status |= E1_TSTREQ;
1477 if (c->status & ESTS_TSTERR)
1478 opte1->status |= E1_TSTERR;
1480 opte1->cursec = c->cursec;
1481 opte1->totsec = c->totsec + c->cursec;
1483 opte1->currnt.bpv = c->currnt.bpv;
1484 opte1->currnt.fse = c->currnt.fse;
1485 opte1->currnt.crce = c->currnt.crce;
1486 opte1->currnt.rcrce = c->currnt.rcrce;
1487 opte1->currnt.uas = c->currnt.uas;
1488 opte1->currnt.les = c->currnt.les;
1489 opte1->currnt.es = c->currnt.es;
1490 opte1->currnt.bes = c->currnt.bes;
1491 opte1->currnt.ses = c->currnt.ses;
1492 opte1->currnt.oofs = c->currnt.oofs;
1493 opte1->currnt.css = c->currnt.css;
1494 opte1->currnt.dm = c->currnt.dm;
1496 opte1->total.bpv = c->total.bpv + c->currnt.bpv;
1497 opte1->total.fse = c->total.fse + c->currnt.fse;
1498 opte1->total.crce = c->total.crce + c->currnt.crce;
1499 opte1->total.rcrce = c->total.rcrce + c->currnt.rcrce;
1500 opte1->total.uas = c->total.uas + c->currnt.uas;
1501 opte1->total.les = c->total.les + c->currnt.les;
1502 opte1->total.es = c->total.es + c->currnt.es;
1503 opte1->total.bes = c->total.bes + c->currnt.bes;
1504 opte1->total.ses = c->total.ses + c->currnt.ses;
1505 opte1->total.oofs = c->total.oofs + c->currnt.oofs;
1506 opte1->total.css = c->total.css + c->currnt.css;
1507 opte1->total.dm = c->total.dm + c->currnt.dm;
1508 for (s=0; s<48; ++s) {
1509 opte1->interval[s].bpv = c->interval[s].bpv;
1510 opte1->interval[s].fse = c->interval[s].fse;
1511 opte1->interval[s].crce = c->interval[s].crce;
1512 opte1->interval[s].rcrce = c->interval[s].rcrce;
1513 opte1->interval[s].uas = c->interval[s].uas;
1514 opte1->interval[s].les = c->interval[s].les;
1515 opte1->interval[s].es = c->interval[s].es;
1516 opte1->interval[s].bes = c->interval[s].bes;
1517 opte1->interval[s].ses = c->interval[s].ses;
1518 opte1->interval[s].oofs = c->interval[s].oofs;
1519 opte1->interval[s].css = c->interval[s].css;
1520 opte1->interval[s].dm = c->interval[s].dm;
1524 case SERIAL_CLRSTAT:
1525 CE_DEBUG2 (d, ("ioctl: clrstat\n"));
1526 /* Only for superuser! */
1527 #if __FreeBSD_version < 500000
1529 #elsif __FreeBSD_version < 700000
1532 error = priv_check (td, PRIV_DRIVER);
1546 bzero (&c->currnt, sizeof (c->currnt));
1547 bzero (&c->total, sizeof (c->total));
1548 bzero (c->interval, sizeof (c->interval));
1551 case SERIAL_GETLOOP:
1552 CE_DEBUG2 (d, ("ioctl: getloop\n"));
1553 if (c->type != T_E1)
1555 *(int*)data = c->lloop;
1558 case SERIAL_SETLOOP:
1559 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1560 if (c->type != T_E1)
1562 /* Only for superuser! */
1563 #if __FreeBSD_version < 500000
1565 #elsif __FreeBSD_version < 700000
1568 error = priv_check (td, PRIV_DRIVER);
1574 ce_set_lloop (c, *(int*)data);
1579 case SERIAL_GETRLOOP:
1580 CE_DEBUG2 (d, ("ioctl: getrloop\n"));
1581 if (c->type != T_E1)
1583 *(int*)data = c->rloop;
1586 case SERIAL_SETRLOOP:
1587 CE_DEBUG2 (d, ("ioctl: setloop\n"));
1588 if (c->type != T_E1)
1590 /* Only for superuser! */
1591 #if __FreeBSD_version < 500000
1593 #elsif __FreeBSD_version < 700000
1596 error = priv_check (td, PRIV_DRIVER);
1602 ce_set_rloop (c, *(int*)data);
1607 case SERIAL_GETDEBUG:
1608 CE_DEBUG2 (d, ("ioctl: getdebug\n"));
1609 *(int*)data = d->chan->debug;
1612 case SERIAL_SETDEBUG:
1613 CE_DEBUG2 (d, ("ioctl: setdebug\n"));
1614 /* Only for superuser! */
1615 #if __FreeBSD_version < 500000
1617 #elsif __FreeBSD_version < 700000
1620 error = priv_check (td, PRIV_DRIVER);
1626 * The debug_shadow is always greater than zero for logic
1627 * simplicity. For switching debug off the IFF_DEBUG is
1630 d->chan->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1631 if (d->ifp->if_flags & IFF_DEBUG)
1632 d->chan->debug = d->chan->debug_shadow;
1634 d->chan->debug = *(int*)data;
1638 case SERIAL_GETBAUD:
1639 CE_DEBUG2 (d, ("ioctl: getbaud\n"));
1640 *(long*)data = c->baud;
1643 case SERIAL_SETBAUD:
1644 CE_DEBUG2 (d, ("ioctl: setbaud\n"));
1645 if (c->type != T_E1 || !c->unfram)
1647 /* Only for superuser! */
1648 #if __FreeBSD_version < 500000
1650 #elsif __FreeBSD_version < 700000
1653 error = priv_check (td, PRIV_DRIVER);
1659 ce_set_baud (c, *(long*)data);
1664 case SERIAL_GETTIMESLOTS:
1665 CE_DEBUG2 (d, ("ioctl: gettimeslots\n"));
1666 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1668 *(u_long*)data = c->ts;
1671 case SERIAL_SETTIMESLOTS:
1672 CE_DEBUG2 (d, ("ioctl: settimeslots\n"));
1673 /* Only for superuser! */
1674 #if __FreeBSD_version < 500000
1676 #elsif __FreeBSD_version < 700000
1679 error = priv_check (td, PRIV_DRIVER);
1683 if ((c->type != T_E1 || c->unfram) && c->type != T_DATA)
1687 ce_set_ts (c, *(u_long*)data);
1692 case SERIAL_GETHIGAIN:
1693 CE_DEBUG2 (d, ("ioctl: gethigain\n"));
1694 if (c->type != T_E1)
1696 *(int*)data = c->higain;
1699 case SERIAL_SETHIGAIN:
1700 CE_DEBUG2 (d, ("ioctl: sethigain\n"));
1701 if (c->type != T_E1)
1703 /* Only for superuser! */
1704 #if __FreeBSD_version < 500000
1706 #elsif __FreeBSD_version < 700000
1709 error = priv_check (td, PRIV_DRIVER);
1715 ce_set_higain (c, *(int*)data);
1720 case SERIAL_GETPHONY:
1721 CE_DEBUG2 (d, ("ioctl: getphony\n"));
1722 *(int*)data = c->phony;
1725 case SERIAL_SETPHONY:
1726 CE_DEBUG2 (d, ("ioctl: setphony\n"));
1727 /* Only for superuser! */
1728 #if __FreeBSD_version < 500000
1730 #elsif __FreeBSD_version < 700000
1733 error = priv_check (td, PRIV_DRIVER);
1739 ce_set_phony (c, *(int*)data);
1744 case SERIAL_GETUNFRAM:
1745 CE_DEBUG2 (d, ("ioctl: getunfram\n"));
1746 if (c->type != T_E1 || c->num != 0)
1748 *(int*)data = c->unfram;
1751 case SERIAL_SETUNFRAM:
1752 CE_DEBUG2 (d, ("ioctl: setunfram\n"));
1753 if (c->type != T_E1 || c->num != 0)
1755 /* Only for superuser! */
1756 #if __FreeBSD_version < 500000
1758 #elsif __FreeBSD_version < 700000
1761 error = priv_check (td, PRIV_DRIVER);
1767 ce_set_unfram (c, *(int*)data);
1772 case SERIAL_GETSCRAMBLER:
1773 CE_DEBUG2 (d, ("ioctl: getscrambler\n"));
1776 *(int*)data = c->scrambler;
1779 case SERIAL_SETSCRAMBLER:
1780 CE_DEBUG2 (d, ("ioctl: setscrambler\n"));
1781 /* Only for superuser! */
1782 #if __FreeBSD_version < 500000
1784 #elsif __FreeBSD_version < 700000
1787 error = priv_check (td, PRIV_DRIVER);
1795 ce_set_scrambler (c, *(int*)data);
1800 case SERIAL_GETMONITOR:
1801 CE_DEBUG2 (d, ("ioctl: getmonitor\n"));
1802 if (c->type != T_E1)
1804 *(int*)data = c->monitor;
1807 case SERIAL_SETMONITOR:
1808 CE_DEBUG2 (d, ("ioctl: setmonitor\n"));
1809 /* Only for superuser! */
1810 #if __FreeBSD_version < 500000
1812 #elsif __FreeBSD_version < 700000
1815 error = priv_check (td, PRIV_DRIVER);
1819 if (c->type != T_E1)
1823 ce_set_monitor (c, *(int*)data);
1828 case SERIAL_GETUSE16:
1829 CE_DEBUG2 (d, ("ioctl: getuse16\n"));
1830 if (c->type != T_E1 || c->unfram)
1832 *(int*)data = c->use16;
1835 case SERIAL_SETUSE16:
1836 CE_DEBUG2 (d, ("ioctl: setuse16\n"));
1837 /* Only for superuser! */
1838 #if __FreeBSD_version < 500000
1840 #elsif __FreeBSD_version < 700000
1843 error = priv_check (td, PRIV_DRIVER);
1847 if (c->type != T_E1)
1851 ce_set_use16 (c, *(int*)data);
1856 case SERIAL_GETCRC4:
1857 CE_DEBUG2 (d, ("ioctl: getcrc4\n"));
1858 if (c->type != T_E1 || c->unfram)
1860 *(int*)data = c->crc4;
1863 case SERIAL_SETCRC4:
1864 CE_DEBUG2 (d, ("ioctl: setcrc4\n"));
1865 /* Only for superuser! */
1866 #if __FreeBSD_version < 500000
1868 #elsif __FreeBSD_version < 700000
1871 error = priv_check (td, PRIV_DRIVER);
1875 if (c->type != T_E1 || c->unfram)
1879 ce_set_crc4 (c, *(int*)data);
1885 CE_DEBUG2 (d, ("ioctl: getclk\n"));
1886 if (c->type != T_E1)
1889 default: *(int*)data = E1CLK_INTERNAL; break;
1890 case GSYN_RCV: *(int*)data = E1CLK_RECEIVE; break;
1891 case GSYN_RCV0: *(int*)data = E1CLK_RECEIVE_CHAN0; break;
1892 case GSYN_RCV1: *(int*)data = E1CLK_RECEIVE_CHAN1; break;
1897 CE_DEBUG2 (d, ("ioctl: setclk\n"));
1898 /* Only for superuser! */
1899 #if __FreeBSD_version < 500000
1901 #elsif __FreeBSD_version < 700000
1904 error = priv_check (td, PRIV_DRIVER);
1908 if (c->type != T_E1)
1912 switch (*(int*)data) {
1913 default: ce_set_gsyn (c, GSYN_INT); break;
1914 case E1CLK_RECEIVE: ce_set_gsyn (c, GSYN_RCV); break;
1915 case E1CLK_RECEIVE_CHAN0: ce_set_gsyn (c, GSYN_RCV0); break;
1916 case E1CLK_RECEIVE_CHAN1: ce_set_gsyn (c, GSYN_RCV1); break;
1924 CE_DEBUG2 (d, ("ioctl: reset\n"));
1925 /* Only for superuser! */
1926 #if __FreeBSD_version < 500000
1928 #elsif __FreeBSD_version < 700000
1931 error = priv_check (td, PRIV_DRIVER);
1937 /* ce_reset (c->board, 0, 0);*/
1942 case SERIAL_HARDRESET:
1943 CE_DEBUG2 (d, ("ioctl: hardreset\n"));
1944 /* Only for superuser! */
1945 #if __FreeBSD_version < 500000
1947 #elsif __FreeBSD_version < 700000
1950 error = priv_check (td, PRIV_DRIVER);
1956 /* hard_reset (c->board); */
1962 case SERIAL_GETCABLE:
1963 CE_DEBUG2 (d, ("ioctl: getcable\n"));
1964 if (c->type != T_E1)
1968 *(int*)data = CABLE_TP;
1974 CE_DEBUG2 (d, ("ioctl: getdir\n"));
1975 if (c->type != T_E1 && c->type != T_DATA)
1977 *(int*)data = c->dir;
1981 CE_DEBUG2 (d, ("ioctl: setdir\n"));
1982 /* Only for superuser! */
1983 #if __FreeBSD_version < 500000
1985 #elsif __FreeBSD_version < 700000
1988 error = priv_check (td, PRIV_DRIVER);
1994 ce_set_dir (c, *(int*)data);
1999 case TIOCSDTR: /* Set DTR */
2007 case TIOCCDTR: /* Clear DTR */
2015 case TIOCMSET: /* Set DTR/RTS */
2018 ce_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
2019 ce_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
2024 case TIOCMBIS: /* Add DTR/RTS */
2027 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 1);
2028 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 1);
2033 case TIOCMBIC: /* Clear DTR/RTS */
2036 if (*(int*)data & TIOCM_DTR) ce_set_dtr (c, 0);
2037 if (*(int*)data & TIOCM_RTS) ce_set_rts (c, 0);
2042 case TIOCMGET: /* Get modem status */
2043 *(int*)data = ce_modem_status (c);
2050 #if __FreeBSD_version >= 500000
2051 static int ng_ce_constructor (node_p node)
2053 drv_t *d = NG_NODE_PRIVATE (node);
2055 static int ng_ce_constructor (node_p *node)
2057 drv_t *d = (*node)->private;
2059 CE_DEBUG (d, ("Constructor\n"));
2063 static int ng_ce_newhook (node_p node, hook_p hook, const char *name)
2066 #if __FreeBSD_version >= 500000
2067 drv_t *d = NG_NODE_PRIVATE (node);
2069 drv_t *d = node->private;
2071 bdrv_t *bd = d->board->sys;
2073 CE_DEBUG (d, ("Newhook\n"));
2074 /* Attach debug hook */
2075 if (strcmp (name, NG_CE_HOOK_DEBUG) == 0) {
2076 #if __FreeBSD_version >= 500000
2077 NG_HOOK_SET_PRIVATE (hook, NULL);
2081 d->debug_hook = hook;
2085 /* Check for raw hook */
2086 if (strcmp (name, NG_CE_HOOK_RAW) != 0)
2089 #if __FreeBSD_version >= 500000
2090 NG_HOOK_SET_PRIVATE (hook, d);
2103 static char *format_timeslots (u_long s)
2105 static char buf [100];
2109 for (i=1; i<32; ++i)
2111 int prev = (i > 1) & (s >> (i-1));
2112 int next = (i < 31) & (s >> (i+1));
2122 *p++ = '0' + i / 10;
2123 *p++ = '0' + i % 10;
2129 static int print_modems (char *s, ce_chan_t *c, int need_header)
2131 int status = ce_modem_status (c);
2135 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2136 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2137 status & TIOCM_LE ? "On" : "-",
2138 status & TIOCM_DTR ? "On" : "-",
2139 status & TIOCM_DSR ? "On" : "-",
2140 status & TIOCM_RTS ? "On" : "-",
2141 status & TIOCM_CTS ? "On" : "-",
2142 status & TIOCM_CD ? "On" : "-");
2146 static int print_stats (char *s, ce_chan_t *c, int need_header)
2151 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2152 length += sprintf (s + length, "%7ld %7ld %7ld %8lu %7ld %7ld %8lu %7ld %7ld\n",
2153 c->rintr, c->tintr, 0l, (unsigned long) c->ibytes,
2154 c->ipkts, c->overrun + c->frame + c->crc,
2155 (unsigned long) c->obytes, c->opkts, c->underrun);
2159 static char *format_e1_status (u_char status)
2161 static char buf [80];
2163 if (status & E1_NOALARM)
2166 if (status & E1_LOS) strcat (buf, ",LOS");
2167 if (status & E1_AIS) strcat (buf, ",AIS");
2168 if (status & E1_LOF) strcat (buf, ",LOF");
2169 if (status & E1_LOMF) strcat (buf, ",LOMF");
2170 if (status & E1_FARLOF) strcat (buf, ",FARLOF");
2171 if (status & E1_AIS16) strcat (buf, ",AIS16");
2172 if (status & E1_FARLOMF) strcat (buf, ",FARLOMF");
2173 if (status & E1_TSTREQ) strcat (buf, ",TSTREQ");
2174 if (status & E1_TSTERR) strcat (buf, ",TSTERR");
2180 static int print_frac (char *s, int leftalign, u_long numerator, u_long divider)
2184 if (numerator < 1 || divider < 1) {
2185 length += sprintf (s+length, leftalign ? "/- " : " -");
2188 n = (int) (0.5 + 1000.0 * numerator / divider);
2190 length += sprintf (s+length, leftalign ? "/.%-3d" : " .%03d", n);
2193 *(s + length) = leftalign ? '/' : ' ';
2196 if (n >= 1000000) n = (n+500) / 1000 * 1000;
2197 else if (n >= 100000) n = (n+50) / 100 * 100;
2198 else if (n >= 10000) n = (n+5) / 10 * 10;
2201 case 1000: length += printf (s+length, ".999"); return length;
2202 case 10000: n = 9990; break;
2203 case 100000: n = 99900; break;
2204 case 1000000: n = 999000; break;
2206 if (n < 10000) length += sprintf (s+length, "%d.%d", n/1000, n/10%100);
2207 else if (n < 100000) length += sprintf (s+length, "%d.%d", n/1000, n/100%10);
2208 else if (n < 1000000) length += sprintf (s+length, "%d.", n/1000);
2209 else length += sprintf (s+length, "%d", n/1000);
2214 static int print_e1_stats (char *s, ce_chan_t *c)
2216 struct e1_counters total;
2220 totsec = c->totsec + c->cursec;
2221 total.bpv = c->total.bpv + c->currnt.bpv;
2222 total.fse = c->total.fse + c->currnt.fse;
2223 total.crce = c->total.crce + c->currnt.crce;
2224 total.rcrce = c->total.rcrce + c->currnt.rcrce;
2225 total.uas = c->total.uas + c->currnt.uas;
2226 total.les = c->total.les + c->currnt.les;
2227 total.es = c->total.es + c->currnt.es;
2228 total.bes = c->total.bes + c->currnt.bes;
2229 total.ses = c->total.ses + c->currnt.ses;
2230 total.oofs = c->total.oofs + c->currnt.oofs;
2231 total.css = c->total.css + c->currnt.css;
2232 total.dm = c->total.dm + c->currnt.dm;
2234 length += sprintf (s + length, " Unav/Degr Bpv/Fsyn CRC/RCRC Err/Lerr Sev/Bur Oof/Slp Status\n");
2236 /* Unavailable seconds, degraded minutes */
2237 length += print_frac (s + length, 0, c->currnt.uas, c->cursec);
2238 length += print_frac (s + length, 1, 60 * c->currnt.dm, c->cursec);
2240 /* Bipolar violations, frame sync errors */
2241 length += print_frac (s + length, 0, c->currnt.bpv, c->cursec);
2242 length += print_frac (s + length, 1, c->currnt.fse, c->cursec);
2244 /* CRC errors, remote CRC errors (E-bit) */
2245 length += print_frac (s + length, 0, c->currnt.crce, c->cursec);
2246 length += print_frac (s + length, 1, c->currnt.rcrce, c->cursec);
2248 /* Errored seconds, line errored seconds */
2249 length += print_frac (s + length, 0, c->currnt.es, c->cursec);
2250 length += print_frac (s + length, 1, c->currnt.les, c->cursec);
2252 /* Severely errored seconds, burst errored seconds */
2253 length += print_frac (s + length, 0, c->currnt.ses, c->cursec);
2254 length += print_frac (s + length, 1, c->currnt.bes, c->cursec);
2256 /* Out of frame seconds, controlled slip seconds */
2257 length += print_frac (s + length, 0, c->currnt.oofs, c->cursec);
2258 length += print_frac (s + length, 1, c->currnt.css, c->cursec);
2260 length += sprintf (s + length, " %s\n", format_e1_status (c->status));
2262 /* Print total statistics. */
2263 length += print_frac (s + length, 0, total.uas, totsec);
2264 length += print_frac (s + length, 1, 60 * total.dm, totsec);
2266 length += print_frac (s + length, 0, total.bpv, totsec);
2267 length += print_frac (s + length, 1, total.fse, totsec);
2269 length += print_frac (s + length, 0, total.crce, totsec);
2270 length += print_frac (s + length, 1, total.rcrce, totsec);
2272 length += print_frac (s + length, 0, total.es, totsec);
2273 length += print_frac (s + length, 1, total.les, totsec);
2275 length += print_frac (s + length, 0, total.ses, totsec);
2276 length += print_frac (s + length, 1, total.bes, totsec);
2278 length += print_frac (s + length, 0, total.oofs, totsec);
2279 length += print_frac (s + length, 1, total.css, totsec);
2281 length += sprintf (s + length, " -- Total\n");
2285 static int print_chan (char *s, ce_chan_t *c)
2290 length += sprintf (s + length, "ce%d", c->board->num * NCHAN + c->num);
2292 length += sprintf (s + length, " debug=%d", d->chan->debug);
2294 if (c->board->mux) {
2295 length += sprintf (s + length, " cfg=C");
2297 length += sprintf (s + length, " cfg=A");
2301 length += sprintf (s + length, " %ld", c->baud);
2303 length += sprintf (s + length, " extclock");
2305 if (c->type == T_E1)
2307 case GSYN_INT : length += sprintf (s + length, " syn=int"); break;
2308 case GSYN_RCV : length += sprintf (s + length, " syn=rcv"); break;
2309 case GSYN_RCV0 : length += sprintf (s + length, " syn=rcv0"); break;
2310 case GSYN_RCV1 : length += sprintf (s + length, " syn=rcv1"); break;
2312 if (c->type == T_E1)
2313 length += sprintf (s + length, " higain=%s", c->higain ? "on" : "off");
2315 length += sprintf (s + length, " loop=%s", c->lloop ? "on" : "off");
2317 if (c->type == T_E1)
2318 length += sprintf (s + length, " ts=%s", format_timeslots (c->ts));
2319 length += sprintf (s + length, "\n");
2323 #if __FreeBSD_version >= 500000
2324 static int ng_ce_rcvmsg (node_p node, item_p item, hook_p lasthook)
2326 drv_t *d = NG_NODE_PRIVATE (node);
2327 struct ng_mesg *msg;
2329 static int ng_ce_rcvmsg (node_p node, struct ng_mesg *msg,
2330 const char *retaddr, struct ng_mesg **rptr)
2332 drv_t *d = node->private;
2334 struct ng_mesg *resp = NULL;
2337 CE_DEBUG (d, ("Rcvmsg\n"));
2338 #if __FreeBSD_version >= 500000
2339 NGI_GET_MSG (item, msg);
2341 switch (msg->header.typecookie) {
2347 printf ("Not implemented yet\n");
2351 case NGM_GENERIC_COOKIE:
2352 switch (msg->header.cmd) {
2357 case NGM_TEXT_STATUS: {
2360 int dl = sizeof (struct ng_mesg) + 730;
2362 #if __FreeBSD_version >= 500000
2363 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2369 MALLOC (resp, struct ng_mesg *, dl,
2370 M_NETGRAPH, M_NOWAIT);
2379 l += print_chan (s + l, d->chan);
2380 l += print_stats (s + l, d->chan, 1);
2381 l += print_modems (s + l, d->chan, 1);
2382 l += print_e1_stats (s + l, d->chan);
2384 l += sprintf (s + l, "Error: node not connect to channel");
2385 #if __FreeBSD_version < 500000
2386 (resp)->header.version = NG_VERSION;
2387 (resp)->header.arglen = strlen (s) + 1;
2388 (resp)->header.token = msg->header.token;
2389 (resp)->header.typecookie = NGM_CE_COOKIE;
2390 (resp)->header.cmd = msg->header.cmd;
2392 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN);
2398 #if __FreeBSD_version >= 500000
2399 NG_RESPOND_MSG (error, node, item, resp);
2403 FREE (msg, M_NETGRAPH);
2408 #if __FreeBSD_version >= 500000
2409 static int ng_ce_rcvdata (hook_p hook, item_p item)
2411 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2413 #if __FreeBSD_version < 502120
2416 struct ng_tag_prio *ptag;
2419 static int ng_ce_rcvdata (hook_p hook, struct mbuf *m, meta_p meta)
2421 drv_t *d = hook->node->private;
2423 bdrv_t *bd = d->board->sys;
2427 CE_DEBUG2 (d, ("Rcvdata\n"));
2428 #if __FreeBSD_version >= 500000
2429 NGI_GET_M (item, m);
2430 #if __FreeBSD_version < 502120
2431 NGI_GET_META (item, meta);
2433 NG_FREE_ITEM (item);
2434 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2436 #if __FreeBSD_version < 502120
2437 NG_FREE_META (meta);
2440 if (! hook->private || ! d) {
2441 NG_FREE_DATA (m,meta);
2446 #if __FreeBSD_version >= 502120
2447 /* Check for high priority data */
2448 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2449 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2454 q = (meta && meta->priority > 0) ? &d->hi_queue : &d->queue;
2459 #if __FreeBSD_version >= 500000
2461 if (_IF_QFULL (q)) {
2467 #if __FreeBSD_version < 502120
2468 NG_FREE_META (meta);
2479 NG_FREE_DATA (m, meta);
2490 static int ng_ce_rmnode (node_p node)
2492 #if __FreeBSD_version >= 500000
2493 drv_t *d = NG_NODE_PRIVATE (node);
2495 CE_DEBUG (d, ("Rmnode\n"));
2496 if (d && d->running) {
2497 bdrv_t *bd = d->board->sys;
2505 #if __FreeBSD_version >= 502120
2506 if (node->nd_flags & NGF_REALLY_DIE) {
2508 if (node->nd_flags & NG_REALLY_DIE) {
2510 NG_NODE_SET_PRIVATE (node, NULL);
2511 NG_NODE_UNREF (node);
2513 #if __FreeBSD_version >= 502120
2514 NG_NODE_REVIVE(node); /* Persistant node */
2516 node->nd_flags &= ~NG_INVALID;
2519 #else /* __FreeBSD_version < 500000 */
2520 drv_t *d = node->private;
2522 if (d && d->running) {
2523 bdrv_t *bd = d->board->sys;
2531 node->flags |= NG_INVALID;
2541 static void ng_ce_watchdog (void *arg)
2546 if (d->timeout == 1)
2550 callout_reset (&d->timeout_handle, hz, ng_ce_watchdog, d);
2554 static int ng_ce_connect (hook_p hook)
2556 #if __FreeBSD_version >= 500000
2557 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2559 drv_t *d = hook->node->private;
2563 CE_DEBUG (d, ("Connect\n"));
2564 callout_reset (&d->timeout_handle, hz, ng_ce_watchdog, d);
2570 static int ng_ce_disconnect (hook_p hook)
2572 #if __FreeBSD_version >= 500000
2573 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2575 drv_t *d = hook->node->private;
2579 CE_DEBUG (d, ("Disconnect\n"));
2580 #if __FreeBSD_version >= 500000
2581 if (NG_HOOK_PRIVATE (hook))
2586 bdrv_t *bd = d->board->sys;
2593 /* If we were wait it than it reasserted now, just stop it. */
2594 if (!callout_drain (&d->timeout_handle))
2595 callout_stop (&d->timeout_handle);
2601 static int ce_modevent (module_t mod, int type, void *unused)
2603 #if __FreeBSD_version < 500000
2605 struct cdevsw *cdsw;
2607 static int load_count = 0;
2609 #if __FreeBSD_version < 500000
2610 dev = makedev (CDEV_MAJOR, 0);
2612 #if __FreeBSD_version >= 502103
2614 ce_cdevsw.d_flags &= ~D_NEEDGIANT;
2619 #if __FreeBSD_version < 500000
2621 (cdsw = devsw (dev)) &&
2622 cdsw->d_maj == CDEV_MAJOR) {
2623 printf ("Tau32-PCI driver is already in system\n");
2627 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2628 if (ng_newtype (&typestruct))
2629 printf ("Failed to register ng_ce\n");
2632 #if __FreeBSD_version <= 500000
2633 cdevsw_add (&ce_cdevsw);
2635 #if __FreeBSD_version >= 500000
2636 callout_init (&timeout_handle, ce_mpsafenet?CALLOUT_MPSAFE:0);
2638 callout_init (&timeout_handle);
2640 callout_reset (&timeout_handle, hz*5, ce_timeout, 0);
2643 if (load_count == 1) {
2644 printf ("Removing device entry for Tau32-PCI\n");
2645 #if __FreeBSD_version <= 500000
2646 cdevsw_remove (&ce_cdevsw);
2648 #if __FreeBSD_version >= 500000 && defined NETGRAPH
2649 ng_rmtype (&typestruct);
2652 /* If we were wait it than it reasserted now, just stop it.
2653 * Actually we shouldn't get this condition. But code could be
2654 * changed in the future, so just be a litle paranoid.
2656 if (!callout_drain (&timeout_handle))
2657 callout_stop (&timeout_handle);
2667 #if __FreeBSD_version >= 502100
2668 static struct ng_type typestruct = {
2669 .version = NG_ABI_VERSION,
2670 .name = NG_CE_NODE_TYPE,
2671 .constructor = ng_ce_constructor,
2672 .rcvmsg = ng_ce_rcvmsg,
2673 .shutdown = ng_ce_rmnode,
2674 .newhook = ng_ce_newhook,
2675 .connect = ng_ce_connect,
2676 .rcvdata = ng_ce_rcvdata,
2677 .disconnect = ng_ce_disconnect,
2679 #else /* __FreeBSD_version < 502100 */
2680 static struct ng_type typestruct = {
2681 #if __FreeBSD_version >= 500000
2695 #if __FreeBSD_version < 500000
2701 #endif /* __FreeBSD_version < 502100 */
2705 #if __FreeBSD_version >= 500000
2707 MODULE_DEPEND (ng_ce, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2709 MODULE_DEPEND (ce, sppp, 1, 1, 1);
2712 DRIVER_MODULE (cemod, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2714 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2716 #else /* if __FreeBSD_version < 500000*/
2718 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ng_mod_event, &typestruct);
2720 DRIVER_MODULE (ce, pci, ce_driver, ce_devclass, ce_modevent, NULL);
2722 #endif /* __FreeBSD_version < 500000 */