Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC. |
| 3 | * |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 4 | * 2005-2010 (c) Aeroflex Gaisler AB |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 5 | * |
| 6 | * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs |
| 7 | * available in the GRLIB VHDL IP core library. |
| 8 | * |
| 9 | * Full documentation of both cores can be found here: |
| 10 | * http://www.gaisler.com/products/grlib/grip.pdf |
| 11 | * |
| 12 | * The Gigabit version supports scatter/gather DMA, any alignment of |
| 13 | * buffers and checksum offloading. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify it |
| 16 | * under the terms of the GNU General Public License as published by the |
| 17 | * Free Software Foundation; either version 2 of the License, or (at your |
| 18 | * option) any later version. |
| 19 | * |
| 20 | * Contributors: Kristoffer Glembo |
| 21 | * Daniel Hellstrom |
| 22 | * Marko Isomaki |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/init.h> |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame^] | 28 | #include <linux/interrupt.h> |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <linux/ethtool.h> |
| 32 | #include <linux/skbuff.h> |
| 33 | #include <linux/io.h> |
| 34 | #include <linux/crc32.h> |
| 35 | #include <linux/mii.h> |
| 36 | #include <linux/of_device.h> |
| 37 | #include <linux/of_platform.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 39 | #include <asm/cacheflush.h> |
| 40 | #include <asm/byteorder.h> |
| 41 | |
| 42 | #ifdef CONFIG_SPARC |
| 43 | #include <asm/idprom.h> |
| 44 | #endif |
| 45 | |
| 46 | #include "greth.h" |
| 47 | |
| 48 | #define GRETH_DEF_MSG_ENABLE \ |
| 49 | (NETIF_MSG_DRV | \ |
| 50 | NETIF_MSG_PROBE | \ |
| 51 | NETIF_MSG_LINK | \ |
| 52 | NETIF_MSG_IFDOWN | \ |
| 53 | NETIF_MSG_IFUP | \ |
| 54 | NETIF_MSG_RX_ERR | \ |
| 55 | NETIF_MSG_TX_ERR) |
| 56 | |
| 57 | static int greth_debug = -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */ |
| 58 | module_param(greth_debug, int, 0); |
| 59 | MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value"); |
| 60 | |
| 61 | /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */ |
| 62 | static int macaddr[6]; |
| 63 | module_param_array(macaddr, int, NULL, 0); |
| 64 | MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address"); |
| 65 | |
| 66 | static int greth_edcl = 1; |
| 67 | module_param(greth_edcl, int, 0); |
| 68 | MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used."); |
| 69 | |
| 70 | static int greth_open(struct net_device *dev); |
kirjanov@gmail.com | 41a655b | 2010-02-24 10:25:33 +0000 | [diff] [blame] | 71 | static netdev_tx_t greth_start_xmit(struct sk_buff *skb, |
| 72 | struct net_device *dev); |
| 73 | static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb, |
| 74 | struct net_device *dev); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 75 | static int greth_rx(struct net_device *dev, int limit); |
| 76 | static int greth_rx_gbit(struct net_device *dev, int limit); |
| 77 | static void greth_clean_tx(struct net_device *dev); |
| 78 | static void greth_clean_tx_gbit(struct net_device *dev); |
| 79 | static irqreturn_t greth_interrupt(int irq, void *dev_id); |
| 80 | static int greth_close(struct net_device *dev); |
| 81 | static int greth_set_mac_add(struct net_device *dev, void *p); |
| 82 | static void greth_set_multicast_list(struct net_device *dev); |
| 83 | |
| 84 | #define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a)))) |
| 85 | #define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a))) |
| 86 | #define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v)))) |
| 87 | #define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v)))) |
| 88 | |
| 89 | #define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK) |
| 90 | #define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK) |
| 91 | #define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK) |
| 92 | |
| 93 | static void greth_print_rx_packet(void *addr, int len) |
| 94 | { |
| 95 | print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1, |
| 96 | addr, len, true); |
| 97 | } |
| 98 | |
| 99 | static void greth_print_tx_packet(struct sk_buff *skb) |
| 100 | { |
| 101 | int i; |
| 102 | int length; |
| 103 | |
| 104 | if (skb_shinfo(skb)->nr_frags == 0) |
| 105 | length = skb->len; |
| 106 | else |
| 107 | length = skb_headlen(skb); |
| 108 | |
| 109 | print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1, |
| 110 | skb->data, length, true); |
| 111 | |
| 112 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 113 | |
| 114 | print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1, |
| 115 | phys_to_virt(page_to_phys(skb_shinfo(skb)->frags[i].page)) + |
| 116 | skb_shinfo(skb)->frags[i].page_offset, |
| 117 | length, true); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static inline void greth_enable_tx(struct greth_private *greth) |
| 122 | { |
| 123 | wmb(); |
| 124 | GRETH_REGORIN(greth->regs->control, GRETH_TXEN); |
| 125 | } |
| 126 | |
| 127 | static inline void greth_disable_tx(struct greth_private *greth) |
| 128 | { |
| 129 | GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN); |
| 130 | } |
| 131 | |
| 132 | static inline void greth_enable_rx(struct greth_private *greth) |
| 133 | { |
| 134 | wmb(); |
| 135 | GRETH_REGORIN(greth->regs->control, GRETH_RXEN); |
| 136 | } |
| 137 | |
| 138 | static inline void greth_disable_rx(struct greth_private *greth) |
| 139 | { |
| 140 | GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN); |
| 141 | } |
| 142 | |
| 143 | static inline void greth_enable_irqs(struct greth_private *greth) |
| 144 | { |
| 145 | GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI); |
| 146 | } |
| 147 | |
| 148 | static inline void greth_disable_irqs(struct greth_private *greth) |
| 149 | { |
| 150 | GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI)); |
| 151 | } |
| 152 | |
| 153 | static inline void greth_write_bd(u32 *bd, u32 val) |
| 154 | { |
| 155 | __raw_writel(cpu_to_be32(val), bd); |
| 156 | } |
| 157 | |
| 158 | static inline u32 greth_read_bd(u32 *bd) |
| 159 | { |
| 160 | return be32_to_cpu(__raw_readl(bd)); |
| 161 | } |
| 162 | |
| 163 | static void greth_clean_rings(struct greth_private *greth) |
| 164 | { |
| 165 | int i; |
| 166 | struct greth_bd *rx_bdp = greth->rx_bd_base; |
| 167 | struct greth_bd *tx_bdp = greth->tx_bd_base; |
| 168 | |
| 169 | if (greth->gbit_mac) { |
| 170 | |
| 171 | /* Free and unmap RX buffers */ |
| 172 | for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) { |
| 173 | if (greth->rx_skbuff[i] != NULL) { |
| 174 | dev_kfree_skb(greth->rx_skbuff[i]); |
| 175 | dma_unmap_single(greth->dev, |
| 176 | greth_read_bd(&rx_bdp->addr), |
| 177 | MAX_FRAME_SIZE+NET_IP_ALIGN, |
| 178 | DMA_FROM_DEVICE); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* TX buffers */ |
| 183 | while (greth->tx_free < GRETH_TXBD_NUM) { |
| 184 | |
| 185 | struct sk_buff *skb = greth->tx_skbuff[greth->tx_last]; |
| 186 | int nr_frags = skb_shinfo(skb)->nr_frags; |
| 187 | tx_bdp = greth->tx_bd_base + greth->tx_last; |
| 188 | greth->tx_last = NEXT_TX(greth->tx_last); |
| 189 | |
| 190 | dma_unmap_single(greth->dev, |
| 191 | greth_read_bd(&tx_bdp->addr), |
| 192 | skb_headlen(skb), |
| 193 | DMA_TO_DEVICE); |
| 194 | |
| 195 | for (i = 0; i < nr_frags; i++) { |
| 196 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 197 | tx_bdp = greth->tx_bd_base + greth->tx_last; |
| 198 | |
| 199 | dma_unmap_page(greth->dev, |
| 200 | greth_read_bd(&tx_bdp->addr), |
| 201 | frag->size, |
| 202 | DMA_TO_DEVICE); |
| 203 | |
| 204 | greth->tx_last = NEXT_TX(greth->tx_last); |
| 205 | } |
| 206 | greth->tx_free += nr_frags+1; |
| 207 | dev_kfree_skb(skb); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | } else { /* 10/100 Mbps MAC */ |
| 212 | |
| 213 | for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) { |
| 214 | kfree(greth->rx_bufs[i]); |
| 215 | dma_unmap_single(greth->dev, |
| 216 | greth_read_bd(&rx_bdp->addr), |
| 217 | MAX_FRAME_SIZE, |
| 218 | DMA_FROM_DEVICE); |
| 219 | } |
| 220 | for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) { |
| 221 | kfree(greth->tx_bufs[i]); |
| 222 | dma_unmap_single(greth->dev, |
| 223 | greth_read_bd(&tx_bdp->addr), |
| 224 | MAX_FRAME_SIZE, |
| 225 | DMA_TO_DEVICE); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static int greth_init_rings(struct greth_private *greth) |
| 231 | { |
| 232 | struct sk_buff *skb; |
| 233 | struct greth_bd *rx_bd, *tx_bd; |
| 234 | u32 dma_addr; |
| 235 | int i; |
| 236 | |
| 237 | rx_bd = greth->rx_bd_base; |
| 238 | tx_bd = greth->tx_bd_base; |
| 239 | |
| 240 | /* Initialize descriptor rings and buffers */ |
| 241 | if (greth->gbit_mac) { |
| 242 | |
| 243 | for (i = 0; i < GRETH_RXBD_NUM; i++) { |
| 244 | skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN); |
| 245 | if (skb == NULL) { |
| 246 | if (netif_msg_ifup(greth)) |
| 247 | dev_err(greth->dev, "Error allocating DMA ring.\n"); |
| 248 | goto cleanup; |
| 249 | } |
| 250 | skb_reserve(skb, NET_IP_ALIGN); |
| 251 | dma_addr = dma_map_single(greth->dev, |
| 252 | skb->data, |
| 253 | MAX_FRAME_SIZE+NET_IP_ALIGN, |
| 254 | DMA_FROM_DEVICE); |
| 255 | |
| 256 | if (dma_mapping_error(greth->dev, dma_addr)) { |
| 257 | if (netif_msg_ifup(greth)) |
| 258 | dev_err(greth->dev, "Could not create initial DMA mapping\n"); |
| 259 | goto cleanup; |
| 260 | } |
| 261 | greth->rx_skbuff[i] = skb; |
| 262 | greth_write_bd(&rx_bd[i].addr, dma_addr); |
| 263 | greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE); |
| 264 | } |
| 265 | |
| 266 | } else { |
| 267 | |
| 268 | /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */ |
| 269 | for (i = 0; i < GRETH_RXBD_NUM; i++) { |
| 270 | |
| 271 | greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL); |
| 272 | |
| 273 | if (greth->rx_bufs[i] == NULL) { |
| 274 | if (netif_msg_ifup(greth)) |
| 275 | dev_err(greth->dev, "Error allocating DMA ring.\n"); |
| 276 | goto cleanup; |
| 277 | } |
| 278 | |
| 279 | dma_addr = dma_map_single(greth->dev, |
| 280 | greth->rx_bufs[i], |
| 281 | MAX_FRAME_SIZE, |
| 282 | DMA_FROM_DEVICE); |
| 283 | |
| 284 | if (dma_mapping_error(greth->dev, dma_addr)) { |
| 285 | if (netif_msg_ifup(greth)) |
| 286 | dev_err(greth->dev, "Could not create initial DMA mapping\n"); |
| 287 | goto cleanup; |
| 288 | } |
| 289 | greth_write_bd(&rx_bd[i].addr, dma_addr); |
| 290 | greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE); |
| 291 | } |
| 292 | for (i = 0; i < GRETH_TXBD_NUM; i++) { |
| 293 | |
| 294 | greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL); |
| 295 | |
| 296 | if (greth->tx_bufs[i] == NULL) { |
| 297 | if (netif_msg_ifup(greth)) |
| 298 | dev_err(greth->dev, "Error allocating DMA ring.\n"); |
| 299 | goto cleanup; |
| 300 | } |
| 301 | |
| 302 | dma_addr = dma_map_single(greth->dev, |
| 303 | greth->tx_bufs[i], |
| 304 | MAX_FRAME_SIZE, |
| 305 | DMA_TO_DEVICE); |
| 306 | |
| 307 | if (dma_mapping_error(greth->dev, dma_addr)) { |
| 308 | if (netif_msg_ifup(greth)) |
| 309 | dev_err(greth->dev, "Could not create initial DMA mapping\n"); |
| 310 | goto cleanup; |
| 311 | } |
| 312 | greth_write_bd(&tx_bd[i].addr, dma_addr); |
| 313 | greth_write_bd(&tx_bd[i].stat, 0); |
| 314 | } |
| 315 | } |
| 316 | greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat, |
| 317 | greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR); |
| 318 | |
| 319 | /* Initialize pointers. */ |
| 320 | greth->rx_cur = 0; |
| 321 | greth->tx_next = 0; |
| 322 | greth->tx_last = 0; |
| 323 | greth->tx_free = GRETH_TXBD_NUM; |
| 324 | |
| 325 | /* Initialize descriptor base address */ |
| 326 | GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys); |
| 327 | GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys); |
| 328 | |
| 329 | return 0; |
| 330 | |
| 331 | cleanup: |
| 332 | greth_clean_rings(greth); |
| 333 | return -ENOMEM; |
| 334 | } |
| 335 | |
| 336 | static int greth_open(struct net_device *dev) |
| 337 | { |
| 338 | struct greth_private *greth = netdev_priv(dev); |
| 339 | int err; |
| 340 | |
| 341 | err = greth_init_rings(greth); |
| 342 | if (err) { |
| 343 | if (netif_msg_ifup(greth)) |
| 344 | dev_err(&dev->dev, "Could not allocate memory for DMA rings\n"); |
| 345 | return err; |
| 346 | } |
| 347 | |
| 348 | err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev); |
| 349 | if (err) { |
| 350 | if (netif_msg_ifup(greth)) |
| 351 | dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq); |
| 352 | greth_clean_rings(greth); |
| 353 | return err; |
| 354 | } |
| 355 | |
| 356 | if (netif_msg_ifup(greth)) |
| 357 | dev_dbg(&dev->dev, " starting queue\n"); |
| 358 | netif_start_queue(dev); |
| 359 | |
Daniel Hellstrom | bbe9e63 | 2011-01-14 03:02:38 +0000 | [diff] [blame] | 360 | GRETH_REGSAVE(greth->regs->status, 0xFF); |
| 361 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 362 | napi_enable(&greth->napi); |
| 363 | |
| 364 | greth_enable_irqs(greth); |
| 365 | greth_enable_tx(greth); |
| 366 | greth_enable_rx(greth); |
| 367 | return 0; |
| 368 | |
| 369 | } |
| 370 | |
| 371 | static int greth_close(struct net_device *dev) |
| 372 | { |
| 373 | struct greth_private *greth = netdev_priv(dev); |
| 374 | |
| 375 | napi_disable(&greth->napi); |
| 376 | |
Daniel Hellstrom | bbe9e63 | 2011-01-14 03:02:38 +0000 | [diff] [blame] | 377 | greth_disable_irqs(greth); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 378 | greth_disable_tx(greth); |
Daniel Hellstrom | bbe9e63 | 2011-01-14 03:02:38 +0000 | [diff] [blame] | 379 | greth_disable_rx(greth); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 380 | |
| 381 | netif_stop_queue(dev); |
| 382 | |
| 383 | free_irq(greth->irq, (void *) dev); |
| 384 | |
| 385 | greth_clean_rings(greth); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
kirjanov@gmail.com | 41a655b | 2010-02-24 10:25:33 +0000 | [diff] [blame] | 390 | static netdev_tx_t |
| 391 | greth_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 392 | { |
| 393 | struct greth_private *greth = netdev_priv(dev); |
| 394 | struct greth_bd *bdp; |
| 395 | int err = NETDEV_TX_OK; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 396 | u32 status, dma_addr, ctrl; |
| 397 | unsigned long flags; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 398 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 399 | /* Clean TX Ring */ |
| 400 | greth_clean_tx(greth->netdev); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 401 | |
| 402 | if (unlikely(greth->tx_free <= 0)) { |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 403 | spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/ |
| 404 | ctrl = GRETH_REGLOAD(greth->regs->control); |
| 405 | /* Enable TX IRQ only if not already in poll() routine */ |
| 406 | if (ctrl & GRETH_RXI) |
| 407 | GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 408 | netif_stop_queue(dev); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 409 | spin_unlock_irqrestore(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 410 | return NETDEV_TX_BUSY; |
| 411 | } |
| 412 | |
| 413 | if (netif_msg_pktdata(greth)) |
| 414 | greth_print_tx_packet(skb); |
| 415 | |
| 416 | |
| 417 | if (unlikely(skb->len > MAX_FRAME_SIZE)) { |
| 418 | dev->stats.tx_errors++; |
| 419 | goto out; |
| 420 | } |
| 421 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 422 | bdp = greth->tx_bd_base + greth->tx_next; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 423 | dma_addr = greth_read_bd(&bdp->addr); |
| 424 | |
| 425 | memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len); |
| 426 | |
| 427 | dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE); |
| 428 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 429 | status = GRETH_BD_EN | GRETH_BD_IE | (skb->len & GRETH_BD_LEN); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 430 | |
| 431 | /* Wrap around descriptor ring */ |
| 432 | if (greth->tx_next == GRETH_TXBD_NUM_MASK) { |
| 433 | status |= GRETH_BD_WR; |
| 434 | } |
| 435 | |
| 436 | greth->tx_next = NEXT_TX(greth->tx_next); |
| 437 | greth->tx_free--; |
| 438 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 439 | /* Write descriptor control word and enable transmission */ |
| 440 | greth_write_bd(&bdp->stat, status); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 441 | spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 442 | greth_enable_tx(greth); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 443 | spin_unlock_irqrestore(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 444 | |
| 445 | out: |
| 446 | dev_kfree_skb(skb); |
| 447 | return err; |
| 448 | } |
| 449 | |
| 450 | |
kirjanov@gmail.com | 41a655b | 2010-02-24 10:25:33 +0000 | [diff] [blame] | 451 | static netdev_tx_t |
| 452 | greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev) |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 453 | { |
| 454 | struct greth_private *greth = netdev_priv(dev); |
| 455 | struct greth_bd *bdp; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 456 | u32 status = 0, dma_addr, ctrl; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 457 | int curr_tx, nr_frags, i, err = NETDEV_TX_OK; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 458 | unsigned long flags; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 459 | |
| 460 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 461 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 462 | /* Clean TX Ring */ |
| 463 | greth_clean_tx_gbit(dev); |
| 464 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 465 | if (greth->tx_free < nr_frags + 1) { |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 466 | spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/ |
| 467 | ctrl = GRETH_REGLOAD(greth->regs->control); |
| 468 | /* Enable TX IRQ only if not already in poll() routine */ |
| 469 | if (ctrl & GRETH_RXI) |
| 470 | GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 471 | netif_stop_queue(dev); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 472 | spin_unlock_irqrestore(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 473 | err = NETDEV_TX_BUSY; |
| 474 | goto out; |
| 475 | } |
| 476 | |
| 477 | if (netif_msg_pktdata(greth)) |
| 478 | greth_print_tx_packet(skb); |
| 479 | |
| 480 | if (unlikely(skb->len > MAX_FRAME_SIZE)) { |
| 481 | dev->stats.tx_errors++; |
| 482 | goto out; |
| 483 | } |
| 484 | |
| 485 | /* Save skb pointer. */ |
| 486 | greth->tx_skbuff[greth->tx_next] = skb; |
| 487 | |
| 488 | /* Linear buf */ |
| 489 | if (nr_frags != 0) |
| 490 | status = GRETH_TXBD_MORE; |
| 491 | |
| 492 | status |= GRETH_TXBD_CSALL; |
| 493 | status |= skb_headlen(skb) & GRETH_BD_LEN; |
| 494 | if (greth->tx_next == GRETH_TXBD_NUM_MASK) |
| 495 | status |= GRETH_BD_WR; |
| 496 | |
| 497 | |
| 498 | bdp = greth->tx_bd_base + greth->tx_next; |
| 499 | greth_write_bd(&bdp->stat, status); |
| 500 | dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE); |
| 501 | |
| 502 | if (unlikely(dma_mapping_error(greth->dev, dma_addr))) |
| 503 | goto map_error; |
| 504 | |
| 505 | greth_write_bd(&bdp->addr, dma_addr); |
| 506 | |
| 507 | curr_tx = NEXT_TX(greth->tx_next); |
| 508 | |
| 509 | /* Frags */ |
| 510 | for (i = 0; i < nr_frags; i++) { |
| 511 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 512 | greth->tx_skbuff[curr_tx] = NULL; |
| 513 | bdp = greth->tx_bd_base + curr_tx; |
| 514 | |
Daniel Hellstrom | 2a2bc01 | 2011-01-14 03:02:39 +0000 | [diff] [blame] | 515 | status = GRETH_TXBD_CSALL | GRETH_BD_EN; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 516 | status |= frag->size & GRETH_BD_LEN; |
| 517 | |
| 518 | /* Wrap around descriptor ring */ |
| 519 | if (curr_tx == GRETH_TXBD_NUM_MASK) |
| 520 | status |= GRETH_BD_WR; |
| 521 | |
| 522 | /* More fragments left */ |
| 523 | if (i < nr_frags - 1) |
| 524 | status |= GRETH_TXBD_MORE; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 525 | else |
| 526 | status |= GRETH_BD_IE; /* enable IRQ on last fragment */ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 527 | |
| 528 | greth_write_bd(&bdp->stat, status); |
| 529 | |
| 530 | dma_addr = dma_map_page(greth->dev, |
| 531 | frag->page, |
| 532 | frag->page_offset, |
| 533 | frag->size, |
| 534 | DMA_TO_DEVICE); |
| 535 | |
| 536 | if (unlikely(dma_mapping_error(greth->dev, dma_addr))) |
| 537 | goto frag_map_error; |
| 538 | |
| 539 | greth_write_bd(&bdp->addr, dma_addr); |
| 540 | |
| 541 | curr_tx = NEXT_TX(curr_tx); |
| 542 | } |
| 543 | |
| 544 | wmb(); |
| 545 | |
Daniel Hellstrom | 2a2bc01 | 2011-01-14 03:02:39 +0000 | [diff] [blame] | 546 | /* Enable the descriptor chain by enabling the first descriptor */ |
| 547 | bdp = greth->tx_bd_base + greth->tx_next; |
| 548 | greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN); |
| 549 | greth->tx_next = curr_tx; |
| 550 | greth->tx_free -= nr_frags + 1; |
| 551 | |
| 552 | wmb(); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 553 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 554 | spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 555 | greth_enable_tx(greth); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 556 | spin_unlock_irqrestore(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 557 | |
| 558 | return NETDEV_TX_OK; |
| 559 | |
| 560 | frag_map_error: |
Daniel Hellstrom | 2a2bc01 | 2011-01-14 03:02:39 +0000 | [diff] [blame] | 561 | /* Unmap SKB mappings that succeeded and disable descriptor */ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 562 | for (i = 0; greth->tx_next + i != curr_tx; i++) { |
| 563 | bdp = greth->tx_bd_base + greth->tx_next + i; |
| 564 | dma_unmap_single(greth->dev, |
| 565 | greth_read_bd(&bdp->addr), |
| 566 | greth_read_bd(&bdp->stat) & GRETH_BD_LEN, |
| 567 | DMA_TO_DEVICE); |
Daniel Hellstrom | 2a2bc01 | 2011-01-14 03:02:39 +0000 | [diff] [blame] | 568 | greth_write_bd(&bdp->stat, 0); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 569 | } |
| 570 | map_error: |
| 571 | if (net_ratelimit()) |
| 572 | dev_warn(greth->dev, "Could not create TX DMA mapping\n"); |
| 573 | dev_kfree_skb(skb); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 574 | out: |
| 575 | return err; |
| 576 | } |
| 577 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 578 | static irqreturn_t greth_interrupt(int irq, void *dev_id) |
| 579 | { |
| 580 | struct net_device *dev = dev_id; |
| 581 | struct greth_private *greth; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 582 | u32 status, ctrl; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 583 | irqreturn_t retval = IRQ_NONE; |
| 584 | |
| 585 | greth = netdev_priv(dev); |
| 586 | |
| 587 | spin_lock(&greth->devlock); |
| 588 | |
| 589 | /* Get the interrupt events that caused us to be here. */ |
| 590 | status = GRETH_REGLOAD(greth->regs->status); |
| 591 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 592 | /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be |
| 593 | * set regardless of whether IRQ is enabled or not. Especially |
| 594 | * important when shared IRQ. |
| 595 | */ |
| 596 | ctrl = GRETH_REGLOAD(greth->regs->control); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 597 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 598 | /* Handle rx and tx interrupts through poll */ |
| 599 | if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) || |
| 600 | ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) { |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 601 | retval = IRQ_HANDLED; |
| 602 | |
| 603 | /* Disable interrupts and schedule poll() */ |
| 604 | greth_disable_irqs(greth); |
| 605 | napi_schedule(&greth->napi); |
| 606 | } |
| 607 | |
| 608 | mmiowb(); |
| 609 | spin_unlock(&greth->devlock); |
| 610 | |
| 611 | return retval; |
| 612 | } |
| 613 | |
| 614 | static void greth_clean_tx(struct net_device *dev) |
| 615 | { |
| 616 | struct greth_private *greth; |
| 617 | struct greth_bd *bdp; |
| 618 | u32 stat; |
| 619 | |
| 620 | greth = netdev_priv(dev); |
| 621 | |
| 622 | while (1) { |
| 623 | bdp = greth->tx_bd_base + greth->tx_last; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 624 | GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX); |
| 625 | mb(); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 626 | stat = greth_read_bd(&bdp->stat); |
| 627 | |
| 628 | if (unlikely(stat & GRETH_BD_EN)) |
| 629 | break; |
| 630 | |
| 631 | if (greth->tx_free == GRETH_TXBD_NUM) |
| 632 | break; |
| 633 | |
| 634 | /* Check status for errors */ |
| 635 | if (unlikely(stat & GRETH_TXBD_STATUS)) { |
| 636 | dev->stats.tx_errors++; |
| 637 | if (stat & GRETH_TXBD_ERR_AL) |
| 638 | dev->stats.tx_aborted_errors++; |
| 639 | if (stat & GRETH_TXBD_ERR_UE) |
| 640 | dev->stats.tx_fifo_errors++; |
| 641 | } |
| 642 | dev->stats.tx_packets++; |
| 643 | greth->tx_last = NEXT_TX(greth->tx_last); |
| 644 | greth->tx_free++; |
| 645 | } |
| 646 | |
| 647 | if (greth->tx_free > 0) { |
| 648 | netif_wake_queue(dev); |
| 649 | } |
| 650 | |
| 651 | } |
| 652 | |
| 653 | static inline void greth_update_tx_stats(struct net_device *dev, u32 stat) |
| 654 | { |
| 655 | /* Check status for errors */ |
| 656 | if (unlikely(stat & GRETH_TXBD_STATUS)) { |
| 657 | dev->stats.tx_errors++; |
| 658 | if (stat & GRETH_TXBD_ERR_AL) |
| 659 | dev->stats.tx_aborted_errors++; |
| 660 | if (stat & GRETH_TXBD_ERR_UE) |
| 661 | dev->stats.tx_fifo_errors++; |
| 662 | if (stat & GRETH_TXBD_ERR_LC) |
| 663 | dev->stats.tx_aborted_errors++; |
| 664 | } |
| 665 | dev->stats.tx_packets++; |
| 666 | } |
| 667 | |
| 668 | static void greth_clean_tx_gbit(struct net_device *dev) |
| 669 | { |
| 670 | struct greth_private *greth; |
| 671 | struct greth_bd *bdp, *bdp_last_frag; |
| 672 | struct sk_buff *skb; |
| 673 | u32 stat; |
| 674 | int nr_frags, i; |
| 675 | |
| 676 | greth = netdev_priv(dev); |
| 677 | |
| 678 | while (greth->tx_free < GRETH_TXBD_NUM) { |
| 679 | |
| 680 | skb = greth->tx_skbuff[greth->tx_last]; |
| 681 | |
| 682 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 683 | |
| 684 | /* We only clean fully completed SKBs */ |
| 685 | bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 686 | |
| 687 | GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX); |
| 688 | mb(); |
| 689 | stat = greth_read_bd(&bdp_last_frag->stat); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 690 | |
| 691 | if (stat & GRETH_BD_EN) |
| 692 | break; |
| 693 | |
| 694 | greth->tx_skbuff[greth->tx_last] = NULL; |
| 695 | |
| 696 | greth_update_tx_stats(dev, stat); |
| 697 | |
| 698 | bdp = greth->tx_bd_base + greth->tx_last; |
| 699 | |
| 700 | greth->tx_last = NEXT_TX(greth->tx_last); |
| 701 | |
| 702 | dma_unmap_single(greth->dev, |
| 703 | greth_read_bd(&bdp->addr), |
| 704 | skb_headlen(skb), |
| 705 | DMA_TO_DEVICE); |
| 706 | |
| 707 | for (i = 0; i < nr_frags; i++) { |
| 708 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 709 | bdp = greth->tx_bd_base + greth->tx_last; |
| 710 | |
| 711 | dma_unmap_page(greth->dev, |
| 712 | greth_read_bd(&bdp->addr), |
| 713 | frag->size, |
| 714 | DMA_TO_DEVICE); |
| 715 | |
| 716 | greth->tx_last = NEXT_TX(greth->tx_last); |
| 717 | } |
| 718 | greth->tx_free += nr_frags+1; |
| 719 | dev_kfree_skb(skb); |
| 720 | } |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 721 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 722 | if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1))) |
| 723 | netif_wake_queue(dev); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static int greth_rx(struct net_device *dev, int limit) |
| 727 | { |
| 728 | struct greth_private *greth; |
| 729 | struct greth_bd *bdp; |
| 730 | struct sk_buff *skb; |
| 731 | int pkt_len; |
| 732 | int bad, count; |
| 733 | u32 status, dma_addr; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 734 | unsigned long flags; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 735 | |
| 736 | greth = netdev_priv(dev); |
| 737 | |
| 738 | for (count = 0; count < limit; ++count) { |
| 739 | |
| 740 | bdp = greth->rx_bd_base + greth->rx_cur; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 741 | GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX); |
| 742 | mb(); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 743 | status = greth_read_bd(&bdp->stat); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 744 | |
| 745 | if (unlikely(status & GRETH_BD_EN)) { |
| 746 | break; |
| 747 | } |
| 748 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 749 | dma_addr = greth_read_bd(&bdp->addr); |
| 750 | bad = 0; |
| 751 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 752 | /* Check status for errors. */ |
| 753 | if (unlikely(status & GRETH_RXBD_STATUS)) { |
| 754 | if (status & GRETH_RXBD_ERR_FT) { |
| 755 | dev->stats.rx_length_errors++; |
| 756 | bad = 1; |
| 757 | } |
| 758 | if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) { |
| 759 | dev->stats.rx_frame_errors++; |
| 760 | bad = 1; |
| 761 | } |
| 762 | if (status & GRETH_RXBD_ERR_CRC) { |
| 763 | dev->stats.rx_crc_errors++; |
| 764 | bad = 1; |
| 765 | } |
| 766 | } |
| 767 | if (unlikely(bad)) { |
| 768 | dev->stats.rx_errors++; |
| 769 | |
| 770 | } else { |
| 771 | |
| 772 | pkt_len = status & GRETH_BD_LEN; |
| 773 | |
| 774 | skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN); |
| 775 | |
| 776 | if (unlikely(skb == NULL)) { |
| 777 | |
| 778 | if (net_ratelimit()) |
| 779 | dev_warn(&dev->dev, "low on memory - " "packet dropped\n"); |
| 780 | |
| 781 | dev->stats.rx_dropped++; |
| 782 | |
| 783 | } else { |
| 784 | skb_reserve(skb, NET_IP_ALIGN); |
| 785 | skb->dev = dev; |
| 786 | |
| 787 | dma_sync_single_for_cpu(greth->dev, |
| 788 | dma_addr, |
| 789 | pkt_len, |
| 790 | DMA_FROM_DEVICE); |
| 791 | |
| 792 | if (netif_msg_pktdata(greth)) |
| 793 | greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len); |
| 794 | |
| 795 | memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len); |
| 796 | |
| 797 | skb->protocol = eth_type_trans(skb, dev); |
| 798 | dev->stats.rx_packets++; |
| 799 | netif_receive_skb(skb); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | status = GRETH_BD_EN | GRETH_BD_IE; |
| 804 | if (greth->rx_cur == GRETH_RXBD_NUM_MASK) { |
| 805 | status |= GRETH_BD_WR; |
| 806 | } |
| 807 | |
| 808 | wmb(); |
| 809 | greth_write_bd(&bdp->stat, status); |
| 810 | |
| 811 | dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE); |
| 812 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 813 | spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 814 | greth_enable_rx(greth); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 815 | spin_unlock_irqrestore(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 816 | |
| 817 | greth->rx_cur = NEXT_RX(greth->rx_cur); |
| 818 | } |
| 819 | |
| 820 | return count; |
| 821 | } |
| 822 | |
| 823 | static inline int hw_checksummed(u32 status) |
| 824 | { |
| 825 | |
| 826 | if (status & GRETH_RXBD_IP_FRAG) |
| 827 | return 0; |
| 828 | |
| 829 | if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR) |
| 830 | return 0; |
| 831 | |
| 832 | if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR) |
| 833 | return 0; |
| 834 | |
| 835 | if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR) |
| 836 | return 0; |
| 837 | |
| 838 | return 1; |
| 839 | } |
| 840 | |
| 841 | static int greth_rx_gbit(struct net_device *dev, int limit) |
| 842 | { |
| 843 | struct greth_private *greth; |
| 844 | struct greth_bd *bdp; |
| 845 | struct sk_buff *skb, *newskb; |
| 846 | int pkt_len; |
| 847 | int bad, count = 0; |
| 848 | u32 status, dma_addr; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 849 | unsigned long flags; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 850 | |
| 851 | greth = netdev_priv(dev); |
| 852 | |
| 853 | for (count = 0; count < limit; ++count) { |
| 854 | |
| 855 | bdp = greth->rx_bd_base + greth->rx_cur; |
| 856 | skb = greth->rx_skbuff[greth->rx_cur]; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 857 | GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX); |
| 858 | mb(); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 859 | status = greth_read_bd(&bdp->stat); |
| 860 | bad = 0; |
| 861 | |
| 862 | if (status & GRETH_BD_EN) |
| 863 | break; |
| 864 | |
| 865 | /* Check status for errors. */ |
| 866 | if (unlikely(status & GRETH_RXBD_STATUS)) { |
| 867 | |
| 868 | if (status & GRETH_RXBD_ERR_FT) { |
| 869 | dev->stats.rx_length_errors++; |
| 870 | bad = 1; |
| 871 | } else if (status & |
| 872 | (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) { |
| 873 | dev->stats.rx_frame_errors++; |
| 874 | bad = 1; |
| 875 | } else if (status & GRETH_RXBD_ERR_CRC) { |
| 876 | dev->stats.rx_crc_errors++; |
| 877 | bad = 1; |
| 878 | } |
| 879 | } |
| 880 | |
Daniel Hellstrom | b669e7f | 2011-01-14 03:02:40 +0000 | [diff] [blame] | 881 | /* Allocate new skb to replace current, not needed if the |
| 882 | * current skb can be reused */ |
| 883 | if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) { |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 884 | skb_reserve(newskb, NET_IP_ALIGN); |
| 885 | |
| 886 | dma_addr = dma_map_single(greth->dev, |
| 887 | newskb->data, |
| 888 | MAX_FRAME_SIZE + NET_IP_ALIGN, |
| 889 | DMA_FROM_DEVICE); |
| 890 | |
| 891 | if (!dma_mapping_error(greth->dev, dma_addr)) { |
| 892 | /* Process the incoming frame. */ |
| 893 | pkt_len = status & GRETH_BD_LEN; |
| 894 | |
| 895 | dma_unmap_single(greth->dev, |
| 896 | greth_read_bd(&bdp->addr), |
| 897 | MAX_FRAME_SIZE + NET_IP_ALIGN, |
| 898 | DMA_FROM_DEVICE); |
| 899 | |
| 900 | if (netif_msg_pktdata(greth)) |
| 901 | greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len); |
| 902 | |
| 903 | skb_put(skb, pkt_len); |
| 904 | |
Michał Mirosław | 131ae32 | 2011-04-17 00:15:47 +0000 | [diff] [blame] | 905 | if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status)) |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 906 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 907 | else |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 908 | skb_checksum_none_assert(skb); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 909 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 910 | skb->protocol = eth_type_trans(skb, dev); |
| 911 | dev->stats.rx_packets++; |
| 912 | netif_receive_skb(skb); |
| 913 | |
| 914 | greth->rx_skbuff[greth->rx_cur] = newskb; |
| 915 | greth_write_bd(&bdp->addr, dma_addr); |
| 916 | } else { |
| 917 | if (net_ratelimit()) |
| 918 | dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n"); |
| 919 | dev_kfree_skb(newskb); |
Daniel Hellstrom | b669e7f | 2011-01-14 03:02:40 +0000 | [diff] [blame] | 920 | /* reusing current skb, so it is a drop */ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 921 | dev->stats.rx_dropped++; |
| 922 | } |
Daniel Hellstrom | b669e7f | 2011-01-14 03:02:40 +0000 | [diff] [blame] | 923 | } else if (bad) { |
| 924 | /* Bad Frame transfer, the skb is reused */ |
| 925 | dev->stats.rx_dropped++; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 926 | } else { |
Daniel Hellstrom | b669e7f | 2011-01-14 03:02:40 +0000 | [diff] [blame] | 927 | /* Failed Allocating a new skb. This is rather stupid |
| 928 | * but the current "filled" skb is reused, as if |
| 929 | * transfer failure. One could argue that RX descriptor |
| 930 | * table handling should be divided into cleaning and |
| 931 | * filling as the TX part of the driver |
| 932 | */ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 933 | if (net_ratelimit()) |
| 934 | dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n"); |
Daniel Hellstrom | b669e7f | 2011-01-14 03:02:40 +0000 | [diff] [blame] | 935 | /* reusing current skb, so it is a drop */ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 936 | dev->stats.rx_dropped++; |
| 937 | } |
| 938 | |
| 939 | status = GRETH_BD_EN | GRETH_BD_IE; |
| 940 | if (greth->rx_cur == GRETH_RXBD_NUM_MASK) { |
| 941 | status |= GRETH_BD_WR; |
| 942 | } |
| 943 | |
| 944 | wmb(); |
| 945 | greth_write_bd(&bdp->stat, status); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 946 | spin_lock_irqsave(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 947 | greth_enable_rx(greth); |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 948 | spin_unlock_irqrestore(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 949 | greth->rx_cur = NEXT_RX(greth->rx_cur); |
| 950 | } |
| 951 | |
| 952 | return count; |
| 953 | |
| 954 | } |
| 955 | |
| 956 | static int greth_poll(struct napi_struct *napi, int budget) |
| 957 | { |
| 958 | struct greth_private *greth; |
| 959 | int work_done = 0; |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 960 | unsigned long flags; |
| 961 | u32 mask, ctrl; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 962 | greth = container_of(napi, struct greth_private, napi); |
| 963 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 964 | restart_txrx_poll: |
| 965 | if (netif_queue_stopped(greth->netdev)) { |
| 966 | if (greth->gbit_mac) |
| 967 | greth_clean_tx_gbit(greth->netdev); |
| 968 | else |
| 969 | greth_clean_tx(greth->netdev); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 972 | if (greth->gbit_mac) { |
| 973 | work_done += greth_rx_gbit(greth->netdev, budget - work_done); |
| 974 | } else { |
| 975 | work_done += greth_rx(greth->netdev, budget - work_done); |
| 976 | } |
| 977 | |
| 978 | if (work_done < budget) { |
| 979 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 980 | spin_lock_irqsave(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 981 | |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 982 | ctrl = GRETH_REGLOAD(greth->regs->control); |
| 983 | if (netif_queue_stopped(greth->netdev)) { |
| 984 | GRETH_REGSAVE(greth->regs->control, |
| 985 | ctrl | GRETH_TXI | GRETH_RXI); |
| 986 | mask = GRETH_INT_RX | GRETH_INT_RE | |
| 987 | GRETH_INT_TX | GRETH_INT_TE; |
| 988 | } else { |
| 989 | GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI); |
| 990 | mask = GRETH_INT_RX | GRETH_INT_RE; |
| 991 | } |
| 992 | |
| 993 | if (GRETH_REGLOAD(greth->regs->status) & mask) { |
| 994 | GRETH_REGSAVE(greth->regs->control, ctrl); |
| 995 | spin_unlock_irqrestore(&greth->devlock, flags); |
| 996 | goto restart_txrx_poll; |
| 997 | } else { |
| 998 | __napi_complete(napi); |
| 999 | spin_unlock_irqrestore(&greth->devlock, flags); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1003 | return work_done; |
| 1004 | } |
| 1005 | |
| 1006 | static int greth_set_mac_add(struct net_device *dev, void *p) |
| 1007 | { |
| 1008 | struct sockaddr *addr = p; |
| 1009 | struct greth_private *greth; |
| 1010 | struct greth_regs *regs; |
| 1011 | |
kirjanov@gmail.com | 6e03718 | 2010-02-19 05:00:52 +0000 | [diff] [blame] | 1012 | greth = netdev_priv(dev); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1013 | regs = (struct greth_regs *) greth->regs; |
| 1014 | |
| 1015 | if (!is_valid_ether_addr(addr->sa_data)) |
| 1016 | return -EINVAL; |
| 1017 | |
| 1018 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
| 1019 | |
| 1020 | GRETH_REGSAVE(regs->esa_msb, addr->sa_data[0] << 8 | addr->sa_data[1]); |
| 1021 | GRETH_REGSAVE(regs->esa_lsb, |
| 1022 | addr->sa_data[2] << 24 | addr-> |
| 1023 | sa_data[3] << 16 | addr->sa_data[4] << 8 | addr->sa_data[5]); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | static u32 greth_hash_get_index(__u8 *addr) |
| 1028 | { |
| 1029 | return (ether_crc(6, addr)) & 0x3F; |
| 1030 | } |
| 1031 | |
| 1032 | static void greth_set_hash_filter(struct net_device *dev) |
| 1033 | { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1034 | struct netdev_hw_addr *ha; |
kirjanov@gmail.com | 6e03718 | 2010-02-19 05:00:52 +0000 | [diff] [blame] | 1035 | struct greth_private *greth = netdev_priv(dev); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1036 | struct greth_regs *regs = (struct greth_regs *) greth->regs; |
| 1037 | u32 mc_filter[2]; |
kirjanov@gmail.com | 6e03718 | 2010-02-19 05:00:52 +0000 | [diff] [blame] | 1038 | unsigned int bitnr; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1039 | |
| 1040 | mc_filter[0] = mc_filter[1] = 0; |
| 1041 | |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1042 | netdev_for_each_mc_addr(ha, dev) { |
| 1043 | bitnr = greth_hash_get_index(ha->addr); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1044 | mc_filter[bitnr >> 5] |= 1 << (bitnr & 31); |
| 1045 | } |
| 1046 | |
| 1047 | GRETH_REGSAVE(regs->hash_msb, mc_filter[1]); |
| 1048 | GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]); |
| 1049 | } |
| 1050 | |
| 1051 | static void greth_set_multicast_list(struct net_device *dev) |
| 1052 | { |
| 1053 | int cfg; |
| 1054 | struct greth_private *greth = netdev_priv(dev); |
| 1055 | struct greth_regs *regs = (struct greth_regs *) greth->regs; |
| 1056 | |
| 1057 | cfg = GRETH_REGLOAD(regs->control); |
| 1058 | if (dev->flags & IFF_PROMISC) |
| 1059 | cfg |= GRETH_CTRL_PR; |
| 1060 | else |
| 1061 | cfg &= ~GRETH_CTRL_PR; |
| 1062 | |
| 1063 | if (greth->multicast) { |
| 1064 | if (dev->flags & IFF_ALLMULTI) { |
| 1065 | GRETH_REGSAVE(regs->hash_msb, -1); |
| 1066 | GRETH_REGSAVE(regs->hash_lsb, -1); |
| 1067 | cfg |= GRETH_CTRL_MCEN; |
| 1068 | GRETH_REGSAVE(regs->control, cfg); |
| 1069 | return; |
| 1070 | } |
| 1071 | |
kirjanov@gmail.com | 6e03718 | 2010-02-19 05:00:52 +0000 | [diff] [blame] | 1072 | if (netdev_mc_empty(dev)) { |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1073 | cfg &= ~GRETH_CTRL_MCEN; |
| 1074 | GRETH_REGSAVE(regs->control, cfg); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | /* Setup multicast filter */ |
| 1079 | greth_set_hash_filter(dev); |
| 1080 | cfg |= GRETH_CTRL_MCEN; |
| 1081 | } |
| 1082 | GRETH_REGSAVE(regs->control, cfg); |
| 1083 | } |
| 1084 | |
| 1085 | static u32 greth_get_msglevel(struct net_device *dev) |
| 1086 | { |
| 1087 | struct greth_private *greth = netdev_priv(dev); |
| 1088 | return greth->msg_enable; |
| 1089 | } |
| 1090 | |
| 1091 | static void greth_set_msglevel(struct net_device *dev, u32 value) |
| 1092 | { |
| 1093 | struct greth_private *greth = netdev_priv(dev); |
| 1094 | greth->msg_enable = value; |
| 1095 | } |
| 1096 | static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1097 | { |
| 1098 | struct greth_private *greth = netdev_priv(dev); |
| 1099 | struct phy_device *phy = greth->phy; |
| 1100 | |
| 1101 | if (!phy) |
| 1102 | return -ENODEV; |
| 1103 | |
| 1104 | return phy_ethtool_gset(phy, cmd); |
| 1105 | } |
| 1106 | |
| 1107 | static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1108 | { |
| 1109 | struct greth_private *greth = netdev_priv(dev); |
| 1110 | struct phy_device *phy = greth->phy; |
| 1111 | |
| 1112 | if (!phy) |
| 1113 | return -ENODEV; |
| 1114 | |
| 1115 | return phy_ethtool_sset(phy, cmd); |
| 1116 | } |
| 1117 | |
| 1118 | static int greth_get_regs_len(struct net_device *dev) |
| 1119 | { |
| 1120 | return sizeof(struct greth_regs); |
| 1121 | } |
| 1122 | |
| 1123 | static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 1124 | { |
| 1125 | struct greth_private *greth = netdev_priv(dev); |
| 1126 | |
| 1127 | strncpy(info->driver, dev_driver_string(greth->dev), 32); |
| 1128 | strncpy(info->version, "revision: 1.0", 32); |
| 1129 | strncpy(info->bus_info, greth->dev->bus->name, 32); |
| 1130 | strncpy(info->fw_version, "N/A", 32); |
| 1131 | info->eedump_len = 0; |
| 1132 | info->regdump_len = sizeof(struct greth_regs); |
| 1133 | } |
| 1134 | |
| 1135 | static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p) |
| 1136 | { |
| 1137 | int i; |
| 1138 | struct greth_private *greth = netdev_priv(dev); |
| 1139 | u32 __iomem *greth_regs = (u32 __iomem *) greth->regs; |
| 1140 | u32 *buff = p; |
| 1141 | |
| 1142 | for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++) |
| 1143 | buff[i] = greth_read_bd(&greth_regs[i]); |
| 1144 | } |
| 1145 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1146 | static const struct ethtool_ops greth_ethtool_ops = { |
| 1147 | .get_msglevel = greth_get_msglevel, |
| 1148 | .set_msglevel = greth_set_msglevel, |
| 1149 | .get_settings = greth_get_settings, |
| 1150 | .set_settings = greth_set_settings, |
| 1151 | .get_drvinfo = greth_get_drvinfo, |
| 1152 | .get_regs_len = greth_get_regs_len, |
| 1153 | .get_regs = greth_get_regs, |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1154 | .get_link = ethtool_op_get_link, |
| 1155 | }; |
| 1156 | |
| 1157 | static struct net_device_ops greth_netdev_ops = { |
Daniel Hellstrom | 0f73f2c | 2011-01-14 03:02:43 +0000 | [diff] [blame] | 1158 | .ndo_open = greth_open, |
| 1159 | .ndo_stop = greth_close, |
| 1160 | .ndo_start_xmit = greth_start_xmit, |
| 1161 | .ndo_set_mac_address = greth_set_mac_add, |
| 1162 | .ndo_validate_addr = eth_validate_addr, |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1163 | }; |
| 1164 | |
| 1165 | static inline int wait_for_mdio(struct greth_private *greth) |
| 1166 | { |
| 1167 | unsigned long timeout = jiffies + 4*HZ/100; |
| 1168 | while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) { |
| 1169 | if (time_after(jiffies, timeout)) |
| 1170 | return 0; |
| 1171 | } |
| 1172 | return 1; |
| 1173 | } |
| 1174 | |
| 1175 | static int greth_mdio_read(struct mii_bus *bus, int phy, int reg) |
| 1176 | { |
| 1177 | struct greth_private *greth = bus->priv; |
| 1178 | int data; |
| 1179 | |
| 1180 | if (!wait_for_mdio(greth)) |
| 1181 | return -EBUSY; |
| 1182 | |
| 1183 | GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2); |
| 1184 | |
| 1185 | if (!wait_for_mdio(greth)) |
| 1186 | return -EBUSY; |
| 1187 | |
| 1188 | if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) { |
| 1189 | data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF; |
| 1190 | return data; |
| 1191 | |
| 1192 | } else { |
| 1193 | return -1; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val) |
| 1198 | { |
| 1199 | struct greth_private *greth = bus->priv; |
| 1200 | |
| 1201 | if (!wait_for_mdio(greth)) |
| 1202 | return -EBUSY; |
| 1203 | |
| 1204 | GRETH_REGSAVE(greth->regs->mdio, |
| 1205 | ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1); |
| 1206 | |
| 1207 | if (!wait_for_mdio(greth)) |
| 1208 | return -EBUSY; |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | static int greth_mdio_reset(struct mii_bus *bus) |
| 1214 | { |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | static void greth_link_change(struct net_device *dev) |
| 1219 | { |
| 1220 | struct greth_private *greth = netdev_priv(dev); |
| 1221 | struct phy_device *phydev = greth->phy; |
| 1222 | unsigned long flags; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1223 | int status_change = 0; |
Daniel Hellstrom | 2436af8 | 2011-01-14 03:02:41 +0000 | [diff] [blame] | 1224 | u32 ctrl; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1225 | |
| 1226 | spin_lock_irqsave(&greth->devlock, flags); |
| 1227 | |
| 1228 | if (phydev->link) { |
| 1229 | |
| 1230 | if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) { |
Daniel Hellstrom | 2436af8 | 2011-01-14 03:02:41 +0000 | [diff] [blame] | 1231 | ctrl = GRETH_REGLOAD(greth->regs->control) & |
| 1232 | ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (phydev->duplex) |
Daniel Hellstrom | 2436af8 | 2011-01-14 03:02:41 +0000 | [diff] [blame] | 1235 | ctrl |= GRETH_CTRL_FD; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1236 | |
Daniel Hellstrom | 2436af8 | 2011-01-14 03:02:41 +0000 | [diff] [blame] | 1237 | if (phydev->speed == SPEED_100) |
| 1238 | ctrl |= GRETH_CTRL_SP; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1239 | else if (phydev->speed == SPEED_1000) |
Daniel Hellstrom | 2436af8 | 2011-01-14 03:02:41 +0000 | [diff] [blame] | 1240 | ctrl |= GRETH_CTRL_GB; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1241 | |
Daniel Hellstrom | 2436af8 | 2011-01-14 03:02:41 +0000 | [diff] [blame] | 1242 | GRETH_REGSAVE(greth->regs->control, ctrl); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1243 | greth->speed = phydev->speed; |
| 1244 | greth->duplex = phydev->duplex; |
| 1245 | status_change = 1; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | if (phydev->link != greth->link) { |
| 1250 | if (!phydev->link) { |
| 1251 | greth->speed = 0; |
| 1252 | greth->duplex = -1; |
| 1253 | } |
| 1254 | greth->link = phydev->link; |
| 1255 | |
| 1256 | status_change = 1; |
| 1257 | } |
| 1258 | |
| 1259 | spin_unlock_irqrestore(&greth->devlock, flags); |
| 1260 | |
| 1261 | if (status_change) { |
| 1262 | if (phydev->link) |
| 1263 | pr_debug("%s: link up (%d/%s)\n", |
| 1264 | dev->name, phydev->speed, |
| 1265 | DUPLEX_FULL == phydev->duplex ? "Full" : "Half"); |
| 1266 | else |
| 1267 | pr_debug("%s: link down\n", dev->name); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | static int greth_mdio_probe(struct net_device *dev) |
| 1272 | { |
| 1273 | struct greth_private *greth = netdev_priv(dev); |
| 1274 | struct phy_device *phy = NULL; |
kirjanov@gmail.com | 6e03718 | 2010-02-19 05:00:52 +0000 | [diff] [blame] | 1275 | int ret; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1276 | |
| 1277 | /* Find the first PHY */ |
kirjanov@gmail.com | 6e03718 | 2010-02-19 05:00:52 +0000 | [diff] [blame] | 1278 | phy = phy_find_first(greth->mdio); |
| 1279 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1280 | if (!phy) { |
| 1281 | if (netif_msg_probe(greth)) |
| 1282 | dev_err(&dev->dev, "no PHY found\n"); |
| 1283 | return -ENXIO; |
| 1284 | } |
| 1285 | |
kirjanov@gmail.com | 6e03718 | 2010-02-19 05:00:52 +0000 | [diff] [blame] | 1286 | ret = phy_connect_direct(dev, phy, &greth_link_change, |
| 1287 | 0, greth->gbit_mac ? |
| 1288 | PHY_INTERFACE_MODE_GMII : |
| 1289 | PHY_INTERFACE_MODE_MII); |
| 1290 | if (ret) { |
| 1291 | if (netif_msg_ifup(greth)) |
| 1292 | dev_err(&dev->dev, "could not attach to PHY\n"); |
| 1293 | return ret; |
| 1294 | } |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1295 | |
| 1296 | if (greth->gbit_mac) |
| 1297 | phy->supported &= PHY_GBIT_FEATURES; |
| 1298 | else |
| 1299 | phy->supported &= PHY_BASIC_FEATURES; |
| 1300 | |
| 1301 | phy->advertising = phy->supported; |
| 1302 | |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1303 | greth->link = 0; |
| 1304 | greth->speed = 0; |
| 1305 | greth->duplex = -1; |
| 1306 | greth->phy = phy; |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | static inline int phy_aneg_done(struct phy_device *phydev) |
| 1312 | { |
| 1313 | int retval; |
| 1314 | |
| 1315 | retval = phy_read(phydev, MII_BMSR); |
| 1316 | |
| 1317 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); |
| 1318 | } |
| 1319 | |
| 1320 | static int greth_mdio_init(struct greth_private *greth) |
| 1321 | { |
| 1322 | int ret, phy; |
| 1323 | unsigned long timeout; |
| 1324 | |
| 1325 | greth->mdio = mdiobus_alloc(); |
| 1326 | if (!greth->mdio) { |
| 1327 | return -ENOMEM; |
| 1328 | } |
| 1329 | |
| 1330 | greth->mdio->name = "greth-mdio"; |
| 1331 | snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq); |
| 1332 | greth->mdio->read = greth_mdio_read; |
| 1333 | greth->mdio->write = greth_mdio_write; |
| 1334 | greth->mdio->reset = greth_mdio_reset; |
| 1335 | greth->mdio->priv = greth; |
| 1336 | |
| 1337 | greth->mdio->irq = greth->mdio_irqs; |
| 1338 | |
| 1339 | for (phy = 0; phy < PHY_MAX_ADDR; phy++) |
| 1340 | greth->mdio->irq[phy] = PHY_POLL; |
| 1341 | |
| 1342 | ret = mdiobus_register(greth->mdio); |
| 1343 | if (ret) { |
| 1344 | goto error; |
| 1345 | } |
| 1346 | |
| 1347 | ret = greth_mdio_probe(greth->netdev); |
| 1348 | if (ret) { |
| 1349 | if (netif_msg_probe(greth)) |
| 1350 | dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n"); |
| 1351 | goto unreg_mdio; |
| 1352 | } |
| 1353 | |
| 1354 | phy_start(greth->phy); |
| 1355 | |
| 1356 | /* If Ethernet debug link is used make autoneg happen right away */ |
| 1357 | if (greth->edcl && greth_edcl == 1) { |
| 1358 | phy_start_aneg(greth->phy); |
| 1359 | timeout = jiffies + 6*HZ; |
| 1360 | while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) { |
| 1361 | } |
| 1362 | genphy_read_status(greth->phy); |
| 1363 | greth_link_change(greth->netdev); |
| 1364 | } |
| 1365 | |
| 1366 | return 0; |
| 1367 | |
| 1368 | unreg_mdio: |
| 1369 | mdiobus_unregister(greth->mdio); |
| 1370 | error: |
| 1371 | mdiobus_free(greth->mdio); |
| 1372 | return ret; |
| 1373 | } |
| 1374 | |
| 1375 | /* Initialize the GRETH MAC */ |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 1376 | static int __devinit greth_of_probe(struct platform_device *ofdev) |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1377 | { |
| 1378 | struct net_device *dev; |
| 1379 | struct greth_private *greth; |
| 1380 | struct greth_regs *regs; |
| 1381 | |
| 1382 | int i; |
| 1383 | int err; |
| 1384 | int tmp; |
| 1385 | unsigned long timeout; |
| 1386 | |
| 1387 | dev = alloc_etherdev(sizeof(struct greth_private)); |
| 1388 | |
| 1389 | if (dev == NULL) |
| 1390 | return -ENOMEM; |
| 1391 | |
| 1392 | greth = netdev_priv(dev); |
| 1393 | greth->netdev = dev; |
| 1394 | greth->dev = &ofdev->dev; |
| 1395 | |
| 1396 | if (greth_debug > 0) |
| 1397 | greth->msg_enable = greth_debug; |
| 1398 | else |
| 1399 | greth->msg_enable = GRETH_DEF_MSG_ENABLE; |
| 1400 | |
| 1401 | spin_lock_init(&greth->devlock); |
| 1402 | |
| 1403 | greth->regs = of_ioremap(&ofdev->resource[0], 0, |
| 1404 | resource_size(&ofdev->resource[0]), |
| 1405 | "grlib-greth regs"); |
| 1406 | |
| 1407 | if (greth->regs == NULL) { |
| 1408 | if (netif_msg_probe(greth)) |
| 1409 | dev_err(greth->dev, "ioremap failure.\n"); |
| 1410 | err = -EIO; |
| 1411 | goto error1; |
| 1412 | } |
| 1413 | |
| 1414 | regs = (struct greth_regs *) greth->regs; |
Grant Likely | 19e4875 | 2010-08-08 00:23:26 -0600 | [diff] [blame] | 1415 | greth->irq = ofdev->archdata.irqs[0]; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1416 | |
| 1417 | dev_set_drvdata(greth->dev, dev); |
| 1418 | SET_NETDEV_DEV(dev, greth->dev); |
| 1419 | |
| 1420 | if (netif_msg_probe(greth)) |
| 1421 | dev_dbg(greth->dev, "reseting controller.\n"); |
| 1422 | |
| 1423 | /* Reset the controller. */ |
| 1424 | GRETH_REGSAVE(regs->control, GRETH_RESET); |
| 1425 | |
| 1426 | /* Wait for MAC to reset itself */ |
| 1427 | timeout = jiffies + HZ/100; |
| 1428 | while (GRETH_REGLOAD(regs->control) & GRETH_RESET) { |
| 1429 | if (time_after(jiffies, timeout)) { |
| 1430 | err = -EIO; |
| 1431 | if (netif_msg_probe(greth)) |
| 1432 | dev_err(greth->dev, "timeout when waiting for reset.\n"); |
| 1433 | goto error2; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | /* Get default PHY address */ |
| 1438 | greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F; |
| 1439 | |
| 1440 | /* Check if we have GBIT capable MAC */ |
| 1441 | tmp = GRETH_REGLOAD(regs->control); |
| 1442 | greth->gbit_mac = (tmp >> 27) & 1; |
| 1443 | |
| 1444 | /* Check for multicast capability */ |
| 1445 | greth->multicast = (tmp >> 25) & 1; |
| 1446 | |
| 1447 | greth->edcl = (tmp >> 31) & 1; |
| 1448 | |
| 1449 | /* If we have EDCL we disable the EDCL speed-duplex FSM so |
| 1450 | * it doesn't interfere with the software */ |
| 1451 | if (greth->edcl != 0) |
| 1452 | GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX); |
| 1453 | |
| 1454 | /* Check if MAC can handle MDIO interrupts */ |
| 1455 | greth->mdio_int_en = (tmp >> 26) & 1; |
| 1456 | |
| 1457 | err = greth_mdio_init(greth); |
| 1458 | if (err) { |
| 1459 | if (netif_msg_probe(greth)) |
| 1460 | dev_err(greth->dev, "failed to register MDIO bus\n"); |
| 1461 | goto error2; |
| 1462 | } |
| 1463 | |
| 1464 | /* Allocate TX descriptor ring in coherent memory */ |
| 1465 | greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev, |
| 1466 | 1024, |
| 1467 | &greth->tx_bd_base_phys, |
| 1468 | GFP_KERNEL); |
| 1469 | |
| 1470 | if (!greth->tx_bd_base) { |
| 1471 | if (netif_msg_probe(greth)) |
| 1472 | dev_err(&dev->dev, "could not allocate descriptor memory.\n"); |
| 1473 | err = -ENOMEM; |
| 1474 | goto error3; |
| 1475 | } |
| 1476 | |
| 1477 | memset(greth->tx_bd_base, 0, 1024); |
| 1478 | |
| 1479 | /* Allocate RX descriptor ring in coherent memory */ |
| 1480 | greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev, |
| 1481 | 1024, |
| 1482 | &greth->rx_bd_base_phys, |
| 1483 | GFP_KERNEL); |
| 1484 | |
| 1485 | if (!greth->rx_bd_base) { |
| 1486 | if (netif_msg_probe(greth)) |
| 1487 | dev_err(greth->dev, "could not allocate descriptor memory.\n"); |
| 1488 | err = -ENOMEM; |
| 1489 | goto error4; |
| 1490 | } |
| 1491 | |
| 1492 | memset(greth->rx_bd_base, 0, 1024); |
| 1493 | |
| 1494 | /* Get MAC address from: module param, OF property or ID prom */ |
| 1495 | for (i = 0; i < 6; i++) { |
| 1496 | if (macaddr[i] != 0) |
| 1497 | break; |
| 1498 | } |
| 1499 | if (i == 6) { |
| 1500 | const unsigned char *addr; |
| 1501 | int len; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 1502 | addr = of_get_property(ofdev->dev.of_node, "local-mac-address", |
| 1503 | &len); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1504 | if (addr != NULL && len == 6) { |
| 1505 | for (i = 0; i < 6; i++) |
| 1506 | macaddr[i] = (unsigned int) addr[i]; |
| 1507 | } else { |
| 1508 | #ifdef CONFIG_SPARC |
| 1509 | for (i = 0; i < 6; i++) |
| 1510 | macaddr[i] = (unsigned int) idprom->id_ethaddr[i]; |
| 1511 | #endif |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | for (i = 0; i < 6; i++) |
| 1516 | dev->dev_addr[i] = macaddr[i]; |
| 1517 | |
| 1518 | macaddr[5]++; |
| 1519 | |
| 1520 | if (!is_valid_ether_addr(&dev->dev_addr[0])) { |
| 1521 | if (netif_msg_probe(greth)) |
| 1522 | dev_err(greth->dev, "no valid ethernet address, aborting.\n"); |
| 1523 | err = -EINVAL; |
| 1524 | goto error5; |
| 1525 | } |
| 1526 | |
| 1527 | GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]); |
| 1528 | GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 | |
| 1529 | dev->dev_addr[4] << 8 | dev->dev_addr[5]); |
| 1530 | |
| 1531 | /* Clear all pending interrupts except PHY irq */ |
| 1532 | GRETH_REGSAVE(regs->status, 0xFF); |
| 1533 | |
| 1534 | if (greth->gbit_mac) { |
Michał Mirosław | 131ae32 | 2011-04-17 00:15:47 +0000 | [diff] [blame] | 1535 | dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | |
| 1536 | NETIF_F_RXCSUM; |
| 1537 | dev->features = dev->hw_features | NETIF_F_HIGHDMA; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1538 | greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit; |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | if (greth->multicast) { |
| 1542 | greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list; |
| 1543 | dev->flags |= IFF_MULTICAST; |
| 1544 | } else { |
| 1545 | dev->flags &= ~IFF_MULTICAST; |
| 1546 | } |
| 1547 | |
| 1548 | dev->netdev_ops = &greth_netdev_ops; |
| 1549 | dev->ethtool_ops = &greth_ethtool_ops; |
| 1550 | |
Tobias Klauser | cb5d991 | 2010-08-17 06:11:24 +0000 | [diff] [blame] | 1551 | err = register_netdev(dev); |
| 1552 | if (err) { |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1553 | if (netif_msg_probe(greth)) |
| 1554 | dev_err(greth->dev, "netdevice registration failed.\n"); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1555 | goto error5; |
| 1556 | } |
| 1557 | |
| 1558 | /* setup NAPI */ |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1559 | netif_napi_add(dev, &greth->napi, greth_poll, 64); |
| 1560 | |
| 1561 | return 0; |
| 1562 | |
| 1563 | error5: |
| 1564 | dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys); |
| 1565 | error4: |
| 1566 | dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys); |
| 1567 | error3: |
| 1568 | mdiobus_unregister(greth->mdio); |
| 1569 | error2: |
| 1570 | of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0])); |
| 1571 | error1: |
| 1572 | free_netdev(dev); |
| 1573 | return err; |
| 1574 | } |
| 1575 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 1576 | static int __devexit greth_of_remove(struct platform_device *of_dev) |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1577 | { |
| 1578 | struct net_device *ndev = dev_get_drvdata(&of_dev->dev); |
| 1579 | struct greth_private *greth = netdev_priv(ndev); |
| 1580 | |
| 1581 | /* Free descriptor areas */ |
| 1582 | dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys); |
| 1583 | |
| 1584 | dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys); |
| 1585 | |
| 1586 | dev_set_drvdata(&of_dev->dev, NULL); |
| 1587 | |
| 1588 | if (greth->phy) |
| 1589 | phy_stop(greth->phy); |
| 1590 | mdiobus_unregister(greth->mdio); |
| 1591 | |
| 1592 | unregister_netdev(ndev); |
| 1593 | free_netdev(ndev); |
| 1594 | |
| 1595 | of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0])); |
| 1596 | |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | static struct of_device_id greth_of_match[] = { |
| 1601 | { |
| 1602 | .name = "GAISLER_ETHMAC", |
| 1603 | }, |
Daniel Hellstrom | ad4650a | 2011-01-14 03:02:37 +0000 | [diff] [blame] | 1604 | { |
| 1605 | .name = "01_01d", |
| 1606 | }, |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1607 | {}, |
| 1608 | }; |
| 1609 | |
| 1610 | MODULE_DEVICE_TABLE(of, greth_of_match); |
| 1611 | |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 1612 | static struct platform_driver greth_of_driver = { |
David S. Miller | bc284f9 | 2010-05-31 05:47:32 -0700 | [diff] [blame] | 1613 | .driver = { |
| 1614 | .name = "grlib-greth", |
| 1615 | .owner = THIS_MODULE, |
| 1616 | .of_match_table = greth_of_match, |
| 1617 | }, |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1618 | .probe = greth_of_probe, |
| 1619 | .remove = __devexit_p(greth_of_remove), |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1620 | }; |
| 1621 | |
| 1622 | static int __init greth_init(void) |
| 1623 | { |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 1624 | return platform_driver_register(&greth_of_driver); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | static void __exit greth_cleanup(void) |
| 1628 | { |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 1629 | platform_driver_unregister(&greth_of_driver); |
Kristoffer Glembo | d4c4113 | 2010-02-15 03:33:44 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | module_init(greth_init); |
| 1633 | module_exit(greth_cleanup); |
| 1634 | |
| 1635 | MODULE_AUTHOR("Aeroflex Gaisler AB."); |
| 1636 | MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver"); |
| 1637 | MODULE_LICENSE("GPL"); |