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.8 2004/01/06 01:40:47 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>
54 void cxswitch (cx_chan_t *c, cx_soft_opt_t new);
56 static int cxprobe (struct isa_device *id);
57 static int cxattach (struct isa_device *id);
58 static void cxput (cx_chan_t *c, char b);
59 static void cxsend (cx_chan_t *c);
60 static void cxrinth (cx_chan_t *c);
61 static ointhand2_t cxintr;
62 static int cxtinth (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,
105 DECLARE_DUMMY_MODULE(if_cx);
108 * Check that the value is contained in the list of correct values.
110 static int valid (unsigned short value, unsigned short *list)
113 if (value == *list++)
119 * Print the mbuf chain, for debug purposes only.
121 static void printmbuf (struct mbuf *m)
124 for (; m; m=m->m_next) {
125 if (m->m_flags & M_PKTHDR)
126 printf (" HDR %d:", m->m_pkthdr.len);
127 if (m->m_flags & M_EXT)
129 printf (" %d", m->m_len);
135 * Make an mbuf from data.
137 static struct mbuf *makembuf (void *buf, unsigned len)
139 struct mbuf *m, *o, *p;
141 MGETHDR (m, M_DONTWAIT, MT_DATA);
144 if (len >= MINCLSIZE)
145 MCLGET (m, M_DONTWAIT);
146 m->m_pkthdr.len = len;
151 unsigned n = M_TRAILINGSPACE (p);
156 /* Allocate new mbuf. */
158 MGET (p, M_DONTWAIT, MT_DATA);
163 if (len >= MINCLSIZE)
164 MCLGET (p, M_DONTWAIT);
168 n = M_TRAILINGSPACE (p);
173 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
176 buf = (char *)buf + n;
183 * Test the presence of the adapter on the given i/o port.
186 cxprobe (struct isa_device *id)
188 int unit = id->id_unit;
189 int iobase = id->id_iobase;
190 int irq = id->id_irq;
191 int drq = id->id_drq;
193 irqnum = ffs (irq) - 1;
195 print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",
196 unit, iobase, irqnum, drq));
197 if (! valid (irqnum, irq_valid_values)) {
198 printf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);
201 if (! valid (iobase, port_valid_values)) {
202 printf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);
205 if (! valid (drq, drq_valid_values)) {
206 printf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);
209 if (! cx_probe_board (iobase))
216 * The adapter is present, initialize the driver structures.
220 cxattach (struct isa_device *id)
222 int unit = id->id_unit;
223 int iobase = id->id_iobase;
224 int irq = id->id_irq;
225 int drq = id->id_drq;
226 cx_board_t *b = cxboard + unit;
230 id->id_ointr = cxintr;
232 /* Initialize the board structure. */
233 cx_init (b, unit, iobase, ffs(irq)-1, drq);
235 for (i=0; i<NCHAN; ++i) {
236 cx_chan_t *c = b->chan + i;
237 int u = b->num*NCHAN + i;
240 if (c->type == T_NONE)
243 /* Allocate the buffer memory. */
244 c->arbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
245 c->brbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
246 c->atbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
247 c->btbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);
249 /* All buffers should be located in lower 16M of memory! */
250 if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {
251 printf ("cx%d.%d: No memory for channel buffers\n",
252 c->board->num, c->num);
263 c->ifp = malloc (IFSTRUCTSZ, M_DEVBUF, M_NOWAIT);
265 printf ("cx%d.%d: No memory for ifnet buffer\n",
266 c->board->num, c->num);
270 bzero (c->ifp, IFSTRUCTSZ);
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);
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);
288 /* Reset the adapter. */
291 /* Activate the timeout routine. */
293 timeout (cxtimeout, 0, hz*5);
295 printf ("cx%d: <Cronyx-%s>\n", unit, b->name);
296 make_dev(&cx_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
300 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
303 * Process an ioctl request.
306 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
308 cx_chan_t *q, *c = ifp->if_softc;
309 int error, s, was_up, should_be_up;
312 * No socket ioctls while the channel is in async mode.
314 if (c->type==T_NONE || c->mode==M_ASYNC)
318 * Socket ioctls on slave subchannels are not allowed.
320 if (c->master != c->ifp)
323 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
324 error = sppp_ioctl (ifp, cmd, data);
328 print (("cxioctl (%d.%d, ", c->board->num, c->num));
331 print (("0x%x)\n", cmd));
334 print (("SIOCADDMULTI)\n"));
337 print (("SIOCDELMULTI)\n"));
340 print (("SIOCSIFFLAGS)\n"));
343 print (("SIOCSIFADDR)\n"));
347 /* We get here only in case of SIFFLAGS or SIFADDR. */
349 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
350 if (!was_up && should_be_up) {
351 /* Interface goes up -- start it. */
354 /* Start all slave subchannels. */
355 for (q=c->slaveq; q; q=q->slaveq)
359 } else if (was_up && !should_be_up) {
360 /* Interface is going down -- stop it. */
363 /* Stop all slave subchannels. */
364 for (q=c->slaveq; q; q=q->slaveq)
367 /* Flush the interface output queue */
376 * Stop the interface. Called on splimp().
379 cxdown (cx_chan_t *c)
381 unsigned short port = c->chip->port;
383 print (("cx%d.%d: cxdown\n", c->board->num, c->num));
385 /* The interface is down, stop it */
386 c->ifp->if_flags &= ~IFF_OACTIVE;
388 /* Reset the channel (for sync modes only) */
389 outb (CAR(port), c->num & 3);
390 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
396 * Start the interface. Called on splimp().
401 unsigned short port = c->chip->port;
403 /* The interface is up, start it */
404 print (("cx%d.%d: cxup\n", c->board->num, c->num));
406 /* Initialize channel, enable receiver and transmitter */
407 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
408 /* Repeat the command, to avoid the rev.H bug */
409 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
412 outw (ARBCNT(port), DMABUFSZ);
413 outb (ARBSTS(port), BSTS_OWN24);
414 outw (BRBCNT(port), DMABUFSZ);
415 outb (BRBSTS(port), BSTS_OWN24);
417 /* Raise DTR and RTS */
421 /* Enable interrupts */
422 outb (IER(port), IER_RXD | IER_TXD);
426 * Fill transmitter buffer with data.
429 cxput (cx_chan_t *c, char b)
433 unsigned short port = c->chip->port, len, cnt_port, sts_port;
435 /* Choose the buffer. */
438 cnt_port = ATBCNT(port);
439 sts_port = ATBSTS(port);
442 cnt_port = BTBCNT(port);
443 sts_port = BTBSTS(port);
447 if (inb (sts_port) & BSTS_OWN24) {
448 if (c->ifp->if_flags & IFF_DEBUG)
449 print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
450 c->board->num, c->num, b,
451 inb (sts_port), BSTS_BITS));
455 /* Get the packet to send. */
456 m = sppp_dequeue (c->master);
459 len = m->m_pkthdr.len;
461 /* Count the transmitted bytes to the subchannel, not the master. */
462 c->master->if_obytes -= len + 3;
463 c->ifp->if_obytes += len + 3;
464 c->stat->obytes += len + 3;
466 if (len >= DMABUFSZ) {
467 printf ("cx%d.%d: too long packet: %d bytes: ",
468 c->board->num, c->num, len);
473 m_copydata (m, 0, len, buf);
475 bpf_mtap (c->ifp, m);
478 /* Start transmitter. */
479 outw (cnt_port, len);
480 outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
482 if (c->ifp->if_flags & IFF_DEBUG)
483 print (("cx%d.%d: enqueue %d bytes to %c\n",
484 c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
486 c->ifp->if_flags |= IFF_OACTIVE;
490 * Start output on the (slave) interface. Get another datagram to send
491 * off of the interface queue, and copy it to the interface
492 * before starting the output.
495 cxsend (cx_chan_t *c)
497 unsigned short port = c->chip->port;
499 if (c->ifp->if_flags & IFF_DEBUG)
500 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
502 /* No output if the interface is down. */
503 if (! (c->ifp->if_flags & IFF_RUNNING))
506 /* Set the current channel number. */
507 outb (CAR(port), c->num & 3);
509 /* Determine the buffer order. */
510 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
518 /* Set up transmit timeout. */
519 if (c->master->if_flags & IFF_OACTIVE)
520 c->master->if_timer = TXTIMEOUT;
523 * Enable TXMPTY interrupt,
524 * to catch the case when the second buffer is empty.
526 if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
527 (inb (BTBSTS(port)) & BSTS_OWN24)) {
528 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
530 outb (IER(port), IER_RXD | IER_TXD);
534 * Start output on the (master) interface and all slave interfaces.
535 * Always called on splimp().
538 cxstart (struct ifnet *ifp)
540 cx_chan_t *q, *c = ifp->if_softc;
542 if (c->ifp->if_flags & IFF_DEBUG)
543 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
545 /* Start the master subchannel. */
548 /* Start all slave subchannels. */
549 if (c->slaveq && ! sppp_isempty (c->master))
550 for (q=c->slaveq; q; q=q->slaveq)
551 if ((q->ifp->if_flags & IFF_RUNNING) &&
552 ! (q->ifp->if_flags & IFF_OACTIVE))
557 * Handle transmit timeouts.
558 * Recover after lost transmit interrupts.
559 * Always called on splimp().
562 cxwatchdog (struct ifnet *ifp)
564 cx_chan_t *q, *c = ifp->if_softc;
566 if (! (ifp->if_flags & IFF_RUNNING))
568 if (ifp->if_flags & IFF_DEBUG)
569 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
572 for (q=c->slaveq; q; q=q->slaveq)
576 for (q=c->slaveq; q; q=q->slaveq)
583 * Handle receive interrupts, including receive errors and
584 * receive timeout interrupt.
587 cxrinth (cx_chan_t *c)
589 unsigned short port = c->chip->port;
590 unsigned short len, risr = inw (RISR(port));
592 /* Receive errors. */
593 if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
594 if (c->ifp->if_flags & IFF_DEBUG)
595 printf ("cx%d.%d: receive error, risr=%b\n",
596 c->board->num, c->num, risr, RISH_BITS);
597 ++c->ifp->if_ierrors;
599 if (risr & RIS_OVERRUN)
600 ++c->ifp->if_collisions;
601 } else if (risr & RIS_EOBUF) {
602 if (c->ifp->if_flags & IFF_DEBUG)
603 print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
604 c->board->num, c->num, risr, RISH_BITS,
605 inb (ARBSTS(port)), BSTS_BITS,
606 inb (BRBSTS(port)), BSTS_BITS));
609 /* Handle received data. */
610 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
611 c->stat->ibytes += len;
612 if (len > DMABUFSZ) {
613 /* Fatal error: actual DMA transfer size
614 * exceeds our buffer size. It could be caused
615 * by incorrectly programmed DMA register or
616 * hardware fault. Possibly, should panic here. */
617 printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
618 c->board->num, c->num, len);
619 ++c->ifp->if_ierrors;
620 } else if (! (risr & RIS_EOFR)) {
621 /* The received frame does not fit in the DMA buffer.
622 * It could be caused by serial lie noise,
623 * or if the peer has too big MTU. */
624 if (c->ifp->if_flags & IFF_DEBUG)
625 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
626 c->board->num, c->num, risr, RISH_BITS);
627 ++c->ifp->if_ierrors;
629 /* Valid frame received. */
630 if (c->ifp->if_flags & IFF_DEBUG)
631 print (("cx%d.%d: hdlc received %d bytes\n",
632 c->board->num, c->num, len));
633 cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
634 ++c->ifp->if_ipackets;
636 } else if (c->ifp->if_flags & IFF_DEBUG) {
637 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
638 c->board->num, c->num, risr, RISH_BITS));
642 /* Restart receiver. */
643 if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
644 outw (ARBCNT(port), DMABUFSZ);
645 outb (ARBSTS(port), BSTS_OWN24);
647 if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
648 outw (BRBCNT(port), DMABUFSZ);
649 outb (BRBSTS(port), BSTS_OWN24);
654 * Handle transmit interrupt.
657 cxtinth (cx_chan_t *c)
659 unsigned short port = c->chip->port;
660 unsigned char tisr = inb (TISR(port));
661 unsigned char teoir = 0;
663 c->ifp->if_flags &= ~IFF_OACTIVE;
664 if (c->ifp == c->master)
665 c->ifp->if_timer = 0;
667 if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
668 /* if (c->ifp->if_flags & IFF_DEBUG) */
669 print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
670 c->board->num, c->num, tisr, TIS_BITS,
671 inb (ATBSTS(port)), BSTS_BITS,
672 inb (BTBSTS(port)), BSTS_BITS));
673 ++c->ifp->if_oerrors;
676 /* Terminate the failed buffer. */
677 /* teoir = TEOI_TERMBUFF; */
678 } else if (c->ifp->if_flags & IFF_DEBUG)
679 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
680 c->board->num, c->num, tisr, TIS_BITS,
681 inb (ATBSTS(port)), BSTS_BITS,
682 inb (BTBSTS(port)), BSTS_BITS));
684 if (tisr & TIS_EOFR) {
685 ++c->ifp->if_opackets;
689 /* Start output on the (sub-) channel. */
698 cx_board_t *b = cxboard + bnum;
699 while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
700 /* Acknowledge the interrupt to enter the interrupt context. */
701 /* Read the local interrupt vector register. */
702 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
703 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
704 unsigned short port = c->chip->port;
705 unsigned short eoiport = REOIR(port);
706 unsigned char eoi = 0;
708 if (c->type == T_NONE) {
709 printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
710 c->board->num, c->num, livr);
711 continue; /* incorrect channel number? */
713 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
714 c->board->num, c->num, livr)); */
716 /* Clear RTS to stop receiver data flow while we are busy
717 * processing the interrupt, thus avoiding underruns. */
718 if (! c->sopt.norts) {
719 outb (MSVR_RTS(port), 0);
724 case LIV_EXCEP: /* receive exception */
725 case LIV_RXDATA: /* receive interrupt */
728 case M_ASYNC: eoi = cxrinta (c); break;
729 case M_HDLC: cxrinth (c); break;
730 default:; /* No bisync and X.21 yet */
733 case LIV_TXDATA: /* transmit interrupt */
735 eoiport = TEOIR(port);
737 case M_ASYNC: cxtinta (c); break;
738 case M_HDLC: eoi = cxtinth (c); break;
739 default:; /* No bisync and X.21 yet */
742 case LIV_MODEM: /* modem/timer interrupt */
744 eoiport = MEOIR(port);
749 /* Raise RTS for this channel if and only if
750 * both receive buffers are empty. */
751 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
752 (inb (ARBSTS(port)) & BSTS_OWN24) &&
753 (inb (BRBSTS(port)) & BSTS_OWN24)) {
754 outb (MSVR_RTS(port), MSV_RTS);
758 /* Exit from interrupt context. */
761 /* Master channel - start output on all idle subchannels. */
762 if (c->master == c->ifp && c->slaveq &&
763 (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
764 ! sppp_isempty (c->ifp)) {
767 for (q=c->slaveq; q; q=q->slaveq)
768 if ((q->ifp->if_flags & IFF_RUNNING) &&
769 ! (q->ifp->if_flags & IFF_OACTIVE))
776 * Process the received packet.
779 cxinput (cx_chan_t *c, void *buf, unsigned len)
782 struct mbuf *m = makembuf (buf, len);
784 if (c->ifp->if_flags & IFF_DEBUG)
785 printf ("cx%d.%d: no memory for packet\n",
786 c->board->num, c->num);
787 ++c->ifp->if_iqdrops;
790 m->m_pkthdr.rcvif = c->master;
792 if (c->ifp->if_flags & IFF_DEBUG)
797 * Check if there's a BPF listener on this interface.
798 * If so, hand off the raw packet to bpf.
801 bpf_tap (c->ifp, buf, len);
803 /* Count the received bytes to the subchannel, not the master. */
804 c->master->if_ibytes -= len + 3;
805 c->ifp->if_ibytes += len + 3;
807 sppp_input (c->master, m);
810 void cxswitch (cx_chan_t *c, cx_soft_opt_t new)
814 struct sppp *sp = (struct sppp*) c->ifp;
816 #if 0 /* Doesn't work this way any more 990402 /phk */
818 sp->pp_flags |= PP_CISCO;
820 sp->pp_flags &= ~PP_CISCO;
823 sp->pp_flags |= PP_KEEPALIVE;
825 sp->pp_flags &= ~PP_KEEPALIVE;