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