Merge from vendor branch LIBSTDC++:
[dragonfly.git] / sys / dev / netif / cx / if_cx.c
1 /*
2  * Cronyx-Sigma adapter driver for FreeBSD.
3  * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
4  * and asyncronous channels with full modem control.
5  * Keepalive protocol implemented in both Cisco and PPP modes.
6  *
7  * Copyright (C) 1994 Cronyx Ltd.
8  * Author: Serge Vakulenko, <vak@zebub.msk.su>
9  *
10  * This software is distributed with NO WARRANTIES, not even the implied
11  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * Authors grant any other persons or organisations permission to use
14  * or modify this software as long as this message is kept with the software,
15  * all derivative works or modified versions.
16  *
17  * Version 1.9, Wed Oct  4 18:58:15 MSK 1995
18  *
19  * $FreeBSD: src/sys/i386/isa/if_cx.c,v 1.32 1999/11/18 08:36:42 peter Exp $
20  * $DragonFly: src/sys/dev/netif/cx/if_cx.c,v 1.8 2004/01/06 01:40:47 dillon Exp $
21  *
22  */
23 #undef DEBUG
24
25 #include "use_cx.h"
26 #include "use_sppp.h"
27 #if NSPPP <= 0
28 #error The device 'cx' requires sppp.
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/mbuf.h>
36 #include <sys/sockio.h>
37 #include <sys/socket.h>
38 #include <sys/conf.h>
39
40 #include <net/if.h>
41
42 #include <net/bpf.h>
43
44 #include <bus/isa/i386/isa_device.h>
45 #define watchdog_func_t void(*)(struct ifnet *)
46 #define start_func_t    void(*)(struct ifnet*)
47
48 #include <net/sppp/if_sppp.h>
49 #include <machine/cronyx.h>
50 #include "cxreg.h"
51 #include "cx.c"
52
53 /* XXX exported. */
54 void cxswitch (cx_chan_t *c, cx_soft_opt_t new);
55
56 static int cxprobe (struct isa_device *id);
57 static int cxattach (struct isa_device *id);
58 static void cxput (cx_chan_t *c, char b);
59 static void cxsend (cx_chan_t *c);
60 static void cxrinth (cx_chan_t *c);
61 static ointhand2_t cxintr;
62 static int cxtinth (cx_chan_t *c);
63
64 #ifdef DEBUG
65 #   define print(s)     printf s
66 #else
67 #   define print(s)     {/*void*/}
68 #endif
69
70 #define TXTIMEOUT       10              /* transmit timeout in seconds */
71 #define DMABUFSZ        (6*256)         /* buffer size */
72 #define PPP_HEADER_LEN  4               /* size of PPP header */
73
74 /*
75  * Under BSDI it's possible to use general p2p protocol scheme,
76  * as well as our own one.  Switching is done via IFF_ALTPHYS flag.
77  * Our ifnet pointer holds the buffer large enough to contain
78  * any of sppp and p2p structures.
79  */
80 #define IFSTRUCTSZ   (sizeof (struct sppp))
81 #define IFNETSZ         (sizeof (struct ifnet))
82
83 static int cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
84 static void cxstart (struct ifnet *ifp);
85 static void cxwatchdog (struct ifnet *ifp);
86 static void cxinput (cx_chan_t *c, void *buf, unsigned len);
87 extern int cxrinta (cx_chan_t *c);
88 extern void cxtinta (cx_chan_t *c);
89 extern void cxmint (cx_chan_t *c);
90 extern timeout_t cxtimeout;
91 static void cxdown (cx_chan_t *c);
92 static void cxup (cx_chan_t *c);
93
94 cx_board_t cxboard [NCX];           /* adapter state structures */
95 cx_chan_t *cxchan [NCX*NCHAN];      /* unit to channel struct pointer */
96
97 extern struct cdevsw cx_cdevsw;
98
99 static unsigned short irq_valid_values [] = { 3, 5, 7, 10, 11, 12, 15, 0 };
100 static unsigned short drq_valid_values [] = { 5, 6, 7, 0 };
101 static unsigned short port_valid_values [] = {
102         0x240, 0x260, 0x280, 0x300, 0x320, 0x380, 0x3a0, 0,
103 };
104
105 DECLARE_DUMMY_MODULE(if_cx);
106
107 /*
108  * Check that the value is contained in the list of correct values.
109  */
110 static int valid (unsigned short value, unsigned short *list)
111 {
112         while (*list)
113                 if (value == *list++)
114                         return (1);
115         return (0);
116 }
117
118 /*
119  * Print the mbuf chain, for debug purposes only.
120  */
121 static void printmbuf (struct mbuf *m)
122 {
123         printf ("mbuf:");
124         for (; m; m=m->m_next) {
125                 if (m->m_flags & M_PKTHDR)
126                         printf (" HDR %d:", m->m_pkthdr.len);
127                 if (m->m_flags & M_EXT)
128                         printf (" EXT:");
129                 printf (" %d", m->m_len);
130         }
131         printf ("\n");
132 }
133
134 /*
135  * Make an mbuf from data.
136  */
137 static struct mbuf *makembuf (void *buf, unsigned len)
138 {
139         struct mbuf *m, *o, *p;
140
141         MGETHDR (m, M_DONTWAIT, MT_DATA);
142         if (! m)
143                 return (0);
144         if (len >= MINCLSIZE)
145                 MCLGET (m, M_DONTWAIT);
146         m->m_pkthdr.len = len;
147         m->m_len = 0;
148
149         p = m;
150         while (len) {
151                 unsigned n = M_TRAILINGSPACE (p);
152                 if (n > len)
153                         n = len;
154
155                 if (! n) {
156                         /* Allocate new mbuf. */
157                         o = p;
158                         MGET (p, M_DONTWAIT, MT_DATA);
159                         if (! p) {
160                                 m_freem (m);
161                                 return (0);
162                         }
163                         if (len >= MINCLSIZE)
164                                 MCLGET (p, M_DONTWAIT);
165                         p->m_len = 0;
166                         o->m_next = p;
167
168                         n = M_TRAILINGSPACE (p);
169                         if (n > len)
170                                 n = len;
171                 }
172
173                 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
174
175                 p->m_len += n;
176                 buf = (char *)buf + n;
177                 len -= n;
178         }
179         return (m);
180 }
181
182 /*
183  * Test the presence of the adapter on the given i/o port.
184  */
185 static int
186 cxprobe (struct isa_device *id)
187 {
188         int unit = id->id_unit;
189         int iobase = id->id_iobase;
190         int irq = id->id_irq;
191         int drq = id->id_drq;
192         int irqnum;
193         irqnum = ffs (irq) - 1;
194
195         print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",
196                 unit, iobase, irqnum, drq));
197         if (! valid (irqnum, irq_valid_values)) {
198                 printf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);
199                 return (0);
200         }
201         if (! valid (iobase, port_valid_values)) {
202                 printf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);
203                 return (0);
204         }
205         if (! valid (drq, drq_valid_values)) {
206                 printf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);
207                 return (0);
208         }
209         if (! cx_probe_board (iobase))
210                 return (0);
211
212         return (1);
213 }
214
215 /*
216  * The adapter is present, initialize the driver structures.
217  */
218
219 static int
220 cxattach (struct isa_device *id)
221 {
222         int unit = id->id_unit;
223         int iobase = id->id_iobase;
224         int irq = id->id_irq;
225         int drq = id->id_drq;
226         cx_board_t *b = cxboard + unit;
227         int i;
228         struct sppp *sp;
229
230         id->id_ointr = cxintr;
231
232         /* Initialize the board structure. */
233         cx_init (b, unit, iobase, ffs(irq)-1, drq);
234
235         for (i=0; i<NCHAN; ++i) {
236                 cx_chan_t *c = b->chan + i;
237                 int u = b->num*NCHAN + i;
238                 cxchan[u] = c;
239
240                 if (c->type == T_NONE)
241                         continue;
242
243                 /* Allocate the buffer memory. */
244                 c->arbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
245                 c->brbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
246                 c->atbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
247                 c->btbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
248
249                 /* All buffers should be located in lower 16M of memory! */
250                 if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {
251                         printf ("cx%d.%d: No memory for channel buffers\n",
252                                 c->board->num, c->num);
253                         c->type = T_NONE;
254                 }
255
256                 switch (c->type) {
257                 case T_SYNC_RS232:
258                 case T_SYNC_V35:
259                 case T_SYNC_RS449:
260                 case T_UNIV_RS232:
261                 case T_UNIV_RS449:
262                 case T_UNIV_V35:
263                         c->ifp = malloc (IFSTRUCTSZ, M_DEVBUF, M_NOWAIT);
264                         if (! c->ifp) {
265                                 printf ("cx%d.%d: No memory for ifnet buffer\n",
266                                         c->board->num, c->num);
267                                 c->type = T_NONE;
268                                 continue;
269                         }
270                         bzero (c->ifp, IFSTRUCTSZ);
271                         c->master = c->ifp;
272                         c->ifp->if_softc = c;
273                         if_initname(c->ifp, "cx", u);
274                         c->ifp->if_mtu = PP_MTU;
275                         c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
276                         c->ifp->if_ioctl = cxsioctl;
277                         c->ifp->if_start = (start_func_t) cxstart;
278                         c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;
279                         /* Init routine is never called by upper level? */
280                         sppp_attach (c->ifp);
281                         if_attach (c->ifp);
282                         sp = (struct sppp*) c->ifp;
283                         /* If BPF is in the kernel, call the attach for it. */
284                         bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);
285                 }
286         }
287
288         /* Reset the adapter. */
289         cx_setup_board (b);
290
291         /* Activate the timeout routine. */
292         if (unit == 0)
293                 timeout (cxtimeout, 0, hz*5);
294
295         printf ("cx%d: <Cronyx-%s>\n", unit, b->name);
296         make_dev(&cx_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
297         return (1);
298 }
299
300 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
301
302 /*
303  * Process an ioctl request.
304  */
305 static int
306 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
307 {
308         cx_chan_t *q, *c = ifp->if_softc;
309         int error, s, was_up, should_be_up;
310
311         /*
312          * No socket ioctls while the channel is in async mode.
313          */
314         if (c->type==T_NONE || c->mode==M_ASYNC)
315                 return (EINVAL);
316
317         /*
318          * Socket ioctls on slave subchannels are not allowed.
319          */
320         if (c->master != c->ifp)
321                 return (EBUSY);
322
323         was_up = (ifp->if_flags & IFF_RUNNING) != 0;
324         error = sppp_ioctl (ifp, cmd, data);
325         if (error)
326                 return (error);
327
328         print (("cxioctl (%d.%d, ", c->board->num, c->num));
329         switch (cmd) {
330         default:
331                 print (("0x%x)\n", cmd));
332                 return (0);
333         case SIOCADDMULTI:
334                 print (("SIOCADDMULTI)\n"));
335                 return (0);
336         case SIOCDELMULTI:
337                 print (("SIOCDELMULTI)\n"));
338                 return (0);
339         case SIOCSIFFLAGS:
340                 print (("SIOCSIFFLAGS)\n"));
341                 break;
342         case SIOCSIFADDR:
343                 print (("SIOCSIFADDR)\n"));
344                 break;
345         }
346
347         /* We get here only in case of SIFFLAGS or SIFADDR. */
348         s = splimp ();
349         should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
350         if (!was_up && should_be_up) {
351                 /* Interface goes up -- start it. */
352                 cxup (c);
353
354                 /* Start all slave subchannels. */
355                 for (q=c->slaveq; q; q=q->slaveq)
356                         cxup (q);
357
358                 cxstart (c->ifp);
359         } else if (was_up && !should_be_up) {
360                 /* Interface is going down -- stop it. */
361                 cxdown (c);
362
363                 /* Stop all slave subchannels. */
364                 for (q=c->slaveq; q; q=q->slaveq)
365                         cxdown (q);
366
367                 /* Flush the interface output queue */
368                 if (! c->sopt.ext)
369                         sppp_flush (c->ifp);
370         }
371         splx (s);
372         return (0);
373 }
374
375 /*
376  * Stop the interface.  Called on splimp().
377  */
378 static void
379 cxdown (cx_chan_t *c)
380 {
381         unsigned short port = c->chip->port;
382
383         print (("cx%d.%d: cxdown\n", c->board->num, c->num));
384
385         /* The interface is down, stop it */
386         c->ifp->if_flags &= ~IFF_OACTIVE;
387
388         /* Reset the channel (for sync modes only) */
389                 outb (CAR(port), c->num & 3);
390                 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
391
392         cx_setup_chan (c);
393 }
394
395 /*
396  * Start the interface.  Called on splimp().
397  */
398 static void
399 cxup (cx_chan_t *c)
400 {
401         unsigned short port = c->chip->port;
402
403                 /* The interface is up, start it */
404                 print (("cx%d.%d: cxup\n", c->board->num, c->num));
405
406                 /* Initialize channel, enable receiver and transmitter */
407                 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
408                 /* Repeat the command, to avoid the rev.H bug */
409                 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
410
411                 /* Start receiver */
412                 outw (ARBCNT(port), DMABUFSZ);
413                 outb (ARBSTS(port), BSTS_OWN24);
414                 outw (BRBCNT(port), DMABUFSZ);
415                 outb (BRBSTS(port), BSTS_OWN24);
416
417                 /* Raise DTR and RTS */
418                 cx_chan_dtr (c, 1);
419                 cx_chan_rts (c, 1);
420
421                 /* Enable interrupts */
422                 outb (IER(port), IER_RXD | IER_TXD);
423 }
424
425 /*
426  * Fill transmitter buffer with data.
427  */
428 static void 
429 cxput (cx_chan_t *c, char b)
430 {
431         struct mbuf *m;
432         unsigned char *buf;
433         unsigned short port = c->chip->port, len, cnt_port, sts_port;
434
435         /* Choose the buffer. */
436         if (b == 'A') {
437                 buf      = c->atbuf;
438                 cnt_port = ATBCNT(port);
439                 sts_port = ATBSTS(port);
440         } else {
441                 buf      = c->btbuf;
442                 cnt_port = BTBCNT(port);
443                 sts_port = BTBSTS(port);
444         }
445
446         /* Is it busy? */
447         if (inb (sts_port) & BSTS_OWN24) {
448                 if (c->ifp->if_flags & IFF_DEBUG)
449                         print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
450                                 c->board->num, c->num, b,
451                                 inb (sts_port), BSTS_BITS));
452                 goto ret;
453         }
454
455         /* Get the packet to send. */
456         m = sppp_dequeue (c->master);
457         if (! m)
458                 return;
459         len = m->m_pkthdr.len;
460
461         /* Count the transmitted bytes to the subchannel, not the master. */
462         c->master->if_obytes -= len + 3;
463         c->ifp->if_obytes += len + 3;
464         c->stat->obytes += len + 3;
465
466         if (len >= DMABUFSZ) {
467                 printf ("cx%d.%d: too long packet: %d bytes: ",
468                         c->board->num, c->num, len);
469                 printmbuf (m);
470                 m_freem (m);
471                 return;
472         }
473         m_copydata (m, 0, len, buf);
474         if (c->ifp->if_bpf)
475                 bpf_mtap (c->ifp, m);
476         m_freem (m);
477
478         /* Start transmitter. */
479         outw (cnt_port, len);
480         outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
481
482         if (c->ifp->if_flags & IFF_DEBUG)
483                 print (("cx%d.%d: enqueue %d bytes to %c\n",
484                         c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
485 ret:
486         c->ifp->if_flags |= IFF_OACTIVE;
487 }
488
489 /*
490  * Start output on the (slave) interface.  Get another datagram to send
491  * off of the interface queue, and copy it to the interface
492  * before starting the output.
493  */
494 static void
495 cxsend (cx_chan_t *c)
496 {
497         unsigned short port = c->chip->port;
498
499         if (c->ifp->if_flags & IFF_DEBUG)
500                 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
501
502         /* No output if the interface is down. */
503         if (! (c->ifp->if_flags & IFF_RUNNING))
504                 return;
505
506         /* Set the current channel number. */
507         outb (CAR(port), c->num & 3);
508
509         /* Determine the buffer order. */
510         if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
511                 cxput (c, 'B');
512                 cxput (c, 'A');
513         } else {
514                 cxput (c, 'A');
515                 cxput (c, 'B');
516         }
517
518         /* Set up transmit timeout. */
519         if (c->master->if_flags & IFF_OACTIVE)
520                 c->master->if_timer = TXTIMEOUT;
521
522         /*
523          * Enable TXMPTY interrupt,
524          * to catch the case when the second buffer is empty.
525          */
526         if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
527             (inb (BTBSTS(port)) & BSTS_OWN24)) {
528                 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
529         } else
530                 outb (IER(port), IER_RXD | IER_TXD);
531 }
532
533 /*
534  * Start output on the (master) interface and all slave interfaces.
535  * Always called on splimp().
536  */
537 static void
538 cxstart (struct ifnet *ifp)
539 {
540         cx_chan_t *q, *c = ifp->if_softc;
541
542         if (c->ifp->if_flags & IFF_DEBUG)
543                 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
544
545         /* Start the master subchannel. */
546         cxsend (c);
547
548         /* Start all slave subchannels. */
549         if (c->slaveq && ! sppp_isempty (c->master))
550                 for (q=c->slaveq; q; q=q->slaveq)
551                         if ((q->ifp->if_flags & IFF_RUNNING) &&
552                             ! (q->ifp->if_flags & IFF_OACTIVE))
553                                 cxsend (q);
554 }
555
556 /*
557  * Handle transmit timeouts.
558  * Recover after lost transmit interrupts.
559  * Always called on splimp().
560  */
561 static void
562 cxwatchdog (struct ifnet *ifp)
563 {
564         cx_chan_t *q, *c = ifp->if_softc;
565
566         if (! (ifp->if_flags & IFF_RUNNING))
567                 return;
568         if (ifp->if_flags & IFF_DEBUG)
569                 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
570
571         cxdown (c);
572         for (q=c->slaveq; q; q=q->slaveq)
573                 cxdown (q);
574
575         cxup (c);
576         for (q=c->slaveq; q; q=q->slaveq)
577                 cxup (q);
578
579                 cxstart (ifp);
580 }
581
582 /*
583  * Handle receive interrupts, including receive errors and
584  * receive timeout interrupt.
585  */
586 static void 
587 cxrinth (cx_chan_t *c)
588 {
589         unsigned short port = c->chip->port;
590         unsigned short len, risr = inw (RISR(port));
591
592         /* Receive errors. */
593         if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
594                 if (c->ifp->if_flags & IFF_DEBUG)
595                         printf ("cx%d.%d: receive error, risr=%b\n",
596                                 c->board->num, c->num, risr, RISH_BITS);
597                 ++c->ifp->if_ierrors;
598                 ++c->stat->ierrs;
599                 if (risr & RIS_OVERRUN)
600                         ++c->ifp->if_collisions;
601         } else if (risr & RIS_EOBUF) {
602                 if (c->ifp->if_flags & IFF_DEBUG)
603                         print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
604                                 c->board->num, c->num, risr, RISH_BITS,
605                                 inb (ARBSTS(port)), BSTS_BITS,
606                                 inb (BRBSTS(port)), BSTS_BITS));
607                 ++c->stat->ipkts;
608
609                 /* Handle received data. */
610                 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
611                 c->stat->ibytes += len;
612                 if (len > DMABUFSZ) {
613                         /* Fatal error: actual DMA transfer size
614                          * exceeds our buffer size.  It could be caused
615                          * by incorrectly programmed DMA register or
616                          * hardware fault.  Possibly, should panic here. */
617                         printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
618                                c->board->num, c->num, len);
619                         ++c->ifp->if_ierrors;
620                 } else if (! (risr & RIS_EOFR)) {
621                         /* The received frame does not fit in the DMA buffer.
622                          * It could be caused by serial lie noise,
623                          * or if the peer has too big MTU. */
624                         if (c->ifp->if_flags & IFF_DEBUG)
625                                 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
626                                         c->board->num, c->num, risr, RISH_BITS);
627                         ++c->ifp->if_ierrors;
628                 } else {
629                         /* Valid frame received. */
630                         if (c->ifp->if_flags & IFF_DEBUG)
631                                 print (("cx%d.%d: hdlc received %d bytes\n",
632                                 c->board->num, c->num, len));
633                         cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
634                         ++c->ifp->if_ipackets;
635                 }
636         } else if (c->ifp->if_flags & IFF_DEBUG) {
637                 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
638                         c->board->num, c->num, risr, RISH_BITS));
639                 ++c->stat->ierrs;
640         }
641
642         /* Restart receiver. */
643         if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
644                 outw (ARBCNT(port), DMABUFSZ);
645                 outb (ARBSTS(port), BSTS_OWN24);
646         }
647         if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
648                 outw (BRBCNT(port), DMABUFSZ);
649                 outb (BRBSTS(port), BSTS_OWN24);
650         }
651 }
652
653 /*
654  * Handle transmit interrupt.
655  */
656 static int
657 cxtinth (cx_chan_t *c)
658 {
659         unsigned short port = c->chip->port;
660         unsigned char tisr = inb (TISR(port));
661         unsigned char teoir = 0;
662
663         c->ifp->if_flags &= ~IFF_OACTIVE;
664         if (c->ifp == c->master)
665                 c->ifp->if_timer = 0;
666
667         if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
668                 /* if (c->ifp->if_flags & IFF_DEBUG) */
669                         print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
670                                 c->board->num, c->num, tisr, TIS_BITS,
671                                 inb (ATBSTS(port)), BSTS_BITS,
672                                 inb (BTBSTS(port)), BSTS_BITS));
673                 ++c->ifp->if_oerrors;
674                 ++c->stat->oerrs;
675
676                 /* Terminate the failed buffer. */
677                 /* teoir = TEOI_TERMBUFF; */
678         } else if (c->ifp->if_flags & IFF_DEBUG)
679                 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
680                         c->board->num, c->num, tisr, TIS_BITS,
681                         inb (ATBSTS(port)), BSTS_BITS,
682                         inb (BTBSTS(port)), BSTS_BITS));
683
684         if (tisr & TIS_EOFR) {
685                 ++c->ifp->if_opackets;
686                 ++c->stat->opkts;
687         }
688
689         /* Start output on the (sub-) channel. */
690         cxsend (c);
691
692         return (teoir);
693 }
694
695 static void
696 cxintr (int bnum)
697 {
698         cx_board_t *b = cxboard + bnum;
699         while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
700                 /* Acknowledge the interrupt to enter the interrupt context. */
701                 /* Read the local interrupt vector register. */
702                 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
703                 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
704                 unsigned short port = c->chip->port;
705                 unsigned short eoiport = REOIR(port);
706                 unsigned char eoi = 0;
707
708                 if (c->type == T_NONE) {
709                         printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
710                                 c->board->num, c->num, livr);
711                         continue;       /* incorrect channel number? */
712                 }
713                 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
714                         c->board->num, c->num, livr)); */
715
716                 /* Clear RTS to stop receiver data flow while we are busy
717                  * processing the interrupt, thus avoiding underruns. */
718                 if (! c->sopt.norts) {
719                         outb (MSVR_RTS(port), 0);
720                         c->rts = 0;
721                 }
722
723                 switch (livr & 3) {
724                 case LIV_EXCEP:         /* receive exception */
725                 case LIV_RXDATA:        /* receive interrupt */
726                         ++c->stat->rintr;
727                         switch (c->mode) {
728                         case M_ASYNC: eoi = cxrinta (c); break;
729                         case M_HDLC:  cxrinth (c);       break;
730                         default:;       /* No bisync and X.21 yet */
731                         }
732                         break;
733                 case LIV_TXDATA:        /* transmit interrupt */
734                         ++c->stat->tintr;
735                         eoiport = TEOIR(port);
736                         switch (c->mode) {
737                         case M_ASYNC: cxtinta (c);       break;
738                         case M_HDLC:  eoi = cxtinth (c); break;
739                         default:;       /* No bisync and X.21 yet */
740                         }
741                         break;
742                 case LIV_MODEM:         /* modem/timer interrupt */
743                         ++c->stat->mintr;
744                         eoiport = MEOIR(port);
745                         cxmint (c);
746                         break;
747                 }
748
749                 /* Raise RTS for this channel if and only if
750                  * both receive buffers are empty. */
751                 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
752                     (inb (ARBSTS(port)) & BSTS_OWN24) &&
753                     (inb (BRBSTS(port)) & BSTS_OWN24)) {
754                         outb (MSVR_RTS(port), MSV_RTS);
755                         c->rts = 1;
756                 }
757
758                 /* Exit from interrupt context. */
759                 outb (eoiport, eoi);
760
761                 /* Master channel - start output on all idle subchannels. */
762                 if (c->master == c->ifp && c->slaveq &&
763                     (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
764                     ! sppp_isempty (c->ifp)) {
765                         cx_chan_t *q;
766
767                         for (q=c->slaveq; q; q=q->slaveq)
768                                 if ((q->ifp->if_flags & IFF_RUNNING) &&
769                                     ! (q->ifp->if_flags & IFF_OACTIVE))
770                                         cxsend (q);
771                 }
772         }
773 }
774
775 /*
776  * Process the received packet.
777  */
778 static void 
779 cxinput (cx_chan_t *c, void *buf, unsigned len)
780 {
781         /* Make an mbuf. */
782         struct mbuf *m = makembuf (buf, len);
783         if (! m) {
784                 if (c->ifp->if_flags & IFF_DEBUG)
785                         printf ("cx%d.%d: no memory for packet\n",
786                                 c->board->num, c->num);
787                 ++c->ifp->if_iqdrops;
788                 return;
789         }
790         m->m_pkthdr.rcvif = c->master;
791 #ifdef DEBUG
792         if (c->ifp->if_flags & IFF_DEBUG)
793         printmbuf (m);
794 #endif
795
796         /*
797          * Check if there's a BPF listener on this interface.
798          * If so, hand off the raw packet to bpf.
799          */
800         if (c->ifp->if_bpf)
801                 bpf_tap (c->ifp, buf, len);
802
803         /* Count the received bytes to the subchannel, not the master. */
804         c->master->if_ibytes -= len + 3;
805         c->ifp->if_ibytes += len + 3;
806
807         sppp_input (c->master, m);
808 }
809
810 void cxswitch (cx_chan_t *c, cx_soft_opt_t new)
811 {
812         new.ext = 0;
813         if (! new.ext) {
814                 struct sppp *sp = (struct sppp*) c->ifp;
815
816 #if 0 /* Doesn't work this way any more 990402 /phk */
817                 if (new.cisco)
818                         sp->pp_flags |= PP_CISCO;
819                 else
820                         sp->pp_flags &= ~PP_CISCO;
821 #endif
822                 if (new.keepalive)
823                         sp->pp_flags |= PP_KEEPALIVE;
824                 else
825                         sp->pp_flags &= ~PP_KEEPALIVE;
826         }
827         c->sopt = new;
828 }