Home | History | Annotate | Line # | Download | only in ic
      1  1.115  riastrad /*	$NetBSD: rtl81x9.c,v 1.115 2024/06/29 12:11:11 riastradh 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.45   tsutsui  * 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.40     lukem 
     88   1.40     lukem #include <sys/cdefs.h>
     89  1.115  riastrad __KERNEL_RCSID(0, "$NetBSD: rtl81x9.c,v 1.115 2024/06/29 12:11:11 riastradh Exp $");
     90    1.1      haya 
     91    1.1      haya 
     92    1.1      haya #include <sys/param.h>
     93    1.1      haya #include <sys/systm.h>
     94    1.1      haya #include <sys/callout.h>
     95    1.1      haya #include <sys/device.h>
     96    1.1      haya #include <sys/sockio.h>
     97    1.1      haya #include <sys/mbuf.h>
     98    1.1      haya #include <sys/kernel.h>
     99    1.1      haya #include <sys/socket.h>
    100    1.1      haya 
    101    1.1      haya #include <net/if.h>
    102    1.1      haya #include <net/if_arp.h>
    103    1.1      haya #include <net/if_ether.h>
    104    1.1      haya #include <net/if_dl.h>
    105    1.1      haya #include <net/if_media.h>
    106    1.1      haya 
    107    1.1      haya #include <net/bpf.h>
    108   1.96  riastrad #include <sys/rndsource.h>
    109    1.1      haya 
    110   1.77        ad #include <sys/bus.h>
    111    1.3   tsutsui #include <machine/endian.h>
    112    1.1      haya 
    113    1.1      haya #include <dev/mii/mii.h>
    114    1.1      haya #include <dev/mii/miivar.h>
    115    1.1      haya 
    116    1.1      haya #include <dev/ic/rtl81x9reg.h>
    117    1.4   tsutsui #include <dev/ic/rtl81x9var.h>
    118    1.1      haya 
    119   1.85   tsutsui static void rtk_reset(struct rtk_softc *);
    120   1.85   tsutsui static void rtk_rxeof(struct rtk_softc *);
    121   1.85   tsutsui static void rtk_txeof(struct rtk_softc *);
    122   1.85   tsutsui static void rtk_start(struct ifnet *);
    123   1.85   tsutsui static int rtk_ioctl(struct ifnet *, u_long, void *);
    124   1.85   tsutsui static int rtk_init(struct ifnet *);
    125   1.85   tsutsui static void rtk_stop(struct ifnet *, int);
    126   1.85   tsutsui 
    127   1.85   tsutsui static void rtk_watchdog(struct ifnet *);
    128   1.85   tsutsui 
    129   1.85   tsutsui static void rtk_eeprom_putbyte(struct rtk_softc *, int, int);
    130   1.85   tsutsui static void rtk_mii_sync(struct rtk_softc *);
    131   1.85   tsutsui static void rtk_mii_send(struct rtk_softc *, uint32_t, int);
    132   1.85   tsutsui static int rtk_mii_readreg(struct rtk_softc *, struct rtk_mii_frame *);
    133   1.85   tsutsui static int rtk_mii_writereg(struct rtk_softc *, struct rtk_mii_frame *);
    134   1.85   tsutsui 
    135  1.104   msaitoh static int rtk_phy_readreg(device_t, int, int, uint16_t *);
    136  1.104   msaitoh static int rtk_phy_writereg(device_t, int, int, uint16_t);
    137   1.94      matt static void rtk_phy_statchg(struct ifnet *);
    138   1.85   tsutsui static void rtk_tick(void *);
    139   1.49     perry 
    140   1.85   tsutsui static int rtk_enable(struct rtk_softc *);
    141   1.85   tsutsui static void rtk_disable(struct rtk_softc *);
    142   1.10   tsutsui 
    143   1.85   tsutsui static void rtk_list_tx_init(struct rtk_softc *);
    144    1.1      haya 
    145    1.1      haya #define EE_SET(x)					\
    146   1.10   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD,			\
    147   1.10   tsutsui 		CSR_READ_1(sc, RTK_EECMD) | (x))
    148    1.1      haya 
    149    1.1      haya #define EE_CLR(x)					\
    150   1.10   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD,			\
    151   1.10   tsutsui 		CSR_READ_1(sc, RTK_EECMD) & ~(x))
    152    1.1      haya 
    153   1.67   tsutsui #define EE_DELAY()	DELAY(100)
    154   1.67   tsutsui 
    155   1.44    bouyer #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
    156   1.44    bouyer 
    157    1.1      haya /*
    158    1.1      haya  * Send a read command and address to the EEPROM, check for ACK.
    159    1.1      haya  */
    160   1.85   tsutsui static void
    161   1.62   tsutsui rtk_eeprom_putbyte(struct rtk_softc *sc, int addr, int addr_len)
    162    1.1      haya {
    163   1.63   tsutsui 	int d, i;
    164    1.1      haya 
    165   1.10   tsutsui 	d = (RTK_EECMD_READ << addr_len) | addr;
    166    1.1      haya 
    167    1.1      haya 	/*
    168    1.1      haya 	 * Feed in each bit and stobe the clock.
    169    1.1      haya 	 */
    170   1.23   tsutsui 	for (i = RTK_EECMD_LEN + addr_len; i > 0; i--) {
    171   1.23   tsutsui 		if (d & (1 << (i - 1))) {
    172   1.10   tsutsui 			EE_SET(RTK_EE_DATAIN);
    173    1.1      haya 		} else {
    174   1.10   tsutsui 			EE_CLR(RTK_EE_DATAIN);
    175    1.1      haya 		}
    176   1.67   tsutsui 		EE_DELAY();
    177   1.10   tsutsui 		EE_SET(RTK_EE_CLK);
    178   1.67   tsutsui 		EE_DELAY();
    179   1.10   tsutsui 		EE_CLR(RTK_EE_CLK);
    180   1.67   tsutsui 		EE_DELAY();
    181    1.1      haya 	}
    182    1.1      haya }
    183    1.1      haya 
    184    1.1      haya /*
    185    1.1      haya  * Read a word of data stored in the EEPROM at address 'addr.'
    186    1.1      haya  */
    187   1.63   tsutsui uint16_t
    188   1.62   tsutsui rtk_read_eeprom(struct rtk_softc *sc, int addr, int addr_len)
    189    1.1      haya {
    190   1.63   tsutsui 	uint16_t word;
    191   1.63   tsutsui 	int i;
    192    1.1      haya 
    193    1.1      haya 	/* Enter EEPROM access mode. */
    194   1.67   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_PROGRAM);
    195   1.67   tsutsui 	EE_DELAY();
    196   1.67   tsutsui 	EE_SET(RTK_EE_SEL);
    197    1.1      haya 
    198    1.1      haya 	/*
    199    1.1      haya 	 * Send address of word we want to read.
    200    1.1      haya 	 */
    201    1.8   thorpej 	rtk_eeprom_putbyte(sc, addr, addr_len);
    202    1.1      haya 
    203    1.1      haya 	/*
    204    1.1      haya 	 * Start reading bits from EEPROM.
    205    1.1      haya 	 */
    206   1.63   tsutsui 	word = 0;
    207   1.23   tsutsui 	for (i = 16; i > 0; i--) {
    208   1.10   tsutsui 		EE_SET(RTK_EE_CLK);
    209   1.67   tsutsui 		EE_DELAY();
    210   1.10   tsutsui 		if (CSR_READ_1(sc, RTK_EECMD) & RTK_EE_DATAOUT)
    211   1.23   tsutsui 			word |= 1 << (i - 1);
    212   1.10   tsutsui 		EE_CLR(RTK_EE_CLK);
    213   1.67   tsutsui 		EE_DELAY();
    214    1.1      haya 	}
    215    1.1      haya 
    216    1.1      haya 	/* Turn off EEPROM access mode. */
    217   1.10   tsutsui 	CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
    218    1.1      haya 
    219   1.63   tsutsui 	return word;
    220    1.1      haya }
    221    1.1      haya 
    222    1.1      haya /*
    223    1.1      haya  * MII access routines are provided for the 8129, which
    224    1.1      haya  * doesn't have a built-in PHY. For the 8139, we fake things
    225    1.8   thorpej  * up by diverting rtk_phy_readreg()/rtk_phy_writereg() to the
    226    1.1      haya  * direct access PHY registers.
    227    1.1      haya  */
    228    1.1      haya #define MII_SET(x)					\
    229   1.23   tsutsui 	CSR_WRITE_1(sc, RTK_MII,			\
    230   1.10   tsutsui 		CSR_READ_1(sc, RTK_MII) | (x))
    231    1.1      haya 
    232    1.1      haya #define MII_CLR(x)					\
    233   1.23   tsutsui 	CSR_WRITE_1(sc, RTK_MII,			\
    234   1.10   tsutsui 		CSR_READ_1(sc, RTK_MII) & ~(x))
    235    1.1      haya 
    236    1.1      haya /*
    237    1.1      haya  * Sync the PHYs by setting data bit and strobing the clock 32 times.
    238    1.1      haya  */
    239   1.85   tsutsui static void
    240   1.62   tsutsui rtk_mii_sync(struct rtk_softc *sc)
    241    1.1      haya {
    242   1.63   tsutsui 	int i;
    243    1.1      haya 
    244  1.105   msaitoh 	MII_SET(RTK_MII_DIR | RTK_MII_DATAOUT);
    245    1.1      haya 
    246    1.1      haya 	for (i = 0; i < 32; i++) {
    247   1.10   tsutsui 		MII_SET(RTK_MII_CLK);
    248    1.1      haya 		DELAY(1);
    249   1.10   tsutsui 		MII_CLR(RTK_MII_CLK);
    250    1.1      haya 		DELAY(1);
    251    1.1      haya 	}
    252    1.1      haya }
    253    1.1      haya 
    254    1.1      haya /*
    255    1.1      haya  * Clock a series of bits through the MII.
    256    1.1      haya  */
    257   1.85   tsutsui static void
    258   1.63   tsutsui rtk_mii_send(struct rtk_softc *sc, uint32_t bits, int cnt)
    259    1.1      haya {
    260   1.63   tsutsui 	int i;
    261    1.1      haya 
    262   1.10   tsutsui 	MII_CLR(RTK_MII_CLK);
    263    1.1      haya 
    264   1.23   tsutsui 	for (i = cnt; i > 0; i--) {
    265   1.61   tsutsui 		if (bits & (1 << (i - 1))) {
    266   1.10   tsutsui 			MII_SET(RTK_MII_DATAOUT);
    267   1.61   tsutsui 		} else {
    268   1.10   tsutsui 			MII_CLR(RTK_MII_DATAOUT);
    269   1.61   tsutsui 		}
    270    1.1      haya 		DELAY(1);
    271   1.10   tsutsui 		MII_CLR(RTK_MII_CLK);
    272    1.1      haya 		DELAY(1);
    273   1.10   tsutsui 		MII_SET(RTK_MII_CLK);
    274    1.1      haya 	}
    275    1.1      haya }
    276    1.1      haya 
    277    1.1      haya /*
    278    1.1      haya  * Read an PHY register through the MII.
    279    1.1      haya  */
    280   1.85   tsutsui static int
    281   1.62   tsutsui rtk_mii_readreg(struct rtk_softc *sc, struct rtk_mii_frame *frame)
    282    1.1      haya {
    283  1.104   msaitoh 	int i, ack, s, rv = 0;
    284    1.1      haya 
    285    1.9   thorpej 	s = splnet();
    286    1.1      haya 
    287    1.1      haya 	/*
    288    1.1      haya 	 * Set up frame for RX.
    289    1.1      haya 	 */
    290   1.10   tsutsui 	frame->mii_stdelim = RTK_MII_STARTDELIM;
    291   1.10   tsutsui 	frame->mii_opcode = RTK_MII_READOP;
    292    1.1      haya 	frame->mii_turnaround = 0;
    293    1.1      haya 	frame->mii_data = 0;
    294   1.23   tsutsui 
    295   1.10   tsutsui 	CSR_WRITE_2(sc, RTK_MII, 0);
    296    1.1      haya 
    297    1.1      haya 	/*
    298   1.61   tsutsui 	 * Turn on data xmit.
    299    1.1      haya 	 */
    300   1.10   tsutsui 	MII_SET(RTK_MII_DIR);
    301    1.1      haya 
    302    1.8   thorpej 	rtk_mii_sync(sc);
    303    1.1      haya 
    304    1.1      haya 	/*
    305    1.1      haya 	 * Send command/address info.
    306    1.1      haya 	 */
    307    1.8   thorpej 	rtk_mii_send(sc, frame->mii_stdelim, 2);
    308    1.8   thorpej 	rtk_mii_send(sc, frame->mii_opcode, 2);
    309    1.8   thorpej 	rtk_mii_send(sc, frame->mii_phyaddr, 5);
    310    1.8   thorpej 	rtk_mii_send(sc, frame->mii_regaddr, 5);
    311    1.1      haya 
    312    1.1      haya 	/* Idle bit */
    313  1.105   msaitoh 	MII_CLR((RTK_MII_CLK | RTK_MII_DATAOUT));
    314    1.1      haya 	DELAY(1);
    315   1.10   tsutsui 	MII_SET(RTK_MII_CLK);
    316    1.1      haya 	DELAY(1);
    317    1.1      haya 
    318    1.1      haya 	/* Turn off xmit. */
    319   1.10   tsutsui 	MII_CLR(RTK_MII_DIR);
    320    1.1      haya 
    321    1.1      haya 	/* Check for ack */
    322   1.10   tsutsui 	MII_CLR(RTK_MII_CLK);
    323    1.1      haya 	DELAY(1);
    324   1.56   tsutsui 	ack = CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN;
    325   1.10   tsutsui 	MII_SET(RTK_MII_CLK);
    326    1.1      haya 	DELAY(1);
    327    1.1      haya 
    328    1.1      haya 	/*
    329    1.1      haya 	 * Now try reading data bits. If the ack failed, we still
    330    1.1      haya 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
    331    1.1      haya 	 */
    332    1.1      haya 	if (ack) {
    333   1.23   tsutsui 		for (i = 0; i < 16; i++) {
    334   1.10   tsutsui 			MII_CLR(RTK_MII_CLK);
    335    1.1      haya 			DELAY(1);
    336   1.10   tsutsui 			MII_SET(RTK_MII_CLK);
    337    1.1      haya 			DELAY(1);
    338    1.1      haya 		}
    339  1.104   msaitoh 		rv = -1;
    340    1.1      haya 		goto fail;
    341    1.1      haya 	}
    342    1.1      haya 
    343   1.23   tsutsui 	for (i = 16; i > 0; i--) {
    344   1.10   tsutsui 		MII_CLR(RTK_MII_CLK);
    345    1.1      haya 		DELAY(1);
    346    1.1      haya 		if (!ack) {
    347   1.10   tsutsui 			if (CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN)
    348   1.23   tsutsui 				frame->mii_data |= 1 << (i - 1);
    349    1.1      haya 			DELAY(1);
    350    1.1      haya 		}
    351   1.10   tsutsui 		MII_SET(RTK_MII_CLK);
    352    1.1      haya 		DELAY(1);
    353    1.1      haya 	}
    354    1.1      haya 
    355   1.23   tsutsui  fail:
    356   1.10   tsutsui 	MII_CLR(RTK_MII_CLK);
    357    1.1      haya 	DELAY(1);
    358   1.10   tsutsui 	MII_SET(RTK_MII_CLK);
    359    1.1      haya 	DELAY(1);
    360    1.1      haya 
    361    1.1      haya 	splx(s);
    362    1.1      haya 
    363  1.104   msaitoh 	return rv;
    364    1.1      haya }
    365    1.1      haya 
    366    1.1      haya /*
    367    1.1      haya  * Write to a PHY register through the MII.
    368    1.1      haya  */
    369   1.85   tsutsui static int
    370   1.62   tsutsui rtk_mii_writereg(struct rtk_softc *sc, struct rtk_mii_frame *frame)
    371    1.1      haya {
    372   1.63   tsutsui 	int s;
    373    1.1      haya 
    374    1.9   thorpej 	s = splnet();
    375    1.1      haya 	/*
    376    1.1      haya 	 * Set up frame for TX.
    377    1.1      haya 	 */
    378   1.10   tsutsui 	frame->mii_stdelim = RTK_MII_STARTDELIM;
    379   1.10   tsutsui 	frame->mii_opcode = RTK_MII_WRITEOP;
    380   1.10   tsutsui 	frame->mii_turnaround = RTK_MII_TURNAROUND;
    381   1.51     perry 
    382    1.1      haya 	/*
    383   1.61   tsutsui 	 * Turn on data output.
    384    1.1      haya 	 */
    385   1.10   tsutsui 	MII_SET(RTK_MII_DIR);
    386    1.1      haya 
    387    1.8   thorpej 	rtk_mii_sync(sc);
    388    1.1      haya 
    389    1.8   thorpej 	rtk_mii_send(sc, frame->mii_stdelim, 2);
    390    1.8   thorpej 	rtk_mii_send(sc, frame->mii_opcode, 2);
    391    1.8   thorpej 	rtk_mii_send(sc, frame->mii_phyaddr, 5);
    392    1.8   thorpej 	rtk_mii_send(sc, frame->mii_regaddr, 5);
    393    1.8   thorpej 	rtk_mii_send(sc, frame->mii_turnaround, 2);
    394    1.8   thorpej 	rtk_mii_send(sc, frame->mii_data, 16);
    395    1.1      haya 
    396    1.1      haya 	/* Idle bit. */
    397   1.10   tsutsui 	MII_SET(RTK_MII_CLK);
    398    1.1      haya 	DELAY(1);
    399   1.10   tsutsui 	MII_CLR(RTK_MII_CLK);
    400    1.1      haya 	DELAY(1);
    401    1.1      haya 
    402    1.1      haya 	/*
    403    1.1      haya 	 * Turn off xmit.
    404    1.1      haya 	 */
    405   1.10   tsutsui 	MII_CLR(RTK_MII_DIR);
    406    1.1      haya 
    407    1.1      haya 	splx(s);
    408    1.1      haya 
    409   1.63   tsutsui 	return 0;
    410    1.1      haya }
    411    1.1      haya 
    412   1.85   tsutsui static int
    413  1.104   msaitoh rtk_phy_readreg(device_t self, int phy, int reg, uint16_t *val)
    414    1.1      haya {
    415   1.78       uwe 	struct rtk_softc *sc = device_private(self);
    416   1.63   tsutsui 	struct rtk_mii_frame frame;
    417  1.104   msaitoh 	int rv;
    418   1.63   tsutsui 	int rtk8139_reg;
    419    1.1      haya 
    420   1.72   tsutsui 	if ((sc->sc_quirk & RTKQ_8129) == 0) {
    421    1.1      haya 		if (phy != 7)
    422  1.104   msaitoh 			return -1;
    423    1.1      haya 
    424   1.63   tsutsui 		switch (reg) {
    425    1.1      haya 		case MII_BMCR:
    426   1.10   tsutsui 			rtk8139_reg = RTK_BMCR;
    427    1.1      haya 			break;
    428    1.1      haya 		case MII_BMSR:
    429   1.10   tsutsui 			rtk8139_reg = RTK_BMSR;
    430    1.1      haya 			break;
    431    1.1      haya 		case MII_ANAR:
    432   1.10   tsutsui 			rtk8139_reg = RTK_ANAR;
    433    1.1      haya 			break;
    434   1.12  drochner 		case MII_ANER:
    435   1.12  drochner 			rtk8139_reg = RTK_ANER;
    436   1.12  drochner 			break;
    437    1.1      haya 		case MII_ANLPAR:
    438   1.10   tsutsui 			rtk8139_reg = RTK_LPAR;
    439    1.1      haya 			break;
    440  1.104   msaitoh 		case MII_PHYIDR1:
    441  1.104   msaitoh 		case MII_PHYIDR2:
    442  1.104   msaitoh 			*val = 0;
    443  1.104   msaitoh 			return 0;
    444    1.1      haya 		default:
    445    1.1      haya #if 0
    446   1.78       uwe 			printf("%s: bad phy register\n", device_xname(self));
    447    1.1      haya #endif
    448  1.104   msaitoh 			return -1;
    449    1.1      haya 		}
    450  1.104   msaitoh 		*val = CSR_READ_2(sc, rtk8139_reg);
    451  1.104   msaitoh 		return 0;
    452    1.1      haya 	}
    453    1.1      haya 
    454   1.84   tsutsui 	memset(&frame, 0, sizeof(frame));
    455    1.1      haya 
    456    1.1      haya 	frame.mii_phyaddr = phy;
    457    1.1      haya 	frame.mii_regaddr = reg;
    458  1.104   msaitoh 	rv = rtk_mii_readreg(sc, &frame);
    459  1.104   msaitoh 	*val = frame.mii_data;
    460    1.1      haya 
    461  1.104   msaitoh 	return rv;
    462    1.1      haya }
    463    1.1      haya 
    464  1.104   msaitoh static int
    465  1.104   msaitoh rtk_phy_writereg(device_t self, int phy, int reg, uint16_t val)
    466    1.1      haya {
    467   1.78       uwe 	struct rtk_softc *sc = device_private(self);
    468   1.63   tsutsui 	struct rtk_mii_frame frame;
    469   1.63   tsutsui 	int rtk8139_reg;
    470    1.1      haya 
    471   1.72   tsutsui 	if ((sc->sc_quirk & RTKQ_8129) == 0) {
    472    1.1      haya 		if (phy != 7)
    473  1.104   msaitoh 			return -1;
    474    1.1      haya 
    475   1.63   tsutsui 		switch (reg) {
    476    1.1      haya 		case MII_BMCR:
    477   1.10   tsutsui 			rtk8139_reg = RTK_BMCR;
    478    1.1      haya 			break;
    479    1.1      haya 		case MII_BMSR:
    480   1.10   tsutsui 			rtk8139_reg = RTK_BMSR;
    481    1.1      haya 			break;
    482    1.1      haya 		case MII_ANAR:
    483   1.10   tsutsui 			rtk8139_reg = RTK_ANAR;
    484    1.1      haya 			break;
    485   1.12  drochner 		case MII_ANER:
    486   1.12  drochner 			rtk8139_reg = RTK_ANER;
    487   1.12  drochner 			break;
    488    1.1      haya 		case MII_ANLPAR:
    489   1.10   tsutsui 			rtk8139_reg = RTK_LPAR;
    490    1.1      haya 			break;
    491    1.1      haya 		default:
    492    1.1      haya #if 0
    493   1.78       uwe 			printf("%s: bad phy register\n", device_xname(self));
    494    1.1      haya #endif
    495  1.104   msaitoh 			return -1;
    496    1.1      haya 		}
    497  1.104   msaitoh 		CSR_WRITE_2(sc, rtk8139_reg, val);
    498  1.104   msaitoh 		return 0;
    499    1.1      haya 	}
    500    1.1      haya 
    501   1.84   tsutsui 	memset(&frame, 0, sizeof(frame));
    502    1.1      haya 
    503    1.1      haya 	frame.mii_phyaddr = phy;
    504    1.1      haya 	frame.mii_regaddr = reg;
    505  1.104   msaitoh 	frame.mii_data = val;
    506    1.1      haya 
    507  1.104   msaitoh 	return rtk_mii_writereg(sc, &frame);
    508    1.1      haya }
    509    1.1      haya 
    510   1.85   tsutsui static void
    511   1.94      matt rtk_phy_statchg(struct ifnet *ifp)
    512    1.1      haya {
    513    1.1      haya 
    514    1.1      haya 	/* Nothing to do. */
    515    1.1      haya }
    516    1.1      haya 
    517    1.8   thorpej #define	rtk_calchash(addr) \
    518    1.7   thorpej 	(ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
    519    1.1      haya 
    520    1.1      haya /*
    521    1.1      haya  * Program the 64-bit multicast hash filter.
    522    1.1      haya  */
    523   1.50  jdolecek void
    524   1.62   tsutsui rtk_setmulti(struct rtk_softc *sc)
    525    1.1      haya {
    526  1.106   msaitoh 	struct ethercom *ec = &sc->ethercom;
    527  1.106   msaitoh 	struct ifnet *ifp = &ec->ec_if;
    528   1.63   tsutsui 	uint32_t hashes[2] = { 0, 0 };
    529   1.72   tsutsui 	uint32_t rxfilt;
    530    1.1      haya 	struct ether_multi *enm;
    531    1.1      haya 	struct ether_multistep step;
    532   1.63   tsutsui 	int h, mcnt;
    533    1.1      haya 
    534   1.10   tsutsui 	rxfilt = CSR_READ_4(sc, RTK_RXCFG);
    535    1.1      haya 
    536   1.28     enami 	if (ifp->if_flags & IFF_PROMISC) {
    537   1.63   tsutsui  allmulti:
    538   1.28     enami 		ifp->if_flags |= IFF_ALLMULTI;
    539   1.10   tsutsui 		rxfilt |= RTK_RXCFG_RX_MULTI;
    540   1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
    541   1.10   tsutsui 		CSR_WRITE_4(sc, RTK_MAR0, 0xFFFFFFFF);
    542   1.10   tsutsui 		CSR_WRITE_4(sc, RTK_MAR4, 0xFFFFFFFF);
    543    1.1      haya 		return;
    544    1.1      haya 	}
    545    1.1      haya 
    546    1.1      haya 	/* first, zot all the existing hash bits */
    547   1.10   tsutsui 	CSR_WRITE_4(sc, RTK_MAR0, 0);
    548   1.10   tsutsui 	CSR_WRITE_4(sc, RTK_MAR4, 0);
    549    1.1      haya 
    550    1.1      haya 	/* now program new ones */
    551  1.106   msaitoh 	ETHER_LOCK(ec);
    552  1.106   msaitoh 	ETHER_FIRST_MULTI(step, ec, enm);
    553   1.63   tsutsui 	mcnt = 0;
    554    1.1      haya 	while (enm != NULL) {
    555    1.4   tsutsui 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    556  1.106   msaitoh 		    ETHER_ADDR_LEN) != 0) {
    557  1.106   msaitoh 			ETHER_UNLOCK(ec);
    558   1.28     enami 			goto allmulti;
    559  1.106   msaitoh 		}
    560    1.4   tsutsui 
    561    1.8   thorpej 		h = rtk_calchash(enm->enm_addrlo);
    562    1.1      haya 		if (h < 32)
    563  1.107   msaitoh 			hashes[0] |= __BIT(h);
    564    1.1      haya 		else
    565  1.107   msaitoh 			hashes[1] |= __BIT(h - 32);
    566    1.1      haya 		mcnt++;
    567    1.1      haya 		ETHER_NEXT_MULTI(step, enm);
    568    1.1      haya 	}
    569  1.106   msaitoh 	ETHER_UNLOCK(ec);
    570   1.28     enami 
    571   1.28     enami 	ifp->if_flags &= ~IFF_ALLMULTI;
    572    1.1      haya 
    573    1.1      haya 	if (mcnt)
    574   1.10   tsutsui 		rxfilt |= RTK_RXCFG_RX_MULTI;
    575    1.1      haya 	else
    576   1.10   tsutsui 		rxfilt &= ~RTK_RXCFG_RX_MULTI;
    577    1.1      haya 
    578   1.10   tsutsui 	CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
    579   1.69   tsutsui 
    580   1.69   tsutsui 	/*
    581   1.69   tsutsui 	 * For some unfathomable reason, RealTek decided to reverse
    582   1.69   tsutsui 	 * the order of the multicast hash registers in the PCI Express
    583   1.69   tsutsui 	 * parts. This means we have to write the hash pattern in reverse
    584   1.69   tsutsui 	 * order for those devices.
    585   1.69   tsutsui 	 */
    586   1.72   tsutsui 	if ((sc->sc_quirk & RTKQ_PCIE) != 0) {
    587   1.69   tsutsui 		CSR_WRITE_4(sc, RTK_MAR0, bswap32(hashes[1]));
    588   1.69   tsutsui 		CSR_WRITE_4(sc, RTK_MAR4, bswap32(hashes[0]));
    589   1.69   tsutsui 	} else {
    590   1.69   tsutsui 		CSR_WRITE_4(sc, RTK_MAR0, hashes[0]);
    591   1.69   tsutsui 		CSR_WRITE_4(sc, RTK_MAR4, hashes[1]);
    592   1.69   tsutsui 	}
    593    1.1      haya }
    594    1.1      haya 
    595   1.50  jdolecek void
    596   1.62   tsutsui rtk_reset(struct rtk_softc *sc)
    597    1.1      haya {
    598   1.63   tsutsui 	int i;
    599    1.1      haya 
    600   1.10   tsutsui 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
    601    1.1      haya 
    602   1.10   tsutsui 	for (i = 0; i < RTK_TIMEOUT; i++) {
    603    1.1      haya 		DELAY(10);
    604   1.23   tsutsui 		if ((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET) == 0)
    605    1.1      haya 			break;
    606    1.1      haya 	}
    607   1.10   tsutsui 	if (i == RTK_TIMEOUT)
    608   1.82   tsutsui 		printf("%s: reset never completed!\n",
    609   1.82   tsutsui 		    device_xname(sc->sc_dev));
    610    1.1      haya }
    611    1.1      haya 
    612    1.1      haya /*
    613    1.1      haya  * Attach the interface. Allocate softc structures, do ifmedia
    614    1.1      haya  * setup and ethernet/BPF attach.
    615    1.1      haya  */
    616    1.1      haya void
    617   1.62   tsutsui rtk_attach(struct rtk_softc *sc)
    618    1.1      haya {
    619   1.82   tsutsui 	device_t self = sc->sc_dev;
    620    1.1      haya 	struct ifnet *ifp;
    621  1.105   msaitoh 	struct mii_data * const mii = &sc->mii;
    622   1.31   thorpej 	struct rtk_tx_desc *txd;
    623   1.63   tsutsui 	uint16_t val;
    624   1.63   tsutsui 	uint8_t eaddr[ETHER_ADDR_LEN];
    625   1.10   tsutsui 	int error;
    626   1.23   tsutsui 	int i, addr_len;
    627    1.1      haya 
    628   1.75        ad 	callout_init(&sc->rtk_tick_ch, 0);
    629  1.110   thorpej 	callout_setfunc(&sc->rtk_tick_ch, rtk_tick, sc);
    630    1.1      haya 
    631    1.6   tsutsui 	/*
    632    1.6   tsutsui 	 * Check EEPROM type 9346 or 9356.
    633    1.6   tsutsui 	 */
    634   1.10   tsutsui 	if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
    635   1.10   tsutsui 		addr_len = RTK_EEADDR_LEN1;
    636    1.6   tsutsui 	else
    637   1.10   tsutsui 		addr_len = RTK_EEADDR_LEN0;
    638    1.6   tsutsui 
    639    1.6   tsutsui 	/*
    640    1.6   tsutsui 	 * Get station address.
    641    1.6   tsutsui 	 */
    642   1.10   tsutsui 	val = rtk_read_eeprom(sc, RTK_EE_EADDR0, addr_len);
    643    1.6   tsutsui 	eaddr[0] = val & 0xff;
    644    1.6   tsutsui 	eaddr[1] = val >> 8;
    645   1.10   tsutsui 	val = rtk_read_eeprom(sc, RTK_EE_EADDR1, addr_len);
    646    1.6   tsutsui 	eaddr[2] = val & 0xff;
    647    1.6   tsutsui 	eaddr[3] = val >> 8;
    648   1.10   tsutsui 	val = rtk_read_eeprom(sc, RTK_EE_EADDR2, addr_len);
    649    1.6   tsutsui 	eaddr[4] = val & 0xff;
    650    1.6   tsutsui 	eaddr[5] = val >> 8;
    651    1.6   tsutsui 
    652    1.1      haya 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    653   1.23   tsutsui 	    RTK_RXBUFLEN + 16, PAGE_SIZE, 0, &sc->sc_dmaseg, 1, &sc->sc_dmanseg,
    654    1.1      haya 	    BUS_DMA_NOWAIT)) != 0) {
    655   1.78       uwe 		aprint_error_dev(self,
    656   1.82   tsutsui 		    "can't allocate recv buffer, error = %d\n", error);
    657   1.10   tsutsui 		goto fail_0;
    658    1.1      haya 	}
    659    1.1      haya 
    660   1.10   tsutsui 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg,
    661   1.71  christos 	    RTK_RXBUFLEN + 16, (void **)&sc->rtk_rx_buf,
    662  1.105   msaitoh 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    663   1.78       uwe 		aprint_error_dev(self,
    664   1.82   tsutsui 		    "can't map recv buffer, error = %d\n", error);
    665   1.10   tsutsui 		goto fail_1;
    666    1.1      haya 	}
    667    1.1      haya 
    668    1.1      haya 	if ((error = bus_dmamap_create(sc->sc_dmat,
    669   1.23   tsutsui 	    RTK_RXBUFLEN + 16, 1, RTK_RXBUFLEN + 16, 0, BUS_DMA_NOWAIT,
    670    1.1      haya 	    &sc->recv_dmamap)) != 0) {
    671   1.78       uwe 		aprint_error_dev(self,
    672   1.82   tsutsui 		    "can't create recv buffer DMA map, error = %d\n", error);
    673   1.10   tsutsui 		goto fail_2;
    674    1.1      haya 	}
    675    1.1      haya 
    676    1.1      haya 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->recv_dmamap,
    677   1.30   thorpej 	    sc->rtk_rx_buf, RTK_RXBUFLEN + 16,
    678  1.105   msaitoh 	    NULL, BUS_DMA_READ | BUS_DMA_NOWAIT)) != 0) {
    679   1.78       uwe 		aprint_error_dev(self,
    680   1.82   tsutsui 		    "can't load recv buffer DMA map, error = %d\n", error);
    681   1.10   tsutsui 		goto fail_3;
    682    1.1      haya 	}
    683    1.1      haya 
    684   1.31   thorpej 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    685   1.31   thorpej 		txd = &sc->rtk_tx_descs[i];
    686    1.4   tsutsui 		if ((error = bus_dmamap_create(sc->sc_dmat,
    687    1.6   tsutsui 		    MCLBYTES, 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
    688   1.31   thorpej 		    &txd->txd_dmamap)) != 0) {
    689   1.78       uwe 			aprint_error_dev(self,
    690   1.82   tsutsui 			    "can't create snd buffer DMA map, error = %d\n",
    691   1.82   tsutsui 			    error);
    692   1.10   tsutsui 			goto fail_4;
    693    1.5   tsutsui 		}
    694   1.31   thorpej 		txd->txd_txaddr = RTK_TXADDR0 + (i * 4);
    695   1.31   thorpej 		txd->txd_txstat = RTK_TXSTAT0 + (i * 4);
    696   1.31   thorpej 	}
    697   1.31   thorpej 	SIMPLEQ_INIT(&sc->rtk_tx_free);
    698   1.31   thorpej 	SIMPLEQ_INIT(&sc->rtk_tx_dirty);
    699   1.31   thorpej 
    700   1.10   tsutsui 	/*
    701   1.10   tsutsui 	 * From this point forward, the attachment cannot fail. A failure
    702  1.114    andvar 	 * before this releases all resources that may have been
    703   1.10   tsutsui 	 * allocated.
    704   1.10   tsutsui 	 */
    705   1.10   tsutsui 	sc->sc_flags |= RTK_ATTACHED;
    706    1.1      haya 
    707    1.6   tsutsui 	/* Reset the adapter. */
    708    1.8   thorpej 	rtk_reset(sc);
    709    1.6   tsutsui 
    710   1.78       uwe 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    711    1.6   tsutsui 
    712    1.1      haya 	ifp = &sc->ethercom.ec_if;
    713    1.1      haya 	ifp->if_softc = sc;
    714   1.78       uwe 	strcpy(ifp->if_xname, device_xname(self));
    715    1.1      haya 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    716    1.8   thorpej 	ifp->if_ioctl = rtk_ioctl;
    717    1.8   thorpej 	ifp->if_start = rtk_start;
    718    1.8   thorpej 	ifp->if_watchdog = rtk_watchdog;
    719   1.15   thorpej 	ifp->if_init = rtk_init;
    720   1.15   thorpej 	ifp->if_stop = rtk_stop;
    721   1.25   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    722    1.1      haya 
    723    1.1      haya 	/*
    724    1.1      haya 	 * Do ifmedia setup.
    725    1.1      haya 	 */
    726  1.105   msaitoh 	mii->mii_ifp = ifp;
    727  1.105   msaitoh 	mii->mii_readreg = rtk_phy_readreg;
    728  1.105   msaitoh 	mii->mii_writereg = rtk_phy_writereg;
    729  1.105   msaitoh 	mii->mii_statchg = rtk_phy_statchg;
    730  1.105   msaitoh 	sc->ethercom.ec_mii = mii;
    731  1.105   msaitoh 	ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
    732   1.81    dyoung 	    ether_mediastatus);
    733  1.105   msaitoh 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    734    1.1      haya 
    735    1.1      haya 	/* Choose a default media. */
    736  1.105   msaitoh 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    737  1.105   msaitoh 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    738  1.105   msaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    739  1.105   msaitoh 	} else
    740  1.105   msaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    741    1.1      haya 
    742    1.1      haya 	/*
    743    1.1      haya 	 * Call MI attach routines.
    744    1.1      haya 	 */
    745    1.1      haya 	if_attach(ifp);
    746  1.102     ozaki 	if_deferred_start_init(ifp, NULL);
    747    1.1      haya 	ether_ifattach(ifp, eaddr);
    748    1.1      haya 
    749   1.78       uwe 	rnd_attach_source(&sc->rnd_source, device_xname(self),
    750   1.95       tls 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    751   1.48       dan 
    752   1.10   tsutsui 	return;
    753   1.23   tsutsui  fail_4:
    754   1.31   thorpej 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    755   1.31   thorpej 		txd = &sc->rtk_tx_descs[i];
    756   1.31   thorpej 		if (txd->txd_dmamap != NULL)
    757   1.31   thorpej 			bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmamap);
    758   1.31   thorpej 	}
    759   1.23   tsutsui  fail_3:
    760   1.10   tsutsui 	bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
    761   1.23   tsutsui  fail_2:
    762   1.84   tsutsui 	bus_dmamem_unmap(sc->sc_dmat, sc->rtk_rx_buf,
    763   1.23   tsutsui 	    RTK_RXBUFLEN + 16);
    764   1.23   tsutsui  fail_1:
    765   1.10   tsutsui 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
    766   1.23   tsutsui  fail_0:
    767    1.1      haya 	return;
    768    1.1      haya }
    769    1.1      haya 
    770    1.1      haya /*
    771    1.1      haya  * Initialize the transmit descriptors.
    772    1.1      haya  */
    773   1.85   tsutsui static void
    774   1.62   tsutsui rtk_list_tx_init(struct rtk_softc *sc)
    775    1.1      haya {
    776   1.31   thorpej 	struct rtk_tx_desc *txd;
    777   1.31   thorpej 	int i;
    778   1.31   thorpej 
    779   1.31   thorpej 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL)
    780   1.41     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
    781   1.31   thorpej 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_free)) != NULL)
    782   1.41     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_free, txd_q);
    783    1.1      haya 
    784   1.10   tsutsui 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    785   1.31   thorpej 		txd = &sc->rtk_tx_descs[i];
    786   1.31   thorpej 		CSR_WRITE_4(sc, txd->txd_txaddr, 0);
    787   1.31   thorpej 		SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_free, txd, txd_q);
    788    1.1      haya 	}
    789    1.1      haya }
    790    1.1      haya 
    791    1.1      haya /*
    792   1.10   tsutsui  * rtk_activate:
    793   1.10   tsutsui  *     Handle device activation/deactivation requests.
    794   1.10   tsutsui  */
    795   1.10   tsutsui int
    796   1.78       uwe rtk_activate(device_t self, enum devact act)
    797   1.10   tsutsui {
    798   1.78       uwe 	struct rtk_softc *sc = device_private(self);
    799   1.23   tsutsui 
    800   1.10   tsutsui 	switch (act) {
    801   1.10   tsutsui 	case DVACT_DEACTIVATE:
    802   1.10   tsutsui 		if_deactivate(&sc->ethercom.ec_if);
    803   1.87    dyoung 		return 0;
    804   1.87    dyoung 	default:
    805   1.87    dyoung 		return EOPNOTSUPP;
    806   1.10   tsutsui 	}
    807   1.10   tsutsui }
    808   1.10   tsutsui 
    809   1.10   tsutsui /*
    810   1.10   tsutsui  * rtk_detach:
    811   1.10   tsutsui  *     Detach a rtk interface.
    812   1.10   tsutsui  */
    813   1.51     perry int
    814   1.62   tsutsui rtk_detach(struct rtk_softc *sc)
    815   1.10   tsutsui {
    816   1.10   tsutsui 	struct ifnet *ifp = &sc->ethercom.ec_if;
    817   1.31   thorpej 	struct rtk_tx_desc *txd;
    818   1.10   tsutsui 	int i;
    819   1.10   tsutsui 
    820   1.10   tsutsui 	/*
    821   1.39       wiz 	 * Succeed now if there isn't any work to do.
    822   1.10   tsutsui 	 */
    823   1.10   tsutsui 	if ((sc->sc_flags & RTK_ATTACHED) == 0)
    824   1.63   tsutsui 		return 0;
    825   1.23   tsutsui 
    826   1.10   tsutsui 	/* Unhook our tick handler. */
    827   1.10   tsutsui 	callout_stop(&sc->rtk_tick_ch);
    828   1.10   tsutsui 
    829   1.10   tsutsui 	/* Detach all PHYs. */
    830   1.10   tsutsui 	mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
    831   1.10   tsutsui 
    832   1.48       dan 	rnd_detach_source(&sc->rnd_source);
    833   1.48       dan 
    834   1.10   tsutsui 	ether_ifdetach(ifp);
    835   1.10   tsutsui 	if_detach(ifp);
    836   1.10   tsutsui 
    837  1.109   thorpej 	/* Delete all remaining media. */
    838  1.109   thorpej 	ifmedia_fini(&sc->mii.mii_media);
    839  1.109   thorpej 
    840   1.31   thorpej 	for (i = 0; i < RTK_TX_LIST_CNT; i++) {
    841   1.31   thorpej 		txd = &sc->rtk_tx_descs[i];
    842   1.31   thorpej 		if (txd->txd_dmamap != NULL)
    843   1.31   thorpej 			bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmamap);
    844   1.31   thorpej 	}
    845   1.10   tsutsui 	bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
    846   1.84   tsutsui 	bus_dmamem_unmap(sc->sc_dmat, sc->rtk_rx_buf,
    847   1.23   tsutsui 	    RTK_RXBUFLEN + 16);
    848   1.24   tsutsui 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
    849   1.10   tsutsui 
    850   1.91  jakllsch 	/* we don't want to run again */
    851   1.91  jakllsch 	sc->sc_flags &= ~RTK_ATTACHED;
    852   1.91  jakllsch 
    853   1.63   tsutsui 	return 0;
    854   1.10   tsutsui }
    855   1.10   tsutsui 
    856   1.10   tsutsui /*
    857   1.10   tsutsui  * rtk_enable:
    858   1.10   tsutsui  *     Enable the RTL81X9 chip.
    859   1.10   tsutsui  */
    860   1.51     perry int
    861   1.62   tsutsui rtk_enable(struct rtk_softc *sc)
    862   1.10   tsutsui {
    863   1.23   tsutsui 
    864   1.10   tsutsui 	if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
    865   1.10   tsutsui 		if ((*sc->sc_enable)(sc) != 0) {
    866   1.10   tsutsui 			printf("%s: device enable failed\n",
    867   1.82   tsutsui 			    device_xname(sc->sc_dev));
    868   1.63   tsutsui 			return EIO;
    869   1.10   tsutsui 		}
    870   1.10   tsutsui 		sc->sc_flags |= RTK_ENABLED;
    871   1.10   tsutsui 	}
    872   1.63   tsutsui 	return 0;
    873   1.10   tsutsui }
    874   1.10   tsutsui 
    875   1.10   tsutsui /*
    876   1.10   tsutsui  * rtk_disable:
    877   1.10   tsutsui  *     Disable the RTL81X9 chip.
    878   1.10   tsutsui  */
    879   1.51     perry void
    880   1.62   tsutsui rtk_disable(struct rtk_softc *sc)
    881   1.10   tsutsui {
    882   1.23   tsutsui 
    883   1.10   tsutsui 	if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
    884   1.10   tsutsui 		(*sc->sc_disable)(sc);
    885   1.10   tsutsui 		sc->sc_flags &= ~RTK_ENABLED;
    886   1.10   tsutsui 	}
    887   1.10   tsutsui }
    888   1.10   tsutsui 
    889   1.10   tsutsui /*
    890    1.1      haya  * A frame has been uploaded: pass the resulting mbuf chain up to
    891    1.1      haya  * the higher level protocols.
    892    1.1      haya  *
    893   1.22   tsutsui  * You know there's something wrong with a PCI bus-master chip design.
    894    1.1      haya  *
    895    1.1      haya  * The receive operation is badly documented in the datasheet, so I'll
    896    1.1      haya  * attempt to document it here. The driver provides a buffer area and
    897    1.1      haya  * places its base address in the RX buffer start address register.
    898    1.1      haya  * The chip then begins copying frames into the RX buffer. Each frame
    899   1.39       wiz  * is preceded by a 32-bit RX status word which specifies the length
    900    1.1      haya  * of the frame and certain other status bits. Each frame (starting with
    901    1.1      haya  * the status word) is also 32-bit aligned. The frame length is in the
    902    1.1      haya  * first 16 bits of the status word; the lower 15 bits correspond with
    903    1.1      haya  * the 'rx status register' mentioned in the datasheet.
    904    1.1      haya  *
    905    1.1      haya  * Note: to make the Alpha happy, the frame payload needs to be aligned
    906   1.22   tsutsui  * on a 32-bit boundary. To achieve this, we copy the data to mbuf
    907   1.22   tsutsui  * shifted forward 2 bytes.
    908    1.1      haya  */
    909   1.85   tsutsui static void
    910   1.62   tsutsui rtk_rxeof(struct rtk_softc *sc)
    911    1.1      haya {
    912   1.63   tsutsui 	struct mbuf *m;
    913   1.63   tsutsui 	struct ifnet *ifp;
    914   1.84   tsutsui 	uint8_t *rxbufpos, *dst;
    915   1.63   tsutsui 	u_int total_len, wrap;
    916   1.63   tsutsui 	uint32_t rxstat;
    917   1.63   tsutsui 	uint16_t cur_rx, new_rx;
    918   1.63   tsutsui 	uint16_t limit;
    919   1.63   tsutsui 	uint16_t rx_bytes, max_bytes;
    920    1.1      haya 
    921    1.1      haya 	ifp = &sc->ethercom.ec_if;
    922    1.1      haya 
    923   1.10   tsutsui 	cur_rx = (CSR_READ_2(sc, RTK_CURRXADDR) + 16) % RTK_RXBUFLEN;
    924    1.1      haya 
    925    1.1      haya 	/* Do not try to read past this point. */
    926   1.10   tsutsui 	limit = CSR_READ_2(sc, RTK_CURRXBUF) % RTK_RXBUFLEN;
    927    1.1      haya 
    928    1.1      haya 	if (limit < cur_rx)
    929   1.10   tsutsui 		max_bytes = (RTK_RXBUFLEN - cur_rx) + limit;
    930    1.1      haya 	else
    931    1.1      haya 		max_bytes = limit - cur_rx;
    932   1.63   tsutsui 	rx_bytes = 0;
    933    1.1      haya 
    934   1.63   tsutsui 	while ((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_EMPTY_RXBUF) == 0) {
    935   1.84   tsutsui 		rxbufpos = sc->rtk_rx_buf + cur_rx;
    936    1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
    937   1.21   tsutsui 		    RTK_RXSTAT_LEN, BUS_DMASYNC_POSTREAD);
    938   1.63   tsutsui 		rxstat = le32toh(*(uint32_t *)rxbufpos);
    939    1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
    940   1.21   tsutsui 		    RTK_RXSTAT_LEN, BUS_DMASYNC_PREREAD);
    941    1.1      haya 
    942    1.1      haya 		/*
    943    1.1      haya 		 * Here's a totally undocumented fact for you. When the
    944    1.1      haya 		 * RealTek chip is in the process of copying a packet into
    945    1.1      haya 		 * RAM for you, the length will be 0xfff0. If you spot a
    946    1.1      haya 		 * packet header with this value, you need to stop. The
    947    1.1      haya 		 * datasheet makes absolutely no mention of this and
    948    1.1      haya 		 * RealTek should be shot for this.
    949    1.1      haya 		 */
    950   1.22   tsutsui 		total_len = rxstat >> 16;
    951   1.22   tsutsui 		if (total_len == RTK_RXSTAT_UNFINISHED)
    952    1.1      haya 			break;
    953   1.22   tsutsui 
    954   1.27   tsutsui 		if ((rxstat & RTK_RXSTAT_RXOK) == 0 ||
    955   1.54   tsutsui 		    total_len < ETHER_MIN_LEN ||
    956   1.68   tsutsui 		    total_len > (MCLBYTES - RTK_ETHER_ALIGN)) {
    957  1.108   thorpej 			if_statinc(ifp, if_ierrors);
    958    1.1      haya 
    959    1.1      haya 			/*
    960   1.51     perry 			 * submitted by:[netbsd-pcmcia:00484]
    961    1.1      haya 			 *	Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
    962    1.1      haya 			 * obtain from:
    963    1.1      haya 			 *     FreeBSD if_rl.c rev 1.24->1.25
    964    1.1      haya 			 *
    965    1.1      haya 			 */
    966    1.1      haya #if 0
    967  1.105   msaitoh 			if (rxstat & (RTK_RXSTAT_BADSYM | RTK_RXSTAT_RUNT |
    968  1.105   msaitoh 			    RTK_RXSTAT_GIANT | RTK_RXSTAT_CRCERR |
    969   1.21   tsutsui 			    RTK_RXSTAT_ALIGNERR)) {
    970   1.10   tsutsui 				CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB);
    971   1.21   tsutsui 				CSR_WRITE_2(sc, RTK_COMMAND,
    972  1.105   msaitoh 				    RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
    973   1.10   tsutsui 				CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
    974   1.10   tsutsui 				CSR_WRITE_4(sc, RTK_RXADDR,
    975   1.21   tsutsui 				    sc->recv_dmamap->dm_segs[0].ds_addr);
    976    1.1      haya 				cur_rx = 0;
    977    1.1      haya 			}
    978    1.1      haya 			break;
    979    1.1      haya #else
    980   1.15   thorpej 			rtk_init(ifp);
    981    1.1      haya 			return;
    982    1.1      haya #endif
    983    1.1      haya 		}
    984    1.1      haya 
    985   1.51     perry 		/* No errors; receive the packet. */
    986   1.21   tsutsui 		rx_bytes += total_len + RTK_RXSTAT_LEN;
    987    1.1      haya 
    988    1.1      haya 		/*
    989    1.1      haya 		 * Avoid trying to read more bytes than we know
    990    1.1      haya 		 * the chip has prepared for us.
    991    1.1      haya 		 */
    992    1.1      haya 		if (rx_bytes > max_bytes)
    993    1.1      haya 			break;
    994    1.1      haya 
    995   1.22   tsutsui 		/*
    996   1.22   tsutsui 		 * Skip the status word, wrapping around to the beginning
    997   1.22   tsutsui 		 * of the Rx area, if necessary.
    998   1.22   tsutsui 		 */
    999   1.29   thorpej 		cur_rx = (cur_rx + RTK_RXSTAT_LEN) % RTK_RXBUFLEN;
   1000   1.84   tsutsui 		rxbufpos = sc->rtk_rx_buf + cur_rx;
   1001    1.4   tsutsui 
   1002   1.22   tsutsui 		/*
   1003   1.22   tsutsui 		 * Compute the number of bytes at which the packet
   1004   1.22   tsutsui 		 * will wrap to the beginning of the ring buffer.
   1005   1.22   tsutsui 		 */
   1006   1.29   thorpej 		wrap = RTK_RXBUFLEN - cur_rx;
   1007    1.1      haya 
   1008   1.22   tsutsui 		/*
   1009   1.22   tsutsui 		 * Compute where the next pending packet is.
   1010   1.22   tsutsui 		 */
   1011   1.22   tsutsui 		if (total_len > wrap)
   1012   1.22   tsutsui 			new_rx = total_len - wrap;
   1013   1.22   tsutsui 		else
   1014   1.22   tsutsui 			new_rx = cur_rx + total_len;
   1015   1.22   tsutsui 		/* Round up to 32-bit boundary. */
   1016   1.83   tsutsui 		new_rx = roundup2(new_rx, sizeof(uint32_t)) % RTK_RXBUFLEN;
   1017    1.1      haya 
   1018   1.22   tsutsui 		/*
   1019   1.54   tsutsui 		 * The RealTek chip includes the CRC with every
   1020   1.54   tsutsui 		 * incoming packet; trim it off here.
   1021   1.54   tsutsui 		 */
   1022   1.54   tsutsui 		total_len -= ETHER_CRC_LEN;
   1023   1.54   tsutsui 
   1024   1.54   tsutsui 		/*
   1025   1.22   tsutsui 		 * Now allocate an mbuf (and possibly a cluster) to hold
   1026   1.22   tsutsui 		 * the packet. Note we offset the packet 2 bytes so that
   1027   1.22   tsutsui 		 * data after the Ethernet header will be 4-byte aligned.
   1028   1.22   tsutsui 		 */
   1029   1.22   tsutsui 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1030   1.22   tsutsui 		if (m == NULL) {
   1031   1.22   tsutsui 			printf("%s: unable to allocate Rx mbuf\n",
   1032   1.82   tsutsui 			    device_xname(sc->sc_dev));
   1033  1.108   thorpej 			if_statinc(ifp, if_ierrors);
   1034   1.22   tsutsui 			goto next_packet;
   1035   1.22   tsutsui 		}
   1036  1.111   thorpej 		MCLAIM(m, &sc->ethercom.ec_rx_mowner);
   1037   1.22   tsutsui 		if (total_len > (MHLEN - RTK_ETHER_ALIGN)) {
   1038   1.22   tsutsui 			MCLGET(m, M_DONTWAIT);
   1039   1.22   tsutsui 			if ((m->m_flags & M_EXT) == 0) {
   1040   1.22   tsutsui 				printf("%s: unable to allocate Rx cluster\n",
   1041   1.82   tsutsui 				    device_xname(sc->sc_dev));
   1042  1.108   thorpej 				if_statinc(ifp, if_ierrors);
   1043   1.22   tsutsui 				m_freem(m);
   1044   1.22   tsutsui 				m = NULL;
   1045   1.22   tsutsui 				goto next_packet;
   1046   1.22   tsutsui 			}
   1047   1.22   tsutsui 		}
   1048   1.22   tsutsui 		m->m_data += RTK_ETHER_ALIGN;	/* for alignment */
   1049  1.100     ozaki 		m_set_rcvif(m, ifp);
   1050   1.22   tsutsui 		m->m_pkthdr.len = m->m_len = total_len;
   1051   1.71  christos 		dst = mtod(m, void *);
   1052    1.1      haya 
   1053   1.22   tsutsui 		/*
   1054   1.22   tsutsui 		 * If the packet wraps, copy up to the wrapping point.
   1055   1.22   tsutsui 		 */
   1056    1.1      haya 		if (total_len > wrap) {
   1057   1.22   tsutsui 			bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1058   1.22   tsutsui 			    cur_rx, wrap, BUS_DMASYNC_POSTREAD);
   1059   1.22   tsutsui 			memcpy(dst, rxbufpos, wrap);
   1060   1.22   tsutsui 			bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1061   1.22   tsutsui 			    cur_rx, wrap, BUS_DMASYNC_PREREAD);
   1062   1.22   tsutsui 			cur_rx = 0;
   1063   1.30   thorpej 			rxbufpos = sc->rtk_rx_buf;
   1064   1.22   tsutsui 			total_len -= wrap;
   1065   1.22   tsutsui 			dst += wrap;
   1066    1.1      haya 		}
   1067    1.1      haya 
   1068    1.1      haya 		/*
   1069   1.22   tsutsui 		 * ...and now the rest.
   1070    1.1      haya 		 */
   1071   1.22   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1072   1.22   tsutsui 		    cur_rx, total_len, BUS_DMASYNC_POSTREAD);
   1073   1.22   tsutsui 		memcpy(dst, rxbufpos, total_len);
   1074   1.22   tsutsui 		bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
   1075   1.22   tsutsui 		    cur_rx, total_len, BUS_DMASYNC_PREREAD);
   1076   1.22   tsutsui 
   1077   1.23   tsutsui  next_packet:
   1078   1.57   tsutsui 		CSR_WRITE_2(sc, RTK_CURRXADDR, (new_rx - 16) % RTK_RXBUFLEN);
   1079   1.22   tsutsui 		cur_rx = new_rx;
   1080    1.1      haya 
   1081    1.1      haya 		if (m == NULL)
   1082    1.1      haya 			continue;
   1083   1.16   thorpej 
   1084    1.1      haya 		/* pass it on. */
   1085   1.97     ozaki 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1086    1.1      haya 	}
   1087    1.1      haya }
   1088    1.1      haya 
   1089    1.1      haya /*
   1090    1.1      haya  * A frame was downloaded to the chip. It's safe for us to clean up
   1091    1.1      haya  * the list buffers.
   1092    1.1      haya  */
   1093   1.85   tsutsui static void
   1094   1.62   tsutsui rtk_txeof(struct rtk_softc *sc)
   1095    1.1      haya {
   1096   1.31   thorpej 	struct ifnet *ifp;
   1097   1.31   thorpej 	struct rtk_tx_desc *txd;
   1098   1.63   tsutsui 	uint32_t txstat;
   1099    1.1      haya 
   1100    1.1      haya 	ifp = &sc->ethercom.ec_if;
   1101    1.1      haya 
   1102    1.1      haya 	/*
   1103    1.1      haya 	 * Go through our tx list and free mbufs for those
   1104    1.1      haya 	 * frames that have been uploaded.
   1105    1.1      haya 	 */
   1106   1.31   thorpej 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL) {
   1107   1.31   thorpej 		txstat = CSR_READ_4(sc, txd->txd_txstat);
   1108  1.105   msaitoh 		if ((txstat & (RTK_TXSTAT_TX_OK |
   1109  1.105   msaitoh 		    RTK_TXSTAT_TX_UNDERRUN | RTK_TXSTAT_TXABRT)) == 0)
   1110    1.1      haya 			break;
   1111    1.1      haya 
   1112   1.41     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
   1113   1.31   thorpej 
   1114   1.31   thorpej 		bus_dmamap_sync(sc->sc_dmat, txd->txd_dmamap, 0,
   1115   1.31   thorpej 		    txd->txd_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1116   1.31   thorpej 		bus_dmamap_unload(sc->sc_dmat, txd->txd_dmamap);
   1117   1.31   thorpej 		m_freem(txd->txd_mbuf);
   1118   1.31   thorpej 		txd->txd_mbuf = NULL;
   1119    1.4   tsutsui 
   1120  1.108   thorpej 		net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   1121  1.115  riastrad 		if_statadd_ref(ifp, nsr, if_collisions,
   1122  1.108   thorpej 		    (txstat & RTK_TXSTAT_COLLCNT) >> 24);
   1123    1.1      haya 
   1124   1.10   tsutsui 		if (txstat & RTK_TXSTAT_TX_OK)
   1125  1.115  riastrad 			if_statinc_ref(ifp, nsr, if_opackets);
   1126    1.1      haya 		else {
   1127  1.115  riastrad 			if_statinc_ref(ifp, nsr, if_oerrors);
   1128   1.36   kanaoka 
   1129   1.36   kanaoka 			/*
   1130   1.36   kanaoka 			 * Increase Early TX threshold if underrun occurred.
   1131   1.36   kanaoka 			 * Increase step 64 bytes.
   1132   1.36   kanaoka 			 */
   1133   1.36   kanaoka 			if (txstat & RTK_TXSTAT_TX_UNDERRUN) {
   1134   1.52   xtraeme #ifdef DEBUG
   1135   1.36   kanaoka 				printf("%s: transmit underrun;",
   1136   1.82   tsutsui 				    device_xname(sc->sc_dev));
   1137   1.52   xtraeme #endif
   1138   1.65   tsutsui 				if (sc->sc_txthresh < RTK_TXTH_MAX) {
   1139   1.36   kanaoka 					sc->sc_txthresh += 2;
   1140   1.52   xtraeme #ifdef DEBUG
   1141   1.36   kanaoka 					printf(" new threshold: %d bytes",
   1142   1.36   kanaoka 					    sc->sc_txthresh * 32);
   1143   1.52   xtraeme #endif
   1144   1.36   kanaoka 				}
   1145   1.86   tsutsui #ifdef DEBUG
   1146   1.36   kanaoka 				printf("\n");
   1147   1.86   tsutsui #endif
   1148   1.36   kanaoka 			}
   1149  1.105   msaitoh 			if (txstat & (RTK_TXSTAT_TXABRT | RTK_TXSTAT_OUTOFWIN))
   1150   1.10   tsutsui 				CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1151    1.1      haya 		}
   1152  1.108   thorpej 		IF_STAT_PUTREF(ifp);
   1153   1.31   thorpej 		SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_free, txd, txd_q);
   1154    1.1      haya 		ifp->if_flags &= ~IFF_OACTIVE;
   1155   1.31   thorpej 	}
   1156   1.55   tsutsui 
   1157   1.55   tsutsui 	/* Clear the timeout timer if there is no pending packet. */
   1158   1.58   tsutsui 	if (SIMPLEQ_EMPTY(&sc->rtk_tx_dirty))
   1159   1.55   tsutsui 		ifp->if_timer = 0;
   1160   1.55   tsutsui 
   1161    1.1      haya }
   1162    1.1      haya 
   1163   1.50  jdolecek int
   1164   1.62   tsutsui rtk_intr(void *arg)
   1165    1.1      haya {
   1166   1.63   tsutsui 	struct rtk_softc *sc;
   1167   1.63   tsutsui 	struct ifnet *ifp;
   1168  1.112   tsutsui 	uint16_t status, rndstatus = 0;
   1169   1.63   tsutsui 	int handled;
   1170    1.1      haya 
   1171    1.1      haya 	sc = arg;
   1172    1.1      haya 	ifp = &sc->ethercom.ec_if;
   1173    1.1      haya 
   1174   1.82   tsutsui 	if (!device_has_power(sc->sc_dev))
   1175   1.80     joerg 		return 0;
   1176   1.80     joerg 
   1177    1.1      haya 	/* Disable interrupts. */
   1178   1.10   tsutsui 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1179    1.1      haya 
   1180   1.63   tsutsui 	handled = 0;
   1181    1.1      haya 	for (;;) {
   1182    1.1      haya 
   1183   1.10   tsutsui 		status = CSR_READ_2(sc, RTK_ISR);
   1184   1.74     joerg 
   1185   1.74     joerg 		if (status == 0xffff)
   1186   1.74     joerg 			break; /* Card is gone... */
   1187   1.74     joerg 
   1188  1.112   tsutsui 		if (status) {
   1189   1.10   tsutsui 			CSR_WRITE_2(sc, RTK_ISR, status);
   1190  1.112   tsutsui 			rndstatus = status;
   1191  1.112   tsutsui 		}
   1192    1.1      haya 
   1193   1.10   tsutsui 		if ((status & RTK_INTRS) == 0)
   1194    1.1      haya 			break;
   1195    1.1      haya 
   1196   1.59   tsutsui 		handled = 1;
   1197   1.59   tsutsui 
   1198   1.10   tsutsui 		if (status & RTK_ISR_RX_OK)
   1199    1.8   thorpej 			rtk_rxeof(sc);
   1200    1.1      haya 
   1201   1.10   tsutsui 		if (status & RTK_ISR_RX_ERR)
   1202    1.8   thorpej 			rtk_rxeof(sc);
   1203    1.1      haya 
   1204  1.105   msaitoh 		if (status & (RTK_ISR_TX_OK | RTK_ISR_TX_ERR))
   1205    1.8   thorpej 			rtk_txeof(sc);
   1206    1.1      haya 
   1207   1.10   tsutsui 		if (status & RTK_ISR_SYSTEM_ERR) {
   1208    1.8   thorpej 			rtk_reset(sc);
   1209   1.15   thorpej 			rtk_init(ifp);
   1210    1.1      haya 		}
   1211    1.1      haya 	}
   1212    1.1      haya 
   1213    1.1      haya 	/* Re-enable interrupts. */
   1214   1.10   tsutsui 	CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
   1215    1.1      haya 
   1216  1.102     ozaki 	if_schedule_deferred_start(ifp);
   1217    1.1      haya 
   1218  1.112   tsutsui 	rnd_add_uint32(&sc->rnd_source, rndstatus);
   1219   1.48       dan 
   1220   1.63   tsutsui 	return handled;
   1221    1.1      haya }
   1222    1.1      haya 
   1223    1.1      haya /*
   1224    1.1      haya  * Main transmit routine.
   1225    1.1      haya  */
   1226    1.1      haya 
   1227   1.85   tsutsui static void
   1228   1.62   tsutsui rtk_start(struct ifnet *ifp)
   1229    1.1      haya {
   1230   1.31   thorpej 	struct rtk_softc *sc;
   1231   1.31   thorpej 	struct rtk_tx_desc *txd;
   1232   1.63   tsutsui 	struct mbuf *m_head, *m_new;
   1233   1.31   thorpej 	int error, len;
   1234    1.1      haya 
   1235    1.1      haya 	sc = ifp->if_softc;
   1236    1.1      haya 
   1237   1.31   thorpej 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_free)) != NULL) {
   1238   1.25   thorpej 		IFQ_POLL(&ifp->if_snd, m_head);
   1239    1.1      haya 		if (m_head == NULL)
   1240    1.1      haya 			break;
   1241   1.26   thorpej 		m_new = NULL;
   1242    1.1      haya 
   1243    1.4   tsutsui 		/*
   1244    1.4   tsutsui 		 * Load the DMA map.  If this fails, the packet didn't
   1245    1.4   tsutsui 		 * fit in one DMA segment, and we need to copy.  Note,
   1246    1.4   tsutsui 		 * the packet must also be aligned.
   1247   1.44    bouyer 		 * if the packet is too small, copy it too, so we're sure
   1248   1.89       snj 		 * so have enough room for the pad buffer.
   1249    1.4   tsutsui 		 */
   1250   1.38       mrg 		if ((mtod(m_head, uintptr_t) & 3) != 0 ||
   1251   1.44    bouyer 		    m_head->m_pkthdr.len < ETHER_PAD_LEN ||
   1252   1.31   thorpej 		    bus_dmamap_load_mbuf(sc->sc_dmat, txd->txd_dmamap,
   1253  1.105   msaitoh 			m_head, BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
   1254    1.4   tsutsui 			MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1255    1.4   tsutsui 			if (m_new == NULL) {
   1256    1.4   tsutsui 				printf("%s: unable to allocate Tx mbuf\n",
   1257   1.82   tsutsui 				    device_xname(sc->sc_dev));
   1258    1.4   tsutsui 				break;
   1259    1.4   tsutsui 			}
   1260  1.111   thorpej 			MCLAIM(m_new, &sc->ethercom.ec_rx_mowner);
   1261    1.4   tsutsui 			if (m_head->m_pkthdr.len > MHLEN) {
   1262    1.4   tsutsui 				MCLGET(m_new, M_DONTWAIT);
   1263    1.4   tsutsui 				if ((m_new->m_flags & M_EXT) == 0) {
   1264    1.4   tsutsui 					printf("%s: unable to allocate Tx "
   1265   1.82   tsutsui 					    "cluster\n",
   1266   1.82   tsutsui 					    device_xname(sc->sc_dev));
   1267    1.4   tsutsui 					m_freem(m_new);
   1268    1.4   tsutsui 					break;
   1269    1.4   tsutsui 				}
   1270    1.4   tsutsui 			}
   1271    1.4   tsutsui 			m_copydata(m_head, 0, m_head->m_pkthdr.len,
   1272   1.71  christos 			    mtod(m_new, void *));
   1273    1.4   tsutsui 			m_new->m_pkthdr.len = m_new->m_len =
   1274    1.4   tsutsui 			    m_head->m_pkthdr.len;
   1275   1.44    bouyer 			if (m_head->m_pkthdr.len < ETHER_PAD_LEN) {
   1276   1.44    bouyer 				memset(
   1277   1.71  christos 				    mtod(m_new, char *) + m_head->m_pkthdr.len,
   1278   1.44    bouyer 				    0, ETHER_PAD_LEN - m_head->m_pkthdr.len);
   1279   1.44    bouyer 				m_new->m_pkthdr.len = m_new->m_len =
   1280   1.44    bouyer 				    ETHER_PAD_LEN;
   1281   1.44    bouyer 			}
   1282    1.4   tsutsui 			error = bus_dmamap_load_mbuf(sc->sc_dmat,
   1283   1.35   thorpej 			    txd->txd_dmamap, m_new,
   1284  1.105   msaitoh 			    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1285    1.4   tsutsui 			if (error) {
   1286    1.4   tsutsui 				printf("%s: unable to load Tx buffer, "
   1287   1.82   tsutsui 				    "error = %d\n",
   1288   1.82   tsutsui 				    device_xname(sc->sc_dev), error);
   1289    1.4   tsutsui 				break;
   1290    1.4   tsutsui 			}
   1291    1.4   tsutsui 		}
   1292   1.25   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1293   1.44    bouyer 		/*
   1294   1.44    bouyer 		 * If there's a BPF listener, bounce a copy of this frame
   1295   1.44    bouyer 		 * to him.
   1296   1.44    bouyer 		 */
   1297  1.103   msaitoh 		bpf_mtap(ifp, m_head, BPF_D_OUT);
   1298   1.26   thorpej 		if (m_new != NULL) {
   1299   1.26   thorpej 			m_freem(m_head);
   1300   1.26   thorpej 			m_head = m_new;
   1301   1.26   thorpej 		}
   1302   1.31   thorpej 		txd->txd_mbuf = m_head;
   1303    1.4   tsutsui 
   1304   1.41     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_free, txd_q);
   1305   1.31   thorpej 		SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_dirty, txd, txd_q);
   1306    1.1      haya 
   1307    1.1      haya 		/*
   1308    1.1      haya 		 * Transmit the frame.
   1309   1.61   tsutsui 		 */
   1310    1.4   tsutsui 		bus_dmamap_sync(sc->sc_dmat,
   1311   1.31   thorpej 		    txd->txd_dmamap, 0, txd->txd_dmamap->dm_mapsize,
   1312    1.4   tsutsui 		    BUS_DMASYNC_PREWRITE);
   1313    1.4   tsutsui 
   1314   1.31   thorpej 		len = txd->txd_dmamap->dm_segs[0].ds_len;
   1315    1.4   tsutsui 
   1316   1.31   thorpej 		CSR_WRITE_4(sc, txd->txd_txaddr,
   1317   1.31   thorpej 		    txd->txd_dmamap->dm_segs[0].ds_addr);
   1318   1.65   tsutsui 		CSR_WRITE_4(sc, txd->txd_txstat,
   1319   1.65   tsutsui 		    RTK_TXSTAT_THRESH(sc->sc_txthresh) | len);
   1320   1.60   tsutsui 
   1321   1.60   tsutsui 		/*
   1322   1.60   tsutsui 		 * Set a timeout in case the chip goes out to lunch.
   1323   1.60   tsutsui 		 */
   1324   1.60   tsutsui 		ifp->if_timer = 5;
   1325    1.1      haya 	}
   1326    1.1      haya 
   1327    1.1      haya 	/*
   1328    1.1      haya 	 * We broke out of the loop because all our TX slots are
   1329    1.1      haya 	 * full. Mark the NIC as busy until it drains some of the
   1330    1.1      haya 	 * packets from the queue.
   1331    1.1      haya 	 */
   1332   1.41     lukem 	if (SIMPLEQ_EMPTY(&sc->rtk_tx_free))
   1333    1.1      haya 		ifp->if_flags |= IFF_OACTIVE;
   1334    1.1      haya }
   1335    1.1      haya 
   1336   1.85   tsutsui static int
   1337   1.62   tsutsui rtk_init(struct ifnet *ifp)
   1338    1.1      haya {
   1339   1.63   tsutsui 	struct rtk_softc *sc = ifp->if_softc;
   1340   1.63   tsutsui 	int error, i;
   1341   1.63   tsutsui 	uint32_t rxcfg;
   1342    1.1      haya 
   1343   1.15   thorpej 	if ((error = rtk_enable(sc)) != 0)
   1344   1.15   thorpej 		goto out;
   1345    1.1      haya 
   1346    1.1      haya 	/*
   1347   1.15   thorpej 	 * Cancel pending I/O.
   1348    1.1      haya 	 */
   1349   1.15   thorpej 	rtk_stop(ifp, 0);
   1350    1.1      haya 
   1351    1.1      haya 	/* Init our MAC address */
   1352    1.1      haya 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1353   1.76    dyoung 		CSR_WRITE_1(sc, RTK_IDR0 + i, CLLADDR(ifp->if_sadl)[i]);
   1354    1.1      haya 	}
   1355    1.1      haya 
   1356    1.1      haya 	/* Init the RX buffer pointer register. */
   1357    1.4   tsutsui 	bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, 0,
   1358    1.4   tsutsui 	    sc->recv_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1359   1.10   tsutsui 	CSR_WRITE_4(sc, RTK_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
   1360    1.1      haya 
   1361    1.1      haya 	/* Init TX descriptors. */
   1362    1.8   thorpej 	rtk_list_tx_init(sc);
   1363    1.1      haya 
   1364   1.36   kanaoka 	/* Init Early TX threshold. */
   1365   1.65   tsutsui 	sc->sc_txthresh = RTK_TXTH_256;
   1366    1.1      haya 	/*
   1367    1.1      haya 	 * Enable transmit and receive.
   1368    1.1      haya 	 */
   1369  1.105   msaitoh 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
   1370    1.1      haya 
   1371    1.1      haya 	/*
   1372    1.1      haya 	 * Set the initial TX and RX configuration.
   1373    1.1      haya 	 */
   1374   1.10   tsutsui 	CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
   1375   1.10   tsutsui 	CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
   1376    1.1      haya 
   1377    1.1      haya 	/* Set the individual bit to receive frames for this host only. */
   1378   1.10   tsutsui 	rxcfg = CSR_READ_4(sc, RTK_RXCFG);
   1379   1.10   tsutsui 	rxcfg |= RTK_RXCFG_RX_INDIV;
   1380    1.1      haya 
   1381    1.1      haya 	/* If we want promiscuous mode, set the allframes bit. */
   1382    1.1      haya 	if (ifp->if_flags & IFF_PROMISC) {
   1383   1.10   tsutsui 		rxcfg |= RTK_RXCFG_RX_ALLPHYS;
   1384   1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1385    1.1      haya 	} else {
   1386   1.10   tsutsui 		rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
   1387   1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1388    1.1      haya 	}
   1389    1.1      haya 
   1390    1.1      haya 	/*
   1391    1.1      haya 	 * Set capture broadcast bit to capture broadcast frames.
   1392    1.1      haya 	 */
   1393    1.1      haya 	if (ifp->if_flags & IFF_BROADCAST) {
   1394   1.10   tsutsui 		rxcfg |= RTK_RXCFG_RX_BROAD;
   1395   1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1396    1.1      haya 	} else {
   1397   1.10   tsutsui 		rxcfg &= ~RTK_RXCFG_RX_BROAD;
   1398   1.10   tsutsui 		CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
   1399    1.1      haya 	}
   1400    1.1      haya 
   1401    1.1      haya 	/*
   1402    1.1      haya 	 * Program the multicast filter, if necessary.
   1403    1.1      haya 	 */
   1404    1.8   thorpej 	rtk_setmulti(sc);
   1405    1.1      haya 
   1406    1.1      haya 	/*
   1407    1.1      haya 	 * Enable interrupts.
   1408    1.1      haya 	 */
   1409   1.10   tsutsui 	CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
   1410    1.1      haya 
   1411    1.1      haya 	/* Start RX/TX process. */
   1412   1.10   tsutsui 	CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
   1413    1.1      haya 
   1414    1.1      haya 	/* Enable receiver and transmitter. */
   1415  1.105   msaitoh 	CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB | RTK_CMD_RX_ENB);
   1416    1.1      haya 
   1417  1.105   msaitoh 	CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD | RTK_CFG1_FULLDUPLEX);
   1418    1.1      haya 
   1419    1.1      haya 	/*
   1420    1.1      haya 	 * Set current media.
   1421    1.1      haya 	 */
   1422   1.81    dyoung 	if ((error = ether_mediachange(ifp)) != 0)
   1423   1.81    dyoung 		goto out;
   1424    1.1      haya 
   1425    1.1      haya 	ifp->if_flags |= IFF_RUNNING;
   1426    1.1      haya 	ifp->if_flags &= ~IFF_OACTIVE;
   1427    1.1      haya 
   1428  1.110   thorpej 	callout_schedule(&sc->rtk_tick_ch, hz);
   1429    1.1      haya 
   1430   1.15   thorpej  out:
   1431   1.15   thorpej 	if (error) {
   1432   1.15   thorpej 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1433   1.15   thorpej 		ifp->if_timer = 0;
   1434   1.82   tsutsui 		printf("%s: interface not running\n", device_xname(sc->sc_dev));
   1435   1.15   thorpej 	}
   1436   1.63   tsutsui 	return error;
   1437    1.1      haya }
   1438    1.1      haya 
   1439   1.85   tsutsui static int
   1440   1.71  christos rtk_ioctl(struct ifnet *ifp, u_long command, void *data)
   1441    1.1      haya {
   1442   1.63   tsutsui 	struct rtk_softc *sc = ifp->if_softc;
   1443   1.63   tsutsui 	int s, error;
   1444    1.1      haya 
   1445    1.9   thorpej 	s = splnet();
   1446   1.81    dyoung 	error = ether_ioctl(ifp, command, data);
   1447   1.81    dyoung 	if (error == ENETRESET) {
   1448   1.81    dyoung 		if (ifp->if_flags & IFF_RUNNING) {
   1449   1.81    dyoung 			/*
   1450   1.81    dyoung 			 * Multicast list has changed.  Set the
   1451   1.81    dyoung 			 * hardware filter accordingly.
   1452   1.81    dyoung 			 */
   1453   1.81    dyoung 			rtk_setmulti(sc);
   1454   1.15   thorpej 		}
   1455   1.81    dyoung 		error = 0;
   1456    1.1      haya 	}
   1457   1.12  drochner 	splx(s);
   1458    1.1      haya 
   1459   1.63   tsutsui 	return error;
   1460    1.1      haya }
   1461    1.1      haya 
   1462   1.85   tsutsui static void
   1463   1.62   tsutsui rtk_watchdog(struct ifnet *ifp)
   1464    1.1      haya {
   1465   1.63   tsutsui 	struct rtk_softc *sc;
   1466    1.1      haya 
   1467    1.1      haya 	sc = ifp->if_softc;
   1468    1.1      haya 
   1469   1.82   tsutsui 	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
   1470  1.108   thorpej 	if_statinc(ifp, if_oerrors);
   1471    1.8   thorpej 	rtk_txeof(sc);
   1472    1.8   thorpej 	rtk_rxeof(sc);
   1473   1.15   thorpej 	rtk_init(ifp);
   1474    1.1      haya }
   1475    1.1      haya 
   1476    1.1      haya /*
   1477    1.1      haya  * Stop the adapter and free any mbufs allocated to the
   1478    1.1      haya  * RX and TX lists.
   1479    1.1      haya  */
   1480   1.85   tsutsui static void
   1481   1.62   tsutsui rtk_stop(struct ifnet *ifp, int disable)
   1482    1.1      haya {
   1483   1.15   thorpej 	struct rtk_softc *sc = ifp->if_softc;
   1484   1.31   thorpej 	struct rtk_tx_desc *txd;
   1485    1.1      haya 
   1486    1.8   thorpej 	callout_stop(&sc->rtk_tick_ch);
   1487    1.1      haya 
   1488    1.1      haya 	mii_down(&sc->mii);
   1489    1.1      haya 
   1490   1.10   tsutsui 	CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
   1491   1.10   tsutsui 	CSR_WRITE_2(sc, RTK_IMR, 0x0000);
   1492    1.1      haya 
   1493    1.1      haya 	/*
   1494    1.1      haya 	 * Free the TX list buffers.
   1495    1.1      haya 	 */
   1496   1.31   thorpej 	while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL) {
   1497   1.41     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
   1498   1.31   thorpej 		bus_dmamap_unload(sc->sc_dmat, txd->txd_dmamap);
   1499   1.31   thorpej 		m_freem(txd->txd_mbuf);
   1500   1.31   thorpej 		txd->txd_mbuf = NULL;
   1501   1.31   thorpej 		CSR_WRITE_4(sc, txd->txd_txaddr, 0);
   1502    1.1      haya 	}
   1503    1.1      haya 
   1504   1.15   thorpej 	if (disable)
   1505   1.15   thorpej 		rtk_disable(sc);
   1506   1.15   thorpej 
   1507    1.1      haya 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1508   1.15   thorpej 	ifp->if_timer = 0;
   1509    1.1      haya }
   1510    1.1      haya 
   1511   1.85   tsutsui static void
   1512   1.62   tsutsui rtk_tick(void *arg)
   1513    1.1      haya {
   1514    1.8   thorpej 	struct rtk_softc *sc = arg;
   1515   1.63   tsutsui 	int s;
   1516    1.1      haya 
   1517   1.63   tsutsui 	s = splnet();
   1518    1.1      haya 	mii_tick(&sc->mii);
   1519    1.1      haya 	splx(s);
   1520    1.1      haya 
   1521  1.110   thorpej 	callout_schedule(&sc->rtk_tick_ch, hz);
   1522    1.1      haya }
   1523