iPXE
Defines | Functions | Variables
natsemi.c File Reference

National Semiconductor "MacPhyter" network card driver. More...

#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/netdevice.h>
#include <ipxe/ethernet.h>
#include <ipxe/if_ether.h>
#include <ipxe/iobuf.h>
#include <ipxe/malloc.h>
#include <ipxe/pci.h>
#include <ipxe/nvs.h>
#include <ipxe/bitbash.h>
#include <ipxe/spi_bit.h>
#include <ipxe/threewire.h>
#include "natsemi.h"

Go to the source code of this file.

Defines

#define DP83815_FLAGS   ( NATSEMI_EEPROM_LITTLE_ENDIAN | NATSEMI_EEPROM_INSANE )
 Flags for DP83815.
#define DP83820_FLAGS   ( NATSEMI_64BIT | NATSEMI_1000 )
 Flags for DP83820.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static int natsemi_spi_read_bit (struct bit_basher *basher, unsigned int bit_id)
 Read input bit.
static void natsemi_spi_write_bit (struct bit_basher *basher, unsigned int bit_id, unsigned long data)
 Set/clear output bit.
static void natsemi_init_eeprom (struct natsemi_nic *natsemi)
 Initialise EEPROM.
static void natsemi_hwaddr_sane (struct natsemi_nic *natsemi, const uint16_t *eeprom, uint16_t *hw_addr)
 Get hardware address from sane EEPROM data.
static void natsemi_hwaddr_insane (struct natsemi_nic *natsemi, const uint16_t *eeprom, uint16_t *hw_addr)
 Get hardware address from insane EEPROM data.
static int natsemi_hwaddr (struct natsemi_nic *natsemi, void *hw_addr)
 Get hardware address from EEPROM.
static int natsemi_soft_reset (struct natsemi_nic *natsemi)
 Reset controller chip.
static int natsemi_reload_config (struct natsemi_nic *natsemi)
 Reload configuration from EEPROM.
static int natsemi_reset (struct natsemi_nic *natsemi)
 Reset hardware.
static void natsemi_check_link (struct net_device *netdev)
 Check link state.
static void natsemi_pmatch (struct natsemi_nic *natsemi, const void *mac)
 Set perfect match filter address.
static int natsemi_create_ring (struct natsemi_nic *natsemi, struct natsemi_ring *ring)
 Create descriptor ring.
static void natsemi_destroy_ring (struct natsemi_nic *natsemi, struct natsemi_ring *ring)
 Destroy descriptor ring.
static void natsemi_refill_rx (struct net_device *netdev)
 Refill receive descriptor ring.
static int natsemi_open (struct net_device *netdev)
 Open network device.
static void natsemi_close (struct net_device *netdev)
 Close network device.
static int natsemi_transmit (struct net_device *netdev, struct io_buffer *iobuf)
 Transmit packet.
static void natsemi_poll_tx (struct net_device *netdev)
 Poll for completed packets.
static void natsemi_poll_rx (struct net_device *netdev)
 Poll for received packets.
static void natsemi_poll (struct net_device *netdev)
 Poll for completed and received packets.
static void natsemi_irq (struct net_device *netdev, int enable)
 Enable or disable interrupts.
static int natsemi_probe (struct pci_device *pci)
 Probe PCI device.
static void natsemi_remove (struct pci_device *pci)
 Remove PCI device.

Variables

static const uint8_t natsemi_eeprom_bits []
 Pin mapping for SPI bit-bashing interface.
static struct bit_basher_operations natsemi_basher_ops
 SPI bit-bashing interface.
static struct net_device_operations natsemi_operations
 National Semiconductor network device operations.
static struct pci_device_id natsemi_nics []
 National Semiconductor PCI device IDs.
struct pci_driver natsemi_driver __pci_driver
 National Semiconductor PCI driver.

Detailed Description

National Semiconductor "MacPhyter" network card driver.

Based on the following datasheets:

http://www.ti.com/lit/ds/symlink/dp83820.pdf http://www.datasheets.org.uk/indexdl/Datasheet-03/DSA0041338.pdf

Definition in file natsemi.c.


Define Documentation

Flags for DP83815.

Definition at line 917 of file natsemi.c.

Flags for DP83820.

Definition at line 920 of file natsemi.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER  )
static int natsemi_spi_read_bit ( struct bit_basher basher,
unsigned int  bit_id 
) [static]

Read input bit.

Parameters:
basherBit-bashing interface
bit_idBit number
Return values:
zeroInput is a logic 0
non-zeroInput is a logic 1

Definition at line 73 of file natsemi.c.

References container_of, DBG_DISABLE, DBG_ENABLE, DBGLVL_IO, natsemi_eeprom_bits, NATSEMI_MEAR, readl(), and reg.

                                                        {
        struct natsemi_nic *natsemi = container_of ( basher, struct natsemi_nic,
                                                     spibit.basher );
        uint32_t mask = natsemi_eeprom_bits[bit_id];
        uint32_t reg;

        DBG_DISABLE ( DBGLVL_IO );
        reg = readl ( natsemi->regs + NATSEMI_MEAR );
        DBG_ENABLE ( DBGLVL_IO );
        return ( reg & mask );
}
static void natsemi_spi_write_bit ( struct bit_basher basher,
unsigned int  bit_id,
unsigned long  data 
) [static]

Set/clear output bit.

Parameters:
basherBit-bashing interface
bit_idBit number
dataValue to write

Definition at line 93 of file natsemi.c.

References container_of, DBG_DISABLE, DBG_ENABLE, DBGLVL_IO, natsemi_eeprom_bits, NATSEMI_MEAR, readl(), reg, and writel().

                                                                              {
        struct natsemi_nic *natsemi = container_of ( basher, struct natsemi_nic,
                                                     spibit.basher );
        uint32_t mask = natsemi_eeprom_bits[bit_id];
        uint32_t reg;

        DBG_DISABLE ( DBGLVL_IO );
        reg = readl ( natsemi->regs + NATSEMI_MEAR );
        reg &= ~mask;
        reg |= ( data & mask );
        writel ( reg, natsemi->regs + NATSEMI_MEAR );
        DBG_ENABLE ( DBGLVL_IO );
}
static void natsemi_init_eeprom ( struct natsemi_nic *  natsemi) [static]

Initialise EEPROM.

Parameters:
natsemiNational Semiconductor device

Definition at line 119 of file natsemi.c.

References init_spi_bit_basher(), natsemi_basher_ops, NATSEMI_EEPROM_LITTLE_ENDIAN, SPI_BIT_BIG_ENDIAN, SPI_BIT_LITTLE_ENDIAN, and SPI_MODE_THREEWIRE.

Referenced by natsemi_probe().

                                                                {

        /* Initialise SPI bit-bashing interface */
        natsemi->spibit.basher.op = &natsemi_basher_ops;
        natsemi->spibit.bus.mode = SPI_MODE_THREEWIRE;
        natsemi->spibit.endianness =
                ( ( natsemi->flags & NATSEMI_EEPROM_LITTLE_ENDIAN ) ?
                  SPI_BIT_LITTLE_ENDIAN : SPI_BIT_BIG_ENDIAN );
        init_spi_bit_basher ( &natsemi->spibit );

        /* Initialise EEPROM device */
        init_at93c06 ( &natsemi->eeprom, 16 );
        natsemi->eeprom.bus = &natsemi->spibit.bus;
}
static void natsemi_hwaddr_sane ( struct natsemi_nic *  natsemi,
const uint16_t eeprom,
uint16_t hw_addr 
) [static]

Get hardware address from sane EEPROM data.

Parameters:
natsemiNational Semiconductor device
eepromEEPROM data
hw_addrHardware address to fill in

Definition at line 141 of file natsemi.c.

References DBGC, ETH_ALEN, and NATSEMI_EEPROM_MAC_SANE.

Referenced by natsemi_hwaddr().

                                                                              {
        int i;

        /* Copy MAC address from EEPROM data */
        for ( i = ( ( ETH_ALEN / 2 ) - 1 ) ; i >= 0 ; i-- )
                *(hw_addr++) = eeprom[ NATSEMI_EEPROM_MAC_SANE + i ];

        DBGC ( natsemi, "NATSEMI %p has sane EEPROM layout\n", natsemi );
}
static void natsemi_hwaddr_insane ( struct natsemi_nic *  natsemi,
const uint16_t eeprom,
uint16_t hw_addr 
) [static]

Get hardware address from insane EEPROM data.

Parameters:
natsemiNational Semiconductor device
eepromEEPROM data
hw_addrHardware address to fill in

Definition at line 159 of file natsemi.c.

References cpu_to_le16, DBGC, ETH_ALEN, le16_to_cpu, NATSEMI_EEPROM_MAC_INSANE, and offset.

Referenced by natsemi_hwaddr().

                                                        {
        unsigned int i;
        unsigned int offset;
        uint16_t word;

        /* Copy MAC address from EEPROM data */
        for ( i = 0 ; i < ( ETH_ALEN / 2 ) ; i++ ) {
                offset = ( NATSEMI_EEPROM_MAC_INSANE + i );
                word = ( ( le16_to_cpu ( eeprom[ offset ] ) >> 15 ) |
                         ( le16_to_cpu ( eeprom[ offset + 1 ] << 1 ) ) );
                hw_addr[i] = cpu_to_le16 ( word );
        }

        DBGC ( natsemi, "NATSEMI %p has insane EEPROM layout\n", natsemi );
}
static int natsemi_hwaddr ( struct natsemi_nic *  natsemi,
void *  hw_addr 
) [static]

Get hardware address from EEPROM.

Parameters:
natsemiNational Semiconductor device
hw_addrHardware address to fill in
Return values:
rcReturn status code

Definition at line 184 of file natsemi.c.

References DBGC, DBGC2, DBGC2_HDA, eeprom, NATSEMI_EEPROM_INSANE, NATSEMI_EEPROM_SIZE, natsemi_hwaddr_insane(), natsemi_hwaddr_sane(), nvs_read(), rc, and strerror().

Referenced by natsemi_probe().

                                                                         {
        uint16_t buf[NATSEMI_EEPROM_SIZE];
        void ( * extract ) ( struct natsemi_nic *natsemi,
                             const uint16_t *eeprom, uint16_t *hw_addr );
        int rc;

        /* Read EEPROM contents */
        if ( ( rc = nvs_read ( &natsemi->eeprom.nvs, 0, buf,
                               sizeof ( buf ) ) ) != 0 ) {
                DBGC ( natsemi, "NATSEMI %p could not read EEPROM: %s\n",
                       natsemi, strerror ( rc ) );
                return rc;
        }
        DBGC2 ( natsemi, "NATSEMI %p EEPROM contents:\n", natsemi );
        DBGC2_HDA ( natsemi, 0, buf, sizeof ( buf ) );

        /* Extract MAC address from EEPROM contents */
        extract = ( ( natsemi->flags & NATSEMI_EEPROM_INSANE ) ?
                    natsemi_hwaddr_insane : natsemi_hwaddr_sane );
        extract ( natsemi, buf, hw_addr );

        return 0;
}
static int natsemi_soft_reset ( struct natsemi_nic *  natsemi) [static]

Reset controller chip.

Parameters:
natsemiNational Semiconductor device
Return values:
rcReturn status code

Definition at line 221 of file natsemi.c.

References DBGC, ETIMEDOUT, mdelay(), NATSEMI_CR, NATSEMI_CR_RST, NATSEMI_RESET_MAX_WAIT_MS, readl(), and writel().

Referenced by natsemi_reset().

                                                              {
        unsigned int i;

        /* Initiate reset */
        writel ( NATSEMI_CR_RST, natsemi->regs + NATSEMI_CR );

        /* Wait for reset to complete */
        for ( i = 0 ; i < NATSEMI_RESET_MAX_WAIT_MS ; i++ ) {

                /* If reset is not complete, delay 1ms and retry */
                if ( readl ( natsemi->regs + NATSEMI_CR ) & NATSEMI_CR_RST ) {
                        mdelay ( 1 );
                        continue;
                }

                return 0;
        }

        DBGC ( natsemi, "NATSEMI %p timed out waiting for reset\n", natsemi );
        return -ETIMEDOUT;
}
static int natsemi_reload_config ( struct natsemi_nic *  natsemi) [static]

Reload configuration from EEPROM.

Parameters:
natsemiNational Semiconductor device
Return values:
rcReturn status code

Definition at line 249 of file natsemi.c.

References DBGC, ETIMEDOUT, mdelay(), NATSEMI_EELOAD_MAX_WAIT_MS, NATSEMI_PTSCR, NATSEMI_PTSCR_EELOAD_EN, readl(), and writel().

Referenced by natsemi_reset().

                                                                 {
        unsigned int i;

        /* Initiate reload */
        writel ( NATSEMI_PTSCR_EELOAD_EN, natsemi->regs + NATSEMI_PTSCR );

        /* Wait for reload to complete */
        for ( i = 0 ; i < NATSEMI_EELOAD_MAX_WAIT_MS ; i++ ) {

                /* If reload is not complete, delay 1ms and retry */
                if ( readl ( natsemi->regs + NATSEMI_PTSCR ) &
                     NATSEMI_PTSCR_EELOAD_EN ) {
                        mdelay ( 1 );
                        continue;
                }

                return 0;
        }

        DBGC ( natsemi, "NATSEMI %p timed out waiting for configuration "
               "reload\n", natsemi );
        return -ETIMEDOUT;
}
static int natsemi_reset ( struct natsemi_nic *  natsemi) [static]

Reset hardware.

Parameters:
natsemiNational Semiconductor device
Return values:
rcReturn status code

Definition at line 279 of file natsemi.c.

References cfg, DBGC, NATSEMI_64BIT, NATSEMI_CFG, NATSEMI_CFG_DATA64_EN, NATSEMI_CFG_EXTSTS_EN, NATSEMI_CFG_M64ADDR, NATSEMI_CFG_PCI64_DET, natsemi_reload_config(), natsemi_soft_reset(), rc, readl(), and writel().

Referenced by natsemi_probe(), and natsemi_remove().

                                                         {
        uint32_t cfg;
        int rc;

        /* Perform soft reset */
        if ( ( rc = natsemi_soft_reset ( natsemi ) ) != 0 )
                return rc;

        /* Reload configuration from EEPROM */
        if ( ( rc = natsemi_reload_config ( natsemi ) ) != 0 )
                return rc;

        /* Configure 64-bit operation, if applicable */
        cfg = readl ( natsemi->regs + NATSEMI_CFG );
        if ( natsemi->flags & NATSEMI_64BIT ) {
                cfg |= ( NATSEMI_CFG_M64ADDR | NATSEMI_CFG_EXTSTS_EN );
                if ( ! ( cfg & NATSEMI_CFG_PCI64_DET ) )
                        cfg &= ~NATSEMI_CFG_DATA64_EN;
        }
        writel ( cfg, natsemi->regs + NATSEMI_CFG );

        /* Invalidate link status cache to force an update */
        natsemi->cfg = ~cfg;

        DBGC ( natsemi, "NATSEMI %p using configuration %08x\n",
               natsemi, cfg );
        return 0;
}
static void natsemi_check_link ( struct net_device netdev) [static]

Check link state.

Parameters:
netdevNetwork device

Definition at line 320 of file natsemi.c.

References cfg, DBGC, NATSEMI_1000, NATSEMI_CFG, NATSEMI_CFG_LNKSTS, NATSEMI_CFG_MODE_1000, NATSEMI_CFG_SPDSTS1, netdev_link_down(), netdev_link_up(), net_device::priv, readl(), and writel().

Referenced by natsemi_open(), natsemi_poll(), and natsemi_probe().

                                                             {
        struct natsemi_nic *natsemi = netdev->priv;
        uint32_t cfg;

        /* Read link status */
        cfg = readl ( natsemi->regs + NATSEMI_CFG );

        /* Do nothing unless link status has changed */
        if ( cfg == natsemi->cfg )
                return;

        /* Set gigabit mode (if applicable) */
        if ( natsemi->flags & NATSEMI_1000 ) {
                cfg &= ~NATSEMI_CFG_MODE_1000;
                if ( ! ( cfg & NATSEMI_CFG_SPDSTS1 ) )
                        cfg |= NATSEMI_CFG_MODE_1000;
                writel ( cfg, natsemi->regs + NATSEMI_CFG );
        }

        /* Update link status */
        natsemi->cfg = cfg;
        DBGC ( natsemi, "NATSEMI %p link status is %08x\n", natsemi, cfg );

        /* Update network device */
        if ( cfg & NATSEMI_CFG_LNKSTS ) {
                netdev_link_up ( netdev );
        } else {
                netdev_link_down ( netdev );
        }
}
static void natsemi_pmatch ( struct natsemi_nic *  natsemi,
const void *  mac 
) [static]

Set perfect match filter address.

Parameters:
natsemiNational Semiconductor device
macMAC address

Definition at line 364 of file natsemi.c.

References ETH_ALEN, le16_to_cpu, mac, NATSEMI_RFADDR_PMATCH_BASE, NATSEMI_RFCR, NATSEMI_RFCR_RFADDR, NATSEMI_RFCR_RFADDR_MASK, NATSEMI_RFDR, NATSEMI_RFDR_BMASK, readl(), rfcr, and writel().

Referenced by natsemi_open().

                                                                            {
        const uint16_t *pmatch = mac;
        uint32_t rfcr;
        unsigned int rfaddr;
        unsigned int i;

        for ( i = 0 ; i < ETH_ALEN ; i += sizeof ( *pmatch ) ) {

                /* Select receive filter register address */
                rfaddr = ( NATSEMI_RFADDR_PMATCH_BASE + i );
                rfcr = readl ( natsemi->regs + NATSEMI_RFCR );
                rfcr &= ~NATSEMI_RFCR_RFADDR_MASK;
                rfcr |= NATSEMI_RFCR_RFADDR ( rfaddr );
                writel ( rfcr, natsemi->regs + NATSEMI_RFCR );

                /* Write receive filter data */
                writel ( ( le16_to_cpu ( *(pmatch++) ) | NATSEMI_RFDR_BMASK ),
                         natsemi->regs + NATSEMI_RFDR );
        }
}
static int natsemi_create_ring ( struct natsemi_nic *  natsemi,
struct natsemi_ring ring 
) [static]

Create descriptor ring.

Parameters:
natsemiNational Semiconductor device
ringDescriptor ring
Return values:
rcReturn status code

Definition at line 392 of file natsemi.c.

References address, natsemi_ring::count, cpu_to_le32, cpu_to_le64, natsemi_descriptor::d32, d32, natsemi_descriptor::d64, DBGC, natsemi_ring::desc, ENOMEM, ENOTSUP, free_dma(), len, natsemi_descriptor_32::link, natsemi_descriptor_64::link, link, malloc_dma(), memset(), NATSEMI_64BIT, NULL, offset, offsetof, rc, natsemi_ring::reg, virt_to_bus(), and writel().

Referenced by natsemi_open().

                                                             {
        size_t len = ( ring->count * sizeof ( ring->desc[0] ) );
        union natsemi_descriptor *desc;
        union natsemi_descriptor *linked_desc;
        physaddr_t address;
        physaddr_t link;
        size_t offset;
        unsigned int i;
        int rc;

        /* Calculate descriptor offset */
        offset = ( ( natsemi->flags & NATSEMI_64BIT ) ? 0 :
                   offsetof ( typeof ( desc[i].d32pad ), d32 ) );

        /* Allocate descriptor ring.  Align ring on its own size to
         * ensure that it can't possibly cross the boundary of 32-bit
         * address space.
         */
        ring->desc = malloc_dma ( len, len );
        if ( ! ring->desc ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        address = ( virt_to_bus ( ring->desc ) + offset );

        /* Check address is usable by card */
        if ( ! natsemi_address_ok ( natsemi, address ) ) {
                DBGC ( natsemi, "NATSEMI %p cannot support 64-bit ring "
                       "address\n", natsemi );
                rc = -ENOTSUP;
                goto err_64bit;
        }

        /* Initialise descriptor ring */
        memset ( ring->desc, 0, len );
        for ( i = 0 ; i < ring->count ; i++ ) {
                linked_desc = &ring->desc [ ( i + 1 ) % ring->count ];
                link = ( virt_to_bus ( linked_desc ) + offset );
                if ( natsemi->flags & NATSEMI_64BIT ) {
                        ring->desc[i].d64.link = cpu_to_le64 ( link );
                } else {
                        ring->desc[i].d32pad.d32.link = cpu_to_le32 ( link );
                }
        }

        /* Program ring address */
        writel ( ( address & 0xffffffffUL ), natsemi->regs + ring->reg );
        if ( natsemi->flags & NATSEMI_64BIT ) {
                if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
                        writel ( ( ( ( uint64_t ) address ) >> 32 ),
                                 natsemi->regs + ring->reg + 4 );
                } else {
                        writel ( 0, natsemi->regs + ring->reg + 4 );
                }
        }

        DBGC ( natsemi, "NATSEMI %p ring %02x is at [%08llx,%08llx)\n",
               natsemi, ring->reg,
               ( ( unsigned long long ) virt_to_bus ( ring->desc ) ),
               ( ( unsigned long long ) virt_to_bus ( ring->desc ) + len ) );

        return 0;

 err_64bit:
        free_dma ( ring->desc, len );
        ring->desc = NULL;
 err_alloc:
        return rc;
}
static void natsemi_destroy_ring ( struct natsemi_nic *  natsemi,
struct natsemi_ring ring 
) [static]

Destroy descriptor ring.

Parameters:
natsemiNational Semiconductor device
ringDescriptor ring

Definition at line 469 of file natsemi.c.

References natsemi_ring::cons, natsemi_ring::count, natsemi_ring::desc, free_dma(), len, NATSEMI_64BIT, NULL, natsemi_ring::prod, natsemi_ring::reg, and writel().

Referenced by natsemi_close(), and natsemi_open().

                                                               {
        size_t len = ( ring->count * sizeof ( ring->desc[0] ) );

        /* Clear ring address */
        writel ( 0, natsemi->regs + ring->reg );
        if ( natsemi->flags & NATSEMI_64BIT )
                writel ( 0, natsemi->regs + ring->reg + 4 );

        /* Free descriptor ring */
        free_dma ( ring->desc, len );
        ring->desc = NULL;
        ring->prod = 0;
        ring->cons = 0;
}
static void natsemi_refill_rx ( struct net_device netdev) [static]

Refill receive descriptor ring.

Parameters:
netdevNetwork device

Definition at line 490 of file natsemi.c.

References address, alloc_iob(), assert, natsemi_descriptor_32::bufptr, natsemi_descriptor_64::bufptr, natsemi_descriptor::cmdsts, cpu_to_le32, cpu_to_le64, natsemi_descriptor::d32, natsemi_descriptor::d64, io_buffer::data, DBGC, DBGC2, ENOTSUP, NATSEMI_64BIT, NATSEMI_CR, NATSEMI_CR_RXE, NATSEMI_DESC_INTR, NATSEMI_NUM_RX_DESC, NATSEMI_RX_MAX_LEN, netdev_rx_err(), NULL, net_device::priv, rx, virt_to_bus(), wmb, and writel().

Referenced by natsemi_open(), and natsemi_poll().

                                                            {
        struct natsemi_nic *natsemi = netdev->priv;
        union natsemi_descriptor *rx;
        struct io_buffer *iobuf;
        unsigned int rx_idx;
        physaddr_t address;

        while ( ( natsemi->rx.prod - natsemi->rx.cons ) < NATSEMI_NUM_RX_DESC ){

                /* Allocate I/O buffer */
                iobuf = alloc_iob ( NATSEMI_RX_MAX_LEN );
                if ( ! iobuf ) {
                        /* Wait for next refill */
                        return;
                }

                /* Check address is usable by card */
                address = virt_to_bus ( iobuf->data );
                if ( ! natsemi_address_ok ( natsemi, address ) ) {
                        DBGC ( natsemi, "NATSEMI %p cannot support 64-bit RX "
                               "buffer address\n", natsemi );
                        netdev_rx_err ( netdev, iobuf, -ENOTSUP );
                        return;
                }

                /* Get next receive descriptor */
                rx_idx = ( natsemi->rx.prod++ % NATSEMI_NUM_RX_DESC );
                rx = &natsemi->rx.desc[rx_idx];

                /* Populate receive descriptor */
                if ( natsemi->flags & NATSEMI_64BIT ) {
                        rx->d64.bufptr = cpu_to_le64 ( address );
                } else {
                        rx->d32pad.d32.bufptr = cpu_to_le32 ( address );
                }
                wmb();
                rx->common.cmdsts = cpu_to_le32 ( NATSEMI_DESC_INTR |
                                                  NATSEMI_RX_MAX_LEN );
                wmb();

                /* Record I/O buffer */
                assert ( natsemi->rx_iobuf[rx_idx] == NULL );
                natsemi->rx_iobuf[rx_idx] = iobuf;

                /* Notify card that there are descriptors available */
                writel ( NATSEMI_CR_RXE, natsemi->regs + NATSEMI_CR );

                DBGC2 ( natsemi, "NATSEMI %p RX %d is [%llx,%llx)\n", natsemi,
                        rx_idx, ( ( unsigned long long ) address ),
                        ( ( unsigned long long ) address + NATSEMI_RX_MAX_LEN));
        }
}
static int natsemi_open ( struct net_device netdev) [static]

Open network device.

Parameters:
netdevNetwork device
Return values:
rcReturn status code

Definition at line 549 of file natsemi.c.

References net_device::ll_addr, NATSEMI_64BIT, natsemi_check_link(), natsemi_create_ring(), natsemi_destroy_ring(), NATSEMI_IMR, NATSEMI_IRQ_RXDESC, NATSEMI_IRQ_TXDESC, natsemi_pmatch(), natsemi_refill_rx(), NATSEMI_RFCR, NATSEMI_RFCR_AAB, NATSEMI_RFCR_AAM, NATSEMI_RFCR_AAU, NATSEMI_RFCR_RFEN, NATSEMI_RXCFG_32, NATSEMI_RXCFG_64, NATSEMI_RXCFG_ALP, NATSEMI_RXCFG_ARP, NATSEMI_RXCFG_ATX, NATSEMI_RXCFG_DRTH_DEFAULT, NATSEMI_RXCFG_MXDMA_DEFAULT, NATSEMI_TXCFG_32, NATSEMI_TXCFG_64, NATSEMI_TXCFG_ATP, NATSEMI_TXCFG_CSI, NATSEMI_TXCFG_DRTH_DEFAULT, NATSEMI_TXCFG_ECRETRY, NATSEMI_TXCFG_FLTH_DEFAULT, NATSEMI_TXCFG_HBI, NATSEMI_TXCFG_MXDMA_DEFAULT, net_device::priv, rc, and writel().

                                                      {
        struct natsemi_nic *natsemi = netdev->priv;
        int rc;

        /* Set MAC address */
        natsemi_pmatch ( natsemi, netdev->ll_addr );

        /* Create transmit descriptor ring */
        if ( ( rc = natsemi_create_ring ( natsemi, &natsemi->tx ) ) != 0 )
                goto err_create_tx;

        /* Set transmit configuration */
        writel ( ( NATSEMI_TXCFG_CSI | NATSEMI_TXCFG_HBI | NATSEMI_TXCFG_ATP |
                   NATSEMI_TXCFG_ECRETRY | NATSEMI_TXCFG_MXDMA_DEFAULT |
                   NATSEMI_TXCFG_FLTH_DEFAULT | NATSEMI_TXCFG_DRTH_DEFAULT ),
                 ( natsemi->regs + ( ( natsemi->flags & NATSEMI_64BIT ) ?
                                     NATSEMI_TXCFG_64 : NATSEMI_TXCFG_32 ) ) );

        /* Create receive descriptor ring */
        if ( ( rc = natsemi_create_ring ( natsemi, &natsemi->rx ) ) != 0 )
                goto err_create_rx;

        /* Set receive configuration */
        writel ( ( NATSEMI_RXCFG_ARP | NATSEMI_RXCFG_ATX | NATSEMI_RXCFG_ALP |
                   NATSEMI_RXCFG_MXDMA_DEFAULT | NATSEMI_RXCFG_DRTH_DEFAULT ),
                 ( natsemi->regs + ( ( natsemi->flags & NATSEMI_64BIT ) ?
                                     NATSEMI_RXCFG_64 : NATSEMI_RXCFG_32 ) ) );

        /* Set receive filter configuration */
        writel ( ( NATSEMI_RFCR_RFEN | NATSEMI_RFCR_AAB | NATSEMI_RFCR_AAM |
                   NATSEMI_RFCR_AAU ), natsemi->regs + NATSEMI_RFCR );

        /* Fill receive ring */
        natsemi_refill_rx ( netdev );

        /* Unmask transmit and receive interrupts.  (Interrupts will
         * not be generated unless enabled via the IER.)
         */
        writel ( ( NATSEMI_IRQ_TXDESC | NATSEMI_IRQ_RXDESC ),
                 natsemi->regs + NATSEMI_IMR );

        /* Update link state */
        natsemi_check_link ( netdev );

        return 0;

        natsemi_destroy_ring ( natsemi, &natsemi->rx );
 err_create_rx:
        natsemi_destroy_ring ( natsemi, &natsemi->tx );
 err_create_tx:
        return rc;
}
static void natsemi_close ( struct net_device netdev) [static]

Close network device.

Parameters:
netdevNetwork device

Definition at line 607 of file natsemi.c.

References free_iob(), NATSEMI_CR, NATSEMI_CR_RXR, NATSEMI_CR_TXR, natsemi_destroy_ring(), NATSEMI_IMR, NATSEMI_NUM_RX_DESC, NULL, net_device::priv, and writel().

                                                        {
        struct natsemi_nic *natsemi = netdev->priv;
        unsigned int i;

        /* Mask transmit and receive interrupts */
        writel ( 0, natsemi->regs + NATSEMI_IMR );

        /* Reset and disable transmitter and receiver */
        writel ( ( NATSEMI_CR_RXR | NATSEMI_CR_TXR ),
                 natsemi->regs + NATSEMI_CR );

        /* Discard any unused receive buffers */
        for ( i = 0 ; i < NATSEMI_NUM_RX_DESC ; i++ ) {
                if ( natsemi->rx_iobuf[i] )
                        free_iob ( natsemi->rx_iobuf[i] );
                natsemi->rx_iobuf[i] = NULL;
        }

        /* Destroy receive descriptor ring */
        natsemi_destroy_ring ( natsemi, &natsemi->rx );

        /* Destroy transmit descriptor ring */
        natsemi_destroy_ring ( natsemi, &natsemi->tx );
}
static int natsemi_transmit ( struct net_device netdev,
struct io_buffer iobuf 
) [static]

Transmit packet.

Parameters:
netdevNetwork device
iobufI/O buffer
Return values:
rcReturn status code

Definition at line 639 of file natsemi.c.

References address, natsemi_descriptor_32::bufptr, natsemi_descriptor_64::bufptr, natsemi_descriptor::cmdsts, cpu_to_le32, cpu_to_le64, natsemi_descriptor::d32, natsemi_descriptor::d64, io_buffer::data, DBGC, DBGC2, ENOBUFS, ENOTSUP, iob_len(), NATSEMI_64BIT, NATSEMI_CR, NATSEMI_CR_TXE, NATSEMI_DESC_INTR, NATSEMI_DESC_OWN, NATSEMI_NUM_TX_DESC, net_device::priv, tx, virt_to_bus(), wmb, and writel().

                                                         {
        struct natsemi_nic *natsemi = netdev->priv;
        union natsemi_descriptor *tx;
        unsigned int tx_idx;
        physaddr_t address;

        /* Check address is usable by card */
        address = virt_to_bus ( iobuf->data );
        if ( ! natsemi_address_ok ( natsemi, address ) ) {
                DBGC ( natsemi, "NATSEMI %p cannot support 64-bit TX buffer "
                       "address\n", natsemi );
                return -ENOTSUP;
        }

        /* Get next transmit descriptor */
        if ( ( natsemi->tx.prod - natsemi->tx.cons ) >= NATSEMI_NUM_TX_DESC ) {
                DBGC ( natsemi, "NATSEMI %p out of transmit descriptors\n",
                       natsemi );
                return -ENOBUFS;
        }
        tx_idx = ( natsemi->tx.prod++ % NATSEMI_NUM_TX_DESC );
        tx = &natsemi->tx.desc[tx_idx];

        /* Populate transmit descriptor */
        if ( natsemi->flags & NATSEMI_64BIT ) {
                tx->d64.bufptr = cpu_to_le64 ( address );
        } else {
                tx->d32pad.d32.bufptr = cpu_to_le32 ( address );
        }
        wmb();
        tx->common.cmdsts = cpu_to_le32 ( NATSEMI_DESC_OWN | NATSEMI_DESC_INTR |
                                          iob_len ( iobuf ) );
        wmb();

        /* Notify card that there are packets ready to transmit */
        writel ( NATSEMI_CR_TXE, natsemi->regs + NATSEMI_CR );

        DBGC2 ( natsemi, "NATSEMI %p TX %d is [%llx,%llx)\n", natsemi, tx_idx,
                ( ( unsigned long long ) address ),
                ( ( unsigned long long ) address + iob_len ( iobuf ) ) );

        return 0;
}
static void natsemi_poll_tx ( struct net_device netdev) [static]

Poll for completed packets.

Parameters:
netdevNetwork device

Definition at line 689 of file natsemi.c.

References natsemi_descriptor::cmdsts, cpu_to_le32, DBGC, DBGC2, EIO, le32_to_cpu, NATSEMI_DESC_OK, NATSEMI_DESC_OWN, NATSEMI_NUM_TX_DESC, netdev_tx_complete_next(), netdev_tx_complete_next_err(), net_device::priv, and tx.

Referenced by natsemi_poll().

                                                          {
        struct natsemi_nic *natsemi = netdev->priv;
        union natsemi_descriptor *tx;
        unsigned int tx_idx;

        /* Check for completed packets */
        while ( natsemi->tx.cons != natsemi->tx.prod ) {

                /* Get next transmit descriptor */
                tx_idx = ( natsemi->tx.cons % NATSEMI_NUM_TX_DESC );
                tx = &natsemi->tx.desc[tx_idx];

                /* Stop if descriptor is still in use */
                if ( tx->common.cmdsts & cpu_to_le32 ( NATSEMI_DESC_OWN ) )
                        return;

                /* Complete TX descriptor */
                if ( tx->common.cmdsts & cpu_to_le32 ( NATSEMI_DESC_OK ) ) {
                        DBGC2 ( natsemi, "NATSEMI %p TX %d complete\n",
                                natsemi, tx_idx );
                        netdev_tx_complete_next ( netdev );
                } else {
                        DBGC ( natsemi, "NATSEMI %p TX %d completion error "
                               "(%08x)\n", natsemi, tx_idx,
                               le32_to_cpu ( tx->common.cmdsts ) );
                        netdev_tx_complete_next_err ( netdev, -EIO );
                }
                natsemi->tx.cons++;
        }
}
static void natsemi_poll_rx ( struct net_device netdev) [static]

Poll for received packets.

Parameters:
netdevNetwork device

Definition at line 725 of file natsemi.c.

References natsemi_descriptor::cmdsts, cpu_to_le32, DBGC, DBGC2, EIO, iob_put, le32_to_cpu, len, NATSEMI_DESC_OK, NATSEMI_DESC_OWN, NATSEMI_DESC_SIZE_MASK, NATSEMI_NUM_RX_DESC, netdev_rx(), netdev_rx_err(), NULL, net_device::priv, and rx.

Referenced by natsemi_poll().

                                                          {
        struct natsemi_nic *natsemi = netdev->priv;
        union natsemi_descriptor *rx;
        struct io_buffer *iobuf;
        unsigned int rx_idx;
        size_t len;

        /* Check for received packets */
        while ( natsemi->rx.cons != natsemi->rx.prod ) {

                /* Get next receive descriptor */
                rx_idx = ( natsemi->rx.cons % NATSEMI_NUM_RX_DESC );
                rx = &natsemi->rx.desc[rx_idx];

                /* Stop if descriptor is still in use */
                if ( ! ( rx->common.cmdsts & NATSEMI_DESC_OWN ) )
                        return;

                /* Populate I/O buffer */
                iobuf = natsemi->rx_iobuf[rx_idx];
                natsemi->rx_iobuf[rx_idx] = NULL;
                len = ( le32_to_cpu ( rx->common.cmdsts ) &
                        NATSEMI_DESC_SIZE_MASK );
                iob_put ( iobuf, len - 4 /* strip CRC */ );

                /* Hand off to network stack */
                if ( rx->common.cmdsts & cpu_to_le32 ( NATSEMI_DESC_OK ) ) {
                        DBGC2 ( natsemi, "NATSEMI %p RX %d complete (length "
                                "%zd)\n", natsemi, rx_idx, len );
                        netdev_rx ( netdev, iobuf );
                } else {
                        DBGC ( natsemi, "NATSEMI %p RX %d error (length %zd, "
                               "status %08x)\n", natsemi, rx_idx, len,
                               le32_to_cpu ( rx->common.cmdsts ) );
                        netdev_rx_err ( netdev, iobuf, -EIO );
                }
                natsemi->rx.cons++;
        }
}
static void natsemi_poll ( struct net_device netdev) [static]

Poll for completed and received packets.

Parameters:
netdevNetwork device

Definition at line 770 of file natsemi.c.

References isr, natsemi_check_link(), NATSEMI_IRQ_RXDESC, NATSEMI_IRQ_TXDESC, NATSEMI_ISR, natsemi_poll_rx(), natsemi_poll_tx(), natsemi_refill_rx(), net_device::priv, and readl().

                                                       {
        struct natsemi_nic *natsemi = netdev->priv;
        uint32_t isr;

        /* Poll for link state.  The PHY interrupt seems not to
         * function as expected, and polling for the link state is
         * only a single register read.
         */
        natsemi_check_link ( netdev );

        /* Check for and acknowledge interrupts */
        isr = readl ( natsemi->regs + NATSEMI_ISR );
        if ( ! isr )
                return;

        /* Poll for TX completions, if applicable */
        if ( isr & NATSEMI_IRQ_TXDESC )
                natsemi_poll_tx ( netdev );

        /* Poll for RX completionsm, if applicable */
        if ( isr & NATSEMI_IRQ_RXDESC )
                natsemi_poll_rx ( netdev );

        /* Refill RX ring */
        natsemi_refill_rx ( netdev );
}
static void natsemi_irq ( struct net_device netdev,
int  enable 
) [static]

Enable or disable interrupts.

Parameters:
netdevNetwork device
enableInterrupts should be enabled

Definition at line 803 of file natsemi.c.

References NATSEMI_IER, NATSEMI_IER_IE, net_device::priv, and writel().

                                                                  {
        struct natsemi_nic *natsemi = netdev->priv;

        /* Enable or disable interrupts */
        writel ( ( enable ? NATSEMI_IER_IE : 0 ), natsemi->regs + NATSEMI_IER );
}
static int natsemi_probe ( struct pci_device pci) [static]

Probe PCI device.

Parameters:
pciPCI device
Return values:
rcReturn status code

Definition at line 832 of file natsemi.c.

References adjust_pci_device(), alloc_etherdev(), pci_device::dev, net_device::dev, pci_device_id::driver_data, ENODEV, ENOMEM, net_device::hw_addr, pci_device::id, ioremap(), iounmap(), pci_device::membase, memset(), NATSEMI_BAR_SIZE, natsemi_check_link(), natsemi_hwaddr(), natsemi_init_eeprom(), NATSEMI_NUM_RX_DESC, NATSEMI_NUM_TX_DESC, natsemi_reset(), NATSEMI_RXDP, NATSEMI_TXDP, netdev, netdev_init(), netdev_nullify(), netdev_put(), pci_set_drvdata(), net_device::priv, rc, register_netdev(), and unregister_netdev().

                                                    {
        struct net_device *netdev;
        struct natsemi_nic *natsemi;
        int rc;

        /* Allocate and initialise net device */
        netdev = alloc_etherdev ( sizeof ( *natsemi ) );
        if ( ! netdev ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        netdev_init ( netdev, &natsemi_operations );
        natsemi = netdev->priv;
        pci_set_drvdata ( pci, netdev );
        netdev->dev = &pci->dev;
        memset ( natsemi, 0, sizeof ( *natsemi ) );
        natsemi->flags = pci->id->driver_data;
        natsemi_init_ring ( &natsemi->tx, NATSEMI_NUM_TX_DESC, NATSEMI_TXDP );
        natsemi_init_ring ( &natsemi->rx, NATSEMI_NUM_RX_DESC, NATSEMI_RXDP );

        /* Fix up PCI device */
        adjust_pci_device ( pci );

        /* Map registers */
        natsemi->regs = ioremap ( pci->membase, NATSEMI_BAR_SIZE );
        if ( ! natsemi->regs ) {
                rc = -ENODEV;
                goto err_ioremap;
        }

        /* Reset the NIC */
        if ( ( rc = natsemi_reset ( natsemi ) ) != 0 )
                goto err_reset;

        /* Initialise EEPROM */
        natsemi_init_eeprom ( natsemi );

        /* Read initial MAC address */
        if ( ( rc = natsemi_hwaddr ( natsemi, netdev->hw_addr ) ) != 0 )
                goto err_hwaddr;

        /* Register network device */
        if ( ( rc = register_netdev ( netdev ) ) != 0 )
                goto err_register_netdev;

        /* Set initial link state */
        natsemi_check_link ( netdev );

        return 0;

        unregister_netdev ( netdev );
 err_register_netdev:
 err_hwaddr:
        natsemi_reset ( natsemi );
 err_reset:
        iounmap ( natsemi->regs );
 err_ioremap:
        netdev_nullify ( netdev );
        netdev_put ( netdev );
 err_alloc:
        return rc;
}
static void natsemi_remove ( struct pci_device pci) [static]

Remove PCI device.

Parameters:
pciPCI device

Definition at line 900 of file natsemi.c.

References iounmap(), natsemi_reset(), netdev, netdev_nullify(), netdev_put(), pci_get_drvdata(), net_device::priv, and unregister_netdev().

                                                      {
        struct net_device *netdev = pci_get_drvdata ( pci );
        struct natsemi_nic *natsemi = netdev->priv;

        /* Unregister network device */
        unregister_netdev ( netdev );

        /* Reset card */
        natsemi_reset ( natsemi );

        /* Free network device */
        iounmap ( natsemi->regs );
        netdev_nullify ( netdev );
        netdev_put ( netdev );
}

Variable Documentation

const uint8_t natsemi_eeprom_bits[] [static]
Initial value:

Pin mapping for SPI bit-bashing interface.

Definition at line 58 of file natsemi.c.

Referenced by natsemi_spi_read_bit(), and natsemi_spi_write_bit().

Initial value:

SPI bit-bashing interface.

Definition at line 109 of file natsemi.c.

Referenced by natsemi_init_eeprom().

Initial value:
 {
        .open           = natsemi_open,
        .close          = natsemi_close,
        .transmit       = natsemi_transmit,
        .poll           = natsemi_poll,
        .irq            = natsemi_irq,
}

National Semiconductor network device operations.

Definition at line 811 of file natsemi.c.

struct pci_device_id natsemi_nics[] [static]
Initial value:
 {
        PCI_ROM ( 0x100b, 0x0020, "dp83815", "DP83815", DP83815_FLAGS ),
        PCI_ROM ( 0x100b, 0x0022, "dp83820", "DP83820", DP83820_FLAGS ),
}

National Semiconductor PCI device IDs.

Definition at line 923 of file natsemi.c.

struct pci_driver natsemi_driver __pci_driver
Initial value:
 {
        .ids = natsemi_nics,
        .id_count = ( sizeof ( natsemi_nics ) / sizeof ( natsemi_nics[0] ) ),
        .probe = natsemi_probe,
        .remove = natsemi_remove,
}

National Semiconductor PCI driver.

Definition at line 929 of file natsemi.c.