Add a DECLARE_DUMMY_MODULE() so we can get linker_set module names
[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.7 2003/11/20 22:07:26 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                         c->ifp->if_unit = u;
274                         c->ifp->if_name = "cx";
275                         c->ifp->if_mtu = PP_MTU;
276                         c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
277                         c->ifp->if_ioctl = cxsioctl;
278                         c->ifp->if_start = (start_func_t) cxstart;
279                         c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;
280                         /* Init routine is never called by upper level? */
281                         sppp_attach (c->ifp);
282                         if_attach (c->ifp);
283                         sp = (struct sppp*) c->ifp;
284                         /* If BPF is in the kernel, call the attach for it. */
285                         bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);
286                 }
287         }
288
289         /* Reset the adapter. */
290         cx_setup_board (b);
291
292         /* Activate the timeout routine. */
293         if (unit == 0)
294                 timeout (cxtimeout, 0, hz*5);
295
296         printf ("cx%d: <Cronyx-%s>\n", unit, b->name);
297         make_dev(&cx_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
298         return (1);
299 }
300
301 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
302
303 /*
304  * Process an ioctl request.
305  */
306 static int
307 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
308 {
309         cx_chan_t *q, *c = ifp->if_softc;
310         int error, s, was_up, should_be_up;
311
312         /*
313          * No socket ioctls while the channel is in async mode.
314          */
315         if (c->type==T_NONE || c->mode==M_ASYNC)
316                 return (EINVAL);
317
318         /*
319          * Socket ioctls on slave subchannels are not allowed.
320          */
321         if (c->master != c->ifp)
322                 return (EBUSY);
323
324         was_up = (ifp->if_flags & IFF_RUNNING) != 0;
325         error = sppp_ioctl (ifp, cmd, data);
326         if (error)
327                 return (error);
328
329         print (("cxioctl (%d.%d, ", c->board->num, c->num));
330         switch (cmd) {
331         default:
332                 print (("0x%x)\n", cmd));
333                 return (0);
334         case SIOCADDMULTI:
335                 print (("SIOCADDMULTI)\n"));
336                 return (0);
337         case SIOCDELMULTI:
338                 print (("SIOCDELMULTI)\n"));
339                 return (0);
340         case SIOCSIFFLAGS:
341                 print (("SIOCSIFFLAGS)\n"));
342                 break;
343         case SIOCSIFADDR:
344                 print (("SIOCSIFADDR)\n"));
345                 break;
346         }
347
348         /* We get here only in case of SIFFLAGS or SIFADDR. */
349         s = splimp ();
350         should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
351         if (!was_up && should_be_up) {
352                 /* Interface goes up -- start it. */
353                 cxup (c);
354
355                 /* Start all slave subchannels. */
356                 for (q=c->slaveq; q; q=q->slaveq)
357                         cxup (q);
358
359                 cxstart (c->ifp);
360         } else if (was_up && !should_be_up) {
361                 /* Interface is going down -- stop it. */
362                 cxdown (c);
363
364                 /* Stop all slave subchannels. */
365                 for (q=c->slaveq; q; q=q->slaveq)
366                         cxdown (q);
367
368                 /* Flush the interface output queue */
369                 if (! c->sopt.ext)
370                         sppp_flush (c->ifp);
371         }
372         splx (s);
373         return (0);
374 }
375
376 /*
377  * Stop the interface.  Called on splimp().
378  */
379 static void
380 cxdown (cx_chan_t *c)
381 {
382         unsigned short port = c->chip->port;
383
384         print (("cx%d.%d: cxdown\n", c->board->num, c->num));
385
386         /* The interface is down, stop it */
387         c->ifp->if_flags &= ~IFF_OACTIVE;
388
389         /* Reset the channel (for sync modes only) */
390                 outb (CAR(port), c->num & 3);
391                 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
392
393         cx_setup_chan (c);
394 }
395
396 /*
397  * Start the interface.  Called on splimp().
398  */
399 static void
400 cxup (cx_chan_t *c)
401 {
402         unsigned short port = c->chip->port;
403
404                 /* The interface is up, start it */
405                 print (("cx%d.%d: cxup\n", c->board->num, c->num));
406
407                 /* Initialize channel, enable receiver and transmitter */
408                 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
409                 /* Repeat the command, to avoid the rev.H bug */
410                 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
411
412                 /* Start receiver */
413                 outw (ARBCNT(port), DMABUFSZ);
414                 outb (ARBSTS(port), BSTS_OWN24);
415                 outw (BRBCNT(port), DMABUFSZ);
416                 outb (BRBSTS(port), BSTS_OWN24);
417
418                 /* Raise DTR and RTS */
419                 cx_chan_dtr (c, 1);
420                 cx_chan_rts (c, 1);
421
422                 /* Enable interrupts */
423                 outb (IER(port), IER_RXD | IER_TXD);
424 }
425
426 /*
427  * Fill transmitter buffer with data.
428  */
429 static void 
430 cxput (cx_chan_t *c, char b)
431 {
432         struct mbuf *m;
433         unsigned char *buf;
434         unsigned short port = c->chip->port, len, cnt_port, sts_port;
435
436         /* Choose the buffer. */
437         if (b == 'A') {
438                 buf      = c->atbuf;
439                 cnt_port = ATBCNT(port);
440                 sts_port = ATBSTS(port);
441         } else {
442                 buf      = c->btbuf;
443                 cnt_port = BTBCNT(port);
444                 sts_port = BTBSTS(port);
445         }
446
447         /* Is it busy? */
448         if (inb (sts_port) & BSTS_OWN24) {
449                 if (c->ifp->if_flags & IFF_DEBUG)
450                         print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
451                                 c->board->num, c->num, b,
452                                 inb (sts_port), BSTS_BITS));
453                 goto ret;
454         }
455
456         /* Get the packet to send. */
457         m = sppp_dequeue (c->master);
458         if (! m)
459                 return;
460         len = m->m_pkthdr.len;
461
462         /* Count the transmitted bytes to the subchannel, not the master. */
463         c->master->if_obytes -= len + 3;
464         c->ifp->if_obytes += len + 3;
465         c->stat->obytes += len + 3;
466
467         if (len >= DMABUFSZ) {
468                 printf ("cx%d.%d: too long packet: %d bytes: ",
469                         c->board->num, c->num, len);
470                 printmbuf (m);
471                 m_freem (m);
472                 return;
473         }
474         m_copydata (m, 0, len, buf);
475         if (c->ifp->if_bpf)
476                 bpf_mtap (c->ifp, m);
477         m_freem (m);
478
479         /* Start transmitter. */
480         outw (cnt_port, len);
481         outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
482
483         if (c->ifp->if_flags & IFF_DEBUG)
484                 print (("cx%d.%d: enqueue %d bytes to %c\n",
485                         c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
486 ret:
487         c->ifp->if_flags |= IFF_OACTIVE;
488 }
489
490 /*
491  * Start output on the (slave) interface.  Get another datagram to send
492  * off of the interface queue, and copy it to the interface
493  * before starting the output.
494  */
495 static void
496 cxsend (cx_chan_t *c)
497 {
498         unsigned short port = c->chip->port;
499
500         if (c->ifp->if_flags & IFF_DEBUG)
501                 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
502
503         /* No output if the interface is down. */
504         if (! (c->ifp->if_flags & IFF_RUNNING))
505                 return;
506
507         /* Set the current channel number. */
508         outb (CAR(port), c->num & 3);
509
510         /* Determine the buffer order. */
511         if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
512                 cxput (c, 'B');
513                 cxput (c, 'A');
514         } else {
515                 cxput (c, 'A');
516                 cxput (c, 'B');
517         }
518
519         /* Set up transmit timeout. */
520         if (c->master->if_flags & IFF_OACTIVE)
521                 c->master->if_timer = TXTIMEOUT;
522
523         /*
524          * Enable TXMPTY interrupt,
525          * to catch the case when the second buffer is empty.
526          */
527         if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
528             (inb (BTBSTS(port)) & BSTS_OWN24)) {
529                 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
530         } else
531                 outb (IER(port), IER_RXD | IER_TXD);
532 }
533
534 /*
535  * Start output on the (master) interface and all slave interfaces.
536  * Always called on splimp().
537  */
538 static void
539 cxstart (struct ifnet *ifp)
540 {
541         cx_chan_t *q, *c = ifp->if_softc;
542
543         if (c->ifp->if_flags & IFF_DEBUG)
544                 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
545
546         /* Start the master subchannel. */
547         cxsend (c);
548
549         /* Start all slave subchannels. */
550         if (c->slaveq && ! sppp_isempty (c->master))
551                 for (q=c->slaveq; q; q=q->slaveq)
552                         if ((q->ifp->if_flags & IFF_RUNNING) &&
553                             ! (q->ifp->if_flags & IFF_OACTIVE))
554                                 cxsend (q);
555 }
556
557 /*
558  * Handle transmit timeouts.
559  * Recover after lost transmit interrupts.
560  * Always called on splimp().
561  */
562 static void
563 cxwatchdog (struct ifnet *ifp)
564 {
565         cx_chan_t *q, *c = ifp->if_softc;
566
567         if (! (ifp->if_flags & IFF_RUNNING))
568                 return;
569         if (ifp->if_flags & IFF_DEBUG)
570                 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
571
572         cxdown (c);
573         for (q=c->slaveq; q; q=q->slaveq)
574                 cxdown (q);
575
576         cxup (c);
577         for (q=c->slaveq; q; q=q->slaveq)
578                 cxup (q);
579
580                 cxstart (ifp);
581 }
582
583 /*
584  * Handle receive interrupts, including receive errors and
585  * receive timeout interrupt.
586  */
587 static void 
588 cxrinth (cx_chan_t *c)
589 {
590         unsigned short port = c->chip->port;
591         unsigned short len, risr = inw (RISR(port));
592
593         /* Receive errors. */
594         if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
595                 if (c->ifp->if_flags & IFF_DEBUG)
596                         printf ("cx%d.%d: receive error, risr=%b\n",
597                                 c->board->num, c->num, risr, RISH_BITS);
598                 ++c->ifp->if_ierrors;
599                 ++c->stat->ierrs;
600                 if (risr & RIS_OVERRUN)
601                         ++c->ifp->if_collisions;
602         } else if (risr & RIS_EOBUF) {
603                 if (c->ifp->if_flags & IFF_DEBUG)
604                         print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
605                                 c->board->num, c->num, risr, RISH_BITS,
606                                 inb (ARBSTS(port)), BSTS_BITS,
607                                 inb (BRBSTS(port)), BSTS_BITS));
608                 ++c->stat->ipkts;
609
610                 /* Handle received data. */
611                 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
612                 c->stat->ibytes += len;
613                 if (len > DMABUFSZ) {
614                         /* Fatal error: actual DMA transfer size
615                          * exceeds our buffer size.  It could be caused
616                          * by incorrectly programmed DMA register or
617                          * hardware fault.  Possibly, should panic here. */
618                         printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
619                                c->board->num, c->num, len);
620                         ++c->ifp->if_ierrors;
621                 } else if (! (risr & RIS_EOFR)) {
622                         /* The received frame does not fit in the DMA buffer.
623                          * It could be caused by serial lie noise,
624                          * or if the peer has too big MTU. */
625                         if (c->ifp->if_flags & IFF_DEBUG)
626                                 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
627                                         c->board->num, c->num, risr, RISH_BITS);
628                         ++c->ifp->if_ierrors;
629                 } else {
630                         /* Valid frame received. */
631                         if (c->ifp->if_flags & IFF_DEBUG)
632                                 print (("cx%d.%d: hdlc received %d bytes\n",
633                                 c->board->num, c->num, len));
634                         cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
635                         ++c->ifp->if_ipackets;
636                 }
637         } else if (c->ifp->if_flags & IFF_DEBUG) {
638                 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
639                         c->board->num, c->num, risr, RISH_BITS));
640                 ++c->stat->ierrs;
641         }
642
643         /* Restart receiver. */
644         if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
645                 outw (ARBCNT(port), DMABUFSZ);
646                 outb (ARBSTS(port), BSTS_OWN24);
647         }
648         if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
649                 outw (BRBCNT(port), DMABUFSZ);
650                 outb (BRBSTS(port), BSTS_OWN24);
651         }
652 }
653
654 /*
655  * Handle transmit interrupt.
656  */
657 static int
658 cxtinth (cx_chan_t *c)
659 {
660         unsigned short port = c->chip->port;
661         unsigned char tisr = inb (TISR(port));
662         unsigned char teoir = 0;
663
664         c->ifp->if_flags &= ~IFF_OACTIVE;
665         if (c->ifp == c->master)
666                 c->ifp->if_timer = 0;
667
668         if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
669                 /* if (c->ifp->if_flags & IFF_DEBUG) */
670                         print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
671                                 c->board->num, c->num, tisr, TIS_BITS,
672                                 inb (ATBSTS(port)), BSTS_BITS,
673                                 inb (BTBSTS(port)), BSTS_BITS));
674                 ++c->ifp->if_oerrors;
675                 ++c->stat->oerrs;
676
677                 /* Terminate the failed buffer. */
678                 /* teoir = TEOI_TERMBUFF; */
679         } else if (c->ifp->if_flags & IFF_DEBUG)
680                 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
681                         c->board->num, c->num, tisr, TIS_BITS,
682                         inb (ATBSTS(port)), BSTS_BITS,
683                         inb (BTBSTS(port)), BSTS_BITS));
684
685         if (tisr & TIS_EOFR) {
686                 ++c->ifp->if_opackets;
687                 ++c->stat->opkts;
688         }
689
690         /* Start output on the (sub-) channel. */
691         cxsend (c);
692
693         return (teoir);
694 }
695
696 static void
697 cxintr (int bnum)
698 {
699         cx_board_t *b = cxboard + bnum;
700         while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
701                 /* Acknowledge the interrupt to enter the interrupt context. */
702                 /* Read the local interrupt vector register. */
703                 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
704                 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
705                 unsigned short port = c->chip->port;
706                 unsigned short eoiport = REOIR(port);
707                 unsigned char eoi = 0;
708
709                 if (c->type == T_NONE) {
710                         printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
711                                 c->board->num, c->num, livr);
712                         continue;       /* incorrect channel number? */
713                 }
714                 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
715                         c->board->num, c->num, livr)); */
716
717                 /* Clear RTS to stop receiver data flow while we are busy
718                  * processing the interrupt, thus avoiding underruns. */
719                 if (! c->sopt.norts) {
720                         outb (MSVR_RTS(port), 0);
721                         c->rts = 0;
722                 }
723
724                 switch (livr & 3) {
725                 case LIV_EXCEP:         /* receive exception */
726                 case LIV_RXDATA:        /* receive interrupt */
727                         ++c->stat->rintr;
728                         switch (c->mode) {
729                         case M_ASYNC: eoi = cxrinta (c); break;
730                         case M_HDLC:  cxrinth (c);       break;
731                         default:;       /* No bisync and X.21 yet */
732                         }
733                         break;
734                 case LIV_TXDATA:        /* transmit interrupt */
735                         ++c->stat->tintr;
736                         eoiport = TEOIR(port);
737                         switch (c->mode) {
738                         case M_ASYNC: cxtinta (c);       break;
739                         case M_HDLC:  eoi = cxtinth (c); break;
740                         default:;       /* No bisync and X.21 yet */
741                         }
742                         break;
743                 case LIV_MODEM:         /* modem/timer interrupt */
744                         ++c->stat->mintr;
745                         eoiport = MEOIR(port);
746                         cxmint (c);
747                         break;
748                 }
749
750                 /* Raise RTS for this channel if and only if
751                  * both receive buffers are empty. */
752                 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
753                     (inb (ARBSTS(port)) & BSTS_OWN24) &&
754                     (inb (BRBSTS(port)) & BSTS_OWN24)) {
755                         outb (MSVR_RTS(port), MSV_RTS);
756                         c->rts = 1;
757                 }
758
759                 /* Exit from interrupt context. */
760                 outb (eoiport, eoi);
761
762                 /* Master channel - start output on all idle subchannels. */
763                 if (c->master == c->ifp && c->slaveq &&
764                     (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
765                     ! sppp_isempty (c->ifp)) {
766                         cx_chan_t *q;
767
768                         for (q=c->slaveq; q; q=q->slaveq)
769                                 if ((q->ifp->if_flags & IFF_RUNNING) &&
770                                     ! (q->ifp->if_flags & IFF_OACTIVE))
771                                         cxsend (q);
772                 }
773         }
774 }
775
776 /*
777  * Process the received packet.
778  */
779 static void 
780 cxinput (cx_chan_t *c, void *buf, unsigned len)
781 {
782         /* Make an mbuf. */
783         struct mbuf *m = makembuf (buf, len);
784         if (! m) {
785                 if (c->ifp->if_flags & IFF_DEBUG)
786                         printf ("cx%d.%d: no memory for packet\n",
787                                 c->board->num, c->num);
788                 ++c->ifp->if_iqdrops;
789                 return;
790         }
791         m->m_pkthdr.rcvif = c->master;
792 #ifdef DEBUG
793         if (c->ifp->if_flags & IFF_DEBUG)
794         printmbuf (m);
795 #endif
796
797         /*
798          * Check if there's a BPF listener on this interface.
799          * If so, hand off the raw packet to bpf.
800          */
801         if (c->ifp->if_bpf)
802                 bpf_tap (c->ifp, buf, len);
803
804         /* Count the received bytes to the subchannel, not the master. */
805         c->master->if_ibytes -= len + 3;
806         c->ifp->if_ibytes += len + 3;
807
808         sppp_input (c->master, m);
809 }
810
811 void cxswitch (cx_chan_t *c, cx_soft_opt_t new)
812 {
813         new.ext = 0;
814         if (! new.ext) {
815                 struct sppp *sp = (struct sppp*) c->ifp;
816
817 #if 0 /* Doesn't work this way any more 990402 /phk */
818                 if (new.cisco)
819                         sp->pp_flags |= PP_CISCO;
820                 else
821                         sp->pp_flags &= ~PP_CISCO;
822 #endif
823                 if (new.keepalive)
824                         sp->pp_flags |= PP_KEEPALIVE;
825                 else
826                         sp->pp_flags &= ~PP_KEEPALIVE;
827         }
828         c->sopt = new;
829 }