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