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