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.21 2006/08/06 12:49:04 swildner 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>
57 #include <sys/thread2.h>
60 #include <machine/bus.h>
62 #include <machine/resource.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/if_arc.h>
69 #include "smc90cx6reg.h"
70 #include "smc90cx6var.h"
72 DECLARE_DUMMY_MODULE(if_cm);
73 MODULE_DEPEND(if_cm, arcnet, 1, 1, 1);
75 /* these should be elsewhere */
78 #define ARC_MIN_FORBID_LEN 254
79 #define ARC_MAX_FORBID_LEN 256
80 #define ARC_MAX_LEN 508
81 #define ARC_ADDR_LEN 1
83 /* for watchdog timer. This should be more than enough. */
84 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
89 bus_space_read_1(rman_get_bustag((sc)->port_res), \
90 rman_get_bushandle((sc)->port_res), \
92 #define PUTREG(off, value) \
93 bus_space_write_1(rman_get_bustag((sc)->port_res), \
94 rman_get_bushandle((sc)->port_res), \
97 bus_space_read_1(rman_get_bustag((sc)->mem_res), \
98 rman_get_bushandle((sc)->mem_res), \
100 #define PUTMEM(off, value) \
101 bus_space_write_1(rman_get_bustag((sc)->mem_res), \
102 rman_get_bushandle((sc)->mem_res), \
105 devclass_t cm_devclass;
108 * This currently uses 2 bufs for tx, 2 for rx
112 * rx has a fillcount variable. If fillcount > (NRXBUF-1),
113 * rx can be switched off from rx hard int.
114 * Else rx is restarted on the other receiver.
115 * rx soft int counts down. if it is == (NRXBUF-1), it restarts
117 * To ensure packet ordering (we need that for 1201 later), we have a counter
118 * which is incremented modulo 256 on each receive and a per buffer
119 * variable, which is set to the counter on filling. The soft int can
120 * compare both values to determine the older packet.
122 * Transmit direction:
124 * cm_start checks tx_fillcount
127 * else fill tx_act ^ 1 && inc tx_fillcount
129 * check tx_fillcount again.
130 * case 2: set IFF_OACTIVE to stop arc_output from filling us.
133 * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
134 * case 1: start tx on tx_act ^ 1, softcall cm_start
135 * case 0: softcall cm_start
137 * #define fill(i) get mbuf && copy mbuf to chip(i)
140 void cm_init (void *);
141 void cm_reset (struct cm_softc *);
142 void cm_start (struct ifnet *);
143 int cm_ioctl (struct ifnet *, unsigned long, caddr_t, struct ucred *);
144 void cm_watchdog (struct ifnet *);
145 void cm_srint (void *vsc);
146 static void cm_tint (struct cm_softc *, int);
147 void cm_reconwatch(void *);
150 cm_probe(device_t dev)
153 struct cm_softc *sc = device_get_softc(dev);
155 error = cm_alloc_port(dev, 0, CM_IO_PORTS);
159 if (GETREG(CMSTAT) == 0xff)
162 error = cm_alloc_memory(dev, 0, 0x800);
170 * Allocate a port resource with the given resource id.
173 cm_alloc_port(device_t dev, int rid, int size)
175 struct cm_softc *sc = device_get_softc(dev);
176 struct resource *res;
178 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
179 0ul, ~0ul, size, RF_ACTIVE);
183 sc->port_used = size;
191 * Allocate a memory resource with the given resource id.
194 cm_alloc_memory(device_t dev, int rid, int size)
196 struct cm_softc *sc = device_get_softc(dev);
197 struct resource *res;
199 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
200 0ul, ~0ul, size, RF_ACTIVE);
212 * Allocate an irq resource with the given resource id.
215 cm_alloc_irq(device_t dev, int rid)
217 struct cm_softc *sc = device_get_softc(dev);
218 struct resource *res;
220 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
231 * Release all resources
234 cm_release_resources(device_t dev)
236 struct cm_softc *sc = device_get_softc(dev);
239 bus_deactivate_resource(dev, SYS_RES_IOPORT,
240 sc->port_rid, sc->port_res);
241 bus_release_resource(dev, SYS_RES_IOPORT,
242 sc->port_rid, sc->port_res);
246 bus_deactivate_resource(dev, SYS_RES_MEMORY,
247 sc->mem_rid, sc->mem_res);
248 bus_release_resource(dev, SYS_RES_MEMORY,
249 sc->mem_rid, sc->mem_res);
253 bus_deactivate_resource(dev, SYS_RES_IRQ,
254 sc->irq_rid, sc->irq_res);
255 bus_release_resource(dev, SYS_RES_IRQ,
256 sc->irq_rid, sc->irq_res);
262 cm_attach(device_t dev)
264 struct cm_softc *sc = device_get_softc(dev);
265 struct ifnet *ifp = &sc->sc_arccom.ac_if;
266 u_int8_t linkaddress;
269 * read the arcnet address from the board
275 } while (!(GETREG(CMSTAT) & CM_POR));
277 linkaddress = GETMEM(CMMACOFF);
279 /* clear the int mask... */
284 PUTREG(CMCMD, CM_CONF(CONF_LONG));
285 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
286 sc->sc_recontime = sc->sc_reconcount = 0;
288 callout_init(&sc->sc_recon_ch);
291 * set interface to stopped condition (reset)
296 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
297 ifp->if_start = cm_start;
298 ifp->if_ioctl = cm_ioctl;
299 ifp->if_watchdog = cm_watchdog;
300 ifp->if_init = cm_init;
301 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
303 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
305 arc_ifattach(ifp, linkaddress, NULL);
316 struct cm_softc *sc = (struct cm_softc *)xsc;
317 struct ifnet *ifp = &sc->sc_arccom.ac_if;
319 if ((ifp->if_flags & IFF_RUNNING) == 0) {
320 ifp->if_flags |= IFF_RUNNING;
327 * Reset the interface...
329 * this assumes that it is called inside a critical section...
333 cm_reset(struct cm_softc *sc)
338 ifp = &sc->sc_arccom.ac_if;
341 if_printf(ifp, "reset\n");
343 /* stop and restart hardware */
348 } while (!(GETREG(CMSTAT) & CM_POR));
350 linkaddress = GETMEM(CMMACOFF);
352 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
353 if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
354 linkaddress, linkaddress);
357 /* tell the routing level about the (possibly changed) link address */
358 arc_storelladdr(ifp, linkaddress);
361 /* POR is NMI, but we need it below: */
362 sc->sc_intmask = CM_RECON|CM_POR;
363 PUTREG(CMSTAT, sc->sc_intmask);
364 PUTREG(CMCMD, CM_CONF(CONF_LONG));
367 if_printf(ifp, "reset: chip configured, status=0x%02x\n",
370 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
373 if_printf(ifp, "reset: bits cleared, status=0x%02x\n", GETREG(CMSTAT));
376 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
380 sc->sc_intmask |= CM_RI;
381 sc->sc_rx_fillcount = 0;
384 PUTREG(CMCMD, CM_RXBC(2));
385 PUTREG(CMSTAT, sc->sc_intmask);
388 if_printf(ifp, "reset: started receiver, status=0x%02x\n",
392 /* and init transmitter status */
394 sc->sc_tx_fillcount = 0;
396 ifp->if_flags |= IFF_RUNNING;
397 ifp->if_flags &= ~IFF_OACTIVE;
403 * Take interface offline
406 cm_stop(struct cm_softc *sc)
408 struct ifnet *ifp = &sc->sc_arccom.ac_if;
410 /* Stop the interrupts */
413 /* Stop the interface */
416 /* Stop watchdog timer */
419 callout_stop(&sc->sc_recon_ch);
420 ifp->if_flags &= ~IFF_RUNNING;
424 * Start output on interface. Get another datagram to send
425 * off the interface queue, and copy it to the
426 * interface becore starting the output
428 * this assumes that it is called inside a critical section...
429 * XXX hm... does it still?
433 cm_start(struct ifnet *ifp)
435 struct cm_softc *sc = ifp->if_softc;
438 int cm_ram_ptr, len, tlen, offset, buffer;
440 u_long copystart, lencopy, perbyte;
443 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
444 if_printf(ifp, "start(%p)\n", ifp);
447 if ((ifp->if_flags & IFF_RUNNING) == 0)
450 if (sc->sc_tx_fillcount >= 2)
453 m = arc_frag_next(ifp);
454 buffer = sc->sc_tx_act ^ 1;
460 if (m->m_len < ARC_HDRLEN)
461 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
462 if_printf(ifp, "start: filling %d from %d to %d type %d\n",
463 buffer, mtod(m, u_char *)[0],
464 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
469 cm_ram_ptr = buffer * 512;
474 /* write the addresses to RAM and throw them away */
477 * Hardware does this: Yet Another Microsecond Saved.
478 * (btw, timing code says usually 2 microseconds)
479 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
482 PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
485 /* get total length left at this point */
486 tlen = m->m_pkthdr.len;
487 if (tlen < ARC_MIN_FORBID_LEN) {
489 PUTMEM(cm_ram_ptr + 2, offset);
491 PUTMEM(cm_ram_ptr + 2, 0);
492 if (tlen <= ARC_MAX_FORBID_LEN)
493 offset = 255; /* !!! */
495 if (tlen > ARC_MAX_LEN)
499 PUTMEM(cm_ram_ptr + 3, offset);
502 cm_ram_ptr += offset;
504 /* lets loop through the mbuf chain */
506 for (mp = m; mp; mp = mp->m_next) {
507 if ((len = mp->m_len)) { /* YAMS */
508 bus_space_write_region_1(
509 rman_get_bustag(sc->mem_res),
510 rman_get_bushandle(sc->mem_res),
511 cm_ram_ptr, mtod(mp, caddr_t), len);
517 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
518 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
520 /* actually transmit the packet */
522 if (++sc->sc_tx_fillcount > 1) {
524 * We are filled up to the rim. No more bufs for the moment,
527 ifp->if_flags |= IFF_OACTIVE;
530 if_printf(ifp, "start: starting transmitter on buffer %d\n",
533 /* Transmitter was off, start it */
534 sc->sc_tx_act = buffer;
537 * We still can accept another buf, so don't:
538 * ifp->if_flags |= IFF_OACTIVE;
540 sc->sc_intmask |= CM_TA;
541 PUTREG(CMCMD, CM_TX(buffer));
542 PUTREG(CMSTAT, sc->sc_intmask);
544 sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
549 * After 10 times reading the docs, I realized
550 * that in the case the receiver NAKs the buffer request,
551 * the hardware retries till shutdown.
552 * This is integrated now in the code above.
559 * Arcnet interface receiver soft interrupt:
560 * get the stuff out of any filled buffer we find.
565 struct cm_softc *sc = (struct cm_softc *)vsc;
566 int buffer, len, offset, type;
569 struct arc_header *ah;
570 struct ifnet *ifp = &sc->sc_arccom.ac_if;
572 buffer = sc->sc_rx_act ^ 1;
575 * Align so that IP packet will be longword aligned. Here we
576 * assume that m_data of new packet is longword aligned.
577 * When implementing PHDS, we might have to change it to 2,
578 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
581 cm_ram_ptr = buffer * 512;
582 offset = GETMEM(cm_ram_ptr + 2);
586 offset = GETMEM(cm_ram_ptr + 3);
591 * Allocate header mbuf.
593 * First +2 bytes for align fixup below.
594 * Second +2 bytes are for src/dst addresses.
596 m = m_getl(len + 2 + 2, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
599 * in case s.th. goes wrong with mem, drop it
600 * to make sure the receiver can be started again
601 * count it as input error (we dont have any other
607 m->m_pkthdr.rcvif = ifp;
609 type = GETMEM(cm_ram_ptr + offset);
610 m->m_data += 1 + arc_isphds(type);
611 /* mbuf filled with ARCnet addresses */
612 m->m_pkthdr.len = m->m_len = len + 2;
614 ah = mtod(m, struct arc_header *);
615 ah->arc_shost = GETMEM(cm_ram_ptr + 0);
616 ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
618 bus_space_read_region_1(
619 rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
620 cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
622 ifp->if_input(ifp, m);
632 /* mark buffer as invalid by source id 0 */
633 PUTMEM(buffer << 9, 0);
635 if (--sc->sc_rx_fillcount == 2 - 1) {
637 /* was off, restart it on buffer just emptied */
638 sc->sc_rx_act = buffer;
639 sc->sc_intmask |= CM_RI;
641 /* this also clears the RI flag interupt: */
642 PUTREG(CMCMD, CM_RXBC(buffer));
643 PUTREG(CMSTAT, sc->sc_intmask);
646 if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
652 cm_tint(struct cm_softc *sc, int isr)
661 ifp = &(sc->sc_arccom.ac_if);
662 buffer = sc->sc_tx_act;
666 * Normal situtations first for fast path:
667 * If acknowledgement received ok or broadcast, we're ok.
671 if (isr & CM_TMA || sc->sc_broadcast[buffer])
672 sc->sc_arccom.ac_if.if_opackets++;
674 else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
675 && --sc->sc_retransmits[buffer] > 0) {
676 /* retransmit same buffer */
677 PUTREG(CMCMD, CM_TX(buffer));
685 /* We know we can accept another buffer at this point. */
686 ifp->if_flags &= ~IFF_OACTIVE;
688 if (--sc->sc_tx_fillcount > 0) {
691 * start tx on other buffer.
692 * This also clears the int flag
695 sc->sc_tx_act = buffer;
699 * sc->sc_intmask |= CM_TA;
700 * PUTREG(CMSTAT, sc->sc_intmask);
702 PUTREG(CMCMD, CM_TX(buffer));
703 /* init watchdog timer */
704 ifp->if_timer = ARCTIMEOUT;
706 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
707 if_printf(ifp, "tint: starting tx on buffer %d, "
708 "status 0x%02x\n", buffer, GETREG(CMSTAT));
711 /* have to disable TX interrupt */
712 sc->sc_intmask &= ~CM_TA;
713 PUTREG(CMSTAT, sc->sc_intmask);
714 /* ... and watchdog timer */
718 if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
723 /* call it directly */
728 * Our interrupt routine
733 struct cm_softc *sc = arg;
734 struct ifnet *ifp = &sc->sc_arccom.ac_if;
736 u_char isr, maskedisr;
740 isr = GETREG(CMSTAT);
741 maskedisr = isr & sc->sc_intmask;
746 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
747 if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
748 isr, sc->sc_intmask);
751 if (maskedisr & CM_POR) {
753 * XXX We should never see this. Don't bother to store
755 * sc->sc_arccom.ac_anaddr = GETMEM(CMMACOFF);
757 PUTREG(CMCMD, CM_CLR(CLR_POR));
759 "%s: intr: got spurious power on reset int\n",
763 if (maskedisr & CM_RECON) {
766 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
768 PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
769 sc->sc_arccom.ac_if.if_collisions++;
772 * If less than 2 seconds per reconfig:
773 * If ARC_EXCESSIVE_RECONFIGS
774 * since last burst, complain and set treshold for
775 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
777 * This allows for, e.g., new stations on the cable, or
778 * cable switching as long as it is over after
779 * (normally) 16 seconds.
781 * XXX TODO: check timeout bits in status word and
782 * double time if necessary.
785 callout_stop(&sc->sc_recon_ch);
786 newsec = time_second;
787 if ((newsec - sc->sc_recontime <= 2) &&
788 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
790 "%s: excessive token losses, "
794 sc->sc_recontime = newsec;
795 callout_reset(&sc->sc_recon_ch, 15 * hz,
796 cm_reconwatch, (void *)sc);
799 if (maskedisr & CM_RI) {
800 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
801 if_printf(ifp, "intr: hard rint, act %d\n",
805 buffer = sc->sc_rx_act;
806 /* look if buffer is marked invalid: */
807 if (GETMEM(buffer * 512) == 0) {
809 * invalid marked buffer (or illegally
813 "%s: spurious RX interupt or sender 0 "
814 " (ignored)\n", ifp->if_xname);
816 * restart receiver on same buffer.
817 * XXX maybe better reset interface?
819 PUTREG(CMCMD, CM_RXBC(buffer));
821 if (++sc->sc_rx_fillcount > 1) {
822 sc->sc_intmask &= ~CM_RI;
823 PUTREG(CMSTAT, sc->sc_intmask);
826 sc->sc_rx_act = buffer;
829 * Start receiver on other receive
830 * buffer. This also clears the RI
833 PUTREG(CMCMD, CM_RXBC(buffer));
834 /* in RX intr, so mask is ok for RX */
837 if_printf(ifp, "strt rx for buf %d, "
839 sc->sc_rx_act, GETREG(CMSTAT));
843 /* this one does the copy here */
847 if (maskedisr & CM_TA) {
850 isr = GETREG(CMSTAT);
851 maskedisr = isr & sc->sc_intmask;
853 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
854 if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
855 isr, sc->sc_intmask);
860 cm_reconwatch(void *arg)
862 struct cm_softc *sc = arg;
863 struct ifnet *ifp = &sc->sc_arccom.ac_if;
865 lwkt_serialize_enter(ifp->if_serializer);
866 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
867 sc->sc_reconcount = 0;
868 log(LOG_WARNING, "%s: token valid again.\n",
871 sc->sc_reconcount = 0;
872 lwkt_serialize_exit(ifp->if_serializer);
877 * Process an ioctl request.
878 * This code needs some work - it looks pretty ugly.
881 cm_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
890 ifa = (struct ifaddr *)data;
891 ifr = (struct ifreq *)data;
893 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
894 if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
899 if ((ifp->if_flags & IFF_UP) == 0 &&
900 (ifp->if_flags & IFF_RUNNING) != 0) {
902 * If interface is marked down and it is running,
906 } else if ((ifp->if_flags & IFF_UP) != 0 &&
907 (ifp->if_flags & IFF_RUNNING) == 0) {
909 * If interface is marked up and it is stopped, then
917 error = arc_ioctl(ifp, command, data);
924 * watchdog routine for transmitter.
926 * We need this, because else a receiver whose hardware is alive, but whose
927 * software has not enabled the Receiver, would make our hardware wait forever
928 * Discovered this after 20 times reading the docs.
930 * Only thing we do is disable transmitter. We'll get an transmit timeout,
931 * and the int handler will have to decide not to retransmit (in case
932 * retransmission is implemented).
936 cm_watchdog(struct ifnet *ifp)
938 struct cm_softc *sc = ifp->if_softc;
940 PUTREG(CMCMD, CM_TXDIS);