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.13 2004/06/02 14:42:50 eirikn 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, MB_DONTWAIT, MT_DATA);
149 if (len >= MINCLSIZE)
150 MCLGET (m, MB_DONTWAIT);
151 m->m_pkthdr.len = len;
156 unsigned n = M_TRAILINGSPACE (p);
161 /* Allocate new mbuf. */
163 MGET (p, MB_DONTWAIT, MT_DATA);
168 if (len >= MINCLSIZE)
169 MCLGET (p, MB_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 cdevsw_add(&cx_cdevsw, -1, unit);
298 make_dev(&cx_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
302 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
305 * Process an ioctl request.
308 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
310 cx_chan_t *q, *c = ifp->if_softc;
311 int error, s, was_up, should_be_up;
314 * No socket ioctls while the channel is in async mode.
316 if (c->type==T_NONE || c->mode==M_ASYNC)
320 * Socket ioctls on slave subchannels are not allowed.
322 if (c->master != c->ifp)
325 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
326 error = sppp_ioctl (ifp, cmd, data);
330 print (("cxioctl (%d.%d, ", c->board->num, c->num));
333 print (("0x%x)\n", cmd));
336 print (("SIOCADDMULTI)\n"));
339 print (("SIOCDELMULTI)\n"));
342 print (("SIOCSIFFLAGS)\n"));
345 print (("SIOCSIFADDR)\n"));
349 /* We get here only in case of SIFFLAGS or SIFADDR. */
351 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
352 if (!was_up && should_be_up) {
353 /* Interface goes up -- start it. */
356 /* Start all slave subchannels. */
357 for (q=c->slaveq; q; q=q->slaveq)
361 } else if (was_up && !should_be_up) {
362 /* Interface is going down -- stop it. */
365 /* Stop all slave subchannels. */
366 for (q=c->slaveq; q; q=q->slaveq)
369 /* Flush the interface output queue */
378 * Stop the interface. Called on splimp().
381 cxdown (cx_chan_t *c)
383 unsigned short port = c->chip->port;
385 print (("cx%d.%d: cxdown\n", c->board->num, c->num));
387 /* The interface is down, stop it */
388 c->ifp->if_flags &= ~IFF_OACTIVE;
390 /* Reset the channel (for sync modes only) */
391 outb (CAR(port), c->num & 3);
392 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
398 * Start the interface. Called on splimp().
403 unsigned short port = c->chip->port;
405 /* The interface is up, start it */
406 print (("cx%d.%d: cxup\n", c->board->num, c->num));
408 /* Initialize channel, enable receiver and transmitter */
409 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
410 /* Repeat the command, to avoid the rev.H bug */
411 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
414 outw (ARBCNT(port), DMABUFSZ);
415 outb (ARBSTS(port), BSTS_OWN24);
416 outw (BRBCNT(port), DMABUFSZ);
417 outb (BRBSTS(port), BSTS_OWN24);
419 /* Raise DTR and RTS */
423 /* Enable interrupts */
424 outb (IER(port), IER_RXD | IER_TXD);
428 * Fill transmitter buffer with data.
431 cxput (cx_chan_t *c, char b)
435 unsigned short port = c->chip->port, len, cnt_port, sts_port;
437 /* Choose the buffer. */
440 cnt_port = ATBCNT(port);
441 sts_port = ATBSTS(port);
444 cnt_port = BTBCNT(port);
445 sts_port = BTBSTS(port);
449 if (inb (sts_port) & BSTS_OWN24) {
450 if (c->ifp->if_flags & IFF_DEBUG)
451 print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
452 c->board->num, c->num, b,
453 inb (sts_port), BSTS_BITS));
457 /* Get the packet to send. */
458 m = sppp_dequeue (c->master);
461 len = m->m_pkthdr.len;
463 /* Count the transmitted bytes to the subchannel, not the master. */
464 c->master->if_obytes -= len + 3;
465 c->ifp->if_obytes += len + 3;
466 c->stat->obytes += len + 3;
468 if (len >= DMABUFSZ) {
469 printf ("cx%d.%d: too long packet: %d bytes: ",
470 c->board->num, c->num, len);
475 m_copydata (m, 0, len, buf);
477 bpf_mtap (c->ifp, m);
480 /* Start transmitter. */
481 outw (cnt_port, len);
482 outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
484 if (c->ifp->if_flags & IFF_DEBUG)
485 print (("cx%d.%d: enqueue %d bytes to %c\n",
486 c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
488 c->ifp->if_flags |= IFF_OACTIVE;
492 * Start output on the (slave) interface. Get another datagram to send
493 * off of the interface queue, and copy it to the interface
494 * before starting the output.
497 cxsend (cx_chan_t *c)
499 unsigned short port = c->chip->port;
501 if (c->ifp->if_flags & IFF_DEBUG)
502 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
504 /* No output if the interface is down. */
505 if (! (c->ifp->if_flags & IFF_RUNNING))
508 /* Set the current channel number. */
509 outb (CAR(port), c->num & 3);
511 /* Determine the buffer order. */
512 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
520 /* Set up transmit timeout. */
521 if (c->master->if_flags & IFF_OACTIVE)
522 c->master->if_timer = TXTIMEOUT;
525 * Enable TXMPTY interrupt,
526 * to catch the case when the second buffer is empty.
528 if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
529 (inb (BTBSTS(port)) & BSTS_OWN24)) {
530 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
532 outb (IER(port), IER_RXD | IER_TXD);
536 * Start output on the (master) interface and all slave interfaces.
537 * Always called on splimp().
540 cxstart (struct ifnet *ifp)
542 cx_chan_t *q, *c = ifp->if_softc;
544 if (c->ifp->if_flags & IFF_DEBUG)
545 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
547 /* Start the master subchannel. */
550 /* Start all slave subchannels. */
551 if (c->slaveq && ! sppp_isempty (c->master))
552 for (q=c->slaveq; q; q=q->slaveq)
553 if ((q->ifp->if_flags & IFF_RUNNING) &&
554 ! (q->ifp->if_flags & IFF_OACTIVE))
559 * Handle transmit timeouts.
560 * Recover after lost transmit interrupts.
561 * Always called on splimp().
564 cxwatchdog (struct ifnet *ifp)
566 cx_chan_t *q, *c = ifp->if_softc;
568 if (! (ifp->if_flags & IFF_RUNNING))
570 if (ifp->if_flags & IFF_DEBUG)
571 printf ("cx%d.%d: device timeout\n", c->board->num, c->num);
574 for (q=c->slaveq; q; q=q->slaveq)
578 for (q=c->slaveq; q; q=q->slaveq)
585 * Handle receive interrupts, including receive errors and
586 * receive timeout interrupt.
589 cxrinth (cx_chan_t *c)
591 unsigned short port = c->chip->port;
592 unsigned short len, risr = inw (RISR(port));
594 /* Receive errors. */
595 if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
596 if (c->ifp->if_flags & IFF_DEBUG)
597 printf ("cx%d.%d: receive error, risr=%b\n",
598 c->board->num, c->num, risr, RISH_BITS);
599 ++c->ifp->if_ierrors;
601 if (risr & RIS_OVERRUN)
602 ++c->ifp->if_collisions;
603 } else if (risr & RIS_EOBUF) {
604 if (c->ifp->if_flags & IFF_DEBUG)
605 print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
606 c->board->num, c->num, risr, RISH_BITS,
607 inb (ARBSTS(port)), BSTS_BITS,
608 inb (BRBSTS(port)), BSTS_BITS));
611 /* Handle received data. */
612 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
613 c->stat->ibytes += len;
614 if (len > DMABUFSZ) {
615 /* Fatal error: actual DMA transfer size
616 * exceeds our buffer size. It could be caused
617 * by incorrectly programmed DMA register or
618 * hardware fault. Possibly, should panic here. */
619 printf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
620 c->board->num, c->num, len);
621 ++c->ifp->if_ierrors;
622 } else if (! (risr & RIS_EOFR)) {
623 /* The received frame does not fit in the DMA buffer.
624 * It could be caused by serial lie noise,
625 * or if the peer has too big MTU. */
626 if (c->ifp->if_flags & IFF_DEBUG)
627 printf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
628 c->board->num, c->num, risr, RISH_BITS);
629 ++c->ifp->if_ierrors;
631 /* Valid frame received. */
632 if (c->ifp->if_flags & IFF_DEBUG)
633 print (("cx%d.%d: hdlc received %d bytes\n",
634 c->board->num, c->num, len));
635 cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
636 ++c->ifp->if_ipackets;
638 } else if (c->ifp->if_flags & IFF_DEBUG) {
639 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
640 c->board->num, c->num, risr, RISH_BITS));
644 /* Restart receiver. */
645 if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
646 outw (ARBCNT(port), DMABUFSZ);
647 outb (ARBSTS(port), BSTS_OWN24);
649 if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
650 outw (BRBCNT(port), DMABUFSZ);
651 outb (BRBSTS(port), BSTS_OWN24);
656 * Handle transmit interrupt.
659 cxtinth (cx_chan_t *c)
661 unsigned short port = c->chip->port;
662 unsigned char tisr = inb (TISR(port));
663 unsigned char teoir = 0;
665 c->ifp->if_flags &= ~IFF_OACTIVE;
666 if (c->ifp == c->master)
667 c->ifp->if_timer = 0;
669 if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
670 /* if (c->ifp->if_flags & IFF_DEBUG) */
671 print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
672 c->board->num, c->num, tisr, TIS_BITS,
673 inb (ATBSTS(port)), BSTS_BITS,
674 inb (BTBSTS(port)), BSTS_BITS));
675 ++c->ifp->if_oerrors;
678 /* Terminate the failed buffer. */
679 /* teoir = TEOI_TERMBUFF; */
680 } else if (c->ifp->if_flags & IFF_DEBUG)
681 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
682 c->board->num, c->num, tisr, TIS_BITS,
683 inb (ATBSTS(port)), BSTS_BITS,
684 inb (BTBSTS(port)), BSTS_BITS));
686 if (tisr & TIS_EOFR) {
687 ++c->ifp->if_opackets;
691 /* Start output on the (sub-) channel. */
700 cx_board_t *b = cxboard + bnum;
701 while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
702 /* Acknowledge the interrupt to enter the interrupt context. */
703 /* Read the local interrupt vector register. */
704 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
705 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
706 unsigned short port = c->chip->port;
707 unsigned short eoiport = REOIR(port);
708 unsigned char eoi = 0;
710 if (c->type == T_NONE) {
711 printf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
712 c->board->num, c->num, livr);
713 continue; /* incorrect channel number? */
715 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
716 c->board->num, c->num, livr)); */
718 /* Clear RTS to stop receiver data flow while we are busy
719 * processing the interrupt, thus avoiding underruns. */
720 if (! c->sopt.norts) {
721 outb (MSVR_RTS(port), 0);
726 case LIV_EXCEP: /* receive exception */
727 case LIV_RXDATA: /* receive interrupt */
730 case M_ASYNC: eoi = cxrinta (c); break;
731 case M_HDLC: cxrinth (c); break;
732 default:; /* No bisync and X.21 yet */
735 case LIV_TXDATA: /* transmit interrupt */
737 eoiport = TEOIR(port);
739 case M_ASYNC: cxtinta (c); break;
740 case M_HDLC: eoi = cxtinth (c); break;
741 default:; /* No bisync and X.21 yet */
744 case LIV_MODEM: /* modem/timer interrupt */
746 eoiport = MEOIR(port);
751 /* Raise RTS for this channel if and only if
752 * both receive buffers are empty. */
753 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
754 (inb (ARBSTS(port)) & BSTS_OWN24) &&
755 (inb (BRBSTS(port)) & BSTS_OWN24)) {
756 outb (MSVR_RTS(port), MSV_RTS);
760 /* Exit from interrupt context. */
763 /* Master channel - start output on all idle subchannels. */
764 if (c->master == c->ifp && c->slaveq &&
765 (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
766 ! sppp_isempty (c->ifp)) {
769 for (q=c->slaveq; q; q=q->slaveq)
770 if ((q->ifp->if_flags & IFF_RUNNING) &&
771 ! (q->ifp->if_flags & IFF_OACTIVE))
778 * Process the received packet.
781 cxinput (cx_chan_t *c, void *buf, unsigned len)
784 struct mbuf *m = makembuf (buf, len);
786 if (c->ifp->if_flags & IFF_DEBUG)
787 printf ("cx%d.%d: no memory for packet\n",
788 c->board->num, c->num);
789 ++c->ifp->if_iqdrops;
792 m->m_pkthdr.rcvif = c->master;
794 if (c->ifp->if_flags & IFF_DEBUG)
799 * Check if there's a BPF listener on this interface.
800 * If so, hand off the raw packet to bpf.
803 bpf_tap (c->ifp, buf, len);
805 /* Count the received bytes to the subchannel, not the master. */
806 c->master->if_ibytes -= len + 3;
807 c->ifp->if_ibytes += len + 3;
809 sppp_input (c->master, m);
812 void cxswitch (cx_chan_t *c, cx_soft_opt_t new)
816 struct sppp *sp = (struct sppp*) c->ifp;
818 #if 0 /* Doesn't work this way any more 990402 /phk */
820 sp->pp_flags |= PP_CISCO;
822 sp->pp_flags &= ~PP_CISCO;
825 sp->pp_flags |= PP_KEEPALIVE;
827 sp->pp_flags &= ~PP_KEEPALIVE;