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(struct ifnet *);
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_watchdog = cm_watchdog;
198 ifp->if_init = cm_init;
199 /* XXX IFQ_SET_READY(&ifp->if_snd); */
200 ifp->if_snd.ifq_maxlen = ifqmaxlen;
202 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
204 arc_ifattach(ifp, linkaddress);
207 sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
208 sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
209 (void (*)(void *))cm_start, ifp);
212 callout_init_mtx(&sc->sc_recon_ch, &sc->sc_mtx, 0);
214 if_printf(ifp, "link addr 0x%02x (%d)\n", linkaddress, linkaddress);
226 struct cm_softc *sc = (struct cm_softc *)xsc;
234 cm_init_locked(struct cm_softc *sc)
236 struct ifnet *ifp = sc->sc_ifp;
238 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
239 ifp->if_drv_flags |= IFF_DRV_RUNNING;
245 * Reset the interface...
247 * Assumes that it is called with sc_mtx held
259 if_printf(ifp, "reset\n");
261 /* stop and restart hardware */
266 } while (!(GETREG(CMSTAT) & CM_POR));
268 linkaddress = GETMEM(CMMACOFF);
270 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
271 if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
272 linkaddress, linkaddress);
275 /* tell the routing level about the (possibly changed) link address */
276 arc_storelladdr(ifp, linkaddress);
279 /* POR is NMI, but we need it below: */
280 sc->sc_intmask = CM_RECON|CM_POR;
281 PUTREG(CMSTAT, sc->sc_intmask);
282 PUTREG(CMCMD, CM_CONF(CONF_LONG));
285 if_printf(ifp, "reset: chip configured, status=0x%02x\n",
288 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
291 if_printf(ifp, "reset: bits cleared, status=0x%02x\n",
295 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
299 sc->sc_intmask |= CM_RI;
300 sc->sc_rx_fillcount = 0;
303 PUTREG(CMCMD, CM_RXBC(2));
304 PUTREG(CMSTAT, sc->sc_intmask);
307 if_printf(ifp, "reset: started receiver, status=0x%02x\n",
311 /* and init transmitter status */
313 sc->sc_tx_fillcount = 0;
315 ifp->if_drv_flags |= IFF_DRV_RUNNING;
316 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
318 cm_start_locked(ifp);
322 * Take interface offline
328 /* Stop the interrupts */
331 /* Stop the interface */
334 /* Stop watchdog timer */
335 sc->sc_ifp->if_timer = 0;
339 cm_start(struct ifnet *ifp)
341 struct cm_softc *sc = ifp->if_softc;
344 cm_start_locked(ifp);
349 * Start output on interface. Get another datagram to send
350 * off the interface queue, and copy it to the
351 * interface becore starting the output
353 * Assumes that sc_mtx is held
359 struct cm_softc *sc = ifp->if_softc;
363 int len, tlen, offset, buffer;
365 u_long copystart, lencopy, perbyte;
368 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
369 if_printf(ifp, "start(%p)\n", ifp);
372 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
375 if (sc->sc_tx_fillcount >= 2)
378 m = arc_frag_next(ifp);
379 buffer = sc->sc_tx_act ^ 1;
385 if (m->m_len < ARC_HDRLEN)
386 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
387 if_printf(ifp, "start: filling %d from %d to %d type %d\n",
388 buffer, mtod(m, u_char *)[0],
389 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
394 cm_ram_ptr = buffer * 512;
399 /* write the addresses to RAM and throw them away */
402 * Hardware does this: Yet Another Microsecond Saved.
403 * (btw, timing code says usually 2 microseconds)
404 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
407 PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
410 /* get total length left at this point */
411 tlen = m->m_pkthdr.len;
412 if (tlen < ARC_MIN_FORBID_LEN) {
414 PUTMEM(cm_ram_ptr + 2, offset);
416 PUTMEM(cm_ram_ptr + 2, 0);
417 if (tlen <= ARC_MAX_FORBID_LEN)
418 offset = 255; /* !!! */
420 if (tlen > ARC_MAX_LEN)
424 PUTMEM(cm_ram_ptr + 3, offset);
427 cm_ram_ptr += offset;
429 /* lets loop through the mbuf chain */
431 for (mp = m; mp; mp = mp->m_next) {
432 if ((len = mp->m_len)) { /* YAMS */
433 bus_space_write_region_1(
434 rman_get_bustag(sc->mem_res),
435 rman_get_bushandle(sc->mem_res),
436 cm_ram_ptr, mtod(mp, caddr_t), len);
442 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
443 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
445 if (++sc->sc_tx_fillcount > 1) {
447 * We are filled up to the rim. No more bufs for the moment,
450 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
453 if_printf(ifp, "start: starting transmitter on buffer %d\n",
456 /* Transmitter was off, start it */
457 sc->sc_tx_act = buffer;
460 * We still can accept another buf, so don't:
461 * ifp->if_drv_flags |= IFF_DRV_OACTIVE;
463 sc->sc_intmask |= CM_TA;
464 PUTREG(CMCMD, CM_TX(buffer));
465 PUTREG(CMSTAT, sc->sc_intmask);
467 ifp->if_timer = ARCTIMEOUT;
472 * After 10 times reading the docs, I realized
473 * that in the case the receiver NAKs the buffer request,
474 * the hardware retries till shutdown.
475 * This is integrated now in the code above.
483 struct cm_softc *sc = (struct cm_softc *)vsc;
486 cm_srint_locked(vsc);
492 * Arcnet interface receiver soft interrupt:
493 * get the stuff out of any filled buffer we find.
499 struct cm_softc *sc = (struct cm_softc *)vsc;
500 int buffer, len, offset, type;
503 struct arc_header *ah;
508 buffer = sc->sc_rx_act ^ 1;
510 /* Allocate header mbuf */
511 MGETHDR(m, M_DONTWAIT, MT_DATA);
515 * in case s.th. goes wrong with mem, drop it
516 * to make sure the receiver can be started again
517 * count it as input error (we dont have any other
524 m->m_pkthdr.rcvif = ifp;
527 * Align so that IP packet will be longword aligned. Here we
528 * assume that m_data of new packet is longword aligned.
529 * When implementing PHDS, we might have to change it to 2,
530 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
533 cm_ram_ptr = buffer * 512;
534 offset = GETMEM(cm_ram_ptr + 2);
538 offset = GETMEM(cm_ram_ptr + 3);
543 * first +2 bytes for align fixup below
544 * second +2 bytes are for src/dst addresses
546 if ((len + 2 + 2) > MHLEN) {
547 /* attach an mbuf cluster */
548 MCLGET(m, M_DONTWAIT);
550 /* Insist on getting a cluster */
551 if ((m->m_flags & M_EXT) == 0) {
562 type = GETMEM(cm_ram_ptr + offset);
563 m->m_data += 1 + arc_isphds(type);
564 /* mbuf filled with ARCnet addresses */
565 m->m_pkthdr.len = m->m_len = len + 2;
567 ah = mtod(m, struct arc_header *);
568 ah->arc_shost = GETMEM(cm_ram_ptr + 0);
569 ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
571 bus_space_read_region_1(
572 rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
573 cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
587 /* mark buffer as invalid by source id 0 */
588 PUTMEM(buffer << 9, 0);
589 if (--sc->sc_rx_fillcount == 2 - 1) {
591 /* was off, restart it on buffer just emptied */
592 sc->sc_rx_act = buffer;
593 sc->sc_intmask |= CM_RI;
595 /* this also clears the RI flag interrupt: */
596 PUTREG(CMCMD, CM_RXBC(buffer));
597 PUTREG(CMSTAT, sc->sc_intmask);
600 if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
606 cm_tint_locked(sc, isr)
618 buffer = sc->sc_tx_act;
622 * Normal situtations first for fast path:
623 * If acknowledgement received ok or broadcast, we're ok.
627 if (isr & CM_TMA || sc->sc_broadcast[buffer])
630 else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
631 && --sc->sc_retransmits[buffer] > 0) {
632 /* retransmit same buffer */
633 PUTREG(CMCMD, CM_TX(buffer));
641 /* We know we can accept another buffer at this point. */
642 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
644 if (--sc->sc_tx_fillcount > 0) {
647 * start tx on other buffer.
648 * This also clears the int flag
651 sc->sc_tx_act = buffer;
655 * sc->sc_intmask |= CM_TA;
656 * PUTREG(CMSTAT, sc->sc_intmask);
658 PUTREG(CMCMD, CM_TX(buffer));
659 /* init watchdog timer */
660 ifp->if_timer = ARCTIMEOUT;
662 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
664 "tint: starting tx on buffer %d, status 0x%02x\n",
665 buffer, GETREG(CMSTAT));
668 /* have to disable TX interrupt */
669 sc->sc_intmask &= ~CM_TA;
670 PUTREG(CMSTAT, sc->sc_intmask);
671 /* ... and watchdog timer */
675 if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
682 /* schedule soft int to fill a new buffer for us */
683 softintr_schedule(sc->sc_txcookie);
685 /* call it directly */
686 cm_start_locked(ifp);
691 * Our interrupt routine
697 struct cm_softc *sc = arg;
698 struct ifnet *ifp = sc->sc_ifp;
700 u_char isr, maskedisr;
706 isr = GETREG(CMSTAT);
707 maskedisr = isr & sc->sc_intmask;
708 if (!maskedisr || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
715 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
716 if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
717 isr, sc->sc_intmask);
720 if (maskedisr & CM_POR) {
722 * XXX We should never see this. Don't bother to store
724 * sc->sc_ifp->if_l2com->ac_anaddr = GETMEM(CMMACOFF);
726 PUTREG(CMCMD, CM_CLR(CLR_POR));
728 "%s: intr: got spurious power on reset int\n",
732 if (maskedisr & CM_RECON) {
735 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
737 PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
738 ifp->if_collisions++;
741 * If less than 2 seconds per reconfig:
742 * If ARC_EXCESSIVE_RECONFIGS
743 * since last burst, complain and set treshold for
744 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
746 * This allows for, e.g., new stations on the cable, or
747 * cable switching as long as it is over after
748 * (normally) 16 seconds.
750 * XXX TODO: check timeout bits in status word and
751 * double time if necessary.
754 callout_stop(&sc->sc_recon_ch);
755 newsec = time_second;
756 if ((newsec - sc->sc_recontime <= 2) &&
757 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
759 "%s: excessive token losses, "
763 sc->sc_recontime = newsec;
764 callout_reset(&sc->sc_recon_ch, 15 * hz,
765 cm_reconwatch_locked, (void *)sc);
768 if (maskedisr & CM_RI) {
769 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
770 if_printf(ifp, "intr: hard rint, act %d\n",
774 buffer = sc->sc_rx_act;
775 /* look if buffer is marked invalid: */
776 if (GETMEM(buffer * 512) == 0) {
778 * invalid marked buffer (or illegally
782 "%s: spurious RX interrupt or sender 0 "
783 " (ignored)\n", ifp->if_xname);
785 * restart receiver on same buffer.
786 * XXX maybe better reset interface?
788 PUTREG(CMCMD, CM_RXBC(buffer));
790 if (++sc->sc_rx_fillcount > 1) {
791 sc->sc_intmask &= ~CM_RI;
792 PUTREG(CMSTAT, sc->sc_intmask);
795 sc->sc_rx_act = buffer;
798 * Start receiver on other receive
799 * buffer. This also clears the RI
802 PUTREG(CMCMD, CM_RXBC(buffer));
803 /* in RX intr, so mask is ok for RX */
806 if_printf(ifp, "strt rx for buf %d, "
808 sc->sc_rx_act, GETREG(CMSTAT));
814 * this one starts a soft int to copy out
817 softintr_schedule(sc->sc_rxcookie);
819 /* this one does the copy here */
824 if (maskedisr & CM_TA) {
825 cm_tint_locked(sc, isr);
827 isr = GETREG(CMSTAT);
828 maskedisr = isr & sc->sc_intmask;
830 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
831 if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
832 isr, sc->sc_intmask);
838 cm_reconwatch_locked(arg)
841 struct cm_softc *sc = arg;
842 struct ifnet *ifp = sc->sc_ifp;
844 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
845 sc->sc_reconcount = 0;
846 log(LOG_WARNING, "%s: token valid again.\n",
849 sc->sc_reconcount = 0;
854 * Process an ioctl request.
855 * This code needs some work - it looks pretty ugly.
858 cm_ioctl(ifp, command, data)
869 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
870 if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
879 error = arc_ioctl(ifp, command, data);
884 if ((ifp->if_flags & IFF_UP) == 0 &&
885 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
887 * If interface is marked down and it is running,
891 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
892 } else if ((ifp->if_flags & IFF_UP) != 0 &&
893 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
895 * If interface is marked up and it is stopped, then
912 * watchdog routine for transmitter.
914 * We need this, because else a receiver whose hardware is alive, but whose
915 * software has not enabled the Receiver, would make our hardware wait forever
916 * Discovered this after 20 times reading the docs.
918 * Only thing we do is disable transmitter. We'll get a transmit timeout,
919 * and the int handler will have to decide not to retransmit (in case
920 * retransmission is implemented).
926 struct cm_softc *sc = ifp->if_softc;
929 PUTREG(CMCMD, CM_TXDIS);