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