blob: 7721c2ab24ef05753e5cfc48204b9df85ceda69e [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>
Benjamin Herrenschmidtf48b3c02017-04-18 08:37:00 +100035#include <linux/crc32.h>
Benjamin Herrenschmidt0fb99682017-04-18 08:37:01 +100036#include <linux/if_vlan.h>
Benjamin Herrenschmidtabcc3eb2017-04-18 08:37:03 +100037#include <linux/of_net.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000038#include <net/ip.h>
Gavin Shanbd466c32016-07-19 11:54:23 +100039#include <net/ncsi.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000040
41#include "ftgmac100.h"
42
43#define DRV_NAME "ftgmac100"
44#define DRV_VERSION "0.7"
45
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +100046/* Arbitrary values, I am not sure the HW has limits */
47#define MAX_RX_QUEUE_ENTRIES 1024
48#define MAX_TX_QUEUE_ENTRIES 1024
49#define MIN_RX_QUEUE_ENTRIES 32
50#define MIN_TX_QUEUE_ENTRIES 32
51
52/* Defaults */
Benjamin Herrenschmidtbd3e4fd2017-04-12 13:27:10 +100053#define DEF_RX_QUEUE_ENTRIES 128
54#define DEF_TX_QUEUE_ENTRIES 128
Po-Yu Chuang69785b72011-06-08 23:32:48 +000055
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +100056#define MAX_PKT_SIZE 1536
57#define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000058
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +100059/* Min number of tx ring entries before stopping queue */
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +100060#define TX_THRESHOLD (MAX_SKB_FRAGS + 1)
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +100061
Po-Yu Chuang69785b72011-06-08 23:32:48 +000062struct ftgmac100 {
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100063 /* Registers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000064 struct resource *res;
65 void __iomem *base;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000066
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100067 /* Rx ring */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +100068 unsigned int rx_q_entries;
69 struct ftgmac100_rxdes *rxdes;
70 dma_addr_t rxdes_dma;
71 struct sk_buff **rx_skbs;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000072 unsigned int rx_pointer;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100073 u32 rxdes0_edorr_mask;
74
75 /* Tx ring */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +100076 unsigned int tx_q_entries;
77 struct ftgmac100_txdes *txdes;
78 dma_addr_t txdes_dma;
79 struct sk_buff **tx_skbs;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000080 unsigned int tx_clean_pointer;
81 unsigned int tx_pointer;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100082 u32 txdes0_edotr_mask;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000083
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +100084 /* Used to signal the reset task of ring change request */
85 unsigned int new_rx_q_entries;
86 unsigned int new_tx_q_entries;
87
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +100088 /* Scratch page to use when rx skb alloc fails */
89 void *rx_scratch;
90 dma_addr_t rx_scratch_dma;
91
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100092 /* Component structures */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000093 struct net_device *netdev;
94 struct device *dev;
Gavin Shanbd466c32016-07-19 11:54:23 +100095 struct ncsi_dev *ndev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000096 struct napi_struct napi;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +100097 struct work_struct reset_task;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000098 struct mii_bus *mii_bus;
Andrew Jeffery7906a4d2016-09-22 08:34:59 +093099
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +1000100 /* Link management */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000101 int cur_speed;
102 int cur_duplex;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +1000103 bool use_ncsi;
104
Benjamin Herrenschmidtf48b3c02017-04-18 08:37:00 +1000105 /* Multicast filter settings */
106 u32 maht0;
107 u32 maht1;
108
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +1000109 /* Flow control settings */
110 bool tx_pause;
111 bool rx_pause;
112 bool aneg_pause;
113
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +1000114 /* Misc */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +1000115 bool need_mac_restart;
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +1000116 bool is_aspeed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000117};
118
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000119static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000120{
121 struct net_device *netdev = priv->netdev;
122 int i;
123
124 /* NOTE: reset clears all registers */
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000125 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
126 iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
127 priv->base + FTGMAC100_OFFSET_MACCR);
128 for (i = 0; i < 50; i++) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000129 unsigned int maccr;
130
131 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
132 if (!(maccr & FTGMAC100_MACCR_SW_RST))
133 return 0;
134
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000135 udelay(1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000136 }
137
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000138 netdev_err(netdev, "Hardware reset failed\n");
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000139 return -EIO;
140}
141
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000142static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
143{
144 u32 maccr = 0;
145
146 switch (priv->cur_speed) {
147 case SPEED_10:
148 case 0: /* no link */
149 break;
150
151 case SPEED_100:
152 maccr |= FTGMAC100_MACCR_FAST_MODE;
153 break;
154
155 case SPEED_1000:
156 maccr |= FTGMAC100_MACCR_GIGA_MODE;
157 break;
158 default:
159 netdev_err(priv->netdev, "Unknown speed %d !\n",
160 priv->cur_speed);
161 break;
162 }
163
164 /* (Re)initialize the queue pointers */
165 priv->rx_pointer = 0;
166 priv->tx_clean_pointer = 0;
167 priv->tx_pointer = 0;
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000168
169 /* The doc says reset twice with 10us interval */
170 if (ftgmac100_reset_mac(priv, maccr))
171 return -EIO;
172 usleep_range(10, 1000);
173 return ftgmac100_reset_mac(priv, maccr);
174}
175
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000176static void ftgmac100_write_mac_addr(struct ftgmac100 *priv, const u8 *mac)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000177{
178 unsigned int maddr = mac[0] << 8 | mac[1];
179 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
180
181 iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
182 iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
183}
184
Benjamin Herrenschmidtba1b1232017-04-12 13:27:06 +1000185static void ftgmac100_initial_mac(struct ftgmac100 *priv)
Gavin Shan113ce102016-07-19 11:54:22 +1000186{
187 u8 mac[ETH_ALEN];
188 unsigned int m;
189 unsigned int l;
190 void *addr;
191
192 addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
193 if (addr) {
194 ether_addr_copy(priv->netdev->dev_addr, mac);
195 dev_info(priv->dev, "Read MAC address %pM from device tree\n",
196 mac);
197 return;
198 }
199
200 m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
201 l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
202
203 mac[0] = (m >> 8) & 0xff;
204 mac[1] = m & 0xff;
205 mac[2] = (l >> 24) & 0xff;
206 mac[3] = (l >> 16) & 0xff;
207 mac[4] = (l >> 8) & 0xff;
208 mac[5] = l & 0xff;
209
Gavin Shan113ce102016-07-19 11:54:22 +1000210 if (is_valid_ether_addr(mac)) {
211 ether_addr_copy(priv->netdev->dev_addr, mac);
212 dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
213 } else {
214 eth_hw_addr_random(priv->netdev);
215 dev_info(priv->dev, "Generated random MAC address %pM\n",
216 priv->netdev->dev_addr);
217 }
218}
219
220static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
221{
222 int ret;
223
224 ret = eth_prepare_mac_addr_change(dev, p);
225 if (ret < 0)
226 return ret;
227
228 eth_commit_mac_addr_change(dev, p);
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000229 ftgmac100_write_mac_addr(netdev_priv(dev), dev->dev_addr);
Gavin Shan113ce102016-07-19 11:54:22 +1000230
231 return 0;
232}
233
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +1000234static void ftgmac100_config_pause(struct ftgmac100 *priv)
235{
236 u32 fcr = FTGMAC100_FCR_PAUSE_TIME(16);
237
238 /* Throttle tx queue when receiving pause frames */
239 if (priv->rx_pause)
240 fcr |= FTGMAC100_FCR_FC_EN;
241
242 /* Enables sending pause frames when the RX queue is past a
243 * certain threshold.
244 */
245 if (priv->tx_pause)
246 fcr |= FTGMAC100_FCR_FCTHR_EN;
247
248 iowrite32(fcr, priv->base + FTGMAC100_OFFSET_FCR);
249}
250
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000251static void ftgmac100_init_hw(struct ftgmac100 *priv)
252{
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000253 u32 reg, rfifo_sz, tfifo_sz;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000254
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000255 /* Clear stale interrupts */
256 reg = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
257 iowrite32(reg, priv->base + FTGMAC100_OFFSET_ISR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000258
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000259 /* Setup RX ring buffer base */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000260 iowrite32(priv->rxdes_dma, priv->base + FTGMAC100_OFFSET_RXR_BADR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000261
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000262 /* Setup TX ring buffer base */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000263 iowrite32(priv->txdes_dma, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000264
265 /* Configure RX buffer size */
266 iowrite32(FTGMAC100_RBSR_SIZE(RX_BUF_SIZE),
267 priv->base + FTGMAC100_OFFSET_RBSR);
268
269 /* Set RX descriptor autopoll */
270 iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1),
271 priv->base + FTGMAC100_OFFSET_APTC);
272
273 /* Write MAC address */
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000274 ftgmac100_write_mac_addr(priv, priv->netdev->dev_addr);
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000275
Benjamin Herrenschmidtf48b3c02017-04-18 08:37:00 +1000276 /* Write multicast filter */
277 iowrite32(priv->maht0, priv->base + FTGMAC100_OFFSET_MAHT0);
278 iowrite32(priv->maht1, priv->base + FTGMAC100_OFFSET_MAHT1);
279
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000280 /* Configure descriptor sizes and increase burst sizes according
281 * to values in Aspeed SDK. The FIFO arbitration is enabled and
282 * the thresholds set based on the recommended values in the
283 * AST2400 specification.
284 */
285 iowrite32(FTGMAC100_DBLAC_RXDES_SIZE(2) | /* 2*8 bytes RX descs */
286 FTGMAC100_DBLAC_TXDES_SIZE(2) | /* 2*8 bytes TX descs */
287 FTGMAC100_DBLAC_RXBURST_SIZE(3) | /* 512 bytes max RX bursts */
288 FTGMAC100_DBLAC_TXBURST_SIZE(3) | /* 512 bytes max TX bursts */
289 FTGMAC100_DBLAC_RX_THR_EN | /* Enable fifo threshold arb */
290 FTGMAC100_DBLAC_RXFIFO_HTHR(6) | /* 6/8 of FIFO high threshold */
291 FTGMAC100_DBLAC_RXFIFO_LTHR(2), /* 2/8 of FIFO low threshold */
292 priv->base + FTGMAC100_OFFSET_DBLAC);
293
294 /* Interrupt mitigation configured for 1 interrupt/packet. HW interrupt
295 * mitigation doesn't seem to provide any benefit with NAPI so leave
296 * it at that.
297 */
298 iowrite32(FTGMAC100_ITC_RXINT_THR(1) |
299 FTGMAC100_ITC_TXINT_THR(1),
300 priv->base + FTGMAC100_OFFSET_ITC);
301
302 /* Configure FIFO sizes in the TPAFCR register */
303 reg = ioread32(priv->base + FTGMAC100_OFFSET_FEAR);
304 rfifo_sz = reg & 0x00000007;
305 tfifo_sz = (reg >> 3) & 0x00000007;
306 reg = ioread32(priv->base + FTGMAC100_OFFSET_TPAFCR);
307 reg &= ~0x3f000000;
308 reg |= (tfifo_sz << 27);
309 reg |= (rfifo_sz << 24);
310 iowrite32(reg, priv->base + FTGMAC100_OFFSET_TPAFCR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000311}
312
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000313static void ftgmac100_start_hw(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000314{
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000315 u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000316
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000317 /* Keep the original GMAC and FAST bits */
318 maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000319
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000320 /* Add all the main enable bits */
321 maccr |= FTGMAC100_MACCR_TXDMA_EN |
322 FTGMAC100_MACCR_RXDMA_EN |
323 FTGMAC100_MACCR_TXMAC_EN |
324 FTGMAC100_MACCR_RXMAC_EN |
325 FTGMAC100_MACCR_CRC_APD |
326 FTGMAC100_MACCR_PHY_LINK_LEVEL |
327 FTGMAC100_MACCR_RX_RUNT |
328 FTGMAC100_MACCR_RX_BROADPKT;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000329
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000330 /* Add other bits as needed */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000331 if (priv->cur_duplex == DUPLEX_FULL)
332 maccr |= FTGMAC100_MACCR_FULLDUP;
Benjamin Herrenschmidtf48b3c02017-04-18 08:37:00 +1000333 if (priv->netdev->flags & IFF_PROMISC)
334 maccr |= FTGMAC100_MACCR_RX_ALL;
335 if (priv->netdev->flags & IFF_ALLMULTI)
336 maccr |= FTGMAC100_MACCR_RX_MULTIPKT;
337 else if (netdev_mc_count(priv->netdev))
338 maccr |= FTGMAC100_MACCR_HT_MULTI_EN;
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000339
Benjamin Herrenschmidt0fb99682017-04-18 08:37:01 +1000340 /* Vlan filtering enabled */
341 if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
342 maccr |= FTGMAC100_MACCR_RM_VLAN;
343
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000344 /* Hit the HW */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000345 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
346}
347
348static void ftgmac100_stop_hw(struct ftgmac100 *priv)
349{
350 iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
351}
352
Benjamin Herrenschmidtf48b3c02017-04-18 08:37:00 +1000353static void ftgmac100_calc_mc_hash(struct ftgmac100 *priv)
354{
355 struct netdev_hw_addr *ha;
356
357 priv->maht1 = 0;
358 priv->maht0 = 0;
359 netdev_for_each_mc_addr(ha, priv->netdev) {
360 u32 crc_val = ether_crc_le(ETH_ALEN, ha->addr);
361
362 crc_val = (~(crc_val >> 2)) & 0x3f;
363 if (crc_val >= 32)
364 priv->maht1 |= 1ul << (crc_val - 32);
365 else
366 priv->maht0 |= 1ul << (crc_val);
367 }
368}
369
370static void ftgmac100_set_rx_mode(struct net_device *netdev)
371{
372 struct ftgmac100 *priv = netdev_priv(netdev);
373
374 /* Setup the hash filter */
375 ftgmac100_calc_mc_hash(priv);
376
377 /* Interface down ? that's all there is to do */
378 if (!netif_running(netdev))
379 return;
380
381 /* Update the HW */
382 iowrite32(priv->maht0, priv->base + FTGMAC100_OFFSET_MAHT0);
383 iowrite32(priv->maht1, priv->base + FTGMAC100_OFFSET_MAHT1);
384
385 /* Reconfigure MACCR */
386 ftgmac100_start_hw(priv);
387}
388
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000389static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry,
390 struct ftgmac100_rxdes *rxdes, gfp_t gfp)
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000391{
392 struct net_device *netdev = priv->netdev;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000393 struct sk_buff *skb;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000394 dma_addr_t map;
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000395 int err;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000396
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000397 skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE);
398 if (unlikely(!skb)) {
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000399 if (net_ratelimit())
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000400 netdev_warn(netdev, "failed to allocate rx skb\n");
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000401 err = -ENOMEM;
402 map = priv->rx_scratch_dma;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000403 } else {
404 map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE,
405 DMA_FROM_DEVICE);
406 if (unlikely(dma_mapping_error(priv->dev, map))) {
407 if (net_ratelimit())
408 netdev_err(netdev, "failed to map rx page\n");
409 dev_kfree_skb_any(skb);
410 map = priv->rx_scratch_dma;
411 skb = NULL;
412 err = -ENOMEM;
413 }
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000414 }
415
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000416 /* Store skb */
417 priv->rx_skbs[entry] = skb;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000418
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000419 /* Store DMA address into RX desc */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000420 rxdes->rxdes3 = cpu_to_le32(map);
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000421
422 /* Ensure the above is ordered vs clearing the OWN bit */
423 dma_wmb();
424
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000425 /* Clean status (which resets own bit) */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000426 if (entry == (priv->rx_q_entries - 1))
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000427 rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask);
428 else
429 rxdes->rxdes0 = 0;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000430
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000431 return 0;
432}
433
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000434static unsigned int ftgmac100_next_rx_pointer(struct ftgmac100 *priv,
435 unsigned int pointer)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000436{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000437 return (pointer + 1) & (priv->rx_q_entries - 1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000438}
439
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000440static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000441{
442 struct net_device *netdev = priv->netdev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000443
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000444 if (status & FTGMAC100_RXDES0_RX_ERR)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000445 netdev->stats.rx_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000446
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000447 if (status & FTGMAC100_RXDES0_CRC_ERR)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000448 netdev->stats.rx_crc_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000449
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000450 if (status & (FTGMAC100_RXDES0_FTL |
451 FTGMAC100_RXDES0_RUNT |
452 FTGMAC100_RXDES0_RX_ODD_NB))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000453 netdev->stats.rx_length_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000454}
455
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000456static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
457{
458 struct net_device *netdev = priv->netdev;
459 struct ftgmac100_rxdes *rxdes;
460 struct sk_buff *skb;
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000461 unsigned int pointer, size;
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000462 u32 status, csum_vlan;
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000463 dma_addr_t map;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000464
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000465 /* Grab next RX descriptor */
466 pointer = priv->rx_pointer;
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000467 rxdes = &priv->rxdes[pointer];
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000468
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000469 /* Grab descriptor status */
470 status = le32_to_cpu(rxdes->rxdes0);
471
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000472 /* Do we have a packet ? */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000473 if (!(status & FTGMAC100_RXDES0_RXPKT_RDY))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000474 return false;
475
Benjamin Herrenschmidt027f4262017-04-06 11:02:50 +1000476 /* Order subsequent reads with the test for the ready bit */
477 dma_rmb();
478
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000479 /* We don't cope with fragmented RX packets */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000480 if (unlikely(!(status & FTGMAC100_RXDES0_FRS) ||
481 !(status & FTGMAC100_RXDES0_LRS)))
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000482 goto drop;
483
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000484 /* Grab received size and csum vlan field in the descriptor */
485 size = status & FTGMAC100_RXDES0_VDBC;
486 csum_vlan = le32_to_cpu(rxdes->rxdes1);
487
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000488 /* Any error (other than csum offload) flagged ? */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000489 if (unlikely(status & RXDES0_ANY_ERROR)) {
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000490 /* Correct for incorrect flagging of runt packets
491 * with vlan tags... Just accept a runt packet that
492 * has been flagged as vlan and whose size is at
493 * least 60 bytes.
494 */
495 if ((status & FTGMAC100_RXDES0_RUNT) &&
496 (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) &&
497 (size >= 60))
498 status &= ~FTGMAC100_RXDES0_RUNT;
499
500 /* Any error still in there ? */
501 if (status & RXDES0_ANY_ERROR) {
502 ftgmac100_rx_packet_error(priv, status);
503 goto drop;
504 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000505 }
506
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000507 /* If the packet had no skb (failed to allocate earlier)
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000508 * then try to allocate one and skip
509 */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000510 skb = priv->rx_skbs[pointer];
511 if (!unlikely(skb)) {
512 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000513 goto drop;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000514 }
515
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000516 if (unlikely(status & FTGMAC100_RXDES0_MULTICAST))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000517 netdev->stats.multicast++;
518
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000519 /* If the HW found checksum errors, bounce it to software.
520 *
521 * If we didn't, we need to see if the packet was recognized
522 * by HW as one of the supported checksummed protocols before
523 * we accept the HW test results.
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000524 */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000525 if (netdev->features & NETIF_F_RXCSUM) {
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000526 u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
527 FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
528 FTGMAC100_RXDES1_IP_CHKSUM_ERR;
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000529 if ((csum_vlan & err_bits) ||
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000530 !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK))
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000531 skb->ip_summed = CHECKSUM_NONE;
532 else
533 skb->ip_summed = CHECKSUM_UNNECESSARY;
534 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000535
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000536 /* Transfer received size to skb */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000537 skb_put(skb, size);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000538
Benjamin Herrenschmidt0fb99682017-04-18 08:37:01 +1000539 /* Extract vlan tag */
540 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
541 (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL))
542 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
543 csum_vlan & 0xffff);
544
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000545 /* Tear down DMA mapping, do necessary cache management */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000546 map = le32_to_cpu(rxdes->rxdes3);
547
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000548#if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU)
549 /* When we don't have an iommu, we can save cycles by not
550 * invalidating the cache for the part of the packet that
551 * wasn't received.
552 */
553 dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE);
554#else
555 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
556#endif
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000557
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000558
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000559 /* Resplenish rx ring */
560 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000561 priv->rx_pointer = ftgmac100_next_rx_pointer(priv, pointer);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000562
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000563 skb->protocol = eth_type_trans(skb, netdev);
564
565 netdev->stats.rx_packets++;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000566 netdev->stats.rx_bytes += size;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000567
568 /* push packet to protocol stack */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000569 if (skb->ip_summed == CHECKSUM_NONE)
570 netif_receive_skb(skb);
571 else
572 napi_gro_receive(&priv->napi, skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000573
574 (*processed)++;
575 return true;
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000576
577 drop:
578 /* Clean rxdes0 (which resets own bit) */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000579 rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask);
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000580 priv->rx_pointer = ftgmac100_next_rx_pointer(priv, pointer);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000581 netdev->stats.rx_dropped++;
582 return true;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000583}
584
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000585static u32 ftgmac100_base_tx_ctlstat(struct ftgmac100 *priv,
586 unsigned int index)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000587{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000588 if (index == (priv->tx_q_entries - 1))
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000589 return priv->txdes0_edotr_mask;
590 else
591 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000592}
593
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000594static unsigned int ftgmac100_next_tx_pointer(struct ftgmac100 *priv,
595 unsigned int pointer)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000596{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000597 return (pointer + 1) & (priv->tx_q_entries - 1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000598}
599
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000600static u32 ftgmac100_tx_buf_avail(struct ftgmac100 *priv)
601{
602 /* Returns the number of available slots in the TX queue
603 *
604 * This always leaves one free slot so we don't have to
605 * worry about empty vs. full, and this simplifies the
606 * test for ftgmac100_tx_buf_cleanable() below
607 */
608 return (priv->tx_clean_pointer - priv->tx_pointer - 1) &
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000609 (priv->tx_q_entries - 1);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000610}
611
612static bool ftgmac100_tx_buf_cleanable(struct ftgmac100 *priv)
613{
614 return priv->tx_pointer != priv->tx_clean_pointer;
615}
616
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000617static void ftgmac100_free_tx_packet(struct ftgmac100 *priv,
618 unsigned int pointer,
619 struct sk_buff *skb,
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000620 struct ftgmac100_txdes *txdes,
621 u32 ctl_stat)
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000622{
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000623 dma_addr_t map = le32_to_cpu(txdes->txdes3);
624 size_t len;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000625
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000626 if (ctl_stat & FTGMAC100_TXDES0_FTS) {
627 len = skb_headlen(skb);
628 dma_unmap_single(priv->dev, map, len, DMA_TO_DEVICE);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000629 } else {
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000630 len = FTGMAC100_TXDES0_TXBUF_SIZE(ctl_stat);
631 dma_unmap_page(priv->dev, map, len, DMA_TO_DEVICE);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000632 }
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000633
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000634 /* Free SKB on last segment */
635 if (ctl_stat & FTGMAC100_TXDES0_LTS)
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000636 dev_kfree_skb(skb);
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000637 priv->tx_skbs[pointer] = NULL;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000638}
639
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000640static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
641{
642 struct net_device *netdev = priv->netdev;
643 struct ftgmac100_txdes *txdes;
644 struct sk_buff *skb;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000645 unsigned int pointer;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000646 u32 ctl_stat;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000647
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000648 pointer = priv->tx_clean_pointer;
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000649 txdes = &priv->txdes[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000650
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000651 ctl_stat = le32_to_cpu(txdes->txdes0);
652 if (ctl_stat & FTGMAC100_TXDES0_TXDMA_OWN)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000653 return false;
654
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000655 skb = priv->tx_skbs[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000656 netdev->stats.tx_packets++;
657 netdev->stats.tx_bytes += skb->len;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000658 ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
659 txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000660
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000661 priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv, pointer);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000662
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000663 return true;
664}
665
666static void ftgmac100_tx_complete(struct ftgmac100 *priv)
667{
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000668 struct net_device *netdev = priv->netdev;
669
670 /* Process all completed packets */
671 while (ftgmac100_tx_buf_cleanable(priv) &&
672 ftgmac100_tx_complete_packet(priv))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000673 ;
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000674
675 /* Restart queue if needed */
676 smp_mb();
677 if (unlikely(netif_queue_stopped(netdev) &&
678 ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)) {
679 struct netdev_queue *txq;
680
681 txq = netdev_get_tx_queue(netdev, 0);
682 __netif_tx_lock(txq, smp_processor_id());
683 if (netif_queue_stopped(netdev) &&
684 ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
685 netif_wake_queue(netdev);
686 __netif_tx_unlock(txq);
687 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000688}
689
Benjamin Herrenschmidt05690d62017-04-12 13:27:01 +1000690static bool ftgmac100_prep_tx_csum(struct sk_buff *skb, u32 *csum_vlan)
691{
692 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
693 u8 ip_proto = ip_hdr(skb)->protocol;
694
695 *csum_vlan |= FTGMAC100_TXDES1_IP_CHKSUM;
696 switch(ip_proto) {
697 case IPPROTO_TCP:
698 *csum_vlan |= FTGMAC100_TXDES1_TCP_CHKSUM;
699 return true;
700 case IPPROTO_UDP:
701 *csum_vlan |= FTGMAC100_TXDES1_UDP_CHKSUM;
702 return true;
703 case IPPROTO_IP:
704 return true;
705 }
706 }
707 return skb_checksum_help(skb) == 0;
708}
709
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000710static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
711 struct net_device *netdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000712{
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000713 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000714 struct ftgmac100_txdes *txdes, *first;
715 unsigned int pointer, nfrags, len, i, j;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000716 u32 f_ctl_stat, ctl_stat, csum_vlan;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000717 dma_addr_t map;
718
Benjamin Herrenschmidt9b0f7712017-04-10 11:15:19 +1000719 /* The HW doesn't pad small frames */
720 if (eth_skb_pad(skb)) {
721 netdev->stats.tx_dropped++;
722 return NETDEV_TX_OK;
723 }
724
725 /* Reject oversize packets */
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000726 if (unlikely(skb->len > MAX_PKT_SIZE)) {
727 if (net_ratelimit())
728 netdev_dbg(netdev, "tx packet too big\n");
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000729 goto drop;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000730 }
731
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000732 /* Do we have a limit on #fragments ? I yet have to get a reply
733 * from Aspeed. If there's one I haven't hit it.
734 */
735 nfrags = skb_shinfo(skb)->nr_frags;
736
737 /* Get header len */
738 len = skb_headlen(skb);
739
740 /* Map the packet head */
741 map = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE);
742 if (dma_mapping_error(priv->dev, map)) {
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000743 if (net_ratelimit())
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000744 netdev_err(netdev, "map tx packet head failed\n");
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000745 goto drop;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000746 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000747
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000748 /* Grab the next free tx descriptor */
749 pointer = priv->tx_pointer;
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000750 txdes = first = &priv->txdes[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000751
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000752 /* Setup it up with the packet head. Don't write the head to the
753 * ring just yet
754 */
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000755 priv->tx_skbs[pointer] = skb;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000756 f_ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
757 f_ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
758 f_ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
759 f_ctl_stat |= FTGMAC100_TXDES0_FTS;
760 if (nfrags == 0)
761 f_ctl_stat |= FTGMAC100_TXDES0_LTS;
762 txdes->txdes3 = cpu_to_le32(map);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000763
764 /* Setup HW checksumming */
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000765 csum_vlan = 0;
Benjamin Herrenschmidt05690d62017-04-12 13:27:01 +1000766 if (skb->ip_summed == CHECKSUM_PARTIAL &&
767 !ftgmac100_prep_tx_csum(skb, &csum_vlan))
768 goto drop;
Benjamin Herrenschmidt0fb99682017-04-18 08:37:01 +1000769
770 /* Add VLAN tag */
771 if (skb_vlan_tag_present(skb)) {
772 csum_vlan |= FTGMAC100_TXDES1_INS_VLANTAG;
773 csum_vlan |= skb_vlan_tag_get(skb) & 0xffff;
774 }
775
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000776 txdes->txdes1 = cpu_to_le32(csum_vlan);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000777
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000778 /* Next descriptor */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000779 pointer = ftgmac100_next_tx_pointer(priv, pointer);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000780
781 /* Add the fragments */
782 for (i = 0; i < nfrags; i++) {
783 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
784
785 len = frag->size;
786
787 /* Map it */
788 map = skb_frag_dma_map(priv->dev, frag, 0, len,
789 DMA_TO_DEVICE);
790 if (dma_mapping_error(priv->dev, map))
791 goto dma_err;
792
793 /* Setup descriptor */
794 priv->tx_skbs[pointer] = skb;
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000795 txdes = &priv->txdes[pointer];
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000796 ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
797 ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
798 ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
799 if (i == (nfrags - 1))
800 ctl_stat |= FTGMAC100_TXDES0_LTS;
801 txdes->txdes0 = cpu_to_le32(ctl_stat);
802 txdes->txdes1 = 0;
803 txdes->txdes3 = cpu_to_le32(map);
804
805 /* Next one */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000806 pointer = ftgmac100_next_tx_pointer(priv, pointer);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000807 }
808
Benjamin Herrenschmidt4a2712b2017-04-10 11:15:22 +1000809 /* Order the previous packet and descriptor udpates
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000810 * before setting the OWN bit on the first descriptor.
Benjamin Herrenschmidt4a2712b2017-04-10 11:15:22 +1000811 */
812 dma_wmb();
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000813 first->txdes0 = cpu_to_le32(f_ctl_stat);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000814
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000815 /* Update next TX pointer */
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000816 priv->tx_pointer = pointer;
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000817
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000818 /* If there isn't enough room for all the fragments of a new packet
819 * in the TX ring, stop the queue. The sequence below is race free
820 * vs. a concurrent restart in ftgmac100_poll()
821 */
822 if (unlikely(ftgmac100_tx_buf_avail(priv) < TX_THRESHOLD)) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000823 netif_stop_queue(netdev);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000824 /* Order the queue stop with the test below */
825 smp_mb();
826 if (ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
827 netif_wake_queue(netdev);
828 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000829
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000830 /* Poke transmitter to read the updated TX descriptors */
831 iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000832
833 return NETDEV_TX_OK;
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000834
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000835 dma_err:
836 if (net_ratelimit())
837 netdev_err(netdev, "map tx fragment failed\n");
838
839 /* Free head */
840 pointer = priv->tx_pointer;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000841 ftgmac100_free_tx_packet(priv, pointer, skb, first, f_ctl_stat);
842 first->txdes0 = cpu_to_le32(f_ctl_stat & priv->txdes0_edotr_mask);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000843
844 /* Then all fragments */
845 for (j = 0; j < i; j++) {
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000846 pointer = ftgmac100_next_tx_pointer(priv, pointer);
847 txdes = &priv->txdes[pointer];
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000848 ctl_stat = le32_to_cpu(txdes->txdes0);
849 ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
850 txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000851 }
852
853 /* This cannot be reached if we successfully mapped the
854 * last fragment, so we know ftgmac100_free_tx_packet()
855 * hasn't freed the skb yet.
856 */
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000857 drop:
858 /* Drop the packet */
859 dev_kfree_skb_any(skb);
860 netdev->stats.tx_dropped++;
861
862 return NETDEV_TX_OK;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000863}
864
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000865static void ftgmac100_free_buffers(struct ftgmac100 *priv)
866{
867 int i;
868
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000869 /* Free all RX buffers */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000870 for (i = 0; i < priv->rx_q_entries; i++) {
871 struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000872 struct sk_buff *skb = priv->rx_skbs[i];
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000873 dma_addr_t map = le32_to_cpu(rxdes->rxdes3);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000874
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000875 if (!skb)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000876 continue;
877
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000878 priv->rx_skbs[i] = NULL;
879 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
880 dev_kfree_skb_any(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000881 }
882
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000883 /* Free all TX buffers */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000884 for (i = 0; i < priv->tx_q_entries; i++) {
885 struct ftgmac100_txdes *txdes = &priv->txdes[i];
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000886 struct sk_buff *skb = priv->tx_skbs[i];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000887
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000888 if (!skb)
889 continue;
890 ftgmac100_free_tx_packet(priv, i, skb, txdes,
891 le32_to_cpu(txdes->txdes0));
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000892 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000893}
894
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000895static void ftgmac100_free_rings(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000896{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000897 /* Free skb arrays */
898 kfree(priv->rx_skbs);
899 kfree(priv->tx_skbs);
900
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000901 /* Free descriptors */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000902 if (priv->rxdes)
903 dma_free_coherent(priv->dev, MAX_RX_QUEUE_ENTRIES *
904 sizeof(struct ftgmac100_rxdes),
905 priv->rxdes, priv->rxdes_dma);
906 priv->rxdes = NULL;
907
908 if (priv->txdes)
909 dma_free_coherent(priv->dev, MAX_TX_QUEUE_ENTRIES *
910 sizeof(struct ftgmac100_txdes),
911 priv->txdes, priv->txdes_dma);
912 priv->txdes = NULL;
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000913
914 /* Free scratch packet buffer */
915 if (priv->rx_scratch)
916 dma_free_coherent(priv->dev, RX_BUF_SIZE,
917 priv->rx_scratch, priv->rx_scratch_dma);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000918}
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000919
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000920static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
921{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000922 /* Allocate skb arrays */
923 priv->rx_skbs = kcalloc(MAX_RX_QUEUE_ENTRIES, sizeof(void *),
924 GFP_KERNEL);
925 if (!priv->rx_skbs)
926 return -ENOMEM;
927 priv->tx_skbs = kcalloc(MAX_TX_QUEUE_ENTRIES, sizeof(void *),
928 GFP_KERNEL);
929 if (!priv->tx_skbs)
930 return -ENOMEM;
931
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000932 /* Allocate descriptors */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000933 priv->rxdes = dma_zalloc_coherent(priv->dev,
934 MAX_RX_QUEUE_ENTRIES *
935 sizeof(struct ftgmac100_rxdes),
936 &priv->rxdes_dma, GFP_KERNEL);
937 if (!priv->rxdes)
938 return -ENOMEM;
939 priv->txdes = dma_zalloc_coherent(priv->dev,
940 MAX_TX_QUEUE_ENTRIES *
941 sizeof(struct ftgmac100_txdes),
942 &priv->txdes_dma, GFP_KERNEL);
943 if (!priv->txdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000944 return -ENOMEM;
945
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000946 /* Allocate scratch packet buffer */
947 priv->rx_scratch = dma_alloc_coherent(priv->dev,
948 RX_BUF_SIZE,
949 &priv->rx_scratch_dma,
950 GFP_KERNEL);
951 if (!priv->rx_scratch)
952 return -ENOMEM;
953
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000954 return 0;
955}
956
957static void ftgmac100_init_rings(struct ftgmac100 *priv)
958{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000959 struct ftgmac100_rxdes *rxdes = NULL;
960 struct ftgmac100_txdes *txdes = NULL;
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000961 int i;
962
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000963 /* Update entries counts */
964 priv->rx_q_entries = priv->new_rx_q_entries;
965 priv->tx_q_entries = priv->new_tx_q_entries;
966
967 if (WARN_ON(priv->rx_q_entries < MIN_RX_QUEUE_ENTRIES))
968 return;
969
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000970 /* Initialize RX ring */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000971 for (i = 0; i < priv->rx_q_entries; i++) {
972 rxdes = &priv->rxdes[i];
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000973 rxdes->rxdes0 = 0;
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000974 rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma);
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000975 }
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000976 /* Mark the end of the ring */
977 rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000978
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000979 if (WARN_ON(priv->tx_q_entries < MIN_RX_QUEUE_ENTRIES))
980 return;
981
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000982 /* Initialize TX ring */
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000983 for (i = 0; i < priv->tx_q_entries; i++) {
984 txdes = &priv->txdes[i];
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000985 txdes->txdes0 = 0;
986 }
987 txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000988}
989
990static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
991{
992 int i;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000993
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +1000994 for (i = 0; i < priv->rx_q_entries; i++) {
995 struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000996
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000997 if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL))
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000998 return -ENOMEM;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000999 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001000 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001001}
1002
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001003static void ftgmac100_adjust_link(struct net_device *netdev)
1004{
1005 struct ftgmac100 *priv = netdev_priv(netdev);
Philippe Reynesb3c40ad2016-05-16 01:35:13 +02001006 struct phy_device *phydev = netdev->phydev;
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001007 bool tx_pause, rx_pause;
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001008 int new_speed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001009
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001010 /* We store "no link" as speed 0 */
1011 if (!phydev->link)
1012 new_speed = 0;
1013 else
1014 new_speed = phydev->speed;
1015
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001016 /* Grab pause settings from PHY if configured to do so */
1017 if (priv->aneg_pause) {
1018 rx_pause = tx_pause = phydev->pause;
1019 if (phydev->asym_pause)
1020 tx_pause = !rx_pause;
1021 } else {
1022 rx_pause = priv->rx_pause;
1023 tx_pause = priv->tx_pause;
1024 }
1025
1026 /* Link hasn't changed, do nothing */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001027 if (phydev->speed == priv->cur_speed &&
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001028 phydev->duplex == priv->cur_duplex &&
1029 rx_pause == priv->rx_pause &&
1030 tx_pause == priv->tx_pause)
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001031 return;
1032
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001033 /* Print status if we have a link or we had one and just lost it,
1034 * don't print otherwise.
1035 */
1036 if (new_speed || priv->cur_speed)
1037 phy_print_status(phydev);
1038
1039 priv->cur_speed = new_speed;
1040 priv->cur_duplex = phydev->duplex;
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001041 priv->rx_pause = rx_pause;
1042 priv->tx_pause = tx_pause;
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001043
1044 /* Link is down, do nothing else */
1045 if (!new_speed)
1046 return;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001047
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001048 /* Disable all interrupts */
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001049 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1050
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001051 /* Reset the adapter asynchronously */
1052 schedule_work(&priv->reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001053}
1054
Benjamin Herrenschmidtabcc3eb2017-04-18 08:37:03 +10001055static int ftgmac100_mii_probe(struct ftgmac100 *priv, phy_interface_t intf)
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001056{
1057 struct net_device *netdev = priv->netdev;
Guenter Roecke574f392016-01-10 12:04:32 -08001058 struct phy_device *phydev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001059
Guenter Roecke574f392016-01-10 12:04:32 -08001060 phydev = phy_find_first(priv->mii_bus);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001061 if (!phydev) {
1062 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
1063 return -ENODEV;
1064 }
1065
Andrew Lunn84eff6d2016-01-06 20:11:10 +01001066 phydev = phy_connect(netdev, phydev_name(phydev),
Benjamin Herrenschmidtabcc3eb2017-04-18 08:37:03 +10001067 &ftgmac100_adjust_link, intf);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001068
1069 if (IS_ERR(phydev)) {
1070 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
1071 return PTR_ERR(phydev);
1072 }
1073
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001074 /* Indicate that we support PAUSE frames (see comment in
1075 * Documentation/networking/phy.txt)
1076 */
1077 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1078 phydev->advertising = phydev->supported;
1079
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001080 return 0;
1081}
1082
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001083static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
1084{
1085 struct net_device *netdev = bus->priv;
1086 struct ftgmac100 *priv = netdev_priv(netdev);
1087 unsigned int phycr;
1088 int i;
1089
1090 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1091
1092 /* preserve MDC cycle threshold */
1093 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
1094
1095 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
1096 FTGMAC100_PHYCR_REGAD(regnum) |
1097 FTGMAC100_PHYCR_MIIRD;
1098
1099 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
1100
1101 for (i = 0; i < 10; i++) {
1102 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1103
1104 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
1105 int data;
1106
1107 data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
1108 return FTGMAC100_PHYDATA_MIIRDATA(data);
1109 }
1110
1111 udelay(100);
1112 }
1113
1114 netdev_err(netdev, "mdio read timed out\n");
1115 return -EIO;
1116}
1117
1118static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
1119 int regnum, u16 value)
1120{
1121 struct net_device *netdev = bus->priv;
1122 struct ftgmac100 *priv = netdev_priv(netdev);
1123 unsigned int phycr;
1124 int data;
1125 int i;
1126
1127 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1128
1129 /* preserve MDC cycle threshold */
1130 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
1131
1132 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
1133 FTGMAC100_PHYCR_REGAD(regnum) |
1134 FTGMAC100_PHYCR_MIIWR;
1135
1136 data = FTGMAC100_PHYDATA_MIIWDATA(value);
1137
1138 iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
1139 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
1140
1141 for (i = 0; i < 10; i++) {
1142 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1143
1144 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
1145 return 0;
1146
1147 udelay(100);
1148 }
1149
1150 netdev_err(netdev, "mdio write timed out\n");
1151 return -EIO;
1152}
1153
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001154static void ftgmac100_get_drvinfo(struct net_device *netdev,
1155 struct ethtool_drvinfo *info)
1156{
Jiri Pirko7826d432013-01-06 00:44:26 +00001157 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1158 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1159 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001160}
1161
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +10001162static void ftgmac100_get_ringparam(struct net_device *netdev,
1163 struct ethtool_ringparam *ering)
1164{
1165 struct ftgmac100 *priv = netdev_priv(netdev);
1166
1167 memset(ering, 0, sizeof(*ering));
1168 ering->rx_max_pending = MAX_RX_QUEUE_ENTRIES;
1169 ering->tx_max_pending = MAX_TX_QUEUE_ENTRIES;
1170 ering->rx_pending = priv->rx_q_entries;
1171 ering->tx_pending = priv->tx_q_entries;
1172}
1173
1174static int ftgmac100_set_ringparam(struct net_device *netdev,
1175 struct ethtool_ringparam *ering)
1176{
1177 struct ftgmac100 *priv = netdev_priv(netdev);
1178
1179 if (ering->rx_pending > MAX_RX_QUEUE_ENTRIES ||
1180 ering->tx_pending > MAX_TX_QUEUE_ENTRIES ||
1181 ering->rx_pending < MIN_RX_QUEUE_ENTRIES ||
1182 ering->tx_pending < MIN_TX_QUEUE_ENTRIES ||
1183 !is_power_of_2(ering->rx_pending) ||
1184 !is_power_of_2(ering->tx_pending))
1185 return -EINVAL;
1186
1187 priv->new_rx_q_entries = ering->rx_pending;
1188 priv->new_tx_q_entries = ering->tx_pending;
1189 if (netif_running(netdev))
1190 schedule_work(&priv->reset_task);
1191
1192 return 0;
1193}
1194
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001195static void ftgmac100_get_pauseparam(struct net_device *netdev,
1196 struct ethtool_pauseparam *pause)
1197{
1198 struct ftgmac100 *priv = netdev_priv(netdev);
1199
1200 pause->autoneg = priv->aneg_pause;
1201 pause->tx_pause = priv->tx_pause;
1202 pause->rx_pause = priv->rx_pause;
1203}
1204
1205static int ftgmac100_set_pauseparam(struct net_device *netdev,
1206 struct ethtool_pauseparam *pause)
1207{
1208 struct ftgmac100 *priv = netdev_priv(netdev);
1209 struct phy_device *phydev = netdev->phydev;
1210
1211 priv->aneg_pause = pause->autoneg;
1212 priv->tx_pause = pause->tx_pause;
1213 priv->rx_pause = pause->rx_pause;
1214
1215 if (phydev) {
1216 phydev->advertising &= ~ADVERTISED_Pause;
1217 phydev->advertising &= ~ADVERTISED_Asym_Pause;
1218
1219 if (pause->rx_pause) {
1220 phydev->advertising |= ADVERTISED_Pause;
1221 phydev->advertising |= ADVERTISED_Asym_Pause;
1222 }
1223
1224 if (pause->tx_pause)
1225 phydev->advertising ^= ADVERTISED_Asym_Pause;
1226 }
1227 if (netif_running(netdev)) {
1228 if (phydev && priv->aneg_pause)
1229 phy_start_aneg(phydev);
1230 else
1231 ftgmac100_config_pause(priv);
1232 }
1233
1234 return 0;
1235}
1236
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001237static const struct ethtool_ops ftgmac100_ethtool_ops = {
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001238 .get_drvinfo = ftgmac100_get_drvinfo,
1239 .get_link = ethtool_op_get_link,
Philippe Reynesfd24d722016-05-16 01:35:14 +02001240 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1241 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Benjamin Herrenschmidte98233a2017-04-18 08:36:58 +10001242 .nway_reset = phy_ethtool_nway_reset,
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +10001243 .get_ringparam = ftgmac100_get_ringparam,
1244 .set_ringparam = ftgmac100_set_ringparam,
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001245 .get_pauseparam = ftgmac100_get_pauseparam,
1246 .set_pauseparam = ftgmac100_set_pauseparam,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001247};
1248
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001249static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
1250{
1251 struct net_device *netdev = dev_id;
1252 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001253 unsigned int status, new_mask = FTGMAC100_INT_BAD;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001254
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001255 /* Fetch and clear interrupt bits, process abnormal ones */
1256 status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1257 iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
1258 if (unlikely(status & FTGMAC100_INT_BAD)) {
1259
1260 /* RX buffer unavailable */
1261 if (status & FTGMAC100_INT_NO_RXBUF)
1262 netdev->stats.rx_over_errors++;
1263
1264 /* received packet lost due to RX FIFO full */
1265 if (status & FTGMAC100_INT_RPKT_LOST)
1266 netdev->stats.rx_fifo_errors++;
1267
1268 /* sent packet lost due to excessive TX collision */
1269 if (status & FTGMAC100_INT_XPKT_LOST)
1270 netdev->stats.tx_fifo_errors++;
1271
1272 /* AHB error -> Reset the chip */
1273 if (status & FTGMAC100_INT_AHB_ERR) {
1274 if (net_ratelimit())
1275 netdev_warn(netdev,
1276 "AHB bus error ! Resetting chip.\n");
1277 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1278 schedule_work(&priv->reset_task);
1279 return IRQ_HANDLED;
1280 }
1281
1282 /* We may need to restart the MAC after such errors, delay
1283 * this until after we have freed some Rx buffers though
1284 */
1285 priv->need_mac_restart = true;
1286
1287 /* Disable those errors until we restart */
1288 new_mask &= ~status;
1289 }
1290
1291 /* Only enable "bad" interrupts while NAPI is on */
1292 iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
1293
1294 /* Schedule NAPI bh */
1295 napi_schedule_irqoff(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001296
1297 return IRQ_HANDLED;
1298}
1299
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +10001300static bool ftgmac100_check_rx(struct ftgmac100 *priv)
1301{
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +10001302 struct ftgmac100_rxdes *rxdes = &priv->rxdes[priv->rx_pointer];
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +10001303
1304 /* Do we have a packet ? */
1305 return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY));
1306}
1307
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001308static int ftgmac100_poll(struct napi_struct *napi, int budget)
1309{
1310 struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001311 int work_done = 0;
1312 bool more;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001313
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001314 /* Handle TX completions */
1315 if (ftgmac100_tx_buf_cleanable(priv))
1316 ftgmac100_tx_complete(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001317
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001318 /* Handle RX packets */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001319 do {
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001320 more = ftgmac100_rx_packet(priv, &work_done);
1321 } while (more && work_done < budget);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001322
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001323
1324 /* The interrupt is telling us to kick the MAC back to life
1325 * after an RX overflow
1326 */
1327 if (unlikely(priv->need_mac_restart)) {
1328 ftgmac100_start_hw(priv);
1329
1330 /* Re-enable "bad" interrupts */
1331 iowrite32(FTGMAC100_INT_BAD,
1332 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001333 }
1334
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001335 /* As long as we are waiting for transmit packets to be
1336 * completed we keep NAPI going
1337 */
1338 if (ftgmac100_tx_buf_cleanable(priv))
1339 work_done = budget;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001340
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001341 if (work_done < budget) {
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001342 /* We are about to re-enable all interrupts. However
1343 * the HW has been latching RX/TX packet interrupts while
1344 * they were masked. So we clear them first, then we need
1345 * to re-check if there's something to process
1346 */
1347 iowrite32(FTGMAC100_INT_RXTX,
1348 priv->base + FTGMAC100_OFFSET_ISR);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001349 if (ftgmac100_check_rx(priv) ||
1350 ftgmac100_tx_buf_cleanable(priv))
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001351 return budget;
1352
1353 /* deschedule NAPI */
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001354 napi_complete(napi);
1355
1356 /* enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001357 iowrite32(FTGMAC100_INT_ALL,
Gavin Shanfc6061c2016-07-19 11:54:25 +10001358 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001359 }
1360
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001361 return work_done;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001362}
1363
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001364static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
1365{
1366 int err = 0;
1367
1368 /* Re-init descriptors (adjust queue sizes) */
1369 ftgmac100_init_rings(priv);
1370
1371 /* Realloc rx descriptors */
1372 err = ftgmac100_alloc_rx_buffers(priv);
1373 if (err && !ignore_alloc_err)
1374 return err;
1375
1376 /* Reinit and restart HW */
1377 ftgmac100_init_hw(priv);
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001378 ftgmac100_config_pause(priv);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001379 ftgmac100_start_hw(priv);
1380
1381 /* Re-enable the device */
1382 napi_enable(&priv->napi);
1383 netif_start_queue(priv->netdev);
1384
1385 /* Enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001386 iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001387
1388 return err;
1389}
1390
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001391static void ftgmac100_reset_task(struct work_struct *work)
1392{
1393 struct ftgmac100 *priv = container_of(work, struct ftgmac100,
1394 reset_task);
1395 struct net_device *netdev = priv->netdev;
1396 int err;
1397
1398 netdev_dbg(netdev, "Resetting NIC...\n");
1399
1400 /* Lock the world */
1401 rtnl_lock();
1402 if (netdev->phydev)
1403 mutex_lock(&netdev->phydev->lock);
1404 if (priv->mii_bus)
1405 mutex_lock(&priv->mii_bus->mdio_lock);
1406
1407
1408 /* Check if the interface is still up */
1409 if (!netif_running(netdev))
1410 goto bail;
1411
1412 /* Stop the network stack */
1413 netif_trans_update(netdev);
1414 napi_disable(&priv->napi);
1415 netif_tx_disable(netdev);
1416
1417 /* Stop and reset the MAC */
1418 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001419 err = ftgmac100_reset_and_config_mac(priv);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001420 if (err) {
1421 /* Not much we can do ... it might come back... */
1422 netdev_err(netdev, "attempting to continue...\n");
1423 }
1424
1425 /* Free all rx and tx buffers */
1426 ftgmac100_free_buffers(priv);
1427
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001428 /* Setup everything again and restart chip */
1429 ftgmac100_init_all(priv, true);
1430
1431 netdev_dbg(netdev, "Reset done !\n");
1432 bail:
1433 if (priv->mii_bus)
1434 mutex_unlock(&priv->mii_bus->mdio_lock);
1435 if (netdev->phydev)
1436 mutex_unlock(&netdev->phydev->lock);
1437 rtnl_unlock();
1438}
1439
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001440static int ftgmac100_open(struct net_device *netdev)
1441{
1442 struct ftgmac100 *priv = netdev_priv(netdev);
1443 int err;
1444
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001445 /* Allocate ring buffers */
1446 err = ftgmac100_alloc_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001447 if (err) {
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001448 netdev_err(netdev, "Failed to allocate descriptors\n");
1449 return err;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001450 }
1451
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001452 /* When using NC-SI we force the speed to 100Mbit/s full duplex,
1453 *
1454 * Otherwise we leave it set to 0 (no link), the link
1455 * message from the PHY layer will handle setting it up to
1456 * something else if needed.
1457 */
1458 if (priv->use_ncsi) {
1459 priv->cur_duplex = DUPLEX_FULL;
1460 priv->cur_speed = SPEED_100;
1461 } else {
1462 priv->cur_duplex = 0;
1463 priv->cur_speed = 0;
1464 }
1465
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001466 /* Reset the hardware */
1467 err = ftgmac100_reset_and_config_mac(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001468 if (err)
1469 goto err_hw;
1470
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001471 /* Initialize NAPI */
1472 netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1473
Benjamin Herrenschmidt81f1eca2017-04-05 12:28:48 +10001474 /* Grab our interrupt */
1475 err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1476 if (err) {
1477 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
1478 goto err_irq;
1479 }
1480
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001481 /* Start things up */
1482 err = ftgmac100_init_all(priv, false);
1483 if (err) {
1484 netdev_err(netdev, "Failed to allocate packet buffers\n");
1485 goto err_alloc;
1486 }
Gavin Shan08c9c122016-09-22 08:35:01 +09301487
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001488 if (netdev->phydev) {
1489 /* If we have a PHY, start polling */
Gavin Shanbd466c32016-07-19 11:54:23 +10001490 phy_start(netdev->phydev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001491 } else if (priv->use_ncsi) {
1492 /* If using NC-SI, set our carrier on and start the stack */
Gavin Shanbd466c32016-07-19 11:54:23 +10001493 netif_carrier_on(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001494
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001495 /* Start the NCSI device */
Gavin Shanbd466c32016-07-19 11:54:23 +10001496 err = ncsi_start_dev(priv->ndev);
1497 if (err)
1498 goto err_ncsi;
1499 }
1500
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001501 return 0;
1502
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001503 err_ncsi:
Gavin Shanbd466c32016-07-19 11:54:23 +10001504 napi_disable(&priv->napi);
1505 netif_stop_queue(netdev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001506 err_alloc:
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001507 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001508 free_irq(netdev->irq, netdev);
1509 err_irq:
1510 netif_napi_del(&priv->napi);
1511 err_hw:
1512 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001513 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001514 return err;
1515}
1516
1517static int ftgmac100_stop(struct net_device *netdev)
1518{
1519 struct ftgmac100 *priv = netdev_priv(netdev);
1520
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001521 /* Note about the reset task: We are called with the rtnl lock
1522 * held, so we are synchronized against the core of the reset
1523 * task. We must not try to synchronously cancel it otherwise
1524 * we can deadlock. But since it will test for netif_running()
1525 * which has already been cleared by the net core, we don't
1526 * anything special to do.
1527 */
1528
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001529 /* disable all interrupts */
1530 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1531
1532 netif_stop_queue(netdev);
1533 napi_disable(&priv->napi);
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001534 netif_napi_del(&priv->napi);
Gavin Shanbd466c32016-07-19 11:54:23 +10001535 if (netdev->phydev)
1536 phy_stop(netdev->phydev);
Gavin Shan2c15f252016-10-04 11:25:54 +11001537 else if (priv->use_ncsi)
1538 ncsi_stop_dev(priv->ndev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001539
1540 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001541 free_irq(netdev->irq, netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001542 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001543 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001544
1545 return 0;
1546}
1547
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001548/* optional */
1549static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1550{
Gavin Shanbd466c32016-07-19 11:54:23 +10001551 if (!netdev->phydev)
1552 return -ENXIO;
1553
Philippe Reynesb3c40ad2016-05-16 01:35:13 +02001554 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001555}
1556
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001557static void ftgmac100_tx_timeout(struct net_device *netdev)
1558{
1559 struct ftgmac100 *priv = netdev_priv(netdev);
1560
1561 /* Disable all interrupts */
1562 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1563
1564 /* Do the reset outside of interrupt context */
1565 schedule_work(&priv->reset_task);
1566}
1567
Benjamin Herrenschmidt0fb99682017-04-18 08:37:01 +10001568static int ftgmac100_set_features(struct net_device *netdev,
1569 netdev_features_t features)
1570{
1571 struct ftgmac100 *priv = netdev_priv(netdev);
1572 netdev_features_t changed = netdev->features ^ features;
1573
1574 if (!netif_running(netdev))
1575 return 0;
1576
1577 /* Update the vlan filtering bit */
1578 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1579 u32 maccr;
1580
1581 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
1582 if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
1583 maccr |= FTGMAC100_MACCR_RM_VLAN;
1584 else
1585 maccr &= ~FTGMAC100_MACCR_RM_VLAN;
1586 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
1587 }
1588
1589 return 0;
1590}
1591
Benjamin Herrenschmidt030d9822017-04-18 08:37:02 +10001592#ifdef CONFIG_NET_POLL_CONTROLLER
1593static void ftgmac100_poll_controller(struct net_device *netdev)
1594{
1595 unsigned long flags;
1596
1597 local_irq_save(flags);
1598 ftgmac100_interrupt(netdev->irq, netdev);
1599 local_irq_restore(flags);
1600}
1601#endif
1602
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001603static const struct net_device_ops ftgmac100_netdev_ops = {
1604 .ndo_open = ftgmac100_open,
1605 .ndo_stop = ftgmac100_stop,
1606 .ndo_start_xmit = ftgmac100_hard_start_xmit,
Gavin Shan113ce102016-07-19 11:54:22 +10001607 .ndo_set_mac_address = ftgmac100_set_mac_addr,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001608 .ndo_validate_addr = eth_validate_addr,
1609 .ndo_do_ioctl = ftgmac100_do_ioctl,
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001610 .ndo_tx_timeout = ftgmac100_tx_timeout,
Benjamin Herrenschmidtf48b3c02017-04-18 08:37:00 +10001611 .ndo_set_rx_mode = ftgmac100_set_rx_mode,
Benjamin Herrenschmidt0fb99682017-04-18 08:37:01 +10001612 .ndo_set_features = ftgmac100_set_features,
Benjamin Herrenschmidt030d9822017-04-18 08:37:02 +10001613#ifdef CONFIG_NET_POLL_CONTROLLER
1614 .ndo_poll_controller = ftgmac100_poll_controller,
1615#endif
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001616};
1617
Gavin Shaneb418182016-07-19 11:54:21 +10001618static int ftgmac100_setup_mdio(struct net_device *netdev)
1619{
1620 struct ftgmac100 *priv = netdev_priv(netdev);
1621 struct platform_device *pdev = to_platform_device(priv->dev);
Benjamin Herrenschmidtabcc3eb2017-04-18 08:37:03 +10001622 int phy_intf = PHY_INTERFACE_MODE_RGMII;
1623 struct device_node *np = pdev->dev.of_node;
Gavin Shaneb418182016-07-19 11:54:21 +10001624 int i, err = 0;
Joel Stanleye07dc632016-09-22 08:35:02 +09301625 u32 reg;
Gavin Shaneb418182016-07-19 11:54:21 +10001626
1627 /* initialize mdio bus */
1628 priv->mii_bus = mdiobus_alloc();
1629 if (!priv->mii_bus)
1630 return -EIO;
1631
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001632 if (priv->is_aspeed) {
Joel Stanleye07dc632016-09-22 08:35:02 +09301633 /* This driver supports the old MDIO interface */
1634 reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
1635 reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
1636 iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
1637 };
1638
Benjamin Herrenschmidtabcc3eb2017-04-18 08:37:03 +10001639 /* Get PHY mode from device-tree */
1640 if (np) {
1641 /* Default to RGMII. It's a gigabit part after all */
1642 phy_intf = of_get_phy_mode(np);
1643 if (phy_intf < 0)
1644 phy_intf = PHY_INTERFACE_MODE_RGMII;
1645
1646 /* Aspeed only supports these. I don't know about other IP
1647 * block vendors so I'm going to just let them through for
1648 * now. Note that this is only a warning if for some obscure
1649 * reason the DT really means to lie about it or it's a newer
1650 * part we don't know about.
1651 *
1652 * On the Aspeed SoC there are additionally straps and SCU
1653 * control bits that could tell us what the interface is
1654 * (or allow us to configure it while the IP block is held
1655 * in reset). For now I chose to keep this driver away from
1656 * those SoC specific bits and assume the device-tree is
1657 * right and the SCU has been configured properly by pinmux
1658 * or the firmware.
1659 */
1660 if (priv->is_aspeed &&
1661 phy_intf != PHY_INTERFACE_MODE_RMII &&
1662 phy_intf != PHY_INTERFACE_MODE_RGMII &&
1663 phy_intf != PHY_INTERFACE_MODE_RGMII_ID &&
1664 phy_intf != PHY_INTERFACE_MODE_RGMII_RXID &&
1665 phy_intf != PHY_INTERFACE_MODE_RGMII_TXID) {
1666 netdev_warn(netdev,
1667 "Unsupported PHY mode %s !\n",
1668 phy_modes(phy_intf));
1669 }
1670 }
1671
Gavin Shaneb418182016-07-19 11:54:21 +10001672 priv->mii_bus->name = "ftgmac100_mdio";
1673 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1674 pdev->name, pdev->id);
1675 priv->mii_bus->priv = priv->netdev;
1676 priv->mii_bus->read = ftgmac100_mdiobus_read;
1677 priv->mii_bus->write = ftgmac100_mdiobus_write;
1678
1679 for (i = 0; i < PHY_MAX_ADDR; i++)
1680 priv->mii_bus->irq[i] = PHY_POLL;
1681
1682 err = mdiobus_register(priv->mii_bus);
1683 if (err) {
1684 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1685 goto err_register_mdiobus;
1686 }
1687
Benjamin Herrenschmidtabcc3eb2017-04-18 08:37:03 +10001688 err = ftgmac100_mii_probe(priv, phy_intf);
Gavin Shaneb418182016-07-19 11:54:21 +10001689 if (err) {
1690 dev_err(priv->dev, "MII Probe failed!\n");
1691 goto err_mii_probe;
1692 }
1693
1694 return 0;
1695
1696err_mii_probe:
1697 mdiobus_unregister(priv->mii_bus);
1698err_register_mdiobus:
1699 mdiobus_free(priv->mii_bus);
1700 return err;
1701}
1702
1703static void ftgmac100_destroy_mdio(struct net_device *netdev)
1704{
1705 struct ftgmac100 *priv = netdev_priv(netdev);
1706
1707 if (!netdev->phydev)
1708 return;
1709
1710 phy_disconnect(netdev->phydev);
1711 mdiobus_unregister(priv->mii_bus);
1712 mdiobus_free(priv->mii_bus);
1713}
1714
Gavin Shanbd466c32016-07-19 11:54:23 +10001715static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1716{
1717 if (unlikely(nd->state != ncsi_dev_state_functional))
1718 return;
1719
1720 netdev_info(nd->dev, "NCSI interface %s\n",
1721 nd->link_up ? "up" : "down");
1722}
1723
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001724static int ftgmac100_probe(struct platform_device *pdev)
1725{
1726 struct resource *res;
1727 int irq;
1728 struct net_device *netdev;
1729 struct ftgmac100 *priv;
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001730 struct device_node *np;
Gavin Shanbd466c32016-07-19 11:54:23 +10001731 int err = 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001732
1733 if (!pdev)
1734 return -ENODEV;
1735
1736 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1737 if (!res)
1738 return -ENXIO;
1739
1740 irq = platform_get_irq(pdev, 0);
1741 if (irq < 0)
1742 return irq;
1743
1744 /* setup net_device */
1745 netdev = alloc_etherdev(sizeof(*priv));
1746 if (!netdev) {
1747 err = -ENOMEM;
1748 goto err_alloc_etherdev;
1749 }
1750
1751 SET_NETDEV_DEV(netdev, &pdev->dev);
1752
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001753 netdev->ethtool_ops = &ftgmac100_ethtool_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001754 netdev->netdev_ops = &ftgmac100_netdev_ops;
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001755 netdev->watchdog_timeo = 5 * HZ;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001756
1757 platform_set_drvdata(pdev, netdev);
1758
1759 /* setup private data */
1760 priv = netdev_priv(netdev);
1761 priv->netdev = netdev;
1762 priv->dev = &pdev->dev;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001763 INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001764
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001765 /* map io memory */
1766 priv->res = request_mem_region(res->start, resource_size(res),
1767 dev_name(&pdev->dev));
1768 if (!priv->res) {
1769 dev_err(&pdev->dev, "Could not reserve memory region\n");
1770 err = -ENOMEM;
1771 goto err_req_mem;
1772 }
1773
1774 priv->base = ioremap(res->start, resource_size(res));
1775 if (!priv->base) {
1776 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1777 err = -EIO;
1778 goto err_ioremap;
1779 }
1780
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001781 netdev->irq = irq;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001782
Benjamin Herrenschmidt7c8e5142017-04-18 08:36:59 +10001783 /* Enable pause */
1784 priv->tx_pause = true;
1785 priv->rx_pause = true;
1786 priv->aneg_pause = true;
1787
Gavin Shan113ce102016-07-19 11:54:22 +10001788 /* MAC address from chip or random one */
Benjamin Herrenschmidtba1b1232017-04-12 13:27:06 +10001789 ftgmac100_initial_mac(priv);
Gavin Shan113ce102016-07-19 11:54:22 +10001790
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001791 np = pdev->dev.of_node;
1792 if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
1793 of_device_is_compatible(np, "aspeed,ast2500-mac"))) {
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301794 priv->rxdes0_edorr_mask = BIT(30);
1795 priv->txdes0_edotr_mask = BIT(30);
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001796 priv->is_aspeed = true;
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301797 } else {
1798 priv->rxdes0_edorr_mask = BIT(15);
1799 priv->txdes0_edotr_mask = BIT(15);
1800 }
1801
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001802 if (np && of_get_property(np, "use-ncsi", NULL)) {
Gavin Shanbd466c32016-07-19 11:54:23 +10001803 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1804 dev_err(&pdev->dev, "NCSI stack not enabled\n");
1805 goto err_ncsi_dev;
1806 }
1807
1808 dev_info(&pdev->dev, "Using NCSI interface\n");
1809 priv->use_ncsi = true;
1810 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1811 if (!priv->ndev)
1812 goto err_ncsi_dev;
1813 } else {
1814 priv->use_ncsi = false;
1815 err = ftgmac100_setup_mdio(netdev);
1816 if (err)
1817 goto err_setup_mdio;
1818 }
1819
Benjamin Herrenschmidt52d91382017-04-12 13:27:09 +10001820 /* Default ring sizes */
1821 priv->rx_q_entries = priv->new_rx_q_entries = DEF_RX_QUEUE_ENTRIES;
1822 priv->tx_q_entries = priv->new_tx_q_entries = DEF_TX_QUEUE_ENTRIES;
1823
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001824 /* Base feature set */
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001825 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
Benjamin Herrenschmidt0fb99682017-04-18 08:37:01 +10001826 NETIF_F_GRO | NETIF_F_SG | NETIF_F_HW_VLAN_CTAG_RX |
1827 NETIF_F_HW_VLAN_CTAG_TX;
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001828
1829 /* AST2400 doesn't have working HW checksum generation */
1830 if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac")))
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001831 netdev->hw_features &= ~NETIF_F_HW_CSUM;
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001832 if (np && of_get_property(np, "no-hw-checksum", NULL))
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001833 netdev->hw_features &= ~(NETIF_F_HW_CSUM | NETIF_F_RXCSUM);
1834 netdev->features |= netdev->hw_features;
Gavin Shanbd466c32016-07-19 11:54:23 +10001835
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001836 /* register network device */
1837 err = register_netdev(netdev);
1838 if (err) {
1839 dev_err(&pdev->dev, "Failed to register netdev\n");
1840 goto err_register_netdev;
1841 }
1842
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001843 netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001844
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001845 return 0;
1846
Gavin Shanbd466c32016-07-19 11:54:23 +10001847err_ncsi_dev:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001848err_register_netdev:
Gavin Shaneb418182016-07-19 11:54:21 +10001849 ftgmac100_destroy_mdio(netdev);
1850err_setup_mdio:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001851 iounmap(priv->base);
1852err_ioremap:
1853 release_resource(priv->res);
1854err_req_mem:
1855 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001856 free_netdev(netdev);
1857err_alloc_etherdev:
1858 return err;
1859}
1860
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001861static int ftgmac100_remove(struct platform_device *pdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001862{
1863 struct net_device *netdev;
1864 struct ftgmac100 *priv;
1865
1866 netdev = platform_get_drvdata(pdev);
1867 priv = netdev_priv(netdev);
1868
1869 unregister_netdev(netdev);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001870
1871 /* There's a small chance the reset task will have been re-queued,
1872 * during stop, make sure it's gone before we free the structure.
1873 */
1874 cancel_work_sync(&priv->reset_task);
1875
Gavin Shaneb418182016-07-19 11:54:21 +10001876 ftgmac100_destroy_mdio(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001877
1878 iounmap(priv->base);
1879 release_resource(priv->res);
1880
1881 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001882 free_netdev(netdev);
1883 return 0;
1884}
1885
Gavin Shanbb168e22016-07-19 11:54:24 +10001886static const struct of_device_id ftgmac100_of_match[] = {
1887 { .compatible = "faraday,ftgmac100" },
1888 { }
1889};
1890MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1891
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001892static struct platform_driver ftgmac100_driver = {
Gavin Shanbb168e22016-07-19 11:54:24 +10001893 .probe = ftgmac100_probe,
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001894 .remove = ftgmac100_remove,
Gavin Shanbb168e22016-07-19 11:54:24 +10001895 .driver = {
1896 .name = DRV_NAME,
1897 .of_match_table = ftgmac100_of_match,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001898 },
1899};
Sachin Kamat14f645d2013-03-18 01:50:48 +00001900module_platform_driver(ftgmac100_driver);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001901
1902MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1903MODULE_DESCRIPTION("FTGMAC100 driver");
1904MODULE_LICENSE("GPL");