blob: 259790b7126e3b7863b300aa8900e083c0dce0ce [file] [log] [blame]
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001/*
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 Faber17f1bbc2012-01-18 13:45:44 +000027#include <linux/interrupt.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000028#include <linux/io.h>
29#include <linux/module.h>
30#include <linux/netdevice.h>
Mark Brown3af887c2017-03-30 17:00:12 +010031#include <linux/of.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000032#include <linux/phy.h>
33#include <linux/platform_device.h>
Mark Brown3af887c2017-03-30 17:00:12 +010034#include <linux/property.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000035#include <net/ip.h>
Gavin Shanbd466c32016-07-19 11:54:23 +100036#include <net/ncsi.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000037
38#include "ftgmac100.h"
39
40#define DRV_NAME "ftgmac100"
41#define DRV_VERSION "0.7"
42
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +100043/* Arbitrary values, I am not sure the HW has limits */
44#define MAX_RX_QUEUE_ENTRIES 1024
45#define MAX_TX_QUEUE_ENTRIES 1024
46#define MIN_RX_QUEUE_ENTRIES 32
47#define MIN_TX_QUEUE_ENTRIES 32
48
49/* Defaults */
50#define DEF_RX_QUEUE_ENTRIES 256
51#define DEF_TX_QUEUE_ENTRIES 512
Po-Yu Chuang69785b72011-06-08 23:32:48 +000052
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +100053#define MAX_PKT_SIZE 1536
54#define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000055
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +100056/* Min number of tx ring entries before stopping queue */
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +100057#define TX_THRESHOLD (MAX_SKB_FRAGS + 1)
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +100058
Po-Yu Chuang69785b72011-06-08 23:32:48 +000059struct ftgmac100 {
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100060 /* Registers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000061 struct resource *res;
62 void __iomem *base;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000063
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100064 /* Rx ring */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +100065 unsigned int rx_q_entries;
66 struct ftgmac100_rxdes *rxdes;
67 dma_addr_t rxdes_dma;
68 struct sk_buff **rx_skbs;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000069 unsigned int rx_pointer;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100070 u32 rxdes0_edorr_mask;
71
72 /* Tx ring */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +100073 unsigned int tx_q_entries;
74 struct ftgmac100_txdes *txdes;
75 dma_addr_t txdes_dma;
76 struct sk_buff **tx_skbs;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000077 unsigned int tx_clean_pointer;
78 unsigned int tx_pointer;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100079 u32 txdes0_edotr_mask;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000080
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +100081 /* Used to signal the reset task of ring change request */
82 unsigned int new_rx_q_entries;
83 unsigned int new_tx_q_entries;
84
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +100085 /* Scratch page to use when rx skb alloc fails */
86 void *rx_scratch;
87 dma_addr_t rx_scratch_dma;
88
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100089 /* Component structures */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000090 struct net_device *netdev;
91 struct device *dev;
Gavin Shanbd466c32016-07-19 11:54:23 +100092 struct ncsi_dev *ndev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000093 struct napi_struct napi;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +100094 struct work_struct reset_task;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000095 struct mii_bus *mii_bus;
Andrew Jeffery7906a4d2016-09-22 08:34:59 +093096
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100097 /* Link management */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +100098 int cur_speed;
99 int cur_duplex;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +1000100 bool use_ncsi;
101
102 /* Misc */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +1000103 bool need_mac_restart;
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +1000104 bool is_aspeed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000105};
106
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000107static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000108{
109 struct net_device *netdev = priv->netdev;
110 int i;
111
112 /* NOTE: reset clears all registers */
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000113 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
114 iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
115 priv->base + FTGMAC100_OFFSET_MACCR);
116 for (i = 0; i < 50; i++) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000117 unsigned int maccr;
118
119 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
120 if (!(maccr & FTGMAC100_MACCR_SW_RST))
121 return 0;
122
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000123 udelay(1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000124 }
125
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000126 netdev_err(netdev, "Hardware reset failed\n");
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000127 return -EIO;
128}
129
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000130static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
131{
132 u32 maccr = 0;
133
134 switch (priv->cur_speed) {
135 case SPEED_10:
136 case 0: /* no link */
137 break;
138
139 case SPEED_100:
140 maccr |= FTGMAC100_MACCR_FAST_MODE;
141 break;
142
143 case SPEED_1000:
144 maccr |= FTGMAC100_MACCR_GIGA_MODE;
145 break;
146 default:
147 netdev_err(priv->netdev, "Unknown speed %d !\n",
148 priv->cur_speed);
149 break;
150 }
151
152 /* (Re)initialize the queue pointers */
153 priv->rx_pointer = 0;
154 priv->tx_clean_pointer = 0;
155 priv->tx_pointer = 0;
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000156
157 /* The doc says reset twice with 10us interval */
158 if (ftgmac100_reset_mac(priv, maccr))
159 return -EIO;
160 usleep_range(10, 1000);
161 return ftgmac100_reset_mac(priv, maccr);
162}
163
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000164static void ftgmac100_write_mac_addr(struct ftgmac100 *priv, const u8 *mac)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000165{
166 unsigned int maddr = mac[0] << 8 | mac[1];
167 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
168
169 iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
170 iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
171}
172
Benjamin Herrenschmidtba1b1232017-04-12 13:27:06 +1000173static void ftgmac100_initial_mac(struct ftgmac100 *priv)
Gavin Shan113ce102016-07-19 11:54:22 +1000174{
175 u8 mac[ETH_ALEN];
176 unsigned int m;
177 unsigned int l;
178 void *addr;
179
180 addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
181 if (addr) {
182 ether_addr_copy(priv->netdev->dev_addr, mac);
183 dev_info(priv->dev, "Read MAC address %pM from device tree\n",
184 mac);
185 return;
186 }
187
188 m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
189 l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
190
191 mac[0] = (m >> 8) & 0xff;
192 mac[1] = m & 0xff;
193 mac[2] = (l >> 24) & 0xff;
194 mac[3] = (l >> 16) & 0xff;
195 mac[4] = (l >> 8) & 0xff;
196 mac[5] = l & 0xff;
197
Gavin Shan113ce102016-07-19 11:54:22 +1000198 if (is_valid_ether_addr(mac)) {
199 ether_addr_copy(priv->netdev->dev_addr, mac);
200 dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
201 } else {
202 eth_hw_addr_random(priv->netdev);
203 dev_info(priv->dev, "Generated random MAC address %pM\n",
204 priv->netdev->dev_addr);
205 }
206}
207
208static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
209{
210 int ret;
211
212 ret = eth_prepare_mac_addr_change(dev, p);
213 if (ret < 0)
214 return ret;
215
216 eth_commit_mac_addr_change(dev, p);
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000217 ftgmac100_write_mac_addr(netdev_priv(dev), dev->dev_addr);
Gavin Shan113ce102016-07-19 11:54:22 +1000218
219 return 0;
220}
221
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000222static void ftgmac100_init_hw(struct ftgmac100 *priv)
223{
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000224 u32 reg, rfifo_sz, tfifo_sz;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000225
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000226 /* Clear stale interrupts */
227 reg = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
228 iowrite32(reg, priv->base + FTGMAC100_OFFSET_ISR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000229
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000230 /* Setup RX ring buffer base */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000231 iowrite32(priv->rxdes_dma, priv->base + FTGMAC100_OFFSET_RXR_BADR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000232
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000233 /* Setup TX ring buffer base */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000234 iowrite32(priv->txdes_dma, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000235
236 /* Configure RX buffer size */
237 iowrite32(FTGMAC100_RBSR_SIZE(RX_BUF_SIZE),
238 priv->base + FTGMAC100_OFFSET_RBSR);
239
240 /* Set RX descriptor autopoll */
241 iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1),
242 priv->base + FTGMAC100_OFFSET_APTC);
243
244 /* Write MAC address */
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000245 ftgmac100_write_mac_addr(priv, priv->netdev->dev_addr);
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000246
247 /* Configure descriptor sizes and increase burst sizes according
248 * to values in Aspeed SDK. The FIFO arbitration is enabled and
249 * the thresholds set based on the recommended values in the
250 * AST2400 specification.
251 */
252 iowrite32(FTGMAC100_DBLAC_RXDES_SIZE(2) | /* 2*8 bytes RX descs */
253 FTGMAC100_DBLAC_TXDES_SIZE(2) | /* 2*8 bytes TX descs */
254 FTGMAC100_DBLAC_RXBURST_SIZE(3) | /* 512 bytes max RX bursts */
255 FTGMAC100_DBLAC_TXBURST_SIZE(3) | /* 512 bytes max TX bursts */
256 FTGMAC100_DBLAC_RX_THR_EN | /* Enable fifo threshold arb */
257 FTGMAC100_DBLAC_RXFIFO_HTHR(6) | /* 6/8 of FIFO high threshold */
258 FTGMAC100_DBLAC_RXFIFO_LTHR(2), /* 2/8 of FIFO low threshold */
259 priv->base + FTGMAC100_OFFSET_DBLAC);
260
261 /* Interrupt mitigation configured for 1 interrupt/packet. HW interrupt
262 * mitigation doesn't seem to provide any benefit with NAPI so leave
263 * it at that.
264 */
265 iowrite32(FTGMAC100_ITC_RXINT_THR(1) |
266 FTGMAC100_ITC_TXINT_THR(1),
267 priv->base + FTGMAC100_OFFSET_ITC);
268
269 /* Configure FIFO sizes in the TPAFCR register */
270 reg = ioread32(priv->base + FTGMAC100_OFFSET_FEAR);
271 rfifo_sz = reg & 0x00000007;
272 tfifo_sz = (reg >> 3) & 0x00000007;
273 reg = ioread32(priv->base + FTGMAC100_OFFSET_TPAFCR);
274 reg &= ~0x3f000000;
275 reg |= (tfifo_sz << 27);
276 reg |= (rfifo_sz << 24);
277 iowrite32(reg, priv->base + FTGMAC100_OFFSET_TPAFCR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000278}
279
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000280static void ftgmac100_start_hw(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000281{
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000282 u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000283
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000284 /* Keep the original GMAC and FAST bits */
285 maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000286
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000287 /* Add all the main enable bits */
288 maccr |= FTGMAC100_MACCR_TXDMA_EN |
289 FTGMAC100_MACCR_RXDMA_EN |
290 FTGMAC100_MACCR_TXMAC_EN |
291 FTGMAC100_MACCR_RXMAC_EN |
292 FTGMAC100_MACCR_CRC_APD |
293 FTGMAC100_MACCR_PHY_LINK_LEVEL |
294 FTGMAC100_MACCR_RX_RUNT |
295 FTGMAC100_MACCR_RX_BROADPKT;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000296
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000297 /* Add other bits as needed */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000298 if (priv->cur_duplex == DUPLEX_FULL)
299 maccr |= FTGMAC100_MACCR_FULLDUP;
300
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000301 /* Hit the HW */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000302 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
303}
304
305static void ftgmac100_stop_hw(struct ftgmac100 *priv)
306{
307 iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
308}
309
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000310static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry,
311 struct ftgmac100_rxdes *rxdes, gfp_t gfp)
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000312{
313 struct net_device *netdev = priv->netdev;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000314 struct sk_buff *skb;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000315 dma_addr_t map;
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000316 int err;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000317
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000318 skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE);
319 if (unlikely(!skb)) {
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000320 if (net_ratelimit())
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000321 netdev_warn(netdev, "failed to allocate rx skb\n");
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000322 err = -ENOMEM;
323 map = priv->rx_scratch_dma;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000324 } else {
325 map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE,
326 DMA_FROM_DEVICE);
327 if (unlikely(dma_mapping_error(priv->dev, map))) {
328 if (net_ratelimit())
329 netdev_err(netdev, "failed to map rx page\n");
330 dev_kfree_skb_any(skb);
331 map = priv->rx_scratch_dma;
332 skb = NULL;
333 err = -ENOMEM;
334 }
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000335 }
336
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000337 /* Store skb */
338 priv->rx_skbs[entry] = skb;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000339
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000340 /* Store DMA address into RX desc */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000341 rxdes->rxdes3 = cpu_to_le32(map);
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000342
343 /* Ensure the above is ordered vs clearing the OWN bit */
344 dma_wmb();
345
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000346 /* Clean status (which resets own bit) */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000347 if (entry == (priv->rx_q_entries - 1))
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000348 rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask);
349 else
350 rxdes->rxdes0 = 0;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000351
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000352 return 0;
353}
354
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000355static unsigned int ftgmac100_next_rx_pointer(struct ftgmac100 *priv,
356 unsigned int pointer)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000357{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000358 return (pointer + 1) & (priv->rx_q_entries - 1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000359}
360
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000361static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000362{
363 struct net_device *netdev = priv->netdev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000364
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000365 if (status & FTGMAC100_RXDES0_RX_ERR)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000366 netdev->stats.rx_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000367
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000368 if (status & FTGMAC100_RXDES0_CRC_ERR)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000369 netdev->stats.rx_crc_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000370
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000371 if (status & (FTGMAC100_RXDES0_FTL |
372 FTGMAC100_RXDES0_RUNT |
373 FTGMAC100_RXDES0_RX_ODD_NB))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000374 netdev->stats.rx_length_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000375}
376
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000377static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
378{
379 struct net_device *netdev = priv->netdev;
380 struct ftgmac100_rxdes *rxdes;
381 struct sk_buff *skb;
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000382 unsigned int pointer, size;
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000383 u32 status, csum_vlan;
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000384 dma_addr_t map;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000385
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000386 /* Grab next RX descriptor */
387 pointer = priv->rx_pointer;
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000388 rxdes = &priv->rxdes[pointer];
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000389
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000390 /* Grab descriptor status */
391 status = le32_to_cpu(rxdes->rxdes0);
392
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000393 /* Do we have a packet ? */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000394 if (!(status & FTGMAC100_RXDES0_RXPKT_RDY))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000395 return false;
396
Benjamin Herrenschmidt027f4262017-04-06 11:02:50 +1000397 /* Order subsequent reads with the test for the ready bit */
398 dma_rmb();
399
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000400 /* We don't cope with fragmented RX packets */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000401 if (unlikely(!(status & FTGMAC100_RXDES0_FRS) ||
402 !(status & FTGMAC100_RXDES0_LRS)))
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000403 goto drop;
404
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000405 /* Grab received size and csum vlan field in the descriptor */
406 size = status & FTGMAC100_RXDES0_VDBC;
407 csum_vlan = le32_to_cpu(rxdes->rxdes1);
408
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000409 /* Any error (other than csum offload) flagged ? */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000410 if (unlikely(status & RXDES0_ANY_ERROR)) {
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000411 /* Correct for incorrect flagging of runt packets
412 * with vlan tags... Just accept a runt packet that
413 * has been flagged as vlan and whose size is at
414 * least 60 bytes.
415 */
416 if ((status & FTGMAC100_RXDES0_RUNT) &&
417 (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) &&
418 (size >= 60))
419 status &= ~FTGMAC100_RXDES0_RUNT;
420
421 /* Any error still in there ? */
422 if (status & RXDES0_ANY_ERROR) {
423 ftgmac100_rx_packet_error(priv, status);
424 goto drop;
425 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000426 }
427
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000428 /* If the packet had no skb (failed to allocate earlier)
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000429 * then try to allocate one and skip
430 */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000431 skb = priv->rx_skbs[pointer];
432 if (!unlikely(skb)) {
433 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000434 goto drop;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000435 }
436
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000437 if (unlikely(status & FTGMAC100_RXDES0_MULTICAST))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000438 netdev->stats.multicast++;
439
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000440 /* If the HW found checksum errors, bounce it to software.
441 *
442 * If we didn't, we need to see if the packet was recognized
443 * by HW as one of the supported checksummed protocols before
444 * we accept the HW test results.
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000445 */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000446 if (netdev->features & NETIF_F_RXCSUM) {
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000447 u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
448 FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
449 FTGMAC100_RXDES1_IP_CHKSUM_ERR;
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000450 if ((csum_vlan & err_bits) ||
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000451 !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK))
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000452 skb->ip_summed = CHECKSUM_NONE;
453 else
454 skb->ip_summed = CHECKSUM_UNNECESSARY;
455 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000456
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000457 /* Transfer received size to skb */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000458 skb_put(skb, size);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000459
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000460 /* Tear down DMA mapping, do necessary cache management */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000461 map = le32_to_cpu(rxdes->rxdes3);
462
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000463#if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU)
464 /* When we don't have an iommu, we can save cycles by not
465 * invalidating the cache for the part of the packet that
466 * wasn't received.
467 */
468 dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE);
469#else
470 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
471#endif
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000472
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000473
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000474 /* Resplenish rx ring */
475 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000476 priv->rx_pointer = ftgmac100_next_rx_pointer(priv, pointer);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000477
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000478 skb->protocol = eth_type_trans(skb, netdev);
479
480 netdev->stats.rx_packets++;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000481 netdev->stats.rx_bytes += size;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000482
483 /* push packet to protocol stack */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000484 if (skb->ip_summed == CHECKSUM_NONE)
485 netif_receive_skb(skb);
486 else
487 napi_gro_receive(&priv->napi, skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000488
489 (*processed)++;
490 return true;
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000491
492 drop:
493 /* Clean rxdes0 (which resets own bit) */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000494 rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask);
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000495 priv->rx_pointer = ftgmac100_next_rx_pointer(priv, pointer);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000496 netdev->stats.rx_dropped++;
497 return true;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000498}
499
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000500static u32 ftgmac100_base_tx_ctlstat(struct ftgmac100 *priv,
501 unsigned int index)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000502{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000503 if (index == (priv->tx_q_entries - 1))
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000504 return priv->txdes0_edotr_mask;
505 else
506 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000507}
508
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000509static unsigned int ftgmac100_next_tx_pointer(struct ftgmac100 *priv,
510 unsigned int pointer)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000511{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000512 return (pointer + 1) & (priv->tx_q_entries - 1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000513}
514
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000515static u32 ftgmac100_tx_buf_avail(struct ftgmac100 *priv)
516{
517 /* Returns the number of available slots in the TX queue
518 *
519 * This always leaves one free slot so we don't have to
520 * worry about empty vs. full, and this simplifies the
521 * test for ftgmac100_tx_buf_cleanable() below
522 */
523 return (priv->tx_clean_pointer - priv->tx_pointer - 1) &
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000524 (priv->tx_q_entries - 1);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000525}
526
527static bool ftgmac100_tx_buf_cleanable(struct ftgmac100 *priv)
528{
529 return priv->tx_pointer != priv->tx_clean_pointer;
530}
531
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000532static void ftgmac100_free_tx_packet(struct ftgmac100 *priv,
533 unsigned int pointer,
534 struct sk_buff *skb,
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000535 struct ftgmac100_txdes *txdes,
536 u32 ctl_stat)
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000537{
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000538 dma_addr_t map = le32_to_cpu(txdes->txdes3);
539 size_t len;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000540
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000541 if (ctl_stat & FTGMAC100_TXDES0_FTS) {
542 len = skb_headlen(skb);
543 dma_unmap_single(priv->dev, map, len, DMA_TO_DEVICE);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000544 } else {
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000545 len = FTGMAC100_TXDES0_TXBUF_SIZE(ctl_stat);
546 dma_unmap_page(priv->dev, map, len, DMA_TO_DEVICE);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000547 }
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000548
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000549 /* Free SKB on last segment */
550 if (ctl_stat & FTGMAC100_TXDES0_LTS)
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000551 dev_kfree_skb(skb);
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000552 priv->tx_skbs[pointer] = NULL;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000553}
554
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000555static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
556{
557 struct net_device *netdev = priv->netdev;
558 struct ftgmac100_txdes *txdes;
559 struct sk_buff *skb;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000560 unsigned int pointer;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000561 u32 ctl_stat;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000562
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000563 pointer = priv->tx_clean_pointer;
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000564 txdes = &priv->txdes[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000565
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000566 ctl_stat = le32_to_cpu(txdes->txdes0);
567 if (ctl_stat & FTGMAC100_TXDES0_TXDMA_OWN)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000568 return false;
569
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000570 skb = priv->tx_skbs[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000571 netdev->stats.tx_packets++;
572 netdev->stats.tx_bytes += skb->len;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000573 ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
574 txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000575
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000576 priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv, pointer);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000577
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000578 return true;
579}
580
581static void ftgmac100_tx_complete(struct ftgmac100 *priv)
582{
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000583 struct net_device *netdev = priv->netdev;
584
585 /* Process all completed packets */
586 while (ftgmac100_tx_buf_cleanable(priv) &&
587 ftgmac100_tx_complete_packet(priv))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000588 ;
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000589
590 /* Restart queue if needed */
591 smp_mb();
592 if (unlikely(netif_queue_stopped(netdev) &&
593 ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)) {
594 struct netdev_queue *txq;
595
596 txq = netdev_get_tx_queue(netdev, 0);
597 __netif_tx_lock(txq, smp_processor_id());
598 if (netif_queue_stopped(netdev) &&
599 ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
600 netif_wake_queue(netdev);
601 __netif_tx_unlock(txq);
602 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000603}
604
Benjamin Herrenschmidt05690d62017-04-12 13:27:01 +1000605static bool ftgmac100_prep_tx_csum(struct sk_buff *skb, u32 *csum_vlan)
606{
607 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
608 u8 ip_proto = ip_hdr(skb)->protocol;
609
610 *csum_vlan |= FTGMAC100_TXDES1_IP_CHKSUM;
611 switch(ip_proto) {
612 case IPPROTO_TCP:
613 *csum_vlan |= FTGMAC100_TXDES1_TCP_CHKSUM;
614 return true;
615 case IPPROTO_UDP:
616 *csum_vlan |= FTGMAC100_TXDES1_UDP_CHKSUM;
617 return true;
618 case IPPROTO_IP:
619 return true;
620 }
621 }
622 return skb_checksum_help(skb) == 0;
623}
624
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000625static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
626 struct net_device *netdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000627{
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000628 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000629 struct ftgmac100_txdes *txdes, *first;
630 unsigned int pointer, nfrags, len, i, j;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000631 u32 f_ctl_stat, ctl_stat, csum_vlan;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000632 dma_addr_t map;
633
Benjamin Herrenschmidt9b0f7712017-04-10 11:15:19 +1000634 /* The HW doesn't pad small frames */
635 if (eth_skb_pad(skb)) {
636 netdev->stats.tx_dropped++;
637 return NETDEV_TX_OK;
638 }
639
640 /* Reject oversize packets */
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000641 if (unlikely(skb->len > MAX_PKT_SIZE)) {
642 if (net_ratelimit())
643 netdev_dbg(netdev, "tx packet too big\n");
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000644 goto drop;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000645 }
646
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000647 /* Do we have a limit on #fragments ? I yet have to get a reply
648 * from Aspeed. If there's one I haven't hit it.
649 */
650 nfrags = skb_shinfo(skb)->nr_frags;
651
652 /* Get header len */
653 len = skb_headlen(skb);
654
655 /* Map the packet head */
656 map = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE);
657 if (dma_mapping_error(priv->dev, map)) {
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000658 if (net_ratelimit())
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000659 netdev_err(netdev, "map tx packet head failed\n");
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000660 goto drop;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000661 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000662
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000663 /* Grab the next free tx descriptor */
664 pointer = priv->tx_pointer;
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000665 txdes = first = &priv->txdes[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000666
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000667 /* Setup it up with the packet head. Don't write the head to the
668 * ring just yet
669 */
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000670 priv->tx_skbs[pointer] = skb;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000671 f_ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
672 f_ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
673 f_ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
674 f_ctl_stat |= FTGMAC100_TXDES0_FTS;
675 if (nfrags == 0)
676 f_ctl_stat |= FTGMAC100_TXDES0_LTS;
677 txdes->txdes3 = cpu_to_le32(map);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000678
679 /* Setup HW checksumming */
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000680 csum_vlan = 0;
Benjamin Herrenschmidt05690d62017-04-12 13:27:01 +1000681 if (skb->ip_summed == CHECKSUM_PARTIAL &&
682 !ftgmac100_prep_tx_csum(skb, &csum_vlan))
683 goto drop;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000684 txdes->txdes1 = cpu_to_le32(csum_vlan);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000685
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000686 /* Next descriptor */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000687 pointer = ftgmac100_next_tx_pointer(priv, pointer);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000688
689 /* Add the fragments */
690 for (i = 0; i < nfrags; i++) {
691 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
692
693 len = frag->size;
694
695 /* Map it */
696 map = skb_frag_dma_map(priv->dev, frag, 0, len,
697 DMA_TO_DEVICE);
698 if (dma_mapping_error(priv->dev, map))
699 goto dma_err;
700
701 /* Setup descriptor */
702 priv->tx_skbs[pointer] = skb;
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000703 txdes = &priv->txdes[pointer];
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000704 ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
705 ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
706 ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
707 if (i == (nfrags - 1))
708 ctl_stat |= FTGMAC100_TXDES0_LTS;
709 txdes->txdes0 = cpu_to_le32(ctl_stat);
710 txdes->txdes1 = 0;
711 txdes->txdes3 = cpu_to_le32(map);
712
713 /* Next one */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000714 pointer = ftgmac100_next_tx_pointer(priv, pointer);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000715 }
716
Benjamin Herrenschmidt4a2712b2017-04-10 11:15:22 +1000717 /* Order the previous packet and descriptor udpates
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000718 * before setting the OWN bit on the first descriptor.
Benjamin Herrenschmidt4a2712b2017-04-10 11:15:22 +1000719 */
720 dma_wmb();
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000721 first->txdes0 = cpu_to_le32(f_ctl_stat);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000722
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000723 /* Update next TX pointer */
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000724 priv->tx_pointer = pointer;
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000725
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000726 /* If there isn't enough room for all the fragments of a new packet
727 * in the TX ring, stop the queue. The sequence below is race free
728 * vs. a concurrent restart in ftgmac100_poll()
729 */
730 if (unlikely(ftgmac100_tx_buf_avail(priv) < TX_THRESHOLD)) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000731 netif_stop_queue(netdev);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000732 /* Order the queue stop with the test below */
733 smp_mb();
734 if (ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
735 netif_wake_queue(netdev);
736 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000737
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000738 /* Poke transmitter to read the updated TX descriptors */
739 iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000740
741 return NETDEV_TX_OK;
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000742
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000743 dma_err:
744 if (net_ratelimit())
745 netdev_err(netdev, "map tx fragment failed\n");
746
747 /* Free head */
748 pointer = priv->tx_pointer;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000749 ftgmac100_free_tx_packet(priv, pointer, skb, first, f_ctl_stat);
750 first->txdes0 = cpu_to_le32(f_ctl_stat & priv->txdes0_edotr_mask);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000751
752 /* Then all fragments */
753 for (j = 0; j < i; j++) {
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000754 pointer = ftgmac100_next_tx_pointer(priv, pointer);
755 txdes = &priv->txdes[pointer];
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000756 ctl_stat = le32_to_cpu(txdes->txdes0);
757 ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
758 txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000759 }
760
761 /* This cannot be reached if we successfully mapped the
762 * last fragment, so we know ftgmac100_free_tx_packet()
763 * hasn't freed the skb yet.
764 */
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000765 drop:
766 /* Drop the packet */
767 dev_kfree_skb_any(skb);
768 netdev->stats.tx_dropped++;
769
770 return NETDEV_TX_OK;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000771}
772
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000773static void ftgmac100_free_buffers(struct ftgmac100 *priv)
774{
775 int i;
776
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000777 /* Free all RX buffers */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000778 for (i = 0; i < priv->rx_q_entries; i++) {
779 struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000780 struct sk_buff *skb = priv->rx_skbs[i];
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000781 dma_addr_t map = le32_to_cpu(rxdes->rxdes3);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000782
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000783 if (!skb)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000784 continue;
785
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000786 priv->rx_skbs[i] = NULL;
787 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
788 dev_kfree_skb_any(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000789 }
790
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000791 /* Free all TX buffers */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000792 for (i = 0; i < priv->tx_q_entries; i++) {
793 struct ftgmac100_txdes *txdes = &priv->txdes[i];
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000794 struct sk_buff *skb = priv->tx_skbs[i];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000795
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000796 if (!skb)
797 continue;
798 ftgmac100_free_tx_packet(priv, i, skb, txdes,
799 le32_to_cpu(txdes->txdes0));
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000800 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000801}
802
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000803static void ftgmac100_free_rings(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000804{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000805 /* Free skb arrays */
806 kfree(priv->rx_skbs);
807 kfree(priv->tx_skbs);
808
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000809 /* Free descriptors */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000810 if (priv->rxdes)
811 dma_free_coherent(priv->dev, MAX_RX_QUEUE_ENTRIES *
812 sizeof(struct ftgmac100_rxdes),
813 priv->rxdes, priv->rxdes_dma);
814 priv->rxdes = NULL;
815
816 if (priv->txdes)
817 dma_free_coherent(priv->dev, MAX_TX_QUEUE_ENTRIES *
818 sizeof(struct ftgmac100_txdes),
819 priv->txdes, priv->txdes_dma);
820 priv->txdes = NULL;
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000821
822 /* Free scratch packet buffer */
823 if (priv->rx_scratch)
824 dma_free_coherent(priv->dev, RX_BUF_SIZE,
825 priv->rx_scratch, priv->rx_scratch_dma);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000826}
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000827
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000828static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
829{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000830 /* Allocate skb arrays */
831 priv->rx_skbs = kcalloc(MAX_RX_QUEUE_ENTRIES, sizeof(void *),
832 GFP_KERNEL);
833 if (!priv->rx_skbs)
834 return -ENOMEM;
835 priv->tx_skbs = kcalloc(MAX_TX_QUEUE_ENTRIES, sizeof(void *),
836 GFP_KERNEL);
837 if (!priv->tx_skbs)
838 return -ENOMEM;
839
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000840 /* Allocate descriptors */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000841 priv->rxdes = dma_zalloc_coherent(priv->dev,
842 MAX_RX_QUEUE_ENTRIES *
843 sizeof(struct ftgmac100_rxdes),
844 &priv->rxdes_dma, GFP_KERNEL);
845 if (!priv->rxdes)
846 return -ENOMEM;
847 priv->txdes = dma_zalloc_coherent(priv->dev,
848 MAX_TX_QUEUE_ENTRIES *
849 sizeof(struct ftgmac100_txdes),
850 &priv->txdes_dma, GFP_KERNEL);
851 if (!priv->txdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000852 return -ENOMEM;
853
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000854 /* Allocate scratch packet buffer */
855 priv->rx_scratch = dma_alloc_coherent(priv->dev,
856 RX_BUF_SIZE,
857 &priv->rx_scratch_dma,
858 GFP_KERNEL);
859 if (!priv->rx_scratch)
860 return -ENOMEM;
861
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000862 return 0;
863}
864
865static void ftgmac100_init_rings(struct ftgmac100 *priv)
866{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000867 struct ftgmac100_rxdes *rxdes = NULL;
868 struct ftgmac100_txdes *txdes = NULL;
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000869 int i;
870
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000871 /* Update entries counts */
872 priv->rx_q_entries = priv->new_rx_q_entries;
873 priv->tx_q_entries = priv->new_tx_q_entries;
874
875 if (WARN_ON(priv->rx_q_entries < MIN_RX_QUEUE_ENTRIES))
876 return;
877
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000878 /* Initialize RX ring */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000879 for (i = 0; i < priv->rx_q_entries; i++) {
880 rxdes = &priv->rxdes[i];
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000881 rxdes->rxdes0 = 0;
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000882 rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma);
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000883 }
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000884 /* Mark the end of the ring */
885 rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000886
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000887 if (WARN_ON(priv->tx_q_entries < MIN_RX_QUEUE_ENTRIES))
888 return;
889
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000890 /* Initialize TX ring */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000891 for (i = 0; i < priv->tx_q_entries; i++) {
892 txdes = &priv->txdes[i];
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000893 txdes->txdes0 = 0;
894 }
895 txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000896}
897
898static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
899{
900 int i;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000901
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000902 for (i = 0; i < priv->rx_q_entries; i++) {
903 struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000904
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000905 if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL))
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000906 return -ENOMEM;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000907 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000908 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000909}
910
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000911static void ftgmac100_adjust_link(struct net_device *netdev)
912{
913 struct ftgmac100 *priv = netdev_priv(netdev);
Philippe Reynesb3c40ad2016-05-16 01:35:13 +0200914 struct phy_device *phydev = netdev->phydev;
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000915 int new_speed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000916
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000917 /* We store "no link" as speed 0 */
918 if (!phydev->link)
919 new_speed = 0;
920 else
921 new_speed = phydev->speed;
922
923 if (phydev->speed == priv->cur_speed &&
924 phydev->duplex == priv->cur_duplex)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000925 return;
926
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000927 /* Print status if we have a link or we had one and just lost it,
928 * don't print otherwise.
929 */
930 if (new_speed || priv->cur_speed)
931 phy_print_status(phydev);
932
933 priv->cur_speed = new_speed;
934 priv->cur_duplex = phydev->duplex;
935
936 /* Link is down, do nothing else */
937 if (!new_speed)
938 return;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000939
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000940 /* Disable all interrupts */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000941 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
942
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000943 /* Reset the adapter asynchronously */
944 schedule_work(&priv->reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000945}
946
947static int ftgmac100_mii_probe(struct ftgmac100 *priv)
948{
949 struct net_device *netdev = priv->netdev;
Guenter Roecke574f392016-01-10 12:04:32 -0800950 struct phy_device *phydev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000951
Guenter Roecke574f392016-01-10 12:04:32 -0800952 phydev = phy_find_first(priv->mii_bus);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000953 if (!phydev) {
954 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
955 return -ENODEV;
956 }
957
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100958 phydev = phy_connect(netdev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000959 &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000960
961 if (IS_ERR(phydev)) {
962 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
963 return PTR_ERR(phydev);
964 }
965
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000966 return 0;
967}
968
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000969static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
970{
971 struct net_device *netdev = bus->priv;
972 struct ftgmac100 *priv = netdev_priv(netdev);
973 unsigned int phycr;
974 int i;
975
976 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
977
978 /* preserve MDC cycle threshold */
979 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
980
981 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
982 FTGMAC100_PHYCR_REGAD(regnum) |
983 FTGMAC100_PHYCR_MIIRD;
984
985 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
986
987 for (i = 0; i < 10; i++) {
988 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
989
990 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
991 int data;
992
993 data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
994 return FTGMAC100_PHYDATA_MIIRDATA(data);
995 }
996
997 udelay(100);
998 }
999
1000 netdev_err(netdev, "mdio read timed out\n");
1001 return -EIO;
1002}
1003
1004static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
1005 int regnum, u16 value)
1006{
1007 struct net_device *netdev = bus->priv;
1008 struct ftgmac100 *priv = netdev_priv(netdev);
1009 unsigned int phycr;
1010 int data;
1011 int i;
1012
1013 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1014
1015 /* preserve MDC cycle threshold */
1016 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
1017
1018 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
1019 FTGMAC100_PHYCR_REGAD(regnum) |
1020 FTGMAC100_PHYCR_MIIWR;
1021
1022 data = FTGMAC100_PHYDATA_MIIWDATA(value);
1023
1024 iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
1025 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
1026
1027 for (i = 0; i < 10; i++) {
1028 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1029
1030 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
1031 return 0;
1032
1033 udelay(100);
1034 }
1035
1036 netdev_err(netdev, "mdio write timed out\n");
1037 return -EIO;
1038}
1039
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001040static void ftgmac100_get_drvinfo(struct net_device *netdev,
1041 struct ethtool_drvinfo *info)
1042{
Jiri Pirko7826d432013-01-06 00:44:26 +00001043 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1044 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1045 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001046}
1047
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +10001048static int ftgmac100_nway_reset(struct net_device *ndev)
1049{
1050 if (!ndev->phydev)
1051 return -ENXIO;
1052 return phy_start_aneg(ndev->phydev);
1053}
1054
1055static void ftgmac100_get_ringparam(struct net_device *netdev,
1056 struct ethtool_ringparam *ering)
1057{
1058 struct ftgmac100 *priv = netdev_priv(netdev);
1059
1060 memset(ering, 0, sizeof(*ering));
1061 ering->rx_max_pending = MAX_RX_QUEUE_ENTRIES;
1062 ering->tx_max_pending = MAX_TX_QUEUE_ENTRIES;
1063 ering->rx_pending = priv->rx_q_entries;
1064 ering->tx_pending = priv->tx_q_entries;
1065}
1066
1067static int ftgmac100_set_ringparam(struct net_device *netdev,
1068 struct ethtool_ringparam *ering)
1069{
1070 struct ftgmac100 *priv = netdev_priv(netdev);
1071
1072 if (ering->rx_pending > MAX_RX_QUEUE_ENTRIES ||
1073 ering->tx_pending > MAX_TX_QUEUE_ENTRIES ||
1074 ering->rx_pending < MIN_RX_QUEUE_ENTRIES ||
1075 ering->tx_pending < MIN_TX_QUEUE_ENTRIES ||
1076 !is_power_of_2(ering->rx_pending) ||
1077 !is_power_of_2(ering->tx_pending))
1078 return -EINVAL;
1079
1080 priv->new_rx_q_entries = ering->rx_pending;
1081 priv->new_tx_q_entries = ering->tx_pending;
1082 if (netif_running(netdev))
1083 schedule_work(&priv->reset_task);
1084
1085 return 0;
1086}
1087
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001088static const struct ethtool_ops ftgmac100_ethtool_ops = {
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001089 .get_drvinfo = ftgmac100_get_drvinfo,
1090 .get_link = ethtool_op_get_link,
Philippe Reynesfd24d722016-05-16 01:35:14 +02001091 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1092 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +10001093 .get_ringparam = ftgmac100_get_ringparam,
1094 .set_ringparam = ftgmac100_set_ringparam,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001095};
1096
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001097static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
1098{
1099 struct net_device *netdev = dev_id;
1100 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001101 unsigned int status, new_mask = FTGMAC100_INT_BAD;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001102
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001103 /* Fetch and clear interrupt bits, process abnormal ones */
1104 status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1105 iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
1106 if (unlikely(status & FTGMAC100_INT_BAD)) {
1107
1108 /* RX buffer unavailable */
1109 if (status & FTGMAC100_INT_NO_RXBUF)
1110 netdev->stats.rx_over_errors++;
1111
1112 /* received packet lost due to RX FIFO full */
1113 if (status & FTGMAC100_INT_RPKT_LOST)
1114 netdev->stats.rx_fifo_errors++;
1115
1116 /* sent packet lost due to excessive TX collision */
1117 if (status & FTGMAC100_INT_XPKT_LOST)
1118 netdev->stats.tx_fifo_errors++;
1119
1120 /* AHB error -> Reset the chip */
1121 if (status & FTGMAC100_INT_AHB_ERR) {
1122 if (net_ratelimit())
1123 netdev_warn(netdev,
1124 "AHB bus error ! Resetting chip.\n");
1125 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1126 schedule_work(&priv->reset_task);
1127 return IRQ_HANDLED;
1128 }
1129
1130 /* We may need to restart the MAC after such errors, delay
1131 * this until after we have freed some Rx buffers though
1132 */
1133 priv->need_mac_restart = true;
1134
1135 /* Disable those errors until we restart */
1136 new_mask &= ~status;
1137 }
1138
1139 /* Only enable "bad" interrupts while NAPI is on */
1140 iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
1141
1142 /* Schedule NAPI bh */
1143 napi_schedule_irqoff(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001144
1145 return IRQ_HANDLED;
1146}
1147
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +10001148static bool ftgmac100_check_rx(struct ftgmac100 *priv)
1149{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +10001150 struct ftgmac100_rxdes *rxdes = &priv->rxdes[priv->rx_pointer];
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +10001151
1152 /* Do we have a packet ? */
1153 return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY));
1154}
1155
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001156static int ftgmac100_poll(struct napi_struct *napi, int budget)
1157{
1158 struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001159 int work_done = 0;
1160 bool more;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001161
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001162 /* Handle TX completions */
1163 if (ftgmac100_tx_buf_cleanable(priv))
1164 ftgmac100_tx_complete(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001165
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001166 /* Handle RX packets */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001167 do {
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001168 more = ftgmac100_rx_packet(priv, &work_done);
1169 } while (more && work_done < budget);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001170
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001171
1172 /* The interrupt is telling us to kick the MAC back to life
1173 * after an RX overflow
1174 */
1175 if (unlikely(priv->need_mac_restart)) {
1176 ftgmac100_start_hw(priv);
1177
1178 /* Re-enable "bad" interrupts */
1179 iowrite32(FTGMAC100_INT_BAD,
1180 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001181 }
1182
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001183 /* As long as we are waiting for transmit packets to be
1184 * completed we keep NAPI going
1185 */
1186 if (ftgmac100_tx_buf_cleanable(priv))
1187 work_done = budget;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001188
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001189 if (work_done < budget) {
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001190 /* We are about to re-enable all interrupts. However
1191 * the HW has been latching RX/TX packet interrupts while
1192 * they were masked. So we clear them first, then we need
1193 * to re-check if there's something to process
1194 */
1195 iowrite32(FTGMAC100_INT_RXTX,
1196 priv->base + FTGMAC100_OFFSET_ISR);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001197 if (ftgmac100_check_rx(priv) ||
1198 ftgmac100_tx_buf_cleanable(priv))
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001199 return budget;
1200
1201 /* deschedule NAPI */
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001202 napi_complete(napi);
1203
1204 /* enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001205 iowrite32(FTGMAC100_INT_ALL,
Gavin Shanfc6061c2016-07-19 11:54:25 +10001206 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001207 }
1208
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001209 return work_done;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001210}
1211
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001212static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
1213{
1214 int err = 0;
1215
1216 /* Re-init descriptors (adjust queue sizes) */
1217 ftgmac100_init_rings(priv);
1218
1219 /* Realloc rx descriptors */
1220 err = ftgmac100_alloc_rx_buffers(priv);
1221 if (err && !ignore_alloc_err)
1222 return err;
1223
1224 /* Reinit and restart HW */
1225 ftgmac100_init_hw(priv);
1226 ftgmac100_start_hw(priv);
1227
1228 /* Re-enable the device */
1229 napi_enable(&priv->napi);
1230 netif_start_queue(priv->netdev);
1231
1232 /* Enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001233 iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001234
1235 return err;
1236}
1237
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001238static void ftgmac100_reset_task(struct work_struct *work)
1239{
1240 struct ftgmac100 *priv = container_of(work, struct ftgmac100,
1241 reset_task);
1242 struct net_device *netdev = priv->netdev;
1243 int err;
1244
1245 netdev_dbg(netdev, "Resetting NIC...\n");
1246
1247 /* Lock the world */
1248 rtnl_lock();
1249 if (netdev->phydev)
1250 mutex_lock(&netdev->phydev->lock);
1251 if (priv->mii_bus)
1252 mutex_lock(&priv->mii_bus->mdio_lock);
1253
1254
1255 /* Check if the interface is still up */
1256 if (!netif_running(netdev))
1257 goto bail;
1258
1259 /* Stop the network stack */
1260 netif_trans_update(netdev);
1261 napi_disable(&priv->napi);
1262 netif_tx_disable(netdev);
1263
1264 /* Stop and reset the MAC */
1265 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001266 err = ftgmac100_reset_and_config_mac(priv);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001267 if (err) {
1268 /* Not much we can do ... it might come back... */
1269 netdev_err(netdev, "attempting to continue...\n");
1270 }
1271
1272 /* Free all rx and tx buffers */
1273 ftgmac100_free_buffers(priv);
1274
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001275 /* Setup everything again and restart chip */
1276 ftgmac100_init_all(priv, true);
1277
1278 netdev_dbg(netdev, "Reset done !\n");
1279 bail:
1280 if (priv->mii_bus)
1281 mutex_unlock(&priv->mii_bus->mdio_lock);
1282 if (netdev->phydev)
1283 mutex_unlock(&netdev->phydev->lock);
1284 rtnl_unlock();
1285}
1286
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001287static int ftgmac100_open(struct net_device *netdev)
1288{
1289 struct ftgmac100 *priv = netdev_priv(netdev);
1290 int err;
1291
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001292 /* Allocate ring buffers */
1293 err = ftgmac100_alloc_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001294 if (err) {
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001295 netdev_err(netdev, "Failed to allocate descriptors\n");
1296 return err;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001297 }
1298
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001299 /* When using NC-SI we force the speed to 100Mbit/s full duplex,
1300 *
1301 * Otherwise we leave it set to 0 (no link), the link
1302 * message from the PHY layer will handle setting it up to
1303 * something else if needed.
1304 */
1305 if (priv->use_ncsi) {
1306 priv->cur_duplex = DUPLEX_FULL;
1307 priv->cur_speed = SPEED_100;
1308 } else {
1309 priv->cur_duplex = 0;
1310 priv->cur_speed = 0;
1311 }
1312
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001313 /* Reset the hardware */
1314 err = ftgmac100_reset_and_config_mac(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001315 if (err)
1316 goto err_hw;
1317
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001318 /* Initialize NAPI */
1319 netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1320
Benjamin Herrenschmidt81f1eca2017-04-05 12:28:48 +10001321 /* Grab our interrupt */
1322 err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1323 if (err) {
1324 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
1325 goto err_irq;
1326 }
1327
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001328 /* Start things up */
1329 err = ftgmac100_init_all(priv, false);
1330 if (err) {
1331 netdev_err(netdev, "Failed to allocate packet buffers\n");
1332 goto err_alloc;
1333 }
Gavin Shan08c9c122016-09-22 08:35:01 +09301334
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001335 if (netdev->phydev) {
1336 /* If we have a PHY, start polling */
Gavin Shanbd466c32016-07-19 11:54:23 +10001337 phy_start(netdev->phydev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001338 } else if (priv->use_ncsi) {
1339 /* If using NC-SI, set our carrier on and start the stack */
Gavin Shanbd466c32016-07-19 11:54:23 +10001340 netif_carrier_on(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001341
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001342 /* Start the NCSI device */
Gavin Shanbd466c32016-07-19 11:54:23 +10001343 err = ncsi_start_dev(priv->ndev);
1344 if (err)
1345 goto err_ncsi;
1346 }
1347
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001348 return 0;
1349
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001350 err_ncsi:
Gavin Shanbd466c32016-07-19 11:54:23 +10001351 napi_disable(&priv->napi);
1352 netif_stop_queue(netdev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001353 err_alloc:
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001354 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001355 free_irq(netdev->irq, netdev);
1356 err_irq:
1357 netif_napi_del(&priv->napi);
1358 err_hw:
1359 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001360 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001361 return err;
1362}
1363
1364static int ftgmac100_stop(struct net_device *netdev)
1365{
1366 struct ftgmac100 *priv = netdev_priv(netdev);
1367
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001368 /* Note about the reset task: We are called with the rtnl lock
1369 * held, so we are synchronized against the core of the reset
1370 * task. We must not try to synchronously cancel it otherwise
1371 * we can deadlock. But since it will test for netif_running()
1372 * which has already been cleared by the net core, we don't
1373 * anything special to do.
1374 */
1375
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001376 /* disable all interrupts */
1377 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1378
1379 netif_stop_queue(netdev);
1380 napi_disable(&priv->napi);
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001381 netif_napi_del(&priv->napi);
Gavin Shanbd466c32016-07-19 11:54:23 +10001382 if (netdev->phydev)
1383 phy_stop(netdev->phydev);
Gavin Shan2c15f252016-10-04 11:25:54 +11001384 else if (priv->use_ncsi)
1385 ncsi_stop_dev(priv->ndev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001386
1387 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001388 free_irq(netdev->irq, netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001389 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001390 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001391
1392 return 0;
1393}
1394
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001395/* optional */
1396static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1397{
Gavin Shanbd466c32016-07-19 11:54:23 +10001398 if (!netdev->phydev)
1399 return -ENXIO;
1400
Philippe Reynesb3c40ad2016-05-16 01:35:13 +02001401 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001402}
1403
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001404static void ftgmac100_tx_timeout(struct net_device *netdev)
1405{
1406 struct ftgmac100 *priv = netdev_priv(netdev);
1407
1408 /* Disable all interrupts */
1409 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1410
1411 /* Do the reset outside of interrupt context */
1412 schedule_work(&priv->reset_task);
1413}
1414
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001415static const struct net_device_ops ftgmac100_netdev_ops = {
1416 .ndo_open = ftgmac100_open,
1417 .ndo_stop = ftgmac100_stop,
1418 .ndo_start_xmit = ftgmac100_hard_start_xmit,
Gavin Shan113ce102016-07-19 11:54:22 +10001419 .ndo_set_mac_address = ftgmac100_set_mac_addr,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001420 .ndo_validate_addr = eth_validate_addr,
1421 .ndo_do_ioctl = ftgmac100_do_ioctl,
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001422 .ndo_tx_timeout = ftgmac100_tx_timeout,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001423};
1424
Gavin Shaneb418182016-07-19 11:54:21 +10001425static int ftgmac100_setup_mdio(struct net_device *netdev)
1426{
1427 struct ftgmac100 *priv = netdev_priv(netdev);
1428 struct platform_device *pdev = to_platform_device(priv->dev);
1429 int i, err = 0;
Joel Stanleye07dc632016-09-22 08:35:02 +09301430 u32 reg;
Gavin Shaneb418182016-07-19 11:54:21 +10001431
1432 /* initialize mdio bus */
1433 priv->mii_bus = mdiobus_alloc();
1434 if (!priv->mii_bus)
1435 return -EIO;
1436
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001437 if (priv->is_aspeed) {
Joel Stanleye07dc632016-09-22 08:35:02 +09301438 /* This driver supports the old MDIO interface */
1439 reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
1440 reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
1441 iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
1442 };
1443
Gavin Shaneb418182016-07-19 11:54:21 +10001444 priv->mii_bus->name = "ftgmac100_mdio";
1445 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1446 pdev->name, pdev->id);
1447 priv->mii_bus->priv = priv->netdev;
1448 priv->mii_bus->read = ftgmac100_mdiobus_read;
1449 priv->mii_bus->write = ftgmac100_mdiobus_write;
1450
1451 for (i = 0; i < PHY_MAX_ADDR; i++)
1452 priv->mii_bus->irq[i] = PHY_POLL;
1453
1454 err = mdiobus_register(priv->mii_bus);
1455 if (err) {
1456 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1457 goto err_register_mdiobus;
1458 }
1459
1460 err = ftgmac100_mii_probe(priv);
1461 if (err) {
1462 dev_err(priv->dev, "MII Probe failed!\n");
1463 goto err_mii_probe;
1464 }
1465
1466 return 0;
1467
1468err_mii_probe:
1469 mdiobus_unregister(priv->mii_bus);
1470err_register_mdiobus:
1471 mdiobus_free(priv->mii_bus);
1472 return err;
1473}
1474
1475static void ftgmac100_destroy_mdio(struct net_device *netdev)
1476{
1477 struct ftgmac100 *priv = netdev_priv(netdev);
1478
1479 if (!netdev->phydev)
1480 return;
1481
1482 phy_disconnect(netdev->phydev);
1483 mdiobus_unregister(priv->mii_bus);
1484 mdiobus_free(priv->mii_bus);
1485}
1486
Gavin Shanbd466c32016-07-19 11:54:23 +10001487static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1488{
1489 if (unlikely(nd->state != ncsi_dev_state_functional))
1490 return;
1491
1492 netdev_info(nd->dev, "NCSI interface %s\n",
1493 nd->link_up ? "up" : "down");
1494}
1495
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001496static int ftgmac100_probe(struct platform_device *pdev)
1497{
1498 struct resource *res;
1499 int irq;
1500 struct net_device *netdev;
1501 struct ftgmac100 *priv;
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001502 struct device_node *np;
Gavin Shanbd466c32016-07-19 11:54:23 +10001503 int err = 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001504
1505 if (!pdev)
1506 return -ENODEV;
1507
1508 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1509 if (!res)
1510 return -ENXIO;
1511
1512 irq = platform_get_irq(pdev, 0);
1513 if (irq < 0)
1514 return irq;
1515
1516 /* setup net_device */
1517 netdev = alloc_etherdev(sizeof(*priv));
1518 if (!netdev) {
1519 err = -ENOMEM;
1520 goto err_alloc_etherdev;
1521 }
1522
1523 SET_NETDEV_DEV(netdev, &pdev->dev);
1524
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001525 netdev->ethtool_ops = &ftgmac100_ethtool_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001526 netdev->netdev_ops = &ftgmac100_netdev_ops;
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001527 netdev->watchdog_timeo = 5 * HZ;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001528
1529 platform_set_drvdata(pdev, netdev);
1530
1531 /* setup private data */
1532 priv = netdev_priv(netdev);
1533 priv->netdev = netdev;
1534 priv->dev = &pdev->dev;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001535 INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001536
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001537 /* map io memory */
1538 priv->res = request_mem_region(res->start, resource_size(res),
1539 dev_name(&pdev->dev));
1540 if (!priv->res) {
1541 dev_err(&pdev->dev, "Could not reserve memory region\n");
1542 err = -ENOMEM;
1543 goto err_req_mem;
1544 }
1545
1546 priv->base = ioremap(res->start, resource_size(res));
1547 if (!priv->base) {
1548 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1549 err = -EIO;
1550 goto err_ioremap;
1551 }
1552
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001553 netdev->irq = irq;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001554
Gavin Shan113ce102016-07-19 11:54:22 +10001555 /* MAC address from chip or random one */
Benjamin Herrenschmidtba1b1232017-04-12 13:27:06 +10001556 ftgmac100_initial_mac(priv);
Gavin Shan113ce102016-07-19 11:54:22 +10001557
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001558 np = pdev->dev.of_node;
1559 if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
1560 of_device_is_compatible(np, "aspeed,ast2500-mac"))) {
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301561 priv->rxdes0_edorr_mask = BIT(30);
1562 priv->txdes0_edotr_mask = BIT(30);
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001563 priv->is_aspeed = true;
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301564 } else {
1565 priv->rxdes0_edorr_mask = BIT(15);
1566 priv->txdes0_edotr_mask = BIT(15);
1567 }
1568
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001569 if (np && of_get_property(np, "use-ncsi", NULL)) {
Gavin Shanbd466c32016-07-19 11:54:23 +10001570 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1571 dev_err(&pdev->dev, "NCSI stack not enabled\n");
1572 goto err_ncsi_dev;
1573 }
1574
1575 dev_info(&pdev->dev, "Using NCSI interface\n");
1576 priv->use_ncsi = true;
1577 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1578 if (!priv->ndev)
1579 goto err_ncsi_dev;
1580 } else {
1581 priv->use_ncsi = false;
1582 err = ftgmac100_setup_mdio(netdev);
1583 if (err)
1584 goto err_setup_mdio;
1585 }
1586
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +10001587 /* Default ring sizes */
1588 priv->rx_q_entries = priv->new_rx_q_entries = DEF_RX_QUEUE_ENTRIES;
1589 priv->tx_q_entries = priv->new_tx_q_entries = DEF_TX_QUEUE_ENTRIES;
1590
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001591 /* Base feature set */
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001592 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +10001593 NETIF_F_GRO | NETIF_F_SG;
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001594
1595 /* AST2400 doesn't have working HW checksum generation */
1596 if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac")))
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001597 netdev->hw_features &= ~NETIF_F_HW_CSUM;
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001598 if (np && of_get_property(np, "no-hw-checksum", NULL))
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001599 netdev->hw_features &= ~(NETIF_F_HW_CSUM | NETIF_F_RXCSUM);
1600 netdev->features |= netdev->hw_features;
Gavin Shanbd466c32016-07-19 11:54:23 +10001601
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001602 /* register network device */
1603 err = register_netdev(netdev);
1604 if (err) {
1605 dev_err(&pdev->dev, "Failed to register netdev\n");
1606 goto err_register_netdev;
1607 }
1608
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001609 netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001610
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001611 return 0;
1612
Gavin Shanbd466c32016-07-19 11:54:23 +10001613err_ncsi_dev:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001614err_register_netdev:
Gavin Shaneb418182016-07-19 11:54:21 +10001615 ftgmac100_destroy_mdio(netdev);
1616err_setup_mdio:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001617 iounmap(priv->base);
1618err_ioremap:
1619 release_resource(priv->res);
1620err_req_mem:
1621 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001622 free_netdev(netdev);
1623err_alloc_etherdev:
1624 return err;
1625}
1626
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001627static int ftgmac100_remove(struct platform_device *pdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001628{
1629 struct net_device *netdev;
1630 struct ftgmac100 *priv;
1631
1632 netdev = platform_get_drvdata(pdev);
1633 priv = netdev_priv(netdev);
1634
1635 unregister_netdev(netdev);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001636
1637 /* There's a small chance the reset task will have been re-queued,
1638 * during stop, make sure it's gone before we free the structure.
1639 */
1640 cancel_work_sync(&priv->reset_task);
1641
Gavin Shaneb418182016-07-19 11:54:21 +10001642 ftgmac100_destroy_mdio(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001643
1644 iounmap(priv->base);
1645 release_resource(priv->res);
1646
1647 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001648 free_netdev(netdev);
1649 return 0;
1650}
1651
Gavin Shanbb168e22016-07-19 11:54:24 +10001652static const struct of_device_id ftgmac100_of_match[] = {
1653 { .compatible = "faraday,ftgmac100" },
1654 { }
1655};
1656MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1657
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001658static struct platform_driver ftgmac100_driver = {
Gavin Shanbb168e22016-07-19 11:54:24 +10001659 .probe = ftgmac100_probe,
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001660 .remove = ftgmac100_remove,
Gavin Shanbb168e22016-07-19 11:54:24 +10001661 .driver = {
1662 .name = DRV_NAME,
1663 .of_match_table = ftgmac100_of_match,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001664 },
1665};
Sachin Kamat14f645d2013-03-18 01:50:48 +00001666module_platform_driver(ftgmac100_driver);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001667
1668MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1669MODULE_DESCRIPTION("FTGMAC100 driver");
1670MODULE_LICENSE("GPL");