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