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.15 2005/01/23 20:21:30 joerg 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);
481 /* Start transmitter. */
482 outw (cnt_port, len);
483 outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
485 if (c->ifp->if_flags & IFF_DEBUG)
486 print (("cx%d.%d: enqueue %d bytes to %c\n",
487 c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
489 c->ifp->if_flags |= IFF_OACTIVE;
493 * Start output on the (slave) interface. Get another datagram to send
494 * off of the interface queue, and copy it to the interface
495 * before starting the output.
498 cxsend (cx_chan_t *c)
500 unsigned short port = c->chip->port;
502 if (c->ifp->if_flags & IFF_DEBUG)
503 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
505 /* No output if the interface is down. */
506 if (! (c->ifp->if_flags & IFF_RUNNING))
509 /* Set the current channel number. */
510 outb (CAR(port), c->num & 3);
512 /* Determine the buffer order. */
513 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
521 /* Set up transmit timeout. */
522 if (c->master->if_flags & IFF_OACTIVE)
523 c->master->if_timer = TXTIMEOUT;
526 * Enable TXMPTY interrupt,
527 * to catch the case when the second buffer is empty.
529 if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
530 (inb (BTBSTS(port)) & BSTS_OWN24)) {
531 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
533 outb (IER(port), IER_RXD | IER_TXD);
537 * Start output on the (master) interface and all slave interfaces.
538 * Always called on splimp().
541 cxstart (struct ifnet *ifp)
543 cx_chan_t *q, *c = ifp->if_softc;
545 if (c->ifp->if_flags & IFF_DEBUG)
546 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
548 /* Start the master subchannel. */
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))
560 * Handle transmit timeouts.
561 * Recover after lost transmit interrupts.
562 * Always called on splimp().
565 cxwatchdog (struct ifnet *ifp)
567 cx_chan_t *q, *c = ifp->if_softc;
569 if (! (ifp->if_flags & IFF_RUNNING))
571 if (ifp->if_flags & IFF_DEBUG)
572 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
575 for (q=c->slaveq; q; q=q->slaveq)
579 for (q=c->slaveq; q; q=q->slaveq)
586 * Handle receive interrupts, including receive errors and
587 * receive timeout interrupt.
590 cxrinth (cx_chan_t *c)
592 unsigned short port = c->chip->port;
593 unsigned short len, risr = inw (RISR(port));
595 /* Receive errors. */
596 if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
597 if (c->ifp->if_flags & IFF_DEBUG)
598 printf ("cx%d.%d: receive error, risr=%b\n",
599 c->board->num, c->num, risr, RISH_BITS);
600 ++c->ifp->if_ierrors;
602 if (risr & RIS_OVERRUN)
603 ++c->ifp->if_collisions;
604 } else if (risr & RIS_EOBUF) {
605 if (c->ifp->if_flags & IFF_DEBUG)
606 print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
607 c->board->num, c->num, risr, RISH_BITS,
608 inb (ARBSTS(port)), BSTS_BITS,
609 inb (BRBSTS(port)), BSTS_BITS));
612 /* Handle received data. */
613 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
614 c->stat->ibytes += len;
615 if (len > DMABUFSZ) {
616 /* Fatal error: actual DMA transfer size
617 * exceeds our buffer size. It could be caused
618 * by incorrectly programmed DMA register or
619 * hardware fault. Possibly, should panic here. */
620 printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
621 c->board->num, c->num, len);
622 ++c->ifp->if_ierrors;
623 } else if (! (risr & RIS_EOFR)) {
624 /* The received frame does not fit in the DMA buffer.
625 * It could be caused by serial lie noise,
626 * or if the peer has too big MTU. */
627 if (c->ifp->if_flags & IFF_DEBUG)
628 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
629 c->board->num, c->num, risr, RISH_BITS);
630 ++c->ifp->if_ierrors;
632 /* Valid frame received. */
633 if (c->ifp->if_flags & IFF_DEBUG)
634 print (("cx%d.%d: hdlc received %d bytes\n",
635 c->board->num, c->num, len));
636 cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
637 ++c->ifp->if_ipackets;
639 } else if (c->ifp->if_flags & IFF_DEBUG) {
640 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
641 c->board->num, c->num, risr, RISH_BITS));
645 /* Restart receiver. */
646 if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
647 outw (ARBCNT(port), DMABUFSZ);
648 outb (ARBSTS(port), BSTS_OWN24);
650 if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
651 outw (BRBCNT(port), DMABUFSZ);
652 outb (BRBSTS(port), BSTS_OWN24);
657 * Handle transmit interrupt.
660 cxtinth (cx_chan_t *c)
662 unsigned short port = c->chip->port;
663 unsigned char tisr = inb (TISR(port));
664 unsigned char teoir = 0;
666 c->ifp->if_flags &= ~IFF_OACTIVE;
667 if (c->ifp == c->master)
668 c->ifp->if_timer = 0;
670 if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
671 /* if (c->ifp->if_flags & IFF_DEBUG) */
672 print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
673 c->board->num, c->num, tisr, TIS_BITS,
674 inb (ATBSTS(port)), BSTS_BITS,
675 inb (BTBSTS(port)), BSTS_BITS));
676 ++c->ifp->if_oerrors;
679 /* Terminate the failed buffer. */
680 /* teoir = TEOI_TERMBUFF; */
681 } else if (c->ifp->if_flags & IFF_DEBUG)
682 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
683 c->board->num, c->num, tisr, TIS_BITS,
684 inb (ATBSTS(port)), BSTS_BITS,
685 inb (BTBSTS(port)), BSTS_BITS));
687 if (tisr & TIS_EOFR) {
688 ++c->ifp->if_opackets;
692 /* Start output on the (sub-) channel. */
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;
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? */
716 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
717 c->board->num, c->num, livr)); */
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);
727 case LIV_EXCEP: /* receive exception */
728 case LIV_RXDATA: /* receive interrupt */
731 case M_ASYNC: eoi = cxrinta (c); break;
732 case M_HDLC: cxrinth (c); break;
733 default:; /* No bisync and X.21 yet */
736 case LIV_TXDATA: /* transmit interrupt */
738 eoiport = TEOIR(port);
740 case M_ASYNC: cxtinta (c); break;
741 case M_HDLC: eoi = cxtinth (c); break;
742 default:; /* No bisync and X.21 yet */
745 case LIV_MODEM: /* modem/timer interrupt */
747 eoiport = MEOIR(port);
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);
761 /* Exit from interrupt context. */
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)) {
770 for (q=c->slaveq; q; q=q->slaveq)
771 if ((q->ifp->if_flags & IFF_RUNNING) &&
772 ! (q->ifp->if_flags & IFF_OACTIVE))
779 * Process the received packet.
782 cxinput (cx_chan_t *c, void *buf, unsigned len)
785 struct mbuf *m = makembuf (buf, len);
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;
793 m->m_pkthdr.rcvif = c->master;
795 if (c->ifp->if_flags & IFF_DEBUG)
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;