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