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.24 2008/08/02 01:14:42 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>
39 #include <sys/thread2.h>
45 #include <bus/isa/isa_device.h>
46 #define watchdog_func_t void(*)(struct ifnet *)
47 #define start_func_t void(*)(struct ifnet*)
49 #include <net/sppp/if_sppp.h>
50 #include <machine/cronyx.h>
56 void cxswitch (cx_chan_t *c, cx_soft_opt_t new);
59 static int cxprobe (struct isa_device *id);
60 static int cxattach (struct isa_device *id);
61 static void cxput (cx_chan_t *c, char b);
62 static void cxsend (cx_chan_t *c);
63 static void cxrinth (cx_chan_t *c);
64 static void cxintr(void *);
65 static int cxtinth (cx_chan_t *c);
68 # define print(s) kprintf s
70 # define print(s) {/*void*/}
73 #define TXTIMEOUT 10 /* transmit timeout in seconds */
74 #define DMABUFSZ (6*256) /* buffer size */
75 #define PPP_HEADER_LEN 4 /* size of PPP header */
78 * Under BSDI it's possible to use general p2p protocol scheme,
79 * as well as our own one. Switching is done via IFF_ALTPHYS flag.
80 * Our ifnet pointer holds the buffer large enough to contain
81 * any of sppp and p2p structures.
83 #define IFSTRUCTSZ (sizeof (struct sppp))
84 #define IFNETSZ (sizeof (struct ifnet))
86 static int cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
88 static void cxstart (struct ifnet *ifp);
89 static void cxwatchdog (struct ifnet *ifp);
90 static void cxinput (cx_chan_t *c, void *buf, unsigned len);
92 extern int cxrinta (cx_chan_t *c);
93 extern void cxtinta (cx_chan_t *c);
94 extern void cxmint (cx_chan_t *c);
95 extern timeout_t cxtimeout;
97 static void cxdown (cx_chan_t *c);
98 static void cxup (cx_chan_t *c);
100 cx_board_t cxboard [NCX]; /* adapter state structures */
101 cx_chan_t *cxchan [NCX*NCHAN]; /* unit to channel struct pointer */
102 static unsigned short irq_valid_values [] = { 3, 5, 7, 10, 11, 12, 15, 0 };
103 static unsigned short drq_valid_values [] = { 5, 6, 7, 0 };
104 static unsigned short port_valid_values [] = {
105 0x240, 0x260, 0x280, 0x300, 0x320, 0x380, 0x3a0, 0,
107 struct callout cxtimeout_ch;
109 DECLARE_DUMMY_MODULE(if_cx);
112 * Check that the value is contained in the list of correct values.
115 valid (unsigned short value, unsigned short *list)
118 if (value == *list++)
124 * Print the mbuf chain, for debug purposes only.
127 printmbuf (struct mbuf *m)
130 for (; m; m=m->m_next) {
131 if (m->m_flags & M_PKTHDR)
132 kprintf (" HDR %d:", m->m_pkthdr.len);
133 if (m->m_flags & M_EXT)
135 kprintf (" %d", m->m_len);
141 * Make an mbuf from data.
144 makembuf (void *buf, unsigned len)
146 struct mbuf *m, *o, *p;
148 MGETHDR (m, MB_DONTWAIT, MT_DATA);
151 if (len >= MINCLSIZE)
152 MCLGET (m, MB_DONTWAIT);
153 m->m_pkthdr.len = len;
158 unsigned n = M_TRAILINGSPACE (p);
163 /* Allocate new mbuf. */
165 MGET (p, MB_DONTWAIT, MT_DATA);
170 if (len >= MINCLSIZE)
171 MCLGET (p, MB_DONTWAIT);
175 n = M_TRAILINGSPACE (p);
180 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
183 buf = (char *)buf + n;
190 * Test the presence of the adapter on the given i/o port.
193 cxprobe (struct isa_device *id)
195 int unit = id->id_unit;
196 int iobase = id->id_iobase;
197 int irq = id->id_irq;
198 int drq = id->id_drq;
200 irqnum = ffs (irq) - 1;
202 print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",
203 unit, iobase, irqnum, drq));
204 if (! valid (irqnum, irq_valid_values)) {
205 kprintf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);
208 if (! valid (iobase, port_valid_values)) {
209 kprintf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);
212 if (! valid (drq, drq_valid_values)) {
213 kprintf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);
216 if (! cx_probe_board (iobase))
223 * The adapter is present, initialize the driver structures.
227 cxattach (struct isa_device *id)
229 int unit = id->id_unit;
230 int iobase = id->id_iobase;
231 int irq = id->id_irq;
232 int drq = id->id_drq;
233 cx_board_t *b = cxboard + unit;
237 id->id_intr = (inthand2_t *)cxintr;
239 /* Initialize the board structure. */
240 cx_init (b, unit, iobase, ffs(irq)-1, drq);
242 for (i=0; i<NCHAN; ++i) {
243 cx_chan_t *c = b->chan + i;
244 int u = b->num*NCHAN + i;
247 if (c->type == T_NONE)
250 /* Allocate the buffer memory. */
251 c->arbuf = kmalloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
252 c->brbuf = kmalloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
253 c->atbuf = kmalloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
254 c->btbuf = kmalloc (DMABUFSZ, M_DEVBUF, M_WAITOK);
257 /* All buffers should be located in lower 16M of memory! */
258 /* XXX all buffers located where? I don't think so! */
259 if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {
260 kprintf ("cx%d.%d: No memory for channel buffers\n",
261 c->board->num, c->num);
273 c->ifp = kmalloc (IFSTRUCTSZ, M_DEVBUF, M_WAITOK | M_ZERO);
275 c->ifp->if_softc = c;
276 if_initname(c->ifp, "cx", u);
277 c->ifp->if_mtu = PP_MTU;
278 c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
279 c->ifp->if_ioctl = cxsioctl;
280 c->ifp->if_start = (start_func_t) cxstart;
281 c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;
282 /* Init routine is never called by upper level? */
283 sppp_attach (c->ifp);
284 if_attach (c->ifp, NULL);
285 sp = (struct sppp*) c->ifp;
286 /* If BPF is in the kernel, call the attach for it. */
287 bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);
291 /* Reset the adapter. */
294 /* Activate the timeout routine. */
296 callout_init(&cxtimeout_ch);
297 callout_reset(&cxtimeout_ch, hz * 5, cxtimeout, NULL);
299 kprintf ("cx%d: <Cronyx-%s>\n", unit, b->name);
300 dev_ops_add(&cx_ops, -1, unit);
301 make_dev(&cx_ops, unit, UID_ROOT, GID_WHEEL, 0600, "cx%d", unit);
305 struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };
308 * Process an ioctl request.
311 cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
313 cx_chan_t *q, *c = ifp->if_softc;
314 int error, was_up, should_be_up;
317 * No socket ioctls while the channel is in async mode.
319 if (c->type==T_NONE || c->mode==M_ASYNC)
323 * Socket ioctls on slave subchannels are not allowed.
325 if (c->master != c->ifp)
328 was_up = (ifp->if_flags & IFF_RUNNING) != 0;
329 error = sppp_ioctl (ifp, cmd, data);
333 print (("cxioctl (%d.%d, ", c->board->num, c->num));
336 print (("0x%x)\n", cmd));
339 print (("SIOCADDMULTI)\n"));
342 print (("SIOCDELMULTI)\n"));
345 print (("SIOCSIFFLAGS)\n"));
348 print (("SIOCSIFADDR)\n"));
352 /* We get here only in case of SIFFLAGS or SIFADDR. */
355 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;
356 if (!was_up && should_be_up) {
357 /* Interface goes up -- start it. */
360 /* Start all slave subchannels. */
361 for (q=c->slaveq; q; q=q->slaveq)
365 } else if (was_up && !should_be_up) {
366 /* Interface is going down -- stop it. */
369 /* Stop all slave subchannels. */
370 for (q=c->slaveq; q; q=q->slaveq)
373 /* Flush the interface output queue */
384 * Stop the interface.
387 cxdown (cx_chan_t *c)
389 unsigned short port = c->chip->port;
391 print (("cx%d.%d: cxdown\n", c->board->num, c->num));
393 /* The interface is down, stop it */
394 c->ifp->if_flags &= ~IFF_OACTIVE;
396 /* Reset the channel (for sync modes only) */
397 outb (CAR(port), c->num & 3);
398 outb (STCR(port), STC_ABORTTX | STC_SNDSPC);
404 * Start the interface.
409 unsigned short port = c->chip->port;
411 /* The interface is up, start it */
412 print (("cx%d.%d: cxup\n", c->board->num, c->num));
414 /* Initialize channel, enable receiver and transmitter */
415 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
416 /* Repeat the command, to avoid the rev.H bug */
417 cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);
420 outw (ARBCNT(port), DMABUFSZ);
421 outb (ARBSTS(port), BSTS_OWN24);
422 outw (BRBCNT(port), DMABUFSZ);
423 outb (BRBSTS(port), BSTS_OWN24);
425 /* Raise DTR and RTS */
429 /* Enable interrupts */
430 outb (IER(port), IER_RXD | IER_TXD);
434 * Fill transmitter buffer with data.
437 cxput (cx_chan_t *c, char b)
441 unsigned short port = c->chip->port, len, cnt_port, sts_port;
443 /* Choose the buffer. */
446 cnt_port = ATBCNT(port);
447 sts_port = ATBSTS(port);
450 cnt_port = BTBCNT(port);
451 sts_port = BTBSTS(port);
455 if (inb (sts_port) & BSTS_OWN24) {
456 if (c->ifp->if_flags & IFF_DEBUG)
457 print (("cx%d.%d: tbuf %c already busy, bsts=%b\n",
458 c->board->num, c->num, b,
459 inb (sts_port), BSTS_BITS));
463 /* Get the packet to send. */
464 m = sppp_dequeue (c->master);
467 len = m->m_pkthdr.len;
469 /* Count the transmitted bytes to the subchannel, not the master. */
470 c->master->if_obytes -= len + 3;
471 c->ifp->if_obytes += len + 3;
472 c->stat->obytes += len + 3;
474 if (len >= DMABUFSZ) {
475 kprintf ("cx%d.%d: too long packet: %d bytes: ",
476 c->board->num, c->num, len);
481 m_copydata (m, 0, len, buf);
485 /* Start transmitter. */
486 outw (cnt_port, len);
487 outb (sts_port, BSTS_EOFR | BSTS_INTR | BSTS_OWN24);
489 if (c->ifp->if_flags & IFF_DEBUG)
490 print (("cx%d.%d: enqueue %d bytes to %c\n",
491 c->board->num, c->num, len, buf==c->atbuf ? 'A' : 'B'));
493 c->ifp->if_flags |= IFF_OACTIVE;
497 * Start output on the (slave) interface. Get another datagram to send
498 * off of the interface queue, and copy it to the interface
499 * before starting the output.
502 cxsend (cx_chan_t *c)
504 unsigned short port = c->chip->port;
506 if (c->ifp->if_flags & IFF_DEBUG)
507 print (("cx%d.%d: cxsend\n", c->board->num, c->num));
509 /* No output if the interface is down. */
510 if (! (c->ifp->if_flags & IFF_RUNNING))
513 /* Set the current channel number. */
514 outb (CAR(port), c->num & 3);
516 /* Determine the buffer order. */
517 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
525 /* Set up transmit timeout. */
526 if (c->master->if_flags & IFF_OACTIVE)
527 c->master->if_timer = TXTIMEOUT;
530 * Enable TXMPTY interrupt,
531 * to catch the case when the second buffer is empty.
533 if ((inb (ATBSTS(port)) & BSTS_OWN24) &&
534 (inb (BTBSTS(port)) & BSTS_OWN24)) {
535 outb (IER(port), IER_RXD | IER_TXD | IER_TXMPTY);
537 outb (IER(port), IER_RXD | IER_TXD);
541 * Start output on the (master) interface and all slave interfaces.
544 cxstart (struct ifnet *ifp)
546 cx_chan_t *q, *c = ifp->if_softc;
548 if (c->ifp->if_flags & IFF_DEBUG)
549 print (("cx%d.%d: cxstart\n", c->board->num, c->num));
551 /* Start the master subchannel. */
554 /* Start all slave subchannels. */
555 if (c->slaveq && ! sppp_isempty (c->master))
556 for (q=c->slaveq; q; q=q->slaveq)
557 if ((q->ifp->if_flags & IFF_RUNNING) &&
558 ! (q->ifp->if_flags & IFF_OACTIVE))
563 * Handle transmit timeouts.
564 * Recover after lost transmit interrupts.
567 cxwatchdog (struct ifnet *ifp)
569 cx_chan_t *q, *c = ifp->if_softc;
571 if (! (ifp->if_flags & IFF_RUNNING))
573 if (ifp->if_flags & IFF_DEBUG)
574 kprintf ("cx%d.%d: device timeout\n", c->board->num, c->num);
577 for (q=c->slaveq; q; q=q->slaveq)
581 for (q=c->slaveq; q; q=q->slaveq)
588 * Handle receive interrupts, including receive errors and
589 * receive timeout interrupt.
592 cxrinth (cx_chan_t *c)
594 unsigned short port = c->chip->port;
595 unsigned short len, risr = inw (RISR(port));
597 /* Receive errors. */
598 if (risr & (RIS_BUSERR | RIS_OVERRUN | RISH_CRCERR | RISH_RXABORT)) {
599 if (c->ifp->if_flags & IFF_DEBUG)
600 kprintf ("cx%d.%d: receive error, risr=%b\n",
601 c->board->num, c->num, risr, RISH_BITS);
602 ++c->ifp->if_ierrors;
604 if (risr & RIS_OVERRUN)
605 ++c->ifp->if_collisions;
606 } else if (risr & RIS_EOBUF) {
607 if (c->ifp->if_flags & IFF_DEBUG)
608 print (("cx%d.%d: hdlc receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
609 c->board->num, c->num, risr, RISH_BITS,
610 inb (ARBSTS(port)), BSTS_BITS,
611 inb (BRBSTS(port)), BSTS_BITS));
614 /* Handle received data. */
615 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
616 c->stat->ibytes += len;
617 if (len > DMABUFSZ) {
618 /* Fatal error: actual DMA transfer size
619 * exceeds our buffer size. It could be caused
620 * by incorrectly programmed DMA register or
621 * hardware fault. Possibly, should panic here. */
622 kprintf ("cx%d.%d: panic! DMA buffer overflow: %d bytes\n",
623 c->board->num, c->num, len);
624 ++c->ifp->if_ierrors;
625 } else if (! (risr & RIS_EOFR)) {
626 /* The received frame does not fit in the DMA buffer.
627 * It could be caused by serial lie noise,
628 * or if the peer has too big MTU. */
629 if (c->ifp->if_flags & IFF_DEBUG)
630 kprintf ("cx%d.%d: received frame length exceeds MTU, risr=%b\n",
631 c->board->num, c->num, risr, RISH_BITS);
632 ++c->ifp->if_ierrors;
634 /* Valid frame received. */
635 if (c->ifp->if_flags & IFF_DEBUG)
636 print (("cx%d.%d: hdlc received %d bytes\n",
637 c->board->num, c->num, len));
638 cxinput (c, (risr & RIS_BB) ? c->brbuf : c->arbuf, len);
639 ++c->ifp->if_ipackets;
641 } else if (c->ifp->if_flags & IFF_DEBUG) {
642 print (("cx%d.%d: unknown hdlc receive interrupt, risr=%b\n",
643 c->board->num, c->num, risr, RISH_BITS));
647 /* Restart receiver. */
648 if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
649 outw (ARBCNT(port), DMABUFSZ);
650 outb (ARBSTS(port), BSTS_OWN24);
652 if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
653 outw (BRBCNT(port), DMABUFSZ);
654 outb (BRBSTS(port), BSTS_OWN24);
659 * Handle transmit interrupt.
662 cxtinth (cx_chan_t *c)
664 unsigned short port = c->chip->port;
665 unsigned char tisr = inb (TISR(port));
666 unsigned char teoir = 0;
668 c->ifp->if_flags &= ~IFF_OACTIVE;
669 if (c->ifp == c->master)
670 c->ifp->if_timer = 0;
672 if (tisr & (TIS_BUSERR | TIS_UNDERRUN)) {
673 /* if (c->ifp->if_flags & IFF_DEBUG) */
674 print (("cx%d.%d: transmit error, tisr=%b, atbsts=%b, btbsts=%b\n",
675 c->board->num, c->num, tisr, TIS_BITS,
676 inb (ATBSTS(port)), BSTS_BITS,
677 inb (BTBSTS(port)), BSTS_BITS));
678 ++c->ifp->if_oerrors;
681 /* Terminate the failed buffer. */
682 /* teoir = TEOI_TERMBUFF; */
683 } else if (c->ifp->if_flags & IFF_DEBUG)
684 print (("cx%d.%d: hdlc transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
685 c->board->num, c->num, tisr, TIS_BITS,
686 inb (ATBSTS(port)), BSTS_BITS,
687 inb (BTBSTS(port)), BSTS_BITS));
689 if (tisr & TIS_EOFR) {
690 ++c->ifp->if_opackets;
694 /* Start output on the (sub-) channel. */
704 cx_board_t *b = cxboard + bnum;
705 while (! (inw (BSR(b->port)) & BSR_NOINTR)) {
706 /* Acknowledge the interrupt to enter the interrupt context. */
707 /* Read the local interrupt vector register. */
708 unsigned char livr = inb (IACK(b->port, BRD_INTR_LEVEL));
709 cx_chan_t *c = b->chan + (livr>>2 & 0xf);
710 unsigned short port = c->chip->port;
711 unsigned short eoiport = REOIR(port);
712 unsigned char eoi = 0;
714 if (c->type == T_NONE) {
715 kprintf ("cx%d.%d: unexpected interrupt, livr=0x%x\n",
716 c->board->num, c->num, livr);
717 continue; /* incorrect channel number? */
719 /* print (("cx%d.%d: interrupt, livr=0x%x\n",
720 c->board->num, c->num, livr)); */
722 /* Clear RTS to stop receiver data flow while we are busy
723 * processing the interrupt, thus avoiding underruns. */
724 if (! c->sopt.norts) {
725 outb (MSVR_RTS(port), 0);
730 case LIV_EXCEP: /* receive exception */
731 case LIV_RXDATA: /* receive interrupt */
734 case M_ASYNC: eoi = cxrinta (c); break;
735 case M_HDLC: cxrinth (c); break;
736 default:; /* No bisync and X.21 yet */
739 case LIV_TXDATA: /* transmit interrupt */
741 eoiport = TEOIR(port);
743 case M_ASYNC: cxtinta (c); break;
744 case M_HDLC: eoi = cxtinth (c); break;
745 default:; /* No bisync and X.21 yet */
748 case LIV_MODEM: /* modem/timer interrupt */
750 eoiport = MEOIR(port);
755 /* Raise RTS for this channel if and only if
756 * both receive buffers are empty. */
757 if (! c->sopt.norts && (inb (CSR(port)) & CSRA_RXEN) &&
758 (inb (ARBSTS(port)) & BSTS_OWN24) &&
759 (inb (BRBSTS(port)) & BSTS_OWN24)) {
760 outb (MSVR_RTS(port), MSV_RTS);
764 /* Exit from interrupt context. */
767 /* Master channel - start output on all idle subchannels. */
768 if (c->master == c->ifp && c->slaveq &&
769 (livr & 3) == LIV_TXDATA && c->mode == M_HDLC &&
770 ! sppp_isempty (c->ifp)) {
773 for (q=c->slaveq; q; q=q->slaveq)
774 if ((q->ifp->if_flags & IFF_RUNNING) &&
775 ! (q->ifp->if_flags & IFF_OACTIVE))
782 * Process the received packet.
785 cxinput (cx_chan_t *c, void *buf, unsigned len)
788 struct mbuf *m = makembuf (buf, len);
790 if (c->ifp->if_flags & IFF_DEBUG)
791 kprintf ("cx%d.%d: no memory for packet\n",
792 c->board->num, c->num);
793 ++c->ifp->if_iqdrops;
796 m->m_pkthdr.rcvif = c->master;
798 if (c->ifp->if_flags & IFF_DEBUG)
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);
812 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;