Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * RDC R6040 Fast Ethernet MAC support |
| 3 | * |
| 4 | * Copyright (C) 2004 Sten Wang <sten.wang@rdc.com.tw> |
| 5 | * Copyright (C) 2007 |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 6 | * Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us> |
Florian Fainelli | 1caf09d | 2012-04-11 07:18:43 +0000 | [diff] [blame] | 7 | * Copyright (C) 2007-2012 Florian Fainelli <florian@openwrt.org> |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version 2 |
| 12 | * of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the |
| 21 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 | * Boston, MA 02110-1301, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/timer.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/ioport.h> |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/pci.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/etherdevice.h> |
| 36 | #include <linux/skbuff.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/delay.h> |
| 39 | #include <linux/mii.h> |
| 40 | #include <linux/ethtool.h> |
| 41 | #include <linux/crc32.h> |
| 42 | #include <linux/spinlock.h> |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 43 | #include <linux/bitops.h> |
| 44 | #include <linux/io.h> |
| 45 | #include <linux/irq.h> |
| 46 | #include <linux/uaccess.h> |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 47 | #include <linux/phy.h> |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 48 | |
| 49 | #include <asm/processor.h> |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 50 | |
| 51 | #define DRV_NAME "r6040" |
Florian Fainelli | 5bdc4f5 | 2011-10-06 23:36:28 +0000 | [diff] [blame] | 52 | #define DRV_VERSION "0.28" |
| 53 | #define DRV_RELDATE "07Oct2011" |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 54 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 55 | /* Time in jiffies before concluding the transmitter is hung. */ |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 56 | #define TX_TIMEOUT (6000 * HZ / 1000) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 57 | |
| 58 | /* RDC MAC I/O Size */ |
| 59 | #define R6040_IO_SIZE 256 |
| 60 | |
| 61 | /* MAX RDC MAC */ |
| 62 | #define MAX_MAC 2 |
| 63 | |
| 64 | /* MAC registers */ |
| 65 | #define MCR0 0x00 /* Control register 0 */ |
Florian Fainelli | 4e16d6e | 2012-01-04 08:59:34 +0000 | [diff] [blame] | 66 | #define MCR0_RCVEN 0x0002 /* Receive enable */ |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 67 | #define MCR0_PROMISC 0x0020 /* Promiscuous mode */ |
| 68 | #define MCR0_HASH_EN 0x0100 /* Enable multicast hash table function */ |
Florian Fainelli | 4e16d6e | 2012-01-04 08:59:34 +0000 | [diff] [blame] | 69 | #define MCR0_XMTEN 0x1000 /* Transmission enable */ |
| 70 | #define MCR0_FD 0x8000 /* Full/Half duplex */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 71 | #define MCR1 0x04 /* Control register 1 */ |
| 72 | #define MAC_RST 0x0001 /* Reset the MAC */ |
| 73 | #define MBCR 0x08 /* Bus control */ |
| 74 | #define MT_ICR 0x0C /* TX interrupt control */ |
| 75 | #define MR_ICR 0x10 /* RX interrupt control */ |
| 76 | #define MTPR 0x14 /* TX poll command register */ |
Florian Fainelli | 940ff7e | 2012-04-11 07:18:41 +0000 | [diff] [blame] | 77 | #define TM2TX 0x0001 /* Trigger MAC to transmit */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 78 | #define MR_BSR 0x18 /* RX buffer size */ |
| 79 | #define MR_DCR 0x1A /* RX descriptor control */ |
| 80 | #define MLSR 0x1C /* Last status */ |
Florian Fainelli | 8dd87a2 | 2012-04-11 07:18:40 +0000 | [diff] [blame] | 81 | #define TX_FIFO_UNDR 0x0200 /* TX FIFO under-run */ |
| 82 | #define TX_EXCEEDC 0x2000 /* Transmit exceed collision */ |
| 83 | #define TX_LATEC 0x4000 /* Transmit late collision */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 84 | #define MMDIO 0x20 /* MDIO control register */ |
| 85 | #define MDIO_WRITE 0x4000 /* MDIO write */ |
| 86 | #define MDIO_READ 0x2000 /* MDIO read */ |
| 87 | #define MMRD 0x24 /* MDIO read data register */ |
| 88 | #define MMWD 0x28 /* MDIO write data register */ |
| 89 | #define MTD_SA0 0x2C /* TX descriptor start address 0 */ |
| 90 | #define MTD_SA1 0x30 /* TX descriptor start address 1 */ |
| 91 | #define MRD_SA0 0x34 /* RX descriptor start address 0 */ |
| 92 | #define MRD_SA1 0x38 /* RX descriptor start address 1 */ |
| 93 | #define MISR 0x3C /* Status register */ |
| 94 | #define MIER 0x40 /* INT enable register */ |
| 95 | #define MSK_INT 0x0000 /* Mask off interrupts */ |
Florian Fainelli | 3d25434 | 2008-07-13 14:28:27 +0200 | [diff] [blame] | 96 | #define RX_FINISH 0x0001 /* RX finished */ |
| 97 | #define RX_NO_DESC 0x0002 /* No RX descriptor available */ |
| 98 | #define RX_FIFO_FULL 0x0004 /* RX FIFO full */ |
| 99 | #define RX_EARLY 0x0008 /* RX early */ |
| 100 | #define TX_FINISH 0x0010 /* TX finished */ |
| 101 | #define TX_EARLY 0x0080 /* TX early */ |
| 102 | #define EVENT_OVRFL 0x0100 /* Event counter overflow */ |
| 103 | #define LINK_CHANGED 0x0200 /* PHY link changed */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 104 | #define ME_CISR 0x44 /* Event counter INT status */ |
| 105 | #define ME_CIER 0x48 /* Event counter INT enable */ |
| 106 | #define MR_CNT 0x50 /* Successfully received packet counter */ |
| 107 | #define ME_CNT0 0x52 /* Event counter 0 */ |
| 108 | #define ME_CNT1 0x54 /* Event counter 1 */ |
| 109 | #define ME_CNT2 0x56 /* Event counter 2 */ |
| 110 | #define ME_CNT3 0x58 /* Event counter 3 */ |
| 111 | #define MT_CNT 0x5A /* Successfully transmit packet counter */ |
| 112 | #define ME_CNT4 0x5C /* Event counter 4 */ |
| 113 | #define MP_CNT 0x5E /* Pause frame counter register */ |
| 114 | #define MAR0 0x60 /* Hash table 0 */ |
| 115 | #define MAR1 0x62 /* Hash table 1 */ |
| 116 | #define MAR2 0x64 /* Hash table 2 */ |
| 117 | #define MAR3 0x66 /* Hash table 3 */ |
| 118 | #define MID_0L 0x68 /* Multicast address MID0 Low */ |
| 119 | #define MID_0M 0x6A /* Multicast address MID0 Medium */ |
| 120 | #define MID_0H 0x6C /* Multicast address MID0 High */ |
| 121 | #define MID_1L 0x70 /* MID1 Low */ |
| 122 | #define MID_1M 0x72 /* MID1 Medium */ |
| 123 | #define MID_1H 0x74 /* MID1 High */ |
| 124 | #define MID_2L 0x78 /* MID2 Low */ |
| 125 | #define MID_2M 0x7A /* MID2 Medium */ |
| 126 | #define MID_2H 0x7C /* MID2 High */ |
| 127 | #define MID_3L 0x80 /* MID3 Low */ |
| 128 | #define MID_3M 0x82 /* MID3 Medium */ |
| 129 | #define MID_3H 0x84 /* MID3 High */ |
| 130 | #define PHY_CC 0x88 /* PHY status change configuration register */ |
Florian Fainelli | 31171ae | 2012-04-11 07:18:42 +0000 | [diff] [blame] | 131 | #define SCEN 0x8000 /* PHY status change enable */ |
| 132 | #define PHYAD_SHIFT 8 /* PHY address shift */ |
| 133 | #define TMRDIV_SHIFT 0 /* Timer divider shift */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 134 | #define PHY_ST 0x8A /* PHY status register */ |
| 135 | #define MAC_SM 0xAC /* MAC status machine */ |
Florian Fainelli | e147763 | 2012-01-04 08:59:36 +0000 | [diff] [blame] | 136 | #define MAC_SM_RST 0x0002 /* MAC status machine reset */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 137 | #define MAC_ID 0xBE /* Identifier register */ |
| 138 | |
| 139 | #define TX_DCNT 0x80 /* TX descriptor count */ |
| 140 | #define RX_DCNT 0x80 /* RX descriptor count */ |
| 141 | #define MAX_BUF_SIZE 0x600 |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 142 | #define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor)) |
| 143 | #define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor)) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 144 | #define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */ |
Florian Fainelli | 3bcf822 | 2010-04-07 16:50:58 -0700 | [diff] [blame] | 145 | #define MCAST_MAX 3 /* Max number multicast addresses to filter */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 146 | |
Florian Fainelli | 2fa15bb | 2012-04-11 07:18:38 +0000 | [diff] [blame] | 147 | #define MAC_DEF_TIMEOUT 2048 /* Default MAC read/write operation timeout */ |
| 148 | |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 149 | /* Descriptor status */ |
| 150 | #define DSC_OWNER_MAC 0x8000 /* MAC is the owner of this descriptor */ |
| 151 | #define DSC_RX_OK 0x4000 /* RX was successful */ |
| 152 | #define DSC_RX_ERR 0x0800 /* RX PHY error */ |
| 153 | #define DSC_RX_ERR_DRI 0x0400 /* RX dribble packet */ |
| 154 | #define DSC_RX_ERR_BUF 0x0200 /* RX length exceeds buffer size */ |
| 155 | #define DSC_RX_ERR_LONG 0x0100 /* RX length > maximum packet length */ |
| 156 | #define DSC_RX_ERR_RUNT 0x0080 /* RX packet length < 64 byte */ |
| 157 | #define DSC_RX_ERR_CRC 0x0040 /* RX CRC error */ |
| 158 | #define DSC_RX_BCAST 0x0020 /* RX broadcast (no error) */ |
| 159 | #define DSC_RX_MCAST 0x0010 /* RX multicast (no error) */ |
| 160 | #define DSC_RX_MCH_HIT 0x0008 /* RX multicast hit in hash table (no error) */ |
| 161 | #define DSC_RX_MIDH_HIT 0x0004 /* RX MID table hit (no error) */ |
| 162 | #define DSC_RX_IDX_MID_MASK 3 /* RX mask for the index of matched MIDx */ |
| 163 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 164 | MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>," |
| 165 | "Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>," |
| 166 | "Florian Fainelli <florian@openwrt.org>"); |
| 167 | MODULE_LICENSE("GPL"); |
| 168 | MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet driver"); |
Florian Fainelli | bc4de26 | 2009-04-08 15:50:43 -0700 | [diff] [blame] | 169 | MODULE_VERSION(DRV_VERSION " " DRV_RELDATE); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 170 | |
Florian Fainelli | 3d25434 | 2008-07-13 14:28:27 +0200 | [diff] [blame] | 171 | /* RX and TX interrupts that we handle */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 172 | #define RX_INTS (RX_FIFO_FULL | RX_NO_DESC | RX_FINISH) |
| 173 | #define TX_INTS (TX_FINISH) |
| 174 | #define INT_MASK (RX_INTS | TX_INTS) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 175 | |
| 176 | struct r6040_descriptor { |
| 177 | u16 status, len; /* 0-3 */ |
| 178 | __le32 buf; /* 4-7 */ |
| 179 | __le32 ndesc; /* 8-B */ |
| 180 | u32 rev1; /* C-F */ |
| 181 | char *vbufp; /* 10-13 */ |
| 182 | struct r6040_descriptor *vndescp; /* 14-17 */ |
| 183 | struct sk_buff *skb_ptr; /* 18-1B */ |
| 184 | u32 rev2; /* 1C-1F */ |
Florian Fainelli | 853d5dc | 2012-01-04 08:59:37 +0000 | [diff] [blame] | 185 | } __aligned(32); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 186 | |
| 187 | struct r6040_private { |
| 188 | spinlock_t lock; /* driver lock */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 189 | struct pci_dev *pdev; |
| 190 | struct r6040_descriptor *rx_insert_ptr; |
| 191 | struct r6040_descriptor *rx_remove_ptr; |
| 192 | struct r6040_descriptor *tx_insert_ptr; |
| 193 | struct r6040_descriptor *tx_remove_ptr; |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 194 | struct r6040_descriptor *rx_ring; |
| 195 | struct r6040_descriptor *tx_ring; |
| 196 | dma_addr_t rx_ring_dma; |
| 197 | dma_addr_t tx_ring_dma; |
Florian Fainelli | 49f2672 | 2012-01-04 08:59:33 +0000 | [diff] [blame] | 198 | u16 tx_free_desc; |
Florian Fainelli | 0db0cfc | 2012-04-11 07:18:37 +0000 | [diff] [blame] | 199 | u16 mcr0; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 200 | struct net_device *dev; |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 201 | struct mii_bus *mii_bus; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 202 | struct napi_struct napi; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 203 | void __iomem *base; |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 204 | struct phy_device *phydev; |
| 205 | int old_link; |
| 206 | int old_duplex; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 209 | static char version[] __devinitdata = DRV_NAME |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 210 | ": RDC R6040 NAPI net driver," |
Florian Fainelli | 9a48ce8 | 2009-01-08 11:00:52 -0800 | [diff] [blame] | 211 | "version "DRV_VERSION " (" DRV_RELDATE ")"; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 212 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 213 | /* Read a word data from PHY Chip */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 214 | static int r6040_phy_read(void __iomem *ioaddr, int phy_addr, int reg) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 215 | { |
Florian Fainelli | 2fa15bb | 2012-04-11 07:18:38 +0000 | [diff] [blame] | 216 | int limit = MAC_DEF_TIMEOUT; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 217 | u16 cmd; |
| 218 | |
| 219 | iowrite16(MDIO_READ + reg + (phy_addr << 8), ioaddr + MMDIO); |
| 220 | /* Wait for the read bit to be cleared */ |
| 221 | while (limit--) { |
| 222 | cmd = ioread16(ioaddr + MMDIO); |
Joe Chou | 11e5e8f | 2008-12-22 19:38:17 -0800 | [diff] [blame] | 223 | if (!(cmd & MDIO_READ)) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | |
| 227 | return ioread16(ioaddr + MMRD); |
| 228 | } |
| 229 | |
| 230 | /* Write a word data from PHY Chip */ |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 231 | static void r6040_phy_write(void __iomem *ioaddr, |
| 232 | int phy_addr, int reg, u16 val) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 233 | { |
Florian Fainelli | 2fa15bb | 2012-04-11 07:18:38 +0000 | [diff] [blame] | 234 | int limit = MAC_DEF_TIMEOUT; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 235 | u16 cmd; |
| 236 | |
| 237 | iowrite16(val, ioaddr + MMWD); |
| 238 | /* Write the command to the MDIO bus */ |
| 239 | iowrite16(MDIO_WRITE + reg + (phy_addr << 8), ioaddr + MMDIO); |
| 240 | /* Wait for the write bit to be cleared */ |
| 241 | while (limit--) { |
| 242 | cmd = ioread16(ioaddr + MMDIO); |
Joe Chou | 11e5e8f | 2008-12-22 19:38:17 -0800 | [diff] [blame] | 243 | if (!(cmd & MDIO_WRITE)) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 248 | static int r6040_mdiobus_read(struct mii_bus *bus, int phy_addr, int reg) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 249 | { |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 250 | struct net_device *dev = bus->priv; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 251 | struct r6040_private *lp = netdev_priv(dev); |
| 252 | void __iomem *ioaddr = lp->base; |
| 253 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 254 | return r6040_phy_read(ioaddr, phy_addr, reg); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 257 | static int r6040_mdiobus_write(struct mii_bus *bus, int phy_addr, |
| 258 | int reg, u16 value) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 259 | { |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 260 | struct net_device *dev = bus->priv; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 261 | struct r6040_private *lp = netdev_priv(dev); |
| 262 | void __iomem *ioaddr = lp->base; |
| 263 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 264 | r6040_phy_write(ioaddr, phy_addr, reg, value); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int r6040_mdiobus_reset(struct mii_bus *bus) |
| 270 | { |
| 271 | return 0; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 274 | static void r6040_free_txbufs(struct net_device *dev) |
| 275 | { |
| 276 | struct r6040_private *lp = netdev_priv(dev); |
| 277 | int i; |
| 278 | |
| 279 | for (i = 0; i < TX_DCNT; i++) { |
| 280 | if (lp->tx_insert_ptr->skb_ptr) { |
Al Viro | ed773b4 | 2008-03-16 22:43:06 +0000 | [diff] [blame] | 281 | pci_unmap_single(lp->pdev, |
| 282 | le32_to_cpu(lp->tx_insert_ptr->buf), |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 283 | MAX_BUF_SIZE, PCI_DMA_TODEVICE); |
| 284 | dev_kfree_skb(lp->tx_insert_ptr->skb_ptr); |
Florian Fainelli | 3b060be | 2008-09-24 21:16:40 +0200 | [diff] [blame] | 285 | lp->tx_insert_ptr->skb_ptr = NULL; |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 286 | } |
| 287 | lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void r6040_free_rxbufs(struct net_device *dev) |
| 292 | { |
| 293 | struct r6040_private *lp = netdev_priv(dev); |
| 294 | int i; |
| 295 | |
| 296 | for (i = 0; i < RX_DCNT; i++) { |
| 297 | if (lp->rx_insert_ptr->skb_ptr) { |
Al Viro | ed773b4 | 2008-03-16 22:43:06 +0000 | [diff] [blame] | 298 | pci_unmap_single(lp->pdev, |
| 299 | le32_to_cpu(lp->rx_insert_ptr->buf), |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 300 | MAX_BUF_SIZE, PCI_DMA_FROMDEVICE); |
| 301 | dev_kfree_skb(lp->rx_insert_ptr->skb_ptr); |
| 302 | lp->rx_insert_ptr->skb_ptr = NULL; |
| 303 | } |
| 304 | lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp; |
| 305 | } |
| 306 | } |
| 307 | |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 308 | static void r6040_init_ring_desc(struct r6040_descriptor *desc_ring, |
| 309 | dma_addr_t desc_dma, int size) |
| 310 | { |
| 311 | struct r6040_descriptor *desc = desc_ring; |
| 312 | dma_addr_t mapping = desc_dma; |
| 313 | |
| 314 | while (size-- > 0) { |
Julia Lawall | 3f6602a | 2008-06-23 23:12:31 +0200 | [diff] [blame] | 315 | mapping += sizeof(*desc); |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 316 | desc->ndesc = cpu_to_le32(mapping); |
| 317 | desc->vndescp = desc + 1; |
| 318 | desc++; |
| 319 | } |
| 320 | desc--; |
| 321 | desc->ndesc = cpu_to_le32(desc_dma); |
| 322 | desc->vndescp = desc_ring; |
| 323 | } |
| 324 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 325 | static void r6040_init_txbufs(struct net_device *dev) |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 326 | { |
| 327 | struct r6040_private *lp = netdev_priv(dev); |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 328 | |
| 329 | lp->tx_free_desc = TX_DCNT; |
| 330 | |
| 331 | lp->tx_remove_ptr = lp->tx_insert_ptr = lp->tx_ring; |
| 332 | r6040_init_ring_desc(lp->tx_ring, lp->tx_ring_dma, TX_DCNT); |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 335 | static int r6040_alloc_rxbufs(struct net_device *dev) |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 336 | { |
| 337 | struct r6040_private *lp = netdev_priv(dev); |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 338 | struct r6040_descriptor *desc; |
| 339 | struct sk_buff *skb; |
| 340 | int rc; |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 341 | |
| 342 | lp->rx_remove_ptr = lp->rx_insert_ptr = lp->rx_ring; |
| 343 | r6040_init_ring_desc(lp->rx_ring, lp->rx_ring_dma, RX_DCNT); |
| 344 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 345 | /* Allocate skbs for the rx descriptors */ |
| 346 | desc = lp->rx_ring; |
| 347 | do { |
| 348 | skb = netdev_alloc_skb(dev, MAX_BUF_SIZE); |
| 349 | if (!skb) { |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 350 | netdev_err(dev, "failed to alloc skb for rx\n"); |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 351 | rc = -ENOMEM; |
| 352 | goto err_exit; |
| 353 | } |
| 354 | desc->skb_ptr = skb; |
| 355 | desc->buf = cpu_to_le32(pci_map_single(lp->pdev, |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 356 | desc->skb_ptr->data, |
| 357 | MAX_BUF_SIZE, PCI_DMA_FROMDEVICE)); |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 358 | desc->status = DSC_OWNER_MAC; |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 359 | desc = desc->vndescp; |
| 360 | } while (desc != lp->rx_ring); |
| 361 | |
| 362 | return 0; |
| 363 | |
| 364 | err_exit: |
| 365 | /* Deallocate all previously allocated skbs */ |
| 366 | r6040_free_rxbufs(dev); |
| 367 | return rc; |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 368 | } |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 369 | |
Florian Fainelli | 90f750a | 2012-04-11 07:18:36 +0000 | [diff] [blame] | 370 | static void r6040_reset_mac(struct r6040_private *lp) |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 371 | { |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 372 | void __iomem *ioaddr = lp->base; |
Florian Fainelli | 2fa15bb | 2012-04-11 07:18:38 +0000 | [diff] [blame] | 373 | int limit = MAC_DEF_TIMEOUT; |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 374 | u16 cmd; |
| 375 | |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 376 | iowrite16(MAC_RST, ioaddr + MCR1); |
| 377 | while (limit--) { |
| 378 | cmd = ioread16(ioaddr + MCR1); |
Florian Fainelli | 58dbc69 | 2012-01-04 08:59:35 +0000 | [diff] [blame] | 379 | if (cmd & MAC_RST) |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 380 | break; |
| 381 | } |
Florian Fainelli | 90f750a | 2012-04-11 07:18:36 +0000 | [diff] [blame] | 382 | |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 383 | /* Reset internal state machine */ |
Florian Fainelli | e147763 | 2012-01-04 08:59:36 +0000 | [diff] [blame] | 384 | iowrite16(MAC_SM_RST, ioaddr + MAC_SM); |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 385 | iowrite16(0, ioaddr + MAC_SM); |
Florian Fainelli | c1d6993 | 2008-09-03 16:50:03 +0200 | [diff] [blame] | 386 | mdelay(5); |
Florian Fainelli | 90f750a | 2012-04-11 07:18:36 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void r6040_init_mac_regs(struct net_device *dev) |
| 390 | { |
| 391 | struct r6040_private *lp = netdev_priv(dev); |
| 392 | void __iomem *ioaddr = lp->base; |
| 393 | |
| 394 | /* Mask Off Interrupt */ |
| 395 | iowrite16(MSK_INT, ioaddr + MIER); |
| 396 | |
| 397 | /* Reset RDC MAC */ |
| 398 | r6040_reset_mac(lp); |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 399 | |
| 400 | /* MAC Bus Control Register */ |
| 401 | iowrite16(MBCR_DEFAULT, ioaddr + MBCR); |
| 402 | |
| 403 | /* Buffer Size Register */ |
| 404 | iowrite16(MAX_BUF_SIZE, ioaddr + MR_BSR); |
| 405 | |
| 406 | /* Write TX ring start address */ |
| 407 | iowrite16(lp->tx_ring_dma, ioaddr + MTD_SA0); |
| 408 | iowrite16(lp->tx_ring_dma >> 16, ioaddr + MTD_SA1); |
| 409 | |
| 410 | /* Write RX ring start address */ |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 411 | iowrite16(lp->rx_ring_dma, ioaddr + MRD_SA0); |
| 412 | iowrite16(lp->rx_ring_dma >> 16, ioaddr + MRD_SA1); |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 413 | |
| 414 | /* Set interrupt waiting time and packet numbers */ |
Florian Fainelli | 31718de | 2008-07-13 14:35:00 +0200 | [diff] [blame] | 415 | iowrite16(0, ioaddr + MT_ICR); |
| 416 | iowrite16(0, ioaddr + MR_ICR); |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 417 | |
| 418 | /* Enable interrupts */ |
| 419 | iowrite16(INT_MASK, ioaddr + MIER); |
| 420 | |
| 421 | /* Enable TX and RX */ |
Florian Fainelli | 4e16d6e | 2012-01-04 08:59:34 +0000 | [diff] [blame] | 422 | iowrite16(lp->mcr0 | MCR0_RCVEN, ioaddr); |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 423 | |
| 424 | /* Let TX poll the descriptors |
| 425 | * we may got called by r6040_tx_timeout which has left |
| 426 | * some unsent tx buffers */ |
Florian Fainelli | 940ff7e | 2012-04-11 07:18:41 +0000 | [diff] [blame] | 427 | iowrite16(TM2TX, ioaddr + MTPR); |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 428 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 429 | |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 430 | static void r6040_tx_timeout(struct net_device *dev) |
| 431 | { |
| 432 | struct r6040_private *priv = netdev_priv(dev); |
| 433 | void __iomem *ioaddr = priv->base; |
| 434 | |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 435 | netdev_warn(dev, "transmit timed out, int enable %4.4x " |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 436 | "status %4.4x\n", |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 437 | ioread16(ioaddr + MIER), |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 438 | ioread16(ioaddr + MISR)); |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 439 | |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 440 | dev->stats.tx_errors++; |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 441 | |
| 442 | /* Reset MAC and re-init all registers */ |
| 443 | r6040_init_mac_regs(dev); |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 446 | static struct net_device_stats *r6040_get_stats(struct net_device *dev) |
| 447 | { |
| 448 | struct r6040_private *priv = netdev_priv(dev); |
| 449 | void __iomem *ioaddr = priv->base; |
| 450 | unsigned long flags; |
| 451 | |
| 452 | spin_lock_irqsave(&priv->lock, flags); |
Florian Fainelli | d248fd7 | 2007-12-12 22:34:55 +0100 | [diff] [blame] | 453 | dev->stats.rx_crc_errors += ioread8(ioaddr + ME_CNT1); |
| 454 | dev->stats.multicast += ioread8(ioaddr + ME_CNT0); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 455 | spin_unlock_irqrestore(&priv->lock, flags); |
| 456 | |
Florian Fainelli | d248fd7 | 2007-12-12 22:34:55 +0100 | [diff] [blame] | 457 | return &dev->stats; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* Stop RDC MAC and Free the allocated resource */ |
| 461 | static void r6040_down(struct net_device *dev) |
| 462 | { |
| 463 | struct r6040_private *lp = netdev_priv(dev); |
| 464 | void __iomem *ioaddr = lp->base; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 465 | u16 *adrp; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 466 | |
| 467 | /* Stop MAC */ |
| 468 | iowrite16(MSK_INT, ioaddr + MIER); /* Mask Off Interrupt */ |
Florian Fainelli | 90f750a | 2012-04-11 07:18:36 +0000 | [diff] [blame] | 469 | |
| 470 | /* Reset RDC MAC */ |
| 471 | r6040_reset_mac(lp); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 472 | |
| 473 | /* Restore MAC Address to MIDx */ |
| 474 | adrp = (u16 *) dev->dev_addr; |
| 475 | iowrite16(adrp[0], ioaddr + MID_0L); |
| 476 | iowrite16(adrp[1], ioaddr + MID_0M); |
| 477 | iowrite16(adrp[2], ioaddr + MID_0H); |
Florian Fainelli | 06e92c3 | 2011-10-06 23:36:22 +0000 | [diff] [blame] | 478 | |
| 479 | phy_stop(lp->phydev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 482 | static int r6040_close(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 483 | { |
| 484 | struct r6040_private *lp = netdev_priv(dev); |
Florian Fainelli | 58854c6 | 2009-01-09 23:19:26 -0800 | [diff] [blame] | 485 | struct pci_dev *pdev = lp->pdev; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 486 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 487 | spin_lock_irq(&lp->lock); |
Florian Fainelli | 129cf9a | 2008-07-13 14:32:45 +0200 | [diff] [blame] | 488 | napi_disable(&lp->napi); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 489 | netif_stop_queue(dev); |
| 490 | r6040_down(dev); |
Florian Fainelli | 58854c6 | 2009-01-09 23:19:26 -0800 | [diff] [blame] | 491 | |
| 492 | free_irq(dev->irq, dev); |
| 493 | |
| 494 | /* Free RX buffer */ |
| 495 | r6040_free_rxbufs(dev); |
| 496 | |
| 497 | /* Free TX buffer */ |
| 498 | r6040_free_txbufs(dev); |
| 499 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 500 | spin_unlock_irq(&lp->lock); |
| 501 | |
Florian Fainelli | 58854c6 | 2009-01-09 23:19:26 -0800 | [diff] [blame] | 502 | /* Free Descriptor memory */ |
| 503 | if (lp->rx_ring) { |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 504 | pci_free_consistent(pdev, |
| 505 | RX_DESC_SIZE, lp->rx_ring, lp->rx_ring_dma); |
Hannes Eder | 5b5103e | 2009-02-14 11:14:04 +0000 | [diff] [blame] | 506 | lp->rx_ring = NULL; |
Florian Fainelli | 58854c6 | 2009-01-09 23:19:26 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | if (lp->tx_ring) { |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 510 | pci_free_consistent(pdev, |
| 511 | TX_DESC_SIZE, lp->tx_ring, lp->tx_ring_dma); |
Hannes Eder | 5b5103e | 2009-02-14 11:14:04 +0000 | [diff] [blame] | 512 | lp->tx_ring = NULL; |
Florian Fainelli | 58854c6 | 2009-01-09 23:19:26 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 518 | static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
| 519 | { |
| 520 | struct r6040_private *lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 521 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 522 | if (!lp->phydev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 523 | return -EINVAL; |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 524 | |
David S. Miller | 4cfa580 | 2010-07-21 21:10:49 -0700 | [diff] [blame] | 525 | return phy_mii_ioctl(lp->phydev, rq, cmd); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static int r6040_rx(struct net_device *dev, int limit) |
| 529 | { |
| 530 | struct r6040_private *priv = netdev_priv(dev); |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 531 | struct r6040_descriptor *descptr = priv->rx_remove_ptr; |
| 532 | struct sk_buff *skb_ptr, *new_skb; |
| 533 | int count = 0; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 534 | u16 err; |
| 535 | |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 536 | /* Limit not reached and the descriptor belongs to the CPU */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 537 | while (count < limit && !(descptr->status & DSC_OWNER_MAC)) { |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 538 | /* Read the descriptor status */ |
| 539 | err = descptr->status; |
| 540 | /* Global error status set */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 541 | if (err & DSC_RX_ERR) { |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 542 | /* RX dribble */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 543 | if (err & DSC_RX_ERR_DRI) |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 544 | dev->stats.rx_frame_errors++; |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 545 | /* Buffer length exceeded */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 546 | if (err & DSC_RX_ERR_BUF) |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 547 | dev->stats.rx_length_errors++; |
| 548 | /* Packet too long */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 549 | if (err & DSC_RX_ERR_LONG) |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 550 | dev->stats.rx_length_errors++; |
| 551 | /* Packet < 64 bytes */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 552 | if (err & DSC_RX_ERR_RUNT) |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 553 | dev->stats.rx_length_errors++; |
| 554 | /* CRC error */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 555 | if (err & DSC_RX_ERR_CRC) { |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 556 | spin_lock(&priv->lock); |
| 557 | dev->stats.rx_crc_errors++; |
| 558 | spin_unlock(&priv->lock); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 559 | } |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 560 | goto next_descr; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 561 | } |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 562 | |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 563 | /* Packet successfully received */ |
| 564 | new_skb = netdev_alloc_skb(dev, MAX_BUF_SIZE); |
| 565 | if (!new_skb) { |
| 566 | dev->stats.rx_dropped++; |
| 567 | goto next_descr; |
| 568 | } |
| 569 | skb_ptr = descptr->skb_ptr; |
| 570 | skb_ptr->dev = priv->dev; |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 571 | |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 572 | /* Do not count the CRC */ |
| 573 | skb_put(skb_ptr, descptr->len - 4); |
| 574 | pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf), |
| 575 | MAX_BUF_SIZE, PCI_DMA_FROMDEVICE); |
| 576 | skb_ptr->protocol = eth_type_trans(skb_ptr, priv->dev); |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 577 | |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 578 | /* Send to upper layer */ |
| 579 | netif_receive_skb(skb_ptr); |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 580 | dev->stats.rx_packets++; |
| 581 | dev->stats.rx_bytes += descptr->len - 4; |
| 582 | |
| 583 | /* put new skb into descriptor */ |
| 584 | descptr->skb_ptr = new_skb; |
| 585 | descptr->buf = cpu_to_le32(pci_map_single(priv->pdev, |
| 586 | descptr->skb_ptr->data, |
| 587 | MAX_BUF_SIZE, PCI_DMA_FROMDEVICE)); |
| 588 | |
| 589 | next_descr: |
| 590 | /* put the descriptor back to the MAC */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 591 | descptr->status = DSC_OWNER_MAC; |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 592 | descptr = descptr->vndescp; |
| 593 | count++; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 594 | } |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 595 | priv->rx_remove_ptr = descptr; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 596 | |
| 597 | return count; |
| 598 | } |
| 599 | |
| 600 | static void r6040_tx(struct net_device *dev) |
| 601 | { |
| 602 | struct r6040_private *priv = netdev_priv(dev); |
| 603 | struct r6040_descriptor *descptr; |
| 604 | void __iomem *ioaddr = priv->base; |
| 605 | struct sk_buff *skb_ptr; |
| 606 | u16 err; |
| 607 | |
| 608 | spin_lock(&priv->lock); |
| 609 | descptr = priv->tx_remove_ptr; |
| 610 | while (priv->tx_free_desc < TX_DCNT) { |
| 611 | /* Check for errors */ |
| 612 | err = ioread16(ioaddr + MLSR); |
| 613 | |
Florian Fainelli | 8dd87a2 | 2012-04-11 07:18:40 +0000 | [diff] [blame] | 614 | if (err & TX_FIFO_UNDR) |
Florian Fainelli | 3440ecc | 2012-04-11 07:18:39 +0000 | [diff] [blame] | 615 | dev->stats.tx_fifo_errors++; |
Florian Fainelli | 8dd87a2 | 2012-04-11 07:18:40 +0000 | [diff] [blame] | 616 | if (err & (TX_EXCEEDC | TX_LATEC)) |
Florian Fainelli | d248fd7 | 2007-12-12 22:34:55 +0100 | [diff] [blame] | 617 | dev->stats.tx_carrier_errors++; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 618 | |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 619 | if (descptr->status & DSC_OWNER_MAC) |
Florian Fainelli | ec6d2d4 | 2007-12-12 23:13:15 +0100 | [diff] [blame] | 620 | break; /* Not complete */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 621 | skb_ptr = descptr->skb_ptr; |
Al Viro | ed773b4 | 2008-03-16 22:43:06 +0000 | [diff] [blame] | 622 | pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf), |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 623 | skb_ptr->len, PCI_DMA_TODEVICE); |
| 624 | /* Free buffer */ |
| 625 | dev_kfree_skb_irq(skb_ptr); |
| 626 | descptr->skb_ptr = NULL; |
| 627 | /* To next descriptor */ |
| 628 | descptr = descptr->vndescp; |
| 629 | priv->tx_free_desc++; |
| 630 | } |
| 631 | priv->tx_remove_ptr = descptr; |
| 632 | |
| 633 | if (priv->tx_free_desc) |
| 634 | netif_wake_queue(dev); |
| 635 | spin_unlock(&priv->lock); |
| 636 | } |
| 637 | |
| 638 | static int r6040_poll(struct napi_struct *napi, int budget) |
| 639 | { |
| 640 | struct r6040_private *priv = |
| 641 | container_of(napi, struct r6040_private, napi); |
| 642 | struct net_device *dev = priv->dev; |
| 643 | void __iomem *ioaddr = priv->base; |
| 644 | int work_done; |
| 645 | |
| 646 | work_done = r6040_rx(dev, budget); |
| 647 | |
| 648 | if (work_done < budget) { |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 649 | napi_complete(napi); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 650 | /* Enable RX interrupt */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 651 | iowrite16(ioread16(ioaddr + MIER) | RX_INTS, ioaddr + MIER); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 652 | } |
| 653 | return work_done; |
| 654 | } |
| 655 | |
| 656 | /* The RDC interrupt handler. */ |
| 657 | static irqreturn_t r6040_interrupt(int irq, void *dev_id) |
| 658 | { |
| 659 | struct net_device *dev = dev_id; |
| 660 | struct r6040_private *lp = netdev_priv(dev); |
| 661 | void __iomem *ioaddr = lp->base; |
Joe Chou | 3e7c469 | 2008-12-22 19:40:02 -0800 | [diff] [blame] | 662 | u16 misr, status; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 663 | |
Joe Chou | 3e7c469 | 2008-12-22 19:40:02 -0800 | [diff] [blame] | 664 | /* Save MIER */ |
| 665 | misr = ioread16(ioaddr + MIER); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 666 | /* Mask off RDC MAC interrupt */ |
| 667 | iowrite16(MSK_INT, ioaddr + MIER); |
| 668 | /* Read MISR status and clear */ |
| 669 | status = ioread16(ioaddr + MISR); |
| 670 | |
Florian Fainelli | 35976d4 | 2009-07-08 03:05:14 +0000 | [diff] [blame] | 671 | if (status == 0x0000 || status == 0xffff) { |
| 672 | /* Restore RDC MAC interrupt */ |
| 673 | iowrite16(misr, ioaddr + MIER); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 674 | return IRQ_NONE; |
Florian Fainelli | 35976d4 | 2009-07-08 03:05:14 +0000 | [diff] [blame] | 675 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 676 | |
| 677 | /* RX interrupt request */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 678 | if (status & RX_INTS) { |
| 679 | if (status & RX_NO_DESC) { |
| 680 | /* RX descriptor unavailable */ |
| 681 | dev->stats.rx_dropped++; |
| 682 | dev->stats.rx_missed_errors++; |
| 683 | } |
| 684 | if (status & RX_FIFO_FULL) |
| 685 | dev->stats.rx_fifo_errors++; |
| 686 | |
Michael Thalmeier | 0d9b6e7 | 2011-07-15 01:28:26 +0000 | [diff] [blame] | 687 | if (likely(napi_schedule_prep(&lp->napi))) { |
| 688 | /* Mask off RX interrupt */ |
| 689 | misr &= ~RX_INTS; |
| 690 | __napi_schedule(&lp->napi); |
| 691 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* TX interrupt request */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 695 | if (status & TX_INTS) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 696 | r6040_tx(dev); |
| 697 | |
Joe Chou | 3e7c469 | 2008-12-22 19:40:02 -0800 | [diff] [blame] | 698 | /* Restore RDC MAC interrupt */ |
| 699 | iowrite16(misr, ioaddr + MIER); |
| 700 | |
Florian Fainelli | ec6d2d4 | 2007-12-12 23:13:15 +0100 | [diff] [blame] | 701 | return IRQ_HANDLED; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 705 | static void r6040_poll_controller(struct net_device *dev) |
| 706 | { |
| 707 | disable_irq(dev->irq); |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 708 | r6040_interrupt(dev->irq, dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 709 | enable_irq(dev->irq); |
| 710 | } |
| 711 | #endif |
| 712 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 713 | /* Init RDC MAC */ |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 714 | static int r6040_up(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 715 | { |
| 716 | struct r6040_private *lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 717 | void __iomem *ioaddr = lp->base; |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 718 | int ret; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 719 | |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 720 | /* Initialise and alloc RX/TX buffers */ |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 721 | r6040_init_txbufs(dev); |
| 722 | ret = r6040_alloc_rxbufs(dev); |
| 723 | if (ret) |
| 724 | return ret; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 725 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 726 | /* improve performance (by RDC guys) */ |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 727 | r6040_phy_write(ioaddr, 30, 17, |
| 728 | (r6040_phy_read(ioaddr, 30, 17) | 0x4000)); |
| 729 | r6040_phy_write(ioaddr, 30, 17, |
| 730 | ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000)); |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 731 | r6040_phy_write(ioaddr, 0, 19, 0x0000); |
| 732 | r6040_phy_write(ioaddr, 0, 30, 0x01F0); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 733 | |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 734 | /* Initialize all MAC registers */ |
| 735 | r6040_init_mac_regs(dev); |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 736 | |
Florian Fainelli | 06e92c3 | 2011-10-06 23:36:22 +0000 | [diff] [blame] | 737 | phy_start(lp->phydev); |
| 738 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 739 | return 0; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 742 | |
| 743 | /* Read/set MAC address routines */ |
| 744 | static void r6040_mac_address(struct net_device *dev) |
| 745 | { |
| 746 | struct r6040_private *lp = netdev_priv(dev); |
| 747 | void __iomem *ioaddr = lp->base; |
| 748 | u16 *adrp; |
| 749 | |
Florian Fainelli | 4852968 | 2012-01-04 08:59:38 +0000 | [diff] [blame] | 750 | /* Reset MAC */ |
Florian Fainelli | 90f750a | 2012-04-11 07:18:36 +0000 | [diff] [blame] | 751 | r6040_reset_mac(lp); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 752 | |
| 753 | /* Restore MAC Address */ |
| 754 | adrp = (u16 *) dev->dev_addr; |
| 755 | iowrite16(adrp[0], ioaddr + MID_0L); |
| 756 | iowrite16(adrp[1], ioaddr + MID_0M); |
| 757 | iowrite16(adrp[2], ioaddr + MID_0H); |
Otavio Salvador | 42099d7 | 2010-09-26 19:58:07 -0700 | [diff] [blame] | 758 | |
| 759 | /* Store MAC Address in perm_addr */ |
| 760 | memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 763 | static int r6040_open(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 764 | { |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 765 | struct r6040_private *lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 766 | int ret; |
| 767 | |
| 768 | /* Request IRQ and Register interrupt handler */ |
Julia Lawall | 91dcbf3 | 2009-11-18 08:23:00 +0000 | [diff] [blame] | 769 | ret = request_irq(dev->irq, r6040_interrupt, |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 770 | IRQF_SHARED, dev->name, dev); |
| 771 | if (ret) |
Denis Kirjanov | ced1de4 | 2010-08-24 23:57:55 +0000 | [diff] [blame] | 772 | goto out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 773 | |
| 774 | /* Set MAC address */ |
| 775 | r6040_mac_address(dev); |
| 776 | |
| 777 | /* Allocate Descriptor memory */ |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 778 | lp->rx_ring = |
| 779 | pci_alloc_consistent(lp->pdev, RX_DESC_SIZE, &lp->rx_ring_dma); |
Denis Kirjanov | ced1de4 | 2010-08-24 23:57:55 +0000 | [diff] [blame] | 780 | if (!lp->rx_ring) { |
| 781 | ret = -ENOMEM; |
| 782 | goto err_free_irq; |
| 783 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 784 | |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 785 | lp->tx_ring = |
| 786 | pci_alloc_consistent(lp->pdev, TX_DESC_SIZE, &lp->tx_ring_dma); |
| 787 | if (!lp->tx_ring) { |
Denis Kirjanov | ced1de4 | 2010-08-24 23:57:55 +0000 | [diff] [blame] | 788 | ret = -ENOMEM; |
| 789 | goto err_free_rx_ring; |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 792 | ret = r6040_up(dev); |
Denis Kirjanov | ced1de4 | 2010-08-24 23:57:55 +0000 | [diff] [blame] | 793 | if (ret) |
| 794 | goto err_free_tx_ring; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 795 | |
| 796 | napi_enable(&lp->napi); |
| 797 | netif_start_queue(dev); |
| 798 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 799 | return 0; |
Denis Kirjanov | ced1de4 | 2010-08-24 23:57:55 +0000 | [diff] [blame] | 800 | |
| 801 | err_free_tx_ring: |
| 802 | pci_free_consistent(lp->pdev, TX_DESC_SIZE, lp->tx_ring, |
| 803 | lp->tx_ring_dma); |
| 804 | err_free_rx_ring: |
| 805 | pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring, |
| 806 | lp->rx_ring_dma); |
| 807 | err_free_irq: |
| 808 | free_irq(dev->irq, dev); |
| 809 | out: |
| 810 | return ret; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 813 | static netdev_tx_t r6040_start_xmit(struct sk_buff *skb, |
| 814 | struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 815 | { |
| 816 | struct r6040_private *lp = netdev_priv(dev); |
| 817 | struct r6040_descriptor *descptr; |
| 818 | void __iomem *ioaddr = lp->base; |
| 819 | unsigned long flags; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 820 | |
| 821 | /* Critical Section */ |
| 822 | spin_lock_irqsave(&lp->lock, flags); |
| 823 | |
| 824 | /* TX resource check */ |
| 825 | if (!lp->tx_free_desc) { |
| 826 | spin_unlock_irqrestore(&lp->lock, flags); |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 827 | netif_stop_queue(dev); |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 828 | netdev_err(dev, ": no tx descriptor\n"); |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 829 | return NETDEV_TX_BUSY; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /* Statistic Counter */ |
| 833 | dev->stats.tx_packets++; |
| 834 | dev->stats.tx_bytes += skb->len; |
| 835 | /* Set TX descriptor & Transmit it */ |
| 836 | lp->tx_free_desc--; |
| 837 | descptr = lp->tx_insert_ptr; |
| 838 | if (skb->len < MISR) |
| 839 | descptr->len = MISR; |
| 840 | else |
| 841 | descptr->len = skb->len; |
| 842 | |
| 843 | descptr->skb_ptr = skb; |
| 844 | descptr->buf = cpu_to_le32(pci_map_single(lp->pdev, |
| 845 | skb->data, skb->len, PCI_DMA_TODEVICE)); |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 846 | descptr->status = DSC_OWNER_MAC; |
Richard Cochran | 2aa8f4c | 2011-06-19 03:31:42 +0000 | [diff] [blame] | 847 | |
| 848 | skb_tx_timestamp(skb); |
| 849 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 850 | /* Trigger the MAC to check the TX descriptor */ |
Florian Fainelli | 940ff7e | 2012-04-11 07:18:41 +0000 | [diff] [blame] | 851 | iowrite16(TM2TX, ioaddr + MTPR); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 852 | lp->tx_insert_ptr = descptr->vndescp; |
| 853 | |
| 854 | /* If no tx resource, stop */ |
| 855 | if (!lp->tx_free_desc) |
| 856 | netif_stop_queue(dev); |
| 857 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 858 | spin_unlock_irqrestore(&lp->lock, flags); |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 859 | |
| 860 | return NETDEV_TX_OK; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 863 | static void r6040_multicast_list(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 864 | { |
| 865 | struct r6040_private *lp = netdev_priv(dev); |
| 866 | void __iomem *ioaddr = lp->base; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 867 | unsigned long flags; |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 868 | struct netdev_hw_addr *ha; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 869 | int i; |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 870 | u16 *adrp; |
| 871 | u16 hash_table[4] = { 0 }; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 872 | |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 873 | spin_lock_irqsave(&lp->lock, flags); |
| 874 | |
| 875 | /* Keep our MAC Address */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 876 | adrp = (u16 *)dev->dev_addr; |
| 877 | iowrite16(adrp[0], ioaddr + MID_0L); |
| 878 | iowrite16(adrp[1], ioaddr + MID_0M); |
| 879 | iowrite16(adrp[2], ioaddr + MID_0H); |
| 880 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 881 | /* Clear AMCP & PROM bits */ |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 882 | lp->mcr0 = ioread16(ioaddr + MCR0) & ~(MCR0_PROMISC | MCR0_HASH_EN); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 883 | |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 884 | /* Promiscuous mode */ |
| 885 | if (dev->flags & IFF_PROMISC) |
| 886 | lp->mcr0 |= MCR0_PROMISC; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 887 | |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 888 | /* Enable multicast hash table function to |
| 889 | * receive all multicast packets. */ |
| 890 | else if (dev->flags & IFF_ALLMULTI) { |
| 891 | lp->mcr0 |= MCR0_HASH_EN; |
| 892 | |
| 893 | for (i = 0; i < MCAST_MAX ; i++) { |
| 894 | iowrite16(0, ioaddr + MID_1L + 8 * i); |
| 895 | iowrite16(0, ioaddr + MID_1M + 8 * i); |
| 896 | iowrite16(0, ioaddr + MID_1H + 8 * i); |
| 897 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 898 | |
| 899 | for (i = 0; i < 4; i++) |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 900 | hash_table[i] = 0xffff; |
| 901 | } |
| 902 | /* Use internal multicast address registers if the number of |
| 903 | * multicast addresses is not greater than MCAST_MAX. */ |
| 904 | else if (netdev_mc_count(dev) <= MCAST_MAX) { |
| 905 | i = 0; |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 906 | netdev_for_each_mc_addr(ha, dev) { |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 907 | u16 *adrp = (u16 *) ha->addr; |
| 908 | iowrite16(adrp[0], ioaddr + MID_1L + 8 * i); |
| 909 | iowrite16(adrp[1], ioaddr + MID_1M + 8 * i); |
| 910 | iowrite16(adrp[2], ioaddr + MID_1H + 8 * i); |
| 911 | i++; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 912 | } |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 913 | while (i < MCAST_MAX) { |
| 914 | iowrite16(0, ioaddr + MID_1L + 8 * i); |
| 915 | iowrite16(0, ioaddr + MID_1M + 8 * i); |
| 916 | iowrite16(0, ioaddr + MID_1H + 8 * i); |
| 917 | i++; |
| 918 | } |
| 919 | } |
| 920 | /* Otherwise, Enable multicast hash table function. */ |
| 921 | else { |
| 922 | u32 crc; |
| 923 | |
| 924 | lp->mcr0 |= MCR0_HASH_EN; |
| 925 | |
| 926 | for (i = 0; i < MCAST_MAX ; i++) { |
| 927 | iowrite16(0, ioaddr + MID_1L + 8 * i); |
| 928 | iowrite16(0, ioaddr + MID_1M + 8 * i); |
| 929 | iowrite16(0, ioaddr + MID_1H + 8 * i); |
| 930 | } |
| 931 | |
| 932 | /* Build multicast hash table */ |
| 933 | netdev_for_each_mc_addr(ha, dev) { |
| 934 | u8 *addrs = ha->addr; |
| 935 | |
| 936 | crc = ether_crc(ETH_ALEN, addrs); |
| 937 | crc >>= 26; |
| 938 | hash_table[crc >> 4] |= 1 << (crc & 0xf); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | iowrite16(lp->mcr0, ioaddr + MCR0); |
| 943 | |
| 944 | /* Fill the MAC hash tables with their values */ |
Florian Fainelli | bbc13ab | 2011-11-16 06:00:08 +0000 | [diff] [blame] | 945 | if (lp->mcr0 & MCR0_HASH_EN) { |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 946 | iowrite16(hash_table[0], ioaddr + MAR0); |
| 947 | iowrite16(hash_table[1], ioaddr + MAR1); |
| 948 | iowrite16(hash_table[2], ioaddr + MAR2); |
| 949 | iowrite16(hash_table[3], ioaddr + MAR3); |
| 950 | } |
Shawn Lin | c60c9c7 | 2011-03-07 00:09:40 +0000 | [diff] [blame] | 951 | |
| 952 | spin_unlock_irqrestore(&lp->lock, flags); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | static void netdev_get_drvinfo(struct net_device *dev, |
| 956 | struct ethtool_drvinfo *info) |
| 957 | { |
| 958 | struct r6040_private *rp = netdev_priv(dev); |
| 959 | |
| 960 | strcpy(info->driver, DRV_NAME); |
| 961 | strcpy(info->version, DRV_VERSION); |
| 962 | strcpy(info->bus_info, pci_name(rp->pdev)); |
| 963 | } |
| 964 | |
| 965 | static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 966 | { |
| 967 | struct r6040_private *rp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 968 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 969 | return phy_ethtool_gset(rp->phydev, cmd); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 973 | { |
| 974 | struct r6040_private *rp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 975 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 976 | return phy_ethtool_sset(rp->phydev, cmd); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 977 | } |
| 978 | |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame] | 979 | static const struct ethtool_ops netdev_ethtool_ops = { |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 980 | .get_drvinfo = netdev_get_drvinfo, |
| 981 | .get_settings = netdev_get_settings, |
| 982 | .set_settings = netdev_set_settings, |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 983 | .get_link = ethtool_op_get_link, |
Richard Cochran | d88e102 | 2012-04-03 22:59:34 +0000 | [diff] [blame] | 984 | .get_ts_info = ethtool_op_get_ts_info, |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 985 | }; |
| 986 | |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame] | 987 | static const struct net_device_ops r6040_netdev_ops = { |
| 988 | .ndo_open = r6040_open, |
| 989 | .ndo_stop = r6040_close, |
| 990 | .ndo_start_xmit = r6040_start_xmit, |
| 991 | .ndo_get_stats = r6040_get_stats, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 992 | .ndo_set_rx_mode = r6040_multicast_list, |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame] | 993 | .ndo_change_mtu = eth_change_mtu, |
| 994 | .ndo_validate_addr = eth_validate_addr, |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 995 | .ndo_set_mac_address = eth_mac_addr, |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame] | 996 | .ndo_do_ioctl = r6040_ioctl, |
| 997 | .ndo_tx_timeout = r6040_tx_timeout, |
| 998 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 999 | .ndo_poll_controller = r6040_poll_controller, |
| 1000 | #endif |
| 1001 | }; |
| 1002 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1003 | static void r6040_adjust_link(struct net_device *dev) |
| 1004 | { |
| 1005 | struct r6040_private *lp = netdev_priv(dev); |
| 1006 | struct phy_device *phydev = lp->phydev; |
| 1007 | int status_changed = 0; |
| 1008 | void __iomem *ioaddr = lp->base; |
| 1009 | |
| 1010 | BUG_ON(!phydev); |
| 1011 | |
| 1012 | if (lp->old_link != phydev->link) { |
| 1013 | status_changed = 1; |
| 1014 | lp->old_link = phydev->link; |
| 1015 | } |
| 1016 | |
| 1017 | /* reflect duplex change */ |
| 1018 | if (phydev->link && (lp->old_duplex != phydev->duplex)) { |
Florian Fainelli | 4e16d6e | 2012-01-04 08:59:34 +0000 | [diff] [blame] | 1019 | lp->mcr0 |= (phydev->duplex == DUPLEX_FULL ? MCR0_FD : 0); |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1020 | iowrite16(lp->mcr0, ioaddr); |
| 1021 | |
| 1022 | status_changed = 1; |
| 1023 | lp->old_duplex = phydev->duplex; |
| 1024 | } |
| 1025 | |
| 1026 | if (status_changed) { |
| 1027 | pr_info("%s: link %s", dev->name, phydev->link ? |
| 1028 | "UP" : "DOWN"); |
| 1029 | if (phydev->link) |
| 1030 | pr_cont(" - %d/%s", phydev->speed, |
| 1031 | DUPLEX_FULL == phydev->duplex ? "full" : "half"); |
| 1032 | pr_cont("\n"); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | static int r6040_mii_probe(struct net_device *dev) |
| 1037 | { |
| 1038 | struct r6040_private *lp = netdev_priv(dev); |
| 1039 | struct phy_device *phydev = NULL; |
| 1040 | |
| 1041 | phydev = phy_find_first(lp->mii_bus); |
| 1042 | if (!phydev) { |
| 1043 | dev_err(&lp->pdev->dev, "no PHY found\n"); |
| 1044 | return -ENODEV; |
| 1045 | } |
| 1046 | |
| 1047 | phydev = phy_connect(dev, dev_name(&phydev->dev), &r6040_adjust_link, |
| 1048 | 0, PHY_INTERFACE_MODE_MII); |
| 1049 | |
| 1050 | if (IS_ERR(phydev)) { |
| 1051 | dev_err(&lp->pdev->dev, "could not attach to PHY\n"); |
| 1052 | return PTR_ERR(phydev); |
| 1053 | } |
| 1054 | |
| 1055 | /* mask with MAC supported features */ |
| 1056 | phydev->supported &= (SUPPORTED_10baseT_Half |
| 1057 | | SUPPORTED_10baseT_Full |
| 1058 | | SUPPORTED_100baseT_Half |
| 1059 | | SUPPORTED_100baseT_Full |
| 1060 | | SUPPORTED_Autoneg |
| 1061 | | SUPPORTED_MII |
| 1062 | | SUPPORTED_TP); |
| 1063 | |
| 1064 | phydev->advertising = phydev->supported; |
| 1065 | lp->phydev = phydev; |
| 1066 | lp->old_link = 0; |
| 1067 | lp->old_duplex = -1; |
| 1068 | |
| 1069 | dev_info(&lp->pdev->dev, "attached PHY driver [%s] " |
| 1070 | "(mii_bus:phy_addr=%s)\n", |
| 1071 | phydev->drv->name, dev_name(&phydev->dev)); |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1076 | static int __devinit r6040_init_one(struct pci_dev *pdev, |
| 1077 | const struct pci_device_id *ent) |
| 1078 | { |
| 1079 | struct net_device *dev; |
| 1080 | struct r6040_private *lp; |
| 1081 | void __iomem *ioaddr; |
| 1082 | int err, io_size = R6040_IO_SIZE; |
| 1083 | static int card_idx = -1; |
| 1084 | int bar = 0; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1085 | u16 *adrp; |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1086 | int i; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1087 | |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 1088 | pr_info("%s\n", version); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1089 | |
| 1090 | err = pci_enable_device(pdev); |
| 1091 | if (err) |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1092 | goto err_out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1093 | |
| 1094 | /* this should always be supported */ |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 1095 | err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1096 | if (err) { |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 1097 | dev_err(&pdev->dev, "32-bit PCI DMA addresses" |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1098 | "not supported by the card\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1099 | goto err_out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1100 | } |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 1101 | err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1102 | if (err) { |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 1103 | dev_err(&pdev->dev, "32-bit PCI DMA addresses" |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 1104 | "not supported by the card\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1105 | goto err_out; |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 1106 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1107 | |
| 1108 | /* IO Size check */ |
Michael Opdenacker | 6f5bec1 | 2009-06-24 21:05:09 +0000 | [diff] [blame] | 1109 | if (pci_resource_len(pdev, bar) < io_size) { |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 1110 | dev_err(&pdev->dev, "Insufficient PCI resources, aborting\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1111 | err = -EIO; |
| 1112 | goto err_out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1115 | pci_set_master(pdev); |
| 1116 | |
| 1117 | dev = alloc_etherdev(sizeof(struct r6040_private)); |
| 1118 | if (!dev) { |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1119 | err = -ENOMEM; |
| 1120 | goto err_out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1121 | } |
| 1122 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 1123 | lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1124 | |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1125 | err = pci_request_regions(pdev, DRV_NAME); |
| 1126 | |
| 1127 | if (err) { |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 1128 | dev_err(&pdev->dev, "Failed to request PCI regions\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1129 | goto err_out_free_dev; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | ioaddr = pci_iomap(pdev, bar, io_size); |
| 1133 | if (!ioaddr) { |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 1134 | dev_err(&pdev->dev, "ioremap failed for device\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1135 | err = -EIO; |
| 1136 | goto err_out_free_res; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1137 | } |
Florian Fainelli | 31171ae | 2012-04-11 07:18:42 +0000 | [diff] [blame] | 1138 | |
Florian Fainelli | 84314bf | 2009-01-08 11:01:58 -0800 | [diff] [blame] | 1139 | /* If PHY status change register is still set to zero it means the |
Florian Fainelli | 31171ae | 2012-04-11 07:18:42 +0000 | [diff] [blame] | 1140 | * bootloader didn't initialize it, so we set it to: |
| 1141 | * - enable phy status change |
| 1142 | * - enable all phy addresses |
| 1143 | * - set to lowest timer divider */ |
Florian Fainelli | 84314bf | 2009-01-08 11:01:58 -0800 | [diff] [blame] | 1144 | if (ioread16(ioaddr + PHY_CC) == 0) |
Florian Fainelli | 31171ae | 2012-04-11 07:18:42 +0000 | [diff] [blame] | 1145 | iowrite16(SCEN | PHY_MAX_ADDR << PHYAD_SHIFT | |
| 1146 | 7 << TMRDIV_SHIFT, ioaddr + PHY_CC); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1147 | |
| 1148 | /* Init system & device */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1149 | lp->base = ioaddr; |
| 1150 | dev->irq = pdev->irq; |
| 1151 | |
| 1152 | spin_lock_init(&lp->lock); |
| 1153 | pci_set_drvdata(pdev, dev); |
| 1154 | |
| 1155 | /* Set MAC address */ |
| 1156 | card_idx++; |
| 1157 | |
| 1158 | adrp = (u16 *)dev->dev_addr; |
| 1159 | adrp[0] = ioread16(ioaddr + MID_0L); |
| 1160 | adrp[1] = ioread16(ioaddr + MID_0M); |
| 1161 | adrp[2] = ioread16(ioaddr + MID_0H); |
| 1162 | |
Florian Fainelli | 1d2b1a7 | 2009-01-08 11:02:30 -0800 | [diff] [blame] | 1163 | /* Some bootloader/BIOSes do not initialize |
| 1164 | * MAC address, warn about that */ |
Florian Fainelli | 9f11361 | 2009-01-08 15:04:45 +0000 | [diff] [blame] | 1165 | if (!(adrp[0] || adrp[1] || adrp[2])) { |
Florian Fainelli | 2154c704 | 2010-08-08 10:08:44 +0000 | [diff] [blame] | 1166 | netdev_warn(dev, "MAC address not initialized, " |
| 1167 | "generating random\n"); |
Danny Kukawka | f2cedb6 | 2012-02-15 06:45:39 +0000 | [diff] [blame] | 1168 | eth_hw_addr_random(dev); |
Florian Fainelli | 9f11361 | 2009-01-08 15:04:45 +0000 | [diff] [blame] | 1169 | } |
Florian Fainelli | 1d2b1a7 | 2009-01-08 11:02:30 -0800 | [diff] [blame] | 1170 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1171 | /* Link new device into r6040_root_dev */ |
| 1172 | lp->pdev = pdev; |
Florian Fainelli | 129cf9a | 2008-07-13 14:32:45 +0200 | [diff] [blame] | 1173 | lp->dev = dev; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1174 | |
| 1175 | /* Init RDC private data */ |
Cesar Eduardo Barros | 77e1e43 | 2012-01-07 05:13:17 +0000 | [diff] [blame] | 1176 | lp->mcr0 = MCR0_XMTEN | MCR0_RCVEN; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1177 | |
| 1178 | /* The RDC-specific entries in the device structure. */ |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame] | 1179 | dev->netdev_ops = &r6040_netdev_ops; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1180 | dev->ethtool_ops = &netdev_ethtool_ops; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1181 | dev->watchdog_timeo = TX_TIMEOUT; |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame] | 1182 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1183 | netif_napi_add(dev, &lp->napi, r6040_poll, 64); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1184 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1185 | lp->mii_bus = mdiobus_alloc(); |
| 1186 | if (!lp->mii_bus) { |
| 1187 | dev_err(&pdev->dev, "mdiobus_alloc() failed\n"); |
Axel Lin | 9c86c0f | 2011-01-04 22:40:04 +0000 | [diff] [blame] | 1188 | err = -ENOMEM; |
Mark Kelly | e03f614 | 2009-08-20 01:26:20 +0000 | [diff] [blame] | 1189 | goto err_out_unmap; |
| 1190 | } |
| 1191 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1192 | lp->mii_bus->priv = dev; |
| 1193 | lp->mii_bus->read = r6040_mdiobus_read; |
| 1194 | lp->mii_bus->write = r6040_mdiobus_write; |
| 1195 | lp->mii_bus->reset = r6040_mdiobus_reset; |
| 1196 | lp->mii_bus->name = "r6040_eth_mii"; |
Florian Fainelli | 817380e | 2012-01-04 08:50:40 +0000 | [diff] [blame] | 1197 | snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
| 1198 | dev_name(&pdev->dev), card_idx); |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1199 | lp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); |
| 1200 | if (!lp->mii_bus->irq) { |
| 1201 | dev_err(&pdev->dev, "mii_bus irq allocation failed\n"); |
Axel Lin | 9c86c0f | 2011-01-04 22:40:04 +0000 | [diff] [blame] | 1202 | err = -ENOMEM; |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1203 | goto err_out_mdio; |
| 1204 | } |
| 1205 | |
| 1206 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 1207 | lp->mii_bus->irq[i] = PHY_POLL; |
| 1208 | |
| 1209 | err = mdiobus_register(lp->mii_bus); |
| 1210 | if (err) { |
| 1211 | dev_err(&pdev->dev, "failed to register MII bus\n"); |
| 1212 | goto err_out_mdio_irq; |
| 1213 | } |
| 1214 | |
| 1215 | err = r6040_mii_probe(dev); |
| 1216 | if (err) { |
| 1217 | dev_err(&pdev->dev, "failed to probe MII bus\n"); |
| 1218 | goto err_out_mdio_unregister; |
| 1219 | } |
| 1220 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1221 | /* Register net device. After this dev->name assign */ |
| 1222 | err = register_netdev(dev); |
| 1223 | if (err) { |
Florian Fainelli | 7d53b80 | 2010-04-07 21:39:27 +0000 | [diff] [blame] | 1224 | dev_err(&pdev->dev, "Failed to register net device\n"); |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1225 | goto err_out_mdio_unregister; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1226 | } |
| 1227 | return 0; |
| 1228 | |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1229 | err_out_mdio_unregister: |
| 1230 | mdiobus_unregister(lp->mii_bus); |
| 1231 | err_out_mdio_irq: |
| 1232 | kfree(lp->mii_bus->irq); |
| 1233 | err_out_mdio: |
| 1234 | mdiobus_free(lp->mii_bus); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1235 | err_out_unmap: |
| 1236 | pci_iounmap(pdev, ioaddr); |
| 1237 | err_out_free_res: |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1238 | pci_release_regions(pdev); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1239 | err_out_free_dev: |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1240 | free_netdev(dev); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1241 | err_out: |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1242 | return err; |
| 1243 | } |
| 1244 | |
| 1245 | static void __devexit r6040_remove_one(struct pci_dev *pdev) |
| 1246 | { |
| 1247 | struct net_device *dev = pci_get_drvdata(pdev); |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1248 | struct r6040_private *lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1249 | |
| 1250 | unregister_netdev(dev); |
Florian Fainelli | 3831861 | 2010-05-31 09:18:57 +0000 | [diff] [blame] | 1251 | mdiobus_unregister(lp->mii_bus); |
| 1252 | kfree(lp->mii_bus->irq); |
| 1253 | mdiobus_free(lp->mii_bus); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1254 | pci_release_regions(pdev); |
| 1255 | free_netdev(dev); |
| 1256 | pci_disable_device(pdev); |
| 1257 | pci_set_drvdata(pdev, NULL); |
| 1258 | } |
| 1259 | |
| 1260 | |
Alexey Dobriyan | a3aa188 | 2010-01-07 11:58:11 +0000 | [diff] [blame] | 1261 | static DEFINE_PCI_DEVICE_TABLE(r6040_pci_tbl) = { |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 1262 | { PCI_DEVICE(PCI_VENDOR_ID_RDC, 0x6040) }, |
| 1263 | { 0 } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1264 | }; |
| 1265 | MODULE_DEVICE_TABLE(pci, r6040_pci_tbl); |
| 1266 | |
| 1267 | static struct pci_driver r6040_driver = { |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 1268 | .name = DRV_NAME, |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1269 | .id_table = r6040_pci_tbl, |
| 1270 | .probe = r6040_init_one, |
| 1271 | .remove = __devexit_p(r6040_remove_one), |
| 1272 | }; |
| 1273 | |
| 1274 | |
| 1275 | static int __init r6040_init(void) |
| 1276 | { |
| 1277 | return pci_register_driver(&r6040_driver); |
| 1278 | } |
| 1279 | |
| 1280 | |
| 1281 | static void __exit r6040_cleanup(void) |
| 1282 | { |
| 1283 | pci_unregister_driver(&r6040_driver); |
| 1284 | } |
| 1285 | |
| 1286 | module_init(r6040_init); |
| 1287 | module_exit(r6040_cleanup); |