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