blob: 329efcad4fe03a7779895c0fd79d8616bf0f4f12 [file] [log] [blame]
Jan Ceuleers0977f812012-06-05 03:42:12 +00001/* drivers/net/ethernet/freescale/gianfar.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Gianfar Ethernet Driver
Andy Fleming7f7f5312005-11-11 12:38:59 -06004 * This driver is designed for the non-CPM ethernet controllers
5 * on the 85xx and 83xx family of integrated processors
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based on 8260_io/fcc_enet.c
7 *
8 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -08009 * Maintainer: Kumar Gala
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000010 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Claudiu Manoil20862782014-02-17 12:53:14 +020012 * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000013 * Copyright 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Gianfar: AKA Lambda Draconis, "Dragon"
21 * RA 11 31 24.2
22 * Dec +69 19 52
23 * V 3.84
24 * B-V +1.62
25 *
26 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050027 *
Andy Flemingb31a1d82008-12-16 15:29:15 -080028 * The driver is initialized through of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050033 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * last descriptor of the ring.
36 *
37 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050038 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * corresponding bit in the IMASK register is also set (if
40 * interrupt coalescing is active, then the interrupt may not
41 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040042 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020044 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050045 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * are none left with data (NAPI will stop after a set number of
47 * packets to give time to other tasks, but will eventually
48 * process all the packets). The data arrives inside a
49 * pre-allocated skb, and so after the skb is passed up to the
50 * stack, a new skb must be allocated, and the address field in
51 * the buffer descriptor must be updated to indicate this new
52 * skb.
53 *
54 * When the kernel requests that a packet be transmitted, the
55 * driver starts where it left off last time, and points the
56 * descriptor at the buffer which was passed in. The driver
57 * then informs the DMA engine that there are packets ready to
58 * be transmitted. Once the controller is finished transmitting
59 * the packet, an interrupt may be triggered (under the same
60 * conditions as for reception, but depending on the TXF bit).
61 * The driver then cleans up the buffer.
62 */
63
Joe Perches59deab22011-06-14 08:57:47 +000064#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65#define DEBUG
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/string.h>
69#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040070#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/slab.h>
72#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/delay.h>
74#include <linux/netdevice.h>
75#include <linux/etherdevice.h>
76#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050077#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/spinlock.h>
79#include <linux/mm.h>
Rob Herring5af50732013-09-17 14:28:33 -050080#include <linux/of_address.h>
81#include <linux/of_irq.h>
Grant Likelyfe192a42009-04-25 12:53:12 +000082#include <linux/of_mdio.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080083#include <linux/of_platform.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050084#include <linux/ip.h>
85#include <linux/tcp.h>
86#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080087#include <linux/in.h>
Manfred Rudigiercc772ab2010-04-08 23:10:03 +000088#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#include <asm/io.h>
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +030091#ifdef CONFIG_PPC
Anton Vorontsov7d350972010-06-30 06:39:12 +000092#include <asm/reg.h>
Claudiu Manoil2969b1f2013-10-09 20:20:41 +030093#include <asm/mpc85xx.h>
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +030094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <asm/irq.h>
96#include <asm/uaccess.h>
97#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/dma-mapping.h>
99#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400100#include <linux/mii.h>
101#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -0800102#include <linux/phy_fixed.h>
103#include <linux/of.h>
David Daney4b6ba8a2010-10-26 15:07:13 -0700104#include <linux/of_net.h>
Claudiu Manoilfd31a952014-10-07 10:44:31 +0300105#include <linux/of_address.h>
106#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#include "gianfar.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Andy Fleming7f7f5312005-11-11 12:38:59 -0600112const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static int gfar_enet_open(struct net_device *dev);
115static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200116static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static void gfar_timeout(struct net_device *dev);
118static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500119struct sk_buff *gfar_new_skb(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000120static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000121 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int gfar_set_mac_address(struct net_device *dev);
123static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100124static irqreturn_t gfar_error(int irq, void *dev_id);
125static irqreturn_t gfar_transmit(int irq, void *dev_id);
126static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static void adjust_link(struct net_device *dev);
Claudiu Manoil6ce29b02014-04-30 14:27:21 +0300128static noinline void gfar_update_link_state(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int init_phy(struct net_device *dev);
Grant Likely74888762011-02-22 21:05:51 -0700130static int gfar_probe(struct platform_device *ofdev);
Grant Likely2dc11582010-08-06 09:25:50 -0600131static int gfar_remove(struct platform_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400132static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void gfar_set_multi(struct net_device *dev);
134static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500135static void gfar_configure_serdes(struct net_device *dev);
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200136static int gfar_poll_rx(struct napi_struct *napi, int budget);
137static int gfar_poll_tx(struct napi_struct *napi, int budget);
138static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
139static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300140#ifdef CONFIG_NET_POLL_CONTROLLER
141static void gfar_netpoll(struct net_device *dev);
142#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000143int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
Claudiu Manoilc233cf402013-03-19 07:40:02 +0000144static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Claudiu Manoil61db26c2013-02-14 05:00:05 +0000145static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
146 int amount_pull, struct napi_struct *napi);
Claudiu Manoilc10650b2014-02-17 12:53:18 +0200147static void gfar_halt_nodisable(struct gfar_private *priv);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600148static void gfar_clear_exact_match(struct net_device *dev);
Joe Perchesb6bc7652010-12-21 02:16:08 -0800149static void gfar_set_mac_for_addr(struct net_device *dev, int num,
150 const u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000151static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153MODULE_AUTHOR("Freescale Semiconductor, Inc");
154MODULE_DESCRIPTION("Gianfar Ethernet Driver");
155MODULE_LICENSE("GPL");
156
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000157static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000158 dma_addr_t buf)
159{
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000160 u32 lstatus;
161
162 bdp->bufPtr = buf;
163
164 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000165 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000166 lstatus |= BD_LFLAG(RXBD_WRAP);
167
Claudiu Manoild55398b2014-10-07 10:44:35 +0300168 gfar_wmb();
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000169
170 bdp->lstatus = lstatus;
171}
172
Anton Vorontsov87283272009-10-12 06:00:39 +0000173static int gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000174{
Anton Vorontsov87283272009-10-12 06:00:39 +0000175 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000176 struct gfar_priv_tx_q *tx_queue = NULL;
177 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000178 struct txbd8 *txbdp;
179 struct rxbd8 *rxbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000180 int i, j;
Anton Vorontsov87283272009-10-12 06:00:39 +0000181
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000182 for (i = 0; i < priv->num_tx_queues; i++) {
183 tx_queue = priv->tx_queue[i];
184 /* Initialize some variables in our dev structure */
185 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
186 tx_queue->dirty_tx = tx_queue->tx_bd_base;
187 tx_queue->cur_tx = tx_queue->tx_bd_base;
188 tx_queue->skb_curtx = 0;
189 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000190
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000191 /* Initialize Transmit Descriptor Ring */
192 txbdp = tx_queue->tx_bd_base;
193 for (j = 0; j < tx_queue->tx_ring_size; j++) {
194 txbdp->lstatus = 0;
195 txbdp->bufPtr = 0;
196 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000197 }
198
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000199 /* Set the last descriptor in the ring to indicate wrap */
200 txbdp--;
201 txbdp->status |= TXBD_WRAP;
202 }
203
204 for (i = 0; i < priv->num_rx_queues; i++) {
205 rx_queue = priv->rx_queue[i];
206 rx_queue->cur_rx = rx_queue->rx_bd_base;
207 rx_queue->skb_currx = 0;
208 rxbdp = rx_queue->rx_bd_base;
209
210 for (j = 0; j < rx_queue->rx_ring_size; j++) {
211 struct sk_buff *skb = rx_queue->rx_skbuff[j];
212
213 if (skb) {
214 gfar_init_rxbdp(rx_queue, rxbdp,
215 rxbdp->bufPtr);
216 } else {
217 skb = gfar_new_skb(ndev);
218 if (!skb) {
Joe Perches59deab22011-06-14 08:57:47 +0000219 netdev_err(ndev, "Can't allocate RX buffers\n");
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +0000220 return -ENOMEM;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000221 }
222 rx_queue->rx_skbuff[j] = skb;
223
224 gfar_new_rxbdp(rx_queue, rxbdp, skb);
225 }
226
227 rxbdp++;
228 }
229
Anton Vorontsov87283272009-10-12 06:00:39 +0000230 }
231
232 return 0;
233}
234
235static int gfar_alloc_skb_resources(struct net_device *ndev)
236{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000237 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000238 dma_addr_t addr;
239 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000240 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil369ec162013-02-14 05:00:02 +0000241 struct device *dev = priv->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000242 struct gfar_priv_tx_q *tx_queue = NULL;
243 struct gfar_priv_rx_q *rx_queue = NULL;
244
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000245 priv->total_tx_ring_size = 0;
246 for (i = 0; i < priv->num_tx_queues; i++)
247 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
248
249 priv->total_rx_ring_size = 0;
250 for (i = 0; i < priv->num_rx_queues; i++)
251 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000252
253 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000254 vaddr = dma_alloc_coherent(dev,
Joe Perchesd0320f72013-03-14 13:07:21 +0000255 (priv->total_tx_ring_size *
256 sizeof(struct txbd8)) +
257 (priv->total_rx_ring_size *
258 sizeof(struct rxbd8)),
259 &addr, GFP_KERNEL);
260 if (!vaddr)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000261 return -ENOMEM;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000262
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000263 for (i = 0; i < priv->num_tx_queues; i++) {
264 tx_queue = priv->tx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000265 tx_queue->tx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000266 tx_queue->tx_bd_dma_base = addr;
267 tx_queue->dev = ndev;
268 /* enet DMA only understands physical addresses */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000269 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
270 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000271 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000272
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000273 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000274 for (i = 0; i < priv->num_rx_queues; i++) {
275 rx_queue = priv->rx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000276 rx_queue->rx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000277 rx_queue->rx_bd_dma_base = addr;
278 rx_queue->dev = ndev;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000279 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
280 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000281 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000282
283 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000284 for (i = 0; i < priv->num_tx_queues; i++) {
285 tx_queue = priv->tx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000286 tx_queue->tx_skbuff =
287 kmalloc_array(tx_queue->tx_ring_size,
288 sizeof(*tx_queue->tx_skbuff),
289 GFP_KERNEL);
290 if (!tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000291 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000292
293 for (k = 0; k < tx_queue->tx_ring_size; k++)
294 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000295 }
296
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000297 for (i = 0; i < priv->num_rx_queues; i++) {
298 rx_queue = priv->rx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000299 rx_queue->rx_skbuff =
300 kmalloc_array(rx_queue->rx_ring_size,
301 sizeof(*rx_queue->rx_skbuff),
302 GFP_KERNEL);
303 if (!rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000304 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000305
306 for (j = 0; j < rx_queue->rx_ring_size; j++)
307 rx_queue->rx_skbuff[j] = NULL;
308 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000309
Anton Vorontsov87283272009-10-12 06:00:39 +0000310 if (gfar_init_bds(ndev))
311 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000312
313 return 0;
314
315cleanup:
316 free_skb_resources(priv);
317 return -ENOMEM;
318}
319
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000320static void gfar_init_tx_rx_base(struct gfar_private *priv)
321{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000322 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000323 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000324 int i;
325
326 baddr = &regs->tbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000327 for (i = 0; i < priv->num_tx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000328 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000329 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000330 }
331
332 baddr = &regs->rbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000333 for (i = 0; i < priv->num_rx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000334 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000335 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000336 }
337}
338
Claudiu Manoil88302642014-02-24 12:13:43 +0200339static void gfar_rx_buff_size_config(struct gfar_private *priv)
340{
Claudiu Manoilf5b720b2014-10-15 19:11:46 +0300341 int frame_size = priv->ndev->mtu + ETH_HLEN + ETH_FCS_LEN;
Claudiu Manoil88302642014-02-24 12:13:43 +0200342
343 /* set this when rx hw offload (TOE) functions are being used */
344 priv->uses_rxfcb = 0;
345
346 if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
347 priv->uses_rxfcb = 1;
348
349 if (priv->hwts_rx_en)
350 priv->uses_rxfcb = 1;
351
352 if (priv->uses_rxfcb)
353 frame_size += GMAC_FCB_LEN;
354
355 frame_size += priv->padding;
356
357 frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
358 INCREMENTAL_BUFFER_SIZE;
359
360 priv->rx_buffer_size = frame_size;
361}
362
Claudiu Manoila328ac92014-02-24 12:13:42 +0200363static void gfar_mac_rx_config(struct gfar_private *priv)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000364{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000365 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000366 u32 rctrl = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000367
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000368 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000369 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000370 /* Program the RIR0 reg with the required distribution */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200371 if (priv->poll_mode == GFAR_SQ_POLLING)
372 gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
373 else /* GFAR_MQ_POLLING */
374 gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000375 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000376
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000377 /* Restore PROMISC mode */
Claudiu Manoila328ac92014-02-24 12:13:42 +0200378 if (priv->ndev->flags & IFF_PROMISC)
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000379 rctrl |= RCTRL_PROM;
380
Claudiu Manoil88302642014-02-24 12:13:43 +0200381 if (priv->ndev->features & NETIF_F_RXCSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000382 rctrl |= RCTRL_CHECKSUMMING;
383
Claudiu Manoil88302642014-02-24 12:13:43 +0200384 if (priv->extended_hash)
385 rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000386
387 if (priv->padding) {
388 rctrl &= ~RCTRL_PAL_MASK;
389 rctrl |= RCTRL_PADDING(priv->padding);
390 }
391
Manfred Rudigier97553f72010-06-11 01:49:05 +0000392 /* Enable HW time stamping if requested from user space */
Claudiu Manoil88302642014-02-24 12:13:43 +0200393 if (priv->hwts_rx_en)
Manfred Rudigier97553f72010-06-11 01:49:05 +0000394 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
395
Claudiu Manoil88302642014-02-24 12:13:43 +0200396 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
Sebastian Pöhnb852b722011-07-26 00:03:13 +0000397 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000398
399 /* Init rctrl based on our settings */
400 gfar_write(&regs->rctrl, rctrl);
Claudiu Manoila328ac92014-02-24 12:13:42 +0200401}
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000402
Claudiu Manoila328ac92014-02-24 12:13:42 +0200403static void gfar_mac_tx_config(struct gfar_private *priv)
404{
405 struct gfar __iomem *regs = priv->gfargrp[0].regs;
406 u32 tctrl = 0;
407
408 if (priv->ndev->features & NETIF_F_IP_CSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000409 tctrl |= TCTRL_INIT_CSUM;
410
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +0000411 if (priv->prio_sched_en)
412 tctrl |= TCTRL_TXSCHED_PRIO;
413 else {
414 tctrl |= TCTRL_TXSCHED_WRRS;
415 gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
416 gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
417 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000418
Claudiu Manoil88302642014-02-24 12:13:43 +0200419 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
420 tctrl |= TCTRL_VLINS;
421
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000422 gfar_write(&regs->tctrl, tctrl);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000423}
424
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200425static void gfar_configure_coalescing(struct gfar_private *priv,
426 unsigned long tx_mask, unsigned long rx_mask)
427{
428 struct gfar __iomem *regs = priv->gfargrp[0].regs;
429 u32 __iomem *baddr;
430
431 if (priv->mode == MQ_MG_MODE) {
432 int i = 0;
433
434 baddr = &regs->txic0;
435 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
436 gfar_write(baddr + i, 0);
437 if (likely(priv->tx_queue[i]->txcoalescing))
438 gfar_write(baddr + i, priv->tx_queue[i]->txic);
439 }
440
441 baddr = &regs->rxic0;
442 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
443 gfar_write(baddr + i, 0);
444 if (likely(priv->rx_queue[i]->rxcoalescing))
445 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
446 }
447 } else {
448 /* Backward compatible case -- even if we enable
449 * multiple queues, there's only single reg to program
450 */
451 gfar_write(&regs->txic, 0);
452 if (likely(priv->tx_queue[0]->txcoalescing))
453 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
454
455 gfar_write(&regs->rxic, 0);
456 if (unlikely(priv->rx_queue[0]->rxcoalescing))
457 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
458 }
459}
460
461void gfar_configure_coalescing_all(struct gfar_private *priv)
462{
463 gfar_configure_coalescing(priv, 0xFF, 0xFF);
464}
465
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000466static struct net_device_stats *gfar_get_stats(struct net_device *dev)
467{
468 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000469 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
470 unsigned long tx_packets = 0, tx_bytes = 0;
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000471 int i;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000472
473 for (i = 0; i < priv->num_rx_queues; i++) {
474 rx_packets += priv->rx_queue[i]->stats.rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000475 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000476 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
477 }
478
479 dev->stats.rx_packets = rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000480 dev->stats.rx_bytes = rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000481 dev->stats.rx_dropped = rx_dropped;
482
483 for (i = 0; i < priv->num_tx_queues; i++) {
Eric Dumazet1ac9ad12011-01-12 12:13:14 +0000484 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
485 tx_packets += priv->tx_queue[i]->stats.tx_packets;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000486 }
487
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000488 dev->stats.tx_bytes = tx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000489 dev->stats.tx_packets = tx_packets;
490
491 return &dev->stats;
492}
493
Andy Fleming26ccfc32009-03-10 12:58:28 +0000494static const struct net_device_ops gfar_netdev_ops = {
495 .ndo_open = gfar_enet_open,
496 .ndo_start_xmit = gfar_start_xmit,
497 .ndo_stop = gfar_close,
498 .ndo_change_mtu = gfar_change_mtu,
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000499 .ndo_set_features = gfar_set_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000500 .ndo_set_rx_mode = gfar_set_multi,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000501 .ndo_tx_timeout = gfar_timeout,
502 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000503 .ndo_get_stats = gfar_get_stats,
Ben Hutchings240c1022009-07-09 17:54:35 +0000504 .ndo_set_mac_address = eth_mac_addr,
505 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000506#ifdef CONFIG_NET_POLL_CONTROLLER
507 .ndo_poll_controller = gfar_netpoll,
508#endif
509};
510
Claudiu Manoilefeddce2014-02-17 12:53:17 +0200511static void gfar_ints_disable(struct gfar_private *priv)
512{
513 int i;
514 for (i = 0; i < priv->num_grps; i++) {
515 struct gfar __iomem *regs = priv->gfargrp[i].regs;
516 /* Clear IEVENT */
517 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
518
519 /* Initialize IMASK */
520 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
521 }
522}
523
524static void gfar_ints_enable(struct gfar_private *priv)
525{
526 int i;
527 for (i = 0; i < priv->num_grps; i++) {
528 struct gfar __iomem *regs = priv->gfargrp[i].regs;
529 /* Unmask the interrupts we look for */
530 gfar_write(&regs->imask, IMASK_DEFAULT);
531 }
532}
533
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000534void lock_tx_qs(struct gfar_private *priv)
535{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000536 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000537
538 for (i = 0; i < priv->num_tx_queues; i++)
539 spin_lock(&priv->tx_queue[i]->txlock);
540}
541
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000542void unlock_tx_qs(struct gfar_private *priv)
543{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000544 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000545
546 for (i = 0; i < priv->num_tx_queues; i++)
547 spin_unlock(&priv->tx_queue[i]->txlock);
548}
549
Claudiu Manoil20862782014-02-17 12:53:14 +0200550static int gfar_alloc_tx_queues(struct gfar_private *priv)
551{
552 int i;
553
554 for (i = 0; i < priv->num_tx_queues; i++) {
555 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
556 GFP_KERNEL);
557 if (!priv->tx_queue[i])
558 return -ENOMEM;
559
560 priv->tx_queue[i]->tx_skbuff = NULL;
561 priv->tx_queue[i]->qindex = i;
562 priv->tx_queue[i]->dev = priv->ndev;
563 spin_lock_init(&(priv->tx_queue[i]->txlock));
564 }
565 return 0;
566}
567
568static int gfar_alloc_rx_queues(struct gfar_private *priv)
569{
570 int i;
571
572 for (i = 0; i < priv->num_rx_queues; i++) {
573 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
574 GFP_KERNEL);
575 if (!priv->rx_queue[i])
576 return -ENOMEM;
577
578 priv->rx_queue[i]->rx_skbuff = NULL;
579 priv->rx_queue[i]->qindex = i;
580 priv->rx_queue[i]->dev = priv->ndev;
Claudiu Manoil20862782014-02-17 12:53:14 +0200581 }
582 return 0;
583}
584
585static void gfar_free_tx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000586{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000587 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000588
589 for (i = 0; i < priv->num_tx_queues; i++)
590 kfree(priv->tx_queue[i]);
591}
592
Claudiu Manoil20862782014-02-17 12:53:14 +0200593static void gfar_free_rx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000594{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000595 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000596
597 for (i = 0; i < priv->num_rx_queues; i++)
598 kfree(priv->rx_queue[i]);
599}
600
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000601static void unmap_group_regs(struct gfar_private *priv)
602{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000603 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000604
605 for (i = 0; i < MAXGROUPS; i++)
606 if (priv->gfargrp[i].regs)
607 iounmap(priv->gfargrp[i].regs);
608}
609
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000610static void free_gfar_dev(struct gfar_private *priv)
611{
612 int i, j;
613
614 for (i = 0; i < priv->num_grps; i++)
615 for (j = 0; j < GFAR_NUM_IRQS; j++) {
616 kfree(priv->gfargrp[i].irqinfo[j]);
617 priv->gfargrp[i].irqinfo[j] = NULL;
618 }
619
620 free_netdev(priv->ndev);
621}
622
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000623static void disable_napi(struct gfar_private *priv)
624{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000625 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000626
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200627 for (i = 0; i < priv->num_grps; i++) {
628 napi_disable(&priv->gfargrp[i].napi_rx);
629 napi_disable(&priv->gfargrp[i].napi_tx);
630 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000631}
632
633static void enable_napi(struct gfar_private *priv)
634{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000635 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000636
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200637 for (i = 0; i < priv->num_grps; i++) {
638 napi_enable(&priv->gfargrp[i].napi_rx);
639 napi_enable(&priv->gfargrp[i].napi_tx);
640 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000641}
642
643static int gfar_parse_group(struct device_node *np,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000644 struct gfar_private *priv, const char *model)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000645{
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000646 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000647 int i;
648
Paul Gortmaker7c1e7e92013-02-04 09:49:42 +0000649 for (i = 0; i < GFAR_NUM_IRQS; i++) {
650 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
651 GFP_KERNEL);
652 if (!grp->irqinfo[i])
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000653 return -ENOMEM;
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000654 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000655
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000656 grp->regs = of_iomap(np, 0);
657 if (!grp->regs)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000658 return -ENOMEM;
659
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000660 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000661
662 /* If we aren't the FEC we have multiple interrupts */
663 if (model && strcasecmp(model, "FEC")) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000664 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
665 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
666 if (gfar_irq(grp, TX)->irq == NO_IRQ ||
667 gfar_irq(grp, RX)->irq == NO_IRQ ||
668 gfar_irq(grp, ER)->irq == NO_IRQ)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000669 return -EINVAL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000670 }
671
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000672 grp->priv = priv;
673 spin_lock_init(&grp->grplock);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000674 if (priv->mode == MQ_MG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200675 u32 *rxq_mask, *txq_mask;
676 rxq_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
677 txq_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
678
679 if (priv->poll_mode == GFAR_SQ_POLLING) {
680 /* One Q per interrupt group: Q0 to G0, Q1 to G1 */
681 grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
682 grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
683 } else { /* GFAR_MQ_POLLING */
684 grp->rx_bit_map = rxq_mask ?
685 *rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
686 grp->tx_bit_map = txq_mask ?
687 *txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
688 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000689 } else {
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000690 grp->rx_bit_map = 0xFF;
691 grp->tx_bit_map = 0xFF;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000692 }
Claudiu Manoil20862782014-02-17 12:53:14 +0200693
694 /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
695 * right to left, so we need to revert the 8 bits to get the q index
696 */
697 grp->rx_bit_map = bitrev8(grp->rx_bit_map);
698 grp->tx_bit_map = bitrev8(grp->tx_bit_map);
699
700 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
701 * also assign queues to groups
702 */
703 for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200704 if (!grp->rx_queue)
705 grp->rx_queue = priv->rx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200706 grp->num_rx_queues++;
707 grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
708 priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
709 priv->rx_queue[i]->grp = grp;
710 }
711
712 for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200713 if (!grp->tx_queue)
714 grp->tx_queue = priv->tx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200715 grp->num_tx_queues++;
716 grp->tstat |= (TSTAT_CLEAR_THALT >> i);
717 priv->tqueue |= (TQUEUE_EN0 >> i);
718 priv->tx_queue[i]->grp = grp;
719 }
720
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000721 priv->num_grps++;
722
723 return 0;
724}
725
Grant Likely2dc11582010-08-06 09:25:50 -0600726static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800727{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800728 const char *model;
729 const char *ctype;
730 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000731 int err = 0, i;
732 struct net_device *dev = NULL;
733 struct gfar_private *priv = NULL;
Grant Likely61c7a082010-04-13 16:12:29 -0700734 struct device_node *np = ofdev->dev.of_node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000735 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800736 const u32 *stash;
737 const u32 *stash_len;
738 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000739 unsigned int num_tx_qs, num_rx_qs;
740 u32 *tx_queues, *rx_queues;
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200741 unsigned short mode, poll_mode;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800742
743 if (!np || !of_device_is_available(np))
744 return -ENODEV;
745
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200746 if (of_device_is_compatible(np, "fsl,etsec2")) {
747 mode = MQ_MG_MODE;
748 poll_mode = GFAR_SQ_POLLING;
749 } else {
750 mode = SQ_SG_MODE;
751 poll_mode = GFAR_SQ_POLLING;
752 }
753
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200754 /* parse the num of HW tx and rx queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000755 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200756 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
757
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200758 if (mode == SQ_SG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200759 num_tx_qs = 1;
760 num_rx_qs = 1;
761 } else { /* MQ_MG_MODE */
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200762 /* get the actual number of supported groups */
763 unsigned int num_grps = of_get_available_child_count(np);
764
765 if (num_grps == 0 || num_grps > MAXGROUPS) {
766 dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
767 num_grps);
768 pr_err("Cannot do alloc_etherdev, aborting\n");
769 return -EINVAL;
770 }
771
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200772 if (poll_mode == GFAR_SQ_POLLING) {
Claudiu Manoilc65d7532014-03-21 09:33:17 +0200773 num_tx_qs = num_grps; /* one txq per int group */
774 num_rx_qs = num_grps; /* one rxq per int group */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200775 } else { /* GFAR_MQ_POLLING */
776 num_tx_qs = tx_queues ? *tx_queues : 1;
777 num_rx_qs = rx_queues ? *rx_queues : 1;
778 }
779 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000780
781 if (num_tx_qs > MAX_TX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000782 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
783 num_tx_qs, MAX_TX_QS);
784 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000785 return -EINVAL;
786 }
787
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000788 if (num_rx_qs > MAX_RX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000789 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
790 num_rx_qs, MAX_RX_QS);
791 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000792 return -EINVAL;
793 }
794
795 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
796 dev = *pdev;
797 if (NULL == dev)
798 return -ENOMEM;
799
800 priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000801 priv->ndev = dev;
802
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200803 priv->mode = mode;
804 priv->poll_mode = poll_mode;
805
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000806 priv->num_tx_queues = num_tx_qs;
Ben Hutchingsfe069122010-09-27 08:27:37 +0000807 netif_set_real_num_rx_queues(dev, num_rx_qs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000808 priv->num_rx_queues = num_rx_qs;
Claudiu Manoil20862782014-02-17 12:53:14 +0200809
810 err = gfar_alloc_tx_queues(priv);
811 if (err)
812 goto tx_alloc_failed;
813
814 err = gfar_alloc_rx_queues(priv);
815 if (err)
816 goto rx_alloc_failed;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800817
Jan Ceuleers0977f812012-06-05 03:42:12 +0000818 /* Init Rx queue filer rule set linked list */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700819 INIT_LIST_HEAD(&priv->rx_list.list);
820 priv->rx_list.count = 0;
821 mutex_init(&priv->rx_queue_access);
822
Andy Flemingb31a1d82008-12-16 15:29:15 -0800823 model = of_get_property(np, "model", NULL);
824
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000825 for (i = 0; i < MAXGROUPS; i++)
826 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800827
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000828 /* Parse and initialize group specific information */
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200829 if (priv->mode == MQ_MG_MODE) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000830 for_each_child_of_node(np, child) {
831 err = gfar_parse_group(child, priv, model);
832 if (err)
833 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800834 }
Claudiu Manoilb338ce22014-03-11 18:01:24 +0200835 } else { /* SQ_SG_MODE */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000836 err = gfar_parse_group(np, priv, model);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000837 if (err)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000838 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800839 }
840
Andy Fleming4d7902f2009-02-04 16:43:44 -0800841 stash = of_get_property(np, "bd-stash", NULL);
842
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000843 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800844 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
845 priv->bd_stash_en = 1;
846 }
847
848 stash_len = of_get_property(np, "rx-stash-len", NULL);
849
850 if (stash_len)
851 priv->rx_stash_size = *stash_len;
852
853 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
854
855 if (stash_idx)
856 priv->rx_stash_index = *stash_idx;
857
858 if (stash_len || stash_idx)
859 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
860
Andy Flemingb31a1d82008-12-16 15:29:15 -0800861 mac_addr = of_get_mac_address(np);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000862
Andy Flemingb31a1d82008-12-16 15:29:15 -0800863 if (mac_addr)
Joe Perches6a3c9102011-11-16 09:38:02 +0000864 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800865
866 if (model && !strcasecmp(model, "TSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200867 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000868 FSL_GIANFAR_DEV_HAS_COALESCE |
869 FSL_GIANFAR_DEV_HAS_RMON |
870 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
871
Andy Flemingb31a1d82008-12-16 15:29:15 -0800872 if (model && !strcasecmp(model, "eTSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200873 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000874 FSL_GIANFAR_DEV_HAS_COALESCE |
875 FSL_GIANFAR_DEV_HAS_RMON |
876 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000877 FSL_GIANFAR_DEV_HAS_CSUM |
878 FSL_GIANFAR_DEV_HAS_VLAN |
879 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
880 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
881 FSL_GIANFAR_DEV_HAS_TIMER;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800882
883 ctype = of_get_property(np, "phy-connection-type", NULL);
884
885 /* We only care about rgmii-id. The rest are autodetected */
886 if (ctype && !strcmp(ctype, "rgmii-id"))
887 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
888 else
889 priv->interface = PHY_INTERFACE_MODE_MII;
890
891 if (of_get_property(np, "fsl,magic-packet", NULL))
892 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
893
Grant Likelyfe192a42009-04-25 12:53:12 +0000894 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800895
Florian Fainellibe403642014-05-22 09:47:48 -0700896 /* In the case of a fixed PHY, the DT node associated
897 * to the PHY is the Ethernet MAC DT node.
898 */
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200899 if (!priv->phy_node && of_phy_is_fixed_link(np)) {
Florian Fainellibe403642014-05-22 09:47:48 -0700900 err = of_phy_register_fixed_link(np);
901 if (err)
902 goto err_grp_init;
903
Uwe Kleine-König6f2c9bd2014-08-07 22:17:07 +0200904 priv->phy_node = of_node_get(np);
Florian Fainellibe403642014-05-22 09:47:48 -0700905 }
906
Andy Flemingb31a1d82008-12-16 15:29:15 -0800907 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000908 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800909
910 return 0;
911
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000912err_grp_init:
913 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +0200914rx_alloc_failed:
915 gfar_free_rx_queues(priv);
916tx_alloc_failed:
917 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000918 free_gfar_dev(priv);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800919 return err;
920}
921
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000922static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000923{
924 struct hwtstamp_config config;
925 struct gfar_private *priv = netdev_priv(netdev);
926
927 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
928 return -EFAULT;
929
930 /* reserved for future extensions */
931 if (config.flags)
932 return -EINVAL;
933
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000934 switch (config.tx_type) {
935 case HWTSTAMP_TX_OFF:
936 priv->hwts_tx_en = 0;
937 break;
938 case HWTSTAMP_TX_ON:
939 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
940 return -ERANGE;
941 priv->hwts_tx_en = 1;
942 break;
943 default:
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000944 return -ERANGE;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000945 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000946
947 switch (config.rx_filter) {
948 case HWTSTAMP_FILTER_NONE:
Manfred Rudigier97553f72010-06-11 01:49:05 +0000949 if (priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000950 priv->hwts_rx_en = 0;
Claudiu Manoil08511332014-02-24 12:13:45 +0200951 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000952 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000953 break;
954 default:
955 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
956 return -ERANGE;
Manfred Rudigier97553f72010-06-11 01:49:05 +0000957 if (!priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000958 priv->hwts_rx_en = 1;
Claudiu Manoil08511332014-02-24 12:13:45 +0200959 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000960 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000961 config.rx_filter = HWTSTAMP_FILTER_ALL;
962 break;
963 }
964
965 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
966 -EFAULT : 0;
967}
968
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000969static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
970{
971 struct hwtstamp_config config;
972 struct gfar_private *priv = netdev_priv(netdev);
973
974 config.flags = 0;
975 config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
976 config.rx_filter = (priv->hwts_rx_en ?
977 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
978
979 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
980 -EFAULT : 0;
981}
982
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000983static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
984{
985 struct gfar_private *priv = netdev_priv(dev);
986
987 if (!netif_running(dev))
988 return -EINVAL;
989
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000990 if (cmd == SIOCSHWTSTAMP)
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000991 return gfar_hwtstamp_set(dev, rq);
992 if (cmd == SIOCGHWTSTAMP)
993 return gfar_hwtstamp_get(dev, rq);
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000994
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000995 if (!priv->phydev)
996 return -ENODEV;
997
Richard Cochran28b04112010-07-17 08:48:55 +0000998 return phy_mii_ioctl(priv->phydev, rq, cmd);
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000999}
1000
Anton Vorontsov18294ad2009-11-04 12:53:00 +00001001static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
1002 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001003{
1004 u32 rqfpr = FPR_FILER_MASK;
1005 u32 rqfcr = 0x0;
1006
1007 rqfar--;
1008 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001009 priv->ftp_rqfpr[rqfar] = rqfpr;
1010 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001011 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1012
1013 rqfar--;
1014 rqfcr = RQFCR_CMP_NOMATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001015 priv->ftp_rqfpr[rqfar] = rqfpr;
1016 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001017 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1018
1019 rqfar--;
1020 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
1021 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001022 priv->ftp_rqfcr[rqfar] = rqfcr;
1023 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001024 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1025
1026 rqfar--;
1027 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
1028 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001029 priv->ftp_rqfcr[rqfar] = rqfcr;
1030 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001031 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1032
1033 return rqfar;
1034}
1035
1036static void gfar_init_filer_table(struct gfar_private *priv)
1037{
1038 int i = 0x0;
1039 u32 rqfar = MAX_FILER_IDX;
1040 u32 rqfcr = 0x0;
1041 u32 rqfpr = FPR_FILER_MASK;
1042
1043 /* Default rule */
1044 rqfcr = RQFCR_CMP_MATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001045 priv->ftp_rqfcr[rqfar] = rqfcr;
1046 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001047 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1048
1049 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1050 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1051 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1052 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1053 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1054 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1055
Uwe Kleine-König85dd08e2010-06-11 12:16:55 +02001056 /* cur_filer_idx indicated the first non-masked rule */
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001057 priv->cur_filer_idx = rqfar;
1058
1059 /* Rest are masked rules */
1060 rqfcr = RQFCR_CMP_NOMATCH;
1061 for (i = 0; i < rqfar; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001062 priv->ftp_rqfcr[i] = rqfcr;
1063 priv->ftp_rqfpr[i] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001064 gfar_write_filer(priv, i, rqfcr, rqfpr);
1065 }
1066}
1067
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001068#ifdef CONFIG_PPC
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001069static void __gfar_detect_errata_83xx(struct gfar_private *priv)
Anton Vorontsov7d350972010-06-30 06:39:12 +00001070{
Anton Vorontsov7d350972010-06-30 06:39:12 +00001071 unsigned int pvr = mfspr(SPRN_PVR);
1072 unsigned int svr = mfspr(SPRN_SVR);
1073 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1074 unsigned int rev = svr & 0xffff;
1075
1076 /* MPC8313 Rev 2.0 and higher; All MPC837x */
1077 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001078 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001079 priv->errata |= GFAR_ERRATA_74;
1080
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001081 /* MPC8313 and MPC837x all rev */
1082 if ((pvr == 0x80850010 && mod == 0x80b0) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001083 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001084 priv->errata |= GFAR_ERRATA_76;
1085
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001086 /* MPC8313 Rev < 2.0 */
1087 if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
Alex Dubov4363c2f2011-03-16 17:57:13 +00001088 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001089}
1090
1091static void __gfar_detect_errata_85xx(struct gfar_private *priv)
1092{
1093 unsigned int svr = mfspr(SPRN_SVR);
1094
1095 if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
1096 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil53fad772013-10-09 20:20:42 +03001097 if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
1098 ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
1099 priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001100}
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001101#endif
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001102
1103static void gfar_detect_errata(struct gfar_private *priv)
1104{
1105 struct device *dev = &priv->ofdev->dev;
1106
1107 /* no plans to fix */
1108 priv->errata |= GFAR_ERRATA_A002;
1109
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001110#ifdef CONFIG_PPC
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001111 if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
1112 __gfar_detect_errata_85xx(priv);
1113 else /* non-mpc85xx parts, i.e. e300 core based */
1114 __gfar_detect_errata_83xx(priv);
Claudiu Manoild6ef0bc2014-10-07 10:44:32 +03001115#endif
Alex Dubov4363c2f2011-03-16 17:57:13 +00001116
Anton Vorontsov7d350972010-06-30 06:39:12 +00001117 if (priv->errata)
1118 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1119 priv->errata);
1120}
1121
Claudiu Manoil08511332014-02-24 12:13:45 +02001122void gfar_mac_reset(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Claudiu Manoil20862782014-02-17 12:53:14 +02001124 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Claudiu Manoila328ac92014-02-24 12:13:42 +02001125 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001128 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Andy Flemingb98ac702009-02-04 16:38:05 -08001130 /* We need to delay at least 3 TX clocks */
Claudiu Manoila328ac92014-02-24 12:13:42 +02001131 udelay(3);
Andy Flemingb98ac702009-02-04 16:38:05 -08001132
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001133 /* the soft reset bit is not self-resetting, so we need to
1134 * clear it before resuming normal operation
1135 */
Claudiu Manoil20862782014-02-17 12:53:14 +02001136 gfar_write(&regs->maccfg1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Claudiu Manoila328ac92014-02-24 12:13:42 +02001138 udelay(3);
1139
Claudiu Manoil88302642014-02-24 12:13:43 +02001140 /* Compute rx_buff_size based on config flags */
1141 gfar_rx_buff_size_config(priv);
1142
1143 /* Initialize the max receive frame/buffer lengths */
1144 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Claudiu Manoila328ac92014-02-24 12:13:42 +02001145 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1146
1147 /* Initialize the Minimum Frame Length Register */
1148 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* Initialize MACCFG2. */
Anton Vorontsov7d350972010-06-30 06:39:12 +00001151 tempval = MACCFG2_INIT_SETTINGS;
Claudiu Manoil88302642014-02-24 12:13:43 +02001152
1153 /* If the mtu is larger than the max size for standard
1154 * ethernet frames (ie, a jumbo frame), then set maccfg2
1155 * to allow huge frames, and to check the length
1156 */
1157 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
1158 gfar_has_errata(priv, GFAR_ERRATA_74))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001159 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
Claudiu Manoil88302642014-02-24 12:13:43 +02001160
Anton Vorontsov7d350972010-06-30 06:39:12 +00001161 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Claudiu Manoila328ac92014-02-24 12:13:42 +02001163 /* Clear mac addr hash registers */
1164 gfar_write(&regs->igaddr0, 0);
1165 gfar_write(&regs->igaddr1, 0);
1166 gfar_write(&regs->igaddr2, 0);
1167 gfar_write(&regs->igaddr3, 0);
1168 gfar_write(&regs->igaddr4, 0);
1169 gfar_write(&regs->igaddr5, 0);
1170 gfar_write(&regs->igaddr6, 0);
1171 gfar_write(&regs->igaddr7, 0);
1172
1173 gfar_write(&regs->gaddr0, 0);
1174 gfar_write(&regs->gaddr1, 0);
1175 gfar_write(&regs->gaddr2, 0);
1176 gfar_write(&regs->gaddr3, 0);
1177 gfar_write(&regs->gaddr4, 0);
1178 gfar_write(&regs->gaddr5, 0);
1179 gfar_write(&regs->gaddr6, 0);
1180 gfar_write(&regs->gaddr7, 0);
1181
1182 if (priv->extended_hash)
1183 gfar_clear_exact_match(priv->ndev);
1184
1185 gfar_mac_rx_config(priv);
1186
1187 gfar_mac_tx_config(priv);
1188
1189 gfar_set_mac_address(priv->ndev);
1190
1191 gfar_set_multi(priv->ndev);
1192
1193 /* clear ievent and imask before configuring coalescing */
1194 gfar_ints_disable(priv);
1195
1196 /* Configure the coalescing support */
1197 gfar_configure_coalescing_all(priv);
1198}
1199
1200static void gfar_hw_init(struct gfar_private *priv)
1201{
1202 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1203 u32 attrs;
1204
1205 /* Stop the DMA engine now, in case it was running before
1206 * (The firmware could have used it, and left it running).
1207 */
1208 gfar_halt(priv);
1209
1210 gfar_mac_reset(priv);
1211
1212 /* Zero out the rmon mib registers if it has them */
1213 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1214 memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1215
1216 /* Mask off the CAM interrupts */
1217 gfar_write(&regs->rmon.cam1, 0xffffffff);
1218 gfar_write(&regs->rmon.cam2, 0xffffffff);
1219 }
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001222 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Claudiu Manoil34018fd2014-02-17 12:53:15 +02001224 /* Set the extraction length and index */
1225 attrs = ATTRELI_EL(priv->rx_stash_size) |
1226 ATTRELI_EI(priv->rx_stash_index);
1227
1228 gfar_write(&regs->attreli, attrs);
1229
1230 /* Start with defaults, and add stashing
1231 * depending on driver parameters
1232 */
1233 attrs = ATTR_INIT_SETTINGS;
1234
1235 if (priv->bd_stash_en)
1236 attrs |= ATTR_BDSTASH;
1237
1238 if (priv->rx_stash_size != 0)
1239 attrs |= ATTR_BUFSTASH;
1240
1241 gfar_write(&regs->attr, attrs);
1242
1243 /* FIFO configs */
1244 gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
1245 gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
1246 gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
1247
Claudiu Manoil20862782014-02-17 12:53:14 +02001248 /* Program the interrupt steering regs, only for MG devices */
1249 if (priv->num_grps > 1)
1250 gfar_write_isrg(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Xiubo Li898157e2014-06-04 16:49:16 +08001253static void gfar_init_addr_hash_table(struct gfar_private *priv)
Claudiu Manoil20862782014-02-17 12:53:14 +02001254{
1255 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001256
Andy Flemingb31a1d82008-12-16 15:29:15 -08001257 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001258 priv->extended_hash = 1;
1259 priv->hash_width = 9;
1260
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001261 priv->hash_regs[0] = &regs->igaddr0;
1262 priv->hash_regs[1] = &regs->igaddr1;
1263 priv->hash_regs[2] = &regs->igaddr2;
1264 priv->hash_regs[3] = &regs->igaddr3;
1265 priv->hash_regs[4] = &regs->igaddr4;
1266 priv->hash_regs[5] = &regs->igaddr5;
1267 priv->hash_regs[6] = &regs->igaddr6;
1268 priv->hash_regs[7] = &regs->igaddr7;
1269 priv->hash_regs[8] = &regs->gaddr0;
1270 priv->hash_regs[9] = &regs->gaddr1;
1271 priv->hash_regs[10] = &regs->gaddr2;
1272 priv->hash_regs[11] = &regs->gaddr3;
1273 priv->hash_regs[12] = &regs->gaddr4;
1274 priv->hash_regs[13] = &regs->gaddr5;
1275 priv->hash_regs[14] = &regs->gaddr6;
1276 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001277
1278 } else {
1279 priv->extended_hash = 0;
1280 priv->hash_width = 8;
1281
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001282 priv->hash_regs[0] = &regs->gaddr0;
1283 priv->hash_regs[1] = &regs->gaddr1;
1284 priv->hash_regs[2] = &regs->gaddr2;
1285 priv->hash_regs[3] = &regs->gaddr3;
1286 priv->hash_regs[4] = &regs->gaddr4;
1287 priv->hash_regs[5] = &regs->gaddr5;
1288 priv->hash_regs[6] = &regs->gaddr6;
1289 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001290 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001291}
1292
1293/* Set up the ethernet device structure, private data,
1294 * and anything else we need before we start
1295 */
1296static int gfar_probe(struct platform_device *ofdev)
1297{
1298 struct net_device *dev = NULL;
1299 struct gfar_private *priv = NULL;
1300 int err = 0, i;
1301
1302 err = gfar_of_init(ofdev, &dev);
1303
1304 if (err)
1305 return err;
1306
1307 priv = netdev_priv(dev);
1308 priv->ndev = dev;
1309 priv->ofdev = ofdev;
1310 priv->dev = &ofdev->dev;
1311 SET_NETDEV_DEV(dev, &ofdev->dev);
1312
1313 spin_lock_init(&priv->bflock);
1314 INIT_WORK(&priv->reset_task, gfar_reset_task);
1315
1316 platform_set_drvdata(ofdev, priv);
1317
1318 gfar_detect_errata(priv);
1319
Claudiu Manoil20862782014-02-17 12:53:14 +02001320 /* Set the dev->base_addr to the gfar reg region */
1321 dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
1322
1323 /* Fill in the dev structure */
1324 dev->watchdog_timeo = TX_TIMEOUT;
1325 dev->mtu = 1500;
1326 dev->netdev_ops = &gfar_netdev_ops;
1327 dev->ethtool_ops = &gfar_ethtool_ops;
1328
1329 /* Register for napi ...We are registering NAPI for each grp */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02001330 for (i = 0; i < priv->num_grps; i++) {
1331 if (priv->poll_mode == GFAR_SQ_POLLING) {
1332 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1333 gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
1334 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1335 gfar_poll_tx_sq, 2);
1336 } else {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02001337 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1338 gfar_poll_rx, GFAR_DEV_WEIGHT);
1339 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1340 gfar_poll_tx, 2);
1341 }
1342 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001343
1344 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
1345 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
1346 NETIF_F_RXCSUM;
1347 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
1348 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
1349 }
1350
1351 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
1352 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
1353 NETIF_F_HW_VLAN_CTAG_RX;
1354 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1355 }
1356
1357 gfar_init_addr_hash_table(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001358
Claudiu Manoil532c37b2014-02-17 12:53:16 +02001359 /* Insert receive time stamps into padding alignment bytes */
1360 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1361 priv->padding = 8;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001362
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001363 if (dev->features & NETIF_F_IP_CSUM ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001364 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
Wu Jiajun-B06378bee9e582012-05-21 23:00:48 +00001365 dev->needed_headroom = GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001369 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001370 for (i = 0; i < priv->num_tx_queues; i++) {
1371 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1372 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1373 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1374 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1375 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001376
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001377 for (i = 0; i < priv->num_rx_queues; i++) {
1378 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1379 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1380 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Jan Ceuleers0977f812012-06-05 03:42:12 +00001383 /* always enable rx filer */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001384 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001385 /* Enable most messages by default */
1386 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +00001387 /* use pritority h/w tx queue scheduling for single queue devices */
1388 if (priv->num_tx_queues == 1)
1389 priv->prio_sched_en = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001390
Claudiu Manoil08511332014-02-24 12:13:45 +02001391 set_bit(GFAR_DOWN, &priv->state);
1392
Claudiu Manoila328ac92014-02-24 12:13:42 +02001393 gfar_hw_init(priv);
Trent Piephod3eab822008-10-02 11:12:24 +00001394
Fabio Estevamd4c642e2014-06-03 19:55:38 -03001395 /* Carrier starts down, phylib will bring it up */
1396 netif_carrier_off(dev);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 err = register_netdev(dev);
1399
1400 if (err) {
Joe Perches59deab22011-06-14 08:57:47 +00001401 pr_err("%s: Cannot register net device, aborting\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto register_fail;
1403 }
1404
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001405 device_init_wakeup(&dev->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001406 priv->device_flags &
1407 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001408
Dai Harukic50a5d92008-12-17 16:51:32 -08001409 /* fill out IRQ number and name fields */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001410 for (i = 0; i < priv->num_grps; i++) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001411 struct gfar_priv_grp *grp = &priv->gfargrp[i];
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001412 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001413 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001414 dev->name, "_g", '0' + i, "_tx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001415 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001416 dev->name, "_g", '0' + i, "_rx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001417 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001418 dev->name, "_g", '0' + i, "_er");
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001419 } else
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001420 strcpy(gfar_irq(grp, TX)->name, dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001421 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001422
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001423 /* Initialize the filer table */
1424 gfar_init_filer_table(priv);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /* Print out the device info */
Joe Perches59deab22011-06-14 08:57:47 +00001427 netdev_info(dev, "mac: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Jan Ceuleers0977f812012-06-05 03:42:12 +00001429 /* Even more device info helps when determining which kernel
1430 * provided which set of benchmarks.
1431 */
Joe Perches59deab22011-06-14 08:57:47 +00001432 netdev_info(dev, "Running with NAPI enabled\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001433 for (i = 0; i < priv->num_rx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001434 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1435 i, priv->rx_queue[i]->rx_ring_size);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001436 for (i = 0; i < priv->num_tx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001437 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1438 i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 return 0;
1441
1442register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001443 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001444 gfar_free_rx_queues(priv);
1445 gfar_free_tx_queues(priv);
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001446 of_node_put(priv->phy_node);
1447 of_node_put(priv->tbi_node);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001448 free_gfar_dev(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Grant Likely2dc11582010-08-06 09:25:50 -06001452static int gfar_remove(struct platform_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001454 struct gfar_private *priv = platform_get_drvdata(ofdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Uwe Kleine-König888c88b2014-08-07 21:20:12 +02001456 of_node_put(priv->phy_node);
1457 of_node_put(priv->tbi_node);
Grant Likelyfe192a42009-04-25 12:53:12 +00001458
David S. Millerd9d8e042009-09-06 01:41:02 -07001459 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001460 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001461 gfar_free_rx_queues(priv);
1462 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001463 free_gfar_dev(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 return 0;
1466}
1467
Scott Woodd87eb122008-07-11 18:04:45 -05001468#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001469
1470static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001471{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001472 struct gfar_private *priv = dev_get_drvdata(dev);
1473 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001474 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001475 unsigned long flags;
1476 u32 tempval;
1477
1478 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001479 (priv->device_flags &
1480 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001481
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001482 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001483
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001484 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001485
1486 local_irq_save(flags);
1487 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001488
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001489 gfar_halt_nodisable(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001490
1491 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001492 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001493
1494 tempval &= ~MACCFG1_TX_EN;
1495
1496 if (!magic_packet)
1497 tempval &= ~MACCFG1_RX_EN;
1498
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001499 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001500
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001501 unlock_tx_qs(priv);
1502 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001503
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001504 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001505
1506 if (magic_packet) {
1507 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001508 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001509
1510 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001511 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001512 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001513 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001514 } else {
1515 phy_stop(priv->phydev);
1516 }
1517 }
1518
1519 return 0;
1520}
1521
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001522static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001523{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001524 struct gfar_private *priv = dev_get_drvdata(dev);
1525 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001526 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001527 unsigned long flags;
1528 u32 tempval;
1529 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001530 (priv->device_flags &
1531 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001532
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001533 if (!netif_running(ndev)) {
1534 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001535 return 0;
1536 }
1537
1538 if (!magic_packet && priv->phydev)
1539 phy_start(priv->phydev);
1540
1541 /* Disable Magic Packet mode, in case something
1542 * else woke us up.
1543 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001544 local_irq_save(flags);
1545 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001546
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001547 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001548 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001549 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001550
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001551 gfar_start(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001552
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001553 unlock_tx_qs(priv);
1554 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001555
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001556 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001557
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001558 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001559
1560 return 0;
1561}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001562
1563static int gfar_restore(struct device *dev)
1564{
1565 struct gfar_private *priv = dev_get_drvdata(dev);
1566 struct net_device *ndev = priv->ndev;
1567
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001568 if (!netif_running(ndev)) {
1569 netif_device_attach(ndev);
1570
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001571 return 0;
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001572 }
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001573
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001574 if (gfar_init_bds(ndev)) {
1575 free_skb_resources(priv);
1576 return -ENOMEM;
1577 }
1578
Claudiu Manoila328ac92014-02-24 12:13:42 +02001579 gfar_mac_reset(priv);
1580
1581 gfar_init_tx_rx_base(priv);
1582
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001583 gfar_start(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001584
1585 priv->oldlink = 0;
1586 priv->oldspeed = 0;
1587 priv->oldduplex = -1;
1588
1589 if (priv->phydev)
1590 phy_start(priv->phydev);
1591
1592 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001593 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001594
1595 return 0;
1596}
1597
1598static struct dev_pm_ops gfar_pm_ops = {
1599 .suspend = gfar_suspend,
1600 .resume = gfar_resume,
1601 .freeze = gfar_suspend,
1602 .thaw = gfar_resume,
1603 .restore = gfar_restore,
1604};
1605
1606#define GFAR_PM_OPS (&gfar_pm_ops)
1607
Scott Woodd87eb122008-07-11 18:04:45 -05001608#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001609
1610#define GFAR_PM_OPS NULL
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001611
Scott Woodd87eb122008-07-11 18:04:45 -05001612#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001614/* Reads the controller's registers to determine what interface
1615 * connects it to the PHY.
1616 */
1617static phy_interface_t gfar_get_interface(struct net_device *dev)
1618{
1619 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001620 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001621 u32 ecntrl;
1622
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001623 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001624
1625 if (ecntrl & ECNTRL_SGMII_MODE)
1626 return PHY_INTERFACE_MODE_SGMII;
1627
1628 if (ecntrl & ECNTRL_TBI_MODE) {
1629 if (ecntrl & ECNTRL_REDUCED_MODE)
1630 return PHY_INTERFACE_MODE_RTBI;
1631 else
1632 return PHY_INTERFACE_MODE_TBI;
1633 }
1634
1635 if (ecntrl & ECNTRL_REDUCED_MODE) {
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001636 if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001637 return PHY_INTERFACE_MODE_RMII;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001638 }
Andy Fleming7132ab72007-07-11 11:43:07 -05001639 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001640 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001641
Jan Ceuleers0977f812012-06-05 03:42:12 +00001642 /* This isn't autodetected right now, so it must
Andy Fleming7132ab72007-07-11 11:43:07 -05001643 * be set by the device tree or platform code.
1644 */
1645 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1646 return PHY_INTERFACE_MODE_RGMII_ID;
1647
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001648 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001649 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001650 }
1651
Andy Flemingb31a1d82008-12-16 15:29:15 -08001652 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001653 return PHY_INTERFACE_MODE_GMII;
1654
1655 return PHY_INTERFACE_MODE_MII;
1656}
1657
1658
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001659/* Initializes driver's PHY state, and attaches to the PHY.
1660 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
1662static int init_phy(struct net_device *dev)
1663{
1664 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001665 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001666 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001667 GFAR_SUPPORTED_GBIT : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001668 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 priv->oldlink = 0;
1671 priv->oldspeed = 0;
1672 priv->oldduplex = -1;
1673
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001674 interface = gfar_get_interface(dev);
1675
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001676 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1677 interface);
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001678 if (!priv->phydev) {
1679 dev_err(&dev->dev, "could not attach to PHY\n");
1680 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Kapil Junejad3c12872007-05-11 18:25:11 -05001683 if (interface == PHY_INTERFACE_MODE_SGMII)
1684 gfar_configure_serdes(dev);
1685
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001686 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001687 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1688 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Pavaluca Matei-B46610cf987af2014-10-27 10:42:42 +02001690 /* Add support for flow control, but don't advertise it by default */
1691 priv->phydev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Jan Ceuleers0977f812012-06-05 03:42:12 +00001696/* Initialize TBI PHY interface for communicating with the
Paul Gortmakerd0313582008-04-17 00:08:10 -04001697 * SERDES lynx PHY on the chip. We communicate with this PHY
1698 * through the MDIO bus on each controller, treating it as a
1699 * "normal" PHY at the address found in the TBIPA register. We assume
1700 * that the TBIPA register is valid. Either the MDIO bus code will set
1701 * it to a value that doesn't conflict with other PHYs on the bus, or the
1702 * value doesn't matter, as there are no other PHYs on the bus.
1703 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001704static void gfar_configure_serdes(struct net_device *dev)
1705{
1706 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001707 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001708
Grant Likelyfe192a42009-04-25 12:53:12 +00001709 if (!priv->tbi_node) {
1710 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1711 "device tree specify a tbi-handle\n");
1712 return;
1713 }
1714
1715 tbiphy = of_phy_find_device(priv->tbi_node);
1716 if (!tbiphy) {
1717 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001718 return;
1719 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001720
Jan Ceuleers0977f812012-06-05 03:42:12 +00001721 /* If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001722 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1723 * everything for us? Resetting it takes the link down and requires
1724 * several seconds for it to come back.
1725 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001726 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001727 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001728
Paul Gortmakerd0313582008-04-17 00:08:10 -04001729 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001730 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001731
Grant Likelyfe192a42009-04-25 12:53:12 +00001732 phy_write(tbiphy, MII_ADVERTISE,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001733 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1734 ADVERTISE_1000XPSE_ASYM);
Kapil Junejad3c12872007-05-11 18:25:11 -05001735
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001736 phy_write(tbiphy, MII_BMCR,
1737 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1738 BMCR_SPEED1000);
Kapil Junejad3c12872007-05-11 18:25:11 -05001739}
1740
Anton Vorontsov511d9342010-06-30 06:39:15 +00001741static int __gfar_is_rx_idle(struct gfar_private *priv)
1742{
1743 u32 res;
1744
Jan Ceuleers0977f812012-06-05 03:42:12 +00001745 /* Normaly TSEC should not hang on GRS commands, so we should
Anton Vorontsov511d9342010-06-30 06:39:15 +00001746 * actually wait for IEVENT_GRSC flag.
1747 */
Claudiu Manoilad3660c2013-10-09 20:20:40 +03001748 if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
Anton Vorontsov511d9342010-06-30 06:39:15 +00001749 return 0;
1750
Jan Ceuleers0977f812012-06-05 03:42:12 +00001751 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
Anton Vorontsov511d9342010-06-30 06:39:15 +00001752 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1753 * and the Rx can be safely reset.
1754 */
1755 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1756 res &= 0x7f807f80;
1757 if ((res & 0xffff) == (res >> 16))
1758 return 1;
1759
1760 return 0;
1761}
Kumar Gala0bbaf062005-06-20 10:54:21 -05001762
1763/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001764static void gfar_halt_nodisable(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001766 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 u32 tempval;
Claudiu Manoila4feee82014-10-07 10:44:34 +03001768 unsigned int timeout;
1769 int stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001771 gfar_ints_disable(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Claudiu Manoila4feee82014-10-07 10:44:34 +03001773 if (gfar_is_dma_stopped(priv))
1774 return;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001777 tempval = gfar_read(&regs->dmactrl);
Claudiu Manoila4feee82014-10-07 10:44:34 +03001778 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1779 gfar_write(&regs->dmactrl, tempval);
Anton Vorontsov511d9342010-06-30 06:39:15 +00001780
Claudiu Manoila4feee82014-10-07 10:44:34 +03001781retry:
1782 timeout = 1000;
1783 while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
1784 cpu_relax();
1785 timeout--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
Claudiu Manoila4feee82014-10-07 10:44:34 +03001787
1788 if (!timeout)
1789 stopped = gfar_is_dma_stopped(priv);
1790
1791 if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
1792 !__gfar_is_rx_idle(priv))
1793 goto retry;
Scott Woodd87eb122008-07-11 18:04:45 -05001794}
Scott Woodd87eb122008-07-11 18:04:45 -05001795
1796/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001797void gfar_halt(struct gfar_private *priv)
Scott Woodd87eb122008-07-11 18:04:45 -05001798{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001799 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001800 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001802 /* Dissable the Rx/Tx hw queues */
1803 gfar_write(&regs->rqueue, 0);
1804 gfar_write(&regs->tqueue, 0);
Scott Wood2a54adc2008-08-12 15:10:46 -05001805
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001806 mdelay(10);
1807
1808 gfar_halt_nodisable(priv);
1809
1810 /* Disable Rx/Tx DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 tempval = gfar_read(&regs->maccfg1);
1812 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1813 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001814}
1815
1816void stop_gfar(struct net_device *dev)
1817{
1818 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001819
Claudiu Manoil08511332014-02-24 12:13:45 +02001820 netif_tx_stop_all_queues(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001821
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001822 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02001823 set_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001824 smp_mb__after_atomic();
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001825
Claudiu Manoil08511332014-02-24 12:13:45 +02001826 disable_napi(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001827
Claudiu Manoil08511332014-02-24 12:13:45 +02001828 /* disable ints and gracefully shut down Rx/Tx DMA */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001829 gfar_halt(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Claudiu Manoil08511332014-02-24 12:13:45 +02001831 phy_stop(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001836static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001839 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001840 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001842 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001844 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1845 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001846 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Claudiu Manoil369ec162013-02-14 05:00:02 +00001848 dma_unmap_single(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001849 txbdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08001850 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001851 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001852 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001853 txbdp++;
Claudiu Manoil369ec162013-02-14 05:00:02 +00001854 dma_unmap_page(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001855 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001857 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001858 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1859 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001861 kfree(tx_queue->tx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001862 tx_queue->tx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001863}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001865static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1866{
1867 struct rxbd8 *rxbdp;
1868 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1869 int i;
1870
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001871 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001873 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1874 if (rx_queue->rx_skbuff[i]) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00001875 dma_unmap_single(priv->dev, rxbdp->bufPtr,
1876 priv->rx_buffer_size,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001877 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001878 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1879 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001881 rxbdp->lstatus = 0;
1882 rxbdp->bufPtr = 0;
1883 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001885 kfree(rx_queue->rx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001886 rx_queue->rx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001887}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001888
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001889/* If there are any tx skbs or rx skbs still around, free them.
Jan Ceuleers0977f812012-06-05 03:42:12 +00001890 * Then free tx_skbuff and rx_skbuff
1891 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001892static void free_skb_resources(struct gfar_private *priv)
1893{
1894 struct gfar_priv_tx_q *tx_queue = NULL;
1895 struct gfar_priv_rx_q *rx_queue = NULL;
1896 int i;
1897
1898 /* Go through all the buffer descriptors and free their data buffers */
1899 for (i = 0; i < priv->num_tx_queues; i++) {
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001900 struct netdev_queue *txq;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001901
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001902 tx_queue = priv->tx_queue[i];
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001903 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001904 if (tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001905 free_skb_tx_queue(tx_queue);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001906 netdev_tx_reset_queue(txq);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001907 }
1908
1909 for (i = 0; i < priv->num_rx_queues; i++) {
1910 rx_queue = priv->rx_queue[i];
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001911 if (rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001912 free_skb_rx_queue(rx_queue);
1913 }
1914
Claudiu Manoil369ec162013-02-14 05:00:02 +00001915 dma_free_coherent(priv->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001916 sizeof(struct txbd8) * priv->total_tx_ring_size +
1917 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1918 priv->tx_queue[0]->tx_bd_base,
1919 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001922void gfar_start(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001923{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001924 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001925 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001926 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001927
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001928 /* Enable Rx/Tx hw queues */
1929 gfar_write(&regs->rqueue, priv->rqueue);
1930 gfar_write(&regs->tqueue, priv->tqueue);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001931
1932 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001933 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001934 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001935 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001936
Kumar Gala0bbaf062005-06-20 10:54:21 -05001937 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001938 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001939 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001940 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001941
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001942 for (i = 0; i < priv->num_grps; i++) {
1943 regs = priv->gfargrp[i].regs;
1944 /* Clear THLT/RHLT, so that the DMA starts polling now */
1945 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1946 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001947 }
Dai Haruki12dea572008-12-16 15:30:20 -08001948
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001949 /* Enable Rx/Tx DMA */
1950 tempval = gfar_read(&regs->maccfg1);
1951 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1952 gfar_write(&regs->maccfg1, tempval);
1953
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001954 gfar_ints_enable(priv);
1955
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001956 priv->ndev->trans_start = jiffies; /* prevent tx timeout */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001957}
1958
Claudiu Manoil80ec3962014-02-24 12:13:44 +02001959static void free_grp_irqs(struct gfar_priv_grp *grp)
1960{
1961 free_irq(gfar_irq(grp, TX)->irq, grp);
1962 free_irq(gfar_irq(grp, RX)->irq, grp);
1963 free_irq(gfar_irq(grp, ER)->irq, grp);
1964}
1965
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001966static int register_grp_irqs(struct gfar_priv_grp *grp)
1967{
1968 struct gfar_private *priv = grp->priv;
1969 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001970 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /* If the device has multiple interrupts, register for
Jan Ceuleers0977f812012-06-05 03:42:12 +00001973 * them. Otherwise, only register for the one
1974 */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001975 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001976 /* Install our interrupt handlers for Error,
Jan Ceuleers0977f812012-06-05 03:42:12 +00001977 * Transmit, and Receive
1978 */
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001979 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1980 gfar_irq(grp, ER)->name, grp);
1981 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001982 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001983 gfar_irq(grp, ER)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001984
Julia Lawall2145f1a2010-08-05 10:26:20 +00001985 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001987 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1988 gfar_irq(grp, TX)->name, grp);
1989 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001990 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001991 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 goto tx_irq_fail;
1993 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001994 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1995 gfar_irq(grp, RX)->name, grp);
1996 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001997 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001998 gfar_irq(grp, RX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 goto rx_irq_fail;
2000 }
2001 } else {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002002 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
2003 gfar_irq(grp, TX)->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, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 goto err_irq_fail;
2008 }
2009 }
2010
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002011 return 0;
2012
2013rx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002014 free_irq(gfar_irq(grp, TX)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002015tx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00002016 free_irq(gfar_irq(grp, ER)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002017err_irq_fail:
2018 return err;
2019
2020}
2021
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002022static void gfar_free_irq(struct gfar_private *priv)
2023{
2024 int i;
2025
2026 /* Free the IRQs */
2027 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2028 for (i = 0; i < priv->num_grps; i++)
2029 free_grp_irqs(&priv->gfargrp[i]);
2030 } else {
2031 for (i = 0; i < priv->num_grps; i++)
2032 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
2033 &priv->gfargrp[i]);
2034 }
2035}
2036
2037static int gfar_request_irq(struct gfar_private *priv)
2038{
2039 int err, i, j;
2040
2041 for (i = 0; i < priv->num_grps; i++) {
2042 err = register_grp_irqs(&priv->gfargrp[i]);
2043 if (err) {
2044 for (j = 0; j < i; j++)
2045 free_grp_irqs(&priv->gfargrp[j]);
2046 return err;
2047 }
2048 }
2049
2050 return 0;
2051}
2052
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002053/* Bring the controller up and running */
2054int startup_gfar(struct net_device *ndev)
2055{
2056 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002057 int err;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002058
Claudiu Manoila328ac92014-02-24 12:13:42 +02002059 gfar_mac_reset(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002060
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002061 err = gfar_alloc_skb_resources(ndev);
2062 if (err)
2063 return err;
2064
Claudiu Manoila328ac92014-02-24 12:13:42 +02002065 gfar_init_tx_rx_base(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002066
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002067 smp_mb__before_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002068 clear_bit(GFAR_DOWN, &priv->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002069 smp_mb__after_atomic();
Claudiu Manoil08511332014-02-24 12:13:45 +02002070
2071 /* Start Rx/Tx DMA and enable the interrupts */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02002072 gfar_start(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00002074 phy_start(priv->phydev);
2075
Claudiu Manoil08511332014-02-24 12:13:45 +02002076 enable_napi(priv);
2077
2078 netif_tx_wake_all_queues(ndev);
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Jan Ceuleers0977f812012-06-05 03:42:12 +00002083/* Called when something needs to use the ethernet device
2084 * Returns 0 for success.
2085 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086static int gfar_enet_open(struct net_device *dev)
2087{
Li Yang94e8cc32007-10-12 21:53:51 +08002088 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 int err;
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 err = init_phy(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002092 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return err;
2094
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002095 err = gfar_request_irq(priv);
2096 if (err)
2097 return err;
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 err = startup_gfar(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002100 if (err)
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04002101 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08002103 device_set_wakeup_enable(&dev->dev, priv->wol_en);
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return err;
2106}
2107
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002108static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002109{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002110 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07002111
2112 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002113
Kumar Gala0bbaf062005-06-20 10:54:21 -05002114 return fcb;
2115}
2116
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002117static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002118 int fcb_length)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002119{
Kumar Gala0bbaf062005-06-20 10:54:21 -05002120 /* If we're here, it's a IP packet with a TCP or UDP
2121 * payload. We set it to checksum, using a pseudo-header
2122 * we provide
2123 */
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00002124 u8 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002125
Jan Ceuleers0977f812012-06-05 03:42:12 +00002126 /* Tell the controller what the protocol is
2127 * And provide the already calculated phcs
2128 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002129 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002130 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03002131 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002132 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05002133 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002134
2135 /* l3os is the distance between the start of the
2136 * frame (skb->data) and the start of the IP hdr.
2137 * l4os is the distance between the start of the
Jan Ceuleers0977f812012-06-05 03:42:12 +00002138 * l3 hdr and the l4 hdr
2139 */
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002140 fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03002141 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002142
Andy Fleming7f7f5312005-11-11 12:38:59 -06002143 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002144}
2145
Andy Fleming7f7f5312005-11-11 12:38:59 -06002146void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002147{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002148 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002149 fcb->vlctl = vlan_tx_tag_get(skb);
2150}
2151
Dai Haruki4669bc92008-12-17 16:51:04 -08002152static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002153 struct txbd8 *base, int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002154{
2155 struct txbd8 *new_bd = bdp + stride;
2156
2157 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2158}
2159
2160static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002161 int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002162{
2163 return skip_txbd(bdp, 1, base, ring_size);
2164}
2165
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002166/* eTSEC12: csum generation not supported for some fcb offsets */
2167static inline bool gfar_csum_errata_12(struct gfar_private *priv,
2168 unsigned long fcb_addr)
2169{
2170 return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
2171 (fcb_addr % 0x20) > 0x18);
2172}
2173
2174/* eTSEC76: csum generation for frames larger than 2500 may
2175 * cause excess delays before start of transmission
2176 */
2177static inline bool gfar_csum_errata_76(struct gfar_private *priv,
2178 unsigned int len)
2179{
2180 return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
2181 (len > 2500));
2182}
2183
Jan Ceuleers0977f812012-06-05 03:42:12 +00002184/* This is called by the kernel when a frame is ready for transmission.
2185 * It is pointed to by the dev->hard_start_xmit function pointer
2186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2188{
2189 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002190 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002191 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002192 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002193 struct txfcb *fcb = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002194 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
Dai Haruki5a5efed2008-12-16 15:34:50 -08002195 u32 lstatus;
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002196 int i, rq = 0;
2197 int do_tstamp, do_csum, do_vlan;
Dai Haruki4669bc92008-12-17 16:51:04 -08002198 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05002199 unsigned long flags;
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002200 unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002201
2202 rq = skb->queue_mapping;
2203 tx_queue = priv->tx_queue[rq];
2204 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002205 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002206 regs = tx_queue->grp->regs;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002207
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002208 do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2209 do_vlan = vlan_tx_tag_present(skb);
2210 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2211 priv->hwts_tx_en;
2212
2213 if (do_csum || do_vlan)
2214 fcb_len = GMAC_FCB_LEN;
2215
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002216 /* check if time stamp should be generated */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002217 if (unlikely(do_tstamp))
2218 fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Dai Haruki4669bc92008-12-17 16:51:04 -08002219
Li Yang5b28bea2009-03-27 15:54:30 -07002220 /* make space for additional header when fcb is needed */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002221 if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002222 struct sk_buff *skb_new;
2223
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002224 skb_new = skb_realloc_headroom(skb, fcb_len);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002225 if (!skb_new) {
2226 dev->stats.tx_errors++;
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002227 dev_kfree_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002228 return NETDEV_TX_OK;
2229 }
Manfred Rudigierdb83d132012-01-09 23:26:50 +00002230
Eric Dumazet313b0372012-07-05 11:45:13 +00002231 if (skb->sk)
2232 skb_set_owner_w(skb_new, skb->sk);
Eric W. Biedermanc9974ad2014-03-11 14:20:26 -07002233 dev_consume_skb_any(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002234 skb = skb_new;
2235 }
2236
Dai Haruki4669bc92008-12-17 16:51:04 -08002237 /* total number of fragments in the SKB */
2238 nr_frags = skb_shinfo(skb)->nr_frags;
2239
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002240 /* calculate the required number of TxBDs for this skb */
2241 if (unlikely(do_tstamp))
2242 nr_txbds = nr_frags + 2;
2243 else
2244 nr_txbds = nr_frags + 1;
2245
Dai Haruki4669bc92008-12-17 16:51:04 -08002246 /* check if there is space to queue this packet */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002247 if (nr_txbds > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002248 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002249 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08002250 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08002251 return NETDEV_TX_BUSY;
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 /* Update transmit stats */
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002255 bytes_sent = skb->len;
2256 tx_queue->stats.tx_bytes += bytes_sent;
2257 /* keep Tx bytes on wire for BQL accounting */
2258 GFAR_CB(skb)->bytes_sent = bytes_sent;
Eric Dumazet1ac9ad12011-01-12 12:13:14 +00002259 tx_queue->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002261 txbdp = txbdp_start = tx_queue->cur_tx;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002262 lstatus = txbdp->lstatus;
2263
2264 /* Time stamp insertion requires one additional TxBD */
2265 if (unlikely(do_tstamp))
2266 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002267 tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Dai Haruki4669bc92008-12-17 16:51:04 -08002269 if (nr_frags == 0) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002270 if (unlikely(do_tstamp))
2271 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002272 TXBD_INTERRUPT);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002273 else
2274 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
Dai Haruki4669bc92008-12-17 16:51:04 -08002275 } else {
2276 /* Place the fragment addresses and lengths into the TxBDs */
2277 for (i = 0; i < nr_frags; i++) {
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002278 unsigned int frag_len;
Dai Haruki4669bc92008-12-17 16:51:04 -08002279 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002280 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002282 frag_len = skb_shinfo(skb)->frags[i].size;
Dai Haruki4669bc92008-12-17 16:51:04 -08002283
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002284 lstatus = txbdp->lstatus | frag_len |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002285 BD_LFLAG(TXBD_READY);
Dai Haruki4669bc92008-12-17 16:51:04 -08002286
2287 /* Handle the last BD specially */
2288 if (i == nr_frags - 1)
2289 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2290
Claudiu Manoil369ec162013-02-14 05:00:02 +00002291 bufaddr = skb_frag_dma_map(priv->dev,
Ian Campbell2234a722011-08-29 23:18:29 +00002292 &skb_shinfo(skb)->frags[i],
2293 0,
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002294 frag_len,
Ian Campbell2234a722011-08-29 23:18:29 +00002295 DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002296
2297 /* set the TxBD length and buffer pointer */
2298 txbdp->bufPtr = bufaddr;
2299 txbdp->lstatus = lstatus;
2300 }
2301
2302 lstatus = txbdp_start->lstatus;
2303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002305 /* Add TxPAL between FCB and frame if required */
2306 if (unlikely(do_tstamp)) {
2307 skb_push(skb, GMAC_TXPAL_LEN);
2308 memset(skb->data, 0, GMAC_TXPAL_LEN);
2309 }
2310
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002311 /* Add TxFCB if required */
2312 if (fcb_len) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002313 fcb = gfar_add_fcb(skb);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002314 lstatus |= BD_LFLAG(TXBD_TOE);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002315 }
2316
2317 /* Set up checksumming */
2318 if (do_csum) {
2319 gfar_tx_checksum(skb, fcb, fcb_len);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002320
2321 if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
2322 unlikely(gfar_csum_errata_76(priv, skb->len))) {
Alex Dubov4363c2f2011-03-16 17:57:13 +00002323 __skb_pull(skb, GMAC_FCB_LEN);
2324 skb_checksum_help(skb);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002325 if (do_vlan || do_tstamp) {
2326 /* put back a new fcb for vlan/tstamp TOE */
2327 fcb = gfar_add_fcb(skb);
2328 } else {
2329 /* Tx TOE not used */
2330 lstatus &= ~(BD_LFLAG(TXBD_TOE));
2331 fcb = NULL;
2332 }
Alex Dubov4363c2f2011-03-16 17:57:13 +00002333 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002334 }
2335
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002336 if (do_vlan)
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002337 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002338
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002339 /* Setup tx hardware time stamping if requested */
2340 if (unlikely(do_tstamp)) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002341 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002342 fcb->ptp = 1;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002343 }
2344
Claudiu Manoil369ec162013-02-14 05:00:02 +00002345 txbdp_start->bufPtr = dma_map_single(priv->dev, skb->data,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002346 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Jan Ceuleers0977f812012-06-05 03:42:12 +00002348 /* If time stamping is requested one additional TxBD must be set up. The
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002349 * first TxBD points to the FCB and must have a data length of
2350 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2351 * the full frame length.
2352 */
2353 if (unlikely(do_tstamp)) {
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002354 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002355 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002356 (skb_headlen(skb) - fcb_len);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002357 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2358 } else {
2359 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002362 netdev_tx_sent_queue(txq, bytes_sent);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002363
Jan Ceuleers0977f812012-06-05 03:42:12 +00002364 /* We can work in parallel with gfar_clean_tx_ring(), except
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002365 * when modifying num_txbdfree. Note that we didn't grab the lock
2366 * when we were reading the num_txbdfree and checking for available
2367 * space, that's because outside of this function it can only grow,
2368 * and once we've got needed space, it cannot suddenly disappear.
2369 *
2370 * The lock also protects us from gfar_error(), which can modify
2371 * regs->tstat and thus retrigger the transfers, which is why we
2372 * also must grab the lock before setting ready bit for the first
2373 * to be transmitted BD.
2374 */
2375 spin_lock_irqsave(&tx_queue->txlock, flags);
2376
Claudiu Manoild55398b2014-10-07 10:44:35 +03002377 gfar_wmb();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002378
Dai Haruki4669bc92008-12-17 16:51:04 -08002379 txbdp_start->lstatus = lstatus;
2380
Claudiu Manoild55398b2014-10-07 10:44:35 +03002381 gfar_wmb(); /* force lstatus write before tx_skbuff */
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002382
2383 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2384
Dai Haruki4669bc92008-12-17 16:51:04 -08002385 /* Update the current skb pointer to the next entry we will use
Jan Ceuleers0977f812012-06-05 03:42:12 +00002386 * (wrapping if necessary)
2387 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002388 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002389 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002390
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002391 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002392
2393 /* reduce TxBD free count */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002394 tx_queue->num_txbdfree -= (nr_txbds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 /* If the next BD still needs to be cleaned up, then the bds
Jan Ceuleers0977f812012-06-05 03:42:12 +00002397 * are full. We need to tell the kernel to stop sending us stuff.
2398 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002399 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002400 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002402 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002406 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002409 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002411 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
2414/* Stops the kernel queue, and halts the controller */
2415static int gfar_close(struct net_device *dev)
2416{
2417 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002418
Sebastian Siewiorab939902008-08-19 21:12:45 +02002419 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 stop_gfar(dev);
2421
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002422 /* Disconnect from the PHY */
2423 phy_disconnect(priv->phydev);
2424 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002426 gfar_free_irq(priv);
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 0;
2429}
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002432static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002434 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 return 0;
2437}
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002442 int frame_size = new_mtu + ETH_HLEN;
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Joe Perches59deab22011-06-14 08:57:47 +00002445 netif_err(priv, drv, dev, "Invalid MTU setting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return -EINVAL;
2447 }
2448
Claudiu Manoil08511332014-02-24 12:13:45 +02002449 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2450 cpu_relax();
2451
Claudiu Manoil88302642014-02-24 12:13:43 +02002452 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 stop_gfar(dev);
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 dev->mtu = new_mtu;
2456
Claudiu Manoil88302642014-02-24 12:13:43 +02002457 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 startup_gfar(dev);
2459
Claudiu Manoil08511332014-02-24 12:13:45 +02002460 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 0;
2463}
2464
Claudiu Manoil08511332014-02-24 12:13:45 +02002465void reset_gfar(struct net_device *ndev)
2466{
2467 struct gfar_private *priv = netdev_priv(ndev);
2468
2469 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2470 cpu_relax();
2471
2472 stop_gfar(ndev);
2473 startup_gfar(ndev);
2474
2475 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2476}
2477
Sebastian Siewiorab939902008-08-19 21:12:45 +02002478/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 * transmitted after a set amount of time.
2480 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002481 * starting over will fix the problem.
2482 */
2483static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002485 struct gfar_private *priv = container_of(work, struct gfar_private,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002486 reset_task);
Claudiu Manoil08511332014-02-24 12:13:45 +02002487 reset_gfar(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489
Sebastian Siewiorab939902008-08-19 21:12:45 +02002490static void gfar_timeout(struct net_device *dev)
2491{
2492 struct gfar_private *priv = netdev_priv(dev);
2493
2494 dev->stats.tx_errors++;
2495 schedule_work(&priv->reset_task);
2496}
2497
Eran Libertyacbc0f02010-07-07 15:54:54 -07002498static void gfar_align_skb(struct sk_buff *skb)
2499{
2500 /* We need the data buffer to be aligned properly. We will reserve
2501 * as many bytes as needed to align the data properly
2502 */
2503 skb_reserve(skb, RXBUF_ALIGNMENT -
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002504 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
Eran Libertyacbc0f02010-07-07 15:54:54 -07002505}
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507/* Interrupt Handler for Transmit complete */
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002508static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002510 struct net_device *dev = tx_queue->dev;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002511 struct netdev_queue *txq;
Dai Harukid080cd62008-04-09 19:37:51 -05002512 struct gfar_private *priv = netdev_priv(dev);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002513 struct txbd8 *bdp, *next = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002514 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002515 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002516 struct sk_buff *skb;
2517 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002518 int tx_ring_size = tx_queue->tx_ring_size;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002519 int frags = 0, nr_txbds = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002520 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002521 int howmany = 0;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002522 int tqi = tx_queue->qindex;
2523 unsigned int bytes_sent = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002524 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002525 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002527 txq = netdev_get_tx_queue(dev, tqi);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002528 bdp = tx_queue->dirty_tx;
2529 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002530
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002531 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002532 unsigned long flags;
2533
Dai Haruki4669bc92008-12-17 16:51:04 -08002534 frags = skb_shinfo(skb)->nr_frags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002535
Jan Ceuleers0977f812012-06-05 03:42:12 +00002536 /* When time stamping, one additional TxBD must be freed.
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002537 * Also, we need to dma_unmap_single() the TxPAL.
2538 */
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002539 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002540 nr_txbds = frags + 2;
2541 else
2542 nr_txbds = frags + 1;
2543
2544 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002545
2546 lstatus = lbdp->lstatus;
2547
2548 /* Only clean completed frames */
2549 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002550 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 break;
2552
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002553 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002554 next = next_txbd(bdp, base, tx_ring_size);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002555 buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002556 } else
2557 buflen = bdp->length;
2558
Claudiu Manoil369ec162013-02-14 05:00:02 +00002559 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002560 buflen, DMA_TO_DEVICE);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002561
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002562 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002563 struct skb_shared_hwtstamps shhwtstamps;
2564 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002565
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002566 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2567 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002568 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002569 skb_tstamp_tx(skb, &shhwtstamps);
2570 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2571 bdp = next;
2572 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002573
2574 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2575 bdp = next_txbd(bdp, base, tx_ring_size);
2576
2577 for (i = 0; i < frags; i++) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00002578 dma_unmap_page(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002579 bdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002580 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2581 bdp = next_txbd(bdp, base, tx_ring_size);
2582 }
2583
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002584 bytes_sent += GFAR_CB(skb)->bytes_sent;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002585
Eric Dumazetacb600d2012-10-05 06:23:55 +00002586 dev_kfree_skb_any(skb);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002587
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002588 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002589
2590 skb_dirtytx = (skb_dirtytx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002591 TX_RING_MOD_MASK(tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002592
Dai Harukid080cd62008-04-09 19:37:51 -05002593 howmany++;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002594 spin_lock_irqsave(&tx_queue->txlock, flags);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002595 tx_queue->num_txbdfree += nr_txbds;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002596 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08002597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Dai Haruki4669bc92008-12-17 16:51:04 -08002599 /* If we freed a buffer, we can restart transmission, if necessary */
Claudiu Manoil08511332014-02-24 12:13:45 +02002600 if (tx_queue->num_txbdfree &&
2601 netif_tx_queue_stopped(txq) &&
2602 !(test_bit(GFAR_DOWN, &priv->state)))
2603 netif_wake_subqueue(priv->ndev, tqi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Dai Haruki4669bc92008-12-17 16:51:04 -08002605 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002606 tx_queue->skb_dirtytx = skb_dirtytx;
2607 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002609 netdev_tx_completed_queue(txq, howmany, bytes_sent);
Dai Harukid080cd62008-04-09 19:37:51 -05002610}
2611
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002612static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002613 struct sk_buff *skb)
Andy Fleming815b97c2008-04-22 17:18:29 -05002614{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002615 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002616 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002617 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002618
Claudiu Manoil369ec162013-02-14 05:00:02 +00002619 buf = dma_map_single(priv->dev, skb->data,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002620 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002621 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002622}
2623
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002624static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
Eran Libertyacbc0f02010-07-07 15:54:54 -07002625{
2626 struct gfar_private *priv = netdev_priv(dev);
Eric Dumazetacb600d2012-10-05 06:23:55 +00002627 struct sk_buff *skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002628
2629 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2630 if (!skb)
2631 return NULL;
2632
2633 gfar_align_skb(skb);
2634
2635 return skb;
2636}
Andy Fleming815b97c2008-04-22 17:18:29 -05002637
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002638struct sk_buff *gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Eric Dumazetacb600d2012-10-05 06:23:55 +00002640 return gfar_alloc_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Li Yang298e1a92007-10-16 14:18:13 +08002643static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Li Yang298e1a92007-10-16 14:18:13 +08002645 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002646 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 struct gfar_extra_stats *estats = &priv->extra_stats;
2648
Jan Ceuleers0977f812012-06-05 03:42:12 +00002649 /* If the packet was truncated, none of the other errors matter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (status & RXBD_TRUNCATED) {
2651 stats->rx_length_errors++;
2652
Paul Gortmaker212079d2013-02-12 15:38:19 -05002653 atomic64_inc(&estats->rx_trunc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 return;
2656 }
2657 /* Count the errors, if there were any */
2658 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2659 stats->rx_length_errors++;
2660
2661 if (status & RXBD_LARGE)
Paul Gortmaker212079d2013-02-12 15:38:19 -05002662 atomic64_inc(&estats->rx_large);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 else
Paul Gortmaker212079d2013-02-12 15:38:19 -05002664 atomic64_inc(&estats->rx_short);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666 if (status & RXBD_NONOCTET) {
2667 stats->rx_frame_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002668 atomic64_inc(&estats->rx_nonoctet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670 if (status & RXBD_CRCERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002671 atomic64_inc(&estats->rx_crcerr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 stats->rx_crc_errors++;
2673 }
2674 if (status & RXBD_OVERRUN) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002675 atomic64_inc(&estats->rx_overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 stats->rx_crc_errors++;
2677 }
2678}
2679
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002680irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002682 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2683 unsigned long flags;
2684 u32 imask;
2685
2686 if (likely(napi_schedule_prep(&grp->napi_rx))) {
2687 spin_lock_irqsave(&grp->grplock, flags);
2688 imask = gfar_read(&grp->regs->imask);
2689 imask &= IMASK_RX_DISABLED;
2690 gfar_write(&grp->regs->imask, imask);
2691 spin_unlock_irqrestore(&grp->grplock, flags);
2692 __napi_schedule(&grp->napi_rx);
2693 } else {
2694 /* Clear IEVENT, so interrupts aren't called again
2695 * because of the packets that have already arrived.
2696 */
2697 gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2698 }
2699
2700 return IRQ_HANDLED;
2701}
2702
2703/* Interrupt Handler for Transmit complete */
2704static irqreturn_t gfar_transmit(int irq, void *grp_id)
2705{
2706 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2707 unsigned long flags;
2708 u32 imask;
2709
2710 if (likely(napi_schedule_prep(&grp->napi_tx))) {
2711 spin_lock_irqsave(&grp->grplock, flags);
2712 imask = gfar_read(&grp->regs->imask);
2713 imask &= IMASK_TX_DISABLED;
2714 gfar_write(&grp->regs->imask, imask);
2715 spin_unlock_irqrestore(&grp->grplock, flags);
2716 __napi_schedule(&grp->napi_tx);
2717 } else {
2718 /* Clear IEVENT, so interrupts aren't called again
2719 * because of the packets that have already arrived.
2720 */
2721 gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2722 }
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return IRQ_HANDLED;
2725}
2726
Kumar Gala0bbaf062005-06-20 10:54:21 -05002727static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2728{
2729 /* If valid headers were found, and valid sums
2730 * were verified, then we tell the kernel that no
Jan Ceuleers0977f812012-06-05 03:42:12 +00002731 * checksumming is necessary. Otherwise, it is [FIXME]
2732 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002733 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002734 skb->ip_summed = CHECKSUM_UNNECESSARY;
2735 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002736 skb_checksum_none_assert(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002737}
2738
2739
Jan Ceuleers0977f812012-06-05 03:42:12 +00002740/* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
Claudiu Manoil61db26c2013-02-14 05:00:05 +00002741static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2742 int amount_pull, struct napi_struct *napi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002745 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Dai Haruki2c2db482008-12-16 15:31:15 -08002747 /* fcb is at the beginning if exists */
2748 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Jan Ceuleers0977f812012-06-05 03:42:12 +00002750 /* Remove the FCB from the skb
2751 * Remove the padded bytes, if there are any
2752 */
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002753 if (amount_pull) {
2754 skb_record_rx_queue(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002755 skb_pull(skb, amount_pull);
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002756 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002757
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002758 /* Get receive timestamp from the skb */
2759 if (priv->hwts_rx_en) {
2760 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2761 u64 *ns = (u64 *) skb->data;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002762
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002763 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2764 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2765 }
2766
2767 if (priv->padding)
2768 skb_pull(skb, priv->padding);
2769
Michał Mirosław8b3afe92011-04-15 04:50:50 +00002770 if (dev->features & NETIF_F_RXCSUM)
Dai Haruki2c2db482008-12-16 15:31:15 -08002771 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002772
Dai Haruki2c2db482008-12-16 15:31:15 -08002773 /* Tell the skb what kind of packet this is */
2774 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002775
Patrick McHardyf6469682013-04-19 02:04:27 +00002776 /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
David S. Miller823dcd22011-08-20 10:39:12 -07002777 * Even if vlan rx accel is disabled, on some chips
2778 * RXFCB_VLN is pseudo randomly set.
2779 */
Patrick McHardyf6469682013-04-19 02:04:27 +00002780 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
David S. Miller823dcd22011-08-20 10:39:12 -07002781 fcb->flags & RXFCB_VLN)
David S. Millere5905c82013-04-22 19:24:19 -04002782 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
Jiri Pirko87c288c2011-07-20 04:54:19 +00002783
Dai Haruki2c2db482008-12-16 15:31:15 -08002784 /* Send the packet up the stack */
Claudiu Manoil953d2762013-03-21 03:12:15 +00002785 napi_gro_receive(napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002790 * until the budget/quota has been reached. Returns the number
2791 * of frames handled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002793int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002795 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002796 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002798 int pkt_len;
2799 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 int howmany = 0;
2801 struct gfar_private *priv = netdev_priv(dev);
2802
2803 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002804 bdp = rx_queue->cur_rx;
2805 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Claudiu Manoilba779712013-02-14 05:00:07 +00002807 amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
Dai Haruki2c2db482008-12-16 15:31:15 -08002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002810 struct sk_buff *newskb;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002811
Scott Wood3b6330c2007-05-16 15:06:59 -05002812 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002813
2814 /* Add another skb for the future */
2815 newskb = gfar_new_skb(dev);
2816
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002817 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Claudiu Manoil369ec162013-02-14 05:00:02 +00002819 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002820 priv->rx_buffer_size, DMA_FROM_DEVICE);
Andy Fleming81183052008-11-12 10:07:11 -06002821
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002822 if (unlikely(!(bdp->status & RXBD_ERR) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002823 bdp->length > priv->rx_buffer_size))
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002824 bdp->status = RXBD_LARGE;
2825
Andy Fleming815b97c2008-04-22 17:18:29 -05002826 /* We drop the frame if we failed to allocate a new buffer */
2827 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002828 bdp->status & RXBD_ERR)) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002829 count_errors(bdp->status, dev);
2830
2831 if (unlikely(!newskb))
2832 newskb = skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002833 else if (skb)
Eric Dumazetacb600d2012-10-05 06:23:55 +00002834 dev_kfree_skb(skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05002835 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002837 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 howmany++;
2839
Dai Haruki2c2db482008-12-16 15:31:15 -08002840 if (likely(skb)) {
2841 pkt_len = bdp->length - ETH_FCS_LEN;
2842 /* Remove the FCS from the packet length */
2843 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002844 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002845 skb_record_rx_queue(skb, rx_queue->qindex);
Wu Jiajun-B06378cd754a52012-04-19 22:54:35 +00002846 gfar_process_frame(dev, skb, amount_pull,
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002847 &rx_queue->grp->napi_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Dai Haruki2c2db482008-12-16 15:31:15 -08002849 } else {
Joe Perches59deab22011-06-14 08:57:47 +00002850 netif_warn(priv, rx_err, dev, "Missing skb!\n");
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002851 rx_queue->stats.rx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002852 atomic64_inc(&priv->extra_stats.rx_skbmissing);
Dai Haruki2c2db482008-12-16 15:31:15 -08002853 }
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002857 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Andy Fleming815b97c2008-04-22 17:18:29 -05002859 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002860 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002863 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 /* update to point at the next skb */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002866 rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2867 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
2869
2870 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002871 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return howmany;
2874}
2875
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002876static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002877{
2878 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002879 container_of(napi, struct gfar_priv_grp, napi_rx);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002880 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002881 struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002882 int work_done = 0;
2883
2884 /* Clear IEVENT, so interrupts aren't called again
2885 * because of the packets that have already arrived
2886 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002887 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002888
2889 work_done = gfar_clean_rx_ring(rx_queue, budget);
2890
2891 if (work_done < budget) {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002892 u32 imask;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002893 napi_complete(napi);
2894 /* Clear the halt bit in RSTAT */
2895 gfar_write(&regs->rstat, gfargrp->rstat);
2896
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002897 spin_lock_irq(&gfargrp->grplock);
2898 imask = gfar_read(&regs->imask);
2899 imask |= IMASK_RX_DEFAULT;
2900 gfar_write(&regs->imask, imask);
2901 spin_unlock_irq(&gfargrp->grplock);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002902 }
2903
2904 return work_done;
2905}
2906
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002907static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002909 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002910 container_of(napi, struct gfar_priv_grp, napi_tx);
2911 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002912 struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002913 u32 imask;
2914
2915 /* Clear IEVENT, so interrupts aren't called again
2916 * because of the packets that have already arrived
2917 */
2918 gfar_write(&regs->ievent, IEVENT_TX_MASK);
2919
2920 /* run Tx cleanup to completion */
2921 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
2922 gfar_clean_tx_ring(tx_queue);
2923
2924 napi_complete(napi);
2925
2926 spin_lock_irq(&gfargrp->grplock);
2927 imask = gfar_read(&regs->imask);
2928 imask |= IMASK_TX_DEFAULT;
2929 gfar_write(&regs->imask, imask);
2930 spin_unlock_irq(&gfargrp->grplock);
2931
2932 return 0;
2933}
2934
2935static int gfar_poll_rx(struct napi_struct *napi, int budget)
2936{
2937 struct gfar_priv_grp *gfargrp =
2938 container_of(napi, struct gfar_priv_grp, napi_rx);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002939 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002940 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002941 struct gfar_priv_rx_q *rx_queue = NULL;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002942 int work_done = 0, work_done_per_q = 0;
Claudiu Manoil39c0a0d2013-03-21 03:12:13 +00002943 int i, budget_per_q = 0;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002944 unsigned long rstat_rxf;
2945 int num_act_queues;
Dai Harukid080cd62008-04-09 19:37:51 -05002946
Dai Haruki8c7396a2008-12-17 16:52:00 -08002947 /* Clear IEVENT, so interrupts aren't called again
Jan Ceuleers0977f812012-06-05 03:42:12 +00002948 * because of the packets that have already arrived
2949 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002950 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002951
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002952 rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
2953
2954 num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
2955 if (num_act_queues)
2956 budget_per_q = budget/num_act_queues;
2957
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002958 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2959 /* skip queue if not active */
2960 if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
2961 continue;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002962
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002963 rx_queue = priv->rx_queue[i];
2964 work_done_per_q =
2965 gfar_clean_rx_ring(rx_queue, budget_per_q);
2966 work_done += work_done_per_q;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002967
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002968 /* finished processing this queue */
2969 if (work_done_per_q < budget_per_q) {
2970 /* clear active queue hw indication */
2971 gfar_write(&regs->rstat,
2972 RSTAT_CLEAR_RXF0 >> i);
2973 num_act_queues--;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002974
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002975 if (!num_act_queues)
2976 break;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002977 }
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002978 }
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002979
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002980 if (!num_act_queues) {
2981 u32 imask;
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002982 napi_complete(napi);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002983
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002984 /* Clear the halt bit in RSTAT */
2985 gfar_write(&regs->rstat, gfargrp->rstat);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002986
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002987 spin_lock_irq(&gfargrp->grplock);
2988 imask = gfar_read(&regs->imask);
2989 imask |= IMASK_RX_DEFAULT;
2990 gfar_write(&regs->imask, imask);
2991 spin_unlock_irq(&gfargrp->grplock);
Dai Harukid080cd62008-04-09 19:37:51 -05002992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002994 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002997static int gfar_poll_tx(struct napi_struct *napi, int budget)
2998{
2999 struct gfar_priv_grp *gfargrp =
3000 container_of(napi, struct gfar_priv_grp, napi_tx);
3001 struct gfar_private *priv = gfargrp->priv;
3002 struct gfar __iomem *regs = gfargrp->regs;
3003 struct gfar_priv_tx_q *tx_queue = NULL;
3004 int has_tx_work = 0;
3005 int i;
3006
3007 /* Clear IEVENT, so interrupts aren't called again
3008 * because of the packets that have already arrived
3009 */
3010 gfar_write(&regs->ievent, IEVENT_TX_MASK);
3011
3012 for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
3013 tx_queue = priv->tx_queue[i];
3014 /* run Tx cleanup to completion */
3015 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
3016 gfar_clean_tx_ring(tx_queue);
3017 has_tx_work = 1;
3018 }
3019 }
3020
3021 if (!has_tx_work) {
3022 u32 imask;
3023 napi_complete(napi);
3024
3025 spin_lock_irq(&gfargrp->grplock);
3026 imask = gfar_read(&regs->imask);
3027 imask |= IMASK_TX_DEFAULT;
3028 gfar_write(&regs->imask, imask);
3029 spin_unlock_irq(&gfargrp->grplock);
3030 }
3031
3032 return 0;
3033}
3034
3035
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003036#ifdef CONFIG_NET_POLL_CONTROLLER
Jan Ceuleers0977f812012-06-05 03:42:12 +00003037/* Polling 'interrupt' - used by things like netconsole to send skbs
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003038 * without having to re-enable interrupts. It's not called while
3039 * the interrupt routine is executing.
3040 */
3041static void gfar_netpoll(struct net_device *dev)
3042{
3043 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00003044 int i;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003045
3046 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003047 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003048 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003049 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3050
3051 disable_irq(gfar_irq(grp, TX)->irq);
3052 disable_irq(gfar_irq(grp, RX)->irq);
3053 disable_irq(gfar_irq(grp, ER)->irq);
3054 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3055 enable_irq(gfar_irq(grp, ER)->irq);
3056 enable_irq(gfar_irq(grp, RX)->irq);
3057 enable_irq(gfar_irq(grp, TX)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003058 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003059 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003060 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003061 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3062
3063 disable_irq(gfar_irq(grp, TX)->irq);
3064 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3065 enable_irq(gfar_irq(grp, TX)->irq);
Anton Vorontsov43de0042009-12-09 02:52:19 -08003066 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003067 }
3068}
3069#endif
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003072static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003074 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003077 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003080 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003081 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
3083 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003084 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003085 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003087 /* Check for errors */
3088 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003089 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 return IRQ_HANDLED;
3092}
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094/* Called every time the controller might need to be made
3095 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003096 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 * function converts those variables into the appropriate
3098 * register values, and can bring down the device if needed.
3099 */
3100static void adjust_link(struct net_device *dev)
3101{
3102 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003103 struct phy_device *phydev = priv->phydev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003105 if (unlikely(phydev->link != priv->oldlink ||
3106 phydev->duplex != priv->oldduplex ||
3107 phydev->speed != priv->oldspeed))
3108 gfar_update_link_state(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111/* Update the hash table based on the current list of multicast
3112 * addresses we subscribe to. Also, change the promiscuity of
3113 * the device based on the flags (this function is called
Jan Ceuleers0977f812012-06-05 03:42:12 +00003114 * whenever dev->flags is changed
3115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116static void gfar_set_multi(struct net_device *dev)
3117{
Jiri Pirko22bedad32010-04-01 21:22:57 +00003118 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003120 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 u32 tempval;
3122
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003123 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 /* Set RCTRL to PROM */
3125 tempval = gfar_read(&regs->rctrl);
3126 tempval |= RCTRL_PROM;
3127 gfar_write(&regs->rctrl, tempval);
3128 } else {
3129 /* Set RCTRL to not PROM */
3130 tempval = gfar_read(&regs->rctrl);
3131 tempval &= ~(RCTRL_PROM);
3132 gfar_write(&regs->rctrl, tempval);
3133 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003134
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003135 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003137 gfar_write(&regs->igaddr0, 0xffffffff);
3138 gfar_write(&regs->igaddr1, 0xffffffff);
3139 gfar_write(&regs->igaddr2, 0xffffffff);
3140 gfar_write(&regs->igaddr3, 0xffffffff);
3141 gfar_write(&regs->igaddr4, 0xffffffff);
3142 gfar_write(&regs->igaddr5, 0xffffffff);
3143 gfar_write(&regs->igaddr6, 0xffffffff);
3144 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 gfar_write(&regs->gaddr0, 0xffffffff);
3146 gfar_write(&regs->gaddr1, 0xffffffff);
3147 gfar_write(&regs->gaddr2, 0xffffffff);
3148 gfar_write(&regs->gaddr3, 0xffffffff);
3149 gfar_write(&regs->gaddr4, 0xffffffff);
3150 gfar_write(&regs->gaddr5, 0xffffffff);
3151 gfar_write(&regs->gaddr6, 0xffffffff);
3152 gfar_write(&regs->gaddr7, 0xffffffff);
3153 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003154 int em_num;
3155 int idx;
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003158 gfar_write(&regs->igaddr0, 0x0);
3159 gfar_write(&regs->igaddr1, 0x0);
3160 gfar_write(&regs->igaddr2, 0x0);
3161 gfar_write(&regs->igaddr3, 0x0);
3162 gfar_write(&regs->igaddr4, 0x0);
3163 gfar_write(&regs->igaddr5, 0x0);
3164 gfar_write(&regs->igaddr6, 0x0);
3165 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 gfar_write(&regs->gaddr0, 0x0);
3167 gfar_write(&regs->gaddr1, 0x0);
3168 gfar_write(&regs->gaddr2, 0x0);
3169 gfar_write(&regs->gaddr3, 0x0);
3170 gfar_write(&regs->gaddr4, 0x0);
3171 gfar_write(&regs->gaddr5, 0x0);
3172 gfar_write(&regs->gaddr6, 0x0);
3173 gfar_write(&regs->gaddr7, 0x0);
3174
Andy Fleming7f7f5312005-11-11 12:38:59 -06003175 /* If we have extended hash tables, we need to
3176 * clear the exact match registers to prepare for
Jan Ceuleers0977f812012-06-05 03:42:12 +00003177 * setting them
3178 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003179 if (priv->extended_hash) {
3180 em_num = GFAR_EM_NUM + 1;
3181 gfar_clear_exact_match(dev);
3182 idx = 1;
3183 } else {
3184 idx = 0;
3185 em_num = 0;
3186 }
3187
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003188 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return;
3190
3191 /* Parse the list, and set the appropriate bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003192 netdev_for_each_mc_addr(ha, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003193 if (idx < em_num) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00003194 gfar_set_mac_for_addr(dev, idx, ha->addr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003195 idx++;
3196 } else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003197 gfar_set_hash_for_addr(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
3199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
Andy Fleming7f7f5312005-11-11 12:38:59 -06003202
3203/* Clears each of the exact match registers to zero, so they
Jan Ceuleers0977f812012-06-05 03:42:12 +00003204 * don't interfere with normal reception
3205 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003206static void gfar_clear_exact_match(struct net_device *dev)
3207{
3208 int idx;
Joe Perches6a3c9102011-11-16 09:38:02 +00003209 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
Andy Fleming7f7f5312005-11-11 12:38:59 -06003210
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003211 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
Joe Perchesb6bc7652010-12-21 02:16:08 -08003212 gfar_set_mac_for_addr(dev, idx, zero_arr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003213}
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215/* Set the appropriate hash bit for the given addr */
3216/* The algorithm works like so:
3217 * 1) Take the Destination Address (ie the multicast address), and
3218 * do a CRC on it (little endian), and reverse the bits of the
3219 * result.
3220 * 2) Use the 8 most significant bits as a hash into a 256-entry
3221 * table. The table is controlled through 8 32-bit registers:
3222 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3223 * gaddr7. This means that the 3 most significant bits in the
3224 * hash index which gaddr register to use, and the 5 other bits
3225 * indicate which bit (assuming an IBM numbering scheme, which
3226 * for PowerPC (tm) is usually the case) in the register holds
Jan Ceuleers0977f812012-06-05 03:42:12 +00003227 * the entry.
3228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3230{
3231 u32 tempval;
3232 struct gfar_private *priv = netdev_priv(dev);
Joe Perches6a3c9102011-11-16 09:38:02 +00003233 u32 result = ether_crc(ETH_ALEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05003234 int width = priv->hash_width;
3235 u8 whichbit = (result >> (32 - width)) & 0x1f;
3236 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 u32 value = (1 << (31-whichbit));
3238
Kumar Gala0bbaf062005-06-20 10:54:21 -05003239 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003241 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Andy Fleming7f7f5312005-11-11 12:38:59 -06003244
3245/* There are multiple MAC Address register pairs on some controllers
3246 * This function sets the numth pair to a given address
3247 */
Joe Perchesb6bc7652010-12-21 02:16:08 -08003248static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3249 const u8 *addr)
Andy Fleming7f7f5312005-11-11 12:38:59 -06003250{
3251 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003252 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003253 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003254 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003255
3256 macptr += num*2;
3257
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003258 /* For a station address of 0x12345678ABCD in transmission
3259 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
3260 * MACnADDR2 is set to 0x34120000.
Jan Ceuleers0977f812012-06-05 03:42:12 +00003261 */
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003262 tempval = (addr[5] << 24) | (addr[4] << 16) |
3263 (addr[3] << 8) | addr[2];
Andy Fleming7f7f5312005-11-11 12:38:59 -06003264
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003265 gfar_write(macptr, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003266
Claudiu Manoil83bfc3c2014-10-07 10:44:33 +03003267 tempval = (addr[1] << 24) | (addr[0] << 16);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003268
3269 gfar_write(macptr+1, tempval);
3270}
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003273static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003275 struct gfar_priv_grp *gfargrp = grp_id;
3276 struct gfar __iomem *regs = gfargrp->regs;
3277 struct gfar_private *priv= gfargrp->priv;
3278 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003281 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003284 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05003285
3286 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003287 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05003288 (events & IEVENT_MAG))
3289 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003292 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003293 netdev_dbg(dev,
3294 "error interrupt (ievent=0x%08x imask=0x%08x)\n",
Joe Perches59deab22011-06-14 08:57:47 +00003295 events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 /* Update the error counters */
3298 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003299 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
3301 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003302 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003304 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (events & IEVENT_XFUN) {
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003306 unsigned long flags;
3307
Joe Perches59deab22011-06-14 08:57:47 +00003308 netif_dbg(priv, tx_err, dev,
3309 "TX FIFO underrun, packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003310 dev->stats.tx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003311 atomic64_inc(&priv->extra_stats.tx_underrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003313 local_irq_save(flags);
3314 lock_tx_qs(priv);
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003317 gfar_write(&regs->tstat, gfargrp->tstat);
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003318
3319 unlock_tx_qs(priv);
3320 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
Joe Perches59deab22011-06-14 08:57:47 +00003322 netif_dbg(priv, tx_err, dev, "Transmit Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
3324 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003325 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003326 atomic64_inc(&priv->extra_stats.rx_bsy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003328 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Joe Perches59deab22011-06-14 08:57:47 +00003330 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3331 gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
3333 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003334 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003335 atomic64_inc(&priv->extra_stats.rx_babr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Joe Perches59deab22011-06-14 08:57:47 +00003337 netif_dbg(priv, rx_err, dev, "babbling RX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 }
3339 if (events & IEVENT_EBERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003340 atomic64_inc(&priv->extra_stats.eberr);
Joe Perches59deab22011-06-14 08:57:47 +00003341 netif_dbg(priv, rx_err, dev, "bus error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
Joe Perches59deab22011-06-14 08:57:47 +00003343 if (events & IEVENT_RXC)
3344 netif_dbg(priv, rx_status, dev, "control frame\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346 if (events & IEVENT_BABT) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003347 atomic64_inc(&priv->extra_stats.tx_babt);
Joe Perches59deab22011-06-14 08:57:47 +00003348 netif_dbg(priv, tx_err, dev, "babbling TX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
3350 return IRQ_HANDLED;
3351}
3352
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003353static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
3354{
3355 struct phy_device *phydev = priv->phydev;
3356 u32 val = 0;
3357
3358 if (!phydev->duplex)
3359 return val;
3360
3361 if (!priv->pause_aneg_en) {
3362 if (priv->tx_pause_en)
3363 val |= MACCFG1_TX_FLOW;
3364 if (priv->rx_pause_en)
3365 val |= MACCFG1_RX_FLOW;
3366 } else {
3367 u16 lcl_adv, rmt_adv;
3368 u8 flowctrl;
3369 /* get link partner capabilities */
3370 rmt_adv = 0;
3371 if (phydev->pause)
3372 rmt_adv = LPA_PAUSE_CAP;
3373 if (phydev->asym_pause)
3374 rmt_adv |= LPA_PAUSE_ASYM;
3375
Pavaluca Matei-B4661043ef8d22014-10-27 10:42:43 +02003376 lcl_adv = 0;
3377 if (phydev->advertising & ADVERTISED_Pause)
3378 lcl_adv |= ADVERTISE_PAUSE_CAP;
3379 if (phydev->advertising & ADVERTISED_Asym_Pause)
3380 lcl_adv |= ADVERTISE_PAUSE_ASYM;
Claudiu Manoil6ce29b02014-04-30 14:27:21 +03003381
3382 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
3383 if (flowctrl & FLOW_CTRL_TX)
3384 val |= MACCFG1_TX_FLOW;
3385 if (flowctrl & FLOW_CTRL_RX)
3386 val |= MACCFG1_RX_FLOW;
3387 }
3388
3389 return val;
3390}
3391
3392static noinline void gfar_update_link_state(struct gfar_private *priv)
3393{
3394 struct gfar __iomem *regs = priv->gfargrp[0].regs;
3395 struct phy_device *phydev = priv->phydev;
3396
3397 if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
3398 return;
3399
3400 if (phydev->link) {
3401 u32 tempval1 = gfar_read(&regs->maccfg1);
3402 u32 tempval = gfar_read(&regs->maccfg2);
3403 u32 ecntrl = gfar_read(&regs->ecntrl);
3404
3405 if (phydev->duplex != priv->oldduplex) {
3406 if (!(phydev->duplex))
3407 tempval &= ~(MACCFG2_FULL_DUPLEX);
3408 else
3409 tempval |= MACCFG2_FULL_DUPLEX;
3410
3411 priv->oldduplex = phydev->duplex;
3412 }
3413
3414 if (phydev->speed != priv->oldspeed) {
3415 switch (phydev->speed) {
3416 case 1000:
3417 tempval =
3418 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
3419
3420 ecntrl &= ~(ECNTRL_R100);
3421 break;
3422 case 100:
3423 case 10:
3424 tempval =
3425 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
3426
3427 /* Reduced mode distinguishes
3428 * between 10 and 100
3429 */
3430 if (phydev->speed == SPEED_100)
3431 ecntrl |= ECNTRL_R100;
3432 else
3433 ecntrl &= ~(ECNTRL_R100);
3434 break;
3435 default:
3436 netif_warn(priv, link, priv->ndev,
3437 "Ack! Speed (%d) is not 10/100/1000!\n",
3438 phydev->speed);
3439 break;
3440 }
3441
3442 priv->oldspeed = phydev->speed;
3443 }
3444
3445 tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
3446 tempval1 |= gfar_get_flowctrl_cfg(priv);
3447
3448 gfar_write(&regs->maccfg1, tempval1);
3449 gfar_write(&regs->maccfg2, tempval);
3450 gfar_write(&regs->ecntrl, ecntrl);
3451
3452 if (!priv->oldlink)
3453 priv->oldlink = 1;
3454
3455 } else if (priv->oldlink) {
3456 priv->oldlink = 0;
3457 priv->oldspeed = 0;
3458 priv->oldduplex = -1;
3459 }
3460
3461 if (netif_msg_link(priv))
3462 phy_print_status(phydev);
3463}
3464
Andy Flemingb31a1d82008-12-16 15:29:15 -08003465static struct of_device_id gfar_match[] =
3466{
3467 {
3468 .type = "network",
3469 .compatible = "gianfar",
3470 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003471 {
3472 .compatible = "fsl,etsec2",
3473 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003474 {},
3475};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003476MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478/* Structure for a device driver */
Grant Likely74888762011-02-22 21:05:51 -07003479static struct platform_driver gfar_driver = {
Grant Likely40182942010-04-13 16:13:02 -07003480 .driver = {
3481 .name = "fsl-gianfar",
3482 .owner = THIS_MODULE,
3483 .pm = GFAR_PM_OPS,
3484 .of_match_table = gfar_match,
3485 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 .probe = gfar_probe,
3487 .remove = gfar_remove,
3488};
3489
Axel Lindb62f682011-11-27 16:44:17 +00003490module_platform_driver(gfar_driver);