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> |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 7 | * Florian Fainelli <florian@openwrt.org> |
| 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> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/pci.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/etherdevice.h> |
| 37 | #include <linux/skbuff.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/delay.h> |
| 40 | #include <linux/mii.h> |
| 41 | #include <linux/ethtool.h> |
| 42 | #include <linux/crc32.h> |
| 43 | #include <linux/spinlock.h> |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 44 | #include <linux/bitops.h> |
| 45 | #include <linux/io.h> |
| 46 | #include <linux/irq.h> |
| 47 | #include <linux/uaccess.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 | 1de9529 | 2008-07-13 14:36:04 +0200 | [diff] [blame] | 52 | #define DRV_VERSION "0.18" |
| 53 | #define DRV_RELDATE "13Jul2008" |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 54 | |
| 55 | /* PHY CHIP Address */ |
| 56 | #define PHY1_ADDR 1 /* For MAC1 */ |
| 57 | #define PHY2_ADDR 2 /* For MAC2 */ |
| 58 | #define PHY_MODE 0x3100 /* PHY CHIP Register 0 */ |
| 59 | #define PHY_CAP 0x01E1 /* PHY CHIP Register 4 */ |
| 60 | |
| 61 | /* Time in jiffies before concluding the transmitter is hung. */ |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 62 | #define TX_TIMEOUT (6000 * HZ / 1000) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 63 | |
| 64 | /* RDC MAC I/O Size */ |
| 65 | #define R6040_IO_SIZE 256 |
| 66 | |
| 67 | /* MAX RDC MAC */ |
| 68 | #define MAX_MAC 2 |
| 69 | |
| 70 | /* MAC registers */ |
| 71 | #define MCR0 0x00 /* Control register 0 */ |
| 72 | #define MCR1 0x04 /* Control register 1 */ |
| 73 | #define MAC_RST 0x0001 /* Reset the MAC */ |
| 74 | #define MBCR 0x08 /* Bus control */ |
| 75 | #define MT_ICR 0x0C /* TX interrupt control */ |
| 76 | #define MR_ICR 0x10 /* RX interrupt control */ |
| 77 | #define MTPR 0x14 /* TX poll command register */ |
| 78 | #define MR_BSR 0x18 /* RX buffer size */ |
| 79 | #define MR_DCR 0x1A /* RX descriptor control */ |
| 80 | #define MLSR 0x1C /* Last status */ |
| 81 | #define MMDIO 0x20 /* MDIO control register */ |
| 82 | #define MDIO_WRITE 0x4000 /* MDIO write */ |
| 83 | #define MDIO_READ 0x2000 /* MDIO read */ |
| 84 | #define MMRD 0x24 /* MDIO read data register */ |
| 85 | #define MMWD 0x28 /* MDIO write data register */ |
| 86 | #define MTD_SA0 0x2C /* TX descriptor start address 0 */ |
| 87 | #define MTD_SA1 0x30 /* TX descriptor start address 1 */ |
| 88 | #define MRD_SA0 0x34 /* RX descriptor start address 0 */ |
| 89 | #define MRD_SA1 0x38 /* RX descriptor start address 1 */ |
| 90 | #define MISR 0x3C /* Status register */ |
| 91 | #define MIER 0x40 /* INT enable register */ |
| 92 | #define MSK_INT 0x0000 /* Mask off interrupts */ |
Florian Fainelli | 3d25434 | 2008-07-13 14:28:27 +0200 | [diff] [blame] | 93 | #define RX_FINISH 0x0001 /* RX finished */ |
| 94 | #define RX_NO_DESC 0x0002 /* No RX descriptor available */ |
| 95 | #define RX_FIFO_FULL 0x0004 /* RX FIFO full */ |
| 96 | #define RX_EARLY 0x0008 /* RX early */ |
| 97 | #define TX_FINISH 0x0010 /* TX finished */ |
| 98 | #define TX_EARLY 0x0080 /* TX early */ |
| 99 | #define EVENT_OVRFL 0x0100 /* Event counter overflow */ |
| 100 | #define LINK_CHANGED 0x0200 /* PHY link changed */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 101 | #define ME_CISR 0x44 /* Event counter INT status */ |
| 102 | #define ME_CIER 0x48 /* Event counter INT enable */ |
| 103 | #define MR_CNT 0x50 /* Successfully received packet counter */ |
| 104 | #define ME_CNT0 0x52 /* Event counter 0 */ |
| 105 | #define ME_CNT1 0x54 /* Event counter 1 */ |
| 106 | #define ME_CNT2 0x56 /* Event counter 2 */ |
| 107 | #define ME_CNT3 0x58 /* Event counter 3 */ |
| 108 | #define MT_CNT 0x5A /* Successfully transmit packet counter */ |
| 109 | #define ME_CNT4 0x5C /* Event counter 4 */ |
| 110 | #define MP_CNT 0x5E /* Pause frame counter register */ |
| 111 | #define MAR0 0x60 /* Hash table 0 */ |
| 112 | #define MAR1 0x62 /* Hash table 1 */ |
| 113 | #define MAR2 0x64 /* Hash table 2 */ |
| 114 | #define MAR3 0x66 /* Hash table 3 */ |
| 115 | #define MID_0L 0x68 /* Multicast address MID0 Low */ |
| 116 | #define MID_0M 0x6A /* Multicast address MID0 Medium */ |
| 117 | #define MID_0H 0x6C /* Multicast address MID0 High */ |
| 118 | #define MID_1L 0x70 /* MID1 Low */ |
| 119 | #define MID_1M 0x72 /* MID1 Medium */ |
| 120 | #define MID_1H 0x74 /* MID1 High */ |
| 121 | #define MID_2L 0x78 /* MID2 Low */ |
| 122 | #define MID_2M 0x7A /* MID2 Medium */ |
| 123 | #define MID_2H 0x7C /* MID2 High */ |
| 124 | #define MID_3L 0x80 /* MID3 Low */ |
| 125 | #define MID_3M 0x82 /* MID3 Medium */ |
| 126 | #define MID_3H 0x84 /* MID3 High */ |
| 127 | #define PHY_CC 0x88 /* PHY status change configuration register */ |
| 128 | #define PHY_ST 0x8A /* PHY status register */ |
| 129 | #define MAC_SM 0xAC /* MAC status machine */ |
| 130 | #define MAC_ID 0xBE /* Identifier register */ |
| 131 | |
| 132 | #define TX_DCNT 0x80 /* TX descriptor count */ |
| 133 | #define RX_DCNT 0x80 /* RX descriptor count */ |
| 134 | #define MAX_BUF_SIZE 0x600 |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 135 | #define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor)) |
| 136 | #define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor)) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 137 | #define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */ |
| 138 | #define MCAST_MAX 4 /* Max number multicast addresses to filter */ |
| 139 | |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 140 | /* Descriptor status */ |
| 141 | #define DSC_OWNER_MAC 0x8000 /* MAC is the owner of this descriptor */ |
| 142 | #define DSC_RX_OK 0x4000 /* RX was successful */ |
| 143 | #define DSC_RX_ERR 0x0800 /* RX PHY error */ |
| 144 | #define DSC_RX_ERR_DRI 0x0400 /* RX dribble packet */ |
| 145 | #define DSC_RX_ERR_BUF 0x0200 /* RX length exceeds buffer size */ |
| 146 | #define DSC_RX_ERR_LONG 0x0100 /* RX length > maximum packet length */ |
| 147 | #define DSC_RX_ERR_RUNT 0x0080 /* RX packet length < 64 byte */ |
| 148 | #define DSC_RX_ERR_CRC 0x0040 /* RX CRC error */ |
| 149 | #define DSC_RX_BCAST 0x0020 /* RX broadcast (no error) */ |
| 150 | #define DSC_RX_MCAST 0x0010 /* RX multicast (no error) */ |
| 151 | #define DSC_RX_MCH_HIT 0x0008 /* RX multicast hit in hash table (no error) */ |
| 152 | #define DSC_RX_MIDH_HIT 0x0004 /* RX MID table hit (no error) */ |
| 153 | #define DSC_RX_IDX_MID_MASK 3 /* RX mask for the index of matched MIDx */ |
| 154 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 155 | /* PHY settings */ |
| 156 | #define ICPLUS_PHY_ID 0x0243 |
| 157 | |
| 158 | MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>," |
| 159 | "Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>," |
| 160 | "Florian Fainelli <florian@openwrt.org>"); |
| 161 | MODULE_LICENSE("GPL"); |
| 162 | MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet driver"); |
| 163 | |
Florian Fainelli | 3d25434 | 2008-07-13 14:28:27 +0200 | [diff] [blame] | 164 | /* RX and TX interrupts that we handle */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 165 | #define RX_INTS (RX_FIFO_FULL | RX_NO_DESC | RX_FINISH) |
| 166 | #define TX_INTS (TX_FINISH) |
| 167 | #define INT_MASK (RX_INTS | TX_INTS) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 168 | |
| 169 | struct r6040_descriptor { |
| 170 | u16 status, len; /* 0-3 */ |
| 171 | __le32 buf; /* 4-7 */ |
| 172 | __le32 ndesc; /* 8-B */ |
| 173 | u32 rev1; /* C-F */ |
| 174 | char *vbufp; /* 10-13 */ |
| 175 | struct r6040_descriptor *vndescp; /* 14-17 */ |
| 176 | struct sk_buff *skb_ptr; /* 18-1B */ |
| 177 | u32 rev2; /* 1C-1F */ |
| 178 | } __attribute__((aligned(32))); |
| 179 | |
| 180 | struct r6040_private { |
| 181 | spinlock_t lock; /* driver lock */ |
| 182 | struct timer_list timer; |
| 183 | struct pci_dev *pdev; |
| 184 | struct r6040_descriptor *rx_insert_ptr; |
| 185 | struct r6040_descriptor *rx_remove_ptr; |
| 186 | struct r6040_descriptor *tx_insert_ptr; |
| 187 | struct r6040_descriptor *tx_remove_ptr; |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 188 | struct r6040_descriptor *rx_ring; |
| 189 | struct r6040_descriptor *tx_ring; |
| 190 | dma_addr_t rx_ring_dma; |
| 191 | dma_addr_t tx_ring_dma; |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 192 | u16 tx_free_desc, phy_addr, phy_mode; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 193 | u16 mcr0, mcr1; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 194 | u16 switch_sig; |
| 195 | struct net_device *dev; |
| 196 | struct mii_if_info mii_if; |
| 197 | struct napi_struct napi; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 198 | void __iomem *base; |
| 199 | }; |
| 200 | |
| 201 | static char version[] __devinitdata = KERN_INFO DRV_NAME |
| 202 | ": RDC R6040 NAPI net driver," |
| 203 | "version "DRV_VERSION " (" DRV_RELDATE ")\n"; |
| 204 | |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 205 | static int phy_table[] = { PHY1_ADDR, PHY2_ADDR }; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 206 | |
| 207 | /* Read a word data from PHY Chip */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 208 | 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] | 209 | { |
| 210 | int limit = 2048; |
| 211 | u16 cmd; |
| 212 | |
| 213 | iowrite16(MDIO_READ + reg + (phy_addr << 8), ioaddr + MMDIO); |
| 214 | /* Wait for the read bit to be cleared */ |
| 215 | while (limit--) { |
| 216 | cmd = ioread16(ioaddr + MMDIO); |
| 217 | if (cmd & MDIO_READ) |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | return ioread16(ioaddr + MMRD); |
| 222 | } |
| 223 | |
| 224 | /* Write a word data from PHY Chip */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 225 | static void r6040_phy_write(void __iomem *ioaddr, int phy_addr, int reg, u16 val) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 226 | { |
| 227 | int limit = 2048; |
| 228 | u16 cmd; |
| 229 | |
| 230 | iowrite16(val, ioaddr + MMWD); |
| 231 | /* Write the command to the MDIO bus */ |
| 232 | iowrite16(MDIO_WRITE + reg + (phy_addr << 8), ioaddr + MMDIO); |
| 233 | /* Wait for the write bit to be cleared */ |
| 234 | while (limit--) { |
| 235 | cmd = ioread16(ioaddr + MMDIO); |
| 236 | if (cmd & MDIO_WRITE) |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 241 | static int r6040_mdio_read(struct net_device *dev, int mii_id, int reg) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 242 | { |
| 243 | struct r6040_private *lp = netdev_priv(dev); |
| 244 | void __iomem *ioaddr = lp->base; |
| 245 | |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 246 | return (r6040_phy_read(ioaddr, lp->phy_addr, reg)); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 249 | static void r6040_mdio_write(struct net_device *dev, int mii_id, int reg, int val) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 250 | { |
| 251 | struct r6040_private *lp = netdev_priv(dev); |
| 252 | void __iomem *ioaddr = lp->base; |
| 253 | |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 254 | r6040_phy_write(ioaddr, lp->phy_addr, reg, val); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 257 | static void r6040_free_txbufs(struct net_device *dev) |
| 258 | { |
| 259 | struct r6040_private *lp = netdev_priv(dev); |
| 260 | int i; |
| 261 | |
| 262 | for (i = 0; i < TX_DCNT; i++) { |
| 263 | if (lp->tx_insert_ptr->skb_ptr) { |
Al Viro | ed773b4 | 2008-03-16 22:43:06 +0000 | [diff] [blame] | 264 | pci_unmap_single(lp->pdev, |
| 265 | le32_to_cpu(lp->tx_insert_ptr->buf), |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 266 | MAX_BUF_SIZE, PCI_DMA_TODEVICE); |
| 267 | dev_kfree_skb(lp->tx_insert_ptr->skb_ptr); |
Florian Fainelli | 3b060be | 2008-09-24 21:16:40 +0200 | [diff] [blame] | 268 | lp->tx_insert_ptr->skb_ptr = NULL; |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 269 | } |
| 270 | lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | static void r6040_free_rxbufs(struct net_device *dev) |
| 275 | { |
| 276 | struct r6040_private *lp = netdev_priv(dev); |
| 277 | int i; |
| 278 | |
| 279 | for (i = 0; i < RX_DCNT; i++) { |
| 280 | if (lp->rx_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->rx_insert_ptr->buf), |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 283 | MAX_BUF_SIZE, PCI_DMA_FROMDEVICE); |
| 284 | dev_kfree_skb(lp->rx_insert_ptr->skb_ptr); |
| 285 | lp->rx_insert_ptr->skb_ptr = NULL; |
| 286 | } |
| 287 | lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp; |
| 288 | } |
| 289 | } |
| 290 | |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 291 | static void r6040_init_ring_desc(struct r6040_descriptor *desc_ring, |
| 292 | dma_addr_t desc_dma, int size) |
| 293 | { |
| 294 | struct r6040_descriptor *desc = desc_ring; |
| 295 | dma_addr_t mapping = desc_dma; |
| 296 | |
| 297 | while (size-- > 0) { |
Julia Lawall | 3f6602a | 2008-06-23 23:12:31 +0200 | [diff] [blame] | 298 | mapping += sizeof(*desc); |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 299 | desc->ndesc = cpu_to_le32(mapping); |
| 300 | desc->vndescp = desc + 1; |
| 301 | desc++; |
| 302 | } |
| 303 | desc--; |
| 304 | desc->ndesc = cpu_to_le32(desc_dma); |
| 305 | desc->vndescp = desc_ring; |
| 306 | } |
| 307 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 308 | static void r6040_init_txbufs(struct net_device *dev) |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 309 | { |
| 310 | struct r6040_private *lp = netdev_priv(dev); |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 311 | |
| 312 | lp->tx_free_desc = TX_DCNT; |
| 313 | |
| 314 | lp->tx_remove_ptr = lp->tx_insert_ptr = lp->tx_ring; |
| 315 | 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] | 316 | } |
| 317 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 318 | static int r6040_alloc_rxbufs(struct net_device *dev) |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 319 | { |
| 320 | struct r6040_private *lp = netdev_priv(dev); |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 321 | struct r6040_descriptor *desc; |
| 322 | struct sk_buff *skb; |
| 323 | int rc; |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 324 | |
| 325 | lp->rx_remove_ptr = lp->rx_insert_ptr = lp->rx_ring; |
| 326 | r6040_init_ring_desc(lp->rx_ring, lp->rx_ring_dma, RX_DCNT); |
| 327 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 328 | /* Allocate skbs for the rx descriptors */ |
| 329 | desc = lp->rx_ring; |
| 330 | do { |
| 331 | skb = netdev_alloc_skb(dev, MAX_BUF_SIZE); |
| 332 | if (!skb) { |
| 333 | printk(KERN_ERR "%s: failed to alloc skb for rx\n", dev->name); |
| 334 | rc = -ENOMEM; |
| 335 | goto err_exit; |
| 336 | } |
| 337 | desc->skb_ptr = skb; |
| 338 | desc->buf = cpu_to_le32(pci_map_single(lp->pdev, |
| 339 | desc->skb_ptr->data, |
| 340 | MAX_BUF_SIZE, PCI_DMA_FROMDEVICE)); |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 341 | desc->status = DSC_OWNER_MAC; |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 342 | desc = desc->vndescp; |
| 343 | } while (desc != lp->rx_ring); |
| 344 | |
| 345 | return 0; |
| 346 | |
| 347 | err_exit: |
| 348 | /* Deallocate all previously allocated skbs */ |
| 349 | r6040_free_rxbufs(dev); |
| 350 | return rc; |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 351 | } |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 352 | |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 353 | static void r6040_init_mac_regs(struct net_device *dev) |
| 354 | { |
| 355 | struct r6040_private *lp = netdev_priv(dev); |
| 356 | void __iomem *ioaddr = lp->base; |
| 357 | int limit = 2048; |
| 358 | u16 cmd; |
| 359 | |
| 360 | /* Mask Off Interrupt */ |
| 361 | iowrite16(MSK_INT, ioaddr + MIER); |
| 362 | |
| 363 | /* Reset RDC MAC */ |
| 364 | iowrite16(MAC_RST, ioaddr + MCR1); |
| 365 | while (limit--) { |
| 366 | cmd = ioread16(ioaddr + MCR1); |
| 367 | if (cmd & 0x1) |
| 368 | break; |
| 369 | } |
| 370 | /* Reset internal state machine */ |
| 371 | iowrite16(2, ioaddr + MAC_SM); |
| 372 | iowrite16(0, ioaddr + MAC_SM); |
Florian Fainelli | c1d6993 | 2008-09-03 16:50:03 +0200 | [diff] [blame] | 373 | mdelay(5); |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 374 | |
| 375 | /* MAC Bus Control Register */ |
| 376 | iowrite16(MBCR_DEFAULT, ioaddr + MBCR); |
| 377 | |
| 378 | /* Buffer Size Register */ |
| 379 | iowrite16(MAX_BUF_SIZE, ioaddr + MR_BSR); |
| 380 | |
| 381 | /* Write TX ring start address */ |
| 382 | iowrite16(lp->tx_ring_dma, ioaddr + MTD_SA0); |
| 383 | iowrite16(lp->tx_ring_dma >> 16, ioaddr + MTD_SA1); |
| 384 | |
| 385 | /* Write RX ring start address */ |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 386 | iowrite16(lp->rx_ring_dma, ioaddr + MRD_SA0); |
| 387 | iowrite16(lp->rx_ring_dma >> 16, ioaddr + MRD_SA1); |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 388 | |
| 389 | /* Set interrupt waiting time and packet numbers */ |
Florian Fainelli | 31718de | 2008-07-13 14:35:00 +0200 | [diff] [blame] | 390 | iowrite16(0, ioaddr + MT_ICR); |
| 391 | iowrite16(0, ioaddr + MR_ICR); |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 392 | |
| 393 | /* Enable interrupts */ |
| 394 | iowrite16(INT_MASK, ioaddr + MIER); |
| 395 | |
| 396 | /* Enable TX and RX */ |
| 397 | iowrite16(lp->mcr0 | 0x0002, ioaddr); |
| 398 | |
| 399 | /* Let TX poll the descriptors |
| 400 | * we may got called by r6040_tx_timeout which has left |
| 401 | * some unsent tx buffers */ |
| 402 | iowrite16(0x01, ioaddr + MTPR); |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 403 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 404 | |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 405 | static void r6040_tx_timeout(struct net_device *dev) |
| 406 | { |
| 407 | struct r6040_private *priv = netdev_priv(dev); |
| 408 | void __iomem *ioaddr = priv->base; |
| 409 | |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 410 | printk(KERN_WARNING "%s: transmit timed out, int enable %4.4x " |
| 411 | "status %4.4x, PHY status %4.4x\n", |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 412 | dev->name, ioread16(ioaddr + MIER), |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 413 | ioread16(ioaddr + MISR), |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 414 | r6040_mdio_read(dev, priv->mii_if.phy_id, MII_BMSR)); |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 415 | |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 416 | dev->stats.tx_errors++; |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 417 | |
| 418 | /* Reset MAC and re-init all registers */ |
| 419 | r6040_init_mac_regs(dev); |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 422 | static struct net_device_stats *r6040_get_stats(struct net_device *dev) |
| 423 | { |
| 424 | struct r6040_private *priv = netdev_priv(dev); |
| 425 | void __iomem *ioaddr = priv->base; |
| 426 | unsigned long flags; |
| 427 | |
| 428 | spin_lock_irqsave(&priv->lock, flags); |
Florian Fainelli | d248fd7 | 2007-12-12 22:34:55 +0100 | [diff] [blame] | 429 | dev->stats.rx_crc_errors += ioread8(ioaddr + ME_CNT1); |
| 430 | dev->stats.multicast += ioread8(ioaddr + ME_CNT0); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 431 | spin_unlock_irqrestore(&priv->lock, flags); |
| 432 | |
Florian Fainelli | d248fd7 | 2007-12-12 22:34:55 +0100 | [diff] [blame] | 433 | return &dev->stats; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Stop RDC MAC and Free the allocated resource */ |
| 437 | static void r6040_down(struct net_device *dev) |
| 438 | { |
| 439 | struct r6040_private *lp = netdev_priv(dev); |
| 440 | void __iomem *ioaddr = lp->base; |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 441 | struct pci_dev *pdev = lp->pdev; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 442 | int limit = 2048; |
| 443 | u16 *adrp; |
| 444 | u16 cmd; |
| 445 | |
| 446 | /* Stop MAC */ |
| 447 | iowrite16(MSK_INT, ioaddr + MIER); /* Mask Off Interrupt */ |
| 448 | iowrite16(MAC_RST, ioaddr + MCR1); /* Reset RDC MAC */ |
| 449 | while (limit--) { |
| 450 | cmd = ioread16(ioaddr + MCR1); |
| 451 | if (cmd & 0x1) |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | /* Restore MAC Address to MIDx */ |
| 456 | adrp = (u16 *) dev->dev_addr; |
| 457 | iowrite16(adrp[0], ioaddr + MID_0L); |
| 458 | iowrite16(adrp[1], ioaddr + MID_0M); |
| 459 | iowrite16(adrp[2], ioaddr + MID_0H); |
| 460 | free_irq(dev->irq, dev); |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 461 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 462 | /* Free RX buffer */ |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 463 | r6040_free_rxbufs(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 464 | |
| 465 | /* Free TX buffer */ |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 466 | r6040_free_txbufs(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 467 | |
| 468 | /* Free Descriptor memory */ |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 469 | pci_free_consistent(pdev, RX_DESC_SIZE, lp->rx_ring, lp->rx_ring_dma); |
| 470 | pci_free_consistent(pdev, TX_DESC_SIZE, lp->tx_ring, lp->tx_ring_dma); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 473 | static int r6040_close(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 474 | { |
| 475 | struct r6040_private *lp = netdev_priv(dev); |
| 476 | |
| 477 | /* deleted timer */ |
| 478 | del_timer_sync(&lp->timer); |
| 479 | |
| 480 | spin_lock_irq(&lp->lock); |
Florian Fainelli | 129cf9a | 2008-07-13 14:32:45 +0200 | [diff] [blame] | 481 | napi_disable(&lp->napi); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 482 | netif_stop_queue(dev); |
| 483 | r6040_down(dev); |
| 484 | spin_unlock_irq(&lp->lock); |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | /* Status of PHY CHIP */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 490 | static int r6040_phy_mode_chk(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 491 | { |
| 492 | struct r6040_private *lp = netdev_priv(dev); |
| 493 | void __iomem *ioaddr = lp->base; |
| 494 | int phy_dat; |
| 495 | |
| 496 | /* PHY Link Status Check */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 497 | phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 498 | if (!(phy_dat & 0x4)) |
| 499 | phy_dat = 0x8000; /* Link Failed, full duplex */ |
| 500 | |
| 501 | /* PHY Chip Auto-Negotiation Status */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 502 | phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 503 | if (phy_dat & 0x0020) { |
| 504 | /* Auto Negotiation Mode */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 505 | phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 5); |
| 506 | phy_dat &= r6040_phy_read(ioaddr, lp->phy_addr, 4); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 507 | if (phy_dat & 0x140) |
| 508 | /* Force full duplex */ |
| 509 | phy_dat = 0x8000; |
| 510 | else |
| 511 | phy_dat = 0; |
| 512 | } else { |
| 513 | /* Force Mode */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 514 | phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 0); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 515 | if (phy_dat & 0x100) |
| 516 | phy_dat = 0x8000; |
| 517 | else |
| 518 | phy_dat = 0x0000; |
| 519 | } |
| 520 | |
| 521 | return phy_dat; |
| 522 | }; |
| 523 | |
| 524 | static void r6040_set_carrier(struct mii_if_info *mii) |
| 525 | { |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 526 | if (r6040_phy_mode_chk(mii->dev)) { |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 527 | /* autoneg is off: Link is always assumed to be up */ |
| 528 | if (!netif_carrier_ok(mii->dev)) |
| 529 | netif_carrier_on(mii->dev); |
| 530 | } else |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 531 | r6040_phy_mode_chk(mii->dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
| 535 | { |
| 536 | struct r6040_private *lp = netdev_priv(dev); |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 537 | struct mii_ioctl_data *data = if_mii(rq); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 538 | int rc; |
| 539 | |
| 540 | if (!netif_running(dev)) |
| 541 | return -EINVAL; |
| 542 | spin_lock_irq(&lp->lock); |
| 543 | rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL); |
| 544 | spin_unlock_irq(&lp->lock); |
| 545 | r6040_set_carrier(&lp->mii_if); |
| 546 | return rc; |
| 547 | } |
| 548 | |
| 549 | static int r6040_rx(struct net_device *dev, int limit) |
| 550 | { |
| 551 | struct r6040_private *priv = netdev_priv(dev); |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 552 | struct r6040_descriptor *descptr = priv->rx_remove_ptr; |
| 553 | struct sk_buff *skb_ptr, *new_skb; |
| 554 | int count = 0; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 555 | u16 err; |
| 556 | |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 557 | /* Limit not reached and the descriptor belongs to the CPU */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 558 | while (count < limit && !(descptr->status & DSC_OWNER_MAC)) { |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 559 | /* Read the descriptor status */ |
| 560 | err = descptr->status; |
| 561 | /* Global error status set */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 562 | if (err & DSC_RX_ERR) { |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 563 | /* RX dribble */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 564 | if (err & DSC_RX_ERR_DRI) |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 565 | dev->stats.rx_frame_errors++; |
| 566 | /* Buffer lenght exceeded */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 567 | if (err & DSC_RX_ERR_BUF) |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 568 | dev->stats.rx_length_errors++; |
| 569 | /* Packet too long */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 570 | if (err & DSC_RX_ERR_LONG) |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 571 | dev->stats.rx_length_errors++; |
| 572 | /* Packet < 64 bytes */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 573 | if (err & DSC_RX_ERR_RUNT) |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 574 | dev->stats.rx_length_errors++; |
| 575 | /* CRC error */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 576 | if (err & DSC_RX_ERR_CRC) { |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 577 | spin_lock(&priv->lock); |
| 578 | dev->stats.rx_crc_errors++; |
| 579 | spin_unlock(&priv->lock); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 580 | } |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 581 | goto next_descr; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 582 | } |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 583 | |
| 584 | /* Packet successfully received */ |
| 585 | new_skb = netdev_alloc_skb(dev, MAX_BUF_SIZE); |
| 586 | if (!new_skb) { |
| 587 | dev->stats.rx_dropped++; |
| 588 | goto next_descr; |
| 589 | } |
| 590 | skb_ptr = descptr->skb_ptr; |
| 591 | skb_ptr->dev = priv->dev; |
| 592 | |
| 593 | /* Do not count the CRC */ |
| 594 | skb_put(skb_ptr, descptr->len - 4); |
| 595 | pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf), |
| 596 | MAX_BUF_SIZE, PCI_DMA_FROMDEVICE); |
| 597 | skb_ptr->protocol = eth_type_trans(skb_ptr, priv->dev); |
| 598 | |
| 599 | /* Send to upper layer */ |
| 600 | netif_receive_skb(skb_ptr); |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 601 | dev->stats.rx_packets++; |
| 602 | dev->stats.rx_bytes += descptr->len - 4; |
| 603 | |
| 604 | /* put new skb into descriptor */ |
| 605 | descptr->skb_ptr = new_skb; |
| 606 | descptr->buf = cpu_to_le32(pci_map_single(priv->pdev, |
| 607 | descptr->skb_ptr->data, |
| 608 | MAX_BUF_SIZE, PCI_DMA_FROMDEVICE)); |
| 609 | |
| 610 | next_descr: |
| 611 | /* put the descriptor back to the MAC */ |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 612 | descptr->status = DSC_OWNER_MAC; |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 613 | descptr = descptr->vndescp; |
| 614 | count++; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 615 | } |
Florian Fainelli | 9ca28dc | 2008-07-13 14:33:36 +0200 | [diff] [blame] | 616 | priv->rx_remove_ptr = descptr; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 617 | |
| 618 | return count; |
| 619 | } |
| 620 | |
| 621 | static void r6040_tx(struct net_device *dev) |
| 622 | { |
| 623 | struct r6040_private *priv = netdev_priv(dev); |
| 624 | struct r6040_descriptor *descptr; |
| 625 | void __iomem *ioaddr = priv->base; |
| 626 | struct sk_buff *skb_ptr; |
| 627 | u16 err; |
| 628 | |
| 629 | spin_lock(&priv->lock); |
| 630 | descptr = priv->tx_remove_ptr; |
| 631 | while (priv->tx_free_desc < TX_DCNT) { |
| 632 | /* Check for errors */ |
| 633 | err = ioread16(ioaddr + MLSR); |
| 634 | |
Florian Fainelli | d248fd7 | 2007-12-12 22:34:55 +0100 | [diff] [blame] | 635 | if (err & 0x0200) |
| 636 | dev->stats.rx_fifo_errors++; |
| 637 | if (err & (0x2000 | 0x4000)) |
| 638 | dev->stats.tx_carrier_errors++; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 639 | |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 640 | if (descptr->status & DSC_OWNER_MAC) |
Florian Fainelli | ec6d2d4 | 2007-12-12 23:13:15 +0100 | [diff] [blame] | 641 | break; /* Not complete */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 642 | skb_ptr = descptr->skb_ptr; |
Al Viro | ed773b4 | 2008-03-16 22:43:06 +0000 | [diff] [blame] | 643 | pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf), |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 644 | skb_ptr->len, PCI_DMA_TODEVICE); |
| 645 | /* Free buffer */ |
| 646 | dev_kfree_skb_irq(skb_ptr); |
| 647 | descptr->skb_ptr = NULL; |
| 648 | /* To next descriptor */ |
| 649 | descptr = descptr->vndescp; |
| 650 | priv->tx_free_desc++; |
| 651 | } |
| 652 | priv->tx_remove_ptr = descptr; |
| 653 | |
| 654 | if (priv->tx_free_desc) |
| 655 | netif_wake_queue(dev); |
| 656 | spin_unlock(&priv->lock); |
| 657 | } |
| 658 | |
| 659 | static int r6040_poll(struct napi_struct *napi, int budget) |
| 660 | { |
| 661 | struct r6040_private *priv = |
| 662 | container_of(napi, struct r6040_private, napi); |
| 663 | struct net_device *dev = priv->dev; |
| 664 | void __iomem *ioaddr = priv->base; |
| 665 | int work_done; |
| 666 | |
| 667 | work_done = r6040_rx(dev, budget); |
| 668 | |
| 669 | if (work_done < budget) { |
| 670 | netif_rx_complete(dev, napi); |
| 671 | /* Enable RX interrupt */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 672 | iowrite16(ioread16(ioaddr + MIER) | RX_INTS, ioaddr + MIER); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 673 | } |
| 674 | return work_done; |
| 675 | } |
| 676 | |
| 677 | /* The RDC interrupt handler. */ |
| 678 | static irqreturn_t r6040_interrupt(int irq, void *dev_id) |
| 679 | { |
| 680 | struct net_device *dev = dev_id; |
| 681 | struct r6040_private *lp = netdev_priv(dev); |
| 682 | void __iomem *ioaddr = lp->base; |
| 683 | u16 status; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 684 | |
| 685 | /* Mask off RDC MAC interrupt */ |
| 686 | iowrite16(MSK_INT, ioaddr + MIER); |
| 687 | /* Read MISR status and clear */ |
| 688 | status = ioread16(ioaddr + MISR); |
| 689 | |
| 690 | if (status == 0x0000 || status == 0xffff) |
| 691 | return IRQ_NONE; |
| 692 | |
| 693 | /* RX interrupt request */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 694 | if (status & RX_INTS) { |
| 695 | if (status & RX_NO_DESC) { |
| 696 | /* RX descriptor unavailable */ |
| 697 | dev->stats.rx_dropped++; |
| 698 | dev->stats.rx_missed_errors++; |
| 699 | } |
| 700 | if (status & RX_FIFO_FULL) |
| 701 | dev->stats.rx_fifo_errors++; |
| 702 | |
Florian Fainelli | 3d25434 | 2008-07-13 14:28:27 +0200 | [diff] [blame] | 703 | /* Mask off RX interrupt */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 704 | iowrite16(ioread16(ioaddr + MIER) & ~RX_INTS, ioaddr + MIER); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 705 | netif_rx_schedule(dev, &lp->napi); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /* TX interrupt request */ |
Florian Fainelli | e24ddf3 | 2008-07-13 14:35:32 +0200 | [diff] [blame] | 709 | if (status & TX_INTS) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 710 | r6040_tx(dev); |
| 711 | |
Florian Fainelli | ec6d2d4 | 2007-12-12 23:13:15 +0100 | [diff] [blame] | 712 | return IRQ_HANDLED; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 716 | static void r6040_poll_controller(struct net_device *dev) |
| 717 | { |
| 718 | disable_irq(dev->irq); |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 719 | r6040_interrupt(dev->irq, dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 720 | enable_irq(dev->irq); |
| 721 | } |
| 722 | #endif |
| 723 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 724 | /* Init RDC MAC */ |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 725 | static int r6040_up(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 726 | { |
| 727 | struct r6040_private *lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 728 | void __iomem *ioaddr = lp->base; |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 729 | int ret; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 730 | |
Florian Fainelli | b4f1255 | 2007-12-12 22:55:34 +0100 | [diff] [blame] | 731 | /* Initialise and alloc RX/TX buffers */ |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 732 | r6040_init_txbufs(dev); |
| 733 | ret = r6040_alloc_rxbufs(dev); |
| 734 | if (ret) |
| 735 | return ret; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 736 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 737 | /* Read the PHY ID */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 738 | lp->switch_sig = r6040_phy_read(ioaddr, 0, 2); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 739 | |
| 740 | if (lp->switch_sig == ICPLUS_PHY_ID) { |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 741 | r6040_phy_write(ioaddr, 29, 31, 0x175C); /* Enable registers */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 742 | lp->phy_mode = 0x8000; |
| 743 | } else { |
| 744 | /* PHY Mode Check */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 745 | r6040_phy_write(ioaddr, lp->phy_addr, 4, PHY_CAP); |
| 746 | r6040_phy_write(ioaddr, lp->phy_addr, 0, PHY_MODE); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 747 | |
| 748 | if (PHY_MODE == 0x3100) |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 749 | lp->phy_mode = r6040_phy_mode_chk(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 750 | else |
| 751 | lp->phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0; |
| 752 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 753 | |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 754 | /* Set duplex mode */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 755 | lp->mcr0 |= lp->phy_mode; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 756 | |
| 757 | /* improve performance (by RDC guys) */ |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 758 | r6040_phy_write(ioaddr, 30, 17, (r6040_phy_read(ioaddr, 30, 17) | 0x4000)); |
| 759 | r6040_phy_write(ioaddr, 30, 17, ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000)); |
| 760 | r6040_phy_write(ioaddr, 0, 19, 0x0000); |
| 761 | r6040_phy_write(ioaddr, 0, 30, 0x01F0); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 762 | |
Florian Fainelli | fec3a23 | 2008-07-13 14:29:20 +0200 | [diff] [blame] | 763 | /* Initialize all MAC registers */ |
| 764 | r6040_init_mac_regs(dev); |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 765 | |
| 766 | return 0; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* |
| 770 | A periodic timer routine |
| 771 | Polling PHY Chip Link Status |
| 772 | */ |
| 773 | static void r6040_timer(unsigned long data) |
| 774 | { |
| 775 | struct net_device *dev = (struct net_device *)data; |
Francois Romieu | e6a9ea1 | 2007-11-28 22:55:36 +0100 | [diff] [blame] | 776 | struct r6040_private *lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 777 | void __iomem *ioaddr = lp->base; |
| 778 | u16 phy_mode; |
| 779 | |
| 780 | /* Polling PHY Chip Status */ |
| 781 | if (PHY_MODE == 0x3100) |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 782 | phy_mode = r6040_phy_mode_chk(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 783 | else |
| 784 | phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0; |
| 785 | |
| 786 | if (phy_mode != lp->phy_mode) { |
| 787 | lp->phy_mode = phy_mode; |
| 788 | lp->mcr0 = (lp->mcr0 & 0x7fff) | phy_mode; |
| 789 | iowrite16(lp->mcr0, ioaddr); |
| 790 | printk(KERN_INFO "Link Change %x \n", ioread16(ioaddr)); |
| 791 | } |
| 792 | |
| 793 | /* Timer active again */ |
Christophe Jaillet | 208aefa | 2008-05-15 23:26:22 +0200 | [diff] [blame] | 794 | mod_timer(&lp->timer, round_jiffies(jiffies + HZ)); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /* Read/set MAC address routines */ |
| 798 | static void r6040_mac_address(struct net_device *dev) |
| 799 | { |
| 800 | struct r6040_private *lp = netdev_priv(dev); |
| 801 | void __iomem *ioaddr = lp->base; |
| 802 | u16 *adrp; |
| 803 | |
| 804 | /* MAC operation register */ |
| 805 | iowrite16(0x01, ioaddr + MCR1); /* Reset MAC */ |
| 806 | iowrite16(2, ioaddr + MAC_SM); /* Reset internal state machine */ |
| 807 | iowrite16(0, ioaddr + MAC_SM); |
Florian Fainelli | c1d6993 | 2008-09-03 16:50:03 +0200 | [diff] [blame] | 808 | mdelay(5); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 809 | |
| 810 | /* Restore MAC Address */ |
| 811 | adrp = (u16 *) dev->dev_addr; |
| 812 | iowrite16(adrp[0], ioaddr + MID_0L); |
| 813 | iowrite16(adrp[1], ioaddr + MID_0M); |
| 814 | iowrite16(adrp[2], ioaddr + MID_0H); |
| 815 | } |
| 816 | |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 817 | static int r6040_open(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 818 | { |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 819 | struct r6040_private *lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 820 | int ret; |
| 821 | |
| 822 | /* Request IRQ and Register interrupt handler */ |
| 823 | ret = request_irq(dev->irq, &r6040_interrupt, |
| 824 | IRQF_SHARED, dev->name, dev); |
| 825 | if (ret) |
| 826 | return ret; |
| 827 | |
| 828 | /* Set MAC address */ |
| 829 | r6040_mac_address(dev); |
| 830 | |
| 831 | /* Allocate Descriptor memory */ |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 832 | lp->rx_ring = |
| 833 | pci_alloc_consistent(lp->pdev, RX_DESC_SIZE, &lp->rx_ring_dma); |
| 834 | if (!lp->rx_ring) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 835 | return -ENOMEM; |
| 836 | |
Francois Romieu | 6c32310 | 2007-11-28 22:31:00 +0100 | [diff] [blame] | 837 | lp->tx_ring = |
| 838 | pci_alloc_consistent(lp->pdev, TX_DESC_SIZE, &lp->tx_ring_dma); |
| 839 | if (!lp->tx_ring) { |
| 840 | pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring, |
| 841 | lp->rx_ring_dma); |
| 842 | return -ENOMEM; |
| 843 | } |
| 844 | |
Florian Fainelli | 3d46341 | 2008-07-13 14:32:18 +0200 | [diff] [blame] | 845 | ret = r6040_up(dev); |
| 846 | if (ret) { |
| 847 | pci_free_consistent(lp->pdev, TX_DESC_SIZE, lp->tx_ring, |
| 848 | lp->tx_ring_dma); |
| 849 | pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring, |
| 850 | lp->rx_ring_dma); |
| 851 | return ret; |
| 852 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 853 | |
| 854 | napi_enable(&lp->napi); |
| 855 | netif_start_queue(dev); |
| 856 | |
Florian Fainelli | 106adf3 | 2007-12-12 23:01:33 +0100 | [diff] [blame] | 857 | /* set and active a timer process */ |
| 858 | setup_timer(&lp->timer, r6040_timer, (unsigned long) dev); |
| 859 | if (lp->switch_sig != ICPLUS_PHY_ID) |
| 860 | mod_timer(&lp->timer, jiffies + HZ); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 861 | return 0; |
| 862 | } |
| 863 | |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 864 | static int r6040_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 865 | { |
| 866 | struct r6040_private *lp = netdev_priv(dev); |
| 867 | struct r6040_descriptor *descptr; |
| 868 | void __iomem *ioaddr = lp->base; |
| 869 | unsigned long flags; |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 870 | int ret = NETDEV_TX_OK; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 871 | |
| 872 | /* Critical Section */ |
| 873 | spin_lock_irqsave(&lp->lock, flags); |
| 874 | |
| 875 | /* TX resource check */ |
| 876 | if (!lp->tx_free_desc) { |
| 877 | spin_unlock_irqrestore(&lp->lock, flags); |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 878 | netif_stop_queue(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 879 | printk(KERN_ERR DRV_NAME ": no tx descriptor\n"); |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 880 | ret = NETDEV_TX_BUSY; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | /* Statistic Counter */ |
| 885 | dev->stats.tx_packets++; |
| 886 | dev->stats.tx_bytes += skb->len; |
| 887 | /* Set TX descriptor & Transmit it */ |
| 888 | lp->tx_free_desc--; |
| 889 | descptr = lp->tx_insert_ptr; |
| 890 | if (skb->len < MISR) |
| 891 | descptr->len = MISR; |
| 892 | else |
| 893 | descptr->len = skb->len; |
| 894 | |
| 895 | descptr->skb_ptr = skb; |
| 896 | descptr->buf = cpu_to_le32(pci_map_single(lp->pdev, |
| 897 | skb->data, skb->len, PCI_DMA_TODEVICE)); |
Florian Fainelli | 32f565d | 2008-07-13 14:34:15 +0200 | [diff] [blame] | 898 | descptr->status = DSC_OWNER_MAC; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 899 | /* Trigger the MAC to check the TX descriptor */ |
| 900 | iowrite16(0x01, ioaddr + MTPR); |
| 901 | lp->tx_insert_ptr = descptr->vndescp; |
| 902 | |
| 903 | /* If no tx resource, stop */ |
| 904 | if (!lp->tx_free_desc) |
| 905 | netif_stop_queue(dev); |
| 906 | |
| 907 | dev->trans_start = jiffies; |
| 908 | spin_unlock_irqrestore(&lp->lock, flags); |
| 909 | return ret; |
| 910 | } |
| 911 | |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 912 | static void r6040_multicast_list(struct net_device *dev) |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 913 | { |
| 914 | struct r6040_private *lp = netdev_priv(dev); |
| 915 | void __iomem *ioaddr = lp->base; |
| 916 | u16 *adrp; |
| 917 | u16 reg; |
| 918 | unsigned long flags; |
| 919 | struct dev_mc_list *dmi = dev->mc_list; |
| 920 | int i; |
| 921 | |
| 922 | /* MAC Address */ |
| 923 | adrp = (u16 *)dev->dev_addr; |
| 924 | iowrite16(adrp[0], ioaddr + MID_0L); |
| 925 | iowrite16(adrp[1], ioaddr + MID_0M); |
| 926 | iowrite16(adrp[2], ioaddr + MID_0H); |
| 927 | |
| 928 | /* Promiscous Mode */ |
| 929 | spin_lock_irqsave(&lp->lock, flags); |
| 930 | |
| 931 | /* Clear AMCP & PROM bits */ |
| 932 | reg = ioread16(ioaddr) & ~0x0120; |
| 933 | if (dev->flags & IFF_PROMISC) { |
| 934 | reg |= 0x0020; |
| 935 | lp->mcr0 |= 0x0020; |
| 936 | } |
| 937 | /* Too many multicast addresses |
| 938 | * accept all traffic */ |
| 939 | else if ((dev->mc_count > MCAST_MAX) |
| 940 | || (dev->flags & IFF_ALLMULTI)) |
| 941 | reg |= 0x0020; |
| 942 | |
| 943 | iowrite16(reg, ioaddr); |
| 944 | spin_unlock_irqrestore(&lp->lock, flags); |
| 945 | |
| 946 | /* Build the hash table */ |
| 947 | if (dev->mc_count > MCAST_MAX) { |
| 948 | u16 hash_table[4]; |
| 949 | u32 crc; |
| 950 | |
| 951 | for (i = 0; i < 4; i++) |
| 952 | hash_table[i] = 0; |
| 953 | |
| 954 | for (i = 0; i < dev->mc_count; i++) { |
| 955 | char *addrs = dmi->dmi_addr; |
| 956 | |
| 957 | dmi = dmi->next; |
| 958 | |
| 959 | if (!(*addrs & 1)) |
| 960 | continue; |
| 961 | |
| 962 | crc = ether_crc_le(6, addrs); |
| 963 | crc >>= 26; |
| 964 | hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf)); |
| 965 | } |
| 966 | /* Write the index of the hash table */ |
| 967 | for (i = 0; i < 4; i++) |
| 968 | iowrite16(hash_table[i] << 14, ioaddr + MCR1); |
| 969 | /* Fill the MAC hash tables with their values */ |
| 970 | iowrite16(hash_table[0], ioaddr + MAR0); |
| 971 | iowrite16(hash_table[1], ioaddr + MAR1); |
| 972 | iowrite16(hash_table[2], ioaddr + MAR2); |
| 973 | iowrite16(hash_table[3], ioaddr + MAR3); |
| 974 | } |
| 975 | /* Multicast Address 1~4 case */ |
| 976 | for (i = 0, dmi; (i < dev->mc_count) && (i < MCAST_MAX); i++) { |
| 977 | adrp = (u16 *)dmi->dmi_addr; |
| 978 | iowrite16(adrp[0], ioaddr + MID_1L + 8*i); |
| 979 | iowrite16(adrp[1], ioaddr + MID_1M + 8*i); |
| 980 | iowrite16(adrp[2], ioaddr + MID_1H + 8*i); |
| 981 | dmi = dmi->next; |
| 982 | } |
| 983 | for (i = dev->mc_count; i < MCAST_MAX; i++) { |
| 984 | iowrite16(0xffff, ioaddr + MID_0L + 8*i); |
| 985 | iowrite16(0xffff, ioaddr + MID_0M + 8*i); |
| 986 | iowrite16(0xffff, ioaddr + MID_0H + 8*i); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | static void netdev_get_drvinfo(struct net_device *dev, |
| 991 | struct ethtool_drvinfo *info) |
| 992 | { |
| 993 | struct r6040_private *rp = netdev_priv(dev); |
| 994 | |
| 995 | strcpy(info->driver, DRV_NAME); |
| 996 | strcpy(info->version, DRV_VERSION); |
| 997 | strcpy(info->bus_info, pci_name(rp->pdev)); |
| 998 | } |
| 999 | |
| 1000 | static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1001 | { |
| 1002 | struct r6040_private *rp = netdev_priv(dev); |
| 1003 | int rc; |
| 1004 | |
| 1005 | spin_lock_irq(&rp->lock); |
| 1006 | rc = mii_ethtool_gset(&rp->mii_if, cmd); |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 1007 | spin_unlock_irq(&rp->lock); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1008 | |
| 1009 | return rc; |
| 1010 | } |
| 1011 | |
| 1012 | static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1013 | { |
| 1014 | struct r6040_private *rp = netdev_priv(dev); |
| 1015 | int rc; |
| 1016 | |
| 1017 | spin_lock_irq(&rp->lock); |
| 1018 | rc = mii_ethtool_sset(&rp->mii_if, cmd); |
| 1019 | spin_unlock_irq(&rp->lock); |
| 1020 | r6040_set_carrier(&rp->mii_if); |
| 1021 | |
| 1022 | return rc; |
| 1023 | } |
| 1024 | |
| 1025 | static u32 netdev_get_link(struct net_device *dev) |
| 1026 | { |
| 1027 | struct r6040_private *rp = netdev_priv(dev); |
| 1028 | |
| 1029 | return mii_link_ok(&rp->mii_if); |
| 1030 | } |
| 1031 | |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame^] | 1032 | static const struct ethtool_ops netdev_ethtool_ops = { |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1033 | .get_drvinfo = netdev_get_drvinfo, |
| 1034 | .get_settings = netdev_get_settings, |
| 1035 | .set_settings = netdev_set_settings, |
| 1036 | .get_link = netdev_get_link, |
| 1037 | }; |
| 1038 | |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame^] | 1039 | static const struct net_device_ops r6040_netdev_ops = { |
| 1040 | .ndo_open = r6040_open, |
| 1041 | .ndo_stop = r6040_close, |
| 1042 | .ndo_start_xmit = r6040_start_xmit, |
| 1043 | .ndo_get_stats = r6040_get_stats, |
| 1044 | .ndo_set_multicast_list = r6040_multicast_list, |
| 1045 | .ndo_change_mtu = eth_change_mtu, |
| 1046 | .ndo_validate_addr = eth_validate_addr, |
| 1047 | .ndo_do_ioctl = r6040_ioctl, |
| 1048 | .ndo_tx_timeout = r6040_tx_timeout, |
| 1049 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1050 | .ndo_poll_controller = r6040_poll_controller, |
| 1051 | #endif |
| 1052 | }; |
| 1053 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1054 | static int __devinit r6040_init_one(struct pci_dev *pdev, |
| 1055 | const struct pci_device_id *ent) |
| 1056 | { |
| 1057 | struct net_device *dev; |
| 1058 | struct r6040_private *lp; |
| 1059 | void __iomem *ioaddr; |
| 1060 | int err, io_size = R6040_IO_SIZE; |
| 1061 | static int card_idx = -1; |
| 1062 | int bar = 0; |
| 1063 | long pioaddr; |
| 1064 | u16 *adrp; |
| 1065 | |
| 1066 | printk(KERN_INFO "%s\n", version); |
| 1067 | |
| 1068 | err = pci_enable_device(pdev); |
| 1069 | if (err) |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1070 | goto err_out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1071 | |
| 1072 | /* this should always be supported */ |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1073 | err = pci_set_dma_mask(pdev, DMA_32BIT_MASK); |
| 1074 | if (err) { |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1075 | printk(KERN_ERR DRV_NAME "32-bit PCI DMA addresses" |
| 1076 | "not supported by the card\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1077 | goto err_out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1078 | } |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1079 | err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK); |
| 1080 | if (err) { |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 1081 | printk(KERN_ERR DRV_NAME "32-bit PCI DMA addresses" |
| 1082 | "not supported by the card\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1083 | goto err_out; |
Jeff Garzik | 092427b | 2007-11-23 21:49:27 -0500 | [diff] [blame] | 1084 | } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1085 | |
| 1086 | /* IO Size check */ |
| 1087 | if (pci_resource_len(pdev, 0) < io_size) { |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1088 | printk(KERN_ERR DRV_NAME "Insufficient PCI resources, aborting\n"); |
| 1089 | err = -EIO; |
| 1090 | goto err_out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | pioaddr = pci_resource_start(pdev, 0); /* IO map base address */ |
| 1094 | pci_set_master(pdev); |
| 1095 | |
| 1096 | dev = alloc_etherdev(sizeof(struct r6040_private)); |
| 1097 | if (!dev) { |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1098 | printk(KERN_ERR DRV_NAME "Failed to allocate etherdev\n"); |
| 1099 | err = -ENOMEM; |
| 1100 | goto err_out; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1101 | } |
| 1102 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 1103 | lp = netdev_priv(dev); |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1104 | |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1105 | err = pci_request_regions(pdev, DRV_NAME); |
| 1106 | |
| 1107 | if (err) { |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1108 | printk(KERN_ERR DRV_NAME ": Failed to request PCI regions\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1109 | goto err_out_free_dev; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | ioaddr = pci_iomap(pdev, bar, io_size); |
| 1113 | if (!ioaddr) { |
| 1114 | printk(KERN_ERR "ioremap failed for device %s\n", |
| 1115 | pci_name(pdev)); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1116 | err = -EIO; |
| 1117 | goto err_out_free_res; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /* Init system & device */ |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1121 | lp->base = ioaddr; |
| 1122 | dev->irq = pdev->irq; |
| 1123 | |
| 1124 | spin_lock_init(&lp->lock); |
| 1125 | pci_set_drvdata(pdev, dev); |
| 1126 | |
| 1127 | /* Set MAC address */ |
| 1128 | card_idx++; |
| 1129 | |
| 1130 | adrp = (u16 *)dev->dev_addr; |
| 1131 | adrp[0] = ioread16(ioaddr + MID_0L); |
| 1132 | adrp[1] = ioread16(ioaddr + MID_0M); |
| 1133 | adrp[2] = ioread16(ioaddr + MID_0H); |
| 1134 | |
| 1135 | /* Link new device into r6040_root_dev */ |
| 1136 | lp->pdev = pdev; |
Florian Fainelli | 129cf9a | 2008-07-13 14:32:45 +0200 | [diff] [blame] | 1137 | lp->dev = dev; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1138 | |
| 1139 | /* Init RDC private data */ |
| 1140 | lp->mcr0 = 0x1002; |
| 1141 | lp->phy_addr = phy_table[card_idx]; |
| 1142 | lp->switch_sig = 0; |
| 1143 | |
| 1144 | /* The RDC-specific entries in the device structure. */ |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame^] | 1145 | dev->netdev_ops = &r6040_netdev_ops; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1146 | dev->ethtool_ops = &netdev_ethtool_ops; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1147 | dev->watchdog_timeo = TX_TIMEOUT; |
Stephen Hemminger | a7bd89c | 2008-11-21 17:34:56 -0800 | [diff] [blame^] | 1148 | |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1149 | netif_napi_add(dev, &lp->napi, r6040_poll, 64); |
| 1150 | lp->mii_if.dev = dev; |
Florian Fainelli | c6e69bb | 2008-07-13 13:39:32 +0200 | [diff] [blame] | 1151 | lp->mii_if.mdio_read = r6040_mdio_read; |
| 1152 | lp->mii_if.mdio_write = r6040_mdio_write; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1153 | lp->mii_if.phy_id = lp->phy_addr; |
| 1154 | lp->mii_if.phy_id_mask = 0x1f; |
| 1155 | lp->mii_if.reg_num_mask = 0x1f; |
| 1156 | |
| 1157 | /* Register net device. After this dev->name assign */ |
| 1158 | err = register_netdev(dev); |
| 1159 | if (err) { |
| 1160 | printk(KERN_ERR DRV_NAME ": Failed to register net device\n"); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1161 | goto err_out_unmap; |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1162 | } |
| 1163 | return 0; |
| 1164 | |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1165 | err_out_unmap: |
| 1166 | pci_iounmap(pdev, ioaddr); |
| 1167 | err_out_free_res: |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1168 | pci_release_regions(pdev); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1169 | err_out_free_dev: |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1170 | free_netdev(dev); |
Florian Fainelli | b0e4539 | 2008-07-21 12:32:29 +0200 | [diff] [blame] | 1171 | err_out: |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1172 | return err; |
| 1173 | } |
| 1174 | |
| 1175 | static void __devexit r6040_remove_one(struct pci_dev *pdev) |
| 1176 | { |
| 1177 | struct net_device *dev = pci_get_drvdata(pdev); |
| 1178 | |
| 1179 | unregister_netdev(dev); |
| 1180 | pci_release_regions(pdev); |
| 1181 | free_netdev(dev); |
| 1182 | pci_disable_device(pdev); |
| 1183 | pci_set_drvdata(pdev, NULL); |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | static struct pci_device_id r6040_pci_tbl[] = { |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 1188 | { PCI_DEVICE(PCI_VENDOR_ID_RDC, 0x6040) }, |
| 1189 | { 0 } |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1190 | }; |
| 1191 | MODULE_DEVICE_TABLE(pci, r6040_pci_tbl); |
| 1192 | |
| 1193 | static struct pci_driver r6040_driver = { |
Francois Romieu | 5ac5d61 | 2007-11-28 23:02:33 +0100 | [diff] [blame] | 1194 | .name = DRV_NAME, |
Sten Wang | 7a47dd7 | 2007-11-12 21:31:11 -0800 | [diff] [blame] | 1195 | .id_table = r6040_pci_tbl, |
| 1196 | .probe = r6040_init_one, |
| 1197 | .remove = __devexit_p(r6040_remove_one), |
| 1198 | }; |
| 1199 | |
| 1200 | |
| 1201 | static int __init r6040_init(void) |
| 1202 | { |
| 1203 | return pci_register_driver(&r6040_driver); |
| 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | static void __exit r6040_cleanup(void) |
| 1208 | { |
| 1209 | pci_unregister_driver(&r6040_driver); |
| 1210 | } |
| 1211 | |
| 1212 | module_init(r6040_init); |
| 1213 | module_exit(r6040_cleanup); |