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