1 /* $NetBSD: smc90cx6.c,v 1.38 2001/07/07 15:57:53 thorpej Exp $ */
2 /* $FreeBSD: src/sys/dev/cm/smc90cx6.c,v 1.1.2.3 2003/02/05 18:42:14 fjoe Exp $ */
3 /* $DragonFly: src/sys/dev/netif/cm/Attic/smc90cx6.c,v 1.22 2006/10/25 20:55:56 dillon Exp $ */
6 * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Ignatios Souvatzis.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
43 * compatibility mode) boards
46 #define CMRETRANSMIT /**/
47 /* #define CM_DEBUG */
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/sockio.h>
53 #include <sys/module.h>
54 #include <sys/kernel.h>
55 #include <sys/socket.h>
56 #include <sys/syslog.h>
59 #include <sys/thread2.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/if_arc.h>
66 #include "smc90cx6reg.h"
67 #include "smc90cx6var.h"
69 DECLARE_DUMMY_MODULE(if_cm);
70 MODULE_DEPEND(if_cm, arcnet, 1, 1, 1);
72 /* these should be elsewhere */
75 #define ARC_MIN_FORBID_LEN 254
76 #define ARC_MAX_FORBID_LEN 256
77 #define ARC_MAX_LEN 508
78 #define ARC_ADDR_LEN 1
80 /* for watchdog timer. This should be more than enough. */
81 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
86 bus_space_read_1(rman_get_bustag((sc)->port_res), \
87 rman_get_bushandle((sc)->port_res), \
89 #define PUTREG(off, value) \
90 bus_space_write_1(rman_get_bustag((sc)->port_res), \
91 rman_get_bushandle((sc)->port_res), \
94 bus_space_read_1(rman_get_bustag((sc)->mem_res), \
95 rman_get_bushandle((sc)->mem_res), \
97 #define PUTMEM(off, value) \
98 bus_space_write_1(rman_get_bustag((sc)->mem_res), \
99 rman_get_bushandle((sc)->mem_res), \
102 devclass_t cm_devclass;
105 * This currently uses 2 bufs for tx, 2 for rx
109 * rx has a fillcount variable. If fillcount > (NRXBUF-1),
110 * rx can be switched off from rx hard int.
111 * Else rx is restarted on the other receiver.
112 * rx soft int counts down. if it is == (NRXBUF-1), it restarts
114 * To ensure packet ordering (we need that for 1201 later), we have a counter
115 * which is incremented modulo 256 on each receive and a per buffer
116 * variable, which is set to the counter on filling. The soft int can
117 * compare both values to determine the older packet.
119 * Transmit direction:
121 * cm_start checks tx_fillcount
124 * else fill tx_act ^ 1 && inc tx_fillcount
126 * check tx_fillcount again.
127 * case 2: set IFF_OACTIVE to stop arc_output from filling us.
130 * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
131 * case 1: start tx on tx_act ^ 1, softcall cm_start
132 * case 0: softcall cm_start
134 * #define fill(i) get mbuf && copy mbuf to chip(i)
137 void cm_init (void *);
138 void cm_reset (struct cm_softc *);
139 void cm_start (struct ifnet *);
140 int cm_ioctl (struct ifnet *, unsigned long, caddr_t, struct ucred *);
141 void cm_watchdog (struct ifnet *);
142 void cm_srint (void *vsc);
143 static void cm_tint (struct cm_softc *, int);
144 void cm_reconwatch(void *);
147 cm_probe(device_t dev)
150 struct cm_softc *sc = device_get_softc(dev);
152 error = cm_alloc_port(dev, 0, CM_IO_PORTS);
156 if (GETREG(CMSTAT) == 0xff)
159 error = cm_alloc_memory(dev, 0, 0x800);
167 * Allocate a port resource with the given resource id.
170 cm_alloc_port(device_t dev, int rid, int size)
172 struct cm_softc *sc = device_get_softc(dev);
173 struct resource *res;
175 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
176 0ul, ~0ul, size, RF_ACTIVE);
180 sc->port_used = size;
188 * Allocate a memory resource with the given resource id.
191 cm_alloc_memory(device_t dev, int rid, int size)
193 struct cm_softc *sc = device_get_softc(dev);
194 struct resource *res;
196 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
197 0ul, ~0ul, size, RF_ACTIVE);
209 * Allocate an irq resource with the given resource id.
212 cm_alloc_irq(device_t dev, int rid)
214 struct cm_softc *sc = device_get_softc(dev);
215 struct resource *res;
217 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
228 * Release all resources
231 cm_release_resources(device_t dev)
233 struct cm_softc *sc = device_get_softc(dev);
236 bus_deactivate_resource(dev, SYS_RES_IOPORT,
237 sc->port_rid, sc->port_res);
238 bus_release_resource(dev, SYS_RES_IOPORT,
239 sc->port_rid, sc->port_res);
243 bus_deactivate_resource(dev, SYS_RES_MEMORY,
244 sc->mem_rid, sc->mem_res);
245 bus_release_resource(dev, SYS_RES_MEMORY,
246 sc->mem_rid, sc->mem_res);
250 bus_deactivate_resource(dev, SYS_RES_IRQ,
251 sc->irq_rid, sc->irq_res);
252 bus_release_resource(dev, SYS_RES_IRQ,
253 sc->irq_rid, sc->irq_res);
259 cm_attach(device_t dev)
261 struct cm_softc *sc = device_get_softc(dev);
262 struct ifnet *ifp = &sc->sc_arccom.ac_if;
263 u_int8_t linkaddress;
266 * read the arcnet address from the board
272 } while (!(GETREG(CMSTAT) & CM_POR));
274 linkaddress = GETMEM(CMMACOFF);
276 /* clear the int mask... */
281 PUTREG(CMCMD, CM_CONF(CONF_LONG));
282 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
283 sc->sc_recontime = sc->sc_reconcount = 0;
285 callout_init(&sc->sc_recon_ch);
288 * set interface to stopped condition (reset)
293 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
294 ifp->if_start = cm_start;
295 ifp->if_ioctl = cm_ioctl;
296 ifp->if_watchdog = cm_watchdog;
297 ifp->if_init = cm_init;
298 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
300 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
302 arc_ifattach(ifp, linkaddress, NULL);
313 struct cm_softc *sc = (struct cm_softc *)xsc;
314 struct ifnet *ifp = &sc->sc_arccom.ac_if;
316 if ((ifp->if_flags & IFF_RUNNING) == 0) {
317 ifp->if_flags |= IFF_RUNNING;
324 * Reset the interface...
326 * this assumes that it is called inside a critical section...
330 cm_reset(struct cm_softc *sc)
335 ifp = &sc->sc_arccom.ac_if;
338 if_printf(ifp, "reset\n");
340 /* stop and restart hardware */
345 } while (!(GETREG(CMSTAT) & CM_POR));
347 linkaddress = GETMEM(CMMACOFF);
349 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
350 if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
351 linkaddress, linkaddress);
354 /* tell the routing level about the (possibly changed) link address */
355 arc_storelladdr(ifp, linkaddress);
358 /* POR is NMI, but we need it below: */
359 sc->sc_intmask = CM_RECON|CM_POR;
360 PUTREG(CMSTAT, sc->sc_intmask);
361 PUTREG(CMCMD, CM_CONF(CONF_LONG));
364 if_printf(ifp, "reset: chip configured, status=0x%02x\n",
367 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
370 if_printf(ifp, "reset: bits cleared, status=0x%02x\n", GETREG(CMSTAT));
373 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
377 sc->sc_intmask |= CM_RI;
378 sc->sc_rx_fillcount = 0;
381 PUTREG(CMCMD, CM_RXBC(2));
382 PUTREG(CMSTAT, sc->sc_intmask);
385 if_printf(ifp, "reset: started receiver, status=0x%02x\n",
389 /* and init transmitter status */
391 sc->sc_tx_fillcount = 0;
393 ifp->if_flags |= IFF_RUNNING;
394 ifp->if_flags &= ~IFF_OACTIVE;
400 * Take interface offline
403 cm_stop(struct cm_softc *sc)
405 struct ifnet *ifp = &sc->sc_arccom.ac_if;
407 /* Stop the interrupts */
410 /* Stop the interface */
413 /* Stop watchdog timer */
416 callout_stop(&sc->sc_recon_ch);
417 ifp->if_flags &= ~IFF_RUNNING;
421 * Start output on interface. Get another datagram to send
422 * off the interface queue, and copy it to the
423 * interface becore starting the output
425 * this assumes that it is called inside a critical section...
426 * XXX hm... does it still?
430 cm_start(struct ifnet *ifp)
432 struct cm_softc *sc = ifp->if_softc;
435 int cm_ram_ptr, len, tlen, offset, buffer;
437 u_long copystart, lencopy, perbyte;
440 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
441 if_printf(ifp, "start(%p)\n", ifp);
444 if ((ifp->if_flags & IFF_RUNNING) == 0)
447 if (sc->sc_tx_fillcount >= 2)
450 m = arc_frag_next(ifp);
451 buffer = sc->sc_tx_act ^ 1;
457 if (m->m_len < ARC_HDRLEN)
458 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
459 if_printf(ifp, "start: filling %d from %d to %d type %d\n",
460 buffer, mtod(m, u_char *)[0],
461 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
466 cm_ram_ptr = buffer * 512;
471 /* write the addresses to RAM and throw them away */
474 * Hardware does this: Yet Another Microsecond Saved.
475 * (btw, timing code says usually 2 microseconds)
476 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
479 PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
482 /* get total length left at this point */
483 tlen = m->m_pkthdr.len;
484 if (tlen < ARC_MIN_FORBID_LEN) {
486 PUTMEM(cm_ram_ptr + 2, offset);
488 PUTMEM(cm_ram_ptr + 2, 0);
489 if (tlen <= ARC_MAX_FORBID_LEN)
490 offset = 255; /* !!! */
492 if (tlen > ARC_MAX_LEN)
496 PUTMEM(cm_ram_ptr + 3, offset);
499 cm_ram_ptr += offset;
501 /* lets loop through the mbuf chain */
503 for (mp = m; mp; mp = mp->m_next) {
504 if ((len = mp->m_len)) { /* YAMS */
505 bus_space_write_region_1(
506 rman_get_bustag(sc->mem_res),
507 rman_get_bushandle(sc->mem_res),
508 cm_ram_ptr, mtod(mp, caddr_t), len);
514 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
515 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
517 /* actually transmit the packet */
519 if (++sc->sc_tx_fillcount > 1) {
521 * We are filled up to the rim. No more bufs for the moment,
524 ifp->if_flags |= IFF_OACTIVE;
527 if_printf(ifp, "start: starting transmitter on buffer %d\n",
530 /* Transmitter was off, start it */
531 sc->sc_tx_act = buffer;
534 * We still can accept another buf, so don't:
535 * ifp->if_flags |= IFF_OACTIVE;
537 sc->sc_intmask |= CM_TA;
538 PUTREG(CMCMD, CM_TX(buffer));
539 PUTREG(CMSTAT, sc->sc_intmask);
541 sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
546 * After 10 times reading the docs, I realized
547 * that in the case the receiver NAKs the buffer request,
548 * the hardware retries till shutdown.
549 * This is integrated now in the code above.
556 * Arcnet interface receiver soft interrupt:
557 * get the stuff out of any filled buffer we find.
562 struct cm_softc *sc = (struct cm_softc *)vsc;
563 int buffer, len, offset, type;
566 struct arc_header *ah;
567 struct ifnet *ifp = &sc->sc_arccom.ac_if;
569 buffer = sc->sc_rx_act ^ 1;
572 * Align so that IP packet will be longword aligned. Here we
573 * assume that m_data of new packet is longword aligned.
574 * When implementing PHDS, we might have to change it to 2,
575 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
578 cm_ram_ptr = buffer * 512;
579 offset = GETMEM(cm_ram_ptr + 2);
583 offset = GETMEM(cm_ram_ptr + 3);
588 * Allocate header mbuf.
590 * First +2 bytes for align fixup below.
591 * Second +2 bytes are for src/dst addresses.
593 m = m_getl(len + 2 + 2, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
596 * in case s.th. goes wrong with mem, drop it
597 * to make sure the receiver can be started again
598 * count it as input error (we dont have any other
604 m->m_pkthdr.rcvif = ifp;
606 type = GETMEM(cm_ram_ptr + offset);
607 m->m_data += 1 + arc_isphds(type);
608 /* mbuf filled with ARCnet addresses */
609 m->m_pkthdr.len = m->m_len = len + 2;
611 ah = mtod(m, struct arc_header *);
612 ah->arc_shost = GETMEM(cm_ram_ptr + 0);
613 ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
615 bus_space_read_region_1(
616 rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
617 cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
619 ifp->if_input(ifp, m);
629 /* mark buffer as invalid by source id 0 */
630 PUTMEM(buffer << 9, 0);
632 if (--sc->sc_rx_fillcount == 2 - 1) {
634 /* was off, restart it on buffer just emptied */
635 sc->sc_rx_act = buffer;
636 sc->sc_intmask |= CM_RI;
638 /* this also clears the RI flag interupt: */
639 PUTREG(CMCMD, CM_RXBC(buffer));
640 PUTREG(CMSTAT, sc->sc_intmask);
643 if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
649 cm_tint(struct cm_softc *sc, int isr)
658 ifp = &(sc->sc_arccom.ac_if);
659 buffer = sc->sc_tx_act;
663 * Normal situtations first for fast path:
664 * If acknowledgement received ok or broadcast, we're ok.
668 if (isr & CM_TMA || sc->sc_broadcast[buffer])
669 sc->sc_arccom.ac_if.if_opackets++;
671 else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
672 && --sc->sc_retransmits[buffer] > 0) {
673 /* retransmit same buffer */
674 PUTREG(CMCMD, CM_TX(buffer));
682 /* We know we can accept another buffer at this point. */
683 ifp->if_flags &= ~IFF_OACTIVE;
685 if (--sc->sc_tx_fillcount > 0) {
688 * start tx on other buffer.
689 * This also clears the int flag
692 sc->sc_tx_act = buffer;
696 * sc->sc_intmask |= CM_TA;
697 * PUTREG(CMSTAT, sc->sc_intmask);
699 PUTREG(CMCMD, CM_TX(buffer));
700 /* init watchdog timer */
701 ifp->if_timer = ARCTIMEOUT;
703 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
704 if_printf(ifp, "tint: starting tx on buffer %d, "
705 "status 0x%02x\n", buffer, GETREG(CMSTAT));
708 /* have to disable TX interrupt */
709 sc->sc_intmask &= ~CM_TA;
710 PUTREG(CMSTAT, sc->sc_intmask);
711 /* ... and watchdog timer */
715 if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
720 /* call it directly */
725 * Our interrupt routine
730 struct cm_softc *sc = arg;
731 struct ifnet *ifp = &sc->sc_arccom.ac_if;
733 u_char isr, maskedisr;
737 isr = GETREG(CMSTAT);
738 maskedisr = isr & sc->sc_intmask;
743 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
744 if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
745 isr, sc->sc_intmask);
748 if (maskedisr & CM_POR) {
750 * XXX We should never see this. Don't bother to store
752 * sc->sc_arccom.ac_anaddr = GETMEM(CMMACOFF);
754 PUTREG(CMCMD, CM_CLR(CLR_POR));
756 "%s: intr: got spurious power on reset int\n",
760 if (maskedisr & CM_RECON) {
763 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
765 PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
766 sc->sc_arccom.ac_if.if_collisions++;
769 * If less than 2 seconds per reconfig:
770 * If ARC_EXCESSIVE_RECONFIGS
771 * since last burst, complain and set treshold for
772 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
774 * This allows for, e.g., new stations on the cable, or
775 * cable switching as long as it is over after
776 * (normally) 16 seconds.
778 * XXX TODO: check timeout bits in status word and
779 * double time if necessary.
782 callout_stop(&sc->sc_recon_ch);
783 newsec = time_second;
784 if ((newsec - sc->sc_recontime <= 2) &&
785 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
787 "%s: excessive token losses, "
791 sc->sc_recontime = newsec;
792 callout_reset(&sc->sc_recon_ch, 15 * hz,
793 cm_reconwatch, (void *)sc);
796 if (maskedisr & CM_RI) {
797 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
798 if_printf(ifp, "intr: hard rint, act %d\n",
802 buffer = sc->sc_rx_act;
803 /* look if buffer is marked invalid: */
804 if (GETMEM(buffer * 512) == 0) {
806 * invalid marked buffer (or illegally
810 "%s: spurious RX interupt or sender 0 "
811 " (ignored)\n", ifp->if_xname);
813 * restart receiver on same buffer.
814 * XXX maybe better reset interface?
816 PUTREG(CMCMD, CM_RXBC(buffer));
818 if (++sc->sc_rx_fillcount > 1) {
819 sc->sc_intmask &= ~CM_RI;
820 PUTREG(CMSTAT, sc->sc_intmask);
823 sc->sc_rx_act = buffer;
826 * Start receiver on other receive
827 * buffer. This also clears the RI
830 PUTREG(CMCMD, CM_RXBC(buffer));
831 /* in RX intr, so mask is ok for RX */
834 if_printf(ifp, "strt rx for buf %d, "
836 sc->sc_rx_act, GETREG(CMSTAT));
840 /* this one does the copy here */
844 if (maskedisr & CM_TA) {
847 isr = GETREG(CMSTAT);
848 maskedisr = isr & sc->sc_intmask;
850 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
851 if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
852 isr, sc->sc_intmask);
857 cm_reconwatch(void *arg)
859 struct cm_softc *sc = arg;
860 struct ifnet *ifp = &sc->sc_arccom.ac_if;
862 lwkt_serialize_enter(ifp->if_serializer);
863 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
864 sc->sc_reconcount = 0;
865 log(LOG_WARNING, "%s: token valid again.\n",
868 sc->sc_reconcount = 0;
869 lwkt_serialize_exit(ifp->if_serializer);
874 * Process an ioctl request.
875 * This code needs some work - it looks pretty ugly.
878 cm_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
887 ifa = (struct ifaddr *)data;
888 ifr = (struct ifreq *)data;
890 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
891 if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
896 if ((ifp->if_flags & IFF_UP) == 0 &&
897 (ifp->if_flags & IFF_RUNNING) != 0) {
899 * If interface is marked down and it is running,
903 } else if ((ifp->if_flags & IFF_UP) != 0 &&
904 (ifp->if_flags & IFF_RUNNING) == 0) {
906 * If interface is marked up and it is stopped, then
914 error = arc_ioctl(ifp, command, data);
921 * watchdog routine for transmitter.
923 * We need this, because else a receiver whose hardware is alive, but whose
924 * software has not enabled the Receiver, would make our hardware wait forever
925 * Discovered this after 20 times reading the docs.
927 * Only thing we do is disable transmitter. We'll get an transmit timeout,
928 * and the int handler will have to decide not to retransmit (in case
929 * retransmission is implemented).
933 cm_watchdog(struct ifnet *ifp)
935 struct cm_softc *sc = ifp->if_softc;
937 PUTREG(CMCMD, CM_TXDIS);