blob: a488105ec51d0ad43a2014a2309cde7a17a62626 [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>
Anton Vorontsov7d350972010-06-30 06:39:12 +000091#include <asm/reg.h>
Claudiu Manoil2969b1f2013-10-09 20:20:41 +030092#include <asm/mpc85xx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <asm/irq.h>
94#include <asm/uaccess.h>
95#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/dma-mapping.h>
97#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040098#include <linux/mii.h>
99#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -0800100#include <linux/phy_fixed.h>
101#include <linux/of.h>
David Daney4b6ba8a2010-10-26 15:07:13 -0700102#include <linux/of_net.h>
Claudiu Manoilfd31a952014-10-07 10:44:31 +0300103#include <linux/of_address.h>
104#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#include "gianfar.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Andy Fleming7f7f5312005-11-11 12:38:59 -0600110const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int gfar_enet_open(struct net_device *dev);
113static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200114static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static void gfar_timeout(struct net_device *dev);
116static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500117struct sk_buff *gfar_new_skb(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000118static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000119 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static int gfar_set_mac_address(struct net_device *dev);
121static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100122static irqreturn_t gfar_error(int irq, void *dev_id);
123static irqreturn_t gfar_transmit(int irq, void *dev_id);
124static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void adjust_link(struct net_device *dev);
Claudiu Manoil6ce29b02014-04-30 14:27:21 +0300126static noinline void gfar_update_link_state(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int init_phy(struct net_device *dev);
Grant Likely74888762011-02-22 21:05:51 -0700128static int gfar_probe(struct platform_device *ofdev);
Grant Likely2dc11582010-08-06 09:25:50 -0600129static int gfar_remove(struct platform_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400130static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void gfar_set_multi(struct net_device *dev);
132static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500133static void gfar_configure_serdes(struct net_device *dev);
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200134static int gfar_poll_rx(struct napi_struct *napi, int budget);
135static int gfar_poll_tx(struct napi_struct *napi, int budget);
136static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
137static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300138#ifdef CONFIG_NET_POLL_CONTROLLER
139static void gfar_netpoll(struct net_device *dev);
140#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000141int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
Claudiu Manoilc233cf402013-03-19 07:40:02 +0000142static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Claudiu Manoil61db26c2013-02-14 05:00:05 +0000143static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
144 int amount_pull, struct napi_struct *napi);
Claudiu Manoilc10650b2014-02-17 12:53:18 +0200145static void gfar_halt_nodisable(struct gfar_private *priv);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600146static void gfar_clear_exact_match(struct net_device *dev);
Joe Perchesb6bc7652010-12-21 02:16:08 -0800147static void gfar_set_mac_for_addr(struct net_device *dev, int num,
148 const u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000149static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151MODULE_AUTHOR("Freescale Semiconductor, Inc");
152MODULE_DESCRIPTION("Gianfar Ethernet Driver");
153MODULE_LICENSE("GPL");
154
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000155static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000156 dma_addr_t buf)
157{
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000158 u32 lstatus;
159
160 bdp->bufPtr = buf;
161
162 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000163 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000164 lstatus |= BD_LFLAG(RXBD_WRAP);
165
166 eieio();
167
168 bdp->lstatus = lstatus;
169}
170
Anton Vorontsov87283272009-10-12 06:00:39 +0000171static int gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000172{
Anton Vorontsov87283272009-10-12 06:00:39 +0000173 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000174 struct gfar_priv_tx_q *tx_queue = NULL;
175 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000176 struct txbd8 *txbdp;
177 struct rxbd8 *rxbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000178 int i, j;
Anton Vorontsov87283272009-10-12 06:00:39 +0000179
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000180 for (i = 0; i < priv->num_tx_queues; i++) {
181 tx_queue = priv->tx_queue[i];
182 /* Initialize some variables in our dev structure */
183 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
184 tx_queue->dirty_tx = tx_queue->tx_bd_base;
185 tx_queue->cur_tx = tx_queue->tx_bd_base;
186 tx_queue->skb_curtx = 0;
187 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000188
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000189 /* Initialize Transmit Descriptor Ring */
190 txbdp = tx_queue->tx_bd_base;
191 for (j = 0; j < tx_queue->tx_ring_size; j++) {
192 txbdp->lstatus = 0;
193 txbdp->bufPtr = 0;
194 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000195 }
196
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000197 /* Set the last descriptor in the ring to indicate wrap */
198 txbdp--;
199 txbdp->status |= TXBD_WRAP;
200 }
201
202 for (i = 0; i < priv->num_rx_queues; i++) {
203 rx_queue = priv->rx_queue[i];
204 rx_queue->cur_rx = rx_queue->rx_bd_base;
205 rx_queue->skb_currx = 0;
206 rxbdp = rx_queue->rx_bd_base;
207
208 for (j = 0; j < rx_queue->rx_ring_size; j++) {
209 struct sk_buff *skb = rx_queue->rx_skbuff[j];
210
211 if (skb) {
212 gfar_init_rxbdp(rx_queue, rxbdp,
213 rxbdp->bufPtr);
214 } else {
215 skb = gfar_new_skb(ndev);
216 if (!skb) {
Joe Perches59deab22011-06-14 08:57:47 +0000217 netdev_err(ndev, "Can't allocate RX buffers\n");
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +0000218 return -ENOMEM;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000219 }
220 rx_queue->rx_skbuff[j] = skb;
221
222 gfar_new_rxbdp(rx_queue, rxbdp, skb);
223 }
224
225 rxbdp++;
226 }
227
Anton Vorontsov87283272009-10-12 06:00:39 +0000228 }
229
230 return 0;
231}
232
233static int gfar_alloc_skb_resources(struct net_device *ndev)
234{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000235 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000236 dma_addr_t addr;
237 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000238 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil369ec162013-02-14 05:00:02 +0000239 struct device *dev = priv->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000240 struct gfar_priv_tx_q *tx_queue = NULL;
241 struct gfar_priv_rx_q *rx_queue = NULL;
242
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000243 priv->total_tx_ring_size = 0;
244 for (i = 0; i < priv->num_tx_queues; i++)
245 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
246
247 priv->total_rx_ring_size = 0;
248 for (i = 0; i < priv->num_rx_queues; i++)
249 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000250
251 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000252 vaddr = dma_alloc_coherent(dev,
Joe Perchesd0320f72013-03-14 13:07:21 +0000253 (priv->total_tx_ring_size *
254 sizeof(struct txbd8)) +
255 (priv->total_rx_ring_size *
256 sizeof(struct rxbd8)),
257 &addr, GFP_KERNEL);
258 if (!vaddr)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000259 return -ENOMEM;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000260
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000261 for (i = 0; i < priv->num_tx_queues; i++) {
262 tx_queue = priv->tx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000263 tx_queue->tx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000264 tx_queue->tx_bd_dma_base = addr;
265 tx_queue->dev = ndev;
266 /* enet DMA only understands physical addresses */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000267 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
268 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000269 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000270
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000271 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000272 for (i = 0; i < priv->num_rx_queues; i++) {
273 rx_queue = priv->rx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000274 rx_queue->rx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000275 rx_queue->rx_bd_dma_base = addr;
276 rx_queue->dev = ndev;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000277 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
278 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000279 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000280
281 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000282 for (i = 0; i < priv->num_tx_queues; i++) {
283 tx_queue = priv->tx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000284 tx_queue->tx_skbuff =
285 kmalloc_array(tx_queue->tx_ring_size,
286 sizeof(*tx_queue->tx_skbuff),
287 GFP_KERNEL);
288 if (!tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000289 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000290
291 for (k = 0; k < tx_queue->tx_ring_size; k++)
292 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000293 }
294
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000295 for (i = 0; i < priv->num_rx_queues; i++) {
296 rx_queue = priv->rx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000297 rx_queue->rx_skbuff =
298 kmalloc_array(rx_queue->rx_ring_size,
299 sizeof(*rx_queue->rx_skbuff),
300 GFP_KERNEL);
301 if (!rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000302 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000303
304 for (j = 0; j < rx_queue->rx_ring_size; j++)
305 rx_queue->rx_skbuff[j] = NULL;
306 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000307
Anton Vorontsov87283272009-10-12 06:00:39 +0000308 if (gfar_init_bds(ndev))
309 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000310
311 return 0;
312
313cleanup:
314 free_skb_resources(priv);
315 return -ENOMEM;
316}
317
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000318static void gfar_init_tx_rx_base(struct gfar_private *priv)
319{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000320 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000321 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000322 int i;
323
324 baddr = &regs->tbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000325 for (i = 0; i < priv->num_tx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000326 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000327 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000328 }
329
330 baddr = &regs->rbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000331 for (i = 0; i < priv->num_rx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000332 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000333 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000334 }
335}
336
Claudiu Manoil88302642014-02-24 12:13:43 +0200337static void gfar_rx_buff_size_config(struct gfar_private *priv)
338{
339 int frame_size = priv->ndev->mtu + ETH_HLEN;
340
341 /* set this when rx hw offload (TOE) functions are being used */
342 priv->uses_rxfcb = 0;
343
344 if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
345 priv->uses_rxfcb = 1;
346
347 if (priv->hwts_rx_en)
348 priv->uses_rxfcb = 1;
349
350 if (priv->uses_rxfcb)
351 frame_size += GMAC_FCB_LEN;
352
353 frame_size += priv->padding;
354
355 frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
356 INCREMENTAL_BUFFER_SIZE;
357
358 priv->rx_buffer_size = frame_size;
359}
360
Claudiu Manoila328ac92014-02-24 12:13:42 +0200361static void gfar_mac_rx_config(struct gfar_private *priv)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000362{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000363 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000364 u32 rctrl = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000365
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000366 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000367 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000368 /* Program the RIR0 reg with the required distribution */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200369 if (priv->poll_mode == GFAR_SQ_POLLING)
370 gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
371 else /* GFAR_MQ_POLLING */
372 gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000373 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000374
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000375 /* Restore PROMISC mode */
Claudiu Manoila328ac92014-02-24 12:13:42 +0200376 if (priv->ndev->flags & IFF_PROMISC)
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000377 rctrl |= RCTRL_PROM;
378
Claudiu Manoil88302642014-02-24 12:13:43 +0200379 if (priv->ndev->features & NETIF_F_RXCSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000380 rctrl |= RCTRL_CHECKSUMMING;
381
Claudiu Manoil88302642014-02-24 12:13:43 +0200382 if (priv->extended_hash)
383 rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000384
385 if (priv->padding) {
386 rctrl &= ~RCTRL_PAL_MASK;
387 rctrl |= RCTRL_PADDING(priv->padding);
388 }
389
Manfred Rudigier97553f72010-06-11 01:49:05 +0000390 /* Enable HW time stamping if requested from user space */
Claudiu Manoil88302642014-02-24 12:13:43 +0200391 if (priv->hwts_rx_en)
Manfred Rudigier97553f72010-06-11 01:49:05 +0000392 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
393
Claudiu Manoil88302642014-02-24 12:13:43 +0200394 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
Sebastian Pöhnb852b722011-07-26 00:03:13 +0000395 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000396
397 /* Init rctrl based on our settings */
398 gfar_write(&regs->rctrl, rctrl);
Claudiu Manoila328ac92014-02-24 12:13:42 +0200399}
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000400
Claudiu Manoila328ac92014-02-24 12:13:42 +0200401static void gfar_mac_tx_config(struct gfar_private *priv)
402{
403 struct gfar __iomem *regs = priv->gfargrp[0].regs;
404 u32 tctrl = 0;
405
406 if (priv->ndev->features & NETIF_F_IP_CSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000407 tctrl |= TCTRL_INIT_CSUM;
408
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +0000409 if (priv->prio_sched_en)
410 tctrl |= TCTRL_TXSCHED_PRIO;
411 else {
412 tctrl |= TCTRL_TXSCHED_WRRS;
413 gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
414 gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
415 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000416
Claudiu Manoil88302642014-02-24 12:13:43 +0200417 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
418 tctrl |= TCTRL_VLINS;
419
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000420 gfar_write(&regs->tctrl, tctrl);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000421}
422
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200423static void gfar_configure_coalescing(struct gfar_private *priv,
424 unsigned long tx_mask, unsigned long rx_mask)
425{
426 struct gfar __iomem *regs = priv->gfargrp[0].regs;
427 u32 __iomem *baddr;
428
429 if (priv->mode == MQ_MG_MODE) {
430 int i = 0;
431
432 baddr = &regs->txic0;
433 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
434 gfar_write(baddr + i, 0);
435 if (likely(priv->tx_queue[i]->txcoalescing))
436 gfar_write(baddr + i, priv->tx_queue[i]->txic);
437 }
438
439 baddr = &regs->rxic0;
440 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
441 gfar_write(baddr + i, 0);
442 if (likely(priv->rx_queue[i]->rxcoalescing))
443 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
444 }
445 } else {
446 /* Backward compatible case -- even if we enable
447 * multiple queues, there's only single reg to program
448 */
449 gfar_write(&regs->txic, 0);
450 if (likely(priv->tx_queue[0]->txcoalescing))
451 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
452
453 gfar_write(&regs->rxic, 0);
454 if (unlikely(priv->rx_queue[0]->rxcoalescing))
455 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
456 }
457}
458
459void gfar_configure_coalescing_all(struct gfar_private *priv)
460{
461 gfar_configure_coalescing(priv, 0xFF, 0xFF);
462}
463
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000464static struct net_device_stats *gfar_get_stats(struct net_device *dev)
465{
466 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000467 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
468 unsigned long tx_packets = 0, tx_bytes = 0;
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000469 int i;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000470
471 for (i = 0; i < priv->num_rx_queues; i++) {
472 rx_packets += priv->rx_queue[i]->stats.rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000473 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000474 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
475 }
476
477 dev->stats.rx_packets = rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000478 dev->stats.rx_bytes = rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000479 dev->stats.rx_dropped = rx_dropped;
480
481 for (i = 0; i < priv->num_tx_queues; i++) {
Eric Dumazet1ac9ad12011-01-12 12:13:14 +0000482 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
483 tx_packets += priv->tx_queue[i]->stats.tx_packets;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000484 }
485
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000486 dev->stats.tx_bytes = tx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000487 dev->stats.tx_packets = tx_packets;
488
489 return &dev->stats;
490}
491
Andy Fleming26ccfc32009-03-10 12:58:28 +0000492static const struct net_device_ops gfar_netdev_ops = {
493 .ndo_open = gfar_enet_open,
494 .ndo_start_xmit = gfar_start_xmit,
495 .ndo_stop = gfar_close,
496 .ndo_change_mtu = gfar_change_mtu,
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000497 .ndo_set_features = gfar_set_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000498 .ndo_set_rx_mode = gfar_set_multi,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000499 .ndo_tx_timeout = gfar_timeout,
500 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000501 .ndo_get_stats = gfar_get_stats,
Ben Hutchings240c1022009-07-09 17:54:35 +0000502 .ndo_set_mac_address = eth_mac_addr,
503 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000504#ifdef CONFIG_NET_POLL_CONTROLLER
505 .ndo_poll_controller = gfar_netpoll,
506#endif
507};
508
Claudiu Manoilefeddce2014-02-17 12:53:17 +0200509static void gfar_ints_disable(struct gfar_private *priv)
510{
511 int i;
512 for (i = 0; i < priv->num_grps; i++) {
513 struct gfar __iomem *regs = priv->gfargrp[i].regs;
514 /* Clear IEVENT */
515 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
516
517 /* Initialize IMASK */
518 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
519 }
520}
521
522static void gfar_ints_enable(struct gfar_private *priv)
523{
524 int i;
525 for (i = 0; i < priv->num_grps; i++) {
526 struct gfar __iomem *regs = priv->gfargrp[i].regs;
527 /* Unmask the interrupts we look for */
528 gfar_write(&regs->imask, IMASK_DEFAULT);
529 }
530}
531
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000532void lock_tx_qs(struct gfar_private *priv)
533{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000534 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000535
536 for (i = 0; i < priv->num_tx_queues; i++)
537 spin_lock(&priv->tx_queue[i]->txlock);
538}
539
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000540void unlock_tx_qs(struct gfar_private *priv)
541{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000542 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000543
544 for (i = 0; i < priv->num_tx_queues; i++)
545 spin_unlock(&priv->tx_queue[i]->txlock);
546}
547
Claudiu Manoil20862782014-02-17 12:53:14 +0200548static int gfar_alloc_tx_queues(struct gfar_private *priv)
549{
550 int i;
551
552 for (i = 0; i < priv->num_tx_queues; i++) {
553 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
554 GFP_KERNEL);
555 if (!priv->tx_queue[i])
556 return -ENOMEM;
557
558 priv->tx_queue[i]->tx_skbuff = NULL;
559 priv->tx_queue[i]->qindex = i;
560 priv->tx_queue[i]->dev = priv->ndev;
561 spin_lock_init(&(priv->tx_queue[i]->txlock));
562 }
563 return 0;
564}
565
566static int gfar_alloc_rx_queues(struct gfar_private *priv)
567{
568 int i;
569
570 for (i = 0; i < priv->num_rx_queues; i++) {
571 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
572 GFP_KERNEL);
573 if (!priv->rx_queue[i])
574 return -ENOMEM;
575
576 priv->rx_queue[i]->rx_skbuff = NULL;
577 priv->rx_queue[i]->qindex = i;
578 priv->rx_queue[i]->dev = priv->ndev;
Claudiu Manoil20862782014-02-17 12:53:14 +0200579 }
580 return 0;
581}
582
583static void gfar_free_tx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000584{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000585 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000586
587 for (i = 0; i < priv->num_tx_queues; i++)
588 kfree(priv->tx_queue[i]);
589}
590
Claudiu Manoil20862782014-02-17 12:53:14 +0200591static void gfar_free_rx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000592{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000593 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000594
595 for (i = 0; i < priv->num_rx_queues; i++)
596 kfree(priv->rx_queue[i]);
597}
598
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000599static void unmap_group_regs(struct gfar_private *priv)
600{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000601 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000602
603 for (i = 0; i < MAXGROUPS; i++)
604 if (priv->gfargrp[i].regs)
605 iounmap(priv->gfargrp[i].regs);
606}
607
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000608static void free_gfar_dev(struct gfar_private *priv)
609{
610 int i, j;
611
612 for (i = 0; i < priv->num_grps; i++)
613 for (j = 0; j < GFAR_NUM_IRQS; j++) {
614 kfree(priv->gfargrp[i].irqinfo[j]);
615 priv->gfargrp[i].irqinfo[j] = NULL;
616 }
617
618 free_netdev(priv->ndev);
619}
620
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000621static void disable_napi(struct gfar_private *priv)
622{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000623 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000624
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200625 for (i = 0; i < priv->num_grps; i++) {
626 napi_disable(&priv->gfargrp[i].napi_rx);
627 napi_disable(&priv->gfargrp[i].napi_tx);
628 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000629}
630
631static void enable_napi(struct gfar_private *priv)
632{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000633 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000634
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200635 for (i = 0; i < priv->num_grps; i++) {
636 napi_enable(&priv->gfargrp[i].napi_rx);
637 napi_enable(&priv->gfargrp[i].napi_tx);
638 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000639}
640
641static int gfar_parse_group(struct device_node *np,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000642 struct gfar_private *priv, const char *model)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000643{
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000644 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000645 int i;
646
Paul Gortmaker7c1e7e92013-02-04 09:49:42 +0000647 for (i = 0; i < GFAR_NUM_IRQS; i++) {
648 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
649 GFP_KERNEL);
650 if (!grp->irqinfo[i])
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000651 return -ENOMEM;
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000652 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000653
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000654 grp->regs = of_iomap(np, 0);
655 if (!grp->regs)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000656 return -ENOMEM;
657
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000658 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000659
660 /* If we aren't the FEC we have multiple interrupts */
661 if (model && strcasecmp(model, "FEC")) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000662 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
663 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
664 if (gfar_irq(grp, TX)->irq == NO_IRQ ||
665 gfar_irq(grp, RX)->irq == NO_IRQ ||
666 gfar_irq(grp, ER)->irq == NO_IRQ)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000667 return -EINVAL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000668 }
669
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000670 grp->priv = priv;
671 spin_lock_init(&grp->grplock);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000672 if (priv->mode == MQ_MG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200673 u32 *rxq_mask, *txq_mask;
674 rxq_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
675 txq_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
676
677 if (priv->poll_mode == GFAR_SQ_POLLING) {
678 /* One Q per interrupt group: Q0 to G0, Q1 to G1 */
679 grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
680 grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
681 } else { /* GFAR_MQ_POLLING */
682 grp->rx_bit_map = rxq_mask ?
683 *rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
684 grp->tx_bit_map = txq_mask ?
685 *txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
686 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000687 } else {
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000688 grp->rx_bit_map = 0xFF;
689 grp->tx_bit_map = 0xFF;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000690 }
Claudiu Manoil20862782014-02-17 12:53:14 +0200691
692 /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
693 * right to left, so we need to revert the 8 bits to get the q index
694 */
695 grp->rx_bit_map = bitrev8(grp->rx_bit_map);
696 grp->tx_bit_map = bitrev8(grp->tx_bit_map);
697
698 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
699 * also assign queues to groups
700 */
701 for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200702 if (!grp->rx_queue)
703 grp->rx_queue = priv->rx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200704 grp->num_rx_queues++;
705 grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
706 priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
707 priv->rx_queue[i]->grp = grp;
708 }
709
710 for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200711 if (!grp->tx_queue)
712 grp->tx_queue = priv->tx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200713 grp->num_tx_queues++;
714 grp->tstat |= (TSTAT_CLEAR_THALT >> i);
715 priv->tqueue |= (TQUEUE_EN0 >> i);
716 priv->tx_queue[i]->grp = grp;
717 }
718
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000719 priv->num_grps++;
720
721 return 0;
722}
723
Grant Likely2dc11582010-08-06 09:25:50 -0600724static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800725{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800726 const char *model;
727 const char *ctype;
728 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000729 int err = 0, i;
730 struct net_device *dev = NULL;
731 struct gfar_private *priv = NULL;
Grant Likely61c7a082010-04-13 16:12:29 -0700732 struct device_node *np = ofdev->dev.of_node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000733 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800734 const u32 *stash;
735 const u32 *stash_len;
736 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000737 unsigned int num_tx_qs, num_rx_qs;
738 u32 *tx_queues, *rx_queues;
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200739 unsigned short mode, poll_mode;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800740
741 if (!np || !of_device_is_available(np))
742 return -ENODEV;
743
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200744 if (of_device_is_compatible(np, "fsl,etsec2")) {
745 mode = MQ_MG_MODE;
746 poll_mode = GFAR_SQ_POLLING;
747 } else {
748 mode = SQ_SG_MODE;
749 poll_mode = GFAR_SQ_POLLING;
750 }
751
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200752 /* parse the num of HW tx and rx queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000753 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200754 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
755
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200756 if (mode == SQ_SG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200757 num_tx_qs = 1;
758 num_rx_qs = 1;
759 } else { /* MQ_MG_MODE */
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200760 /* get the actual number of supported groups */
761 unsigned int num_grps = of_get_available_child_count(np);
762
763 if (num_grps == 0 || num_grps > MAXGROUPS) {
764 dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
765 num_grps);
766 pr_err("Cannot do alloc_etherdev, aborting\n");
767 return -EINVAL;
768 }
769
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200770 if (poll_mode == GFAR_SQ_POLLING) {
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200771 num_tx_qs = num_grps; /* one txq per int group */
772 num_rx_qs = num_grps; /* one rxq per int group */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200773 } else { /* GFAR_MQ_POLLING */
774 num_tx_qs = tx_queues ? *tx_queues : 1;
775 num_rx_qs = rx_queues ? *rx_queues : 1;
776 }
777 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000778
779 if (num_tx_qs > MAX_TX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000780 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
781 num_tx_qs, MAX_TX_QS);
782 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000783 return -EINVAL;
784 }
785
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000786 if (num_rx_qs > MAX_RX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000787 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
788 num_rx_qs, MAX_RX_QS);
789 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000790 return -EINVAL;
791 }
792
793 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
794 dev = *pdev;
795 if (NULL == dev)
796 return -ENOMEM;
797
798 priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000799 priv->ndev = dev;
800
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200801 priv->mode = mode;
802 priv->poll_mode = poll_mode;
803
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000804 priv->num_tx_queues = num_tx_qs;
Ben Hutchingsfe069122010-09-27 08:27:37 +0000805 netif_set_real_num_rx_queues(dev, num_rx_qs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000806 priv->num_rx_queues = num_rx_qs;
Claudiu Manoil20862782014-02-17 12:53:14 +0200807
808 err = gfar_alloc_tx_queues(priv);
809 if (err)
810 goto tx_alloc_failed;
811
812 err = gfar_alloc_rx_queues(priv);
813 if (err)
814 goto rx_alloc_failed;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800815
Jan Ceuleers0977f812012-06-05 03:42:12 +0000816 /* Init Rx queue filer rule set linked list */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700817 INIT_LIST_HEAD(&priv->rx_list.list);
818 priv->rx_list.count = 0;
819 mutex_init(&priv->rx_queue_access);
820
Andy Flemingb31a1d82008-12-16 15:29:15 -0800821 model = of_get_property(np, "model", NULL);
822
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000823 for (i = 0; i < MAXGROUPS; i++)
824 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800825
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000826 /* Parse and initialize group specific information */
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200827 if (priv->mode == MQ_MG_MODE) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000828 for_each_child_of_node(np, child) {
829 err = gfar_parse_group(child, priv, model);
830 if (err)
831 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800832 }
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200833 } else { /* SQ_SG_MODE */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000834 err = gfar_parse_group(np, priv, model);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000835 if (err)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000836 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800837 }
838
Andy Fleming4d7902f2009-02-04 16:43:44 -0800839 stash = of_get_property(np, "bd-stash", NULL);
840
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000841 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800842 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
843 priv->bd_stash_en = 1;
844 }
845
846 stash_len = of_get_property(np, "rx-stash-len", NULL);
847
848 if (stash_len)
849 priv->rx_stash_size = *stash_len;
850
851 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
852
853 if (stash_idx)
854 priv->rx_stash_index = *stash_idx;
855
856 if (stash_len || stash_idx)
857 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
858
Andy Flemingb31a1d82008-12-16 15:29:15 -0800859 mac_addr = of_get_mac_address(np);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000860
Andy Flemingb31a1d82008-12-16 15:29:15 -0800861 if (mac_addr)
Joe Perches6a3c9102011-11-16 09:38:02 +0000862 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800863
864 if (model && !strcasecmp(model, "TSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200865 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000866 FSL_GIANFAR_DEV_HAS_COALESCE |
867 FSL_GIANFAR_DEV_HAS_RMON |
868 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
869
Andy Flemingb31a1d82008-12-16 15:29:15 -0800870 if (model && !strcasecmp(model, "eTSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200871 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000872 FSL_GIANFAR_DEV_HAS_COALESCE |
873 FSL_GIANFAR_DEV_HAS_RMON |
874 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000875 FSL_GIANFAR_DEV_HAS_CSUM |
876 FSL_GIANFAR_DEV_HAS_VLAN |
877 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
878 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
879 FSL_GIANFAR_DEV_HAS_TIMER;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800880
881 ctype = of_get_property(np, "phy-connection-type", NULL);
882
883 /* We only care about rgmii-id. The rest are autodetected */
884 if (ctype && !strcmp(ctype, "rgmii-id"))
885 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
886 else
887 priv->interface = PHY_INTERFACE_MODE_MII;
888
889 if (of_get_property(np, "fsl,magic-packet", NULL))
890 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
891
Grant Likelyfe192a42009-04-25 12:53:12 +0000892 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800893
Florian Fainellibe403642014-05-22 09:47:48 -0700894 /* In the case of a fixed PHY, the DT node associated
895 * to the PHY is the Ethernet MAC DT node.
896 */
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200897 if (!priv->phy_node && of_phy_is_fixed_link(np)) {
Florian Fainellibe403642014-05-22 09:47:48 -0700898 err = of_phy_register_fixed_link(np);
899 if (err)
900 goto err_grp_init;
901
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200902 priv->phy_node = of_node_get(np);
Florian Fainellibe403642014-05-22 09:47:48 -0700903 }
904
Andy Flemingb31a1d82008-12-16 15:29:15 -0800905 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000906 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800907
908 return 0;
909
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000910err_grp_init:
911 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +0200912rx_alloc_failed:
913 gfar_free_rx_queues(priv);
914tx_alloc_failed:
915 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000916 free_gfar_dev(priv);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800917 return err;
918}
919
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000920static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000921{
922 struct hwtstamp_config config;
923 struct gfar_private *priv = netdev_priv(netdev);
924
925 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
926 return -EFAULT;
927
928 /* reserved for future extensions */
929 if (config.flags)
930 return -EINVAL;
931
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000932 switch (config.tx_type) {
933 case HWTSTAMP_TX_OFF:
934 priv->hwts_tx_en = 0;
935 break;
936 case HWTSTAMP_TX_ON:
937 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
938 return -ERANGE;
939 priv->hwts_tx_en = 1;
940 break;
941 default:
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000942 return -ERANGE;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000943 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000944
945 switch (config.rx_filter) {
946 case HWTSTAMP_FILTER_NONE:
Manfred Rudigier97553f72010-06-11 01:49:05 +0000947 if (priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000948 priv->hwts_rx_en = 0;
Claudiu Manoil08511332014-02-24 12:13:45 +0200949 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000950 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000951 break;
952 default:
953 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
954 return -ERANGE;
Manfred Rudigier97553f72010-06-11 01:49:05 +0000955 if (!priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000956 priv->hwts_rx_en = 1;
Claudiu Manoil08511332014-02-24 12:13:45 +0200957 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000958 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000959 config.rx_filter = HWTSTAMP_FILTER_ALL;
960 break;
961 }
962
963 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
964 -EFAULT : 0;
965}
966
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000967static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
968{
969 struct hwtstamp_config config;
970 struct gfar_private *priv = netdev_priv(netdev);
971
972 config.flags = 0;
973 config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
974 config.rx_filter = (priv->hwts_rx_en ?
975 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
976
977 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
978 -EFAULT : 0;
979}
980
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000981static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
982{
983 struct gfar_private *priv = netdev_priv(dev);
984
985 if (!netif_running(dev))
986 return -EINVAL;
987
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000988 if (cmd == SIOCSHWTSTAMP)
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000989 return gfar_hwtstamp_set(dev, rq);
990 if (cmd == SIOCGHWTSTAMP)
991 return gfar_hwtstamp_get(dev, rq);
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000992
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000993 if (!priv->phydev)
994 return -ENODEV;
995
Richard Cochran28b04112010-07-17 08:48:55 +0000996 return phy_mii_ioctl(priv->phydev, rq, cmd);
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000997}
998
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000999static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
1000 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001001{
1002 u32 rqfpr = FPR_FILER_MASK;
1003 u32 rqfcr = 0x0;
1004
1005 rqfar--;
1006 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001007 priv->ftp_rqfpr[rqfar] = rqfpr;
1008 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001009 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1010
1011 rqfar--;
1012 rqfcr = RQFCR_CMP_NOMATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001013 priv->ftp_rqfpr[rqfar] = rqfpr;
1014 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001015 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1016
1017 rqfar--;
1018 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
1019 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001020 priv->ftp_rqfcr[rqfar] = rqfcr;
1021 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001022 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1023
1024 rqfar--;
1025 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
1026 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001027 priv->ftp_rqfcr[rqfar] = rqfcr;
1028 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001029 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1030
1031 return rqfar;
1032}
1033
1034static void gfar_init_filer_table(struct gfar_private *priv)
1035{
1036 int i = 0x0;
1037 u32 rqfar = MAX_FILER_IDX;
1038 u32 rqfcr = 0x0;
1039 u32 rqfpr = FPR_FILER_MASK;
1040
1041 /* Default rule */
1042 rqfcr = RQFCR_CMP_MATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001043 priv->ftp_rqfcr[rqfar] = rqfcr;
1044 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001045 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1046
1047 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1048 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1049 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1050 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1051 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1052 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1053
Uwe Kleine-König85dd08e2010-06-11 12:16:55 +02001054 /* cur_filer_idx indicated the first non-masked rule */
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001055 priv->cur_filer_idx = rqfar;
1056
1057 /* Rest are masked rules */
1058 rqfcr = RQFCR_CMP_NOMATCH;
1059 for (i = 0; i < rqfar; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001060 priv->ftp_rqfcr[i] = rqfcr;
1061 priv->ftp_rqfpr[i] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001062 gfar_write_filer(priv, i, rqfcr, rqfpr);
1063 }
1064}
1065
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001066static void __gfar_detect_errata_83xx(struct gfar_private *priv)
Anton Vorontsov7d350972010-06-30 06:39:12 +00001067{
Anton Vorontsov7d350972010-06-30 06:39:12 +00001068 unsigned int pvr = mfspr(SPRN_PVR);
1069 unsigned int svr = mfspr(SPRN_SVR);
1070 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1071 unsigned int rev = svr & 0xffff;
1072
1073 /* MPC8313 Rev 2.0 and higher; All MPC837x */
1074 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001075 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001076 priv->errata |= GFAR_ERRATA_74;
1077
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001078 /* MPC8313 and MPC837x all rev */
1079 if ((pvr == 0x80850010 && mod == 0x80b0) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001080 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001081 priv->errata |= GFAR_ERRATA_76;
1082
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001083 /* MPC8313 Rev < 2.0 */
1084 if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
Alex Dubov4363c2f2011-03-16 17:57:13 +00001085 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001086}
1087
1088static void __gfar_detect_errata_85xx(struct gfar_private *priv)
1089{
1090 unsigned int svr = mfspr(SPRN_SVR);
1091
1092 if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
1093 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil53fad772013-10-09 20:20:42 +03001094 if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
1095 ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
1096 priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001097}
1098
1099static void gfar_detect_errata(struct gfar_private *priv)
1100{
1101 struct device *dev = &priv->ofdev->dev;
1102
1103 /* no plans to fix */
1104 priv->errata |= GFAR_ERRATA_A002;
1105
1106 if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
1107 __gfar_detect_errata_85xx(priv);
1108 else /* non-mpc85xx parts, i.e. e300 core based */
1109 __gfar_detect_errata_83xx(priv);
Alex Dubov4363c2f2011-03-16 17:57:13 +00001110
Anton Vorontsov7d350972010-06-30 06:39:12 +00001111 if (priv->errata)
1112 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1113 priv->errata);
1114}
1115
Claudiu Manoil08511332014-02-24 12:13:45 +02001116void gfar_mac_reset(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Claudiu Manoil20862782014-02-17 12:53:14 +02001118 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Claudiu Manoila328ac92014-02-24 12:13:42 +02001119 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001122 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Andy Flemingb98ac702009-02-04 16:38:05 -08001124 /* We need to delay at least 3 TX clocks */
Claudiu Manoila328ac92014-02-24 12:13:42 +02001125 udelay(3);
Andy Flemingb98ac702009-02-04 16:38:05 -08001126
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001127 /* the soft reset bit is not self-resetting, so we need to
1128 * clear it before resuming normal operation
1129 */
Claudiu Manoil20862782014-02-17 12:53:14 +02001130 gfar_write(&regs->maccfg1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Claudiu Manoila328ac92014-02-24 12:13:42 +02001132 udelay(3);
1133
Claudiu Manoil88302642014-02-24 12:13:43 +02001134 /* Compute rx_buff_size based on config flags */
1135 gfar_rx_buff_size_config(priv);
1136
1137 /* Initialize the max receive frame/buffer lengths */
1138 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Claudiu Manoila328ac92014-02-24 12:13:42 +02001139 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1140
1141 /* Initialize the Minimum Frame Length Register */
1142 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* Initialize MACCFG2. */
Anton Vorontsov7d350972010-06-30 06:39:12 +00001145 tempval = MACCFG2_INIT_SETTINGS;
Claudiu Manoil88302642014-02-24 12:13:43 +02001146
1147 /* If the mtu is larger than the max size for standard
1148 * ethernet frames (ie, a jumbo frame), then set maccfg2
1149 * to allow huge frames, and to check the length
1150 */
1151 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
1152 gfar_has_errata(priv, GFAR_ERRATA_74))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001153 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
Claudiu Manoil88302642014-02-24 12:13:43 +02001154
Anton Vorontsov7d350972010-06-30 06:39:12 +00001155 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Claudiu Manoila328ac92014-02-24 12:13:42 +02001157 /* Clear mac addr hash registers */
1158 gfar_write(&regs->igaddr0, 0);
1159 gfar_write(&regs->igaddr1, 0);
1160 gfar_write(&regs->igaddr2, 0);
1161 gfar_write(&regs->igaddr3, 0);
1162 gfar_write(&regs->igaddr4, 0);
1163 gfar_write(&regs->igaddr5, 0);
1164 gfar_write(&regs->igaddr6, 0);
1165 gfar_write(&regs->igaddr7, 0);
1166
1167 gfar_write(&regs->gaddr0, 0);
1168 gfar_write(&regs->gaddr1, 0);
1169 gfar_write(&regs->gaddr2, 0);
1170 gfar_write(&regs->gaddr3, 0);
1171 gfar_write(&regs->gaddr4, 0);
1172 gfar_write(&regs->gaddr5, 0);
1173 gfar_write(&regs->gaddr6, 0);
1174 gfar_write(&regs->gaddr7, 0);
1175
1176 if (priv->extended_hash)
1177 gfar_clear_exact_match(priv->ndev);
1178
1179 gfar_mac_rx_config(priv);
1180
1181 gfar_mac_tx_config(priv);
1182
1183 gfar_set_mac_address(priv->ndev);
1184
1185 gfar_set_multi(priv->ndev);
1186
1187 /* clear ievent and imask before configuring coalescing */
1188 gfar_ints_disable(priv);
1189
1190 /* Configure the coalescing support */
1191 gfar_configure_coalescing_all(priv);
1192}
1193
1194static void gfar_hw_init(struct gfar_private *priv)
1195{
1196 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1197 u32 attrs;
1198
1199 /* Stop the DMA engine now, in case it was running before
1200 * (The firmware could have used it, and left it running).
1201 */
1202 gfar_halt(priv);
1203
1204 gfar_mac_reset(priv);
1205
1206 /* Zero out the rmon mib registers if it has them */
1207 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1208 memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1209
1210 /* Mask off the CAM interrupts */
1211 gfar_write(&regs->rmon.cam1, 0xffffffff);
1212 gfar_write(&regs->rmon.cam2, 0xffffffff);
1213 }
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001216 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Claudiu Manoil34018fd2014-02-17 12:53:15 +02001218 /* Set the extraction length and index */
1219 attrs = ATTRELI_EL(priv->rx_stash_size) |
1220 ATTRELI_EI(priv->rx_stash_index);
1221
1222 gfar_write(&regs->attreli, attrs);
1223
1224 /* Start with defaults, and add stashing
1225 * depending on driver parameters
1226 */
1227 attrs = ATTR_INIT_SETTINGS;
1228
1229 if (priv->bd_stash_en)
1230 attrs |= ATTR_BDSTASH;
1231
1232 if (priv->rx_stash_size != 0)
1233 attrs |= ATTR_BUFSTASH;
1234
1235 gfar_write(&regs->attr, attrs);
1236
1237 /* FIFO configs */
1238 gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
1239 gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
1240 gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
1241
Claudiu Manoil20862782014-02-17 12:53:14 +02001242 /* Program the interrupt steering regs, only for MG devices */
1243 if (priv->num_grps > 1)
1244 gfar_write_isrg(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Xiubo Li898157e2014-06-04 16:49:16 +08001247static void gfar_init_addr_hash_table(struct gfar_private *priv)
Claudiu Manoil20862782014-02-17 12:53:14 +02001248{
1249 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001250
Andy Flemingb31a1d82008-12-16 15:29:15 -08001251 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001252 priv->extended_hash = 1;
1253 priv->hash_width = 9;
1254
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001255 priv->hash_regs[0] = &regs->igaddr0;
1256 priv->hash_regs[1] = &regs->igaddr1;
1257 priv->hash_regs[2] = &regs->igaddr2;
1258 priv->hash_regs[3] = &regs->igaddr3;
1259 priv->hash_regs[4] = &regs->igaddr4;
1260 priv->hash_regs[5] = &regs->igaddr5;
1261 priv->hash_regs[6] = &regs->igaddr6;
1262 priv->hash_regs[7] = &regs->igaddr7;
1263 priv->hash_regs[8] = &regs->gaddr0;
1264 priv->hash_regs[9] = &regs->gaddr1;
1265 priv->hash_regs[10] = &regs->gaddr2;
1266 priv->hash_regs[11] = &regs->gaddr3;
1267 priv->hash_regs[12] = &regs->gaddr4;
1268 priv->hash_regs[13] = &regs->gaddr5;
1269 priv->hash_regs[14] = &regs->gaddr6;
1270 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001271
1272 } else {
1273 priv->extended_hash = 0;
1274 priv->hash_width = 8;
1275
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001276 priv->hash_regs[0] = &regs->gaddr0;
1277 priv->hash_regs[1] = &regs->gaddr1;
1278 priv->hash_regs[2] = &regs->gaddr2;
1279 priv->hash_regs[3] = &regs->gaddr3;
1280 priv->hash_regs[4] = &regs->gaddr4;
1281 priv->hash_regs[5] = &regs->gaddr5;
1282 priv->hash_regs[6] = &regs->gaddr6;
1283 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001284 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001285}
1286
1287/* Set up the ethernet device structure, private data,
1288 * and anything else we need before we start
1289 */
1290static int gfar_probe(struct platform_device *ofdev)
1291{
1292 struct net_device *dev = NULL;
1293 struct gfar_private *priv = NULL;
1294 int err = 0, i;
1295
1296 err = gfar_of_init(ofdev, &dev);
1297
1298 if (err)
1299 return err;
1300
1301 priv = netdev_priv(dev);
1302 priv->ndev = dev;
1303 priv->ofdev = ofdev;
1304 priv->dev = &ofdev->dev;
1305 SET_NETDEV_DEV(dev, &ofdev->dev);
1306
1307 spin_lock_init(&priv->bflock);
1308 INIT_WORK(&priv->reset_task, gfar_reset_task);
1309
1310 platform_set_drvdata(ofdev, priv);
1311
1312 gfar_detect_errata(priv);
1313
Claudiu Manoil20862782014-02-17 12:53:14 +02001314 /* Set the dev->base_addr to the gfar reg region */
1315 dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
1316
1317 /* Fill in the dev structure */
1318 dev->watchdog_timeo = TX_TIMEOUT;
1319 dev->mtu = 1500;
1320 dev->netdev_ops = &gfar_netdev_ops;
1321 dev->ethtool_ops = &gfar_ethtool_ops;
1322
1323 /* Register for napi ...We are registering NAPI for each grp */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02001324 for (i = 0; i < priv->num_grps; i++) {
1325 if (priv->poll_mode == GFAR_SQ_POLLING) {
1326 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1327 gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
1328 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1329 gfar_poll_tx_sq, 2);
1330 } else {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02001331 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1332 gfar_poll_rx, GFAR_DEV_WEIGHT);
1333 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1334 gfar_poll_tx, 2);
1335 }
1336 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001337
1338 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
1339 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
1340 NETIF_F_RXCSUM;
1341 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
1342 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
1343 }
1344
1345 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
1346 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
1347 NETIF_F_HW_VLAN_CTAG_RX;
1348 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1349 }
1350
1351 gfar_init_addr_hash_table(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001352
Claudiu Manoil532c37b2014-02-17 12:53:16 +02001353 /* Insert receive time stamps into padding alignment bytes */
1354 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1355 priv->padding = 8;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001356
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001357 if (dev->features & NETIF_F_IP_CSUM ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001358 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
Wu Jiajun-B06378bee9e582012-05-21 23:00:48 +00001359 dev->needed_headroom = GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001363 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001364 for (i = 0; i < priv->num_tx_queues; i++) {
1365 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1366 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1367 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1368 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1369 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001370
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001371 for (i = 0; i < priv->num_rx_queues; i++) {
1372 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1373 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1374 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jan Ceuleers0977f812012-06-05 03:42:12 +00001377 /* always enable rx filer */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001378 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001379 /* Enable most messages by default */
1380 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +00001381 /* use pritority h/w tx queue scheduling for single queue devices */
1382 if (priv->num_tx_queues == 1)
1383 priv->prio_sched_en = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001384
Claudiu Manoil08511332014-02-24 12:13:45 +02001385 set_bit(GFAR_DOWN, &priv->state);
1386
Claudiu Manoila328ac92014-02-24 12:13:42 +02001387 gfar_hw_init(priv);
Trent Piephod3eab822008-10-02 11:12:24 +00001388
Fabio Estevamd4c642e2014-06-03 19:55:38 -03001389 /* Carrier starts down, phylib will bring it up */
1390 netif_carrier_off(dev);
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 err = register_netdev(dev);
1393
1394 if (err) {
Joe Perches59deab22011-06-14 08:57:47 +00001395 pr_err("%s: Cannot register net device, aborting\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 goto register_fail;
1397 }
1398
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001399 device_init_wakeup(&dev->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001400 priv->device_flags &
1401 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001402
Dai Harukic50a5d92008-12-17 16:51:32 -08001403 /* fill out IRQ number and name fields */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001404 for (i = 0; i < priv->num_grps; i++) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001405 struct gfar_priv_grp *grp = &priv->gfargrp[i];
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001406 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001407 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001408 dev->name, "_g", '0' + i, "_tx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001409 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001410 dev->name, "_g", '0' + i, "_rx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001411 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001412 dev->name, "_g", '0' + i, "_er");
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001413 } else
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001414 strcpy(gfar_irq(grp, TX)->name, dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001415 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001416
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001417 /* Initialize the filer table */
1418 gfar_init_filer_table(priv);
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* Print out the device info */
Joe Perches59deab22011-06-14 08:57:47 +00001421 netdev_info(dev, "mac: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Jan Ceuleers0977f812012-06-05 03:42:12 +00001423 /* Even more device info helps when determining which kernel
1424 * provided which set of benchmarks.
1425 */
Joe Perches59deab22011-06-14 08:57:47 +00001426 netdev_info(dev, "Running with NAPI enabled\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001427 for (i = 0; i < priv->num_rx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001428 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1429 i, priv->rx_queue[i]->rx_ring_size);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001430 for (i = 0; i < priv->num_tx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001431 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1432 i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 return 0;
1435
1436register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001437 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001438 gfar_free_rx_queues(priv);
1439 gfar_free_tx_queues(priv);
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001440 of_node_put(priv->phy_node);
1441 of_node_put(priv->tbi_node);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001442 free_gfar_dev(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001443 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Grant Likely2dc11582010-08-06 09:25:50 -06001446static int gfar_remove(struct platform_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001448 struct gfar_private *priv = platform_get_drvdata(ofdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001450 of_node_put(priv->phy_node);
1451 of_node_put(priv->tbi_node);
Grant Likelyfe192a42009-04-25 12:53:12 +00001452
David S. Millerd9d8e042009-09-06 01:41:02 -07001453 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001454 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001455 gfar_free_rx_queues(priv);
1456 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001457 free_gfar_dev(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 return 0;
1460}
1461
Scott Woodd87eb122008-07-11 18:04:45 -05001462#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001463
1464static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001465{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001466 struct gfar_private *priv = dev_get_drvdata(dev);
1467 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001468 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001469 unsigned long flags;
1470 u32 tempval;
1471
1472 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001473 (priv->device_flags &
1474 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001475
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001476 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001477
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001478 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001479
1480 local_irq_save(flags);
1481 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001482
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001483 gfar_halt_nodisable(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001484
1485 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001486 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001487
1488 tempval &= ~MACCFG1_TX_EN;
1489
1490 if (!magic_packet)
1491 tempval &= ~MACCFG1_RX_EN;
1492
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001493 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001494
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001495 unlock_tx_qs(priv);
1496 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001497
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001498 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001499
1500 if (magic_packet) {
1501 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001502 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001503
1504 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001505 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001506 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001507 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001508 } else {
1509 phy_stop(priv->phydev);
1510 }
1511 }
1512
1513 return 0;
1514}
1515
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001516static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001517{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001518 struct gfar_private *priv = dev_get_drvdata(dev);
1519 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001520 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001521 unsigned long flags;
1522 u32 tempval;
1523 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001524 (priv->device_flags &
1525 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001526
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001527 if (!netif_running(ndev)) {
1528 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001529 return 0;
1530 }
1531
1532 if (!magic_packet && priv->phydev)
1533 phy_start(priv->phydev);
1534
1535 /* Disable Magic Packet mode, in case something
1536 * else woke us up.
1537 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001538 local_irq_save(flags);
1539 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001540
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001541 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001542 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001543 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001544
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001545 gfar_start(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001546
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001547 unlock_tx_qs(priv);
1548 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001549
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001550 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001551
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001552 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001553
1554 return 0;
1555}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001556
1557static int gfar_restore(struct device *dev)
1558{
1559 struct gfar_private *priv = dev_get_drvdata(dev);
1560 struct net_device *ndev = priv->ndev;
1561
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001562 if (!netif_running(ndev)) {
1563 netif_device_attach(ndev);
1564
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001565 return 0;
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001566 }
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001567
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001568 if (gfar_init_bds(ndev)) {
1569 free_skb_resources(priv);
1570 return -ENOMEM;
1571 }
1572
Claudiu Manoila328ac92014-02-24 12:13:42 +02001573 gfar_mac_reset(priv);
1574
1575 gfar_init_tx_rx_base(priv);
1576
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001577 gfar_start(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001578
1579 priv->oldlink = 0;
1580 priv->oldspeed = 0;
1581 priv->oldduplex = -1;
1582
1583 if (priv->phydev)
1584 phy_start(priv->phydev);
1585
1586 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001587 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001588
1589 return 0;
1590}
1591
1592static struct dev_pm_ops gfar_pm_ops = {
1593 .suspend = gfar_suspend,
1594 .resume = gfar_resume,
1595 .freeze = gfar_suspend,
1596 .thaw = gfar_resume,
1597 .restore = gfar_restore,
1598};
1599
1600#define GFAR_PM_OPS (&gfar_pm_ops)
1601
Scott Woodd87eb122008-07-11 18:04:45 -05001602#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001603
1604#define GFAR_PM_OPS NULL
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001605
Scott Woodd87eb122008-07-11 18:04:45 -05001606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001608/* Reads the controller's registers to determine what interface
1609 * connects it to the PHY.
1610 */
1611static phy_interface_t gfar_get_interface(struct net_device *dev)
1612{
1613 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001614 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001615 u32 ecntrl;
1616
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001617 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001618
1619 if (ecntrl & ECNTRL_SGMII_MODE)
1620 return PHY_INTERFACE_MODE_SGMII;
1621
1622 if (ecntrl & ECNTRL_TBI_MODE) {
1623 if (ecntrl & ECNTRL_REDUCED_MODE)
1624 return PHY_INTERFACE_MODE_RTBI;
1625 else
1626 return PHY_INTERFACE_MODE_TBI;
1627 }
1628
1629 if (ecntrl & ECNTRL_REDUCED_MODE) {
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001630 if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001631 return PHY_INTERFACE_MODE_RMII;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001632 }
Andy Fleming7132ab72007-07-11 11:43:07 -05001633 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001634 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001635
Jan Ceuleers0977f812012-06-05 03:42:12 +00001636 /* This isn't autodetected right now, so it must
Andy Fleming7132ab72007-07-11 11:43:07 -05001637 * be set by the device tree or platform code.
1638 */
1639 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1640 return PHY_INTERFACE_MODE_RGMII_ID;
1641
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001642 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001643 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001644 }
1645
Andy Flemingb31a1d82008-12-16 15:29:15 -08001646 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001647 return PHY_INTERFACE_MODE_GMII;
1648
1649 return PHY_INTERFACE_MODE_MII;
1650}
1651
1652
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001653/* Initializes driver's PHY state, and attaches to the PHY.
1654 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
1656static int init_phy(struct net_device *dev)
1657{
1658 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001659 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001660 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001661 GFAR_SUPPORTED_GBIT : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001662 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 priv->oldlink = 0;
1665 priv->oldspeed = 0;
1666 priv->oldduplex = -1;
1667
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001668 interface = gfar_get_interface(dev);
1669
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001670 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1671 interface);
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001672 if (!priv->phydev) {
1673 dev_err(&dev->dev, "could not attach to PHY\n");
1674 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Kapil Junejad3c12872007-05-11 18:25:11 -05001677 if (interface == PHY_INTERFACE_MODE_SGMII)
1678 gfar_configure_serdes(dev);
1679
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001680 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001681 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1682 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Jan Ceuleers0977f812012-06-05 03:42:12 +00001687/* Initialize TBI PHY interface for communicating with the
Paul Gortmakerd0313582008-04-17 00:08:10 -04001688 * SERDES lynx PHY on the chip. We communicate with this PHY
1689 * through the MDIO bus on each controller, treating it as a
1690 * "normal" PHY at the address found in the TBIPA register. We assume
1691 * that the TBIPA register is valid. Either the MDIO bus code will set
1692 * it to a value that doesn't conflict with other PHYs on the bus, or the
1693 * value doesn't matter, as there are no other PHYs on the bus.
1694 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001695static void gfar_configure_serdes(struct net_device *dev)
1696{
1697 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001698 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001699
Grant Likelyfe192a42009-04-25 12:53:12 +00001700 if (!priv->tbi_node) {
1701 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1702 "device tree specify a tbi-handle\n");
1703 return;
1704 }
1705
1706 tbiphy = of_phy_find_device(priv->tbi_node);
1707 if (!tbiphy) {
1708 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001709 return;
1710 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001711
Jan Ceuleers0977f812012-06-05 03:42:12 +00001712 /* If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001713 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1714 * everything for us? Resetting it takes the link down and requires
1715 * several seconds for it to come back.
1716 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001717 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001718 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001719
Paul Gortmakerd0313582008-04-17 00:08:10 -04001720 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001721 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001722
Grant Likelyfe192a42009-04-25 12:53:12 +00001723 phy_write(tbiphy, MII_ADVERTISE,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001724 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1725 ADVERTISE_1000XPSE_ASYM);
Kapil Junejad3c12872007-05-11 18:25:11 -05001726
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001727 phy_write(tbiphy, MII_BMCR,
1728 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1729 BMCR_SPEED1000);
Kapil Junejad3c12872007-05-11 18:25:11 -05001730}
1731
Anton Vorontsov511d9342010-06-30 06:39:15 +00001732static int __gfar_is_rx_idle(struct gfar_private *priv)
1733{
1734 u32 res;
1735
Jan Ceuleers0977f812012-06-05 03:42:12 +00001736 /* Normaly TSEC should not hang on GRS commands, so we should
Anton Vorontsov511d9342010-06-30 06:39:15 +00001737 * actually wait for IEVENT_GRSC flag.
1738 */
Claudiu Manoilad3660c2013-10-09 20:20:40 +03001739 if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
Anton Vorontsov511d9342010-06-30 06:39:15 +00001740 return 0;
1741
Jan Ceuleers0977f812012-06-05 03:42:12 +00001742 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
Anton Vorontsov511d9342010-06-30 06:39:15 +00001743 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1744 * and the Rx can be safely reset.
1745 */
1746 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1747 res &= 0x7f807f80;
1748 if ((res & 0xffff) == (res >> 16))
1749 return 1;
1750
1751 return 0;
1752}
Kumar Gala0bbaf062005-06-20 10:54:21 -05001753
1754/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001755static void gfar_halt_nodisable(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001757 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 u32 tempval;
1759
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001760 gfar_ints_disable(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001763 tempval = gfar_read(&regs->dmactrl);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001764 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS)) !=
1765 (DMACTRL_GRS | DMACTRL_GTS)) {
Anton Vorontsov511d9342010-06-30 06:39:15 +00001766 int ret;
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001769 gfar_write(&regs->dmactrl, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Anton Vorontsov511d9342010-06-30 06:39:15 +00001771 do {
1772 ret = spin_event_timeout(((gfar_read(&regs->ievent) &
1773 (IEVENT_GRSC | IEVENT_GTSC)) ==
1774 (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0);
1775 if (!ret && !(gfar_read(&regs->ievent) & IEVENT_GRSC))
1776 ret = __gfar_is_rx_idle(priv);
1777 } while (!ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Scott Woodd87eb122008-07-11 18:04:45 -05001779}
Scott Woodd87eb122008-07-11 18:04:45 -05001780
1781/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001782void gfar_halt(struct gfar_private *priv)
Scott Woodd87eb122008-07-11 18:04:45 -05001783{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001784 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001785 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001787 /* Dissable the Rx/Tx hw queues */
1788 gfar_write(&regs->rqueue, 0);
1789 gfar_write(&regs->tqueue, 0);
Scott Wood2a54adc2008-08-12 15:10:46 -05001790
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001791 mdelay(10);
1792
1793 gfar_halt_nodisable(priv);
1794
1795 /* Disable Rx/Tx DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 tempval = gfar_read(&regs->maccfg1);
1797 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1798 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001799}
1800
1801void stop_gfar(struct net_device *dev)
1802{
1803 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001804
Claudiu Manoil08511332014-02-24 12:13:45 +02001805 netif_tx_stop_all_queues(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001806
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001807 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02001808 set_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001809 smp_mb__after_atomic();
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001810
Claudiu Manoil08511332014-02-24 12:13:45 +02001811 disable_napi(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001812
Claudiu Manoil08511332014-02-24 12:13:45 +02001813 /* disable ints and gracefully shut down Rx/Tx DMA */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001814 gfar_halt(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Claudiu Manoil08511332014-02-24 12:13:45 +02001816 phy_stop(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001821static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001824 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001825 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001827 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001829 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1830 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001831 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Claudiu Manoil369ec162013-02-14 05:00:02 +00001833 dma_unmap_single(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001834 txbdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08001835 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001836 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001837 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001838 txbdp++;
Claudiu Manoil369ec162013-02-14 05:00:02 +00001839 dma_unmap_page(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001840 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001842 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001843 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1844 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001846 kfree(tx_queue->tx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001847 tx_queue->tx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001850static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1851{
1852 struct rxbd8 *rxbdp;
1853 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1854 int i;
1855
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001856 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001858 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1859 if (rx_queue->rx_skbuff[i]) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00001860 dma_unmap_single(priv->dev, rxbdp->bufPtr,
1861 priv->rx_buffer_size,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001862 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001863 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1864 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001866 rxbdp->lstatus = 0;
1867 rxbdp->bufPtr = 0;
1868 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001870 kfree(rx_queue->rx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001871 rx_queue->rx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001872}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001873
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001874/* If there are any tx skbs or rx skbs still around, free them.
Jan Ceuleers0977f812012-06-05 03:42:12 +00001875 * Then free tx_skbuff and rx_skbuff
1876 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001877static void free_skb_resources(struct gfar_private *priv)
1878{
1879 struct gfar_priv_tx_q *tx_queue = NULL;
1880 struct gfar_priv_rx_q *rx_queue = NULL;
1881 int i;
1882
1883 /* Go through all the buffer descriptors and free their data buffers */
1884 for (i = 0; i < priv->num_tx_queues; i++) {
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001885 struct netdev_queue *txq;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001886
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001887 tx_queue = priv->tx_queue[i];
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001888 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001889 if (tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001890 free_skb_tx_queue(tx_queue);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001891 netdev_tx_reset_queue(txq);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001892 }
1893
1894 for (i = 0; i < priv->num_rx_queues; i++) {
1895 rx_queue = priv->rx_queue[i];
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001896 if (rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001897 free_skb_rx_queue(rx_queue);
1898 }
1899
Claudiu Manoil369ec162013-02-14 05:00:02 +00001900 dma_free_coherent(priv->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001901 sizeof(struct txbd8) * priv->total_tx_ring_size +
1902 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1903 priv->tx_queue[0]->tx_bd_base,
1904 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001907void gfar_start(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001908{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001909 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001910 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001911 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001912
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001913 /* Enable Rx/Tx hw queues */
1914 gfar_write(&regs->rqueue, priv->rqueue);
1915 gfar_write(&regs->tqueue, priv->tqueue);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001916
1917 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001918 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001919 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001920 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001921
Kumar Gala0bbaf062005-06-20 10:54:21 -05001922 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001923 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001924 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001925 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001926
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001927 for (i = 0; i < priv->num_grps; i++) {
1928 regs = priv->gfargrp[i].regs;
1929 /* Clear THLT/RHLT, so that the DMA starts polling now */
1930 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1931 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001932 }
Dai Haruki12dea572008-12-16 15:30:20 -08001933
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001934 /* Enable Rx/Tx DMA */
1935 tempval = gfar_read(&regs->maccfg1);
1936 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1937 gfar_write(&regs->maccfg1, tempval);
1938
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001939 gfar_ints_enable(priv);
1940
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001941 priv->ndev->trans_start = jiffies; /* prevent tx timeout */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001942}
1943
Claudiu Manoil80ec3962014-02-24 12:13:44 +02001944static void free_grp_irqs(struct gfar_priv_grp *grp)
1945{
1946 free_irq(gfar_irq(grp, TX)->irq, grp);
1947 free_irq(gfar_irq(grp, RX)->irq, grp);
1948 free_irq(gfar_irq(grp, ER)->irq, grp);
1949}
1950
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001951static int register_grp_irqs(struct gfar_priv_grp *grp)
1952{
1953 struct gfar_private *priv = grp->priv;
1954 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001955 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /* If the device has multiple interrupts, register for
Jan Ceuleers0977f812012-06-05 03:42:12 +00001958 * them. Otherwise, only register for the one
1959 */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001960 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001961 /* Install our interrupt handlers for Error,
Jan Ceuleers0977f812012-06-05 03:42:12 +00001962 * Transmit, and Receive
1963 */
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001964 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1965 gfar_irq(grp, ER)->name, grp);
1966 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001967 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001968 gfar_irq(grp, ER)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001969
Julia Lawall2145f1a2010-08-05 10:26:20 +00001970 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001972 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1973 gfar_irq(grp, TX)->name, grp);
1974 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001975 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001976 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 goto tx_irq_fail;
1978 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001979 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1980 gfar_irq(grp, RX)->name, grp);
1981 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001982 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001983 gfar_irq(grp, RX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 goto rx_irq_fail;
1985 }
1986 } else {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001987 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
1988 gfar_irq(grp, TX)->name, grp);
1989 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001990 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001991 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 goto err_irq_fail;
1993 }
1994 }
1995
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001996 return 0;
1997
1998rx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001999 free_irq(gfar_irq(grp, TX)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002000tx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002001 free_irq(gfar_irq(grp, ER)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002002err_irq_fail:
2003 return err;
2004
2005}
2006
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002007static void gfar_free_irq(struct gfar_private *priv)
2008{
2009 int i;
2010
2011 /* Free the IRQs */
2012 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2013 for (i = 0; i < priv->num_grps; i++)
2014 free_grp_irqs(&priv->gfargrp[i]);
2015 } else {
2016 for (i = 0; i < priv->num_grps; i++)
2017 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
2018 &priv->gfargrp[i]);
2019 }
2020}
2021
2022static int gfar_request_irq(struct gfar_private *priv)
2023{
2024 int err, i, j;
2025
2026 for (i = 0; i < priv->num_grps; i++) {
2027 err = register_grp_irqs(&priv->gfargrp[i]);
2028 if (err) {
2029 for (j = 0; j < i; j++)
2030 free_grp_irqs(&priv->gfargrp[j]);
2031 return err;
2032 }
2033 }
2034
2035 return 0;
2036}
2037
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002038/* Bring the controller up and running */
2039int startup_gfar(struct net_device *ndev)
2040{
2041 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002042 int err;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002043
Claudiu Manoila328ac92014-02-24 12:13:42 +02002044 gfar_mac_reset(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002045
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002046 err = gfar_alloc_skb_resources(ndev);
2047 if (err)
2048 return err;
2049
Claudiu Manoila328ac92014-02-24 12:13:42 +02002050 gfar_init_tx_rx_base(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002051
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002052 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002053 clear_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002054 smp_mb__after_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002055
2056 /* Start Rx/Tx DMA and enable the interrupts */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02002057 gfar_start(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00002059 phy_start(priv->phydev);
2060
Claudiu Manoil08511332014-02-24 12:13:45 +02002061 enable_napi(priv);
2062
2063 netif_tx_wake_all_queues(ndev);
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
Jan Ceuleers0977f812012-06-05 03:42:12 +00002068/* Called when something needs to use the ethernet device
2069 * Returns 0 for success.
2070 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071static int gfar_enet_open(struct net_device *dev)
2072{
Li Yang94e8cc32007-10-12 21:53:51 +08002073 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 int err;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 err = init_phy(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002077 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return err;
2079
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002080 err = gfar_request_irq(priv);
2081 if (err)
2082 return err;
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 err = startup_gfar(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002085 if (err)
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04002086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08002088 device_set_wakeup_enable(&dev->dev, priv->wol_en);
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return err;
2091}
2092
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002093static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002094{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002095 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07002096
2097 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002098
Kumar Gala0bbaf062005-06-20 10:54:21 -05002099 return fcb;
2100}
2101
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002102static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002103 int fcb_length)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002104{
Kumar Gala0bbaf062005-06-20 10:54:21 -05002105 /* If we're here, it's a IP packet with a TCP or UDP
2106 * payload. We set it to checksum, using a pseudo-header
2107 * we provide
2108 */
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00002109 u8 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002110
Jan Ceuleers0977f812012-06-05 03:42:12 +00002111 /* Tell the controller what the protocol is
2112 * And provide the already calculated phcs
2113 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002114 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002115 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03002116 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002117 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05002118 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002119
2120 /* l3os is the distance between the start of the
2121 * frame (skb->data) and the start of the IP hdr.
2122 * l4os is the distance between the start of the
Jan Ceuleers0977f812012-06-05 03:42:12 +00002123 * l3 hdr and the l4 hdr
2124 */
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002125 fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03002126 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002127
Andy Fleming7f7f5312005-11-11 12:38:59 -06002128 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002129}
2130
Andy Fleming7f7f5312005-11-11 12:38:59 -06002131void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002132{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002133 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002134 fcb->vlctl = vlan_tx_tag_get(skb);
2135}
2136
Dai Haruki4669bc92008-12-17 16:51:04 -08002137static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002138 struct txbd8 *base, int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002139{
2140 struct txbd8 *new_bd = bdp + stride;
2141
2142 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2143}
2144
2145static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002146 int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002147{
2148 return skip_txbd(bdp, 1, base, ring_size);
2149}
2150
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002151/* eTSEC12: csum generation not supported for some fcb offsets */
2152static inline bool gfar_csum_errata_12(struct gfar_private *priv,
2153 unsigned long fcb_addr)
2154{
2155 return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
2156 (fcb_addr % 0x20) > 0x18);
2157}
2158
2159/* eTSEC76: csum generation for frames larger than 2500 may
2160 * cause excess delays before start of transmission
2161 */
2162static inline bool gfar_csum_errata_76(struct gfar_private *priv,
2163 unsigned int len)
2164{
2165 return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
2166 (len > 2500));
2167}
2168
Jan Ceuleers0977f812012-06-05 03:42:12 +00002169/* This is called by the kernel when a frame is ready for transmission.
2170 * It is pointed to by the dev->hard_start_xmit function pointer
2171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2173{
2174 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002175 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002176 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002177 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002178 struct txfcb *fcb = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002179 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
Dai Haruki5a5efed2008-12-16 15:34:50 -08002180 u32 lstatus;
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002181 int i, rq = 0;
2182 int do_tstamp, do_csum, do_vlan;
Dai Haruki4669bc92008-12-17 16:51:04 -08002183 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05002184 unsigned long flags;
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002185 unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002186
2187 rq = skb->queue_mapping;
2188 tx_queue = priv->tx_queue[rq];
2189 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002190 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002191 regs = tx_queue->grp->regs;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002192
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002193 do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2194 do_vlan = vlan_tx_tag_present(skb);
2195 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2196 priv->hwts_tx_en;
2197
2198 if (do_csum || do_vlan)
2199 fcb_len = GMAC_FCB_LEN;
2200
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002201 /* check if time stamp should be generated */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002202 if (unlikely(do_tstamp))
2203 fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Dai Haruki4669bc92008-12-17 16:51:04 -08002204
Li Yang5b28bea2009-03-27 15:54:30 -07002205 /* make space for additional header when fcb is needed */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002206 if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002207 struct sk_buff *skb_new;
2208
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002209 skb_new = skb_realloc_headroom(skb, fcb_len);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002210 if (!skb_new) {
2211 dev->stats.tx_errors++;
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002212 dev_kfree_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002213 return NETDEV_TX_OK;
2214 }
Manfred Rudigierdb83d132012-01-09 23:26:50 +00002215
Eric Dumazet313b0372012-07-05 11:45:13 +00002216 if (skb->sk)
2217 skb_set_owner_w(skb_new, skb->sk);
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002218 dev_consume_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002219 skb = skb_new;
2220 }
2221
Dai Haruki4669bc92008-12-17 16:51:04 -08002222 /* total number of fragments in the SKB */
2223 nr_frags = skb_shinfo(skb)->nr_frags;
2224
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002225 /* calculate the required number of TxBDs for this skb */
2226 if (unlikely(do_tstamp))
2227 nr_txbds = nr_frags + 2;
2228 else
2229 nr_txbds = nr_frags + 1;
2230
Dai Haruki4669bc92008-12-17 16:51:04 -08002231 /* check if there is space to queue this packet */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002232 if (nr_txbds > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002233 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002234 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08002235 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08002236 return NETDEV_TX_BUSY;
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* Update transmit stats */
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002240 bytes_sent = skb->len;
2241 tx_queue->stats.tx_bytes += bytes_sent;
2242 /* keep Tx bytes on wire for BQL accounting */
2243 GFAR_CB(skb)->bytes_sent = bytes_sent;
Eric Dumazet1ac9ad12011-01-12 12:13:14 +00002244 tx_queue->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002246 txbdp = txbdp_start = tx_queue->cur_tx;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002247 lstatus = txbdp->lstatus;
2248
2249 /* Time stamp insertion requires one additional TxBD */
2250 if (unlikely(do_tstamp))
2251 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002252 tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Dai Haruki4669bc92008-12-17 16:51:04 -08002254 if (nr_frags == 0) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002255 if (unlikely(do_tstamp))
2256 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002257 TXBD_INTERRUPT);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002258 else
2259 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
Dai Haruki4669bc92008-12-17 16:51:04 -08002260 } else {
2261 /* Place the fragment addresses and lengths into the TxBDs */
2262 for (i = 0; i < nr_frags; i++) {
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002263 unsigned int frag_len;
Dai Haruki4669bc92008-12-17 16:51:04 -08002264 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002265 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002267 frag_len = skb_shinfo(skb)->frags[i].size;
Dai Haruki4669bc92008-12-17 16:51:04 -08002268
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002269 lstatus = txbdp->lstatus | frag_len |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002270 BD_LFLAG(TXBD_READY);
Dai Haruki4669bc92008-12-17 16:51:04 -08002271
2272 /* Handle the last BD specially */
2273 if (i == nr_frags - 1)
2274 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2275
Claudiu Manoil369ec162013-02-14 05:00:02 +00002276 bufaddr = skb_frag_dma_map(priv->dev,
Ian Campbell2234a722011-08-29 23:18:29 +00002277 &skb_shinfo(skb)->frags[i],
2278 0,
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002279 frag_len,
Ian Campbell2234a722011-08-29 23:18:29 +00002280 DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002281
2282 /* set the TxBD length and buffer pointer */
2283 txbdp->bufPtr = bufaddr;
2284 txbdp->lstatus = lstatus;
2285 }
2286
2287 lstatus = txbdp_start->lstatus;
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002290 /* Add TxPAL between FCB and frame if required */
2291 if (unlikely(do_tstamp)) {
2292 skb_push(skb, GMAC_TXPAL_LEN);
2293 memset(skb->data, 0, GMAC_TXPAL_LEN);
2294 }
2295
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002296 /* Add TxFCB if required */
2297 if (fcb_len) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002298 fcb = gfar_add_fcb(skb);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002299 lstatus |= BD_LFLAG(TXBD_TOE);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002300 }
2301
2302 /* Set up checksumming */
2303 if (do_csum) {
2304 gfar_tx_checksum(skb, fcb, fcb_len);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002305
2306 if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
2307 unlikely(gfar_csum_errata_76(priv, skb->len))) {
Alex Dubov4363c2f2011-03-16 17:57:13 +00002308 __skb_pull(skb, GMAC_FCB_LEN);
2309 skb_checksum_help(skb);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002310 if (do_vlan || do_tstamp) {
2311 /* put back a new fcb for vlan/tstamp TOE */
2312 fcb = gfar_add_fcb(skb);
2313 } else {
2314 /* Tx TOE not used */
2315 lstatus &= ~(BD_LFLAG(TXBD_TOE));
2316 fcb = NULL;
2317 }
Alex Dubov4363c2f2011-03-16 17:57:13 +00002318 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002319 }
2320
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002321 if (do_vlan)
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002322 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002323
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002324 /* Setup tx hardware time stamping if requested */
2325 if (unlikely(do_tstamp)) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002326 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002327 fcb->ptp = 1;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002328 }
2329
Claudiu Manoil369ec162013-02-14 05:00:02 +00002330 txbdp_start->bufPtr = dma_map_single(priv->dev, skb->data,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002331 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Jan Ceuleers0977f812012-06-05 03:42:12 +00002333 /* If time stamping is requested one additional TxBD must be set up. The
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002334 * first TxBD points to the FCB and must have a data length of
2335 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2336 * the full frame length.
2337 */
2338 if (unlikely(do_tstamp)) {
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002339 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002340 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002341 (skb_headlen(skb) - fcb_len);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002342 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2343 } else {
2344 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002347 netdev_tx_sent_queue(txq, bytes_sent);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002348
Jan Ceuleers0977f812012-06-05 03:42:12 +00002349 /* We can work in parallel with gfar_clean_tx_ring(), except
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002350 * when modifying num_txbdfree. Note that we didn't grab the lock
2351 * when we were reading the num_txbdfree and checking for available
2352 * space, that's because outside of this function it can only grow,
2353 * and once we've got needed space, it cannot suddenly disappear.
2354 *
2355 * The lock also protects us from gfar_error(), which can modify
2356 * regs->tstat and thus retrigger the transfers, which is why we
2357 * also must grab the lock before setting ready bit for the first
2358 * to be transmitted BD.
2359 */
2360 spin_lock_irqsave(&tx_queue->txlock, flags);
2361
Jan Ceuleers0977f812012-06-05 03:42:12 +00002362 /* The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05002363 * semantics (it requires synchronization between cacheable and
2364 * uncacheable mappings, which eieio doesn't provide and which we
2365 * don't need), thus requiring a more expensive sync instruction. At
2366 * some point, the set of architecture-independent barrier functions
2367 * should be expanded to include weaker barriers.
2368 */
Scott Wood3b6330c2007-05-16 15:06:59 -05002369 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002370
Dai Haruki4669bc92008-12-17 16:51:04 -08002371 txbdp_start->lstatus = lstatus;
2372
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002373 eieio(); /* force lstatus write before tx_skbuff */
2374
2375 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2376
Dai Haruki4669bc92008-12-17 16:51:04 -08002377 /* Update the current skb pointer to the next entry we will use
Jan Ceuleers0977f812012-06-05 03:42:12 +00002378 * (wrapping if necessary)
2379 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002380 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002381 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002382
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002383 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002384
2385 /* reduce TxBD free count */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002386 tx_queue->num_txbdfree -= (nr_txbds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 /* If the next BD still needs to be cleaned up, then the bds
Jan Ceuleers0977f812012-06-05 03:42:12 +00002389 * are full. We need to tell the kernel to stop sending us stuff.
2390 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002391 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002392 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002394 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002398 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002401 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002403 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406/* Stops the kernel queue, and halts the controller */
2407static int gfar_close(struct net_device *dev)
2408{
2409 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002410
Sebastian Siewiorab939902008-08-19 21:12:45 +02002411 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 stop_gfar(dev);
2413
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002414 /* Disconnect from the PHY */
2415 phy_disconnect(priv->phydev);
2416 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002418 gfar_free_irq(priv);
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return 0;
2421}
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002424static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002426 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 return 0;
2429}
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002434 int frame_size = new_mtu + ETH_HLEN;
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Joe Perches59deab22011-06-14 08:57:47 +00002437 netif_err(priv, drv, dev, "Invalid MTU setting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return -EINVAL;
2439 }
2440
Claudiu Manoil08511332014-02-24 12:13:45 +02002441 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2442 cpu_relax();
2443
Claudiu Manoil88302642014-02-24 12:13:43 +02002444 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 stop_gfar(dev);
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 dev->mtu = new_mtu;
2448
Claudiu Manoil88302642014-02-24 12:13:43 +02002449 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 startup_gfar(dev);
2451
Claudiu Manoil08511332014-02-24 12:13:45 +02002452 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 0;
2455}
2456
Claudiu Manoil08511332014-02-24 12:13:45 +02002457void reset_gfar(struct net_device *ndev)
2458{
2459 struct gfar_private *priv = netdev_priv(ndev);
2460
2461 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2462 cpu_relax();
2463
2464 stop_gfar(ndev);
2465 startup_gfar(ndev);
2466
2467 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2468}
2469
Sebastian Siewiorab939902008-08-19 21:12:45 +02002470/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 * transmitted after a set amount of time.
2472 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002473 * starting over will fix the problem.
2474 */
2475static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002477 struct gfar_private *priv = container_of(work, struct gfar_private,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002478 reset_task);
Claudiu Manoil08511332014-02-24 12:13:45 +02002479 reset_gfar(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Sebastian Siewiorab939902008-08-19 21:12:45 +02002482static void gfar_timeout(struct net_device *dev)
2483{
2484 struct gfar_private *priv = netdev_priv(dev);
2485
2486 dev->stats.tx_errors++;
2487 schedule_work(&priv->reset_task);
2488}
2489
Eran Libertyacbc0f02010-07-07 15:54:54 -07002490static void gfar_align_skb(struct sk_buff *skb)
2491{
2492 /* We need the data buffer to be aligned properly. We will reserve
2493 * as many bytes as needed to align the data properly
2494 */
2495 skb_reserve(skb, RXBUF_ALIGNMENT -
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002496 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
Eran Libertyacbc0f02010-07-07 15:54:54 -07002497}
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499/* Interrupt Handler for Transmit complete */
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002500static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002502 struct net_device *dev = tx_queue->dev;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002503 struct netdev_queue *txq;
Dai Harukid080cd62008-04-09 19:37:51 -05002504 struct gfar_private *priv = netdev_priv(dev);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002505 struct txbd8 *bdp, *next = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002506 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002507 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002508 struct sk_buff *skb;
2509 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002510 int tx_ring_size = tx_queue->tx_ring_size;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002511 int frags = 0, nr_txbds = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002512 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002513 int howmany = 0;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002514 int tqi = tx_queue->qindex;
2515 unsigned int bytes_sent = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002516 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002517 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002519 txq = netdev_get_tx_queue(dev, tqi);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002520 bdp = tx_queue->dirty_tx;
2521 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002522
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002523 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002524 unsigned long flags;
2525
Dai Haruki4669bc92008-12-17 16:51:04 -08002526 frags = skb_shinfo(skb)->nr_frags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002527
Jan Ceuleers0977f812012-06-05 03:42:12 +00002528 /* When time stamping, one additional TxBD must be freed.
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002529 * Also, we need to dma_unmap_single() the TxPAL.
2530 */
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002531 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002532 nr_txbds = frags + 2;
2533 else
2534 nr_txbds = frags + 1;
2535
2536 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002537
2538 lstatus = lbdp->lstatus;
2539
2540 /* Only clean completed frames */
2541 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002542 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 break;
2544
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002545 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002546 next = next_txbd(bdp, base, tx_ring_size);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002547 buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002548 } else
2549 buflen = bdp->length;
2550
Claudiu Manoil369ec162013-02-14 05:00:02 +00002551 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002552 buflen, DMA_TO_DEVICE);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002553
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002554 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002555 struct skb_shared_hwtstamps shhwtstamps;
2556 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002557
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002558 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2559 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002560 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002561 skb_tstamp_tx(skb, &shhwtstamps);
2562 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2563 bdp = next;
2564 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002565
2566 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2567 bdp = next_txbd(bdp, base, tx_ring_size);
2568
2569 for (i = 0; i < frags; i++) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00002570 dma_unmap_page(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002571 bdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002572 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2573 bdp = next_txbd(bdp, base, tx_ring_size);
2574 }
2575
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002576 bytes_sent += GFAR_CB(skb)->bytes_sent;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002577
Eric Dumazetacb600d2012-10-05 06:23:55 +00002578 dev_kfree_skb_any(skb);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002579
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002580 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002581
2582 skb_dirtytx = (skb_dirtytx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002583 TX_RING_MOD_MASK(tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002584
Dai Harukid080cd62008-04-09 19:37:51 -05002585 howmany++;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002586 spin_lock_irqsave(&tx_queue->txlock, flags);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002587 tx_queue->num_txbdfree += nr_txbds;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002588 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08002589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Dai Haruki4669bc92008-12-17 16:51:04 -08002591 /* If we freed a buffer, we can restart transmission, if necessary */
Claudiu Manoil08511332014-02-24 12:13:45 +02002592 if (tx_queue->num_txbdfree &&
2593 netif_tx_queue_stopped(txq) &&
2594 !(test_bit(GFAR_DOWN, &priv->state)))
2595 netif_wake_subqueue(priv->ndev, tqi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Dai Haruki4669bc92008-12-17 16:51:04 -08002597 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002598 tx_queue->skb_dirtytx = skb_dirtytx;
2599 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002601 netdev_tx_completed_queue(txq, howmany, bytes_sent);
Dai Harukid080cd62008-04-09 19:37:51 -05002602}
2603
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002604static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002605 struct sk_buff *skb)
Andy Fleming815b97c2008-04-22 17:18:29 -05002606{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002607 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002608 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002609 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002610
Claudiu Manoil369ec162013-02-14 05:00:02 +00002611 buf = dma_map_single(priv->dev, skb->data,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002612 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002613 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002614}
2615
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002616static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
Eran Libertyacbc0f02010-07-07 15:54:54 -07002617{
2618 struct gfar_private *priv = netdev_priv(dev);
Eric Dumazetacb600d2012-10-05 06:23:55 +00002619 struct sk_buff *skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002620
2621 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2622 if (!skb)
2623 return NULL;
2624
2625 gfar_align_skb(skb);
2626
2627 return skb;
2628}
Andy Fleming815b97c2008-04-22 17:18:29 -05002629
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002630struct sk_buff *gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Eric Dumazetacb600d2012-10-05 06:23:55 +00002632 return gfar_alloc_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Li Yang298e1a92007-10-16 14:18:13 +08002635static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Li Yang298e1a92007-10-16 14:18:13 +08002637 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002638 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 struct gfar_extra_stats *estats = &priv->extra_stats;
2640
Jan Ceuleers0977f812012-06-05 03:42:12 +00002641 /* If the packet was truncated, none of the other errors matter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (status & RXBD_TRUNCATED) {
2643 stats->rx_length_errors++;
2644
Paul Gortmaker212079d2013-02-12 15:38:19 -05002645 atomic64_inc(&estats->rx_trunc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 return;
2648 }
2649 /* Count the errors, if there were any */
2650 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2651 stats->rx_length_errors++;
2652
2653 if (status & RXBD_LARGE)
Paul Gortmaker212079d2013-02-12 15:38:19 -05002654 atomic64_inc(&estats->rx_large);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 else
Paul Gortmaker212079d2013-02-12 15:38:19 -05002656 atomic64_inc(&estats->rx_short);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658 if (status & RXBD_NONOCTET) {
2659 stats->rx_frame_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002660 atomic64_inc(&estats->rx_nonoctet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 if (status & RXBD_CRCERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002663 atomic64_inc(&estats->rx_crcerr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 stats->rx_crc_errors++;
2665 }
2666 if (status & RXBD_OVERRUN) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002667 atomic64_inc(&estats->rx_overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 stats->rx_crc_errors++;
2669 }
2670}
2671
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002672irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002674 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2675 unsigned long flags;
2676 u32 imask;
2677
2678 if (likely(napi_schedule_prep(&grp->napi_rx))) {
2679 spin_lock_irqsave(&grp->grplock, flags);
2680 imask = gfar_read(&grp->regs->imask);
2681 imask &= IMASK_RX_DISABLED;
2682 gfar_write(&grp->regs->imask, imask);
2683 spin_unlock_irqrestore(&grp->grplock, flags);
2684 __napi_schedule(&grp->napi_rx);
2685 } else {
2686 /* Clear IEVENT, so interrupts aren't called again
2687 * because of the packets that have already arrived.
2688 */
2689 gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2690 }
2691
2692 return IRQ_HANDLED;
2693}
2694
2695/* Interrupt Handler for Transmit complete */
2696static irqreturn_t gfar_transmit(int irq, void *grp_id)
2697{
2698 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2699 unsigned long flags;
2700 u32 imask;
2701
2702 if (likely(napi_schedule_prep(&grp->napi_tx))) {
2703 spin_lock_irqsave(&grp->grplock, flags);
2704 imask = gfar_read(&grp->regs->imask);
2705 imask &= IMASK_TX_DISABLED;
2706 gfar_write(&grp->regs->imask, imask);
2707 spin_unlock_irqrestore(&grp->grplock, flags);
2708 __napi_schedule(&grp->napi_tx);
2709 } else {
2710 /* Clear IEVENT, so interrupts aren't called again
2711 * because of the packets that have already arrived.
2712 */
2713 gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2714 }
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return IRQ_HANDLED;
2717}
2718
Kumar Gala0bbaf062005-06-20 10:54:21 -05002719static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2720{
2721 /* If valid headers were found, and valid sums
2722 * were verified, then we tell the kernel that no
Jan Ceuleers0977f812012-06-05 03:42:12 +00002723 * checksumming is necessary. Otherwise, it is [FIXME]
2724 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002725 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002726 skb->ip_summed = CHECKSUM_UNNECESSARY;
2727 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002728 skb_checksum_none_assert(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002729}
2730
2731
Jan Ceuleers0977f812012-06-05 03:42:12 +00002732/* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
Claudiu Manoil61db26c2013-02-14 05:00:05 +00002733static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2734 int amount_pull, struct napi_struct *napi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
2736 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002737 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Dai Haruki2c2db482008-12-16 15:31:15 -08002739 /* fcb is at the beginning if exists */
2740 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Jan Ceuleers0977f812012-06-05 03:42:12 +00002742 /* Remove the FCB from the skb
2743 * Remove the padded bytes, if there are any
2744 */
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002745 if (amount_pull) {
2746 skb_record_rx_queue(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002747 skb_pull(skb, amount_pull);
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002748 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002749
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002750 /* Get receive timestamp from the skb */
2751 if (priv->hwts_rx_en) {
2752 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2753 u64 *ns = (u64 *) skb->data;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002754
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002755 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2756 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2757 }
2758
2759 if (priv->padding)
2760 skb_pull(skb, priv->padding);
2761
Michał Mirosław8b3afe92011-04-15 04:50:50 +00002762 if (dev->features & NETIF_F_RXCSUM)
Dai Haruki2c2db482008-12-16 15:31:15 -08002763 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002764
Dai Haruki2c2db482008-12-16 15:31:15 -08002765 /* Tell the skb what kind of packet this is */
2766 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002767
Patrick McHardyf6469682013-04-19 02:04:27 +00002768 /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
David S. Miller823dcd22011-08-20 10:39:12 -07002769 * Even if vlan rx accel is disabled, on some chips
2770 * RXFCB_VLN is pseudo randomly set.
2771 */
Patrick McHardyf6469682013-04-19 02:04:27 +00002772 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
David S. Miller823dcd22011-08-20 10:39:12 -07002773 fcb->flags & RXFCB_VLN)
David S. Millere5905c82013-04-22 19:24:19 -04002774 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
Jiri Pirko87c288c2011-07-20 04:54:19 +00002775
Dai Haruki2c2db482008-12-16 15:31:15 -08002776 /* Send the packet up the stack */
Claudiu Manoil953d2762013-03-21 03:12:15 +00002777 napi_gro_receive(napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
2781/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002782 * until the budget/quota has been reached. Returns the number
2783 * of frames handled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002785int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002787 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002788 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002790 int pkt_len;
2791 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 int howmany = 0;
2793 struct gfar_private *priv = netdev_priv(dev);
2794
2795 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002796 bdp = rx_queue->cur_rx;
2797 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Claudiu Manoilba779712013-02-14 05:00:07 +00002799 amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
Dai Haruki2c2db482008-12-16 15:31:15 -08002800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002802 struct sk_buff *newskb;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002803
Scott Wood3b6330c2007-05-16 15:06:59 -05002804 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002805
2806 /* Add another skb for the future */
2807 newskb = gfar_new_skb(dev);
2808
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002809 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Claudiu Manoil369ec162013-02-14 05:00:02 +00002811 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002812 priv->rx_buffer_size, DMA_FROM_DEVICE);
Andy Fleming81183052008-11-12 10:07:11 -06002813
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002814 if (unlikely(!(bdp->status & RXBD_ERR) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002815 bdp->length > priv->rx_buffer_size))
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002816 bdp->status = RXBD_LARGE;
2817
Andy Fleming815b97c2008-04-22 17:18:29 -05002818 /* We drop the frame if we failed to allocate a new buffer */
2819 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002820 bdp->status & RXBD_ERR)) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002821 count_errors(bdp->status, dev);
2822
2823 if (unlikely(!newskb))
2824 newskb = skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002825 else if (skb)
Eric Dumazetacb600d2012-10-05 06:23:55 +00002826 dev_kfree_skb(skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05002827 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002829 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 howmany++;
2831
Dai Haruki2c2db482008-12-16 15:31:15 -08002832 if (likely(skb)) {
2833 pkt_len = bdp->length - ETH_FCS_LEN;
2834 /* Remove the FCS from the packet length */
2835 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002836 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002837 skb_record_rx_queue(skb, rx_queue->qindex);
Wu Jiajun-B06378cd754a52012-04-19 22:54:35 +00002838 gfar_process_frame(dev, skb, amount_pull,
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002839 &rx_queue->grp->napi_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Dai Haruki2c2db482008-12-16 15:31:15 -08002841 } else {
Joe Perches59deab22011-06-14 08:57:47 +00002842 netif_warn(priv, rx_err, dev, "Missing skb!\n");
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002843 rx_queue->stats.rx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002844 atomic64_inc(&priv->extra_stats.rx_skbmissing);
Dai Haruki2c2db482008-12-16 15:31:15 -08002845 }
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 }
2848
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002849 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Andy Fleming815b97c2008-04-22 17:18:29 -05002851 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002852 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002855 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 /* update to point at the next skb */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002858 rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2859 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
2861
2862 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002863 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return howmany;
2866}
2867
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002868static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002869{
2870 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002871 container_of(napi, struct gfar_priv_grp, napi_rx);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002872 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002873 struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002874 int work_done = 0;
2875
2876 /* Clear IEVENT, so interrupts aren't called again
2877 * because of the packets that have already arrived
2878 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002879 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002880
2881 work_done = gfar_clean_rx_ring(rx_queue, budget);
2882
2883 if (work_done < budget) {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002884 u32 imask;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002885 napi_complete(napi);
2886 /* Clear the halt bit in RSTAT */
2887 gfar_write(&regs->rstat, gfargrp->rstat);
2888
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002889 spin_lock_irq(&gfargrp->grplock);
2890 imask = gfar_read(&regs->imask);
2891 imask |= IMASK_RX_DEFAULT;
2892 gfar_write(&regs->imask, imask);
2893 spin_unlock_irq(&gfargrp->grplock);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002894 }
2895
2896 return work_done;
2897}
2898
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002899static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002901 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002902 container_of(napi, struct gfar_priv_grp, napi_tx);
2903 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002904 struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002905 u32 imask;
2906
2907 /* Clear IEVENT, so interrupts aren't called again
2908 * because of the packets that have already arrived
2909 */
2910 gfar_write(&regs->ievent, IEVENT_TX_MASK);
2911
2912 /* run Tx cleanup to completion */
2913 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
2914 gfar_clean_tx_ring(tx_queue);
2915
2916 napi_complete(napi);
2917
2918 spin_lock_irq(&gfargrp->grplock);
2919 imask = gfar_read(&regs->imask);
2920 imask |= IMASK_TX_DEFAULT;
2921 gfar_write(&regs->imask, imask);
2922 spin_unlock_irq(&gfargrp->grplock);
2923
2924 return 0;
2925}
2926
2927static int gfar_poll_rx(struct napi_struct *napi, int budget)
2928{
2929 struct gfar_priv_grp *gfargrp =
2930 container_of(napi, struct gfar_priv_grp, napi_rx);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002931 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002932 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002933 struct gfar_priv_rx_q *rx_queue = NULL;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002934 int work_done = 0, work_done_per_q = 0;
Claudiu Manoil39c0a0d2013-03-21 03:12:13 +00002935 int i, budget_per_q = 0;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002936 unsigned long rstat_rxf;
2937 int num_act_queues;
Dai Harukid080cd62008-04-09 19:37:51 -05002938
Dai Haruki8c7396a2008-12-17 16:52:00 -08002939 /* Clear IEVENT, so interrupts aren't called again
Jan Ceuleers0977f812012-06-05 03:42:12 +00002940 * because of the packets that have already arrived
2941 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002942 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002943
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002944 rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
2945
2946 num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
2947 if (num_act_queues)
2948 budget_per_q = budget/num_act_queues;
2949
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002950 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2951 /* skip queue if not active */
2952 if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
2953 continue;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002954
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002955 rx_queue = priv->rx_queue[i];
2956 work_done_per_q =
2957 gfar_clean_rx_ring(rx_queue, budget_per_q);
2958 work_done += work_done_per_q;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002959
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002960 /* finished processing this queue */
2961 if (work_done_per_q < budget_per_q) {
2962 /* clear active queue hw indication */
2963 gfar_write(&regs->rstat,
2964 RSTAT_CLEAR_RXF0 >> i);
2965 num_act_queues--;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002966
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002967 if (!num_act_queues)
2968 break;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002969 }
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002970 }
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002971
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002972 if (!num_act_queues) {
2973 u32 imask;
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002974 napi_complete(napi);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002975
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002976 /* Clear the halt bit in RSTAT */
2977 gfar_write(&regs->rstat, gfargrp->rstat);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002978
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002979 spin_lock_irq(&gfargrp->grplock);
2980 imask = gfar_read(&regs->imask);
2981 imask |= IMASK_RX_DEFAULT;
2982 gfar_write(&regs->imask, imask);
2983 spin_unlock_irq(&gfargrp->grplock);
Dai Harukid080cd62008-04-09 19:37:51 -05002984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002986 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002989static int gfar_poll_tx(struct napi_struct *napi, int budget)
2990{
2991 struct gfar_priv_grp *gfargrp =
2992 container_of(napi, struct gfar_priv_grp, napi_tx);
2993 struct gfar_private *priv = gfargrp->priv;
2994 struct gfar __iomem *regs = gfargrp->regs;
2995 struct gfar_priv_tx_q *tx_queue = NULL;
2996 int has_tx_work = 0;
2997 int i;
2998
2999 /* Clear IEVENT, so interrupts aren't called again
3000 * because of the packets that have already arrived
3001 */
3002 gfar_write(&regs->ievent, IEVENT_TX_MASK);
3003
3004 for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
3005 tx_queue = priv->tx_queue[i];
3006 /* run Tx cleanup to completion */
3007 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
3008 gfar_clean_tx_ring(tx_queue);
3009 has_tx_work = 1;
3010 }
3011 }
3012
3013 if (!has_tx_work) {
3014 u32 imask;
3015 napi_complete(napi);
3016
3017 spin_lock_irq(&gfargrp->grplock);
3018 imask = gfar_read(&regs->imask);
3019 imask |= IMASK_TX_DEFAULT;
3020 gfar_write(&regs->imask, imask);
3021 spin_unlock_irq(&gfargrp->grplock);
3022 }
3023
3024 return 0;
3025}
3026
3027
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003028#ifdef CONFIG_NET_POLL_CONTROLLER
Jan Ceuleers0977f812012-06-05 03:42:12 +00003029/* Polling 'interrupt' - used by things like netconsole to send skbs
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003030 * without having to re-enable interrupts. It's not called while
3031 * the interrupt routine is executing.
3032 */
3033static void gfar_netpoll(struct net_device *dev)
3034{
3035 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00003036 int i;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003037
3038 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003039 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003040 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003041 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3042
3043 disable_irq(gfar_irq(grp, TX)->irq);
3044 disable_irq(gfar_irq(grp, RX)->irq);
3045 disable_irq(gfar_irq(grp, ER)->irq);
3046 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3047 enable_irq(gfar_irq(grp, ER)->irq);
3048 enable_irq(gfar_irq(grp, RX)->irq);
3049 enable_irq(gfar_irq(grp, TX)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003050 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003051 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003052 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003053 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3054
3055 disable_irq(gfar_irq(grp, TX)->irq);
3056 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3057 enable_irq(gfar_irq(grp, TX)->irq);
Anton Vorontsov43de0042009-12-09 02:52:19 -08003058 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003059 }
3060}
3061#endif
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003064static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003066 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003069 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003072 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003073 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003076 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003077 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003079 /* Check for errors */
3080 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003081 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
3083 return IRQ_HANDLED;
3084}
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086/* Called every time the controller might need to be made
3087 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003088 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 * function converts those variables into the appropriate
3090 * register values, and can bring down the device if needed.
3091 */
3092static void adjust_link(struct net_device *dev)
3093{
3094 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003095 struct phy_device *phydev = priv->phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003097 if (unlikely(phydev->link != priv->oldlink ||
3098 phydev->duplex != priv->oldduplex ||
3099 phydev->speed != priv->oldspeed))
3100 gfar_update_link_state(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103/* Update the hash table based on the current list of multicast
3104 * addresses we subscribe to. Also, change the promiscuity of
3105 * the device based on the flags (this function is called
Jan Ceuleers0977f812012-06-05 03:42:12 +00003106 * whenever dev->flags is changed
3107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108static void gfar_set_multi(struct net_device *dev)
3109{
Jiri Pirko22bedad32010-04-01 21:22:57 +00003110 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003112 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 u32 tempval;
3114
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003115 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 /* Set RCTRL to PROM */
3117 tempval = gfar_read(&regs->rctrl);
3118 tempval |= RCTRL_PROM;
3119 gfar_write(&regs->rctrl, tempval);
3120 } else {
3121 /* Set RCTRL to not PROM */
3122 tempval = gfar_read(&regs->rctrl);
3123 tempval &= ~(RCTRL_PROM);
3124 gfar_write(&regs->rctrl, tempval);
3125 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003126
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003127 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003129 gfar_write(&regs->igaddr0, 0xffffffff);
3130 gfar_write(&regs->igaddr1, 0xffffffff);
3131 gfar_write(&regs->igaddr2, 0xffffffff);
3132 gfar_write(&regs->igaddr3, 0xffffffff);
3133 gfar_write(&regs->igaddr4, 0xffffffff);
3134 gfar_write(&regs->igaddr5, 0xffffffff);
3135 gfar_write(&regs->igaddr6, 0xffffffff);
3136 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 gfar_write(&regs->gaddr0, 0xffffffff);
3138 gfar_write(&regs->gaddr1, 0xffffffff);
3139 gfar_write(&regs->gaddr2, 0xffffffff);
3140 gfar_write(&regs->gaddr3, 0xffffffff);
3141 gfar_write(&regs->gaddr4, 0xffffffff);
3142 gfar_write(&regs->gaddr5, 0xffffffff);
3143 gfar_write(&regs->gaddr6, 0xffffffff);
3144 gfar_write(&regs->gaddr7, 0xffffffff);
3145 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003146 int em_num;
3147 int idx;
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003150 gfar_write(&regs->igaddr0, 0x0);
3151 gfar_write(&regs->igaddr1, 0x0);
3152 gfar_write(&regs->igaddr2, 0x0);
3153 gfar_write(&regs->igaddr3, 0x0);
3154 gfar_write(&regs->igaddr4, 0x0);
3155 gfar_write(&regs->igaddr5, 0x0);
3156 gfar_write(&regs->igaddr6, 0x0);
3157 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 gfar_write(&regs->gaddr0, 0x0);
3159 gfar_write(&regs->gaddr1, 0x0);
3160 gfar_write(&regs->gaddr2, 0x0);
3161 gfar_write(&regs->gaddr3, 0x0);
3162 gfar_write(&regs->gaddr4, 0x0);
3163 gfar_write(&regs->gaddr5, 0x0);
3164 gfar_write(&regs->gaddr6, 0x0);
3165 gfar_write(&regs->gaddr7, 0x0);
3166
Andy Fleming7f7f5312005-11-11 12:38:59 -06003167 /* If we have extended hash tables, we need to
3168 * clear the exact match registers to prepare for
Jan Ceuleers0977f812012-06-05 03:42:12 +00003169 * setting them
3170 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003171 if (priv->extended_hash) {
3172 em_num = GFAR_EM_NUM + 1;
3173 gfar_clear_exact_match(dev);
3174 idx = 1;
3175 } else {
3176 idx = 0;
3177 em_num = 0;
3178 }
3179
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003180 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return;
3182
3183 /* Parse the list, and set the appropriate bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003184 netdev_for_each_mc_addr(ha, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003185 if (idx < em_num) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00003186 gfar_set_mac_for_addr(dev, idx, ha->addr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003187 idx++;
3188 } else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003189 gfar_set_hash_for_addr(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
3191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
Andy Fleming7f7f5312005-11-11 12:38:59 -06003194
3195/* Clears each of the exact match registers to zero, so they
Jan Ceuleers0977f812012-06-05 03:42:12 +00003196 * don't interfere with normal reception
3197 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003198static void gfar_clear_exact_match(struct net_device *dev)
3199{
3200 int idx;
Joe Perches6a3c9102011-11-16 09:38:02 +00003201 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
Andy Fleming7f7f5312005-11-11 12:38:59 -06003202
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003203 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
Joe Perchesb6bc7652010-12-21 02:16:08 -08003204 gfar_set_mac_for_addr(dev, idx, zero_arr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003205}
3206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207/* Set the appropriate hash bit for the given addr */
3208/* The algorithm works like so:
3209 * 1) Take the Destination Address (ie the multicast address), and
3210 * do a CRC on it (little endian), and reverse the bits of the
3211 * result.
3212 * 2) Use the 8 most significant bits as a hash into a 256-entry
3213 * table. The table is controlled through 8 32-bit registers:
3214 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3215 * gaddr7. This means that the 3 most significant bits in the
3216 * hash index which gaddr register to use, and the 5 other bits
3217 * indicate which bit (assuming an IBM numbering scheme, which
3218 * for PowerPC (tm) is usually the case) in the register holds
Jan Ceuleers0977f812012-06-05 03:42:12 +00003219 * the entry.
3220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3222{
3223 u32 tempval;
3224 struct gfar_private *priv = netdev_priv(dev);
Joe Perches6a3c9102011-11-16 09:38:02 +00003225 u32 result = ether_crc(ETH_ALEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05003226 int width = priv->hash_width;
3227 u8 whichbit = (result >> (32 - width)) & 0x1f;
3228 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 u32 value = (1 << (31-whichbit));
3230
Kumar Gala0bbaf062005-06-20 10:54:21 -05003231 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003233 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
Andy Fleming7f7f5312005-11-11 12:38:59 -06003236
3237/* There are multiple MAC Address register pairs on some controllers
3238 * This function sets the numth pair to a given address
3239 */
Joe Perchesb6bc7652010-12-21 02:16:08 -08003240static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3241 const u8 *addr)
Andy Fleming7f7f5312005-11-11 12:38:59 -06003242{
3243 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003244 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003245 int idx;
Joe Perches6a3c9102011-11-16 09:38:02 +00003246 char tmpbuf[ETH_ALEN];
Andy Fleming7f7f5312005-11-11 12:38:59 -06003247 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003248 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003249
3250 macptr += num*2;
3251
Jan Ceuleers0977f812012-06-05 03:42:12 +00003252 /* Now copy it into the mac registers backwards, cuz
3253 * little endian is silly
3254 */
Joe Perches6a3c9102011-11-16 09:38:02 +00003255 for (idx = 0; idx < ETH_ALEN; idx++)
3256 tmpbuf[ETH_ALEN - 1 - idx] = addr[idx];
Andy Fleming7f7f5312005-11-11 12:38:59 -06003257
3258 gfar_write(macptr, *((u32 *) (tmpbuf)));
3259
3260 tempval = *((u32 *) (tmpbuf + 4));
3261
3262 gfar_write(macptr+1, tempval);
3263}
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003266static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003268 struct gfar_priv_grp *gfargrp = grp_id;
3269 struct gfar __iomem *regs = gfargrp->regs;
3270 struct gfar_private *priv= gfargrp->priv;
3271 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003274 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003277 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05003278
3279 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003280 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05003281 (events & IEVENT_MAG))
3282 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003285 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003286 netdev_dbg(dev,
3287 "error interrupt (ievent=0x%08x imask=0x%08x)\n",
Joe Perches59deab22011-06-14 08:57:47 +00003288 events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 /* Update the error counters */
3291 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003292 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003295 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003297 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (events & IEVENT_XFUN) {
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003299 unsigned long flags;
3300
Joe Perches59deab22011-06-14 08:57:47 +00003301 netif_dbg(priv, tx_err, dev,
3302 "TX FIFO underrun, packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003303 dev->stats.tx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003304 atomic64_inc(&priv->extra_stats.tx_underrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003306 local_irq_save(flags);
3307 lock_tx_qs(priv);
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003310 gfar_write(&regs->tstat, gfargrp->tstat);
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003311
3312 unlock_tx_qs(priv);
3313 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Joe Perches59deab22011-06-14 08:57:47 +00003315 netif_dbg(priv, tx_err, dev, "Transmit Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
3317 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003318 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003319 atomic64_inc(&priv->extra_stats.rx_bsy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003321 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Joe Perches59deab22011-06-14 08:57:47 +00003323 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3324 gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003327 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003328 atomic64_inc(&priv->extra_stats.rx_babr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Joe Perches59deab22011-06-14 08:57:47 +00003330 netif_dbg(priv, rx_err, dev, "babbling RX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
3332 if (events & IEVENT_EBERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003333 atomic64_inc(&priv->extra_stats.eberr);
Joe Perches59deab22011-06-14 08:57:47 +00003334 netif_dbg(priv, rx_err, dev, "bus error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
Joe Perches59deab22011-06-14 08:57:47 +00003336 if (events & IEVENT_RXC)
3337 netif_dbg(priv, rx_status, dev, "control frame\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
3339 if (events & IEVENT_BABT) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003340 atomic64_inc(&priv->extra_stats.tx_babt);
Joe Perches59deab22011-06-14 08:57:47 +00003341 netif_dbg(priv, tx_err, dev, "babbling TX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343 return IRQ_HANDLED;
3344}
3345
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003346static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
3347{
3348 struct phy_device *phydev = priv->phydev;
3349 u32 val = 0;
3350
3351 if (!phydev->duplex)
3352 return val;
3353
3354 if (!priv->pause_aneg_en) {
3355 if (priv->tx_pause_en)
3356 val |= MACCFG1_TX_FLOW;
3357 if (priv->rx_pause_en)
3358 val |= MACCFG1_RX_FLOW;
3359 } else {
3360 u16 lcl_adv, rmt_adv;
3361 u8 flowctrl;
3362 /* get link partner capabilities */
3363 rmt_adv = 0;
3364 if (phydev->pause)
3365 rmt_adv = LPA_PAUSE_CAP;
3366 if (phydev->asym_pause)
3367 rmt_adv |= LPA_PAUSE_ASYM;
3368
3369 lcl_adv = mii_advertise_flowctrl(phydev->advertising);
3370
3371 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
3372 if (flowctrl & FLOW_CTRL_TX)
3373 val |= MACCFG1_TX_FLOW;
3374 if (flowctrl & FLOW_CTRL_RX)
3375 val |= MACCFG1_RX_FLOW;
3376 }
3377
3378 return val;
3379}
3380
3381static noinline void gfar_update_link_state(struct gfar_private *priv)
3382{
3383 struct gfar __iomem *regs = priv->gfargrp[0].regs;
3384 struct phy_device *phydev = priv->phydev;
3385
3386 if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
3387 return;
3388
3389 if (phydev->link) {
3390 u32 tempval1 = gfar_read(&regs->maccfg1);
3391 u32 tempval = gfar_read(&regs->maccfg2);
3392 u32 ecntrl = gfar_read(&regs->ecntrl);
3393
3394 if (phydev->duplex != priv->oldduplex) {
3395 if (!(phydev->duplex))
3396 tempval &= ~(MACCFG2_FULL_DUPLEX);
3397 else
3398 tempval |= MACCFG2_FULL_DUPLEX;
3399
3400 priv->oldduplex = phydev->duplex;
3401 }
3402
3403 if (phydev->speed != priv->oldspeed) {
3404 switch (phydev->speed) {
3405 case 1000:
3406 tempval =
3407 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
3408
3409 ecntrl &= ~(ECNTRL_R100);
3410 break;
3411 case 100:
3412 case 10:
3413 tempval =
3414 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
3415
3416 /* Reduced mode distinguishes
3417 * between 10 and 100
3418 */
3419 if (phydev->speed == SPEED_100)
3420 ecntrl |= ECNTRL_R100;
3421 else
3422 ecntrl &= ~(ECNTRL_R100);
3423 break;
3424 default:
3425 netif_warn(priv, link, priv->ndev,
3426 "Ack! Speed (%d) is not 10/100/1000!\n",
3427 phydev->speed);
3428 break;
3429 }
3430
3431 priv->oldspeed = phydev->speed;
3432 }
3433
3434 tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
3435 tempval1 |= gfar_get_flowctrl_cfg(priv);
3436
3437 gfar_write(&regs->maccfg1, tempval1);
3438 gfar_write(&regs->maccfg2, tempval);
3439 gfar_write(&regs->ecntrl, ecntrl);
3440
3441 if (!priv->oldlink)
3442 priv->oldlink = 1;
3443
3444 } else if (priv->oldlink) {
3445 priv->oldlink = 0;
3446 priv->oldspeed = 0;
3447 priv->oldduplex = -1;
3448 }
3449
3450 if (netif_msg_link(priv))
3451 phy_print_status(phydev);
3452}
3453
Andy Flemingb31a1d82008-12-16 15:29:15 -08003454static struct of_device_id gfar_match[] =
3455{
3456 {
3457 .type = "network",
3458 .compatible = "gianfar",
3459 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003460 {
3461 .compatible = "fsl,etsec2",
3462 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003463 {},
3464};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003465MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467/* Structure for a device driver */
Grant Likely74888762011-02-22 21:05:51 -07003468static struct platform_driver gfar_driver = {
Grant Likely40182942010-04-13 16:13:02 -07003469 .driver = {
3470 .name = "fsl-gianfar",
3471 .owner = THIS_MODULE,
3472 .pm = GFAR_PM_OPS,
3473 .of_match_table = gfar_match,
3474 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 .probe = gfar_probe,
3476 .remove = gfar_remove,
3477};
3478
Axel Lindb62f682011-11-27 16:44:17 +00003479module_platform_driver(gfar_driver);