blob: 379b1a578d3da37deb4e23114a72631085231252 [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);
Andy Fleming815b97c2008-04-22 17:18:29 -0500119struct sk_buff *gfar_new_skb(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000120static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000121 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int gfar_set_mac_address(struct net_device *dev);
123static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100124static irqreturn_t gfar_error(int irq, void *dev_id);
125static irqreturn_t gfar_transmit(int irq, void *dev_id);
126static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static void adjust_link(struct net_device *dev);
Claudiu Manoil6ce29b02014-04-30 14:27:21 +0300128static noinline void gfar_update_link_state(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int init_phy(struct net_device *dev);
Grant Likely74888762011-02-22 21:05:51 -0700130static int gfar_probe(struct platform_device *ofdev);
Grant Likely2dc11582010-08-06 09:25:50 -0600131static int gfar_remove(struct platform_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400132static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void gfar_set_multi(struct net_device *dev);
134static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500135static void gfar_configure_serdes(struct net_device *dev);
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200136static int gfar_poll_rx(struct napi_struct *napi, int budget);
137static int gfar_poll_tx(struct napi_struct *napi, int budget);
138static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
139static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300140#ifdef CONFIG_NET_POLL_CONTROLLER
141static void gfar_netpoll(struct net_device *dev);
142#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000143int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
Claudiu Manoilc233cf402013-03-19 07:40:02 +0000144static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Claudiu Manoil61db26c2013-02-14 05:00:05 +0000145static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
146 int amount_pull, struct napi_struct *napi);
Claudiu Manoilc10650b2014-02-17 12:53:18 +0200147static void gfar_halt_nodisable(struct gfar_private *priv);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600148static void gfar_clear_exact_match(struct net_device *dev);
Joe Perchesb6bc7652010-12-21 02:16:08 -0800149static void gfar_set_mac_for_addr(struct net_device *dev, int num,
150 const u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000151static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153MODULE_AUTHOR("Freescale Semiconductor, Inc");
154MODULE_DESCRIPTION("Gianfar Ethernet Driver");
155MODULE_LICENSE("GPL");
156
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000157static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000158 dma_addr_t buf)
159{
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000160 u32 lstatus;
161
162 bdp->bufPtr = buf;
163
164 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000165 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000166 lstatus |= BD_LFLAG(RXBD_WRAP);
167
Claudiu Manoild55398b2014-10-07 10:44:35 +0300168 gfar_wmb();
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000169
170 bdp->lstatus = lstatus;
171}
172
Anton Vorontsov87283272009-10-12 06:00:39 +0000173static int gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000174{
Anton Vorontsov87283272009-10-12 06:00:39 +0000175 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000176 struct gfar_priv_tx_q *tx_queue = NULL;
177 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000178 struct txbd8 *txbdp;
179 struct rxbd8 *rxbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000180 int i, j;
Anton Vorontsov87283272009-10-12 06:00:39 +0000181
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000182 for (i = 0; i < priv->num_tx_queues; i++) {
183 tx_queue = priv->tx_queue[i];
184 /* Initialize some variables in our dev structure */
185 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
186 tx_queue->dirty_tx = tx_queue->tx_bd_base;
187 tx_queue->cur_tx = tx_queue->tx_bd_base;
188 tx_queue->skb_curtx = 0;
189 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000190
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000191 /* Initialize Transmit Descriptor Ring */
192 txbdp = tx_queue->tx_bd_base;
193 for (j = 0; j < tx_queue->tx_ring_size; j++) {
194 txbdp->lstatus = 0;
195 txbdp->bufPtr = 0;
196 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000197 }
198
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000199 /* Set the last descriptor in the ring to indicate wrap */
200 txbdp--;
201 txbdp->status |= TXBD_WRAP;
202 }
203
204 for (i = 0; i < priv->num_rx_queues; i++) {
205 rx_queue = priv->rx_queue[i];
206 rx_queue->cur_rx = rx_queue->rx_bd_base;
207 rx_queue->skb_currx = 0;
208 rxbdp = rx_queue->rx_bd_base;
209
210 for (j = 0; j < rx_queue->rx_ring_size; j++) {
211 struct sk_buff *skb = rx_queue->rx_skbuff[j];
212
213 if (skb) {
214 gfar_init_rxbdp(rx_queue, rxbdp,
215 rxbdp->bufPtr);
216 } else {
217 skb = gfar_new_skb(ndev);
218 if (!skb) {
Joe Perches59deab22011-06-14 08:57:47 +0000219 netdev_err(ndev, "Can't allocate RX buffers\n");
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +0000220 return -ENOMEM;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000221 }
222 rx_queue->rx_skbuff[j] = skb;
223
224 gfar_new_rxbdp(rx_queue, rxbdp, skb);
225 }
226
227 rxbdp++;
228 }
229
Anton Vorontsov87283272009-10-12 06:00:39 +0000230 }
231
232 return 0;
233}
234
235static int gfar_alloc_skb_resources(struct net_device *ndev)
236{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000237 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000238 dma_addr_t addr;
239 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000240 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil369ec162013-02-14 05:00:02 +0000241 struct device *dev = priv->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000242 struct gfar_priv_tx_q *tx_queue = NULL;
243 struct gfar_priv_rx_q *rx_queue = NULL;
244
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000245 priv->total_tx_ring_size = 0;
246 for (i = 0; i < priv->num_tx_queues; i++)
247 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
248
249 priv->total_rx_ring_size = 0;
250 for (i = 0; i < priv->num_rx_queues; i++)
251 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000252
253 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000254 vaddr = dma_alloc_coherent(dev,
Joe Perchesd0320f72013-03-14 13:07:21 +0000255 (priv->total_tx_ring_size *
256 sizeof(struct txbd8)) +
257 (priv->total_rx_ring_size *
258 sizeof(struct rxbd8)),
259 &addr, GFP_KERNEL);
260 if (!vaddr)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000261 return -ENOMEM;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000262
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000263 for (i = 0; i < priv->num_tx_queues; i++) {
264 tx_queue = priv->tx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000265 tx_queue->tx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000266 tx_queue->tx_bd_dma_base = addr;
267 tx_queue->dev = ndev;
268 /* enet DMA only understands physical addresses */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000269 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
270 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000271 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000272
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000273 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000274 for (i = 0; i < priv->num_rx_queues; i++) {
275 rx_queue = priv->rx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000276 rx_queue->rx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000277 rx_queue->rx_bd_dma_base = addr;
278 rx_queue->dev = ndev;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000279 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
280 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000281 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000282
283 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000284 for (i = 0; i < priv->num_tx_queues; i++) {
285 tx_queue = priv->tx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000286 tx_queue->tx_skbuff =
287 kmalloc_array(tx_queue->tx_ring_size,
288 sizeof(*tx_queue->tx_skbuff),
289 GFP_KERNEL);
290 if (!tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000291 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000292
293 for (k = 0; k < tx_queue->tx_ring_size; k++)
294 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000295 }
296
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000297 for (i = 0; i < priv->num_rx_queues; i++) {
298 rx_queue = priv->rx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000299 rx_queue->rx_skbuff =
300 kmalloc_array(rx_queue->rx_ring_size,
301 sizeof(*rx_queue->rx_skbuff),
302 GFP_KERNEL);
303 if (!rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000304 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000305
306 for (j = 0; j < rx_queue->rx_ring_size; j++)
307 rx_queue->rx_skbuff[j] = NULL;
308 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000309
Anton Vorontsov87283272009-10-12 06:00:39 +0000310 if (gfar_init_bds(ndev))
311 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000312
313 return 0;
314
315cleanup:
316 free_skb_resources(priv);
317 return -ENOMEM;
318}
319
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000320static void gfar_init_tx_rx_base(struct gfar_private *priv)
321{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000322 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000323 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000324 int i;
325
326 baddr = &regs->tbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000327 for (i = 0; i < priv->num_tx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000328 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000329 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000330 }
331
332 baddr = &regs->rbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000333 for (i = 0; i < priv->num_rx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000334 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000335 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000336 }
337}
338
Claudiu Manoil88302642014-02-24 12:13:43 +0200339static void gfar_rx_buff_size_config(struct gfar_private *priv)
340{
341 int frame_size = priv->ndev->mtu + ETH_HLEN;
342
343 /* set this when rx hw offload (TOE) functions are being used */
344 priv->uses_rxfcb = 0;
345
346 if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
347 priv->uses_rxfcb = 1;
348
349 if (priv->hwts_rx_en)
350 priv->uses_rxfcb = 1;
351
352 if (priv->uses_rxfcb)
353 frame_size += GMAC_FCB_LEN;
354
355 frame_size += priv->padding;
356
357 frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
358 INCREMENTAL_BUFFER_SIZE;
359
360 priv->rx_buffer_size = frame_size;
361}
362
Claudiu Manoila328ac92014-02-24 12:13:42 +0200363static void gfar_mac_rx_config(struct gfar_private *priv)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000364{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000365 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000366 u32 rctrl = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000367
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000368 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000369 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000370 /* Program the RIR0 reg with the required distribution */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200371 if (priv->poll_mode == GFAR_SQ_POLLING)
372 gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
373 else /* GFAR_MQ_POLLING */
374 gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000375 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000376
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000377 /* Restore PROMISC mode */
Claudiu Manoila328ac92014-02-24 12:13:42 +0200378 if (priv->ndev->flags & IFF_PROMISC)
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000379 rctrl |= RCTRL_PROM;
380
Claudiu Manoil88302642014-02-24 12:13:43 +0200381 if (priv->ndev->features & NETIF_F_RXCSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000382 rctrl |= RCTRL_CHECKSUMMING;
383
Claudiu Manoil88302642014-02-24 12:13:43 +0200384 if (priv->extended_hash)
385 rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000386
387 if (priv->padding) {
388 rctrl &= ~RCTRL_PAL_MASK;
389 rctrl |= RCTRL_PADDING(priv->padding);
390 }
391
Manfred Rudigier97553f72010-06-11 01:49:05 +0000392 /* Enable HW time stamping if requested from user space */
Claudiu Manoil88302642014-02-24 12:13:43 +0200393 if (priv->hwts_rx_en)
Manfred Rudigier97553f72010-06-11 01:49:05 +0000394 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
395
Claudiu Manoil88302642014-02-24 12:13:43 +0200396 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
Sebastian Pöhnb852b722011-07-26 00:03:13 +0000397 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000398
399 /* Init rctrl based on our settings */
400 gfar_write(&regs->rctrl, rctrl);
Claudiu Manoila328ac92014-02-24 12:13:42 +0200401}
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000402
Claudiu Manoila328ac92014-02-24 12:13:42 +0200403static void gfar_mac_tx_config(struct gfar_private *priv)
404{
405 struct gfar __iomem *regs = priv->gfargrp[0].regs;
406 u32 tctrl = 0;
407
408 if (priv->ndev->features & NETIF_F_IP_CSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000409 tctrl |= TCTRL_INIT_CSUM;
410
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +0000411 if (priv->prio_sched_en)
412 tctrl |= TCTRL_TXSCHED_PRIO;
413 else {
414 tctrl |= TCTRL_TXSCHED_WRRS;
415 gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
416 gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
417 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000418
Claudiu Manoil88302642014-02-24 12:13:43 +0200419 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
420 tctrl |= TCTRL_VLINS;
421
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000422 gfar_write(&regs->tctrl, tctrl);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000423}
424
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200425static void gfar_configure_coalescing(struct gfar_private *priv,
426 unsigned long tx_mask, unsigned long rx_mask)
427{
428 struct gfar __iomem *regs = priv->gfargrp[0].regs;
429 u32 __iomem *baddr;
430
431 if (priv->mode == MQ_MG_MODE) {
432 int i = 0;
433
434 baddr = &regs->txic0;
435 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
436 gfar_write(baddr + i, 0);
437 if (likely(priv->tx_queue[i]->txcoalescing))
438 gfar_write(baddr + i, priv->tx_queue[i]->txic);
439 }
440
441 baddr = &regs->rxic0;
442 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
443 gfar_write(baddr + i, 0);
444 if (likely(priv->rx_queue[i]->rxcoalescing))
445 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
446 }
447 } else {
448 /* Backward compatible case -- even if we enable
449 * multiple queues, there's only single reg to program
450 */
451 gfar_write(&regs->txic, 0);
452 if (likely(priv->tx_queue[0]->txcoalescing))
453 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
454
455 gfar_write(&regs->rxic, 0);
456 if (unlikely(priv->rx_queue[0]->rxcoalescing))
457 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
458 }
459}
460
461void gfar_configure_coalescing_all(struct gfar_private *priv)
462{
463 gfar_configure_coalescing(priv, 0xFF, 0xFF);
464}
465
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000466static struct net_device_stats *gfar_get_stats(struct net_device *dev)
467{
468 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000469 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
470 unsigned long tx_packets = 0, tx_bytes = 0;
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000471 int i;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000472
473 for (i = 0; i < priv->num_rx_queues; i++) {
474 rx_packets += priv->rx_queue[i]->stats.rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000475 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000476 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
477 }
478
479 dev->stats.rx_packets = rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000480 dev->stats.rx_bytes = rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000481 dev->stats.rx_dropped = rx_dropped;
482
483 for (i = 0; i < priv->num_tx_queues; i++) {
Eric Dumazet1ac9ad12011-01-12 12:13:14 +0000484 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
485 tx_packets += priv->tx_queue[i]->stats.tx_packets;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000486 }
487
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000488 dev->stats.tx_bytes = tx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000489 dev->stats.tx_packets = tx_packets;
490
491 return &dev->stats;
492}
493
Andy Fleming26ccfc32009-03-10 12:58:28 +0000494static const struct net_device_ops gfar_netdev_ops = {
495 .ndo_open = gfar_enet_open,
496 .ndo_start_xmit = gfar_start_xmit,
497 .ndo_stop = gfar_close,
498 .ndo_change_mtu = gfar_change_mtu,
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000499 .ndo_set_features = gfar_set_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000500 .ndo_set_rx_mode = gfar_set_multi,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000501 .ndo_tx_timeout = gfar_timeout,
502 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000503 .ndo_get_stats = gfar_get_stats,
Ben Hutchings240c1022009-07-09 17:54:35 +0000504 .ndo_set_mac_address = eth_mac_addr,
505 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000506#ifdef CONFIG_NET_POLL_CONTROLLER
507 .ndo_poll_controller = gfar_netpoll,
508#endif
509};
510
Claudiu Manoilefeddce2014-02-17 12:53:17 +0200511static void gfar_ints_disable(struct gfar_private *priv)
512{
513 int i;
514 for (i = 0; i < priv->num_grps; i++) {
515 struct gfar __iomem *regs = priv->gfargrp[i].regs;
516 /* Clear IEVENT */
517 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
518
519 /* Initialize IMASK */
520 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
521 }
522}
523
524static void gfar_ints_enable(struct gfar_private *priv)
525{
526 int i;
527 for (i = 0; i < priv->num_grps; i++) {
528 struct gfar __iomem *regs = priv->gfargrp[i].regs;
529 /* Unmask the interrupts we look for */
530 gfar_write(&regs->imask, IMASK_DEFAULT);
531 }
532}
533
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000534void lock_tx_qs(struct gfar_private *priv)
535{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000536 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000537
538 for (i = 0; i < priv->num_tx_queues; i++)
539 spin_lock(&priv->tx_queue[i]->txlock);
540}
541
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000542void unlock_tx_qs(struct gfar_private *priv)
543{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000544 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000545
546 for (i = 0; i < priv->num_tx_queues; i++)
547 spin_unlock(&priv->tx_queue[i]->txlock);
548}
549
Claudiu Manoil20862782014-02-17 12:53:14 +0200550static int gfar_alloc_tx_queues(struct gfar_private *priv)
551{
552 int i;
553
554 for (i = 0; i < priv->num_tx_queues; i++) {
555 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
556 GFP_KERNEL);
557 if (!priv->tx_queue[i])
558 return -ENOMEM;
559
560 priv->tx_queue[i]->tx_skbuff = NULL;
561 priv->tx_queue[i]->qindex = i;
562 priv->tx_queue[i]->dev = priv->ndev;
563 spin_lock_init(&(priv->tx_queue[i]->txlock));
564 }
565 return 0;
566}
567
568static int gfar_alloc_rx_queues(struct gfar_private *priv)
569{
570 int i;
571
572 for (i = 0; i < priv->num_rx_queues; i++) {
573 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
574 GFP_KERNEL);
575 if (!priv->rx_queue[i])
576 return -ENOMEM;
577
578 priv->rx_queue[i]->rx_skbuff = NULL;
579 priv->rx_queue[i]->qindex = i;
580 priv->rx_queue[i]->dev = priv->ndev;
Claudiu Manoil20862782014-02-17 12:53:14 +0200581 }
582 return 0;
583}
584
585static void gfar_free_tx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000586{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000587 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000588
589 for (i = 0; i < priv->num_tx_queues; i++)
590 kfree(priv->tx_queue[i]);
591}
592
Claudiu Manoil20862782014-02-17 12:53:14 +0200593static void gfar_free_rx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000594{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000595 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000596
597 for (i = 0; i < priv->num_rx_queues; i++)
598 kfree(priv->rx_queue[i]);
599}
600
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000601static void unmap_group_regs(struct gfar_private *priv)
602{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000603 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000604
605 for (i = 0; i < MAXGROUPS; i++)
606 if (priv->gfargrp[i].regs)
607 iounmap(priv->gfargrp[i].regs);
608}
609
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000610static void free_gfar_dev(struct gfar_private *priv)
611{
612 int i, j;
613
614 for (i = 0; i < priv->num_grps; i++)
615 for (j = 0; j < GFAR_NUM_IRQS; j++) {
616 kfree(priv->gfargrp[i].irqinfo[j]);
617 priv->gfargrp[i].irqinfo[j] = NULL;
618 }
619
620 free_netdev(priv->ndev);
621}
622
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000623static void disable_napi(struct gfar_private *priv)
624{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000625 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000626
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200627 for (i = 0; i < priv->num_grps; i++) {
628 napi_disable(&priv->gfargrp[i].napi_rx);
629 napi_disable(&priv->gfargrp[i].napi_tx);
630 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000631}
632
633static void enable_napi(struct gfar_private *priv)
634{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000635 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000636
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200637 for (i = 0; i < priv->num_grps; i++) {
638 napi_enable(&priv->gfargrp[i].napi_rx);
639 napi_enable(&priv->gfargrp[i].napi_tx);
640 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000641}
642
643static int gfar_parse_group(struct device_node *np,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000644 struct gfar_private *priv, const char *model)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000645{
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000646 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000647 int i;
648
Paul Gortmaker7c1e7e92013-02-04 09:49:42 +0000649 for (i = 0; i < GFAR_NUM_IRQS; i++) {
650 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
651 GFP_KERNEL);
652 if (!grp->irqinfo[i])
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000653 return -ENOMEM;
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000654 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000655
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000656 grp->regs = of_iomap(np, 0);
657 if (!grp->regs)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000658 return -ENOMEM;
659
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000660 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000661
662 /* If we aren't the FEC we have multiple interrupts */
663 if (model && strcasecmp(model, "FEC")) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000664 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
665 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
666 if (gfar_irq(grp, TX)->irq == NO_IRQ ||
667 gfar_irq(grp, RX)->irq == NO_IRQ ||
668 gfar_irq(grp, ER)->irq == NO_IRQ)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000669 return -EINVAL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000670 }
671
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000672 grp->priv = priv;
673 spin_lock_init(&grp->grplock);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000674 if (priv->mode == MQ_MG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200675 u32 *rxq_mask, *txq_mask;
676 rxq_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
677 txq_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
678
679 if (priv->poll_mode == GFAR_SQ_POLLING) {
680 /* One Q per interrupt group: Q0 to G0, Q1 to G1 */
681 grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
682 grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
683 } else { /* GFAR_MQ_POLLING */
684 grp->rx_bit_map = rxq_mask ?
685 *rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
686 grp->tx_bit_map = txq_mask ?
687 *txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
688 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000689 } else {
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000690 grp->rx_bit_map = 0xFF;
691 grp->tx_bit_map = 0xFF;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000692 }
Claudiu Manoil20862782014-02-17 12:53:14 +0200693
694 /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
695 * right to left, so we need to revert the 8 bits to get the q index
696 */
697 grp->rx_bit_map = bitrev8(grp->rx_bit_map);
698 grp->tx_bit_map = bitrev8(grp->tx_bit_map);
699
700 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
701 * also assign queues to groups
702 */
703 for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200704 if (!grp->rx_queue)
705 grp->rx_queue = priv->rx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200706 grp->num_rx_queues++;
707 grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
708 priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
709 priv->rx_queue[i]->grp = grp;
710 }
711
712 for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200713 if (!grp->tx_queue)
714 grp->tx_queue = priv->tx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200715 grp->num_tx_queues++;
716 grp->tstat |= (TSTAT_CLEAR_THALT >> i);
717 priv->tqueue |= (TQUEUE_EN0 >> i);
718 priv->tx_queue[i]->grp = grp;
719 }
720
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000721 priv->num_grps++;
722
723 return 0;
724}
725
Grant Likely2dc11582010-08-06 09:25:50 -0600726static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800727{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800728 const char *model;
729 const char *ctype;
730 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000731 int err = 0, i;
732 struct net_device *dev = NULL;
733 struct gfar_private *priv = NULL;
Grant Likely61c7a082010-04-13 16:12:29 -0700734 struct device_node *np = ofdev->dev.of_node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000735 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800736 const u32 *stash;
737 const u32 *stash_len;
738 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000739 unsigned int num_tx_qs, num_rx_qs;
740 u32 *tx_queues, *rx_queues;
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200741 unsigned short mode, poll_mode;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800742
743 if (!np || !of_device_is_available(np))
744 return -ENODEV;
745
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200746 if (of_device_is_compatible(np, "fsl,etsec2")) {
747 mode = MQ_MG_MODE;
748 poll_mode = GFAR_SQ_POLLING;
749 } else {
750 mode = SQ_SG_MODE;
751 poll_mode = GFAR_SQ_POLLING;
752 }
753
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200754 /* parse the num of HW tx and rx queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000755 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200756 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
757
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200758 if (mode == SQ_SG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200759 num_tx_qs = 1;
760 num_rx_qs = 1;
761 } else { /* MQ_MG_MODE */
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200762 /* get the actual number of supported groups */
763 unsigned int num_grps = of_get_available_child_count(np);
764
765 if (num_grps == 0 || num_grps > MAXGROUPS) {
766 dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
767 num_grps);
768 pr_err("Cannot do alloc_etherdev, aborting\n");
769 return -EINVAL;
770 }
771
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200772 if (poll_mode == GFAR_SQ_POLLING) {
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200773 num_tx_qs = num_grps; /* one txq per int group */
774 num_rx_qs = num_grps; /* one rxq per int group */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200775 } else { /* GFAR_MQ_POLLING */
776 num_tx_qs = tx_queues ? *tx_queues : 1;
777 num_rx_qs = rx_queues ? *rx_queues : 1;
778 }
779 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000780
781 if (num_tx_qs > MAX_TX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000782 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
783 num_tx_qs, MAX_TX_QS);
784 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000785 return -EINVAL;
786 }
787
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000788 if (num_rx_qs > MAX_RX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000789 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
790 num_rx_qs, MAX_RX_QS);
791 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000792 return -EINVAL;
793 }
794
795 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
796 dev = *pdev;
797 if (NULL == dev)
798 return -ENOMEM;
799
800 priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000801 priv->ndev = dev;
802
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200803 priv->mode = mode;
804 priv->poll_mode = poll_mode;
805
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000806 priv->num_tx_queues = num_tx_qs;
Ben Hutchingsfe069122010-09-27 08:27:37 +0000807 netif_set_real_num_rx_queues(dev, num_rx_qs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000808 priv->num_rx_queues = num_rx_qs;
Claudiu Manoil20862782014-02-17 12:53:14 +0200809
810 err = gfar_alloc_tx_queues(priv);
811 if (err)
812 goto tx_alloc_failed;
813
814 err = gfar_alloc_rx_queues(priv);
815 if (err)
816 goto rx_alloc_failed;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800817
Jan Ceuleers0977f812012-06-05 03:42:12 +0000818 /* Init Rx queue filer rule set linked list */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700819 INIT_LIST_HEAD(&priv->rx_list.list);
820 priv->rx_list.count = 0;
821 mutex_init(&priv->rx_queue_access);
822
Andy Flemingb31a1d82008-12-16 15:29:15 -0800823 model = of_get_property(np, "model", NULL);
824
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000825 for (i = 0; i < MAXGROUPS; i++)
826 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800827
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000828 /* Parse and initialize group specific information */
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200829 if (priv->mode == MQ_MG_MODE) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000830 for_each_child_of_node(np, child) {
831 err = gfar_parse_group(child, priv, model);
832 if (err)
833 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800834 }
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200835 } else { /* SQ_SG_MODE */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000836 err = gfar_parse_group(np, priv, model);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000837 if (err)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000838 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800839 }
840
Andy Fleming4d7902f2009-02-04 16:43:44 -0800841 stash = of_get_property(np, "bd-stash", NULL);
842
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000843 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800844 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
845 priv->bd_stash_en = 1;
846 }
847
848 stash_len = of_get_property(np, "rx-stash-len", NULL);
849
850 if (stash_len)
851 priv->rx_stash_size = *stash_len;
852
853 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
854
855 if (stash_idx)
856 priv->rx_stash_index = *stash_idx;
857
858 if (stash_len || stash_idx)
859 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
860
Andy Flemingb31a1d82008-12-16 15:29:15 -0800861 mac_addr = of_get_mac_address(np);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000862
Andy Flemingb31a1d82008-12-16 15:29:15 -0800863 if (mac_addr)
Joe Perches6a3c9102011-11-16 09:38:02 +0000864 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800865
866 if (model && !strcasecmp(model, "TSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200867 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000868 FSL_GIANFAR_DEV_HAS_COALESCE |
869 FSL_GIANFAR_DEV_HAS_RMON |
870 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
871
Andy Flemingb31a1d82008-12-16 15:29:15 -0800872 if (model && !strcasecmp(model, "eTSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200873 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000874 FSL_GIANFAR_DEV_HAS_COALESCE |
875 FSL_GIANFAR_DEV_HAS_RMON |
876 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000877 FSL_GIANFAR_DEV_HAS_CSUM |
878 FSL_GIANFAR_DEV_HAS_VLAN |
879 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
880 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
881 FSL_GIANFAR_DEV_HAS_TIMER;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800882
883 ctype = of_get_property(np, "phy-connection-type", NULL);
884
885 /* We only care about rgmii-id. The rest are autodetected */
886 if (ctype && !strcmp(ctype, "rgmii-id"))
887 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
888 else
889 priv->interface = PHY_INTERFACE_MODE_MII;
890
891 if (of_get_property(np, "fsl,magic-packet", NULL))
892 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
893
Grant Likelyfe192a42009-04-25 12:53:12 +0000894 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800895
Florian Fainellibe403642014-05-22 09:47:48 -0700896 /* In the case of a fixed PHY, the DT node associated
897 * to the PHY is the Ethernet MAC DT node.
898 */
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200899 if (!priv->phy_node && of_phy_is_fixed_link(np)) {
Florian Fainellibe403642014-05-22 09:47:48 -0700900 err = of_phy_register_fixed_link(np);
901 if (err)
902 goto err_grp_init;
903
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200904 priv->phy_node = of_node_get(np);
Florian Fainellibe403642014-05-22 09:47:48 -0700905 }
906
Andy Flemingb31a1d82008-12-16 15:29:15 -0800907 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000908 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800909
910 return 0;
911
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000912err_grp_init:
913 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +0200914rx_alloc_failed:
915 gfar_free_rx_queues(priv);
916tx_alloc_failed:
917 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000918 free_gfar_dev(priv);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800919 return err;
920}
921
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000922static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000923{
924 struct hwtstamp_config config;
925 struct gfar_private *priv = netdev_priv(netdev);
926
927 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
928 return -EFAULT;
929
930 /* reserved for future extensions */
931 if (config.flags)
932 return -EINVAL;
933
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000934 switch (config.tx_type) {
935 case HWTSTAMP_TX_OFF:
936 priv->hwts_tx_en = 0;
937 break;
938 case HWTSTAMP_TX_ON:
939 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
940 return -ERANGE;
941 priv->hwts_tx_en = 1;
942 break;
943 default:
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000944 return -ERANGE;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000945 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000946
947 switch (config.rx_filter) {
948 case HWTSTAMP_FILTER_NONE:
Manfred Rudigier97553f72010-06-11 01:49:05 +0000949 if (priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000950 priv->hwts_rx_en = 0;
Claudiu Manoil08511332014-02-24 12:13:45 +0200951 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000952 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000953 break;
954 default:
955 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
956 return -ERANGE;
Manfred Rudigier97553f72010-06-11 01:49:05 +0000957 if (!priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000958 priv->hwts_rx_en = 1;
Claudiu Manoil08511332014-02-24 12:13:45 +0200959 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000960 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000961 config.rx_filter = HWTSTAMP_FILTER_ALL;
962 break;
963 }
964
965 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
966 -EFAULT : 0;
967}
968
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000969static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
970{
971 struct hwtstamp_config config;
972 struct gfar_private *priv = netdev_priv(netdev);
973
974 config.flags = 0;
975 config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
976 config.rx_filter = (priv->hwts_rx_en ?
977 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
978
979 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
980 -EFAULT : 0;
981}
982
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000983static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
984{
985 struct gfar_private *priv = netdev_priv(dev);
986
987 if (!netif_running(dev))
988 return -EINVAL;
989
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000990 if (cmd == SIOCSHWTSTAMP)
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000991 return gfar_hwtstamp_set(dev, rq);
992 if (cmd == SIOCGHWTSTAMP)
993 return gfar_hwtstamp_get(dev, rq);
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000994
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000995 if (!priv->phydev)
996 return -ENODEV;
997
Richard Cochran28b04112010-07-17 08:48:55 +0000998 return phy_mii_ioctl(priv->phydev, rq, cmd);
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000999}
1000
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001001static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
1002 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001003{
1004 u32 rqfpr = FPR_FILER_MASK;
1005 u32 rqfcr = 0x0;
1006
1007 rqfar--;
1008 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001009 priv->ftp_rqfpr[rqfar] = rqfpr;
1010 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001011 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1012
1013 rqfar--;
1014 rqfcr = RQFCR_CMP_NOMATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001015 priv->ftp_rqfpr[rqfar] = rqfpr;
1016 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001017 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1018
1019 rqfar--;
1020 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
1021 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001022 priv->ftp_rqfcr[rqfar] = rqfcr;
1023 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001024 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1025
1026 rqfar--;
1027 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
1028 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001029 priv->ftp_rqfcr[rqfar] = rqfcr;
1030 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001031 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1032
1033 return rqfar;
1034}
1035
1036static void gfar_init_filer_table(struct gfar_private *priv)
1037{
1038 int i = 0x0;
1039 u32 rqfar = MAX_FILER_IDX;
1040 u32 rqfcr = 0x0;
1041 u32 rqfpr = FPR_FILER_MASK;
1042
1043 /* Default rule */
1044 rqfcr = RQFCR_CMP_MATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001045 priv->ftp_rqfcr[rqfar] = rqfcr;
1046 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001047 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1048
1049 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1050 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1051 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1052 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1053 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1054 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1055
Uwe Kleine-König85dd08e2010-06-11 12:16:55 +02001056 /* cur_filer_idx indicated the first non-masked rule */
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001057 priv->cur_filer_idx = rqfar;
1058
1059 /* Rest are masked rules */
1060 rqfcr = RQFCR_CMP_NOMATCH;
1061 for (i = 0; i < rqfar; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001062 priv->ftp_rqfcr[i] = rqfcr;
1063 priv->ftp_rqfpr[i] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001064 gfar_write_filer(priv, i, rqfcr, rqfpr);
1065 }
1066}
1067
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001068#ifdef CONFIG_PPC
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001069static void __gfar_detect_errata_83xx(struct gfar_private *priv)
Anton Vorontsov7d350972010-06-30 06:39:12 +00001070{
Anton Vorontsov7d350972010-06-30 06:39:12 +00001071 unsigned int pvr = mfspr(SPRN_PVR);
1072 unsigned int svr = mfspr(SPRN_SVR);
1073 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1074 unsigned int rev = svr & 0xffff;
1075
1076 /* MPC8313 Rev 2.0 and higher; All MPC837x */
1077 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001078 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001079 priv->errata |= GFAR_ERRATA_74;
1080
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001081 /* MPC8313 and MPC837x all rev */
1082 if ((pvr == 0x80850010 && mod == 0x80b0) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001083 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001084 priv->errata |= GFAR_ERRATA_76;
1085
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001086 /* MPC8313 Rev < 2.0 */
1087 if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
Alex Dubov4363c2f2011-03-16 17:57:13 +00001088 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001089}
1090
1091static void __gfar_detect_errata_85xx(struct gfar_private *priv)
1092{
1093 unsigned int svr = mfspr(SPRN_SVR);
1094
1095 if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
1096 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil53fad772013-10-09 20:20:42 +03001097 if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
1098 ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
1099 priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001100}
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001101#endif
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001102
1103static void gfar_detect_errata(struct gfar_private *priv)
1104{
1105 struct device *dev = &priv->ofdev->dev;
1106
1107 /* no plans to fix */
1108 priv->errata |= GFAR_ERRATA_A002;
1109
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001110#ifdef CONFIG_PPC
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001111 if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
1112 __gfar_detect_errata_85xx(priv);
1113 else /* non-mpc85xx parts, i.e. e300 core based */
1114 __gfar_detect_errata_83xx(priv);
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001115#endif
Alex Dubov4363c2f2011-03-16 17:57:13 +00001116
Anton Vorontsov7d350972010-06-30 06:39:12 +00001117 if (priv->errata)
1118 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1119 priv->errata);
1120}
1121
Claudiu Manoil08511332014-02-24 12:13:45 +02001122void gfar_mac_reset(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Claudiu Manoil20862782014-02-17 12:53:14 +02001124 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Claudiu Manoila328ac92014-02-24 12:13:42 +02001125 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001128 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Andy Flemingb98ac702009-02-04 16:38:05 -08001130 /* We need to delay at least 3 TX clocks */
Claudiu Manoila328ac92014-02-24 12:13:42 +02001131 udelay(3);
Andy Flemingb98ac702009-02-04 16:38:05 -08001132
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001133 /* the soft reset bit is not self-resetting, so we need to
1134 * clear it before resuming normal operation
1135 */
Claudiu Manoil20862782014-02-17 12:53:14 +02001136 gfar_write(&regs->maccfg1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Claudiu Manoila328ac92014-02-24 12:13:42 +02001138 udelay(3);
1139
Claudiu Manoil88302642014-02-24 12:13:43 +02001140 /* Compute rx_buff_size based on config flags */
1141 gfar_rx_buff_size_config(priv);
1142
1143 /* Initialize the max receive frame/buffer lengths */
1144 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Claudiu Manoila328ac92014-02-24 12:13:42 +02001145 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1146
1147 /* Initialize the Minimum Frame Length Register */
1148 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* Initialize MACCFG2. */
Anton Vorontsov7d350972010-06-30 06:39:12 +00001151 tempval = MACCFG2_INIT_SETTINGS;
Claudiu Manoil88302642014-02-24 12:13:43 +02001152
1153 /* If the mtu is larger than the max size for standard
1154 * ethernet frames (ie, a jumbo frame), then set maccfg2
1155 * to allow huge frames, and to check the length
1156 */
1157 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
1158 gfar_has_errata(priv, GFAR_ERRATA_74))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001159 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
Claudiu Manoil88302642014-02-24 12:13:43 +02001160
Anton Vorontsov7d350972010-06-30 06:39:12 +00001161 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Claudiu Manoila328ac92014-02-24 12:13:42 +02001163 /* Clear mac addr hash registers */
1164 gfar_write(&regs->igaddr0, 0);
1165 gfar_write(&regs->igaddr1, 0);
1166 gfar_write(&regs->igaddr2, 0);
1167 gfar_write(&regs->igaddr3, 0);
1168 gfar_write(&regs->igaddr4, 0);
1169 gfar_write(&regs->igaddr5, 0);
1170 gfar_write(&regs->igaddr6, 0);
1171 gfar_write(&regs->igaddr7, 0);
1172
1173 gfar_write(&regs->gaddr0, 0);
1174 gfar_write(&regs->gaddr1, 0);
1175 gfar_write(&regs->gaddr2, 0);
1176 gfar_write(&regs->gaddr3, 0);
1177 gfar_write(&regs->gaddr4, 0);
1178 gfar_write(&regs->gaddr5, 0);
1179 gfar_write(&regs->gaddr6, 0);
1180 gfar_write(&regs->gaddr7, 0);
1181
1182 if (priv->extended_hash)
1183 gfar_clear_exact_match(priv->ndev);
1184
1185 gfar_mac_rx_config(priv);
1186
1187 gfar_mac_tx_config(priv);
1188
1189 gfar_set_mac_address(priv->ndev);
1190
1191 gfar_set_multi(priv->ndev);
1192
1193 /* clear ievent and imask before configuring coalescing */
1194 gfar_ints_disable(priv);
1195
1196 /* Configure the coalescing support */
1197 gfar_configure_coalescing_all(priv);
1198}
1199
1200static void gfar_hw_init(struct gfar_private *priv)
1201{
1202 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1203 u32 attrs;
1204
1205 /* Stop the DMA engine now, in case it was running before
1206 * (The firmware could have used it, and left it running).
1207 */
1208 gfar_halt(priv);
1209
1210 gfar_mac_reset(priv);
1211
1212 /* Zero out the rmon mib registers if it has them */
1213 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1214 memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1215
1216 /* Mask off the CAM interrupts */
1217 gfar_write(&regs->rmon.cam1, 0xffffffff);
1218 gfar_write(&regs->rmon.cam2, 0xffffffff);
1219 }
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001222 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Claudiu Manoil34018fd2014-02-17 12:53:15 +02001224 /* Set the extraction length and index */
1225 attrs = ATTRELI_EL(priv->rx_stash_size) |
1226 ATTRELI_EI(priv->rx_stash_index);
1227
1228 gfar_write(&regs->attreli, attrs);
1229
1230 /* Start with defaults, and add stashing
1231 * depending on driver parameters
1232 */
1233 attrs = ATTR_INIT_SETTINGS;
1234
1235 if (priv->bd_stash_en)
1236 attrs |= ATTR_BDSTASH;
1237
1238 if (priv->rx_stash_size != 0)
1239 attrs |= ATTR_BUFSTASH;
1240
1241 gfar_write(&regs->attr, attrs);
1242
1243 /* FIFO configs */
1244 gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
1245 gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
1246 gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
1247
Claudiu Manoil20862782014-02-17 12:53:14 +02001248 /* Program the interrupt steering regs, only for MG devices */
1249 if (priv->num_grps > 1)
1250 gfar_write_isrg(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Xiubo Li898157e2014-06-04 16:49:16 +08001253static void gfar_init_addr_hash_table(struct gfar_private *priv)
Claudiu Manoil20862782014-02-17 12:53:14 +02001254{
1255 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001256
Andy Flemingb31a1d82008-12-16 15:29:15 -08001257 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001258 priv->extended_hash = 1;
1259 priv->hash_width = 9;
1260
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001261 priv->hash_regs[0] = &regs->igaddr0;
1262 priv->hash_regs[1] = &regs->igaddr1;
1263 priv->hash_regs[2] = &regs->igaddr2;
1264 priv->hash_regs[3] = &regs->igaddr3;
1265 priv->hash_regs[4] = &regs->igaddr4;
1266 priv->hash_regs[5] = &regs->igaddr5;
1267 priv->hash_regs[6] = &regs->igaddr6;
1268 priv->hash_regs[7] = &regs->igaddr7;
1269 priv->hash_regs[8] = &regs->gaddr0;
1270 priv->hash_regs[9] = &regs->gaddr1;
1271 priv->hash_regs[10] = &regs->gaddr2;
1272 priv->hash_regs[11] = &regs->gaddr3;
1273 priv->hash_regs[12] = &regs->gaddr4;
1274 priv->hash_regs[13] = &regs->gaddr5;
1275 priv->hash_regs[14] = &regs->gaddr6;
1276 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001277
1278 } else {
1279 priv->extended_hash = 0;
1280 priv->hash_width = 8;
1281
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001282 priv->hash_regs[0] = &regs->gaddr0;
1283 priv->hash_regs[1] = &regs->gaddr1;
1284 priv->hash_regs[2] = &regs->gaddr2;
1285 priv->hash_regs[3] = &regs->gaddr3;
1286 priv->hash_regs[4] = &regs->gaddr4;
1287 priv->hash_regs[5] = &regs->gaddr5;
1288 priv->hash_regs[6] = &regs->gaddr6;
1289 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001290 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001291}
1292
1293/* Set up the ethernet device structure, private data,
1294 * and anything else we need before we start
1295 */
1296static int gfar_probe(struct platform_device *ofdev)
1297{
1298 struct net_device *dev = NULL;
1299 struct gfar_private *priv = NULL;
1300 int err = 0, i;
1301
1302 err = gfar_of_init(ofdev, &dev);
1303
1304 if (err)
1305 return err;
1306
1307 priv = netdev_priv(dev);
1308 priv->ndev = dev;
1309 priv->ofdev = ofdev;
1310 priv->dev = &ofdev->dev;
1311 SET_NETDEV_DEV(dev, &ofdev->dev);
1312
1313 spin_lock_init(&priv->bflock);
1314 INIT_WORK(&priv->reset_task, gfar_reset_task);
1315
1316 platform_set_drvdata(ofdev, priv);
1317
1318 gfar_detect_errata(priv);
1319
Claudiu Manoil20862782014-02-17 12:53:14 +02001320 /* Set the dev->base_addr to the gfar reg region */
1321 dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
1322
1323 /* Fill in the dev structure */
1324 dev->watchdog_timeo = TX_TIMEOUT;
1325 dev->mtu = 1500;
1326 dev->netdev_ops = &gfar_netdev_ops;
1327 dev->ethtool_ops = &gfar_ethtool_ops;
1328
1329 /* Register for napi ...We are registering NAPI for each grp */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02001330 for (i = 0; i < priv->num_grps; i++) {
1331 if (priv->poll_mode == GFAR_SQ_POLLING) {
1332 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1333 gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
1334 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1335 gfar_poll_tx_sq, 2);
1336 } else {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02001337 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1338 gfar_poll_rx, GFAR_DEV_WEIGHT);
1339 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1340 gfar_poll_tx, 2);
1341 }
1342 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001343
1344 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
1345 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
1346 NETIF_F_RXCSUM;
1347 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
1348 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
1349 }
1350
1351 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
1352 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
1353 NETIF_F_HW_VLAN_CTAG_RX;
1354 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1355 }
1356
1357 gfar_init_addr_hash_table(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001358
Claudiu Manoil532c37b2014-02-17 12:53:16 +02001359 /* Insert receive time stamps into padding alignment bytes */
1360 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1361 priv->padding = 8;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001362
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001363 if (dev->features & NETIF_F_IP_CSUM ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001364 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
Wu Jiajun-B06378bee9e582012-05-21 23:00:48 +00001365 dev->needed_headroom = GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001369 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001370 for (i = 0; i < priv->num_tx_queues; i++) {
1371 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1372 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1373 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1374 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1375 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001376
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001377 for (i = 0; i < priv->num_rx_queues; i++) {
1378 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1379 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1380 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Jan Ceuleers0977f812012-06-05 03:42:12 +00001383 /* always enable rx filer */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001384 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001385 /* Enable most messages by default */
1386 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +00001387 /* use pritority h/w tx queue scheduling for single queue devices */
1388 if (priv->num_tx_queues == 1)
1389 priv->prio_sched_en = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001390
Claudiu Manoil08511332014-02-24 12:13:45 +02001391 set_bit(GFAR_DOWN, &priv->state);
1392
Claudiu Manoila328ac92014-02-24 12:13:42 +02001393 gfar_hw_init(priv);
Trent Piephod3eab822008-10-02 11:12:24 +00001394
Fabio Estevamd4c642e2014-06-03 19:55:38 -03001395 /* Carrier starts down, phylib will bring it up */
1396 netif_carrier_off(dev);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 err = register_netdev(dev);
1399
1400 if (err) {
Joe Perches59deab22011-06-14 08:57:47 +00001401 pr_err("%s: Cannot register net device, aborting\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto register_fail;
1403 }
1404
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001405 device_init_wakeup(&dev->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001406 priv->device_flags &
1407 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001408
Dai Harukic50a5d92008-12-17 16:51:32 -08001409 /* fill out IRQ number and name fields */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001410 for (i = 0; i < priv->num_grps; i++) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001411 struct gfar_priv_grp *grp = &priv->gfargrp[i];
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001412 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001413 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001414 dev->name, "_g", '0' + i, "_tx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001415 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001416 dev->name, "_g", '0' + i, "_rx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001417 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001418 dev->name, "_g", '0' + i, "_er");
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001419 } else
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001420 strcpy(gfar_irq(grp, TX)->name, dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001421 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001422
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001423 /* Initialize the filer table */
1424 gfar_init_filer_table(priv);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /* Print out the device info */
Joe Perches59deab22011-06-14 08:57:47 +00001427 netdev_info(dev, "mac: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Jan Ceuleers0977f812012-06-05 03:42:12 +00001429 /* Even more device info helps when determining which kernel
1430 * provided which set of benchmarks.
1431 */
Joe Perches59deab22011-06-14 08:57:47 +00001432 netdev_info(dev, "Running with NAPI enabled\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001433 for (i = 0; i < priv->num_rx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001434 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1435 i, priv->rx_queue[i]->rx_ring_size);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001436 for (i = 0; i < priv->num_tx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001437 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1438 i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 return 0;
1441
1442register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001443 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001444 gfar_free_rx_queues(priv);
1445 gfar_free_tx_queues(priv);
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001446 of_node_put(priv->phy_node);
1447 of_node_put(priv->tbi_node);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001448 free_gfar_dev(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Grant Likely2dc11582010-08-06 09:25:50 -06001452static int gfar_remove(struct platform_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001454 struct gfar_private *priv = platform_get_drvdata(ofdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001456 of_node_put(priv->phy_node);
1457 of_node_put(priv->tbi_node);
Grant Likelyfe192a42009-04-25 12:53:12 +00001458
David S. Millerd9d8e042009-09-06 01:41:02 -07001459 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001460 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001461 gfar_free_rx_queues(priv);
1462 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001463 free_gfar_dev(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 return 0;
1466}
1467
Scott Woodd87eb122008-07-11 18:04:45 -05001468#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001469
1470static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001471{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001472 struct gfar_private *priv = dev_get_drvdata(dev);
1473 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001474 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001475 unsigned long flags;
1476 u32 tempval;
1477
1478 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001479 (priv->device_flags &
1480 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001481
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001482 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001483
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001484 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001485
1486 local_irq_save(flags);
1487 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001488
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001489 gfar_halt_nodisable(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001490
1491 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001492 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001493
1494 tempval &= ~MACCFG1_TX_EN;
1495
1496 if (!magic_packet)
1497 tempval &= ~MACCFG1_RX_EN;
1498
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001499 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001500
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001501 unlock_tx_qs(priv);
1502 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001503
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001504 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001505
1506 if (magic_packet) {
1507 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001508 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001509
1510 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001511 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001512 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001513 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001514 } else {
1515 phy_stop(priv->phydev);
1516 }
1517 }
1518
1519 return 0;
1520}
1521
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001522static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001523{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001524 struct gfar_private *priv = dev_get_drvdata(dev);
1525 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001526 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001527 unsigned long flags;
1528 u32 tempval;
1529 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001530 (priv->device_flags &
1531 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001532
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001533 if (!netif_running(ndev)) {
1534 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001535 return 0;
1536 }
1537
1538 if (!magic_packet && priv->phydev)
1539 phy_start(priv->phydev);
1540
1541 /* Disable Magic Packet mode, in case something
1542 * else woke us up.
1543 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001544 local_irq_save(flags);
1545 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001546
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001547 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001548 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001549 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001550
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001551 gfar_start(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001552
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001553 unlock_tx_qs(priv);
1554 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001555
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001556 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001557
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001558 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001559
1560 return 0;
1561}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001562
1563static int gfar_restore(struct device *dev)
1564{
1565 struct gfar_private *priv = dev_get_drvdata(dev);
1566 struct net_device *ndev = priv->ndev;
1567
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001568 if (!netif_running(ndev)) {
1569 netif_device_attach(ndev);
1570
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001571 return 0;
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001572 }
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001573
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001574 if (gfar_init_bds(ndev)) {
1575 free_skb_resources(priv);
1576 return -ENOMEM;
1577 }
1578
Claudiu Manoila328ac92014-02-24 12:13:42 +02001579 gfar_mac_reset(priv);
1580
1581 gfar_init_tx_rx_base(priv);
1582
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001583 gfar_start(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001584
1585 priv->oldlink = 0;
1586 priv->oldspeed = 0;
1587 priv->oldduplex = -1;
1588
1589 if (priv->phydev)
1590 phy_start(priv->phydev);
1591
1592 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001593 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001594
1595 return 0;
1596}
1597
1598static struct dev_pm_ops gfar_pm_ops = {
1599 .suspend = gfar_suspend,
1600 .resume = gfar_resume,
1601 .freeze = gfar_suspend,
1602 .thaw = gfar_resume,
1603 .restore = gfar_restore,
1604};
1605
1606#define GFAR_PM_OPS (&gfar_pm_ops)
1607
Scott Woodd87eb122008-07-11 18:04:45 -05001608#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001609
1610#define GFAR_PM_OPS NULL
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001611
Scott Woodd87eb122008-07-11 18:04:45 -05001612#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001614/* Reads the controller's registers to determine what interface
1615 * connects it to the PHY.
1616 */
1617static phy_interface_t gfar_get_interface(struct net_device *dev)
1618{
1619 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001620 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001621 u32 ecntrl;
1622
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001623 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001624
1625 if (ecntrl & ECNTRL_SGMII_MODE)
1626 return PHY_INTERFACE_MODE_SGMII;
1627
1628 if (ecntrl & ECNTRL_TBI_MODE) {
1629 if (ecntrl & ECNTRL_REDUCED_MODE)
1630 return PHY_INTERFACE_MODE_RTBI;
1631 else
1632 return PHY_INTERFACE_MODE_TBI;
1633 }
1634
1635 if (ecntrl & ECNTRL_REDUCED_MODE) {
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001636 if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001637 return PHY_INTERFACE_MODE_RMII;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001638 }
Andy Fleming7132ab72007-07-11 11:43:07 -05001639 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001640 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001641
Jan Ceuleers0977f812012-06-05 03:42:12 +00001642 /* This isn't autodetected right now, so it must
Andy Fleming7132ab72007-07-11 11:43:07 -05001643 * be set by the device tree or platform code.
1644 */
1645 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1646 return PHY_INTERFACE_MODE_RGMII_ID;
1647
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001648 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001649 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001650 }
1651
Andy Flemingb31a1d82008-12-16 15:29:15 -08001652 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001653 return PHY_INTERFACE_MODE_GMII;
1654
1655 return PHY_INTERFACE_MODE_MII;
1656}
1657
1658
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001659/* Initializes driver's PHY state, and attaches to the PHY.
1660 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
1662static int init_phy(struct net_device *dev)
1663{
1664 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001665 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001666 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001667 GFAR_SUPPORTED_GBIT : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001668 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 priv->oldlink = 0;
1671 priv->oldspeed = 0;
1672 priv->oldduplex = -1;
1673
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001674 interface = gfar_get_interface(dev);
1675
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001676 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1677 interface);
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001678 if (!priv->phydev) {
1679 dev_err(&dev->dev, "could not attach to PHY\n");
1680 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Kapil Junejad3c12872007-05-11 18:25:11 -05001683 if (interface == PHY_INTERFACE_MODE_SGMII)
1684 gfar_configure_serdes(dev);
1685
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001686 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001687 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1688 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
Jan Ceuleers0977f812012-06-05 03:42:12 +00001693/* Initialize TBI PHY interface for communicating with the
Paul Gortmakerd0313582008-04-17 00:08:10 -04001694 * SERDES lynx PHY on the chip. We communicate with this PHY
1695 * through the MDIO bus on each controller, treating it as a
1696 * "normal" PHY at the address found in the TBIPA register. We assume
1697 * that the TBIPA register is valid. Either the MDIO bus code will set
1698 * it to a value that doesn't conflict with other PHYs on the bus, or the
1699 * value doesn't matter, as there are no other PHYs on the bus.
1700 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001701static void gfar_configure_serdes(struct net_device *dev)
1702{
1703 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001704 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001705
Grant Likelyfe192a42009-04-25 12:53:12 +00001706 if (!priv->tbi_node) {
1707 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1708 "device tree specify a tbi-handle\n");
1709 return;
1710 }
1711
1712 tbiphy = of_phy_find_device(priv->tbi_node);
1713 if (!tbiphy) {
1714 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001715 return;
1716 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001717
Jan Ceuleers0977f812012-06-05 03:42:12 +00001718 /* If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001719 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1720 * everything for us? Resetting it takes the link down and requires
1721 * several seconds for it to come back.
1722 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001723 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001724 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001725
Paul Gortmakerd0313582008-04-17 00:08:10 -04001726 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001727 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001728
Grant Likelyfe192a42009-04-25 12:53:12 +00001729 phy_write(tbiphy, MII_ADVERTISE,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001730 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1731 ADVERTISE_1000XPSE_ASYM);
Kapil Junejad3c12872007-05-11 18:25:11 -05001732
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001733 phy_write(tbiphy, MII_BMCR,
1734 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1735 BMCR_SPEED1000);
Kapil Junejad3c12872007-05-11 18:25:11 -05001736}
1737
Anton Vorontsov511d9342010-06-30 06:39:15 +00001738static int __gfar_is_rx_idle(struct gfar_private *priv)
1739{
1740 u32 res;
1741
Jan Ceuleers0977f812012-06-05 03:42:12 +00001742 /* Normaly TSEC should not hang on GRS commands, so we should
Anton Vorontsov511d9342010-06-30 06:39:15 +00001743 * actually wait for IEVENT_GRSC flag.
1744 */
Claudiu Manoilad3660c2013-10-09 20:20:40 +03001745 if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
Anton Vorontsov511d9342010-06-30 06:39:15 +00001746 return 0;
1747
Jan Ceuleers0977f812012-06-05 03:42:12 +00001748 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
Anton Vorontsov511d9342010-06-30 06:39:15 +00001749 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1750 * and the Rx can be safely reset.
1751 */
1752 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1753 res &= 0x7f807f80;
1754 if ((res & 0xffff) == (res >> 16))
1755 return 1;
1756
1757 return 0;
1758}
Kumar Gala0bbaf062005-06-20 10:54:21 -05001759
1760/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001761static void gfar_halt_nodisable(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001763 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 u32 tempval;
Claudiu Manoila4feee82014-10-07 10:44:34 +03001765 unsigned int timeout;
1766 int stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001768 gfar_ints_disable(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Claudiu Manoila4feee82014-10-07 10:44:34 +03001770 if (gfar_is_dma_stopped(priv))
1771 return;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001774 tempval = gfar_read(&regs->dmactrl);
Claudiu Manoila4feee82014-10-07 10:44:34 +03001775 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1776 gfar_write(&regs->dmactrl, tempval);
Anton Vorontsov511d9342010-06-30 06:39:15 +00001777
Claudiu Manoila4feee82014-10-07 10:44:34 +03001778retry:
1779 timeout = 1000;
1780 while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
1781 cpu_relax();
1782 timeout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
Claudiu Manoila4feee82014-10-07 10:44:34 +03001784
1785 if (!timeout)
1786 stopped = gfar_is_dma_stopped(priv);
1787
1788 if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
1789 !__gfar_is_rx_idle(priv))
1790 goto retry;
Scott Woodd87eb122008-07-11 18:04:45 -05001791}
Scott Woodd87eb122008-07-11 18:04:45 -05001792
1793/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001794void gfar_halt(struct gfar_private *priv)
Scott Woodd87eb122008-07-11 18:04:45 -05001795{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001796 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001797 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001799 /* Dissable the Rx/Tx hw queues */
1800 gfar_write(&regs->rqueue, 0);
1801 gfar_write(&regs->tqueue, 0);
Scott Wood2a54adc2008-08-12 15:10:46 -05001802
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001803 mdelay(10);
1804
1805 gfar_halt_nodisable(priv);
1806
1807 /* Disable Rx/Tx DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 tempval = gfar_read(&regs->maccfg1);
1809 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1810 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001811}
1812
1813void stop_gfar(struct net_device *dev)
1814{
1815 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001816
Claudiu Manoil08511332014-02-24 12:13:45 +02001817 netif_tx_stop_all_queues(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001818
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001819 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02001820 set_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001821 smp_mb__after_atomic();
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001822
Claudiu Manoil08511332014-02-24 12:13:45 +02001823 disable_napi(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001824
Claudiu Manoil08511332014-02-24 12:13:45 +02001825 /* disable ints and gracefully shut down Rx/Tx DMA */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001826 gfar_halt(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Claudiu Manoil08511332014-02-24 12:13:45 +02001828 phy_stop(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001833static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001836 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001837 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001839 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001841 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1842 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001843 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Claudiu Manoil369ec162013-02-14 05:00:02 +00001845 dma_unmap_single(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001846 txbdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08001847 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001848 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001849 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001850 txbdp++;
Claudiu Manoil369ec162013-02-14 05:00:02 +00001851 dma_unmap_page(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001852 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001854 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001855 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1856 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001858 kfree(tx_queue->tx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001859 tx_queue->tx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001860}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001862static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1863{
1864 struct rxbd8 *rxbdp;
1865 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1866 int i;
1867
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001868 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001870 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1871 if (rx_queue->rx_skbuff[i]) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00001872 dma_unmap_single(priv->dev, rxbdp->bufPtr,
1873 priv->rx_buffer_size,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001874 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001875 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1876 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001878 rxbdp->lstatus = 0;
1879 rxbdp->bufPtr = 0;
1880 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001882 kfree(rx_queue->rx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001883 rx_queue->rx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001884}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001885
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001886/* If there are any tx skbs or rx skbs still around, free them.
Jan Ceuleers0977f812012-06-05 03:42:12 +00001887 * Then free tx_skbuff and rx_skbuff
1888 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001889static void free_skb_resources(struct gfar_private *priv)
1890{
1891 struct gfar_priv_tx_q *tx_queue = NULL;
1892 struct gfar_priv_rx_q *rx_queue = NULL;
1893 int i;
1894
1895 /* Go through all the buffer descriptors and free their data buffers */
1896 for (i = 0; i < priv->num_tx_queues; i++) {
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001897 struct netdev_queue *txq;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001898
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001899 tx_queue = priv->tx_queue[i];
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001900 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001901 if (tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001902 free_skb_tx_queue(tx_queue);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001903 netdev_tx_reset_queue(txq);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001904 }
1905
1906 for (i = 0; i < priv->num_rx_queues; i++) {
1907 rx_queue = priv->rx_queue[i];
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001908 if (rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001909 free_skb_rx_queue(rx_queue);
1910 }
1911
Claudiu Manoil369ec162013-02-14 05:00:02 +00001912 dma_free_coherent(priv->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001913 sizeof(struct txbd8) * priv->total_tx_ring_size +
1914 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1915 priv->tx_queue[0]->tx_bd_base,
1916 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001919void gfar_start(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001920{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001921 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001922 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001923 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001924
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001925 /* Enable Rx/Tx hw queues */
1926 gfar_write(&regs->rqueue, priv->rqueue);
1927 gfar_write(&regs->tqueue, priv->tqueue);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001928
1929 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001930 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001931 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001932 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001933
Kumar Gala0bbaf062005-06-20 10:54:21 -05001934 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001935 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001936 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001937 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001938
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001939 for (i = 0; i < priv->num_grps; i++) {
1940 regs = priv->gfargrp[i].regs;
1941 /* Clear THLT/RHLT, so that the DMA starts polling now */
1942 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1943 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001944 }
Dai Haruki12dea572008-12-16 15:30:20 -08001945
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001946 /* Enable Rx/Tx DMA */
1947 tempval = gfar_read(&regs->maccfg1);
1948 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1949 gfar_write(&regs->maccfg1, tempval);
1950
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001951 gfar_ints_enable(priv);
1952
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001953 priv->ndev->trans_start = jiffies; /* prevent tx timeout */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001954}
1955
Claudiu Manoil80ec3962014-02-24 12:13:44 +02001956static void free_grp_irqs(struct gfar_priv_grp *grp)
1957{
1958 free_irq(gfar_irq(grp, TX)->irq, grp);
1959 free_irq(gfar_irq(grp, RX)->irq, grp);
1960 free_irq(gfar_irq(grp, ER)->irq, grp);
1961}
1962
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001963static int register_grp_irqs(struct gfar_priv_grp *grp)
1964{
1965 struct gfar_private *priv = grp->priv;
1966 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001967 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* If the device has multiple interrupts, register for
Jan Ceuleers0977f812012-06-05 03:42:12 +00001970 * them. Otherwise, only register for the one
1971 */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001972 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001973 /* Install our interrupt handlers for Error,
Jan Ceuleers0977f812012-06-05 03:42:12 +00001974 * Transmit, and Receive
1975 */
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001976 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1977 gfar_irq(grp, ER)->name, grp);
1978 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001979 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001980 gfar_irq(grp, ER)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001981
Julia Lawall2145f1a2010-08-05 10:26:20 +00001982 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001984 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1985 gfar_irq(grp, TX)->name, grp);
1986 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001987 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001988 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 goto tx_irq_fail;
1990 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001991 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1992 gfar_irq(grp, RX)->name, grp);
1993 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001994 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001995 gfar_irq(grp, RX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 goto rx_irq_fail;
1997 }
1998 } else {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001999 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
2000 gfar_irq(grp, TX)->name, grp);
2001 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002002 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002003 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 goto err_irq_fail;
2005 }
2006 }
2007
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002008 return 0;
2009
2010rx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002011 free_irq(gfar_irq(grp, TX)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002012tx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002013 free_irq(gfar_irq(grp, ER)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002014err_irq_fail:
2015 return err;
2016
2017}
2018
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002019static void gfar_free_irq(struct gfar_private *priv)
2020{
2021 int i;
2022
2023 /* Free the IRQs */
2024 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2025 for (i = 0; i < priv->num_grps; i++)
2026 free_grp_irqs(&priv->gfargrp[i]);
2027 } else {
2028 for (i = 0; i < priv->num_grps; i++)
2029 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
2030 &priv->gfargrp[i]);
2031 }
2032}
2033
2034static int gfar_request_irq(struct gfar_private *priv)
2035{
2036 int err, i, j;
2037
2038 for (i = 0; i < priv->num_grps; i++) {
2039 err = register_grp_irqs(&priv->gfargrp[i]);
2040 if (err) {
2041 for (j = 0; j < i; j++)
2042 free_grp_irqs(&priv->gfargrp[j]);
2043 return err;
2044 }
2045 }
2046
2047 return 0;
2048}
2049
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002050/* Bring the controller up and running */
2051int startup_gfar(struct net_device *ndev)
2052{
2053 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002054 int err;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002055
Claudiu Manoila328ac92014-02-24 12:13:42 +02002056 gfar_mac_reset(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002057
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002058 err = gfar_alloc_skb_resources(ndev);
2059 if (err)
2060 return err;
2061
Claudiu Manoila328ac92014-02-24 12:13:42 +02002062 gfar_init_tx_rx_base(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002063
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002064 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002065 clear_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002066 smp_mb__after_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002067
2068 /* Start Rx/Tx DMA and enable the interrupts */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02002069 gfar_start(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00002071 phy_start(priv->phydev);
2072
Claudiu Manoil08511332014-02-24 12:13:45 +02002073 enable_napi(priv);
2074
2075 netif_tx_wake_all_queues(ndev);
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Jan Ceuleers0977f812012-06-05 03:42:12 +00002080/* Called when something needs to use the ethernet device
2081 * Returns 0 for success.
2082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static int gfar_enet_open(struct net_device *dev)
2084{
Li Yang94e8cc32007-10-12 21:53:51 +08002085 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 int err;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 err = init_phy(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002089 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return err;
2091
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002092 err = gfar_request_irq(priv);
2093 if (err)
2094 return err;
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 err = startup_gfar(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002097 if (err)
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04002098 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08002100 device_set_wakeup_enable(&dev->dev, priv->wol_en);
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return err;
2103}
2104
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002105static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002106{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002107 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07002108
2109 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002110
Kumar Gala0bbaf062005-06-20 10:54:21 -05002111 return fcb;
2112}
2113
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002114static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002115 int fcb_length)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002116{
Kumar Gala0bbaf062005-06-20 10:54:21 -05002117 /* If we're here, it's a IP packet with a TCP or UDP
2118 * payload. We set it to checksum, using a pseudo-header
2119 * we provide
2120 */
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00002121 u8 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002122
Jan Ceuleers0977f812012-06-05 03:42:12 +00002123 /* Tell the controller what the protocol is
2124 * And provide the already calculated phcs
2125 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002126 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002127 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03002128 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002129 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05002130 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002131
2132 /* l3os is the distance between the start of the
2133 * frame (skb->data) and the start of the IP hdr.
2134 * l4os is the distance between the start of the
Jan Ceuleers0977f812012-06-05 03:42:12 +00002135 * l3 hdr and the l4 hdr
2136 */
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002137 fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03002138 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002139
Andy Fleming7f7f5312005-11-11 12:38:59 -06002140 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002141}
2142
Andy Fleming7f7f5312005-11-11 12:38:59 -06002143void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002144{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002145 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002146 fcb->vlctl = vlan_tx_tag_get(skb);
2147}
2148
Dai Haruki4669bc92008-12-17 16:51:04 -08002149static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002150 struct txbd8 *base, int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002151{
2152 struct txbd8 *new_bd = bdp + stride;
2153
2154 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2155}
2156
2157static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002158 int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002159{
2160 return skip_txbd(bdp, 1, base, ring_size);
2161}
2162
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002163/* eTSEC12: csum generation not supported for some fcb offsets */
2164static inline bool gfar_csum_errata_12(struct gfar_private *priv,
2165 unsigned long fcb_addr)
2166{
2167 return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
2168 (fcb_addr % 0x20) > 0x18);
2169}
2170
2171/* eTSEC76: csum generation for frames larger than 2500 may
2172 * cause excess delays before start of transmission
2173 */
2174static inline bool gfar_csum_errata_76(struct gfar_private *priv,
2175 unsigned int len)
2176{
2177 return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
2178 (len > 2500));
2179}
2180
Jan Ceuleers0977f812012-06-05 03:42:12 +00002181/* This is called by the kernel when a frame is ready for transmission.
2182 * It is pointed to by the dev->hard_start_xmit function pointer
2183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2185{
2186 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002187 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002188 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002189 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002190 struct txfcb *fcb = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002191 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
Dai Haruki5a5efed2008-12-16 15:34:50 -08002192 u32 lstatus;
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002193 int i, rq = 0;
2194 int do_tstamp, do_csum, do_vlan;
Dai Haruki4669bc92008-12-17 16:51:04 -08002195 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05002196 unsigned long flags;
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002197 unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002198
2199 rq = skb->queue_mapping;
2200 tx_queue = priv->tx_queue[rq];
2201 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002202 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002203 regs = tx_queue->grp->regs;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002204
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002205 do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2206 do_vlan = vlan_tx_tag_present(skb);
2207 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2208 priv->hwts_tx_en;
2209
2210 if (do_csum || do_vlan)
2211 fcb_len = GMAC_FCB_LEN;
2212
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002213 /* check if time stamp should be generated */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002214 if (unlikely(do_tstamp))
2215 fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Dai Haruki4669bc92008-12-17 16:51:04 -08002216
Li Yang5b28bea2009-03-27 15:54:30 -07002217 /* make space for additional header when fcb is needed */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002218 if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002219 struct sk_buff *skb_new;
2220
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002221 skb_new = skb_realloc_headroom(skb, fcb_len);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002222 if (!skb_new) {
2223 dev->stats.tx_errors++;
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002224 dev_kfree_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002225 return NETDEV_TX_OK;
2226 }
Manfred Rudigierdb83d132012-01-09 23:26:50 +00002227
Eric Dumazet313b0372012-07-05 11:45:13 +00002228 if (skb->sk)
2229 skb_set_owner_w(skb_new, skb->sk);
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002230 dev_consume_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002231 skb = skb_new;
2232 }
2233
Dai Haruki4669bc92008-12-17 16:51:04 -08002234 /* total number of fragments in the SKB */
2235 nr_frags = skb_shinfo(skb)->nr_frags;
2236
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002237 /* calculate the required number of TxBDs for this skb */
2238 if (unlikely(do_tstamp))
2239 nr_txbds = nr_frags + 2;
2240 else
2241 nr_txbds = nr_frags + 1;
2242
Dai Haruki4669bc92008-12-17 16:51:04 -08002243 /* check if there is space to queue this packet */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002244 if (nr_txbds > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002245 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002246 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08002247 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08002248 return NETDEV_TX_BUSY;
2249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 /* Update transmit stats */
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002252 bytes_sent = skb->len;
2253 tx_queue->stats.tx_bytes += bytes_sent;
2254 /* keep Tx bytes on wire for BQL accounting */
2255 GFAR_CB(skb)->bytes_sent = bytes_sent;
Eric Dumazet1ac9ad12011-01-12 12:13:14 +00002256 tx_queue->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002258 txbdp = txbdp_start = tx_queue->cur_tx;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002259 lstatus = txbdp->lstatus;
2260
2261 /* Time stamp insertion requires one additional TxBD */
2262 if (unlikely(do_tstamp))
2263 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002264 tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Dai Haruki4669bc92008-12-17 16:51:04 -08002266 if (nr_frags == 0) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002267 if (unlikely(do_tstamp))
2268 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002269 TXBD_INTERRUPT);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002270 else
2271 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
Dai Haruki4669bc92008-12-17 16:51:04 -08002272 } else {
2273 /* Place the fragment addresses and lengths into the TxBDs */
2274 for (i = 0; i < nr_frags; i++) {
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002275 unsigned int frag_len;
Dai Haruki4669bc92008-12-17 16:51:04 -08002276 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002277 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002279 frag_len = skb_shinfo(skb)->frags[i].size;
Dai Haruki4669bc92008-12-17 16:51:04 -08002280
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002281 lstatus = txbdp->lstatus | frag_len |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002282 BD_LFLAG(TXBD_READY);
Dai Haruki4669bc92008-12-17 16:51:04 -08002283
2284 /* Handle the last BD specially */
2285 if (i == nr_frags - 1)
2286 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2287
Claudiu Manoil369ec162013-02-14 05:00:02 +00002288 bufaddr = skb_frag_dma_map(priv->dev,
Ian Campbell2234a722011-08-29 23:18:29 +00002289 &skb_shinfo(skb)->frags[i],
2290 0,
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002291 frag_len,
Ian Campbell2234a722011-08-29 23:18:29 +00002292 DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002293
2294 /* set the TxBD length and buffer pointer */
2295 txbdp->bufPtr = bufaddr;
2296 txbdp->lstatus = lstatus;
2297 }
2298
2299 lstatus = txbdp_start->lstatus;
2300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002302 /* Add TxPAL between FCB and frame if required */
2303 if (unlikely(do_tstamp)) {
2304 skb_push(skb, GMAC_TXPAL_LEN);
2305 memset(skb->data, 0, GMAC_TXPAL_LEN);
2306 }
2307
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002308 /* Add TxFCB if required */
2309 if (fcb_len) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002310 fcb = gfar_add_fcb(skb);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002311 lstatus |= BD_LFLAG(TXBD_TOE);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002312 }
2313
2314 /* Set up checksumming */
2315 if (do_csum) {
2316 gfar_tx_checksum(skb, fcb, fcb_len);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002317
2318 if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
2319 unlikely(gfar_csum_errata_76(priv, skb->len))) {
Alex Dubov4363c2f2011-03-16 17:57:13 +00002320 __skb_pull(skb, GMAC_FCB_LEN);
2321 skb_checksum_help(skb);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002322 if (do_vlan || do_tstamp) {
2323 /* put back a new fcb for vlan/tstamp TOE */
2324 fcb = gfar_add_fcb(skb);
2325 } else {
2326 /* Tx TOE not used */
2327 lstatus &= ~(BD_LFLAG(TXBD_TOE));
2328 fcb = NULL;
2329 }
Alex Dubov4363c2f2011-03-16 17:57:13 +00002330 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002331 }
2332
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002333 if (do_vlan)
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002334 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002335
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002336 /* Setup tx hardware time stamping if requested */
2337 if (unlikely(do_tstamp)) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002338 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002339 fcb->ptp = 1;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002340 }
2341
Claudiu Manoil369ec162013-02-14 05:00:02 +00002342 txbdp_start->bufPtr = dma_map_single(priv->dev, skb->data,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002343 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Jan Ceuleers0977f812012-06-05 03:42:12 +00002345 /* If time stamping is requested one additional TxBD must be set up. The
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002346 * first TxBD points to the FCB and must have a data length of
2347 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2348 * the full frame length.
2349 */
2350 if (unlikely(do_tstamp)) {
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002351 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002352 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002353 (skb_headlen(skb) - fcb_len);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002354 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2355 } else {
2356 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002359 netdev_tx_sent_queue(txq, bytes_sent);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002360
Jan Ceuleers0977f812012-06-05 03:42:12 +00002361 /* We can work in parallel with gfar_clean_tx_ring(), except
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002362 * when modifying num_txbdfree. Note that we didn't grab the lock
2363 * when we were reading the num_txbdfree and checking for available
2364 * space, that's because outside of this function it can only grow,
2365 * and once we've got needed space, it cannot suddenly disappear.
2366 *
2367 * The lock also protects us from gfar_error(), which can modify
2368 * regs->tstat and thus retrigger the transfers, which is why we
2369 * also must grab the lock before setting ready bit for the first
2370 * to be transmitted BD.
2371 */
2372 spin_lock_irqsave(&tx_queue->txlock, flags);
2373
Claudiu Manoild55398b2014-10-07 10:44:35 +03002374 gfar_wmb();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002375
Dai Haruki4669bc92008-12-17 16:51:04 -08002376 txbdp_start->lstatus = lstatus;
2377
Claudiu Manoild55398b2014-10-07 10:44:35 +03002378 gfar_wmb(); /* force lstatus write before tx_skbuff */
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002379
2380 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2381
Dai Haruki4669bc92008-12-17 16:51:04 -08002382 /* Update the current skb pointer to the next entry we will use
Jan Ceuleers0977f812012-06-05 03:42:12 +00002383 * (wrapping if necessary)
2384 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002385 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002386 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002387
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002388 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002389
2390 /* reduce TxBD free count */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002391 tx_queue->num_txbdfree -= (nr_txbds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 /* If the next BD still needs to be cleaned up, then the bds
Jan Ceuleers0977f812012-06-05 03:42:12 +00002394 * are full. We need to tell the kernel to stop sending us stuff.
2395 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002396 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002397 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002399 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002403 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002406 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002408 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
2411/* Stops the kernel queue, and halts the controller */
2412static int gfar_close(struct net_device *dev)
2413{
2414 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002415
Sebastian Siewiorab939902008-08-19 21:12:45 +02002416 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 stop_gfar(dev);
2418
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002419 /* Disconnect from the PHY */
2420 phy_disconnect(priv->phydev);
2421 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002423 gfar_free_irq(priv);
2424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return 0;
2426}
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002429static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002431 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 return 0;
2434}
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2437{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002439 int frame_size = new_mtu + ETH_HLEN;
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Joe Perches59deab22011-06-14 08:57:47 +00002442 netif_err(priv, drv, dev, "Invalid MTU setting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return -EINVAL;
2444 }
2445
Claudiu Manoil08511332014-02-24 12:13:45 +02002446 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2447 cpu_relax();
2448
Claudiu Manoil88302642014-02-24 12:13:43 +02002449 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 stop_gfar(dev);
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 dev->mtu = new_mtu;
2453
Claudiu Manoil88302642014-02-24 12:13:43 +02002454 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 startup_gfar(dev);
2456
Claudiu Manoil08511332014-02-24 12:13:45 +02002457 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return 0;
2460}
2461
Claudiu Manoil08511332014-02-24 12:13:45 +02002462void reset_gfar(struct net_device *ndev)
2463{
2464 struct gfar_private *priv = netdev_priv(ndev);
2465
2466 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2467 cpu_relax();
2468
2469 stop_gfar(ndev);
2470 startup_gfar(ndev);
2471
2472 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2473}
2474
Sebastian Siewiorab939902008-08-19 21:12:45 +02002475/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 * transmitted after a set amount of time.
2477 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002478 * starting over will fix the problem.
2479 */
2480static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002482 struct gfar_private *priv = container_of(work, struct gfar_private,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002483 reset_task);
Claudiu Manoil08511332014-02-24 12:13:45 +02002484 reset_gfar(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Sebastian Siewiorab939902008-08-19 21:12:45 +02002487static void gfar_timeout(struct net_device *dev)
2488{
2489 struct gfar_private *priv = netdev_priv(dev);
2490
2491 dev->stats.tx_errors++;
2492 schedule_work(&priv->reset_task);
2493}
2494
Eran Libertyacbc0f02010-07-07 15:54:54 -07002495static void gfar_align_skb(struct sk_buff *skb)
2496{
2497 /* We need the data buffer to be aligned properly. We will reserve
2498 * as many bytes as needed to align the data properly
2499 */
2500 skb_reserve(skb, RXBUF_ALIGNMENT -
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002501 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
Eran Libertyacbc0f02010-07-07 15:54:54 -07002502}
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504/* Interrupt Handler for Transmit complete */
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002505static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002507 struct net_device *dev = tx_queue->dev;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002508 struct netdev_queue *txq;
Dai Harukid080cd62008-04-09 19:37:51 -05002509 struct gfar_private *priv = netdev_priv(dev);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002510 struct txbd8 *bdp, *next = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002511 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002512 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002513 struct sk_buff *skb;
2514 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002515 int tx_ring_size = tx_queue->tx_ring_size;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002516 int frags = 0, nr_txbds = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002517 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002518 int howmany = 0;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002519 int tqi = tx_queue->qindex;
2520 unsigned int bytes_sent = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002521 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002522 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002524 txq = netdev_get_tx_queue(dev, tqi);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002525 bdp = tx_queue->dirty_tx;
2526 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002527
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002528 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002529 unsigned long flags;
2530
Dai Haruki4669bc92008-12-17 16:51:04 -08002531 frags = skb_shinfo(skb)->nr_frags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002532
Jan Ceuleers0977f812012-06-05 03:42:12 +00002533 /* When time stamping, one additional TxBD must be freed.
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002534 * Also, we need to dma_unmap_single() the TxPAL.
2535 */
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002536 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002537 nr_txbds = frags + 2;
2538 else
2539 nr_txbds = frags + 1;
2540
2541 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002542
2543 lstatus = lbdp->lstatus;
2544
2545 /* Only clean completed frames */
2546 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002547 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 break;
2549
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002550 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002551 next = next_txbd(bdp, base, tx_ring_size);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002552 buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002553 } else
2554 buflen = bdp->length;
2555
Claudiu Manoil369ec162013-02-14 05:00:02 +00002556 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002557 buflen, DMA_TO_DEVICE);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002558
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002559 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002560 struct skb_shared_hwtstamps shhwtstamps;
2561 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002562
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002563 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2564 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002565 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002566 skb_tstamp_tx(skb, &shhwtstamps);
2567 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2568 bdp = next;
2569 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002570
2571 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2572 bdp = next_txbd(bdp, base, tx_ring_size);
2573
2574 for (i = 0; i < frags; i++) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00002575 dma_unmap_page(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002576 bdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002577 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2578 bdp = next_txbd(bdp, base, tx_ring_size);
2579 }
2580
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002581 bytes_sent += GFAR_CB(skb)->bytes_sent;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002582
Eric Dumazetacb600d2012-10-05 06:23:55 +00002583 dev_kfree_skb_any(skb);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002584
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002585 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002586
2587 skb_dirtytx = (skb_dirtytx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002588 TX_RING_MOD_MASK(tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002589
Dai Harukid080cd62008-04-09 19:37:51 -05002590 howmany++;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002591 spin_lock_irqsave(&tx_queue->txlock, flags);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002592 tx_queue->num_txbdfree += nr_txbds;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002593 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08002594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Dai Haruki4669bc92008-12-17 16:51:04 -08002596 /* If we freed a buffer, we can restart transmission, if necessary */
Claudiu Manoil08511332014-02-24 12:13:45 +02002597 if (tx_queue->num_txbdfree &&
2598 netif_tx_queue_stopped(txq) &&
2599 !(test_bit(GFAR_DOWN, &priv->state)))
2600 netif_wake_subqueue(priv->ndev, tqi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Dai Haruki4669bc92008-12-17 16:51:04 -08002602 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002603 tx_queue->skb_dirtytx = skb_dirtytx;
2604 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002606 netdev_tx_completed_queue(txq, howmany, bytes_sent);
Dai Harukid080cd62008-04-09 19:37:51 -05002607}
2608
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002609static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002610 struct sk_buff *skb)
Andy Fleming815b97c2008-04-22 17:18:29 -05002611{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002612 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002613 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002614 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002615
Claudiu Manoil369ec162013-02-14 05:00:02 +00002616 buf = dma_map_single(priv->dev, skb->data,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002617 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002618 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002619}
2620
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002621static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
Eran Libertyacbc0f02010-07-07 15:54:54 -07002622{
2623 struct gfar_private *priv = netdev_priv(dev);
Eric Dumazetacb600d2012-10-05 06:23:55 +00002624 struct sk_buff *skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002625
2626 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2627 if (!skb)
2628 return NULL;
2629
2630 gfar_align_skb(skb);
2631
2632 return skb;
2633}
Andy Fleming815b97c2008-04-22 17:18:29 -05002634
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002635struct sk_buff *gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Eric Dumazetacb600d2012-10-05 06:23:55 +00002637 return gfar_alloc_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Li Yang298e1a92007-10-16 14:18:13 +08002640static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
Li Yang298e1a92007-10-16 14:18:13 +08002642 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002643 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 struct gfar_extra_stats *estats = &priv->extra_stats;
2645
Jan Ceuleers0977f812012-06-05 03:42:12 +00002646 /* If the packet was truncated, none of the other errors matter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (status & RXBD_TRUNCATED) {
2648 stats->rx_length_errors++;
2649
Paul Gortmaker212079d2013-02-12 15:38:19 -05002650 atomic64_inc(&estats->rx_trunc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 return;
2653 }
2654 /* Count the errors, if there were any */
2655 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2656 stats->rx_length_errors++;
2657
2658 if (status & RXBD_LARGE)
Paul Gortmaker212079d2013-02-12 15:38:19 -05002659 atomic64_inc(&estats->rx_large);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 else
Paul Gortmaker212079d2013-02-12 15:38:19 -05002661 atomic64_inc(&estats->rx_short);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 if (status & RXBD_NONOCTET) {
2664 stats->rx_frame_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002665 atomic64_inc(&estats->rx_nonoctet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
2667 if (status & RXBD_CRCERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002668 atomic64_inc(&estats->rx_crcerr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 stats->rx_crc_errors++;
2670 }
2671 if (status & RXBD_OVERRUN) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002672 atomic64_inc(&estats->rx_overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 stats->rx_crc_errors++;
2674 }
2675}
2676
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002677irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002679 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2680 unsigned long flags;
2681 u32 imask;
2682
2683 if (likely(napi_schedule_prep(&grp->napi_rx))) {
2684 spin_lock_irqsave(&grp->grplock, flags);
2685 imask = gfar_read(&grp->regs->imask);
2686 imask &= IMASK_RX_DISABLED;
2687 gfar_write(&grp->regs->imask, imask);
2688 spin_unlock_irqrestore(&grp->grplock, flags);
2689 __napi_schedule(&grp->napi_rx);
2690 } else {
2691 /* Clear IEVENT, so interrupts aren't called again
2692 * because of the packets that have already arrived.
2693 */
2694 gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2695 }
2696
2697 return IRQ_HANDLED;
2698}
2699
2700/* Interrupt Handler for Transmit complete */
2701static irqreturn_t gfar_transmit(int irq, void *grp_id)
2702{
2703 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2704 unsigned long flags;
2705 u32 imask;
2706
2707 if (likely(napi_schedule_prep(&grp->napi_tx))) {
2708 spin_lock_irqsave(&grp->grplock, flags);
2709 imask = gfar_read(&grp->regs->imask);
2710 imask &= IMASK_TX_DISABLED;
2711 gfar_write(&grp->regs->imask, imask);
2712 spin_unlock_irqrestore(&grp->grplock, flags);
2713 __napi_schedule(&grp->napi_tx);
2714 } else {
2715 /* Clear IEVENT, so interrupts aren't called again
2716 * because of the packets that have already arrived.
2717 */
2718 gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2719 }
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return IRQ_HANDLED;
2722}
2723
Kumar Gala0bbaf062005-06-20 10:54:21 -05002724static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2725{
2726 /* If valid headers were found, and valid sums
2727 * were verified, then we tell the kernel that no
Jan Ceuleers0977f812012-06-05 03:42:12 +00002728 * checksumming is necessary. Otherwise, it is [FIXME]
2729 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002730 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002731 skb->ip_summed = CHECKSUM_UNNECESSARY;
2732 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002733 skb_checksum_none_assert(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002734}
2735
2736
Jan Ceuleers0977f812012-06-05 03:42:12 +00002737/* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
Claudiu Manoil61db26c2013-02-14 05:00:05 +00002738static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2739 int amount_pull, struct napi_struct *napi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
2741 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002742 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Dai Haruki2c2db482008-12-16 15:31:15 -08002744 /* fcb is at the beginning if exists */
2745 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Jan Ceuleers0977f812012-06-05 03:42:12 +00002747 /* Remove the FCB from the skb
2748 * Remove the padded bytes, if there are any
2749 */
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002750 if (amount_pull) {
2751 skb_record_rx_queue(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002752 skb_pull(skb, amount_pull);
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002753 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002754
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002755 /* Get receive timestamp from the skb */
2756 if (priv->hwts_rx_en) {
2757 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2758 u64 *ns = (u64 *) skb->data;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002759
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002760 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2761 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2762 }
2763
2764 if (priv->padding)
2765 skb_pull(skb, priv->padding);
2766
Michał Mirosław8b3afe92011-04-15 04:50:50 +00002767 if (dev->features & NETIF_F_RXCSUM)
Dai Haruki2c2db482008-12-16 15:31:15 -08002768 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002769
Dai Haruki2c2db482008-12-16 15:31:15 -08002770 /* Tell the skb what kind of packet this is */
2771 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002772
Patrick McHardyf6469682013-04-19 02:04:27 +00002773 /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
David S. Miller823dcd22011-08-20 10:39:12 -07002774 * Even if vlan rx accel is disabled, on some chips
2775 * RXFCB_VLN is pseudo randomly set.
2776 */
Patrick McHardyf6469682013-04-19 02:04:27 +00002777 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
David S. Miller823dcd22011-08-20 10:39:12 -07002778 fcb->flags & RXFCB_VLN)
David S. Millere5905c82013-04-22 19:24:19 -04002779 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
Jiri Pirko87c288c2011-07-20 04:54:19 +00002780
Dai Haruki2c2db482008-12-16 15:31:15 -08002781 /* Send the packet up the stack */
Claudiu Manoil953d2762013-03-21 03:12:15 +00002782 napi_gro_receive(napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002787 * until the budget/quota has been reached. Returns the number
2788 * of frames handled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002790int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002792 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002793 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002795 int pkt_len;
2796 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 int howmany = 0;
2798 struct gfar_private *priv = netdev_priv(dev);
2799
2800 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002801 bdp = rx_queue->cur_rx;
2802 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Claudiu Manoilba779712013-02-14 05:00:07 +00002804 amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
Dai Haruki2c2db482008-12-16 15:31:15 -08002805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002807 struct sk_buff *newskb;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002808
Scott Wood3b6330c2007-05-16 15:06:59 -05002809 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002810
2811 /* Add another skb for the future */
2812 newskb = gfar_new_skb(dev);
2813
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002814 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Claudiu Manoil369ec162013-02-14 05:00:02 +00002816 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002817 priv->rx_buffer_size, DMA_FROM_DEVICE);
Andy Fleming81183052008-11-12 10:07:11 -06002818
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002819 if (unlikely(!(bdp->status & RXBD_ERR) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002820 bdp->length > priv->rx_buffer_size))
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002821 bdp->status = RXBD_LARGE;
2822
Andy Fleming815b97c2008-04-22 17:18:29 -05002823 /* We drop the frame if we failed to allocate a new buffer */
2824 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002825 bdp->status & RXBD_ERR)) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002826 count_errors(bdp->status, dev);
2827
2828 if (unlikely(!newskb))
2829 newskb = skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002830 else if (skb)
Eric Dumazetacb600d2012-10-05 06:23:55 +00002831 dev_kfree_skb(skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05002832 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002834 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 howmany++;
2836
Dai Haruki2c2db482008-12-16 15:31:15 -08002837 if (likely(skb)) {
2838 pkt_len = bdp->length - ETH_FCS_LEN;
2839 /* Remove the FCS from the packet length */
2840 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002841 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002842 skb_record_rx_queue(skb, rx_queue->qindex);
Wu Jiajun-B06378cd754a52012-04-19 22:54:35 +00002843 gfar_process_frame(dev, skb, amount_pull,
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002844 &rx_queue->grp->napi_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Dai Haruki2c2db482008-12-16 15:31:15 -08002846 } else {
Joe Perches59deab22011-06-14 08:57:47 +00002847 netif_warn(priv, rx_err, dev, "Missing skb!\n");
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002848 rx_queue->stats.rx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002849 atomic64_inc(&priv->extra_stats.rx_skbmissing);
Dai Haruki2c2db482008-12-16 15:31:15 -08002850 }
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002854 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Andy Fleming815b97c2008-04-22 17:18:29 -05002856 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002857 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002860 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 /* update to point at the next skb */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002863 rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2864 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
2866
2867 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002868 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return howmany;
2871}
2872
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002873static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002874{
2875 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002876 container_of(napi, struct gfar_priv_grp, napi_rx);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002877 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002878 struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002879 int work_done = 0;
2880
2881 /* Clear IEVENT, so interrupts aren't called again
2882 * because of the packets that have already arrived
2883 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002884 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002885
2886 work_done = gfar_clean_rx_ring(rx_queue, budget);
2887
2888 if (work_done < budget) {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002889 u32 imask;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002890 napi_complete(napi);
2891 /* Clear the halt bit in RSTAT */
2892 gfar_write(&regs->rstat, gfargrp->rstat);
2893
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002894 spin_lock_irq(&gfargrp->grplock);
2895 imask = gfar_read(&regs->imask);
2896 imask |= IMASK_RX_DEFAULT;
2897 gfar_write(&regs->imask, imask);
2898 spin_unlock_irq(&gfargrp->grplock);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002899 }
2900
2901 return work_done;
2902}
2903
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002904static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002906 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002907 container_of(napi, struct gfar_priv_grp, napi_tx);
2908 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002909 struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002910 u32 imask;
2911
2912 /* Clear IEVENT, so interrupts aren't called again
2913 * because of the packets that have already arrived
2914 */
2915 gfar_write(&regs->ievent, IEVENT_TX_MASK);
2916
2917 /* run Tx cleanup to completion */
2918 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
2919 gfar_clean_tx_ring(tx_queue);
2920
2921 napi_complete(napi);
2922
2923 spin_lock_irq(&gfargrp->grplock);
2924 imask = gfar_read(&regs->imask);
2925 imask |= IMASK_TX_DEFAULT;
2926 gfar_write(&regs->imask, imask);
2927 spin_unlock_irq(&gfargrp->grplock);
2928
2929 return 0;
2930}
2931
2932static int gfar_poll_rx(struct napi_struct *napi, int budget)
2933{
2934 struct gfar_priv_grp *gfargrp =
2935 container_of(napi, struct gfar_priv_grp, napi_rx);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002936 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002937 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002938 struct gfar_priv_rx_q *rx_queue = NULL;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002939 int work_done = 0, work_done_per_q = 0;
Claudiu Manoil39c0a0d2013-03-21 03:12:13 +00002940 int i, budget_per_q = 0;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002941 unsigned long rstat_rxf;
2942 int num_act_queues;
Dai Harukid080cd62008-04-09 19:37:51 -05002943
Dai Haruki8c7396a2008-12-17 16:52:00 -08002944 /* Clear IEVENT, so interrupts aren't called again
Jan Ceuleers0977f812012-06-05 03:42:12 +00002945 * because of the packets that have already arrived
2946 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002947 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002948
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002949 rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
2950
2951 num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
2952 if (num_act_queues)
2953 budget_per_q = budget/num_act_queues;
2954
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002955 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2956 /* skip queue if not active */
2957 if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
2958 continue;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002959
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002960 rx_queue = priv->rx_queue[i];
2961 work_done_per_q =
2962 gfar_clean_rx_ring(rx_queue, budget_per_q);
2963 work_done += work_done_per_q;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002964
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002965 /* finished processing this queue */
2966 if (work_done_per_q < budget_per_q) {
2967 /* clear active queue hw indication */
2968 gfar_write(&regs->rstat,
2969 RSTAT_CLEAR_RXF0 >> i);
2970 num_act_queues--;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002971
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002972 if (!num_act_queues)
2973 break;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002974 }
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002975 }
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002976
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002977 if (!num_act_queues) {
2978 u32 imask;
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002979 napi_complete(napi);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002980
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002981 /* Clear the halt bit in RSTAT */
2982 gfar_write(&regs->rstat, gfargrp->rstat);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002983
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002984 spin_lock_irq(&gfargrp->grplock);
2985 imask = gfar_read(&regs->imask);
2986 imask |= IMASK_RX_DEFAULT;
2987 gfar_write(&regs->imask, imask);
2988 spin_unlock_irq(&gfargrp->grplock);
Dai Harukid080cd62008-04-09 19:37:51 -05002989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002991 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002994static int gfar_poll_tx(struct napi_struct *napi, int budget)
2995{
2996 struct gfar_priv_grp *gfargrp =
2997 container_of(napi, struct gfar_priv_grp, napi_tx);
2998 struct gfar_private *priv = gfargrp->priv;
2999 struct gfar __iomem *regs = gfargrp->regs;
3000 struct gfar_priv_tx_q *tx_queue = NULL;
3001 int has_tx_work = 0;
3002 int i;
3003
3004 /* Clear IEVENT, so interrupts aren't called again
3005 * because of the packets that have already arrived
3006 */
3007 gfar_write(&regs->ievent, IEVENT_TX_MASK);
3008
3009 for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
3010 tx_queue = priv->tx_queue[i];
3011 /* run Tx cleanup to completion */
3012 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
3013 gfar_clean_tx_ring(tx_queue);
3014 has_tx_work = 1;
3015 }
3016 }
3017
3018 if (!has_tx_work) {
3019 u32 imask;
3020 napi_complete(napi);
3021
3022 spin_lock_irq(&gfargrp->grplock);
3023 imask = gfar_read(&regs->imask);
3024 imask |= IMASK_TX_DEFAULT;
3025 gfar_write(&regs->imask, imask);
3026 spin_unlock_irq(&gfargrp->grplock);
3027 }
3028
3029 return 0;
3030}
3031
3032
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003033#ifdef CONFIG_NET_POLL_CONTROLLER
Jan Ceuleers0977f812012-06-05 03:42:12 +00003034/* Polling 'interrupt' - used by things like netconsole to send skbs
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003035 * without having to re-enable interrupts. It's not called while
3036 * the interrupt routine is executing.
3037 */
3038static void gfar_netpoll(struct net_device *dev)
3039{
3040 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00003041 int i;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003042
3043 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003044 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003045 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003046 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3047
3048 disable_irq(gfar_irq(grp, TX)->irq);
3049 disable_irq(gfar_irq(grp, RX)->irq);
3050 disable_irq(gfar_irq(grp, ER)->irq);
3051 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3052 enable_irq(gfar_irq(grp, ER)->irq);
3053 enable_irq(gfar_irq(grp, RX)->irq);
3054 enable_irq(gfar_irq(grp, TX)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003055 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003056 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003057 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003058 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3059
3060 disable_irq(gfar_irq(grp, TX)->irq);
3061 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3062 enable_irq(gfar_irq(grp, TX)->irq);
Anton Vorontsov43de0042009-12-09 02:52:19 -08003063 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003064 }
3065}
3066#endif
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003069static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003071 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003074 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003077 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003078 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003081 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003082 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003084 /* Check for errors */
3085 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003086 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 return IRQ_HANDLED;
3089}
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091/* Called every time the controller might need to be made
3092 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003093 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 * function converts those variables into the appropriate
3095 * register values, and can bring down the device if needed.
3096 */
3097static void adjust_link(struct net_device *dev)
3098{
3099 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003100 struct phy_device *phydev = priv->phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003102 if (unlikely(phydev->link != priv->oldlink ||
3103 phydev->duplex != priv->oldduplex ||
3104 phydev->speed != priv->oldspeed))
3105 gfar_update_link_state(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108/* Update the hash table based on the current list of multicast
3109 * addresses we subscribe to. Also, change the promiscuity of
3110 * the device based on the flags (this function is called
Jan Ceuleers0977f812012-06-05 03:42:12 +00003111 * whenever dev->flags is changed
3112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113static void gfar_set_multi(struct net_device *dev)
3114{
Jiri Pirko22bedad32010-04-01 21:22:57 +00003115 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003117 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 u32 tempval;
3119
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003120 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 /* Set RCTRL to PROM */
3122 tempval = gfar_read(&regs->rctrl);
3123 tempval |= RCTRL_PROM;
3124 gfar_write(&regs->rctrl, tempval);
3125 } else {
3126 /* Set RCTRL to not PROM */
3127 tempval = gfar_read(&regs->rctrl);
3128 tempval &= ~(RCTRL_PROM);
3129 gfar_write(&regs->rctrl, tempval);
3130 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003131
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003132 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003134 gfar_write(&regs->igaddr0, 0xffffffff);
3135 gfar_write(&regs->igaddr1, 0xffffffff);
3136 gfar_write(&regs->igaddr2, 0xffffffff);
3137 gfar_write(&regs->igaddr3, 0xffffffff);
3138 gfar_write(&regs->igaddr4, 0xffffffff);
3139 gfar_write(&regs->igaddr5, 0xffffffff);
3140 gfar_write(&regs->igaddr6, 0xffffffff);
3141 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 gfar_write(&regs->gaddr0, 0xffffffff);
3143 gfar_write(&regs->gaddr1, 0xffffffff);
3144 gfar_write(&regs->gaddr2, 0xffffffff);
3145 gfar_write(&regs->gaddr3, 0xffffffff);
3146 gfar_write(&regs->gaddr4, 0xffffffff);
3147 gfar_write(&regs->gaddr5, 0xffffffff);
3148 gfar_write(&regs->gaddr6, 0xffffffff);
3149 gfar_write(&regs->gaddr7, 0xffffffff);
3150 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003151 int em_num;
3152 int idx;
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003155 gfar_write(&regs->igaddr0, 0x0);
3156 gfar_write(&regs->igaddr1, 0x0);
3157 gfar_write(&regs->igaddr2, 0x0);
3158 gfar_write(&regs->igaddr3, 0x0);
3159 gfar_write(&regs->igaddr4, 0x0);
3160 gfar_write(&regs->igaddr5, 0x0);
3161 gfar_write(&regs->igaddr6, 0x0);
3162 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 gfar_write(&regs->gaddr0, 0x0);
3164 gfar_write(&regs->gaddr1, 0x0);
3165 gfar_write(&regs->gaddr2, 0x0);
3166 gfar_write(&regs->gaddr3, 0x0);
3167 gfar_write(&regs->gaddr4, 0x0);
3168 gfar_write(&regs->gaddr5, 0x0);
3169 gfar_write(&regs->gaddr6, 0x0);
3170 gfar_write(&regs->gaddr7, 0x0);
3171
Andy Fleming7f7f5312005-11-11 12:38:59 -06003172 /* If we have extended hash tables, we need to
3173 * clear the exact match registers to prepare for
Jan Ceuleers0977f812012-06-05 03:42:12 +00003174 * setting them
3175 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003176 if (priv->extended_hash) {
3177 em_num = GFAR_EM_NUM + 1;
3178 gfar_clear_exact_match(dev);
3179 idx = 1;
3180 } else {
3181 idx = 0;
3182 em_num = 0;
3183 }
3184
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003185 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return;
3187
3188 /* Parse the list, and set the appropriate bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003189 netdev_for_each_mc_addr(ha, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003190 if (idx < em_num) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00003191 gfar_set_mac_for_addr(dev, idx, ha->addr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003192 idx++;
3193 } else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003194 gfar_set_hash_for_addr(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Andy Fleming7f7f5312005-11-11 12:38:59 -06003199
3200/* Clears each of the exact match registers to zero, so they
Jan Ceuleers0977f812012-06-05 03:42:12 +00003201 * don't interfere with normal reception
3202 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003203static void gfar_clear_exact_match(struct net_device *dev)
3204{
3205 int idx;
Joe Perches6a3c9102011-11-16 09:38:02 +00003206 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
Andy Fleming7f7f5312005-11-11 12:38:59 -06003207
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003208 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
Joe Perchesb6bc7652010-12-21 02:16:08 -08003209 gfar_set_mac_for_addr(dev, idx, zero_arr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003210}
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212/* Set the appropriate hash bit for the given addr */
3213/* The algorithm works like so:
3214 * 1) Take the Destination Address (ie the multicast address), and
3215 * do a CRC on it (little endian), and reverse the bits of the
3216 * result.
3217 * 2) Use the 8 most significant bits as a hash into a 256-entry
3218 * table. The table is controlled through 8 32-bit registers:
3219 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3220 * gaddr7. This means that the 3 most significant bits in the
3221 * hash index which gaddr register to use, and the 5 other bits
3222 * indicate which bit (assuming an IBM numbering scheme, which
3223 * for PowerPC (tm) is usually the case) in the register holds
Jan Ceuleers0977f812012-06-05 03:42:12 +00003224 * the entry.
3225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3227{
3228 u32 tempval;
3229 struct gfar_private *priv = netdev_priv(dev);
Joe Perches6a3c9102011-11-16 09:38:02 +00003230 u32 result = ether_crc(ETH_ALEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05003231 int width = priv->hash_width;
3232 u8 whichbit = (result >> (32 - width)) & 0x1f;
3233 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 u32 value = (1 << (31-whichbit));
3235
Kumar Gala0bbaf062005-06-20 10:54:21 -05003236 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003238 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Andy Fleming7f7f5312005-11-11 12:38:59 -06003241
3242/* There are multiple MAC Address register pairs on some controllers
3243 * This function sets the numth pair to a given address
3244 */
Joe Perchesb6bc7652010-12-21 02:16:08 -08003245static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3246 const u8 *addr)
Andy Fleming7f7f5312005-11-11 12:38:59 -06003247{
3248 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003249 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003250 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003251 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003252
3253 macptr += num*2;
3254
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003255 /* For a station address of 0x12345678ABCD in transmission
3256 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
3257 * MACnADDR2 is set to 0x34120000.
Jan Ceuleers0977f812012-06-05 03:42:12 +00003258 */
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003259 tempval = (addr[5] << 24) | (addr[4] << 16) |
3260 (addr[3] << 8) | addr[2];
Andy Fleming7f7f5312005-11-11 12:38:59 -06003261
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003262 gfar_write(macptr, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003263
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003264 tempval = (addr[1] << 24) | (addr[0] << 16);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003265
3266 gfar_write(macptr+1, tempval);
3267}
3268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003270static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003272 struct gfar_priv_grp *gfargrp = grp_id;
3273 struct gfar __iomem *regs = gfargrp->regs;
3274 struct gfar_private *priv= gfargrp->priv;
3275 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003278 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003281 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05003282
3283 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003284 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05003285 (events & IEVENT_MAG))
3286 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003289 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003290 netdev_dbg(dev,
3291 "error interrupt (ievent=0x%08x imask=0x%08x)\n",
Joe Perches59deab22011-06-14 08:57:47 +00003292 events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 /* Update the error counters */
3295 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003296 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003299 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003301 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (events & IEVENT_XFUN) {
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003303 unsigned long flags;
3304
Joe Perches59deab22011-06-14 08:57:47 +00003305 netif_dbg(priv, tx_err, dev,
3306 "TX FIFO underrun, packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003307 dev->stats.tx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003308 atomic64_inc(&priv->extra_stats.tx_underrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003310 local_irq_save(flags);
3311 lock_tx_qs(priv);
3312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003314 gfar_write(&regs->tstat, gfargrp->tstat);
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003315
3316 unlock_tx_qs(priv);
3317 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
Joe Perches59deab22011-06-14 08:57:47 +00003319 netif_dbg(priv, tx_err, dev, "Transmit Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003322 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003323 atomic64_inc(&priv->extra_stats.rx_bsy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003325 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Joe Perches59deab22011-06-14 08:57:47 +00003327 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3328 gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
3330 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003331 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003332 atomic64_inc(&priv->extra_stats.rx_babr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Joe Perches59deab22011-06-14 08:57:47 +00003334 netif_dbg(priv, rx_err, dev, "babbling RX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
3336 if (events & IEVENT_EBERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003337 atomic64_inc(&priv->extra_stats.eberr);
Joe Perches59deab22011-06-14 08:57:47 +00003338 netif_dbg(priv, rx_err, dev, "bus error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
Joe Perches59deab22011-06-14 08:57:47 +00003340 if (events & IEVENT_RXC)
3341 netif_dbg(priv, rx_status, dev, "control frame\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 if (events & IEVENT_BABT) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003344 atomic64_inc(&priv->extra_stats.tx_babt);
Joe Perches59deab22011-06-14 08:57:47 +00003345 netif_dbg(priv, tx_err, dev, "babbling TX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
3347 return IRQ_HANDLED;
3348}
3349
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003350static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
3351{
3352 struct phy_device *phydev = priv->phydev;
3353 u32 val = 0;
3354
3355 if (!phydev->duplex)
3356 return val;
3357
3358 if (!priv->pause_aneg_en) {
3359 if (priv->tx_pause_en)
3360 val |= MACCFG1_TX_FLOW;
3361 if (priv->rx_pause_en)
3362 val |= MACCFG1_RX_FLOW;
3363 } else {
3364 u16 lcl_adv, rmt_adv;
3365 u8 flowctrl;
3366 /* get link partner capabilities */
3367 rmt_adv = 0;
3368 if (phydev->pause)
3369 rmt_adv = LPA_PAUSE_CAP;
3370 if (phydev->asym_pause)
3371 rmt_adv |= LPA_PAUSE_ASYM;
3372
3373 lcl_adv = mii_advertise_flowctrl(phydev->advertising);
3374
3375 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
3376 if (flowctrl & FLOW_CTRL_TX)
3377 val |= MACCFG1_TX_FLOW;
3378 if (flowctrl & FLOW_CTRL_RX)
3379 val |= MACCFG1_RX_FLOW;
3380 }
3381
3382 return val;
3383}
3384
3385static noinline void gfar_update_link_state(struct gfar_private *priv)
3386{
3387 struct gfar __iomem *regs = priv->gfargrp[0].regs;
3388 struct phy_device *phydev = priv->phydev;
3389
3390 if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
3391 return;
3392
3393 if (phydev->link) {
3394 u32 tempval1 = gfar_read(&regs->maccfg1);
3395 u32 tempval = gfar_read(&regs->maccfg2);
3396 u32 ecntrl = gfar_read(&regs->ecntrl);
3397
3398 if (phydev->duplex != priv->oldduplex) {
3399 if (!(phydev->duplex))
3400 tempval &= ~(MACCFG2_FULL_DUPLEX);
3401 else
3402 tempval |= MACCFG2_FULL_DUPLEX;
3403
3404 priv->oldduplex = phydev->duplex;
3405 }
3406
3407 if (phydev->speed != priv->oldspeed) {
3408 switch (phydev->speed) {
3409 case 1000:
3410 tempval =
3411 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
3412
3413 ecntrl &= ~(ECNTRL_R100);
3414 break;
3415 case 100:
3416 case 10:
3417 tempval =
3418 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
3419
3420 /* Reduced mode distinguishes
3421 * between 10 and 100
3422 */
3423 if (phydev->speed == SPEED_100)
3424 ecntrl |= ECNTRL_R100;
3425 else
3426 ecntrl &= ~(ECNTRL_R100);
3427 break;
3428 default:
3429 netif_warn(priv, link, priv->ndev,
3430 "Ack! Speed (%d) is not 10/100/1000!\n",
3431 phydev->speed);
3432 break;
3433 }
3434
3435 priv->oldspeed = phydev->speed;
3436 }
3437
3438 tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
3439 tempval1 |= gfar_get_flowctrl_cfg(priv);
3440
3441 gfar_write(&regs->maccfg1, tempval1);
3442 gfar_write(&regs->maccfg2, tempval);
3443 gfar_write(&regs->ecntrl, ecntrl);
3444
3445 if (!priv->oldlink)
3446 priv->oldlink = 1;
3447
3448 } else if (priv->oldlink) {
3449 priv->oldlink = 0;
3450 priv->oldspeed = 0;
3451 priv->oldduplex = -1;
3452 }
3453
3454 if (netif_msg_link(priv))
3455 phy_print_status(phydev);
3456}
3457
Andy Flemingb31a1d82008-12-16 15:29:15 -08003458static struct of_device_id gfar_match[] =
3459{
3460 {
3461 .type = "network",
3462 .compatible = "gianfar",
3463 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003464 {
3465 .compatible = "fsl,etsec2",
3466 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003467 {},
3468};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003469MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471/* Structure for a device driver */
Grant Likely74888762011-02-22 21:05:51 -07003472static struct platform_driver gfar_driver = {
Grant Likely40182942010-04-13 16:13:02 -07003473 .driver = {
3474 .name = "fsl-gianfar",
3475 .owner = THIS_MODULE,
3476 .pm = GFAR_PM_OPS,
3477 .of_match_table = gfar_match,
3478 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 .probe = gfar_probe,
3480 .remove = gfar_remove,
3481};
3482
Axel Lindb62f682011-11-27 16:44:17 +00003483module_platform_driver(gfar_driver);