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_deactivate_resource(dev, SYS_RES_IOPORT,
144 bus_release_resource(dev, SYS_RES_IOPORT,
148 if (sc->mem_res != NULL) {
149 bus_deactivate_resource(dev, SYS_RES_MEMORY,
151 bus_release_resource(dev, SYS_RES_MEMORY,
155 if (sc->irq_res != NULL) {
156 bus_deactivate_resource(dev, SYS_RES_IRQ,
158 bus_release_resource(dev, SYS_RES_IRQ,
168 struct cm_softc *sc = device_get_softc(dev);
170 u_int8_t linkaddress;
172 ifp = sc->sc_ifp = if_alloc(IFT_ARCNET);
177 * read the arcnet address from the board
182 } while (!(GETREG(CMSTAT) & CM_POR));
183 linkaddress = GETMEM(CMMACOFF);
185 /* clear the int mask... */
189 PUTREG(CMCMD, CM_CONF(CONF_LONG));
190 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
191 sc->sc_recontime = sc->sc_reconcount = 0;
194 * set interface to stopped condition (reset)
199 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
200 ifp->if_output = arc_output;
201 ifp->if_start = cm_start;
202 ifp->if_ioctl = cm_ioctl;
203 ifp->if_watchdog = cm_watchdog;
204 ifp->if_init = cm_init;
205 /* XXX IFQ_SET_READY(&ifp->if_snd); */
206 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
208 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
210 arc_ifattach(ifp, linkaddress);
213 sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
214 sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
215 (void (*)(void *))cm_start, ifp);
218 callout_init_mtx(&sc->sc_recon_ch, &sc->sc_mtx, 0);
220 if_printf(ifp, "link addr 0x%02x (%d)\n", linkaddress, linkaddress);
232 struct cm_softc *sc = (struct cm_softc *)xsc;
240 cm_init_locked(struct cm_softc *sc)
242 struct ifnet *ifp = sc->sc_ifp;
244 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
245 ifp->if_drv_flags |= IFF_DRV_RUNNING;
251 * Reset the interface...
253 * Assumes that it is called with sc_mtx held
265 if_printf(ifp, "reset\n");
267 /* stop and restart hardware */
272 } while (!(GETREG(CMSTAT) & CM_POR));
274 linkaddress = GETMEM(CMMACOFF);
276 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
277 if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
278 linkaddress, linkaddress);
281 /* tell the routing level about the (possibly changed) link address */
282 arc_storelladdr(ifp, linkaddress);
285 /* POR is NMI, but we need it below: */
286 sc->sc_intmask = CM_RECON|CM_POR;
287 PUTREG(CMSTAT, sc->sc_intmask);
288 PUTREG(CMCMD, CM_CONF(CONF_LONG));
291 if_printf(ifp, "reset: chip configured, status=0x%02x\n",
294 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
297 if_printf(ifp, "reset: bits cleared, status=0x%02x\n",
301 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
305 sc->sc_intmask |= CM_RI;
306 sc->sc_rx_fillcount = 0;
309 PUTREG(CMCMD, CM_RXBC(2));
310 PUTREG(CMSTAT, sc->sc_intmask);
313 if_printf(ifp, "reset: started receiver, status=0x%02x\n",
317 /* and init transmitter status */
319 sc->sc_tx_fillcount = 0;
321 ifp->if_drv_flags |= IFF_DRV_RUNNING;
322 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
324 cm_start_locked(ifp);
328 * Take interface offline
334 /* Stop the interrupts */
337 /* Stop the interface */
340 /* Stop watchdog timer */
341 sc->sc_ifp->if_timer = 0;
345 cm_start(struct ifnet *ifp)
347 struct cm_softc *sc = ifp->if_softc;
350 cm_start_locked(ifp);
355 * Start output on interface. Get another datagram to send
356 * off the interface queue, and copy it to the
357 * interface becore starting the output
359 * Assumes that sc_mtx is held
365 struct cm_softc *sc = ifp->if_softc;
369 int len, tlen, offset, buffer;
371 u_long copystart, lencopy, perbyte;
374 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
375 if_printf(ifp, "start(%p)\n", ifp);
378 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
381 if (sc->sc_tx_fillcount >= 2)
384 m = arc_frag_next(ifp);
385 buffer = sc->sc_tx_act ^ 1;
391 if (m->m_len < ARC_HDRLEN)
392 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
393 if_printf(ifp, "start: filling %d from %d to %d type %d\n",
394 buffer, mtod(m, u_char *)[0],
395 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
400 cm_ram_ptr = buffer * 512;
405 /* write the addresses to RAM and throw them away */
408 * Hardware does this: Yet Another Microsecond Saved.
409 * (btw, timing code says usually 2 microseconds)
410 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
413 PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
416 /* get total length left at this point */
417 tlen = m->m_pkthdr.len;
418 if (tlen < ARC_MIN_FORBID_LEN) {
420 PUTMEM(cm_ram_ptr + 2, offset);
422 PUTMEM(cm_ram_ptr + 2, 0);
423 if (tlen <= ARC_MAX_FORBID_LEN)
424 offset = 255; /* !!! */
426 if (tlen > ARC_MAX_LEN)
430 PUTMEM(cm_ram_ptr + 3, offset);
433 cm_ram_ptr += offset;
435 /* lets loop through the mbuf chain */
437 for (mp = m; mp; mp = mp->m_next) {
438 if ((len = mp->m_len)) { /* YAMS */
439 bus_space_write_region_1(
440 rman_get_bustag(sc->mem_res),
441 rman_get_bushandle(sc->mem_res),
442 cm_ram_ptr, mtod(mp, caddr_t), len);
448 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
449 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
451 if (++sc->sc_tx_fillcount > 1) {
453 * We are filled up to the rim. No more bufs for the moment,
456 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
459 if_printf(ifp, "start: starting transmitter on buffer %d\n",
462 /* Transmitter was off, start it */
463 sc->sc_tx_act = buffer;
466 * We still can accept another buf, so don't:
467 * ifp->if_drv_flags |= IFF_DRV_OACTIVE;
469 sc->sc_intmask |= CM_TA;
470 PUTREG(CMCMD, CM_TX(buffer));
471 PUTREG(CMSTAT, sc->sc_intmask);
473 ifp->if_timer = ARCTIMEOUT;
478 * After 10 times reading the docs, I realized
479 * that in the case the receiver NAKs the buffer request,
480 * the hardware retries till shutdown.
481 * This is integrated now in the code above.
489 struct cm_softc *sc = (struct cm_softc *)vsc;
492 cm_srint_locked(vsc);
498 * Arcnet interface receiver soft interrupt:
499 * get the stuff out of any filled buffer we find.
505 struct cm_softc *sc = (struct cm_softc *)vsc;
506 int buffer, len, offset, type;
509 struct arc_header *ah;
514 buffer = sc->sc_rx_act ^ 1;
516 /* Allocate header mbuf */
517 MGETHDR(m, M_DONTWAIT, MT_DATA);
521 * in case s.th. goes wrong with mem, drop it
522 * to make sure the receiver can be started again
523 * count it as input error (we dont have any other
530 m->m_pkthdr.rcvif = ifp;
533 * Align so that IP packet will be longword aligned. Here we
534 * assume that m_data of new packet is longword aligned.
535 * When implementing PHDS, we might have to change it to 2,
536 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
539 cm_ram_ptr = buffer * 512;
540 offset = GETMEM(cm_ram_ptr + 2);
544 offset = GETMEM(cm_ram_ptr + 3);
549 * first +2 bytes for align fixup below
550 * second +2 bytes are for src/dst addresses
552 if ((len + 2 + 2) > MHLEN) {
553 /* attach an mbuf cluster */
554 MCLGET(m, M_DONTWAIT);
556 /* Insist on getting a cluster */
557 if ((m->m_flags & M_EXT) == 0) {
568 type = GETMEM(cm_ram_ptr + offset);
569 m->m_data += 1 + arc_isphds(type);
570 /* mbuf filled with ARCnet addresses */
571 m->m_pkthdr.len = m->m_len = len + 2;
573 ah = mtod(m, struct arc_header *);
574 ah->arc_shost = GETMEM(cm_ram_ptr + 0);
575 ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
577 bus_space_read_region_1(
578 rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
579 cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
593 /* mark buffer as invalid by source id 0 */
594 PUTMEM(buffer << 9, 0);
595 if (--sc->sc_rx_fillcount == 2 - 1) {
597 /* was off, restart it on buffer just emptied */
598 sc->sc_rx_act = buffer;
599 sc->sc_intmask |= CM_RI;
601 /* this also clears the RI flag interupt: */
602 PUTREG(CMCMD, CM_RXBC(buffer));
603 PUTREG(CMSTAT, sc->sc_intmask);
606 if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
612 cm_tint_locked(sc, isr)
624 buffer = sc->sc_tx_act;
628 * Normal situtations first for fast path:
629 * If acknowledgement received ok or broadcast, we're ok.
633 if (isr & CM_TMA || sc->sc_broadcast[buffer])
636 else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
637 && --sc->sc_retransmits[buffer] > 0) {
638 /* retransmit same buffer */
639 PUTREG(CMCMD, CM_TX(buffer));
647 /* We know we can accept another buffer at this point. */
648 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
650 if (--sc->sc_tx_fillcount > 0) {
653 * start tx on other buffer.
654 * This also clears the int flag
657 sc->sc_tx_act = buffer;
661 * sc->sc_intmask |= CM_TA;
662 * PUTREG(CMSTAT, sc->sc_intmask);
664 PUTREG(CMCMD, CM_TX(buffer));
665 /* init watchdog timer */
666 ifp->if_timer = ARCTIMEOUT;
668 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
670 "tint: starting tx on buffer %d, status 0x%02x\n",
671 buffer, GETREG(CMSTAT));
674 /* have to disable TX interrupt */
675 sc->sc_intmask &= ~CM_TA;
676 PUTREG(CMSTAT, sc->sc_intmask);
677 /* ... and watchdog timer */
681 if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
688 /* schedule soft int to fill a new buffer for us */
689 softintr_schedule(sc->sc_txcookie);
691 /* call it directly */
692 cm_start_locked(ifp);
697 * Our interrupt routine
703 struct cm_softc *sc = arg;
704 struct ifnet *ifp = sc->sc_ifp;
706 u_char isr, maskedisr;
712 isr = GETREG(CMSTAT);
713 maskedisr = isr & sc->sc_intmask;
714 if (!maskedisr || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
721 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
722 if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
723 isr, sc->sc_intmask);
726 if (maskedisr & CM_POR) {
728 * XXX We should never see this. Don't bother to store
730 * sc->sc_ifp->if_l2com->ac_anaddr = GETMEM(CMMACOFF);
732 PUTREG(CMCMD, CM_CLR(CLR_POR));
734 "%s: intr: got spurious power on reset int\n",
738 if (maskedisr & CM_RECON) {
741 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
743 PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
744 ifp->if_collisions++;
747 * If less than 2 seconds per reconfig:
748 * If ARC_EXCESSIVE_RECONFIGS
749 * since last burst, complain and set treshold for
750 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
752 * This allows for, e.g., new stations on the cable, or
753 * cable switching as long as it is over after
754 * (normally) 16 seconds.
756 * XXX TODO: check timeout bits in status word and
757 * double time if necessary.
760 callout_stop(&sc->sc_recon_ch);
761 newsec = time_second;
762 if ((newsec - sc->sc_recontime <= 2) &&
763 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
765 "%s: excessive token losses, "
769 sc->sc_recontime = newsec;
770 callout_reset(&sc->sc_recon_ch, 15 * hz,
771 cm_reconwatch_locked, (void *)sc);
774 if (maskedisr & CM_RI) {
775 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
776 if_printf(ifp, "intr: hard rint, act %d\n",
780 buffer = sc->sc_rx_act;
781 /* look if buffer is marked invalid: */
782 if (GETMEM(buffer * 512) == 0) {
784 * invalid marked buffer (or illegally
788 "%s: spurious RX interupt or sender 0 "
789 " (ignored)\n", ifp->if_xname);
791 * restart receiver on same buffer.
792 * XXX maybe better reset interface?
794 PUTREG(CMCMD, CM_RXBC(buffer));
796 if (++sc->sc_rx_fillcount > 1) {
797 sc->sc_intmask &= ~CM_RI;
798 PUTREG(CMSTAT, sc->sc_intmask);
801 sc->sc_rx_act = buffer;
804 * Start receiver on other receive
805 * buffer. This also clears the RI
808 PUTREG(CMCMD, CM_RXBC(buffer));
809 /* in RX intr, so mask is ok for RX */
812 if_printf(ifp, "strt rx for buf %d, "
814 sc->sc_rx_act, GETREG(CMSTAT));
820 * this one starts a soft int to copy out
823 softintr_schedule(sc->sc_rxcookie);
825 /* this one does the copy here */
830 if (maskedisr & CM_TA) {
831 cm_tint_locked(sc, isr);
833 isr = GETREG(CMSTAT);
834 maskedisr = isr & sc->sc_intmask;
836 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
837 if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
838 isr, sc->sc_intmask);
844 cm_reconwatch_locked(arg)
847 struct cm_softc *sc = arg;
848 struct ifnet *ifp = sc->sc_ifp;
850 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
851 sc->sc_reconcount = 0;
852 log(LOG_WARNING, "%s: token valid again.\n",
855 sc->sc_reconcount = 0;
860 * Process an ioctl request.
861 * This code needs some work - it looks pretty ugly.
864 cm_ioctl(ifp, command, data)
876 ifa = (struct ifaddr *)data;
877 ifr = (struct ifreq *)data;
879 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
880 if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
889 error = arc_ioctl(ifp, command, data);
894 if ((ifp->if_flags & IFF_UP) == 0 &&
895 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
897 * If interface is marked down and it is running,
901 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
902 } else if ((ifp->if_flags & IFF_UP) != 0 &&
903 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
905 * If interface is marked up and it is stopped, then
922 * watchdog routine for transmitter.
924 * We need this, because else a receiver whose hardware is alive, but whose
925 * software has not enabled the Receiver, would make our hardware wait forever
926 * Discovered this after 20 times reading the docs.
928 * Only thing we do is disable transmitter. We'll get a transmit timeout,
929 * and the int handler will have to decide not to retransmit (in case
930 * retransmission is implemented).
936 struct cm_softc *sc = ifp->if_softc;
939 PUTREG(CMCMD, CM_TXDIS);