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.5 2003/08/09 20:54:39 drhodus 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>
54 void cxswitch (cx_chan_t *c, cx_soft_opt_t new);
56 static int cxprobe __P((struct isa_device *id));
57 static int cxattach __P((struct isa_device *id));
58 static void cxput __P((cx_chan_t *c, char b));
59 static void cxsend __P((cx_chan_t *c));
60 static void cxrinth __P((cx_chan_t *c));
61 static ointhand2_t cxintr;
62 static int cxtinth __P((cx_chan_t *c));
65 # define print(s) printf s
67 # define print(s) {/*void*/}
70 #define TXTIMEOUT 10 /* transmit timeout in seconds */
71 #define DMABUFSZ (6*256) /* buffer size */
72 #define PPP_HEADER_LEN 4 /* size of PPP header */
75 * Under BSDI it's possible to use general p2p protocol scheme,
76 * as well as our own one. Switching is done via IFF_ALTPHYS flag.
77 * Our ifnet pointer holds the buffer large enough to contain
78 * any of sppp and p2p structures.
80 #define IFSTRUCTSZ (sizeof (struct sppp))
81 #define IFNETSZ (sizeof (struct ifnet))
83 static int cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
84 static void cxstart (struct ifnet *ifp);
85 static void cxwatchdog (struct ifnet *ifp);
86 static void cxinput (cx_chan_t *c, void *buf, unsigned len);
87 extern int cxrinta (cx_chan_t *c);
88 extern void cxtinta (cx_chan_t *c);
89 extern void cxmint (cx_chan_t *c);
90 extern timeout_t cxtimeout;
91 static void cxdown (cx_chan_t *c);
92 static void cxup (cx_chan_t *c);
94 cx_board_t cxboard [NCX]; /* adapter state structures */
95 cx_chan_t *cxchan [NCX*NCHAN]; /* unit to channel struct pointer */
97 extern struct cdevsw cx_cdevsw;
99 static unsigned short irq_valid_values [] = { 3, 5, 7, 10, 11, 12, 15, 0 };
100 static unsigned short drq_valid_values [] = { 5, 6, 7, 0 };
101 static unsigned short port_valid_values [] = {
102 0x240, 0x260, 0x280, 0x300, 0x320, 0x380, 0x3a0, 0,
106 * Check that the value is contained in the list of correct values.
108 static int valid (unsigned short value, unsigned short *list)
111 if (value == *list++)
117 * Print the mbuf chain, for debug purposes only.
119 static void printmbuf (struct mbuf *m)
122 for (; m; m=m->m_next) {
123 if (m->m_flags & M_PKTHDR)
124 printf (" HDR %d:", m->m_pkthdr.len);
125 if (m->m_flags & M_EXT)
127 printf (" %d", m->m_len);
133 * Make an mbuf from data.
135 static struct mbuf *makembuf (void *buf, unsigned len)
137 struct mbuf *m, *o, *p;
139 MGETHDR (m, M_DONTWAIT, MT_DATA);
142 if (len >= MINCLSIZE)
143 MCLGET (m, M_DONTWAIT);
144 m->m_pkthdr.len = len;
149 unsigned n = M_TRAILINGSPACE (p);
154 /* Allocate new mbuf. */
156 MGET (p, M_DONTWAIT, MT_DATA);
161 if (len >= MINCLSIZE)
162 MCLGET (p, M_DONTWAIT);
166 n = M_TRAILINGSPACE (p);
171 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
174 buf = (char *)buf + n;
181 * Test the presence of the adapter on the given i/o port.
184 cxprobe (struct isa_device *id)
186 int unit = id->id_unit;
187 int iobase = id->id_iobase;
188 int irq = id->id_irq;
189 int drq = id->id_drq;
191 irqnum = ffs (irq) - 1;
193 print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",
194 unit, iobase, irqnum, drq));
195 if (! valid (irqnum, irq_valid_values)) {
196 printf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);
199 if (! valid (iobase, port_valid_values)) {
200 printf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);
203 if (! valid (drq, drq_valid_values)) {
204 printf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);
207 if (! cx_probe_board (iobase))
214 * The adapter is present, initialize the driver structures.
218 cxattach (struct isa_device *id)
220 int unit = id->id_unit;
221 int iobase = id->id_iobase;
222 int irq = id->id_irq;
223 int drq = id->id_drq;
224 cx_board_t *b = cxboard + unit;
228 id->id_ointr = cxintr;
230 /* Initialize the board structure. */
231 cx_init (b, unit, iobase, ffs(irq)-1, drq);
233 for (i=0; i<NCHAN; ++i) {
234 cx_chan_t *c = b->chan + i;
235 int u = b->num*NCHAN + i;
238 if (c->type == T_NONE)
241 /* Allocate the buffer memory. */
242 c->arbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
243 c->brbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
244 c->atbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
245 c->btbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
247 /* All buffers should be located in lower 16M of memory! */
248 if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {
249 printf ("cx%d.%d: No memory for channel buffers\n",
250 c->board->num, c->num);
261 c->ifp = malloc (IFSTRUCTSZ, M_DEVBUF, M_NOWAIT);
263 printf ("cx%d.%d: No memory for ifnet buffer\n",
264 c->board->num, c->num);
268 bzero (c->ifp, IFSTRUCTSZ);
270 c->ifp->if_softc = c;
272 c->ifp->if_name = "cx";
273 c->ifp->if_mtu = PP_MTU;
274 c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
275 c->ifp->if_ioctl = cxsioctl;
276 c->ifp->if_start = (start_func_t) cxstart;
277 c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;
278 /* Init routine is never called by upper level? */
279 sppp_attach (c->ifp);
281 sp = (struct sppp*) c->ifp;
282 /* If BPF is in the kernel, call the attach for it. */
283 bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);
287 /* Reset the adapter. */
290 /* Activate the timeout routine. */
292 timeout (cxtimeout, 0, hz*5);
294 printf ("cx%d: <Cronyx-%s>\n", unit, b->name);
295 make_dev(&cx_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
299 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
302 * Process an ioctl request.
305 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
307 cx_chan_t *q, *c = ifp->if_softc;
308 int error, s, was_up, should_be_up;
311 * No socket ioctls while the channel is in async mode.
313 if (c->type==T_NONE || c->mode==M_ASYNC)
317 * Socket ioctls on slave subchannels are not allowed.
319 if (c->master != c->ifp)
322 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
323 error = sppp_ioctl (ifp, cmd, data);
327 print (("cxioctl (%d.%d, ", c->board->num, c->num));
330 print (("0x%x)\n", cmd));
333 print (("SIOCADDMULTI)\n"));
336 print (("SIOCDELMULTI)\n"));
339 print (("SIOCSIFFLAGS)\n"));
342 print (("SIOCSIFADDR)\n"));
346 /* We get here only in case of SIFFLAGS or SIFADDR. */
348 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
349 if (!was_up && should_be_up) {
350 /* Interface goes up -- start it. */
353 /* Start all slave subchannels. */
354 for (q=c->slaveq; q; q=q->slaveq)
358 } else if (was_up && !should_be_up) {
359 /* Interface is going down -- stop it. */
362 /* Stop all slave subchannels. */
363 for (q=c->slaveq; q; q=q->slaveq)
366 /* Flush the interface output queue */
375 * Stop the interface. Called on splimp().
378 cxdown (cx_chan_t *c)
380 unsigned short port = c->chip->port;
382 print (("cx%d.%d: cxdown\n", c->board->num, c->num));
384 /* The interface is down, stop it */
385 c->ifp->if_flags &= ~IFF_OACTIVE;
387 /* Reset the channel (for sync modes only) */
388 outb (CAR(port), c->num & 3);
389 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
395 * Start the interface. Called on splimp().
400 unsigned short port = c->chip->port;
402 /* The interface is up, start it */
403 print (("cx%d.%d: cxup\n", c->board->num, c->num));
405 /* Initialize channel, enable receiver and transmitter */
406 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
407 /* Repeat the command, to avoid the rev.H bug */
408 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
411 outw (ARBCNT(port), DMABUFSZ);
412 outb (ARBSTS(port), BSTS_OWN24);
413 outw (BRBCNT(port), DMABUFSZ);
414 outb (BRBSTS(port), BSTS_OWN24);
416 /* Raise DTR and RTS */
420 /* Enable interrupts */
421 outb (IER(port), IER_RXD | IER_TXD);
425 * Fill transmitter buffer with data.
428 cxput (cx_chan_t *c, char b)
432 unsigned short port = c->chip->port, len, cnt_port, sts_port;
434 /* Choose the buffer. */
437 cnt_port = ATBCNT(port);
438 sts_port = ATBSTS(port);
441 cnt_port = BTBCNT(port);
442 sts_port = BTBSTS(port);
446 if (inb (sts_port) & BSTS_OWN24) {
447 if (c->ifp->if_flags & IFF_DEBUG)
448 print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
449 c->board->num, c->num, b,
450 inb (sts_port), BSTS_BITS));
454 /* Get the packet to send. */
455 m = sppp_dequeue (c->master);
458 len = m->m_pkthdr.len;
460 /* Count the transmitted bytes to the subchannel, not the master. */
461 c->master->if_obytes -= len + 3;
462 c->ifp->if_obytes += len + 3;
463 c->stat->obytes += len + 3;
465 if (len >= DMABUFSZ) {
466 printf ("cx%d.%d: too long packet: %d bytes: ",
467 c->board->num, c->num, len);
472 m_copydata (m, 0, len, buf);
474 bpf_mtap (c->ifp, m);
477 /* Start transmitter. */
478 outw (cnt_port, len);
479 outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
481 if (c->ifp->if_flags & IFF_DEBUG)
482 print (("cx%d.%d: enqueue %d bytes to %c\n",
483 c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
485 c->ifp->if_flags |= IFF_OACTIVE;
489 * Start output on the (slave) interface. Get another datagram to send
490 * off of the interface queue, and copy it to the interface
491 * before starting the output.
494 cxsend (cx_chan_t *c)
496 unsigned short port = c->chip->port;
498 if (c->ifp->if_flags & IFF_DEBUG)
499 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
501 /* No output if the interface is down. */
502 if (! (c->ifp->if_flags & IFF_RUNNING))
505 /* Set the current channel number. */
506 outb (CAR(port), c->num & 3);
508 /* Determine the buffer order. */
509 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
517 /* Set up transmit timeout. */
518 if (c->master->if_flags & IFF_OACTIVE)
519 c->master->if_timer = TXTIMEOUT;
522 * Enable TXMPTY interrupt,
523 * to catch the case when the second buffer is empty.
525 if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
526 (inb (BTBSTS(port)) & BSTS_OWN24)) {
527 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
529 outb (IER(port), IER_RXD | IER_TXD);
533 * Start output on the (master) interface and all slave interfaces.
534 * Always called on splimp().
537 cxstart (struct ifnet *ifp)
539 cx_chan_t *q, *c = ifp->if_softc;
541 if (c->ifp->if_flags & IFF_DEBUG)
542 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
544 /* Start the master subchannel. */
547 /* Start all slave subchannels. */
548 if (c->slaveq && ! sppp_isempty (c->master))
549 for (q=c->slaveq; q; q=q->slaveq)
550 if ((q->ifp->if_flags & IFF_RUNNING) &&
551 ! (q->ifp->if_flags & IFF_OACTIVE))
556 * Handle transmit timeouts.
557 * Recover after lost transmit interrupts.
558 * Always called on splimp().
561 cxwatchdog (struct ifnet *ifp)
563 cx_chan_t *q, *c = ifp->if_softc;
565 if (! (ifp->if_flags & IFF_RUNNING))
567 if (ifp->if_flags & IFF_DEBUG)
568 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
571 for (q=c->slaveq; q; q=q->slaveq)
575 for (q=c->slaveq; q; q=q->slaveq)
582 * Handle receive interrupts, including receive errors and
583 * receive timeout interrupt.
586 cxrinth (cx_chan_t *c)
588 unsigned short port = c->chip->port;
589 unsigned short len, risr = inw (RISR(port));
591 /* Receive errors. */
592 if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
593 if (c->ifp->if_flags & IFF_DEBUG)
594 printf ("cx%d.%d: receive error, risr=%b\n",
595 c->board->num, c->num, risr, RISH_BITS);
596 ++c->ifp->if_ierrors;
598 if (risr & RIS_OVERRUN)
599 ++c->ifp->if_collisions;
600 } else if (risr & RIS_EOBUF) {
601 if (c->ifp->if_flags & IFF_DEBUG)
602 print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
603 c->board->num, c->num, risr, RISH_BITS,
604 inb (ARBSTS(port)), BSTS_BITS,
605 inb (BRBSTS(port)), BSTS_BITS));
608 /* Handle received data. */
609 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
610 c->stat->ibytes += len;
611 if (len > DMABUFSZ) {
612 /* Fatal error: actual DMA transfer size
613 * exceeds our buffer size. It could be caused
614 * by incorrectly programmed DMA register or
615 * hardware fault. Possibly, should panic here. */
616 printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
617 c->board->num, c->num, len);
618 ++c->ifp->if_ierrors;
619 } else if (! (risr & RIS_EOFR)) {
620 /* The received frame does not fit in the DMA buffer.
621 * It could be caused by serial lie noise,
622 * or if the peer has too big MTU. */
623 if (c->ifp->if_flags & IFF_DEBUG)
624 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
625 c->board->num, c->num, risr, RISH_BITS);
626 ++c->ifp->if_ierrors;
628 /* Valid frame received. */
629 if (c->ifp->if_flags & IFF_DEBUG)
630 print (("cx%d.%d: hdlc received %d bytes\n",
631 c->board->num, c->num, len));
632 cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
633 ++c->ifp->if_ipackets;
635 } else if (c->ifp->if_flags & IFF_DEBUG) {
636 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
637 c->board->num, c->num, risr, RISH_BITS));
641 /* Restart receiver. */
642 if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
643 outw (ARBCNT(port), DMABUFSZ);
644 outb (ARBSTS(port), BSTS_OWN24);
646 if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
647 outw (BRBCNT(port), DMABUFSZ);
648 outb (BRBSTS(port), BSTS_OWN24);
653 * Handle transmit interrupt.
656 cxtinth (cx_chan_t *c)
658 unsigned short port = c->chip->port;
659 unsigned char tisr = inb (TISR(port));
660 unsigned char teoir = 0;
662 c->ifp->if_flags &= ~IFF_OACTIVE;
663 if (c->ifp == c->master)
664 c->ifp->if_timer = 0;
666 if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
667 /* if (c->ifp->if_flags & IFF_DEBUG) */
668 print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
669 c->board->num, c->num, tisr, TIS_BITS,
670 inb (ATBSTS(port)), BSTS_BITS,
671 inb (BTBSTS(port)), BSTS_BITS));
672 ++c->ifp->if_oerrors;
675 /* Terminate the failed buffer. */
676 /* teoir = TEOI_TERMBUFF; */
677 } else if (c->ifp->if_flags & IFF_DEBUG)
678 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
679 c->board->num, c->num, tisr, TIS_BITS,
680 inb (ATBSTS(port)), BSTS_BITS,
681 inb (BTBSTS(port)), BSTS_BITS));
683 if (tisr & TIS_EOFR) {
684 ++c->ifp->if_opackets;
688 /* Start output on the (sub-) channel. */
697 cx_board_t *b = cxboard + bnum;
698 while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
699 /* Acknowledge the interrupt to enter the interrupt context. */
700 /* Read the local interrupt vector register. */
701 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
702 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
703 unsigned short port = c->chip->port;
704 unsigned short eoiport = REOIR(port);
705 unsigned char eoi = 0;
707 if (c->type == T_NONE) {
708 printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
709 c->board->num, c->num, livr);
710 continue; /* incorrect channel number? */
712 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
713 c->board->num, c->num, livr)); */
715 /* Clear RTS to stop receiver data flow while we are busy
716 * processing the interrupt, thus avoiding underruns. */
717 if (! c->sopt.norts) {
718 outb (MSVR_RTS(port), 0);
723 case LIV_EXCEP: /* receive exception */
724 case LIV_RXDATA: /* receive interrupt */
727 case M_ASYNC: eoi = cxrinta (c); break;
728 case M_HDLC: cxrinth (c); break;
729 default:; /* No bisync and X.21 yet */
732 case LIV_TXDATA: /* transmit interrupt */
734 eoiport = TEOIR(port);
736 case M_ASYNC: cxtinta (c); break;
737 case M_HDLC: eoi = cxtinth (c); break;
738 default:; /* No bisync and X.21 yet */
741 case LIV_MODEM: /* modem/timer interrupt */
743 eoiport = MEOIR(port);
748 /* Raise RTS for this channel if and only if
749 * both receive buffers are empty. */
750 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
751 (inb (ARBSTS(port)) & BSTS_OWN24) &&
752 (inb (BRBSTS(port)) & BSTS_OWN24)) {
753 outb (MSVR_RTS(port), MSV_RTS);
757 /* Exit from interrupt context. */
760 /* Master channel - start output on all idle subchannels. */
761 if (c->master == c->ifp && c->slaveq &&
762 (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
763 ! sppp_isempty (c->ifp)) {
766 for (q=c->slaveq; q; q=q->slaveq)
767 if ((q->ifp->if_flags & IFF_RUNNING) &&
768 ! (q->ifp->if_flags & IFF_OACTIVE))
775 * Process the received packet.
778 cxinput (cx_chan_t *c, void *buf, unsigned len)
781 struct mbuf *m = makembuf (buf, len);
783 if (c->ifp->if_flags & IFF_DEBUG)
784 printf ("cx%d.%d: no memory for packet\n",
785 c->board->num, c->num);
786 ++c->ifp->if_iqdrops;
789 m->m_pkthdr.rcvif = c->master;
791 if (c->ifp->if_flags & IFF_DEBUG)
796 * Check if there's a BPF listener on this interface.
797 * If so, hand off the raw packet to bpf.
800 bpf_tap (c->ifp, buf, len);
802 /* Count the received bytes to the subchannel, not the master. */
803 c->master->if_ibytes -= len + 3;
804 c->ifp->if_ibytes += len + 3;
806 sppp_input (c->master, m);
809 void cxswitch (cx_chan_t *c, cx_soft_opt_t new)
813 struct sppp *sp = (struct sppp*) c->ifp;
815 #if 0 /* Doesn't work this way any more 990402 /phk */
817 sp->pp_flags |= PP_CISCO;
819 sp->pp_flags &= ~PP_CISCO;
822 sp->pp_flags |= PP_KEEPALIVE;
824 sp->pp_flags &= ~PP_KEEPALIVE;