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