blob: 7654d5eafeeb4abb3c00d362f1506e3689220f4a [file] [log] [blame]
Jan Ceuleers0977f812012-06-05 03:42:12 +00001/* drivers/net/ethernet/freescale/gianfar.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Gianfar Ethernet Driver
Andy Fleming7f7f5312005-11-11 12:38:59 -06004 * This driver is designed for the non-CPM ethernet controllers
5 * on the 85xx and 83xx family of integrated processors
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based on 8260_io/fcc_enet.c
7 *
8 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -08009 * Maintainer: Kumar Gala
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000010 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Claudiu Manoil20862782014-02-17 12:53:14 +020012 * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000013 * Copyright 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Gianfar: AKA Lambda Draconis, "Dragon"
21 * RA 11 31 24.2
22 * Dec +69 19 52
23 * V 3.84
24 * B-V +1.62
25 *
26 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050027 *
Andy Flemingb31a1d82008-12-16 15:29:15 -080028 * The driver is initialized through of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050033 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * last descriptor of the ring.
36 *
37 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050038 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * corresponding bit in the IMASK register is also set (if
40 * interrupt coalescing is active, then the interrupt may not
41 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040042 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020044 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050045 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * are none left with data (NAPI will stop after a set number of
47 * packets to give time to other tasks, but will eventually
48 * process all the packets). The data arrives inside a
49 * pre-allocated skb, and so after the skb is passed up to the
50 * stack, a new skb must be allocated, and the address field in
51 * the buffer descriptor must be updated to indicate this new
52 * skb.
53 *
54 * When the kernel requests that a packet be transmitted, the
55 * driver starts where it left off last time, and points the
56 * descriptor at the buffer which was passed in. The driver
57 * then informs the DMA engine that there are packets ready to
58 * be transmitted. Once the controller is finished transmitting
59 * the packet, an interrupt may be triggered (under the same
60 * conditions as for reception, but depending on the TXF bit).
61 * The driver then cleans up the buffer.
62 */
63
Joe Perches59deab22011-06-14 08:57:47 +000064#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65#define DEBUG
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/string.h>
69#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040070#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/slab.h>
72#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/delay.h>
74#include <linux/netdevice.h>
75#include <linux/etherdevice.h>
76#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050077#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/spinlock.h>
79#include <linux/mm.h>
Rob Herring5af50732013-09-17 14:28:33 -050080#include <linux/of_address.h>
81#include <linux/of_irq.h>
Grant Likelyfe192a42009-04-25 12:53:12 +000082#include <linux/of_mdio.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080083#include <linux/of_platform.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050084#include <linux/ip.h>
85#include <linux/tcp.h>
86#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080087#include <linux/in.h>
Manfred Rudigiercc772ab2010-04-08 23:10:03 +000088#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#include <asm/io.h>
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +030091#ifdef CONFIG_PPC
Anton Vorontsov7d350972010-06-30 06:39:12 +000092#include <asm/reg.h>
Claudiu Manoil2969b1f2013-10-09 20:20:41 +030093#include <asm/mpc85xx.h>
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +030094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <asm/irq.h>
96#include <asm/uaccess.h>
97#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/dma-mapping.h>
99#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400100#include <linux/mii.h>
101#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -0800102#include <linux/phy_fixed.h>
103#include <linux/of.h>
David Daney4b6ba8a2010-10-26 15:07:13 -0700104#include <linux/of_net.h>
Claudiu Manoilfd31a952014-10-07 10:44:31 +0300105#include <linux/of_address.h>
106#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#include "gianfar.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Andy Fleming7f7f5312005-11-11 12:38:59 -0600112const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int gfar_enet_open(struct net_device *dev);
115static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200116static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static void gfar_timeout(struct net_device *dev);
118static int gfar_close(struct net_device *dev);
Claudiu Manoil76f31e82015-07-13 16:22:03 +0300119static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
120 int alloc_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static int gfar_set_mac_address(struct net_device *dev);
122static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100123static irqreturn_t gfar_error(int irq, void *dev_id);
124static irqreturn_t gfar_transmit(int irq, void *dev_id);
125static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void adjust_link(struct net_device *dev);
Claudiu Manoil6ce29b02014-04-30 14:27:21 +0300127static noinline void gfar_update_link_state(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int init_phy(struct net_device *dev);
Grant Likely74888762011-02-22 21:05:51 -0700129static int gfar_probe(struct platform_device *ofdev);
Grant Likely2dc11582010-08-06 09:25:50 -0600130static int gfar_remove(struct platform_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400131static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static void gfar_set_multi(struct net_device *dev);
133static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500134static void gfar_configure_serdes(struct net_device *dev);
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200135static int gfar_poll_rx(struct napi_struct *napi, int budget);
136static int gfar_poll_tx(struct napi_struct *napi, int budget);
137static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
138static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300139#ifdef CONFIG_NET_POLL_CONTROLLER
140static void gfar_netpoll(struct net_device *dev);
141#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000142int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
Claudiu Manoilc233cf402013-03-19 07:40:02 +0000143static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Claudiu Manoilf23223f2015-07-13 16:22:05 +0300144static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb);
Claudiu Manoilc10650b2014-02-17 12:53:18 +0200145static void gfar_halt_nodisable(struct gfar_private *priv);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600146static void gfar_clear_exact_match(struct net_device *dev);
Joe Perchesb6bc7652010-12-21 02:16:08 -0800147static void gfar_set_mac_for_addr(struct net_device *dev, int num,
148 const u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000149static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151MODULE_AUTHOR("Freescale Semiconductor, Inc");
152MODULE_DESCRIPTION("Gianfar Ethernet Driver");
153MODULE_LICENSE("GPL");
154
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000155static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000156 dma_addr_t buf)
157{
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000158 u32 lstatus;
159
Claudiu Manoila7312d52015-03-13 10:36:28 +0200160 bdp->bufPtr = cpu_to_be32(buf);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000161
162 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000163 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000164 lstatus |= BD_LFLAG(RXBD_WRAP);
165
Claudiu Manoild55398b2014-10-07 10:44:35 +0300166 gfar_wmb();
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000167
Claudiu Manoila7312d52015-03-13 10:36:28 +0200168 bdp->lstatus = cpu_to_be32(lstatus);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000169}
170
Claudiu Manoil76f31e82015-07-13 16:22:03 +0300171static void gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000172{
Anton Vorontsov87283272009-10-12 06:00:39 +0000173 struct gfar_private *priv = netdev_priv(ndev);
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200174 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000175 struct gfar_priv_tx_q *tx_queue = NULL;
176 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000177 struct txbd8 *txbdp;
Kevin Hao03366a332014-12-24 14:05:45 +0800178 u32 __iomem *rfbptr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000179 int i, j;
Anton Vorontsov87283272009-10-12 06:00:39 +0000180
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000181 for (i = 0; i < priv->num_tx_queues; i++) {
182 tx_queue = priv->tx_queue[i];
183 /* Initialize some variables in our dev structure */
184 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
185 tx_queue->dirty_tx = tx_queue->tx_bd_base;
186 tx_queue->cur_tx = tx_queue->tx_bd_base;
187 tx_queue->skb_curtx = 0;
188 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000189
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000190 /* Initialize Transmit Descriptor Ring */
191 txbdp = tx_queue->tx_bd_base;
192 for (j = 0; j < tx_queue->tx_ring_size; j++) {
193 txbdp->lstatus = 0;
194 txbdp->bufPtr = 0;
195 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000196 }
197
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000198 /* Set the last descriptor in the ring to indicate wrap */
199 txbdp--;
Claudiu Manoila7312d52015-03-13 10:36:28 +0200200 txbdp->status = cpu_to_be16(be16_to_cpu(txbdp->status) |
201 TXBD_WRAP);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000202 }
203
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200204 rfbptr = &regs->rfbptr0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000205 for (i = 0; i < priv->num_rx_queues; i++) {
206 rx_queue = priv->rx_queue[i];
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000207
Claudiu Manoil76f31e82015-07-13 16:22:03 +0300208 rx_queue->next_to_clean = 0;
209 rx_queue->next_to_use = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000210
Claudiu Manoil76f31e82015-07-13 16:22:03 +0300211 /* make sure next_to_clean != next_to_use after this
212 * by leaving at least 1 unused descriptor
213 */
214 gfar_alloc_rx_buffs(rx_queue, gfar_rxbd_unused(rx_queue));
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000215
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200216 rx_queue->rfbptr = rfbptr;
217 rfbptr += 2;
Anton Vorontsov87283272009-10-12 06:00:39 +0000218 }
Anton Vorontsov87283272009-10-12 06:00:39 +0000219}
220
221static int gfar_alloc_skb_resources(struct net_device *ndev)
222{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000223 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000224 dma_addr_t addr;
225 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000226 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil369ec162013-02-14 05:00:02 +0000227 struct device *dev = priv->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000228 struct gfar_priv_tx_q *tx_queue = NULL;
229 struct gfar_priv_rx_q *rx_queue = NULL;
230
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000231 priv->total_tx_ring_size = 0;
232 for (i = 0; i < priv->num_tx_queues; i++)
233 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
234
235 priv->total_rx_ring_size = 0;
236 for (i = 0; i < priv->num_rx_queues; i++)
237 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000238
239 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000240 vaddr = dma_alloc_coherent(dev,
Joe Perchesd0320f72013-03-14 13:07:21 +0000241 (priv->total_tx_ring_size *
242 sizeof(struct txbd8)) +
243 (priv->total_rx_ring_size *
244 sizeof(struct rxbd8)),
245 &addr, GFP_KERNEL);
246 if (!vaddr)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000247 return -ENOMEM;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000248
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000249 for (i = 0; i < priv->num_tx_queues; i++) {
250 tx_queue = priv->tx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000251 tx_queue->tx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000252 tx_queue->tx_bd_dma_base = addr;
253 tx_queue->dev = ndev;
254 /* enet DMA only understands physical addresses */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000255 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
256 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000257 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000258
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000259 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000260 for (i = 0; i < priv->num_rx_queues; i++) {
261 rx_queue = priv->rx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000262 rx_queue->rx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000263 rx_queue->rx_bd_dma_base = addr;
Claudiu Manoilf23223f2015-07-13 16:22:05 +0300264 rx_queue->ndev = ndev;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000265 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
266 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000267 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000268
269 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000270 for (i = 0; i < priv->num_tx_queues; i++) {
271 tx_queue = priv->tx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000272 tx_queue->tx_skbuff =
273 kmalloc_array(tx_queue->tx_ring_size,
274 sizeof(*tx_queue->tx_skbuff),
275 GFP_KERNEL);
276 if (!tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000277 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000278
279 for (k = 0; k < tx_queue->tx_ring_size; k++)
280 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000281 }
282
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000283 for (i = 0; i < priv->num_rx_queues; i++) {
284 rx_queue = priv->rx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000285 rx_queue->rx_skbuff =
286 kmalloc_array(rx_queue->rx_ring_size,
287 sizeof(*rx_queue->rx_skbuff),
288 GFP_KERNEL);
289 if (!rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000290 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000291
292 for (j = 0; j < rx_queue->rx_ring_size; j++)
293 rx_queue->rx_skbuff[j] = NULL;
294 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000295
Claudiu Manoil76f31e82015-07-13 16:22:03 +0300296 gfar_init_bds(ndev);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000297
298 return 0;
299
300cleanup:
301 free_skb_resources(priv);
302 return -ENOMEM;
303}
304
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000305static void gfar_init_tx_rx_base(struct gfar_private *priv)
306{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000307 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000308 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000309 int i;
310
311 baddr = &regs->tbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000312 for (i = 0; i < priv->num_tx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000313 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000314 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000315 }
316
317 baddr = &regs->rbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000318 for (i = 0; i < priv->num_rx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000319 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000320 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000321 }
322}
323
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200324static void gfar_init_rqprm(struct gfar_private *priv)
325{
326 struct gfar __iomem *regs = priv->gfargrp[0].regs;
327 u32 __iomem *baddr;
328 int i;
329
330 baddr = &regs->rqprm0;
331 for (i = 0; i < priv->num_rx_queues; i++) {
332 gfar_write(baddr, priv->rx_queue[i]->rx_ring_size |
333 (DEFAULT_RX_LFC_THR << FBTHR_SHIFT));
334 baddr++;
335 }
336}
337
Claudiu Manoil88302642014-02-24 12:13:43 +0200338static void gfar_rx_buff_size_config(struct gfar_private *priv)
339{
Claudiu Manoilf5b720b2014-10-15 19:11:46 +0300340 int frame_size = priv->ndev->mtu + ETH_HLEN + ETH_FCS_LEN;
Claudiu Manoil88302642014-02-24 12:13:43 +0200341
342 /* set this when rx hw offload (TOE) functions are being used */
343 priv->uses_rxfcb = 0;
344
345 if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
346 priv->uses_rxfcb = 1;
347
348 if (priv->hwts_rx_en)
349 priv->uses_rxfcb = 1;
350
351 if (priv->uses_rxfcb)
352 frame_size += GMAC_FCB_LEN;
353
354 frame_size += priv->padding;
355
356 frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
357 INCREMENTAL_BUFFER_SIZE;
358
359 priv->rx_buffer_size = frame_size;
360}
361
Claudiu Manoila328ac92014-02-24 12:13:42 +0200362static void gfar_mac_rx_config(struct gfar_private *priv)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000363{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000364 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000365 u32 rctrl = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000366
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000367 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000368 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000369 /* Program the RIR0 reg with the required distribution */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200370 if (priv->poll_mode == GFAR_SQ_POLLING)
371 gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
372 else /* GFAR_MQ_POLLING */
373 gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000374 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000375
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000376 /* Restore PROMISC mode */
Claudiu Manoila328ac92014-02-24 12:13:42 +0200377 if (priv->ndev->flags & IFF_PROMISC)
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000378 rctrl |= RCTRL_PROM;
379
Claudiu Manoil88302642014-02-24 12:13:43 +0200380 if (priv->ndev->features & NETIF_F_RXCSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000381 rctrl |= RCTRL_CHECKSUMMING;
382
Claudiu Manoil88302642014-02-24 12:13:43 +0200383 if (priv->extended_hash)
384 rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000385
386 if (priv->padding) {
387 rctrl &= ~RCTRL_PAL_MASK;
388 rctrl |= RCTRL_PADDING(priv->padding);
389 }
390
Manfred Rudigier97553f72010-06-11 01:49:05 +0000391 /* Enable HW time stamping if requested from user space */
Claudiu Manoil88302642014-02-24 12:13:43 +0200392 if (priv->hwts_rx_en)
Manfred Rudigier97553f72010-06-11 01:49:05 +0000393 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
394
Claudiu Manoil88302642014-02-24 12:13:43 +0200395 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
Sebastian Pöhnb852b722011-07-26 00:03:13 +0000396 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000397
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200398 /* Clear the LFC bit */
399 gfar_write(&regs->rctrl, rctrl);
400 /* Init flow control threshold values */
401 gfar_init_rqprm(priv);
402 gfar_write(&regs->ptv, DEFAULT_LFC_PTVVAL);
403 rctrl |= RCTRL_LFC;
404
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000405 /* Init rctrl based on our settings */
406 gfar_write(&regs->rctrl, rctrl);
Claudiu Manoila328ac92014-02-24 12:13:42 +0200407}
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000408
Claudiu Manoila328ac92014-02-24 12:13:42 +0200409static void gfar_mac_tx_config(struct gfar_private *priv)
410{
411 struct gfar __iomem *regs = priv->gfargrp[0].regs;
412 u32 tctrl = 0;
413
414 if (priv->ndev->features & NETIF_F_IP_CSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000415 tctrl |= TCTRL_INIT_CSUM;
416
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +0000417 if (priv->prio_sched_en)
418 tctrl |= TCTRL_TXSCHED_PRIO;
419 else {
420 tctrl |= TCTRL_TXSCHED_WRRS;
421 gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
422 gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
423 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000424
Claudiu Manoil88302642014-02-24 12:13:43 +0200425 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
426 tctrl |= TCTRL_VLINS;
427
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000428 gfar_write(&regs->tctrl, tctrl);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000429}
430
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200431static void gfar_configure_coalescing(struct gfar_private *priv,
432 unsigned long tx_mask, unsigned long rx_mask)
433{
434 struct gfar __iomem *regs = priv->gfargrp[0].regs;
435 u32 __iomem *baddr;
436
437 if (priv->mode == MQ_MG_MODE) {
438 int i = 0;
439
440 baddr = &regs->txic0;
441 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
442 gfar_write(baddr + i, 0);
443 if (likely(priv->tx_queue[i]->txcoalescing))
444 gfar_write(baddr + i, priv->tx_queue[i]->txic);
445 }
446
447 baddr = &regs->rxic0;
448 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
449 gfar_write(baddr + i, 0);
450 if (likely(priv->rx_queue[i]->rxcoalescing))
451 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
452 }
453 } else {
454 /* Backward compatible case -- even if we enable
455 * multiple queues, there's only single reg to program
456 */
457 gfar_write(&regs->txic, 0);
458 if (likely(priv->tx_queue[0]->txcoalescing))
459 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
460
461 gfar_write(&regs->rxic, 0);
462 if (unlikely(priv->rx_queue[0]->rxcoalescing))
463 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
464 }
465}
466
467void gfar_configure_coalescing_all(struct gfar_private *priv)
468{
469 gfar_configure_coalescing(priv, 0xFF, 0xFF);
470}
471
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000472static struct net_device_stats *gfar_get_stats(struct net_device *dev)
473{
474 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000475 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
476 unsigned long tx_packets = 0, tx_bytes = 0;
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000477 int i;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000478
479 for (i = 0; i < priv->num_rx_queues; i++) {
480 rx_packets += priv->rx_queue[i]->stats.rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000481 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000482 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
483 }
484
485 dev->stats.rx_packets = rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000486 dev->stats.rx_bytes = rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000487 dev->stats.rx_dropped = rx_dropped;
488
489 for (i = 0; i < priv->num_tx_queues; i++) {
Eric Dumazet1ac9ad12011-01-12 12:13:14 +0000490 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
491 tx_packets += priv->tx_queue[i]->stats.tx_packets;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000492 }
493
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000494 dev->stats.tx_bytes = tx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000495 dev->stats.tx_packets = tx_packets;
496
497 return &dev->stats;
498}
499
Claudiu Manoil3d23a052015-05-06 18:07:30 +0300500static int gfar_set_mac_addr(struct net_device *dev, void *p)
501{
502 eth_mac_addr(dev, p);
503
504 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
505
506 return 0;
507}
508
Andy Fleming26ccfc32009-03-10 12:58:28 +0000509static const struct net_device_ops gfar_netdev_ops = {
510 .ndo_open = gfar_enet_open,
511 .ndo_start_xmit = gfar_start_xmit,
512 .ndo_stop = gfar_close,
513 .ndo_change_mtu = gfar_change_mtu,
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000514 .ndo_set_features = gfar_set_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000515 .ndo_set_rx_mode = gfar_set_multi,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000516 .ndo_tx_timeout = gfar_timeout,
517 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000518 .ndo_get_stats = gfar_get_stats,
Claudiu Manoil3d23a052015-05-06 18:07:30 +0300519 .ndo_set_mac_address = gfar_set_mac_addr,
Ben Hutchings240c1022009-07-09 17:54:35 +0000520 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000521#ifdef CONFIG_NET_POLL_CONTROLLER
522 .ndo_poll_controller = gfar_netpoll,
523#endif
524};
525
Claudiu Manoilefeddce2014-02-17 12:53:17 +0200526static void gfar_ints_disable(struct gfar_private *priv)
527{
528 int i;
529 for (i = 0; i < priv->num_grps; i++) {
530 struct gfar __iomem *regs = priv->gfargrp[i].regs;
531 /* Clear IEVENT */
532 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
533
534 /* Initialize IMASK */
535 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
536 }
537}
538
539static void gfar_ints_enable(struct gfar_private *priv)
540{
541 int i;
542 for (i = 0; i < priv->num_grps; i++) {
543 struct gfar __iomem *regs = priv->gfargrp[i].regs;
544 /* Unmask the interrupts we look for */
545 gfar_write(&regs->imask, IMASK_DEFAULT);
546 }
547}
548
Kevin Hao91c53f762014-12-24 14:05:44 +0800549static void lock_tx_qs(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000550{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000551 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000552
553 for (i = 0; i < priv->num_tx_queues; i++)
554 spin_lock(&priv->tx_queue[i]->txlock);
555}
556
Kevin Hao91c53f762014-12-24 14:05:44 +0800557static void unlock_tx_qs(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000558{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000559 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000560
561 for (i = 0; i < priv->num_tx_queues; i++)
562 spin_unlock(&priv->tx_queue[i]->txlock);
563}
564
Claudiu Manoil20862782014-02-17 12:53:14 +0200565static int gfar_alloc_tx_queues(struct gfar_private *priv)
566{
567 int i;
568
569 for (i = 0; i < priv->num_tx_queues; i++) {
570 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
571 GFP_KERNEL);
572 if (!priv->tx_queue[i])
573 return -ENOMEM;
574
575 priv->tx_queue[i]->tx_skbuff = NULL;
576 priv->tx_queue[i]->qindex = i;
577 priv->tx_queue[i]->dev = priv->ndev;
578 spin_lock_init(&(priv->tx_queue[i]->txlock));
579 }
580 return 0;
581}
582
583static int gfar_alloc_rx_queues(struct gfar_private *priv)
584{
585 int i;
586
587 for (i = 0; i < priv->num_rx_queues; i++) {
588 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
589 GFP_KERNEL);
590 if (!priv->rx_queue[i])
591 return -ENOMEM;
592
593 priv->rx_queue[i]->rx_skbuff = NULL;
594 priv->rx_queue[i]->qindex = i;
Claudiu Manoilf23223f2015-07-13 16:22:05 +0300595 priv->rx_queue[i]->ndev = priv->ndev;
Claudiu Manoil20862782014-02-17 12:53:14 +0200596 }
597 return 0;
598}
599
600static void gfar_free_tx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000601{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000602 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000603
604 for (i = 0; i < priv->num_tx_queues; i++)
605 kfree(priv->tx_queue[i]);
606}
607
Claudiu Manoil20862782014-02-17 12:53:14 +0200608static void gfar_free_rx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000609{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000610 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000611
612 for (i = 0; i < priv->num_rx_queues; i++)
613 kfree(priv->rx_queue[i]);
614}
615
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000616static void unmap_group_regs(struct gfar_private *priv)
617{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000618 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000619
620 for (i = 0; i < MAXGROUPS; i++)
621 if (priv->gfargrp[i].regs)
622 iounmap(priv->gfargrp[i].regs);
623}
624
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000625static void free_gfar_dev(struct gfar_private *priv)
626{
627 int i, j;
628
629 for (i = 0; i < priv->num_grps; i++)
630 for (j = 0; j < GFAR_NUM_IRQS; j++) {
631 kfree(priv->gfargrp[i].irqinfo[j]);
632 priv->gfargrp[i].irqinfo[j] = NULL;
633 }
634
635 free_netdev(priv->ndev);
636}
637
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000638static void disable_napi(struct gfar_private *priv)
639{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000640 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000641
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200642 for (i = 0; i < priv->num_grps; i++) {
643 napi_disable(&priv->gfargrp[i].napi_rx);
644 napi_disable(&priv->gfargrp[i].napi_tx);
645 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000646}
647
648static void enable_napi(struct gfar_private *priv)
649{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000650 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000651
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200652 for (i = 0; i < priv->num_grps; i++) {
653 napi_enable(&priv->gfargrp[i].napi_rx);
654 napi_enable(&priv->gfargrp[i].napi_tx);
655 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000656}
657
658static int gfar_parse_group(struct device_node *np,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000659 struct gfar_private *priv, const char *model)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000660{
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000661 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000662 int i;
663
Paul Gortmaker7c1e7e92013-02-04 09:49:42 +0000664 for (i = 0; i < GFAR_NUM_IRQS; i++) {
665 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
666 GFP_KERNEL);
667 if (!grp->irqinfo[i])
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000668 return -ENOMEM;
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000669 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000670
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000671 grp->regs = of_iomap(np, 0);
672 if (!grp->regs)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000673 return -ENOMEM;
674
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000675 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000676
677 /* If we aren't the FEC we have multiple interrupts */
678 if (model && strcasecmp(model, "FEC")) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000679 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
680 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
681 if (gfar_irq(grp, TX)->irq == NO_IRQ ||
682 gfar_irq(grp, RX)->irq == NO_IRQ ||
683 gfar_irq(grp, ER)->irq == NO_IRQ)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000684 return -EINVAL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000685 }
686
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000687 grp->priv = priv;
688 spin_lock_init(&grp->grplock);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000689 if (priv->mode == MQ_MG_MODE) {
Jingchang Lu55917642015-03-13 10:52:32 +0200690 u32 rxq_mask, txq_mask;
691 int ret;
692
693 grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
694 grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
695
696 ret = of_property_read_u32(np, "fsl,rx-bit-map", &rxq_mask);
697 if (!ret) {
698 grp->rx_bit_map = rxq_mask ?
699 rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
700 }
701
702 ret = of_property_read_u32(np, "fsl,tx-bit-map", &txq_mask);
703 if (!ret) {
704 grp->tx_bit_map = txq_mask ?
705 txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
706 }
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200707
708 if (priv->poll_mode == GFAR_SQ_POLLING) {
709 /* One Q per interrupt group: Q0 to G0, Q1 to G1 */
710 grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
711 grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200712 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000713 } else {
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000714 grp->rx_bit_map = 0xFF;
715 grp->tx_bit_map = 0xFF;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000716 }
Claudiu Manoil20862782014-02-17 12:53:14 +0200717
718 /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
719 * right to left, so we need to revert the 8 bits to get the q index
720 */
721 grp->rx_bit_map = bitrev8(grp->rx_bit_map);
722 grp->tx_bit_map = bitrev8(grp->tx_bit_map);
723
724 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
725 * also assign queues to groups
726 */
727 for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200728 if (!grp->rx_queue)
729 grp->rx_queue = priv->rx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200730 grp->num_rx_queues++;
731 grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
732 priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
733 priv->rx_queue[i]->grp = grp;
734 }
735
736 for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200737 if (!grp->tx_queue)
738 grp->tx_queue = priv->tx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200739 grp->num_tx_queues++;
740 grp->tstat |= (TSTAT_CLEAR_THALT >> i);
741 priv->tqueue |= (TQUEUE_EN0 >> i);
742 priv->tx_queue[i]->grp = grp;
743 }
744
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000745 priv->num_grps++;
746
747 return 0;
748}
749
Tobias Waldekranzf50724c2015-03-05 14:48:23 +0100750static int gfar_of_group_count(struct device_node *np)
751{
752 struct device_node *child;
753 int num = 0;
754
755 for_each_available_child_of_node(np, child)
756 if (!of_node_cmp(child->name, "queue-group"))
757 num++;
758
759 return num;
760}
761
Grant Likely2dc11582010-08-06 09:25:50 -0600762static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800763{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800764 const char *model;
765 const char *ctype;
766 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000767 int err = 0, i;
768 struct net_device *dev = NULL;
769 struct gfar_private *priv = NULL;
Grant Likely61c7a082010-04-13 16:12:29 -0700770 struct device_node *np = ofdev->dev.of_node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000771 struct device_node *child = NULL;
Jingchang Lu55917642015-03-13 10:52:32 +0200772 struct property *stash;
773 u32 stash_len = 0;
774 u32 stash_idx = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000775 unsigned int num_tx_qs, num_rx_qs;
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200776 unsigned short mode, poll_mode;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800777
Kevin Hao4b222ca2015-01-28 20:06:48 +0800778 if (!np)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800779 return -ENODEV;
780
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200781 if (of_device_is_compatible(np, "fsl,etsec2")) {
782 mode = MQ_MG_MODE;
783 poll_mode = GFAR_SQ_POLLING;
784 } else {
785 mode = SQ_SG_MODE;
786 poll_mode = GFAR_SQ_POLLING;
787 }
788
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200789 if (mode == SQ_SG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200790 num_tx_qs = 1;
791 num_rx_qs = 1;
792 } else { /* MQ_MG_MODE */
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200793 /* get the actual number of supported groups */
Tobias Waldekranzf50724c2015-03-05 14:48:23 +0100794 unsigned int num_grps = gfar_of_group_count(np);
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200795
796 if (num_grps == 0 || num_grps > MAXGROUPS) {
797 dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
798 num_grps);
799 pr_err("Cannot do alloc_etherdev, aborting\n");
800 return -EINVAL;
801 }
802
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200803 if (poll_mode == GFAR_SQ_POLLING) {
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200804 num_tx_qs = num_grps; /* one txq per int group */
805 num_rx_qs = num_grps; /* one rxq per int group */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200806 } else { /* GFAR_MQ_POLLING */
Jingchang Lu55917642015-03-13 10:52:32 +0200807 u32 tx_queues, rx_queues;
808 int ret;
809
810 /* parse the num of HW tx and rx queues */
811 ret = of_property_read_u32(np, "fsl,num_tx_queues",
812 &tx_queues);
813 num_tx_qs = ret ? 1 : tx_queues;
814
815 ret = of_property_read_u32(np, "fsl,num_rx_queues",
816 &rx_queues);
817 num_rx_qs = ret ? 1 : rx_queues;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200818 }
819 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000820
821 if (num_tx_qs > MAX_TX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000822 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
823 num_tx_qs, MAX_TX_QS);
824 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000825 return -EINVAL;
826 }
827
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000828 if (num_rx_qs > MAX_RX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000829 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
830 num_rx_qs, MAX_RX_QS);
831 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000832 return -EINVAL;
833 }
834
835 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
836 dev = *pdev;
837 if (NULL == dev)
838 return -ENOMEM;
839
840 priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000841 priv->ndev = dev;
842
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200843 priv->mode = mode;
844 priv->poll_mode = poll_mode;
845
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000846 priv->num_tx_queues = num_tx_qs;
Ben Hutchingsfe069122010-09-27 08:27:37 +0000847 netif_set_real_num_rx_queues(dev, num_rx_qs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000848 priv->num_rx_queues = num_rx_qs;
Claudiu Manoil20862782014-02-17 12:53:14 +0200849
850 err = gfar_alloc_tx_queues(priv);
851 if (err)
852 goto tx_alloc_failed;
853
854 err = gfar_alloc_rx_queues(priv);
855 if (err)
856 goto rx_alloc_failed;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800857
Jingchang Lu55917642015-03-13 10:52:32 +0200858 err = of_property_read_string(np, "model", &model);
859 if (err) {
860 pr_err("Device model property missing, aborting\n");
861 goto rx_alloc_failed;
862 }
863
Jan Ceuleers0977f812012-06-05 03:42:12 +0000864 /* Init Rx queue filer rule set linked list */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700865 INIT_LIST_HEAD(&priv->rx_list.list);
866 priv->rx_list.count = 0;
867 mutex_init(&priv->rx_queue_access);
868
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000869 for (i = 0; i < MAXGROUPS; i++)
870 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800871
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000872 /* Parse and initialize group specific information */
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200873 if (priv->mode == MQ_MG_MODE) {
Tobias Waldekranzf50724c2015-03-05 14:48:23 +0100874 for_each_available_child_of_node(np, child) {
875 if (of_node_cmp(child->name, "queue-group"))
876 continue;
877
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000878 err = gfar_parse_group(child, priv, model);
879 if (err)
880 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800881 }
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200882 } else { /* SQ_SG_MODE */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000883 err = gfar_parse_group(np, priv, model);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000884 if (err)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000885 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800886 }
887
Jingchang Lu55917642015-03-13 10:52:32 +0200888 stash = of_find_property(np, "bd-stash", NULL);
Andy Fleming4d7902f2009-02-04 16:43:44 -0800889
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000890 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800891 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
892 priv->bd_stash_en = 1;
893 }
894
Jingchang Lu55917642015-03-13 10:52:32 +0200895 err = of_property_read_u32(np, "rx-stash-len", &stash_len);
Andy Fleming4d7902f2009-02-04 16:43:44 -0800896
Jingchang Lu55917642015-03-13 10:52:32 +0200897 if (err == 0)
898 priv->rx_stash_size = stash_len;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800899
Jingchang Lu55917642015-03-13 10:52:32 +0200900 err = of_property_read_u32(np, "rx-stash-idx", &stash_idx);
Andy Fleming4d7902f2009-02-04 16:43:44 -0800901
Jingchang Lu55917642015-03-13 10:52:32 +0200902 if (err == 0)
903 priv->rx_stash_index = stash_idx;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800904
905 if (stash_len || stash_idx)
906 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
907
Andy Flemingb31a1d82008-12-16 15:29:15 -0800908 mac_addr = of_get_mac_address(np);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000909
Andy Flemingb31a1d82008-12-16 15:29:15 -0800910 if (mac_addr)
Joe Perches6a3c910c2011-11-16 09:38:02 +0000911 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800912
913 if (model && !strcasecmp(model, "TSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200914 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000915 FSL_GIANFAR_DEV_HAS_COALESCE |
916 FSL_GIANFAR_DEV_HAS_RMON |
917 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
918
Andy Flemingb31a1d82008-12-16 15:29:15 -0800919 if (model && !strcasecmp(model, "eTSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200920 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000921 FSL_GIANFAR_DEV_HAS_COALESCE |
922 FSL_GIANFAR_DEV_HAS_RMON |
923 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000924 FSL_GIANFAR_DEV_HAS_CSUM |
925 FSL_GIANFAR_DEV_HAS_VLAN |
926 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
927 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
928 FSL_GIANFAR_DEV_HAS_TIMER;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800929
Jingchang Lu55917642015-03-13 10:52:32 +0200930 err = of_property_read_string(np, "phy-connection-type", &ctype);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800931
932 /* We only care about rgmii-id. The rest are autodetected */
Jingchang Lu55917642015-03-13 10:52:32 +0200933 if (err == 0 && !strcmp(ctype, "rgmii-id"))
Andy Flemingb31a1d82008-12-16 15:29:15 -0800934 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
935 else
936 priv->interface = PHY_INTERFACE_MODE_MII;
937
Jingchang Lu55917642015-03-13 10:52:32 +0200938 if (of_find_property(np, "fsl,magic-packet", NULL))
Andy Flemingb31a1d82008-12-16 15:29:15 -0800939 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
940
Grant Likelyfe192a42009-04-25 12:53:12 +0000941 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800942
Florian Fainellibe403642014-05-22 09:47:48 -0700943 /* In the case of a fixed PHY, the DT node associated
944 * to the PHY is the Ethernet MAC DT node.
945 */
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200946 if (!priv->phy_node && of_phy_is_fixed_link(np)) {
Florian Fainellibe403642014-05-22 09:47:48 -0700947 err = of_phy_register_fixed_link(np);
948 if (err)
949 goto err_grp_init;
950
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200951 priv->phy_node = of_node_get(np);
Florian Fainellibe403642014-05-22 09:47:48 -0700952 }
953
Andy Flemingb31a1d82008-12-16 15:29:15 -0800954 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000955 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800956
957 return 0;
958
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000959err_grp_init:
960 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +0200961rx_alloc_failed:
962 gfar_free_rx_queues(priv);
963tx_alloc_failed:
964 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000965 free_gfar_dev(priv);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800966 return err;
967}
968
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000969static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000970{
971 struct hwtstamp_config config;
972 struct gfar_private *priv = netdev_priv(netdev);
973
974 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
975 return -EFAULT;
976
977 /* reserved for future extensions */
978 if (config.flags)
979 return -EINVAL;
980
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000981 switch (config.tx_type) {
982 case HWTSTAMP_TX_OFF:
983 priv->hwts_tx_en = 0;
984 break;
985 case HWTSTAMP_TX_ON:
986 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
987 return -ERANGE;
988 priv->hwts_tx_en = 1;
989 break;
990 default:
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000991 return -ERANGE;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000992 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000993
994 switch (config.rx_filter) {
995 case HWTSTAMP_FILTER_NONE:
Manfred Rudigier97553f72010-06-11 01:49:05 +0000996 if (priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000997 priv->hwts_rx_en = 0;
Claudiu Manoil08511332014-02-24 12:13:45 +0200998 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000999 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001000 break;
1001 default:
1002 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
1003 return -ERANGE;
Manfred Rudigier97553f72010-06-11 01:49:05 +00001004 if (!priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +00001005 priv->hwts_rx_en = 1;
Claudiu Manoil08511332014-02-24 12:13:45 +02001006 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +00001007 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001008 config.rx_filter = HWTSTAMP_FILTER_ALL;
1009 break;
1010 }
1011
1012 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1013 -EFAULT : 0;
1014}
1015
Ben Hutchingsca0c88c2013-11-18 23:05:27 +00001016static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
1017{
1018 struct hwtstamp_config config;
1019 struct gfar_private *priv = netdev_priv(netdev);
1020
1021 config.flags = 0;
1022 config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1023 config.rx_filter = (priv->hwts_rx_en ?
1024 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
1025
1026 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1027 -EFAULT : 0;
1028}
1029
Clifford Wolf0faac9f2009-01-09 10:23:11 +00001030static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1031{
1032 struct gfar_private *priv = netdev_priv(dev);
1033
1034 if (!netif_running(dev))
1035 return -EINVAL;
1036
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001037 if (cmd == SIOCSHWTSTAMP)
Ben Hutchingsca0c88c2013-11-18 23:05:27 +00001038 return gfar_hwtstamp_set(dev, rq);
1039 if (cmd == SIOCGHWTSTAMP)
1040 return gfar_hwtstamp_get(dev, rq);
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001041
Clifford Wolf0faac9f2009-01-09 10:23:11 +00001042 if (!priv->phydev)
1043 return -ENODEV;
1044
Richard Cochran28b04112010-07-17 08:48:55 +00001045 return phy_mii_ioctl(priv->phydev, rq, cmd);
Clifford Wolf0faac9f2009-01-09 10:23:11 +00001046}
1047
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001048static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
1049 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001050{
1051 u32 rqfpr = FPR_FILER_MASK;
1052 u32 rqfcr = 0x0;
1053
1054 rqfar--;
1055 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001056 priv->ftp_rqfpr[rqfar] = rqfpr;
1057 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001058 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1059
1060 rqfar--;
1061 rqfcr = RQFCR_CMP_NOMATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001062 priv->ftp_rqfpr[rqfar] = rqfpr;
1063 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001064 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1065
1066 rqfar--;
1067 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
1068 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001069 priv->ftp_rqfcr[rqfar] = rqfcr;
1070 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001071 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1072
1073 rqfar--;
1074 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
1075 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001076 priv->ftp_rqfcr[rqfar] = rqfcr;
1077 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001078 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1079
1080 return rqfar;
1081}
1082
1083static void gfar_init_filer_table(struct gfar_private *priv)
1084{
1085 int i = 0x0;
1086 u32 rqfar = MAX_FILER_IDX;
1087 u32 rqfcr = 0x0;
1088 u32 rqfpr = FPR_FILER_MASK;
1089
1090 /* Default rule */
1091 rqfcr = RQFCR_CMP_MATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001092 priv->ftp_rqfcr[rqfar] = rqfcr;
1093 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001094 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1095
1096 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1097 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1098 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1099 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1100 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1101 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1102
Uwe Kleine-König85dd08e2010-06-11 12:16:55 +02001103 /* cur_filer_idx indicated the first non-masked rule */
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001104 priv->cur_filer_idx = rqfar;
1105
1106 /* Rest are masked rules */
1107 rqfcr = RQFCR_CMP_NOMATCH;
1108 for (i = 0; i < rqfar; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001109 priv->ftp_rqfcr[i] = rqfcr;
1110 priv->ftp_rqfpr[i] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001111 gfar_write_filer(priv, i, rqfcr, rqfpr);
1112 }
1113}
1114
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001115#ifdef CONFIG_PPC
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001116static void __gfar_detect_errata_83xx(struct gfar_private *priv)
Anton Vorontsov7d350972010-06-30 06:39:12 +00001117{
Anton Vorontsov7d350972010-06-30 06:39:12 +00001118 unsigned int pvr = mfspr(SPRN_PVR);
1119 unsigned int svr = mfspr(SPRN_SVR);
1120 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1121 unsigned int rev = svr & 0xffff;
1122
1123 /* MPC8313 Rev 2.0 and higher; All MPC837x */
1124 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001125 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001126 priv->errata |= GFAR_ERRATA_74;
1127
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001128 /* MPC8313 and MPC837x all rev */
1129 if ((pvr == 0x80850010 && mod == 0x80b0) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001130 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001131 priv->errata |= GFAR_ERRATA_76;
1132
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001133 /* MPC8313 Rev < 2.0 */
1134 if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00001135 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001136}
1137
1138static void __gfar_detect_errata_85xx(struct gfar_private *priv)
1139{
1140 unsigned int svr = mfspr(SPRN_SVR);
1141
1142 if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
1143 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil53fad772013-10-09 20:20:42 +03001144 if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
1145 ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
1146 priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001147}
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001148#endif
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001149
1150static void gfar_detect_errata(struct gfar_private *priv)
1151{
1152 struct device *dev = &priv->ofdev->dev;
1153
1154 /* no plans to fix */
1155 priv->errata |= GFAR_ERRATA_A002;
1156
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001157#ifdef CONFIG_PPC
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001158 if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
1159 __gfar_detect_errata_85xx(priv);
1160 else /* non-mpc85xx parts, i.e. e300 core based */
1161 __gfar_detect_errata_83xx(priv);
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001162#endif
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00001163
Anton Vorontsov7d350972010-06-30 06:39:12 +00001164 if (priv->errata)
1165 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1166 priv->errata);
1167}
1168
Claudiu Manoil08511332014-02-24 12:13:45 +02001169void gfar_mac_reset(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Claudiu Manoil20862782014-02-17 12:53:14 +02001171 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Claudiu Manoila328ac92014-02-24 12:13:42 +02001172 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001175 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Andy Flemingb98ac702009-02-04 16:38:05 -08001177 /* We need to delay at least 3 TX clocks */
Claudiu Manoila328ac92014-02-24 12:13:42 +02001178 udelay(3);
Andy Flemingb98ac702009-02-04 16:38:05 -08001179
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001180 /* the soft reset bit is not self-resetting, so we need to
1181 * clear it before resuming normal operation
1182 */
Claudiu Manoil20862782014-02-17 12:53:14 +02001183 gfar_write(&regs->maccfg1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Claudiu Manoila328ac92014-02-24 12:13:42 +02001185 udelay(3);
1186
Claudiu Manoil88302642014-02-24 12:13:43 +02001187 /* Compute rx_buff_size based on config flags */
1188 gfar_rx_buff_size_config(priv);
1189
1190 /* Initialize the max receive frame/buffer lengths */
1191 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Claudiu Manoila328ac92014-02-24 12:13:42 +02001192 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1193
1194 /* Initialize the Minimum Frame Length Register */
1195 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* Initialize MACCFG2. */
Anton Vorontsov7d350972010-06-30 06:39:12 +00001198 tempval = MACCFG2_INIT_SETTINGS;
Claudiu Manoil88302642014-02-24 12:13:43 +02001199
1200 /* If the mtu is larger than the max size for standard
1201 * ethernet frames (ie, a jumbo frame), then set maccfg2
1202 * to allow huge frames, and to check the length
1203 */
1204 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
1205 gfar_has_errata(priv, GFAR_ERRATA_74))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001206 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
Claudiu Manoil88302642014-02-24 12:13:43 +02001207
Anton Vorontsov7d350972010-06-30 06:39:12 +00001208 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Claudiu Manoila328ac92014-02-24 12:13:42 +02001210 /* Clear mac addr hash registers */
1211 gfar_write(&regs->igaddr0, 0);
1212 gfar_write(&regs->igaddr1, 0);
1213 gfar_write(&regs->igaddr2, 0);
1214 gfar_write(&regs->igaddr3, 0);
1215 gfar_write(&regs->igaddr4, 0);
1216 gfar_write(&regs->igaddr5, 0);
1217 gfar_write(&regs->igaddr6, 0);
1218 gfar_write(&regs->igaddr7, 0);
1219
1220 gfar_write(&regs->gaddr0, 0);
1221 gfar_write(&regs->gaddr1, 0);
1222 gfar_write(&regs->gaddr2, 0);
1223 gfar_write(&regs->gaddr3, 0);
1224 gfar_write(&regs->gaddr4, 0);
1225 gfar_write(&regs->gaddr5, 0);
1226 gfar_write(&regs->gaddr6, 0);
1227 gfar_write(&regs->gaddr7, 0);
1228
1229 if (priv->extended_hash)
1230 gfar_clear_exact_match(priv->ndev);
1231
1232 gfar_mac_rx_config(priv);
1233
1234 gfar_mac_tx_config(priv);
1235
1236 gfar_set_mac_address(priv->ndev);
1237
1238 gfar_set_multi(priv->ndev);
1239
1240 /* clear ievent and imask before configuring coalescing */
1241 gfar_ints_disable(priv);
1242
1243 /* Configure the coalescing support */
1244 gfar_configure_coalescing_all(priv);
1245}
1246
1247static void gfar_hw_init(struct gfar_private *priv)
1248{
1249 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1250 u32 attrs;
1251
1252 /* Stop the DMA engine now, in case it was running before
1253 * (The firmware could have used it, and left it running).
1254 */
1255 gfar_halt(priv);
1256
1257 gfar_mac_reset(priv);
1258
1259 /* Zero out the rmon mib registers if it has them */
1260 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1261 memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1262
1263 /* Mask off the CAM interrupts */
1264 gfar_write(&regs->rmon.cam1, 0xffffffff);
1265 gfar_write(&regs->rmon.cam2, 0xffffffff);
1266 }
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001269 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Claudiu Manoil34018fd2014-02-17 12:53:15 +02001271 /* Set the extraction length and index */
1272 attrs = ATTRELI_EL(priv->rx_stash_size) |
1273 ATTRELI_EI(priv->rx_stash_index);
1274
1275 gfar_write(&regs->attreli, attrs);
1276
1277 /* Start with defaults, and add stashing
1278 * depending on driver parameters
1279 */
1280 attrs = ATTR_INIT_SETTINGS;
1281
1282 if (priv->bd_stash_en)
1283 attrs |= ATTR_BDSTASH;
1284
1285 if (priv->rx_stash_size != 0)
1286 attrs |= ATTR_BUFSTASH;
1287
1288 gfar_write(&regs->attr, attrs);
1289
1290 /* FIFO configs */
1291 gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
1292 gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
1293 gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
1294
Claudiu Manoil20862782014-02-17 12:53:14 +02001295 /* Program the interrupt steering regs, only for MG devices */
1296 if (priv->num_grps > 1)
1297 gfar_write_isrg(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Xiubo Li898157e2014-06-04 16:49:16 +08001300static void gfar_init_addr_hash_table(struct gfar_private *priv)
Claudiu Manoil20862782014-02-17 12:53:14 +02001301{
1302 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001303
Andy Flemingb31a1d82008-12-16 15:29:15 -08001304 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001305 priv->extended_hash = 1;
1306 priv->hash_width = 9;
1307
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001308 priv->hash_regs[0] = &regs->igaddr0;
1309 priv->hash_regs[1] = &regs->igaddr1;
1310 priv->hash_regs[2] = &regs->igaddr2;
1311 priv->hash_regs[3] = &regs->igaddr3;
1312 priv->hash_regs[4] = &regs->igaddr4;
1313 priv->hash_regs[5] = &regs->igaddr5;
1314 priv->hash_regs[6] = &regs->igaddr6;
1315 priv->hash_regs[7] = &regs->igaddr7;
1316 priv->hash_regs[8] = &regs->gaddr0;
1317 priv->hash_regs[9] = &regs->gaddr1;
1318 priv->hash_regs[10] = &regs->gaddr2;
1319 priv->hash_regs[11] = &regs->gaddr3;
1320 priv->hash_regs[12] = &regs->gaddr4;
1321 priv->hash_regs[13] = &regs->gaddr5;
1322 priv->hash_regs[14] = &regs->gaddr6;
1323 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001324
1325 } else {
1326 priv->extended_hash = 0;
1327 priv->hash_width = 8;
1328
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001329 priv->hash_regs[0] = &regs->gaddr0;
1330 priv->hash_regs[1] = &regs->gaddr1;
1331 priv->hash_regs[2] = &regs->gaddr2;
1332 priv->hash_regs[3] = &regs->gaddr3;
1333 priv->hash_regs[4] = &regs->gaddr4;
1334 priv->hash_regs[5] = &regs->gaddr5;
1335 priv->hash_regs[6] = &regs->gaddr6;
1336 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001337 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001338}
1339
1340/* Set up the ethernet device structure, private data,
1341 * and anything else we need before we start
1342 */
1343static int gfar_probe(struct platform_device *ofdev)
1344{
1345 struct net_device *dev = NULL;
1346 struct gfar_private *priv = NULL;
1347 int err = 0, i;
1348
1349 err = gfar_of_init(ofdev, &dev);
1350
1351 if (err)
1352 return err;
1353
1354 priv = netdev_priv(dev);
1355 priv->ndev = dev;
1356 priv->ofdev = ofdev;
1357 priv->dev = &ofdev->dev;
1358 SET_NETDEV_DEV(dev, &ofdev->dev);
1359
1360 spin_lock_init(&priv->bflock);
1361 INIT_WORK(&priv->reset_task, gfar_reset_task);
1362
1363 platform_set_drvdata(ofdev, priv);
1364
1365 gfar_detect_errata(priv);
1366
Claudiu Manoil20862782014-02-17 12:53:14 +02001367 /* Set the dev->base_addr to the gfar reg region */
1368 dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
1369
1370 /* Fill in the dev structure */
1371 dev->watchdog_timeo = TX_TIMEOUT;
1372 dev->mtu = 1500;
1373 dev->netdev_ops = &gfar_netdev_ops;
1374 dev->ethtool_ops = &gfar_ethtool_ops;
1375
1376 /* Register for napi ...We are registering NAPI for each grp */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02001377 for (i = 0; i < priv->num_grps; i++) {
1378 if (priv->poll_mode == GFAR_SQ_POLLING) {
1379 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1380 gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
1381 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1382 gfar_poll_tx_sq, 2);
1383 } else {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02001384 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1385 gfar_poll_rx, GFAR_DEV_WEIGHT);
1386 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1387 gfar_poll_tx, 2);
1388 }
1389 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001390
1391 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
1392 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
1393 NETIF_F_RXCSUM;
1394 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
1395 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
1396 }
1397
1398 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
1399 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
1400 NETIF_F_HW_VLAN_CTAG_RX;
1401 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1402 }
1403
Claudiu Manoil3d23a052015-05-06 18:07:30 +03001404 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1405
Claudiu Manoil20862782014-02-17 12:53:14 +02001406 gfar_init_addr_hash_table(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001407
Claudiu Manoil532c37b2014-02-17 12:53:16 +02001408 /* Insert receive time stamps into padding alignment bytes */
1409 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1410 priv->padding = 8;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001411
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001412 if (dev->features & NETIF_F_IP_CSUM ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001413 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
Wu Jiajun-B06378bee9e582012-05-21 23:00:48 +00001414 dev->needed_headroom = GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001418 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001419 for (i = 0; i < priv->num_tx_queues; i++) {
1420 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1421 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1422 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1423 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1424 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001425
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001426 for (i = 0; i < priv->num_rx_queues; i++) {
1427 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1428 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1429 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Jan Ceuleers0977f812012-06-05 03:42:12 +00001432 /* always enable rx filer */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001433 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001434 /* Enable most messages by default */
1435 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +00001436 /* use pritority h/w tx queue scheduling for single queue devices */
1437 if (priv->num_tx_queues == 1)
1438 priv->prio_sched_en = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001439
Claudiu Manoil08511332014-02-24 12:13:45 +02001440 set_bit(GFAR_DOWN, &priv->state);
1441
Claudiu Manoila328ac92014-02-24 12:13:42 +02001442 gfar_hw_init(priv);
Trent Piephod3eab822008-10-02 11:12:24 +00001443
Fabio Estevamd4c642e2014-06-03 19:55:38 -03001444 /* Carrier starts down, phylib will bring it up */
1445 netif_carrier_off(dev);
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 err = register_netdev(dev);
1448
1449 if (err) {
Joe Perches59deab22011-06-14 08:57:47 +00001450 pr_err("%s: Cannot register net device, aborting\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto register_fail;
1452 }
1453
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001454 device_init_wakeup(&dev->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001455 priv->device_flags &
1456 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001457
Dai Harukic50a5d92008-12-17 16:51:32 -08001458 /* fill out IRQ number and name fields */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001459 for (i = 0; i < priv->num_grps; i++) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001460 struct gfar_priv_grp *grp = &priv->gfargrp[i];
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001461 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001462 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001463 dev->name, "_g", '0' + i, "_tx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001464 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001465 dev->name, "_g", '0' + i, "_rx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001466 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001467 dev->name, "_g", '0' + i, "_er");
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001468 } else
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001469 strcpy(gfar_irq(grp, TX)->name, dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001470 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001471
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001472 /* Initialize the filer table */
1473 gfar_init_filer_table(priv);
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /* Print out the device info */
Joe Perches59deab22011-06-14 08:57:47 +00001476 netdev_info(dev, "mac: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Jan Ceuleers0977f812012-06-05 03:42:12 +00001478 /* Even more device info helps when determining which kernel
1479 * provided which set of benchmarks.
1480 */
Joe Perches59deab22011-06-14 08:57:47 +00001481 netdev_info(dev, "Running with NAPI enabled\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001482 for (i = 0; i < priv->num_rx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001483 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1484 i, priv->rx_queue[i]->rx_ring_size);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001485 for (i = 0; i < priv->num_tx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001486 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1487 i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 return 0;
1490
1491register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001492 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001493 gfar_free_rx_queues(priv);
1494 gfar_free_tx_queues(priv);
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001495 of_node_put(priv->phy_node);
1496 of_node_put(priv->tbi_node);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001497 free_gfar_dev(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Grant Likely2dc11582010-08-06 09:25:50 -06001501static int gfar_remove(struct platform_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001503 struct gfar_private *priv = platform_get_drvdata(ofdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001505 of_node_put(priv->phy_node);
1506 of_node_put(priv->tbi_node);
Grant Likelyfe192a42009-04-25 12:53:12 +00001507
David S. Millerd9d8e042009-09-06 01:41:02 -07001508 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001509 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001510 gfar_free_rx_queues(priv);
1511 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001512 free_gfar_dev(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 return 0;
1515}
1516
Scott Woodd87eb122008-07-11 18:04:45 -05001517#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001518
1519static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001520{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001521 struct gfar_private *priv = dev_get_drvdata(dev);
1522 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001523 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001524 unsigned long flags;
1525 u32 tempval;
1526
1527 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001528 (priv->device_flags &
1529 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001530
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001531 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001532
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001533 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001534
1535 local_irq_save(flags);
1536 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001537
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001538 gfar_halt_nodisable(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001539
1540 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001541 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001542
1543 tempval &= ~MACCFG1_TX_EN;
1544
1545 if (!magic_packet)
1546 tempval &= ~MACCFG1_RX_EN;
1547
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001548 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001549
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001550 unlock_tx_qs(priv);
1551 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001552
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001553 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001554
1555 if (magic_packet) {
1556 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001557 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001558
1559 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001560 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001561 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001562 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001563 } else {
1564 phy_stop(priv->phydev);
1565 }
1566 }
1567
1568 return 0;
1569}
1570
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001571static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001572{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001573 struct gfar_private *priv = dev_get_drvdata(dev);
1574 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001575 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001576 unsigned long flags;
1577 u32 tempval;
1578 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001579 (priv->device_flags &
1580 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001581
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001582 if (!netif_running(ndev)) {
1583 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001584 return 0;
1585 }
1586
1587 if (!magic_packet && priv->phydev)
1588 phy_start(priv->phydev);
1589
1590 /* Disable Magic Packet mode, in case something
1591 * else woke us up.
1592 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001593 local_irq_save(flags);
1594 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001595
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001596 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001597 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001598 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001599
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001600 gfar_start(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001601
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001602 unlock_tx_qs(priv);
1603 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001604
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001605 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001606
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001607 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001608
1609 return 0;
1610}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001611
1612static int gfar_restore(struct device *dev)
1613{
1614 struct gfar_private *priv = dev_get_drvdata(dev);
1615 struct net_device *ndev = priv->ndev;
1616
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001617 if (!netif_running(ndev)) {
1618 netif_device_attach(ndev);
1619
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001620 return 0;
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001621 }
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001622
Claudiu Manoil76f31e82015-07-13 16:22:03 +03001623 gfar_init_bds(ndev);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001624
Claudiu Manoila328ac92014-02-24 12:13:42 +02001625 gfar_mac_reset(priv);
1626
1627 gfar_init_tx_rx_base(priv);
1628
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001629 gfar_start(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001630
1631 priv->oldlink = 0;
1632 priv->oldspeed = 0;
1633 priv->oldduplex = -1;
1634
1635 if (priv->phydev)
1636 phy_start(priv->phydev);
1637
1638 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001639 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001640
1641 return 0;
1642}
1643
1644static struct dev_pm_ops gfar_pm_ops = {
1645 .suspend = gfar_suspend,
1646 .resume = gfar_resume,
1647 .freeze = gfar_suspend,
1648 .thaw = gfar_resume,
1649 .restore = gfar_restore,
1650};
1651
1652#define GFAR_PM_OPS (&gfar_pm_ops)
1653
Scott Woodd87eb122008-07-11 18:04:45 -05001654#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001655
1656#define GFAR_PM_OPS NULL
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001657
Scott Woodd87eb122008-07-11 18:04:45 -05001658#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001660/* Reads the controller's registers to determine what interface
1661 * connects it to the PHY.
1662 */
1663static phy_interface_t gfar_get_interface(struct net_device *dev)
1664{
1665 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001666 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001667 u32 ecntrl;
1668
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001669 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001670
1671 if (ecntrl & ECNTRL_SGMII_MODE)
1672 return PHY_INTERFACE_MODE_SGMII;
1673
1674 if (ecntrl & ECNTRL_TBI_MODE) {
1675 if (ecntrl & ECNTRL_REDUCED_MODE)
1676 return PHY_INTERFACE_MODE_RTBI;
1677 else
1678 return PHY_INTERFACE_MODE_TBI;
1679 }
1680
1681 if (ecntrl & ECNTRL_REDUCED_MODE) {
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001682 if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001683 return PHY_INTERFACE_MODE_RMII;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001684 }
Andy Fleming7132ab72007-07-11 11:43:07 -05001685 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001686 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001687
Jan Ceuleers0977f812012-06-05 03:42:12 +00001688 /* This isn't autodetected right now, so it must
Andy Fleming7132ab72007-07-11 11:43:07 -05001689 * be set by the device tree or platform code.
1690 */
1691 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1692 return PHY_INTERFACE_MODE_RGMII_ID;
1693
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001694 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001695 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001696 }
1697
Andy Flemingb31a1d82008-12-16 15:29:15 -08001698 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001699 return PHY_INTERFACE_MODE_GMII;
1700
1701 return PHY_INTERFACE_MODE_MII;
1702}
1703
1704
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001705/* Initializes driver's PHY state, and attaches to the PHY.
1706 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
1708static int init_phy(struct net_device *dev)
1709{
1710 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001711 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001712 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001713 GFAR_SUPPORTED_GBIT : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001714 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 priv->oldlink = 0;
1717 priv->oldspeed = 0;
1718 priv->oldduplex = -1;
1719
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001720 interface = gfar_get_interface(dev);
1721
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001722 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1723 interface);
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001724 if (!priv->phydev) {
1725 dev_err(&dev->dev, "could not attach to PHY\n");
1726 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Kapil Junejad3c12872007-05-11 18:25:11 -05001729 if (interface == PHY_INTERFACE_MODE_SGMII)
1730 gfar_configure_serdes(dev);
1731
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001732 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001733 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1734 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Pavaluca Matei-B46610cf987af2014-10-27 10:42:42 +02001736 /* Add support for flow control, but don't advertise it by default */
1737 priv->phydev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
Jan Ceuleers0977f812012-06-05 03:42:12 +00001742/* Initialize TBI PHY interface for communicating with the
Paul Gortmakerd0313582008-04-17 00:08:10 -04001743 * SERDES lynx PHY on the chip. We communicate with this PHY
1744 * through the MDIO bus on each controller, treating it as a
1745 * "normal" PHY at the address found in the TBIPA register. We assume
1746 * that the TBIPA register is valid. Either the MDIO bus code will set
1747 * it to a value that doesn't conflict with other PHYs on the bus, or the
1748 * value doesn't matter, as there are no other PHYs on the bus.
1749 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001750static void gfar_configure_serdes(struct net_device *dev)
1751{
1752 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001753 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001754
Grant Likelyfe192a42009-04-25 12:53:12 +00001755 if (!priv->tbi_node) {
1756 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1757 "device tree specify a tbi-handle\n");
1758 return;
1759 }
1760
1761 tbiphy = of_phy_find_device(priv->tbi_node);
1762 if (!tbiphy) {
1763 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001764 return;
1765 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001766
Jan Ceuleers0977f812012-06-05 03:42:12 +00001767 /* If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001768 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1769 * everything for us? Resetting it takes the link down and requires
1770 * several seconds for it to come back.
1771 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001772 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001773 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001774
Paul Gortmakerd0313582008-04-17 00:08:10 -04001775 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001776 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001777
Grant Likelyfe192a42009-04-25 12:53:12 +00001778 phy_write(tbiphy, MII_ADVERTISE,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001779 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1780 ADVERTISE_1000XPSE_ASYM);
Kapil Junejad3c12872007-05-11 18:25:11 -05001781
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001782 phy_write(tbiphy, MII_BMCR,
1783 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1784 BMCR_SPEED1000);
Kapil Junejad3c12872007-05-11 18:25:11 -05001785}
1786
Anton Vorontsov511d9342010-06-30 06:39:15 +00001787static int __gfar_is_rx_idle(struct gfar_private *priv)
1788{
1789 u32 res;
1790
Jan Ceuleers0977f812012-06-05 03:42:12 +00001791 /* Normaly TSEC should not hang on GRS commands, so we should
Anton Vorontsov511d9342010-06-30 06:39:15 +00001792 * actually wait for IEVENT_GRSC flag.
1793 */
Claudiu Manoilad3660c2013-10-09 20:20:40 +03001794 if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
Anton Vorontsov511d9342010-06-30 06:39:15 +00001795 return 0;
1796
Jan Ceuleers0977f812012-06-05 03:42:12 +00001797 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
Anton Vorontsov511d9342010-06-30 06:39:15 +00001798 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1799 * and the Rx can be safely reset.
1800 */
1801 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1802 res &= 0x7f807f80;
1803 if ((res & 0xffff) == (res >> 16))
1804 return 1;
1805
1806 return 0;
1807}
Kumar Gala0bbaf062005-06-20 10:54:21 -05001808
1809/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001810static void gfar_halt_nodisable(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001812 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 u32 tempval;
Claudiu Manoila4feee82014-10-07 10:44:34 +03001814 unsigned int timeout;
1815 int stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001817 gfar_ints_disable(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Claudiu Manoila4feee82014-10-07 10:44:34 +03001819 if (gfar_is_dma_stopped(priv))
1820 return;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001823 tempval = gfar_read(&regs->dmactrl);
Claudiu Manoila4feee82014-10-07 10:44:34 +03001824 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1825 gfar_write(&regs->dmactrl, tempval);
Anton Vorontsov511d9342010-06-30 06:39:15 +00001826
Claudiu Manoila4feee82014-10-07 10:44:34 +03001827retry:
1828 timeout = 1000;
1829 while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
1830 cpu_relax();
1831 timeout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
Claudiu Manoila4feee82014-10-07 10:44:34 +03001833
1834 if (!timeout)
1835 stopped = gfar_is_dma_stopped(priv);
1836
1837 if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
1838 !__gfar_is_rx_idle(priv))
1839 goto retry;
Scott Woodd87eb122008-07-11 18:04:45 -05001840}
Scott Woodd87eb122008-07-11 18:04:45 -05001841
1842/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001843void gfar_halt(struct gfar_private *priv)
Scott Woodd87eb122008-07-11 18:04:45 -05001844{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001845 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001846 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001848 /* Dissable the Rx/Tx hw queues */
1849 gfar_write(&regs->rqueue, 0);
1850 gfar_write(&regs->tqueue, 0);
Scott Wood2a54adc2008-08-12 15:10:46 -05001851
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001852 mdelay(10);
1853
1854 gfar_halt_nodisable(priv);
1855
1856 /* Disable Rx/Tx DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 tempval = gfar_read(&regs->maccfg1);
1858 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1859 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001860}
1861
1862void stop_gfar(struct net_device *dev)
1863{
1864 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001865
Claudiu Manoil08511332014-02-24 12:13:45 +02001866 netif_tx_stop_all_queues(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001867
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001868 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02001869 set_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001870 smp_mb__after_atomic();
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001871
Claudiu Manoil08511332014-02-24 12:13:45 +02001872 disable_napi(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001873
Claudiu Manoil08511332014-02-24 12:13:45 +02001874 /* disable ints and gracefully shut down Rx/Tx DMA */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001875 gfar_halt(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Claudiu Manoil08511332014-02-24 12:13:45 +02001877 phy_stop(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001882static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001885 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001886 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001888 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001890 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1891 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001892 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Claudiu Manoila7312d52015-03-13 10:36:28 +02001894 dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr),
1895 be16_to_cpu(txbdp->length), DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08001896 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001897 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001898 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001899 txbdp++;
Claudiu Manoila7312d52015-03-13 10:36:28 +02001900 dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr),
1901 be16_to_cpu(txbdp->length),
1902 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001904 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001905 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1906 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001908 kfree(tx_queue->tx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001909 tx_queue->tx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001910}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001912static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1913{
1914 struct rxbd8 *rxbdp;
Claudiu Manoilf23223f2015-07-13 16:22:05 +03001915 struct gfar_private *priv = netdev_priv(rx_queue->ndev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001916 int i;
1917
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001918 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001920 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1921 if (rx_queue->rx_skbuff[i]) {
Claudiu Manoila7312d52015-03-13 10:36:28 +02001922 dma_unmap_single(priv->dev, be32_to_cpu(rxbdp->bufPtr),
Claudiu Manoil369ec162013-02-14 05:00:02 +00001923 priv->rx_buffer_size,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001924 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001925 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1926 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001928 rxbdp->lstatus = 0;
1929 rxbdp->bufPtr = 0;
1930 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001932 kfree(rx_queue->rx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001933 rx_queue->rx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001934}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001935
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001936/* If there are any tx skbs or rx skbs still around, free them.
Jan Ceuleers0977f812012-06-05 03:42:12 +00001937 * Then free tx_skbuff and rx_skbuff
1938 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001939static void free_skb_resources(struct gfar_private *priv)
1940{
1941 struct gfar_priv_tx_q *tx_queue = NULL;
1942 struct gfar_priv_rx_q *rx_queue = NULL;
1943 int i;
1944
1945 /* Go through all the buffer descriptors and free their data buffers */
1946 for (i = 0; i < priv->num_tx_queues; i++) {
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001947 struct netdev_queue *txq;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001948
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001949 tx_queue = priv->tx_queue[i];
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001950 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001951 if (tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001952 free_skb_tx_queue(tx_queue);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001953 netdev_tx_reset_queue(txq);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001954 }
1955
1956 for (i = 0; i < priv->num_rx_queues; i++) {
1957 rx_queue = priv->rx_queue[i];
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001958 if (rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001959 free_skb_rx_queue(rx_queue);
1960 }
1961
Claudiu Manoil369ec162013-02-14 05:00:02 +00001962 dma_free_coherent(priv->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001963 sizeof(struct txbd8) * priv->total_tx_ring_size +
1964 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1965 priv->tx_queue[0]->tx_bd_base,
1966 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001969void gfar_start(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001970{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001971 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001972 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001973 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001974
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001975 /* Enable Rx/Tx hw queues */
1976 gfar_write(&regs->rqueue, priv->rqueue);
1977 gfar_write(&regs->tqueue, priv->tqueue);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001978
1979 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001980 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001981 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001982 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001983
Kumar Gala0bbaf062005-06-20 10:54:21 -05001984 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001985 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001986 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001987 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001988
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001989 for (i = 0; i < priv->num_grps; i++) {
1990 regs = priv->gfargrp[i].regs;
1991 /* Clear THLT/RHLT, so that the DMA starts polling now */
1992 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1993 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001994 }
Dai Haruki12dea572008-12-16 15:30:20 -08001995
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001996 /* Enable Rx/Tx DMA */
1997 tempval = gfar_read(&regs->maccfg1);
1998 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1999 gfar_write(&regs->maccfg1, tempval);
2000
Claudiu Manoilefeddce2014-02-17 12:53:17 +02002001 gfar_ints_enable(priv);
2002
Claudiu Manoilc10650b2014-02-17 12:53:18 +02002003 priv->ndev->trans_start = jiffies; /* prevent tx timeout */
Kumar Gala0bbaf062005-06-20 10:54:21 -05002004}
2005
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002006static void free_grp_irqs(struct gfar_priv_grp *grp)
2007{
2008 free_irq(gfar_irq(grp, TX)->irq, grp);
2009 free_irq(gfar_irq(grp, RX)->irq, grp);
2010 free_irq(gfar_irq(grp, ER)->irq, grp);
2011}
2012
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002013static int register_grp_irqs(struct gfar_priv_grp *grp)
2014{
2015 struct gfar_private *priv = grp->priv;
2016 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00002017 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /* If the device has multiple interrupts, register for
Jan Ceuleers0977f812012-06-05 03:42:12 +00002020 * them. Otherwise, only register for the one
2021 */
Andy Flemingb31a1d82008-12-16 15:29:15 -08002022 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05002023 /* Install our interrupt handlers for Error,
Jan Ceuleers0977f812012-06-05 03:42:12 +00002024 * Transmit, and Receive
2025 */
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002026 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
2027 gfar_irq(grp, ER)->name, grp);
2028 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002029 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002030 gfar_irq(grp, ER)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002031
Julia Lawall2145f1a2010-08-05 10:26:20 +00002032 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002034 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
2035 gfar_irq(grp, TX)->name, grp);
2036 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002037 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002038 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto tx_irq_fail;
2040 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002041 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
2042 gfar_irq(grp, RX)->name, grp);
2043 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002044 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002045 gfar_irq(grp, RX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 goto rx_irq_fail;
2047 }
2048 } else {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002049 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
2050 gfar_irq(grp, TX)->name, grp);
2051 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002052 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002053 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto err_irq_fail;
2055 }
2056 }
2057
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002058 return 0;
2059
2060rx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002061 free_irq(gfar_irq(grp, TX)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002062tx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002063 free_irq(gfar_irq(grp, ER)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002064err_irq_fail:
2065 return err;
2066
2067}
2068
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002069static void gfar_free_irq(struct gfar_private *priv)
2070{
2071 int i;
2072
2073 /* Free the IRQs */
2074 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2075 for (i = 0; i < priv->num_grps; i++)
2076 free_grp_irqs(&priv->gfargrp[i]);
2077 } else {
2078 for (i = 0; i < priv->num_grps; i++)
2079 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
2080 &priv->gfargrp[i]);
2081 }
2082}
2083
2084static int gfar_request_irq(struct gfar_private *priv)
2085{
2086 int err, i, j;
2087
2088 for (i = 0; i < priv->num_grps; i++) {
2089 err = register_grp_irqs(&priv->gfargrp[i]);
2090 if (err) {
2091 for (j = 0; j < i; j++)
2092 free_grp_irqs(&priv->gfargrp[j]);
2093 return err;
2094 }
2095 }
2096
2097 return 0;
2098}
2099
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002100/* Bring the controller up and running */
2101int startup_gfar(struct net_device *ndev)
2102{
2103 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002104 int err;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002105
Claudiu Manoila328ac92014-02-24 12:13:42 +02002106 gfar_mac_reset(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002107
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002108 err = gfar_alloc_skb_resources(ndev);
2109 if (err)
2110 return err;
2111
Claudiu Manoila328ac92014-02-24 12:13:42 +02002112 gfar_init_tx_rx_base(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002113
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002114 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002115 clear_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002116 smp_mb__after_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002117
2118 /* Start Rx/Tx DMA and enable the interrupts */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02002119 gfar_start(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00002121 phy_start(priv->phydev);
2122
Claudiu Manoil08511332014-02-24 12:13:45 +02002123 enable_napi(priv);
2124
2125 netif_tx_wake_all_queues(ndev);
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Jan Ceuleers0977f812012-06-05 03:42:12 +00002130/* Called when something needs to use the ethernet device
2131 * Returns 0 for success.
2132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133static int gfar_enet_open(struct net_device *dev)
2134{
Li Yang94e8cc32007-10-12 21:53:51 +08002135 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 int err;
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 err = init_phy(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002139 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return err;
2141
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002142 err = gfar_request_irq(priv);
2143 if (err)
2144 return err;
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 err = startup_gfar(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002147 if (err)
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04002148 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08002150 device_set_wakeup_enable(&dev->dev, priv->wol_en);
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return err;
2153}
2154
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002155static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002156{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002157 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07002158
2159 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002160
Kumar Gala0bbaf062005-06-20 10:54:21 -05002161 return fcb;
2162}
2163
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002164static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002165 int fcb_length)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002166{
Kumar Gala0bbaf062005-06-20 10:54:21 -05002167 /* If we're here, it's a IP packet with a TCP or UDP
2168 * payload. We set it to checksum, using a pseudo-header
2169 * we provide
2170 */
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00002171 u8 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002172
Jan Ceuleers0977f812012-06-05 03:42:12 +00002173 /* Tell the controller what the protocol is
2174 * And provide the already calculated phcs
2175 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002176 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002177 flags |= TXFCB_UDP;
Claudiu Manoil26eb9372015-03-13 10:36:29 +02002178 fcb->phcs = (__force __be16)(udp_hdr(skb)->check);
Andy Fleming7f7f5312005-11-11 12:38:59 -06002179 } else
Claudiu Manoil26eb9372015-03-13 10:36:29 +02002180 fcb->phcs = (__force __be16)(tcp_hdr(skb)->check);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002181
2182 /* l3os is the distance between the start of the
2183 * frame (skb->data) and the start of the IP hdr.
2184 * l4os is the distance between the start of the
Jan Ceuleers0977f812012-06-05 03:42:12 +00002185 * l3 hdr and the l4 hdr
2186 */
Claudiu Manoil26eb9372015-03-13 10:36:29 +02002187 fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03002188 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002189
Andy Fleming7f7f5312005-11-11 12:38:59 -06002190 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002191}
2192
Andy Fleming7f7f5312005-11-11 12:38:59 -06002193void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002194{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002195 fcb->flags |= TXFCB_VLN;
Claudiu Manoil26eb9372015-03-13 10:36:29 +02002196 fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb));
Kumar Gala0bbaf062005-06-20 10:54:21 -05002197}
2198
Dai Haruki4669bc92008-12-17 16:51:04 -08002199static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002200 struct txbd8 *base, int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002201{
2202 struct txbd8 *new_bd = bdp + stride;
2203
2204 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2205}
2206
2207static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002208 int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002209{
2210 return skip_txbd(bdp, 1, base, ring_size);
2211}
2212
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002213/* eTSEC12: csum generation not supported for some fcb offsets */
2214static inline bool gfar_csum_errata_12(struct gfar_private *priv,
2215 unsigned long fcb_addr)
2216{
2217 return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
2218 (fcb_addr % 0x20) > 0x18);
2219}
2220
2221/* eTSEC76: csum generation for frames larger than 2500 may
2222 * cause excess delays before start of transmission
2223 */
2224static inline bool gfar_csum_errata_76(struct gfar_private *priv,
2225 unsigned int len)
2226{
2227 return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
2228 (len > 2500));
2229}
2230
Jan Ceuleers0977f812012-06-05 03:42:12 +00002231/* This is called by the kernel when a frame is ready for transmission.
2232 * It is pointed to by the dev->hard_start_xmit function pointer
2233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2235{
2236 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002237 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002238 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002239 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002240 struct txfcb *fcb = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002241 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
Dai Haruki5a5efed2008-12-16 15:34:50 -08002242 u32 lstatus;
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002243 int i, rq = 0;
2244 int do_tstamp, do_csum, do_vlan;
Dai Haruki4669bc92008-12-17 16:51:04 -08002245 u32 bufaddr;
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002246 unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002247
2248 rq = skb->queue_mapping;
2249 tx_queue = priv->tx_queue[rq];
2250 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002251 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002252 regs = tx_queue->grp->regs;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002253
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002254 do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002255 do_vlan = skb_vlan_tag_present(skb);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002256 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2257 priv->hwts_tx_en;
2258
2259 if (do_csum || do_vlan)
2260 fcb_len = GMAC_FCB_LEN;
2261
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002262 /* check if time stamp should be generated */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002263 if (unlikely(do_tstamp))
2264 fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Dai Haruki4669bc92008-12-17 16:51:04 -08002265
Li Yang5b28bea2009-03-27 15:54:30 -07002266 /* make space for additional header when fcb is needed */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002267 if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002268 struct sk_buff *skb_new;
2269
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002270 skb_new = skb_realloc_headroom(skb, fcb_len);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002271 if (!skb_new) {
2272 dev->stats.tx_errors++;
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002273 dev_kfree_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002274 return NETDEV_TX_OK;
2275 }
Manfred Rudigierdb83d132012-01-09 23:26:50 +00002276
Eric Dumazet313b0372012-07-05 11:45:13 +00002277 if (skb->sk)
2278 skb_set_owner_w(skb_new, skb->sk);
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002279 dev_consume_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002280 skb = skb_new;
2281 }
2282
Dai Haruki4669bc92008-12-17 16:51:04 -08002283 /* total number of fragments in the SKB */
2284 nr_frags = skb_shinfo(skb)->nr_frags;
2285
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002286 /* calculate the required number of TxBDs for this skb */
2287 if (unlikely(do_tstamp))
2288 nr_txbds = nr_frags + 2;
2289 else
2290 nr_txbds = nr_frags + 1;
2291
Dai Haruki4669bc92008-12-17 16:51:04 -08002292 /* check if there is space to queue this packet */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002293 if (nr_txbds > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002294 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002295 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08002296 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08002297 return NETDEV_TX_BUSY;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 /* Update transmit stats */
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002301 bytes_sent = skb->len;
2302 tx_queue->stats.tx_bytes += bytes_sent;
2303 /* keep Tx bytes on wire for BQL accounting */
2304 GFAR_CB(skb)->bytes_sent = bytes_sent;
Eric Dumazet1ac9ad12011-01-12 12:13:14 +00002305 tx_queue->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002307 txbdp = txbdp_start = tx_queue->cur_tx;
Claudiu Manoila7312d52015-03-13 10:36:28 +02002308 lstatus = be32_to_cpu(txbdp->lstatus);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002309
2310 /* Time stamp insertion requires one additional TxBD */
2311 if (unlikely(do_tstamp))
2312 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002313 tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Dai Haruki4669bc92008-12-17 16:51:04 -08002315 if (nr_frags == 0) {
Claudiu Manoila7312d52015-03-13 10:36:28 +02002316 if (unlikely(do_tstamp)) {
2317 u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
2318
2319 lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2320 txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
2321 } else {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002322 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
Claudiu Manoila7312d52015-03-13 10:36:28 +02002323 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002324 } else {
2325 /* Place the fragment addresses and lengths into the TxBDs */
2326 for (i = 0; i < nr_frags; i++) {
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002327 unsigned int frag_len;
Dai Haruki4669bc92008-12-17 16:51:04 -08002328 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002329 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002331 frag_len = skb_shinfo(skb)->frags[i].size;
Dai Haruki4669bc92008-12-17 16:51:04 -08002332
Claudiu Manoila7312d52015-03-13 10:36:28 +02002333 lstatus = be32_to_cpu(txbdp->lstatus) | frag_len |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002334 BD_LFLAG(TXBD_READY);
Dai Haruki4669bc92008-12-17 16:51:04 -08002335
2336 /* Handle the last BD specially */
2337 if (i == nr_frags - 1)
2338 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2339
Claudiu Manoil369ec162013-02-14 05:00:02 +00002340 bufaddr = skb_frag_dma_map(priv->dev,
Ian Campbell2234a722011-08-29 23:18:29 +00002341 &skb_shinfo(skb)->frags[i],
2342 0,
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002343 frag_len,
Ian Campbell2234a722011-08-29 23:18:29 +00002344 DMA_TO_DEVICE);
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002345 if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
2346 goto dma_map_err;
Dai Haruki4669bc92008-12-17 16:51:04 -08002347
2348 /* set the TxBD length and buffer pointer */
Claudiu Manoila7312d52015-03-13 10:36:28 +02002349 txbdp->bufPtr = cpu_to_be32(bufaddr);
2350 txbdp->lstatus = cpu_to_be32(lstatus);
Dai Haruki4669bc92008-12-17 16:51:04 -08002351 }
2352
Claudiu Manoila7312d52015-03-13 10:36:28 +02002353 lstatus = be32_to_cpu(txbdp_start->lstatus);
Dai Haruki4669bc92008-12-17 16:51:04 -08002354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002356 /* Add TxPAL between FCB and frame if required */
2357 if (unlikely(do_tstamp)) {
2358 skb_push(skb, GMAC_TXPAL_LEN);
2359 memset(skb->data, 0, GMAC_TXPAL_LEN);
2360 }
2361
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002362 /* Add TxFCB if required */
2363 if (fcb_len) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002364 fcb = gfar_add_fcb(skb);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002365 lstatus |= BD_LFLAG(TXBD_TOE);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002366 }
2367
2368 /* Set up checksumming */
2369 if (do_csum) {
2370 gfar_tx_checksum(skb, fcb, fcb_len);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002371
2372 if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
2373 unlikely(gfar_csum_errata_76(priv, skb->len))) {
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00002374 __skb_pull(skb, GMAC_FCB_LEN);
2375 skb_checksum_help(skb);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002376 if (do_vlan || do_tstamp) {
2377 /* put back a new fcb for vlan/tstamp TOE */
2378 fcb = gfar_add_fcb(skb);
2379 } else {
2380 /* Tx TOE not used */
2381 lstatus &= ~(BD_LFLAG(TXBD_TOE));
2382 fcb = NULL;
2383 }
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00002384 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002385 }
2386
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002387 if (do_vlan)
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002388 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002389
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002390 /* Setup tx hardware time stamping if requested */
2391 if (unlikely(do_tstamp)) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002392 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002393 fcb->ptp = 1;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002394 }
2395
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002396 bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
2397 DMA_TO_DEVICE);
2398 if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
2399 goto dma_map_err;
2400
Claudiu Manoila7312d52015-03-13 10:36:28 +02002401 txbdp_start->bufPtr = cpu_to_be32(bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Jan Ceuleers0977f812012-06-05 03:42:12 +00002403 /* If time stamping is requested one additional TxBD must be set up. The
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002404 * first TxBD points to the FCB and must have a data length of
2405 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2406 * the full frame length.
2407 */
2408 if (unlikely(do_tstamp)) {
Claudiu Manoila7312d52015-03-13 10:36:28 +02002409 u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
2410
2411 bufaddr = be32_to_cpu(txbdp_start->bufPtr);
2412 bufaddr += fcb_len;
2413 lstatus_ts |= BD_LFLAG(TXBD_READY) |
2414 (skb_headlen(skb) - fcb_len);
2415
2416 txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr);
2417 txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002418 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2419 } else {
2420 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002423 netdev_tx_sent_queue(txq, bytes_sent);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002424
Claudiu Manoild55398b2014-10-07 10:44:35 +03002425 gfar_wmb();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002426
Claudiu Manoila7312d52015-03-13 10:36:28 +02002427 txbdp_start->lstatus = cpu_to_be32(lstatus);
Dai Haruki4669bc92008-12-17 16:51:04 -08002428
Claudiu Manoild55398b2014-10-07 10:44:35 +03002429 gfar_wmb(); /* force lstatus write before tx_skbuff */
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002430
2431 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2432
Dai Haruki4669bc92008-12-17 16:51:04 -08002433 /* Update the current skb pointer to the next entry we will use
Jan Ceuleers0977f812012-06-05 03:42:12 +00002434 * (wrapping if necessary)
2435 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002436 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002437 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002438
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002439 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002440
Claudiu Manoilbc602282015-05-06 18:07:29 +03002441 /* We can work in parallel with gfar_clean_tx_ring(), except
2442 * when modifying num_txbdfree. Note that we didn't grab the lock
2443 * when we were reading the num_txbdfree and checking for available
2444 * space, that's because outside of this function it can only grow.
2445 */
2446 spin_lock_bh(&tx_queue->txlock);
Dai Haruki4669bc92008-12-17 16:51:04 -08002447 /* reduce TxBD free count */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002448 tx_queue->num_txbdfree -= (nr_txbds);
Claudiu Manoilbc602282015-05-06 18:07:29 +03002449 spin_unlock_bh(&tx_queue->txlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /* If the next BD still needs to be cleaned up, then the bds
Jan Ceuleers0977f812012-06-05 03:42:12 +00002452 * are full. We need to tell the kernel to stop sending us stuff.
2453 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002454 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002455 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002457 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 }
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002461 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002463 return NETDEV_TX_OK;
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002464
2465dma_map_err:
2466 txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size);
2467 if (do_tstamp)
2468 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2469 for (i = 0; i < nr_frags; i++) {
Claudiu Manoila7312d52015-03-13 10:36:28 +02002470 lstatus = be32_to_cpu(txbdp->lstatus);
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002471 if (!(lstatus & BD_LFLAG(TXBD_READY)))
2472 break;
2473
Claudiu Manoila7312d52015-03-13 10:36:28 +02002474 lstatus &= ~BD_LFLAG(TXBD_READY);
2475 txbdp->lstatus = cpu_to_be32(lstatus);
2476 bufaddr = be32_to_cpu(txbdp->bufPtr);
2477 dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length),
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002478 DMA_TO_DEVICE);
2479 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2480 }
2481 gfar_wmb();
2482 dev_kfree_skb_any(skb);
2483 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
2486/* Stops the kernel queue, and halts the controller */
2487static int gfar_close(struct net_device *dev)
2488{
2489 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002490
Sebastian Siewiorab939902008-08-19 21:12:45 +02002491 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 stop_gfar(dev);
2493
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002494 /* Disconnect from the PHY */
2495 phy_disconnect(priv->phydev);
2496 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002498 gfar_free_irq(priv);
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501}
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002504static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002506 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 return 0;
2509}
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002514 int frame_size = new_mtu + ETH_HLEN;
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Joe Perches59deab22011-06-14 08:57:47 +00002517 netif_err(priv, drv, dev, "Invalid MTU setting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return -EINVAL;
2519 }
2520
Claudiu Manoil08511332014-02-24 12:13:45 +02002521 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2522 cpu_relax();
2523
Claudiu Manoil88302642014-02-24 12:13:43 +02002524 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 stop_gfar(dev);
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 dev->mtu = new_mtu;
2528
Claudiu Manoil88302642014-02-24 12:13:43 +02002529 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 startup_gfar(dev);
2531
Claudiu Manoil08511332014-02-24 12:13:45 +02002532 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return 0;
2535}
2536
Claudiu Manoil08511332014-02-24 12:13:45 +02002537void reset_gfar(struct net_device *ndev)
2538{
2539 struct gfar_private *priv = netdev_priv(ndev);
2540
2541 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2542 cpu_relax();
2543
2544 stop_gfar(ndev);
2545 startup_gfar(ndev);
2546
2547 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2548}
2549
Sebastian Siewiorab939902008-08-19 21:12:45 +02002550/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 * transmitted after a set amount of time.
2552 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002553 * starting over will fix the problem.
2554 */
2555static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002557 struct gfar_private *priv = container_of(work, struct gfar_private,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002558 reset_task);
Claudiu Manoil08511332014-02-24 12:13:45 +02002559 reset_gfar(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Sebastian Siewiorab939902008-08-19 21:12:45 +02002562static void gfar_timeout(struct net_device *dev)
2563{
2564 struct gfar_private *priv = netdev_priv(dev);
2565
2566 dev->stats.tx_errors++;
2567 schedule_work(&priv->reset_task);
2568}
2569
Eran Libertyacbc0f02010-07-07 15:54:54 -07002570static void gfar_align_skb(struct sk_buff *skb)
2571{
2572 /* We need the data buffer to be aligned properly. We will reserve
2573 * as many bytes as needed to align the data properly
2574 */
2575 skb_reserve(skb, RXBUF_ALIGNMENT -
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002576 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
Eran Libertyacbc0f02010-07-07 15:54:54 -07002577}
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579/* Interrupt Handler for Transmit complete */
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002580static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002582 struct net_device *dev = tx_queue->dev;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002583 struct netdev_queue *txq;
Dai Harukid080cd62008-04-09 19:37:51 -05002584 struct gfar_private *priv = netdev_priv(dev);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002585 struct txbd8 *bdp, *next = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002586 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002587 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002588 struct sk_buff *skb;
2589 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002590 int tx_ring_size = tx_queue->tx_ring_size;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002591 int frags = 0, nr_txbds = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002592 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002593 int howmany = 0;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002594 int tqi = tx_queue->qindex;
2595 unsigned int bytes_sent = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002596 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002597 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002599 txq = netdev_get_tx_queue(dev, tqi);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002600 bdp = tx_queue->dirty_tx;
2601 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002602
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002603 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002604
Dai Haruki4669bc92008-12-17 16:51:04 -08002605 frags = skb_shinfo(skb)->nr_frags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002606
Jan Ceuleers0977f812012-06-05 03:42:12 +00002607 /* When time stamping, one additional TxBD must be freed.
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002608 * Also, we need to dma_unmap_single() the TxPAL.
2609 */
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002610 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002611 nr_txbds = frags + 2;
2612 else
2613 nr_txbds = frags + 1;
2614
2615 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002616
Claudiu Manoila7312d52015-03-13 10:36:28 +02002617 lstatus = be32_to_cpu(lbdp->lstatus);
Dai Haruki4669bc92008-12-17 16:51:04 -08002618
2619 /* Only clean completed frames */
2620 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002621 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 break;
2623
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002624 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002625 next = next_txbd(bdp, base, tx_ring_size);
Claudiu Manoila7312d52015-03-13 10:36:28 +02002626 buflen = be16_to_cpu(next->length) +
2627 GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002628 } else
Claudiu Manoila7312d52015-03-13 10:36:28 +02002629 buflen = be16_to_cpu(bdp->length);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002630
Claudiu Manoila7312d52015-03-13 10:36:28 +02002631 dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr),
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002632 buflen, DMA_TO_DEVICE);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002633
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002634 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002635 struct skb_shared_hwtstamps shhwtstamps;
2636 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002637
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002638 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2639 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002640 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002641 skb_tstamp_tx(skb, &shhwtstamps);
Claudiu Manoila7312d52015-03-13 10:36:28 +02002642 gfar_clear_txbd_status(bdp);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002643 bdp = next;
2644 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002645
Claudiu Manoila7312d52015-03-13 10:36:28 +02002646 gfar_clear_txbd_status(bdp);
Dai Haruki4669bc92008-12-17 16:51:04 -08002647 bdp = next_txbd(bdp, base, tx_ring_size);
2648
2649 for (i = 0; i < frags; i++) {
Claudiu Manoila7312d52015-03-13 10:36:28 +02002650 dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr),
2651 be16_to_cpu(bdp->length),
2652 DMA_TO_DEVICE);
2653 gfar_clear_txbd_status(bdp);
Dai Haruki4669bc92008-12-17 16:51:04 -08002654 bdp = next_txbd(bdp, base, tx_ring_size);
2655 }
2656
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002657 bytes_sent += GFAR_CB(skb)->bytes_sent;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002658
Eric Dumazetacb600d2012-10-05 06:23:55 +00002659 dev_kfree_skb_any(skb);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002660
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002661 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002662
2663 skb_dirtytx = (skb_dirtytx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002664 TX_RING_MOD_MASK(tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002665
Dai Harukid080cd62008-04-09 19:37:51 -05002666 howmany++;
Claudiu Manoilbc602282015-05-06 18:07:29 +03002667 spin_lock(&tx_queue->txlock);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002668 tx_queue->num_txbdfree += nr_txbds;
Claudiu Manoilbc602282015-05-06 18:07:29 +03002669 spin_unlock(&tx_queue->txlock);
Dai Haruki4669bc92008-12-17 16:51:04 -08002670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Dai Haruki4669bc92008-12-17 16:51:04 -08002672 /* If we freed a buffer, we can restart transmission, if necessary */
Claudiu Manoil08511332014-02-24 12:13:45 +02002673 if (tx_queue->num_txbdfree &&
2674 netif_tx_queue_stopped(txq) &&
2675 !(test_bit(GFAR_DOWN, &priv->state)))
2676 netif_wake_subqueue(priv->ndev, tqi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Dai Haruki4669bc92008-12-17 16:51:04 -08002678 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002679 tx_queue->skb_dirtytx = skb_dirtytx;
2680 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002682 netdev_tx_completed_queue(txq, howmany, bytes_sent);
Dai Harukid080cd62008-04-09 19:37:51 -05002683}
2684
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002685static struct sk_buff *gfar_new_skb(struct net_device *ndev,
2686 dma_addr_t *bufaddr)
Eran Libertyacbc0f02010-07-07 15:54:54 -07002687{
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002688 struct gfar_private *priv = netdev_priv(ndev);
Eric Dumazetacb600d2012-10-05 06:23:55 +00002689 struct sk_buff *skb;
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002690 dma_addr_t addr;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002691
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002692 skb = netdev_alloc_skb(ndev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
Eran Libertyacbc0f02010-07-07 15:54:54 -07002693 if (!skb)
2694 return NULL;
2695
2696 gfar_align_skb(skb);
2697
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002698 addr = dma_map_single(priv->dev, skb->data,
2699 priv->rx_buffer_size, DMA_FROM_DEVICE);
2700 if (unlikely(dma_mapping_error(priv->dev, addr))) {
2701 dev_kfree_skb_any(skb);
2702 return NULL;
2703 }
2704
2705 *bufaddr = addr;
2706 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002709static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue)
2710{
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002711 struct gfar_private *priv = netdev_priv(rx_queue->ndev);
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002712 struct gfar_extra_stats *estats = &priv->extra_stats;
2713
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002714 netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n");
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002715 atomic64_inc(&estats->rx_alloc_err);
2716}
2717
2718static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
2719 int alloc_cnt)
2720{
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002721 struct net_device *ndev = rx_queue->ndev;
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002722 struct rxbd8 *bdp, *base;
2723 dma_addr_t bufaddr;
2724 int i;
2725
2726 i = rx_queue->next_to_use;
2727 base = rx_queue->rx_bd_base;
2728 bdp = &rx_queue->rx_bd_base[i];
2729
2730 while (alloc_cnt--) {
2731 struct sk_buff *skb = rx_queue->rx_skbuff[i];
2732
2733 if (likely(!skb)) {
2734 skb = gfar_new_skb(ndev, &bufaddr);
2735 if (unlikely(!skb)) {
2736 gfar_rx_alloc_err(rx_queue);
2737 break;
2738 }
2739 } else { /* restore from sleep state */
2740 bufaddr = be32_to_cpu(bdp->bufPtr);
2741 }
2742
2743 rx_queue->rx_skbuff[i] = skb;
2744
2745 /* Setup the new RxBD */
2746 gfar_init_rxbdp(rx_queue, bdp, bufaddr);
2747
2748 /* Update to the next pointer */
2749 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
2750
2751 if (unlikely(++i == rx_queue->rx_ring_size))
2752 i = 0;
2753 }
2754
2755 rx_queue->next_to_use = i;
2756}
2757
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002758static void count_errors(u32 lstatus, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002760 struct gfar_private *priv = netdev_priv(ndev);
2761 struct net_device_stats *stats = &ndev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 struct gfar_extra_stats *estats = &priv->extra_stats;
2763
Jan Ceuleers0977f812012-06-05 03:42:12 +00002764 /* If the packet was truncated, none of the other errors matter */
Claudiu Manoilf9660822015-07-13 16:22:04 +03002765 if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 stats->rx_length_errors++;
2767
Paul Gortmaker212079d2013-02-12 15:38:19 -05002768 atomic64_inc(&estats->rx_trunc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 return;
2771 }
2772 /* Count the errors, if there were any */
Claudiu Manoilf9660822015-07-13 16:22:04 +03002773 if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 stats->rx_length_errors++;
2775
Claudiu Manoilf9660822015-07-13 16:22:04 +03002776 if (lstatus & BD_LFLAG(RXBD_LARGE))
Paul Gortmaker212079d2013-02-12 15:38:19 -05002777 atomic64_inc(&estats->rx_large);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 else
Paul Gortmaker212079d2013-02-12 15:38:19 -05002779 atomic64_inc(&estats->rx_short);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Claudiu Manoilf9660822015-07-13 16:22:04 +03002781 if (lstatus & BD_LFLAG(RXBD_NONOCTET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 stats->rx_frame_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002783 atomic64_inc(&estats->rx_nonoctet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Claudiu Manoilf9660822015-07-13 16:22:04 +03002785 if (lstatus & BD_LFLAG(RXBD_CRCERR)) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002786 atomic64_inc(&estats->rx_crcerr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 stats->rx_crc_errors++;
2788 }
Claudiu Manoilf9660822015-07-13 16:22:04 +03002789 if (lstatus & BD_LFLAG(RXBD_OVERRUN)) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002790 atomic64_inc(&estats->rx_overrun);
Claudiu Manoilf9660822015-07-13 16:22:04 +03002791 stats->rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793}
2794
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002795irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002797 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2798 unsigned long flags;
2799 u32 imask;
2800
2801 if (likely(napi_schedule_prep(&grp->napi_rx))) {
2802 spin_lock_irqsave(&grp->grplock, flags);
2803 imask = gfar_read(&grp->regs->imask);
2804 imask &= IMASK_RX_DISABLED;
2805 gfar_write(&grp->regs->imask, imask);
2806 spin_unlock_irqrestore(&grp->grplock, flags);
2807 __napi_schedule(&grp->napi_rx);
2808 } else {
2809 /* Clear IEVENT, so interrupts aren't called again
2810 * because of the packets that have already arrived.
2811 */
2812 gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2813 }
2814
2815 return IRQ_HANDLED;
2816}
2817
2818/* Interrupt Handler for Transmit complete */
2819static irqreturn_t gfar_transmit(int irq, void *grp_id)
2820{
2821 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2822 unsigned long flags;
2823 u32 imask;
2824
2825 if (likely(napi_schedule_prep(&grp->napi_tx))) {
2826 spin_lock_irqsave(&grp->grplock, flags);
2827 imask = gfar_read(&grp->regs->imask);
2828 imask &= IMASK_TX_DISABLED;
2829 gfar_write(&grp->regs->imask, imask);
2830 spin_unlock_irqrestore(&grp->grplock, flags);
2831 __napi_schedule(&grp->napi_tx);
2832 } else {
2833 /* Clear IEVENT, so interrupts aren't called again
2834 * because of the packets that have already arrived.
2835 */
2836 gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2837 }
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return IRQ_HANDLED;
2840}
2841
Kumar Gala0bbaf062005-06-20 10:54:21 -05002842static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2843{
2844 /* If valid headers were found, and valid sums
2845 * were verified, then we tell the kernel that no
Jan Ceuleers0977f812012-06-05 03:42:12 +00002846 * checksumming is necessary. Otherwise, it is [FIXME]
2847 */
Claudiu Manoil26eb9372015-03-13 10:36:29 +02002848 if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) ==
2849 (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002850 skb->ip_summed = CHECKSUM_UNNECESSARY;
2851 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002852 skb_checksum_none_assert(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002853}
2854
Jan Ceuleers0977f812012-06-05 03:42:12 +00002855/* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002856static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002858 struct gfar_private *priv = netdev_priv(ndev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002859 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Dai Haruki2c2db482008-12-16 15:31:15 -08002861 /* fcb is at the beginning if exists */
2862 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Jan Ceuleers0977f812012-06-05 03:42:12 +00002864 /* Remove the FCB from the skb
2865 * Remove the padded bytes, if there are any
2866 */
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002867 if (priv->uses_rxfcb)
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002868 skb_pull(skb, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002869
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002870 /* Get receive timestamp from the skb */
2871 if (priv->hwts_rx_en) {
2872 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2873 u64 *ns = (u64 *) skb->data;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002874
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002875 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2876 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2877 }
2878
2879 if (priv->padding)
2880 skb_pull(skb, priv->padding);
2881
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002882 if (ndev->features & NETIF_F_RXCSUM)
Dai Haruki2c2db482008-12-16 15:31:15 -08002883 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002884
Dai Haruki2c2db482008-12-16 15:31:15 -08002885 /* Tell the skb what kind of packet this is */
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002886 skb->protocol = eth_type_trans(skb, ndev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002887
Patrick McHardyf6469682013-04-19 02:04:27 +00002888 /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
David S. Miller823dcd22011-08-20 10:39:12 -07002889 * Even if vlan rx accel is disabled, on some chips
2890 * RXFCB_VLN is pseudo randomly set.
2891 */
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002892 if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX &&
Claudiu Manoil26eb9372015-03-13 10:36:29 +02002893 be16_to_cpu(fcb->flags) & RXFCB_VLN)
2894 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2895 be16_to_cpu(fcb->vlctl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
2898/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002899 * until the budget/quota has been reached. Returns the number
2900 * of frames handled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002902int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002904 struct net_device *ndev = rx_queue->ndev;
Andy Fleming31de1982008-12-16 15:33:40 -08002905 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 struct sk_buff *skb;
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002907 int i, howmany = 0;
2908 int cleaned_cnt = gfar_rxbd_unused(rx_queue);
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002909 struct gfar_private *priv = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002912 base = rx_queue->rx_bd_base;
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002913 i = rx_queue->next_to_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002915 while (rx_work_limit--) {
Claudiu Manoilf9660822015-07-13 16:22:04 +03002916 u32 lstatus;
Dai Haruki2c2db482008-12-16 15:31:15 -08002917
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002918 if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) {
2919 gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
2920 cleaned_cnt = 0;
2921 }
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002922
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002923 bdp = &rx_queue->rx_bd_base[i];
Claudiu Manoilf9660822015-07-13 16:22:04 +03002924 lstatus = be32_to_cpu(bdp->lstatus);
2925 if (lstatus & BD_LFLAG(RXBD_EMPTY))
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002926 break;
2927
2928 /* order rx buffer descriptor reads */
Scott Wood3b6330c2007-05-16 15:06:59 -05002929 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002930
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002931 /* fetch next to clean buffer from the ring */
2932 skb = rx_queue->rx_skbuff[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Claudiu Manoila7312d52015-03-13 10:36:28 +02002934 dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr),
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002935 priv->rx_buffer_size, DMA_FROM_DEVICE);
Andy Fleming81183052008-11-12 10:07:11 -06002936
Claudiu Manoilf9660822015-07-13 16:22:04 +03002937 if (unlikely(!(lstatus & BD_LFLAG(RXBD_ERR)) &&
2938 (lstatus & BD_LENGTH_MASK) > priv->rx_buffer_size))
2939 lstatus |= BD_LFLAG(RXBD_LARGE);
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002940
Claudiu Manoilf9660822015-07-13 16:22:04 +03002941 if (unlikely(!(lstatus & BD_LFLAG(RXBD_LAST)) ||
2942 (lstatus & BD_LFLAG(RXBD_ERR)))) {
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002943 count_errors(lstatus, ndev);
Andy Fleming815b97c2008-04-22 17:18:29 -05002944
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002945 /* discard faulty buffer */
2946 dev_kfree_skb(skb);
2947
Andy Fleming815b97c2008-04-22 17:18:29 -05002948 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002950 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 howmany++;
2952
Dai Haruki2c2db482008-12-16 15:31:15 -08002953 if (likely(skb)) {
Claudiu Manoilf9660822015-07-13 16:22:04 +03002954 int pkt_len = (lstatus & BD_LENGTH_MASK) -
Claudiu Manoila7312d52015-03-13 10:36:28 +02002955 ETH_FCS_LEN;
Dai Haruki2c2db482008-12-16 15:31:15 -08002956 /* Remove the FCS from the packet length */
2957 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002958 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002959 skb_record_rx_queue(skb, rx_queue->qindex);
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002960 gfar_process_frame(ndev, skb);
2961
2962 /* Send the packet up the stack */
2963 napi_gro_receive(&rx_queue->grp->napi_rx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Dai Haruki2c2db482008-12-16 15:31:15 -08002965 } else {
Claudiu Manoilf23223f2015-07-13 16:22:05 +03002966 netif_warn(priv, rx_err, ndev, "Missing skb!\n");
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002967 rx_queue->stats.rx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002968 atomic64_inc(&priv->extra_stats.rx_skbmissing);
Dai Haruki2c2db482008-12-16 15:31:15 -08002969 }
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
2972
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002973 rx_queue->rx_skbuff[i] = NULL;
2974 cleaned_cnt++;
2975 if (unlikely(++i == rx_queue->rx_ring_size))
2976 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
2978
Claudiu Manoil76f31e82015-07-13 16:22:03 +03002979 rx_queue->next_to_clean = i;
2980
2981 if (cleaned_cnt)
2982 gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
2983
2984 /* Update Last Free RxBD pointer for LFC */
2985 if (unlikely(priv->tx_actual_en)) {
2986 bdp = gfar_rxbd_lastfree(rx_queue);
2987 gfar_write(rx_queue->rfbptr, (u32)bdp);
2988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return howmany;
2991}
2992
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002993static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002994{
2995 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002996 container_of(napi, struct gfar_priv_grp, napi_rx);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002997 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002998 struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002999 int work_done = 0;
3000
3001 /* Clear IEVENT, so interrupts aren't called again
3002 * because of the packets that have already arrived
3003 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003004 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03003005
3006 work_done = gfar_clean_rx_ring(rx_queue, budget);
3007
3008 if (work_done < budget) {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003009 u32 imask;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03003010 napi_complete(napi);
3011 /* Clear the halt bit in RSTAT */
3012 gfar_write(&regs->rstat, gfargrp->rstat);
3013
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003014 spin_lock_irq(&gfargrp->grplock);
3015 imask = gfar_read(&regs->imask);
3016 imask |= IMASK_RX_DEFAULT;
3017 gfar_write(&regs->imask, imask);
3018 spin_unlock_irq(&gfargrp->grplock);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03003019 }
3020
3021 return work_done;
3022}
3023
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003024static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003026 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003027 container_of(napi, struct gfar_priv_grp, napi_tx);
3028 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02003029 struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003030 u32 imask;
3031
3032 /* Clear IEVENT, so interrupts aren't called again
3033 * because of the packets that have already arrived
3034 */
3035 gfar_write(&regs->ievent, IEVENT_TX_MASK);
3036
3037 /* run Tx cleanup to completion */
3038 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
3039 gfar_clean_tx_ring(tx_queue);
3040
3041 napi_complete(napi);
3042
3043 spin_lock_irq(&gfargrp->grplock);
3044 imask = gfar_read(&regs->imask);
3045 imask |= IMASK_TX_DEFAULT;
3046 gfar_write(&regs->imask, imask);
3047 spin_unlock_irq(&gfargrp->grplock);
3048
3049 return 0;
3050}
3051
3052static int gfar_poll_rx(struct napi_struct *napi, int budget)
3053{
3054 struct gfar_priv_grp *gfargrp =
3055 container_of(napi, struct gfar_priv_grp, napi_rx);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003056 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003057 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003058 struct gfar_priv_rx_q *rx_queue = NULL;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003059 int work_done = 0, work_done_per_q = 0;
Claudiu Manoil39c0a0d2013-03-21 03:12:13 +00003060 int i, budget_per_q = 0;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00003061 unsigned long rstat_rxf;
3062 int num_act_queues;
Dai Harukid080cd62008-04-09 19:37:51 -05003063
Dai Haruki8c7396a2008-12-17 16:52:00 -08003064 /* Clear IEVENT, so interrupts aren't called again
Jan Ceuleers0977f812012-06-05 03:42:12 +00003065 * because of the packets that have already arrived
3066 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003067 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08003068
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00003069 rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
3070
3071 num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
3072 if (num_act_queues)
3073 budget_per_q = budget/num_act_queues;
3074
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003075 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
3076 /* skip queue if not active */
3077 if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
3078 continue;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003079
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003080 rx_queue = priv->rx_queue[i];
3081 work_done_per_q =
3082 gfar_clean_rx_ring(rx_queue, budget_per_q);
3083 work_done += work_done_per_q;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003084
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003085 /* finished processing this queue */
3086 if (work_done_per_q < budget_per_q) {
3087 /* clear active queue hw indication */
3088 gfar_write(&regs->rstat,
3089 RSTAT_CLEAR_RXF0 >> i);
3090 num_act_queues--;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00003091
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003092 if (!num_act_queues)
3093 break;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003094 }
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003095 }
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003096
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003097 if (!num_act_queues) {
3098 u32 imask;
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003099 napi_complete(napi);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003100
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003101 /* Clear the halt bit in RSTAT */
3102 gfar_write(&regs->rstat, gfargrp->rstat);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003103
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003104 spin_lock_irq(&gfargrp->grplock);
3105 imask = gfar_read(&regs->imask);
3106 imask |= IMASK_RX_DEFAULT;
3107 gfar_write(&regs->imask, imask);
3108 spin_unlock_irq(&gfargrp->grplock);
Dai Harukid080cd62008-04-09 19:37:51 -05003109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003111 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003114static int gfar_poll_tx(struct napi_struct *napi, int budget)
3115{
3116 struct gfar_priv_grp *gfargrp =
3117 container_of(napi, struct gfar_priv_grp, napi_tx);
3118 struct gfar_private *priv = gfargrp->priv;
3119 struct gfar __iomem *regs = gfargrp->regs;
3120 struct gfar_priv_tx_q *tx_queue = NULL;
3121 int has_tx_work = 0;
3122 int i;
3123
3124 /* Clear IEVENT, so interrupts aren't called again
3125 * because of the packets that have already arrived
3126 */
3127 gfar_write(&regs->ievent, IEVENT_TX_MASK);
3128
3129 for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
3130 tx_queue = priv->tx_queue[i];
3131 /* run Tx cleanup to completion */
3132 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
3133 gfar_clean_tx_ring(tx_queue);
3134 has_tx_work = 1;
3135 }
3136 }
3137
3138 if (!has_tx_work) {
3139 u32 imask;
3140 napi_complete(napi);
3141
3142 spin_lock_irq(&gfargrp->grplock);
3143 imask = gfar_read(&regs->imask);
3144 imask |= IMASK_TX_DEFAULT;
3145 gfar_write(&regs->imask, imask);
3146 spin_unlock_irq(&gfargrp->grplock);
3147 }
3148
3149 return 0;
3150}
3151
3152
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003153#ifdef CONFIG_NET_POLL_CONTROLLER
Jan Ceuleers0977f812012-06-05 03:42:12 +00003154/* Polling 'interrupt' - used by things like netconsole to send skbs
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003155 * without having to re-enable interrupts. It's not called while
3156 * the interrupt routine is executing.
3157 */
3158static void gfar_netpoll(struct net_device *dev)
3159{
3160 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00003161 int i;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003162
3163 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003164 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003165 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003166 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3167
3168 disable_irq(gfar_irq(grp, TX)->irq);
3169 disable_irq(gfar_irq(grp, RX)->irq);
3170 disable_irq(gfar_irq(grp, ER)->irq);
3171 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3172 enable_irq(gfar_irq(grp, ER)->irq);
3173 enable_irq(gfar_irq(grp, RX)->irq);
3174 enable_irq(gfar_irq(grp, TX)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003175 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003176 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003177 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003178 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3179
3180 disable_irq(gfar_irq(grp, TX)->irq);
3181 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3182 enable_irq(gfar_irq(grp, TX)->irq);
Anton Vorontsov43de0042009-12-09 02:52:19 -08003183 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003184 }
3185}
3186#endif
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003189static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003191 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003194 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003197 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003198 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003201 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003202 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003204 /* Check for errors */
3205 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003206 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208 return IRQ_HANDLED;
3209}
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211/* Called every time the controller might need to be made
3212 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003213 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 * function converts those variables into the appropriate
3215 * register values, and can bring down the device if needed.
3216 */
3217static void adjust_link(struct net_device *dev)
3218{
3219 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003220 struct phy_device *phydev = priv->phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003222 if (unlikely(phydev->link != priv->oldlink ||
Guenter Roeck0ae93b22015-03-02 12:03:27 -08003223 (phydev->link && (phydev->duplex != priv->oldduplex ||
3224 phydev->speed != priv->oldspeed))))
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003225 gfar_update_link_state(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228/* Update the hash table based on the current list of multicast
3229 * addresses we subscribe to. Also, change the promiscuity of
3230 * the device based on the flags (this function is called
Jan Ceuleers0977f812012-06-05 03:42:12 +00003231 * whenever dev->flags is changed
3232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233static void gfar_set_multi(struct net_device *dev)
3234{
Jiri Pirko22bedad32010-04-01 21:22:57 +00003235 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003237 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 u32 tempval;
3239
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003240 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 /* Set RCTRL to PROM */
3242 tempval = gfar_read(&regs->rctrl);
3243 tempval |= RCTRL_PROM;
3244 gfar_write(&regs->rctrl, tempval);
3245 } else {
3246 /* Set RCTRL to not PROM */
3247 tempval = gfar_read(&regs->rctrl);
3248 tempval &= ~(RCTRL_PROM);
3249 gfar_write(&regs->rctrl, tempval);
3250 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003251
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003252 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003254 gfar_write(&regs->igaddr0, 0xffffffff);
3255 gfar_write(&regs->igaddr1, 0xffffffff);
3256 gfar_write(&regs->igaddr2, 0xffffffff);
3257 gfar_write(&regs->igaddr3, 0xffffffff);
3258 gfar_write(&regs->igaddr4, 0xffffffff);
3259 gfar_write(&regs->igaddr5, 0xffffffff);
3260 gfar_write(&regs->igaddr6, 0xffffffff);
3261 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 gfar_write(&regs->gaddr0, 0xffffffff);
3263 gfar_write(&regs->gaddr1, 0xffffffff);
3264 gfar_write(&regs->gaddr2, 0xffffffff);
3265 gfar_write(&regs->gaddr3, 0xffffffff);
3266 gfar_write(&regs->gaddr4, 0xffffffff);
3267 gfar_write(&regs->gaddr5, 0xffffffff);
3268 gfar_write(&regs->gaddr6, 0xffffffff);
3269 gfar_write(&regs->gaddr7, 0xffffffff);
3270 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003271 int em_num;
3272 int idx;
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003275 gfar_write(&regs->igaddr0, 0x0);
3276 gfar_write(&regs->igaddr1, 0x0);
3277 gfar_write(&regs->igaddr2, 0x0);
3278 gfar_write(&regs->igaddr3, 0x0);
3279 gfar_write(&regs->igaddr4, 0x0);
3280 gfar_write(&regs->igaddr5, 0x0);
3281 gfar_write(&regs->igaddr6, 0x0);
3282 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 gfar_write(&regs->gaddr0, 0x0);
3284 gfar_write(&regs->gaddr1, 0x0);
3285 gfar_write(&regs->gaddr2, 0x0);
3286 gfar_write(&regs->gaddr3, 0x0);
3287 gfar_write(&regs->gaddr4, 0x0);
3288 gfar_write(&regs->gaddr5, 0x0);
3289 gfar_write(&regs->gaddr6, 0x0);
3290 gfar_write(&regs->gaddr7, 0x0);
3291
Andy Fleming7f7f5312005-11-11 12:38:59 -06003292 /* If we have extended hash tables, we need to
3293 * clear the exact match registers to prepare for
Jan Ceuleers0977f812012-06-05 03:42:12 +00003294 * setting them
3295 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003296 if (priv->extended_hash) {
3297 em_num = GFAR_EM_NUM + 1;
3298 gfar_clear_exact_match(dev);
3299 idx = 1;
3300 } else {
3301 idx = 0;
3302 em_num = 0;
3303 }
3304
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003305 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return;
3307
3308 /* Parse the list, and set the appropriate bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003309 netdev_for_each_mc_addr(ha, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003310 if (idx < em_num) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00003311 gfar_set_mac_for_addr(dev, idx, ha->addr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003312 idx++;
3313 } else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003314 gfar_set_hash_for_addr(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
Andy Fleming7f7f5312005-11-11 12:38:59 -06003319
3320/* Clears each of the exact match registers to zero, so they
Jan Ceuleers0977f812012-06-05 03:42:12 +00003321 * don't interfere with normal reception
3322 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003323static void gfar_clear_exact_match(struct net_device *dev)
3324{
3325 int idx;
Joe Perches6a3c910c2011-11-16 09:38:02 +00003326 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
Andy Fleming7f7f5312005-11-11 12:38:59 -06003327
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003328 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
Joe Perchesb6bc7652010-12-21 02:16:08 -08003329 gfar_set_mac_for_addr(dev, idx, zero_arr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003330}
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332/* Set the appropriate hash bit for the given addr */
3333/* The algorithm works like so:
3334 * 1) Take the Destination Address (ie the multicast address), and
3335 * do a CRC on it (little endian), and reverse the bits of the
3336 * result.
3337 * 2) Use the 8 most significant bits as a hash into a 256-entry
3338 * table. The table is controlled through 8 32-bit registers:
3339 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3340 * gaddr7. This means that the 3 most significant bits in the
3341 * hash index which gaddr register to use, and the 5 other bits
3342 * indicate which bit (assuming an IBM numbering scheme, which
3343 * for PowerPC (tm) is usually the case) in the register holds
Jan Ceuleers0977f812012-06-05 03:42:12 +00003344 * the entry.
3345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3347{
3348 u32 tempval;
3349 struct gfar_private *priv = netdev_priv(dev);
Joe Perches6a3c910c2011-11-16 09:38:02 +00003350 u32 result = ether_crc(ETH_ALEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05003351 int width = priv->hash_width;
3352 u8 whichbit = (result >> (32 - width)) & 0x1f;
3353 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 u32 value = (1 << (31-whichbit));
3355
Kumar Gala0bbaf062005-06-20 10:54:21 -05003356 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003358 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
Andy Fleming7f7f5312005-11-11 12:38:59 -06003361
3362/* There are multiple MAC Address register pairs on some controllers
3363 * This function sets the numth pair to a given address
3364 */
Joe Perchesb6bc7652010-12-21 02:16:08 -08003365static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3366 const u8 *addr)
Andy Fleming7f7f5312005-11-11 12:38:59 -06003367{
3368 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003369 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003370 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003371 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003372
3373 macptr += num*2;
3374
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003375 /* For a station address of 0x12345678ABCD in transmission
3376 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
3377 * MACnADDR2 is set to 0x34120000.
Jan Ceuleers0977f812012-06-05 03:42:12 +00003378 */
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003379 tempval = (addr[5] << 24) | (addr[4] << 16) |
3380 (addr[3] << 8) | addr[2];
Andy Fleming7f7f5312005-11-11 12:38:59 -06003381
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003382 gfar_write(macptr, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003383
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003384 tempval = (addr[1] << 24) | (addr[0] << 16);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003385
3386 gfar_write(macptr+1, tempval);
3387}
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003390static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003392 struct gfar_priv_grp *gfargrp = grp_id;
3393 struct gfar __iomem *regs = gfargrp->regs;
3394 struct gfar_private *priv= gfargrp->priv;
3395 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003398 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
3400 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003401 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05003402
3403 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003404 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05003405 (events & IEVENT_MAG))
3406 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003409 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003410 netdev_dbg(dev,
3411 "error interrupt (ievent=0x%08x imask=0x%08x)\n",
Joe Perches59deab22011-06-14 08:57:47 +00003412 events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
3414 /* Update the error counters */
3415 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003416 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
3418 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003419 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003421 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 if (events & IEVENT_XFUN) {
Joe Perches59deab22011-06-14 08:57:47 +00003423 netif_dbg(priv, tx_err, dev,
3424 "TX FIFO underrun, packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003425 dev->stats.tx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003426 atomic64_inc(&priv->extra_stats.tx_underrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Claudiu Manoilbc602282015-05-06 18:07:29 +03003428 schedule_work(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
Joe Perches59deab22011-06-14 08:57:47 +00003430 netif_dbg(priv, tx_err, dev, "Transmit Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
3432 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003433 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003434 atomic64_inc(&priv->extra_stats.rx_bsy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003436 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Joe Perches59deab22011-06-14 08:57:47 +00003438 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3439 gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
3441 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003442 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003443 atomic64_inc(&priv->extra_stats.rx_babr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Joe Perches59deab22011-06-14 08:57:47 +00003445 netif_dbg(priv, rx_err, dev, "babbling RX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
3447 if (events & IEVENT_EBERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003448 atomic64_inc(&priv->extra_stats.eberr);
Joe Perches59deab22011-06-14 08:57:47 +00003449 netif_dbg(priv, rx_err, dev, "bus error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 }
Joe Perches59deab22011-06-14 08:57:47 +00003451 if (events & IEVENT_RXC)
3452 netif_dbg(priv, rx_status, dev, "control frame\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
3454 if (events & IEVENT_BABT) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003455 atomic64_inc(&priv->extra_stats.tx_babt);
Joe Perches59deab22011-06-14 08:57:47 +00003456 netif_dbg(priv, tx_err, dev, "babbling TX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
3458 return IRQ_HANDLED;
3459}
3460
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003461static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
3462{
3463 struct phy_device *phydev = priv->phydev;
3464 u32 val = 0;
3465
3466 if (!phydev->duplex)
3467 return val;
3468
3469 if (!priv->pause_aneg_en) {
3470 if (priv->tx_pause_en)
3471 val |= MACCFG1_TX_FLOW;
3472 if (priv->rx_pause_en)
3473 val |= MACCFG1_RX_FLOW;
3474 } else {
3475 u16 lcl_adv, rmt_adv;
3476 u8 flowctrl;
3477 /* get link partner capabilities */
3478 rmt_adv = 0;
3479 if (phydev->pause)
3480 rmt_adv = LPA_PAUSE_CAP;
3481 if (phydev->asym_pause)
3482 rmt_adv |= LPA_PAUSE_ASYM;
3483
Pavaluca Matei-B4661043ef8d22014-10-27 10:42:43 +02003484 lcl_adv = 0;
3485 if (phydev->advertising & ADVERTISED_Pause)
3486 lcl_adv |= ADVERTISE_PAUSE_CAP;
3487 if (phydev->advertising & ADVERTISED_Asym_Pause)
3488 lcl_adv |= ADVERTISE_PAUSE_ASYM;
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003489
3490 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
3491 if (flowctrl & FLOW_CTRL_TX)
3492 val |= MACCFG1_TX_FLOW;
3493 if (flowctrl & FLOW_CTRL_RX)
3494 val |= MACCFG1_RX_FLOW;
3495 }
3496
3497 return val;
3498}
3499
3500static noinline void gfar_update_link_state(struct gfar_private *priv)
3501{
3502 struct gfar __iomem *regs = priv->gfargrp[0].regs;
3503 struct phy_device *phydev = priv->phydev;
Matei Pavaluca45b679c92014-10-27 10:42:44 +02003504 struct gfar_priv_rx_q *rx_queue = NULL;
3505 int i;
3506 struct rxbd8 *bdp;
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003507
3508 if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
3509 return;
3510
3511 if (phydev->link) {
3512 u32 tempval1 = gfar_read(&regs->maccfg1);
3513 u32 tempval = gfar_read(&regs->maccfg2);
3514 u32 ecntrl = gfar_read(&regs->ecntrl);
Matei Pavaluca45b679c92014-10-27 10:42:44 +02003515 u32 tx_flow_oldval = (tempval & MACCFG1_TX_FLOW);
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003516
3517 if (phydev->duplex != priv->oldduplex) {
3518 if (!(phydev->duplex))
3519 tempval &= ~(MACCFG2_FULL_DUPLEX);
3520 else
3521 tempval |= MACCFG2_FULL_DUPLEX;
3522
3523 priv->oldduplex = phydev->duplex;
3524 }
3525
3526 if (phydev->speed != priv->oldspeed) {
3527 switch (phydev->speed) {
3528 case 1000:
3529 tempval =
3530 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
3531
3532 ecntrl &= ~(ECNTRL_R100);
3533 break;
3534 case 100:
3535 case 10:
3536 tempval =
3537 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
3538
3539 /* Reduced mode distinguishes
3540 * between 10 and 100
3541 */
3542 if (phydev->speed == SPEED_100)
3543 ecntrl |= ECNTRL_R100;
3544 else
3545 ecntrl &= ~(ECNTRL_R100);
3546 break;
3547 default:
3548 netif_warn(priv, link, priv->ndev,
3549 "Ack! Speed (%d) is not 10/100/1000!\n",
3550 phydev->speed);
3551 break;
3552 }
3553
3554 priv->oldspeed = phydev->speed;
3555 }
3556
3557 tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
3558 tempval1 |= gfar_get_flowctrl_cfg(priv);
3559
Matei Pavaluca45b679c92014-10-27 10:42:44 +02003560 /* Turn last free buffer recording on */
3561 if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) {
3562 for (i = 0; i < priv->num_rx_queues; i++) {
3563 rx_queue = priv->rx_queue[i];
Claudiu Manoil76f31e82015-07-13 16:22:03 +03003564 bdp = gfar_rxbd_lastfree(rx_queue);
3565 gfar_write(rx_queue->rfbptr, (u32)bdp);
Matei Pavaluca45b679c92014-10-27 10:42:44 +02003566 }
3567
3568 priv->tx_actual_en = 1;
3569 }
3570
3571 if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval))
3572 priv->tx_actual_en = 0;
3573
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003574 gfar_write(&regs->maccfg1, tempval1);
3575 gfar_write(&regs->maccfg2, tempval);
3576 gfar_write(&regs->ecntrl, ecntrl);
3577
3578 if (!priv->oldlink)
3579 priv->oldlink = 1;
3580
3581 } else if (priv->oldlink) {
3582 priv->oldlink = 0;
3583 priv->oldspeed = 0;
3584 priv->oldduplex = -1;
3585 }
3586
3587 if (netif_msg_link(priv))
3588 phy_print_status(phydev);
3589}
3590
Fabian Frederick94e5a2a2015-03-17 19:37:34 +01003591static const struct of_device_id gfar_match[] =
Andy Flemingb31a1d82008-12-16 15:29:15 -08003592{
3593 {
3594 .type = "network",
3595 .compatible = "gianfar",
3596 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003597 {
3598 .compatible = "fsl,etsec2",
3599 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003600 {},
3601};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003602MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604/* Structure for a device driver */
Grant Likely74888762011-02-22 21:05:51 -07003605static struct platform_driver gfar_driver = {
Grant Likely40182942010-04-13 16:13:02 -07003606 .driver = {
3607 .name = "fsl-gianfar",
Grant Likely40182942010-04-13 16:13:02 -07003608 .pm = GFAR_PM_OPS,
3609 .of_match_table = gfar_match,
3610 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 .probe = gfar_probe,
3612 .remove = gfar_remove,
3613};
3614
Axel Lindb62f682011-11-27 16:44:17 +00003615module_platform_driver(gfar_driver);