blob: 5645342f5b288768674d517cdeee40bf5d1fd46d [file] [log] [blame]
Jan Ceuleers0977f812012-06-05 03:42:12 +00001/* drivers/net/ethernet/freescale/gianfar.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Gianfar Ethernet Driver
Andy Fleming7f7f5312005-11-11 12:38:59 -06004 * This driver is designed for the non-CPM ethernet controllers
5 * on the 85xx and 83xx family of integrated processors
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based on 8260_io/fcc_enet.c
7 *
8 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -08009 * Maintainer: Kumar Gala
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000010 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Claudiu Manoil20862782014-02-17 12:53:14 +020012 * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000013 * Copyright 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Gianfar: AKA Lambda Draconis, "Dragon"
21 * RA 11 31 24.2
22 * Dec +69 19 52
23 * V 3.84
24 * B-V +1.62
25 *
26 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050027 *
Andy Flemingb31a1d82008-12-16 15:29:15 -080028 * The driver is initialized through of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050033 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * last descriptor of the ring.
36 *
37 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050038 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * corresponding bit in the IMASK register is also set (if
40 * interrupt coalescing is active, then the interrupt may not
41 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040042 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020044 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050045 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * are none left with data (NAPI will stop after a set number of
47 * packets to give time to other tasks, but will eventually
48 * process all the packets). The data arrives inside a
49 * pre-allocated skb, and so after the skb is passed up to the
50 * stack, a new skb must be allocated, and the address field in
51 * the buffer descriptor must be updated to indicate this new
52 * skb.
53 *
54 * When the kernel requests that a packet be transmitted, the
55 * driver starts where it left off last time, and points the
56 * descriptor at the buffer which was passed in. The driver
57 * then informs the DMA engine that there are packets ready to
58 * be transmitted. Once the controller is finished transmitting
59 * the packet, an interrupt may be triggered (under the same
60 * conditions as for reception, but depending on the TXF bit).
61 * The driver then cleans up the buffer.
62 */
63
Joe Perches59deab22011-06-14 08:57:47 +000064#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65#define DEBUG
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/string.h>
69#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040070#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/slab.h>
72#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/delay.h>
74#include <linux/netdevice.h>
75#include <linux/etherdevice.h>
76#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050077#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/spinlock.h>
79#include <linux/mm.h>
Rob Herring5af50732013-09-17 14:28:33 -050080#include <linux/of_address.h>
81#include <linux/of_irq.h>
Grant Likelyfe192a42009-04-25 12:53:12 +000082#include <linux/of_mdio.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080083#include <linux/of_platform.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050084#include <linux/ip.h>
85#include <linux/tcp.h>
86#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080087#include <linux/in.h>
Manfred Rudigiercc772ab2010-04-08 23:10:03 +000088#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#include <asm/io.h>
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +030091#ifdef CONFIG_PPC
Anton Vorontsov7d350972010-06-30 06:39:12 +000092#include <asm/reg.h>
Claudiu Manoil2969b1f2013-10-09 20:20:41 +030093#include <asm/mpc85xx.h>
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +030094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <asm/irq.h>
96#include <asm/uaccess.h>
97#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/dma-mapping.h>
99#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400100#include <linux/mii.h>
101#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -0800102#include <linux/phy_fixed.h>
103#include <linux/of.h>
David Daney4b6ba8a2010-10-26 15:07:13 -0700104#include <linux/of_net.h>
Claudiu Manoilfd31a952014-10-07 10:44:31 +0300105#include <linux/of_address.h>
106#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#include "gianfar.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Andy Fleming7f7f5312005-11-11 12:38:59 -0600112const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int gfar_enet_open(struct net_device *dev);
115static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200116static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static void gfar_timeout(struct net_device *dev);
118static int gfar_close(struct net_device *dev);
Kevin Hao0a4b5a22014-12-11 14:08:41 +0800119struct sk_buff *gfar_new_skb(struct net_device *dev, dma_addr_t *bufaddr);
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
Claudiu Manoild55398b2014-10-07 10:44:35 +0300166 gfar_wmb();
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000167
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);
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200174 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000175 struct gfar_priv_tx_q *tx_queue = NULL;
176 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000177 struct txbd8 *txbdp;
178 struct rxbd8 *rxbdp;
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200179 u32 *rfbptr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000180 int i, j;
Kevin Hao0a4b5a22014-12-11 14:08:41 +0800181 dma_addr_t bufaddr;
Anton Vorontsov87283272009-10-12 06:00:39 +0000182
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000183 for (i = 0; i < priv->num_tx_queues; i++) {
184 tx_queue = priv->tx_queue[i];
185 /* Initialize some variables in our dev structure */
186 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
187 tx_queue->dirty_tx = tx_queue->tx_bd_base;
188 tx_queue->cur_tx = tx_queue->tx_bd_base;
189 tx_queue->skb_curtx = 0;
190 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000191
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000192 /* Initialize Transmit Descriptor Ring */
193 txbdp = tx_queue->tx_bd_base;
194 for (j = 0; j < tx_queue->tx_ring_size; j++) {
195 txbdp->lstatus = 0;
196 txbdp->bufPtr = 0;
197 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000198 }
199
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000200 /* Set the last descriptor in the ring to indicate wrap */
201 txbdp--;
202 txbdp->status |= TXBD_WRAP;
203 }
204
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200205 rfbptr = &regs->rfbptr0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000206 for (i = 0; i < priv->num_rx_queues; i++) {
207 rx_queue = priv->rx_queue[i];
208 rx_queue->cur_rx = rx_queue->rx_bd_base;
209 rx_queue->skb_currx = 0;
210 rxbdp = rx_queue->rx_bd_base;
211
212 for (j = 0; j < rx_queue->rx_ring_size; j++) {
213 struct sk_buff *skb = rx_queue->rx_skbuff[j];
214
215 if (skb) {
Kevin Hao0a4b5a22014-12-11 14:08:41 +0800216 bufaddr = rxbdp->bufPtr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000217 } else {
Kevin Hao0a4b5a22014-12-11 14:08:41 +0800218 skb = gfar_new_skb(ndev, &bufaddr);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000219 if (!skb) {
Joe Perches59deab22011-06-14 08:57:47 +0000220 netdev_err(ndev, "Can't allocate RX buffers\n");
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +0000221 return -ENOMEM;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000222 }
223 rx_queue->rx_skbuff[j] = skb;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000224 }
225
Kevin Hao0a4b5a22014-12-11 14:08:41 +0800226 gfar_init_rxbdp(rx_queue, rxbdp, bufaddr);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000227 rxbdp++;
228 }
229
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200230 rx_queue->rfbptr = rfbptr;
231 rfbptr += 2;
Anton Vorontsov87283272009-10-12 06:00:39 +0000232 }
233
234 return 0;
235}
236
237static int gfar_alloc_skb_resources(struct net_device *ndev)
238{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000239 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000240 dma_addr_t addr;
241 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000242 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil369ec162013-02-14 05:00:02 +0000243 struct device *dev = priv->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000244 struct gfar_priv_tx_q *tx_queue = NULL;
245 struct gfar_priv_rx_q *rx_queue = NULL;
246
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000247 priv->total_tx_ring_size = 0;
248 for (i = 0; i < priv->num_tx_queues; i++)
249 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
250
251 priv->total_rx_ring_size = 0;
252 for (i = 0; i < priv->num_rx_queues; i++)
253 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000254
255 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000256 vaddr = dma_alloc_coherent(dev,
Joe Perchesd0320f72013-03-14 13:07:21 +0000257 (priv->total_tx_ring_size *
258 sizeof(struct txbd8)) +
259 (priv->total_rx_ring_size *
260 sizeof(struct rxbd8)),
261 &addr, GFP_KERNEL);
262 if (!vaddr)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000263 return -ENOMEM;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000264
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000265 for (i = 0; i < priv->num_tx_queues; i++) {
266 tx_queue = priv->tx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000267 tx_queue->tx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000268 tx_queue->tx_bd_dma_base = addr;
269 tx_queue->dev = ndev;
270 /* enet DMA only understands physical addresses */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000271 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
272 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000273 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000274
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000275 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000276 for (i = 0; i < priv->num_rx_queues; i++) {
277 rx_queue = priv->rx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000278 rx_queue->rx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000279 rx_queue->rx_bd_dma_base = addr;
280 rx_queue->dev = ndev;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000281 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
282 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000283 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000284
285 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000286 for (i = 0; i < priv->num_tx_queues; i++) {
287 tx_queue = priv->tx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000288 tx_queue->tx_skbuff =
289 kmalloc_array(tx_queue->tx_ring_size,
290 sizeof(*tx_queue->tx_skbuff),
291 GFP_KERNEL);
292 if (!tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000293 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000294
295 for (k = 0; k < tx_queue->tx_ring_size; k++)
296 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000297 }
298
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000299 for (i = 0; i < priv->num_rx_queues; i++) {
300 rx_queue = priv->rx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000301 rx_queue->rx_skbuff =
302 kmalloc_array(rx_queue->rx_ring_size,
303 sizeof(*rx_queue->rx_skbuff),
304 GFP_KERNEL);
305 if (!rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000306 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000307
308 for (j = 0; j < rx_queue->rx_ring_size; j++)
309 rx_queue->rx_skbuff[j] = NULL;
310 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000311
Anton Vorontsov87283272009-10-12 06:00:39 +0000312 if (gfar_init_bds(ndev))
313 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000314
315 return 0;
316
317cleanup:
318 free_skb_resources(priv);
319 return -ENOMEM;
320}
321
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000322static void gfar_init_tx_rx_base(struct gfar_private *priv)
323{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000324 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000325 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000326 int i;
327
328 baddr = &regs->tbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000329 for (i = 0; i < priv->num_tx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000330 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000331 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000332 }
333
334 baddr = &regs->rbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000335 for (i = 0; i < priv->num_rx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000336 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000337 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000338 }
339}
340
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200341static void gfar_init_rqprm(struct gfar_private *priv)
342{
343 struct gfar __iomem *regs = priv->gfargrp[0].regs;
344 u32 __iomem *baddr;
345 int i;
346
347 baddr = &regs->rqprm0;
348 for (i = 0; i < priv->num_rx_queues; i++) {
349 gfar_write(baddr, priv->rx_queue[i]->rx_ring_size |
350 (DEFAULT_RX_LFC_THR << FBTHR_SHIFT));
351 baddr++;
352 }
353}
354
Claudiu Manoil88302642014-02-24 12:13:43 +0200355static void gfar_rx_buff_size_config(struct gfar_private *priv)
356{
Claudiu Manoilf5b720b2014-10-15 19:11:46 +0300357 int frame_size = priv->ndev->mtu + ETH_HLEN + ETH_FCS_LEN;
Claudiu Manoil88302642014-02-24 12:13:43 +0200358
359 /* set this when rx hw offload (TOE) functions are being used */
360 priv->uses_rxfcb = 0;
361
362 if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
363 priv->uses_rxfcb = 1;
364
365 if (priv->hwts_rx_en)
366 priv->uses_rxfcb = 1;
367
368 if (priv->uses_rxfcb)
369 frame_size += GMAC_FCB_LEN;
370
371 frame_size += priv->padding;
372
373 frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
374 INCREMENTAL_BUFFER_SIZE;
375
376 priv->rx_buffer_size = frame_size;
377}
378
Claudiu Manoila328ac92014-02-24 12:13:42 +0200379static void gfar_mac_rx_config(struct gfar_private *priv)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000380{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000381 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000382 u32 rctrl = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000383
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000384 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000385 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000386 /* Program the RIR0 reg with the required distribution */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200387 if (priv->poll_mode == GFAR_SQ_POLLING)
388 gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
389 else /* GFAR_MQ_POLLING */
390 gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000391 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000392
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000393 /* Restore PROMISC mode */
Claudiu Manoila328ac92014-02-24 12:13:42 +0200394 if (priv->ndev->flags & IFF_PROMISC)
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000395 rctrl |= RCTRL_PROM;
396
Claudiu Manoil88302642014-02-24 12:13:43 +0200397 if (priv->ndev->features & NETIF_F_RXCSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000398 rctrl |= RCTRL_CHECKSUMMING;
399
Claudiu Manoil88302642014-02-24 12:13:43 +0200400 if (priv->extended_hash)
401 rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000402
403 if (priv->padding) {
404 rctrl &= ~RCTRL_PAL_MASK;
405 rctrl |= RCTRL_PADDING(priv->padding);
406 }
407
Manfred Rudigier97553f72010-06-11 01:49:05 +0000408 /* Enable HW time stamping if requested from user space */
Claudiu Manoil88302642014-02-24 12:13:43 +0200409 if (priv->hwts_rx_en)
Manfred Rudigier97553f72010-06-11 01:49:05 +0000410 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
411
Claudiu Manoil88302642014-02-24 12:13:43 +0200412 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
Sebastian Pöhnb852b722011-07-26 00:03:13 +0000413 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000414
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200415 /* Clear the LFC bit */
416 gfar_write(&regs->rctrl, rctrl);
417 /* Init flow control threshold values */
418 gfar_init_rqprm(priv);
419 gfar_write(&regs->ptv, DEFAULT_LFC_PTVVAL);
420 rctrl |= RCTRL_LFC;
421
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000422 /* Init rctrl based on our settings */
423 gfar_write(&regs->rctrl, rctrl);
Claudiu Manoila328ac92014-02-24 12:13:42 +0200424}
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000425
Claudiu Manoila328ac92014-02-24 12:13:42 +0200426static void gfar_mac_tx_config(struct gfar_private *priv)
427{
428 struct gfar __iomem *regs = priv->gfargrp[0].regs;
429 u32 tctrl = 0;
430
431 if (priv->ndev->features & NETIF_F_IP_CSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000432 tctrl |= TCTRL_INIT_CSUM;
433
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +0000434 if (priv->prio_sched_en)
435 tctrl |= TCTRL_TXSCHED_PRIO;
436 else {
437 tctrl |= TCTRL_TXSCHED_WRRS;
438 gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
439 gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
440 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000441
Claudiu Manoil88302642014-02-24 12:13:43 +0200442 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
443 tctrl |= TCTRL_VLINS;
444
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000445 gfar_write(&regs->tctrl, tctrl);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000446}
447
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200448static void gfar_configure_coalescing(struct gfar_private *priv,
449 unsigned long tx_mask, unsigned long rx_mask)
450{
451 struct gfar __iomem *regs = priv->gfargrp[0].regs;
452 u32 __iomem *baddr;
453
454 if (priv->mode == MQ_MG_MODE) {
455 int i = 0;
456
457 baddr = &regs->txic0;
458 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
459 gfar_write(baddr + i, 0);
460 if (likely(priv->tx_queue[i]->txcoalescing))
461 gfar_write(baddr + i, priv->tx_queue[i]->txic);
462 }
463
464 baddr = &regs->rxic0;
465 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
466 gfar_write(baddr + i, 0);
467 if (likely(priv->rx_queue[i]->rxcoalescing))
468 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
469 }
470 } else {
471 /* Backward compatible case -- even if we enable
472 * multiple queues, there's only single reg to program
473 */
474 gfar_write(&regs->txic, 0);
475 if (likely(priv->tx_queue[0]->txcoalescing))
476 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
477
478 gfar_write(&regs->rxic, 0);
479 if (unlikely(priv->rx_queue[0]->rxcoalescing))
480 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
481 }
482}
483
484void gfar_configure_coalescing_all(struct gfar_private *priv)
485{
486 gfar_configure_coalescing(priv, 0xFF, 0xFF);
487}
488
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000489static struct net_device_stats *gfar_get_stats(struct net_device *dev)
490{
491 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000492 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
493 unsigned long tx_packets = 0, tx_bytes = 0;
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000494 int i;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000495
496 for (i = 0; i < priv->num_rx_queues; i++) {
497 rx_packets += priv->rx_queue[i]->stats.rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000498 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000499 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
500 }
501
502 dev->stats.rx_packets = rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000503 dev->stats.rx_bytes = rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000504 dev->stats.rx_dropped = rx_dropped;
505
506 for (i = 0; i < priv->num_tx_queues; i++) {
Eric Dumazet1ac9ad12011-01-12 12:13:14 +0000507 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
508 tx_packets += priv->tx_queue[i]->stats.tx_packets;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000509 }
510
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000511 dev->stats.tx_bytes = tx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000512 dev->stats.tx_packets = tx_packets;
513
514 return &dev->stats;
515}
516
Andy Fleming26ccfc32009-03-10 12:58:28 +0000517static const struct net_device_ops gfar_netdev_ops = {
518 .ndo_open = gfar_enet_open,
519 .ndo_start_xmit = gfar_start_xmit,
520 .ndo_stop = gfar_close,
521 .ndo_change_mtu = gfar_change_mtu,
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000522 .ndo_set_features = gfar_set_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000523 .ndo_set_rx_mode = gfar_set_multi,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000524 .ndo_tx_timeout = gfar_timeout,
525 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000526 .ndo_get_stats = gfar_get_stats,
Ben Hutchings240c1022009-07-09 17:54:35 +0000527 .ndo_set_mac_address = eth_mac_addr,
528 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000529#ifdef CONFIG_NET_POLL_CONTROLLER
530 .ndo_poll_controller = gfar_netpoll,
531#endif
532};
533
Claudiu Manoilefeddce2014-02-17 12:53:17 +0200534static void gfar_ints_disable(struct gfar_private *priv)
535{
536 int i;
537 for (i = 0; i < priv->num_grps; i++) {
538 struct gfar __iomem *regs = priv->gfargrp[i].regs;
539 /* Clear IEVENT */
540 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
541
542 /* Initialize IMASK */
543 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
544 }
545}
546
547static void gfar_ints_enable(struct gfar_private *priv)
548{
549 int i;
550 for (i = 0; i < priv->num_grps; i++) {
551 struct gfar __iomem *regs = priv->gfargrp[i].regs;
552 /* Unmask the interrupts we look for */
553 gfar_write(&regs->imask, IMASK_DEFAULT);
554 }
555}
556
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000557void lock_tx_qs(struct gfar_private *priv)
558{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000559 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000560
561 for (i = 0; i < priv->num_tx_queues; i++)
562 spin_lock(&priv->tx_queue[i]->txlock);
563}
564
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000565void unlock_tx_qs(struct gfar_private *priv)
566{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000567 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000568
569 for (i = 0; i < priv->num_tx_queues; i++)
570 spin_unlock(&priv->tx_queue[i]->txlock);
571}
572
Claudiu Manoil20862782014-02-17 12:53:14 +0200573static int gfar_alloc_tx_queues(struct gfar_private *priv)
574{
575 int i;
576
577 for (i = 0; i < priv->num_tx_queues; i++) {
578 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
579 GFP_KERNEL);
580 if (!priv->tx_queue[i])
581 return -ENOMEM;
582
583 priv->tx_queue[i]->tx_skbuff = NULL;
584 priv->tx_queue[i]->qindex = i;
585 priv->tx_queue[i]->dev = priv->ndev;
586 spin_lock_init(&(priv->tx_queue[i]->txlock));
587 }
588 return 0;
589}
590
591static int gfar_alloc_rx_queues(struct gfar_private *priv)
592{
593 int i;
594
595 for (i = 0; i < priv->num_rx_queues; i++) {
596 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
597 GFP_KERNEL);
598 if (!priv->rx_queue[i])
599 return -ENOMEM;
600
601 priv->rx_queue[i]->rx_skbuff = NULL;
602 priv->rx_queue[i]->qindex = i;
603 priv->rx_queue[i]->dev = priv->ndev;
Claudiu Manoil20862782014-02-17 12:53:14 +0200604 }
605 return 0;
606}
607
608static void gfar_free_tx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000609{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000610 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000611
612 for (i = 0; i < priv->num_tx_queues; i++)
613 kfree(priv->tx_queue[i]);
614}
615
Claudiu Manoil20862782014-02-17 12:53:14 +0200616static void gfar_free_rx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000617{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000618 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000619
620 for (i = 0; i < priv->num_rx_queues; i++)
621 kfree(priv->rx_queue[i]);
622}
623
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000624static void unmap_group_regs(struct gfar_private *priv)
625{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000626 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000627
628 for (i = 0; i < MAXGROUPS; i++)
629 if (priv->gfargrp[i].regs)
630 iounmap(priv->gfargrp[i].regs);
631}
632
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000633static void free_gfar_dev(struct gfar_private *priv)
634{
635 int i, j;
636
637 for (i = 0; i < priv->num_grps; i++)
638 for (j = 0; j < GFAR_NUM_IRQS; j++) {
639 kfree(priv->gfargrp[i].irqinfo[j]);
640 priv->gfargrp[i].irqinfo[j] = NULL;
641 }
642
643 free_netdev(priv->ndev);
644}
645
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000646static void disable_napi(struct gfar_private *priv)
647{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000648 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000649
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200650 for (i = 0; i < priv->num_grps; i++) {
651 napi_disable(&priv->gfargrp[i].napi_rx);
652 napi_disable(&priv->gfargrp[i].napi_tx);
653 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000654}
655
656static void enable_napi(struct gfar_private *priv)
657{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000658 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000659
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200660 for (i = 0; i < priv->num_grps; i++) {
661 napi_enable(&priv->gfargrp[i].napi_rx);
662 napi_enable(&priv->gfargrp[i].napi_tx);
663 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000664}
665
666static int gfar_parse_group(struct device_node *np,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000667 struct gfar_private *priv, const char *model)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000668{
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000669 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000670 int i;
671
Paul Gortmaker7c1e7e92013-02-04 09:49:42 +0000672 for (i = 0; i < GFAR_NUM_IRQS; i++) {
673 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
674 GFP_KERNEL);
675 if (!grp->irqinfo[i])
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000676 return -ENOMEM;
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000677 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000678
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000679 grp->regs = of_iomap(np, 0);
680 if (!grp->regs)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000681 return -ENOMEM;
682
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000683 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000684
685 /* If we aren't the FEC we have multiple interrupts */
686 if (model && strcasecmp(model, "FEC")) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000687 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
688 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
689 if (gfar_irq(grp, TX)->irq == NO_IRQ ||
690 gfar_irq(grp, RX)->irq == NO_IRQ ||
691 gfar_irq(grp, ER)->irq == NO_IRQ)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000692 return -EINVAL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000693 }
694
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000695 grp->priv = priv;
696 spin_lock_init(&grp->grplock);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000697 if (priv->mode == MQ_MG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200698 u32 *rxq_mask, *txq_mask;
699 rxq_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
700 txq_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
701
702 if (priv->poll_mode == GFAR_SQ_POLLING) {
703 /* One Q per interrupt group: Q0 to G0, Q1 to G1 */
704 grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
705 grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
706 } else { /* GFAR_MQ_POLLING */
707 grp->rx_bit_map = rxq_mask ?
708 *rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
709 grp->tx_bit_map = txq_mask ?
710 *txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
711 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000712 } else {
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000713 grp->rx_bit_map = 0xFF;
714 grp->tx_bit_map = 0xFF;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000715 }
Claudiu Manoil20862782014-02-17 12:53:14 +0200716
717 /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
718 * right to left, so we need to revert the 8 bits to get the q index
719 */
720 grp->rx_bit_map = bitrev8(grp->rx_bit_map);
721 grp->tx_bit_map = bitrev8(grp->tx_bit_map);
722
723 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
724 * also assign queues to groups
725 */
726 for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200727 if (!grp->rx_queue)
728 grp->rx_queue = priv->rx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200729 grp->num_rx_queues++;
730 grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
731 priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
732 priv->rx_queue[i]->grp = grp;
733 }
734
735 for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200736 if (!grp->tx_queue)
737 grp->tx_queue = priv->tx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200738 grp->num_tx_queues++;
739 grp->tstat |= (TSTAT_CLEAR_THALT >> i);
740 priv->tqueue |= (TQUEUE_EN0 >> i);
741 priv->tx_queue[i]->grp = grp;
742 }
743
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000744 priv->num_grps++;
745
746 return 0;
747}
748
Grant Likely2dc11582010-08-06 09:25:50 -0600749static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800750{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800751 const char *model;
752 const char *ctype;
753 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000754 int err = 0, i;
755 struct net_device *dev = NULL;
756 struct gfar_private *priv = NULL;
Grant Likely61c7a082010-04-13 16:12:29 -0700757 struct device_node *np = ofdev->dev.of_node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000758 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800759 const u32 *stash;
760 const u32 *stash_len;
761 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000762 unsigned int num_tx_qs, num_rx_qs;
763 u32 *tx_queues, *rx_queues;
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200764 unsigned short mode, poll_mode;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800765
766 if (!np || !of_device_is_available(np))
767 return -ENODEV;
768
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200769 if (of_device_is_compatible(np, "fsl,etsec2")) {
770 mode = MQ_MG_MODE;
771 poll_mode = GFAR_SQ_POLLING;
772 } else {
773 mode = SQ_SG_MODE;
774 poll_mode = GFAR_SQ_POLLING;
775 }
776
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200777 /* parse the num of HW tx and rx queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000778 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200779 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
780
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200781 if (mode == SQ_SG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200782 num_tx_qs = 1;
783 num_rx_qs = 1;
784 } else { /* MQ_MG_MODE */
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200785 /* get the actual number of supported groups */
786 unsigned int num_grps = of_get_available_child_count(np);
787
788 if (num_grps == 0 || num_grps > MAXGROUPS) {
789 dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
790 num_grps);
791 pr_err("Cannot do alloc_etherdev, aborting\n");
792 return -EINVAL;
793 }
794
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200795 if (poll_mode == GFAR_SQ_POLLING) {
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200796 num_tx_qs = num_grps; /* one txq per int group */
797 num_rx_qs = num_grps; /* one rxq per int group */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200798 } else { /* GFAR_MQ_POLLING */
799 num_tx_qs = tx_queues ? *tx_queues : 1;
800 num_rx_qs = rx_queues ? *rx_queues : 1;
801 }
802 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000803
804 if (num_tx_qs > MAX_TX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000805 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
806 num_tx_qs, MAX_TX_QS);
807 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000808 return -EINVAL;
809 }
810
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000811 if (num_rx_qs > MAX_RX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000812 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
813 num_rx_qs, MAX_RX_QS);
814 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000815 return -EINVAL;
816 }
817
818 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
819 dev = *pdev;
820 if (NULL == dev)
821 return -ENOMEM;
822
823 priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000824 priv->ndev = dev;
825
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200826 priv->mode = mode;
827 priv->poll_mode = poll_mode;
828
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000829 priv->num_tx_queues = num_tx_qs;
Ben Hutchingsfe069122010-09-27 08:27:37 +0000830 netif_set_real_num_rx_queues(dev, num_rx_qs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000831 priv->num_rx_queues = num_rx_qs;
Claudiu Manoil20862782014-02-17 12:53:14 +0200832
833 err = gfar_alloc_tx_queues(priv);
834 if (err)
835 goto tx_alloc_failed;
836
837 err = gfar_alloc_rx_queues(priv);
838 if (err)
839 goto rx_alloc_failed;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800840
Jan Ceuleers0977f812012-06-05 03:42:12 +0000841 /* Init Rx queue filer rule set linked list */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700842 INIT_LIST_HEAD(&priv->rx_list.list);
843 priv->rx_list.count = 0;
844 mutex_init(&priv->rx_queue_access);
845
Andy Flemingb31a1d82008-12-16 15:29:15 -0800846 model = of_get_property(np, "model", NULL);
847
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000848 for (i = 0; i < MAXGROUPS; i++)
849 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800850
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000851 /* Parse and initialize group specific information */
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200852 if (priv->mode == MQ_MG_MODE) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000853 for_each_child_of_node(np, child) {
854 err = gfar_parse_group(child, priv, model);
855 if (err)
856 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800857 }
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200858 } else { /* SQ_SG_MODE */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000859 err = gfar_parse_group(np, priv, model);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000860 if (err)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000861 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800862 }
863
Andy Fleming4d7902f2009-02-04 16:43:44 -0800864 stash = of_get_property(np, "bd-stash", NULL);
865
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000866 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800867 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
868 priv->bd_stash_en = 1;
869 }
870
871 stash_len = of_get_property(np, "rx-stash-len", NULL);
872
873 if (stash_len)
874 priv->rx_stash_size = *stash_len;
875
876 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
877
878 if (stash_idx)
879 priv->rx_stash_index = *stash_idx;
880
881 if (stash_len || stash_idx)
882 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
883
Andy Flemingb31a1d82008-12-16 15:29:15 -0800884 mac_addr = of_get_mac_address(np);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000885
Andy Flemingb31a1d82008-12-16 15:29:15 -0800886 if (mac_addr)
Joe Perches6a3c910c2011-11-16 09:38:02 +0000887 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800888
889 if (model && !strcasecmp(model, "TSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200890 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000891 FSL_GIANFAR_DEV_HAS_COALESCE |
892 FSL_GIANFAR_DEV_HAS_RMON |
893 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
894
Andy Flemingb31a1d82008-12-16 15:29:15 -0800895 if (model && !strcasecmp(model, "eTSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200896 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000897 FSL_GIANFAR_DEV_HAS_COALESCE |
898 FSL_GIANFAR_DEV_HAS_RMON |
899 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000900 FSL_GIANFAR_DEV_HAS_CSUM |
901 FSL_GIANFAR_DEV_HAS_VLAN |
902 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
903 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
904 FSL_GIANFAR_DEV_HAS_TIMER;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800905
906 ctype = of_get_property(np, "phy-connection-type", NULL);
907
908 /* We only care about rgmii-id. The rest are autodetected */
909 if (ctype && !strcmp(ctype, "rgmii-id"))
910 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
911 else
912 priv->interface = PHY_INTERFACE_MODE_MII;
913
914 if (of_get_property(np, "fsl,magic-packet", NULL))
915 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
916
Grant Likelyfe192a42009-04-25 12:53:12 +0000917 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800918
Florian Fainellibe403642014-05-22 09:47:48 -0700919 /* In the case of a fixed PHY, the DT node associated
920 * to the PHY is the Ethernet MAC DT node.
921 */
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200922 if (!priv->phy_node && of_phy_is_fixed_link(np)) {
Florian Fainellibe403642014-05-22 09:47:48 -0700923 err = of_phy_register_fixed_link(np);
924 if (err)
925 goto err_grp_init;
926
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200927 priv->phy_node = of_node_get(np);
Florian Fainellibe403642014-05-22 09:47:48 -0700928 }
929
Andy Flemingb31a1d82008-12-16 15:29:15 -0800930 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000931 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800932
933 return 0;
934
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000935err_grp_init:
936 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +0200937rx_alloc_failed:
938 gfar_free_rx_queues(priv);
939tx_alloc_failed:
940 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000941 free_gfar_dev(priv);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800942 return err;
943}
944
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000945static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000946{
947 struct hwtstamp_config config;
948 struct gfar_private *priv = netdev_priv(netdev);
949
950 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
951 return -EFAULT;
952
953 /* reserved for future extensions */
954 if (config.flags)
955 return -EINVAL;
956
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000957 switch (config.tx_type) {
958 case HWTSTAMP_TX_OFF:
959 priv->hwts_tx_en = 0;
960 break;
961 case HWTSTAMP_TX_ON:
962 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
963 return -ERANGE;
964 priv->hwts_tx_en = 1;
965 break;
966 default:
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000967 return -ERANGE;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000968 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000969
970 switch (config.rx_filter) {
971 case HWTSTAMP_FILTER_NONE:
Manfred Rudigier97553f72010-06-11 01:49:05 +0000972 if (priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000973 priv->hwts_rx_en = 0;
Claudiu Manoil08511332014-02-24 12:13:45 +0200974 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000975 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000976 break;
977 default:
978 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
979 return -ERANGE;
Manfred Rudigier97553f72010-06-11 01:49:05 +0000980 if (!priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000981 priv->hwts_rx_en = 1;
Claudiu Manoil08511332014-02-24 12:13:45 +0200982 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000983 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000984 config.rx_filter = HWTSTAMP_FILTER_ALL;
985 break;
986 }
987
988 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
989 -EFAULT : 0;
990}
991
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000992static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
993{
994 struct hwtstamp_config config;
995 struct gfar_private *priv = netdev_priv(netdev);
996
997 config.flags = 0;
998 config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
999 config.rx_filter = (priv->hwts_rx_en ?
1000 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
1001
1002 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1003 -EFAULT : 0;
1004}
1005
Clifford Wolf0faac9f2009-01-09 10:23:11 +00001006static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1007{
1008 struct gfar_private *priv = netdev_priv(dev);
1009
1010 if (!netif_running(dev))
1011 return -EINVAL;
1012
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001013 if (cmd == SIOCSHWTSTAMP)
Ben Hutchingsca0c88c2013-11-18 23:05:27 +00001014 return gfar_hwtstamp_set(dev, rq);
1015 if (cmd == SIOCGHWTSTAMP)
1016 return gfar_hwtstamp_get(dev, rq);
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001017
Clifford Wolf0faac9f2009-01-09 10:23:11 +00001018 if (!priv->phydev)
1019 return -ENODEV;
1020
Richard Cochran28b04112010-07-17 08:48:55 +00001021 return phy_mii_ioctl(priv->phydev, rq, cmd);
Clifford Wolf0faac9f2009-01-09 10:23:11 +00001022}
1023
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001024static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
1025 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001026{
1027 u32 rqfpr = FPR_FILER_MASK;
1028 u32 rqfcr = 0x0;
1029
1030 rqfar--;
1031 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001032 priv->ftp_rqfpr[rqfar] = rqfpr;
1033 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001034 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1035
1036 rqfar--;
1037 rqfcr = RQFCR_CMP_NOMATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001038 priv->ftp_rqfpr[rqfar] = rqfpr;
1039 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001040 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1041
1042 rqfar--;
1043 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
1044 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001045 priv->ftp_rqfcr[rqfar] = rqfcr;
1046 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001047 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1048
1049 rqfar--;
1050 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
1051 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001052 priv->ftp_rqfcr[rqfar] = rqfcr;
1053 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001054 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1055
1056 return rqfar;
1057}
1058
1059static void gfar_init_filer_table(struct gfar_private *priv)
1060{
1061 int i = 0x0;
1062 u32 rqfar = MAX_FILER_IDX;
1063 u32 rqfcr = 0x0;
1064 u32 rqfpr = FPR_FILER_MASK;
1065
1066 /* Default rule */
1067 rqfcr = RQFCR_CMP_MATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001068 priv->ftp_rqfcr[rqfar] = rqfcr;
1069 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001070 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1071
1072 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1073 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1074 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1075 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1076 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1077 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1078
Uwe Kleine-König85dd08e2010-06-11 12:16:55 +02001079 /* cur_filer_idx indicated the first non-masked rule */
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001080 priv->cur_filer_idx = rqfar;
1081
1082 /* Rest are masked rules */
1083 rqfcr = RQFCR_CMP_NOMATCH;
1084 for (i = 0; i < rqfar; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001085 priv->ftp_rqfcr[i] = rqfcr;
1086 priv->ftp_rqfpr[i] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001087 gfar_write_filer(priv, i, rqfcr, rqfpr);
1088 }
1089}
1090
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001091#ifdef CONFIG_PPC
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001092static void __gfar_detect_errata_83xx(struct gfar_private *priv)
Anton Vorontsov7d350972010-06-30 06:39:12 +00001093{
Anton Vorontsov7d350972010-06-30 06:39:12 +00001094 unsigned int pvr = mfspr(SPRN_PVR);
1095 unsigned int svr = mfspr(SPRN_SVR);
1096 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1097 unsigned int rev = svr & 0xffff;
1098
1099 /* MPC8313 Rev 2.0 and higher; All MPC837x */
1100 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001101 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001102 priv->errata |= GFAR_ERRATA_74;
1103
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001104 /* MPC8313 and MPC837x all rev */
1105 if ((pvr == 0x80850010 && mod == 0x80b0) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001106 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001107 priv->errata |= GFAR_ERRATA_76;
1108
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001109 /* MPC8313 Rev < 2.0 */
1110 if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00001111 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001112}
1113
1114static void __gfar_detect_errata_85xx(struct gfar_private *priv)
1115{
1116 unsigned int svr = mfspr(SPRN_SVR);
1117
1118 if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
1119 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil53fad772013-10-09 20:20:42 +03001120 if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
1121 ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
1122 priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001123}
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001124#endif
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001125
1126static void gfar_detect_errata(struct gfar_private *priv)
1127{
1128 struct device *dev = &priv->ofdev->dev;
1129
1130 /* no plans to fix */
1131 priv->errata |= GFAR_ERRATA_A002;
1132
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001133#ifdef CONFIG_PPC
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001134 if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
1135 __gfar_detect_errata_85xx(priv);
1136 else /* non-mpc85xx parts, i.e. e300 core based */
1137 __gfar_detect_errata_83xx(priv);
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001138#endif
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00001139
Anton Vorontsov7d350972010-06-30 06:39:12 +00001140 if (priv->errata)
1141 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1142 priv->errata);
1143}
1144
Claudiu Manoil08511332014-02-24 12:13:45 +02001145void gfar_mac_reset(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Claudiu Manoil20862782014-02-17 12:53:14 +02001147 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Claudiu Manoila328ac92014-02-24 12:13:42 +02001148 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001151 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Andy Flemingb98ac702009-02-04 16:38:05 -08001153 /* We need to delay at least 3 TX clocks */
Claudiu Manoila328ac92014-02-24 12:13:42 +02001154 udelay(3);
Andy Flemingb98ac702009-02-04 16:38:05 -08001155
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001156 /* the soft reset bit is not self-resetting, so we need to
1157 * clear it before resuming normal operation
1158 */
Claudiu Manoil20862782014-02-17 12:53:14 +02001159 gfar_write(&regs->maccfg1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Claudiu Manoila328ac92014-02-24 12:13:42 +02001161 udelay(3);
1162
Claudiu Manoil88302642014-02-24 12:13:43 +02001163 /* Compute rx_buff_size based on config flags */
1164 gfar_rx_buff_size_config(priv);
1165
1166 /* Initialize the max receive frame/buffer lengths */
1167 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Claudiu Manoila328ac92014-02-24 12:13:42 +02001168 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1169
1170 /* Initialize the Minimum Frame Length Register */
1171 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* Initialize MACCFG2. */
Anton Vorontsov7d350972010-06-30 06:39:12 +00001174 tempval = MACCFG2_INIT_SETTINGS;
Claudiu Manoil88302642014-02-24 12:13:43 +02001175
1176 /* If the mtu is larger than the max size for standard
1177 * ethernet frames (ie, a jumbo frame), then set maccfg2
1178 * to allow huge frames, and to check the length
1179 */
1180 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
1181 gfar_has_errata(priv, GFAR_ERRATA_74))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001182 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
Claudiu Manoil88302642014-02-24 12:13:43 +02001183
Anton Vorontsov7d350972010-06-30 06:39:12 +00001184 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Claudiu Manoila328ac92014-02-24 12:13:42 +02001186 /* Clear mac addr hash registers */
1187 gfar_write(&regs->igaddr0, 0);
1188 gfar_write(&regs->igaddr1, 0);
1189 gfar_write(&regs->igaddr2, 0);
1190 gfar_write(&regs->igaddr3, 0);
1191 gfar_write(&regs->igaddr4, 0);
1192 gfar_write(&regs->igaddr5, 0);
1193 gfar_write(&regs->igaddr6, 0);
1194 gfar_write(&regs->igaddr7, 0);
1195
1196 gfar_write(&regs->gaddr0, 0);
1197 gfar_write(&regs->gaddr1, 0);
1198 gfar_write(&regs->gaddr2, 0);
1199 gfar_write(&regs->gaddr3, 0);
1200 gfar_write(&regs->gaddr4, 0);
1201 gfar_write(&regs->gaddr5, 0);
1202 gfar_write(&regs->gaddr6, 0);
1203 gfar_write(&regs->gaddr7, 0);
1204
1205 if (priv->extended_hash)
1206 gfar_clear_exact_match(priv->ndev);
1207
1208 gfar_mac_rx_config(priv);
1209
1210 gfar_mac_tx_config(priv);
1211
1212 gfar_set_mac_address(priv->ndev);
1213
1214 gfar_set_multi(priv->ndev);
1215
1216 /* clear ievent and imask before configuring coalescing */
1217 gfar_ints_disable(priv);
1218
1219 /* Configure the coalescing support */
1220 gfar_configure_coalescing_all(priv);
1221}
1222
1223static void gfar_hw_init(struct gfar_private *priv)
1224{
1225 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1226 u32 attrs;
1227
1228 /* Stop the DMA engine now, in case it was running before
1229 * (The firmware could have used it, and left it running).
1230 */
1231 gfar_halt(priv);
1232
1233 gfar_mac_reset(priv);
1234
1235 /* Zero out the rmon mib registers if it has them */
1236 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1237 memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1238
1239 /* Mask off the CAM interrupts */
1240 gfar_write(&regs->rmon.cam1, 0xffffffff);
1241 gfar_write(&regs->rmon.cam2, 0xffffffff);
1242 }
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001245 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Claudiu Manoil34018fd2014-02-17 12:53:15 +02001247 /* Set the extraction length and index */
1248 attrs = ATTRELI_EL(priv->rx_stash_size) |
1249 ATTRELI_EI(priv->rx_stash_index);
1250
1251 gfar_write(&regs->attreli, attrs);
1252
1253 /* Start with defaults, and add stashing
1254 * depending on driver parameters
1255 */
1256 attrs = ATTR_INIT_SETTINGS;
1257
1258 if (priv->bd_stash_en)
1259 attrs |= ATTR_BDSTASH;
1260
1261 if (priv->rx_stash_size != 0)
1262 attrs |= ATTR_BUFSTASH;
1263
1264 gfar_write(&regs->attr, attrs);
1265
1266 /* FIFO configs */
1267 gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
1268 gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
1269 gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
1270
Claudiu Manoil20862782014-02-17 12:53:14 +02001271 /* Program the interrupt steering regs, only for MG devices */
1272 if (priv->num_grps > 1)
1273 gfar_write_isrg(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Xiubo Li898157e2014-06-04 16:49:16 +08001276static void gfar_init_addr_hash_table(struct gfar_private *priv)
Claudiu Manoil20862782014-02-17 12:53:14 +02001277{
1278 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001279
Andy Flemingb31a1d82008-12-16 15:29:15 -08001280 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001281 priv->extended_hash = 1;
1282 priv->hash_width = 9;
1283
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001284 priv->hash_regs[0] = &regs->igaddr0;
1285 priv->hash_regs[1] = &regs->igaddr1;
1286 priv->hash_regs[2] = &regs->igaddr2;
1287 priv->hash_regs[3] = &regs->igaddr3;
1288 priv->hash_regs[4] = &regs->igaddr4;
1289 priv->hash_regs[5] = &regs->igaddr5;
1290 priv->hash_regs[6] = &regs->igaddr6;
1291 priv->hash_regs[7] = &regs->igaddr7;
1292 priv->hash_regs[8] = &regs->gaddr0;
1293 priv->hash_regs[9] = &regs->gaddr1;
1294 priv->hash_regs[10] = &regs->gaddr2;
1295 priv->hash_regs[11] = &regs->gaddr3;
1296 priv->hash_regs[12] = &regs->gaddr4;
1297 priv->hash_regs[13] = &regs->gaddr5;
1298 priv->hash_regs[14] = &regs->gaddr6;
1299 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001300
1301 } else {
1302 priv->extended_hash = 0;
1303 priv->hash_width = 8;
1304
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001305 priv->hash_regs[0] = &regs->gaddr0;
1306 priv->hash_regs[1] = &regs->gaddr1;
1307 priv->hash_regs[2] = &regs->gaddr2;
1308 priv->hash_regs[3] = &regs->gaddr3;
1309 priv->hash_regs[4] = &regs->gaddr4;
1310 priv->hash_regs[5] = &regs->gaddr5;
1311 priv->hash_regs[6] = &regs->gaddr6;
1312 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001313 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001314}
1315
1316/* Set up the ethernet device structure, private data,
1317 * and anything else we need before we start
1318 */
1319static int gfar_probe(struct platform_device *ofdev)
1320{
1321 struct net_device *dev = NULL;
1322 struct gfar_private *priv = NULL;
1323 int err = 0, i;
1324
1325 err = gfar_of_init(ofdev, &dev);
1326
1327 if (err)
1328 return err;
1329
1330 priv = netdev_priv(dev);
1331 priv->ndev = dev;
1332 priv->ofdev = ofdev;
1333 priv->dev = &ofdev->dev;
1334 SET_NETDEV_DEV(dev, &ofdev->dev);
1335
1336 spin_lock_init(&priv->bflock);
1337 INIT_WORK(&priv->reset_task, gfar_reset_task);
1338
1339 platform_set_drvdata(ofdev, priv);
1340
1341 gfar_detect_errata(priv);
1342
Claudiu Manoil20862782014-02-17 12:53:14 +02001343 /* Set the dev->base_addr to the gfar reg region */
1344 dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
1345
1346 /* Fill in the dev structure */
1347 dev->watchdog_timeo = TX_TIMEOUT;
1348 dev->mtu = 1500;
1349 dev->netdev_ops = &gfar_netdev_ops;
1350 dev->ethtool_ops = &gfar_ethtool_ops;
1351
1352 /* Register for napi ...We are registering NAPI for each grp */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02001353 for (i = 0; i < priv->num_grps; i++) {
1354 if (priv->poll_mode == GFAR_SQ_POLLING) {
1355 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1356 gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
1357 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1358 gfar_poll_tx_sq, 2);
1359 } else {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02001360 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1361 gfar_poll_rx, GFAR_DEV_WEIGHT);
1362 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1363 gfar_poll_tx, 2);
1364 }
1365 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001366
1367 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
1368 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
1369 NETIF_F_RXCSUM;
1370 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
1371 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
1372 }
1373
1374 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
1375 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
1376 NETIF_F_HW_VLAN_CTAG_RX;
1377 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1378 }
1379
1380 gfar_init_addr_hash_table(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001381
Claudiu Manoil532c37b2014-02-17 12:53:16 +02001382 /* Insert receive time stamps into padding alignment bytes */
1383 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1384 priv->padding = 8;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001385
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001386 if (dev->features & NETIF_F_IP_CSUM ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001387 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
Wu Jiajun-B06378bee9e582012-05-21 23:00:48 +00001388 dev->needed_headroom = GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001392 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001393 for (i = 0; i < priv->num_tx_queues; i++) {
1394 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1395 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1396 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1397 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1398 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001399
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001400 for (i = 0; i < priv->num_rx_queues; i++) {
1401 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1402 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1403 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Jan Ceuleers0977f812012-06-05 03:42:12 +00001406 /* always enable rx filer */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001407 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001408 /* Enable most messages by default */
1409 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +00001410 /* use pritority h/w tx queue scheduling for single queue devices */
1411 if (priv->num_tx_queues == 1)
1412 priv->prio_sched_en = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001413
Claudiu Manoil08511332014-02-24 12:13:45 +02001414 set_bit(GFAR_DOWN, &priv->state);
1415
Claudiu Manoila328ac92014-02-24 12:13:42 +02001416 gfar_hw_init(priv);
Trent Piephod3eab822008-10-02 11:12:24 +00001417
Fabio Estevamd4c642e2014-06-03 19:55:38 -03001418 /* Carrier starts down, phylib will bring it up */
1419 netif_carrier_off(dev);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 err = register_netdev(dev);
1422
1423 if (err) {
Joe Perches59deab22011-06-14 08:57:47 +00001424 pr_err("%s: Cannot register net device, aborting\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 goto register_fail;
1426 }
1427
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001428 device_init_wakeup(&dev->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001429 priv->device_flags &
1430 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001431
Dai Harukic50a5d92008-12-17 16:51:32 -08001432 /* fill out IRQ number and name fields */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001433 for (i = 0; i < priv->num_grps; i++) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001434 struct gfar_priv_grp *grp = &priv->gfargrp[i];
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001435 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001436 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001437 dev->name, "_g", '0' + i, "_tx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001438 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001439 dev->name, "_g", '0' + i, "_rx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001440 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001441 dev->name, "_g", '0' + i, "_er");
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001442 } else
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001443 strcpy(gfar_irq(grp, TX)->name, dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001444 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001445
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001446 /* Initialize the filer table */
1447 gfar_init_filer_table(priv);
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* Print out the device info */
Joe Perches59deab22011-06-14 08:57:47 +00001450 netdev_info(dev, "mac: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Jan Ceuleers0977f812012-06-05 03:42:12 +00001452 /* Even more device info helps when determining which kernel
1453 * provided which set of benchmarks.
1454 */
Joe Perches59deab22011-06-14 08:57:47 +00001455 netdev_info(dev, "Running with NAPI enabled\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001456 for (i = 0; i < priv->num_rx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001457 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1458 i, priv->rx_queue[i]->rx_ring_size);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001459 for (i = 0; i < priv->num_tx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001460 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1461 i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 return 0;
1464
1465register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001466 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001467 gfar_free_rx_queues(priv);
1468 gfar_free_tx_queues(priv);
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001469 of_node_put(priv->phy_node);
1470 of_node_put(priv->tbi_node);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001471 free_gfar_dev(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Grant Likely2dc11582010-08-06 09:25:50 -06001475static int gfar_remove(struct platform_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001477 struct gfar_private *priv = platform_get_drvdata(ofdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001479 of_node_put(priv->phy_node);
1480 of_node_put(priv->tbi_node);
Grant Likelyfe192a42009-04-25 12:53:12 +00001481
David S. Millerd9d8e042009-09-06 01:41:02 -07001482 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001483 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001484 gfar_free_rx_queues(priv);
1485 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001486 free_gfar_dev(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 return 0;
1489}
1490
Scott Woodd87eb122008-07-11 18:04:45 -05001491#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001492
1493static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001494{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001495 struct gfar_private *priv = dev_get_drvdata(dev);
1496 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001497 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001498 unsigned long flags;
1499 u32 tempval;
1500
1501 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001502 (priv->device_flags &
1503 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001504
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001505 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001506
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001507 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001508
1509 local_irq_save(flags);
1510 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001511
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001512 gfar_halt_nodisable(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001513
1514 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001515 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001516
1517 tempval &= ~MACCFG1_TX_EN;
1518
1519 if (!magic_packet)
1520 tempval &= ~MACCFG1_RX_EN;
1521
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001522 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001523
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001524 unlock_tx_qs(priv);
1525 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001526
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001527 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001528
1529 if (magic_packet) {
1530 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001531 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001532
1533 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001534 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001535 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001536 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001537 } else {
1538 phy_stop(priv->phydev);
1539 }
1540 }
1541
1542 return 0;
1543}
1544
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001545static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001546{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001547 struct gfar_private *priv = dev_get_drvdata(dev);
1548 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001549 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001550 unsigned long flags;
1551 u32 tempval;
1552 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001553 (priv->device_flags &
1554 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001555
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001556 if (!netif_running(ndev)) {
1557 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001558 return 0;
1559 }
1560
1561 if (!magic_packet && priv->phydev)
1562 phy_start(priv->phydev);
1563
1564 /* Disable Magic Packet mode, in case something
1565 * else woke us up.
1566 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001567 local_irq_save(flags);
1568 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001569
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001570 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001571 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001572 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001573
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001574 gfar_start(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001575
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001576 unlock_tx_qs(priv);
1577 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001578
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001579 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001580
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001581 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001582
1583 return 0;
1584}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001585
1586static int gfar_restore(struct device *dev)
1587{
1588 struct gfar_private *priv = dev_get_drvdata(dev);
1589 struct net_device *ndev = priv->ndev;
1590
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001591 if (!netif_running(ndev)) {
1592 netif_device_attach(ndev);
1593
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001594 return 0;
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001595 }
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001596
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001597 if (gfar_init_bds(ndev)) {
1598 free_skb_resources(priv);
1599 return -ENOMEM;
1600 }
1601
Claudiu Manoila328ac92014-02-24 12:13:42 +02001602 gfar_mac_reset(priv);
1603
1604 gfar_init_tx_rx_base(priv);
1605
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001606 gfar_start(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001607
1608 priv->oldlink = 0;
1609 priv->oldspeed = 0;
1610 priv->oldduplex = -1;
1611
1612 if (priv->phydev)
1613 phy_start(priv->phydev);
1614
1615 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001616 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001617
1618 return 0;
1619}
1620
1621static struct dev_pm_ops gfar_pm_ops = {
1622 .suspend = gfar_suspend,
1623 .resume = gfar_resume,
1624 .freeze = gfar_suspend,
1625 .thaw = gfar_resume,
1626 .restore = gfar_restore,
1627};
1628
1629#define GFAR_PM_OPS (&gfar_pm_ops)
1630
Scott Woodd87eb122008-07-11 18:04:45 -05001631#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001632
1633#define GFAR_PM_OPS NULL
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001634
Scott Woodd87eb122008-07-11 18:04:45 -05001635#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001637/* Reads the controller's registers to determine what interface
1638 * connects it to the PHY.
1639 */
1640static phy_interface_t gfar_get_interface(struct net_device *dev)
1641{
1642 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001643 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001644 u32 ecntrl;
1645
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001646 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001647
1648 if (ecntrl & ECNTRL_SGMII_MODE)
1649 return PHY_INTERFACE_MODE_SGMII;
1650
1651 if (ecntrl & ECNTRL_TBI_MODE) {
1652 if (ecntrl & ECNTRL_REDUCED_MODE)
1653 return PHY_INTERFACE_MODE_RTBI;
1654 else
1655 return PHY_INTERFACE_MODE_TBI;
1656 }
1657
1658 if (ecntrl & ECNTRL_REDUCED_MODE) {
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001659 if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001660 return PHY_INTERFACE_MODE_RMII;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001661 }
Andy Fleming7132ab72007-07-11 11:43:07 -05001662 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001663 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001664
Jan Ceuleers0977f812012-06-05 03:42:12 +00001665 /* This isn't autodetected right now, so it must
Andy Fleming7132ab72007-07-11 11:43:07 -05001666 * be set by the device tree or platform code.
1667 */
1668 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1669 return PHY_INTERFACE_MODE_RGMII_ID;
1670
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001671 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001672 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001673 }
1674
Andy Flemingb31a1d82008-12-16 15:29:15 -08001675 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001676 return PHY_INTERFACE_MODE_GMII;
1677
1678 return PHY_INTERFACE_MODE_MII;
1679}
1680
1681
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001682/* Initializes driver's PHY state, and attaches to the PHY.
1683 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 */
1685static int init_phy(struct net_device *dev)
1686{
1687 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001688 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001689 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001690 GFAR_SUPPORTED_GBIT : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001691 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 priv->oldlink = 0;
1694 priv->oldspeed = 0;
1695 priv->oldduplex = -1;
1696
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001697 interface = gfar_get_interface(dev);
1698
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001699 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1700 interface);
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001701 if (!priv->phydev) {
1702 dev_err(&dev->dev, "could not attach to PHY\n");
1703 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Kapil Junejad3c12872007-05-11 18:25:11 -05001706 if (interface == PHY_INTERFACE_MODE_SGMII)
1707 gfar_configure_serdes(dev);
1708
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001709 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001710 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1711 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Pavaluca Matei-B46610cf987af2014-10-27 10:42:42 +02001713 /* Add support for flow control, but don't advertise it by default */
1714 priv->phydev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Jan Ceuleers0977f812012-06-05 03:42:12 +00001719/* Initialize TBI PHY interface for communicating with the
Paul Gortmakerd0313582008-04-17 00:08:10 -04001720 * SERDES lynx PHY on the chip. We communicate with this PHY
1721 * through the MDIO bus on each controller, treating it as a
1722 * "normal" PHY at the address found in the TBIPA register. We assume
1723 * that the TBIPA register is valid. Either the MDIO bus code will set
1724 * it to a value that doesn't conflict with other PHYs on the bus, or the
1725 * value doesn't matter, as there are no other PHYs on the bus.
1726 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001727static void gfar_configure_serdes(struct net_device *dev)
1728{
1729 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001730 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001731
Grant Likelyfe192a42009-04-25 12:53:12 +00001732 if (!priv->tbi_node) {
1733 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1734 "device tree specify a tbi-handle\n");
1735 return;
1736 }
1737
1738 tbiphy = of_phy_find_device(priv->tbi_node);
1739 if (!tbiphy) {
1740 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001741 return;
1742 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001743
Jan Ceuleers0977f812012-06-05 03:42:12 +00001744 /* If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001745 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1746 * everything for us? Resetting it takes the link down and requires
1747 * several seconds for it to come back.
1748 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001749 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001750 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001751
Paul Gortmakerd0313582008-04-17 00:08:10 -04001752 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001753 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001754
Grant Likelyfe192a42009-04-25 12:53:12 +00001755 phy_write(tbiphy, MII_ADVERTISE,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001756 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1757 ADVERTISE_1000XPSE_ASYM);
Kapil Junejad3c12872007-05-11 18:25:11 -05001758
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001759 phy_write(tbiphy, MII_BMCR,
1760 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1761 BMCR_SPEED1000);
Kapil Junejad3c12872007-05-11 18:25:11 -05001762}
1763
Anton Vorontsov511d9342010-06-30 06:39:15 +00001764static int __gfar_is_rx_idle(struct gfar_private *priv)
1765{
1766 u32 res;
1767
Jan Ceuleers0977f812012-06-05 03:42:12 +00001768 /* Normaly TSEC should not hang on GRS commands, so we should
Anton Vorontsov511d9342010-06-30 06:39:15 +00001769 * actually wait for IEVENT_GRSC flag.
1770 */
Claudiu Manoilad3660c2013-10-09 20:20:40 +03001771 if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
Anton Vorontsov511d9342010-06-30 06:39:15 +00001772 return 0;
1773
Jan Ceuleers0977f812012-06-05 03:42:12 +00001774 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
Anton Vorontsov511d9342010-06-30 06:39:15 +00001775 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1776 * and the Rx can be safely reset.
1777 */
1778 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1779 res &= 0x7f807f80;
1780 if ((res & 0xffff) == (res >> 16))
1781 return 1;
1782
1783 return 0;
1784}
Kumar Gala0bbaf062005-06-20 10:54:21 -05001785
1786/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001787static void gfar_halt_nodisable(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001789 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 u32 tempval;
Claudiu Manoila4feee82014-10-07 10:44:34 +03001791 unsigned int timeout;
1792 int stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001794 gfar_ints_disable(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Claudiu Manoila4feee82014-10-07 10:44:34 +03001796 if (gfar_is_dma_stopped(priv))
1797 return;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001800 tempval = gfar_read(&regs->dmactrl);
Claudiu Manoila4feee82014-10-07 10:44:34 +03001801 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1802 gfar_write(&regs->dmactrl, tempval);
Anton Vorontsov511d9342010-06-30 06:39:15 +00001803
Claudiu Manoila4feee82014-10-07 10:44:34 +03001804retry:
1805 timeout = 1000;
1806 while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
1807 cpu_relax();
1808 timeout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Claudiu Manoila4feee82014-10-07 10:44:34 +03001810
1811 if (!timeout)
1812 stopped = gfar_is_dma_stopped(priv);
1813
1814 if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
1815 !__gfar_is_rx_idle(priv))
1816 goto retry;
Scott Woodd87eb122008-07-11 18:04:45 -05001817}
Scott Woodd87eb122008-07-11 18:04:45 -05001818
1819/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001820void gfar_halt(struct gfar_private *priv)
Scott Woodd87eb122008-07-11 18:04:45 -05001821{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001822 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001823 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001825 /* Dissable the Rx/Tx hw queues */
1826 gfar_write(&regs->rqueue, 0);
1827 gfar_write(&regs->tqueue, 0);
Scott Wood2a54adc2008-08-12 15:10:46 -05001828
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001829 mdelay(10);
1830
1831 gfar_halt_nodisable(priv);
1832
1833 /* Disable Rx/Tx DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 tempval = gfar_read(&regs->maccfg1);
1835 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1836 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001837}
1838
1839void stop_gfar(struct net_device *dev)
1840{
1841 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001842
Claudiu Manoil08511332014-02-24 12:13:45 +02001843 netif_tx_stop_all_queues(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001844
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001845 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02001846 set_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001847 smp_mb__after_atomic();
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001848
Claudiu Manoil08511332014-02-24 12:13:45 +02001849 disable_napi(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001850
Claudiu Manoil08511332014-02-24 12:13:45 +02001851 /* disable ints and gracefully shut down Rx/Tx DMA */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001852 gfar_halt(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Claudiu Manoil08511332014-02-24 12:13:45 +02001854 phy_stop(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001859static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001862 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001863 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001865 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001867 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1868 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001869 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Claudiu Manoil369ec162013-02-14 05:00:02 +00001871 dma_unmap_single(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001872 txbdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08001873 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001874 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001875 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001876 txbdp++;
Claudiu Manoil369ec162013-02-14 05:00:02 +00001877 dma_unmap_page(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001878 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001880 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001881 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1882 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001884 kfree(tx_queue->tx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001885 tx_queue->tx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001886}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001888static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1889{
1890 struct rxbd8 *rxbdp;
1891 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1892 int i;
1893
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001894 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001896 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1897 if (rx_queue->rx_skbuff[i]) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00001898 dma_unmap_single(priv->dev, rxbdp->bufPtr,
1899 priv->rx_buffer_size,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001900 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001901 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1902 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001904 rxbdp->lstatus = 0;
1905 rxbdp->bufPtr = 0;
1906 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001908 kfree(rx_queue->rx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001909 rx_queue->rx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001910}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001911
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001912/* If there are any tx skbs or rx skbs still around, free them.
Jan Ceuleers0977f812012-06-05 03:42:12 +00001913 * Then free tx_skbuff and rx_skbuff
1914 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001915static void free_skb_resources(struct gfar_private *priv)
1916{
1917 struct gfar_priv_tx_q *tx_queue = NULL;
1918 struct gfar_priv_rx_q *rx_queue = NULL;
1919 int i;
1920
1921 /* Go through all the buffer descriptors and free their data buffers */
1922 for (i = 0; i < priv->num_tx_queues; i++) {
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001923 struct netdev_queue *txq;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001924
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001925 tx_queue = priv->tx_queue[i];
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001926 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001927 if (tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001928 free_skb_tx_queue(tx_queue);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001929 netdev_tx_reset_queue(txq);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001930 }
1931
1932 for (i = 0; i < priv->num_rx_queues; i++) {
1933 rx_queue = priv->rx_queue[i];
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001934 if (rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001935 free_skb_rx_queue(rx_queue);
1936 }
1937
Claudiu Manoil369ec162013-02-14 05:00:02 +00001938 dma_free_coherent(priv->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001939 sizeof(struct txbd8) * priv->total_tx_ring_size +
1940 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1941 priv->tx_queue[0]->tx_bd_base,
1942 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001945void gfar_start(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001946{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001947 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001948 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001949 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001950
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001951 /* Enable Rx/Tx hw queues */
1952 gfar_write(&regs->rqueue, priv->rqueue);
1953 gfar_write(&regs->tqueue, priv->tqueue);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001954
1955 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001956 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001957 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001958 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001959
Kumar Gala0bbaf062005-06-20 10:54:21 -05001960 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001961 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001962 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001963 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001964
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001965 for (i = 0; i < priv->num_grps; i++) {
1966 regs = priv->gfargrp[i].regs;
1967 /* Clear THLT/RHLT, so that the DMA starts polling now */
1968 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1969 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001970 }
Dai Haruki12dea572008-12-16 15:30:20 -08001971
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001972 /* Enable Rx/Tx DMA */
1973 tempval = gfar_read(&regs->maccfg1);
1974 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1975 gfar_write(&regs->maccfg1, tempval);
1976
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001977 gfar_ints_enable(priv);
1978
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001979 priv->ndev->trans_start = jiffies; /* prevent tx timeout */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001980}
1981
Claudiu Manoil80ec3962014-02-24 12:13:44 +02001982static void free_grp_irqs(struct gfar_priv_grp *grp)
1983{
1984 free_irq(gfar_irq(grp, TX)->irq, grp);
1985 free_irq(gfar_irq(grp, RX)->irq, grp);
1986 free_irq(gfar_irq(grp, ER)->irq, grp);
1987}
1988
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001989static int register_grp_irqs(struct gfar_priv_grp *grp)
1990{
1991 struct gfar_private *priv = grp->priv;
1992 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001993 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* If the device has multiple interrupts, register for
Jan Ceuleers0977f812012-06-05 03:42:12 +00001996 * them. Otherwise, only register for the one
1997 */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001998 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001999 /* Install our interrupt handlers for Error,
Jan Ceuleers0977f812012-06-05 03:42:12 +00002000 * Transmit, and Receive
2001 */
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002002 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
2003 gfar_irq(grp, ER)->name, grp);
2004 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002005 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002006 gfar_irq(grp, ER)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002007
Julia Lawall2145f1a2010-08-05 10:26:20 +00002008 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002010 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
2011 gfar_irq(grp, TX)->name, grp);
2012 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002013 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002014 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 goto tx_irq_fail;
2016 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002017 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
2018 gfar_irq(grp, RX)->name, grp);
2019 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002020 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002021 gfar_irq(grp, RX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto rx_irq_fail;
2023 }
2024 } else {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002025 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
2026 gfar_irq(grp, TX)->name, grp);
2027 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00002028 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002029 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto err_irq_fail;
2031 }
2032 }
2033
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002034 return 0;
2035
2036rx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002037 free_irq(gfar_irq(grp, TX)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002038tx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002039 free_irq(gfar_irq(grp, ER)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002040err_irq_fail:
2041 return err;
2042
2043}
2044
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002045static void gfar_free_irq(struct gfar_private *priv)
2046{
2047 int i;
2048
2049 /* Free the IRQs */
2050 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2051 for (i = 0; i < priv->num_grps; i++)
2052 free_grp_irqs(&priv->gfargrp[i]);
2053 } else {
2054 for (i = 0; i < priv->num_grps; i++)
2055 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
2056 &priv->gfargrp[i]);
2057 }
2058}
2059
2060static int gfar_request_irq(struct gfar_private *priv)
2061{
2062 int err, i, j;
2063
2064 for (i = 0; i < priv->num_grps; i++) {
2065 err = register_grp_irqs(&priv->gfargrp[i]);
2066 if (err) {
2067 for (j = 0; j < i; j++)
2068 free_grp_irqs(&priv->gfargrp[j]);
2069 return err;
2070 }
2071 }
2072
2073 return 0;
2074}
2075
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002076/* Bring the controller up and running */
2077int startup_gfar(struct net_device *ndev)
2078{
2079 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002080 int err;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002081
Claudiu Manoila328ac92014-02-24 12:13:42 +02002082 gfar_mac_reset(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002083
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002084 err = gfar_alloc_skb_resources(ndev);
2085 if (err)
2086 return err;
2087
Claudiu Manoila328ac92014-02-24 12:13:42 +02002088 gfar_init_tx_rx_base(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002089
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002090 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002091 clear_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002092 smp_mb__after_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002093
2094 /* Start Rx/Tx DMA and enable the interrupts */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02002095 gfar_start(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00002097 phy_start(priv->phydev);
2098
Claudiu Manoil08511332014-02-24 12:13:45 +02002099 enable_napi(priv);
2100
2101 netif_tx_wake_all_queues(ndev);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Jan Ceuleers0977f812012-06-05 03:42:12 +00002106/* Called when something needs to use the ethernet device
2107 * Returns 0 for success.
2108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109static int gfar_enet_open(struct net_device *dev)
2110{
Li Yang94e8cc32007-10-12 21:53:51 +08002111 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int err;
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 err = init_phy(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002115 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return err;
2117
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002118 err = gfar_request_irq(priv);
2119 if (err)
2120 return err;
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 err = startup_gfar(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002123 if (err)
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04002124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08002126 device_set_wakeup_enable(&dev->dev, priv->wol_en);
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return err;
2129}
2130
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002131static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002132{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002133 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07002134
2135 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002136
Kumar Gala0bbaf062005-06-20 10:54:21 -05002137 return fcb;
2138}
2139
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002140static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002141 int fcb_length)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002142{
Kumar Gala0bbaf062005-06-20 10:54:21 -05002143 /* If we're here, it's a IP packet with a TCP or UDP
2144 * payload. We set it to checksum, using a pseudo-header
2145 * we provide
2146 */
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00002147 u8 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002148
Jan Ceuleers0977f812012-06-05 03:42:12 +00002149 /* Tell the controller what the protocol is
2150 * And provide the already calculated phcs
2151 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002152 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002153 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03002154 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002155 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05002156 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002157
2158 /* l3os is the distance between the start of the
2159 * frame (skb->data) and the start of the IP hdr.
2160 * l4os is the distance between the start of the
Jan Ceuleers0977f812012-06-05 03:42:12 +00002161 * l3 hdr and the l4 hdr
2162 */
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002163 fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03002164 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002165
Andy Fleming7f7f5312005-11-11 12:38:59 -06002166 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002167}
2168
Andy Fleming7f7f5312005-11-11 12:38:59 -06002169void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002170{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002171 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002172 fcb->vlctl = vlan_tx_tag_get(skb);
2173}
2174
Dai Haruki4669bc92008-12-17 16:51:04 -08002175static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002176 struct txbd8 *base, int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002177{
2178 struct txbd8 *new_bd = bdp + stride;
2179
2180 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2181}
2182
2183static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002184 int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002185{
2186 return skip_txbd(bdp, 1, base, ring_size);
2187}
2188
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002189/* eTSEC12: csum generation not supported for some fcb offsets */
2190static inline bool gfar_csum_errata_12(struct gfar_private *priv,
2191 unsigned long fcb_addr)
2192{
2193 return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
2194 (fcb_addr % 0x20) > 0x18);
2195}
2196
2197/* eTSEC76: csum generation for frames larger than 2500 may
2198 * cause excess delays before start of transmission
2199 */
2200static inline bool gfar_csum_errata_76(struct gfar_private *priv,
2201 unsigned int len)
2202{
2203 return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
2204 (len > 2500));
2205}
2206
Jan Ceuleers0977f812012-06-05 03:42:12 +00002207/* This is called by the kernel when a frame is ready for transmission.
2208 * It is pointed to by the dev->hard_start_xmit function pointer
2209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2211{
2212 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002213 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002214 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002215 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002216 struct txfcb *fcb = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002217 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
Dai Haruki5a5efed2008-12-16 15:34:50 -08002218 u32 lstatus;
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002219 int i, rq = 0;
2220 int do_tstamp, do_csum, do_vlan;
Dai Haruki4669bc92008-12-17 16:51:04 -08002221 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05002222 unsigned long flags;
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002223 unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002224
2225 rq = skb->queue_mapping;
2226 tx_queue = priv->tx_queue[rq];
2227 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002228 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002229 regs = tx_queue->grp->regs;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002230
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002231 do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2232 do_vlan = vlan_tx_tag_present(skb);
2233 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2234 priv->hwts_tx_en;
2235
2236 if (do_csum || do_vlan)
2237 fcb_len = GMAC_FCB_LEN;
2238
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002239 /* check if time stamp should be generated */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002240 if (unlikely(do_tstamp))
2241 fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Dai Haruki4669bc92008-12-17 16:51:04 -08002242
Li Yang5b28bea2009-03-27 15:54:30 -07002243 /* make space for additional header when fcb is needed */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002244 if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002245 struct sk_buff *skb_new;
2246
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002247 skb_new = skb_realloc_headroom(skb, fcb_len);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002248 if (!skb_new) {
2249 dev->stats.tx_errors++;
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002250 dev_kfree_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002251 return NETDEV_TX_OK;
2252 }
Manfred Rudigierdb83d132012-01-09 23:26:50 +00002253
Eric Dumazet313b0372012-07-05 11:45:13 +00002254 if (skb->sk)
2255 skb_set_owner_w(skb_new, skb->sk);
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002256 dev_consume_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002257 skb = skb_new;
2258 }
2259
Dai Haruki4669bc92008-12-17 16:51:04 -08002260 /* total number of fragments in the SKB */
2261 nr_frags = skb_shinfo(skb)->nr_frags;
2262
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002263 /* calculate the required number of TxBDs for this skb */
2264 if (unlikely(do_tstamp))
2265 nr_txbds = nr_frags + 2;
2266 else
2267 nr_txbds = nr_frags + 1;
2268
Dai Haruki4669bc92008-12-17 16:51:04 -08002269 /* check if there is space to queue this packet */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002270 if (nr_txbds > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002271 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002272 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08002273 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08002274 return NETDEV_TX_BUSY;
2275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 /* Update transmit stats */
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002278 bytes_sent = skb->len;
2279 tx_queue->stats.tx_bytes += bytes_sent;
2280 /* keep Tx bytes on wire for BQL accounting */
2281 GFAR_CB(skb)->bytes_sent = bytes_sent;
Eric Dumazet1ac9ad12011-01-12 12:13:14 +00002282 tx_queue->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002284 txbdp = txbdp_start = tx_queue->cur_tx;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002285 lstatus = txbdp->lstatus;
2286
2287 /* Time stamp insertion requires one additional TxBD */
2288 if (unlikely(do_tstamp))
2289 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002290 tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Dai Haruki4669bc92008-12-17 16:51:04 -08002292 if (nr_frags == 0) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002293 if (unlikely(do_tstamp))
2294 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002295 TXBD_INTERRUPT);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002296 else
2297 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
Dai Haruki4669bc92008-12-17 16:51:04 -08002298 } else {
2299 /* Place the fragment addresses and lengths into the TxBDs */
2300 for (i = 0; i < nr_frags; i++) {
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002301 unsigned int frag_len;
Dai Haruki4669bc92008-12-17 16:51:04 -08002302 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002303 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002305 frag_len = skb_shinfo(skb)->frags[i].size;
Dai Haruki4669bc92008-12-17 16:51:04 -08002306
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002307 lstatus = txbdp->lstatus | frag_len |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002308 BD_LFLAG(TXBD_READY);
Dai Haruki4669bc92008-12-17 16:51:04 -08002309
2310 /* Handle the last BD specially */
2311 if (i == nr_frags - 1)
2312 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2313
Claudiu Manoil369ec162013-02-14 05:00:02 +00002314 bufaddr = skb_frag_dma_map(priv->dev,
Ian Campbell2234a722011-08-29 23:18:29 +00002315 &skb_shinfo(skb)->frags[i],
2316 0,
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002317 frag_len,
Ian Campbell2234a722011-08-29 23:18:29 +00002318 DMA_TO_DEVICE);
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002319 if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
2320 goto dma_map_err;
Dai Haruki4669bc92008-12-17 16:51:04 -08002321
2322 /* set the TxBD length and buffer pointer */
2323 txbdp->bufPtr = bufaddr;
2324 txbdp->lstatus = lstatus;
2325 }
2326
2327 lstatus = txbdp_start->lstatus;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002330 /* Add TxPAL between FCB and frame if required */
2331 if (unlikely(do_tstamp)) {
2332 skb_push(skb, GMAC_TXPAL_LEN);
2333 memset(skb->data, 0, GMAC_TXPAL_LEN);
2334 }
2335
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002336 /* Add TxFCB if required */
2337 if (fcb_len) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002338 fcb = gfar_add_fcb(skb);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002339 lstatus |= BD_LFLAG(TXBD_TOE);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002340 }
2341
2342 /* Set up checksumming */
2343 if (do_csum) {
2344 gfar_tx_checksum(skb, fcb, fcb_len);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002345
2346 if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
2347 unlikely(gfar_csum_errata_76(priv, skb->len))) {
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00002348 __skb_pull(skb, GMAC_FCB_LEN);
2349 skb_checksum_help(skb);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002350 if (do_vlan || do_tstamp) {
2351 /* put back a new fcb for vlan/tstamp TOE */
2352 fcb = gfar_add_fcb(skb);
2353 } else {
2354 /* Tx TOE not used */
2355 lstatus &= ~(BD_LFLAG(TXBD_TOE));
2356 fcb = NULL;
2357 }
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00002358 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002359 }
2360
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002361 if (do_vlan)
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002362 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002363
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002364 /* Setup tx hardware time stamping if requested */
2365 if (unlikely(do_tstamp)) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002366 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002367 fcb->ptp = 1;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002368 }
2369
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002370 bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
2371 DMA_TO_DEVICE);
2372 if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
2373 goto dma_map_err;
2374
2375 txbdp_start->bufPtr = bufaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Jan Ceuleers0977f812012-06-05 03:42:12 +00002377 /* If time stamping is requested one additional TxBD must be set up. The
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002378 * first TxBD points to the FCB and must have a data length of
2379 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2380 * the full frame length.
2381 */
2382 if (unlikely(do_tstamp)) {
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002383 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002384 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002385 (skb_headlen(skb) - fcb_len);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002386 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2387 } else {
2388 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002391 netdev_tx_sent_queue(txq, bytes_sent);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002392
Jan Ceuleers0977f812012-06-05 03:42:12 +00002393 /* We can work in parallel with gfar_clean_tx_ring(), except
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002394 * when modifying num_txbdfree. Note that we didn't grab the lock
2395 * when we were reading the num_txbdfree and checking for available
2396 * space, that's because outside of this function it can only grow,
2397 * and once we've got needed space, it cannot suddenly disappear.
2398 *
2399 * The lock also protects us from gfar_error(), which can modify
2400 * regs->tstat and thus retrigger the transfers, which is why we
2401 * also must grab the lock before setting ready bit for the first
2402 * to be transmitted BD.
2403 */
2404 spin_lock_irqsave(&tx_queue->txlock, flags);
2405
Claudiu Manoild55398b2014-10-07 10:44:35 +03002406 gfar_wmb();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002407
Dai Haruki4669bc92008-12-17 16:51:04 -08002408 txbdp_start->lstatus = lstatus;
2409
Claudiu Manoild55398b2014-10-07 10:44:35 +03002410 gfar_wmb(); /* force lstatus write before tx_skbuff */
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002411
2412 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2413
Dai Haruki4669bc92008-12-17 16:51:04 -08002414 /* Update the current skb pointer to the next entry we will use
Jan Ceuleers0977f812012-06-05 03:42:12 +00002415 * (wrapping if necessary)
2416 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002417 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002418 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002419
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002420 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002421
2422 /* reduce TxBD free count */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002423 tx_queue->num_txbdfree -= (nr_txbds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 /* If the next BD still needs to be cleaned up, then the bds
Jan Ceuleers0977f812012-06-05 03:42:12 +00002426 * are full. We need to tell the kernel to stop sending us stuff.
2427 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002428 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002429 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002431 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002435 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002438 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002440 return NETDEV_TX_OK;
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002441
2442dma_map_err:
2443 txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size);
2444 if (do_tstamp)
2445 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2446 for (i = 0; i < nr_frags; i++) {
2447 lstatus = txbdp->lstatus;
2448 if (!(lstatus & BD_LFLAG(TXBD_READY)))
2449 break;
2450
2451 txbdp->lstatus = lstatus & ~BD_LFLAG(TXBD_READY);
2452 bufaddr = txbdp->bufPtr;
2453 dma_unmap_page(priv->dev, bufaddr, txbdp->length,
2454 DMA_TO_DEVICE);
2455 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2456 }
2457 gfar_wmb();
2458 dev_kfree_skb_any(skb);
2459 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
2462/* Stops the kernel queue, and halts the controller */
2463static int gfar_close(struct net_device *dev)
2464{
2465 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002466
Sebastian Siewiorab939902008-08-19 21:12:45 +02002467 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 stop_gfar(dev);
2469
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002470 /* Disconnect from the PHY */
2471 phy_disconnect(priv->phydev);
2472 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002474 gfar_free_irq(priv);
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return 0;
2477}
2478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002480static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002482 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 return 0;
2485}
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2488{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002490 int frame_size = new_mtu + ETH_HLEN;
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Joe Perches59deab22011-06-14 08:57:47 +00002493 netif_err(priv, drv, dev, "Invalid MTU setting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return -EINVAL;
2495 }
2496
Claudiu Manoil08511332014-02-24 12:13:45 +02002497 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2498 cpu_relax();
2499
Claudiu Manoil88302642014-02-24 12:13:43 +02002500 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 stop_gfar(dev);
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 dev->mtu = new_mtu;
2504
Claudiu Manoil88302642014-02-24 12:13:43 +02002505 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 startup_gfar(dev);
2507
Claudiu Manoil08511332014-02-24 12:13:45 +02002508 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return 0;
2511}
2512
Claudiu Manoil08511332014-02-24 12:13:45 +02002513void reset_gfar(struct net_device *ndev)
2514{
2515 struct gfar_private *priv = netdev_priv(ndev);
2516
2517 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2518 cpu_relax();
2519
2520 stop_gfar(ndev);
2521 startup_gfar(ndev);
2522
2523 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2524}
2525
Sebastian Siewiorab939902008-08-19 21:12:45 +02002526/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 * transmitted after a set amount of time.
2528 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002529 * starting over will fix the problem.
2530 */
2531static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002533 struct gfar_private *priv = container_of(work, struct gfar_private,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002534 reset_task);
Claudiu Manoil08511332014-02-24 12:13:45 +02002535 reset_gfar(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
Sebastian Siewiorab939902008-08-19 21:12:45 +02002538static void gfar_timeout(struct net_device *dev)
2539{
2540 struct gfar_private *priv = netdev_priv(dev);
2541
2542 dev->stats.tx_errors++;
2543 schedule_work(&priv->reset_task);
2544}
2545
Eran Libertyacbc0f02010-07-07 15:54:54 -07002546static void gfar_align_skb(struct sk_buff *skb)
2547{
2548 /* We need the data buffer to be aligned properly. We will reserve
2549 * as many bytes as needed to align the data properly
2550 */
2551 skb_reserve(skb, RXBUF_ALIGNMENT -
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002552 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
Eran Libertyacbc0f02010-07-07 15:54:54 -07002553}
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555/* Interrupt Handler for Transmit complete */
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002556static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002558 struct net_device *dev = tx_queue->dev;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002559 struct netdev_queue *txq;
Dai Harukid080cd62008-04-09 19:37:51 -05002560 struct gfar_private *priv = netdev_priv(dev);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002561 struct txbd8 *bdp, *next = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002562 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002563 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002564 struct sk_buff *skb;
2565 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002566 int tx_ring_size = tx_queue->tx_ring_size;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002567 int frags = 0, nr_txbds = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002568 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002569 int howmany = 0;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002570 int tqi = tx_queue->qindex;
2571 unsigned int bytes_sent = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002572 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002573 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002575 txq = netdev_get_tx_queue(dev, tqi);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002576 bdp = tx_queue->dirty_tx;
2577 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002578
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002579 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002580 unsigned long flags;
2581
Dai Haruki4669bc92008-12-17 16:51:04 -08002582 frags = skb_shinfo(skb)->nr_frags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002583
Jan Ceuleers0977f812012-06-05 03:42:12 +00002584 /* When time stamping, one additional TxBD must be freed.
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002585 * Also, we need to dma_unmap_single() the TxPAL.
2586 */
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002587 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002588 nr_txbds = frags + 2;
2589 else
2590 nr_txbds = frags + 1;
2591
2592 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002593
2594 lstatus = lbdp->lstatus;
2595
2596 /* Only clean completed frames */
2597 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002598 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 break;
2600
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002601 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002602 next = next_txbd(bdp, base, tx_ring_size);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002603 buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002604 } else
2605 buflen = bdp->length;
2606
Claudiu Manoil369ec162013-02-14 05:00:02 +00002607 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002608 buflen, DMA_TO_DEVICE);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002609
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002610 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002611 struct skb_shared_hwtstamps shhwtstamps;
2612 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002613
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002614 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2615 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002616 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002617 skb_tstamp_tx(skb, &shhwtstamps);
2618 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2619 bdp = next;
2620 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002621
2622 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2623 bdp = next_txbd(bdp, base, tx_ring_size);
2624
2625 for (i = 0; i < frags; i++) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00002626 dma_unmap_page(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002627 bdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002628 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2629 bdp = next_txbd(bdp, base, tx_ring_size);
2630 }
2631
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002632 bytes_sent += GFAR_CB(skb)->bytes_sent;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002633
Eric Dumazetacb600d2012-10-05 06:23:55 +00002634 dev_kfree_skb_any(skb);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002635
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002636 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002637
2638 skb_dirtytx = (skb_dirtytx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002639 TX_RING_MOD_MASK(tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002640
Dai Harukid080cd62008-04-09 19:37:51 -05002641 howmany++;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002642 spin_lock_irqsave(&tx_queue->txlock, flags);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002643 tx_queue->num_txbdfree += nr_txbds;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002644 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08002645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Dai Haruki4669bc92008-12-17 16:51:04 -08002647 /* If we freed a buffer, we can restart transmission, if necessary */
Claudiu Manoil08511332014-02-24 12:13:45 +02002648 if (tx_queue->num_txbdfree &&
2649 netif_tx_queue_stopped(txq) &&
2650 !(test_bit(GFAR_DOWN, &priv->state)))
2651 netif_wake_subqueue(priv->ndev, tqi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Dai Haruki4669bc92008-12-17 16:51:04 -08002653 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002654 tx_queue->skb_dirtytx = skb_dirtytx;
2655 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002657 netdev_tx_completed_queue(txq, howmany, bytes_sent);
Dai Harukid080cd62008-04-09 19:37:51 -05002658}
2659
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002660static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
Eran Libertyacbc0f02010-07-07 15:54:54 -07002661{
2662 struct gfar_private *priv = netdev_priv(dev);
Eric Dumazetacb600d2012-10-05 06:23:55 +00002663 struct sk_buff *skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002664
2665 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2666 if (!skb)
2667 return NULL;
2668
2669 gfar_align_skb(skb);
2670
2671 return skb;
2672}
Andy Fleming815b97c2008-04-22 17:18:29 -05002673
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002674struct sk_buff *gfar_new_skb(struct net_device *dev, dma_addr_t *bufaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002676 struct gfar_private *priv = netdev_priv(dev);
2677 struct sk_buff *skb;
2678 dma_addr_t addr;
2679
2680 skb = gfar_alloc_skb(dev);
2681 if (!skb)
2682 return NULL;
2683
2684 addr = dma_map_single(priv->dev, skb->data,
2685 priv->rx_buffer_size, DMA_FROM_DEVICE);
2686 if (unlikely(dma_mapping_error(priv->dev, addr))) {
2687 dev_kfree_skb_any(skb);
2688 return NULL;
2689 }
2690
2691 *bufaddr = addr;
2692 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
Li Yang298e1a92007-10-16 14:18:13 +08002695static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Li Yang298e1a92007-10-16 14:18:13 +08002697 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002698 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 struct gfar_extra_stats *estats = &priv->extra_stats;
2700
Jan Ceuleers0977f812012-06-05 03:42:12 +00002701 /* If the packet was truncated, none of the other errors matter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (status & RXBD_TRUNCATED) {
2703 stats->rx_length_errors++;
2704
Paul Gortmaker212079d2013-02-12 15:38:19 -05002705 atomic64_inc(&estats->rx_trunc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 return;
2708 }
2709 /* Count the errors, if there were any */
2710 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2711 stats->rx_length_errors++;
2712
2713 if (status & RXBD_LARGE)
Paul Gortmaker212079d2013-02-12 15:38:19 -05002714 atomic64_inc(&estats->rx_large);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 else
Paul Gortmaker212079d2013-02-12 15:38:19 -05002716 atomic64_inc(&estats->rx_short);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
2718 if (status & RXBD_NONOCTET) {
2719 stats->rx_frame_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002720 atomic64_inc(&estats->rx_nonoctet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
2722 if (status & RXBD_CRCERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002723 atomic64_inc(&estats->rx_crcerr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 stats->rx_crc_errors++;
2725 }
2726 if (status & RXBD_OVERRUN) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002727 atomic64_inc(&estats->rx_overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 stats->rx_crc_errors++;
2729 }
2730}
2731
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002732irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002734 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2735 unsigned long flags;
2736 u32 imask;
2737
2738 if (likely(napi_schedule_prep(&grp->napi_rx))) {
2739 spin_lock_irqsave(&grp->grplock, flags);
2740 imask = gfar_read(&grp->regs->imask);
2741 imask &= IMASK_RX_DISABLED;
2742 gfar_write(&grp->regs->imask, imask);
2743 spin_unlock_irqrestore(&grp->grplock, flags);
2744 __napi_schedule(&grp->napi_rx);
2745 } else {
2746 /* Clear IEVENT, so interrupts aren't called again
2747 * because of the packets that have already arrived.
2748 */
2749 gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2750 }
2751
2752 return IRQ_HANDLED;
2753}
2754
2755/* Interrupt Handler for Transmit complete */
2756static irqreturn_t gfar_transmit(int irq, void *grp_id)
2757{
2758 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2759 unsigned long flags;
2760 u32 imask;
2761
2762 if (likely(napi_schedule_prep(&grp->napi_tx))) {
2763 spin_lock_irqsave(&grp->grplock, flags);
2764 imask = gfar_read(&grp->regs->imask);
2765 imask &= IMASK_TX_DISABLED;
2766 gfar_write(&grp->regs->imask, imask);
2767 spin_unlock_irqrestore(&grp->grplock, flags);
2768 __napi_schedule(&grp->napi_tx);
2769 } else {
2770 /* Clear IEVENT, so interrupts aren't called again
2771 * because of the packets that have already arrived.
2772 */
2773 gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2774 }
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return IRQ_HANDLED;
2777}
2778
Kumar Gala0bbaf062005-06-20 10:54:21 -05002779static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2780{
2781 /* If valid headers were found, and valid sums
2782 * were verified, then we tell the kernel that no
Jan Ceuleers0977f812012-06-05 03:42:12 +00002783 * checksumming is necessary. Otherwise, it is [FIXME]
2784 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002785 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002786 skb->ip_summed = CHECKSUM_UNNECESSARY;
2787 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002788 skb_checksum_none_assert(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002789}
2790
2791
Jan Ceuleers0977f812012-06-05 03:42:12 +00002792/* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
Claudiu Manoil61db26c2013-02-14 05:00:05 +00002793static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2794 int amount_pull, struct napi_struct *napi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
2796 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002797 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Dai Haruki2c2db482008-12-16 15:31:15 -08002799 /* fcb is at the beginning if exists */
2800 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Jan Ceuleers0977f812012-06-05 03:42:12 +00002802 /* Remove the FCB from the skb
2803 * Remove the padded bytes, if there are any
2804 */
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002805 if (amount_pull) {
2806 skb_record_rx_queue(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002807 skb_pull(skb, amount_pull);
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002808 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002809
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002810 /* Get receive timestamp from the skb */
2811 if (priv->hwts_rx_en) {
2812 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2813 u64 *ns = (u64 *) skb->data;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002814
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002815 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2816 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2817 }
2818
2819 if (priv->padding)
2820 skb_pull(skb, priv->padding);
2821
Michał Mirosław8b3afe92011-04-15 04:50:50 +00002822 if (dev->features & NETIF_F_RXCSUM)
Dai Haruki2c2db482008-12-16 15:31:15 -08002823 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002824
Dai Haruki2c2db482008-12-16 15:31:15 -08002825 /* Tell the skb what kind of packet this is */
2826 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002827
Patrick McHardyf6469682013-04-19 02:04:27 +00002828 /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
David S. Miller823dcd22011-08-20 10:39:12 -07002829 * Even if vlan rx accel is disabled, on some chips
2830 * RXFCB_VLN is pseudo randomly set.
2831 */
Patrick McHardyf6469682013-04-19 02:04:27 +00002832 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
David S. Miller823dcd22011-08-20 10:39:12 -07002833 fcb->flags & RXFCB_VLN)
David S. Millere5905c82013-04-22 19:24:19 -04002834 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
Jiri Pirko87c288c2011-07-20 04:54:19 +00002835
Dai Haruki2c2db482008-12-16 15:31:15 -08002836 /* Send the packet up the stack */
Claudiu Manoil953d2762013-03-21 03:12:15 +00002837 napi_gro_receive(napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}
2840
2841/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002842 * until the budget/quota has been reached. Returns the number
2843 * of frames handled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002845int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002847 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002848 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002850 int pkt_len;
2851 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 int howmany = 0;
2853 struct gfar_private *priv = netdev_priv(dev);
2854
2855 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002856 bdp = rx_queue->cur_rx;
2857 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Claudiu Manoilba779712013-02-14 05:00:07 +00002859 amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
Dai Haruki2c2db482008-12-16 15:31:15 -08002860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002862 struct sk_buff *newskb;
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002863 dma_addr_t bufaddr;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002864
Scott Wood3b6330c2007-05-16 15:06:59 -05002865 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002866
2867 /* Add another skb for the future */
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002868 newskb = gfar_new_skb(dev, &bufaddr);
Andy Fleming815b97c2008-04-22 17:18:29 -05002869
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002870 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Claudiu Manoil369ec162013-02-14 05:00:02 +00002872 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002873 priv->rx_buffer_size, DMA_FROM_DEVICE);
Andy Fleming81183052008-11-12 10:07:11 -06002874
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002875 if (unlikely(!(bdp->status & RXBD_ERR) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002876 bdp->length > priv->rx_buffer_size))
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002877 bdp->status = RXBD_LARGE;
2878
Andy Fleming815b97c2008-04-22 17:18:29 -05002879 /* We drop the frame if we failed to allocate a new buffer */
2880 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002881 bdp->status & RXBD_ERR)) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002882 count_errors(bdp->status, dev);
2883
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002884 if (unlikely(!newskb)) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002885 newskb = skb;
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002886 bufaddr = bdp->bufPtr;
2887 } else if (skb)
Eric Dumazetacb600d2012-10-05 06:23:55 +00002888 dev_kfree_skb(skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05002889 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002891 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 howmany++;
2893
Dai Haruki2c2db482008-12-16 15:31:15 -08002894 if (likely(skb)) {
2895 pkt_len = bdp->length - ETH_FCS_LEN;
2896 /* Remove the FCS from the packet length */
2897 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002898 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002899 skb_record_rx_queue(skb, rx_queue->qindex);
Wu Jiajun-B06378cd754a52012-04-19 22:54:35 +00002900 gfar_process_frame(dev, skb, amount_pull,
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002901 &rx_queue->grp->napi_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Dai Haruki2c2db482008-12-16 15:31:15 -08002903 } else {
Joe Perches59deab22011-06-14 08:57:47 +00002904 netif_warn(priv, rx_err, dev, "Missing skb!\n");
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002905 rx_queue->stats.rx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002906 atomic64_inc(&priv->extra_stats.rx_skbmissing);
Dai Haruki2c2db482008-12-16 15:31:15 -08002907 }
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
2910
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002911 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Andy Fleming815b97c2008-04-22 17:18:29 -05002913 /* Setup the new bdp */
Kevin Hao0a4b5a22014-12-11 14:08:41 +08002914 gfar_init_rxbdp(rx_queue, bdp, bufaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Matei Pavaluca45b679c92014-10-27 10:42:44 +02002916 /* Update Last Free RxBD pointer for LFC */
2917 if (unlikely(rx_queue->rfbptr && priv->tx_actual_en))
2918 gfar_write(rx_queue->rfbptr, (u32)bdp);
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002921 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 /* update to point at the next skb */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002924 rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2925 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927
2928 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002929 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return howmany;
2932}
2933
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002934static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002935{
2936 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002937 container_of(napi, struct gfar_priv_grp, napi_rx);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002938 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002939 struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002940 int work_done = 0;
2941
2942 /* Clear IEVENT, so interrupts aren't called again
2943 * because of the packets that have already arrived
2944 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002945 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002946
2947 work_done = gfar_clean_rx_ring(rx_queue, budget);
2948
2949 if (work_done < budget) {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002950 u32 imask;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002951 napi_complete(napi);
2952 /* Clear the halt bit in RSTAT */
2953 gfar_write(&regs->rstat, gfargrp->rstat);
2954
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002955 spin_lock_irq(&gfargrp->grplock);
2956 imask = gfar_read(&regs->imask);
2957 imask |= IMASK_RX_DEFAULT;
2958 gfar_write(&regs->imask, imask);
2959 spin_unlock_irq(&gfargrp->grplock);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002960 }
2961
2962 return work_done;
2963}
2964
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002965static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002967 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002968 container_of(napi, struct gfar_priv_grp, napi_tx);
2969 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002970 struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002971 u32 imask;
2972
2973 /* Clear IEVENT, so interrupts aren't called again
2974 * because of the packets that have already arrived
2975 */
2976 gfar_write(&regs->ievent, IEVENT_TX_MASK);
2977
2978 /* run Tx cleanup to completion */
2979 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
2980 gfar_clean_tx_ring(tx_queue);
2981
2982 napi_complete(napi);
2983
2984 spin_lock_irq(&gfargrp->grplock);
2985 imask = gfar_read(&regs->imask);
2986 imask |= IMASK_TX_DEFAULT;
2987 gfar_write(&regs->imask, imask);
2988 spin_unlock_irq(&gfargrp->grplock);
2989
2990 return 0;
2991}
2992
2993static int gfar_poll_rx(struct napi_struct *napi, int budget)
2994{
2995 struct gfar_priv_grp *gfargrp =
2996 container_of(napi, struct gfar_priv_grp, napi_rx);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002997 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002998 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002999 struct gfar_priv_rx_q *rx_queue = NULL;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003000 int work_done = 0, work_done_per_q = 0;
Claudiu Manoil39c0a0d2013-03-21 03:12:13 +00003001 int i, budget_per_q = 0;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00003002 unsigned long rstat_rxf;
3003 int num_act_queues;
Dai Harukid080cd62008-04-09 19:37:51 -05003004
Dai Haruki8c7396a2008-12-17 16:52:00 -08003005 /* Clear IEVENT, so interrupts aren't called again
Jan Ceuleers0977f812012-06-05 03:42:12 +00003006 * because of the packets that have already arrived
3007 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003008 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08003009
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00003010 rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
3011
3012 num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
3013 if (num_act_queues)
3014 budget_per_q = budget/num_act_queues;
3015
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003016 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
3017 /* skip queue if not active */
3018 if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
3019 continue;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003020
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003021 rx_queue = priv->rx_queue[i];
3022 work_done_per_q =
3023 gfar_clean_rx_ring(rx_queue, budget_per_q);
3024 work_done += work_done_per_q;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003025
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003026 /* finished processing this queue */
3027 if (work_done_per_q < budget_per_q) {
3028 /* clear active queue hw indication */
3029 gfar_write(&regs->rstat,
3030 RSTAT_CLEAR_RXF0 >> i);
3031 num_act_queues--;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00003032
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003033 if (!num_act_queues)
3034 break;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003035 }
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003036 }
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003037
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003038 if (!num_act_queues) {
3039 u32 imask;
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003040 napi_complete(napi);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003041
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03003042 /* Clear the halt bit in RSTAT */
3043 gfar_write(&regs->rstat, gfargrp->rstat);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003044
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003045 spin_lock_irq(&gfargrp->grplock);
3046 imask = gfar_read(&regs->imask);
3047 imask |= IMASK_RX_DEFAULT;
3048 gfar_write(&regs->imask, imask);
3049 spin_unlock_irq(&gfargrp->grplock);
Dai Harukid080cd62008-04-09 19:37:51 -05003050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Claudiu Manoilc233cf402013-03-19 07:40:02 +00003052 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02003055static int gfar_poll_tx(struct napi_struct *napi, int budget)
3056{
3057 struct gfar_priv_grp *gfargrp =
3058 container_of(napi, struct gfar_priv_grp, napi_tx);
3059 struct gfar_private *priv = gfargrp->priv;
3060 struct gfar __iomem *regs = gfargrp->regs;
3061 struct gfar_priv_tx_q *tx_queue = NULL;
3062 int has_tx_work = 0;
3063 int i;
3064
3065 /* Clear IEVENT, so interrupts aren't called again
3066 * because of the packets that have already arrived
3067 */
3068 gfar_write(&regs->ievent, IEVENT_TX_MASK);
3069
3070 for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
3071 tx_queue = priv->tx_queue[i];
3072 /* run Tx cleanup to completion */
3073 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
3074 gfar_clean_tx_ring(tx_queue);
3075 has_tx_work = 1;
3076 }
3077 }
3078
3079 if (!has_tx_work) {
3080 u32 imask;
3081 napi_complete(napi);
3082
3083 spin_lock_irq(&gfargrp->grplock);
3084 imask = gfar_read(&regs->imask);
3085 imask |= IMASK_TX_DEFAULT;
3086 gfar_write(&regs->imask, imask);
3087 spin_unlock_irq(&gfargrp->grplock);
3088 }
3089
3090 return 0;
3091}
3092
3093
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003094#ifdef CONFIG_NET_POLL_CONTROLLER
Jan Ceuleers0977f812012-06-05 03:42:12 +00003095/* Polling 'interrupt' - used by things like netconsole to send skbs
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003096 * without having to re-enable interrupts. It's not called while
3097 * the interrupt routine is executing.
3098 */
3099static void gfar_netpoll(struct net_device *dev)
3100{
3101 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00003102 int i;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003103
3104 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003105 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003106 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003107 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3108
3109 disable_irq(gfar_irq(grp, TX)->irq);
3110 disable_irq(gfar_irq(grp, RX)->irq);
3111 disable_irq(gfar_irq(grp, ER)->irq);
3112 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3113 enable_irq(gfar_irq(grp, ER)->irq);
3114 enable_irq(gfar_irq(grp, RX)->irq);
3115 enable_irq(gfar_irq(grp, TX)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003116 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003117 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003118 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003119 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3120
3121 disable_irq(gfar_irq(grp, TX)->irq);
3122 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3123 enable_irq(gfar_irq(grp, TX)->irq);
Anton Vorontsov43de0042009-12-09 02:52:19 -08003124 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003125 }
3126}
3127#endif
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003130static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003132 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003135 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003138 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003139 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003142 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003143 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003145 /* Check for errors */
3146 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003147 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 return IRQ_HANDLED;
3150}
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152/* Called every time the controller might need to be made
3153 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003154 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 * function converts those variables into the appropriate
3156 * register values, and can bring down the device if needed.
3157 */
3158static void adjust_link(struct net_device *dev)
3159{
3160 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003161 struct phy_device *phydev = priv->phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003163 if (unlikely(phydev->link != priv->oldlink ||
3164 phydev->duplex != priv->oldduplex ||
3165 phydev->speed != priv->oldspeed))
3166 gfar_update_link_state(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169/* Update the hash table based on the current list of multicast
3170 * addresses we subscribe to. Also, change the promiscuity of
3171 * the device based on the flags (this function is called
Jan Ceuleers0977f812012-06-05 03:42:12 +00003172 * whenever dev->flags is changed
3173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174static void gfar_set_multi(struct net_device *dev)
3175{
Jiri Pirko22bedad32010-04-01 21:22:57 +00003176 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003178 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 u32 tempval;
3180
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003181 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /* Set RCTRL to PROM */
3183 tempval = gfar_read(&regs->rctrl);
3184 tempval |= RCTRL_PROM;
3185 gfar_write(&regs->rctrl, tempval);
3186 } else {
3187 /* Set RCTRL to not PROM */
3188 tempval = gfar_read(&regs->rctrl);
3189 tempval &= ~(RCTRL_PROM);
3190 gfar_write(&regs->rctrl, tempval);
3191 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003192
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003193 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003195 gfar_write(&regs->igaddr0, 0xffffffff);
3196 gfar_write(&regs->igaddr1, 0xffffffff);
3197 gfar_write(&regs->igaddr2, 0xffffffff);
3198 gfar_write(&regs->igaddr3, 0xffffffff);
3199 gfar_write(&regs->igaddr4, 0xffffffff);
3200 gfar_write(&regs->igaddr5, 0xffffffff);
3201 gfar_write(&regs->igaddr6, 0xffffffff);
3202 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 gfar_write(&regs->gaddr0, 0xffffffff);
3204 gfar_write(&regs->gaddr1, 0xffffffff);
3205 gfar_write(&regs->gaddr2, 0xffffffff);
3206 gfar_write(&regs->gaddr3, 0xffffffff);
3207 gfar_write(&regs->gaddr4, 0xffffffff);
3208 gfar_write(&regs->gaddr5, 0xffffffff);
3209 gfar_write(&regs->gaddr6, 0xffffffff);
3210 gfar_write(&regs->gaddr7, 0xffffffff);
3211 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003212 int em_num;
3213 int idx;
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003216 gfar_write(&regs->igaddr0, 0x0);
3217 gfar_write(&regs->igaddr1, 0x0);
3218 gfar_write(&regs->igaddr2, 0x0);
3219 gfar_write(&regs->igaddr3, 0x0);
3220 gfar_write(&regs->igaddr4, 0x0);
3221 gfar_write(&regs->igaddr5, 0x0);
3222 gfar_write(&regs->igaddr6, 0x0);
3223 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 gfar_write(&regs->gaddr0, 0x0);
3225 gfar_write(&regs->gaddr1, 0x0);
3226 gfar_write(&regs->gaddr2, 0x0);
3227 gfar_write(&regs->gaddr3, 0x0);
3228 gfar_write(&regs->gaddr4, 0x0);
3229 gfar_write(&regs->gaddr5, 0x0);
3230 gfar_write(&regs->gaddr6, 0x0);
3231 gfar_write(&regs->gaddr7, 0x0);
3232
Andy Fleming7f7f5312005-11-11 12:38:59 -06003233 /* If we have extended hash tables, we need to
3234 * clear the exact match registers to prepare for
Jan Ceuleers0977f812012-06-05 03:42:12 +00003235 * setting them
3236 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003237 if (priv->extended_hash) {
3238 em_num = GFAR_EM_NUM + 1;
3239 gfar_clear_exact_match(dev);
3240 idx = 1;
3241 } else {
3242 idx = 0;
3243 em_num = 0;
3244 }
3245
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003246 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return;
3248
3249 /* Parse the list, and set the appropriate bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003250 netdev_for_each_mc_addr(ha, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003251 if (idx < em_num) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00003252 gfar_set_mac_for_addr(dev, idx, ha->addr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003253 idx++;
3254 } else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003255 gfar_set_hash_for_addr(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 }
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Andy Fleming7f7f5312005-11-11 12:38:59 -06003260
3261/* Clears each of the exact match registers to zero, so they
Jan Ceuleers0977f812012-06-05 03:42:12 +00003262 * don't interfere with normal reception
3263 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003264static void gfar_clear_exact_match(struct net_device *dev)
3265{
3266 int idx;
Joe Perches6a3c910c2011-11-16 09:38:02 +00003267 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
Andy Fleming7f7f5312005-11-11 12:38:59 -06003268
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003269 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
Joe Perchesb6bc7652010-12-21 02:16:08 -08003270 gfar_set_mac_for_addr(dev, idx, zero_arr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003271}
3272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273/* Set the appropriate hash bit for the given addr */
3274/* The algorithm works like so:
3275 * 1) Take the Destination Address (ie the multicast address), and
3276 * do a CRC on it (little endian), and reverse the bits of the
3277 * result.
3278 * 2) Use the 8 most significant bits as a hash into a 256-entry
3279 * table. The table is controlled through 8 32-bit registers:
3280 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3281 * gaddr7. This means that the 3 most significant bits in the
3282 * hash index which gaddr register to use, and the 5 other bits
3283 * indicate which bit (assuming an IBM numbering scheme, which
3284 * for PowerPC (tm) is usually the case) in the register holds
Jan Ceuleers0977f812012-06-05 03:42:12 +00003285 * the entry.
3286 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3288{
3289 u32 tempval;
3290 struct gfar_private *priv = netdev_priv(dev);
Joe Perches6a3c910c2011-11-16 09:38:02 +00003291 u32 result = ether_crc(ETH_ALEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05003292 int width = priv->hash_width;
3293 u8 whichbit = (result >> (32 - width)) & 0x1f;
3294 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 u32 value = (1 << (31-whichbit));
3296
Kumar Gala0bbaf062005-06-20 10:54:21 -05003297 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003299 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
Andy Fleming7f7f5312005-11-11 12:38:59 -06003302
3303/* There are multiple MAC Address register pairs on some controllers
3304 * This function sets the numth pair to a given address
3305 */
Joe Perchesb6bc7652010-12-21 02:16:08 -08003306static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3307 const u8 *addr)
Andy Fleming7f7f5312005-11-11 12:38:59 -06003308{
3309 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003310 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003311 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003312 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003313
3314 macptr += num*2;
3315
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003316 /* For a station address of 0x12345678ABCD in transmission
3317 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
3318 * MACnADDR2 is set to 0x34120000.
Jan Ceuleers0977f812012-06-05 03:42:12 +00003319 */
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003320 tempval = (addr[5] << 24) | (addr[4] << 16) |
3321 (addr[3] << 8) | addr[2];
Andy Fleming7f7f5312005-11-11 12:38:59 -06003322
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003323 gfar_write(macptr, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003324
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003325 tempval = (addr[1] << 24) | (addr[0] << 16);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003326
3327 gfar_write(macptr+1, tempval);
3328}
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003331static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003333 struct gfar_priv_grp *gfargrp = grp_id;
3334 struct gfar __iomem *regs = gfargrp->regs;
3335 struct gfar_private *priv= gfargrp->priv;
3336 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003339 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003342 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05003343
3344 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003345 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05003346 (events & IEVENT_MAG))
3347 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
3349 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003350 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003351 netdev_dbg(dev,
3352 "error interrupt (ievent=0x%08x imask=0x%08x)\n",
Joe Perches59deab22011-06-14 08:57:47 +00003353 events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
3355 /* Update the error counters */
3356 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003357 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003360 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003362 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (events & IEVENT_XFUN) {
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003364 unsigned long flags;
3365
Joe Perches59deab22011-06-14 08:57:47 +00003366 netif_dbg(priv, tx_err, dev,
3367 "TX FIFO underrun, packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003368 dev->stats.tx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003369 atomic64_inc(&priv->extra_stats.tx_underrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003371 local_irq_save(flags);
3372 lock_tx_qs(priv);
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003375 gfar_write(&regs->tstat, gfargrp->tstat);
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003376
3377 unlock_tx_qs(priv);
3378 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
Joe Perches59deab22011-06-14 08:57:47 +00003380 netif_dbg(priv, tx_err, dev, "Transmit Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 }
3382 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003383 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003384 atomic64_inc(&priv->extra_stats.rx_bsy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003386 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Joe Perches59deab22011-06-14 08:57:47 +00003388 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3389 gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
3391 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003392 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003393 atomic64_inc(&priv->extra_stats.rx_babr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
Joe Perches59deab22011-06-14 08:57:47 +00003395 netif_dbg(priv, rx_err, dev, "babbling RX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
3397 if (events & IEVENT_EBERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003398 atomic64_inc(&priv->extra_stats.eberr);
Joe Perches59deab22011-06-14 08:57:47 +00003399 netif_dbg(priv, rx_err, dev, "bus error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
Joe Perches59deab22011-06-14 08:57:47 +00003401 if (events & IEVENT_RXC)
3402 netif_dbg(priv, rx_status, dev, "control frame\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 if (events & IEVENT_BABT) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003405 atomic64_inc(&priv->extra_stats.tx_babt);
Joe Perches59deab22011-06-14 08:57:47 +00003406 netif_dbg(priv, tx_err, dev, "babbling TX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
3408 return IRQ_HANDLED;
3409}
3410
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003411static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
3412{
3413 struct phy_device *phydev = priv->phydev;
3414 u32 val = 0;
3415
3416 if (!phydev->duplex)
3417 return val;
3418
3419 if (!priv->pause_aneg_en) {
3420 if (priv->tx_pause_en)
3421 val |= MACCFG1_TX_FLOW;
3422 if (priv->rx_pause_en)
3423 val |= MACCFG1_RX_FLOW;
3424 } else {
3425 u16 lcl_adv, rmt_adv;
3426 u8 flowctrl;
3427 /* get link partner capabilities */
3428 rmt_adv = 0;
3429 if (phydev->pause)
3430 rmt_adv = LPA_PAUSE_CAP;
3431 if (phydev->asym_pause)
3432 rmt_adv |= LPA_PAUSE_ASYM;
3433
Pavaluca Matei-B4661043ef8d22014-10-27 10:42:43 +02003434 lcl_adv = 0;
3435 if (phydev->advertising & ADVERTISED_Pause)
3436 lcl_adv |= ADVERTISE_PAUSE_CAP;
3437 if (phydev->advertising & ADVERTISED_Asym_Pause)
3438 lcl_adv |= ADVERTISE_PAUSE_ASYM;
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003439
3440 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
3441 if (flowctrl & FLOW_CTRL_TX)
3442 val |= MACCFG1_TX_FLOW;
3443 if (flowctrl & FLOW_CTRL_RX)
3444 val |= MACCFG1_RX_FLOW;
3445 }
3446
3447 return val;
3448}
3449
3450static noinline void gfar_update_link_state(struct gfar_private *priv)
3451{
3452 struct gfar __iomem *regs = priv->gfargrp[0].regs;
3453 struct phy_device *phydev = priv->phydev;
Matei Pavaluca45b679c92014-10-27 10:42:44 +02003454 struct gfar_priv_rx_q *rx_queue = NULL;
3455 int i;
3456 struct rxbd8 *bdp;
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003457
3458 if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
3459 return;
3460
3461 if (phydev->link) {
3462 u32 tempval1 = gfar_read(&regs->maccfg1);
3463 u32 tempval = gfar_read(&regs->maccfg2);
3464 u32 ecntrl = gfar_read(&regs->ecntrl);
Matei Pavaluca45b679c92014-10-27 10:42:44 +02003465 u32 tx_flow_oldval = (tempval & MACCFG1_TX_FLOW);
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003466
3467 if (phydev->duplex != priv->oldduplex) {
3468 if (!(phydev->duplex))
3469 tempval &= ~(MACCFG2_FULL_DUPLEX);
3470 else
3471 tempval |= MACCFG2_FULL_DUPLEX;
3472
3473 priv->oldduplex = phydev->duplex;
3474 }
3475
3476 if (phydev->speed != priv->oldspeed) {
3477 switch (phydev->speed) {
3478 case 1000:
3479 tempval =
3480 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
3481
3482 ecntrl &= ~(ECNTRL_R100);
3483 break;
3484 case 100:
3485 case 10:
3486 tempval =
3487 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
3488
3489 /* Reduced mode distinguishes
3490 * between 10 and 100
3491 */
3492 if (phydev->speed == SPEED_100)
3493 ecntrl |= ECNTRL_R100;
3494 else
3495 ecntrl &= ~(ECNTRL_R100);
3496 break;
3497 default:
3498 netif_warn(priv, link, priv->ndev,
3499 "Ack! Speed (%d) is not 10/100/1000!\n",
3500 phydev->speed);
3501 break;
3502 }
3503
3504 priv->oldspeed = phydev->speed;
3505 }
3506
3507 tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
3508 tempval1 |= gfar_get_flowctrl_cfg(priv);
3509
Matei Pavaluca45b679c92014-10-27 10:42:44 +02003510 /* Turn last free buffer recording on */
3511 if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) {
3512 for (i = 0; i < priv->num_rx_queues; i++) {
3513 rx_queue = priv->rx_queue[i];
3514 bdp = rx_queue->cur_rx;
3515 /* skip to previous bd */
3516 bdp = skip_bd(bdp, rx_queue->rx_ring_size - 1,
3517 rx_queue->rx_bd_base,
3518 rx_queue->rx_ring_size);
3519
3520 if (rx_queue->rfbptr)
3521 gfar_write(rx_queue->rfbptr, (u32)bdp);
3522 }
3523
3524 priv->tx_actual_en = 1;
3525 }
3526
3527 if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval))
3528 priv->tx_actual_en = 0;
3529
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003530 gfar_write(&regs->maccfg1, tempval1);
3531 gfar_write(&regs->maccfg2, tempval);
3532 gfar_write(&regs->ecntrl, ecntrl);
3533
3534 if (!priv->oldlink)
3535 priv->oldlink = 1;
3536
3537 } else if (priv->oldlink) {
3538 priv->oldlink = 0;
3539 priv->oldspeed = 0;
3540 priv->oldduplex = -1;
3541 }
3542
3543 if (netif_msg_link(priv))
3544 phy_print_status(phydev);
3545}
3546
Andy Flemingb31a1d82008-12-16 15:29:15 -08003547static struct of_device_id gfar_match[] =
3548{
3549 {
3550 .type = "network",
3551 .compatible = "gianfar",
3552 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003553 {
3554 .compatible = "fsl,etsec2",
3555 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003556 {},
3557};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003558MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560/* Structure for a device driver */
Grant Likely74888762011-02-22 21:05:51 -07003561static struct platform_driver gfar_driver = {
Grant Likely40182942010-04-13 16:13:02 -07003562 .driver = {
3563 .name = "fsl-gianfar",
Grant Likely40182942010-04-13 16:13:02 -07003564 .pm = GFAR_PM_OPS,
3565 .of_match_table = gfar_match,
3566 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 .probe = gfar_probe,
3568 .remove = gfar_remove,
3569};
3570
Axel Lindb62f682011-11-27 16:44:17 +00003571module_platform_driver(gfar_driver);