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.14 2004/09/19 01:27:23 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>
55 void cxswitch (cx_chan_t *c, cx_soft_opt_t new);
58 static int cxprobe (struct isa_device *id);
59 static int cxattach (struct isa_device *id);
60 static void cxput (cx_chan_t *c, char b);
61 static void cxsend (cx_chan_t *c);
62 static void cxrinth (cx_chan_t *c);
63 static ointhand2_t cxintr;
64 static int cxtinth (cx_chan_t *c);
67 # define print(s) printf s
69 # define print(s) {/*void*/}
72 #define TXTIMEOUT 10 /* transmit timeout in seconds */
73 #define DMABUFSZ (6*256) /* buffer size */
74 #define PPP_HEADER_LEN 4 /* size of PPP header */
77 * Under BSDI it's possible to use general p2p protocol scheme,
78 * as well as our own one. Switching is done via IFF_ALTPHYS flag.
79 * Our ifnet pointer holds the buffer large enough to contain
80 * any of sppp and p2p structures.
82 #define IFSTRUCTSZ (sizeof (struct sppp))
83 #define IFNETSZ (sizeof (struct ifnet))
85 static int cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
87 static void cxstart (struct ifnet *ifp);
88 static void cxwatchdog (struct ifnet *ifp);
89 static void cxinput (cx_chan_t *c, void *buf, unsigned len);
91 extern int cxrinta (cx_chan_t *c);
92 extern void cxtinta (cx_chan_t *c);
93 extern void cxmint (cx_chan_t *c);
94 extern timeout_t cxtimeout;
96 static void cxdown (cx_chan_t *c);
97 static void cxup (cx_chan_t *c);
99 cx_board_t cxboard [NCX]; /* adapter state structures */
100 cx_chan_t *cxchan [NCX*NCHAN]; /* unit to channel struct pointer */
102 extern struct cdevsw cx_cdevsw;
104 static unsigned short irq_valid_values [] = { 3, 5, 7, 10, 11, 12, 15, 0 };
105 static unsigned short drq_valid_values [] = { 5, 6, 7, 0 };
106 static unsigned short port_valid_values [] = {
107 0x240, 0x260, 0x280, 0x300, 0x320, 0x380, 0x3a0, 0,
109 struct callout cxtimeout_ch;
111 DECLARE_DUMMY_MODULE(if_cx);
114 * Check that the value is contained in the list of correct values.
116 static int valid (unsigned short value, unsigned short *list)
119 if (value == *list++)
125 * Print the mbuf chain, for debug purposes only.
127 static void printmbuf (struct mbuf *m)
130 for (; m; m=m->m_next) {
131 if (m->m_flags & M_PKTHDR)
132 printf (" HDR %d:", m->m_pkthdr.len);
133 if (m->m_flags & M_EXT)
135 printf (" %d", m->m_len);
141 * Make an mbuf from data.
143 static struct mbuf *makembuf (void *buf, unsigned len)
145 struct mbuf *m, *o, *p;
147 MGETHDR (m, MB_DONTWAIT, MT_DATA);
150 if (len >= MINCLSIZE)
151 MCLGET (m, MB_DONTWAIT);
152 m->m_pkthdr.len = len;
157 unsigned n = M_TRAILINGSPACE (p);
162 /* Allocate new mbuf. */
164 MGET (p, MB_DONTWAIT, MT_DATA);
169 if (len >= MINCLSIZE)
170 MCLGET (p, MB_DONTWAIT);
174 n = M_TRAILINGSPACE (p);
179 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
182 buf = (char *)buf + n;
189 * Test the presence of the adapter on the given i/o port.
192 cxprobe (struct isa_device *id)
194 int unit = id->id_unit;
195 int iobase = id->id_iobase;
196 int irq = id->id_irq;
197 int drq = id->id_drq;
199 irqnum = ffs (irq) - 1;
201 print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",
202 unit, iobase, irqnum, drq));
203 if (! valid (irqnum, irq_valid_values)) {
204 printf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);
207 if (! valid (iobase, port_valid_values)) {
208 printf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);
211 if (! valid (drq, drq_valid_values)) {
212 printf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);
215 if (! cx_probe_board (iobase))
222 * The adapter is present, initialize the driver structures.
226 cxattach (struct isa_device *id)
228 int unit = id->id_unit;
229 int iobase = id->id_iobase;
230 int irq = id->id_irq;
231 int drq = id->id_drq;
232 cx_board_t *b = cxboard + unit;
236 id->id_ointr = cxintr;
238 /* Initialize the board structure. */
239 cx_init (b, unit, iobase, ffs(irq)-1, drq);
241 for (i=0; i<NCHAN; ++i) {
242 cx_chan_t *c = b->chan + i;
243 int u = b->num*NCHAN + i;
246 if (c->type == T_NONE)
249 /* Allocate the buffer memory. */
250 c->arbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
251 c->brbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
252 c->atbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
253 c->btbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
256 /* All buffers should be located in lower 16M of memory! */
257 /* XXX all buffers located where? I don't think so! */
258 if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {
259 printf ("cx%d.%d: No memory for channel buffers\n",
260 c->board->num, c->num);
272 c->ifp = malloc (IFSTRUCTSZ, M_DEVBUF, M_WAITOK | M_ZERO);
274 c->ifp->if_softc = c;
275 if_initname(c->ifp, "cx", u);
276 c->ifp->if_mtu = PP_MTU;
277 c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
278 c->ifp->if_ioctl = cxsioctl;
279 c->ifp->if_start = (start_func_t) cxstart;
280 c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;
281 /* Init routine is never called by upper level? */
282 sppp_attach (c->ifp);
284 sp = (struct sppp*) c->ifp;
285 /* If BPF is in the kernel, call the attach for it. */
286 bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);
290 /* Reset the adapter. */
293 /* Activate the timeout routine. */
295 callout_init(&cxtimeout_ch);
296 callout_reset(&cxtimeout_ch, hz * 5, cxtimeout, NULL);
298 printf ("cx%d: <Cronyx-%s>\n", unit, b->name);
299 cdevsw_add(&cx_cdevsw, -1, unit);
300 make_dev(&cx_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
304 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
307 * Process an ioctl request.
310 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
312 cx_chan_t *q, *c = ifp->if_softc;
313 int error, s, was_up, should_be_up;
316 * No socket ioctls while the channel is in async mode.
318 if (c->type==T_NONE || c->mode==M_ASYNC)
322 * Socket ioctls on slave subchannels are not allowed.
324 if (c->master != c->ifp)
327 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
328 error = sppp_ioctl (ifp, cmd, data);
332 print (("cxioctl (%d.%d, ", c->board->num, c->num));
335 print (("0x%x)\n", cmd));
338 print (("SIOCADDMULTI)\n"));
341 print (("SIOCDELMULTI)\n"));
344 print (("SIOCSIFFLAGS)\n"));
347 print (("SIOCSIFADDR)\n"));
351 /* We get here only in case of SIFFLAGS or SIFADDR. */
353 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
354 if (!was_up && should_be_up) {
355 /* Interface goes up -- start it. */
358 /* Start all slave subchannels. */
359 for (q=c->slaveq; q; q=q->slaveq)
363 } else if (was_up && !should_be_up) {
364 /* Interface is going down -- stop it. */
367 /* Stop all slave subchannels. */
368 for (q=c->slaveq; q; q=q->slaveq)
371 /* Flush the interface output queue */
380 * Stop the interface. Called on splimp().
383 cxdown (cx_chan_t *c)
385 unsigned short port = c->chip->port;
387 print (("cx%d.%d: cxdown\n", c->board->num, c->num));
389 /* The interface is down, stop it */
390 c->ifp->if_flags &= ~IFF_OACTIVE;
392 /* Reset the channel (for sync modes only) */
393 outb (CAR(port), c->num & 3);
394 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
400 * Start the interface. Called on splimp().
405 unsigned short port = c->chip->port;
407 /* The interface is up, start it */
408 print (("cx%d.%d: cxup\n", c->board->num, c->num));
410 /* Initialize channel, enable receiver and transmitter */
411 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
412 /* Repeat the command, to avoid the rev.H bug */
413 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
416 outw (ARBCNT(port), DMABUFSZ);
417 outb (ARBSTS(port), BSTS_OWN24);
418 outw (BRBCNT(port), DMABUFSZ);
419 outb (BRBSTS(port), BSTS_OWN24);
421 /* Raise DTR and RTS */
425 /* Enable interrupts */
426 outb (IER(port), IER_RXD | IER_TXD);
430 * Fill transmitter buffer with data.
433 cxput (cx_chan_t *c, char b)
437 unsigned short port = c->chip->port, len, cnt_port, sts_port;
439 /* Choose the buffer. */
442 cnt_port = ATBCNT(port);
443 sts_port = ATBSTS(port);
446 cnt_port = BTBCNT(port);
447 sts_port = BTBSTS(port);
451 if (inb (sts_port) & BSTS_OWN24) {
452 if (c->ifp->if_flags & IFF_DEBUG)
453 print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
454 c->board->num, c->num, b,
455 inb (sts_port), BSTS_BITS));
459 /* Get the packet to send. */
460 m = sppp_dequeue (c->master);
463 len = m->m_pkthdr.len;
465 /* Count the transmitted bytes to the subchannel, not the master. */
466 c->master->if_obytes -= len + 3;
467 c->ifp->if_obytes += len + 3;
468 c->stat->obytes += len + 3;
470 if (len >= DMABUFSZ) {
471 printf ("cx%d.%d: too long packet: %d bytes: ",
472 c->board->num, c->num, len);
477 m_copydata (m, 0, len, buf);
479 bpf_mtap (c->ifp, m);
482 /* Start transmitter. */
483 outw (cnt_port, len);
484 outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
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'));
490 c->ifp->if_flags |= IFF_OACTIVE;
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.
499 cxsend (cx_chan_t *c)
501 unsigned short port = c->chip->port;
503 if (c->ifp->if_flags & IFF_DEBUG)
504 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
506 /* No output if the interface is down. */
507 if (! (c->ifp->if_flags & IFF_RUNNING))
510 /* Set the current channel number. */
511 outb (CAR(port), c->num & 3);
513 /* Determine the buffer order. */
514 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
522 /* Set up transmit timeout. */
523 if (c->master->if_flags & IFF_OACTIVE)
524 c->master->if_timer = TXTIMEOUT;
527 * Enable TXMPTY interrupt,
528 * to catch the case when the second buffer is empty.
530 if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
531 (inb (BTBSTS(port)) & BSTS_OWN24)) {
532 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
534 outb (IER(port), IER_RXD | IER_TXD);
538 * Start output on the (master) interface and all slave interfaces.
539 * Always called on splimp().
542 cxstart (struct ifnet *ifp)
544 cx_chan_t *q, *c = ifp->if_softc;
546 if (c->ifp->if_flags & IFF_DEBUG)
547 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
549 /* Start the master subchannel. */
552 /* Start all slave subchannels. */
553 if (c->slaveq && ! sppp_isempty (c->master))
554 for (q=c->slaveq; q; q=q->slaveq)
555 if ((q->ifp->if_flags & IFF_RUNNING) &&
556 ! (q->ifp->if_flags & IFF_OACTIVE))
561 * Handle transmit timeouts.
562 * Recover after lost transmit interrupts.
563 * Always called on splimp().
566 cxwatchdog (struct ifnet *ifp)
568 cx_chan_t *q, *c = ifp->if_softc;
570 if (! (ifp->if_flags & IFF_RUNNING))
572 if (ifp->if_flags & IFF_DEBUG)
573 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
576 for (q=c->slaveq; q; q=q->slaveq)
580 for (q=c->slaveq; q; q=q->slaveq)
587 * Handle receive interrupts, including receive errors and
588 * receive timeout interrupt.
591 cxrinth (cx_chan_t *c)
593 unsigned short port = c->chip->port;
594 unsigned short len, risr = inw (RISR(port));
596 /* Receive errors. */
597 if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
598 if (c->ifp->if_flags & IFF_DEBUG)
599 printf ("cx%d.%d: receive error, risr=%b\n",
600 c->board->num, c->num, risr, RISH_BITS);
601 ++c->ifp->if_ierrors;
603 if (risr & RIS_OVERRUN)
604 ++c->ifp->if_collisions;
605 } else if (risr & RIS_EOBUF) {
606 if (c->ifp->if_flags & IFF_DEBUG)
607 print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
608 c->board->num, c->num, risr, RISH_BITS,
609 inb (ARBSTS(port)), BSTS_BITS,
610 inb (BRBSTS(port)), BSTS_BITS));
613 /* Handle received data. */
614 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
615 c->stat->ibytes += len;
616 if (len > DMABUFSZ) {
617 /* Fatal error: actual DMA transfer size
618 * exceeds our buffer size. It could be caused
619 * by incorrectly programmed DMA register or
620 * hardware fault. Possibly, should panic here. */
621 printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
622 c->board->num, c->num, len);
623 ++c->ifp->if_ierrors;
624 } else if (! (risr & RIS_EOFR)) {
625 /* The received frame does not fit in the DMA buffer.
626 * It could be caused by serial lie noise,
627 * or if the peer has too big MTU. */
628 if (c->ifp->if_flags & IFF_DEBUG)
629 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
630 c->board->num, c->num, risr, RISH_BITS);
631 ++c->ifp->if_ierrors;
633 /* Valid frame received. */
634 if (c->ifp->if_flags & IFF_DEBUG)
635 print (("cx%d.%d: hdlc received %d bytes\n",
636 c->board->num, c->num, len));
637 cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
638 ++c->ifp->if_ipackets;
640 } else if (c->ifp->if_flags & IFF_DEBUG) {
641 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
642 c->board->num, c->num, risr, RISH_BITS));
646 /* Restart receiver. */
647 if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
648 outw (ARBCNT(port), DMABUFSZ);
649 outb (ARBSTS(port), BSTS_OWN24);
651 if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
652 outw (BRBCNT(port), DMABUFSZ);
653 outb (BRBSTS(port), BSTS_OWN24);
658 * Handle transmit interrupt.
661 cxtinth (cx_chan_t *c)
663 unsigned short port = c->chip->port;
664 unsigned char tisr = inb (TISR(port));
665 unsigned char teoir = 0;
667 c->ifp->if_flags &= ~IFF_OACTIVE;
668 if (c->ifp == c->master)
669 c->ifp->if_timer = 0;
671 if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
672 /* if (c->ifp->if_flags & IFF_DEBUG) */
673 print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
674 c->board->num, c->num, tisr, TIS_BITS,
675 inb (ATBSTS(port)), BSTS_BITS,
676 inb (BTBSTS(port)), BSTS_BITS));
677 ++c->ifp->if_oerrors;
680 /* Terminate the failed buffer. */
681 /* teoir = TEOI_TERMBUFF; */
682 } else if (c->ifp->if_flags & IFF_DEBUG)
683 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
684 c->board->num, c->num, tisr, TIS_BITS,
685 inb (ATBSTS(port)), BSTS_BITS,
686 inb (BTBSTS(port)), BSTS_BITS));
688 if (tisr & TIS_EOFR) {
689 ++c->ifp->if_opackets;
693 /* Start output on the (sub-) channel. */
702 cx_board_t *b = cxboard + bnum;
703 while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
704 /* Acknowledge the interrupt to enter the interrupt context. */
705 /* Read the local interrupt vector register. */
706 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
707 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
708 unsigned short port = c->chip->port;
709 unsigned short eoiport = REOIR(port);
710 unsigned char eoi = 0;
712 if (c->type == T_NONE) {
713 printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
714 c->board->num, c->num, livr);
715 continue; /* incorrect channel number? */
717 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
718 c->board->num, c->num, livr)); */
720 /* Clear RTS to stop receiver data flow while we are busy
721 * processing the interrupt, thus avoiding underruns. */
722 if (! c->sopt.norts) {
723 outb (MSVR_RTS(port), 0);
728 case LIV_EXCEP: /* receive exception */
729 case LIV_RXDATA: /* receive interrupt */
732 case M_ASYNC: eoi = cxrinta (c); break;
733 case M_HDLC: cxrinth (c); break;
734 default:; /* No bisync and X.21 yet */
737 case LIV_TXDATA: /* transmit interrupt */
739 eoiport = TEOIR(port);
741 case M_ASYNC: cxtinta (c); break;
742 case M_HDLC: eoi = cxtinth (c); break;
743 default:; /* No bisync and X.21 yet */
746 case LIV_MODEM: /* modem/timer interrupt */
748 eoiport = MEOIR(port);
753 /* Raise RTS for this channel if and only if
754 * both receive buffers are empty. */
755 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
756 (inb (ARBSTS(port)) & BSTS_OWN24) &&
757 (inb (BRBSTS(port)) & BSTS_OWN24)) {
758 outb (MSVR_RTS(port), MSV_RTS);
762 /* Exit from interrupt context. */
765 /* Master channel - start output on all idle subchannels. */
766 if (c->master == c->ifp && c->slaveq &&
767 (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
768 ! sppp_isempty (c->ifp)) {
771 for (q=c->slaveq; q; q=q->slaveq)
772 if ((q->ifp->if_flags & IFF_RUNNING) &&
773 ! (q->ifp->if_flags & IFF_OACTIVE))
780 * Process the received packet.
783 cxinput (cx_chan_t *c, void *buf, unsigned len)
786 struct mbuf *m = makembuf (buf, len);
788 if (c->ifp->if_flags & IFF_DEBUG)
789 printf ("cx%d.%d: no memory for packet\n",
790 c->board->num, c->num);
791 ++c->ifp->if_iqdrops;
794 m->m_pkthdr.rcvif = c->master;
796 if (c->ifp->if_flags & IFF_DEBUG)
801 * Check if there's a BPF listener on this interface.
802 * If so, hand off the raw packet to bpf.
805 bpf_tap (c->ifp, buf, len);
807 /* Count the received bytes to the subchannel, not the master. */
808 c->master->if_ibytes -= len + 3;
809 c->ifp->if_ibytes += len + 3;
811 sppp_input (c->master, m);
814 void cxswitch (cx_chan_t *c, cx_soft_opt_t new)
818 struct sppp *sp = (struct sppp*) c->ifp;
820 #if 0 /* Doesn't work this way any more 990402 /phk */
822 sp->pp_flags |= PP_CISCO;
824 sp->pp_flags &= ~PP_CISCO;
827 sp->pp_flags |= PP_KEEPALIVE;
829 sp->pp_flags &= ~PP_KEEPALIVE;