Home | History | Annotate | Line # | Download | only in ic
dm9000.c revision 1.17
      1  1.17   msaitoh /*	$NetBSD: dm9000.c,v 1.17 2019/04/24 08:21:25 msaitoh Exp $	*/
      2   1.1     ahoka 
      3   1.1     ahoka /*
      4   1.1     ahoka  * Copyright (c) 2009 Paul Fleischer
      5   1.1     ahoka  * All rights reserved.
      6   1.1     ahoka  *
      7   1.1     ahoka  * 1. Redistributions of source code must retain the above copyright
      8   1.1     ahoka  *    notice, this list of conditions and the following disclaimer.
      9   1.1     ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     10   1.1     ahoka  *    notice, this list of conditions and the following disclaimer in the
     11   1.1     ahoka  *    documentation and/or other materials provided with the distribution.
     12   1.1     ahoka  * 3. The name of the company nor the name of the author may be used to
     13   1.1     ahoka  *    endorse or promote products derived from this software without specific
     14   1.1     ahoka  *    prior written permission.
     15   1.1     ahoka  *
     16   1.1     ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17   1.1     ahoka  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18   1.1     ahoka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1     ahoka  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     20   1.1     ahoka  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21   1.1     ahoka  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22   1.1     ahoka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1     ahoka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1     ahoka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1     ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1     ahoka  * SUCH DAMAGE.
     27   1.1     ahoka  */
     28   1.1     ahoka 
     29   1.1     ahoka /* based on sys/dev/ic/cs89x0.c */
     30   1.1     ahoka /*
     31   1.1     ahoka  * Copyright (c) 2004 Christopher Gilbert
     32   1.1     ahoka  * All rights reserved.
     33   1.1     ahoka  *
     34   1.1     ahoka  * 1. Redistributions of source code must retain the above copyright
     35   1.1     ahoka  *    notice, this list of conditions and the following disclaimer.
     36   1.1     ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     37   1.1     ahoka  *    notice, this list of conditions and the following disclaimer in the
     38   1.1     ahoka  *    documentation and/or other materials provided with the distribution.
     39   1.1     ahoka  * 3. The name of the company nor the name of the author may be used to
     40   1.1     ahoka  *    endorse or promote products derived from this software without specific
     41   1.1     ahoka  *    prior written permission.
     42   1.1     ahoka  *
     43   1.1     ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     44   1.1     ahoka  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     45   1.1     ahoka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     46   1.1     ahoka  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     47   1.1     ahoka  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     48   1.1     ahoka  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     49   1.1     ahoka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     50   1.1     ahoka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     51   1.1     ahoka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     52   1.1     ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     53   1.1     ahoka  * SUCH DAMAGE.
     54   1.1     ahoka  */
     55   1.1     ahoka 
     56   1.1     ahoka /*
     57   1.1     ahoka  * Copyright 1997
     58   1.1     ahoka  * Digital Equipment Corporation. All rights reserved.
     59   1.1     ahoka  *
     60   1.1     ahoka  * This software is furnished under license and may be used and
     61   1.1     ahoka  * copied only in accordance with the following terms and conditions.
     62   1.1     ahoka  * Subject to these conditions, you may download, copy, install,
     63   1.1     ahoka  * use, modify and distribute this software in source and/or binary
     64   1.1     ahoka  * form. No title or ownership is transferred hereby.
     65   1.1     ahoka  *
     66   1.1     ahoka  * 1) Any source code used, modified or distributed must reproduce
     67   1.1     ahoka  *    and retain this copyright notice and list of conditions as
     68   1.1     ahoka  *    they appear in the source file.
     69   1.1     ahoka  *
     70   1.1     ahoka  * 2) No right is granted to use any trade name, trademark, or logo of
     71   1.1     ahoka  *    Digital Equipment Corporation. Neither the "Digital Equipment
     72   1.1     ahoka  *    Corporation" name nor any trademark or logo of Digital Equipment
     73   1.1     ahoka  *    Corporation may be used to endorse or promote products derived
     74   1.1     ahoka  *    from this software without the prior written permission of
     75   1.1     ahoka  *    Digital Equipment Corporation.
     76   1.1     ahoka  *
     77   1.1     ahoka  * 3) This software is provided "AS-IS" and any express or implied
     78   1.1     ahoka  *    warranties, including but not limited to, any implied warranties
     79   1.1     ahoka  *    of merchantability, fitness for a particular purpose, or
     80   1.1     ahoka  *    non-infringement are disclaimed. In no event shall DIGITAL be
     81   1.1     ahoka  *    liable for any damages whatsoever, and in particular, DIGITAL
     82   1.1     ahoka  *    shall not be liable for special, indirect, consequential, or
     83   1.1     ahoka  *    incidental damages or damages for lost profits, loss of
     84   1.1     ahoka  *    revenue or loss of use, whether such damages arise in contract,
     85   1.1     ahoka  *    negligence, tort, under statute, in equity, at law or otherwise,
     86   1.1     ahoka  *    even if advised of the possibility of such damage.
     87   1.1     ahoka  */
     88   1.1     ahoka 
     89   1.1     ahoka #include <sys/cdefs.h>
     90   1.1     ahoka 
     91   1.1     ahoka #include <sys/param.h>
     92   1.4  nisimura #include <sys/kernel.h>
     93   1.1     ahoka #include <sys/systm.h>
     94   1.1     ahoka #include <sys/mbuf.h>
     95   1.1     ahoka #include <sys/syslog.h>
     96   1.1     ahoka #include <sys/socket.h>
     97   1.1     ahoka #include <sys/device.h>
     98   1.1     ahoka #include <sys/malloc.h>
     99   1.1     ahoka #include <sys/ioctl.h>
    100   1.1     ahoka #include <sys/errno.h>
    101   1.1     ahoka 
    102   1.1     ahoka #include <net/if.h>
    103   1.1     ahoka #include <net/if_ether.h>
    104   1.1     ahoka #include <net/if_media.h>
    105  1.13   msaitoh #include <net/bpf.h>
    106  1.13   msaitoh 
    107   1.1     ahoka #ifdef INET
    108   1.1     ahoka #include <netinet/in.h>
    109   1.1     ahoka #include <netinet/if_inarp.h>
    110   1.1     ahoka #endif
    111   1.1     ahoka 
    112   1.1     ahoka #include <sys/bus.h>
    113   1.1     ahoka #include <sys/intr.h>
    114   1.1     ahoka 
    115   1.1     ahoka #include <dev/ic/dm9000var.h>
    116   1.1     ahoka #include <dev/ic/dm9000reg.h>
    117   1.1     ahoka 
    118   1.1     ahoka #if 1
    119   1.1     ahoka #undef DM9000_DEBUG
    120   1.4  nisimura #undef DM9000_TX_DEBUG
    121   1.1     ahoka #undef DM9000_TX_DATA_DEBUG
    122   1.1     ahoka #undef DM9000_RX_DEBUG
    123   1.1     ahoka #undef  DM9000_RX_DATA_DEBUG
    124   1.1     ahoka #else
    125   1.1     ahoka #define DM9000_DEBUG
    126   1.1     ahoka #define  DM9000_TX_DEBUG
    127   1.1     ahoka #define DM9000_TX_DATA_DEBUG
    128   1.1     ahoka #define DM9000_RX_DEBUG
    129   1.1     ahoka #define  DM9000_RX_DATA_DEBUG
    130   1.1     ahoka #endif
    131   1.1     ahoka 
    132   1.1     ahoka #ifdef DM9000_DEBUG
    133   1.1     ahoka #define DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
    134   1.1     ahoka #else
    135   1.1     ahoka #define DPRINTF(s) do {} while (/*CONSTCOND*/0)
    136   1.1     ahoka #endif
    137   1.1     ahoka 
    138   1.1     ahoka #ifdef DM9000_TX_DEBUG
    139   1.1     ahoka #define TX_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
    140   1.1     ahoka #else
    141   1.1     ahoka #define TX_DPRINTF(s) do {} while (/*CONSTCOND*/0)
    142   1.1     ahoka #endif
    143   1.1     ahoka 
    144   1.1     ahoka #ifdef DM9000_RX_DEBUG
    145   1.1     ahoka #define RX_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
    146   1.1     ahoka #else
    147   1.1     ahoka #define RX_DPRINTF(s) do {} while (/*CONSTCOND*/0)
    148   1.1     ahoka #endif
    149   1.1     ahoka 
    150   1.1     ahoka #ifdef DM9000_RX_DATA_DEBUG
    151   1.1     ahoka #define RX_DATA_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
    152   1.1     ahoka #else
    153   1.1     ahoka #define RX_DATA_DPRINTF(s) do {} while (/*CONSTCOND*/0)
    154   1.1     ahoka #endif
    155   1.1     ahoka 
    156   1.1     ahoka #ifdef DM9000_TX_DATA_DEBUG
    157   1.1     ahoka #define TX_DATA_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
    158   1.1     ahoka #else
    159   1.1     ahoka #define TX_DATA_DPRINTF(s) do {} while (/*CONSTCOND*/0)
    160   1.1     ahoka #endif
    161   1.1     ahoka 
    162   1.4  nisimura /*** Internal PHY functions ***/
    163   1.7  macallan uint16_t dme_phy_read(struct dme_softc *, int );
    164   1.7  macallan void	dme_phy_write(struct dme_softc *, int, uint16_t);
    165   1.7  macallan void	dme_phy_init(struct dme_softc *);
    166   1.7  macallan void	dme_phy_reset(struct dme_softc *);
    167   1.7  macallan void	dme_phy_update_media(struct dme_softc *);
    168   1.7  macallan void	dme_phy_check_link(void *);
    169   1.1     ahoka 
    170   1.1     ahoka /*** Methods registered in struct ifnet ***/
    171   1.7  macallan void	dme_start_output(struct ifnet *);
    172   1.7  macallan int	dme_init(struct ifnet *);
    173   1.7  macallan int	dme_ioctl(struct ifnet *, u_long, void *);
    174   1.7  macallan void	dme_stop(struct ifnet *, int);
    175   1.1     ahoka 
    176   1.7  macallan int	dme_mediachange(struct ifnet *);
    177   1.7  macallan void	dme_mediastatus(struct ifnet *, struct ifmediareq *);
    178   1.1     ahoka 
    179   1.1     ahoka /*** Internal methods ***/
    180   1.1     ahoka 
    181   1.1     ahoka /* Prepare data to be transmitted (i.e. dequeue and load it into the DM9000) */
    182   1.7  macallan void    dme_prepare(struct dme_softc *, struct ifnet *);
    183   1.1     ahoka 
    184   1.1     ahoka /* Transmit prepared data */
    185   1.7  macallan void    dme_transmit(struct dme_softc *);
    186   1.1     ahoka 
    187   1.1     ahoka /* Receive data */
    188   1.7  macallan void    dme_receive(struct dme_softc *, struct ifnet *);
    189   1.1     ahoka 
    190   1.1     ahoka /* Software Initialize/Reset of the DM9000 */
    191   1.7  macallan void    dme_reset(struct dme_softc *);
    192   1.1     ahoka 
    193   1.4  nisimura /* Configure multicast filter */
    194   1.7  macallan void	dme_set_addr_filter(struct dme_softc *);
    195   1.4  nisimura 
    196   1.4  nisimura /* Set media */
    197   1.7  macallan int	dme_set_media(struct dme_softc *, int );
    198   1.4  nisimura 
    199   1.4  nisimura /* Read/write packet data from/to DM9000 IC in various transfer sizes */
    200   1.7  macallan int	dme_pkt_read_2(struct dme_softc *, struct ifnet *, struct mbuf **);
    201   1.7  macallan int	dme_pkt_write_2(struct dme_softc *, struct mbuf *);
    202   1.7  macallan int	dme_pkt_read_1(struct dme_softc *, struct ifnet *, struct mbuf **);
    203   1.7  macallan int	dme_pkt_write_1(struct dme_softc *, struct mbuf *);
    204   1.6  macallan /* TODO: Implement 32 bit read/write functions */
    205   1.4  nisimura 
    206   1.1     ahoka uint16_t
    207   1.1     ahoka dme_phy_read(struct dme_softc *sc, int reg)
    208   1.1     ahoka {
    209   1.1     ahoka 	uint16_t val;
    210   1.1     ahoka 	/* Select Register to read*/
    211   1.1     ahoka 	dme_write(sc, DM9000_EPAR, DM9000_EPAR_INT_PHY +
    212   1.1     ahoka 	    (reg & DM9000_EPAR_EROA_MASK));
    213   1.1     ahoka 	/* Select read operation (DM9000_EPCR_ERPRR) from the PHY */
    214   1.1     ahoka 	dme_write(sc, DM9000_EPCR, DM9000_EPCR_ERPRR + DM9000_EPCR_EPOS_PHY);
    215   1.1     ahoka 
    216   1.1     ahoka 	/* Wait until access to PHY has completed */
    217   1.1     ahoka 	while (dme_read(sc, DM9000_EPCR) & DM9000_EPCR_ERRE);
    218   1.1     ahoka 
    219   1.1     ahoka 	/* Reset ERPRR-bit */
    220   1.1     ahoka 	dme_write(sc, DM9000_EPCR, DM9000_EPCR_EPOS_PHY);
    221   1.1     ahoka 
    222   1.1     ahoka 	val = dme_read(sc, DM9000_EPDRL);
    223   1.1     ahoka 	val += dme_read(sc, DM9000_EPDRH) << 8;
    224   1.1     ahoka 
    225   1.1     ahoka 	return val;
    226   1.1     ahoka }
    227   1.1     ahoka 
    228   1.1     ahoka void
    229   1.1     ahoka dme_phy_write(struct dme_softc *sc, int reg, uint16_t value)
    230   1.1     ahoka {
    231   1.1     ahoka 	/* Select Register to write*/
    232   1.1     ahoka 	dme_write(sc, DM9000_EPAR, DM9000_EPAR_INT_PHY +
    233   1.1     ahoka 	    (reg & DM9000_EPAR_EROA_MASK));
    234   1.1     ahoka 
    235   1.1     ahoka 	/* Write data to the two data registers */
    236   1.1     ahoka 	dme_write(sc, DM9000_EPDRL, value & 0xFF);
    237   1.1     ahoka 	dme_write(sc, DM9000_EPDRH, (value >> 8) & 0xFF);
    238   1.1     ahoka 
    239   1.1     ahoka 	/* Select write operation (DM9000_EPCR_ERPRW) from the PHY */
    240   1.1     ahoka 	dme_write(sc, DM9000_EPCR, DM9000_EPCR_ERPRW + DM9000_EPCR_EPOS_PHY);
    241   1.1     ahoka 
    242   1.1     ahoka 	/* Wait until access to PHY has completed */
    243  1.17   msaitoh 	while (dme_read(sc, DM9000_EPCR) & DM9000_EPCR_ERRE)
    244  1.17   msaitoh 		;
    245   1.1     ahoka 
    246   1.4  nisimura 	/* Reset ERPRR-bit */
    247   1.4  nisimura 	dme_write(sc, DM9000_EPCR, DM9000_EPCR_EPOS_PHY);
    248   1.4  nisimura }
    249   1.4  nisimura 
    250   1.4  nisimura void
    251   1.4  nisimura dme_phy_init(struct dme_softc *sc)
    252   1.4  nisimura {
    253   1.4  nisimura 	u_int ifm_media = sc->sc_media.ifm_media;
    254   1.4  nisimura 	uint32_t bmcr, anar;
    255   1.4  nisimura 
    256   1.4  nisimura 	bmcr = dme_phy_read(sc, DM9000_PHY_BMCR);
    257   1.4  nisimura 	anar = dme_phy_read(sc, DM9000_PHY_ANAR);
    258   1.4  nisimura 
    259   1.4  nisimura 	anar = anar & ~DM9000_PHY_ANAR_10_HDX
    260   1.4  nisimura 		& ~DM9000_PHY_ANAR_10_FDX
    261   1.4  nisimura 		& ~DM9000_PHY_ANAR_TX_HDX
    262   1.4  nisimura 		& ~DM9000_PHY_ANAR_TX_FDX;
    263   1.4  nisimura 
    264   1.4  nisimura 	switch (IFM_SUBTYPE(ifm_media)) {
    265   1.4  nisimura 	case IFM_AUTO:
    266   1.4  nisimura 		bmcr |= DM9000_PHY_BMCR_AUTO_NEG_EN;
    267   1.4  nisimura 		anar |= DM9000_PHY_ANAR_10_HDX |
    268   1.4  nisimura 			DM9000_PHY_ANAR_10_FDX |
    269   1.4  nisimura 			DM9000_PHY_ANAR_TX_HDX |
    270   1.4  nisimura 			DM9000_PHY_ANAR_TX_FDX;
    271   1.4  nisimura 		break;
    272   1.4  nisimura 	case IFM_10_T:
    273   1.4  nisimura 		//bmcr &= ~DM9000_PHY_BMCR_AUTO_NEG_EN;
    274   1.4  nisimura 		bmcr &= ~DM9000_PHY_BMCR_SPEED_SELECT;
    275   1.4  nisimura 		if (ifm_media & IFM_FDX)
    276   1.4  nisimura 			anar |= DM9000_PHY_ANAR_10_FDX;
    277   1.4  nisimura 		else
    278   1.4  nisimura 			anar |= DM9000_PHY_ANAR_10_HDX;
    279   1.4  nisimura 		break;
    280   1.4  nisimura 	case IFM_100_TX:
    281   1.4  nisimura 		//bmcr &= ~DM9000_PHY_BMCR_AUTO_NEG_EN;
    282   1.4  nisimura 		bmcr |= DM9000_PHY_BMCR_SPEED_SELECT;
    283   1.4  nisimura 		if (ifm_media & IFM_FDX)
    284   1.4  nisimura 			anar |= DM9000_PHY_ANAR_TX_FDX;
    285   1.4  nisimura 		else
    286   1.4  nisimura 			anar |= DM9000_PHY_ANAR_TX_HDX;
    287   1.4  nisimura 
    288   1.4  nisimura 		break;
    289   1.4  nisimura 	}
    290   1.4  nisimura 
    291  1.17   msaitoh 	if (ifm_media & IFM_FDX)
    292   1.4  nisimura 		bmcr |= DM9000_PHY_BMCR_DUPLEX_MODE;
    293  1.17   msaitoh 	else
    294   1.4  nisimura 		bmcr &= ~DM9000_PHY_BMCR_DUPLEX_MODE;
    295   1.4  nisimura 
    296   1.4  nisimura 	dme_phy_write(sc, DM9000_PHY_BMCR, bmcr);
    297   1.4  nisimura 	dme_phy_write(sc, DM9000_PHY_ANAR, anar);
    298   1.4  nisimura }
    299   1.4  nisimura 
    300   1.4  nisimura void
    301   1.4  nisimura dme_phy_reset(struct dme_softc *sc)
    302   1.4  nisimura {
    303   1.4  nisimura 	uint32_t reg;
    304   1.4  nisimura 
    305   1.4  nisimura 	/* PHY Reset */
    306   1.4  nisimura 	dme_phy_write(sc, DM9000_PHY_BMCR, DM9000_PHY_BMCR_RESET);
    307   1.4  nisimura 
    308   1.4  nisimura 	reg = dme_read(sc, DM9000_GPCR);
    309   1.4  nisimura 	dme_write(sc, DM9000_GPCR, reg & ~DM9000_GPCR_GPIO0_OUT);
    310   1.4  nisimura 	reg = dme_read(sc, DM9000_GPR);
    311   1.4  nisimura 	dme_write(sc, DM9000_GPR, reg | DM9000_GPR_PHY_PWROFF);
    312   1.4  nisimura 
    313   1.4  nisimura 	dme_phy_init(sc);
    314   1.1     ahoka 
    315   1.4  nisimura 	reg = dme_read(sc, DM9000_GPR);
    316   1.4  nisimura 	dme_write(sc, DM9000_GPR, reg & ~DM9000_GPR_PHY_PWROFF);
    317   1.4  nisimura 	reg = dme_read(sc, DM9000_GPCR);
    318   1.4  nisimura 	dme_write(sc, DM9000_GPCR, reg | DM9000_GPCR_GPIO0_OUT);
    319   1.1     ahoka 
    320   1.4  nisimura 	dme_phy_update_media(sc);
    321   1.4  nisimura }
    322   1.4  nisimura 
    323   1.4  nisimura void
    324   1.4  nisimura dme_phy_update_media(struct dme_softc *sc)
    325   1.4  nisimura {
    326   1.4  nisimura 	u_int ifm_media = sc->sc_media.ifm_media;
    327   1.4  nisimura 	uint32_t reg;
    328   1.4  nisimura 
    329   1.4  nisimura 	if (IFM_SUBTYPE(ifm_media) == IFM_AUTO) {
    330   1.4  nisimura 		/* If auto-negotiation is used, ensures that it is completed
    331   1.4  nisimura 		 before trying to extract any media information. */
    332   1.4  nisimura 		reg = dme_phy_read(sc, DM9000_PHY_BMSR);
    333   1.4  nisimura 		if ((reg & DM9000_PHY_BMSR_AUTO_NEG_AB) == 0) {
    334   1.4  nisimura 			/* Auto-negotation not possible, therefore there is no
    335   1.4  nisimura 			   reason to try obtain any media information. */
    336   1.4  nisimura 			return;
    337   1.4  nisimura 		}
    338   1.4  nisimura 
    339   1.4  nisimura 		/* Then loop until the negotiation is completed. */
    340   1.4  nisimura 		while ((reg & DM9000_PHY_BMSR_AUTO_NEG_COM) == 0) {
    341   1.4  nisimura 			/* TODO: Bail out after a finite number of attempts
    342   1.4  nisimura 			 in case something goes wrong. */
    343   1.4  nisimura 			preempt();
    344   1.4  nisimura 			reg = dme_phy_read(sc, DM9000_PHY_BMSR);
    345   1.4  nisimura 		}
    346   1.4  nisimura 	}
    347   1.4  nisimura 
    348   1.4  nisimura 
    349   1.4  nisimura 	sc->sc_media_active = IFM_ETHER;
    350   1.4  nisimura 	reg = dme_phy_read(sc, DM9000_PHY_BMCR);
    351   1.4  nisimura 
    352  1.17   msaitoh 	if (reg & DM9000_PHY_BMCR_SPEED_SELECT)
    353   1.4  nisimura 		sc->sc_media_active |= IFM_100_TX;
    354  1.17   msaitoh 	else
    355   1.4  nisimura 		sc->sc_media_active |= IFM_10_T;
    356   1.4  nisimura 
    357  1.17   msaitoh 	if (reg & DM9000_PHY_BMCR_DUPLEX_MODE)
    358   1.4  nisimura 		sc->sc_media_active |= IFM_FDX;
    359   1.4  nisimura }
    360   1.4  nisimura 
    361   1.4  nisimura void
    362   1.4  nisimura dme_phy_check_link(void *arg)
    363   1.4  nisimura {
    364   1.4  nisimura 	struct dme_softc *sc = arg;
    365   1.4  nisimura 	uint32_t reg;
    366   1.4  nisimura 	int s;
    367   1.4  nisimura 
    368   1.4  nisimura 	s = splnet();
    369   1.4  nisimura 
    370   1.4  nisimura 	reg = dme_read(sc, DM9000_NSR) & DM9000_NSR_LINKST;
    371   1.4  nisimura 
    372  1.17   msaitoh 	if (reg)
    373   1.4  nisimura 		reg = IFM_ETHER | IFM_AVALID | IFM_ACTIVE;
    374   1.4  nisimura 	else {
    375   1.4  nisimura 		reg = IFM_ETHER | IFM_AVALID;
    376   1.4  nisimura 		sc->sc_media_active = IFM_NONE;
    377   1.4  nisimura 	}
    378   1.4  nisimura 
    379  1.17   msaitoh 	if ((sc->sc_media_status != reg) && (reg & IFM_ACTIVE))
    380   1.4  nisimura 		dme_phy_reset(sc);
    381   1.4  nisimura 
    382   1.4  nisimura 	sc->sc_media_status = reg;
    383   1.4  nisimura 
    384   1.4  nisimura 	callout_schedule(&sc->sc_link_callout, mstohz(2000));
    385   1.4  nisimura 	splx(s);
    386   1.4  nisimura }
    387   1.4  nisimura 
    388   1.4  nisimura int
    389   1.4  nisimura dme_set_media(struct dme_softc *sc, int media)
    390   1.4  nisimura {
    391   1.4  nisimura 	int s;
    392   1.4  nisimura 
    393   1.4  nisimura 	s = splnet();
    394   1.4  nisimura 	sc->sc_media.ifm_media = media;
    395   1.4  nisimura 	dme_phy_reset(sc);
    396   1.4  nisimura 
    397   1.4  nisimura 	splx(s);
    398   1.4  nisimura 
    399   1.4  nisimura 	return 0;
    400   1.1     ahoka }
    401   1.1     ahoka 
    402   1.1     ahoka int
    403   1.4  nisimura dme_attach(struct dme_softc *sc, const uint8_t *enaddr)
    404   1.1     ahoka {
    405   1.4  nisimura 	struct ifnet	*ifp = &sc->sc_ethercom.ec_if;
    406   1.4  nisimura 	uint8_t		b[2];
    407   1.4  nisimura 	uint16_t	io_mode;
    408   1.1     ahoka 
    409   1.1     ahoka 	dme_read_c(sc, DM9000_VID0, b, 2);
    410   1.1     ahoka #if BYTE_ORDER == BIG_ENDIAN
    411   1.1     ahoka 	sc->sc_vendor_id = (b[0] << 8) | b[1];
    412   1.1     ahoka #else
    413   1.1     ahoka 	sc->sc_vendor_id = b[0] | (b[1] << 8);
    414   1.1     ahoka #endif
    415   1.1     ahoka 	dme_read_c(sc, DM9000_PID0, b, 2);
    416   1.1     ahoka #if BYTE_ORDER == BIG_ENDIAN
    417   1.1     ahoka 	sc->sc_product_id = (b[0] << 8) | b[1];
    418   1.1     ahoka #else
    419   1.1     ahoka 	sc->sc_product_id = b[0] | (b[1] << 8);
    420   1.1     ahoka #endif
    421   1.1     ahoka 	/* TODO: Check the vendor ID as well */
    422   1.1     ahoka 	if (sc->sc_product_id != 0x9000) {
    423   1.1     ahoka 		panic("dme_attach: product id mismatch (0x%hx != 0x9000)",
    424   1.1     ahoka 		    sc->sc_product_id);
    425   1.1     ahoka 	}
    426   1.1     ahoka 
    427   1.1     ahoka 	/* Initialize ifnet structure. */
    428   1.1     ahoka 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    429   1.1     ahoka 	ifp->if_softc = sc;
    430   1.1     ahoka 	ifp->if_start = dme_start_output;
    431   1.1     ahoka 	ifp->if_init = dme_init;
    432   1.1     ahoka 	ifp->if_ioctl = dme_ioctl;
    433   1.1     ahoka 	ifp->if_stop = dme_stop;
    434   1.1     ahoka 	ifp->if_watchdog = NULL;	/* no watchdog at this stage */
    435  1.16   msaitoh 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
    436   1.1     ahoka 	IFQ_SET_READY(&ifp->if_snd);
    437   1.1     ahoka 
    438   1.1     ahoka 	/* Initialize ifmedia structures. */
    439   1.1     ahoka 	ifmedia_init(&sc->sc_media, 0, dme_mediachange, dme_mediastatus);
    440  1.17   msaitoh 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
    441  1.17   msaitoh 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
    442  1.17   msaitoh 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10_T, 0, NULL);
    443  1.17   msaitoh 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
    444  1.17   msaitoh 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_100_TX, 0, NULL);
    445   1.4  nisimura 
    446   1.4  nisimura 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
    447   1.1     ahoka 
    448   1.1     ahoka 	if (enaddr != NULL)
    449   1.1     ahoka 		memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
    450   1.4  nisimura 	/* TODO: Support an EEPROM attached to the DM9000 chip */
    451   1.4  nisimura 
    452   1.4  nisimura 	callout_init(&sc->sc_link_callout, 0);
    453   1.4  nisimura 	callout_setfunc(&sc->sc_link_callout, dme_phy_check_link, sc);
    454   1.4  nisimura 
    455   1.4  nisimura 	sc->sc_media_status = 0;
    456   1.1     ahoka 
    457   1.1     ahoka 	/* Configure DM9000 with the MAC address */
    458   1.1     ahoka 	dme_write_c(sc, DM9000_PAB0, sc->sc_enaddr, 6);
    459   1.1     ahoka 
    460   1.1     ahoka #ifdef DM9000_DEBUG
    461   1.1     ahoka 	{
    462   1.1     ahoka 		uint8_t macAddr[6];
    463   1.1     ahoka 		dme_read_c(sc, DM9000_PAB0, macAddr, 6);
    464   1.1     ahoka 		printf("DM9000 configured with MAC address: ");
    465  1.17   msaitoh 		for (int i = 0; i < 6; i++)
    466   1.1     ahoka 			printf("%02X:", macAddr[i]);
    467   1.1     ahoka 		printf("\n");
    468   1.1     ahoka 	}
    469   1.1     ahoka #endif
    470   1.1     ahoka 
    471   1.1     ahoka 	if_attach(ifp);
    472   1.1     ahoka 	ether_ifattach(ifp, sc->sc_enaddr);
    473   1.1     ahoka 
    474   1.1     ahoka #ifdef DM9000_DEBUG
    475   1.1     ahoka 	{
    476   1.1     ahoka 		uint8_t network_state;
    477   1.1     ahoka 		network_state = dme_read(sc, DM9000_NSR);
    478   1.1     ahoka 		printf("DM9000 Link status: ");
    479   1.1     ahoka 		if (network_state & DM9000_NSR_LINKST) {
    480   1.1     ahoka 			if (network_state & DM9000_NSR_SPEED)
    481   1.1     ahoka 				printf("10Mbps");
    482   1.1     ahoka 			else
    483   1.1     ahoka 				printf("100Mbps");
    484  1.17   msaitoh 		} else
    485   1.1     ahoka 			printf("Down");
    486   1.1     ahoka 		printf("\n");
    487   1.1     ahoka 	}
    488   1.1     ahoka #endif
    489   1.1     ahoka 
    490   1.4  nisimura 	io_mode = (dme_read(sc, DM9000_ISR) &
    491   1.1     ahoka 	    DM9000_IOMODE_MASK) >> DM9000_IOMODE_SHIFT;
    492   1.4  nisimura 
    493   1.4  nisimura 	DPRINTF(("DM9000 Operation Mode: "));
    494   1.4  nisimura 	switch( io_mode) {
    495   1.1     ahoka 	case DM9000_MODE_16BIT:
    496   1.4  nisimura 		DPRINTF(("16-bit mode"));
    497   1.4  nisimura 		sc->sc_data_width = 2;
    498   1.4  nisimura 		sc->sc_pkt_write = dme_pkt_write_2;
    499   1.4  nisimura 		sc->sc_pkt_read = dme_pkt_read_2;
    500   1.1     ahoka 		break;
    501   1.1     ahoka 	case DM9000_MODE_32BIT:
    502   1.4  nisimura 		DPRINTF(("32-bit mode"));
    503   1.4  nisimura 		sc->sc_data_width = 4;
    504   1.6  macallan 		panic("32bit mode is unsupported\n");
    505   1.1     ahoka 		break;
    506   1.1     ahoka 	case DM9000_MODE_8BIT:
    507   1.4  nisimura 		DPRINTF(("8-bit mode"));
    508   1.4  nisimura 		sc->sc_data_width = 1;
    509   1.6  macallan 		sc->sc_pkt_write = dme_pkt_write_1;
    510   1.6  macallan 		sc->sc_pkt_read = dme_pkt_read_1;
    511   1.1     ahoka 		break;
    512   1.4  nisimura 	default:
    513   1.4  nisimura 		DPRINTF(("Invalid mode"));
    514   1.1     ahoka 		break;
    515   1.1     ahoka 	}
    516   1.4  nisimura 	DPRINTF(("\n"));
    517   1.4  nisimura 
    518   1.4  nisimura 	callout_schedule(&sc->sc_link_callout, mstohz(2000));
    519   1.1     ahoka 
    520   1.1     ahoka 	return 0;
    521   1.1     ahoka }
    522   1.1     ahoka 
    523   1.1     ahoka int dme_intr(void *arg)
    524   1.1     ahoka {
    525   1.1     ahoka 	struct dme_softc *sc = arg;
    526   1.1     ahoka 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    527   1.1     ahoka 	uint8_t status;
    528   1.1     ahoka 
    529   1.4  nisimura 
    530   1.4  nisimura 	DPRINTF(("dme_intr: Begin\n"));
    531   1.4  nisimura 
    532   1.1     ahoka 	/* Disable interrupts */
    533   1.1     ahoka 	dme_write(sc, DM9000_IMR, DM9000_IMR_PAR );
    534   1.1     ahoka 
    535   1.1     ahoka 	status = dme_read(sc, DM9000_ISR);
    536   1.1     ahoka 	dme_write(sc, DM9000_ISR, status);
    537   1.1     ahoka 
    538   1.1     ahoka 	if (status & DM9000_ISR_PRS) {
    539   1.1     ahoka 		if (ifp->if_flags & IFF_RUNNING )
    540   1.1     ahoka 			dme_receive(sc, ifp);
    541   1.1     ahoka 	}
    542   1.1     ahoka 	if (status & DM9000_ISR_PTS) {
    543   1.1     ahoka 		uint8_t nsr;
    544   1.1     ahoka 		uint8_t tx_status = 0x01; /* Initialize to an error value */
    545   1.1     ahoka 
    546   1.1     ahoka 		/* A packet has been transmitted */
    547   1.1     ahoka 		sc->txbusy = 0;
    548   1.1     ahoka 
    549   1.1     ahoka 		nsr = dme_read(sc, DM9000_NSR);
    550   1.1     ahoka 
    551   1.1     ahoka 		if (nsr & DM9000_NSR_TX1END) {
    552   1.1     ahoka 			tx_status = dme_read(sc, DM9000_TSR1);
    553   1.1     ahoka 			TX_DPRINTF(("dme_intr: Sent using channel 0\n"));
    554   1.1     ahoka 		} else if (nsr & DM9000_NSR_TX2END) {
    555   1.1     ahoka 			tx_status = dme_read(sc, DM9000_TSR2);
    556   1.1     ahoka 			TX_DPRINTF(("dme_intr: Sent using channel 1\n"));
    557   1.1     ahoka 		}
    558   1.1     ahoka 
    559   1.1     ahoka 		if (tx_status == 0x0) {
    560   1.1     ahoka 			/* Frame successfully sent */
    561   1.1     ahoka 			ifp->if_opackets++;
    562   1.1     ahoka 		} else {
    563   1.1     ahoka 			ifp->if_oerrors++;
    564   1.1     ahoka 		}
    565   1.1     ahoka 
    566   1.1     ahoka 		/* If we have nothing ready to transmit, prepare something */
    567  1.17   msaitoh 		if (!sc->txready)
    568   1.1     ahoka 			dme_prepare(sc, ifp);
    569   1.1     ahoka 
    570   1.1     ahoka 		if (sc->txready)
    571   1.1     ahoka 			dme_transmit(sc);
    572   1.1     ahoka 
    573   1.1     ahoka 		/* Prepare the next frame */
    574   1.1     ahoka 		dme_prepare(sc, ifp);
    575   1.1     ahoka 
    576   1.1     ahoka 	}
    577   1.1     ahoka #ifdef notyet
    578   1.1     ahoka 	if (status & DM9000_ISR_LNKCHNG) {
    579   1.1     ahoka 	}
    580   1.1     ahoka #endif
    581   1.1     ahoka 
    582   1.1     ahoka 	/* Enable interrupts again */
    583  1.17   msaitoh 	dme_write(sc, DM9000_IMR,
    584  1.17   msaitoh 	    DM9000_IMR_PAR | DM9000_IMR_PRM | DM9000_IMR_PTM);
    585   1.1     ahoka 
    586   1.4  nisimura 	DPRINTF(("dme_intr: End\n"));
    587   1.4  nisimura 
    588   1.1     ahoka 	return 1;
    589   1.1     ahoka }
    590   1.1     ahoka 
    591   1.1     ahoka void
    592   1.1     ahoka dme_start_output(struct ifnet *ifp)
    593   1.1     ahoka {
    594   1.1     ahoka 	struct dme_softc *sc;
    595   1.1     ahoka 
    596   1.1     ahoka 	sc = ifp->if_softc;
    597   1.1     ahoka 
    598   1.4  nisimura 	DPRINTF(("dme_start_output: Begin\n"));
    599   1.4  nisimura 
    600   1.1     ahoka 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
    601   1.1     ahoka 		printf("No output\n");
    602   1.1     ahoka 		return;
    603   1.1     ahoka 	}
    604   1.1     ahoka 
    605  1.17   msaitoh 	if (sc->txbusy && sc->txready)
    606   1.1     ahoka 		panic("DM9000: Internal error, trying to send without"
    607   1.1     ahoka 		    " any empty queue\n");
    608   1.1     ahoka 
    609   1.1     ahoka 	dme_prepare(sc, ifp);
    610   1.1     ahoka 
    611   1.1     ahoka 	if (sc->txbusy == 0) {
    612   1.1     ahoka 		/* We are ready to transmit right away */
    613   1.1     ahoka 		dme_transmit(sc);
    614   1.1     ahoka 		dme_prepare(sc, ifp); /* Prepare next one */
    615   1.1     ahoka 	} else {
    616   1.1     ahoka 		/* We need to wait until the current packet has
    617   1.1     ahoka 		 * been transmitted.
    618   1.1     ahoka 		 */
    619   1.1     ahoka 		ifp->if_flags |= IFF_OACTIVE;
    620   1.1     ahoka 	}
    621   1.4  nisimura 
    622   1.4  nisimura 	DPRINTF(("dme_start_output: End\n"));
    623   1.1     ahoka }
    624   1.1     ahoka 
    625   1.1     ahoka void
    626   1.1     ahoka dme_prepare(struct dme_softc *sc, struct ifnet *ifp)
    627   1.1     ahoka {
    628   1.1     ahoka 	struct mbuf *bufChain;
    629   1.1     ahoka 	uint16_t length;
    630   1.1     ahoka 
    631   1.1     ahoka 	TX_DPRINTF(("dme_prepare: Entering\n"));
    632   1.1     ahoka 
    633   1.1     ahoka 	if (sc->txready)
    634   1.1     ahoka 		panic("dme_prepare: Someone called us with txready set\n");
    635   1.1     ahoka 
    636   1.1     ahoka 	IFQ_DEQUEUE(&ifp->if_snd, bufChain);
    637   1.1     ahoka 	if (bufChain == NULL) {
    638   1.1     ahoka 		TX_DPRINTF(("dme_prepare: Nothing to transmit\n"));
    639   1.1     ahoka 		ifp->if_flags &= ~IFF_OACTIVE; /* Clear OACTIVE bit */
    640   1.1     ahoka 		return; /* Nothing to transmit */
    641   1.1     ahoka 	}
    642   1.1     ahoka 
    643   1.1     ahoka 	/* Element has now been removed from the queue, so we better send it */
    644   1.1     ahoka 
    645  1.15   msaitoh 	bpf_mtap(ifp, bufChain, BPF_D_OUT);
    646   1.1     ahoka 
    647   1.1     ahoka 	/* Setup the DM9000 to accept the writes, and then write each buf in
    648   1.1     ahoka 	   the chain. */
    649   1.1     ahoka 
    650   1.1     ahoka 	TX_DATA_DPRINTF(("dme_prepare: Writing data: "));
    651   1.1     ahoka 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->dme_io, DM9000_MWCMD);
    652   1.4  nisimura 	length = sc->sc_pkt_write(sc, bufChain);
    653   1.1     ahoka 	TX_DATA_DPRINTF(("\n"));
    654   1.1     ahoka 
    655  1.17   msaitoh 	if (length % sc->sc_data_width != 0)
    656   1.4  nisimura 		panic("dme_prepare: length is not compatible with IO_MODE");
    657   1.1     ahoka 
    658   1.1     ahoka 	sc->txready_length = length;
    659   1.1     ahoka 	sc->txready = 1;
    660   1.1     ahoka 
    661   1.1     ahoka 	TX_DPRINTF(("dme_prepare: txbusy: %d\ndme_prepare: "
    662   1.1     ahoka 		"txready: %d, txready_length: %d\n",
    663   1.1     ahoka 		sc->txbusy, sc->txready, sc->txready_length));
    664   1.1     ahoka 
    665   1.1     ahoka 	m_freem(bufChain);
    666   1.1     ahoka 
    667   1.1     ahoka 	TX_DPRINTF(("dme_prepare: Leaving\n"));
    668   1.1     ahoka }
    669   1.1     ahoka 
    670   1.1     ahoka int
    671   1.1     ahoka dme_init(struct ifnet *ifp)
    672   1.1     ahoka {
    673   1.1     ahoka 	int s;
    674   1.1     ahoka 	struct dme_softc *sc = ifp->if_softc;
    675   1.1     ahoka 
    676   1.1     ahoka 	dme_stop(ifp, 0);
    677   1.1     ahoka 
    678   1.1     ahoka 	s = splnet();
    679   1.1     ahoka 
    680   1.1     ahoka 	dme_reset(sc);
    681   1.1     ahoka 
    682   1.1     ahoka 	sc->sc_ethercom.ec_if.if_flags |= IFF_RUNNING;
    683   1.1     ahoka 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
    684   1.1     ahoka 	sc->sc_ethercom.ec_if.if_timer = 0;
    685   1.1     ahoka 
    686   1.1     ahoka 	splx(s);
    687   1.1     ahoka 
    688   1.1     ahoka 	return 0;
    689   1.1     ahoka }
    690   1.1     ahoka 
    691   1.1     ahoka int
    692   1.1     ahoka dme_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    693   1.1     ahoka {
    694   1.1     ahoka 	struct dme_softc *sc = ifp->if_softc;
    695   1.1     ahoka 	struct ifreq *ifr = data;
    696   1.1     ahoka 	int s, error = 0;
    697   1.1     ahoka 
    698   1.1     ahoka 	s = splnet();
    699   1.1     ahoka 
    700   1.1     ahoka 	switch(cmd) {
    701   1.1     ahoka 	case SIOCGIFMEDIA:
    702   1.1     ahoka 	case SIOCSIFMEDIA:
    703   1.1     ahoka 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    704   1.1     ahoka 		break;
    705   1.1     ahoka 	default:
    706   1.1     ahoka 		error = ether_ioctl(ifp, cmd, data);
    707   1.4  nisimura 		if (error == ENETRESET) {
    708   1.4  nisimura 			if (ifp->if_flags && IFF_RUNNING) {
    709   1.4  nisimura 				/* Address list has changed, reconfigure
    710   1.4  nisimura 				   filter */
    711   1.4  nisimura 				dme_set_addr_filter(sc);
    712   1.4  nisimura 			}
    713   1.4  nisimura 			error = 0;
    714   1.4  nisimura 		}
    715   1.1     ahoka 		break;
    716   1.1     ahoka 	}
    717   1.1     ahoka 
    718   1.1     ahoka 	splx(s);
    719   1.1     ahoka 	return error;
    720   1.1     ahoka }
    721   1.1     ahoka 
    722   1.1     ahoka void
    723   1.1     ahoka dme_stop(struct ifnet *ifp, int disable)
    724   1.1     ahoka {
    725   1.1     ahoka 	struct dme_softc *sc = ifp->if_softc;
    726   1.1     ahoka 
    727   1.1     ahoka 	/* Not quite sure what to do when called with disable == 0 */
    728   1.1     ahoka 	if (disable) {
    729   1.1     ahoka 		/* Disable RX */
    730   1.1     ahoka 		dme_write(sc, DM9000_RCR, 0x0);
    731   1.1     ahoka 	}
    732   1.1     ahoka 
    733   1.1     ahoka 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    734   1.1     ahoka 	ifp->if_timer = 0;
    735   1.1     ahoka }
    736   1.1     ahoka 
    737   1.1     ahoka int
    738   1.1     ahoka dme_mediachange(struct ifnet *ifp)
    739   1.1     ahoka {
    740   1.4  nisimura 	struct dme_softc *sc = ifp->if_softc;
    741   1.4  nisimura 
    742   1.4  nisimura 	return dme_set_media(sc, sc->sc_media.ifm_cur->ifm_media);
    743   1.1     ahoka }
    744   1.1     ahoka 
    745   1.1     ahoka void
    746   1.1     ahoka dme_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    747   1.1     ahoka {
    748   1.1     ahoka 	struct dme_softc *sc = ifp->if_softc;
    749   1.1     ahoka 
    750   1.4  nisimura 	ifmr->ifm_active = sc->sc_media_active;
    751   1.4  nisimura 	ifmr->ifm_status = sc->sc_media_status;
    752   1.1     ahoka }
    753   1.1     ahoka 
    754   1.1     ahoka void
    755   1.1     ahoka dme_transmit(struct dme_softc *sc)
    756   1.1     ahoka {
    757   1.1     ahoka 
    758   1.1     ahoka 	TX_DPRINTF(("dme_transmit: PRE: txready: %d, txbusy: %d\n",
    759   1.1     ahoka 		sc->txready, sc->txbusy));
    760   1.1     ahoka 
    761   1.1     ahoka 	dme_write(sc, DM9000_TXPLL, sc->txready_length & 0xff);
    762   1.1     ahoka 	dme_write(sc, DM9000_TXPLH, (sc->txready_length >> 8) & 0xff );
    763   1.1     ahoka 
    764   1.1     ahoka 	/* Request to send the packet */
    765   1.5     skrll 	dme_read(sc, DM9000_ISR);
    766   1.1     ahoka 
    767   1.1     ahoka 	dme_write(sc, DM9000_TCR, DM9000_TCR_TXREQ);
    768   1.1     ahoka 
    769   1.1     ahoka 	sc->txready = 0;
    770   1.1     ahoka 	sc->txbusy = 1;
    771   1.1     ahoka 	sc->txready_length = 0;
    772   1.1     ahoka }
    773   1.1     ahoka 
    774   1.1     ahoka void
    775   1.1     ahoka dme_receive(struct dme_softc *sc, struct ifnet *ifp)
    776   1.1     ahoka {
    777   1.1     ahoka 	uint8_t ready = 0x01;
    778   1.1     ahoka 
    779   1.1     ahoka 	DPRINTF(("inside dme_receive\n"));
    780   1.1     ahoka 
    781   1.1     ahoka 	while (ready == 0x01) {
    782   1.1     ahoka 		/* Packet received, retrieve it */
    783   1.1     ahoka 
    784   1.7  macallan 		/* Read without address increment to get the ready byte without
    785   1.7  macallan 		   moving past it. */
    786   1.1     ahoka 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    787   1.1     ahoka 		    sc->dme_io, DM9000_MRCMDX);
    788   1.1     ahoka 		/* Dummy ready */
    789   1.1     ahoka 		ready = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data);
    790   1.1     ahoka 		ready = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data);
    791   1.1     ahoka 		ready &= 0x03;	/* we only want bits 1:0 */
    792   1.1     ahoka 		if (ready == 0x01) {
    793   1.4  nisimura 			uint8_t		rx_status;
    794   1.4  nisimura 			struct mbuf	*m;
    795   1.1     ahoka 
    796   1.1     ahoka 			/* Read with address increment. */
    797   1.1     ahoka 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    798   1.4  nisimura 					  sc->dme_io, DM9000_MRCMD);
    799   1.1     ahoka 
    800   1.4  nisimura 			rx_status = sc->sc_pkt_read(sc, ifp, &m);
    801   1.8  macallan 			if (m == NULL) {
    802   1.8  macallan 				/* failed to allocate a receive buffer */
    803   1.8  macallan 				ifp->if_ierrors++;
    804   1.8  macallan 				RX_DPRINTF(("dme_receive: "
    805   1.8  macallan 					"Error allocating buffer\n"));
    806   1.8  macallan 			} else if (rx_status & (DM9000_RSR_CE | DM9000_RSR_PLE)) {
    807   1.1     ahoka 				/* Error while receiving the packet,
    808   1.1     ahoka 				 * discard it and keep track of counters
    809   1.1     ahoka 				 */
    810   1.1     ahoka 				ifp->if_ierrors++;
    811   1.1     ahoka 				RX_DPRINTF(("dme_receive: "
    812   1.1     ahoka 					"Error reciving packet\n"));
    813   1.1     ahoka 			} else if (rx_status & DM9000_RSR_LCS) {
    814   1.1     ahoka 				ifp->if_collisions++;
    815   1.1     ahoka 			} else {
    816   1.9     ozaki 				if_percpuq_enqueue(ifp->if_percpuq, m);
    817   1.1     ahoka 			}
    818   1.1     ahoka 
    819   1.1     ahoka 		} else if (ready != 0x00) {
    820   1.1     ahoka 			/* Should this be logged somehow? */
    821   1.4  nisimura 			printf("%s: Resetting chip\n",
    822   1.4  nisimura 			       device_xname(sc->sc_dev));
    823   1.1     ahoka 			dme_reset(sc);
    824   1.1     ahoka 		}
    825   1.1     ahoka 	}
    826   1.1     ahoka }
    827   1.1     ahoka 
    828   1.1     ahoka void
    829   1.1     ahoka dme_reset(struct dme_softc *sc)
    830   1.1     ahoka {
    831   1.1     ahoka 	uint8_t var;
    832   1.1     ahoka 
    833   1.4  nisimura 	/* We only re-initialized the PHY in this function the first time it is
    834   1.4  nisimura 	   called. */
    835  1.17   msaitoh 	if (!sc->sc_phy_initialized) {
    836   1.4  nisimura 		/* PHY Reset */
    837   1.4  nisimura 		dme_phy_write(sc, DM9000_PHY_BMCR, DM9000_PHY_BMCR_RESET);
    838   1.4  nisimura 
    839   1.4  nisimura 		/* PHY Power Down */
    840   1.4  nisimura 		var = dme_read(sc, DM9000_GPR);
    841   1.4  nisimura 		dme_write(sc, DM9000_GPR, var | DM9000_GPR_PHY_PWROFF);
    842   1.4  nisimura 	}
    843   1.1     ahoka 
    844   1.4  nisimura 	/* Reset the DM9000 twice, as described in section 2 of the Programming
    845   1.4  nisimura 	   Guide.
    846   1.4  nisimura 	   The PHY is initialized and enabled between those two resets.
    847   1.1     ahoka 	 */
    848   1.4  nisimura 
    849   1.4  nisimura 	/* Software Reset*/
    850   1.1     ahoka 	dme_write(sc, DM9000_NCR,
    851   1.1     ahoka 	    DM9000_NCR_RST | DM9000_NCR_LBK_MAC_INTERNAL);
    852   1.1     ahoka 	delay(20);
    853   1.1     ahoka 	dme_write(sc, DM9000_NCR, 0x0);
    854   1.4  nisimura 
    855  1.17   msaitoh 	if (!sc->sc_phy_initialized) {
    856   1.4  nisimura 		/* PHY Initialization */
    857   1.4  nisimura 		dme_phy_init(sc);
    858   1.4  nisimura 
    859   1.4  nisimura 		/* PHY Enable */
    860   1.4  nisimura 		var = dme_read(sc, DM9000_GPR);
    861   1.4  nisimura 		dme_write(sc, DM9000_GPR, var & ~DM9000_GPR_PHY_PWROFF);
    862   1.4  nisimura 		var = dme_read(sc, DM9000_GPCR);
    863   1.4  nisimura 		dme_write(sc, DM9000_GPCR, var | DM9000_GPCR_GPIO0_OUT);
    864   1.4  nisimura 
    865   1.4  nisimura 		dme_write(sc, DM9000_NCR,
    866  1.17   msaitoh 		    DM9000_NCR_RST | DM9000_NCR_LBK_MAC_INTERNAL);
    867   1.4  nisimura 		delay(20);
    868   1.4  nisimura 		dme_write(sc, DM9000_NCR, 0x0);
    869   1.4  nisimura 	}
    870   1.1     ahoka 
    871   1.1     ahoka 	/* Select internal PHY, no wakeup event, no collosion mode,
    872   1.4  nisimura 	 * normal loopback mode.
    873   1.1     ahoka 	 */
    874   1.1     ahoka 	dme_write(sc, DM9000_NCR, DM9000_NCR_LBK_NORMAL );
    875   1.1     ahoka 
    876   1.1     ahoka 	/* Will clear TX1END, TX2END, and WAKEST fields by reading DM9000_NSR*/
    877   1.1     ahoka 	dme_read(sc, DM9000_NSR);
    878   1.1     ahoka 
    879   1.1     ahoka 	/* Enable wraparound of read/write pointer, packet received latch,
    880   1.1     ahoka 	 * and packet transmitted latch.
    881   1.1     ahoka 	 */
    882   1.1     ahoka 	dme_write(sc, DM9000_IMR,
    883   1.1     ahoka 	    DM9000_IMR_PAR | DM9000_IMR_PRM | DM9000_IMR_PTM);
    884   1.1     ahoka 
    885   1.4  nisimura 	/* Setup multicast address filter, and enable RX. */
    886   1.4  nisimura 	dme_set_addr_filter(sc);
    887   1.4  nisimura 
    888   1.4  nisimura 	/* Obtain media information from PHY */
    889   1.4  nisimura 	dme_phy_update_media(sc);
    890   1.1     ahoka 
    891   1.1     ahoka 	sc->txbusy = 0;
    892   1.1     ahoka 	sc->txready = 0;
    893   1.4  nisimura 	sc->sc_phy_initialized = 1;
    894   1.4  nisimura }
    895   1.4  nisimura 
    896   1.4  nisimura void
    897   1.4  nisimura dme_set_addr_filter(struct dme_softc *sc)
    898   1.4  nisimura {
    899   1.4  nisimura 	struct ether_multi	*enm;
    900   1.4  nisimura 	struct ether_multistep	step;
    901   1.4  nisimura 	struct ethercom		*ec;
    902   1.4  nisimura 	struct ifnet		*ifp;
    903   1.4  nisimura 	uint16_t		af[4];
    904   1.4  nisimura 	int			i;
    905   1.4  nisimura 
    906   1.4  nisimura 	ec = &sc->sc_ethercom;
    907   1.4  nisimura 	ifp = &ec->ec_if;
    908   1.4  nisimura 
    909   1.4  nisimura 	if (ifp->if_flags & IFF_PROMISC) {
    910   1.4  nisimura 		dme_write(sc, DM9000_RCR, DM9000_RCR_RXEN  |
    911   1.4  nisimura 					  DM9000_RCR_WTDIS |
    912   1.4  nisimura 					  DM9000_RCR_PRMSC);
    913   1.4  nisimura 		ifp->if_flags |= IFF_ALLMULTI;
    914   1.4  nisimura 		return;
    915   1.4  nisimura 	}
    916   1.4  nisimura 
    917   1.4  nisimura 	af[0] = af[1] = af[2] = af[3] = 0x0000;
    918   1.4  nisimura 	ifp->if_flags &= ~IFF_ALLMULTI;
    919   1.4  nisimura 
    920   1.4  nisimura 	ETHER_FIRST_MULTI(step, ec, enm);
    921   1.4  nisimura 	while (enm != NULL) {
    922   1.4  nisimura 		uint16_t hash;
    923   1.4  nisimura 		if (memcpy(enm->enm_addrlo, enm->enm_addrhi,
    924   1.4  nisimura 		    sizeof(enm->enm_addrlo))) {
    925   1.4  nisimura 			/*
    926   1.4  nisimura 	                 * We must listen to a range of multicast addresses.
    927   1.4  nisimura 	                 * For now, just accept all multicasts, rather than
    928   1.4  nisimura 	                 * trying to set only those filter bits needed to match
    929   1.4  nisimura 	                 * the range.  (At this time, the only use of address
    930   1.4  nisimura 	                 * ranges is for IP multicast routing, for which the
    931   1.4  nisimura 	                 * range is big enough to require all bits set.)
    932   1.4  nisimura 	                 */
    933   1.4  nisimura 			ifp->if_flags |= IFF_ALLMULTI;
    934   1.4  nisimura 			af[0] = af[1] = af[2] = af[3] = 0xffff;
    935   1.4  nisimura 			break;
    936   1.4  nisimura 		} else {
    937   1.4  nisimura 			hash = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3F;
    938   1.4  nisimura 			af[(uint16_t)(hash>>4)] |= (uint16_t)(1 << (hash % 16));
    939   1.4  nisimura 			ETHER_NEXT_MULTI(step, enm);
    940   1.4  nisimura 		}
    941   1.4  nisimura 	}
    942   1.4  nisimura 
    943   1.4  nisimura 	/* Write the multicast address filter */
    944  1.17   msaitoh 	for (i = 0; i < 4; i++) {
    945   1.4  nisimura 		dme_write(sc, DM9000_MAB0+i*2, af[i] & 0xFF);
    946   1.4  nisimura 		dme_write(sc, DM9000_MAB0+i*2+1, (af[i] >> 8) & 0xFF);
    947   1.4  nisimura 	}
    948   1.4  nisimura 
    949   1.4  nisimura 	/* Setup RX controls */
    950   1.4  nisimura 	dme_write(sc, DM9000_RCR, DM9000_RCR_RXEN | DM9000_RCR_WTDIS);
    951   1.4  nisimura }
    952   1.4  nisimura 
    953   1.4  nisimura int
    954   1.4  nisimura dme_pkt_write_2(struct dme_softc *sc, struct mbuf *bufChain)
    955   1.4  nisimura {
    956   1.4  nisimura 	int left_over_count = 0; /* Number of bytes from previous mbuf, which
    957   1.4  nisimura 				    need to be written with the next.*/
    958   1.4  nisimura 	uint16_t left_over_buf = 0;
    959   1.4  nisimura 	int length = 0;
    960   1.4  nisimura 	struct mbuf *buf;
    961   1.4  nisimura 	uint8_t *write_ptr;
    962   1.4  nisimura 
    963   1.4  nisimura 	/* We expect that the DM9000 has been setup to accept writes before
    964   1.4  nisimura 	   this function is called. */
    965   1.4  nisimura 
    966   1.4  nisimura 	for (buf = bufChain; buf != NULL; buf = buf->m_next) {
    967   1.4  nisimura 		int to_write = buf->m_len;
    968   1.4  nisimura 
    969   1.4  nisimura 		length += to_write;
    970   1.4  nisimura 
    971   1.4  nisimura 		write_ptr = buf->m_data;
    972   1.4  nisimura 		while (to_write > 0 ||
    973  1.17   msaitoh 		    (buf->m_next == NULL && left_over_count > 0)) {
    974   1.4  nisimura 			if (left_over_count > 0) {
    975   1.4  nisimura 				uint8_t b = 0;
    976   1.4  nisimura 				DPRINTF(("dme_pkt_write_16: "
    977   1.4  nisimura 					 "Writing left over byte\n"));
    978   1.4  nisimura 
    979   1.4  nisimura 				if (to_write > 0) {
    980   1.4  nisimura 					b = *write_ptr;
    981   1.4  nisimura 					to_write--;
    982   1.4  nisimura 					write_ptr++;
    983   1.4  nisimura 
    984   1.4  nisimura 					DPRINTF(("Took single byte\n"));
    985   1.4  nisimura 				} else {
    986   1.4  nisimura 					DPRINTF(("Leftover in last run\n"));
    987   1.4  nisimura 					length++;
    988   1.4  nisimura 				}
    989   1.4  nisimura 
    990   1.4  nisimura 				/* Does shift direction depend on endianess? */
    991   1.4  nisimura 				left_over_buf = left_over_buf | (b << 8);
    992   1.4  nisimura 
    993   1.4  nisimura 				bus_space_write_2(sc->sc_iot, sc->sc_ioh,
    994   1.4  nisimura 						  sc->dme_data, left_over_buf);
    995   1.4  nisimura 				TX_DATA_DPRINTF(("%02X ", left_over_buf));
    996   1.4  nisimura 				left_over_count = 0;
    997   1.4  nisimura 			} else if ((long)write_ptr % 2 != 0) {
    998   1.4  nisimura 				/* Misaligned data */
    999   1.4  nisimura 				DPRINTF(("dme_pkt_write_16: "
   1000   1.4  nisimura 					 "Detected misaligned data\n"));
   1001   1.4  nisimura 				left_over_buf = *write_ptr;
   1002   1.4  nisimura 				left_over_count = 1;
   1003   1.4  nisimura 				write_ptr++;
   1004   1.4  nisimura 				to_write--;
   1005   1.4  nisimura 			} else {
   1006   1.4  nisimura 				int i;
   1007   1.7  macallan 				uint16_t *dptr = (uint16_t *)write_ptr;
   1008   1.4  nisimura 
   1009   1.4  nisimura 				/* A block of aligned data. */
   1010  1.17   msaitoh 				for (i = 0; i < to_write / 2; i++) {
   1011   1.4  nisimura 					/* buf will be half-word aligned
   1012   1.4  nisimura 					 * all the time
   1013   1.4  nisimura 					 */
   1014   1.4  nisimura 					bus_space_write_2(sc->sc_iot,
   1015   1.7  macallan 					    sc->sc_ioh, sc->dme_data, *dptr);
   1016   1.4  nisimura 					TX_DATA_DPRINTF(("%02X %02X ",
   1017   1.7  macallan 					    *dptr & 0xFF, (*dptr >> 8) & 0xFF));
   1018   1.4  nisimura 					dptr++;
   1019   1.4  nisimura 				}
   1020   1.4  nisimura 
   1021   1.7  macallan 				write_ptr += i * 2;
   1022   1.4  nisimura 				if (to_write % 2 != 0) {
   1023   1.4  nisimura 					DPRINTF(("dme_pkt_write_16: "
   1024   1.4  nisimura 						 "to_write %% 2: %d\n",
   1025   1.4  nisimura 						 to_write % 2));
   1026   1.4  nisimura 					left_over_count = 1;
   1027   1.4  nisimura 					/* XXX: Does this depend on
   1028   1.4  nisimura 					 * the endianess?
   1029   1.4  nisimura 					 */
   1030   1.4  nisimura 					left_over_buf = *write_ptr;
   1031   1.4  nisimura 
   1032   1.4  nisimura 					write_ptr++;
   1033   1.4  nisimura 					to_write--;
   1034   1.4  nisimura 					DPRINTF(("dme_pkt_write_16: "
   1035   1.4  nisimura 						 "to_write (after): %d\n",
   1036   1.4  nisimura 						 to_write));
   1037   1.7  macallan 					DPRINTF(("dme_pkt_write_16: i * 2: %d\n",
   1038   1.4  nisimura 						 i*2));
   1039   1.4  nisimura 				}
   1040   1.7  macallan 				to_write -= i * 2;
   1041   1.4  nisimura 			}
   1042   1.4  nisimura 		} /* while(...) */
   1043   1.4  nisimura 	} /* for(...) */
   1044   1.4  nisimura 
   1045   1.4  nisimura 	return length;
   1046   1.4  nisimura }
   1047   1.4  nisimura 
   1048   1.4  nisimura int
   1049   1.4  nisimura dme_pkt_read_2(struct dme_softc *sc, struct ifnet *ifp, struct mbuf **outBuf)
   1050   1.4  nisimura {
   1051   1.4  nisimura 	uint8_t rx_status;
   1052   1.4  nisimura 	struct mbuf *m;
   1053   1.4  nisimura 	uint16_t data;
   1054   1.4  nisimura 	uint16_t frame_length;
   1055   1.4  nisimura 	uint16_t i;
   1056   1.4  nisimura 	uint16_t *buf;
   1057   1.4  nisimura 
   1058   1.7  macallan 	data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, sc->dme_data);
   1059   1.4  nisimura 
   1060   1.4  nisimura 	rx_status = data & 0xFF;
   1061   1.4  nisimura 	frame_length = bus_space_read_2(sc->sc_iot,
   1062   1.4  nisimura 					sc->sc_ioh, sc->dme_data);
   1063   1.4  nisimura 	if (frame_length > ETHER_MAX_LEN) {
   1064   1.4  nisimura 		printf("Got frame of length: %d\n", frame_length);
   1065   1.4  nisimura 		printf("ETHER_MAX_LEN is: %d\n", ETHER_MAX_LEN);
   1066   1.4  nisimura 		panic("Something is rotten");
   1067   1.4  nisimura 	}
   1068  1.17   msaitoh 	RX_DPRINTF(("dme_receive: rx_statux: 0x%x, frame_length: %d\n",
   1069  1.17   msaitoh 		rx_status, frame_length));
   1070   1.4  nisimura 
   1071   1.4  nisimura 
   1072   1.4  nisimura 	m = dme_alloc_receive_buffer(ifp, frame_length);
   1073   1.8  macallan 	if (m == NULL) {
   1074   1.8  macallan 		/*
   1075   1.8  macallan 		 * didn't get a receive buffer, so we read the rest of the
   1076   1.8  macallan 		 * packet, throw it away and return an error
   1077   1.8  macallan 		 */
   1078   1.8  macallan 		for (i = 0; i < frame_length; i += 2 ) {
   1079   1.8  macallan 			data = bus_space_read_2(sc->sc_iot,
   1080   1.8  macallan 					sc->sc_ioh, sc->dme_data);
   1081   1.8  macallan 		}
   1082   1.8  macallan 		*outBuf = NULL;
   1083   1.8  macallan 		return 0;
   1084   1.8  macallan 	}
   1085   1.4  nisimura 
   1086   1.4  nisimura 	buf = mtod(m, uint16_t*);
   1087   1.4  nisimura 
   1088   1.4  nisimura 	RX_DPRINTF(("dme_receive: "));
   1089   1.4  nisimura 
   1090   1.7  macallan 	for (i = 0; i < frame_length; i += 2 ) {
   1091   1.4  nisimura 		data = bus_space_read_2(sc->sc_iot,
   1092   1.4  nisimura 					sc->sc_ioh, sc->dme_data);
   1093   1.4  nisimura 		if ( (frame_length % 2 != 0) &&
   1094   1.7  macallan 		     (i == frame_length - 1) ) {
   1095   1.4  nisimura 			data = data & 0xff;
   1096   1.4  nisimura 			RX_DPRINTF((" L "));
   1097   1.4  nisimura 		}
   1098   1.4  nisimura 		*buf = data;
   1099   1.4  nisimura 		buf++;
   1100   1.4  nisimura 		RX_DATA_DPRINTF(("%02X %02X ", data & 0xff,
   1101   1.7  macallan 				 (data >> 8) & 0xff));
   1102   1.4  nisimura 	}
   1103   1.4  nisimura 
   1104   1.4  nisimura 	RX_DATA_DPRINTF(("\n"));
   1105   1.4  nisimura 	RX_DPRINTF(("Read %d bytes\n", i));
   1106   1.4  nisimura 
   1107   1.4  nisimura 	*outBuf = m;
   1108   1.4  nisimura 	return rx_status;
   1109   1.4  nisimura }
   1110   1.4  nisimura 
   1111   1.6  macallan int
   1112   1.6  macallan dme_pkt_write_1(struct dme_softc *sc, struct mbuf *bufChain)
   1113   1.6  macallan {
   1114   1.6  macallan 	int length = 0, i;
   1115   1.6  macallan 	struct mbuf *buf;
   1116   1.6  macallan 	uint8_t *write_ptr;
   1117   1.6  macallan 
   1118  1.17   msaitoh 	/*
   1119  1.17   msaitoh 	 * We expect that the DM9000 has been setup to accept writes before
   1120  1.17   msaitoh 	 * this function is called.
   1121  1.17   msaitoh 	 */
   1122   1.6  macallan 
   1123   1.6  macallan 	for (buf = bufChain; buf != NULL; buf = buf->m_next) {
   1124   1.6  macallan 		int to_write = buf->m_len;
   1125   1.6  macallan 
   1126   1.6  macallan 		length += to_write;
   1127   1.6  macallan 
   1128   1.6  macallan 		write_ptr = buf->m_data;
   1129   1.7  macallan 		for (i = 0; i < to_write; i++) {
   1130   1.6  macallan 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1131   1.6  macallan 			    sc->dme_data, *write_ptr);
   1132   1.6  macallan 			write_ptr++;
   1133   1.6  macallan 		}
   1134   1.6  macallan 	} /* for(...) */
   1135   1.6  macallan 
   1136   1.6  macallan 	return length;
   1137   1.6  macallan }
   1138   1.6  macallan 
   1139   1.6  macallan int
   1140   1.6  macallan dme_pkt_read_1(struct dme_softc *sc, struct ifnet *ifp, struct mbuf **outBuf)
   1141   1.6  macallan {
   1142   1.6  macallan 	uint8_t rx_status;
   1143   1.6  macallan 	struct mbuf *m;
   1144   1.6  macallan 	uint8_t *buf;
   1145   1.6  macallan 	uint16_t frame_length;
   1146   1.6  macallan 	uint16_t i, reg;
   1147   1.6  macallan 	uint8_t data;
   1148   1.6  macallan 
   1149   1.6  macallan 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data);
   1150   1.6  macallan 	reg |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data) << 8;
   1151   1.6  macallan 	rx_status = reg & 0xFF;
   1152   1.6  macallan 
   1153   1.6  macallan 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data);
   1154   1.6  macallan 	reg |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data) << 8;
   1155   1.6  macallan 	frame_length = reg;
   1156   1.7  macallan 
   1157   1.6  macallan 	if (frame_length > ETHER_MAX_LEN) {
   1158   1.6  macallan 		printf("Got frame of length: %d\n", frame_length);
   1159   1.6  macallan 		printf("ETHER_MAX_LEN is: %d\n", ETHER_MAX_LEN);
   1160   1.6  macallan 		panic("Something is rotten");
   1161   1.6  macallan 	}
   1162   1.6  macallan 	RX_DPRINTF(("dme_receive: "
   1163   1.6  macallan 		    "rx_statux: 0x%x, frame_length: %d\n",
   1164   1.6  macallan 		    rx_status, frame_length));
   1165   1.6  macallan 
   1166   1.6  macallan 
   1167   1.6  macallan 	m = dme_alloc_receive_buffer(ifp, frame_length);
   1168   1.8  macallan 	if (m == NULL) {
   1169   1.8  macallan 		/*
   1170   1.8  macallan 		 * didn't get a receive buffer, so we read the rest of the
   1171   1.8  macallan 		 * packet, throw it away and return an error
   1172   1.8  macallan 		 */
   1173   1.8  macallan 		for (i = 0; i < frame_length; i++ ) {
   1174   1.8  macallan 			data = bus_space_read_2(sc->sc_iot,
   1175   1.8  macallan 					sc->sc_ioh, sc->dme_data);
   1176   1.8  macallan 		}
   1177   1.8  macallan 		*outBuf = NULL;
   1178   1.8  macallan 		return 0;
   1179   1.8  macallan 	}
   1180   1.6  macallan 
   1181   1.7  macallan 	buf = mtod(m, uint8_t *);
   1182   1.6  macallan 
   1183   1.6  macallan 	RX_DPRINTF(("dme_receive: "));
   1184   1.6  macallan 
   1185   1.7  macallan 	for (i = 0; i< frame_length; i += 1 ) {
   1186   1.7  macallan 		data = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data);
   1187   1.6  macallan 		*buf = data;
   1188   1.6  macallan 		buf++;
   1189   1.6  macallan 		RX_DATA_DPRINTF(("%02X ", data));
   1190   1.6  macallan 	}
   1191   1.6  macallan 
   1192   1.6  macallan 	RX_DATA_DPRINTF(("\n"));
   1193   1.6  macallan 	RX_DPRINTF(("Read %d bytes\n", i));
   1194   1.6  macallan 
   1195   1.6  macallan 	*outBuf = m;
   1196   1.6  macallan 	return rx_status;
   1197   1.6  macallan }
   1198   1.6  macallan 
   1199   1.4  nisimura struct mbuf*
   1200   1.4  nisimura dme_alloc_receive_buffer(struct ifnet *ifp, unsigned int frame_length)
   1201   1.4  nisimura {
   1202   1.4  nisimura 	struct dme_softc *sc = ifp->if_softc;
   1203   1.4  nisimura 	struct mbuf *m;
   1204   1.4  nisimura 	int pad;
   1205   1.4  nisimura 
   1206   1.4  nisimura 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1207   1.8  macallan 	if (m == NULL) return NULL;
   1208   1.8  macallan 
   1209  1.10     ozaki 	m_set_rcvif(m, ifp);
   1210   1.4  nisimura 	/* Ensure that we always allocate an even number of
   1211   1.4  nisimura 	 * bytes in order to avoid writing beyond the buffer
   1212   1.4  nisimura 	 */
   1213   1.4  nisimura 	m->m_pkthdr.len = frame_length + (frame_length % sc->sc_data_width);
   1214   1.4  nisimura 	pad = ALIGN(sizeof(struct ether_header)) -
   1215   1.4  nisimura 		sizeof(struct ether_header);
   1216   1.4  nisimura 	/* All our frames have the CRC attached */
   1217   1.4  nisimura 	m->m_flags |= M_HASFCS;
   1218  1.12  riastrad 	if (m->m_pkthdr.len + pad > MHLEN) {
   1219   1.4  nisimura 		MCLGET(m, M_DONTWAIT);
   1220  1.12  riastrad 		if ((m->m_flags & M_EXT) == 0) {
   1221  1.12  riastrad 			m_freem(m);
   1222  1.12  riastrad 			return NULL;
   1223  1.12  riastrad 		}
   1224  1.12  riastrad 	}
   1225   1.4  nisimura 
   1226   1.4  nisimura 	m->m_data += pad;
   1227   1.4  nisimura 	m->m_len = frame_length + (frame_length % sc->sc_data_width);
   1228   1.4  nisimura 
   1229   1.4  nisimura 	return m;
   1230   1.1     ahoka }
   1231