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.11 2004/05/04 12:12:13 hmp 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,
110 DECLARE_DUMMY_MODULE(if_cx);
113 * Check that the value is contained in the list of correct values.
115 static int valid (unsigned short value, unsigned short *list)
118 if (value == *list++)
124 * Print the mbuf chain, for debug purposes only.
126 static void printmbuf (struct mbuf *m)
129 for (; m; m=m->m_next) {
130 if (m->m_flags & M_PKTHDR)
131 printf (" HDR %d:", m->m_pkthdr.len);
132 if (m->m_flags & M_EXT)
134 printf (" %d", m->m_len);
140 * Make an mbuf from data.
142 static struct mbuf *makembuf (void *buf, unsigned len)
144 struct mbuf *m, *o, *p;
146 MGETHDR (m, M_DONTWAIT, MT_DATA);
149 if (len >= MINCLSIZE)
150 MCLGET (m, M_DONTWAIT);
151 m->m_pkthdr.len = len;
156 unsigned n = M_TRAILINGSPACE (p);
161 /* Allocate new mbuf. */
163 MGET (p, M_DONTWAIT, MT_DATA);
168 if (len >= MINCLSIZE)
169 MCLGET (p, M_DONTWAIT);
173 n = M_TRAILINGSPACE (p);
178 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
181 buf = (char *)buf + n;
188 * Test the presence of the adapter on the given i/o port.
191 cxprobe (struct isa_device *id)
193 int unit = id->id_unit;
194 int iobase = id->id_iobase;
195 int irq = id->id_irq;
196 int drq = id->id_drq;
198 irqnum = ffs (irq) - 1;
200 print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",
201 unit, iobase, irqnum, drq));
202 if (! valid (irqnum, irq_valid_values)) {
203 printf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);
206 if (! valid (iobase, port_valid_values)) {
207 printf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);
210 if (! valid (drq, drq_valid_values)) {
211 printf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);
214 if (! cx_probe_board (iobase))
221 * The adapter is present, initialize the driver structures.
225 cxattach (struct isa_device *id)
227 int unit = id->id_unit;
228 int iobase = id->id_iobase;
229 int irq = id->id_irq;
230 int drq = id->id_drq;
231 cx_board_t *b = cxboard + unit;
235 id->id_ointr = cxintr;
237 /* Initialize the board structure. */
238 cx_init (b, unit, iobase, ffs(irq)-1, drq);
240 for (i=0; i<NCHAN; ++i) {
241 cx_chan_t *c = b->chan + i;
242 int u = b->num*NCHAN + i;
245 if (c->type == T_NONE)
248 /* Allocate the buffer memory. */
249 c->arbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
250 c->brbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
251 c->atbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
252 c->btbuf = malloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
255 /* All buffers should be located in lower 16M of memory! */
256 /* XXX all buffers located where? I don't think so! */
257 if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {
258 printf ("cx%d.%d: No memory for channel buffers\n",
259 c->board->num, c->num);
271 c->ifp = malloc (IFSTRUCTSZ, M_DEVBUF, M_WAITOK | M_ZERO);
273 c->ifp->if_softc = c;
274 if_initname(c->ifp, "cx", u);
275 c->ifp->if_mtu = PP_MTU;
276 c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
277 c->ifp->if_ioctl = cxsioctl;
278 c->ifp->if_start = (start_func_t) cxstart;
279 c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;
280 /* Init routine is never called by upper level? */
281 sppp_attach (c->ifp);
283 sp = (struct sppp*) c->ifp;
284 /* If BPF is in the kernel, call the attach for it. */
285 bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);
289 /* Reset the adapter. */
292 /* Activate the timeout routine. */
294 timeout (cxtimeout, 0, hz*5);
296 printf ("cx%d: <Cronyx-%s>\n", unit, b->name);
297 make_dev(&cx_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
301 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
304 * Process an ioctl request.
307 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
309 cx_chan_t *q, *c = ifp->if_softc;
310 int error, s, was_up, should_be_up;
313 * No socket ioctls while the channel is in async mode.
315 if (c->type==T_NONE || c->mode==M_ASYNC)
319 * Socket ioctls on slave subchannels are not allowed.
321 if (c->master != c->ifp)
324 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
325 error = sppp_ioctl (ifp, cmd, data);
329 print (("cxioctl (%d.%d, ", c->board->num, c->num));
332 print (("0x%x)\n", cmd));
335 print (("SIOCADDMULTI)\n"));
338 print (("SIOCDELMULTI)\n"));
341 print (("SIOCSIFFLAGS)\n"));
344 print (("SIOCSIFADDR)\n"));
348 /* We get here only in case of SIFFLAGS or SIFADDR. */
350 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
351 if (!was_up && should_be_up) {
352 /* Interface goes up -- start it. */
355 /* Start all slave subchannels. */
356 for (q=c->slaveq; q; q=q->slaveq)
360 } else if (was_up && !should_be_up) {
361 /* Interface is going down -- stop it. */
364 /* Stop all slave subchannels. */
365 for (q=c->slaveq; q; q=q->slaveq)
368 /* Flush the interface output queue */
377 * Stop the interface. Called on splimp().
380 cxdown (cx_chan_t *c)
382 unsigned short port = c->chip->port;
384 print (("cx%d.%d: cxdown\n", c->board->num, c->num));
386 /* The interface is down, stop it */
387 c->ifp->if_flags &= ~IFF_OACTIVE;
389 /* Reset the channel (for sync modes only) */
390 outb (CAR(port), c->num & 3);
391 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
397 * Start the interface. Called on splimp().
402 unsigned short port = c->chip->port;
404 /* The interface is up, start it */
405 print (("cx%d.%d: cxup\n", c->board->num, c->num));
407 /* Initialize channel, enable receiver and transmitter */
408 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
409 /* Repeat the command, to avoid the rev.H bug */
410 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
413 outw (ARBCNT(port), DMABUFSZ);
414 outb (ARBSTS(port), BSTS_OWN24);
415 outw (BRBCNT(port), DMABUFSZ);
416 outb (BRBSTS(port), BSTS_OWN24);
418 /* Raise DTR and RTS */
422 /* Enable interrupts */
423 outb (IER(port), IER_RXD | IER_TXD);
427 * Fill transmitter buffer with data.
430 cxput (cx_chan_t *c, char b)
434 unsigned short port = c->chip->port, len, cnt_port, sts_port;
436 /* Choose the buffer. */
439 cnt_port = ATBCNT(port);
440 sts_port = ATBSTS(port);
443 cnt_port = BTBCNT(port);
444 sts_port = BTBSTS(port);
448 if (inb (sts_port) & BSTS_OWN24) {
449 if (c->ifp->if_flags & IFF_DEBUG)
450 print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
451 c->board->num, c->num, b,
452 inb (sts_port), BSTS_BITS));
456 /* Get the packet to send. */
457 m = sppp_dequeue (c->master);
460 len = m->m_pkthdr.len;
462 /* Count the transmitted bytes to the subchannel, not the master. */
463 c->master->if_obytes -= len + 3;
464 c->ifp->if_obytes += len + 3;
465 c->stat->obytes += len + 3;
467 if (len >= DMABUFSZ) {
468 printf ("cx%d.%d: too long packet: %d bytes: ",
469 c->board->num, c->num, len);
474 m_copydata (m, 0, len, buf);
476 bpf_mtap (c->ifp, m);
479 /* Start transmitter. */
480 outw (cnt_port, len);
481 outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
483 if (c->ifp->if_flags & IFF_DEBUG)
484 print (("cx%d.%d: enqueue %d bytes to %c\n",
485 c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
487 c->ifp->if_flags |= IFF_OACTIVE;
491 * Start output on the (slave) interface. Get another datagram to send
492 * off of the interface queue, and copy it to the interface
493 * before starting the output.
496 cxsend (cx_chan_t *c)
498 unsigned short port = c->chip->port;
500 if (c->ifp->if_flags & IFF_DEBUG)
501 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
503 /* No output if the interface is down. */
504 if (! (c->ifp->if_flags & IFF_RUNNING))
507 /* Set the current channel number. */
508 outb (CAR(port), c->num & 3);
510 /* Determine the buffer order. */
511 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
519 /* Set up transmit timeout. */
520 if (c->master->if_flags & IFF_OACTIVE)
521 c->master->if_timer = TXTIMEOUT;
524 * Enable TXMPTY interrupt,
525 * to catch the case when the second buffer is empty.
527 if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
528 (inb (BTBSTS(port)) & BSTS_OWN24)) {
529 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
531 outb (IER(port), IER_RXD | IER_TXD);
535 * Start output on the (master) interface and all slave interfaces.
536 * Always called on splimp().
539 cxstart (struct ifnet *ifp)
541 cx_chan_t *q, *c = ifp->if_softc;
543 if (c->ifp->if_flags & IFF_DEBUG)
544 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
546 /* Start the master subchannel. */
549 /* Start all slave subchannels. */
550 if (c->slaveq && ! sppp_isempty (c->master))
551 for (q=c->slaveq; q; q=q->slaveq)
552 if ((q->ifp->if_flags & IFF_RUNNING) &&
553 ! (q->ifp->if_flags & IFF_OACTIVE))
558 * Handle transmit timeouts.
559 * Recover after lost transmit interrupts.
560 * Always called on splimp().
563 cxwatchdog (struct ifnet *ifp)
565 cx_chan_t *q, *c = ifp->if_softc;
567 if (! (ifp->if_flags & IFF_RUNNING))
569 if (ifp->if_flags & IFF_DEBUG)
570 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
573 for (q=c->slaveq; q; q=q->slaveq)
577 for (q=c->slaveq; q; q=q->slaveq)
584 * Handle receive interrupts, including receive errors and
585 * receive timeout interrupt.
588 cxrinth (cx_chan_t *c)
590 unsigned short port = c->chip->port;
591 unsigned short len, risr = inw (RISR(port));
593 /* Receive errors. */
594 if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
595 if (c->ifp->if_flags & IFF_DEBUG)
596 printf ("cx%d.%d: receive error, risr=%b\n",
597 c->board->num, c->num, risr, RISH_BITS);
598 ++c->ifp->if_ierrors;
600 if (risr & RIS_OVERRUN)
601 ++c->ifp->if_collisions;
602 } else if (risr & RIS_EOBUF) {
603 if (c->ifp->if_flags & IFF_DEBUG)
604 print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
605 c->board->num, c->num, risr, RISH_BITS,
606 inb (ARBSTS(port)), BSTS_BITS,
607 inb (BRBSTS(port)), BSTS_BITS));
610 /* Handle received data. */
611 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
612 c->stat->ibytes += len;
613 if (len > DMABUFSZ) {
614 /* Fatal error: actual DMA transfer size
615 * exceeds our buffer size. It could be caused
616 * by incorrectly programmed DMA register or
617 * hardware fault. Possibly, should panic here. */
618 printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
619 c->board->num, c->num, len);
620 ++c->ifp->if_ierrors;
621 } else if (! (risr & RIS_EOFR)) {
622 /* The received frame does not fit in the DMA buffer.
623 * It could be caused by serial lie noise,
624 * or if the peer has too big MTU. */
625 if (c->ifp->if_flags & IFF_DEBUG)
626 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
627 c->board->num, c->num, risr, RISH_BITS);
628 ++c->ifp->if_ierrors;
630 /* Valid frame received. */
631 if (c->ifp->if_flags & IFF_DEBUG)
632 print (("cx%d.%d: hdlc received %d bytes\n",
633 c->board->num, c->num, len));
634 cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
635 ++c->ifp->if_ipackets;
637 } else if (c->ifp->if_flags & IFF_DEBUG) {
638 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
639 c->board->num, c->num, risr, RISH_BITS));
643 /* Restart receiver. */
644 if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
645 outw (ARBCNT(port), DMABUFSZ);
646 outb (ARBSTS(port), BSTS_OWN24);
648 if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
649 outw (BRBCNT(port), DMABUFSZ);
650 outb (BRBSTS(port), BSTS_OWN24);
655 * Handle transmit interrupt.
658 cxtinth (cx_chan_t *c)
660 unsigned short port = c->chip->port;
661 unsigned char tisr = inb (TISR(port));
662 unsigned char teoir = 0;
664 c->ifp->if_flags &= ~IFF_OACTIVE;
665 if (c->ifp == c->master)
666 c->ifp->if_timer = 0;
668 if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
669 /* if (c->ifp->if_flags & IFF_DEBUG) */
670 print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
671 c->board->num, c->num, tisr, TIS_BITS,
672 inb (ATBSTS(port)), BSTS_BITS,
673 inb (BTBSTS(port)), BSTS_BITS));
674 ++c->ifp->if_oerrors;
677 /* Terminate the failed buffer. */
678 /* teoir = TEOI_TERMBUFF; */
679 } else if (c->ifp->if_flags & IFF_DEBUG)
680 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
681 c->board->num, c->num, tisr, TIS_BITS,
682 inb (ATBSTS(port)), BSTS_BITS,
683 inb (BTBSTS(port)), BSTS_BITS));
685 if (tisr & TIS_EOFR) {
686 ++c->ifp->if_opackets;
690 /* Start output on the (sub-) channel. */
699 cx_board_t *b = cxboard + bnum;
700 while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
701 /* Acknowledge the interrupt to enter the interrupt context. */
702 /* Read the local interrupt vector register. */
703 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
704 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
705 unsigned short port = c->chip->port;
706 unsigned short eoiport = REOIR(port);
707 unsigned char eoi = 0;
709 if (c->type == T_NONE) {
710 printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
711 c->board->num, c->num, livr);
712 continue; /* incorrect channel number? */
714 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
715 c->board->num, c->num, livr)); */
717 /* Clear RTS to stop receiver data flow while we are busy
718 * processing the interrupt, thus avoiding underruns. */
719 if (! c->sopt.norts) {
720 outb (MSVR_RTS(port), 0);
725 case LIV_EXCEP: /* receive exception */
726 case LIV_RXDATA: /* receive interrupt */
729 case M_ASYNC: eoi = cxrinta (c); break;
730 case M_HDLC: cxrinth (c); break;
731 default:; /* No bisync and X.21 yet */
734 case LIV_TXDATA: /* transmit interrupt */
736 eoiport = TEOIR(port);
738 case M_ASYNC: cxtinta (c); break;
739 case M_HDLC: eoi = cxtinth (c); break;
740 default:; /* No bisync and X.21 yet */
743 case LIV_MODEM: /* modem/timer interrupt */
745 eoiport = MEOIR(port);
750 /* Raise RTS for this channel if and only if
751 * both receive buffers are empty. */
752 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
753 (inb (ARBSTS(port)) & BSTS_OWN24) &&
754 (inb (BRBSTS(port)) & BSTS_OWN24)) {
755 outb (MSVR_RTS(port), MSV_RTS);
759 /* Exit from interrupt context. */
762 /* Master channel - start output on all idle subchannels. */
763 if (c->master == c->ifp && c->slaveq &&
764 (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
765 ! sppp_isempty (c->ifp)) {
768 for (q=c->slaveq; q; q=q->slaveq)
769 if ((q->ifp->if_flags & IFF_RUNNING) &&
770 ! (q->ifp->if_flags & IFF_OACTIVE))
777 * Process the received packet.
780 cxinput (cx_chan_t *c, void *buf, unsigned len)
783 struct mbuf *m = makembuf (buf, len);
785 if (c->ifp->if_flags & IFF_DEBUG)
786 printf ("cx%d.%d: no memory for packet\n",
787 c->board->num, c->num);
788 ++c->ifp->if_iqdrops;
791 m->m_pkthdr.rcvif = c->master;
793 if (c->ifp->if_flags & IFF_DEBUG)
798 * Check if there's a BPF listener on this interface.
799 * If so, hand off the raw packet to bpf.
802 bpf_tap (c->ifp, buf, len);
804 /* Count the received bytes to the subchannel, not the master. */
805 c->master->if_ibytes -= len + 3;
806 c->ifp->if_ibytes += len + 3;
808 sppp_input (c->master, m);
811 void cxswitch (cx_chan_t *c, cx_soft_opt_t new)
815 struct sppp *sp = (struct sppp*) c->ifp;
817 #if 0 /* Doesn't work this way any more 990402 /phk */
819 sp->pp_flags |= PP_CISCO;
821 sp->pp_flags &= ~PP_CISCO;
824 sp->pp_flags |= PP_KEEPALIVE;
826 sp->pp_flags &= ~PP_KEEPALIVE;