1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
- #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/threewire.h>
- #include <ipxe/bitbash.h>
- #include <ipxe/mii.h>
- #include "realtek.h"
-
-
-
-
-
-
- static __attribute__ (( unused )) void realtek_dump ( struct realtek_nic *rtl ){
- uint8_t regs[256];
- unsigned int i;
-
-
- if ( ! DBG_LOG )
- return;
-
-
- for ( i = 0 ; i < sizeof ( regs ) ; i++ )
- regs[i] = readb ( rtl->regs + i );
- DBGC ( rtl, "REALTEK %p register dump:\n", rtl );
- DBGC_HDA ( rtl, 0, regs, sizeof ( regs ) );
- }
-
-
-
-
- static const uint8_t realtek_eeprom_bits[] = {
- [SPI_BIT_SCLK] = RTL_9346CR_EESK,
- [SPI_BIT_MOSI] = RTL_9346CR_EEDI,
- [SPI_BIT_MISO] = RTL_9346CR_EEDO,
- [SPI_BIT_SS(0)] = RTL_9346CR_EECS,
- };
-
-
- static void realtek_spi_open_bit ( struct bit_basher *basher ) {
- struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
- spibit.basher );
-
-
- writeb ( RTL_9346CR_EEM_EEPROM, rtl->regs + RTL_9346CR );
- readb ( rtl->regs + RTL_9346CR );
- }
-
-
- static void realtek_spi_close_bit ( struct bit_basher *basher ) {
- struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
- spibit.basher );
-
-
- writeb ( RTL_9346CR_EEM_NORMAL, rtl->regs + RTL_9346CR );
- readb ( rtl->regs + RTL_9346CR );
- }
-
-
- static int realtek_spi_read_bit ( struct bit_basher *basher,
- unsigned int bit_id ) {
- struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
- spibit.basher );
- uint8_t mask = realtek_eeprom_bits[bit_id];
- uint8_t reg;
-
- DBG_DISABLE ( DBGLVL_IO );
- reg = readb ( rtl->regs + RTL_9346CR );
- DBG_ENABLE ( DBGLVL_IO );
- return ( reg & mask );
- }
-
-
- static void realtek_spi_write_bit ( struct bit_basher *basher,
- unsigned int bit_id, unsigned long data ) {
- struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
- spibit.basher );
- uint8_t mask = realtek_eeprom_bits[bit_id];
- uint8_t reg;
-
- DBG_DISABLE ( DBGLVL_IO );
- reg = readb ( rtl->regs + RTL_9346CR );
- reg &= ~mask;
- reg |= ( data & mask );
- writeb ( reg, rtl->regs + RTL_9346CR );
- readb ( rtl->regs + RTL_9346CR );
- DBG_ENABLE ( DBGLVL_IO );
- }
-
-
- static struct bit_basher_operations realtek_basher_ops = {
- .open = realtek_spi_open_bit,
- .close = realtek_spi_close_bit,
- .read = realtek_spi_read_bit,
- .write = realtek_spi_write_bit,
- };
-
-
- static int realtek_init_eeprom ( struct net_device *netdev ) {
- struct realtek_nic *rtl = netdev->priv;
- uint16_t id;
- int rc;
-
-
- rtl->spibit.basher.op = &realtek_basher_ops;
- rtl->spibit.bus.mode = SPI_MODE_THREEWIRE;
- init_spi_bit_basher ( &rtl->spibit );
-
-
- if ( readl ( rtl->regs + RTL_RCR ) & RTL_RCR_9356SEL ) {
- DBGC ( rtl, "REALTEK %p EEPROM is a 93C56\n", rtl );
- init_at93c56 ( &rtl->eeprom, 16 );
- } else {
- DBGC ( rtl, "REALTEK %p EEPROM is a 93C46\n", rtl );
- init_at93c46 ( &rtl->eeprom, 16 );
- }
- rtl->eeprom.bus = &rtl->spibit.bus;
-
-
-
- if ( ( rc = nvs_read ( &rtl->eeprom.nvs, RTL_EEPROM_ID,
- &id, sizeof ( id ) ) ) != 0 ) {
- DBGC ( rtl, "REALTEK %p could not read EEPROM ID: %s\n",
- rtl, strerror ( rc ) );
- return rc;
- }
- if ( id != cpu_to_le16 ( RTL_EEPROM_ID_MAGIC ) ) {
- DBGC ( rtl, "REALTEK %p EEPROM ID incorrect (%#04x); assuming "
- "no EEPROM\n", rtl, le16_to_cpu ( id ) );
- return -ENODEV;
- }
-
-
-
- if ( readb ( rtl->regs + RTL_CONFIG1 ) & RTL_CONFIG1_VPD ) {
- DBGC ( rtl, "REALTEK %p EEPROM in use for VPD; cannot use "
- "for options\n", rtl );
- } else {
- nvo_init ( &rtl->nvo, &rtl->eeprom.nvs, RTL_EEPROM_VPD,
- RTL_EEPROM_VPD_LEN, NULL, &netdev->refcnt );
- }
-
- return 0;
- }
-
-
-
-
- static int realtek_mii_read ( struct mii_interface *mii, unsigned int reg ) {
- struct realtek_nic *rtl = container_of ( mii, struct realtek_nic, mii );
- unsigned int i;
- uint32_t value;
-
-
- if ( ! rtl->have_phy_regs )
- return -ENOTSUP;
-
-
- writel ( RTL_PHYAR_VALUE ( 0, reg, 0 ), rtl->regs + RTL_PHYAR );
-
-
- for ( i = 0 ; i < RTL_MII_MAX_WAIT_US ; i++ ) {
-
-
- value = readl ( rtl->regs + RTL_PHYAR );
- if ( ! ( value & RTL_PHYAR_FLAG ) ) {
- udelay ( 1 );
- continue;
- }
-
-
- return ( RTL_PHYAR_DATA ( value ) );
- }
-
- DBGC ( rtl, "REALTEK %p timed out waiting for MII read\n", rtl );
- return -ETIMEDOUT;
- }
-
-
- static int realtek_mii_write ( struct mii_interface *mii, unsigned int reg,
- unsigned int data) {
- struct realtek_nic *rtl = container_of ( mii, struct realtek_nic, mii );
- unsigned int i;
-
-
- if ( ! rtl->have_phy_regs )
- return -ENOTSUP;
-
-
- writel ( RTL_PHYAR_VALUE ( RTL_PHYAR_FLAG, reg, data ),
- rtl->regs + RTL_PHYAR );
-
-
- for ( i = 0 ; i < RTL_MII_MAX_WAIT_US ; i++ ) {
-
-
- if ( readl ( rtl->regs + RTL_PHYAR ) & RTL_PHYAR_FLAG ) {
- udelay ( 1 );
- continue;
- }
-
- return 0;
- }
-
- DBGC ( rtl, "REALTEK %p timed out waiting for MII write\n", rtl );
- return -ETIMEDOUT;
- }
-
-
- static struct mii_operations realtek_mii_operations = {
- .read = realtek_mii_read,
- .write = realtek_mii_write,
- };
-
-
-
-
- static int realtek_reset ( struct realtek_nic *rtl ) {
- unsigned int i;
-
-
- writeb ( RTL_CR_RST, rtl->regs + RTL_CR );
-
-
- for ( i = 0 ; i < RTL_RESET_MAX_WAIT_MS ; i++ ) {
-
-
- if ( readb ( rtl->regs + RTL_CR ) & RTL_CR_RST ) {
- mdelay ( 1 );
- continue;
- }
-
- return 0;
- }
-
- DBGC ( rtl, "REALTEK %p timed out waiting for reset\n", rtl );
- return -ETIMEDOUT;
- }
-
-
- static int realtek_phy_speed ( struct realtek_nic *rtl ) {
- int ctrl1000;
- int rc;
-
-
- ctrl1000 = mii_read ( &rtl->mii, MII_CTRL1000 );
- if ( ctrl1000 < 0 ) {
- rc = ctrl1000;
- DBGC ( rtl, "REALTEK %p could not read CTRL1000: %s\n",
- rtl, strerror ( rc ) );
- return rc;
- }
-
-
- ctrl1000 |= ( ADVERTISE_1000FULL | ADVERTISE_1000HALF );
- if ( ( rc = mii_write ( &rtl->mii, MII_CTRL1000, ctrl1000 ) ) != 0 ) {
- DBGC ( rtl, "REALTEK %p could not write CTRL1000: %s\n",
- rtl, strerror ( rc ) );
- return rc;
- }
-
- return 0;
- }
-
-
- static int realtek_phy_reset ( struct realtek_nic *rtl ) {
- int rc;
-
-
- if ( ! rtl->have_phy_regs )
- return 0;
-
-
- if ( ( rc = mii_reset ( &rtl->mii ) ) != 0 ) {
- DBGC ( rtl, "REALTEK %p could not reset MII: %s\n",
- rtl, strerror ( rc ) );
- return rc;
- }
-
-
-
- if ( ( rc = realtek_phy_speed ( rtl ) ) != 0 ) {
-
-
- }
-
-
- if ( ( rc = mii_restart ( &rtl->mii ) ) != 0 ) {
- DBGC ( rtl, "REALTEK %p could not restart MII: %s\n",
- rtl, strerror ( rc ) );
- return rc;
- }
-
- return 0;
- }
-
-
-
-
- static void realtek_check_link ( struct net_device *netdev ) {
- struct realtek_nic *rtl = netdev->priv;
- uint8_t phystatus;
- uint8_t msr;
- int link_up;
-
-
- if ( rtl->have_phy_regs ) {
- mii_dump ( &rtl->mii );
- phystatus = readb ( rtl->regs + RTL_PHYSTATUS );
- link_up = ( phystatus & RTL_PHYSTATUS_LINKSTS );
- DBGC ( rtl, "REALTEK %p PHY status is %02x (%s%s%s%s%s%s, "
- "Link%s, %sDuplex)\n", rtl, phystatus,
- ( ( phystatus & RTL_PHYSTATUS_ENTBI ) ? "TBI" : "GMII" ),
- ( ( phystatus & RTL_PHYSTATUS_TXFLOW ) ?
- ", TxFlow" : "" ),
- ( ( phystatus & RTL_PHYSTATUS_RXFLOW ) ?
- ", RxFlow" : "" ),
- ( ( phystatus & RTL_PHYSTATUS_1000MF ) ?
- ", 1000Mbps" : "" ),
- ( ( phystatus & RTL_PHYSTATUS_100M ) ?
- ", 100Mbps" : "" ),
- ( ( phystatus & RTL_PHYSTATUS_10M ) ?
- ", 10Mbps" : "" ),
- ( ( phystatus & RTL_PHYSTATUS_LINKSTS ) ?
- "Up" : "Down" ),
- ( ( phystatus & RTL_PHYSTATUS_FULLDUP ) ?
- "Full" : "Half" ) );
- } else {
- msr = readb ( rtl->regs + RTL_MSR );
- link_up = ( ! ( msr & RTL_MSR_LINKB ) );
- DBGC ( rtl, "REALTEK %p media status is %02x (Link%s, "
- "%dMbps%s%s%s%s%s)\n", rtl, msr,
- ( ( msr & RTL_MSR_LINKB ) ? "Down" : "Up" ),
- ( ( msr & RTL_MSR_SPEED_10 ) ? 10 : 100 ),
- ( ( msr & RTL_MSR_TXFCE ) ? ", TxFlow" : "" ),
- ( ( msr & RTL_MSR_RXFCE ) ? ", RxFlow" : "" ),
- ( ( msr & RTL_MSR_AUX_STATUS ) ? ", AuxPwr" : "" ),
- ( ( msr & RTL_MSR_TXPF ) ? ", TxPause" : "" ),
- ( ( msr & RTL_MSR_RXPF ) ? ", RxPause" : "" ) );
- }
-
-
- if ( link_up ) {
- netdev_link_up ( netdev );
- } else {
- netdev_link_down ( netdev );
- }
- }
-
-
-
-
- static int realtek_create_buffer ( struct realtek_nic *rtl ) {
- size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
- physaddr_t address;
- int rc;
-
-
- if ( ! rtl->legacy )
- return 0;
-
-
- rtl->rx_buffer = malloc_dma ( len, RTL_RXBUF_ALIGN );
- if ( ! rtl->rx_buffer ) {
- rc = -ENOMEM;
- goto err_alloc;
- }
- address = virt_to_bus ( rtl->rx_buffer );
-
-
- if ( address & ~0xffffffffULL ) {
- DBGC ( rtl, "REALTEK %p cannot support 64-bit RX buffer "
- "address\n", rtl );
- rc = -ENOTSUP;
- goto err_64bit;
- }
-
-
- writel ( address, rtl->regs + RTL_RBSTART );
- DBGC ( rtl, "REALTEK %p receive buffer is at [%08llx,%08llx,%08llx)\n",
- rtl, ( ( unsigned long long ) address ),
- ( ( unsigned long long ) address + RTL_RXBUF_LEN ),
- ( ( unsigned long long ) address + len ) );
-
- return 0;
-
- err_64bit:
- free_dma ( rtl->rx_buffer, len );
- rtl->rx_buffer = NULL;
- err_alloc:
- return rc;
- }
-
-
- static void realtek_destroy_buffer ( struct realtek_nic *rtl ) {
- size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
-
-
- if ( ! rtl->legacy )
- return;
-
-
- writel ( 0, rtl->regs + RTL_RBSTART );
-
-
- free_dma ( rtl->rx_buffer, len );
- rtl->rx_buffer = NULL;
- rtl->rx_offset = 0;
- }
-
-
- static int realtek_create_ring ( struct realtek_nic *rtl,
- struct realtek_ring *ring ) {
- physaddr_t address;
-
-
- if ( rtl->legacy )
- return 0;
-
-
- ring->desc = malloc_dma ( ring->len, RTL_RING_ALIGN );
- if ( ! ring->desc )
- return -ENOMEM;
-
-
- memset ( ring->desc, 0, ring->len );
-
-
- address = virt_to_bus ( ring->desc );
- writel ( ( ( ( uint64_t ) address ) >> 32 ),
- rtl->regs + ring->reg + 4 );
- writel ( ( address & 0xffffffffUL ), rtl->regs + ring->reg );
- DBGC ( rtl, "REALTEK %p ring %02x is at [%08llx,%08llx)\n",
- rtl, ring->reg, ( ( unsigned long long ) address ),
- ( ( unsigned long long ) address + ring->len ) );
-
- return 0;
- }
-
-
- static void realtek_destroy_ring ( struct realtek_nic *rtl,
- struct realtek_ring *ring ) {
-
-
- ring->prod = 0;
- ring->cons = 0;
-
-
- if ( rtl->legacy )
- return;
-
-
- writel ( 0, rtl->regs + ring->reg );
- writel ( 0, rtl->regs + ring->reg + 4 );
-
-
- free_dma ( ring->desc, ring->len );
- ring->desc = NULL;
- }
-
-
- static void realtek_refill_rx ( struct realtek_nic *rtl ) {
- struct realtek_descriptor *rx;
- struct io_buffer *iobuf;
- unsigned int rx_idx;
- physaddr_t address;
- int is_last;
-
-
- if ( rtl->legacy )
- return;
-
- while ( ( rtl->rx.prod - rtl->rx.cons ) < RTL_NUM_RX_DESC ) {
-
-
- iobuf = alloc_iob ( RTL_RX_MAX_LEN );
- if ( ! iobuf ) {
-
- return;
- }
-
-
- rx_idx = ( rtl->rx.prod++ % RTL_NUM_RX_DESC );
- is_last = ( rx_idx == ( RTL_NUM_RX_DESC - 1 ) );
- rx = &rtl->rx.desc[rx_idx];
-
-
- address = virt_to_bus ( iobuf->data );
- rx->address = cpu_to_le64 ( address );
- rx->length = cpu_to_le16 ( RTL_RX_MAX_LEN );
- wmb();
- rx->flags = ( cpu_to_le16 ( RTL_DESC_OWN ) |
- ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
- wmb();
-
-
- assert ( rtl->rx_iobuf[rx_idx] == NULL );
- rtl->rx_iobuf[rx_idx] = iobuf;
-
- DBGC2 ( rtl, "REALTEK %p RX %d is [%llx,%llx)\n", rtl, rx_idx,
- ( ( unsigned long long ) address ),
- ( ( unsigned long long ) address + RTL_RX_MAX_LEN ) );
- }
- }
-
-
- static int realtek_open ( struct net_device *netdev ) {
- struct realtek_nic *rtl = netdev->priv;
- uint32_t tcr;
- uint32_t rcr;
- int rc;
-
-
- if ( ( rc = realtek_create_ring ( rtl, &rtl->tx ) ) != 0 )
- goto err_create_tx;
-
-
- if ( ( rc = realtek_create_ring ( rtl, &rtl->rx ) ) != 0 )
- goto err_create_rx;
-
-
- if ( ( rc = realtek_create_buffer ( rtl ) ) != 0 )
- goto err_create_buffer;
-
-
- writel ( 0xffffffffUL, rtl->regs + RTL_MAR0 );
- writel ( 0xffffffffUL, rtl->regs + RTL_MAR4 );
-
-
-
- writeb ( ( RTL_CR_TE | RTL_CR_RE ), rtl->regs + RTL_CR );
-
-
- tcr = readl ( rtl->regs + RTL_TCR );
- tcr &= ~RTL_TCR_MXDMA_MASK;
- tcr |= RTL_TCR_MXDMA_DEFAULT;
- writel ( tcr, rtl->regs + RTL_TCR );
-
-
- rcr = readl ( rtl->regs + RTL_RCR );
- rcr &= ~( RTL_RCR_STOP_WORKING | RTL_RCR_RXFTH_MASK |
- RTL_RCR_RBLEN_MASK | RTL_RCR_MXDMA_MASK );
- rcr |= ( RTL_RCR_RXFTH_DEFAULT | RTL_RCR_RBLEN_DEFAULT |
- RTL_RCR_MXDMA_DEFAULT | RTL_RCR_WRAP | RTL_RCR_AB |
- RTL_RCR_AM | RTL_RCR_APM | RTL_RCR_AAP );
- writel ( rcr, rtl->regs + RTL_RCR );
-
-
- realtek_refill_rx ( rtl );
-
-
- realtek_check_link ( netdev );
-
- return 0;
-
- realtek_destroy_buffer ( rtl );
- err_create_buffer:
- realtek_destroy_ring ( rtl, &rtl->rx );
- err_create_rx:
- realtek_destroy_ring ( rtl, &rtl->tx );
- err_create_tx:
- return rc;
- }
-
-
- static void realtek_close ( struct net_device *netdev ) {
- struct realtek_nic *rtl = netdev->priv;
- unsigned int i;
-
-
- writeb ( 0, rtl->regs + RTL_CR );
-
-
- realtek_destroy_buffer ( rtl );
-
-
- realtek_destroy_ring ( rtl, &rtl->rx );
-
-
- for ( i = 0 ; i < RTL_NUM_RX_DESC ; i++ ) {
- if ( rtl->rx_iobuf[i] )
- free_iob ( rtl->rx_iobuf[i] );
- rtl->rx_iobuf[i] = NULL;
- }
-
-
- realtek_destroy_ring ( rtl, &rtl->tx );
- }
-
-
- static int realtek_transmit ( struct net_device *netdev,
- struct io_buffer *iobuf ) {
- struct realtek_nic *rtl = netdev->priv;
- struct realtek_descriptor *tx;
- unsigned int tx_idx;
- physaddr_t address;
- int is_last;
-
-
- if ( ( rtl->tx.prod - rtl->tx.cons ) >= RTL_NUM_TX_DESC ) {
- netdev_tx_defer ( netdev, iobuf );
- return 0;
- }
- tx_idx = ( rtl->tx.prod++ % RTL_NUM_TX_DESC );
-
-
- if ( rtl->legacy ) {
-
-
- iob_pad ( iobuf, ETH_ZLEN );
- address = virt_to_bus ( iobuf->data );
-
-
- if ( address & ~0xffffffffULL ) {
- DBGC ( rtl, "REALTEK %p cannot support 64-bit TX "
- "buffer address\n", rtl );
- return -ENOTSUP;
- }
-
-
- writel ( address, rtl->regs + RTL_TSAD ( tx_idx ) );
- writel ( ( RTL_TSD_ERTXTH_DEFAULT | iob_len ( iobuf ) ),
- rtl->regs + RTL_TSD ( tx_idx ) );
-
- } else {
-
-
- address = virt_to_bus ( iobuf->data );
- is_last = ( tx_idx == ( RTL_NUM_TX_DESC - 1 ) );
- tx = &rtl->tx.desc[tx_idx];
- tx->address = cpu_to_le64 ( address );
- tx->length = cpu_to_le16 ( iob_len ( iobuf ) );
- wmb();
- tx->flags = ( cpu_to_le16 ( RTL_DESC_OWN | RTL_DESC_FS |
- RTL_DESC_LS ) |
- ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
- wmb();
-
-
- writeb ( RTL_TPPOLL_NPQ, rtl->regs + rtl->tppoll );
- }
-
- DBGC2 ( rtl, "REALTEK %p TX %d is [%llx,%llx)\n", rtl, tx_idx,
- ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ),
- ( ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ) +
- iob_len ( iobuf ) ) );
-
- return 0;
- }
-
-
- static void realtek_poll_tx ( struct net_device *netdev ) {
- struct realtek_nic *rtl = netdev->priv;
- struct realtek_descriptor *tx;
- unsigned int tx_idx;
-
-
- while ( rtl->tx.cons != rtl->tx.prod ) {
-
-
- tx_idx = ( rtl->tx.cons % RTL_NUM_TX_DESC );
-
-
- if ( rtl->legacy ) {
-
-
- if ( ! ( readl ( rtl->regs + RTL_TSD ( tx_idx ) ) &
- RTL_TSD_OWN ) )
- return;
-
- } else {
-
-
- tx = &rtl->tx.desc[tx_idx];
- if ( tx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
- return;
- }
-
- DBGC2 ( rtl, "REALTEK %p TX %d complete\n", rtl, tx_idx );
-
-
- rtl->tx.cons++;
- netdev_tx_complete_next ( netdev );
- }
- }
-
-
- static void realtek_legacy_poll_rx ( struct net_device *netdev ) {
- struct realtek_nic *rtl = netdev->priv;
- struct realtek_legacy_header *rx;
- struct io_buffer *iobuf;
- size_t len;
-
-
- while ( ! ( readb ( rtl->regs + RTL_CR ) & RTL_CR_BUFE ) ) {
-
-
- rx = ( rtl->rx_buffer + rtl->rx_offset );
- len = le16_to_cpu ( rx->length );
- if ( rx->status & cpu_to_le16 ( RTL_STAT_ROK ) ) {
-
- DBGC2 ( rtl, "REALTEK %p RX offset %x+%zx\n",
- rtl, rtl->rx_offset, len );
-
-
- iobuf = alloc_iob ( len );
- if ( ! iobuf ) {
- netdev_rx_err ( netdev, NULL, -ENOMEM );
-
- break;
- }
-
-
- memcpy ( iob_put ( iobuf, len ), rx->data, len );
- iob_unput ( iobuf, 4 );
-
-
- netdev_rx ( netdev, iobuf );
-
- } else {
-
- DBGC ( rtl, "REALTEK %p RX offset %x+%zx error %04x\n",
- rtl, rtl->rx_offset, len,
- le16_to_cpu ( rx->status ) );
- netdev_rx_err ( netdev, NULL, -EIO );
- }
-
-
- rtl->rx_offset = ( rtl->rx_offset + sizeof ( *rx ) + len );
- rtl->rx_offset = ( ( rtl->rx_offset + 3 ) & ~3 );
- rtl->rx_offset = ( rtl->rx_offset % RTL_RXBUF_LEN );
- writew ( ( rtl->rx_offset - 16 ), rtl->regs + RTL_CAPR );
-
-
- readw ( rtl->regs + RTL_CAPR );
- }
- }
-
-
- static void realtek_poll_rx ( struct net_device *netdev ) {
- struct realtek_nic *rtl = netdev->priv;
- struct realtek_descriptor *rx;
- struct io_buffer *iobuf;
- unsigned int rx_idx;
- size_t len;
-
-
- if ( rtl->legacy ) {
- realtek_legacy_poll_rx ( netdev );
- return;
- }
-
-
- while ( rtl->rx.cons != rtl->rx.prod ) {
-
-
- rx_idx = ( rtl->rx.cons % RTL_NUM_RX_DESC );
- rx = &rtl->rx.desc[rx_idx];
-
-
- if ( rx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
- return;
-
-
- iobuf = rtl->rx_iobuf[rx_idx];
- rtl->rx_iobuf[rx_idx] = NULL;
- len = ( le16_to_cpu ( rx->length ) & RTL_DESC_SIZE_MASK );
- iob_put ( iobuf, ( len - 4 ) );
-
-
- if ( rx->flags & cpu_to_le16 ( RTL_DESC_RES ) ) {
- DBGC ( rtl, "REALTEK %p RX %d error (length %zd, "
- "flags %04x)\n", rtl, rx_idx, len,
- le16_to_cpu ( rx->flags ) );
- netdev_rx_err ( netdev, iobuf, -EIO );
- } else {
- DBGC2 ( rtl, "REALTEK %p RX %d complete (length "
- "%zd)\n", rtl, rx_idx, len );
- netdev_rx ( netdev, iobuf );
- }
- rtl->rx.cons++;
- }
- }
-
-
- static void realtek_poll ( struct net_device *netdev ) {
- struct realtek_nic *rtl = netdev->priv;
- uint16_t isr;
-
-
- isr = readw ( rtl->regs + RTL_ISR );
- if ( ! isr )
- return;
- writew ( isr, rtl->regs + RTL_ISR );
-
-
- if ( isr & ( RTL_IRQ_TER | RTL_IRQ_TOK ) )
- realtek_poll_tx ( netdev );
-
-
- if ( isr & ( RTL_IRQ_RER | RTL_IRQ_ROK ) )
- realtek_poll_rx ( netdev );
-
-
- if ( isr & RTL_IRQ_PUN_LINKCHG )
- realtek_check_link ( netdev );
-
-
- realtek_refill_rx ( rtl );
- }
-
-
- static void realtek_irq ( struct net_device *netdev, int enable ) {
- struct realtek_nic *rtl = netdev->priv;
- uint16_t imr;
-
-
- imr = ( enable ? ( RTL_IRQ_PUN_LINKCHG | RTL_IRQ_TER | RTL_IRQ_TOK |
- RTL_IRQ_RER | RTL_IRQ_ROK ) : 0 );
- writew ( imr, rtl->regs + RTL_IMR );
- }
-
-
- static struct net_device_operations realtek_operations = {
- .open = realtek_open,
- .close = realtek_close,
- .transmit = realtek_transmit,
- .poll = realtek_poll,
- .irq = realtek_irq,
- };
-
-
-
-
- static void realtek_detect ( struct realtek_nic *rtl ) {
- uint16_t rms;
- uint16_t check_rms;
- uint16_t cpcr;
- uint16_t check_cpcr;
-
-
-
- rms = RTL_RX_MAX_LEN;
- writew ( rms, rtl->regs + RTL_RMS );
- check_rms = readw ( rtl->regs + RTL_RMS );
-
-
-
- cpcr = readw ( rtl->regs + RTL_CPCR );
- cpcr |= ( RTL_CPCR_MULRW | RTL_CPCR_CPRX | RTL_CPCR_CPTX );
- if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) )
- cpcr |= RTL_CPCR_DAC;
- writew ( cpcr, rtl->regs + RTL_CPCR );
- check_cpcr = readw ( rtl->regs + RTL_CPCR );
-
-
- if ( check_rms == rms ) {
- DBGC ( rtl, "REALTEK %p appears to be an RTL8169\n", rtl );
- rtl->have_phy_regs = 1;
- rtl->tppoll = RTL_TPPOLL_8169;
- } else {
- if ( ( check_cpcr == cpcr ) && ( cpcr != 0xffff ) ) {
- DBGC ( rtl, "REALTEK %p appears to be an RTL8139C+\n",
- rtl );
- rtl->tppoll = RTL_TPPOLL_8139CP;
- } else {
- DBGC ( rtl, "REALTEK %p appears to be an RTL8139\n",
- rtl );
- rtl->legacy = 1;
- }
- }
- }
-
-
- static int realtek_probe ( struct pci_device *pci ) {
- struct net_device *netdev;
- struct realtek_nic *rtl;
- unsigned int i;
- int rc;
-
-
- netdev = alloc_etherdev ( sizeof ( *rtl ) );
- if ( ! netdev ) {
- rc = -ENOMEM;
- goto err_alloc;
- }
- netdev_init ( netdev, &realtek_operations );
- rtl = netdev->priv;
- pci_set_drvdata ( pci, netdev );
- netdev->dev = &pci->dev;
- memset ( rtl, 0, sizeof ( *rtl ) );
- realtek_init_ring ( &rtl->tx, RTL_NUM_TX_DESC, RTL_TNPDS );
- realtek_init_ring ( &rtl->rx, RTL_NUM_RX_DESC, RTL_RDSAR );
-
-
- adjust_pci_device ( pci );
-
-
- rtl->regs = ioremap ( pci->membase, RTL_BAR_SIZE );
-
-
- if ( ( rc = realtek_reset ( rtl ) ) != 0 )
- goto err_reset;
-
-
- realtek_detect ( rtl );
-
-
- if ( ( rc = realtek_init_eeprom ( netdev ) ) == 0 ) {
-
-
- if ( ( rc = nvs_read ( &rtl->eeprom.nvs, RTL_EEPROM_MAC,
- netdev->hw_addr, ETH_ALEN ) ) != 0 ) {
- DBGC ( rtl, "REALTEK %p could not read MAC address: "
- "%s\n", rtl, strerror ( rc ) );
- goto err_nvs_read;
- }
-
- } else {
-
-
-
- for ( i = 0 ; i < ETH_ALEN ; i++ )
- netdev->hw_addr[i] = readb ( rtl->regs + RTL_IDR0 + i );
- }
-
-
- mii_init ( &rtl->mii, &realtek_mii_operations );
- if ( ( rc = realtek_phy_reset ( rtl ) ) != 0 )
- goto err_phy_reset;
-
-
- if ( ( rc = register_netdev ( netdev ) ) != 0 )
- goto err_register_netdev;
-
-
- realtek_check_link ( netdev );
-
-
- if ( rtl->nvo.nvs ) {
- if ( ( rc = register_nvo ( &rtl->nvo,
- netdev_settings ( netdev ) ) ) != 0)
- goto err_register_nvo;
- }
-
- return 0;
-
- err_register_nvo:
- unregister_netdev ( netdev );
- err_register_netdev:
- err_phy_reset:
- err_nvs_read:
- realtek_reset ( rtl );
- err_reset:
- iounmap ( rtl->regs );
- netdev_nullify ( netdev );
- netdev_put ( netdev );
- err_alloc:
- return rc;
- }
-
-
- static void realtek_remove ( struct pci_device *pci ) {
- struct net_device *netdev = pci_get_drvdata ( pci );
- struct realtek_nic *rtl = netdev->priv;
-
-
- if ( rtl->nvo.nvs )
- unregister_nvo ( &rtl->nvo );
-
-
- unregister_netdev ( netdev );
-
-
- realtek_reset ( rtl );
-
-
- iounmap ( rtl->regs );
- netdev_nullify ( netdev );
- netdev_put ( netdev );
- }
-
-
- static struct pci_device_id realtek_nics[] = {
- PCI_ROM ( 0x0001, 0x8168, "clone8169", "Cloned 8169", 0 ),
- PCI_ROM ( 0x018a, 0x0106, "fpc0106tx", "LevelOne FPC-0106TX", 0 ),
- PCI_ROM ( 0x021b, 0x8139, "hne300", "Compaq HNE-300", 0 ),
- PCI_ROM ( 0x02ac, 0x1012, "s1012", "SpeedStream 1012", 0 ),
- PCI_ROM ( 0x0357, 0x000a, "ttpmon", "TTTech TTP-Monitoring", 0 ),
- PCI_ROM ( 0x10ec, 0x8129, "rtl8129", "RTL-8129", 0 ),
- PCI_ROM ( 0x10ec, 0x8136, "rtl8136", "RTL8101E/RTL8102E", 0 ),
- PCI_ROM ( 0x10ec, 0x8138, "rtl8138", "RT8139 (B/C)", 0 ),
- PCI_ROM ( 0x10ec, 0x8139, "rtl8139", "RTL-8139/8139C/8139C+", 0 ),
- PCI_ROM ( 0x10ec, 0x8167, "rtl8167", "RTL-8110SC/8169SC", 0 ),
- PCI_ROM ( 0x10ec, 0x8168, "rtl8168", "RTL8111/8168B", 0 ),
- PCI_ROM ( 0x10ec, 0x8169, "rtl8169", "RTL-8169", 0 ),
- PCI_ROM ( 0x1113, 0x1211, "smc1211", "SMC2-1211TX", 0 ),
- PCI_ROM ( 0x1186, 0x1300, "dfe538", "DFE530TX+/DFE538TX", 0 ),
- PCI_ROM ( 0x1186, 0x1340, "dfe690", "DFE-690TXD", 0 ),
- PCI_ROM ( 0x1186, 0x4300, "dge528t", "DGE-528T", 0 ),
- PCI_ROM ( 0x11db, 0x1234, "sega8139", "Sega Enterprises 8139", 0 ),
- PCI_ROM ( 0x1259, 0xa117, "allied8139", "Allied Telesyn 8139", 0 ),
- PCI_ROM ( 0x1259, 0xa11e, "allied81xx", "Allied Telesyn 81xx", 0 ),
- PCI_ROM ( 0x1259, 0xc107, "allied8169", "Allied Telesyn 8169", 0 ),
- PCI_ROM ( 0x126c, 0x1211, "northen8139","Northern Telecom 8139", 0 ),
- PCI_ROM ( 0x13d1, 0xab06, "fe2000vx", "Abocom FE2000VX", 0 ),
- PCI_ROM ( 0x1432, 0x9130, "edi8139", "Edimax 8139", 0 ),
- PCI_ROM ( 0x14ea, 0xab06, "fnw3603tx", "Planex FNW-3603-TX", 0 ),
- PCI_ROM ( 0x14ea, 0xab07, "fnw3800tx", "Planex FNW-3800-TX", 0 ),
- PCI_ROM ( 0x1500, 0x1360, "delta8139", "Delta Electronics 8139", 0 ),
- PCI_ROM ( 0x16ec, 0x0116, "usr997902", "USR997902", 0 ),
- PCI_ROM ( 0x1737, 0x1032, "linksys8169","Linksys 8169", 0 ),
- PCI_ROM ( 0x1743, 0x8139, "rolf100", "Peppercorn ROL/F-100", 0 ),
- PCI_ROM ( 0x4033, 0x1360, "addron8139", "Addtron 8139", 0 ),
- PCI_ROM ( 0xffff, 0x8139, "clonse8139", "Cloned 8139", 0 ),
- };
-
-
- struct pci_driver realtek_driver __pci_driver = {
- .ids = realtek_nics,
- .id_count = ( sizeof ( realtek_nics ) / sizeof ( realtek_nics[0] ) ),
- .probe = realtek_probe,
- .remove = realtek_remove,
- };
|