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.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
37 * compatibility mode) boards
40 /* #define CMSOFTCOPY */
41 #define CMRETRANSMIT /**/
42 /* #define CM_DEBUG */
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sockio.h>
48 #include <sys/module.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
54 #include <machine/bus.h>
56 #include <machine/resource.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/if_arc.h>
63 #include <dev/cm/smc90cx6reg.h>
64 #include <dev/cm/smc90cx6var.h>
66 MODULE_DEPEND(if_cm, arcnet, 1, 1, 1);
68 /* these should be elsewhere */
71 #define ARC_MIN_FORBID_LEN 254
72 #define ARC_MAX_FORBID_LEN 256
73 #define ARC_MAX_LEN 508
74 #define ARC_ADDR_LEN 1
76 /* for watchdog timer. This should be more than enough. */
77 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
79 devclass_t cm_devclass;
82 * This currently uses 2 bufs for tx, 2 for rx
86 * rx has a fillcount variable. If fillcount > (NRXBUF-1),
87 * rx can be switched off from rx hard int.
88 * Else rx is restarted on the other receiver.
89 * rx soft int counts down. if it is == (NRXBUF-1), it restarts
91 * To ensure packet ordering (we need that for 1201 later), we have a counter
92 * which is incremented modulo 256 on each receive and a per buffer
93 * variable, which is set to the counter on filling. The soft int can
94 * compare both values to determine the older packet.
98 * cm_start checks tx_fillcount
101 * else fill tx_act ^ 1 && inc tx_fillcount
103 * check tx_fillcount again.
104 * case 2: set IFF_DRV_OACTIVE to stop arc_output from filling us.
107 * tint clears IFF_OACTIVE, decrements and checks tx_fillcount
108 * case 1: start tx on tx_act ^ 1, softcall cm_start
109 * case 0: softcall cm_start
111 * #define fill(i) get mbuf && copy mbuf to chip(i)
114 void cm_init(void *);
115 static void cm_init_locked(struct cm_softc *);
116 static void cm_reset_locked(struct cm_softc *);
117 void cm_start(struct ifnet *);
118 void cm_start_locked(struct ifnet *);
119 int cm_ioctl(struct ifnet *, unsigned long, caddr_t);
120 void cm_watchdog(void *);
121 void cm_srint_locked(void *vsc);
122 static void cm_tint_locked(struct cm_softc *, int);
123 void cm_reconwatch_locked(void *);
126 * Release all resources
129 cm_release_resources(dev)
132 struct cm_softc *sc = device_get_softc(dev);
134 if (sc->port_res != NULL) {
135 bus_release_resource(dev, SYS_RES_IOPORT,
139 if (sc->mem_res != NULL) {
140 bus_release_resource(dev, SYS_RES_MEMORY,
144 if (sc->irq_res != NULL) {
145 bus_release_resource(dev, SYS_RES_IRQ,
155 struct cm_softc *sc = device_get_softc(dev);
157 u_int8_t linkaddress;
159 ifp = sc->sc_ifp = if_alloc(IFT_ARCNET);
164 * read the arcnet address from the board
169 } while (!(GETREG(CMSTAT) & CM_POR));
170 linkaddress = GETMEM(CMMACOFF);
172 /* clear the int mask... */
176 PUTREG(CMCMD, CM_CONF(CONF_LONG));
177 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
178 sc->sc_recontime = sc->sc_reconcount = 0;
181 * set interface to stopped condition (reset)
186 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
187 ifp->if_output = arc_output;
188 ifp->if_start = cm_start;
189 ifp->if_ioctl = cm_ioctl;
190 ifp->if_init = cm_init;
191 /* XXX IFQ_SET_READY(&ifp->if_snd); */
192 ifp->if_snd.ifq_maxlen = ifqmaxlen;
193 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
195 arc_ifattach(ifp, linkaddress);
198 sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
199 sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
200 (void (*)(void *))cm_start, ifp);
203 callout_init_mtx(&sc->sc_recon_ch, &sc->sc_mtx, 0);
204 callout_init_mtx(&sc->sc_watchdog_timer, &sc->sc_mtx, 0);
206 if_printf(ifp, "link addr 0x%02x (%d)\n", linkaddress, linkaddress);
218 struct cm_softc *sc = (struct cm_softc *)xsc;
226 cm_init_locked(struct cm_softc *sc)
228 struct ifnet *ifp = sc->sc_ifp;
230 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
231 ifp->if_drv_flags |= IFF_DRV_RUNNING;
237 * Reset the interface...
239 * Assumes that it is called with sc_mtx held
251 if_printf(ifp, "reset\n");
253 /* stop and restart hardware */
258 } while (!(GETREG(CMSTAT) & CM_POR));
260 linkaddress = GETMEM(CMMACOFF);
262 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
263 if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
264 linkaddress, linkaddress);
267 /* tell the routing level about the (possibly changed) link address */
268 arc_storelladdr(ifp, linkaddress);
271 /* POR is NMI, but we need it below: */
272 sc->sc_intmask = CM_RECON|CM_POR;
273 PUTREG(CMSTAT, sc->sc_intmask);
274 PUTREG(CMCMD, CM_CONF(CONF_LONG));
277 if_printf(ifp, "reset: chip configured, status=0x%02x\n",
280 PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
283 if_printf(ifp, "reset: bits cleared, status=0x%02x\n",
287 sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
291 sc->sc_intmask |= CM_RI;
292 sc->sc_rx_fillcount = 0;
295 PUTREG(CMCMD, CM_RXBC(2));
296 PUTREG(CMSTAT, sc->sc_intmask);
299 if_printf(ifp, "reset: started receiver, status=0x%02x\n",
303 /* and init transmitter status */
305 sc->sc_tx_fillcount = 0;
307 ifp->if_drv_flags |= IFF_DRV_RUNNING;
308 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
310 callout_reset(&sc->sc_watchdog_timer, hz, cm_watchdog, sc);
311 cm_start_locked(ifp);
315 * Take interface offline
321 /* Stop the interrupts */
324 /* Stop the interface */
327 /* Stop watchdog timer */
328 callout_stop(&sc->sc_watchdog_timer);
333 cm_start(struct ifnet *ifp)
335 struct cm_softc *sc = ifp->if_softc;
338 cm_start_locked(ifp);
343 * Start output on interface. Get another datagram to send
344 * off the interface queue, and copy it to the
345 * interface becore starting the output
347 * Assumes that sc_mtx is held
353 struct cm_softc *sc = ifp->if_softc;
357 int len, tlen, offset, buffer;
359 u_long copystart, lencopy, perbyte;
362 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
363 if_printf(ifp, "start(%p)\n", ifp);
366 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
369 if (sc->sc_tx_fillcount >= 2)
372 m = arc_frag_next(ifp);
373 buffer = sc->sc_tx_act ^ 1;
379 if (m->m_len < ARC_HDRLEN)
380 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
381 if_printf(ifp, "start: filling %d from %d to %d type %d\n",
382 buffer, mtod(m, u_char *)[0],
383 mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
388 cm_ram_ptr = buffer * 512;
393 /* write the addresses to RAM and throw them away */
396 * Hardware does this: Yet Another Microsecond Saved.
397 * (btw, timing code says usually 2 microseconds)
398 * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
401 PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
404 /* get total length left at this point */
405 tlen = m->m_pkthdr.len;
406 if (tlen < ARC_MIN_FORBID_LEN) {
408 PUTMEM(cm_ram_ptr + 2, offset);
410 PUTMEM(cm_ram_ptr + 2, 0);
411 if (tlen <= ARC_MAX_FORBID_LEN)
412 offset = 255; /* !!! */
414 if (tlen > ARC_MAX_LEN)
418 PUTMEM(cm_ram_ptr + 3, offset);
421 cm_ram_ptr += offset;
423 /* lets loop through the mbuf chain */
425 for (mp = m; mp; mp = mp->m_next) {
426 if ((len = mp->m_len)) { /* YAMS */
427 bus_space_write_region_1(
428 rman_get_bustag(sc->mem_res),
429 rman_get_bushandle(sc->mem_res),
430 cm_ram_ptr, mtod(mp, caddr_t), len);
436 sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
437 sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
439 if (++sc->sc_tx_fillcount > 1) {
441 * We are filled up to the rim. No more bufs for the moment,
444 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
447 if_printf(ifp, "start: starting transmitter on buffer %d\n",
450 /* Transmitter was off, start it */
451 sc->sc_tx_act = buffer;
454 * We still can accept another buf, so don't:
455 * ifp->if_drv_flags |= IFF_DRV_OACTIVE;
457 sc->sc_intmask |= CM_TA;
458 PUTREG(CMCMD, CM_TX(buffer));
459 PUTREG(CMSTAT, sc->sc_intmask);
461 sc->sc_timer = ARCTIMEOUT;
466 * After 10 times reading the docs, I realized
467 * that in the case the receiver NAKs the buffer request,
468 * the hardware retries till shutdown.
469 * This is integrated now in the code above.
477 struct cm_softc *sc = (struct cm_softc *)vsc;
480 cm_srint_locked(vsc);
486 * Arcnet interface receiver soft interrupt:
487 * get the stuff out of any filled buffer we find.
493 struct cm_softc *sc = (struct cm_softc *)vsc;
494 int buffer, len, offset, type;
497 struct arc_header *ah;
502 buffer = sc->sc_rx_act ^ 1;
504 /* Allocate header mbuf */
505 MGETHDR(m, M_NOWAIT, MT_DATA);
509 * in case s.th. goes wrong with mem, drop it
510 * to make sure the receiver can be started again
511 * count it as input error (we dont have any other
518 m->m_pkthdr.rcvif = ifp;
521 * Align so that IP packet will be longword aligned. Here we
522 * assume that m_data of new packet is longword aligned.
523 * When implementing PHDS, we might have to change it to 2,
524 * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
527 cm_ram_ptr = buffer * 512;
528 offset = GETMEM(cm_ram_ptr + 2);
532 offset = GETMEM(cm_ram_ptr + 3);
537 * first +2 bytes for align fixup below
538 * second +2 bytes are for src/dst addresses
540 if ((len + 2 + 2) > MHLEN) {
541 /* attach an mbuf cluster */
544 /* Insist on getting a cluster */
545 if ((m->m_flags & M_EXT) == 0) {
556 type = GETMEM(cm_ram_ptr + offset);
557 m->m_data += 1 + arc_isphds(type);
558 /* mbuf filled with ARCnet addresses */
559 m->m_pkthdr.len = m->m_len = len + 2;
561 ah = mtod(m, struct arc_header *);
562 ah->arc_shost = GETMEM(cm_ram_ptr + 0);
563 ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
565 bus_space_read_region_1(
566 rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
567 cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
581 /* mark buffer as invalid by source id 0 */
582 PUTMEM(buffer << 9, 0);
583 if (--sc->sc_rx_fillcount == 2 - 1) {
585 /* was off, restart it on buffer just emptied */
586 sc->sc_rx_act = buffer;
587 sc->sc_intmask |= CM_RI;
589 /* this also clears the RI flag interrupt: */
590 PUTREG(CMCMD, CM_RXBC(buffer));
591 PUTREG(CMSTAT, sc->sc_intmask);
594 if_printf(ifp, "srint: restarted rx on buf %d\n", buffer);
600 cm_tint_locked(sc, isr)
612 buffer = sc->sc_tx_act;
616 * Normal situtations first for fast path:
617 * If acknowledgement received ok or broadcast, we're ok.
621 if (isr & CM_TMA || sc->sc_broadcast[buffer])
624 else if (ifp->if_flags & IFF_LINK2 && sc->sc_timer > 0
625 && --sc->sc_retransmits[buffer] > 0) {
626 /* retransmit same buffer */
627 PUTREG(CMCMD, CM_TX(buffer));
635 /* We know we can accept another buffer at this point. */
636 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
638 if (--sc->sc_tx_fillcount > 0) {
641 * start tx on other buffer.
642 * This also clears the int flag
645 sc->sc_tx_act = buffer;
649 * sc->sc_intmask |= CM_TA;
650 * PUTREG(CMSTAT, sc->sc_intmask);
652 PUTREG(CMCMD, CM_TX(buffer));
653 /* init watchdog timer */
654 sc->sc_timer = ARCTIMEOUT;
656 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
658 "tint: starting tx on buffer %d, status 0x%02x\n",
659 buffer, GETREG(CMSTAT));
662 /* have to disable TX interrupt */
663 sc->sc_intmask &= ~CM_TA;
664 PUTREG(CMSTAT, sc->sc_intmask);
665 /* ... and watchdog timer */
669 if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
676 /* schedule soft int to fill a new buffer for us */
677 softintr_schedule(sc->sc_txcookie);
679 /* call it directly */
680 cm_start_locked(ifp);
685 * Our interrupt routine
691 struct cm_softc *sc = arg;
692 struct ifnet *ifp = sc->sc_ifp;
694 u_char isr, maskedisr;
700 isr = GETREG(CMSTAT);
701 maskedisr = isr & sc->sc_intmask;
702 if (!maskedisr || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
709 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
710 if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
711 isr, sc->sc_intmask);
714 if (maskedisr & CM_POR) {
716 * XXX We should never see this. Don't bother to store
718 * sc->sc_ifp->if_l2com->ac_anaddr = GETMEM(CMMACOFF);
720 PUTREG(CMCMD, CM_CLR(CLR_POR));
722 "%s: intr: got spurious power on reset int\n",
726 if (maskedisr & CM_RECON) {
729 * PUTREG(CMCMD, CM_CONF(CONF_LONG));
731 PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
732 ifp->if_collisions++;
735 * If less than 2 seconds per reconfig:
736 * If ARC_EXCESSIVE_RECONFIGS
737 * since last burst, complain and set treshold for
738 * warnings to ARC_EXCESSIVE_RECONS_REWARN.
740 * This allows for, e.g., new stations on the cable, or
741 * cable switching as long as it is over after
742 * (normally) 16 seconds.
744 * XXX TODO: check timeout bits in status word and
745 * double time if necessary.
748 callout_stop(&sc->sc_recon_ch);
749 newsec = time_second;
750 if ((newsec - sc->sc_recontime <= 2) &&
751 (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
753 "%s: excessive token losses, "
757 sc->sc_recontime = newsec;
758 callout_reset(&sc->sc_recon_ch, 15 * hz,
759 cm_reconwatch_locked, (void *)sc);
762 if (maskedisr & CM_RI) {
763 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
764 if_printf(ifp, "intr: hard rint, act %d\n",
768 buffer = sc->sc_rx_act;
769 /* look if buffer is marked invalid: */
770 if (GETMEM(buffer * 512) == 0) {
772 * invalid marked buffer (or illegally
776 "%s: spurious RX interrupt or sender 0 "
777 " (ignored)\n", ifp->if_xname);
779 * restart receiver on same buffer.
780 * XXX maybe better reset interface?
782 PUTREG(CMCMD, CM_RXBC(buffer));
784 if (++sc->sc_rx_fillcount > 1) {
785 sc->sc_intmask &= ~CM_RI;
786 PUTREG(CMSTAT, sc->sc_intmask);
789 sc->sc_rx_act = buffer;
792 * Start receiver on other receive
793 * buffer. This also clears the RI
796 PUTREG(CMCMD, CM_RXBC(buffer));
797 /* in RX intr, so mask is ok for RX */
800 if_printf(ifp, "strt rx for buf %d, "
802 sc->sc_rx_act, GETREG(CMSTAT));
808 * this one starts a soft int to copy out
811 softintr_schedule(sc->sc_rxcookie);
813 /* this one does the copy here */
818 if (maskedisr & CM_TA) {
819 cm_tint_locked(sc, isr);
821 isr = GETREG(CMSTAT);
822 maskedisr = isr & sc->sc_intmask;
824 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
825 if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
826 isr, sc->sc_intmask);
832 cm_reconwatch_locked(arg)
835 struct cm_softc *sc = arg;
836 struct ifnet *ifp = sc->sc_ifp;
838 if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
839 sc->sc_reconcount = 0;
840 log(LOG_WARNING, "%s: token valid again.\n",
843 sc->sc_reconcount = 0;
848 * Process an ioctl request.
849 * This code needs some work - it looks pretty ugly.
852 cm_ioctl(ifp, command, data)
863 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
864 if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
873 error = arc_ioctl(ifp, command, data);
878 if ((ifp->if_flags & IFF_UP) == 0 &&
879 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
881 * If interface is marked down and it is running,
885 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
886 } else if ((ifp->if_flags & IFF_UP) != 0 &&
887 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
889 * If interface is marked up and it is stopped, then
906 * watchdog routine for transmitter.
908 * We need this, because else a receiver whose hardware is alive, but whose
909 * software has not enabled the Receiver, would make our hardware wait forever
910 * Discovered this after 20 times reading the docs.
912 * Only thing we do is disable transmitter. We'll get a transmit timeout,
913 * and the int handler will have to decide not to retransmit (in case
914 * retransmission is implemented).
917 cm_watchdog(void *arg)
922 callout_reset(&sc->sc_watchdog_timer, hz, cm_watchdog, sc);
923 if (sc->sc_timer == 0 || --sc->sc_timer > 0)
925 PUTREG(CMCMD, CM_TXDIS);