Home | History | Annotate | Line # | Download | only in ic
rtl81x9.c revision 1.16
      1  1.16   thorpej /*	$NetBSD: rtl81x9.c,v 1.16 2000/10/15 19:59:22 thorpej Exp $	*/
      2   1.1      haya 
      3   1.1      haya /*
      4   1.1      haya  * Copyright (c) 1997, 1998
      5   1.1      haya  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6   1.1      haya  *
      7   1.1      haya  * Redistribution and use in source and binary forms, with or without
      8   1.1      haya  * modification, are permitted provided that the following conditions
      9   1.1      haya  * are met:
     10   1.1      haya  * 1. Redistributions of source code must retain the above copyright
     11   1.1      haya  *    notice, this list of conditions and the following disclaimer.
     12   1.1      haya  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      haya  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      haya  *    documentation and/or other materials provided with the distribution.
     15   1.1      haya  * 3. All advertising materials mentioning features or use of this software
     16   1.1      haya  *    must display the following acknowledgement:
     17   1.1      haya  *	This product includes software developed by Bill Paul.
     18   1.1      haya  * 4. Neither the name of the author nor the names of any co-contributors
     19   1.1      haya  *    may be used to endorse or promote products derived from this software
     20   1.1      haya  *    without specific prior written permission.
     21   1.1      haya  *
     22   1.1      haya  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23   1.1      haya  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1      haya  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1      haya  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26   1.1      haya  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27   1.1      haya  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28   1.1      haya  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29   1.1      haya  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30   1.1      haya  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31   1.1      haya  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32   1.1      haya  * THE POSSIBILITY OF SUCH DAMAGE.
     33   1.1      haya  *
     34   1.1      haya  *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
     35   1.1      haya  */
     36   1.1      haya 
     37   1.1      haya /*
     38   1.1      haya  * RealTek 8129/8139 PCI NIC driver
     39   1.1      haya  *
     40   1.1      haya  * Supports several extremely cheap PCI 10/100 adapters based on
     41   1.1      haya  * the RealTek chipset. Datasheets can be obtained from
     42   1.1      haya  * www.realtek.com.tw.
     43   1.1      haya  *
     44   1.1      haya  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     45   1.1      haya  * Electrical Engineering Department
     46   1.1      haya  * Columbia University, New York City
     47   1.1      haya  */
     48   1.1      haya 
     49   1.1      haya /*
     50   1.1      haya  * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
     51   1.1      haya  * probably the worst PCI ethernet controller ever made, with the possible
     52   1.1      haya  * exception of the FEAST chip made by SMC. The 8139 supports bus-master
     53   1.1      haya  * DMA, but it has a terrible interface that nullifies any performance
     54   1.1      haya  * gains that bus-master DMA usually offers.
     55   1.1      haya  *
     56   1.1      haya  * For transmission, the chip offers a series of four TX descriptor
     57   1.1      haya  * registers. Each transmit frame must be in a contiguous buffer, aligned
     58   1.1      haya  * on a longword (32-bit) boundary. This means we almost always have to
     59   1.1      haya  * do mbuf copies in order to transmit a frame, except in the unlikely
     60   1.1      haya  * case where a) the packet fits into a single mbuf, and b) the packet
     61   1.1      haya  * is 32-bit aligned within the mbuf's data area. The presence of only
     62   1.1      haya  * four descriptor registers means that we can never have more than four
     63   1.1      haya  * packets queued for transmission at any one time.
     64   1.1      haya  *
     65   1.1      haya  * Reception is not much better. The driver has to allocate a single large
     66   1.1      haya  * buffer area (up to 64K in size) into which the chip will DMA received
     67   1.1      haya  * frames. Because we don't know where within this region received packets
     68   1.1      haya  * will begin or end, we have no choice but to copy data from the buffer
     69   1.1      haya  * area into mbufs in order to pass the packets up to the higher protocol
     70   1.1      haya  * levels.
     71   1.1      haya  *
     72   1.1      haya  * It's impossible given this rotten design to really achieve decent
     73   1.1      haya  * performance at 100Mbps, unless you happen to have a 400Mhz PII or
     74   1.1      haya  * some equally overmuscled CPU to drive it.
     75   1.1      haya  *
     76   1.1      haya  * On the bright side, the 8139 does have a built-in PHY, although
     77   1.1      haya  * rather than using an MDIO serial interface like most other NICs, the
     78   1.1      haya  * PHY registers are directly accessible through the 8139's register
     79   1.1      haya  * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
     80   1.1      haya  * filter.
     81   1.1      haya  *
     82   1.1      haya  * The 8129 chip is an older version of the 8139 that uses an external PHY
     83   1.1      haya  * chip. The 8129 has a serial MDIO interface for accessing the MII where
     84   1.1      haya  * the 8139 lets you directly access the on-board PHY registers. We need
     85   1.1      haya  * to select which interface to use depending on the chip type.
     86   1.1      haya  */
     87   1.1      haya 
     88   1.1      haya #include "opt_inet.h"
     89   1.1      haya #include "opt_ns.h"
     90   1.1      haya #include "bpfilter.h"
     91   1.1      haya #include "rnd.h"
     92   1.1      haya 
     93   1.1      haya #include <sys/param.h>
     94   1.1      haya #include <sys/systm.h>
     95   1.1      haya #include <sys/callout.h>
     96   1.1      haya #include <sys/device.h>
     97   1.1      haya #include <sys/sockio.h>
     98   1.1      haya #include <sys/mbuf.h>
     99   1.1      haya #include <sys/malloc.h>
    100   1.1      haya #include <sys/kernel.h>
    101   1.1      haya #include <sys/socket.h>
    102   1.1      haya 
    103   1.1      haya #include <net/if.h>
    104   1.1      haya #include <net/if_arp.h>
    105   1.1      haya #include <net/if_ether.h>
    106   1.1      haya #include <net/if_dl.h>
    107   1.1      haya #include <net/if_media.h>
    108   1.1      haya #ifdef INET
    109   1.1      haya #include <netinet/in.h>
    110   1.1      haya #include <netinet/if_inarp.h>
    111   1.1      haya #endif
    112   1.1      haya #ifdef NS
    113   1.1      haya #include <netns/ns.h>
    114   1.1      haya #include <netns/ns_if.h>
    115   1.1      haya #endif
    116   1.1      haya 
    117   1.1      haya #if NBPFILTER > 0
    118   1.1      haya #include <net/bpf.h>
    119   1.1      haya #endif
    120   1.1      haya #if NRND > 0
    121   1.1      haya #include <sys/rnd.h>
    122   1.1      haya #endif
    123   1.1      haya 
    124   1.1      haya #include <machine/bus.h>
    125   1.3   tsutsui #include <machine/endian.h>
    126   1.1      haya 
    127   1.1      haya #include <dev/mii/mii.h>
    128   1.1      haya #include <dev/mii/miivar.h>
    129   1.1      haya 
    130   1.1      haya #include <dev/ic/rtl81x9reg.h>
    131   1.4   tsutsui #include <dev/ic/rtl81x9var.h>
    132   1.1      haya 
    133   1.1      haya #if defined DEBUG
    134   1.1      haya #define STATIC
    135   1.1      haya #else
    136   1.1      haya #define STATIC static
    137   1.1      haya #endif
    138   1.1      haya 
    139   1.8   thorpej STATIC void rtk_reset		__P((struct rtk_softc *));
    140   1.8   thorpej STATIC void rtk_rxeof		__P((struct rtk_softc *));
    141   1.8   thorpej STATIC void rtk_txeof		__P((struct rtk_softc *));
    142   1.8   thorpej STATIC void rtk_start		__P((struct ifnet *));
    143   1.8   thorpej STATIC int rtk_ioctl		__P((struct ifnet *, u_long, caddr_t));
    144  1.15   thorpej STATIC int rtk_init		__P((struct ifnet *));
    145  1.15   thorpej STATIC void rtk_stop		__P((struct ifnet *, int));
    146  1.15   thorpej 
    147  1.10   tsutsui STATIC void rtk_watchdog	__P((struct ifnet *));
    148  1.10   tsutsui STATIC void rtk_shutdown	__P((void *));
    149   1.8   thorpej STATIC int rtk_ifmedia_upd	__P((struct ifnet *));
    150   1.8   thorpej STATIC void rtk_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
    151   1.8   thorpej 
    152   1.8   thorpej STATIC u_int16_t rtk_read_eeprom __P((struct rtk_softc *, int, int));
    153   1.8   thorpej STATIC void rtk_eeprom_putbyte	__P((struct rtk_softc *, int, int));
    154  1.10   tsutsui STATIC void rtk_mii_sync	__P((struct rtk_softc *));
    155  1.10   tsutsui STATIC void rtk_mii_send	__P((struct rtk_softc *, u_int32_t, int));
    156   1.8   thorpej STATIC int rtk_mii_readreg	__P((struct rtk_softc *, struct rtk_mii_frame *));
    157   1.8   thorpej STATIC int rtk_mii_writereg	__P((struct rtk_softc *, struct rtk_mii_frame *));
    158   1.8   thorpej 
    159   1.8   thorpej STATIC int rtk_phy_readreg	__P((struct device *, int, int));
    160   1.8   thorpej STATIC void rtk_phy_writereg	__P((struct device *, int, int, int));
    161   1.8   thorpej STATIC void rtk_phy_statchg	__P((struct device *));
    162  1.10   tsutsui STATIC void rtk_tick		__P((void *));
    163   1.1      haya 
    164  1.10   tsutsui STATIC int rtk_enable		__P((struct rtk_softc *));
    165  1.10   tsutsui STATIC void rtk_disable		__P((struct rtk_softc *));
    166  1.10   tsutsui STATIC void rtk_power		__P((int, void *));
    167  1.10   tsutsui 
    168  1.10   tsutsui STATIC void rtk_setmulti	__P((struct rtk_softc *));
    169   1.8   thorpej STATIC int rtk_list_tx_init	__P((struct rtk_softc *));
    170   1.1      haya 
    171   1.1      haya #define EE_SET(x)					\
    172  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD,			\
    173  1.10   tsutsui 		CSR_READ_1(sc, RTK_EECMD) | (x))
    174   1.1      haya 
    175   1.1      haya #define EE_CLR(x)					\
    176  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD,			\
    177  1.10   tsutsui 		CSR_READ_1(sc, RTK_EECMD) & ~(x))
    178   1.1      haya 
    179   1.1      haya /*
    180   1.1      haya  * Send a read command and address to the EEPROM, check for ACK.
    181   1.1      haya  */
    182   1.8   thorpej STATIC void rtk_eeprom_putbyte(sc, addr, addr_len)
    183  1.10   tsutsui 	struct rtk_softc	*sc;
    184   1.5   tsutsui 	int			addr, addr_len;
    185   1.1      haya {
    186   1.2   tsutsui 	int			d, i;
    187   1.1      haya 
    188  1.10   tsutsui 	d = (RTK_EECMD_READ << addr_len) | addr;
    189   1.1      haya 
    190   1.1      haya 	/*
    191   1.1      haya 	 * Feed in each bit and stobe the clock.
    192   1.1      haya 	 */
    193  1.10   tsutsui 	for (i = RTK_EECMD_LEN + addr_len - 1; i >= 0; i--) {
    194   1.5   tsutsui 		if (d & (1 << i)) {
    195  1.10   tsutsui 			EE_SET(RTK_EE_DATAIN);
    196   1.1      haya 		} else {
    197  1.10   tsutsui 			EE_CLR(RTK_EE_DATAIN);
    198   1.1      haya 		}
    199   1.1      haya 		DELAY(100);
    200  1.10   tsutsui 		EE_SET(RTK_EE_CLK);
    201   1.1      haya 		DELAY(150);
    202  1.10   tsutsui 		EE_CLR(RTK_EE_CLK);
    203   1.1      haya 		DELAY(100);
    204   1.1      haya 	}
    205   1.1      haya }
    206   1.1      haya 
    207   1.1      haya /*
    208   1.1      haya  * Read a word of data stored in the EEPROM at address 'addr.'
    209   1.1      haya  */
    210   1.8   thorpej u_int16_t rtk_read_eeprom(sc, addr, addr_len)
    211  1.10   tsutsui 	struct rtk_softc	*sc;
    212   1.5   tsutsui 	int			addr, addr_len;
    213   1.1      haya {
    214   1.5   tsutsui 	u_int16_t		word = 0;
    215   1.2   tsutsui 	int			i;
    216   1.1      haya 
    217   1.1      haya 	/* Enter EEPROM access mode. */
    218  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_PROGRAM|RTK_EE_SEL);
    219   1.1      haya 
    220   1.1      haya 	/*
    221   1.1      haya 	 * Send address of word we want to read.
    222   1.1      haya 	 */
    223   1.8   thorpej 	rtk_eeprom_putbyte(sc, addr, addr_len);
    224   1.1      haya 
    225  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_PROGRAM|RTK_EE_SEL);
    226   1.1      haya 
    227   1.1      haya 	/*
    228   1.1      haya 	 * Start reading bits from EEPROM.
    229   1.1      haya 	 */
    230   1.5   tsutsui 	for (i = 15; i >= 0; i--) {
    231  1.10   tsutsui 		EE_SET(RTK_EE_CLK);
    232   1.1      haya 		DELAY(100);
    233  1.10   tsutsui 		if (CSR_READ_1(sc, RTK_EECMD) & RTK_EE_DATAOUT)
    234   1.5   tsutsui 			word |= (1 << i);
    235  1.10   tsutsui 		EE_CLR(RTK_EE_CLK);
    236   1.1      haya 		DELAY(100);
    237   1.1      haya 	}
    238   1.1      haya 
    239   1.1      haya 	/* Turn off EEPROM access mode. */
    240  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
    241   1.1      haya 
    242   1.5   tsutsui 	return (word);
    243   1.1      haya }
    244   1.1      haya 
    245   1.1      haya /*
    246   1.1      haya  * MII access routines are provided for the 8129, which
    247   1.1      haya  * doesn't have a built-in PHY. For the 8139, we fake things
    248   1.8   thorpej  * up by diverting rtk_phy_readreg()/rtk_phy_writereg() to the
    249   1.1      haya  * direct access PHY registers.
    250   1.1      haya  */
    251   1.1      haya #define MII_SET(x)					\
    252  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_MII,				\
    253  1.10   tsutsui 		CSR_READ_1(sc, RTK_MII) | (x))
    254   1.1      haya 
    255   1.1      haya #define MII_CLR(x)					\
    256  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_MII,				\
    257  1.10   tsutsui 		CSR_READ_1(sc, RTK_MII) & ~(x))
    258   1.1      haya 
    259   1.1      haya /*
    260   1.1      haya  * Sync the PHYs by setting data bit and strobing the clock 32 times.
    261   1.1      haya  */
    262   1.8   thorpej STATIC void rtk_mii_sync(sc)
    263  1.10   tsutsui 	struct rtk_softc	*sc;
    264   1.1      haya {
    265   1.2   tsutsui 	int			i;
    266   1.1      haya 
    267  1.10   tsutsui 	MII_SET(RTK_MII_DIR|RTK_MII_DATAOUT);
    268   1.1      haya 
    269   1.1      haya 	for (i = 0; i < 32; i++) {
    270  1.10   tsutsui 		MII_SET(RTK_MII_CLK);
    271   1.1      haya 		DELAY(1);
    272  1.10   tsutsui 		MII_CLR(RTK_MII_CLK);
    273   1.1      haya 		DELAY(1);
    274   1.1      haya 	}
    275   1.1      haya 
    276   1.1      haya 	return;
    277   1.1      haya }
    278   1.1      haya 
    279   1.1      haya /*
    280   1.1      haya  * Clock a series of bits through the MII.
    281   1.1      haya  */
    282   1.8   thorpej STATIC void rtk_mii_send(sc, bits, cnt)
    283  1.10   tsutsui 	struct rtk_softc	*sc;
    284   1.1      haya 	u_int32_t		bits;
    285   1.1      haya 	int			cnt;
    286   1.1      haya {
    287   1.1      haya 	int			i;
    288   1.1      haya 
    289  1.10   tsutsui 	MII_CLR(RTK_MII_CLK);
    290   1.1      haya 
    291   1.1      haya 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
    292   1.1      haya                 if (bits & i) {
    293  1.10   tsutsui 			MII_SET(RTK_MII_DATAOUT);
    294   1.1      haya                 } else {
    295  1.10   tsutsui 			MII_CLR(RTK_MII_DATAOUT);
    296   1.1      haya                 }
    297   1.1      haya 		DELAY(1);
    298  1.10   tsutsui 		MII_CLR(RTK_MII_CLK);
    299   1.1      haya 		DELAY(1);
    300  1.10   tsutsui 		MII_SET(RTK_MII_CLK);
    301   1.1      haya 	}
    302   1.1      haya }
    303   1.1      haya 
    304   1.1      haya /*
    305   1.1      haya  * Read an PHY register through the MII.
    306   1.1      haya  */
    307   1.8   thorpej STATIC int rtk_mii_readreg(sc, frame)
    308  1.10   tsutsui 	struct rtk_softc	*sc;
    309   1.8   thorpej 	struct rtk_mii_frame	*frame;
    310   1.1      haya 
    311   1.1      haya {
    312   1.1      haya 	int			i, ack, s;
    313   1.1      haya 
    314   1.9   thorpej 	s = splnet();
    315   1.1      haya 
    316   1.1      haya 	/*
    317   1.1      haya 	 * Set up frame for RX.
    318   1.1      haya 	 */
    319  1.10   tsutsui 	frame->mii_stdelim = RTK_MII_STARTDELIM;
    320  1.10   tsutsui 	frame->mii_opcode = RTK_MII_READOP;
    321   1.1      haya 	frame->mii_turnaround = 0;
    322   1.1      haya 	frame->mii_data = 0;
    323   1.1      haya 
    324  1.10   tsutsui 	CSR_WRITE_2(sc, RTK_MII, 0);
    325   1.1      haya 
    326   1.1      haya 	/*
    327   1.1      haya  	 * Turn on data xmit.
    328   1.1      haya 	 */
    329  1.10   tsutsui 	MII_SET(RTK_MII_DIR);
    330   1.1      haya 
    331   1.8   thorpej 	rtk_mii_sync(sc);
    332   1.1      haya 
    333   1.1      haya 	/*
    334   1.1      haya 	 * Send command/address info.
    335   1.1      haya 	 */
    336   1.8   thorpej 	rtk_mii_send(sc, frame->mii_stdelim, 2);
    337   1.8   thorpej 	rtk_mii_send(sc, frame->mii_opcode, 2);
    338   1.8   thorpej 	rtk_mii_send(sc, frame->mii_phyaddr, 5);
    339   1.8   thorpej 	rtk_mii_send(sc, frame->mii_regaddr, 5);
    340   1.1      haya 
    341   1.1      haya 	/* Idle bit */
    342  1.10   tsutsui 	MII_CLR((RTK_MII_CLK|RTK_MII_DATAOUT));
    343   1.1      haya 	DELAY(1);
    344  1.10   tsutsui 	MII_SET(RTK_MII_CLK);
    345   1.1      haya 	DELAY(1);
    346   1.1      haya 
    347   1.1      haya 	/* Turn off xmit. */
    348  1.10   tsutsui 	MII_CLR(RTK_MII_DIR);
    349   1.1      haya 
    350   1.1      haya 	/* Check for ack */
    351  1.10   tsutsui 	MII_CLR(RTK_MII_CLK);
    352   1.1      haya 	DELAY(1);
    353  1.10   tsutsui 	MII_SET(RTK_MII_CLK);
    354   1.1      haya 	DELAY(1);
    355  1.10   tsutsui 	ack = CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN;
    356   1.1      haya 
    357   1.1      haya 	/*
    358   1.1      haya 	 * Now try reading data bits. If the ack failed, we still
    359   1.1      haya 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
    360   1.1      haya 	 */
    361   1.1      haya 	if (ack) {
    362   1.1      haya 		for(i = 0; i < 16; i++) {
    363  1.10   tsutsui 			MII_CLR(RTK_MII_CLK);
    364   1.1      haya 			DELAY(1);
    365  1.10   tsutsui 			MII_SET(RTK_MII_CLK);
    366   1.1      haya 			DELAY(1);
    367   1.1      haya 		}
    368   1.1      haya 		goto fail;
    369   1.1      haya 	}
    370   1.1      haya 
    371   1.1      haya 	for (i = 0x8000; i; i >>= 1) {
    372  1.10   tsutsui 		MII_CLR(RTK_MII_CLK);
    373   1.1      haya 		DELAY(1);
    374   1.1      haya 		if (!ack) {
    375  1.10   tsutsui 			if (CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN)
    376   1.1      haya 				frame->mii_data |= i;
    377   1.1      haya 			DELAY(1);
    378   1.1      haya 		}
    379  1.10   tsutsui 		MII_SET(RTK_MII_CLK);
    380   1.1      haya 		DELAY(1);
    381   1.1      haya 	}
    382   1.1      haya 
    383   1.1      haya fail:
    384   1.1      haya 
    385  1.10   tsutsui 	MII_CLR(RTK_MII_CLK);
    386   1.1      haya 	DELAY(1);
    387  1.10   tsutsui 	MII_SET(RTK_MII_CLK);
    388   1.1      haya 	DELAY(1);
    389   1.1      haya 
    390   1.1      haya 	splx(s);
    391   1.1      haya 
    392   1.1      haya 	if (ack)
    393   1.1      haya 		return(1);
    394   1.1      haya 	return(0);
    395   1.1      haya }
    396   1.1      haya 
    397   1.1      haya /*
    398   1.1      haya  * Write to a PHY register through the MII.
    399   1.1      haya  */
    400   1.8   thorpej STATIC int rtk_mii_writereg(sc, frame)
    401  1.10   tsutsui 	struct rtk_softc	*sc;
    402   1.8   thorpej 	struct rtk_mii_frame	*frame;
    403   1.1      haya 
    404   1.1      haya {
    405   1.1      haya 	int			s;
    406   1.1      haya 
    407   1.9   thorpej 	s = splnet();
    408   1.1      haya 	/*
    409   1.1      haya 	 * Set up frame for TX.
    410   1.1      haya 	 */
    411   1.1      haya 
    412  1.10   tsutsui 	frame->mii_stdelim = RTK_MII_STARTDELIM;
    413  1.10   tsutsui 	frame->mii_opcode = RTK_MII_WRITEOP;
    414  1.10   tsutsui 	frame->mii_turnaround = RTK_MII_TURNAROUND;
    415   1.1      haya 
    416   1.1      haya 	/*
    417   1.1      haya  	 * Turn on data output.
    418   1.1      haya 	 */
    419  1.10   tsutsui 	MII_SET(RTK_MII_DIR);
    420   1.1      haya 
    421   1.8   thorpej 	rtk_mii_sync(sc);
    422   1.1      haya 
    423   1.8   thorpej 	rtk_mii_send(sc, frame->mii_stdelim, 2);
    424   1.8   thorpej 	rtk_mii_send(sc, frame->mii_opcode, 2);
    425   1.8   thorpej 	rtk_mii_send(sc, frame->mii_phyaddr, 5);
    426   1.8   thorpej 	rtk_mii_send(sc, frame->mii_regaddr, 5);
    427   1.8   thorpej 	rtk_mii_send(sc, frame->mii_turnaround, 2);
    428   1.8   thorpej 	rtk_mii_send(sc, frame->mii_data, 16);
    429   1.1      haya 
    430   1.1      haya 	/* Idle bit. */
    431  1.10   tsutsui 	MII_SET(RTK_MII_CLK);
    432   1.1      haya 	DELAY(1);
    433  1.10   tsutsui 	MII_CLR(RTK_MII_CLK);
    434   1.1      haya 	DELAY(1);
    435   1.1      haya 
    436   1.1      haya 	/*
    437   1.1      haya 	 * Turn off xmit.
    438   1.1      haya 	 */
    439  1.10   tsutsui 	MII_CLR(RTK_MII_DIR);
    440   1.1      haya 
    441   1.1      haya 	splx(s);
    442   1.1      haya 
    443   1.1      haya 	return(0);
    444   1.1      haya }
    445   1.1      haya 
    446   1.8   thorpej STATIC int rtk_phy_readreg(self, phy, reg)
    447   1.1      haya 	struct device		*self;
    448   1.1      haya 	int			phy, reg;
    449   1.1      haya {
    450  1.10   tsutsui 	struct rtk_softc	*sc = (void *)self;
    451   1.8   thorpej 	struct rtk_mii_frame	frame;
    452   1.1      haya 	u_int16_t		rval = 0;
    453  1.10   tsutsui 	u_int16_t		rtk8139_reg = 0;
    454   1.1      haya 
    455  1.10   tsutsui 	if (sc->rtk_type == RTK_8139) {
    456   1.1      haya 		if (phy != 7)
    457   1.1      haya 			return (0);
    458   1.1      haya 
    459   1.1      haya 		switch(reg) {
    460   1.1      haya 		case MII_BMCR:
    461  1.10   tsutsui 			rtk8139_reg = RTK_BMCR;
    462   1.1      haya 			break;
    463   1.1      haya 		case MII_BMSR:
    464  1.10   tsutsui 			rtk8139_reg = RTK_BMSR;
    465   1.1      haya 			break;
    466   1.1      haya 		case MII_ANAR:
    467  1.10   tsutsui 			rtk8139_reg = RTK_ANAR;
    468   1.1      haya 			break;
    469  1.12  drochner 		case MII_ANER:
    470  1.12  drochner 			rtk8139_reg = RTK_ANER;
    471  1.12  drochner 			break;
    472   1.1      haya 		case MII_ANLPAR:
    473  1.10   tsutsui 			rtk8139_reg = RTK_LPAR;
    474   1.1      haya 			break;
    475   1.1      haya 		default:
    476   1.1      haya #if 0
    477   1.1      haya 			printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
    478   1.1      haya #endif
    479   1.1      haya 			return(0);
    480   1.1      haya 		}
    481  1.10   tsutsui 		rval = CSR_READ_2(sc, rtk8139_reg);
    482   1.1      haya 		return(rval);
    483   1.1      haya 	}
    484   1.1      haya 
    485   1.1      haya 	bzero((char *)&frame, sizeof(frame));
    486   1.1      haya 
    487   1.1      haya 	frame.mii_phyaddr = phy;
    488   1.1      haya 	frame.mii_regaddr = reg;
    489   1.8   thorpej 	rtk_mii_readreg(sc, &frame);
    490   1.1      haya 
    491   1.1      haya 	return(frame.mii_data);
    492   1.1      haya }
    493   1.1      haya 
    494   1.8   thorpej STATIC void rtk_phy_writereg(self, phy, reg, data)
    495   1.1      haya 	struct device		*self;
    496   1.1      haya 	int			phy, reg;
    497   1.1      haya 	int			data;
    498   1.1      haya {
    499  1.10   tsutsui 	struct rtk_softc	*sc = (void *)self;
    500   1.8   thorpej 	struct rtk_mii_frame	frame;
    501  1.10   tsutsui 	u_int16_t		rtk8139_reg = 0;
    502   1.1      haya 
    503  1.10   tsutsui 	if (sc->rtk_type == RTK_8139) {
    504   1.1      haya 		if (phy != 7)
    505   1.1      haya 			return;
    506   1.1      haya 
    507   1.1      haya 		switch(reg) {
    508   1.1      haya 		case MII_BMCR:
    509  1.10   tsutsui 			rtk8139_reg = RTK_BMCR;
    510   1.1      haya 			break;
    511   1.1      haya 		case MII_BMSR:
    512  1.10   tsutsui 			rtk8139_reg = RTK_BMSR;
    513   1.1      haya 			break;
    514   1.1      haya 		case MII_ANAR:
    515  1.10   tsutsui 			rtk8139_reg = RTK_ANAR;
    516   1.1      haya 			break;
    517  1.12  drochner 		case MII_ANER:
    518  1.12  drochner 			rtk8139_reg = RTK_ANER;
    519  1.12  drochner 			break;
    520   1.1      haya 		case MII_ANLPAR:
    521  1.10   tsutsui 			rtk8139_reg = RTK_LPAR;
    522   1.1      haya 			break;
    523   1.1      haya 		default:
    524   1.1      haya #if 0
    525   1.1      haya 			printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
    526   1.1      haya #endif
    527   1.1      haya 			return;
    528   1.1      haya 		}
    529  1.10   tsutsui 		CSR_WRITE_2(sc, rtk8139_reg, data);
    530   1.1      haya 		return;
    531   1.1      haya 	}
    532   1.1      haya 
    533   1.1      haya 	bzero((char *)&frame, sizeof(frame));
    534   1.1      haya 
    535   1.1      haya 	frame.mii_phyaddr = phy;
    536   1.1      haya 	frame.mii_regaddr = reg;
    537   1.1      haya 	frame.mii_data = data;
    538   1.1      haya 
    539   1.8   thorpej 	rtk_mii_writereg(sc, &frame);
    540   1.1      haya 
    541   1.1      haya 	return;
    542   1.1      haya }
    543   1.1      haya 
    544   1.1      haya STATIC void
    545   1.8   thorpej rtk_phy_statchg(v)
    546   1.1      haya 	struct device *v;
    547   1.1      haya {
    548   1.1      haya 
    549   1.1      haya 	/* Nothing to do. */
    550   1.1      haya }
    551   1.1      haya 
    552   1.8   thorpej #define	rtk_calchash(addr) \
    553   1.7   thorpej 	(ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
    554   1.1      haya 
    555   1.1      haya /*
    556   1.1      haya  * Program the 64-bit multicast hash filter.
    557   1.1      haya  */
    558   1.8   thorpej STATIC void rtk_setmulti(sc)
    559  1.10   tsutsui 	struct rtk_softc	*sc;
    560   1.1      haya {
    561   1.1      haya 	struct ifnet		*ifp;
    562   1.1      haya 	int			h = 0;
    563   1.1      haya 	u_int32_t		hashes[2] = { 0, 0 };
    564   1.1      haya 	u_int32_t		rxfilt;
    565   1.1      haya 	int			mcnt = 0;
    566   1.1      haya 	struct ether_multi *enm;
    567   1.1      haya 	struct ether_multistep step;
    568   1.1      haya 
    569   1.1      haya 	ifp = &sc->ethercom.ec_if;
    570   1.1      haya 
    571  1.10   tsutsui 	rxfilt = CSR_READ_4(sc, RTK_RXCFG);
    572   1.1      haya 
    573   1.1      haya 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    574  1.10   tsutsui 		rxfilt |= RTK_RXCFG_RX_MULTI;
    575  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
    576  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_MAR0, 0xFFFFFFFF);
    577  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_MAR4, 0xFFFFFFFF);
    578   1.1      haya 		return;
    579   1.1      haya 	}
    580   1.1      haya 
    581   1.1      haya 	/* first, zot all the existing hash bits */
    582  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_MAR0, 0);
    583  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_MAR4, 0);
    584   1.1      haya 
    585   1.1      haya 	/* now program new ones */
    586   1.1      haya 	ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
    587   1.1      haya 	while (enm != NULL) {
    588   1.4   tsutsui 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    589   1.4   tsutsui 		    ETHER_ADDR_LEN) != 0)
    590   1.4   tsutsui 			continue;
    591   1.4   tsutsui 
    592   1.8   thorpej 		h = rtk_calchash(enm->enm_addrlo);
    593   1.1      haya 		if (h < 32)
    594   1.1      haya 			hashes[0] |= (1 << h);
    595   1.1      haya 		else
    596   1.1      haya 			hashes[1] |= (1 << (h - 32));
    597   1.1      haya 		mcnt++;
    598   1.1      haya 		ETHER_NEXT_MULTI(step, enm);
    599   1.1      haya 	}
    600   1.1      haya 
    601   1.1      haya 	if (mcnt)
    602  1.10   tsutsui 		rxfilt |= RTK_RXCFG_RX_MULTI;
    603   1.1      haya 	else
    604  1.10   tsutsui 		rxfilt &= ~RTK_RXCFG_RX_MULTI;
    605   1.1      haya 
    606  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
    607  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_MAR0, hashes[0]);
    608  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_MAR4, hashes[1]);
    609   1.1      haya 
    610   1.1      haya 	return;
    611   1.1      haya }
    612   1.1      haya 
    613   1.8   thorpej void rtk_reset(sc)
    614  1.10   tsutsui 	struct rtk_softc	*sc;
    615   1.1      haya {
    616   1.2   tsutsui 	int			i;
    617   1.1      haya 
    618  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
    619   1.1      haya 
    620  1.10   tsutsui 	for (i = 0; i < RTK_TIMEOUT; i++) {
    621   1.1      haya 		DELAY(10);
    622  1.10   tsutsui 		if (!(CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET))
    623   1.1      haya 			break;
    624   1.1      haya 	}
    625  1.10   tsutsui 	if (i == RTK_TIMEOUT)
    626   1.1      haya 		printf("%s: reset never completed!\n", sc->sc_dev.dv_xname);
    627   1.1      haya 
    628   1.1      haya         return;
    629   1.1      haya }
    630   1.1      haya 
    631   1.1      haya /*
    632   1.1      haya  * Attach the interface. Allocate softc structures, do ifmedia
    633   1.1      haya  * setup and ethernet/BPF attach.
    634   1.1      haya  */
    635   1.1      haya void
    636   1.8   thorpej rtk_attach(sc)
    637   1.8   thorpej 	struct rtk_softc *sc;
    638   1.1      haya {
    639   1.1      haya 
    640   1.1      haya 	struct ifnet *ifp;
    641   1.6   tsutsui 	u_int16_t val;
    642   1.6   tsutsui 	u_int8_t eaddr[ETHER_ADDR_LEN];
    643  1.10   tsutsui 	int error;
    644   1.6   tsutsui 	int i,addr_len;
    645   1.1      haya 
    646   1.8   thorpej 	callout_init(&sc->rtk_tick_ch);
    647   1.1      haya 
    648   1.6   tsutsui 	/*
    649   1.6   tsutsui 	 * Check EEPROM type 9346 or 9356.
    650   1.6   tsutsui 	 */
    651  1.10   tsutsui 	if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
    652  1.10   tsutsui 		addr_len = RTK_EEADDR_LEN1;
    653   1.6   tsutsui 	else
    654  1.10   tsutsui 		addr_len = RTK_EEADDR_LEN0;
    655   1.6   tsutsui 
    656   1.6   tsutsui 	/*
    657   1.6   tsutsui 	 * Get station address.
    658   1.6   tsutsui 	 */
    659  1.10   tsutsui 	val = rtk_read_eeprom(sc, RTK_EE_EADDR0, addr_len);
    660   1.6   tsutsui 	eaddr[0] = val & 0xff;
    661   1.6   tsutsui 	eaddr[1] = val >> 8;
    662  1.10   tsutsui 	val = rtk_read_eeprom(sc, RTK_EE_EADDR1, addr_len);
    663   1.6   tsutsui 	eaddr[2] = val & 0xff;
    664   1.6   tsutsui 	eaddr[3] = val >> 8;
    665  1.10   tsutsui 	val = rtk_read_eeprom(sc, RTK_EE_EADDR2, addr_len);
    666   1.6   tsutsui 	eaddr[4] = val & 0xff;
    667   1.6   tsutsui 	eaddr[5] = val >> 8;
    668   1.6   tsutsui 
    669   1.1      haya 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    670  1.10   tsutsui 	    RTK_RXBUFLEN + 32, NBPG, 0, &sc->sc_dmaseg, 1, &sc->sc_dmanseg,
    671   1.1      haya 	    BUS_DMA_NOWAIT)) != 0) {
    672   1.1      haya 		printf("%s: can't allocate recv buffer, error = %d\n",
    673   1.1      haya 		       sc->sc_dev.dv_xname, error);
    674  1.10   tsutsui 		goto fail_0;
    675   1.1      haya 	}
    676   1.1      haya 
    677  1.10   tsutsui 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg,
    678  1.10   tsutsui 	    RTK_RXBUFLEN + 32, (caddr_t *)&sc->rtk_cdata.rtk_rx_buf,
    679   1.1      haya 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    680   1.1      haya 		printf("%s: can't map recv buffer, error = %d\n",
    681   1.1      haya 		       sc->sc_dev.dv_xname, error);
    682  1.10   tsutsui 		goto fail_1;
    683   1.1      haya 	}
    684   1.1      haya 
    685   1.1      haya 	/* Leave a few bytes before the start of the RX ring buffer. */
    686   1.8   thorpej 	sc->rtk_cdata.rtk_rx_buf_ptr = sc->rtk_cdata.rtk_rx_buf;
    687   1.8   thorpej 	sc->rtk_cdata.rtk_rx_buf += sizeof(u_int64_t);
    688   1.1      haya 
    689   1.1      haya 	if ((error = bus_dmamap_create(sc->sc_dmat,
    690  1.10   tsutsui 	    RTK_RXBUFLEN + 32 - sizeof(u_int64_t), 1,
    691  1.10   tsutsui 	    RTK_RXBUFLEN + 32 - sizeof(u_int64_t), 0, BUS_DMA_NOWAIT,
    692   1.1      haya 	    &sc->recv_dmamap)) != 0) {
    693   1.1      haya 		printf("%s: can't create recv buffer DMA map, error = %d\n",
    694   1.1      haya 		       sc->sc_dev.dv_xname, error);
    695  1.10   tsutsui 		goto fail_2;
    696   1.1      haya 	}
    697   1.1      haya 
    698   1.1      haya 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->recv_dmamap,
    699  1.10   tsutsui 	    sc->rtk_cdata.rtk_rx_buf, RTK_RXBUFLEN + 32 - sizeof(u_int64_t),
    700  1.10   tsutsui 	    NULL, BUS_DMA_NOWAIT)) != 0) {
    701   1.1      haya 		printf("%s: can't load recv buffer DMA map, error = %d\n",
    702   1.1      haya 		       sc->sc_dev.dv_xname, error);
    703  1.10   tsutsui 		goto fail_3;
    704   1.1      haya 	}
    705   1.1      haya 
    706  1.10   tsutsui 	for (i = 0; i < RTK_TX_LIST_CNT; i++)
    707   1.4   tsutsui 		if ((error = bus_dmamap_create(sc->sc_dmat,
    708   1.6   tsutsui 		    MCLBYTES, 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
    709   1.4   tsutsui 		    &sc->snd_dmamap[i])) != 0) {
    710   1.4   tsutsui 			printf("%s: can't create snd buffer DMA map,"
    711   1.4   tsutsui 			    " error = %d\n", sc->sc_dev.dv_xname, error);
    712  1.10   tsutsui 			goto fail_4;
    713   1.5   tsutsui 		}
    714  1.10   tsutsui 	/*
    715  1.10   tsutsui 	 * From this point forward, the attachment cannot fail. A failure
    716  1.10   tsutsui 	 * before this releases all resources thar may have been
    717  1.10   tsutsui 	 * allocated.
    718  1.10   tsutsui 	 */
    719  1.10   tsutsui 	sc->sc_flags |= RTK_ATTACHED;
    720   1.1      haya 
    721   1.6   tsutsui 	/* Reset the adapter. */
    722   1.8   thorpej 	rtk_reset(sc);
    723   1.6   tsutsui 
    724   1.6   tsutsui 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    725   1.6   tsutsui 	       ether_sprintf(eaddr));
    726   1.6   tsutsui 
    727   1.1      haya 	ifp = &sc->ethercom.ec_if;
    728   1.1      haya 	ifp->if_softc = sc;
    729   1.1      haya 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    730   1.1      haya 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    731   1.8   thorpej 	ifp->if_ioctl = rtk_ioctl;
    732   1.8   thorpej 	ifp->if_start = rtk_start;
    733   1.8   thorpej 	ifp->if_watchdog = rtk_watchdog;
    734  1.15   thorpej 	ifp->if_init = rtk_init;
    735  1.15   thorpej 	ifp->if_stop = rtk_stop;
    736   1.1      haya 
    737   1.1      haya 	/*
    738   1.1      haya 	 * Do ifmedia setup.
    739   1.1      haya 	 */
    740   1.1      haya 	sc->mii.mii_ifp = ifp;
    741   1.8   thorpej 	sc->mii.mii_readreg = rtk_phy_readreg;
    742   1.8   thorpej 	sc->mii.mii_writereg = rtk_phy_writereg;
    743   1.8   thorpej 	sc->mii.mii_statchg = rtk_phy_statchg;
    744   1.8   thorpej 	ifmedia_init(&sc->mii.mii_media, 0, rtk_ifmedia_upd, rtk_ifmedia_sts);
    745   1.1      haya 	mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff,
    746   1.1      haya 				MII_PHY_ANY, MII_OFFSET_ANY, 0);
    747   1.1      haya 
    748   1.1      haya 	/* Choose a default media. */
    749   1.1      haya 	if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
    750  1.10   tsutsui 		ifmedia_add(&sc->mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    751   1.1      haya 		ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_NONE);
    752   1.1      haya 	} else {
    753   1.1      haya 		ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_AUTO);
    754   1.1      haya 	}
    755   1.1      haya 
    756   1.1      haya 	/*
    757   1.1      haya 	 * Call MI attach routines.
    758   1.1      haya 	 */
    759   1.1      haya 	if_attach(ifp);
    760   1.1      haya 	ether_ifattach(ifp, eaddr);
    761   1.1      haya 
    762   1.1      haya #if NBPFILTER > 0
    763   1.1      haya 	bpfattach(&sc->ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    764   1.1      haya 		  sizeof(struct ether_header));
    765   1.1      haya #endif
    766  1.10   tsutsui 	/*
    767  1.10   tsutsui 	 * Make sure the interface is shutdown during reboot.
    768  1.10   tsutsui 	 */
    769  1.10   tsutsui 	sc->sc_sdhook = shutdownhook_establish(rtk_shutdown, sc);
    770  1.10   tsutsui 	if (sc->sc_sdhook == NULL)
    771  1.10   tsutsui 		printf("%s: WARNING: unbale to establish shutdown hook\n",
    772  1.10   tsutsui 			sc->sc_dev.dv_xname);
    773  1.10   tsutsui 	/*
    774  1.10   tsutsui 	 * Add a suspend hook to make sure we come back up after a
    775  1.10   tsutsui 	 * resume.
    776  1.10   tsutsui 	 */
    777  1.10   tsutsui 	sc->sc_powerhook = powerhook_establish(rtk_power, sc);
    778  1.10   tsutsui 	if (sc->sc_powerhook == NULL)
    779  1.10   tsutsui 		printf("%s: WARNING: unable to establish power hook\n",
    780  1.10   tsutsui 			sc->sc_dev.dv_xname);
    781   1.1      haya 
    782  1.10   tsutsui 	return;
    783  1.10   tsutsui fail_4:
    784  1.10   tsutsui 	for (i = 0; i < RTK_TX_LIST_CNT; i++)
    785  1.10   tsutsui 		if (sc->snd_dmamap[i] != NULL)
    786  1.10   tsutsui 			bus_dmamap_destroy(sc->sc_dmat, sc->snd_dmamap[i]);
    787  1.10   tsutsui fail_3:
    788  1.10   tsutsui 	bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
    789  1.10   tsutsui fail_2:
    790  1.10   tsutsui 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_cdata.rtk_rx_buf_ptr,
    791  1.10   tsutsui 	    RTK_RXBUFLEN + 32 - sizeof(u_int64_t));
    792  1.10   tsutsui fail_1:
    793  1.10   tsutsui 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
    794  1.10   tsutsui fail_0:
    795   1.1      haya 	return;
    796   1.1      haya }
    797   1.1      haya 
    798   1.1      haya /*
    799   1.1      haya  * Initialize the transmit descriptors.
    800   1.1      haya  */
    801   1.8   thorpej STATIC int rtk_list_tx_init(sc)
    802  1.10   tsutsui 	struct rtk_softc	*sc;
    803   1.1      haya {
    804   1.8   thorpej 	struct rtk_chain_data	*cd;
    805   1.1      haya 	int			i;
    806   1.1      haya 
    807   1.8   thorpej 	cd = &sc->rtk_cdata;
    808  1.10   tsutsui 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    809   1.8   thorpej 		cd->rtk_tx_chain[i] = NULL;
    810   1.1      haya 		CSR_WRITE_4(sc,
    811  1.10   tsutsui 		    RTK_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000);
    812   1.1      haya 	}
    813   1.1      haya 
    814   1.8   thorpej 	sc->rtk_cdata.cur_tx = 0;
    815   1.8   thorpej 	sc->rtk_cdata.last_tx = 0;
    816   1.1      haya 
    817   1.1      haya 	return(0);
    818   1.1      haya }
    819   1.1      haya 
    820   1.1      haya /*
    821  1.10   tsutsui  * rtk_activate:
    822  1.10   tsutsui  *     Handle device activation/deactivation requests.
    823  1.10   tsutsui  */
    824  1.10   tsutsui int
    825  1.10   tsutsui rtk_activate(self, act)
    826  1.10   tsutsui 	struct device *self;
    827  1.10   tsutsui 	enum devact act;
    828  1.10   tsutsui {
    829  1.10   tsutsui 	struct rtk_softc *sc = (void *) self;
    830  1.10   tsutsui 	int s, error = 0;
    831  1.10   tsutsui 
    832  1.10   tsutsui 	s = splnet();
    833  1.10   tsutsui 	switch (act) {
    834  1.10   tsutsui 	case DVACT_ACTIVATE:
    835  1.10   tsutsui 		error = EOPNOTSUPP;
    836  1.10   tsutsui 		break;
    837  1.10   tsutsui 	case DVACT_DEACTIVATE:
    838  1.10   tsutsui 		mii_activate(&sc->mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
    839  1.10   tsutsui 		if_deactivate(&sc->ethercom.ec_if);
    840  1.10   tsutsui 		break;
    841  1.10   tsutsui 	}
    842  1.10   tsutsui 	splx(s);
    843  1.10   tsutsui 
    844  1.10   tsutsui 	return (error);
    845  1.10   tsutsui }
    846  1.10   tsutsui 
    847  1.10   tsutsui /*
    848  1.10   tsutsui  * rtk_detach:
    849  1.10   tsutsui  *     Detach a rtk interface.
    850  1.10   tsutsui  */
    851  1.10   tsutsui int
    852  1.10   tsutsui rtk_detach(sc)
    853  1.10   tsutsui 	struct rtk_softc *sc;
    854  1.10   tsutsui {
    855  1.10   tsutsui 	struct ifnet *ifp = &sc->ethercom.ec_if;
    856  1.10   tsutsui 	int i;
    857  1.10   tsutsui 
    858  1.10   tsutsui 	/*
    859  1.10   tsutsui 	 * Succeed now if thereisn't any work to do.
    860  1.10   tsutsui 	 */
    861  1.10   tsutsui 	if ((sc->sc_flags & RTK_ATTACHED) == 0)
    862  1.10   tsutsui 		return (0);
    863  1.10   tsutsui 
    864  1.10   tsutsui 	/* Unhook our tick handler. */
    865  1.10   tsutsui 	callout_stop(&sc->rtk_tick_ch);
    866  1.10   tsutsui 
    867  1.10   tsutsui 	/* Detach all PHYs. */
    868  1.10   tsutsui 	mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
    869  1.10   tsutsui 
    870  1.10   tsutsui 	/* Delete all remaining media. */
    871  1.10   tsutsui 	ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
    872  1.10   tsutsui 
    873  1.10   tsutsui #if NBPFILTER > 0
    874  1.10   tsutsui 	bpfdetach(ifp);
    875  1.10   tsutsui #endif
    876  1.10   tsutsui 	ether_ifdetach(ifp);
    877  1.10   tsutsui 	if_detach(ifp);
    878  1.10   tsutsui 
    879  1.10   tsutsui 	for (i = 0; i < RTK_TX_LIST_CNT; i++)
    880  1.10   tsutsui 		if (sc->snd_dmamap[i] != NULL)
    881  1.10   tsutsui 			bus_dmamap_destroy(sc->sc_dmat, sc->snd_dmamap[i]);
    882  1.10   tsutsui 	bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
    883  1.10   tsutsui 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_cdata.rtk_rx_buf_ptr,
    884  1.10   tsutsui 	    RTK_RXBUFLEN + 32 - sizeof(u_int64_t));
    885  1.10   tsutsui 
    886  1.10   tsutsui 	shutdownhook_disestablish(sc->sc_sdhook);
    887  1.10   tsutsui 	powerhook_disestablish(sc->sc_powerhook);
    888  1.10   tsutsui 
    889  1.10   tsutsui 	return (0);
    890  1.10   tsutsui }
    891  1.10   tsutsui 
    892  1.10   tsutsui /*
    893  1.10   tsutsui  * rtk_enable:
    894  1.10   tsutsui  *     Enable the RTL81X9 chip.
    895  1.10   tsutsui  */
    896  1.10   tsutsui int
    897  1.10   tsutsui rtk_enable(sc)
    898  1.10   tsutsui 	struct rtk_softc *sc;
    899  1.10   tsutsui {
    900  1.10   tsutsui 	if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
    901  1.10   tsutsui 		if ((*sc->sc_enable)(sc) != 0) {
    902  1.10   tsutsui 			printf("%s: device enable failed\n",
    903  1.10   tsutsui 				sc->sc_dev.dv_xname);
    904  1.10   tsutsui 			return(EIO);
    905  1.10   tsutsui 		}
    906  1.10   tsutsui 		sc->sc_flags |= RTK_ENABLED;
    907  1.10   tsutsui 	}
    908  1.10   tsutsui 	return (0);
    909  1.10   tsutsui }
    910  1.10   tsutsui 
    911  1.10   tsutsui /*
    912  1.10   tsutsui  * rtk_disable:
    913  1.10   tsutsui  *     Disable the RTL81X9 chip.
    914  1.10   tsutsui  */
    915  1.10   tsutsui void
    916  1.10   tsutsui rtk_disable(sc)
    917  1.10   tsutsui 	struct rtk_softc *sc;
    918  1.10   tsutsui {
    919  1.10   tsutsui 	if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
    920  1.10   tsutsui 		(*sc->sc_disable)(sc);
    921  1.10   tsutsui 		sc->sc_flags &= ~RTK_ENABLED;
    922  1.10   tsutsui 	}
    923  1.10   tsutsui }
    924  1.10   tsutsui 
    925  1.10   tsutsui /*
    926  1.10   tsutsui  * rtk_power:
    927  1.10   tsutsui  *     Power management (suspend/resume) hook.
    928  1.10   tsutsui  */
    929  1.10   tsutsui void
    930  1.10   tsutsui rtk_power(why, arg)
    931  1.10   tsutsui 	int why;
    932  1.10   tsutsui 	void *arg;
    933  1.10   tsutsui {
    934  1.10   tsutsui 	struct rtk_softc *sc = (void *) arg;
    935  1.10   tsutsui 	struct ifnet *ifp = &sc->ethercom.ec_if;
    936  1.10   tsutsui 	int s;
    937  1.10   tsutsui 
    938  1.10   tsutsui 	s = splnet();
    939  1.10   tsutsui 	if (why != PWR_RESUME) {
    940  1.15   thorpej 		rtk_stop(ifp, 0);
    941  1.10   tsutsui 		if (sc->sc_power != NULL)
    942  1.10   tsutsui 			(*sc->sc_power)(sc, why);
    943  1.10   tsutsui 	} else if (ifp->if_flags & IFF_UP) {
    944  1.10   tsutsui 		if (sc->sc_power != NULL)
    945  1.10   tsutsui 			(*sc->sc_power)(sc, why);
    946  1.15   thorpej 		rtk_init(ifp);
    947  1.10   tsutsui 	}
    948  1.10   tsutsui 	splx(s);
    949  1.10   tsutsui 
    950  1.10   tsutsui }
    951  1.10   tsutsui 
    952  1.10   tsutsui /*
    953   1.1      haya  * A frame has been uploaded: pass the resulting mbuf chain up to
    954   1.1      haya  * the higher level protocols.
    955   1.1      haya  *
    956   1.1      haya  * You know there's something wrong with a PCI bus-master chip design
    957   1.1      haya  * when you have to use m_devget().
    958   1.1      haya  *
    959   1.1      haya  * The receive operation is badly documented in the datasheet, so I'll
    960   1.1      haya  * attempt to document it here. The driver provides a buffer area and
    961   1.1      haya  * places its base address in the RX buffer start address register.
    962   1.1      haya  * The chip then begins copying frames into the RX buffer. Each frame
    963   1.1      haya  * is preceeded by a 32-bit RX status word which specifies the length
    964   1.1      haya  * of the frame and certain other status bits. Each frame (starting with
    965   1.1      haya  * the status word) is also 32-bit aligned. The frame length is in the
    966   1.1      haya  * first 16 bits of the status word; the lower 15 bits correspond with
    967   1.1      haya  * the 'rx status register' mentioned in the datasheet.
    968   1.1      haya  *
    969   1.1      haya  * Note: to make the Alpha happy, the frame payload needs to be aligned
    970   1.1      haya  * on a 32-bit boundary. To achieve this, we cheat a bit by copying from
    971   1.1      haya  * the ring buffer starting at an address two bytes before the actual
    972   1.1      haya  * data location. We can then shave off the first two bytes using m_adj().
    973   1.1      haya  * The reason we do this is because m_devget() doesn't let us specify an
    974   1.1      haya  * offset into the mbuf storage space, so we have to artificially create
    975   1.1      haya  * one. The ring is allocated in such a way that there are a few unused
    976   1.1      haya  * bytes of space preceecing it so that it will be safe for us to do the
    977   1.1      haya  * 2-byte backstep even if reading from the ring at offset 0.
    978   1.1      haya  */
    979   1.8   thorpej STATIC void rtk_rxeof(sc)
    980  1.10   tsutsui 	struct rtk_softc	*sc;
    981   1.1      haya {
    982   1.1      haya         struct mbuf		*m;
    983   1.1      haya         struct ifnet		*ifp;
    984   1.1      haya 	int			total_len = 0;
    985   1.1      haya 	u_int32_t		rxstat;
    986   1.1      haya 	caddr_t			rxbufpos;
    987   1.1      haya 	int			wrap = 0;
    988   1.1      haya 	u_int16_t		cur_rx;
    989   1.1      haya 	u_int16_t		limit;
    990   1.1      haya 	u_int16_t		rx_bytes = 0, max_bytes;
    991   1.1      haya 
    992   1.1      haya 	ifp = &sc->ethercom.ec_if;
    993   1.1      haya 
    994  1.10   tsutsui 	cur_rx = (CSR_READ_2(sc, RTK_CURRXADDR) + 16) % RTK_RXBUFLEN;
    995   1.1      haya 
    996   1.1      haya 	/* Do not try to read past this point. */
    997  1.10   tsutsui 	limit = CSR_READ_2(sc, RTK_CURRXBUF) % RTK_RXBUFLEN;
    998   1.1      haya 
    999   1.1      haya 	if (limit < cur_rx)
   1000  1.10   tsutsui 		max_bytes = (RTK_RXBUFLEN - cur_rx) + limit;
   1001   1.1      haya 	else
   1002   1.1      haya 		max_bytes = limit - cur_rx;
   1003   1.1      haya 
   1004  1.10   tsutsui 	while((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_EMPTY_RXBUF) == 0) {
   1005   1.8   thorpej 		rxbufpos = sc->rtk_cdata.rtk_rx_buf + cur_rx;
   1006   1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
   1007   1.4   tsutsui 		    sizeof(u_int32_t *), BUS_DMASYNC_POSTREAD);
   1008   1.3   tsutsui 		rxstat = le32toh(*(u_int32_t *)rxbufpos);
   1009   1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
   1010   1.4   tsutsui 		    sizeof(u_int32_t *), BUS_DMASYNC_PREREAD);
   1011   1.1      haya 
   1012   1.1      haya 		/*
   1013   1.1      haya 		 * Here's a totally undocumented fact for you. When the
   1014   1.1      haya 		 * RealTek chip is in the process of copying a packet into
   1015   1.1      haya 		 * RAM for you, the length will be 0xfff0. If you spot a
   1016   1.1      haya 		 * packet header with this value, you need to stop. The
   1017   1.1      haya 		 * datasheet makes absolutely no mention of this and
   1018   1.1      haya 		 * RealTek should be shot for this.
   1019   1.1      haya 		 */
   1020  1.10   tsutsui 		if ((u_int16_t)(rxstat >> 16) == RTK_RXSTAT_UNFINISHED)
   1021   1.1      haya 			break;
   1022   1.1      haya 
   1023  1.10   tsutsui 		if (!(rxstat & RTK_RXSTAT_RXOK)) {
   1024   1.1      haya 			ifp->if_ierrors++;
   1025   1.1      haya 
   1026   1.1      haya 			/*
   1027   1.1      haya 			 * submitted by:[netbsd-pcmcia:00484]
   1028   1.1      haya 			 *	Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
   1029   1.1      haya 			 * obtain from:
   1030   1.1      haya 			 *     FreeBSD if_rl.c rev 1.24->1.25
   1031   1.1      haya 			 *
   1032   1.1      haya 			 */
   1033   1.1      haya #if 0
   1034  1.10   tsutsui 			if (rxstat & (RTK_RXSTAT_BADSYM|RTK_RXSTAT_RUNT|
   1035  1.10   tsutsui 					RTK_RXSTAT_GIANT|RTK_RXSTAT_CRCERR|
   1036  1.10   tsutsui 					RTK_RXSTAT_ALIGNERR)) {
   1037  1.10   tsutsui 				CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB);
   1038  1.10   tsutsui 				CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB|
   1039  1.10   tsutsui 							RTK_CMD_RX_ENB);
   1040  1.10   tsutsui 				CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
   1041  1.10   tsutsui 				CSR_WRITE_4(sc, RTK_RXADDR,
   1042   1.1      haya 					    sc->recv_dmamap->dm_segs[0].ds_addr);
   1043  1.10   tsutsui 				CSR_WRITE_2(sc, RTK_CURRXADDR, cur_rx - 16);
   1044   1.1      haya 				cur_rx = 0;
   1045   1.1      haya 			}
   1046   1.1      haya 			break;
   1047   1.1      haya #else
   1048  1.15   thorpej 			rtk_init(ifp);
   1049   1.1      haya 			return;
   1050   1.1      haya #endif
   1051   1.1      haya 		}
   1052   1.1      haya 
   1053   1.1      haya 		/* No errors; receive the packet. */
   1054   1.1      haya 		total_len = rxstat >> 16;
   1055   1.1      haya 		rx_bytes += total_len + 4;
   1056   1.1      haya 
   1057   1.1      haya 		/*
   1058   1.1      haya 		 * Avoid trying to read more bytes than we know
   1059   1.1      haya 		 * the chip has prepared for us.
   1060   1.1      haya 		 */
   1061   1.1      haya 		if (rx_bytes > max_bytes)
   1062   1.1      haya 			break;
   1063   1.1      haya 
   1064   1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1065   1.6   tsutsui 		    cur_rx + sizeof(u_int32_t), total_len,
   1066   1.6   tsutsui 		    BUS_DMASYNC_POSTREAD);
   1067   1.4   tsutsui 
   1068   1.8   thorpej 		rxbufpos = sc->rtk_cdata.rtk_rx_buf +
   1069  1.10   tsutsui 			((cur_rx + sizeof(u_int32_t)) % RTK_RXBUFLEN);
   1070   1.1      haya 
   1071  1.10   tsutsui 		if (rxbufpos == (sc->rtk_cdata.rtk_rx_buf + RTK_RXBUFLEN))
   1072   1.8   thorpej 			rxbufpos = sc->rtk_cdata.rtk_rx_buf;
   1073   1.1      haya 
   1074  1.10   tsutsui 		wrap = (sc->rtk_cdata.rtk_rx_buf + RTK_RXBUFLEN) - rxbufpos;
   1075   1.1      haya 
   1076   1.1      haya 		if (total_len > wrap) {
   1077  1.10   tsutsui 			m = m_devget(rxbufpos - RTK_ETHER_ALIGN,
   1078  1.10   tsutsui 			   wrap + RTK_ETHER_ALIGN, 0, ifp, NULL);
   1079   1.1      haya 			if (m == NULL) {
   1080   1.1      haya 				ifp->if_ierrors++;
   1081   1.1      haya 				printf("%s: out of mbufs, tried to "
   1082   1.6   tsutsui 				    "copy %d bytes\n", sc->sc_dev.dv_xname,
   1083   1.6   tsutsui 				    wrap);
   1084  1.10   tsutsui 			} else {
   1085  1.10   tsutsui 				m_adj(m, RTK_ETHER_ALIGN);
   1086   1.1      haya 				m_copyback(m, wrap, total_len - wrap,
   1087   1.8   thorpej 					sc->rtk_cdata.rtk_rx_buf);
   1088   1.1      haya 			}
   1089   1.1      haya 			cur_rx = (total_len - wrap + ETHER_CRC_LEN);
   1090   1.1      haya 		} else {
   1091  1.10   tsutsui 			m = m_devget(rxbufpos - RTK_ETHER_ALIGN,
   1092  1.10   tsutsui 			    total_len + RTK_ETHER_ALIGN, 0, ifp, NULL);
   1093   1.1      haya 			if (m == NULL) {
   1094   1.1      haya 				ifp->if_ierrors++;
   1095   1.1      haya 				printf("%s: out of mbufs, tried to "
   1096   1.6   tsutsui 				    "copy %d bytes\n", sc->sc_dev.dv_xname,
   1097   1.6   tsutsui 				    total_len);
   1098   1.1      haya 			} else
   1099  1.10   tsutsui 				m_adj(m, RTK_ETHER_ALIGN);
   1100   1.1      haya 			cur_rx += total_len + 4 + ETHER_CRC_LEN;
   1101   1.1      haya 		}
   1102   1.1      haya 
   1103   1.1      haya 		/*
   1104   1.1      haya 		 * Round up to 32-bit boundary.
   1105   1.1      haya 		 */
   1106   1.1      haya 		cur_rx = (cur_rx + 3) & ~3;
   1107  1.10   tsutsui 		CSR_WRITE_2(sc, RTK_CURRXADDR, cur_rx - 16);
   1108   1.1      haya 
   1109   1.1      haya 		if (m == NULL)
   1110   1.1      haya 			continue;
   1111  1.16   thorpej 
   1112  1.16   thorpej 		/*
   1113  1.16   thorpej 		 * The RealTek chip includes the CRC with every
   1114  1.16   thorpej 		 * incoming packet.
   1115  1.16   thorpej 		 */
   1116  1.16   thorpej 		m->m_flags |= M_HASFCS;
   1117   1.1      haya 
   1118   1.1      haya 		ifp->if_ipackets++;
   1119   1.1      haya 
   1120   1.1      haya #if NBPFILTER > 0
   1121   1.1      haya 		/*
   1122   1.1      haya 		 * Handle BPF listeners. Let the BPF user see the packet, but
   1123   1.1      haya 		 * don't pass it up to the ether_input() layer unless it's
   1124   1.1      haya 		 * a broadcast packet, multicast packet, matches our ethernet
   1125   1.1      haya 		 * address or the interface is in promiscuous mode.
   1126   1.1      haya 		 */
   1127  1.14   thorpej 		if (ifp->if_bpf)
   1128   1.1      haya 			bpf_mtap(ifp->if_bpf, m);
   1129   1.1      haya #endif
   1130   1.1      haya 		/* pass it on. */
   1131   1.1      haya 		(*ifp->if_input)(ifp, m);
   1132   1.4   tsutsui 
   1133   1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1134   1.4   tsutsui 		    cur_rx + sizeof(u_int32_t),
   1135   1.4   tsutsui 		    total_len, BUS_DMASYNC_PREREAD);
   1136   1.1      haya 	}
   1137   1.1      haya 
   1138   1.1      haya 	return;
   1139   1.1      haya }
   1140   1.1      haya 
   1141   1.1      haya /*
   1142   1.1      haya  * A frame was downloaded to the chip. It's safe for us to clean up
   1143   1.1      haya  * the list buffers.
   1144   1.1      haya  */
   1145   1.8   thorpej STATIC void rtk_txeof(sc)
   1146  1.10   tsutsui 	struct rtk_softc	*sc;
   1147   1.1      haya {
   1148   1.1      haya 	struct ifnet		*ifp;
   1149   1.1      haya 	u_int32_t		txstat;
   1150   1.1      haya 
   1151   1.1      haya 	ifp = &sc->ethercom.ec_if;
   1152   1.1      haya 
   1153   1.1      haya 	/* Clear the timeout timer. */
   1154   1.1      haya 	ifp->if_timer = 0;
   1155   1.1      haya 
   1156   1.1      haya 	/*
   1157   1.1      haya 	 * Go through our tx list and free mbufs for those
   1158   1.1      haya 	 * frames that have been uploaded.
   1159   1.1      haya 	 */
   1160   1.1      haya 	do {
   1161  1.10   tsutsui 		txstat = CSR_READ_4(sc, RTK_LAST_TXSTAT(sc));
   1162  1.10   tsutsui 		if (!(txstat & (RTK_TXSTAT_TX_OK|
   1163  1.10   tsutsui 		    RTK_TXSTAT_TX_UNDERRUN|RTK_TXSTAT_TXABRT)))
   1164   1.1      haya 			break;
   1165   1.1      haya 
   1166   1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat,
   1167   1.8   thorpej 		    sc->snd_dmamap[sc->rtk_cdata.last_tx], 0,
   1168   1.8   thorpej 		    sc->snd_dmamap[sc->rtk_cdata.last_tx]->dm_mapsize,
   1169   1.4   tsutsui 		    BUS_DMASYNC_POSTWRITE);
   1170   1.4   tsutsui 		bus_dmamap_unload(sc->sc_dmat,
   1171   1.8   thorpej 		    sc->snd_dmamap[sc->rtk_cdata.last_tx]);
   1172  1.10   tsutsui 		m_freem(RTK_LAST_TXMBUF(sc));
   1173  1.10   tsutsui 		RTK_LAST_TXMBUF(sc) = NULL;
   1174   1.4   tsutsui 
   1175  1.10   tsutsui 		ifp->if_collisions += (txstat & RTK_TXSTAT_COLLCNT) >> 24;
   1176   1.1      haya 
   1177  1.10   tsutsui 		if (txstat & RTK_TXSTAT_TX_OK)
   1178   1.1      haya 			ifp->if_opackets++;
   1179   1.1      haya 		else {
   1180   1.1      haya 			ifp->if_oerrors++;
   1181  1.10   tsutsui 			if ((txstat & RTK_TXSTAT_TXABRT) ||
   1182  1.10   tsutsui 			    (txstat & RTK_TXSTAT_OUTOFWIN))
   1183  1.10   tsutsui 				CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1184   1.1      haya 		}
   1185  1.10   tsutsui 		RTK_INC(sc->rtk_cdata.last_tx);
   1186   1.1      haya 		ifp->if_flags &= ~IFF_OACTIVE;
   1187   1.8   thorpej 	} while (sc->rtk_cdata.last_tx != sc->rtk_cdata.cur_tx);
   1188   1.1      haya 
   1189   1.1      haya 	return;
   1190   1.1      haya }
   1191   1.1      haya 
   1192   1.8   thorpej int rtk_intr(arg)
   1193   1.1      haya 	void			*arg;
   1194   1.1      haya {
   1195  1.10   tsutsui 	struct rtk_softc	*sc;
   1196   1.1      haya 	struct ifnet		*ifp;
   1197   1.1      haya 	u_int16_t		status;
   1198   1.1      haya 	int handled = 0;
   1199   1.1      haya 
   1200   1.1      haya 	sc = arg;
   1201   1.1      haya 	ifp = &sc->ethercom.ec_if;
   1202   1.1      haya 
   1203   1.1      haya 	/* Disable interrupts. */
   1204  1.10   tsutsui 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1205   1.1      haya 
   1206   1.1      haya 	for (;;) {
   1207   1.1      haya 
   1208  1.10   tsutsui 		status = CSR_READ_2(sc, RTK_ISR);
   1209   1.1      haya 		if (status)
   1210  1.10   tsutsui 			CSR_WRITE_2(sc, RTK_ISR, status);
   1211   1.1      haya 
   1212   1.1      haya 		handled = 1;
   1213   1.1      haya 
   1214  1.10   tsutsui 		if ((status & RTK_INTRS) == 0)
   1215   1.1      haya 			break;
   1216   1.1      haya 
   1217  1.10   tsutsui 		if (status & RTK_ISR_RX_OK)
   1218   1.8   thorpej 			rtk_rxeof(sc);
   1219   1.1      haya 
   1220  1.10   tsutsui 		if (status & RTK_ISR_RX_ERR)
   1221   1.8   thorpej 			rtk_rxeof(sc);
   1222   1.1      haya 
   1223  1.10   tsutsui 		if ((status & RTK_ISR_TX_OK) || (status & RTK_ISR_TX_ERR))
   1224   1.8   thorpej 			rtk_txeof(sc);
   1225   1.1      haya 
   1226  1.10   tsutsui 		if (status & RTK_ISR_SYSTEM_ERR) {
   1227   1.8   thorpej 			rtk_reset(sc);
   1228  1.15   thorpej 			rtk_init(ifp);
   1229   1.1      haya 		}
   1230   1.1      haya 
   1231   1.1      haya 	}
   1232   1.1      haya 
   1233   1.1      haya 	/* Re-enable interrupts. */
   1234  1.10   tsutsui 	CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
   1235   1.1      haya 
   1236   1.1      haya 	if (ifp->if_snd.ifq_head != NULL) {
   1237   1.8   thorpej 		rtk_start(ifp);
   1238   1.1      haya 	}
   1239   1.1      haya 
   1240   1.1      haya 	return (handled);
   1241   1.1      haya }
   1242   1.1      haya 
   1243   1.1      haya /*
   1244   1.1      haya  * Main transmit routine.
   1245   1.1      haya  */
   1246   1.1      haya 
   1247   1.8   thorpej STATIC void rtk_start(ifp)
   1248   1.1      haya 	struct ifnet		*ifp;
   1249   1.1      haya {
   1250  1.10   tsutsui 	struct rtk_softc	*sc;
   1251   1.4   tsutsui 	struct mbuf		*m_head = NULL, *m_new;
   1252   1.4   tsutsui 	int			error, idx, len;
   1253   1.1      haya 
   1254   1.1      haya 	sc = ifp->if_softc;
   1255   1.1      haya 
   1256  1.10   tsutsui 	while(RTK_CUR_TXMBUF(sc) == NULL) {
   1257   1.1      haya 		IF_DEQUEUE(&ifp->if_snd, m_head);
   1258   1.1      haya 		if (m_head == NULL)
   1259   1.1      haya 			break;
   1260   1.1      haya 
   1261   1.8   thorpej 		idx = sc->rtk_cdata.cur_tx;
   1262   1.4   tsutsui 
   1263   1.4   tsutsui 		/*
   1264   1.4   tsutsui 		 * Load the DMA map.  If this fails, the packet didn't
   1265   1.4   tsutsui 		 * fit in one DMA segment, and we need to copy.  Note,
   1266   1.4   tsutsui 		 * the packet must also be aligned.
   1267   1.4   tsutsui 		 */
   1268   1.4   tsutsui 		if ((mtod(m_head, bus_addr_t) & 3) != 0 ||
   1269   1.4   tsutsui 		    bus_dmamap_load_mbuf(sc->sc_dmat, sc->snd_dmamap[idx],
   1270   1.4   tsutsui 			m_head, BUS_DMA_NOWAIT) != 0) {
   1271   1.4   tsutsui 			MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1272   1.4   tsutsui 			if (m_new == NULL) {
   1273   1.4   tsutsui 				printf("%s: unable to allocate Tx mbuf\n",
   1274   1.4   tsutsui 				    sc->sc_dev.dv_xname);
   1275   1.4   tsutsui 				IF_PREPEND(&ifp->if_snd, m_new);
   1276   1.4   tsutsui 				break;
   1277   1.4   tsutsui 			}
   1278   1.4   tsutsui 			if (m_head->m_pkthdr.len > MHLEN) {
   1279   1.4   tsutsui 				MCLGET(m_new, M_DONTWAIT);
   1280   1.4   tsutsui 				if ((m_new->m_flags & M_EXT) == 0) {
   1281   1.4   tsutsui 					printf("%s: unable to allocate Tx "
   1282   1.4   tsutsui 					    "cluster\n", sc->sc_dev.dv_xname);
   1283   1.4   tsutsui 					m_freem(m_new);
   1284   1.4   tsutsui 					IF_PREPEND(&ifp->if_snd, m_head);
   1285   1.4   tsutsui 					break;
   1286   1.4   tsutsui 				}
   1287   1.4   tsutsui 			}
   1288   1.4   tsutsui 			m_copydata(m_head, 0, m_head->m_pkthdr.len,
   1289   1.4   tsutsui 			    mtod(m_new, caddr_t));
   1290   1.4   tsutsui 			m_new->m_pkthdr.len = m_new->m_len =
   1291   1.4   tsutsui 			    m_head->m_pkthdr.len;
   1292   1.4   tsutsui 			m_freem(m_head);
   1293   1.4   tsutsui 			m_head = m_new;
   1294   1.4   tsutsui 			error = bus_dmamap_load_mbuf(sc->sc_dmat,
   1295   1.4   tsutsui 			    sc->snd_dmamap[idx], m_head, BUS_DMA_NOWAIT);
   1296   1.4   tsutsui 			if (error) {
   1297   1.4   tsutsui 				printf("%s: unable to load Tx buffer, "
   1298   1.4   tsutsui 				    "error = %d\n", sc->sc_dev.dv_xname, error);
   1299   1.4   tsutsui 				IF_PREPEND(&ifp->if_snd, m_head);
   1300   1.4   tsutsui 				break;
   1301   1.4   tsutsui 			}
   1302   1.4   tsutsui 		}
   1303   1.4   tsutsui 
   1304  1.10   tsutsui 		RTK_CUR_TXMBUF(sc) = m_head;
   1305   1.1      haya 
   1306   1.1      haya #if NBPFILTER > 0
   1307   1.1      haya 		/*
   1308   1.1      haya 		 * If there's a BPF listener, bounce a copy of this frame
   1309   1.1      haya 		 * to him.
   1310   1.1      haya 		 */
   1311   1.1      haya 		if (ifp->if_bpf)
   1312  1.10   tsutsui 			bpf_mtap(ifp->if_bpf, RTK_CUR_TXMBUF(sc));
   1313   1.1      haya #endif
   1314   1.1      haya 		/*
   1315   1.1      haya 		 * Transmit the frame.
   1316   1.1      haya 	 	 */
   1317   1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat,
   1318   1.4   tsutsui 		    sc->snd_dmamap[idx], 0, sc->snd_dmamap[idx]->dm_mapsize,
   1319   1.4   tsutsui 		    BUS_DMASYNC_PREWRITE);
   1320   1.4   tsutsui 
   1321   1.4   tsutsui 		len = sc->snd_dmamap[idx]->dm_segs[0].ds_len;
   1322   1.4   tsutsui 		if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
   1323   1.4   tsutsui 			len = (ETHER_MIN_LEN - ETHER_CRC_LEN);
   1324   1.4   tsutsui 
   1325  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_CUR_TXADDR(sc),
   1326   1.4   tsutsui 			    sc->snd_dmamap[idx]->dm_segs[0].ds_addr);
   1327  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_CUR_TXSTAT(sc), RTK_TX_EARLYTHRESH | len);
   1328   1.1      haya 
   1329  1.10   tsutsui 		RTK_INC(sc->rtk_cdata.cur_tx);
   1330   1.1      haya 	}
   1331   1.1      haya 
   1332   1.1      haya 	/*
   1333   1.1      haya 	 * We broke out of the loop because all our TX slots are
   1334   1.1      haya 	 * full. Mark the NIC as busy until it drains some of the
   1335   1.1      haya 	 * packets from the queue.
   1336   1.1      haya 	 */
   1337  1.10   tsutsui 	if (RTK_CUR_TXMBUF(sc) != NULL)
   1338   1.1      haya 		ifp->if_flags |= IFF_OACTIVE;
   1339   1.1      haya 
   1340   1.1      haya 	/*
   1341   1.1      haya 	 * Set a timeout in case the chip goes out to lunch.
   1342   1.1      haya 	 */
   1343   1.1      haya 	ifp->if_timer = 5;
   1344   1.1      haya 
   1345   1.1      haya 	return;
   1346   1.1      haya }
   1347   1.1      haya 
   1348  1.15   thorpej STATIC int rtk_init(ifp)
   1349  1.15   thorpej 	struct ifnet *ifp;
   1350   1.1      haya {
   1351  1.15   thorpej 	struct rtk_softc	*sc = ifp->if_softc;
   1352  1.15   thorpej 	int			error = 0, i;
   1353   1.4   tsutsui 	u_int32_t		rxcfg;
   1354   1.1      haya 
   1355  1.15   thorpej 	if ((error = rtk_enable(sc)) != 0)
   1356  1.15   thorpej 		goto out;
   1357   1.1      haya 
   1358   1.1      haya 	/*
   1359  1.15   thorpej 	 * Cancel pending I/O.
   1360   1.1      haya 	 */
   1361  1.15   thorpej 	rtk_stop(ifp, 0);
   1362   1.1      haya 
   1363   1.1      haya 	/* Init our MAC address */
   1364   1.1      haya 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1365  1.10   tsutsui 		CSR_WRITE_1(sc, RTK_IDR0 + i, LLADDR(ifp->if_sadl)[i]);
   1366   1.1      haya 	}
   1367   1.1      haya 
   1368   1.1      haya 	/* Init the RX buffer pointer register. */
   1369   1.4   tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, 0,
   1370   1.4   tsutsui 	    sc->recv_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1371  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
   1372   1.1      haya 
   1373   1.1      haya 	/* Init TX descriptors. */
   1374   1.8   thorpej 	rtk_list_tx_init(sc);
   1375   1.1      haya 
   1376   1.1      haya 	/*
   1377   1.1      haya 	 * Enable transmit and receive.
   1378   1.1      haya 	 */
   1379  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
   1380   1.1      haya 
   1381   1.1      haya 	/*
   1382   1.1      haya 	 * Set the initial TX and RX configuration.
   1383   1.1      haya 	 */
   1384  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1385  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
   1386   1.1      haya 
   1387   1.1      haya 	/* Set the individual bit to receive frames for this host only. */
   1388  1.10   tsutsui 	rxcfg = CSR_READ_4(sc, RTK_RXCFG);
   1389  1.10   tsutsui 	rxcfg |= RTK_RXCFG_RX_INDIV;
   1390   1.1      haya 
   1391   1.1      haya 	/* If we want promiscuous mode, set the allframes bit. */
   1392   1.1      haya 	if (ifp->if_flags & IFF_PROMISC) {
   1393  1.10   tsutsui 		rxcfg |= RTK_RXCFG_RX_ALLPHYS;
   1394  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1395   1.1      haya 	} else {
   1396  1.10   tsutsui 		rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
   1397  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1398   1.1      haya 	}
   1399   1.1      haya 
   1400   1.1      haya 	/*
   1401   1.1      haya 	 * Set capture broadcast bit to capture broadcast frames.
   1402   1.1      haya 	 */
   1403   1.1      haya 	if (ifp->if_flags & IFF_BROADCAST) {
   1404  1.10   tsutsui 		rxcfg |= RTK_RXCFG_RX_BROAD;
   1405  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1406   1.1      haya 	} else {
   1407  1.10   tsutsui 		rxcfg &= ~RTK_RXCFG_RX_BROAD;
   1408  1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1409   1.1      haya 	}
   1410   1.1      haya 
   1411   1.1      haya 	/*
   1412   1.1      haya 	 * Program the multicast filter, if necessary.
   1413   1.1      haya 	 */
   1414   1.8   thorpej 	rtk_setmulti(sc);
   1415   1.1      haya 
   1416   1.1      haya 	/*
   1417   1.1      haya 	 * Enable interrupts.
   1418   1.1      haya 	 */
   1419  1.10   tsutsui 	CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
   1420   1.1      haya 
   1421   1.1      haya 	/* Start RX/TX process. */
   1422  1.10   tsutsui 	CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
   1423   1.1      haya 
   1424   1.1      haya 	/* Enable receiver and transmitter. */
   1425  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
   1426   1.1      haya 
   1427  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD|RTK_CFG1_FULLDUPLEX);
   1428   1.1      haya 
   1429   1.1      haya 	/*
   1430   1.1      haya 	 * Set current media.
   1431   1.1      haya 	 */
   1432   1.1      haya 	mii_mediachg(&sc->mii);
   1433   1.1      haya 
   1434   1.1      haya 	ifp->if_flags |= IFF_RUNNING;
   1435   1.1      haya 	ifp->if_flags &= ~IFF_OACTIVE;
   1436   1.1      haya 
   1437  1.15   thorpej 	callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
   1438   1.1      haya 
   1439  1.15   thorpej  out:
   1440  1.15   thorpej 	if (error) {
   1441  1.15   thorpej 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1442  1.15   thorpej 		ifp->if_timer = 0;
   1443  1.15   thorpej 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1444  1.15   thorpej 	}
   1445  1.15   thorpej 	return (error);
   1446   1.1      haya }
   1447   1.1      haya 
   1448   1.1      haya /*
   1449   1.1      haya  * Set media options.
   1450   1.1      haya  */
   1451   1.8   thorpej STATIC int rtk_ifmedia_upd(ifp)
   1452   1.1      haya 	struct ifnet		*ifp;
   1453   1.1      haya {
   1454  1.10   tsutsui 	struct rtk_softc	*sc;
   1455   1.1      haya 
   1456   1.1      haya 	sc = ifp->if_softc;
   1457   1.1      haya 
   1458   1.1      haya 	return (mii_mediachg(&sc->mii));
   1459   1.1      haya }
   1460   1.1      haya 
   1461   1.1      haya /*
   1462   1.1      haya  * Report current media status.
   1463   1.1      haya  */
   1464   1.8   thorpej STATIC void rtk_ifmedia_sts(ifp, ifmr)
   1465   1.1      haya 	struct ifnet		*ifp;
   1466   1.1      haya 	struct ifmediareq	*ifmr;
   1467   1.1      haya {
   1468  1.10   tsutsui 	struct rtk_softc	*sc;
   1469   1.1      haya 
   1470   1.1      haya 	sc = ifp->if_softc;
   1471   1.1      haya 
   1472   1.1      haya 	mii_pollstat(&sc->mii);
   1473   1.1      haya 	ifmr->ifm_status = sc->mii.mii_media_status;
   1474   1.1      haya 	ifmr->ifm_active = sc->mii.mii_media_active;
   1475   1.1      haya }
   1476   1.1      haya 
   1477   1.8   thorpej STATIC int rtk_ioctl(ifp, command, data)
   1478   1.1      haya 	struct ifnet		*ifp;
   1479   1.1      haya 	u_long			command;
   1480   1.1      haya 	caddr_t			data;
   1481   1.1      haya {
   1482  1.10   tsutsui 	struct rtk_softc	*sc = ifp->if_softc;
   1483   1.1      haya 	struct ifreq		*ifr = (struct ifreq *) data;
   1484   1.1      haya 	int			s, error = 0;
   1485   1.1      haya 
   1486   1.9   thorpej 	s = splnet();
   1487   1.1      haya 
   1488  1.12  drochner 	switch (command) {
   1489   1.1      haya 	case SIOCGIFMEDIA:
   1490   1.1      haya 	case SIOCSIFMEDIA:
   1491   1.1      haya 		error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
   1492   1.1      haya 		break;
   1493  1.15   thorpej 
   1494   1.1      haya 	default:
   1495  1.15   thorpej 		error = ether_ioctl(ifp, command, data);
   1496  1.15   thorpej 		if (error == ENETRESET) {
   1497  1.15   thorpej 			if (RTK_IS_ENABLED(sc)) {
   1498  1.15   thorpej 				/*
   1499  1.15   thorpej 				 * Multicast list has changed.  Set the
   1500  1.15   thorpej 				 * hardware filter accordingly.
   1501  1.15   thorpej 				 */
   1502  1.15   thorpej 				rtk_setmulti(sc);
   1503  1.15   thorpej 			}
   1504  1.15   thorpej 			error = 0;
   1505  1.15   thorpej 		}
   1506   1.1      haya 		break;
   1507   1.1      haya 	}
   1508   1.1      haya 
   1509  1.12  drochner 	splx(s);
   1510   1.1      haya 
   1511   1.1      haya 	return(error);
   1512   1.1      haya }
   1513   1.1      haya 
   1514   1.8   thorpej STATIC void rtk_watchdog(ifp)
   1515   1.1      haya 	struct ifnet		*ifp;
   1516   1.1      haya {
   1517  1.10   tsutsui 	struct rtk_softc	*sc;
   1518   1.1      haya 
   1519   1.1      haya 	sc = ifp->if_softc;
   1520   1.1      haya 
   1521   1.1      haya 	printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
   1522   1.1      haya 	ifp->if_oerrors++;
   1523   1.8   thorpej 	rtk_txeof(sc);
   1524   1.8   thorpej 	rtk_rxeof(sc);
   1525  1.15   thorpej 	rtk_init(ifp);
   1526   1.1      haya 
   1527   1.1      haya 	return;
   1528   1.1      haya }
   1529   1.1      haya 
   1530   1.1      haya /*
   1531   1.1      haya  * Stop the adapter and free any mbufs allocated to the
   1532   1.1      haya  * RX and TX lists.
   1533   1.1      haya  */
   1534  1.15   thorpej STATIC void rtk_stop(ifp, disable)
   1535  1.15   thorpej 	struct ifnet *ifp;
   1536  1.15   thorpej 	int disable;
   1537   1.1      haya {
   1538  1.15   thorpej 	struct rtk_softc *sc = ifp->if_softc;
   1539  1.15   thorpej 	int i;
   1540   1.1      haya 
   1541   1.8   thorpej 	callout_stop(&sc->rtk_tick_ch);
   1542   1.1      haya 
   1543   1.1      haya 	mii_down(&sc->mii);
   1544   1.1      haya 
   1545  1.10   tsutsui 	CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
   1546  1.10   tsutsui 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1547   1.1      haya 
   1548   1.1      haya 	/*
   1549   1.1      haya 	 * Free the TX list buffers.
   1550   1.1      haya 	 */
   1551  1.10   tsutsui 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
   1552   1.8   thorpej 		if (sc->rtk_cdata.rtk_tx_chain[i] != NULL) {
   1553  1.11   tsutsui 			bus_dmamap_unload(sc->sc_dmat, sc->snd_dmamap[i]);
   1554   1.8   thorpej 			m_freem(sc->rtk_cdata.rtk_tx_chain[i]);
   1555   1.8   thorpej 			sc->rtk_cdata.rtk_tx_chain[i] = NULL;
   1556  1.10   tsutsui 			CSR_WRITE_4(sc, RTK_TXADDR0 + i, 0x0000000);
   1557   1.1      haya 		}
   1558   1.1      haya 	}
   1559   1.1      haya 
   1560  1.15   thorpej 	if (disable)
   1561  1.15   thorpej 		rtk_disable(sc);
   1562  1.15   thorpej 
   1563   1.1      haya 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1564  1.15   thorpej 	ifp->if_timer = 0;
   1565   1.1      haya }
   1566   1.1      haya 
   1567   1.1      haya /*
   1568   1.1      haya  * Stop all chip I/O so that the kernel's probe routines don't
   1569   1.1      haya  * get confused by errant DMAs when rebooting.
   1570   1.1      haya  */
   1571   1.8   thorpej STATIC void rtk_shutdown(vsc)
   1572   1.1      haya 	void			*vsc;
   1573   1.1      haya {
   1574  1.10   tsutsui 	struct rtk_softc	*sc = (struct rtk_softc *)vsc;
   1575   1.1      haya 
   1576  1.15   thorpej 	rtk_stop(&sc->ethercom.ec_if, 0);
   1577   1.1      haya }
   1578   1.1      haya 
   1579   1.1      haya STATIC void
   1580   1.8   thorpej rtk_tick(arg)
   1581   1.1      haya 	void *arg;
   1582   1.1      haya {
   1583   1.8   thorpej 	struct rtk_softc *sc = arg;
   1584   1.1      haya 	int s = splnet();
   1585   1.1      haya 
   1586   1.1      haya 	mii_tick(&sc->mii);
   1587   1.1      haya 	splx(s);
   1588   1.1      haya 
   1589   1.8   thorpej 	callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
   1590   1.1      haya }
   1591