MASSIVE reorganization of the device operations vector. Change cdevsw
[games.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.20 2006/07/28 02:17:37 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/i386/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)     printf 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 valid (unsigned short value, unsigned short *list)
115 {
116         while (*list)
117                 if (value == *list++)
118                         return (1);
119         return (0);
120 }
121
122 /*
123  * Print the mbuf chain, for debug purposes only.
124  */
125 static void printmbuf (struct mbuf *m)
126 {
127         printf ("mbuf:");
128         for (; m; m=m->m_next) {
129                 if (m->m_flags & M_PKTHDR)
130                         printf (" HDR %d:", m->m_pkthdr.len);
131                 if (m->m_flags & M_EXT)
132                         printf (" EXT:");
133                 printf (" %d", m->m_len);
134         }
135         printf ("\n");
136 }
137
138 /*
139  * Make an mbuf from data.
140  */
141 static struct mbuf *makembuf (void *buf, unsigned len)
142 {
143         struct mbuf *m, *o, *p;
144
145         MGETHDR (m, MB_DONTWAIT, MT_DATA);
146         if (! m)
147                 return (0);
148         if (len >= MINCLSIZE)
149                 MCLGET (m, MB_DONTWAIT);
150         m->m_pkthdr.len = len;
151         m->m_len = 0;
152
153         p = m;
154         while (len) {
155                 unsigned n = M_TRAILINGSPACE (p);
156                 if (n > len)
157                         n = len;
158
159                 if (! n) {
160                         /* Allocate new mbuf. */
161                         o = p;
162                         MGET (p, MB_DONTWAIT, MT_DATA);
163                         if (! p) {
164                                 m_freem (m);
165                                 return (0);
166                         }
167                         if (len >= MINCLSIZE)
168                                 MCLGET (p, MB_DONTWAIT);
169                         p->m_len = 0;
170                         o->m_next = p;
171
172                         n = M_TRAILINGSPACE (p);
173                         if (n > len)
174                                 n = len;
175                 }
176
177                 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
178
179                 p->m_len += n;
180                 buf = (char *)buf + n;
181                 len -= n;
182         }
183         return (m);
184 }
185
186 /*
187  * Test the presence of the adapter on the given i/o port.
188  */
189 static int
190 cxprobe (struct isa_device *id)
191 {
192         int unit = id->id_unit;
193         int iobase = id->id_iobase;
194         int irq = id->id_irq;
195         int drq = id->id_drq;
196         int irqnum;
197         irqnum = ffs (irq) - 1;
198
199         print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",
200                 unit, iobase, irqnum, drq));
201         if (! valid (irqnum, irq_valid_values)) {
202                 printf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);
203                 return (0);
204         }
205         if (! valid (iobase, port_valid_values)) {
206                 printf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);
207                 return (0);
208         }
209         if (! valid (drq, drq_valid_values)) {
210                 printf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);
211                 return (0);
212         }
213         if (! cx_probe_board (iobase))
214                 return (0);
215
216         return (1);
217 }
218
219 /*
220  * The adapter is present, initialize the driver structures.
221  */
222
223 static int
224 cxattach (struct isa_device *id)
225 {
226         int unit = id->id_unit;
227         int iobase = id->id_iobase;
228         int irq = id->id_irq;
229         int drq = id->id_drq;
230         cx_board_t *b = cxboard + unit;
231         int i;
232         struct sppp *sp;
233
234         id->id_intr = (inthand2_t *)cxintr;
235
236         /* Initialize the board structure. */
237         cx_init (b, unit, iobase, ffs(irq)-1, drq);
238
239         for (i=0; i<NCHAN; ++i) {
240                 cx_chan_t *c = b->chan + i;
241                 int u = b->num*NCHAN + i;
242                 cxchan[u] = c;
243
244                 if (c->type == T_NONE)
245                         continue;
246
247                 /* Allocate the buffer memory. */
248                 c->arbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
249                 c->brbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
250                 c->atbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
251                 c->btbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
252
253 #if 0
254                 /* All buffers should be located in lower 16M of memory! */
255                 /* XXX all buffers located where?  I don't think so! */
256                 if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {
257                         printf ("cx%d.%d: No memory for channel buffers\n",
258                                 c->board->num, c->num);
259                         c->type = T_NONE;
260                 }
261 #endif
262
263                 switch (c->type) {
264                 case T_SYNC_RS232:
265                 case T_SYNC_V35:
266                 case T_SYNC_RS449:
267                 case T_UNIV_RS232:
268                 case T_UNIV_RS449:
269                 case T_UNIV_V35:
270                         c->ifp = malloc (IFSTRUCTSZ, M_DEVBUF, M_WAITOK | M_ZERO);
271                         c->master = c->ifp;
272                         c->ifp->if_softc = c;
273                         if_initname(c->ifp, "cx", u);
274                         c->ifp->if_mtu = PP_MTU;
275                         c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
276                         c->ifp->if_ioctl = cxsioctl;
277                         c->ifp->if_start = (start_func_t) cxstart;
278                         c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;
279                         /* Init routine is never called by upper level? */
280                         sppp_attach (c->ifp);
281                         if_attach (c->ifp, NULL);
282                         sp = (struct sppp*) c->ifp;
283                         /* If BPF is in the kernel, call the attach for it. */
284                         bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);
285                 }
286         }
287
288         /* Reset the adapter. */
289         cx_setup_board (b);
290
291         /* Activate the timeout routine. */
292         if (unit == 0) {
293                 callout_init(&cxtimeout_ch);
294                 callout_reset(&cxtimeout_ch, hz * 5, cxtimeout, NULL);
295         }
296         printf ("cx%d: <Cronyx-%s>\n", unit, b->name);
297         dev_ops_add(&cx_ops, -1, unit);
298         make_dev(&cx_ops, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
299         return (1);
300 }
301
302 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
303
304 /*
305  * Process an ioctl request.
306  */
307 static int
308 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
309 {
310         cx_chan_t *q, *c = ifp->if_softc;
311         int error, was_up, should_be_up;
312
313         /*
314          * No socket ioctls while the channel is in async mode.
315          */
316         if (c->type==T_NONE || c->mode==M_ASYNC)
317                 return (EINVAL);
318
319         /*
320          * Socket ioctls on slave subchannels are not allowed.
321          */
322         if (c->master != c->ifp)
323                 return (EBUSY);
324
325         was_up = (ifp->if_flags & IFF_RUNNING) != 0;
326         error = sppp_ioctl (ifp, cmd, data);
327         if (error)
328                 return (error);
329
330         print (("cxioctl (%d.%d, ", c->board->num, c->num));
331         switch (cmd) {
332         default:
333                 print (("0x%x)\n", cmd));
334                 return (0);
335         case SIOCADDMULTI:
336                 print (("SIOCADDMULTI)\n"));
337                 return (0);
338         case SIOCDELMULTI:
339                 print (("SIOCDELMULTI)\n"));
340                 return (0);
341         case SIOCSIFFLAGS:
342                 print (("SIOCSIFFLAGS)\n"));
343                 break;
344         case SIOCSIFADDR:
345                 print (("SIOCSIFADDR)\n"));
346                 break;
347         }
348
349         /* We get here only in case of SIFFLAGS or SIFADDR. */
350         crit_enter();
351
352         should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
353         if (!was_up && should_be_up) {
354                 /* Interface goes up -- start it. */
355                 cxup (c);
356
357                 /* Start all slave subchannels. */
358                 for (q=c->slaveq; q; q=q->slaveq)
359                         cxup (q);
360
361                 cxstart (c->ifp);
362         } else if (was_up && !should_be_up) {
363                 /* Interface is going down -- stop it. */
364                 cxdown (c);
365
366                 /* Stop all slave subchannels. */
367                 for (q=c->slaveq; q; q=q->slaveq)
368                         cxdown (q);
369
370                 /* Flush the interface output queue */
371                 if (! c->sopt.ext)
372                         sppp_flush (c->ifp);
373         }
374
375         crit_exit();
376
377         return (0);
378 }
379
380 /*
381  * Stop the interface.
382  */
383 static void
384 cxdown (cx_chan_t *c)
385 {
386         unsigned short port = c->chip->port;
387
388         print (("cx%d.%d: cxdown\n", c->board->num, c->num));
389
390         /* The interface is down, stop it */
391         c->ifp->if_flags &= ~IFF_OACTIVE;
392
393         /* Reset the channel (for sync modes only) */
394                 outb (CAR(port), c->num & 3);
395                 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
396
397         cx_setup_chan (c);
398 }
399
400 /*
401  * Start the interface.
402  */
403 static void
404 cxup (cx_chan_t *c)
405 {
406         unsigned short port = c->chip->port;
407
408                 /* The interface is up, start it */
409                 print (("cx%d.%d: cxup\n", c->board->num, c->num));
410
411                 /* Initialize channel, enable receiver and transmitter */
412                 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
413                 /* Repeat the command, to avoid the rev.H bug */
414                 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
415
416                 /* Start receiver */
417                 outw (ARBCNT(port), DMABUFSZ);
418                 outb (ARBSTS(port), BSTS_OWN24);
419                 outw (BRBCNT(port), DMABUFSZ);
420                 outb (BRBSTS(port), BSTS_OWN24);
421
422                 /* Raise DTR and RTS */
423                 cx_chan_dtr (c, 1);
424                 cx_chan_rts (c, 1);
425
426                 /* Enable interrupts */
427                 outb (IER(port), IER_RXD | IER_TXD);
428 }
429
430 /*
431  * Fill transmitter buffer with data.
432  */
433 static void 
434 cxput (cx_chan_t *c, char b)
435 {
436         struct mbuf *m;
437         unsigned char *buf;
438         unsigned short port = c->chip->port, len, cnt_port, sts_port;
439
440         /* Choose the buffer. */
441         if (b == 'A') {
442                 buf      = c->atbuf;
443                 cnt_port = ATBCNT(port);
444                 sts_port = ATBSTS(port);
445         } else {
446                 buf      = c->btbuf;
447                 cnt_port = BTBCNT(port);
448                 sts_port = BTBSTS(port);
449         }
450
451         /* Is it busy? */
452         if (inb (sts_port) & BSTS_OWN24) {
453                 if (c->ifp->if_flags & IFF_DEBUG)
454                         print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
455                                 c->board->num, c->num, b,
456                                 inb (sts_port), BSTS_BITS));
457                 goto ret;
458         }
459
460         /* Get the packet to send. */
461         m = sppp_dequeue (c->master);
462         if (! m)
463                 return;
464         len = m->m_pkthdr.len;
465
466         /* Count the transmitted bytes to the subchannel, not the master. */
467         c->master->if_obytes -= len + 3;
468         c->ifp->if_obytes += len + 3;
469         c->stat->obytes += len + 3;
470
471         if (len >= DMABUFSZ) {
472                 printf ("cx%d.%d: too long packet: %d bytes: ",
473                         c->board->num, c->num, len);
474                 printmbuf (m);
475                 m_freem (m);
476                 return;
477         }
478         m_copydata (m, 0, len, buf);
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  */
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  */
563 static void
564 cxwatchdog (struct ifnet *ifp)
565 {
566         cx_chan_t *q, *c = ifp->if_softc;
567
568         if (! (ifp->if_flags & IFF_RUNNING))
569                 return;
570         if (ifp->if_flags & IFF_DEBUG)
571                 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
572
573         cxdown (c);
574         for (q=c->slaveq; q; q=q->slaveq)
575                 cxdown (q);
576
577         cxup (c);
578         for (q=c->slaveq; q; q=q->slaveq)
579                 cxup (q);
580
581                 cxstart (ifp);
582 }
583
584 /*
585  * Handle receive interrupts, including receive errors and
586  * receive timeout interrupt.
587  */
588 static void 
589 cxrinth (cx_chan_t *c)
590 {
591         unsigned short port = c->chip->port;
592         unsigned short len, risr = inw (RISR(port));
593
594         /* Receive errors. */
595         if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
596                 if (c->ifp->if_flags & IFF_DEBUG)
597                         printf ("cx%d.%d: receive error, risr=%b\n",
598                                 c->board->num, c->num, risr, RISH_BITS);
599                 ++c->ifp->if_ierrors;
600                 ++c->stat->ierrs;
601                 if (risr & RIS_OVERRUN)
602                         ++c->ifp->if_collisions;
603         } else if (risr & RIS_EOBUF) {
604                 if (c->ifp->if_flags & IFF_DEBUG)
605                         print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
606                                 c->board->num, c->num, risr, RISH_BITS,
607                                 inb (ARBSTS(port)), BSTS_BITS,
608                                 inb (BRBSTS(port)), BSTS_BITS));
609                 ++c->stat->ipkts;
610
611                 /* Handle received data. */
612                 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
613                 c->stat->ibytes += len;
614                 if (len > DMABUFSZ) {
615                         /* Fatal error: actual DMA transfer size
616                          * exceeds our buffer size.  It could be caused
617                          * by incorrectly programmed DMA register or
618                          * hardware fault.  Possibly, should panic here. */
619                         printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
620                                c->board->num, c->num, len);
621                         ++c->ifp->if_ierrors;
622                 } else if (! (risr & RIS_EOFR)) {
623                         /* The received frame does not fit in the DMA buffer.
624                          * It could be caused by serial lie noise,
625                          * or if the peer has too big MTU. */
626                         if (c->ifp->if_flags & IFF_DEBUG)
627                                 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
628                                         c->board->num, c->num, risr, RISH_BITS);
629                         ++c->ifp->if_ierrors;
630                 } else {
631                         /* Valid frame received. */
632                         if (c->ifp->if_flags & IFF_DEBUG)
633                                 print (("cx%d.%d: hdlc received %d bytes\n",
634                                 c->board->num, c->num, len));
635                         cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
636                         ++c->ifp->if_ipackets;
637                 }
638         } else if (c->ifp->if_flags & IFF_DEBUG) {
639                 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
640                         c->board->num, c->num, risr, RISH_BITS));
641                 ++c->stat->ierrs;
642         }
643
644         /* Restart receiver. */
645         if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
646                 outw (ARBCNT(port), DMABUFSZ);
647                 outb (ARBSTS(port), BSTS_OWN24);
648         }
649         if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
650                 outw (BRBCNT(port), DMABUFSZ);
651                 outb (BRBSTS(port), BSTS_OWN24);
652         }
653 }
654
655 /*
656  * Handle transmit interrupt.
657  */
658 static int
659 cxtinth (cx_chan_t *c)
660 {
661         unsigned short port = c->chip->port;
662         unsigned char tisr = inb (TISR(port));
663         unsigned char teoir = 0;
664
665         c->ifp->if_flags &= ~IFF_OACTIVE;
666         if (c->ifp == c->master)
667                 c->ifp->if_timer = 0;
668
669         if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
670                 /* if (c->ifp->if_flags & IFF_DEBUG) */
671                         print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
672                                 c->board->num, c->num, tisr, TIS_BITS,
673                                 inb (ATBSTS(port)), BSTS_BITS,
674                                 inb (BTBSTS(port)), BSTS_BITS));
675                 ++c->ifp->if_oerrors;
676                 ++c->stat->oerrs;
677
678                 /* Terminate the failed buffer. */
679                 /* teoir = TEOI_TERMBUFF; */
680         } else if (c->ifp->if_flags & IFF_DEBUG)
681                 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
682                         c->board->num, c->num, tisr, TIS_BITS,
683                         inb (ATBSTS(port)), BSTS_BITS,
684                         inb (BTBSTS(port)), BSTS_BITS));
685
686         if (tisr & TIS_EOFR) {
687                 ++c->ifp->if_opackets;
688                 ++c->stat->opkts;
689         }
690
691         /* Start output on the (sub-) channel. */
692         cxsend (c);
693
694         return (teoir);
695 }
696
697 static void
698 cxintr (void *arg)
699 {
700         int bnum = (int)arg;
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 }