Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'gianfar-some-assorted-cleanup'

Arseny Solokha says:

====================
gianfar: some assorted cleanup

This is a cleanup series for the gianfar Ethernet driver, following up a
discussion in [1]. It is intended to precede a conversion of gianfar from
PHYLIB to PHYLINK API, which will be submitted later in its version 2.
However, it won't make a conversion cleaner, except for the last patch in
this series. Obviously this series is not intended for -stable.

The first patch looks super controversial to me, as it moves lots of code
around for the sole purpose of getting rid of static forward declarations
in two translation units. On the other hand, this change is purely
mechanical and cannot do any harm other than cluttering git blame output.
I can prepare an alternative patch for only swapping adjacent functions
around, if necessary.

The second patch is a trivial follow-up to the first one, making functions
that are only called from the same translation unit static.

The third patch removes some now unused macro and structure definitions
from gianfar.h, slipped away from various cleanups in the past.

The fourth patch, also suggested in [1], makes the driver consistently use
PHY connection type value obtained from a Device Tree node, instead of
ignoring it and using the one auto-detected by MAC, when connecting to PHY.
Obviously a value has to be specified correctly in DT source, or omitted
altogether, in which case the driver will fall back to auto-detection. When
querying a DT node, the driver will also take both applicable properties
into account by making a proper API call instead of open-coding the lookup
half-way correctly.

[1] https://lore.kernel.org/netdev/CA+h21hruqt6nGG5ksDSwrGH_w5GtGF4fjAMCWJne7QJrjusERQ@mail.gmail.com/
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+2303 -2402
+2303 -2344
drivers/net/ethernet/freescale/gianfar.c
··· 105 105 106 106 const char gfar_driver_version[] = "2.0"; 107 107 108 - static int gfar_enet_open(struct net_device *dev); 109 - static netdev_tx_t gfar_start_xmit(struct sk_buff *skb, struct net_device *dev); 110 - static void gfar_reset_task(struct work_struct *work); 111 - static void gfar_timeout(struct net_device *dev); 112 - static int gfar_close(struct net_device *dev); 113 - static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue, 114 - int alloc_cnt); 115 - static int gfar_set_mac_address(struct net_device *dev); 116 - static int gfar_change_mtu(struct net_device *dev, int new_mtu); 117 - static irqreturn_t gfar_error(int irq, void *dev_id); 118 - static irqreturn_t gfar_transmit(int irq, void *dev_id); 119 - static irqreturn_t gfar_interrupt(int irq, void *dev_id); 120 - static void adjust_link(struct net_device *dev); 121 - static noinline void gfar_update_link_state(struct gfar_private *priv); 122 - static int init_phy(struct net_device *dev); 123 - static int gfar_probe(struct platform_device *ofdev); 124 - static int gfar_remove(struct platform_device *ofdev); 125 - static void free_skb_resources(struct gfar_private *priv); 126 - static void gfar_set_multi(struct net_device *dev); 127 - static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr); 128 - static void gfar_configure_serdes(struct net_device *dev); 129 - static int gfar_poll_rx(struct napi_struct *napi, int budget); 130 - static int gfar_poll_tx(struct napi_struct *napi, int budget); 131 - static int gfar_poll_rx_sq(struct napi_struct *napi, int budget); 132 - static int gfar_poll_tx_sq(struct napi_struct *napi, int budget); 133 - #ifdef CONFIG_NET_POLL_CONTROLLER 134 - static void gfar_netpoll(struct net_device *dev); 135 - #endif 136 - int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit); 137 - static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue); 138 - static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb); 139 - static void gfar_halt_nodisable(struct gfar_private *priv); 140 - static void gfar_clear_exact_match(struct net_device *dev); 141 - static void gfar_set_mac_for_addr(struct net_device *dev, int num, 142 - const u8 *addr); 143 - static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd); 144 - 145 108 MODULE_AUTHOR("Freescale Semiconductor, Inc"); 146 109 MODULE_DESCRIPTION("Gianfar Ethernet Driver"); 147 110 MODULE_LICENSE("GPL"); ··· 123 160 gfar_wmb(); 124 161 125 162 bdp->lstatus = cpu_to_be32(lstatus); 126 - } 127 - 128 - static void gfar_init_bds(struct net_device *ndev) 129 - { 130 - struct gfar_private *priv = netdev_priv(ndev); 131 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 132 - struct gfar_priv_tx_q *tx_queue = NULL; 133 - struct gfar_priv_rx_q *rx_queue = NULL; 134 - struct txbd8 *txbdp; 135 - u32 __iomem *rfbptr; 136 - int i, j; 137 - 138 - for (i = 0; i < priv->num_tx_queues; i++) { 139 - tx_queue = priv->tx_queue[i]; 140 - /* Initialize some variables in our dev structure */ 141 - tx_queue->num_txbdfree = tx_queue->tx_ring_size; 142 - tx_queue->dirty_tx = tx_queue->tx_bd_base; 143 - tx_queue->cur_tx = tx_queue->tx_bd_base; 144 - tx_queue->skb_curtx = 0; 145 - tx_queue->skb_dirtytx = 0; 146 - 147 - /* Initialize Transmit Descriptor Ring */ 148 - txbdp = tx_queue->tx_bd_base; 149 - for (j = 0; j < tx_queue->tx_ring_size; j++) { 150 - txbdp->lstatus = 0; 151 - txbdp->bufPtr = 0; 152 - txbdp++; 153 - } 154 - 155 - /* Set the last descriptor in the ring to indicate wrap */ 156 - txbdp--; 157 - txbdp->status = cpu_to_be16(be16_to_cpu(txbdp->status) | 158 - TXBD_WRAP); 159 - } 160 - 161 - rfbptr = &regs->rfbptr0; 162 - for (i = 0; i < priv->num_rx_queues; i++) { 163 - rx_queue = priv->rx_queue[i]; 164 - 165 - rx_queue->next_to_clean = 0; 166 - rx_queue->next_to_use = 0; 167 - rx_queue->next_to_alloc = 0; 168 - 169 - /* make sure next_to_clean != next_to_use after this 170 - * by leaving at least 1 unused descriptor 171 - */ 172 - gfar_alloc_rx_buffs(rx_queue, gfar_rxbd_unused(rx_queue)); 173 - 174 - rx_queue->rfbptr = rfbptr; 175 - rfbptr += 2; 176 - } 177 - } 178 - 179 - static int gfar_alloc_skb_resources(struct net_device *ndev) 180 - { 181 - void *vaddr; 182 - dma_addr_t addr; 183 - int i, j; 184 - struct gfar_private *priv = netdev_priv(ndev); 185 - struct device *dev = priv->dev; 186 - struct gfar_priv_tx_q *tx_queue = NULL; 187 - struct gfar_priv_rx_q *rx_queue = NULL; 188 - 189 - priv->total_tx_ring_size = 0; 190 - for (i = 0; i < priv->num_tx_queues; i++) 191 - priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size; 192 - 193 - priv->total_rx_ring_size = 0; 194 - for (i = 0; i < priv->num_rx_queues; i++) 195 - priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size; 196 - 197 - /* Allocate memory for the buffer descriptors */ 198 - vaddr = dma_alloc_coherent(dev, 199 - (priv->total_tx_ring_size * 200 - sizeof(struct txbd8)) + 201 - (priv->total_rx_ring_size * 202 - sizeof(struct rxbd8)), 203 - &addr, GFP_KERNEL); 204 - if (!vaddr) 205 - return -ENOMEM; 206 - 207 - for (i = 0; i < priv->num_tx_queues; i++) { 208 - tx_queue = priv->tx_queue[i]; 209 - tx_queue->tx_bd_base = vaddr; 210 - tx_queue->tx_bd_dma_base = addr; 211 - tx_queue->dev = ndev; 212 - /* enet DMA only understands physical addresses */ 213 - addr += sizeof(struct txbd8) * tx_queue->tx_ring_size; 214 - vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size; 215 - } 216 - 217 - /* Start the rx descriptor ring where the tx ring leaves off */ 218 - for (i = 0; i < priv->num_rx_queues; i++) { 219 - rx_queue = priv->rx_queue[i]; 220 - rx_queue->rx_bd_base = vaddr; 221 - rx_queue->rx_bd_dma_base = addr; 222 - rx_queue->ndev = ndev; 223 - rx_queue->dev = dev; 224 - addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size; 225 - vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size; 226 - } 227 - 228 - /* Setup the skbuff rings */ 229 - for (i = 0; i < priv->num_tx_queues; i++) { 230 - tx_queue = priv->tx_queue[i]; 231 - tx_queue->tx_skbuff = 232 - kmalloc_array(tx_queue->tx_ring_size, 233 - sizeof(*tx_queue->tx_skbuff), 234 - GFP_KERNEL); 235 - if (!tx_queue->tx_skbuff) 236 - goto cleanup; 237 - 238 - for (j = 0; j < tx_queue->tx_ring_size; j++) 239 - tx_queue->tx_skbuff[j] = NULL; 240 - } 241 - 242 - for (i = 0; i < priv->num_rx_queues; i++) { 243 - rx_queue = priv->rx_queue[i]; 244 - rx_queue->rx_buff = kcalloc(rx_queue->rx_ring_size, 245 - sizeof(*rx_queue->rx_buff), 246 - GFP_KERNEL); 247 - if (!rx_queue->rx_buff) 248 - goto cleanup; 249 - } 250 - 251 - gfar_init_bds(ndev); 252 - 253 - return 0; 254 - 255 - cleanup: 256 - free_skb_resources(priv); 257 - return -ENOMEM; 258 163 } 259 164 260 165 static void gfar_init_tx_rx_base(struct gfar_private *priv) ··· 275 444 } 276 445 } 277 446 278 - void gfar_configure_coalescing_all(struct gfar_private *priv) 447 + static void gfar_configure_coalescing_all(struct gfar_private *priv) 279 448 { 280 449 gfar_configure_coalescing(priv, 0xFF, 0xFF); 281 450 } ··· 308 477 return &dev->stats; 309 478 } 310 479 480 + /* Set the appropriate hash bit for the given addr */ 481 + /* The algorithm works like so: 482 + * 1) Take the Destination Address (ie the multicast address), and 483 + * do a CRC on it (little endian), and reverse the bits of the 484 + * result. 485 + * 2) Use the 8 most significant bits as a hash into a 256-entry 486 + * table. The table is controlled through 8 32-bit registers: 487 + * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is 488 + * gaddr7. This means that the 3 most significant bits in the 489 + * hash index which gaddr register to use, and the 5 other bits 490 + * indicate which bit (assuming an IBM numbering scheme, which 491 + * for PowerPC (tm) is usually the case) in the register holds 492 + * the entry. 493 + */ 494 + static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr) 495 + { 496 + u32 tempval; 497 + struct gfar_private *priv = netdev_priv(dev); 498 + u32 result = ether_crc(ETH_ALEN, addr); 499 + int width = priv->hash_width; 500 + u8 whichbit = (result >> (32 - width)) & 0x1f; 501 + u8 whichreg = result >> (32 - width + 5); 502 + u32 value = (1 << (31-whichbit)); 503 + 504 + tempval = gfar_read(priv->hash_regs[whichreg]); 505 + tempval |= value; 506 + gfar_write(priv->hash_regs[whichreg], tempval); 507 + } 508 + 509 + /* There are multiple MAC Address register pairs on some controllers 510 + * This function sets the numth pair to a given address 511 + */ 512 + static void gfar_set_mac_for_addr(struct net_device *dev, int num, 513 + const u8 *addr) 514 + { 515 + struct gfar_private *priv = netdev_priv(dev); 516 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 517 + u32 tempval; 518 + u32 __iomem *macptr = &regs->macstnaddr1; 519 + 520 + macptr += num*2; 521 + 522 + /* For a station address of 0x12345678ABCD in transmission 523 + * order (BE), MACnADDR1 is set to 0xCDAB7856 and 524 + * MACnADDR2 is set to 0x34120000. 525 + */ 526 + tempval = (addr[5] << 24) | (addr[4] << 16) | 527 + (addr[3] << 8) | addr[2]; 528 + 529 + gfar_write(macptr, tempval); 530 + 531 + tempval = (addr[1] << 24) | (addr[0] << 16); 532 + 533 + gfar_write(macptr+1, tempval); 534 + } 535 + 311 536 static int gfar_set_mac_addr(struct net_device *dev, void *p) 312 537 { 313 538 eth_mac_addr(dev, p); ··· 372 485 373 486 return 0; 374 487 } 375 - 376 - static const struct net_device_ops gfar_netdev_ops = { 377 - .ndo_open = gfar_enet_open, 378 - .ndo_start_xmit = gfar_start_xmit, 379 - .ndo_stop = gfar_close, 380 - .ndo_change_mtu = gfar_change_mtu, 381 - .ndo_set_features = gfar_set_features, 382 - .ndo_set_rx_mode = gfar_set_multi, 383 - .ndo_tx_timeout = gfar_timeout, 384 - .ndo_do_ioctl = gfar_ioctl, 385 - .ndo_get_stats = gfar_get_stats, 386 - .ndo_change_carrier = fixed_phy_change_carrier, 387 - .ndo_set_mac_address = gfar_set_mac_addr, 388 - .ndo_validate_addr = eth_validate_addr, 389 - #ifdef CONFIG_NET_POLL_CONTROLLER 390 - .ndo_poll_controller = gfar_netpoll, 391 - #endif 392 - }; 393 488 394 489 static void gfar_ints_disable(struct gfar_private *priv) 395 490 { ··· 592 723 return num; 593 724 } 594 725 726 + /* Reads the controller's registers to determine what interface 727 + * connects it to the PHY. 728 + */ 729 + static phy_interface_t gfar_get_interface(struct net_device *dev) 730 + { 731 + struct gfar_private *priv = netdev_priv(dev); 732 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 733 + u32 ecntrl; 734 + 735 + ecntrl = gfar_read(&regs->ecntrl); 736 + 737 + if (ecntrl & ECNTRL_SGMII_MODE) 738 + return PHY_INTERFACE_MODE_SGMII; 739 + 740 + if (ecntrl & ECNTRL_TBI_MODE) { 741 + if (ecntrl & ECNTRL_REDUCED_MODE) 742 + return PHY_INTERFACE_MODE_RTBI; 743 + else 744 + return PHY_INTERFACE_MODE_TBI; 745 + } 746 + 747 + if (ecntrl & ECNTRL_REDUCED_MODE) { 748 + if (ecntrl & ECNTRL_REDUCED_MII_MODE) { 749 + return PHY_INTERFACE_MODE_RMII; 750 + } 751 + else { 752 + phy_interface_t interface = priv->interface; 753 + 754 + /* This isn't autodetected right now, so it must 755 + * be set by the device tree or platform code. 756 + */ 757 + if (interface == PHY_INTERFACE_MODE_RGMII_ID) 758 + return PHY_INTERFACE_MODE_RGMII_ID; 759 + 760 + return PHY_INTERFACE_MODE_RGMII; 761 + } 762 + } 763 + 764 + if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT) 765 + return PHY_INTERFACE_MODE_GMII; 766 + 767 + return PHY_INTERFACE_MODE_MII; 768 + } 769 + 595 770 static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev) 596 771 { 597 772 const char *model; 598 - const char *ctype; 599 773 const void *mac_addr; 600 774 int err = 0, i; 601 775 struct net_device *dev = NULL; ··· 801 889 FSL_GIANFAR_DEV_HAS_TIMER | 802 890 FSL_GIANFAR_DEV_HAS_RX_FILER; 803 891 804 - err = of_property_read_string(np, "phy-connection-type", &ctype); 805 - 806 - /* We only care about rgmii-id. The rest are autodetected */ 807 - if (err == 0 && !strcmp(ctype, "rgmii-id")) 808 - priv->interface = PHY_INTERFACE_MODE_RGMII_ID; 892 + /* Use PHY connection type from the DT node if one is specified there. 893 + * rgmii-id really needs to be specified. Other types can be 894 + * detected by hardware 895 + */ 896 + err = of_get_phy_mode(np); 897 + if (err >= 0) 898 + priv->interface = err; 809 899 else 810 - priv->interface = PHY_INTERFACE_MODE_MII; 900 + priv->interface = gfar_get_interface(dev); 811 901 812 902 if (of_find_property(np, "fsl,magic-packet", NULL)) 813 903 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET; ··· 843 929 gfar_free_tx_queues(priv); 844 930 free_gfar_dev(priv); 845 931 return err; 846 - } 847 - 848 - static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr) 849 - { 850 - struct hwtstamp_config config; 851 - struct gfar_private *priv = netdev_priv(netdev); 852 - 853 - if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 854 - return -EFAULT; 855 - 856 - /* reserved for future extensions */ 857 - if (config.flags) 858 - return -EINVAL; 859 - 860 - switch (config.tx_type) { 861 - case HWTSTAMP_TX_OFF: 862 - priv->hwts_tx_en = 0; 863 - break; 864 - case HWTSTAMP_TX_ON: 865 - if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) 866 - return -ERANGE; 867 - priv->hwts_tx_en = 1; 868 - break; 869 - default: 870 - return -ERANGE; 871 - } 872 - 873 - switch (config.rx_filter) { 874 - case HWTSTAMP_FILTER_NONE: 875 - if (priv->hwts_rx_en) { 876 - priv->hwts_rx_en = 0; 877 - reset_gfar(netdev); 878 - } 879 - break; 880 - default: 881 - if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) 882 - return -ERANGE; 883 - if (!priv->hwts_rx_en) { 884 - priv->hwts_rx_en = 1; 885 - reset_gfar(netdev); 886 - } 887 - config.rx_filter = HWTSTAMP_FILTER_ALL; 888 - break; 889 - } 890 - 891 - return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 892 - -EFAULT : 0; 893 - } 894 - 895 - static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr) 896 - { 897 - struct hwtstamp_config config; 898 - struct gfar_private *priv = netdev_priv(netdev); 899 - 900 - config.flags = 0; 901 - config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 902 - config.rx_filter = (priv->hwts_rx_en ? 903 - HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE); 904 - 905 - return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 906 - -EFAULT : 0; 907 - } 908 - 909 - static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 910 - { 911 - struct phy_device *phydev = dev->phydev; 912 - 913 - if (!netif_running(dev)) 914 - return -EINVAL; 915 - 916 - if (cmd == SIOCSHWTSTAMP) 917 - return gfar_hwtstamp_set(dev, rq); 918 - if (cmd == SIOCGHWTSTAMP) 919 - return gfar_hwtstamp_get(dev, rq); 920 - 921 - if (!phydev) 922 - return -ENODEV; 923 - 924 - return phy_mii_ioctl(phydev, rq, cmd); 925 932 } 926 933 927 934 static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar, ··· 966 1131 if (priv->errata) 967 1132 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n", 968 1133 priv->errata); 1134 + } 1135 + 1136 + static void gfar_init_addr_hash_table(struct gfar_private *priv) 1137 + { 1138 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 1139 + 1140 + if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) { 1141 + priv->extended_hash = 1; 1142 + priv->hash_width = 9; 1143 + 1144 + priv->hash_regs[0] = &regs->igaddr0; 1145 + priv->hash_regs[1] = &regs->igaddr1; 1146 + priv->hash_regs[2] = &regs->igaddr2; 1147 + priv->hash_regs[3] = &regs->igaddr3; 1148 + priv->hash_regs[4] = &regs->igaddr4; 1149 + priv->hash_regs[5] = &regs->igaddr5; 1150 + priv->hash_regs[6] = &regs->igaddr6; 1151 + priv->hash_regs[7] = &regs->igaddr7; 1152 + priv->hash_regs[8] = &regs->gaddr0; 1153 + priv->hash_regs[9] = &regs->gaddr1; 1154 + priv->hash_regs[10] = &regs->gaddr2; 1155 + priv->hash_regs[11] = &regs->gaddr3; 1156 + priv->hash_regs[12] = &regs->gaddr4; 1157 + priv->hash_regs[13] = &regs->gaddr5; 1158 + priv->hash_regs[14] = &regs->gaddr6; 1159 + priv->hash_regs[15] = &regs->gaddr7; 1160 + 1161 + } else { 1162 + priv->extended_hash = 0; 1163 + priv->hash_width = 8; 1164 + 1165 + priv->hash_regs[0] = &regs->gaddr0; 1166 + priv->hash_regs[1] = &regs->gaddr1; 1167 + priv->hash_regs[2] = &regs->gaddr2; 1168 + priv->hash_regs[3] = &regs->gaddr3; 1169 + priv->hash_regs[4] = &regs->gaddr4; 1170 + priv->hash_regs[5] = &regs->gaddr5; 1171 + priv->hash_regs[6] = &regs->gaddr6; 1172 + priv->hash_regs[7] = &regs->gaddr7; 1173 + } 1174 + } 1175 + 1176 + static int __gfar_is_rx_idle(struct gfar_private *priv) 1177 + { 1178 + u32 res; 1179 + 1180 + /* Normaly TSEC should not hang on GRS commands, so we should 1181 + * actually wait for IEVENT_GRSC flag. 1182 + */ 1183 + if (!gfar_has_errata(priv, GFAR_ERRATA_A002)) 1184 + return 0; 1185 + 1186 + /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are 1187 + * the same as bits 23-30, the eTSEC Rx is assumed to be idle 1188 + * and the Rx can be safely reset. 1189 + */ 1190 + res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c); 1191 + res &= 0x7f807f80; 1192 + if ((res & 0xffff) == (res >> 16)) 1193 + return 1; 1194 + 1195 + return 0; 1196 + } 1197 + 1198 + /* Halt the receive and transmit queues */ 1199 + static void gfar_halt_nodisable(struct gfar_private *priv) 1200 + { 1201 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 1202 + u32 tempval; 1203 + unsigned int timeout; 1204 + int stopped; 1205 + 1206 + gfar_ints_disable(priv); 1207 + 1208 + if (gfar_is_dma_stopped(priv)) 1209 + return; 1210 + 1211 + /* Stop the DMA, and wait for it to stop */ 1212 + tempval = gfar_read(&regs->dmactrl); 1213 + tempval |= (DMACTRL_GRS | DMACTRL_GTS); 1214 + gfar_write(&regs->dmactrl, tempval); 1215 + 1216 + retry: 1217 + timeout = 1000; 1218 + while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) { 1219 + cpu_relax(); 1220 + timeout--; 1221 + } 1222 + 1223 + if (!timeout) 1224 + stopped = gfar_is_dma_stopped(priv); 1225 + 1226 + if (!stopped && !gfar_is_rx_dma_stopped(priv) && 1227 + !__gfar_is_rx_idle(priv)) 1228 + goto retry; 1229 + } 1230 + 1231 + /* Halt the receive and transmit queues */ 1232 + static void gfar_halt(struct gfar_private *priv) 1233 + { 1234 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 1235 + u32 tempval; 1236 + 1237 + /* Dissable the Rx/Tx hw queues */ 1238 + gfar_write(&regs->rqueue, 0); 1239 + gfar_write(&regs->tqueue, 0); 1240 + 1241 + mdelay(10); 1242 + 1243 + gfar_halt_nodisable(priv); 1244 + 1245 + /* Disable Rx/Tx DMA */ 1246 + tempval = gfar_read(&regs->maccfg1); 1247 + tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN); 1248 + gfar_write(&regs->maccfg1, tempval); 1249 + } 1250 + 1251 + static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue) 1252 + { 1253 + struct txbd8 *txbdp; 1254 + struct gfar_private *priv = netdev_priv(tx_queue->dev); 1255 + int i, j; 1256 + 1257 + txbdp = tx_queue->tx_bd_base; 1258 + 1259 + for (i = 0; i < tx_queue->tx_ring_size; i++) { 1260 + if (!tx_queue->tx_skbuff[i]) 1261 + continue; 1262 + 1263 + dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr), 1264 + be16_to_cpu(txbdp->length), DMA_TO_DEVICE); 1265 + txbdp->lstatus = 0; 1266 + for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags; 1267 + j++) { 1268 + txbdp++; 1269 + dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr), 1270 + be16_to_cpu(txbdp->length), 1271 + DMA_TO_DEVICE); 1272 + } 1273 + txbdp++; 1274 + dev_kfree_skb_any(tx_queue->tx_skbuff[i]); 1275 + tx_queue->tx_skbuff[i] = NULL; 1276 + } 1277 + kfree(tx_queue->tx_skbuff); 1278 + tx_queue->tx_skbuff = NULL; 1279 + } 1280 + 1281 + static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue) 1282 + { 1283 + int i; 1284 + 1285 + struct rxbd8 *rxbdp = rx_queue->rx_bd_base; 1286 + 1287 + dev_kfree_skb(rx_queue->skb); 1288 + 1289 + for (i = 0; i < rx_queue->rx_ring_size; i++) { 1290 + struct gfar_rx_buff *rxb = &rx_queue->rx_buff[i]; 1291 + 1292 + rxbdp->lstatus = 0; 1293 + rxbdp->bufPtr = 0; 1294 + rxbdp++; 1295 + 1296 + if (!rxb->page) 1297 + continue; 1298 + 1299 + dma_unmap_page(rx_queue->dev, rxb->dma, 1300 + PAGE_SIZE, DMA_FROM_DEVICE); 1301 + __free_page(rxb->page); 1302 + 1303 + rxb->page = NULL; 1304 + } 1305 + 1306 + kfree(rx_queue->rx_buff); 1307 + rx_queue->rx_buff = NULL; 1308 + } 1309 + 1310 + /* If there are any tx skbs or rx skbs still around, free them. 1311 + * Then free tx_skbuff and rx_skbuff 1312 + */ 1313 + static void free_skb_resources(struct gfar_private *priv) 1314 + { 1315 + struct gfar_priv_tx_q *tx_queue = NULL; 1316 + struct gfar_priv_rx_q *rx_queue = NULL; 1317 + int i; 1318 + 1319 + /* Go through all the buffer descriptors and free their data buffers */ 1320 + for (i = 0; i < priv->num_tx_queues; i++) { 1321 + struct netdev_queue *txq; 1322 + 1323 + tx_queue = priv->tx_queue[i]; 1324 + txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex); 1325 + if (tx_queue->tx_skbuff) 1326 + free_skb_tx_queue(tx_queue); 1327 + netdev_tx_reset_queue(txq); 1328 + } 1329 + 1330 + for (i = 0; i < priv->num_rx_queues; i++) { 1331 + rx_queue = priv->rx_queue[i]; 1332 + if (rx_queue->rx_buff) 1333 + free_skb_rx_queue(rx_queue); 1334 + } 1335 + 1336 + dma_free_coherent(priv->dev, 1337 + sizeof(struct txbd8) * priv->total_tx_ring_size + 1338 + sizeof(struct rxbd8) * priv->total_rx_ring_size, 1339 + priv->tx_queue[0]->tx_bd_base, 1340 + priv->tx_queue[0]->tx_bd_dma_base); 1341 + } 1342 + 1343 + void stop_gfar(struct net_device *dev) 1344 + { 1345 + struct gfar_private *priv = netdev_priv(dev); 1346 + 1347 + netif_tx_stop_all_queues(dev); 1348 + 1349 + smp_mb__before_atomic(); 1350 + set_bit(GFAR_DOWN, &priv->state); 1351 + smp_mb__after_atomic(); 1352 + 1353 + disable_napi(priv); 1354 + 1355 + /* disable ints and gracefully shut down Rx/Tx DMA */ 1356 + gfar_halt(priv); 1357 + 1358 + phy_stop(dev->phydev); 1359 + 1360 + free_skb_resources(priv); 1361 + } 1362 + 1363 + static void gfar_start(struct gfar_private *priv) 1364 + { 1365 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 1366 + u32 tempval; 1367 + int i = 0; 1368 + 1369 + /* Enable Rx/Tx hw queues */ 1370 + gfar_write(&regs->rqueue, priv->rqueue); 1371 + gfar_write(&regs->tqueue, priv->tqueue); 1372 + 1373 + /* Initialize DMACTRL to have WWR and WOP */ 1374 + tempval = gfar_read(&regs->dmactrl); 1375 + tempval |= DMACTRL_INIT_SETTINGS; 1376 + gfar_write(&regs->dmactrl, tempval); 1377 + 1378 + /* Make sure we aren't stopped */ 1379 + tempval = gfar_read(&regs->dmactrl); 1380 + tempval &= ~(DMACTRL_GRS | DMACTRL_GTS); 1381 + gfar_write(&regs->dmactrl, tempval); 1382 + 1383 + for (i = 0; i < priv->num_grps; i++) { 1384 + regs = priv->gfargrp[i].regs; 1385 + /* Clear THLT/RHLT, so that the DMA starts polling now */ 1386 + gfar_write(&regs->tstat, priv->gfargrp[i].tstat); 1387 + gfar_write(&regs->rstat, priv->gfargrp[i].rstat); 1388 + } 1389 + 1390 + /* Enable Rx/Tx DMA */ 1391 + tempval = gfar_read(&regs->maccfg1); 1392 + tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN); 1393 + gfar_write(&regs->maccfg1, tempval); 1394 + 1395 + gfar_ints_enable(priv); 1396 + 1397 + netif_trans_update(priv->ndev); /* prevent tx timeout */ 1398 + } 1399 + 1400 + static bool gfar_new_page(struct gfar_priv_rx_q *rxq, struct gfar_rx_buff *rxb) 1401 + { 1402 + struct page *page; 1403 + dma_addr_t addr; 1404 + 1405 + page = dev_alloc_page(); 1406 + if (unlikely(!page)) 1407 + return false; 1408 + 1409 + addr = dma_map_page(rxq->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE); 1410 + if (unlikely(dma_mapping_error(rxq->dev, addr))) { 1411 + __free_page(page); 1412 + 1413 + return false; 1414 + } 1415 + 1416 + rxb->dma = addr; 1417 + rxb->page = page; 1418 + rxb->page_offset = 0; 1419 + 1420 + return true; 1421 + } 1422 + 1423 + static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue) 1424 + { 1425 + struct gfar_private *priv = netdev_priv(rx_queue->ndev); 1426 + struct gfar_extra_stats *estats = &priv->extra_stats; 1427 + 1428 + netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n"); 1429 + atomic64_inc(&estats->rx_alloc_err); 1430 + } 1431 + 1432 + static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue, 1433 + int alloc_cnt) 1434 + { 1435 + struct rxbd8 *bdp; 1436 + struct gfar_rx_buff *rxb; 1437 + int i; 1438 + 1439 + i = rx_queue->next_to_use; 1440 + bdp = &rx_queue->rx_bd_base[i]; 1441 + rxb = &rx_queue->rx_buff[i]; 1442 + 1443 + while (alloc_cnt--) { 1444 + /* try reuse page */ 1445 + if (unlikely(!rxb->page)) { 1446 + if (unlikely(!gfar_new_page(rx_queue, rxb))) { 1447 + gfar_rx_alloc_err(rx_queue); 1448 + break; 1449 + } 1450 + } 1451 + 1452 + /* Setup the new RxBD */ 1453 + gfar_init_rxbdp(rx_queue, bdp, 1454 + rxb->dma + rxb->page_offset + RXBUF_ALIGNMENT); 1455 + 1456 + /* Update to the next pointer */ 1457 + bdp++; 1458 + rxb++; 1459 + 1460 + if (unlikely(++i == rx_queue->rx_ring_size)) { 1461 + i = 0; 1462 + bdp = rx_queue->rx_bd_base; 1463 + rxb = rx_queue->rx_buff; 1464 + } 1465 + } 1466 + 1467 + rx_queue->next_to_use = i; 1468 + rx_queue->next_to_alloc = i; 1469 + } 1470 + 1471 + static void gfar_init_bds(struct net_device *ndev) 1472 + { 1473 + struct gfar_private *priv = netdev_priv(ndev); 1474 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 1475 + struct gfar_priv_tx_q *tx_queue = NULL; 1476 + struct gfar_priv_rx_q *rx_queue = NULL; 1477 + struct txbd8 *txbdp; 1478 + u32 __iomem *rfbptr; 1479 + int i, j; 1480 + 1481 + for (i = 0; i < priv->num_tx_queues; i++) { 1482 + tx_queue = priv->tx_queue[i]; 1483 + /* Initialize some variables in our dev structure */ 1484 + tx_queue->num_txbdfree = tx_queue->tx_ring_size; 1485 + tx_queue->dirty_tx = tx_queue->tx_bd_base; 1486 + tx_queue->cur_tx = tx_queue->tx_bd_base; 1487 + tx_queue->skb_curtx = 0; 1488 + tx_queue->skb_dirtytx = 0; 1489 + 1490 + /* Initialize Transmit Descriptor Ring */ 1491 + txbdp = tx_queue->tx_bd_base; 1492 + for (j = 0; j < tx_queue->tx_ring_size; j++) { 1493 + txbdp->lstatus = 0; 1494 + txbdp->bufPtr = 0; 1495 + txbdp++; 1496 + } 1497 + 1498 + /* Set the last descriptor in the ring to indicate wrap */ 1499 + txbdp--; 1500 + txbdp->status = cpu_to_be16(be16_to_cpu(txbdp->status) | 1501 + TXBD_WRAP); 1502 + } 1503 + 1504 + rfbptr = &regs->rfbptr0; 1505 + for (i = 0; i < priv->num_rx_queues; i++) { 1506 + rx_queue = priv->rx_queue[i]; 1507 + 1508 + rx_queue->next_to_clean = 0; 1509 + rx_queue->next_to_use = 0; 1510 + rx_queue->next_to_alloc = 0; 1511 + 1512 + /* make sure next_to_clean != next_to_use after this 1513 + * by leaving at least 1 unused descriptor 1514 + */ 1515 + gfar_alloc_rx_buffs(rx_queue, gfar_rxbd_unused(rx_queue)); 1516 + 1517 + rx_queue->rfbptr = rfbptr; 1518 + rfbptr += 2; 1519 + } 1520 + } 1521 + 1522 + static int gfar_alloc_skb_resources(struct net_device *ndev) 1523 + { 1524 + void *vaddr; 1525 + dma_addr_t addr; 1526 + int i, j; 1527 + struct gfar_private *priv = netdev_priv(ndev); 1528 + struct device *dev = priv->dev; 1529 + struct gfar_priv_tx_q *tx_queue = NULL; 1530 + struct gfar_priv_rx_q *rx_queue = NULL; 1531 + 1532 + priv->total_tx_ring_size = 0; 1533 + for (i = 0; i < priv->num_tx_queues; i++) 1534 + priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size; 1535 + 1536 + priv->total_rx_ring_size = 0; 1537 + for (i = 0; i < priv->num_rx_queues; i++) 1538 + priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size; 1539 + 1540 + /* Allocate memory for the buffer descriptors */ 1541 + vaddr = dma_alloc_coherent(dev, 1542 + (priv->total_tx_ring_size * 1543 + sizeof(struct txbd8)) + 1544 + (priv->total_rx_ring_size * 1545 + sizeof(struct rxbd8)), 1546 + &addr, GFP_KERNEL); 1547 + if (!vaddr) 1548 + return -ENOMEM; 1549 + 1550 + for (i = 0; i < priv->num_tx_queues; i++) { 1551 + tx_queue = priv->tx_queue[i]; 1552 + tx_queue->tx_bd_base = vaddr; 1553 + tx_queue->tx_bd_dma_base = addr; 1554 + tx_queue->dev = ndev; 1555 + /* enet DMA only understands physical addresses */ 1556 + addr += sizeof(struct txbd8) * tx_queue->tx_ring_size; 1557 + vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size; 1558 + } 1559 + 1560 + /* Start the rx descriptor ring where the tx ring leaves off */ 1561 + for (i = 0; i < priv->num_rx_queues; i++) { 1562 + rx_queue = priv->rx_queue[i]; 1563 + rx_queue->rx_bd_base = vaddr; 1564 + rx_queue->rx_bd_dma_base = addr; 1565 + rx_queue->ndev = ndev; 1566 + rx_queue->dev = dev; 1567 + addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size; 1568 + vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size; 1569 + } 1570 + 1571 + /* Setup the skbuff rings */ 1572 + for (i = 0; i < priv->num_tx_queues; i++) { 1573 + tx_queue = priv->tx_queue[i]; 1574 + tx_queue->tx_skbuff = 1575 + kmalloc_array(tx_queue->tx_ring_size, 1576 + sizeof(*tx_queue->tx_skbuff), 1577 + GFP_KERNEL); 1578 + if (!tx_queue->tx_skbuff) 1579 + goto cleanup; 1580 + 1581 + for (j = 0; j < tx_queue->tx_ring_size; j++) 1582 + tx_queue->tx_skbuff[j] = NULL; 1583 + } 1584 + 1585 + for (i = 0; i < priv->num_rx_queues; i++) { 1586 + rx_queue = priv->rx_queue[i]; 1587 + rx_queue->rx_buff = kcalloc(rx_queue->rx_ring_size, 1588 + sizeof(*rx_queue->rx_buff), 1589 + GFP_KERNEL); 1590 + if (!rx_queue->rx_buff) 1591 + goto cleanup; 1592 + } 1593 + 1594 + gfar_init_bds(ndev); 1595 + 1596 + return 0; 1597 + 1598 + cleanup: 1599 + free_skb_resources(priv); 1600 + return -ENOMEM; 1601 + } 1602 + 1603 + /* Bring the controller up and running */ 1604 + int startup_gfar(struct net_device *ndev) 1605 + { 1606 + struct gfar_private *priv = netdev_priv(ndev); 1607 + int err; 1608 + 1609 + gfar_mac_reset(priv); 1610 + 1611 + err = gfar_alloc_skb_resources(ndev); 1612 + if (err) 1613 + return err; 1614 + 1615 + gfar_init_tx_rx_base(priv); 1616 + 1617 + smp_mb__before_atomic(); 1618 + clear_bit(GFAR_DOWN, &priv->state); 1619 + smp_mb__after_atomic(); 1620 + 1621 + /* Start Rx/Tx DMA and enable the interrupts */ 1622 + gfar_start(priv); 1623 + 1624 + /* force link state update after mac reset */ 1625 + priv->oldlink = 0; 1626 + priv->oldspeed = 0; 1627 + priv->oldduplex = -1; 1628 + 1629 + phy_start(ndev->phydev); 1630 + 1631 + enable_napi(priv); 1632 + 1633 + netif_tx_wake_all_queues(ndev); 1634 + 1635 + return 0; 1636 + } 1637 + 1638 + static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv) 1639 + { 1640 + struct net_device *ndev = priv->ndev; 1641 + struct phy_device *phydev = ndev->phydev; 1642 + u32 val = 0; 1643 + 1644 + if (!phydev->duplex) 1645 + return val; 1646 + 1647 + if (!priv->pause_aneg_en) { 1648 + if (priv->tx_pause_en) 1649 + val |= MACCFG1_TX_FLOW; 1650 + if (priv->rx_pause_en) 1651 + val |= MACCFG1_RX_FLOW; 1652 + } else { 1653 + u16 lcl_adv, rmt_adv; 1654 + u8 flowctrl; 1655 + /* get link partner capabilities */ 1656 + rmt_adv = 0; 1657 + if (phydev->pause) 1658 + rmt_adv = LPA_PAUSE_CAP; 1659 + if (phydev->asym_pause) 1660 + rmt_adv |= LPA_PAUSE_ASYM; 1661 + 1662 + lcl_adv = linkmode_adv_to_lcl_adv_t(phydev->advertising); 1663 + flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv); 1664 + if (flowctrl & FLOW_CTRL_TX) 1665 + val |= MACCFG1_TX_FLOW; 1666 + if (flowctrl & FLOW_CTRL_RX) 1667 + val |= MACCFG1_RX_FLOW; 1668 + } 1669 + 1670 + return val; 1671 + } 1672 + 1673 + static noinline void gfar_update_link_state(struct gfar_private *priv) 1674 + { 1675 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 1676 + struct net_device *ndev = priv->ndev; 1677 + struct phy_device *phydev = ndev->phydev; 1678 + struct gfar_priv_rx_q *rx_queue = NULL; 1679 + int i; 1680 + 1681 + if (unlikely(test_bit(GFAR_RESETTING, &priv->state))) 1682 + return; 1683 + 1684 + if (phydev->link) { 1685 + u32 tempval1 = gfar_read(&regs->maccfg1); 1686 + u32 tempval = gfar_read(&regs->maccfg2); 1687 + u32 ecntrl = gfar_read(&regs->ecntrl); 1688 + u32 tx_flow_oldval = (tempval1 & MACCFG1_TX_FLOW); 1689 + 1690 + if (phydev->duplex != priv->oldduplex) { 1691 + if (!(phydev->duplex)) 1692 + tempval &= ~(MACCFG2_FULL_DUPLEX); 1693 + else 1694 + tempval |= MACCFG2_FULL_DUPLEX; 1695 + 1696 + priv->oldduplex = phydev->duplex; 1697 + } 1698 + 1699 + if (phydev->speed != priv->oldspeed) { 1700 + switch (phydev->speed) { 1701 + case 1000: 1702 + tempval = 1703 + ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII); 1704 + 1705 + ecntrl &= ~(ECNTRL_R100); 1706 + break; 1707 + case 100: 1708 + case 10: 1709 + tempval = 1710 + ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII); 1711 + 1712 + /* Reduced mode distinguishes 1713 + * between 10 and 100 1714 + */ 1715 + if (phydev->speed == SPEED_100) 1716 + ecntrl |= ECNTRL_R100; 1717 + else 1718 + ecntrl &= ~(ECNTRL_R100); 1719 + break; 1720 + default: 1721 + netif_warn(priv, link, priv->ndev, 1722 + "Ack! Speed (%d) is not 10/100/1000!\n", 1723 + phydev->speed); 1724 + break; 1725 + } 1726 + 1727 + priv->oldspeed = phydev->speed; 1728 + } 1729 + 1730 + tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW); 1731 + tempval1 |= gfar_get_flowctrl_cfg(priv); 1732 + 1733 + /* Turn last free buffer recording on */ 1734 + if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) { 1735 + for (i = 0; i < priv->num_rx_queues; i++) { 1736 + u32 bdp_dma; 1737 + 1738 + rx_queue = priv->rx_queue[i]; 1739 + bdp_dma = gfar_rxbd_dma_lastfree(rx_queue); 1740 + gfar_write(rx_queue->rfbptr, bdp_dma); 1741 + } 1742 + 1743 + priv->tx_actual_en = 1; 1744 + } 1745 + 1746 + if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval)) 1747 + priv->tx_actual_en = 0; 1748 + 1749 + gfar_write(&regs->maccfg1, tempval1); 1750 + gfar_write(&regs->maccfg2, tempval); 1751 + gfar_write(&regs->ecntrl, ecntrl); 1752 + 1753 + if (!priv->oldlink) 1754 + priv->oldlink = 1; 1755 + 1756 + } else if (priv->oldlink) { 1757 + priv->oldlink = 0; 1758 + priv->oldspeed = 0; 1759 + priv->oldduplex = -1; 1760 + } 1761 + 1762 + if (netif_msg_link(priv)) 1763 + phy_print_status(phydev); 1764 + } 1765 + 1766 + /* Called every time the controller might need to be made 1767 + * aware of new link state. The PHY code conveys this 1768 + * information through variables in the phydev structure, and this 1769 + * function converts those variables into the appropriate 1770 + * register values, and can bring down the device if needed. 1771 + */ 1772 + static void adjust_link(struct net_device *dev) 1773 + { 1774 + struct gfar_private *priv = netdev_priv(dev); 1775 + struct phy_device *phydev = dev->phydev; 1776 + 1777 + if (unlikely(phydev->link != priv->oldlink || 1778 + (phydev->link && (phydev->duplex != priv->oldduplex || 1779 + phydev->speed != priv->oldspeed)))) 1780 + gfar_update_link_state(priv); 1781 + } 1782 + 1783 + /* Initialize TBI PHY interface for communicating with the 1784 + * SERDES lynx PHY on the chip. We communicate with this PHY 1785 + * through the MDIO bus on each controller, treating it as a 1786 + * "normal" PHY at the address found in the TBIPA register. We assume 1787 + * that the TBIPA register is valid. Either the MDIO bus code will set 1788 + * it to a value that doesn't conflict with other PHYs on the bus, or the 1789 + * value doesn't matter, as there are no other PHYs on the bus. 1790 + */ 1791 + static void gfar_configure_serdes(struct net_device *dev) 1792 + { 1793 + struct gfar_private *priv = netdev_priv(dev); 1794 + struct phy_device *tbiphy; 1795 + 1796 + if (!priv->tbi_node) { 1797 + dev_warn(&dev->dev, "error: SGMII mode requires that the " 1798 + "device tree specify a tbi-handle\n"); 1799 + return; 1800 + } 1801 + 1802 + tbiphy = of_phy_find_device(priv->tbi_node); 1803 + if (!tbiphy) { 1804 + dev_err(&dev->dev, "error: Could not get TBI device\n"); 1805 + return; 1806 + } 1807 + 1808 + /* If the link is already up, we must already be ok, and don't need to 1809 + * configure and reset the TBI<->SerDes link. Maybe U-Boot configured 1810 + * everything for us? Resetting it takes the link down and requires 1811 + * several seconds for it to come back. 1812 + */ 1813 + if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) { 1814 + put_device(&tbiphy->mdio.dev); 1815 + return; 1816 + } 1817 + 1818 + /* Single clk mode, mii mode off(for serdes communication) */ 1819 + phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT); 1820 + 1821 + phy_write(tbiphy, MII_ADVERTISE, 1822 + ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 1823 + ADVERTISE_1000XPSE_ASYM); 1824 + 1825 + phy_write(tbiphy, MII_BMCR, 1826 + BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX | 1827 + BMCR_SPEED1000); 1828 + 1829 + put_device(&tbiphy->mdio.dev); 1830 + } 1831 + 1832 + /* Initializes driver's PHY state, and attaches to the PHY. 1833 + * Returns 0 on success. 1834 + */ 1835 + static int init_phy(struct net_device *dev) 1836 + { 1837 + __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1838 + struct gfar_private *priv = netdev_priv(dev); 1839 + phy_interface_t interface = priv->interface; 1840 + struct phy_device *phydev; 1841 + struct ethtool_eee edata; 1842 + 1843 + linkmode_set_bit_array(phy_10_100_features_array, 1844 + ARRAY_SIZE(phy_10_100_features_array), 1845 + mask); 1846 + linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask); 1847 + linkmode_set_bit(ETHTOOL_LINK_MODE_MII_BIT, mask); 1848 + if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT) 1849 + linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, mask); 1850 + 1851 + priv->oldlink = 0; 1852 + priv->oldspeed = 0; 1853 + priv->oldduplex = -1; 1854 + 1855 + phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0, 1856 + interface); 1857 + if (!phydev) { 1858 + dev_err(&dev->dev, "could not attach to PHY\n"); 1859 + return -ENODEV; 1860 + } 1861 + 1862 + if (interface == PHY_INTERFACE_MODE_SGMII) 1863 + gfar_configure_serdes(dev); 1864 + 1865 + /* Remove any features not supported by the controller */ 1866 + linkmode_and(phydev->supported, phydev->supported, mask); 1867 + linkmode_copy(phydev->advertising, phydev->supported); 1868 + 1869 + /* Add support for flow control */ 1870 + phy_support_asym_pause(phydev); 1871 + 1872 + /* disable EEE autoneg, EEE not supported by eTSEC */ 1873 + memset(&edata, 0, sizeof(struct ethtool_eee)); 1874 + phy_ethtool_set_eee(phydev, &edata); 1875 + 1876 + return 0; 1877 + } 1878 + 1879 + static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb) 1880 + { 1881 + struct txfcb *fcb = skb_push(skb, GMAC_FCB_LEN); 1882 + 1883 + memset(fcb, 0, GMAC_FCB_LEN); 1884 + 1885 + return fcb; 1886 + } 1887 + 1888 + static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb, 1889 + int fcb_length) 1890 + { 1891 + /* If we're here, it's a IP packet with a TCP or UDP 1892 + * payload. We set it to checksum, using a pseudo-header 1893 + * we provide 1894 + */ 1895 + u8 flags = TXFCB_DEFAULT; 1896 + 1897 + /* Tell the controller what the protocol is 1898 + * And provide the already calculated phcs 1899 + */ 1900 + if (ip_hdr(skb)->protocol == IPPROTO_UDP) { 1901 + flags |= TXFCB_UDP; 1902 + fcb->phcs = (__force __be16)(udp_hdr(skb)->check); 1903 + } else 1904 + fcb->phcs = (__force __be16)(tcp_hdr(skb)->check); 1905 + 1906 + /* l3os is the distance between the start of the 1907 + * frame (skb->data) and the start of the IP hdr. 1908 + * l4os is the distance between the start of the 1909 + * l3 hdr and the l4 hdr 1910 + */ 1911 + fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length); 1912 + fcb->l4os = skb_network_header_len(skb); 1913 + 1914 + fcb->flags = flags; 1915 + } 1916 + 1917 + static inline void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb) 1918 + { 1919 + fcb->flags |= TXFCB_VLN; 1920 + fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb)); 1921 + } 1922 + 1923 + static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride, 1924 + struct txbd8 *base, int ring_size) 1925 + { 1926 + struct txbd8 *new_bd = bdp + stride; 1927 + 1928 + return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd; 1929 + } 1930 + 1931 + static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base, 1932 + int ring_size) 1933 + { 1934 + return skip_txbd(bdp, 1, base, ring_size); 1935 + } 1936 + 1937 + /* eTSEC12: csum generation not supported for some fcb offsets */ 1938 + static inline bool gfar_csum_errata_12(struct gfar_private *priv, 1939 + unsigned long fcb_addr) 1940 + { 1941 + return (gfar_has_errata(priv, GFAR_ERRATA_12) && 1942 + (fcb_addr % 0x20) > 0x18); 1943 + } 1944 + 1945 + /* eTSEC76: csum generation for frames larger than 2500 may 1946 + * cause excess delays before start of transmission 1947 + */ 1948 + static inline bool gfar_csum_errata_76(struct gfar_private *priv, 1949 + unsigned int len) 1950 + { 1951 + return (gfar_has_errata(priv, GFAR_ERRATA_76) && 1952 + (len > 2500)); 1953 + } 1954 + 1955 + /* This is called by the kernel when a frame is ready for transmission. 1956 + * It is pointed to by the dev->hard_start_xmit function pointer 1957 + */ 1958 + static netdev_tx_t gfar_start_xmit(struct sk_buff *skb, struct net_device *dev) 1959 + { 1960 + struct gfar_private *priv = netdev_priv(dev); 1961 + struct gfar_priv_tx_q *tx_queue = NULL; 1962 + struct netdev_queue *txq; 1963 + struct gfar __iomem *regs = NULL; 1964 + struct txfcb *fcb = NULL; 1965 + struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL; 1966 + u32 lstatus; 1967 + skb_frag_t *frag; 1968 + int i, rq = 0; 1969 + int do_tstamp, do_csum, do_vlan; 1970 + u32 bufaddr; 1971 + unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0; 1972 + 1973 + rq = skb->queue_mapping; 1974 + tx_queue = priv->tx_queue[rq]; 1975 + txq = netdev_get_tx_queue(dev, rq); 1976 + base = tx_queue->tx_bd_base; 1977 + regs = tx_queue->grp->regs; 1978 + 1979 + do_csum = (CHECKSUM_PARTIAL == skb->ip_summed); 1980 + do_vlan = skb_vlan_tag_present(skb); 1981 + do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 1982 + priv->hwts_tx_en; 1983 + 1984 + if (do_csum || do_vlan) 1985 + fcb_len = GMAC_FCB_LEN; 1986 + 1987 + /* check if time stamp should be generated */ 1988 + if (unlikely(do_tstamp)) 1989 + fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN; 1990 + 1991 + /* make space for additional header when fcb is needed */ 1992 + if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) { 1993 + struct sk_buff *skb_new; 1994 + 1995 + skb_new = skb_realloc_headroom(skb, fcb_len); 1996 + if (!skb_new) { 1997 + dev->stats.tx_errors++; 1998 + dev_kfree_skb_any(skb); 1999 + return NETDEV_TX_OK; 2000 + } 2001 + 2002 + if (skb->sk) 2003 + skb_set_owner_w(skb_new, skb->sk); 2004 + dev_consume_skb_any(skb); 2005 + skb = skb_new; 2006 + } 2007 + 2008 + /* total number of fragments in the SKB */ 2009 + nr_frags = skb_shinfo(skb)->nr_frags; 2010 + 2011 + /* calculate the required number of TxBDs for this skb */ 2012 + if (unlikely(do_tstamp)) 2013 + nr_txbds = nr_frags + 2; 2014 + else 2015 + nr_txbds = nr_frags + 1; 2016 + 2017 + /* check if there is space to queue this packet */ 2018 + if (nr_txbds > tx_queue->num_txbdfree) { 2019 + /* no space, stop the queue */ 2020 + netif_tx_stop_queue(txq); 2021 + dev->stats.tx_fifo_errors++; 2022 + return NETDEV_TX_BUSY; 2023 + } 2024 + 2025 + /* Update transmit stats */ 2026 + bytes_sent = skb->len; 2027 + tx_queue->stats.tx_bytes += bytes_sent; 2028 + /* keep Tx bytes on wire for BQL accounting */ 2029 + GFAR_CB(skb)->bytes_sent = bytes_sent; 2030 + tx_queue->stats.tx_packets++; 2031 + 2032 + txbdp = txbdp_start = tx_queue->cur_tx; 2033 + lstatus = be32_to_cpu(txbdp->lstatus); 2034 + 2035 + /* Add TxPAL between FCB and frame if required */ 2036 + if (unlikely(do_tstamp)) { 2037 + skb_push(skb, GMAC_TXPAL_LEN); 2038 + memset(skb->data, 0, GMAC_TXPAL_LEN); 2039 + } 2040 + 2041 + /* Add TxFCB if required */ 2042 + if (fcb_len) { 2043 + fcb = gfar_add_fcb(skb); 2044 + lstatus |= BD_LFLAG(TXBD_TOE); 2045 + } 2046 + 2047 + /* Set up checksumming */ 2048 + if (do_csum) { 2049 + gfar_tx_checksum(skb, fcb, fcb_len); 2050 + 2051 + if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) || 2052 + unlikely(gfar_csum_errata_76(priv, skb->len))) { 2053 + __skb_pull(skb, GMAC_FCB_LEN); 2054 + skb_checksum_help(skb); 2055 + if (do_vlan || do_tstamp) { 2056 + /* put back a new fcb for vlan/tstamp TOE */ 2057 + fcb = gfar_add_fcb(skb); 2058 + } else { 2059 + /* Tx TOE not used */ 2060 + lstatus &= ~(BD_LFLAG(TXBD_TOE)); 2061 + fcb = NULL; 2062 + } 2063 + } 2064 + } 2065 + 2066 + if (do_vlan) 2067 + gfar_tx_vlan(skb, fcb); 2068 + 2069 + bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb), 2070 + DMA_TO_DEVICE); 2071 + if (unlikely(dma_mapping_error(priv->dev, bufaddr))) 2072 + goto dma_map_err; 2073 + 2074 + txbdp_start->bufPtr = cpu_to_be32(bufaddr); 2075 + 2076 + /* Time stamp insertion requires one additional TxBD */ 2077 + if (unlikely(do_tstamp)) 2078 + txbdp_tstamp = txbdp = next_txbd(txbdp, base, 2079 + tx_queue->tx_ring_size); 2080 + 2081 + if (likely(!nr_frags)) { 2082 + if (likely(!do_tstamp)) 2083 + lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 2084 + } else { 2085 + u32 lstatus_start = lstatus; 2086 + 2087 + /* Place the fragment addresses and lengths into the TxBDs */ 2088 + frag = &skb_shinfo(skb)->frags[0]; 2089 + for (i = 0; i < nr_frags; i++, frag++) { 2090 + unsigned int size; 2091 + 2092 + /* Point at the next BD, wrapping as needed */ 2093 + txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 2094 + 2095 + size = skb_frag_size(frag); 2096 + 2097 + lstatus = be32_to_cpu(txbdp->lstatus) | size | 2098 + BD_LFLAG(TXBD_READY); 2099 + 2100 + /* Handle the last BD specially */ 2101 + if (i == nr_frags - 1) 2102 + lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 2103 + 2104 + bufaddr = skb_frag_dma_map(priv->dev, frag, 0, 2105 + size, DMA_TO_DEVICE); 2106 + if (unlikely(dma_mapping_error(priv->dev, bufaddr))) 2107 + goto dma_map_err; 2108 + 2109 + /* set the TxBD length and buffer pointer */ 2110 + txbdp->bufPtr = cpu_to_be32(bufaddr); 2111 + txbdp->lstatus = cpu_to_be32(lstatus); 2112 + } 2113 + 2114 + lstatus = lstatus_start; 2115 + } 2116 + 2117 + /* If time stamping is requested one additional TxBD must be set up. The 2118 + * first TxBD points to the FCB and must have a data length of 2119 + * GMAC_FCB_LEN. The second TxBD points to the actual frame data with 2120 + * the full frame length. 2121 + */ 2122 + if (unlikely(do_tstamp)) { 2123 + u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus); 2124 + 2125 + bufaddr = be32_to_cpu(txbdp_start->bufPtr); 2126 + bufaddr += fcb_len; 2127 + 2128 + lstatus_ts |= BD_LFLAG(TXBD_READY) | 2129 + (skb_headlen(skb) - fcb_len); 2130 + if (!nr_frags) 2131 + lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 2132 + 2133 + txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr); 2134 + txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts); 2135 + lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN; 2136 + 2137 + /* Setup tx hardware time stamping */ 2138 + skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 2139 + fcb->ptp = 1; 2140 + } else { 2141 + lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb); 2142 + } 2143 + 2144 + netdev_tx_sent_queue(txq, bytes_sent); 2145 + 2146 + gfar_wmb(); 2147 + 2148 + txbdp_start->lstatus = cpu_to_be32(lstatus); 2149 + 2150 + gfar_wmb(); /* force lstatus write before tx_skbuff */ 2151 + 2152 + tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb; 2153 + 2154 + /* Update the current skb pointer to the next entry we will use 2155 + * (wrapping if necessary) 2156 + */ 2157 + tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) & 2158 + TX_RING_MOD_MASK(tx_queue->tx_ring_size); 2159 + 2160 + tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size); 2161 + 2162 + /* We can work in parallel with gfar_clean_tx_ring(), except 2163 + * when modifying num_txbdfree. Note that we didn't grab the lock 2164 + * when we were reading the num_txbdfree and checking for available 2165 + * space, that's because outside of this function it can only grow. 2166 + */ 2167 + spin_lock_bh(&tx_queue->txlock); 2168 + /* reduce TxBD free count */ 2169 + tx_queue->num_txbdfree -= (nr_txbds); 2170 + spin_unlock_bh(&tx_queue->txlock); 2171 + 2172 + /* If the next BD still needs to be cleaned up, then the bds 2173 + * are full. We need to tell the kernel to stop sending us stuff. 2174 + */ 2175 + if (!tx_queue->num_txbdfree) { 2176 + netif_tx_stop_queue(txq); 2177 + 2178 + dev->stats.tx_fifo_errors++; 2179 + } 2180 + 2181 + /* Tell the DMA to go go go */ 2182 + gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex); 2183 + 2184 + return NETDEV_TX_OK; 2185 + 2186 + dma_map_err: 2187 + txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size); 2188 + if (do_tstamp) 2189 + txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 2190 + for (i = 0; i < nr_frags; i++) { 2191 + lstatus = be32_to_cpu(txbdp->lstatus); 2192 + if (!(lstatus & BD_LFLAG(TXBD_READY))) 2193 + break; 2194 + 2195 + lstatus &= ~BD_LFLAG(TXBD_READY); 2196 + txbdp->lstatus = cpu_to_be32(lstatus); 2197 + bufaddr = be32_to_cpu(txbdp->bufPtr); 2198 + dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length), 2199 + DMA_TO_DEVICE); 2200 + txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 2201 + } 2202 + gfar_wmb(); 2203 + dev_kfree_skb_any(skb); 2204 + return NETDEV_TX_OK; 2205 + } 2206 + 2207 + /* Changes the mac address if the controller is not running. */ 2208 + static int gfar_set_mac_address(struct net_device *dev) 2209 + { 2210 + gfar_set_mac_for_addr(dev, 0, dev->dev_addr); 2211 + 2212 + return 0; 2213 + } 2214 + 2215 + static int gfar_change_mtu(struct net_device *dev, int new_mtu) 2216 + { 2217 + struct gfar_private *priv = netdev_priv(dev); 2218 + 2219 + while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state)) 2220 + cpu_relax(); 2221 + 2222 + if (dev->flags & IFF_UP) 2223 + stop_gfar(dev); 2224 + 2225 + dev->mtu = new_mtu; 2226 + 2227 + if (dev->flags & IFF_UP) 2228 + startup_gfar(dev); 2229 + 2230 + clear_bit_unlock(GFAR_RESETTING, &priv->state); 2231 + 2232 + return 0; 2233 + } 2234 + 2235 + void reset_gfar(struct net_device *ndev) 2236 + { 2237 + struct gfar_private *priv = netdev_priv(ndev); 2238 + 2239 + while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state)) 2240 + cpu_relax(); 2241 + 2242 + stop_gfar(ndev); 2243 + startup_gfar(ndev); 2244 + 2245 + clear_bit_unlock(GFAR_RESETTING, &priv->state); 2246 + } 2247 + 2248 + /* gfar_reset_task gets scheduled when a packet has not been 2249 + * transmitted after a set amount of time. 2250 + * For now, assume that clearing out all the structures, and 2251 + * starting over will fix the problem. 2252 + */ 2253 + static void gfar_reset_task(struct work_struct *work) 2254 + { 2255 + struct gfar_private *priv = container_of(work, struct gfar_private, 2256 + reset_task); 2257 + reset_gfar(priv->ndev); 2258 + } 2259 + 2260 + static void gfar_timeout(struct net_device *dev) 2261 + { 2262 + struct gfar_private *priv = netdev_priv(dev); 2263 + 2264 + dev->stats.tx_errors++; 2265 + schedule_work(&priv->reset_task); 2266 + } 2267 + 2268 + static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr) 2269 + { 2270 + struct hwtstamp_config config; 2271 + struct gfar_private *priv = netdev_priv(netdev); 2272 + 2273 + if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 2274 + return -EFAULT; 2275 + 2276 + /* reserved for future extensions */ 2277 + if (config.flags) 2278 + return -EINVAL; 2279 + 2280 + switch (config.tx_type) { 2281 + case HWTSTAMP_TX_OFF: 2282 + priv->hwts_tx_en = 0; 2283 + break; 2284 + case HWTSTAMP_TX_ON: 2285 + if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) 2286 + return -ERANGE; 2287 + priv->hwts_tx_en = 1; 2288 + break; 2289 + default: 2290 + return -ERANGE; 2291 + } 2292 + 2293 + switch (config.rx_filter) { 2294 + case HWTSTAMP_FILTER_NONE: 2295 + if (priv->hwts_rx_en) { 2296 + priv->hwts_rx_en = 0; 2297 + reset_gfar(netdev); 2298 + } 2299 + break; 2300 + default: 2301 + if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) 2302 + return -ERANGE; 2303 + if (!priv->hwts_rx_en) { 2304 + priv->hwts_rx_en = 1; 2305 + reset_gfar(netdev); 2306 + } 2307 + config.rx_filter = HWTSTAMP_FILTER_ALL; 2308 + break; 2309 + } 2310 + 2311 + return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 2312 + -EFAULT : 0; 2313 + } 2314 + 2315 + static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr) 2316 + { 2317 + struct hwtstamp_config config; 2318 + struct gfar_private *priv = netdev_priv(netdev); 2319 + 2320 + config.flags = 0; 2321 + config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 2322 + config.rx_filter = (priv->hwts_rx_en ? 2323 + HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE); 2324 + 2325 + return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 2326 + -EFAULT : 0; 2327 + } 2328 + 2329 + static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 2330 + { 2331 + struct phy_device *phydev = dev->phydev; 2332 + 2333 + if (!netif_running(dev)) 2334 + return -EINVAL; 2335 + 2336 + if (cmd == SIOCSHWTSTAMP) 2337 + return gfar_hwtstamp_set(dev, rq); 2338 + if (cmd == SIOCGHWTSTAMP) 2339 + return gfar_hwtstamp_get(dev, rq); 2340 + 2341 + if (!phydev) 2342 + return -ENODEV; 2343 + 2344 + return phy_mii_ioctl(phydev, rq, cmd); 2345 + } 2346 + 2347 + /* Interrupt Handler for Transmit complete */ 2348 + static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue) 2349 + { 2350 + struct net_device *dev = tx_queue->dev; 2351 + struct netdev_queue *txq; 2352 + struct gfar_private *priv = netdev_priv(dev); 2353 + struct txbd8 *bdp, *next = NULL; 2354 + struct txbd8 *lbdp = NULL; 2355 + struct txbd8 *base = tx_queue->tx_bd_base; 2356 + struct sk_buff *skb; 2357 + int skb_dirtytx; 2358 + int tx_ring_size = tx_queue->tx_ring_size; 2359 + int frags = 0, nr_txbds = 0; 2360 + int i; 2361 + int howmany = 0; 2362 + int tqi = tx_queue->qindex; 2363 + unsigned int bytes_sent = 0; 2364 + u32 lstatus; 2365 + size_t buflen; 2366 + 2367 + txq = netdev_get_tx_queue(dev, tqi); 2368 + bdp = tx_queue->dirty_tx; 2369 + skb_dirtytx = tx_queue->skb_dirtytx; 2370 + 2371 + while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) { 2372 + 2373 + frags = skb_shinfo(skb)->nr_frags; 2374 + 2375 + /* When time stamping, one additional TxBD must be freed. 2376 + * Also, we need to dma_unmap_single() the TxPAL. 2377 + */ 2378 + if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) 2379 + nr_txbds = frags + 2; 2380 + else 2381 + nr_txbds = frags + 1; 2382 + 2383 + lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size); 2384 + 2385 + lstatus = be32_to_cpu(lbdp->lstatus); 2386 + 2387 + /* Only clean completed frames */ 2388 + if ((lstatus & BD_LFLAG(TXBD_READY)) && 2389 + (lstatus & BD_LENGTH_MASK)) 2390 + break; 2391 + 2392 + if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) { 2393 + next = next_txbd(bdp, base, tx_ring_size); 2394 + buflen = be16_to_cpu(next->length) + 2395 + GMAC_FCB_LEN + GMAC_TXPAL_LEN; 2396 + } else 2397 + buflen = be16_to_cpu(bdp->length); 2398 + 2399 + dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr), 2400 + buflen, DMA_TO_DEVICE); 2401 + 2402 + if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) { 2403 + struct skb_shared_hwtstamps shhwtstamps; 2404 + u64 *ns = (u64 *)(((uintptr_t)skb->data + 0x10) & 2405 + ~0x7UL); 2406 + 2407 + memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 2408 + shhwtstamps.hwtstamp = ns_to_ktime(be64_to_cpu(*ns)); 2409 + skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN); 2410 + skb_tstamp_tx(skb, &shhwtstamps); 2411 + gfar_clear_txbd_status(bdp); 2412 + bdp = next; 2413 + } 2414 + 2415 + gfar_clear_txbd_status(bdp); 2416 + bdp = next_txbd(bdp, base, tx_ring_size); 2417 + 2418 + for (i = 0; i < frags; i++) { 2419 + dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr), 2420 + be16_to_cpu(bdp->length), 2421 + DMA_TO_DEVICE); 2422 + gfar_clear_txbd_status(bdp); 2423 + bdp = next_txbd(bdp, base, tx_ring_size); 2424 + } 2425 + 2426 + bytes_sent += GFAR_CB(skb)->bytes_sent; 2427 + 2428 + dev_kfree_skb_any(skb); 2429 + 2430 + tx_queue->tx_skbuff[skb_dirtytx] = NULL; 2431 + 2432 + skb_dirtytx = (skb_dirtytx + 1) & 2433 + TX_RING_MOD_MASK(tx_ring_size); 2434 + 2435 + howmany++; 2436 + spin_lock(&tx_queue->txlock); 2437 + tx_queue->num_txbdfree += nr_txbds; 2438 + spin_unlock(&tx_queue->txlock); 2439 + } 2440 + 2441 + /* If we freed a buffer, we can restart transmission, if necessary */ 2442 + if (tx_queue->num_txbdfree && 2443 + netif_tx_queue_stopped(txq) && 2444 + !(test_bit(GFAR_DOWN, &priv->state))) 2445 + netif_wake_subqueue(priv->ndev, tqi); 2446 + 2447 + /* Update dirty indicators */ 2448 + tx_queue->skb_dirtytx = skb_dirtytx; 2449 + tx_queue->dirty_tx = bdp; 2450 + 2451 + netdev_tx_completed_queue(txq, howmany, bytes_sent); 2452 + } 2453 + 2454 + static void count_errors(u32 lstatus, struct net_device *ndev) 2455 + { 2456 + struct gfar_private *priv = netdev_priv(ndev); 2457 + struct net_device_stats *stats = &ndev->stats; 2458 + struct gfar_extra_stats *estats = &priv->extra_stats; 2459 + 2460 + /* If the packet was truncated, none of the other errors matter */ 2461 + if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) { 2462 + stats->rx_length_errors++; 2463 + 2464 + atomic64_inc(&estats->rx_trunc); 2465 + 2466 + return; 2467 + } 2468 + /* Count the errors, if there were any */ 2469 + if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) { 2470 + stats->rx_length_errors++; 2471 + 2472 + if (lstatus & BD_LFLAG(RXBD_LARGE)) 2473 + atomic64_inc(&estats->rx_large); 2474 + else 2475 + atomic64_inc(&estats->rx_short); 2476 + } 2477 + if (lstatus & BD_LFLAG(RXBD_NONOCTET)) { 2478 + stats->rx_frame_errors++; 2479 + atomic64_inc(&estats->rx_nonoctet); 2480 + } 2481 + if (lstatus & BD_LFLAG(RXBD_CRCERR)) { 2482 + atomic64_inc(&estats->rx_crcerr); 2483 + stats->rx_crc_errors++; 2484 + } 2485 + if (lstatus & BD_LFLAG(RXBD_OVERRUN)) { 2486 + atomic64_inc(&estats->rx_overrun); 2487 + stats->rx_over_errors++; 2488 + } 2489 + } 2490 + 2491 + static irqreturn_t gfar_receive(int irq, void *grp_id) 2492 + { 2493 + struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id; 2494 + unsigned long flags; 2495 + u32 imask, ievent; 2496 + 2497 + ievent = gfar_read(&grp->regs->ievent); 2498 + 2499 + if (unlikely(ievent & IEVENT_FGPI)) { 2500 + gfar_write(&grp->regs->ievent, IEVENT_FGPI); 2501 + return IRQ_HANDLED; 2502 + } 2503 + 2504 + if (likely(napi_schedule_prep(&grp->napi_rx))) { 2505 + spin_lock_irqsave(&grp->grplock, flags); 2506 + imask = gfar_read(&grp->regs->imask); 2507 + imask &= IMASK_RX_DISABLED; 2508 + gfar_write(&grp->regs->imask, imask); 2509 + spin_unlock_irqrestore(&grp->grplock, flags); 2510 + __napi_schedule(&grp->napi_rx); 2511 + } else { 2512 + /* Clear IEVENT, so interrupts aren't called again 2513 + * because of the packets that have already arrived. 2514 + */ 2515 + gfar_write(&grp->regs->ievent, IEVENT_RX_MASK); 2516 + } 2517 + 2518 + return IRQ_HANDLED; 2519 + } 2520 + 2521 + /* Interrupt Handler for Transmit complete */ 2522 + static irqreturn_t gfar_transmit(int irq, void *grp_id) 2523 + { 2524 + struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id; 2525 + unsigned long flags; 2526 + u32 imask; 2527 + 2528 + if (likely(napi_schedule_prep(&grp->napi_tx))) { 2529 + spin_lock_irqsave(&grp->grplock, flags); 2530 + imask = gfar_read(&grp->regs->imask); 2531 + imask &= IMASK_TX_DISABLED; 2532 + gfar_write(&grp->regs->imask, imask); 2533 + spin_unlock_irqrestore(&grp->grplock, flags); 2534 + __napi_schedule(&grp->napi_tx); 2535 + } else { 2536 + /* Clear IEVENT, so interrupts aren't called again 2537 + * because of the packets that have already arrived. 2538 + */ 2539 + gfar_write(&grp->regs->ievent, IEVENT_TX_MASK); 2540 + } 2541 + 2542 + return IRQ_HANDLED; 2543 + } 2544 + 2545 + static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus, 2546 + struct sk_buff *skb, bool first) 2547 + { 2548 + int size = lstatus & BD_LENGTH_MASK; 2549 + struct page *page = rxb->page; 2550 + 2551 + if (likely(first)) { 2552 + skb_put(skb, size); 2553 + } else { 2554 + /* the last fragments' length contains the full frame length */ 2555 + if (lstatus & BD_LFLAG(RXBD_LAST)) 2556 + size -= skb->len; 2557 + 2558 + skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 2559 + rxb->page_offset + RXBUF_ALIGNMENT, 2560 + size, GFAR_RXB_TRUESIZE); 2561 + } 2562 + 2563 + /* try reuse page */ 2564 + if (unlikely(page_count(page) != 1 || page_is_pfmemalloc(page))) 2565 + return false; 2566 + 2567 + /* change offset to the other half */ 2568 + rxb->page_offset ^= GFAR_RXB_TRUESIZE; 2569 + 2570 + page_ref_inc(page); 2571 + 2572 + return true; 2573 + } 2574 + 2575 + static void gfar_reuse_rx_page(struct gfar_priv_rx_q *rxq, 2576 + struct gfar_rx_buff *old_rxb) 2577 + { 2578 + struct gfar_rx_buff *new_rxb; 2579 + u16 nta = rxq->next_to_alloc; 2580 + 2581 + new_rxb = &rxq->rx_buff[nta]; 2582 + 2583 + /* find next buf that can reuse a page */ 2584 + nta++; 2585 + rxq->next_to_alloc = (nta < rxq->rx_ring_size) ? nta : 0; 2586 + 2587 + /* copy page reference */ 2588 + *new_rxb = *old_rxb; 2589 + 2590 + /* sync for use by the device */ 2591 + dma_sync_single_range_for_device(rxq->dev, old_rxb->dma, 2592 + old_rxb->page_offset, 2593 + GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE); 2594 + } 2595 + 2596 + static struct sk_buff *gfar_get_next_rxbuff(struct gfar_priv_rx_q *rx_queue, 2597 + u32 lstatus, struct sk_buff *skb) 2598 + { 2599 + struct gfar_rx_buff *rxb = &rx_queue->rx_buff[rx_queue->next_to_clean]; 2600 + struct page *page = rxb->page; 2601 + bool first = false; 2602 + 2603 + if (likely(!skb)) { 2604 + void *buff_addr = page_address(page) + rxb->page_offset; 2605 + 2606 + skb = build_skb(buff_addr, GFAR_SKBFRAG_SIZE); 2607 + if (unlikely(!skb)) { 2608 + gfar_rx_alloc_err(rx_queue); 2609 + return NULL; 2610 + } 2611 + skb_reserve(skb, RXBUF_ALIGNMENT); 2612 + first = true; 2613 + } 2614 + 2615 + dma_sync_single_range_for_cpu(rx_queue->dev, rxb->dma, rxb->page_offset, 2616 + GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE); 2617 + 2618 + if (gfar_add_rx_frag(rxb, lstatus, skb, first)) { 2619 + /* reuse the free half of the page */ 2620 + gfar_reuse_rx_page(rx_queue, rxb); 2621 + } else { 2622 + /* page cannot be reused, unmap it */ 2623 + dma_unmap_page(rx_queue->dev, rxb->dma, 2624 + PAGE_SIZE, DMA_FROM_DEVICE); 2625 + } 2626 + 2627 + /* clear rxb content */ 2628 + rxb->page = NULL; 2629 + 2630 + return skb; 2631 + } 2632 + 2633 + static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb) 2634 + { 2635 + /* If valid headers were found, and valid sums 2636 + * were verified, then we tell the kernel that no 2637 + * checksumming is necessary. Otherwise, it is [FIXME] 2638 + */ 2639 + if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) == 2640 + (RXFCB_CIP | RXFCB_CTU)) 2641 + skb->ip_summed = CHECKSUM_UNNECESSARY; 2642 + else 2643 + skb_checksum_none_assert(skb); 2644 + } 2645 + 2646 + /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */ 2647 + static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb) 2648 + { 2649 + struct gfar_private *priv = netdev_priv(ndev); 2650 + struct rxfcb *fcb = NULL; 2651 + 2652 + /* fcb is at the beginning if exists */ 2653 + fcb = (struct rxfcb *)skb->data; 2654 + 2655 + /* Remove the FCB from the skb 2656 + * Remove the padded bytes, if there are any 2657 + */ 2658 + if (priv->uses_rxfcb) 2659 + skb_pull(skb, GMAC_FCB_LEN); 2660 + 2661 + /* Get receive timestamp from the skb */ 2662 + if (priv->hwts_rx_en) { 2663 + struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 2664 + u64 *ns = (u64 *) skb->data; 2665 + 2666 + memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2667 + shhwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*ns)); 2668 + } 2669 + 2670 + if (priv->padding) 2671 + skb_pull(skb, priv->padding); 2672 + 2673 + /* Trim off the FCS */ 2674 + pskb_trim(skb, skb->len - ETH_FCS_LEN); 2675 + 2676 + if (ndev->features & NETIF_F_RXCSUM) 2677 + gfar_rx_checksum(skb, fcb); 2678 + 2679 + /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here. 2680 + * Even if vlan rx accel is disabled, on some chips 2681 + * RXFCB_VLN is pseudo randomly set. 2682 + */ 2683 + if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX && 2684 + be16_to_cpu(fcb->flags) & RXFCB_VLN) 2685 + __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 2686 + be16_to_cpu(fcb->vlctl)); 2687 + } 2688 + 2689 + /* gfar_clean_rx_ring() -- Processes each frame in the rx ring 2690 + * until the budget/quota has been reached. Returns the number 2691 + * of frames handled 2692 + */ 2693 + static int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, 2694 + int rx_work_limit) 2695 + { 2696 + struct net_device *ndev = rx_queue->ndev; 2697 + struct gfar_private *priv = netdev_priv(ndev); 2698 + struct rxbd8 *bdp; 2699 + int i, howmany = 0; 2700 + struct sk_buff *skb = rx_queue->skb; 2701 + int cleaned_cnt = gfar_rxbd_unused(rx_queue); 2702 + unsigned int total_bytes = 0, total_pkts = 0; 2703 + 2704 + /* Get the first full descriptor */ 2705 + i = rx_queue->next_to_clean; 2706 + 2707 + while (rx_work_limit--) { 2708 + u32 lstatus; 2709 + 2710 + if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) { 2711 + gfar_alloc_rx_buffs(rx_queue, cleaned_cnt); 2712 + cleaned_cnt = 0; 2713 + } 2714 + 2715 + bdp = &rx_queue->rx_bd_base[i]; 2716 + lstatus = be32_to_cpu(bdp->lstatus); 2717 + if (lstatus & BD_LFLAG(RXBD_EMPTY)) 2718 + break; 2719 + 2720 + /* order rx buffer descriptor reads */ 2721 + rmb(); 2722 + 2723 + /* fetch next to clean buffer from the ring */ 2724 + skb = gfar_get_next_rxbuff(rx_queue, lstatus, skb); 2725 + if (unlikely(!skb)) 2726 + break; 2727 + 2728 + cleaned_cnt++; 2729 + howmany++; 2730 + 2731 + if (unlikely(++i == rx_queue->rx_ring_size)) 2732 + i = 0; 2733 + 2734 + rx_queue->next_to_clean = i; 2735 + 2736 + /* fetch next buffer if not the last in frame */ 2737 + if (!(lstatus & BD_LFLAG(RXBD_LAST))) 2738 + continue; 2739 + 2740 + if (unlikely(lstatus & BD_LFLAG(RXBD_ERR))) { 2741 + count_errors(lstatus, ndev); 2742 + 2743 + /* discard faulty buffer */ 2744 + dev_kfree_skb(skb); 2745 + skb = NULL; 2746 + rx_queue->stats.rx_dropped++; 2747 + continue; 2748 + } 2749 + 2750 + gfar_process_frame(ndev, skb); 2751 + 2752 + /* Increment the number of packets */ 2753 + total_pkts++; 2754 + total_bytes += skb->len; 2755 + 2756 + skb_record_rx_queue(skb, rx_queue->qindex); 2757 + 2758 + skb->protocol = eth_type_trans(skb, ndev); 2759 + 2760 + /* Send the packet up the stack */ 2761 + napi_gro_receive(&rx_queue->grp->napi_rx, skb); 2762 + 2763 + skb = NULL; 2764 + } 2765 + 2766 + /* Store incomplete frames for completion */ 2767 + rx_queue->skb = skb; 2768 + 2769 + rx_queue->stats.rx_packets += total_pkts; 2770 + rx_queue->stats.rx_bytes += total_bytes; 2771 + 2772 + if (cleaned_cnt) 2773 + gfar_alloc_rx_buffs(rx_queue, cleaned_cnt); 2774 + 2775 + /* Update Last Free RxBD pointer for LFC */ 2776 + if (unlikely(priv->tx_actual_en)) { 2777 + u32 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue); 2778 + 2779 + gfar_write(rx_queue->rfbptr, bdp_dma); 2780 + } 2781 + 2782 + return howmany; 2783 + } 2784 + 2785 + static int gfar_poll_rx_sq(struct napi_struct *napi, int budget) 2786 + { 2787 + struct gfar_priv_grp *gfargrp = 2788 + container_of(napi, struct gfar_priv_grp, napi_rx); 2789 + struct gfar __iomem *regs = gfargrp->regs; 2790 + struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue; 2791 + int work_done = 0; 2792 + 2793 + /* Clear IEVENT, so interrupts aren't called again 2794 + * because of the packets that have already arrived 2795 + */ 2796 + gfar_write(&regs->ievent, IEVENT_RX_MASK); 2797 + 2798 + work_done = gfar_clean_rx_ring(rx_queue, budget); 2799 + 2800 + if (work_done < budget) { 2801 + u32 imask; 2802 + napi_complete_done(napi, work_done); 2803 + /* Clear the halt bit in RSTAT */ 2804 + gfar_write(&regs->rstat, gfargrp->rstat); 2805 + 2806 + spin_lock_irq(&gfargrp->grplock); 2807 + imask = gfar_read(&regs->imask); 2808 + imask |= IMASK_RX_DEFAULT; 2809 + gfar_write(&regs->imask, imask); 2810 + spin_unlock_irq(&gfargrp->grplock); 2811 + } 2812 + 2813 + return work_done; 2814 + } 2815 + 2816 + static int gfar_poll_tx_sq(struct napi_struct *napi, int budget) 2817 + { 2818 + struct gfar_priv_grp *gfargrp = 2819 + container_of(napi, struct gfar_priv_grp, napi_tx); 2820 + struct gfar __iomem *regs = gfargrp->regs; 2821 + struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue; 2822 + u32 imask; 2823 + 2824 + /* Clear IEVENT, so interrupts aren't called again 2825 + * because of the packets that have already arrived 2826 + */ 2827 + gfar_write(&regs->ievent, IEVENT_TX_MASK); 2828 + 2829 + /* run Tx cleanup to completion */ 2830 + if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) 2831 + gfar_clean_tx_ring(tx_queue); 2832 + 2833 + napi_complete(napi); 2834 + 2835 + spin_lock_irq(&gfargrp->grplock); 2836 + imask = gfar_read(&regs->imask); 2837 + imask |= IMASK_TX_DEFAULT; 2838 + gfar_write(&regs->imask, imask); 2839 + spin_unlock_irq(&gfargrp->grplock); 2840 + 2841 + return 0; 2842 + } 2843 + 2844 + static int gfar_poll_rx(struct napi_struct *napi, int budget) 2845 + { 2846 + struct gfar_priv_grp *gfargrp = 2847 + container_of(napi, struct gfar_priv_grp, napi_rx); 2848 + struct gfar_private *priv = gfargrp->priv; 2849 + struct gfar __iomem *regs = gfargrp->regs; 2850 + struct gfar_priv_rx_q *rx_queue = NULL; 2851 + int work_done = 0, work_done_per_q = 0; 2852 + int i, budget_per_q = 0; 2853 + unsigned long rstat_rxf; 2854 + int num_act_queues; 2855 + 2856 + /* Clear IEVENT, so interrupts aren't called again 2857 + * because of the packets that have already arrived 2858 + */ 2859 + gfar_write(&regs->ievent, IEVENT_RX_MASK); 2860 + 2861 + rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK; 2862 + 2863 + num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS); 2864 + if (num_act_queues) 2865 + budget_per_q = budget/num_act_queues; 2866 + 2867 + for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) { 2868 + /* skip queue if not active */ 2869 + if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i))) 2870 + continue; 2871 + 2872 + rx_queue = priv->rx_queue[i]; 2873 + work_done_per_q = 2874 + gfar_clean_rx_ring(rx_queue, budget_per_q); 2875 + work_done += work_done_per_q; 2876 + 2877 + /* finished processing this queue */ 2878 + if (work_done_per_q < budget_per_q) { 2879 + /* clear active queue hw indication */ 2880 + gfar_write(&regs->rstat, 2881 + RSTAT_CLEAR_RXF0 >> i); 2882 + num_act_queues--; 2883 + 2884 + if (!num_act_queues) 2885 + break; 2886 + } 2887 + } 2888 + 2889 + if (!num_act_queues) { 2890 + u32 imask; 2891 + napi_complete_done(napi, work_done); 2892 + 2893 + /* Clear the halt bit in RSTAT */ 2894 + gfar_write(&regs->rstat, gfargrp->rstat); 2895 + 2896 + spin_lock_irq(&gfargrp->grplock); 2897 + imask = gfar_read(&regs->imask); 2898 + imask |= IMASK_RX_DEFAULT; 2899 + gfar_write(&regs->imask, imask); 2900 + spin_unlock_irq(&gfargrp->grplock); 2901 + } 2902 + 2903 + return work_done; 2904 + } 2905 + 2906 + static int gfar_poll_tx(struct napi_struct *napi, int budget) 2907 + { 2908 + struct gfar_priv_grp *gfargrp = 2909 + container_of(napi, struct gfar_priv_grp, napi_tx); 2910 + struct gfar_private *priv = gfargrp->priv; 2911 + struct gfar __iomem *regs = gfargrp->regs; 2912 + struct gfar_priv_tx_q *tx_queue = NULL; 2913 + int has_tx_work = 0; 2914 + int i; 2915 + 2916 + /* Clear IEVENT, so interrupts aren't called again 2917 + * because of the packets that have already arrived 2918 + */ 2919 + gfar_write(&regs->ievent, IEVENT_TX_MASK); 2920 + 2921 + for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) { 2922 + tx_queue = priv->tx_queue[i]; 2923 + /* run Tx cleanup to completion */ 2924 + if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) { 2925 + gfar_clean_tx_ring(tx_queue); 2926 + has_tx_work = 1; 2927 + } 2928 + } 2929 + 2930 + if (!has_tx_work) { 2931 + u32 imask; 2932 + napi_complete(napi); 2933 + 2934 + spin_lock_irq(&gfargrp->grplock); 2935 + imask = gfar_read(&regs->imask); 2936 + imask |= IMASK_TX_DEFAULT; 2937 + gfar_write(&regs->imask, imask); 2938 + spin_unlock_irq(&gfargrp->grplock); 2939 + } 2940 + 2941 + return 0; 2942 + } 2943 + 2944 + /* GFAR error interrupt handler */ 2945 + static irqreturn_t gfar_error(int irq, void *grp_id) 2946 + { 2947 + struct gfar_priv_grp *gfargrp = grp_id; 2948 + struct gfar __iomem *regs = gfargrp->regs; 2949 + struct gfar_private *priv= gfargrp->priv; 2950 + struct net_device *dev = priv->ndev; 2951 + 2952 + /* Save ievent for future reference */ 2953 + u32 events = gfar_read(&regs->ievent); 2954 + 2955 + /* Clear IEVENT */ 2956 + gfar_write(&regs->ievent, events & IEVENT_ERR_MASK); 2957 + 2958 + /* Magic Packet is not an error. */ 2959 + if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) && 2960 + (events & IEVENT_MAG)) 2961 + events &= ~IEVENT_MAG; 2962 + 2963 + /* Hmm... */ 2964 + if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv)) 2965 + netdev_dbg(dev, 2966 + "error interrupt (ievent=0x%08x imask=0x%08x)\n", 2967 + events, gfar_read(&regs->imask)); 2968 + 2969 + /* Update the error counters */ 2970 + if (events & IEVENT_TXE) { 2971 + dev->stats.tx_errors++; 2972 + 2973 + if (events & IEVENT_LC) 2974 + dev->stats.tx_window_errors++; 2975 + if (events & IEVENT_CRL) 2976 + dev->stats.tx_aborted_errors++; 2977 + if (events & IEVENT_XFUN) { 2978 + netif_dbg(priv, tx_err, dev, 2979 + "TX FIFO underrun, packet dropped\n"); 2980 + dev->stats.tx_dropped++; 2981 + atomic64_inc(&priv->extra_stats.tx_underrun); 2982 + 2983 + schedule_work(&priv->reset_task); 2984 + } 2985 + netif_dbg(priv, tx_err, dev, "Transmit Error\n"); 2986 + } 2987 + if (events & IEVENT_BSY) { 2988 + dev->stats.rx_over_errors++; 2989 + atomic64_inc(&priv->extra_stats.rx_bsy); 2990 + 2991 + netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n", 2992 + gfar_read(&regs->rstat)); 2993 + } 2994 + if (events & IEVENT_BABR) { 2995 + dev->stats.rx_errors++; 2996 + atomic64_inc(&priv->extra_stats.rx_babr); 2997 + 2998 + netif_dbg(priv, rx_err, dev, "babbling RX error\n"); 2999 + } 3000 + if (events & IEVENT_EBERR) { 3001 + atomic64_inc(&priv->extra_stats.eberr); 3002 + netif_dbg(priv, rx_err, dev, "bus error\n"); 3003 + } 3004 + if (events & IEVENT_RXC) 3005 + netif_dbg(priv, rx_status, dev, "control frame\n"); 3006 + 3007 + if (events & IEVENT_BABT) { 3008 + atomic64_inc(&priv->extra_stats.tx_babt); 3009 + netif_dbg(priv, tx_err, dev, "babbling TX error\n"); 3010 + } 3011 + return IRQ_HANDLED; 3012 + } 3013 + 3014 + /* The interrupt handler for devices with one interrupt */ 3015 + static irqreturn_t gfar_interrupt(int irq, void *grp_id) 3016 + { 3017 + struct gfar_priv_grp *gfargrp = grp_id; 3018 + 3019 + /* Save ievent for future reference */ 3020 + u32 events = gfar_read(&gfargrp->regs->ievent); 3021 + 3022 + /* Check for reception */ 3023 + if (events & IEVENT_RX_MASK) 3024 + gfar_receive(irq, grp_id); 3025 + 3026 + /* Check for transmit completion */ 3027 + if (events & IEVENT_TX_MASK) 3028 + gfar_transmit(irq, grp_id); 3029 + 3030 + /* Check for errors */ 3031 + if (events & IEVENT_ERR_MASK) 3032 + gfar_error(irq, grp_id); 3033 + 3034 + return IRQ_HANDLED; 3035 + } 3036 + 3037 + #ifdef CONFIG_NET_POLL_CONTROLLER 3038 + /* Polling 'interrupt' - used by things like netconsole to send skbs 3039 + * without having to re-enable interrupts. It's not called while 3040 + * the interrupt routine is executing. 3041 + */ 3042 + static void gfar_netpoll(struct net_device *dev) 3043 + { 3044 + struct gfar_private *priv = netdev_priv(dev); 3045 + int i; 3046 + 3047 + /* If the device has multiple interrupts, run tx/rx */ 3048 + if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 3049 + for (i = 0; i < priv->num_grps; i++) { 3050 + struct gfar_priv_grp *grp = &priv->gfargrp[i]; 3051 + 3052 + disable_irq(gfar_irq(grp, TX)->irq); 3053 + disable_irq(gfar_irq(grp, RX)->irq); 3054 + disable_irq(gfar_irq(grp, ER)->irq); 3055 + gfar_interrupt(gfar_irq(grp, TX)->irq, grp); 3056 + enable_irq(gfar_irq(grp, ER)->irq); 3057 + enable_irq(gfar_irq(grp, RX)->irq); 3058 + enable_irq(gfar_irq(grp, TX)->irq); 3059 + } 3060 + } else { 3061 + for (i = 0; i < priv->num_grps; i++) { 3062 + struct gfar_priv_grp *grp = &priv->gfargrp[i]; 3063 + 3064 + disable_irq(gfar_irq(grp, TX)->irq); 3065 + gfar_interrupt(gfar_irq(grp, TX)->irq, grp); 3066 + enable_irq(gfar_irq(grp, TX)->irq); 3067 + } 3068 + } 3069 + } 3070 + #endif 3071 + 3072 + static void free_grp_irqs(struct gfar_priv_grp *grp) 3073 + { 3074 + free_irq(gfar_irq(grp, TX)->irq, grp); 3075 + free_irq(gfar_irq(grp, RX)->irq, grp); 3076 + free_irq(gfar_irq(grp, ER)->irq, grp); 3077 + } 3078 + 3079 + static int register_grp_irqs(struct gfar_priv_grp *grp) 3080 + { 3081 + struct gfar_private *priv = grp->priv; 3082 + struct net_device *dev = priv->ndev; 3083 + int err; 3084 + 3085 + /* If the device has multiple interrupts, register for 3086 + * them. Otherwise, only register for the one 3087 + */ 3088 + if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 3089 + /* Install our interrupt handlers for Error, 3090 + * Transmit, and Receive 3091 + */ 3092 + err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0, 3093 + gfar_irq(grp, ER)->name, grp); 3094 + if (err < 0) { 3095 + netif_err(priv, intr, dev, "Can't get IRQ %d\n", 3096 + gfar_irq(grp, ER)->irq); 3097 + 3098 + goto err_irq_fail; 3099 + } 3100 + enable_irq_wake(gfar_irq(grp, ER)->irq); 3101 + 3102 + err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0, 3103 + gfar_irq(grp, TX)->name, grp); 3104 + if (err < 0) { 3105 + netif_err(priv, intr, dev, "Can't get IRQ %d\n", 3106 + gfar_irq(grp, TX)->irq); 3107 + goto tx_irq_fail; 3108 + } 3109 + err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0, 3110 + gfar_irq(grp, RX)->name, grp); 3111 + if (err < 0) { 3112 + netif_err(priv, intr, dev, "Can't get IRQ %d\n", 3113 + gfar_irq(grp, RX)->irq); 3114 + goto rx_irq_fail; 3115 + } 3116 + enable_irq_wake(gfar_irq(grp, RX)->irq); 3117 + 3118 + } else { 3119 + err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0, 3120 + gfar_irq(grp, TX)->name, grp); 3121 + if (err < 0) { 3122 + netif_err(priv, intr, dev, "Can't get IRQ %d\n", 3123 + gfar_irq(grp, TX)->irq); 3124 + goto err_irq_fail; 3125 + } 3126 + enable_irq_wake(gfar_irq(grp, TX)->irq); 3127 + } 3128 + 3129 + return 0; 3130 + 3131 + rx_irq_fail: 3132 + free_irq(gfar_irq(grp, TX)->irq, grp); 3133 + tx_irq_fail: 3134 + free_irq(gfar_irq(grp, ER)->irq, grp); 3135 + err_irq_fail: 3136 + return err; 3137 + 3138 + } 3139 + 3140 + static void gfar_free_irq(struct gfar_private *priv) 3141 + { 3142 + int i; 3143 + 3144 + /* Free the IRQs */ 3145 + if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 3146 + for (i = 0; i < priv->num_grps; i++) 3147 + free_grp_irqs(&priv->gfargrp[i]); 3148 + } else { 3149 + for (i = 0; i < priv->num_grps; i++) 3150 + free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq, 3151 + &priv->gfargrp[i]); 3152 + } 3153 + } 3154 + 3155 + static int gfar_request_irq(struct gfar_private *priv) 3156 + { 3157 + int err, i, j; 3158 + 3159 + for (i = 0; i < priv->num_grps; i++) { 3160 + err = register_grp_irqs(&priv->gfargrp[i]); 3161 + if (err) { 3162 + for (j = 0; j < i; j++) 3163 + free_grp_irqs(&priv->gfargrp[j]); 3164 + return err; 3165 + } 3166 + } 3167 + 3168 + return 0; 3169 + } 3170 + 3171 + /* Called when something needs to use the ethernet device 3172 + * Returns 0 for success. 3173 + */ 3174 + static int gfar_enet_open(struct net_device *dev) 3175 + { 3176 + struct gfar_private *priv = netdev_priv(dev); 3177 + int err; 3178 + 3179 + err = init_phy(dev); 3180 + if (err) 3181 + return err; 3182 + 3183 + err = gfar_request_irq(priv); 3184 + if (err) 3185 + return err; 3186 + 3187 + err = startup_gfar(dev); 3188 + if (err) 3189 + return err; 3190 + 3191 + return err; 3192 + } 3193 + 3194 + /* Stops the kernel queue, and halts the controller */ 3195 + static int gfar_close(struct net_device *dev) 3196 + { 3197 + struct gfar_private *priv = netdev_priv(dev); 3198 + 3199 + cancel_work_sync(&priv->reset_task); 3200 + stop_gfar(dev); 3201 + 3202 + /* Disconnect from the PHY */ 3203 + phy_disconnect(dev->phydev); 3204 + 3205 + gfar_free_irq(priv); 3206 + 3207 + return 0; 3208 + } 3209 + 3210 + /* Clears each of the exact match registers to zero, so they 3211 + * don't interfere with normal reception 3212 + */ 3213 + static void gfar_clear_exact_match(struct net_device *dev) 3214 + { 3215 + int idx; 3216 + static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; 3217 + 3218 + for (idx = 1; idx < GFAR_EM_NUM + 1; idx++) 3219 + gfar_set_mac_for_addr(dev, idx, zero_arr); 3220 + } 3221 + 3222 + /* Update the hash table based on the current list of multicast 3223 + * addresses we subscribe to. Also, change the promiscuity of 3224 + * the device based on the flags (this function is called 3225 + * whenever dev->flags is changed 3226 + */ 3227 + static void gfar_set_multi(struct net_device *dev) 3228 + { 3229 + struct netdev_hw_addr *ha; 3230 + struct gfar_private *priv = netdev_priv(dev); 3231 + struct gfar __iomem *regs = priv->gfargrp[0].regs; 3232 + u32 tempval; 3233 + 3234 + if (dev->flags & IFF_PROMISC) { 3235 + /* Set RCTRL to PROM */ 3236 + tempval = gfar_read(&regs->rctrl); 3237 + tempval |= RCTRL_PROM; 3238 + gfar_write(&regs->rctrl, tempval); 3239 + } else { 3240 + /* Set RCTRL to not PROM */ 3241 + tempval = gfar_read(&regs->rctrl); 3242 + tempval &= ~(RCTRL_PROM); 3243 + gfar_write(&regs->rctrl, tempval); 3244 + } 3245 + 3246 + if (dev->flags & IFF_ALLMULTI) { 3247 + /* Set the hash to rx all multicast frames */ 3248 + gfar_write(&regs->igaddr0, 0xffffffff); 3249 + gfar_write(&regs->igaddr1, 0xffffffff); 3250 + gfar_write(&regs->igaddr2, 0xffffffff); 3251 + gfar_write(&regs->igaddr3, 0xffffffff); 3252 + gfar_write(&regs->igaddr4, 0xffffffff); 3253 + gfar_write(&regs->igaddr5, 0xffffffff); 3254 + gfar_write(&regs->igaddr6, 0xffffffff); 3255 + gfar_write(&regs->igaddr7, 0xffffffff); 3256 + gfar_write(&regs->gaddr0, 0xffffffff); 3257 + gfar_write(&regs->gaddr1, 0xffffffff); 3258 + gfar_write(&regs->gaddr2, 0xffffffff); 3259 + gfar_write(&regs->gaddr3, 0xffffffff); 3260 + gfar_write(&regs->gaddr4, 0xffffffff); 3261 + gfar_write(&regs->gaddr5, 0xffffffff); 3262 + gfar_write(&regs->gaddr6, 0xffffffff); 3263 + gfar_write(&regs->gaddr7, 0xffffffff); 3264 + } else { 3265 + int em_num; 3266 + int idx; 3267 + 3268 + /* zero out the hash */ 3269 + gfar_write(&regs->igaddr0, 0x0); 3270 + gfar_write(&regs->igaddr1, 0x0); 3271 + gfar_write(&regs->igaddr2, 0x0); 3272 + gfar_write(&regs->igaddr3, 0x0); 3273 + gfar_write(&regs->igaddr4, 0x0); 3274 + gfar_write(&regs->igaddr5, 0x0); 3275 + gfar_write(&regs->igaddr6, 0x0); 3276 + gfar_write(&regs->igaddr7, 0x0); 3277 + gfar_write(&regs->gaddr0, 0x0); 3278 + gfar_write(&regs->gaddr1, 0x0); 3279 + gfar_write(&regs->gaddr2, 0x0); 3280 + gfar_write(&regs->gaddr3, 0x0); 3281 + gfar_write(&regs->gaddr4, 0x0); 3282 + gfar_write(&regs->gaddr5, 0x0); 3283 + gfar_write(&regs->gaddr6, 0x0); 3284 + gfar_write(&regs->gaddr7, 0x0); 3285 + 3286 + /* If we have extended hash tables, we need to 3287 + * clear the exact match registers to prepare for 3288 + * setting them 3289 + */ 3290 + if (priv->extended_hash) { 3291 + em_num = GFAR_EM_NUM + 1; 3292 + gfar_clear_exact_match(dev); 3293 + idx = 1; 3294 + } else { 3295 + idx = 0; 3296 + em_num = 0; 3297 + } 3298 + 3299 + if (netdev_mc_empty(dev)) 3300 + return; 3301 + 3302 + /* Parse the list, and set the appropriate bits */ 3303 + netdev_for_each_mc_addr(ha, dev) { 3304 + if (idx < em_num) { 3305 + gfar_set_mac_for_addr(dev, idx, ha->addr); 3306 + idx++; 3307 + } else 3308 + gfar_set_hash_for_addr(dev, ha->addr); 3309 + } 3310 + } 969 3311 } 970 3312 971 3313 void gfar_mac_reset(struct gfar_private *priv) ··· 3274 1262 gfar_write_isrg(priv); 3275 1263 } 3276 1264 3277 - static void gfar_init_addr_hash_table(struct gfar_private *priv) 3278 - { 3279 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 3280 - 3281 - if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) { 3282 - priv->extended_hash = 1; 3283 - priv->hash_width = 9; 3284 - 3285 - priv->hash_regs[0] = &regs->igaddr0; 3286 - priv->hash_regs[1] = &regs->igaddr1; 3287 - priv->hash_regs[2] = &regs->igaddr2; 3288 - priv->hash_regs[3] = &regs->igaddr3; 3289 - priv->hash_regs[4] = &regs->igaddr4; 3290 - priv->hash_regs[5] = &regs->igaddr5; 3291 - priv->hash_regs[6] = &regs->igaddr6; 3292 - priv->hash_regs[7] = &regs->igaddr7; 3293 - priv->hash_regs[8] = &regs->gaddr0; 3294 - priv->hash_regs[9] = &regs->gaddr1; 3295 - priv->hash_regs[10] = &regs->gaddr2; 3296 - priv->hash_regs[11] = &regs->gaddr3; 3297 - priv->hash_regs[12] = &regs->gaddr4; 3298 - priv->hash_regs[13] = &regs->gaddr5; 3299 - priv->hash_regs[14] = &regs->gaddr6; 3300 - priv->hash_regs[15] = &regs->gaddr7; 3301 - 3302 - } else { 3303 - priv->extended_hash = 0; 3304 - priv->hash_width = 8; 3305 - 3306 - priv->hash_regs[0] = &regs->gaddr0; 3307 - priv->hash_regs[1] = &regs->gaddr1; 3308 - priv->hash_regs[2] = &regs->gaddr2; 3309 - priv->hash_regs[3] = &regs->gaddr3; 3310 - priv->hash_regs[4] = &regs->gaddr4; 3311 - priv->hash_regs[5] = &regs->gaddr5; 3312 - priv->hash_regs[6] = &regs->gaddr6; 3313 - priv->hash_regs[7] = &regs->gaddr7; 3314 - } 3315 - } 1265 + static const struct net_device_ops gfar_netdev_ops = { 1266 + .ndo_open = gfar_enet_open, 1267 + .ndo_start_xmit = gfar_start_xmit, 1268 + .ndo_stop = gfar_close, 1269 + .ndo_change_mtu = gfar_change_mtu, 1270 + .ndo_set_features = gfar_set_features, 1271 + .ndo_set_rx_mode = gfar_set_multi, 1272 + .ndo_tx_timeout = gfar_timeout, 1273 + .ndo_do_ioctl = gfar_ioctl, 1274 + .ndo_get_stats = gfar_get_stats, 1275 + .ndo_change_carrier = fixed_phy_change_carrier, 1276 + .ndo_set_mac_address = gfar_set_mac_addr, 1277 + .ndo_validate_addr = eth_validate_addr, 1278 + #ifdef CONFIG_NET_POLL_CONTROLLER 1279 + .ndo_poll_controller = gfar_netpoll, 1280 + #endif 1281 + }; 3316 1282 3317 1283 /* Set up the ethernet device structure, private data, 3318 1284 * and anything else we need before we start ··· 3720 1730 #define GFAR_PM_OPS NULL 3721 1731 3722 1732 #endif 3723 - 3724 - /* Reads the controller's registers to determine what interface 3725 - * connects it to the PHY. 3726 - */ 3727 - static phy_interface_t gfar_get_interface(struct net_device *dev) 3728 - { 3729 - struct gfar_private *priv = netdev_priv(dev); 3730 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 3731 - u32 ecntrl; 3732 - 3733 - ecntrl = gfar_read(&regs->ecntrl); 3734 - 3735 - if (ecntrl & ECNTRL_SGMII_MODE) 3736 - return PHY_INTERFACE_MODE_SGMII; 3737 - 3738 - if (ecntrl & ECNTRL_TBI_MODE) { 3739 - if (ecntrl & ECNTRL_REDUCED_MODE) 3740 - return PHY_INTERFACE_MODE_RTBI; 3741 - else 3742 - return PHY_INTERFACE_MODE_TBI; 3743 - } 3744 - 3745 - if (ecntrl & ECNTRL_REDUCED_MODE) { 3746 - if (ecntrl & ECNTRL_REDUCED_MII_MODE) { 3747 - return PHY_INTERFACE_MODE_RMII; 3748 - } 3749 - else { 3750 - phy_interface_t interface = priv->interface; 3751 - 3752 - /* This isn't autodetected right now, so it must 3753 - * be set by the device tree or platform code. 3754 - */ 3755 - if (interface == PHY_INTERFACE_MODE_RGMII_ID) 3756 - return PHY_INTERFACE_MODE_RGMII_ID; 3757 - 3758 - return PHY_INTERFACE_MODE_RGMII; 3759 - } 3760 - } 3761 - 3762 - if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT) 3763 - return PHY_INTERFACE_MODE_GMII; 3764 - 3765 - return PHY_INTERFACE_MODE_MII; 3766 - } 3767 - 3768 - 3769 - /* Initializes driver's PHY state, and attaches to the PHY. 3770 - * Returns 0 on success. 3771 - */ 3772 - static int init_phy(struct net_device *dev) 3773 - { 3774 - __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 3775 - struct gfar_private *priv = netdev_priv(dev); 3776 - phy_interface_t interface; 3777 - struct phy_device *phydev; 3778 - struct ethtool_eee edata; 3779 - 3780 - linkmode_set_bit_array(phy_10_100_features_array, 3781 - ARRAY_SIZE(phy_10_100_features_array), 3782 - mask); 3783 - linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask); 3784 - linkmode_set_bit(ETHTOOL_LINK_MODE_MII_BIT, mask); 3785 - if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT) 3786 - linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, mask); 3787 - 3788 - priv->oldlink = 0; 3789 - priv->oldspeed = 0; 3790 - priv->oldduplex = -1; 3791 - 3792 - interface = gfar_get_interface(dev); 3793 - 3794 - phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0, 3795 - interface); 3796 - if (!phydev) { 3797 - dev_err(&dev->dev, "could not attach to PHY\n"); 3798 - return -ENODEV; 3799 - } 3800 - 3801 - if (interface == PHY_INTERFACE_MODE_SGMII) 3802 - gfar_configure_serdes(dev); 3803 - 3804 - /* Remove any features not supported by the controller */ 3805 - linkmode_and(phydev->supported, phydev->supported, mask); 3806 - linkmode_copy(phydev->advertising, phydev->supported); 3807 - 3808 - /* Add support for flow control */ 3809 - phy_support_asym_pause(phydev); 3810 - 3811 - /* disable EEE autoneg, EEE not supported by eTSEC */ 3812 - memset(&edata, 0, sizeof(struct ethtool_eee)); 3813 - phy_ethtool_set_eee(phydev, &edata); 3814 - 3815 - return 0; 3816 - } 3817 - 3818 - /* Initialize TBI PHY interface for communicating with the 3819 - * SERDES lynx PHY on the chip. We communicate with this PHY 3820 - * through the MDIO bus on each controller, treating it as a 3821 - * "normal" PHY at the address found in the TBIPA register. We assume 3822 - * that the TBIPA register is valid. Either the MDIO bus code will set 3823 - * it to a value that doesn't conflict with other PHYs on the bus, or the 3824 - * value doesn't matter, as there are no other PHYs on the bus. 3825 - */ 3826 - static void gfar_configure_serdes(struct net_device *dev) 3827 - { 3828 - struct gfar_private *priv = netdev_priv(dev); 3829 - struct phy_device *tbiphy; 3830 - 3831 - if (!priv->tbi_node) { 3832 - dev_warn(&dev->dev, "error: SGMII mode requires that the " 3833 - "device tree specify a tbi-handle\n"); 3834 - return; 3835 - } 3836 - 3837 - tbiphy = of_phy_find_device(priv->tbi_node); 3838 - if (!tbiphy) { 3839 - dev_err(&dev->dev, "error: Could not get TBI device\n"); 3840 - return; 3841 - } 3842 - 3843 - /* If the link is already up, we must already be ok, and don't need to 3844 - * configure and reset the TBI<->SerDes link. Maybe U-Boot configured 3845 - * everything for us? Resetting it takes the link down and requires 3846 - * several seconds for it to come back. 3847 - */ 3848 - if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) { 3849 - put_device(&tbiphy->mdio.dev); 3850 - return; 3851 - } 3852 - 3853 - /* Single clk mode, mii mode off(for serdes communication) */ 3854 - phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT); 3855 - 3856 - phy_write(tbiphy, MII_ADVERTISE, 3857 - ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 3858 - ADVERTISE_1000XPSE_ASYM); 3859 - 3860 - phy_write(tbiphy, MII_BMCR, 3861 - BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX | 3862 - BMCR_SPEED1000); 3863 - 3864 - put_device(&tbiphy->mdio.dev); 3865 - } 3866 - 3867 - static int __gfar_is_rx_idle(struct gfar_private *priv) 3868 - { 3869 - u32 res; 3870 - 3871 - /* Normaly TSEC should not hang on GRS commands, so we should 3872 - * actually wait for IEVENT_GRSC flag. 3873 - */ 3874 - if (!gfar_has_errata(priv, GFAR_ERRATA_A002)) 3875 - return 0; 3876 - 3877 - /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are 3878 - * the same as bits 23-30, the eTSEC Rx is assumed to be idle 3879 - * and the Rx can be safely reset. 3880 - */ 3881 - res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c); 3882 - res &= 0x7f807f80; 3883 - if ((res & 0xffff) == (res >> 16)) 3884 - return 1; 3885 - 3886 - return 0; 3887 - } 3888 - 3889 - /* Halt the receive and transmit queues */ 3890 - static void gfar_halt_nodisable(struct gfar_private *priv) 3891 - { 3892 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 3893 - u32 tempval; 3894 - unsigned int timeout; 3895 - int stopped; 3896 - 3897 - gfar_ints_disable(priv); 3898 - 3899 - if (gfar_is_dma_stopped(priv)) 3900 - return; 3901 - 3902 - /* Stop the DMA, and wait for it to stop */ 3903 - tempval = gfar_read(&regs->dmactrl); 3904 - tempval |= (DMACTRL_GRS | DMACTRL_GTS); 3905 - gfar_write(&regs->dmactrl, tempval); 3906 - 3907 - retry: 3908 - timeout = 1000; 3909 - while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) { 3910 - cpu_relax(); 3911 - timeout--; 3912 - } 3913 - 3914 - if (!timeout) 3915 - stopped = gfar_is_dma_stopped(priv); 3916 - 3917 - if (!stopped && !gfar_is_rx_dma_stopped(priv) && 3918 - !__gfar_is_rx_idle(priv)) 3919 - goto retry; 3920 - } 3921 - 3922 - /* Halt the receive and transmit queues */ 3923 - void gfar_halt(struct gfar_private *priv) 3924 - { 3925 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 3926 - u32 tempval; 3927 - 3928 - /* Dissable the Rx/Tx hw queues */ 3929 - gfar_write(&regs->rqueue, 0); 3930 - gfar_write(&regs->tqueue, 0); 3931 - 3932 - mdelay(10); 3933 - 3934 - gfar_halt_nodisable(priv); 3935 - 3936 - /* Disable Rx/Tx DMA */ 3937 - tempval = gfar_read(&regs->maccfg1); 3938 - tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN); 3939 - gfar_write(&regs->maccfg1, tempval); 3940 - } 3941 - 3942 - void stop_gfar(struct net_device *dev) 3943 - { 3944 - struct gfar_private *priv = netdev_priv(dev); 3945 - 3946 - netif_tx_stop_all_queues(dev); 3947 - 3948 - smp_mb__before_atomic(); 3949 - set_bit(GFAR_DOWN, &priv->state); 3950 - smp_mb__after_atomic(); 3951 - 3952 - disable_napi(priv); 3953 - 3954 - /* disable ints and gracefully shut down Rx/Tx DMA */ 3955 - gfar_halt(priv); 3956 - 3957 - phy_stop(dev->phydev); 3958 - 3959 - free_skb_resources(priv); 3960 - } 3961 - 3962 - static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue) 3963 - { 3964 - struct txbd8 *txbdp; 3965 - struct gfar_private *priv = netdev_priv(tx_queue->dev); 3966 - int i, j; 3967 - 3968 - txbdp = tx_queue->tx_bd_base; 3969 - 3970 - for (i = 0; i < tx_queue->tx_ring_size; i++) { 3971 - if (!tx_queue->tx_skbuff[i]) 3972 - continue; 3973 - 3974 - dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr), 3975 - be16_to_cpu(txbdp->length), DMA_TO_DEVICE); 3976 - txbdp->lstatus = 0; 3977 - for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags; 3978 - j++) { 3979 - txbdp++; 3980 - dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr), 3981 - be16_to_cpu(txbdp->length), 3982 - DMA_TO_DEVICE); 3983 - } 3984 - txbdp++; 3985 - dev_kfree_skb_any(tx_queue->tx_skbuff[i]); 3986 - tx_queue->tx_skbuff[i] = NULL; 3987 - } 3988 - kfree(tx_queue->tx_skbuff); 3989 - tx_queue->tx_skbuff = NULL; 3990 - } 3991 - 3992 - static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue) 3993 - { 3994 - int i; 3995 - 3996 - struct rxbd8 *rxbdp = rx_queue->rx_bd_base; 3997 - 3998 - dev_kfree_skb(rx_queue->skb); 3999 - 4000 - for (i = 0; i < rx_queue->rx_ring_size; i++) { 4001 - struct gfar_rx_buff *rxb = &rx_queue->rx_buff[i]; 4002 - 4003 - rxbdp->lstatus = 0; 4004 - rxbdp->bufPtr = 0; 4005 - rxbdp++; 4006 - 4007 - if (!rxb->page) 4008 - continue; 4009 - 4010 - dma_unmap_page(rx_queue->dev, rxb->dma, 4011 - PAGE_SIZE, DMA_FROM_DEVICE); 4012 - __free_page(rxb->page); 4013 - 4014 - rxb->page = NULL; 4015 - } 4016 - 4017 - kfree(rx_queue->rx_buff); 4018 - rx_queue->rx_buff = NULL; 4019 - } 4020 - 4021 - /* If there are any tx skbs or rx skbs still around, free them. 4022 - * Then free tx_skbuff and rx_skbuff 4023 - */ 4024 - static void free_skb_resources(struct gfar_private *priv) 4025 - { 4026 - struct gfar_priv_tx_q *tx_queue = NULL; 4027 - struct gfar_priv_rx_q *rx_queue = NULL; 4028 - int i; 4029 - 4030 - /* Go through all the buffer descriptors and free their data buffers */ 4031 - for (i = 0; i < priv->num_tx_queues; i++) { 4032 - struct netdev_queue *txq; 4033 - 4034 - tx_queue = priv->tx_queue[i]; 4035 - txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex); 4036 - if (tx_queue->tx_skbuff) 4037 - free_skb_tx_queue(tx_queue); 4038 - netdev_tx_reset_queue(txq); 4039 - } 4040 - 4041 - for (i = 0; i < priv->num_rx_queues; i++) { 4042 - rx_queue = priv->rx_queue[i]; 4043 - if (rx_queue->rx_buff) 4044 - free_skb_rx_queue(rx_queue); 4045 - } 4046 - 4047 - dma_free_coherent(priv->dev, 4048 - sizeof(struct txbd8) * priv->total_tx_ring_size + 4049 - sizeof(struct rxbd8) * priv->total_rx_ring_size, 4050 - priv->tx_queue[0]->tx_bd_base, 4051 - priv->tx_queue[0]->tx_bd_dma_base); 4052 - } 4053 - 4054 - void gfar_start(struct gfar_private *priv) 4055 - { 4056 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 4057 - u32 tempval; 4058 - int i = 0; 4059 - 4060 - /* Enable Rx/Tx hw queues */ 4061 - gfar_write(&regs->rqueue, priv->rqueue); 4062 - gfar_write(&regs->tqueue, priv->tqueue); 4063 - 4064 - /* Initialize DMACTRL to have WWR and WOP */ 4065 - tempval = gfar_read(&regs->dmactrl); 4066 - tempval |= DMACTRL_INIT_SETTINGS; 4067 - gfar_write(&regs->dmactrl, tempval); 4068 - 4069 - /* Make sure we aren't stopped */ 4070 - tempval = gfar_read(&regs->dmactrl); 4071 - tempval &= ~(DMACTRL_GRS | DMACTRL_GTS); 4072 - gfar_write(&regs->dmactrl, tempval); 4073 - 4074 - for (i = 0; i < priv->num_grps; i++) { 4075 - regs = priv->gfargrp[i].regs; 4076 - /* Clear THLT/RHLT, so that the DMA starts polling now */ 4077 - gfar_write(&regs->tstat, priv->gfargrp[i].tstat); 4078 - gfar_write(&regs->rstat, priv->gfargrp[i].rstat); 4079 - } 4080 - 4081 - /* Enable Rx/Tx DMA */ 4082 - tempval = gfar_read(&regs->maccfg1); 4083 - tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN); 4084 - gfar_write(&regs->maccfg1, tempval); 4085 - 4086 - gfar_ints_enable(priv); 4087 - 4088 - netif_trans_update(priv->ndev); /* prevent tx timeout */ 4089 - } 4090 - 4091 - static void free_grp_irqs(struct gfar_priv_grp *grp) 4092 - { 4093 - free_irq(gfar_irq(grp, TX)->irq, grp); 4094 - free_irq(gfar_irq(grp, RX)->irq, grp); 4095 - free_irq(gfar_irq(grp, ER)->irq, grp); 4096 - } 4097 - 4098 - static int register_grp_irqs(struct gfar_priv_grp *grp) 4099 - { 4100 - struct gfar_private *priv = grp->priv; 4101 - struct net_device *dev = priv->ndev; 4102 - int err; 4103 - 4104 - /* If the device has multiple interrupts, register for 4105 - * them. Otherwise, only register for the one 4106 - */ 4107 - if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 4108 - /* Install our interrupt handlers for Error, 4109 - * Transmit, and Receive 4110 - */ 4111 - err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0, 4112 - gfar_irq(grp, ER)->name, grp); 4113 - if (err < 0) { 4114 - netif_err(priv, intr, dev, "Can't get IRQ %d\n", 4115 - gfar_irq(grp, ER)->irq); 4116 - 4117 - goto err_irq_fail; 4118 - } 4119 - enable_irq_wake(gfar_irq(grp, ER)->irq); 4120 - 4121 - err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0, 4122 - gfar_irq(grp, TX)->name, grp); 4123 - if (err < 0) { 4124 - netif_err(priv, intr, dev, "Can't get IRQ %d\n", 4125 - gfar_irq(grp, TX)->irq); 4126 - goto tx_irq_fail; 4127 - } 4128 - err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0, 4129 - gfar_irq(grp, RX)->name, grp); 4130 - if (err < 0) { 4131 - netif_err(priv, intr, dev, "Can't get IRQ %d\n", 4132 - gfar_irq(grp, RX)->irq); 4133 - goto rx_irq_fail; 4134 - } 4135 - enable_irq_wake(gfar_irq(grp, RX)->irq); 4136 - 4137 - } else { 4138 - err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0, 4139 - gfar_irq(grp, TX)->name, grp); 4140 - if (err < 0) { 4141 - netif_err(priv, intr, dev, "Can't get IRQ %d\n", 4142 - gfar_irq(grp, TX)->irq); 4143 - goto err_irq_fail; 4144 - } 4145 - enable_irq_wake(gfar_irq(grp, TX)->irq); 4146 - } 4147 - 4148 - return 0; 4149 - 4150 - rx_irq_fail: 4151 - free_irq(gfar_irq(grp, TX)->irq, grp); 4152 - tx_irq_fail: 4153 - free_irq(gfar_irq(grp, ER)->irq, grp); 4154 - err_irq_fail: 4155 - return err; 4156 - 4157 - } 4158 - 4159 - static void gfar_free_irq(struct gfar_private *priv) 4160 - { 4161 - int i; 4162 - 4163 - /* Free the IRQs */ 4164 - if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 4165 - for (i = 0; i < priv->num_grps; i++) 4166 - free_grp_irqs(&priv->gfargrp[i]); 4167 - } else { 4168 - for (i = 0; i < priv->num_grps; i++) 4169 - free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq, 4170 - &priv->gfargrp[i]); 4171 - } 4172 - } 4173 - 4174 - static int gfar_request_irq(struct gfar_private *priv) 4175 - { 4176 - int err, i, j; 4177 - 4178 - for (i = 0; i < priv->num_grps; i++) { 4179 - err = register_grp_irqs(&priv->gfargrp[i]); 4180 - if (err) { 4181 - for (j = 0; j < i; j++) 4182 - free_grp_irqs(&priv->gfargrp[j]); 4183 - return err; 4184 - } 4185 - } 4186 - 4187 - return 0; 4188 - } 4189 - 4190 - /* Bring the controller up and running */ 4191 - int startup_gfar(struct net_device *ndev) 4192 - { 4193 - struct gfar_private *priv = netdev_priv(ndev); 4194 - int err; 4195 - 4196 - gfar_mac_reset(priv); 4197 - 4198 - err = gfar_alloc_skb_resources(ndev); 4199 - if (err) 4200 - return err; 4201 - 4202 - gfar_init_tx_rx_base(priv); 4203 - 4204 - smp_mb__before_atomic(); 4205 - clear_bit(GFAR_DOWN, &priv->state); 4206 - smp_mb__after_atomic(); 4207 - 4208 - /* Start Rx/Tx DMA and enable the interrupts */ 4209 - gfar_start(priv); 4210 - 4211 - /* force link state update after mac reset */ 4212 - priv->oldlink = 0; 4213 - priv->oldspeed = 0; 4214 - priv->oldduplex = -1; 4215 - 4216 - phy_start(ndev->phydev); 4217 - 4218 - enable_napi(priv); 4219 - 4220 - netif_tx_wake_all_queues(ndev); 4221 - 4222 - return 0; 4223 - } 4224 - 4225 - /* Called when something needs to use the ethernet device 4226 - * Returns 0 for success. 4227 - */ 4228 - static int gfar_enet_open(struct net_device *dev) 4229 - { 4230 - struct gfar_private *priv = netdev_priv(dev); 4231 - int err; 4232 - 4233 - err = init_phy(dev); 4234 - if (err) 4235 - return err; 4236 - 4237 - err = gfar_request_irq(priv); 4238 - if (err) 4239 - return err; 4240 - 4241 - err = startup_gfar(dev); 4242 - if (err) 4243 - return err; 4244 - 4245 - return err; 4246 - } 4247 - 4248 - static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb) 4249 - { 4250 - struct txfcb *fcb = skb_push(skb, GMAC_FCB_LEN); 4251 - 4252 - memset(fcb, 0, GMAC_FCB_LEN); 4253 - 4254 - return fcb; 4255 - } 4256 - 4257 - static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb, 4258 - int fcb_length) 4259 - { 4260 - /* If we're here, it's a IP packet with a TCP or UDP 4261 - * payload. We set it to checksum, using a pseudo-header 4262 - * we provide 4263 - */ 4264 - u8 flags = TXFCB_DEFAULT; 4265 - 4266 - /* Tell the controller what the protocol is 4267 - * And provide the already calculated phcs 4268 - */ 4269 - if (ip_hdr(skb)->protocol == IPPROTO_UDP) { 4270 - flags |= TXFCB_UDP; 4271 - fcb->phcs = (__force __be16)(udp_hdr(skb)->check); 4272 - } else 4273 - fcb->phcs = (__force __be16)(tcp_hdr(skb)->check); 4274 - 4275 - /* l3os is the distance between the start of the 4276 - * frame (skb->data) and the start of the IP hdr. 4277 - * l4os is the distance between the start of the 4278 - * l3 hdr and the l4 hdr 4279 - */ 4280 - fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length); 4281 - fcb->l4os = skb_network_header_len(skb); 4282 - 4283 - fcb->flags = flags; 4284 - } 4285 - 4286 - static inline void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb) 4287 - { 4288 - fcb->flags |= TXFCB_VLN; 4289 - fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb)); 4290 - } 4291 - 4292 - static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride, 4293 - struct txbd8 *base, int ring_size) 4294 - { 4295 - struct txbd8 *new_bd = bdp + stride; 4296 - 4297 - return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd; 4298 - } 4299 - 4300 - static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base, 4301 - int ring_size) 4302 - { 4303 - return skip_txbd(bdp, 1, base, ring_size); 4304 - } 4305 - 4306 - /* eTSEC12: csum generation not supported for some fcb offsets */ 4307 - static inline bool gfar_csum_errata_12(struct gfar_private *priv, 4308 - unsigned long fcb_addr) 4309 - { 4310 - return (gfar_has_errata(priv, GFAR_ERRATA_12) && 4311 - (fcb_addr % 0x20) > 0x18); 4312 - } 4313 - 4314 - /* eTSEC76: csum generation for frames larger than 2500 may 4315 - * cause excess delays before start of transmission 4316 - */ 4317 - static inline bool gfar_csum_errata_76(struct gfar_private *priv, 4318 - unsigned int len) 4319 - { 4320 - return (gfar_has_errata(priv, GFAR_ERRATA_76) && 4321 - (len > 2500)); 4322 - } 4323 - 4324 - /* This is called by the kernel when a frame is ready for transmission. 4325 - * It is pointed to by the dev->hard_start_xmit function pointer 4326 - */ 4327 - static netdev_tx_t gfar_start_xmit(struct sk_buff *skb, struct net_device *dev) 4328 - { 4329 - struct gfar_private *priv = netdev_priv(dev); 4330 - struct gfar_priv_tx_q *tx_queue = NULL; 4331 - struct netdev_queue *txq; 4332 - struct gfar __iomem *regs = NULL; 4333 - struct txfcb *fcb = NULL; 4334 - struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL; 4335 - u32 lstatus; 4336 - skb_frag_t *frag; 4337 - int i, rq = 0; 4338 - int do_tstamp, do_csum, do_vlan; 4339 - u32 bufaddr; 4340 - unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0; 4341 - 4342 - rq = skb->queue_mapping; 4343 - tx_queue = priv->tx_queue[rq]; 4344 - txq = netdev_get_tx_queue(dev, rq); 4345 - base = tx_queue->tx_bd_base; 4346 - regs = tx_queue->grp->regs; 4347 - 4348 - do_csum = (CHECKSUM_PARTIAL == skb->ip_summed); 4349 - do_vlan = skb_vlan_tag_present(skb); 4350 - do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 4351 - priv->hwts_tx_en; 4352 - 4353 - if (do_csum || do_vlan) 4354 - fcb_len = GMAC_FCB_LEN; 4355 - 4356 - /* check if time stamp should be generated */ 4357 - if (unlikely(do_tstamp)) 4358 - fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN; 4359 - 4360 - /* make space for additional header when fcb is needed */ 4361 - if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) { 4362 - struct sk_buff *skb_new; 4363 - 4364 - skb_new = skb_realloc_headroom(skb, fcb_len); 4365 - if (!skb_new) { 4366 - dev->stats.tx_errors++; 4367 - dev_kfree_skb_any(skb); 4368 - return NETDEV_TX_OK; 4369 - } 4370 - 4371 - if (skb->sk) 4372 - skb_set_owner_w(skb_new, skb->sk); 4373 - dev_consume_skb_any(skb); 4374 - skb = skb_new; 4375 - } 4376 - 4377 - /* total number of fragments in the SKB */ 4378 - nr_frags = skb_shinfo(skb)->nr_frags; 4379 - 4380 - /* calculate the required number of TxBDs for this skb */ 4381 - if (unlikely(do_tstamp)) 4382 - nr_txbds = nr_frags + 2; 4383 - else 4384 - nr_txbds = nr_frags + 1; 4385 - 4386 - /* check if there is space to queue this packet */ 4387 - if (nr_txbds > tx_queue->num_txbdfree) { 4388 - /* no space, stop the queue */ 4389 - netif_tx_stop_queue(txq); 4390 - dev->stats.tx_fifo_errors++; 4391 - return NETDEV_TX_BUSY; 4392 - } 4393 - 4394 - /* Update transmit stats */ 4395 - bytes_sent = skb->len; 4396 - tx_queue->stats.tx_bytes += bytes_sent; 4397 - /* keep Tx bytes on wire for BQL accounting */ 4398 - GFAR_CB(skb)->bytes_sent = bytes_sent; 4399 - tx_queue->stats.tx_packets++; 4400 - 4401 - txbdp = txbdp_start = tx_queue->cur_tx; 4402 - lstatus = be32_to_cpu(txbdp->lstatus); 4403 - 4404 - /* Add TxPAL between FCB and frame if required */ 4405 - if (unlikely(do_tstamp)) { 4406 - skb_push(skb, GMAC_TXPAL_LEN); 4407 - memset(skb->data, 0, GMAC_TXPAL_LEN); 4408 - } 4409 - 4410 - /* Add TxFCB if required */ 4411 - if (fcb_len) { 4412 - fcb = gfar_add_fcb(skb); 4413 - lstatus |= BD_LFLAG(TXBD_TOE); 4414 - } 4415 - 4416 - /* Set up checksumming */ 4417 - if (do_csum) { 4418 - gfar_tx_checksum(skb, fcb, fcb_len); 4419 - 4420 - if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) || 4421 - unlikely(gfar_csum_errata_76(priv, skb->len))) { 4422 - __skb_pull(skb, GMAC_FCB_LEN); 4423 - skb_checksum_help(skb); 4424 - if (do_vlan || do_tstamp) { 4425 - /* put back a new fcb for vlan/tstamp TOE */ 4426 - fcb = gfar_add_fcb(skb); 4427 - } else { 4428 - /* Tx TOE not used */ 4429 - lstatus &= ~(BD_LFLAG(TXBD_TOE)); 4430 - fcb = NULL; 4431 - } 4432 - } 4433 - } 4434 - 4435 - if (do_vlan) 4436 - gfar_tx_vlan(skb, fcb); 4437 - 4438 - bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb), 4439 - DMA_TO_DEVICE); 4440 - if (unlikely(dma_mapping_error(priv->dev, bufaddr))) 4441 - goto dma_map_err; 4442 - 4443 - txbdp_start->bufPtr = cpu_to_be32(bufaddr); 4444 - 4445 - /* Time stamp insertion requires one additional TxBD */ 4446 - if (unlikely(do_tstamp)) 4447 - txbdp_tstamp = txbdp = next_txbd(txbdp, base, 4448 - tx_queue->tx_ring_size); 4449 - 4450 - if (likely(!nr_frags)) { 4451 - if (likely(!do_tstamp)) 4452 - lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 4453 - } else { 4454 - u32 lstatus_start = lstatus; 4455 - 4456 - /* Place the fragment addresses and lengths into the TxBDs */ 4457 - frag = &skb_shinfo(skb)->frags[0]; 4458 - for (i = 0; i < nr_frags; i++, frag++) { 4459 - unsigned int size; 4460 - 4461 - /* Point at the next BD, wrapping as needed */ 4462 - txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 4463 - 4464 - size = skb_frag_size(frag); 4465 - 4466 - lstatus = be32_to_cpu(txbdp->lstatus) | size | 4467 - BD_LFLAG(TXBD_READY); 4468 - 4469 - /* Handle the last BD specially */ 4470 - if (i == nr_frags - 1) 4471 - lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 4472 - 4473 - bufaddr = skb_frag_dma_map(priv->dev, frag, 0, 4474 - size, DMA_TO_DEVICE); 4475 - if (unlikely(dma_mapping_error(priv->dev, bufaddr))) 4476 - goto dma_map_err; 4477 - 4478 - /* set the TxBD length and buffer pointer */ 4479 - txbdp->bufPtr = cpu_to_be32(bufaddr); 4480 - txbdp->lstatus = cpu_to_be32(lstatus); 4481 - } 4482 - 4483 - lstatus = lstatus_start; 4484 - } 4485 - 4486 - /* If time stamping is requested one additional TxBD must be set up. The 4487 - * first TxBD points to the FCB and must have a data length of 4488 - * GMAC_FCB_LEN. The second TxBD points to the actual frame data with 4489 - * the full frame length. 4490 - */ 4491 - if (unlikely(do_tstamp)) { 4492 - u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus); 4493 - 4494 - bufaddr = be32_to_cpu(txbdp_start->bufPtr); 4495 - bufaddr += fcb_len; 4496 - 4497 - lstatus_ts |= BD_LFLAG(TXBD_READY) | 4498 - (skb_headlen(skb) - fcb_len); 4499 - if (!nr_frags) 4500 - lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT); 4501 - 4502 - txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr); 4503 - txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts); 4504 - lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN; 4505 - 4506 - /* Setup tx hardware time stamping */ 4507 - skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 4508 - fcb->ptp = 1; 4509 - } else { 4510 - lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb); 4511 - } 4512 - 4513 - netdev_tx_sent_queue(txq, bytes_sent); 4514 - 4515 - gfar_wmb(); 4516 - 4517 - txbdp_start->lstatus = cpu_to_be32(lstatus); 4518 - 4519 - gfar_wmb(); /* force lstatus write before tx_skbuff */ 4520 - 4521 - tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb; 4522 - 4523 - /* Update the current skb pointer to the next entry we will use 4524 - * (wrapping if necessary) 4525 - */ 4526 - tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) & 4527 - TX_RING_MOD_MASK(tx_queue->tx_ring_size); 4528 - 4529 - tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size); 4530 - 4531 - /* We can work in parallel with gfar_clean_tx_ring(), except 4532 - * when modifying num_txbdfree. Note that we didn't grab the lock 4533 - * when we were reading the num_txbdfree and checking for available 4534 - * space, that's because outside of this function it can only grow. 4535 - */ 4536 - spin_lock_bh(&tx_queue->txlock); 4537 - /* reduce TxBD free count */ 4538 - tx_queue->num_txbdfree -= (nr_txbds); 4539 - spin_unlock_bh(&tx_queue->txlock); 4540 - 4541 - /* If the next BD still needs to be cleaned up, then the bds 4542 - * are full. We need to tell the kernel to stop sending us stuff. 4543 - */ 4544 - if (!tx_queue->num_txbdfree) { 4545 - netif_tx_stop_queue(txq); 4546 - 4547 - dev->stats.tx_fifo_errors++; 4548 - } 4549 - 4550 - /* Tell the DMA to go go go */ 4551 - gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex); 4552 - 4553 - return NETDEV_TX_OK; 4554 - 4555 - dma_map_err: 4556 - txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size); 4557 - if (do_tstamp) 4558 - txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 4559 - for (i = 0; i < nr_frags; i++) { 4560 - lstatus = be32_to_cpu(txbdp->lstatus); 4561 - if (!(lstatus & BD_LFLAG(TXBD_READY))) 4562 - break; 4563 - 4564 - lstatus &= ~BD_LFLAG(TXBD_READY); 4565 - txbdp->lstatus = cpu_to_be32(lstatus); 4566 - bufaddr = be32_to_cpu(txbdp->bufPtr); 4567 - dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length), 4568 - DMA_TO_DEVICE); 4569 - txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size); 4570 - } 4571 - gfar_wmb(); 4572 - dev_kfree_skb_any(skb); 4573 - return NETDEV_TX_OK; 4574 - } 4575 - 4576 - /* Stops the kernel queue, and halts the controller */ 4577 - static int gfar_close(struct net_device *dev) 4578 - { 4579 - struct gfar_private *priv = netdev_priv(dev); 4580 - 4581 - cancel_work_sync(&priv->reset_task); 4582 - stop_gfar(dev); 4583 - 4584 - /* Disconnect from the PHY */ 4585 - phy_disconnect(dev->phydev); 4586 - 4587 - gfar_free_irq(priv); 4588 - 4589 - return 0; 4590 - } 4591 - 4592 - /* Changes the mac address if the controller is not running. */ 4593 - static int gfar_set_mac_address(struct net_device *dev) 4594 - { 4595 - gfar_set_mac_for_addr(dev, 0, dev->dev_addr); 4596 - 4597 - return 0; 4598 - } 4599 - 4600 - static int gfar_change_mtu(struct net_device *dev, int new_mtu) 4601 - { 4602 - struct gfar_private *priv = netdev_priv(dev); 4603 - 4604 - while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state)) 4605 - cpu_relax(); 4606 - 4607 - if (dev->flags & IFF_UP) 4608 - stop_gfar(dev); 4609 - 4610 - dev->mtu = new_mtu; 4611 - 4612 - if (dev->flags & IFF_UP) 4613 - startup_gfar(dev); 4614 - 4615 - clear_bit_unlock(GFAR_RESETTING, &priv->state); 4616 - 4617 - return 0; 4618 - } 4619 - 4620 - void reset_gfar(struct net_device *ndev) 4621 - { 4622 - struct gfar_private *priv = netdev_priv(ndev); 4623 - 4624 - while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state)) 4625 - cpu_relax(); 4626 - 4627 - stop_gfar(ndev); 4628 - startup_gfar(ndev); 4629 - 4630 - clear_bit_unlock(GFAR_RESETTING, &priv->state); 4631 - } 4632 - 4633 - /* gfar_reset_task gets scheduled when a packet has not been 4634 - * transmitted after a set amount of time. 4635 - * For now, assume that clearing out all the structures, and 4636 - * starting over will fix the problem. 4637 - */ 4638 - static void gfar_reset_task(struct work_struct *work) 4639 - { 4640 - struct gfar_private *priv = container_of(work, struct gfar_private, 4641 - reset_task); 4642 - reset_gfar(priv->ndev); 4643 - } 4644 - 4645 - static void gfar_timeout(struct net_device *dev) 4646 - { 4647 - struct gfar_private *priv = netdev_priv(dev); 4648 - 4649 - dev->stats.tx_errors++; 4650 - schedule_work(&priv->reset_task); 4651 - } 4652 - 4653 - /* Interrupt Handler for Transmit complete */ 4654 - static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue) 4655 - { 4656 - struct net_device *dev = tx_queue->dev; 4657 - struct netdev_queue *txq; 4658 - struct gfar_private *priv = netdev_priv(dev); 4659 - struct txbd8 *bdp, *next = NULL; 4660 - struct txbd8 *lbdp = NULL; 4661 - struct txbd8 *base = tx_queue->tx_bd_base; 4662 - struct sk_buff *skb; 4663 - int skb_dirtytx; 4664 - int tx_ring_size = tx_queue->tx_ring_size; 4665 - int frags = 0, nr_txbds = 0; 4666 - int i; 4667 - int howmany = 0; 4668 - int tqi = tx_queue->qindex; 4669 - unsigned int bytes_sent = 0; 4670 - u32 lstatus; 4671 - size_t buflen; 4672 - 4673 - txq = netdev_get_tx_queue(dev, tqi); 4674 - bdp = tx_queue->dirty_tx; 4675 - skb_dirtytx = tx_queue->skb_dirtytx; 4676 - 4677 - while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) { 4678 - 4679 - frags = skb_shinfo(skb)->nr_frags; 4680 - 4681 - /* When time stamping, one additional TxBD must be freed. 4682 - * Also, we need to dma_unmap_single() the TxPAL. 4683 - */ 4684 - if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) 4685 - nr_txbds = frags + 2; 4686 - else 4687 - nr_txbds = frags + 1; 4688 - 4689 - lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size); 4690 - 4691 - lstatus = be32_to_cpu(lbdp->lstatus); 4692 - 4693 - /* Only clean completed frames */ 4694 - if ((lstatus & BD_LFLAG(TXBD_READY)) && 4695 - (lstatus & BD_LENGTH_MASK)) 4696 - break; 4697 - 4698 - if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) { 4699 - next = next_txbd(bdp, base, tx_ring_size); 4700 - buflen = be16_to_cpu(next->length) + 4701 - GMAC_FCB_LEN + GMAC_TXPAL_LEN; 4702 - } else 4703 - buflen = be16_to_cpu(bdp->length); 4704 - 4705 - dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr), 4706 - buflen, DMA_TO_DEVICE); 4707 - 4708 - if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) { 4709 - struct skb_shared_hwtstamps shhwtstamps; 4710 - u64 *ns = (u64 *)(((uintptr_t)skb->data + 0x10) & 4711 - ~0x7UL); 4712 - 4713 - memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 4714 - shhwtstamps.hwtstamp = ns_to_ktime(be64_to_cpu(*ns)); 4715 - skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN); 4716 - skb_tstamp_tx(skb, &shhwtstamps); 4717 - gfar_clear_txbd_status(bdp); 4718 - bdp = next; 4719 - } 4720 - 4721 - gfar_clear_txbd_status(bdp); 4722 - bdp = next_txbd(bdp, base, tx_ring_size); 4723 - 4724 - for (i = 0; i < frags; i++) { 4725 - dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr), 4726 - be16_to_cpu(bdp->length), 4727 - DMA_TO_DEVICE); 4728 - gfar_clear_txbd_status(bdp); 4729 - bdp = next_txbd(bdp, base, tx_ring_size); 4730 - } 4731 - 4732 - bytes_sent += GFAR_CB(skb)->bytes_sent; 4733 - 4734 - dev_kfree_skb_any(skb); 4735 - 4736 - tx_queue->tx_skbuff[skb_dirtytx] = NULL; 4737 - 4738 - skb_dirtytx = (skb_dirtytx + 1) & 4739 - TX_RING_MOD_MASK(tx_ring_size); 4740 - 4741 - howmany++; 4742 - spin_lock(&tx_queue->txlock); 4743 - tx_queue->num_txbdfree += nr_txbds; 4744 - spin_unlock(&tx_queue->txlock); 4745 - } 4746 - 4747 - /* If we freed a buffer, we can restart transmission, if necessary */ 4748 - if (tx_queue->num_txbdfree && 4749 - netif_tx_queue_stopped(txq) && 4750 - !(test_bit(GFAR_DOWN, &priv->state))) 4751 - netif_wake_subqueue(priv->ndev, tqi); 4752 - 4753 - /* Update dirty indicators */ 4754 - tx_queue->skb_dirtytx = skb_dirtytx; 4755 - tx_queue->dirty_tx = bdp; 4756 - 4757 - netdev_tx_completed_queue(txq, howmany, bytes_sent); 4758 - } 4759 - 4760 - static bool gfar_new_page(struct gfar_priv_rx_q *rxq, struct gfar_rx_buff *rxb) 4761 - { 4762 - struct page *page; 4763 - dma_addr_t addr; 4764 - 4765 - page = dev_alloc_page(); 4766 - if (unlikely(!page)) 4767 - return false; 4768 - 4769 - addr = dma_map_page(rxq->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE); 4770 - if (unlikely(dma_mapping_error(rxq->dev, addr))) { 4771 - __free_page(page); 4772 - 4773 - return false; 4774 - } 4775 - 4776 - rxb->dma = addr; 4777 - rxb->page = page; 4778 - rxb->page_offset = 0; 4779 - 4780 - return true; 4781 - } 4782 - 4783 - static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue) 4784 - { 4785 - struct gfar_private *priv = netdev_priv(rx_queue->ndev); 4786 - struct gfar_extra_stats *estats = &priv->extra_stats; 4787 - 4788 - netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n"); 4789 - atomic64_inc(&estats->rx_alloc_err); 4790 - } 4791 - 4792 - static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue, 4793 - int alloc_cnt) 4794 - { 4795 - struct rxbd8 *bdp; 4796 - struct gfar_rx_buff *rxb; 4797 - int i; 4798 - 4799 - i = rx_queue->next_to_use; 4800 - bdp = &rx_queue->rx_bd_base[i]; 4801 - rxb = &rx_queue->rx_buff[i]; 4802 - 4803 - while (alloc_cnt--) { 4804 - /* try reuse page */ 4805 - if (unlikely(!rxb->page)) { 4806 - if (unlikely(!gfar_new_page(rx_queue, rxb))) { 4807 - gfar_rx_alloc_err(rx_queue); 4808 - break; 4809 - } 4810 - } 4811 - 4812 - /* Setup the new RxBD */ 4813 - gfar_init_rxbdp(rx_queue, bdp, 4814 - rxb->dma + rxb->page_offset + RXBUF_ALIGNMENT); 4815 - 4816 - /* Update to the next pointer */ 4817 - bdp++; 4818 - rxb++; 4819 - 4820 - if (unlikely(++i == rx_queue->rx_ring_size)) { 4821 - i = 0; 4822 - bdp = rx_queue->rx_bd_base; 4823 - rxb = rx_queue->rx_buff; 4824 - } 4825 - } 4826 - 4827 - rx_queue->next_to_use = i; 4828 - rx_queue->next_to_alloc = i; 4829 - } 4830 - 4831 - static void count_errors(u32 lstatus, struct net_device *ndev) 4832 - { 4833 - struct gfar_private *priv = netdev_priv(ndev); 4834 - struct net_device_stats *stats = &ndev->stats; 4835 - struct gfar_extra_stats *estats = &priv->extra_stats; 4836 - 4837 - /* If the packet was truncated, none of the other errors matter */ 4838 - if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) { 4839 - stats->rx_length_errors++; 4840 - 4841 - atomic64_inc(&estats->rx_trunc); 4842 - 4843 - return; 4844 - } 4845 - /* Count the errors, if there were any */ 4846 - if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) { 4847 - stats->rx_length_errors++; 4848 - 4849 - if (lstatus & BD_LFLAG(RXBD_LARGE)) 4850 - atomic64_inc(&estats->rx_large); 4851 - else 4852 - atomic64_inc(&estats->rx_short); 4853 - } 4854 - if (lstatus & BD_LFLAG(RXBD_NONOCTET)) { 4855 - stats->rx_frame_errors++; 4856 - atomic64_inc(&estats->rx_nonoctet); 4857 - } 4858 - if (lstatus & BD_LFLAG(RXBD_CRCERR)) { 4859 - atomic64_inc(&estats->rx_crcerr); 4860 - stats->rx_crc_errors++; 4861 - } 4862 - if (lstatus & BD_LFLAG(RXBD_OVERRUN)) { 4863 - atomic64_inc(&estats->rx_overrun); 4864 - stats->rx_over_errors++; 4865 - } 4866 - } 4867 - 4868 - irqreturn_t gfar_receive(int irq, void *grp_id) 4869 - { 4870 - struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id; 4871 - unsigned long flags; 4872 - u32 imask, ievent; 4873 - 4874 - ievent = gfar_read(&grp->regs->ievent); 4875 - 4876 - if (unlikely(ievent & IEVENT_FGPI)) { 4877 - gfar_write(&grp->regs->ievent, IEVENT_FGPI); 4878 - return IRQ_HANDLED; 4879 - } 4880 - 4881 - if (likely(napi_schedule_prep(&grp->napi_rx))) { 4882 - spin_lock_irqsave(&grp->grplock, flags); 4883 - imask = gfar_read(&grp->regs->imask); 4884 - imask &= IMASK_RX_DISABLED; 4885 - gfar_write(&grp->regs->imask, imask); 4886 - spin_unlock_irqrestore(&grp->grplock, flags); 4887 - __napi_schedule(&grp->napi_rx); 4888 - } else { 4889 - /* Clear IEVENT, so interrupts aren't called again 4890 - * because of the packets that have already arrived. 4891 - */ 4892 - gfar_write(&grp->regs->ievent, IEVENT_RX_MASK); 4893 - } 4894 - 4895 - return IRQ_HANDLED; 4896 - } 4897 - 4898 - /* Interrupt Handler for Transmit complete */ 4899 - static irqreturn_t gfar_transmit(int irq, void *grp_id) 4900 - { 4901 - struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id; 4902 - unsigned long flags; 4903 - u32 imask; 4904 - 4905 - if (likely(napi_schedule_prep(&grp->napi_tx))) { 4906 - spin_lock_irqsave(&grp->grplock, flags); 4907 - imask = gfar_read(&grp->regs->imask); 4908 - imask &= IMASK_TX_DISABLED; 4909 - gfar_write(&grp->regs->imask, imask); 4910 - spin_unlock_irqrestore(&grp->grplock, flags); 4911 - __napi_schedule(&grp->napi_tx); 4912 - } else { 4913 - /* Clear IEVENT, so interrupts aren't called again 4914 - * because of the packets that have already arrived. 4915 - */ 4916 - gfar_write(&grp->regs->ievent, IEVENT_TX_MASK); 4917 - } 4918 - 4919 - return IRQ_HANDLED; 4920 - } 4921 - 4922 - static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus, 4923 - struct sk_buff *skb, bool first) 4924 - { 4925 - int size = lstatus & BD_LENGTH_MASK; 4926 - struct page *page = rxb->page; 4927 - 4928 - if (likely(first)) { 4929 - skb_put(skb, size); 4930 - } else { 4931 - /* the last fragments' length contains the full frame length */ 4932 - if (lstatus & BD_LFLAG(RXBD_LAST)) 4933 - size -= skb->len; 4934 - 4935 - skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 4936 - rxb->page_offset + RXBUF_ALIGNMENT, 4937 - size, GFAR_RXB_TRUESIZE); 4938 - } 4939 - 4940 - /* try reuse page */ 4941 - if (unlikely(page_count(page) != 1 || page_is_pfmemalloc(page))) 4942 - return false; 4943 - 4944 - /* change offset to the other half */ 4945 - rxb->page_offset ^= GFAR_RXB_TRUESIZE; 4946 - 4947 - page_ref_inc(page); 4948 - 4949 - return true; 4950 - } 4951 - 4952 - static void gfar_reuse_rx_page(struct gfar_priv_rx_q *rxq, 4953 - struct gfar_rx_buff *old_rxb) 4954 - { 4955 - struct gfar_rx_buff *new_rxb; 4956 - u16 nta = rxq->next_to_alloc; 4957 - 4958 - new_rxb = &rxq->rx_buff[nta]; 4959 - 4960 - /* find next buf that can reuse a page */ 4961 - nta++; 4962 - rxq->next_to_alloc = (nta < rxq->rx_ring_size) ? nta : 0; 4963 - 4964 - /* copy page reference */ 4965 - *new_rxb = *old_rxb; 4966 - 4967 - /* sync for use by the device */ 4968 - dma_sync_single_range_for_device(rxq->dev, old_rxb->dma, 4969 - old_rxb->page_offset, 4970 - GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE); 4971 - } 4972 - 4973 - static struct sk_buff *gfar_get_next_rxbuff(struct gfar_priv_rx_q *rx_queue, 4974 - u32 lstatus, struct sk_buff *skb) 4975 - { 4976 - struct gfar_rx_buff *rxb = &rx_queue->rx_buff[rx_queue->next_to_clean]; 4977 - struct page *page = rxb->page; 4978 - bool first = false; 4979 - 4980 - if (likely(!skb)) { 4981 - void *buff_addr = page_address(page) + rxb->page_offset; 4982 - 4983 - skb = build_skb(buff_addr, GFAR_SKBFRAG_SIZE); 4984 - if (unlikely(!skb)) { 4985 - gfar_rx_alloc_err(rx_queue); 4986 - return NULL; 4987 - } 4988 - skb_reserve(skb, RXBUF_ALIGNMENT); 4989 - first = true; 4990 - } 4991 - 4992 - dma_sync_single_range_for_cpu(rx_queue->dev, rxb->dma, rxb->page_offset, 4993 - GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE); 4994 - 4995 - if (gfar_add_rx_frag(rxb, lstatus, skb, first)) { 4996 - /* reuse the free half of the page */ 4997 - gfar_reuse_rx_page(rx_queue, rxb); 4998 - } else { 4999 - /* page cannot be reused, unmap it */ 5000 - dma_unmap_page(rx_queue->dev, rxb->dma, 5001 - PAGE_SIZE, DMA_FROM_DEVICE); 5002 - } 5003 - 5004 - /* clear rxb content */ 5005 - rxb->page = NULL; 5006 - 5007 - return skb; 5008 - } 5009 - 5010 - static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb) 5011 - { 5012 - /* If valid headers were found, and valid sums 5013 - * were verified, then we tell the kernel that no 5014 - * checksumming is necessary. Otherwise, it is [FIXME] 5015 - */ 5016 - if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) == 5017 - (RXFCB_CIP | RXFCB_CTU)) 5018 - skb->ip_summed = CHECKSUM_UNNECESSARY; 5019 - else 5020 - skb_checksum_none_assert(skb); 5021 - } 5022 - 5023 - /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */ 5024 - static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb) 5025 - { 5026 - struct gfar_private *priv = netdev_priv(ndev); 5027 - struct rxfcb *fcb = NULL; 5028 - 5029 - /* fcb is at the beginning if exists */ 5030 - fcb = (struct rxfcb *)skb->data; 5031 - 5032 - /* Remove the FCB from the skb 5033 - * Remove the padded bytes, if there are any 5034 - */ 5035 - if (priv->uses_rxfcb) 5036 - skb_pull(skb, GMAC_FCB_LEN); 5037 - 5038 - /* Get receive timestamp from the skb */ 5039 - if (priv->hwts_rx_en) { 5040 - struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 5041 - u64 *ns = (u64 *) skb->data; 5042 - 5043 - memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 5044 - shhwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*ns)); 5045 - } 5046 - 5047 - if (priv->padding) 5048 - skb_pull(skb, priv->padding); 5049 - 5050 - /* Trim off the FCS */ 5051 - pskb_trim(skb, skb->len - ETH_FCS_LEN); 5052 - 5053 - if (ndev->features & NETIF_F_RXCSUM) 5054 - gfar_rx_checksum(skb, fcb); 5055 - 5056 - /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here. 5057 - * Even if vlan rx accel is disabled, on some chips 5058 - * RXFCB_VLN is pseudo randomly set. 5059 - */ 5060 - if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX && 5061 - be16_to_cpu(fcb->flags) & RXFCB_VLN) 5062 - __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 5063 - be16_to_cpu(fcb->vlctl)); 5064 - } 5065 - 5066 - /* gfar_clean_rx_ring() -- Processes each frame in the rx ring 5067 - * until the budget/quota has been reached. Returns the number 5068 - * of frames handled 5069 - */ 5070 - int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit) 5071 - { 5072 - struct net_device *ndev = rx_queue->ndev; 5073 - struct gfar_private *priv = netdev_priv(ndev); 5074 - struct rxbd8 *bdp; 5075 - int i, howmany = 0; 5076 - struct sk_buff *skb = rx_queue->skb; 5077 - int cleaned_cnt = gfar_rxbd_unused(rx_queue); 5078 - unsigned int total_bytes = 0, total_pkts = 0; 5079 - 5080 - /* Get the first full descriptor */ 5081 - i = rx_queue->next_to_clean; 5082 - 5083 - while (rx_work_limit--) { 5084 - u32 lstatus; 5085 - 5086 - if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) { 5087 - gfar_alloc_rx_buffs(rx_queue, cleaned_cnt); 5088 - cleaned_cnt = 0; 5089 - } 5090 - 5091 - bdp = &rx_queue->rx_bd_base[i]; 5092 - lstatus = be32_to_cpu(bdp->lstatus); 5093 - if (lstatus & BD_LFLAG(RXBD_EMPTY)) 5094 - break; 5095 - 5096 - /* order rx buffer descriptor reads */ 5097 - rmb(); 5098 - 5099 - /* fetch next to clean buffer from the ring */ 5100 - skb = gfar_get_next_rxbuff(rx_queue, lstatus, skb); 5101 - if (unlikely(!skb)) 5102 - break; 5103 - 5104 - cleaned_cnt++; 5105 - howmany++; 5106 - 5107 - if (unlikely(++i == rx_queue->rx_ring_size)) 5108 - i = 0; 5109 - 5110 - rx_queue->next_to_clean = i; 5111 - 5112 - /* fetch next buffer if not the last in frame */ 5113 - if (!(lstatus & BD_LFLAG(RXBD_LAST))) 5114 - continue; 5115 - 5116 - if (unlikely(lstatus & BD_LFLAG(RXBD_ERR))) { 5117 - count_errors(lstatus, ndev); 5118 - 5119 - /* discard faulty buffer */ 5120 - dev_kfree_skb(skb); 5121 - skb = NULL; 5122 - rx_queue->stats.rx_dropped++; 5123 - continue; 5124 - } 5125 - 5126 - gfar_process_frame(ndev, skb); 5127 - 5128 - /* Increment the number of packets */ 5129 - total_pkts++; 5130 - total_bytes += skb->len; 5131 - 5132 - skb_record_rx_queue(skb, rx_queue->qindex); 5133 - 5134 - skb->protocol = eth_type_trans(skb, ndev); 5135 - 5136 - /* Send the packet up the stack */ 5137 - napi_gro_receive(&rx_queue->grp->napi_rx, skb); 5138 - 5139 - skb = NULL; 5140 - } 5141 - 5142 - /* Store incomplete frames for completion */ 5143 - rx_queue->skb = skb; 5144 - 5145 - rx_queue->stats.rx_packets += total_pkts; 5146 - rx_queue->stats.rx_bytes += total_bytes; 5147 - 5148 - if (cleaned_cnt) 5149 - gfar_alloc_rx_buffs(rx_queue, cleaned_cnt); 5150 - 5151 - /* Update Last Free RxBD pointer for LFC */ 5152 - if (unlikely(priv->tx_actual_en)) { 5153 - u32 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue); 5154 - 5155 - gfar_write(rx_queue->rfbptr, bdp_dma); 5156 - } 5157 - 5158 - return howmany; 5159 - } 5160 - 5161 - static int gfar_poll_rx_sq(struct napi_struct *napi, int budget) 5162 - { 5163 - struct gfar_priv_grp *gfargrp = 5164 - container_of(napi, struct gfar_priv_grp, napi_rx); 5165 - struct gfar __iomem *regs = gfargrp->regs; 5166 - struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue; 5167 - int work_done = 0; 5168 - 5169 - /* Clear IEVENT, so interrupts aren't called again 5170 - * because of the packets that have already arrived 5171 - */ 5172 - gfar_write(&regs->ievent, IEVENT_RX_MASK); 5173 - 5174 - work_done = gfar_clean_rx_ring(rx_queue, budget); 5175 - 5176 - if (work_done < budget) { 5177 - u32 imask; 5178 - napi_complete_done(napi, work_done); 5179 - /* Clear the halt bit in RSTAT */ 5180 - gfar_write(&regs->rstat, gfargrp->rstat); 5181 - 5182 - spin_lock_irq(&gfargrp->grplock); 5183 - imask = gfar_read(&regs->imask); 5184 - imask |= IMASK_RX_DEFAULT; 5185 - gfar_write(&regs->imask, imask); 5186 - spin_unlock_irq(&gfargrp->grplock); 5187 - } 5188 - 5189 - return work_done; 5190 - } 5191 - 5192 - static int gfar_poll_tx_sq(struct napi_struct *napi, int budget) 5193 - { 5194 - struct gfar_priv_grp *gfargrp = 5195 - container_of(napi, struct gfar_priv_grp, napi_tx); 5196 - struct gfar __iomem *regs = gfargrp->regs; 5197 - struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue; 5198 - u32 imask; 5199 - 5200 - /* Clear IEVENT, so interrupts aren't called again 5201 - * because of the packets that have already arrived 5202 - */ 5203 - gfar_write(&regs->ievent, IEVENT_TX_MASK); 5204 - 5205 - /* run Tx cleanup to completion */ 5206 - if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) 5207 - gfar_clean_tx_ring(tx_queue); 5208 - 5209 - napi_complete(napi); 5210 - 5211 - spin_lock_irq(&gfargrp->grplock); 5212 - imask = gfar_read(&regs->imask); 5213 - imask |= IMASK_TX_DEFAULT; 5214 - gfar_write(&regs->imask, imask); 5215 - spin_unlock_irq(&gfargrp->grplock); 5216 - 5217 - return 0; 5218 - } 5219 - 5220 - static int gfar_poll_rx(struct napi_struct *napi, int budget) 5221 - { 5222 - struct gfar_priv_grp *gfargrp = 5223 - container_of(napi, struct gfar_priv_grp, napi_rx); 5224 - struct gfar_private *priv = gfargrp->priv; 5225 - struct gfar __iomem *regs = gfargrp->regs; 5226 - struct gfar_priv_rx_q *rx_queue = NULL; 5227 - int work_done = 0, work_done_per_q = 0; 5228 - int i, budget_per_q = 0; 5229 - unsigned long rstat_rxf; 5230 - int num_act_queues; 5231 - 5232 - /* Clear IEVENT, so interrupts aren't called again 5233 - * because of the packets that have already arrived 5234 - */ 5235 - gfar_write(&regs->ievent, IEVENT_RX_MASK); 5236 - 5237 - rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK; 5238 - 5239 - num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS); 5240 - if (num_act_queues) 5241 - budget_per_q = budget/num_act_queues; 5242 - 5243 - for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) { 5244 - /* skip queue if not active */ 5245 - if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i))) 5246 - continue; 5247 - 5248 - rx_queue = priv->rx_queue[i]; 5249 - work_done_per_q = 5250 - gfar_clean_rx_ring(rx_queue, budget_per_q); 5251 - work_done += work_done_per_q; 5252 - 5253 - /* finished processing this queue */ 5254 - if (work_done_per_q < budget_per_q) { 5255 - /* clear active queue hw indication */ 5256 - gfar_write(&regs->rstat, 5257 - RSTAT_CLEAR_RXF0 >> i); 5258 - num_act_queues--; 5259 - 5260 - if (!num_act_queues) 5261 - break; 5262 - } 5263 - } 5264 - 5265 - if (!num_act_queues) { 5266 - u32 imask; 5267 - napi_complete_done(napi, work_done); 5268 - 5269 - /* Clear the halt bit in RSTAT */ 5270 - gfar_write(&regs->rstat, gfargrp->rstat); 5271 - 5272 - spin_lock_irq(&gfargrp->grplock); 5273 - imask = gfar_read(&regs->imask); 5274 - imask |= IMASK_RX_DEFAULT; 5275 - gfar_write(&regs->imask, imask); 5276 - spin_unlock_irq(&gfargrp->grplock); 5277 - } 5278 - 5279 - return work_done; 5280 - } 5281 - 5282 - static int gfar_poll_tx(struct napi_struct *napi, int budget) 5283 - { 5284 - struct gfar_priv_grp *gfargrp = 5285 - container_of(napi, struct gfar_priv_grp, napi_tx); 5286 - struct gfar_private *priv = gfargrp->priv; 5287 - struct gfar __iomem *regs = gfargrp->regs; 5288 - struct gfar_priv_tx_q *tx_queue = NULL; 5289 - int has_tx_work = 0; 5290 - int i; 5291 - 5292 - /* Clear IEVENT, so interrupts aren't called again 5293 - * because of the packets that have already arrived 5294 - */ 5295 - gfar_write(&regs->ievent, IEVENT_TX_MASK); 5296 - 5297 - for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) { 5298 - tx_queue = priv->tx_queue[i]; 5299 - /* run Tx cleanup to completion */ 5300 - if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) { 5301 - gfar_clean_tx_ring(tx_queue); 5302 - has_tx_work = 1; 5303 - } 5304 - } 5305 - 5306 - if (!has_tx_work) { 5307 - u32 imask; 5308 - napi_complete(napi); 5309 - 5310 - spin_lock_irq(&gfargrp->grplock); 5311 - imask = gfar_read(&regs->imask); 5312 - imask |= IMASK_TX_DEFAULT; 5313 - gfar_write(&regs->imask, imask); 5314 - spin_unlock_irq(&gfargrp->grplock); 5315 - } 5316 - 5317 - return 0; 5318 - } 5319 - 5320 - 5321 - #ifdef CONFIG_NET_POLL_CONTROLLER 5322 - /* Polling 'interrupt' - used by things like netconsole to send skbs 5323 - * without having to re-enable interrupts. It's not called while 5324 - * the interrupt routine is executing. 5325 - */ 5326 - static void gfar_netpoll(struct net_device *dev) 5327 - { 5328 - struct gfar_private *priv = netdev_priv(dev); 5329 - int i; 5330 - 5331 - /* If the device has multiple interrupts, run tx/rx */ 5332 - if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) { 5333 - for (i = 0; i < priv->num_grps; i++) { 5334 - struct gfar_priv_grp *grp = &priv->gfargrp[i]; 5335 - 5336 - disable_irq(gfar_irq(grp, TX)->irq); 5337 - disable_irq(gfar_irq(grp, RX)->irq); 5338 - disable_irq(gfar_irq(grp, ER)->irq); 5339 - gfar_interrupt(gfar_irq(grp, TX)->irq, grp); 5340 - enable_irq(gfar_irq(grp, ER)->irq); 5341 - enable_irq(gfar_irq(grp, RX)->irq); 5342 - enable_irq(gfar_irq(grp, TX)->irq); 5343 - } 5344 - } else { 5345 - for (i = 0; i < priv->num_grps; i++) { 5346 - struct gfar_priv_grp *grp = &priv->gfargrp[i]; 5347 - 5348 - disable_irq(gfar_irq(grp, TX)->irq); 5349 - gfar_interrupt(gfar_irq(grp, TX)->irq, grp); 5350 - enable_irq(gfar_irq(grp, TX)->irq); 5351 - } 5352 - } 5353 - } 5354 - #endif 5355 - 5356 - /* The interrupt handler for devices with one interrupt */ 5357 - static irqreturn_t gfar_interrupt(int irq, void *grp_id) 5358 - { 5359 - struct gfar_priv_grp *gfargrp = grp_id; 5360 - 5361 - /* Save ievent for future reference */ 5362 - u32 events = gfar_read(&gfargrp->regs->ievent); 5363 - 5364 - /* Check for reception */ 5365 - if (events & IEVENT_RX_MASK) 5366 - gfar_receive(irq, grp_id); 5367 - 5368 - /* Check for transmit completion */ 5369 - if (events & IEVENT_TX_MASK) 5370 - gfar_transmit(irq, grp_id); 5371 - 5372 - /* Check for errors */ 5373 - if (events & IEVENT_ERR_MASK) 5374 - gfar_error(irq, grp_id); 5375 - 5376 - return IRQ_HANDLED; 5377 - } 5378 - 5379 - /* Called every time the controller might need to be made 5380 - * aware of new link state. The PHY code conveys this 5381 - * information through variables in the phydev structure, and this 5382 - * function converts those variables into the appropriate 5383 - * register values, and can bring down the device if needed. 5384 - */ 5385 - static void adjust_link(struct net_device *dev) 5386 - { 5387 - struct gfar_private *priv = netdev_priv(dev); 5388 - struct phy_device *phydev = dev->phydev; 5389 - 5390 - if (unlikely(phydev->link != priv->oldlink || 5391 - (phydev->link && (phydev->duplex != priv->oldduplex || 5392 - phydev->speed != priv->oldspeed)))) 5393 - gfar_update_link_state(priv); 5394 - } 5395 - 5396 - /* Update the hash table based on the current list of multicast 5397 - * addresses we subscribe to. Also, change the promiscuity of 5398 - * the device based on the flags (this function is called 5399 - * whenever dev->flags is changed 5400 - */ 5401 - static void gfar_set_multi(struct net_device *dev) 5402 - { 5403 - struct netdev_hw_addr *ha; 5404 - struct gfar_private *priv = netdev_priv(dev); 5405 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 5406 - u32 tempval; 5407 - 5408 - if (dev->flags & IFF_PROMISC) { 5409 - /* Set RCTRL to PROM */ 5410 - tempval = gfar_read(&regs->rctrl); 5411 - tempval |= RCTRL_PROM; 5412 - gfar_write(&regs->rctrl, tempval); 5413 - } else { 5414 - /* Set RCTRL to not PROM */ 5415 - tempval = gfar_read(&regs->rctrl); 5416 - tempval &= ~(RCTRL_PROM); 5417 - gfar_write(&regs->rctrl, tempval); 5418 - } 5419 - 5420 - if (dev->flags & IFF_ALLMULTI) { 5421 - /* Set the hash to rx all multicast frames */ 5422 - gfar_write(&regs->igaddr0, 0xffffffff); 5423 - gfar_write(&regs->igaddr1, 0xffffffff); 5424 - gfar_write(&regs->igaddr2, 0xffffffff); 5425 - gfar_write(&regs->igaddr3, 0xffffffff); 5426 - gfar_write(&regs->igaddr4, 0xffffffff); 5427 - gfar_write(&regs->igaddr5, 0xffffffff); 5428 - gfar_write(&regs->igaddr6, 0xffffffff); 5429 - gfar_write(&regs->igaddr7, 0xffffffff); 5430 - gfar_write(&regs->gaddr0, 0xffffffff); 5431 - gfar_write(&regs->gaddr1, 0xffffffff); 5432 - gfar_write(&regs->gaddr2, 0xffffffff); 5433 - gfar_write(&regs->gaddr3, 0xffffffff); 5434 - gfar_write(&regs->gaddr4, 0xffffffff); 5435 - gfar_write(&regs->gaddr5, 0xffffffff); 5436 - gfar_write(&regs->gaddr6, 0xffffffff); 5437 - gfar_write(&regs->gaddr7, 0xffffffff); 5438 - } else { 5439 - int em_num; 5440 - int idx; 5441 - 5442 - /* zero out the hash */ 5443 - gfar_write(&regs->igaddr0, 0x0); 5444 - gfar_write(&regs->igaddr1, 0x0); 5445 - gfar_write(&regs->igaddr2, 0x0); 5446 - gfar_write(&regs->igaddr3, 0x0); 5447 - gfar_write(&regs->igaddr4, 0x0); 5448 - gfar_write(&regs->igaddr5, 0x0); 5449 - gfar_write(&regs->igaddr6, 0x0); 5450 - gfar_write(&regs->igaddr7, 0x0); 5451 - gfar_write(&regs->gaddr0, 0x0); 5452 - gfar_write(&regs->gaddr1, 0x0); 5453 - gfar_write(&regs->gaddr2, 0x0); 5454 - gfar_write(&regs->gaddr3, 0x0); 5455 - gfar_write(&regs->gaddr4, 0x0); 5456 - gfar_write(&regs->gaddr5, 0x0); 5457 - gfar_write(&regs->gaddr6, 0x0); 5458 - gfar_write(&regs->gaddr7, 0x0); 5459 - 5460 - /* If we have extended hash tables, we need to 5461 - * clear the exact match registers to prepare for 5462 - * setting them 5463 - */ 5464 - if (priv->extended_hash) { 5465 - em_num = GFAR_EM_NUM + 1; 5466 - gfar_clear_exact_match(dev); 5467 - idx = 1; 5468 - } else { 5469 - idx = 0; 5470 - em_num = 0; 5471 - } 5472 - 5473 - if (netdev_mc_empty(dev)) 5474 - return; 5475 - 5476 - /* Parse the list, and set the appropriate bits */ 5477 - netdev_for_each_mc_addr(ha, dev) { 5478 - if (idx < em_num) { 5479 - gfar_set_mac_for_addr(dev, idx, ha->addr); 5480 - idx++; 5481 - } else 5482 - gfar_set_hash_for_addr(dev, ha->addr); 5483 - } 5484 - } 5485 - } 5486 - 5487 - 5488 - /* Clears each of the exact match registers to zero, so they 5489 - * don't interfere with normal reception 5490 - */ 5491 - static void gfar_clear_exact_match(struct net_device *dev) 5492 - { 5493 - int idx; 5494 - static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; 5495 - 5496 - for (idx = 1; idx < GFAR_EM_NUM + 1; idx++) 5497 - gfar_set_mac_for_addr(dev, idx, zero_arr); 5498 - } 5499 - 5500 - /* Set the appropriate hash bit for the given addr */ 5501 - /* The algorithm works like so: 5502 - * 1) Take the Destination Address (ie the multicast address), and 5503 - * do a CRC on it (little endian), and reverse the bits of the 5504 - * result. 5505 - * 2) Use the 8 most significant bits as a hash into a 256-entry 5506 - * table. The table is controlled through 8 32-bit registers: 5507 - * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is 5508 - * gaddr7. This means that the 3 most significant bits in the 5509 - * hash index which gaddr register to use, and the 5 other bits 5510 - * indicate which bit (assuming an IBM numbering scheme, which 5511 - * for PowerPC (tm) is usually the case) in the register holds 5512 - * the entry. 5513 - */ 5514 - static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr) 5515 - { 5516 - u32 tempval; 5517 - struct gfar_private *priv = netdev_priv(dev); 5518 - u32 result = ether_crc(ETH_ALEN, addr); 5519 - int width = priv->hash_width; 5520 - u8 whichbit = (result >> (32 - width)) & 0x1f; 5521 - u8 whichreg = result >> (32 - width + 5); 5522 - u32 value = (1 << (31-whichbit)); 5523 - 5524 - tempval = gfar_read(priv->hash_regs[whichreg]); 5525 - tempval |= value; 5526 - gfar_write(priv->hash_regs[whichreg], tempval); 5527 - } 5528 - 5529 - 5530 - /* There are multiple MAC Address register pairs on some controllers 5531 - * This function sets the numth pair to a given address 5532 - */ 5533 - static void gfar_set_mac_for_addr(struct net_device *dev, int num, 5534 - const u8 *addr) 5535 - { 5536 - struct gfar_private *priv = netdev_priv(dev); 5537 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 5538 - u32 tempval; 5539 - u32 __iomem *macptr = &regs->macstnaddr1; 5540 - 5541 - macptr += num*2; 5542 - 5543 - /* For a station address of 0x12345678ABCD in transmission 5544 - * order (BE), MACnADDR1 is set to 0xCDAB7856 and 5545 - * MACnADDR2 is set to 0x34120000. 5546 - */ 5547 - tempval = (addr[5] << 24) | (addr[4] << 16) | 5548 - (addr[3] << 8) | addr[2]; 5549 - 5550 - gfar_write(macptr, tempval); 5551 - 5552 - tempval = (addr[1] << 24) | (addr[0] << 16); 5553 - 5554 - gfar_write(macptr+1, tempval); 5555 - } 5556 - 5557 - /* GFAR error interrupt handler */ 5558 - static irqreturn_t gfar_error(int irq, void *grp_id) 5559 - { 5560 - struct gfar_priv_grp *gfargrp = grp_id; 5561 - struct gfar __iomem *regs = gfargrp->regs; 5562 - struct gfar_private *priv= gfargrp->priv; 5563 - struct net_device *dev = priv->ndev; 5564 - 5565 - /* Save ievent for future reference */ 5566 - u32 events = gfar_read(&regs->ievent); 5567 - 5568 - /* Clear IEVENT */ 5569 - gfar_write(&regs->ievent, events & IEVENT_ERR_MASK); 5570 - 5571 - /* Magic Packet is not an error. */ 5572 - if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) && 5573 - (events & IEVENT_MAG)) 5574 - events &= ~IEVENT_MAG; 5575 - 5576 - /* Hmm... */ 5577 - if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv)) 5578 - netdev_dbg(dev, 5579 - "error interrupt (ievent=0x%08x imask=0x%08x)\n", 5580 - events, gfar_read(&regs->imask)); 5581 - 5582 - /* Update the error counters */ 5583 - if (events & IEVENT_TXE) { 5584 - dev->stats.tx_errors++; 5585 - 5586 - if (events & IEVENT_LC) 5587 - dev->stats.tx_window_errors++; 5588 - if (events & IEVENT_CRL) 5589 - dev->stats.tx_aborted_errors++; 5590 - if (events & IEVENT_XFUN) { 5591 - netif_dbg(priv, tx_err, dev, 5592 - "TX FIFO underrun, packet dropped\n"); 5593 - dev->stats.tx_dropped++; 5594 - atomic64_inc(&priv->extra_stats.tx_underrun); 5595 - 5596 - schedule_work(&priv->reset_task); 5597 - } 5598 - netif_dbg(priv, tx_err, dev, "Transmit Error\n"); 5599 - } 5600 - if (events & IEVENT_BSY) { 5601 - dev->stats.rx_over_errors++; 5602 - atomic64_inc(&priv->extra_stats.rx_bsy); 5603 - 5604 - netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n", 5605 - gfar_read(&regs->rstat)); 5606 - } 5607 - if (events & IEVENT_BABR) { 5608 - dev->stats.rx_errors++; 5609 - atomic64_inc(&priv->extra_stats.rx_babr); 5610 - 5611 - netif_dbg(priv, rx_err, dev, "babbling RX error\n"); 5612 - } 5613 - if (events & IEVENT_EBERR) { 5614 - atomic64_inc(&priv->extra_stats.eberr); 5615 - netif_dbg(priv, rx_err, dev, "bus error\n"); 5616 - } 5617 - if (events & IEVENT_RXC) 5618 - netif_dbg(priv, rx_status, dev, "control frame\n"); 5619 - 5620 - if (events & IEVENT_BABT) { 5621 - atomic64_inc(&priv->extra_stats.tx_babt); 5622 - netif_dbg(priv, tx_err, dev, "babbling TX error\n"); 5623 - } 5624 - return IRQ_HANDLED; 5625 - } 5626 - 5627 - static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv) 5628 - { 5629 - struct net_device *ndev = priv->ndev; 5630 - struct phy_device *phydev = ndev->phydev; 5631 - u32 val = 0; 5632 - 5633 - if (!phydev->duplex) 5634 - return val; 5635 - 5636 - if (!priv->pause_aneg_en) { 5637 - if (priv->tx_pause_en) 5638 - val |= MACCFG1_TX_FLOW; 5639 - if (priv->rx_pause_en) 5640 - val |= MACCFG1_RX_FLOW; 5641 - } else { 5642 - u16 lcl_adv, rmt_adv; 5643 - u8 flowctrl; 5644 - /* get link partner capabilities */ 5645 - rmt_adv = 0; 5646 - if (phydev->pause) 5647 - rmt_adv = LPA_PAUSE_CAP; 5648 - if (phydev->asym_pause) 5649 - rmt_adv |= LPA_PAUSE_ASYM; 5650 - 5651 - lcl_adv = linkmode_adv_to_lcl_adv_t(phydev->advertising); 5652 - flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv); 5653 - if (flowctrl & FLOW_CTRL_TX) 5654 - val |= MACCFG1_TX_FLOW; 5655 - if (flowctrl & FLOW_CTRL_RX) 5656 - val |= MACCFG1_RX_FLOW; 5657 - } 5658 - 5659 - return val; 5660 - } 5661 - 5662 - static noinline void gfar_update_link_state(struct gfar_private *priv) 5663 - { 5664 - struct gfar __iomem *regs = priv->gfargrp[0].regs; 5665 - struct net_device *ndev = priv->ndev; 5666 - struct phy_device *phydev = ndev->phydev; 5667 - struct gfar_priv_rx_q *rx_queue = NULL; 5668 - int i; 5669 - 5670 - if (unlikely(test_bit(GFAR_RESETTING, &priv->state))) 5671 - return; 5672 - 5673 - if (phydev->link) { 5674 - u32 tempval1 = gfar_read(&regs->maccfg1); 5675 - u32 tempval = gfar_read(&regs->maccfg2); 5676 - u32 ecntrl = gfar_read(&regs->ecntrl); 5677 - u32 tx_flow_oldval = (tempval1 & MACCFG1_TX_FLOW); 5678 - 5679 - if (phydev->duplex != priv->oldduplex) { 5680 - if (!(phydev->duplex)) 5681 - tempval &= ~(MACCFG2_FULL_DUPLEX); 5682 - else 5683 - tempval |= MACCFG2_FULL_DUPLEX; 5684 - 5685 - priv->oldduplex = phydev->duplex; 5686 - } 5687 - 5688 - if (phydev->speed != priv->oldspeed) { 5689 - switch (phydev->speed) { 5690 - case 1000: 5691 - tempval = 5692 - ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII); 5693 - 5694 - ecntrl &= ~(ECNTRL_R100); 5695 - break; 5696 - case 100: 5697 - case 10: 5698 - tempval = 5699 - ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII); 5700 - 5701 - /* Reduced mode distinguishes 5702 - * between 10 and 100 5703 - */ 5704 - if (phydev->speed == SPEED_100) 5705 - ecntrl |= ECNTRL_R100; 5706 - else 5707 - ecntrl &= ~(ECNTRL_R100); 5708 - break; 5709 - default: 5710 - netif_warn(priv, link, priv->ndev, 5711 - "Ack! Speed (%d) is not 10/100/1000!\n", 5712 - phydev->speed); 5713 - break; 5714 - } 5715 - 5716 - priv->oldspeed = phydev->speed; 5717 - } 5718 - 5719 - tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW); 5720 - tempval1 |= gfar_get_flowctrl_cfg(priv); 5721 - 5722 - /* Turn last free buffer recording on */ 5723 - if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) { 5724 - for (i = 0; i < priv->num_rx_queues; i++) { 5725 - u32 bdp_dma; 5726 - 5727 - rx_queue = priv->rx_queue[i]; 5728 - bdp_dma = gfar_rxbd_dma_lastfree(rx_queue); 5729 - gfar_write(rx_queue->rfbptr, bdp_dma); 5730 - } 5731 - 5732 - priv->tx_actual_en = 1; 5733 - } 5734 - 5735 - if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval)) 5736 - priv->tx_actual_en = 0; 5737 - 5738 - gfar_write(&regs->maccfg1, tempval1); 5739 - gfar_write(&regs->maccfg2, tempval); 5740 - gfar_write(&regs->ecntrl, ecntrl); 5741 - 5742 - if (!priv->oldlink) 5743 - priv->oldlink = 1; 5744 - 5745 - } else if (priv->oldlink) { 5746 - priv->oldlink = 0; 5747 - priv->oldspeed = 0; 5748 - priv->oldduplex = -1; 5749 - } 5750 - 5751 - if (netif_msg_link(priv)) 5752 - phy_print_status(phydev); 5753 - } 5754 1733 5755 1734 static const struct of_device_id gfar_match[] = 5756 1735 {
-45
drivers/net/ethernet/freescale/gianfar.h
··· 67 67 /* Number of bytes to align the rx bufs to */ 68 68 #define RXBUF_ALIGNMENT 64 69 69 70 - #define PHY_INIT_TIMEOUT 100000 71 - 72 70 #define DRV_NAME "gfar-enet" 73 71 extern const char gfar_driver_version[]; 74 72 ··· 86 88 #define GFAR_RX_MAX_RING_SIZE 256 87 89 #define GFAR_TX_MAX_RING_SIZE 256 88 90 89 - #define GFAR_MAX_FIFO_THRESHOLD 511 90 - #define GFAR_MAX_FIFO_STARVE 511 91 - #define GFAR_MAX_FIFO_STARVE_OFF 511 92 - 93 91 #define FBTHR_SHIFT 24 94 92 #define DEFAULT_RX_LFC_THR 16 95 93 #define DEFAULT_LFC_PTVVAL 4 ··· 103 109 #define DEFAULT_FIFO_TX_THR 0x100 104 110 #define DEFAULT_FIFO_TX_STARVE 0x40 105 111 #define DEFAULT_FIFO_TX_STARVE_OFF 0x80 106 - #define DEFAULT_BD_STASH 1 107 - #define DEFAULT_STASH_LENGTH 96 108 - #define DEFAULT_STASH_INDEX 0 109 112 110 113 /* The number of Exact Match registers */ 111 114 #define GFAR_EM_NUM 15 ··· 129 138 130 139 #define DEFAULT_RX_COALESCE 0 131 140 #define DEFAULT_RXCOUNT 0 132 - 133 - #define GFAR_SUPPORTED (SUPPORTED_10baseT_Half \ 134 - | SUPPORTED_10baseT_Full \ 135 - | SUPPORTED_100baseT_Half \ 136 - | SUPPORTED_100baseT_Full \ 137 - | SUPPORTED_Autoneg \ 138 - | SUPPORTED_MII) 139 - 140 - #define GFAR_SUPPORTED_GBIT SUPPORTED_1000baseT_Full 141 141 142 142 /* TBI register addresses */ 143 143 #define MII_TBICON 0x11 ··· 166 184 #define ECNTRL_R100 0x00000008 167 185 #define ECNTRL_REDUCED_MII_MODE 0x00000004 168 186 #define ECNTRL_SGMII_MODE 0x00000002 169 - 170 - #define MRBLR_INIT_SETTINGS DEFAULT_RX_BUFFER_SIZE 171 187 172 188 #define MINFLR_INIT_SETTINGS 0x00000040 173 189 ··· 245 265 246 266 #define DEFAULT_TXIC mk_ic_value(DEFAULT_TXCOUNT, DEFAULT_TXTIME) 247 267 #define DEFAULT_RXIC mk_ic_value(DEFAULT_RXCOUNT, DEFAULT_RXTIME) 248 - 249 - #define skip_bd(bdp, stride, base, ring_size) ({ \ 250 - typeof(bdp) new_bd = (bdp) + (stride); \ 251 - (new_bd >= (base) + (ring_size)) ? (new_bd - (ring_size)) : new_bd; }) 252 - 253 - #define next_bd(bdp, base, ring_size) skip_bd(bdp, 1, base, ring_size) 254 268 255 269 #define RCTRL_TS_ENABLE 0x01000000 256 270 #define RCTRL_PAL_MASK 0x001f0000 ··· 358 384 359 385 #define IMASK_RX_DISABLED ((~(IMASK_RX_DEFAULT)) & IMASK_DEFAULT) 360 386 #define IMASK_TX_DISABLED ((~(IMASK_TX_DEFAULT)) & IMASK_DEFAULT) 361 - 362 - /* Fifo management */ 363 - #define FIFO_TX_THR_MASK 0x01ff 364 - #define FIFO_TX_STARVE_MASK 0x01ff 365 - #define FIFO_TX_STARVE_OFF_MASK 0x01ff 366 387 367 388 /* Attribute fields */ 368 389 ··· 1295 1326 return bdp_dma; 1296 1327 } 1297 1328 1298 - irqreturn_t gfar_receive(int irq, void *dev_id); 1299 1329 int startup_gfar(struct net_device *dev); 1300 1330 void stop_gfar(struct net_device *dev); 1301 - void reset_gfar(struct net_device *dev); 1302 1331 void gfar_mac_reset(struct gfar_private *priv); 1303 - void gfar_halt(struct gfar_private *priv); 1304 - void gfar_start(struct gfar_private *priv); 1305 - void gfar_phy_test(struct mii_bus *bus, struct phy_device *phydev, int enable, 1306 - u32 regnum, u32 read); 1307 - void gfar_configure_coalescing_all(struct gfar_private *priv); 1308 1332 int gfar_set_features(struct net_device *dev, netdev_features_t features); 1309 1333 1310 1334 extern const struct ethtool_ops gfar_ethtool_ops; ··· 1309 1347 #define RQFCR_PID_VID_MASK 0xFFFFF000 1310 1348 #define RQFCR_PID_PORT_MASK 0xFFFF0000 1311 1349 #define RQFCR_PID_MAC_MASK 0xFF000000 1312 - 1313 - struct gfar_mask_entry { 1314 - unsigned int mask; /* The mask value which is valid form start to end */ 1315 - unsigned int start; 1316 - unsigned int end; 1317 - unsigned int block; /* Same block values indicate depended entries */ 1318 - }; 1319 1350 1320 1351 /* Represents a receive filer table entry */ 1321 1352 struct gfar_filer_entry {
-13
drivers/net/ethernet/freescale/gianfar_ethtool.c
··· 45 45 46 46 #define GFAR_MAX_COAL_USECS 0xffff 47 47 #define GFAR_MAX_COAL_FRAMES 0xff 48 - static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, 49 - u64 *buf); 50 - static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf); 51 - static int gfar_gcoalesce(struct net_device *dev, 52 - struct ethtool_coalesce *cvals); 53 - static int gfar_scoalesce(struct net_device *dev, 54 - struct ethtool_coalesce *cvals); 55 - static void gfar_gringparam(struct net_device *dev, 56 - struct ethtool_ringparam *rvals); 57 - static int gfar_sringparam(struct net_device *dev, 58 - struct ethtool_ringparam *rvals); 59 - static void gfar_gdrvinfo(struct net_device *dev, 60 - struct ethtool_drvinfo *drvinfo); 61 48 62 49 static const char stat_gstrings[][ETH_GSTRING_LEN] = { 63 50 /* extra stats */