Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Faraday FTGMAC100 Gigabit Ethernet |
| 3 | * |
| 4 | * (C) Copyright 2009-2011 Faraday Technology |
| 5 | * Po-Yu Chuang <ratbert@faraday-tech.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
| 24 | #include <linux/dma-mapping.h> |
| 25 | #include <linux/etherdevice.h> |
| 26 | #include <linux/ethtool.h> |
Thomas Faber | 17f1bbc | 2012-01-18 13:45:44 +0000 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 28 | #include <linux/io.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/netdevice.h> |
Mark Brown | 3af887c | 2017-03-30 17:00:12 +0100 | [diff] [blame] | 31 | #include <linux/of.h> |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 32 | #include <linux/phy.h> |
| 33 | #include <linux/platform_device.h> |
Mark Brown | 3af887c | 2017-03-30 17:00:12 +0100 | [diff] [blame] | 34 | #include <linux/property.h> |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 35 | #include <net/ip.h> |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 36 | #include <net/ncsi.h> |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 37 | |
| 38 | #include "ftgmac100.h" |
| 39 | |
| 40 | #define DRV_NAME "ftgmac100" |
| 41 | #define DRV_VERSION "0.7" |
| 42 | |
| 43 | #define RX_QUEUE_ENTRIES 256 /* must be power of 2 */ |
| 44 | #define TX_QUEUE_ENTRIES 512 /* must be power of 2 */ |
| 45 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 46 | #define MAX_PKT_SIZE 1536 |
| 47 | #define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 48 | |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 49 | /* Min number of tx ring entries before stopping queue */ |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 50 | #define TX_THRESHOLD (MAX_SKB_FRAGS + 1) |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 51 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 52 | struct ftgmac100_descs { |
| 53 | struct ftgmac100_rxdes rxdes[RX_QUEUE_ENTRIES]; |
| 54 | struct ftgmac100_txdes txdes[TX_QUEUE_ENTRIES]; |
| 55 | }; |
| 56 | |
| 57 | struct ftgmac100 { |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 58 | /* Registers */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 59 | struct resource *res; |
| 60 | void __iomem *base; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 61 | |
| 62 | struct ftgmac100_descs *descs; |
| 63 | dma_addr_t descs_dma_addr; |
| 64 | |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 65 | /* Rx ring */ |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 66 | struct sk_buff *rx_skbs[RX_QUEUE_ENTRIES]; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 67 | unsigned int rx_pointer; |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 68 | u32 rxdes0_edorr_mask; |
| 69 | |
| 70 | /* Tx ring */ |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 71 | struct sk_buff *tx_skbs[TX_QUEUE_ENTRIES]; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 72 | unsigned int tx_clean_pointer; |
| 73 | unsigned int tx_pointer; |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 74 | u32 txdes0_edotr_mask; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 75 | |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 76 | /* Scratch page to use when rx skb alloc fails */ |
| 77 | void *rx_scratch; |
| 78 | dma_addr_t rx_scratch_dma; |
| 79 | |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 80 | /* Component structures */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 81 | struct net_device *netdev; |
| 82 | struct device *dev; |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 83 | struct ncsi_dev *ndev; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 84 | struct napi_struct napi; |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 85 | struct work_struct reset_task; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 86 | struct mii_bus *mii_bus; |
Andrew Jeffery | 7906a4d | 2016-09-22 08:34:59 +0930 | [diff] [blame] | 87 | |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 88 | /* Link management */ |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 89 | int cur_speed; |
| 90 | int cur_duplex; |
Benjamin Herrenschmidt | 831fb33 | 2017-04-05 12:28:43 +1000 | [diff] [blame] | 91 | bool use_ncsi; |
| 92 | |
| 93 | /* Misc */ |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 94 | bool need_mac_restart; |
Benjamin Herrenschmidt | 78d2854 | 2017-04-12 13:27:02 +1000 | [diff] [blame] | 95 | bool is_aspeed; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 98 | static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 99 | { |
| 100 | struct net_device *netdev = priv->netdev; |
| 101 | int i; |
| 102 | |
| 103 | /* NOTE: reset clears all registers */ |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 104 | iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR); |
| 105 | iowrite32(maccr | FTGMAC100_MACCR_SW_RST, |
| 106 | priv->base + FTGMAC100_OFFSET_MACCR); |
| 107 | for (i = 0; i < 50; i++) { |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 108 | unsigned int maccr; |
| 109 | |
| 110 | maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR); |
| 111 | if (!(maccr & FTGMAC100_MACCR_SW_RST)) |
| 112 | return 0; |
| 113 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 114 | udelay(1); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 117 | netdev_err(netdev, "Hardware reset failed\n"); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 118 | return -EIO; |
| 119 | } |
| 120 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 121 | static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv) |
| 122 | { |
| 123 | u32 maccr = 0; |
| 124 | |
| 125 | switch (priv->cur_speed) { |
| 126 | case SPEED_10: |
| 127 | case 0: /* no link */ |
| 128 | break; |
| 129 | |
| 130 | case SPEED_100: |
| 131 | maccr |= FTGMAC100_MACCR_FAST_MODE; |
| 132 | break; |
| 133 | |
| 134 | case SPEED_1000: |
| 135 | maccr |= FTGMAC100_MACCR_GIGA_MODE; |
| 136 | break; |
| 137 | default: |
| 138 | netdev_err(priv->netdev, "Unknown speed %d !\n", |
| 139 | priv->cur_speed); |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | /* (Re)initialize the queue pointers */ |
| 144 | priv->rx_pointer = 0; |
| 145 | priv->tx_clean_pointer = 0; |
| 146 | priv->tx_pointer = 0; |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 147 | |
| 148 | /* The doc says reset twice with 10us interval */ |
| 149 | if (ftgmac100_reset_mac(priv, maccr)) |
| 150 | return -EIO; |
| 151 | usleep_range(10, 1000); |
| 152 | return ftgmac100_reset_mac(priv, maccr); |
| 153 | } |
| 154 | |
Benjamin Herrenschmidt | f39c71b | 2017-04-12 13:27:05 +1000 | [diff] [blame] | 155 | static void ftgmac100_write_mac_addr(struct ftgmac100 *priv, const u8 *mac) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 156 | { |
| 157 | unsigned int maddr = mac[0] << 8 | mac[1]; |
| 158 | unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5]; |
| 159 | |
| 160 | iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR); |
| 161 | iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR); |
| 162 | } |
| 163 | |
Benjamin Herrenschmidt | ba1b123 | 2017-04-12 13:27:06 +1000 | [diff] [blame] | 164 | static void ftgmac100_initial_mac(struct ftgmac100 *priv) |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 165 | { |
| 166 | u8 mac[ETH_ALEN]; |
| 167 | unsigned int m; |
| 168 | unsigned int l; |
| 169 | void *addr; |
| 170 | |
| 171 | addr = device_get_mac_address(priv->dev, mac, ETH_ALEN); |
| 172 | if (addr) { |
| 173 | ether_addr_copy(priv->netdev->dev_addr, mac); |
| 174 | dev_info(priv->dev, "Read MAC address %pM from device tree\n", |
| 175 | mac); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR); |
| 180 | l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR); |
| 181 | |
| 182 | mac[0] = (m >> 8) & 0xff; |
| 183 | mac[1] = m & 0xff; |
| 184 | mac[2] = (l >> 24) & 0xff; |
| 185 | mac[3] = (l >> 16) & 0xff; |
| 186 | mac[4] = (l >> 8) & 0xff; |
| 187 | mac[5] = l & 0xff; |
| 188 | |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 189 | if (is_valid_ether_addr(mac)) { |
| 190 | ether_addr_copy(priv->netdev->dev_addr, mac); |
| 191 | dev_info(priv->dev, "Read MAC address %pM from chip\n", mac); |
| 192 | } else { |
| 193 | eth_hw_addr_random(priv->netdev); |
| 194 | dev_info(priv->dev, "Generated random MAC address %pM\n", |
| 195 | priv->netdev->dev_addr); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static int ftgmac100_set_mac_addr(struct net_device *dev, void *p) |
| 200 | { |
| 201 | int ret; |
| 202 | |
| 203 | ret = eth_prepare_mac_addr_change(dev, p); |
| 204 | if (ret < 0) |
| 205 | return ret; |
| 206 | |
| 207 | eth_commit_mac_addr_change(dev, p); |
Benjamin Herrenschmidt | f39c71b | 2017-04-12 13:27:05 +1000 | [diff] [blame] | 208 | ftgmac100_write_mac_addr(netdev_priv(dev), dev->dev_addr); |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 213 | static void ftgmac100_init_hw(struct ftgmac100 *priv) |
| 214 | { |
Benjamin Herrenschmidt | 3833dc6 | 2017-04-12 13:27:08 +1000 | [diff] [blame^] | 215 | u32 reg, rfifo_sz, tfifo_sz; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 216 | |
Benjamin Herrenschmidt | 3833dc6 | 2017-04-12 13:27:08 +1000 | [diff] [blame^] | 217 | /* Clear stale interrupts */ |
| 218 | reg = ioread32(priv->base + FTGMAC100_OFFSET_ISR); |
| 219 | iowrite32(reg, priv->base + FTGMAC100_OFFSET_ISR); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 220 | |
Benjamin Herrenschmidt | 8eecf7c | 2017-04-12 13:27:07 +1000 | [diff] [blame] | 221 | /* Setup RX ring buffer base */ |
| 222 | iowrite32(priv->descs_dma_addr + |
| 223 | offsetof(struct ftgmac100_descs, rxdes), |
| 224 | priv->base + FTGMAC100_OFFSET_RXR_BADR); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 225 | |
Benjamin Herrenschmidt | 8eecf7c | 2017-04-12 13:27:07 +1000 | [diff] [blame] | 226 | /* Setup TX ring buffer base */ |
| 227 | iowrite32(priv->descs_dma_addr + |
| 228 | offsetof(struct ftgmac100_descs, txdes), |
| 229 | priv->base + FTGMAC100_OFFSET_NPTXR_BADR); |
| 230 | |
| 231 | /* Configure RX buffer size */ |
| 232 | iowrite32(FTGMAC100_RBSR_SIZE(RX_BUF_SIZE), |
| 233 | priv->base + FTGMAC100_OFFSET_RBSR); |
| 234 | |
| 235 | /* Set RX descriptor autopoll */ |
| 236 | iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1), |
| 237 | priv->base + FTGMAC100_OFFSET_APTC); |
| 238 | |
| 239 | /* Write MAC address */ |
Benjamin Herrenschmidt | f39c71b | 2017-04-12 13:27:05 +1000 | [diff] [blame] | 240 | ftgmac100_write_mac_addr(priv, priv->netdev->dev_addr); |
Benjamin Herrenschmidt | 3833dc6 | 2017-04-12 13:27:08 +1000 | [diff] [blame^] | 241 | |
| 242 | /* Configure descriptor sizes and increase burst sizes according |
| 243 | * to values in Aspeed SDK. The FIFO arbitration is enabled and |
| 244 | * the thresholds set based on the recommended values in the |
| 245 | * AST2400 specification. |
| 246 | */ |
| 247 | iowrite32(FTGMAC100_DBLAC_RXDES_SIZE(2) | /* 2*8 bytes RX descs */ |
| 248 | FTGMAC100_DBLAC_TXDES_SIZE(2) | /* 2*8 bytes TX descs */ |
| 249 | FTGMAC100_DBLAC_RXBURST_SIZE(3) | /* 512 bytes max RX bursts */ |
| 250 | FTGMAC100_DBLAC_TXBURST_SIZE(3) | /* 512 bytes max TX bursts */ |
| 251 | FTGMAC100_DBLAC_RX_THR_EN | /* Enable fifo threshold arb */ |
| 252 | FTGMAC100_DBLAC_RXFIFO_HTHR(6) | /* 6/8 of FIFO high threshold */ |
| 253 | FTGMAC100_DBLAC_RXFIFO_LTHR(2), /* 2/8 of FIFO low threshold */ |
| 254 | priv->base + FTGMAC100_OFFSET_DBLAC); |
| 255 | |
| 256 | /* Interrupt mitigation configured for 1 interrupt/packet. HW interrupt |
| 257 | * mitigation doesn't seem to provide any benefit with NAPI so leave |
| 258 | * it at that. |
| 259 | */ |
| 260 | iowrite32(FTGMAC100_ITC_RXINT_THR(1) | |
| 261 | FTGMAC100_ITC_TXINT_THR(1), |
| 262 | priv->base + FTGMAC100_OFFSET_ITC); |
| 263 | |
| 264 | /* Configure FIFO sizes in the TPAFCR register */ |
| 265 | reg = ioread32(priv->base + FTGMAC100_OFFSET_FEAR); |
| 266 | rfifo_sz = reg & 0x00000007; |
| 267 | tfifo_sz = (reg >> 3) & 0x00000007; |
| 268 | reg = ioread32(priv->base + FTGMAC100_OFFSET_TPAFCR); |
| 269 | reg &= ~0x3f000000; |
| 270 | reg |= (tfifo_sz << 27); |
| 271 | reg |= (rfifo_sz << 24); |
| 272 | iowrite32(reg, priv->base + FTGMAC100_OFFSET_TPAFCR); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 275 | static void ftgmac100_start_hw(struct ftgmac100 *priv) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 276 | { |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 277 | u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 278 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 279 | /* Keep the original GMAC and FAST bits */ |
| 280 | maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 281 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 282 | /* Add all the main enable bits */ |
| 283 | maccr |= FTGMAC100_MACCR_TXDMA_EN | |
| 284 | FTGMAC100_MACCR_RXDMA_EN | |
| 285 | FTGMAC100_MACCR_TXMAC_EN | |
| 286 | FTGMAC100_MACCR_RXMAC_EN | |
| 287 | FTGMAC100_MACCR_CRC_APD | |
| 288 | FTGMAC100_MACCR_PHY_LINK_LEVEL | |
| 289 | FTGMAC100_MACCR_RX_RUNT | |
| 290 | FTGMAC100_MACCR_RX_BROADPKT; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 291 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 292 | /* Add other bits as needed */ |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 293 | if (priv->cur_duplex == DUPLEX_FULL) |
| 294 | maccr |= FTGMAC100_MACCR_FULLDUP; |
| 295 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 296 | /* Hit the HW */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 297 | iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR); |
| 298 | } |
| 299 | |
| 300 | static void ftgmac100_stop_hw(struct ftgmac100 *priv) |
| 301 | { |
| 302 | iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR); |
| 303 | } |
| 304 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 305 | static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry, |
| 306 | struct ftgmac100_rxdes *rxdes, gfp_t gfp) |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 307 | { |
| 308 | struct net_device *netdev = priv->netdev; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 309 | struct sk_buff *skb; |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 310 | dma_addr_t map; |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 311 | int err; |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 312 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 313 | skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE); |
| 314 | if (unlikely(!skb)) { |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 315 | if (net_ratelimit()) |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 316 | netdev_warn(netdev, "failed to allocate rx skb\n"); |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 317 | err = -ENOMEM; |
| 318 | map = priv->rx_scratch_dma; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 319 | } else { |
| 320 | map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE, |
| 321 | DMA_FROM_DEVICE); |
| 322 | if (unlikely(dma_mapping_error(priv->dev, map))) { |
| 323 | if (net_ratelimit()) |
| 324 | netdev_err(netdev, "failed to map rx page\n"); |
| 325 | dev_kfree_skb_any(skb); |
| 326 | map = priv->rx_scratch_dma; |
| 327 | skb = NULL; |
| 328 | err = -ENOMEM; |
| 329 | } |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 330 | } |
| 331 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 332 | /* Store skb */ |
| 333 | priv->rx_skbs[entry] = skb; |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 334 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 335 | /* Store DMA address into RX desc */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 336 | rxdes->rxdes3 = cpu_to_le32(map); |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 337 | |
| 338 | /* Ensure the above is ordered vs clearing the OWN bit */ |
| 339 | dma_wmb(); |
| 340 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 341 | /* Clean status (which resets own bit) */ |
| 342 | if (entry == (RX_QUEUE_ENTRIES - 1)) |
| 343 | rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask); |
| 344 | else |
| 345 | rxdes->rxdes0 = 0; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 346 | |
Benjamin Herrenschmidt | c06f73f | 2017-04-06 11:02:43 +1000 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 350 | static int ftgmac100_next_rx_pointer(int pointer) |
| 351 | { |
| 352 | return (pointer + 1) & (RX_QUEUE_ENTRIES - 1); |
| 353 | } |
| 354 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 355 | static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 356 | { |
| 357 | struct net_device *netdev = priv->netdev; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 358 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 359 | if (status & FTGMAC100_RXDES0_RX_ERR) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 360 | netdev->stats.rx_errors++; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 361 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 362 | if (status & FTGMAC100_RXDES0_CRC_ERR) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 363 | netdev->stats.rx_crc_errors++; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 364 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 365 | if (status & (FTGMAC100_RXDES0_FTL | |
| 366 | FTGMAC100_RXDES0_RUNT | |
| 367 | FTGMAC100_RXDES0_RX_ODD_NB)) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 368 | netdev->stats.rx_length_errors++; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 371 | static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed) |
| 372 | { |
| 373 | struct net_device *netdev = priv->netdev; |
| 374 | struct ftgmac100_rxdes *rxdes; |
| 375 | struct sk_buff *skb; |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 376 | unsigned int pointer, size; |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 377 | u32 status, csum_vlan; |
Benjamin Herrenschmidt | b1977bf | 2017-04-06 11:02:44 +1000 | [diff] [blame] | 378 | dma_addr_t map; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 379 | |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 380 | /* Grab next RX descriptor */ |
| 381 | pointer = priv->rx_pointer; |
| 382 | rxdes = &priv->descs->rxdes[pointer]; |
| 383 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 384 | /* Grab descriptor status */ |
| 385 | status = le32_to_cpu(rxdes->rxdes0); |
| 386 | |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 387 | /* Do we have a packet ? */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 388 | if (!(status & FTGMAC100_RXDES0_RXPKT_RDY)) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 389 | return false; |
| 390 | |
Benjamin Herrenschmidt | 027f426 | 2017-04-06 11:02:50 +1000 | [diff] [blame] | 391 | /* Order subsequent reads with the test for the ready bit */ |
| 392 | dma_rmb(); |
| 393 | |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 394 | /* We don't cope with fragmented RX packets */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 395 | if (unlikely(!(status & FTGMAC100_RXDES0_FRS) || |
| 396 | !(status & FTGMAC100_RXDES0_LRS))) |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 397 | goto drop; |
| 398 | |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 399 | /* Grab received size and csum vlan field in the descriptor */ |
| 400 | size = status & FTGMAC100_RXDES0_VDBC; |
| 401 | csum_vlan = le32_to_cpu(rxdes->rxdes1); |
| 402 | |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 403 | /* Any error (other than csum offload) flagged ? */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 404 | if (unlikely(status & RXDES0_ANY_ERROR)) { |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 405 | /* Correct for incorrect flagging of runt packets |
| 406 | * with vlan tags... Just accept a runt packet that |
| 407 | * has been flagged as vlan and whose size is at |
| 408 | * least 60 bytes. |
| 409 | */ |
| 410 | if ((status & FTGMAC100_RXDES0_RUNT) && |
| 411 | (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) && |
| 412 | (size >= 60)) |
| 413 | status &= ~FTGMAC100_RXDES0_RUNT; |
| 414 | |
| 415 | /* Any error still in there ? */ |
| 416 | if (status & RXDES0_ANY_ERROR) { |
| 417 | ftgmac100_rx_packet_error(priv, status); |
| 418 | goto drop; |
| 419 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 422 | /* If the packet had no skb (failed to allocate earlier) |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 423 | * then try to allocate one and skip |
| 424 | */ |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 425 | skb = priv->rx_skbs[pointer]; |
| 426 | if (!unlikely(skb)) { |
| 427 | ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC); |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 428 | goto drop; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 431 | if (unlikely(status & FTGMAC100_RXDES0_MULTICAST)) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 432 | netdev->stats.multicast++; |
| 433 | |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 434 | /* If the HW found checksum errors, bounce it to software. |
| 435 | * |
| 436 | * If we didn't, we need to see if the packet was recognized |
| 437 | * by HW as one of the supported checksummed protocols before |
| 438 | * we accept the HW test results. |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 439 | */ |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 440 | if (netdev->features & NETIF_F_RXCSUM) { |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 441 | u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR | |
| 442 | FTGMAC100_RXDES1_UDP_CHKSUM_ERR | |
| 443 | FTGMAC100_RXDES1_IP_CHKSUM_ERR; |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 444 | if ((csum_vlan & err_bits) || |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 445 | !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK)) |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 446 | skb->ip_summed = CHECKSUM_NONE; |
| 447 | else |
| 448 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 449 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 450 | |
Benjamin Herrenschmidt | d930655 | 2017-04-06 11:02:52 +1000 | [diff] [blame] | 451 | /* Transfer received size to skb */ |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 452 | skb_put(skb, size); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 453 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 454 | /* Tear down DMA mapping, do necessary cache management */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 455 | map = le32_to_cpu(rxdes->rxdes3); |
| 456 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 457 | #if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU) |
| 458 | /* When we don't have an iommu, we can save cycles by not |
| 459 | * invalidating the cache for the part of the packet that |
| 460 | * wasn't received. |
| 461 | */ |
| 462 | dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE); |
| 463 | #else |
| 464 | dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE); |
| 465 | #endif |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 466 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 467 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 468 | /* Resplenish rx ring */ |
| 469 | ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC); |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 470 | priv->rx_pointer = ftgmac100_next_rx_pointer(pointer); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 471 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 472 | skb->protocol = eth_type_trans(skb, netdev); |
| 473 | |
| 474 | netdev->stats.rx_packets++; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 475 | netdev->stats.rx_bytes += size; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 476 | |
| 477 | /* push packet to protocol stack */ |
Benjamin Herrenschmidt | 6720219 | 2017-04-06 11:02:46 +1000 | [diff] [blame] | 478 | if (skb->ip_summed == CHECKSUM_NONE) |
| 479 | netif_receive_skb(skb); |
| 480 | else |
| 481 | napi_gro_receive(&priv->napi, skb); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 482 | |
| 483 | (*processed)++; |
| 484 | return true; |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 485 | |
| 486 | drop: |
| 487 | /* Clean rxdes0 (which resets own bit) */ |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 488 | rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask); |
Benjamin Herrenschmidt | 01dd70b | 2017-04-06 11:02:48 +1000 | [diff] [blame] | 489 | priv->rx_pointer = ftgmac100_next_rx_pointer(pointer); |
| 490 | netdev->stats.rx_dropped++; |
| 491 | return true; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 494 | static u32 ftgmac100_base_tx_ctlstat(struct ftgmac100 *priv, |
| 495 | unsigned int index) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 496 | { |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 497 | if (index == (TX_QUEUE_ENTRIES - 1)) |
| 498 | return priv->txdes0_edotr_mask; |
| 499 | else |
| 500 | return 0; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 503 | static int ftgmac100_next_tx_pointer(int pointer) |
| 504 | { |
| 505 | return (pointer + 1) & (TX_QUEUE_ENTRIES - 1); |
| 506 | } |
| 507 | |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 508 | static u32 ftgmac100_tx_buf_avail(struct ftgmac100 *priv) |
| 509 | { |
| 510 | /* Returns the number of available slots in the TX queue |
| 511 | * |
| 512 | * This always leaves one free slot so we don't have to |
| 513 | * worry about empty vs. full, and this simplifies the |
| 514 | * test for ftgmac100_tx_buf_cleanable() below |
| 515 | */ |
| 516 | return (priv->tx_clean_pointer - priv->tx_pointer - 1) & |
| 517 | (TX_QUEUE_ENTRIES - 1); |
| 518 | } |
| 519 | |
| 520 | static bool ftgmac100_tx_buf_cleanable(struct ftgmac100 *priv) |
| 521 | { |
| 522 | return priv->tx_pointer != priv->tx_clean_pointer; |
| 523 | } |
| 524 | |
Benjamin Herrenschmidt | 42c2d19 | 2017-04-10 11:15:23 +1000 | [diff] [blame] | 525 | static void ftgmac100_free_tx_packet(struct ftgmac100 *priv, |
| 526 | unsigned int pointer, |
| 527 | struct sk_buff *skb, |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 528 | struct ftgmac100_txdes *txdes, |
| 529 | u32 ctl_stat) |
Benjamin Herrenschmidt | 42c2d19 | 2017-04-10 11:15:23 +1000 | [diff] [blame] | 530 | { |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 531 | dma_addr_t map = le32_to_cpu(txdes->txdes3); |
| 532 | size_t len; |
Benjamin Herrenschmidt | 42c2d19 | 2017-04-10 11:15:23 +1000 | [diff] [blame] | 533 | |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 534 | if (ctl_stat & FTGMAC100_TXDES0_FTS) { |
| 535 | len = skb_headlen(skb); |
| 536 | dma_unmap_single(priv->dev, map, len, DMA_TO_DEVICE); |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 537 | } else { |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 538 | len = FTGMAC100_TXDES0_TXBUF_SIZE(ctl_stat); |
| 539 | dma_unmap_page(priv->dev, map, len, DMA_TO_DEVICE); |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 540 | } |
Benjamin Herrenschmidt | 42c2d19 | 2017-04-10 11:15:23 +1000 | [diff] [blame] | 541 | |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 542 | /* Free SKB on last segment */ |
| 543 | if (ctl_stat & FTGMAC100_TXDES0_LTS) |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 544 | dev_kfree_skb(skb); |
Benjamin Herrenschmidt | 42c2d19 | 2017-04-10 11:15:23 +1000 | [diff] [blame] | 545 | priv->tx_skbs[pointer] = NULL; |
Benjamin Herrenschmidt | 42c2d19 | 2017-04-10 11:15:23 +1000 | [diff] [blame] | 546 | } |
| 547 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 548 | static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv) |
| 549 | { |
| 550 | struct net_device *netdev = priv->netdev; |
| 551 | struct ftgmac100_txdes *txdes; |
| 552 | struct sk_buff *skb; |
Benjamin Herrenschmidt | 42c2d19 | 2017-04-10 11:15:23 +1000 | [diff] [blame] | 553 | unsigned int pointer; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 554 | u32 ctl_stat; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 555 | |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 556 | pointer = priv->tx_clean_pointer; |
| 557 | txdes = &priv->descs->txdes[pointer]; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 558 | |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 559 | ctl_stat = le32_to_cpu(txdes->txdes0); |
| 560 | if (ctl_stat & FTGMAC100_TXDES0_TXDMA_OWN) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 561 | return false; |
| 562 | |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 563 | skb = priv->tx_skbs[pointer]; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 564 | netdev->stats.tx_packets++; |
| 565 | netdev->stats.tx_bytes += skb->len; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 566 | ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat); |
| 567 | txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 568 | |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 569 | priv->tx_clean_pointer = ftgmac100_next_tx_pointer(pointer); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 570 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 571 | return true; |
| 572 | } |
| 573 | |
| 574 | static void ftgmac100_tx_complete(struct ftgmac100 *priv) |
| 575 | { |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 576 | struct net_device *netdev = priv->netdev; |
| 577 | |
| 578 | /* Process all completed packets */ |
| 579 | while (ftgmac100_tx_buf_cleanable(priv) && |
| 580 | ftgmac100_tx_complete_packet(priv)) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 581 | ; |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 582 | |
| 583 | /* Restart queue if needed */ |
| 584 | smp_mb(); |
| 585 | if (unlikely(netif_queue_stopped(netdev) && |
| 586 | ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)) { |
| 587 | struct netdev_queue *txq; |
| 588 | |
| 589 | txq = netdev_get_tx_queue(netdev, 0); |
| 590 | __netif_tx_lock(txq, smp_processor_id()); |
| 591 | if (netif_queue_stopped(netdev) && |
| 592 | ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD) |
| 593 | netif_wake_queue(netdev); |
| 594 | __netif_tx_unlock(txq); |
| 595 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Benjamin Herrenschmidt | 05690d6 | 2017-04-12 13:27:01 +1000 | [diff] [blame] | 598 | static bool ftgmac100_prep_tx_csum(struct sk_buff *skb, u32 *csum_vlan) |
| 599 | { |
| 600 | if (skb->protocol == cpu_to_be16(ETH_P_IP)) { |
| 601 | u8 ip_proto = ip_hdr(skb)->protocol; |
| 602 | |
| 603 | *csum_vlan |= FTGMAC100_TXDES1_IP_CHKSUM; |
| 604 | switch(ip_proto) { |
| 605 | case IPPROTO_TCP: |
| 606 | *csum_vlan |= FTGMAC100_TXDES1_TCP_CHKSUM; |
| 607 | return true; |
| 608 | case IPPROTO_UDP: |
| 609 | *csum_vlan |= FTGMAC100_TXDES1_UDP_CHKSUM; |
| 610 | return true; |
| 611 | case IPPROTO_IP: |
| 612 | return true; |
| 613 | } |
| 614 | } |
| 615 | return skb_checksum_help(skb) == 0; |
| 616 | } |
| 617 | |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame] | 618 | static int ftgmac100_hard_start_xmit(struct sk_buff *skb, |
| 619 | struct net_device *netdev) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 620 | { |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame] | 621 | struct ftgmac100 *priv = netdev_priv(netdev); |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 622 | struct ftgmac100_txdes *txdes, *first; |
| 623 | unsigned int pointer, nfrags, len, i, j; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 624 | u32 f_ctl_stat, ctl_stat, csum_vlan; |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame] | 625 | dma_addr_t map; |
| 626 | |
Benjamin Herrenschmidt | 9b0f771 | 2017-04-10 11:15:19 +1000 | [diff] [blame] | 627 | /* The HW doesn't pad small frames */ |
| 628 | if (eth_skb_pad(skb)) { |
| 629 | netdev->stats.tx_dropped++; |
| 630 | return NETDEV_TX_OK; |
| 631 | } |
| 632 | |
| 633 | /* Reject oversize packets */ |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame] | 634 | if (unlikely(skb->len > MAX_PKT_SIZE)) { |
| 635 | if (net_ratelimit()) |
| 636 | netdev_dbg(netdev, "tx packet too big\n"); |
Benjamin Herrenschmidt | 3e427a3 | 2017-04-10 11:15:18 +1000 | [diff] [blame] | 637 | goto drop; |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame] | 638 | } |
| 639 | |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 640 | /* Do we have a limit on #fragments ? I yet have to get a reply |
| 641 | * from Aspeed. If there's one I haven't hit it. |
| 642 | */ |
| 643 | nfrags = skb_shinfo(skb)->nr_frags; |
| 644 | |
| 645 | /* Get header len */ |
| 646 | len = skb_headlen(skb); |
| 647 | |
| 648 | /* Map the packet head */ |
| 649 | map = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE); |
| 650 | if (dma_mapping_error(priv->dev, map)) { |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame] | 651 | if (net_ratelimit()) |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 652 | netdev_err(netdev, "map tx packet head failed\n"); |
Benjamin Herrenschmidt | 3e427a3 | 2017-04-10 11:15:18 +1000 | [diff] [blame] | 653 | goto drop; |
Benjamin Herrenschmidt | 43b25ee | 2017-04-10 11:15:17 +1000 | [diff] [blame] | 654 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 655 | |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 656 | /* Grab the next free tx descriptor */ |
| 657 | pointer = priv->tx_pointer; |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 658 | txdes = first = &priv->descs->txdes[pointer]; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 659 | |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 660 | /* Setup it up with the packet head. Don't write the head to the |
| 661 | * ring just yet |
| 662 | */ |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 663 | priv->tx_skbs[pointer] = skb; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 664 | f_ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer); |
| 665 | f_ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN; |
| 666 | f_ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len); |
| 667 | f_ctl_stat |= FTGMAC100_TXDES0_FTS; |
| 668 | if (nfrags == 0) |
| 669 | f_ctl_stat |= FTGMAC100_TXDES0_LTS; |
| 670 | txdes->txdes3 = cpu_to_le32(map); |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 671 | |
| 672 | /* Setup HW checksumming */ |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 673 | csum_vlan = 0; |
Benjamin Herrenschmidt | 05690d6 | 2017-04-12 13:27:01 +1000 | [diff] [blame] | 674 | if (skb->ip_summed == CHECKSUM_PARTIAL && |
| 675 | !ftgmac100_prep_tx_csum(skb, &csum_vlan)) |
| 676 | goto drop; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 677 | txdes->txdes1 = cpu_to_le32(csum_vlan); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 678 | |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 679 | /* Next descriptor */ |
| 680 | pointer = ftgmac100_next_tx_pointer(pointer); |
| 681 | |
| 682 | /* Add the fragments */ |
| 683 | for (i = 0; i < nfrags; i++) { |
| 684 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 685 | |
| 686 | len = frag->size; |
| 687 | |
| 688 | /* Map it */ |
| 689 | map = skb_frag_dma_map(priv->dev, frag, 0, len, |
| 690 | DMA_TO_DEVICE); |
| 691 | if (dma_mapping_error(priv->dev, map)) |
| 692 | goto dma_err; |
| 693 | |
| 694 | /* Setup descriptor */ |
| 695 | priv->tx_skbs[pointer] = skb; |
| 696 | txdes = &priv->descs->txdes[pointer]; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 697 | ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer); |
| 698 | ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN; |
| 699 | ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len); |
| 700 | if (i == (nfrags - 1)) |
| 701 | ctl_stat |= FTGMAC100_TXDES0_LTS; |
| 702 | txdes->txdes0 = cpu_to_le32(ctl_stat); |
| 703 | txdes->txdes1 = 0; |
| 704 | txdes->txdes3 = cpu_to_le32(map); |
| 705 | |
| 706 | /* Next one */ |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 707 | pointer = ftgmac100_next_tx_pointer(pointer); |
| 708 | } |
| 709 | |
Benjamin Herrenschmidt | 4a2712b | 2017-04-10 11:15:22 +1000 | [diff] [blame] | 710 | /* Order the previous packet and descriptor udpates |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 711 | * before setting the OWN bit on the first descriptor. |
Benjamin Herrenschmidt | 4a2712b | 2017-04-10 11:15:22 +1000 | [diff] [blame] | 712 | */ |
| 713 | dma_wmb(); |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 714 | first->txdes0 = cpu_to_le32(f_ctl_stat); |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 715 | |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 716 | /* Update next TX pointer */ |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 717 | priv->tx_pointer = pointer; |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 718 | |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 719 | /* If there isn't enough room for all the fragments of a new packet |
| 720 | * in the TX ring, stop the queue. The sequence below is race free |
| 721 | * vs. a concurrent restart in ftgmac100_poll() |
| 722 | */ |
| 723 | if (unlikely(ftgmac100_tx_buf_avail(priv) < TX_THRESHOLD)) { |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 724 | netif_stop_queue(netdev); |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 725 | /* Order the queue stop with the test below */ |
| 726 | smp_mb(); |
| 727 | if (ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD) |
| 728 | netif_wake_queue(netdev); |
| 729 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 730 | |
Benjamin Herrenschmidt | 8eecf7c | 2017-04-12 13:27:07 +1000 | [diff] [blame] | 731 | /* Poke transmitter to read the updated TX descriptors */ |
| 732 | iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 733 | |
| 734 | return NETDEV_TX_OK; |
Benjamin Herrenschmidt | 3e427a3 | 2017-04-10 11:15:18 +1000 | [diff] [blame] | 735 | |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 736 | dma_err: |
| 737 | if (net_ratelimit()) |
| 738 | netdev_err(netdev, "map tx fragment failed\n"); |
| 739 | |
| 740 | /* Free head */ |
| 741 | pointer = priv->tx_pointer; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 742 | ftgmac100_free_tx_packet(priv, pointer, skb, first, f_ctl_stat); |
| 743 | first->txdes0 = cpu_to_le32(f_ctl_stat & priv->txdes0_edotr_mask); |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 744 | |
| 745 | /* Then all fragments */ |
| 746 | for (j = 0; j < i; j++) { |
| 747 | pointer = ftgmac100_next_tx_pointer(pointer); |
| 748 | txdes = &priv->descs->txdes[pointer]; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 749 | ctl_stat = le32_to_cpu(txdes->txdes0); |
| 750 | ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat); |
| 751 | txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask); |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* This cannot be reached if we successfully mapped the |
| 755 | * last fragment, so we know ftgmac100_free_tx_packet() |
| 756 | * hasn't freed the skb yet. |
| 757 | */ |
Benjamin Herrenschmidt | 3e427a3 | 2017-04-10 11:15:18 +1000 | [diff] [blame] | 758 | drop: |
| 759 | /* Drop the packet */ |
| 760 | dev_kfree_skb_any(skb); |
| 761 | netdev->stats.tx_dropped++; |
| 762 | |
| 763 | return NETDEV_TX_OK; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 766 | static void ftgmac100_free_buffers(struct ftgmac100 *priv) |
| 767 | { |
| 768 | int i; |
| 769 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 770 | /* Free all RX buffers */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 771 | for (i = 0; i < RX_QUEUE_ENTRIES; i++) { |
| 772 | struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i]; |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 773 | struct sk_buff *skb = priv->rx_skbs[i]; |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 774 | dma_addr_t map = le32_to_cpu(rxdes->rxdes3); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 775 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 776 | if (!skb) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 777 | continue; |
| 778 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 779 | priv->rx_skbs[i] = NULL; |
| 780 | dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE); |
| 781 | dev_kfree_skb_any(skb); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 784 | /* Free all TX buffers */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 785 | for (i = 0; i < TX_QUEUE_ENTRIES; i++) { |
| 786 | struct ftgmac100_txdes *txdes = &priv->descs->txdes[i]; |
Benjamin Herrenschmidt | 8361731 | 2017-04-10 11:15:20 +1000 | [diff] [blame] | 787 | struct sk_buff *skb = priv->tx_skbs[i]; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 788 | |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 789 | if (!skb) |
| 790 | continue; |
| 791 | ftgmac100_free_tx_packet(priv, i, skb, txdes, |
| 792 | le32_to_cpu(txdes->txdes0)); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 793 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 796 | static void ftgmac100_free_rings(struct ftgmac100 *priv) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 797 | { |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 798 | /* Free descriptors */ |
| 799 | if (priv->descs) |
| 800 | dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs), |
| 801 | priv->descs, priv->descs_dma_addr); |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 802 | |
| 803 | /* Free scratch packet buffer */ |
| 804 | if (priv->rx_scratch) |
| 805 | dma_free_coherent(priv->dev, RX_BUF_SIZE, |
| 806 | priv->rx_scratch, priv->rx_scratch_dma); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 807 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 808 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 809 | static int ftgmac100_alloc_rings(struct ftgmac100 *priv) |
| 810 | { |
| 811 | /* Allocate descriptors */ |
Joe Perches | ede23fa8 | 2013-08-26 22:45:23 -0700 | [diff] [blame] | 812 | priv->descs = dma_zalloc_coherent(priv->dev, |
| 813 | sizeof(struct ftgmac100_descs), |
| 814 | &priv->descs_dma_addr, GFP_KERNEL); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 815 | if (!priv->descs) |
| 816 | return -ENOMEM; |
| 817 | |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 818 | /* Allocate scratch packet buffer */ |
| 819 | priv->rx_scratch = dma_alloc_coherent(priv->dev, |
| 820 | RX_BUF_SIZE, |
| 821 | &priv->rx_scratch_dma, |
| 822 | GFP_KERNEL); |
| 823 | if (!priv->rx_scratch) |
| 824 | return -ENOMEM; |
| 825 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static void ftgmac100_init_rings(struct ftgmac100 *priv) |
| 830 | { |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 831 | struct ftgmac100_rxdes *rxdes; |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 832 | struct ftgmac100_txdes *txdes; |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 833 | int i; |
| 834 | |
| 835 | /* Initialize RX ring */ |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 836 | for (i = 0; i < RX_QUEUE_ENTRIES; i++) { |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 837 | rxdes = &priv->descs->rxdes[i]; |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 838 | rxdes->rxdes0 = 0; |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 839 | rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma); |
Benjamin Herrenschmidt | d72e01a | 2017-04-06 11:02:45 +1000 | [diff] [blame] | 840 | } |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 841 | /* Mark the end of the ring */ |
| 842 | rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 843 | |
| 844 | /* Initialize TX ring */ |
Benjamin Herrenschmidt | 52c0cae | 2017-04-10 11:15:26 +1000 | [diff] [blame] | 845 | for (i = 0; i < TX_QUEUE_ENTRIES; i++) { |
| 846 | txdes = &priv->descs->txdes[i]; |
| 847 | txdes->txdes0 = 0; |
| 848 | } |
| 849 | txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv) |
| 853 | { |
| 854 | int i; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 855 | |
| 856 | for (i = 0; i < RX_QUEUE_ENTRIES; i++) { |
| 857 | struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i]; |
| 858 | |
Benjamin Herrenschmidt | 7b49cd1 | 2017-04-06 11:02:49 +1000 | [diff] [blame] | 859 | if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL)) |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 860 | return -ENOMEM; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 861 | } |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 862 | return 0; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 865 | static void ftgmac100_adjust_link(struct net_device *netdev) |
| 866 | { |
| 867 | struct ftgmac100 *priv = netdev_priv(netdev); |
Philippe Reynes | b3c40ad | 2016-05-16 01:35:13 +0200 | [diff] [blame] | 868 | struct phy_device *phydev = netdev->phydev; |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 869 | int new_speed; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 870 | |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 871 | /* We store "no link" as speed 0 */ |
| 872 | if (!phydev->link) |
| 873 | new_speed = 0; |
| 874 | else |
| 875 | new_speed = phydev->speed; |
| 876 | |
| 877 | if (phydev->speed == priv->cur_speed && |
| 878 | phydev->duplex == priv->cur_duplex) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 879 | return; |
| 880 | |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 881 | /* Print status if we have a link or we had one and just lost it, |
| 882 | * don't print otherwise. |
| 883 | */ |
| 884 | if (new_speed || priv->cur_speed) |
| 885 | phy_print_status(phydev); |
| 886 | |
| 887 | priv->cur_speed = new_speed; |
| 888 | priv->cur_duplex = phydev->duplex; |
| 889 | |
| 890 | /* Link is down, do nothing else */ |
| 891 | if (!new_speed) |
| 892 | return; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 893 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 894 | /* Disable all interrupts */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 895 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
| 896 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 897 | /* Reset the adapter asynchronously */ |
| 898 | schedule_work(&priv->reset_task); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static int ftgmac100_mii_probe(struct ftgmac100 *priv) |
| 902 | { |
| 903 | struct net_device *netdev = priv->netdev; |
Guenter Roeck | e574f39 | 2016-01-10 12:04:32 -0800 | [diff] [blame] | 904 | struct phy_device *phydev; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 905 | |
Guenter Roeck | e574f39 | 2016-01-10 12:04:32 -0800 | [diff] [blame] | 906 | phydev = phy_find_first(priv->mii_bus); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 907 | if (!phydev) { |
| 908 | netdev_info(netdev, "%s: no PHY found\n", netdev->name); |
| 909 | return -ENODEV; |
| 910 | } |
| 911 | |
Andrew Lunn | 84eff6d | 2016-01-06 20:11:10 +0100 | [diff] [blame] | 912 | phydev = phy_connect(netdev, phydev_name(phydev), |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 913 | &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 914 | |
| 915 | if (IS_ERR(phydev)) { |
| 916 | netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name); |
| 917 | return PTR_ERR(phydev); |
| 918 | } |
| 919 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 923 | static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum) |
| 924 | { |
| 925 | struct net_device *netdev = bus->priv; |
| 926 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 927 | unsigned int phycr; |
| 928 | int i; |
| 929 | |
| 930 | phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR); |
| 931 | |
| 932 | /* preserve MDC cycle threshold */ |
| 933 | phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK; |
| 934 | |
| 935 | phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) | |
| 936 | FTGMAC100_PHYCR_REGAD(regnum) | |
| 937 | FTGMAC100_PHYCR_MIIRD; |
| 938 | |
| 939 | iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR); |
| 940 | |
| 941 | for (i = 0; i < 10; i++) { |
| 942 | phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR); |
| 943 | |
| 944 | if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) { |
| 945 | int data; |
| 946 | |
| 947 | data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA); |
| 948 | return FTGMAC100_PHYDATA_MIIRDATA(data); |
| 949 | } |
| 950 | |
| 951 | udelay(100); |
| 952 | } |
| 953 | |
| 954 | netdev_err(netdev, "mdio read timed out\n"); |
| 955 | return -EIO; |
| 956 | } |
| 957 | |
| 958 | static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr, |
| 959 | int regnum, u16 value) |
| 960 | { |
| 961 | struct net_device *netdev = bus->priv; |
| 962 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 963 | unsigned int phycr; |
| 964 | int data; |
| 965 | int i; |
| 966 | |
| 967 | phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR); |
| 968 | |
| 969 | /* preserve MDC cycle threshold */ |
| 970 | phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK; |
| 971 | |
| 972 | phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) | |
| 973 | FTGMAC100_PHYCR_REGAD(regnum) | |
| 974 | FTGMAC100_PHYCR_MIIWR; |
| 975 | |
| 976 | data = FTGMAC100_PHYDATA_MIIWDATA(value); |
| 977 | |
| 978 | iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA); |
| 979 | iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR); |
| 980 | |
| 981 | for (i = 0; i < 10; i++) { |
| 982 | phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR); |
| 983 | |
| 984 | if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0) |
| 985 | return 0; |
| 986 | |
| 987 | udelay(100); |
| 988 | } |
| 989 | |
| 990 | netdev_err(netdev, "mdio write timed out\n"); |
| 991 | return -EIO; |
| 992 | } |
| 993 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 994 | static void ftgmac100_get_drvinfo(struct net_device *netdev, |
| 995 | struct ethtool_drvinfo *info) |
| 996 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 997 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 998 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 999 | strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info)); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1002 | static const struct ethtool_ops ftgmac100_ethtool_ops = { |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1003 | .get_drvinfo = ftgmac100_get_drvinfo, |
| 1004 | .get_link = ethtool_op_get_link, |
Philippe Reynes | fd24d72 | 2016-05-16 01:35:14 +0200 | [diff] [blame] | 1005 | .get_link_ksettings = phy_ethtool_get_link_ksettings, |
| 1006 | .set_link_ksettings = phy_ethtool_set_link_ksettings, |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1007 | }; |
| 1008 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1009 | static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id) |
| 1010 | { |
| 1011 | struct net_device *netdev = dev_id; |
| 1012 | struct ftgmac100 *priv = netdev_priv(netdev); |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1013 | unsigned int status, new_mask = FTGMAC100_INT_BAD; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1014 | |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1015 | /* Fetch and clear interrupt bits, process abnormal ones */ |
| 1016 | status = ioread32(priv->base + FTGMAC100_OFFSET_ISR); |
| 1017 | iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR); |
| 1018 | if (unlikely(status & FTGMAC100_INT_BAD)) { |
| 1019 | |
| 1020 | /* RX buffer unavailable */ |
| 1021 | if (status & FTGMAC100_INT_NO_RXBUF) |
| 1022 | netdev->stats.rx_over_errors++; |
| 1023 | |
| 1024 | /* received packet lost due to RX FIFO full */ |
| 1025 | if (status & FTGMAC100_INT_RPKT_LOST) |
| 1026 | netdev->stats.rx_fifo_errors++; |
| 1027 | |
| 1028 | /* sent packet lost due to excessive TX collision */ |
| 1029 | if (status & FTGMAC100_INT_XPKT_LOST) |
| 1030 | netdev->stats.tx_fifo_errors++; |
| 1031 | |
| 1032 | /* AHB error -> Reset the chip */ |
| 1033 | if (status & FTGMAC100_INT_AHB_ERR) { |
| 1034 | if (net_ratelimit()) |
| 1035 | netdev_warn(netdev, |
| 1036 | "AHB bus error ! Resetting chip.\n"); |
| 1037 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
| 1038 | schedule_work(&priv->reset_task); |
| 1039 | return IRQ_HANDLED; |
| 1040 | } |
| 1041 | |
| 1042 | /* We may need to restart the MAC after such errors, delay |
| 1043 | * this until after we have freed some Rx buffers though |
| 1044 | */ |
| 1045 | priv->need_mac_restart = true; |
| 1046 | |
| 1047 | /* Disable those errors until we restart */ |
| 1048 | new_mask &= ~status; |
| 1049 | } |
| 1050 | |
| 1051 | /* Only enable "bad" interrupts while NAPI is on */ |
| 1052 | iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER); |
| 1053 | |
| 1054 | /* Schedule NAPI bh */ |
| 1055 | napi_schedule_irqoff(&priv->napi); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1056 | |
| 1057 | return IRQ_HANDLED; |
| 1058 | } |
| 1059 | |
Benjamin Herrenschmidt | 4ca2415 | 2017-04-06 11:02:51 +1000 | [diff] [blame] | 1060 | static bool ftgmac100_check_rx(struct ftgmac100 *priv) |
| 1061 | { |
| 1062 | struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[priv->rx_pointer]; |
| 1063 | |
| 1064 | /* Do we have a packet ? */ |
| 1065 | return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY)); |
| 1066 | } |
| 1067 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1068 | static int ftgmac100_poll(struct napi_struct *napi, int budget) |
| 1069 | { |
| 1070 | struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi); |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 1071 | int work_done = 0; |
| 1072 | bool more; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1073 | |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 1074 | /* Handle TX completions */ |
| 1075 | if (ftgmac100_tx_buf_cleanable(priv)) |
| 1076 | ftgmac100_tx_complete(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1077 | |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 1078 | /* Handle RX packets */ |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1079 | do { |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 1080 | more = ftgmac100_rx_packet(priv, &work_done); |
| 1081 | } while (more && work_done < budget); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1082 | |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1083 | |
| 1084 | /* The interrupt is telling us to kick the MAC back to life |
| 1085 | * after an RX overflow |
| 1086 | */ |
| 1087 | if (unlikely(priv->need_mac_restart)) { |
| 1088 | ftgmac100_start_hw(priv); |
| 1089 | |
| 1090 | /* Re-enable "bad" interrupts */ |
| 1091 | iowrite32(FTGMAC100_INT_BAD, |
| 1092 | priv->base + FTGMAC100_OFFSET_IER); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 1095 | /* As long as we are waiting for transmit packets to be |
| 1096 | * completed we keep NAPI going |
| 1097 | */ |
| 1098 | if (ftgmac100_tx_buf_cleanable(priv)) |
| 1099 | work_done = budget; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1100 | |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 1101 | if (work_done < budget) { |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1102 | /* We are about to re-enable all interrupts. However |
| 1103 | * the HW has been latching RX/TX packet interrupts while |
| 1104 | * they were masked. So we clear them first, then we need |
| 1105 | * to re-check if there's something to process |
| 1106 | */ |
| 1107 | iowrite32(FTGMAC100_INT_RXTX, |
| 1108 | priv->base + FTGMAC100_OFFSET_ISR); |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 1109 | if (ftgmac100_check_rx(priv) || |
| 1110 | ftgmac100_tx_buf_cleanable(priv)) |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1111 | return budget; |
| 1112 | |
| 1113 | /* deschedule NAPI */ |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1114 | napi_complete(napi); |
| 1115 | |
| 1116 | /* enable all interrupts */ |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1117 | iowrite32(FTGMAC100_INT_ALL, |
Gavin Shan | fc6061c | 2016-07-19 11:54:25 +1000 | [diff] [blame] | 1118 | priv->base + FTGMAC100_OFFSET_IER); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Benjamin Herrenschmidt | 6ad3d7e | 2017-04-10 11:15:21 +1000 | [diff] [blame] | 1121 | return work_done; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1124 | static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err) |
| 1125 | { |
| 1126 | int err = 0; |
| 1127 | |
| 1128 | /* Re-init descriptors (adjust queue sizes) */ |
| 1129 | ftgmac100_init_rings(priv); |
| 1130 | |
| 1131 | /* Realloc rx descriptors */ |
| 1132 | err = ftgmac100_alloc_rx_buffers(priv); |
| 1133 | if (err && !ignore_alloc_err) |
| 1134 | return err; |
| 1135 | |
| 1136 | /* Reinit and restart HW */ |
| 1137 | ftgmac100_init_hw(priv); |
| 1138 | ftgmac100_start_hw(priv); |
| 1139 | |
| 1140 | /* Re-enable the device */ |
| 1141 | napi_enable(&priv->napi); |
| 1142 | netif_start_queue(priv->netdev); |
| 1143 | |
| 1144 | /* Enable all interrupts */ |
Benjamin Herrenschmidt | 10cbd64 | 2017-04-05 12:28:53 +1000 | [diff] [blame] | 1145 | iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER); |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1146 | |
| 1147 | return err; |
| 1148 | } |
| 1149 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1150 | static void ftgmac100_reset_task(struct work_struct *work) |
| 1151 | { |
| 1152 | struct ftgmac100 *priv = container_of(work, struct ftgmac100, |
| 1153 | reset_task); |
| 1154 | struct net_device *netdev = priv->netdev; |
| 1155 | int err; |
| 1156 | |
| 1157 | netdev_dbg(netdev, "Resetting NIC...\n"); |
| 1158 | |
| 1159 | /* Lock the world */ |
| 1160 | rtnl_lock(); |
| 1161 | if (netdev->phydev) |
| 1162 | mutex_lock(&netdev->phydev->lock); |
| 1163 | if (priv->mii_bus) |
| 1164 | mutex_lock(&priv->mii_bus->mdio_lock); |
| 1165 | |
| 1166 | |
| 1167 | /* Check if the interface is still up */ |
| 1168 | if (!netif_running(netdev)) |
| 1169 | goto bail; |
| 1170 | |
| 1171 | /* Stop the network stack */ |
| 1172 | netif_trans_update(netdev); |
| 1173 | napi_disable(&priv->napi); |
| 1174 | netif_tx_disable(netdev); |
| 1175 | |
| 1176 | /* Stop and reset the MAC */ |
| 1177 | ftgmac100_stop_hw(priv); |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 1178 | err = ftgmac100_reset_and_config_mac(priv); |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1179 | if (err) { |
| 1180 | /* Not much we can do ... it might come back... */ |
| 1181 | netdev_err(netdev, "attempting to continue...\n"); |
| 1182 | } |
| 1183 | |
| 1184 | /* Free all rx and tx buffers */ |
| 1185 | ftgmac100_free_buffers(priv); |
| 1186 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1187 | /* Setup everything again and restart chip */ |
| 1188 | ftgmac100_init_all(priv, true); |
| 1189 | |
| 1190 | netdev_dbg(netdev, "Reset done !\n"); |
| 1191 | bail: |
| 1192 | if (priv->mii_bus) |
| 1193 | mutex_unlock(&priv->mii_bus->mdio_lock); |
| 1194 | if (netdev->phydev) |
| 1195 | mutex_unlock(&netdev->phydev->lock); |
| 1196 | rtnl_unlock(); |
| 1197 | } |
| 1198 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1199 | static int ftgmac100_open(struct net_device *netdev) |
| 1200 | { |
| 1201 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1202 | int err; |
| 1203 | |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1204 | /* Allocate ring buffers */ |
| 1205 | err = ftgmac100_alloc_rings(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1206 | if (err) { |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1207 | netdev_err(netdev, "Failed to allocate descriptors\n"); |
| 1208 | return err; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Benjamin Herrenschmidt | 5176477 | 2017-04-05 12:28:45 +1000 | [diff] [blame] | 1211 | /* When using NC-SI we force the speed to 100Mbit/s full duplex, |
| 1212 | * |
| 1213 | * Otherwise we leave it set to 0 (no link), the link |
| 1214 | * message from the PHY layer will handle setting it up to |
| 1215 | * something else if needed. |
| 1216 | */ |
| 1217 | if (priv->use_ncsi) { |
| 1218 | priv->cur_duplex = DUPLEX_FULL; |
| 1219 | priv->cur_speed = SPEED_100; |
| 1220 | } else { |
| 1221 | priv->cur_duplex = 0; |
| 1222 | priv->cur_speed = 0; |
| 1223 | } |
| 1224 | |
Benjamin Herrenschmidt | 874b55b | 2017-04-05 12:28:51 +1000 | [diff] [blame] | 1225 | /* Reset the hardware */ |
| 1226 | err = ftgmac100_reset_and_config_mac(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1227 | if (err) |
| 1228 | goto err_hw; |
| 1229 | |
Benjamin Herrenschmidt | b8dbecf | 2017-04-05 12:28:47 +1000 | [diff] [blame] | 1230 | /* Initialize NAPI */ |
| 1231 | netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64); |
| 1232 | |
Benjamin Herrenschmidt | 81f1eca | 2017-04-05 12:28:48 +1000 | [diff] [blame] | 1233 | /* Grab our interrupt */ |
| 1234 | err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev); |
| 1235 | if (err) { |
| 1236 | netdev_err(netdev, "failed to request irq %d\n", netdev->irq); |
| 1237 | goto err_irq; |
| 1238 | } |
| 1239 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1240 | /* Start things up */ |
| 1241 | err = ftgmac100_init_all(priv, false); |
| 1242 | if (err) { |
| 1243 | netdev_err(netdev, "Failed to allocate packet buffers\n"); |
| 1244 | goto err_alloc; |
| 1245 | } |
Gavin Shan | 08c9c12 | 2016-09-22 08:35:01 +0930 | [diff] [blame] | 1246 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1247 | if (netdev->phydev) { |
| 1248 | /* If we have a PHY, start polling */ |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1249 | phy_start(netdev->phydev); |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1250 | } else if (priv->use_ncsi) { |
| 1251 | /* If using NC-SI, set our carrier on and start the stack */ |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1252 | netif_carrier_on(netdev); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1253 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1254 | /* Start the NCSI device */ |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1255 | err = ncsi_start_dev(priv->ndev); |
| 1256 | if (err) |
| 1257 | goto err_ncsi; |
| 1258 | } |
| 1259 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1260 | return 0; |
| 1261 | |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1262 | err_ncsi: |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1263 | napi_disable(&priv->napi); |
| 1264 | netif_stop_queue(netdev); |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1265 | err_alloc: |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1266 | ftgmac100_free_buffers(priv); |
Benjamin Herrenschmidt | da40d9d | 2017-04-05 12:28:49 +1000 | [diff] [blame] | 1267 | free_irq(netdev->irq, netdev); |
| 1268 | err_irq: |
| 1269 | netif_napi_del(&priv->napi); |
| 1270 | err_hw: |
| 1271 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1272 | ftgmac100_free_rings(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1273 | return err; |
| 1274 | } |
| 1275 | |
| 1276 | static int ftgmac100_stop(struct net_device *netdev) |
| 1277 | { |
| 1278 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1279 | |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1280 | /* Note about the reset task: We are called with the rtnl lock |
| 1281 | * held, so we are synchronized against the core of the reset |
| 1282 | * task. We must not try to synchronously cancel it otherwise |
| 1283 | * we can deadlock. But since it will test for netif_running() |
| 1284 | * which has already been cleared by the net core, we don't |
| 1285 | * anything special to do. |
| 1286 | */ |
| 1287 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1288 | /* disable all interrupts */ |
| 1289 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
| 1290 | |
| 1291 | netif_stop_queue(netdev); |
| 1292 | napi_disable(&priv->napi); |
Benjamin Herrenschmidt | b8dbecf | 2017-04-05 12:28:47 +1000 | [diff] [blame] | 1293 | netif_napi_del(&priv->napi); |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1294 | if (netdev->phydev) |
| 1295 | phy_stop(netdev->phydev); |
Gavin Shan | 2c15f25 | 2016-10-04 11:25:54 +1100 | [diff] [blame] | 1296 | else if (priv->use_ncsi) |
| 1297 | ncsi_stop_dev(priv->ndev); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1298 | |
| 1299 | ftgmac100_stop_hw(priv); |
Benjamin Herrenschmidt | 60b28a1 | 2017-04-05 12:28:41 +1000 | [diff] [blame] | 1300 | free_irq(netdev->irq, netdev); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1301 | ftgmac100_free_buffers(priv); |
Benjamin Herrenschmidt | 87d1875 | 2017-04-05 12:28:46 +1000 | [diff] [blame] | 1302 | ftgmac100_free_rings(priv); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1303 | |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1307 | /* optional */ |
| 1308 | static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) |
| 1309 | { |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1310 | if (!netdev->phydev) |
| 1311 | return -ENXIO; |
| 1312 | |
Philippe Reynes | b3c40ad | 2016-05-16 01:35:13 +0200 | [diff] [blame] | 1313 | return phy_mii_ioctl(netdev->phydev, ifr, cmd); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Benjamin Herrenschmidt | d3ca8fb | 2017-04-10 11:15:15 +1000 | [diff] [blame] | 1316 | static void ftgmac100_tx_timeout(struct net_device *netdev) |
| 1317 | { |
| 1318 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1319 | |
| 1320 | /* Disable all interrupts */ |
| 1321 | iowrite32(0, priv->base + FTGMAC100_OFFSET_IER); |
| 1322 | |
| 1323 | /* Do the reset outside of interrupt context */ |
| 1324 | schedule_work(&priv->reset_task); |
| 1325 | } |
| 1326 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1327 | static const struct net_device_ops ftgmac100_netdev_ops = { |
| 1328 | .ndo_open = ftgmac100_open, |
| 1329 | .ndo_stop = ftgmac100_stop, |
| 1330 | .ndo_start_xmit = ftgmac100_hard_start_xmit, |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 1331 | .ndo_set_mac_address = ftgmac100_set_mac_addr, |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1332 | .ndo_validate_addr = eth_validate_addr, |
| 1333 | .ndo_do_ioctl = ftgmac100_do_ioctl, |
Benjamin Herrenschmidt | d3ca8fb | 2017-04-10 11:15:15 +1000 | [diff] [blame] | 1334 | .ndo_tx_timeout = ftgmac100_tx_timeout, |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1335 | }; |
| 1336 | |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1337 | static int ftgmac100_setup_mdio(struct net_device *netdev) |
| 1338 | { |
| 1339 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1340 | struct platform_device *pdev = to_platform_device(priv->dev); |
| 1341 | int i, err = 0; |
Joel Stanley | e07dc63 | 2016-09-22 08:35:02 +0930 | [diff] [blame] | 1342 | u32 reg; |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1343 | |
| 1344 | /* initialize mdio bus */ |
| 1345 | priv->mii_bus = mdiobus_alloc(); |
| 1346 | if (!priv->mii_bus) |
| 1347 | return -EIO; |
| 1348 | |
Benjamin Herrenschmidt | 78d2854 | 2017-04-12 13:27:02 +1000 | [diff] [blame] | 1349 | if (priv->is_aspeed) { |
Joel Stanley | e07dc63 | 2016-09-22 08:35:02 +0930 | [diff] [blame] | 1350 | /* This driver supports the old MDIO interface */ |
| 1351 | reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR); |
| 1352 | reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE; |
| 1353 | iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR); |
| 1354 | }; |
| 1355 | |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1356 | priv->mii_bus->name = "ftgmac100_mdio"; |
| 1357 | snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d", |
| 1358 | pdev->name, pdev->id); |
| 1359 | priv->mii_bus->priv = priv->netdev; |
| 1360 | priv->mii_bus->read = ftgmac100_mdiobus_read; |
| 1361 | priv->mii_bus->write = ftgmac100_mdiobus_write; |
| 1362 | |
| 1363 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 1364 | priv->mii_bus->irq[i] = PHY_POLL; |
| 1365 | |
| 1366 | err = mdiobus_register(priv->mii_bus); |
| 1367 | if (err) { |
| 1368 | dev_err(priv->dev, "Cannot register MDIO bus!\n"); |
| 1369 | goto err_register_mdiobus; |
| 1370 | } |
| 1371 | |
| 1372 | err = ftgmac100_mii_probe(priv); |
| 1373 | if (err) { |
| 1374 | dev_err(priv->dev, "MII Probe failed!\n"); |
| 1375 | goto err_mii_probe; |
| 1376 | } |
| 1377 | |
| 1378 | return 0; |
| 1379 | |
| 1380 | err_mii_probe: |
| 1381 | mdiobus_unregister(priv->mii_bus); |
| 1382 | err_register_mdiobus: |
| 1383 | mdiobus_free(priv->mii_bus); |
| 1384 | return err; |
| 1385 | } |
| 1386 | |
| 1387 | static void ftgmac100_destroy_mdio(struct net_device *netdev) |
| 1388 | { |
| 1389 | struct ftgmac100 *priv = netdev_priv(netdev); |
| 1390 | |
| 1391 | if (!netdev->phydev) |
| 1392 | return; |
| 1393 | |
| 1394 | phy_disconnect(netdev->phydev); |
| 1395 | mdiobus_unregister(priv->mii_bus); |
| 1396 | mdiobus_free(priv->mii_bus); |
| 1397 | } |
| 1398 | |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1399 | static void ftgmac100_ncsi_handler(struct ncsi_dev *nd) |
| 1400 | { |
| 1401 | if (unlikely(nd->state != ncsi_dev_state_functional)) |
| 1402 | return; |
| 1403 | |
| 1404 | netdev_info(nd->dev, "NCSI interface %s\n", |
| 1405 | nd->link_up ? "up" : "down"); |
| 1406 | } |
| 1407 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1408 | static int ftgmac100_probe(struct platform_device *pdev) |
| 1409 | { |
| 1410 | struct resource *res; |
| 1411 | int irq; |
| 1412 | struct net_device *netdev; |
| 1413 | struct ftgmac100 *priv; |
Benjamin Herrenschmidt | 78d2854 | 2017-04-12 13:27:02 +1000 | [diff] [blame] | 1414 | struct device_node *np; |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1415 | int err = 0; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (!pdev) |
| 1418 | return -ENODEV; |
| 1419 | |
| 1420 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1421 | if (!res) |
| 1422 | return -ENXIO; |
| 1423 | |
| 1424 | irq = platform_get_irq(pdev, 0); |
| 1425 | if (irq < 0) |
| 1426 | return irq; |
| 1427 | |
| 1428 | /* setup net_device */ |
| 1429 | netdev = alloc_etherdev(sizeof(*priv)); |
| 1430 | if (!netdev) { |
| 1431 | err = -ENOMEM; |
| 1432 | goto err_alloc_etherdev; |
| 1433 | } |
| 1434 | |
| 1435 | SET_NETDEV_DEV(netdev, &pdev->dev); |
| 1436 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1437 | netdev->ethtool_ops = &ftgmac100_ethtool_ops; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1438 | netdev->netdev_ops = &ftgmac100_netdev_ops; |
Benjamin Herrenschmidt | d3ca8fb | 2017-04-10 11:15:15 +1000 | [diff] [blame] | 1439 | netdev->watchdog_timeo = 5 * HZ; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1440 | |
| 1441 | platform_set_drvdata(pdev, netdev); |
| 1442 | |
| 1443 | /* setup private data */ |
| 1444 | priv = netdev_priv(netdev); |
| 1445 | priv->netdev = netdev; |
| 1446 | priv->dev = &pdev->dev; |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1447 | INIT_WORK(&priv->reset_task, ftgmac100_reset_task); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1448 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1449 | /* map io memory */ |
| 1450 | priv->res = request_mem_region(res->start, resource_size(res), |
| 1451 | dev_name(&pdev->dev)); |
| 1452 | if (!priv->res) { |
| 1453 | dev_err(&pdev->dev, "Could not reserve memory region\n"); |
| 1454 | err = -ENOMEM; |
| 1455 | goto err_req_mem; |
| 1456 | } |
| 1457 | |
| 1458 | priv->base = ioremap(res->start, resource_size(res)); |
| 1459 | if (!priv->base) { |
| 1460 | dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n"); |
| 1461 | err = -EIO; |
| 1462 | goto err_ioremap; |
| 1463 | } |
| 1464 | |
Benjamin Herrenschmidt | 60b28a1 | 2017-04-05 12:28:41 +1000 | [diff] [blame] | 1465 | netdev->irq = irq; |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1466 | |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 1467 | /* MAC address from chip or random one */ |
Benjamin Herrenschmidt | ba1b123 | 2017-04-12 13:27:06 +1000 | [diff] [blame] | 1468 | ftgmac100_initial_mac(priv); |
Gavin Shan | 113ce10 | 2016-07-19 11:54:22 +1000 | [diff] [blame] | 1469 | |
Benjamin Herrenschmidt | 78d2854 | 2017-04-12 13:27:02 +1000 | [diff] [blame] | 1470 | np = pdev->dev.of_node; |
| 1471 | if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac") || |
| 1472 | of_device_is_compatible(np, "aspeed,ast2500-mac"))) { |
Joel Stanley | 2a0ab8eb | 2016-09-22 08:35:00 +0930 | [diff] [blame] | 1473 | priv->rxdes0_edorr_mask = BIT(30); |
| 1474 | priv->txdes0_edotr_mask = BIT(30); |
Benjamin Herrenschmidt | 78d2854 | 2017-04-12 13:27:02 +1000 | [diff] [blame] | 1475 | priv->is_aspeed = true; |
Joel Stanley | 2a0ab8eb | 2016-09-22 08:35:00 +0930 | [diff] [blame] | 1476 | } else { |
| 1477 | priv->rxdes0_edorr_mask = BIT(15); |
| 1478 | priv->txdes0_edotr_mask = BIT(15); |
| 1479 | } |
| 1480 | |
Benjamin Herrenschmidt | 78d2854 | 2017-04-12 13:27:02 +1000 | [diff] [blame] | 1481 | if (np && of_get_property(np, "use-ncsi", NULL)) { |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1482 | if (!IS_ENABLED(CONFIG_NET_NCSI)) { |
| 1483 | dev_err(&pdev->dev, "NCSI stack not enabled\n"); |
| 1484 | goto err_ncsi_dev; |
| 1485 | } |
| 1486 | |
| 1487 | dev_info(&pdev->dev, "Using NCSI interface\n"); |
| 1488 | priv->use_ncsi = true; |
| 1489 | priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler); |
| 1490 | if (!priv->ndev) |
| 1491 | goto err_ncsi_dev; |
| 1492 | } else { |
| 1493 | priv->use_ncsi = false; |
| 1494 | err = ftgmac100_setup_mdio(netdev); |
| 1495 | if (err) |
| 1496 | goto err_setup_mdio; |
| 1497 | } |
| 1498 | |
Benjamin Herrenschmidt | 6aff0bf | 2017-04-12 13:27:03 +1000 | [diff] [blame] | 1499 | /* Base feature set */ |
Benjamin Herrenschmidt | 8c3ed13 | 2017-04-12 13:27:04 +1000 | [diff] [blame] | 1500 | netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM | |
Benjamin Herrenschmidt | 6db7470 | 2017-04-10 11:15:25 +1000 | [diff] [blame] | 1501 | NETIF_F_GRO | NETIF_F_SG; |
Benjamin Herrenschmidt | 6aff0bf | 2017-04-12 13:27:03 +1000 | [diff] [blame] | 1502 | |
| 1503 | /* AST2400 doesn't have working HW checksum generation */ |
| 1504 | if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac"))) |
Benjamin Herrenschmidt | 8c3ed13 | 2017-04-12 13:27:04 +1000 | [diff] [blame] | 1505 | netdev->hw_features &= ~NETIF_F_HW_CSUM; |
Benjamin Herrenschmidt | 6aff0bf | 2017-04-12 13:27:03 +1000 | [diff] [blame] | 1506 | if (np && of_get_property(np, "no-hw-checksum", NULL)) |
Benjamin Herrenschmidt | 8c3ed13 | 2017-04-12 13:27:04 +1000 | [diff] [blame] | 1507 | netdev->hw_features &= ~(NETIF_F_HW_CSUM | NETIF_F_RXCSUM); |
| 1508 | netdev->features |= netdev->hw_features; |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1509 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1510 | /* register network device */ |
| 1511 | err = register_netdev(netdev); |
| 1512 | if (err) { |
| 1513 | dev_err(&pdev->dev, "Failed to register netdev\n"); |
| 1514 | goto err_register_netdev; |
| 1515 | } |
| 1516 | |
Benjamin Herrenschmidt | 60b28a1 | 2017-04-05 12:28:41 +1000 | [diff] [blame] | 1517 | netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1518 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1519 | return 0; |
| 1520 | |
Gavin Shan | bd466c3 | 2016-07-19 11:54:23 +1000 | [diff] [blame] | 1521 | err_ncsi_dev: |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1522 | err_register_netdev: |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1523 | ftgmac100_destroy_mdio(netdev); |
| 1524 | err_setup_mdio: |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1525 | iounmap(priv->base); |
| 1526 | err_ioremap: |
| 1527 | release_resource(priv->res); |
| 1528 | err_req_mem: |
| 1529 | netif_napi_del(&priv->napi); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1530 | free_netdev(netdev); |
| 1531 | err_alloc_etherdev: |
| 1532 | return err; |
| 1533 | } |
| 1534 | |
Dmitry Torokhov | be12502 | 2017-03-01 17:24:47 -0800 | [diff] [blame] | 1535 | static int ftgmac100_remove(struct platform_device *pdev) |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1536 | { |
| 1537 | struct net_device *netdev; |
| 1538 | struct ftgmac100 *priv; |
| 1539 | |
| 1540 | netdev = platform_get_drvdata(pdev); |
| 1541 | priv = netdev_priv(netdev); |
| 1542 | |
| 1543 | unregister_netdev(netdev); |
Benjamin Herrenschmidt | 855944c | 2017-04-05 12:28:50 +1000 | [diff] [blame] | 1544 | |
| 1545 | /* There's a small chance the reset task will have been re-queued, |
| 1546 | * during stop, make sure it's gone before we free the structure. |
| 1547 | */ |
| 1548 | cancel_work_sync(&priv->reset_task); |
| 1549 | |
Gavin Shan | eb41818 | 2016-07-19 11:54:21 +1000 | [diff] [blame] | 1550 | ftgmac100_destroy_mdio(netdev); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1551 | |
| 1552 | iounmap(priv->base); |
| 1553 | release_resource(priv->res); |
| 1554 | |
| 1555 | netif_napi_del(&priv->napi); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1556 | free_netdev(netdev); |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
Gavin Shan | bb168e2 | 2016-07-19 11:54:24 +1000 | [diff] [blame] | 1560 | static const struct of_device_id ftgmac100_of_match[] = { |
| 1561 | { .compatible = "faraday,ftgmac100" }, |
| 1562 | { } |
| 1563 | }; |
| 1564 | MODULE_DEVICE_TABLE(of, ftgmac100_of_match); |
| 1565 | |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1566 | static struct platform_driver ftgmac100_driver = { |
Gavin Shan | bb168e2 | 2016-07-19 11:54:24 +1000 | [diff] [blame] | 1567 | .probe = ftgmac100_probe, |
Dmitry Torokhov | be12502 | 2017-03-01 17:24:47 -0800 | [diff] [blame] | 1568 | .remove = ftgmac100_remove, |
Gavin Shan | bb168e2 | 2016-07-19 11:54:24 +1000 | [diff] [blame] | 1569 | .driver = { |
| 1570 | .name = DRV_NAME, |
| 1571 | .of_match_table = ftgmac100_of_match, |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1572 | }, |
| 1573 | }; |
Sachin Kamat | 14f645d | 2013-03-18 01:50:48 +0000 | [diff] [blame] | 1574 | module_platform_driver(ftgmac100_driver); |
Po-Yu Chuang | 69785b7 | 2011-06-08 23:32:48 +0000 | [diff] [blame] | 1575 | |
| 1576 | MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>"); |
| 1577 | MODULE_DESCRIPTION("FTGMAC100 driver"); |
| 1578 | MODULE_LICENSE("GPL"); |