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.
7 * Copyright (C) 1994 Cronyx Ltd.
8 * Author: Serge Vakulenko, <vak@zebub.msk.su>
10 * This software is distributed with NO WARRANTIES, not even the implied
11 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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.
17 * Version 1.9, Wed Oct 4 18:58:15 MSK 1995
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.4 2003/08/07 21:54:29 dillon Exp $
28 #error The device 'cx' requires sppp.
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <sys/sockio.h>
37 #include <sys/socket.h>
44 #include <bus/isa/i386/isa_device.h>
45 #define watchdog_func_t void(*)(struct ifnet *)
46 #define start_func_t void(*)(struct ifnet*)
48 #include <net/sppp/if_sppp.h>
49 #include <machine/cronyx.h>
53 void cxswitch (cx_chan_t *c, cx_soft_opt_t new);
55 static int cxprobe __P((struct isa_device *id));
56 static int cxattach __P((struct isa_device *id));
57 static void cxput __P((cx_chan_t *c, char b));
58 static void cxsend __P((cx_chan_t *c));
59 static void cxrinth __P((cx_chan_t *c));
60 static ointhand2_t cxintr;
61 static int cxtinth __P((cx_chan_t *c));
64 # define print(s) printf s
66 # define print(s) {/*void*/}
69 #define TXTIMEOUT 10 /* transmit timeout in seconds */
70 #define DMABUFSZ (6*256) /* buffer size */
71 #define PPP_HEADER_LEN 4 /* size of PPP header */
74 * Under BSDI it's possible to use general p2p protocol scheme,
75 * as well as our own one. Switching is done via IFF_ALTPHYS flag.
76 * Our ifnet pointer holds the buffer large enough to contain
77 * any of sppp and p2p structures.
79 #define IFSTRUCTSZ (sizeof (struct sppp))
80 #define IFNETSZ (sizeof (struct ifnet))
82 static int cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
83 static void cxstart (struct ifnet *ifp);
84 static void cxwatchdog (struct ifnet *ifp);
85 static void cxinput (cx_chan_t *c, void *buf, unsigned len);
86 extern int cxrinta (cx_chan_t *c);
87 extern void cxtinta (cx_chan_t *c);
88 extern void cxmint (cx_chan_t *c);
89 extern timeout_t cxtimeout;
90 static void cxdown (cx_chan_t *c);
91 static void cxup (cx_chan_t *c);
93 cx_board_t cxboard [NCX]; /* adapter state structures */
94 cx_chan_t *cxchan [NCX*NCHAN]; /* unit to channel struct pointer */
96 extern struct cdevsw cx_cdevsw;
98 static unsigned short irq_valid_values [] = { 3, 5, 7, 10, 11, 12, 15, 0 };
99 static unsigned short drq_valid_values [] = { 5, 6, 7, 0 };
100 static unsigned short port_valid_values [] = {
101 0x240, 0x260, 0x280, 0x300, 0x320, 0x380, 0x3a0, 0,
105 * Check that the value is contained in the list of correct values.
107 static int valid (unsigned short value, unsigned short *list)
110 if (value == *list++)
116 * Print the mbuf chain, for debug purposes only.
118 static void printmbuf (struct mbuf *m)
121 for (; m; m=m->m_next) {
122 if (m->m_flags & M_PKTHDR)
123 printf (" HDR %d:", m->m_pkthdr.len);
124 if (m->m_flags & M_EXT)
126 printf (" %d", m->m_len);
132 * Make an mbuf from data.
134 static struct mbuf *makembuf (void *buf, unsigned len)
136 struct mbuf *m, *o, *p;
138 MGETHDR (m, M_DONTWAIT, MT_DATA);
141 if (len >= MINCLSIZE)
142 MCLGET (m, M_DONTWAIT);
143 m->m_pkthdr.len = len;
148 unsigned n = M_TRAILINGSPACE (p);
153 /* Allocate new mbuf. */
155 MGET (p, M_DONTWAIT, MT_DATA);
160 if (len >= MINCLSIZE)
161 MCLGET (p, M_DONTWAIT);
165 n = M_TRAILINGSPACE (p);
170 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
173 buf = (char *)buf + n;
180 * Test the presence of the adapter on the given i/o port.
183 cxprobe (struct isa_device *id)
185 int unit = id->id_unit;
186 int iobase = id->id_iobase;
187 int irq = id->id_irq;
188 int drq = id->id_drq;
190 irqnum = ffs (irq) - 1;
192 print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",
193 unit, iobase, irqnum, drq));
194 if (! valid (irqnum, irq_valid_values)) {
195 printf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);
198 if (! valid (iobase, port_valid_values)) {
199 printf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);
202 if (! valid (drq, drq_valid_values)) {
203 printf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);
206 if (! cx_probe_board (iobase))
213 * The adapter is present, initialize the driver structures.
217 cxattach (struct isa_device *id)
219 int unit = id->id_unit;
220 int iobase = id->id_iobase;
221 int irq = id->id_irq;
222 int drq = id->id_drq;
223 cx_board_t *b = cxboard + unit;
227 id->id_ointr = cxintr;
229 /* Initialize the board structure. */
230 cx_init (b, unit, iobase, ffs(irq)-1, drq);
232 for (i=0; i<NCHAN; ++i) {
233 cx_chan_t *c = b->chan + i;
234 int u = b->num*NCHAN + i;
237 if (c->type == T_NONE)
240 /* Allocate the buffer memory. */
241 c->arbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
242 c->brbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
243 c->atbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
244 c->btbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
246 /* All buffers should be located in lower 16M of memory! */
247 if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {
248 printf ("cx%d.%d: No memory for channel buffers\n",
249 c->board->num, c->num);
260 c->ifp = malloc (IFSTRUCTSZ, M_DEVBUF, M_NOWAIT);
262 printf ("cx%d.%d: No memory for ifnet buffer\n",
263 c->board->num, c->num);
267 bzero (c->ifp, IFSTRUCTSZ);
269 c->ifp->if_softc = c;
271 c->ifp->if_name = "cx";
272 c->ifp->if_mtu = PP_MTU;
273 c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
274 c->ifp->if_ioctl = cxsioctl;
275 c->ifp->if_start = (start_func_t) cxstart;
276 c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;
277 /* Init routine is never called by upper level? */
278 sppp_attach (c->ifp);
280 sp = (struct sppp*) c->ifp;
281 /* If BPF is in the kernel, call the attach for it. */
282 bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);
286 /* Reset the adapter. */
289 /* Activate the timeout routine. */
291 timeout (cxtimeout, 0, hz*5);
293 printf ("cx%d: <Cronyx-%s>\n", unit, b->name);
294 make_dev(&cx_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
298 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
301 * Process an ioctl request.
304 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
306 cx_chan_t *q, *c = ifp->if_softc;
307 int error, s, was_up, should_be_up;
310 * No socket ioctls while the channel is in async mode.
312 if (c->type==T_NONE || c->mode==M_ASYNC)
316 * Socket ioctls on slave subchannels are not allowed.
318 if (c->master != c->ifp)
321 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
322 error = sppp_ioctl (ifp, cmd, data);
326 print (("cxioctl (%d.%d, ", c->board->num, c->num));
329 print (("0x%x)\n", cmd));
332 print (("SIOCADDMULTI)\n"));
335 print (("SIOCDELMULTI)\n"));
338 print (("SIOCSIFFLAGS)\n"));
341 print (("SIOCSIFADDR)\n"));
345 /* We get here only in case of SIFFLAGS or SIFADDR. */
347 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
348 if (!was_up && should_be_up) {
349 /* Interface goes up -- start it. */
352 /* Start all slave subchannels. */
353 for (q=c->slaveq; q; q=q->slaveq)
357 } else if (was_up && !should_be_up) {
358 /* Interface is going down -- stop it. */
361 /* Stop all slave subchannels. */
362 for (q=c->slaveq; q; q=q->slaveq)
365 /* Flush the interface output queue */
374 * Stop the interface. Called on splimp().
377 cxdown (cx_chan_t *c)
379 unsigned short port = c->chip->port;
381 print (("cx%d.%d: cxdown\n", c->board->num, c->num));
383 /* The interface is down, stop it */
384 c->ifp->if_flags &= ~IFF_OACTIVE;
386 /* Reset the channel (for sync modes only) */
387 outb (CAR(port), c->num & 3);
388 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
394 * Start the interface. Called on splimp().
399 unsigned short port = c->chip->port;
401 /* The interface is up, start it */
402 print (("cx%d.%d: cxup\n", c->board->num, c->num));
404 /* Initialize channel, enable receiver and transmitter */
405 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
406 /* Repeat the command, to avoid the rev.H bug */
407 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
410 outw (ARBCNT(port), DMABUFSZ);
411 outb (ARBSTS(port), BSTS_OWN24);
412 outw (BRBCNT(port), DMABUFSZ);
413 outb (BRBSTS(port), BSTS_OWN24);
415 /* Raise DTR and RTS */
419 /* Enable interrupts */
420 outb (IER(port), IER_RXD | IER_TXD);
424 * Fill transmitter buffer with data.
427 cxput (cx_chan_t *c, char b)
431 unsigned short port = c->chip->port, len, cnt_port, sts_port;
433 /* Choose the buffer. */
436 cnt_port = ATBCNT(port);
437 sts_port = ATBSTS(port);
440 cnt_port = BTBCNT(port);
441 sts_port = BTBSTS(port);
445 if (inb (sts_port) & BSTS_OWN24) {
446 if (c->ifp->if_flags & IFF_DEBUG)
447 print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
448 c->board->num, c->num, b,
449 inb (sts_port), BSTS_BITS));
453 /* Get the packet to send. */
454 m = sppp_dequeue (c->master);
457 len = m->m_pkthdr.len;
459 /* Count the transmitted bytes to the subchannel, not the master. */
460 c->master->if_obytes -= len + 3;
461 c->ifp->if_obytes += len + 3;
462 c->stat->obytes += len + 3;
464 if (len >= DMABUFSZ) {
465 printf ("cx%d.%d: too long packet: %d bytes: ",
466 c->board->num, c->num, len);
471 m_copydata (m, 0, len, buf);
473 bpf_mtap (c->ifp, m);
476 /* Start transmitter. */
477 outw (cnt_port, len);
478 outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
480 if (c->ifp->if_flags & IFF_DEBUG)
481 print (("cx%d.%d: enqueue %d bytes to %c\n",
482 c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
484 c->ifp->if_flags |= IFF_OACTIVE;
488 * Start output on the (slave) interface. Get another datagram to send
489 * off of the interface queue, and copy it to the interface
490 * before starting the output.
493 cxsend (cx_chan_t *c)
495 unsigned short port = c->chip->port;
497 if (c->ifp->if_flags & IFF_DEBUG)
498 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
500 /* No output if the interface is down. */
501 if (! (c->ifp->if_flags & IFF_RUNNING))
504 /* Set the current channel number. */
505 outb (CAR(port), c->num & 3);
507 /* Determine the buffer order. */
508 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
516 /* Set up transmit timeout. */
517 if (c->master->if_flags & IFF_OACTIVE)
518 c->master->if_timer = TXTIMEOUT;
521 * Enable TXMPTY interrupt,
522 * to catch the case when the second buffer is empty.
524 if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
525 (inb (BTBSTS(port)) & BSTS_OWN24)) {
526 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
528 outb (IER(port), IER_RXD | IER_TXD);
532 * Start output on the (master) interface and all slave interfaces.
533 * Always called on splimp().
536 cxstart (struct ifnet *ifp)
538 cx_chan_t *q, *c = ifp->if_softc;
540 if (c->ifp->if_flags & IFF_DEBUG)
541 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
543 /* Start the master subchannel. */
546 /* Start all slave subchannels. */
547 if (c->slaveq && ! sppp_isempty (c->master))
548 for (q=c->slaveq; q; q=q->slaveq)
549 if ((q->ifp->if_flags & IFF_RUNNING) &&
550 ! (q->ifp->if_flags & IFF_OACTIVE))
555 * Handle transmit timeouts.
556 * Recover after lost transmit interrupts.
557 * Always called on splimp().
560 cxwatchdog (struct ifnet *ifp)
562 cx_chan_t *q, *c = ifp->if_softc;
564 if (! (ifp->if_flags & IFF_RUNNING))
566 if (ifp->if_flags & IFF_DEBUG)
567 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
570 for (q=c->slaveq; q; q=q->slaveq)
574 for (q=c->slaveq; q; q=q->slaveq)
581 * Handle receive interrupts, including receive errors and
582 * receive timeout interrupt.
585 cxrinth (cx_chan_t *c)
587 unsigned short port = c->chip->port;
588 unsigned short len, risr = inw (RISR(port));
590 /* Receive errors. */
591 if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
592 if (c->ifp->if_flags & IFF_DEBUG)
593 printf ("cx%d.%d: receive error, risr=%b\n",
594 c->board->num, c->num, risr, RISH_BITS);
595 ++c->ifp->if_ierrors;
597 if (risr & RIS_OVERRUN)
598 ++c->ifp->if_collisions;
599 } else if (risr & RIS_EOBUF) {
600 if (c->ifp->if_flags & IFF_DEBUG)
601 print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
602 c->board->num, c->num, risr, RISH_BITS,
603 inb (ARBSTS(port)), BSTS_BITS,
604 inb (BRBSTS(port)), BSTS_BITS));
607 /* Handle received data. */
608 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
609 c->stat->ibytes += len;
610 if (len > DMABUFSZ) {
611 /* Fatal error: actual DMA transfer size
612 * exceeds our buffer size. It could be caused
613 * by incorrectly programmed DMA register or
614 * hardware fault. Possibly, should panic here. */
615 printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
616 c->board->num, c->num, len);
617 ++c->ifp->if_ierrors;
618 } else if (! (risr & RIS_EOFR)) {
619 /* The received frame does not fit in the DMA buffer.
620 * It could be caused by serial lie noise,
621 * or if the peer has too big MTU. */
622 if (c->ifp->if_flags & IFF_DEBUG)
623 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
624 c->board->num, c->num, risr, RISH_BITS);
625 ++c->ifp->if_ierrors;
627 /* Valid frame received. */
628 if (c->ifp->if_flags & IFF_DEBUG)
629 print (("cx%d.%d: hdlc received %d bytes\n",
630 c->board->num, c->num, len));
631 cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
632 ++c->ifp->if_ipackets;
634 } else if (c->ifp->if_flags & IFF_DEBUG) {
635 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
636 c->board->num, c->num, risr, RISH_BITS));
640 /* Restart receiver. */
641 if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
642 outw (ARBCNT(port), DMABUFSZ);
643 outb (ARBSTS(port), BSTS_OWN24);
645 if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
646 outw (BRBCNT(port), DMABUFSZ);
647 outb (BRBSTS(port), BSTS_OWN24);
652 * Handle transmit interrupt.
655 cxtinth (cx_chan_t *c)
657 unsigned short port = c->chip->port;
658 unsigned char tisr = inb (TISR(port));
659 unsigned char teoir = 0;
661 c->ifp->if_flags &= ~IFF_OACTIVE;
662 if (c->ifp == c->master)
663 c->ifp->if_timer = 0;
665 if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
666 /* if (c->ifp->if_flags & IFF_DEBUG) */
667 print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
668 c->board->num, c->num, tisr, TIS_BITS,
669 inb (ATBSTS(port)), BSTS_BITS,
670 inb (BTBSTS(port)), BSTS_BITS));
671 ++c->ifp->if_oerrors;
674 /* Terminate the failed buffer. */
675 /* teoir = TEOI_TERMBUFF; */
676 } else if (c->ifp->if_flags & IFF_DEBUG)
677 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
678 c->board->num, c->num, tisr, TIS_BITS,
679 inb (ATBSTS(port)), BSTS_BITS,
680 inb (BTBSTS(port)), BSTS_BITS));
682 if (tisr & TIS_EOFR) {
683 ++c->ifp->if_opackets;
687 /* Start output on the (sub-) channel. */
696 cx_board_t *b = cxboard + bnum;
697 while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
698 /* Acknowledge the interrupt to enter the interrupt context. */
699 /* Read the local interrupt vector register. */
700 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
701 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
702 unsigned short port = c->chip->port;
703 unsigned short eoiport = REOIR(port);
704 unsigned char eoi = 0;
706 if (c->type == T_NONE) {
707 printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
708 c->board->num, c->num, livr);
709 continue; /* incorrect channel number? */
711 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
712 c->board->num, c->num, livr)); */
714 /* Clear RTS to stop receiver data flow while we are busy
715 * processing the interrupt, thus avoiding underruns. */
716 if (! c->sopt.norts) {
717 outb (MSVR_RTS(port), 0);
722 case LIV_EXCEP: /* receive exception */
723 case LIV_RXDATA: /* receive interrupt */
726 case M_ASYNC: eoi = cxrinta (c); break;
727 case M_HDLC: cxrinth (c); break;
728 default:; /* No bisync and X.21 yet */
731 case LIV_TXDATA: /* transmit interrupt */
733 eoiport = TEOIR(port);
735 case M_ASYNC: cxtinta (c); break;
736 case M_HDLC: eoi = cxtinth (c); break;
737 default:; /* No bisync and X.21 yet */
740 case LIV_MODEM: /* modem/timer interrupt */
742 eoiport = MEOIR(port);
747 /* Raise RTS for this channel if and only if
748 * both receive buffers are empty. */
749 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
750 (inb (ARBSTS(port)) & BSTS_OWN24) &&
751 (inb (BRBSTS(port)) & BSTS_OWN24)) {
752 outb (MSVR_RTS(port), MSV_RTS);
756 /* Exit from interrupt context. */
759 /* Master channel - start output on all idle subchannels. */
760 if (c->master == c->ifp && c->slaveq &&
761 (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
762 ! sppp_isempty (c->ifp)) {
765 for (q=c->slaveq; q; q=q->slaveq)
766 if ((q->ifp->if_flags & IFF_RUNNING) &&
767 ! (q->ifp->if_flags & IFF_OACTIVE))
774 * Process the received packet.
777 cxinput (cx_chan_t *c, void *buf, unsigned len)
780 struct mbuf *m = makembuf (buf, len);
782 if (c->ifp->if_flags & IFF_DEBUG)
783 printf ("cx%d.%d: no memory for packet\n",
784 c->board->num, c->num);
785 ++c->ifp->if_iqdrops;
788 m->m_pkthdr.rcvif = c->master;
790 if (c->ifp->if_flags & IFF_DEBUG)
795 * Check if there's a BPF listener on this interface.
796 * If so, hand off the raw packet to bpf.
799 bpf_tap (c->ifp, buf, len);
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;
805 sppp_input (c->master, m);
808 void cxswitch (cx_chan_t *c, cx_soft_opt_t new)
812 struct sppp *sp = (struct sppp*) c->ifp;
814 #if 0 /* Doesn't work this way any more 990402 /phk */
816 sp->pp_flags |= PP_CISCO;
818 sp->pp_flags &= ~PP_CISCO;
821 sp->pp_flags |= PP_KEEPALIVE;
823 sp->pp_flags &= ~PP_KEEPALIVE;