blob: 49c19b786122ee02a451ab8b910b5f890712d6c3 [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
43#define RX_QUEUE_ENTRIES 256 /* must be power of 2 */
44#define TX_QUEUE_ENTRIES 512 /* must be power of 2 */
45
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +100046#define MAX_PKT_SIZE 1536
47#define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000048
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +100049/* Min number of tx ring entries before stopping queue */
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +100050#define TX_THRESHOLD (MAX_SKB_FRAGS + 1)
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +100051
Po-Yu Chuang69785b72011-06-08 23:32:48 +000052struct ftgmac100_descs {
53 struct ftgmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
54 struct ftgmac100_txdes txdes[TX_QUEUE_ENTRIES];
55};
56
57struct ftgmac100 {
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100058 /* Registers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000059 struct resource *res;
60 void __iomem *base;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000061
62 struct ftgmac100_descs *descs;
63 dma_addr_t descs_dma_addr;
64
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100065 /* Rx ring */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +100066 struct sk_buff *rx_skbs[RX_QUEUE_ENTRIES];
Po-Yu Chuang69785b72011-06-08 23:32:48 +000067 unsigned int rx_pointer;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100068 u32 rxdes0_edorr_mask;
69
70 /* Tx ring */
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +100071 struct sk_buff *tx_skbs[TX_QUEUE_ENTRIES];
Po-Yu Chuang69785b72011-06-08 23:32:48 +000072 unsigned int tx_clean_pointer;
73 unsigned int tx_pointer;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100074 u32 txdes0_edotr_mask;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000075
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +100076 /* Scratch page to use when rx skb alloc fails */
77 void *rx_scratch;
78 dma_addr_t rx_scratch_dma;
79
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100080 /* Component structures */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000081 struct net_device *netdev;
82 struct device *dev;
Gavin Shanbd466c32016-07-19 11:54:23 +100083 struct ncsi_dev *ndev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000084 struct napi_struct napi;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +100085 struct work_struct reset_task;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000086 struct mii_bus *mii_bus;
Andrew Jeffery7906a4d2016-09-22 08:34:59 +093087
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100088 /* Link management */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +100089 int cur_speed;
90 int cur_duplex;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100091 bool use_ncsi;
92
93 /* Misc */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +100094 bool need_mac_restart;
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +100095 bool is_aspeed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000096};
97
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +100098static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
Po-Yu Chuang69785b72011-06-08 23:32:48 +000099{
100 struct net_device *netdev = priv->netdev;
101 int i;
102
103 /* NOTE: reset clears all registers */
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000104 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
105 iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
106 priv->base + FTGMAC100_OFFSET_MACCR);
107 for (i = 0; i < 50; i++) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000108 unsigned int maccr;
109
110 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
111 if (!(maccr & FTGMAC100_MACCR_SW_RST))
112 return 0;
113
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000114 udelay(1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000115 }
116
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000117 netdev_err(netdev, "Hardware reset failed\n");
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000118 return -EIO;
119}
120
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000121static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
122{
123 u32 maccr = 0;
124
125 switch (priv->cur_speed) {
126 case SPEED_10:
127 case 0: /* no link */
128 break;
129
130 case SPEED_100:
131 maccr |= FTGMAC100_MACCR_FAST_MODE;
132 break;
133
134 case SPEED_1000:
135 maccr |= FTGMAC100_MACCR_GIGA_MODE;
136 break;
137 default:
138 netdev_err(priv->netdev, "Unknown speed %d !\n",
139 priv->cur_speed);
140 break;
141 }
142
143 /* (Re)initialize the queue pointers */
144 priv->rx_pointer = 0;
145 priv->tx_clean_pointer = 0;
146 priv->tx_pointer = 0;
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000147
148 /* The doc says reset twice with 10us interval */
149 if (ftgmac100_reset_mac(priv, maccr))
150 return -EIO;
151 usleep_range(10, 1000);
152 return ftgmac100_reset_mac(priv, maccr);
153}
154
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000155static void ftgmac100_write_mac_addr(struct ftgmac100 *priv, const u8 *mac)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000156{
157 unsigned int maddr = mac[0] << 8 | mac[1];
158 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
159
160 iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
161 iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
162}
163
Benjamin Herrenschmidtba1b1232017-04-12 13:27:06 +1000164static void ftgmac100_initial_mac(struct ftgmac100 *priv)
Gavin Shan113ce102016-07-19 11:54:22 +1000165{
166 u8 mac[ETH_ALEN];
167 unsigned int m;
168 unsigned int l;
169 void *addr;
170
171 addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
172 if (addr) {
173 ether_addr_copy(priv->netdev->dev_addr, mac);
174 dev_info(priv->dev, "Read MAC address %pM from device tree\n",
175 mac);
176 return;
177 }
178
179 m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
180 l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
181
182 mac[0] = (m >> 8) & 0xff;
183 mac[1] = m & 0xff;
184 mac[2] = (l >> 24) & 0xff;
185 mac[3] = (l >> 16) & 0xff;
186 mac[4] = (l >> 8) & 0xff;
187 mac[5] = l & 0xff;
188
Gavin Shan113ce102016-07-19 11:54:22 +1000189 if (is_valid_ether_addr(mac)) {
190 ether_addr_copy(priv->netdev->dev_addr, mac);
191 dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
192 } else {
193 eth_hw_addr_random(priv->netdev);
194 dev_info(priv->dev, "Generated random MAC address %pM\n",
195 priv->netdev->dev_addr);
196 }
197}
198
199static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
200{
201 int ret;
202
203 ret = eth_prepare_mac_addr_change(dev, p);
204 if (ret < 0)
205 return ret;
206
207 eth_commit_mac_addr_change(dev, p);
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000208 ftgmac100_write_mac_addr(netdev_priv(dev), dev->dev_addr);
Gavin Shan113ce102016-07-19 11:54:22 +1000209
210 return 0;
211}
212
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000213static void ftgmac100_init_hw(struct ftgmac100 *priv)
214{
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000215 u32 reg, rfifo_sz, tfifo_sz;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000216
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000217 /* Clear stale interrupts */
218 reg = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
219 iowrite32(reg, priv->base + FTGMAC100_OFFSET_ISR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000220
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000221 /* Setup RX ring buffer base */
222 iowrite32(priv->descs_dma_addr +
223 offsetof(struct ftgmac100_descs, rxdes),
224 priv->base + FTGMAC100_OFFSET_RXR_BADR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000225
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000226 /* Setup TX ring buffer base */
227 iowrite32(priv->descs_dma_addr +
228 offsetof(struct ftgmac100_descs, txdes),
229 priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
230
231 /* Configure RX buffer size */
232 iowrite32(FTGMAC100_RBSR_SIZE(RX_BUF_SIZE),
233 priv->base + FTGMAC100_OFFSET_RBSR);
234
235 /* Set RX descriptor autopoll */
236 iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1),
237 priv->base + FTGMAC100_OFFSET_APTC);
238
239 /* Write MAC address */
Benjamin Herrenschmidtf39c71b2017-04-12 13:27:05 +1000240 ftgmac100_write_mac_addr(priv, priv->netdev->dev_addr);
Benjamin Herrenschmidt3833dc62017-04-12 13:27:08 +1000241
242 /* Configure descriptor sizes and increase burst sizes according
243 * to values in Aspeed SDK. The FIFO arbitration is enabled and
244 * the thresholds set based on the recommended values in the
245 * AST2400 specification.
246 */
247 iowrite32(FTGMAC100_DBLAC_RXDES_SIZE(2) | /* 2*8 bytes RX descs */
248 FTGMAC100_DBLAC_TXDES_SIZE(2) | /* 2*8 bytes TX descs */
249 FTGMAC100_DBLAC_RXBURST_SIZE(3) | /* 512 bytes max RX bursts */
250 FTGMAC100_DBLAC_TXBURST_SIZE(3) | /* 512 bytes max TX bursts */
251 FTGMAC100_DBLAC_RX_THR_EN | /* Enable fifo threshold arb */
252 FTGMAC100_DBLAC_RXFIFO_HTHR(6) | /* 6/8 of FIFO high threshold */
253 FTGMAC100_DBLAC_RXFIFO_LTHR(2), /* 2/8 of FIFO low threshold */
254 priv->base + FTGMAC100_OFFSET_DBLAC);
255
256 /* Interrupt mitigation configured for 1 interrupt/packet. HW interrupt
257 * mitigation doesn't seem to provide any benefit with NAPI so leave
258 * it at that.
259 */
260 iowrite32(FTGMAC100_ITC_RXINT_THR(1) |
261 FTGMAC100_ITC_TXINT_THR(1),
262 priv->base + FTGMAC100_OFFSET_ITC);
263
264 /* Configure FIFO sizes in the TPAFCR register */
265 reg = ioread32(priv->base + FTGMAC100_OFFSET_FEAR);
266 rfifo_sz = reg & 0x00000007;
267 tfifo_sz = (reg >> 3) & 0x00000007;
268 reg = ioread32(priv->base + FTGMAC100_OFFSET_TPAFCR);
269 reg &= ~0x3f000000;
270 reg |= (tfifo_sz << 27);
271 reg |= (rfifo_sz << 24);
272 iowrite32(reg, priv->base + FTGMAC100_OFFSET_TPAFCR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000273}
274
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000275static void ftgmac100_start_hw(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000276{
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000277 u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000278
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000279 /* Keep the original GMAC and FAST bits */
280 maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000281
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000282 /* Add all the main enable bits */
283 maccr |= FTGMAC100_MACCR_TXDMA_EN |
284 FTGMAC100_MACCR_RXDMA_EN |
285 FTGMAC100_MACCR_TXMAC_EN |
286 FTGMAC100_MACCR_RXMAC_EN |
287 FTGMAC100_MACCR_CRC_APD |
288 FTGMAC100_MACCR_PHY_LINK_LEVEL |
289 FTGMAC100_MACCR_RX_RUNT |
290 FTGMAC100_MACCR_RX_BROADPKT;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000291
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000292 /* Add other bits as needed */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000293 if (priv->cur_duplex == DUPLEX_FULL)
294 maccr |= FTGMAC100_MACCR_FULLDUP;
295
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000296 /* Hit the HW */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000297 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
298}
299
300static void ftgmac100_stop_hw(struct ftgmac100 *priv)
301{
302 iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
303}
304
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000305static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry,
306 struct ftgmac100_rxdes *rxdes, gfp_t gfp)
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000307{
308 struct net_device *netdev = priv->netdev;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000309 struct sk_buff *skb;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000310 dma_addr_t map;
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000311 int err;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000312
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000313 skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE);
314 if (unlikely(!skb)) {
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000315 if (net_ratelimit())
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000316 netdev_warn(netdev, "failed to allocate rx skb\n");
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000317 err = -ENOMEM;
318 map = priv->rx_scratch_dma;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000319 } else {
320 map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE,
321 DMA_FROM_DEVICE);
322 if (unlikely(dma_mapping_error(priv->dev, map))) {
323 if (net_ratelimit())
324 netdev_err(netdev, "failed to map rx page\n");
325 dev_kfree_skb_any(skb);
326 map = priv->rx_scratch_dma;
327 skb = NULL;
328 err = -ENOMEM;
329 }
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000330 }
331
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000332 /* Store skb */
333 priv->rx_skbs[entry] = skb;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000334
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000335 /* Store DMA address into RX desc */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000336 rxdes->rxdes3 = cpu_to_le32(map);
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000337
338 /* Ensure the above is ordered vs clearing the OWN bit */
339 dma_wmb();
340
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000341 /* Clean status (which resets own bit) */
342 if (entry == (RX_QUEUE_ENTRIES - 1))
343 rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask);
344 else
345 rxdes->rxdes0 = 0;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000346
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000347 return 0;
348}
349
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000350static int ftgmac100_next_rx_pointer(int pointer)
351{
352 return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
353}
354
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000355static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000356{
357 struct net_device *netdev = priv->netdev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000358
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000359 if (status & FTGMAC100_RXDES0_RX_ERR)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000360 netdev->stats.rx_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000361
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000362 if (status & FTGMAC100_RXDES0_CRC_ERR)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000363 netdev->stats.rx_crc_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000364
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000365 if (status & (FTGMAC100_RXDES0_FTL |
366 FTGMAC100_RXDES0_RUNT |
367 FTGMAC100_RXDES0_RX_ODD_NB))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000368 netdev->stats.rx_length_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000369}
370
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000371static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
372{
373 struct net_device *netdev = priv->netdev;
374 struct ftgmac100_rxdes *rxdes;
375 struct sk_buff *skb;
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000376 unsigned int pointer, size;
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000377 u32 status, csum_vlan;
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000378 dma_addr_t map;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000379
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000380 /* Grab next RX descriptor */
381 pointer = priv->rx_pointer;
382 rxdes = &priv->descs->rxdes[pointer];
383
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000384 /* Grab descriptor status */
385 status = le32_to_cpu(rxdes->rxdes0);
386
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000387 /* Do we have a packet ? */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000388 if (!(status & FTGMAC100_RXDES0_RXPKT_RDY))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000389 return false;
390
Benjamin Herrenschmidt027f4262017-04-06 11:02:50 +1000391 /* Order subsequent reads with the test for the ready bit */
392 dma_rmb();
393
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000394 /* We don't cope with fragmented RX packets */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000395 if (unlikely(!(status & FTGMAC100_RXDES0_FRS) ||
396 !(status & FTGMAC100_RXDES0_LRS)))
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000397 goto drop;
398
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000399 /* Grab received size and csum vlan field in the descriptor */
400 size = status & FTGMAC100_RXDES0_VDBC;
401 csum_vlan = le32_to_cpu(rxdes->rxdes1);
402
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000403 /* Any error (other than csum offload) flagged ? */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000404 if (unlikely(status & RXDES0_ANY_ERROR)) {
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000405 /* Correct for incorrect flagging of runt packets
406 * with vlan tags... Just accept a runt packet that
407 * has been flagged as vlan and whose size is at
408 * least 60 bytes.
409 */
410 if ((status & FTGMAC100_RXDES0_RUNT) &&
411 (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) &&
412 (size >= 60))
413 status &= ~FTGMAC100_RXDES0_RUNT;
414
415 /* Any error still in there ? */
416 if (status & RXDES0_ANY_ERROR) {
417 ftgmac100_rx_packet_error(priv, status);
418 goto drop;
419 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000420 }
421
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000422 /* If the packet had no skb (failed to allocate earlier)
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000423 * then try to allocate one and skip
424 */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000425 skb = priv->rx_skbs[pointer];
426 if (!unlikely(skb)) {
427 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000428 goto drop;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000429 }
430
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000431 if (unlikely(status & FTGMAC100_RXDES0_MULTICAST))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000432 netdev->stats.multicast++;
433
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000434 /* If the HW found checksum errors, bounce it to software.
435 *
436 * If we didn't, we need to see if the packet was recognized
437 * by HW as one of the supported checksummed protocols before
438 * we accept the HW test results.
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000439 */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000440 if (netdev->features & NETIF_F_RXCSUM) {
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000441 u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
442 FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
443 FTGMAC100_RXDES1_IP_CHKSUM_ERR;
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000444 if ((csum_vlan & err_bits) ||
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000445 !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK))
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000446 skb->ip_summed = CHECKSUM_NONE;
447 else
448 skb->ip_summed = CHECKSUM_UNNECESSARY;
449 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000450
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000451 /* Transfer received size to skb */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000452 skb_put(skb, size);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000453
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000454 /* Tear down DMA mapping, do necessary cache management */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000455 map = le32_to_cpu(rxdes->rxdes3);
456
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000457#if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU)
458 /* When we don't have an iommu, we can save cycles by not
459 * invalidating the cache for the part of the packet that
460 * wasn't received.
461 */
462 dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE);
463#else
464 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
465#endif
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000466
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000467
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000468 /* Resplenish rx ring */
469 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000470 priv->rx_pointer = ftgmac100_next_rx_pointer(pointer);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000471
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000472 skb->protocol = eth_type_trans(skb, netdev);
473
474 netdev->stats.rx_packets++;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000475 netdev->stats.rx_bytes += size;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000476
477 /* push packet to protocol stack */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000478 if (skb->ip_summed == CHECKSUM_NONE)
479 netif_receive_skb(skb);
480 else
481 napi_gro_receive(&priv->napi, skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000482
483 (*processed)++;
484 return true;
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000485
486 drop:
487 /* Clean rxdes0 (which resets own bit) */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000488 rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000489 priv->rx_pointer = ftgmac100_next_rx_pointer(pointer);
490 netdev->stats.rx_dropped++;
491 return true;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000492}
493
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000494static u32 ftgmac100_base_tx_ctlstat(struct ftgmac100 *priv,
495 unsigned int index)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000496{
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000497 if (index == (TX_QUEUE_ENTRIES - 1))
498 return priv->txdes0_edotr_mask;
499 else
500 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000501}
502
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000503static int ftgmac100_next_tx_pointer(int pointer)
504{
505 return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
506}
507
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000508static u32 ftgmac100_tx_buf_avail(struct ftgmac100 *priv)
509{
510 /* Returns the number of available slots in the TX queue
511 *
512 * This always leaves one free slot so we don't have to
513 * worry about empty vs. full, and this simplifies the
514 * test for ftgmac100_tx_buf_cleanable() below
515 */
516 return (priv->tx_clean_pointer - priv->tx_pointer - 1) &
517 (TX_QUEUE_ENTRIES - 1);
518}
519
520static bool ftgmac100_tx_buf_cleanable(struct ftgmac100 *priv)
521{
522 return priv->tx_pointer != priv->tx_clean_pointer;
523}
524
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000525static void ftgmac100_free_tx_packet(struct ftgmac100 *priv,
526 unsigned int pointer,
527 struct sk_buff *skb,
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000528 struct ftgmac100_txdes *txdes,
529 u32 ctl_stat)
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000530{
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000531 dma_addr_t map = le32_to_cpu(txdes->txdes3);
532 size_t len;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000533
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000534 if (ctl_stat & FTGMAC100_TXDES0_FTS) {
535 len = skb_headlen(skb);
536 dma_unmap_single(priv->dev, map, len, DMA_TO_DEVICE);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000537 } else {
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000538 len = FTGMAC100_TXDES0_TXBUF_SIZE(ctl_stat);
539 dma_unmap_page(priv->dev, map, len, DMA_TO_DEVICE);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000540 }
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000541
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000542 /* Free SKB on last segment */
543 if (ctl_stat & FTGMAC100_TXDES0_LTS)
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000544 dev_kfree_skb(skb);
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000545 priv->tx_skbs[pointer] = NULL;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000546}
547
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000548static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
549{
550 struct net_device *netdev = priv->netdev;
551 struct ftgmac100_txdes *txdes;
552 struct sk_buff *skb;
Benjamin Herrenschmidt42c2d192017-04-10 11:15:23 +1000553 unsigned int pointer;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000554 u32 ctl_stat;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000555
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000556 pointer = priv->tx_clean_pointer;
557 txdes = &priv->descs->txdes[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000558
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000559 ctl_stat = le32_to_cpu(txdes->txdes0);
560 if (ctl_stat & FTGMAC100_TXDES0_TXDMA_OWN)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000561 return false;
562
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000563 skb = priv->tx_skbs[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000564 netdev->stats.tx_packets++;
565 netdev->stats.tx_bytes += skb->len;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000566 ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
567 txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000568
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000569 priv->tx_clean_pointer = ftgmac100_next_tx_pointer(pointer);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000570
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000571 return true;
572}
573
574static void ftgmac100_tx_complete(struct ftgmac100 *priv)
575{
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000576 struct net_device *netdev = priv->netdev;
577
578 /* Process all completed packets */
579 while (ftgmac100_tx_buf_cleanable(priv) &&
580 ftgmac100_tx_complete_packet(priv))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000581 ;
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000582
583 /* Restart queue if needed */
584 smp_mb();
585 if (unlikely(netif_queue_stopped(netdev) &&
586 ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)) {
587 struct netdev_queue *txq;
588
589 txq = netdev_get_tx_queue(netdev, 0);
590 __netif_tx_lock(txq, smp_processor_id());
591 if (netif_queue_stopped(netdev) &&
592 ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
593 netif_wake_queue(netdev);
594 __netif_tx_unlock(txq);
595 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000596}
597
Benjamin Herrenschmidt05690d62017-04-12 13:27:01 +1000598static bool ftgmac100_prep_tx_csum(struct sk_buff *skb, u32 *csum_vlan)
599{
600 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
601 u8 ip_proto = ip_hdr(skb)->protocol;
602
603 *csum_vlan |= FTGMAC100_TXDES1_IP_CHKSUM;
604 switch(ip_proto) {
605 case IPPROTO_TCP:
606 *csum_vlan |= FTGMAC100_TXDES1_TCP_CHKSUM;
607 return true;
608 case IPPROTO_UDP:
609 *csum_vlan |= FTGMAC100_TXDES1_UDP_CHKSUM;
610 return true;
611 case IPPROTO_IP:
612 return true;
613 }
614 }
615 return skb_checksum_help(skb) == 0;
616}
617
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000618static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
619 struct net_device *netdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000620{
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000621 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000622 struct ftgmac100_txdes *txdes, *first;
623 unsigned int pointer, nfrags, len, i, j;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000624 u32 f_ctl_stat, ctl_stat, csum_vlan;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000625 dma_addr_t map;
626
Benjamin Herrenschmidt9b0f7712017-04-10 11:15:19 +1000627 /* The HW doesn't pad small frames */
628 if (eth_skb_pad(skb)) {
629 netdev->stats.tx_dropped++;
630 return NETDEV_TX_OK;
631 }
632
633 /* Reject oversize packets */
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000634 if (unlikely(skb->len > MAX_PKT_SIZE)) {
635 if (net_ratelimit())
636 netdev_dbg(netdev, "tx packet too big\n");
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000637 goto drop;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000638 }
639
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000640 /* Do we have a limit on #fragments ? I yet have to get a reply
641 * from Aspeed. If there's one I haven't hit it.
642 */
643 nfrags = skb_shinfo(skb)->nr_frags;
644
645 /* Get header len */
646 len = skb_headlen(skb);
647
648 /* Map the packet head */
649 map = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE);
650 if (dma_mapping_error(priv->dev, map)) {
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000651 if (net_ratelimit())
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000652 netdev_err(netdev, "map tx packet head failed\n");
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000653 goto drop;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000654 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000655
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000656 /* Grab the next free tx descriptor */
657 pointer = priv->tx_pointer;
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000658 txdes = first = &priv->descs->txdes[pointer];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000659
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000660 /* Setup it up with the packet head. Don't write the head to the
661 * ring just yet
662 */
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000663 priv->tx_skbs[pointer] = skb;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000664 f_ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
665 f_ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
666 f_ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
667 f_ctl_stat |= FTGMAC100_TXDES0_FTS;
668 if (nfrags == 0)
669 f_ctl_stat |= FTGMAC100_TXDES0_LTS;
670 txdes->txdes3 = cpu_to_le32(map);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000671
672 /* Setup HW checksumming */
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000673 csum_vlan = 0;
Benjamin Herrenschmidt05690d62017-04-12 13:27:01 +1000674 if (skb->ip_summed == CHECKSUM_PARTIAL &&
675 !ftgmac100_prep_tx_csum(skb, &csum_vlan))
676 goto drop;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000677 txdes->txdes1 = cpu_to_le32(csum_vlan);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000678
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000679 /* Next descriptor */
680 pointer = ftgmac100_next_tx_pointer(pointer);
681
682 /* Add the fragments */
683 for (i = 0; i < nfrags; i++) {
684 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
685
686 len = frag->size;
687
688 /* Map it */
689 map = skb_frag_dma_map(priv->dev, frag, 0, len,
690 DMA_TO_DEVICE);
691 if (dma_mapping_error(priv->dev, map))
692 goto dma_err;
693
694 /* Setup descriptor */
695 priv->tx_skbs[pointer] = skb;
696 txdes = &priv->descs->txdes[pointer];
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000697 ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
698 ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
699 ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
700 if (i == (nfrags - 1))
701 ctl_stat |= FTGMAC100_TXDES0_LTS;
702 txdes->txdes0 = cpu_to_le32(ctl_stat);
703 txdes->txdes1 = 0;
704 txdes->txdes3 = cpu_to_le32(map);
705
706 /* Next one */
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000707 pointer = ftgmac100_next_tx_pointer(pointer);
708 }
709
Benjamin Herrenschmidt4a2712b2017-04-10 11:15:22 +1000710 /* Order the previous packet and descriptor udpates
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000711 * before setting the OWN bit on the first descriptor.
Benjamin Herrenschmidt4a2712b2017-04-10 11:15:22 +1000712 */
713 dma_wmb();
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000714 first->txdes0 = cpu_to_le32(f_ctl_stat);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000715
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000716 /* Update next TX pointer */
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000717 priv->tx_pointer = pointer;
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000718
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000719 /* If there isn't enough room for all the fragments of a new packet
720 * in the TX ring, stop the queue. The sequence below is race free
721 * vs. a concurrent restart in ftgmac100_poll()
722 */
723 if (unlikely(ftgmac100_tx_buf_avail(priv) < TX_THRESHOLD)) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000724 netif_stop_queue(netdev);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +1000725 /* Order the queue stop with the test below */
726 smp_mb();
727 if (ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
728 netif_wake_queue(netdev);
729 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000730
Benjamin Herrenschmidt8eecf7c2017-04-12 13:27:07 +1000731 /* Poke transmitter to read the updated TX descriptors */
732 iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000733
734 return NETDEV_TX_OK;
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000735
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000736 dma_err:
737 if (net_ratelimit())
738 netdev_err(netdev, "map tx fragment failed\n");
739
740 /* Free head */
741 pointer = priv->tx_pointer;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000742 ftgmac100_free_tx_packet(priv, pointer, skb, first, f_ctl_stat);
743 first->txdes0 = cpu_to_le32(f_ctl_stat & priv->txdes0_edotr_mask);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000744
745 /* Then all fragments */
746 for (j = 0; j < i; j++) {
747 pointer = ftgmac100_next_tx_pointer(pointer);
748 txdes = &priv->descs->txdes[pointer];
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000749 ctl_stat = le32_to_cpu(txdes->txdes0);
750 ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
751 txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +1000752 }
753
754 /* This cannot be reached if we successfully mapped the
755 * last fragment, so we know ftgmac100_free_tx_packet()
756 * hasn't freed the skb yet.
757 */
Benjamin Herrenschmidt3e427a32017-04-10 11:15:18 +1000758 drop:
759 /* Drop the packet */
760 dev_kfree_skb_any(skb);
761 netdev->stats.tx_dropped++;
762
763 return NETDEV_TX_OK;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000764}
765
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000766static void ftgmac100_free_buffers(struct ftgmac100 *priv)
767{
768 int i;
769
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000770 /* Free all RX buffers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000771 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
772 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000773 struct sk_buff *skb = priv->rx_skbs[i];
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000774 dma_addr_t map = le32_to_cpu(rxdes->rxdes3);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000775
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000776 if (!skb)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000777 continue;
778
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000779 priv->rx_skbs[i] = NULL;
780 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
781 dev_kfree_skb_any(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000782 }
783
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000784 /* Free all TX buffers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000785 for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
786 struct ftgmac100_txdes *txdes = &priv->descs->txdes[i];
Benjamin Herrenschmidt83617312017-04-10 11:15:20 +1000787 struct sk_buff *skb = priv->tx_skbs[i];
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000788
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000789 if (!skb)
790 continue;
791 ftgmac100_free_tx_packet(priv, i, skb, txdes,
792 le32_to_cpu(txdes->txdes0));
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000793 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000794}
795
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000796static void ftgmac100_free_rings(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000797{
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000798 /* Free descriptors */
799 if (priv->descs)
800 dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs),
801 priv->descs, priv->descs_dma_addr);
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000802
803 /* Free scratch packet buffer */
804 if (priv->rx_scratch)
805 dma_free_coherent(priv->dev, RX_BUF_SIZE,
806 priv->rx_scratch, priv->rx_scratch_dma);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000807}
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000808
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000809static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
810{
811 /* Allocate descriptors */
Joe Perchesede23fa82013-08-26 22:45:23 -0700812 priv->descs = dma_zalloc_coherent(priv->dev,
813 sizeof(struct ftgmac100_descs),
814 &priv->descs_dma_addr, GFP_KERNEL);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000815 if (!priv->descs)
816 return -ENOMEM;
817
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000818 /* Allocate scratch packet buffer */
819 priv->rx_scratch = dma_alloc_coherent(priv->dev,
820 RX_BUF_SIZE,
821 &priv->rx_scratch_dma,
822 GFP_KERNEL);
823 if (!priv->rx_scratch)
824 return -ENOMEM;
825
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000826 return 0;
827}
828
829static void ftgmac100_init_rings(struct ftgmac100 *priv)
830{
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000831 struct ftgmac100_rxdes *rxdes;
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000832 struct ftgmac100_txdes *txdes;
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000833 int i;
834
835 /* Initialize RX ring */
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000836 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000837 rxdes = &priv->descs->rxdes[i];
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000838 rxdes->rxdes0 = 0;
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000839 rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma);
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000840 }
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000841 /* Mark the end of the ring */
842 rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000843
844 /* Initialize TX ring */
Benjamin Herrenschmidt52c0cae2017-04-10 11:15:26 +1000845 for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
846 txdes = &priv->descs->txdes[i];
847 txdes->txdes0 = 0;
848 }
849 txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000850}
851
852static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
853{
854 int i;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000855
856 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
857 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
858
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000859 if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL))
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000860 return -ENOMEM;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000861 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000862 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000863}
864
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000865static void ftgmac100_adjust_link(struct net_device *netdev)
866{
867 struct ftgmac100 *priv = netdev_priv(netdev);
Philippe Reynesb3c40ad2016-05-16 01:35:13 +0200868 struct phy_device *phydev = netdev->phydev;
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000869 int new_speed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000870
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000871 /* We store "no link" as speed 0 */
872 if (!phydev->link)
873 new_speed = 0;
874 else
875 new_speed = phydev->speed;
876
877 if (phydev->speed == priv->cur_speed &&
878 phydev->duplex == priv->cur_duplex)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000879 return;
880
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000881 /* Print status if we have a link or we had one and just lost it,
882 * don't print otherwise.
883 */
884 if (new_speed || priv->cur_speed)
885 phy_print_status(phydev);
886
887 priv->cur_speed = new_speed;
888 priv->cur_duplex = phydev->duplex;
889
890 /* Link is down, do nothing else */
891 if (!new_speed)
892 return;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000893
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000894 /* Disable all interrupts */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000895 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
896
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000897 /* Reset the adapter asynchronously */
898 schedule_work(&priv->reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000899}
900
901static int ftgmac100_mii_probe(struct ftgmac100 *priv)
902{
903 struct net_device *netdev = priv->netdev;
Guenter Roecke574f392016-01-10 12:04:32 -0800904 struct phy_device *phydev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000905
Guenter Roecke574f392016-01-10 12:04:32 -0800906 phydev = phy_find_first(priv->mii_bus);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000907 if (!phydev) {
908 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
909 return -ENODEV;
910 }
911
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100912 phydev = phy_connect(netdev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000913 &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000914
915 if (IS_ERR(phydev)) {
916 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
917 return PTR_ERR(phydev);
918 }
919
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000920 return 0;
921}
922
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000923static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
924{
925 struct net_device *netdev = bus->priv;
926 struct ftgmac100 *priv = netdev_priv(netdev);
927 unsigned int phycr;
928 int i;
929
930 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
931
932 /* preserve MDC cycle threshold */
933 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
934
935 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
936 FTGMAC100_PHYCR_REGAD(regnum) |
937 FTGMAC100_PHYCR_MIIRD;
938
939 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
940
941 for (i = 0; i < 10; i++) {
942 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
943
944 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
945 int data;
946
947 data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
948 return FTGMAC100_PHYDATA_MIIRDATA(data);
949 }
950
951 udelay(100);
952 }
953
954 netdev_err(netdev, "mdio read timed out\n");
955 return -EIO;
956}
957
958static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
959 int regnum, u16 value)
960{
961 struct net_device *netdev = bus->priv;
962 struct ftgmac100 *priv = netdev_priv(netdev);
963 unsigned int phycr;
964 int data;
965 int i;
966
967 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
968
969 /* preserve MDC cycle threshold */
970 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
971
972 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
973 FTGMAC100_PHYCR_REGAD(regnum) |
974 FTGMAC100_PHYCR_MIIWR;
975
976 data = FTGMAC100_PHYDATA_MIIWDATA(value);
977
978 iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
979 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
980
981 for (i = 0; i < 10; i++) {
982 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
983
984 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
985 return 0;
986
987 udelay(100);
988 }
989
990 netdev_err(netdev, "mdio write timed out\n");
991 return -EIO;
992}
993
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000994static void ftgmac100_get_drvinfo(struct net_device *netdev,
995 struct ethtool_drvinfo *info)
996{
Jiri Pirko7826d432013-01-06 00:44:26 +0000997 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
998 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
999 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001000}
1001
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001002static const struct ethtool_ops ftgmac100_ethtool_ops = {
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001003 .get_drvinfo = ftgmac100_get_drvinfo,
1004 .get_link = ethtool_op_get_link,
Philippe Reynesfd24d722016-05-16 01:35:14 +02001005 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1006 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001007};
1008
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001009static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
1010{
1011 struct net_device *netdev = dev_id;
1012 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001013 unsigned int status, new_mask = FTGMAC100_INT_BAD;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001014
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001015 /* Fetch and clear interrupt bits, process abnormal ones */
1016 status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1017 iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
1018 if (unlikely(status & FTGMAC100_INT_BAD)) {
1019
1020 /* RX buffer unavailable */
1021 if (status & FTGMAC100_INT_NO_RXBUF)
1022 netdev->stats.rx_over_errors++;
1023
1024 /* received packet lost due to RX FIFO full */
1025 if (status & FTGMAC100_INT_RPKT_LOST)
1026 netdev->stats.rx_fifo_errors++;
1027
1028 /* sent packet lost due to excessive TX collision */
1029 if (status & FTGMAC100_INT_XPKT_LOST)
1030 netdev->stats.tx_fifo_errors++;
1031
1032 /* AHB error -> Reset the chip */
1033 if (status & FTGMAC100_INT_AHB_ERR) {
1034 if (net_ratelimit())
1035 netdev_warn(netdev,
1036 "AHB bus error ! Resetting chip.\n");
1037 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1038 schedule_work(&priv->reset_task);
1039 return IRQ_HANDLED;
1040 }
1041
1042 /* We may need to restart the MAC after such errors, delay
1043 * this until after we have freed some Rx buffers though
1044 */
1045 priv->need_mac_restart = true;
1046
1047 /* Disable those errors until we restart */
1048 new_mask &= ~status;
1049 }
1050
1051 /* Only enable "bad" interrupts while NAPI is on */
1052 iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
1053
1054 /* Schedule NAPI bh */
1055 napi_schedule_irqoff(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001056
1057 return IRQ_HANDLED;
1058}
1059
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +10001060static bool ftgmac100_check_rx(struct ftgmac100 *priv)
1061{
1062 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[priv->rx_pointer];
1063
1064 /* Do we have a packet ? */
1065 return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY));
1066}
1067
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001068static int ftgmac100_poll(struct napi_struct *napi, int budget)
1069{
1070 struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001071 int work_done = 0;
1072 bool more;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001073
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001074 /* Handle TX completions */
1075 if (ftgmac100_tx_buf_cleanable(priv))
1076 ftgmac100_tx_complete(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001077
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001078 /* Handle RX packets */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001079 do {
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001080 more = ftgmac100_rx_packet(priv, &work_done);
1081 } while (more && work_done < budget);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001082
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001083
1084 /* The interrupt is telling us to kick the MAC back to life
1085 * after an RX overflow
1086 */
1087 if (unlikely(priv->need_mac_restart)) {
1088 ftgmac100_start_hw(priv);
1089
1090 /* Re-enable "bad" interrupts */
1091 iowrite32(FTGMAC100_INT_BAD,
1092 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001093 }
1094
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001095 /* As long as we are waiting for transmit packets to be
1096 * completed we keep NAPI going
1097 */
1098 if (ftgmac100_tx_buf_cleanable(priv))
1099 work_done = budget;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001100
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001101 if (work_done < budget) {
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001102 /* We are about to re-enable all interrupts. However
1103 * the HW has been latching RX/TX packet interrupts while
1104 * they were masked. So we clear them first, then we need
1105 * to re-check if there's something to process
1106 */
1107 iowrite32(FTGMAC100_INT_RXTX,
1108 priv->base + FTGMAC100_OFFSET_ISR);
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001109 if (ftgmac100_check_rx(priv) ||
1110 ftgmac100_tx_buf_cleanable(priv))
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001111 return budget;
1112
1113 /* deschedule NAPI */
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001114 napi_complete(napi);
1115
1116 /* enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001117 iowrite32(FTGMAC100_INT_ALL,
Gavin Shanfc6061c2016-07-19 11:54:25 +10001118 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001119 }
1120
Benjamin Herrenschmidt6ad3d7e2017-04-10 11:15:21 +10001121 return work_done;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001122}
1123
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001124static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
1125{
1126 int err = 0;
1127
1128 /* Re-init descriptors (adjust queue sizes) */
1129 ftgmac100_init_rings(priv);
1130
1131 /* Realloc rx descriptors */
1132 err = ftgmac100_alloc_rx_buffers(priv);
1133 if (err && !ignore_alloc_err)
1134 return err;
1135
1136 /* Reinit and restart HW */
1137 ftgmac100_init_hw(priv);
1138 ftgmac100_start_hw(priv);
1139
1140 /* Re-enable the device */
1141 napi_enable(&priv->napi);
1142 netif_start_queue(priv->netdev);
1143
1144 /* Enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001145 iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001146
1147 return err;
1148}
1149
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001150static void ftgmac100_reset_task(struct work_struct *work)
1151{
1152 struct ftgmac100 *priv = container_of(work, struct ftgmac100,
1153 reset_task);
1154 struct net_device *netdev = priv->netdev;
1155 int err;
1156
1157 netdev_dbg(netdev, "Resetting NIC...\n");
1158
1159 /* Lock the world */
1160 rtnl_lock();
1161 if (netdev->phydev)
1162 mutex_lock(&netdev->phydev->lock);
1163 if (priv->mii_bus)
1164 mutex_lock(&priv->mii_bus->mdio_lock);
1165
1166
1167 /* Check if the interface is still up */
1168 if (!netif_running(netdev))
1169 goto bail;
1170
1171 /* Stop the network stack */
1172 netif_trans_update(netdev);
1173 napi_disable(&priv->napi);
1174 netif_tx_disable(netdev);
1175
1176 /* Stop and reset the MAC */
1177 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001178 err = ftgmac100_reset_and_config_mac(priv);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001179 if (err) {
1180 /* Not much we can do ... it might come back... */
1181 netdev_err(netdev, "attempting to continue...\n");
1182 }
1183
1184 /* Free all rx and tx buffers */
1185 ftgmac100_free_buffers(priv);
1186
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001187 /* Setup everything again and restart chip */
1188 ftgmac100_init_all(priv, true);
1189
1190 netdev_dbg(netdev, "Reset done !\n");
1191 bail:
1192 if (priv->mii_bus)
1193 mutex_unlock(&priv->mii_bus->mdio_lock);
1194 if (netdev->phydev)
1195 mutex_unlock(&netdev->phydev->lock);
1196 rtnl_unlock();
1197}
1198
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001199static int ftgmac100_open(struct net_device *netdev)
1200{
1201 struct ftgmac100 *priv = netdev_priv(netdev);
1202 int err;
1203
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001204 /* Allocate ring buffers */
1205 err = ftgmac100_alloc_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001206 if (err) {
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001207 netdev_err(netdev, "Failed to allocate descriptors\n");
1208 return err;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001209 }
1210
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001211 /* When using NC-SI we force the speed to 100Mbit/s full duplex,
1212 *
1213 * Otherwise we leave it set to 0 (no link), the link
1214 * message from the PHY layer will handle setting it up to
1215 * something else if needed.
1216 */
1217 if (priv->use_ncsi) {
1218 priv->cur_duplex = DUPLEX_FULL;
1219 priv->cur_speed = SPEED_100;
1220 } else {
1221 priv->cur_duplex = 0;
1222 priv->cur_speed = 0;
1223 }
1224
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001225 /* Reset the hardware */
1226 err = ftgmac100_reset_and_config_mac(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001227 if (err)
1228 goto err_hw;
1229
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001230 /* Initialize NAPI */
1231 netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1232
Benjamin Herrenschmidt81f1eca2017-04-05 12:28:48 +10001233 /* Grab our interrupt */
1234 err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1235 if (err) {
1236 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
1237 goto err_irq;
1238 }
1239
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001240 /* Start things up */
1241 err = ftgmac100_init_all(priv, false);
1242 if (err) {
1243 netdev_err(netdev, "Failed to allocate packet buffers\n");
1244 goto err_alloc;
1245 }
Gavin Shan08c9c122016-09-22 08:35:01 +09301246
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001247 if (netdev->phydev) {
1248 /* If we have a PHY, start polling */
Gavin Shanbd466c32016-07-19 11:54:23 +10001249 phy_start(netdev->phydev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001250 } else if (priv->use_ncsi) {
1251 /* If using NC-SI, set our carrier on and start the stack */
Gavin Shanbd466c32016-07-19 11:54:23 +10001252 netif_carrier_on(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001253
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001254 /* Start the NCSI device */
Gavin Shanbd466c32016-07-19 11:54:23 +10001255 err = ncsi_start_dev(priv->ndev);
1256 if (err)
1257 goto err_ncsi;
1258 }
1259
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001260 return 0;
1261
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001262 err_ncsi:
Gavin Shanbd466c32016-07-19 11:54:23 +10001263 napi_disable(&priv->napi);
1264 netif_stop_queue(netdev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001265 err_alloc:
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001266 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001267 free_irq(netdev->irq, netdev);
1268 err_irq:
1269 netif_napi_del(&priv->napi);
1270 err_hw:
1271 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001272 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001273 return err;
1274}
1275
1276static int ftgmac100_stop(struct net_device *netdev)
1277{
1278 struct ftgmac100 *priv = netdev_priv(netdev);
1279
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001280 /* Note about the reset task: We are called with the rtnl lock
1281 * held, so we are synchronized against the core of the reset
1282 * task. We must not try to synchronously cancel it otherwise
1283 * we can deadlock. But since it will test for netif_running()
1284 * which has already been cleared by the net core, we don't
1285 * anything special to do.
1286 */
1287
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001288 /* disable all interrupts */
1289 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1290
1291 netif_stop_queue(netdev);
1292 napi_disable(&priv->napi);
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001293 netif_napi_del(&priv->napi);
Gavin Shanbd466c32016-07-19 11:54:23 +10001294 if (netdev->phydev)
1295 phy_stop(netdev->phydev);
Gavin Shan2c15f252016-10-04 11:25:54 +11001296 else if (priv->use_ncsi)
1297 ncsi_stop_dev(priv->ndev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001298
1299 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001300 free_irq(netdev->irq, netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001301 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001302 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001303
1304 return 0;
1305}
1306
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001307/* optional */
1308static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1309{
Gavin Shanbd466c32016-07-19 11:54:23 +10001310 if (!netdev->phydev)
1311 return -ENXIO;
1312
Philippe Reynesb3c40ad2016-05-16 01:35:13 +02001313 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001314}
1315
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001316static void ftgmac100_tx_timeout(struct net_device *netdev)
1317{
1318 struct ftgmac100 *priv = netdev_priv(netdev);
1319
1320 /* Disable all interrupts */
1321 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1322
1323 /* Do the reset outside of interrupt context */
1324 schedule_work(&priv->reset_task);
1325}
1326
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001327static const struct net_device_ops ftgmac100_netdev_ops = {
1328 .ndo_open = ftgmac100_open,
1329 .ndo_stop = ftgmac100_stop,
1330 .ndo_start_xmit = ftgmac100_hard_start_xmit,
Gavin Shan113ce102016-07-19 11:54:22 +10001331 .ndo_set_mac_address = ftgmac100_set_mac_addr,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001332 .ndo_validate_addr = eth_validate_addr,
1333 .ndo_do_ioctl = ftgmac100_do_ioctl,
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001334 .ndo_tx_timeout = ftgmac100_tx_timeout,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001335};
1336
Gavin Shaneb418182016-07-19 11:54:21 +10001337static int ftgmac100_setup_mdio(struct net_device *netdev)
1338{
1339 struct ftgmac100 *priv = netdev_priv(netdev);
1340 struct platform_device *pdev = to_platform_device(priv->dev);
1341 int i, err = 0;
Joel Stanleye07dc632016-09-22 08:35:02 +09301342 u32 reg;
Gavin Shaneb418182016-07-19 11:54:21 +10001343
1344 /* initialize mdio bus */
1345 priv->mii_bus = mdiobus_alloc();
1346 if (!priv->mii_bus)
1347 return -EIO;
1348
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001349 if (priv->is_aspeed) {
Joel Stanleye07dc632016-09-22 08:35:02 +09301350 /* This driver supports the old MDIO interface */
1351 reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
1352 reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
1353 iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
1354 };
1355
Gavin Shaneb418182016-07-19 11:54:21 +10001356 priv->mii_bus->name = "ftgmac100_mdio";
1357 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1358 pdev->name, pdev->id);
1359 priv->mii_bus->priv = priv->netdev;
1360 priv->mii_bus->read = ftgmac100_mdiobus_read;
1361 priv->mii_bus->write = ftgmac100_mdiobus_write;
1362
1363 for (i = 0; i < PHY_MAX_ADDR; i++)
1364 priv->mii_bus->irq[i] = PHY_POLL;
1365
1366 err = mdiobus_register(priv->mii_bus);
1367 if (err) {
1368 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1369 goto err_register_mdiobus;
1370 }
1371
1372 err = ftgmac100_mii_probe(priv);
1373 if (err) {
1374 dev_err(priv->dev, "MII Probe failed!\n");
1375 goto err_mii_probe;
1376 }
1377
1378 return 0;
1379
1380err_mii_probe:
1381 mdiobus_unregister(priv->mii_bus);
1382err_register_mdiobus:
1383 mdiobus_free(priv->mii_bus);
1384 return err;
1385}
1386
1387static void ftgmac100_destroy_mdio(struct net_device *netdev)
1388{
1389 struct ftgmac100 *priv = netdev_priv(netdev);
1390
1391 if (!netdev->phydev)
1392 return;
1393
1394 phy_disconnect(netdev->phydev);
1395 mdiobus_unregister(priv->mii_bus);
1396 mdiobus_free(priv->mii_bus);
1397}
1398
Gavin Shanbd466c32016-07-19 11:54:23 +10001399static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1400{
1401 if (unlikely(nd->state != ncsi_dev_state_functional))
1402 return;
1403
1404 netdev_info(nd->dev, "NCSI interface %s\n",
1405 nd->link_up ? "up" : "down");
1406}
1407
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001408static int ftgmac100_probe(struct platform_device *pdev)
1409{
1410 struct resource *res;
1411 int irq;
1412 struct net_device *netdev;
1413 struct ftgmac100 *priv;
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001414 struct device_node *np;
Gavin Shanbd466c32016-07-19 11:54:23 +10001415 int err = 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001416
1417 if (!pdev)
1418 return -ENODEV;
1419
1420 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1421 if (!res)
1422 return -ENXIO;
1423
1424 irq = platform_get_irq(pdev, 0);
1425 if (irq < 0)
1426 return irq;
1427
1428 /* setup net_device */
1429 netdev = alloc_etherdev(sizeof(*priv));
1430 if (!netdev) {
1431 err = -ENOMEM;
1432 goto err_alloc_etherdev;
1433 }
1434
1435 SET_NETDEV_DEV(netdev, &pdev->dev);
1436
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001437 netdev->ethtool_ops = &ftgmac100_ethtool_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001438 netdev->netdev_ops = &ftgmac100_netdev_ops;
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001439 netdev->watchdog_timeo = 5 * HZ;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001440
1441 platform_set_drvdata(pdev, netdev);
1442
1443 /* setup private data */
1444 priv = netdev_priv(netdev);
1445 priv->netdev = netdev;
1446 priv->dev = &pdev->dev;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001447 INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001448
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001449 /* map io memory */
1450 priv->res = request_mem_region(res->start, resource_size(res),
1451 dev_name(&pdev->dev));
1452 if (!priv->res) {
1453 dev_err(&pdev->dev, "Could not reserve memory region\n");
1454 err = -ENOMEM;
1455 goto err_req_mem;
1456 }
1457
1458 priv->base = ioremap(res->start, resource_size(res));
1459 if (!priv->base) {
1460 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1461 err = -EIO;
1462 goto err_ioremap;
1463 }
1464
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001465 netdev->irq = irq;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001466
Gavin Shan113ce102016-07-19 11:54:22 +10001467 /* MAC address from chip or random one */
Benjamin Herrenschmidtba1b1232017-04-12 13:27:06 +10001468 ftgmac100_initial_mac(priv);
Gavin Shan113ce102016-07-19 11:54:22 +10001469
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001470 np = pdev->dev.of_node;
1471 if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
1472 of_device_is_compatible(np, "aspeed,ast2500-mac"))) {
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301473 priv->rxdes0_edorr_mask = BIT(30);
1474 priv->txdes0_edotr_mask = BIT(30);
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001475 priv->is_aspeed = true;
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301476 } else {
1477 priv->rxdes0_edorr_mask = BIT(15);
1478 priv->txdes0_edotr_mask = BIT(15);
1479 }
1480
Benjamin Herrenschmidt78d28542017-04-12 13:27:02 +10001481 if (np && of_get_property(np, "use-ncsi", NULL)) {
Gavin Shanbd466c32016-07-19 11:54:23 +10001482 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1483 dev_err(&pdev->dev, "NCSI stack not enabled\n");
1484 goto err_ncsi_dev;
1485 }
1486
1487 dev_info(&pdev->dev, "Using NCSI interface\n");
1488 priv->use_ncsi = true;
1489 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1490 if (!priv->ndev)
1491 goto err_ncsi_dev;
1492 } else {
1493 priv->use_ncsi = false;
1494 err = ftgmac100_setup_mdio(netdev);
1495 if (err)
1496 goto err_setup_mdio;
1497 }
1498
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001499 /* Base feature set */
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001500 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
Benjamin Herrenschmidt6db74702017-04-10 11:15:25 +10001501 NETIF_F_GRO | NETIF_F_SG;
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001502
1503 /* AST2400 doesn't have working HW checksum generation */
1504 if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac")))
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001505 netdev->hw_features &= ~NETIF_F_HW_CSUM;
Benjamin Herrenschmidt6aff0bf2017-04-12 13:27:03 +10001506 if (np && of_get_property(np, "no-hw-checksum", NULL))
Benjamin Herrenschmidt8c3ed132017-04-12 13:27:04 +10001507 netdev->hw_features &= ~(NETIF_F_HW_CSUM | NETIF_F_RXCSUM);
1508 netdev->features |= netdev->hw_features;
Gavin Shanbd466c32016-07-19 11:54:23 +10001509
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001510 /* register network device */
1511 err = register_netdev(netdev);
1512 if (err) {
1513 dev_err(&pdev->dev, "Failed to register netdev\n");
1514 goto err_register_netdev;
1515 }
1516
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001517 netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001518
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001519 return 0;
1520
Gavin Shanbd466c32016-07-19 11:54:23 +10001521err_ncsi_dev:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001522err_register_netdev:
Gavin Shaneb418182016-07-19 11:54:21 +10001523 ftgmac100_destroy_mdio(netdev);
1524err_setup_mdio:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001525 iounmap(priv->base);
1526err_ioremap:
1527 release_resource(priv->res);
1528err_req_mem:
1529 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001530 free_netdev(netdev);
1531err_alloc_etherdev:
1532 return err;
1533}
1534
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001535static int ftgmac100_remove(struct platform_device *pdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001536{
1537 struct net_device *netdev;
1538 struct ftgmac100 *priv;
1539
1540 netdev = platform_get_drvdata(pdev);
1541 priv = netdev_priv(netdev);
1542
1543 unregister_netdev(netdev);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001544
1545 /* There's a small chance the reset task will have been re-queued,
1546 * during stop, make sure it's gone before we free the structure.
1547 */
1548 cancel_work_sync(&priv->reset_task);
1549
Gavin Shaneb418182016-07-19 11:54:21 +10001550 ftgmac100_destroy_mdio(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001551
1552 iounmap(priv->base);
1553 release_resource(priv->res);
1554
1555 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001556 free_netdev(netdev);
1557 return 0;
1558}
1559
Gavin Shanbb168e22016-07-19 11:54:24 +10001560static const struct of_device_id ftgmac100_of_match[] = {
1561 { .compatible = "faraday,ftgmac100" },
1562 { }
1563};
1564MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1565
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001566static struct platform_driver ftgmac100_driver = {
Gavin Shanbb168e22016-07-19 11:54:24 +10001567 .probe = ftgmac100_probe,
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001568 .remove = ftgmac100_remove,
Gavin Shanbb168e22016-07-19 11:54:24 +10001569 .driver = {
1570 .name = DRV_NAME,
1571 .of_match_table = ftgmac100_of_match,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001572 },
1573};
Sachin Kamat14f645d2013-03-18 01:50:48 +00001574module_platform_driver(ftgmac100_driver);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001575
1576MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1577MODULE_DESCRIPTION("FTGMAC100 driver");
1578MODULE_LICENSE("GPL");