Merge from vendor branch HEIMDAL:
[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.15 2005/01/23 20:21:30 joerg 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         BPF_MTAP(c->ifp, m);
479         m_freem (m);
480
481         /* Start transmitter. */
482         outw (cnt_port, len);
483         outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
484
485         if (c->ifp->if_flags & IFF_DEBUG)
486                 print (("cx%d.%d: enqueue %d bytes to %c\n",
487                         c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
488 ret:
489         c->ifp->if_flags |= IFF_OACTIVE;
490 }
491
492 /*
493  * Start output on the (slave) interface.  Get another datagram to send
494  * off of the interface queue, and copy it to the interface
495  * before starting the output.
496  */
497 static void
498 cxsend (cx_chan_t *c)
499 {
500         unsigned short port = c->chip->port;
501
502         if (c->ifp->if_flags & IFF_DEBUG)
503                 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
504
505         /* No output if the interface is down. */
506         if (! (c->ifp->if_flags & IFF_RUNNING))
507                 return;
508
509         /* Set the current channel number. */
510         outb (CAR(port), c->num & 3);
511
512         /* Determine the buffer order. */
513         if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
514                 cxput (c, 'B');
515                 cxput (c, 'A');
516         } else {
517                 cxput (c, 'A');
518                 cxput (c, 'B');
519         }
520
521         /* Set up transmit timeout. */
522         if (c->master->if_flags & IFF_OACTIVE)
523                 c->master->if_timer = TXTIMEOUT;
524
525         /*
526          * Enable TXMPTY interrupt,
527          * to catch the case when the second buffer is empty.
528          */
529         if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
530             (inb (BTBSTS(port)) & BSTS_OWN24)) {
531                 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
532         } else
533                 outb (IER(port), IER_RXD | IER_TXD);
534 }
535
536 /*
537  * Start output on the (master) interface and all slave interfaces.
538  * Always called on splimp().
539  */
540 static void
541 cxstart (struct ifnet *ifp)
542 {
543         cx_chan_t *q, *c = ifp->if_softc;
544
545         if (c->ifp->if_flags & IFF_DEBUG)
546                 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
547
548         /* Start the master subchannel. */
549         cxsend (c);
550
551         /* Start all slave subchannels. */
552         if (c->slaveq && ! sppp_isempty (c->master))
553                 for (q=c->slaveq; q; q=q->slaveq)
554                         if ((q->ifp->if_flags & IFF_RUNNING) &&
555                             ! (q->ifp->if_flags & IFF_OACTIVE))
556                                 cxsend (q);
557 }
558
559 /*
560  * Handle transmit timeouts.
561  * Recover after lost transmit interrupts.
562  * Always called on splimp().
563  */
564 static void
565 cxwatchdog (struct ifnet *ifp)
566 {
567         cx_chan_t *q, *c = ifp->if_softc;
568
569         if (! (ifp->if_flags & IFF_RUNNING))
570                 return;
571         if (ifp->if_flags & IFF_DEBUG)
572                 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
573
574         cxdown (c);
575         for (q=c->slaveq; q; q=q->slaveq)
576                 cxdown (q);
577
578         cxup (c);
579         for (q=c->slaveq; q; q=q->slaveq)
580                 cxup (q);
581
582                 cxstart (ifp);
583 }
584
585 /*
586  * Handle receive interrupts, including receive errors and
587  * receive timeout interrupt.
588  */
589 static void 
590 cxrinth (cx_chan_t *c)
591 {
592         unsigned short port = c->chip->port;
593         unsigned short len, risr = inw (RISR(port));
594
595         /* Receive errors. */
596         if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
597                 if (c->ifp->if_flags & IFF_DEBUG)
598                         printf ("cx%d.%d: receive error, risr=%b\n",
599                                 c->board->num, c->num, risr, RISH_BITS);
600                 ++c->ifp->if_ierrors;
601                 ++c->stat->ierrs;
602                 if (risr & RIS_OVERRUN)
603                         ++c->ifp->if_collisions;
604         } else if (risr & RIS_EOBUF) {
605                 if (c->ifp->if_flags & IFF_DEBUG)
606                         print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
607                                 c->board->num, c->num, risr, RISH_BITS,
608                                 inb (ARBSTS(port)), BSTS_BITS,
609                                 inb (BRBSTS(port)), BSTS_BITS));
610                 ++c->stat->ipkts;
611
612                 /* Handle received data. */
613                 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
614                 c->stat->ibytes += len;
615                 if (len > DMABUFSZ) {
616                         /* Fatal error: actual DMA transfer size
617                          * exceeds our buffer size.  It could be caused
618                          * by incorrectly programmed DMA register or
619                          * hardware fault.  Possibly, should panic here. */
620                         printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
621                                c->board->num, c->num, len);
622                         ++c->ifp->if_ierrors;
623                 } else if (! (risr & RIS_EOFR)) {
624                         /* The received frame does not fit in the DMA buffer.
625                          * It could be caused by serial lie noise,
626                          * or if the peer has too big MTU. */
627                         if (c->ifp->if_flags & IFF_DEBUG)
628                                 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
629                                         c->board->num, c->num, risr, RISH_BITS);
630                         ++c->ifp->if_ierrors;
631                 } else {
632                         /* Valid frame received. */
633                         if (c->ifp->if_flags & IFF_DEBUG)
634                                 print (("cx%d.%d: hdlc received %d bytes\n",
635                                 c->board->num, c->num, len));
636                         cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
637                         ++c->ifp->if_ipackets;
638                 }
639         } else if (c->ifp->if_flags & IFF_DEBUG) {
640                 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
641                         c->board->num, c->num, risr, RISH_BITS));
642                 ++c->stat->ierrs;
643         }
644
645         /* Restart receiver. */
646         if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
647                 outw (ARBCNT(port), DMABUFSZ);
648                 outb (ARBSTS(port), BSTS_OWN24);
649         }
650         if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
651                 outw (BRBCNT(port), DMABUFSZ);
652                 outb (BRBSTS(port), BSTS_OWN24);
653         }
654 }
655
656 /*
657  * Handle transmit interrupt.
658  */
659 static int
660 cxtinth (cx_chan_t *c)
661 {
662         unsigned short port = c->chip->port;
663         unsigned char tisr = inb (TISR(port));
664         unsigned char teoir = 0;
665
666         c->ifp->if_flags &= ~IFF_OACTIVE;
667         if (c->ifp == c->master)
668                 c->ifp->if_timer = 0;
669
670         if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
671                 /* if (c->ifp->if_flags & IFF_DEBUG) */
672                         print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
673                                 c->board->num, c->num, tisr, TIS_BITS,
674                                 inb (ATBSTS(port)), BSTS_BITS,
675                                 inb (BTBSTS(port)), BSTS_BITS));
676                 ++c->ifp->if_oerrors;
677                 ++c->stat->oerrs;
678
679                 /* Terminate the failed buffer. */
680                 /* teoir = TEOI_TERMBUFF; */
681         } else if (c->ifp->if_flags & IFF_DEBUG)
682                 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
683                         c->board->num, c->num, tisr, TIS_BITS,
684                         inb (ATBSTS(port)), BSTS_BITS,
685                         inb (BTBSTS(port)), BSTS_BITS));
686
687         if (tisr & TIS_EOFR) {
688                 ++c->ifp->if_opackets;
689                 ++c->stat->opkts;
690         }
691
692         /* Start output on the (sub-) channel. */
693         cxsend (c);
694
695         return (teoir);
696 }
697
698 static void
699 cxintr (int bnum)
700 {
701         cx_board_t *b = cxboard + bnum;
702         while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
703                 /* Acknowledge the interrupt to enter the interrupt context. */
704                 /* Read the local interrupt vector register. */
705                 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
706                 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
707                 unsigned short port = c->chip->port;
708                 unsigned short eoiport = REOIR(port);
709                 unsigned char eoi = 0;
710
711                 if (c->type == T_NONE) {
712                         printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
713                                 c->board->num, c->num, livr);
714                         continue;       /* incorrect channel number? */
715                 }
716                 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
717                         c->board->num, c->num, livr)); */
718
719                 /* Clear RTS to stop receiver data flow while we are busy
720                  * processing the interrupt, thus avoiding underruns. */
721                 if (! c->sopt.norts) {
722                         outb (MSVR_RTS(port), 0);
723                         c->rts = 0;
724                 }
725
726                 switch (livr & 3) {
727                 case LIV_EXCEP:         /* receive exception */
728                 case LIV_RXDATA:        /* receive interrupt */
729                         ++c->stat->rintr;
730                         switch (c->mode) {
731                         case M_ASYNC: eoi = cxrinta (c); break;
732                         case M_HDLC:  cxrinth (c);       break;
733                         default:;       /* No bisync and X.21 yet */
734                         }
735                         break;
736                 case LIV_TXDATA:        /* transmit interrupt */
737                         ++c->stat->tintr;
738                         eoiport = TEOIR(port);
739                         switch (c->mode) {
740                         case M_ASYNC: cxtinta (c);       break;
741                         case M_HDLC:  eoi = cxtinth (c); break;
742                         default:;       /* No bisync and X.21 yet */
743                         }
744                         break;
745                 case LIV_MODEM:         /* modem/timer interrupt */
746                         ++c->stat->mintr;
747                         eoiport = MEOIR(port);
748                         cxmint (c);
749                         break;
750                 }
751
752                 /* Raise RTS for this channel if and only if
753                  * both receive buffers are empty. */
754                 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
755                     (inb (ARBSTS(port)) & BSTS_OWN24) &&
756                     (inb (BRBSTS(port)) & BSTS_OWN24)) {
757                         outb (MSVR_RTS(port), MSV_RTS);
758                         c->rts = 1;
759                 }
760
761                 /* Exit from interrupt context. */
762                 outb (eoiport, eoi);
763
764                 /* Master channel - start output on all idle subchannels. */
765                 if (c->master == c->ifp && c->slaveq &&
766                     (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
767                     ! sppp_isempty (c->ifp)) {
768                         cx_chan_t *q;
769
770                         for (q=c->slaveq; q; q=q->slaveq)
771                                 if ((q->ifp->if_flags & IFF_RUNNING) &&
772                                     ! (q->ifp->if_flags & IFF_OACTIVE))
773                                         cxsend (q);
774                 }
775         }
776 }
777
778 /*
779  * Process the received packet.
780  */
781 static void 
782 cxinput (cx_chan_t *c, void *buf, unsigned len)
783 {
784         /* Make an mbuf. */
785         struct mbuf *m = makembuf (buf, len);
786         if (! m) {
787                 if (c->ifp->if_flags & IFF_DEBUG)
788                         printf ("cx%d.%d: no memory for packet\n",
789                                 c->board->num, c->num);
790                 ++c->ifp->if_iqdrops;
791                 return;
792         }
793         m->m_pkthdr.rcvif = c->master;
794 #ifdef DEBUG
795         if (c->ifp->if_flags & IFF_DEBUG)
796         printmbuf (m);
797 #endif
798
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 }