1 /* $NetBSD: smc90cx6.c,v 1.38 2001/07/07 15:57:53 thorpej Exp $ */
7 * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Ignatios Souvatzis.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
44 * compatibility mode) boards
47 /* #define CMSOFTCOPY */
48 #define CMRETRANSMIT /**/
49 /* #define CM_DEBUG */
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/sockio.h>
55 #include <sys/module.h>
56 #include <sys/kernel.h>
57 #include <sys/socket.h>
58 #include <sys/syslog.h>
61 #include <machine/bus.h>
63 #include <machine/resource.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/if_arc.h>
70 #include <dev/cm/smc90cx6reg.h>
71 #include <dev/cm/smc90cx6var.h>
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)
86 devclass_t cm_devclass;
89 * This currently uses 2 bufs for tx, 2 for rx
93 * rx has a fillcount variable. If fillcount > (NRXBUF-1),
94 * rx can be switched off from rx hard int.
95 * Else rx is restarted on the other receiver.
96 * rx soft int counts down. if it is == (NRXBUF-1), it restarts
98 * To ensure packet ordering (we need that for 1201 later), we have a counter
99 * which is incremented modulo 256 on each receive and a per buffer
100 * variable, which is set to the counter on filling. The soft int can
101 * compare both values to determine the older packet.
103 * Transmit direction:
105 * cm_start checks tx_fillcount
108 * else fill tx_act ^ 1 && inc tx_fillcount
110 * check tx_fillcount again.
111 * case 2: set IFF_DRV_OACTIVE to stop arc_output from filling us.
114 * tint clears IFF_OACTIVE, decrements and checks tx_fillcount
115 * case 1: start tx on tx_act ^ 1, softcall cm_start
116 * case 0: softcall cm_start
118 * #define fill(i) get mbuf && copy mbuf to chip(i)
121 void cm_init(void *);
122 static void cm_init_locked(struct cm_softc *);
123 static void cm_reset_locked(struct cm_softc *);
124 void cm_start(struct ifnet *);
125 void cm_start_locked(struct ifnet *);
126 int cm_ioctl(struct ifnet *, unsigned long, caddr_t);
127 void cm_watchdog(void *);
128 void cm_srint_locked(void *vsc);
129 static void cm_tint_locked(struct cm_softc *, int);
130 void cm_reconwatch_locked(void *);
133 * Release all resources
136 cm_release_resources(dev)
139 struct cm_softc *sc = device_get_softc(dev);
141 if (sc->port_res != NULL) {
142 bus_release_resource(dev, SYS_RES_IOPORT,
146 if (sc->mem_res != NULL) {
147 bus_release_resource(dev, SYS_RES_MEMORY,
151 if (sc->irq_res != NULL) {
152 bus_release_resource(dev, SYS_RES_IRQ,
162 struct cm_softc *sc = device_get_softc(dev);
164 u_int8_t linkaddress;
166 ifp = sc->sc_ifp = if_alloc(IFT_ARCNET);
171 * read the arcnet address from the board
176 } while (!(GETREG(CMSTAT) & CM_POR));
177 linkaddress = GETMEM(CMMACOFF);
179 /* clear the int mask... */
183 PUTREG(CMCMD, CM_CONF(CONF_LONG));
184 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
185 sc->sc_recontime = sc->sc_reconcount = 0;
188 * set interface to stopped condition (reset)
193 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
194 ifp->if_output = arc_output;
195 ifp->if_start = cm_start;
196 ifp->if_ioctl = cm_ioctl;
197 ifp->if_init = cm_init;
198 /* XXX IFQ_SET_READY(&ifp->if_snd); */
199 ifp->if_snd.ifq_maxlen = ifqmaxlen;
200 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
202 arc_ifattach(ifp, linkaddress);
205 sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
206 sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
207 (void (*)(void *))cm_start, ifp);
210 callout_init_mtx(&sc->sc_recon_ch, &sc->sc_mtx, 0);
211 callout_init_mtx(&sc->sc_watchdog_timer, &sc->sc_mtx, 0);
213 if_printf(ifp, "link addr 0x%02x (%d)\n", linkaddress, linkaddress);
225 struct cm_softc *sc = (struct cm_softc *)xsc;
233 cm_init_locked(struct cm_softc *sc)
235 struct ifnet *ifp = sc->sc_ifp;
237 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
238 ifp->if_drv_flags |= IFF_DRV_RUNNING;
244 * Reset the interface...
246 * Assumes that it is called with sc_mtx held
258 if_printf(ifp, "reset\n");
260 /* stop and restart hardware */
265 } while (!(GETREG(CMSTAT) & CM_POR));
267 linkaddress = GETMEM(CMMACOFF);
269 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
270 if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
271 linkaddress, linkaddress);
274 /* tell the routing level about the (possibly changed) link address */
275 arc_storelladdr(ifp, linkaddress);
278 /* POR is NMI, but we need it below: */
279 sc->sc_intmask = CM_RECON|CM_POR;
280 PUTREG(CMSTAT, sc->sc_intmask);
281 PUTREG(CMCMD, CM_CONF(CONF_LONG));
284 if_printf(ifp, "reset: chip configured, status=0x%02x\n",
287 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
290 if_printf(ifp, "reset: bits cleared, status=0x%02x\n",
294 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
298 sc->sc_intmask |= CM_RI;
299 sc->sc_rx_fillcount = 0;
302 PUTREG(CMCMD, CM_RXBC(2));
303 PUTREG(CMSTAT, sc->sc_intmask);
306 if_printf(ifp, "reset: started receiver, status=0x%02x\n",
310 /* and init transmitter status */
312 sc->sc_tx_fillcount = 0;
314 ifp->if_drv_flags |= IFF_DRV_RUNNING;
315 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
317 callout_reset(&sc->sc_watchdog_timer, hz, cm_watchdog, sc);
318 cm_start_locked(ifp);
322 * Take interface offline
328 /* Stop the interrupts */
331 /* Stop the interface */
334 /* Stop watchdog timer */
335 callout_stop(&sc->sc_watchdog_timer);
340 cm_start(struct ifnet *ifp)
342 struct cm_softc *sc = ifp->if_softc;
345 cm_start_locked(ifp);
350 * Start output on interface. Get another datagram to send
351 * off the interface queue, and copy it to the
352 * interface becore starting the output
354 * Assumes that sc_mtx is held
360 struct cm_softc *sc = ifp->if_softc;
364 int len, tlen, offset, buffer;
366 u_long copystart, lencopy, perbyte;
369 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
370 if_printf(ifp, "start(%p)\n", ifp);
373 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
376 if (sc->sc_tx_fillcount >= 2)
379 m = arc_frag_next(ifp);
380 buffer = sc->sc_tx_act ^ 1;
386 if (m->m_len < ARC_HDRLEN)
387 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
388 if_printf(ifp, "start: filling %d from %d to %d type %d\n",
389 buffer, mtod(m, u_char *)[0],
390 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
395 cm_ram_ptr = buffer * 512;
400 /* write the addresses to RAM and throw them away */
403 * Hardware does this: Yet Another Microsecond Saved.
404 * (btw, timing code says usually 2 microseconds)
405 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
408 PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
411 /* get total length left at this point */
412 tlen = m->m_pkthdr.len;
413 if (tlen < ARC_MIN_FORBID_LEN) {
415 PUTMEM(cm_ram_ptr + 2, offset);
417 PUTMEM(cm_ram_ptr + 2, 0);
418 if (tlen <= ARC_MAX_FORBID_LEN)
419 offset = 255; /* !!! */
421 if (tlen > ARC_MAX_LEN)
425 PUTMEM(cm_ram_ptr + 3, offset);
428 cm_ram_ptr += offset;
430 /* lets loop through the mbuf chain */
432 for (mp = m; mp; mp = mp->m_next) {
433 if ((len = mp->m_len)) { /* YAMS */
434 bus_space_write_region_1(
435 rman_get_bustag(sc->mem_res),
436 rman_get_bushandle(sc->mem_res),
437 cm_ram_ptr, mtod(mp, caddr_t), len);
443 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
444 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
446 if (++sc->sc_tx_fillcount > 1) {
448 * We are filled up to the rim. No more bufs for the moment,
451 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
454 if_printf(ifp, "start: starting transmitter on buffer %d\n",
457 /* Transmitter was off, start it */
458 sc->sc_tx_act = buffer;
461 * We still can accept another buf, so don't:
462 * ifp->if_drv_flags |= IFF_DRV_OACTIVE;
464 sc->sc_intmask |= CM_TA;
465 PUTREG(CMCMD, CM_TX(buffer));
466 PUTREG(CMSTAT, sc->sc_intmask);
468 sc->sc_timer = ARCTIMEOUT;
473 * After 10 times reading the docs, I realized
474 * that in the case the receiver NAKs the buffer request,
475 * the hardware retries till shutdown.
476 * This is integrated now in the code above.
484 struct cm_softc *sc = (struct cm_softc *)vsc;
487 cm_srint_locked(vsc);
493 * Arcnet interface receiver soft interrupt:
494 * get the stuff out of any filled buffer we find.
500 struct cm_softc *sc = (struct cm_softc *)vsc;
501 int buffer, len, offset, type;
504 struct arc_header *ah;
509 buffer = sc->sc_rx_act ^ 1;
511 /* Allocate header mbuf */
512 MGETHDR(m, M_DONTWAIT, MT_DATA);
516 * in case s.th. goes wrong with mem, drop it
517 * to make sure the receiver can be started again
518 * count it as input error (we dont have any other
525 m->m_pkthdr.rcvif = ifp;
528 * Align so that IP packet will be longword aligned. Here we
529 * assume that m_data of new packet is longword aligned.
530 * When implementing PHDS, we might have to change it to 2,
531 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
534 cm_ram_ptr = buffer * 512;
535 offset = GETMEM(cm_ram_ptr + 2);
539 offset = GETMEM(cm_ram_ptr + 3);
544 * first +2 bytes for align fixup below
545 * second +2 bytes are for src/dst addresses
547 if ((len + 2 + 2) > MHLEN) {
548 /* attach an mbuf cluster */
549 MCLGET(m, M_DONTWAIT);
551 /* Insist on getting a cluster */
552 if ((m->m_flags & M_EXT) == 0) {
563 type = GETMEM(cm_ram_ptr + offset);
564 m->m_data += 1 + arc_isphds(type);
565 /* mbuf filled with ARCnet addresses */
566 m->m_pkthdr.len = m->m_len = len + 2;
568 ah = mtod(m, struct arc_header *);
569 ah->arc_shost = GETMEM(cm_ram_ptr + 0);
570 ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
572 bus_space_read_region_1(
573 rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
574 cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
588 /* mark buffer as invalid by source id 0 */
589 PUTMEM(buffer << 9, 0);
590 if (--sc->sc_rx_fillcount == 2 - 1) {
592 /* was off, restart it on buffer just emptied */
593 sc->sc_rx_act = buffer;
594 sc->sc_intmask |= CM_RI;
596 /* this also clears the RI flag interrupt: */
597 PUTREG(CMCMD, CM_RXBC(buffer));
598 PUTREG(CMSTAT, sc->sc_intmask);
601 if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
607 cm_tint_locked(sc, isr)
619 buffer = sc->sc_tx_act;
623 * Normal situtations first for fast path:
624 * If acknowledgement received ok or broadcast, we're ok.
628 if (isr & CM_TMA || sc->sc_broadcast[buffer])
631 else if (ifp->if_flags & IFF_LINK2 && sc->sc_timer > 0
632 && --sc->sc_retransmits[buffer] > 0) {
633 /* retransmit same buffer */
634 PUTREG(CMCMD, CM_TX(buffer));
642 /* We know we can accept another buffer at this point. */
643 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
645 if (--sc->sc_tx_fillcount > 0) {
648 * start tx on other buffer.
649 * This also clears the int flag
652 sc->sc_tx_act = buffer;
656 * sc->sc_intmask |= CM_TA;
657 * PUTREG(CMSTAT, sc->sc_intmask);
659 PUTREG(CMCMD, CM_TX(buffer));
660 /* init watchdog timer */
661 sc->sc_timer = ARCTIMEOUT;
663 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
665 "tint: starting tx on buffer %d, status 0x%02x\n",
666 buffer, GETREG(CMSTAT));
669 /* have to disable TX interrupt */
670 sc->sc_intmask &= ~CM_TA;
671 PUTREG(CMSTAT, sc->sc_intmask);
672 /* ... and watchdog timer */
676 if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
683 /* schedule soft int to fill a new buffer for us */
684 softintr_schedule(sc->sc_txcookie);
686 /* call it directly */
687 cm_start_locked(ifp);
692 * Our interrupt routine
698 struct cm_softc *sc = arg;
699 struct ifnet *ifp = sc->sc_ifp;
701 u_char isr, maskedisr;
707 isr = GETREG(CMSTAT);
708 maskedisr = isr & sc->sc_intmask;
709 if (!maskedisr || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
716 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
717 if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
718 isr, sc->sc_intmask);
721 if (maskedisr & CM_POR) {
723 * XXX We should never see this. Don't bother to store
725 * sc->sc_ifp->if_l2com->ac_anaddr = GETMEM(CMMACOFF);
727 PUTREG(CMCMD, CM_CLR(CLR_POR));
729 "%s: intr: got spurious power on reset int\n",
733 if (maskedisr & CM_RECON) {
736 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
738 PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
739 ifp->if_collisions++;
742 * If less than 2 seconds per reconfig:
743 * If ARC_EXCESSIVE_RECONFIGS
744 * since last burst, complain and set treshold for
745 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
747 * This allows for, e.g., new stations on the cable, or
748 * cable switching as long as it is over after
749 * (normally) 16 seconds.
751 * XXX TODO: check timeout bits in status word and
752 * double time if necessary.
755 callout_stop(&sc->sc_recon_ch);
756 newsec = time_second;
757 if ((newsec - sc->sc_recontime <= 2) &&
758 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
760 "%s: excessive token losses, "
764 sc->sc_recontime = newsec;
765 callout_reset(&sc->sc_recon_ch, 15 * hz,
766 cm_reconwatch_locked, (void *)sc);
769 if (maskedisr & CM_RI) {
770 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
771 if_printf(ifp, "intr: hard rint, act %d\n",
775 buffer = sc->sc_rx_act;
776 /* look if buffer is marked invalid: */
777 if (GETMEM(buffer * 512) == 0) {
779 * invalid marked buffer (or illegally
783 "%s: spurious RX interrupt or sender 0 "
784 " (ignored)\n", ifp->if_xname);
786 * restart receiver on same buffer.
787 * XXX maybe better reset interface?
789 PUTREG(CMCMD, CM_RXBC(buffer));
791 if (++sc->sc_rx_fillcount > 1) {
792 sc->sc_intmask &= ~CM_RI;
793 PUTREG(CMSTAT, sc->sc_intmask);
796 sc->sc_rx_act = buffer;
799 * Start receiver on other receive
800 * buffer. This also clears the RI
803 PUTREG(CMCMD, CM_RXBC(buffer));
804 /* in RX intr, so mask is ok for RX */
807 if_printf(ifp, "strt rx for buf %d, "
809 sc->sc_rx_act, GETREG(CMSTAT));
815 * this one starts a soft int to copy out
818 softintr_schedule(sc->sc_rxcookie);
820 /* this one does the copy here */
825 if (maskedisr & CM_TA) {
826 cm_tint_locked(sc, isr);
828 isr = GETREG(CMSTAT);
829 maskedisr = isr & sc->sc_intmask;
831 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
832 if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
833 isr, sc->sc_intmask);
839 cm_reconwatch_locked(arg)
842 struct cm_softc *sc = arg;
843 struct ifnet *ifp = sc->sc_ifp;
845 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
846 sc->sc_reconcount = 0;
847 log(LOG_WARNING, "%s: token valid again.\n",
850 sc->sc_reconcount = 0;
855 * Process an ioctl request.
856 * This code needs some work - it looks pretty ugly.
859 cm_ioctl(ifp, command, data)
870 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
871 if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
880 error = arc_ioctl(ifp, command, data);
885 if ((ifp->if_flags & IFF_UP) == 0 &&
886 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
888 * If interface is marked down and it is running,
892 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
893 } else if ((ifp->if_flags & IFF_UP) != 0 &&
894 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
896 * If interface is marked up and it is stopped, then
913 * watchdog routine for transmitter.
915 * We need this, because else a receiver whose hardware is alive, but whose
916 * software has not enabled the Receiver, would make our hardware wait forever
917 * Discovered this after 20 times reading the docs.
919 * Only thing we do is disable transmitter. We'll get a transmit timeout,
920 * and the int handler will have to decide not to retransmit (in case
921 * retransmission is implemented).
924 cm_watchdog(void *arg)
929 callout_reset(&sc->sc_watchdog_timer, hz, cm_watchdog, sc);
930 if (sc->sc_timer == 0 || --sc->sc_timer > 0)
932 PUTREG(CMCMD, CM_TXDIS);